Пример #1
0
    def test_unwind_expression(self):
        """Test unwinding from an expression."""
        self.build()

        exe = os.path.join(os.getcwd(), "a.out")

        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Create the breakpoint.
        main_spec = lldb.SBFileSpec("main.cpp", False)
        breakpoint = target.BreakpointCreateBySourceRegex("// Set a breakpoint here to get started", main_spec)
        self.assertTrue(breakpoint, VALID_BREAKPOINT)

        # Launch the process, and do not stop at the entry point.
        process = target.LaunchSimple (None, None, self.get_process_working_directory())

        if not process:
            self.fail("SBTarget.LaunchProcess() failed")

        if process.GetState() != lldb.eStateStopped:
            self.fail("Process should be in the 'stopped' state, "
                      "instead the actual state is: '%s'" %
                      lldbutil.state_type_to_str(process.GetState()))

        thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, breakpoint)
        self.assertIsNotNone(thread, "Expected one thread to be stopped at the breakpoint")

        #
        # Use Python API to evaluate expressions while stopped in a stack frame.
        #
        main_frame = thread.GetFrameAtIndex(0)

        # Next set a breakpoint in this function, set up Expression options to stop on 
        # breakpoint hits, and call the function.
        fun_bkpt = target.BreakpointCreateBySourceRegex("// Stop inside the function here.", main_spec)
        self.assertTrue(fun_bkpt, VALID_BREAKPOINT)
        options = lldb.SBExpressionOptions()
        options.SetIgnoreBreakpoints(False)
        options.SetUnwindOnError(False)

        val = main_frame.EvaluateExpression("a_function_to_call()", options)

        self.assertTrue(val.GetError().Fail(), "We did not complete the execution.")
        error_str = val.GetError().GetCString()
        self.assertTrue("Execution was interrupted, reason: breakpoint" in error_str, "And the reason was right.")

        thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, fun_bkpt)
        self.assertTrue(thread.IsValid(), "We are indeed stopped at our breakpoint")
                
        # Now unwind the expression, and make sure we got back to where we started.
        error = thread.UnwindInnermostExpression()
        self.assertTrue(error.Success(), "We succeeded in unwinding")
        
        cur_frame = thread.GetFrameAtIndex(0)
        self.assertTrue(cur_frame.IsEqual(main_frame), "We got back to the main frame.")
Пример #2
0
    def test_step_over(self):
        #lldb.DBG.EnableLog("lldb", ["step","breakpoint"])
        
        self.thread.StepOver()
        # We should be stopped at the breakpoint_2 line with stop plan complete reason
        self.assertEquals(self.process.GetState(), lldb.eStateStopped)
        self.assertEquals(self.thread.GetStopReason(), lldb.eStopReasonPlanComplete)

        self.thread.StepOver()
        # We should be stopped at the breakpoint_3 line with stop plan complete reason
        self.assertEquals(self.process.GetState(), lldb.eStateStopped)
        self.assertEquals(self.thread.GetStopReason(), lldb.eStopReasonPlanComplete)

        self.thread.StepOver()
        # We should be stopped at the breakpoint_4
        self.assertEquals(self.process.GetState(), lldb.eStateStopped)
        self.assertEquals(self.thread.GetStopReason(), lldb.eStopReasonBreakpoint)
        thread1 = lldbutil.get_one_thread_stopped_at_breakpoint(self.process, self.breakpoint4)
        self.assertEquals(self.thread, thread1, "Didn't stop at breakpoint 4.")

        # Check that stepping does not affect breakpoint's hit count
        self.assertEquals(self.breakpoint1.GetHitCount(), 1)
        self.assertEquals(self.breakpoint2.GetHitCount(), 0)
        self.assertEquals(self.breakpoint3.GetHitCount(), 0)
        self.assertEquals(self.breakpoint4.GetHitCount(), 1)

        # Run the process until termination
        self.process.Continue()
        self.assertEquals(self.process.GetState(), lldb.eStateExited)
