GDB - How does it know about function calling stack?

user218867

When use gdb to debug assembly program, bt will print the calling stack.

The questions are:

  • (a) Does gdb know about that according to rbp values stored in register for current function, and in stack for previous rbp values?
  • If yes, (b-1) how gdb know which function it is according to a rbp value? (b-2) Is the mapping between stack base & function stored in executable file when -g option is specified on compiling? (b-3) And how to read that mapping data, with readelf? Which part?
  • If no, (c) then how gdb track the function calling stack?
Ross Ridge

Debuggers like GDB have two primary means of walking the stack in order to print a backtrace. They either assume the value in the frame pointer register (RBP) is a pointer to start of a linked list of stack frames, or they use special unwind info stored in the executable that describes how to walk (unwind) the stack.

Using the frame pointer

When using the frame pointer, the assumption is that it points to where the current function saved the value of its caller's frame pointer. It also assumes that just before that saved frame pointer is where the return address for the current function is stored. So that's how it knows both what the RBP value of the calling function was, and what function called the current function, which it can easily determine from the return address. It can then find all the previous stack frames and functions on the stack by walking the linked RBP values.

However, this assumes that functions use the frame pointer this way, and generally there's no guarantee that they will. Basically it's assuming that the function prologue and epilogue looks something like this:

func:
    push %rbp         # save previous frame pointer
    mov  %rsp, %rbp   # new frame pointer points to previous value
    sub  $24, %rsp    # allocate stack space for this funciton

    ...

    pop %rbp          # restore previous frame pointer
    ret

But when optimizing many compilers won't do this, because they rarely need to use a frame pointer and instead will treat RBP like any other general purpose register and use it for something else.

Using unwind info

So to generate a backtrace across functions that don't use RBP as a frame pointer, a debugger can potentially use unwind information. This is special data stored in executables (and dynamic libraries) that describes exactly how to virtually undo all the stack operations performed by a function at any point in the execution of that function. The format and location of the unwind info varies based on the executable format and CPU type. For ELF x86-64 executables the unwind info is stored in the .eh_frame section in a format based on the DWARF debugging format's unwind info. This format is too complex to describe here, but you can read the System V AMD64 ABI for more details.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

GDB - How does it know about function calling stack?

From Dev

What mechanism does gdb use to know where to "finish" a function call?

From Dev

gdb and current function stack

From Dev

How does php know about wordpress functions

From Dev

How does TFS know about nuget?

From Dev

How does REST know about the service schema?

From Dev

How does InvokeMember know about the HighPart property?

From Dev

How to know if gdb is installed?

From Dev

How does GDB know where an executable has been relocated?

From Dev

How does linux know when to allocate more pages to a call stack?

From Dev

Two buttons calling the same function, how to know which button called

From Dev

How do operating systems know which process is calling a function?

From Dev

How does debugger know function names?

From Dev

How does the compiler/interpreter know a function is asynchronous?

From Dev

How does Apache Spark know about HDFS data nodes?

From Dev

How does Android Studio know about new dependency versions?

From Dev

does anyone know how to go about fixing this issue i ran into?

From Dev

How to let TypeScript know about a function attached the the global namespace

From Dev

How to call function when i can't know about the signature?

From Dev

Does anyone know about char a[' ']?

From Dev

Does anyone know about char a[' ']?

From Dev

Navigate using function call stack in gdb

From Dev

gdb reports Segmentation fault - how to know where?

From Dev

C++: How does the compiler know how much memory to allocate for each stack frame?

From Dev

how to interpret stack memory from gdb

From Dev

How does the caller of a function know whether Return Value Optimization was used?

From Dev

How does the compiler know which entry in vtable corresponds to a virtual function?

From Dev

How does "BeginPaint" function know the update region - windows programming?

From Dev

How does the wait function know which thread information to choose from

Related Related

  1. 1

    GDB - How does it know about function calling stack?

  2. 2

    What mechanism does gdb use to know where to "finish" a function call?

  3. 3

    gdb and current function stack

  4. 4

    How does php know about wordpress functions

  5. 5

    How does TFS know about nuget?

  6. 6

    How does REST know about the service schema?

  7. 7

    How does InvokeMember know about the HighPart property?

  8. 8

    How to know if gdb is installed?

  9. 9

    How does GDB know where an executable has been relocated?

  10. 10

    How does linux know when to allocate more pages to a call stack?

  11. 11

    Two buttons calling the same function, how to know which button called

  12. 12

    How do operating systems know which process is calling a function?

  13. 13

    How does debugger know function names?

  14. 14

    How does the compiler/interpreter know a function is asynchronous?

  15. 15

    How does Apache Spark know about HDFS data nodes?

  16. 16

    How does Android Studio know about new dependency versions?

  17. 17

    does anyone know how to go about fixing this issue i ran into?

  18. 18

    How to let TypeScript know about a function attached the the global namespace

  19. 19

    How to call function when i can't know about the signature?

  20. 20

    Does anyone know about char a[' ']?

  21. 21

    Does anyone know about char a[' ']?

  22. 22

    Navigate using function call stack in gdb

  23. 23

    gdb reports Segmentation fault - how to know where?

  24. 24

    C++: How does the compiler know how much memory to allocate for each stack frame?

  25. 25

    how to interpret stack memory from gdb

  26. 26

    How does the caller of a function know whether Return Value Optimization was used?

  27. 27

    How does the compiler know which entry in vtable corresponds to a virtual function?

  28. 28

    How does "BeginPaint" function know the update region - windows programming?

  29. 29

    How does the wait function know which thread information to choose from

HotTag

Archive