keep previous history after reset to mr1 plus aah changes (ics-aah-wip)
diff --git a/debuggerd/Android.mk b/debuggerd/Android.mk
index f127363..6cfe79b 100644
--- a/debuggerd/Android.mk
+++ b/debuggerd/Android.mk
@@ -5,9 +5,12 @@
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES:= debuggerd.c utility.c getevent.c $(TARGET_ARCH)/machine.c
+LOCAL_SRC_FILES:= debuggerd.c utility.c getevent.c $(TARGET_ARCH)/machine.c $(TARGET_ARCH)/unwind.c symbol_table.c
+ifeq ($(TARGET_ARCH),arm)
+LOCAL_SRC_FILES += $(TARGET_ARCH)/pr-support.c
+endif
 
-LOCAL_CFLAGS := -Wall -Werror -std=gnu99
+LOCAL_CFLAGS := -Wall
 LOCAL_MODULE := debuggerd
 
 ifeq ($(ARCH_ARM_HAVE_VFP),true)
@@ -17,7 +20,7 @@
 LOCAL_CFLAGS += -DWITH_VFP_D32
 endif # ARCH_ARM_HAVE_VFP_D32
 
-LOCAL_SHARED_LIBRARIES := libcutils libc libcorkscrew
+LOCAL_STATIC_LIBRARIES := libcutils libc
 
 include $(BUILD_EXECUTABLE)
 
diff --git a/debuggerd/arm/machine.c b/debuggerd/arm/machine.c
index d941684..5055444 100644
--- a/debuggerd/arm/machine.c
+++ b/debuggerd/arm/machine.c
@@ -34,11 +34,10 @@
 #include <linux/input.h>
 #include <linux/user.h>
 
-#include "../utility.h"
-#include "../machine.h"
+#include "utility.h"
 
 /* enable to dump memory pointed to by every register */
-#define DUMP_MEMORY_FOR_ALL_REGISTERS 1
+#define DUMP_MEM_FOR_ALL_REGS 1
 
 #ifdef WITH_VFP
 #ifdef WITH_VFP_D32
@@ -48,22 +47,172 @@
 #endif
 #endif
 
+/* Main entry point to get the backtrace from the crashing process */
+extern int unwind_backtrace_with_ptrace(int tfd, pid_t pid, mapinfo *map,
+                                        unsigned int sp_list[],
+                                        int *frame0_pc_sane,
+                                        bool at_fault);
+
 /*
- * If configured to do so, dump memory around *all* registers
- * for the crashing thread.
+ * If this isn't clearly a null pointer dereference, dump the
+ * /proc/maps entries near the fault address.
+ *
+ * This only makes sense to do on the thread that crashed.
  */
-static void dump_memory_and_code(int tfd, pid_t tid, bool at_fault) {
-    struct pt_regs regs;
-    if(ptrace(PTRACE_GETREGS, tid, 0, &regs)) {
+static void show_nearby_maps(int tfd, int pid, mapinfo *map)
+{
+    siginfo_t si;
+
+    memset(&si, 0, sizeof(si));
+    if (ptrace(PTRACE_GETSIGINFO, pid, 0, &si)) {
+        _LOG(tfd, false, "cannot get siginfo for %d: %s\n",
+            pid, strerror(errno));
         return;
     }
+    if (!signal_has_address(si.si_signo))
+        return;
 
-    if (at_fault && DUMP_MEMORY_FOR_ALL_REGISTERS) {
-        static const char REG_NAMES[] = "r0r1r2r3r4r5r6r7r8r9slfpipsp";
+    uintptr_t addr = (uintptr_t) si.si_addr;
+    addr &= ~0xfff;     /* round to 4K page boundary */
+    if (addr == 0)      /* null-pointer deref */
+        return;
 
-        for (int reg = 0; reg < 14; reg++) {
+    _LOG(tfd, false, "\nmemory map around addr %08x:\n", si.si_addr);
+
+    /*
+     * Search for a match, or for a hole where the match would be.  The list
+     * is backward from the file content, so it starts at high addresses.
+     */
+    bool found = false;
+    mapinfo *next = NULL;
+    mapinfo *prev = NULL;
+    while (map != NULL) {
+        if (addr >= map->start && addr < map->end) {
+            found = true;
+            next = map->next;
+            break;
+        } else if (addr >= map->end) {
+            /* map would be between "prev" and this entry */
+            next = map;
+            map = NULL;
+            break;
+        }
+
+        prev = map;
+        map = map->next;
+    }
+
+    /*
+     * Show "next" then "match" then "prev" so that the addresses appear in
+     * ascending order (like /proc/pid/maps).
+     */
+    if (next != NULL) {
+        _LOG(tfd, false, "%08x-%08x %s\n", next->start, next->end, next->name);
+    } else {
+        _LOG(tfd, false, "(no map below)\n");
+    }
+    if (map != NULL) {
+        _LOG(tfd, false, "%08x-%08x %s\n", map->start, map->end, map->name);
+    } else {
+        _LOG(tfd, false, "(no map for address)\n");
+    }
+    if (prev != NULL) {
+        _LOG(tfd, false, "%08x-%08x %s\n", prev->start, prev->end, prev->name);
+    } else {
+        _LOG(tfd, false, "(no map above)\n");
+    }
+}
+
+/*
+ * Dumps a few bytes of memory, starting a bit before and ending a bit
+ * after the specified address.
+ */
+static void dump_memory(int tfd, int pid, uintptr_t addr,
+    bool only_in_tombstone)
+{
+    char code_buffer[64];       /* actual 8+1+((8+1)*4) + 1 == 45 */
+    char ascii_buffer[32];      /* actual 16 + 1 == 17 */
+    uintptr_t p, end;
+
+    p = addr & ~3;
+    p -= 32;
+    if (p > addr) {
+        /* catch underflow */
+        p = 0;
+    }
+    end = p + 80;
+    /* catch overflow; 'end - p' has to be multiples of 16 */
+    while (end < p)
+        end -= 16;
+
+    /* Dump the code around PC as:
+     *  addr     contents                             ascii
+     *  00008d34 ef000000 e8bd0090 e1b00000 512fff1e  ............../Q
+     *  00008d44 ea00b1f9 e92d0090 e3a070fc ef000000  ......-..p......
+     */
+    while (p < end) {
+        char* asc_out = ascii_buffer;
+
+        sprintf(code_buffer, "%08x ", p);
+
+        int i;
+        for (i = 0; i < 4; i++) {
+            /*
+             * If we see (data == -1 && errno != 0), we know that the ptrace
+             * call failed, probably because we're dumping memory in an
+             * unmapped or inaccessible page.  I don't know if there's
+             * value in making that explicit in the output -- it likely
+             * just complicates parsing and clarifies nothing for the
+             * enlightened reader.
+             */
+            long data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
+            sprintf(code_buffer + strlen(code_buffer), "%08lx ", data);
+
+            int j;
+            for (j = 0; j < 4; j++) {
+                /*
+                 * Our isprint() allows high-ASCII characters that display
+                 * differently (often badly) in different viewers, so we
+                 * just use a simpler test.
+                 */
+                char val = (data >> (j*8)) & 0xff;
+                if (val >= 0x20 && val < 0x7f) {
+                    *asc_out++ = val;
+                } else {
+                    *asc_out++ = '.';
+                }
+            }
+            p += 4;
+        }
+        *asc_out = '\0';
+        _LOG(tfd, only_in_tombstone, "%s %s\n", code_buffer, ascii_buffer);
+    }
+
+}
+
+void dump_stack_and_code(int tfd, int pid, mapinfo *map,
+                         int unwind_depth, unsigned int sp_list[],
+                         bool at_fault)
+{
+    struct pt_regs r;
+    int sp_depth;
+    bool only_in_tombstone = !at_fault;
+
+    if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return;
+
+    if (DUMP_MEM_FOR_ALL_REGS && at_fault) {
+        /*
+         * If configured to do so, dump memory around *all* registers
+         * for the crashing thread.
+         *
+         * TODO: remove duplicates.
+         */
+        static const char REG_NAMES[] = "R0R1R2R3R4R5R6R7R8R9SLFPIPSPLRPC";
+
+        int reg;
+        for (reg = 0; reg < 16; reg++) {
             /* this may not be a valid way to access, but it'll do for now */
-            uintptr_t addr = regs.uregs[reg];
+            uintptr_t addr = r.uregs[reg];
 
             /*
              * Don't bother if it looks like a small int or ~= null, or if
@@ -73,65 +222,152 @@
                 continue;
             }
 
-            _LOG(tfd, false, "\nmemory near %.2s:\n", &REG_NAMES[reg * 2]);
-            dump_memory(tfd, tid, addr, at_fault);
+            _LOG(tfd, only_in_tombstone, "\nmem near %.2s:\n",
+                &REG_NAMES[reg*2]);
+            dump_memory(tfd, pid, addr, false);
+        }
+    } else {
+        unsigned int pc, lr;
+        pc = r.ARM_pc;
+        lr = r.ARM_lr;
+
+        _LOG(tfd, only_in_tombstone, "\ncode around pc:\n");
+        dump_memory(tfd, pid, (uintptr_t) pc, only_in_tombstone);
+
+        if (lr != pc) {
+            _LOG(tfd, only_in_tombstone, "\ncode around lr:\n");
+            dump_memory(tfd, pid, (uintptr_t) lr, only_in_tombstone);
         }
     }
 
-    _LOG(tfd, !at_fault, "\ncode around pc:\n");
-    dump_memory(tfd, tid, (uintptr_t)regs.ARM_pc, at_fault);
+    if (at_fault) {
+        show_nearby_maps(tfd, pid, map);
+    }
 
-    if (regs.ARM_pc != regs.ARM_lr) {
-        _LOG(tfd, !at_fault, "\ncode around lr:\n");
-        dump_memory(tfd, tid, (uintptr_t)regs.ARM_lr, at_fault);
+    unsigned int p, end;
+    unsigned int sp = r.ARM_sp;
+
+    p = sp - 64;
+    if (p > sp)
+        p = 0;
+    p &= ~3;
+    if (unwind_depth != 0) {
+        if (unwind_depth < STACK_CONTENT_DEPTH) {
+            end = sp_list[unwind_depth-1];
+        }
+        else {
+            end = sp_list[STACK_CONTENT_DEPTH-1];
+        }
+    }
+    else {
+        end = p + 256;
+        /* 'end - p' has to be multiples of 4 */
+        if (end < p)
+            end = ~7;
+    }
+
+    _LOG(tfd, only_in_tombstone, "\nstack:\n");
+
+    /* If the crash is due to PC == 0, there will be two frames that
+     * have identical SP value.
+     */
+    if (sp_list[0] == sp_list[1]) {
+        sp_depth = 1;
+    }
+    else {
+        sp_depth = 0;
+    }
+
+    while (p <= end) {
+         char *prompt;
+         char level[16];
+         long data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
+         if (p == sp_list[sp_depth]) {
+             sprintf(level, "#%02d", sp_depth++);
+             prompt = level;
+         }
+         else {
+             prompt = "   ";
+         }
+
+         /* Print the stack content in the log for the first 3 frames. For the
+          * rest only print them in the tombstone file.
+          */
+         _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
+              "%s %08x  %08x  %s\n", prompt, p, data,
+              map_to_name(map, data, ""));
+         p += 4;
+    }
+    /* print another 64-byte of stack data after the last frame */
+
+    end = p+64;
+    /* 'end - p' has to be multiples of 4 */
+    if (end < p)
+        end = ~7;
+
+    while (p <= end) {
+         long data = ptrace(PTRACE_PEEKTEXT, pid, (void*)p, NULL);
+         _LOG(tfd, (sp_depth > 2) || only_in_tombstone,
+              "    %08x  %08x  %s\n", p, data,
+              map_to_name(map, data, ""));
+         p += 4;
     }
 }
 
-void dump_registers(ptrace_context_t* context __attribute((unused)),
-        int tfd, pid_t tid, bool at_fault)
+void dump_pc_and_lr(int tfd, int pid, mapinfo *map, int unwound_level,
+                    bool at_fault)
+{
+    struct pt_regs r;
+
+    if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
+        _LOG(tfd, !at_fault, "tid %d not responding!\n", pid);
+        return;
+    }
+
+    if (unwound_level == 0) {
+        _LOG(tfd, !at_fault, "         #%02d  pc %08x  %s\n", 0, r.ARM_pc,
+             map_to_name(map, r.ARM_pc, "<unknown>"));
+    }
+    _LOG(tfd, !at_fault, "         #%02d  lr %08x  %s\n", 1, r.ARM_lr,
+            map_to_name(map, r.ARM_lr, "<unknown>"));
+}
+
+void dump_registers(int tfd, int pid, bool at_fault)
 {
     struct pt_regs r;
     bool only_in_tombstone = !at_fault;
 
-    if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
-        _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno));
+    if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
+        _LOG(tfd, only_in_tombstone,
+             "cannot get registers: %s\n", strerror(errno));
         return;
     }
 
-    _LOG(tfd, only_in_tombstone, "    r0 %08x  r1 %08x  r2 %08x  r3 %08x\n",
-            (uint32_t)r.ARM_r0, (uint32_t)r.ARM_r1, (uint32_t)r.ARM_r2, (uint32_t)r.ARM_r3);
-    _LOG(tfd, only_in_tombstone, "    r4 %08x  r5 %08x  r6 %08x  r7 %08x\n",
-            (uint32_t)r.ARM_r4, (uint32_t)r.ARM_r5, (uint32_t)r.ARM_r6, (uint32_t)r.ARM_r7);
-    _LOG(tfd, only_in_tombstone, "    r8 %08x  r9 %08x  sl %08x  fp %08x\n",
-            (uint32_t)r.ARM_r8, (uint32_t)r.ARM_r9, (uint32_t)r.ARM_r10, (uint32_t)r.ARM_fp);
-    _LOG(tfd, only_in_tombstone, "    ip %08x  sp %08x  lr %08x  pc %08x  cpsr %08x\n",
-            (uint32_t)r.ARM_ip, (uint32_t)r.ARM_sp, (uint32_t)r.ARM_lr,
-            (uint32_t)r.ARM_pc, (uint32_t)r.ARM_cpsr);
+    _LOG(tfd, only_in_tombstone, " r0 %08x  r1 %08x  r2 %08x  r3 %08x\n",
+         r.ARM_r0, r.ARM_r1, r.ARM_r2, r.ARM_r3);
+    _LOG(tfd, only_in_tombstone, " r4 %08x  r5 %08x  r6 %08x  r7 %08x\n",
+         r.ARM_r4, r.ARM_r5, r.ARM_r6, r.ARM_r7);
+    _LOG(tfd, only_in_tombstone, " r8 %08x  r9 %08x  10 %08x  fp %08x\n",
+         r.ARM_r8, r.ARM_r9, r.ARM_r10, r.ARM_fp);
+    _LOG(tfd, only_in_tombstone,
+         " ip %08x  sp %08x  lr %08x  pc %08x  cpsr %08x\n",
+         r.ARM_ip, r.ARM_sp, r.ARM_lr, r.ARM_pc, r.ARM_cpsr);
 
 #ifdef WITH_VFP
     struct user_vfp vfp_regs;
     int i;
 
-    if(ptrace(PTRACE_GETVFPREGS, tid, 0, &vfp_regs)) {
-        _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno));
+    if(ptrace(PTRACE_GETVFPREGS, pid, 0, &vfp_regs)) {
+        _LOG(tfd, only_in_tombstone,
+             "cannot get registers: %s\n", strerror(errno));
         return;
     }
 
     for (i = 0; i < NUM_VFP_REGS; i += 2) {
-        _LOG(tfd, only_in_tombstone, "    d%-2d %016llx  d%-2d %016llx\n",
-                i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]);
+        _LOG(tfd, only_in_tombstone,
+             " d%-2d %016llx  d%-2d %016llx\n",
+              i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]);
     }
-    _LOG(tfd, only_in_tombstone, "    scr %08lx\n", vfp_regs.fpscr);
+    _LOG(tfd, only_in_tombstone, " scr %08lx\n\n", vfp_regs.fpscr);
 #endif
 }
