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
|
//===-- CommandObjectCommands.cpp -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "CommandObjectCommands.h"
#include "CommandObjectHelp.h"
#include "CommandObjectRegexCommand.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/IOHandler.h"
#include "lldb/Interpreter/CommandHistory.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandOptionArgumentTable.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Interpreter/OptionArgParser.h"
#include "lldb/Interpreter/OptionValueBoolean.h"
#include "lldb/Interpreter/OptionValueString.h"
#include "lldb/Interpreter/OptionValueUInt64.h"
#include "lldb/Interpreter/Options.h"
#include "lldb/Interpreter/ScriptInterpreter.h"
#include "lldb/Utility/Args.h"
#include "lldb/Utility/StringList.h"
#include "llvm/ADT/StringRef.h"
#include <optional>
using namespace lldb;
using namespace lldb_private;
// CommandObjectCommandsSource
#define LLDB_OPTIONS_source
#include "CommandOptions.inc"
class CommandObjectCommandsSource : public CommandObjectParsed {
public:
CommandObjectCommandsSource(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "command source",
"Read and execute LLDB commands from the file <filename>.",
nullptr) {
AddSimpleArgumentList(eArgTypeFilename);
}
~CommandObjectCommandsSource() override = default;
std::optional<std::string> GetRepeatCommand(Args ¤t_command_args,
uint32_t index) override {
return std::string("");
}
Options *GetOptions() override { return &m_options; }
protected:
class CommandOptions : public Options {
public:
CommandOptions()
: m_stop_on_error(true), m_silent_run(false), m_stop_on_continue(true),
m_cmd_relative_to_command_file(false) {}
~CommandOptions() override = default;
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
ExecutionContext *execution_context) override {
Status error;
const int short_option = m_getopt_table[option_idx].val;
switch (short_option) {
case 'e':
error = m_stop_on_error.SetValueFromString(option_arg);
break;
case 'c':
error = m_stop_on_continue.SetValueFromString(option_arg);
break;
case 'C':
m_cmd_relative_to_command_file = true;
break;
case 's':
error = m_silent_run.SetValueFromString(option_arg);
break;
default:
llvm_unreachable("Unimplemented option");
}
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
m_stop_on_error.Clear();
m_silent_run.Clear();
m_stop_on_continue.Clear();
m_cmd_relative_to_command_file.Clear();
}
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
return llvm::ArrayRef(g_source_options);
}
// Instance variables to hold the values for command options.
OptionValueBoolean m_stop_on_error;
OptionValueBoolean m_silent_run;
OptionValueBoolean m_stop_on_continue;
OptionValueBoolean m_cmd_relative_to_command_file;
};
void DoExecute(Args &command, CommandReturnObject &result) override {
if (command.GetArgumentCount() != 1) {
result.AppendErrorWithFormat(
"'%s' takes exactly one executable filename argument.\n",
GetCommandName().str().c_str());
return;
}
FileSpec source_dir = {};
if (m_options.m_cmd_relative_to_command_file) {
source_dir = GetDebugger().GetCommandInterpreter().GetCurrentSourceDir();
if (!source_dir) {
result.AppendError("command source -C can only be specified "
"from a command file");
result.SetStatus(eReturnStatusFailed);
return;
}
}
FileSpec cmd_file(command[0].ref());
if (source_dir) {
// Prepend the source_dir to the cmd_file path:
if (!cmd_file.IsRelative()) {
result.AppendError("command source -C can only be used "
"with a relative path.");
result.SetStatus(eReturnStatusFailed);
return;
}
cmd_file.MakeAbsolute(source_dir);
}
FileSystem::Instance().Resolve(cmd_file);
CommandInterpreterRunOptions options;
// If any options were set, then use them
if (m_options.m_stop_on_error.OptionWasSet() ||
m_options.m_silent_run.OptionWasSet() ||
m_options.m_stop_on_continue.OptionWasSet()) {
if (m_options.m_stop_on_continue.OptionWasSet())
options.SetStopOnContinue(
m_options.m_stop_on_continue.GetCurrentValue());
if (m_options.m_stop_on_error.OptionWasSet())
options.SetStopOnError(m_options.m_stop_on_error.GetCurrentValue());
// Individual silent setting is override for global command echo settings.
if (m_options.m_silent_run.GetCurrentValue()) {
options.SetSilent(true);
} else {
options.SetPrintResults(true);
options.SetPrintErrors(true);
options.SetEchoCommands(m_interpreter.GetEchoCommands());
options.SetEchoCommentCommands(m_interpreter.GetEchoCommentCommands());
}
}
m_interpreter.HandleCommandsFromFile(cmd_file, options, result);
}
CommandOptions m_options;
};
#pragma mark CommandObjectCommandsAlias
// CommandObjectCommandsAlias
#define LLDB_OPTIONS_alias
#include "CommandOptions.inc"
static const char *g_python_command_instructions =
"Enter your Python command(s). Type 'DONE' to end.\n"
"You must define a Python function with this signature:\n"
"def my_command_impl(debugger, args, exe_ctx, result, internal_dict):\n";
class CommandObjectCommandsAlias : public CommandObjectRaw {
protected:
class CommandOptions : public OptionGroup {
public:
CommandOptions() = default;
~CommandOptions() override = default;
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
return llvm::ArrayRef(g_alias_options);
}
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_value,
ExecutionContext *execution_context) override {
Status error;
const int short_option = GetDefinitions()[option_idx].short_option;
std::string option_str(option_value);
switch (short_option) {
case 'h':
m_help.SetCurrentValue(option_str);
m_help.SetOptionWasSet();
break;
case 'H':
m_long_help.SetCurrentValue(option_str);
m_long_help.SetOptionWasSet();
break;
default:
llvm_unreachable("Unimplemented option");
}
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
m_help.Clear();
m_long_help.Clear();
}
OptionValueString m_help;
OptionValueString m_long_help;
};
OptionGroupOptions m_option_group;
CommandOptions m_command_options;
public:
Options *GetOptions() override { return &m_option_group; }
CommandObjectCommandsAlias(CommandInterpreter &interpreter)
: CommandObjectRaw(
interpreter, "command alias",
"Define a custom command in terms of an existing command.") {
m_option_group.Append(&m_command_options);
m_option_group.Finalize();
SetHelpLong(
"'alias' allows the user to create a short-cut or abbreviation for long \
commands, multi-word commands, and commands that take particular options. \
Below are some simple examples of how one might use the 'alias' command:"
R"(
(lldb) command alias sc script
Creates the abbreviation 'sc' for the 'script' command.
(lldb) command alias bp breakpoint
)"
" Creates the abbreviation 'bp' for the 'breakpoint' command. Since \
breakpoint commands are two-word commands, the user would still need to \
enter the second word after 'bp', e.g. 'bp enable' or 'bp delete'."
R"(
(lldb) command alias bpl breakpoint list
Creates the abbreviation 'bpl' for the two-word command 'breakpoint list'.
)"
"An alias can include some options for the command, with the values either \
filled in at the time the alias is created, or specified as positional \
arguments, to be filled in when the alias is invoked. The following example \
shows how to create aliases with options:"
R"(
(lldb) command alias bfl breakpoint set -f %1 -l %2
)"
" Creates the abbreviation 'bfl' (for break-file-line), with the -f and -l \
options already part of the alias. So if the user wants to set a breakpoint \
by file and line without explicitly having to use the -f and -l options, the \
user can now use 'bfl' instead. The '%1' and '%2' are positional placeholders \
for the actual arguments that will be passed when the alias command is used. \
The number in the placeholder refers to the position/order the actual value \
occupies when the alias is used. All the occurrences of '%1' in the alias \
will be replaced with the first argument, all the occurrences of '%2' in the \
alias will be replaced with the second argument, and so on. This also allows \
actual arguments to be used multiple times within an alias (see 'process \
launch' example below)."
R"(
)"
"Note: the positional arguments must substitute as whole words in the resultant \
command, so you can't at present do something like this to append the file extension \
\".cpp\":"
R"(
(lldb) command alias bcppfl breakpoint set -f %1.cpp -l %2
)"
"For more complex aliasing, use the \"command regex\" command instead. In the \
'bfl' case above, the actual file value will be filled in with the first argument \
following 'bfl' and the actual line number value will be filled in with the second \
argument. The user would use this alias as follows:"
R"(
(lldb) command alias bfl breakpoint set -f %1 -l %2
(lldb) bfl my-file.c 137
This would be the same as if the user had entered 'breakpoint set -f my-file.c -l 137'.
Another example:
(lldb) command alias pltty process launch -s -o %1 -e %1
(lldb) pltty /dev/tty0
Interpreted as 'process launch -s -o /dev/tty0 -e /dev/tty0'
)"
"If the user always wanted to pass the same value to a particular option, the \
alias could be defined with that value directly in the alias as a constant, \
rather than using a positional placeholder:"
R"(
(lldb) command alias bl3 breakpoint set -f %1 -l 3
Always sets a breakpoint on line 3 of whatever file is indicated.
)"
"If the alias abbreviation or the full alias command collides with another \
existing command, the command resolver will prefer to use the alias over any \
other command as far as there is only one alias command match.");
CommandArgumentEntry arg1;
CommandArgumentEntry arg2;
CommandArgumentEntry arg3;
CommandArgumentData alias_arg;
CommandArgumentData cmd_arg;
CommandArgumentData options_arg;
// Define the first (and only) variant of this arg.
alias_arg.arg_type = eArgTypeAliasName;
alias_arg.arg_repetition = eArgRepeatPlain;
// There is only one variant this argument could be; put it into the
// argument entry.
arg1.push_back(alias_arg);
// Define the first (and only) variant of this arg.
cmd_arg.arg_type = eArgTypeCommandName;
cmd_arg.arg_repetition = eArgRepeatPlain;
// There is only one variant this argument could be; put it into the
// argument entry.
arg2.push_back(cmd_arg);
// Define the first (and only) variant of this arg.
options_arg.arg_type = eArgTypeAliasOptions;
options_arg.arg_repetition = eArgRepeatOptional;
// There is only one variant this argument could be; put it into the
// argument entry.
arg3.push_back(options_arg);
// Push the data for the first argument into the m_arguments vector.
m_arguments.push_back(arg1);
m_arguments.push_back(arg2);
m_arguments.push_back(arg3);
}
~CommandObjectCommandsAlias() override = default;
protected:
void DoExecute(llvm::StringRef raw_command_line,
CommandReturnObject &result) override {
if (raw_command_line.empty()) {
result.AppendError("'command alias' requires at least two arguments");
return;
}
ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext();
m_option_group.NotifyOptionParsingStarting(&exe_ctx);
OptionsWithRaw args_with_suffix(raw_command_line);
if (args_with_suffix.HasArgs())
if (!ParseOptionsAndNotify(args_with_suffix.GetArgs(), result,
m_option_group, exe_ctx))
return;
llvm::StringRef raw_command_string = args_with_suffix.GetRawPart();
Args args(raw_command_string);
if (args.GetArgumentCount() < 2) {
result.AppendError("'command alias' requires at least two arguments");
return;
}
// Get the alias command.
auto alias_command = args[0].ref();
if (alias_command.starts_with("-")) {
result.AppendError("aliases starting with a dash are not supported");
if (alias_command == "--help" || alias_command == "--long-help") {
result.AppendWarning("if trying to pass options to 'command alias' add "
"a -- at the end of the options");
}
return;
}
// Strip the new alias name off 'raw_command_string' (leave it on args,
// which gets passed to 'Execute', which does the stripping itself.
size_t pos = raw_command_string.find(alias_command);
if (pos == 0) {
raw_command_string = raw_command_string.substr(alias_command.size());
pos = raw_command_string.find_first_not_of(' ');
if ((pos != std::string::npos) && (pos > 0))
raw_command_string = raw_command_string.substr(pos);
} else {
result.AppendError("Error parsing command string. No alias created.");
return;
}
// Verify that the command is alias-able.
if (m_interpreter.CommandExists(alias_command)) {
result.AppendErrorWithFormat(
"'%s' is a permanent debugger command and cannot be redefined.\n",
args[0].c_str());
return;
}
if (m_interpreter.UserMultiwordCommandExists(alias_command)) {
result.AppendErrorWithFormat(
"'%s' is a user container command and cannot be overwritten.\n"
"Delete it first with 'command container delete'\n",
args[0].c_str());
return;
}
// Get CommandObject that is being aliased. The command name is read from
// the front of raw_command_string. raw_command_string is returned with the
// name of the command object stripped off the front.
llvm::StringRef original_raw_command_string = raw_command_string;
CommandObject *cmd_obj =
m_interpreter.GetCommandObjectForCommand(raw_command_string);
if (!cmd_obj) {
result.AppendErrorWithFormat("invalid command given to 'command alias'. "
"'%s' does not begin with a valid command."
" No alias created.",
original_raw_command_string.str().c_str());
} else if (!cmd_obj->WantsRawCommandString()) {
// Note that args was initialized with the original command, and has not
// been updated to this point. Therefore can we pass it to the version of
// Execute that does not need/expect raw input in the alias.
HandleAliasingNormalCommand(args, result);
} else {
HandleAliasingRawCommand(alias_command, raw_command_string, *cmd_obj,
result);
}
}
bool HandleAliasingRawCommand(llvm::StringRef alias_command,
llvm::StringRef raw_command_string,
CommandObject &cmd_obj,
CommandReturnObject &result) {
// Verify & handle any options/arguments passed to the alias command
OptionArgVectorSP option_arg_vector_sp =
OptionArgVectorSP(new OptionArgVector);
const bool include_aliases = true;
// Look up the command using command's name first. This is to resolve
// aliases when you are making nested aliases. But if you don't find
// it that way, then it wasn't an alias and we can just use the object
// we were passed in.
CommandObjectSP cmd_obj_sp = m_interpreter.GetCommandSPExact(
cmd_obj.GetCommandName(), include_aliases);
if (!cmd_obj_sp)
cmd_obj_sp = cmd_obj.shared_from_this();
if (m_interpreter.AliasExists(alias_command) ||
m_interpreter.UserCommandExists(alias_command)) {
result.AppendWarningWithFormat(
"Overwriting existing definition for '%s'.\n",
alias_command.str().c_str());
}
if (CommandAlias *alias = m_interpreter.AddAlias(
alias_command, cmd_obj_sp, raw_command_string)) {
if (m_command_options.m_help.OptionWasSet())
alias->SetHelp(m_command_options.m_help.GetCurrentValue());
if (m_command_options.m_long_help.OptionWasSet())
alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
result.SetStatus(eReturnStatusSuccessFinishNoResult);
} else {
result.AppendError("Unable to create requested alias.\n");
}
return result.Succeeded();
}
bool HandleAliasingNormalCommand(Args &args, CommandReturnObject &result) {
size_t argc = args.GetArgumentCount();
if (argc < 2) {
result.AppendError("'command alias' requires at least two arguments");
return false;
}
// Save these in std::strings since we're going to shift them off.
const std::string alias_command(std::string(args[0].ref()));
const std::string actual_command(std::string(args[1].ref()));
args.Shift(); // Shift the alias command word off the argument vector.
args.Shift(); // Shift the old command word off the argument vector.
// Verify that the command is alias'able, and get the appropriate command
// object.
if (m_interpreter.CommandExists(alias_command)) {
result.AppendErrorWithFormat(
"'%s' is a permanent debugger command and cannot be redefined.\n",
alias_command.c_str());
return false;
}
if (m_interpreter.UserMultiwordCommandExists(alias_command)) {
result.AppendErrorWithFormat(
"'%s' is user container command and cannot be overwritten.\n"
"Delete it first with 'command container delete'",
alias_command.c_str());
return false;
}
CommandObjectSP command_obj_sp(
m_interpreter.GetCommandSPExact(actual_command, true));
CommandObjectSP subcommand_obj_sp;
bool use_subcommand = false;
if (!command_obj_sp) {
result.AppendErrorWithFormat("'%s' is not an existing command.\n",
actual_command.c_str());
return false;
}
CommandObject *cmd_obj = command_obj_sp.get();
CommandObject *sub_cmd_obj = nullptr;
OptionArgVectorSP option_arg_vector_sp =
OptionArgVectorSP(new OptionArgVector);
while (cmd_obj->IsMultiwordObject() && !args.empty()) {
auto sub_command = args[0].ref();
assert(!sub_command.empty());
subcommand_obj_sp = cmd_obj->GetSubcommandSP(sub_command);
if (!subcommand_obj_sp) {
result.AppendErrorWithFormat(
"'%s' is not a valid sub-command of '%s'. "
"Unable to create alias.\n",
args[0].c_str(), actual_command.c_str());
return false;
}
sub_cmd_obj = subcommand_obj_sp.get();
use_subcommand = true;
args.Shift(); // Shift the sub_command word off the argument vector.
cmd_obj = sub_cmd_obj;
}
// Verify & handle any options/arguments passed to the alias command
std::string args_string;
if (!args.empty()) {
CommandObjectSP tmp_sp =
m_interpreter.GetCommandSPExact(cmd_obj->GetCommandName());
if (use_subcommand)
tmp_sp = m_interpreter.GetCommandSPExact(sub_cmd_obj->GetCommandName());
args.GetCommandString(args_string);
}
if (m_interpreter.AliasExists(alias_command) ||
m_interpreter.UserCommandExists(alias_command)) {
result.AppendWarningWithFormat(
"Overwriting existing definition for '%s'.\n", alias_command.c_str());
}
if (CommandAlias *alias = m_interpreter.AddAlias(
alias_command, use_subcommand ? subcommand_obj_sp : command_obj_sp,
args_string)) {
if (m_command_options.m_help.OptionWasSet())
alias->SetHelp(m_command_options.m_help.GetCurrentValue());
if (m_command_options.m_long_help.OptionWasSet())
alias->SetHelpLong(m_command_options.m_long_help.GetCurrentValue());
result.SetStatus(eReturnStatusSuccessFinishNoResult);
} else {
result.AppendError("Unable to create requested alias.\n");
return false;
}
return result.Succeeded();
}
};
#pragma mark CommandObjectCommandsUnalias
// CommandObjectCommandsUnalias
class CommandObjectCommandsUnalias : public CommandObjectParsed {
public:
CommandObjectCommandsUnalias(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "command unalias",
"Delete one or more custom commands defined by 'command alias'.",
nullptr) {
AddSimpleArgumentList(eArgTypeAliasName);
}
~CommandObjectCommandsUnalias() override = default;
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
if (!m_interpreter.HasCommands() || request.GetCursorIndex() != 0)
return;
for (const auto &ent : m_interpreter.GetAliases()) {
request.TryCompleteCurrentArg(ent.first, ent.second->GetHelp());
}
}
protected:
void DoExecute(Args &args, CommandReturnObject &result) override {
CommandObject::CommandMap::iterator pos;
CommandObject *cmd_obj;
if (args.empty()) {
result.AppendError("must call 'unalias' with a valid alias");
return;
}
auto command_name = args[0].ref();
cmd_obj = m_interpreter.GetCommandObject(command_name);
if (!cmd_obj) {
result.AppendErrorWithFormat(
"'%s' is not a known command.\nTry 'help' to see a "
"current list of commands.\n",
args[0].c_str());
return;
}
if (m_interpreter.CommandExists(command_name)) {
if (cmd_obj->IsRemovable()) {
result.AppendErrorWithFormat(
"'%s' is not an alias, it is a debugger command which can be "
"removed using the 'command delete' command.\n",
args[0].c_str());
} else {
result.AppendErrorWithFormat(
"'%s' is a permanent debugger command and cannot be removed.\n",
args[0].c_str());
}
return;
}
if (!m_interpreter.RemoveAlias(command_name)) {
if (m_interpreter.AliasExists(command_name))
result.AppendErrorWithFormat(
"Error occurred while attempting to unalias '%s'.\n",
args[0].c_str());
else
result.AppendErrorWithFormat("'%s' is not an existing alias.\n",
args[0].c_str());
return;
}
result.SetStatus(eReturnStatusSuccessFinishNoResult);
}
};
#pragma mark CommandObjectCommandsDelete
// CommandObjectCommandsDelete
class CommandObjectCommandsDelete : public CommandObjectParsed {
public:
CommandObjectCommandsDelete(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "command delete",
"Delete one or more custom commands defined by 'command regex'.",
nullptr) {
AddSimpleArgumentList(eArgTypeCommandName);
}
~CommandObjectCommandsDelete() override = default;
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
if (!m_interpreter.HasCommands() || request.GetCursorIndex() != 0)
return;
for (const auto &ent : m_interpreter.GetCommands()) {
if (ent.second->IsRemovable())
request.TryCompleteCurrentArg(ent.first, ent.second->GetHelp());
}
}
protected:
void DoExecute(Args &args, CommandReturnObject &result) override {
CommandObject::CommandMap::iterator pos;
if (args.empty()) {
result.AppendErrorWithFormat("must call '%s' with one or more valid user "
"defined regular expression command names",
GetCommandName().str().c_str());
return;
}
auto command_name = args[0].ref();
if (!m_interpreter.CommandExists(command_name)) {
StreamString error_msg_stream;
const bool generate_upropos = true;
const bool generate_type_lookup = false;
CommandObjectHelp::GenerateAdditionalHelpAvenuesMessage(
&error_msg_stream, command_name, llvm::StringRef(), llvm::StringRef(),
generate_upropos, generate_type_lookup);
result.AppendError(error_msg_stream.GetString());
return;
}
if (!m_interpreter.RemoveCommand(command_name)) {
result.AppendErrorWithFormat(
"'%s' is a permanent debugger command and cannot be removed.\n",
args[0].c_str());
return;
}
result.SetStatus(eReturnStatusSuccessFinishNoResult);
}
};
// CommandObjectCommandsAddRegex
#define LLDB_OPTIONS_regex
#include "CommandOptions.inc"
#pragma mark CommandObjectCommandsAddRegex
class CommandObjectCommandsAddRegex : public CommandObjectParsed,
public IOHandlerDelegateMultiline {
public:
CommandObjectCommandsAddRegex(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "command regex",
"Define a custom command in terms of "
"existing commands by matching "
"regular expressions.",
"command regex <cmd-name> [s/<regex>/<subst>/ ...]"),
IOHandlerDelegateMultiline("",
IOHandlerDelegate::Completion::LLDBCommand) {
SetHelpLong(
R"(
)"
"This command allows the user to create powerful regular expression commands \
with substitutions. The regular expressions and substitutions are specified \
using the regular expression substitution format of:"
R"(
s/<regex>/<subst>/
)"
"<regex> is a regular expression that can use parenthesis to capture regular \
expression input and substitute the captured matches in the output using %1 \
for the first match, %2 for the second, and so on."
R"(
)"
"The regular expressions can all be specified on the command line if more than \
one argument is provided. If just the command name is provided on the command \
line, then the regular expressions and substitutions can be entered on separate \
lines, followed by an empty line to terminate the command definition."
R"(
EXAMPLES
)"
"The following example will define a regular expression command named 'f' that \
will call 'finish' if there are no arguments, or 'frame select <frame-idx>' if \
a number follows 'f':"
R"(
(lldb) command regex f s/^$/finish/ 's/([0-9]+)/frame select %1/')");
AddSimpleArgumentList(eArgTypeSEDStylePair, eArgRepeatOptional);
}
~CommandObjectCommandsAddRegex() override = default;
protected:
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
if (output_sp && interactive) {
output_sp->PutCString("Enter one or more sed substitution commands in "
"the form: 's/<regex>/<subst>/'.\nTerminate the "
"substitution list with an empty line.\n");
output_sp->Flush();
}
}
void IOHandlerInputComplete(IOHandler &io_handler,
std::string &data) override {
io_handler.SetIsDone(true);
if (m_regex_cmd_up) {
StringList lines;
if (lines.SplitIntoLines(data)) {
bool check_only = false;
for (const std::string &line : lines) {
Status error = AppendRegexSubstitution(line, check_only);
if (error.Fail()) {
if (!GetDebugger().GetCommandInterpreter().GetBatchCommandMode()) {
StreamSP out_stream = GetDebugger().GetAsyncOutputStream();
out_stream->Printf("error: %s\n", error.AsCString());
}
}
}
}
if (m_regex_cmd_up->HasRegexEntries()) {
CommandObjectSP cmd_sp(m_regex_cmd_up.release());
m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
}
}
}
void DoExecute(Args &command, CommandReturnObject &result) override {
const size_t argc = command.GetArgumentCount();
if (argc == 0) {
result.AppendError("usage: 'command regex <command-name> "
"[s/<regex1>/<subst1>/ s/<regex2>/<subst2>/ ...]'\n");
return;
}
Status error;
auto name = command[0].ref();
m_regex_cmd_up = std::make_unique<CommandObjectRegexCommand>(
m_interpreter, name, m_options.GetHelp(), m_options.GetSyntax(), 0,
true);
if (argc == 1) {
Debugger &debugger = GetDebugger();
bool color_prompt = debugger.GetUseColor();
const bool multiple_lines = true; // Get multiple lines
IOHandlerSP io_handler_sp(new IOHandlerEditline(
debugger, IOHandler::Type::Other,
"lldb-regex", // Name of input reader for history
llvm::StringRef("> "), // Prompt
llvm::StringRef(), // Continuation prompt
multiple_lines, color_prompt,
0, // Don't show line numbers
*this));
if (io_handler_sp) {
debugger.RunIOHandlerAsync(io_handler_sp);
result.SetStatus(eReturnStatusSuccessFinishNoResult);
}
} else {
for (auto &entry : command.entries().drop_front()) {
bool check_only = false;
error = AppendRegexSubstitution(entry.ref(), check_only);
if (error.Fail())
break;
}
if (error.Success()) {
AddRegexCommandToInterpreter();
}
}
if (error.Fail()) {
result.AppendError(error.AsCString());
}
}
Status AppendRegexSubstitution(const llvm::StringRef ®ex_sed,
bool check_only) {
Status error;
if (!m_regex_cmd_up) {
return Status::FromErrorStringWithFormat(
"invalid regular expression command object for: '%.*s'",
(int)regex_sed.size(), regex_sed.data());
return error;
}
size_t regex_sed_size = regex_sed.size();
if (regex_sed_size <= 1) {
return Status::FromErrorStringWithFormat(
"regular expression substitution string is too short: '%.*s'",
(int)regex_sed.size(), regex_sed.data());
return error;
}
if (regex_sed[0] != 's') {
return Status::FromErrorStringWithFormat(
"regular expression substitution string "
"doesn't start with 's': '%.*s'",
(int)regex_sed.size(), regex_sed.data());
return error;
}
const size_t first_separator_char_pos = 1;
// use the char that follows 's' as the regex separator character so we can
// have "s/<regex>/<subst>/" or "s|<regex>|<subst>|"
const char separator_char = regex_sed[first_separator_char_pos];
const size_t second_separator_char_pos =
regex_sed.find(separator_char, first_separator_char_pos + 1);
if (second_separator_char_pos == std::string::npos) {
return Status::FromErrorStringWithFormat(
"missing second '%c' separator char after '%.*s' in '%.*s'",
separator_char,
(int)(regex_sed.size() - first_separator_char_pos - 1),
regex_sed.data() + (first_separator_char_pos + 1),
(int)regex_sed.size(), regex_sed.data());
return error;
}
const size_t third_separator_char_pos =
regex_sed.find(separator_char, second_separator_char_pos + 1);
if (third_separator_char_pos == std::string::npos) {
return Status::FromErrorStringWithFormat(
"missing third '%c' separator char after '%.*s' in '%.*s'",
separator_char,
(int)(regex_sed.size() - second_separator_char_pos - 1),
regex_sed.data() + (second_separator_char_pos + 1),
(int)regex_sed.size(), regex_sed.data());
return error;
}
if (third_separator_char_pos != regex_sed_size - 1) {
// Make sure that everything that follows the last regex separator char
if (regex_sed.find_first_not_of("\t\n\v\f\r ",
third_separator_char_pos + 1) !=
std::string::npos) {
return Status::FromErrorStringWithFormat(
"extra data found after the '%.*s' regular expression substitution "
"string: '%.*s'",
(int)third_separator_char_pos + 1, regex_sed.data(),
(int)(regex_sed.size() - third_separator_char_pos - 1),
regex_sed.data() + (third_separator_char_pos + 1));
return error;
}
} else if (first_separator_char_pos + 1 == second_separator_char_pos) {
return Status::FromErrorStringWithFormat(
"<regex> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
separator_char, separator_char, separator_char, (int)regex_sed.size(),
regex_sed.data());
return error;
} else if (second_separator_char_pos + 1 == third_separator_char_pos) {
return Status::FromErrorStringWithFormat(
"<subst> can't be empty in 's%c<regex>%c<subst>%c' string: '%.*s'",
separator_char, separator_char, separator_char, (int)regex_sed.size(),
regex_sed.data());
return error;
}
if (!check_only) {
std::string regex(std::string(regex_sed.substr(
first_separator_char_pos + 1,
second_separator_char_pos - first_separator_char_pos - 1)));
std::string subst(std::string(regex_sed.substr(
second_separator_char_pos + 1,
third_separator_char_pos - second_separator_char_pos - 1)));
m_regex_cmd_up->AddRegexCommand(regex, subst);
}
return error;
}
void AddRegexCommandToInterpreter() {
if (m_regex_cmd_up) {
if (m_regex_cmd_up->HasRegexEntries()) {
CommandObjectSP cmd_sp(m_regex_cmd_up.release());
m_interpreter.AddCommand(cmd_sp->GetCommandName(), cmd_sp, true);
}
}
}
private:
std::unique_ptr<CommandObjectRegexCommand> m_regex_cmd_up;
class CommandOptions : public Options {
public:
CommandOptions() = default;
~CommandOptions() override = default;
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
ExecutionContext *execution_context) override {
Status error;
const int short_option = m_getopt_table[option_idx].val;
switch (short_option) {
case 'h':
m_help.assign(std::string(option_arg));
break;
case 's':
m_syntax.assign(std::string(option_arg));
break;
default:
llvm_unreachable("Unimplemented option");
}
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
m_help.clear();
m_syntax.clear();
}
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
return llvm::ArrayRef(g_regex_options);
}
llvm::StringRef GetHelp() { return m_help; }
llvm::StringRef GetSyntax() { return m_syntax; }
protected:
// Instance variables to hold the values for command options.
std::string m_help;
std::string m_syntax;
};
Options *GetOptions() override { return &m_options; }
CommandOptions m_options;
};
class CommandObjectPythonFunction : public CommandObjectRaw {
public:
CommandObjectPythonFunction(CommandInterpreter &interpreter, std::string name,
std::string funct, std::string help,
ScriptedCommandSynchronicity synch,
CompletionType completion_type)
: CommandObjectRaw(interpreter, name), m_function_name(funct),
m_synchro(synch), m_completion_type(completion_type) {
if (!help.empty())
SetHelp(help);
else {
StreamString stream;
stream.Printf("For more information run 'help %s'", name.c_str());
SetHelp(stream.GetString());
}
}
~CommandObjectPythonFunction() override = default;
bool IsRemovable() const override { return true; }
const std::string &GetFunctionName() { return m_function_name; }
ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
llvm::StringRef GetHelpLong() override {
if (m_fetched_help_long)
return CommandObjectRaw::GetHelpLong();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return CommandObjectRaw::GetHelpLong();
std::string docstring;
m_fetched_help_long =
scripter->GetDocumentationForItem(m_function_name.c_str(), docstring);
if (!docstring.empty())
SetHelpLong(docstring);
return CommandObjectRaw::GetHelpLong();
}
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), m_completion_type, request, nullptr);
}
bool WantsCompletion() override { return true; }
protected:
void DoExecute(llvm::StringRef raw_command_line,
CommandReturnObject &result) override {
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
m_interpreter.IncreaseCommandUsage(*this);
Status error;
result.SetStatus(eReturnStatusInvalid);
if (!scripter || !scripter->RunScriptBasedCommand(
m_function_name.c_str(), raw_command_line, m_synchro,
result, error, m_exe_ctx)) {
result.AppendError(error.AsCString());
} else {
// Don't change the status if the command already set it...
if (result.GetStatus() == eReturnStatusInvalid) {
if (result.GetOutputString().empty())
result.SetStatus(eReturnStatusSuccessFinishNoResult);
else
result.SetStatus(eReturnStatusSuccessFinishResult);
}
}
}
private:
std::string m_function_name;
ScriptedCommandSynchronicity m_synchro;
bool m_fetched_help_long = false;
CompletionType m_completion_type = eNoCompletion;
};
/// This class implements a "raw" scripted command. lldb does no parsing of the
/// command line, instead passing the line unaltered (except for backtick
/// substitution).
class CommandObjectScriptingObjectRaw : public CommandObjectRaw {
public:
CommandObjectScriptingObjectRaw(CommandInterpreter &interpreter,
std::string name,
StructuredData::GenericSP cmd_obj_sp,
ScriptedCommandSynchronicity synch,
CompletionType completion_type)
: CommandObjectRaw(interpreter, name), m_cmd_obj_sp(cmd_obj_sp),
m_synchro(synch), m_fetched_help_short(false),
m_fetched_help_long(false), m_completion_type(completion_type) {
StreamString stream;
stream.Printf("For more information run 'help %s'", name.c_str());
SetHelp(stream.GetString());
if (ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter())
GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
}
~CommandObjectScriptingObjectRaw() override = default;
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
GetCommandInterpreter(), m_completion_type, request, nullptr);
}
bool WantsCompletion() override { return true; }
bool IsRemovable() const override { return true; }
ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
std::optional<std::string> GetRepeatCommand(Args &args,
uint32_t index) override {
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return std::nullopt;
return scripter->GetRepeatCommandForScriptedCommand(m_cmd_obj_sp, args);
}
llvm::StringRef GetHelp() override {
if (m_fetched_help_short)
return CommandObjectRaw::GetHelp();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return CommandObjectRaw::GetHelp();
std::string docstring;
m_fetched_help_short =
scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
if (!docstring.empty())
SetHelp(docstring);
return CommandObjectRaw::GetHelp();
}
llvm::StringRef GetHelpLong() override {
if (m_fetched_help_long)
return CommandObjectRaw::GetHelpLong();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return CommandObjectRaw::GetHelpLong();
std::string docstring;
m_fetched_help_long =
scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
if (!docstring.empty())
SetHelpLong(docstring);
return CommandObjectRaw::GetHelpLong();
}
protected:
void DoExecute(llvm::StringRef raw_command_line,
CommandReturnObject &result) override {
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
Status error;
result.SetStatus(eReturnStatusInvalid);
if (!scripter ||
!scripter->RunScriptBasedCommand(m_cmd_obj_sp, raw_command_line,
m_synchro, result, error, m_exe_ctx)) {
result.AppendError(error.AsCString());
} else {
// Don't change the status if the command already set it...
if (result.GetStatus() == eReturnStatusInvalid) {
if (result.GetOutputString().empty())
result.SetStatus(eReturnStatusSuccessFinishNoResult);
else
result.SetStatus(eReturnStatusSuccessFinishResult);
}
}
}
private:
StructuredData::GenericSP m_cmd_obj_sp;
ScriptedCommandSynchronicity m_synchro;
bool m_fetched_help_short : 1;
bool m_fetched_help_long : 1;
CompletionType m_completion_type = eNoCompletion;
};
/// This command implements a lldb parsed scripted command. The command
/// provides a definition of the options and arguments, and a option value
/// setting callback, and then the command's execution function gets passed
/// just the parsed arguments.
/// Note, implementing a command in Python using these base interfaces is a bit
/// of a pain, but it is much easier to export this low level interface, and
/// then make it nicer on the Python side, than to try to do that in a
/// script language neutral way.
/// So I've also added a base class in Python that provides a table-driven
/// way of defining the options and arguments, which automatically fills the
/// option values, making them available as properties in Python.
///
class CommandObjectScriptingObjectParsed : public CommandObjectParsed {
private:
class CommandOptions : public Options {
public:
CommandOptions(CommandInterpreter &interpreter,
StructuredData::GenericSP cmd_obj_sp) : m_interpreter(interpreter),
m_cmd_obj_sp(cmd_obj_sp) {}
~CommandOptions() override = default;
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
ExecutionContext *execution_context) override {
Status error;
ScriptInterpreter *scripter =
m_interpreter.GetDebugger().GetScriptInterpreter();
if (!scripter) {
return Status::FromErrorString(
"No script interpreter for SetOptionValue.");
return error;
}
if (!m_cmd_obj_sp) {
return Status::FromErrorString(
"SetOptionValue called with empty cmd_obj.");
return error;
}
if (!m_options_definition_up) {
return Status::FromErrorString(
"SetOptionValue called before options definitions "
"were created.");
return error;
}
// Pass the long option, since you aren't actually required to have a
// short_option, and for those options the index or short option character
// aren't meaningful on the python side.
const char * long_option =
m_options_definition_up.get()[option_idx].long_option;
bool success = scripter->SetOptionValueForCommandObject(m_cmd_obj_sp,
execution_context, long_option, option_arg);
if (!success)
return Status::FromErrorStringWithFormatv(
"Error setting option: {0} to {1}", long_option, option_arg);
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
ScriptInterpreter *scripter =
m_interpreter.GetDebugger().GetScriptInterpreter();
if (!scripter || !m_cmd_obj_sp)
return;
scripter->OptionParsingStartedForCommandObject(m_cmd_obj_sp);
}
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
if (!m_options_definition_up)
return {};
return llvm::ArrayRef(m_options_definition_up.get(), m_num_options);
}
static Status ParseUsageMaskFromArray(StructuredData::ObjectSP obj_sp,
size_t counter, uint32_t &usage_mask) {
// If the usage entry is not provided, we use LLDB_OPT_SET_ALL.
// If the usage mask is a UINT, the option belongs to that group.
// If the usage mask is a vector of UINT's, the option belongs to all the
// groups listed.
// If a subelement of the vector is a vector of two ints, then the option
// belongs to the inclusive range from the first to the second element.
Status error;
if (!obj_sp) {
usage_mask = LLDB_OPT_SET_ALL;
return error;
}
usage_mask = 0;
StructuredData::UnsignedInteger *uint_val =
obj_sp->GetAsUnsignedInteger();
if (uint_val) {
// If this is an integer, then this specifies a single group:
uint32_t value = uint_val->GetValue();
if (value == 0) {
return Status::FromErrorStringWithFormatv(
"0 is not a valid group for option {0}", counter);
}
usage_mask = (1 << (value - 1));
return error;
}
// Otherwise it has to be an array:
StructuredData::Array *array_val = obj_sp->GetAsArray();
if (!array_val) {
return Status::FromErrorStringWithFormatv(
"required field is not a array for option {0}", counter);
}
// This is the array ForEach for accumulating a group usage mask from
// an array of string descriptions of groups.
auto groups_accumulator
= [counter, &usage_mask, &error]
(StructuredData::Object *obj) -> bool {
StructuredData::UnsignedInteger *int_val = obj->GetAsUnsignedInteger();
if (int_val) {
uint32_t value = int_val->GetValue();
if (value == 0) {
error = Status::FromErrorStringWithFormatv(
"0 is not a valid group for element {0}", counter);
return false;
}
usage_mask |= (1 << (value - 1));
return true;
}
StructuredData::Array *arr_val = obj->GetAsArray();
if (!arr_val) {
error = Status::FromErrorStringWithFormatv(
"Group element not an int or array of integers for element {0}",
counter);
return false;
}
size_t num_range_elem = arr_val->GetSize();
if (num_range_elem != 2) {
error = Status::FromErrorStringWithFormatv(
"Subranges of a group not a start and a stop for element {0}",
counter);
return false;
}
int_val = arr_val->GetItemAtIndex(0)->GetAsUnsignedInteger();
if (!int_val) {
error = Status::FromErrorStringWithFormatv(
"Start element of a subrange of a "
"group not unsigned int for element {0}",
counter);
return false;
}
uint32_t start = int_val->GetValue();
int_val = arr_val->GetItemAtIndex(1)->GetAsUnsignedInteger();
if (!int_val) {
error = Status::FromErrorStringWithFormatv(
"End element of a subrange of a group"
" not unsigned int for element {0}",
counter);
return false;
}
uint32_t end = int_val->GetValue();
if (start == 0 || end == 0 || start > end) {
error = Status::FromErrorStringWithFormatv(
"Invalid subrange of a group: {0} - "
"{1} for element {2}",
start, end, counter);
return false;
}
for (uint32_t i = start; i <= end; i++) {
usage_mask |= (1 << (i - 1));
}
return true;
};
array_val->ForEach(groups_accumulator);
return error;
}
Status SetOptionsFromArray(StructuredData::Dictionary &options) {
Status error;
m_num_options = options.GetSize();
m_options_definition_up.reset(new OptionDefinition[m_num_options]);
// We need to hand out pointers to contents of these vectors; we reserve
// as much as we'll need up front so they don't get freed on resize...
m_usage_container.resize(m_num_options);
m_enum_storage.resize(m_num_options);
m_enum_vector.resize(m_num_options);
size_t counter = 0;
size_t short_opt_counter = 0;
// This is the Array::ForEach function for adding option elements:
auto add_element = [this, &error, &counter, &short_opt_counter]
(llvm::StringRef long_option, StructuredData::Object *object) -> bool {
StructuredData::Dictionary *opt_dict = object->GetAsDictionary();
if (!opt_dict) {
error = Status::FromErrorString(
"Value in options dictionary is not a dictionary");
return false;
}
OptionDefinition &option_def = m_options_definition_up.get()[counter];
// We aren't exposing the validator yet, set it to null
option_def.validator = nullptr;
// We don't require usage masks, so set it to one group by default:
option_def.usage_mask = 1;
// Now set the fields of the OptionDefinition Array from the dictionary:
//
// Note that I don't check for unknown fields in the option dictionaries
// so a scriptor can add extra elements that are helpful when they go to
// do "set_option_value"
// Usage Mask:
StructuredData::ObjectSP obj_sp = opt_dict->GetValueForKey("groups");
if (obj_sp) {
error = ParseUsageMaskFromArray(obj_sp, counter,
option_def.usage_mask);
if (error.Fail())
return false;
}
// Required:
option_def.required = false;
obj_sp = opt_dict->GetValueForKey("required");
if (obj_sp) {
StructuredData::Boolean *boolean_val = obj_sp->GetAsBoolean();
if (!boolean_val) {
error = Status::FromErrorStringWithFormatv(
"'required' field is not a boolean "
"for option {0}",
counter);
return false;
}
option_def.required = boolean_val->GetValue();
}
// Short Option:
int short_option;
obj_sp = opt_dict->GetValueForKey("short_option");
if (obj_sp) {
// The value is a string, so pull the
llvm::StringRef short_str = obj_sp->GetStringValue();
if (short_str.empty()) {
error = Status::FromErrorStringWithFormatv(
"short_option field empty for "
"option {0}",
counter);
return false;
} else if (short_str.size() != 1) {
error = Status::FromErrorStringWithFormatv(
"short_option field has extra "
"characters for option {0}",
counter);
return false;
}
short_option = (int) short_str[0];
} else {
// If the short option is not provided, then we need a unique value
// less than the lowest printable ASCII character.
short_option = short_opt_counter++;
}
option_def.short_option = short_option;
// Long Option is the key from the outer dict:
if (long_option.empty()) {
error = Status::FromErrorStringWithFormatv(
"empty long_option for option {0}", counter);
return false;
}
auto inserted = g_string_storer.insert(long_option.str());
option_def.long_option = ((*(inserted.first)).data());
// Value Type:
obj_sp = opt_dict->GetValueForKey("value_type");
if (obj_sp) {
StructuredData::UnsignedInteger *uint_val
= obj_sp->GetAsUnsignedInteger();
if (!uint_val) {
error = Status::FromErrorStringWithFormatv(
"Value type must be an unsigned "
"integer");
return false;
}
uint64_t val_type = uint_val->GetValue();
if (val_type >= eArgTypeLastArg) {
error =
Status::FromErrorStringWithFormatv("Value type {0} beyond the "
"CommandArgumentType bounds",
val_type);
return false;
}
option_def.argument_type = (CommandArgumentType) val_type;
option_def.option_has_arg = true;
} else {
option_def.argument_type = eArgTypeNone;
option_def.option_has_arg = false;
}
// Completion Type:
obj_sp = opt_dict->GetValueForKey("completion_type");
if (obj_sp) {
StructuredData::UnsignedInteger *uint_val = obj_sp->GetAsUnsignedInteger();
if (!uint_val) {
error = Status::FromErrorStringWithFormatv(
"Completion type must be an "
"unsigned integer for option {0}",
counter);
return false;
}
uint64_t completion_type = uint_val->GetValue();
if (completion_type > eCustomCompletion) {
error = Status::FromErrorStringWithFormatv(
"Completion type for option {0} "
"beyond the CompletionType bounds",
completion_type);
return false;
}
option_def.completion_type = (CommandArgumentType) completion_type;
} else
option_def.completion_type = eNoCompletion;
// Usage Text:
std::string usage_text;
obj_sp = opt_dict->GetValueForKey("help");
if (!obj_sp) {
error = Status::FromErrorStringWithFormatv(
"required usage missing from option "
"{0}",
counter);
return false;
}
llvm::StringRef usage_stref;
usage_stref = obj_sp->GetStringValue();
if (usage_stref.empty()) {
error = Status::FromErrorStringWithFormatv(
"empty usage text for option {0}", counter);
return false;
}
m_usage_container[counter] = usage_stref.str().c_str();
option_def.usage_text = m_usage_container[counter].data();
// Enum Values:
obj_sp = opt_dict->GetValueForKey("enum_values");
if (obj_sp) {
StructuredData::Array *array = obj_sp->GetAsArray();
if (!array) {
error = Status::FromErrorStringWithFormatv(
"enum values must be an array for "
"option {0}",
counter);
return false;
}
size_t num_elem = array->GetSize();
size_t enum_ctr = 0;
m_enum_storage[counter] = std::vector<EnumValueStorage>(num_elem);
std::vector<EnumValueStorage> &curr_elem = m_enum_storage[counter];
// This is the Array::ForEach function for adding enum elements:
// Since there are only two fields to specify the enum, use a simple
// two element array with value first, usage second.
// counter is only used for reporting so I pass it by value here.
auto add_enum = [&enum_ctr, &curr_elem, counter, &error]
(StructuredData::Object *object) -> bool {
StructuredData::Array *enum_arr = object->GetAsArray();
if (!enum_arr) {
error = Status::FromErrorStringWithFormatv(
"Enum values for option {0} not "
"an array",
counter);
return false;
}
size_t num_enum_elements = enum_arr->GetSize();
if (num_enum_elements != 2) {
error = Status::FromErrorStringWithFormatv(
"Wrong number of elements: {0} "
"for enum {1} in option {2}",
num_enum_elements, enum_ctr, counter);
return false;
}
// Enum Value:
StructuredData::ObjectSP obj_sp = enum_arr->GetItemAtIndex(0);
llvm::StringRef val_stref = obj_sp->GetStringValue();
std::string value_cstr_str = val_stref.str().c_str();
// Enum Usage:
obj_sp = enum_arr->GetItemAtIndex(1);
if (!obj_sp) {
error = Status::FromErrorStringWithFormatv(
"No usage for enum {0} in option "
"{1}",
enum_ctr, counter);
return false;
}
llvm::StringRef usage_stref = obj_sp->GetStringValue();
std::string usage_cstr_str = usage_stref.str().c_str();
curr_elem[enum_ctr] = EnumValueStorage(value_cstr_str,
usage_cstr_str, enum_ctr);
enum_ctr++;
return true;
}; // end of add_enum
array->ForEach(add_enum);
if (!error.Success())
return false;
// We have to have a vector of elements to set in the options, make
// that here:
for (auto &elem : curr_elem)
m_enum_vector[counter].emplace_back(elem.element);
option_def.enum_values = llvm::ArrayRef(m_enum_vector[counter]);
}
counter++;
return true;
}; // end of add_element
options.ForEach(add_element);
return error;
}
size_t GetNumOptions() { return m_num_options; }
void PrepareOptionsForCompletion(CompletionRequest &request,
OptionElementVector &option_vec,
ExecutionContext *exe_ctx) {
// I'm not sure if we'll get into trouble doing an option parsing start
// and end in this context. If so, then I'll have to directly tell the
// scripter to do this.
OptionParsingStarting(exe_ctx);
auto opt_defs = GetDefinitions();
// Iterate through the options we found so far, and push them into
// the scripted side.
for (auto option_elem : option_vec) {
int cur_defs_index = option_elem.opt_defs_index;
// If we don't recognize this option we can't set it.
if (cur_defs_index == OptionArgElement::eUnrecognizedArg ||
cur_defs_index == OptionArgElement::eBareDash ||
cur_defs_index == OptionArgElement::eBareDoubleDash)
continue;
bool option_has_arg = opt_defs[cur_defs_index].option_has_arg;
llvm::StringRef cur_arg_value;
if (option_has_arg) {
int cur_arg_pos = option_elem.opt_arg_pos;
if (cur_arg_pos != OptionArgElement::eUnrecognizedArg &&
cur_arg_pos != OptionArgElement::eBareDash &&
cur_arg_pos != OptionArgElement::eBareDoubleDash) {
cur_arg_value =
request.GetParsedLine().GetArgumentAtIndex(cur_arg_pos);
}
}
SetOptionValue(cur_defs_index, cur_arg_value, exe_ctx);
}
OptionParsingFinished(exe_ctx);
}
void
ProcessCompletionDict(CompletionRequest &request,
StructuredData::DictionarySP &completion_dict_sp) {
// We don't know how to process an empty completion dict, our callers have
// to do that.
assert(completion_dict_sp && "Must have valid completion dict");
// First handle the case of a single completion:
llvm::StringRef completion;
// If the dictionary has one element "no-completion" then we return here
if (completion_dict_sp->GetValueForKeyAsString("no-completion",
completion))
return;
if (completion_dict_sp->GetValueForKeyAsString("completion",
completion)) {
llvm::StringRef mode_str;
CompletionMode mode = CompletionMode::Normal;
if (completion_dict_sp->GetValueForKeyAsString("mode", mode_str)) {
if (mode_str == "complete")
mode = CompletionMode::Normal;
else if (mode_str == "partial")
mode = CompletionMode::Partial;
else {
// FIXME - how do I report errors here?
return;
}
}
request.AddCompletion(completion, "", mode);
return;
}
// The completions are required, the descriptions are not:
StructuredData::Array *completions;
StructuredData::Array *descriptions;
if (completion_dict_sp->GetValueForKeyAsArray("values", completions)) {
completion_dict_sp->GetValueForKeyAsArray("descriptions", descriptions);
size_t num_completions = completions->GetSize();
for (size_t idx = 0; idx < num_completions; idx++) {
auto val = completions->GetItemAtIndexAsString(idx);
if (!val)
// FIXME: How do I report this error?
return;
if (descriptions) {
auto desc = descriptions->GetItemAtIndexAsString(idx);
request.AddCompletion(*val, desc ? *desc : "");
} else
request.AddCompletion(*val);
}
}
}
void
HandleOptionArgumentCompletion(lldb_private::CompletionRequest &request,
OptionElementVector &option_vec,
int opt_element_index,
CommandInterpreter &interpreter) override {
ScriptInterpreter *scripter =
interpreter.GetDebugger().GetScriptInterpreter();
if (!scripter)
return;
ExecutionContext exe_ctx = interpreter.GetExecutionContext();
PrepareOptionsForCompletion(request, option_vec, &exe_ctx);
auto defs = GetDefinitions();
size_t defs_index = option_vec[opt_element_index].opt_defs_index;
llvm::StringRef option_name = defs[defs_index].long_option;
bool is_enum = defs[defs_index].enum_values.size() != 0;
if (option_name.empty())
return;
// If this is an enum, we don't call the custom completer, just let the
// regular option completer handle that:
StructuredData::DictionarySP completion_dict_sp;
if (!is_enum)
completion_dict_sp =
scripter->HandleOptionArgumentCompletionForScriptedCommand(
m_cmd_obj_sp, option_name, request.GetCursorCharPos());
if (!completion_dict_sp) {
Options::HandleOptionArgumentCompletion(request, option_vec,
opt_element_index, interpreter);
return;
}
ProcessCompletionDict(request, completion_dict_sp);
}
private:
struct EnumValueStorage {
EnumValueStorage() {
element.string_value = "value not set";
element.usage = "usage not set";
element.value = 0;
}
EnumValueStorage(std::string in_str_val, std::string in_usage,
size_t in_value) : value(std::move(in_str_val)), usage(std::move(in_usage)) {
SetElement(in_value);
}
EnumValueStorage(const EnumValueStorage &in) : value(in.value),
usage(in.usage) {
SetElement(in.element.value);
}
EnumValueStorage &operator=(const EnumValueStorage &in) {
value = in.value;
usage = in.usage;
SetElement(in.element.value);
return *this;
}
void SetElement(size_t in_value) {
element.value = in_value;
element.string_value = value.data();
element.usage = usage.data();
}
std::string value;
std::string usage;
OptionEnumValueElement element;
};
// We have to provide char * values for the long option, usage and enum
// values, that's what the option definitions hold.
// The long option strings are quite likely to be reused in other added
// commands, so those are stored in a global set: g_string_storer.
// But the usages are much less likely to be reused, so those are stored in
// a vector in the command instance. It gets resized to the correct size
// and then filled with null-terminated strings in the std::string, so the
// are valid C-strings that won't move around.
// The enum values and descriptions are treated similarly - these aren't
// all that common so it's not worth the effort to dedup them.
size_t m_num_options = 0;
std::unique_ptr<OptionDefinition> m_options_definition_up;
std::vector<std::vector<EnumValueStorage>> m_enum_storage;
std::vector<std::vector<OptionEnumValueElement>> m_enum_vector;
std::vector<std::string> m_usage_container;
CommandInterpreter &m_interpreter;
StructuredData::GenericSP m_cmd_obj_sp;
static std::unordered_set<std::string> g_string_storer;
};
public:
static CommandObjectSP Create(CommandInterpreter &interpreter,
std::string name,
StructuredData::GenericSP cmd_obj_sp,
ScriptedCommandSynchronicity synch,
CommandReturnObject &result) {
CommandObjectSP new_cmd_sp(new CommandObjectScriptingObjectParsed(
interpreter, name, cmd_obj_sp, synch));
CommandObjectScriptingObjectParsed *parsed_cmd
= static_cast<CommandObjectScriptingObjectParsed *>(new_cmd_sp.get());
// Now check all the failure modes, and report if found.
Status opt_error = parsed_cmd->GetOptionsError();
Status arg_error = parsed_cmd->GetArgsError();
if (opt_error.Fail())
result.AppendErrorWithFormat("failed to parse option definitions: %s",
opt_error.AsCString());
if (arg_error.Fail())
result.AppendErrorWithFormat("%sfailed to parse argument definitions: %s",
opt_error.Fail() ? ", also " : "",
arg_error.AsCString());
if (!result.Succeeded())
return {};
return new_cmd_sp;
}
CommandObjectScriptingObjectParsed(CommandInterpreter &interpreter,
std::string name,
StructuredData::GenericSP cmd_obj_sp,
ScriptedCommandSynchronicity synch)
: CommandObjectParsed(interpreter, name.c_str()),
m_cmd_obj_sp(cmd_obj_sp), m_synchro(synch),
m_options(interpreter, cmd_obj_sp), m_fetched_help_short(false),
m_fetched_help_long(false) {
StreamString stream;
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter) {
m_options_error = Status::FromErrorString("No script interpreter");
return;
}
// Set the flags:
GetFlags().Set(scripter->GetFlagsForCommandObject(cmd_obj_sp));
// Now set up the options definitions from the options:
StructuredData::ObjectSP options_object_sp
= scripter->GetOptionsForCommandObject(cmd_obj_sp);
// It's okay not to have an options dict.
if (options_object_sp) {
// The options come as a dictionary of dictionaries. The key of the
// outer dict is the long option name (since that's required). The
// value holds all the other option specification bits.
StructuredData::Dictionary *options_dict
= options_object_sp->GetAsDictionary();
// but if it exists, it has to be an array.
if (options_dict) {
m_options_error = m_options.SetOptionsFromArray(*(options_dict));
// If we got an error don't bother with the arguments...
if (m_options_error.Fail())
return;
} else {
m_options_error = Status::FromErrorString("Options array not an array");
return;
}
}
// Then fetch the args. Since the arguments can have usage masks you need
// an array of arrays.
StructuredData::ObjectSP args_object_sp
= scripter->GetArgumentsForCommandObject(cmd_obj_sp);
if (args_object_sp) {
StructuredData::Array *args_array = args_object_sp->GetAsArray();
if (!args_array) {
m_args_error =
Status::FromErrorString("Argument specification is not an array");
return;
}
size_t counter = 0;
// This is the Array::ForEach function that handles the
// CommandArgumentEntry arrays one by one:
auto arg_array_adder = [this, &counter] (StructuredData::Object *object)
-> bool {
// This is the Array::ForEach function to add argument entries:
CommandArgumentEntry this_entry;
size_t elem_counter = 0;
auto args_adder = [this, counter, &elem_counter, &this_entry]
(StructuredData::Object *object) -> bool {
// The arguments definition has three fields, the argument type, the
// repeat and the usage mask.
CommandArgumentType arg_type = eArgTypeNone;
ArgumentRepetitionType arg_repetition = eArgRepeatOptional;
uint32_t arg_opt_set_association;
auto report_error = [this, elem_counter,
counter](const char *err_txt) -> bool {
m_args_error = Status::FromErrorStringWithFormatv(
"Element {0} of arguments "
"list element {1}: %s.",
elem_counter, counter, err_txt);
return false;
};
StructuredData::Dictionary *arg_dict = object->GetAsDictionary();
if (!arg_dict) {
report_error("is not a dictionary.");
return false;
}
// Argument Type:
StructuredData::ObjectSP obj_sp
= arg_dict->GetValueForKey("arg_type");
if (obj_sp) {
StructuredData::UnsignedInteger *uint_val
= obj_sp->GetAsUnsignedInteger();
if (!uint_val) {
report_error("value type must be an unsigned integer");
return false;
}
uint64_t arg_type_int = uint_val->GetValue();
if (arg_type_int >= eArgTypeLastArg) {
report_error("value type beyond ArgumentRepetitionType bounds");
return false;
}
arg_type = (CommandArgumentType) arg_type_int;
}
// Repeat Value:
obj_sp = arg_dict->GetValueForKey("repeat");
std::optional<ArgumentRepetitionType> repeat;
if (obj_sp) {
llvm::StringRef repeat_str = obj_sp->GetStringValue();
if (repeat_str.empty()) {
report_error("repeat value is empty");
return false;
}
repeat = ArgRepetitionFromString(repeat_str);
if (!repeat) {
report_error("invalid repeat value");
return false;
}
arg_repetition = *repeat;
}
// Usage Mask:
obj_sp = arg_dict->GetValueForKey("groups");
m_args_error = CommandOptions::ParseUsageMaskFromArray(obj_sp,
counter, arg_opt_set_association);
this_entry.emplace_back(arg_type, arg_repetition,
arg_opt_set_association);
elem_counter++;
return true;
};
StructuredData::Array *args_array = object->GetAsArray();
if (!args_array) {
m_args_error =
Status::FromErrorStringWithFormatv("Argument definition element "
"{0} is not an array",
counter);
}
args_array->ForEach(args_adder);
if (m_args_error.Fail())
return false;
if (this_entry.empty()) {
m_args_error =
Status::FromErrorStringWithFormatv("Argument definition element "
"{0} is empty",
counter);
return false;
}
m_arguments.push_back(this_entry);
counter++;
return true;
}; // end of arg_array_adder
// Here we actually parse the args definition:
args_array->ForEach(arg_array_adder);
}
}
~CommandObjectScriptingObjectParsed() override = default;
Status GetOptionsError() { return m_options_error.Clone(); }
Status GetArgsError() { return m_args_error.Clone(); }
bool WantsCompletion() override { return true; }
private:
void PrepareOptionsForCompletion(CompletionRequest &request,
OptionElementVector &option_vec) {
// First, we have to tell the Scripted side to set the values in its
// option store, then we call into the handle_completion passing in
// an array of the args, the arg index and the cursor position in the arg.
// We want the script side to have a chance to clear its state, so tell
// it argument parsing has started:
Options *options = GetOptions();
// If there are not options, this will be nullptr, and in that case we
// can just skip setting the options on the scripted side:
if (options)
m_options.PrepareOptionsForCompletion(request, option_vec, &m_exe_ctx);
}
public:
void HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &option_vec) override {
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return;
// Set up the options values on the scripted side:
PrepareOptionsForCompletion(request, option_vec);
// Now we have to make up the argument list.
// The ParseForCompletion only identifies tokens in the m_parsed_line
// it doesn't remove the options leaving only the args as it does for
// the regular Parse, so we have to filter out the option ones using the
// option_element_vector:
Options *options = GetOptions();
auto defs = options->GetDefinitions();
std::unordered_set<size_t> option_slots;
for (const auto &elem : option_vec) {
if (elem.opt_defs_index == -1)
continue;
option_slots.insert(elem.opt_pos);
if (defs[elem.opt_defs_index].option_has_arg)
option_slots.insert(elem.opt_arg_pos);
}
std::vector<llvm::StringRef> args_vec;
Args &args = request.GetParsedLine();
size_t num_args = args.GetArgumentCount();
size_t cursor_idx = request.GetCursorIndex();
size_t args_elem_pos = cursor_idx;
for (size_t idx = 0; idx < num_args; idx++) {
if (option_slots.count(idx) == 0)
args_vec.push_back(args[idx].ref());
else if (idx < cursor_idx)
args_elem_pos--;
}
StructuredData::DictionarySP completion_dict_sp =
scripter->HandleArgumentCompletionForScriptedCommand(
m_cmd_obj_sp, args_vec, args_elem_pos, request.GetCursorCharPos());
if (!completion_dict_sp) {
CommandObject::HandleArgumentCompletion(request, option_vec);
return;
}
m_options.ProcessCompletionDict(request, completion_dict_sp);
}
bool IsRemovable() const override { return true; }
ScriptedCommandSynchronicity GetSynchronicity() { return m_synchro; }
std::optional<std::string> GetRepeatCommand(Args &args,
uint32_t index) override {
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return std::nullopt;
return scripter->GetRepeatCommandForScriptedCommand(m_cmd_obj_sp, args);
}
llvm::StringRef GetHelp() override {
if (m_fetched_help_short)
return CommandObjectParsed::GetHelp();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return CommandObjectParsed::GetHelp();
std::string docstring;
m_fetched_help_short =
scripter->GetShortHelpForCommandObject(m_cmd_obj_sp, docstring);
if (!docstring.empty())
SetHelp(docstring);
return CommandObjectParsed::GetHelp();
}
llvm::StringRef GetHelpLong() override {
if (m_fetched_help_long)
return CommandObjectParsed::GetHelpLong();
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
if (!scripter)
return CommandObjectParsed::GetHelpLong();
std::string docstring;
m_fetched_help_long =
scripter->GetLongHelpForCommandObject(m_cmd_obj_sp, docstring);
if (!docstring.empty())
SetHelpLong(docstring);
return CommandObjectParsed::GetHelpLong();
}
Options *GetOptions() override {
// CommandObjectParsed requires that a command with no options return
// nullptr.
if (m_options.GetNumOptions() == 0)
return nullptr;
return &m_options;
}
protected:
void DoExecute(Args &args,
CommandReturnObject &result) override {
ScriptInterpreter *scripter = GetDebugger().GetScriptInterpreter();
Status error;
result.SetStatus(eReturnStatusInvalid);
if (!scripter ||
!scripter->RunScriptBasedParsedCommand(m_cmd_obj_sp, args,
m_synchro, result, error, m_exe_ctx)) {
result.AppendError(error.AsCString());
} else {
// Don't change the status if the command already set it...
if (result.GetStatus() == eReturnStatusInvalid) {
if (result.GetOutputString().empty())
result.SetStatus(eReturnStatusSuccessFinishNoResult);
else
result.SetStatus(eReturnStatusSuccessFinishResult);
}
}
}
private:
StructuredData::GenericSP m_cmd_obj_sp;
ScriptedCommandSynchronicity m_synchro;
CommandOptions m_options;
Status m_options_error;
Status m_args_error;
bool m_fetched_help_short : 1;
bool m_fetched_help_long : 1;
};
std::unordered_set<std::string>
CommandObjectScriptingObjectParsed::CommandOptions::g_string_storer;
// CommandObjectCommandsScriptImport
#define LLDB_OPTIONS_script_import
#include "CommandOptions.inc"
class CommandObjectCommandsScriptImport : public CommandObjectParsed {
public:
CommandObjectCommandsScriptImport(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "command script import",
"Import a scripting module in LLDB.", nullptr) {
AddSimpleArgumentList(eArgTypeFilename, eArgRepeatPlus);
}
~CommandObjectCommandsScriptImport() override = default;
Options *GetOptions() override { return &m_options; }
protected:
class CommandOptions : public Options {
public:
CommandOptions() = default;
~CommandOptions() override = default;
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
ExecutionContext *execution_context) override {
Status error;
const int short_option = m_getopt_table[option_idx].val;
switch (short_option) {
case 'r':
// NO-OP
break;
case 'c':
relative_to_command_file = true;
break;
case 's':
silent = true;
break;
default:
llvm_unreachable("Unimplemented option");
}
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
relative_to_command_file = false;
}
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
return llvm::ArrayRef(g_script_import_options);
}
bool relative_to_command_file = false;
bool silent = false;
};
void DoExecute(Args &command, CommandReturnObject &result) override {
if (command.empty()) {
result.AppendError("command script import needs one or more arguments");
return;
}
FileSpec source_dir = {};
if (m_options.relative_to_command_file) {
source_dir = GetDebugger().GetCommandInterpreter().GetCurrentSourceDir();
if (!source_dir) {
result.AppendError("command script import -c can only be specified "
"from a command file");
return;
}
}
for (auto &entry : command.entries()) {
Status error;
LoadScriptOptions options;
options.SetInitSession(true);
options.SetSilent(m_options.silent);
// FIXME: this is necessary because CommandObject::CheckRequirements()
// assumes that commands won't ever be recursively invoked, but it's
// actually possible to craft a Python script that does other "command
// script imports" in __lldb_init_module the real fix is to have
// recursive commands possible with a CommandInvocation object separate
// from the CommandObject itself, so that recursive command invocations
// won't stomp on each other (wrt to execution contents, options, and
// more)
m_exe_ctx.Clear();
if (GetDebugger().GetScriptInterpreter()->LoadScriptingModule(
entry.c_str(), options, error, /*module_sp=*/nullptr,
source_dir)) {
result.SetStatus(eReturnStatusSuccessFinishNoResult);
} else {
result.AppendErrorWithFormat("module importing failed: %s",
error.AsCString());
}
}
}
CommandOptions m_options;
};
#define LLDB_OPTIONS_script_add
#include "CommandOptions.inc"
class CommandObjectCommandsScriptAdd : public CommandObjectParsed,
public IOHandlerDelegateMultiline {
public:
CommandObjectCommandsScriptAdd(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "command script add",
"Add a scripted function as an LLDB command.",
"Add a scripted function as an lldb command. "
"If you provide a single argument, the command "
"will be added at the root level of the command "
"hierarchy. If there are more arguments they "
"must be a path to a user-added container "
"command, and the last element will be the new "
"command name."),
IOHandlerDelegateMultiline("DONE") {
AddSimpleArgumentList(eArgTypeCommand, eArgRepeatPlus);
}
~CommandObjectCommandsScriptAdd() override = default;
Options *GetOptions() override { return &m_options; }
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
CommandCompletions::CompleteModifiableCmdPathArgs(m_interpreter, request,
opt_element_vector);
}
protected:
class CommandOptions : public Options {
public:
CommandOptions() = default;
~CommandOptions() override = default;
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
ExecutionContext *execution_context) override {
Status error;
const int short_option = m_getopt_table[option_idx].val;
switch (short_option) {
case 'f':
if (!option_arg.empty())
m_funct_name = std::string(option_arg);
break;
case 'c':
if (!option_arg.empty())
m_class_name = std::string(option_arg);
break;
case 'h':
if (!option_arg.empty())
m_short_help = std::string(option_arg);
break;
case 'o':
m_overwrite_lazy = eLazyBoolYes;
break;
case 'p':
m_parsed_command = true;
break;
case 's':
m_synchronicity =
(ScriptedCommandSynchronicity)OptionArgParser::ToOptionEnum(
option_arg, GetDefinitions()[option_idx].enum_values, 0, error);
if (!error.Success())
return Status::FromErrorStringWithFormat(
"unrecognized value for synchronicity '%s'",
option_arg.str().c_str());
break;
case 'C': {
Status error;
OptionDefinition definition = GetDefinitions()[option_idx];
lldb::CompletionType completion_type =
static_cast<lldb::CompletionType>(OptionArgParser::ToOptionEnum(
option_arg, definition.enum_values, eNoCompletion, error));
if (!error.Success())
return Status::FromErrorStringWithFormat(
"unrecognized value for command completion type '%s'",
option_arg.str().c_str());
m_completion_type = completion_type;
} break;
default:
llvm_unreachable("Unimplemented option");
}
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
m_class_name.clear();
m_funct_name.clear();
m_short_help.clear();
m_completion_type = eNoCompletion;
m_overwrite_lazy = eLazyBoolCalculate;
m_synchronicity = eScriptedCommandSynchronicitySynchronous;
m_parsed_command = false;
}
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
return llvm::ArrayRef(g_script_add_options);
}
// Instance variables to hold the values for command options.
std::string m_class_name;
std::string m_funct_name;
std::string m_short_help;
LazyBool m_overwrite_lazy = eLazyBoolCalculate;
ScriptedCommandSynchronicity m_synchronicity =
eScriptedCommandSynchronicitySynchronous;
CompletionType m_completion_type = eNoCompletion;
bool m_parsed_command = false;
};
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override {
StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
if (output_sp && interactive) {
output_sp->PutCString(g_python_command_instructions);
output_sp->Flush();
}
}
void IOHandlerInputComplete(IOHandler &io_handler,
std::string &data) override {
StreamFileSP error_sp = io_handler.GetErrorStreamFileSP();
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
if (interpreter) {
StringList lines;
lines.SplitIntoLines(data);
if (lines.GetSize() > 0) {
std::string funct_name_str;
if (interpreter->GenerateScriptAliasFunction(lines, funct_name_str)) {
if (funct_name_str.empty()) {
error_sp->Printf("error: unable to obtain a function name, didn't "
"add python command.\n");
error_sp->Flush();
} else {
// everything should be fine now, let's add this alias
CommandObjectSP command_obj_sp(new CommandObjectPythonFunction(
m_interpreter, m_cmd_name, funct_name_str, m_short_help,
m_synchronicity, m_completion_type));
if (!m_container) {
Status error = m_interpreter.AddUserCommand(
m_cmd_name, command_obj_sp, m_overwrite);
if (error.Fail()) {
error_sp->Printf("error: unable to add selected command: '%s'",
error.AsCString());
error_sp->Flush();
}
} else {
llvm::Error llvm_error = m_container->LoadUserSubcommand(
m_cmd_name, command_obj_sp, m_overwrite);
if (llvm_error) {
error_sp->Printf("error: unable to add selected command: '%s'",
llvm::toString(std::move(llvm_error)).c_str());
error_sp->Flush();
}
}
}
} else {
error_sp->Printf(
"error: unable to create function, didn't add python command\n");
error_sp->Flush();
}
} else {
error_sp->Printf("error: empty function, didn't add python command\n");
error_sp->Flush();
}
} else {
error_sp->Printf(
"error: script interpreter missing, didn't add python command\n");
error_sp->Flush();
}
io_handler.SetIsDone(true);
}
void DoExecute(Args &command, CommandReturnObject &result) override {
if (GetDebugger().GetScriptLanguage() != lldb::eScriptLanguagePython) {
result.AppendError("only scripting language supported for scripted "
"commands is currently Python");
return;
}
if (command.GetArgumentCount() == 0) {
result.AppendError("'command script add' requires at least one argument");
return;
}
// Store the options in case we get multi-line input, also figure out the
// default if not user supplied:
switch (m_options.m_overwrite_lazy) {
case eLazyBoolCalculate:
m_overwrite = !GetDebugger().GetCommandInterpreter().GetRequireCommandOverwrite();
break;
case eLazyBoolYes:
m_overwrite = true;
break;
case eLazyBoolNo:
m_overwrite = false;
}
Status path_error;
m_container = GetCommandInterpreter().VerifyUserMultiwordCmdPath(
command, true, path_error);
if (path_error.Fail()) {
result.AppendErrorWithFormat("error in command path: %s",
path_error.AsCString());
return;
}
if (!m_container) {
// This is getting inserted into the root of the interpreter.
m_cmd_name = std::string(command[0].ref());
} else {
size_t num_args = command.GetArgumentCount();
m_cmd_name = std::string(command[num_args - 1].ref());
}
m_short_help.assign(m_options.m_short_help);
m_synchronicity = m_options.m_synchronicity;
m_completion_type = m_options.m_completion_type;
// Handle the case where we prompt for the script code first:
if (m_options.m_class_name.empty() && m_options.m_funct_name.empty()) {
m_interpreter.GetPythonCommandsFromIOHandler(" ", // Prompt
*this); // IOHandlerDelegate
return;
}
CommandObjectSP new_cmd_sp;
if (m_options.m_class_name.empty()) {
new_cmd_sp.reset(new CommandObjectPythonFunction(
m_interpreter, m_cmd_name, m_options.m_funct_name,
m_options.m_short_help, m_synchronicity, m_completion_type));
} else {
ScriptInterpreter *interpreter = GetDebugger().GetScriptInterpreter();
if (!interpreter) {
result.AppendError("cannot find ScriptInterpreter");
return;
}
auto cmd_obj_sp = interpreter->CreateScriptCommandObject(
m_options.m_class_name.c_str());
if (!cmd_obj_sp) {
result.AppendErrorWithFormatv("cannot create helper object for: "
"'{0}'", m_options.m_class_name);
return;
}
if (m_options.m_parsed_command) {
new_cmd_sp = CommandObjectScriptingObjectParsed::Create(m_interpreter,
m_cmd_name, cmd_obj_sp, m_synchronicity, result);
if (!result.Succeeded())
return;
} else
new_cmd_sp.reset(new CommandObjectScriptingObjectRaw(
m_interpreter, m_cmd_name, cmd_obj_sp, m_synchronicity,
m_completion_type));
}
// Assume we're going to succeed...
result.SetStatus(eReturnStatusSuccessFinishNoResult);
if (!m_container) {
Status add_error =
m_interpreter.AddUserCommand(m_cmd_name, new_cmd_sp, m_overwrite);
if (add_error.Fail())
result.AppendErrorWithFormat("cannot add command: %s",
add_error.AsCString());
} else {
llvm::Error llvm_error =
m_container->LoadUserSubcommand(m_cmd_name, new_cmd_sp, m_overwrite);
if (llvm_error)
result.AppendErrorWithFormat(
"cannot add command: %s",
llvm::toString(std::move(llvm_error)).c_str());
}
}
CommandOptions m_options;
std::string m_cmd_name;
CommandObjectMultiword *m_container = nullptr;
std::string m_short_help;
bool m_overwrite = false;
ScriptedCommandSynchronicity m_synchronicity =
eScriptedCommandSynchronicitySynchronous;
CompletionType m_completion_type = eNoCompletion;
};
// CommandObjectCommandsScriptList
class CommandObjectCommandsScriptList : public CommandObjectParsed {
public:
CommandObjectCommandsScriptList(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "command script list",
"List defined top-level scripted commands.",
nullptr) {}
~CommandObjectCommandsScriptList() override = default;
void DoExecute(Args &command, CommandReturnObject &result) override {
m_interpreter.GetHelp(result, CommandInterpreter::eCommandTypesUserDef);
result.SetStatus(eReturnStatusSuccessFinishResult);
}
};
// CommandObjectCommandsScriptClear
class CommandObjectCommandsScriptClear : public CommandObjectParsed {
public:
CommandObjectCommandsScriptClear(CommandInterpreter &interpreter)
: CommandObjectParsed(interpreter, "command script clear",
"Delete all scripted commands.", nullptr) {}
~CommandObjectCommandsScriptClear() override = default;
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
m_interpreter.RemoveAllUser();
result.SetStatus(eReturnStatusSuccessFinishResult);
}
};
// CommandObjectCommandsScriptDelete
class CommandObjectCommandsScriptDelete : public CommandObjectParsed {
public:
CommandObjectCommandsScriptDelete(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "command script delete",
"Delete a scripted command by specifying the path to the command.",
nullptr) {
AddSimpleArgumentList(eArgTypeCommand, eArgRepeatPlus);
}
~CommandObjectCommandsScriptDelete() override = default;
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
lldb_private::CommandCompletions::CompleteModifiableCmdPathArgs(
m_interpreter, request, opt_element_vector);
}
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
llvm::StringRef root_cmd = command[0].ref();
size_t num_args = command.GetArgumentCount();
if (root_cmd.empty()) {
result.AppendErrorWithFormat("empty root command name");
return;
}
if (!m_interpreter.HasUserCommands() &&
!m_interpreter.HasUserMultiwordCommands()) {
result.AppendErrorWithFormat("can only delete user defined commands, "
"but no user defined commands found");
return;
}
CommandObjectSP cmd_sp = m_interpreter.GetCommandSPExact(root_cmd);
if (!cmd_sp) {
result.AppendErrorWithFormat("command '%s' not found.",
command[0].c_str());
return;
}
if (!cmd_sp->IsUserCommand()) {
result.AppendErrorWithFormat("command '%s' is not a user command.",
command[0].c_str());
return;
}
if (cmd_sp->GetAsMultiwordCommand() && num_args == 1) {
result.AppendErrorWithFormat("command '%s' is a multi-word command.\n "
"Delete with \"command container delete\"",
command[0].c_str());
return;
}
if (command.GetArgumentCount() == 1) {
m_interpreter.RemoveUser(root_cmd);
result.SetStatus(eReturnStatusSuccessFinishResult);
return;
}
// We're deleting a command from a multiword command. Verify the command
// path:
Status error;
CommandObjectMultiword *container =
GetCommandInterpreter().VerifyUserMultiwordCmdPath(command, true,
error);
if (error.Fail()) {
result.AppendErrorWithFormat("could not resolve command path: %s",
error.AsCString());
return;
}
if (!container) {
// This means that command only had a leaf command, so the container is
// the root. That should have been handled above.
result.AppendErrorWithFormat("could not find a container for '%s'",
command[0].c_str());
return;
}
const char *leaf_cmd = command[num_args - 1].c_str();
llvm::Error llvm_error =
container->RemoveUserSubcommand(leaf_cmd,
/* multiword not okay */ false);
if (llvm_error) {
result.AppendErrorWithFormat(
"could not delete command '%s': %s", leaf_cmd,
llvm::toString(std::move(llvm_error)).c_str());
return;
}
Stream &out_stream = result.GetOutputStream();
out_stream << "Deleted command:";
for (size_t idx = 0; idx < num_args; idx++) {
out_stream << ' ';
out_stream << command[idx].c_str();
}
out_stream << '\n';
result.SetStatus(eReturnStatusSuccessFinishResult);
}
};
#pragma mark CommandObjectMultiwordCommandsScript
// CommandObjectMultiwordCommandsScript
class CommandObjectMultiwordCommandsScript : public CommandObjectMultiword {
public:
CommandObjectMultiwordCommandsScript(CommandInterpreter &interpreter)
: CommandObjectMultiword(
interpreter, "command script",
"Commands for managing custom "
"commands implemented by "
"interpreter scripts.",
"command script <subcommand> [<subcommand-options>]") {
LoadSubCommand("add", CommandObjectSP(
new CommandObjectCommandsScriptAdd(interpreter)));
LoadSubCommand(
"delete",
CommandObjectSP(new CommandObjectCommandsScriptDelete(interpreter)));
LoadSubCommand(
"clear",
CommandObjectSP(new CommandObjectCommandsScriptClear(interpreter)));
LoadSubCommand("list", CommandObjectSP(new CommandObjectCommandsScriptList(
interpreter)));
LoadSubCommand(
"import",
CommandObjectSP(new CommandObjectCommandsScriptImport(interpreter)));
}
~CommandObjectMultiwordCommandsScript() override = default;
};
#pragma mark CommandObjectCommandContainer
#define LLDB_OPTIONS_container_add
#include "CommandOptions.inc"
class CommandObjectCommandsContainerAdd : public CommandObjectParsed {
public:
CommandObjectCommandsContainerAdd(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "command container add",
"Add a container command to lldb. Adding to built-"
"in container commands is not allowed.",
"command container add [[path1]...] container-name") {
AddSimpleArgumentList(eArgTypeCommand, eArgRepeatPlus);
}
~CommandObjectCommandsContainerAdd() override = default;
Options *GetOptions() override { return &m_options; }
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
lldb_private::CommandCompletions::CompleteModifiableCmdPathArgs(
m_interpreter, request, opt_element_vector);
}
protected:
class CommandOptions : public Options {
public:
CommandOptions() = default;
~CommandOptions() override = default;
Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg,
ExecutionContext *execution_context) override {
Status error;
const int short_option = m_getopt_table[option_idx].val;
switch (short_option) {
case 'h':
if (!option_arg.empty())
m_short_help = std::string(option_arg);
break;
case 'o':
m_overwrite = true;
break;
case 'H':
if (!option_arg.empty())
m_long_help = std::string(option_arg);
break;
default:
llvm_unreachable("Unimplemented option");
}
return error;
}
void OptionParsingStarting(ExecutionContext *execution_context) override {
m_short_help.clear();
m_long_help.clear();
m_overwrite = false;
}
llvm::ArrayRef<OptionDefinition> GetDefinitions() override {
return llvm::ArrayRef(g_container_add_options);
}
// Instance variables to hold the values for command options.
std::string m_short_help;
std::string m_long_help;
bool m_overwrite = false;
};
void DoExecute(Args &command, CommandReturnObject &result) override {
size_t num_args = command.GetArgumentCount();
if (num_args == 0) {
result.AppendError("no command was specified");
return;
}
if (num_args == 1) {
// We're adding this as a root command, so use the interpreter.
const char *cmd_name = command.GetArgumentAtIndex(0);
auto cmd_sp = CommandObjectSP(new CommandObjectMultiword(
GetCommandInterpreter(), cmd_name, m_options.m_short_help.c_str(),
m_options.m_long_help.c_str()));
cmd_sp->GetAsMultiwordCommand()->SetRemovable(true);
Status add_error = GetCommandInterpreter().AddUserCommand(
cmd_name, cmd_sp, m_options.m_overwrite);
if (add_error.Fail()) {
result.AppendErrorWithFormat("error adding command: %s",
add_error.AsCString());
return;
}
result.SetStatus(eReturnStatusSuccessFinishNoResult);
return;
}
// We're adding this to a subcommand, first find the subcommand:
Status path_error;
CommandObjectMultiword *add_to_me =
GetCommandInterpreter().VerifyUserMultiwordCmdPath(command, true,
path_error);
if (!add_to_me) {
result.AppendErrorWithFormat("error adding command: %s",
path_error.AsCString());
return;
}
const char *cmd_name = command.GetArgumentAtIndex(num_args - 1);
auto cmd_sp = CommandObjectSP(new CommandObjectMultiword(
GetCommandInterpreter(), cmd_name, m_options.m_short_help.c_str(),
m_options.m_long_help.c_str()));
llvm::Error llvm_error =
add_to_me->LoadUserSubcommand(cmd_name, cmd_sp, m_options.m_overwrite);
if (llvm_error) {
result.AppendErrorWithFormat("error adding subcommand: %s",
llvm::toString(std::move(llvm_error)).c_str());
return;
}
result.SetStatus(eReturnStatusSuccessFinishNoResult);
}
private:
CommandOptions m_options;
};
#define LLDB_OPTIONS_multiword_delete
#include "CommandOptions.inc"
class CommandObjectCommandsContainerDelete : public CommandObjectParsed {
public:
CommandObjectCommandsContainerDelete(CommandInterpreter &interpreter)
: CommandObjectParsed(
interpreter, "command container delete",
"Delete a container command previously added to "
"lldb.",
"command container delete [[path1] ...] container-cmd") {
AddSimpleArgumentList(eArgTypeCommand, eArgRepeatPlus);
}
~CommandObjectCommandsContainerDelete() override = default;
void
HandleArgumentCompletion(CompletionRequest &request,
OptionElementVector &opt_element_vector) override {
lldb_private::CommandCompletions::CompleteModifiableCmdPathArgs(
m_interpreter, request, opt_element_vector);
}
protected:
void DoExecute(Args &command, CommandReturnObject &result) override {
size_t num_args = command.GetArgumentCount();
if (num_args == 0) {
result.AppendError("No command was specified.");
return;
}
if (num_args == 1) {
// We're removing a root command, so we need to delete it from the
// interpreter.
const char *cmd_name = command.GetArgumentAtIndex(0);
// Let's do a little more work here so we can do better error reporting.
CommandInterpreter &interp = GetCommandInterpreter();
CommandObjectSP cmd_sp = interp.GetCommandSPExact(cmd_name);
if (!cmd_sp) {
result.AppendErrorWithFormat("container command %s doesn't exist.",
cmd_name);
return;
}
if (!cmd_sp->IsUserCommand()) {
result.AppendErrorWithFormat(
"container command %s is not a user command", cmd_name);
return;
}
if (!cmd_sp->GetAsMultiwordCommand()) {
result.AppendErrorWithFormat("command %s is not a container command",
cmd_name);
return;
}
bool did_remove = GetCommandInterpreter().RemoveUserMultiword(cmd_name);
if (!did_remove) {
result.AppendErrorWithFormat("error removing command %s.", cmd_name);
return;
}
result.SetStatus(eReturnStatusSuccessFinishNoResult);
return;
}
// We're removing a subcommand, first find the subcommand's owner:
Status path_error;
CommandObjectMultiword *container =
GetCommandInterpreter().VerifyUserMultiwordCmdPath(command, true,
path_error);
if (!container) {
result.AppendErrorWithFormat("error removing container command: %s",
path_error.AsCString());
return;
}
const char *leaf = command.GetArgumentAtIndex(num_args - 1);
llvm::Error llvm_error =
container->RemoveUserSubcommand(leaf, /* multiword okay */ true);
if (llvm_error) {
result.AppendErrorWithFormat("error removing container command: %s",
llvm::toString(std::move(llvm_error)).c_str());
return;
}
result.SetStatus(eReturnStatusSuccessFinishNoResult);
}
};
class CommandObjectCommandContainer : public CommandObjectMultiword {
public:
CommandObjectCommandContainer(CommandInterpreter &interpreter)
: CommandObjectMultiword(
interpreter, "command container",
"Commands for adding container commands to lldb. "
"Container commands are containers for other commands. You can "
"add nested container commands by specifying a command path, "
"but you can't add commands into the built-in command hierarchy.",
"command container <subcommand> [<subcommand-options>]") {
LoadSubCommand("add", CommandObjectSP(new CommandObjectCommandsContainerAdd(
interpreter)));
LoadSubCommand(
"delete",
CommandObjectSP(new CommandObjectCommandsContainerDelete(interpreter)));
}
~CommandObjectCommandContainer() override = default;
};
#pragma mark CommandObjectMultiwordCommands
// CommandObjectMultiwordCommands
CommandObjectMultiwordCommands::CommandObjectMultiwordCommands(
CommandInterpreter &interpreter)
: CommandObjectMultiword(interpreter, "command",
"Commands for managing custom LLDB commands.",
"command <subcommand> [<subcommand-options>]") {
LoadSubCommand("source",
CommandObjectSP(new CommandObjectCommandsSource(interpreter)));
LoadSubCommand("alias",
CommandObjectSP(new CommandObjectCommandsAlias(interpreter)));
LoadSubCommand("unalias", CommandObjectSP(
new CommandObjectCommandsUnalias(interpreter)));
LoadSubCommand("delete",
CommandObjectSP(new CommandObjectCommandsDelete(interpreter)));
LoadSubCommand("container", CommandObjectSP(new CommandObjectCommandContainer(
interpreter)));
LoadSubCommand(
"regex", CommandObjectSP(new CommandObjectCommandsAddRegex(interpreter)));
LoadSubCommand(
"script",
CommandObjectSP(new CommandObjectMultiwordCommandsScript(interpreter)));
}
CommandObjectMultiwordCommands::~CommandObjectMultiwordCommands() = default;
|