Re: GDB interactions with GCC

Jean Christophe Beyler
2009-11-03T17:22:25Z

> That is an simplified answer based on my limited knowledge of the subject.

Thanks, it's a start.

I am first gathering information and these seem to be like huge newbie
questions, for which I apologize. Can someone explain how GDB does to
remember what arguments were used in a backtrace ? Is it necessary
that they all reside on the stack or does GDB copy them somewhere for
later use, it the case of architectures that do not need to pass the
arguments on the stack?

Technically, on my architecture, if we had:

int bar (int a)
{
return a;
}

int foo (int a, int b)
{
return a + b;
}

the compiler would generate for foo something like:

...
add FirstInputRegister, FirstInputRegister, SecondInputRegister
call bar
...

Therefore, if I put a breakpoint at bar, if I look at
FirstInputRegister, I would have a+b but if I backtraced to foo, how
would GDB remember the values of a and b.

In this case, must GCC generate something like this instead :

...
store FirstInputRegister, 0(stack_pointer)
store SecondInputRegister, 8(stack_pointer) #8 is just an offset,
let's suppose int is 8 bytes...
add FirstInputRegister, FirstInputRegister, SecondInputRegister
call bar
...

so that the arguments reside on the stack so that GDB can get them at
a later date ?

If so, doesn't that mean that if we want GDB on an architecture that
does not require any stack copies, we still have to generate those
uselessly (except for GDB ;-)) ?

Thanks again,
Jc

On Mon, Nov 2, 2009 at 6:16 PM, David Daney <ddaney@caviumnetworks.com> wrote:
> Jean Christophe Beyler wrote:
> [...]
>>
>> For example, on my architecture, the return address is passed in one
>> register that the function can push on the stack if it's necessary (if
>> a call happens) but not if it's a leaf function. The local variables
>> are also sent via registers and therefore, potentially are never
>> stored on the stack.
>
> This is similar to MIPS which is supported by gdb.
>
> [...]
>
>> And if someone can answer these questions:
>>
>> - It seems to me that GCC outputs debug information that helps GDB
>> follow the course of the program:
>>   - With this information, is GDB able to follow where the local
>> variables are kept (register or stack) depending on what the program
>> is doing ?
>
> Usually.
>
>>   - Does GCC need to output anything in particular to allow GDB to
>> give the information of the backtrace for example?
>>
>
> Yes.
>
>> In other words:
>>
>> - Does the ABI need to store the stack pointer for each frame in order
>> for GDB to give all the backtrace, variable information? Or does GDB
>> keep track of this independantly of the ABI.
>
> For some (many) archectures, the compiler emits debug information describing
> the information needed to generate a backtrace.  Look at .debug_frame
> sections in DWARF for example.  Given a lack of frame meta-data, you can
> have target specific code that does ad hoc stack and code examination to try
> to generate a backtrace.
>
>
>>
>> - Does GDB require that the ABI store arguments at some point on the
>> stack in order to provide information about a function calls
>> arguments? I would assume not because otherwise register-based ABIs
>> would have to redundantly copy things on the stack for GDB use...
>>
>
> The debug data emitted by the compiler should allow gdb to track variable
> locations.  For most Linux systems this is specified by DWARF.
>
> That is an simplified answer based on my limited knowledge of the subject.
>
> David Daney
>
>