TestBreakpointConditions.py [plain text]
"""
Test breakpoint conditions with 'breakpoint modify -c <expr> id'.
"""
import os, time
import re
import unittest2
import lldb, lldbutil
from lldbtest import *
class BreakpointConditionsTestCase(TestBase):
mydir = os.path.join("functionalities", "breakpoint", "breakpoint_conditions")
@unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
def test_with_dsym_and_run_command(self):
"""Exercise breakpoint condition with 'breakpoint modify -c <expr> id'."""
self.buildDsym()
self.breakpoint_conditions()
@unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
@python_api_test
def test_with_dsym_and_python_api(self):
"""Use Python APIs to set breakpoint conditions."""
self.buildDsym()
self.breakpoint_conditions_python()
def test_with_dwarf_and_run_command(self):
"""Exercise breakpoint condition with 'breakpoint modify -c <expr> id'."""
self.buildDwarf()
self.breakpoint_conditions()
@python_api_test
def test_with_dwarf_and_python_api(self):
"""Use Python APIs to set breakpoint conditions."""
self.buildDwarf()
self.breakpoint_conditions_python()
def setUp(self):
TestBase.setUp(self)
self.line1 = line_number('main.c', '// Find the line number of function "c" here.')
self.line2 = line_number('main.c', "// Find the line number of c's parent call here.")
def breakpoint_conditions(self):
"""Exercise breakpoint condition with 'breakpoint modify -c <expr> id'."""
exe = os.path.join(os.getcwd(), "a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
self.expect("breakpoint set -n c", BREAKPOINT_CREATED,
startstr = "Breakpoint created: 1: name = 'c', locations = 1")
self.runCmd("breakpoint modify -c 'val == 3' 1")
self.runCmd("run", RUN_SUCCEEDED)
self.expect("process status", PROCESS_STOPPED,
patterns = ['Process .* stopped'])
self.expect("frame variable -T val", VARIABLES_DISPLAYED_CORRECTLY,
startstr = '(int) val = 3')
self.expect("breakpoint list -f", BREAKPOINT_HIT_THRICE,
substrs = ["resolved = 1",
"Condition: val == 3",
"hit count = 3"])
self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT_CONDITION,
patterns = ["frame #0.*main.c:%d" % self.line1,
"frame #1.*main.c:%d" % self.line2])
self.runCmd("process kill")
self.runCmd("breakpoint modify -c ''")
self.expect("breakpoint list -f", BREAKPOINT_STATE_CORRECT, matching=False,
substrs = ["Condition:"])
self.runCmd("run", RUN_SUCCEEDED)
self.expect("process status", PROCESS_STOPPED,
patterns = ['Process .* stopped'])
self.expect("frame variable -T val", VARIABLES_DISPLAYED_CORRECTLY,
startstr = '(int) val = 1')
def breakpoint_conditions_python(self):
"""Use Python APIs to set breakpoint conditions."""
exe = os.path.join(os.getcwd(), "a.out")
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
breakpoint = target.BreakpointCreateByName('c', 'a.out')
self.assertTrue(breakpoint and
breakpoint.GetNumLocations() == 1,
VALID_BREAKPOINT)
self.assertTrue(breakpoint.GetThreadIndex() == lldb.UINT32_MAX,
"The thread index should be invalid")
self.assertTrue(breakpoint.GetThreadName() is None,
"The thread name should be invalid")
breakpoint.SetThreadIndex(1) self.assertTrue(breakpoint.GetThreadIndex() == 1,
"The thread index has been set correctly")
location = breakpoint.GetLocationAtIndex(0)
self.assertTrue(location and
location.IsEnabled(),
VALID_BREAKPOINT_LOCATION)
location.SetCondition('val == 3')
self.expect(location.GetCondition(), exe=False,
startstr = 'val == 3')
process = target.LaunchSimple(None, None, os.getcwd())
self.assertTrue(process, PROCESS_IS_VALID)
from lldbutil import get_stopped_thread
thread = get_stopped_thread(process, lldb.eStopReasonPlanComplete)
self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
frame0 = thread.GetFrameAtIndex(0)
var = frame0.FindValue('val', lldb.eValueTypeVariableArgument)
self.assertTrue(frame0.GetLineEntry().GetLine() == self.line1 and
var.GetValue(frame0) == '3')
self.assertTrue(breakpoint.GetHitCount() == 3)
process.Continue()
if __name__ == '__main__':
import atexit
lldb.SBDebugger.Initialize()
atexit.register(lambda: lldb.SBDebugger.Terminate())
unittest2.main()