-
-void dump_thread(ptrace_context_t* context, int tfd, pid_t tid, bool at_fault) {
-    dump_registers(context, tfd, tid, at_fault);
-
-    dump_backtrace_and_stack(context, tfd, tid, at_fault);
-
-    if (at_fault) {
-        dump_memory_and_code(tfd, tid, at_fault);
-        dump_nearby_maps(context, tfd, tid);
-    }
-}
diff --git a/debuggerd/arm/pr-support.c b/debuggerd/arm/pr-support.c
new file mode 100644
index 0000000..358d9b7
--- /dev/null
+++ b/debuggerd/arm/pr-support.c
@@ -0,0 +1,345 @@
+/* ARM EABI compliant unwinding routines
+   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
+   Contributed by Paul Brook
+ 
+   This file is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by the
+   Free Software Foundation; either version 2, or (at your option) any
+   later version.
+
+   In addition to the permissions in the GNU General Public License, the
+   Free Software Foundation gives you unlimited permission to link the
+   compiled version of this file into combinations with other programs,
+   and to distribute those combinations without any restriction coming
+   from the use of this file.  (The General Public License restrictions
+   do apply in other respects; for example, they cover modification of
+   the file, and distribution when not linked into a combine
+   executable.)
+
+   This file is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; see the file COPYING.  If not, write to
+   the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.  */
+
+/****************************************************************************
+ * The functions here are derived from gcc/config/arm/pr-support.c from the 
+ * 4.3.x release. The main changes here involve the use of ptrace to retrieve
+ * memory/processor states from a remote process.
+ ****************************************************************************/
+
+#include <sys/types.h>
+#include <unwind.h>
+
+#include "utility.h"
+
+/* We add a prototype for abort here to avoid creating a dependency on
+   target headers.  */
+extern void abort (void);
+
+/* Derived from _Unwind_VRS_Pop to use ptrace */
+extern _Unwind_VRS_Result 
+unwind_VRS_Pop_with_ptrace (_Unwind_Context *context, 
+                            _Unwind_VRS_RegClass regclass, 
+                            _uw discriminator, 
+                            _Unwind_VRS_DataRepresentation representation, 
+                            pid_t pid);
+
+typedef struct _ZSt9type_info type_info; /* This names C++ type_info type */
+
+/* Misc constants.  */
+#define R_IP    12
+#define R_SP    13
+#define R_LR    14
+#define R_PC    15
+
+#define uint32_highbit (((_uw) 1) << 31)
+
+void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);
+
+/* Unwind descriptors.  */
+
+typedef struct
+{
+  _uw16 length;
+  _uw16 offset;
+} EHT16;
+
+typedef struct
+{
+  _uw length;
+  _uw offset;
+} EHT32;
+
+/* Personality routine helper functions.  */
+
+#define CODE_FINISH (0xb0)
+
+/* Derived from next_unwind_byte to use ptrace */
+/* Return the next byte of unwinding information, or CODE_FINISH if there is
+   no data remaining.  */
+static inline _uw8
+next_unwind_byte_with_ptrace (__gnu_unwind_state * uws, pid_t pid)
+{
+  _uw8 b;
+
+  if (uws->bytes_left == 0)
+    {
+      /* Load another word */
+      if (uws->words_left == 0)
+	return CODE_FINISH; /* Nothing left.  */
+      uws->words_left--;
+      uws->data = get_remote_word(pid, uws->next);
+      uws->next++;
+      uws->bytes_left = 3;
+    }
+  else
+    uws->bytes_left--;
+
+  /* Extract the most significant byte.  */
+  b = (uws->data >> 24) & 0xff;
+  uws->data <<= 8;
+  return b;
+}
+
+/* Execute the unwinding instructions described by UWS.  */
+_Unwind_Reason_Code
+unwind_execute_with_ptrace(_Unwind_Context * context, __gnu_unwind_state * uws,
+                           pid_t pid)
+{
+  _uw op;
+  int set_pc;
+  _uw reg;
+
+  set_pc = 0;
+  for (;;)
+    {
+      op = next_unwind_byte_with_ptrace (uws, pid);
+      if (op == CODE_FINISH)
+	{
+	  /* If we haven't already set pc then copy it from lr.  */
+	  if (!set_pc)
+	    {
+	      _Unwind_VRS_Get (context, _UVRSC_CORE, R_LR, _UVRSD_UINT32,
+			       &reg);
+	      _Unwind_VRS_Set (context, _UVRSC_CORE, R_PC, _UVRSD_UINT32,
+			       &reg);
+	      set_pc = 1;
+	    }
+	  /* Drop out of the loop.  */
+	  break;
+	}
+      if ((op & 0x80) == 0)
+	{
+	  /* vsp = vsp +- (imm6 << 2 + 4).  */
+	  _uw offset;
+
+	  offset = ((op & 0x3f) << 2) + 4;
+	  _Unwind_VRS_Get (context, _UVRSC_CORE, R_SP, _UVRSD_UINT32, &reg);
+	  if (op & 0x40)
+	    reg -= offset;
+	  else
+	    reg += offset;
+	  _Unwind_VRS_Set (context, _UVRSC_CORE, R_SP, _UVRSD_UINT32, &reg);
+	  continue;
+	}
+      
+      if ((op & 0xf0) == 0x80)
+	{
+	  op = (op << 8) | next_unwind_byte_with_ptrace (uws, pid);
+	  if (op == 0x8000)
+	    {
+	      /* Refuse to unwind.  */
+	      return _URC_FAILURE;
+	    }
+	  /* Pop r4-r15 under mask.  */
+	  op = (op << 4) & 0xfff0;
+	  if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_CORE, op, _UVRSD_UINT32, 
+                                      pid)
+	      != _UVRSR_OK)
+	    return _URC_FAILURE;
+	  if (op & (1 << R_PC))
+	    set_pc = 1;
+	  continue;
+	}
+      if ((op & 0xf0) == 0x90)
+	{
+	  op &= 0xf;
+	  if (op == 13 || op == 15)
+	    /* Reserved.  */
+	    return _URC_FAILURE;
+	  /* vsp = r[nnnn].  */
+	  _Unwind_VRS_Get (context, _UVRSC_CORE, op, _UVRSD_UINT32, &reg);
+	  _Unwind_VRS_Set (context, _UVRSC_CORE, R_SP, _UVRSD_UINT32, &reg);
+	  continue;
+	}
+      if ((op & 0xf0) == 0xa0)
+	{
+	  /* Pop r4-r[4+nnn], [lr].  */
+	  _uw mask;
+	  
+	  mask = (0xff0 >> (7 - (op & 7))) & 0xff0;
+	  if (op & 8)
+	    mask |= (1 << R_LR);
+	  if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_CORE, mask, _UVRSD_UINT32,
+                                      pid)
+	      != _UVRSR_OK)
+	    return _URC_FAILURE;
+	  continue;
+	}
+      if ((op & 0xf0) == 0xb0)
+	{
+	  /* op == 0xb0 already handled.  */
+	  if (op == 0xb1)
+	    {
+	      op = next_unwind_byte_with_ptrace (uws, pid);
+	      if (op == 0 || ((op & 0xf0) != 0))
+		/* Spare.  */
+		return _URC_FAILURE;
+	      /* Pop r0-r4 under mask.  */
+	      if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_CORE, op, 
+                                          _UVRSD_UINT32, pid)
+		  != _UVRSR_OK)
+		return _URC_FAILURE;
+	      continue;
+	    }
+	  if (op == 0xb2)
+	    {
+	      /* vsp = vsp + 0x204 + (uleb128 << 2).  */
+	      int shift;
+
+	      _Unwind_VRS_Get (context, _UVRSC_CORE, R_SP, _UVRSD_UINT32,
+			       &reg);
+	      op = next_unwind_byte_with_ptrace (uws, pid);
+	      shift = 2;
+	      while (op & 0x80)
+		{
+		  reg += ((op & 0x7f) << shift);
+		  shift += 7;
+		  op = next_unwind_byte_with_ptrace (uws, pid);
+		}
+	      reg += ((op & 0x7f) << shift) + 0x204;
+	      _Unwind_VRS_Set (context, _UVRSC_CORE, R_SP, _UVRSD_UINT32,
+			       &reg);
+	      continue;
+	    }
+	  if (op == 0xb3)
+	    {
+	      /* Pop VFP registers with fldmx.  */
+	      op = next_unwind_byte_with_ptrace (uws, pid);
+	      op = ((op & 0xf0) << 12) | ((op & 0xf) + 1);
+	      if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_VFP, op, _UVRSD_VFPX, 
+                                          pid)
+		  != _UVRSR_OK)
+		return _URC_FAILURE;
+	      continue;
+	    }
+	  if ((op & 0xfc) == 0xb4)
+	    {
+	      /* Pop FPA E[4]-E[4+nn].  */
+	      op = 0x40000 | ((op & 3) + 1);
+	      if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_FPA, op, _UVRSD_FPAX, 
+                                          pid)
+		  != _UVRSR_OK)
+		return _URC_FAILURE;
+	      continue;
+	    }
+	  /* op & 0xf8 == 0xb8.  */
+	  /* Pop VFP D[8]-D[8+nnn] with fldmx.  */
+	  op = 0x80000 | ((op & 7) + 1);
+	  if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_VFP, op, _UVRSD_VFPX, pid)
+	      != _UVRSR_OK)
+	    return _URC_FAILURE;
+	  continue;
+	}
+      if ((op & 0xf0) == 0xc0)
+	{
+	  if (op == 0xc6)
+	    {
+	      /* Pop iWMMXt D registers.  */
+	      op = next_unwind_byte_with_ptrace (uws, pid);
+	      op = ((op & 0xf0) << 12) | ((op & 0xf) + 1);
+	      if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_WMMXD, op, 
+                                          _UVRSD_UINT64, pid)
+		  != _UVRSR_OK)
+		return _URC_FAILURE;
+	      continue;
+	    }
+	  if (op == 0xc7)
+	    {
+	      op = next_unwind_byte_with_ptrace (uws, pid);
+	      if (op == 0 || (op & 0xf0) != 0)
+		/* Spare.  */
+		return _URC_FAILURE;
+	      /* Pop iWMMXt wCGR{3,2,1,0} under mask.  */
+	      if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_WMMXC, op, 
+                                          _UVRSD_UINT32, pid)
+		  != _UVRSR_OK)
+		return _URC_FAILURE;
+	      continue;
+	    }
+	  if ((op & 0xf8) == 0xc0)
+	    {
+	      /* Pop iWMMXt wR[10]-wR[10+nnn].  */
+	      op = 0xa0000 | ((op & 0xf) + 1);
+	      if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_WMMXD, op, 
+                                          _UVRSD_UINT64, pid)
+		  != _UVRSR_OK)
+		return _URC_FAILURE;
+	      continue;
+	    }
+	  if (op == 0xc8)
+	    {
+#ifndef __VFP_FP__
+ 	      /* Pop FPA registers.  */
+ 	      op = next_unwind_byte_with_ptrace (uws, pid);
+	      op = ((op & 0xf0) << 12) | ((op & 0xf) + 1);
+ 	      if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_FPA, op, _UVRSD_FPAX,
+                                          pid)
+ 		  != _UVRSR_OK)
+ 		return _URC_FAILURE;
+ 	      continue;
+#else
+              /* Pop VFPv3 registers D[16+ssss]-D[16+ssss+cccc] with vldm.  */
+              op = next_unwind_byte_with_ptrace (uws, pid);
+              op = (((op & 0xf0) + 16) << 12) | ((op & 0xf) + 1);
+              if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_VFP, op, 
+                                              _UVRSD_DOUBLE, pid)
+                  != _UVRSR_OK)
+                return _URC_FAILURE;
+              continue;
+#endif
+	    }
+	  if (op == 0xc9)
+	    {
+	      /* Pop VFP registers with fldmd.  */
+	      op = next_unwind_byte_with_ptrace (uws, pid);
+	      op = ((op & 0xf0) << 12) | ((op & 0xf) + 1);
+	      if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_VFP, op, 
+                                          _UVRSD_DOUBLE, pid)
+		  != _UVRSR_OK)
+		return _URC_FAILURE;
+	      continue;
+	    }
+	  /* Spare.  */
+	  return _URC_FAILURE;
+	}
+      if ((op & 0xf8) == 0xd0)
+	{
+	  /* Pop VFP D[8]-D[8+nnn] with fldmd.  */
+	  op = 0x80000 | ((op & 7) + 1);
+	  if (unwind_VRS_Pop_with_ptrace (context, _UVRSC_VFP, op, _UVRSD_DOUBLE, 
+                                      pid)
+	      != _UVRSR_OK)
+	    return _URC_FAILURE;
+	  continue;
+	}
+      /* Spare.  */
+      return _URC_FAILURE;
+    }
+  return _URC_OK;
+}
diff --git a/debuggerd/arm/unwind.c b/debuggerd/arm/unwind.c
new file mode 100644
index 0000000..d9600b7
--- /dev/null
+++ b/debuggerd/arm/unwind.c
@@ -0,0 +1,667 @@
+/* ARM EABI compliant unwinding routines.
+   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
+   Contributed by Paul Brook
+
+   This file is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by the
+   Free Software Foundation; either version 2, or (at your option) any
+   later version.
+
+   In addition to the permissions in the GNU General Public License, the
+   Free Software Foundation gives you unlimited permission to link the
+   compiled version of this file into combinations with other programs,
+   and to distribute those combinations without any restriction coming
+   from the use of this file.  (The General Public License restrictions
+   do apply in other respects; for example, they cover modification of
+   the file, and distribution when not linked into a combine
+   executable.)
+
+   This file is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; see the file COPYING.  If not, write to
+   the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.  */
+
+/****************************************************************************
+ * The functions here are derived from gcc/config/arm/unwind-arm.c from the 
+ * 4.3.x release. The main changes here involve the use of ptrace to retrieve
+ * memory/processor states from a remote process.
+ ****************************************************************************/
+
+#include <cutils/logd.h>
+#include <sys/ptrace.h>
+#include <unwind.h>
+#include "utility.h"
+
+#include "symbol_table.h"
+
+typedef struct _ZSt9type_info type_info; /* This names C++ type_info type */
+
+void __attribute__((weak)) __cxa_call_unexpected(_Unwind_Control_Block *ucbp);
+bool __attribute__((weak)) __cxa_begin_cleanup(_Unwind_Control_Block *ucbp);
+bool __attribute__((weak)) __cxa_type_match(_Unwind_Control_Block *ucbp,
+                        const type_info *rttip,
+                        bool is_reference,
+                        void **matched_object);
+
+/* Misc constants.  */
+#define R_IP	12
+#define R_SP	13
+#define R_LR	14
+#define R_PC	15
+
+#define EXIDX_CANTUNWIND 1
+#define uint32_highbit (((_uw) 1) << 31)
+
+#define UCB_FORCED_STOP_FN(ucbp) ((ucbp)->unwinder_cache.reserved1)
+#define UCB_PR_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved2)
+#define UCB_SAVED_CALLSITE_ADDR(ucbp) ((ucbp)->unwinder_cache.reserved3)
+#define UCB_FORCED_STOP_ARG(ucbp) ((ucbp)->unwinder_cache.reserved4)
+
+struct core_regs
+{
+  _uw r[16];
+};
+
+/* We use normal integer types here to avoid the compiler generating
+   coprocessor instructions.  */
+struct vfp_regs
+{
+  _uw64 d[16];
+  _uw pad;
+};
+
+struct vfpv3_regs
+{
+  /* Always populated via VSTM, so no need for the "pad" field from
+     vfp_regs (which is used to store the format word for FSTMX).  */
+  _uw64 d[16];
+};
+
+struct fpa_reg
+{
+  _uw w[3];
+};
+
+struct fpa_regs
+{
+  struct fpa_reg f[8];
+};
+
+struct wmmxd_regs
+{
+  _uw64 wd[16];
+};
+
+struct wmmxc_regs
+{
+  _uw wc[4];
+};
+
+/* Unwind descriptors.  */
+
+typedef struct
+{
+  _uw16 length;
+  _uw16 offset;
+} EHT16;
+
+typedef struct
+{
+  _uw length;
+  _uw offset;
+} EHT32;
+
+/* The ABI specifies that the unwind routines may only use core registers,
+   except when actually manipulating coprocessor state.  This allows
+   us to write one implementation that works on all platforms by
+   demand-saving coprocessor registers.
+
+   During unwinding we hold the coprocessor state in the actual hardware
+   registers and allocate demand-save areas for use during phase1
+   unwinding.  */
+
+typedef struct
+{
+  /* The first fields must be the same as a phase2_vrs.  */
+  _uw demand_save_flags;
+  struct core_regs core;
+  _uw prev_sp; /* Only valid during forced unwinding.  */
+  struct vfp_regs vfp;
+  struct vfpv3_regs vfp_regs_16_to_31;
+  struct fpa_regs fpa;
+  struct wmmxd_regs wmmxd;
+  struct wmmxc_regs wmmxc;
+} phase1_vrs;
+
+/* This must match the structure created by the assembly wrappers.  */
+typedef struct
+{
+  _uw demand_save_flags;
+  struct core_regs core;
+} phase2_vrs;
+
+
+/* An exception index table entry.  */
+
+typedef struct __EIT_entry
+{
+  _uw fnoffset;
+  _uw content;
+} __EIT_entry;
+
+/* Derived version to use ptrace */
+typedef _Unwind_Reason_Code (*personality_routine_with_ptrace)
+           (_Unwind_State,
+			_Unwind_Control_Block *,
+			_Unwind_Context *,
+            pid_t);
+
+/* Derived version to use ptrace */
+/* ABI defined personality routines.  */
+static _Unwind_Reason_Code unwind_cpp_pr0_with_ptrace (_Unwind_State,
+    _Unwind_Control_Block *, _Unwind_Context *, pid_t);
+static _Unwind_Reason_Code unwind_cpp_pr1_with_ptrace (_Unwind_State,
+    _Unwind_Control_Block *, _Unwind_Context *, pid_t);
+static _Unwind_Reason_Code unwind_cpp_pr2_with_ptrace (_Unwind_State,
+    _Unwind_Control_Block *, _Unwind_Context *, pid_t);
+
+/* Execute the unwinding instructions described by UWS.  */
+extern _Unwind_Reason_Code
+unwind_execute_with_ptrace(_Unwind_Context * context, __gnu_unwind_state * uws,
+                           pid_t pid);
+
+/* Derived version to use ptrace. Only handles core registers. Disregards
+ * FP and others. 
+ */
+/* ABI defined function to pop registers off the stack.  */
+
+_Unwind_VRS_Result unwind_VRS_Pop_with_ptrace (_Unwind_Context *context,
+				    _Unwind_VRS_RegClass regclass,
+				    _uw discriminator,
+				    _Unwind_VRS_DataRepresentation representation,
+                    pid_t pid)
+{
+  phase1_vrs *vrs = (phase1_vrs *) context;
+
+  switch (regclass)
+    {
+    case _UVRSC_CORE:
+      {
+	_uw *ptr;
+	_uw mask;
+	int i;
+
+	if (representation != _UVRSD_UINT32)
+	  return _UVRSR_FAILED;
+
+	mask = discriminator & 0xffff;
+	ptr = (_uw *) vrs->core.r[R_SP];
+	/* Pop the requested registers.  */
+	for (i = 0; i < 16; i++)
+	  {
+	    if (mask & (1 << i)) {
+	      vrs->core.r[i] = get_remote_word(pid, ptr);
+          ptr++;
+        }
+	  }
+	/* Writeback the stack pointer value if it wasn't restored.  */
+	if ((mask & (1 << R_SP)) == 0)
+	  vrs->core.r[R_SP] = (_uw) ptr;
+      }
+      return _UVRSR_OK;
+
+    default:
+      return _UVRSR_FAILED;
+    }
+}
+
+/* Core unwinding functions.  */
+
+/* Calculate the address encoded by a 31-bit self-relative offset at address
+   P.  */
+static inline _uw
+selfrel_offset31 (const _uw *p, pid_t pid)
+{
+  _uw offset = get_remote_word(pid, (void*)p);
+
+  //offset = *p;
+  /* Sign extend to 32 bits.  */
+  if (offset & (1 << 30))
+    offset |= 1u << 31;
+  else
+    offset &= ~(1u << 31);
+
+  return offset + (_uw) p;
+}
+
+
+/* Perform a binary search for RETURN_ADDRESS in TABLE.  The table contains
+   NREC entries.  */
+
+static const __EIT_entry *
+search_EIT_table (const __EIT_entry * table, int nrec, _uw return_address,
+                  pid_t pid)
+{
+  _uw next_fn;
+  _uw this_fn;
+  int n, left, right;
+
+  if (nrec == 0)
+    return (__EIT_entry *) 0;
+
+  left = 0;
+  right = nrec - 1;
+
+  while (1)
+    {
+      n = (left + right) / 2;
+      this_fn = selfrel_offset31 (&table[n].fnoffset, pid);
+      if (n != nrec - 1)
+	next_fn = selfrel_offset31 (&table[n + 1].fnoffset, pid) - 1;
+      else
+	next_fn = (_uw)0 - 1;
+
+      if (return_address < this_fn)
+	{
+	  if (n == left)
+	    return (__EIT_entry *) 0;
+	  right = n - 1;
+	}
+      else if (return_address <= next_fn)
+	return &table[n];
+      else
+	left = n + 1;
+    }
+}
+
+/* Find the exception index table eintry for the given address. */
+static const __EIT_entry*
+get_eitp(_uw return_address, pid_t pid, mapinfo *map, mapinfo **containing_map)
+{
+  const __EIT_entry *eitp = NULL;
+  int nrec;
+  mapinfo *mi;
+  
+  /* The return address is the address of the instruction following the
+     call instruction (plus one in thumb mode).  If this was the last
+     instruction in the function the address will lie in the following
+     function.  Subtract 2 from the address so that it points within the call
+     instruction itself.  */
+  if (return_address >= 2)
+      return_address -= 2;
+
+  for (mi = map; mi != NULL; mi = mi->next) {
+    if (return_address >= mi->start && return_address <= mi->end) break;
+  }
+
+  if (mi) {
+    if (containing_map) *containing_map = mi;
+    eitp = (__EIT_entry *) mi->exidx_start;
+    nrec = (mi->exidx_end - mi->exidx_start)/sizeof(__EIT_entry);
+    eitp = search_EIT_table (eitp, nrec, return_address, pid);
+  }
+  return eitp;
+}
+
+/* Find the exception index table eintry for the given address.
+   Fill in the relevant fields of the UCB.
+   Returns _URC_FAILURE if an error occurred, _URC_OK on success.  */
+
+static _Unwind_Reason_Code
+get_eit_entry (_Unwind_Control_Block *ucbp, _uw return_address, pid_t pid, 
+               mapinfo *map, mapinfo **containing_map)
+{
+  const __EIT_entry *eitp;
+  
+  eitp = get_eitp(return_address, pid, map, containing_map);
+
+  if (!eitp)
+    {
+      UCB_PR_ADDR (ucbp) = 0;
+      return _URC_FAILURE;
+    }
+  ucbp->pr_cache.fnstart = selfrel_offset31 (&eitp->fnoffset, pid);
+
+  _uw eitp_content = get_remote_word(pid, (void *)&eitp->content);
+
+  /* Can this frame be unwound at all?  */
+  if (eitp_content == EXIDX_CANTUNWIND)
+    {
+      UCB_PR_ADDR (ucbp) = 0;
+      return _URC_END_OF_STACK;
+    }
+
+  /* Obtain the address of the "real" __EHT_Header word.  */
+
+  if (eitp_content & uint32_highbit)
+    {
+      /* It is immediate data.  */
+      ucbp->pr_cache.ehtp = (_Unwind_EHT_Header *)&eitp->content;
+      ucbp->pr_cache.additional = 1;
+    }
+  else
+    {
+      /* The low 31 bits of the content field are a self-relative
+	 offset to an _Unwind_EHT_Entry structure.  */
+      ucbp->pr_cache.ehtp =
+	(_Unwind_EHT_Header *) selfrel_offset31 (&eitp->content, pid);
+      ucbp->pr_cache.additional = 0;
+    }
+
+  /* Discover the personality routine address.  */
+  if (get_remote_word(pid, ucbp->pr_cache.ehtp) & (1u << 31))
+    {
+      /* One of the predefined standard routines.  */
+      _uw idx = (get_remote_word(pid, ucbp->pr_cache.ehtp) >> 24) & 0xf;
+      if (idx == 0)
+	UCB_PR_ADDR (ucbp) = (_uw) &unwind_cpp_pr0_with_ptrace;
+      else if (idx == 1)
+	UCB_PR_ADDR (ucbp) = (_uw) &unwind_cpp_pr1_with_ptrace;
+      else if (idx == 2)
+	UCB_PR_ADDR (ucbp) = (_uw) &unwind_cpp_pr2_with_ptrace;
+      else
+	{ /* Failed */
+	  UCB_PR_ADDR (ucbp) = 0;
+	  return _URC_FAILURE;
+	}
+    } 
+  else
+    {
+      /* Execute region offset to PR */
+      UCB_PR_ADDR (ucbp) = selfrel_offset31 (ucbp->pr_cache.ehtp, pid);
+      /* Since we are unwinding the stack from a different process, it is
+       * impossible to execute the personality routine in debuggerd. Punt here.
+       */
+	  return _URC_FAILURE;
+    }
+  return _URC_OK;
+}
+
+/* Print out the current call level, pc, and module name in the crash log */
+static _Unwind_Reason_Code log_function(_Unwind_Context *context, pid_t pid, 
+                                        int tfd,
+                                        int stack_level,
+                                        mapinfo *map,
+                                        unsigned int sp_list[],
+                                        bool at_fault)
+{
+    _uw pc;
+    _uw rel_pc; 
+    phase2_vrs *vrs = (phase2_vrs*) context;
+    const mapinfo *mi;
+    bool only_in_tombstone = !at_fault;
+    const struct symbol* sym = 0;
+
+    if (stack_level < STACK_CONTENT_DEPTH) {
+        sp_list[stack_level] = vrs->core.r[R_SP];
+    }
+    pc = vrs->core.r[R_PC];
+
+    // Top level frame
+    if (stack_level == 0) {
+        pc &= ~1;
+    }
+    // For deeper framers, rollback pc by one instruction
+    else {
+        pc = vrs->core.r[R_PC];
+        /* Thumb mode - need to check whether the bl(x) has long offset or not.
+         * Examples:
+         *
+         * arm blx in the middle of thumb:
+         * 187ae:       2300            movs    r3, #0
+         * 187b0:       f7fe ee1c       blx     173ec
+         * 187b4:       2c00            cmp     r4, #0
+         *
+         * arm bl in the middle of thumb:
+         * 187d8:       1c20            adds    r0, r4, #0
+         * 187da:       f136 fd15       bl      14f208
+         * 187de:       2800            cmp     r0, #0
+         *
+         * pure thumb:
+         * 18894:       189b            adds    r3, r3, r2
+         * 18896:       4798            blx     r3
+         * 18898:       b001            add     sp, #4
+         */
+        if (pc & 1) {
+            _uw prev_word;
+            pc = (pc & ~1);
+            prev_word = get_remote_word(pid, (char *) pc-4);
+            // Long offset 
+            if ((prev_word & 0xf0000000) == 0xf0000000 && 
+                (prev_word & 0x0000e000) == 0x0000e000) {
+                pc -= 4;
+            }
+            else {
+                pc -= 2;
+            }
+        }
+        else { 
+            pc -= 4;
+        }
+    }
+
+    /* We used to print the absolute PC in the back trace, and mask out the top
+     * 3 bits to guesstimate the offset in the .so file. This is not working for
+     * non-prelinked libraries since the starting offset may not be aligned on 
+     * 1MB boundaries, and the library may be larger than 1MB. So for .so 
+     * addresses we print the relative offset in back trace.
+     */
+    mi = pc_to_mapinfo(map, pc, &rel_pc);
+
+    /* See if we can determine what symbol this stack frame resides in */
+    if (mi != 0 && mi->symbols != 0) {
+        sym = symbol_table_lookup(mi->symbols, rel_pc);
+    }
+
+    if (sym) {
+        _LOG(tfd, only_in_tombstone,
+            "         #%02d  pc %08x  %s (%s)\n", stack_level, rel_pc,
+            mi ? mi->name : "", sym->name);
+    } else {
+        _LOG(tfd, only_in_tombstone,
+            "         #%02d  pc %08x  %s\n", stack_level, rel_pc,
+            mi ? mi->name : "");
+    }
+
+    return _URC_NO_REASON;
+}
+
+/* Derived from __gnu_Unwind_Backtrace to use ptrace */
+/* Perform stack backtrace through unwind data. Return the level of stack it
+ * unwinds.
+ */
+int unwind_backtrace_with_ptrace(int tfd, pid_t pid, mapinfo *map, 
+                                 unsigned int sp_list[], int *frame0_pc_sane,
+                                 bool at_fault)
+{
+    phase1_vrs saved_vrs;
+    _Unwind_Reason_Code code = _URC_OK;
+    struct pt_regs r;
+    int i;
+    int stack_level = 0;
+
+    _Unwind_Control_Block ucb;
+    _Unwind_Control_Block *ucbp = &ucb;
+
+    if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return 0;
+
+    for (i = 0; i < 16; i++) {
+        saved_vrs.core.r[i] = r.uregs[i];
+        /*
+        _LOG(tfd, "r[%d] = 0x%x\n", i, saved_vrs.core.r[i]);
+        */
+    }
+
+    /* Set demand-save flags.  */
+    saved_vrs.demand_save_flags = ~(_uw) 0;
+
+    /* 
+     * If the app crashes because of calling the weeds, we cannot pass the PC 
+     * to the usual unwinding code as the EXIDX mapping will fail. 
+     * Instead, we simply print out the 0 as the top frame, and resume the 
+     * unwinding process with the value stored in LR.
+     */
+    if (get_eitp(saved_vrs.core.r[R_PC], pid, map, NULL) == NULL) { 
+        *frame0_pc_sane = 0;
+        log_function ((_Unwind_Context *) &saved_vrs, pid, tfd, stack_level, 
+                      map, sp_list, at_fault);
+        saved_vrs.core.r[R_PC] = saved_vrs.core.r[R_LR];
+        stack_level++;
+    }
+
+    do {
+        mapinfo *this_map = NULL;
+        /* Find the entry for this routine.  */
+        if (get_eit_entry(ucbp, saved_vrs.core.r[R_PC], pid, map, &this_map)
+            != _URC_OK) {
+            /* Uncomment the code below to study why the unwinder failed */
+#if 0
+            /* Shed more debugging info for stack unwinder improvement */
+            if (this_map) {
+                _LOG(tfd, 1, 
+                     "Relative PC=%#x from %s not contained in EXIDX\n", 
+                     saved_vrs.core.r[R_PC] - this_map->start, this_map->name);
+            }
+            _LOG(tfd, 1, "PC=%#x SP=%#x\n", 
+                 saved_vrs.core.r[R_PC], saved_vrs.core.r[R_SP]);
+#endif
+            code = _URC_FAILURE;
+            break;
+        }
+
+        /* The dwarf unwinder assumes the context structure holds things
+        like the function and LSDA pointers.  The ARM implementation
+        caches these in the exception header (UCB).  To avoid
+        rewriting everything we make the virtual IP register point at
+        the UCB.  */
+        _Unwind_SetGR((_Unwind_Context *)&saved_vrs, 12, (_Unwind_Ptr) ucbp);
+
+        /* Call log function.  */
+        if (log_function ((_Unwind_Context *) &saved_vrs, pid, tfd, stack_level,
+                          map, sp_list, at_fault) != _URC_NO_REASON) {
+            code = _URC_FAILURE;
+            break;
+        }
+        stack_level++;
+
+        /* Call the pr to decide what to do.  */
+        code = ((personality_routine_with_ptrace) UCB_PR_ADDR (ucbp))(
+                _US_VIRTUAL_UNWIND_FRAME | _US_FORCE_UNWIND, ucbp, 
+                (void *) &saved_vrs, pid);
+    /* 
+     * In theory the unwinding process will stop when the end of stack is
+     * reached or there is no unwinding information for the code address.
+     * To add another level of guarantee that the unwinding process
+     * will terminate we will stop it when the STACK_CONTENT_DEPTH is reached.
+     */
+    } while (code != _URC_END_OF_STACK && code != _URC_FAILURE && 
+             stack_level < STACK_CONTENT_DEPTH);
+    return stack_level;
+}
+
+
+/* Derived version to use ptrace */
+/* Common implementation for ARM ABI defined personality routines.
+   ID is the index of the personality routine, other arguments are as defined
+   by __aeabi_unwind_cpp_pr{0,1,2}.  */
+
+static _Unwind_Reason_Code
+unwind_pr_common_with_ptrace (_Unwind_State state,
+			_Unwind_Control_Block *ucbp,
+			_Unwind_Context *context,
+			int id,
+            pid_t pid)
+{
+  __gnu_unwind_state uws;
+  _uw *data;
+  int phase2_call_unexpected_after_unwind = 0;
+
+  state &= _US_ACTION_MASK;
+
+  data = (_uw *) ucbp->pr_cache.ehtp;
+  uws.data = get_remote_word(pid, data);
+  data++;
+  uws.next = data;
+  if (id == 0)
+    {
+      uws.data <<= 8;
+      uws.words_left = 0;
+      uws.bytes_left = 3;
+    }
+  else
+    {
+      uws.words_left = (uws.data >> 16) & 0xff;
+      uws.data <<= 16;
+      uws.bytes_left = 2;
+      data += uws.words_left;
+    }
+
+  /* Restore the saved pointer.  */
+  if (state == _US_UNWIND_FRAME_RESUME)
+    data = (_uw *) ucbp->cleanup_cache.bitpattern[0];
+
+  if ((ucbp->pr_cache.additional & 1) == 0)
+    {
+      /* Process descriptors.  */
+      while (get_remote_word(pid, data)) {
+      /**********************************************************************
+       * The original code here seems to deal with exceptions that are not
+       * applicable in our toolchain, thus there is no way to test it for now.
+       * Instead of leaving it here and causing potential instability in
+       * debuggerd, we'd better punt here and leave the stack unwound.
+       * In the future when we discover cases where the stack should be unwound
+       * further but is not, we can revisit the code here.
+       **********************************************************************/
+        return _URC_FAILURE;
+	  }
+	  /* Finished processing this descriptor.  */
+    }
+
+  if (unwind_execute_with_ptrace (context, &uws, pid) != _URC_OK)
+    return _URC_FAILURE;
+
+  if (phase2_call_unexpected_after_unwind)
+    {
+      /* Enter __cxa_unexpected as if called from the call site.  */
+      _Unwind_SetGR (context, R_LR, _Unwind_GetGR (context, R_PC));
+      _Unwind_SetGR (context, R_PC, (_uw) &__cxa_call_unexpected);
+      return _URC_INSTALL_CONTEXT;
+    }
+
+  return _URC_CONTINUE_UNWIND;
+}
+
+
+/* ABI defined personality routine entry points.  */
+
+static _Unwind_Reason_Code
+unwind_cpp_pr0_with_ptrace (_Unwind_State state,
+			_Unwind_Control_Block *ucbp,
+			_Unwind_Context *context,
+            pid_t pid)
+{
+  return unwind_pr_common_with_ptrace (state, ucbp, context, 0, pid);
+}
+
+static _Unwind_Reason_Code
+unwind_cpp_pr1_with_ptrace (_Unwind_State state,
+			_Unwind_Control_Block *ucbp,
+			_Unwind_Context *context,
+            pid_t pid)
+{
+  return unwind_pr_common_with_ptrace (state, ucbp, context, 1, pid);
+}
+
+static _Unwind_Reason_Code
+unwind_cpp_pr2_with_ptrace (_Unwind_State state,
+			_Unwind_Control_Block *ucbp,
+			_Unwind_Context *context,
+            pid_t pid)
+{
+  return unwind_pr_common_with_ptrace (state, ucbp, context, 2, pid);
+}
diff --git a/debuggerd/debuggerd.c b/debuggerd/debuggerd.c
index 5a180f1..2acf26d 100644
--- a/debuggerd/debuggerd.c
+++ b/debuggerd/debuggerd.c
@@ -28,26 +28,83 @@
 #include <sys/wait.h>
 #include <sys/exec_elf.h>
 #include <sys/stat.h>
