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
|
package service_test
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/go-delve/delve/service"
"github.com/go-delve/delve/service/api"
)
func assertNoError(err error, t *testing.T, s string) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fname := filepath.Base(file)
t.Fatalf("failed assertion at %s:%d: %s - %s\n", fname, line, s, err)
}
}
func assertError(err error, t *testing.T, s string) {
if err == nil {
_, file, line, _ := runtime.Caller(1)
fname := filepath.Base(file)
t.Fatalf("failed assertion at %s:%d: %s (no error)\n", fname, line, s)
}
if strings.Contains(err.Error(), "Internal debugger error") {
_, file, line, _ := runtime.Caller(1)
fname := filepath.Base(file)
t.Fatalf("failed assertion at %s:%d: %s internal debugger error: %v\n", fname, line, s, err)
}
}
func init() {
runtime.GOMAXPROCS(2)
}
type nextTest struct {
begin, end int
}
func testProgPath(t *testing.T, name string) string {
fp, err := filepath.Abs(fmt.Sprintf("_fixtures/%s.go", name))
if err != nil {
t.Fatal(err)
}
if _, err := os.Stat(fp); err != nil {
fp, err = filepath.Abs(fmt.Sprintf("../../_fixtures/%s.go", name))
if err != nil {
t.Fatal(err)
}
}
sympath, err := filepath.EvalSymlinks(fp)
if err == nil {
fp = strings.ReplaceAll(sympath, "\\", "/")
}
return fp
}
type BreakpointLister interface {
ListBreakpoints() ([]*api.Breakpoint, error)
}
func countBreakpoints(t *testing.T, c service.Client) int {
bps, err := c.ListBreakpoints(false)
assertNoError(err, t, "ListBreakpoints()")
bpcount := 0
for _, bp := range bps {
if bp.ID >= 0 {
bpcount++
}
}
return bpcount
}
type locationFinder1 interface {
FindLocation(api.EvalScope, string) ([]api.Location, error)
}
type locationFinder2 interface {
FindLocation(api.EvalScope, string, bool, [][2]string) ([]api.Location, string, error)
}
func findLocationHelper(t *testing.T, c interface{}, loc string, shouldErr bool, count int, checkAddr uint64) []uint64 {
var locs []api.Location
var err error
switch c := c.(type) {
case locationFinder1:
locs, err = c.FindLocation(api.EvalScope{GoroutineID: -1}, loc)
case locationFinder2:
locs, _, err = c.FindLocation(api.EvalScope{GoroutineID: -1}, loc, false, nil)
default:
t.Errorf("unexpected type %T passed to findLocationHelper", c)
}
t.Logf("FindLocation(\"%s\") → %v\n", loc, locs)
if shouldErr {
if err == nil {
t.Fatalf("Resolving location <%s> didn't return an error: %v", loc, locs)
}
} else {
if err != nil {
t.Fatalf("Error resolving location <%s>: %v", loc, err)
}
}
if (count >= 0) && (len(locs) != count) {
t.Fatalf("Wrong number of breakpoints returned for location <%s> (got %d, expected %d)", loc, len(locs), count)
}
if checkAddr != 0 && checkAddr != locs[0].PC {
t.Fatalf("Wrong address returned for location <%s> (got %#x, expected %#x)", loc, locs[0].PC, checkAddr)
}
addrs := make([]uint64, len(locs))
for i := range locs {
addrs[i] = locs[i].PC
}
return addrs
}
func getCurinstr(d3 api.AsmInstructions) *api.AsmInstruction {
for i := range d3 {
if d3[i].AtPC {
return &d3[i]
}
}
return nil
}
|