From 5647d5ee8e426edd18dbc5765a32951a6c7efdcf Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 19 Jan 2017 12:36:06 +0800 Subject: [PATCH] common/BackTrace: demangle on FreeBSD also the output on FreeBSD/clang looks like: 1: 0x44bfb3 <_Z3foov+0x413> at /usr/srcs/Ceph/work/ceph/build/bin/unittest_back_trace 2: 0x44c23e <_ZN20BackTrace_Basic_Test8TestBodyEv+0x1e> at /usr/srcs/Ceph/work/ceph/build/bin/unittest_back_trace 3: 0x4d068a <_ZN7testing8internal38HandleSehExceptionsInMethodIfSupportedINS_4TestEvEET0_PT_MS4_FS3_vEPKc+0x7a> at /usr/srcs/Ceph/work/ceph/build/bin/unittest_back_trace 4: 0x4b5977 <_ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS_4TestEvEET0_PT_MS4_FS3_vEPKc+0x77> at /usr/srcs/Ceph/work/ceph/build/bin/unittest_back_trace ... and update the test accordingly, as FreeBSD/clang uses '<>' to enclose the mangled function and offset. also, only demangle the C++ mangled names. those names always start with "_Z". on FreeBSD, after demangling, "main" is turned into "unsigned long", which does not make sense. Signed-off-by: Kefu Chai --- src/common/BackTrace.cc | 14 +++++++++++--- src/rocksdb | 2 +- src/test/common/test_back_trace.cc | 7 ++++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/common/BackTrace.cc b/src/common/BackTrace.cc index 33408f33d448a..5fc6311d52892 100644 --- a/src/common/BackTrace.cc +++ b/src/common/BackTrace.cc @@ -27,8 +27,13 @@ void BackTrace::print(std::ostream& out) const char *begin = 0, *end = 0; // find the parentheses and address offset surrounding the mangled name +#ifdef __FreeBSD__ + static constexpr char OPEN = '<'; +#else + static constexpr char OPEN = '('; +#endif for (char *j = strings[i]; *j; ++j) { - if (*j == '(') + if (*j == OPEN) begin = j+1; else if (*j == '+') end = j; @@ -44,7 +49,10 @@ void BackTrace::print(std::ostream& out) const foo[len] = 0; int status; - char *ret = abi::__cxa_demangle(foo, function, &sz, &status); + char *ret = nullptr; + // only demangle a C++ mangled name + if (foo[0] == '_' && foo[1] == 'Z') + ret = abi::__cxa_demangle(foo, function, &sz, &status); if (ret) { // return value may be a realloc() of the input function = ret; @@ -55,7 +63,7 @@ void BackTrace::print(std::ostream& out) const strncat(function, "()", sz); function[sz-1] = 0; } - out << " " << (i-skip+1) << ": (" << function << end << std::endl; + out << " " << (i-skip+1) << ": " << OPEN << function << end << std::endl; //fprintf(out, " %s:%s\n", stack.strings[i], function); free(foo); } else { diff --git a/src/rocksdb b/src/rocksdb index a9b0a54f4925a..a0deec960f3a8 160000 --- a/src/rocksdb +++ b/src/rocksdb @@ -1 +1 @@ -Subproject commit a9b0a54f4925a5a48c70fc19257af988acebcea9 +Subproject commit a0deec960f3a8190831c673e5ba998fe6fb7ea90 diff --git a/src/test/common/test_back_trace.cc b/src/test/common/test_back_trace.cc index 389f4e3de3af9..ff626f5a4c4fd 100644 --- a/src/test/common/test_back_trace.cc +++ b/src/test/common/test_back_trace.cc @@ -33,9 +33,14 @@ TEST(BackTrace, Basic) { boost::split(lines, bt, boost::is_any_of("\n")); const unsigned lineno = 1; ASSERT_GT(lines.size(), lineno); - ASSERT_EQ(lines[0].find(pretty_version_to_str()), 1); + ASSERT_EQ(lines[0].find(pretty_version_to_str()), 1U); boost::regex e{"^ 1: " +#ifdef __FreeBSD__ + "\\s" + "at\\s.*$"}; +#else "\\(foo.*\\)\\s" "\\[0x[[:xdigit:]]+\\]$"}; +#endif EXPECT_TRUE(boost::regex_match(lines[lineno], e)); } -- 2.47.3