5

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?

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
techshack
  • 359
  • 2
  • 18

3 Answers3

4

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.

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
tfk
  • 402
  • 3
  • 9
  • 1
    I don't know why you wouldn't `dissassemble SingletonType::getInstance` directly? For one thing, depending on the base address, `disassemble ` might not even do the correct thing. – sehe Jul 16 '13 at 20:03
  • You are right. The given procedure gives me what I want but it is absolutely possible to disassemble by using the name of the function and it is also more elegant :) – techshack Jul 17 '13 at 07:50
  • Good point sehe. I removed the objdump part and updated the dissassemble command in my answer. – tfk Jul 17 '13 at 07:59
3

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.

sehe
  • 374,641
  • 47
  • 450
  • 633
1

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)