File: debugger.go

package info (click to toggle)
delve 1.24.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,092 kB
  • sloc: ansic: 111,943; sh: 169; asm: 141; makefile: 43; python: 23
file content (2596 lines) | stat: -rw-r--r-- 74,488 bytes parent folder | download
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
package debugger

import (
	"debug/dwarf"
	"debug/elf"
	"debug/macho"
	"debug/pe"
	"errors"
	"fmt"
	"io"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"regexp"
	"runtime"
	"slices"
	"sort"
	"strings"
	"sync"
	"time"

	"github.com/go-delve/delve/pkg/dwarf/op"
	"github.com/go-delve/delve/pkg/gobuild"
	"github.com/go-delve/delve/pkg/goversion"
	"github.com/go-delve/delve/pkg/locspec"
	"github.com/go-delve/delve/pkg/logflags"
	"github.com/go-delve/delve/pkg/proc"
	"github.com/go-delve/delve/pkg/proc/core"
	"github.com/go-delve/delve/pkg/proc/gdbserial"
	"github.com/go-delve/delve/pkg/proc/native"
	"github.com/go-delve/delve/service/api"
)

var (
	// ErrCanNotRestart is returned when the target cannot be restarted.
	// This is returned for targets that have been attached to, or when
	// debugging core files.
	ErrCanNotRestart = errors.New("can not restart this target")

	// ErrNotRecording is returned when StopRecording is called while the
	// debugger is not recording the target.
	ErrNotRecording = errors.New("debugger is not recording")

	// ErrCoreDumpInProgress is returned when a core dump is already in progress.
	ErrCoreDumpInProgress = errors.New("core dump in progress")

	// ErrCoreDumpNotSupported is returned when core dumping is not supported
	ErrCoreDumpNotSupported = errors.New("core dumping not supported")

	// ErrNotImplementedWithMultitarget is returned for operations that are not implemented with multiple targets
	ErrNotImplementedWithMultitarget = errors.New("not implemented for multiple targets")
)

// Debugger service.
//
// Debugger provides a higher level of
// abstraction over proc.Process.
// It handles converting from internal types to
// the types expected by clients. It also handles
// functionality needed by clients, but not needed in
// lower lever packages such as proc.
type Debugger struct {
	config *Config
	// arguments to launch a new process.
	processArgs []string

	targetMutex sync.Mutex
	target      *proc.TargetGroup

	log logflags.Logger

	running      bool
	runningMutex sync.Mutex

	stopRecording func() error
	recordMutex   sync.Mutex

	dumpState proc.DumpState

	breakpointIDCounter int
}

type ExecuteKind int

const (
	ExecutingExistingFile = ExecuteKind(iota)
	ExecutingGeneratedFile
	ExecutingGeneratedTest
	ExecutingOther
)

// Config provides the configuration to start a Debugger.
//
// Only one of ProcessArgs or AttachPid should be specified. If ProcessArgs is
// provided, a new process will be launched. Otherwise, the debugger will try
// to attach to an existing process with AttachPid.
type Config struct {
	// WorkingDir is working directory of the new process. This field is used
	// only when launching a new process.
	WorkingDir string

	// AttachPid is the PID of an existing process to which the debugger should
	// attach.
	AttachPid int
	// If AttachWaitFor is set the debugger will wait for a process with a name
	// starting with WaitFor and attach to it.
	AttachWaitFor string
	// AttachWaitForInterval is the time (in milliseconds) that the debugger
	// waits between checks for WaitFor.
	AttachWaitForInterval float64
	// AttachWaitForDuration is the time (in milliseconds) that the debugger
	// waits for WaitFor.
	AttachWaitForDuration float64

	// CoreFile specifies the path to the core dump to open.
	CoreFile string

	// Backend specifies the debugger backend.
	Backend string

	// Foreground lets target process access stdin.
	Foreground bool

	// DebugInfoDirectories is the list of directories to look for
	// when resolving external debug info files.
	DebugInfoDirectories []string

	// CheckGoVersion is true if the debugger should check the version of Go
	// used to compile the executable and refuse to work on incompatible
	// versions.
	CheckGoVersion bool

	// TTY is passed along to the target process on creation. Used to specify a
	// TTY for that process.
	TTY string

	// Packages contains the packages that we are debugging.
	Packages []string

	// BuildFlags contains the flags passed to the compiler.
	BuildFlags string

	// ExecuteKind contains the kind of the executed program.
	ExecuteKind ExecuteKind

	// Stdin Redirect file path for stdin
	Stdin string

	// Redirects specifies redirect rules for stdout
	Stdout proc.OutputRedirect

	// Redirects specifies redirect rules for stderr
	Stderr proc.OutputRedirect

	// DisableASLR disables ASLR
	DisableASLR bool

	RrOnProcessPid int
}

// New creates a new Debugger. ProcessArgs specify the commandline arguments for the
// new process.
func New(config *Config, processArgs []string) (*Debugger, error) {
	logger := logflags.DebuggerLogger()
	d := &Debugger{
		config:      config,
		processArgs: processArgs,
		log:         logger,
	}

	// Create the process by either attaching or launching.
	switch {
	case d.config.AttachPid > 0 || d.config.AttachWaitFor != "":
		d.log.Infof("attaching to pid %d", d.config.AttachPid)
		path := ""
		if len(d.processArgs) > 0 {
			path = d.processArgs[0]
		}
		var waitFor *proc.WaitFor
		if d.config.AttachWaitFor != "" {
			waitFor = &proc.WaitFor{
				Name:     d.config.AttachWaitFor,
				Interval: time.Duration(d.config.AttachWaitForInterval * float64(time.Millisecond)),
				Duration: time.Duration(d.config.AttachWaitForDuration * float64(time.Millisecond)),
			}
		}
		var err error
		d.target, err = d.Attach(d.config.AttachPid, path, waitFor)
		if err != nil {
			err = go11DecodeErrorCheck(err)
			err = noDebugErrorWarning(err)
			return nil, attachErrorMessage(d.config.AttachPid, err)
		}

	case d.config.CoreFile != "":
		var err error
		switch d.config.Backend {
		case "rr":
			d.log.Infof("opening trace %s", d.config.CoreFile)
			d.target, err = gdbserial.Replay(d.config.CoreFile, false, false, d.config.DebugInfoDirectories, d.config.RrOnProcessPid, "")
		default:
			d.log.Infof("opening core file %s (executable %s)", d.config.CoreFile, d.processArgs[0])
			d.target, err = core.OpenCore(d.config.CoreFile, d.processArgs[0], d.config.DebugInfoDirectories)
		}
		if err != nil {
			err = go11DecodeErrorCheck(err)
			return nil, err
		}
		if err := d.checkGoVersion(); err != nil {
			d.target.Detach(true)
			return nil, err
		}

	default:
		d.log.Infof("launching process with args: %v", d.processArgs)
		var err error
		d.target, err = d.Launch(d.processArgs, d.config.WorkingDir)
		if err != nil {
			if !errors.Is(err, &proc.ErrUnsupportedArch{}) {
				err = go11DecodeErrorCheck(err)
				err = noDebugErrorWarning(err)
				err = fmt.Errorf("could not launch process: %s", err)
			}
			return nil, err
		}
		if err := d.checkGoVersion(); err != nil {
			d.target.Detach(true)
			return nil, err
		}
	}

	return d, nil
}

// canRestart returns true if the target was started with Launch and can be restarted
func (d *Debugger) canRestart() bool {
	switch {
	case d.config.AttachPid > 0:
		return false
	case d.config.CoreFile != "":
		return false
	default:
		return true
	}
}

func (d *Debugger) checkGoVersion() error {
	if d.isRecording() {
		// do not do anything if we are still recording
		return nil
	}
	producer := d.target.Selected.BinInfo().Producer()
	if producer == "" {
		return nil
	}
	return goversion.Compatible(producer, !d.config.CheckGoVersion)
}

func (d *Debugger) TargetGoVersion() string {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.Selected.BinInfo().Producer()
}

// Launch will start a process with the given args and working directory.
func (d *Debugger) Launch(processArgs []string, wd string) (*proc.TargetGroup, error) {
	fullpath, err := verifyBinaryFormat(processArgs[0])
	if err != nil {
		return nil, err
	}
	processArgs[0] = fullpath

	launchFlags := proc.LaunchFlags(0)
	if d.config.Foreground {
		launchFlags |= proc.LaunchForeground
	}
	if d.config.DisableASLR {
		launchFlags |= proc.LaunchDisableASLR
	}

	switch d.config.Backend {
	case "native":
		return native.Launch(processArgs, wd, launchFlags, d.config.DebugInfoDirectories, d.config.TTY, d.config.Stdin, d.config.Stdout, d.config.Stderr)
	case "lldb":
		return betterGdbserialLaunchError(gdbserial.LLDBLaunch(processArgs, wd, launchFlags, d.config.DebugInfoDirectories, d.config.TTY, [3]string{d.config.Stdin, d.config.Stdout.Path, d.config.Stderr.Path}))
	case "rr":
		if d.target != nil {
			// restart should not call us if the backend is 'rr'
			panic("internal error: call to Launch with rr backend and target already exists")
		}

		run, stop, err := gdbserial.RecordAsync(processArgs, wd, false, d.config.Stdin, d.config.Stdout, d.config.Stderr)
		if err != nil {
			return nil, err
		}

		// let the initialization proceed but hold the targetMutex lock so that
		// any other request to debugger will block except State(nowait=true) and
		// Command(halt).
		d.targetMutex.Lock()
		d.recordingStart(stop)

		go func() {
			defer d.targetMutex.Unlock()

			grp, err := d.recordingRun(run)
			if err != nil {
				d.log.Errorf("could not record target: %v", err)
				// this is ugly, but we can't respond to any client requests at this
				// point, so it's better if we die.
				os.Exit(1)
			}
			d.recordingDone()
			d.target = grp
			if err := d.checkGoVersion(); err != nil {
				d.log.Error(err)
				err := d.target.Detach(true)
				if err != nil {
					d.log.Errorf("Error detaching from target: %v", err)
				}
			}
		}()
		return nil, nil

	case "default":
		if runtime.GOOS == "darwin" {
			return betterGdbserialLaunchError(gdbserial.LLDBLaunch(processArgs, wd, launchFlags, d.config.DebugInfoDirectories, d.config.TTY, [3]string{d.config.Stdin, d.config.Stdout.Path, d.config.Stderr.Path}))
		}
		return native.Launch(processArgs, wd, launchFlags, d.config.DebugInfoDirectories, d.config.TTY, d.config.Stdin, d.config.Stdout, d.config.Stderr)
	default:
		return nil, fmt.Errorf("unknown backend %q", d.config.Backend)
	}
}

