【问题标题】:GDB and Assembly: how to examine consts variables defined in heap?GDB 和汇编:如何检查堆中定义的 consts 变量?
【发布时间】:2014-04-27 02:51:51
【问题描述】:

例如在下面的代码中 "justatest" 和格式 "%s" 在堆中定义:

char str[15]="justatest";
int main(){
    printf("%s",str);
    return 0;
}

在 GDB 中,我在调用 printf 之前得到了汇编代码:

=> 0x0804841f <+14>:    movl   $0x804a020,0x4(%esp)
   0x08048427 <+22>:    movl   $0x80484d8,(%esp)
   0x0804842e <+29>:    call   0x80482f0 <printf@plt>

我是否必须使用“x/s 0x804a020”和“x/s 0x80484d8”检查参数 1by1

或者是否有我可以直接引用的堆中定义的常量表?

谢谢!

【问题讨论】:

  • 您的程序在哪里使用堆内存?不清楚你想在这里实现/问什么?是否要打印 printf() 中传递的参数?
  • str 不在堆中,而是在映射可执行文件的全局空间中。无论如何,如果可执行文件是用调试符号编译的,你可以按名称引用该变量。

标签: c assembly gdb heap-memory


【解决方案1】:

您对 str 驻留在 heap 上的理解不正确。它的全局变量被存储到数据段中。关于您的打印全局变量,您可以在我的 GNU/Linux 终端上执行以下操作。

$ gcc -g -Wall hello.c
$ gdb -q ./a.out 
Reading symbols from /home/mantosh/practice/a.out...done.
(gdb) break main
Breakpoint 1 at 0x400524: file hello.c, line 6.
(gdb) run
Starting program: /home/mantosh/practice/a.out 

Breakpoint 1, main () at bakwas.c:6
6       printf("%s",str);
(gdb) disassemble main
Dump of assembler code for function main:
   0x0000000000400520 <+0>: push   %rbp
   0x0000000000400521 <+1>: mov    %rsp,%rbp
=> 0x0000000000400524 <+4>: mov    $0x601020,%esi
   0x0000000000400529 <+9>: mov    $0x4005e4,%edi
   0x000000000040052e <+14>:    mov    $0x0,%eax
   0x0000000000400533 <+19>:    callq  0x4003f0 <printf@plt>
   0x0000000000400538 <+24>:    mov    $0x0,%eax
   0x000000000040053d <+29>:    pop    %rbp
   0x000000000040053e <+30>:    retq   
End of assembler dump.

(gdb) p str
$1 = "justatest\000\000\000\000\000"
(gdb) p &str
$2 = (char (*)[15]) 0x601020

// These are addresses of two arguments which would be passed in printf.
// From assembly instruction we can verify that before calling the printf
// these are getting stored into the registers.
(gdb) x/s 0x4005e4
0x4005e4:    "%s"
(gdb) x/s 0x601020
0x601020 <str>:  "justatest

【讨论】:

    【解决方案2】:

    后来我发现对于没有调试符号表的目标文件

    objdump -t obj
    

    将包含大部分全局变量/函数的符号及其地址 ,和

    objdump -D obj     instead of -d
    

    将包括所有部分,例如 .text/.data/.rodata 而不是仅 .text

    这两个组合提供了对我上面提到的内容的足够访问,例如切换表/常量字符串/全局变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2019-09-18
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多