Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Oct 7, 2023
1 parent 335804b commit 09c10b8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
39 changes: 22 additions & 17 deletions be/src/common/stack_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,28 +385,28 @@ static void toStringEveryLineImpl([[maybe_unused]] const std::string dwarf_locat
out << " ";
}

if (shouldShowAddress(physical_addr)) {
out << "@ ";
writePointerHex(physical_addr, out);
}

if (const auto* const symbol = symbol_index.findSymbol(virtual_addr)) {
out << " " << collapseNames(demangle(symbol->name));
} else {
out << " ?";
}

if (std::error_code ec; object && std::filesystem::exists(object->name, ec) && !ec) {
auto dwarf_it = dwarfs.try_emplace(object->name, object->elf).first;

doris::Dwarf::LocationInfo location;

if (dwarf_it->second.findAddress(uintptr_t(physical_addr), location, mode,
inline_frames)) {
out << " " << location.file.toString() << ":" << location.line;
out << location.file.toString() << ":" << location.line;
}
}

if (const auto* const symbol = symbol_index.findSymbol(virtual_addr)) {
out << " " << collapseNames(demangle(symbol->name));
} else {
out << " ?";
}

if (shouldShowAddress(physical_addr)) {
out << " @ ";
writePointerHex(physical_addr, out);
}

out << " in " << (object ? object->name : "?");

callback(out.str());
Expand Down Expand Up @@ -458,11 +458,16 @@ std::string toStringCached(const StackTrace::FramePointers& pointers, size_t off
}
}

std::string StackTrace::toString() const {
// Delete the first three frame pointers, which are inside the stacktrace.
StackTrace::FramePointers frame_pointers_raw {};
std::copy(frame_pointers.begin() + 3, frame_pointers.end(), frame_pointers_raw.begin());
return toStringCached(frame_pointers_raw, offset, size - 3);
std::string StackTrace::toString(int start_pointers_index) const {
if (start_pointers_index == 0) {
return toStringCached(frame_pointers, offset, size);
} else {
// Start output from the start_pointers_index position of frame pointers.
StackTrace::FramePointers frame_pointers_raw {};
std::copy(frame_pointers.begin() + start_pointers_index, frame_pointers.end(),
frame_pointers_raw.begin());
return toStringCached(frame_pointers_raw, offset, size - start_pointers_index);
}
}

std::string StackTrace::toString(void** frame_pointers_raw, size_t offset, size_t size) {
Expand Down
2 changes: 1 addition & 1 deletion be/src/common/stack_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class StackTrace {
[[nodiscard]] constexpr size_t getSize() const { return size; }
[[nodiscard]] constexpr size_t getOffset() const { return offset; }
[[nodiscard]] const FramePointers& getFramePointers() const { return frame_pointers; }
[[nodiscard]] std::string toString() const;
[[nodiscard]] std::string toString(int start_pointers_index = 0) const;

static std::string toString(void** frame_pointers, size_t offset, size_t size);
static void createCache();
Expand Down
5 changes: 3 additions & 2 deletions be/src/common/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ class [[nodiscard]] Status {
}
#ifdef ENABLE_STACKTRACE
if constexpr (stacktrace && capture_stacktrace(code)) {
status._err_msg->_stack = get_stack_trace();
// Delete the first four frame pointers, which are inside the StackTrace and Status.
status._err_msg->_stack = get_stack_trace(4);
LOG(WARNING) << "meet error status: " << status; // may print too many stacks.
}
#endif
Expand All @@ -395,7 +396,7 @@ class [[nodiscard]] Status {
}
#ifdef ENABLE_STACKTRACE
if (stacktrace && capture_stacktrace(code)) {
status._err_msg->_stack = get_stack_trace();
status._err_msg->_stack = get_stack_trace(4);
LOG(WARNING) << "meet error status: " << status; // may print too many stacks.
}
#endif
Expand Down
8 changes: 4 additions & 4 deletions be/src/util/stack_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void DumpStackTraceToString(std::string* stacktrace);

namespace doris {

std::string get_stack_trace() {
std::string get_stack_trace(int start_pointers_index) {
#ifdef ENABLE_STACKTRACE
auto tool = config::get_stack_trace_tool;
if (tool == "glog") {
Expand All @@ -48,7 +48,7 @@ std::string get_stack_trace() {
#if defined(__APPLE__) // TODO
return get_stack_trace_by_glog();
#endif
return get_stack_trace_by_libunwind();
return get_stack_trace_by_libunwind(start_pointers_index);
} else {
return "no stack";
}
Expand Down Expand Up @@ -80,8 +80,8 @@ std::string get_stack_trace_by_glibc() {
return out.str();
}

std::string get_stack_trace_by_libunwind() {
return "\n" + StackTrace().toString();
std::string get_stack_trace_by_libunwind(int start_pointers_index) {
return "\n" + StackTrace().toString(start_pointers_index);
}

} // namespace doris
5 changes: 3 additions & 2 deletions be/src/util/stack_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace doris {
// boost: 1000 times cost 1min, has line numbers, but has memory leak.
// glibc: 1000 times cost 1min, no line numbers, unresolved backtrace symbol.
// libunwind: cost is negligible, has line numbers.
std::string get_stack_trace();
// Delete the first three frame pointers, which are inside the StackTrace.
std::string get_stack_trace(int start_pointers_index = 3);

// Note: there is a libc bug that causes this not to work on 64 bit machines
// for recursive calls.
Expand All @@ -53,6 +54,6 @@ std::string get_stack_trace_by_glibc();
// 2. Support signal handle
// 3. libunwid support unw_backtrace for jemalloc
// 4. Use of undefined compile option USE_MUSL for later
std::string get_stack_trace_by_libunwind();
std::string get_stack_trace_by_libunwind(int start_pointers_index);

} // namespace doris

0 comments on commit 09c10b8

Please sign in to comment.