summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/r3/solaris/coredumper-solaris.cpp
blob: d1a24b2c7c9d380a666d3e7d6e01f58b18262fff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
/* $Id: coredumper-solaris.cpp $ */
/** @file
 * IPRT - Custom Core Dumper, Solaris.
 */

/*
 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_DEFAULT
#include <iprt/coredumper.h>

#include <iprt/asm.h>
#include <iprt/dir.h>
#include <iprt/err.h>
#include <iprt/log.h>
#include <iprt/param.h>
#include <iprt/path.h>
#include <iprt/process.h>
#include <iprt/string.h>
#include <iprt/thread.h>
#include "coredumper-solaris.h"

#ifdef RT_OS_SOLARIS
# include <syslog.h>
# include <signal.h>
# include <stdlib.h>
# include <unistd.h>
# include <errno.h>
# include <zone.h>
# include <sys/proc.h>
# include <sys/sysmacros.h>
# include <sys/systeminfo.h>
# include <sys/mman.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <ucontext.h>
#endif  /* RT_OS_SOLARIS */

#include <iprt/formats/elf.h>
#include <iprt/formats/elf64.h>


/*********************************************************************************************************************************
*   Globals                                                                                                                      *
*********************************************************************************************************************************/
static RTNATIVETHREAD volatile  g_CoreDumpThread             = NIL_RTNATIVETHREAD;
static bool volatile            g_fCoreDumpSignalSetup       = false;
static uint32_t volatile        g_fCoreDumpFlags             = 0;
static char                     g_szCoreDumpDir[PATH_MAX]    = { 0 };
static char                     g_szCoreDumpFile[PATH_MAX]   = { 0 };


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
#define CORELOG_NAME        "CoreDumper: "
#define CORELOG(a)          Log(a)
#define CORELOGRELSYS(a)       \
    do { \
        rtCoreDumperSysLogWrapper a; \
    } while (0)


/**
 * ELFNOTEHDR: ELF NOTE header.
 */
typedef struct ELFNOTEHDR
{
    Elf64_Nhdr                      Hdr;                        /* Header of NOTE section */
    char                            achName[8];                 /* Name of NOTE section */
} ELFNOTEHDR;
typedef ELFNOTEHDR *PELFNOTEHDR;

/**
 * Wrapper function to write IPRT format style string to the syslog.
 *
 * @param pszFormat         Format string
 */
static void rtCoreDumperSysLogWrapper(const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    char szBuf[1024];
    RTStrPrintfV(szBuf, sizeof(szBuf), pszFormat, va);
    va_end(va);
    syslog(LOG_ERR, "%s", szBuf);
}


/**
 * Determines endianness of the system. Just for completeness.
 *
 * @return Will return false if system is little endian, true otherwise.
 */
static bool IsBigEndian()
{
    const int i = 1;
    char *p = (char *)&i;
    if (p[0] == 1)
        return false;
    return true;
}


/**
 * Reads from a file making sure an interruption doesn't cause a failure.
 *
 * @param fd                Handle to the file to read.
 * @param pv                Where to store the read data.
 * @param cbToRead          Size of data to read.
 *
 * @return IPRT status code.
 */
static int ReadFileNoIntr(int fd, void *pv, size_t cbToRead)
{
    for (;;)
    {
        ssize_t cbRead = read(fd, pv, cbToRead);
        if (cbRead < 0)
        {
            if (errno == EINTR)
                continue;
            return RTErrConvertFromErrno(errno);
        }
        if ((size_t)cbRead == cbToRead)
            return VINF_SUCCESS;
        if ((size_t)cbRead > cbToRead)
            return VERR_INTERNAL_ERROR_3;
        if (cbRead == 0)
            return VERR_EOF;
        pv = (uint8_t *)pv + cbRead;
        cbToRead -= cbRead;
    }
}


/**
 * Writes to a file making sure an interruption doesn't cause a failure.
 *
 * @param fd                Handle to the file to write to.
 * @param pv                Pointer to what to write.
 * @param cbToWrite         Size of data to write.
 *
 * @return IPRT status code.
 */
static int WriteFileNoIntr(int fd, const void *pv, size_t cbToWrite)
{
    for (;;)
    {
        ssize_t cbWritten = write(fd, pv, cbToWrite);
        if (cbWritten < 0)
        {
            if (errno == EINTR)
                continue;
            return RTErrConvertFromErrno(errno);
        }
        if ((size_t)cbWritten == cbToWrite)
            return VINF_SUCCESS;
        if ((size_t)cbWritten > cbToWrite)
            return VERR_INTERNAL_ERROR_2;
        pv = (uint8_t const *)pv + cbWritten;
        cbToWrite -= cbWritten;
    }
}


/**
 * Read from a given offset in the process' address space.
 *
 * @param pSolProc         Pointer to the solaris process.
 * @param off              The offset to read from.
 * @param pvBuf            Where to read the data into.
 * @param cbToRead         Number of bytes to read.
 *
 * @return VINF_SUCCESS, if all the given bytes was read in, otherwise VERR_READ_ERROR.
 */
static ssize_t ProcReadAddrSpace(PRTSOLCOREPROCESS pSolProc, RTFOFF off, void *pvBuf, size_t cbToRead)
{
    for (;;)
    {
        ssize_t cbRead = pread(pSolProc->fdAs, pvBuf, cbToRead, off);
        if (cbRead < 0)
        {
            if (errno == EINTR)
                continue;
            return RTErrConvertFromErrno(errno);
        }
        if ((size_t)cbRead == cbToRead)
            return VINF_SUCCESS;
        if ((size_t)cbRead > cbToRead)
            return VERR_INTERNAL_ERROR_4;
        if (cbRead == 0)
            return VERR_EOF;

        pvBuf     = (uint8_t *)pvBuf + cbRead;
        cbToRead -= cbRead;
        off      += cbRead;
    }
}


/**
 * Determines if the current process' architecture is suitable for dumping core.
 *
 * @param pSolProc         Pointer to the solaris process.
 *
 * @return true if the architecture matches the current one.
 */
static inline bool IsProcessArchNative(PRTSOLCOREPROCESS pSolProc)
{
    psinfo_t *pProcInfo = (psinfo_t *)pSolProc->pvProcInfo;
    return pProcInfo->pr_dmodel == PR_MODEL_NATIVE;
}


/**
 * Helper function to get the size_t compatible file size from a file
 * descriptor.
 *
 * @return  The file size (in bytes).
 * @param   fd              The file descriptor.
 */
static size_t GetFileSizeByFd(int fd)
{
    struct stat st;
    if (fstat(fd, &st) == 0)
    {
        if (st.st_size <= 0)
            return 0;
        size_t cbFile = (size_t)st.st_size;
        return (off_t)cbFile == st.st_size ? cbFile : ~(size_t)0;
    }

    CORELOGRELSYS((CORELOG_NAME "GetFileSizeByFd: fstat failed rc=%Rrc\n", RTErrConvertFromErrno(errno)));
    return 0;
}


/**
 * Helper function to get the size_t compatible size of a file given its path.
 *
 * @return  The file size (in bytes).
 * @param   pszPath         Pointer to the full path of the file.
 */
static size_t GetFileSizeByName(const char *pszPath)
{
    int fd = open(pszPath, O_RDONLY);
    if (fd < 0)
    {
        CORELOGRELSYS((CORELOG_NAME "GetFileSizeByName: failed to open %s rc=%Rrc\n", pszPath, RTErrConvertFromErrno(errno)));
        return 0;
    }

    size_t cb = GetFileSizeByFd(fd);
    close(fd);
    return cb;
}


/**
 * Pre-compute and pre-allocate sufficient memory for dumping core.
 * This is meant to be called once, as a single-large anonymously
 * mapped memory area which will be used during the core dumping routines.
 *
 * @param pSolCore          Pointer to the core object.
 *
 * @return IPRT status code.
 */
static int AllocMemoryArea(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore->pvCore == NULL, VERR_ALREADY_EXISTS);

    static struct
    {
        const char *pszFilePath;        /* Proc based path */
        size_t      cbHeader;           /* Size of header */
        size_t      cbEntry;            /* Size of each entry in file */
        size_t      cbAccounting;       /* Size of each accounting entry per entry */
    } const s_aPreAllocTable[] =
    {
        { "/proc/%d/psinfo",     0,                  0,                     0 },
        { "/proc/%d/map",        0,                  sizeof(prmap_t),       sizeof(RTSOLCOREMAPINFO) },
        { "/proc/%d/auxv",       0,                  0,                     0 },
        { "/proc/%d/lpsinfo",    sizeof(prheader_t), sizeof(lwpsinfo_t),    sizeof(RTSOLCORETHREADINFO) },
        { "/proc/%d/lstatus",    0,                  0,                     0 },
        { "/proc/%d/ldt",        0,                  0,                     0 },
        { "/proc/%d/cred",       sizeof(prcred_t),   sizeof(gid_t),         0 },
        { "/proc/%d/priv",       sizeof(prpriv_t),   sizeof(priv_chunk_t),  0 },
    };

    size_t cb = 0;
    for (unsigned i = 0; i < RT_ELEMENTS(s_aPreAllocTable); i++)
    {
        char szPath[PATH_MAX];
        RTStrPrintf(szPath, sizeof(szPath), s_aPreAllocTable[i].pszFilePath, (int)pSolCore->SolProc.Process);
        size_t cbFile = GetFileSizeByName(szPath);
        cb += cbFile;
        if (   cbFile > 0
            && s_aPreAllocTable[i].cbEntry > 0)
        {
            cb += ((cbFile - s_aPreAllocTable[i].cbHeader) / s_aPreAllocTable[i].cbEntry)
                * (s_aPreAllocTable[i].cbAccounting > 0 ? s_aPreAllocTable[i].cbAccounting : 1);
            cb += s_aPreAllocTable[i].cbHeader;
        }
    }

    /*
     * Make room for our own mapping accountant entry which will also be included in the core.
     */
    cb += sizeof(RTSOLCOREMAPINFO);

    /*
     * Allocate the required space, plus some extra room.
     */
    cb += _128K;
    void *pv = mmap(NULL, cb, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1 /* fd */, 0 /* offset */);
    if (pv != MAP_FAILED)
    {
        CORELOG((CORELOG_NAME "AllocMemoryArea: memory area of %u bytes allocated.\n", cb));
        pSolCore->pvCore = pv;
        pSolCore->pvFree = pv;
        pSolCore->cbCore = cb;
        return VINF_SUCCESS;
    }
    CORELOGRELSYS((CORELOG_NAME "AllocMemoryArea: failed cb=%u\n", cb));
    return VERR_NO_MEMORY;
}


/**
 * Free memory area used by the core object.
 *
 * @param pSolCore          Pointer to the core object.
 */
static void FreeMemoryArea(PRTSOLCORE pSolCore)
{
    AssertReturnVoid(pSolCore);
    AssertReturnVoid(pSolCore->pvCore);
    AssertReturnVoid(pSolCore->cbCore > 0);

    munmap(pSolCore->pvCore, pSolCore->cbCore);
    CORELOG((CORELOG_NAME "FreeMemoryArea: memory area of %u bytes freed.\n", pSolCore->cbCore));

    pSolCore->pvCore = NULL;
    pSolCore->pvFree= NULL;
    pSolCore->cbCore = 0;
}