Пример #3
0
    def setUp(self):
        TestBase.setUp(self)
       
        self.build()
        exe = os.path.join(os.getcwd(), "a.out")
        src = lldb.SBFileSpec("main.cpp")

        # Create a target by the debugger.
        self.target = self.dbg.CreateTarget(exe)
        self.assertTrue(self.target, VALID_TARGET)

        # Setup four breakpoints, two of them with false condition
        self.line1 = line_number('main.cpp', "breakpoint_1")
        self.line4 = line_number('main.cpp', "breakpoint_4")

        self.breakpoint1 = self.target.BreakpointCreateByLocation(src, self.line1)        
        self.assertTrue(
            self.breakpoint1 and self.breakpoint1.GetNumLocations() == 1,
            VALID_BREAKPOINT)

        self.breakpoint2 = self.target.BreakpointCreateBySourceRegex("breakpoint_2", src)
        self.breakpoint2.GetLocationAtIndex(0).SetCondition('false')

        self.breakpoint3 = self.target.BreakpointCreateBySourceRegex("breakpoint_3", src)
        self.breakpoint3.GetLocationAtIndex(0).SetCondition('false')

        self.breakpoint4 = self.target.BreakpointCreateByLocation(src, self.line4)

        # Start debugging
        self.process = self.target.LaunchSimple(
            None, None, self.get_process_working_directory())
        self.assertIsNotNone(self.process, PROCESS_IS_VALID)
        self.thread = lldbutil.get_one_thread_stopped_at_breakpoint(self.process, self.breakpoint1)
        self.assertIsNotNone(self.thread, "Didn't stop at breakpoint 1.")
Пример #4
0
    def prepare_test(self):
        self.build()
        exe = os.path.join (os.getcwd(), "a.out")

        # Create a target by the debugger.
        self.target = self.dbg.CreateTarget(exe)
        self.assertTrue(self.target, VALID_TARGET)

        breakpoint1 = self.target.BreakpointCreateBySourceRegex("Set breakpoint here", lldb.SBFileSpec("main.cpp"))
        self.assertTrue(breakpoint1 and breakpoint1.GetNumLocations() == 1, VALID_BREAKPOINT)

        # Now launch the process, and do not stop at entry point.
        self.process = self.target.LaunchSimple (None, None, self.get_process_working_directory())
        self.assertIsNotNone(self.process, PROCESS_IS_VALID)

        # We should be stopped at the first breakpoint
        self.thread = lldbutil.get_one_thread_stopped_at_breakpoint(self.process, breakpoint1)
        self.assertIsNotNone(self.thread, "Expected one thread to be stopped at breakpoint 1")

        # Set breakpoint to the next instruction
        frame = self.thread.GetFrameAtIndex(0)

        address = frame.GetPCAddress()
        instructions = self.target.ReadInstructions(address, 2)
        self.assertTrue(len(instructions) == 2)
        self.bkpt_address = instructions[1].GetAddress()
        self.breakpoint2 = self.target.BreakpointCreateByAddress(self.bkpt_address.GetLoadAddress(self.target))
        self.assertTrue(self.breakpoint2 and self.breakpoint2.GetNumLocations() == 1, VALID_BREAKPOINT)
Пример #5
0
    def test(self):
        self.build()
        exe = self.getBuildArtifact("a.out")

        self.runCmd("target create %s" % exe)

        # Create the target
        target = self.dbg.CreateTarget(exe)

        # Create any breakpoints we need
        breakpoint = target.BreakpointCreateBySourceRegex(
            'break here', lldb.SBFileSpec("main.cpp", False))
        self.assertTrue(breakpoint, VALID_BREAKPOINT)

        self.runCmd("process launch 1 2 3")

        process = self.process()
        thread = lldbutil.get_one_thread_stopped_at_breakpoint(
            process, breakpoint)
        self.assertIsNotNone(
            thread, "Process should be stopped at a breakpoint in main")
        self.assertTrue(thread.IsValid(), "Stopped thread is not valid")

        self.expect("frame variable argv[1]", substrs=['1'])
        self.expect("frame variable argv[2]", substrs=['2'])
        self.expect("frame variable argv[3]", substrs=['3'])

        # Let program exit
        self.runCmd("continue")

        # Re-run with no args and make sure we still run with 1 2 3 as arguments as
        # they should have been stored in "target.run-args"
        self.runCmd("process launch")

        process = self.process()
        thread = lldbutil.get_one_thread_stopped_at_breakpoint(
            process, breakpoint)

        self.assertIsNotNone(
            thread, "Process should be stopped at a breakpoint in main")
        self.assertTrue(thread.IsValid(), "Stopped thread is not valid")

        self.expect("frame variable argv[1]", substrs=['1'])
        self.expect("frame variable argv[2]", substrs=['2'])
        self.expect("frame variable argv[3]", substrs=['3'])