func (d *Debugger) recordingStart(stop func() error) {
	d.recordMutex.Lock()
	d.stopRecording = stop
	d.recordMutex.Unlock()
}

func (d *Debugger) recordingDone() {
	d.recordMutex.Lock()
	d.stopRecording = nil
	d.recordMutex.Unlock()
}

func (d *Debugger) isRecording() bool {
	d.recordMutex.Lock()
	defer d.recordMutex.Unlock()
	return d.stopRecording != nil
}

func (d *Debugger) recordingRun(run func() (string, error)) (*proc.TargetGroup, error) {
	tracedir, err := run()
	if err != nil && tracedir == "" {
		return nil, err
	}

	return gdbserial.Replay(tracedir, false, true, d.config.DebugInfoDirectories, 0, strings.Join(d.processArgs, " "))
}

// Attach will attach to the process specified by 'pid'.
func (d *Debugger) Attach(pid int, path string, waitFor *proc.WaitFor) (*proc.TargetGroup, error) {
	switch d.config.Backend {
	case "native":
		return native.Attach(pid, waitFor, d.config.DebugInfoDirectories)
	case "lldb":
		return betterGdbserialLaunchError(gdbserial.LLDBAttach(pid, path, waitFor, d.config.DebugInfoDirectories))
	case "default":
		if runtime.GOOS == "darwin" {
			return betterGdbserialLaunchError(gdbserial.LLDBAttach(pid, path, waitFor, d.config.DebugInfoDirectories))
		}
		return native.Attach(pid, waitFor, d.config.DebugInfoDirectories)
	default:
		return nil, fmt.Errorf("unknown backend %q", d.config.Backend)
	}
}

var errMacOSBackendUnavailable = errors.New("debugserver or lldb-server not found: install Xcode's command line tools or lldb-server")

func betterGdbserialLaunchError(p *proc.TargetGroup, err error) (*proc.TargetGroup, error) {
	if runtime.GOOS != "darwin" {
		return p, err
	}
	if !errors.Is(err, &gdbserial.ErrBackendUnavailable{}) {
		return p, err
	}

	return p, errMacOSBackendUnavailable
}

// ProcessPid returns the PID of the process
// the debugger is debugging.
func (d *Debugger) ProcessPid() int {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.Selected.Pid()
}

// LastModified returns the time that the process' executable was last
// modified.
func (d *Debugger) LastModified() time.Time {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.Selected.BinInfo().LastModified()
}

// FunctionReturnLocations returns all return locations
// for the given function, a list of addresses corresponding
// to 'ret' or 'call runtime.deferreturn'.
func (d *Debugger) FunctionReturnLocations(fnName string) ([]uint64, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	if len(d.target.Targets()) > 1 {
		return nil, ErrNotImplementedWithMultitarget
	}

	var (
		p = d.target.Selected
		g = p.SelectedGoroutine()
	)

	fns, err := p.BinInfo().FindFunction(fnName)
	if err != nil {
		return nil, err
	}

	var addrs []uint64

	for _, fn := range fns {
		var regs proc.Registers
		mem := p.Memory()
		if g != nil && g.Thread != nil {
			regs, _ = g.Thread.Registers()
		}
		instructions, err := proc.Disassemble(mem, regs, p.Breakpoints(), p.BinInfo(), fn.Entry, fn.End)
		if err != nil {
			return nil, err
		}

		for _, instruction := range instructions {
			if instruction.IsRet() {
				addrs = append(addrs, instruction.Loc.PC)
			}
		}
		addrs = append(addrs, proc.FindDeferReturnCalls(instructions)...)
	}

	return addrs, nil
}

// Detach detaches from the target process.
// If `kill` is true we will kill the process after
// detaching.
func (d *Debugger) Detach(kill bool) error {
	d.log.Debug("detaching")
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.detach(kill)
}

func (d *Debugger) detach(kill bool) error {
	if d.config.AttachPid == 0 {
		kill = true
	}
	return d.target.Detach(kill)
}

// Restart will restart the target process, first killing
// and then exec'ing it again.
// If the target process is a recording it will restart it from the given
// position. If pos starts with 'c' it's a checkpoint ID, otherwise it's an
// event number. If resetArgs is true, newArgs will replace the process args.
func (d *Debugger) Restart(rerecord bool, pos string, resetArgs bool, newArgs []string, newRedirects [3]string, rebuild bool) ([]api.DiscardedBreakpoint, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	recorded, _ := d.target.Recorded()
	if recorded && !rerecord {
		d.target.ResumeNotify(nil)
		return nil, d.target.Restart(pos)
	}

	if pos != "" {
		return nil, proc.ErrNotRecorded
	}

	if !d.canRestart() {
		return nil, ErrCanNotRestart
	}

	if !resetArgs && (d.config.Stdout.File != nil || d.config.Stderr.File != nil) {
		return nil, ErrCanNotRestart
	}

	if err := d.detach(true); err != nil {
		return nil, err
	}
	if resetArgs {
		d.processArgs = append([]string{d.processArgs[0]}, newArgs...)
		d.config.Stdin = newRedirects[0]
		d.config.Stdout = proc.OutputRedirect{Path: newRedirects[1]}
		d.config.Stderr = proc.OutputRedirect{Path: newRedirects[2]}
	}
	var grp *proc.TargetGroup
	var err error

	if rebuild {
		switch d.config.ExecuteKind {
		case ExecutingGeneratedFile:
			err = gobuild.GoBuild(d.processArgs[0], d.config.Packages, d.config.BuildFlags)
			if err != nil {
				return nil, fmt.Errorf("could not rebuild process: %s", err)
			}
		case ExecutingGeneratedTest:
			err = gobuild.GoTestBuild(d.processArgs[0], d.config.Packages, d.config.BuildFlags)
			if err != nil {
				return nil, fmt.Errorf("could not rebuild process: %s", err)
			}
		default:
			// We cannot build a process that we didn't start, because we don't know how it was built.
			return nil, errors.New("cannot rebuild a binary")
		}
	}

	if recorded {
		run, stop, err2 := gdbserial.RecordAsync(d.processArgs, d.config.WorkingDir, false, d.config.Stdin, d.config.Stdout, d.config.Stderr)
		if err2 != nil {
			return nil, err2
		}

		d.recordingStart(stop)
		grp, err = d.recordingRun(run)
		d.recordingDone()
	} else {
		grp, err = d.Launch(d.processArgs, d.config.WorkingDir)
	}
	if err != nil {
		return nil, fmt.Errorf("could not launch process: %s", err)
	}

	discarded := []api.DiscardedBreakpoint{}
	proc.Restart(grp, d.target, func(oldBp *proc.LogicalBreakpoint, err error) {
		discarded = append(discarded, api.DiscardedBreakpoint{Breakpoint: api.ConvertLogicalBreakpoint(oldBp), Reason: err.Error()})
	})
	d.target = grp
	return discarded, nil
}

// State returns the current state of the debugger.
func (d *Debugger) State(nowait bool) (*api.DebuggerState, error) {
	if d.IsRunning() && nowait {
		return &api.DebuggerState{Running: true}, nil
	}

	if d.isRecording() && nowait {
		return &api.DebuggerState{Recording: true}, nil
	}

	d.dumpState.Mutex.Lock()
	if d.dumpState.Dumping && nowait {
		return &api.DebuggerState{CoreDumping: true}, nil
	}
	d.dumpState.Mutex.Unlock()

	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.state(nil, false)
}

func (d *Debugger) state(retLoadCfg *proc.LoadConfig, withBreakpointInfo bool) (*api.DebuggerState, error) {
	if _, err := d.target.Valid(); err != nil {
		return nil, err
	}

	var (
		state     *api.DebuggerState
		goroutine *api.Goroutine
	)

	tgt := d.target.Selected

	if tgt.SelectedGoroutine() != nil {
		goroutine = api.ConvertGoroutine(tgt, tgt.SelectedGoroutine())
	}

	exited := false
	if _, err := tgt.Valid(); err != nil {
		var errProcessExited proc.ErrProcessExited
		exited = errors.As(err, &errProcessExited)
	}

	state = &api.DebuggerState{
		Pid:               tgt.Pid(),
		TargetCommandLine: tgt.CmdLine,
		SelectedGoroutine: goroutine,
		Exited:            exited,
	}

	for _, thread := range d.target.ThreadList() {
		th := api.ConvertThread(thread, d.ConvertThreadBreakpoint(thread))

		th.CallReturn = thread.Common().CallReturn
		if retLoadCfg != nil {
			th.ReturnValues = api.ConvertVars(thread.Common().ReturnValues(*retLoadCfg))
		}

		if withBreakpointInfo {
			err := d.collectBreakpointInformation(th, thread)
			if err != nil {
				return nil, err
			}
		}

		state.Threads = append(state.Threads, th)
		if thread.ThreadID() == tgt.CurrentThread().ThreadID() {
			state.CurrentThread = th
		}
	}

	state.NextInProgress = d.target.HasSteppingBreakpoints()

	if recorded, _ := d.target.Recorded(); recorded {
		state.When, _ = d.target.When()
	}

	t := proc.ValidTargets{Group: d.target}
	for t.Next() {
		for _, bp := range t.Breakpoints().WatchOutOfScope {
			abp := api.ConvertLogicalBreakpoint(bp.Logical)
			api.ConvertPhysicalBreakpoints(abp, bp.Logical, []int{t.Pid()}, []*proc.Breakpoint{bp})
			state.WatchOutOfScope = append(state.WatchOutOfScope, abp)
		}
	}

	return state, nil
}