/**
 * Get a chunk from the area of allocated memory.
 *
 * @param pSolCore          Pointer to the core object.
 * @param cb                Size of requested chunk.
 *
 * @return Pointer to allocated memory, or NULL on failure.
 */
static void *GetMemoryChunk(PRTSOLCORE pSolCore, size_t cb)
{
    AssertReturn(pSolCore, NULL);
    AssertReturn(pSolCore->pvCore, NULL);
    AssertReturn(pSolCore->pvFree, NULL);

    size_t cbAllocated = (char *)pSolCore->pvFree - (char *)pSolCore->pvCore;
    if (cbAllocated < pSolCore->cbCore)
    {
        char *pb = (char *)pSolCore->pvFree;
        pSolCore->pvFree = pb + cb;
        return pb;
    }

    return NULL;
}


/**
 * Reads the proc file's content into a newly allocated buffer.
 *
 * @param pSolCore          Pointer to the core object.
 * @param pszProcFileName   Only the name of the file to read from
 *                          (/proc/\<pid\> will be prepended)
 * @param ppv               Where to store the allocated buffer.
 * @param pcb               Where to store size of the buffer.
 *
 * @return IPRT status code. If the proc file is 0 bytes, VINF_SUCCESS is
 *          returned with pointed to values of @c ppv, @c pcb set to NULL and 0
 *          respectively.
 */
static int ProcReadFileInto(PRTSOLCORE pSolCore, const char *pszProcFileName, void **ppv, size_t *pcb)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    char szPath[PATH_MAX];
    RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/%s", (int)pSolCore->SolProc.Process, pszProcFileName);
    int rc = VINF_SUCCESS;
    int fd = open(szPath, O_RDONLY);
    if (fd >= 0)
    {
        *pcb = GetFileSizeByFd(fd);
        if (*pcb > 0)
        {
            *ppv = GetMemoryChunk(pSolCore, *pcb);
            if (*ppv)
                rc = ReadFileNoIntr(fd, *ppv, *pcb);
            else
                rc = VERR_NO_MEMORY;
        }
        else
        {
            *pcb =  0;
            *ppv = NULL;
            rc = VINF_SUCCESS;
        }
        close(fd);
    }
    else
    {
        rc = RTErrConvertFromErrno(fd);
        CORELOGRELSYS((CORELOG_NAME "ProcReadFileInto: failed to open %s. rc=%Rrc\n", szPath, rc));
    }
    return rc;
}


/**
 * Read process information (format psinfo_t) from /proc.
 *
 * @param pSolCore          Pointer to the core object.
 *
 * @return IPRT status code.
 */
static int ProcReadInfo(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    return ProcReadFileInto(pSolCore, "psinfo", &pSolProc->pvProcInfo, &pSolProc->cbProcInfo);
}


/**
 * Read process status (format pstatus_t) from /proc.
 *
 * @param pSolCore          Pointer to the core object.
 *
 * @return IPRT status code.
 */
static int ProcReadStatus(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;

    char szPath[PATH_MAX];
    int rc = VINF_SUCCESS;

    RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/status", (int)pSolProc->Process);
    int fd = open(szPath, O_RDONLY);
    if (fd >= 0)
    {
        size_t cbProcStatus = sizeof(pstatus_t);
        AssertCompile(sizeof(pstatus_t) == sizeof(pSolProc->ProcStatus));
        rc = ReadFileNoIntr(fd, &pSolProc->ProcStatus, cbProcStatus);
        close(fd);
    }
    else
    {
        rc = RTErrConvertFromErrno(fd);
        CORELOGRELSYS((CORELOG_NAME "ProcReadStatus: failed to open %s. rc=%Rrc\n", szPath, rc));
    }
    return rc;
}


/**
 * Read process credential information (format prcred_t + array of guid_t)
 *
 * @return IPRT status code.
 * @param pSolCore          Pointer to the core object.
 *
 * @remarks Should not be called before successful call to @see AllocMemoryArea()
 */
static int ProcReadCred(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    return ProcReadFileInto(pSolCore, "cred", &pSolProc->pvCred, &pSolProc->cbCred);
}


/**
 * Read process privilege information (format prpriv_t + array of priv_chunk_t)
 *
 * @return IPRT status code.
 * @param pSolCore          Pointer to the core object.
 *
 * @remarks Should not be called before successful call to @see AllocMemoryArea()
 */
static int ProcReadPriv(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    int rc = ProcReadFileInto(pSolCore, "priv", (void **)&pSolProc->pPriv, &pSolProc->cbPriv);
    if (RT_FAILURE(rc))
        return rc;
    pSolProc->pcPrivImpl = getprivimplinfo();
    if (!pSolProc->pcPrivImpl)
    {
        CORELOGRELSYS((CORELOG_NAME "ProcReadPriv: getprivimplinfo returned NULL.\n"));
        return VERR_INVALID_STATE;
    }
    return rc;
}


/**
 * Read process LDT information (format array of struct ssd) from /proc.
 *
 * @return  IPRT status code.
 * @param   pSolCore        Pointer to the core object.
 *
 * @remarks Should not be called before successful call to @see AllocMemoryArea()
 */
static int ProcReadLdt(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    return ProcReadFileInto(pSolCore, "ldt", &pSolProc->pvLdt, &pSolProc->cbLdt);
}


/**
 * Read process auxiliary vectors (format auxv_t) for the process.
 *
 * @return IPRT status code.
 * @param pSolCore          Pointer to the core object.
 *
 * @remarks Should not be called before successful call to @see AllocMemoryArea()
 */
static int ProcReadAuxVecs(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    char szPath[PATH_MAX];
    int rc = VINF_SUCCESS;
    RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/auxv", (int)pSolProc->Process);
    int fd = open(szPath, O_RDONLY);
    if (fd < 0)
    {
        rc = RTErrConvertFromErrno(fd);
        CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: failed to open %s rc=%Rrc\n", szPath, rc));
        return rc;
    }

    size_t cbAuxFile = GetFileSizeByFd(fd);
    if (cbAuxFile >= sizeof(auxv_t))
    {
        pSolProc->pAuxVecs = (auxv_t*)GetMemoryChunk(pSolCore, cbAuxFile + sizeof(auxv_t));
        if (pSolProc->pAuxVecs)
        {
            rc = ReadFileNoIntr(fd, pSolProc->pAuxVecs, cbAuxFile);
            if (RT_SUCCESS(rc))
            {
                /* Terminate list of vectors */
                pSolProc->cAuxVecs = cbAuxFile / sizeof(auxv_t);
                CORELOG((CORELOG_NAME "ProcReadAuxVecs: cbAuxFile=%u auxv_t size %d cAuxVecs=%u\n", cbAuxFile, sizeof(auxv_t),
                         pSolProc->cAuxVecs));
                if (pSolProc->cAuxVecs > 0)
                {
                    pSolProc->pAuxVecs[pSolProc->cAuxVecs].a_type = AT_NULL;
                    pSolProc->pAuxVecs[pSolProc->cAuxVecs].a_un.a_val = 0L;
                    close(fd);
                    return VINF_SUCCESS;
                }

                CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: Invalid vector count %u\n", pSolProc->cAuxVecs));
                rc = VERR_READ_ERROR;
            }
            else
                CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: ReadFileNoIntr failed. rc=%Rrc cbAuxFile=%u\n", rc, cbAuxFile));

            pSolProc->pAuxVecs = NULL;
            pSolProc->cAuxVecs = 0;
        }
        else
        {
            CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: no memory for %u bytes\n", cbAuxFile + sizeof(auxv_t)));
            rc = VERR_NO_MEMORY;
        }
    }
    else
    {
        CORELOGRELSYS((CORELOG_NAME "ProcReadAuxVecs: aux file too small %u, expecting %u or more\n", cbAuxFile, sizeof(auxv_t)));
        rc = VERR_READ_ERROR;
    }

    close(fd);
    return rc;
}


/*
 * Find an element in the process' auxiliary vector.
 */
static long GetAuxVal(PRTSOLCOREPROCESS pSolProc, int Type)
{
    AssertReturn(pSolProc, -1);
    if (pSolProc->pAuxVecs)
    {
        auxv_t *pAuxVec = pSolProc->pAuxVecs;
        for (; pAuxVec->a_type != AT_NULL; pAuxVec++)
        {
            if (pAuxVec->a_type == Type)
                return pAuxVec->a_un.a_val;
        }
    }
    return -1;
}


/**
 * Read the process mappings (format prmap_t array).
 *
 * @return IPRT status code.
 * @param   pSolCore            Pointer to the core object.
 *
 * @remarks Should not be called before successful call to @see AllocMemoryArea()
 */
static int ProcReadMappings(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    char szPath[PATH_MAX];
    int rc = VINF_SUCCESS;
    RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/map", (int)pSolProc->Process);
    int fdMap = open(szPath, O_RDONLY);
    if (fdMap < 0)
    {
        rc = RTErrConvertFromErrno(errno);
        CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: failed to open %s. rc=%Rrc\n", szPath, rc));
        return rc;
    }

    RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/as", (int)pSolProc->Process);
    pSolProc->fdAs = open(szPath, O_RDONLY);
    if (pSolProc->fdAs >= 0)
    {
        /*
         * Allocate and read all the prmap_t objects from proc.
         */
        size_t cbMapFile = GetFileSizeByFd(fdMap);
        if (cbMapFile >= sizeof(prmap_t))
        {
            prmap_t *pMap = (prmap_t*)GetMemoryChunk(pSolCore, cbMapFile);
            if (pMap)
            {
                rc = ReadFileNoIntr(fdMap, pMap, cbMapFile);
                if (RT_SUCCESS(rc))
                {
                    pSolProc->cMappings = cbMapFile / sizeof(prmap_t);
                    if (pSolProc->cMappings > 0)
                    {
                        /*
                         * Allocate for each prmap_t object, a corresponding RTSOLCOREMAPINFO object.
                         */
                        pSolProc->pMapInfoHead = (PRTSOLCOREMAPINFO)GetMemoryChunk(pSolCore,
                                                                                pSolProc->cMappings * sizeof(RTSOLCOREMAPINFO));
                        if (pSolProc->pMapInfoHead)
                        {
                            /*
                             * Associate the prmap_t with the mapping info object.
                             */
                            /*Assert(pSolProc->pMapInfoHead == NULL); - does not make sense */
                            PRTSOLCOREMAPINFO pCur = pSolProc->pMapInfoHead;
                            PRTSOLCOREMAPINFO pPrev = NULL;
                            for (uint64_t i = 0; i < pSolProc->cMappings; i++, pMap++, pCur++)
                            {
                                memcpy(&pCur->pMap, pMap, sizeof(pCur->pMap));
                                if (pPrev)
                                    pPrev->pNext = pCur;

                                pCur->fError = 0;

                                /*
                                 * Make sure we can read the mapping, otherwise mark them to be skipped.
                                 */
                                char achBuf[PAGE_SIZE];
                                uint64_t k = 0;
                                while (k < pCur->pMap.pr_size)
                                {
                                    size_t cb = RT_MIN(sizeof(achBuf), pCur->pMap.pr_size - k);
                                    int rc2 = ProcReadAddrSpace(pSolProc, pCur->pMap.pr_vaddr + k, &achBuf, cb);
                                    if (RT_FAILURE(rc2))
                                    {
                                        CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: skipping mapping. vaddr=%#x rc=%Rrc\n",
                                                       pCur->pMap.pr_vaddr, rc2));

                                        /*
                                         * Instead of storing the actual mapping data which we failed to read, the core
                                         * will contain an errno in place. So we adjust the prmap_t's size field too
                                         * so the program header offsets match.
                                         */
                                        pCur->pMap.pr_size = RT_ALIGN_Z(sizeof(int), 8);
                                        pCur->fError = errno;
                                        if (pCur->fError == 0)  /* huh!? somehow errno got reset? fake one! EFAULT is nice. */
                                            pCur->fError = EFAULT;
                                        break;
                                    }
                                    k += cb;
                                }

                                pPrev = pCur;
                            }
                            if (pPrev)
                                pPrev->pNext = NULL;

                            close(fdMap);
                            close(pSolProc->fdAs);
                            pSolProc->fdAs = -1;
                            CORELOG((CORELOG_NAME "ProcReadMappings: successfully read in %u mappings\n", pSolProc->cMappings));
                            return VINF_SUCCESS;
                        }

                        CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: GetMemoryChunk failed %u\n",
                                       pSolProc->cMappings * sizeof(RTSOLCOREMAPINFO)));
                        rc = VERR_NO_MEMORY;
                    }
                    else
                    {
                        CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: Invalid mapping count %u\n", pSolProc->cMappings));
                        rc = VERR_READ_ERROR;
                    }
                }
                else
                {
                    CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: FileReadNoIntr failed. rc=%Rrc cbMapFile=%u\n", rc,
                                   cbMapFile));
                }
            }
            else
            {
                CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: GetMemoryChunk failed. cbMapFile=%u\n", cbMapFile));
                rc = VERR_NO_MEMORY;
            }
        }

        close(pSolProc->fdAs);
        pSolProc->fdAs = -1;
    }
    else
        CORELOGRELSYS((CORELOG_NAME "ProcReadMappings: failed to open %s. rc=%Rrc\n", szPath, rc));

    close(fdMap);
    return rc;
}


