[libunwind] r292722 - DWARF: allow enabling tracing at runtime
Saleem Abdulrasool via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 21 08:22:57 PST 2017
Author: compnerd
Date: Sat Jan 21 10:22:57 2017
New Revision: 292722
URL: http://llvm.org/viewvc/llvm-project?rev=292722&view=rev
Log:
DWARF: allow enabling tracing at runtime
Introduce `logDWARF` and the associated environment variable
`LIBUNWIND_PRINT_DWARF` to trace the CFI instructions.
Modified:
libunwind/trunk/src/DwarfParser.hpp
libunwind/trunk/src/libunwind.cpp
Modified: libunwind/trunk/src/DwarfParser.hpp
URL: http://llvm.org/viewvc/llvm-project/libunwind/trunk/src/DwarfParser.hpp?rev=292722&r1=292721&r2=292722&view=diff
==============================================================================
--- libunwind/trunk/src/DwarfParser.hpp (original)
+++ libunwind/trunk/src/DwarfParser.hpp Sat Jan 21 10:22:57 2017
@@ -23,6 +23,14 @@
#include "AddressSpace.hpp"
+extern "C" bool logDWARF();
+
+#define _LIBUNWIND_TRACE_DWARF(...) \
+ do { \
+ if (logDWARF()) \
+ fprintf(stderr, __VA_ARGS__); \
+ } while (0)
+
namespace libunwind {
/// CFI_Parser does basic parsing of a CFI (Call Frame Information) records.
@@ -364,13 +372,12 @@ bool CFI_Parser<A>::parseInstructions(A
const CIE_Info &cieInfo, pint_t pcoffset,
PrologInfoStackEntry *&rememberStack,
PrologInfo *results) {
- const bool logDwarf = false;
pint_t p = instructions;
pint_t codeOffset = 0;
PrologInfo initialState = *results;
- if (logDwarf)
- fprintf(stderr, "parseInstructions(instructions=0x%0" PRIx64 ")\n",
- (uint64_t)instructionsEnd);
+
+ _LIBUNWIND_TRACE_DWARF("parseInstructions(instructions=0x%0" PRIx64 ")\n",
+ static_cast<uint64_t>(instructionsEnd));
// see DWARF Spec, section 6.4.2 for details on unwind opcodes
while ((p < instructionsEnd) && (codeOffset < pcoffset)) {
@@ -386,35 +393,30 @@ bool CFI_Parser<A>::parseInstructions(A
++p;
switch (opcode) {
case DW_CFA_nop:
- if (logDwarf)
- fprintf(stderr, "DW_CFA_nop\n");
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_nop\n");
break;
case DW_CFA_set_loc:
codeOffset =
addressSpace.getEncodedP(p, instructionsEnd, cieInfo.pointerEncoding);
- if (logDwarf)
- fprintf(stderr, "DW_CFA_set_loc\n");
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_set_loc\n");
break;
case DW_CFA_advance_loc1:
codeOffset += (addressSpace.get8(p) * cieInfo.codeAlignFactor);
p += 1;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_advance_loc1: new offset=%" PRIu64 "\n",
- (uint64_t)codeOffset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc1: new offset=%" PRIu64 "\n",
+ static_cast<uint64_t>(codeOffset));
break;
case DW_CFA_advance_loc2:
codeOffset += (addressSpace.get16(p) * cieInfo.codeAlignFactor);
p += 2;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_advance_loc2: new offset=%" PRIu64 "\n",
- (uint64_t)codeOffset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc2: new offset=%" PRIu64 "\n",
+ static_cast<uint64_t>(codeOffset));
break;
case DW_CFA_advance_loc4:
codeOffset += (addressSpace.get32(p) * cieInfo.codeAlignFactor);
p += 4;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_advance_loc4: new offset=%" PRIu64 "\n",
- (uint64_t)codeOffset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc4: new offset=%" PRIu64 "\n",
+ static_cast<uint64_t>(codeOffset));
break;
case DW_CFA_offset_extended:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -426,21 +428,18 @@ bool CFI_Parser<A>::parseInstructions(A
}
results->savedRegisters[reg].location = kRegisterInCFA;
results->savedRegisters[reg].value = offset;
- if (logDwarf)
- fprintf(stderr,
- "DW_CFA_offset_extended(reg=%" PRIu64 ", offset=%" PRId64 ")\n",
- reg, offset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended(reg=%" PRIu64 ", "
+ "offset=%" PRId64 ")\n",
+ reg, offset);
break;
case DW_CFA_restore_extended:
reg = addressSpace.getULEB128(p, instructionsEnd);
- ;
if (reg > kMaxRegisterNumber) {
_LIBUNWIND_LOG("malformed DWARF DW_CFA_restore_extended, reg too big");
return false;
}
results->savedRegisters[reg] = initialState.savedRegisters[reg];
- if (logDwarf)
- fprintf(stderr, "DW_CFA_restore_extended(reg=%" PRIu64 ")\n", reg);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_extended(reg=%" PRIu64 ")\n", reg);
break;
case DW_CFA_undefined:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -449,8 +448,7 @@ bool CFI_Parser<A>::parseInstructions(A
return false;
}
results->savedRegisters[reg].location = kRegisterUnused;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
break;
case DW_CFA_same_value:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -465,8 +463,7 @@ bool CFI_Parser<A>::parseInstructions(A
results->savedRegisters[reg].location = kRegisterUnused;
// set flag to disable conversion to compact unwind
results->sameValueUsed = true;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_same_value(reg=%" PRIu64 ")\n", reg);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_same_value(reg=%" PRIu64 ")\n", reg);
break;
case DW_CFA_register:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -483,9 +480,8 @@ bool CFI_Parser<A>::parseInstructions(A
results->savedRegisters[reg].value = (int64_t)reg2;
// set flag to disable conversion to compact unwind
results->registersInOtherRegisters = true;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n",
- reg, reg2);
+ _LIBUNWIND_TRACE_DWARF(
+ "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n", reg, reg2);
break;
#if !defined(_LIBUNWIND_NO_HEAP)
case DW_CFA_remember_state:
@@ -498,8 +494,7 @@ bool CFI_Parser<A>::parseInstructions(A
} else {
return false;
}
- if (logDwarf)
- fprintf(stderr, "DW_CFA_remember_state\n");
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_remember_state\n");
break;
case DW_CFA_restore_state:
if (rememberStack != NULL) {
@@ -510,8 +505,7 @@ bool CFI_Parser<A>::parseInstructions(A
} else {
return false;
}
- if (logDwarf)
- fprintf(stderr, "DW_CFA_restore_state\n");
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_state\n");
break;
#endif
case DW_CFA_def_cfa:
@@ -523,9 +517,8 @@ bool CFI_Parser<A>::parseInstructions(A
}
results->cfaRegister = (uint32_t)reg;
results->cfaRegisterOffset = (int32_t)offset;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_def_cfa(reg=%" PRIu64 ", offset=%" PRIu64 ")\n",
- reg, offset);
+ _LIBUNWIND_TRACE_DWARF(
+ "DW_CFA_def_cfa(reg=%" PRIu64 ", offset=%" PRIu64 ")\n", reg, offset);
break;
case DW_CFA_def_cfa_register:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -534,26 +527,23 @@ bool CFI_Parser<A>::parseInstructions(A
return false;
}
results->cfaRegister = (uint32_t)reg;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_def_cfa_register(%" PRIu64 ")\n", reg);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_register(%" PRIu64 ")\n", reg);
break;
case DW_CFA_def_cfa_offset:
results->cfaRegisterOffset = (int32_t)
addressSpace.getULEB128(p, instructionsEnd);
results->codeOffsetAtStackDecrement = (uint32_t)codeOffset;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_def_cfa_offset(%d)\n",
- results->cfaRegisterOffset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset(%d)\n",
+ results->cfaRegisterOffset);
break;
case DW_CFA_def_cfa_expression:
results->cfaRegister = 0;
results->cfaExpression = (int64_t)p;
length = addressSpace.getULEB128(p, instructionsEnd);
p += length;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_def_cfa_expression(expression=0x%" PRIx64
- ", length=%" PRIu64 ")\n",
- results->cfaExpression, length);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_expression(expression=0x%" PRIx64
+ ", length=%" PRIu64 ")\n",
+ results->cfaExpression, length);
break;
case DW_CFA_expression:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -565,10 +555,10 @@ bool CFI_Parser<A>::parseInstructions(A
results->savedRegisters[reg].value = (int64_t)p;
length = addressSpace.getULEB128(p, instructionsEnd);
p += length;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_expression(reg=%" PRIu64
- ", expression=0x%" PRIx64 ", length=%" PRIu64 ")\n",
- reg, results->savedRegisters[reg].value, length);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_expression(reg=%" PRIu64 ", "
+ "expression=0x%" PRIx64 ", "
+ "length=%" PRIu64 ")\n",
+ reg, results->savedRegisters[reg].value, length);
break;
case DW_CFA_offset_extended_sf:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -581,10 +571,9 @@ bool CFI_Parser<A>::parseInstructions(A
addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
results->savedRegisters[reg].location = kRegisterInCFA;
results->savedRegisters[reg].value = offset;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_offset_extended_sf(reg=%" PRIu64
- ", offset=%" PRId64 ")\n",
- reg, offset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended_sf(reg=%" PRIu64 ", "
+ "offset=%" PRId64 ")\n",
+ reg, offset);
break;
case DW_CFA_def_cfa_sf:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -596,18 +585,16 @@ bool CFI_Parser<A>::parseInstructions(A
}
results->cfaRegister = (uint32_t)reg;
results->cfaRegisterOffset = (int32_t)offset;
- if (logDwarf)
- fprintf(stderr,
- "DW_CFA_def_cfa_sf(reg=%" PRIu64 ", offset=%" PRId64 ")\n", reg,
- offset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_sf(reg=%" PRIu64 ", "
+ "offset=%" PRId64 ")\n",
+ reg, offset);
break;
case DW_CFA_def_cfa_offset_sf:
results->cfaRegisterOffset = (int32_t)
(addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor);
results->codeOffsetAtStackDecrement = (uint32_t)codeOffset;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_def_cfa_offset_sf(%d)\n",
- results->cfaRegisterOffset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset_sf(%d)\n",
+ results->cfaRegisterOffset);
break;
case DW_CFA_val_offset:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -615,10 +602,9 @@ bool CFI_Parser<A>::parseInstructions(A
* cieInfo.dataAlignFactor;
results->savedRegisters[reg].location = kRegisterOffsetFromCFA;
results->savedRegisters[reg].value = offset;
- if (logDwarf)
- fprintf(stderr,
- "DW_CFA_val_offset(reg=%" PRIu64 ", offset=%" PRId64 "\n", reg,
- offset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset(reg=%" PRIu64 ", "
+ "offset=%" PRId64 "\n",
+ reg, offset);
break;
case DW_CFA_val_offset_sf:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -630,10 +616,9 @@ bool CFI_Parser<A>::parseInstructions(A
addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
results->savedRegisters[reg].location = kRegisterOffsetFromCFA;
results->savedRegisters[reg].value = offset;
- if (logDwarf)
- fprintf(stderr,
- "DW_CFA_val_offset_sf(reg=%" PRIu64 ", offset=%" PRId64 "\n",
- reg, offset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset_sf(reg=%" PRIu64 ", "
+ "offset=%" PRId64 "\n",
+ reg, offset);
break;
case DW_CFA_val_expression:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -645,16 +630,14 @@ bool CFI_Parser<A>::parseInstructions(A
results->savedRegisters[reg].value = (int64_t)p;
length = addressSpace.getULEB128(p, instructionsEnd);
p += length;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_val_expression(reg=%" PRIu64
- ", expression=0x%" PRIx64 ", length=%" PRIu64 ")\n",
- reg, results->savedRegisters[reg].value, length);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_val_expression(reg=%" PRIu64 ", "
+ "expression=0x%" PRIx64 ", length=%" PRIu64 ")\n",
+ reg, results->savedRegisters[reg].value, length);
break;
case DW_CFA_GNU_args_size:
length = addressSpace.getULEB128(p, instructionsEnd);
results->spExtraArgSize = (uint32_t)length;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_GNU_args_size(%" PRIu64 ")\n", length);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_args_size(%" PRIu64 ")\n", length);
break;
case DW_CFA_GNU_negative_offset_extended:
reg = addressSpace.getULEB128(p, instructionsEnd);
@@ -667,9 +650,8 @@ bool CFI_Parser<A>::parseInstructions(A
* cieInfo.dataAlignFactor;
results->savedRegisters[reg].location = kRegisterInCFA;
results->savedRegisters[reg].value = -offset;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n",
- offset);
+ _LIBUNWIND_TRACE_DWARF(
+ "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n", offset);
break;
default:
operand = opcode & 0x3F;
@@ -680,25 +662,22 @@ bool CFI_Parser<A>::parseInstructions(A
* cieInfo.dataAlignFactor;
results->savedRegisters[reg].location = kRegisterInCFA;
results->savedRegisters[reg].value = offset;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_offset(reg=%d, offset=%" PRId64 ")\n",
- operand, offset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_offset(reg=%d, offset=%" PRId64 ")\n",
+ operand, offset);
break;
case DW_CFA_advance_loc:
codeOffset += operand * cieInfo.codeAlignFactor;
- if (logDwarf)
- fprintf(stderr, "DW_CFA_advance_loc: new offset=%" PRIu64 "\n",
- (uint64_t)codeOffset);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc: new offset=%" PRIu64 "\n",
+ static_cast<uint64_t>(codeOffset));
break;
case DW_CFA_restore:
reg = operand;
results->savedRegisters[reg] = initialState.savedRegisters[reg];
- if (logDwarf)
- fprintf(stderr, "DW_CFA_restore(reg=%" PRIu64 ")\n", reg);
+ _LIBUNWIND_TRACE_DWARF("DW_CFA_restore(reg=%" PRIu64 ")\n",
+ static_cast<long>(operand));
break;
default:
- if (logDwarf)
- fprintf(stderr, "unknown CFA opcode 0x%02X\n", opcode);
+ _LIBUNWIND_TRACE_DWARF("unknown CFA opcode 0x%02X\n", opcode);
return false;
}
}
Modified: libunwind/trunk/src/libunwind.cpp
URL: http://llvm.org/viewvc/llvm-project/libunwind/trunk/src/libunwind.cpp?rev=292722&r1=292721&r2=292722&view=diff
==============================================================================
--- libunwind/trunk/src/libunwind.cpp (original)
+++ libunwind/trunk/src/libunwind.cpp Sat Jan 21 10:22:57 2017
@@ -371,5 +371,17 @@ bool logUnwinding() {
return log;
}
+_LIBUNWIND_HIDDEN
+bool logDWARF() {
+ // do manual lock to avoid use of _cxa_guard_acquire or initializers
+ static bool checked = false;
+ static bool log = false;
+ if (!checked) {
+ log = (getenv("LIBUNWIND_PRINT_DWARF") != NULL);
+ checked = true;
+ }
+ return log;
+}
+
#endif // NDEBUG
More information about the cfe-commits
mailing list