-#include <sys/poll.h>
 
 #include <cutils/sockets.h>
 #include <cutils/logd.h>
 #include <cutils/logger.h>
 #include <cutils/properties.h>
 
-#include <corkscrew/backtrace.h>
-
 #include <linux/input.h>
 
 #include <private/android_filesystem_config.h>
 
-#include "getevent.h"
-#include "machine.h"
+#include "debuggerd.h"
 #include "utility.h"
 
 #define ANDROID_LOG_INFO 4
 
-static void dump_build_info(int tfd)
+void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...)
+    __attribute__ ((format(printf, 3, 4)));
+
+/* Log information onto the tombstone */
+void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...)
+{
+    char buf[512];
+
+    va_list ap;
+    va_start(ap, fmt);
+
+    if (tfd >= 0) {
+        int len;
+        vsnprintf(buf, sizeof(buf), fmt, ap);
+        len = strlen(buf);
+        if(tfd >= 0) write(tfd, buf, len);
+    }
+
+    if (!in_tombstone_only)
+        __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
+    va_end(ap);
+}
+
+// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419   /system/lib/libcomposer.so
+// 012345678901234567890123456789012345678901234567890123456789
+// 0         1         2         3         4         5
+
+mapinfo *parse_maps_line(char *line)
+{
+    mapinfo *mi;
+    int len = strlen(line);
+
+    if (len < 1) return 0;      /* not expected */
+    line[--len] = 0;
+
+    if (len < 50) {
+        mi = malloc(sizeof(mapinfo) + 1);
+    } else {
+        mi = malloc(sizeof(mapinfo) + (len - 47));
+    }
+    if (mi == 0) return 0;
+
+    mi->isExecutable = (line[20] == 'x');
+
+    mi->start = strtoul(line, 0, 16);
+    mi->end = strtoul(line + 9, 0, 16);
+    /* To be filled in parse_elf_info if the mapped section starts with
+     * elf_header
+     */
+    mi->exidx_start = mi->exidx_end = 0;
+    mi->symbols = 0;
+    mi->next = 0;
+    if (len < 50) {
+        mi->name[0] = '\0';
+    } else {
+        strcpy(mi->name, line + 49);
+    }
+
+    return mi;
+}
+
+void dump_build_info(int tfd)
 {
     char fingerprint[PROPERTY_VALUE_MAX];
 
@@ -56,7 +113,7 @@
     _LOG(tfd, false, "Build fingerprint: '%s'\n", fingerprint);
 }
 
-static const char *get_signame(int sig)
+const char *get_signame(int sig)
 {
     switch(sig) {
     case SIGILL:     return "SIGILL";
@@ -65,12 +122,11 @@
     case SIGFPE:     return "SIGFPE";
     case SIGSEGV:    return "SIGSEGV";
     case SIGSTKFLT:  return "SIGSTKFLT";
-    case SIGSTOP:    return "SIGSTOP";
     default:         return "?";
     }
 }
 
-static const char *get_sigcode(int signo, int code)
+const char *get_sigcode(int signo, int code)
 {
     switch (signo) {
     case SIGILL:
@@ -114,7 +170,7 @@
     return "?";
 }
 
-static void dump_fault_addr(int tfd, pid_t pid, int sig)
+void dump_fault_addr(int tfd, int pid, int sig)
 {
     siginfo_t si;
 
@@ -132,7 +188,7 @@
     }
 }
 
-static void dump_crash_banner(int tfd, pid_t pid, pid_t tid, int sig)
+void dump_crash_banner(int tfd, unsigned pid, unsigned tid, int sig)
 {
     char data[1024];
     char *x = 0;
@@ -146,62 +202,225 @@
     }
 
     _LOG(tfd, false,
-            "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
+         "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
     dump_build_info(tfd);
     _LOG(tfd, false, "pid: %d, tid: %d  >>> %s <<<\n",
          pid, tid, x ? x : "UNKNOWN");
 
-    if(sig) {
-        dump_fault_addr(tfd, tid, sig);
+    if(sig) dump_fault_addr(tfd, tid, sig);
+}
+
+static void parse_elf_info(mapinfo *milist, pid_t pid)
+{
+    mapinfo *mi;
+    for (mi = milist; mi != NULL; mi = mi->next) {
+        if (!mi->isExecutable)
+            continue;
+
+        Elf32_Ehdr ehdr;
+
+        memset(&ehdr, 0, sizeof(Elf32_Ehdr));
+        /* Read in sizeof(Elf32_Ehdr) worth of data from the beginning of
+         * mapped section.
+         */
+        get_remote_struct(pid, (void *) (mi->start), &ehdr,
+                          sizeof(Elf32_Ehdr));
+        /* Check if it has the matching magic words */
+        if (IS_ELF(ehdr)) {
+            Elf32_Phdr phdr;
+            Elf32_Phdr *ptr;
+            int i;
+
+            ptr = (Elf32_Phdr *) (mi->start + ehdr.e_phoff);
+            for (i = 0; i < ehdr.e_phnum; i++) {
+                /* Parse the program header */
+                get_remote_struct(pid, (char *) (ptr+i), &phdr,
+                                  sizeof(Elf32_Phdr));
+#ifdef __arm__
+                /* Found a EXIDX segment? */
+                if (phdr.p_type == PT_ARM_EXIDX) {
+                    mi->exidx_start = mi->start + phdr.p_offset;
+                    mi->exidx_end = mi->exidx_start + phdr.p_filesz;
+                    break;
+                }
+#endif
+            }
+
+            /* Try to load symbols from this file */
+            mi->symbols = symbol_table_create(mi->name);
+        }
     }
 }
 
-/* Return true if some thread is not detached cleanly */
-static bool dump_sibling_thread_report(ptrace_context_t* context,
-        int tfd, pid_t pid, pid_t tid) {
-    char task_path[64];
-    snprintf(task_path, sizeof(task_path), "/proc/%d/task", pid);
+void dump_crash_report(int tfd, unsigned pid, unsigned tid, bool at_fault)
+{
+    char data[1024];
+    FILE *fp;
+    mapinfo *milist = 0;
+    unsigned int sp_list[STACK_CONTENT_DEPTH];
+    int stack_depth;
+#ifdef __arm__
+    int frame0_pc_sane = 1;
+#endif
 
-    DIR* d = opendir(task_path);
+    if (!at_fault) {
+        _LOG(tfd, true,
+         "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
+        _LOG(tfd, true, "pid: %d, tid: %d\n", pid, tid);
+    }
+
+    dump_registers(tfd, tid, at_fault);
+
+    /* Clear stack pointer records */
+    memset(sp_list, 0, sizeof(sp_list));
+
+    sprintf(data, "/proc/%d/maps", pid);
+    fp = fopen(data, "r");
+    if(fp) {
+        while(fgets(data, 1024, fp)) {
+            mapinfo *mi = parse_maps_line(data);
+            if(mi) {
+                mi->next = milist;
+                milist = mi;
+            }
+        }
+        fclose(fp);
+    }
+
+    parse_elf_info(milist, tid);
+
+#if __arm__
+    /* If stack unwinder fails, use the default solution to dump the stack
+     * content.
+     */
+    stack_depth = unwind_backtrace_with_ptrace(tfd, tid, milist, sp_list,
+                                               &frame0_pc_sane, at_fault);
+
+    /* The stack unwinder should at least unwind two levels of stack. If less
+     * level is seen we make sure at lease pc and lr are dumped.
+     */
+    if (stack_depth < 2) {
+        dump_pc_and_lr(tfd, tid, milist, stack_depth, at_fault);
+    }
+
+    dump_stack_and_code(tfd, tid, milist, stack_depth, sp_list, at_fault);
+#elif __i386__
+    /* If stack unwinder fails, use the default solution to dump the stack
+    * content.
+    */
+    stack_depth = unwind_backtrace_with_ptrace_x86(tfd, tid, milist,at_fault);
+#else
+#error "Unsupported architecture"
+#endif
+
+    while(milist) {
+        mapinfo *next = milist->next;
+        symbol_table_free(milist->symbols);
+        free(milist);
+        milist = next;
+    }
+}
+
+#define MAX_TOMBSTONES	10
+
+#define typecheck(x,y) {    \
+    typeof(x) __dummy1;     \
+    typeof(y) __dummy2;     \
+    (void)(&__dummy1 == &__dummy2); }
+
+#define TOMBSTONE_DIR	"/data/tombstones"
+
+/*
+ * find_and_open_tombstone - find an available tombstone slot, if any, of the
+ * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
+ * file is available, we reuse the least-recently-modified file.
+ */
+static int find_and_open_tombstone(void)
+{
+    unsigned long mtime = ULONG_MAX;
+    struct stat sb;
+    char path[128];
+    int fd, i, oldest = 0;
+
+    /*
+     * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
+     * to, our logic breaks. This check will generate a warning if that happens.
+     */
+    typecheck(mtime, sb.st_mtime);
+
+    /*
+     * In a single wolf-like pass, find an available slot and, in case none
+     * exist, find and record the least-recently-modified file.
+     */
+    for (i = 0; i < MAX_TOMBSTONES; i++) {
+        snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
+
+        if (!stat(path, &sb)) {
+            if (sb.st_mtime < mtime) {
+                oldest = i;
+                mtime = sb.st_mtime;
+            }
+            continue;
+        }
+        if (errno != ENOENT)
+            continue;
+
+        fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
+        if (fd < 0)
+            continue;	/* raced ? */
+
+        fchown(fd, AID_SYSTEM, AID_SYSTEM);
+        return fd;
+    }
+
+    /* we didn't find an available file, so we clobber the oldest one */
+    snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
+    fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
+    fchown(fd, AID_SYSTEM, AID_SYSTEM);
+
+    return fd;
+}
+
+/* Return true if some thread is not detached cleanly */
+static bool dump_sibling_thread_report(int tfd, unsigned pid, unsigned tid)
+{
+    char task_path[1024];
+
+    sprintf(task_path, "/proc/%d/task", pid);
+    DIR *d;
+    struct dirent *de;
+    int need_cleanup = 0;
+
+    d = opendir(task_path);
     /* Bail early if cannot open the task directory */
     if (d == NULL) {
         XLOG("Cannot open /proc/%d/task\n", pid);
         return false;
     }
-
-    bool detach_failed = false;
-    struct dirent *de;
     while ((de = readdir(d)) != NULL) {
-        pid_t new_tid;
+        unsigned new_tid;
         /* Ignore "." and ".." */
-        if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
+        if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
             continue;
-        }
-
         new_tid = atoi(de->d_name);
         /* The main thread at fault has been handled individually */
-        if (new_tid == tid) {
+        if (new_tid == tid)
             continue;
-        }
 
         /* Skip this thread if cannot ptrace it */
-        if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0) {
+        if (ptrace(PTRACE_ATTACH, new_tid, 0, 0) < 0)
             continue;
-        }
 
-        _LOG(tfd, true, "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
-        _LOG(tfd, true, "pid: %d, tid: %d\n", pid, new_tid);
-
-        dump_thread(context, tfd, new_tid, false);
+        dump_crash_report(tfd, pid, new_tid, false);
 
         if (ptrace(PTRACE_DETACH, new_tid, 0, 0) != 0) {
-            LOG("ptrace detach from %d failed: %s\n", new_tid, strerror(errno));
-            detach_failed = true;
+            XLOG("detach of tid %d failed: %s\n", new_tid, strerror(errno));
+            need_cleanup = 1;
         }
     }
-
     closedir(d);
-    return detach_failed;
+
+    return need_cleanup != 0;
 }
 
 /*
@@ -210,7 +429,7 @@
  *
  * If "tailOnly" is set, we only print the last few lines.
  */
-static void dump_log_file(int tfd, pid_t pid, const char* filename,
+static void dump_log_file(int tfd, unsigned pid, const char* filename,
     bool tailOnly)
 {
     bool first = true;
@@ -342,122 +561,52 @@
  * Dumps the logs generated by the specified pid to the tombstone, from both
  * "system" and "main" log devices.  Ideally we'd interleave the output.
  */
-static void dump_logs(int tfd, pid_t pid, bool tailOnly)
+static void dump_logs(int tfd, unsigned pid, bool tailOnly)
 {
     dump_log_file(tfd, pid, "/dev/log/system", tailOnly);
     dump_log_file(tfd, pid, "/dev/log/main", tailOnly);
 }
 
-/*
- * Dumps all information about the specified pid to the tombstone.
- */
-static bool dump_crash(int tfd, pid_t pid, pid_t tid, int signal,
-        bool dump_sibling_threads)
+/* Return true if some thread is not detached cleanly */
+static bool engrave_tombstone(unsigned pid, unsigned tid, int debug_uid,
+                              int signal)
 {
+    int fd;
+    bool need_cleanup = false;
+
     /* don't copy log messages to tombstone unless this is a dev device */
     char value[PROPERTY_VALUE_MAX];
     property_get("ro.debuggable", value, "0");
     bool wantLogs = (value[0] == '1');
 
-    dump_crash_banner(tfd, pid, tid, signal);
-
-    ptrace_context_t* context = load_ptrace_context(pid);
-
-    dump_thread(context, tfd, tid, true);
-
-    if (wantLogs) {
-        dump_logs(tfd, pid, true);
-    }
-
-    bool detach_failed = false;
-    if (dump_sibling_threads) {
-        detach_failed = dump_sibling_thread_report(context, tfd, pid, tid);
-    }
-
-    free_ptrace_context(context);
-
-    if (wantLogs) {
-        dump_logs(tfd, pid, false);
-    }
-    return detach_failed;
-}
-
-#define MAX_TOMBSTONES	10
-
-#define typecheck(x,y) {    \
-    typeof(x) __dummy1;     \
-    typeof(y) __dummy2;     \
-    (void)(&__dummy1 == &__dummy2); }
-
-#define TOMBSTONE_DIR	"/data/tombstones"
-
-/*
- * find_and_open_tombstone - find an available tombstone slot, if any, of the
- * form tombstone_XX where XX is 00 to MAX_TOMBSTONES-1, inclusive. If no
- * file is available, we reuse the least-recently-modified file.
- */
-static int find_and_open_tombstone(void)
-{
-    unsigned long mtime = ULONG_MAX;
-    struct stat sb;
-    char path[128];
-    int fd, i, oldest = 0;
-
-    /*
-     * XXX: Our stat.st_mtime isn't time_t. If it changes, as it probably ought
-     * to, our logic breaks. This check will generate a warning if that happens.
-     */
-    typecheck(mtime, sb.st_mtime);
-
-    /*
-     * In a single wolf-like pass, find an available slot and, in case none
-     * exist, find and record the least-recently-modified file.
-     */
-    for (i = 0; i < MAX_TOMBSTONES; i++) {
-        snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", i);
-
-        if (!stat(path, &sb)) {
-            if (sb.st_mtime < mtime) {
-                oldest = i;
-                mtime = sb.st_mtime;
-            }
-            continue;
-        }
-        if (errno != ENOENT)
-            continue;
-
-        fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0600);
-        if (fd < 0)
-            continue;	/* raced ? */
-
-        fchown(fd, AID_SYSTEM, AID_SYSTEM);
-        return fd;
-    }
-
-    /* we didn't find an available file, so we clobber the oldest one */
-    snprintf(path, sizeof(path), TOMBSTONE_DIR"/tombstone_%02d", oldest);
-    fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
-    fchown(fd, AID_SYSTEM, AID_SYSTEM);
-
-    return fd;
-}
-
-/* Return true if some thread is not detached cleanly */
-static bool engrave_tombstone(pid_t pid, pid_t tid, int signal,
-        bool dump_sibling_threads)
-{
     mkdir(TOMBSTONE_DIR, 0755);
     chown(TOMBSTONE_DIR, AID_SYSTEM, AID_SYSTEM);
 
-    int fd = find_and_open_tombstone();
-    if (fd < 0) {
-        return false;
+    fd = find_and_open_tombstone();
+    if (fd < 0)
+        return need_cleanup;
+
+    dump_crash_banner(fd, pid, tid, signal);
+    dump_crash_report(fd, pid, tid, true);
+
+    if (wantLogs) {
+        dump_logs(fd, pid, true);
     }
 
-    bool detach_failed = dump_crash(fd, pid, tid, signal, dump_sibling_threads);
+    /*
+     * If the user has requested to attach gdb, don't collect the per-thread
+     * information as it increases the chance to lose track of the process.
+     */
+    if ((signed)pid > debug_uid) {
+        need_cleanup = dump_sibling_thread_report(fd, pid, tid);
+    }
+
+    if (wantLogs) {
+        dump_logs(fd, pid, false);
+    }
 
     close(fd);
-    return detach_failed;
+    return need_cleanup;
 }
 
 static int
@@ -505,21 +654,25 @@
     write_string("/sys/class/leds/left/cadence", "0,0");
 }
 
-static void wait_for_user_action(pid_t pid) {
+extern int init_getevent();
+extern void uninit_getevent();
+extern int get_event(struct input_event* event, int timeout);
+
+static void wait_for_user_action(unsigned tid, struct ucred* cr)
+{
+    (void)tid;
     /* First log a helpful message */
     LOG(    "********************************************************\n"
             "* Process %d has been suspended while crashing.  To\n"
-            "* attach gdbserver for a gdb connection on port 5039\n"
-            "* and start gdbclient:\n"
+            "* attach gdbserver for a gdb connection on port 5039:\n"
             "*\n"
-            "*     gdbclient app_process :5039 %d\n"
+            "*     adb shell gdbserver :5039 --attach %d &\n"
             "*\n"
-            "* Wait for gdb to start, then press HOME or VOLUME DOWN key\n"
-            "* to let the process continue crashing.\n"
+            "* Press HOME key to let the process continue crashing.\n"
             "********************************************************\n",
-            pid, pid);
+            cr->pid, cr->pid);
 
-    /* wait for HOME or VOLUME DOWN key */
+    /* wait for HOME key (TODO: something useful for devices w/o HOME key) */
     if (init_getevent() == 0) {
         int ms = 1200 / 10;
         int dit = 1;
@@ -532,18 +685,15 @@
         };
         size_t s = 0;
         struct input_event e;
-        bool done = false;
+        int home = 0;
         init_debug_led();
         enable_debug_led();
         do {
             int timeout = abs((int)(codes[s])) * ms;
             int res = get_event(&e, timeout);
             if (res == 0) {
-                if (e.type == EV_KEY
-                        && (e.code == KEY_HOME || e.code == KEY_VOLUMEDOWN)
-                        && e.value == 0) {
-                    done = true;
-                }
+                if (e.type==EV_KEY && e.code==KEY_HOME && e.value==0)
+                    home = 1;
             } else if (res == 1) {
                 if (++s >= sizeof(codes)/sizeof(*codes))
                     s = 0;
@@ -553,281 +703,202 @@
                     disable_debug_led();
                 }
             }
-        } while (!done);
+        } while (!home);
         uninit_getevent();
     }
 
     /* don't forget to turn debug led off */
     disable_debug_led();
-    LOG("debuggerd resuming process %d", pid);
-}
 
-static int get_process_info(pid_t tid, pid_t* out_pid, uid_t* out_uid, uid_t* out_gid) {
-    char path[64];
-    snprintf(path, sizeof(path), "/proc/%d/status", tid);
+    /* close filedescriptor */
+    LOG("debuggerd resuming process %d", cr->pid);
+ }
 