/**
 * Reads the thread information for all threads in the process.
 *
 * @return IPRT status code.
 * @param pSolCore          Pointer to the core object.
 *
 * @remarks Should not be called before successful call to @see AllocMemoryArea()
 */
static int ProcReadThreads(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    AssertReturn(pSolProc->pCurThreadCtx, VERR_NO_DATA);

    /*
     * Read the information for threads.
     * Format: prheader_t + array of lwpsinfo_t's.
     */
    size_t cbInfoHdrAndData;
    void *pvInfoHdr = NULL;
    int rc = ProcReadFileInto(pSolCore, "lpsinfo", &pvInfoHdr, &cbInfoHdrAndData);
    if (RT_SUCCESS(rc))
    {
        /*
         * Read the status of threads.
         * Format: prheader_t + array of lwpstatus_t's.
         */
        void *pvStatusHdr = NULL;
        size_t cbStatusHdrAndData;
        rc = ProcReadFileInto(pSolCore, "lstatus", &pvStatusHdr, &cbStatusHdrAndData);
        if (RT_SUCCESS(rc))
        {
            prheader_t *pInfoHdr   = (prheader_t *)pvInfoHdr;
            prheader_t *pStatusHdr = (prheader_t *)pvStatusHdr;
            lwpstatus_t *pStatus   = (lwpstatus_t *)((uintptr_t)pStatusHdr + sizeof(prheader_t));
            lwpsinfo_t *pInfo      = (lwpsinfo_t *)((uintptr_t)pInfoHdr + sizeof(prheader_t));
            uint64_t cStatus       = pStatusHdr->pr_nent;
            uint64_t cInfo         = pInfoHdr->pr_nent;

            CORELOG((CORELOG_NAME "ProcReadThreads: read info(%u) status(%u), threads:cInfo=%u cStatus=%u\n", cbInfoHdrAndData,
                        cbStatusHdrAndData, cInfo, cStatus));

            /*
             * Minor sanity size check (remember sizeof lwpstatus_t & lwpsinfo_t is <= size in file per entry).
             */
            if (   (cbStatusHdrAndData - sizeof(prheader_t)) % pStatusHdr->pr_entsize == 0
                && (cbInfoHdrAndData - sizeof(prheader_t)) % pInfoHdr->pr_entsize == 0)
            {
                /*
                 * Make sure we have a matching lstatus entry for an lpsinfo entry unless
                 * it is a zombie thread, in which case we will not have a matching lstatus entry.
                 */
                for (; cInfo != 0; cInfo--)
                {
                    if (pInfo->pr_sname != 'Z') /* zombie */
                    {
                        if (   cStatus == 0
                            || pStatus->pr_lwpid != pInfo->pr_lwpid)
                        {
                            CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: cStatus = %u pStatuslwpid=%d infolwpid=%d\n", cStatus,
                                        pStatus->pr_lwpid, pInfo->pr_lwpid));
                            rc = VERR_INVALID_STATE;
                            break;
                        }
                        pStatus = (lwpstatus_t *)((uintptr_t)pStatus + pStatusHdr->pr_entsize);
                        cStatus--;
                    }
                    pInfo = (lwpsinfo_t *)((uintptr_t)pInfo + pInfoHdr->pr_entsize);
                }

                if (RT_SUCCESS(rc))
                {
                    /*
                     * There can still be more lwpsinfo_t's than lwpstatus_t's, build the
                     * lists accordingly.
                     */
                    pStatus = (lwpstatus_t *)((uintptr_t)pStatusHdr + sizeof(prheader_t));
                    pInfo = (lwpsinfo_t *)((uintptr_t)pInfoHdr + sizeof(prheader_t));
                    cInfo = pInfoHdr->pr_nent;
                    cStatus = pInfoHdr->pr_nent;

                    size_t cbThreadInfo = RT_MAX(cStatus, cInfo) * sizeof(RTSOLCORETHREADINFO);
                    pSolProc->pThreadInfoHead = (PRTSOLCORETHREADINFO)GetMemoryChunk(pSolCore, cbThreadInfo);
                    if (pSolProc->pThreadInfoHead)
                    {
                        PRTSOLCORETHREADINFO pCur = pSolProc->pThreadInfoHead;
                        PRTSOLCORETHREADINFO pPrev = NULL;
                        for (uint64_t i = 0; i < cInfo; i++, pCur++)
                        {
                            pCur->Info = *pInfo;
                            if (   pInfo->pr_sname != 'Z'
                                && pInfo->pr_lwpid == pStatus->pr_lwpid)
                            {
                                /*
                                 * Adjust the context of the dumping thread to reflect the context
                                 * when the core dump got initiated before whatever signal caused it.
                                 */
                                if (   pStatus          /* noid droid */
                                    && pStatus->pr_lwpid == (id_t)pSolProc->hCurThread)
                                {
                                    AssertCompile(sizeof(pStatus->pr_reg) == sizeof(pSolProc->pCurThreadCtx->uc_mcontext.gregs));
                                    AssertCompile(sizeof(pStatus->pr_fpreg) == sizeof(pSolProc->pCurThreadCtx->uc_mcontext.fpregs));
                                    memcpy(&pStatus->pr_reg, &pSolProc->pCurThreadCtx->uc_mcontext.gregs, sizeof(pStatus->pr_reg));
                                    memcpy(&pStatus->pr_fpreg, &pSolProc->pCurThreadCtx->uc_mcontext.fpregs, sizeof(pStatus->pr_fpreg));

                                    AssertCompile(sizeof(pStatus->pr_lwphold) == sizeof(pSolProc->pCurThreadCtx->uc_sigmask));
                                    memcpy(&pStatus->pr_lwphold, &pSolProc->pCurThreadCtx->uc_sigmask, sizeof(pStatus->pr_lwphold));
                                    pStatus->pr_ustack = (uintptr_t)&pSolProc->pCurThreadCtx->uc_stack;

                                    CORELOG((CORELOG_NAME "ProcReadThreads: patched dumper thread with pre-dump time context.\n"));
                                }

                                pCur->pStatus = pStatus;
                                pStatus = (lwpstatus_t *)((uintptr_t)pStatus + pStatusHdr->pr_entsize);
                            }
                            else
                            {
                                CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: missing status for lwp %d\n", pInfo->pr_lwpid));
                                pCur->pStatus = NULL;
                            }

                            if (pPrev)
                                pPrev->pNext = pCur;
                            pPrev = pCur;
                            pInfo = (lwpsinfo_t *)((uintptr_t)pInfo + pInfoHdr->pr_entsize);
                        }
                        if (pPrev)
                            pPrev->pNext = NULL;

                        CORELOG((CORELOG_NAME "ProcReadThreads: successfully read %u threads.\n", cInfo));
                        pSolProc->cThreads = cInfo;
                        return VINF_SUCCESS;
                    }
                    else
                    {
                        CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: GetMemoryChunk failed for %u bytes\n", cbThreadInfo));
                        rc = VERR_NO_MEMORY;
                    }
                }
                else
                    CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: Invalid state information for threads. rc=%Rrc\n", rc));
            }
            else
            {
                CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: huh!? cbStatusHdrAndData=%u prheader_t=%u entsize=%u\n",
                               cbStatusHdrAndData, sizeof(prheader_t), pStatusHdr->pr_entsize));
                CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: huh!? cbInfoHdrAndData=%u entsize=%u\n", cbInfoHdrAndData,
                               pStatusHdr->pr_entsize));
                rc = VERR_INVALID_STATE;
            }
        }
        else
            CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: ReadFileNoIntr failed for \"lpsinfo\" rc=%Rrc\n", rc));
    }
    else
        CORELOGRELSYS((CORELOG_NAME "ProcReadThreads: ReadFileNoIntr failed for \"lstatus\" rc=%Rrc\n", rc));
    return rc;
}


/**
 * Reads miscellaneous information that is collected as part of a core file.
 * This may include platform name, zone name and other OS-specific information.
 *
 * @param pSolCore          Pointer to the core object.
 *
 * @return IPRT status code.
 */
static int ProcReadMiscInfo(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;

#ifdef RT_OS_SOLARIS
    /*
     * Read the platform name, uname string and zone name.
     */
    int rc = sysinfo(SI_PLATFORM, pSolProc->szPlatform, sizeof(pSolProc->szPlatform));
    if (rc == -1)
    {
        CORELOGRELSYS((CORELOG_NAME "ProcReadMiscInfo: sysinfo failed. rc=%d errno=%d\n", rc, errno));
        return VERR_GENERAL_FAILURE;
    }
    pSolProc->szPlatform[sizeof(pSolProc->szPlatform) - 1] = '\0';

    rc = uname(&pSolProc->UtsName);
    if (rc == -1)
    {
        CORELOGRELSYS((CORELOG_NAME "ProcReadMiscInfo: uname failed. rc=%d errno=%d\n", rc, errno));
        return VERR_GENERAL_FAILURE;
    }

    /*
     * See comment in GetOldProcessInfo() for why we need to verify the offset here.
     * It's not perfect, but it should be fine unless they really mess up the structure
     * layout in the future. See @bugref{8479}.
     */
    size_t const offZoneId = RT_UOFFSETOF(psinfo_t, pr_zoneid);
    if (pSolProc->cbProcInfo < offZoneId)
    {
        CORELOGRELSYS((CORELOG_NAME "ProcReadMiscInfo: psinfo size mismatch. cbProcInfo=%u expected >= %u\n",
                       pSolProc->cbProcInfo, offZoneId));
        return VERR_GENERAL_FAILURE;
    }

    psinfo_t *pProcInfo = (psinfo_t *)pSolProc->pvProcInfo;
    rc = getzonenamebyid(pProcInfo->pr_zoneid, pSolProc->szZoneName, sizeof(pSolProc->szZoneName));
    if (rc < 0)
    {
        CORELOGRELSYS((CORELOG_NAME "ProcReadMiscInfo: getzonenamebyid failed. rc=%d errno=%d zoneid=%d\n", rc, errno,
                       pProcInfo->pr_zoneid));
        return VERR_GENERAL_FAILURE;
    }
    pSolProc->szZoneName[sizeof(pSolProc->szZoneName) - 1] = '\0';
    rc = VINF_SUCCESS;

#else
# error Port Me!
#endif
    return rc;
}


