struct StackState { void** frames; int frame_count; int cur_frame = 0;
StackState(void** frames, int frame_count) : frames(frames), frame_count(frame_count) {}
};
static _Unwind_Reason_Code TraceFunction(_Unwind_Context* context, void* arg) { // The instruction pointer is pointing at the instruction after the return // call on all architectures. // Modify the pc to point at the real function.
uintptr_t ip = _Unwind_GetIP(context); if (ip != 0) { #ifdefined(__arm__) // If the ip is suspiciously low, do nothing to avoid a segfault trying // to access this memory. if (ip >= 4096) { // Check bits [15:11] of the first halfword assuming the instruction // is 32 bits long. If the bits are any of these values, then our // assumption was correct: // b11101 // b11110 // b11111 // Otherwise, this is a 16 bit instruction.
uint16_t value = (*reinterpret_cast<uint16_t*>(ip - 2)) >> 11; if (value == 0x1f || value == 0x1e || value == 0x1d) {
ip -= 4;
} else {
ip -= 2;
}
} #elifdefined(__aarch64__) // All instructions are 4 bytes long, skip back one instruction.
ip -= 4; #elifdefined(__riscv) // C instructions are the shortest at 2 bytes long. (Unlike thumb, it's // non-trivial to recognize C instructions when going backwards in the // instruction stream.)
ip -= 2; #elifdefined(__i386__) || defined(__x86_64__) // It's difficult to decode exactly where the previous instruction is, // so subtract 1 to estimate where the instruction lives.
ip--; #endif
}
char** backtrace_symbols(void* const* buffer, int size) { if (size <= 0) { return nullptr;
} // Do this calculation first in case the user passes in a bad value.
size_t ptr_size; if (__builtin_mul_overflow(sizeof(char*), size, &ptr_size)) { return nullptr;
}
// Get the size of the file.
off_t file_size = lseek(fd.get(), 0, SEEK_END); if (file_size <= 0) { return nullptr;
}
// The interface for backtrace_symbols indicates that only the single // returned pointer must be freed by the caller. Therefore, allocate a // buffer that includes the memory for the strings and all of the pointers. // Add one byte at the end just in case the file didn't end with a '\n'.
size_t symbol_data_size; if (__builtin_add_overflow(ptr_size, file_size, &symbol_data_size) ||
__builtin_add_overflow(symbol_data_size, 1, &symbol_data_size)) { return nullptr;
}
// Copy the string data into the buffer. char* cur_string = reinterpret_cast<char*>(&symbol_data[ptr_size]); // If this fails, the read won't read back the correct number of bytes.
lseek(fd.get(), 0, SEEK_SET);
ssize_t num_read = read(fd.get(), cur_string, file_size);
fd.reset(-1); if (num_read != file_size) {
free(symbol_data); return nullptr;
}
// Make sure the last character in the file is '\n'. if (cur_string[file_size] != '\n') {
cur_string[file_size++] = '\n';
}
for (int i = 0; i < size; i++) {
(reinterpret_cast<char**>(symbol_data))[i] = cur_string;
cur_string = strchr(cur_string, '\n'); if (cur_string == nullptr) {
free(symbol_data); return nullptr;
}
cur_string[0] = '\0';
cur_string++;
} returnreinterpret_cast<char**>(symbol_data);
}
// This function should do no allocations if possible. void backtrace_symbols_fd(void* const* buffer, int size, int fd) { if (size <= 0 || fd < 0) { return;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.