Skip to content

Commit

Permalink
allow main module snapshot trace by default
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Nov 9, 2024
1 parent 38209d9 commit fc8ddef
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 14 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Prepare typo config
run: |
# Don't correct the word "Incrimental"
echo -ne '[files]\nextend-exclude = ["**/vendir/**/*"]\n[default.extend-words]\nIncrimental = "Incrimental"\n' > /tmp/typo-config.toml
cat /tmp/typo-config.toml
- name: Check spelling of files
uses: crate-ci/typos@master
continue-on-error: false
with:
files: ./
config: /tmp/typo-config.toml
isolated: true

- name: Set up Go1.17
uses: actions/setup-go@v4
Expand Down
1 change: 1 addition & 0 deletions cmd/xgo/exec_tool/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func getDebugEnvList(xgoCompilerEnableEnv string) [][2]string {
// strace
{XGO_STACK_TRACE, os.Getenv(XGO_STACK_TRACE)},
{XGO_STACK_TRACE_DIR, os.Getenv(XGO_STACK_TRACE_DIR)},
{XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT, os.Getenv(XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT)},

{XGO_STD_LIB_TRAP_DEFAULT_ALLOW, os.Getenv(XGO_STD_LIB_TRAP_DEFAULT_ALLOW)},
{XGO_DEBUG_COMPILE_PKG, os.Getenv(XGO_DEBUG_COMPILE_PKG)},
Expand Down
3 changes: 3 additions & 0 deletions cmd/xgo/exec_tool/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const XGO_STACK_TRACE = "XGO_STACK_TRACE"
// --strace-dir
const XGO_STACK_TRACE_DIR = "XGO_STACK_TRACE_DIR"

// --strace-snapshot-main-module-default
const XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT = "XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT"

const XGO_COMPILE_PKG_DATA_DIR = "XGO_COMPILE_PKG_DATA_DIR"

const XGO_STD_LIB_TRAP_DEFAULT_ALLOW = "XGO_STD_LIB_TRAP_DEFAULT_ALLOW"
Expand Down
6 changes: 6 additions & 0 deletions cmd/xgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func handleBuild(cmd string, args []string) error {
dumpAST := opts.dumpAST
stackTrace := opts.stackTrace
stackTraceDir := opts.stackTraceDir
straceSnapshotMainModuleDefault := opts.straceSnapshotMainModuleDefault
trapStdlib := opts.trapStdlib

if cmdExec && len(remainArgs) == 0 {
Expand Down Expand Up @@ -293,7 +294,9 @@ func handleBuild(cmd string, args []string) error {
h.Write(optionsFromFileContent)
buildCacheSuffix += "-" + hex.EncodeToString(h.Sum(nil))
}
var enableStackTrace bool
if stackTrace == "on" || stackTrace == "true" {
enableStackTrace = true
v := stackTrace
if v == "true" {
v = "on"
Expand Down Expand Up @@ -660,6 +663,9 @@ func handleBuild(cmd string, args []string) error {
if stackTraceDir != "" {
execCmd.Env = append(execCmd.Env, exec_tool.XGO_STACK_TRACE_DIR+"="+stackTraceDir)
}
if enableStackTrace && straceSnapshotMainModuleDefault != "" {
execCmd.Env = append(execCmd.Env, exec_tool.XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT+"="+straceSnapshotMainModuleDefault)
}

// trap stdlib
var trapStdlibEnv string
Expand Down
23 changes: 16 additions & 7 deletions cmd/xgo/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type options struct {
stackTrace string
// --strace-dir
stackTraceDir string
// --strace-snapshot-main-module-default
straceSnapshotMainModuleDefault string

remainArgs []string

Expand Down Expand Up @@ -129,6 +131,7 @@ func parseOptions(cmd string, args []string) (*options, error) {
var modfile string
var stackTrace string
var stackTraceDir string
var straceSnapshotMainModuleDefault string
var trapStdlib bool

var remainArgs []string
Expand Down Expand Up @@ -358,6 +361,11 @@ func parseOptions(cmd string, args []string) (*options, error) {
stackTrace = argVal
continue
}
stackTraceMainModuleDefaultFlag, val := flag.TrySingleFlag([]string{"--strace-snapshot-main-module-default"}, arg)
if stackTraceMainModuleDefaultFlag != "" {
straceSnapshotMainModuleDefault = val
continue
}

// supported flag: --trap-stdlib, --trap-stdlib=false, --trap-stdlib=true
trapStdlibFlag, trapStdlibVal := flag.TrySingleFlag([]string{"--trap-stdlib"}, arg)
Expand Down Expand Up @@ -458,13 +466,14 @@ func parseOptions(cmd string, args []string) (*options, error) {
// default true
syncWithLink: syncWithLink == nil || *syncWithLink,

mod: mod,
gcflags: gcflags,
overlay: overlay,
modfile: modfile,
stackTrace: stackTrace,
stackTraceDir: stackTraceDir,
trapStdlib: trapStdlib,
mod: mod,
gcflags: gcflags,
overlay: overlay,
modfile: modfile,
stackTrace: stackTrace,
stackTraceDir: stackTraceDir,
straceSnapshotMainModuleDefault: straceSnapshotMainModuleDefault,
trapStdlib: trapStdlib,

remainArgs: remainArgs,
testArgs: testArgs,
Expand Down
22 changes: 22 additions & 0 deletions cmd/xgo/runtime_gen/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"os"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand All @@ -27,7 +29,18 @@ type testInfo struct {
onFinish func()
}

var effectMainModule string

func init() {
if flags.MAIN_MODULE != "" {
// fmt.Fprintf(os.Stderr, "DEBUG main module from flags: %s\n", flags.MAIN_MODULE)
effectMainModule = flags.MAIN_MODULE
} else {
buildInfo, _ := debug.ReadBuildInfo()
if buildInfo != nil {
effectMainModule = buildInfo.Main.Path
}
}
__xgo_link_on_test_start(func(t *testing.T, fn func(t *testing.T)) {
name := t.Name()
if name == "" {
Expand Down Expand Up @@ -268,6 +281,15 @@ func handleTracePre(ctx context.Context, f *core.FuncInfo, args core.Object, res
break
}
}

// check if allow main module to be defaultly snapshoted
if !anySnapshot && flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT != "false" && effectMainModule != "" && strings.HasPrefix(f.Pkg, effectMainModule) {
// main_module or main_module/*
if len(f.Pkg) == len(effectMainModule) || f.Pkg[len(effectMainModule)] == '/' {
// fmt.Fprintf(os.Stderr, "DEBUG main module snapshot: %s of %s\n", f.Pkg, effectMainModule)
anySnapshot = true
}
}
if anySnapshot {
stack.Snapshot = true
stack.Args = premarshal(stack.Args)
Expand Down
27 changes: 27 additions & 0 deletions cmd/xgo/runtime_gen/trap/flags/flags.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package flags

// to inject these flags, see patch/syntax/syntax.go, search for flag_MAIN_MODULE
// this package exists to make flags passed to xgo to be persisted in building and running.

// flag: none
// env: XGO_MAIN_MODULE
// description:
//
// auto detected by xgo in the beginning of building.
// can be used prior to ask runtime/debug's info:
// var mainModulePath = runtime/debug.ReadBuildInfo().Main.Path
const MAIN_MODULE = ""

// flag: --strace
// env: XGO_STACK_TRACE
// description:
Expand All @@ -21,6 +33,21 @@ const STRACE = ""
// directory, default current dir
const STRACE_DIR = ""

// flag: --strace-snapshot-main-module
// env: XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT
// description:
//
// collecting main module's trace in snapshot mode,
// while other's are non snapshot mode
//
// snapshot mode: args are serialized before executing
// the function, and results are serilaized after return.
// this is useful if an object will be modified in later
// process.
//
// values: true or false
const STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT = ""

// flag: --trap-stdlib
// env: XGO_STD_LIB_TRAP_DEFAULT_ALLOW
// description: if true, stdlib trap is by default allowed
Expand Down
12 changes: 9 additions & 3 deletions cmd/xgo/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type importResult struct {
var runtimeGenFS embed.FS

// TODO: may apply tags
// importRuntimeDep detect if the target package already
// has github.com/xhd2015/xgo/runtime as dependency,
// if not, dynamically modify the go.mod to include that,
// and add a blank import in the main package
func importRuntimeDep(test bool, goroot string, goBinary string, goVersion *goinfo.GoVersion, absModFile string, xgoSrc string, projectDir string, modRootRel []string, mainModule string, mod string, args []string) (*importResult, error) {
if mainModule == "" {
// only work with module
Expand Down Expand Up @@ -64,7 +68,6 @@ func importRuntimeDep(test bool, goroot string, goBinary string, goVersion *goin
return nil, err
}

pkgArgs := getPkgArgs(args)
tmpRoot, tmpProjectDir, err := createWorkDir(projectRoot)
if err != nil {
return nil, err
Expand All @@ -84,6 +87,7 @@ func importRuntimeDep(test bool, goroot string, goBinary string, goVersion *goin
}
}

pkgArgs := getPkgArgs(args)
fileReplace, err := addBlankImports(goroot, goBinary, projectDir, pkgArgs, test, tmpProjectDir)
if err != nil {
return nil, err
Expand Down Expand Up @@ -171,8 +175,10 @@ type Overlay struct {

type dependencyInfo struct {
modReplace map[string]string
mod string
modfile string // alternative go.mod
// the -mod flag
mod string
// the -modfile flag
modfile string // alternative go.mod
}

func createWorkDir(projectRoot string) (tmpRoot string, tmpProjectDir string, err error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import "fmt"
// VERSION is manually updated when needed a new tag
// see also runtime/core/version.go
const VERSION = "1.0.51"
const REVISION = "f43d1801162be8396dd5b73a27b529253a121a88+1"
const NUMBER = 319
const REVISION = "38209d904dbe681a458641ad5e7f1c408c8a3626+1"
const NUMBER = 320

// the matching runtime/core's version
// manually updated
Expand Down
11 changes: 9 additions & 2 deletions doc/test-explorer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ An example config:
"env":{
"TEST_WITH_XGO":"true"
},
"flags":["-p=12"],
"flags":["-p=4"],
"args":["--my-program-env","TEST"],
"mock_rules":[{
"stdlib": true,
Expand Down Expand Up @@ -149,10 +149,17 @@ Definition:
Definition:
```json
{
"disabled": true
"disabled": true|false,
"diff_with": "origin/master",
"include": [...],
"exclude": [...]
}
```

By default, if `coverage` is missing or `null`, then coverage is enabled.

If set `diff_with` to `none`, then incremental coverage will be disabled.

`include` and `exclude` specify which files will be included or excluded in coverage display.

Setting `"coverage": false` or `"coverage":{"disabled": true}` will disable it.
11 changes: 11 additions & 0 deletions patch/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,27 @@ const XGO_VERSION = "XGO_VERSION"
const XGO_REVISION = "XGO_REVISION"
const XGO_NUMBER = "XGO_NUMBER"

const XGO_MAIN_MODULE = "XGO_MAIN_MODULE"

// --strace
const XGO_STACK_TRACE = "XGO_STACK_TRACE"
const XGO_STACK_TRACE_DIR = "XGO_STACK_TRACE_DIR"
const XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT = "XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT"
const XGO_STD_LIB_TRAP_DEFAULT_ALLOW = "XGO_STD_LIB_TRAP_DEFAULT_ALLOW"

// Deprecated: use flag_STRACE,.. instead
const straceFlagConstName = "__xgo_injected_StraceFlag"
const trapStdlibFlagConstName = "__xgo_injected_StdlibTrapDefaultAllow"

const (
// auto detected
flag_MAIN_MODULE = "MAIN_MODULE"
// --strace
flag_STRACE = "STRACE"
// --strace-dir
flag_STRACE_DIR = "STRACE_DIR"
// --strace-snapshot-main-module
flag_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT = "STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT"
// --trap-stdlib
flag_TRAP_STDLIB = "TRAP_STDLIB"
)
Expand Down Expand Up @@ -454,10 +461,14 @@ func injectXgoGeneralFlags(fileList []*syntax.File) {
forEachConst(file.DeclList, func(constDecl *syntax.ConstDecl) bool {
for _, name := range constDecl.NameList {
switch name.Value {
case flag_MAIN_MODULE:
constDecl.Values = newStringLit(os.Getenv(XGO_MAIN_MODULE))
case flag_STRACE:
constDecl.Values = newStringLit(os.Getenv(XGO_STACK_TRACE))
case flag_STRACE_DIR:
constDecl.Values = newStringLit(os.Getenv(XGO_STACK_TRACE_DIR))
case flag_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT:
constDecl.Values = newStringLit(os.Getenv(XGO_STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT))
case flag_TRAP_STDLIB:
constDecl.Values = newStringLit(os.Getenv(XGO_STD_LIB_TRAP_DEFAULT_ALLOW))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package snapshot_main_default

import "testing"

func TestSnapshotMainDefault(t *testing.T) {
// TODO: I forgot how to write test against trace
// see https://github.com/xhd2015/xgo/issues/281#issuecomment-2466153734
// for test detail
}
6 changes: 6 additions & 0 deletions runtime/test/trace/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
// NOTE: do not run with -cover, because
// extra function will be included in trace

// TODO: this test currently fails, but
// it is not so urgent to fix it, so
// I'll keep it here.
// once I got enough time to do, I'll
// try my best.
// see related https://github.com/xhd2015/xgo/issues/281
func TestNoSnapshot(t *testing.T) {
test(t, noSnapshotExpect, nil)
}
Expand Down
22 changes: 22 additions & 0 deletions runtime/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"os"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand All @@ -27,7 +29,18 @@ type testInfo struct {
onFinish func()
}

var effectMainModule string

func init() {
if flags.MAIN_MODULE != "" {
// fmt.Fprintf(os.Stderr, "DEBUG main module from flags: %s\n", flags.MAIN_MODULE)
effectMainModule = flags.MAIN_MODULE
} else {
buildInfo, _ := debug.ReadBuildInfo()
if buildInfo != nil {
effectMainModule = buildInfo.Main.Path
}
}
__xgo_link_on_test_start(func(t *testing.T, fn func(t *testing.T)) {
name := t.Name()
if name == "" {
Expand Down Expand Up @@ -268,6 +281,15 @@ func handleTracePre(ctx context.Context, f *core.FuncInfo, args core.Object, res
break
}
}

// check if allow main module to be defaultly snapshoted
if !anySnapshot && flags.STRACE_SNAPSHOT_MAIN_MODULE_DEFAULT != "false" && effectMainModule != "" && strings.HasPrefix(f.Pkg, effectMainModule) {
// main_module or main_module/*
if len(f.Pkg) == len(effectMainModule) || f.Pkg[len(effectMainModule)] == '/' {
// fmt.Fprintf(os.Stderr, "DEBUG main module snapshot: %s of %s\n", f.Pkg, effectMainModule)
anySnapshot = true
}
}
if anySnapshot {
stack.Snapshot = true
stack.Args = premarshal(stack.Args)
Expand Down
Loading

0 comments on commit fc8ddef

Please sign in to comment.