-    FILE* fp = fopen(path, "r");
-    if (!fp) {
-        return -1;
-    }
-
-    int fields = 0;
-    char line[1024];
-    while (fgets(line, sizeof(line), fp)) {
-        size_t len = strlen(line);
-        if (len > 6 && !memcmp(line, "Tgid:\t", 6)) {
-            *out_pid = atoi(line + 6);
-            fields |= 1;
-        } else if (len > 5 && !memcmp(line, "Uid:\t", 5)) {
-            *out_uid = atoi(line + 5);
-            fields |= 2;
-        } else if (len > 5 && !memcmp(line, "Gid:\t", 5)) {
-            *out_gid = atoi(line + 5);
-            fields |= 4;
-        }
-    }
-    fclose(fp);
-    return fields == 7 ? 0 : -1;
-}
-
-static int wait_for_signal(pid_t tid, int* total_sleep_time_usec) {
-    const int sleep_time_usec = 200000;         /* 0.2 seconds */
-    const int max_total_sleep_usec = 3000000;   /* 3 seconds */
-    for (;;) {
-        int status;
-        pid_t n = waitpid(tid, &status, __WALL | WNOHANG);
-        if (n < 0) {
-            if(errno == EAGAIN) continue;
-            LOG("waitpid failed: %s\n", strerror(errno));
-            return -1;
-        } else if (n > 0) {
-            XLOG("waitpid: n=%d status=%08x\n", n, status);
-            if (WIFSTOPPED(status)) {
-                return WSTOPSIG(status);
-            } else {
-                LOG("unexpected waitpid response: n=%d, status=%08x\n", n, status);
-                return -1;
-            }
-        }
-
-        if (*total_sleep_time_usec > max_total_sleep_usec) {
-            LOG("timed out waiting for tid=%d to die\n", tid);
-            return -1;
-        }
-
-        /* not ready yet */
-        XLOG("not ready yet\n");
-        usleep(sleep_time_usec);
-        *total_sleep_time_usec += sleep_time_usec;
-    }
-}
-
-enum {
-    REQUEST_TYPE_CRASH,
-    REQUEST_TYPE_DUMP,
-};
-
-typedef struct {
-    int type;
-    pid_t pid, tid;
-    uid_t uid, gid;
-} request_t;
-
-static int read_request(int fd, request_t* out_request) {
+static void handle_crashing_process(int fd)
+{
+    char buf[64];
+    struct stat s;
+    unsigned tid;
     struct ucred cr;
-    int len = sizeof(cr);
-    int status = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
-    if (status != 0) {
+    int n, len, status;
+    int tid_attach_status = -1;
+    unsigned retry = 30;
+    bool need_cleanup = false;
+
+    char value[PROPERTY_VALUE_MAX];
+    property_get("debug.db.uid", value, "-1");
+    int debug_uid = atoi(value);
+
+    XLOG("handle_crashing_process(%d)\n", fd);
+
+    len = sizeof(cr);
+    n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
+    if(n != 0) {
         LOG("cannot get credentials\n");
-        return -1;
+        goto done;
     }
 
     XLOG("reading tid\n");
     fcntl(fd, F_SETFL, O_NONBLOCK);
-
-    struct pollfd pollfds[1];
-    pollfds[0].fd = fd;
-    pollfds[0].events = POLLIN;
-    pollfds[0].revents = 0;
-    status = TEMP_FAILURE_RETRY(poll(pollfds, 1, 3000));
-    if (status != 1) {
-        LOG("timed out reading tid\n");
-        return -1;
-    }
-
-    status = TEMP_FAILURE_RETRY(read(fd, &out_request->tid, sizeof(pid_t)));
-    if (status < 0) {
-        LOG("read failure? %s\n", strerror(errno));
-        return -1;
-    }
-    if (status != sizeof(pid_t)) {
-        LOG("invalid crash request of size %d\n", status);
-        return -1;
-    }
-
-    if (out_request->tid < 0 && cr.uid == 0) {
-        /* Root can ask us to attach to any process and dump it explicitly. */
-        out_request->type = REQUEST_TYPE_DUMP;
-        out_request->tid = -out_request->tid;
-        status = get_process_info(out_request->tid, &out_request->pid,
-                &out_request->uid, &out_request->gid);
-        if (status < 0) {
-            LOG("tid %d does not exist. ignoring explicit dump request\n",
-                    out_request->tid);
-            return -1;
+    while((n = read(fd, &tid, sizeof(unsigned))) != sizeof(unsigned)) {
+        if(errno == EINTR) continue;
+        if(errno == EWOULDBLOCK) {
+            if(retry-- > 0) {
+                usleep(100 * 1000);
+                continue;
+            }
+            LOG("timed out reading tid\n");
+            goto done;
         }
-        return 0;
+        LOG("read failure? %s\n", strerror(errno));
+        goto done;
     }
 
-    /* Ensure that the tid reported by the crashing process is valid. */
-    out_request->type = REQUEST_TYPE_CRASH;
-    out_request->pid = cr.pid;
-    out_request->uid = cr.uid;
-    out_request->gid = cr.gid;
-
-    char buf[64];
-    struct stat s;
-    snprintf(buf, sizeof buf, "/proc/%d/task/%d", out_request->pid, out_request->tid);
+    snprintf(buf, sizeof buf, "/proc/%d/task/%d", cr.pid, tid);
     if(stat(buf, &s)) {
         LOG("tid %d does not exist in pid %d. ignoring debug request\n",
-                out_request->tid, out_request->pid);
-        return -1;
+            tid, cr.pid);
+        close(fd);
+        return;
     }
-    return 0;
-}
 
-static bool should_attach_gdb(request_t* request) {
-    if (request->type == REQUEST_TYPE_CRASH) {
-        char value[PROPERTY_VALUE_MAX];
-        property_get("debug.db.uid", value, "-1");
-        int debug_uid = atoi(value);
-        return debug_uid >= 0 && request->uid <= (uid_t)debug_uid;
+    XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", cr.pid, cr.uid, cr.gid, tid);
+
+    /* Note that at this point, the target thread's signal handler
+     * is blocked in a read() call. This gives us the time to PTRACE_ATTACH
+     * to it before it has a chance to really fault.
+     *
+     * The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
+     * won't necessarily have stopped by the time ptrace() returns.  (We
+     * currently assume it does.)  We write to the file descriptor to
+     * ensure that it can run as soon as we call PTRACE_CONT below.
+     * See details in bionic/libc/linker/debugger.c, in function
+     * debugger_signal_handler().
+     */
+    tid_attach_status = ptrace(PTRACE_ATTACH, tid, 0, 0);
+    int ptrace_error = errno;
+
+    if (TEMP_FAILURE_RETRY(write(fd, &tid, 1)) != 1) {
+        XLOG("failed responding to client: %s\n",
+            strerror(errno));
+        goto done;
     }
-    return false;
-}
 
-static void handle_request(int fd) {
-    XLOG("handle_request(%d)\n", fd);
+    if(tid_attach_status < 0) {
+        LOG("ptrace attach failed: %s\n", strerror(ptrace_error));
+        goto done;
+    }
 
-    request_t request;
-    int status = read_request(fd, &request);
-    if (!status) {
-        XLOG("BOOM: pid=%d uid=%d gid=%d tid=%d\n", pid, uid, gid, tid);
+    close(fd);
+    fd = -1;
 
-        /* At this point, the thread that made the request is blocked in
-         * a read() call.  If the thread has crashed, then this gives us
-         * time to PTRACE_ATTACH to it before it has a chance to really fault.
-         *
-         * The PTRACE_ATTACH sends a SIGSTOP to the target process, but it
-         * won't necessarily have stopped by the time ptrace() returns.  (We
-         * currently assume it does.)  We write to the file descriptor to
-         * ensure that it can run as soon as we call PTRACE_CONT below.
-         * See details in bionic/libc/linker/debugger.c, in function
-         * debugger_signal_handler().
-         */
-        if (ptrace(PTRACE_ATTACH, request.tid, 0, 0)) {
-            LOG("ptrace attach failed: %s\n", strerror(errno));
-        } else {
-            bool detach_failed = false;
-            bool attach_gdb = should_attach_gdb(&request);
-            char response = 0;
-            if (TEMP_FAILURE_RETRY(write(fd, &response, 1)) != 1) {
-                LOG("failed responding to client: %s\n", strerror(errno));
-            } else {
-                close(fd);
-                fd = -1;
+    const int sleep_time_usec = 200000;         /* 0.2 seconds */
+    const int max_total_sleep_usec = 3000000;   /* 3 seconds */
+    int loop_limit = max_total_sleep_usec / sleep_time_usec;
+    for(;;) {
+        if (loop_limit-- == 0) {
+            LOG("timed out waiting for pid=%d tid=%d uid=%d to die\n",
+                cr.pid, tid, cr.uid);
+            goto done;
+        }
+        n = waitpid(tid, &status, __WALL | WNOHANG);
 
-                int total_sleep_time_usec = 0;
-                for (;;) {
-                    int signal = wait_for_signal(request.tid, &total_sleep_time_usec);
-                    if (signal < 0) {
-                        break;
-                    }
-
-                    switch (signal) {
-                    case SIGSTOP:
-                        if (request.type == REQUEST_TYPE_DUMP) {
-                            XLOG("stopped -- dumping\n");
-                            detach_failed = engrave_tombstone(request.pid, request.tid,
-                                    signal, true);
-                        } else {
-                            XLOG("stopped -- continuing\n");
-                            status = ptrace(PTRACE_CONT, request.tid, 0, 0);
-                            if (status) {
-                                LOG("ptrace continue failed: %s\n", strerror(errno));
-                            }
-                            continue; /* loop again */
-                        }
-                        break;
-
-                    case SIGILL:
-                    case SIGABRT:
-                    case SIGBUS:
-                    case SIGFPE:
-                    case SIGSEGV:
-                    case SIGSTKFLT: {
-                        XLOG("stopped -- fatal signal\n");
-                        /* don't dump sibling threads when attaching to GDB because it
-                         * makes the process less reliable, apparently... */
-                        detach_failed = engrave_tombstone(request.pid, request.tid,
-                                signal, !attach_gdb);
-                        break;
-                    }
-
-                    default:
-                        XLOG("stopped -- unexpected signal\n");
-                        LOG("process stopped due to unexpected signal %d\n", signal);
-                        break;
-                    }
-                    break;
-                }
-            }
-
-            XLOG("detaching\n");
-            if (attach_gdb) {
-                /* stop the process so we can debug */
-                kill(request.pid, SIGSTOP);
-
-                /* detach so we can attach gdbserver */
-                if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
-                    LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
-                    detach_failed = true;
-                }
-
-                /*
-                 * if debug.db.uid is set, its value indicates if we should wait
-                 * for user action for the crashing process.
-                 * in this case, we log a message and turn the debug LED on
-                 * waiting for a gdb connection (for instance)
-                 */
-                wait_for_user_action(request.pid);
-            } else {
-                /* just detach */
-                if (ptrace(PTRACE_DETACH, request.tid, 0, 0)) {
-                    LOG("ptrace detach from %d failed: %s\n", request.tid, strerror(errno));
-                    detach_failed = true;
-                }
-            }
-
-            /* resume stopped process (so it can crash in peace). */
-            kill(request.pid, SIGCONT);
-
-            /* If we didn't successfully detach, we're still the parent, and the
-             * actual parent won't receive a death notification via wait(2).  At this point
-             * there's not much we can do about that. */
-            if (detach_failed) {
-                LOG("debuggerd committing suicide to free the zombie!\n");
-                kill(getpid(), SIGKILL);
-            }
+        if (n == 0) {
+            /* not ready yet */
+            XLOG("not ready yet\n");
+            usleep(sleep_time_usec);
+            continue;
         }
 
+        if(n < 0) {
+            if(errno == EAGAIN) continue;
+            LOG("waitpid failed: %s\n", strerror(errno));
+            goto done;
+        }
+
+        XLOG("waitpid: n=%d status=%08x\n", n, status);
+
+        if(WIFSTOPPED(status)){
+            n = WSTOPSIG(status);
+            switch(n) {
+            case SIGSTOP:
+                XLOG("stopped -- continuing\n");
+                n = ptrace(PTRACE_CONT, tid, 0, 0);
+                if(n) {
+                    LOG("ptrace failed: %s\n", strerror(errno));
+                    goto done;
+                }
+                continue;
+
+            case SIGILL:
+            case SIGABRT:
+            case SIGBUS:
+            case SIGFPE:
+            case SIGSEGV:
+            case SIGSTKFLT: {
+                XLOG("stopped -- fatal signal\n");
+                need_cleanup = engrave_tombstone(cr.pid, tid, debug_uid, n);
+                kill(tid, SIGSTOP);
+                goto done;
+            }
+
+            default:
+                XLOG("stopped -- unexpected signal\n");
+                goto done;
+            }
+        } else {
+            XLOG("unexpected waitpid response\n");
+            goto done;
+        }
     }
-    if (fd >= 0) {
-        close(fd);
+
+done:
+    XLOG("detaching\n");
+
+    /* stop the process so we can debug */
+    kill(cr.pid, SIGSTOP);
+
+    /*
+     * If a thread has been attached by ptrace, make sure it is detached
+     * successfully otherwise we will get a zombie.
+     */
+    if (tid_attach_status == 0) {
+        int detach_status;
+        /* detach so we can attach gdbserver */
+        detach_status = ptrace(PTRACE_DETACH, tid, 0, 0);
+        need_cleanup |= (detach_status != 0);
     }
+
+    /*
+     * if debug.db.uid is set, its value indicates if we should wait
+     * for user action for the crashing process.
+     * in this case, we log a message and turn the debug LED on
+     * waiting for a gdb connection (for instance)
+     */
+
+    if ((signed)cr.uid <= debug_uid) {
+        wait_for_user_action(tid, &cr);
+    }
+
+    /*
+     * Resume stopped process (so it can crash in peace).  If we didn't
+     * successfully detach, we're still the parent, and the actual parent
+     * won't receive a death notification via wait(2).  At this point
+     * there's not much we can do about that.
+     */
+    kill(cr.pid, SIGCONT);
+
+    if (need_cleanup) {
+        LOG("debuggerd committing suicide to free the zombie!\n");
+        kill(getpid(), SIGKILL);
+    }
+
+    if(fd != -1) close(fd);
 }
 
-static int do_server() {
+
+int main()
+{
     int s;
     struct sigaction act;
     int logsocket = -1;
@@ -860,7 +931,7 @@
 
     s = socket_local_server("android:debuggerd",
             ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
-    if(s < 0) return 1;
+    if(s < 0) return -1;
     fcntl(s, F_SETFD, FD_CLOEXEC);
 
     LOG("debuggerd: " __DATE__ " " __TIME__ "\n");
@@ -880,42 +951,7 @@
 
         fcntl(fd, F_SETFD, FD_CLOEXEC);
 
-        handle_request(fd);
+        handle_crashing_process(fd);
     }
     return 0;
 }
-
-static int do_explicit_dump(pid_t tid) {
-    fprintf(stdout, "Sending request to dump task %d.\n", tid);
-
-    int fd = socket_local_client("android:debuggerd",
-            ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
-    if (fd < 0) {
-        fputs("Error opening local socket to debuggerd.\n", stderr);
-        return 1;
-    }
-
-    pid_t request = -tid;
-    write(fd, &request, sizeof(pid_t));
-    if (read(fd, &request, 1) != 1) {
-        /* did not get expected reply, debuggerd must have closed the socket */
-        fputs("Error sending request.  Did not receive reply from debuggerd.\n", stderr);
-    }
-    close(fd);
-    return 0;
-}
-
-int main(int argc, char** argv) {
-    if (argc == 2) {
-        pid_t tid = atoi(argv[1]);
-        if (!tid) {
-            fputs("Usage: [<tid>]\n"
-                    "\n"
-                    "If tid specified, sends a request to debuggerd to dump that task.\n"
-                    "Otherwise, starts the debuggerd server.\n", stderr);
-            return 1;
-        }
-        return do_explicit_dump(tid);
-    }
-    return do_server();
-}
diff --git a/debuggerd/debuggerd.h b/debuggerd/debuggerd.h
new file mode 100644
index 0000000..e3cdc7c
--- /dev/null
+++ b/debuggerd/debuggerd.h
@@ -0,0 +1,39 @@
+/* system/debuggerd/debuggerd.h
+**
+** Copyright 2006, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#include <cutils/logd.h>
+#include <sys/ptrace.h>
+#include <unwind.h>
+#include "utility.h"
+#include "symbol_table.h"
+
+
+/* Main entry point to get the backtrace from the crashing process */
+extern int unwind_backtrace_with_ptrace(int tfd, pid_t pid, mapinfo *map,
+                                        unsigned int sp_list[],
+                                        int *frame0_pc_sane,
+                                        bool at_fault);
+
+extern void dump_registers(int tfd, int pid, bool at_fault);
+
+extern int unwind_backtrace_with_ptrace_x86(int tfd, pid_t pid, mapinfo *map, bool at_fault);
+
+void dump_pc_and_lr(int tfd, int pid, mapinfo *map, int unwound_level, bool at_fault);
+
+void dump_stack_and_code(int tfd, int pid, mapinfo *map,
+                         int unwind_depth, unsigned int sp_list[],
+                         bool at_fault);
diff --git a/debuggerd/getevent.h b/debuggerd/getevent.h
deleted file mode 100644
index 426139d..0000000
--- a/debuggerd/getevent.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _DEBUGGERD_GETEVENT_H
-#define _DEBUGGERD_GETEVENT_H
-
-int init_getevent();
-void uninit_getevent();
-int get_event(struct input_event* event, int timeout);
-
-#endif // _DEBUGGERD_GETEVENT_H
diff --git a/debuggerd/machine.h b/debuggerd/machine.h
deleted file mode 100644
index f9ca6bd..0000000
--- a/debuggerd/machine.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _DEBUGGERD_MACHINE_H
-#define _DEBUGGERD_MACHINE_H
-
-#include <corkscrew/backtrace.h>
-#include <sys/types.h>
-
-void dump_thread(ptrace_context_t* context, int tfd, pid_t tid, bool at_fault);
-
-#endif // _DEBUGGERD_MACHINE_H
diff --git a/debuggerd/symbol_table.c b/debuggerd/symbol_table.c
new file mode 100644
index 0000000..23572a3
--- /dev/null
+++ b/debuggerd/symbol_table.c
@@ -0,0 +1,240 @@
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+
+#include "symbol_table.h"
+#include "utility.h"
+
+#include <linux/elf.h>
+
+// Compare func for qsort
+static int qcompar(const void *a, const void *b)
+{
+    return ((struct symbol*)a)->addr - ((struct symbol*)b)->addr;
+}
+
+// Compare func for bsearch
+static int bcompar(const void *addr, const void *element)
+{
+    struct symbol *symbol = (struct symbol*)element;
+
+    if((unsigned int)addr < symbol->addr) {
+        return -1;
+    }
+
+    if((unsigned int)addr - symbol->addr >= symbol->size) {
+        return 1;
+    }
+
+    return 0;
+}
+
+/*
+ *  Create a symbol table from a given file
+ *
+ *  Parameters:
+ *      filename - Filename to process
+ *
+ *  Returns:
+ *      A newly-allocated SymbolTable structure, or NULL if error.
+ *      Free symbol table with symbol_table_free()
+ */
+struct symbol_table *symbol_table_create(const char *filename)
+{
+    struct symbol_table *table = NULL;
+
+    // Open the file, and map it into memory
+    struct stat sb;
+    int length;
+    char *base;
+
+    XLOG2("Creating symbol table for %s\n", filename);
+    int fd = open(filename, O_RDONLY);
+
+    if(fd < 0) {
+        goto out;
+    }
+
+    fstat(fd, &sb);
+    length = sb.st_size;
+
+    base = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
+
+    if(!base) {
+        goto out_close;
+    }
+
+    // Parse the file header
+    Elf32_Ehdr *hdr = (Elf32_Ehdr*)base;
+    Elf32_Shdr *shdr = (Elf32_Shdr*)(base + hdr->e_shoff);
+
+    // Search for the dynamic symbols section
+    int sym_idx = -1;
+    int dynsym_idx = -1;
+    int i;
+
+    for(i = 0; i < hdr->e_shnum; i++) {
+        if(shdr[i].sh_type == SHT_SYMTAB ) {
+            sym_idx = i;
+        }
+        if(shdr[i].sh_type == SHT_DYNSYM ) {
+            dynsym_idx = i;
+        }
+    }
+    if ((dynsym_idx == -1) && (sym_idx == -1)) {
+        goto out_unmap;
+    }
+
+    table = malloc(sizeof(struct symbol_table));
+    if(!table) {
+        goto out_unmap;
+    }
+    table->name = strdup(filename);
+    table->num_symbols = 0;
+
+    Elf32_Sym *dynsyms = NULL;
+    Elf32_Sym *syms = NULL;
+    int dynnumsyms = 0;
+    int numsyms = 0;
+    char *dynstr = NULL;
+    char *str = NULL;
+
+    if (dynsym_idx != -1) {
+        dynsyms = (Elf32_Sym*)(base + shdr[dynsym_idx].sh_offset);
+        dynnumsyms = shdr[dynsym_idx].sh_size / shdr[dynsym_idx].sh_entsize;
+        int dynstr_idx = shdr[dynsym_idx].sh_link;
+        dynstr = base + shdr[dynstr_idx].sh_offset;
+    }
+
+    if (sym_idx != -1) {
+        syms = (Elf32_Sym*)(base + shdr[sym_idx].sh_offset);
+        numsyms = shdr[sym_idx].sh_size / shdr[sym_idx].sh_entsize;
+        int str_idx = shdr[sym_idx].sh_link;
+        str = base + shdr[str_idx].sh_offset;
+    }
+
+    int symbol_count = 0;
+    int dynsymbol_count = 0;
+
+    if (dynsym_idx != -1) {
+        // Iterate through the dynamic symbol table, and count how many symbols
+        // are actually defined
+        for(i = 0; i < dynnumsyms; i++) {
+            if(dynsyms[i].st_shndx != SHN_UNDEF) {
+                dynsymbol_count++;
+            }
+        }
+        XLOG2("Dynamic Symbol count: %d\n", dynsymbol_count);
+    }
+
+    if (sym_idx != -1) {
+        // Iterate through the symbol table, and count how many symbols
+        // are actually defined
+        for(i = 0; i < numsyms; i++) {
+            if((syms[i].st_shndx != SHN_UNDEF) &&
+                (strlen(str+syms[i].st_name)) &&
+                (syms[i].st_value != 0) && (syms[i].st_size != 0)) {
+                symbol_count++;
+            }
+        }
+        XLOG2("Symbol count: %d\n", symbol_count);
+    }
+
+    // Now, create an entry in our symbol table structure for each symbol...
+    table->num_symbols += symbol_count + dynsymbol_count;
+    table->symbols = malloc(table->num_symbols * sizeof(struct symbol));
+    if(!table->symbols) {
+        free(table);
+        table = NULL;
+        goto out_unmap;
+    }
+
+
+    int j = 0;
+    if (dynsym_idx != -1) {
+        // ...and populate them
+        for(i = 0; i < dynnumsyms; i++) {
+            if(dynsyms[i].st_shndx != SHN_UNDEF) {
+                table->symbols[j].name = strdup(dynstr + dynsyms[i].st_name);
+                table->symbols[j].addr = dynsyms[i].st_value;
+                table->symbols[j].size = dynsyms[i].st_size;
+                XLOG2("name: %s, addr: %x, size: %x\n",
+                    table->symbols[j].name, table->symbols[j].addr, table->symbols[j].size);
+                j++;
+            }
+        }
+    }
+
+    if (sym_idx != -1) {
+        // ...and populate them
+        for(i = 0; i < numsyms; i++) {
+            if((syms[i].st_shndx != SHN_UNDEF) &&
+                (strlen(str+syms[i].st_name)) &&
+                (syms[i].st_value != 0) && (syms[i].st_size != 0)) {
+                table->symbols[j].name = strdup(str + syms[i].st_name);
+                table->symbols[j].addr = syms[i].st_value;
+                table->symbols[j].size = syms[i].st_size;
+                XLOG2("name: %s, addr: %x, size: %x\n",
+                    table->symbols[j].name, table->symbols[j].addr, table->symbols[j].size);
+                j++;
+            }
+        }
+    }
+
+    // Sort the symbol table entries, so they can be bsearched later
+    qsort(table->symbols, table->num_symbols, sizeof(struct symbol), qcompar);
+
+out_unmap:
+    munmap(base, length);
+
+out_close:
+    close(fd);
+
+out:
+    return table;
+}
+
+/*
+ * Free a symbol table
+ *
+ * Parameters:
+ *     table - Table to free
+ */
+void symbol_table_free(struct symbol_table *table)
+{
+    int i;
+
+    if(!table) {
+        return;
+    }
+
+    for(i=0; i<table->num_symbols; i++) {
+        free(table->symbols[i].name);
+    }
+
+    free(table->symbols);
+    free(table);
+}
+
+/*
+ * Search for an address in the symbol table
+ *
+ * Parameters:
+ *      table - Table to search in
+ *      addr - Address to search for.
+ *
+ * Returns:
+ *      A pointer to the Symbol structure corresponding to the
+ *      symbol which contains this address, or NULL if no symbol
+ *      contains it.
+ */
+const struct symbol *symbol_table_lookup(struct symbol_table *table, unsigned int addr)
+{
+    if(!table) {
+        return NULL;
+    }
+
+    return bsearch((void*)addr, table->symbols, table->num_symbols, sizeof(struct symbol), bcompar);
+}
diff --git a/debuggerd/symbol_table.h b/debuggerd/symbol_table.h
new file mode 100644
index 0000000..7f41f91
--- /dev/null
+++ b/debuggerd/symbol_table.h
@@ -0,0 +1,20 @@
+#ifndef SYMBOL_TABLE_H
+#define SYMBOL_TABLE_H
+
+struct symbol {
+    unsigned int addr;
+    unsigned int size;
+    char *name;
+};
+
+struct symbol_table {
+    struct symbol *symbols;
+    int num_symbols;
+    char *name;
+};
+
+struct symbol_table *symbol_table_create(const char *filename);
+void symbol_table_free(struct symbol_table *table);
+const struct symbol *symbol_table_lookup(struct symbol_table *table, unsigned int addr);
+
+#endif
diff --git a/debuggerd/utility.c b/debuggerd/utility.c
index c0fb13a..409209c 100644
--- a/debuggerd/utility.c
+++ b/debuggerd/utility.c
@@ -15,37 +15,81 @@
 ** limitations under the License.
 */
 
-#include <signal.h>
-#include <string.h>
-#include <cutils/logd.h>
 #include <sys/ptrace.h>
+#include <sys/exec_elf.h>
+#include <signal.h>
+#include <assert.h>
+#include <string.h>
 #include <errno.h>
-#include <corkscrew/demangle.h>
 
 #include "utility.h"
 
-#define STACK_DEPTH 32
-#define STACK_WORDS 16
-
-void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...) {
-    char buf[512];
-
-    va_list ap;
-    va_start(ap, fmt);
-
-    if (tfd >= 0) {
-        int len;
-        vsnprintf(buf, sizeof(buf), fmt, ap);
-        len = strlen(buf);
-        if(tfd >= 0) write(tfd, buf, len);
-    }
-
-    if (!in_tombstone_only)
-        __android_log_vprint(ANDROID_LOG_INFO, "DEBUG", fmt, ap);
-    va_end(ap);
+/* Get a word from pid using ptrace. The result is the return value. */
+int get_remote_word(int pid, void *src)
+{
+    return ptrace(PTRACE_PEEKTEXT, pid, src, NULL);
 }
 
-bool signal_has_address(int sig) {
+
+/* Handy routine to read aggregated data from pid using ptrace. The read 
+ * values are written to the dest locations directly. 
+ */
+void get_remote_struct(int pid, void *src, void *dst, size_t size)
+{
+    unsigned int i;
+
+    for (i = 0; i+4 <= size; i+=4) {
+        *(int *)((char *)dst+i) = ptrace(PTRACE_PEEKTEXT, pid, (char *)src+i, NULL);
+    }
+
+    if (i < size) {
+        int val;
+
+        assert((size - i) < 4);
+        val = ptrace(PTRACE_PEEKTEXT, pid, (char *)src+i, NULL);
+        while (i < size) {
+            ((unsigned char *)dst)[i] = val & 0xff;
+            i++;
+            val >>= 8;
+        }
+    }
+}
+
+/* Map a pc address to the name of the containing ELF file */
+const char *map_to_name(mapinfo *mi, unsigned pc, const char* def)
+{
+    while(mi) {
+        if((pc >= mi->start) && (pc < mi->end)){
+            return mi->name;
+        }
+        mi = mi->next;
+    }
+    return def;
+}
+
+/* Find the containing map info for the pc */
+const mapinfo *pc_to_mapinfo(mapinfo *mi, unsigned pc, unsigned *rel_pc)
+{
+    *rel_pc = pc;
+    while(mi) {
+        if((pc >= mi->start) && (pc < mi->end)){
+            // Only calculate the relative offset for shared libraries
+            if (strstr(mi->name, ".so")) {
+                *rel_pc -= mi->start;
+            }
+            return mi;
+        }
+        mi = mi->next;
+    }
+    return NULL;
+}
+
+/*
+ * Returns true if the specified signal has an associated address (i.e. it
+ * sets siginfo_t.si_addr).
+ */
+bool signal_has_address(int sig)
+{
     switch (sig) {
         case SIGILL:
         case SIGFPE:
@@ -56,248 +100,3 @@
             return false;
     }
 }
-
-static void dump_backtrace(ptrace_context_t* context __attribute((unused)),
-        int tfd, int pid __attribute((unused)), bool at_fault,
-        const backtrace_frame_t* backtrace, size_t frames) {
-    _LOG(tfd, !at_fault, "\nbacktrace:\n");
-
-    backtrace_symbol_t backtrace_symbols[STACK_DEPTH];
-    get_backtrace_symbols_ptrace(context, backtrace, frames, backtrace_symbols);
-    for (size_t i = 0; i < frames; i++) {
-        const backtrace_symbol_t* symbol = &backtrace_symbols[i];
-        const char* map_name = symbol->map_info ? symbol->map_info->name : "<unknown>";
-        const char* symbol_name = symbol->demangled_name ? symbol->demangled_name : symbol->name;
-        if (symbol_name) {
-            _LOG(tfd, !at_fault, "    #%02d  pc %08x  %s (%s)\n",
-                    (int)i, symbol->relative_pc, map_name, symbol_name);
-        } else {
-            _LOG(tfd, !at_fault, "    #%02d  pc %08x  %s\n",
-                    (int)i, symbol->relative_pc, map_name);
-        }
-    }
-    free_backtrace_symbols(backtrace_symbols, frames);
-}
-
-static void dump_stack_segment(ptrace_context_t* context, int tfd, int pid,
-        bool only_in_tombstone, uintptr_t* sp, size_t words, int label) {
-    for (size_t i = 0; i < words; i++) {
-        uint32_t stack_content;
-        if (!try_get_word(pid, *sp, &stack_content)) {
-            break;
-        }
-
-        const map_info_t* mi;
-        const symbol_t* symbol;
-        find_symbol_ptrace(context, stack_content, &mi, &symbol);
-
-        if (symbol) {
-            char* demangled_name = demangle_symbol_name(symbol->name);
-            const char* symbol_name = demangled_name ? demangled_name : symbol->name;
-            if (!i && label >= 0) {
-                _LOG(tfd, only_in_tombstone, "    #%02d  %08x  %08x  %s (%s)\n",
-                        label, *sp, stack_content, mi ? mi->name : "", symbol_name);
-            } else {
-                _LOG(tfd, only_in_tombstone, "         %08x  %08x  %s (%s)\n",
-                        *sp, stack_content, mi ? mi->name : "", symbol_name);
-            }
-            free(demangled_name);
-        } else {
-            if (!i && label >= 0) {
-                _LOG(tfd, only_in_tombstone, "    #%02d  %08x  %08x  %s\n",
-                        label, *sp, stack_content, mi ? mi->name : "");
-            } else {
-                _LOG(tfd, only_in_tombstone, "         %08x  %08x  %s\n",
-                        *sp, stack_content, mi ? mi->name : "");
-            }
-        }
-
-        *sp += sizeof(uint32_t);
-    }
-}
-
-static void dump_stack(ptrace_context_t* context, int tfd, int pid, bool at_fault,
-        const backtrace_frame_t* backtrace, size_t frames) {
-    bool have_first = false;
-    size_t first, last;
-    for (size_t i = 0; i < frames; i++) {
-        if (backtrace[i].stack_top) {
-            if (!have_first) {
-                have_first = true;
-                first = i;
-            }
-            last = i;
-        }
-    }
-    if (!have_first) {
-        return;
-    }
-
-    _LOG(tfd, !at_fault, "\nstack:\n");
-
-    // Dump a few words before the first frame.
-    bool only_in_tombstone = !at_fault;
-    uintptr_t sp = backtrace[first].stack_top - STACK_WORDS * sizeof(uint32_t);
-    dump_stack_segment(context, tfd, pid, only_in_tombstone, &sp, STACK_WORDS, -1);
-
-    // Dump a few words from all successive frames.
-    // Only log the first 3 frames, put the rest in the tombstone.
-    for (size_t i = first; i <= last; i++) {
-        const backtrace_frame_t* frame = &backtrace[i];
-        if (sp != frame->stack_top) {
-            _LOG(tfd, only_in_tombstone, "         ........  ........\n");
-            sp = frame->stack_top;
-        }
-        if (i - first == 3) {
-            only_in_tombstone = true;
-        }
-        if (i == last) {
-            dump_stack_segment(context, tfd, pid, only_in_tombstone, &sp, STACK_WORDS, i);
-            if (sp < frame->stack_top + frame->stack_size) {
-                _LOG(tfd, only_in_tombstone, "         ........  ........\n");
-            }
-        } else {
-            size_t words = frame->stack_size / sizeof(uint32_t);
-            if (words == 0) {
-                words = 1;
-            } else if (words > STACK_WORDS) {
-                words = STACK_WORDS;
-            }
-            dump_stack_segment(context, tfd, pid, only_in_tombstone, &sp, words, i);
-        }
-    }
-}
-
-void dump_backtrace_and_stack(ptrace_context_t* context, int tfd, pid_t tid, bool at_fault) {
-    backtrace_frame_t backtrace[STACK_DEPTH];
-    ssize_t frames = unwind_backtrace_ptrace(tid, context, backtrace, 0, STACK_DEPTH);
-    if (frames > 0) {
-        dump_backtrace(context, tfd, tid, at_fault, backtrace, frames);
-        dump_stack(context, tfd, tid, at_fault, backtrace, frames);
-    }
-}
-
-void dump_memory(int tfd, pid_t tid, uintptr_t addr, bool at_fault) {
-    char code_buffer[64];       /* actual 8+1+((8+1)*4) + 1 == 45 */
-    char ascii_buffer[32];      /* actual 16 + 1 == 17 */
-    uintptr_t p, end;
-
-    p = addr & ~3;
-    p -= 32;
-    if (p > addr) {
-        /* catch underflow */
-        p = 0;
-    }
-    end = p + 80;
-    /* catch overflow; 'end - p' has to be multiples of 16 */
-    while (end < p)
-        end -= 16;
-
-    /* Dump the code around PC as:
-     *  addr     contents                             ascii
-     *  00008d34 ef000000 e8bd0090 e1b00000 512fff1e  ............../Q
-     *  00008d44 ea00b1f9 e92d0090 e3a070fc ef000000  ......-..p......
-     */
-    while (p < end) {
-        char* asc_out = ascii_buffer;
-
-        sprintf(code_buffer, "%08x ", p);
-
-        int i;
-        for (i = 0; i < 4; i++) {
-            /*
-             * If we see (data == -1 && errno != 0), we know that the ptrace
-             * call failed, probably because we're dumping memory in an
-             * unmapped or inaccessible page.  I don't know if there's
-             * value in making that explicit in the output -- it likely
-             * just complicates parsing and clarifies nothing for the
-             * enlightened reader.
-             */
-            long data = ptrace(PTRACE_PEEKTEXT, tid, (void*)p, NULL);
-            sprintf(code_buffer + strlen(code_buffer), "%08lx ", data);
-
-            int j;
-            for (j = 0; j < 4; j++) {
-                /*
-                 * Our isprint() allows high-ASCII characters that display
-                 * differently (often badly) in different viewers, so we
-                 * just use a simpler test.
-                 */
-                char val = (data >> (j*8)) & 0xff;
-                if (val >= 0x20 && val < 0x7f) {
-                    *asc_out++ = val;
-                } else {
-                    *asc_out++ = '.';
-                }
-            }
-            p += 4;
-        }
-        *asc_out = '\0';
-        _LOG(tfd, !at_fault, "    %s %s\n", code_buffer, ascii_buffer);
-    }
-}
-
-void dump_nearby_maps(ptrace_context_t* context, int tfd, pid_t tid) {
-    siginfo_t si;
-    memset(&si, 0, sizeof(si));
-    if (ptrace(PTRACE_GETSIGINFO, tid, 0, &si)) {
-        _LOG(tfd, false, "cannot get siginfo for %d: %s\n",
-                tid, strerror(errno));
-        return;
-    }
-    if (!signal_has_address(si.si_signo)) {
-        return;
-    }
-
-    uintptr_t addr = (uintptr_t) si.si_addr;
-    addr &= ~0xfff;     /* round to 4K page boundary */
-    if (addr == 0) {    /* null-pointer deref */
-        return;
-    }
-
-    _LOG(tfd, false, "\nmemory map around fault addr %08x:\n", (int)si.si_addr);
-
-    /*
-     * Search for a match, or for a hole where the match would be.  The list
-     * is backward from the file content, so it starts at high addresses.
-     */
-    bool found = false;
-    map_info_t* map = context->map_info_list;
-    map_info_t *next = NULL;
-    map_info_t *prev = NULL;
-    while (map != NULL) {
-        if (addr >= map->start && addr < map->end) {
-            found = true;
-            next = map->next;
-            break;
-        } else if (addr >= map->end) {
-            /* map would be between "prev" and this entry */
-            next = map;
-            map = NULL;
-            break;
-        }
-
-        prev = map;
-        map = map->next;
-    }
-
-    /*
-     * Show "next" then "match" then "prev" so that the addresses appear in
-     * ascending order (like /proc/pid/maps).
-     */
-    if (next != NULL) {
-        _LOG(tfd, false, "    %08x-%08x %s\n", next->start, next->end, next->name);
-    } else {
-        _LOG(tfd, false, "    (no map below)\n");
-    }
-    if (map != NULL) {
-        _LOG(tfd, false, "    %08x-%08x %s\n", map->start, map->end, map->name);
-    } else {
-        _LOG(tfd, false, "    (no map for address)\n");
-    }
-    if (prev != NULL) {
-        _LOG(tfd, false, "    %08x-%08x %s\n", prev->start, prev->end, prev->name);
-    } else {
-        _LOG(tfd, false, "    (no map above)\n");
-    }
-}
diff --git a/debuggerd/utility.h b/debuggerd/utility.h
index 879c8b4..4a935d2 100644
--- a/debuggerd/utility.h
+++ b/debuggerd/utility.h
@@ -15,17 +15,50 @@
 ** limitations under the License.
 */
 
-#ifndef _DEBUGGERD_UTILITY_H
-#define _DEBUGGERD_UTILITY_H
+#ifndef __utility_h
+#define __utility_h
 
 #include <stddef.h>
 #include <stdbool.h>
-#include <sys/types.h>
-#include <corkscrew/backtrace.h>
 
-/* Log information onto the tombstone. */
-void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...)
-        __attribute__ ((format(printf, 3, 4)));
+#include "symbol_table.h"
+
+#ifndef PT_ARM_EXIDX
+#define PT_ARM_EXIDX    0x70000001      /* .ARM.exidx segment */
+#endif
+
+#define STACK_CONTENT_DEPTH 32
+
+typedef struct mapinfo {
+    struct mapinfo *next;
+    unsigned start;
+    unsigned end;
+    unsigned exidx_start;
+    unsigned exidx_end;
+    struct symbol_table *symbols;
+    bool isExecutable;
+    char name[];
+} mapinfo;
+
+/* Get a word from pid using ptrace. The result is the return value. */
+extern int get_remote_word(int pid, void *src);
+
+/* Handy routine to read aggregated data from pid using ptrace. The read 
+ * values are written to the dest locations directly. 
+ */
+extern void get_remote_struct(int pid, void *src, void *dst, size_t size);
+
+/* Find the containing map for the pc */
+const mapinfo *pc_to_mapinfo (mapinfo *mi, unsigned pc, unsigned *rel_pc);
+
+/* Map a pc address to the name of the containing ELF file */
+const char *map_to_name(mapinfo *mi, unsigned pc, const char* def);
+
+/* Log information onto the tombstone */
+extern void _LOG(int tfd, bool in_tombstone_only, const char *fmt, ...);
+
+/* Determine whether si_addr is valid for this signal */
+bool signal_has_address(int sig);
 
 #define LOG(fmt...) _LOG(-1, 0, fmt)
 
@@ -43,30 +76,4 @@
 #define XLOG2(fmt...) do {} while(0)
 #endif
 
-/*
- * Returns true if the specified signal has an associated address.
- * (i.e. it sets siginfo_t.si_addr).
- */
-bool signal_has_address(int sig);
-
-/*
- * Dumps the backtrace and contents of the stack.
- */
-void dump_backtrace_and_stack(ptrace_context_t* context, int tfd, pid_t pid, bool at_fault);
-
-/*
- * Dumps a few bytes of memory, starting a bit before and ending a bit
- * after the specified address.
- */
-void dump_memory(int tfd, pid_t tid, uintptr_t addr, bool at_fault);
-
-/*
- * If this isn't clearly a null pointer dereference, dump the
- * /proc/maps entries near the fault address.
- *
- * This only makes sense to do on the thread that crashed.
- */
-void dump_nearby_maps(ptrace_context_t* context, int tfd, pid_t tid);
-
-
-#endif // _DEBUGGERD_UTILITY_H
+#endif
diff --git a/debuggerd/x86/machine.c b/debuggerd/x86/machine.c
index 57f51c8..9d418cf 100644
--- a/debuggerd/x86/machine.c
+++ b/debuggerd/x86/machine.c
@@ -31,43 +31,31 @@
 #include <cutils/sockets.h>
 #include <cutils/properties.h>
 
-#include <corkscrew/backtrace.h>
-#include <corkscrew/ptrace.h>
-
 #include <linux/input.h>
 
-#include "../machine.h"
 #include "../utility.h"
+#include "x86_utility.h"
 
-static void dump_registers(ptrace_context_t* context __attribute((unused)),
-        int tfd, pid_t pid, bool at_fault) {
+void dump_registers(int tfd, int pid, bool at_fault)
+{
     struct pt_regs_x86 r;
     bool only_in_tombstone = !at_fault;
 
     if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
-        _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno));
+        _LOG(tfd, only_in_tombstone,
+             "cannot get registers: %s\n", strerror(errno));
         return;
     }
-    //if there is no stack, no print just like arm
+//if there is no stack, no print just like arm
     if(!r.ebp)
         return;
-    _LOG(tfd, only_in_tombstone, "    eax %08x  ebx %08x  ecx %08x  edx %08x\n",
+    _LOG(tfd, only_in_tombstone, " eax %08x  ebx %08x  ecx %08x  edx %08x\n",
          r.eax, r.ebx, r.ecx, r.edx);
-    _LOG(tfd, only_in_tombstone, "    esi %08x  edi %08x\n",
+    _LOG(tfd, only_in_tombstone, " esi %08x  edi %08x\n",
          r.esi, r.edi);
-    _LOG(tfd, only_in_tombstone, "    xcs %08x  xds %08x  xes %08x  xfs %08x  xss %08x\n",
+    _LOG(tfd, only_in_tombstone, " xcs %08x  xds %08x  xes %08x  xfs %08x xss %08x\n",
          r.xcs, r.xds, r.xes, r.xfs, r.xss);
-    _LOG(tfd, only_in_tombstone, "    eip %08x  ebp %08x  esp %08x  flags %08x\n",
+    _LOG(tfd, only_in_tombstone,
+         " eip %08x  ebp %08x  esp %08x  flags %08x\n",
          r.eip, r.ebp, r.esp, r.eflags);
 }
-
-void dump_thread(ptrace_context_t* context, int tfd, pid_t tid, bool at_fault) {
-    dump_registers(context, tfd, tid, at_fault);
-
-    dump_backtrace_and_stack(context, tfd, tid, at_fault);
-
-    if (at_fault) {
-        dump_nearby_maps(context, tfd, tid);
-    }
-}
-
diff --git a/debuggerd/x86/unwind.c b/debuggerd/x86/unwind.c
new file mode 100644
index 0000000..0a7f04c
--- /dev/null
+++ b/debuggerd/x86/unwind.c
@@ -0,0 +1,86 @@
+#include <cutils/logd.h>
+#include <sys/ptrace.h>
+#include "../utility.h"
+#include "x86_utility.h"
+
+
+int unwind_backtrace_with_ptrace_x86(int tfd, pid_t pid, mapinfo *map,
+                                 bool at_fault)
+{
+    struct pt_regs_x86 r;
+    unsigned int stack_level = 0;
+    unsigned int stack_depth = 0;
+    unsigned int rel_pc;
+    unsigned int stack_ptr;
+    unsigned int stack_content;
+
+    if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return 0;
+    unsigned int eip = (unsigned int)r.eip;
+    unsigned int ebp = (unsigned int)r.ebp;
+    unsigned int cur_sp = (unsigned int)r.esp;
+    const mapinfo *mi;
+    const struct symbol* sym = 0;
+
+
+//ebp==0, it indicates that the stack is poped to the bottom or there is no stack at all.
+    while (ebp) {
+        mi = pc_to_mapinfo(map, eip, &rel_pc);
+
+        /* See if we can determine what symbol this stack frame resides in */
+        if (mi != 0 && mi->symbols != 0) {
+            sym = symbol_table_lookup(mi->symbols, rel_pc);
+        }
+        if (sym) {
+            _LOG(tfd, !at_fault, "    #%02d  eip: %08x  %s (%s)\n",
+                 stack_level, eip, mi ? mi->name : "", sym->name);
+        } else {
+            _LOG(tfd, !at_fault, "    #%02d  eip: %08x  %s\n",
+                 stack_level, eip, mi ? mi->name : "");
+        }
+
+        stack_level++;
+        if (stack_level >= STACK_DEPTH || eip == 0)
+            break;
+        eip = ptrace(PTRACE_PEEKTEXT, pid, (void*)(ebp + 4), NULL);
+        ebp = ptrace(PTRACE_PEEKTEXT, pid, (void*)ebp, NULL);
+    }
+    ebp = (unsigned int)r.ebp;
+    stack_depth = stack_level;
+    stack_level = 0;
+    if (ebp)
+        _LOG(tfd, !at_fault, "stack: \n");
+    while (ebp) {
+        stack_ptr = cur_sp;
+        while((int)(ebp - stack_ptr) >= 0) {
+            stack_content = ptrace(PTRACE_PEEKTEXT, pid, (void*)stack_ptr, NULL);
+            mi = pc_to_mapinfo(map, stack_content, &rel_pc);
+
+            /* See if we can determine what symbol this stack frame resides in */
+            if (mi != 0 && mi->symbols != 0) {
+                sym = symbol_table_lookup(mi->symbols, rel_pc);
+            }
+            if (sym) {
+                _LOG(tfd, !at_fault, "    #%02d  %08x  %08x  %s (%s)\n",
+                     stack_level, stack_ptr, stack_content, mi ? mi->name : "", sym->name);
+            } else {
+                _LOG(tfd, !at_fault, "    #%02d  %08x  %08x  %s\n",
+                     stack_level, stack_ptr, stack_content, mi ? mi->name : "");
+            }
+
+            stack_ptr = stack_ptr + 4;
+            //the stack frame may be very deep.
+            if((int)(stack_ptr - cur_sp) >= STACK_FRAME_DEPTH) {
+                _LOG(tfd, !at_fault, "    ......  ......  \n");
+                break;
+            }
+        }
+        cur_sp = ebp + 4;
+        stack_level++;
+        if (stack_level >= STACK_DEPTH || stack_level >= stack_depth)
+            break;
+        ebp = ptrace(PTRACE_PEEKTEXT, pid, (void*)ebp, NULL);
+    }
+
+    return stack_depth;
+}
+
diff --git a/debuggerd/x86/x86_utility.h b/debuggerd/x86/x86_utility.h
new file mode 100644
index 0000000..ac6a885
--- /dev/null
+++ b/debuggerd/x86/x86_utility.h
@@ -0,0 +1,40 @@
+/*
+**
+** Copyright 2006, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#define STACK_DEPTH 8
+#define STACK_FRAME_DEPTH 64
+
+typedef struct pt_regs_x86 {
+    long ebx;
+    long ecx;
+    long edx;
+    long esi;
+    long edi;
+    long ebp;
+    long eax;
+    int  xds;
+    int  xes;
+    int  xfs;
+    int  xgs;
+    long orig_eax;
+    long eip;
+    int  xcs;
+    long eflags;
+    long esp;
+    int  xss;
+}pt_regs_x86;
+
diff --git a/include/corkscrew/backtrace.h b/include/corkscrew/backtrace.h
deleted file mode 100644
index cecb13d..0000000
--- a/include/corkscrew/backtrace.h
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* A stack unwinder. */
-
-#ifndef _CORKSCREW_BACKTRACE_H
-#define _CORKSCREW_BACKTRACE_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <sys/types.h>
-#include <corkscrew/ptrace.h>
-#include <corkscrew/map_info.h>
-#include <corkscrew/symbol_table.h>
-
-/*
- * Describes a single frame of a backtrace.
- */
-typedef struct {
-    uintptr_t absolute_pc;     /* absolute PC offset */
-    uintptr_t stack_top;       /* top of stack for this frame */
-    size_t stack_size;         /* size of this stack frame */
-} backtrace_frame_t;
-
-/*
- * Describes the symbols associated with a backtrace frame.
- */
-typedef struct {
-    uintptr_t relative_pc;       /* relative PC offset from the start of the library,
-                                    or the absolute PC if the library is unknown */
-    const map_info_t* map_info;  /* memory map of the library, or NULL if unknown */
-    const char* name;            /* symbol name, or NULL if unknown */
-    char* demangled_name;        /* demangled symbol name, or NULL if unknown */
-} backtrace_symbol_t;
-
-/*
- * Unwinds the call stack for the current thread of execution.
- * Populates the backtrace array with the program counters from the call stack.
- * Returns the number of frames collected, or -1 if an error occurred.
- */
-ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
-
-/*
- * Unwinds the call stack for a thread within this process.
- * Populates the backtrace array with the program counters from the call stack.
- * Returns the number of frames collected, or -1 if an error occurred.
- *
- * The task is briefly suspended while the backtrace is being collected.
- */
-ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
-        size_t ignore_depth, size_t max_depth);
-
-/*
- * Unwinds the call stack of a task within a remote process using ptrace().
- * Populates the backtrace array with the program counters from the call stack.
- * Returns the number of frames collected, or -1 if an error occurred.
- */
-ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
-        backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
-
-/*
- * Gets the symbols for each frame of a backtrace.
- * The symbols array must be big enough to hold one symbol record per frame.
- * The symbols must later be freed using free_backtrace_symbols.
- */
-void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
-        backtrace_symbol_t* backtrace_symbols);
-
-/*
- * Gets the symbols for each frame of a backtrace from a remote process.
- * The symbols array must be big enough to hold one symbol record per frame.
- * The symbols must later be freed using free_backtrace_symbols.
- */
-void get_backtrace_symbols_ptrace(const ptrace_context_t* context,
-        const backtrace_frame_t* backtrace, size_t frames,
-        backtrace_symbol_t* backtrace_symbols);
-
-/*
- * Frees the storage associated with backtrace symbols.
- */
-void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_BACKTRACE_H
diff --git a/include/corkscrew/demangle.h b/include/corkscrew/demangle.h
deleted file mode 100644
index 04b0225..0000000
--- a/include/corkscrew/demangle.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* C++ symbol name demangling. */
-
-#ifndef _CORKSCREW_DEMANGLE_H
-#define _CORKSCREW_DEMANGLE_H
-
-#include <sys/types.h>
-#include <stdbool.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * Demangles a C++ symbol name.
- * If name is NULL or if the name cannot be demangled, returns NULL.
- * Otherwise, returns a newly allocated string that contains the demangled name.
- *
- * The caller must free the returned string using free().
- */
-char* demangle_symbol_name(const char* name);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_DEMANGLE_H
diff --git a/include/corkscrew/map_info.h b/include/corkscrew/map_info.h
deleted file mode 100644
index a1c344f..0000000
--- a/include/corkscrew/map_info.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Process memory map. */
-
-#ifndef _CORKSCREW_MAP_INFO_H
-#define _CORKSCREW_MAP_INFO_H
-
-#include <sys/types.h>
-#include <stdbool.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef struct map_info {
-    struct map_info* next;
-    uintptr_t start;
-    uintptr_t end;
-    bool is_executable;
-    void* data; // arbitrary data associated with the map by the user, initially NULL
-    char name[];
-} map_info_t;
-
-/* Loads memory map from /proc/<tid>/maps. */
-map_info_t* load_map_info_list(pid_t tid);
-
-/* Frees memory map. */
-void free_map_info_list(map_info_t* milist);
-
-/* Finds the memory map that contains the specified address. */
-const map_info_t* find_map_info(const map_info_t* milist, uintptr_t addr);
-
-/* Gets the memory map for this process.  (result is cached) */
-const map_info_t* my_map_info_list();
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_MAP_INFO_H
diff --git a/include/corkscrew/ptrace.h b/include/corkscrew/ptrace.h
deleted file mode 100644
index 6acf9eb..0000000
--- a/include/corkscrew/ptrace.h
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Useful ptrace() utility functions. */
-
-#ifndef _CORKSCREW_PTRACE_H
-#define _CORKSCREW_PTRACE_H
-
-#include <corkscrew/map_info.h>
-#include <corkscrew/symbol_table.h>
-
-#include <sys/types.h>
-#include <stdbool.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Stores information about a process that is used for several different
- * ptrace() based operations. */
-typedef struct {
-    map_info_t* map_info_list;
-} ptrace_context_t;
-
-#if __i386__
-/* ptrace() register context. */
-typedef struct pt_regs_x86 {
-    uint32_t ebx;
-    uint32_t ecx;
-    uint32_t edx;
-    uint32_t esi;
-    uint32_t edi;
-    uint32_t ebp;
-    uint32_t eax;
-    uint32_t xds;
-    uint32_t xes;
-    uint32_t xfs;
-    uint32_t xgs;
-    uint32_t orig_eax;
-    uint32_t eip;
-    uint32_t xcs;
-    uint32_t eflags;
-    uint32_t esp;
-    uint32_t xss;
-} pt_regs_x86_t;
-#endif
-
-/*
- * Reads a word of memory safely.
- * Uses ptrace() if tid >= 0, local memory otherwise.
- * Returns false if the word could not be read.
- */
-bool try_get_word(pid_t tid, uintptr_t ptr, uint32_t* out_value);
-
-/*
- * Loads information needed for examining a remote process using ptrace().
- * The caller must already have successfully attached to the process
- * using ptrace().
- *
- * The context can be used for any threads belonging to that process
- * assuming ptrace() is attached to them before performing the actual
- * unwinding.  The context can continue to be used to decode backtraces
- * even after ptrace() has been detached from the process.
- */
-ptrace_context_t* load_ptrace_context(pid_t pid);
-
-/*
- * Frees a ptrace context.
- */
-void free_ptrace_context(ptrace_context_t* context);
-
-/*
- * Finds a symbol using ptrace.
- * Returns the containing map and information about the symbol, or
- * NULL if one or the other is not available.
- */
-void find_symbol_ptrace(const ptrace_context_t* context,
-        uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_PTRACE_H
diff --git a/include/corkscrew/symbol_table.h b/include/corkscrew/symbol_table.h
deleted file mode 100644
index 020c8b8..0000000
--- a/include/corkscrew/symbol_table.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _CORKSCREW_SYMBOL_TABLE_H
-#define _CORKSCREW_SYMBOL_TABLE_H
-
-#include <sys/types.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef struct {
-    uintptr_t start;
-    uintptr_t end;
-    char* name;
-} symbol_t;
-
-typedef struct {
-    symbol_t* symbols;
-    size_t num_symbols;
-} symbol_table_t;
-
-/*
- * Loads a symbol table from a given file.
- * Returns NULL on error.
- */
-symbol_table_t* load_symbol_table(const char* filename);
-
-/*
- * Frees a symbol table.
- */
-void free_symbol_table(symbol_table_t* table);
-
-/*
- * Finds a symbol associated with an address in the symbol table.
- * Returns NULL if not found.
- */
-const symbol_t* find_symbol(const symbol_table_t* table, uintptr_t addr);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_SYMBOL_TABLE_H
diff --git a/init/init.c b/init/init.c
index 95ae0bf..68e9f49 100755
--- a/init/init.c
+++ b/init/init.c
@@ -315,7 +315,11 @@
         /* if the service has not yet started, prevent
          * it from auto-starting with its class
          */
-    svc->flags |= how;
+    if (how == SVC_RESET) {
+        svc->flags |= (svc->flags & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET;
+    } else {
+        svc->flags |= how;
+    }
 
     if (svc->pid) {
         NOTICE("service '%s' is being killed\n", svc->name);
diff --git a/init/init.h b/init/init.h
index 2d98c7c..a91d9d4 100644
--- a/init/init.h
+++ b/init/init.h
@@ -71,6 +71,7 @@
 #define SVC_CRITICAL    0x20  /* will reboot into recovery if keeps crashing */
 #define SVC_RESET       0x40  /* Use when stopping a process, but not disabling
                                  so it can be restarted with its class */
+#define SVC_RC_DISABLED 0x80  /* Remember if the disabled flag was set in the rc script */
 
 #define NR_SVC_SUPP_GIDS 12    /* twelve supplementary groups */
 
diff --git a/init/init_parser.c b/init/init_parser.c
index fa813b9..13c94eb 100644
--- a/init/init_parser.c
+++ b/init/init_parser.c
@@ -499,6 +499,7 @@
         break;
     case K_disabled:
         svc->flags |= SVC_DISABLED;
+        svc->flags |= SVC_RC_DISABLED;
         break;
     case K_ioprio:
         if (nargs != 3) {
diff --git a/libcorkscrew/Android.mk b/libcorkscrew/Android.mk
deleted file mode 100644
index df019c3..0000000
--- a/libcorkscrew/Android.mk
+++ /dev/null
@@ -1,47 +0,0 @@
-# Copyright (C) 2011 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
-	backtrace.c \
-	backtrace-helper.c \
-	demangle.c \
-	map_info.c \
-	ptrace.c \
-	symbol_table.c
-
-ifeq ($(TARGET_ARCH),arm)
-LOCAL_SRC_FILES += \
-	arch-arm/backtrace-arm.c \
-	arch-arm/ptrace-arm.c
-LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH -DCORKSCREW_HAVE_LIBIBERTY
-LOCAL_LDFLAGS += -liberty
-endif
-ifeq ($(TARGET_ARCH),x86)
-LOCAL_SRC_FILES += \
-	arch-x86/backtrace-x86.c \
-	arch-x86/ptrace-x86.c
-LOCAL_CFLAGS += -DCORKSCREW_HAVE_ARCH
-endif
-
-LOCAL_SHARED_LIBRARIES += libdl libcutils
-
-LOCAL_CFLAGS += -std=gnu99 -Werror
-LOCAL_MODULE := libcorkscrew
-LOCAL_MODULE_TAGS := optional
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/libcorkscrew/MODULE_LICENSE_APACHE2 b/libcorkscrew/MODULE_LICENSE_APACHE2
deleted file mode 100644
index e69de29..0000000
--- a/libcorkscrew/MODULE_LICENSE_APACHE2
+++ /dev/null
diff --git a/libcorkscrew/NOTICE b/libcorkscrew/NOTICE
deleted file mode 100644
index becc120..0000000
--- a/libcorkscrew/NOTICE
+++ /dev/null
@@ -1,190 +0,0 @@
-
-   Copyright (c) 2011, The Android Open Source Project
-
-   Licensed under the Apache License, Version 2.0 (the "License");
-   you may not use this file except in compliance with the License.
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-
-
-                                 Apache License
-                           Version 2.0, January 2004
-                        http://www.apache.org/licenses/
-
-   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
-   1. Definitions.
-
-      "License" shall mean the terms and conditions for use, reproduction,
-      and distribution as defined by Sections 1 through 9 of this document.
-
-      "Licensor" shall mean the copyright owner or entity authorized by
-      the copyright owner that is granting the License.
-
-      "Legal Entity" shall mean the union of the acting entity and all
-      other entities that control, are controlled by, or are under common
-      control with that entity. For the purposes of this definition,
-      "control" means (i) the power, direct or indirect, to cause the
-      direction or management of such entity, whether by contract or
-      otherwise, or (ii) ownership of fifty percent (50%) or more of the
-      outstanding shares, or (iii) beneficial ownership of such entity.
-
-      "You" (or "Your") shall mean an individual or Legal Entity
-      exercising permissions granted by this License.
-
-      "Source" form shall mean the preferred form for making modifications,
-      including but not limited to software source code, documentation
-      source, and configuration files.
-
-      "Object" form shall mean any form resulting from mechanical
-      transformation or translation of a Source form, including but
-      not limited to compiled object code, generated documentation,
-      and conversions to other media types.
-
-      "Work" shall mean the work of authorship, whether in Source or
-      Object form, made available under the License, as indicated by a
-      copyright notice that is included in or attached to the work
-      (an example is provided in the Appendix below).
-
-      "Derivative Works" shall mean any work, whether in Source or Object
-      form, that is based on (or derived from) the Work and for which the
-      editorial revisions, annotations, elaborations, or other modifications
-      represent, as a whole, an original work of authorship. For the purposes
-      of this License, Derivative Works shall not include works that remain
-      separable from, or merely link (or bind by name) to the interfaces of,
-      the Work and Derivative Works thereof.
-
-      "Contribution" shall mean any work of authorship, including
-      the original version of the Work and any modifications or additions
-      to that Work or Derivative Works thereof, that is intentionally
-      submitted to Licensor for inclusion in the Work by the copyright owner
-      or by an individual or Legal Entity authorized to submit on behalf of
-      the copyright owner. For the purposes of this definition, "submitted"
-      means any form of electronic, verbal, or written communication sent
-      to the Licensor or its representatives, including but not limited to
-      communication on electronic mailing lists, source code control systems,
-      and issue tracking systems that are managed by, or on behalf of, the
-      Licensor for the purpose of discussing and improving the Work, but
-      excluding communication that is conspicuously marked or otherwise
-      designated in writing by the copyright owner as "Not a Contribution."
-
-      "Contributor" shall mean Licensor and any individual or Legal Entity
-      on behalf of whom a Contribution has been received by Licensor and
-      subsequently incorporated within the Work.
-
-   2. Grant of Copyright License. Subject to the terms and conditions of
-      this License, each Contributor hereby grants to You a perpetual,
-      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
-      copyright license to reproduce, prepare Derivative Works of,
-      publicly display, publicly perform, sublicense, and distribute the
-      Work and such Derivative Works in Source or Object form.
-
-   3. Grant of Patent License. Subject to the terms and conditions of
-      this License, each Contributor hereby grants to You a perpetual,
-      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
-      (except as stated in this section) patent license to make, have made,
-      use, offer to sell, sell, import, and otherwise transfer the Work,
-      where such license applies only to those patent claims licensable
-      by such Contributor that are necessarily infringed by their
-      Contribution(s) alone or by combination of their Contribution(s)
-      with the Work to which such Contribution(s) was submitted. If You
-      institute patent litigation against any entity (including a
-      cross-claim or counterclaim in a lawsuit) alleging that the Work
-      or a Contribution incorporated within the Work constitutes direct
-      or contributory patent infringement, then any patent licenses
-      granted to You under this License for that Work shall terminate
-      as of the date such litigation is filed.
-
-   4. Redistribution. You may reproduce and distribute copies of the
-      Work or Derivative Works thereof in any medium, with or without
-      modifications, and in Source or Object form, provided that You
-      meet the following conditions:
-
-      (a) You must give any other recipients of the Work or
-          Derivative Works a copy of this License; and
-
-      (b) You must cause any modified files to carry prominent notices
-          stating that You changed the files; and
-
-      (c) You must retain, in the Source form of any Derivative Works
-          that You distribute, all copyright, patent, trademark, and
-          attribution notices from the Source form of the Work,
-          excluding those notices that do not pertain to any part of
-          the Derivative Works; and
-
-      (d) If the Work includes a "NOTICE" text file as part of its
-          distribution, then any Derivative Works that You distribute must
-          include a readable copy of the attribution notices contained
-          within such NOTICE file, excluding those notices that do not
-          pertain to any part of the Derivative Works, in at least one
-          of the following places: within a NOTICE text file distributed
-          as part of the Derivative Works; within the Source form or
-          documentation, if provided along with the Derivative Works; or,
-          within a display generated by the Derivative Works, if and
-          wherever such third-party notices normally appear. The contents
-          of the NOTICE file are for informational purposes only and
-          do not modify the License. You may add Your own attribution
-          notices within Derivative Works that You distribute, alongside
-          or as an addendum to the NOTICE text from the Work, provided
-          that such additional attribution notices cannot be construed
-          as modifying the License.
-
-      You may add Your own copyright statement to Your modifications and
-      may provide additional or different license terms and conditions
-      for use, reproduction, or distribution of Your modifications, or
-      for any such Derivative Works as a whole, provided Your use,
-      reproduction, and distribution of the Work otherwise complies with
-      the conditions stated in this License.
-
-   5. Submission of Contributions. Unless You explicitly state otherwise,
-      any Contribution intentionally submitted for inclusion in the Work
-      by You to the Licensor shall be under the terms and conditions of
-      this License, without any additional terms or conditions.
-      Notwithstanding the above, nothing herein shall supersede or modify
-      the terms of any separate license agreement you may have executed
-      with Licensor regarding such Contributions.
-
-   6. Trademarks. This License does not grant permission to use the trade
-      names, trademarks, service marks, or product names of the Licensor,
-      except as required for reasonable and customary use in describing the
-      origin of the Work and reproducing the content of the NOTICE file.
-
-   7. Disclaimer of Warranty. Unless required by applicable law or
-      agreed to in writing, Licensor provides the Work (and each
-      Contributor provides its Contributions) on an "AS IS" BASIS,
-      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
-      implied, including, without limitation, any warranties or conditions
-      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
-      PARTICULAR PURPOSE. You are solely responsible for determining the
-      appropriateness of using or redistributing the Work and assume any
-      risks associated with Your exercise of permissions under this License.
-
-   8. Limitation of Liability. In no event and under no legal theory,
-      whether in tort (including negligence), contract, or otherwise,
-      unless required by applicable law (such as deliberate and grossly
-      negligent acts) or agreed to in writing, shall any Contributor be
-      liable to You for damages, including any direct, indirect, special,
-      incidental, or consequential damages of any character arising as a
-      result of this License or out of the use or inability to use the
-      Work (including but not limited to damages for loss of goodwill,
-      work stoppage, computer failure or malfunction, or any and all
-      other commercial damages or losses), even if such Contributor
-      has been advised of the possibility of such damages.
-
-   9. Accepting Warranty or Additional Liability. While redistributing
-      the Work or Derivative Works thereof, You may choose to offer,
-      and charge a fee for, acceptance of support, warranty, indemnity,
-      or other liability obligations and/or rights consistent with this
-      License. However, in accepting such obligations, You may act only
-      on Your own behalf and on Your sole responsibility, not on behalf
-      of any other Contributor, and only if You agree to indemnify,
-      defend, and hold each Contributor harmless for any liability
-      incurred by, or claims asserted against, such Contributor by reason
-      of your accepting any such warranty or additional liability.
-
-   END OF TERMS AND CONDITIONS
-
diff --git a/libcorkscrew/arch-arm/backtrace-arm.c b/libcorkscrew/arch-arm/backtrace-arm.c
deleted file mode 100644
index 597662e..0000000
--- a/libcorkscrew/arch-arm/backtrace-arm.c
+++ /dev/null
@@ -1,530 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/*
- * Backtracing functions for ARM.
- *
- * This implementation uses the exception unwinding tables provided by
- * the compiler to unwind call frames.  Refer to the ARM Exception Handling ABI
- * documentation (EHABI) for more details about what's going on here.
- *
- * An ELF binary may contain an EXIDX section that provides an index to
- * the exception handling table of each function, sorted by program
- * counter address.
- *
- * This implementation also supports unwinding other processes via ptrace().
- * In that case, the EXIDX section is found by reading the ELF section table
- * structures using ptrace().
- *
- * Because the tables are used for exception handling, it can happen that
- * a given function will not have an exception handling table.  In particular,
- * exceptions are assumed to only ever be thrown at call sites.  Therefore,
- * by definition leaf functions will not have exception handling tables.
- * This may make unwinding impossible in some cases although we can still get
- * some idea of the call stack by examining the PC and LR registers.
- *
- * As we are only interested in backtrace information, we do not need
- * to perform all of the work of unwinding such as restoring register
- * state and running cleanup functions.  Unwinding is performed virtually on
- * an abstract machine context consisting of just the ARM core registers.
- * Furthermore, we do not run generic "personality functions" because
- * we may not be in a position to execute arbitrary code, especially if
- * we are running in a signal handler or using ptrace()!
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../backtrace-arch.h"
-#include "../backtrace-helper.h"
-#include "../ptrace-arch.h"
-#include <corkscrew/ptrace.h>
-
-#include <stdlib.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <limits.h>
-#include <errno.h>
-#include <sys/ptrace.h>
-#include <sys/exec_elf.h>
-#include <cutils/log.h>
-
-/* Machine context at the time a signal was raised. */
-typedef struct ucontext {
-    uint32_t uc_flags;
-    struct ucontext* uc_link;
-    stack_t uc_stack;
-    struct sigcontext {
-        uint32_t trap_no;
-        uint32_t error_code;
-        uint32_t oldmask;
-        uint32_t gregs[16];
-        uint32_t arm_cpsr;
-        uint32_t fault_address;
-    } uc_mcontext;
-    uint32_t uc_sigmask;
-} ucontext_t;
-
-/* Unwind state. */
-typedef struct {
-    uint32_t gregs[16];
-} unwind_state_t;
-
-static const int R_SP = 13;
-static const int R_LR = 14;
-static const int R_PC = 15;
-
-/* Special EXIDX value that indicates that a frame cannot be unwound. */
-static const uint32_t EXIDX_CANTUNWIND = 1;
-
-/* Get the EXIDX section start and size for the module that contains a
- * given program counter address.
- *
- * When the executable is statically linked, the EXIDX section can be
- * accessed by querying the values of the __exidx_start and __exidx_end
- * symbols.
- *
- * When the executable is dynamically linked, the linker exports a function
- * called dl_unwind_find_exidx that obtains the EXIDX section for a given
- * absolute program counter address.
- *
- * Bionic exports a helpful function called __gnu_Unwind_Find_exidx that
- * handles both cases, so we use that here.
- */
-typedef long unsigned int* _Unwind_Ptr;
-extern _Unwind_Ptr __gnu_Unwind_Find_exidx(_Unwind_Ptr pc, int *pcount);
-
-static uintptr_t find_exidx(uintptr_t pc, size_t* out_exidx_size) {
-    int count;
-    uintptr_t start = (uintptr_t)__gnu_Unwind_Find_exidx((_Unwind_Ptr)pc, &count);
-    *out_exidx_size = count;
-    return start;
-}
-
-/* Transforms a 31-bit place-relative offset to an absolute address.
- * We assume the most significant bit is clear. */
-static uintptr_t prel_to_absolute(uintptr_t place, uint32_t prel_offset) {
-    return place + (((int32_t)(prel_offset << 1)) >> 1);
-}
-
-static uintptr_t get_exception_handler(
-        const ptrace_context_t* context, pid_t tid, uintptr_t pc) {
-    uintptr_t exidx_start;
-    size_t exidx_size;
-    const map_info_t* mi;
-    if (tid < 0) {
-        mi = NULL;
-        exidx_start = find_exidx(pc, &exidx_size);
-    } else {
-        mi = find_map_info(context->map_info_list, pc);
-        if (mi && mi->data) {
-            const map_info_data_t* data = (const map_info_data_t*)mi->data;
-            exidx_start = data->exidx_start;
-            exidx_size = data->exidx_size / 8;
-        } else {
-            exidx_start = 0;
-            exidx_size = 0;
-        }
-    }
-
-    // The PC points to the instruction following the branch.
-    // We want to find the exception handler entry that corresponds to the branch itself,
-    // so we offset the PC backwards into the previous instruction.
-    // ARM instructions are 4 bytes, Thumb are 2, so we just subtract two so we either
-    // end up in the middle (ARM) or at the beginning of the instruction (Thumb).
-    if (pc >= 2) {
-        pc -= 2;
-    }
-
-    uintptr_t handler = 0;
-    if (exidx_start) {
-        uint32_t low = 0;
-        uint32_t high = exidx_size;
-        while (low < high) {
-            uint32_t index = (low + high) / 2;
-            uintptr_t entry = exidx_start + index * 8;
-            uint32_t entry_prel_pc;
-            if (!try_get_word(tid, entry, &entry_prel_pc)) {
-                break;
-            }
-            uintptr_t entry_pc = prel_to_absolute(entry, entry_prel_pc);
-            if (pc < entry_pc) {
-                high = index;
-                continue;
-            }
-            if (index + 1 < exidx_size) {
-                uintptr_t next_entry = entry + 8;
-                uint32_t next_entry_prel_pc;
-                if (!try_get_word(tid, next_entry, &next_entry_prel_pc)) {
-                    break;
-                }
-                uintptr_t next_entry_pc = prel_to_absolute(next_entry, next_entry_prel_pc);
-                if (pc >= next_entry_pc) {
-                    low = index + 1;
-                    continue;
-                }
-            }
-
-            uintptr_t entry_handler_ptr = entry + 4;
-            uint32_t entry_handler;
-            if (!try_get_word(tid, entry_handler_ptr, &entry_handler)) {
-                break;
-            }
-            if (entry_handler & (1L << 31)) {
-                handler = entry_handler_ptr; // in-place handler data
-            } else if (entry_handler != EXIDX_CANTUNWIND) {
-                handler = prel_to_absolute(entry_handler_ptr, entry_handler);
-            }
-            break;
-        }
-    }
-    LOGV("get_exception_handler: pc=0x%08x, module='%s', module_start=0x%08x, "
-            "exidx_start=0x%08x, exidx_size=%d, handler=0x%08x",
-            pc, mi ? mi->name : "<unknown>", mi ? mi->start : 0,
-            exidx_start, exidx_size, handler);
-    return handler;
-}
-
-typedef struct {
-    uintptr_t ptr;
-    uint32_t word;
-} byte_stream_t;
-
-static bool try_next_byte(pid_t tid, byte_stream_t* stream, uint8_t* out_value) {
-    uint8_t result;
-    switch (stream->ptr & 3) {
-    case 0:
-        if (!try_get_word(tid, stream->ptr, &stream->word)) {
-            *out_value = 0;
-            return false;
-        }
-        *out_value = stream->word >> 24;
-        break;
-
-    case 1:
-        *out_value = stream->word >> 16;
-        break;
-
-    case 2:
-        *out_value = stream->word >> 8;
-        break;
-
-    default:
-        *out_value = stream->word;
-        break;
-    }
-
-    LOGV("next_byte: ptr=0x%08x, value=0x%02x", stream->ptr, *out_value);
-    stream->ptr += 1;
-    return true;
-}
-
-static void set_reg(unwind_state_t* state, uint32_t reg, uint32_t value) {
-    LOGV("set_reg: reg=%d, value=0x%08x", reg, value);
-    state->gregs[reg] = value;
-}
-
-static bool try_pop_registers(pid_t tid, unwind_state_t* state, uint32_t mask) {
-    uint32_t sp = state->gregs[R_SP];
-    bool sp_updated = false;
-    for (int i = 0; i < 16; i++) {
-        if (mask & (1 << i)) {
-            uint32_t value;
-            if (!try_get_word(tid, sp, &value)) {
-                return false;
-            }
-            if (i == R_SP) {
-                sp_updated = true;
-            }
-            set_reg(state, i, value);
-            sp += 4;
-        }
-    }
-    if (!sp_updated) {
-        set_reg(state, R_SP, sp);
-    }
-    return true;
-}
-
-/* Executes a built-in personality routine as defined in the EHABI.
- * Returns true if unwinding should continue.
- *
- * The data for the built-in personality routines consists of a sequence
- * of unwinding instructions, followed by a sequence of scope descriptors,
- * each of which has a length and offset encoded using 16-bit or 32-bit
- * values.
- *
- * We only care about the unwinding instructions.  They specify the
- * operations of an abstract machine whose purpose is to transform the
- * virtual register state (including the stack pointer) such that
- * the call frame is unwound and the PC register points to the call site.
- */
-static bool execute_personality_routine(pid_t tid, unwind_state_t* state,
-        byte_stream_t* stream, int pr_index) {
-    size_t size;
-    switch (pr_index) {
-    case 0: // Personality routine #0, short frame, descriptors have 16-bit scope.
-        size = 3;
-        break;
-    case 1: // Personality routine #1, long frame, descriptors have 16-bit scope.
-    case 2: { // Personality routine #2, long frame, descriptors have 32-bit scope.
-        uint8_t size_byte;
-        if (!try_next_byte(tid, stream, &size_byte)) {
-            return false;
-        }
-        size = (uint32_t)size_byte * sizeof(uint32_t) + 2;
-        break;
-    }
-    default: // Unknown personality routine.  Stop here.
-        return false;
-    }
-
-    bool pc_was_set = false;
-    while (size--) {
-        uint8_t op;
-        if (!try_next_byte(tid, stream, &op)) {
-            return false;
-        }
-        if ((op & 0xc0) == 0x00) {
-            // "vsp = vsp + (xxxxxx << 2) + 4"
-            set_reg(state, R_SP, state->gregs[R_SP] + ((op & 0x3f) << 2) + 4);
-        } else if ((op & 0xc0) == 0x40) {
-            // "vsp = vsp - (xxxxxx << 2) - 4"
-            set_reg(state, R_SP, state->gregs[R_SP] - ((op & 0x3f) << 2) - 4);
-        } else if ((op & 0xf0) == 0x80) {
-            uint8_t op2;
-            if (!(size--) || !try_next_byte(tid, stream, &op2)) {
-                return false;
-            }
-            uint32_t mask = (((uint32_t)op & 0x0f) << 12) | ((uint32_t)op2 << 4);
-            if (mask) {
-                // "Pop up to 12 integer registers under masks {r15-r12}, {r11-r4}"
-                if (!try_pop_registers(tid, state, mask)) {
-                    return false;
-                }
-                if (mask & (1 << R_PC)) {
-                    pc_was_set = true;
-                }
-            } else {
-                // "Refuse to unwind"
-                return false;
-            }
-        } else if ((op & 0xf0) == 0x90) {
-            if (op != 0x9d && op != 0x9f) {
-                // "Set vsp = r[nnnn]"
-                set_reg(state, R_SP, state->gregs[op & 0x0f]);
-            } else {
-                // "Reserved as prefix for ARM register to register moves"
-                // "Reserved as prefix for Intel Wireless MMX register to register moves"
-                return false;
-            }
-        } else if ((op & 0xf8) == 0xa0) {
-            // "Pop r4-r[4+nnn]"
-            uint32_t mask = (0x0ff0 >> (7 - (op & 0x07))) & 0x0ff0;
-            if (!try_pop_registers(tid, state, mask)) {
-                return false;
-            }
-        } else if ((op & 0xf8) == 0xa8) {
-            // "Pop r4-r[4+nnn], r14"
-            uint32_t mask = ((0x0ff0 >> (7 - (op & 0x07))) & 0x0ff0) | 0x4000;
-            if (!try_pop_registers(tid, state, mask)) {
-                return false;
-            }
-        } else if (op == 0xb0) {
-            // "Finish"
-            break;
-        } else if (op == 0xb1) {
-            uint8_t op2;
-            if (!(size--) || !try_next_byte(tid, stream, &op2)) {
-                return false;
-            }
-            if (op2 != 0x00 && (op2 & 0xf0) == 0x00) {
-                // "Pop integer registers under mask {r3, r2, r1, r0}"
-                if (!try_pop_registers(tid, state, op2)) {
-                    return false;
-                }
-            } else {
-                // "Spare"
-                return false;
-            }
-        } else if (op == 0xb2) {
-            // "vsp = vsp + 0x204 + (uleb128 << 2)"
-            uint32_t value = 0;
-            uint32_t shift = 0;
-            uint8_t op2;
-            do {
-                if (!(size--) || !try_next_byte(tid, stream, &op2)) {
-                    return false;
-                }
-                value |= (op2 & 0x7f) << shift;
-                shift += 7;
-            } while (op2 & 0x80);
-            set_reg(state, R_SP, state->gregs[R_SP] + (value << 2) + 0x204);
-        } else if (op == 0xb3) {
-            // "Pop VFP double-precision registers D[ssss]-D[ssss+cccc] saved (as if) by FSTMFDX"
-            uint8_t op2;
-            if (!(size--) || !try_next_byte(tid, stream, &op2)) {
-                return false;
-            }
-            set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op2 & 0x0f) * 8 + 12);
-        } else if ((op & 0xf8) == 0xb8) {
-            // "Pop VFP double-precision registers D[8]-D[8+nnn] saved (as if) by FSTMFDX"
-            set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op & 0x07) * 8 + 12);
-        } else if ((op & 0xf8) == 0xc0) {
-            // "Intel Wireless MMX pop wR[10]-wR[10+nnn]"
-            set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op & 0x07) * 8 + 8);
-        } else if (op == 0xc6) {
-            // "Intel Wireless MMX pop wR[ssss]-wR[ssss+cccc]"
-            uint8_t op2;
-            if (!(size--) || !try_next_byte(tid, stream, &op2)) {
-                return false;
-            }
-            set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op2 & 0x0f) * 8 + 8);
-        } else if (op == 0xc7) {
-            uint8_t op2;
-            if (!(size--) || !try_next_byte(tid, stream, &op2)) {
-                return false;
-            }
-            if (op2 != 0x00 && (op2 & 0xf0) == 0x00) {
-                // "Intel Wireless MMX pop wCGR registers under mask {wCGR3,2,1,0}"
-                set_reg(state, R_SP, state->gregs[R_SP] + __builtin_popcount(op2) * 4);
-            } else {
-                // "Spare"
-                return false;
-            }
-        } else if (op == 0xc8) {
-            // "Pop VFP double precision registers D[16+ssss]-D[16+ssss+cccc]
-            // saved (as if) by FSTMFD"
-            uint8_t op2;
-            if (!(size--) || !try_next_byte(tid, stream, &op2)) {
-                return false;
-            }
-            set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op2 & 0x0f) * 8 + 8);
-        } else if (op == 0xc9) {
-            // "Pop VFP double precision registers D[ssss]-D[ssss+cccc] saved (as if) by FSTMFDD"
-            uint8_t op2;
-            if (!(size--) || !try_next_byte(tid, stream, &op2)) {
-                return false;
-            }
-            set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op2 & 0x0f) * 8 + 8);
-        } else if ((op == 0xf8) == 0xd0) {
-            // "Pop VFP double-precision registers D[8]-D[8+nnn] saved (as if) by FSTMFDD"
-            set_reg(state, R_SP, state->gregs[R_SP] + (uint32_t)(op & 0x07) * 8 + 8);
-        } else {
-            // "Spare"
-            return false;
-        }
-    }
-    if (!pc_was_set) {
-        set_reg(state, R_PC, state->gregs[R_LR]);
-    }
-    return true;
-}
-
-static ssize_t unwind_backtrace_common(pid_t tid, const ptrace_context_t* context,
-        unwind_state_t* state, backtrace_frame_t* backtrace,
-        size_t ignore_depth, size_t max_depth) {
-    size_t ignored_frames = 0;
-    size_t returned_frames = 0;
-
-    uintptr_t handler = get_exception_handler(context, tid, state->gregs[R_PC]);
-    if (!handler) {
-        // If there is no handler for the PC, the program may have branched to
-        // an invalid address.  Check whether we have a handler for the LR
-        // where we came from and use that instead.
-        backtrace_frame_t* frame = add_backtrace_entry(state->gregs[R_PC], backtrace,
-                ignore_depth, max_depth, &ignored_frames, &returned_frames);
-        if (frame) {
-            frame->stack_top = state->gregs[R_SP];
-        }
-
-        handler = get_exception_handler(context, tid, state->gregs[R_LR]);
-        if (!handler) {
-            // We don't have a handler here either.  Unwinding will not be possible.
-            // Return the PC and LR (if it looks sane) and call it good.
-            if (state->gregs[R_LR] && state->gregs[R_LR] != state->gregs[R_PC]) {
-                // Don't return the SP for this second frame because we don't
-                // know how big the first one is so we don't know where this
-                // one starts.
-                add_backtrace_entry(state->gregs[R_LR], backtrace,
-                        ignore_depth, max_depth, &ignored_frames, &returned_frames);
-            }
-            return returned_frames;
-        }
-
-        // Ok, continue from the LR.
-        set_reg(state, R_PC, state->gregs[R_LR]);
-    }
-
-    while (handler && returned_frames < max_depth) {
-        backtrace_frame_t* frame = add_backtrace_entry(state->gregs[R_PC], backtrace,
-                ignore_depth, max_depth, &ignored_frames, &returned_frames);
-        if (frame) {
-            frame->stack_top = state->gregs[R_SP];
-        }
-
-        byte_stream_t stream;
-        stream.ptr = handler;
-        uint8_t pr;
-        if (!try_next_byte(tid, &stream, &pr)) {
-            break;
-        }
-        if ((pr & 0xf0) != 0x80) {
-            // The first word is a place-relative pointer to a generic personality
-            // routine function.  We don't support invoking such functions, so stop here.
-            break;
-        }
-
-        // The first byte indicates the personality routine to execute.
-        // Following bytes provide instructions to the personality routine.
-        if (!execute_personality_routine(tid, state, &stream, pr & 0x0f)) {
-            break;
-        }
-        if (frame && state->gregs[R_SP] > frame->stack_top) {
-            frame->stack_size = state->gregs[R_SP] - frame->stack_top;
-        }
-
-        handler = get_exception_handler(context, tid, state->gregs[R_PC]);
-    }
-    return returned_frames;
-}
-
-ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo, void* sigcontext,
-        backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
-    const ucontext_t* uc = (const ucontext_t*)sigcontext;
-
-    unwind_state_t state;
-    for (int i = 0; i < 16; i++) {
-        state.gregs[i] = uc->uc_mcontext.gregs[i];
-    }
-
-    return unwind_backtrace_common(-1, NULL, &state, backtrace, ignore_depth, max_depth);
-}
-
-ssize_t unwind_backtrace_ptrace_arch(pid_t tid, const ptrace_context_t* context,
-        backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
-    struct pt_regs regs;
-    if (ptrace(PTRACE_GETREGS, tid, 0, &regs)) {
-        return -1;
-    }
-
-    unwind_state_t state;
-    for (int i = 0; i < 16; i++) {
-        state.gregs[i] = regs.uregs[i];
-    }
-
-    return unwind_backtrace_common(tid, context, &state, backtrace, ignore_depth, max_depth);
-}
diff --git a/libcorkscrew/arch-arm/ptrace-arm.c b/libcorkscrew/arch-arm/ptrace-arm.c
deleted file mode 100644
index fd15505..0000000
--- a/libcorkscrew/arch-arm/ptrace-arm.c
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../ptrace-arch.h"
-
-#include <sys/exec_elf.h>
-#include <cutils/log.h>
-
-#ifndef PT_ARM_EXIDX
-#define PT_ARM_EXIDX 0x70000001
-#endif
-
-static void load_exidx_header(pid_t pid, map_info_t* mi,
-        uintptr_t* out_exidx_start, size_t* out_exidx_size) {
-    uint32_t elf_phoff;
-    uint32_t elf_phnum;
-    if (try_get_word(pid, mi->start + offsetof(Elf32_Ehdr, e_phoff), &elf_phoff)
-            && try_get_word(pid, mi->start + offsetof(Elf32_Ehdr, e_phnum), &elf_phnum)) {
-        for (uint32_t i = 0; i < elf_phnum; i++) {
-            uintptr_t elf_phdr = mi->start + elf_phoff + i * sizeof(Elf32_Phdr);
-            uint32_t elf_phdr_type;
-            if (!try_get_word(pid, elf_phdr + offsetof(Elf32_Phdr, p_type), &elf_phdr_type)) {
-                break;
-            }
-            if (elf_phdr_type == PT_ARM_EXIDX) {
-                uint32_t elf_phdr_offset;
-                uint32_t elf_phdr_filesz;
-                if (!try_get_word(pid, elf_phdr + offsetof(Elf32_Phdr, p_offset),
-                        &elf_phdr_offset)
-                        || !try_get_word(pid, elf_phdr + offsetof(Elf32_Phdr, p_filesz),
-                                &elf_phdr_filesz)) {
-                    break;
-                }
-                *out_exidx_start = mi->start + elf_phdr_offset;
-                *out_exidx_size = elf_phdr_filesz;
-                return;
-            }
-        }
-    }
-    *out_exidx_start = 0;
-    *out_exidx_size = 0;
-}
-
-void load_ptrace_map_info_data_arch(pid_t pid, map_info_t* mi, map_info_data_t* data) {
-    load_exidx_header(pid, mi, &data->exidx_start, &data->exidx_size);
-}
-
-void free_ptrace_map_info_data_arch(map_info_t* mi, map_info_data_t* data) {
-}
diff --git a/libcorkscrew/arch-x86/backtrace-x86.c b/libcorkscrew/arch-x86/backtrace-x86.c
deleted file mode 100644
index 1324899..0000000
--- a/libcorkscrew/arch-x86/backtrace-x86.c
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/*
- * Backtracing functions for x86.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../backtrace-arch.h"
-#include "../backtrace-helper.h"
-#include <corkscrew/ptrace.h>
-
-#include <stdlib.h>
-#include <signal.h>
-#include <stdbool.h>
-#include <limits.h>
-#include <errno.h>
-#include <sys/ptrace.h>
-#include <sys/exec_elf.h>
-#include <cutils/log.h>
-
-/* Machine context at the time a signal was raised. */
-typedef struct ucontext {
-    uint32_t uc_flags;
-    struct ucontext* uc_link;
-    stack_t uc_stack;
-    struct sigcontext {
-        uint32_t gs;
-        uint32_t fs;
-        uint32_t es;
-        uint32_t ds;
-        uint32_t edi;
-        uint32_t esi;
-        uint32_t ebp;
-        uint32_t esp;
-        uint32_t ebx;
-        uint32_t edx;
-        uint32_t ecx;
-        uint32_t eax;
-        uint32_t trapno;
-        uint32_t err;
-        uint32_t eip;
-        uint32_t cs;
-        uint32_t efl;
-        uint32_t uesp;
-        uint32_t ss;
-        void* fpregs;
-        uint32_t oldmask;
-        uint32_t cr2;
-    } uc_mcontext;
-    uint32_t uc_sigmask;
-} ucontext_t;
-
-/* Unwind state. */
-typedef struct {
-    uint32_t ebp;
-    uint32_t eip;
-    uint32_t esp;
-} unwind_state_t;
-
-static ssize_t unwind_backtrace_common(pid_t tid, const ptrace_context_t* context,
-        unwind_state_t* state, backtrace_frame_t* backtrace,
-        size_t ignore_depth, size_t max_depth) {
-    size_t ignored_frames = 0;
-    size_t returned_frames = 0;
-
-    while (state->ebp && returned_frames < max_depth) {
-        backtrace_frame_t* frame = add_backtrace_entry(state->eip,
-                backtrace, ignore_depth, max_depth,
-                &ignored_frames, &returned_frames);
-        uint32_t next_esp = state->ebp + 8;
-        if (frame) {
-            frame->stack_top = state->esp;
-            if (state->esp < next_esp) {
-                frame->stack_size = next_esp - state->esp;
-            }
-        }
-        state->esp = next_esp;
-        if (!try_get_word(tid, state->ebp + 4, &state->eip)
-                || !try_get_word(tid, state->ebp, &state->ebp)
-                || !state->eip) {
-            break;
-        }
-    }
-
-    return returned_frames;
-}
-
-ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo, void* sigcontext,
-        backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
-    const ucontext_t* uc = (const ucontext_t*)sigcontext;
-
-    unwind_state_t state;
-    state.ebp = uc->uc_mcontext.ebp;
-    state.eip = uc->uc_mcontext.eip;
-    state.esp = uc->uc_mcontext.esp;
-
-    return unwind_backtrace_common(-1, NULL, &state, backtrace, ignore_depth, max_depth);
-}
-
-ssize_t unwind_backtrace_ptrace_arch(pid_t tid, const ptrace_context_t* context,
-        backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
-    pt_regs_x86_t regs;
-    if (ptrace(PTRACE_GETREGS, tid, 0, &regs)) {
-        return -1;
-    }
-
-    unwind_state_t state;
-    state.ebp = regs.ebp;
-    state.eip = regs.eip;
-    state.esp = regs.esp;
-
-    return unwind_backtrace_common(tid, context, &state, backtrace, ignore_depth, max_depth);
-}
diff --git a/libcorkscrew/arch-x86/ptrace-x86.c b/libcorkscrew/arch-x86/ptrace-x86.c
deleted file mode 100644
index f0ea110..0000000
--- a/libcorkscrew/arch-x86/ptrace-x86.c
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "../ptrace-arch.h"
-
-#include <cutils/log.h>
-
-void load_ptrace_map_info_data_arch(pid_t pid, map_info_t* mi, map_info_data_t* data) {
-}
-
-void free_ptrace_map_info_data_arch(map_info_t* mi, map_info_data_t* data) {
-}
diff --git a/libcorkscrew/backtrace-arch.h b/libcorkscrew/backtrace-arch.h
deleted file mode 100644
index 80bca2b..0000000
--- a/libcorkscrew/backtrace-arch.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Architecture dependent functions. */
-
-#ifndef _CORKSCREW_BACKTRACE_ARCH_H
-#define _CORKSCREW_BACKTRACE_ARCH_H
-
-#include "ptrace-arch.h"
-#include <corkscrew/backtrace.h>
-
-#include <signal.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-ssize_t unwind_backtrace_signal_arch(siginfo_t* siginfo, void* sigcontext,
-        backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
-
-ssize_t unwind_backtrace_ptrace_arch(pid_t tid, const ptrace_context_t* context,
-        backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_BACKTRACE_ARCH_H
diff --git a/libcorkscrew/backtrace-helper.c b/libcorkscrew/backtrace-helper.c
deleted file mode 100644
index bf9d3f3..0000000
--- a/libcorkscrew/backtrace-helper.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "backtrace-helper.h"
-
-#include <cutils/log.h>
-
-backtrace_frame_t* add_backtrace_entry(uintptr_t pc, backtrace_frame_t* backtrace,
-        size_t ignore_depth, size_t max_depth,
-        size_t* ignored_frames, size_t* returned_frames) {
-    if (*ignored_frames < ignore_depth) {
-        *ignored_frames += 1;
-        return NULL;
-    }
-    if (*returned_frames >= max_depth) {
-        return NULL;
-    }
-    backtrace_frame_t* frame = &backtrace[*returned_frames];
-    frame->absolute_pc = pc;
-    frame->stack_top = 0;
-    frame->stack_size = 0;
-    *returned_frames += 1;
-    return frame;
-}
diff --git a/libcorkscrew/backtrace-helper.h b/libcorkscrew/backtrace-helper.h
deleted file mode 100644
index 4d8a874..0000000
--- a/libcorkscrew/backtrace-helper.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Backtrace helper functions. */
-
-#ifndef _CORKSCREW_BACKTRACE_HELPER_H
-#define _CORKSCREW_BACKTRACE_HELPER_H
-
-#include <corkscrew/backtrace.h>
-#include <sys/types.h>
-#include <stdbool.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/*
- * Add a program counter to a backtrace if it will fit.
- * Returns the newly added frame, or NULL if none.
- */
-backtrace_frame_t* add_backtrace_entry(uintptr_t pc,
-        backtrace_frame_t* backtrace,
-        size_t ignore_depth, size_t max_depth,
-        size_t* ignored_frames, size_t* returned_frames);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_BACKTRACE_HELPER_H
diff --git a/libcorkscrew/backtrace.c b/libcorkscrew/backtrace.c
deleted file mode 100644
index b03c43f..0000000
--- a/libcorkscrew/backtrace.c
+++ /dev/null
@@ -1,205 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "backtrace-arch.h"
-#include "backtrace-helper.h"
-#include "ptrace-arch.h"
-#include <corkscrew/map_info.h>
-#include <corkscrew/symbol_table.h>
-#include <corkscrew/ptrace.h>
-#include <corkscrew/demangle.h>
-
-#include <unistd.h>
-#include <signal.h>
-#include <pthread.h>
-#include <unwind.h>
-#include <sys/exec_elf.h>
-#include <cutils/log.h>
-
-#if HAVE_DLADDR
-#include <dlfcn.h>
-#endif
-
-typedef struct {
-    backtrace_frame_t* backtrace;
-    size_t ignore_depth;
-    size_t max_depth;
-    size_t ignored_frames;
-    size_t returned_frames;
-} backtrace_state_t;
-
-static _Unwind_Reason_Code unwind_backtrace_callback(struct _Unwind_Context* context, void* arg) {
-    backtrace_state_t* state = (backtrace_state_t*)arg;
-    uintptr_t pc = _Unwind_GetIP(context);
-    if (pc) {
-        // TODO: Get information about the stack layout from the _Unwind_Context.
-        //       This will require a new architecture-specific function to query
-        //       the appropriate registers.  Current callers of unwind_backtrace
-        //       don't need this information, so we won't bother collecting it just yet.
-        add_backtrace_entry(pc, state->backtrace,
-                state->ignore_depth, state->max_depth,
-                &state->ignored_frames, &state->returned_frames);
-    }
-    return state->returned_frames < state->max_depth ? _URC_NO_REASON : _URC_END_OF_STACK;
-}
-
-ssize_t unwind_backtrace(backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
-    backtrace_state_t state;
-    state.backtrace = backtrace;
-    state.ignore_depth = ignore_depth;
-    state.max_depth = max_depth;
-    state.ignored_frames = 0;
-    state.returned_frames = 0;
-
-    _Unwind_Reason_Code rc =_Unwind_Backtrace(unwind_backtrace_callback, &state);
-    if (state.returned_frames) {
-        return state.returned_frames;
-    }
-    return rc == _URC_END_OF_STACK ? 0 : -1;
-}
-
-#ifdef CORKSCREW_HAVE_ARCH
-static pthread_mutex_t g_unwind_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
-static volatile struct {
-    backtrace_frame_t* backtrace;
-    size_t ignore_depth;
-    size_t max_depth;
-    size_t returned_frames;
-    bool done;
-} g_unwind_signal_state;
-
-static void unwind_backtrace_thread_signal_handler(int n, siginfo_t* siginfo, void* sigcontext) {
-    backtrace_frame_t* backtrace = g_unwind_signal_state.backtrace;
-    if (backtrace) {
-        g_unwind_signal_state.backtrace = NULL;
-        g_unwind_signal_state.returned_frames = unwind_backtrace_signal_arch(
-                siginfo, sigcontext, backtrace,
-                g_unwind_signal_state.ignore_depth,
-                g_unwind_signal_state.max_depth);
-        g_unwind_signal_state.done = true;
-    }
-}
-#endif
-
-ssize_t unwind_backtrace_thread(pid_t tid, backtrace_frame_t* backtrace,
-        size_t ignore_depth, size_t max_depth) {
-#ifdef CORKSCREW_HAVE_ARCH
-    struct sigaction act;
-    struct sigaction oact;
-    memset(&act, 0, sizeof(act));
-    act.sa_sigaction = unwind_backtrace_thread_signal_handler;
-    act.sa_flags = SA_RESTART | SA_SIGINFO;
-    sigemptyset(&act.sa_mask);
-
-    pthread_mutex_lock(&g_unwind_signal_mutex);
-
-    g_unwind_signal_state.backtrace = backtrace;
-    g_unwind_signal_state.ignore_depth = ignore_depth;
-    g_unwind_signal_state.max_depth = max_depth;
-    g_unwind_signal_state.returned_frames = 0;
-    g_unwind_signal_state.done = false;
-
-    ssize_t frames = -1;
-    if (!sigaction(SIGURG, &act, &oact)) {
-        if (!kill(tid, SIGURG)) {
-            while (!g_unwind_signal_state.done) {
-                usleep(1000);
-            }
-            frames = g_unwind_signal_state.returned_frames;
-        }
-        sigaction(SIGURG, &oact, NULL);
-    }
-
-    g_unwind_signal_state.backtrace = NULL;
-
-    pthread_mutex_unlock(&g_unwind_signal_mutex);
-    return frames;
-#else
-    return -1;
-#endif
-}
-
-ssize_t unwind_backtrace_ptrace(pid_t tid, const ptrace_context_t* context,
-        backtrace_frame_t* backtrace, size_t ignore_depth, size_t max_depth) {
-#ifdef CORKSCREW_HAVE_ARCH
-    return unwind_backtrace_ptrace_arch(tid, context, backtrace, ignore_depth, max_depth);
-#else
-    return -1;
-#endif
-}
-
-static void init_backtrace_symbol(backtrace_symbol_t* symbol, uintptr_t pc) {
-    symbol->relative_pc = pc;
-    symbol->map_info = NULL;
-    symbol->name = NULL;
-    symbol->demangled_name = NULL;
-}
-
-void get_backtrace_symbols(const backtrace_frame_t* backtrace, size_t frames,
-        backtrace_symbol_t* backtrace_symbols) {
-    const map_info_t* milist = my_map_info_list();
-    for (size_t i = 0; i < frames; i++) {
-        const backtrace_frame_t* frame = &backtrace[i];
-        backtrace_symbol_t* symbol = &backtrace_symbols[i];
-        init_backtrace_symbol(symbol, frame->absolute_pc);
-
-        const map_info_t* mi = find_map_info(milist, frame->absolute_pc);
-        if (mi) {
-            symbol->relative_pc = frame->absolute_pc - mi->start;
-            symbol->map_info = mi;
-#if HAVE_DLADDR
-            Dl_info info;
-            if (dladdr((const void*)frame->absolute_pc, &info) && info.dli_sname) {
-                symbol->name = info.dli_sname;
-                symbol->demangled_name = demangle_symbol_name(symbol->name);
-            }
-#endif
-        }
-    }
-}
-
-void get_backtrace_symbols_ptrace(const ptrace_context_t* context,
-        const backtrace_frame_t* backtrace, size_t frames,
-        backtrace_symbol_t* backtrace_symbols) {
-    for (size_t i = 0; i < frames; i++) {
-        const backtrace_frame_t* frame = &backtrace[i];
-        backtrace_symbol_t* symbol = &backtrace_symbols[i];
-        init_backtrace_symbol(symbol, frame->absolute_pc);
-
-        const map_info_t* mi;
-        const symbol_t* s;
-        find_symbol_ptrace(context, frame->absolute_pc, &mi, &s);
-        if (mi) {
-            symbol->relative_pc = frame->absolute_pc - mi->start;
-            symbol->map_info = mi;
-        }
-        if (s) {
-            symbol->name = s->name;
-            symbol->demangled_name = demangle_symbol_name(symbol->name);
-        }
-    }
-}
-
-void free_backtrace_symbols(backtrace_symbol_t* backtrace_symbols, size_t frames) {
-    for (size_t i = 0; i < frames; i++) {
-        backtrace_symbol_t* symbol = &backtrace_symbols[i];
-        free(symbol->demangled_name);
-        init_backtrace_symbol(symbol, 0);
-    }
-}
diff --git a/libcorkscrew/demangle.c b/libcorkscrew/demangle.c
deleted file mode 100644
index ecb2c98..0000000
--- a/libcorkscrew/demangle.c
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include <corkscrew/demangle.h>
-
-#include <cutils/log.h>
-
-#ifdef CORKSCREW_HAVE_LIBIBERTY
-// Defined in libiberty.a
-extern char *cplus_demangle(const char *mangled, int options);
-#endif
-
-char* demangle_symbol_name(const char* name) {
-#ifdef CORKSCREW_HAVE_LIBIBERTY
-    return name ? cplus_demangle(name, 0) : NULL;
-#else
-    return NULL;
-#endif
-}
diff --git a/libcorkscrew/map_info.c b/libcorkscrew/map_info.c
deleted file mode 100644
index 60ed9b4..0000000
--- a/libcorkscrew/map_info.c
+++ /dev/null
@@ -1,112 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include <corkscrew/map_info.h>
-
-#include <ctype.h>
-#include <stdio.h>
-#include <string.h>
-#include <limits.h>
-#include <pthread.h>
-#include <unistd.h>
-#include <cutils/log.h>
-
-// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419   /system/lib/libcomposer.so\n
-// 012345678901234567890123456789012345678901234567890123456789
-// 0         1         2         3         4         5
-static map_info_t* parse_maps_line(const char* line)
-{
-    unsigned long int start;
-    unsigned long int end;
-    char permissions[5];
-    int name_pos;
-    if (sscanf(line, "%lx-%lx %4s %*x %*x:%*x %*d%n", &start, &end,
-            permissions, &name_pos) != 3) {
-        return NULL;
-    }
-
-    while (isspace(line[name_pos])) {
-        name_pos += 1;
-    }
-    const char* name = line + name_pos;
-    size_t name_len = strlen(name);
-    if (name_len && name[name_len - 1] == '\n') {
-        name_len -= 1;
-    }
-
-    map_info_t* mi = calloc(1, sizeof(map_info_t) + name_len + 1);
-    if (mi) {
-        mi->start = start;
-        mi->end = end;
-        mi->is_executable = strlen(permissions) == 4 && permissions[2] == 'x';
-        mi->data = NULL;
-        memcpy(mi->name, name, name_len);
-        mi->name[name_len] = '\0';
-    }
-    return mi;
-}
-
-map_info_t* load_map_info_list(pid_t tid) {
-    char path[PATH_MAX];
-    char line[1024];
-    FILE* fp;
-    map_info_t* milist = NULL;
-
-    snprintf(path, PATH_MAX, "/proc/%d/maps", tid);
-    fp = fopen(path, "r");
-    if (fp) {
-        while(fgets(line, sizeof(line), fp)) {
-            map_info_t* mi = parse_maps_line(line);
-            if (mi) {
-                mi->next = milist;
-                milist = mi;
-            }
-        }
-        fclose(fp);
-    }
-    return milist;
-}
-
-void free_map_info_list(map_info_t* milist) {
-    while (milist) {
-        map_info_t* next = milist->next;
-        free(milist);
-        milist = next;
-    }
-}
-
-const map_info_t* find_map_info(const map_info_t* milist, uintptr_t addr) {
-    const map_info_t* mi = milist;
-    while (mi && !(addr >= mi->start && addr < mi->end)) {
-        mi = mi->next;
-    }
-    return mi;
-}
-
-static pthread_once_t g_my_milist_once = PTHREAD_ONCE_INIT;
-static map_info_t* g_my_milist = NULL;
-
-static void init_my_milist_once() {
-    g_my_milist = load_map_info_list(getpid());
-}
-
-const map_info_t* my_map_info_list() {
-    pthread_once(&g_my_milist_once, init_my_milist_once);
-    return g_my_milist;
-}
diff --git a/libcorkscrew/ptrace-arch.h b/libcorkscrew/ptrace-arch.h
deleted file mode 100644
index c02df52..0000000
--- a/libcorkscrew/ptrace-arch.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Architecture dependent functions. */
-
-#ifndef _CORKSCREW_PTRACE_ARCH_H
-#define _CORKSCREW_PTRACE_ARCH_H
-
-#include <corkscrew/ptrace.h>
-#include <corkscrew/map_info.h>
-#include <corkscrew/symbol_table.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/* Custom extra data we stuff into map_info_t structures as part
- * of our ptrace_context_t. */
-typedef struct {
-#ifdef __arm__
-    uintptr_t exidx_start;
-    size_t exidx_size;
-#endif
-    symbol_table_t* symbol_table;
-} map_info_data_t;
-
-void load_ptrace_map_info_data_arch(pid_t pid, map_info_t* mi, map_info_data_t* data);
-void free_ptrace_map_info_data_arch(map_info_t* mi, map_info_data_t* data);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // _CORKSCREW_PTRACE_ARCH_H
diff --git a/libcorkscrew/ptrace.c b/libcorkscrew/ptrace.c
deleted file mode 100644
index 6ed77c4..0000000
--- a/libcorkscrew/ptrace.c
+++ /dev/null
@@ -1,134 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include "ptrace-arch.h"
-#include <corkscrew/ptrace.h>
-
-#include <errno.h>
-#include <sys/ptrace.h>
-#include <cutils/log.h>
-
-static const uint32_t ELF_MAGIC = 0x464C457f; // "ELF\0177"
-
-#ifndef PAGE_SIZE
-#define PAGE_SIZE 4096
-#endif
-
-#ifndef PAGE_MASK
-#define PAGE_MASK (~(PAGE_SIZE - 1))
-#endif
-
-bool try_get_word(pid_t tid, uintptr_t ptr, uint32_t* out_value) {
-    if (ptr & 3) {
-        LOGV("try_get_word: invalid pointer 0x%08x", ptr);
-        *out_value = 0;
-        return false;
-    }
-    if (tid < 0) {
-#if 0 /*unreliable, unclear whether this is safe from a signal handler context*/
-        // Determine whether the pointer is likely to be valid before dereferencing it.
-        unsigned char vec[1];
-        while (mincore((void*)(ptr & PAGE_MASK), sizeof(uint32_t), vec)) {
-            if (errno != EAGAIN && errno != EINTR) {
-                LOGV("try_get_word: invalid pointer 0x%08x, mincore() errno=%d", ptr, errno);
-                *out_value = 0;
-                return false;
-            }
-        }
-#endif
-        *out_value = *(uint32_t*)ptr;
-        return true;
-    } else {
-        // ptrace() returns -1 and sets errno when the operation fails.
-        // To disambiguate -1 from a valid result, we clear errno beforehand.
-        errno = 0;
-        *out_value = ptrace(PTRACE_PEEKTEXT, tid, (void*)ptr, NULL);
-        if (*out_value == 0xffffffffL && errno) {
-            LOGV("try_get_word: invalid pointer 0x%08x, ptrace() errno=%d", ptr, errno);
-            *out_value = 0;
-            return false;
-        }
-        return true;
-    }
-}
-
-static void load_ptrace_map_info_data(pid_t pid, map_info_t* mi) {
-    if (mi->is_executable) {
-        uint32_t elf_magic;
-        if (try_get_word(pid, mi->start, &elf_magic) && elf_magic == ELF_MAGIC) {
-            map_info_data_t* data = (map_info_data_t*)calloc(1, sizeof(map_info_data_t));
-            if (data) {
-                mi->data = data;
-                if (mi->name[0]) {
-                    data->symbol_table = load_symbol_table(mi->name);
-                }
-#ifdef CORKSCREW_HAVE_ARCH
-                load_ptrace_map_info_data_arch(pid, mi, data);
-#endif
-            }
-        }
-    }
-}
-
-ptrace_context_t* load_ptrace_context(pid_t pid) {
-    ptrace_context_t* context =
-            (ptrace_context_t*)calloc(1, sizeof(ptrace_context_t));
-    if (context) {
-        context->map_info_list = load_map_info_list(pid);
-        for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) {
-            load_ptrace_map_info_data(pid, mi);
-        }
-    }
-    return context;
-}
-
-static void free_ptrace_map_info_data(map_info_t* mi) {
-    map_info_data_t* data = (map_info_data_t*)mi->data;
-    if (data) {
-        if (data->symbol_table) {
-            free_symbol_table(data->symbol_table);
-        }
-#ifdef CORKSCREW_HAVE_ARCH
-        free_ptrace_map_info_data_arch(mi, data);
-#endif
-        free(data);
-        mi->data = NULL;
-    }
-}
-
-void free_ptrace_context(ptrace_context_t* context) {
-    for (map_info_t* mi = context->map_info_list; mi; mi = mi->next) {
-        free_ptrace_map_info_data(mi);
-    }
-    free_map_info_list(context->map_info_list);
-}
-
-void find_symbol_ptrace(const ptrace_context_t* context,
-        uintptr_t addr, const map_info_t** out_map_info, const symbol_t** out_symbol) {
-    const map_info_t* mi = find_map_info(context->map_info_list, addr);
-    const symbol_t* symbol = NULL;
-    if (mi) {
-        const map_info_data_t* data = (const map_info_data_t*)mi->data;
-        if (data && data->symbol_table) {
-            symbol = find_symbol(data->symbol_table, addr - mi->start);
-        }
-    }
-    *out_map_info = mi;
-    *out_symbol = symbol;
-}
diff --git a/libcorkscrew/symbol_table.c b/libcorkscrew/symbol_table.c
deleted file mode 100644
index 8257c77..0000000
--- a/libcorkscrew/symbol_table.c
+++ /dev/null
@@ -1,204 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define LOG_TAG "Corkscrew"
-//#define LOG_NDEBUG 0
-
-#include <corkscrew/symbol_table.h>
-
-#include <stdlib.h>
-#include <fcntl.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/exec_elf.h>
-#include <cutils/log.h>
-
-// Compare function for qsort
-static int qcompar(const void *a, const void *b) {
-    const symbol_t* asym = (const symbol_t*)a;
-    const symbol_t* bsym = (const symbol_t*)b;
-    if (asym->start > bsym->start) return 1;
-    if (asym->start < bsym->start) return -1;
-    return 0;
-}
-
-// Compare function for bsearch
-static int bcompar(const void *key, const void *element) {
-    uintptr_t addr = *(const uintptr_t*)key;
-    const symbol_t* symbol = (const symbol_t*)element;
-    if (addr < symbol->start) return -1;
-    if (addr >= symbol->end) return 1;
-    return 0;
-}
-
-symbol_table_t* load_symbol_table(const char *filename) {
-    symbol_table_t* table = NULL;
-
-    int fd = open(filename, O_RDONLY);
-    if (fd < 0) {
-        goto out;
-    }
-
-    struct stat sb;
-    if (fstat(fd, &sb)) {
-        goto out_close;
-    }
-
-    size_t length = sb.st_size;
-    char* base = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
-    if (base == MAP_FAILED) {
-        goto out_close;
-    }
-
-    // Parse the file header
-    Elf32_Ehdr *hdr = (Elf32_Ehdr*)base;
-    if (!IS_ELF(*hdr)) {
-        goto out_close;
-    }
-    Elf32_Shdr *shdr = (Elf32_Shdr*)(base + hdr->e_shoff);
-
-    // Search for the dynamic symbols section
-    int sym_idx = -1;
-    int dynsym_idx = -1;
-    for (Elf32_Half i = 0; i < hdr->e_shnum; i++) {
-        if (shdr[i].sh_type == SHT_SYMTAB) {
-            sym_idx = i;
-        }
-        if (shdr[i].sh_type == SHT_DYNSYM) {
-            dynsym_idx = i;
-        }
-    }
-    if (dynsym_idx == -1 && sym_idx == -1) {
-        goto out_unmap;
-    }
-
-    table = malloc(sizeof(symbol_table_t));
-    if(!table) {
-        goto out_unmap;
-    }
-    table->num_symbols = 0;
-
-    Elf32_Sym *dynsyms = NULL;
-    int dynnumsyms = 0;
-    char *dynstr = NULL;
-    if (dynsym_idx != -1) {
-        dynsyms = (Elf32_Sym*)(base + shdr[dynsym_idx].sh_offset);
-        dynnumsyms = shdr[dynsym_idx].sh_size / shdr[dynsym_idx].sh_entsize;
-        int dynstr_idx = shdr[dynsym_idx].sh_link;
-        dynstr = base + shdr[dynstr_idx].sh_offset;
-    }
-
-    Elf32_Sym *syms = NULL;
-    int numsyms = 0;
-    char *str = NULL;
-    if (sym_idx != -1) {
-        syms = (Elf32_Sym*)(base + shdr[sym_idx].sh_offset);
-        numsyms = shdr[sym_idx].sh_size / shdr[sym_idx].sh_entsize;
-        int str_idx = shdr[sym_idx].sh_link;
-        str = base + shdr[str_idx].sh_offset;
-    }
-
-    int dynsymbol_count = 0;
-    if (dynsym_idx != -1) {
-        // Iterate through the dynamic symbol table, and count how many symbols
-        // are actually defined
-        for (int i = 0; i < dynnumsyms; i++) {
-            if (dynsyms[i].st_shndx != SHN_UNDEF) {
-                dynsymbol_count++;
-            }
-        }
-    }
-
-    size_t symbol_count = 0;
-    if (sym_idx != -1) {
-        // Iterate through the symbol table, and count how many symbols
-        // are actually defined
-        for (int i = 0; i < numsyms; i++) {
-            if (syms[i].st_shndx != SHN_UNDEF
-                    && str[syms[i].st_name]
-                    && syms[i].st_value
-                    && syms[i].st_size) {
-                symbol_count++;
-            }
-        }
-    }
-
-    // Now, create an entry in our symbol table structure for each symbol...
-    table->num_symbols += symbol_count + dynsymbol_count;
-    table->symbols = malloc(table->num_symbols * sizeof(symbol_t));
-    if (!table->symbols) {
-        free(table);
-        table = NULL;
-        goto out_unmap;
-    }
-
-    size_t symbol_index = 0;
-    if (dynsym_idx != -1) {
-        // ...and populate them
-        for (int i = 0; i < dynnumsyms; i++) {
-            if (dynsyms[i].st_shndx != SHN_UNDEF) {
-                table->symbols[symbol_index].name = strdup(dynstr + dynsyms[i].st_name);
-                table->symbols[symbol_index].start = dynsyms[i].st_value;
-                table->symbols[symbol_index].end = dynsyms[i].st_value + dynsyms[i].st_size;
-                symbol_index += 1;
-            }
-        }
-    }
-
-    if (sym_idx != -1) {
-        // ...and populate them
-        for (int i = 0; i < numsyms; i++) {
-            if (syms[i].st_shndx != SHN_UNDEF
-                    && str[syms[i].st_name]
-                    && syms[i].st_value
-                    && syms[i].st_size) {
-                table->symbols[symbol_index].name = strdup(str + syms[i].st_name);
-                table->symbols[symbol_index].start = syms[i].st_value;
-                table->symbols[symbol_index].end = syms[i].st_value + syms[i].st_size;
-                symbol_index += 1;
-            }
-        }
-    }
-
-    // Sort the symbol table entries, so they can be bsearched later
-    qsort(table->symbols, table->num_symbols, sizeof(symbol_t), qcompar);
-
-out_unmap:
-    munmap(base, length);
-
-out_close:
-    close(fd);
-
-out:
-    return table;
-}
-
-void free_symbol_table(symbol_table_t* table) {
-    if (table) {
-        for (size_t i = 0; i < table->num_symbols; i++) {
-            free(table->symbols[i].name);
-        }
-        free(table->symbols);
-        free(table);
-    }
-}
-
-const symbol_t* find_symbol(const symbol_table_t* table, uintptr_t addr) {
-    if (!table) return NULL;
-    return (const symbol_t*)bsearch(&addr, table->symbols, table->num_symbols,
-            sizeof(symbol_t), bcompar);
-}