Use the demangler from libunwindstack.

The current demangler does not handle rust code, so use the libunwindstack
demangler which does.

Test: All unit tests pass.
Test: Verified that backtraces through Rust code get demangled.
Change-Id: I8fa45d88d9c1664c9f164edeab2a55d7c18b1283
diff --git a/libc/malloc_debug/Android.bp b/libc/malloc_debug/Android.bp
index 408a046..fadaae3 100644
--- a/libc/malloc_debug/Android.bp
+++ b/libc/malloc_debug/Android.bp
@@ -25,8 +25,9 @@
     stl: "libc++_static",
 
     whole_static_libs: [
-        "libbase",
         "libasync_safe",
+        "libbase",
+        "libunwindstack_demangle",
     ],
 
     include_dirs: ["bionic/libc"],
diff --git a/libc/malloc_debug/PointerData.cpp b/libc/malloc_debug/PointerData.cpp
index c8aaa08..5129bf6 100644
--- a/libc/malloc_debug/PointerData.cpp
+++ b/libc/malloc_debug/PointerData.cpp
@@ -26,7 +26,6 @@
  * SUCH DAMAGE.
  */
 
-#include <cxxabi.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <signal.h>
@@ -49,6 +48,7 @@
 #include <android-base/stringprintf.h>
 #include <android-base/thread_annotations.h>
 #include <platform/bionic/macros.h>
+#include <unwindstack/Demangle.h>
 
 #include "Config.h"
 #include "DebugData.h"
@@ -619,16 +619,9 @@
         if (frame.function_name.empty()) {
           dprintf(fd, " \"\" 0}");
         } else {
-          char* demangled_name =
-              abi::__cxa_demangle(frame.function_name.c_str(), nullptr, nullptr, nullptr);
-          const char* name;
-          if (demangled_name != nullptr) {
-            name = demangled_name;
-          } else {
-            name = frame.function_name.c_str();
-          }
-          dprintf(fd, " \"%s\" %" PRIx64 "}", name, frame.function_offset);
-          free(demangled_name);
+          dprintf(fd, " \"%s\" %" PRIx64 "}",
+                  unwindstack::DemangleNameIfNeeded(frame.function_name).c_str(),
+                  frame.function_offset);
         }
       }
       dprintf(fd, "\n");
diff --git a/libc/malloc_debug/UnwindBacktrace.cpp b/libc/malloc_debug/UnwindBacktrace.cpp
index 8a6ff7b..740fabe 100644
--- a/libc/malloc_debug/UnwindBacktrace.cpp
+++ b/libc/malloc_debug/UnwindBacktrace.cpp
@@ -26,7 +26,6 @@
  * SUCH DAMAGE.
  */
 
-#include <cxxabi.h>
 #include <inttypes.h>
 #include <pthread.h>
 #include <stdint.h>
@@ -38,6 +37,7 @@
 
 #include <android-base/stringprintf.h>
 #include <unwindstack/AndroidUnwinder.h>
+#include <unwindstack/Demangle.h>
 #include <unwindstack/Unwinder.h>
 
 #include "UnwindBacktrace.h"
@@ -88,14 +88,7 @@
 
     if (!info->function_name.empty()) {
       line += " (";
-      char* demangled_name =
-          abi::__cxa_demangle(info->function_name.c_str(), nullptr, nullptr, nullptr);
-      if (demangled_name != nullptr) {
-        line += demangled_name;
-        free(demangled_name);
-      } else {
-        line += info->function_name;
-      }
+      line += unwindstack::DemangleNameIfNeeded(info->function_name);
       if (info->function_offset != 0) {
         line += "+" + std::to_string(info->function_offset);
       }
diff --git a/libc/malloc_debug/backtrace.cpp b/libc/malloc_debug/backtrace.cpp
index 6a32fca..8b58be3 100644
--- a/libc/malloc_debug/backtrace.cpp
+++ b/libc/malloc_debug/backtrace.cpp
@@ -26,7 +26,6 @@
  * SUCH DAMAGE.
  */
 
-#include <cxxabi.h>
 #include <dlfcn.h>
 #include <errno.h>
 #include <inttypes.h>
@@ -41,6 +40,8 @@
 #include "backtrace.h"
 #include "debug_log.h"
 
+#include <unwindstack/Demangle.h>
+
 #if defined(__LP64__)
 #define PAD_PTR "016" PRIxPTR
 #else
@@ -154,18 +155,10 @@
 
     char buf[1024];
     if (symbol != nullptr) {
-      char* demangled_name = abi::__cxa_demangle(symbol, nullptr, nullptr, nullptr);
-      const char* name;
-      if (demangled_name != nullptr) {
-        name = demangled_name;
-      } else {
-        name = symbol;
-      }
-      async_safe_format_buffer(buf, sizeof(buf),
-                               "          #%02zd  pc %" PAD_PTR "  %s%s (%s+%" PRIuPTR ")\n",
-                               frame_num, rel_pc, soname, offset_buf, name,
-                               frames[frame_num] - offset);
-      free(demangled_name);
+      async_safe_format_buffer(
+          buf, sizeof(buf), "          #%02zd  pc %" PAD_PTR "  %s%s (%s+%" PRIuPTR ")\n",
+          frame_num, rel_pc, soname, offset_buf, unwindstack::DemangleNameIfNeeded(symbol).c_str(),
+          frames[frame_num] - offset);
     } else {
       async_safe_format_buffer(buf, sizeof(buf), "          #%02zd  pc %" PAD_PTR "  %s%s\n",
                                frame_num, rel_pc, soname, offset_buf);