diff --git a/_fixtures/issue877.go b/_fixtures/issue877.go new file mode 100644 index 00000000..5b73ee53 --- /dev/null +++ b/_fixtures/issue877.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "os" + "runtime" +) + +func main() { + dyldenv := os.Getenv("DYLD_LIBRARY_PATH") + runtime.Breakpoint() + fmt.Println(dyldenv) +} diff --git a/pkg/proc/gdbserial/gdbserver.go b/pkg/proc/gdbserial/gdbserver.go index 11869cb7..f6d920b1 100644 --- a/pkg/proc/gdbserial/gdbserver.go +++ b/pkg/proc/gdbserial/gdbserver.go @@ -334,6 +334,20 @@ const debugserverExecutable = "/Library/Developer/CommandLineTools/Library/Priva var ErrUnsupportedOS = errors.New("lldb backend not supported on windows") +func getLdEnvVars() []string { + var result []string + + environ := os.Environ() + for i := 0; i < len(environ); i++ { + if strings.HasPrefix(environ[i], "LD_") || + strings.HasPrefix(environ[i], "DYLD_") { + result = append(result, "-e", environ[i]) + } + } + + return result +} + // LLDBLaunch starts an instance of lldb-server and connects to it, asking // it to launch the specified target program with the specified arguments // (cmd) on the specified directory wd. @@ -358,7 +372,9 @@ func LLDBLaunch(cmd []string, wd string) (*Process, error) { if err != nil { return nil, err } - args := make([]string, 0, len(cmd)+4) + ldEnvVars := getLdEnvVars() + args := make([]string, 0, len(cmd)+4+len(ldEnvVars)) + args = append(args, ldEnvVars...) args = append(args, "-F", "-R", fmt.Sprintf("127.0.0.1:%d", listener.Addr().(*net.TCPAddr).Port), "--") args = append(args, cmd...) diff --git a/pkg/proc/proc_test.go b/pkg/proc/proc_test.go index b2c0b806..fcfa6ee5 100644 --- a/pkg/proc/proc_test.go +++ b/pkg/proc/proc_test.go @@ -2934,3 +2934,23 @@ func TestRecursiveNext(t *testing.T) { } }) } + +// TestIssue877 ensures that the environment variables starting with DYLD_ and LD_ +// are passed when executing the binary on OSX via debugserver +func TestIssue877(t *testing.T) { + if runtime.GOOS != "darwin" && testBackend == "lldb" { + return + } + const envval = "/usr/local/lib" + os.Setenv("DYLD_LIBRARY_PATH", envval) + withTestProcess("issue877", t, func(p proc.Process, fixture protest.Fixture) { + assertNoError(proc.Continue(p), t, "Continue()") + v, err := evalVariable(p, "dyldenv") + assertNoError(err, t, "EvalVariable()") + vv := constant.StringVal(v.Value) + t.Logf("v = %q", vv) + if vv != envval { + t.Fatalf("value of v is %q (expected %q)", vv, envval) + } + }) +}