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
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
|
// Copyright (c) 2006, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// minidump.cc: A minidump reader.
//
// See minidump.h for documentation.
//
// Author: Mark Mentovai
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#ifdef _WIN32
#include <io.h>
typedef SSIZE_T ssize_t;
#define open _open
#define read _read
#define lseek _lseek
#else // _WIN32
#define O_BINARY 0
#endif // _WIN32
#include <cassert>
#include <map>
#include <vector>
#include "processor/range_map-inl.h"
#include "google_breakpad/processor/minidump.h"
#include "processor/basic_code_module.h"
#include "processor/basic_code_modules.h"
#include "processor/scoped_ptr.h"
namespace google_breakpad {
using std::vector;
//
// Swapping routines
//
// Inlining these doesn't increase code size significantly, and it saves
// a whole lot of unnecessary jumping back and forth.
//
// Swapping an 8-bit quantity is a no-op. This function is only provided
// to account for certain templatized operations that require swapping for
// wider types but handle u_int8_t too
// (MinidumpMemoryRegion::GetMemoryAtAddressInternal).
static inline void Swap(u_int8_t* value) {
}
// Optimization: don't need to AND the furthest right shift, because we're
// shifting an unsigned quantity. The standard requires zero-filling in this
// case. If the quantities were signed, a bitmask whould be needed for this
// right shift to avoid an arithmetic shift (which retains the sign bit).
// The furthest left shift never needs to be ANDed bitmask.
static inline void Swap(u_int16_t* value) {
*value = (*value >> 8) |
(*value << 8);
}
static inline void Swap(u_int32_t* value) {
*value = (*value >> 24) |
((*value >> 8) & 0x0000ff00) |
((*value << 8) & 0x00ff0000) |
(*value << 24);
}
static inline void Swap(u_int64_t* value) {
*value = (*value >> 56) |
((*value >> 40) & 0x000000000000ff00LL) |
((*value >> 24) & 0x0000000000ff0000LL) |
((*value >> 8) & 0x00000000ff000000LL) |
((*value << 8) & 0x000000ff00000000LL) |
((*value << 24) & 0x0000ff0000000000LL) |
((*value << 40) & 0x00ff000000000000LL) |
(*value << 56);
}
// Given a pointer to a 128-bit int in the minidump data, set the "low"
// and "high" fields appropriately.
static void Normalize128(u_int128_t* value, bool is_big_endian) {
// The struct format is [high, low], so if the format is big-endian,
// the most significant bytes will already be in the high field.
if (!is_big_endian) {
u_int64_t temp = value->low;
value->low = value->high;
value->high = temp;
}
}
// This just swaps each int64 half of the 128-bit value.
// The value should also be normalized by calling Normalize128().
static void Swap(u_int128_t* value) {
Swap(&value->low);
Swap(&value->high);
}
static inline void Swap(MDLocationDescriptor* location_descriptor) {
Swap(&location_descriptor->data_size);
Swap(&location_descriptor->rva);
}
static inline void Swap(MDMemoryDescriptor* memory_descriptor) {
Swap(&memory_descriptor->start_of_memory_range);
Swap(&memory_descriptor->memory);
}
static inline void Swap(MDGUID* guid) {
Swap(&guid->data1);
Swap(&guid->data2);
Swap(&guid->data3);
// Don't swap guid->data4[] because it contains 8-bit quantities.
}
//
// Character conversion routines
//
// Standard wide-character conversion routines depend on the system's own
// idea of what width a wide character should be: some use 16 bits, and
// some use 32 bits. For the purposes of a minidump, wide strings are
// always represented with 16-bit UTF-16 chracters. iconv isn't available
// everywhere, and its interface varies where it is available. iconv also
// deals purely with char* pointers, so in addition to considering the swap
// parameter, a converter that uses iconv would also need to take the host
// CPU's endianness into consideration. It doesn't seems worth the trouble
// of making it a dependency when we don't care about anything but UTF-16.
static string* UTF16ToUTF8(const vector<u_int16_t>& in,
bool swap) {
scoped_ptr<string> out(new string());
// Set the string's initial capacity to the number of UTF-16 characters,
// because the UTF-8 representation will always be at least this long.
// If the UTF-8 representation is longer, the string will grow dynamically.
out->reserve(in.size());
for (vector<u_int16_t>::const_iterator iterator = in.begin();
iterator != in.end();
++iterator) {
// Get a 16-bit value from the input
u_int16_t in_word = *iterator;
if (swap)
Swap(&in_word);
// Convert the input value (in_word) into a Unicode code point (unichar).
u_int32_t unichar;
if (in_word >= 0xdc00 && in_word <= 0xdcff) {
// Low surrogate not following high surrogate, fail.
return NULL;
} else if (in_word >= 0xd800 && in_word <= 0xdbff) {
// High surrogate.
unichar = (in_word - 0xd7c0) << 10;
if (++iterator == in.end()) {
// End of input
return NULL;
}
in_word = *iterator;
if (in_word < 0xdc00 || in_word > 0xdcff) {
// Expected low surrogate, found something else
return NULL;
}
unichar |= in_word & 0x03ff;
} else {
// The ordinary case, a single non-surrogate Unicode character encoded
// as a single 16-bit value.
unichar = in_word;
}
// Convert the Unicode code point (unichar) into its UTF-8 representation,
// appending it to the out string.
if (unichar < 0x80) {
(*out) += unichar;
} else if (unichar < 0x800) {
(*out) += 0xc0 | (unichar >> 6);
(*out) += 0x80 | (unichar & 0x3f);
} else if (unichar < 0x10000) {
(*out) += 0xe0 | (unichar >> 12);
(*out) += 0x80 | ((unichar >> 6) & 0x3f);
(*out) += 0x80 | (unichar & 0x3f);
} else if (unichar < 0x200000) {
(*out) += 0xf0 | (unichar >> 18);
(*out) += 0x80 | ((unichar >> 12) & 0x3f);
(*out) += 0x80 | ((unichar >> 6) & 0x3f);
(*out) += 0x80 | (unichar & 0x3f);
} else {
// Some (high) value that's not (presently) defined in UTF-8
return NULL;
}
}
return out.release();
}
//
// MinidumpObject
//
MinidumpObject::MinidumpObject(Minidump* minidump)
: minidump_(minidump),
valid_(false) {
}
//
// MinidumpStream
//
MinidumpStream::MinidumpStream(Minidump* minidump)
: MinidumpObject(minidump) {
}
//
// MinidumpContext
//
MinidumpContext::MinidumpContext(Minidump* minidump)
: MinidumpStream(minidump),
context_() {
}
MinidumpContext::~MinidumpContext() {
FreeContext();
}
bool MinidumpContext::Read(u_int32_t expected_size) {
valid_ = false;
FreeContext();
// First, figure out what type of CPU this context structure is for.
u_int32_t context_flags;
if (!minidump_->ReadBytes(&context_flags, sizeof(context_flags)))
return false;
if (minidump_->swap())
Swap(&context_flags);
u_int32_t cpu_type = context_flags & MD_CONTEXT_CPU_MASK;
// Allocate the context structure for the correct CPU and fill it. The
// casts are slightly unorthodox, but it seems better to do that than to
// maintain a separate pointer for each type of CPU context structure
// when only one of them will be used.
switch (cpu_type) {
case MD_CONTEXT_X86: {
if (expected_size != sizeof(MDRawContextX86))
return false;
scoped_ptr<MDRawContextX86> context_x86(new MDRawContextX86());
// Set the context_flags member, which has already been read, and
// read the rest of the structure beginning with the first member
// after context_flags.
context_x86->context_flags = context_flags;
size_t flags_size = sizeof(context_x86->context_flags);
u_int8_t* context_after_flags =
reinterpret_cast<u_int8_t*>(context_x86.get()) + flags_size;
if (!minidump_->ReadBytes(context_after_flags,
sizeof(MDRawContextX86) - flags_size)) {
return false;
}
// Do this after reading the entire MDRawContext structure because
// GetSystemInfo may seek minidump to a new position.
if (!CheckAgainstSystemInfo(cpu_type))
return false;
if (minidump_->swap()) {
// context_x86->context_flags was already swapped.
Swap(&context_x86->dr0);
Swap(&context_x86->dr1);
Swap(&context_x86->dr2);
Swap(&context_x86->dr3);
Swap(&context_x86->dr6);
Swap(&context_x86->dr7);
Swap(&context_x86->float_save.control_word);
Swap(&context_x86->float_save.status_word);
Swap(&context_x86->float_save.tag_word);
Swap(&context_x86->float_save.error_offset);
Swap(&context_x86->float_save.error_selector);
Swap(&context_x86->float_save.data_offset);
Swap(&context_x86->float_save.data_selector);
// context_x86->float_save.register_area[] contains 8-bit quantities
// and does not need to be swapped.
Swap(&context_x86->float_save.cr0_npx_state);
Swap(&context_x86->gs);
Swap(&context_x86->fs);
Swap(&context_x86->es);
Swap(&context_x86->ds);
Swap(&context_x86->edi);
Swap(&context_x86->esi);
Swap(&context_x86->ebx);
Swap(&context_x86->edx);
Swap(&context_x86->ecx);
Swap(&context_x86->eax);
Swap(&context_x86->ebp);
Swap(&context_x86->eip);
Swap(&context_x86->cs);
Swap(&context_x86->eflags);
Swap(&context_x86->esp);
Swap(&context_x86->ss);
// context_x86->extended_registers[] contains 8-bit quantities and
// does not need to be swapped.
}
context_.x86 = context_x86.release();
break;
}
case MD_CONTEXT_PPC: {
if (expected_size != sizeof(MDRawContextPPC))
return false;
scoped_ptr<MDRawContextPPC> context_ppc(new MDRawContextPPC());
// Set the context_flags member, which has already been read, and
// read the rest of the structure beginning with the first member
// after context_flags.
context_ppc->context_flags = context_flags;
size_t flags_size = sizeof(context_ppc->context_flags);
u_int8_t* context_after_flags =
reinterpret_cast<u_int8_t*>(context_ppc.get()) + flags_size;
if (!minidump_->ReadBytes(context_after_flags,
sizeof(MDRawContextPPC) - flags_size)) {
return false;
}
// Do this after reading the entire MDRawContext structure because
// GetSystemInfo may seek minidump to a new position.
if (!CheckAgainstSystemInfo(cpu_type))
return false;
// Normalize the 128-bit types in the dump.
// Since this is PowerPC, by definition, the values are big-endian.
for (unsigned int vr_index = 0;
vr_index < MD_VECTORSAVEAREA_PPC_VR_COUNT;
++vr_index) {
Normalize128(&context_ppc->vector_save.save_vr[vr_index], true);
}
if (minidump_->swap()) {
// context_ppc->context_flags was already swapped.
Swap(&context_ppc->srr0);
Swap(&context_ppc->srr1);
for (unsigned int gpr_index = 0;
gpr_index < MD_CONTEXT_PPC_GPR_COUNT;
++gpr_index) {
Swap(&context_ppc->gpr[gpr_index]);
}
Swap(&context_ppc->cr);
Swap(&context_ppc->xer);
Swap(&context_ppc->lr);
Swap(&context_ppc->ctr);
Swap(&context_ppc->mq);
Swap(&context_ppc->vrsave);
for (unsigned int fpr_index = 0;
fpr_index < MD_FLOATINGSAVEAREA_PPC_FPR_COUNT;
++fpr_index) {
Swap(&context_ppc->float_save.fpregs[fpr_index]);
}
// Don't swap context_ppc->float_save.fpscr_pad because it is only
// used for padding.
Swap(&context_ppc->float_save.fpscr);
for (unsigned int vr_index = 0;
vr_index < MD_VECTORSAVEAREA_PPC_VR_COUNT;
++vr_index) {
Swap(&context_ppc->vector_save.save_vr[vr_index]);
}
Swap(&context_ppc->vector_save.save_vscr);
// Don't swap the padding fields in vector_save.
Swap(&context_ppc->vector_save.save_vrvalid);
}
context_.ppc = context_ppc.release();
break;
}
default: {
// Unknown context type
return false;
break;
}
}
valid_ = true;
return true;
}
u_int32_t MinidumpContext::GetContextCPU() const {
return valid_ ? context_.base->context_flags & MD_CONTEXT_CPU_MASK : 0;
}
const MDRawContextX86* MinidumpContext::GetContextX86() const {
return GetContextCPU() == MD_CONTEXT_X86 ? context_.x86 : NULL;
}
const MDRawContextPPC* MinidumpContext::GetContextPPC() const {
return GetContextCPU() == MD_CONTEXT_PPC ? context_.ppc : NULL;
}
void MinidumpContext::FreeContext() {
switch (GetContextCPU()) {
case MD_CONTEXT_X86:
delete context_.x86;
break;
case MD_CONTEXT_PPC:
delete context_.ppc;
break;
default:
// There is no context record (valid_ is false) or there's a
// context record for an unknown CPU (shouldn't happen, only known
// records are stored by Read).
break;
}
context_.base = NULL;
}
bool MinidumpContext::CheckAgainstSystemInfo(u_int32_t context_cpu_type) {
// It's OK if the minidump doesn't contain an MD_SYSTEM_INFO_STREAM,
// as this function just implements a sanity check.
MinidumpSystemInfo* system_info = minidump_->GetSystemInfo();
if (!system_info)
return true;
// If there is an MD_SYSTEM_INFO_STREAM, it should contain valid system info.
const MDRawSystemInfo* raw_system_info = system_info->system_info();
if (!raw_system_info)
return false;
MDCPUArchitecture system_info_cpu_type = static_cast<MDCPUArchitecture>(
raw_system_info->processor_architecture);
// Compare the CPU type of the context record to the CPU type in the
// minidump's system info stream.
switch (context_cpu_type) {
case MD_CONTEXT_X86:
if (system_info_cpu_type != MD_CPU_ARCHITECTURE_X86 &&
system_info_cpu_type != MD_CPU_ARCHITECTURE_X86_WIN64) {
return false;
}
break;
case MD_CONTEXT_PPC:
if (system_info_cpu_type != MD_CPU_ARCHITECTURE_PPC)
return false;
break;
default:
// Unknown context_cpu_type, this should not happen.
return false;
break;
}
return true;
}
void MinidumpContext::Print() {
switch (GetContextCPU()) {
case MD_CONTEXT_X86: {
const MDRawContextX86* context_x86 = GetContextX86();
printf("MDRawContextX86\n");
printf(" context_flags = 0x%x\n",
context_x86->context_flags);
printf(" dr0 = 0x%x\n", context_x86->dr0);
printf(" dr1 = 0x%x\n", context_x86->dr1);
printf(" dr2 = 0x%x\n", context_x86->dr2);
printf(" dr3 = 0x%x\n", context_x86->dr3);
printf(" dr6 = 0x%x\n", context_x86->dr6);
printf(" dr7 = 0x%x\n", context_x86->dr7);
printf(" float_save.control_word = 0x%x\n",
context_x86->float_save.control_word);
printf(" float_save.status_word = 0x%x\n",
context_x86->float_save.status_word);
printf(" float_save.tag_word = 0x%x\n",
context_x86->float_save.tag_word);
printf(" float_save.error_offset = 0x%x\n",
context_x86->float_save.error_offset);
printf(" float_save.error_selector = 0x%x\n",
context_x86->float_save.error_selector);
printf(" float_save.data_offset = 0x%x\n",
context_x86->float_save.data_offset);
printf(" float_save.data_selector = 0x%x\n",
context_x86->float_save.data_selector);
printf(" float_save.register_area[%2d] = 0x",
MD_FLOATINGSAVEAREA_X86_REGISTERAREA_SIZE);
for (unsigned int register_index = 0;
register_index < MD_FLOATINGSAVEAREA_X86_REGISTERAREA_SIZE;
++register_index) {
printf("%02x", context_x86->float_save.register_area[register_index]);
}
printf("\n");
printf(" float_save.cr0_npx_state = 0x%x\n",
context_x86->float_save.cr0_npx_state);
printf(" gs = 0x%x\n", context_x86->gs);
printf(" fs = 0x%x\n", context_x86->fs);
printf(" es = 0x%x\n", context_x86->es);
printf(" ds = 0x%x\n", context_x86->ds);
printf(" edi = 0x%x\n", context_x86->edi);
printf(" esi = 0x%x\n", context_x86->esi);
printf(" ebx = 0x%x\n", context_x86->ebx);
printf(" edx = 0x%x\n", context_x86->edx);
printf(" ecx = 0x%x\n", context_x86->ecx);
printf(" eax = 0x%x\n", context_x86->eax);
printf(" ebp = 0x%x\n", context_x86->ebp);
printf(" eip = 0x%x\n", context_x86->eip);
printf(" cs = 0x%x\n", context_x86->cs);
printf(" eflags = 0x%x\n", context_x86->eflags);
printf(" esp = 0x%x\n", context_x86->esp);
printf(" ss = 0x%x\n", context_x86->ss);
printf(" extended_registers[%3d] = 0x",
MD_CONTEXT_X86_EXTENDED_REGISTERS_SIZE);
for (unsigned int register_index = 0;
register_index < MD_CONTEXT_X86_EXTENDED_REGISTERS_SIZE;
++register_index) {
printf("%02x", context_x86->extended_registers[register_index]);
}
printf("\n\n");
break;
}
case MD_CONTEXT_PPC: {
const MDRawContextPPC* context_ppc = GetContextPPC();
printf("MDRawContextPPC\n");
printf(" context_flags = 0x%x\n",
context_ppc->context_flags);
printf(" srr0 = 0x%x\n", context_ppc->srr0);
printf(" srr1 = 0x%x\n", context_ppc->srr1);
for (unsigned int gpr_index = 0;
gpr_index < MD_CONTEXT_PPC_GPR_COUNT;
++gpr_index) {
printf(" gpr[%2d] = 0x%x\n",
gpr_index, context_ppc->gpr[gpr_index]);
}
printf(" cr = 0x%x\n", context_ppc->cr);
printf(" xer = 0x%x\n", context_ppc->xer);
printf(" lr = 0x%x\n", context_ppc->lr);
printf(" ctr = 0x%x\n", context_ppc->ctr);
printf(" mq = 0x%x\n", context_ppc->mq);
printf(" vrsave = 0x%x\n", context_ppc->vrsave);
for (unsigned int fpr_index = 0;
fpr_index < MD_FLOATINGSAVEAREA_PPC_FPR_COUNT;
++fpr_index) {
printf(" float_save.fpregs[%2d] = 0x%llx\n",
fpr_index, context_ppc->float_save.fpregs[fpr_index]);
}
printf(" float_save.fpscr = 0x%x\n",
context_ppc->float_save.fpscr);
// TODO(mmentovai): print the 128-bit quantities in
// context_ppc->vector_save. This isn't done yet because printf
// doesn't support 128-bit quantities, and printing them using
// %llx as two 64-bit quantities requires knowledge of the CPU's
// byte ordering.
printf(" vector_save.save_vrvalid = 0x%x\n",
context_ppc->vector_save.save_vrvalid);
printf("\n");
break;
}
default: {
break;
}
}
}
//
// MinidumpMemoryRegion
//
MinidumpMemoryRegion::MinidumpMemoryRegion(Minidump* minidump)
: MinidumpObject(minidump),
descriptor_(NULL),
memory_(NULL) {
}
MinidumpMemoryRegion::~MinidumpMemoryRegion() {
delete memory_;
}
void MinidumpMemoryRegion::SetDescriptor(MDMemoryDescriptor* descriptor) {
descriptor_ = descriptor;
valid_ = descriptor &&
(descriptor_->start_of_memory_range +
descriptor_->memory.data_size) >
descriptor_->start_of_memory_range;
}
const u_int8_t* MinidumpMemoryRegion::GetMemory() {
if (!valid_)
return NULL;
if (!memory_) {
if (descriptor_->memory.data_size == 0)
return NULL;
if (!minidump_->SeekSet(descriptor_->memory.rva))
return NULL;
// TODO(mmentovai): verify rational size!
scoped_ptr< vector<u_int8_t> > memory(
new vector<u_int8_t>(descriptor_->memory.data_size));
if (!minidump_->ReadBytes(&(*memory)[0], descriptor_->memory.data_size))
return NULL;
memory_ = memory.release();
}
return &(*memory_)[0];
}
u_int64_t MinidumpMemoryRegion::GetBase() {
return valid_ ?
descriptor_->start_of_memory_range : static_cast<u_int64_t>(-1);
}
u_int32_t MinidumpMemoryRegion::GetSize() {
return valid_ ? descriptor_->memory.data_size : 0;
}
void MinidumpMemoryRegion::FreeMemory() {
delete memory_;
memory_ = NULL;
}
template<typename T>
bool MinidumpMemoryRegion::GetMemoryAtAddressInternal(u_int64_t address,
T* value) {
if (!valid_ || !value)
return false;
if (address < descriptor_->start_of_memory_range ||
address + sizeof(T) > descriptor_->start_of_memory_range +
descriptor_->memory.data_size) {
return false;
}
const u_int8_t* memory = GetMemory();
if (!memory)
return false;
// If the CPU requires memory accesses to be aligned, this can crash.
// x86 and ppc are able to cope, though.
*value = *reinterpret_cast<const T*>(
&memory[address - descriptor_->start_of_memory_range]);
if (minidump_->swap())
Swap(value);
return true;
}
bool MinidumpMemoryRegion::GetMemoryAtAddress(u_int64_t address,
u_int8_t* value) {
return GetMemoryAtAddressInternal(address, value);
}
bool MinidumpMemoryRegion::GetMemoryAtAddress(u_int64_t address,
u_int16_t* value) {
return GetMemoryAtAddressInternal(address, value);
}
bool MinidumpMemoryRegion::GetMemoryAtAddress(u_int64_t address,
u_int32_t* value) {
return GetMemoryAtAddressInternal(address, value);
}
bool MinidumpMemoryRegion::GetMemoryAtAddress(u_int64_t address,
u_int64_t* value) {
return GetMemoryAtAddressInternal(address, value);
}
void MinidumpMemoryRegion::Print() {
if (!valid_)
return;
const u_int8_t* memory = GetMemory();
if (memory) {
printf("0x");
for (unsigned int byte_index = 0;
byte_index < descriptor_->memory.data_size;
byte_index++) {
printf("%02x", memory[byte_index]);
}
printf("\n");
} else {
printf("No memory\n");
}
}
//
// MinidumpThread
//
MinidumpThread::MinidumpThread(Minidump* minidump)
: MinidumpObject(minidump),
thread_(),
memory_(NULL),
context_(NULL) {
}
MinidumpThread::~MinidumpThread() {
delete memory_;
delete context_;
}
bool MinidumpThread::Read() {
// Invalidate cached data.
delete memory_;
memory_ = NULL;
delete context_;
context_ = NULL;
valid_ = false;
if (!minidump_->ReadBytes(&thread_, sizeof(thread_)))
return false;
if (minidump_->swap()) {
Swap(&thread_.thread_id);
Swap(&thread_.suspend_count);
Swap(&thread_.priority_class);
Swap(&thread_.priority);
Swap(&thread_.teb);
Swap(&thread_.stack);
Swap(&thread_.thread_context);
}
// Check for base + size overflow or undersize. A separate size==0
// check is needed in case base == 0.
u_int64_t high_address = thread_.stack.start_of_memory_range +
thread_.stack.memory.data_size - 1;
if (thread_.stack.memory.data_size == 0 ||
high_address < thread_.stack.start_of_memory_range)
return false;
memory_ = new MinidumpMemoryRegion(minidump_);
memory_->SetDescriptor(&thread_.stack);
valid_ = true;
return true;
}
MinidumpMemoryRegion* MinidumpThread::GetMemory() {
return !valid_ ? NULL : memory_;
}
MinidumpContext* MinidumpThread::GetContext() {
if (!valid_)
return NULL;
if (!context_) {
if (!minidump_->SeekSet(thread_.thread_context.rva))
return NULL;
scoped_ptr<MinidumpContext> context(new MinidumpContext(minidump_));
if (!context->Read(thread_.thread_context.data_size))
return NULL;
context_ = context.release();
}
return context_;
}
bool MinidumpThread::GetThreadID(u_int32_t *thread_id) const {
if (!thread_id || !valid_)
return false;
*thread_id = thread_.thread_id;
return true;
}
void MinidumpThread::Print() {
if (!valid_)
return;
printf("MDRawThread\n");
printf(" thread_id = 0x%x\n", thread_.thread_id);
printf(" suspend_count = %d\n", thread_.suspend_count);
printf(" priority_class = 0x%x\n", thread_.priority_class);
printf(" priority = 0x%x\n", thread_.priority);
printf(" teb = 0x%llx\n", thread_.teb);
printf(" stack.start_of_memory_range = 0x%llx\n",
thread_.stack.start_of_memory_range);
printf(" stack.memory.data_size = 0x%x\n",
thread_.stack.memory.data_size);
printf(" stack.memory.rva = 0x%x\n", thread_.stack.memory.rva);
printf(" thread_context.data_size = 0x%x\n",
thread_.thread_context.data_size);
printf(" thread_context.rva = 0x%x\n",
thread_.thread_context.rva);
MinidumpContext* context = GetContext();
if (context) {
printf("\n");
context->Print();
} else {
printf(" (no context)\n");
printf("\n");
}
MinidumpMemoryRegion* memory = GetMemory();
if (memory) {
printf("Stack\n");
memory->Print();
} else {
printf("No stack\n");
}
printf("\n");
}
//
// MinidumpThreadList
//
MinidumpThreadList::MinidumpThreadList(Minidump* minidump)
: MinidumpStream(minidump),
id_to_thread_map_(),
threads_(NULL),
thread_count_(0) {
}
MinidumpThreadList::~MinidumpThreadList() {
delete threads_;
}
bool MinidumpThreadList::Read(u_int32_t expected_size) {
// Invalidate cached data.
id_to_thread_map_.clear();
delete threads_;
threads_ = NULL;
thread_count_ = 0;
valid_ = false;
u_int32_t thread_count;
if (expected_size < sizeof(thread_count))
return false;
if (!minidump_->ReadBytes(&thread_count, sizeof(thread_count)))
return false;
if (minidump_->swap())
Swap(&thread_count);
if (expected_size != sizeof(thread_count) +
thread_count * sizeof(MDRawThread)) {
return false;
}
if (thread_count) {
// TODO(mmentovai): verify rational size!
scoped_ptr<MinidumpThreads> threads(
new MinidumpThreads(thread_count, MinidumpThread(minidump_)));
for (unsigned int thread_index = 0;
thread_index < thread_count;
++thread_index) {
MinidumpThread* thread = &(*threads)[thread_index];
// Assume that the file offset is correct after the last read.
if (!thread->Read())
return false;
u_int32_t thread_id;
if (!thread->GetThreadID(&thread_id))
return false;
if (GetThreadByID(thread_id)) {
// Another thread with this ID is already in the list. Data error.
return false;
}
id_to_thread_map_[thread_id] = thread;
}
threads_ = threads.release();
}
thread_count_ = thread_count;
valid_ = true;
return true;
}
MinidumpThread* MinidumpThreadList::GetThreadAtIndex(unsigned int index)
const {
if (!valid_ || index >= thread_count_)
return NULL;
return &(*threads_)[index];
}
MinidumpThread* MinidumpThreadList::GetThreadByID(u_int32_t thread_id) {
// Don't check valid_. Read calls this method before everything is
// validated. It is safe to not check valid_ here.
return id_to_thread_map_[thread_id];
}
void MinidumpThreadList::Print() {
if (!valid_)
return;
printf("MinidumpThreadList\n");
printf(" thread_count = %d\n", thread_count_);
printf("\n");
for (unsigned int thread_index = 0;
thread_index < thread_count_;
++thread_index) {
printf("thread[%d]\n", thread_index);
(*threads_)[thread_index].Print();
}
}
//
// MinidumpModule
//
MinidumpModule::MinidumpModule(Minidump* minidump)
: MinidumpObject(minidump),
module_valid_(false),
module_(),
name_(NULL),
cv_record_(NULL),
cv_record_signature_(MD_CVINFOUNKNOWN_SIGNATURE),
misc_record_(NULL) {
}
MinidumpModule::~MinidumpModule() {
delete name_;
delete cv_record_;
delete misc_record_;
}
bool MinidumpModule::Read() {
// Invalidate cached data.
delete name_;
name_ = NULL;
delete cv_record_;
cv_record_ = NULL;
cv_record_signature_ = MD_CVINFOUNKNOWN_SIGNATURE;
delete misc_record_;
misc_record_ = NULL;
module_valid_ = false;
valid_ = false;
if (!minidump_->ReadBytes(&module_, MD_MODULE_SIZE))
return false;
if (minidump_->swap()) {
Swap(&module_.base_of_image);
Swap(&module_.size_of_image);
Swap(&module_.checksum);
Swap(&module_.time_date_stamp);
Swap(&module_.module_name_rva);
Swap(&module_.version_info.signature);
Swap(&module_.version_info.struct_version);
Swap(&module_.version_info.file_version_hi);
Swap(&module_.version_info.file_version_lo);
Swap(&module_.version_info.product_version_hi);
Swap(&module_.version_info.product_version_lo);
Swap(&module_.version_info.file_flags_mask);
Swap(&module_.version_info.file_flags);
Swap(&module_.version_info.file_os);
Swap(&module_.version_info.file_type);
Swap(&module_.version_info.file_subtype);
Swap(&module_.version_info.file_date_hi);
Swap(&module_.version_info.file_date_lo);
Swap(&module_.cv_record);
Swap(&module_.misc_record);
// Don't swap reserved fields because their contents are unknown (as
// are their proper widths).
}
// Check for base + size overflow or undersize. A separate size==0
// check is needed in case base == 0.
u_int64_t high_address = module_.base_of_image + module_.size_of_image - 1;
if (module_.size_of_image == 0 || high_address < module_.base_of_image)
return false;
module_valid_ = true;
return true;
}
bool MinidumpModule::ReadAuxiliaryData() {
if (!module_valid_)
return false;
// Each module must have a name.
name_ = minidump_->ReadString(module_.module_name_rva);
if (!name_)
return false;
// CodeView and miscellaneous debug records are only required if the
// module indicates that they exist.
if (module_.cv_record.data_size && !GetCVRecord(NULL))
return false;
if (module_.misc_record.data_size && !GetMiscRecord(NULL))
return false;
valid_ = true;
return true;
}
string MinidumpModule::code_file() const {
if (!valid_)
return "";
return *name_;
}
string MinidumpModule::code_identifier() const {
if (!valid_)
return "";
MinidumpSystemInfo *minidump_system_info = minidump_->GetSystemInfo();
if (!minidump_system_info)
return "";
const MDRawSystemInfo *raw_system_info = minidump_system_info->system_info();
if (!raw_system_info)
return "";
string identifier;
switch (raw_system_info->platform_id) {
case MD_OS_WIN32_NT:
case MD_OS_WIN32_WINDOWS: {
// Use the same format that the MS symbol server uses in filesystem
// hierarchies.
char identifier_string[17];
snprintf(identifier_string, sizeof(identifier_string), "%08X%x",
module_.time_date_stamp, module_.size_of_image);
identifier = identifier_string;
break;
}
case MD_OS_MAC_OS_X: {
// TODO(mmentovai): support uuid extension if present, otherwise fall
// back to version (from LC_ID_DYLIB?), otherwise fall back to something
// else.
identifier = "id";
break;
}
default: {
// Without knowing what OS generated the dump, we can't generate a good
// identifier. Return an empty string, signalling failure.
break;
}
}
return identifier;
}
string MinidumpModule::debug_file() const {
if (!valid_)
return "";
string file;
// Prefer the CodeView record if present.
if (cv_record_) {
if (cv_record_signature_ == MD_CVINFOPDB70_SIGNATURE) {
// It's actually an MDCVInfoPDB70 structure.
const MDCVInfoPDB70* cv_record_70 =
reinterpret_cast<const MDCVInfoPDB70*>(&(*cv_record_)[0]);
assert(cv_record_70->cv_signature == MD_CVINFOPDB70_SIGNATURE);
// GetCVRecord guarantees pdb_file_name is null-terminated.
file = reinterpret_cast<const char*>(cv_record_70->pdb_file_name);
} else if (cv_record_signature_ == MD_CVINFOPDB20_SIGNATURE) {
// It's actually an MDCVInfoPDB20 structure.
const MDCVInfoPDB20* cv_record_20 =
reinterpret_cast<const MDCVInfoPDB20*>(&(*cv_record_)[0]);
assert(cv_record_20->cv_header.signature == MD_CVINFOPDB20_SIGNATURE);
// GetCVRecord guarantees pdb_file_name is null-terminated.
file = reinterpret_cast<const char*>(cv_record_20->pdb_file_name);
}
// If there's a CodeView record but it doesn't match a known signature,
// try the miscellaneous record.
}
if (file.empty()) {
// No usable CodeView record. Try the miscellaneous debug record.
if (misc_record_) {
const MDImageDebugMisc* misc_record =
reinterpret_cast<const MDImageDebugMisc *>(&(*misc_record_)[0]);
if (!misc_record->unicode) {
// If it's not Unicode, just stuff it into the string. It's unclear
// if misc_record->data is 0-terminated, so use an explicit size.
file = string(
reinterpret_cast<const char*>(misc_record->data),
module_.misc_record.data_size - sizeof(MDImageDebugMisc));
} else {
// There's a misc_record but it encodes the debug filename in UTF-16.
// (Actually, because miscellaneous records are so old, it's probably
// UCS-2.) Convert it to UTF-8 for congruity with the other strings
// that this method (and all other methods in the Minidump family)
// return.
unsigned int bytes =
module_.misc_record.data_size - sizeof(MDImageDebugMisc);
if (bytes % 2 == 0) {
unsigned int utf16_words = bytes / 2;
// UTF16ToUTF8 expects a vector<u_int16_t>, so create a temporary one
// and copy the UTF-16 data into it.
vector<u_int16_t> string_utf16(utf16_words);
if (utf16_words)
memcpy(&string_utf16[0], &misc_record->data, bytes);
// GetMiscRecord already byte-swapped the data[] field if it contains
// UTF-16, so pass false as the swap argument.
scoped_ptr<string> new_file(UTF16ToUTF8(string_utf16, false));
file = *new_file;
}
}
}
}
return file;
}
string MinidumpModule::debug_identifier() const {
if (!valid_)
return "";
string identifier;
// Use the CodeView record if present.
if (cv_record_) {
if (cv_record_signature_ == MD_CVINFOPDB70_SIGNATURE) {
// It's actually an MDCVInfoPDB70 structure.
const MDCVInfoPDB70* cv_record_70 =
reinterpret_cast<const MDCVInfoPDB70*>(&(*cv_record_)[0]);
assert(cv_record_70->cv_signature == MD_CVINFOPDB70_SIGNATURE);
// Use the same format that the MS symbol server uses in filesystem
// hierarchies.
char identifier_string[41];
snprintf(identifier_string, sizeof(identifier_string),
"%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%x",
cv_record_70->signature.data1,
cv_record_70->signature.data2,
cv_record_70->signature.data3,
cv_record_70->signature.data4[0],
cv_record_70->signature.data4[1],
cv_record_70->signature.data4[2],
cv_record_70->signature.data4[3],
cv_record_70->signature.data4[4],
cv_record_70->signature.data4[5],
cv_record_70->signature.data4[6],
cv_record_70->signature.data4[7],
cv_record_70->age);
identifier = identifier_string;
} else if (cv_record_signature_ == MD_CVINFOPDB20_SIGNATURE) {
// It's actually an MDCVInfoPDB20 structure.
const MDCVInfoPDB20* cv_record_20 =
reinterpret_cast<const MDCVInfoPDB20*>(&(*cv_record_)[0]);
assert(cv_record_20->cv_header.signature == MD_CVINFOPDB20_SIGNATURE);
// Use the same format that the MS symbol server uses in filesystem
// hierarchies.
char identifier_string[17];
snprintf(identifier_string, sizeof(identifier_string),
"%08X%x", cv_record_20->signature, cv_record_20->age);
identifier = identifier_string;
}
}
// TODO(mmentovai): if there's no usable CodeView record, there might be a
// miscellaneous debug record. It only carries a filename, though, and no
// identifier. I'm not sure what the right thing to do for the identifier
// is in that case, but I don't expect to find many modules without a
// CodeView record (or some other Breakpad extension structure in place of
// a CodeView record). Treat it as an error (empty identifier) for now.
// TODO(mmentovai): on the Mac, provide fallbacks as in code_identifier().
return identifier;
}
string MinidumpModule::version() const {
if (!valid_)
return "";
string version;
if (module_.version_info.signature == MD_VSFIXEDFILEINFO_SIGNATURE &&
module_.version_info.struct_version & MD_VSFIXEDFILEINFO_VERSION) {
char version_string[24];
snprintf(version_string, sizeof(version_string), "%u.%u.%u.%u",
module_.version_info.file_version_hi >> 16,
module_.version_info.file_version_hi & 0xffff,
module_.version_info.file_version_lo >> 16,
module_.version_info.file_version_lo & 0xffff);
version = version_string;
}
// TODO(mmentovai): possibly support other struct types in place of
// the one used with MD_VSFIXEDFILEINFO_SIGNATURE. We can possibly use
// a different structure that better represents versioning facilities on
// Mac OS X and Linux, instead of forcing them to adhere to the dotted
// quad of 16-bit ints that Windows uses.
return version;
}
const CodeModule* MinidumpModule::Copy() const {
return new BasicCodeModule(this);
}
const u_int8_t* MinidumpModule::GetCVRecord(u_int32_t* size) {
if (!module_valid_)
return NULL;
if (!cv_record_) {
// This just guards against 0-sized CodeView records; more specific checks
// are used when the signature is checked against various structure types.
if (!module_.cv_record.data_size)
return NULL;
if (!minidump_->SeekSet(module_.cv_record.rva))
return NULL;
// TODO(mmentovai): verify rational size!
// Allocating something that will be accessed as MDCVInfoPDB70 or
// MDCVInfoPDB20 but is allocated as u_int8_t[] can cause alignment
// problems. x86 and ppc are able to cope, though. This allocation
// style is needed because the MDCVInfoPDB70 or MDCVInfoPDB20 are
// variable-sized due to their pdb_file_name fields; these structures
// are not sizeof(MDCVInfoPDB70) or sizeof(MDCVInfoPDB20) and treating
// them as such would result in incomplete structures or overruns.
scoped_ptr< vector<u_int8_t> > cv_record(
new vector<u_int8_t>(module_.cv_record.data_size));
if (!minidump_->ReadBytes(&(*cv_record)[0], module_.cv_record.data_size))
return NULL;
u_int32_t signature = MD_CVINFOUNKNOWN_SIGNATURE;
if (module_.cv_record.data_size > sizeof(signature)) {
MDCVInfoPDB70* cv_record_signature =
reinterpret_cast<MDCVInfoPDB70*>(&(*cv_record)[0]);
signature = cv_record_signature->cv_signature;
if (minidump_->swap())
Swap(&signature);
}
if (signature == MD_CVINFOPDB70_SIGNATURE) {
// Now that the structure type is known, recheck the size.
if (sizeof(MDCVInfoPDB70) > module_.cv_record.data_size)
return NULL;
if (minidump_->swap()) {
MDCVInfoPDB70* cv_record_70 =
reinterpret_cast<MDCVInfoPDB70*>(&(*cv_record)[0]);
Swap(&cv_record_70->cv_signature);
Swap(&cv_record_70->signature);
Swap(&cv_record_70->age);
// Don't swap cv_record_70.pdb_file_name because it's an array of 8-bit
// quantities. (It's a path, is it UTF-8?)
}
// The last field of either structure is null-terminated 8-bit character
// data. Ensure that it's null-terminated.
if ((*cv_record)[module_.cv_record.data_size - 1] != '\0')
return NULL;
} else if (signature == MD_CVINFOPDB20_SIGNATURE) {
// Now that the structure type is known, recheck the size.
if (sizeof(MDCVInfoPDB20) > module_.cv_record.data_size)
return NULL;
if (minidump_->swap()) {
MDCVInfoPDB20* cv_record_20 =
reinterpret_cast<MDCVInfoPDB20*>(&(*cv_record)[0]);
Swap(&cv_record_20->cv_header.signature);
Swap(&cv_record_20->cv_header.offset);
Swap(&cv_record_20->signature);
Swap(&cv_record_20->age);
// Don't swap cv_record_20.pdb_file_name because it's an array of 8-bit
// quantities. (It's a path, is it UTF-8?)
}
// The last field of either structure is null-terminated 8-bit character
// data. Ensure that it's null-terminated.
if ((*cv_record)[module_.cv_record.data_size - 1] != '\0')
return NULL;
}
// If the signature doesn't match something above, it's not something
// that Breakpad can presently handle directly. Because some modules in
// the wild contain such CodeView records as MD_CVINFOCV50_SIGNATURE,
// don't bail out here - allow the data to be returned to the user,
// although byte-swapping can't be done.
// Store the vector type because that's how storage was allocated, but
// return it casted to u_int8_t*.
cv_record_ = cv_record.release();
cv_record_signature_ = signature;
}
if (size)
*size = module_.cv_record.data_size;
return &(*cv_record_)[0];
}
const MDImageDebugMisc* MinidumpModule::GetMiscRecord(u_int32_t* size) {
if (!module_valid_)
return NULL;
if (!misc_record_) {
if (sizeof(MDImageDebugMisc) > module_.misc_record.data_size)
return NULL;
if (!minidump_->SeekSet(module_.misc_record.rva))
return NULL;
// TODO(mmentovai): verify rational size!
// Allocating something that will be accessed as MDImageDebugMisc but
// is allocated as u_int8_t[] can cause alignment problems. x86 and
// ppc are able to cope, though. This allocation style is needed
// because the MDImageDebugMisc is variable-sized due to its data field;
// this structure is not sizeof(MDImageDebugMisc) and treating it as such
// would result in an incomplete structure or an overrun.
scoped_ptr< vector<u_int8_t> > misc_record_mem(
new vector<u_int8_t>(module_.misc_record.data_size));
MDImageDebugMisc* misc_record =
reinterpret_cast<MDImageDebugMisc*>(&(*misc_record_mem)[0]);
if (!minidump_->ReadBytes(misc_record, module_.misc_record.data_size))
return NULL;
if (minidump_->swap()) {
Swap(&misc_record->data_type);
Swap(&misc_record->length);
// Don't swap misc_record.unicode because it's an 8-bit quantity.
// Don't swap the reserved fields for the same reason, and because
// they don't contain any valid data.
if (misc_record->unicode) {
// There is a potential alignment problem, but shouldn't be a problem
// in practice due to the layout of MDImageDebugMisc.
u_int16_t* data16 = reinterpret_cast<u_int16_t*>(&(misc_record->data));
unsigned int dataBytes = module_.misc_record.data_size -
sizeof(MDImageDebugMisc);
unsigned int dataLength = dataBytes / 2;
for (unsigned int characterIndex = 0;
characterIndex < dataLength;
++characterIndex) {
Swap(&data16[characterIndex]);
}
}
}
if (module_.misc_record.data_size != misc_record->length)
return NULL;
// Store the vector type because that's how storage was allocated, but
// return it casted to MDImageDebugMisc*.
misc_record_ = misc_record_mem.release();
}
if (size)
*size = module_.misc_record.data_size;
return reinterpret_cast<MDImageDebugMisc*>(&(*misc_record_)[0]);
}
void MinidumpModule::Print() {
if (!valid_)
return;
printf("MDRawModule\n");
printf(" base_of_image = 0x%llx\n",
module_.base_of_image);
printf(" size_of_image = 0x%x\n",
module_.size_of_image);
printf(" checksum = 0x%x\n",
module_.checksum);
printf(" time_date_stamp = 0x%x\n",
module_.time_date_stamp);
printf(" module_name_rva = 0x%x\n",
module_.module_name_rva);
printf(" version_info.signature = 0x%x\n",
module_.version_info.signature);
printf(" version_info.struct_version = 0x%x\n",
module_.version_info.struct_version);
printf(" version_info.file_version = 0x%x:0x%x\n",
module_.version_info.file_version_hi,
module_.version_info.file_version_lo);
printf(" version_info.product_version = 0x%x:0x%x\n",
module_.version_info.product_version_hi,
module_.version_info.product_version_lo);
printf(" version_info.file_flags_mask = 0x%x\n",
module_.version_info.file_flags_mask);
printf(" version_info.file_flags = 0x%x\n",
module_.version_info.file_flags);
printf(" version_info.file_os = 0x%x\n",
module_.version_info.file_os);
printf(" version_info.file_type = 0x%x\n",
module_.version_info.file_type);
printf(" version_info.file_subtype = 0x%x\n",
module_.version_info.file_subtype);
printf(" version_info.file_date = 0x%x:0x%x\n",
module_.version_info.file_date_hi,
module_.version_info.file_date_lo);
printf(" cv_record.data_size = %d\n",
module_.cv_record.data_size);
printf(" cv_record.rva = 0x%x\n",
module_.cv_record.rva);
printf(" misc_record.data_size = %d\n",
module_.misc_record.data_size);
printf(" misc_record.rva = 0x%x\n",
module_.misc_record.rva);
printf(" (code_file) = \"%s\"\n", code_file().c_str());
printf(" (code_identifier) = \"%s\"\n",
code_identifier().c_str());
u_int32_t cv_record_size;
const u_int8_t *cv_record = GetCVRecord(&cv_record_size);
if (cv_record) {
if (cv_record_signature_ == MD_CVINFOPDB70_SIGNATURE) {
const MDCVInfoPDB70* cv_record_70 =
reinterpret_cast<const MDCVInfoPDB70*>(cv_record);
assert(cv_record_70->cv_signature == MD_CVINFOPDB70_SIGNATURE);
printf(" (cv_record).cv_signature = 0x%x\n",
cv_record_70->cv_signature);
printf(" (cv_record).signature = %08x-%04x-%04x-%02x%02x-",
cv_record_70->signature.data1,
cv_record_70->signature.data2,
cv_record_70->signature.data3,
cv_record_70->signature.data4[0],
cv_record_70->signature.data4[1]);
for (unsigned int guidIndex = 2;
guidIndex < 8;
++guidIndex) {
printf("%02x", cv_record_70->signature.data4[guidIndex]);
}
printf("\n");
printf(" (cv_record).age = %d\n",
cv_record_70->age);
printf(" (cv_record).pdb_file_name = \"%s\"\n",
cv_record_70->pdb_file_name);
} else if (cv_record_signature_ == MD_CVINFOPDB20_SIGNATURE) {
const MDCVInfoPDB20* cv_record_20 =
reinterpret_cast<const MDCVInfoPDB20*>(cv_record);
assert(cv_record_20->cv_header.signature == MD_CVINFOPDB20_SIGNATURE);
printf(" (cv_record).cv_header.signature = 0x%x\n",
cv_record_20->cv_header.signature);
printf(" (cv_record).cv_header.offset = 0x%x\n",
cv_record_20->cv_header.offset);
printf(" (cv_record).signature = 0x%x\n",
cv_record_20->signature);
printf(" (cv_record).age = %d\n",
cv_record_20->age);
printf(" (cv_record).pdb_file_name = \"%s\"\n",
cv_record_20->pdb_file_name);
} else {
printf(" (cv_record) = ");
for (unsigned int cv_byte_index = 0;
cv_byte_index < cv_record_size;
++cv_byte_index) {
printf("%02x", cv_record[cv_byte_index]);
}
printf("\n");
}
} else {
printf(" (cv_record) = (null)\n");
}
const MDImageDebugMisc* misc_record = GetMiscRecord(NULL);
if (misc_record) {
printf(" (misc_record).data_type = 0x%x\n",
misc_record->data_type);
printf(" (misc_record).length = 0x%x\n",
misc_record->length);
printf(" (misc_record).unicode = %d\n",
misc_record->unicode);
// Don't bother printing the UTF-16, we don't really even expect to ever
// see this misc_record anyway.
if (misc_record->unicode)
printf(" (misc_record).data = \"%s\"\n",
misc_record->data);
else
printf(" (misc_record).data = (UTF-16)\n");
} else {
printf(" (misc_record) = (null)\n");
}
printf(" (debug_file) = \"%s\"\n", debug_file().c_str());
printf(" (debug_identifier) = \"%s\"\n",
debug_identifier().c_str());
printf(" (version) = \"%s\"\n", version().c_str());
printf("\n");
}
//
// MinidumpModuleList
//
MinidumpModuleList::MinidumpModuleList(Minidump* minidump)
: MinidumpStream(minidump),
range_map_(new RangeMap<u_int64_t, unsigned int>()),
modules_(NULL),
module_count_(0) {
}
MinidumpModuleList::~MinidumpModuleList() {
delete range_map_;
delete modules_;
}
bool MinidumpModuleList::Read(u_int32_t expected_size) {
// Invalidate cached data.
range_map_->Clear();
delete modules_;
modules_ = NULL;
module_count_ = 0;
valid_ = false;
u_int32_t module_count;
if (expected_size < sizeof(module_count))
return false;
if (!minidump_->ReadBytes(&module_count, sizeof(module_count)))
return false;
if (minidump_->swap())
Swap(&module_count);
if (expected_size != sizeof(module_count) +
module_count * MD_MODULE_SIZE) {
return false;
}
if (module_count) {
// TODO(mmentovai): verify rational size!
scoped_ptr<MinidumpModules> modules(
new MinidumpModules(module_count, MinidumpModule(minidump_)));
for (unsigned int module_index = 0;
module_index < module_count;
++module_index) {
MinidumpModule* module = &(*modules)[module_index];
// Assume that the file offset is correct after the last read.
if (!module->Read())
return false;
}
// Loop through the module list once more to read additional data and
// build the range map. This is done in a second pass because
// MinidumpModule::ReadAuxiliaryData seeks around, and if it were
// included in the loop above, additional seeks would be needed where
// none are now to read contiguous data.
for (unsigned int module_index = 0;
module_index < module_count;
++module_index) {
MinidumpModule* module = &(*modules)[module_index];
if (!module->ReadAuxiliaryData())
return false;
u_int64_t base_address = module->base_address();
u_int64_t module_size = module->size();
if (base_address == static_cast<u_int64_t>(-1))
return false;
if (!range_map_->StoreRange(base_address, module_size, module_index))
return false;
}
modules_ = modules.release();
}
module_count_ = module_count;
valid_ = true;
return true;
}
const MinidumpModule* MinidumpModuleList::GetModuleForAddress(
u_int64_t address) const {
if (!valid_)
return NULL;
unsigned int module_index;
if (!range_map_->RetrieveRange(address, &module_index, NULL, NULL))
return NULL;
return GetModuleAtIndex(module_index);
}
const MinidumpModule* MinidumpModuleList::GetMainModule() const {
if (!valid_)
return NULL;
// The main code module is the first one present in a minidump file's
// MDRawModuleList.
return GetModuleAtSequence(0);
}
const MinidumpModule* MinidumpModuleList::GetModuleAtSequence(
unsigned int sequence) const {
if (!valid_ || sequence >= module_count_)
return NULL;
unsigned int module_index;
if (!range_map_->RetrieveRangeAtIndex(sequence, &module_index, NULL, NULL))
return NULL;
return GetModuleAtIndex(module_index);
}
const MinidumpModule* MinidumpModuleList::GetModuleAtIndex(
unsigned int index) const {
if (!valid_ || index >= module_count_)
return NULL;
return &(*modules_)[index];
}
const CodeModules* MinidumpModuleList::Copy() const {
return new BasicCodeModules(this);
}
void MinidumpModuleList::Print() {
if (!valid_)
return;
printf("MinidumpModuleList\n");
printf(" module_count = %d\n", module_count_);
printf("\n");
for (unsigned int module_index = 0;
module_index < module_count_;
++module_index) {
printf("module[%d]\n", module_index);
(*modules_)[module_index].Print();
}
}
//
// MinidumpMemoryList
//
MinidumpMemoryList::MinidumpMemoryList(Minidump* minidump)
: MinidumpStream(minidump),
range_map_(new RangeMap<u_int64_t, unsigned int>()),
descriptors_(NULL),
regions_(NULL),
region_count_(0) {
}
MinidumpMemoryList::~MinidumpMemoryList() {
delete range_map_;
delete descriptors_;
delete regions_;
}
bool MinidumpMemoryList::Read(u_int32_t expected_size) {
// Invalidate cached data.
delete descriptors_;
descriptors_ = NULL;
delete regions_;
regions_ = NULL;
range_map_->Clear();
region_count_ = 0;
valid_ = false;
u_int32_t region_count;
if (expected_size < sizeof(region_count))
return false;
if (!minidump_->ReadBytes(®ion_count, sizeof(region_count)))
return false;
if (minidump_->swap())
Swap(®ion_count);
if (expected_size != sizeof(region_count) +
region_count * sizeof(MDMemoryDescriptor)) {
return false;
}
if (region_count) {
// TODO(mmentovai): verify rational size!
scoped_ptr<MemoryDescriptors> descriptors(
new MemoryDescriptors(region_count));
// Read the entire array in one fell swoop, instead of reading one entry
// at a time in the loop.
if (!minidump_->ReadBytes(&(*descriptors)[0],
sizeof(MDMemoryDescriptor) * region_count)) {
return false;
}
scoped_ptr<MemoryRegions> regions(
new MemoryRegions(region_count, MinidumpMemoryRegion(minidump_)));
for (unsigned int region_index = 0;
region_index < region_count;
++region_index) {
MDMemoryDescriptor* descriptor = &(*descriptors)[region_index];
if (minidump_->swap())
Swap(descriptor);
u_int64_t base_address = descriptor->start_of_memory_range;
u_int32_t region_size = descriptor->memory.data_size;
// Check for base + size overflow or undersize. A separate size==0
// check is needed in case base == 0.
u_int64_t high_address = base_address + region_size - 1;
if (region_size == 0 || high_address < base_address)
return false;
if (!range_map_->StoreRange(base_address, region_size, region_index))
return false;
(*regions)[region_index].SetDescriptor(descriptor);
}
descriptors_ = descriptors.release();
regions_ = regions.release();
}
region_count_ = region_count;
valid_ = true;
return true;
}
MinidumpMemoryRegion* MinidumpMemoryList::GetMemoryRegionAtIndex(
unsigned int index) {
if (!valid_ || index >= region_count_)
return NULL;
return &(*regions_)[index];
}
MinidumpMemoryRegion* MinidumpMemoryList::GetMemoryRegionForAddress(
u_int64_t address) {
if (!valid_)
return NULL;
unsigned int region_index;
if (!range_map_->RetrieveRange(address, ®ion_index, NULL, NULL))
return NULL;
return GetMemoryRegionAtIndex(region_index);
}
void MinidumpMemoryList::Print() {
if (!valid_)
return;
printf("MinidumpMemoryList\n");
printf(" region_count = %d\n", region_count_);
printf("\n");
for (unsigned int region_index = 0;
region_index < region_count_;
++region_index) {
MDMemoryDescriptor* descriptor = &(*descriptors_)[region_index];
printf("region[%d]\n", region_index);
printf("MDMemoryDescriptor\n");
printf(" start_of_memory_range = 0x%llx\n",
descriptor->start_of_memory_range);
printf(" memory.data_size = 0x%x\n", descriptor->memory.data_size);
printf(" memory.rva = 0x%x\n", descriptor->memory.rva);
MinidumpMemoryRegion* region = GetMemoryRegionAtIndex(region_index);
if (region) {
printf("Memory\n");
region->Print();
} else {
printf("No memory\n");
}
printf("\n");
}
}
//
// MinidumpException
//
MinidumpException::MinidumpException(Minidump* minidump)
: MinidumpStream(minidump),
exception_(),
context_(NULL) {
}
MinidumpException::~MinidumpException() {
delete context_;
}
bool MinidumpException::Read(u_int32_t expected_size) {
// Invalidate cached data.
delete context_;
context_ = NULL;
valid_ = false;
if (expected_size != sizeof(exception_))
return false;
if (!minidump_->ReadBytes(&exception_, sizeof(exception_)))
return false;
if (minidump_->swap()) {
Swap(&exception_.thread_id);
// exception_.__align is for alignment only and does not need to be
// swapped.
Swap(&exception_.exception_record.exception_code);
Swap(&exception_.exception_record.exception_flags);
Swap(&exception_.exception_record.exception_record);
Swap(&exception_.exception_record.exception_address);
Swap(&exception_.exception_record.number_parameters);
// exception_.exception_record.__align is for alignment only and does not
// need to be swapped.
for (unsigned int parameter_index = 0;
parameter_index < MD_EXCEPTION_MAXIMUM_PARAMETERS;
++parameter_index) {
Swap(&exception_.exception_record.exception_information[parameter_index]);
}
Swap(&exception_.thread_context);
}
valid_ = true;
return true;
}
bool MinidumpException::GetThreadID(u_int32_t *thread_id) const {
if (!thread_id || !valid_)
return false;
*thread_id = exception_.thread_id;
return true;
}
MinidumpContext* MinidumpException::GetContext() {
if (!valid_)
return NULL;
if (!context_) {
if (!minidump_->SeekSet(exception_.thread_context.rva))
return NULL;
scoped_ptr<MinidumpContext> context(new MinidumpContext(minidump_));
if (!context->Read(exception_.thread_context.data_size))
return NULL;
context_ = context.release();
}
return context_;
}
void MinidumpException::Print() {
if (!valid_)
return;
printf("MDException\n");
printf(" thread_id = 0x%x\n",
exception_.thread_id);
printf(" exception_record.exception_code = 0x%x\n",
exception_.exception_record.exception_code);
printf(" exception_record.exception_flags = 0x%x\n",
exception_.exception_record.exception_flags);
printf(" exception_record.exception_record = 0x%llx\n",
exception_.exception_record.exception_record);
printf(" exception_record.exception_address = 0x%llx\n",
exception_.exception_record.exception_address);
printf(" exception_record.number_parameters = %d\n",
exception_.exception_record.number_parameters);
for (unsigned int parameterIndex = 0;
parameterIndex < exception_.exception_record.number_parameters;
++parameterIndex) {
printf(" exception_record.exception_information[%2d] = 0x%llx\n",
parameterIndex,
exception_.exception_record.exception_information[parameterIndex]);
}
printf(" thread_context.data_size = %d\n",
exception_.thread_context.data_size);
printf(" thread_context.rva = 0x%x\n",
exception_.thread_context.rva);
MinidumpContext* context = GetContext();
if (context) {
printf("\n");
context->Print();
} else {
printf(" (no context)\n");
printf("\n");
}
}
//
// MinidumpSystemInfo
//
MinidumpSystemInfo::MinidumpSystemInfo(Minidump* minidump)
: MinidumpStream(minidump),
system_info_(),
csd_version_(NULL),
cpu_vendor_(NULL) {
}
MinidumpSystemInfo::~MinidumpSystemInfo() {
delete csd_version_;
delete cpu_vendor_;
}
bool MinidumpSystemInfo::Read(u_int32_t expected_size) {
// Invalidate cached data.
delete csd_version_;
csd_version_ = NULL;
delete cpu_vendor_;
cpu_vendor_ = NULL;
valid_ = false;
if (expected_size != sizeof(system_info_))
return false;
if (!minidump_->ReadBytes(&system_info_, sizeof(system_info_)))
return false;
if (minidump_->swap()) {
Swap(&system_info_.processor_architecture);
Swap(&system_info_.processor_level);
Swap(&system_info_.processor_revision);
// number_of_processors and product_type are 8-bit quantities and need no
// swapping.
Swap(&system_info_.major_version);
Swap(&system_info_.minor_version);
Swap(&system_info_.build_number);
Swap(&system_info_.platform_id);
Swap(&system_info_.csd_version_rva);
Swap(&system_info_.suite_mask);
// Don't swap the reserved2 field because its contents are unknown.
if (system_info_.processor_architecture == MD_CPU_ARCHITECTURE_X86 ||
system_info_.processor_architecture == MD_CPU_ARCHITECTURE_X86_WIN64) {
for (unsigned int i = 0; i < 3; ++i)
Swap(&system_info_.cpu.x86_cpu_info.vendor_id[i]);
Swap(&system_info_.cpu.x86_cpu_info.version_information);
Swap(&system_info_.cpu.x86_cpu_info.feature_information);
Swap(&system_info_.cpu.x86_cpu_info.amd_extended_cpu_features);
} else {
for (unsigned int i = 0; i < 2; ++i)
Swap(&system_info_.cpu.other_cpu_info.processor_features[i]);
}
}
valid_ = true;
return true;
}
string MinidumpSystemInfo::GetOS() {
if (!valid_)
return NULL;
string os;
switch (system_info_.platform_id) {
case MD_OS_WIN32_NT:
case MD_OS_WIN32_WINDOWS:
os = "windows";
break;
case MD_OS_MAC_OS_X:
os = "mac";
break;
case MD_OS_LINUX:
os = "linux";
break;
}
return os;
}
string MinidumpSystemInfo::GetCPU() {
if (!valid_)
return "";
string cpu;
switch (system_info_.processor_architecture) {
case MD_CPU_ARCHITECTURE_X86:
case MD_CPU_ARCHITECTURE_X86_WIN64:
cpu = "x86";
break;
case MD_CPU_ARCHITECTURE_PPC:
cpu = "ppc";
break;
}
return cpu;
}
const string* MinidumpSystemInfo::GetCSDVersion() {
if (!valid_)
return NULL;
if (!csd_version_)
csd_version_ = minidump_->ReadString(system_info_.csd_version_rva);
return csd_version_;
}
const string* MinidumpSystemInfo::GetCPUVendor() {
if (!valid_)
return NULL;
// CPU vendor information can only be determined from x86 minidumps.
if (!cpu_vendor_ &&
(system_info_.processor_architecture == MD_CPU_ARCHITECTURE_X86 ||
system_info_.processor_architecture == MD_CPU_ARCHITECTURE_X86_WIN64)) {
char cpu_vendor_string[13];
snprintf(cpu_vendor_string, sizeof(cpu_vendor_string),
"%c%c%c%c%c%c%c%c%c%c%c%c",
system_info_.cpu.x86_cpu_info.vendor_id[0] & 0xff,
(system_info_.cpu.x86_cpu_info.vendor_id[0] >> 8) & 0xff,
(system_info_.cpu.x86_cpu_info.vendor_id[0] >> 16) & 0xff,
(system_info_.cpu.x86_cpu_info.vendor_id[0] >> 24) & 0xff,
system_info_.cpu.x86_cpu_info.vendor_id[1] & 0xff,
(system_info_.cpu.x86_cpu_info.vendor_id[1] >> 8) & 0xff,
(system_info_.cpu.x86_cpu_info.vendor_id[1] >> 16) & 0xff,
(system_info_.cpu.x86_cpu_info.vendor_id[1] >> 24) & 0xff,
system_info_.cpu.x86_cpu_info.vendor_id[2] & 0xff,
(system_info_.cpu.x86_cpu_info.vendor_id[2] >> 8) & 0xff,
(system_info_.cpu.x86_cpu_info.vendor_id[2] >> 16) & 0xff,
(system_info_.cpu.x86_cpu_info.vendor_id[2] >> 24) & 0xff);
cpu_vendor_ = new string(cpu_vendor_string);
}
return cpu_vendor_;
}
void MinidumpSystemInfo::Print() {
if (!valid_)
return;
printf("MDRawSystemInfo\n");
printf(" processor_architecture = %d\n",
system_info_.processor_architecture);
printf(" processor_level = %d\n",
system_info_.processor_level);
printf(" processor_revision = 0x%x\n",
system_info_.processor_revision);
printf(" number_of_processors = %d\n",
system_info_.number_of_processors);
printf(" product_type = %d\n",
system_info_.product_type);
printf(" major_version = %d\n",
system_info_.major_version);
printf(" minor_version = %d\n",
system_info_.minor_version);
printf(" build_number = %d\n",
system_info_.build_number);
printf(" platform_id = %d\n",
system_info_.platform_id);
printf(" csd_version_rva = 0x%x\n",
system_info_.csd_version_rva);
printf(" suite_mask = 0x%x\n",
system_info_.suite_mask);
for (unsigned int i = 0; i < 3; ++i) {
printf(" cpu.x86_cpu_info.vendor_id[%d] = 0x%x\n",
i, system_info_.cpu.x86_cpu_info.vendor_id[i]);
}
printf(" cpu.x86_cpu_info.version_information = 0x%x\n",
system_info_.cpu.x86_cpu_info.version_information);
printf(" cpu.x86_cpu_info.feature_information = 0x%x\n",
system_info_.cpu.x86_cpu_info.feature_information);
printf(" cpu.x86_cpu_info.amd_extended_cpu_features = 0x%x\n",
system_info_.cpu.x86_cpu_info.amd_extended_cpu_features);
const string* csd_version = GetCSDVersion();
if (csd_version) {
printf(" (csd_version) = \"%s\"\n",
csd_version->c_str());
} else {
printf(" (csd_version) = (null)\n");
}
const string* cpu_vendor = GetCPUVendor();
if (cpu_vendor) {
printf(" (cpu_vendor) = \"%s\"\n",
cpu_vendor->c_str());
} else {
printf(" (cpu_vendor) = (null)\n");
}
printf("\n");
}
//
// MinidumpMiscInfo
//
MinidumpMiscInfo::MinidumpMiscInfo(Minidump* minidump)
: MinidumpStream(minidump),
misc_info_() {
}
bool MinidumpMiscInfo::Read(u_int32_t expected_size) {
valid_ = false;
if (expected_size != MD_MISCINFO_SIZE &&
expected_size != MD_MISCINFO2_SIZE) {
return false;
}
if (!minidump_->ReadBytes(&misc_info_, expected_size))
return false;
if (minidump_->swap()) {
Swap(&misc_info_.size_of_info);
Swap(&misc_info_.flags1);
Swap(&misc_info_.process_id);
Swap(&misc_info_.process_create_time);
Swap(&misc_info_.process_user_time);
Swap(&misc_info_.process_kernel_time);
if (misc_info_.size_of_info > MD_MISCINFO_SIZE) {
Swap(&misc_info_.processor_max_mhz);
Swap(&misc_info_.processor_current_mhz);
Swap(&misc_info_.processor_mhz_limit);
Swap(&misc_info_.processor_max_idle_state);
Swap(&misc_info_.processor_current_idle_state);
}
}
if (misc_info_.size_of_info != expected_size)
return false;
valid_ = true;
return true;
}
void MinidumpMiscInfo::Print() {
if (!valid_)
return;
printf("MDRawMiscInfo\n");
printf(" size_of_info = %d\n", misc_info_.size_of_info);
printf(" flags1 = 0x%x\n", misc_info_.flags1);
printf(" process_id = 0x%x\n", misc_info_.process_id);
printf(" process_create_time = 0x%x\n",
misc_info_.process_create_time);
printf(" process_user_time = 0x%x\n",
misc_info_.process_user_time);
printf(" process_kernel_time = 0x%x\n",
misc_info_.process_kernel_time);
if (misc_info_.size_of_info > MD_MISCINFO_SIZE) {
printf(" processor_max_mhz = %d\n",
misc_info_.processor_max_mhz);
printf(" processor_current_mhz = %d\n",
misc_info_.processor_current_mhz);
printf(" processor_mhz_limit = %d\n",
misc_info_.processor_mhz_limit);
printf(" processor_max_idle_state = 0x%x\n",
misc_info_.processor_max_idle_state);
printf(" processor_current_idle_state = 0x%x\n",
misc_info_.processor_current_idle_state);
}
printf("\n");
}
//
// MinidumpBreakpadInfo
//
MinidumpBreakpadInfo::MinidumpBreakpadInfo(Minidump* minidump)
: MinidumpStream(minidump),
breakpad_info_() {
}
bool MinidumpBreakpadInfo::Read(u_int32_t expected_size) {
valid_ = false;
if (expected_size != sizeof(breakpad_info_))
return false;
if (!minidump_->ReadBytes(&breakpad_info_, sizeof(breakpad_info_)))
return false;
if (minidump_->swap()) {
Swap(&breakpad_info_.validity);
Swap(&breakpad_info_.dump_thread_id);
Swap(&breakpad_info_.requesting_thread_id);
}
valid_ = true;
return true;
}
bool MinidumpBreakpadInfo::GetDumpThreadID(u_int32_t *thread_id) const {
if (!thread_id || !valid_ ||
!(breakpad_info_.validity & MD_BREAKPAD_INFO_VALID_DUMP_THREAD_ID)) {
return false;
}
*thread_id = breakpad_info_.dump_thread_id;
return true;
}
bool MinidumpBreakpadInfo::GetRequestingThreadID(u_int32_t *thread_id)
const {
if (!thread_id || !valid_ ||
!(breakpad_info_.validity & MD_BREAKPAD_INFO_VALID_REQUESTING_THREAD_ID)) {
return false;
}
*thread_id = breakpad_info_.requesting_thread_id;
return true;
}
void MinidumpBreakpadInfo::Print() {
if (!valid_)
return;
printf("MDRawBreakpadInfo\n");
printf(" validity = 0x%x\n", breakpad_info_.validity);
if (breakpad_info_.validity & MD_BREAKPAD_INFO_VALID_DUMP_THREAD_ID) {
printf(" dump_thread_id = 0x%x\n", breakpad_info_.dump_thread_id);
} else {
printf(" dump_thread_id = (invalid)\n");
}
if (breakpad_info_.validity & MD_BREAKPAD_INFO_VALID_DUMP_THREAD_ID) {
printf(" requesting_thread_id = 0x%x\n",
breakpad_info_.requesting_thread_id);
} else {
printf(" requesting_thread_id = (invalid)\n");
}
printf("\n");
}
//
// Minidump
//
Minidump::Minidump(const string& path)
: header_(),
directory_(NULL),
stream_map_(new MinidumpStreamMap()),
path_(path),
fd_(-1),
swap_(false),
valid_(false) {
}
Minidump::~Minidump() {
delete directory_;
delete stream_map_;
if (fd_ != -1)
close(fd_);
}
bool Minidump::Open() {
if (fd_ != -1) {
// The file is already open. Seek to the beginning, which is the position
// the file would be at if it were opened anew.
return SeekSet(0);
}
// O_BINARY is useful (and defined) on Windows. On other platforms, it's
// useless, and because it's defined as 0 above, harmless.
fd_ = open(path_.c_str(), O_RDONLY | O_BINARY);
if (fd_ == -1)
return false;
return true;
}
bool Minidump::Read() {
// Invalidate cached data.
delete directory_;
directory_ = NULL;
stream_map_->clear();
valid_ = false;
if (!Open())
return false;
if (!ReadBytes(&header_, sizeof(MDRawHeader)))
return false;
if (header_.signature != MD_HEADER_SIGNATURE) {
// The file may be byte-swapped. Under the present architecture, these
// classes don't know or need to know what CPU (or endianness) the
// minidump was produced on in order to parse it. Use the signature as
// a byte order marker.
u_int32_t signature_swapped = header_.signature;
Swap(&signature_swapped);
if (signature_swapped != MD_HEADER_SIGNATURE) {
// This isn't a minidump or a byte-swapped minidump.
return false;
}
swap_ = true;
} else {
// The file is not byte-swapped. Set swap_ false (it may have been true
// if the object is being reused?)
swap_ = false;
}
if (swap_) {
Swap(&header_.signature);
Swap(&header_.version);
Swap(&header_.stream_count);
Swap(&header_.stream_directory_rva);
Swap(&header_.checksum);
Swap(&header_.time_date_stamp);
Swap(&header_.flags);
}
// Version check. The high 16 bits of header_.version contain something
// else "implementation specific."
if ((header_.version & 0x0000ffff) != MD_HEADER_VERSION) {
return false;
}
if (!SeekSet(header_.stream_directory_rva))
return false;
if (header_.stream_count) {
// TODO(mmentovai): verify rational size!
scoped_ptr<MinidumpDirectoryEntries> directory(
new MinidumpDirectoryEntries(header_.stream_count));
// Read the entire array in one fell swoop, instead of reading one entry
// at a time in the loop.
if (!ReadBytes(&(*directory)[0],
sizeof(MDRawDirectory) * header_.stream_count))
return false;
for (unsigned int stream_index = 0;
stream_index < header_.stream_count;
++stream_index) {
MDRawDirectory* directory_entry = &(*directory)[stream_index];
if (swap_) {
Swap(&directory_entry->stream_type);
Swap(&directory_entry->location);
}
// Initialize the stream_map_ map, which speeds locating a stream by
// type.
unsigned int stream_type = directory_entry->stream_type;
switch (stream_type) {
case MD_THREAD_LIST_STREAM:
case MD_MODULE_LIST_STREAM:
case MD_MEMORY_LIST_STREAM:
case MD_EXCEPTION_STREAM:
case MD_SYSTEM_INFO_STREAM:
case MD_MISC_INFO_STREAM:
case MD_BREAKPAD_INFO_STREAM: {
if (stream_map_->find(stream_type) != stream_map_->end()) {
// Another stream with this type was already found. A minidump
// file should contain at most one of each of these stream types.
return false;
}
// Fall through to default
}
default: {
// Overwrites for stream types other than those above, but it's
// expected to be the user's burden in that case.
(*stream_map_)[stream_type].stream_index = stream_index;
}
}
}
directory_ = directory.release();
}
valid_ = true;
return true;
}
MinidumpThreadList* Minidump::GetThreadList() {
MinidumpThreadList* thread_list;
return GetStream(&thread_list);
}
MinidumpModuleList* Minidump::GetModuleList() {
MinidumpModuleList* module_list;
return GetStream(&module_list);
}
MinidumpMemoryList* Minidump::GetMemoryList() {
MinidumpMemoryList* memory_list;
return GetStream(&memory_list);
}
MinidumpException* Minidump::GetException() {
MinidumpException* exception;
return GetStream(&exception);
}
MinidumpSystemInfo* Minidump::GetSystemInfo() {
MinidumpSystemInfo* system_info;
return GetStream(&system_info);
}
MinidumpMiscInfo* Minidump::GetMiscInfo() {
MinidumpMiscInfo* misc_info;
return GetStream(&misc_info);
}
MinidumpBreakpadInfo* Minidump::GetBreakpadInfo() {
MinidumpBreakpadInfo* breakpad_info;
return GetStream(&breakpad_info);
}
void Minidump::Print() {
if (!valid_)
return;
printf("MDRawHeader\n");
printf(" signature = 0x%x\n", header_.signature);
printf(" version = 0x%x\n", header_.version);
printf(" stream_count = %d\n", header_.stream_count);
printf(" stream_directory_rva = 0x%x\n", header_.stream_directory_rva);
printf(" checksum = 0x%x\n", header_.checksum);
struct tm timestruct;
gmtime_r(reinterpret_cast<time_t*>(&header_.time_date_stamp), ×truct);
char timestr[20];
strftime(timestr, 20, "%Y-%m-%d %H:%M:%S", ×truct);
printf(" time_date_stamp = 0x%x %s\n", header_.time_date_stamp,
timestr);
printf(" flags = 0x%llx\n", header_.flags);
printf("\n");
for (unsigned int stream_index = 0;
stream_index < header_.stream_count;
++stream_index) {
MDRawDirectory* directory_entry = &(*directory_)[stream_index];
printf("mDirectory[%d]\n", stream_index);
printf("MDRawDirectory\n");
printf(" stream_type = %d\n", directory_entry->stream_type);
printf(" location.data_size = %d\n",
directory_entry->location.data_size);
printf(" location.rva = 0x%x\n", directory_entry->location.rva);
printf("\n");
}
printf("Streams:\n");
for (MinidumpStreamMap::const_iterator iterator = stream_map_->begin();
iterator != stream_map_->end();
++iterator) {
u_int32_t stream_type = iterator->first;
MinidumpStreamInfo info = iterator->second;
printf(" stream type 0x%x at index %d\n", stream_type, info.stream_index);
}
printf("\n");
}
const MDRawDirectory* Minidump::GetDirectoryEntryAtIndex(unsigned int index)
const {
if (!valid_ || index >= header_.stream_count)
return NULL;
return &(*directory_)[index];
}
bool Minidump::ReadBytes(void* bytes, size_t count) {
// Can't check valid_ because Read needs to call this method before
// validity can be determined. The only member that this method
// depends on is mFD, and an unset or invalid fd may generate an
// error but should not cause a crash.
ssize_t bytes_read = read(fd_, bytes, count);
if (static_cast<size_t>(bytes_read) != count)
return false;
return true;
}
bool Minidump::SeekSet(off_t offset) {
// Can't check valid_ because Read needs to call this method before
// validity can be determined. The only member that this method
// depends on is mFD, and an unset or invalid fd may generate an
// error but should not cause a crash.
off_t sought = lseek(fd_, offset, SEEK_SET);
if (sought != offset)
return false;
return true;
}
string* Minidump::ReadString(off_t offset) {
if (!valid_)
return NULL;
if (!SeekSet(offset))
return NULL;
u_int32_t bytes;
if (!ReadBytes(&bytes, sizeof(bytes)))
return NULL;
if (swap_)
Swap(&bytes);
if (bytes % 2 != 0)
return NULL;
unsigned int utf16_words = bytes / 2;
// TODO(mmentovai): verify rational size!
vector<u_int16_t> string_utf16(utf16_words);
if (utf16_words) {
if (!ReadBytes(&string_utf16[0], bytes)) {
return NULL;
}
}
return UTF16ToUTF8(string_utf16, swap_);
}
bool Minidump::SeekToStreamType(u_int32_t stream_type,
u_int32_t* stream_length) {
if (!valid_ || !stream_length)
return false;
MinidumpStreamMap::const_iterator iterator = stream_map_->find(stream_type);
if (iterator == stream_map_->end()) {
// This stream type didn't exist in the directory.
return false;
}
MinidumpStreamInfo info = iterator->second;
if (info.stream_index >= header_.stream_count)
return false;
MDRawDirectory* directory_entry = &(*directory_)[info.stream_index];
if (!SeekSet(directory_entry->location.rva))
return false;
*stream_length = directory_entry->location.data_size;
return true;
}
template<typename T>
T* Minidump::GetStream(T** stream) {
// stream is a garbage parameter that's present only to account for C++'s
// inability to overload a method based solely on its return type.
if (!stream)
return NULL;
*stream = NULL;
if (!valid_)
return NULL;
u_int32_t stream_type = T::kStreamType;
MinidumpStreamMap::iterator iterator = stream_map_->find(stream_type);
if (iterator == stream_map_->end()) {
// This stream type didn't exist in the directory.
return NULL;
}
// Get a pointer so that the stored stream field can be altered.
MinidumpStreamInfo* info = &iterator->second;
if (info->stream) {
// This cast is safe because info.stream is only populated by this
// method, and there is a direct correlation between T and stream_type.
*stream = static_cast<T*>(info->stream);
return *stream;
}
u_int32_t stream_length;
if (!SeekToStreamType(stream_type, &stream_length))
return NULL;
scoped_ptr<T> new_stream(new T(this));
if (!new_stream->Read(stream_length))
return NULL;
*stream = new_stream.release();
info->stream = *stream;
return *stream;
}
} // namespace google_breakpad
|