blob: 7bee800692dcfc13bcd0067db6fbf3e0f5314a79 [file]
"""
Test breakpoint conditions with 'breakpoint modify -c <expr> id'.
"""
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
# Flaky on buildbot, https://github.com/llvm/llvm-project/issues/206141.
@skipIf(oslist=["windows"], archs=["aarch64"])
class BreakpointConditionsTestCase(TestBase):
def test_breakpoint_condition_and_run_command(self):
"""Exercise breakpoint condition with 'breakpoint modify -c <expr> id'."""
self.build()
self.breakpoint_conditions()
def test_breakpoint_condition_inline_and_run_command(self):
"""Exercise breakpoint condition inline with 'breakpoint set'."""
self.build()
self.breakpoint_conditions(inline=True)
def test_breakpoint_condition_and_run_command_language(self):
"""Exercise breakpoint condition with 'breakpoint modify -c <expr> id'."""
self.build()
self.breakpoint_conditions(cpp=True)
def test_breakpoint_condition_inline_and_run_command_language(self):
"""Exercise breakpoint condition inline with 'breakpoint set'."""
self.build()
self.breakpoint_conditions(inline=True, cpp=True)
@add_test_categories(["pyapi"])
def test_breakpoint_condition_and_python_api(self):
"""Use Python APIs to set breakpoint conditions."""
self.build()
self.breakpoint_conditions_python()
@add_test_categories(["pyapi"])
def test_breakpoint_invalid_condition_and_python_api(self):
"""Use Python APIs to set breakpoint conditions."""
self.build()
self.breakpoint_invalid_conditions_python()
def test_breakpoint_condition_with_DIL(self):
"""Test evaluating breakpoint conditions with Data Inspection Language."""
self.build()
self.breakpoint_conditions_with_DIL()
def setUp(self):
# Call super's setUp().
TestBase.setUp(self)
# Find the line number to of function 'c'.
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, inline=False, cpp=False):
"""Exercise breakpoint condition with 'breakpoint modify -c <expr> id'."""
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
if cpp:
condition = "&val != nullptr && val == 3"
cmd_args = " -c '{}' -Y c++".format(condition)
else:
condition = "val == 3"
cmd_args = "-c '{}'".format(condition)
if inline:
# Create a breakpoint by function name 'c' and set the condition.
lldbutil.run_break_set_by_symbol(
self,
"c",
extra_options=cmd_args,
num_expected_locations=1,
sym_exact=True,
)
else:
# Create a breakpoint by function name 'c'.
lldbutil.run_break_set_by_symbol(
self, "c", num_expected_locations=1, sym_exact=True
)
# And set a condition on the breakpoint to stop on when 'val == 3'.
self.runCmd("breakpoint modify " + cmd_args + " 1")
# Now run the program.
self.runCmd("run", RUN_SUCCEEDED)
# The process should be stopped at this point.
self.expect("process status", PROCESS_STOPPED, patterns=["Process .* stopped"])
# 'frame variable --show-types val' should return 3 due to breakpoint condition.
self.expect(
"frame variable --show-types val",
VARIABLES_DISPLAYED_CORRECTLY,
startstr="(int) val = 3",
)
# Also check the hit count, which should be 3, by design.
self.expect(
"breakpoint list -f",
BREAKPOINT_HIT_ONCE,
substrs=[
"resolved = 1",
"Condition: {}".format(condition),
"hit count = 1",
],
)
# The frame #0 should correspond to main.c:36, the executable statement
# in function name 'c'. And the parent frame should point to
# main.c:24.
self.expect(
"thread backtrace",
STOPPED_DUE_TO_BREAKPOINT_CONDITION,
# substrs = ["stop reason = breakpoint"],
patterns=[
"frame #0.*main.c:%d" % self.line1,
"frame #1.*main.c:%d" % self.line2,
],
)
# Test that "breakpoint modify -c ''" clears the condition for the last
# created breakpoint, so that when the breakpoint hits, val == 1.
self.runCmd("process kill")
self.runCmd("breakpoint modify -c ''")
self.expect(
"breakpoint list -f",
BREAKPOINT_STATE_CORRECT,
matching=False,
substrs=["Condition:"],
)
# Now run the program again.
self.runCmd("run", RUN_SUCCEEDED)
# The process should be stopped at this point.
self.expect("process status", PROCESS_STOPPED, patterns=["Process .* stopped"])
# 'frame variable --show-types val' should return 1 since it is the first breakpoint hit.
self.expect(
"frame variable --show-types val",
VARIABLES_DISPLAYED_CORRECTLY,
startstr="(int) val = 1",
)
self.runCmd("process kill")
def breakpoint_conditions_python(self):
"""Use Python APIs to set breakpoint conditions."""
target = self.createTestTarget()
# Now create a breakpoint on main.c by name 'c'.
breakpoint = target.BreakpointCreateByName("c", "a.out")
self.trace("breakpoint:", breakpoint)
self.assertTrue(
breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT
)
# We didn't associate a thread index with the breakpoint, so it should
# be invalid.
self.assertEqual(
breakpoint.GetThreadIndex(),
lldb.UINT32_MAX,
"The thread index should be invalid",
)
# The thread name should be invalid, too.
self.assertIsNone(
breakpoint.GetThreadName(), "The thread name should be invalid"
)
# Let's set the thread index for this breakpoint and verify that it is,
# indeed, being set correctly.
# There's only one thread for the process.
breakpoint.SetThreadIndex(1)
self.assertEqual(
breakpoint.GetThreadIndex(), 1, "The thread index has been set correctly"
)
# Get the breakpoint location from breakpoint after we verified that,
# indeed, it has one location.
location = breakpoint.GetLocationAtIndex(0)
self.assertTrue(location and location.IsEnabled(), VALID_BREAKPOINT_LOCATION)
# Set the condition on the breakpoint location.
location.SetCondition("val == 3")
self.expect(location.GetCondition(), exe=False, startstr="val == 3")
# 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)
# Frame #0 should be on self.line1 and the break condition should hold.
from lldbsuite.test.lldbutil import get_stopped_thread
thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
self.assertTrue(
thread.IsValid(),
"There should be a thread stopped due to breakpoint condition",
)
frame0 = thread.GetFrameAtIndex(0)
var = frame0.FindValue("val", lldb.eValueTypeVariableArgument)
self.assertEqual(
frame0.GetLineEntry().GetLine(),
self.line1,
"The debugger stopped on the correct line",
)
self.assertEqual(var.GetValue(), "3")
# The hit count for the breakpoint should be 1.
self.assertEqual(breakpoint.GetHitCount(), 1)
# Test that the condition expression didn't create a result variable:
options = lldb.SBExpressionOptions()
value = frame0.EvaluateExpression("$0", options)
self.assertTrue(
value.GetError().Fail(), "Conditions should not make result variables."
)
process.Continue()
def breakpoint_invalid_conditions_python(self):
"""Use Python APIs to set breakpoint conditions."""
exe = self.getBuildArtifact("a.out")
# Create a target by the debugger.
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)
# Now create a breakpoint on main.c by name 'c'.
breakpoint = target.BreakpointCreateByName("c", "a.out")
self.trace("breakpoint:", breakpoint)
self.assertTrue(
breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT
)
# Set the condition on the breakpoint.
breakpoint.SetCondition("no_such_variable == not_this_one_either")
self.expect(
breakpoint.GetCondition(),
exe=False,
startstr="no_such_variable == not_this_one_either",
)
# 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)
# Frame #0 should be on self.line1 and the break condition should hold.
from lldbsuite.test.lldbutil import get_stopped_thread
thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
self.assertTrue(
thread.IsValid(),
"There should be a thread stopped due to breakpoint condition",
)
frame0 = thread.GetFrameAtIndex(0)
var = frame0.FindValue("val", lldb.eValueTypeVariableArgument)
self.assertEqual(frame0.GetLineEntry().GetLine(), self.line1)
# The hit count for the breakpoint should be 1.
self.assertEqual(breakpoint.GetHitCount(), 1)
def breakpoint_conditions_with_DIL(self):
"""Test evaluating breakpoint conditions with Data Inspection Language."""
exe = self.getBuildArtifact("a.out")
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
target = self.dbg.CreateTarget(exe)
self.runCmd("settings set target.experimental.use-DIL true")
def break_on_symbol_with_condition(symbol, condition, mode=""):
if mode:
mode = "-Z {}".format(mode)
lldbutil.run_break_set_by_symbol(
self,
symbol,
extra_options=" -c '{}' -Y C++ {}".format(condition, mode),
num_expected_locations=1,
sym_exact=True,
)
# Enable logging
log_file = self.getBuildArtifact("log-file.txt")
if os.path.exists(log_file):
os.remove(log_file)
self.runCmd("log enable -f '%s' lldb break" % (log_file))
## Check that breakpoint "a" is successfully evaluated by DIL.
# CHECK: DIL successfully parsed the condition: val == 1
# CHECK: Condition successfully evaluated by DIL, result is true.
break_on_symbol_with_condition("a", "val == 1")
## Run the program until breakpoint "a"
self.runCmd("settings set target.breakpoints-condition-mode dwim")
self.runCmd("run", RUN_SUCCEEDED)
## Check that when DIL's lexer, parser or interpreter fail,
## the evaluation falls back to UserExpression.
# CHECK: Lexing condition with DIL failed
# CHECK: error: <user expression
break_on_symbol_with_condition("b", "val # 1")
# CHECK: Parsing condition with DIL failed
# CHECK: error: <user expression
break_on_symbol_with_condition("b", "val == ?")
# CHECK: DIL successfully parsed the condition
# CHECK: Evaluating condition with DIL failed
# CHECK: error: <user expression
break_on_symbol_with_condition("b", "val == no_such_variable")
# Continue until breakpoint "b"
self.runCmd("continue")
## Create breakpoints with a valid condition where DIL fails during evaluating
## to test various condition evaluation modes.
## Check that "target.breakpoints-condition-mode" setting changes the default
## mode to "expr" and DIL is not called.
# CHECK-NOT: DIL
# CHECK: Condition successfully evaluated by UserExpression, result is true
self.runCmd("settings set target.breakpoints-condition-mode expr")
break_on_symbol_with_condition("c", "val == static_cast<int>(1.0)")
## Check that when condition evaluation fails in "dil" mode,
## the evaluation does not fall back to UserExpression.
# CHECK: Evaluating condition with DIL failed
# CHECK: Error evaluating condition
break_on_symbol_with_condition("c", "val == static_cast<int>(1.0)", "dil")
## Check that condition evaluation in "expr" mode does not attempt DIL
## and successfully uses UserExpression.
# CHECK-NOT: DIL
# CHECK: Condition successfully evaluated by UserExpression, result is true
break_on_symbol_with_condition("c", "val == static_cast<int>(1.0)", "expr")
## Check that when DIL fails during condition evaluation in "dwim" mode,
## the evaluation falls back to UserExpression and succeeds.
# CHECK: DIL successfully parsed the condition: val == static_cast<int>(1.0)
# CHECK: Evaluating condition with DIL failed
# CHECK: Condition successfully evaluated by UserExpression, result is true.
break_on_symbol_with_condition("c", "val == static_cast<int>(1.0)", "dwim")
## Check that the breakpoint condition mode is set to DIL via SB API,
## and UserExpression is not called.
# CHECK: Evaluating condition with DIL failed
# CHECK: Error evaluating condition
breakpoint = target.BreakpointCreateByName("c")
breakpoint.SetCondition("val == static_cast<int>(1.0)")
breakpoint.SetConditionMode(lldb.eBreakpointConditionModeDIL)
self.assertEqual(
breakpoint.GetConditionMode(), lldb.eBreakpointConditionModeDIL
)
# Continue until breakpoint "c"
self.runCmd("continue")
self.expect("process status", PROCESS_STOPPED, patterns=["Process .* stopped"])
self.filecheck_log(log_file, __file__)
self.runCmd("process kill")