Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lldb : Setting conditional breakpoint with string equality as condition

I would like to set a conditional breakpoint with lldb. This is usually done using -c option :

breakpoint set -f myFile.cpp -l 123 -c 'a==3'

However, in my case I want to test if a std::string object is equal to a certain string value but doing this

breakpoint set -f myFile.cpp -l 123 -c 'a=="hello"'

does not work… Lldb does not complain (while gdb would return an error) but it ignores the condition string upon reaching the breakpoint and breaks too early…

This question is similar to this one but with lldb instead of gdb. The solution presented there

breakpoint set -f myFile.cpp -l 123 if strcmp(a, "hello")==0

does not seem to be valid with lldb

Lldb version used : 3.4

like image 335
Gael Lorieul Avatar asked May 13 '16 08:05

Gael Lorieul


People also ask

How do you set a breakpoint in LLDB?

In lldb you can set breakpoints by typing either break or b followed by information on where you want the program to pause. After the b command, you can put either: a function name (e.g., b my_subroutine ) a line number (e.g., b 12 )

How do you set conditional breakpoints?

To set a conditional breakpoint, activate the context menu in the source pane, on the line where you want the breakpoint, and select “Add Conditional Breakpoint”. You'll then see a textbox where you can enter the expression. Press Return to finish.

How do you make if condition true in GDB?

Conditional breakpoints can help with this. Here [CONDITION] is a boolean expression, which, in GDB is true if the result is nonzero, otherwise it is false. The condition can include a function call, the value of a variable or the result of any GDB expression. Type help condition at the GDB prompt for more.


1 Answers

(lldb) br s -n main -c '(int)strcmp("test", var)==0'
Breakpoint 1: where = a.out`main + 11 at a.c:3, address = 0x0000000100000f8b
(lldb) br li
Current breakpoints:
1: name = 'main', locations = 1
Condition: (int)strcmp("test", var)==0

  1.1: where = a.out`main + 11 at a.c:3, address = a.out[0x0000000100000f8b], unresolved, hit count = 0 

(lldb) 

You can add the conditional expression after the fact. Like

(lldb) br s -n main
Breakpoint 1: where = a.out`main + 11 at a.c:3, address = 0x0000000100000f8b
(lldb) br mod -c '(int) strcmp("test", var) == 0'
(lldb) br li
Current breakpoints:
1: name = 'main', locations = 1
Condition: (int) strcmp("test", var) == 0

  1.1: where = a.out`main + 11 at a.c:3, address = a.out[0x0000000100000f8b], unresolved, hit count = 0 

(lldb) 

breakpoint modify takes a breakpoint number / list of breakpoint numbers at the end, defaulting to the newest breakpoint if none are specified (which is what I did here).

like image 187
Jason Molenda Avatar answered Sep 27 '22 05:09

Jason Molenda