53

For instance, I know that 0x46767f0 belongs to an NSString*, is there any way I can find out what NSString it is to help me find some bugs I'm after?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Coocoo4Cocoa
  • 48,756
  • 50
  • 150
  • 175
  • Reverse question: [gdb - Find the exact address of variable Buf - Stack Overflow](https://stackoverflow.com/questions/4462915/find-the-exact-address-of-variable-buf) ■ To do in gdb Python API: [GDB Python API: Getting symbol name from address - Stack Overflow](https://stackoverflow.com/questions/47916564/gdb-python-api-getting-symbol-name-from-address?noredirect=1&lq=1) ■ For local variables: [c++ - Why can't GDB find symbol from address with info symbol? - Stack Overflow](https://stackoverflow.com/questions/61507267/why-cant-gdb-find-symbol-from-address-with-info-symbol) – user202729 Dec 14 '21 at 05:23

5 Answers5

72

I believe you're looking for:

info symbol <addresss>

Print the name of a symbol which is stored at the address addr. If no symbol is stored exactly at addr, GDB prints the nearest symbol and an offset from it.

Example:

(gdb) info symbol 0x400225
_start + 5 in section .text of /tmp/a.out

(gdb) info symbol 0x2aaaac2811cf
__read_nocancel + 6 in section .text of /usr/lib64/libc.so.6

You can read more about it here.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
Brian Gianforcaro
  • 26,564
  • 11
  • 58
  • 77
16

gdb> list *0xAABBCCDD

That tells you the filename and line number.

Jay Ram
  • 219
  • 3
  • 2
6

If it is a stack variable, there is no way that I am aware to do it. Otherwise, try p/a <pointer symbol or address> and it will print the symbol name (or offset to the nearest symbol name).

Simon Broadhead
  • 3,483
  • 21
  • 19
3

addr2line

This Binutils utility can handle any symbol address, including variables and function names.

It is non-interactive by default, which is useful in some cases, when doing post-mortems.

main.c

#include <stdio.h>

int myvar;

int main(void) {
    printf("myvar = %d\n", myvar);
}

Compile and disassemble:

gcc -O 0 -g gdb3 -o main -pedantic-errors -std=c89 -Wextra main.c
readelf -s tmp.out  | grep -E ' (main|myvar)'

Gives:

55: 0000000000201014     4 OBJECT  GLOBAL DEFAULT   24 myvar
65: 000000000000064a    32 FUNC    GLOBAL DEFAULT   14 main

And now we can try:

addr2line -e main 201014 64a

which gives:

/full/path/to/main.c:3
/full/path/to/main.c:5

Boost stack trace library uses it for example to show stack trace lines: print call stack in C or C++

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
3

po 0x46767f0

will send a -description message to the object. That will print out the contents of your NSString but I suggest using Brian's answer to check the contents of your address before you send random messages to random addresses.

Rog
  • 17,070
  • 9
  • 50
  • 73