Discussion:
Issue : Tail Recursion and X86_64 with optimized Mode (newbie question)
(too old to reply)
s***@gmail.com
2013-09-30 10:46:12 UTC
Permalink
Hi All,

i am Facing Issue in X86_64 on optimized Mode.

asm("mov %%rsp, %0" : "=r" (pCurStackFrame)); /* pCurStackFrame = (void*)rsp; */
asm("mov %%rbp, %0" : "=r" (pPrevStackFrame)); /* pPrevStackFrame = (void*)rbp;*/

I getting correct pPrevStackFrame Address in debug mode of X86_64, but in Optimized mode, i am getting Incorrect Value.
gcc --ver
Target: x86_64-redhat-linux
Configured with: ../gcc-4.4.5-20110214/

Please do let me know How to get correct pPrevStackFrame Address in Optimized mode of X86_64 or i am Hitting some Compiler related Optimization w.r.t Frame Pointer

Thanks
Pallav Singh
Florian Weimer
2013-09-30 12:26:16 UTC
Permalink
Post by s***@gmail.com
Please do let me know How to get correct pPrevStackFrame Address in
Optimized mode of X86_64 or i am Hitting some Compiler related
Optimization w.r.t Frame Pointer
You could try __builtin_return_address:

<http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html>

However, with optimization, function boundaries are not clearly
defined, so you might not get what you want.
pallav singh
2013-10-03 05:23:51 UTC
Permalink
Post by Florian Weimer
Post by s***@gmail.com
Please do let me know How to get correct pPrevStackFrame Address in
Optimized mode of X86_64 or i am Hitting some Compiler related
Optimization w.r.t Frame Pointer
<http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html>
However, with optimization, function boundaries are not clearly
defined, so you might not get what you want.
Thanks for the Link.

Please do let me know few scenarios(or Link discussing it), when function boundaries are not clearly defined.

How does GDB and other debuggers handle it on optimized gcc code on Linux X86_64.

Thanks
Pallav Singh
Florian Weimer
2013-10-03 21:31:35 UTC
Permalink
Post by pallav singh
Please do let me know few scenarios(or Link discussing it), when
function boundaries are not clearly defined.
Mainly inlining.
Post by pallav singh
How 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.
pallav singh
2013-10-04 10:23:38 UTC
Permalink
Post by Florian Weimer
Post by pallav singh
Please do let me know few scenarios(or Link discussing it), when
function boundaries are not clearly defined.
Mainly inlining.
Post by pallav singh
How 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 Weimer
Post by pallav singh
gcc -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 Weimer
Post by pallav singh
gcc -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
pallav singh
2013-10-06 14:04:24 UTC
Permalink
Post by Florian Weimer
Post by pallav singh
Please do let me know few scenarios(or Link discussing it), when
function boundaries are not clearly defined.
Mainly inlining.
Post by pallav singh
How 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.
Hi ,

can we get the starting address the of program i.e main and its address range in Optimized Gcc Code on X86_64.

Thanks
Pallav Singh
pallav singh
2013-10-16 19:38:44 UTC
Permalink
Post by pallav singh
Post by Florian Weimer
Post by pallav singh
Please do let me know few scenarios(or Link discussing it), when
function boundaries are not clearly defined.
Mainly inlining.
Post by pallav singh
How 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.
Hi ,
can we get the starting address the of program i.e main and its address range in Optimized Gcc Code on X86_64.
Thanks
Pallav Singh
Please provide link at starting point to start on extracting information from executable using dwarf format.

Kindly also let me know if there is any open-source group regarding the same.

Thanks
Pallav Singh

Loading...