web-dev-qa-db-ja.com

変数Bufの正確なアドレスを見つける

参考として、次のコードを使用しています。

#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変数のアドレスを表示するにはどうすればよいですか?

23
Neefra

(gdb) p &a変数aのアドレスが必要な場合。ただし、変数がレジスタにキャッシュされる場合があります。その場合、GDBはaddress requested for identifier "a" which is in register $xxx

補足:getsを使用しないでください。 here を参照してください。

36

&演算子は、gdbがC言語モード(およびObjective-C)に設定されている場合に機能します。

どの言語モードでも使用できます

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

(出力はコードに正確に対応していません。)この質問は他の言語(自分を含む)の回答を探している人でも見つけられるため、この回答を書いています。 set language cで常にCモードに切り替えることもできますが、この変更後はシンボル名が異なる場合があります。

8
Vladimir F

以下を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)
4
paxdiablo