找到变量Buf的准确地址

发布于 2024-10-08 03:25:16 字数 396 浏览 7 评论 0原文

作为参考,我使用以下代码:

#include <stdio.h>
#include <string.h>

int main (void) {
    char buf[100]; // ------> How do I find the address in gdb?

    printf ("Buffer is at memory location: %08x\n", &buf);
    strcpy (buf, "some random text");
    printf ("Text is [%s]\n", buf);

    return 0;
}

如何让 gdb 向我显示 buf 变量的地址?

As reference, I'm using the following code:

#include <stdio.h>
#include <string.h>

int main (void) {
    char buf[100]; // ------> How do I find the address in gdb?

    printf ("Buffer is at memory location: %08x\n", &buf);
    strcpy (buf, "some random text");
    printf ("Text is [%s]\n", buf);

    return 0;
}

How can I get gdb to show me the address of the buf variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深府石板幽径 2024-10-15 03:25:16

(gdb) p &a 如果您需要变量 a 的地址。不过,变量可能会缓存在寄存器中,在这种情况下,GDB 会告诉您为寄存器 $xxx 中的标识符“a”请求的地址

旁注:不要使用 gets,请参阅此处

(gdb) p &a if you need the address of variable a. A variable might be cached in a register though, in which case GDB would tell you address requested for identifier "a" which is in register $xxx.

Sidenote: do not use gets, see here.

甚是思念 2024-10-15 03:25:16

当 gdb 设置为 C 语言模式(和 Objective-C)时,& 运算符将起作用。

在任何语言模式下,您都可以使用

(gdb) info address buf
Symbol "buf" is static storage at address 0x903278.

(输出与您的代码不完全对应。)我写这个答案是因为即使是寻找其他语言答案的人(包括我自己)也发现了这个问题。人们也可以随时通过set language c切换到C模式,但更改后符号名称可能会有所不同。

The & operator will work when gdb is set to C language mode (and Objective-C).

In any language mode you can use

(gdb) info address buf
Symbol "buf" is static storage at address 0x903278.

(The output does not correspond exactly to your code.) I am writing this answer because this question is found even by people looking for the answer for other languages (including myself). One can also always switch to the C mode by set language c, but the symbol names may be different after this change.

故事灯 2024-10-15 03:25:16

如果您在 gdb 中输入以下内容,您将获得地址:

start
p &buf

如以下记录所示:

pax$ gdb ./qq.exe
GNU gdb 6.8.0.20080328-cvs (cygwin-special)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-cygwin"...
(gdb) start
Breakpoint 1 at 0x401144: file qq.c, line 2.
Starting program: /home/pax/qq.exe
[New thread 2912.0xf9c]
[New thread 2912.0x518]
main () at qq.c:2
2       int main (int argc, char **argv) {
(gdb) p &buf
$1 = (char (*)[100]) 0x22ccd0
(gdb)

If you enter the following into gdb, you'll get the address:

start
p &buf

as in the following transcript:

pax$ gdb ./qq.exe
GNU gdb 6.8.0.20080328-cvs (cygwin-special)
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-cygwin"...
(gdb) start
Breakpoint 1 at 0x401144: file qq.c, line 2.
Starting program: /home/pax/qq.exe
[New thread 2912.0xf9c]
[New thread 2912.0x518]
main () at qq.c:2
2       int main (int argc, char **argv) {
(gdb) p &buf
$1 = (char (*)[100]) 0x22ccd0
(gdb)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文