When print_trace() is called from unit tests it cannot use stack walker
functionallity because it's stubbed. There is no need to use it anyway
because we should not encounter JIT frames there. Solution for this
is to use standard backtrace() function for stack trace printing
in unit tests.

Signed-off-by: Tomek Grabiec <tgrab...@gmail.com>
---
 arch/x86/backtrace.c       |   59 ++-----------------------------------------
 include/vm/backtrace.h     |    4 +++
 include/vm/stack-trace.h   |    2 +
 test/vm/stack-trace-stub.c |   33 ++++++++++++++++++++++++
 vm/stack-trace.c           |   43 ++++++++++++++++++++++++++++++++
 5 files changed, 85 insertions(+), 56 deletions(-)

diff --git a/arch/x86/backtrace.c b/arch/x86/backtrace.c
index 842ce7a..aaa0615 100644
--- a/arch/x86/backtrace.c
+++ b/arch/x86/backtrace.c
@@ -58,7 +58,7 @@ static asymbol *lookup_symbol(asymbol ** symbols, int 
nr_symbols,
        return NULL;
 }
 
-static bool show_exe_function(void *addr)
+bool show_exe_function(void *addr)
 {
        bool ret;
        const char *function_name;
@@ -129,7 +129,7 @@ failed:
        goto out;
 }
 
-static void show_function(void *addr)
+void show_function(void *addr)
 {
        if (show_exe_function(addr))
                return;
@@ -137,57 +137,6 @@ static void show_function(void *addr)
        printf("<unknown>\n");
 }
 
-/* Must be inline so this does not change the backtrace. */
-static inline void __show_stack_trace(unsigned long start, unsigned long 
caller)
-{
-       void *array[10];
-       size_t size;
-       size_t i;
-
-       size = backtrace(array, 10);
-       array[1] = (void *) caller;
-
-       printf("Native stack trace:\n");
-       for (i = start; i < size; i++) {
-               printf(" [<%08lx>] ", (unsigned long) array[i]);
-               show_function(array[i]);
-       }
-}
-
-static void show_mixed_stack_trace(struct stack_trace_elem *elem)
-{
-       printf("Native and JAVA stack trace:\n");
-       do {
-               printf(" [<%08lx>] %-10s : ", elem->addr,
-                      stack_trace_elem_type_name(elem->type));
-
-               if (elem->type != STACK_TRACE_ELEM_TYPE_OTHER) {
-                       print_java_stack_trace_elem(elem);
-                       printf("\n");
-
-                       printf("%-27s"," ");
-                       if (!show_exe_function((void *) elem->addr))
-                               printf("\r");
-
-                       continue;
-               }
-
-               show_function((void *) elem->addr);
-       } while (stack_trace_elem_next(elem) == 0);
-}
-
-void print_trace(void)
-{
-       struct stack_trace_elem elem;
-
-       init_stack_trace_elem_current(&elem);
-
-       /* Skip init_stack_trace_elem_current() */
-       stack_trace_elem_next(&elem);
-
-       show_mixed_stack_trace(&elem);
-}
-
 static unsigned long get_greg(gregset_t gregs, int reg)
 {
        return (unsigned long)gregs[reg];
@@ -290,9 +239,7 @@ void print_backtrace_and_die(int sig, siginfo_t *info, void 
*secret)
        };
        show_registers(uc->uc_mcontext.gregs);
 
-       struct stack_trace_elem elem;
-       init_stack_trace_elem(&elem, eip, (void *) ebp);
-       show_mixed_stack_trace(&elem);
+       print_trace_from(eip, (void *) ebp);
 
        exit(1);
 }
diff --git a/include/vm/backtrace.h b/include/vm/backtrace.h
index f79fa77..c7dd4de 100644
--- a/include/vm/backtrace.h
+++ b/include/vm/backtrace.h
@@ -2,8 +2,12 @@
 #define __BACKTRACE_H
 
 #include <signal.h>
+#include <stdbool.h>
 
 extern void print_backtrace_and_die(int, siginfo_t *, void *);
 extern void print_trace(void);
+extern void print_trace_from(unsigned long, void*);
+extern bool show_exe_function(void *addr);
+extern void show_function(void *addr);
 
 #endif
