8145184: [aix] Implement os::platform_print_native_stack on AIX
authorstuefe
Wed, 13 Jan 2016 17:55:57 +0100
changeset 35515 179755aaa4e0
parent 35514 616a5f8dbb98
child 35517 7d5ff5ee627c
8145184: [aix] Implement os::platform_print_native_stack on AIX Reviewed-by: goetz
hotspot/src/os/aix/vm/decoder_aix.hpp
hotspot/src/os/aix/vm/misc_aix.hpp
hotspot/src/os/aix/vm/os_aix.cpp
hotspot/src/os/aix/vm/porting_aix.cpp
hotspot/src/os/aix/vm/porting_aix.hpp
hotspot/src/os_cpu/aix_ppc/vm/os_aix_ppc.cpp
hotspot/src/os_cpu/aix_ppc/vm/os_aix_ppc.hpp
--- a/hotspot/src/os/aix/vm/decoder_aix.hpp	Sat Jan 16 00:28:28 2016 +0000
+++ b/hotspot/src/os/aix/vm/decoder_aix.hpp	Wed Jan 13 17:55:57 2016 +0100
@@ -36,13 +36,15 @@
 
   virtual bool can_decode_C_frame_in_vm() const { return true; }
 
-  virtual bool demangle(const char* symbol, char* buf, int buflen) { return false; } // demangled by getFuncName
+  virtual bool demangle(const char* symbol, char* buf, int buflen) { return false; } // use AixSymbols::get_function_name to demangle
 
   virtual bool decode(address addr, char* buf, int buflen, int* offset, const char* modulepath, bool demangle) {
-    return (::getFuncName((codeptr_t)addr, buf, buflen, offset, 0, 0, 0, demangle) == 0);
+    return AixSymbols::get_function_name(addr, buf, buflen, offset, 0, demangle);
   }
   virtual bool decode(address addr, char *buf, int buflen, int* offset, const void *base) {
     ShouldNotReachHere();
     return false;
   }
+
 };
+
--- a/hotspot/src/os/aix/vm/misc_aix.hpp	Sat Jan 16 00:28:28 2016 +0000
+++ b/hotspot/src/os/aix/vm/misc_aix.hpp	Wed Jan 13 17:55:57 2016 +0100
@@ -41,7 +41,6 @@
     fputc('\n', stderr); fflush(stderr); \
   } \
 }
-#define ERRBYE(s) { trcVerbose(s); return -1; }
 
 #define assert0(b) assert((b), "")
 #define guarantee0(b) guarantee((b), "")
--- a/hotspot/src/os/aix/vm/os_aix.cpp	Sat Jan 16 00:28:28 2016 +0000
+++ b/hotspot/src/os/aix/vm/os_aix.cpp	Wed Jan 13 17:55:57 2016 +0100
@@ -130,61 +130,10 @@
 #define ERROR_MP_VMGETINFO_FAILED                    102
 #define ERROR_MP_VMGETINFO_CLAIMS_NO_SUPPORT_FOR_64K 103
 
-// The semantics in this file are thus that codeptr_t is a *real code ptr*.
-// This means that any function taking codeptr_t as arguments will assume
-// a real codeptr and won't handle function descriptors (eg getFuncName),
-// whereas functions taking address as args will deal with function
-// descriptors (eg os::dll_address_to_library_name).
-typedef unsigned int* codeptr_t;
-
-// Typedefs for stackslots, stack pointers, pointers to op codes.
-typedef unsigned long stackslot_t;
-typedef stackslot_t* stackptr_t;
-
 // Query dimensions of the stack of the calling thread.
 static bool query_stack_dimensions(address* p_stack_base, size_t* p_stack_size);
 static address resolve_function_descriptor_to_code_pointer(address p);
 
