Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use GDB to find what function a memory address corresponds to

Tags:

c++

gdb

I am using google's heap checker to track down a memory leak. It gives me a stack trace such as:

Leak of 21 bytes in 1 objects allocated from:                                                                                                                                                                    @ 0xf6088241                                                                                                                                                                                                    @ 0xf60890d2                                                                                                                                                                                                    @ 0xf6089246                                                                                                                                                                                                    @ 0x8054781                                                                                                                                                                                                     @ 0x8054862                                                                                                                                                                                                     @ 0xf684ee76                                                                                                                                                                                                    @ 0xf684f343                                                                                                                                                                                                    @ 0x804be4c                                                                                                                                                                                                     @ 0x80544f6                                                                                                                                                                                                     @ 0xf5e52bb6                                                                                                                                                                                                    @ 0x804b101   

How do I determine what functions/lines of code these memory addresses correspond to?

like image 641
Nathaniel Flath Avatar asked Oct 03 '11 18:10

Nathaniel Flath


People also ask

How do I find the address of a function in gdb?

Use info symbol gdb command. 16 Examining the Symbol Table. This is the opposite of the info address command. You can use it to find out the name of a variable or a function given its address.

How do you find the function of an address?

We can get the address of a function by just writing the function's name without parentheses. Please refer function pointer in C for details. In C/C++, name of a function can be used to find address of function.

How do you find address memory?

In C, we can get the memory address of any variable or member field (of struct). To do so, we use the address of (&) operator, the %p specifier to print it and a casting of (void*) on the address. Note: We have used & before opengenus to get the address in form of a pointer.

Which command in gdb is used to find the type of variable?

The ptype [ARG] command will print the type. Show activity on this post. This question may be related: vtable in polymorphic class of C++ using gdb: (gdb) help set print object Set printing of object's derived type based on vtable info.


1 Answers

Use info symbol gdb command. 16 Examining the Symbol Table.

info symbol addr 

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:

(gdb) info symbol 0x54320 _initialize_vx + 396 in section .text 

This is the opposite of the info address command. You can use it to find out the name of a variable or a function given its address.

For dynamically linked executables, the name of executable or shared library containing the symbol is also printed:

(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 
like image 102
ks1322 Avatar answered Sep 30 '22 19:09

ks1322