// CreateBreakpoint creates a breakpoint using information from the provided `requestedBp`.
// This function accepts several different ways of specifying where and how to create the
// breakpoint that has been requested. Any error encountered during the attempt to set the
// breakpoint will be returned to the caller.
//
// The ways of specifying a breakpoint are listed below in the order they are considered by
// this function:
//
// - If requestedBp.TraceReturn is true then it is expected that
// requestedBp.Addrs will contain the list of return addresses
// supplied by the caller.
//
// - If requestedBp.File is not an empty string the breakpoint
// will be created on the specified file:line location
//
// - If requestedBp.FunctionName is not an empty string
// the breakpoint will be created on the specified function:line
// location.
//
// - If requestedBp.Addrs is filled it will create a logical breakpoint
// corresponding to all specified addresses.
//
// - Otherwise the value specified by arg.Breakpoint.Addr will be used.
//
// Note that this method will use the first successful method in order to
// create a breakpoint, so mixing different fields will not result is multiple
// breakpoints being set.
//
// If LocExpr is specified it will be used, along with substitutePathRules,
// to re-enable the breakpoint after it is disabled.
//
// If suspended is true a logical breakpoint will be created even if the
// location can not be found, the backend will attempt to enable the
// breakpoint every time a new plugin is loaded.
func (d *Debugger) CreateBreakpoint(requestedBp *api.Breakpoint, locExpr string, substitutePathRules [][2]string, suspended bool) (*api.Breakpoint, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	var (
		setbp proc.SetBreakpoint
		err   error
	)

	if requestedBp.Name != "" {
		if d.findBreakpointByName(requestedBp.Name) != nil {
			return nil, errors.New("breakpoint name already exists")
		}
	}

	if lbp := d.target.LogicalBreakpoints[requestedBp.ID]; lbp != nil {
		abp := d.convertBreakpoint(lbp)
		return abp, proc.BreakpointExistsError{File: lbp.File, Line: lbp.Line}
	}

	switch {
	case requestedBp.TraceReturn:
		if len(d.target.Targets()) != 1 {
			return nil, ErrNotImplementedWithMultitarget
		}
		setbp.PidAddrs = []proc.PidAddr{{Pid: d.target.Selected.Pid(), Addr: requestedBp.Addr}}
	case len(requestedBp.File) > 0:
		fileName := requestedBp.File
		if runtime.GOOS == "windows" {
			// Accept fileName which is case-insensitive and slash-insensitive match
			fileNameNormalized := strings.ToLower(filepath.ToSlash(fileName))
			t := proc.ValidTargets{Group: d.target}
		caseInsensitiveSearch:
			for t.Next() {
				for _, symFile := range t.BinInfo().Sources {
					if fileNameNormalized == strings.ToLower(filepath.ToSlash(symFile)) {
						fileName = symFile
						break caseInsensitiveSearch
					}
				}
			}
		}
		setbp.File = fileName
		setbp.Line = requestedBp.Line
	case len(requestedBp.FunctionName) > 0:
		setbp.FunctionName = requestedBp.FunctionName
		setbp.Line = requestedBp.Line
	case len(requestedBp.Addrs) > 0:
		setbp.PidAddrs = make([]proc.PidAddr, len(requestedBp.Addrs))
		if len(d.target.Targets()) == 1 {
			pid := d.target.Selected.Pid()
			for i, addr := range requestedBp.Addrs {
				setbp.PidAddrs[i] = proc.PidAddr{Pid: pid, Addr: addr}
			}
		} else {
			if len(requestedBp.Addrs) != len(requestedBp.AddrPid) {
				return nil, errors.New("mismatched length in addrs and addrpid")
			}
			for i, addr := range requestedBp.Addrs {
				setbp.PidAddrs[i] = proc.PidAddr{Pid: requestedBp.AddrPid[i], Addr: addr}
			}
		}
	default:
		if requestedBp.Addr != 0 {
			setbp.PidAddrs = []proc.PidAddr{{Pid: d.target.Selected.Pid(), Addr: requestedBp.Addr}}
		}
	}

	if locExpr != "" {
		loc, err := locspec.Parse(locExpr)
		if err != nil {
			return nil, err
		}
		setbp.Expr = func(t *proc.Target) []uint64 {
			locs, _, err := loc.Find(t, d.processArgs, nil, locExpr, false, substitutePathRules)
			if err != nil || len(locs) != 1 {
				logflags.DebuggerLogger().Debugf("could not evaluate breakpoint expression %q: %v (number of results %d)", locExpr, err, len(locs))
				return nil
			}
			return locs[0].PCs
		}
		setbp.ExprString = locExpr
	}

	id := requestedBp.ID

	if id <= 0 {
		d.breakpointIDCounter++
		id = d.breakpointIDCounter
	} else {
		d.breakpointIDCounter = id
	}

	lbp := &proc.LogicalBreakpoint{LogicalID: id, HitCount: make(map[int64]uint64)}
	d.target.LogicalBreakpoints[id] = lbp

	err = d.copyLogicalBreakpointInfo(lbp, requestedBp)
	if err != nil {
		return nil, err
	}

	lbp.Set = setbp

	if lbp.Set.Expr != nil {
		addrs := lbp.Set.Expr(d.Target())
		if len(addrs) > 0 {
			f, l, fn := d.Target().BinInfo().PCToLine(addrs[0])
			lbp.File = f
			lbp.Line = l
			if fn != nil {
				lbp.FunctionName = fn.Name
			}
		}
	}

	err = d.target.SetBreakpointEnabled(lbp, true)
	if err != nil {
		if suspended {
			logflags.DebuggerLogger().Debugf("could not enable new breakpoint: %v (breakpoint will be suspended)", err)
		} else {
			delete(d.target.LogicalBreakpoints, lbp.LogicalID)
			return nil, err
		}
	}

	createdBp := d.convertBreakpoint(lbp)
	d.log.Infof("created breakpoint: %#v", createdBp)
	return createdBp, nil
}

func (d *Debugger) convertBreakpoint(lbp *proc.LogicalBreakpoint) *api.Breakpoint {
	abp := api.ConvertLogicalBreakpoint(lbp)
	bps := []*proc.Breakpoint{}
	pids := []int{}
	t := proc.ValidTargets{Group: d.target}
	for t.Next() {
		for _, bp := range t.Breakpoints().M {
			if bp.LogicalID() == lbp.LogicalID {
				bps = append(bps, bp)
				pids = append(pids, t.Pid())
			}
		}
	}
	api.ConvertPhysicalBreakpoints(abp, lbp, pids, bps)
	return abp
}

func (d *Debugger) ConvertThreadBreakpoint(thread proc.Thread) *api.Breakpoint {
	if b := thread.Breakpoint(); b.Active && b.Breakpoint.Logical != nil {
		return d.convertBreakpoint(b.Breakpoint.Logical)
	}
	return nil
}

func (d *Debugger) CreateEBPFTracepoint(fnName string) error {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	if len(d.target.Targets()) != 1 {
		return ErrNotImplementedWithMultitarget
	}
	p := d.target.Selected
	return p.SetEBPFTracepoint(fnName)
}

// amendBreakpoint will update the breakpoint with the matching ID.
// It also enables or disables the breakpoint.
// We can consume this function to avoid locking a goroutine.
func (d *Debugger) amendBreakpoint(amend *api.Breakpoint) error {
	original := d.target.LogicalBreakpoints[amend.ID]
	if original == nil {
		return fmt.Errorf("no breakpoint with ID %d", amend.ID)
	}
	if d.isWatchpoint(original) && amend.Disabled {
		return errors.New("can not disable watchpoints")
	}
	err := d.copyLogicalBreakpointInfo(original, amend)
	if err != nil {
		return err
	}
	err = d.target.SetBreakpointEnabled(original, !amend.Disabled)
	if err != nil {
		return err
	}

	return nil
}

func (d *Debugger) isWatchpoint(lbp *proc.LogicalBreakpoint) bool {
	t := proc.ValidTargets{Group: d.target}
	for t.Next() {
		for _, bp := range t.Breakpoints().M {
			if bp.LogicalID() == lbp.LogicalID {
				return bp.WatchType != 0
			}
		}
	}
	return false
}

// AmendBreakpoint will update the breakpoint with the matching ID.
// It also enables or disables the breakpoint.
func (d *Debugger) AmendBreakpoint(amend *api.Breakpoint) error {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	return d.amendBreakpoint(amend)
}

// CancelNext will clear internal breakpoints, thus cancelling the 'next',
// 'step' or 'stepout' operation.
func (d *Debugger) CancelNext() error {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.ClearSteppingBreakpoints()
}

func (d *Debugger) copyLogicalBreakpointInfo(lbp *proc.LogicalBreakpoint, requested *api.Breakpoint) error {
	lbp.Name = requested.Name
	lbp.Tracepoint = requested.Tracepoint
	lbp.TraceReturn = requested.TraceReturn
	lbp.Goroutine = requested.Goroutine
	lbp.Stacktrace = requested.Stacktrace
	lbp.Variables = requested.Variables
	lbp.LoadArgs = api.LoadConfigToProc(requested.LoadArgs)
	lbp.LoadLocals = api.LoadConfigToProc(requested.LoadLocals)
	lbp.UserData = requested.UserData
	lbp.RootFuncName = requested.RootFuncName
	lbp.TraceFollowCalls = requested.TraceFollowCalls

	return d.target.ChangeBreakpointCondition(lbp, requested.Cond, requested.HitCond, requested.HitCondPerG)
}

// ClearBreakpoint clears a breakpoint.
func (d *Debugger) ClearBreakpoint(requestedBp *api.Breakpoint) (*api.Breakpoint, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	if requestedBp.ID <= 0 {
		if len(d.target.Targets()) != 1 {
			return nil, ErrNotImplementedWithMultitarget
		}
		bp := d.target.Selected.Breakpoints().M[requestedBp.Addr]
		requestedBp.ID = bp.LogicalID()
	}

	lbp := d.target.LogicalBreakpoints[requestedBp.ID]
	clearedBp := d.convertBreakpoint(lbp)

	err := d.target.SetBreakpointEnabled(lbp, false)
	if err != nil {
		return nil, err
	}

	delete(d.target.LogicalBreakpoints, requestedBp.ID)

	d.log.Infof("cleared breakpoint: %#v", clearedBp)
	return clearedBp, nil
}