diff --git a/include/vm/stack-trace.h b/include/vm/stack-trace.h
index 7a6cb1f..315fb29 100644
--- a/include/vm/stack-trace.h
+++ b/include/vm/stack-trace.h
@@ -153,5 +153,7 @@ struct vm_object *native_vmthrowable_get_stack_trace(struct 
vm_object *, struct
 bool called_from_jit_trampoline(struct native_stack_frame *frame);
 void vm_print_exception(struct vm_object *exception);
 struct vm_object *vm_alloc_stack_overflow_error(void);
+void print_trace_from(unsigned long eip, void *frame);
+void print_trace(void);
 
 #endif /* JATO_VM_STACK_TRACE_H */
diff --git a/test/vm/stack-trace-stub.c b/test/vm/stack-trace-stub.c
index 37ebc5d..48cb7c9 100644
--- a/test/vm/stack-trace-stub.c
+++ b/test/vm/stack-trace-stub.c
@@ -1,3 +1,7 @@
+#include <execinfo.h>
+#include <stdio.h>
+
+#include "vm/backtrace.h"
 #include "vm/stack-trace.h"
 
 int vm_enter_jni(void *caller_frame, unsigned long call_site_addr,
@@ -37,3 +41,32 @@ const char *stack_trace_elem_type_name(enum 
stack_trace_elem_type type)
 {
        return NULL;
 }
+
+/* Must be inline so this does not change the backtrace. */
+static inline void __show_stack_trace(unsigned long start, unsigned long 
caller)
+{
+       void *array[10];
+       size_t size;
+       size_t i;
+
+       size = backtrace(array, 10);
+
+       if (caller)
+               array[1] = (void *) caller;
+
+       printf("Native stack trace:\n");
+       for (i = start; i < size; i++) {
+               printf(" [<%08lx>] ", (unsigned long) array[i]);
+               show_function(array[i]);
+       }
+}
+
+void print_trace(void)
+{
+       __show_stack_trace(1, 0);
+}
+
+void print_trace_from(unsigned long eip, void *frame)
+{
+       __show_stack_trace(1, eip);
+}
diff --git a/vm/stack-trace.c b/vm/stack-trace.c
index 11fc9a7..fc1575e 100644
--- a/vm/stack-trace.c
+++ b/vm/stack-trace.c
@@ -24,6 +24,7 @@
  * Please refer to the file LICENSE for details.
  */
 
+#include "vm/backtrace.h"
 #include "vm/call.h"
 #include "vm/class.h"
 #include "vm/classloader.h"
@@ -700,3 +701,45 @@ const char *stack_trace_elem_type_name(enum 
stack_trace_elem_type type)
 
        return stack_trace_elem_type_names[type];
 }
+
+static void show_mixed_stack_trace(struct stack_trace_elem *elem)
+{
+       printf("Native and JAVA stack trace:\n");
+       do {
+               printf(" [<%08lx>] %-10s : ", elem->addr,
+                      stack_trace_elem_type_name(elem->type));
+
+               if (elem->type != STACK_TRACE_ELEM_TYPE_OTHER) {
+                       print_java_stack_trace_elem(elem);
+                       printf("\n");
+
+                       printf("%-27s"," ");
+                       if (!show_exe_function((void *) elem->addr))
+                               printf("\r");
+
+                       continue;
+               }
+
+               show_function((void *) elem->addr);
+       } while (stack_trace_elem_next(elem) == 0);
+}
+
+void print_trace(void)
+{
+       struct stack_trace_elem elem;
+
+       init_stack_trace_elem_current(&elem);
+
+       /* Skip init_stack_trace_elem_current() */
+       stack_trace_elem_next(&elem);
+
+       show_mixed_stack_trace(&elem);
+}
+
+void print_trace_from(unsigned long eip, void *frame)
+{
+       struct stack_trace_elem elem;
+
+       init_stack_trace_elem(&elem, eip, frame);
+       show_mixed_stack_trace(&elem);
+}
-- 
1.6.0.6


------------------------------------------------------------------------------
_______________________________________________
Jatovm-devel mailing list
Jatovm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jatovm-devel

Reply via email to