/**
 * On Solaris use the old-style procfs interfaces but the core file still should have this
 * info. for backward and GDB compatibility, hence the need for this ugly function.
 *
 * @returns IPRT status code.
 *
 * @param pSolCore          Pointer to the core object.
 * @param pInfo             Pointer to the old prpsinfo_t structure to update.
 */
static int GetOldProcessInfo(PRTSOLCORE pSolCore, prpsinfo_t *pInfo)
{
    AssertReturn(pSolCore, VERR_INVALID_PARAMETER);
    AssertReturn(pInfo,    VERR_INVALID_PARAMETER);

    /*
     * The psinfo_t and the size of /proc/<pid>/psinfo varies both within the same Solaris system
     * and across Solaris major versions. However, manual dumping of the structure and offsets shows
     * that they changed the size of lwpsinfo_t and the size of the lwpsinfo_t::pr_filler.
     *
     * The proc psinfo file will be what gets dumped ultimately in the core file but we still need
     * to read the fields to translate to the older process info structure here.
     *
     * See @bugref{8479}.
     */
    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;

    size_t offLwp         = RT_UOFFSETOF(psinfo_t, pr_lwp);
    /* last member we care about in lwpsinfo_t is pr_bindpset which is also present on ancient Solaris version we use for
       building the additions. Should be safe enough as we don't need/access members upto or beyond that point anyway. */
    size_t offLastOnProc  = RT_UOFFSETOF(lwpsinfo_t, pr_bindpset);
    if (pSolProc->cbProcInfo >= offLwp + offLastOnProc)
    {
        psinfo_t *pSrc = (psinfo_t *)pSolProc->pvProcInfo;
        memset(pInfo, 0, sizeof(prpsinfo_t));
        pInfo->pr_state    = pSrc->pr_lwp.pr_state;
        pInfo->pr_zomb     = (pInfo->pr_state == SZOMB);
        RTStrCopy(pInfo->pr_clname, sizeof(pInfo->pr_clname), pSrc->pr_lwp.pr_clname);
        RTStrCopy(pInfo->pr_fname, sizeof(pInfo->pr_fname), pSrc->pr_fname);
        memcpy(&pInfo->pr_psargs, &pSrc->pr_psargs, sizeof(pInfo->pr_psargs));
        pInfo->pr_nice     = pSrc->pr_lwp.pr_nice;
        pInfo->pr_flag     = pSrc->pr_lwp.pr_flag;
        pInfo->pr_uid      = pSrc->pr_uid;
        pInfo->pr_gid      = pSrc->pr_gid;
        pInfo->pr_pid      = pSrc->pr_pid;
        pInfo->pr_ppid     = pSrc->pr_ppid;
        pInfo->pr_pgrp     = pSrc->pr_pgid;
        pInfo->pr_sid      = pSrc->pr_sid;
        pInfo->pr_addr     = (caddr_t)pSrc->pr_addr;
        pInfo->pr_size     = pSrc->pr_size;
        pInfo->pr_rssize   = pSrc->pr_rssize;
        pInfo->pr_wchan    = (caddr_t)pSrc->pr_lwp.pr_wchan;
        pInfo->pr_start    = pSrc->pr_start;
        pInfo->pr_time     = pSrc->pr_time;
        pInfo->pr_pri      = pSrc->pr_lwp.pr_pri;
        pInfo->pr_oldpri   = pSrc->pr_lwp.pr_oldpri;
        pInfo->pr_cpu      = pSrc->pr_lwp.pr_cpu;
        pInfo->pr_ottydev  = cmpdev(pSrc->pr_ttydev);
        pInfo->pr_lttydev  = pSrc->pr_ttydev;
        pInfo->pr_syscall  = pSrc->pr_lwp.pr_syscall;
        pInfo->pr_ctime    = pSrc->pr_ctime;
        pInfo->pr_bysize   = pSrc->pr_size * PAGESIZE;
        pInfo->pr_byrssize = pSrc->pr_rssize * PAGESIZE;
        pInfo->pr_argc     = pSrc->pr_argc;
        pInfo->pr_argv     = (char **)pSrc->pr_argv;
        pInfo->pr_envp     = (char **)pSrc->pr_envp;
        pInfo->pr_wstat    = pSrc->pr_wstat;
        pInfo->pr_pctcpu   = pSrc->pr_pctcpu;
        pInfo->pr_pctmem   = pSrc->pr_pctmem;
        pInfo->pr_euid     = pSrc->pr_euid;
        pInfo->pr_egid     = pSrc->pr_egid;
        pInfo->pr_aslwpid  = 0;
        pInfo->pr_dmodel   = pSrc->pr_dmodel;

        return VINF_SUCCESS;
    }

    CORELOGRELSYS((CORELOG_NAME "GetOldProcessInfo: Size/offset mismatch. offLwp=%u offLastOnProc=%u cbProcInfo=%u\n",
                   offLwp, offLastOnProc, pSolProc->cbProcInfo));
    return VERR_MISMATCH;
}


/**
 * On Solaris use the old-style procfs interfaces but the core file still should have this
 * info. for backward and GDB compatibility, hence the need for this ugly function.
 *
 * @param pSolCore          Pointer to the core object.
 * @param pInfo             Pointer to the thread info.
 * @param pStatus           Pointer to the thread status.
 * @param pDst              Pointer to the old-style status structure to update.
 *
 */
static void GetOldProcessStatus(PRTSOLCORE pSolCore, lwpsinfo_t *pInfo, lwpstatus_t *pStatus, prstatus_t *pDst)
{
    AssertReturnVoid(pSolCore);
    AssertReturnVoid(pInfo);
    AssertReturnVoid(pStatus);
    AssertReturnVoid(pDst);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    memset(pDst, 0, sizeof(prstatus_t));
    if (pStatus->pr_flags & PR_STOPPED)
        pDst->pr_flags = 0x0001;
    if (pStatus->pr_flags & PR_ISTOP)
        pDst->pr_flags = 0x0002;
    if (pStatus->pr_flags & PR_DSTOP)
        pDst->pr_flags = 0x0004;
    if (pStatus->pr_flags & PR_ASLEEP)
        pDst->pr_flags = 0x0008;
    if (pStatus->pr_flags & PR_FORK)
        pDst->pr_flags = 0x0010;
    if (pStatus->pr_flags & PR_RLC)
        pDst->pr_flags = 0x0020;
    /* PR_PTRACE is never set */
    if (pStatus->pr_flags & PR_PCINVAL)
        pDst->pr_flags = 0x0080;
    if (pStatus->pr_flags & PR_ISSYS)
        pDst->pr_flags = 0x0100;
    if (pStatus->pr_flags & PR_STEP)
        pDst->pr_flags = 0x0200;
    if (pStatus->pr_flags & PR_KLC)
        pDst->pr_flags = 0x0400;
    if (pStatus->pr_flags & PR_ASYNC)
        pDst->pr_flags = 0x0800;
    if (pStatus->pr_flags & PR_PTRACE)
        pDst->pr_flags = 0x1000;
    if (pStatus->pr_flags & PR_MSACCT)
        pDst->pr_flags = 0x2000;
    if (pStatus->pr_flags & PR_BPTADJ)
        pDst->pr_flags = 0x4000;
    if (pStatus->pr_flags & PR_ASLWP)
        pDst->pr_flags = 0x8000;

    pDst->pr_who        = pStatus->pr_lwpid;
    pDst->pr_why        = pStatus->pr_why;
    pDst->pr_what       = pStatus->pr_what;
    pDst->pr_info       = pStatus->pr_info;
    pDst->pr_cursig     = pStatus->pr_cursig;
    pDst->pr_sighold    = pStatus->pr_lwphold;
    pDst->pr_altstack   = pStatus->pr_altstack;
    pDst->pr_action     = pStatus->pr_action;
    pDst->pr_syscall    = pStatus->pr_syscall;
    pDst->pr_nsysarg    = pStatus->pr_nsysarg;
    pDst->pr_lwppend    = pStatus->pr_lwppend;
    pDst->pr_oldcontext = (ucontext_t *)pStatus->pr_oldcontext;
    memcpy(pDst->pr_reg, pStatus->pr_reg, sizeof(pDst->pr_reg));
    memcpy(pDst->pr_sysarg, pStatus->pr_sysarg, sizeof(pDst->pr_sysarg));
    RTStrCopy(pDst->pr_clname, sizeof(pDst->pr_clname), pStatus->pr_clname);

    pDst->pr_nlwp       = pSolProc->ProcStatus.pr_nlwp;
    pDst->pr_sigpend    = pSolProc->ProcStatus.pr_sigpend;
    pDst->pr_pid        = pSolProc->ProcStatus.pr_pid;
    pDst->pr_ppid       = pSolProc->ProcStatus.pr_ppid;
    pDst->pr_pgrp       = pSolProc->ProcStatus.pr_pgid;
    pDst->pr_sid        = pSolProc->ProcStatus.pr_sid;
    pDst->pr_utime      = pSolProc->ProcStatus.pr_utime;
    pDst->pr_stime      = pSolProc->ProcStatus.pr_stime;
    pDst->pr_cutime     = pSolProc->ProcStatus.pr_cutime;
    pDst->pr_cstime     = pSolProc->ProcStatus.pr_cstime;
    pDst->pr_brkbase    = (caddr_t)pSolProc->ProcStatus.pr_brkbase;
    pDst->pr_brksize    = pSolProc->ProcStatus.pr_brksize;
    pDst->pr_stkbase    = (caddr_t)pSolProc->ProcStatus.pr_stkbase;
    pDst->pr_stksize    = pSolProc->ProcStatus.pr_stksize;

    pDst->pr_processor  = (short)pInfo->pr_onpro;
    pDst->pr_bind       = (short)pInfo->pr_bindpro;
    pDst->pr_instr      = pStatus->pr_instr;
}


/**
 * Callback for rtCoreDumperForEachThread to suspend a thread.
 *
 * @param pSolCore              Pointer to the core object.
 * @param pvThreadInfo          Opaque pointer to thread information.
 *
 * @return IPRT status code.
 */
static int suspendThread(PRTSOLCORE pSolCore, void *pvThreadInfo)
{
    AssertPtrReturn(pvThreadInfo, VERR_INVALID_POINTER);
    NOREF(pSolCore);

    lwpsinfo_t *pThreadInfo = (lwpsinfo_t *)pvThreadInfo;
    CORELOG((CORELOG_NAME ":suspendThread %d\n", (lwpid_t)pThreadInfo->pr_lwpid));
    if ((lwpid_t)pThreadInfo->pr_lwpid != pSolCore->SolProc.hCurThread)
        _lwp_suspend(pThreadInfo->pr_lwpid);
    return VINF_SUCCESS;
}