// Breakpoints returns the list of current breakpoints.
func (d *Debugger) Breakpoints(all bool) []*api.Breakpoint {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	abps := []*api.Breakpoint{}
	if all {
		t := proc.ValidTargets{Group: d.target}
		for t.Next() {
			for _, bp := range t.Breakpoints().M {
				var abp *api.Breakpoint
				if bp.Logical != nil {
					abp = api.ConvertLogicalBreakpoint(bp.Logical)
				} else {
					abp = &api.Breakpoint{}
				}
				api.ConvertPhysicalBreakpoints(abp, bp.Logical, []int{t.Pid()}, []*proc.Breakpoint{bp})
				abp.VerboseDescr = bp.VerboseDescr()
				abps = append(abps, abp)
			}
		}
	} else {
		for _, lbp := range d.target.LogicalBreakpoints {
			abps = append(abps, d.convertBreakpoint(lbp))
		}
	}
	return abps
}

// FindBreakpoint returns the breakpoint specified by 'id'.
func (d *Debugger) FindBreakpoint(id int) *api.Breakpoint {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	lbp := d.target.LogicalBreakpoints[id]
	if lbp == nil {
		return nil
	}
	return d.convertBreakpoint(lbp)
}

// FindBreakpointByName returns the breakpoint specified by 'name'
func (d *Debugger) FindBreakpointByName(name string) *api.Breakpoint {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.findBreakpointByName(name)
}

func (d *Debugger) findBreakpointByName(name string) *api.Breakpoint {
	for _, lbp := range d.target.LogicalBreakpoints {
		if lbp.Name == name {
			return d.convertBreakpoint(lbp)
		}
	}
	return nil
}

// CreateWatchpoint creates a watchpoint on the specified expression.
func (d *Debugger) CreateWatchpoint(goid int64, frame, deferredCall int, expr string, wtype api.WatchType) (*api.Breakpoint, error) {
	p := d.target.Selected

	s, err := proc.ConvertEvalScope(p, goid, frame, deferredCall)
	if err != nil {
		return nil, err
	}
	d.breakpointIDCounter++
	bp, err := p.SetWatchpoint(d.breakpointIDCounter, s, expr, proc.WatchType(wtype), nil)
	if err != nil {
		return nil, err
	}
	if d.findBreakpointByName(expr) == nil {
		bp.Logical.Name = expr
	}
	return d.convertBreakpoint(bp.Logical), nil
}

// Threads returns the threads of the target process.
func (d *Debugger) Threads() ([]proc.Thread, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	if _, err := d.target.Valid(); err != nil {
		return nil, err
	}

	return d.target.ThreadList(), nil
}

// FindThread returns the thread for the given 'id'.
func (d *Debugger) FindThread(id int) (proc.Thread, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	if _, err := d.target.Valid(); err != nil {
		return nil, err
	}

	for _, th := range d.target.ThreadList() {
		if th.ThreadID() == id {
			return th, nil
		}
	}
	return nil, nil
}

// FindGoroutine returns the goroutine for the given 'id'.
func (d *Debugger) FindGoroutine(id int64) (*proc.G, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	return proc.FindGoroutine(d.target.Selected, id)
}

func (d *Debugger) setRunning(running bool) {
	d.runningMutex.Lock()
	d.running = running
	d.runningMutex.Unlock()
}

func (d *Debugger) IsRunning() bool {
	d.runningMutex.Lock()
	defer d.runningMutex.Unlock()
	return d.running
}

// Command handles commands which control the debugger lifecycle
func (d *Debugger) Command(command *api.DebuggerCommand, resumeNotify chan struct{}, clientStatusCh chan struct{}) (state *api.DebuggerState, err error) {
	if command.Name == api.Halt {
		// RequestManualStop does not invoke any ptrace syscalls, so it's safe to
		// access the process directly.
		d.log.Debug("halting")

		d.recordMutex.Lock()
		if d.stopRecording == nil {
			err = d.target.RequestManualStop()
			// The error returned from d.target.Valid will have more context
			// about the exited process.
			if _, valErr := d.target.Valid(); valErr != nil {
				err = valErr
			}
		}
		d.recordMutex.Unlock()
	}

	withBreakpointInfo := true

	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	d.setRunning(true)
	defer d.setRunning(false)

	if command.Name != api.SwitchGoroutine && command.Name != api.SwitchThread && command.Name != api.Halt {
		d.target.ResumeNotify(resumeNotify)
	} else if resumeNotify != nil {
		close(resumeNotify)
	}

	switch command.Name {
	case api.Continue:
		d.log.Debug("continuing")
		if err := d.target.ChangeDirection(proc.Forward); err != nil {
			return nil, err
		}
		err = d.target.Continue()
	case api.DirectionCongruentContinue:
		d.log.Debug("continuing (direction congruent)")
		err = d.target.Continue()
	case api.Call:
		d.log.Debugf("function call %s", command.Expr)
		if err := d.target.ChangeDirection(proc.Forward); err != nil {
			return nil, err
		}
		if command.ReturnInfoLoadConfig == nil {
			return nil, errors.New("can not call function with nil ReturnInfoLoadConfig")
		}
		g := d.target.Selected.SelectedGoroutine()
		if command.GoroutineID > 0 {
			g, err = proc.FindGoroutine(d.target.Selected, command.GoroutineID)
			if err != nil {
				return nil, err
			}
		}
		err = proc.EvalExpressionWithCalls(d.target, g, command.Expr, *api.LoadConfigToProc(command.ReturnInfoLoadConfig), !command.UnsafeCall)
	case api.Rewind:
		d.log.Debug("rewinding")
		if err := d.target.ChangeDirection(proc.Backward); err != nil {
			return nil, err
		}
		err = d.target.Continue()
	case api.Next:
		d.log.Debug("nexting")
		if err := d.target.ChangeDirection(proc.Forward); err != nil {
			return nil, err
		}
		err = d.target.Next()
	case api.ReverseNext:
		d.log.Debug("reverse nexting")
		if err := d.target.ChangeDirection(proc.Backward); err != nil {
			return nil, err
		}
		err = d.target.Next()
	case api.Step:
		d.log.Debug("stepping")
		if err := d.target.ChangeDirection(proc.Forward); err != nil {
			return nil, err
		}
		err = d.target.Step()
	case api.ReverseStep:
		d.log.Debug("reverse stepping")
		if err := d.target.ChangeDirection(proc.Backward); err != nil {
			return nil, err
		}
		err = d.target.Step()
	case api.StepInstruction:
		d.log.Debug("single stepping")
		if err := d.target.ChangeDirection(proc.Forward); err != nil {
			return nil, err
		}
		err = d.target.StepInstruction(false)
	case api.ReverseStepInstruction:
		d.log.Debug("reverse single stepping")
		if err := d.target.ChangeDirection(proc.Backward); err != nil {
			return nil, err
		}
		err = d.target.StepInstruction(false)
	case api.NextInstruction:
		d.log.Debug("single stepping")
		if err := d.target.ChangeDirection(proc.Forward); err != nil {
			return nil, err
		}
		err = d.target.StepInstruction(true)
	case api.ReverseNextInstruction:
		d.log.Debug("reverse single stepping")
		if err := d.target.ChangeDirection(proc.Backward); err != nil {
			return nil, err
		}
		err = d.target.StepInstruction(true)
	case api.StepOut:
		d.log.Debug("step out")
		if err := d.target.ChangeDirection(proc.Forward); err != nil {
			return nil, err
		}
		err = d.target.StepOut()
	case api.ReverseStepOut:
		d.log.Debug("reverse step out")
		if err := d.target.ChangeDirection(proc.Backward); err != nil {
			return nil, err
		}
		err = d.target.StepOut()
	case api.SwitchThread:
		d.log.Debugf("switching to thread %d", command.ThreadID)
		t := proc.ValidTargets{Group: d.target}
		for t.Next() {
			if _, ok := t.FindThread(command.ThreadID); ok {
				d.target.Selected = t.Target
				break
			}
		}
		err = d.target.Selected.SwitchThread(command.ThreadID)
		withBreakpointInfo = false
	case api.SwitchGoroutine:
		d.log.Debugf("switching to goroutine %d", command.GoroutineID)
		var g *proc.G
		g, err = proc.FindGoroutine(d.target.Selected, command.GoroutineID)
		if err == nil {
			err = d.target.Selected.SwitchGoroutine(g)
		}
		withBreakpointInfo = false
	case api.Halt:
		// RequestManualStop already called
		withBreakpointInfo = false
	}

	if err != nil {
		var errProcessExited proc.ErrProcessExited
		if errors.As(err, &errProcessExited) && command.Name != api.SwitchGoroutine && command.Name != api.SwitchThread {
			state := &api.DebuggerState{}
			state.Pid = d.target.Selected.Pid()
			state.Exited = true
			state.ExitStatus = errProcessExited.Status
			state.Err = errProcessExited
			d.maybePrintUnattendedStopWarning(proc.StopExited, state.CurrentThread, clientStatusCh)
			return state, nil
		}
		return nil, err
	}
	state, stateErr := d.state(api.LoadConfigToProc(command.ReturnInfoLoadConfig), withBreakpointInfo)
	if stateErr != nil {
		return state, stateErr
	}
	for _, th := range state.Threads {
		if th.Breakpoint != nil && th.Breakpoint.TraceReturn {
			for _, v := range th.BreakpointInfo.Arguments {
				if (v.Flags & api.VariableReturnArgument) != 0 {
					th.ReturnValues = append(th.ReturnValues, v)
				}
			}
		}
	}

	d.maybePrintUnattendedStopWarning(d.target.Selected.StopReason, state.CurrentThread, clientStatusCh)
	return state, err
}

