Post by Florian WeimerPost by pallav singhPlease do let me know few scenarios(or Link discussing it), when
function boundaries are not clearly defined.
Mainly inlining.
Post by pallav singhHow does GDB and other debuggers handle it on optimized gcc code on
Linux X86_64.
There is special DWARF support for inlined functions. However, at
present, optimized code can be quite difficult to debug with GDB
because intermediate values are often gone when you need to inspect
them.
i am wondering why gcc is not maintaining the frame boundaries when ever we have Optimization Level > 0, even i am giving -fno-optimize-sibling-calls -fno-omit-frame-pointer on X86_64.
Is there a know gcc Flag we need to use in X86_64 for have Frame Pointer, or i have messed something in my code.
#include <execinfo.h>
#include <stddef.h>
#include <stdio.h>
struct layout
{
struct layout *__unbounded next;
long condition_register;
void *__unbounded return_address;
};
/* Get some notion of the current stack. Need not be exactly the top
of the stack, just something somewhere in the current frame. */
#ifndef CURRENT_STACK_FRAME
# define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
#endif
/* By default we assume that the stack grows downward. */
#ifndef INNER_THAN
# define INNER_THAN <
#endif
/* By default assume the `next' pointer in struct layout points to the
next struct layout. */
#ifndef ADVANCE_STACK_FRAME
# define ADVANCE_STACK_FRAME(next) BOUNDED_1 ((struct layout *) (next))
#endif
/* By default, the frame pointer is just what we get from gcc. */
#ifndef FIRST_FRAME_POINTER
# define FIRST_FRAME_POINTER __builtin_frame_address (0)
#endif
/* Need to handle -fbounded-pointers. */
#define BOUNDED_N(PTR, N) (PTR)
#define BOUNDED_1(PTR) BOUNDED_N (PTR, 1)
/* Store up to SIZE return address of the current program state in
ARRAY and return the exact number of values stored. */
int
backtrace (array, size)
void **array;
int size;
{
int count = 0;
struct layout *current;
void *__unbounded top_frame;
void *__unbounded top_stack;
void *__libc_stack_end;
top_frame = FIRST_FRAME_POINTER;
top_stack = CURRENT_STACK_FRAME;
/* We skip the call to this function, it makes no sense to record it. */
current = BOUNDED_1 ((struct layout *) top_frame);
while (count < size)
{
/* This means the address is out of range. Note that for the
toplevel we see a frame pointer with value NULL which clearly is
out of range. */
if ((void *) current INNER_THAN top_stack ||
!((void *) current INNER_THAN __libc_stack_end))
break;
array[count++] = current->return_address;
current = ADVANCE_STACK_FRAME (current->next);
}
return count;
}
void __attribute__ ((noinline)) show_backtrace()
{
#define STACK_SIZE 512
static long long frame_stack[STACK_SIZE];
long nTotalFrames = 0;
long i = 0;
nTotalFrames = backtrace ((void **)&frame_stack, STACK_SIZE);
printf(": investigation callstack size %d listing.\n", nTotalFrames);
for (i = 0; i < nTotalFrames; i++) {
printf("Frame Number = %d pointer = %p \n", i , frame_stack[i]);
}
}
void __attribute__ ((noinline)) function0() { show_backtrace(); }
void __attribute__ ((noinline)) function1() { function0(); }
void __attribute__ ((noinline)) function2() { function1(); }
void __attribute__ ((noinline)) function3() { function2(); }
void __attribute__ ((noinline)) function4() { function3(); }
void __attribute__ ((noinline)) function5() { function4(); }
void __attribute__ ((noinline)) function6() { function5(); }
void __attribute__ ((noinline)) function7() { function6(); }
int main(int argc, char **argv)
{
function7 ();
return 0;
}
Post by Florian WeimerPost by pallav singhgcc -rdynamic -g frame.c -o frame -L/usr/local/lib -ldl
./frame
investigation callstack size 11 listing.
Frame Number = 0 pointer = (nil)
Frame Number = 1 pointer = 0x7fffb484b300
Frame Number = 2 pointer = 0x7fffb484b310
Frame Number = 3 pointer = 0x7fffb484b320
Frame Number = 4 pointer = 0x7fffb484b330
Frame Number = 5 pointer = 0x7fffb484b340
Frame Number = 6 pointer = 0x7fffb484b350
Frame Number = 7 pointer = 0x7fffb484b360
Frame Number = 8 pointer = 0x7fffb484b380
Frame Number = 9 pointer = 0x7fffb484b468
Frame Number = 10 pointer = 0x400780
Post by Florian WeimerPost by pallav singhgcc -rdynamic -g frame.c -o frame -L/usr/local/lib -ldl -fno-optimize-sibling-calls -fno-omit-frame-pointer -O1
./frame investigation callstack size 0 listing.
Thanks