Skip to content

Commit

Permalink
stacktrace: Change print_backtrace_symbol to get_backtrace_string
Browse files Browse the repository at this point in the history
To make backtrace string could be printed selectively, Change each
function not to print inside the function rather return the report
string.

  print_backtrace_symbol            -> get_backtrace_string
  print_backtrace_symbol_flamegraph -> get_backtrace_string_flamegraph

Signed-off-by: Bojun Seo <[email protected]>
  • Loading branch information
Bojun-Seo committed Nov 24, 2024
1 parent 5766e98 commit 5155231
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/stacktrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <map>
#include <sstream>
#include <vector>
Expand Down Expand Up @@ -93,7 +94,7 @@ void release_backtrace(void *addr)
addrmap.erase(addrit);
}

static void print_backtrace_symbol(int count, void *addr)
static void get_backtrace_string(int count, void *addr, std::stringstream &ss_bt)
{
Dl_info dlip;
char *symbol;
Expand All @@ -102,15 +103,12 @@ static void print_backtrace_symbol(int count, void *addr)
int dl_ret;
int len = SYMBOL_MAXLEN;

#if __SIZEOF_LONG__ == 4
pr_out("%2d [%#10lx] ", count, (unsigned long)addr);
#else
pr_out("%2d [%#14lx] ", count, (unsigned long)addr);
#endif
ss_bt << std::dec << count << " [0x" << std::hex <<
std::setw(4 + __SIZEOF_LONG__) << (unsigned long)addr << "] ";
// dladdr() translates address to symbolic info.
dl_ret = dladdr(addr, &dlip);
if (dl_ret == 0) {
pr_out("?\n");
ss_bt << "?\n";
return;
}

Expand All @@ -127,14 +125,15 @@ static void print_backtrace_symbol(int count, void *addr)
}
offset = static_cast<int>(static_cast<int *>(addr) -
static_cast<int *>(dlip.dli_saddr));
pr_out("%s +%#x ", symbol, offset);
ss_bt << symbol << " +0x" << offset << " ";
free(symbol);
}
offset = (int)((char *)addr - (char *)(dlip.dli_fbase));
pr_out("(%s +%#x)\n", dlip.dli_fname, offset);
ss_bt << "(" << dlip.dli_fname << " +0x" << offset << ")\n";
}

static void print_backtrace_symbol_flamegraph(void *addr, const char *semicolon)
static void get_backtrace_string_flamegraph(void *addr, const char *semicolon,
std::stringstream &ss_bt)
{
Dl_info dlip;
char *symbol;
Expand All @@ -160,11 +159,11 @@ static void print_backtrace_symbol_flamegraph(void *addr, const char *semicolon)
}
offset = static_cast<int>(static_cast<int *>(addr) -
static_cast<int *>(dlip.dli_saddr));
pr_out("%s%s+%#x", semicolon, symbol, offset);
ss_bt << semicolon << symbol << "+0x" << offset;
free(symbol);
}
else {
pr_out("%s%s+%p", semicolon, dlip.dli_fname, addr);
ss_bt << semicolon << dlip.dli_fname << addr;
}
}

Expand Down Expand Up @@ -305,16 +304,19 @@ static void print_dump_stackmap(std::vector<std::pair<stack_trace_t, stack_info_

const stack_trace_t &stack_trace = sorted_stack[i].first;
std::string age = get_delta_time_unit(current - info.birth_time);

pr_out("=== backtrace #%d === [count/peak: %zd/%zd] "
"[size/peak: %s/%s] [age: %s]\n",
++cnt, info.count, info.peak_count, get_byte_unit(info.total_size).c_str(),
get_byte_unit(info.peak_total_size).c_str(), age.c_str());

std::stringstream ss_intro;
std::stringstream ss_bt;

++cnt;
ss_intro << "=== backtrace #" << cnt << " === [count/peak: " << info.count << "/"
<< info.peak_count << "] "
<< "[size/peak: " << get_byte_unit(info.total_size) << "/"
<< get_byte_unit(info.peak_total_size) << "] [age: " << age << "]\n";
ss_bt << std::setfill('0');
for (int j = 0; j < info.stack_depth; j++)
print_backtrace_symbol(j, stack_trace[j]);
get_backtrace_string(j, stack_trace[j], ss_bt);

pr_out("\n");
pr_out("%s%s\n", ss_intro.str().c_str(), ss_bt.str().c_str());
}
}

Expand All @@ -331,12 +333,15 @@ print_dump_stackmap_flamegraph(std::vector<std::pair<stack_trace_t, stack_info_t
break;

const stack_trace_t &stack_trace = sorted_stack[i].first;
std::stringstream ss_bt;

ss_bt << std::hex;
for (size_t j = 0; j < info.stack_depth; ++j) {
print_backtrace_symbol_flamegraph(stack_trace[info.stack_depth - 1 - j],
semicolon);
get_backtrace_string_flamegraph(stack_trace[info.stack_depth - 1 - j],
semicolon, ss_bt);
semicolon = ";";
}
pr_out("%s", ss_bt.str().c_str());
pr_out(" %" PRIu64 "\n", size);
}

Expand Down

0 comments on commit 5155231

Please sign in to comment.