func (d *Debugger) collectBreakpointInformation(apiThread *api.Thread, thread proc.Thread) error {
	if apiThread.Breakpoint == nil || apiThread.BreakpointInfo != nil {
		return nil
	}

	bp := apiThread.Breakpoint
	bpi := &api.BreakpointInfo{}
	apiThread.BreakpointInfo = bpi

	tgt := d.target.TargetForThread(thread.ThreadID())

	// If we're dealing with a stripped binary don't attempt to load more
	// information, we won't be able to.
	img := tgt.BinInfo().PCToImage(bp.Addr)
	if img != nil && img.Stripped() {
		return nil
	}

	if bp.Goroutine {
		g, err := proc.GetG(thread)
		if err != nil {
			return err
		}
		bpi.Goroutine = api.ConvertGoroutine(tgt, g)
	}

	if bp.Stacktrace > 0 {
		rawlocs, err := proc.ThreadStacktrace(tgt, thread, bp.Stacktrace)
		if err != nil {
			return err
		}
		bpi.Stacktrace, err = d.convertStacktrace(rawlocs, nil)
		if err != nil {
			return err
		}
	}

	if len(bp.Variables) == 0 && bp.LoadArgs == nil && bp.LoadLocals == nil {
		// don't try to create goroutine scope if there is nothing to load
		return nil
	}

	s, err := proc.GoroutineScope(tgt, thread)
	if err != nil {
		var errNoGoroutine proc.ErrNoGoroutine
		if errors.As(err, &errNoGoroutine) {
			s, err = proc.ThreadScope(tgt, thread)
		}
		if err != nil {
			return err
		}
	}

	if len(bp.Variables) > 0 {
		bpi.Variables = make([]api.Variable, len(bp.Variables))
	}
	for i := range bp.Variables {
		v, err := s.EvalExpression(bp.Variables[i], proc.LoadConfig{FollowPointers: true, MaxVariableRecurse: 1, MaxStringLen: 64, MaxArrayValues: 64, MaxStructFields: -1})
		if err != nil {
			bpi.Variables[i] = api.Variable{Name: bp.Variables[i], Unreadable: fmt.Sprintf("eval error: %v", err)}
		} else {
			bpi.Variables[i] = *api.ConvertVar(v)
		}
	}
	if bp.LoadArgs != nil {
		if vars, err := s.FunctionArguments(*api.LoadConfigToProc(bp.LoadArgs)); err == nil {
			bpi.Arguments = api.ConvertVars(vars)
		}
	}
	if bp.LoadLocals != nil {
		if locals, err := s.LocalVariables(*api.LoadConfigToProc(bp.LoadLocals)); err == nil {
			bpi.Locals = api.ConvertVars(locals)
		}
	}
	return nil
}

// Sources returns a list of the source files for target binary.
func (d *Debugger) Sources(filter string) ([]string, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	regex, err := regexp.Compile(filter)
	if err != nil {
		return nil, fmt.Errorf("invalid filter argument: %s", err.Error())
	}

	files := []string{}
	t := proc.ValidTargets{Group: d.target}
	for t.Next() {
		for _, f := range t.BinInfo().Sources {
			if regex.MatchString(f) {
				files = append(files, f)
			}
		}
	}
	sort.Strings(files)
	files = slices.Compact(files)
	return files, nil
}

// Functions returns a list of functions in the target process.
func (d *Debugger) Functions(filter string, followCalls int) ([]string, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	regex, err := regexp.Compile(filter)
	if err != nil {
		return nil, fmt.Errorf("invalid filter argument: %s", err.Error())
	}

	funcs := []string{}
	t := proc.ValidTargets{Group: d.target}
	for t.Next() {
		for _, f := range t.BinInfo().Functions {
			if regex.MatchString(f.Name) {
				if followCalls > 0 {
					newfuncs, err := traverse(t, &f, 1, followCalls)
					if err != nil {
						return nil, fmt.Errorf("traverse failed with error %w", err)
					}
					funcs = append(funcs, newfuncs...)
				} else {
					funcs = append(funcs, f.Name)
				}
			}
		}
	}
	sort.Strings(funcs)
	funcs = slices.Compact(funcs)
	return funcs, nil
}

func traverse(t proc.ValidTargets, f *proc.Function, depth int, followCalls int) ([]string, error) {
	type TraceFunc struct {
		Func    *proc.Function
		Depth   int
		visited bool
	}
	type TraceFuncptr *TraceFunc

	TraceMap := make(map[string]TraceFuncptr)
	queue := make([]TraceFuncptr, 0, 40)
	funcs := []string{}
	rootnode := &TraceFunc{Func: new(proc.Function), Depth: depth, visited: false}
	rootnode.Func = f

	// cache function details in a map for reuse
	TraceMap[f.Name] = rootnode
	queue = append(queue, rootnode)
	for len(queue) > 0 {
		parent := queue[0]
		queue = queue[1:]
		if parent == nil {
			panic("attempting to open file Delve cannot parse")
		}
		if parent.Depth > followCalls {
			continue
		}
		if !parent.visited {
			funcs = append(funcs, parent.Func.Name)
			parent.visited = true
		} else if parent.visited {
			continue
		}

		if parent.Depth+1 > followCalls {
			// Avoid diassembling if we already cross the follow-calls depth
			continue
		}
		f := parent.Func
		text, err := proc.Disassemble(t.Memory(), nil, t.Breakpoints(), t.BinInfo(), f.Entry, f.End)
		if err != nil {
			return nil, fmt.Errorf("disassemble failed with error %w", err)
		}
		for _, instr := range text {
			if instr.IsCall() && instr.DestLoc != nil && instr.DestLoc.Fn != nil {
				cf := instr.DestLoc.Fn
				if (strings.HasPrefix(cf.Name, "runtime.") || strings.HasPrefix(cf.Name, "runtime/internal")) && cf.Name != "runtime.deferreturn" && cf.Name != "runtime.gorecover" && cf.Name != "runtime.gopanic" {
					continue
				}
				childnode := TraceMap[cf.Name]
				if childnode == nil {
					childnode = &TraceFunc{Func: nil, Depth: parent.Depth + 1, visited: false}
					childnode.Func = cf
					TraceMap[cf.Name] = childnode
					queue = append(queue, childnode)
				}
			}
		}
	}
	return funcs, nil
}

// Types returns all type information in the binary.
func (d *Debugger) Types(filter string) ([]string, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	regex, err := regexp.Compile(filter)
	if err != nil {
		return nil, fmt.Errorf("invalid filter argument: %s", err.Error())
	}

	r := []string{}

	t := proc.ValidTargets{Group: d.target}
	for t.Next() {
		types, err := t.BinInfo().Types()
		if err != nil {
			return nil, err
		}

		for _, typ := range types {
			if regex.MatchString(typ) {
				r = append(r, typ)
			}
		}
	}
	sort.Strings(r)
	r = slices.Compact(r)

	return r, nil
}

// PackageVariables returns a list of package variables for the thread,
// optionally regexp filtered using regexp described in 'filter'.
func (d *Debugger) PackageVariables(filter string, cfg proc.LoadConfig) ([]*proc.Variable, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	p := d.target.Selected

	regex, err := regexp.Compile(filter)
	if err != nil {
		return nil, fmt.Errorf("invalid filter argument: %s", err.Error())
	}

	scope, err := proc.ThreadScope(p, p.CurrentThread())
	if err != nil {
		return nil, err
	}
	pv, err := scope.PackageVariables(cfg)
	if err != nil {
		return nil, err
	}
	pvr := pv[:0]
	for i := range pv {
		if regex.MatchString(pv[i].Name) {
			pvr = append(pvr, pv[i])
		}
	}
	return pvr, nil
}

// ThreadRegisters returns registers of the specified thread.
func (d *Debugger) ThreadRegisters(threadID int) (*op.DwarfRegisters, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	thread, found := d.target.Selected.FindThread(threadID)
	if !found {
		return nil, fmt.Errorf("couldn't find thread %d", threadID)
	}
	regs, err := thread.Registers()
	if err != nil {
		return nil, err
	}
	return d.target.Selected.BinInfo().Arch.RegistersToDwarfRegisters(0, regs), nil
}

// ScopeRegisters returns registers for the specified scope.
func (d *Debugger) ScopeRegisters(goid int64, frame, deferredCall int) (*op.DwarfRegisters, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	s, err := proc.ConvertEvalScope(d.target.Selected, goid, frame, deferredCall)
	if err != nil {
		return nil, err
	}
	return &s.Regs, nil
}

// DwarfRegisterToString returns the name and value representation of the given register.
func (d *Debugger) DwarfRegisterToString(i int, reg *op.DwarfRegister) (string, bool, string) {
	return d.target.Selected.BinInfo().Arch.DwarfRegisterToString(i, reg)
}

// LocalVariables returns a list of the local variables.
func (d *Debugger) LocalVariables(goid int64, frame, deferredCall int, cfg proc.LoadConfig) ([]*proc.Variable, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	s, err := proc.ConvertEvalScope(d.target.Selected, goid, frame, deferredCall)
	if err != nil {
		return nil, err
	}
	return s.LocalVariables(cfg)
}

// FunctionArguments returns the arguments to the current function.
func (d *Debugger) FunctionArguments(goid int64, frame, deferredCall int, cfg proc.LoadConfig) ([]*proc.Variable, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	s, err := proc.ConvertEvalScope(d.target.Selected, goid, frame, deferredCall)
	if err != nil {
		return nil, err
	}
	return s.FunctionArguments(cfg)
}

// Function returns the current function.
func (d *Debugger) Function(goid int64, frame, deferredCall int) (*proc.Function, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	s, err := proc.ConvertEvalScope(d.target.Selected, goid, frame, deferredCall)
	if err != nil {
		return nil, err
	}
	return s.Fn, nil
}

// EvalVariableInScope will attempt to evaluate the 'expr' in the scope
// corresponding to the given 'frame' on the goroutine identified by 'goid'.
func (d *Debugger) EvalVariableInScope(goid int64, frame, deferredCall int, expr string, cfg proc.LoadConfig) (*proc.Variable, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	s, err := proc.ConvertEvalScope(d.target.Selected, goid, frame, deferredCall)
	if err != nil {
		return nil, err
	}
	return s.EvalExpression(expr, cfg)
}

// LoadResliced will attempt to 'reslice' a map, array or slice so that the values
// up to cfg.MaxArrayValues children are loaded starting from index start.
func (d *Debugger) LoadResliced(v *proc.Variable, start int, cfg proc.LoadConfig) (*proc.Variable, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return v.LoadResliced(start, cfg)
}

// SetVariableInScope will set the value of the variable represented by
// 'symbol' to the value given, in the given scope.
func (d *Debugger) SetVariableInScope(goid int64, frame, deferredCall int, symbol, value string) error {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	s, err := proc.ConvertEvalScope(d.target.Selected, goid, frame, deferredCall)
	if err != nil {
		return err
	}
	return s.SetVariable(symbol, value)
}

// Goroutines will return a list of goroutines in the target process.
func (d *Debugger) Goroutines(start, count int) ([]*proc.G, int, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return proc.GoroutinesInfo(d.target.Selected, start, count)
}