Пример #6
0
    def do_conditional_break(self):
        """Exercise some thread and frame APIs to break if c() is called by a()."""
        exe = os.path.join(os.getcwd(), "a.out")

        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        breakpoint = target.BreakpointCreateByName("c", exe)
        self.assertTrue(breakpoint, VALID_BREAKPOINT)

        # Now launch the process, and do not stop at entry point.
        process = target.LaunchSimple (None, None, self.get_process_working_directory())

        self.assertTrue(process, PROCESS_IS_VALID)

        # The stop reason of the thread should be breakpoint.
        self.assertTrue(process.GetState() == lldb.eStateStopped,
                        STOPPED_DUE_TO_BREAKPOINT)

        # Find the line number where a's parent frame function is c.
        line = line_number('main.c',
            "// Find the line number where c's parent frame is a here.")

        # Suppose we are only interested in the call scenario where c()'s
        # immediate caller is a() and we want to find out the value passed from
        # a().
        #
        # The 10 in range(10) is just an arbitrary number, which means we would
        # like to try for at most 10 times.
        for j in range(10):
            if self.TraceOn():
                print("j is: ", j)
            thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, breakpoint)
            self.assertIsNotNone(thread, "Expected one thread to be stopped at the breakpoint")
            
            if thread.GetNumFrames() >= 2:
                frame0 = thread.GetFrameAtIndex(0)
                name0 = frame0.GetFunction().GetName()
                frame1 = thread.GetFrameAtIndex(1)
                name1 = frame1.GetFunction().GetName()
                #lldbutil.print_stacktrace(thread)
                self.assertTrue(name0 == "c", "Break on function c()")
                if (name1 == "a"):
                    # By design, we know that a() calls c() only from main.c:27.
                    # In reality, similar logic can be used to find out the call
                    # site.
                    self.assertTrue(frame1.GetLineEntry().GetLine() == line,
                                    "Immediate caller a() at main.c:%d" % line)

                    # And the local variable 'val' should have a value of (int) 3.
                    val = frame1.FindVariable("val")
                    self.assertEqual("int", val.GetTypeName())
                    self.assertEqual("3", val.GetValue())
                    break

            process.Continue()
Пример #7
0
    def test_continue(self):
        """Test that continue stops at the second breakpoint."""
        self.prepare_test()

        self.process.Continue()
        self.assertEquals(self.process.GetState(), lldb.eStateStopped)
        # We should be stopped at the second breakpoint
        self.thread = lldbutil.get_one_thread_stopped_at_breakpoint(self.process, self.breakpoint2)
        self.assertIsNotNone(self.thread, "Expected one thread to be stopped at breakpoint 2")

        self.finish_test()
Пример #8
0
    def test_single_step(self):
        """Test that single step stops at the second breakpoint."""
        self.prepare_test()

        step_over = False
        self.thread.StepInstruction(step_over)

        self.assertEquals(self.process.GetState(), lldb.eStateStopped)
        self.assertEquals(self.thread.GetFrameAtIndex(0).GetPCAddress().GetLoadAddress(self.target),
                self.bkpt_address.GetLoadAddress(self.target))
        self.thread = lldbutil.get_one_thread_stopped_at_breakpoint(self.process, self.breakpoint2)
        self.assertIsNotNone(self.thread, "Expected one thread to be stopped at breakpoint 2")

        self.finish_test()
Пример #9
0
    def do_unwind_test(self, thread, bkpt, timeout):
        #
        # Use Python API to evaluate expressions while stopped in a stack frame.
        #
        main_frame = thread.GetFrameAtIndex(0)

        options = lldb.SBExpressionOptions()
        options.SetIgnoreBreakpoints(False)
        options.SetUnwindOnError(False)
        options.SetOneThreadTimeoutInMicroSeconds(timeout)

        val = main_frame.EvaluateExpression("a_function_to_call()", options)

        self.assertTrue(
            val.GetError().Fail(),
            "We did not complete the execution.")
        error_str = val.GetError().GetCString()
        self.assertTrue(
            "Execution was interrupted, reason: breakpoint" in error_str,
            "And the reason was right.")

        thread = lldbutil.get_one_thread_stopped_at_breakpoint(
            self.process(), bkpt)
        self.assertTrue(
            thread.IsValid(),
            "We are indeed stopped at our breakpoint")

        # Now unwind the expression, and make sure we got back to where we
        # started.
        error = thread.UnwindInnermostExpression()
        self.assertTrue(error.Success(), "We succeeded in unwinding")

        cur_frame = thread.GetFrameAtIndex(0)
        self.assertTrue(
            cur_frame.IsEqual(main_frame),
            "We got back to the main frame.")
