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 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298
|
#!/usr/bin/env python3
#
# Copyright 2012 the V8 project authors. 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.
# flake8: noqa # https://bugs.chromium.org/p/v8/issues/detail?id=8784
import http.server as http_server
import bisect
import html
import cmd
import codecs
import ctypes
import datetime
import disasm
import inspect
import mmap
import optparse
import os
import re
import io
import sys
import types
import urllib.parse
import webbrowser
try:
import v8heapconst
except ImportError:
print("Importing v8heapconst.py failed. "
"Run `tools/dev/gm.py mkgrokdump` to regenerate it.")
sys.exit(1)
PORT_NUMBER = 8081
USAGE="""usage: %prog [OPTIONS] [DUMP-FILE]
Minidump analyzer.
Shows the processor state at the point of exception including the
stack of the active thread and the referenced objects in the V8
heap. Code objects are disassembled and the addresses linked from the
stack (e.g. pushed return addresses) are marked with "=>".
Examples:
$ %prog 12345678-1234-1234-1234-123456789abcd-full.dmp"""
DEBUG=False
def DebugPrint(s):
if not DEBUG: return
print(s)
class Descriptor(object):
"""Descriptor of a structure in a memory."""
def __init__(self, fields):
self.fields = fields
self.is_flexible = False
for _, type_or_func in fields:
if isinstance(type_or_func, types.FunctionType):
self.is_flexible = True
break
if not self.is_flexible:
self.ctype = Descriptor._GetCtype(fields)
self.size = ctypes.sizeof(self.ctype)
def Read(self, memory, offset):
if self.is_flexible:
fields_copy = self.fields[:]
last = 0
for name, type_or_func in fields_copy:
if isinstance(type_or_func, types.FunctionType):
partial_ctype = Descriptor._GetCtype(fields_copy[:last])
partial_object = partial_ctype.from_buffer(memory, offset)
type = type_or_func(partial_object)
if type is not None:
fields_copy[last] = (name, type)
last += 1
else:
last += 1
complete_ctype = Descriptor._GetCtype(fields_copy[:last])
else:
complete_ctype = self.ctype
return complete_ctype.from_buffer(memory, offset)
@staticmethod
def _GetCtype(fields):
class Raw(ctypes.Structure):
_fields_ = fields
_pack_ = 1
def __str__(self):
return "{" + ", ".join("%s: %s" % (field, self.__getattribute__(field))
for field, _ in Raw._fields_) + "}"
return Raw
def FullDump(reader, heap):
"""Dump all available memory regions."""
def dump_region(reader, start, size, location):
print()
while start & 3 != 0:
start += 1
size -= 1
location += 1
is_executable = reader.IsProbableExecutableRegion(location, size)
is_ascii = reader.IsProbableASCIIRegion(location, size)
if is_executable is not False:
lines = reader.GetDisasmLines(start, size)
for line in lines:
print(FormatDisasmLine(start, heap, line))
print()
if is_ascii is not False:
# Output in the same format as the Unix hd command
addr = start
for i in range(0, size, 16):
slot = i + location
hex_line = ""
asc_line = ""
for i in range(16):
if slot + i < location + size:
byte = ctypes.c_uint8.from_buffer(reader.minidump, slot + i).value
if byte >= 0x20 and byte < 0x7f:
asc_line += chr(byte)
else:
asc_line += "."
hex_line += " %02x" % (byte)
else:
hex_line += " "
if i == 7:
hex_line += " "
print("%s %s |%s|" % (reader.FormatIntPtr(addr),
hex_line,
asc_line))
addr += 16
if is_executable is not True and is_ascii is not True:
print("%s - %s" % (reader.FormatIntPtr(start),
reader.FormatIntPtr(start + size)))
print(start + size + 1);
for i in range(0, size, reader.MachinePointerSize()):
slot = start + i
maybe_address = reader.ReadUIntPtr(slot)
heap_object = heap.FindObject(maybe_address)
print("%s: %s" % (reader.FormatIntPtr(slot),
reader.FormatIntPtr(maybe_address)))
if heap_object:
heap_object.Print(Printer())
print()
reader.ForEachMemoryRegion(dump_region)
# Heap constants generated by 'make grokdump' in v8heapconst module.
INSTANCE_TYPES = v8heapconst.INSTANCE_TYPES
KNOWN_MAPS = v8heapconst.KNOWN_MAPS
KNOWN_OBJECTS = v8heapconst.KNOWN_OBJECTS
FRAME_MARKERS = v8heapconst.FRAME_MARKERS
# Markers pushed on the stack by PushStackTraceAndDie
MAGIC_MARKER_PAIRS = (
(0xbbbbbbbb, 0xbbbbbbbb),
(0xfefefefe, 0xfefefeff),
)
# See StackTraceFailureMessage in isolate.h
STACK_TRACE_MARKER = 0xdecade30
STACK_TRACE_MID_MARKER = 0xdecade33
STACK_TRACE_END_MARKER = 0xdecade36
STACK_TRACE_OLD_END_MARKER = 0xdecade31
# See FailureMessage in logging.cc
ERROR_MESSAGE_MARKER = 0xdecade10
# Set of structures and constants that describe the layout of minidump
# files. Based on MSDN and Google Breakpad.
MINIDUMP_HEADER = Descriptor([
("signature", ctypes.c_uint32),
("version", ctypes.c_uint32),
("stream_count", ctypes.c_uint32),
("stream_directories_rva", ctypes.c_uint32),
("checksum", ctypes.c_uint32),
("time_date_stampt", ctypes.c_uint32),
("flags", ctypes.c_uint64)
])
MINIDUMP_LOCATION_DESCRIPTOR = Descriptor([
("data_size", ctypes.c_uint32),
("rva", ctypes.c_uint32)
])
MINIDUMP_STRING = Descriptor([
("length", ctypes.c_uint32),
("buffer", lambda t: ctypes.c_uint8 * (t.length + 2))
])
MINIDUMP_DIRECTORY = Descriptor([
("stream_type", ctypes.c_uint32),
("location", MINIDUMP_LOCATION_DESCRIPTOR.ctype)
])
MD_EXCEPTION_MAXIMUM_PARAMETERS = 15
MINIDUMP_EXCEPTION = Descriptor([
("code", ctypes.c_uint32),
("flags", ctypes.c_uint32),
("record", ctypes.c_uint64),
("address", ctypes.c_uint64),
("parameter_count", ctypes.c_uint32),
("unused_alignment", ctypes.c_uint32),
("information", ctypes.c_uint64 * MD_EXCEPTION_MAXIMUM_PARAMETERS)
])
MINIDUMP_EXCEPTION_STREAM = Descriptor([
("thread_id", ctypes.c_uint32),
("unused_alignment", ctypes.c_uint32),
("exception", MINIDUMP_EXCEPTION.ctype),
("thread_context", MINIDUMP_LOCATION_DESCRIPTOR.ctype)
])
# Stream types.
MD_UNUSED_STREAM = 0
MD_RESERVED_STREAM_0 = 1
MD_RESERVED_STREAM_1 = 2
MD_THREAD_LIST_STREAM = 3
MD_MODULE_LIST_STREAM = 4
MD_MEMORY_LIST_STREAM = 5
MD_EXCEPTION_STREAM = 6
MD_SYSTEM_INFO_STREAM = 7
MD_THREAD_EX_LIST_STREAM = 8
MD_MEMORY_64_LIST_STREAM = 9
MD_COMMENT_STREAM_A = 10
MD_COMMENT_STREAM_W = 11
MD_HANDLE_DATA_STREAM = 12
MD_FUNCTION_TABLE_STREAM = 13
MD_UNLOADED_MODULE_LIST_STREAM = 14
MD_MISC_INFO_STREAM = 15
MD_MEMORY_INFO_LIST_STREAM = 16
MD_THREAD_INFO_LIST_STREAM = 17
MD_HANDLE_OPERATION_LIST_STREAM = 18
MD_FLOATINGSAVEAREA_X86_REGISTERAREA_SIZE = 80
MINIDUMP_FLOATING_SAVE_AREA_X86 = Descriptor([
("control_word", ctypes.c_uint32),
("status_word", ctypes.c_uint32),
("tag_word", ctypes.c_uint32),
("error_offset", ctypes.c_uint32),
("error_selector", ctypes.c_uint32),
("data_offset", ctypes.c_uint32),
("data_selector", ctypes.c_uint32),
("register_area", ctypes.c_uint8 * MD_FLOATINGSAVEAREA_X86_REGISTERAREA_SIZE),
("cr0_npx_state", ctypes.c_uint32)
])
MD_CONTEXT_X86_EXTENDED_REGISTERS_SIZE = 512
# Context flags.
MD_CONTEXT_X86 = 0x00010000
MD_CONTEXT_X86_CONTROL = (MD_CONTEXT_X86 | 0x00000001)
MD_CONTEXT_X86_INTEGER = (MD_CONTEXT_X86 | 0x00000002)
MD_CONTEXT_X86_SEGMENTS = (MD_CONTEXT_X86 | 0x00000004)
MD_CONTEXT_X86_FLOATING_POINT = (MD_CONTEXT_X86 | 0x00000008)
MD_CONTEXT_X86_DEBUG_REGISTERS = (MD_CONTEXT_X86 | 0x00000010)
MD_CONTEXT_X86_EXTENDED_REGISTERS = (MD_CONTEXT_X86 | 0x00000020)
def EnableOnFlag(type, flag):
return lambda o: [None, type][int((o.context_flags & flag) != 0)]
MINIDUMP_CONTEXT_X86 = Descriptor([
("context_flags", ctypes.c_uint32),
# MD_CONTEXT_X86_DEBUG_REGISTERS.
("dr0", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_DEBUG_REGISTERS)),
("dr1", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_DEBUG_REGISTERS)),
("dr2", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_DEBUG_REGISTERS)),
("dr3", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_DEBUG_REGISTERS)),
("dr6", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_DEBUG_REGISTERS)),
("dr7", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_DEBUG_REGISTERS)),
# MD_CONTEXT_X86_FLOATING_POINT.
("float_save", EnableOnFlag(MINIDUMP_FLOATING_SAVE_AREA_X86.ctype,
MD_CONTEXT_X86_FLOATING_POINT)),
# MD_CONTEXT_X86_SEGMENTS.
("gs", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_SEGMENTS)),
("fs", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_SEGMENTS)),
("es", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_SEGMENTS)),
("ds", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_SEGMENTS)),
# MD_CONTEXT_X86_INTEGER.
("edi", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_INTEGER)),
("esi", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_INTEGER)),
("ebx", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_INTEGER)),
("edx", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_INTEGER)),
("ecx", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_INTEGER)),
("eax", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_INTEGER)),
# MD_CONTEXT_X86_CONTROL.
("ebp", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_CONTROL)),
("eip", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_CONTROL)),
("cs", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_CONTROL)),
("eflags", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_CONTROL)),
("esp", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_CONTROL)),
("ss", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_X86_CONTROL)),
# MD_CONTEXT_X86_EXTENDED_REGISTERS.
("extended_registers",
EnableOnFlag(ctypes.c_uint8 * MD_CONTEXT_X86_EXTENDED_REGISTERS_SIZE,
MD_CONTEXT_X86_EXTENDED_REGISTERS))
])
MD_CONTEXT_ARM = 0x40000000
MD_CONTEXT_ARM_INTEGER = (MD_CONTEXT_ARM | 0x00000002)
MD_CONTEXT_ARM_FLOATING_POINT = (MD_CONTEXT_ARM | 0x00000004)
MD_FLOATINGSAVEAREA_ARM_FPR_COUNT = 32
MD_FLOATINGSAVEAREA_ARM_FPEXTRA_COUNT = 8
MINIDUMP_FLOATING_SAVE_AREA_ARM = Descriptor([
("fpscr", ctypes.c_uint64),
("regs", ctypes.c_uint64 * MD_FLOATINGSAVEAREA_ARM_FPR_COUNT),
("extra", ctypes.c_uint64 * MD_FLOATINGSAVEAREA_ARM_FPEXTRA_COUNT)
])
MINIDUMP_CONTEXT_ARM = Descriptor([
("context_flags", ctypes.c_uint32),
# MD_CONTEXT_ARM_INTEGER.
("r0", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r1", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r2", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r3", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r4", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r5", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r6", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r7", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r8", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r9", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r10", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r11", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("r12", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("sp", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("lr", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("pc", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_ARM_INTEGER)),
("cpsr", ctypes.c_uint32),
("float_save", EnableOnFlag(MINIDUMP_FLOATING_SAVE_AREA_ARM.ctype,
MD_CONTEXT_ARM_FLOATING_POINT))
])
MD_CONTEXT_ARM64 = 0x80000000
MD_CONTEXT_ARM64_INTEGER = (MD_CONTEXT_ARM64 | 0x00000002)
MD_CONTEXT_ARM64_FLOATING_POINT = (MD_CONTEXT_ARM64 | 0x00000004)
MD_FLOATINGSAVEAREA_ARM64_FPR_COUNT = 64
MINIDUMP_FLOATING_SAVE_AREA_ARM = Descriptor([
("fpscr", ctypes.c_uint64),
("regs", ctypes.c_uint64 * MD_FLOATINGSAVEAREA_ARM64_FPR_COUNT),
])
MINIDUMP_CONTEXT_ARM64 = Descriptor([
("context_flags", ctypes.c_uint64),
# MD_CONTEXT_ARM64_INTEGER.
("r0", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r1", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r2", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r3", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r4", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r5", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r6", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r7", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r8", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r9", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r10", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r11", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r12", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r13", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r14", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r15", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r16", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r17", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r18", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r19", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r20", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r21", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r22", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r23", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r24", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r25", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r26", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r27", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("r28", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("fp", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("lr", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("sp", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("pc", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_ARM64_INTEGER)),
("cpsr", ctypes.c_uint32),
("float_save", EnableOnFlag(MINIDUMP_FLOATING_SAVE_AREA_ARM.ctype,
MD_CONTEXT_ARM64_FLOATING_POINT))
])
MD_CONTEXT_AMD64 = 0x00100000
MD_CONTEXT_AMD64_CONTROL = (MD_CONTEXT_AMD64 | 0x00000001)
MD_CONTEXT_AMD64_INTEGER = (MD_CONTEXT_AMD64 | 0x00000002)
MD_CONTEXT_AMD64_SEGMENTS = (MD_CONTEXT_AMD64 | 0x00000004)
MD_CONTEXT_AMD64_FLOATING_POINT = (MD_CONTEXT_AMD64 | 0x00000008)
MD_CONTEXT_AMD64_DEBUG_REGISTERS = (MD_CONTEXT_AMD64 | 0x00000010)
MINIDUMP_CONTEXT_AMD64 = Descriptor([
("p1_home", ctypes.c_uint64),
("p2_home", ctypes.c_uint64),
("p3_home", ctypes.c_uint64),
("p4_home", ctypes.c_uint64),
("p5_home", ctypes.c_uint64),
("p6_home", ctypes.c_uint64),
("context_flags", ctypes.c_uint32),
("mx_csr", ctypes.c_uint32),
# MD_CONTEXT_AMD64_CONTROL.
("cs", EnableOnFlag(ctypes.c_uint16, MD_CONTEXT_AMD64_CONTROL)),
# MD_CONTEXT_AMD64_SEGMENTS
("ds", EnableOnFlag(ctypes.c_uint16, MD_CONTEXT_AMD64_SEGMENTS)),
("es", EnableOnFlag(ctypes.c_uint16, MD_CONTEXT_AMD64_SEGMENTS)),
("fs", EnableOnFlag(ctypes.c_uint16, MD_CONTEXT_AMD64_SEGMENTS)),
("gs", EnableOnFlag(ctypes.c_uint16, MD_CONTEXT_AMD64_SEGMENTS)),
# MD_CONTEXT_AMD64_CONTROL.
("ss", EnableOnFlag(ctypes.c_uint16, MD_CONTEXT_AMD64_CONTROL)),
("eflags", EnableOnFlag(ctypes.c_uint32, MD_CONTEXT_AMD64_CONTROL)),
# MD_CONTEXT_AMD64_DEBUG_REGISTERS.
("dr0", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
("dr1", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
("dr2", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
("dr3", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
("dr6", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
("dr7", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
# MD_CONTEXT_AMD64_INTEGER.
("rax", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("rcx", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("rdx", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("rbx", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
# MD_CONTEXT_AMD64_CONTROL.
("rsp", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_CONTROL)),
# MD_CONTEXT_AMD64_INTEGER.
("rbp", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("rsi", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("rdi", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("r8", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("r9", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("r10", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("r11", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("r12", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("r13", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("r14", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
("r15", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_INTEGER)),
# MD_CONTEXT_AMD64_CONTROL.
("rip", EnableOnFlag(ctypes.c_uint64, MD_CONTEXT_AMD64_CONTROL)),
# MD_CONTEXT_AMD64_FLOATING_POINT
("sse_registers", EnableOnFlag(ctypes.c_uint8 * (16 * 26),
MD_CONTEXT_AMD64_FLOATING_POINT)),
("vector_registers", EnableOnFlag(ctypes.c_uint8 * (16 * 26),
MD_CONTEXT_AMD64_FLOATING_POINT)),
("vector_control", EnableOnFlag(ctypes.c_uint64,
MD_CONTEXT_AMD64_FLOATING_POINT)),
# MD_CONTEXT_AMD64_DEBUG_REGISTERS.
("debug_control", EnableOnFlag(ctypes.c_uint64,
MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
("last_branch_to_rip", EnableOnFlag(ctypes.c_uint64,
MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
("last_branch_from_rip", EnableOnFlag(ctypes.c_uint64,
MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
("last_exception_to_rip", EnableOnFlag(ctypes.c_uint64,
MD_CONTEXT_AMD64_DEBUG_REGISTERS)),
("last_exception_from_rip", EnableOnFlag(ctypes.c_uint64,
MD_CONTEXT_AMD64_DEBUG_REGISTERS))
])
MINIDUMP_MEMORY_DESCRIPTOR = Descriptor([
("start", ctypes.c_uint64),
("memory", MINIDUMP_LOCATION_DESCRIPTOR.ctype)
])
MINIDUMP_MEMORY_DESCRIPTOR64 = Descriptor([
("start", ctypes.c_uint64),
("size", ctypes.c_uint64)
])
MINIDUMP_MEMORY_LIST = Descriptor([
("range_count", ctypes.c_uint32),
("ranges", lambda m: MINIDUMP_MEMORY_DESCRIPTOR.ctype * m.range_count)
])
MINIDUMP_MEMORY_LIST_Mac = Descriptor([
("range_count", ctypes.c_uint32),
("junk", ctypes.c_uint32),
("ranges", lambda m: MINIDUMP_MEMORY_DESCRIPTOR.ctype * m.range_count)
])
MINIDUMP_MEMORY_LIST64 = Descriptor([
("range_count", ctypes.c_uint64),
("base_rva", ctypes.c_uint64),
("ranges", lambda m: MINIDUMP_MEMORY_DESCRIPTOR64.ctype * m.range_count)
])
MINIDUMP_THREAD = Descriptor([
("id", ctypes.c_uint32),
("suspend_count", ctypes.c_uint32),
("priority_class", ctypes.c_uint32),
("priority", ctypes.c_uint32),
("ted", ctypes.c_uint64),
("stack", MINIDUMP_MEMORY_DESCRIPTOR.ctype),
("context", MINIDUMP_LOCATION_DESCRIPTOR.ctype)
])
MINIDUMP_THREAD_LIST = Descriptor([
("thread_count", ctypes.c_uint32),
("threads", lambda t: MINIDUMP_THREAD.ctype * t.thread_count)
])
MINIDUMP_THREAD_LIST_Mac = Descriptor([
("thread_count", ctypes.c_uint32),
("junk", ctypes.c_uint32),
("threads", lambda t: MINIDUMP_THREAD.ctype * t.thread_count)
])
MINIDUMP_VS_FIXEDFILEINFO = Descriptor([
("dwSignature", ctypes.c_uint32),
("dwStrucVersion", ctypes.c_uint32),
("dwFileVersionMS", ctypes.c_uint32),
("dwFileVersionLS", ctypes.c_uint32),
("dwProductVersionMS", ctypes.c_uint32),
("dwProductVersionLS", ctypes.c_uint32),
("dwFileFlagsMask", ctypes.c_uint32),
("dwFileFlags", ctypes.c_uint32),
("dwFileOS", ctypes.c_uint32),
("dwFileType", ctypes.c_uint32),
("dwFileSubtype", ctypes.c_uint32),
("dwFileDateMS", ctypes.c_uint32),
("dwFileDateLS", ctypes.c_uint32)
])
MINIDUMP_RAW_MODULE = Descriptor([
("base_of_image", ctypes.c_uint64),
("size_of_image", ctypes.c_uint32),
("checksum", ctypes.c_uint32),
("time_date_stamp", ctypes.c_uint32),
("module_name_rva", ctypes.c_uint32),
("version_info", MINIDUMP_VS_FIXEDFILEINFO.ctype),
("cv_record", MINIDUMP_LOCATION_DESCRIPTOR.ctype),
("misc_record", MINIDUMP_LOCATION_DESCRIPTOR.ctype),
("reserved0", ctypes.c_uint32 * 2),
("reserved1", ctypes.c_uint32 * 2)
])
MINIDUMP_MODULE_LIST = Descriptor([
("number_of_modules", ctypes.c_uint32),
("modules", lambda t: MINIDUMP_RAW_MODULE.ctype * t.number_of_modules)
])
MINIDUMP_MODULE_LIST_Mac = Descriptor([
("number_of_modules", ctypes.c_uint32),
("junk", ctypes.c_uint32),
("modules", lambda t: MINIDUMP_RAW_MODULE.ctype * t.number_of_modules)
])
MINIDUMP_RAW_SYSTEM_INFO = Descriptor([
("processor_architecture", ctypes.c_uint16)
])
MD_CPU_ARCHITECTURE_X86 = 0
MD_CPU_ARCHITECTURE_ARM = 5
# Breakpad used a custom value of 0x8003 here; Crashpad uses the new
# standardized value 12.
MD_CPU_ARCHITECTURE_ARM64 = 12
MD_CPU_ARCHITECTURE_ARM64_BREAKPAD_LEGACY = 0x8003
MD_CPU_ARCHITECTURE_AMD64 = 9
OBJDUMP_BIN = None
DEFAULT_OBJDUMP_BIN = '/usr/bin/objdump'
class FuncSymbol:
def __init__(self, start, size, name):
self.start = start
self.end = self.start + size
self.name = name
def __cmp__(self, other):
if isinstance(other, FuncSymbol):
return self.start - other.start
return self.start - other
def Covers(self, addr):
return (self.start <= addr) and (addr < self.end)
class MinidumpReader(object):
"""Minidump (.dmp) reader."""
_HEADER_MAGIC = 0x504d444d
def __init__(self, options, minidump_name):
self._reset()
self.minidump_name = minidump_name
if sys.platform == 'win32':
self.minidump_file = open(minidump_name, "a+")
self.minidump = mmap.mmap(self.minidump_file.fileno(), 0)
else:
self.minidump_file = open(minidump_name, "r")
self.minidump = mmap.mmap(self.minidump_file.fileno(), 0, mmap.MAP_PRIVATE)
self.header = MINIDUMP_HEADER.Read(self.minidump, 0)
if self.header.signature != MinidumpReader._HEADER_MAGIC:
print("Warning: Unsupported minidump header magic!", file=sys.stderr)
DebugPrint(self.header)
offset = self.header.stream_directories_rva
directories = []
for _ in range(self.header.stream_count):
directories.append(MINIDUMP_DIRECTORY.Read(self.minidump, offset))
offset += MINIDUMP_DIRECTORY.size
self.symdir = options.symdir
self._ReadArchitecture(directories)
self._ReadDirectories(directories)
self._FindObjdump(options)
def _reset(self):
self.header = None
self.arch = None
self.exception = None
self.exception_context = None
self.memory_list = None
self.memory_list64 = None
self.module_list = None
self.thread_map = {}
self.modules_with_symbols = []
self.symbols = []
def _ReadArchitecture(self, directories):
# Find MDRawSystemInfo stream and determine arch.
for d in directories:
if d.stream_type == MD_SYSTEM_INFO_STREAM:
system_info = MINIDUMP_RAW_SYSTEM_INFO.Read(
self.minidump, d.location.rva)
self.arch = system_info.processor_architecture
if self.arch == MD_CPU_ARCHITECTURE_ARM64_BREAKPAD_LEGACY:
self.arch = MD_CPU_ARCHITECTURE_ARM64
assert self.arch in [MD_CPU_ARCHITECTURE_AMD64,
MD_CPU_ARCHITECTURE_ARM,
MD_CPU_ARCHITECTURE_ARM64,
MD_CPU_ARCHITECTURE_X86]
assert not self.arch is None
def _ReadDirectories(self, directories):
for d in directories:
DebugPrint(d)
if d.stream_type == MD_EXCEPTION_STREAM:
self.exception = MINIDUMP_EXCEPTION_STREAM.Read(
self.minidump, d.location.rva)
DebugPrint(self.exception)
self.exception_context = self.ContextDescriptor().Read(
self.minidump, self.exception.thread_context.rva)
DebugPrint(self.exception_context)
elif d.stream_type == MD_THREAD_LIST_STREAM:
thread_list = MINIDUMP_THREAD_LIST.Read(self.minidump, d.location.rva)
if ctypes.sizeof(thread_list) + 4 == d.location.data_size:
thread_list = MINIDUMP_THREAD_LIST_Mac.Read(
self.minidump, d.location.rva)
assert ctypes.sizeof(thread_list) == d.location.data_size
DebugPrint(thread_list)
for thread in thread_list.threads:
DebugPrint(thread)
self.thread_map[thread.id] = thread
elif d.stream_type == MD_MODULE_LIST_STREAM:
assert self.module_list is None
self.module_list = MINIDUMP_MODULE_LIST.Read(
self.minidump, d.location.rva)
if ctypes.sizeof(self.module_list) + 4 == d.location.data_size:
self.module_list = MINIDUMP_MODULE_LIST_Mac.Read(
self.minidump, d.location.rva)
assert ctypes.sizeof(self.module_list) == d.location.data_size
DebugPrint(self.module_list)
elif d.stream_type == MD_MEMORY_LIST_STREAM:
print("Warning: This is not a full minidump!", file=sys.stderr)
assert self.memory_list is None
self.memory_list = MINIDUMP_MEMORY_LIST.Read(
self.minidump, d.location.rva)
if ctypes.sizeof(self.memory_list) + 4 == d.location.data_size:
self.memory_list = MINIDUMP_MEMORY_LIST_Mac.Read(
self.minidump, d.location.rva)
assert ctypes.sizeof(self.memory_list) == d.location.data_size
DebugPrint(self.memory_list)
elif d.stream_type == MD_MEMORY_64_LIST_STREAM:
assert self.memory_list64 is None
self.memory_list64 = MINIDUMP_MEMORY_LIST64.Read(
self.minidump, d.location.rva)
assert ctypes.sizeof(self.memory_list64) == d.location.data_size
DebugPrint(self.memory_list64)
def _FindObjdump(self, options):
if options.objdump:
objdump_bin = options.objdump
else:
objdump_bin = self._FindThirdPartyObjdump()
if not objdump_bin or not os.path.exists(objdump_bin):
print("# Cannot find '%s', falling back to default objdump '%s'" % (
objdump_bin, DEFAULT_OBJDUMP_BIN))
objdump_bin = DEFAULT_OBJDUMP_BIN
global OBJDUMP_BIN
OBJDUMP_BIN = objdump_bin
disasm.OBJDUMP_BIN = objdump_bin
def _FindThirdPartyObjdump(self):
# Try to find the platform specific objdump.
if self.arch == MD_CPU_ARCHITECTURE_ARM:
platform_filter = 'arm-linux'
elif self.arch == MD_CPU_ARCHITECTURE_ARM64:
platform_filter = 'aarch64'
else:
# Use default otherwise.
return None
print(("# Looking for platform specific (%s) objdump in "
"third_party directory.") % platform_filter)
third_party_dir = os.path.join(
os.path.dirname(os.path.dirname(__file__)), 'third_party')
for root, dirs, files in os.walk(third_party_dir):
for file in files:
if file.endswith("objdump") and platform_filter in file:
return os.path.join(root, file)
print("# Could not find platform specific objdump in third_party.")
print("# Make sure you installed the correct SDK.")
return None
def ContextDescriptor(self):
if self.arch == MD_CPU_ARCHITECTURE_X86:
return MINIDUMP_CONTEXT_X86
elif self.arch == MD_CPU_ARCHITECTURE_AMD64:
return MINIDUMP_CONTEXT_AMD64
elif self.arch == MD_CPU_ARCHITECTURE_ARM:
return MINIDUMP_CONTEXT_ARM
elif self.arch == MD_CPU_ARCHITECTURE_ARM64:
return MINIDUMP_CONTEXT_ARM64
else:
return None
def IsValidAlignedAddress(self, address):
return self.IsAlignedAddress(address) and self.IsValidAddress(address)
def IsValidAddress(self, address):
return self.FindLocation(address) is not None
def IsAlignedAddress(self, address):
return (address % self.MachinePointerSize()) == 0
def IsExceptionStackAddress(self, address):
if not self.IsAlignedAddress(address): return False
return self.IsAnyExceptionStackAddress(address)
def IsAnyExceptionStackAddress(self, address):
return self.StackTop() <= address <= self.StackBottom()
def IsValidExceptionStackAddress(self, address):
if not self.IsValidAddress(address): return False
return self.IsExceptionStackAddress(address)
def IsModuleAddress(self, address):
return self.GetModuleForAddress(address) is not None
def GetModuleForAddress(self, address):
for module in self.module_list.modules:
start = module.base_of_image
end = start + module.size_of_image
if start <= address < end: return module
return None
def ReadU8(self, address):
location = self.FindLocation(address)
return ctypes.c_uint8.from_buffer(self.minidump, location).value
def ReadU32(self, address):
location = self.FindLocation(address)
return ctypes.c_uint32.from_buffer(self.minidump, location).value
def ReadU64(self, address):
location = self.FindLocation(address)
return ctypes.c_uint64.from_buffer(self.minidump, location).value
def Is64(self):
return (self.arch == MD_CPU_ARCHITECTURE_ARM64 or
self.arch == MD_CPU_ARCHITECTURE_AMD64)
def IsPointerCompressed(self):
# Assume all 64-bit builds are pointer compressed.
return self.Is64()
def Is32BitTagged(self):
return not self.Is64() or self.IsPointerCompressed()
def ReadTagged(self, address):
if self.Is32BitTagged():
tagged = self.ReadU32(address)
if self.IsPointerCompressed():
# Uncompress using the slot address.
return (address & (0xFFFFFFFF << 32)) | (tagged & 0xFFFFFFFF)
else:
return tagged
return self.ReadU64(address)
def ReadUIntPtr(self, address):
if self.Is64():
return self.ReadU64(address)
return self.ReadU32(address)
def ReadSized(self, address, size):
if size == 8:
return self.ReadU64(address)
assert (size == 4)
return self.ReadU32(address)
def ReadBytes(self, address, size):
location = self.FindLocation(address)
return self.minidump[location:location + size]
def _ReadWord(self, location):
if self.Is64():
return ctypes.c_uint64.from_buffer(self.minidump, location).value
return ctypes.c_uint32.from_buffer(self.minidump, location).value
def ReadAsciiPtr(self, address):
ascii_content = [
chr(c) if c >= 0x20 and c < 0x7f else '.'
for c in self.ReadBytes(address, self.MachinePointerSize())
]
return ''.join(ascii_content)
def ReadAsciiString(self, address):
string = ""
while self.IsValidAddress(address):
code = self.ReadU8(address)
if 0 < code < 128:
string += chr(code)
else:
break
address += 1
return string
def IsProbableASCIIRegion(self, location, length):
ascii_bytes = 0
non_ascii_bytes = 0
for i in range(length):
loc = location + i
byte = ctypes.c_uint8.from_buffer(self.minidump, loc).value
if byte >= 0x7f:
non_ascii_bytes += 1
if byte < 0x20 and byte != 0:
non_ascii_bytes += 1
if byte < 0x7f and byte >= 0x20:
ascii_bytes += 1
if byte == 0xa: # newline
ascii_bytes += 1
if ascii_bytes * 10 <= length:
return False
if length > 0 and ascii_bytes > non_ascii_bytes * 7:
return True
if ascii_bytes > non_ascii_bytes * 3:
return None # Maybe
return False
def IsProbableExecutableRegion(self, location, length):
opcode_bytes = 0
sixty_four = self.Is64()
for i in range(length):
loc = location + i
byte = ctypes.c_uint8.from_buffer(self.minidump, loc).value
if (byte == 0x8b or # mov
byte == 0x89 or # mov reg-reg
(byte & 0xf0) == 0x50 or # push/pop
(sixty_four and (byte & 0xf0) == 0x40) or # rex prefix
byte == 0xc3 or # return
byte == 0x74 or # jeq
byte == 0x84 or # jeq far
byte == 0x75 or # jne
byte == 0x85 or # jne far
byte == 0xe8 or # call
byte == 0xe9 or # jmp far
byte == 0xeb): # jmp near
opcode_bytes += 1
opcode_percent = (opcode_bytes * 100) / length
threshold = 20
if opcode_percent > threshold + 2:
return True
if opcode_percent > threshold - 2:
return None # Maybe
return False
def FindRegion(self, addr):
answer = [-1, -1]
def is_in(reader, start, size, location):
if addr >= start and addr < start + size:
answer[0] = start
answer[1] = size
self.ForEachMemoryRegion(is_in)
if answer[0] == -1:
return None
return answer
def ForEachMemoryRegion(self, cb):
if self.memory_list64 is not None:
for r in self.memory_list64.ranges:
location = self.memory_list64.base_rva + offset
cb(self, r.start, r.size, location)
offset += r.size
if self.memory_list is not None:
for r in self.memory_list.ranges:
cb(self, r.start, r.memory.data_size, r.memory.rva)
def FindWord(self, word, alignment=0):
def search_inside_region(reader, start, size, location):
location = (location + alignment) & ~alignment
for i in range(size - self.MachinePointerSize()):
loc = location + i
if reader._ReadWord(loc) == word:
slot = start + (loc - location)
print("%s: %s" % (reader.FormatIntPtr(slot),
reader.FormatIntPtr(word)))
self.ForEachMemoryRegion(search_inside_region)
def FindPtr(self, expected_value, start, end):
ptr_size = self.MachinePointerSize()
for slot in range(start, end, ptr_size):
if not self.IsValidAddress(slot):
return None
value = self.ReadUIntPtr(slot)
if value == expected_value:
return slot
return None
def FindWordList(self, word):
aligned_res = []
unaligned_res = []
def search_inside_region(reader, start, size, location):
for i in range(size - self.MachinePointerSize()):
loc = location + i
if reader._ReadWord(loc) == word:
slot = start + (loc - location)
if self.IsAlignedAddress(slot):
aligned_res.append(slot)
else:
unaligned_res.append(slot)
self.ForEachMemoryRegion(search_inside_region)
return (aligned_res, unaligned_res)
def FindLocation(self, address):
offset = 0
if self.memory_list64 is not None:
for r in self.memory_list64.ranges:
if r.start <= address < r.start + r.size:
return self.memory_list64.base_rva + offset + address - r.start
offset += r.size
if self.memory_list is not None:
for r in self.memory_list.ranges:
if r.start <= address < r.start + r.memory.data_size:
return r.memory.rva + address - r.start
return None
def GetDisasmLines(self, address, size):
def CountUndefinedInstructions(lines):
pattern = "<UNDEFINED>"
return sum([line.count(pattern) for (ignore, line) in lines])
location = self.FindLocation(address)
if location is None: return []
arch = None
possible_objdump_flags = [""]
if self.arch == MD_CPU_ARCHITECTURE_X86:
arch = "ia32"
elif self.arch == MD_CPU_ARCHITECTURE_ARM:
arch = "arm"
possible_objdump_flags = ["", "--disassembler-options=force-thumb"]
elif self.arch == MD_CPU_ARCHITECTURE_ARM64:
arch = "arm64"
possible_objdump_flags = ["", "--disassembler-options=force-thumb"]
elif self.arch == MD_CPU_ARCHITECTURE_AMD64:
arch = "x64"
results = [ disasm.GetDisasmLines(self.minidump_name,
location,
size,
arch,
False,
objdump_flags)
for objdump_flags in possible_objdump_flags ]
return min(results, key=CountUndefinedInstructions)
def Dispose(self):
self._reset()
self.minidump.close()
self.minidump_file.close()
def ExceptionIP(self):
if self.arch == MD_CPU_ARCHITECTURE_AMD64:
return self.exception_context.rip
elif self.arch == MD_CPU_ARCHITECTURE_ARM:
return self.exception_context.pc
elif self.arch == MD_CPU_ARCHITECTURE_ARM64:
return self.exception_context.pc
elif self.arch == MD_CPU_ARCHITECTURE_X86:
return self.exception_context.eip
def ExceptionSP(self):
if self.arch == MD_CPU_ARCHITECTURE_AMD64:
return self.exception_context.rsp
elif self.arch == MD_CPU_ARCHITECTURE_ARM:
return self.exception_context.sp
elif self.arch == MD_CPU_ARCHITECTURE_ARM64:
return self.exception_context.sp
elif self.arch == MD_CPU_ARCHITECTURE_X86:
return self.exception_context.esp
def ExceptionFP(self):
if self.arch == MD_CPU_ARCHITECTURE_AMD64:
return self.exception_context.rbp
elif self.arch == MD_CPU_ARCHITECTURE_ARM:
return None
elif self.arch == MD_CPU_ARCHITECTURE_ARM64:
return self.exception_context.fp
elif self.arch == MD_CPU_ARCHITECTURE_X86:
return self.exception_context.ebp
def ExceptionThread(self):
return self.thread_map[self.exception.thread_id]
def StackTop(self):
return self.ExceptionSP()
def StackBottom(self):
exception_thread = self.ExceptionThread()
return exception_thread.stack.start + \
exception_thread.stack.memory.data_size
def FormatIntPtr(self, value):
if self.Is64():
return "%016x" % value
return "%08x" % value
def FormatTagged(self, value):
if self.Is64() and not self.IsPointerCompressed():
return "%016x" % value
return "%08x" % value
def MachinePointerSize(self):
if self.Is64():
return 8
return 4
def TaggedPointerSize(self):
if self.IsPointerCompressed():
return 4
return self.MachinePointerSize()
def Register(self, name):
return self.exception_context.__getattribute__(name)
def ReadMinidumpString(self, rva):
string = bytearray(MINIDUMP_STRING.Read(self.minidump, rva).buffer)
string = string.decode("utf16")
return string[0:len(string) - 1]
# Load FUNC records from a BreakPad symbol file
#
# http://code.google.com/p/google-breakpad/wiki/SymbolFiles
#
def _LoadSymbolsFrom(self, symfile, baseaddr):
print("Loading symbols from %s" % (symfile))
with open(symfile) as f:
for line in f:
result = re.match(
r"^FUNC ([a-f0-9]+) ([a-f0-9]+) ([a-f0-9]+) (.*)$", line)
if result is not None:
start = int(result.group(1), 16)
size = int(result.group(2), 16)
name = result.group(4).rstrip()
bisect.insort_left(self.symbols,
FuncSymbol(baseaddr + start, size, name))
print(" ... done")
def TryLoadSymbolsFor(self, modulename, module):
try:
symfile = os.path.join(self.symdir,
modulename.replace('.', '_') + ".pdb.sym")
if os.path.isfile(symfile):
self._LoadSymbolsFrom(symfile, module.base_of_image)
self.modules_with_symbols.append(module)
except Exception as e:
print(" ... failure (%s)" % (e))
# Returns true if address is covered by some module that has loaded symbols.
def _IsInModuleWithSymbols(self, addr):
for module in self.modules_with_symbols:
start = module.base_of_image
end = start + module.size_of_image
if (start <= addr) and (addr < end):
return True
return False
# Find symbol covering the given address and return its name in format
# <symbol name>+<offset from the start>
def FindSymbol(self, addr):
if not self._IsInModuleWithSymbols(addr):
return None
i = bisect.bisect_left(self.symbols, addr)
symbol = None
if (0 < i) and self.symbols[i - 1].Covers(addr):
symbol = self.symbols[i - 1]
elif (i < len(self.symbols)) and self.symbols[i].Covers(addr):
symbol = self.symbols[i]
else:
return None
diff = addr - symbol.start
return "%s+0x%x" % (symbol.name, diff)
class Printer(object):
"""Printer with indentation support."""
def __init__(self):
self.indent = 0
def Indent(self):
self.indent += 2
def Dedent(self):
self.indent -= 2
def Print(self, string):
print("%s%s" % (self._IndentString(), string))
def PrintLines(self, lines):
indent = self._IndentString()
print("\n".join("%s%s" % (indent, line) for line in lines))
def _IndentString(self):
return self.indent * " "
ADDRESS_RE = re.compile(r"0x[0-9a-fA-F]+")
def FormatDisasmLine(start, heap, line):
line_address = start + line[0]
stack_slot = heap.stack_map.get(line_address)
marker = " "
if stack_slot:
marker = "=>"
code = AnnotateAddresses(heap, line[1])
# Compute the actual call target which the disassembler is too stupid
# to figure out (it adds the call offset to the disassembly offset rather
# than the absolute instruction address).
if heap.reader.arch == MD_CPU_ARCHITECTURE_X86:
if code.startswith("e8"):
words = code.split()
if len(words) > 6 and words[5] == "call":
offset = int(words[4] + words[3] + words[2] + words[1], 16)
target = (line_address + offset + 5) & 0xFFFFFFFF
code = code.replace(words[6], "0x%08x" % target)
# TODO(jkummerow): port this hack to ARM and x64.
return "%s%08x %08x: %s" % (marker, line_address, line[0], code)
def AnnotateAddresses(heap, line):
extra = []
for m in ADDRESS_RE.finditer(line):
maybe_address = int(m.group(0), 16)
object = heap.FindObject(maybe_address)
if not object: continue
extra.append(str(object))
if len(extra) == 0: return line
return "%s ;; %s" % (line, ", ".join(extra))
class HeapObject(object):
def __init__(self, heap, map, address):
self.heap = heap
self.map = map
self.address = address
def Is(self, cls):
return isinstance(self, cls)
def Print(self, p):
p.Print(str(self))
def __str__(self):
instance_type = "???"
if self.map is not None:
instance_type = INSTANCE_TYPES[self.map.instance_type]
return "%s(%s, %s)" % (self.__class__.__name__,
self.heap.reader.FormatIntPtr(self.address),
instance_type)
def ObjectField(self, offset):
if not self.heap.reader.IsValidAddress(self.address + offset):
return None
field_value = self.heap.reader.ReadTagged(self.address + offset)
return self.heap.FindObjectOrSmi(field_value)
def SmiField(self, offset):
if not self.heap.reader.IsValidAddress(self.address + offset):
return None
field_value = self.heap.reader.ReadTagged(self.address + offset)
if self.heap.IsSmi(field_value):
return self.heap.SmiUntag(field_value)
return None
def Uint32Field(self, offset):
if not self.heap.reader.IsValidAddress(self.address + offset):
return None
return self.heap.reader.ReadU32(self.address + offset)
class Map(HeapObject):
def Decode(self, offset, size, value):
return (value >> offset) & ((1 << size) - 1)
# Instance Sizes
def InstanceSizesOffset(self):
return self.heap.TaggedPointerSize()
def InstanceSizeOffset(self):
return self.InstanceSizesOffset()
def InObjectProperties(self):
return self.InstanceSizeOffset() + 1
def UnusedByte(self):
return self.InObjectProperties() + 1
def VisitorId(self):
return self.UnusedByte() + 1
# Instance Attributes
def InstanceAttributesOffset(self):
return self.InstanceSizesOffset() + self.heap.IntSize()
def InstanceTypeOffset(self):
return self.InstanceAttributesOffset()
def BitFieldOffset(self):
return self.InstanceTypeOffset() + 1
def BitField2Offset(self):
return self.BitFieldOffset() + 1
def UnusedPropertyFieldsOffset(self):
return self.BitField2Offset() + 1
# Other fields
def BitField3Offset(self):
return self.InstanceAttributesOffset() + self.heap.IntSize()
def PrototypeOffset(self):
return self.BitField3Offset() + self.heap.TaggedPointerSize()
def ConstructorOrBackPointerOffset(self):
return self.PrototypeOffset() + self.heap.TaggedPointerSize()
def TransitionsOrPrototypeInfoOffset(self):
return self.ConstructorOrBackPointerOffset() + self.heap.TaggedPointerSize()
def DescriptorsOffset(self):
return (self.TransitionsOrPrototypeInfoOffset() +
self.heap.TaggedPointerSize())
def CodeCacheOffset(self):
return self.DescriptorsOffset() + self.heap.TaggedPointerSize()
def DependentCodeOffset(self):
return self.CodeCacheOffset() + self.heap.TaggedPointerSize()
def ReadByte(self, offset):
return self.heap.reader.ReadU8(self.address + offset)
def ReadSlot(self, offset):
return self.heap.reader.ReadTagged(self.address + offset)
def Print(self, p):
p.Print("Map(%08x)" % (self.address))
p.Print(" - size: %d, inobject: %d, (unused: %d), visitor: %d" % (
self.ReadByte(self.InstanceSizeOffset()),
self.ReadByte(self.InObjectProperties()),
self.ReadByte(self.UnusedByte()),
self.VisitorId()))
instance_type = INSTANCE_TYPES[self.ReadByte(self.InstanceTypeOffset())]
bitfield = self.ReadByte(self.BitFieldOffset())
bitfield2 = self.ReadByte(self.BitField2Offset())
unused = self.ReadByte(self.UnusedPropertyFieldsOffset())
p.Print(" - %s, bf: %d, bf2: %d, unused: %d" % (
instance_type, bitfield, bitfield2, unused))
p.Print(" - kind: %s" % (self.Decode(3, 5, bitfield2)))
bitfield3 = self.ReadSlot(self.BitField3Offset())
p.Print(
" - EnumLength: %d NumberOfOwnDescriptors: %d OwnsDescriptors: %s" % (
self.Decode(0, 10, bitfield3),
self.Decode(10, 10, bitfield3),
self.Decode(21, 1, bitfield3)))
p.Print(" - DictionaryMap: %s" % (self.Decode(20, 1, bitfield3)))
p.Print(" - Deprecated: %s" % (self.Decode(23, 1, bitfield3)))
p.Print(" - IsUnstable: %s" % (self.Decode(24, 1, bitfield3)))
p.Print(" - NewTargetIsBase: %s" % (self.Decode(27, 1, bitfield3)))
descriptors = self.ObjectField(self.DescriptorsOffset())
if descriptors.__class__ == FixedArray:
DescriptorArray(descriptors).Print(p)
else:
p.Print(" - Descriptors: %s" % (descriptors))
transitions = self.ObjectField(self.TransitionsOrPrototypeInfoOffset())
if transitions.__class__ == FixedArray:
TransitionArray(transitions).Print(p)
else:
p.Print(" - TransitionsOrPrototypeInfo: %s" % (transitions))
p.Print(" - Prototype: %s" % self.ObjectField(self.PrototypeOffset()))
def __init__(self, heap, map, address):
HeapObject.__init__(self, heap, map, address)
self.instance_type = \
heap.reader.ReadU8(self.address + self.InstanceTypeOffset())
class String(HeapObject):
def LengthOffset(self):
# First word after the map is the hash, the second is the length.
return self.heap.TaggedPointerSize() * 2
def __init__(self, heap, map, address):
HeapObject.__init__(self, heap, map, address)
self.length = self.Uint32Field(self.LengthOffset())
def GetChars(self):
return "?string?"
def Print(self, p):
p.Print(str(self))
def __str__(self):
return "\"%s\"" % self.GetChars()
class SeqString(String):
def CharsOffset(self):
return self.heap.TaggedPointerSize() * 3
def __init__(self, heap, map, address):
String.__init__(self, heap, map, address)
self.chars = heap.reader.ReadBytes(self.address + self.CharsOffset(),
self.length).decode('utf8')
def GetChars(self):
return self.chars
class ExternalString(String):
# TODO(vegorov) fix ExternalString for X64 architecture
RESOURCE_OFFSET = 12
WEBKIT_RESOUCE_STRING_IMPL_OFFSET = 4
WEBKIT_STRING_IMPL_CHARS_OFFSET = 8
def __init__(self, heap, map, address):
String.__init__(self, heap, map, address)
reader = heap.reader
self.resource = \
reader.ReadU32(self.address + ExternalString.RESOURCE_OFFSET)
self.chars = "?external string?"
if not reader.IsValidAddress(self.resource): return
string_impl_address = self.resource + \
ExternalString.WEBKIT_RESOUCE_STRING_IMPL_OFFSET
if not reader.IsValidAddress(string_impl_address): return
string_impl = reader.ReadU32(string_impl_address)
chars_ptr_address = string_impl + \
ExternalString.WEBKIT_STRING_IMPL_CHARS_OFFSET
if not reader.IsValidAddress(chars_ptr_address): return
chars_ptr = reader.ReadU32(chars_ptr_address)
if not reader.IsValidAddress(chars_ptr): return
raw_chars = reader.ReadBytes(chars_ptr, 2 * self.length)
self.chars = codecs.getdecoder("utf16")(raw_chars)[0]
def GetChars(self):
return self.chars
class ConsString(String):
def LeftOffset(self):
return self.heap.TaggedPointerSize() * 3
def RightOffset(self):
return self.heap.TaggedPointerSize() * 4
def __init__(self, heap, map, address):
String.__init__(self, heap, map, address)
self.left = self.ObjectField(self.LeftOffset())
self.right = self.ObjectField(self.RightOffset())
def GetChars(self):
try:
left = self.left.GetChars() if self.left is not None else "<lhs>"
right = self.right.GetChars() if self.right is not None else "<rhs>"
return left + right
except Exception as e:
return "***CAUGHT EXCEPTION IN GROKDUMP***: " + str(e)
class Oddball(HeapObject):
#Should match declarations in objects.h
KINDS = [
"False",
"True",
"TheHole",
"Null",
"ArgumentMarker",
"Undefined",
"Other"
]
def ToStringOffset(self):
return self.heap.TaggedPointerSize()
def ToNumberOffset(self):
return self.ToStringOffset() + self.heap.TaggedPointerSize()
def KindOffset(self):
return self.ToNumberOffset() + self.heap.TaggedPointerSize()
def __init__(self, heap, map, address):
HeapObject.__init__(self, heap, map, address)
self.to_string = self.ObjectField(self.ToStringOffset())
self.kind = self.SmiField(self.KindOffset())
def Print(self, p):
p.Print(str(self))
def __str__(self):
if self.to_string:
return "Oddball(%08x, <%s>)" % (self.address, str(self.to_string))
else:
kind = "???"
if 0 <= self.kind < len(Oddball.KINDS):
kind = Oddball.KINDS[self.kind]
return "Oddball(%08x, kind=%s)" % (self.address, kind)
class FixedArray(HeapObject):
def LengthOffset(self):
return self.heap.TaggedPointerSize()
def ElementsOffset(self):
return self.heap.TaggedPointerSize() * 2
def MemberOffset(self, i):
return self.ElementsOffset() + self.heap.TaggedPointerSize() * i
def Get(self, i):
return self.ObjectField(self.MemberOffset(i))
def __init__(self, heap, map, address):
HeapObject.__init__(self, heap, map, address)
self.length = self.SmiField(self.LengthOffset())
def Print(self, p):
p.Print("FixedArray(%s) {" % self.heap.reader.FormatIntPtr(self.address))
p.Indent()
p.Print("length: %d" % self.length)
base_offset = self.ElementsOffset()
for i in range(self.length):
offset = base_offset + 4 * i
try:
p.Print("[%08d] = %s" % (i, self.ObjectField(offset)))
except TypeError:
p.Dedent()
p.Print("...")
p.Print("}")
return
p.Dedent()
p.Print("}")
def __str__(self):
return "FixedArray(%08x, length=%d)" % (self.address, self.length)
class DescriptorArray(object):
def __init__(self, array):
self.array = array
def Length(self):
return self.array.Get(0)
def Decode(self, offset, size, value):
return (value >> offset) & ((1 << size) - 1)
TYPES = [
"normal",
"field",
"function",
"callbacks"
]
def Type(self, value):
return DescriptorArray.TYPES[self.Decode(0, 3, value)]
def Attributes(self, value):
attributes = self.Decode(3, 3, value)
result = []
if (attributes & 0): result += ["ReadOnly"]
if (attributes & 1): result += ["DontEnum"]
if (attributes & 2): result += ["DontDelete"]
return "[" + (",".join(result)) + "]"
def Deleted(self, value):
return self.Decode(6, 1, value) == 1
def FieldIndex(self, value):
return self.Decode(20, 11, value)
def Pointer(self, value):
return self.Decode(6, 11, value)
def Details(self, di, value):
return (
di,
self.Type(value),
self.Attributes(value),
self.FieldIndex(value),
self.Pointer(value)
)
def Print(self, p):
length = self.Length()
array = self.array
p.Print("Descriptors(%08x, length=%d)" % (array.address, length))
p.Print("[et] %s" % (array.Get(1)))
for di in range(length):
i = 2 + di * 3
p.Print("0x%x" % (array.address + array.MemberOffset(i)))
p.Print("[%i] name: %s" % (di, array.Get(i + 0)))
p.Print("[%i] details: %s %s field-index %i pointer %i" % \
self.Details(di, array.Get(i + 1)))
p.Print("[%i] value: %s" % (di, array.Get(i + 2)))
end = self.array.length // 3
if length != end:
p.Print("[%i-%i] slack descriptors" % (length, end))
class TransitionArray(object):
def __init__(self, array):
self.array = array
def IsSimpleTransition(self):
return self.array.length <= 2
def Length(self):
# SimpleTransition cases
if self.IsSimpleTransition():
return self.array.length - 1
return (self.array.length - 3) // 2
def Print(self, p):
length = self.Length()
array = self.array
p.Print("Transitions(%08x, length=%d)" % (array.address, length))
p.Print("[backpointer] %s" % (array.Get(0)))
if self.IsSimpleTransition():
if length == 1:
p.Print("[simple target] %s" % (array.Get(1)))
return
elements = array.Get(1)
if elements is not None:
p.Print("[elements ] %s" % (elements))
prototype = array.Get(2)
if prototype is not None:
p.Print("[prototype ] %s" % (prototype))
for di in range(length):
i = 3 + di * 2
p.Print("[%i] symbol: %s" % (di, array.Get(i + 0)))
p.Print("[%i] target: %s" % (di, array.Get(i + 1)))
class JSFunction(HeapObject):
def CodeEntryOffset(self):
return 3 * self.heap.TaggedPointerSize()
def SharedOffset(self):
return 5 * self.heap.TaggedPointerSize()
def __init__(self, heap, map, address):
HeapObject.__init__(self, heap, map, address)
code_entry = \
heap.reader.ReadU32(self.address + self.CodeEntryOffset())
self.code = heap.FindObject(code_entry - Code.HeaderSize(heap) + 1)
self.shared = self.ObjectField(self.SharedOffset())
def Print(self, p):
source = "\n".join(" %s" % line for line in self._GetSource().split("\n"))
p.Print("JSFunction(%s) {" % self.heap.reader.FormatIntPtr(self.address))
p.Indent()
p.Print("inferred name: %s" % self.shared.inferred_name)
if self.shared.script.Is(Script) and self.shared.script.name.Is(String):
p.Print("script name: %s" % self.shared.script.name)
p.Print("source:")
p.Print(source)
p.Print("code:")
self.code.Print(p)
if self.code != self.shared.code:
p.Print("unoptimized code:")
self.shared.code.Print(p)
p.Dedent()
p.Print("}")
def __str__(self):
inferred_name = ""
if self.shared is not None and self.shared.Is(SharedFunctionInfo):
inferred_name = self.shared.inferred_name
return "JSFunction(%s, %s) " % \
(self.heap.reader.FormatIntPtr(self.address), inferred_name)
def _GetSource(self):
source = "?source?"
start = self.shared.start_position
end = self.shared.end_position
if not self.shared.script.Is(Script): return source
script_source = self.shared.script.source
if not script_source.Is(String): return source
if start and end:
source = script_source.GetChars()[start:end]
return source
class SharedFunctionInfo(HeapObject):
def CodeOffset(self):
return 2 * self.heap.TaggedPointerSize()
def ScriptOffset(self):
return 7 * self.heap.TaggedPointerSize()
def InferredNameOffset(self):
return 9 * self.heap.TaggedPointerSize()
def EndPositionOffset(self):
return 12 * self.heap.TaggedPointerSize() + 4 * self.heap.IntSize()
def StartPositionAndTypeOffset(self):
return 12 * self.heap.TaggedPointerSize() + 5 * self.heap.IntSize()
def __init__(self, heap, map, address):
HeapObject.__init__(self, heap, map, address)
try:
self.code = self.ObjectField(self.CodeOffset())
self.script = self.ObjectField(self.ScriptOffset())
self.inferred_name = self.ObjectField(self.InferredNameOffset())
if heap.TaggedPointerSize() == 8:
start_position_and_type = \
heap.reader.ReadU32(self.StartPositionAndTypeOffset())
self.start_position = start_position_and_type >> 2
pseudo_smi_end_position = \
heap.reader.ReadU32(self.EndPositionOffset())
self.end_position = pseudo_smi_end_position >> 2
else:
start_position_and_type = \
self.SmiField(self.StartPositionAndTypeOffset())
if start_position_and_type:
self.start_position = start_position_and_type >> 2
else:
self.start_position = None
self.end_position = \
self.SmiField(self.EndPositionOffset())
except:
print("*** Error while reading SharedFunctionInfo")
class Script(HeapObject):
def SourceOffset(self):
return self.heap.TaggedPointerSize()
def NameOffset(self):
return self.SourceOffset() + self.heap.TaggedPointerSize()
def __init__(self, heap, map, address):
HeapObject.__init__(self, heap, map, address)
self.source = self.ObjectField(self.SourceOffset())
self.name = self.ObjectField(self.NameOffset())
class CodeCache(HeapObject):
def DefaultCacheOffset(self):
return self.heap.TaggedPointerSize()
def NormalTypeCacheOffset(self):
return self.DefaultCacheOffset() + self.heap.TaggedPointerSize()
def __init__(self, heap, map, address):
HeapObject.__init__(self, heap, map, address)
self.default_cache = self.ObjectField(self.DefaultCacheOffset())
self.normal_type_cache = self.ObjectField(self.NormalTypeCacheOffset())
def Print(self, p):
p.Print("CodeCache(%s) {" % self.heap.reader.FormatIntPtr(self.address))
p.Indent()
p.Print("default cache: %s" % self.default_cache)
p.Print("normal type cache: %s" % self.normal_type_cache)
p.Dedent()
p.Print("}")
class Code(HeapObject):
CODE_ALIGNMENT_MASK = (1 << 5) - 1
def InstructionSizeOffset(self):
return self.heap.TaggedPointerSize()
@staticmethod
def HeaderSize(heap):
return (heap.TaggedPointerSize() + heap.IntSize() + \
4 * heap.TaggedPointerSize() + 3 * heap.IntSize() + \
Code.CODE_ALIGNMENT_MASK) & ~Code.CODE_ALIGNMENT_MASK
def __init__(self, heap, map, address):
HeapObject.__init__(self, heap, map, address)
self.entry = self.address + Code.HeaderSize(heap)
self.instruction_size = \
heap.reader.ReadU32(self.address + self.InstructionSizeOffset())
def Print(self, p):
lines = self.heap.reader.GetDisasmLines(self.entry, self.instruction_size)
p.Print("Code(%s) {" % self.heap.reader.FormatIntPtr(self.address))
p.Indent()
p.Print("instruction_size: %d" % self.instruction_size)
p.PrintLines(self._FormatLine(line) for line in lines)
p.Dedent()
p.Print("}")
def _FormatLine(self, line):
return FormatDisasmLine(self.entry, self.heap, line)
class V8Heap(object):
CLASS_MAP = {
"SYMBOL_TYPE": SeqString,
"SEQ_TWO_BYTE_STRING_TYPE": SeqString,
"SEQ_ONE_BYTE_STRING_TYPE": SeqString,
"INTERNALIZED_TWO_BYTE_STRING_TYPE": SeqString,
"INTERNALIZED_ONE_BYTE_STRING_TYPE": SeqString,
"CONS_TWO_BYTE_STRING_TYPE": ConsString,
"CONS_ONE_BYTE_STRING_TYPE": ConsString,
"EXTERNAL_TWO_BYTE_STRING_TYPE": ExternalString,
"EXTERNAL_ONE_BYTE_STRING_TYPE": ExternalString,
"EXTERNAL_INTERNALIZED_TWO_BYTE_STRING_TYPE": ExternalString,
"EXTERNAL_INTERNALIZED_ONE_BYTE_STRING_TYPE": ExternalString,
"MAP_TYPE": Map,
"ODDBALL_TYPE": Oddball,
"FIXED_ARRAY_TYPE": FixedArray,
"HASH_TABLE_TYPE": FixedArray,
"OBJECT_BOILERPLATE_DESCRIPTION_TYPE": FixedArray,
"SCOPE_INFO_TYPE": FixedArray,
"JS_FUNCTION_TYPE": JSFunction,
"SHARED_FUNCTION_INFO_TYPE": SharedFunctionInfo,
"SCRIPT_TYPE": Script,
"CODE_CACHE_TYPE": CodeCache,
"CODE_TYPE": Code,
}
def __init__(self, reader, stack_map):
self.reader = reader
self.stack_map = stack_map
self.objects = {}
def FindObjectOrSmi(self, tagged_address):
if self.IsSmi(tagged_address): return self.SmiUntag(tagged_address)
return self.FindObject(tagged_address)
def FindObject(self, tagged_address):
if tagged_address in self.objects:
return self.objects[tagged_address]
if not self.IsTaggedObjectAddress(tagged_address): return None
address = tagged_address - 1
if not self.reader.IsValidAddress(address): return None
map_tagged_address = self.reader.ReadTagged(address)
if tagged_address == map_tagged_address:
# Meta map?
meta_map = Map(self, None, address)
instance_type_name = INSTANCE_TYPES.get(meta_map.instance_type)
if instance_type_name != "MAP_TYPE": return None
meta_map.map = meta_map
object = meta_map
else:
map = self.FindMap(map_tagged_address)
if map is None: return None
instance_type_name = INSTANCE_TYPES.get(map.instance_type)
if instance_type_name is None: return None
cls = V8Heap.CLASS_MAP.get(instance_type_name, HeapObject)
object = cls(self, map, address)
self.objects[tagged_address] = object
return object
def FindMap(self, tagged_address):
address = self.FindMapAddress(tagged_address)
if not address: return None
object = Map(self, None, address)
return object
def FindMapAddress(self, tagged_address):
if not self.IsTaggedMapAddress(tagged_address): return None
address = tagged_address - 1
if not self.reader.IsValidAddress(address): return None
return address
def IntSize(self):
return 4
def MachinePointerSize(self):
return self.reader.MachinePointerSize()
def TaggedPointerSize(self):
return self.reader.TaggedPointerSize()
def IsPointerCompressed(self):
return self.reader.IsPointerCompressed()
def ObjectAlignmentMask(self):
return self.TaggedPointerSize() - 1
def IsTaggedObjectAddress(self, address):
return (address & self.ObjectAlignmentMask()) == 1
def IsWeakTaggedObjectAddress(self, address):
return (address & self.ObjectAlignmentMask()) == 3
def IsValidTaggedObjectAddress(self, address):
if not self.IsTaggedObjectAddress(address): return False
return self.reader.IsValidAddress(address)
def IsTaggedMapAddress(self, address):
return (address & self.MapAlignmentMask()) == 1
def MapAlignmentMask(self):
if self.reader.arch == MD_CPU_ARCHITECTURE_AMD64:
return (1 << 4) - 1
elif self.reader.arch == MD_CPU_ARCHITECTURE_ARM:
return (1 << 4) - 1
elif self.reader.arch == MD_CPU_ARCHITECTURE_ARM64:
return (1 << 4) - 1
elif self.reader.arch == MD_CPU_ARCHITECTURE_X86:
return (1 << 5) - 1
def PageAlignmentMask(self):
return (1 << 19) - 1
def IsTaggedAddress(self, address):
return (address & self.ObjectAlignmentMask()) == 1
def IsWeakTaggedAddress(self, address):
return (address & self.ObjectAlignmentMask()) == 3
def IsMaybeWeakTaggedAddress(self, address):
return (address & 1) == 1
def IsSmi(self, tagged_address):
if self.reader.Is64() and not self.reader.IsPointerCompressed():
return (tagged_address & 0xFFFFFFFF) == 0
return (tagged_address & 1) == 0
def SmiUntag(self, tagged_address):
if self.reader.Is64() and not self.reader.IsPointerCompressed():
return tagged_address >> 32
return (tagged_address >> 1) & 0xFFFFFFFF
def AddressTypeMarker(self, address):
if not self.reader.IsValidAddress(address): return " "
if self.reader.IsExceptionStackAddress(address): return "S"
if self.reader.IsModuleAddress(address): return "C"
if self.IsTaggedAddress(address):
# Cannot have an tagged pointer into the stack
if self.reader.IsAnyExceptionStackAddress(address): return "s"
return "T"
return "*"
def FormatIntPtr(self, address):
marker = self.AddressTypeMarker(address)
address = self.reader.FormatIntPtr(address)
if marker == " ": return address
return "%s %s" % (address, marker)
def RelativeOffset(self, slot, address):
if not self.reader.IsValidAlignedAddress(slot): return None
if self.IsTaggedObjectAddress(address):
address -= 1
if not self.reader.IsValidAlignedAddress(address): return None
offset = (address - slot) / self.MachinePointerSize()
lower_limit = -32
upper_limit = 128
if self.reader.IsExceptionStackAddress(address):
upper_limit = 0xFFFFFF
if offset < lower_limit or upper_limit < offset: return None
target_address = self.reader.ReadUIntPtr(address)
return "[%+02d]=%s %s" % (offset, self.reader.FormatIntPtr(target_address),
self.AddressTypeMarker(target_address))
def FindObjectPointers(self, start=0, end=0):
objects = set()
def find_object_in_region(reader, start, size, location):
for slot in range(start, start + size, self.reader.TaggedPointerSize()):
if not self.reader.IsValidAddress(slot): break
# Collect only tagged pointers (object) to tagged pointers (map)
tagged_address = self.reader.ReadTagged(slot)
if not self.IsValidTaggedObjectAddress(tagged_address): continue
map_address = self.reader.ReadTagged(tagged_address - 1)
if not self.IsTaggedMapAddress(map_address): continue
objects.add(tagged_address)
if not start and not end:
self.reader.ForEachMemoryRegion(find_object_in_region)
else:
find_object_in_region(self.reader, start, end-start, None)
return objects
for instance_type in V8Heap.CLASS_MAP.keys():
if instance_type not in set(INSTANCE_TYPES.values()):
print(
f"Warning: No instance type {instance_type} in"
"v8heapconst.INSTANCE_TYPES, you may need to regenerate v8heapconst.py")
class UnknownObject(HeapObject):
def __init__(self, heap, address):
HeapObject.__init__(self, heap, None, None)
self.address = address
def GetChars(self):
return "<???>"
def __str__(self):
return "<0x%x>" % self.address
class KnownObject(HeapObject):
def __init__(self, heap, known_name):
HeapObject.__init__(self, heap, None, None)
self.known_name = known_name
def __str__(self):
return "<%s>" % self.known_name
class KnownMap(HeapObject):
def __init__(self, heap, known_name, instance_type):
HeapObject.__init__(self, heap, None, None)
self.instance_type = instance_type
self.known_name = known_name
def __str__(self):
return "<%s>" % self.known_name
COMMENT_RE = re.compile(r"^C (0x[0-9a-fA-F]+) (.*)$")
PAGEADDRESS_RE = re.compile(
r"^P (mappage|oldpage) (0x[0-9a-fA-F]+)$")
class InspectionInfo(object):
def __init__(self, minidump_name, reader):
self.comment_file = minidump_name + ".comments"
self.address_comments = {}
self.page_address = {}
if os.path.exists(self.comment_file):
with open(self.comment_file, "r") as f:
lines = f.readlines()
f.close()
for l in lines:
m = COMMENT_RE.match(l)
if m:
self.address_comments[int(m.group(1), 0)] = m.group(2)
m = PAGEADDRESS_RE.match(l)
if m:
self.page_address[m.group(1)] = int(m.group(2), 0)
self.reader = reader
self.styles = {}
self.color_addresses()
return
def get_page_address(self, page_kind):
return self.page_address.get(page_kind, 0)
def save_page_address(self, page_kind, address):
with open(self.comment_file, "a") as f:
f.write("P %s 0x%x\n" % (page_kind, address))
f.close()
def color_addresses(self):
# Color all stack addresses.
exception_thread = self.reader.thread_map[self.reader.exception.thread_id]
stack_top = self.reader.ExceptionSP()
stack_bottom = exception_thread.stack.start + \
exception_thread.stack.memory.data_size
frame_pointer = self.reader.ExceptionFP()
self.styles[frame_pointer] = "frame"
for slot in range(stack_top, stack_bottom,
self.reader.MachinePointerSize()):
# stack address
self.styles[slot] = "sa"
for slot in range(stack_top, stack_bottom,
self.reader.MachinePointerSize()):
maybe_address = self.reader.ReadUIntPtr(slot)
# stack value
self.styles[maybe_address] = "sv"
if slot == frame_pointer:
self.styles[slot] = "frame"
frame_pointer = maybe_address
self.styles[self.reader.ExceptionIP()] = "pc"
def get_style_class(self, address):
return self.styles.get(address, None)
def get_style_class_string(self, address):
style = self.get_style_class(address)
if style is not None:
return " class=%s " % style
else:
return ""
def set_comment(self, address, comment):
self.address_comments[address] = comment
with open(self.comment_file, "a") as f:
f.write("C 0x%x %s\n" % (address, comment))
f.close()
def get_comment(self, address):
return self.address_comments.get(address, "")
class InspectionPadawan(object):
"""The padawan can improve annotations by sensing well-known objects."""
def __init__(self, reader, heap):
self.reader = reader
self.heap = heap
self.known_first_map_page = 0
self.known_first_old_page = 0
self.context = None
def __getattr__(self, name):
"""An InspectionPadawan can be used instead of V8Heap, even though
it does not inherit from V8Heap (aka. mixin)."""
return getattr(self.heap, name)
def GetPageAddress(self, tagged_address):
return tagged_address & ~self.heap.PageAlignmentMask()
def GetPageOffset(self, tagged_address):
return tagged_address & self.heap.PageAlignmentMask()
def IsInKnownMapSpace(self, tagged_address):
page_address = tagged_address & ~self.heap.PageAlignmentMask()
return page_address == self.known_first_map_page
def IsInKnownOldSpace(self, tagged_address):
page_address = tagged_address & ~self.heap.PageAlignmentMask()
return page_address == self.known_first_old_page
def ContainingKnownOldSpaceName(self, tagged_address):
page_address = tagged_address & ~self.heap.PageAlignmentMask()
if page_address == self.known_first_old_page: return "OLD_SPACE"
return None
def FrameMarkerName(self, value):
# The frame marker is Smi-tagged but not Smi encoded and 0 is not a valid
# frame type.
value = (value >> 1) - 1
if 0 <= value < len(FRAME_MARKERS):
return "Possibly %s frame marker" % FRAME_MARKERS[value]
return None
def IsFrameMarker(self, slot, address):
if not slot: return False
# Frame markers only occur directly after a frame pointer and only on the
# stack.
if not self.reader.IsExceptionStackAddress(slot): return False
next_slot = slot + self.reader.MachinePointerSize()
if not self.reader.IsValidAddress(next_slot): return False
next_address = self.reader.ReadUIntPtr(next_slot)
return self.reader.IsExceptionStackAddress(next_address)
def FormatSmi(self, address):
value = self.heap.SmiUntag(address)
# On 32-bit systems almost everything looks like a Smi.
if not self.reader.Is64() or value == 0: return None
return "Tagged<Smi>(%d)" % value
def SenseObject(self, address, slot=None):
if self.IsFrameMarker(slot, address):
return self.FrameMarkerName(address)
if self.heap.IsSmi(address):
return self.FormatSmi(address)
if not self.heap.IsMaybeWeakTaggedAddress(address):
return None
tagged_address = address
is_weak = self.heap.IsWeakTaggedAddress(address)
if is_weak:
tagged_address &= ~2
if self.heap.IsPointerCompressed():
# Interpret the first page as the read-only space in pointer-compression.
page = self.GetPageAddress(tagged_address)
tagged_page = page & 0xFFFFFFFF
if tagged_page == 0:
offset = self.GetPageOffset(tagged_address)
if offset == 1 and is_weak:
return "<cleared>"
lookup_key = ("read_only_space", offset)
known_obj_name = KNOWN_OBJECTS.get(lookup_key)
if known_obj_name:
return KnownObject(self, known_obj_name)
known_map_info = KNOWN_MAPS.get(lookup_key)
if known_map_info:
known_map_type, known_map_name = known_map_info
return KnownObject(self, known_map_name)
if self.IsInKnownOldSpace(tagged_address):
offset = self.GetPageOffset(tagged_address)
lookup_key = (self.ContainingKnownOldSpaceName(tagged_address), offset)
known_obj_name = KNOWN_OBJECTS.get(lookup_key)
if known_obj_name:
return KnownObject(self, known_obj_name)
known_map_info = KNOWN_MAPS.get(lookup_key)
if known_map_info:
known_map_type, known_map_name = known_map_info
return KnownObject(self, known_map_name)
if self.IsInKnownMapSpace(tagged_address):
known_map = self.SenseMap(tagged_address)
if known_map:
return known_map
found_obj = self.heap.FindObject(tagged_address)
if found_obj:
return found_obj
address = tagged_address - 1
if self.reader.IsValidAddress(address):
map_tagged_address = self.reader.ReadTagged(address)
map = self.SenseMap(map_tagged_address)
if map is None:
return None
instance_type_name = INSTANCE_TYPES.get(map.instance_type)
if instance_type_name is None:
return None
cls = V8Heap.CLASS_MAP.get(instance_type_name, HeapObject)
return cls(self, map, address)
return None
def SenseMap(self, tagged_address):
if self.heap.IsPointerCompressed():
# Interpret the first page as the read-only space in pointer-compression.
page = self.GetPageAddress(tagged_address)
tagged_page = page & 0xFFFFFFFF
if tagged_page == 0:
offset = self.GetPageOffset(tagged_address)
lookup_key = ("read_only_space", offset)
known_map_info = KNOWN_MAPS.get(lookup_key)
if known_map_info:
known_map_type, known_map_name = known_map_info
return KnownMap(self, known_map_name, known_map_type)
if self.IsInKnownMapSpace(tagged_address):
offset = self.GetPageOffset(tagged_address)
lookup_key = ("old_space", offset)
known_map_info = KNOWN_MAPS.get(lookup_key)
if known_map_info:
known_map_type, known_map_name = known_map_info
return KnownMap(self, known_map_name, known_map_type)
found_map = self.heap.FindMap(tagged_address)
if found_map:
return found_map
return None
def FindObjectOrSmi(self, tagged_address):
"""When used as a mixin in place of V8Heap."""
found_obj = self.SenseObject(tagged_address)
if found_obj:
return found_obj
if self.IsSmi(tagged_address):
return self.FormatSmi(tagged_address)
else:
return UnknownObject(self, tagged_address)
def FindObject(self, tagged_address):
"""When used as a mixin in place of V8Heap."""
raise NotImplementedError
def FindMap(self, tagged_address):
"""When used as a mixin in place of V8Heap."""
raise NotImplementedError
def PrintKnowledge(self):
print(" known_first_map_page = %s\n"\
" known_first_old_page = %s" % (
self.reader.FormatIntPtr(self.known_first_map_page),
self.reader.FormatIntPtr(self.known_first_old_page)))
def FindFirstAsciiString(self, start, end=None, min_length=32):
""" Walk the memory until we find a large string """
if not end:
end = start + 64
for slot in range(start, end):
if not self.reader.IsValidAddress(slot):
break
message = self.reader.ReadAsciiString(slot)
if len(message) > min_length:
return (slot, message)
return (None,None)
def PrintStackTraceMessage(self, start=None, print_message=True):
"""
Try to print a possible message from PushStackTraceAndDie.
Returns the first address where the normal stack starts again.
"""
# Only look at the first 1k words on the stack
ptr_size = self.reader.MachinePointerSize()
if start is None:
start = self.reader.ExceptionSP()
if not self.reader.IsValidAddress(start):
return start
end = start + ptr_size * 1024 * 4
magic1 = None
for slot in range(start, end, ptr_size):
if not self.reader.IsValidAddress(slot + ptr_size):
break
magic1 = self.reader.ReadUIntPtr(slot)
magic2 = self.reader.ReadUIntPtr(slot + ptr_size)
pair = (magic1 & 0xFFFFFFFF, magic2 & 0xFFFFFFFF)
if pair in MAGIC_MARKER_PAIRS:
return self.TryExtractOldStyleStackTrace(slot, start, end,
print_message)
if pair[0] == STACK_TRACE_MARKER:
return self.TryExtractStackTrace(slot, start, end, print_message)
elif pair[0] == ERROR_MESSAGE_MARKER:
return self.TryExtractErrorMessage(slot, start, end, print_message)
# Simple fallback in case not stack trace object was found
return self.TryExtractOldStyleStackTrace(0, start, end, print_message)
def TryExtractStackTrace(self, slot, start, end, print_message):
ptr_size = self.reader.MachinePointerSize()
assert self.reader.ReadUIntPtr(slot) & 0xFFFFFFFF == STACK_TRACE_MARKER
# Look for the mid marker after the start slot.
mid_slot = self.reader.FindPtr(STACK_TRACE_MID_MARKER, start + ptr_size,
start + ptr_size * 100)
if not mid_slot:
return self.TryExtractOldStackTrace(slot, start, end, print_message)
end_search = mid_slot + (32 * 1024) + (4 * ptr_size)
end_slot = self.reader.FindPtr(STACK_TRACE_END_MARKER, end_search,
end_search + ptr_size * 512)
if not end_slot:
return start
print("Stack Message (start=%s):" % self.heap.FormatIntPtr(slot))
value = self.reader.ReadUIntPtr(slot)
print(" %s: %s" % ("isolate".rjust(14), self.heap.FormatIntPtr(value)))
slot += ptr_size
i = 0
while slot < mid_slot:
value = self.reader.ReadUIntPtr(slot)
print(" %s: %s" % ("ptr%d" % i, self.heap.FormatIntPtr(value)))
slot += ptr_size
i += 1
slot = mid_slot + ptr_size
for i in range(4):
value = self.reader.ReadUIntPtr(slot)
print(" %s: %s" % ("codeObject%d" % i, self.heap.FormatIntPtr(value)))
slot += ptr_size
print(" message start: %s" % self.heap.FormatIntPtr(slot))
stack_start = end_slot + ptr_size
print(" stack_start: %s" % self.heap.FormatIntPtr(stack_start))
(message_start, message) = self.FindFirstAsciiString(slot)
self.FormatStackTrace(message, print_message)
return stack_start
def TryExtractOldStackTrace(self, slot, start, end, print_message):
ptr_size = self.reader.MachinePointerSize()
assert self.reader.ReadUIntPtr(slot) & 0xFFFFFFFF == STACK_TRACE_MARKER
header_size = 10
# Look for the end marker after the fields and the message buffer.
end_search = start + (32 * 1024) + (header_size * ptr_size)
end_slot = self.reader.FindPtr(STACK_TRACE_OLD_END_MARKER, end_search,
end_search + ptr_size * 512)
if not end_slot:
return start
print("Stack Message (start=%s):" % self.heap.FormatIntPtr(slot))
for name in ("isolate", "ptr1", "ptr2", "ptr3", "ptr4", "ptr5", "ptr6",
"codeObject1", "codeObject2", "codeObject3", "codeObject4"):
value = self.reader.ReadUIntPtr(slot)
print(" %s: %s" % (name.rjust(14), self.heap.FormatIntPtr(value)))
slot += ptr_size
print(" message start: %s" % self.heap.FormatIntPtr(slot))
stack_start = end_slot + ptr_size
print(" stack_start: %s" % self.heap.FormatIntPtr(stack_start))
(message_start, message) = self.FindFirstAsciiString(slot)
self.FormatStackTrace(message, print_message)
return stack_start
def TryExtractErrorMessage(self, slot, start, end, print_message):
ptr_size = self.reader.MachinePointerSize()
end_marker = ERROR_MESSAGE_MARKER + 1
header_size = 1
end_search = start + 1024 + (header_size * ptr_size)
end_slot = self.reader.FindPtr(end_marker, end_search,
end_search + ptr_size * 512)
if not end_slot:
return start
print("Error Message (start=%s):" % self.heap.FormatIntPtr(slot))
slot += ptr_size
(message_start, message) = self.FindFirstAsciiString(slot)
self.FormatStackTrace(message, print_message)
stack_start = end_slot + ptr_size
return stack_start
def TryExtractOldStyleStackTrace(self, message_slot, start, end,
print_message):
ptr_size = self.reader.MachinePointerSize()
if message_slot == 0:
"""
On Mac we don't always get proper magic markers, so just try printing
the first long ascii string found on the stack.
"""
magic1 = None
magic2 = None
message_start, message = self.FindFirstAsciiString(start, end, 128)
if message_start is None:
return start
else:
message_start = self.reader.ReadUIntPtr(message_slot + ptr_size * 4)
message = self.reader.ReadAsciiString(message_start)
stack_start = message_start + len(message) + 1
# Make sure the address is word aligned
stack_start = stack_start - (stack_start % ptr_size)
if magic1 is None:
print("Stack Message:")
print(" message start: %s" % self.heap.FormatIntPtr(message_start))
print(" stack_start: %s" % self.heap.FormatIntPtr(stack_start ))
else:
ptr1 = self.reader.ReadUIntPtr(slot + ptr_size * 2)
ptr2 = self.reader.ReadUIntPtr(slot + ptr_size * 3)
print("Stack Message:")
print(" magic1: %s" % self.heap.FormatIntPtr(magic1))
print(" magic2: %s" % self.heap.FormatIntPtr(magic2))
print(" ptr1: %s" % self.heap.FormatIntPtr(ptr1))
print(" ptr2: %s" % self.heap.FormatIntPtr(ptr2))
print(" message start: %s" % self.heap.FormatIntPtr(message_start))
print(" stack_start: %s" % self.heap.FormatIntPtr(stack_start ))
print("")
self.FormatStackTrace(message, print_message)
return stack_start
def FormatStackTrace(self, message, print_message):
if not print_message:
print(" Use `dsa` to print the message with annotated addresses.")
print("")
return
ptr_size = self.reader.MachinePointerSize()
# Annotate all addresses in the dumped message
prog = re.compile("[0-9a-fA-F]{%s}" % ptr_size*2)
addresses = list(set(prog.findall(message)))
for i in range(len(addresses)):
address_org = addresses[i]
address = self.heap.FormatIntPtr(int(address_org, 16))
if address_org != address:
message = message.replace(address_org, address)
print("Message:")
print("="*80)
print(message)
print("="*80)
print("")
def TryInferFramePointer(self, slot, address):
""" Assume we have a framepointer if we find 4 consecutive links """
for i in range(0, 4):
if not self.reader.IsExceptionStackAddress(address):
return 0
next_address = self.reader.ReadUIntPtr(address)
if next_address == address:
return 0
address = next_address
return slot
def TryInferContext(self, address):
if self.context:
return
ptr_size = self.reader.MachinePointerSize()
possible_context = dict()
count = 0
while self.reader.IsExceptionStackAddress(address):
prev_addr = self.reader.ReadUIntPtr(address-ptr_size)
if self.heap.IsTaggedObjectAddress(prev_addr):
if prev_addr in possible_context:
possible_context[prev_addr] += 1
else:
possible_context[prev_addr] = 1
address = self.reader.ReadUIntPtr(address)
count += 1
if count <= 5 or len(possible_context) == 0:
return
# Find entry with highest count
possible_context = list(possible_context.items())
possible_context.sort(key=lambda pair: pair[1])
address,count = possible_context[-1]
if count <= 4:
return
self.context = address
def InterpretMemory(self, start, end):
# On 64 bit we omit frame pointers, so we have to do some more guesswork.
frame_pointer = 0
if not self.reader.Is64():
frame_pointer = self.reader.ExceptionFP()
# Follow the framepointer into the address range
while frame_pointer and frame_pointer < start:
frame_pointer = self.reader.ReadUIntPtr(frame_pointer)
if not self.reader.IsExceptionStackAddress(frame_pointer) or \
not frame_pointer:
frame_pointer = 0
break
in_oom_dump_area = False
is_stack = self.reader.IsExceptionStackAddress(start)
free_space_end = 0
ptr_size = self.reader.TaggedPointerSize()
for slot in range(start, end, ptr_size):
if not self.reader.IsValidAddress(slot):
print("%s: Address is not contained within the minidump!" % slot)
return
maybe_address = self.reader.ReadUIntPtr(slot)
address_info = []
# Mark continuous free space objects
if slot == free_space_end:
address_info.append("+")
elif slot <= free_space_end:
address_info.append("|")
else:
free_space_end = 0
heap_object = self.SenseObject(maybe_address, slot)
if heap_object:
# Detect Free-space ranges
if isinstance(heap_object, KnownMap) and \
heap_object.known_name == "FreeSpaceMap":
# The free-space length is is stored as a Smi in the next slot.
length = self.reader.ReadTagged(slot + ptr_size)
if self.heap.IsSmi(length):
length = self.heap.SmiUntag(length)
free_space_end = slot + length - ptr_size
address_info.append(str(heap_object))
relative_offset = self.heap.RelativeOffset(slot, maybe_address)
if relative_offset:
address_info.append(relative_offset)
if maybe_address == self.context:
address_info.append("CONTEXT")
maybe_address_contents = None
if is_stack:
if self.reader.IsExceptionStackAddress(maybe_address):
maybe_address_contents = \
self.reader.ReadUIntPtr(maybe_address) & 0xFFFFFFFF
if maybe_address_contents == 0xdecade00:
in_oom_dump_area = True
if frame_pointer == 0:
frame_pointer = self.TryInferFramePointer(slot, maybe_address)
if frame_pointer != 0:
self.TryInferContext(slot)
maybe_symbol = self.reader.FindSymbol(maybe_address)
if in_oom_dump_area:
if maybe_address_contents == 0xdecade00:
address_info = ["<==== HeapStats start marker"]
elif maybe_address_contents == 0xdecade01:
address_info = ["<==== HeapStats end marker"]
elif maybe_address_contents is not None:
address_info = [
" %d (%d Mbytes)" %
(maybe_address_contents, maybe_address_contents >> 20)
]
if slot == frame_pointer:
if not self.reader.IsExceptionStackAddress(maybe_address):
address_info.append("<==== BAD frame pointer")
frame_pointer = 0
else:
address_info.append("<==== Frame pointer")
frame_pointer = maybe_address
address_type_marker = self.heap.AddressTypeMarker(maybe_address)
string_value = self.reader.ReadAsciiPtr(slot)
print("%s: %s %s %s %s" %
(self.reader.FormatIntPtr(slot),
self.reader.FormatIntPtr(maybe_address), address_type_marker,
string_value, ' | '.join(address_info)))
if maybe_address_contents == 0xdecade01:
in_oom_dump_area = False
heap_object = self.heap.FindObject(maybe_address)
if heap_object:
heap_object.Print(Printer())
print("")
WEB_HEADER = """
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<style media="screen" type="text/css">
.code {
font-family: monospace;
}
.dmptable {
border-collapse : collapse;
border-spacing : 0px;
table-layout: fixed;
}
.codedump {
border-collapse : collapse;
border-spacing : 0px;
table-layout: fixed;
}
.addrcomments {
border : 0px;
}
.register {
padding-right : 1em;
}
.header {
clear : both;
}
.header .navigation {
float : left;
}
.header .dumpname {
float : right;
}
tr.highlight-line {
background-color : yellow;
}
.highlight {
background-color : magenta;
}
tr.inexact-highlight-line {
background-color : pink;
}
input {
background-color: inherit;
border: 1px solid LightGray;
}
.dumpcomments {
border : 1px solid LightGray;
width : 32em;
}
.regions td {
padding:0 15px 0 15px;
}
.stackframe td {
background-color : cyan;
}
.stackaddress, .sa {
background-color : LightGray;
}
.stackval, .sv {
background-color : LightCyan;
}
.frame {
background-color : cyan;
}
.commentinput, .ci {
width : 20em;
}
/* a.nodump */
a.nd:visited {
color : black;
text-decoration : none;
}
a.nd:link {
color : black;
text-decoration : none;
}
a:visited {
color : blueviolet;
}
a:link {
color : blue;
}
.disasmcomment {
color : DarkGreen;
}
</style>
<script type="application/javascript">
var address_str = "address-";
var address_len = address_str.length;
function comment() {
var s = event.srcElement.id;
var index = s.indexOf(address_str);
if (index >= 0) {
send_comment(s.substring(index + address_len), event.srcElement.value);
}
}
var c = comment;
function send_comment(address, comment) {
xmlhttp = new XMLHttpRequest();
address = encodeURIComponent(address)
comment = encodeURIComponent(comment)
xmlhttp.open("GET",
"setcomment?%(query_dump)s&address=" + address +
"&comment=" + comment, true);
xmlhttp.send();
}
var dump_str = "dump-";
var dump_len = dump_str.length;
function dump_comment() {
var s = event.srcElement.id;
var index = s.indexOf(dump_str);
if (index >= 0) {
send_dump_desc(s.substring(index + dump_len), event.srcElement.value);
}
}
function send_dump_desc(name, desc) {
xmlhttp = new XMLHttpRequest();
name = encodeURIComponent(name)
desc = encodeURIComponent(desc)
xmlhttp.open("GET",
"setdumpdesc?dump=" + name +
"&description=" + desc, true);
xmlhttp.send();
}
function onpage(kind, address) {
xmlhttp = new XMLHttpRequest();
kind = encodeURIComponent(kind)
address = encodeURIComponent(address)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
location.reload(true)
}
};
xmlhttp.open("GET",
"setpageaddress?%(query_dump)s&kind=" + kind +
"&address=" + address);
xmlhttp.send();
}
</script>
<title>Dump %(dump_name)s</title>
</head>
<body>
<div class="header">
<form class="navigation" action="search.html">
<a href="summary.html?%(query_dump)s">Context info</a>
<a href="info.html?%(query_dump)s">Dump info</a>
<a href="modules.html?%(query_dump)s">Modules</a>
<input type="search" name="val">
<input type="submit" name="search" value="Search">
<input type="hidden" name="dump" value="%(dump_name)s">
</form>
<form class="navigation" action="disasm.html#highlight">
<input type="search" name="val">
<input type="submit" name="disasm" value="Disasm">
<a href="dumps.html">Dumps...</a>
</form>
</div>
<br>
<hr>
"""
WEB_FOOTER = """
</body>
</html>
"""
class WebParameterError(Exception):
pass
class InspectionWebHandler(http_server.BaseHTTPRequestHandler):
def formatter(self, query_components):
name = query_components.get("dump", [None])[0]
return self.server.get_dump_formatter(name)
def send_success_html_headers(self):
self.send_response(200)
self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
self.send_header("Pragma", "no-cache")
self.send_header("Expires", "0")
self.send_header('Content-type','text/html')
self.end_headers()
return
def write(self, string):
self.wfile.write(string.encode('utf-8'))
def do_GET(self):
try:
parsedurl = urllib.parse.urlparse(self.path)
query_components = urllib.parse.parse_qs(parsedurl.query)
out_buffer = io.StringIO()
if parsedurl.path == "/dumps.html":
self.send_success_html_headers()
self.server.output_dumps(out_buffer)
self.write(out_buffer.getvalue())
elif parsedurl.path == "/summary.html":
self.send_success_html_headers()
self.formatter(query_components).output_summary(out_buffer)
self.write(out_buffer.getvalue())
elif parsedurl.path == "/info.html":
self.send_success_html_headers()
self.formatter(query_components).output_info(out_buffer)
self.write(out_buffer.getvalue())
elif parsedurl.path == "/modules.html":
self.send_success_html_headers()
self.formatter(query_components).output_modules(out_buffer)
self.write(out_buffer.getvalue())
elif parsedurl.path == "/search.html" or parsedurl.path == "/s":
address = query_components.get("val", [])
if len(address) != 1:
self.send_error(404, "Invalid params")
return
self.send_success_html_headers()
self.formatter(query_components).output_search_res(
out_buffer, address[0])
self.write(out_buffer.getvalue())
elif parsedurl.path == "/disasm.html":
address = query_components.get("val", [])
exact = query_components.get("exact", ["on"])
if len(address) != 1:
self.send_error(404, "Invalid params")
return
self.send_success_html_headers()
self.formatter(query_components).output_disasm(
out_buffer, address[0], exact[0])
self.write(out_buffer.getvalue())
elif parsedurl.path == "/data.html":
address = query_components.get("val", [])
datakind = query_components.get("type", ["address"])
if len(address) == 1 and len(datakind) == 1:
self.send_success_html_headers()
self.formatter(query_components).output_data(
out_buffer, address[0], datakind[0])
self.write(out_buffer.getvalue())
else:
self.send_error(404,'Invalid params')
elif parsedurl.path == "/setdumpdesc":
name = query_components.get("dump", [""])
description = query_components.get("description", [""])
if len(name) == 1 and len(description) == 1:
name = name[0]
description = description[0]
if self.server.set_dump_desc(name, description):
self.send_success_html_headers()
self.write("OK")
return
self.send_error(404,'Invalid params')
elif parsedurl.path == "/setcomment":
address = query_components.get("address", [])
comment = query_components.get("comment", [""])
if len(address) == 1 and len(comment) == 1:
address = address[0]
comment = comment[0]
self.formatter(query_components).set_comment(address, comment)
self.send_success_html_headers()
self.write("OK")
else:
self.send_error(404,'Invalid params')
elif parsedurl.path == "/setpageaddress":
kind = query_components.get("kind", [])
address = query_components.get("address", [""])
if len(kind) == 1 and len(address) == 1:
kind = kind[0]
address = address[0]
self.formatter(query_components).set_page_address(kind, address)
self.send_success_html_headers()
self.write("OK")
else:
self.send_error(404,'Invalid params')
else:
self.send_error(404,'File Not Found: %s' % self.path)
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
except WebParameterError as e:
self.send_error(404, 'Web parameter error: %s' % e.message)
HTML_REG_FORMAT = "<span class=\"register\"><b>%s</b>: %s</span><br/>\n"
class InspectionWebFormatter(object):
CONTEXT_FULL = 0
CONTEXT_SHORT = 1
def __init__(self, switches, minidump_name, http_server):
self.dumpfilename = os.path.split(minidump_name)[1]
self.encfilename = urllib.parse.urlencode({'dump': self.dumpfilename})
self.reader = MinidumpReader(switches, minidump_name)
self.server = http_server
# Set up the heap
exception_thread = self.reader.thread_map[self.reader.exception.thread_id]
stack_top = self.reader.ExceptionSP()
stack_bottom = exception_thread.stack.start + \
exception_thread.stack.memory.data_size
stack_map = {self.reader.ExceptionIP(): -1}
for slot in range(stack_top, stack_bottom,
self.reader.MachinePointerSize()):
maybe_address = self.reader.ReadUIntPtr(slot)
if not maybe_address in stack_map:
stack_map[maybe_address] = slot
self.heap = V8Heap(self.reader, stack_map)
self.padawan = InspectionPadawan(self.reader, self.heap)
self.comments = InspectionInfo(minidump_name, self.reader)
self.padawan.known_first_old_page = (
self.comments.get_page_address("oldpage"))
self.padawan.known_first_map_page = (
self.comments.get_page_address("mappage"))
def set_comment(self, straddress, comment):
try:
address = int(straddress, 0)
self.comments.set_comment(address, comment)
except ValueError:
print("Invalid address")
def set_page_address(self, kind, straddress):
try:
address = int(straddress, 0)
if kind == "oldpage":
self.padawan.known_first_old_page = address
elif kind == "mappage":
self.padawan.known_first_map_page = address
self.comments.save_page_address(kind, address)
except ValueError:
print("Invalid address")
def td_from_address(self, f, address):
f.write("<td %s>" % self.comments.get_style_class_string(address))
def format_address(self, maybeaddress, straddress = None):
if maybeaddress is None:
return "not in dump"
else:
if straddress is None:
straddress = "0x" + self.reader.FormatIntPtr(maybeaddress)
style_class = ""
if not self.reader.IsValidAddress(maybeaddress):
style_class = "class=nd"
return ("<a %s href=s?%s&val=%s>%s</a>" %
(style_class, self.encfilename, straddress, straddress))
def format_onheap_address(self, size, maybeaddress, uncompressed):
if maybeaddress is None:
return "not in dump"
else:
straddress = "0x" + self.reader.FormatTagged(maybeaddress)
struncompressed = "0x" + self.reader.FormatIntPtr(uncompressed)
style_class = ""
if not self.reader.IsValidAddress(uncompressed):
style_class = "class=nd"
return ("<a %s href=s?%s&val=%s>%s</a>" %
(style_class, self.encfilename, struncompressed, straddress))
def output_header(self, f):
f.write(WEB_HEADER % {
"query_dump": self.encfilename,
"dump_name": html.escape(self.dumpfilename)
})
def output_footer(self, f):
f.write(WEB_FOOTER)
MAX_CONTEXT_STACK = 2048
def output_summary(self, f):
self.output_header(f)
f.write('<div class="code">')
self.output_context(f, InspectionWebFormatter.CONTEXT_SHORT)
self.output_disasm_pc(f)
# Output stack, trying to also output the stack trace if dumped.
stack_top = self.try_output_stack_trace(f)
exception_thread = self.reader.thread_map[self.reader.exception.thread_id]
stack_bottom = min(exception_thread.stack.start + \
exception_thread.stack.memory.data_size,
stack_top + self.MAX_CONTEXT_STACK)
self.output_words(f, stack_top - 16, stack_bottom, stack_top, "Stack",
self.heap.MachinePointerSize())
f.write('</div>')
self.output_footer(f)
return
def try_output_stack_trace(self, f, start=None, print_message=True):
"""
Try to print a possible message from PushStackTraceAndDie.
Returns the first address where the normal stack starts again.
"""
# Only look at the first 1k words on the stack
ptr_size = self.reader.MachinePointerSize()
if start is None:
start = self.reader.ExceptionSP()
if not self.reader.IsValidAddress(start):
return start
end = start + ptr_size * 1024 * 4
for slot in range(start, end, ptr_size):
if not self.reader.IsValidAddress(slot + ptr_size):
break
magic = self.reader.ReadUIntPtr(slot)
if magic == STACK_TRACE_MARKER:
return self.output_stack_trace(f, slot, start, end, print_message)
return start
def output_stack_trace(self, f, slot, start, end, print_message):
ptr_size = self.reader.MachinePointerSize()
assert self.reader.ReadUIntPtr(slot) & 0xFFFFFFFF == STACK_TRACE_MARKER
# Look for the mid marker after the start slot.
mid_slot = self.reader.FindPtr(STACK_TRACE_MID_MARKER, start + ptr_size,
start + ptr_size * 100)
if not mid_slot:
return self.output_old_stack_trace(f, slot, start, end, print_message)
end_search = mid_slot + (32 * 1024) + (4 * ptr_size)
end_slot = self.reader.FindPtr(STACK_TRACE_END_MARKER, end_search,
end_search + ptr_size * 512)
if not end_slot:
return start
f.write("<h3>PushStackTraceAndDie Stack Message (start=%s):</h3>" %
self.format_address(slot))
slot += ptr_size
value = self.reader.ReadUIntPtr(slot)
f.write(f"<b>isolate</b>: {self.format_address(value)}<br>")
slot += ptr_size
i = 0
while slot < mid_slot:
value = self.reader.ReadUIntPtr(slot)
try:
obj = self.format_object(value)
except Exception as e:
obj = str(e)
f.write(f"<b>ptr{i}</b>: {self.format_address(value)} {obj}<br>")
slot += ptr_size
i += 1
slot = mid_slot + ptr_size
for i in range(4):
value = self.reader.ReadUIntPtr(slot)
try:
obj = self.format_object(value)
except Exception as e:
obj = str(e)
f.write(f"<b>codeObject{i}</b>: {self.format_address(value)} {obj}<br>")
slot += ptr_size
f.write("<b>message start</b>: %s<br>" % self.format_address(slot))
stack_start = end_slot + ptr_size
f.write("<b>stack_start</b>: %s<br>" % self.format_address(stack_start))
(message_start, message) = self.padawan.FindFirstAsciiString(slot)
if print_message and message is not None:
f.write("<a href='#eom'>Scroll to end of message...</a><br>")
self.output_stack_trace_message(f, message)
f.write("<span id=eom></span>")
return stack_start
def output_old_stack_trace(self, f, slot, start, end, print_message):
ptr_size = self.reader.MachinePointerSize()
assert self.reader.ReadUIntPtr(slot) & 0xFFFFFFFF == STACK_TRACE_MARKER
header_size = 10
# Look for the end marker after the fields and the message buffer.
end_search = start + (32 * 1024) + (header_size * ptr_size)
end_slot = self.reader.FindPtr(STACK_TRACE_OLD_END_MARKER, end_search,
end_search + ptr_size * 512)
if not end_slot:
return start
f.write("<h3>PushStackTraceAndDie Stack Message (start=%s):</h3>" %
self.format_address(slot))
slot += ptr_size
for name in ("isolate", "ptr1", "ptr2", "ptr3", "ptr4", "ptr5", "ptr6",
"codeObject1", "codeObject2", "codeObject3", "codeObject4"):
value = self.reader.ReadUIntPtr(slot)
f.write("<b>%s</b>: %s %s<br>" %
(name.rjust(14), self.format_address(value),
"" if name == "isolate" else self.format_object(value)))
slot += ptr_size
f.write("<b>message start</b>: %s<br>" % self.format_address(slot))
stack_start = end_slot + ptr_size
f.write("<b>stack_start</b>: %s<br>" % self.format_address(stack_start))
(message_start, message) = self.padawan.FindFirstAsciiString(slot)
if print_message and message is not None:
f.write("<a href='#eom'>Scroll to end of message...</a><br>")
self.output_stack_trace_message(f, message)
f.write("<span id=eom></span>")
return stack_start
def output_stack_trace_message(self, f, message):
ptr_size = self.reader.MachinePointerSize()
# Annotate all addresses in the dumped message
prog = re.compile("0x[0-9a-fA-F]+")
message = html.escape(message)
message = message.replace("\n", "<br>")
addresses = list(set(prog.findall(message)))
for i in range(len(addresses)):
address_org = addresses[i]
address = int(address_org, 16)
formatted_address = self.format_address(address, address_org)
message = message.replace(address_org, formatted_address)
f.write(message)
def output_info(self, f):
self.output_header(f)
f.write("<h3>Dump info</h3>")
f.write("Description: ")
self.server.output_dump_desc_field(f, self.dumpfilename)
f.write("<br>")
f.write("Filename: ")
f.write("<span class=\"code\">%s</span><br>" % (self.dumpfilename))
dt = datetime.datetime.fromtimestamp(self.reader.header.time_date_stampt)
f.write("Timestamp: %s<br>" % dt.strftime('%Y-%m-%d %H:%M:%S'))
self.output_context(f, InspectionWebFormatter.CONTEXT_FULL)
self.output_address_ranges(f)
self.output_footer(f)
return
def output_address_ranges(self, f):
regions = {}
def print_region(_reader, start, size, _location):
regions[start] = size
self.reader.ForEachMemoryRegion(print_region)
f.write("<h3>Available memory regions</h3>")
f.write('<div class="code">')
f.write("<table class=\"regions\">")
f.write("<thead><tr>")
f.write("<th>Start address</th>")
f.write("<th>End address</th>")
f.write("<th>Number of bytes</th>")
f.write("</tr></thead>")
for start in sorted(regions):
size = regions[start]
f.write("<tr>")
f.write("<td>%s</td>" % self.format_address(start))
f.write("<td> %s</td>" % self.format_address(start + size))
f.write("<td> %d</td>" % size)
f.write("</tr>")
f.write("</table>")
f.write('</div>')
return
def output_module_details(self, f, module):
f.write("<b>%s</b>" % GetModuleName(self.reader, module))
file_version = GetVersionString(module.version_info.dwFileVersionMS,
module.version_info.dwFileVersionLS)
product_version = GetVersionString(module.version_info.dwProductVersionMS,
module.version_info.dwProductVersionLS)
f.write("<br> ")
f.write("base: %s" % self.reader.FormatIntPtr(module.base_of_image))
f.write("<br> ")
f.write(" end: %s" % self.reader.FormatIntPtr(module.base_of_image +
module.size_of_image))
f.write("<br> ")
f.write(" file version: %s" % file_version)
f.write("<br> ")
f.write(" product version: %s" % product_version)
f.write("<br> ")
time_date_stamp = datetime.datetime.fromtimestamp(module.time_date_stamp)
f.write(" timestamp: %s" % time_date_stamp)
f.write("<br>");
def output_modules(self, f):
self.output_header(f)
f.write('<div class="code">')
for module in self.reader.module_list.modules:
self.output_module_details(f, module)
f.write("</div>")
self.output_footer(f)
return
def output_context(self, f, details):
exception_thread = self.reader.thread_map[self.reader.exception.thread_id]
f.write("<h3>Exception context</h3>")
f.write('<div class="code">')
f.write("Thread id: %d" % exception_thread.id)
f.write(" Exception code: %08X<br/>" %
self.reader.exception.exception.code)
if details == InspectionWebFormatter.CONTEXT_FULL:
if self.reader.exception.exception.parameter_count > 0:
f.write(" Exception parameters: ")
for i in range(0, self.reader.exception.exception.parameter_count):
f.write("%08x" % self.reader.exception.exception.information[i])
f.write("<br><br>")
for r in CONTEXT_FOR_ARCH[self.reader.arch]:
f.write(HTML_REG_FORMAT %
(r, self.format_address(self.reader.Register(r))))
# TODO(vitalyr): decode eflags.
if self.reader.arch in [MD_CPU_ARCHITECTURE_ARM, MD_CPU_ARCHITECTURE_ARM64]:
f.write("<b>cpsr</b>: %s" % bin(self.reader.exception_context.cpsr)[2:])
else:
f.write("<b>eflags</b>: %s" %
bin(self.reader.exception_context.eflags)[2:])
f.write('</div>')
return
def align_down(self, a, size):
alignment_correction = a % size
return a - alignment_correction
def align_up(self, a, size):
alignment_correction = (size - 1) - ((a + size - 1) % size)
return a + alignment_correction
def format_object(self, address):
heap_object = self.padawan.SenseObject(address)
return html.escape(str(heap_object or ""))
def output_data(self, f, straddress, datakind):
try:
self.output_header(f)
address = int(straddress, 0)
if not self.reader.IsValidAddress(address):
f.write("<h3>Address 0x%x not found in the dump.</h3>" % address)
return
region = self.reader.FindRegion(address)
if datakind == "address":
self.output_words(f, region[0], region[0] + region[1], address, "Dump",
self.heap.MachinePointerSize())
if datakind == "tagged":
self.output_words(f, region[0], region[0] + region[1], address,
"Tagged Dump", self.heap.TaggedPointerSize())
elif datakind == "ascii":
self.output_ascii(f, region[0], region[0] + region[1], address)
self.output_footer(f)
except ValueError:
f.write("<h3>Unrecognized address format \"%s\".</h3>" % straddress)
return
def output_words(self, f, start_address, end_address, highlight_address, desc,
size):
region = self.reader.FindRegion(highlight_address)
if region is None:
f.write("<h3>Address 0x%x not found in the dump.</h3>" %
(highlight_address))
return
start_address = self.align_down(start_address, size)
low = self.align_down(region[0], size)
high = self.align_up(region[0] + region[1], size)
if start_address < low:
start_address = low
end_address = self.align_up(end_address, size)
if end_address > high:
end_address = high
expand = ""
if start_address != low or end_address != high:
additional_query = ""
if self.heap.IsPointerCompressed(
) and size == self.reader.TaggedPointerSize():
additional_query = "&type=tagged"
expand = ("(<a href=\"data.html?%s&val=0x%x%s#highlight\">"
" more..."
" </a>)" %
(self.encfilename, highlight_address, additional_query))
f.write("<h3>%s 0x%x - 0x%x, "
"highlighting <a href=\"#highlight\">0x%x</a> %s</h3>" %
(desc, start_address, end_address, highlight_address, expand))
f.write('<div class="code">')
f.write("<table class=codedump>")
for j in range(0, end_address - start_address, size):
slot = start_address + j
heap_object = ""
maybe_address = None
maybe_uncompressed_address = None
end_region = region[0] + region[1]
if slot < region[0] or slot + size > end_region:
straddress = "0x"
for i in range(end_region, slot + size):
straddress += "??"
for i in reversed(
range(max(slot, region[0]), min(slot + size, end_region))):
straddress += "%02x" % self.reader.ReadU8(i)
for i in range(slot, region[0]):
straddress += "??"
else:
maybe_address = self.reader.ReadSized(slot, size)
if size == self.reader.MachinePointerSize():
maybe_uncompressed_address = maybe_address
else:
maybe_uncompressed_address = (slot & (0xFFFFFFFF << 32)) | (
maybe_address & 0xFFFFFFFF)
if size == self.reader.TaggedPointerSize():
straddress = self.format_onheap_address(size, maybe_address,
maybe_uncompressed_address)
if maybe_address:
heap_object = self.format_object(maybe_uncompressed_address)
else:
straddress = self.format_address(maybe_address)
address_fmt = "%s </td>"
if slot == highlight_address:
f.write("<tr class=highlight-line>")
address_fmt = "<a id=highlight></a>%s </td>"
elif slot < highlight_address and highlight_address < slot + size:
f.write("<tr class=inexact-highlight-line>")
address_fmt = "<a id=highlight></a>%s </td>"
else:
f.write("<tr>")
f.write("<td>")
self.output_comment_box(f, "da-", slot)
f.write("</td>")
self.td_from_address(f, slot)
f.write(address_fmt % self.format_address(slot))
self.td_from_address(f, maybe_uncompressed_address)
f.write(": %s </td>" % straddress)
f.write("<td>")
if maybe_uncompressed_address is not None:
self.output_comment_box(f, "sv-" + self.reader.FormatIntPtr(slot),
maybe_uncompressed_address)
f.write("</td>")
f.write("<td>%s</td>" % (heap_object or ''))
f.write("</tr>")
f.write("</table>")
f.write("</div>")
return
def output_ascii(self, f, start_address, end_address, highlight_address):
region = self.reader.FindRegion(highlight_address)
if region is None:
f.write("<h3>Address %x not found in the dump.</h3>" %
highlight_address)
return
if start_address < region[0]:
start_address = region[0]
if end_address > region[0] + region[1]:
end_address = region[0] + region[1]
expand = ""
if start_address != region[0] or end_address != region[0] + region[1]:
link = ("data.html?%s&val=0x%x&type=ascii#highlight" %
(self.encfilename, highlight_address))
expand = "(<a href=\"%s\">more...</a>)" % link
f.write("<h3>ASCII dump 0x%x - 0x%x, highlighting 0x%x %s</h3>" %
(start_address, end_address, highlight_address, expand))
line_width = 64
f.write('<div class="code">')
start = self.align_down(start_address, line_width)
for i in range(end_address - start):
address = start + i
if address % 64 == 0:
if address != start:
f.write("<br>")
f.write("0x%08x: " % address)
if address < start_address:
f.write(" ")
else:
if address == highlight_address:
f.write("<span class=\"highlight\">")
code = self.reader.ReadU8(address)
if code < 127 and code >= 32:
f.write("&#")
f.write(str(code))
f.write(";")
else:
f.write("·")
if address == highlight_address:
f.write("</span>")
f.write("</div>")
return
def output_disasm(self, f, straddress, strexact):
try:
self.output_header(f)
address = int(straddress, 0)
if not self.reader.IsValidAddress(address):
f.write("<h3>Address 0x%x not found in the dump.</h3>" % address)
return
region = self.reader.FindRegion(address)
self.output_disasm_range(
f, region[0], region[0] + region[1], address, strexact == "on")
self.output_footer(f)
except ValueError:
f.write("<h3>Unrecognized address format \"%s\".</h3>" % straddress)
return
def output_disasm_range(
self, f, start_address, end_address, highlight_address, exact):
region = self.reader.FindRegion(highlight_address)
if start_address < region[0]:
start_address = region[0]
if end_address > region[0] + region[1]:
end_address = region[0] + region[1]
count = end_address - start_address
lines = self.reader.GetDisasmLines(start_address, count)
found = False
if exact:
for line in lines:
if line[0] + start_address == highlight_address:
found = True
break
if not found:
start_address = highlight_address
count = end_address - start_address
lines = self.reader.GetDisasmLines(highlight_address, count)
expand = ""
if start_address != region[0] or end_address != region[0] + region[1]:
exactness = ""
if exact and not found and end_address == region[0] + region[1]:
exactness = "&exact=off"
expand = ("(<a href=\"disasm.html?%s%s"
"&val=0x%x#highlight\">more...</a>)" %
(self.encfilename, exactness, highlight_address))
f.write("<h3>Disassembling 0x%x - 0x%x, highlighting 0x%x %s</h3>" %
(start_address, end_address, highlight_address, expand))
f.write('<div class="code">')
f.write("<table class=\"codedump\">");
for i in range(len(lines)):
line = lines[i]
next_address = count
if i + 1 < len(lines):
next_line = lines[i + 1]
next_address = next_line[0]
self.format_disasm_line(
f, start_address, line, next_address, highlight_address)
f.write("</table>")
f.write("</div>")
return
def annotate_disasm_addresses(self, line):
extra = []
for m in ADDRESS_RE.finditer(line):
maybe_address = int(m.group(0), 16)
formatted_address = self.format_address(maybe_address, m.group(0))
line = line.replace(m.group(0), formatted_address)
object_info = self.padawan.SenseObject(maybe_address)
if not object_info:
continue
extra.append(html.escape(str(object_info)))
if len(extra) == 0:
return line
return ("%s <span class=disasmcomment>;; %s</span>" %
(line, ", ".join(extra)))
def format_disasm_line(
self, f, start, line, next_address, highlight_address):
line_address = start + line[0]
address_fmt = " <td>%s</td>"
if line_address == highlight_address:
f.write("<tr class=highlight-line>")
address_fmt = " <td><a id=highlight>%s</a></td>"
elif (line_address < highlight_address and
highlight_address < next_address + start):
f.write("<tr class=inexact-highlight-line>")
address_fmt = " <td><a id=highlight>%s</a></td>"
else:
f.write("<tr>")
num_bytes = next_address - line[0]
stack_slot = self.heap.stack_map.get(line_address)
marker = ""
if stack_slot:
marker = "=>"
code = line[1]
# Some disassemblers insert spaces between each byte,
# while some do not.
if code[2] == " ":
op_offset = 3 * num_bytes - 1
else:
op_offset = 2 * num_bytes
# Compute the actual call target which the disassembler is too stupid
# to figure out (it adds the call offset to the disassembly offset rather
# than the absolute instruction address).
if self.heap.reader.arch == MD_CPU_ARCHITECTURE_X86:
if code.startswith("e8"):
words = code.split()
if len(words) > 6 and words[5] == "call":
offset = int(words[4] + words[3] + words[2] + words[1], 16)
target = (line_address + offset + 5) & 0xFFFFFFFF
code = code.replace(words[6], "0x%08x" % target)
# TODO(jkummerow): port this hack to ARM and x64.
opcodes = code[:op_offset]
code = self.annotate_disasm_addresses(code[op_offset:])
f.write(" <td>")
self.output_comment_box(f, "codel-", line_address)
f.write("</td>")
f.write(address_fmt % marker)
f.write(" ")
self.td_from_address(f, line_address)
f.write(self.format_address(line_address))
f.write(" (+0x%x)</td>" % line[0])
f.write("<td>: %s </td>" % opcodes)
f.write("<td>%s</td>" % code)
f.write("</tr>")
def output_comment_box(self, f, prefix, address):
comment = self.comments.get_comment(address)
value = ""
if comment:
value = " value=\"%s\"" % html.escape(comment)
f.write("<input type=text class=ci "
"id=%s-address-0x%s onchange=c()%s>" %
(prefix,
self.reader.FormatIntPtr(address),
value))
MAX_FOUND_RESULTS = 100
def output_find_results(self, f, results):
f.write("Addresses")
toomany = len(results) > self.MAX_FOUND_RESULTS
if toomany:
f.write("(found %i results, displaying only first %i)" %
(len(results), self.MAX_FOUND_RESULTS))
f.write(": ")
results = sorted(results)
results = results[:min(len(results), self.MAX_FOUND_RESULTS)]
for address in results:
f.write("<span %s>%s</span>" %
(self.comments.get_style_class_string(address),
self.format_address(address)))
if toomany:
f.write("...")
def output_page_info(self, f, page_kind, page_address, my_page_address):
if my_page_address == page_address and page_address != 0:
f.write("Marked first %s page." % page_kind)
else:
f.write("<span id=\"%spage\" style=\"display:none\">" % page_kind)
f.write("Marked first %s page." % page_kind)
f.write("</span>\n")
f.write("<button onclick=\"onpage('%spage', '0x%x')\">" %
(page_kind, my_page_address))
f.write("Mark as first %s page</button>" % page_kind)
return
def output_search_res(self, f, straddress):
try:
self.output_header(f)
f.write("<h3>Search results for %s</h3>" % straddress)
address = int(straddress, 0)
f.write("Comment: ")
self.output_comment_box(f, "search-", address)
f.write("<br>")
page_address = address & ~self.heap.PageAlignmentMask()
f.write("Page info: ")
self.output_page_info(f, "old", self.padawan.known_first_old_page, \
page_address)
self.output_page_info(f, "map", self.padawan.known_first_map_page, \
page_address)
if not self.reader.IsValidAddress(address):
f.write("<h3>The contents at address %s not found in the dump.</h3>" % \
straddress)
else:
# Print as words
self.output_words(f, address - 8, address + 32, address, "Dump",
self.heap.MachinePointerSize())
if self.heap.IsPointerCompressed():
self.output_words(f, address - 8, address + 32, address,
"Tagged Dump", self.heap.TaggedPointerSize())
# Print as ASCII
f.write("<hr>")
self.output_ascii(f, address, address + 256, address)
# Print as code
f.write("<hr>")
self.output_disasm_range(f, address - 16, address + 16, address, True)
aligned_res, unaligned_res = self.reader.FindWordList(address)
if len(aligned_res) > 0:
f.write("<h3>Occurrences of 0x%x at aligned addresses</h3>" %
address)
self.output_find_results(f, aligned_res)
if len(unaligned_res) > 0:
f.write("<h3>Occurrences of 0x%x at unaligned addresses</h3>" % \
address)
self.output_find_results(f, unaligned_res)
if len(aligned_res) + len(unaligned_res) == 0:
f.write("<h3>No occurrences of 0x%x found in the dump</h3>" % address)
self.output_footer(f)
except ValueError:
f.write("<h3>Unrecognized address format \"%s\".</h3>" % straddress)
return
def output_disasm_pc(self, f):
address = self.reader.ExceptionIP()
if not self.reader.IsValidAddress(address):
return
self.output_disasm_range(f, address - 16, address + 16, address, True)
WEB_DUMPS_HEADER = """
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<style media="screen" type="text/css">
.dumplist {
border-collapse : collapse;
border-spacing : 0px;
font-family: monospace;
}
.dumpcomments {
border : 1px solid LightGray;
width : 32em;
}
</style>
<script type="application/javascript">
var dump_str = "dump-";
var dump_len = dump_str.length;
function dump_comment() {
var s = event.srcElement.id;
var index = s.indexOf(dump_str);
if (index >= 0) {
send_dump_desc(s.substring(index + dump_len), event.srcElement.value);
}
}
function send_dump_desc(name, desc) {
xmlhttp = new XMLHttpRequest();
name = encodeURIComponent(name)
desc = encodeURIComponent(desc)
xmlhttp.open("GET",
"setdumpdesc?dump=" + name +
"&description=" + desc, true);
xmlhttp.send();
}
</script>
<title>Dump list</title>
</head>
<body>
"""
WEB_DUMPS_FOOTER = """
</body>
</html>
"""
DUMP_FILE_RE = re.compile(r"[-_0-9a-zA-Z][-\._0-9a-zA-Z]*\.dmp$")
class InspectionWebServer(http_server.HTTPServer):
def __init__(self, port_number, switches, minidump_name):
super().__init__(('localhost', port_number), InspectionWebHandler)
splitpath = os.path.split(minidump_name)
self.dumppath = splitpath[0]
self.dumpfilename = splitpath[1]
self.default_formatter = InspectionWebFormatter(
switches, minidump_name, self)
self.formatters = { self.dumpfilename : self.default_formatter }
self.switches = switches
def output_dump_desc_field(self, f, name):
try:
descfile = open(os.path.join(self.dumppath, name + ".desc"), "r")
desc = descfile.readline()
descfile.close()
except IOError:
desc = ""
f.write("<input type=\"text\" class=\"dumpcomments\" "
"id=\"dump-%s\" onchange=\"dump_comment()\" value=\"%s\">\n" %
(html.escape(name), desc))
def set_dump_desc(self, name, description):
if not DUMP_FILE_RE.match(name):
return False
fname = os.path.join(self.dumppath, name)
if not os.path.isfile(fname):
return False
fname = fname + ".desc"
descfile = open(fname, "w")
descfile.write(description)
descfile.close()
return True
def get_dump_formatter(self, name):
if name is None:
return self.default_formatter
else:
if not DUMP_FILE_RE.match(name):
raise WebParameterError("Invalid name '%s'" % name)
formatter = self.formatters.get(name, None)
if formatter is None:
try:
formatter = InspectionWebFormatter(
self.switches, os.path.join(self.dumppath, name), self)
self.formatters[name] = formatter
except IOError:
raise WebParameterError("Could not open dump '%s'" % name)
return formatter
def output_dumps(self, f):
f.write(WEB_DUMPS_HEADER)
f.write("<h3>List of available dumps</h3>")
f.write("<table class=\"dumplist\">\n")
f.write("<thead><tr>")
f.write("<th>Name</th>")
f.write("<th>File time</th>")
f.write("<th>Comment</th>")
f.write("</tr></thead>")
dumps_by_time = {}
for fname in os.listdir(self.dumppath):
if DUMP_FILE_RE.match(fname):
mtime = os.stat(os.path.join(self.dumppath, fname)).st_mtime
fnames = dumps_by_time.get(mtime, [])
fnames.append(fname)
dumps_by_time[mtime] = fnames
for mtime in sorted(dumps_by_time, reverse=True):
fnames = dumps_by_time[mtime]
for fname in fnames:
f.write("<tr>\n")
f.write("<td><a href=\"summary.html?%s\">%s</a></td>\n" %
((urllib.parse.urlencode({'dump': fname}), fname)))
f.write("<td> ")
f.write(datetime.datetime.fromtimestamp(mtime))
f.write("</td>")
f.write("<td> ")
self.output_dump_desc_field(f, fname)
f.write("</td>")
f.write("</tr>\n")
f.write("</table>\n")
f.write(WEB_DUMPS_FOOTER)
return
class InspectionShell(cmd.Cmd):
def __init__(self, reader, heap):
cmd.Cmd.__init__(self)
self.reader = reader
self.heap = heap
self.padawan = InspectionPadawan(reader, heap)
self.prompt = "(grok) "
self.dd_start = 0
self.dd_num = 0x10
self.u_start = 0
self.u_num = 0
def EvalExpression(self, expr):
# Auto convert hex numbers to a python compatible format
if expr[:2] == "00":
expr = "0x"+expr
result = None
try:
# Ugly hack to patch in register values.
registers = [register
for register,value in self.reader.ContextDescriptor().fields]
registers.sort(key=lambda r: len(r))
registers.reverse()
for register in registers:
expr = expr.replace("$"+register, str(self.reader.Register(register)))
result = eval(expr)
except Exception as e:
print("**** Could not evaluate '%s': %s" % (expr, e))
raise e
return result
def ParseAddressExpr(self, expr):
address = 0;
try:
result = self.EvalExpression(expr)
except:
return 0
try:
address = int(result)
except Exception as e:
print("**** Could not convert '%s' => %s to valid address: %s" % (
expr, result , e))
return address
def do_help(self, cmd=None):
if len(cmd) == 0:
print("Available commands")
print("=" * 79)
prefix = "do_"
methods = inspect.getmembers(InspectionShell, predicate=inspect.ismethod)
for name,method in methods:
if not name.startswith(prefix): continue
doc = inspect.getdoc(method)
if not doc: continue
name = prefix.join(name.split(prefix)[1:])
description = doc.splitlines()[0]
print((name + ": ").ljust(16) + description)
print("=" * 79)
else:
return super(InspectionShell, self).do_help(cmd)
def do_p(self, cmd):
""" see print """
return self.do_print(cmd)
def do_print(self, cmd):
"""
Evaluate an arbitrary python command.
"""
try:
print(self.EvalExpression(cmd))
except:
pass
def do_da(self, address):
""" see display_ascii"""
return self.do_display_ascii(address)
def do_display_ascii(self, address):
"""
Print ASCII string starting at specified address.
"""
address = self.ParseAddressExpr(address)
string = self.reader.ReadAsciiString(address)
if string == "":
print("Not an ASCII string at %s" % self.reader.FormatIntPtr(address))
else:
print("%s\n" % string)
def do_dsa(self, address):
""" see display_stack_ascii"""
return self.do_display_stack_ascii(address)
def do_display_stack_ascii(self, address):
"""
Print ASCII stack error message.
"""
if self.reader.exception is None:
print("Minidump has no exception info")
return
if len(address) == 0:
address = None
else:
address = self.ParseAddressExpr(address)
self.padawan.PrintStackTraceMessage(address)
def do_dd(self, args):
"""
Interpret memory in the given region [address, address + num * word_size)
(if available) as a sequence of words. Automatic alignment is not performed.
If the num is not specified, a default value of 16 words is usif not self.Is
If no address is given, dd continues printing at the next word.
Synopsis: dd 0x<address>|$register [0x<num>]
"""
if len(args) != 0:
args = args.split(' ')
self.dd_start = self.ParseAddressExpr(args[0])
self.dd_num = int(args[1], 16) if len(args) > 1 else 0x10
else:
self.dd_start += self.dd_num * self.reader.MachinePointerSize()
if not self.reader.IsAlignedAddress(self.dd_start):
print("Warning: Dumping un-aligned memory, is this what you had in mind?")
end = self.dd_start + self.reader.MachinePointerSize() * self.dd_num
self.padawan.InterpretMemory(self.dd_start, end)
def do_do(self, address):
""" see display_object """
return self.do_display_object(address)
def do_display_object(self, address):
"""
Interpret memory at the given address as a V8 object.
Automatic alignment makes sure that you can pass tagged as well as
un-tagged addresses.
"""
address = self.ParseAddressExpr(address)
if self.reader.IsAlignedAddress(address):
address = address + 1
elif not self.heap.IsTaggedObjectAddress(address):
print("Address doesn't look like a valid pointer!")
return
heap_object = self.padawan.SenseObject(address)
if heap_object:
heap_object.Print(Printer())
else:
print("Address cannot be interpreted as object!")
def do_dso(self, args):
""" see display_stack_objects """
return self.do_display_stack_objects(args)
def do_display_stack_objects(self, args):
"""
Find and Print object pointers in the given range.
Print all possible object pointers that are on the stack or in the given
address range.
Usage: dso [START_ADDR,[END_ADDR]]
"""
start = self.reader.StackTop()
end = self.reader.StackBottom()
if len(args) != 0:
args = args.split(' ')
start = self.ParseAddressExpr(args[0])
end = self.ParseAddressExpr(args[1]) if len(args) > 1 else end
objects = self.heap.FindObjectPointers(start, end)
for address in objects:
heap_object = self.padawan.SenseObject(address)
info = ""
if heap_object:
info = str(heap_object)
print("%s %s" % (self.padawan.FormatIntPtr(address), info))
def do_do_desc(self, address):
"""
Print a descriptor array in a readable format.
"""
start = self.ParseAddressExpr(address)
if ((start & 1) == 1): start = start - 1
DescriptorArray(FixedArray(self.heap, None, start)).Print(Printer())
def do_do_map(self, address):
"""
Print a Map in a readable format.
"""
start = self.ParseAddressExpr(address)
if ((start & 1) == 1): start = start - 1
Map(self.heap, None, start).Print(Printer())
def do_do_trans(self, address):
"""
Print a transition array in a readable format.
"""
start = self.ParseAddressExpr(address)
if ((start & 1) == 1): start = start - 1
TransitionArray(FixedArray(self.heap, None, start)).Print(Printer())
def do_dp(self, address):
""" see display_page """
return self.do_display_page(address)
def do_display_page(self, address):
"""
Prints details about the V8 heap page of the given address.
Interpret memory at the given address as being on a V8 heap page
and print information about the page header (if available).
"""
address = self.ParseAddressExpr(address)
page_address = address & ~self.heap.PageAlignmentMask()
if self.reader.IsValidAddress(page_address):
print("**** Not Implemented")
return
else:
print("Page header is not available!")
def do_k(self, arguments):
"""
Teach V8 heap layout information to the inspector.
This increases the amount of annotations the inspector can produce while
dumping data. The first page of each heap space is of particular interest
because it contains known objects that do not move.
"""
self.padawan.PrintKnowledge()
def do_ko(self, address):
""" see known_oldspace """
return self.do_known_oldspace(address)
def do_known_oldspace(self, address):
"""
Teach V8 heap layout information to the inspector.
Set the first old space page by passing any pointer into that page.
"""
address = self.ParseAddressExpr(address)
page_address = address & ~self.heap.PageAlignmentMask()
self.padawan.known_first_old_page = page_address
def do_km(self, address):
""" see known_map """
return self.do_known_map(address)
def do_known_map(self, address):
"""
Teach V8 heap layout information to the inspector.
Set the first map-space page by passing any pointer into that page.
"""
address = self.ParseAddressExpr(address)
page_address = address & ~self.heap.PageAlignmentMask()
self.padawan.known_first_map_page = page_address
def do_list(self, smth):
"""
List all available memory regions.
"""
def print_region(reader, start, size, location):
print(" %s - %s (%d bytes)" % (reader.FormatIntPtr(start),
reader.FormatIntPtr(start + size),
size))
print("Available memory regions:")
self.reader.ForEachMemoryRegion(print_region)
def do_lm(self, arg):
""" see list_modules """
return self.do_list_modules(arg)
def do_list_modules(self, arg):
"""
List details for all loaded modules in the minidump.
An argument can be passed to limit the output to only those modules that
contain the argument as a substring (case insensitive match).
"""
for module in self.reader.module_list.modules:
if arg:
name = GetModuleName(self.reader, module).lower()
if name.find(arg.lower()) >= 0:
PrintModuleDetails(self.reader, module)
else:
PrintModuleDetails(self.reader, module)
print()
def do_s(self, word):
""" see search """
return self.do_search(word)
def do_search(self, word):
"""
Search for a given word in available memory regions.
The given word is expanded to full pointer size and searched at aligned
as well as un-aligned memory locations. Use 'sa' to search aligned locations
only.
"""
try:
word = self.ParseAddressExpr(word)
except ValueError:
print("Malformed word, prefix with '0x' to use hexadecimal format.")
return
print(
"Searching for word %d/0x%s:" % (word, self.reader.FormatIntPtr(word)))
self.reader.FindWord(word)
def do_sh(self, none):
"""
Search for the V8 Heap object in all available memory regions.
You might get lucky and find this rare treasure full of invaluable
information.
"""
print("**** Not Implemented")
def do_u(self, args):
""" see disassemble """
return self.do_disassemble(args)
def do_disassemble(self, args):
"""
Unassemble memory in the region [address, address + size).
If the size is not specified, a default value of 32 bytes is used.
Synopsis: u 0x<address> 0x<size>
"""
if len(args) != 0:
args = args.split(' ')
self.u_start = self.ParseAddressExpr(args[0])
self.u_size = self.ParseAddressExpr(args[1]) if len(args) > 1 else 0x20
skip = False
else:
# Skip the first instruction if we reuse the last address.
skip = True
if not self.reader.IsValidAddress(self.u_start):
print("Address %s is not contained within the minidump!" % (
self.reader.FormatIntPtr(self.u_start)))
return
lines = self.reader.GetDisasmLines(self.u_start, self.u_size)
if len(lines) == 0:
print("Address %s could not be disassembled!" % (
self.reader.FormatIntPtr(self.u_start)))
print(" Could not disassemble using %s." % OBJDUMP_BIN)
print(" Pass path to architecture specific objdump via --objdump?")
return
for line in lines:
if skip:
skip = False
continue
print(FormatDisasmLine(self.u_start, self.heap, line))
# Set the next start address = last line
self.u_start += lines[-1][0]
print()
def do_EOF(self, none):
raise KeyboardInterrupt
EIP_PROXIMITY = 64
CONTEXT_FOR_ARCH = {
MD_CPU_ARCHITECTURE_AMD64:
['rax', 'rbx', 'rcx', 'rdx', 'rdi', 'rsi', 'rbp', 'rsp', 'rip',
'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15'],
MD_CPU_ARCHITECTURE_ARM:
['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9',
'r10', 'r11', 'r12', 'sp', 'lr', 'pc'],
MD_CPU_ARCHITECTURE_ARM64:
['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9',
'r10', 'r11', 'r12', 'r13', 'r14', 'r15', 'r16', 'r17', 'r18', 'r19',
'r20', 'r21', 'r22', 'r23', 'r24', 'r25', 'r26', 'r27', 'r28',
'fp', 'lr', 'sp', 'pc'],
MD_CPU_ARCHITECTURE_X86:
['eax', 'ebx', 'ecx', 'edx', 'edi', 'esi', 'ebp', 'esp', 'eip']
}
KNOWN_MODULES = {'chrome.exe', 'chrome.dll'}
def GetVersionString(ms, ls):
return "%d.%d.%d.%d" % (ms >> 16, ms & 0xffff, ls >> 16, ls & 0xffff)
def GetModuleName(reader, module):
name = reader.ReadMinidumpString(module.module_name_rva)
# simplify for path manipulation
name = name.encode('utf-8')
return str(os.path.basename(str(name).replace("\\", "/")))
def PrintModuleDetails(reader, module):
print("%s" % GetModuleName(reader, module))
file_version = GetVersionString(module.version_info.dwFileVersionMS,
module.version_info.dwFileVersionLS);
product_version = GetVersionString(module.version_info.dwProductVersionMS,
module.version_info.dwProductVersionLS)
print(" base: %s" % reader.FormatIntPtr(module.base_of_image))
print(" end: %s" % reader.FormatIntPtr(module.base_of_image +
module.size_of_image))
print(" file version: %s" % file_version)
print(" product version: %s" % product_version)
time_date_stamp = datetime.datetime.fromtimestamp(module.time_date_stamp)
print(" timestamp: %s" % time_date_stamp)
def AnalyzeMinidump(options, minidump_name):
reader = MinidumpReader(options, minidump_name)
# Use a separate function to prevent leaking the minidump buffer through
# ctypes in local variables.
_AnalyzeMinidump(options, reader)
reader.Dispose()
def _AnalyzeMinidump(options, reader):
heap = None
stack_top = reader.ExceptionSP()
stack_bottom = reader.StackBottom()
stack_map = {reader.ExceptionIP(): -1}
for slot in range(stack_top, stack_bottom, reader.MachinePointerSize()):
maybe_address = reader.ReadUIntPtr(slot)
if not maybe_address in stack_map:
stack_map[maybe_address] = slot
heap = V8Heap(reader, stack_map)
padawan = InspectionPadawan(reader, heap)
DebugPrint("========================================")
if reader.exception is None:
print("Minidump has no exception info")
else:
print("Address markers:")
print(" T = valid tagged pointer in the minidump")
print(" S = address on the exception stack")
print(" C = address in loaded C/C++ module")
print(" * = address in the minidump")
print("")
print("Exception info:")
exception_thread = reader.ExceptionThread()
print(" thread id: %d" % exception_thread.id)
print(" code: %08X" % reader.exception.exception.code)
print(" context:")
context = CONTEXT_FOR_ARCH[reader.arch]
maxWidth = max(map(lambda s: len(s), context))
for r in context:
register_value = reader.Register(r)
print(" %s: %s" % (r.rjust(maxWidth),
heap.FormatIntPtr(register_value)))
# TODO(vitalyr): decode eflags.
if reader.arch in [MD_CPU_ARCHITECTURE_ARM, MD_CPU_ARCHITECTURE_ARM64]:
print(" cpsr: %s" % bin(reader.exception_context.cpsr)[2:])
else:
print(" eflags: %s" % bin(reader.exception_context.eflags)[2:])
print()
print(" modules:")
for module in reader.module_list.modules:
name = GetModuleName(reader, module)
if name in KNOWN_MODULES:
print(" %s at %08X" % (name, module.base_of_image))
reader.TryLoadSymbolsFor(name, module)
print()
print(" stack-top: %s" % heap.FormatIntPtr(reader.StackTop()))
print(" stack-bottom: %s" % heap.FormatIntPtr(reader.StackBottom()))
print("")
if options.shell:
padawan.PrintStackTraceMessage(print_message=False)
print("Disassembly around exception.eip:")
eip_symbol = reader.FindSymbol(reader.ExceptionIP())
if eip_symbol is not None:
print(eip_symbol)
disasm_start = reader.ExceptionIP() - EIP_PROXIMITY
disasm_bytes = 2 * EIP_PROXIMITY
if (options.full):
full_range = reader.FindRegion(reader.ExceptionIP())
if full_range is not None:
disasm_start = full_range[0]
disasm_bytes = full_range[1]
lines = reader.GetDisasmLines(disasm_start, disasm_bytes)
if not lines:
print("Could not disassemble using %s." % OBJDUMP_BIN)
print("Pass path to architecture specific objdump via --objdump?")
for line in lines:
print(FormatDisasmLine(disasm_start, heap, line))
print()
if heap is None:
heap = V8Heap(reader, None)
if options.full:
FullDump(reader, heap)
if options.command:
InspectionShell(reader, heap).onecmd(options.command)
if options.shell:
try:
InspectionShell(reader, heap).cmdloop("type help to get help")
except KeyboardInterrupt:
print("Kthxbye.")
elif not options.command:
if reader.exception is not None:
print("Annotated stack (from exception.esp to bottom):")
stack_start = padawan.PrintStackTraceMessage()
padawan.InterpretMemory(stack_start, stack_bottom)
if __name__ == "__main__":
parser = optparse.OptionParser(USAGE)
parser.add_option("-s", "--shell", dest="shell", action="store_true",
help="start an interactive inspector shell")
parser.add_option("-w", "--web", dest="web", action="store_true",
help="start a web server on localhost:%i" % PORT_NUMBER)
parser.add_option("-c", "--command", dest="command", default="",
help="run an interactive inspector shell command and exit")
parser.add_option("-f", "--full", dest="full", action="store_true",
help="dump all information contained in the minidump")
parser.add_option("--symdir", dest="symdir", default=".",
help="directory containing *.pdb.sym file with symbols")
parser.add_option("--objdump", default="",
help="objdump tool to use [default: %s]" % (
DEFAULT_OBJDUMP_BIN))
options, args = parser.parse_args()
if len(args) != 1:
parser.print_help()
sys.exit(1)
if options.web:
try:
server = InspectionWebServer(PORT_NUMBER, options, args[0])
print('Started httpserver on port ' , PORT_NUMBER)
webbrowser.open('http://localhost:%i/summary.html' % PORT_NUMBER)
server.serve_forever()
except KeyboardInterrupt:
print('^C received, shutting down the web server')
server.socket.close()
else:
AnalyzeMinidump(options, args[0])
|