-// Function to check a given stack pointer against given stack limits.
-inline bool is_valid_stackpointer(stackptr_t sp, stackptr_t stack_base, size_t stack_size) {
-  if (((uintptr_t)sp) & 0x7) {
-    return false;
-  }
-  if (sp > stack_base) {
-    return false;
-  }
-  if (sp < (stackptr_t) ((address)stack_base - stack_size)) {
-    return false;
-  }
-  return true;
-}
-
-// Returns true if function is a valid codepointer.
-inline bool is_valid_codepointer(codeptr_t p) {
-  if (!p) {
-    return false;
-  }
-  if (((uintptr_t)p) & 0x3) {
-    return false;
-  }
-  if (LoadedLibraries::find_for_text_address(p, NULL) == NULL) {
-    return false;
-  }
-  return true;
-}
-
-// Macro to check a given stack pointer against given stack limits and to die if test fails.
-#define CHECK_STACK_PTR(sp, stack_base, stack_size) { \
-    guarantee(is_valid_stackpointer((stackptr_t)(sp), (stackptr_t)(stack_base), stack_size), "Stack Pointer Invalid"); \
-}
-
-// Macro to check the current stack pointer against given stacklimits.
-#define CHECK_CURRENT_STACK_PTR(stack_base, stack_size) { \
-  address sp; \
-  sp = os::current_stack_pointer(); \
-  CHECK_STACK_PTR(sp, stack_base, stack_size); \
-}
-
 static void vmembk_print_on(outputStream* os);
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -859,9 +808,6 @@
     trcVerbose("Thread " UINT64_FORMAT ": stack not in data segment.", (uint64_t) pthread_id);
   }
 
-  // Do some sanity checks.
-  CHECK_CURRENT_STACK_PTR(thread->stack_base(), thread->stack_size());
-
   // Try to randomize the cache line index of hot stack frames.
   // This helps when threads of the same stack traces evict each other's
   // cache lines. The threads can be either from the same JVM instance, or
@@ -1028,9 +974,6 @@
   // initialize floating point control register
   os::Aix::init_thread_fpu_state();
 
-  // some sanity checks
-  CHECK_CURRENT_STACK_PTR(thread->stack_base(), thread->stack_size());
-
   // Initial thread state is RUNNABLE
   osthread->set_state(RUNNABLE);
 
@@ -1382,32 +1325,7 @@
     return false;
   }
 
-  // Go through Decoder::decode to call getFuncName which reads the name from the traceback table.
-  return Decoder::decode(addr, buf, buflen, offset, demangle);
-}
-
-static int getModuleName(codeptr_t pc,                    // [in] program counter
-                         char* p_name, size_t namelen,    // [out] optional: function name
-                         char* p_errmsg, size_t errmsglen // [out] optional: user provided buffer for error messages
-                         ) {
-
-  if (p_name && namelen > 0) {
-    *p_name = '\0';
-  }
-  if (p_errmsg && errmsglen > 0) {
-    *p_errmsg = '\0';
-  }
-
-  if (p_name && namelen > 0) {
-    loaded_module_t lm;
-    if (LoadedLibraries::find_for_text_address(pc, &lm) != NULL) {
-      strncpy(p_name, lm.shortname, namelen);
-      p_name[namelen - 1] = '\0';
-    }
-    return 0;
-  }
-
-  return -1;
+  return AixSymbols::get_function_name(addr, buf, buflen, offset, NULL, demangle);
 }
 
 bool os::dll_address_to_library_name(address addr, char* buf,
@@ -1425,10 +1343,7 @@
     return false;
   }
 
-  if (::getModuleName((codeptr_t) addr, buf, buflen, 0, 0) == 0) {
-    return true;
-  }
-  return false;
+  return AixSymbols::get_module_name(addr, buf, buflen);
 }
 
 // Loads .dll/.so and in case of error it checks if .dll/.so was built
--- a/hotspot/src/os/aix/vm/porting_aix.cpp	Sat Jan 16 00:28:28 2016 +0000
+++ b/hotspot/src/os/aix/vm/porting_aix.cpp	Wed Jan 13 17:55:57 2016 +0100
@@ -23,30 +23,35 @@
  */
 
 #include "asm/assembler.hpp"
+#include "compiler/disassembler.hpp"
 #include "loadlib_aix.hpp"
 #include "memory/allocation.hpp"
 #include "memory/allocation.inline.hpp"
-// For CritSect
 #include "misc_aix.hpp"
 #include "porting_aix.hpp"
 #include "runtime/os.hpp"
+#include "runtime/thread.hpp"
 #include "utilities/debug.hpp"
 
 #include <demangle.h>
 #include <sys/debug.h>
+#include <ucontext.h>
 
 //////////////////////////////////
 // Provide implementation for dladdr based on LoadedLibraries pool and
-// traceback table scan (see getFuncName).
+// traceback table scan
 
 // Search traceback table in stack,
 // return procedure name from trace back table.
 #define MAX_FUNC_SEARCH_LEN 0x10000
