dbbd
dbbd

Reputation: 894

how to find all objects (class objects/structs) of a C++ executable

Is there a way, maybe using nm, or gdb, that will let me create a list of all the object types that an executable contains?

To clarify, I have the source code. I need a method for figuring out all the class/struct sizes that are used at runtime. So this is probably a two part problem:

  1. create a list of all classes/structs
  2. use sizeof() on each of the items on the list, in gdb.

Upvotes: 1

Views: 2200

Answers (3)

James Moore
James Moore

Reputation: 9026

It's easy to get a list of types from gdb. You just want info types and then ptype if you want to drill down into the type (limiting it to types matching a string just to keep this small):

(gdb) info types Q
All types matching regular expression "Q":

File foo.cpp:
Qq;
(gdb) ptype Qq
type = class Qq {
  private:
    int qx;

  public:
    Qq(int);
    std::__cxx11::string something(std::__cxx11::list<int, std::allocator<int> >);
    int getQ(void);
}

And sizeof tells you how big the structure is (of course, it's the structure itself, so this may or may not be all that useful):

(gdb) p sizeof(Qq)
$1 = 4
(gdb) 

You'll probably want to run gdb in a script and parse the output somehow.

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477140

"Types" aren't a property of machine code. They're a property of a high-level, abstract language, which is compiled into machine code. Unless the compiler makes specific arrangements for you to recover information about the source program, type information generally doesn't exist at all.

Upvotes: 9

Kishore Kumar
Kishore Kumar

Reputation: 12874

http://www.hex-rays.com/products/ida/index.shtml : DeCompiler for C++

You will usually not get good C++ out of a binary unless you compiled in debugging information. Prepare to spend a lot of manual labor reversing the code.

If you didn't strip the binaries there is some hope as IDA Pro can produce C-alike code for you to work with.

Upvotes: 2

Related Questions