1""" 2Test conditionally break on a function and inspect its variables. 3""" 4 5from __future__ import print_function 6 7 8import lldb 9from lldbsuite.test.decorators import * 10from lldbsuite.test.lldbtest import * 11from lldbsuite.test import lldbutil 12 13# rdar://problem/8532131 14# lldb not able to digest the clang-generated debug info correctly with respect to function name 15# 16# This class currently fails for clang as well as llvm-gcc. 17 18 19class ConditionalBreakTestCase(TestBase): 20 21 @add_test_categories(['pyapi']) 22 def test_with_python(self): 23 """Exercise some thread and frame APIs to break if c() is called by a().""" 24 self.build() 25 self.do_conditional_break() 26 27 def test_with_command(self): 28 """Simulate a user using lldb commands to break on c() if called from a().""" 29 self.build() 30 self.simulate_conditional_break_by_user() 31 32 def do_conditional_break(self): 33 """Exercise some thread and frame APIs to break if c() is called by a().""" 34 exe = self.getBuildArtifact("a.out") 35 36 target = self.dbg.CreateTarget(exe) 37 self.assertTrue(target, VALID_TARGET) 38 39 breakpoint = target.BreakpointCreateByName("c", exe) 40 self.assertTrue(breakpoint, VALID_BREAKPOINT) 41 42 # Now launch the process, and do not stop at entry point. 43 process = target.LaunchSimple( 44 None, None, self.get_process_working_directory()) 45 46 self.assertTrue(process, PROCESS_IS_VALID) 47 48 # The stop reason of the thread should be breakpoint. 49 self.assertState(process.GetState(), lldb.eStateStopped, 50 STOPPED_DUE_TO_BREAKPOINT) 51 52 # Find the line number where a's parent frame function is c. 53 line = line_number( 54 'main.c', 55 "// Find the line number where c's parent frame is a here.") 56 57 # Suppose we are only interested in the call scenario where c()'s 58 # immediate caller is a() and we want to find out the value passed from 59 # a(). 60 # 61 # The 10 in range(10) is just an arbitrary number, which means we would 62 # like to try for at most 10 times. 63 for j in range(10): 64 if self.TraceOn(): 65 print("j is: ", j) 66 thread = lldbutil.get_one_thread_stopped_at_breakpoint( 67 process, breakpoint) 68 self.assertIsNotNone( 69 thread, "Expected one thread to be stopped at the breakpoint") 70 71 if thread.GetNumFrames() >= 2: 72 frame0 = thread.GetFrameAtIndex(0) 73 name0 = frame0.GetFunction().GetName() 74 frame1 = thread.GetFrameAtIndex(1) 75 name1 = frame1.GetFunction().GetName() 76 # lldbutil.print_stacktrace(thread) 77 self.assertEqual(name0, "c", "Break on function c()") 78 if (name1 == "a"): 79 # By design, we know that a() calls c() only from main.c:27. 80 # In reality, similar logic can be used to find out the call 81 # site. 82 self.assertEqual(frame1.GetLineEntry().GetLine(), line, 83 "Immediate caller a() at main.c:%d" % line) 84 85 # And the local variable 'val' should have a value of (int) 86 # 3. 87 val = frame1.FindVariable("val") 88 self.assertEqual("int", val.GetTypeName()) 89 self.assertEqual("3", val.GetValue()) 90 break 91 92 process.Continue() 93 94 def simulate_conditional_break_by_user(self): 95 """Simulate a user using lldb commands to break on c() if called from a().""" 96 97 # Sourcing .lldb in the current working directory, which sets the main 98 # executable, sets the breakpoint on c(), and adds the callback for the 99 # breakpoint such that lldb only stops when the caller of c() is a(). 100 # the "my" package that defines the date() function. 101 if self.TraceOn(): 102 print("About to source .lldb") 103 104 if not self.TraceOn(): 105 self.HideStdout() 106 107 # Separate out the "file " + self.getBuildArtifact("a.out") command from .lldb file, for the sake of 108 # remote testsuite. 109 self.runCmd("file " + self.getBuildArtifact("a.out")) 110 self.runCmd("command source .lldb") 111 112 self.runCmd("break list") 113 114 if self.TraceOn(): 115 print("About to run.") 116 self.runCmd("run", RUN_SUCCEEDED) 117 118 self.runCmd("break list") 119 120 if self.TraceOn(): 121 print("Done running") 122 123 # The stop reason of the thread should be breakpoint. 124 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 125 substrs=['stopped', 'stop reason = breakpoint']) 126 127 # The frame info for frame #0 points to a.out`c and its immediate caller 128 # (frame #1) points to a.out`a. 129 130 self.expect("frame info", "We should stop at c()", 131 substrs=["a.out`c"]) 132 133 # Select our parent frame as the current frame. 134 self.runCmd("frame select 1") 135 self.expect("frame info", "The immediate caller should be a()", 136 substrs=["a.out`a"]) 137