// FilterGoroutines returns the goroutines in gs that satisfy the specified filters.
func (d *Debugger) FilterGoroutines(gs []*proc.G, filters []api.ListGoroutinesFilter) []*proc.G {
	if len(filters) == 0 {
		return gs
	}
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	r := []*proc.G{}
	for _, g := range gs {
		ok := true
		for i := range filters {
			if !matchGoroutineFilter(d.target.Selected, g, &filters[i]) {
				ok = false
				break
			}
		}
		if ok {
			r = append(r, g)
		}
	}
	return r
}

func matchGoroutineFilter(tgt *proc.Target, g *proc.G, filter *api.ListGoroutinesFilter) bool {
	var val bool
	switch filter.Kind {
	default:
		fallthrough
	case api.GoroutineFieldNone:
		val = true
	case api.GoroutineCurrentLoc:
		val = matchGoroutineLocFilter(g.CurrentLoc, filter.Arg)
	case api.GoroutineUserLoc:
		val = matchGoroutineLocFilter(g.UserCurrent(), filter.Arg)
	case api.GoroutineGoLoc:
		val = matchGoroutineLocFilter(g.Go(), filter.Arg)
	case api.GoroutineStartLoc:
		val = matchGoroutineLocFilter(g.StartLoc(tgt), filter.Arg)
	case api.GoroutineLabel:
		idx := strings.Index(filter.Arg, "=")
		if idx >= 0 {
			val = g.Labels()[filter.Arg[:idx]] == filter.Arg[idx+1:]
		} else {
			_, val = g.Labels()[filter.Arg]
		}
	case api.GoroutineRunning:
		val = g.Thread != nil
	case api.GoroutineUser:
		val = !g.System(tgt)
	case api.GoroutineWaitingOnChannel:
		val = true // handled elsewhere
	}
	if filter.Negated {
		val = !val
	}
	return val
}

func matchGoroutineLocFilter(loc proc.Location, arg string) bool {
	return strings.Contains(formatLoc(loc), arg)
}

func formatLoc(loc proc.Location) string {
	fnname := "?"
	if loc.Fn != nil {
		fnname = loc.Fn.Name
	}
	return fmt.Sprintf("%s:%d in %s", loc.File, loc.Line, fnname)
}

// GroupGoroutines divides goroutines in gs into groups as specified by
// group.{GroupBy,GroupByKey}. A maximum of group.MaxGroupMembers are saved in
// each group, but the total number of goroutines in each group is recorded. If
// group.MaxGroups is set, then at most that many groups are returned. If some
// groups end up being dropped because of this limit, the tooManyGroups return
// value is set.
//
// The first return value represents the goroutines that have been included in
// one of the returned groups (subject to the MaxGroupMembers and MaxGroups
// limits). The second return value represents the groups.
func (d *Debugger) GroupGoroutines(gs []*proc.G, group *api.GoroutineGroupingOptions) ([]*proc.G, []api.GoroutineGroup, bool) {
	if group.GroupBy == api.GoroutineFieldNone {
		return gs, nil, false
	}
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	groupMembers := map[string][]*proc.G{}
	totals := map[string]int{}

	for _, g := range gs {
		var key string
		switch group.GroupBy {
		case api.GoroutineCurrentLoc:
			key = formatLoc(g.CurrentLoc)
		case api.GoroutineUserLoc:
			key = formatLoc(g.UserCurrent())
		case api.GoroutineGoLoc:
			key = formatLoc(g.Go())
		case api.GoroutineStartLoc:
			key = formatLoc(g.StartLoc(d.target.Selected))
		case api.GoroutineLabel:
			key = fmt.Sprintf("%s=%s", group.GroupByKey, g.Labels()[group.GroupByKey])
		case api.GoroutineRunning:
			key = fmt.Sprintf("running=%v", g.Thread != nil)
		case api.GoroutineUser:
			key = fmt.Sprintf("user=%v", !g.System(d.target.Selected))
		}
		if len(groupMembers[key]) < group.MaxGroupMembers {
			groupMembers[key] = append(groupMembers[key], g)
		}
		totals[key]++
	}

	keys := make([]string, 0, len(groupMembers))
	for key := range groupMembers {
		keys = append(keys, key)
	}
	sort.Strings(keys)

	tooManyGroups := false
	gsout := []*proc.G{}
	groups := []api.GoroutineGroup{}
	for _, key := range keys {
		if group.MaxGroups > 0 && len(groups) >= group.MaxGroups {
			tooManyGroups = true
			break
		}
		groups = append(groups, api.GoroutineGroup{Name: key, Offset: len(gsout), Count: len(groupMembers[key]), Total: totals[key]})
		gsout = append(gsout, groupMembers[key]...)
	}
	return gsout, groups, tooManyGroups
}

// Stacktrace returns a list of Stackframes for the given goroutine. The
// length of the returned list will be min(stack_len, depth).
// If 'full' is true, then local vars, function args, etc. will be returned as well.
func (d *Debugger) Stacktrace(goroutineID int64, depth int, opts api.StacktraceOptions) ([]proc.Stackframe, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.stacktrace(goroutineID, depth, opts)
}

func (d *Debugger) stacktrace(goroutineID int64, depth int, opts api.StacktraceOptions) ([]proc.Stackframe, error) {
	if _, err := d.target.Valid(); err != nil {
		return nil, err
	}

	g, err := proc.FindGoroutine(d.target.Selected, goroutineID)
	if err != nil {
		return nil, err
	}

	if g == nil {
		return proc.ThreadStacktrace(d.target.Selected, d.target.Selected.CurrentThread(), depth)
	} else {
		return proc.GoroutineStacktrace(d.target.Selected, g, depth, proc.StacktraceOptions(opts))
	}
}

// Ancestors returns the stacktraces for the ancestors of a goroutine.
func (d *Debugger) Ancestors(goroutineID int64, numAncestors, depth int) ([]api.Ancestor, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	if _, err := d.target.Valid(); err != nil {
		return nil, err
	}

	g, err := proc.FindGoroutine(d.target.Selected, goroutineID)
	if err != nil {
		return nil, err
	}
	if g == nil {
		return nil, errors.New("no selected goroutine")
	}

	ancestors, err := proc.Ancestors(d.target.Selected, g, numAncestors)
	if err != nil {
		return nil, err
	}

	r := make([]api.Ancestor, len(ancestors))
	for i := range ancestors {
		r[i].ID = ancestors[i].ID
		if ancestors[i].Unreadable != nil {
			r[i].Unreadable = ancestors[i].Unreadable.Error()
			continue
		}
		frames, err := ancestors[i].Stack(depth)
		if err != nil {
			r[i].Unreadable = fmt.Sprintf("could not read ancestor stacktrace: %v", err)
			continue
		}
		r[i].Stack, err = d.convertStacktrace(frames, nil)
		if err != nil {
			r[i].Unreadable = fmt.Sprintf("could not read ancestor stacktrace: %v", err)
		}
	}
	return r, nil
}

// ConvertStacktrace converts a slice of proc.Stackframe into a slice of
// api.Stackframe, loading local variables and arguments of each frame if
// cfg is not nil.
func (d *Debugger) ConvertStacktrace(rawlocs []proc.Stackframe, cfg *proc.LoadConfig) ([]api.Stackframe, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.convertStacktrace(rawlocs, cfg)
}

func (d *Debugger) convertStacktrace(rawlocs []proc.Stackframe, cfg *proc.LoadConfig) ([]api.Stackframe, error) {
	locations := make([]api.Stackframe, 0, len(rawlocs))
	for i := range rawlocs {
		frame := api.Stackframe{
			Location: api.ConvertLocation(rawlocs[i].Call),

			FrameOffset:        rawlocs[i].FrameOffset(),
			FramePointerOffset: rawlocs[i].FramePointerOffset(),

			Defers: d.convertDefers(rawlocs[i].Defers),

			Bottom: rawlocs[i].Bottom,
		}
		if rawlocs[i].Err != nil {
			frame.Err = rawlocs[i].Err.Error()
		}
		if cfg != nil && rawlocs[i].Current.Fn != nil {
			scope := proc.FrameToScope(d.target.Selected, d.target.Selected.Memory(), nil, 0, rawlocs[i:]...)
			locals, err := scope.LocalVariables(*cfg)
			if err != nil {
				return nil, err
			}
			arguments, err := scope.FunctionArguments(*cfg)
			if err != nil {
				return nil, err
			}

			frame.Locals = api.ConvertVars(locals)
			frame.Arguments = api.ConvertVars(arguments)
		}
		locations = append(locations, frame)
	}

	return locations, nil
}

func (d *Debugger) convertDefers(defers []*proc.Defer) []api.Defer {
	r := make([]api.Defer, len(defers))
	for i := range defers {
		ddf, ddl, ddfn := defers[i].DeferredFunc(d.target.Selected)
		drf, drl, drfn := d.target.Selected.BinInfo().PCToLine(defers[i].DeferPC)

		if defers[i].Unreadable != nil {
			r[i].Unreadable = defers[i].Unreadable.Error()
		} else {
			var entry = defers[i].DeferPC
			if ddfn != nil {
				entry = ddfn.Entry
			}
			r[i] = api.Defer{
				DeferredLoc: api.ConvertLocation(proc.Location{
					PC:   entry,
					File: ddf,
					Line: ddl,
					Fn:   ddfn,
				}),
				DeferLoc: api.ConvertLocation(proc.Location{
					PC:   defers[i].DeferPC,
					File: drf,
					Line: drl,
					Fn:   drfn,
				}),
				SP: defers[i].SP,
			}
		}
	}

	return r
}

// CurrentPackage returns the fully qualified name of the
// package corresponding to the function location of the
// current thread.
func (d *Debugger) CurrentPackage() (string, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	if _, err := d.target.Valid(); err != nil {
		return "", err
	}
	loc, err := d.target.Selected.CurrentThread().Location()
	if err != nil {
		return "", err
	}
	if loc.Fn == nil {
		return "", errors.New("unable to determine current package due to unspecified function location")
	}
	return loc.Fn.PackageName(), nil
}

// FindLocation will find the location specified by 'locStr'.
func (d *Debugger) FindLocation(goid int64, frame, deferredCall int, locStr string, includeNonExecutableLines bool, substitutePathRules [][2]string) ([]api.Location, string, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	if _, err := d.target.Valid(); err != nil {
		return nil, "", err
	}

	loc, err := locspec.Parse(locStr)
	if err != nil {
		return nil, "", err
	}

	return d.findLocation(goid, frame, deferredCall, locStr, loc, includeNonExecutableLines, substitutePathRules)
}

