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.
20 func checkLldbPython(t
*testing
.T
) {
21 cmd
:= exec
.Command("lldb", "-P")
22 out
, err
:= cmd
.CombinedOutput()
24 t
.Skipf("skipping due to issue running lldb: %v\n%s", err
, out
)
26 lldbPath
= strings
.TrimSpace(string(out
))
28 cmd
= exec
.Command("/usr/bin/python2.7", "-c", "import sys;sys.path.append(sys.argv[1]);import lldb; print('go lldb python support')", lldbPath
)
29 out
, err
= cmd
.CombinedOutput()
32 t
.Skipf("skipping due to issue running python: %v\n%s", err
, out
)
34 if string(out
) != "go lldb python support\n" {
35 t
.Skipf("skipping due to lack of python lldb support: %s", out
)
38 if runtime
.GOOS
== "darwin" {
39 // Try to see if we have debugging permissions.
40 cmd
= exec
.Command("/usr/sbin/DevToolsSecurity", "-status")
41 out
, err
= cmd
.CombinedOutput()
43 t
.Skipf("DevToolsSecurity failed: %v", err
)
44 } else if !strings
.Contains(string(out
), "enabled") {
47 cmd
= exec
.Command("/usr/bin/groups")
48 out
, err
= cmd
.CombinedOutput()
50 t
.Skipf("groups failed: %v", err
)
51 } else if !strings
.Contains(string(out
), "_developer") {
52 t
.Skip("Not in _developer group")
57 const lldbHelloSource
= `
61 mapvar := make(map[string]string,5)
66 fmt.Println("hi") // line 10
71 const lldbScriptSource
= `
73 sys.path.append(sys.argv[1])
79 debugger = lldb.SBDebugger.Create()
80 debugger.SetAsync(True)
81 target = debugger.CreateTargetWithFileAndArch("a.exe", None)
83 print "Created target"
84 main_bp = target.BreakpointCreateByLocation("main.go", 10)
86 print "Created breakpoint"
87 process = target.LaunchSimple(None, None, os.getcwd())
89 print "Process launched"
90 listener = debugger.GetListener()
91 process.broadcaster.AddListener(listener, lldb.SBProcess.eBroadcastBitStateChanged)
93 event = lldb.SBEvent()
94 if listener.WaitForEvent(TIMEOUT_SECS, event):
95 if lldb.SBProcess.GetRestartedFromEvent(event):
97 state = process.GetState()
98 if state in [lldb.eStateUnloaded, lldb.eStateLaunching, lldb.eStateRunning]:
101 print "Timeout launching"
103 if state == lldb.eStateStopped:
104 for t in process.threads:
105 if t.GetStopReason() == lldb.eStopReasonBreakpoint:
106 print "Hit breakpoint"
107 frame = t.GetFrameAtIndex(0)
110 print "Stopped at %s:%d" % (frame.line_entry.file.basename, frame.line_entry.line)
112 print "Stopped in %s" % (frame.function.name,)
113 var = frame.FindVariable('intvar')
115 print "intvar = %s" % (var.GetValue(),)
119 print "Process state", state
122 print "Failed to create target a.exe"
124 lldb.SBDebugger.Destroy(debugger)
128 const expectedLldbOutput
= `Created target
132 Stopped at main.go:10
137 func TestLldbPython(t
*testing
.T
) {
138 testenv
.MustHaveGoBuild(t
)
139 if final
:= os
.Getenv("GOROOT_FINAL"); final
!= "" && runtime
.GOROOT() != final
{
140 t
.Skip("gdb test can fail with GOROOT_FINAL pending")
145 dir
, err
:= ioutil
.TempDir("", "go-build")
147 t
.Fatalf("failed to create temp directory: %v", err
)
149 defer os
.RemoveAll(dir
)
151 src
:= filepath
.Join(dir
, "main.go")
152 err
= ioutil
.WriteFile(src
, []byte(lldbHelloSource
), 0644)
154 t
.Fatalf("failed to create file: %v", err
)
157 cmd
:= exec
.Command(testenv
.GoToolPath(t
), "build", "-gcflags=all=-N -l", "-o", "a.exe")
159 out
, err
:= cmd
.CombinedOutput()
161 t
.Fatalf("building source %v\n%s", err
, out
)
164 src
= filepath
.Join(dir
, "script.py")
165 err
= ioutil
.WriteFile(src
, []byte(lldbScriptSource
), 0755)
167 t
.Fatalf("failed to create script: %v", err
)
170 cmd
= exec
.Command("/usr/bin/python2.7", "script.py", lldbPath
)
172 got
, _
:= cmd
.CombinedOutput()
174 if string(got
) != expectedLldbOutput
{
175 if strings
.Contains(string(got
), "Timeout launching") {
176 t
.Skip("Timeout launching")
178 t
.Fatalf("Unexpected lldb output:\n%s", got
)