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

3

6

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?

Pink answered 16/7, 2013 at 14:43 Comment(2)
p/x &SingletonType::getInstance()?Altman
Nope, unfortunately you cannot do this whitout a running process.Pink
M
6

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.

Monostome answered 16/7, 2013 at 15:41 Comment(3)
I don't know why you wouldn't dissassemble SingletonType::getInstance directly? For one thing, depending on the base address, disassemble <address_from_objdump> might not even do the correct thing.Ardell
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 :)Pink
Good point sehe. I removed the objdump part and updated the dissassemble command in my answer.Monostome
A
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.

Ardell answered 16/7, 2013 at 14:52 Comment(0)
M
2

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 you have the address you can print your instance' pointed memory:

(gdb) p *((SingletonType*)0x86aa960)
Mcmurray answered 9/5, 2017 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.