Пример #10
0
    def create_during_step_base(self, step_cmd, step_stop_reason):
        """Test thread creation while using step-in."""
        exe = self.getBuildArtifact("a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # Get the target process
        target = self.dbg.GetSelectedTarget()

        # This should create a breakpoint in the stepping thread.
        self.bkpt = target.BreakpointCreateByLocation("main.cpp", self.breakpoint)

        # Run the program.
        self.runCmd("run", RUN_SUCCEEDED)

        process = target.GetProcess()

        # The stop reason of the thread should be breakpoint.
        stepping_thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, self.bkpt)
        self.assertTrue(stepping_thread.IsValid(), "We stopped at the right breakpoint")

        # Get the number of threads
        num_threads = process.GetNumThreads()

        # Make sure we see only two threads
        self.assertTrue(
            num_threads == 2,
            'Number of expected threads and actual threads do not match.')

        # Get the thread objects
        thread1 = process.GetThreadAtIndex(0)
        thread2 = process.GetThreadAtIndex(1)

        current_line = self.breakpoint
        # Keep stepping until we've reached our designated continue point
        while current_line != self.continuepoint:
            if stepping_thread != process.GetSelectedThread():
                process.SetSelectedThread(stepping_thread)

            self.runCmd(step_cmd)

            frame = stepping_thread.GetFrameAtIndex(0)
            current_line = frame.GetLineEntry().GetLine()

            # Make sure we're still where we thought we were
            self.assertTrue(
                current_line >= self.breakpoint,
                "Stepped to unexpected line, " +
                str(current_line))
            self.assertTrue(
                current_line <= self.continuepoint,
                "Stepped to unexpected line, " +
                str(current_line))

        # Update the number of threads
        num_threads = process.GetNumThreads()

        # Check to see that we increased the number of threads as expected
        self.assertTrue(
            num_threads == 3,
            'Number of expected threads and actual threads do not match after thread exit.')

        stop_reason = stepping_thread.GetStopReason()
        self.assertEqual(stop_reason, lldb.eStopReasonPlanComplete, "Stopped for plan completion")

        # Run to completion
        self.runCmd("process continue")

        # At this point, the inferior process should have exited.
        self.assertTrue(
            process.GetState() == lldb.eStateExited,
            PROCESS_EXITED)
