summaryrefslogtreecommitdiff
path: root/shared/debug.go
diff options
context:
space:
mode:
author Lukacs T. Berki <lberki@google.com> 2021-03-02 10:09:41 +0100
committer Lukacs T. Berki <lberki@google.com> 2021-03-08 08:27:58 +0100
commit7d613bfe2cf0a864ce8b676212958dcb62bd2894 (patch)
tree49b4fae7485f1ac1d902a58b3f7d9627948cca70 /shared/debug.go
parent6d3e72688729ea72bf64f1e7dacccc5b57f384be (diff)
Make it possible to debug soong_ui.
This works by setting the SOONG_UI_DELVE= environment variable to the port on which soong_ui should accept a Delve connection on. This is achieved by reusing the Delve execution logic between soong_ui and soong_build. Test: Manual. Change-Id: Id2c1d4b6faac1a4a3918c91030ce2239f7daf54f
Diffstat (limited to 'shared/debug.go')
-rw-r--r--shared/debug.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/shared/debug.go b/shared/debug.go
index 0c9ba4f95..5392f2b79 100644
--- a/shared/debug.go
+++ b/shared/debug.go
@@ -1,8 +1,15 @@
package shared
import (
+ "fmt"
"os"
"os/exec"
+ "strings"
+ "syscall"
+)
+
+var (
+ isDebugging bool
)
// Finds the Delve binary to use. Either uses the SOONG_DELVE_PATH environment
@@ -15,3 +22,48 @@ func ResolveDelveBinary() string {
return result
}
+
+// Returns whether the current process is running under Delve due to
+// ReexecWithDelveMaybe().
+func IsDebugging() bool {
+ return isDebugging
+}
+
+// Re-executes the binary in question under the control of Delve when
+// delveListen is not the empty string. delvePath gives the path to the Delve.
+func ReexecWithDelveMaybe(delveListen, delvePath string) {
+ isDebugging = os.Getenv("SOONG_DELVE_REEXECUTED") == "true"
+ if isDebugging || delveListen == "" {
+ return
+ }
+
+ if delvePath == "" {
+ fmt.Fprintln(os.Stderr, "Delve debugging requested but failed to find dlv")
+ os.Exit(1)
+ }
+
+ soongDelveEnv := []string{}
+ for _, env := range os.Environ() {
+ idx := strings.IndexRune(env, '=')
+ if idx != -1 {
+ soongDelveEnv = append(soongDelveEnv, env)
+ }
+ }
+
+ soongDelveEnv = append(soongDelveEnv, "SOONG_DELVE_REEXECUTED=true")
+
+ dlvArgv := []string{
+ delvePath,
+ "--listen=:" + delveListen,
+ "--headless=true",
+ "--api-version=2",
+ "exec",
+ os.Args[0],
+ "--",
+ }
+
+ dlvArgv = append(dlvArgv, os.Args[1:]...)
+ syscall.Exec(delvePath, dlvArgv, soongDelveEnv)
+ fmt.Fprintln(os.Stderr, "exec() failed while trying to reexec with Delve")
+ os.Exit(1)
+}