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
|
(* This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2, 3 or any later version
of the License (at your option).
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*)
(*
settings set target.output-path /tmp/out.txt
*)
unit LldbDebugger;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, math, DbgIntfDebuggerBase, DbgIntfBaseTypes, LazLoggerBase,
LazClasses, LazFileUtils, Maps, LCLProc, strutils, DebugProcess,
LldbInstructions, LldbHelper;
type
(*
* Commands
*)
TLldbDebugger = class;
TLldbDebuggerCommand = class;
{ TLldbDebuggerCommandQueue }
TLldbDebuggerCommandQueue = class(TRefCntObjList)
private
FDebugger: TLldbDebugger;
FLockQueueRun: Integer;
function Get(Index: Integer): TLldbDebuggerCommand;
procedure Put(Index: Integer; const AValue: TLldbDebuggerCommand);
private
FRunningCommand: TLldbDebuggerCommand;
procedure Run; // Call Debugger.OnIdle // set IsIdle
protected
procedure CommandFinished(ACommand: TLldbDebuggerCommand);
public
constructor Create(ADebugger: TLldbDebugger);
destructor Destroy; override;
procedure CancelAll;
procedure LockQueueRun;
procedure UnLockQueueRun;
property Items[Index: Integer]: TLldbDebuggerCommand read Get write Put; default;
procedure QueueCommand(AValue: TLldbDebuggerCommand);
procedure DoLineDataReceived(var ALine: String);
property RunningCommand: TLldbDebuggerCommand read FRunningCommand;
end;
{ TLldbDebuggerCommand }
TLldbDebuggerCommand = class(TRefCountedObject)
private
FOwner: TLldbDebugger;
FIsRunning: Boolean;
function GetDebuggerState: TDBGState;
function GetCommandQueue: TLldbDebuggerCommandQueue;
function GetInstructionQueue: TLldbInstructionQueue;
protected
procedure DoLineDataReceived(var ALine: String); virtual;
procedure DoExecute; virtual; abstract;
procedure DoCancel; virtual;
procedure Finished;
procedure InstructionSucceeded(AnInstruction: TObject);
procedure InstructionFailed(AnInstruction: TObject);
procedure QueueInstruction(AnInstruction: TLldbInstruction);
procedure SetDebuggerState(const AValue: TDBGState);
property Debugger: TLldbDebugger read FOwner;
property CommandQueue: TLldbDebuggerCommandQueue read GetCommandQueue;
property InstructionQueue: TLldbInstructionQueue read GetInstructionQueue;
property DebuggerState: TDBGState read GetDebuggerState;
public
constructor Create(AOwner: TLldbDebugger);
destructor Destroy; override;
procedure Execute;
procedure Cancel;
end;
{ TLldbDebuggerCommandInit }
TLldbDebuggerCommandInit = class(TLldbDebuggerCommand)
protected
procedure DoExecute; override;
end;
{ TLldbDebuggerCommandRun }
TLldbDebuggerCommandRun = class(TLldbDebuggerCommand)
private type
TExceptionInfoCommand = (exiReg0, exiReg2, exiClass, exiMsg);
TExceptionInfoCommands = set of TExceptionInfoCommand;
private
FMode: (cmRun, cmRunToCatch, cmRunAfterCatch, cmRunToTmpBrk);
FState: (crRunning, crReadingThreads, crStopped, crStoppedRaise, crDone);
FNextStepAction: TLldbInstructionProcessStepAction;
FWaitToResume: Boolean;
FCurBrkId, FTmpBreakId: Integer;
FUnknownStopReason: String;
FThreadInstr: TLldbInstructionThreadList;
FCurrentExceptionInfo: record
FHasCommandData: TExceptionInfoCommands; // cleared in Setstate
FObjAddress, FFramePtr: TDBGPtr;
FExceptClass: String;
FExceptMsg: String;
end;
FFramePtrAtStart: TDBGPtr;
FFramesDescending: Boolean;
procedure ThreadInstructionSucceeded(Sender: TObject);
procedure ExceptionReadReg0Success(Sender: TObject);
procedure ExceptionReadReg2Success(Sender: TObject);
procedure ExceptionReadClassSuccess(Sender: TObject);
procedure ExceptionReadMsgSuccess(Sender: TObject);
procedure CatchesStackInstructionFinished(Sender: TObject);
procedure SearchFpStackInstructionFinished(Sender: TObject);
procedure SearchExceptFpStackInstructionFinished(Sender: TObject);
procedure TempBreakPointSet(Sender: TObject);
procedure RunInstructionSucceeded(AnInstruction: TObject);
procedure ResetStateToRun;
procedure SetNextStepCommand(AStepAction: TLldbInstructionProcessStepAction);
procedure ResumeWithNextStepCommand;
procedure SetTempBreakPoint(AnAddr: TDBGPtr);
procedure DeleteTempBreakPoint;
Procedure SetDebuggerLocation(AnAddr, AFrame: TDBGPtr; AFuncName, AFile, AFullFile: String; SrcLine: integer);
protected
FStepAction: TLldbInstructionProcessStepAction;
procedure DoLineDataReceived(var ALine: String); override;
procedure DoCancel; override;
procedure DoInitialExecute; virtual; abstract;
procedure DoExecute; override;
public
constructor Create(AOwner: TLldbDebugger);
destructor Destroy; override;
end;
{ TLldbDebuggerCommandRunStep }
TLldbDebuggerCommandRunStep = class(TLldbDebuggerCommandRun)
private
protected
procedure DoInitialExecute; override;
public
constructor Create(AOwner: TLldbDebugger; AStepAction: TLldbInstructionProcessStepAction);
end;
{ TLldbDebuggerCommandRunLaunch }
TLldbDebuggerCommandRunLaunch = class(TLldbDebuggerCommandRun)
private
FRunInstr: TLldbInstruction;
procedure ExceptBreakInstructionFinished(Sender: TObject);
procedure TargetCreated(Sender: TObject);
protected
procedure DoInitialExecute; override;
constructor Create(AOwner: TLldbDebugger);
end;
{ TLldbDebuggerCommandStop }
TLldbDebuggerCommandStop = class(TLldbDebuggerCommand)
private
procedure StopInstructionSucceeded(Sender: TObject);
protected
procedure DoExecute; override;
end;
{ TLldbDebuggerCommandLocals }
TLldbDebuggerCommandLocals = class(TLldbDebuggerCommand)
private
FLocals: TLocals;
FLocalsInstr: TLldbInstructionLocals;
procedure DoLocalsFreed(Sender: TObject);
procedure LocalsInstructionFinished(Sender: TObject);
protected
procedure DoExecute; override;
public
constructor Create(AOwner: TLldbDebugger; ALocals: TLocals);
destructor Destroy; override;
end;
{ TLldbDebuggerCommandEvaluate }
TLldbDebuggerCommandEvaluate = class(TLldbDebuggerCommand)
private
FInstr: TLldbInstructionExpression;
FWatchValue: TWatchValue;
FExpr: String;
FFlags: TDBGEvaluateFlags;
FCallback: TDBGEvaluateResultCallback;
procedure DoWatchFreed(Sender: TObject);
procedure EvalInstructionFailed(Sender: TObject);
procedure EvalInstructionSucceeded(Sender: TObject);
protected
procedure DoExecute; override;
public
// TODO: Pass FCurrentStackFrame to create
constructor Create(AOwner: TLldbDebugger; AWatchValue: TWatchValue);
constructor Create(AOwner: TLldbDebugger; AnExpr: String; AFlags: TDBGEvaluateFlags;
ACallback: TDBGEvaluateResultCallback);
destructor Destroy; override;
end;
{ TlldbInternalBreakPoint }
TlldbInternalBreakPoint = class
private
FName: String;
FBeforePrologue: Boolean;
FId: Integer;
FDebugger: TLldbDebugger;
FOnFail: TNotifyEvent;
FOnFinish: TNotifyEvent;
procedure BreakSetSuccess(Sender: TObject);
procedure DoFailed(Sender: TObject);
procedure DoFinshed(Sender: TObject);
procedure QueueInstruction(AnInstr: TLldbInstruction);
public
constructor Create(AName: String; ADebugger: TLldbDebugger; ABeforePrologue: Boolean = False);
destructor Destroy; override;
procedure Enable;
procedure Disable;
procedure Remove;
property BreakId: Integer read FId;
property OnFail: TNotifyEvent read FOnFail write FOnFail;
property OnFinish: TNotifyEvent read FOnFinish write FOnFinish;
end;
{ TLldbDebuggerCommandRegister }
TLldbDebuggerCommandRegister = class(TLldbDebuggerCommand)
private
FRegisters: TRegisters;
procedure RegisterInstructionFinished(Sender: TObject);
protected
procedure DoExecute; override;
public
constructor Create(AOwner: TLldbDebugger; ARegisters: TRegisters);
destructor Destroy; override;
property Registers: TRegisters read FRegisters;
end;
(*
* Debugger
*)
{ TLldbDebugger }
TLldbDebugger = class(TDebuggerIntf)
private
FDebugProcess: TDebugProcess;
FDebugInstructionQueue: TLldbInstructionQueue;
FCommandQueue: TLldbDebuggerCommandQueue;
FInIdle: Boolean;
FCurrentLocation: TDBGLocationRec;
FCurrentStackFrame: Integer;
FCurrentThreadId: Integer;
FCurrentThreadFramePtr: TDBGPtr;
FBreakErrorBreak: TlldbInternalBreakPoint;
FRunErrorBreak: TlldbInternalBreakPoint;
FExceptionBreak: TlldbInternalBreakPoint;
FPopExceptStack, FCatchesBreak, FReRaiseBreak: TlldbInternalBreakPoint;
FTargetWidth: Byte;
FTargetRegisters: array[0..2] of String;
FLldbMissingBreakSetDisable: Boolean;
FExceptionInfo: record
FReg0Cmd, FReg2Cmd, FExceptClassCmd, FExceptMsgCmd: String;
FAtExcepiton: Boolean; // cleared in Setstate
end;
(* DoAfterLineReceived is called after DebugInstruction.ProcessInputFromDbg
(but not if ProcessInputFromDbg already handled the Line)
DoAfterLineReceived will first call CommandQueue.DoLineDataReceived
*)
procedure DoAfterLineReceived(var ALine: String); //
procedure DoBeforeLineReceived(var ALine: String); // Before DebugInstruction.ProcessInputFromDbg
procedure DoCmdLineDebuggerTerminated(Sender: TObject);
procedure DoLineSentToDbg(Sender: TObject; ALine: String);
function LldbRun: Boolean;
function LldbStep(AStepAction: TLldbInstructionProcessStepAction): Boolean;
function LldbStop: Boolean;
function LldbPause: Boolean;
function LldbEvaluate(const AExpression: String; EvalFlags: TDBGEvaluateFlags; ACallback: TDBGEvaluateResultCallback): Boolean;
function LldbEnvironment(const AVariable: String; const ASet: Boolean): Boolean;
protected
procedure DoBeforeLaunch; virtual;
procedure DoBeginReceivingLines(Sender: TObject);
procedure DoEndReceivingLines(Sender: TObject);
procedure LockRelease; override;
procedure UnlockRelease; override;
procedure QueueCommand(const ACommand: TLldbDebuggerCommand);
//procedure DoState(const OldState: TDBGState); override;
//procedure DoBeforeState(const OldState: TDBGState); override;
function DoExceptionHit(AExcClass, AExcMsg: String): Boolean;
function DoBreakpointHit(BrkId: Integer): Boolean;
property CurrentThreadId: Integer read FCurrentThreadId;
property CurrentStackFrame: Integer read FCurrentStackFrame;
property CurrentLocation: TDBGLocationRec read FCurrentLocation;
property DebugInstructionQueue: TLldbInstructionQueue read FDebugInstructionQueue;
property CommandQueue: TLldbDebuggerCommandQueue read FCommandQueue;
protected
function CreateBreakPoints: TDBGBreakPoints; override;
function CreateLocals: TLocalsSupplier; override;
//function CreateLineInfo: TDBGLineInfo; override;
function CreateRegisters: TRegisterSupplier; override;
function CreateCallStack: TCallStackSupplier; override;
//function CreateDisassembler: TDBGDisassembler; override;
function CreateWatches: TWatchesSupplier; override;
function CreateThreads: TThreadsSupplier; override;
function GetTargetWidth: Byte; override;
function GetIsIdle: Boolean; override;
function GetSupportedCommands: TDBGCommands; override;
//function GetCommands: TDBGCommands; override;
function RequestCommand(const ACommand: TDBGCommand;
const AParams: array of const;
const ACallback: TMethod): Boolean; override;
public
// class function CreateProperties: TDebuggerProperties; override; // Creates debuggerproperties
class function Caption: String; override;
// class function ExePaths: String; override;
constructor Create(const AExternalDebugger: String); override;
destructor Destroy; override;
procedure Init; override; // Initializes external debugger
procedure Done; override; // Kills external debugger
class function RequiredCompilerOpts(ATargetCPU, ATargetOS: String
): TDebugCompilerRequirements; override;
function GetLocation: TDBGLocationRec; override;
// function GetProcessList({%H-}AList: TRunningProcessInfoList): boolean; override;
function NeedReset: Boolean; override;
procedure TestCmd(const ACommand: String); override;
end;
procedure Register;
implementation
var
DBG_VERBOSE: PLazLoggerLogGroup;
type
{%region
*****
***** Threads
***** }
{ TLldbDebuggerCommandThreads }
TLldbDebuggerCommandThreads = class(TLldbDebuggerCommand)
private
procedure ThreadInstructionSucceeded(Sender: TObject);
protected
procedure DoExecute; override;
end;
{ TLldbThreads }
TLldbThreads = class(TThreadsSupplier)
private
FThreadFramePointers: Array of TDBGPtr;
function GetDebugger: TLldbDebugger;
protected
procedure DoStateEnterPause; override;
procedure ReadFromThreadInstruction(Instr: TLldbInstructionThreadList);
public
procedure RequestMasterData; override;
procedure ChangeCurrentThread(ANewId: Integer); override;
function GetFramePointerForThread(AnId: Integer): TDBGPtr;
property Debugger: TLldbDebugger read GetDebugger;
end;
{%endregion ^^^^^ Threads ^^^^^ }
{%region
*****
***** CallStack
***** }
{ TLldbDebuggerCommandCallStack }
TLldbDebuggerCommandCallStack = class(TLldbDebuggerCommand)
private
FCurrentCallStack: TCallStackBase;
procedure DoCallstackFreed(Sender: TObject);
procedure StackInstructionFinished(Sender: TObject);
protected
procedure DoExecute; override;
public
constructor Create(AOwner: TLldbDebugger; ACurrentCallStack: TCallStackBase);
destructor Destroy; override;
property CurrentCallStack: TCallStackBase read FCurrentCallStack;
end;
{ TLldbCallStack }
TLldbCallStack = class(TCallStackSupplier)
protected
//procedure Clear;
//procedure DoThreadChanged;
procedure ParentRequestEntries(ACallstack: TCallStackBase);
public
procedure RequestAtLeastCount(ACallstack: TCallStackBase;
ARequiredMinCount: Integer); override;
procedure UpdateCurrentIndex; override;
procedure RequestCurrent(ACallstack: TCallStackBase); override;
procedure RequestEntries(ACallstack: TCallStackBase); override;
end;
{%endregion ^^^^^ CallStack ^^^^^ }
{%region
*****
***** Locals
***** }
{ TLldbLocals }
TLldbLocals = class(TLocalsSupplier)
public
procedure RequestData(ALocals: TLocals); override;
end;
{%endregion ^^^^^ Locals ^^^^^ }
{%region
*****
***** Watches
***** }
{ TLldbWatches }
TLldbWatches = class(TWatchesSupplier)
private
protected
procedure InternalRequestData(AWatchValue: TWatchValue); override;
public
end;
{%endregion ^^^^^ Watches ^^^^^ }
{%region
*****
***** BreakPoint
***** }
{ TLldbBreakPoint }
TLldbBreakPoint = class(TDBGBreakPoint)
private
FBreakID: Integer;
FCurrentInstruction: TLldbInstruction;
FNeededChanges: TDbgBpChangeIndicators;
procedure InstructionSetBreakFinished(Sender: TObject);
procedure InstructionUpdateBreakFinished(Sender: TObject);
procedure SetBreakPoint;
procedure ReleaseBreakPoint;
procedure UpdateProperties(AChanged: TDbgBpChangeIndicators);
procedure DoCurrentInstructionFinished;
procedure CancelCurrentInstruction;
protected
procedure DoStateChange(const AOldState: TDBGState); override;
procedure DoPropertiesChanged(AChanged: TDbgBpChangeIndicators); override;
public
// constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
// procedure DoLogExpression(const AnExpression: String); override;
end;
{ TLldbBreakPoints }
TLldbBreakPoints = class(TDBGBreakPoints)
protected
function FindById(AnId: Integer): TLldbBreakPoint;
end;
{%endregion ^^^^^ BreakPoint ^^^^^ }
{%region
*****
***** Register
***** }
{ TLldbRegisterSupplier }
TLldbRegisterSupplier = class(TRegisterSupplier)
public
procedure Changed;
procedure RequestData(ARegisters: TRegisters); override;
end;
{ TLldbDebuggerCommandRun }
procedure TLldbDebuggerCommandRun.CatchesStackInstructionFinished(Sender: TObject);
var
Instr: TLldbInstruction;
r: TStringArray;
Id, line: Integer;
IsCur: Boolean;
addr, stack, frame: TDBGPtr;
func, filename, fullfile, d: String;
Arguments: TStringList;
begin
r := TLldbInstructionStackTrace(Sender).Res;
if Length(r) < 1 then begin
SetDebuggerState(dsPause);
Finished;
exit;
end;
ParseNewFrameLocation(r[0], Id, IsCur, addr, stack, frame, func, Arguments, filename, fullfile, line, d);
Arguments.Free;
if addr = 0 then begin
SetDebuggerState(dsPause);
Finished;
exit;
end;
if FMode = cmRunToTmpBrk then begin
if (FFramesDescending and (frame > FFramePtrAtStart)) or
((not FFramesDescending) and (frame < FFramePtrAtStart))
then begin
ResetStateToRun;
SetNextStepCommand(saContinue);
exit;
end;
DeleteTempBreakPoint; // except stepped out below temp brkpoint
end;
SetTempBreakPoint(Addr);
ResetStateToRun;
FMode := cmRunAfterCatch;
SetNextStepCommand(saContinue);
end;
procedure TLldbDebuggerCommandRun.TempBreakPointSet(Sender: TObject);
begin
FTmpBreakId := TLldbInstructionBreakSet(Sender).BreakId;
end;
procedure TLldbDebuggerCommandRun.SearchFpStackInstructionFinished(
Sender: TObject);
var
r: TStringArray;
fr: Integer;
Id, line: Integer;
IsCur: Boolean;
addr, stack, frame, prev: TDBGPtr;
func, filename, fullfile, d: String;
Arguments: TStringList;
begin
r := TLldbInstructionStackTrace(Sender).Res;
if Length(r) < 1 then begin
SetDebuggerState(dsPause);
Finished;
exit;
end;
fr := 0;
prev := 0;
repeat
ParseNewFrameLocation(r[fr], Id, IsCur, addr, stack, frame, func, Arguments, filename, fullfile, line, d);
Arguments.Free;
if fr = 0 then begin
FFramesDescending := frame > FFramePtrAtStart;
if (FState = crStoppedRaise) and (Length(r) >= 2) then begin
inc(fr);
Continue;
end;
end;
if not( (frame = 0) or ((fr > 0) and (frame = {%H-}prev)) ) then begin
if frame = FFramePtrAtStart then
break;
if (prev <> 0) and (
( (fr < prev) and not(FFramePtrAtStart < fr) ) or
( (fr > prev) and not(FFramePtrAtStart > fr) )
)
then begin
SetDebuggerState(dsPause);
Finished;
exit;
end;
prev := frame;
end;
inc(fr);
until fr >= Length(r);
if (fr >= Length(r)) or (addr = 0) then begin
SetDebuggerState(dsPause);
Finished;
exit;
end;
SetTempBreakPoint(Addr);
ResetStateToRun;
FMode := cmRunToTmpBrk;
SetNextStepCommand(saContinue);
end;
procedure TLldbDebuggerCommandRun.SearchExceptFpStackInstructionFinished(
Sender: TObject);
var
r: TStringArray;
fr: Integer;
Id, line: Integer;
IsCur: Boolean;
addr, stack, frame: TDBGPtr;
func, filename, fullfile, d: String;
Arguments: TStringList;
begin
r := TLldbInstructionStackTrace(Sender).Res;
if Length(r) < 2 then begin
SetDebuggerState(dsPause);
Finished;
exit;
end;
fr := 1;
repeat
ParseNewFrameLocation(r[fr], Id, IsCur, addr, stack, frame, func, Arguments, filename, fullfile, line, d);
Arguments.Free;
if frame = FCurrentExceptionInfo.FFramePtr then begin
SetDebuggerLocation(Addr, Frame, Func, filename, FullFile, Line);
break;
end;
inc(fr);
until fr >= Length(r);
SetDebuggerState(dsPause);
Debugger.DoCurrent(Debugger.FCurrentLocation);
Finished;
end;
procedure TLldbDebuggerCommandRun.ThreadInstructionSucceeded(Sender: TObject);
begin
FState := crStopped;
end;
procedure TLldbDebuggerCommandRun.ExceptionReadReg0Success(Sender: TObject);
var
v: String;
i: SizeInt;
begin
v := TLldbInstructionReadExpression(Sender).Res;
i := pos(' = ', v);
if i > 1 then
delete(v, 1, i+2);
FCurrentExceptionInfo.FObjAddress := StrToInt64Def(v, 0);
Include(FCurrentExceptionInfo.FHasCommandData, exiReg0);
end;
procedure TLldbDebuggerCommandRun.ExceptionReadReg2Success(Sender: TObject);
var
v: String;
i: SizeInt;
begin
v := TLldbInstructionReadExpression(Sender).Res;
i := pos(' = ', v);
if i > 1 then
delete(v, 1, i+2);
FCurrentExceptionInfo.FFramePtr := StrToInt64Def(v, 0);
Include(FCurrentExceptionInfo.FHasCommandData, exiReg2);
end;
procedure TLldbDebuggerCommandRun.ExceptionReadClassSuccess(Sender: TObject);
var
s: String;
i: SizeInt;
l: Integer;
begin
// (char * ) $2 = 0x005c18d0 "\tException"
// (char *) $10 = 0x00652d44 "\x04TXXX"
s := TLldbInstructionReadExpression(Sender).Res;
i := pos('"', s);
l := 255;
if i > 0 then begin
if s[i+1] = '\' then begin
inc(i, 2);
if s[i] = 'x' then begin
l := StrToIntDef('$'+copy(s, i+1, 2), 255);
inc(i, 2);
end
else begin
case s[i] of
'a': l := 7;
'b': l := 8;
't': l := 9;
'n': l := 10;
'v': l := 11;
'f': l := 12;
'r': l := 13;
'e': l := 27;
's': l := 32;
'\': l := 92;
'd': l := 127;
end;
end;
end
else begin
inc(i, 1);
l := ord(s[i]);
end;
s := copy(s, i+1, Min(l, Length(s)-i-1));
end;
FCurrentExceptionInfo.FExceptClass := s;
Include(FCurrentExceptionInfo.FHasCommandData, exiClass);
end;
procedure TLldbDebuggerCommandRun.ExceptionReadMsgSuccess(Sender: TObject);
var
s: String;
i: SizeInt;
begin
s := TLldbInstructionReadExpression(Sender).Res;
i := pos('"', s);
if i > 0 then
s := copy(s, i+1, Length(s)-i-1);
FCurrentExceptionInfo.FExceptMsg := s;
Include(FCurrentExceptionInfo.FHasCommandData, exiMsg);
end;
procedure TLldbDebuggerCommandRun.DoLineDataReceived(var ALine: String);
const
MaxStackSearch = 99;
procedure ContinueRunning;
var
Instr: TLldbInstruction;
begin
if FStepAction = saContinue then begin
DeleteTempBreakPoint;
ResetStateToRun;
FMode := cmRun;
SetNextStepCommand(saContinue);
exit;
end;
if FFramePtrAtStart = 0 then begin
SetDebuggerState(dsPause);
Finished;
exit;
end;
if FTmpBreakId = 0 then begin
FMode := cmRunToTmpBrk;
FState := crRunning; // Ignore the STEP 3 / frame
Instr := TLldbInstructionStackTrace.Create(MaxStackSearch, 0, Debugger.FCurrentThreadId);
Instr.OnFinish := @SearchFpStackInstructionFinished;
QueueInstruction(Instr);
Instr.ReleaseReference;
exit;
end;
ResetStateToRun;
FMode := cmRunToTmpBrk;
SetNextStepCommand(saContinue);
//case FStepAction of
// saInsIn: SetDebuggerState(dsPause);
// saInsOver, saOver: ;
// saInto: ;
// saOut: ;
//end;
end;
function GetBreakPointId(AReason: String): Integer;
var
i: Integer;
begin
i := pos('.', AReason);
if i = 0 then i := Length(AReason)+1;
Result := StrToIntDef(copy(AReason, 12, i-12), -1);
debugln(['DoBreakPointHit ', AReason, ' / ', Result]);
end;
procedure DoException;
var
ExcClass, ExcMsg: String;
CanContinue: Boolean;
Instr: TLldbInstructionStackTrace;
ExceptItem: TBaseException;
begin
if exiClass in FCurrentExceptionInfo.FHasCommandData then
ExcClass := FCurrentExceptionInfo.FExceptClass
else
ExcClass := '<Unknown Class>'; // TODO: move to IDE
if exiMsg in FCurrentExceptionInfo.FHasCommandData then
ExcMsg := FCurrentExceptionInfo.FExceptMsg
else
ExcMsg := '<Unknown Message>'; // TODO: move to IDE
ExceptItem := Debugger.Exceptions.Find(ExcClass);
if (ExceptItem <> nil) and (ExceptItem.Enabled)
then begin
FState := crStoppedRaise;
ContinueRunning;
exit;
end;
CanContinue := Debugger.DoExceptionHit(ExcClass, ExcMsg);
if CanContinue then begin
FState := crStoppedRaise;
ContinueRunning;
exit;
end
else begin
Debugger.FExceptionInfo.FAtExcepiton := True;
if (exiReg2 in FCurrentExceptionInfo.FHasCommandData) and (FCurrentExceptionInfo.FFramePtr <> 0) then begin
FState := crRunning; // ensure command is not finished early
Instr := TLldbInstructionStackTrace.Create(MaxStackSearch, 0, Debugger.FCurrentThreadId);
Instr.OnFinish := @SearchExceptFpStackInstructionFinished;
QueueInstruction(Instr);
Instr.ReleaseReference;
exit;
end;
Debugger.FCurrentLocation.SrcLine := -1;
SetDebuggerState(dsPause); // after GetLocation => dsPause may run stack, watches etc
end;
end;
procedure DoRunError;
var
CanContinue: Boolean;
ErrNo: Integer;
ExceptName: String;
ExceptItem: TBaseException;
begin
ErrNo := 0;
if exiReg0 in FCurrentExceptionInfo.FHasCommandData then
ErrNo := FCurrentExceptionInfo.FObjAddress;
ErrNo := ErrNo and $FFFF;
ExceptName := Format('RunError(%d)', [ErrNo]);
ExceptItem := Debugger.Exceptions.Find(ExceptName);
if (ExceptItem <> nil) and (ExceptItem.Enabled)
then begin
FState := crStoppedRaise;
ContinueRunning;
exit;
end;
Debugger.DoException(deRunError, ExceptName, Debugger.FCurrentLocation, '', CanContinue);
if CanContinue
then begin
FState := crStoppedRaise;
ContinueRunning;
exit;
end;
SetDebuggerState(dsPause); // after GetLocation => dsPause may run stack, watches etc
end;
procedure DoUnknownStopReason(AStopReason: String);
var
CanContinue: Boolean;
begin
Debugger.DoException(deExternal, Format('Debugger stopped with reason: %s', [AStopReason]), Debugger.FCurrentLocation, '', CanContinue);
SetDebuggerState(dsPause); // after GetLocation => dsPause may run stack, watches etc
end;
procedure DoCatchesHit;
var
Instr: TLldbInstruction;
begin
FState := crRunning; // Ignore the STEP 3 / frame
Instr := TLldbInstructionStackTrace.Create(1, 1, Debugger.FCurrentThreadId);
Instr.OnFinish := @CatchesStackInstructionFinished;
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
procedure DoStopTemp;
begin
if (FMode in [cmRunAfterCatch, cmRunToTmpBrk]) and (Debugger.FCurrentLocation.SrcLine = 0) then begin
DeleteTempBreakPoint;
ResetStateToRun;
FMode := cmRun;
SetNextStepCommand(saOver);
exit;
end;
SetDebuggerState(dsPause);
end;
procedure DoBreakPointHit(BrkId: Integer);
var
BreakPoint: TLldbBreakPoint;
CanContinue: Boolean;
begin
CanContinue := Debugger.DoBreakpointHit(BrkId);
if CanContinue
then begin
// Important trigger State => as snapshot is taken in TDebugManager.DebuggerChangeState
SetDebuggerState(dsInternalPause); // TODO: need location?
ContinueRunning;
end
else begin
SetDebuggerState(dsPause);
end;
end;
var
Instr: TLldbInstruction;
found: TStringArray;
AnId, SrcLine, i: Integer;
AnIsCurrent: Boolean;
AnAddr, stack, frame: TDBGPtr;
AFuncName, AFile, AReminder, AFullFile, s, Name: String;
AnArgs: TStringList;
begin
(* When the debuggee stops (pause), the following will be received:
// for EXCEPTIONS ONLY (less the spaces between * ) )
p/x $eax
(unsigned int) $1 = 0x04dfd920
p ((char *** )$eax)[0][3]
(char * ) $2 = 0x005c18d0 "\tException"
p ((char ** )$eax)[1]
(char * ) $3 = 0x00000000 <no value available>
// Hit breakpoint
Process 10992 stopped
* thread #1: tid=0x1644: 0x00409e91, 0x0158FF38, 0x0158FF50 &&//FULL: \FPC\SVN\fixes_3_0\rtl\win32\..\inc\except.inc &&//SHORT: except.inc &&//LINE: 185 &&//MOD: project1.exe &&//FUNC: fpc_raiseexception(OBJ=0x038f5a90, ANADDR=0x00401601, AFRAME=0x0158ff58) <<&&//FRAME"
thread #2: tid=0x27bc: 0x77abf8dc, 0x0557FE64, 0x0557FEC8 &&//FULL: &&//SHORT: &&//LINE: &&//MOD: ntdll.dll &&//FUNC: NtDelayExecution <<&&//FRAME"
Process 10992 stopped
* thread #1, stop reason = breakpoint 6.1
frame #0: 0x0042b855 &&//FULL: \tmp\New Folder (2)\unit1.pas &&//SHORT: unit1.pas &&//LINE: 54 &&//MOD: project1.exe &&//FUNC: FORMCREATE(this=0x04c81248, SENDER=0x04c81248) <<&&//FRAME
... stop reason = Exception 0xc0000005 encountered at address 0x42e067
... stop reason = EXC_BAD_ACCESS (code=1, address=0x4)
... stop reason = step over
... stop reason = step in
... stop reason = instruction step over
... google stop reason = signal / trace / watchpoint
*)
{%region exception }
s := TrimLeft(ALine);
Instr := nil;
if StrStartsWith(s, Debugger.FExceptionInfo.FReg0Cmd, True) then begin
Instr := TLldbInstructionReadExpression.Create;
Instr.OnSuccess := @ExceptionReadReg0Success;
end
else
if StrStartsWith(s, Debugger.FExceptionInfo.FReg2Cmd, True) then begin
Instr := TLldbInstructionReadExpression.Create;
Instr.OnSuccess := @ExceptionReadReg2Success;
end
else
if StrStartsWith(s, Debugger.FExceptionInfo.FExceptClassCmd, True) then begin
Instr := TLldbInstructionReadExpression.Create;
Instr.OnSuccess := @ExceptionReadClassSuccess;
end
else
if StrStartsWith(s, Debugger.FExceptionInfo.FExceptMsgCmd, True) then begin
Instr := TLldbInstructionReadExpression.Create;
Instr.OnSuccess := @ExceptionReadMsgSuccess;
end;
if Instr <> nil then begin
ALine := '';
debugln(['Reading exception info']);
assert(InstructionQueue.RunningInstruction = nil, 'InstructionQueue.RunningInstruction = nil');
QueueInstruction(Instr);
Instr.ReleaseReference;
exit;
end;
{%endregion exception }
// STEP 1: Process 10992 stopped
if (FState = crRunning) and StrMatches(ALine, ['Process ', 'stopped']) then begin
FState := crReadingThreads;
debugln(['Reading thread info']);
FThreadInstr := TLldbInstructionThreadListReader.Create();
FThreadInstr.OnSuccess := @ThreadInstructionSucceeded;
QueueInstruction(FThreadInstr);
exit;
end;
// STEP 2: * thread #1, stop reason = breakpoint 6.1
if StrMatches(ALine, ['* thread #', ', stop reason = ', ''], found) then begin
FState := crStopped;
debugln(['Reading stopped thread']);
SetDebuggerLocation(0, 0, '', '', '', 0);
if StrStartsWith(found[1], 'breakpoint ') then begin
FCurBrkId := GetBreakPointId(found[1])
//end else
//if StrStartsWith(found[1], 'watchpoint ') then begin
end else begin
FUnknownStopReason := '';
if not( ( StrStartsWith(found[1], 'step ') or StrStartsWith(found[1], 'instruction step ') ) and
( StrContains(found[1], ' in') or StrContains(found[1], ' over') or StrContains(found[1], ' out') )
)
then
FUnknownStopReason := found[1];
FCurBrkId := -1;
end;
ParseNewThreadLocation(ALine, AnId, AnIsCurrent, Name, AnAddr,
Stack, Frame, AFuncName, AnArgs, AFile, AFullFile, SrcLine, AReminder);
AnArgs.Free;
Debugger.FCurrentThreadId := AnId;
Debugger.FCurrentStackFrame := 0;
SetDebuggerLocation(AnAddr, Frame, AFuncName, AFile, AFullFile, SrcLine);
InstructionQueue.SetKnownThreadAndFrame(Debugger.FCurrentThreadId, 0);
Debugger.Threads.CurrentThreads.CurrentThreadId := Debugger.FCurrentThreadId; // set again from thread list
if StrStartsWith(found[1], 'breakpoint ') then begin
if FCurBrkId = Debugger.FExceptionBreak.BreakId then
DoException
else
if FCurBrkId = Debugger.FRunErrorBreak.BreakId then
DoRunError // location = frame with fp // see gdbmi
else
if FCurBrkId = Debugger.FBreakErrorBreak.BreakId then
DoRunError // location = frame(1) // see gdbmi
else
if (FCurBrkId = Debugger.FCatchesBreak.BreakId) or
(FCurBrkId = Debugger.FPopExceptStack.BreakId)
then
DoCatchesHit
else
if FCurBrkId = Debugger.FReRaiseBreak.BreakId then begin
FState := crStoppedRaise;
ContinueRunning;
end
else
if FCurBrkId = FTmpBreakId then
DoStopTemp
else
DoBreakPointHit(FCurBrkId);
end
else
if FUnknownStopReason <> '' then begin
DoUnknownStopReason(FUnknownStopReason);
end
else
SetDebuggerState(dsPause);
if (FState = crRunning) then
exit;
if DebuggerState in [dsPause, dsInternalPause, dsStop] then
Debugger.DoCurrent(Debugger.FCurrentLocation);
FState := crDone;
ALine := '';
exit;
end;
if (FState = crRunning) then
exit;
// STEP 3: frame #0: 0x0042b855 &&//FULL: \tmp\New Folder (2)\unit1.pas &&//SHORT: unit1.pas &&//LINE: 54 &&//MOD: project1.exe &&//FUNC: FORMCREATE(this=0x04c81248, SENDER=0x04c81248) <<&&//FRAME
if ParseNewFrameLocation(ALine, AnId, AnIsCurrent, AnAddr, stack, frame, AFuncName, AnArgs,
AFile, AFullFile, SrcLine, AReminder)
then begin
AnArgs.Free;
if FState = crReadingThreads then begin
FState := crStopped;
// did not execute "thread list" / thread cmd reader has read "stop reason"
for i := 0 to length(FThreadInstr.Res) - 1 do
DoLineDataReceived(FThreadInstr.Res[i]);
end;
DeleteTempBreakPoint;
Finished;
end;
if (LeftStr(ALine, 8) = 'Process ') and (pos('exited with status = ', ALine) > 0) then begin
DeleteTempBreakPoint;
Finished;
exit; // handle in main debugger
end;
end;
procedure TLldbDebuggerCommandRun.DoCancel;
begin
InstructionQueue.CancelAllForCommand(Self); // in case there still are any
DeleteTempBreakPoint;
// Must not call Finished; => would cancel DeleteTempBreakPoint;
if CommandQueue.RunningCommand = Self then
CommandQueue.CommandFinished(Self);
end;
procedure TLldbDebuggerCommandRun.DoExecute;
begin
if FWaitToResume then
ResumeWithNextStepCommand
else
DoInitialExecute;
end;
procedure TLldbDebuggerCommandRun.RunInstructionSucceeded(AnInstruction: TObject
);
begin
FCurrentExceptionInfo.FHasCommandData := [];
end;
procedure TLldbDebuggerCommandRun.ResetStateToRun;
begin
FState := crRunning;
FCurBrkId := 0;
if FThreadInstr <> nil then begin
FThreadInstr.ReleaseReference;
FThreadInstr := nil;
end;
FCurrentExceptionInfo.FHasCommandData := [];
end;
procedure TLldbDebuggerCommandRun.SetNextStepCommand(
AStepAction: TLldbInstructionProcessStepAction);
begin
FNextStepAction := AStepAction;
{$IFDEF LLDB_SKIP_SNAP}
ResumeWithNextStepCommand;
exit;
{$ENDIF}
FWaitToResume := True;
// Run the queue, before continue
CommandQueue.QueueCommand(Self);
CommandQueue.CommandFinished(Self);
//CommandQueue.FRunningCommand := nil;
//CommandQueue.Run;
end;
procedure TLldbDebuggerCommandRun.ResumeWithNextStepCommand;
var
Instr: TLldbInstructionProcessStep;
begin
if FNextStepAction in [saOver, saInto, saOut, saInsOver] then
Debugger.FReRaiseBreak.Enable
else
Debugger.FReRaiseBreak.Disable;
if FMode in [cmRunToCatch, cmRunToTmpBrk] then begin
Debugger.FCatchesBreak.Enable;
Debugger.FPopExceptStack.Enable;
Instr := TLldbInstructionProcessStep.Create(saContinue);
end
else begin
Debugger.FCatchesBreak.Disable;
Debugger.FPopExceptStack.Disable;
Instr := TLldbInstructionProcessStep.Create(FNextStepAction);
end;
Instr.OnFinish := @RunInstructionSucceeded;
QueueInstruction(Instr);
Instr.ReleaseReference;
if DebuggerState <> dsRun then
SetDebuggerState(dsRun);
end;
procedure TLldbDebuggerCommandRun.SetTempBreakPoint(AnAddr: TDBGPtr);
var
Instr: TLldbInstructionBreakSet;
begin
Instr := TLldbInstructionBreakSet.Create(AnAddr);
Instr.OnFinish := @TempBreakPointSet;
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
procedure TLldbDebuggerCommandRun.DeleteTempBreakPoint;
var
Instr: TLldbInstruction;
begin
if FTmpBreakId = 0 then
exit;
Instr := TLldbInstructionBreakDelete.Create(FTmpBreakId);
QueueInstruction(Instr);
Instr.ReleaseReference;
FTmpBreakId := 0;
end;
procedure TLldbDebuggerCommandRun.SetDebuggerLocation(AnAddr, AFrame: TDBGPtr;
AFuncName, AFile, AFullFile: String; SrcLine: integer);
begin
Debugger.FCurrentThreadFramePtr := AFrame;
Debugger.FCurrentLocation.Address := AnAddr;
Debugger.FCurrentLocation.FuncName := AFuncName;
Debugger.FCurrentLocation.SrcFile := AFile;
Debugger.FCurrentLocation.SrcFullName := AFullFile;
Debugger.FCurrentLocation.SrcLine := SrcLine;
end;
constructor TLldbDebuggerCommandRun.Create(AOwner: TLldbDebugger);
begin
AOwner.FExceptionInfo.FAtExcepiton := False;
FState := crRunning;
FMode := cmRun;
FFramePtrAtStart := AOwner.FCurrentThreadFramePtr;
inherited Create(AOwner);
end;
destructor TLldbDebuggerCommandRun.Destroy;
begin
FThreadInstr.ReleaseReference;
inherited Destroy;
end;
{ TLldbDebuggerCommandLocals }
procedure TLldbDebuggerCommandLocals.LocalsInstructionFinished(Sender: TObject
);
var
n: String;
i: Integer;
begin
if FLocals <> nil then begin
FLocals.Clear;
for i := 0 to FLocalsInstr.Res.Count - 1 do begin
n := FLocalsInstr.Res.Names[i];
FLocals.Add(n, FLocalsInstr.Res.Values[n]);
end;
FLocals.SetDataValidity(ddsValid);
end;
ReleaseRefAndNil(FLocalsInstr);
Finished;
end;
procedure TLldbDebuggerCommandLocals.DoLocalsFreed(Sender: TObject);
begin
FLocals := nil;
if FLocalsInstr <> nil then begin
FLocalsInstr.OnFinish := nil;
FLocalsInstr.Cancel;
ReleaseRefAndNil(FLocalsInstr);
Finished;
end;
end;
procedure TLldbDebuggerCommandLocals.DoExecute;
begin
if FLocals = nil then begin
Finished;
exit;
end;
if FLocalsInstr <> nil then begin
FLocalsInstr.OnFinish := nil;
ReleaseRefAndNil(FLocalsInstr);
end;
FLocalsInstr := TLldbInstructionLocals.Create(FLocals.ThreadId, FLocals.StackFrame);
FLocalsInstr.OnFinish := @LocalsInstructionFinished;
QueueInstruction(FLocalsInstr);
end;
constructor TLldbDebuggerCommandLocals.Create(AOwner: TLldbDebugger;
ALocals: TLocals);
begin
FLocals := ALocals;
FLocals.AddFreeNotification(@DoLocalsFreed);
inherited Create(AOwner);
end;
destructor TLldbDebuggerCommandLocals.Destroy;
begin
if FLocalsInstr <> nil then begin
FLocalsInstr.OnFinish := nil;
ReleaseRefAndNil(FLocalsInstr);
end;
if FLocals <> nil then
FLocals.RemoveFreeNotification(@DoLocalsFreed);
inherited Destroy;
end;
{%endregion ^^^^^ Register ^^^^^ }
{%region
*****
***** Threads
***** }
{ TLldbDebuggerCommandThreads }
procedure TLldbDebuggerCommandThreads.ThreadInstructionSucceeded(Sender: TObject);
begin
TLldbThreads(Debugger.Threads).ReadFromThreadInstruction(TLldbInstructionThreadList(Sender));
Finished;
end;
procedure TLldbDebuggerCommandThreads.DoExecute;
var
Instr: TLldbInstructionThreadList;
begin
Instr := TLldbInstructionThreadList.Create();
Instr.OnFinish := @ThreadInstructionSucceeded;
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
{ TLldbThreads }
function TLldbThreads.GetDebugger: TLldbDebugger;
begin
Result := TLldbDebugger(inherited Debugger);
end;
procedure TLldbThreads.DoStateEnterPause;
begin
inherited DoStateEnterPause;
Changed;
end;
procedure TLldbThreads.ReadFromThreadInstruction(
Instr: TLldbInstructionThreadList);
var
i, j, line: Integer;
s, func, filename, name, d, fullfile: String;
found, foundFunc, foundArg: TStringArray;
TId, CurThrId: LongInt;
CurThr: Boolean;
Arguments: TStringList;
addr, stack, frame: TDBGPtr;
te: TThreadEntry;
begin
CurrentThreads.Clear;
SetLength(FThreadFramePointers, Length(Instr.Res));
for i := 0 to Length(Instr.Res) - 1 do begin
s := Instr.Res[i];
ParseNewThreadLocation(s, TId, CurThr, name, addr, stack, frame, func, Arguments, filename, fullfile, line, d);
FThreadFramePointers[i] := frame;
if CurThr then
CurThrId := TId;
te := CurrentThreads.CreateEntry(
addr,
Arguments,
func,
filename, fullfile,
line,
TId, name, ''
);
CurrentThreads.Add(te);
te.Free;
Arguments.Free;
end;
CurrentThreads.CurrentThreadId := CurThrId;
CurrentThreads.SetValidity(ddsValid);
end;
procedure TLldbThreads.RequestMasterData;
var
Cmd: TLldbDebuggerCommandThreads;
RunCmd: TLldbDebuggerCommand;
Instr: TLldbInstructionThreadList;
begin
if not (Debugger.State in [dsPause, dsInternalPause]) then
exit;
RunCmd := Debugger.CommandQueue.RunningCommand;
if (RunCmd <> nil) and (RunCmd is TLldbDebuggerCommandRun) then begin
Instr := TLldbDebuggerCommandRun(RunCmd).FThreadInstr;
if (Instr <> nil) and Instr.IsSuccess then begin
ReadFromThreadInstruction(TLldbInstructionThreadList(Instr));
exit;
end;
end;
Cmd := TLldbDebuggerCommandThreads.Create(Debugger);
Debugger.QueueCommand(Cmd);
Cmd.ReleaseReference;
end;
procedure TLldbThreads.ChangeCurrentThread(ANewId: Integer);
begin
if Debugger = nil then Exit;
if not(Debugger.State in [dsPause, dsInternalPause]) then exit;
Debugger.FCurrentThreadId := ANewId;
Debugger.FCurrentThreadFramePtr := GetFramePointerForThread(ANewId);
if CurrentThreads <> nil
then CurrentThreads.CurrentThreadId := ANewId;
end;
function TLldbThreads.GetFramePointerForThread(AnId: Integer): TDBGPtr;
begin
if (AnId < 0) or (AnId >= Length(FThreadFramePointers)) then
exit(0);
Result := FThreadFramePointers[AnId];
end;
{%endregion ^^^^^ Threads ^^^^^ }
{%region
*****
***** CallStack
***** }
{ TLldbDebuggerCommandCallStack }
procedure TLldbDebuggerCommandCallStack.StackInstructionFinished(Sender: TObject
);
var
Instr: TLldbInstructionStackTrace absolute Sender;
i, FId, line: Integer;
e: TCallStackEntry;
found, foundArg: TStringArray;
Arguments: TStringList;
It: TMapIterator;
s, func, filename, d, fullfile: String;
IsCur: Boolean;
addr, stack, frame: TDBGPtr;
begin
if FCurrentCallStack = nil then begin
Finished;
exit;
end;
It := TMapIterator.Create(FCurrentCallStack.RawEntries);
for i := 0 to Length(Instr.Res) - 1 do begin
s := Instr.Res[i];
ParseNewFrameLocation(s, FId, IsCur, addr, stack, frame, func, Arguments, filename, fullfile, line, d);
if It.Locate(FId) then begin
e := TCallStackEntry(It.DataPtr^);
e.Init(addr, Arguments, func, filename, fullfile, line);
end;
Arguments.Free;
end;
It.Free;
TLldbCallStack(Debugger.CallStack).ParentRequestEntries(FCurrentCallStack);
Finished;
end;
procedure TLldbDebuggerCommandCallStack.DoCallstackFreed(Sender: TObject);
begin
FCurrentCallStack := nil;
//TODO cancel
end;
procedure TLldbDebuggerCommandCallStack.DoExecute;
var
StartIdx, EndIdx: Integer;
Instr: TLldbInstructionStackTrace;
begin
if FCurrentCallStack = nil then begin
Finished;
exit;
end;
StartIdx := Max(FCurrentCallStack.LowestUnknown, 0);
EndIdx := FCurrentCallStack.HighestUnknown;
if EndIdx < StartIdx then begin
Finished;
exit;
end;
Instr := TLldbInstructionStackTrace.Create(EndIdx+1, FCurrentCallStack.ThreadId);
Instr.OnFinish := @StackInstructionFinished;
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
constructor TLldbDebuggerCommandCallStack.Create(AOwner: TLldbDebugger;
ACurrentCallStack: TCallStackBase);
begin
inherited Create(AOwner);
FCurrentCallStack := ACurrentCallStack;
FCurrentCallStack.AddFreeNotification(@DoCallstackFreed);
end;
destructor TLldbDebuggerCommandCallStack.Destroy;
begin
if FCurrentCallStack <> nil then
FCurrentCallStack.RemoveFreeNotification(@DoCallstackFreed);
inherited Destroy;
end;
{ TLldbCallStack }
procedure TLldbCallStack.ParentRequestEntries(ACallstack: TCallStackBase);
begin
inherited RequestEntries(ACallstack);
end;
procedure TLldbCallStack.RequestAtLeastCount(ACallstack: TCallStackBase;
ARequiredMinCount: Integer);
begin
if (Debugger = nil) or not(Debugger.State in [dsPause, dsInternalPause]) then begin
ACallstack.SetCurrentValidity(ddsInvalid);
Exit;
end;
ACallstack.Count := ARequiredMinCount + 1; // TODO: get data, and return correct result
ACallstack.SetCountValidity(ddsValid);
end;
procedure TLldbCallStack.UpdateCurrentIndex;
var
tid, idx: Integer;
cs: TCallStackBase;
begin
if (Debugger = nil) or not(Debugger.State in [dsPause, dsInternalPause]) then begin
exit;
end;
tid := Debugger.Threads.CurrentThreads.CurrentThreadId; // FCurrentThreadId ?
cs := TCallStackBase(CurrentCallStackList.EntriesForThreads[tid]);
idx := cs.NewCurrentIndex; // NEW-CURRENT
if TLldbDebugger(Debugger).FCurrentStackFrame = idx then Exit;
TLldbDebugger(Debugger).FCurrentStackFrame := idx;
if cs <> nil then begin
cs.CurrentIndex := idx;
cs.SetCurrentValidity(ddsValid);
end;
end;
procedure TLldbCallStack.RequestCurrent(ACallstack: TCallStackBase);
begin
if (Debugger = nil) or not(Debugger.State in [dsPause, dsInternalPause]) then begin
ACallstack.SetCurrentValidity(ddsInvalid);
Exit;
end;
ACallstack.CurrentIndex := 0; // will be used, if thread is changed
ACallstack.SetCurrentValidity(ddsValid);
end;
procedure TLldbCallStack.RequestEntries(ACallstack: TCallStackBase);
var
Cmd: TLldbDebuggerCommandCallStack;
begin
if not (Debugger.State in [dsPause, dsInternalPause]) then
exit;
Cmd := TLldbDebuggerCommandCallStack.Create(TLldbDebugger(Debugger), ACallstack);
TLldbDebugger(Debugger).QueueCommand(Cmd);
Cmd.ReleaseReference;
end;
{%endregion ^^^^^ CallStack ^^^^^ }
{ TLldbLocals }
procedure TLldbLocals.RequestData(ALocals: TLocals);
var
Cmd: TLldbDebuggerCommandLocals;
begin
if (Debugger = nil) or not(Debugger.State in [dsPause, dsInternalPause]) then Exit;
Cmd := TLldbDebuggerCommandLocals.Create(TLldbDebugger(Debugger), ALocals);
TLldbDebugger(Debugger).QueueCommand(Cmd);
Cmd.ReleaseReference;
end;
{ TLldbWatches }
procedure TLldbWatches.InternalRequestData(AWatchValue: TWatchValue);
var
Cmd: TLldbDebuggerCommandEvaluate;
begin
if (Debugger = nil) or not(Debugger.State in [dsPause, dsInternalPause]) then Exit;
Cmd := TLldbDebuggerCommandEvaluate.Create(TLldbDebugger(Debugger), AWatchValue);
TLldbDebugger(Debugger).QueueCommand(Cmd);
Cmd.ReleaseReference;
end;
{ TLldbBreakPoint }
procedure TLldbBreakPoint.SetBreakPoint;
var
i: Integer;
s: String;
Instr: TLldbInstruction;
en: Boolean;
begin
debugln(['TLldbBreakPoint.SetBreakPoint ']);
if FCurrentInstruction <> nil then begin
if (FBreakID <> 0) or (not FCurrentInstruction.IsRunning) then begin
// Can be a queued SetBreak => replace
// Or an Update => don't care, ReleaseBreakpoint will be called
CancelCurrentInstruction;
end
else begin
// already running a SetBreakPoint
FNeededChanges := FNeededChanges + [ciLocation]; // wait for instruction to finish // need ID to del
exit;
end;
end;
if (FBreakID <> 0) then
ReleaseBreakPoint;
en := Enabled;
if TLldbDebugger(Debugger).FLldbMissingBreakSetDisable and (not en) and (Kind <> bpkData) then begin
en := True;
FNeededChanges := FNeededChanges + [ciEnabled];
end;
case Kind of
bpkSource: begin
i := LastPos(PathDelim, Source);
if i > 0 then
s := Copy(Source, i+1, Length(Source))
else
s := Source;
Instr := TLldbInstructionBreakSet.Create(s, Line, not en, Expression);
end;
bpkAddress: begin
Instr := TLldbInstructionBreakSet.Create(Address, not en, Expression);
end;
bpkData: begin
if not Enabled then // do not set, if not enabled
exit;
// TODO: scope
// TODO: apply , Expression, not Enabled
Instr := TLldbInstructionWatchSet.Create(WatchData, WatchKind);
if Expression <> '' then
FNeededChanges := FNeededChanges + [ciCondition];
end;
end;
Instr.OnFinish := @InstructionSetBreakFinished;
TLldbDebugger(Debugger).FDebugInstructionQueue.QueueInstruction(Instr);
FCurrentInstruction := Instr;
end;
procedure TLldbBreakPoint.InstructionSetBreakFinished(Sender: TObject);
var
nc: TDbgBpChangeIndicators;
begin
DoCurrentInstructionFinished;
if TLldbInstructionBreakOrWatchSet(Sender).LldbNoDisableError then begin
TLldbDebugger(Debugger).FLldbMissingBreakSetDisable := True;
FNeededChanges := FNeededChanges + [ciLocation]
end;
if TLldbInstructionBreakOrWatchSet(Sender).IsSuccess then begin
FBreakID := TLldbInstructionBreakOrWatchSet(Sender).BreakId;
if FNeededChanges * [ciDestroy, ciLocation] = [] then
SetValid(TLldbInstructionBreakOrWatchSet(Sender).State);
end
else
SetValid(vsInvalid);
nc := FNeededChanges;
FNeededChanges := [];
MarkPropertiesChanged(nc);
end;
procedure TLldbBreakPoint.InstructionUpdateBreakFinished(Sender: TObject);
var
nc: TDbgBpChangeIndicators;
begin
DoCurrentInstructionFinished;
nc := FNeededChanges;
FNeededChanges := [];
MarkPropertiesChanged(nc);
end;
procedure TLldbBreakPoint.ReleaseBreakPoint;
var
Instr: TLldbInstruction;
begin
CancelCurrentInstruction;
if FBreakID <= 0 then exit;
SetHitCount(0);
case Kind of
bpkSource, bpkAddress:
Instr := TLldbInstructionBreakDelete.Create(FBreakID);
bpkData:
Instr := TLldbInstructionWatchDelete.Create(FBreakID);
end;
FBreakID := 0; // Allow a new location to be set immediately
//Instr.OwningCommand := Self; // if it needs to be cancelled
TLldbDebugger(Debugger).FDebugInstructionQueue.QueueInstruction(Instr);
Instr.ReleaseReference;
end;
procedure TLldbBreakPoint.UpdateProperties(AChanged: TDbgBpChangeIndicators);
var
Instr: TLldbInstruction;
begin
assert(AChanged * [ciEnabled, ciCondition] <> [], 'break.UpdateProperties() AChanged * [ciEnabled, ciCondition] <> []');
if (FCurrentInstruction <> nil) then begin
FNeededChanges := FNeededChanges + AChanged;
exit;
end;
if FBreakID = 0 then // SetBreakPoint may have failed / nothing to do
exit;
case Kind of
bpkSource, bpkAddress:
if ciCondition in AChanged
then Instr := TLldbInstructionBreakModify.Create(FBreakID, not Enabled, Expression)
else Instr := TLldbInstructionBreakModify.Create(FBreakID, not Enabled);
bpkData:
begin
if Enabled <> (FBreakID <> 0) then begin
if Enabled
then SetBreakPoint // will
else ReleaseBreakPoint;
exit;
end;
if ciCondition in AChanged then
Instr := TLldbInstructionWatchModify.Create(FBreakID, Expression);
end;
end;
TLldbDebugger(Debugger).FDebugInstructionQueue.QueueInstruction(Instr);
Instr.OnFinish := @InstructionUpdateBreakFinished;
FCurrentInstruction := Instr;
end;
procedure TLldbBreakPoint.DoCurrentInstructionFinished;
begin
if FCurrentInstruction <> nil then begin
FCurrentInstruction.OnFinish := nil;
ReleaseRefAndNil(FCurrentInstruction);
end;
end;
procedure TLldbBreakPoint.CancelCurrentInstruction;
begin
if FCurrentInstruction <> nil then begin
FCurrentInstruction.OnFinish := nil;
FCurrentInstruction.Cancel;
ReleaseRefAndNil(FCurrentInstruction);
end;
end;
procedure TLldbBreakPoint.DoStateChange(const AOldState: TDBGState);
begin
inherited DoStateChange(AOldState);
case DebuggerState of
dsRun: if AOldState = dsInit then begin
// Disabled data breakpoints: wait until enabled
// Disabled other breakpoints: Give to LLDB to see if they are valid
SetBreakPoint
end;
dsStop: begin
if FBreakID > 0
then ReleaseBreakpoint;
end;
end;
end;
procedure TLldbBreakPoint.DoPropertiesChanged(AChanged: TDbgBpChangeIndicators);
begin
FNeededChanges := [];
if not (DebuggerState in [dsPause, dsInternalPause, dsRun]) then
exit;
if ciDestroy in AChanged then begin
ReleaseBreakPoint;
DoCurrentInstructionFinished;
exit;
end;
if AChanged * [ciLocation, ciCreated] <> [] then
SetBreakPoint
else
UpdateProperties(AChanged);
end;
destructor TLldbBreakPoint.Destroy;
begin
DoCurrentInstructionFinished;
inherited Destroy;
end;
{ TLldbBreakPoints }
function TLldbBreakPoints.FindById(AnId: Integer): TLldbBreakPoint;
var
i: Integer;
begin
for i := 0 to Count - 1 do begin
Result := TLldbBreakPoint(Items[i]);
if Result.FBreakID = AnId then
exit;
end;
Result := nil;
end;
{%region
*****
***** Register
***** }
{ TLldbDebuggerCommandRegister }
procedure TLldbDebuggerCommandRegister.RegisterInstructionFinished(
Sender: TObject);
var
Instr: TLldbInstructionRegister absolute Sender;
RegVal: TRegisterValue;
n: String;
i: Integer;
begin
if not Instr.IsSuccess then begin
if FRegisters.DataValidity in [ddsRequested, ddsEvaluating] then
FRegisters.DataValidity := ddsInvalid;
exit;
end;
FRegisters.DataValidity := ddsEvaluating;
for i := 0 to Instr.Res.Count - 1 do begin
n := Instr.Res.Names[i];
RegVal := FRegisters.EntriesByName[n];
RegVal.Value := Instr.Res.Values[n];
RegVal.DataValidity := ddsValid;
end;
FRegisters.DataValidity := ddsValid;
Finished;
end;
procedure TLldbDebuggerCommandRegister.DoExecute;
var
Instr: TLldbInstructionRegister;
begin
// TODO: store thread/frame when command is created
Instr := TLldbInstructionRegister.Create(FRegisters.ThreadId, FRegisters.StackFrame);
Instr.OnFinish := @RegisterInstructionFinished;
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
constructor TLldbDebuggerCommandRegister.Create(AOwner: TLldbDebugger;
ARegisters: TRegisters);
begin
FRegisters := ARegisters;
FRegisters.AddReference;
inherited Create(AOwner);
end;
destructor TLldbDebuggerCommandRegister.Destroy;
begin
ReleaseRefAndNil(FRegisters);
inherited Destroy;
end;
{ TLldbRegisterSupplier }
procedure TLldbRegisterSupplier.Changed;
begin
if CurrentRegistersList <> nil
then CurrentRegistersList.Clear;
end;
procedure TLldbRegisterSupplier.RequestData(ARegisters: TRegisters);
var
Cmd: TLldbDebuggerCommandRegister;
begin
if (Debugger = nil) or not(Debugger.State in [dsPause, dsInternalPause, dsStop]) then
exit;
Cmd := TLldbDebuggerCommandRegister.Create(TLldbDebugger(Debugger), ARegisters);
TLldbDebugger(Debugger).QueueCommand(Cmd);
Cmd.ReleaseReference;
end;
{%endregion ^^^^^ Register ^^^^^ }
{ TLldbDebuggerCommandQueue }
function TLldbDebuggerCommandQueue.Get(Index: Integer): TLldbDebuggerCommand;
begin
Result := TLldbDebuggerCommand(inherited Get(Index));
end;
procedure TLldbDebuggerCommandQueue.Put(Index: Integer;
const AValue: TLldbDebuggerCommand);
begin
inherited Put(Index, AValue);
end;
procedure TLldbDebuggerCommandQueue.QueueCommand(AValue: TLldbDebuggerCommand);
begin
debugln(['CommandQueue.QueueCommand ', AValue.ClassName]);
Insert(Count, AValue);
Run;
end;
procedure TLldbDebuggerCommandQueue.DoLineDataReceived(var ALine: String);
begin
if FRunningCommand <> nil then
FRunningCommand.DoLineDataReceived(ALine);
end;
procedure TLldbDebuggerCommandQueue.Run;
begin
if (FRunningCommand <> nil) or (FLockQueueRun > 0) then
exit;
{$IFnDEF LLDB_SKIP_IDLE}
if Count = 0 then begin
if Assigned(FDebugger.OnIdle) and (not FDebugger.FInIdle) then begin
FDebugger.FInIdle := True;
LockQueueRun;
FDebugger.OnIdle(Self);
UnLockQueueRun;
FDebugger.FInIdle := False;
end;
exit;
end;
{$ENDIF}
FRunningCommand := Items[0];
FRunningCommand.AddReference;
Delete(0);
DebugLnEnter(['||||||||>>> CommandQueue.Run ', FRunningCommand.ClassName, ', ', dbgs(fDebugger.State), ' Cnt:',Count]);
FRunningCommand.Execute;
// debugger and queue may get destroyed at the end of execute
end;
procedure TLldbDebuggerCommandQueue.CommandFinished(
ACommand: TLldbDebuggerCommand);
begin
if FRunningCommand = ACommand then begin
DebugLnExit(['||||||||<<< CommandQueue.Run ', FRunningCommand.ClassName, ', ', dbgs(fDebugger.State), ' Cnt:',Count]);
ReleaseRefAndNil(FRunningCommand);
end//;
else DebugLn(['|||||||| TLldbDebuggerCommandQueue.CommandFinished >> unknown ???', ', ', dbgs(fDebugger.State), ' Cnt:',Count]);
if not(FDebugger.State in [dsError, dsDestroying, dsNone]) then
Run;
end;
constructor TLldbDebuggerCommandQueue.Create(ADebugger: TLldbDebugger);
begin
FDebugger := ADebugger;
inherited Create;
end;
destructor TLldbDebuggerCommandQueue.Destroy;
begin
while Count > 0 do
Delete(0);
if FRunningCommand <> nil then begin
DebugLnExit(['<<< CommandQueue.Run (Destroy)', FRunningCommand.ClassName, ', ', fDebugger.State]);
ReleaseRefAndNil(FRunningCommand);
end;
inherited Destroy;
end;
procedure TLldbDebuggerCommandQueue.CancelAll;
var
i: Integer;
begin
i := Count - 1;
while i >= 0 do begin
Items[i].Cancel;
dec(i);
if i > Count then
i := Count - 1;
end;
if FRunningCommand <> nil then
FRunningCommand.Cancel;
end;
procedure TLldbDebuggerCommandQueue.LockQueueRun;
begin
inc(FLockQueueRun);
debugln(['TLldbDebuggerCommandQueue.LockQueueRun ',FLockQueueRun]);
end;
procedure TLldbDebuggerCommandQueue.UnLockQueueRun;
begin
debugln(['TLldbDebuggerCommandQueue.UnLockQueueRun ',FLockQueueRun]);
dec(FLockQueueRun);
if FLockQueueRun = 0 then Run;
end;
{ TLldbDebuggerCommand }
function TLldbDebuggerCommand.GetDebuggerState: TDBGState;
begin
Result := Debugger.State;
end;
procedure TLldbDebuggerCommand.InstructionSucceeded(AnInstruction: TObject);
begin
Finished;
end;
procedure TLldbDebuggerCommand.InstructionFailed(AnInstruction: TObject);
begin
SetDebuggerState(dsError);
Finished;
end;
procedure TLldbDebuggerCommand.Finished;
begin
InstructionQueue.CancelAllForCommand(Self); // in case there still are any
CommandQueue.CommandFinished(Self);
end;
function TLldbDebuggerCommand.GetCommandQueue: TLldbDebuggerCommandQueue;
begin
Result := Debugger.FCommandQueue;
end;
function TLldbDebuggerCommand.GetInstructionQueue: TLldbInstructionQueue;
begin
Result := Debugger.FDebugInstructionQueue;
end;
procedure TLldbDebuggerCommand.QueueInstruction(AnInstruction: TLldbInstruction);
begin
AnInstruction.OwningCommand := Self;
InstructionQueue.QueueInstruction(AnInstruction);
end;
procedure TLldbDebuggerCommand.SetDebuggerState(const AValue: TDBGState);
begin
Debugger.SetState(AValue);
end;
constructor TLldbDebuggerCommand.Create(AOwner: TLldbDebugger);
begin
FOwner := AOwner;
inherited Create;
AddReference;
end;
destructor TLldbDebuggerCommand.Destroy;
begin
if InstructionQueue <> nil then
InstructionQueue.CancelAllForCommand(Self);
inherited Destroy;
end;
procedure TLldbDebuggerCommand.Execute;
var
d: TLldbDebugger;
begin
FIsRunning := True;
d := Debugger;
try
d.LockRelease;
DoExecute; // may call Finished and Destroy Self
finally
d.UnlockRelease;
end;
end;
procedure TLldbDebuggerCommand.Cancel;
begin
Debugger.CommandQueue.Remove(Self); // current running command is not on queue // dec refcount, may call destroy
if FIsRunning then
DoCancel; // should call CommandQueue.CommandFinished
end;
procedure TLldbDebuggerCommand.DoLineDataReceived(var ALine: String);
begin
//
end;
procedure TLldbDebuggerCommand.DoCancel;
begin
//
end;
{ TLldbDebuggerCommandInit }
procedure TLldbDebuggerCommandInit.DoExecute;
const
FRAME_INFO =
'${frame.pc}, {${frame.sp}}, {${frame.fp}}' +
' &&//FULL: {${line.file.fullpath}} &&//SHORT: {${line.file.basename}} &&//LINE: {${line.number}}' +
' &&//MOD: {${module.file.basename}} &&//FUNC: {${function.name-with-args}}' +
' <<&&//FRAME';
var
Instr: TLldbInstruction;
begin
Instr := TLldbInstructionSettingSet.Create('frame-format',
'"frame #${frame.index}: ' + FRAME_INFO + '\n"'
);
QueueInstruction(Instr);
Instr.ReleaseReference;
Instr := TLldbInstructionSettingSet.Create('thread-format',
'"thread #${thread.index}: tid=${thread.id%tid}: ' + FRAME_INFO +
//'{, activity = ''${thread.info.activity.name}''}{, ${thread.info.trace_messages} messages}' +
'{, stop reason = ${thread.stop-reason}}' +
//'{\nReturn value: ${thread.return-value}}{\nCompleted expression: ${thread.completed-expression}}' +
'\n"'
);
QueueInstruction(Instr);
Instr.ReleaseReference;
// Not all versions of lldb have this
Instr := TLldbInstructionSettingSet.Create('thread-stop-format',
'"thread #${thread.index}: tid=${thread.id%tid}: ' + FRAME_INFO +
//'{, activity = ''${thread.info.activity.name}''}{, ${thread.info.trace_messages} messages}' +
'{, stop reason = ${thread.stop-reason}}' +
//'{\nReturn value: ${thread.return-value}}{\nCompleted expression: ${thread.completed-expression}}' +
'\n"'
);
QueueInstruction(Instr);
Instr.ReleaseReference;
Instr := TLldbInstructionTargetStopHook.Create('thread list');
QueueInstruction(Instr);
Instr.ReleaseReference;
Instr := TLldbInstructionSettingSet.Create('stop-line-count-after', '0');
QueueInstruction(Instr);
Instr.ReleaseReference;
Instr := TLldbInstructionSettingSet.Create('stop-line-count-before', '0');
QueueInstruction(Instr);
Instr.ReleaseReference;
Instr := TLldbInstructionSettingSet.Create('stop-disassembly-count', '0');
Instr.OnFinish := @InstructionSucceeded;
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
{ TLldbDebuggerCommandRunStep }
procedure TLldbDebuggerCommandRunStep.DoInitialExecute;
begin
SetNextStepCommand(FStepAction);
end;
constructor TLldbDebuggerCommandRunStep.Create(AOwner: TLldbDebugger;
AStepAction: TLldbInstructionProcessStepAction);
var
AtExcepiton: Boolean;
begin
AtExcepiton := AOwner.FExceptionInfo.FAtExcepiton;
FStepAction := AStepAction;
inherited Create(AOwner);
if AtExcepiton and
(AStepAction in [saOver, saInto, saOut])
then
FMode := cmRunToCatch;
end;
{ TLldbDebuggerCommandRunLaunch }
procedure TLldbDebuggerCommandRunLaunch.TargetCreated(Sender: TObject);
var
TargetInstr: TLldbInstructionTargetCreate absolute Sender;
Instr: TLldbInstruction;
found: TStringArray;
begin
if not TargetInstr.IsSuccess then begin
SetDebuggerState(dsError);
Finished;
end;
If StrMatches(TargetInstr.Res, [''{}, '','('{}, ')',''], found) then begin
if (found[1] = 'i386') or (found[1] = 'i686') then begin
DebugLn(DBG_VERBOSE, ['Target 32 bit: ', found[1]]);
Debugger.FTargetWidth := 32;
Debugger.FTargetRegisters[0] := '$eax';
Debugger.FTargetRegisters[1] := '$edx';
Debugger.FTargetRegisters[2] := '$ecx';
end
else
if (found[1] = '(x86_64)') or (found[1] = 'x86_64') then begin
DebugLn(DBG_VERBOSE, ['Target 64 bit: ', found[1]]);
Debugger.FTargetWidth := 64;
// target list gives more detailed result. But until remote debugging is added, use the current system
{$IFDEF MSWindows}
Debugger.FTargetRegisters[0] := '$rcx';
Debugger.FTargetRegisters[1] := '$rdx';
Debugger.FTargetRegisters[2] := '$r8';
{$ELSE}
Debugger.FTargetRegisters[0] := '$rdi';
Debugger.FTargetRegisters[1] := '$rsi';
Debugger.FTargetRegisters[2] := '$rdx';
{$ENDIF}
end
else found := nil;
end
else found := nil;
if found = nil then begin
DebugLn(DBG_VERBOSE, ['Target bitness UNKNOWN']);
// use architecture of IDE
{$IFDEF cpu64}
Debugger.FTargetWidth := 64;
{$IFDEF MSWindows}
Debugger.FTargetRegisters[0] := '$rcx';
Debugger.FTargetRegisters[1] := '$rdx';
Debugger.FTargetRegisters[2] := '$r8';
{$ELSE}
Debugger.FTargetRegisters[0] := '$rdi';
Debugger.FTargetRegisters[1] := '$rsi';
Debugger.FTargetRegisters[2] := '$rdx';
{$ENDIF}
{$ELSE}
Debugger.FTargetWidth := 32;
Debugger.FTargetRegisters[0] := '$eax';
Debugger.FTargetRegisters[1] := '$edx';
Debugger.FTargetRegisters[2] := '$ecx';
{$ENDIF}
end;
if Trim(Debugger.Arguments) <> '' then
Instr := TLldbInstructionSettingSet.Create('target.run-args', Debugger.Arguments)
else
Instr := TLldbInstructionSettingClear.Create('target.run-args');
QueueInstruction(Instr);
Instr.ReleaseReference;
Debugger.FBreakErrorBreak.Enable;
Debugger.FRunErrorBreak.Enable;
Debugger.FExceptionBreak.OnFinish := @ExceptBreakInstructionFinished;
Debugger.FExceptionBreak.Enable;
end;
procedure TLldbDebuggerCommandRunLaunch.ExceptBreakInstructionFinished(Sender: TObject
);
var
Instr: TLldbInstruction;
BrkId: Integer;
begin
Debugger.FBreakErrorBreak.OnFinish := nil;
Debugger.FExceptionInfo.FReg0Cmd := '';
Debugger.FExceptionInfo.FReg2Cmd := '';
Debugger.FExceptionInfo.FExceptClassCmd := '';
Debugger.FExceptionInfo.FExceptMsgCmd := '';
BrkId := Debugger.FExceptionBreak.BreakId;
if BrkId > 0 then begin
Debugger.FExceptionInfo.FReg0Cmd := 'p/x ' + Debugger.FTargetRegisters[0];
Debugger.FExceptionInfo.FReg2Cmd := 'p/x ' + Debugger.FTargetRegisters[2];
Debugger.FExceptionInfo.FExceptClassCmd := 'p ((char ***)' + Debugger.FTargetRegisters[0] + ')[0][3]';
Debugger.FExceptionInfo.FExceptMsgCmd := 'p ((char **)' + Debugger.FTargetRegisters[0] + ')[1]';
// 'p ((EXCEPTION *)' + Debugger.FTargetRegisters[0] + ')->FMESSAGE'
Instr := TLldbInstructionBreakAddCommands.Create(BrkId, [
Debugger.FExceptionInfo.FReg0Cmd, Debugger.FExceptionInfo.FReg2Cmd, Debugger.FExceptionInfo.FExceptClassCmd, Debugger.FExceptionInfo.FExceptMsgCmd
]);
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
BrkId := Debugger.FRunErrorBreak.BreakId;
if BrkId > 0 then begin
Debugger.FExceptionInfo.FReg0Cmd := 'p/x ' + Debugger.FTargetRegisters[0];
Instr := TLldbInstructionBreakAddCommands.Create(BrkId, [Debugger.FExceptionInfo.FReg0Cmd]);
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
BrkId := Debugger.FBreakErrorBreak.BreakId;
if BrkId > 0 then begin
Debugger.FExceptionInfo.FReg0Cmd := 'p/x ' + Debugger.FTargetRegisters[0];
Instr := TLldbInstructionBreakAddCommands.Create(BrkId, [Debugger.FExceptionInfo.FReg0Cmd]);
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
SetDebuggerState(dsRun);
// the state change allows breakpoints to be set, before the run command is issued.
FRunInstr := TLldbInstructionProcessLaunch.Create();
FRunInstr.OnSuccess := @RunInstructionSucceeded;
FRunInstr.OnFailure := @InstructionFailed;
QueueInstruction(FRunInstr);
FRunInstr.ReleaseReference;
end;
procedure TLldbDebuggerCommandRunLaunch.DoInitialExecute;
var
Instr: TLldbInstruction;
begin
Instr := TLldbInstructionTargetCreate.Create(Debugger.FileName);
Instr.OnSuccess := @TargetCreated;
Instr.OnFailure := @InstructionFailed;
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
constructor TLldbDebuggerCommandRunLaunch.Create(AOwner: TLldbDebugger);
begin
FStepAction := saContinue;
inherited Create(AOwner);
end;
{ TLldbDebuggerCommandStop }
procedure TLldbDebuggerCommandStop.StopInstructionSucceeded(Sender: TObject);
begin
if DebuggerState <> dsIdle then
SetDebuggerState(dsStop);
end;
procedure TLldbDebuggerCommandStop.DoExecute;
var
Instr: TLldbInstruction;
begin
Instr := TLldbInstructionProcessKill.Create();
Instr.OnSuccess := @StopInstructionSucceeded;
Instr.OnFailure := @InstructionFailed;
QueueInstruction(Instr);
Instr.ReleaseReference;
Instr := TLldbInstructionTargetDelete.Create();
Instr.OnFailure := @InstructionFailed;
QueueInstruction(Instr);
Instr.ReleaseReference;
Instr := TLldbInstructionTargetDelete.Create();
Instr.OnSuccess := @InstructionSucceeded;
Instr.OnFailure := @InstructionFailed;
QueueInstruction(Instr);
Instr.ReleaseReference;
end;
{ TLldbDebuggerCommandEvaluate }
procedure TLldbDebuggerCommandEvaluate.EvalInstructionSucceeded(Sender: TObject
);
begin
if FWatchValue <> nil then begin
FWatchValue.Value := FInstr.Res;
//FWatchValue.TypeInfo := TypeInfo;
FWatchValue.Validity := ddsValid;
end
else
if FCallback <> nil then
FCallback(Debugger, True, FInstr.Res, nil);
FInstr.ReleaseReference;
Finished;
end;
procedure TLldbDebuggerCommandEvaluate.EvalInstructionFailed(Sender: TObject);
begin
if FWatchValue <> nil then
FWatchValue.Validity := ddsError
else
if FCallback <> nil then
FCallback(Debugger, False, '', nil);
FInstr.ReleaseReference;
Finished;
end;
procedure TLldbDebuggerCommandEvaluate.DoWatchFreed(Sender: TObject);
begin
FWatchValue := nil;
end;
procedure TLldbDebuggerCommandEvaluate.DoExecute;
begin
if FWatchValue <> nil then
FInstr := TLldbInstructionExpression.Create(FWatchValue.Expression, Debugger.FCurrentThreadId, Debugger.FCurrentStackFrame)
else
// todo: only if FCallback ?
FInstr := TLldbInstructionExpression.Create(FExpr, Debugger.FCurrentThreadId, Debugger.FCurrentStackFrame);
FInstr.OnSuccess := @EvalInstructionSucceeded;
FInstr.OnFailure := @EvalInstructionFailed;
QueueInstruction(FInstr);
end;
constructor TLldbDebuggerCommandEvaluate.Create(AOwner: TLldbDebugger;
AWatchValue: TWatchValue);
begin
FWatchValue := AWatchValue;
FWatchValue.AddFreeNotification(@DoWatchFreed);
inherited Create(AOwner);
end;
constructor TLldbDebuggerCommandEvaluate.Create(AOwner: TLldbDebugger;
AnExpr: String; AFlags: TDBGEvaluateFlags;
ACallback: TDBGEvaluateResultCallback);
begin
FExpr := AnExpr;
FFlags := AFlags;
FCallback := ACallback;
inherited Create(AOwner);
end;
destructor TLldbDebuggerCommandEvaluate.Destroy;
begin
if FWatchValue <> nil then
FWatchValue.RemoveFreeNotification(@DoWatchFreed);
inherited Destroy;
end;
{ TlldbInternalBreakPoint }
procedure TlldbInternalBreakPoint.QueueInstruction(AnInstr: TLldbInstruction);
begin
AnInstr.OnFinish := @DoFinshed;
FDebugger.DebugInstructionQueue.QueueInstruction(AnInstr);
AnInstr.ReleaseReference;
end;
procedure TlldbInternalBreakPoint.BreakSetSuccess(Sender: TObject);
begin
FId := TLldbInstructionBreakSet(Sender).BreakId;
end;
procedure TlldbInternalBreakPoint.DoFailed(Sender: TObject);
begin
if FId = 0 then
FId := -1;
if OnFail <> nil then
OnFail(Self);
end;
procedure TlldbInternalBreakPoint.DoFinshed(Sender: TObject);
begin
if OnFinish <> nil then
OnFinish(Self);
end;
constructor TlldbInternalBreakPoint.Create(AName: String;
ADebugger: TLldbDebugger; ABeforePrologue: Boolean);
begin
FName := AName;
FDebugger := ADebugger;
FBeforePrologue := ABeforePrologue;
FId := 0;
inherited Create;
end;
destructor TlldbInternalBreakPoint.Destroy;
begin
Remove;
inherited Destroy;
end;
procedure TlldbInternalBreakPoint.Enable;
var
Instr: TLldbInstruction;
begin
if FId = 0 then begin
Instr := TLldbInstructionBreakSet.Create(FName, False, FBeforePrologue);
Instr.OnSuccess := @BreakSetSuccess;
Instr.OnFailure := @DoFailed;
QueueInstruction(Instr);
exit;
end;
if FId < 0 then begin
DoFailed(nil);
exit;
end;
Instr := TLldbInstructionBreakModify.Create(FId, False);
Instr.OnFailure := @DoFailed;
QueueInstruction(Instr);
end;
procedure TlldbInternalBreakPoint.Disable;
var
Instr: TLldbInstruction;
begin
if FId <= 0 then
exit;
Instr := TLldbInstructionBreakModify.Create(FId, True);
Instr.OnFailure := @DoFailed;
QueueInstruction(Instr);
end;
procedure TlldbInternalBreakPoint.Remove;
var
Instr: TLldbInstruction;
begin
if FId <= 0 then
exit;
Instr := TLldbInstructionBreakDelete.Create(FId);
QueueInstruction(Instr);
FId := 0;
end;
{ TLldbDebugger }
function TLldbDebugger.LldbRun: Boolean;
var
Cmd: TLldbDebuggerCommandRunLaunch;
begin
DebugLn('*** Run');
Result := True;
if State in [dsPause, dsInternalPause, dsRun] then begin // dsRun in case of exception
LldbStep(saContinue);
exit;
end;
if State in [dsNone, dsIdle, dsStop] then
SetState(dsInit);
FInIdle := False;
DoBeforeLaunch;
Cmd := TLldbDebuggerCommandRunLaunch.Create(Self);
QueueCommand(Cmd);
Cmd.ReleaseReference;
end;
procedure TLldbDebugger.DoAfterLineReceived(var ALine: String);
var
Instr: TLldbInstruction;
begin
if ALine = '' then
exit;
FCommandQueue.DoLineDataReceived(ALine);
if ALine = '' then
exit;
// Process 8888 exited with status = 0 (0x00000000)
if (LeftStr(ALine, 8) = 'Process ') and (pos('exited with status = ', ALine) > 0) then begin
// todo: target delete
if State <> dsIdle then
SetState(dsStop);
ALine := '';
Instr := TLldbInstructionTargetDelete.Create();
FDebugInstructionQueue.QueueInstruction(Instr);
Instr.ReleaseReference;
exit;
end;
end;
procedure TLldbDebugger.DoBeforeLineReceived(var ALine: String);
begin
DoDbgOutput(ALine);
end;
procedure TLldbDebugger.DoBeginReceivingLines(Sender: TObject);
begin
LockRelease;
end;
procedure TLldbDebugger.DoCmdLineDebuggerTerminated(Sender: TObject);
begin
SetState(dsError);
end;
procedure TLldbDebugger.DoLineSentToDbg(Sender: TObject; ALine: String);
begin
DoDbgOutput('>> '+ALine);
end;
procedure TLldbDebugger.DoEndReceivingLines(Sender: TObject);
begin
UnlockRelease;
end;
function TLldbDebugger.LldbStep(AStepAction: TLldbInstructionProcessStepAction
): Boolean;
var
Cmd: TLldbDebuggerCommandRunStep;
begin
Result := True;
Cmd := TLldbDebuggerCommandRunStep.Create(Self, AStepAction);
QueueCommand(Cmd);
Cmd.ReleaseReference;
end;
function TLldbDebugger.LldbStop: Boolean;
var
Cmd: TLldbDebuggerCommandStop;
begin
DebugLn('*** Stop');
Result := True;
CommandQueue.CancelAll;
Cmd := TLldbDebuggerCommandStop.Create(Self);
QueueCommand(Cmd);
Cmd.ReleaseReference;
end;
function TLldbDebugger.LldbPause: Boolean;
var
Instr: TLldbInstruction;
begin
Result := True;
Instr := TLldbInstructionProcessInterrupt.Create();
FDebugInstructionQueue.QueueInstruction(Instr);
Instr.ReleaseReference;
end;
function TLldbDebugger.LldbEvaluate(const AExpression: String;
EvalFlags: TDBGEvaluateFlags; ACallback: TDBGEvaluateResultCallback): Boolean;
var
Cmd: TLldbDebuggerCommandEvaluate;
begin
Cmd := TLldbDebuggerCommandEvaluate.Create(Self, AExpression, EvalFlags, ACallback);
QueueCommand(Cmd);
Cmd.ReleaseReference;
Result := True;
end;
function TLldbDebugger.LldbEnvironment(const AVariable: String;
const ASet: Boolean): Boolean;
var
Instr: TLldbInstruction;
s: String;
begin
debugln(['-----------------------------------------', AVariable]);
if ASet then
Instr := TLldbInstructionSettingSet.Create('target.env-vars', AVariable, False, True)
else begin
s := AVariable;
Instr := TLldbInstructionSettingRemove.Create('target.env-vars', GetPart([], ['='], s, False, False));
end;
FDebugInstructionQueue.QueueInstruction(Instr);
Instr.ReleaseReference;
Result := True;
end;
procedure TLldbDebugger.DoBeforeLaunch;
begin
//
end;
procedure TLldbDebugger.LockRelease;
begin
inherited LockRelease;
end;
procedure TLldbDebugger.UnlockRelease;
begin
inherited UnlockRelease;
end;
procedure TLldbDebugger.QueueCommand(const ACommand: TLldbDebuggerCommand);
begin
FCommandQueue.QueueCommand(ACommand);
end;
function TLldbDebugger.DoExceptionHit(AExcClass, AExcMsg: String): Boolean;
begin
if Assigned(EventLogHandler) then
EventLogHandler.LogCustomEvent(ecDebugger, etExceptionRaised,
Format('Exception class "%s" at $%.' + IntToStr(TargetWidth div 4) + 'x with message "%s"',
[AExcClass, FCurrentLocation.Address, AExcMsg]));
if Assigned(OnException) then
OnException(Self, deInternal, AExcClass, FCurrentLocation, AExcMsg, Result) // TODO: Location
else
Result := True; // CanContinue
end;
function TLldbDebugger.DoBreakpointHit(BrkId: Integer): Boolean;
var
BreakPoint: TLldbBreakPoint;
begin
if (BrkId >= 0) then
BreakPoint := TLldbBreakPoints(BreakPoints).FindById(BrkId)
else
BreakPoint := nil;
if Assigned(EventLogHandler) then
EventLogHandler.LogEventBreakPointHit(Breakpoint, FCurrentLocation);
if BreakPoint <> nil then begin
if (BreakPoint.Valid = vsPending) then
BreakPoint.SetPendingToValid(vsValid);
try
BreakPoint.AddReference;
// Important: The Queue must be unlocked
// BreakPoint.Hit may evaluate stack and expressions
// SetDebuggerState may evaluate data for Snapshot
Result := False; // Result;
BreakPoint.Hit(Result);
finally
BreakPoint.ReleaseReference;
end;
end
else
if (State = dsRun)
then begin
debugln(['********** WARNING: breakpoint hit, but nothing known about it ABreakId=', BrkId]);
end;
end;
function TLldbDebugger.CreateBreakPoints: TDBGBreakPoints;
begin
Result := TLldbBreakPoints.Create(Self, TLldbBreakPoint);
end;
function TLldbDebugger.CreateLocals: TLocalsSupplier;
begin
Result := TLldbLocals.Create(Self);
end;
function TLldbDebugger.CreateRegisters: TRegisterSupplier;
begin
Result := TLldbRegisterSupplier.Create(Self);
end;
function TLldbDebugger.CreateCallStack: TCallStackSupplier;
begin
Result := TLldbCallStack.Create(Self);
end;
function TLldbDebugger.CreateWatches: TWatchesSupplier;
begin
Result := TLldbWatches.Create(Self);
end;
function TLldbDebugger.CreateThreads: TThreadsSupplier;
begin
Result := TLldbThreads.Create(Self);
end;
function TLldbDebugger.GetTargetWidth: Byte;
begin
Result := FTargetWidth;
end;
function TLldbDebugger.GetIsIdle: Boolean;
begin
Result := FInIdle or ( (CommandQueue.Count = 0) and (CommandQueue.RunningCommand = nil) );
end;
function TLldbDebugger.GetSupportedCommands: TDBGCommands;
begin
Result := [dcRun, dcStop, dcStepOver, dcStepInto, dcStepOut, dcEvaluate,
dcStepOverInstr, dcStepIntoInstr, dcPause, dcEnvironment];
// Result := [dcRunTo, dcAttach, dcDetach, dcJumpto,
// dcBreak, dcWatch, dcLocal, dcEvaluate, dcModify,
// dcSetStackFrame, dcDisassemble
// ];
end;
function TLldbDebugger.RequestCommand(const ACommand: TDBGCommand;
const AParams: array of const; const ACallback: TMethod): Boolean;
var
EvalFlags: TDBGEvaluateFlags;
begin
LockRelease;
try
case ACommand of
dcRun: Result := LldbRun;
dcPause: Result := LldbPause;
dcStop: Result := LldbStop;
dcStepOver: Result := LldbStep(saOver);
dcStepInto: Result := LldbStep(saInto);
dcStepOut: Result := LldbStep(saOut);
dcStepOverInstr: Result := LldbStep(saInsOver);
dcStepIntoInstr: Result := LldbStep(saInsIn);
dcEvaluate: begin
EvalFlags := [];
if high(AParams) >= 1 then
EvalFlags := TDBGEvaluateFlags(AParams[1].VInteger);
Result := LldbEvaluate(String(AParams[0].VAnsiString),
EvalFlags, TDBGEvaluateResultCallback(ACallback));
end;
// dcRunTo: Result := GDBRunTo(String(AParams[0].VAnsiString), AParams[1].VInteger);
// dcJumpto: Result := GDBJumpTo(String(AParams[0].VAnsiString), AParams[1].VInteger);
// dcAttach: Result := GDBAttach(String(AParams[0].VAnsiString));
// dcDetach: Result := GDBDetach;
// dcModify: Result := GDBModify(String(AParams[0].VAnsiString), String(AParams[1].VAnsiString));
dcEnvironment: Result := LldbEnvironment(String(AParams[0].VAnsiString), AParams[1].VBoolean);
// dcDisassemble: Result := GDBDisassemble(AParams[0].VQWord^, AParams[1].VBoolean, TDbgPtr(AParams[2].VPointer^),
// String(AParams[3].VPointer^), String(AParams[4].VPointer^),
// String(AParams[5].VPointer^), Integer(AParams[6].VPointer^))
// {%H-};
end;
finally
UnlockRelease;
end;
end;
class function TLldbDebugger.Caption: String;
begin
Result := 'LLDB Debugger (Alpha)';
end;
constructor TLldbDebugger.Create(const AExternalDebugger: String);
begin
inherited Create(AExternalDebugger);
FDebugProcess := TDebugProcess.Create(AExternalDebugger);
FDebugProcess.OnLineSent := @DoLineSentToDbg;
FDebugInstructionQueue := TLldbInstructionQueue.Create(FDebugProcess);
FDebugInstructionQueue.OnBeginLinesReceived := @DoBeginReceivingLines;
FDebugInstructionQueue.OnEndLinesReceived := @DoEndReceivingLines;
FDebugInstructionQueue.OnBeforeHandleLineReceived := @DoBeforeLineReceived;
FDebugInstructionQueue.OnAfterHandleLineReceived := @DoAfterLineReceived;
FDebugInstructionQueue.OnDebuggerTerminated := @DoCmdLineDebuggerTerminated;
FCommandQueue := TLldbDebuggerCommandQueue.Create(Self);
FBreakErrorBreak := TlldbInternalBreakPoint.Create('fpc_break_error', Self, True);
FRunErrorBreak := TlldbInternalBreakPoint.Create('fpc_runerror', Self, True);
FExceptionBreak := TlldbInternalBreakPoint.Create('fpc_raiseexception', Self, True);
FPopExceptStack := TlldbInternalBreakPoint.Create('fpc_popaddrstack', Self);
FCatchesBreak := TlldbInternalBreakPoint.Create('fpc_catches', Self);
FReRaiseBreak := TlldbInternalBreakPoint.Create('fpc_reraise', Self);
end;
destructor TLldbDebugger.Destroy;
begin
debugln(['!!!!!!!!!!!!!!! TLldbDebugger.Destroy ']);
FBreakErrorBreak.Remove;
FRunErrorBreak.Remove;
FExceptionBreak.Remove;
FPopExceptStack.Remove;
FCatchesBreak.Remove;
FReRaiseBreak.Remove;
FDebugInstructionQueue.LockQueueRun;
inherited Destroy;
FBreakErrorBreak.Destroy;
FRunErrorBreak.Destroy;
FExceptionBreak.Destroy;
FPopExceptStack.Destroy;
FCatchesBreak.Destroy;
FReRaiseBreak.Destroy;
FCommandQueue.Destroy;
FDebugInstructionQueue.Destroy;
FDebugProcess.Destroy;
end;
procedure TLldbDebugger.Init;
var
Cmd: TLldbDebuggerCommandInit;
begin
FDebugProcess.CreateDebugProcess('', Environment);
inherited Init;
Cmd := TLldbDebuggerCommandInit.Create(Self);
QueueCommand(Cmd);
Cmd.ReleaseReference;
end;
procedure TLldbDebugger.Done;
begin
DebugLnEnter('!!! TLldbDebugger.Done;');
// TODO: cancel all commands
if FDebugProcess.DebugProcessRunning then begin
FDebugProcess.SendCmdLn('process kill');
FDebugProcess.SendCmdLn('quit');
end;
FDebugInstructionQueue.OnDebuggerTerminated := nil; // TODO: use a flag to prevent this
FDebugProcess.StopDebugProcess;
FDebugInstructionQueue.OnDebuggerTerminated := @DoCmdLineDebuggerTerminated;
inherited Done;
DebugLnExit('!!! TLldbDebugger.Done;');
end;
class function TLldbDebugger.RequiredCompilerOpts(ATargetCPU, ATargetOS: String
): TDebugCompilerRequirements;
begin
Result:=[dcrDwarfOnly];
end;
function TLldbDebugger.GetLocation: TDBGLocationRec;
begin
Result := FCurrentLocation;
end;
function TLldbDebugger.NeedReset: Boolean;
begin
Result := true;
end;
procedure TLldbDebugger.TestCmd(const ACommand: String);
begin
FDebugProcess.SendCmdLn(ACommand);
end;
procedure Register;
begin
RegisterDebugger(TLldbDebugger);
end;
initialization
DBG_VERBOSE := DebugLogger.FindOrRegisterLogGroup('DBG_VERBOSE' {$IFDEF DBG_VERBOSE} , True {$ENDIF} );
end.
|