Пример #11
0
    def test_evaluate_expression_python(self):
        """Test SBFrame.EvaluateExpression() API for evaluating an expression."""
        self.build()

        exe = os.path.join(os.getcwd(), "a.out")

        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Create the breakpoint.
        filespec = lldb.SBFileSpec("main.cpp", False)
        breakpoint = target.BreakpointCreateByLocation(filespec, self.line)
        self.assertTrue(breakpoint, VALID_BREAKPOINT)

        # Verify the breakpoint just created.
        self.expect(str(breakpoint), BREAKPOINT_CREATED, exe=False,
            substrs = ['main.cpp',
                       str(self.line)])

        # Launch the process, and do not stop at the entry point.
        # Pass 'X Y Z' as the args, which makes argc == 4.
        process = target.LaunchSimple (['X', 'Y', 'Z'], None, self.get_process_working_directory())

        if not process:
            self.fail("SBTarget.LaunchProcess() failed")

        if process.GetState() != lldb.eStateStopped:
            self.fail("Process should be in the 'stopped' state, "
                      "instead the actual state is: '%s'" %
                      lldbutil.state_type_to_str(process.GetState()))

        thread = lldbutil.get_one_thread_stopped_at_breakpoint(process, breakpoint)
        self.assertIsNotNone(thread, "Expected one thread to be stopped at the breakpoint")

        # The filename of frame #0 should be 'main.cpp' and function is main.
        self.expect(lldbutil.get_filenames(thread)[0],
                    "Break correctly at main.cpp", exe=False,
            startstr = "main.cpp")
        self.expect(lldbutil.get_function_names(thread)[0],
                    "Break correctly at main()", exe=False,
            startstr = "main")

        # We should be stopped on the breakpoint with a hit count of 1.
        self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE)

        #
        # Use Python API to evaluate expressions while stopped in a stack frame.
        #
        frame = thread.GetFrameAtIndex(0)

        val = frame.EvaluateExpression("2.234")
        self.expect(val.GetValue(), "2.345 evaluated correctly", exe=False,
            startstr = "2.234")
        self.expect(val.GetTypeName(), "2.345 evaluated correctly", exe=False,
            startstr = "double")
        self.DebugSBValue(val)

        val = frame.EvaluateExpression("argc")
        self.expect(val.GetValue(), "Argc evaluated correctly", exe=False,
            startstr = "4")
        self.DebugSBValue(val)

        val = frame.EvaluateExpression("*argv[1]")
        self.expect(val.GetValue(), "Argv[1] evaluated correctly", exe=False,
            startstr = "'X'")
        self.DebugSBValue(val)

        val = frame.EvaluateExpression("*argv[2]")
        self.expect(val.GetValue(), "Argv[2] evaluated correctly", exe=False,
            startstr = "'Y'")
        self.DebugSBValue(val)

        val = frame.EvaluateExpression("*argv[3]")
        self.expect(val.GetValue(), "Argv[3] evaluated correctly", exe=False,
            startstr = "'Z'")
        self.DebugSBValue(val)

        callee_break = target.BreakpointCreateByName ("a_function_to_call", None)
        self.assertTrue(callee_break.GetNumLocations() > 0)

        # Make sure ignoring breakpoints works from the command line:
        self.expect("expression -i true -- a_function_to_call()",
                    substrs = ['(int) $', ' 1'])
        self.assertTrue (callee_break.GetHitCount() == 1)

        # Now try ignoring breakpoints using the SB API's:
        options = lldb.SBExpressionOptions()
        options.SetIgnoreBreakpoints(True)
        value = frame.EvaluateExpression('a_function_to_call()', options)
        self.assertTrue (value.IsValid())
        self.assertTrue (value.GetValueAsSigned(0) == 2)
        self.assertTrue (callee_break.GetHitCount() == 2)
Пример #12
0
    def test_evaluate_expression_python(self):
        """Test SBFrame.EvaluateExpression() API for evaluating an expression."""
        self.build()

        exe = self.getBuildArtifact("a.out")

        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Create the breakpoint.
        filespec = lldb.SBFileSpec("main.cpp", False)
        breakpoint = target.BreakpointCreateByLocation(filespec, self.line)
        self.assertTrue(breakpoint, VALID_BREAKPOINT)

        # Verify the breakpoint just created.
        self.expect(str(breakpoint), BREAKPOINT_CREATED, exe=False,
                    substrs=['main.cpp',
                             str(self.line)])

        # Launch the process, and do not stop at the entry point.
        # Pass 'X Y Z' as the args, which makes argc == 4.
        process = target.LaunchSimple(
            ['X', 'Y', 'Z'], None, self.get_process_working_directory())

        if not process:
            self.fail("SBTarget.LaunchProcess() failed")

        if process.GetState() != lldb.eStateStopped:
            self.fail("Process should be in the 'stopped' state, "
                      "instead the actual state is: '%s'" %
                      lldbutil.state_type_to_str(process.GetState()))

        thread = lldbutil.get_one_thread_stopped_at_breakpoint(
            process, breakpoint)
        self.assertIsNotNone(
            thread, "Expected one thread to be stopped at the breakpoint")

        # The filename of frame #0 should be 'main.cpp' and function is main.
        self.expect(lldbutil.get_filenames(thread)[0],
                    "Break correctly at main.cpp", exe=False,
                    startstr="main.cpp")
        self.expect(lldbutil.get_function_names(thread)[0],
                    "Break correctly at main()", exe=False,
                    startstr="main")

        # We should be stopped on the breakpoint with a hit count of 1.
        self.assertTrue(breakpoint.GetHitCount() == 1, BREAKPOINT_HIT_ONCE)

        #
        # Use Python API to evaluate expressions while stopped in a stack frame.
        #
        frame = thread.GetFrameAtIndex(0)

        val = frame.EvaluateExpression("2.234")
        self.expect(val.GetValue(), "2.345 evaluated correctly", exe=False,
                    startstr="2.234")
        self.expect(val.GetTypeName(), "2.345 evaluated correctly", exe=False,
                    startstr="double")
        self.DebugSBValue(val)

        val = frame.EvaluateExpression("argc")
        self.expect(val.GetValue(), "Argc evaluated correctly", exe=False,
                    startstr="4")
        self.DebugSBValue(val)

        val = frame.EvaluateExpression("*argv[1]")
        self.expect(val.GetValue(), "Argv[1] evaluated correctly", exe=False,
                    startstr="'X'")
        self.DebugSBValue(val)

        val = frame.EvaluateExpression("*argv[2]")
        self.expect(val.GetValue(), "Argv[2] evaluated correctly", exe=False,
                    startstr="'Y'")
        self.DebugSBValue(val)

        val = frame.EvaluateExpression("*argv[3]")
        self.expect(val.GetValue(), "Argv[3] evaluated correctly", exe=False,
                    startstr="'Z'")
        self.DebugSBValue(val)

        callee_break = target.BreakpointCreateByName(
            "a_function_to_call", None)
        self.assertTrue(callee_break.GetNumLocations() > 0)

        # Make sure ignoring breakpoints works from the command line:
        self.expect("expression -i true -- a_function_to_call()",
                    substrs=['(int) $', ' 1'])
        self.assertTrue(callee_break.GetHitCount() == 1)

        # Now try ignoring breakpoints using the SB API's:
        options = lldb.SBExpressionOptions()
        options.SetIgnoreBreakpoints(True)
        value = frame.EvaluateExpression('a_function_to_call()', options)
        self.assertTrue(value.IsValid())
        self.assertTrue(value.GetValueAsSigned(0) == 2)
        self.assertTrue(callee_break.GetHitCount() == 2)