/**
 * Callback for rtCoreDumperForEachThread to resume a thread.
 *
 * @param pSolCore              Pointer to the core object.
 * @param pvThreadInfo          Opaque pointer to thread information.
 *
 * @return IPRT status code.
 */
static int resumeThread(PRTSOLCORE pSolCore, void *pvThreadInfo)
{
    AssertPtrReturn(pvThreadInfo, VERR_INVALID_POINTER);
    NOREF(pSolCore);

    lwpsinfo_t *pThreadInfo = (lwpsinfo_t *)pvThreadInfo;
    CORELOG((CORELOG_NAME ":resumeThread %d\n", (lwpid_t)pThreadInfo->pr_lwpid));
    if ((lwpid_t)pThreadInfo->pr_lwpid != (lwpid_t)pSolCore->SolProc.hCurThread)
        _lwp_continue(pThreadInfo->pr_lwpid);
    return VINF_SUCCESS;
}


/**
 * Calls a thread worker function for all threads in the process as described by /proc
 *
 * @param pSolCore              Pointer to the core object.
 * @param pcThreads             Number of threads read.
 * @param pfnWorker             Callback function for each thread.
 *
 * @return IPRT status code.
 */
static int rtCoreDumperForEachThread(PRTSOLCORE pSolCore,  uint64_t *pcThreads, PFNRTSOLCORETHREADWORKER pfnWorker)
{
    AssertPtrReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;

    /*
     * Read the information for threads.
     * Format: prheader_t + array of lwpsinfo_t's.
     */
    char szLpsInfoPath[PATH_MAX];
    RTStrPrintf(szLpsInfoPath, sizeof(szLpsInfoPath), "/proc/%d/lpsinfo", (int)pSolProc->Process);

    int rc = VINF_SUCCESS;
    int fd = open(szLpsInfoPath, O_RDONLY);
    if (fd >= 0)
    {
        size_t cbInfoHdrAndData = GetFileSizeByFd(fd);
        void *pvInfoHdr = mmap(NULL, cbInfoHdrAndData, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
                               -1 /* fd */, 0 /* offset */);
        if (pvInfoHdr != MAP_FAILED)
        {
            rc = ReadFileNoIntr(fd, pvInfoHdr, cbInfoHdrAndData);
            if (RT_SUCCESS(rc))
            {
                prheader_t *pHeader = (prheader_t *)pvInfoHdr;
                lwpsinfo_t *pThreadInfo = (lwpsinfo_t *)((uintptr_t)pvInfoHdr + sizeof(prheader_t));
                for (long i = 0; i < pHeader->pr_nent; i++)
                {
                    pfnWorker(pSolCore, pThreadInfo);
                    pThreadInfo = (lwpsinfo_t *)((uintptr_t)pThreadInfo + pHeader->pr_entsize);
                }
                if (pcThreads)
                    *pcThreads = pHeader->pr_nent;
            }

            munmap(pvInfoHdr, cbInfoHdrAndData);
        }
        else
            rc = VERR_NO_MEMORY;
        close(fd);
    }
    else
        rc = RTErrConvertFromErrno(rc);

    return rc;
}


/**
 * Resume all threads of this process.
 *
 * @param pSolCore              Pointer to the core object.
 *
 * @return IPRT status code..
 */
static int rtCoreDumperResumeThreads(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    uint64_t cThreads;
    return rtCoreDumperForEachThread(pSolCore, &cThreads, resumeThread);
}


/**
 * Stop all running threads of this process except the current one.
 *
 * @param pSolCore          Pointer to the core object.
 *
 * @return IPRT status code.
 */
static int rtCoreDumperSuspendThreads(PRTSOLCORE pSolCore)
{
    AssertPtrReturn(pSolCore, VERR_INVALID_POINTER);

    /*
     * This function tries to ensures while we suspend threads, no newly spawned threads
     * or a combination of spawning and terminating threads can cause any threads to be left running.
     * The assumption here is that threads can only increase not decrease across iterations.
     */
    uint16_t cTries = 0;
    uint64_t aThreads[4];
    RT_ZERO(aThreads);
    int rc = VERR_GENERAL_FAILURE;
    void *pv = NULL;
    size_t cb = 0;
    for (cTries = 0; cTries < RT_ELEMENTS(aThreads); cTries++)
    {
        rc = rtCoreDumperForEachThread(pSolCore, &aThreads[cTries], suspendThread);
        if (RT_FAILURE(rc))
            break;
    }
    if (   RT_SUCCESS(rc)
        && aThreads[cTries - 1] != aThreads[cTries - 2])
    {
        CORELOGRELSYS((CORELOG_NAME "rtCoreDumperSuspendThreads: possible thread bomb!?\n"));
        rc = VERR_TIMEOUT;
    }
    return rc;
}


/**
 * Returns size of an ELF NOTE header given the size of data the NOTE section will contain.
 *
 * @param cb                Size of the data.
 *
 * @return Size of data actually used for NOTE header and section.
 */
static inline size_t ElfNoteHeaderSize(size_t cb)
{
    return sizeof(ELFNOTEHDR) + RT_ALIGN_Z(cb, 4);
}


/**
 * Write an ELF NOTE header into the core file.
 *
 * @param pSolCore          Pointer to the core object.
 * @param Type              Type of this NOTE section.
 * @param pcv               Opaque pointer to the data, if NULL only computes size.
 * @param cb                Size of the data.
 *
 * @return IPRT status code.
 */
static int ElfWriteNoteHeader(PRTSOLCORE pSolCore, uint_t Type, const void *pcv, size_t cb)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);
    AssertReturn(pcv, VERR_INVALID_POINTER);
    AssertReturn(cb > 0, VERR_NO_DATA);
    AssertReturn(pSolCore->pfnWriter, VERR_WRITE_ERROR);
    AssertReturn(pSolCore->fdCoreFile >= 0, VERR_INVALID_HANDLE);

    int rc = VERR_GENERAL_FAILURE;
#ifdef RT_OS_SOLARIS
    ELFNOTEHDR ElfNoteHdr;
    RT_ZERO(ElfNoteHdr);
    ElfNoteHdr.achName[0] = 'C';
    ElfNoteHdr.achName[1] = 'O';
    ElfNoteHdr.achName[2] = 'R';
    ElfNoteHdr.achName[3] = 'E';

    /*
     * This is a known violation of the 64-bit ELF spec., see xTracker @bugref{5211}
     * for the historic reasons as to the padding and 'namesz' anomalies.
     */
    static const char s_achPad[3] = { 0, 0, 0 };
    size_t cbAlign = RT_ALIGN_Z(cb, 4);
    ElfNoteHdr.Hdr.n_namesz = 5;
    ElfNoteHdr.Hdr.n_type = Type;
    ElfNoteHdr.Hdr.n_descsz = cbAlign;

    /*
     * Write note header and description.
     */
    rc = pSolCore->pfnWriter(pSolCore->fdCoreFile, &ElfNoteHdr, sizeof(ElfNoteHdr));
    if (RT_SUCCESS(rc))
    {
       rc = pSolCore->pfnWriter(pSolCore->fdCoreFile, pcv, cb);
       if (RT_SUCCESS(rc))
       {
           if (cbAlign > cb)
               rc = pSolCore->pfnWriter(pSolCore->fdCoreFile, s_achPad, cbAlign - cb);
       }
    }

    if (RT_FAILURE(rc))
        CORELOGRELSYS((CORELOG_NAME "ElfWriteNote: pfnWriter failed. Type=%d rc=%Rrc\n", Type, rc));
#else
#error Port Me!
#endif
    return rc;
}


/**
 * Computes the size of NOTE section for the given core type.
 * Solaris has two types of program header information (new and old).
 *
 * @param pSolCore          Pointer to the core object.
 * @param enmType           Type of core file information required.
 *
 * @return Size of NOTE section.
 */
static size_t ElfNoteSectionSize(PRTSOLCORE pSolCore, RTSOLCORETYPE enmType)
{
    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    size_t cb = 0;
    switch (enmType)
    {
        case enmOldEra:
        {
            cb += ElfNoteHeaderSize(sizeof(prpsinfo_t));
            cb += ElfNoteHeaderSize(pSolProc->cAuxVecs * sizeof(auxv_t));
            cb += ElfNoteHeaderSize(strlen(pSolProc->szPlatform));

            PRTSOLCORETHREADINFO pThreadInfo = pSolProc->pThreadInfoHead;
            while (pThreadInfo)
            {
                if (pThreadInfo->pStatus)
                {
                    cb += ElfNoteHeaderSize(sizeof(prstatus_t));
                    cb += ElfNoteHeaderSize(sizeof(prfpregset_t));
                }
                pThreadInfo = pThreadInfo->pNext;
            }

            break;
        }

        case enmNewEra:
        {
            cb += ElfNoteHeaderSize(sizeof(psinfo_t));
            cb += ElfNoteHeaderSize(sizeof(pstatus_t));
            cb += ElfNoteHeaderSize(pSolProc->cAuxVecs * sizeof(auxv_t));
            cb += ElfNoteHeaderSize(strlen(pSolProc->szPlatform) + 1);
            cb += ElfNoteHeaderSize(sizeof(struct utsname));
            cb += ElfNoteHeaderSize(sizeof(core_content_t));
            cb += ElfNoteHeaderSize(pSolProc->cbCred);

            if (pSolProc->pPriv)
                cb += ElfNoteHeaderSize(PRIV_PRPRIV_SIZE(pSolProc->pPriv));   /* Ought to be same as cbPriv!? */

            if (pSolProc->pcPrivImpl)
                cb += ElfNoteHeaderSize(PRIV_IMPL_INFO_SIZE(pSolProc->pcPrivImpl));

            cb += ElfNoteHeaderSize(strlen(pSolProc->szZoneName) + 1);
            if (pSolProc->cbLdt > 0)
                cb += ElfNoteHeaderSize(pSolProc->cbLdt);

            PRTSOLCORETHREADINFO pThreadInfo = pSolProc->pThreadInfoHead;
            while (pThreadInfo)
            {
                cb += ElfNoteHeaderSize(sizeof(lwpsinfo_t));
                if (pThreadInfo->pStatus)
                    cb += ElfNoteHeaderSize(sizeof(lwpstatus_t));

                pThreadInfo = pThreadInfo->pNext;
            }

            break;
        }

        default:
        {
            CORELOGRELSYS((CORELOG_NAME "ElfNoteSectionSize: Unknown segment era %d\n", enmType));
            break;
        }
    }

    return cb;
}


/**
 * Write the note section for the given era into the core file.
 * Solaris has two types of program  header information (new and old).
 *
 * @param pSolCore          Pointer to the core object.
 * @param enmType           Type of core file information required.
 *
 * @return IPRT status code.
 */
static int ElfWriteNoteSection(PRTSOLCORE pSolCore, RTSOLCORETYPE enmType)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    int rc = VERR_GENERAL_FAILURE;