-// Any PC below this value is considered toast.
-#define MINIMUM_VALUE_FOR_PC ((unsigned int*)0x1024)
 
 #define PTRDIFF_BYTES(p1,p2) (((ptrdiff_t)p1) - ((ptrdiff_t)p2))
 
+// Typedefs for stackslots, stack pointers, pointers to op codes.
+typedef unsigned long stackslot_t;
+typedef stackslot_t* stackptr_t;
+typedef unsigned int* codeptr_t;
+
 // Unfortunately, the interface of dladdr makes the implementator
 // responsible for maintaining memory for function name/library
 // name. I guess this is because most OS's keep those values as part
@@ -91,15 +96,12 @@
 
 static fixed_strings dladdr_fixed_strings;
 
-// Given a code pointer, returns the function name and the displacement.
-// Function looks for the traceback table at the end of the function.
-extern "C" int getFuncName(
-    codeptr_t pc,                    // [in] program counter
+bool AixSymbols::get_function_name (
+    address pc0,                     // [in] program counter
     char* p_name, size_t namelen,    // [out] optional: function name ("" if not available)
     int* p_displacement,             // [out] optional: displacement (-1 if not available)
     const struct tbtable** p_tb,     // [out] optional: ptr to traceback table to get further
                                      //                 information (NULL if not available)
-    char* p_errmsg, size_t errmsglen,// [out] optional: user provided buffer for error messages
     bool demangle                    // [in] whether to demangle the name
   ) {
   struct tbtable* tb = 0;
@@ -109,9 +111,6 @@
   if (p_name && namelen > 0) {
     *p_name = '\0';
   }
-  if (p_errmsg && errmsglen > 0) {
-    *p_errmsg = '\0';
-  }
   if (p_displacement) {
     *p_displacement = -1;
   }
@@ -119,9 +118,12 @@
     *p_tb = NULL;
   }
 
+  codeptr_t pc = (codeptr_t)pc0;
+
   // weed out obvious bogus states
-  if (pc < MINIMUM_VALUE_FOR_PC) {
-    ERRBYE("invalid program counter");
+  if (pc < (codeptr_t)0x1000) {
+    trcVerbose("invalid program counter");
+    return false;
   }
 
   // We see random but frequent crashes in this function since some months mainly on shutdown
@@ -130,7 +132,8 @@
   // As the pc cannot be trusted to be anything sensible lets make all reads via SafeFetch. Also
   // bail if this is not a text address right now.
   if (!LoadedLibraries::find_for_text_address(pc, NULL)) {
-    ERRBYE("not a text address");
+    trcVerbose("not a text address");
+    return false;
   }
 
   // .. (Note that is_readable_pointer returns true if safefetch stubs are not there yet;
@@ -138,10 +141,11 @@
   // error files than not having a callstack.)
 #define CHECK_POINTER_READABLE(p) \
   if (!MiscUtils::is_readable_pointer(p)) { \
-    ERRBYE("pc not readable"); \
+    trcVerbose("pc not readable"); \
+    return false; \
   }
 
-  codeptr_t pc2 = pc;
+  codeptr_t pc2 = (codeptr_t) pc;
 
   // Make sure the pointer is word aligned.
   pc2 = (codeptr_t) align_ptr_up((char*)pc2, 4);
@@ -154,7 +158,8 @@
     pc2++;
   }
   if (*pc2 != 0) {
-    ERRBYE("no traceback table found");
+    trcVerbose("no traceback table found");
+    return false;
   }
   //
   // Set up addressability to the traceback table
@@ -166,7 +171,8 @@
   if (tb->tb.lang >= 0xf && tb->tb.lang <= 0xfb) {
     // Language specifiers, go from 0 (C) to 14 (Objective C).
     // According to spec, 0xf-0xfa reserved, 0xfb-0xff reserved for ibm.
-    ERRBYE("no traceback table found");
+    trcVerbose("no traceback table found");
+    return false;
   }
 
   // Existence of fields in the tbtable extension are contingent upon
@@ -188,7 +194,8 @@
 
     // Weed out the cases where we did find the wrong traceback table.
     if (pc < start_of_procedure) {
-      ERRBYE("no traceback table found");
+      trcVerbose("no traceback table found");
+      return false;
     }
 
     // return the displacement
@@ -218,23 +225,19 @@
   if (p_name && namelen > 0) {
     if (tb->tb.name_present) {
       // Copy name from text because it may not be zero terminated.
-      // 256 is good enough for most cases; do not use large buffers here.
-      char buf[256];
-      const short l = MIN2<short>(*((short*)pc2), sizeof(buf) - 1);
+      const short l = MIN2<short>(*((short*)pc2), namelen - 1);
       // Be very careful.
       int i = 0; char* const p = (char*)pc2 + sizeof(short);
       while (i < l && MiscUtils::is_readable_pointer(p + i)) {
-        buf[i] = p[i];
+        p_name[i] = p[i];
         i++;
       }
-      buf[i] = '\0';
-
-      p_name[0] = '\0';
+      p_name[i] = '\0';
 
       // If it is a C++ name, try and demangle it using the Demangle interface (see demangle.h).
       if (demangle) {
         char* rest;
-        Name* const name = Demangle(buf, rest);
+        Name* const name = Demangle(p_name, rest);
         if (name) {
           const char* const demangled_name = name->Text();
           if (demangled_name) {
@@ -244,24 +247,35 @@
           delete name;
         }
       }
-
-      // Fallback: if demangling did not work, just provide the unmangled name.
-      if (p_name[0] == '\0') {
-        strncpy(p_name, buf, namelen-1);
-        p_name[namelen-1] = '\0';
-      }
-
     } else {
       strncpy(p_name, "<nameless function>", namelen-1);
       p_name[namelen-1] = '\0';
     }
   }
+
   // Return traceback table, if user wants it.
   if (p_tb) {
     (*p_tb) = tb;
   }
 
-  return 0;
+  return true;
+
+}
+
+bool AixSymbols::get_module_name(address pc,
+                         char* p_name, size_t namelen) {
+
+  if (p_name && namelen > 0) {
+    p_name[0] = '\0';
+    loaded_module_t lm;
+    if (LoadedLibraries::find_for_text_address(pc, &lm) != NULL) {
+      strncpy(p_name, lm.shortname, namelen);
+      p_name[namelen - 1] = '\0';
+      return true;
+    }
+  }
+
+  return false;
 }
 
 // Special implementation of dladdr for Aix based on LoadedLibraries
@@ -341,8 +355,8 @@
       char funcname[256] = "";
       int displacement = 0;
 
-      if (getFuncName((codeptr_t) p, funcname, sizeof(funcname), &displacement,
-                      NULL, NULL, 0, false) == 0) {
+      if (AixSymbols::get_function_name(p, funcname, sizeof(funcname),
+                      &displacement, NULL, true)) {
         if (funcname[0] != '\0') {
           const char* const interned = dladdr_fixed_strings.intern(funcname);
           info->dli_sname = interned;
@@ -385,3 +399,414 @@
   return rc; // error: return 0 [sic]
 
 }
+
+/////////////////////////////////////////////////////////////////////////////
+// Native callstack dumping
+
+// Print the traceback table for one stack frame.
+static void print_tbtable (outputStream* st, const struct tbtable* p_tb) {
+
+  if (p_tb == NULL) {
+    st->print("<null>");
+    return;
+  }
+
+  switch(p_tb->tb.lang) {
+    case TB_C: st->print("C"); break;
+    case TB_FORTRAN: st->print("FORTRAN"); break;
+    case TB_PASCAL: st->print("PASCAL"); break;
+    case TB_ADA: st->print("ADA"); break;
+    case TB_PL1: st->print("PL1"); break;
+    case TB_BASIC: st->print("BASIC"); break;
+    case TB_LISP: st->print("LISP"); break;
+    case TB_COBOL: st->print("COBOL"); break;
+    case TB_MODULA2: st->print("MODULA2"); break;
+    case TB_CPLUSPLUS: st->print("C++"); break;
+    case TB_RPG: st->print("RPG"); break;
+    case TB_PL8: st->print("PL8"); break;
+    case TB_ASM: st->print("ASM"); break;
+    case TB_HPJ: st->print("HPJ"); break;
+    default: st->print("unknown");
+  }
+  st->print(" ");
+
+  if (p_tb->tb.globallink) {
+    st->print("globallink ");
+  }
+  if (p_tb->tb.is_eprol) {
+    st->print("eprol ");
+  }
+  if (p_tb->tb.int_proc) {
+    st->print("int_proc ");
+  }
+  if (p_tb->tb.tocless) {
+    st->print("tocless ");
+  }
+  if (p_tb->tb.fp_present) {
+    st->print("fp_present ");
+  }
+  if (p_tb->tb.int_hndl) {
+    st->print("interrupt_handler ");
+  }
+  if (p_tb->tb.uses_alloca) {
+    st->print("uses_alloca ");
+  }
+  if (p_tb->tb.saves_cr) {
+    st->print("saves_cr ");
+  }
+  if (p_tb->tb.saves_lr) {
+    st->print("saves_lr ");
+  }
+  if (p_tb->tb.stores_bc) {
+    st->print("stores_bc ");
+  }
+  if (p_tb->tb.fixup) {
+    st->print("fixup ");
+  }
+  if (p_tb->tb.fpr_saved > 0) {
+    st->print("fpr_saved:%d ", p_tb->tb.fpr_saved);
+  }
+  if (p_tb->tb.gpr_saved > 0) {
+    st->print("gpr_saved:%d ", p_tb->tb.gpr_saved);
+  }
+  if (p_tb->tb.fixedparms > 0) {
+    st->print("fixedparms:%d ", p_tb->tb.fixedparms);
+  }
+  if (p_tb->tb.floatparms > 0) {
+    st->print("floatparms:%d ", p_tb->tb.floatparms);
+  }
+  if (p_tb->tb.parmsonstk > 0) {
+    st->print("parmsonstk:%d", p_tb->tb.parmsonstk);
+  }
+}
+
+// Print information for pc (module, function, displacement, traceback table)
+// on one line.
+static void print_info_for_pc (outputStream* st, codeptr_t pc, char* buf,
+                               size_t buf_size, bool demangle) {
+  const struct tbtable* tb = NULL;
+  int displacement = -1;
+
+  if (!MiscUtils::is_readable_pointer(pc)) {
+    st->print("(invalid)");
+    return;
+  }
+
+  if (AixSymbols::get_module_name((address)pc, buf, buf_size)) {
+    st->print("%s", buf);
+  } else {
+    st->print("(unknown module)");
+  }
+  st->print("::");
+  if (AixSymbols::get_function_name((address)pc, buf, buf_size,
+                                     &displacement, &tb, demangle)) {
+    st->print("%s", buf);
+  } else {
+    st->print("(unknown function)");
+  }
+  if (displacement == -1) {
+    st->print("+?");
+  } else {
+    st->print("+0x%x", displacement);
+  }
+  if (tb) {
+    st->fill_to(64);
+    st->print("  (");
+    print_tbtable(st, tb);
+    st->print(")");
+  }
+}
+
+static void print_stackframe(outputStream* st, stackptr_t sp, char* buf,
+                             size_t buf_size, bool demangle) {
+
+  stackptr_t sp2 = sp;
+
+  // skip backchain
+
+  sp2++;
+
+  // skip crsave
+
+  sp2++;
+
+  // retrieve lrsave. That is the only info I need to get the function/displacement
+
+  codeptr_t lrsave = (codeptr_t) *(sp2);
+  st->print (PTR64_FORMAT " - " PTR64_FORMAT " ", sp2, lrsave);
+
+  if (lrsave != NULL) {
+    print_info_for_pc(st, lrsave, buf, buf_size, demangle);
+  }
+
+}
+
+// Function to check a given stack pointer against given stack limits.
+static bool is_valid_stackpointer(stackptr_t sp, stackptr_t stack_base, size_t stack_size) {
+  if (((uintptr_t)sp) & 0x7) {
+    return false;
+  }
+  if (sp > stack_base) {
+    return false;
+  }
+  if (sp < (stackptr_t) ((address)stack_base - stack_size)) {
+    return false;
+  }
+  return true;
+}
+
+// Returns true if function is a valid codepointer.
+static bool is_valid_codepointer(codeptr_t p) {
+  if (!p) {
+    return false;
+  }
+  if (((uintptr_t)p) & 0x3) {
+    return false;
+  }
+  if (LoadedLibraries::find_for_text_address(p, NULL) == NULL) {
+    return false;
+  }
+  return true;
+}
+
+// Function tries to guess if the given combination of stack pointer, stack base
+// and stack size is a valid stack frame.
+static bool is_valid_frame (stackptr_t p, stackptr_t stack_base, size_t stack_size) {
+
+  if (!is_valid_stackpointer(p, stack_base, stack_size)) {
+    return false;
+  }
+
+  // First check - the occurrence of a valid backchain pointer up the stack, followed by a
+  // valid codeptr, counts as a good candidate.
+  stackptr_t sp2 = (stackptr_t) *p;
+  if (is_valid_stackpointer(sp2, stack_base, stack_size) && // found a valid stack pointer in the stack...
+     ((sp2 - p) > 6) &&  // ... pointing upwards and not into my frame...
+     is_valid_codepointer((codeptr_t)(*(sp2 + 2)))) // ... followed by a code pointer after two slots...
+  {
+    return true;
+  }
+
+  return false;
+}
+
+// Try to relocate a stack back chain in a given stack.
+// Used in callstack dumping, when the backchain is broken by an overwriter
+static stackptr_t try_find_backchain (stackptr_t last_known_good_frame,
+                                      stackptr_t stack_base, size_t stack_size)
+{
+  if (!is_valid_stackpointer(last_known_good_frame, stack_base, stack_size)) {
+    return NULL;
+  }
+
+  stackptr_t sp = last_known_good_frame;
+
+  sp += 6; // Omit next fixed frame slots.
+  while (sp < stack_base) {
+    if (is_valid_frame(sp, stack_base, stack_size)) {
+      return sp;
+    }
+    sp ++;
+  }
+
+  return NULL;
+}
+
+static void decode_instructions_at_pc(const char* header,
+                                      codeptr_t pc, int num_before,
+                                      int num_after, outputStream* st) {
+  // TODO: PPC port Disassembler::decode(pc, 16, 16, st);
+}
+
+
+void AixNativeCallstack::print_callstack_for_context(outputStream* st, const ucontext_t* context,
+                                                     bool demangle, char* buf, size_t buf_size) {
+
+#define MAX_CALLSTACK_DEPTH 50
+
+  unsigned long* sp;
+  unsigned long* sp_last;
+  int frame;
+
+  // To print the first frame, use the current value of iar:
+  // current entry indicated by iar (the current pc)
+  codeptr_t cur_iar = 0;
+  stackptr_t cur_sp = 0;
+  codeptr_t cur_rtoc = 0;
+  codeptr_t cur_lr = 0;
+
+  const ucontext_t* uc = (const ucontext_t*) context;
+
+  // fallback: use the current context
+  ucontext_t local_context;
+  if (!uc) {
+    if (getcontext(&local_context) == 0) {
+      uc = &local_context;
+    } else {
+      st->print_cr("No context given and getcontext failed. ");
+      return;
+    }
+  }
+
+  cur_iar = (codeptr_t)uc->uc_mcontext.jmp_context.iar;
+  cur_sp = (stackptr_t)uc->uc_mcontext.jmp_context.gpr[1];
+  cur_rtoc = (codeptr_t)uc->uc_mcontext.jmp_context.gpr[2];
+  cur_lr = (codeptr_t)uc->uc_mcontext.jmp_context.lr;
+
+  // syntax used here:
+  //  n   --------------   <-- stack_base,   stack_to
+  //  n-1 |            |
+  //  ... | older      |
+  //  ... |   frames   | |
+  //      |            | | stack grows downward
+  //  ... | younger    | |
+  //  ... |   frames   | V
+  //      |            |
+  //      |------------|   <-- cur_sp, current stack ptr
+  //      |            |
+  //      |  unsused   |
+  //      |    stack   |
+  //      |            |
+  //      .            .
+  //      .            .
+  //      .            .
+  //      .            .
+  //      |            |
+  //   0  --------------   <-- stack_from
+  //
+
+  // Retrieve current stack base, size from the current thread. If there is none,
+  // retrieve it from the OS.
+  stackptr_t stack_base = NULL;
+  size_t stack_size = NULL;
+  Thread* const thread = Thread::current_or_null_safe();
+  if (thread) {
+    stack_base = (stackptr_t) thread->stack_base();
+    stack_size = thread->stack_size();
+  } else {
+    stack_base = (stackptr_t) os::current_stack_base();
+    stack_size = os::current_stack_size();
+  }
+
+  st->print_cr("------ current frame:");
+  st->print("iar:  " PTR64_FORMAT " ", p2i(cur_iar));
+  print_info_for_pc(st, cur_iar, buf, buf_size, demangle);
+  st->cr();
+
+  if (cur_iar && MiscUtils::is_readable_pointer(cur_iar)) {
+    decode_instructions_at_pc(
+      "Decoded instructions at iar:",
+      cur_iar, 32, 16, st);
+  }
+
+  // Print out lr too, which may be interesting if we did jump to some bogus location;
+  // in those cases the new frame is not built up yet and the caller location is only
+  // preserved via lr register.
+  st->print("lr:   " PTR64_FORMAT " ", p2i(cur_lr));
+  print_info_for_pc(st, cur_lr, buf, buf_size, demangle);
+  st->cr();
+
+  if (cur_lr && MiscUtils::is_readable_pointer(cur_lr)) {
+    decode_instructions_at_pc(
+      "Decoded instructions at lr:",
+      cur_lr, 32, 16, st);
+  }
+
+  // Check and print sp.
+  st->print("sp:   " PTR64_FORMAT " ", p2i(cur_sp));
+  if (!is_valid_stackpointer(cur_sp, stack_base, stack_size)) {
+    st->print("(invalid) ");
+    goto cleanup;
+  } else {
+    st->print("(base - 0x%X) ", PTRDIFF_BYTES(stack_base, cur_sp));
+  }
+  st->cr();
+
+  // Check and print rtoc.
+  st->print("rtoc: "  PTR64_FORMAT " ", p2i(cur_rtoc));
+  if (cur_rtoc == NULL || cur_rtoc == (codeptr_t)-1 ||
+      !MiscUtils::is_readable_pointer(cur_rtoc)) {
+    st->print("(invalid)");
+  } else if (((uintptr_t)cur_rtoc) & 0x7) {
+    st->print("(unaligned)");
+  }
+  st->cr();
+
+  st->print_cr("|---stackaddr----|   |----lrsave------|:   <function name>");
+
+  ///
+  // Walk callstack.
+  //
+  // (if no context was given, use the current stack)
+  sp = (unsigned long*)(*(unsigned long*)cur_sp); // Stack pointer
+  sp_last = cur_sp;
+
+  frame = 0;
+
+  while (frame < MAX_CALLSTACK_DEPTH) {
+
+    // Check sp.
+    bool retry = false;
+    if (sp == NULL) {
+      // The backchain pointer was NULL. This normally means the end of the chain. But the
+      // stack might be corrupted, and it may be worth looking for the stack chain.
+      if (is_valid_stackpointer(sp_last, stack_base, stack_size) && (stack_base - 0x10) > sp_last) {
+        // If we are not within <guess> 0x10 stackslots of the stack base, we assume that this
+        // is indeed not the end of the chain but that the stack was corrupted. So lets try to
+        // find the end of the chain.
+        st->print_cr("*** back chain pointer is NULL - end of stack or broken backchain ? ***");
+        retry = true;
+      } else {
+        st->print_cr("*** end of backchain ***");
+        goto end_walk_callstack;
+      }
+    } else if (!is_valid_stackpointer(sp, stack_base, stack_size)) {
+      st->print_cr("*** stack pointer invalid - backchain corrupted (" PTR_FORMAT ") ***", p2i(sp));
+      retry = true;
+    } else if (sp < sp_last) {
+      st->print_cr("invalid stack pointer: " PTR_FORMAT " (not monotone raising)", p2i(sp));
+      retry = true;
+    }
+
+    // If backchain is broken, try to recover, by manually scanning the stack for a pattern
+    // which looks like a valid stack.
+    if (retry) {
+      st->print_cr("trying to recover and find backchain...");
+      sp = try_find_backchain(sp_last, stack_base, stack_size);
+      if (sp) {
+        st->print_cr("found something which looks like a backchain at " PTR64_FORMAT ", after 0x%x bytes... ",
+            p2i(sp), PTRDIFF_BYTES(sp, sp_last));
+      } else {
+        st->print_cr("did not find a backchain, giving up.");
+        goto end_walk_callstack;
+      }
+    }
+
+    // Print stackframe.
+    print_stackframe(st, sp, buf, buf_size, demangle);
+    st->cr();
+    frame ++;
+
+    // Next stack frame and link area.
+    sp_last = sp;
+    sp = (unsigned long*)(*sp);
+  }
+
+  // Prevent endless loops in case of invalid callstacks.
+  if (frame == MAX_CALLSTACK_DEPTH) {
+    st->print_cr("...(stopping after %d frames.", MAX_CALLSTACK_DEPTH);
+  }
+
+end_walk_callstack:
+
+  st->print_cr("-----------------------");
+
+cleanup:
+
+  return;
+
+}
+
+
+
+
--- a/hotspot/src/os/aix/vm/porting_aix.hpp	Sat Jan 16 00:28:28 2016 +0000
+++ b/hotspot/src/os/aix/vm/porting_aix.hpp	Wed Jan 13 17:55:57 2016 +0100
@@ -61,24 +61,37 @@
 #endif
 int dladdr(void *addr, Dl_info *info);
 
-typedef unsigned int* codeptr_t;
-
 struct tbtable;
 
-// helper function - given a program counter, tries to locate the traceback table and
-// returns info from it (like, most importantly, function name, displacement of the
-// pc inside the function, and the traceback table itself.
-#ifdef __cplusplus
-extern "C"
-#endif
-int getFuncName(
-      codeptr_t pc,                    // [in] program counter
-      char* p_name, size_t namelen,    // [out] optional: user provided buffer for the function name
-      int* p_displacement,             // [out] optional: displacement
-      const struct tbtable** p_tb,     // [out] optional: ptr to traceback table to get further information
-      char* p_errmsg, size_t errmsglen, // [out] optional: user provided buffer for error messages
-      bool demangle                    // [in] whether to demangle the name
-    );
+class AixSymbols {
+ public:
+
+  // Given a program counter, tries to locate the traceback table and returns info from
+  // it - e.g. function name, displacement of the pc inside the function, and the traceback
+  // table itself.
+  static bool get_function_name (
+    address pc,                      // [in] program counter
+    char* p_name, size_t namelen,    // [out] optional: user provided buffer for the function name
+    int* p_displacement,             // [out] optional: displacement
+    const struct tbtable** p_tb,     // [out] optional: ptr to traceback table to get further information
+    bool demangle                    // [in] whether to demangle the name
+  );
+
+  // Given a program counter, returns the name of the module (library and module) the pc points to
+  static bool get_module_name (
+    address pc,                      // [in] program counter
+    char* p_name, size_t namelen     // [out] module name
+  );
+
+};
+
+class AixNativeCallstack {
+ public:
+  static void print_callstack_for_context(outputStream* st, const ucontext_t* uc,
+                                          bool demangle,
+                                          char* buf, size_t buf_size);
+};
+
 
 #endif // OS_AIX_VM_PORTING_AIX_HPP
 
--- a/hotspot/src/os_cpu/aix_ppc/vm/os_aix_ppc.cpp	Sat Jan 16 00:28:28 2016 +0000
+++ b/hotspot/src/os_cpu/aix_ppc/vm/os_aix_ppc.cpp	Wed Jan 13 17:55:57 2016 +0100
@@ -40,6 +40,7 @@
 #include "prims/jniFastGetField.hpp"
 #include "prims/jvm.h"
 #include "prims/jvm_misc.hpp"
+#include "porting_aix.hpp"
 #include "runtime/arguments.hpp"
 #include "runtime/extendedPC.hpp"
 #include "runtime/frame.inline.hpp"
@@ -579,3 +580,9 @@
   return 0;
 }
 
+bool os::platform_print_native_stack(outputStream* st, void* context, char *buf, int buf_size) {
+  AixNativeCallstack::print_callstack_for_context(st, (const ucontext_t*)context, true, buf, (size_t) buf_size);
+  return true;
+}
+
+
--- a/hotspot/src/os_cpu/aix_ppc/vm/os_aix_ppc.hpp	Sat Jan 16 00:28:28 2016 +0000
+++ b/hotspot/src/os_cpu/aix_ppc/vm/os_aix_ppc.hpp	Wed Jan 13 17:55:57 2016 +0100
@@ -32,4 +32,8 @@
   // Note: Currently only used in 64 bit Windows implementations
   static bool register_code_area(char *low, char *high) { return true; }
 
+#define PLATFORM_PRINT_NATIVE_STACK 1
+static bool platform_print_native_stack(outputStream* st, void* context,
+                                        char *buf, int buf_size);
+
 #endif // OS_CPU_AIX_PPC_VM_OS_AIX_PPC_HPP