Is there a simple DWARF CFI represenation for func

2019-02-22 08:36发布

问题:

I'm programming in a mix of C, C++ and assembly and I'd like to get reliable backtraces from any part of the code.

This mostly works fine for the C and C++ code since I can generate debugging info with -g, which for modern x86 compilers and platforms generates DWARF debugging information which means the final binary includes CFI (Call Frame Information). This information allows walking backwards though the functions in the current call stack. It can support complex scenarios such as functions without a base pointer and where rsp changes in dynamic ways. For C and C++ code I don't have to care: the compiler just generates the right CFI even for optimized code.

For the x86 assembly code, which I write in nasm, the CFI is a problem. Both nasm and the syntax-compatible competitor yasm have some limited support for generating DWARF info, but it mostly involves instruction pointer to line mapping and doens't include any CFI1 (indeed, the an assembly couldn't really produce this even if it wanted since assembly is too low level to unambiguously express the call/return semantics).

I'd like to add CFI for the assembly generated routines, but I don't need the full flexibility of the CFI to identify the CFA based on instruction-granular changes to rsp and so on: I am perfectly happy to set up a standard frame-pointer in rbp for each assembly function and leave it intact for the entire function. Does DWARF3 support this scenario with a simple bit of CFI information, hopefully one that I can generate with an assembler macro?


1 The experimental "nextgen" version of yasm does have support for CFI directives, but that project hasn't been updated in 5 years. There is an open issue to port this support back to the mainline version of yasm.