#ifdef RT_OS_SOLARIS
    typedef int (*PFNELFWRITENOTEHDR)(PRTSOLCORE pSolCore, uint_t, const void *pcv, size_t cb);
    typedef struct
    {
        const char        *pszType;
        uint_t             Type;
        const void        *pcv;
        size_t             cb;
    } ELFWRITENOTE;

    switch (enmType)
    {
        case enmOldEra:
        {
            ELFWRITENOTE aElfNotes[] =
            {
                { "NT_PRPSINFO", NT_PRPSINFO, &pSolProc->ProcInfoOld,   sizeof(prpsinfo_t) },
                { "NT_AUXV",     NT_AUXV,      pSolProc->pAuxVecs,      pSolProc->cAuxVecs * sizeof(auxv_t) },
                { "NT_PLATFORM", NT_PLATFORM,  pSolProc->szPlatform,    strlen(pSolProc->szPlatform) + 1 }
            };

            for (unsigned i = 0; i < RT_ELEMENTS(aElfNotes); i++)
            {
                rc = ElfWriteNoteHeader(pSolCore, aElfNotes[i].Type, aElfNotes[i].pcv, aElfNotes[i].cb);
                if (RT_FAILURE(rc))
                {
                    CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader failed for %s. rc=%Rrc\n",
                                   aElfNotes[i].pszType, rc));
                    break;
                }
            }

            /*
             * Write old-style thread info., they contain nothing about zombies,
             * so we just skip if there is no status information for them.
             */
            PRTSOLCORETHREADINFO pThreadInfo = pSolProc->pThreadInfoHead;
            for (; pThreadInfo; pThreadInfo = pThreadInfo->pNext)
            {
                if (!pThreadInfo->pStatus)
                    continue;

                prstatus_t OldProcessStatus;
                GetOldProcessStatus(pSolCore, &pThreadInfo->Info, pThreadInfo->pStatus, &OldProcessStatus);
                rc = ElfWriteNoteHeader(pSolCore, NT_PRSTATUS, &OldProcessStatus, sizeof(prstatus_t));
                if (RT_SUCCESS(rc))
                {
                    rc = ElfWriteNoteHeader(pSolCore, NT_PRFPREG, &pThreadInfo->pStatus->pr_fpreg, sizeof(prfpregset_t));
                    if (RT_FAILURE(rc))
                    {
                        CORELOGRELSYS((CORELOG_NAME "ElfWriteSegment: ElfWriteNote failed for NT_PRFPREF. rc=%Rrc\n", rc));
                        break;
                    }
                }
                else
                {
                    CORELOGRELSYS((CORELOG_NAME "ElfWriteSegment: ElfWriteNote failed for NT_PRSTATUS. rc=%Rrc\n", rc));
                    break;
                }
            }
            break;
        }

        case enmNewEra:
        {
            ELFWRITENOTE aElfNotes[] =
            {
                { "NT_PSINFO",     NT_PSINFO,      pSolProc->pvProcInfo,   pSolProc->cbProcInfo },
                { "NT_PSTATUS",    NT_PSTATUS,    &pSolProc->ProcStatus,   sizeof(pstatus_t) },
                { "NT_AUXV",       NT_AUXV,        pSolProc->pAuxVecs,     pSolProc->cAuxVecs * sizeof(auxv_t) },
                { "NT_PLATFORM",   NT_PLATFORM,    pSolProc->szPlatform,   strlen(pSolProc->szPlatform) + 1 },
                { "NT_UTSNAME",    NT_UTSNAME,    &pSolProc->UtsName,      sizeof(struct utsname) },
                { "NT_CONTENT",    NT_CONTENT,    &pSolProc->CoreContent,  sizeof(core_content_t) },
                { "NT_PRCRED",     NT_PRCRED,      pSolProc->pvCred,       pSolProc->cbCred },
                { "NT_PRPRIV",     NT_PRPRIV,      pSolProc->pPriv,        PRIV_PRPRIV_SIZE(pSolProc->pPriv) },
                { "NT_PRPRIVINFO", NT_PRPRIVINFO,  pSolProc->pcPrivImpl,   PRIV_IMPL_INFO_SIZE(pSolProc->pcPrivImpl) },
                { "NT_ZONENAME",   NT_ZONENAME,    pSolProc->szZoneName,   strlen(pSolProc->szZoneName) + 1 }
            };

            for (unsigned i = 0; i < RT_ELEMENTS(aElfNotes); i++)
            {
                rc = ElfWriteNoteHeader(pSolCore, aElfNotes[i].Type, aElfNotes[i].pcv, aElfNotes[i].cb);
                if (RT_FAILURE(rc))
                {
                    CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader failed for %s. rc=%Rrc\n",
                                   aElfNotes[i].pszType, rc));
                    break;
                }
            }

            /*
             * Write new-style thread info., missing lwpstatus_t indicates it's a zombie thread
             * we only dump the lwpsinfo_t in that case.
             */
            PRTSOLCORETHREADINFO pThreadInfo = pSolProc->pThreadInfoHead;
            for (; pThreadInfo; pThreadInfo = pThreadInfo->pNext)
            {
                rc = ElfWriteNoteHeader(pSolCore, NT_LWPSINFO, &pThreadInfo->Info, sizeof(lwpsinfo_t));
                if (RT_FAILURE(rc))
                {
                    CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader for NT_LWPSINFO failed. rc=%Rrc\n", rc));
                    break;
                }

                if (pThreadInfo->pStatus)
                {
                    rc = ElfWriteNoteHeader(pSolCore, NT_LWPSTATUS, pThreadInfo->pStatus, sizeof(lwpstatus_t));
                    if (RT_FAILURE(rc))
                    {
                        CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: ElfWriteNoteHeader for NT_LWPSTATUS failed. rc=%Rrc\n",
                                       rc));
                        break;
                    }
                }
            }
            break;
        }

        default:
        {
            CORELOGRELSYS((CORELOG_NAME "ElfWriteNoteSection: Invalid type %d\n", enmType));
            rc = VERR_GENERAL_FAILURE;
            break;
        }
    }
#else
# error Port Me!
#endif
    return rc;
}


/**
 * Write mappings into the core file.
 *
 * @param pSolCore          Pointer to the core object.
 *
 * @return IPRT status code.
 */
static int ElfWriteMappings(PRTSOLCORE pSolCore)
{
    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;

    int rc = VERR_GENERAL_FAILURE;
    PRTSOLCOREMAPINFO pMapInfo = pSolProc->pMapInfoHead;
    while (pMapInfo)
    {
        if (!pMapInfo->fError)
        {
            uint64_t k = 0;
            char achBuf[PAGE_SIZE];
            while (k < pMapInfo->pMap.pr_size)
            {
                size_t cb = RT_MIN(sizeof(achBuf), pMapInfo->pMap.pr_size - k);
                int rc2 = ProcReadAddrSpace(pSolProc, pMapInfo->pMap.pr_vaddr + k, &achBuf, cb);
                if (RT_FAILURE(rc2))
                {
                    CORELOGRELSYS((CORELOG_NAME "ElfWriteMappings: Failed to read mapping, can't recover. Bye. rc=%Rrc\n", rc));
                    return VERR_INVALID_STATE;
                }

                rc = pSolCore->pfnWriter(pSolCore->fdCoreFile, achBuf, sizeof(achBuf));
                if (RT_FAILURE(rc))
                {
                    CORELOGRELSYS((CORELOG_NAME "ElfWriteMappings: pfnWriter failed. rc=%Rrc\n", rc));
                    return rc;
                }
                k += cb;
            }
        }
        else
        {
            char achBuf[RT_ALIGN_Z(sizeof(int), 8)];
            RT_ZERO(achBuf);
            memcpy(achBuf, &pMapInfo->fError, sizeof(pMapInfo->fError));
            if (sizeof(achBuf) != pMapInfo->pMap.pr_size)
                CORELOGRELSYS((CORELOG_NAME "ElfWriteMappings: Huh!? something is wrong!\n"));
            rc = pSolCore->pfnWriter(pSolCore->fdCoreFile, &achBuf, sizeof(achBuf));
            if (RT_FAILURE(rc))
            {
                CORELOGRELSYS((CORELOG_NAME "ElfWriteMappings: pfnWriter(2) failed. rc=%Rrc\n", rc));
                return rc;
            }
        }

        pMapInfo = pMapInfo->pNext;
    }

    return VINF_SUCCESS;
}


/**
 * Write program headers for all mappings into the core file.
 *
 * @param pSolCore          Pointer to the core object.
 *
 * @return IPRT status code.
 */
static int ElfWriteMappingHeaders(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    Elf_Phdr ProgHdr;
    RT_ZERO(ProgHdr);
    ProgHdr.p_type = PT_LOAD;

    int rc = VERR_GENERAL_FAILURE;
    PRTSOLCOREMAPINFO pMapInfo = pSolProc->pMapInfoHead;
    while (pMapInfo)
    {
        ProgHdr.p_vaddr  = pMapInfo->pMap.pr_vaddr;     /* Virtual address of this mapping in the process address space */
        ProgHdr.p_offset = pSolCore->offWrite;          /* Where this mapping is located in the core file */
        ProgHdr.p_memsz  = pMapInfo->pMap.pr_size;      /* Size of the memory image of the mapping */
        ProgHdr.p_filesz = pMapInfo->pMap.pr_size;      /* Size of the file image of the mapping */

        ProgHdr.p_flags = 0;                            /* Reset fields in a loop when needed! */
        if (pMapInfo->pMap.pr_mflags & MA_READ)
            ProgHdr.p_flags |= PF_R;
        if (pMapInfo->pMap.pr_mflags & MA_WRITE)
            ProgHdr.p_flags |= PF_W;
        if (pMapInfo->pMap.pr_mflags & MA_EXEC)
            ProgHdr.p_flags |= PF_X;

        if (pMapInfo->fError)
            ProgHdr.p_flags |= PF_SUNW_FAILURE;

        rc = pSolCore->pfnWriter(pSolCore->fdCoreFile, &ProgHdr, sizeof(ProgHdr));
        if (RT_FAILURE(rc))
        {
            CORELOGRELSYS((CORELOG_NAME "ElfWriteMappingHeaders: pfnWriter failed. rc=%Rrc\n", rc));
            return rc;
        }

        pSolCore->offWrite += ProgHdr.p_filesz;
        pMapInfo = pMapInfo->pNext;
    }
    return rc;
}

/**
 * Inner worker for rtCoreDumperWriteCore, which purpose is to
 * squash cleanup gotos.
 */
