]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/BackTrace: demangle on FreeBSD also 12992/head
authorKefu Chai <kchai@redhat.com>
Thu, 19 Jan 2017 04:36:06 +0000 (12:36 +0800)
committerKefu Chai <kchai@redhat.com>
Sun, 22 Jan 2017 08:14:56 +0000 (16:14 +0800)
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 <kchai@redhat.com>
src/common/BackTrace.cc
src/rocksdb
src/test/common/test_back_trace.cc

index 33408f33d448ad0a42ed567c2539162622795c5b..5fc6311d528928360924348b38122d4b19c444cb 100644 (file)
@@ -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 {
index a9b0a54f4925a5a48c70fc19257af988acebcea9..a0deec960f3a8190831c673e5ba998fe6fb7ea90 160000 (submodule)
@@ -1 +1 @@
-Subproject commit a9b0a54f4925a5a48c70fc19257af988acebcea9
+Subproject commit a0deec960f3a8190831c673e5ba998fe6fb7ea90
index 389f4e3de3af9fd0523e4cfeb403586edafd85fb..ff626f5a4c4fd9d218d848b04713b43075edd501 100644 (file)
@@ -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__
+                "<foo.*>\\s"
+                "at\\s.*$"};
+#else
                 "\\(foo.*\\)\\s"
                 "\\[0x[[:xdigit:]]+\\]$"};
+#endif
   EXPECT_TRUE(boost::regex_match(lines[lineno], e));
 }