Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make --strace standalone works #76

Merged
merged 9 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
files: ./

- name: Build
run: go build -v ./cmd/...
run: go build -o /dev/null -v ./cmd/xgo

- name: Test
run: go run ./script/run-test --reset-instrument --debug -v -cover -coverpkg github.com/xhd2015/xgo/runtime/... -coverprofile cover.out
Expand Down
2 changes: 1 addition & 1 deletion README_zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func C() { fmt.Printf("C\n") }

```sh
# 执行测试, 生成TestTrace.json
xgo test ./
xgo test --strace ./

# 查看Trace
xgo tool trace TestTrace.json
Expand Down
3 changes: 2 additions & 1 deletion cmd/xgo/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func setupDebugLog(logDebugOption *string) (func(), error) {
if logDebugOption != nil {
logDebugFileName = *logDebugOption
if logDebugFileName == "" {
logDebugFileName = "debug.log"
// default to stderr
logDebugFileName = "stderr"
}
}
if logDebugFileName == "" {
Expand Down
40 changes: 38 additions & 2 deletions cmd/xgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ func handleBuild(cmd string, args []string) error {
syncWithLink := opts.syncWithLink
debug := opts.debug
vscode := opts.vscode
mod := opts.mod
gcflags := opts.gcflags
overlay := opts.overlay
modfile := opts.modfile
withGoroot := opts.withGoroot
dumpIR := opts.dumpIR
dumpAST := opts.dumpAST
Expand Down Expand Up @@ -358,12 +361,36 @@ func handleBuild(cmd string, args []string) error {
execCmdEnv := os.Environ()
var execCmd *exec.Cmd
if !cmdExec {
mainModule, err := goinfo.ResolveMainModule(projectDir, remainArgs)
if modfile != "" {
// make modfile absolute
modfile, err = filepath.Abs(modfile)
if err != nil {
return nil
}
}
instrumentGo := filepath.Join(instrumentGoroot, "bin", "go")
subPaths, mainModule, err := goinfo.ResolveMainModule(projectDir, remainArgs)
if err != nil {
if !errors.Is(err, goinfo.ErrGoModNotFound) && !errors.Is(err, goinfo.ErrGoModDoesNotHaveModule) {
return err
}
}
if stackTrace == "on" && overlay == "" {
// check if xgo/runtime ready
impResult, impRuntimeErr := importRuntimeDep(cmdTest, instrumentGoroot, instrumentGo, goVersion, modfile, realXgoSrc, projectDir, subPaths, mainModule, mod, remainArgs)
if impRuntimeErr != nil {
// can be silently ignored
fmt.Fprintf(os.Stderr, "WARNING: --strace requires: import _ %q\n failed to auto import %s: %v\n", RUNTIME_TRACE_PKG, RUNTIME_TRACE_PKG, impRuntimeErr)
} else if impResult != nil {
overlay = impResult.overlayFile
if impResult.mod != "" {
mod = impResult.mod
}
if impResult.modfile != "" {
modfile = impResult.modfile
}
}
}
logDebug("resolved main module: %s", mainModule)
execCmdEnv = append(execCmdEnv, exec_tool.XGO_MAIN_MODULE+"="+mainModule)
// GOCACHE="$shdir/build-cache" PATH=$goroot/bin:$PATH GOROOT=$goroot DEBUG_PKG=$debug go build -toolexec="$shdir/exce_tool $cmd" "${build_flags[@]}" "$@"
Expand All @@ -383,6 +410,16 @@ func handleBuild(cmd string, args []string) error {
if flagC {
buildCmdArgs = append(buildCmdArgs, "-c")
}
if overlay != "" {
buildCmdArgs = append(buildCmdArgs, "-overlay", overlay)
}
if mod != "" {
buildCmdArgs = append(buildCmdArgs, "-mod="+mod)
}
if modfile != "" {
buildCmdArgs = append(buildCmdArgs, "-modfile", modfile)
}

for _, f := range gcflags {
buildCmdArgs = append(buildCmdArgs, "-gcflags="+f)
}
Expand All @@ -405,7 +442,6 @@ func handleBuild(cmd string, args []string) error {
}
}
buildCmdArgs = append(buildCmdArgs, remainArgs...)
instrumentGo := filepath.Join(instrumentGoroot, "bin", "go")
logDebug("command: %s %v", instrumentGo, buildCmdArgs)
execCmd = exec.Command(instrumentGo, buildCmdArgs...)
} else {
Expand Down
63 changes: 63 additions & 0 deletions cmd/xgo/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ type options struct {
buildCompiler bool
syncWithLink bool

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

overlay string
modfile string

// xgo test --trace

// --strace, --strace=on, --strace=off
Expand Down Expand Up @@ -88,7 +92,10 @@ func parseOptions(args []string) (*options, error) {

var noBuildOutput bool

var mod string
var gcflags []string
var overlay string
var modfile string
var stackTrace string

var remainArgs []string
Expand Down Expand Up @@ -134,12 +141,30 @@ func parseOptions(args []string) (*options, error) {
Flags: []string{"--dump-ast"},
Value: &dumpAST,
},
{
Flags: []string{"-mod"},
Set: func(v string) {
mod = v
},
},
{
Flags: []string{"-gcflags"},
Set: func(v string) {
gcflags = append(gcflags, v)
},
},
{
Flags: []string{"-overlay"},
Set: func(v string) {
overlay = v
},
},
{
Flags: []string{"-modfile"},
Set: func(v string) {
modfile = v
},
},
{
Flags: []string{"--log-debug"},
Single: true,
Expand Down Expand Up @@ -310,7 +335,10 @@ func parseOptions(args []string) (*options, error) {
// default true
syncWithLink: syncWithLink == nil || *syncWithLink,

mod: mod,
gcflags: gcflags,
overlay: overlay,
modfile: modfile,
stackTrace: stackTrace,

remainArgs: remainArgs,
Expand Down Expand Up @@ -342,3 +370,38 @@ func parseStackTraceFlag(arg string) (string, bool) {
}
panic(fmt.Errorf("unrecognized value %s: %s, expects on|off", arg, val))
}

func getPkgArgs(args []string) []string {
n := len(args)
newArgs := make([]string, 0, n)
for i := 0; i < n; i++ {
arg := args[i]
if !strings.HasPrefix(arg, "-") {
// stop at first non-arg
newArgs = append(newArgs, args[i:]...)
break
}
if arg == "-args" {
// go test -args: pass everything after to underlying program
break
}
eqIdx := strings.Index(arg, "=")
if eqIdx >= 0 {
// self hosted arg
continue
}
// make --opt equivalent with -opt
if strings.HasPrefix(arg, "--") {
arg = arg[1:]
}
switch arg {
case "-a", "-n", "-race", "-masan", "-asan", "-cover", "-v", "-work", "-x", "-linkshared", "-buildvcs", // shared among build,test,run
"-args", "-c", "-json": // -json for test
// zero arg
default:
// 1 arg
i++
}
}
return newArgs
}
24 changes: 14 additions & 10 deletions cmd/xgo/patch_compiler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"embed"
"errors"
"fmt"
"io/fs"
Expand Down Expand Up @@ -413,32 +414,35 @@ func importCompileInternalPatch(goroot string, xgoSrc string, forceReset bool, s
}
}

patchFS := xgo.PatchFS
// read from embed
err := fs.WalkDir(patchFS, patchCompilerName, func(path string, d fs.DirEntry, err error) error {
err := copyEmbedDir(xgo.PatchFS, patchCompilerName, dstDir)
if err != nil {
return err
}

return nil
}

func copyEmbedDir(srcFS embed.FS, subName string, dstDir string) error {
return fs.WalkDir(srcFS, subName, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == patchCompilerName {
if path == subName {
return os.MkdirAll(dstDir, 0755)
}
// TODO: test on Windows if "/" works
dstPath := filepath.Join(dstDir, path[len(patchCompilerName)+len("/"):])
dstPath := filepath.Join(dstDir, path[len(subName)+len("/"):])
if d.IsDir() {
return os.MkdirAll(dstPath, 0755)
}

content, err := patchFS.ReadFile(path)
content, err := srcFS.ReadFile(path)
if err != nil {
return err
}
return os.WriteFile(dstPath, content, 0755)
})
if err != nil {
return err
}

return nil
}

func readXgoSrc(xgoSrc string, paths []string) ([]byte, error) {
Expand Down
19 changes: 3 additions & 16 deletions cmd/xgo/pathsum/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/xhd2015/xgo/support/fileutil"
)

func PathSum(prefix string, path string) (string, error) {
Expand All @@ -31,7 +33,7 @@ func shortPath(path string, maxSeg int) string {
segs := strings.Split(path, string(os.PathSeparator))
shortSeg := make([]string, 0, len(segs))
for _, seg := range segs {
seg = processSpecial(seg)
seg = fileutil.CleanSpecial(seg)
// seg = strings.ReplaceAll(seg, "/", "")
// seg = strings.ReplaceAll(seg, "\\", "")
// seg = strings.ReplaceAll(seg, "?", "")
Expand Down Expand Up @@ -60,18 +62,3 @@ func shortPath(path string, maxSeg int) string {
}
return strings.Join(shortSeg, "_")
}

func processSpecial(path string) string {
chars := []rune(path)
n := len(chars)
j := 0
for i := 0; i < n; i++ {
ch := chars[i]
if ch < 128 && !(ch == '_' || ch == '-' || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
continue
}
chars[j] = chars[i]
j++
}
return string(chars[:j])
}
Loading
Loading