isaacselement isaacselement - 5 months ago 75
Objective-C Question

gdb/lldb call a function and break in it

I have a global function in a long run program :

int test()
{
int a = 12;
int c = 10;
printf("a=%d",a);
a += c ;
printf("a=%d", a);
return a;
}


I debug the program and break, then issue the following command:

(lldb) call test()
a=12a=22(int) $0 = 22
(lldb)


I want it to break in
test()
method every line after I hit
call test()
, not just return the result immediately. Anyone knows how to do it ?

------------------------------------ Answer Below ------------------------------------

@Jason Molenda 's answer is the right answer,use
expr -i0 -- test()
instead of
call test()
:

(lldb) b test
Breakpoint 1: 4 locations.
(lldb) expr -i0 -- test()
error: Execution was interrupted, reason: breakpoint 1.1.
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
(lldb)


Now it break in
test()
, but raise an error!!! How to avoid the error ?

Answer

The expression command in lldb (call is an alias for expression) takes a dozen or so options, one of them being whether lldb should stop on a breakpoint while executing the expression, --ignore-breakpoints false, or -i false, or -i 0.

(lldb) br s -n printf
Breakpoint 2: where = libsystem_c.dylib`printf, address = 0x00007fff89ee7930
(lldb) expr -- (void)printf("hi\n")
hi
(lldb) expr -i0 -- (void)printf("hi\n")
error: Execution was interrupted, reason: breakpoint 2.1.
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
Process 15259 stopped
* thread #1: tid = 0xf0daf, 0x00007fff89ee7930 libsystem_c.dylib`printf, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    #0: 0x00007fff89ee7930 libsystem_c.dylib`printf
libsystem_c.dylib`printf:
-> 0x7fff89ee7930:  pushq  %rbp
   0x7fff89ee7931:  movq   %rsp, %rbp
   0x7fff89ee7934:  pushq  %r15
   0x7fff89ee7936:  pushq  %r14
(lldb)  

There was some thought put in to the default behavior (whether to stop on a breakpoint or not), and this seemed the behavior most people would expect.

As I said, the call command is just an alias for expression. If you want to change the behavior of it, you can overwrite the alias with one of your own. e.g. command alias call expr -i false -- will do the trick. You can put this in your ~/.lldbinit file and you'll be set.