Пример #13
0
    def create_during_step_base(self, step_cmd, step_stop_reason):
        """Test thread creation while using step-in."""
        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # Get the target process
        target = self.dbg.GetSelectedTarget()

        # This should create a breakpoint in the stepping thread.
        self.bkpt = target.BreakpointCreateByLocation("main.cpp",
                                                      self.breakpoint)

        # Run the program.
        self.runCmd("run", RUN_SUCCEEDED)

        process = target.GetProcess()

        # The stop reason of the thread should be breakpoint.
        stepping_thread = lldbutil.get_one_thread_stopped_at_breakpoint(
            process, self.bkpt)
        self.assertTrue(stepping_thread.IsValid(),
                        "We stopped at the right breakpoint")

        # Get the number of threads
        num_threads = process.GetNumThreads()

        # Make sure we see only two threads
        self.assertTrue(
            num_threads == 2,
            'Number of expected threads and actual threads do not match.')

        # Get the thread objects
        thread1 = process.GetThreadAtIndex(0)
        thread2 = process.GetThreadAtIndex(1)

        current_line = self.breakpoint
        # Keep stepping until we've reached our designated continue point
        while current_line != self.continuepoint:
            if stepping_thread != process.GetSelectedThread():
                process.SetSelectedThread(stepping_thread)

            self.runCmd(step_cmd)

            frame = stepping_thread.GetFrameAtIndex(0)
            current_line = frame.GetLineEntry().GetLine()

            # Make sure we're still where we thought we were
            self.assertTrue(current_line >= self.breakpoint,
                            "Stepped to unexpected line, " + str(current_line))
            self.assertTrue(current_line <= self.continuepoint,
                            "Stepped to unexpected line, " + str(current_line))

        # Update the number of threads
        num_threads = process.GetNumThreads()

        # Check to see that we increased the number of threads as expected
        self.assertTrue(
            num_threads == 3,
            'Number of expected threads and actual threads do not match after thread exit.'
        )

        stop_reason = stepping_thread.GetStopReason()
        self.assertEqual(stop_reason, lldb.eStopReasonPlanComplete,
                         "Stopped for plan completion")

        # Run to completion
        self.runCmd("process continue")

        # At this point, the inferior process should have exited.
        self.assertTrue(process.GetState() == lldb.eStateExited,
                        PROCESS_EXITED)
