Skip to content

Commit 26f967c

Browse files
committed
enable --trap-all if has call to trap.AddInterceptor
1 parent 09acbf9 commit 26f967c

File tree

9 files changed

+65
-22
lines changed

9 files changed

+65
-22
lines changed

cmd/xgo/instrument.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
const MAX_FILE_SIZE = 1 * 1024 * 1024
4141

4242
// goroot is critical for stdlib
43-
func instrumentUserCode(goroot string, projectDir string, projectRoot string, goVersion *goinfo.GoVersion, xgoSrc string, mod string, modfile string, mainModule string, xgoRuntimeModuleDir string, mayHaveCover bool, overlayFS overlay.Overlay, includeTest bool, rules []Rule, trapPkgs []string, trapAll bool, collectTestTrace bool, collectTestTraceDir string, goFlag bool, triedUpgrade bool) error {
43+
func instrumentUserCode(goroot string, projectDir string, projectRoot string, goVersion *goinfo.GoVersion, xgoSrc string, mod string, modfile string, mainModule string, xgoRuntimeModuleDir string, mayHaveCover bool, overlayFS overlay.Overlay, includeTest bool, rules []Rule, trapPkgs []string, trapAll string, collectTestTrace bool, collectTestTraceDir string, goFlag bool, triedUpgrade bool) error {
4444
logDebug("instrumentUserSpace: mod=%s, modfile=%s, xgoRuntimeModuleDir=%s, includeTest=%v, collectTestTrace=%v", mod, modfile, xgoRuntimeModuleDir, includeTest, collectTestTrace)
4545
if mod == "" {
4646
// check vendor dir
@@ -189,13 +189,14 @@ func instrumentUserCode(goroot string, projectDir string, projectRoot string, go
189189
mainPkgs := pkgs.Filter(func(pkg *edit.Package) bool {
190190
return pkg.Main && pkg.AllowInstrument
191191
})
192-
var recorder resolve.Recorder
193192
for _, pkg := range mainPkgs {
194193
resolve.CollectDecls(pkg)
195194
}
196195

197196
traverseBegin := time.Now()
198197
logDebug("traverse: len(mainPkgs)=%d", len(mainPkgs))
198+
199+
var recorder resolve.Recorder
199200
err = resolve.Traverse(reg, mainPkgs, &recorder)
200201
if err != nil {
201202
return err
@@ -208,7 +209,10 @@ func instrumentUserCode(goroot string, projectDir string, projectRoot string, go
208209
return err
209210
}
210211

211-
logDebug("start instrumentFuncTrap: len(packages)=%d", len(pkgs.Packages))
212+
// ""->default
213+
needTrapAll := trapAll == "true" || (trapAll == "" && recorder.HasTrapInterceptorRef)
214+
215+
logDebug("start instrumentFuncTrap: len(packages)=%d, needTrapAll=%v", len(pkgs.Packages), needTrapAll)
212216
for _, pkg := range pkgs.Packages {
213217
if !pkg.AllowInstrument {
214218
continue
@@ -217,7 +221,7 @@ func instrumentUserCode(goroot string, projectDir string, projectRoot string, go
217221
cfg := config.GetPkgConfig(pkgPath)
218222
var defaultAllow bool
219223
if !pkg.LoadPackage.GoPackage.Standard {
220-
if trapAll || pkg.Initial {
224+
if needTrapAll || pkg.Initial {
221225
defaultAllow = true
222226
}
223227
}

cmd/xgo/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ func handleBuild(cmd string, args []string) error {
445445
buildCacheSuffix += "-strace_" + v
446446
}
447447
// see https://github.com/xhd2015/xgo/issues/311
448-
if trapAll {
448+
if trapAll != "false" {
449449
buildCacheSuffix += "-trap-all"
450450
}
451451
if stackTraceDir != "" && stackTraceDir != "." && stackTraceDir != "./" {

cmd/xgo/option.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ type options struct {
6464
// --trap-stdlib
6565
trapStdlib bool
6666
// --trap-all
67-
trapAll bool
67+
// "": default, "true"->true, "false"->false
68+
trapAll string
6869

6970
// --trap pkg
7071
// where pkg cannot be runtime
@@ -149,7 +150,7 @@ func parseOptions(cmd string, args []string) (*options, error) {
149150
var stackTraceDir string
150151
var straceSnapshotMainModuleDefault string
151152
var trapStdlib bool
152-
var trapAll bool
153+
var trapAll string
153154
var trap []string
154155

155156
var remainArgs []string
@@ -439,9 +440,9 @@ func parseOptions(cmd string, args []string) (*options, error) {
439440
trapAllFlag, trapAllVal := flag.TrySingleFlag([]string{"--trap-all"}, arg)
440441
if trapAllFlag != "" {
441442
if trapAllVal == "" || trapAllVal == "true" {
442-
trapAll = true
443+
trapAll = "true"
443444
} else {
444-
trapAll = false
445+
trapAll = "false"
445446
}
446447
continue
447448
}

cmd/xgo/version.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import "fmt"
77
// VERSION is manually updated when needed a new tag
88
// if you did not install git hooks, you can manually update them
99
const VERSION = "1.1.1"
10-
const REVISION = "0d3787edd9e0b47f8b62e3e445b2461d753d17a5+1"
11-
const NUMBER = 404
10+
const REVISION = "09acbf99d8ada59af1648fe155931d901f3614d5+1"
11+
const NUMBER = 405
1212

1313
// The CORE_REVISION is cache related.
1414
// because xgo contains multiple packages, not every

instrument/resolve/record.go

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ func (c *Scope) recordTrap(sel *ast.SelectorExpr) bool {
3131
if name == "InfoFunc" || name == "InfoVar" {
3232
return pkgPath == constants.RUNTIME_FUNCTAB_PKG
3333
}
34+
if name == "AddInterceptor" {
35+
if pkgPath == constants.RUNTIME_TRAP_PKG {
36+
c.Global.Recorder.HasTrapInterceptorRef = true
37+
}
38+
return false
39+
}
3440
return false
3541
}
3642

instrument/resolve/scope.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import (
1717
type PkgScopeNames map[string]*edit.Decl
1818

1919
type Recorder struct {
20-
Pkgs map[string]*PkgRecorder
20+
// has called `trap.AddInterceptor`?
21+
// if so, we need to enable trapAll mode
22+
// as mentioned in https://github.com/xhd2015/xgo/issues/308#issuecomment-2800327536
23+
HasTrapInterceptorRef bool
24+
Pkgs map[string]*PkgRecorder
2125
}
2226

2327
func (c *Recorder) GetOrInit(pkgPath string) *PkgRecorder {

runtime/test/build/overlay_build_cache_error_with_go/gomod_test.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/xhd2015/xgo/runtime/test/build/util"
1212
"github.com/xhd2015/xgo/support/cmd"
13+
"github.com/xhd2015/xgo/support/goinfo"
1314
)
1415

1516
func TestGoModNonOverlayFirstShouldError(t *testing.T) {
@@ -33,16 +34,43 @@ func TestGoModNonOverlayFirstShouldError(t *testing.T) {
3334
// this should error
3435
var errOut bytes.Buffer
3536
afterErr := cmd.Debug().Stdout(&errOut).Stderr(&errOut).Env([]string{"GOCACHE=" + gocache, "GO_BYPASS_XGO=true"}).Run("go", "test", "-v", "-overlay", overlayFile, "./overlay_test_with_gomod")
36-
if afterErr == nil {
37-
t.Errorf("expect cache+overlay combination error, actual nil")
37+
38+
// 17, 18 passes, only after go1.19 this issue happens
39+
minor, err := getGo1MinorVersion()
40+
if err != nil {
41+
t.Fatalf("failed to get go minor version: %v", err)
3842
}
39-
expectContains := "could not import runtime (open : no such file or directory)"
40-
errOutStr := errOut.String()
41-
if !strings.Contains(errOutStr, expectContains) {
42-
t.Errorf("expect containing %q, actual none", expectContains)
43-
t.Logf("DEBUG: %s", errOutStr)
44-
return
43+
if minor >= 19 {
44+
if afterErr == nil {
45+
t.Errorf("expect cache+overlay combination error, actual nil")
46+
}
47+
expectContains := "could not import runtime (open : no such file or directory)"
48+
errOutStr := errOut.String()
49+
if !strings.Contains(errOutStr, expectContains) {
50+
t.Errorf("expect containing %q, actual none", expectContains)
51+
t.Logf("DEBUG: %s", errOutStr)
52+
return
53+
}
54+
} else {
55+
if afterErr != nil {
56+
t.Errorf("expect cache+overlay under go1.19 success, actual error: %v", afterErr)
57+
}
58+
}
59+
}
60+
61+
func getGo1MinorVersion() (int, error) {
62+
rv, err := goinfo.GetGoVersionOutput("go")
63+
if err != nil {
64+
return 0, err
65+
}
66+
goV, err := goinfo.ParseGoVersion(rv)
67+
if err != nil {
68+
return 0, err
69+
}
70+
if goV.Major != 1 {
71+
return 0, fmt.Errorf("unrecognized go major: %s", rv)
4572
}
73+
return goV.Minor, nil
4674
}
4775

4876
func TestGoModNonOverlayLaterShouldSucceed(t *testing.T) {

runtime/test/test-config.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
flags: --trap-stdlib
1+
flags: --trap-stdlib --trap-all=false
22
args: ./bugs/...
33
args: ./core/...
44
args: ./functab/...

script/run-test/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,8 @@ func doRunTest(goroot string, usePlainGo bool, dir string, args []string, tests
714714
}
715715
testArgs = testArgs[:i]
716716
}
717-
// debug
718717

718+
// debug
719719
var binary string
720720
if debug {
721721
fmt.Printf("testArgs: %v\n", testArgs)

0 commit comments

Comments
 (0)