// FindLocationSpec will find the location specified by 'locStr' and 'locSpec'.
// 'locSpec' should be the result of calling 'locspec.Parse(locStr)'. 'locStr'
// is also passed, because it made be used to broaden the search criteria, if
// the parsed result did not find anything.
func (d *Debugger) FindLocationSpec(goid int64, frame, deferredCall int, locStr string, locSpec locspec.LocationSpec, includeNonExecutableLines bool, substitutePathRules [][2]string) ([]api.Location, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	if _, err := d.target.Valid(); err != nil {
		return nil, err
	}

	locs, _, err := d.findLocation(goid, frame, deferredCall, locStr, locSpec, includeNonExecutableLines, substitutePathRules)
	return locs, err
}

func (d *Debugger) findLocation(goid int64, frame, deferredCall int, locStr string, locSpec locspec.LocationSpec, includeNonExecutableLines bool, substitutePathRules [][2]string) ([]api.Location, string, error) {
	locations := []api.Location{}
	t := proc.ValidTargets{Group: d.target}
	subst := ""
	for t.Next() {
		pid := t.Pid()
		s, _ := proc.ConvertEvalScope(t.Target, goid, frame, deferredCall)
		locs, s1, err := locSpec.Find(t.Target, d.processArgs, s, locStr, includeNonExecutableLines, substitutePathRules)
		if s1 != "" {
			subst = s1
		}
		if err != nil {
			return nil, "", err
		}
		for i := range locs {
			if locs[i].PC == 0 {
				continue
			}
			file, line, fn := t.BinInfo().PCToLine(locs[i].PC)
			locs[i].File = file
			locs[i].Line = line
			locs[i].Function = api.ConvertFunction(fn)
			locs[i].PCPids = make([]int, len(locs[i].PCs))
			for j := range locs[i].PCs {
				locs[i].PCPids[j] = pid
			}
		}
		locations = append(locations, locs...)
	}
	return locations, subst, nil
}

// Disassemble code between startPC and endPC.
// if endPC == 0 it will find the function containing startPC and disassemble the whole function.
func (d *Debugger) Disassemble(goroutineID int64, addr1, addr2 uint64) ([]proc.AsmInstruction, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	if _, err := d.target.Valid(); err != nil {
		return nil, err
	}

	if addr2 == 0 {
		fn := d.target.Selected.BinInfo().PCToFunc(addr1)
		if fn == nil {
			return nil, fmt.Errorf("address %#x does not belong to any function", addr1)
		}
		addr1 = fn.Entry
		addr2 = fn.End
	}

	g, err := proc.FindGoroutine(d.target.Selected, goroutineID)
	if err != nil {
		return nil, err
	}

	curthread := d.target.Selected.CurrentThread()
	if g != nil && g.Thread != nil {
		curthread = g.Thread
	}
	regs, _ := curthread.Registers()

	return proc.Disassemble(d.target.Selected.Memory(), regs, d.target.Selected.Breakpoints(), d.target.Selected.BinInfo(), addr1, addr2)
}

func (d *Debugger) AsmInstructionText(inst *proc.AsmInstruction, flavour proc.AssemblyFlavour) string {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return inst.Text(flavour, d.target.Selected.BinInfo())
}

// Recorded returns true if the target is a recording.
func (d *Debugger) Recorded() (recorded bool, tracedir string) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.Recorded()
}

// FindThreadReturnValues returns the return values of the function that
// the thread of the given 'id' just stepped out of.
func (d *Debugger) FindThreadReturnValues(id int, cfg proc.LoadConfig) ([]*proc.Variable, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	if _, err := d.target.Valid(); err != nil {
		return nil, err
	}

	thread, found := d.target.Selected.FindThread(id)
	if !found {
		return nil, fmt.Errorf("could not find thread %d", id)
	}

	return thread.Common().ReturnValues(cfg), nil
}

// Checkpoint will set a checkpoint specified by the locspec.
func (d *Debugger) Checkpoint(where string) (int, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.Checkpoint(where)
}

// Checkpoints will return a list of checkpoints.
func (d *Debugger) Checkpoints() ([]proc.Checkpoint, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.Checkpoints()
}

// ClearCheckpoint will clear the checkpoint of the given ID.
func (d *Debugger) ClearCheckpoint(id int) error {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.ClearCheckpoint(id)
}

// ListDynamicLibraries returns a list of loaded dynamic libraries.
func (d *Debugger) ListDynamicLibraries() []*proc.Image {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.Selected.BinInfo().Images[1:] // skips the first image because it's the executable file
}

// ExamineMemory returns the raw memory stored at the given address.
// The amount of data to be read is specified by length.
// This function will return an error if it reads less than `length` bytes.
func (d *Debugger) ExamineMemory(address uint64, length int) ([]byte, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()

	mem := d.target.Selected.Memory()
	data := make([]byte, length)
	n, err := mem.ReadMemory(data, address)
	if err != nil {
		return nil, err
	}
	if length != n {
		return nil, errors.New("the specific range has exceeded readable area")
	}
	return data, nil
}

func (d *Debugger) GetVersion(out *api.GetVersionOut) error {
	if d.config.CoreFile != "" {
		if d.config.Backend == "rr" {
			out.Backend = "rr"
		} else {
			out.Backend = "core"
		}
	} else {
		if d.config.Backend == "default" {
			if runtime.GOOS == "darwin" {
				out.Backend = "lldb"
			} else {
				out.Backend = "native"
			}
		} else {
			out.Backend = d.config.Backend
		}
	}

	if !d.isRecording() && !d.IsRunning() {
		out.TargetGoVersion = d.target.Selected.BinInfo().Producer()
	}

	out.MinSupportedVersionOfGo = fmt.Sprintf("%d.%d.0", goversion.MinSupportedVersionOfGoMajor, goversion.MinSupportedVersionOfGoMinor)
	out.MaxSupportedVersionOfGo = fmt.Sprintf("%d.%d.0", goversion.MaxSupportedVersionOfGoMajor, goversion.MaxSupportedVersionOfGoMinor)

	return nil
}

// ListPackagesBuildInfo returns the list of packages used by the program along with
// the directory where each package was compiled and optionally the list of
// files constituting the package.
func (d *Debugger) ListPackagesBuildInfo(includeFiles bool) []*proc.PackageBuildInfo {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.Selected.BinInfo().ListPackagesBuildInfo(includeFiles)
}

// StopRecording stops a recording (if one is in progress)
func (d *Debugger) StopRecording() error {
	d.recordMutex.Lock()
	defer d.recordMutex.Unlock()
	if d.stopRecording == nil {
		return ErrNotRecording
	}
	return d.stopRecording()
}

// StopReason returns the reason why the target process is stopped.
// A process could be stopped for multiple simultaneous reasons, in which
// case only one will be reported.
func (d *Debugger) StopReason() proc.StopReason {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.Selected.StopReason
}

// LockTarget acquires the target mutex.
func (d *Debugger) LockTarget() {
	d.targetMutex.Lock()
}

// UnlockTarget releases the target mutex.
func (d *Debugger) UnlockTarget() {
	d.targetMutex.Unlock()
}

// DumpStart starts a core dump to dest.
func (d *Debugger) DumpStart(dest string) error {
	d.targetMutex.Lock()
	// targetMutex will only be unlocked when the dump is done

	//TODO(aarzilli): what do we do if the user switches to a different target after starting a dump but before it's finished?

	if !d.target.CanDump {
		d.targetMutex.Unlock()
		return ErrCoreDumpNotSupported
	}

	d.dumpState.Mutex.Lock()
	defer d.dumpState.Mutex.Unlock()

	if d.dumpState.Dumping {
		d.targetMutex.Unlock()
		return ErrCoreDumpInProgress
	}

	fh, err := os.Create(dest)
	if err != nil {
		d.targetMutex.Unlock()
		return err
	}

	d.dumpState.Dumping = true
	d.dumpState.AllDone = false
	d.dumpState.Canceled = false
	d.dumpState.DoneChan = make(chan struct{})
	d.dumpState.ThreadsDone = 0
	d.dumpState.ThreadsTotal = 0
	d.dumpState.MemDone = 0
	d.dumpState.MemTotal = 0
	d.dumpState.Err = nil
	go func() {
		defer d.targetMutex.Unlock()
		d.target.Selected.Dump(fh, 0, &d.dumpState)
	}()

	return nil
}

// DumpWait waits for the dump to finish, or for the duration of wait.
// Returns the state of the dump.
// If wait == 0 returns immediately.
func (d *Debugger) DumpWait(wait time.Duration) *proc.DumpState {
	d.dumpState.Mutex.Lock()
	if !d.dumpState.Dumping {
		d.dumpState.Mutex.Unlock()
		return &d.dumpState
	}
	d.dumpState.Mutex.Unlock()

	if wait > 0 {
		alarm := time.After(wait)
		select {
		case <-alarm:
		case <-d.dumpState.DoneChan:
		}
	}

	return &d.dumpState
}

// DumpCancel cancels a dump in progress
func (d *Debugger) DumpCancel() error {
	d.dumpState.Mutex.Lock()
	d.dumpState.Canceled = true
	d.dumpState.Mutex.Unlock()
	return nil
}

func (d *Debugger) Target() *proc.Target {
	return d.target.Selected
}

func (d *Debugger) TargetGroup() *proc.TargetGroup {
	return d.target
}

func (d *Debugger) BuildID() string {
	loc, err := d.target.Selected.CurrentThread().Location()
	if err != nil {
		return ""
	}
	img := d.target.Selected.BinInfo().PCToImage(loc.PC)
	return img.BuildID
}

func (d *Debugger) AttachPid() int {
	return d.config.AttachPid
}

func (d *Debugger) GetBufferedTracepoints() []api.TracepointResult {
	traces := d.target.Selected.GetBufferedTracepoints()
	if traces == nil {
		return nil
	}
	results := make([]api.TracepointResult, len(traces))
	for i, trace := range traces {
		results[i].IsRet = trace.IsRet

		f, l, fn := d.target.Selected.BinInfo().PCToLine(uint64(trace.FnAddr))

		results[i].FunctionName = fn.Name
		results[i].Line = l
		results[i].File = f
		results[i].GoroutineID = trace.GoroutineID

		for _, p := range trace.InputParams {
			results[i].InputParams = append(results[i].InputParams, *api.ConvertVar(p))
		}
		for _, p := range trace.ReturnParams {
			results[i].ReturnParams = append(results[i].ReturnParams, *api.ConvertVar(p))
		}
	}
	return results
}

