Skip to content

Commit

Permalink
make --strace and --trap-stdlib persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed May 29, 2024
1 parent f52fb37 commit ff558f4
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 42 deletions.
4 changes: 2 additions & 2 deletions cmd/xgo/runtime_gen/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.37"
const REVISION = "da25b0b8838244b76b23707349c5a2b343abc5d9+1"
const NUMBER = 242
const REVISION = "f52fb37283a2f6c877d110627da71df63638a916+1"
const NUMBER = 243

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
13 changes: 7 additions & 6 deletions cmd/xgo/runtime_gen/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import (
var stackMap sync.Map // uintptr(goroutine) -> *Root
var testInfoMapping sync.Map // uintptr(goroutine) -> *testInfo

// persist the --strace flag when invoking xgo test
// stack trace options:
//
// on: automatically collect when test starts and ends
var xgoStackTrace = os.Getenv("XGO_STACK_TRACE")
const __xgo_injected_StraceFlag = ""

// options:
//
// true: stdlib is by default allowed
// TODO: use compiler inserted flag instead of env
var xgoStdlibTrapDefaultAllow = os.Getenv("XGO_STD_LIB_TRAP_DEFAULT_ALLOW")
var skipStdlibTraceByDefault = xgoStdlibTrapDefaultAllow == "true"
// true: stdlib is by default allowed
const __xgo_injected_StdlibTrapDefaultAllow = ""

var skipStdlibTraceByDefault = __xgo_injected_StdlibTrapDefaultAllow == "true"

type testInfo struct {
name string
Expand All @@ -49,7 +50,7 @@ func init() {
name: name,
}
testInfoMapping.LoadOrStore(key, tInfo)
if xgoStackTrace == "on" {
if __xgo_injected_StraceFlag == "on" {
tInfo.onFinish = Begin()
}
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.37"
const REVISION = "da25b0b8838244b76b23707349c5a2b343abc5d9+1"
const NUMBER = 242
const REVISION = "f52fb37283a2f6c877d110627da71df63638a916+1"
const NUMBER = 243

func getRevision() string {
revSuffix := ""
Expand Down
93 changes: 76 additions & 17 deletions patch/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const XGO_VERSION = "XGO_VERSION"
const XGO_REVISION = "XGO_REVISION"
const XGO_NUMBER = "XGO_NUMBER"

// --strace
const XGO_STACK_TRACE = "XGO_STACK_TRACE"
const XGO_STD_LIB_TRAP_DEFAULT_ALLOW = "XGO_STD_LIB_TRAP_DEFAULT_ALLOW"
const straceFlagConstName = "__xgo_injected_StraceFlag"
const trapStdlibFlagConstName = "__xgo_injected_StdlibTrapDefaultAllow"

// this link function is considered safe as we do not allow user
// to define such one,there will no abuse
const XgoLinkGeneratedRegisterFunc = "__xgo_link_generated_register_func"
Expand All @@ -39,7 +45,7 @@ func init() {

func AfterFilesParsed(fileList []*syntax.File, addFile func(name string, r io.Reader) *syntax.File) {
debugSyntax(fileList)
patchVersions(fileList)
injectXgoFlags(fileList)
fillFuncArgResNames(fileList)
registerAndTrapFuncs(fileList, addFile)
}
Expand Down Expand Up @@ -303,11 +309,38 @@ func registerAndTrapFuncs(fileList []*syntax.File, addFile func(name string, r i
}
}

func patchVersions(fileList []*syntax.File) {
func injectXgoFlags(fileList []*syntax.File) {
pkgPath := xgo_ctxt.GetPkgPath()
if pkgPath != xgo_ctxt.XgoRuntimeCorePkg {
return
switch pkgPath {
case xgo_ctxt.XgoRuntimeCorePkg:
patchXgoRuntimeCoreVersions(fileList)
case xgo_ctxt.XgoRuntimeTracePkg:
injectXgoStraceFlag(fileList)
}
}

func findFile(fileList []*syntax.File, name string) *syntax.File {
n := len(name)
for _, file := range fileList {
relFileName := file.Pos().RelFilename()
if !strings.HasSuffix(relFileName, name) {
continue
}
fn := len(relFileName)
if fn == n {
return file
}
c := relFileName[fn-n-1]

// must be a separator
if c == '/' || c == '\\' || c == filepath.Separator {
return file
}
}
return nil
}

func patchXgoRuntimeCoreVersions(fileList []*syntax.File) {
version := os.Getenv(XGO_TOOLCHAIN_VERSION)
if version == "" {
return
Expand All @@ -322,22 +355,11 @@ func patchVersions(fileList []*syntax.File) {
return
}

var versionFile *syntax.File
for _, file := range fileList {
if strings.HasSuffix(file.Pos().RelFilename(), "version.go") {
versionFile = file
break
}
}
versionFile := findFile(fileList, "version.go")
if versionFile == nil {
return
}
for _, decl := range versionFile.DeclList {
constDecl, ok := decl.(*syntax.ConstDecl)
if !ok {
continue
}

forEachConst(versionFile.DeclList, func(constDecl *syntax.ConstDecl) bool {
for _, name := range constDecl.NameList {
switch name.Value {
case XGO_VERSION:
Expand All @@ -348,7 +370,44 @@ func patchVersions(fileList []*syntax.File) {
constDecl.Values = newIntLit(int(versionNum))
}
}
return false
})
}
func forEachConst(declList []syntax.Decl, fn func(constDecl *syntax.ConstDecl) bool) {
for _, decl := range declList {
constDecl, ok := decl.(*syntax.ConstDecl)
if !ok {
continue
}
if fn(constDecl) {
return
}
}
}

func injectXgoStraceFlag(fileList []*syntax.File) {
straceFlag := os.Getenv(XGO_STACK_TRACE)
trapStdlibFlag := os.Getenv(XGO_STD_LIB_TRAP_DEFAULT_ALLOW)
if straceFlag == "" && trapStdlibFlag == "" {
return
}

traceFile := findFile(fileList, "trace.go")
if traceFile == nil {
return
}

forEachConst(traceFile.DeclList, func(constDecl *syntax.ConstDecl) bool {
for _, name := range constDecl.NameList {
switch name.Value {
case straceFlagConstName:
constDecl.Values = newStringLit(straceFlag)
case trapStdlibFlagConstName:
constDecl.Values = newStringLit(trapStdlibFlag)
}
}
return false
})
}

func getFileIndexMapping(files []*syntax.File) map[*syntax.File]int {
Expand Down
4 changes: 2 additions & 2 deletions runtime/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const VERSION = "1.0.37"
const REVISION = "da25b0b8838244b76b23707349c5a2b343abc5d9+1"
const NUMBER = 242
const REVISION = "f52fb37283a2f6c877d110627da71df63638a916+1"
const NUMBER = 243

// these fields will be filled by compiler
const XGO_VERSION = ""
Expand Down
10 changes: 3 additions & 7 deletions runtime/test/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@

package debug

const DefaultDateLayout = "2006-01-02 15:04:05.000Z"
import "testing"

func chainedConst() {
wrap(DefaultDateLayout).Min(nil)
}

func wrap(e string) interface{ Min(t interface{}) } {
return nil
func TestHello(t *testing.T) {
t.Logf("hello world!")
}
7 changes: 7 additions & 0 deletions runtime/test/hello_world/hello_world_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "testing"

func TestHello(t *testing.T) {
t.Logf("hello world!")
}
13 changes: 7 additions & 6 deletions runtime/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import (
var stackMap sync.Map // uintptr(goroutine) -> *Root
var testInfoMapping sync.Map // uintptr(goroutine) -> *testInfo

// persist the --strace flag when invoking xgo test
// stack trace options:
//
// on: automatically collect when test starts and ends
var xgoStackTrace = os.Getenv("XGO_STACK_TRACE")
const __xgo_injected_StraceFlag = ""

// options:
//
// true: stdlib is by default allowed
// TODO: use compiler inserted flag instead of env
var xgoStdlibTrapDefaultAllow = os.Getenv("XGO_STD_LIB_TRAP_DEFAULT_ALLOW")
var skipStdlibTraceByDefault = xgoStdlibTrapDefaultAllow == "true"
// true: stdlib is by default allowed
const __xgo_injected_StdlibTrapDefaultAllow = ""

var skipStdlibTraceByDefault = __xgo_injected_StdlibTrapDefaultAllow == "true"

type testInfo struct {
name string
Expand All @@ -49,7 +50,7 @@ func init() {
name: name,
}
testInfoMapping.LoadOrStore(key, tInfo)
if xgoStackTrace == "on" {
if __xgo_injected_StraceFlag == "on" {
tInfo.onFinish = Begin()
}
})
Expand Down

0 comments on commit ff558f4

Please sign in to comment.