S__J
S__J

Reputation: 11

Extracting structure member names from C File

I am writing a windbg extension to print contents of a structure using ExtRemoteData. I see that I need to keep changing my code as and when the structure changes.

Instead I think it would be more flexible if I can directly read the C file and parse my structure to get the structure member names.

Is there a tool/function I can read a C File, and enumerate the various elements of my structure? The C compiler internally is doing that, but I am not sure how I can extract that info.

Something like Tool.exe Name value pairs would contain info such as {(membername1,type1),(membername2,type2).... (membernameN,typeN)}

Upvotes: 1

Views: 411

Answers (4)

ussrhero
ussrhero

Reputation: 606

pykd has built-in clang parser, so it can get symbol information from C code:

src = '''

class Bike {
public:
    int color;      // color of the bike
    int gearCount;      // number of configurable gear
    Bike() {
        // bla bla
    }
   ~Bike() {
        // bla bla
   }
   void operate() {
       // bla bla
   }
};

'''
#the next print statements will get equal output
print( getTypeFromSource(src, 'Bike') )
print( typeInfo('compiled_module!Bike') )

Upvotes: 0

KRoy
KRoy

Reputation: 1406

I am not sure if it fits the windbg. But someone provided a way to use pykd. So I am mentioning a way to extract the metadata and load it in python.

I used the SWIG tool to extract the CSV metadata out of a C/C++ source file.

Suppose the C code contains structure like the following,

class Bike {
public:
    int color;      // color of the bike
    int gearCount;      // number of configurable gear
    Bike() {
        // bla bla
    }
    ~Bike() {
        // bla bla
    }
    void operate() {
        // bla bla
    }
};

Then it will generate the following CSV metadata,

Bike|color|int|variable|public|
Bike|gearCount|int|variable|public|
Bike|operate|void|function|public|f().

Now it is easy to parse the CSV file with cut, or awk or python if needed.

import csv
with open('bike.csv', 'rb') as csvfile:
    bike_metadata = csv.reader(csvfile, delimiter='|')
    # do your thing

Upvotes: 0

Thomas Weller
Thomas Weller

Reputation: 59218

Type information is typically included in PDB (program database) symbol files. There are public symbols and private symbols. You might need private symbols to get all information you want.

You can generate private PDBs not only for debug builds but also for release builds. It should only be a setting in your preferred IDE.

Once you have private symbols, you can read it with the DbgHelp API. Depending on what information you already have, e.g. SymFromName() sounds useful.

Although parsing a C file might also be an option, be aware that the source code file might already have changed, but the compiled DLL and PDBs haven't.

Upvotes: 0

Addy
Addy

Reputation: 731

There is more research needed here but as a shortcut you may want to consider scripting languages like python to do your parsing following refers to a python library which can do what you intend to do. parsing C code using python

Now comes the part of integrating python with windbg as an extension that's already available checkout http://pykd.codeplex.com/

Upvotes: 1

Related Questions