Skip to content

Commit

Permalink
add --debug
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Jul 4, 2024
1 parent e64e4df commit c6caedb
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 91 deletions.
14 changes: 1 addition & 13 deletions cmd/go-tool-debug-compile/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,7 @@ func dlvExecListen(dir string, env []string, compilerBinary string, args []strin
fmt.Println(prompt)
}, func(port int) error {
// dlv exec --api-version=2 --listen=localhost:2345 --accept-multiclient --headless ./debug.bin
dlvArgs := []string{
"--api-version=2",
fmt.Sprintf("--listen=localhost:%d", port),
"--check-go-version=false",
"--log=true",
// "--accept-multiclient", // exits when client exits
"--headless", "exec",
compilerBinary,
"--",
}
// dlvArgs = append(dlvArgs, compilerBin)
dlvArgs = append(dlvArgs, args...)
err := cmd.Dir(dir).Env(env).Run("dlv", dlvArgs...)
err := cmd.Dir(dir).Env(env).Run("dlv", debug.FormatDlvArgs(compilerBinary, port, args)...)
if err != nil {
return fmt.Errorf("dlv: %w", err)
}
Expand Down
13 changes: 2 additions & 11 deletions cmd/xgo/exec_tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

cmd_exec "github.com/xhd2015/xgo/support/cmd"
"github.com/xhd2015/xgo/support/debug"
"github.com/xhd2015/xgo/support/goinfo"
)

