Skip to content

Commit

Permalink
Left align demangled stacktrace output. (kokkos#6191)
Browse files Browse the repository at this point in the history
* Left align demangled stacktrace output.

Closes kokkos#6190.

* Remove now unused parameter to fix CI.

* Fix clang-format for CI.

* Print the address first

* Fix unused parameter Werror.

* Remove bool last parameter from for_each_token.

* Make clang-format happy
  • Loading branch information
vbrunini committed Jun 9, 2023
1 parent a406372 commit 43a797b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
70 changes: 25 additions & 45 deletions core/src/impl/Kokkos_Stacktrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void for_each_token(const std::string& s, Callback c) {
const size_t end = find_first_whitespace(s, cur);
const bool last = (end == std::string::npos);
const size_t count = last ? end : size_t(end - cur);
c(s.substr(cur, count), last);
c(s.substr(cur, count));
cur = find_first_non_whitespace(s, end);
}
}
Expand All @@ -147,15 +147,14 @@ void for_each_token(const std::string& s, Callback c) {
struct main_column_info {
bool found_main;
size_t main_col;
std::vector<size_t> main_col_lens;
};

main_column_info find_main_column(const std::vector<std::string>& traceback) {
bool found_main = false;
size_t main_col = 0;
for (auto&& entry : traceback) {
size_t col_count = 0;
for_each_token(entry, [&](const std::string& s, bool) {
for_each_token(entry, [&](const std::string& s) {
const size_t pos = s.find("main");
if (pos != std::string::npos) {
found_main = true;
Expand All @@ -168,52 +167,33 @@ main_column_info find_main_column(const std::vector<std::string>& traceback) {
}
}

// Make another pass to get the column lengths.
// Only demangle the column of functions.
std::vector<size_t> max_col_lengths;
for (auto&& entry : traceback) {
size_t col_count = 0;
for_each_token(entry, [&](const std::string& s, bool) {
const size_t cur_col_len =
(found_main && col_count == main_col) ? demangle(s).size() : s.size();
++col_count;
if (max_col_lengths.size() < col_count) {
max_col_lengths.push_back(cur_col_len);
} else {
const size_t old_max_len = max_col_lengths[col_count - 1];
if (old_max_len < cur_col_len) {
max_col_lengths[col_count - 1] = cur_col_len;
}
}
});
}
return main_column_info{found_main, main_col, max_col_lengths};
return main_column_info{found_main, main_col};
}

void demangle_and_print_traceback_entry(
std::ostream& out, const std::string& traceback_entry,
const bool found_main, const size_t main_col,
const std::vector<size_t>& max_col_lens) {
void demangle_and_print_traceback_entry(std::ostream& out,
const std::string& traceback_entry,
const bool found_main,
const size_t main_col) {
std::vector<std::string> tokens;
size_t cur_col = 0;
for_each_token(traceback_entry, [&](const std::string& s, bool last) {
const size_t old_width(out.width());
out.width(max_col_lens[cur_col]);
try {
if (found_main && cur_col == main_col) {
out << demangle(s);
} else {
out << s;
}
if (!last) {
out << " ";
}
++cur_col;
} catch (...) {
out.width(old_width);
throw;

// Print the address column first
for_each_token(traceback_entry, [&](const std::string& s) {
if (!(found_main && cur_col == main_col)) {
out << s;
}
++cur_col;
});

out << " ";

// Then the function name
cur_col = 0;
for_each_token(traceback_entry, [&](const std::string& s) {
if (found_main && cur_col == main_col) {
out << demangle(s);
}
out.width(old_width);
++cur_col;
});
}

Expand All @@ -222,7 +202,7 @@ void demangle_and_print_traceback(std::ostream& out,
const auto result = find_main_column(traceback);
for (auto&& entry : traceback) {
demangle_and_print_traceback_entry(out, entry, result.found_main,
result.main_col, result.main_col_lens);
result.main_col);
out << std::endl;
}
}
Expand Down
16 changes: 12 additions & 4 deletions core/unit_test/TestStackTrace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ void stacktrace_test_f4();

void my_fancy_handler();

size_t find_first_non_whitespace(const std::string& s, const size_t start_pos) {
constexpr size_t num_ws_chars = 3;
const char ws_chars[] = "\n\t ";
return s.find_first_not_of(ws_chars, start_pos, num_ws_chars);
}

void test_stacktrace(bool bTerminate, bool bCustom = true) {
stacktrace_test_f1(std::cout);
bool bDynamic = false;
Expand All @@ -44,7 +50,7 @@ void test_stacktrace(bool bTerminate, bool bCustom = true) {
bDynamic = std::string::npos != foutput.find("stacktrace");

if (bDynamic) {
printf("test_f1: %s \n", foutput.c_str());
printf("test_f1:\n%s \n", foutput.c_str());
ASSERT_NE(std::string::npos, foutput.find("stacktrace_test_f1"));
for (auto x : {"stacktrace_test_f0", "stacktrace_test_f2",
"stacktrace_test_f3", "stacktrace_test_f4"}) {
Expand All @@ -59,12 +65,13 @@ void test_stacktrace(bool bTerminate, bool bCustom = true) {

if (bDynamic) {
std::string foutput = sstream.str();
printf("demangled test_f1: %s \n", foutput.c_str());
printf("demangled test_f1:\n%s \n", foutput.c_str());
ASSERT_NE(std::string::npos, foutput.find("Test::stacktrace_test_f1"));
for (auto x : {"stacktrace_test_f0", "stacktrace_test_f2",
"stacktrace_test_f3", "stacktrace_test_f4"}) {
ASSERT_EQ(std::string::npos, foutput.find(x));
}
EXPECT_EQ(0u, find_first_non_whitespace(foutput, 0));
}
}

Expand All @@ -83,7 +90,7 @@ void test_stacktrace(bool bTerminate, bool bCustom = true) {

if (bDynamic) {
std::string foutput = sstream.str();
printf("test_f3: %s \n", foutput.c_str());
printf("test_f3:\n%s \n", foutput.c_str());
for (auto x : {"stacktrace_test_f1", "stacktrace_test_f3"}) {
ASSERT_NE(std::string::npos, foutput.find(x));
}
Expand All @@ -98,10 +105,11 @@ void test_stacktrace(bool bTerminate, bool bCustom = true) {

if (bDynamic) {
std::string foutput = sstream.str();
printf("demangled test_f3: %s \n", foutput.c_str());
printf("demangled test_f3:\n%s \n", foutput.c_str());
for (auto x : {"stacktrace_test_f1", "stacktrace_test_f3"}) {
ASSERT_NE(std::string::npos, foutput.find(x));
}
EXPECT_EQ(0u, find_first_non_whitespace(foutput, 0));
}

// TODO make sure stacktrace_test_f2/4 don't show up
Expand Down

0 comments on commit 43a797b

Please sign in to comment.