static int rtCoreDumperWriteCoreDoIt(PRTSOLCORE pSolCore, PFNRTCOREWRITER pfnWriter,
                                     PRTSOLCOREPROCESS pSolProc)
{
    pSolCore->offWrite = 0;
    uint32_t cProgHdrs = pSolProc->cMappings + 2; /* two PT_NOTE program headers (old, new style) */

    /*
     * Write the ELF header.
     */
    Elf_Ehdr ElfHdr;
    RT_ZERO(ElfHdr);
    ElfHdr.e_ident[EI_MAG0]  = ELFMAG0;
    ElfHdr.e_ident[EI_MAG1]  = ELFMAG1;
    ElfHdr.e_ident[EI_MAG2]  = ELFMAG2;
    ElfHdr.e_ident[EI_MAG3]  = ELFMAG3;
    ElfHdr.e_ident[EI_DATA]  = IsBigEndian() ? ELFDATA2MSB : ELFDATA2LSB;
    ElfHdr.e_type            = ET_CORE;
    ElfHdr.e_version         = EV_CURRENT;
#ifdef RT_ARCH_AMD64
    ElfHdr.e_machine         = EM_AMD64;
    ElfHdr.e_ident[EI_CLASS] = ELFCLASS64;
#else
    ElfHdr.e_machine         = EM_386;
    ElfHdr.e_ident[EI_CLASS] = ELFCLASS32;
#endif
    if (cProgHdrs >= PN_XNUM)
        ElfHdr.e_phnum       = PN_XNUM;
    else
        ElfHdr.e_phnum       = cProgHdrs;
    ElfHdr.e_ehsize          = sizeof(ElfHdr);
    ElfHdr.e_phoff           = sizeof(ElfHdr);
    ElfHdr.e_phentsize       = sizeof(Elf_Phdr);
    ElfHdr.e_shentsize       = sizeof(Elf_Shdr);
    int rc = pSolCore->pfnWriter(pSolCore->fdCoreFile, &ElfHdr, sizeof(ElfHdr));
    if (RT_FAILURE(rc))
    {
        CORELOGRELSYS((CORELOG_NAME "WriteCore: pfnWriter failed writing ELF header. rc=%Rrc\n", rc));
        return rc;
    }

    /*
     * Setup program header.
     */
    Elf_Phdr ProgHdr;
    RT_ZERO(ProgHdr);
    ProgHdr.p_type = PT_NOTE;
    ProgHdr.p_flags = PF_R;

    /*
     * Write old-style NOTE program header.
     */
    pSolCore->offWrite += sizeof(ElfHdr) + cProgHdrs * sizeof(ProgHdr);
    ProgHdr.p_offset = pSolCore->offWrite;
    ProgHdr.p_filesz = ElfNoteSectionSize(pSolCore, enmOldEra);
    rc = pSolCore->pfnWriter(pSolCore->fdCoreFile, &ProgHdr, sizeof(ProgHdr));
    if (RT_FAILURE(rc))
    {
        CORELOGRELSYS((CORELOG_NAME "WriteCore: pfnWriter failed writing old-style ELF program Header. rc=%Rrc\n", rc));
        return rc;
    }

    /*
     * Write new-style NOTE program header.
     */
    pSolCore->offWrite += ProgHdr.p_filesz;
    ProgHdr.p_offset = pSolCore->offWrite;
    ProgHdr.p_filesz = ElfNoteSectionSize(pSolCore, enmNewEra);
    rc = pSolCore->pfnWriter(pSolCore->fdCoreFile, &ProgHdr, sizeof(ProgHdr));
    if (RT_FAILURE(rc))
    {
        CORELOGRELSYS((CORELOG_NAME "WriteCore: pfnWriter failed writing new-style ELF program header. rc=%Rrc\n", rc));
        return rc;
    }

    /*
     * Write program headers per mapping.
     */
    pSolCore->offWrite += ProgHdr.p_filesz;
    rc = ElfWriteMappingHeaders(pSolCore);
    if (RT_FAILURE(rc))
    {
        CORELOGRELSYS((CORELOG_NAME "Write: ElfWriteMappings failed. rc=%Rrc\n", rc));
        return rc;
    }

    /*
     * Write old-style note section.
     */
    rc = ElfWriteNoteSection(pSolCore, enmOldEra);
    if (RT_FAILURE(rc))
    {
        CORELOGRELSYS((CORELOG_NAME "WriteCore: ElfWriteNoteSection old-style failed. rc=%Rrc\n", rc));
        return rc;
    }

    /*
     * Write new-style section.
     */
    rc = ElfWriteNoteSection(pSolCore, enmNewEra);
    if (RT_FAILURE(rc))
    {
        CORELOGRELSYS((CORELOG_NAME "WriteCore: ElfWriteNoteSection new-style failed. rc=%Rrc\n", rc));
        return rc;
    }

    /*
     * Write all mappings.
     */
    rc = ElfWriteMappings(pSolCore);
    if (RT_FAILURE(rc))
    {
        CORELOGRELSYS((CORELOG_NAME "WriteCore: ElfWriteMappings failed. rc=%Rrc\n", rc));
        return rc;
    }

    return rc;
}


/**
 * Write a prepared core file using a user-passed in writer function, requires all threads
 * to be in suspended state (i.e. called after CreateCore).
 *
 * @return IPRT status code.
 * @param pSolCore          Pointer to the core object.
 * @param pfnWriter         Pointer to the writer function to override default writer (NULL uses default).
 *
 * @remarks Resumes all suspended threads, unless it's an invalid core. This
 *          function must be called only -after- rtCoreDumperCreateCore().
 */
static int rtCoreDumperWriteCore(PRTSOLCORE pSolCore, PFNRTCOREWRITER pfnWriter)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);

    if (!pSolCore->fIsValid)
        return VERR_INVALID_STATE;

    if (pfnWriter)
        pSolCore->pfnWriter = pfnWriter;

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    char szPath[PATH_MAX];
    int  rc;

    /*
     * Open the process address space file.
     */
    RTStrPrintf(szPath, sizeof(szPath), "/proc/%d/as", (int)pSolProc->Process);
    int fd = open(szPath, O_RDONLY);
    if (fd >= 0)
    {
        pSolProc->fdAs = fd;

        /*
         * Create the core file.
         */
        fd = open(pSolCore->szCorePath, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR);
        if (fd >= 0)
        {
            pSolCore->fdCoreFile = fd;

            /*
             * Do the actual writing.
             */
            rc = rtCoreDumperWriteCoreDoIt(pSolCore, pfnWriter, pSolProc);

            close(pSolCore->fdCoreFile);
            pSolCore->fdCoreFile = -1;
        }
        else
        {
            rc = RTErrConvertFromErrno(fd);
            CORELOGRELSYS((CORELOG_NAME "WriteCore: failed to open %s. rc=%Rrc\n", pSolCore->szCorePath, rc));
        }
        close(pSolProc->fdAs);
        pSolProc->fdAs = -1;
    }
    else
    {
        rc = RTErrConvertFromErrno(fd);
        CORELOGRELSYS((CORELOG_NAME "WriteCore: Failed to open address space, %s. rc=%Rrc\n", szPath, rc));
    }

    rtCoreDumperResumeThreads(pSolCore);
    return rc;
}


/**
 * Takes a process snapshot into a passed-in core object. It has the side-effect of halting
 * all threads which can lead to things like spurious wakeups of threads (if and when threads
 * are ultimately resumed en-masse) already suspended while calling this function.
 *
 * @return IPRT status code.
 * @param pSolCore          Pointer to a core object.
 * @param pContext          Pointer to the caller context thread.
 * @param pszCoreFilePath   Path to the core file. If NULL is passed, the global
 *                          path specified in RTCoreDumperSetup() would be used.
 *
 * @remarks Halts all threads.
 */
static int rtCoreDumperCreateCore(PRTSOLCORE pSolCore, ucontext_t *pContext, const char *pszCoreFilePath)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);
    AssertReturn(pContext, VERR_INVALID_POINTER);

    /*
     * Initialize core structures.
     */
    memset(pSolCore, 0, sizeof(RTSOLCORE));
    pSolCore->pfnReader = &ReadFileNoIntr;
    pSolCore->pfnWriter = &WriteFileNoIntr;
    pSolCore->fIsValid  = false;
    pSolCore->fdCoreFile = -1;

    PRTSOLCOREPROCESS pSolProc = &pSolCore->SolProc;
    pSolProc->Process        = RTProcSelf();
    pSolProc->hCurThread     = _lwp_self(); /* thr_self() */
    pSolProc->fdAs           = -1;
    pSolProc->pCurThreadCtx  = pContext;
    pSolProc->CoreContent    = CC_CONTENT_DEFAULT;

    RTProcGetExecutablePath(pSolProc->szExecPath, sizeof(pSolProc->szExecPath));  /* this gets full path not just name */
    pSolProc->pszExecName = RTPathFilename(pSolProc->szExecPath);

    /*
     * If a path has been specified, use it. Otherwise use the global path.
     */
    if (!pszCoreFilePath)
    {
        /*
         * If no output directory is specified, use current directory.
         */
        if (g_szCoreDumpDir[0] == '\0')
            g_szCoreDumpDir[0] = '.';

        if (g_szCoreDumpFile[0] == '\0')
        {
            /* We cannot call RTPathAbs*() as they call getcwd() which calls malloc. */
            RTStrPrintf(pSolCore->szCorePath, sizeof(pSolCore->szCorePath), "%s/core.vb.%s.%d",
                        g_szCoreDumpDir, pSolProc->pszExecName, (int)pSolProc->Process);
        }
        else
            RTStrPrintf(pSolCore->szCorePath, sizeof(pSolCore->szCorePath), "%s/core.vb.%s", g_szCoreDumpDir, g_szCoreDumpFile);
    }
    else
        RTStrCopy(pSolCore->szCorePath, sizeof(pSolCore->szCorePath), pszCoreFilePath);

    CORELOG((CORELOG_NAME  "CreateCore: Taking Core %s from Thread %d\n", pSolCore->szCorePath, (int)pSolProc->hCurThread));

    /*
     * Quiesce the process.
     */
    int rc = rtCoreDumperSuspendThreads(pSolCore);
    if (RT_SUCCESS(rc))
    {
        rc = AllocMemoryArea(pSolCore);
        if (RT_SUCCESS(rc))
        {
            rc = ProcReadInfo(pSolCore);
            if (RT_SUCCESS(rc))
            {
                rc = GetOldProcessInfo(pSolCore, &pSolProc->ProcInfoOld);
                if (RT_SUCCESS(rc))
                {
                    if (IsProcessArchNative(pSolProc))
                    {
                        /*
                         * Read process status, information such as number of active LWPs will be
                         * invalid since we just quiesced the process.
                         */
                        rc = ProcReadStatus(pSolCore);
                        if (RT_SUCCESS(rc))
                        {
                            struct COREACCUMULATOR
                            {
                                const char        *pszName;
                                PFNRTSOLCOREACCUMULATOR pfnAcc;
                                bool               fOptional;
                            } aAccumulators[] =
                            {
                                { "ProcReadLdt",      &ProcReadLdt,      false },
                                { "ProcReadCred",     &ProcReadCred,     false },
                                { "ProcReadPriv",     &ProcReadPriv,     false },
                                { "ProcReadAuxVecs",  &ProcReadAuxVecs,  false },
                                { "ProcReadMappings", &ProcReadMappings, false },
                                { "ProcReadThreads",  &ProcReadThreads,  false },
                                { "ProcReadMiscInfo", &ProcReadMiscInfo, false }
                            };

                            for (unsigned i = 0; i < RT_ELEMENTS(aAccumulators); i++)
                            {
                                rc = aAccumulators[i].pfnAcc(pSolCore);
                                if (RT_FAILURE(rc))
                                {
                                    CORELOGRELSYS((CORELOG_NAME "CreateCore: %s failed. rc=%Rrc\n", aAccumulators[i].pszName, rc));
                                    if (!aAccumulators[i].fOptional)
                                        break;
                                }
                            }

                            if (RT_SUCCESS(rc))
                            {
                                pSolCore->fIsValid = true;
                                return VINF_SUCCESS;
                            }

                            FreeMemoryArea(pSolCore);
                        }
                        else
                            CORELOGRELSYS((CORELOG_NAME "CreateCore: ProcReadStatus failed. rc=%Rrc\n", rc));
                    }
                    else
                    {
                        CORELOGRELSYS((CORELOG_NAME "CreateCore: IsProcessArchNative failed.\n"));
                        rc = VERR_BAD_EXE_FORMAT;
                    }
                }
                else
                    CORELOGRELSYS((CORELOG_NAME "CreateCore: GetOldProcessInfo failed. rc=%Rrc\n", rc));
            }
            else
                CORELOGRELSYS((CORELOG_NAME "CreateCore: ProcReadInfo failed. rc=%Rrc\n", rc));
        }
        else
            CORELOGRELSYS((CORELOG_NAME "CreateCore: AllocMemoryArea failed. rc=%Rrc\n", rc));

        /*
         * Resume threads on failure.
         */
        rtCoreDumperResumeThreads(pSolCore);
    }
    else
        CORELOG((CORELOG_NAME "CreateCore: SuspendAllThreads failed. Thread bomb!?! rc=%Rrc\n", rc));

    return rc;
}


