gdb/lldb call a function and break in it

孤人 提交于 2019-12-07 12:01:09

问题


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 ?


回答1:


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.




回答2:


It's not a joke? Just checked standard breakpoint to be sure:

(gdb) b test
Breakpoint 2 at 0x400671: file t.c, line 6.
(gdb) call test()

Breakpoint 2, test () at t.c:6
6       printf("a\n");


来源:https://stackoverflow.com/questions/27775080/gdb-lldb-call-a-function-and-break-in-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!