[LLVMdev] Writing my own debugger... use __builtin_frame_address or is there something better?

John Smith xp0bpe at gmail.com
Sun Jan 4 07:02:30 PST 2015


Hi LLVM list,

Thanks for having me here.

I'm writing my own debugger for my (secret) language...

I don't know anything about LLVM beyond the general "big picture"... I haven't any real practical experience working with it... beyond just using XCode...

So... really the problem is... I'm generating some functions... by "compiling to C"... so my compiler just writes a plain ".cpp" text file. I've tried debugging the output in Xcode but it's a horrible experience. It's like a C++ developer having to  stepping through ASM instead of C...

So I thought "it won't be too hard to make my own debugger"... "all I need is the ability to get the current variables off the current function... and probably the variables from the calling functions... as long as I can do THAT... I can do the rest myself".

Something like this:


int Func(Type1* self, Type2* P) {
    DB_GetStackPointer(1); // "DB_" means its a function for debugging the code
                           // So we save the current stack pointer to a global variable
                           // and tell the debugger that we are on line 1 of the current file's source code.
    int N = 0;
    DB_Line(2);            // Tell the debugger that we have advanced to line 2 of our source code.
    Type2* Curr = GetFirst(P);
    DB_Line(3);
    while (Curr) {
        DB_Line(4);
        Type3* Tmp = SubFunc(Curr, self, nil);
        if (!Tmp) {
            PrintError("Error");
            return 0;
        }
        DB_Line(5);
        N++;
        DB_Line(6);
        Item = GetNext(Curr);
        DB_Line(7);
    };

    DB_Line(8);
    return N + 1;
}


All I need to do then... is implement DB_Line and DB_GetStackPointer. The idea is that DB_GetStackPointer will save the current stack pointer... and DB_Line will go to a func that lets me read the current variables off that stack pointer, and then send them via a socket/TCP-connection... to my debugger.

However... I've fooled around with __builtin_frame_address and... I can't figure out how to properly use it.


    int* FA1 = (int*)__builtin_frame_address(1);
    int P0_ = FA1[0];
    int P1_ = FA1[1];
    int P2_ = FA1[2];
    int P3_ = FA1[3];


Something like this... but NONE of these int variables contain the actual pointers stored in the calling function!

I'm looking for the value "Type2* Value = 0x25b1160"

But I just see values like this:

P0_	int	0xbffff758	0xbffff758
P1_	int	0x00008ec3	0x00008ec3
P2_	int	0x004040f8	0x004040f8
P3_	int	0x00426638	0x00426638

Any ideas?

Or is there some kind of inbuilt LLVM things to help me write my own debugger? Something better than __builtin_frame_address

I don't mind relying on LLVM... It doesn't need to be used outside of LLVM...






More information about the llvm-dev mailing list