Search for a pointer to a single-user postmart in GDB (C ++)

I am doing a posthumous analysis of a broken program. I am on Linux (Ubuntu 12.04, x86), the code is written in C ++. The program uses some singletones that may contain valuable information. Is it possible to find a pointer to a singleton instance if it was created as follows:

SingletonType& SingletonType::getInstance(){ static SingletonType* instance = new SingletonType(); return *instance; } 

And if possible, how is this done in GDB?

+4
source share
3 answers

Run gdb with the main file and run the command

 disassemble SingletonType::getInstance 

In my test program, I found the instruction mov 0x<addr>, %eax at the end of the method. A print *(*(SingletonType**) <0xaddr>) should print the contents of your singleton structure.

+3
source

show modules 1 should probably tell you the base addresses, and instance , being statically distributed, should be visible in some objdump / nm report. Yes, hairy math.

An alternative would be to parse SingletonType::getInstance() and see which effective address loads in the initialization / return path.


1 Mmm can't find the exact match that I remember. info sharedlibrary will provide you more information.

+3
source

this is what i am doing and inside the kernel with gdb:

 (gdb) info var instance 

this will display all the addresses of all singleton instances, among which you will find one of SingletonType

 0x86aa960 SingletonType::getInstance()::instance 

Now that I have the address, you can print the reverse memory of your instance:

 (gdb) p *((SingletonType*)0x86aa960) 
0
source

Source: https://habr.com/ru/post/1491672/


All Articles