Search code examples
cgdbsizeofcoredump

Can I find a structure with certain size in c source?


I'm trying to debug one core dump (mainly using gdb) and all I found out so far, is that that there is a structure of exactly 124 bytes, that is causing problems. Given all the sources of this program, is there a way to find that structure? (I mean is there a way to find structure, whose size is 124 bytes)

  • PS. I know exact place in memory of this structure, yet there is no clue about it's purpose if I look at it. It is also common structure, so I can make as many core dumps as I wish.

  • PS2. So far I tried:

    1. to use regular expression grep '^ *[a-zA-Z][^ ;,."()]* [a-zA-Z][^ ;,."()]*' * | grep -v 'return' | sed 's/[^:]*: *\([^ ]*\).*/\1/' | sort | uniq > tmp.txt , add p sizeof(x) to each found line and input to gdb.
    2. to use info variables in gdb, log output, extract variable types and add sizeof(x) to each type and output to gdb.

Solution

  • In a header file which is included by all the source file, define a macro,

    #define malloc(size) my_malloc(size, __FILE__, __LINE__)
    

    And then in the implementation:

    #undef malloc
    void * my_malloc(size_t size, const char* file, int line)
    {
        //if the size equal to 124 bytes, log it, then you will have a chance know where this kind of allocation happens, so you know the struct.
        if(124==size) printf(...);
    
        return malloc(size);
    }