pythonc++gdbgdb-python

python gdb extension: i'm trying to get the calling line and file info for a macro call but getting wrong values due to comments


I have the following C++ code:

main.cpp:

#include "a.h"
int main(){

    int x[3];
    some_macro(x)
    // comment
    // comment
    return x[2];
}

a.h:

template <typename T>
inline void some_macro_break_point()
{
}

#define some_macro(name)                                         \
{                                                                \
    some_macro_break_point();                     \
}

and using GDB python extension I'm trying to get the line and file where the macro was called while using GDB, given the fact that I have an automatic break point over some_macro_break_point

I do the following when hitting the break point:

frame = gdb.newest_frame()
frame = frame.older()
pc = frame.pc()
symtab_line = gdb.find_pc_line(pc)
print(">>>> ", symtab_line.symtab.fullname() + ":" + str(symtab_line.line), "<<<<\n") 

In the case I have comments under the macro call in the main, I will get the line + <number_of_comments_under_it>

How can I detect the exact line the macro was called in the main correctly? When I don't have comments I get the line with the offset of 1.

I tried to get the symtab of the gdb frame but values were affected by the fact that there were comments under the macro call.


Solution

  • ssbssa> Try subtracting 1 from pc, like pc = frame.pc() - 1 Ruba Rushrush> substracting 1 from the pc worked! but what's the logic here?

    The logic is: when your breakpoint fired, the frame on the stack is some_macro_break_point, and the frame.older().pc() points to the location in main where some_macro_break_point() would return.

    That location is after the location from which main called some_macro_break_point(), namely it's the next executable line.

    This all would become clearer if you disas/s main and print frame.older().pc().