Skip to content

Commit

Permalink
add --strace flag and allow trace to read time directly
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 16, 2024
1 parent 6eeff52 commit 5380dab
Show file tree
Hide file tree
Showing 20 changed files with 556 additions and 50 deletions.
2 changes: 1 addition & 1 deletion cmd/xgo/exec_tool/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func parseOptions(args []string, stopAfterFirstArg bool) (*options, error) {
continue
}

ok, err := flag.TryParseFlagsValue([]string{"--debug"}, &debug, &i, args)
ok, err := flag.TryParseFlagsValue([]string{"--debug"}, &debug, nil, &i, args)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions cmd/xgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func handleBuild(cmd string, args []string) error {
withGoroot := opts.withGoroot
dumpIR := opts.dumpIR
dumpAST := opts.dumpAST
stackTrace := opts.stackTrace

if cmdExec && len(remainArgs) == 0 {
return fmt.Errorf("exec requires command")
Expand Down Expand Up @@ -237,7 +238,7 @@ func handleBuild(cmd string, args []string) error {
// gcflags can cause the build cache to invalidate
// so separate them with normal one
buildCacheSuffix := ""
if gcflags != "" {
if len(gcflags) > 0 {
buildCacheSuffix = "-gcflags"
}
buildCacheDir := filepath.Join(instrumentDir, "build-cache"+buildCacheSuffix)
Expand Down Expand Up @@ -382,8 +383,8 @@ func handleBuild(cmd string, args []string) error {
if flagC {
buildCmdArgs = append(buildCmdArgs, "-c")
}
if gcflags != "" {
buildCmdArgs = append(buildCmdArgs, "-gcflags="+gcflags)
for _, f := range gcflags {
buildCmdArgs = append(buildCmdArgs, "-gcflags="+f)
}
if cmdBuild || (cmdTest && flagC) {
// output
Expand Down Expand Up @@ -435,6 +436,9 @@ func handleBuild(cmd string, args []string) error {
if vscodeDebugFile != "" {
execCmd.Env = append(execCmd.Env, "XGO_DEBUG_VSCODE="+vscodeDebugFile+vscodeDebugFileSuffix)
}
if stackTrace != "" {
execCmd.Env = append(execCmd.Env, "XGO_STACK_TRACE="+stackTrace)
}
}
logDebug("command env: %v", execCmd.Env)
execCmd.Stdout = os.Stdout
Expand Down
55 changes: 50 additions & 5 deletions cmd/xgo/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ type options struct {

// recognize go flags as is
// -gcflags
gcflags string
// can repeat
gcflags []string

// xgo test --trace

// --strace, --strace=on, --strace=off
// --stack-stackTrace, --stack-stackTrace=off, --stack-stackTrace=on
// to be used in test mode
stackTrace string

remainArgs []string
}
Expand Down Expand Up @@ -80,7 +88,8 @@ func parseOptions(args []string) (*options, error) {

var noBuildOutput bool

var gcflags string
var gcflags []string
var stackTrace string

var remainArgs []string
nArg := len(args)
Expand Down Expand Up @@ -127,7 +136,9 @@ func parseOptions(args []string) (*options, error) {
},
{
Flags: []string{"-gcflags"},
Value: &gcflags,
Set: func(v string) {
gcflags = append(gcflags, v)
},
},
{
Flags: []string{"--log-debug"},
Expand Down Expand Up @@ -211,6 +222,13 @@ func parseOptions(args []string) (*options, error) {
noSetup = true
continue
}

argVal, ok := parseStackTraceFlag(arg)
if ok {
stackTrace = argVal
continue
}

if isDevelopment && arg == "--debug-with-dlv" {
debugWithDlv = true
continue
Expand All @@ -226,7 +244,7 @@ func parseOptions(args []string) (*options, error) {
}
continue
}
ok, err := flag.TryParseFlagsValue(flagVal.Flags, flagVal.Value, &i, args)
ok, err := flag.TryParseFlagsValue(flagVal.Flags, flagVal.Value, flagVal.Set, &i, args)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -292,8 +310,35 @@ func parseOptions(args []string) (*options, error) {
// default true
syncWithLink: syncWithLink == nil || *syncWithLink,

gcflags: gcflags,
gcflags: gcflags,
stackTrace: stackTrace,

remainArgs: remainArgs,
}, nil
}

func parseStackTraceFlag(arg string) (string, bool) {
var stackTracePrefix string
if strings.HasPrefix(arg, "--strace") {
stackTracePrefix = "--strace"
} else if strings.HasPrefix(arg, "--stack-trace") {
stackTracePrefix = "--stack-trace"
}
if stackTracePrefix == "" {
return "", false
}
if len(arg) == len(stackTracePrefix) {
return "on", true
}
if arg[len(stackTracePrefix)] != '=' {
return "", false
}
val := arg[len(stackTracePrefix)+1:]
if val == "" || val == "on" {
return "on", true
}
if val == "off" {
return "off", true
}
panic(fmt.Errorf("unrecognized value %s: %s, expects on|off", arg, val))
}
9 changes: 9 additions & 0 deletions cmd/xgo/patch/runtime_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ const TestingCallbackDeclarations = `func __xgo_link_get_test_starts() []interfa
return nil
}
`
const TestingEndCallbackDeclarations = `func __xgo_link_get_test_ends() []interface{}{
// link by compiler
return nil
}
`

const TestingStart = `for _,__xgo_on_test_start:=range __xgo_link_get_test_starts(){
(__xgo_on_test_start.(func(*T,func(*T))))(t,fn)
}
`
const TestingEnd = `for _,__xgo_on_test_end:=range __xgo_link_get_test_ends(){
defer (__xgo_on_test_end.(func(*T,func(*T))))(t,fn)
}
`

const RuntimeGetFuncName_Go117_120 = `
func __xgo_get_pc_name_impl(pc uintptr) string {
Expand Down
2 changes: 2 additions & 0 deletions cmd/xgo/patch/runtime_def_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions cmd/xgo/patch_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var testingFilePatch = &FilePatch{
"{",
"\n",
},
Content: patch.TestingCallbackDeclarations,
Content: patch.TestingCallbackDeclarations + patch.TestingEndCallbackDeclarations,
},
{
Mark: "call_testing_callback_v2",
Expand All @@ -43,7 +43,7 @@ var testingFilePatch = &FilePatch{
`t.start = time.Now()`,
"fn(t",
},
Content: patch.TestingStart,
Content: patch.TestingStart + patch.TestingEnd,
},
},
}
Expand Down Expand Up @@ -151,10 +151,10 @@ func patchRuntimeProc(goroot string, goVersion *goinfo.GoVersion) error {
procDecl := `func newproc(fn`
newProc := `newg := newproc1(fn, gp, pc)`
if goVersion.Major == 1 && goVersion.Minor <= 17 {
// siz: to avoid typo check
const siz = "s" + "i" + "z"
procDecl = `func newproc(` + siz + ` int32`
newProc = `newg := newproc1(fn, argp, ` + siz + `, gp, pc)`
// to avoid typo check
const size = "s" + "i" + "z"
procDecl = `func newproc(` + size + ` int32`
newProc = `newg := newproc1(fn, argp, ` + size + `, gp, pc)`
}

// see https://github.com/xhd2015/xgo/issues/67
Expand Down
6 changes: 3 additions & 3 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import "fmt"

const VERSION = "1.0.24"
const REVISION = "56315bee3eb7415a4c3d88d4f26bd79c8333a434+1"
const NUMBER = 185
const VERSION = "1.0.25"
const REVISION = "6eeff524a454a6032a2294b47d1dc1c892b5545f+1"
const NUMBER = 186

func getRevision() string {
revSuffix := ""
Expand Down
1 change: 1 addition & 0 deletions patch/ctxt/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
const XgoModule = "github.com/xhd2015/xgo"
const XgoRuntimePkg = XgoModule + "/runtime"
const XgoRuntimeCorePkg = XgoModule + "/runtime/core"
const XgoRuntimeTracePkg = XgoModule + "/runtime/trace"

var XgoMainModule = os.Getenv("XGO_MAIN_MODULE")
var XgoCompilePkgDataDir = os.Getenv("XGO_COMPILE_PKG_DATA_DIR")
Expand Down
5 changes: 4 additions & 1 deletion patch/link_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const xgoRuntimeTrapPkg = xgoRuntimePkgPrefix + "trap"

// accepts interface{} as argument
const xgoOnTestStart = "__xgo_on_test_start"
const xgoOnTestEnd = "__xgo_on_test_end"

const XgoLinkSetTrap = "__xgo_link_set_trap"
const XgoLinkSetTrapVar = "__xgo_link_set_trap_var"
Expand All @@ -39,7 +40,9 @@ var linkMap = map[string]string{
"__xgo_link_on_gonewproc": "__xgo_on_gonewproc",
"__xgo_link_on_goexit": "__xgo_on_goexit",
"__xgo_link_on_test_start": xgoOnTestStart,
"__xgo_link_on_test_end": xgoOnTestEnd,
"__xgo_link_get_test_starts": "__xgo_get_test_starts",
"__xgo_link_get_test_ends": "__xgo_get_test_ends",
"__xgo_link_retrieve_all_funcs_and_clear": "__xgo_retrieve_all_funcs_and_clear",
"__xgo_link_peek_panic": "__xgo_peek_panic",
"__xgo_link_mem_equal": "__xgo_mem_equal",
Expand Down Expand Up @@ -130,7 +133,7 @@ func replaceWithRuntimeCall(fn *ir.Func, name string) {
resNames := getTypeNames(results)
fnPos := fn.Pos()

if needConvertArg && name == xgoOnTestStart {
if needConvertArg && (name == xgoOnTestStart || name == xgoOnTestEnd) {
for i, p := range paramNames {
paramNames[i] = convToEFace(fnPos, p, p.(*ir.Name).Type(), false)
}
Expand Down
10 changes: 10 additions & 0 deletions patch/syntax/call_expr_go1.17_18_19.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ import "cmd/compile/internal/syntax"
func (ctx *BlockContext) traverseCallStmtCallExpr(node *syntax.CallExpr, globaleNames map[string]*DeclInfo, imports map[string]string) *syntax.CallExpr {
return ctx.traverseCallExpr(node, globaleNames, imports)
}

func copyCallExpr(expr *syntax.CallExpr) *syntax.CallExpr {
if expr == nil {
return nil
}
x := *expr
x.Fun = copyExpr(expr.Fun)
x.ArgList = copyExprs(expr.ArgList)
return &x
}
4 changes: 4 additions & 0 deletions patch/syntax/call_expr_go1.20.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ import "cmd/compile/internal/syntax"
func (ctx *BlockContext) traverseCallStmtCallExpr(node syntax.Expr, globaleNames map[string]*DeclInfo, imports map[string]string) syntax.Expr {
return ctx.traverseExpr(node, globaleNames, imports)
}

func copyCallExpr(expr syntax.Expr) syntax.Expr {
return copyExpr(expr)
}
Loading

0 comments on commit 5380dab

Please sign in to comment.