forked from go-delve/delve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration2_test.go
2963 lines (2588 loc) · 92.4 KB
/
integration2_test.go
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
package service_test
import (
"flag"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"time"
protest "github.com/go-delve/delve/pkg/proc/test"
"github.com/go-delve/delve/service/debugger"
"github.com/go-delve/delve/pkg/goversion"
"github.com/go-delve/delve/pkg/logflags"
"github.com/go-delve/delve/service"
"github.com/go-delve/delve/service/api"
"github.com/go-delve/delve/service/rpc2"
"github.com/go-delve/delve/service/rpccommon"
)
var normalLoadConfig = api.LoadConfig{
FollowPointers: true,
MaxVariableRecurse: 1,
MaxStringLen: 64,
MaxArrayValues: 64,
MaxStructFields: -1,
}
var testBackend, buildMode string
func TestMain(m *testing.M) {
flag.StringVar(&testBackend, "backend", "", "selects backend")
flag.StringVar(&buildMode, "test-buildmode", "", "selects build mode")
var logOutput string
flag.StringVar(&logOutput, "log-output", "", "configures log output")
flag.Parse()
protest.DefaultTestBackend(&testBackend)
if buildMode != "" && buildMode != "pie" {
fmt.Fprintf(os.Stderr, "unknown build mode %q", buildMode)
os.Exit(1)
}
logflags.Setup(logOutput != "", logOutput, "")
os.Exit(protest.RunTestsWithFixtures(m))
}
func withTestClient2(name string, t *testing.T, fn func(c service.Client)) {
withTestClient2Extended(name, t, 0, [3]string{}, nil, func(c service.Client, fixture protest.Fixture) {
fn(c)
})
}
func startServer(name string, buildFlags protest.BuildFlags, t *testing.T, redirects [3]string, args []string) (clientConn net.Conn, fixture protest.Fixture) {
if testBackend == "rr" {
protest.MustHaveRecordingAllowed(t)
}
listener, clientConn := service.ListenerPipe()
defer listener.Close()
if buildMode == "pie" {
buildFlags |= protest.BuildModePIE
}
fixture = protest.BuildFixture(name, buildFlags)
for i := range redirects {
if redirects[i] != "" {
redirects[i] = filepath.Join(fixture.BuildDir, redirects[i])
}
}
server := rpccommon.NewServer(&service.Config{
Listener: listener,
ProcessArgs: append([]string{fixture.Path}, args...),
Debugger: debugger.Config{
Backend: testBackend,
CheckGoVersion: true,
Packages: []string{fixture.Source},
BuildFlags: "", // build flags can be an empty string here because the only test that uses it, does not set special flags.
ExecuteKind: debugger.ExecutingGeneratedFile,
Redirects: redirects,
},
})
if err := server.Run(); err != nil {
t.Fatal(err)
}
return clientConn, fixture
}
func withTestClient2Extended(name string, t *testing.T, buildFlags protest.BuildFlags, redirects [3]string, args []string, fn func(c service.Client, fixture protest.Fixture)) {
clientConn, fixture := startServer(name, buildFlags, t, redirects, args)
client := rpc2.NewClientFromConn(clientConn)
defer func() {
client.Detach(true)
}()
fn(client, fixture)
}
func TestRunWithInvalidPath(t *testing.T) {
if testBackend == "rr" {
// This test won't work because rr returns an error, after recording, when
// the recording failed but also when the recording succeeded but the
// inferior returned an error. Therefore we have to ignore errors from rr.
return
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("couldn't start listener: %s\n", err)
}
defer listener.Close()
server := rpccommon.NewServer(&service.Config{
Listener: listener,
ProcessArgs: []string{"invalid_path"},
APIVersion: 2,
Debugger: debugger.Config{
Backend: testBackend,
ExecuteKind: debugger.ExecutingGeneratedFile,
},
})
if err := server.Run(); err == nil {
t.Fatal("Expected Run to return error for invalid program path")
}
}
func TestRestart_afterExit(t *testing.T) {
withTestClient2("continuetestprog", t, func(c service.Client) {
origPid := c.ProcessPid()
state := <-c.Continue()
if !state.Exited {
t.Fatal("expected initial process to have exited")
}
if _, err := c.Restart(false); err != nil {
t.Fatal(err)
}
if c.ProcessPid() == origPid {
t.Fatal("did not spawn new process, has same PID")
}
state = <-c.Continue()
if !state.Exited {
t.Fatalf("expected restarted process to have exited %v", state)
}
})
}
func TestRestart_breakpointPreservation(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("continuetestprog", t, func(c service.Client) {
_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1, Name: "firstbreakpoint", Tracepoint: true})
assertNoError(err, t, "CreateBreakpoint()")
stateCh := c.Continue()
state := <-stateCh
if state.CurrentThread.Breakpoint.Name != "firstbreakpoint" || !state.CurrentThread.Breakpoint.Tracepoint {
t.Fatalf("Wrong breakpoint: %#v\n", state.CurrentThread.Breakpoint)
}
state = <-stateCh
if !state.Exited {
t.Fatal("Did not exit after first tracepoint")
}
t.Log("Restart")
c.Restart(false)
stateCh = c.Continue()
state = <-stateCh
if state.CurrentThread.Breakpoint.Name != "firstbreakpoint" || !state.CurrentThread.Breakpoint.Tracepoint {
t.Fatalf("Wrong breakpoint (after restart): %#v\n", state.CurrentThread.Breakpoint)
}
state = <-stateCh
if !state.Exited {
t.Fatal("Did not exit after first tracepoint (after restart)")
}
})
}
func TestRestart_duringStop(t *testing.T) {
withTestClient2("continuetestprog", t, func(c service.Client) {
origPid := c.ProcessPid()
_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1})
if err != nil {
t.Fatal(err)
}
state := <-c.Continue()
if state.CurrentThread.Breakpoint == nil {
t.Fatal("did not hit breakpoint")
}
if _, err := c.Restart(false); err != nil {
t.Fatal(err)
}
if c.ProcessPid() == origPid {
t.Fatal("did not spawn new process, has same PID")
}
bps, err := c.ListBreakpoints(false)
if err != nil {
t.Fatal(err)
}
if len(bps) == 0 {
t.Fatal("breakpoints not preserved")
}
})
}
// This source is a slightly modified version of
// _fixtures/testenv.go. The only difference is that
// the name of the environment variable we are trying to
// read is named differently, so we can assert the code
// was actually changed in the test.
const modifiedSource = `package main
import (
"fmt"
"os"
"runtime"
)
func main() {
x := os.Getenv("SOMEMODIFIEDVAR")
runtime.Breakpoint()
fmt.Printf("SOMEMODIFIEDVAR=%s\n", x)
}
`
func TestRestart_rebuild(t *testing.T) {
// In the original fixture file the env var tested for is SOMEVAR.
os.Setenv("SOMEVAR", "bah")
withTestClient2Extended("testenv", t, 0, [3]string{}, nil, func(c service.Client, f protest.Fixture) {
<-c.Continue()
var1, err := c.EvalVariable(api.EvalScope{GoroutineID: -1}, "x", normalLoadConfig)
assertNoError(err, t, "EvalVariable")
if var1.Value != "bah" {
t.Fatalf("expected 'bah' got %q", var1.Value)
}
fi, err := os.Stat(f.Source)
assertNoError(err, t, "Stat fixture.Source")
originalSource, err := ioutil.ReadFile(f.Source)
assertNoError(err, t, "Reading original source")
// Ensure we write the original source code back after the test exits.
defer ioutil.WriteFile(f.Source, originalSource, fi.Mode())
// Write modified source code to the fixture file.
err = ioutil.WriteFile(f.Source, []byte(modifiedSource), fi.Mode())
assertNoError(err, t, "Writing modified source")
// First set our new env var and ensure later that the
// modified source code picks it up.
os.Setenv("SOMEMODIFIEDVAR", "foobar")
// Restart the program, rebuilding from source.
_, err = c.Restart(true)
assertNoError(err, t, "Restart(true)")
<-c.Continue()
var1, err = c.EvalVariable(api.EvalScope{GoroutineID: -1}, "x", normalLoadConfig)
assertNoError(err, t, "EvalVariable")
if var1.Value != "foobar" {
t.Fatalf("expected 'foobar' got %q", var1.Value)
}
})
}
func TestClientServer_exit(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("continuetestprog", t, func(c service.Client) {
state, err := c.GetState()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if e, a := false, state.Exited; e != a {
t.Fatalf("Expected exited %v, got %v", e, a)
}
state = <-c.Continue()
if state.Err == nil {
t.Fatalf("Error expected after continue")
}
if !state.Exited {
t.Fatalf("Expected exit after continue: %v", state)
}
_, err = c.GetState()
if err == nil {
t.Fatal("Expected error on querying state from exited process")
}
})
}
func TestClientServer_step(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("testprog", t, func(c service.Client) {
_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: -1})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
stateBefore := <-c.Continue()
if stateBefore.Err != nil {
t.Fatalf("Unexpected error: %v", stateBefore.Err)
}
stateAfter, err := c.Step()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if before, after := stateBefore.CurrentThread.PC, stateAfter.CurrentThread.PC; before >= after {
t.Fatalf("Expected %#v to be greater than %#v", after, before)
}
})
}
func TestClientServer_stepout(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("testnextprog", t, func(c service.Client) {
_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: -1})
assertNoError(err, t, "CreateBreakpoint()")
stateBefore := <-c.Continue()
assertNoError(stateBefore.Err, t, "Continue()")
if stateBefore.CurrentThread.Line != 13 {
t.Fatalf("wrong line number %s:%d, expected %d", stateBefore.CurrentThread.File, stateBefore.CurrentThread.Line, 13)
}
stateAfter, err := c.StepOut()
assertNoError(err, t, "StepOut()")
if stateAfter.CurrentThread.Line != 35 {
t.Fatalf("wrong line number %s:%d, expected %d", stateAfter.CurrentThread.File, stateAfter.CurrentThread.Line, 13)
}
})
}
func testnext2(testcases []nextTest, initialLocation string, t *testing.T) {
protest.AllowRecording(t)
withTestClient2("testnextprog", t, func(c service.Client) {
bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: initialLocation, Line: -1})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
state := <-c.Continue()
if state.Err != nil {
t.Fatalf("Unexpected error: %v", state.Err)
}
_, err = c.ClearBreakpoint(bp.ID)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
for _, tc := range testcases {
if state.CurrentThread.Line != tc.begin {
t.Fatalf("Program not stopped at correct spot expected %d was %d", tc.begin, state.CurrentThread.Line)
}
t.Logf("Next for scenario %#v", tc)
state, err = c.Next()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if state.CurrentThread.Line != tc.end {
t.Fatalf("Program did not continue to correct next location expected %d was %d", tc.end, state.CurrentThread.Line)
}
}
})
}
func TestNextGeneral(t *testing.T) {
var testcases []nextTest
ver, _ := goversion.Parse(runtime.Version())
if ver.Major < 0 || ver.AfterOrEqual(goversion.GoVersion{Major: 1, Minor: 7, Rev: -1}) {
testcases = []nextTest{
{17, 19},
{19, 20},
{20, 23},
{23, 24},
{24, 26},
{26, 31},
{31, 23},
{23, 24},
{24, 26},
{26, 31},
{31, 23},
{23, 24},
{24, 26},
{26, 27},
{27, 28},
{28, 34},
}
} else {
testcases = []nextTest{
{17, 19},
{19, 20},
{20, 23},
{23, 24},
{24, 26},
{26, 31},
{31, 23},
{23, 24},
{24, 26},
{26, 31},
{31, 23},
{23, 24},
{24, 26},
{26, 27},
{27, 34},
}
}
testnext2(testcases, "main.testnext", t)
}
func TestNextFunctionReturn(t *testing.T) {
testcases := []nextTest{
{13, 14},
{14, 15},
{15, 35},
}
testnext2(testcases, "main.helloworld", t)
}
func TestClientServer_breakpointInMainThread(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("testprog", t, func(c service.Client) {
bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.helloworld", Line: 1})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
state := <-c.Continue()
if state.Err != nil {
t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
}
pc := state.CurrentThread.PC
if pc-1 != bp.Addr && pc != bp.Addr {
f, l := state.CurrentThread.File, state.CurrentThread.Line
t.Fatalf("Break not respected:\nPC:%#v %s:%d\nFN:%#v \n", pc, f, l, bp.Addr)
}
})
}
func TestClientServer_breakpointInSeparateGoroutine(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("testthreads", t, func(c service.Client) {
_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.anotherthread", Line: 1})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
state := <-c.Continue()
if state.Err != nil {
t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
}
f, l := state.CurrentThread.File, state.CurrentThread.Line
if f != "testthreads.go" && l != 9 {
t.Fatal("Program did not hit breakpoint")
}
})
}
func TestClientServer_breakAtNonexistentPoint(t *testing.T) {
withTestClient2("testprog", t, func(c service.Client) {
_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "nowhere", Line: 1})
if err == nil {
t.Fatal("Should not be able to break at non existent function")
}
})
}
func TestClientServer_clearBreakpoint(t *testing.T) {
withTestClient2("testprog", t, func(c service.Client) {
bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sleepytime", Line: 1})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if e, a := 1, countBreakpoints(t, c); e != a {
t.Fatalf("Expected breakpoint count %d, got %d", e, a)
}
deleted, err := c.ClearBreakpoint(bp.ID)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if deleted.ID != bp.ID {
t.Fatalf("Expected deleted breakpoint ID %v, got %v", bp.ID, deleted.ID)
}
if e, a := 0, countBreakpoints(t, c); e != a {
t.Fatalf("Expected breakpoint count %d, got %d", e, a)
}
})
}
func TestClientServer_toggleBreakpoint(t *testing.T) {
withTestClient2("testtoggle", t, func(c service.Client) {
toggle := func(bp *api.Breakpoint) {
t.Helper()
dbp, err := c.ToggleBreakpoint(bp.ID)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if dbp.ID != bp.ID {
t.Fatalf("The IDs don't match")
}
}
// This one is toggled twice
bp1, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.lineOne", Tracepoint: true})
if err != nil {
t.Fatalf("Unexpected error: %v\n", err)
}
toggle(bp1)
toggle(bp1)
// This one is toggled once
bp2, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.lineTwo", Tracepoint: true})
if err != nil {
t.Fatalf("Unexpected error: %v\n", err)
}
toggle(bp2)
// This one is never toggled
bp3, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.lineThree", Tracepoint: true})
if err != nil {
t.Fatalf("Unexpected error: %v\n", err)
}
if e, a := 3, countBreakpoints(t, c); e != a {
t.Fatalf("Expected breakpoint count %d, got %d", e, a)
}
enableCount := 0
disabledCount := 0
contChan := c.Continue()
for state := range contChan {
if state.CurrentThread != nil && state.CurrentThread.Breakpoint != nil {
switch state.CurrentThread.Breakpoint.ID {
case bp1.ID, bp3.ID:
enableCount++
case bp2.ID:
disabledCount++
}
t.Logf("%v", state)
}
if state.Exited {
continue
}
if state.Err != nil {
t.Fatalf("Unexpected error during continue: %v\n", state.Err)
}
}
if enableCount != 2 {
t.Fatalf("Wrong number of enabled hits: %d\n", enableCount)
}
if disabledCount != 0 {
t.Fatalf("A disabled breakpoint was hit: %d\n", disabledCount)
}
})
}
func TestClientServer_toggleAmendedBreakpoint(t *testing.T) {
withTestClient2("testtoggle", t, func(c service.Client) {
toggle := func(bp *api.Breakpoint) {
dbp, err := c.ToggleBreakpoint(bp.ID)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if dbp.ID != bp.ID {
t.Fatalf("The IDs don't match")
}
}
// This one is toggled twice
bp, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.lineOne", Tracepoint: true})
if err != nil {
t.Fatalf("Unexpected error: %v\n", err)
}
bp.Cond = "n == 7"
assertNoError(c.AmendBreakpoint(bp), t, "AmendBreakpoint() 1")
// Toggle off.
toggle(bp)
// Toggle on.
toggle(bp)
amended, err := c.GetBreakpoint(bp.ID)
if err != nil {
t.Fatal(err)
}
if amended.Cond == "" {
t.Fatal("breakpoint amendedments not preserved after toggle")
}
})
}
func TestClientServer_disableHitCondLSSBreakpoint(t *testing.T) {
withTestClient2("break", t, func(c service.Client) {
fp := testProgPath(t, "break")
hitCondBp, err := c.CreateBreakpoint(&api.Breakpoint{
File: fp,
Line: 7,
HitCond: "< 3",
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
state := <-c.Continue()
if state.Err != nil {
t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
}
f, l := state.CurrentThread.File, state.CurrentThread.Line
if f != "break.go" && l != 7 {
t.Fatal("Program did not hit breakpoint")
}
ivar, err := c.EvalVariable(api.EvalScope{GoroutineID: -1}, "i", normalLoadConfig)
assertNoError(err, t, "EvalVariable")
t.Logf("ivar: %s", ivar.SinglelineString())
if ivar.Value != "1" {
t.Fatalf("Wrong variable value: %s", ivar.Value)
}
bp, err := c.GetBreakpoint(hitCondBp.ID)
assertNoError(err, t, "GetBreakpoint()")
if bp.Disabled {
t.Fatalf(
"Hit condition %s is still satisfiable but breakpoint has been disabled",
bp.HitCond,
)
}
state = <-c.Continue()
if state.Err != nil {
t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
}
f, l = state.CurrentThread.File, state.CurrentThread.Line
if f != "break.go" && l != 7 {
t.Fatal("Program did not hit breakpoint")
}
ivar, err = c.EvalVariable(api.EvalScope{GoroutineID: -1}, "i", normalLoadConfig)
assertNoError(err, t, "EvalVariable")
t.Logf("ivar: %s", ivar.SinglelineString())
if ivar.Value != "2" {
t.Fatalf("Wrong variable value: %s", ivar.Value)
}
bp, err = c.GetBreakpoint(hitCondBp.ID)
assertNoError(err, t, "GetBreakpoint()")
if !bp.Disabled {
t.Fatalf(
"Hit condition %s is no more satisfiable but breakpoint has not been disabled",
bp.HitCond,
)
}
})
}
func TestClientServer_disableHitEQLCondBreakpoint(t *testing.T) {
withTestClient2("break", t, func(c service.Client) {
fp := testProgPath(t, "break")
hitCondBp, err := c.CreateBreakpoint(&api.Breakpoint{
File: fp,
Line: 7,
HitCond: "== 3",
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
state := <-c.Continue()
if state.Err != nil {
t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
}
f, l := state.CurrentThread.File, state.CurrentThread.Line
if f != "break.go" && l != 7 {
t.Fatal("Program did not hit breakpoint")
}
ivar, err := c.EvalVariable(api.EvalScope{GoroutineID: -1}, "i", normalLoadConfig)
assertNoError(err, t, "EvalVariable")
t.Logf("ivar: %s", ivar.SinglelineString())
if ivar.Value != "3" {
t.Fatalf("Wrong variable value: %s", ivar.Value)
}
bp, err := c.GetBreakpoint(hitCondBp.ID)
assertNoError(err, t, "GetBreakpoint()")
if !bp.Disabled {
t.Fatalf(
"Hit condition %s is no more satisfiable but breakpoint has not been disabled",
bp.HitCond,
)
}
})
}
func TestClientServer_switchThread(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("testnextprog", t, func(c service.Client) {
// With invalid thread id
_, err := c.SwitchThread(-1)
if err == nil {
t.Fatal("Expected error for invalid thread id")
}
_, err = c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
state := <-c.Continue()
if state.Err != nil {
t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
}
var nt int
ct := state.CurrentThread.ID
threads, err := c.ListThreads()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
for _, th := range threads {
if th.ID != ct {
nt = th.ID
break
}
}
if nt == 0 {
t.Fatal("could not find thread to switch to")
}
// With valid thread id
state, err = c.SwitchThread(nt)
if err != nil {
t.Fatal(err)
}
if state.CurrentThread.ID != nt {
t.Fatal("Did not switch threads")
}
})
}
func TestClientServer_infoLocals(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("testnextprog", t, func(c service.Client) {
fp := testProgPath(t, "testnextprog")
_, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 24})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
state := <-c.Continue()
if state.Err != nil {
t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
}
locals, err := c.ListLocalVariables(api.EvalScope{GoroutineID: -1}, normalLoadConfig)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(locals) != 3 {
t.Fatalf("Expected 3 locals, got %d %#v", len(locals), locals)
}
})
}
func TestClientServer_infoArgs(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("testnextprog", t, func(c service.Client) {
fp := testProgPath(t, "testnextprog")
_, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 47})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
state := <-c.Continue()
if state.Err != nil {
t.Fatalf("Unexpected error: %v, state: %#v", state.Err, state)
}
regs, err := c.ListThreadRegisters(0, false)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(regs) == 0 {
t.Fatal("Expected string showing registers values, got empty string")
}
regs, err = c.ListScopeRegisters(api.EvalScope{GoroutineID: -1, Frame: 0}, false)
assertNoError(err, t, "ListScopeRegisters(-1, 0)")
if len(regs) == 0 {
t.Fatal("Expected string showing registers values, got empty string")
}
t.Logf("GoroutineID: -1, Frame: 0\n%s", regs.String())
regs, err = c.ListScopeRegisters(api.EvalScope{GoroutineID: -1, Frame: 1}, false)
assertNoError(err, t, "ListScopeRegisters(-1, 1)")
if len(regs) == 0 {
t.Fatal("Expected string showing registers values, got empty string")
}
t.Logf("GoroutineID: -1, Frame: 1\n%s", regs.String())
locals, err := c.ListFunctionArgs(api.EvalScope{GoroutineID: -1}, normalLoadConfig)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(locals) != 2 {
t.Fatalf("Expected 2 function args, got %d %#v", len(locals), locals)
}
})
}
func TestClientServer_traceContinue(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("integrationprog", t, func(c service.Client) {
fp := testProgPath(t, "integrationprog")
_, err := c.CreateBreakpoint(&api.Breakpoint{File: fp, Line: 15, Tracepoint: true, Goroutine: true, Stacktrace: 5, Variables: []string{"i"}})
if err != nil {
t.Fatalf("Unexpected error: %v\n", err)
}
count := 0
contChan := c.Continue()
for state := range contChan {
if state.CurrentThread != nil && state.CurrentThread.Breakpoint != nil {
count++
t.Logf("%v", state)
bpi := state.CurrentThread.BreakpointInfo
if bpi.Goroutine == nil {
t.Fatalf("No goroutine information")
}
if len(bpi.Stacktrace) <= 0 {
t.Fatalf("No stacktrace\n")
}
if len(bpi.Variables) != 1 {
t.Fatalf("Wrong number of variables returned: %d", len(bpi.Variables))
}
if bpi.Variables[0].Name != "i" {
t.Fatalf("Wrong variable returned %s", bpi.Variables[0].Name)
}
t.Logf("Variable i is %v", bpi.Variables[0])
n, err := strconv.Atoi(bpi.Variables[0].Value)
if err != nil || n != count-1 {
t.Fatalf("Wrong variable value %q (%v %d)", bpi.Variables[0].Value, err, count)
}
}
if state.Exited {
continue
}
t.Logf("%v", state)
if state.Err != nil {
t.Fatalf("Unexpected error during continue: %v\n", state.Err)
}
}
if count != 3 {
t.Fatalf("Wrong number of continues hit: %d\n", count)
}
})
}
func TestClientServer_traceContinue2(t *testing.T) {
protest.AllowRecording(t)
withTestClient2("integrationprog", t, func(c service.Client) {
bp1, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 1, Tracepoint: true})
if err != nil {
t.Fatalf("Unexpected error: %v\n", err)
}
bp2, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.sayhi", Line: 1, Tracepoint: true})
if err != nil {
t.Fatalf("Unexpected error: %v\n", err)
}
countMain := 0
countSayhi := 0
contChan := c.Continue()
for state := range contChan {
if state.CurrentThread != nil && state.CurrentThread.Breakpoint != nil {
switch state.CurrentThread.Breakpoint.ID {
case bp1.ID:
countMain++
case bp2.ID:
countSayhi++
}
t.Logf("%v", state)
}
if state.Exited {
continue
}
if state.Err != nil {
t.Fatalf("Unexpected error during continue: %v\n", state.Err)
}
}
if countMain != 1 {
t.Fatalf("Wrong number of continues (main.main) hit: %d\n", countMain)
}
if countSayhi != 3 {
t.Fatalf("Wrong number of continues (main.sayhi) hit: %d\n", countSayhi)
}
})
}
func TestClientServer_FindLocations(t *testing.T) {
withTestClient2("locationsprog", t, func(c service.Client) {
someFunctionCallAddr := findLocationHelper(t, c, "locationsprog.go:26", false, 1, 0)[0]
someFunctionLine1 := findLocationHelper(t, c, "locationsprog.go:27", false, 1, 0)[0]
findLocationHelper(t, c, "anotherFunction:1", false, 1, someFunctionLine1)
findLocationHelper(t, c, "main.anotherFunction:1", false, 1, someFunctionLine1)
findLocationHelper(t, c, "anotherFunction", false, 1, someFunctionCallAddr)
findLocationHelper(t, c, "main.anotherFunction", false, 1, someFunctionCallAddr)
findLocationHelper(t, c, fmt.Sprintf("*0x%x", someFunctionCallAddr), false, 1, someFunctionCallAddr)
findLocationHelper(t, c, "sprog.go:26", true, 0, 0)
findLocationHelper(t, c, "String", true, 0, 0)
findLocationHelper(t, c, "main.String", true, 0, 0)
someTypeStringFuncAddr := findLocationHelper(t, c, "locationsprog.go:14", false, 1, 0)[0]
otherTypeStringFuncAddr := findLocationHelper(t, c, "locationsprog.go:18", false, 1, 0)[0]
findLocationHelper(t, c, "SomeType.String", false, 1, someTypeStringFuncAddr)
findLocationHelper(t, c, "(*SomeType).String", false, 1, someTypeStringFuncAddr)
findLocationHelper(t, c, "main.SomeType.String", false, 1, someTypeStringFuncAddr)
findLocationHelper(t, c, "main.(*SomeType).String", false, 1, someTypeStringFuncAddr)
// Issue #275
readfile := findLocationHelper(t, c, "io/ioutil.ReadFile", false, 1, 0)[0]
// Issue #296
findLocationHelper(t, c, "/io/ioutil.ReadFile", false, 1, readfile)
findLocationHelper(t, c, "ioutil.ReadFile", false, 1, readfile)
stringAddrs := findLocationHelper(t, c, "/^main.*Type.*String$/", false, 2, 0)
if otherTypeStringFuncAddr != stringAddrs[0] && otherTypeStringFuncAddr != stringAddrs[1] {
t.Fatalf("Wrong locations returned for \"/.*Type.*String/\", got: %v expected: %v and %v\n", stringAddrs, someTypeStringFuncAddr, otherTypeStringFuncAddr)
}
_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.main", Line: 4, Tracepoint: false})
if err != nil {
t.Fatalf("CreateBreakpoint(): %v\n", err)
}
<-c.Continue()
locationsprog35Addr := findLocationHelper(t, c, "locationsprog.go:35", false, 1, 0)[0]
findLocationHelper(t, c, fmt.Sprintf("%s:35", testProgPath(t, "locationsprog")), false, 1, locationsprog35Addr)
findLocationHelper(t, c, "+1", false, 1, locationsprog35Addr)
findLocationHelper(t, c, "35", false, 1, locationsprog35Addr)
findLocationHelper(t, c, "-1", false, 1, findLocationHelper(t, c, "locationsprog.go:33", false, 1, 0)[0])
findLocationHelper(t, c, `*amap["k"]`, false, 1, findLocationHelper(t, c, `amap["k"]`, false, 1, 0)[0])
locsNoSubst, _ := c.FindLocation(api.EvalScope{GoroutineID: -1}, "_fixtures/locationsprog.go:35", false, nil)
sep := "/"
if strings.Contains(locsNoSubst[0].File, "\\") {
sep = "\\"
}
substRules := [][2]string{[2]string{strings.Replace(locsNoSubst[0].File, "locationsprog.go", "", 1), strings.Replace(locsNoSubst[0].File, "_fixtures"+sep+"locationsprog.go", "nonexistent", 1)}}
t.Logf("substitute rules: %q -> %q", substRules[0][0], substRules[0][1])