Search code examples
stack-unwindingdwarf

Getting value of stack pointer while stack unwinding with dwarf2


In DWARF2 debugging format, stack unwinding is supported with the help of CFI(Call Frame Information) present in .debug_frame section. This is precisely a table that keeps a rule for every register to get its value in previous frame. However, all of these rules relies on the fact that the registers are saved on stack at some location. This is not true for getting value of stack pointer in previous frame register when there is no frame-pointer. In such cases, stack pointer may not be saved on stack but just managed by incrementing and decrementing its value. However, there is no way in dwarf2 (or dwarf format in general) to indicate that the register value is an expression over its current value. So, my question is, how can one get the value of stack pointer during stack unwinding with dwarf2 debugging format (when no frame-pointer is there).

-BV


Solution

  • It seems that dwarf3 supports DW_CFA_val_offset for such a case. So, this can be used to record value of SP in previous frame based on a arithmetic expression over current value. Another possible solution could be to save stack pointer on stack itself (will work with dwarf2). This can be done at -O0 optimization level where the code efficiency is not that important.