1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 func checkLldbPython(t
*testing
.T
) {
20 cmd
:= exec
.Command("lldb", "-P")
21 out
, err
:= cmd
.CombinedOutput()
23 t
.Skipf("skipping due to issue running lldb: %v\n%s", err
, out
)
25 lldbPath
= strings
.TrimSpace(string(out
))
27 cmd
= exec
.Command("/usr/bin/python2.7", "-c", "import sys;sys.path.append(sys.argv[1]);import lldb; print('go lldb python support')", lldbPath
)
28 out
, err
= cmd
.CombinedOutput()
31 t
.Skipf("skipping due to issue running python: %v\n%s", err
, out
)
33 if string(out
) != "go lldb python support\n" {
34 t
.Skipf("skipping due to lack of python lldb support: %s", out
)
37 if runtime
.GOOS
== "darwin" {
38 // Try to see if we have debugging permissions.
39 cmd
= exec
.Command("/usr/sbin/DevToolsSecurity", "-status")
40 out
, err
= cmd
.CombinedOutput()
42 t
.Skipf("DevToolsSecurity failed: %v", err
)
43 } else if !strings
.Contains(string(out
), "enabled") {
46 cmd
= exec
.Command("/usr/bin/groups")
47 out
, err
= cmd
.CombinedOutput()
49 t
.Skipf("groups failed: %v", err
)
50 } else if !strings
.Contains(string(out
), "_developer") {
51 t
.Skip("Not in _developer group")
56 const lldbHelloSource
= `
60 mapvar := make(map[string]string,5)
65 fmt.Println("hi") // line 10
70 const lldbScriptSource
= `
72 sys.path.append(sys.argv[1])
78 debugger = lldb.SBDebugger.Create()
79 debugger.SetAsync(True)
80 target = debugger.CreateTargetWithFileAndArch("a.exe", None)
82 print "Created target"
83 main_bp = target.BreakpointCreateByLocation("main.go", 10)
85 print "Created breakpoint"
86 process = target.LaunchSimple(None, None, os.getcwd())
88 print "Process launched"
89 listener = debugger.GetListener()
90 process.broadcaster.AddListener(listener, lldb.SBProcess.eBroadcastBitStateChanged)
92 event = lldb.SBEvent()
93 if listener.WaitForEvent(TIMEOUT_SECS, event):
94 if lldb.SBProcess.GetRestartedFromEvent(event):
96 state = process.GetState()
97 if state in [lldb.eStateUnloaded, lldb.eStateLaunching, lldb.eStateRunning]:
100 print "Timeout launching"
102 if state == lldb.eStateStopped:
103 for t in process.threads:
104 if t.GetStopReason() == lldb.eStopReasonBreakpoint:
105 print "Hit breakpoint"
106 frame = t.GetFrameAtIndex(0)
109 print "Stopped at %s:%d" % (frame.line_entry.file.basename, frame.line_entry.line)
111 print "Stopped in %s" % (frame.function.name,)
112 var = frame.FindVariable('intvar')
114 print "intvar = %s" % (var.GetValue(),)
118 print "Process state", state
121 print "Failed to create target a.exe"
123 lldb.SBDebugger.Destroy(debugger)
127 const expectedLldbOutput
= `Created target
131 Stopped at main.go:10
136 func TestLldbPython(t
*testing
.T
) {
137 testenv
.MustHaveGoBuild(t
)
138 if final
:= os
.Getenv("GOROOT_FINAL"); final
!= "" && runtime
.GOROOT() != final
{
139 t
.Skip("gdb test can fail with GOROOT_FINAL pending")
141 testenv
.SkipFlaky(t
, 31188)
147 src
:= filepath
.Join(dir
, "main.go")
148 err
:= os
.WriteFile(src
, []byte(lldbHelloSource
), 0644)
150 t
.Fatalf("failed to create src file: %v", err
)
153 mod
:= filepath
.Join(dir
, "go.mod")
154 err
= os
.WriteFile(mod
, []byte("module lldbtest"), 0644)
156 t
.Fatalf("failed to create mod file: %v", err
)
159 // As of 2018-07-17, lldb doesn't support compressed DWARF, so
160 // disable it for this test.
161 cmd
:= exec
.Command(testenv
.GoToolPath(t
), "build", "-gcflags=all=-N -l", "-ldflags=-compressdwarf=false", "-o", "a.exe")
163 cmd
.Env
= append(os
.Environ(), "GOPATH=") // issue 31100
164 out
, err
:= cmd
.CombinedOutput()
166 t
.Fatalf("building source %v\n%s", err
, out
)
169 src
= filepath
.Join(dir
, "script.py")
170 err
= os
.WriteFile(src
, []byte(lldbScriptSource
), 0755)
172 t
.Fatalf("failed to create script: %v", err
)
175 cmd
= exec
.Command("/usr/bin/python2.7", "script.py", lldbPath
)
177 got
, _
:= cmd
.CombinedOutput()
179 if string(got
) != expectedLldbOutput
{
180 if strings
.Contains(string(got
), "Timeout launching") {
181 t
.Skip("Timeout launching")
183 t
.Fatalf("Unexpected lldb output:\n%s", got
)