简体   繁体   English

如何在GDB中检查函数的返回值?

[英]How to inspect the return value of a function in GDB?

假设返回值分配给变量,是否可以在gdb中检查函数的返回值?

I imagine there are better ways to do it, but the finish command executes until the current stack frame is popped off and prints the return value -- given the program 我想有更好的方法可以做到这一点,但是执行完成命令直到弹出当前的堆栈帧并打印返回值 - 给定程序

int fun() {
    return 42;
}

int main( int argc, char *v[] ) {
    fun();
    return 0;
}

You can debug it as such -- 你可以这样调试 -

(gdb) r
Starting program: /usr/home/hark/a.out 

Breakpoint 1, fun () at test.c:2
2               return 42;
(gdb) finish
Run till exit from #0  fun () at test.c:2
main () at test.c:7
7               return 0;
Value returned is $1 = 42
(gdb) 

The finish command can be abbreviated as fin . finish命令可以缩写为fin Do NOT use the f , which is abbreviation of frame command! 不要使用f ,这是frame命令的缩写!

Yes, just examine the EAX register by typing print $eax . 是的,只需输入print $eax来检查EAX寄存器。 For most functions, the return value is stored in that register, even if it's not used. 对于大多数函数,返回值存储在该寄存器中,即使它未被使用。

The exceptions to this are functions returning types larger than 32 bits, specifically 64-bit integers ( long long ), double s, and structs or classes . 例外情况是函数返回大于32位的类型,特别是64位整数( long long ), double s和structsclasses

The other exception is if you're not running on an Intel architecture. 另一个例外是如果您没有在英特尔架构上运行。 In that case, you'll have to figure out which register is used, if any. 在这种情况下,您必须确定使用哪个寄存器,如果有的话。

Here's how todo this with no symbols. 这是没有符号的todo。

gdb ls
This GDB was configured as "ppc64-yellowdog-linux-gnu"...
(no debugging symbols found)
Using host libthread_db library "/lib64/libthread_db.so.1".

(gdb) break __libc_start_main
Breakpoint 1 at 0x10013cb0
(gdb) r
Starting program: /bin/ls
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
(no debugging symbols found)
Breakpoint 1 at 0xfdfed3c
(no debugging symbols found)
[Thread debugging using libthread_db enabled]
[New Thread 4160418656 (LWP 10650)]
(no debugging symbols found)
(no debugging symbols found)
[Switching to Thread 4160418656 (LWP 10650)]

Breakpoint 1, 0x0fdfed3c in __libc_start_main () from /lib/libc.so.6
(gdb) info frame
Stack level 0, frame at 0xffd719a0:
 pc = 0xfdfed3c in __libc_start_main; saved pc 0x0
 called by frame at 0x0
 Arglist at 0xffd71970, args:
 Locals at 0xffd71970, Previous frame's sp is 0xffd719a0
 Saved registers:
  r24 at 0xffd71980, r25 at 0xffd71984, r26 at 0xffd71988, r27 at 0xffd7198c,
  r28 at 0xffd71990, r29 at 0xffd71994, r30 at 0xffd71998, r31 at 0xffd7199c,
  pc at 0xffd719a4, lr at 0xffd719a4
(gdb) frame 0
#0  0x0fdfed3c in __libc_start_main () from /lib/libc.so.6
(gdb) info fr
Stack level 0, frame at 0xffd719a0:
 pc = 0xfdfed3c in __libc_start_main; saved pc 0x0
 called by frame at 0x0
 Arglist at 0xffd71970, args:
 Locals at 0xffd71970, Previous frame's sp is 0xffd719a0
 Saved registers:
  r24 at 0xffd71980, r25 at 0xffd71984, r26 at 0xffd71988, r27 at 0xffd7198c,
  r28 at 0xffd71990, r29 at 0xffd71994, r30 at 0xffd71998, r31 at 0xffd7199c,
  pc at 0xffd719a4, lr at 0xffd719a4

Formatting kinda messed up there, note the use of "info frame" to inspect frames, and "frame #" to navigate your context to another context (up and down the stack) 格式化有点混乱,注意使用“信息框架”检查帧,并使用“帧#”将您的上下文导航到另一个上下文(在堆栈中上下)

bt also show's an abbreviated stack to help out. bt还显示了一个缩写堆栈来帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM