1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230
|
// Package terminal implements functions for responding to user
// input and dispatching to appropriate backend commands.
package terminal
//lint:file-ignore ST1005 errors here can be capitalized
import (
"bufio"
"bytes"
"errors"
"fmt"
"go/parser"
"go/scanner"
"io"
"io/ioutil"
"math"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"text/tabwriter"
"github.com/cosiner/argv"
"github.com/go-delve/delve/pkg/config"
"github.com/go-delve/delve/pkg/locspec"
"github.com/go-delve/delve/pkg/proc/debuginfod"
"github.com/go-delve/delve/service"
"github.com/go-delve/delve/service/api"
"github.com/go-delve/delve/service/rpc2"
)
const optimizedFunctionWarning = "Warning: debugging optimized function"
type cmdPrefix int
const (
noPrefix = cmdPrefix(0)
onPrefix = cmdPrefix(1 << iota)
deferredPrefix
revPrefix
)
type callContext struct {
Prefix cmdPrefix
Scope api.EvalScope
Breakpoint *api.Breakpoint
}
func (ctx *callContext) scoped() bool {
return ctx.Scope.GoroutineID >= 0 || ctx.Scope.Frame > 0
}
type frameDirection int
const (
frameSet frameDirection = iota
frameUp
frameDown
)
type cmdfunc func(t *Term, ctx callContext, args string) error
type command struct {
aliases []string
builtinAliases []string
group commandGroup
allowedPrefixes cmdPrefix
helpMsg string
cmdFn cmdfunc
}
// Returns true if the command string matches one of the aliases for this command
func (c command) match(cmdstr string) bool {
for _, v := range c.aliases {
if v == cmdstr {
return true
}
}
return false
}
// Commands represents the commands for Delve terminal process.
type Commands struct {
cmds []command
client service.Client
frame int // Current frame as set by frame/up/down commands.
}
var (
// longLoadConfig loads more information:
// * Follows pointers
// * Loads more array values
// * Does not limit struct fields
longLoadConfig = api.LoadConfig{FollowPointers: true, MaxVariableRecurse: 1, MaxStringLen: 64, MaxArrayValues: 64, MaxStructFields: -1}
// ShortLoadConfig loads less information, not following pointers
// and limiting struct fields loaded to 3.
ShortLoadConfig = api.LoadConfig{MaxStringLen: 64, MaxStructFields: 3}
)
// byFirstAlias will sort by the first
// alias of a command.
type byFirstAlias []command
func (a byFirstAlias) Len() int { return len(a) }
func (a byFirstAlias) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byFirstAlias) Less(i, j int) bool { return a[i].aliases[0] < a[j].aliases[0] }
// DebugCommands returns a Commands struct with default commands defined.
func DebugCommands(client service.Client) *Commands {
c := &Commands{client: client}
c.cmds = []command{
{aliases: []string{"help", "h"}, cmdFn: c.help, helpMsg: `Prints the help message.
help [command]
Type "help" followed by the name of a command for more information about it.`},
{aliases: []string{"break", "b"}, group: breakCmds, cmdFn: breakpoint, helpMsg: `Sets a breakpoint.
break [name] [locspec]
See Documentation/cli/locspec.md for the syntax of locspec. If locspec is omitted a breakpoint will be set on the current line.
See also: "help on", "help cond" and "help clear"`},
{aliases: []string{"trace", "t"}, group: breakCmds, cmdFn: tracepoint, allowedPrefixes: onPrefix, helpMsg: `Set tracepoint.
trace [name] [locspec]
A tracepoint is a breakpoint that does not stop the execution of the program, instead when the tracepoint is hit a notification is displayed. See Documentation/cli/locspec.md for the syntax of locspec. If locspec is omitted a tracepoint will be set on the current line.
See also: "help on", "help cond" and "help clear"`},
{aliases: []string{"watch"}, group: breakCmds, cmdFn: watchpoint, helpMsg: `Set watchpoint.
watch [-r|-w|-rw] <expr>
-r stops when the memory location is read
-w stops when the memory location is written
-rw stops when the memory location is read or written
The memory location is specified with the same expression language used by 'print', for example:
watch v
watch -w *(*int)(0x1400007c018)
will watch the address of variable 'v' and writes to an int at addr '0x1400007c018'.
Note that writes that do not change the value of the watched memory address might not be reported.
See also: "help print".`},
{aliases: []string{"restart", "r"}, group: runCmds, cmdFn: restart, helpMsg: `Restart process.
For recorded targets the command takes the following forms:
restart resets to the start of the recording
restart [checkpoint] resets the recording to the given checkpoint
restart -r [newargv...] [redirects...] re-records the target process
For live targets the command takes the following forms:
restart [newargv...] [redirects...] restarts the process
If newargv is omitted the process is restarted (or re-recorded) with the same argument vector.
If -noargs is specified instead, the argument vector is cleared.
A list of file redirections can be specified after the new argument list to override the redirections defined using the '--redirect' command line option. A syntax similar to Unix shells is used:
<input.txt redirects the standard input of the target process from input.txt
>output.txt redirects the standard output of the target process to output.txt
2>error.txt redirects the standard error of the target process to error.txt
`},
{aliases: []string{"rebuild"}, group: runCmds, cmdFn: c.rebuild, allowedPrefixes: revPrefix, helpMsg: "Rebuild the target executable and restarts it. It does not work if the executable was not built by delve."},
{aliases: []string{"continue", "c"}, group: runCmds, cmdFn: c.cont, allowedPrefixes: revPrefix, helpMsg: `Run until breakpoint or program termination.
continue [<locspec>]
Optional locspec argument allows you to continue until a specific location is reached. The program will halt if a breakpoint is hit before reaching the specified location.
For example:
continue main.main
continue encoding/json.Marshal
`},
{aliases: []string{"step", "s"}, group: runCmds, cmdFn: c.step, allowedPrefixes: revPrefix, helpMsg: "Single step through program."},
{aliases: []string{"step-instruction", "si"}, group: runCmds, allowedPrefixes: revPrefix, cmdFn: c.stepInstruction, helpMsg: "Single step a single cpu instruction."},
{aliases: []string{"next", "n"}, group: runCmds, cmdFn: c.next, allowedPrefixes: revPrefix, helpMsg: `Step over to next source line.
next [count]
Optional [count] argument allows you to skip multiple lines.
`},
{aliases: []string{"stepout", "so"}, group: runCmds, allowedPrefixes: revPrefix, cmdFn: c.stepout, helpMsg: "Step out of the current function."},
{aliases: []string{"call"}, group: runCmds, cmdFn: c.call, helpMsg: `Resumes process, injecting a function call (EXPERIMENTAL!!!)
call [-unsafe] <function call expression>
Current limitations:
- only pointers to stack-allocated objects can be passed as argument.
- only some automatic type conversions are supported.
- functions can only be called on running goroutines that are not
executing the runtime.
- the current goroutine needs to have at least 256 bytes of free space on
the stack.
- functions can only be called when the goroutine is stopped at a safe
point.
- calling a function will resume execution of all goroutines.
- only supported on linux's native backend.
`},
{aliases: []string{"threads"}, group: goroutineCmds, cmdFn: threads, helpMsg: "Print out info for every traced thread."},
{aliases: []string{"thread", "tr"}, group: goroutineCmds, cmdFn: thread, helpMsg: `Switch to the specified thread.
thread <id>`},
{aliases: []string{"clear"}, group: breakCmds, cmdFn: clear, helpMsg: `Deletes breakpoint.
clear <breakpoint name or id>`},
{aliases: []string{"clearall"}, group: breakCmds, cmdFn: clearAll, helpMsg: `Deletes multiple breakpoints.
clearall [<locspec>]
If called with the locspec argument it will delete all the breakpoints matching the locspec. If locspec is omitted all breakpoints are deleted.`},
{aliases: []string{"toggle"}, group: breakCmds, cmdFn: toggle, helpMsg: `Toggles on or off a breakpoint.
toggle <breakpoint name or id>`},
{aliases: []string{"goroutines", "grs"}, group: goroutineCmds, cmdFn: c.goroutines, helpMsg: `List program goroutines.
goroutines [-u|-r|-g|-s] [-t [depth]] [-l] [-with loc expr] [-without loc expr] [-group argument] [-exec command]
Print out info for every goroutine. The flag controls what information is shown along with each goroutine:
-u displays location of topmost stackframe in user code (default)
-r displays location of topmost stackframe (including frames inside private runtime functions)
-g displays location of go instruction that created the goroutine
-s displays location of the start function
-t displays goroutine's stacktrace (an optional depth value can be specified, default: 10)
-l displays goroutine's labels
If no flag is specified the default is -u, i.e. the first frame within the first 30 frames that is not executing a runtime private function.
FILTERING
If -with or -without are specified only goroutines that match the given condition are returned.
To only display goroutines where the specified location contains (or does not contain, for -without and -wo) expr as a substring, use:
goroutines -with (userloc|curloc|goloc|startloc) expr
goroutines -w (userloc|curloc|goloc|startloc) expr
goroutines -without (userloc|curloc|goloc|startloc) expr
goroutines -wo (userloc|curloc|goloc|startloc) expr
To only display goroutines that have (or do not have) the specified label key and value, use:
goroutines -with label key=value
goroutines -without label key=value
To only display goroutines that have (or do not have) the specified label key, use:
goroutines -with label key
goroutines -without label key
To only display goroutines that are running (or are not running) on a OS thread, use:
goroutines -with running
goroutines -without running
To only display user (or runtime) goroutines, use:
goroutines -with user
goroutines -without user
GROUPING
goroutines -group (userloc|curloc|goloc|startloc|running|user)
Groups goroutines by the given location, running status or user classification, up to 5 goroutines per group will be displayed as well as the total number of goroutines in the group.
goroutines -group label key
Groups goroutines by the value of the label with the specified key.
EXEC
goroutines -exec <command>
Runs the command on every goroutine.
`},
{aliases: []string{"goroutine", "gr"}, group: goroutineCmds, allowedPrefixes: onPrefix, cmdFn: c.goroutine, helpMsg: `Shows or changes current goroutine
goroutine
goroutine <id>
goroutine <id> <command>
Called without arguments it will show information about the current goroutine.
Called with a single argument it will switch to the specified goroutine.
Called with more arguments it will execute a command on the specified goroutine.`},
{aliases: []string{"breakpoints", "bp"}, group: breakCmds, cmdFn: breakpoints, helpMsg: `Print out info for active breakpoints.
breakpoints [-a]
Specifying -a prints all physical breakpoint, including internal breakpoints.`},
{aliases: []string{"print", "p"}, group: dataCmds, allowedPrefixes: onPrefix | deferredPrefix, cmdFn: printVar, helpMsg: `Evaluate an expression.
[goroutine <n>] [frame <m>] print [%format] <expression>
See Documentation/cli/expr.md for a description of supported expressions.
The optional format argument is a format specifier, like the ones used by the fmt package. For example "print %x v" will print v as an hexadecimal number.`},
{aliases: []string{"whatis"}, group: dataCmds, cmdFn: whatisCommand, helpMsg: `Prints type of an expression.
whatis <expression>`},
{aliases: []string{"set"}, group: dataCmds, cmdFn: setVar, helpMsg: `Changes the value of a variable.
[goroutine <n>] [frame <m>] set <variable> = <value>
See Documentation/cli/expr.md for a description of supported expressions. Only numerical variables and pointers can be changed.`},
{aliases: []string{"sources"}, cmdFn: sources, helpMsg: `Print list of source files.
sources [<regex>]
If regex is specified only the source files matching it will be returned.`},
{aliases: []string{"funcs"}, cmdFn: funcs, helpMsg: `Print list of functions.
funcs [<regex>]
If regex is specified only the functions matching it will be returned.`},
{aliases: []string{"types"}, cmdFn: types, helpMsg: `Print list of types
types [<regex>]
If regex is specified only the types matching it will be returned.`},
{aliases: []string{"args"}, allowedPrefixes: onPrefix | deferredPrefix, group: dataCmds, cmdFn: args, helpMsg: `Print function arguments.
[goroutine <n>] [frame <m>] args [-v] [<regex>]
If regex is specified only function arguments with a name matching it will be returned. If -v is specified more information about each function argument will be shown.`},
{aliases: []string{"locals"}, allowedPrefixes: onPrefix | deferredPrefix, group: dataCmds, cmdFn: locals, helpMsg: `Print local variables.
[goroutine <n>] [frame <m>] locals [-v] [<regex>]
The name of variables that are shadowed in the current scope will be shown in parenthesis.
If regex is specified only local variables with a name matching it will be returned. If -v is specified more information about each local variable will be shown.`},
{aliases: []string{"vars"}, cmdFn: vars, group: dataCmds, helpMsg: `Print package variables.
vars [-v] [<regex>]
If regex is specified only package variables with a name matching it will be returned. If -v is specified more information about each package variable will be shown.`},
{aliases: []string{"regs"}, cmdFn: regs, group: dataCmds, helpMsg: `Print contents of CPU registers.
regs [-a]
Argument -a shows more registers. Individual registers can also be displayed by 'print' and 'display'. See Documentation/cli/expr.md.`},
{aliases: []string{"exit", "quit", "q"}, cmdFn: exitCommand, helpMsg: `Exit the debugger.
exit [-c]
When connected to a headless instance started with the --accept-multiclient, pass -c to resume the execution of the target process before disconnecting.`},
{aliases: []string{"list", "ls", "l"}, cmdFn: listCommand, helpMsg: `Show source code.
[goroutine <n>] [frame <m>] list [<locspec>]
Show source around current point or provided locspec.
For example:
frame 1 list 69
list testvariables.go:10000
list main.main:30
list 40`},
{aliases: []string{"stack", "bt"}, allowedPrefixes: onPrefix, group: stackCmds, cmdFn: stackCommand, helpMsg: `Print stack trace.
[goroutine <n>] [frame <m>] stack [<depth>] [-full] [-offsets] [-defer] [-a <n>] [-adepth <depth>] [-mode <mode>]
-full every stackframe is decorated with the value of its local variables and arguments.
-offsets prints frame offset of each frame.
-defer prints deferred function call stack for each frame.
-a <n> prints stacktrace of n ancestors of the selected goroutine (target process must have tracebackancestors enabled)
-adepth <depth> configures depth of ancestor stacktrace
-mode <mode> specifies the stacktrace mode, possible values are:
normal - attempts to automatically switch between cgo frames and go frames
simple - disables automatic switch between cgo and go
fromg - starts from the registers stored in the runtime.g struct
`},
{aliases: []string{"frame"},
group: stackCmds,
cmdFn: func(t *Term, ctx callContext, arg string) error {
return c.frameCommand(t, ctx, arg, frameSet)
},
helpMsg: `Set the current frame, or execute command on a different frame.
frame <m>
frame <m> <command>
The first form sets frame used by subsequent commands such as "print" or "set".
The second form runs the command on the given frame.`},
{aliases: []string{"up"},
group: stackCmds,
cmdFn: func(t *Term, ctx callContext, arg string) error {
return c.frameCommand(t, ctx, arg, frameUp)
},
helpMsg: `Move the current frame up.
up [<m>]
up [<m>] <command>
Move the current frame up by <m>. The second form runs the command on the given frame.`},
{aliases: []string{"down"},
group: stackCmds,
cmdFn: func(t *Term, ctx callContext, arg string) error {
return c.frameCommand(t, ctx, arg, frameDown)
},
helpMsg: `Move the current frame down.
down [<m>]
down [<m>] <command>
Move the current frame down by <m>. The second form runs the command on the given frame.`},
{aliases: []string{"deferred"}, group: stackCmds, cmdFn: c.deferredCommand, helpMsg: `Executes command in the context of a deferred call.
deferred <n> <command>
Executes the specified command (print, args, locals) in the context of the n-th deferred call in the current frame.`},
{aliases: []string{"source"}, cmdFn: c.sourceCommand, helpMsg: `Executes a file containing a list of delve commands
source <path>
If path ends with the .star extension it will be interpreted as a starlark script. See Documentation/cli/starlark.md for the syntax.
If path is a single '-' character an interactive starlark interpreter will start instead. Type 'exit' to exit.`},
{aliases: []string{"disassemble", "disass"}, cmdFn: disassCommand, helpMsg: `Disassembler.
[goroutine <n>] [frame <m>] disassemble [-a <start> <end>] [-l <locspec>]
If no argument is specified the function being executed in the selected stack frame will be executed.
-a <start> <end> disassembles the specified address range
-l <locspec> disassembles the specified function`},
{aliases: []string{"on"}, group: breakCmds, cmdFn: c.onCmd, helpMsg: `Executes a command when a breakpoint is hit.
on <breakpoint name or id> <command>
on <breakpoint name or id> -edit
Supported commands: print, stack, goroutine, trace and cond.
To convert a breakpoint into a tracepoint use:
on <breakpoint name or id> trace
The command 'on <bp> cond <cond-arguments>' is equivalent to 'cond <bp> <cond-arguments>'.
The command 'on x -edit' can be used to edit the list of commands executed when the breakpoint is hit.`},
{aliases: []string{"condition", "cond"}, group: breakCmds, cmdFn: conditionCmd, allowedPrefixes: onPrefix, helpMsg: `Set breakpoint condition.
condition <breakpoint name or id> <boolean expression>.
condition -hitcount <breakpoint name or id> <operator> <argument>.
condition -per-g-hitcount <breakpoint name or id> <operator> <argument>.
condition -clear <breakpoint name or id>.
Specifies that the breakpoint, tracepoint or watchpoint should break only if the boolean expression is true.
See Documentation/cli/expr.md for a description of supported expressions.
With the -hitcount option a condition on the breakpoint hit count can be set, the following operators are supported
condition -hitcount bp > n
condition -hitcount bp >= n
condition -hitcount bp < n
condition -hitcount bp <= n
condition -hitcount bp == n
condition -hitcount bp != n
condition -hitcount bp % n
The -per-g-hitcount option works like -hitcount, but use per goroutine hitcount to compare with n.
With the -clear option a condition on the breakpoint can removed.
The '% n' form means we should stop at the breakpoint when the hitcount is a multiple of n.
Examples:
cond 2 i == 10 breakpoint 2 will stop when variable i equals 10
cond name runtime.curg.goid == 5 breakpoint 'name' will stop only on goroutine 5
cond -clear 2 the condition on breakpoint 2 will be removed
`},
{aliases: []string{"config"}, cmdFn: configureCmd, helpMsg: `Changes configuration parameters.
config -list
Show all configuration parameters.
config -save
Saves the configuration file to disk, overwriting the current configuration file.
config <parameter> <value>
Changes the value of a configuration parameter.
config substitute-path <from> <to>
config substitute-path <from>
Adds or removes a path substitution rule.
config alias <command> <alias>
config alias <alias>
Defines <alias> as an alias to <command> or removes an alias.`},
{aliases: []string{"edit", "ed"}, cmdFn: edit, helpMsg: `Open where you are in $DELVE_EDITOR or $EDITOR
edit [locspec]
If locspec is omitted edit will open the current source file in the editor, otherwise it will open the specified location.`},
{aliases: []string{"libraries"}, cmdFn: libraries, helpMsg: `List loaded dynamic libraries`},
{aliases: []string{"examinemem", "x"}, group: dataCmds, cmdFn: examineMemoryCmd, helpMsg: `Examine raw memory at the given address.
Examine memory:
examinemem [-fmt <format>] [-count|-len <count>] [-size <size>] <address>
examinemem [-fmt <format>] [-count|-len <count>] [-size <size>] -x <expression>
Format represents the data format and the value is one of this list (default hex): bin(binary), oct(octal), dec(decimal), hex(hexadecimal).
Length is the number of bytes (default 1) and must be less than or equal to 1000.
Address is the memory location of the target to examine. Please note '-len' is deprecated by '-count and -size'.
Expression can be an integer expression or pointer value of the memory location to examine.
For example:
x -fmt hex -count 20 -size 1 0xc00008af38
x -fmt hex -count 20 -size 1 -x 0xc00008af38 + 8
x -fmt hex -count 20 -size 1 -x &myVar
x -fmt hex -count 20 -size 1 -x myPtrVar`},
{aliases: []string{"display"}, group: dataCmds, cmdFn: display, helpMsg: `Print value of an expression every time the program stops.
display -a [%format] <expression>
display -d <number>
The '-a' option adds an expression to the list of expression printed every time the program stops. The '-d' option removes the specified expression from the list.
If display is called without arguments it will print the value of all expression in the list.`},
{aliases: []string{"dump"}, cmdFn: dump, helpMsg: `Creates a core dump from the current process state
dump <output file>
The core dump is always written in ELF, even on systems (windows, macOS) where this is not customary. For environments other than linux/amd64 threads and registers are dumped in a format that only Delve can read back.`},
{aliases: []string{"transcript"}, cmdFn: transcript, helpMsg: `Appends command output to a file.
transcript [-t] [-x] <output file>
transcript -off
Output of Delve's command is appended to the specified output file. If '-t' is specified and the output file exists it is truncated. If '-x' is specified output to stdout is suppressed instead.
Using the -off option disables the transcript.`},
}
addrecorded := client == nil
if !addrecorded {
if state, err := client.GetStateNonBlocking(); err == nil {
addrecorded = state.Recording
if !addrecorded {
addrecorded = client.Recorded()
}
}
}
if addrecorded {
c.cmds = append(c.cmds,
command{
aliases: []string{"rewind", "rw"},
group: runCmds,
cmdFn: c.rewind,
helpMsg: "Run backwards until breakpoint or start of recorded history.",
},
command{
aliases: []string{"check", "checkpoint"},
cmdFn: checkpoint,
helpMsg: `Creates a checkpoint at the current position.
checkpoint [note]
The "note" is arbitrary text that can be used to identify the checkpoint, if it is not specified it defaults to the current filename:line position.`,
},
command{
aliases: []string{"checkpoints"},
cmdFn: checkpoints,
helpMsg: "Print out info for existing checkpoints.",
},
command{
aliases: []string{"clear-checkpoint", "clearcheck"},
cmdFn: clearCheckpoint,
helpMsg: `Deletes checkpoint.
clear-checkpoint <id>`,
},
command{
aliases: []string{"rev"},
group: runCmds,
cmdFn: c.revCmd,
helpMsg: `Reverses the execution of the target program for the command specified.
Currently, rev next, step, step-instruction and stepout commands are supported.`,
})
}
sort.Sort(byFirstAlias(c.cmds))
return c
}
// Register custom commands. Expects cf to be a func of type cmdfunc,
// returning only an error.
func (c *Commands) Register(cmdstr string, cf cmdfunc, helpMsg string) {
for _, v := range c.cmds {
if v.match(cmdstr) {
v.cmdFn = cf
return
}
}
c.cmds = append(c.cmds, command{aliases: []string{cmdstr}, cmdFn: cf, helpMsg: helpMsg})
}
// Find will look up the command function for the given command input.
// If it cannot find the command it will default to noCmdAvailable().
// If the command is an empty string it will replay the last command.
func (c *Commands) Find(cmdstr string, prefix cmdPrefix) command {
// If <enter> use last command, if there was one.
if cmdstr == "" {
return command{aliases: []string{"nullcmd"}, cmdFn: nullCommand}
}
for _, v := range c.cmds {
if v.match(cmdstr) {
if prefix != noPrefix && v.allowedPrefixes&prefix == 0 {
continue
}
return v
}
}
return command{aliases: []string{"nocmd"}, cmdFn: noCmdAvailable}
}
// CallWithContext takes a command and a context that command should be executed in.
func (c *Commands) CallWithContext(cmdstr string, t *Term, ctx callContext) error {
vals := strings.SplitN(strings.TrimSpace(cmdstr), " ", 2)
cmdname := vals[0]
var args string
if len(vals) > 1 {
args = strings.TrimSpace(vals[1])
}
return c.Find(cmdname, ctx.Prefix).cmdFn(t, ctx, args)
}
// Call takes a command to execute.
func (c *Commands) Call(cmdstr string, t *Term) error {
ctx := callContext{Prefix: noPrefix, Scope: api.EvalScope{GoroutineID: -1, Frame: c.frame, DeferredCall: 0}}
return c.CallWithContext(cmdstr, t, ctx)
}
// Merge takes aliases defined in the config struct and merges them with the default aliases.
func (c *Commands) Merge(allAliases map[string][]string) {
for i := range c.cmds {
if c.cmds[i].builtinAliases != nil {
c.cmds[i].aliases = append(c.cmds[i].aliases[:0], c.cmds[i].builtinAliases...)
}
}
for i := range c.cmds {
if aliases, ok := allAliases[c.cmds[i].aliases[0]]; ok {
if c.cmds[i].builtinAliases == nil {
c.cmds[i].builtinAliases = make([]string, len(c.cmds[i].aliases))
copy(c.cmds[i].builtinAliases, c.cmds[i].aliases)
}
c.cmds[i].aliases = append(c.cmds[i].aliases, aliases...)
}
}
}
var errNoCmd = errors.New("command not available")
func noCmdAvailable(t *Term, ctx callContext, args string) error {
return errNoCmd
}
func nullCommand(t *Term, ctx callContext, args string) error {
return nil
}
func (c *Commands) help(t *Term, ctx callContext, args string) error {
if args != "" {
for _, cmd := range c.cmds {
for _, alias := range cmd.aliases {
if alias == args {
fmt.Fprintln(t.stdout, cmd.helpMsg)
return nil
}
}
}
return errNoCmd
}
fmt.Fprintln(t.stdout, "The following commands are available:")
for _, cgd := range commandGroupDescriptions {
fmt.Fprintf(t.stdout, "\n%s:\n", cgd.description)
w := new(tabwriter.Writer)
w.Init(t.stdout, 0, 8, 0, '-', 0)
for _, cmd := range c.cmds {
if cmd.group != cgd.group {
continue
}
h := cmd.helpMsg
if idx := strings.Index(h, "\n"); idx >= 0 {
h = h[:idx]
}
if len(cmd.aliases) > 1 {
fmt.Fprintf(w, " %s (alias: %s) \t %s\n", cmd.aliases[0], strings.Join(cmd.aliases[1:], " | "), h)
} else {
fmt.Fprintf(w, " %s \t %s\n", cmd.aliases[0], h)
}
}
if err := w.Flush(); err != nil {
return err
}
}
fmt.Fprintln(t.stdout)
fmt.Fprintln(t.stdout, "Type help followed by a command for full documentation.")
return nil
}
type byThreadID []*api.Thread
func (a byThreadID) Len() int { return len(a) }
func (a byThreadID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byThreadID) Less(i, j int) bool { return a[i].ID < a[j].ID }
func threads(t *Term, ctx callContext, args string) error {
threads, err := t.client.ListThreads()
if err != nil {
return err
}
state, err := t.client.GetState()
if err != nil {
return err
}
sort.Sort(byThreadID(threads))
done := false
t.stdout.pw.PageMaybe(func() { done = false })
for _, th := range threads {
if done {
break
}
prefix := " "
if state.CurrentThread != nil && state.CurrentThread.ID == th.ID {
prefix = "* "
}
if th.Function != nil {
fmt.Fprintf(t.stdout, "%sThread %d at %#v %s:%d %s\n",
prefix, th.ID, th.PC, t.formatPath(th.File),
th.Line, th.Function.Name())
} else {
fmt.Fprintf(t.stdout, "%sThread %s\n", prefix, t.formatThread(th))
}
}
return nil
}
func thread(t *Term, ctx callContext, args string) error {
if len(args) == 0 {
return fmt.Errorf("you must specify a thread")
}
tid, err := strconv.Atoi(args)
if err != nil {
return err
}
oldState, err := t.client.GetState()
if err != nil {
return err
}
newState, err := t.client.SwitchThread(tid)
if err != nil {
return err
}
oldThread := "<none>"
newThread := "<none>"
if oldState.CurrentThread != nil {
oldThread = strconv.Itoa(oldState.CurrentThread.ID)
}
if newState.CurrentThread != nil {
newThread = strconv.Itoa(newState.CurrentThread.ID)
}
fmt.Fprintf(t.stdout, "Switched from %s to %s\n", oldThread, newThread)
return nil
}
type byGoroutineID []*api.Goroutine
func (a byGoroutineID) Len() int { return len(a) }
func (a byGoroutineID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byGoroutineID) Less(i, j int) bool { return a[i].ID < a[j].ID }
func (c *Commands) printGoroutines(t *Term, ctx callContext, indent string, gs []*api.Goroutine, fgl api.FormatGoroutineLoc, flags api.PrintGoroutinesFlags, depth int, cmd string, pdone *bool, state *api.DebuggerState) error {
for _, g := range gs {
if t.longCommandCanceled() || (pdone != nil && *pdone) {
break
}
prefix := indent + " "
if state.SelectedGoroutine != nil && g.ID == state.SelectedGoroutine.ID {
prefix = indent + "* "
}
fmt.Fprintf(t.stdout, "%sGoroutine %s\n", prefix, t.formatGoroutine(g, fgl))
if flags&api.PrintGoroutinesLabels != 0 {
writeGoroutineLabels(t.stdout, g, indent+"\t")
}
if flags&api.PrintGoroutinesStack != 0 {
stack, err := t.client.Stacktrace(g.ID, depth, 0, nil)
if err != nil {
return err
}
printStack(t, t.stdout, stack, indent+"\t", false)
}
if cmd != "" {
ctx.Scope.GoroutineID = g.ID
if err := c.CallWithContext(cmd, t, ctx); err != nil {
return err
}
}
}
return nil
}
func (c *Commands) goroutines(t *Term, ctx callContext, argstr string) error {
filters, group, fgl, flags, depth, batchSize, cmd, err := api.ParseGoroutineArgs(argstr)
if err != nil {
return err
}
state, err := t.client.GetState()
if err != nil {
return err
}
var (
start = 0
gslen = 0
gs []*api.Goroutine
groups []api.GoroutineGroup
tooManyGroups bool
)
done := false
t.stdout.pw.PageMaybe(func() { done = true })
t.longCommandStart()
for start >= 0 {
if t.longCommandCanceled() || done {
fmt.Fprintf(t.stdout, "interrupted\n")
return nil
}
gs, groups, start, tooManyGroups, err = t.client.ListGoroutinesWithFilter(start, batchSize, filters, &group)
if err != nil {
return err
}
if len(groups) > 0 {
for i := range groups {
fmt.Fprintf(t.stdout, "%s\n", groups[i].Name)
err = c.printGoroutines(t, ctx, "\t", gs[groups[i].Offset:][:groups[i].Count], fgl, flags, depth, cmd, &done, state)
if err != nil {
return err
}
fmt.Fprintf(t.stdout, "\tTotal: %d\n", groups[i].Total)
if i != len(groups)-1 {
fmt.Fprintf(t.stdout, "\n")
}
}
if tooManyGroups {
fmt.Fprintf(t.stdout, "Too many groups\n")
}
} else {
sort.Sort(byGoroutineID(gs))
err = c.printGoroutines(t, ctx, "", gs, fgl, flags, depth, cmd, &done, state)
if err != nil {
return err
}
gslen += len(gs)
}
}
if gslen > 0 {
fmt.Fprintf(t.stdout, "[%d goroutines]\n", gslen)
}
return nil
}
func selectedGID(state *api.DebuggerState) int64 {
if state.SelectedGoroutine == nil {
return 0
}
return state.SelectedGoroutine.ID
}
func (c *Commands) goroutine(t *Term, ctx callContext, argstr string) error {
args := config.Split2PartsBySpace(argstr)
if ctx.Prefix == onPrefix {
if len(args) != 1 || args[0] != "" {
return errors.New("too many arguments to goroutine")
}
ctx.Breakpoint.Goroutine = true
return nil
}
if len(args) == 1 {
if args[0] == "" {
return printscope(t)
}
gid, err := strconv.ParseInt(argstr, 10, 64)
if err != nil {
return err
}
oldState, err := t.client.GetState()
if err != nil {
return err
}
newState, err := t.client.SwitchGoroutine(gid)
if err != nil {
return err
}
c.frame = 0
fmt.Fprintf(t.stdout, "Switched from %d to %d (thread %d)\n", selectedGID(oldState), gid, newState.CurrentThread.ID)
return nil
}
var err error
ctx.Scope.GoroutineID, err = strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}
return c.CallWithContext(args[1], t, ctx)
}
// Handle "frame", "up", "down" commands.
func (c *Commands) frameCommand(t *Term, ctx callContext, argstr string, direction frameDirection) error {
frame := 1
arg := ""
if len(argstr) == 0 {
if direction == frameSet {
return errors.New("not enough arguments")
}
} else {
args := config.Split2PartsBySpace(argstr)
var err error
if frame, err = strconv.Atoi(args[0]); err != nil {
return err
}
if len(args) > 1 {
arg = args[1]
}
}
switch direction {
case frameUp:
frame = c.frame + frame
case frameDown:
frame = c.frame - frame
}
if len(arg) > 0 {
ctx.Scope.Frame = frame
return c.CallWithContext(arg, t, ctx)
}
if frame < 0 {
return fmt.Errorf("Invalid frame %d", frame)
}
stack, err := t.client.Stacktrace(ctx.Scope.GoroutineID, frame, 0, nil)
if err != nil {
return err
}
if frame >= len(stack) {
return fmt.Errorf("Invalid frame %d", frame)
}
c.frame = frame
state, err := t.client.GetState()
if err != nil {
return err
}
printcontext(t, state)
th := stack[frame]
fmt.Fprintf(t.stdout, "Frame %d: %s:%d (PC: %x)\n", frame, t.formatPath(th.File), th.Line, th.PC)
printfile(t, th.File, th.Line, true)
return nil
}
func (c *Commands) deferredCommand(t *Term, ctx callContext, argstr string) error {
ctx.Prefix = deferredPrefix
space := strings.IndexRune(argstr, ' ')
if space < 0 {
return errors.New("not enough arguments")
}
var err error
ctx.Scope.DeferredCall, err = strconv.Atoi(argstr[:space])
if err != nil {
return err
}
if ctx.Scope.DeferredCall <= 0 {
return errors.New("argument of deferred must be a number greater than 0 (use 'stack -defer' to see the list of deferred calls)")
}
return c.CallWithContext(argstr[space:], t, ctx)
}
func printscope(t *Term) error {
state, err := t.client.GetState()
if err != nil {
return err
}
fmt.Fprintf(t.stdout, "Thread %s\n", t.formatThread(state.CurrentThread))
if state.SelectedGoroutine != nil {
writeGoroutineLong(t, t.stdout, state.SelectedGoroutine, "")
}
return nil
}
func (t *Term) formatThread(th *api.Thread) string {
if th == nil {
return "<nil>"
}
return fmt.Sprintf("%d at %s:%d", th.ID, t.formatPath(th.File), th.Line)
}
func (t *Term) formatLocation(loc api.Location) string {
return fmt.Sprintf("%s:%d %s (%#v)", t.formatPath(loc.File), loc.Line, loc.Function.Name(), loc.PC)
}
func (t *Term) formatGoroutine(g *api.Goroutine, fgl api.FormatGoroutineLoc) string {
if g == nil {
return "<nil>"
}
if g.Unreadable != "" {
return fmt.Sprintf("(unreadable %s)", g.Unreadable)
}
var locname string
var loc api.Location
switch fgl {
case api.FglRuntimeCurrent:
locname = "Runtime"
loc = g.CurrentLoc
case api.FglUserCurrent:
locname = "User"
loc = g.UserCurrentLoc
case api.FglGo:
locname = "Go"
loc = g.GoStatementLoc
case api.FglStart:
locname = "Start"
loc = g.StartLoc
}
buf := new(strings.Builder)
fmt.Fprintf(buf, "%d - %s: %s", g.ID, locname, t.formatLocation(loc))
if g.ThreadID != 0 {
fmt.Fprintf(buf, " (thread %d)", g.ThreadID)
}
if (g.Status == api.GoroutineWaiting || g.Status == api.GoroutineSyscall) && g.WaitReason != 0 {
var wr string
if g.WaitReason > 0 && g.WaitReason < int64(len(waitReasonStrings)) {
wr = waitReasonStrings[g.WaitReason]
} else {
wr = fmt.Sprintf("unknown wait reason %d", g.WaitReason)
}
fmt.Fprintf(buf, " [%s", wr)
if g.WaitSince > 0 {
fmt.Fprintf(buf, " %d", g.WaitSince)
}
fmt.Fprintf(buf, "]")
}
return buf.String()
}
var waitReasonStrings = [...]string{
"",
"GC assist marking",
"IO wait",
"chan receive (nil chan)",
"chan send (nil chan)",
"dumping heap",
"garbage collection",
"garbage collection scan",
"panicwait",
"select",
"select (no cases)",
"GC assist wait",
"GC sweep wait",
"GC scavenge wait",
"chan receive",
"chan send",
"finalizer wait",
"force gc (idle)",
"semacquire",
"sleep",
"sync.Cond.Wait",
"timer goroutine (idle)",
"trace reader (blocked)",
"wait for GC cycle",
"GC worker (idle)",
"preempted",
"debug call",
}
func writeGoroutineLong(t *Term, w io.Writer, g *api.Goroutine, prefix string) {
fmt.Fprintf(w, "%sGoroutine %d:\n%s\tRuntime: %s\n%s\tUser: %s\n%s\tGo: %s\n%s\tStart: %s\n",
prefix, g.ID,
prefix, t.formatLocation(g.CurrentLoc),
prefix, t.formatLocation(g.UserCurrentLoc),
prefix, t.formatLocation(g.GoStatementLoc),
prefix, t.formatLocation(g.StartLoc))
writeGoroutineLabels(w, g, prefix+"\t")
}
func writeGoroutineLabels(w io.Writer, g *api.Goroutine, prefix string) {
const maxNumberOfGoroutineLabels = 5
if len(g.Labels) <= 0 {
return
}
keys := make([]string, 0, len(g.Labels))
for k := range g.Labels {
keys = append(keys, k)
}
sort.Strings(keys)
more := false
if len(keys) > maxNumberOfGoroutineLabels {
more = true
keys = keys[:maxNumberOfGoroutineLabels]
}
fmt.Fprintf(w, "%sLabels: ", prefix)
for i, k := range keys {
fmt.Fprintf(w, "%q:%q", k, g.Labels[k])
if i != len(keys)-1 {
fmt.Fprintf(w, ", ")
} else if more {
fmt.Fprintf(w, "... (%d more)", len(g.Labels)-maxNumberOfGoroutineLabels)
}
}
fmt.Fprintf(w, "\n")
}
func restart(t *Term, ctx callContext, args string) error {
if t.client.Recorded() {
return restartRecorded(t, ctx, args)
}
return restartLive(t, ctx, args)
}
func restartRecorded(t *Term, ctx callContext, args string) error {
v := config.Split2PartsBySpace(args)
rerecord := false
resetArgs := false
newArgv := []string{}
newRedirects := [3]string{}
restartPos := ""
if len(v) > 0 {
if v[0] == "-r" {
rerecord = true
if len(v) == 2 {
var err error
resetArgs, newArgv, newRedirects, err = parseNewArgv(v[1])
if err != nil {
return err
}
}
} else {
if len(v) > 1 {
return fmt.Errorf("too many arguments to restart")
}
restartPos = v[0]
}
}
if err := restartIntl(t, rerecord, restartPos, resetArgs, newArgv, newRedirects); err != nil {
return err
}
state, err := t.client.GetState()
if err != nil {
return err
}
printcontext(t, state)
printPos(t, state.CurrentThread, printPosShowArrow)
t.onStop()
return nil
}
// parseOptionalCount parses an optional count argument.
// If there are not arguments, a value of 1 is returned as the default.
func parseOptionalCount(arg string) (int64, error) {
if len(arg) == 0 {
return 1, nil
}
return strconv.ParseInt(arg, 0, 64)
}
func restartLive(t *Term, ctx callContext, args string) error {
resetArgs, newArgv, newRedirects, err := parseNewArgv(args)
if err != nil {
return err
}
if err := restartIntl(t, false, "", resetArgs, newArgv, newRedirects); err != nil {
return err
}
fmt.Fprintln(t.stdout, "Process restarted with PID", t.client.ProcessPid())
return nil
}
func restartIntl(t *Term, rerecord bool, restartPos string, resetArgs bool, newArgv []string, newRedirects [3]string) error {
discarded, err := t.client.RestartFrom(rerecord, restartPos, resetArgs, newArgv, newRedirects, false)
if err != nil {
return err
}
for i := range discarded {
fmt.Fprintf(t.stdout, "Discarded %s at %s: %v\n", formatBreakpointName(discarded[i].Breakpoint, false), t.formatBreakpointLocation(discarded[i].Breakpoint), discarded[i].Reason)
}
return nil
}
func parseNewArgv(args string) (resetArgs bool, newArgv []string, newRedirects [3]string, err error) {
if args == "" {
return false, nil, [3]string{}, nil
}
v, err := argv.Argv(args,
func(s string) (string, error) {
return "", fmt.Errorf("Backtick not supported in '%s'", s)
},
nil)
if err != nil {
return false, nil, [3]string{}, err
}
if len(v) != 1 {
return false, nil, [3]string{}, fmt.Errorf("illegal commandline '%s'", args)
}
w := v[0]
if len(w) == 0 {
return false, nil, [3]string{}, nil
}
if w[0] == "-noargs" {
if len(w) > 1 {
return false, nil, [3]string{}, fmt.Errorf("too many arguments to restart")
}
return true, nil, [3]string{}, nil
}
redirs := [3]string{}
for len(w) > 0 {
var found bool
var err error
w, found, err = parseOneRedirect(w, &redirs)
if err != nil {
return false, nil, [3]string{}, err
}
if !found {
break
}
}
return true, w, redirs, nil
}
func parseOneRedirect(w []string, redirs *[3]string) ([]string, bool, error) {
prefixes := []string{"<", ">", "2>"}
names := []string{"stdin", "stdout", "stderr"}
if len(w) >= 2 {
for _, prefix := range prefixes {
if w[len(w)-2] == prefix {
w[len(w)-2] += w[len(w)-1]
w = w[:len(w)-1]
break
}
}
}
for i, prefix := range prefixes {
if strings.HasPrefix(w[len(w)-1], prefix) {
if redirs[i] != "" {
return nil, false, fmt.Errorf("redirect error: %s redirected twice", names[i])
}
redirs[i] = w[len(w)-1][len(prefix):]
return w[:len(w)-1], true, nil
}
}
return w, false, nil
}
func printcontextNoState(t *Term) {
state, _ := t.client.GetState()
if state == nil || state.CurrentThread == nil {
return
}
printcontext(t, state)
}
func (c *Commands) rebuild(t *Term, ctx callContext, args string) error {
if ctx.Prefix == revPrefix {
return c.rewind(t, ctx, args)
}
defer t.onStop()
discarded, err := t.client.Restart(true)
if len(discarded) > 0 {
fmt.Fprintf(t.stdout, "not all breakpoints could be restored.")
}
return err
}
func (c *Commands) cont(t *Term, ctx callContext, args string) error {
if args != "" {
tmp, err := setBreakpoint(t, ctx, false, args)
if err != nil {
if !strings.Contains(err.Error(), "Breakpoint exists") {
return err
}
}
defer func() {
for _, bp := range tmp {
if _, err := t.client.ClearBreakpoint(bp.ID); err != nil {
fmt.Fprintf(t.stdout, "failed to clear temporary breakpoint: %d", bp.ID)
}
}
}()
}
if ctx.Prefix == revPrefix {
return c.rewind(t, ctx, args)
}
defer t.onStop()
c.frame = 0
stateChan := t.client.Continue()
var state *api.DebuggerState
for state = range stateChan {
if state.Err != nil {
printcontextNoState(t)
return state.Err
}
printcontext(t, state)
}
printPos(t, state.CurrentThread, printPosShowArrow)
return nil
}
func continueUntilCompleteNext(t *Term, state *api.DebuggerState, op string, shouldPrintFile bool) error {
defer t.onStop()
if !state.NextInProgress {
if shouldPrintFile {
printPos(t, state.CurrentThread, printPosShowArrow)
}
return nil
}
skipBreakpoints := false
for {
fmt.Fprintf(t.stdout, "\tbreakpoint hit during %s", op)
if !skipBreakpoints {
fmt.Fprintf(t.stdout, "\n")
answer, err := promptAutoContinue(t, op)
switch answer {
case "f": // finish next
skipBreakpoints = true
fallthrough
case "c": // continue once
fmt.Fprintf(t.stdout, "continuing...\n")
case "s": // stop and cancel
fallthrough
default:
t.client.CancelNext()
printPos(t, state.CurrentThread, printPosShowArrow)
return err
}
} else {
fmt.Fprintf(t.stdout, ", continuing...\n")
}
stateChan := t.client.DirectionCongruentContinue()
var state *api.DebuggerState
for state = range stateChan {
if state.Err != nil {
printcontextNoState(t)
return state.Err
}
printcontext(t, state)
}
if !state.NextInProgress {
printPos(t, state.CurrentThread, printPosShowArrow)
return nil
}
}
}
func promptAutoContinue(t *Term, op string) (string, error) {
for {
answer, err := t.line.Prompt(fmt.Sprintf("[c] continue [s] stop here and cancel %s, [f] finish %s skipping all breakpoints? ", op, op))
if err != nil {
return "", err
}
answer = strings.ToLower(strings.TrimSpace(answer))
switch answer {
case "f", "c", "s":
return answer, nil
}
}
}
func scopePrefixSwitch(t *Term, ctx callContext) error {
if ctx.Scope.GoroutineID > 0 {
_, err := t.client.SwitchGoroutine(ctx.Scope.GoroutineID)
if err != nil {
return err
}
}
return nil
}
func exitedToError(state *api.DebuggerState, err error) (*api.DebuggerState, error) {
if err == nil && state.Exited {
return nil, fmt.Errorf("Process %d has exited with status %d", state.Pid, state.ExitStatus)
}
return state, err
}
func (c *Commands) step(t *Term, ctx callContext, args string) error {
if err := scopePrefixSwitch(t, ctx); err != nil {
return err
}
c.frame = 0
stepfn := t.client.Step
if ctx.Prefix == revPrefix {
stepfn = t.client.ReverseStep
}
state, err := exitedToError(stepfn())
if err != nil {
printcontextNoState(t)
return err
}
printcontext(t, state)
return continueUntilCompleteNext(t, state, "step", true)
}
var errNotOnFrameZero = errors.New("not on topmost frame")
func (c *Commands) stepInstruction(t *Term, ctx callContext, args string) error {
if err := scopePrefixSwitch(t, ctx); err != nil {
return err
}
if c.frame != 0 {
return errNotOnFrameZero
}
defer t.onStop()
var fn func() (*api.DebuggerState, error)
if ctx.Prefix == revPrefix {
fn = t.client.ReverseStepInstruction
} else {
fn = t.client.StepInstruction
}
state, err := exitedToError(fn())
if err != nil {
printcontextNoState(t)
return err
}
printcontext(t, state)
printPos(t, state.CurrentThread, printPosShowArrow|printPosStepInstruction)
return nil
}
func (c *Commands) revCmd(t *Term, ctx callContext, args string) error {
if len(args) == 0 {
return errors.New("not enough arguments")
}
ctx.Prefix = revPrefix
return c.CallWithContext(args, t, ctx)
}
func (c *Commands) next(t *Term, ctx callContext, args string) error {
if err := scopePrefixSwitch(t, ctx); err != nil {
return err
}
if c.frame != 0 {
return errNotOnFrameZero
}
nextfn := t.client.Next
if ctx.Prefix == revPrefix {
nextfn = t.client.ReverseNext
}
var count int64
var err error
if count, err = parseOptionalCount(args); err != nil {
return err
} else if count <= 0 {
return errors.New("Invalid next count")
}
for ; count > 0; count-- {
state, err := exitedToError(nextfn())
if err != nil {
printcontextNoState(t)
return err
}
// If we're about the exit the loop, print the context.
finishedNext := count == 1
if finishedNext {
printcontext(t, state)
}
if err := continueUntilCompleteNext(t, state, "next", finishedNext); err != nil {
return err
}
}
return nil
}
func (c *Commands) stepout(t *Term, ctx callContext, args string) error {
if err := scopePrefixSwitch(t, ctx); err != nil {
return err
}
if c.frame != 0 {
return errNotOnFrameZero
}
stepoutfn := t.client.StepOut
if ctx.Prefix == revPrefix {
stepoutfn = t.client.ReverseStepOut
}
state, err := exitedToError(stepoutfn())
if err != nil {
printcontextNoState(t)
return err
}
printcontext(t, state)
return continueUntilCompleteNext(t, state, "stepout", true)
}
func (c *Commands) call(t *Term, ctx callContext, args string) error {
if err := scopePrefixSwitch(t, ctx); err != nil {
return err
}
const unsafePrefix = "-unsafe "
unsafe := false
if strings.HasPrefix(args, unsafePrefix) {
unsafe = true
args = args[len(unsafePrefix):]
}
state, err := exitedToError(t.client.Call(ctx.Scope.GoroutineID, args, unsafe))
c.frame = 0
if err != nil {
printcontextNoState(t)
return err
}
printcontext(t, state)
return continueUntilCompleteNext(t, state, "call", true)
}
func clear(t *Term, ctx callContext, args string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments")
}
id, err := strconv.Atoi(args)
var bp *api.Breakpoint
if err == nil {
bp, err = t.client.ClearBreakpoint(id)
} else {
bp, err = t.client.ClearBreakpointByName(args)
}
if err != nil {
return err
}
fmt.Fprintf(t.stdout, "%s cleared at %s\n", formatBreakpointName(bp, true), t.formatBreakpointLocation(bp))
return nil
}
func clearAll(t *Term, ctx callContext, args string) error {
breakPoints, err := t.client.ListBreakpoints(false)
if err != nil {
return err
}
var locPCs map[uint64]struct{}
if args != "" {
locs, err := t.client.FindLocation(api.EvalScope{GoroutineID: -1, Frame: 0}, args, true, t.substitutePathRules())
if err != nil {
return err
}
locPCs = make(map[uint64]struct{})
for _, loc := range locs {
for _, pc := range loc.PCs {
locPCs[pc] = struct{}{}
}
locPCs[loc.PC] = struct{}{}
}
}
for _, bp := range breakPoints {
if locPCs != nil {
if _, ok := locPCs[bp.Addr]; !ok {
continue
}
}
if bp.ID < 0 {
continue
}
_, err := t.client.ClearBreakpoint(bp.ID)
if err != nil {
fmt.Fprintf(t.stdout, "Couldn't delete %s at %s: %s\n", formatBreakpointName(bp, false), t.formatBreakpointLocation(bp), err)
}
fmt.Fprintf(t.stdout, "%s cleared at %s\n", formatBreakpointName(bp, true), t.formatBreakpointLocation(bp))
}
return nil
}
func toggle(t *Term, ctx callContext, args string) error {
if args == "" {
return fmt.Errorf("not enough arguments")
}
id, err := strconv.Atoi(args)
var bp *api.Breakpoint
if err == nil {
bp, err = t.client.ToggleBreakpoint(id)
} else {
bp, err = t.client.ToggleBreakpointByName(args)
}
if err != nil {
return err
}
fmt.Fprintf(t.stdout, "%s toggled at %s\n", formatBreakpointName(bp, true), t.formatBreakpointLocation(bp))
return nil
}
// byID sorts breakpoints by ID.
type byID []*api.Breakpoint
func (a byID) Len() int { return len(a) }
func (a byID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byID) Less(i, j int) bool { return a[i].ID < a[j].ID }
func breakpoints(t *Term, ctx callContext, args string) error {
breakPoints, err := t.client.ListBreakpoints(args == "-a")
if err != nil {
return err
}
sort.Sort(byID(breakPoints))
for _, bp := range breakPoints {
enabled := "(enabled)"
if bp.Disabled {
enabled = "(disabled)"
}
fmt.Fprintf(t.stdout, "%s %s at %v (%d)\n", formatBreakpointName(bp, true), enabled, t.formatBreakpointLocation(bp), bp.TotalHitCount)
attrs := formatBreakpointAttrs("\t", bp, false)
if len(attrs) > 0 {
fmt.Fprintf(t.stdout, "%s\n", strings.Join(attrs, "\n"))
}
}
return nil
}
func formatBreakpointAttrs(prefix string, bp *api.Breakpoint, includeTrace bool) []string {
var attrs []string
if bp.Cond != "" {
attrs = append(attrs, fmt.Sprintf("%scond %s", prefix, bp.Cond))
}
if bp.HitCond != "" {
if bp.HitCondPerG {
attrs = append(attrs, fmt.Sprintf("%scond -per-g-hitcount %s", prefix, bp.HitCond))
} else {
attrs = append(attrs, fmt.Sprintf("%scond -hitcount %s", prefix, bp.HitCond))
}
}
if bp.Stacktrace > 0 {
attrs = append(attrs, fmt.Sprintf("%sstack %d", prefix, bp.Stacktrace))
}
if bp.Goroutine {
attrs = append(attrs, fmt.Sprintf("%sgoroutine", prefix))
}
if bp.LoadArgs != nil {
if *(bp.LoadArgs) == longLoadConfig {
attrs = append(attrs, fmt.Sprintf("%sargs -v", prefix))
} else {
attrs = append(attrs, fmt.Sprintf("%sargs", prefix))
}
}
if bp.LoadLocals != nil {
if *(bp.LoadLocals) == longLoadConfig {
attrs = append(attrs, fmt.Sprintf("%slocals -v", prefix))
} else {
attrs = append(attrs, fmt.Sprintf("%slocals", prefix))
}
}
for i := range bp.Variables {
attrs = append(attrs, fmt.Sprintf("%sprint %s", prefix, bp.Variables[i]))
}
if includeTrace && bp.Tracepoint {
attrs = append(attrs, fmt.Sprintf("%strace", prefix))
}
for i := range bp.VerboseDescr {
attrs = append(attrs, fmt.Sprintf("%s%s", prefix, bp.VerboseDescr[i]))
}
return attrs
}
func setBreakpoint(t *Term, ctx callContext, tracepoint bool, argstr string) ([]*api.Breakpoint, error) {
args := config.Split2PartsBySpace(argstr)
requestedBp := &api.Breakpoint{}
spec := ""
switch len(args) {
case 1:
if len(args[0]) != 0 {
spec = argstr
} else {
// no arg specified
spec = "+0"
}
case 2:
if api.ValidBreakpointName(args[0]) == nil {
requestedBp.Name = args[0]
spec = args[1]
} else {
spec = argstr
}
default:
return nil, fmt.Errorf("address required")
}
requestedBp.Tracepoint = tracepoint
locs, findLocErr := t.client.FindLocation(ctx.Scope, spec, true, t.substitutePathRules())
if findLocErr != nil && requestedBp.Name != "" {
requestedBp.Name = ""
spec = argstr
var err2 error
locs, err2 = t.client.FindLocation(ctx.Scope, spec, true, t.substitutePathRules())
if err2 == nil {
findLocErr = nil
}
}
if findLocErr != nil && shouldAskToSuspendBreakpoint(t) {
fmt.Fprintf(os.Stderr, "Command failed: %s\n", findLocErr.Error())
question := "Set a suspended breakpoint (Delve will try to set this breakpoint when a plugin is loaded) [Y/n]?"
if isErrProcessExited(findLocErr) {
question = "Set a suspended breakpoint (Delve will try to set this breakpoint when the process is restarted) [Y/n]?"
}
answer, err := yesno(t.line, question, "yes")
if err != nil {
return nil, err
}
if !answer {
return nil, nil
}
findLocErr = nil
bp, err := t.client.CreateBreakpointWithExpr(requestedBp, spec, t.substitutePathRules(), true)
if err != nil {
return nil, err
}
fmt.Fprintf(t.stdout, "%s set at %s\n", formatBreakpointName(bp, true), t.formatBreakpointLocation(bp))
return nil, nil
}
if findLocErr != nil {
return nil, findLocErr
}
created := []*api.Breakpoint{}
for _, loc := range locs {
requestedBp.Addr = loc.PC
requestedBp.Addrs = loc.PCs
requestedBp.AddrPid = loc.PCPids
if tracepoint {
requestedBp.LoadArgs = &ShortLoadConfig
}
bp, err := t.client.CreateBreakpointWithExpr(requestedBp, spec, t.substitutePathRules(), false)
if err != nil {
return nil, err
}
created = append(created, bp)
fmt.Fprintf(t.stdout, "%s set at %s\n", formatBreakpointName(bp, true), t.formatBreakpointLocation(bp))
}
var shouldSetReturnBreakpoints bool
loc, err := locspec.Parse(spec)
if err != nil {
return nil, err
}
switch t := loc.(type) {
case *locspec.NormalLocationSpec:
shouldSetReturnBreakpoints = t.LineOffset == -1 && t.FuncBase != nil
case *locspec.RegexLocationSpec:
shouldSetReturnBreakpoints = true
}
if tracepoint && shouldSetReturnBreakpoints && locs[0].Function != nil {
for i := range locs {
if locs[i].Function == nil {
continue
}
addrs, err := t.client.(*rpc2.RPCClient).FunctionReturnLocations(locs[0].Function.Name())
if err != nil {
return nil, err
}
for j := range addrs {
_, err = t.client.CreateBreakpoint(&api.Breakpoint{
Addr: addrs[j],
TraceReturn: true,
Line: -1,
LoadArgs: &ShortLoadConfig,
})
if err != nil {
return nil, err
}
}
}
}
return created, nil
}
func breakpoint(t *Term, ctx callContext, args string) error {
_, err := setBreakpoint(t, ctx, false, args)
return err
}
func tracepoint(t *Term, ctx callContext, args string) error {
if ctx.Prefix == onPrefix {
if args != "" {
return errors.New("too many arguments to trace")
}
ctx.Breakpoint.Tracepoint = true
return nil
}
_, err := setBreakpoint(t, ctx, true, args)
return err
}
func runEditor(args ...string) error {
var editor string
if editor = os.Getenv("DELVE_EDITOR"); editor == "" {
if editor = os.Getenv("EDITOR"); editor == "" {
return fmt.Errorf("Neither DELVE_EDITOR or EDITOR is set")
}
}
cmd := exec.Command(editor, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func edit(t *Term, ctx callContext, args string) error {
file, lineno, _, err := getLocation(t, ctx, args, false)
if err != nil {
return err
}
return runEditor(fmt.Sprintf("+%d", lineno), file)
}
func watchpoint(t *Term, ctx callContext, args string) error {
v := strings.SplitN(args, " ", 2)
if len(v) != 2 {
return errors.New("wrong number of arguments: watch [-r|-w|-rw] <expr>")
}
var wtype api.WatchType
switch v[0] {
case "-r":
wtype = api.WatchRead
case "-w":
wtype = api.WatchWrite
case "-rw":
wtype = api.WatchRead | api.WatchWrite
default:
return fmt.Errorf("wrong argument %q to watch", v[0])
}
bp, err := t.client.CreateWatchpoint(ctx.Scope, v[1], wtype)
if err != nil {
return err
}
fmt.Fprintf(t.stdout, "%s set at %s\n", formatBreakpointName(bp, true), t.formatBreakpointLocation(bp))
return nil
}
func examineMemoryCmd(t *Term, ctx callContext, argstr string) error {
var (
address uint64
err error
ok bool
args = strings.Split(argstr, " ")
)
// Default value
priFmt := byte('x')
count := 1
size := 1
isExpr := false
// nextArg returns the next argument that is not an empty string, if any, and
// advances the args slice to the position after that.
nextArg := func() string {
for len(args) > 0 {
arg := args[0]
args = args[1:]
if arg != "" {
return arg
}
}
return ""
}
loop:
for {
switch cmd := nextArg(); cmd {
case "":
// no more arguments
break loop
case "-fmt":
arg := nextArg()
if arg == "" {
return fmt.Errorf("expected argument after -fmt")
}
fmtMapToPriFmt := map[string]byte{
"oct": 'o',
"octal": 'o',
"hex": 'x',
"hexadecimal": 'x',
"dec": 'd',
"decimal": 'd',
"bin": 'b',
"binary": 'b',
}
priFmt, ok = fmtMapToPriFmt[arg]
if !ok {
return fmt.Errorf("%q is not a valid format", arg)
}
case "-count", "-len":
arg := nextArg()
if arg == "" {
return fmt.Errorf("expected argument after -count/-len")
}
var err error
count, err = strconv.Atoi(arg)
if err != nil || count <= 0 {
return fmt.Errorf("count/len must be a positive integer")
}
case "-size":
arg := nextArg()
if arg == "" {
return fmt.Errorf("expected argument after -size")
}
var err error
size, err = strconv.Atoi(arg)
if err != nil || size <= 0 || size > 8 {
return fmt.Errorf("size must be a positive integer (<=8)")
}
case "-x":
isExpr = true
break loop // remaining args are going to be interpreted as expression
default:
if len(args) > 0 {
return fmt.Errorf("unknown option %q", args[0])
}
args = []string{cmd}
break loop // only one arg left to be evaluated as a uint
}
}
// TODO, maybe configured by user.
if count*size > 1000 {
return fmt.Errorf("read memory range (count*size) must be less than or equal to 1000 bytes")
}
if len(args) == 0 {
return fmt.Errorf("no address specified")
}
if isExpr {
expr := strings.Join(args, " ")
val, err := t.client.EvalVariable(ctx.Scope, expr, t.loadConfig())
if err != nil {
return err
}
// "-x &myVar" or "-x myPtrVar"
if val.Kind == reflect.Ptr {
if len(val.Children) < 1 {
return fmt.Errorf("bug? invalid pointer: %#v", val)
}
address = val.Children[0].Addr
// "-x 0xc000079f20 + 8" or -x 824634220320 + 8
} else if val.Kind == reflect.Int && val.Value != "" {
address, err = strconv.ParseUint(val.Value, 0, 64)
if err != nil {
return fmt.Errorf("bad expression result: %q: %s", val.Value, err)
}
} else {
return fmt.Errorf("unsupported expression type: %s", val.Kind)
}
} else {
address, err = strconv.ParseUint(args[0], 0, 64)
if err != nil {
return fmt.Errorf("convert address into uintptr type failed, %s", err)
}
}
memArea, isLittleEndian, err := t.client.ExamineMemory(address, count*size)
if err != nil {
return err
}
t.stdout.pw.PageMaybe(nil)
fmt.Fprint(t.stdout, api.PrettyExamineMemory(uintptr(address), memArea, isLittleEndian, priFmt, size))
return nil
}
func parseFormatArg(args string) (fmtstr, argsOut string) {
if len(args) < 1 || args[0] != '%' {
return "", args
}
v := strings.SplitN(args, " ", 2)
if len(v) == 1 {
return v[0], ""
}
return v[0], v[1]
}
func printVar(t *Term, ctx callContext, args string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments")
}
if ctx.Prefix == onPrefix {
ctx.Breakpoint.Variables = append(ctx.Breakpoint.Variables, args)
return nil
}
fmtstr, args := parseFormatArg(args)
val, err := t.client.EvalVariable(ctx.Scope, args, t.loadConfig())
if err != nil {
return err
}
fmt.Fprintln(t.stdout, val.MultilineString("", fmtstr))
return nil
}
func whatisCommand(t *Term, ctx callContext, args string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments")
}
val, err := t.client.EvalVariable(ctx.Scope, args, ShortLoadConfig)
if err != nil {
return err
}
if val.Flags&api.VariableCPURegister != 0 {
fmt.Fprintln(t.stdout, "CPU Register")
return nil
}
if val.Type != "" {
fmt.Fprintln(t.stdout, val.Type)
}
if val.RealType != val.Type {
fmt.Fprintf(t.stdout, "Real type: %s\n", val.RealType)
}
if val.Kind == reflect.Interface && len(val.Children) > 0 {
fmt.Fprintf(t.stdout, "Concrete type: %s\n", val.Children[0].Type)
}
if t.conf.ShowLocationExpr && val.LocationExpr != "" {
fmt.Fprintf(t.stdout, "location: %s\n", val.LocationExpr)
}
return nil
}
func setVar(t *Term, ctx callContext, args string) error {
// HACK: in go '=' is not an operator, we detect the error and try to recover from it by splitting the input string
_, err := parser.ParseExpr(args)
if err == nil {
return fmt.Errorf("syntax error '=' not found")
}
el, ok := err.(scanner.ErrorList)
if !ok || el[0].Msg != "expected '==', found '='" {
return err
}
lexpr := args[:el[0].Pos.Offset]
rexpr := args[el[0].Pos.Offset+1:]
return t.client.SetVariable(ctx.Scope, lexpr, rexpr)
}
func (t *Term) printFilteredVariables(varType string, vars []api.Variable, filter string, cfg api.LoadConfig) error {
reg, err := regexp.Compile(filter)
if err != nil {
return err
}
match := false
for _, v := range vars {
if reg == nil || reg.Match([]byte(v.Name)) {
match = true
name := v.Name
if v.Flags&api.VariableShadowed != 0 {
name = "(" + name + ")"
}
if cfg == ShortLoadConfig {
fmt.Fprintf(t.stdout, "%s = %s\n", name, v.SinglelineString())
} else {
fmt.Fprintf(t.stdout, "%s = %s\n", name, v.MultilineString("", ""))
}
}
}
if !match {
fmt.Fprintf(t.stdout, "(no %s)\n", varType)
}
return nil
}
func (t *Term) printSortedStrings(v []string, err error) error {
if err != nil {
return err
}
sort.Strings(v)
done := false
t.stdout.pw.PageMaybe(func() { done = false })
for _, d := range v {
if done {
break
}
fmt.Fprintln(t.stdout, d)
}
return nil
}
func sources(t *Term, ctx callContext, args string) error {
return t.printSortedStrings(t.client.ListSources(args))
}
func funcs(t *Term, ctx callContext, args string) error {
return t.printSortedStrings(t.client.ListFunctions(args))
}
func types(t *Term, ctx callContext, args string) error {
return t.printSortedStrings(t.client.ListTypes(args))
}
func parseVarArguments(args string, t *Term) (filter string, cfg api.LoadConfig) {
if v := config.Split2PartsBySpace(args); len(v) >= 1 && v[0] == "-v" {
if len(v) == 2 {
return v[1], t.loadConfig()
} else {
return "", t.loadConfig()
}
}
return args, ShortLoadConfig
}
func args(t *Term, ctx callContext, args string) error {
filter, cfg := parseVarArguments(args, t)
if ctx.Prefix == onPrefix {
if filter != "" {
return fmt.Errorf("filter not supported on breakpoint")
}
ctx.Breakpoint.LoadArgs = &cfg
return nil
}
vars, err := t.client.ListFunctionArgs(ctx.Scope, cfg)
if err != nil {
return err
}
return t.printFilteredVariables("args", vars, filter, cfg)
}
func locals(t *Term, ctx callContext, args string) error {
filter, cfg := parseVarArguments(args, t)
if ctx.Prefix == onPrefix {
if filter != "" {
return fmt.Errorf("filter not supported on breakpoint")
}
ctx.Breakpoint.LoadLocals = &cfg
return nil
}
locals, err := t.client.ListLocalVariables(ctx.Scope, cfg)
if err != nil {
return err
}
return t.printFilteredVariables("locals", locals, filter, cfg)
}
func vars(t *Term, ctx callContext, args string) error {
filter, cfg := parseVarArguments(args, t)
vars, err := t.client.ListPackageVariables(filter, cfg)
if err != nil {
return err
}
return t.printFilteredVariables("vars", vars, filter, cfg)
}
func regs(t *Term, ctx callContext, args string) error {
includeFp := false
if args == "-a" {
includeFp = true
}
var regs api.Registers
var err error
if ctx.Scope.GoroutineID < 0 && ctx.Scope.Frame == 0 {
regs, err = t.client.ListThreadRegisters(0, includeFp)
} else {
regs, err = t.client.ListScopeRegisters(ctx.Scope, includeFp)
}
if err != nil {
return err
}
fmt.Fprintln(t.stdout, regs)
return nil
}
func stackCommand(t *Term, ctx callContext, args string) error {
sa, err := parseStackArgs(args)
if err != nil {
return err
}
if ctx.Prefix == onPrefix {
ctx.Breakpoint.Stacktrace = sa.depth
return nil
}
var cfg *api.LoadConfig
if sa.full {
cfg = &ShortLoadConfig
}
stack, err := t.client.Stacktrace(ctx.Scope.GoroutineID, sa.depth, sa.opts, cfg)
if err != nil {
return err
}
t.stdout.pw.PageMaybe(nil)
printStack(t, t.stdout, stack, "", sa.offsets)
if sa.ancestors > 0 {
ancestors, err := t.client.Ancestors(ctx.Scope.GoroutineID, sa.ancestors, sa.ancestorDepth)
if err != nil {
return err
}
for _, ancestor := range ancestors {
fmt.Fprintf(t.stdout, "Created by Goroutine %d:\n", ancestor.ID)
if ancestor.Unreadable != "" {
fmt.Fprintf(t.stdout, "\t%s\n", ancestor.Unreadable)
continue
}
printStack(t, t.stdout, ancestor.Stack, "\t", false)
}
}
return nil
}
type stackArgs struct {
depth int
full bool
offsets bool
opts api.StacktraceOptions
ancestors int
ancestorDepth int
}
func parseStackArgs(argstr string) (stackArgs, error) {
r := stackArgs{
depth: 50,
full: false,
}
if argstr != "" {
args := strings.Split(argstr, " ")
for i := 0; i < len(args); i++ {
numarg := func(name string) (int, error) {
if i >= len(args) {
return 0, fmt.Errorf("expected number after %s", name)
}
n, err := strconv.Atoi(args[i])
if err != nil {
return 0, fmt.Errorf("expected number after %s: %v", name, err)
}
return n, nil
}
switch args[i] {
case "-full":
r.full = true
case "-offsets":
r.offsets = true
case "-defer":
r.opts |= api.StacktraceReadDefers
case "-mode":
i++
if i >= len(args) {
return stackArgs{}, fmt.Errorf("expected normal, simple or fromg after -mode")
}
switch args[i] {
case "normal":
r.opts &^= api.StacktraceSimple
r.opts &^= api.StacktraceG
case "simple":
r.opts |= api.StacktraceSimple
case "fromg":
r.opts |= api.StacktraceG | api.StacktraceSimple
default:
return stackArgs{}, fmt.Errorf("expected normal, simple or fromg after -mode")
}
case "-a":
i++
n, err := numarg("-a")
if err != nil {
return stackArgs{}, err
}
r.ancestors = n
case "-adepth":
i++
n, err := numarg("-adepth")
if err != nil {
return stackArgs{}, err
}
r.ancestorDepth = n
default:
n, err := strconv.Atoi(args[i])
if err != nil {
return stackArgs{}, fmt.Errorf("depth must be a number")
}
r.depth = n
}
}
}
if r.ancestors > 0 && r.ancestorDepth == 0 {
r.ancestorDepth = r.depth
}
return r, nil
}
// getLocation returns the current location or the locations specified by the argument.
// getLocation is used to process the argument of list and edit commands.
func getLocation(t *Term, ctx callContext, args string, showContext bool) (file string, lineno int, showarrow bool, err error) {
switch {
case len(args) == 0 && !ctx.scoped():
state, err := t.client.GetState()
if err != nil {
return "", 0, false, err
}
if showContext {
printcontext(t, state)
}
if state.SelectedGoroutine != nil {
return state.SelectedGoroutine.CurrentLoc.File, state.SelectedGoroutine.CurrentLoc.Line, true, nil
}
return state.CurrentThread.File, state.CurrentThread.Line, true, nil
case len(args) == 0 && ctx.scoped():
locs, err := t.client.Stacktrace(ctx.Scope.GoroutineID, ctx.Scope.Frame, 0, nil)
if err != nil {
return "", 0, false, err
}
if ctx.Scope.Frame >= len(locs) {
return "", 0, false, fmt.Errorf("Frame %d does not exist in goroutine %d", ctx.Scope.Frame, ctx.Scope.GoroutineID)
}
loc := locs[ctx.Scope.Frame]
gid := ctx.Scope.GoroutineID
if gid < 0 {
state, err := t.client.GetState()
if err != nil {
return "", 0, false, err
}
if state.SelectedGoroutine != nil {
gid = state.SelectedGoroutine.ID
}
}
if showContext {
fmt.Fprintf(t.stdout, "Goroutine %d frame %d at %s:%d (PC: %#x)\n", gid, ctx.Scope.Frame, loc.File, loc.Line, loc.PC)
}
return loc.File, loc.Line, true, nil
default:
locs, err := t.client.FindLocation(ctx.Scope, args, false, t.substitutePathRules())
if err != nil {
return "", 0, false, err
}
if len(locs) > 1 {
return "", 0, false, locspec.AmbiguousLocationError{Location: args, CandidatesLocation: locs}
}
loc := locs[0]
if showContext {
fmt.Fprintf(t.stdout, "Showing %s:%d (PC: %#x)\n", loc.File, loc.Line, loc.PC)
}
return loc.File, loc.Line, false, nil
}
}
func listCommand(t *Term, ctx callContext, args string) error {
file, lineno, showarrow, err := getLocation(t, ctx, args, true)
if err != nil {
return err
}
return printfile(t, file, lineno, showarrow)
}
func (c *Commands) sourceCommand(t *Term, ctx callContext, args string) error {
if len(args) == 0 {
return fmt.Errorf("wrong number of arguments: source <filename>")
}
if filepath.Ext(args) == ".star" {
_, err := t.starlarkEnv.Execute(args, nil, "main", nil)
return err
}
if args == "-" {
return t.starlarkEnv.REPL()
}
return c.executeFile(t, args)
}
var errDisasmUsage = errors.New("wrong number of arguments: disassemble [-a <start> <end>] [-l <locspec>]")
func disassCommand(t *Term, ctx callContext, args string) error {
var cmd, rest string
if args != "" {
argv := config.Split2PartsBySpace(args)
if len(argv) != 2 {
return errDisasmUsage
}
cmd = argv[0]
rest = argv[1]
}
t.stdout.pw.PageMaybe(nil)
flavor := t.conf.GetDisassembleFlavour()
var disasm api.AsmInstructions
var disasmErr error
switch cmd {
case "":
locs, err := t.client.FindLocation(ctx.Scope, "+0", true, t.substitutePathRules())
if err != nil {
return err
}
disasm, disasmErr = t.client.DisassemblePC(ctx.Scope, locs[0].PC, flavor)
case "-a":
v := config.Split2PartsBySpace(rest)
if len(v) != 2 {
return errDisasmUsage
}
startpc, err := strconv.ParseInt(v[0], 0, 64)
if err != nil {
return fmt.Errorf("wrong argument: %q is not a number", v[0])
}
endpc, err := strconv.ParseInt(v[1], 0, 64)
if err != nil {
return fmt.Errorf("wrong argument: %q is not a number", v[1])
}
disasm, disasmErr = t.client.DisassembleRange(ctx.Scope, uint64(startpc), uint64(endpc), flavor)
case "-l":
locs, err := t.client.FindLocation(ctx.Scope, rest, true, t.substitutePathRules())
if err != nil {
return err
}
if len(locs) != 1 {
return errors.New("expression specifies multiple locations")
}
disasm, disasmErr = t.client.DisassemblePC(ctx.Scope, locs[0].PC, flavor)
default:
return errDisasmUsage
}
if disasmErr != nil {
return disasmErr
}
disasmPrint(disasm, t.stdout, true)
return nil
}
func libraries(t *Term, ctx callContext, args string) error {
libs, err := t.client.ListDynamicLibraries()
if err != nil {
return err
}
d := digits(len(libs))
for i := range libs {
fmt.Fprintf(t.stdout, "%"+strconv.Itoa(d)+"d. %#x %s\n", i, libs[i].Address, libs[i].Path)
}
return nil
}
func digits(n int) int {
if n <= 0 {
return 1
}
return int(math.Floor(math.Log10(float64(n)))) + 1
}
func printStack(t *Term, out io.Writer, stack []api.Stackframe, ind string, offsets bool) {
api.PrintStack(t.formatPath, out, stack, ind, offsets, func(api.Stackframe) bool { return true })
}
func printcontext(t *Term, state *api.DebuggerState) {
if t.IsTraceNonInteractive() {
// If we're just running the `trace` subcommand there isn't any need
// to print out the rest of the state below.
for i := range state.Threads {
if state.Threads[i].Breakpoint != nil {
printcontextThread(t, state.Threads[i])
}
}
return
}
for i := range state.Threads {
if (state.CurrentThread != nil) && (state.Threads[i].ID == state.CurrentThread.ID) {
continue
}
if state.Threads[i].Breakpoint != nil {
printcontextThread(t, state.Threads[i])
}
}
if state.CurrentThread == nil {
fmt.Fprintln(t.stdout, "No current thread available")
return
}
var th *api.Thread
if state.SelectedGoroutine == nil {
th = state.CurrentThread
} else {
for i := range state.Threads {
if state.Threads[i].ID == state.SelectedGoroutine.ThreadID {
th = state.Threads[i]
break
}
}
if th == nil {
printcontextLocation(t, state.SelectedGoroutine.CurrentLoc)
return
}
}
if th.File == "" {
fmt.Fprintf(t.stdout, "Stopped at: 0x%x\n", state.CurrentThread.PC)
t.stdout.ColorizePrint("", bytes.NewReader([]byte("no source available")), 1, 10, 1)
return
}
printcontextThread(t, th)
if state.When != "" {
fmt.Fprintln(t.stdout, state.When)
}
for _, watchpoint := range state.WatchOutOfScope {
fmt.Fprintf(t.stdout, "%s went out of scope and was cleared\n", formatBreakpointName(watchpoint, true))
}
}
func printcontextLocation(t *Term, loc api.Location) {
fmt.Fprintf(t.stdout, "> %s() %s:%d (PC: %#v)\n", loc.Function.Name(), t.formatPath(loc.File), loc.Line, loc.PC)
if loc.Function != nil && loc.Function.Optimized {
fmt.Fprintln(t.stdout, optimizedFunctionWarning)
}
}
func printReturnValues(t *Term, th *api.Thread) {
if th.ReturnValues == nil {
return
}
fmt.Fprintln(t.stdout, "Values returned:")
for _, v := range th.ReturnValues {
fmt.Fprintf(t.stdout, "\t%s: %s\n", v.Name, v.MultilineString("\t", ""))
}
fmt.Fprintln(t.stdout)
}
func printcontextThread(t *Term, th *api.Thread) {
fn := th.Function
if th.Breakpoint == nil {
printcontextLocation(t, api.Location{PC: th.PC, File: th.File, Line: th.Line, Function: th.Function})
printReturnValues(t, th)
return
}
args := ""
var hasReturnValue bool
if th.BreakpointInfo != nil && th.Breakpoint.LoadArgs != nil && *th.Breakpoint.LoadArgs == ShortLoadConfig {
var arg []string
for _, ar := range th.BreakpointInfo.Arguments {
// For AI compatibility return values are included in the
// argument list. This is a relic of the dark ages when the
// Go debug information did not distinguish between the two.
// Filter them out here instead, so during trace operations
// they are not printed as an argument.
if (ar.Flags & api.VariableArgument) != 0 {
arg = append(arg, ar.SinglelineString())
}
if (ar.Flags & api.VariableReturnArgument) != 0 {
hasReturnValue = true
}
}
args = strings.Join(arg, ", ")
}
bpname := ""
if th.Breakpoint.WatchExpr != "" {
bpname = fmt.Sprintf("watchpoint on [%s] ", th.Breakpoint.WatchExpr)
} else if th.Breakpoint.Name != "" {
bpname = fmt.Sprintf("[%s] ", th.Breakpoint.Name)
}
if th.Breakpoint.Tracepoint || th.Breakpoint.TraceReturn {
printTracepoint(t, th, bpname, fn, args, hasReturnValue)
return
}
if hitCount, ok := th.Breakpoint.HitCount[strconv.FormatInt(th.GoroutineID, 10)]; ok {
fmt.Fprintf(t.stdout, "> %s%s(%s) %s:%d (hits goroutine(%d):%d total:%d) (PC: %#v)\n",
bpname,
fn.Name(),
args,
t.formatPath(th.File),
th.Line,
th.GoroutineID,
hitCount,
th.Breakpoint.TotalHitCount,
th.PC)
} else {
fmt.Fprintf(t.stdout, "> %s%s(%s) %s:%d (hits total:%d) (PC: %#v)\n",
bpname,
fn.Name(),
args,
t.formatPath(th.File),
th.Line,
th.Breakpoint.TotalHitCount,
th.PC)
}
if th.Function != nil && th.Function.Optimized {
fmt.Fprintln(t.stdout, optimizedFunctionWarning)
}
printReturnValues(t, th)
printBreakpointInfo(t, th, false)
}
func printBreakpointInfo(t *Term, th *api.Thread, tracepointOnNewline bool) {
if th.BreakpointInfo == nil {
return
}
bp := th.Breakpoint
bpi := th.BreakpointInfo
if bp.TraceReturn {
return
}
didprintnl := tracepointOnNewline
tracepointnl := func() {
if !bp.Tracepoint || didprintnl {
return
}
didprintnl = true
fmt.Fprintln(t.stdout)
}
if bpi.Goroutine != nil {
tracepointnl()
writeGoroutineLong(t, t.stdout, bpi.Goroutine, "\t")
}
for _, v := range bpi.Variables {
tracepointnl()
fmt.Fprintf(t.stdout, "\t%s: %s\n", v.Name, v.MultilineString("\t", ""))
}
for _, v := range bpi.Locals {
tracepointnl()
if *bp.LoadLocals == longLoadConfig {
fmt.Fprintf(t.stdout, "\t%s: %s\n", v.Name, v.MultilineString("\t", ""))
} else {
fmt.Fprintf(t.stdout, "\t%s: %s\n", v.Name, v.SinglelineString())
}
}
if bp.LoadArgs != nil && *bp.LoadArgs == longLoadConfig {
for _, v := range bpi.Arguments {
tracepointnl()
fmt.Fprintf(t.stdout, "\t%s: %s\n", v.Name, v.MultilineString("\t", ""))
}
}
if bpi.Stacktrace != nil {
tracepointnl()
fmt.Fprintf(t.stdout, "\tStack:\n")
printStack(t, t.stdout, bpi.Stacktrace, "\t\t", false)
}
}
func printTracepoint(t *Term, th *api.Thread, bpname string, fn *api.Function, args string, hasReturnValue bool) {
if th.Breakpoint.Tracepoint {
fmt.Fprintf(t.stdout, "> goroutine(%d): %s%s(%s)\n", th.GoroutineID, bpname, fn.Name(), args)
printBreakpointInfo(t, th, !hasReturnValue)
}
if th.Breakpoint.TraceReturn {
retVals := make([]string, 0, len(th.ReturnValues))
for _, v := range th.ReturnValues {
retVals = append(retVals, v.SinglelineString())
}
fmt.Fprintf(t.stdout, ">> goroutine(%d): => (%s)\n", th.GoroutineID, strings.Join(retVals, ","))
}
if th.Breakpoint.TraceReturn || !hasReturnValue {
if th.BreakpointInfo != nil && th.BreakpointInfo.Stacktrace != nil {
fmt.Fprintf(t.stdout, "\tStack:\n")
printStack(t, t.stdout, th.BreakpointInfo.Stacktrace, "\t\t", false)
}
}
}
type printPosFlags uint8
const (
printPosShowArrow printPosFlags = 1 << iota
printPosStepInstruction
)
func printPos(t *Term, th *api.Thread, flags printPosFlags) error {
if flags&printPosStepInstruction != 0 {
if t.conf.Position == config.PositionSource {
return printfile(t, th.File, th.Line, flags&printPosShowArrow != 0)
}
return printdisass(t, th.PC)
}
if t.conf.Position == config.PositionDisassembly {
return printdisass(t, th.PC)
}
return printfile(t, th.File, th.Line, flags&printPosShowArrow != 0)
}
func printfile(t *Term, filename string, line int, showArrow bool) error {
if filename == "" {
return nil
}
lineCount := t.conf.GetSourceListLineCount()
arrowLine := 0
if showArrow {
arrowLine = line
}
var file *os.File
path := t.substitutePath(filename)
if _, err := os.Stat(path); os.IsNotExist(err) {
foundPath, err := debuginfod.GetSource(t.client.BuildID(), filename)
if err == nil {
path = foundPath
}
}
file, err := os.OpenFile(path, 0, os.ModePerm)
if err != nil {
return err
}
defer file.Close()
fi, _ := file.Stat()
lastModExe := t.client.LastModified()
if fi.ModTime().After(lastModExe) {
fmt.Fprintln(t.stdout, "Warning: listing may not match stale executable")
}
return t.stdout.ColorizePrint(file.Name(), file, line-lineCount, line+lineCount+1, arrowLine)
}
func printdisass(t *Term, pc uint64) error {
disasm, err := t.client.DisassemblePC(api.EvalScope{GoroutineID: -1, Frame: 0, DeferredCall: 0}, pc, t.conf.GetDisassembleFlavour())
if err != nil {
return err
}
lineCount := t.conf.GetSourceListLineCount()
showHeader := true
for i := range disasm {
if disasm[i].AtPC {
s := i - lineCount
if s < 0 {
s = 0
}
e := i + lineCount + 1
if e > len(disasm) {
e = len(disasm)
}
showHeader = s == 0
disasm = disasm[s:e]
break
}
}
disasmPrint(disasm, t.stdout, showHeader)
return nil
}
// ExitRequestError is returned when the user
// exits Delve.
type ExitRequestError struct{}
func (ere ExitRequestError) Error() string {
return ""
}
func exitCommand(t *Term, ctx callContext, args string) error {
if args == "-c" {
if !t.client.IsMulticlient() {
return errors.New("not connected to an --accept-multiclient server")
}
t.quitContinue = true
}
return ExitRequestError{}
}
func getBreakpointByIDOrName(t *Term, arg string) (*api.Breakpoint, error) {
if id, err := strconv.Atoi(arg); err == nil {
return t.client.GetBreakpoint(id)
}
return t.client.GetBreakpointByName(arg)
}
func (c *Commands) onCmd(t *Term, ctx callContext, argstr string) error {
args := config.Split2PartsBySpace(argstr)
if len(args) < 2 {
return errors.New("not enough arguments")
}
bp, err := getBreakpointByIDOrName(t, args[0])
if err != nil {
return err
}
ctx.Prefix = onPrefix
ctx.Breakpoint = bp
if args[1] == "-edit" {
f, err := ioutil.TempFile("", "dlv-on-cmd-")
if err != nil {
return err
}
defer func() {
_ = os.Remove(f.Name())
}()
attrs := formatBreakpointAttrs("", ctx.Breakpoint, true)
_, err = f.Write([]byte(strings.Join(attrs, "\n")))
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
err = runEditor(f.Name())
if err != nil {
return err
}
fin, err := os.Open(f.Name())
if err != nil {
return err
}
defer fin.Close()
err = c.parseBreakpointAttrs(t, ctx, fin)
if err != nil {
return err
}
} else {
err = c.CallWithContext(args[1], t, ctx)
if err != nil {
return err
}
}
return t.client.AmendBreakpoint(ctx.Breakpoint)
}
func (c *Commands) parseBreakpointAttrs(t *Term, ctx callContext, r io.Reader) error {
ctx.Breakpoint.Tracepoint = false
ctx.Breakpoint.Goroutine = false
ctx.Breakpoint.Stacktrace = 0
ctx.Breakpoint.Variables = ctx.Breakpoint.Variables[:0]
ctx.Breakpoint.Cond = ""
ctx.Breakpoint.HitCond = ""
scan := bufio.NewScanner(r)
lineno := 0
for scan.Scan() {
lineno++
err := c.CallWithContext(scan.Text(), t, ctx)
if err != nil {
fmt.Fprintf(t.stdout, "%d: %s\n", lineno, err.Error())
}
}
return scan.Err()
}
func conditionCmd(t *Term, ctx callContext, argstr string) error {
args := config.Split2PartsBySpace(argstr)
if len(args) < 2 {
return fmt.Errorf("not enough arguments")
}
hitCondPerG := args[0] == "-per-g-hitcount"
if args[0] == "-hitcount" || hitCondPerG {
// hitcount breakpoint
if ctx.Prefix == onPrefix {
ctx.Breakpoint.HitCond = args[1]
ctx.Breakpoint.HitCondPerG = hitCondPerG
return nil
}
args = config.Split2PartsBySpace(args[1])
if len(args) < 2 {
return fmt.Errorf("not enough arguments")
}
bp, err := getBreakpointByIDOrName(t, args[0])
if err != nil {
return err
}
bp.HitCond = args[1]
bp.HitCondPerG = hitCondPerG
return t.client.AmendBreakpoint(bp)
}
if args[0] == "-clear" {
bp, err := getBreakpointByIDOrName(t, args[1])
if err != nil {
return err
}
bp.Cond = ""
return t.client.AmendBreakpoint(bp)
}
if ctx.Prefix == onPrefix {
ctx.Breakpoint.Cond = argstr
return nil
}
bp, err := getBreakpointByIDOrName(t, args[0])
if err != nil {
return err
}
bp.Cond = args[1]
return t.client.AmendBreakpoint(bp)
}
func (c *Commands) executeFile(t *Term, name string) error {
fh, err := os.Open(name)
if err != nil {
return err
}
defer fh.Close()
scanner := bufio.NewScanner(fh)
lineno := 0
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
lineno++
if line == "" || line[0] == '#' {
continue
}
if err := c.Call(line, t); err != nil {
if _, isExitRequest := err.(ExitRequestError); isExitRequest {
return err
}
fmt.Fprintf(t.stdout, "%s:%d: %v\n", name, lineno, err)
}
}
return scanner.Err()
}
func (c *Commands) rewind(t *Term, ctx callContext, args string) error {
c.frame = 0
stateChan := t.client.Rewind()
var state *api.DebuggerState
for state = range stateChan {
if state.Err != nil {
return state.Err
}
printcontext(t, state)
}
printPos(t, state.CurrentThread, printPosShowArrow)
return nil
}
func checkpoint(t *Term, ctx callContext, args string) error {
if args == "" {
state, err := t.client.GetState()
if err != nil {
return err
}
var loc api.Location = api.Location{PC: state.CurrentThread.PC, File: state.CurrentThread.File, Line: state.CurrentThread.Line, Function: state.CurrentThread.Function}
if state.SelectedGoroutine != nil {
loc = state.SelectedGoroutine.CurrentLoc
}
args = fmt.Sprintf("%s() %s:%d (%#x)", loc.Function.Name(), loc.File, loc.Line, loc.PC)
}
cpid, err := t.client.Checkpoint(args)
if err != nil {
return err
}
fmt.Fprintf(t.stdout, "Checkpoint c%d created.\n", cpid)
return nil
}
func checkpoints(t *Term, ctx callContext, args string) error {
cps, err := t.client.ListCheckpoints()
if err != nil {
return err
}
w := new(tabwriter.Writer)
w.Init(t.stdout, 4, 4, 2, ' ', 0)
fmt.Fprintln(w, "ID\tWhen\tNote")
for _, cp := range cps {
fmt.Fprintf(w, "c%d\t%s\t%s\n", cp.ID, cp.When, cp.Where)
}
w.Flush()
return nil
}
func clearCheckpoint(t *Term, ctx callContext, args string) error {
if len(args) == 0 {
return errors.New("not enough arguments to clear-checkpoint")
}
if args[0] != 'c' {
return errors.New("clear-checkpoint argument must be a checkpoint ID")
}
id, err := strconv.Atoi(args[1:])
if err != nil {
return errors.New("clear-checkpoint argument must be a checkpoint ID")
}
return t.client.ClearCheckpoint(id)
}
func display(t *Term, ctx callContext, args string) error {
const (
addOption = "-a "
delOption = "-d "
)
switch {
case args == "":
t.printDisplays()
case strings.HasPrefix(args, addOption):
args = strings.TrimSpace(args[len(addOption):])
fmtstr, args := parseFormatArg(args)
if args == "" {
return fmt.Errorf("not enough arguments")
}
t.addDisplay(args, fmtstr)
t.printDisplay(len(t.displays) - 1)
case strings.HasPrefix(args, delOption):
args = strings.TrimSpace(args[len(delOption):])
n, err := strconv.Atoi(args)
if err != nil {
return fmt.Errorf("%q is not a number", args)
}
return t.removeDisplay(n)
default:
return fmt.Errorf("wrong arguments")
}
return nil
}
func dump(t *Term, ctx callContext, args string) error {
if args == "" {
return fmt.Errorf("not enough arguments")
}
dumpState, err := t.client.CoreDumpStart(args)
if err != nil {
return err
}
for {
if dumpState.ThreadsDone != dumpState.ThreadsTotal {
fmt.Fprintf(t.stdout, "\rDumping threads %d / %d...", dumpState.ThreadsDone, dumpState.ThreadsTotal)
} else {
fmt.Fprintf(t.stdout, "\rDumping memory %d / %d...", dumpState.MemDone, dumpState.MemTotal)
}
if !dumpState.Dumping {
break
}
dumpState = t.client.CoreDumpWait(1000)
}
fmt.Fprintf(t.stdout, "\n")
if dumpState.Err != "" {
fmt.Fprintf(t.stdout, "error dumping: %s\n", dumpState.Err)
} else if !dumpState.AllDone {
fmt.Fprintf(t.stdout, "canceled\n")
} else if dumpState.MemDone != dumpState.MemTotal {
fmt.Fprintf(t.stdout, "Core dump could be incomplete\n")
}
return nil
}
func transcript(t *Term, ctx callContext, args string) error {
argv := strings.SplitN(args, " ", -1)
truncate := false
fileOnly := false
disable := false
path := ""
for _, arg := range argv {
switch arg {
case "-x":
fileOnly = true
case "-t":
truncate = true
case "-off":
disable = true
default:
if path != "" || strings.HasPrefix(arg, "-") {
return fmt.Errorf("unrecognized option %q", arg)
} else {
path = arg
}
}
}
if disable {
if path != "" {
return errors.New("-o option specified with an output path")
}
return t.stdout.CloseTranscript()
}
if path == "" {
return errors.New("no output path specified")
}
flags := os.O_APPEND | os.O_WRONLY | os.O_CREATE
if truncate {
flags |= os.O_TRUNC
}
fh, err := os.OpenFile(path, flags, 0660)
if err != nil {
return err
}
if err := t.stdout.CloseTranscript(); err != nil {
return err
}
t.stdout.TranscribeTo(fh, fileOnly)
return nil
}
func formatBreakpointName(bp *api.Breakpoint, upcase bool) string {
thing := "breakpoint"
if bp.Tracepoint {
thing = "tracepoint"
}
if bp.WatchExpr != "" {
thing = "watchpoint"
}
if upcase {
thing = strings.Title(thing)
}
id := bp.Name
if id == "" {
id = strconv.Itoa(bp.ID)
}
if bp.WatchExpr != "" && bp.WatchExpr != bp.Name {
return fmt.Sprintf("%s %s on [%s]", thing, id, bp.WatchExpr)
}
return fmt.Sprintf("%s %s", thing, id)
}
func (t *Term) formatBreakpointLocation(bp *api.Breakpoint) string {
var out bytes.Buffer
if len(bp.Addrs) > 0 {
for i, addr := range bp.Addrs {
if i == 0 {
fmt.Fprintf(&out, "%#x", addr)
} else {
fmt.Fprintf(&out, ",%#x", addr)
}
}
} else {
// In case we are connecting to an older version of delve that does not return the Addrs field.
fmt.Fprintf(&out, "%#x", bp.Addr)
}
if bp.WatchExpr == "" {
fmt.Fprintf(&out, " for ")
p := t.formatPath(bp.File)
if bp.FunctionName != "" {
fmt.Fprintf(&out, "%s() ", bp.FunctionName)
}
fmt.Fprintf(&out, "%s:%d", p, bp.Line)
}
return out.String()
}
func shouldAskToSuspendBreakpoint(t *Term) bool {
fns, _ := t.client.ListFunctions(`^plugin\.Open$`)
_, err := t.client.GetState()
return len(fns) > 0 || isErrProcessExited(err)
}
|