Rename stacktrace_frame.col to column, set a proper sentinel value, and update stacktrace.to_string to not have a tailing newline

This commit is contained in:
Jeremy 2023-09-19 15:15:33 -04:00
parent 137dc78710
commit 5c3df2571e
No known key found for this signature in database
GPG Key ID: 19AA8270105E8EB4
10 changed files with 41 additions and 36 deletions

View File

@ -156,7 +156,7 @@ namespace cpptrace {
struct stacktrace_frame { struct stacktrace_frame {
uintptr_t address; uintptr_t address;
std::uint_least32_t line; std::uint_least32_t line;
std::uint_least32_t col; std::uint_least32_t column; // UINT_LEAST32_MAX if not present
std::string filename; std::string filename;
std::string symbol; std::string symbol;
bool operator==(const stacktrace_frame& other) const; bool operator==(const stacktrace_frame& other) const;

View File

@ -56,13 +56,13 @@ namespace cpptrace {
struct stacktrace_frame { struct stacktrace_frame {
uintptr_t address; uintptr_t address;
std::uint_least32_t line; std::uint_least32_t line;
std::uint_least32_t col; std::uint_least32_t column = UINT_LEAST32_MAX; // UINT_LEAST32_MAX if not present
std::string filename; std::string filename;
std::string symbol; std::string symbol;
bool operator==(const stacktrace_frame& other) const { bool operator==(const stacktrace_frame& other) const {
return address == other.address return address == other.address
&& line == other.line && line == other.line
&& col == other.col && column == other.column
&& filename == other.filename && filename == other.filename
&& symbol == other.symbol; && symbol == other.symbol;
} }
@ -86,6 +86,8 @@ namespace cpptrace {
inline const_iterator cbegin() const noexcept { return frames.cbegin(); } inline const_iterator cbegin() const noexcept { return frames.cbegin(); }
inline iterator end() noexcept { return frames.end(); } inline iterator end() noexcept { return frames.end(); }
inline const_iterator cend() const noexcept { return frames.cend(); } inline const_iterator cend() const noexcept { return frames.cend(); }
private:
CPPTRACE_API void print(std::ostream& stream, bool color, bool newline_at_end) const;
}; };
CPPTRACE_API raw_trace generate_raw_trace(std::uint32_t skip = 0); CPPTRACE_API raw_trace generate_raw_trace(std::uint32_t skip = 0);

View File

@ -66,6 +66,11 @@ namespace cpptrace {
CPPTRACE_API CPPTRACE_API
void stacktrace::print(std::ostream& stream, bool color) const { void stacktrace::print(std::ostream& stream, bool color) const {
print(stream, color, true);
}
CPPTRACE_API
void stacktrace::print(std::ostream& stream, bool color, bool newline_at_end) const {
if(color) { if(color) {
detail::enable_virtual_terminal_processing_if_needed(); detail::enable_virtual_terminal_processing_if_needed();
} }
@ -100,20 +105,31 @@ namespace cpptrace {
<< " at " << " at "
<< (color ? GREEN : "") << (color ? GREEN : "")
<< frame.filename << frame.filename
<< (color ? RESET : "") << (color ? RESET : "");
<< ":" if(frame.line != 0) {
<< (color ? BLUE : "") stream
<< frame.line << ":"
<< (color ? RESET : "") << (color ? BLUE : "")
<< (frame.col > 0 ? (color ? ":" BLUE : ":") + std::to_string(frame.col) + (color ? RESET : "") : "") << frame.line
<< std::endl; << (color ? RESET : "");
if(frame.column != UINT_LEAST32_MAX) {
stream << (
frame.column > 0
? (color ? ":" BLUE : ":") + std::to_string(frame.column) + (color ? RESET : "")
: ""
);
}
}
if(newline_at_end || &frame != &frames.back()) {
stream << std::endl;
}
} }
} }
CPPTRACE_API CPPTRACE_API
std::string stacktrace::to_string() const { std::string stacktrace::to_string() const {
std::ostringstream oss; std::ostringstream oss;
print(oss, false); print(oss, false, false);
return std::move(oss).str(); return std::move(oss).str();
} }

View File

@ -38,8 +38,8 @@ namespace detail {
if(result[i].line == 0) { if(result[i].line == 0) {
result[i].line = trace[i].line; result[i].line = trace[i].line;
} }
if(result[i].col == 0) { if(result[i].column == 0) {
result[i].col = trace[i].col; result[i].column = trace[i].column;
} }
if(result[i].filename.empty()) { if(result[i].filename.empty()) {
result[i].filename = std::move(trace[i].filename); result[i].filename = std::move(trace[i].filename);

View File

@ -273,7 +273,7 @@ namespace addr2line {
// NOLINTNEXTLINE(readability-convert-member-functions-to-static) // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
std::vector<stacktrace_frame> resolve_frames(const std::vector<object_frame>& frames) { std::vector<stacktrace_frame> resolve_frames(const std::vector<object_frame>& frames) {
// TODO: Refactor better // TODO: Refactor better
std::vector<stacktrace_frame> trace(frames.size(), stacktrace_frame { 0, 0, 0, "", "" }); std::vector<stacktrace_frame> trace(frames.size());
for(size_t i = 0; i < frames.size(); i++) { for(size_t i = 0; i < frames.size(); i++) {
trace[i].address = frames[i].raw_address; trace[i].address = frames[i].raw_address;
// Set what is known for now, and resolutions from addr2line should overwrite // Set what is known for now, and resolutions from addr2line should overwrite

View File

@ -346,7 +346,7 @@ namespace dbghelp {
return { return {
addr, addr,
static_cast<std::uint_least32_t>(line.LineNumber), static_cast<std::uint_least32_t>(line.LineNumber),
0, UINT_LEAST32_MAX,
line.FileName, line.FileName,
symbol->Name symbol->Name
}; };
@ -377,7 +377,7 @@ namespace dbghelp {
return { return {
addr, addr,
static_cast<std::uint_least32_t>(line.LineNumber), static_cast<std::uint_least32_t>(line.LineNumber),
0, UINT_LEAST32_MAX,
line.FileName, line.FileName,
signature signature
}; };
@ -385,7 +385,7 @@ namespace dbghelp {
return { return {
addr, addr,
0, 0,
0, UINT_LEAST32_MAX,
"", "",
symbol->Name symbol->Name
}; };
@ -394,7 +394,7 @@ namespace dbghelp {
return { return {
addr, addr,
0, 0,
0, UINT_LEAST32_MAX,
"", "",
"" ""
}; };

View File

@ -18,7 +18,7 @@ namespace libdl {
return { return {
addr, addr,
0, 0,
0, UINT_LEAST32_MAX,
info.dli_fname ? info.dli_fname : "", info.dli_fname ? info.dli_fname : "",
info.dli_sname ? info.dli_sname : "" info.dli_sname ? info.dli_sname : ""
}; };
@ -26,7 +26,7 @@ namespace libdl {
return { return {
addr, addr,
0, 0,
0, UINT_LEAST32_MAX,
"", "",
"" ""
}; };

View File

@ -60,7 +60,6 @@ namespace libbacktrace {
// TODO: Handle backtrace_pcinfo calling the callback multiple times on inlined functions // TODO: Handle backtrace_pcinfo calling the callback multiple times on inlined functions
stacktrace_frame resolve_frame(const uintptr_t addr) { stacktrace_frame resolve_frame(const uintptr_t addr) {
stacktrace_frame frame; stacktrace_frame frame;
frame.col = 0;
backtrace_pcinfo( backtrace_pcinfo(
get_backtrace_state(), get_backtrace_state(),
addr, addr,

View File

@ -1037,7 +1037,7 @@ namespace libdwarf {
CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING CPPTRACE_FORCE_NO_INLINE_FOR_PROFILING
std::vector<stacktrace_frame> resolve_frames(const std::vector<object_frame>& frames) { std::vector<stacktrace_frame> resolve_frames(const std::vector<object_frame>& frames) {
std::vector<stacktrace_frame> trace(frames.size(), stacktrace_frame { 0, 0, 0, "", "" }); std::vector<stacktrace_frame> trace(frames.size());
for(const auto& obj_entry : collate_frames(frames, trace)) { for(const auto& obj_entry : collate_frames(frames, trace)) {
const auto& obj_name = obj_entry.first; const auto& obj_name = obj_entry.first;
dwarf_resolver resolver(obj_name); dwarf_resolver resolver(obj_name);

View File

@ -9,23 +9,11 @@ namespace cpptrace {
namespace detail { namespace detail {
namespace nothing { namespace nothing {
std::vector<stacktrace_frame> resolve_frames(const std::vector<uintptr_t>& frames) { std::vector<stacktrace_frame> resolve_frames(const std::vector<uintptr_t>& frames) {
return std::vector<stacktrace_frame>(frames.size(), { return std::vector<stacktrace_frame>(frames.size());
0,
0,
0,
"",
""
});
} }
std::vector<stacktrace_frame> resolve_frames(const std::vector<object_frame>& frames) { std::vector<stacktrace_frame> resolve_frames(const std::vector<object_frame>& frames) {
return std::vector<stacktrace_frame>(frames.size(), { return std::vector<stacktrace_frame>(frames.size());
0,
0,
0,
"",
""
});
} }
} }
} }