Пример #14
0
    def do_conditional_break(self):
        """Exercise some thread and frame APIs to break if c() is called by a()."""
        exe = self.getBuildArtifact("a.out")

        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        breakpoint = target.BreakpointCreateByName("c", exe)
        self.assertTrue(breakpoint, VALID_BREAKPOINT)

        # Now launch the process, and do not stop at entry point.
        process = target.LaunchSimple(None, None,
                                      self.get_process_working_directory())

        self.assertTrue(process, PROCESS_IS_VALID)

        # The stop reason of the thread should be breakpoint.
        self.assertTrue(process.GetState() == lldb.eStateStopped,
                        STOPPED_DUE_TO_BREAKPOINT)

        # Find the line number where a's parent frame function is c.
        line = line_number(
            'main.c',
            "// Find the line number where c's parent frame is a here.")

        # Suppose we are only interested in the call scenario where c()'s
        # immediate caller is a() and we want to find out the value passed from
        # a().
        #
        # The 10 in range(10) is just an arbitrary number, which means we would
        # like to try for at most 10 times.
        for j in range(10):
            if self.TraceOn():
                print("j is: ", j)
            thread = lldbutil.get_one_thread_stopped_at_breakpoint(
                process, breakpoint)
            self.assertIsNotNone(
                thread, "Expected one thread to be stopped at the breakpoint")

            if thread.GetNumFrames() >= 2:
                frame0 = thread.GetFrameAtIndex(0)
                name0 = frame0.GetFunction().GetName()
                frame1 = thread.GetFrameAtIndex(1)
                name1 = frame1.GetFunction().GetName()
                # lldbutil.print_stacktrace(thread)
                self.assertTrue(name0 == "c", "Break on function c()")
                if (name1 == "a"):
                    # By design, we know that a() calls c() only from main.c:27.
                    # In reality, similar logic can be used to find out the call
                    # site.
                    self.assertTrue(frame1.GetLineEntry().GetLine() == line,
                                    "Immediate caller a() at main.c:%d" % line)

                    # And the local variable 'val' should have a value of (int)
                    # 3.
                    val = frame1.FindVariable("val")
                    self.assertEqual("int", val.GetTypeName())
                    self.assertEqual("3", val.GetValue())
                    break

            process.Continue()
Пример #15
0
    def test_unwind_expression(self):
        """Test unwinding from an expression."""
        self.build()

        exe = os.path.join(os.getcwd(), "a.out")

        target = self.dbg.CreateTarget(exe)
        self.assertTrue(target, VALID_TARGET)

        # Create the breakpoint.
        main_spec = lldb.SBFileSpec("main.cpp", False)
        breakpoint = target.BreakpointCreateBySourceRegex(
            "// Set a breakpoint here to get started", main_spec)
        self.assertTrue(breakpoint, VALID_BREAKPOINT)

        # Launch the process, and do not stop at the entry point.
        process = target.LaunchSimple(None, None,
                                      self.get_process_working_directory())

        if not process:
            self.fail("SBTarget.LaunchProcess() failed")

        if process.GetState() != lldb.eStateStopped:
            self.fail("Process should be in the 'stopped' state, "
                      "instead the actual state is: '%s'" %
                      lldbutil.state_type_to_str(process.GetState()))

        thread = lldbutil.get_one_thread_stopped_at_breakpoint(
            process, breakpoint)
        self.assertIsNotNone(
            thread, "Expected one thread to be stopped at the breakpoint")

        #
        # Use Python API to evaluate expressions while stopped in a stack frame.
        #
        main_frame = thread.GetFrameAtIndex(0)

        # Next set a breakpoint in this function, set up Expression options to stop on
        # breakpoint hits, and call the function.
        fun_bkpt = target.BreakpointCreateBySourceRegex(
            "// Stop inside the function here.", main_spec)
        self.assertTrue(fun_bkpt, VALID_BREAKPOINT)
        options = lldb.SBExpressionOptions()
        options.SetIgnoreBreakpoints(False)
        options.SetUnwindOnError(False)

        val = main_frame.EvaluateExpression("a_function_to_call()", options)

        self.assertTrue(val.GetError().Fail(),
                        "We did not complete the execution.")
        error_str = val.GetError().GetCString()
        self.assertTrue(
            "Execution was interrupted, reason: breakpoint" in error_str,
            "And the reason was right.")

        thread = lldbutil.get_one_thread_stopped_at_breakpoint(
            process, fun_bkpt)
        self.assertTrue(thread.IsValid(),
                        "We are indeed stopped at our breakpoint")

        # Now unwind the expression, and make sure we got back to where we
        # started.
        error = thread.UnwindInnermostExpression()
        self.assertTrue(error.Success(), "We succeeded in unwinding")

        cur_frame = thread.GetFrameAtIndex(0)
        self.assertTrue(cur_frame.IsEqual(main_frame),
                        "We got back to the main frame.")