// FollowExec enabled or disables follow exec mode.
func (d *Debugger) FollowExec(enabled bool, regex string) error {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.FollowExec(enabled, regex)
}

// FollowExecEnabled returns true if follow exec mode is enabled.
func (d *Debugger) FollowExecEnabled() bool {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	return d.target.FollowExecEnabled()
}

func (d *Debugger) SetDebugInfoDirectories(v []string) {
	d.recordMutex.Lock()
	defer d.recordMutex.Unlock()
	it := proc.ValidTargets{Group: d.target}
	for it.Next() {
		it.BinInfo().DebugInfoDirectories = v
	}
}

func (d *Debugger) DebugInfoDirectories() []string {
	d.recordMutex.Lock()
	defer d.recordMutex.Unlock()
	return d.target.Selected.BinInfo().DebugInfoDirectories
}

// ChanGoroutines returns the list of goroutines waiting on the channel specified by expr.
func (d *Debugger) ChanGoroutines(goid int64, frame, deferredCall int, expr string, start, count int) ([]*proc.G, error) {
	d.targetMutex.Lock()
	defer d.targetMutex.Unlock()
	s, err := proc.ConvertEvalScope(d.target.Selected, goid, frame, deferredCall)
	if err != nil {
		return nil, err
	}

	goids, err := s.ChanGoroutines(expr, start, count)
	if err != nil {
		return nil, err
	}

	gs := make([]*proc.G, len(goids))
	for i := range goids {
		g, err := proc.FindGoroutine(d.target.Selected, goids[i])
		if g == nil {
			g = &proc.G{Unreadable: err}
		}
		gs[i] = g
	}
	return gs, nil
}

func go11DecodeErrorCheck(err error) error {
	if !errors.Is(err, dwarf.DecodeError{}) {
		return err
	}

	gover, ok := goversion.Installed()
	if !ok || !gover.AfterOrEqual(goversion.GoVersion{Major: 1, Minor: 11, Rev: -1}) || goversion.VersionAfterOrEqual(runtime.Version(), 1, 11) {
		return err
	}

	return errors.New("executables built by Go 1.11 or later need Delve built by Go 1.11 or later")
}

const NoDebugWarning string = "debuggee must not be built with 'go run' or -ldflags='-s -w', which strip debug info"

func noDebugErrorWarning(err error) error {
	if errors.Is(err, dwarf.DecodeError{}) || strings.Contains(err.Error(), "could not open debug info") {
		return fmt.Errorf("%s - %s", err.Error(), NoDebugWarning)
	}
	return err
}

func verifyBinaryFormat(exePath string) (string, error) {
	fullpath, err := filepath.Abs(exePath)
	if err != nil {
		return "", err
	}

	f, err := os.Open(fullpath)
	if err != nil {
		return "", err
	}
	defer f.Close()

	// Skip this check on Windows.
	// TODO(derekparker) exec.LookPath looks for valid Windows extensions.
	// We don't create our binaries with valid extensions, even though we should.
	// Skip this check for now.
	if runtime.GOOS != "windows" {
		_, err = exec.LookPath(fullpath)
		if err != nil {
			return "", api.ErrNotExecutable
		}
	}

	// check that the binary format is what we expect for the host system
	var exe io.Closer
	switch runtime.GOOS {
	case "darwin":
		exe, err = macho.NewFile(f)
	case "linux", "freebsd":
		exe, err = elf.NewFile(f)
	case "windows":
		exe, err = pe.NewFile(f)
	default:
		panic("attempting to open file Delve cannot parse")
	}

	if err != nil {
		return "", api.ErrNotExecutable
	}
	exe.Close()
	return fullpath, nil
}

var attachErrorMessage = attachErrorMessageDefault

func attachErrorMessageDefault(pid int, err error) error {
	return fmt.Errorf("could not attach to pid %d: %s", pid, err)
}

func (d *Debugger) maybePrintUnattendedStopWarning(stopReason proc.StopReason, currentThread *api.Thread, clientStatusCh <-chan struct{}) {
	select {
	case <-clientStatusCh:
		// the channel will be closed if the client that sends the command has left
		// i.e. closed the connection.
	default:
		return
	}

	if currentThread == nil || currentThread.Breakpoint == nil {
		switch stopReason {
		case proc.StopManual:
			// print nothing
			return
		default:
			fmt.Fprintln(os.Stderr, "Stop reason: "+stopReason.String())
			return
		}
	}

	const defaultStackTraceDepth = 50
	frames, err := d.stacktrace(currentThread.GoroutineID, defaultStackTraceDepth, 0)
	if err != nil {
		fmt.Fprintln(os.Stderr, "err", err)
		return
	}

	apiFrames, err := d.convertStacktrace(frames, nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, "err", err)
		return
	}

	bp := currentThread.Breakpoint
	switch bp.Name {
	case proc.FatalThrow, proc.UnrecoveredPanic:
		fmt.Fprintln(os.Stderr, "\n** execution is paused because your program is panicking **")
	default:
		fmt.Fprintln(os.Stderr, "\n** execution is paused because a breakpoint is hit **")
	}

	fmt.Fprintf(os.Stderr, "To continue the execution please connect your client to the debugger.")
	fmt.Fprintln(os.Stderr, "\nStack trace:")

	formatPathFunc := func(s string) string {
		return s
	}
	includeFunc := func(f api.Stackframe) bool {
		// todo(fata): do not include the final panic/fatal function if bp.Name is fatalthrow/panic
		return true
	}
	api.PrintStack(formatPathFunc, os.Stderr, apiFrames, "", false, api.StackTraceColors{}, includeFunc)
}

// GuessSubstitutePath returns a substitute-path configuration that maps
// server paths to client paths by examining the executable file and a map
// of module paths to client directories (clientMod2Dir) passed as input.
func (d *Debugger) GuessSubstitutePath(args *api.GuessSubstitutePathIn) map[string]string {
	bis := []*proc.BinaryInfo{}
	bins := [][]proc.Function{}
	tgt := proc.ValidTargets{Group: d.target}
	for tgt.Next() {
		bi := tgt.BinInfo()
		bis = append(bis, bi)
		bins = append(bins, bi.Functions)
	}
	return guessSubstitutePath(args, bins, func(biIdx int, fn *proc.Function) string {
		file, _ := bis[biIdx].EntryLineForFunc(fn)
		return file
	})
}

func guessSubstitutePath(args *api.GuessSubstitutePathIn, bins [][]proc.Function, fileForFunc func(int, *proc.Function) string) map[string]string {
	serverMod2Dir := map[string]string{}
	serverMod2DirCandidate := map[string]map[string]int{}
	pkg2mod := map[string]string{}

	for mod := range args.ClientModuleDirectories {
		serverMod2DirCandidate[mod] = make(map[string]int)
	}

	const minEvidence = 10
	const decisionThreshold = 0.8

	totCandidates := func(mod string) int {
		r := 0
		for _, cnt := range serverMod2DirCandidate[mod] {
			r += cnt
		}
		return r
	}

	bestCandidate := func(mod string) string {
		best := ""
		for dir, cnt := range serverMod2DirCandidate[mod] {
			if cnt > serverMod2DirCandidate[mod][best] {
				best = dir
			}
		}
		return best
	}

	slashes := func(s string) int {
		r := 0
		for _, ch := range s {
			if ch == '/' {
				r++
			}
		}
		return r
	}

	serverGoroot := ""

	logger := logflags.DebuggerLogger()

	for binIdx, bin := range bins {
		for i := range bin {
			fn := &bin[i]

			if fn.Name == "runtime.main" && serverGoroot == "" {
				file := fileForFunc(binIdx, fn)
				serverGoroot = path.Dir(path.Dir(path.Dir(file)))
				continue
			}

			fnpkg := fn.PackageName()
			if fn.CompilationUnitName() != "" && strings.ReplaceAll(fn.CompilationUnitName(), "\\", "/") != fnpkg {
				// inlined
				continue
			}

			if fnpkg == "main" && binIdx == 0 && args.ImportPathOfMainPackage != "" {
				fnpkg = args.ImportPathOfMainPackage
			}

			fnmod := ""

			if mod, ok := pkg2mod[fnpkg]; ok {
				fnmod = mod
			} else {
				for mod := range args.ClientModuleDirectories {
					if strings.HasPrefix(fnpkg, mod) {
						fnmod = mod
						break
					}
				}
				pkg2mod[fnpkg] = fnmod
				if fnmod == "" {
					logger.Debugf("No module detected for server package %q", fnpkg)
				}
			}

			if fnmod == "" {
				// not in any module we are interested in
				continue
			}
			if serverMod2Dir[fnmod] != "" {
				// already decided
				continue
			}

			elems := slashes(fnpkg[len(fnmod):])

			file := fileForFunc(binIdx, fn)
			if file == "" || file == "<autogenerated>" {
				continue
			}
			logger.Debugf("considering %s pkg:%s compile unit:%s file:%s", fn.Name, fnpkg, fn.CompilationUnitName(), file)
			dir := path.Dir(file) // note: paths are normalized to always use '/' as a separator by pkg/dwarf/line
			if slashes(dir) < elems {
				continue
			}
			for i := 0; i < elems; i++ {
				dir = path.Dir(dir)
			}

			serverMod2DirCandidate[fnmod][dir]++

			n := totCandidates(fnmod)
			best := bestCandidate(fnmod)
			if n > minEvidence && float64(serverMod2DirCandidate[fnmod][best])/float64(n) > decisionThreshold {
				serverMod2Dir[fnmod] = best
			}
		}
	}

	for mod := range args.ClientModuleDirectories {
		if serverMod2Dir[mod] == "" {
			serverMod2Dir[mod] = bestCandidate(mod)
		}
	}

	server2Client := make(map[string]string)

	for mod, clientDir := range args.ClientModuleDirectories {
		if serverMod2Dir[mod] != "" {
			server2Client[serverMod2Dir[mod]] = clientDir
		}
	}

	if serverGoroot != "" && args.ClientGOROOT != "" {
		server2Client[serverGoroot] = args.ClientGOROOT
	}

	return server2Client
}