/**
 * Destroy an existing core object.
 *
 * @param pSolCore          Pointer to the core object.
 *
 * @return IPRT status code.
 */
static int rtCoreDumperDestroyCore(PRTSOLCORE pSolCore)
{
    AssertReturn(pSolCore, VERR_INVALID_POINTER);
    if (!pSolCore->fIsValid)
        return VERR_INVALID_STATE;

    FreeMemoryArea(pSolCore);
    pSolCore->fIsValid = false;
    return VINF_SUCCESS;
}


/**
 * Takes a core dump.
 *
 * @param   pContext            The context of the caller.
 * @param   pszOutputFile       Path of the core file. If NULL is passed, the
 *                              global path passed in RTCoreDumperSetup will
 *                              be used.
 * @returns IPRT status code.
 */
static int rtCoreDumperTakeDump(ucontext_t *pContext, const char *pszOutputFile)
{
    if (!pContext)
    {
        CORELOGRELSYS((CORELOG_NAME "TakeDump: Missing context.\n"));
        return VERR_INVALID_POINTER;
    }

    /*
     * Take a snapshot, then dump core to disk, all threads except this one are halted
     * from before taking the snapshot until writing the core is completely finished.
     * Any errors would resume all threads if they were halted.
     */
    RTSOLCORE SolCore;
    RT_ZERO(SolCore);
    int rc = rtCoreDumperCreateCore(&SolCore, pContext, pszOutputFile);
    if (RT_SUCCESS(rc))
    {
        rc = rtCoreDumperWriteCore(&SolCore, &WriteFileNoIntr);
        if (RT_SUCCESS(rc))
            CORELOGRELSYS((CORELOG_NAME "Core dumped in %s\n", SolCore.szCorePath));
        else
            CORELOGRELSYS((CORELOG_NAME "TakeDump: WriteCore failed. szCorePath=%s rc=%Rrc\n", SolCore.szCorePath, rc));

        rtCoreDumperDestroyCore(&SolCore);
    }
    else
        CORELOGRELSYS((CORELOG_NAME "TakeDump: CreateCore failed. rc=%Rrc\n", rc));

    return rc;
}


/**
 * The signal handler that will be invoked to take core dumps.
 *
 * @param Sig                   The signal that invoked us.
 * @param pSigInfo              The signal information.
 * @param pvArg                 Opaque pointer to the caller context structure,
 *                              this cannot be NULL.
 */
static void rtCoreDumperSignalHandler(int Sig, siginfo_t *pSigInfo, void *pvArg)
{
    CORELOG((CORELOG_NAME "SignalHandler Sig=%d pvArg=%p\n", Sig, pvArg));

    RTNATIVETHREAD  hCurNativeThread = RTThreadNativeSelf();
    int             rc               = VERR_GENERAL_FAILURE;
    bool            fCallSystemDump  = false;
    bool            fRc;
    ASMAtomicCmpXchgHandle(&g_CoreDumpThread, hCurNativeThread, NIL_RTNATIVETHREAD, fRc);
    if (fRc)
    {
        rc = rtCoreDumperTakeDump((ucontext_t *)pvArg, NULL /* Use Global Core filepath */);
        ASMAtomicWriteHandle(&g_CoreDumpThread, NIL_RTNATIVETHREAD);

        if (RT_FAILURE(rc))
            CORELOGRELSYS((CORELOG_NAME "TakeDump failed! rc=%Rrc\n", rc));
    }
    else if (Sig == SIGSEGV || Sig == SIGBUS || Sig == SIGTRAP)
    {
        /*
         * Core dumping is already in progress and we've somehow ended up being
         * signalled again.
         */
        rc = VERR_INTERNAL_ERROR;

        /*
         * If our dumper has crashed. No point in waiting, trigger the system one.
         * Wait only when the dumping thread is not the one generating this signal.
         */
        RTNATIVETHREAD hNativeDumperThread;
        ASMAtomicReadHandle(&g_CoreDumpThread, &hNativeDumperThread);
        if (hNativeDumperThread == RTThreadNativeSelf())
        {
            CORELOGRELSYS((CORELOG_NAME "SignalHandler: Core dumper (thread %u) crashed Sig=%d. Triggering system dump\n",
                           RTThreadSelf(), Sig));
            fCallSystemDump = true;
        }
        else
        {
            /*
             * Some other thread in the process is triggering a crash, wait a while
             * to let our core dumper finish, on timeout trigger system dump.
             */
            CORELOGRELSYS((CORELOG_NAME "SignalHandler: Core dump already in progress! Waiting a while for completion Sig=%d.\n",
                           Sig));
            int64_t iTimeout = 16000;  /* timeout (ms) */
            for (;;)
            {
                ASMAtomicReadHandle(&g_CoreDumpThread, &hNativeDumperThread);
                if (hNativeDumperThread == NIL_RTNATIVETHREAD)
                    break;
                RTThreadSleep(200);
                iTimeout -= 200;
                if (iTimeout <= 0)
                    break;
            }
            if (iTimeout <= 0)
            {
                fCallSystemDump = true;
                CORELOGRELSYS((CORELOG_NAME "SignalHandler: Core dumper seems to be stuck. Signalling new signal %d\n", Sig));
            }
        }
    }

    if (Sig == SIGSEGV || Sig == SIGBUS || Sig == SIGTRAP)
    {
        /*
         * Reset signal handlers, we're not a live core we will be blown away
         * one way or another.
         */
        signal(SIGSEGV, SIG_DFL);
        signal(SIGBUS, SIG_DFL);
        signal(SIGTRAP, SIG_DFL);

        /*
         * Hard terminate the process if this is not a live dump without invoking
         * the system core dumping behaviour.
         */
        if (RT_SUCCESS(rc))
            raise(SIGKILL);

        /*
         * Something went wrong, fall back to the system core dumper.
         */
        if (fCallSystemDump)
            abort();
    }
}


RTDECL(int) RTCoreDumperTakeDump(const char *pszOutputFile, bool fLiveCore)
{
    ucontext_t Context;
    int rc = getcontext(&Context);
    if (!rc)
    {
        /*
         * Block SIGSEGV and co. while we write the core.
         */
        sigset_t SigSet, OldSigSet;
        sigemptyset(&SigSet);
        sigaddset(&SigSet, SIGSEGV);
        sigaddset(&SigSet, SIGBUS);
        sigaddset(&SigSet, SIGTRAP);
        sigaddset(&SigSet, SIGUSR2);
        pthread_sigmask(SIG_BLOCK, &SigSet, &OldSigSet);
        rc = rtCoreDumperTakeDump(&Context, pszOutputFile);
        if (RT_FAILURE(rc))
            CORELOGRELSYS(("RTCoreDumperTakeDump: rtCoreDumperTakeDump failed rc=%Rrc\n", rc));

        if (!fLiveCore)
        {
            signal(SIGSEGV, SIG_DFL);
            signal(SIGBUS, SIG_DFL);
            signal(SIGTRAP, SIG_DFL);
            if (RT_SUCCESS(rc))
                raise(SIGKILL);
            else
                abort();
        }
        pthread_sigmask(SIG_SETMASK, &OldSigSet, NULL);
    }
    else
    {
        CORELOGRELSYS(("RTCoreDumperTakeDump: getcontext failed rc=%d.\n", rc));
        rc = VERR_INVALID_CONTEXT;
    }

    return rc;
}


RTDECL(int) RTCoreDumperSetup(const char *pszOutputDir, uint32_t fFlags)
{
    /*
     * Validate flags.
     */
    AssertReturn(fFlags, VERR_INVALID_PARAMETER);
    AssertReturn(!(fFlags & ~(  RTCOREDUMPER_FLAGS_REPLACE_SYSTEM_DUMP
                              | RTCOREDUMPER_FLAGS_LIVE_CORE)),
                 VERR_INVALID_PARAMETER);


    /*
     * Setup/change the core dump directory if specified.
     */
    RT_ZERO(g_szCoreDumpDir);
    if (pszOutputDir)
    {
        if (!RTDirExists(pszOutputDir))
            return VERR_NOT_A_DIRECTORY;
        RTStrCopy(g_szCoreDumpDir, sizeof(g_szCoreDumpDir), pszOutputDir);
    }

    /*
     * Install core dump signal handler only if the flags changed or if it's the first time.
     */
    if (   ASMAtomicReadBool(&g_fCoreDumpSignalSetup) == false
        || ASMAtomicReadU32(&g_fCoreDumpFlags) != fFlags)
    {
        struct sigaction sigAct;
        RT_ZERO(sigAct);
        sigAct.sa_sigaction = &rtCoreDumperSignalHandler;

        if (   (fFlags & RTCOREDUMPER_FLAGS_REPLACE_SYSTEM_DUMP)
            && !(g_fCoreDumpFlags & RTCOREDUMPER_FLAGS_REPLACE_SYSTEM_DUMP))
        {
            sigemptyset(&sigAct.sa_mask);
            sigAct.sa_flags = SA_RESTART | SA_SIGINFO | SA_NODEFER;
            sigaction(SIGSEGV, &sigAct, NULL);
            sigaction(SIGBUS, &sigAct, NULL);
            sigaction(SIGTRAP, &sigAct, NULL);
        }

        if (   fFlags & RTCOREDUMPER_FLAGS_LIVE_CORE
            && !(g_fCoreDumpFlags & RTCOREDUMPER_FLAGS_LIVE_CORE))
        {
            sigfillset(&sigAct.sa_mask);                        /* Block all signals while in it's signal handler */
            sigAct.sa_flags = SA_RESTART | SA_SIGINFO;
            sigaction(SIGUSR2, &sigAct, NULL);
        }

        ASMAtomicWriteU32(&g_fCoreDumpFlags, fFlags);
        ASMAtomicWriteBool(&g_fCoreDumpSignalSetup, true);
    }

    return VINF_SUCCESS;
}


RTDECL(int) RTCoreDumperDisable(void)
{
    /*
     * Remove core dump signal handler & reset variables.
     */
    if (ASMAtomicReadBool(&g_fCoreDumpSignalSetup) == true)
    {
        signal(SIGSEGV, SIG_DFL);
        signal(SIGBUS, SIG_DFL);
        signal(SIGTRAP, SIG_DFL);
        signal(SIGUSR2, SIG_DFL);
        ASMAtomicWriteBool(&g_fCoreDumpSignalSetup, false);
    }

    RT_ZERO(g_szCoreDumpDir);
    RT_ZERO(g_szCoreDumpFile);
    ASMAtomicWriteU32(&g_fCoreDumpFlags, 0);
    return VINF_SUCCESS;
}