GDB/LLDB 调用一个函数并中断它



我在长期运行程序中有一个全局函数:

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

我调试程序并中断,然后发出以下命令:

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

我希望它在我点击call test()后每行都中断test()方法,而不仅仅是立即返回结果。有人知道该怎么做吗?

------------------------------------下面的答案------------------------------------

@Jason 莫伦达的答案是正确的答案,用expr -i0 -- test()代替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) 

现在它中断test(),但引发错误!!如何避免错误?

lldb 中的 expression 命令(callexpression 的别名)需要十几个选项,其中一个是在执行表达式、--ignore-breakpoints false-i false-i 0时 lldb 是否应该在断点处停止。

(lldb) br s -n printf
Breakpoint 2: where = libsystem_c.dylib`printf, address = 0x00007fff89ee7930
(lldb) expr -- (void)printf("hin")
hi
(lldb) expr -i0 -- (void)printf("hin")
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)  

对默认行为(是否在断点处停止)进行了一些思考,这似乎是大多数人所期望的行为。

正如我所说,call命令只是expression的别名。 如果要更改其行为,可以用自己的别名覆盖别名。 例如 command alias call expr -i false --会解决问题。 你可以把它放在你的~/.lldbinit文件中,你就会被设置。

这不是

开玩笑吗?刚刚检查了标准断点以确保:

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

最新更新