Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the pointer to a singleton postmortem in GDB (C++)

I am doing a postmortem analysis of a crashed program. I am on Linux (Ubuntu 12.04, x86), the code is written in C++. The Program is using some singletons that may contain valuable information. Is it possible to find the pointer to the instance of a singleton if it was created like this:

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

And if its is possible, how is it done in GDB?

like image 545
techshack Avatar asked Jul 16 '13 14:07

techshack


3 Answers

Run gdb with the core file, and run the command

disassemble  SingletonType::getInstance

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

like image 170
tfk Avatar answered Nov 12 '22 00:11

tfk


show modules1 should probably tell you the base addresses, and instance, being statically allocated, should be visible in some kind of objdump/nm report. Yeah hairy maths.

The alternative would be to disassemble SingletonType::getInstance() and see what effective address gets loaded in the initialization/return path.


1 Mmm can't find the exact match I was remembering. info sharedlibrary would get you most info.

like image 3
sehe Avatar answered Nov 12 '22 00:11

sehe


this is what I do, while inside the core with gdb:

(gdb) info var instance

this will list all the addresses of all the singletons instances, among which you will find the one of SingletonType

0x86aa960 SingletonType::getInstance()::instance

Now that I have the address you can print the your instance' pointed memory:

(gdb) p *((SingletonType*)0x86aa960)
like image 1
ellysisland Avatar answered Nov 11 '22 22:11

ellysisland