Expand Down Expand Up @@ -122,18 +123,8 @@ func handleCompile(cmd string, opts *options, args []string) error {
if isDebug {
// TODO: add env
if logCompileEnable || debugWithDlv {
dlvArgs := []string{"--api-version=2",
"--listen=localhost:2345",
"--check-go-version=false",
"--log=true",
// "--accept-multiclient", // exits when client exits
"--headless", "exec",
compilerBin,
"--",
}
dlvArgs := debug.FormatDlvArgs(compilerBin, 2345, args)
envs := getDebugEnvMapping(xgoCompilerEnableEnv)
// dlvArgs = append(dlvArgs, compilerBin)
dlvArgs = append(dlvArgs, args...)
var strPrint []string
envList := make([]string, 0, len(envs))
for k, v := range envs {
Expand Down
103 changes: 90 additions & 13 deletions cmd/xgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import (
"strings"
"time"

cmd_support "github.com/xhd2015/xgo/support/cmd"

debug_support "github.com/xhd2015/xgo/support/debug"

"github.com/xhd2015/xgo/support/cmd"
"github.com/xhd2015/xgo/support/netutil"
"github.com/xhd2015/xgo/support/osinfo"

"github.com/xhd2015/xgo/cmd/xgo/exec_tool"
Expand Down Expand Up @@ -113,6 +118,7 @@ func main() {
}

func handleBuild(cmd string, args []string) error {
cmdRun := cmd == "run"
cmdBuild := cmd == "build"
cmdTest := cmd == "test"
cmdExec := cmd == "exec"
Expand All @@ -128,9 +134,11 @@ func handleBuild(cmd string, args []string) error {
flagV := opts.flagV
flagX := opts.flagX
flagC := opts.flagC
flagRun := opts.flagRun
logCompile := opts.logCompile
logDebugOption := opts.logDebug
debugCompile := opts.debugCompile
debug := opts.debug
optXgoSrc := opts.xgoSrc
noBuildOutput := opts.noBuildOutput
noInstrument := opts.noInstrument
Expand All @@ -145,7 +153,7 @@ func handleBuild(cmd string, args []string) error {
setupDev := opts.setupDev
buildCompiler := opts.buildCompiler
syncWithLink := opts.syncWithLink
debug := opts.debug
debugTarget := opts.debugTarget
vscode := opts.vscode
mod := opts.mod
gcflags := opts.gcflags
Expand Down Expand Up @@ -189,7 +197,7 @@ func handleBuild(cmd string, args []string) error {

var vscodeDebugFile string
var vscodeDebugFileSuffix string
if !noInstrument && debug != "" {
if !noInstrument && debugTarget != "" {
var err error
vscodeDebugFile, vscodeDebugFileSuffix, err = getVscodeDebugFile(tmpDir, vscode)
if err != nil {
Expand Down Expand Up @@ -249,7 +257,7 @@ func handleBuild(cmd string, args []string) error {
if trapStdlib {
buildCacheSuffix += "-trapstd"
}
if len(gcflags) > 0 {
if len(gcflags) > 0 || debug != nil {
buildCacheSuffix += "-gcflags"
}
if optionsFromFile != "" {
Expand Down Expand Up @@ -357,7 +365,7 @@ func handleBuild(cmd string, args []string) error {
if !noInstrument {
logDebug("build instrument tools: %s", instrumentGoroot)
xgoBin := os.Args[0]
compilerChanged, toolExecFlag, err = buildInstrumentTool(instrumentGoroot, realXgoSrc, compilerBin, compilerBuildID, "", xgoBin, debug, logCompile, noSetup, debugWithDlv)
compilerChanged, toolExecFlag, err = buildInstrumentTool(instrumentGoroot, realXgoSrc, compilerBin, compilerBuildID, "", xgoBin, debugTarget, logCompile, noSetup, debugWithDlv)
if err != nil {
return err
}
Expand All @@ -380,6 +388,30 @@ func handleBuild(cmd string, args []string) error {
var debugCompilePkg string
var debugCompileLogFile string

var runDebug bool
var testDebug bool
var buildDebug bool

var runFlagsAfterBuild []string
if debug != nil {
if cmdRun {
runDebug = true
} else if cmdTest {
if !flagC {
testDebug = true
} else {
buildDebug = true
}
} else if cmdBuild {
buildDebug = true
} else {
return fmt.Errorf("invalid --debug flag")
}
}

debugMode := runDebug || testDebug || buildDebug
var finalBuildOutput string

execCmdEnv := os.Environ()
var execCmd *exec.Cmd
if !cmdExec {
Expand Down Expand Up @@ -445,14 +477,25 @@ func handleBuild(cmd string, args []string) error {
buildCmdArgs = append(buildCmdArgs, "-a")
}
if flagV {
buildCmdArgs = append(buildCmdArgs, "-v")
if !testDebug {
buildCmdArgs = append(buildCmdArgs, "-v")
} else {
runFlagsAfterBuild = append(runFlagsAfterBuild, "-test.v")
}
}
if flagX {
buildCmdArgs = append(buildCmdArgs, "-x")
}
if flagC {
if flagC && !testDebug {
buildCmdArgs = append(buildCmdArgs, "-c")
}
if flagRun != "" {
if !testDebug {
buildCmdArgs = append(buildCmdArgs, "-run", flagRun)
} else {
runFlagsAfterBuild = append(runFlagsAfterBuild, "-test.run", flagRun)
}
}
if overlay != "" {
buildCmdArgs = append(buildCmdArgs, "-overlay", overlay)
}
Expand All @@ -462,31 +505,53 @@ func handleBuild(cmd string, args []string) error {
if modfile != "" {
buildCmdArgs = append(buildCmdArgs, "-modfile", modfile)
}

var hasBuildDebug bool
for _, f := range gcflags {
if f == "all=-N -l" || f == "all=-l -N" {
hasBuildDebug = true
}
buildCmdArgs = append(buildCmdArgs, "-gcflags="+f)
}
if cmdBuild || (cmdTest && flagC) {

if debugMode {
if !hasBuildDebug {
buildCmdArgs = append(buildCmdArgs, "-gcflags=all=-N -l")
}
if testDebug {
buildCmdArgs = append(buildCmdArgs, "-c")
}
}

if cmdBuild || (cmdTest && flagC) || debugMode {
// output
if noBuildOutput {
if !debugMode && noBuildOutput {
discardOut := filepath.Join(tmpDir, "discard.out")
buildCmdArgs = append(buildCmdArgs, "-o", discardOut)
} else if output != "" {
realOut := output
finalBuildOutput = output
if projectDir != "" {
// make absolute
absOutput, err := filepath.Abs(output)
if err != nil {
return fmt.Errorf("make output absolute: %w", err)
}
realOut = absOutput
finalBuildOutput = absOutput
}
buildCmdArgs = append(buildCmdArgs, "-o", realOut)
} else if debugMode {
finalBuildOutput = filepath.Join(tmpDir, "debug.bin")
}
if finalBuildOutput != "" {
buildCmdArgs = append(buildCmdArgs, "-o", finalBuildOutput)
}
}
buildCmdArgs = append(buildCmdArgs, remainArgs...)
if cmdTest && len(testArgs) > 0 {
buildCmdArgs = append(buildCmdArgs, testArgs...)
if !testDebug {
buildCmdArgs = append(buildCmdArgs, "-args")
buildCmdArgs = append(buildCmdArgs, testArgs...)
} else {
runFlagsAfterBuild = append(runFlagsAfterBuild, testArgs...)
}
}
logDebug("command: %s %v", instrumentGo, buildCmdArgs)
execCmd = exec.Command(instrumentGo, buildCmdArgs...)
Expand Down Expand Up @@ -568,6 +633,18 @@ func handleBuild(cmd string, args []string) error {
return err
}

if debugMode {
err := netutil.ServePort("localhost", 2345, true, 500*time.Millisecond, func(port int) {
fmt.Fprintln(os.Stderr, debug_support.FormatDlvPrompt(port))
}, func(port int) error {
// dlv exec --api-version=2 --listen=localhost:2345 --accept-multiclient --headless ./debug.bin
return cmd_support.Debug().Run("dlv", debug_support.FormatDlvArgs(finalBuildOutput, port, runFlagsAfterBuild)...)
})
if err != nil {
return err
}
}

// if dump IR is not nil, output to stdout
if tmpIRFile != "" {
// ast file not exists
Expand Down
Loading

0 comments on commit c6caedb

Please sign in to comment.