Example:

  $ jato -Xtrace:invoke -Xtrace:method 'Vector\.add' jvm/PrintTest
  [main] trace invoke: java/util/Vector.add(Ljava/lang/Object;)Z
  [main] trace invoke: java/util/Vector.addElement(Ljava/lang/Object;)V
  [main] trace invoke: java/util/Vector.add(Ljava/lang/Object;)Z
  [main] trace invoke: java/util/Vector.addElement(Ljava/lang/Object;)V
  [main] trace invoke: java/util/Vector.add(Ljava/lang/Object;)Z
  [main] trace invoke: java/util/Vector.addElement(Ljava/lang/Object;)V
  PrintTest OK.

Signed-off-by: Vegard Nossum <vegard.nos...@gmail.com>
---
 include/jit/compiler.h |   12 ++++++++
 jit/trace-jit.c        |   68 +++++++++++++++++++++++++++++++++++++++++++++++-
 vm/jato.c              |   63 ++++++++------------------------------------
 3 files changed, 90 insertions(+), 53 deletions(-)

diff --git a/include/jit/compiler.h b/include/jit/compiler.h
index 8f5b615..014fb43 100644
--- a/include/jit/compiler.h
+++ b/include/jit/compiler.h
@@ -8,7 +8,9 @@
 #include "vm/vm.h"
 
 #include <pthread.h>
+#include <regex.h>
 #include <stdbool.h>
+#include <sys/types.h>
 
 struct vm_method;
 struct compilation_unit;
@@ -90,6 +92,9 @@ bool is_on_heap(unsigned long addr);
 
 void fixup_direct_calls(struct jit_trampoline *trampoline, unsigned long 
target);
 
+extern bool opt_trace_method;
+extern regex_t method_trace_regex;
+
 extern bool opt_trace_cfg;
 extern bool opt_trace_tree_ir;
 extern bool opt_trace_lir;
@@ -105,6 +110,13 @@ extern bool opt_trace_exceptions;
 extern bool opt_trace_bytecode;
 extern bool opt_trace_compile;
 
+bool method_matches_regex(struct vm_method *vmm);
+
+static inline bool cu_matches_regex(struct compilation_unit *cu)
+{
+       return method_matches_regex(cu->method);
+}
+
 void trace_magic_trampoline(struct compilation_unit *);
 void trace_method(struct compilation_unit *);
 void trace_cfg(struct compilation_unit *);
diff --git a/jit/trace-jit.c b/jit/trace-jit.c
index ea765d1..0246c3b 100644
--- a/jit/trace-jit.c
+++ b/jit/trace-jit.c
@@ -33,9 +33,14 @@
 
 #include <ctype.h>
 #include <malloc.h>
+#include <pthread.h>
+#include <regex.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <pthread.h>
+#include <sys/types.h>
+
+bool opt_trace_method;
+regex_t method_trace_regex;
 
 bool opt_trace_cfg;
 bool opt_trace_tree_ir;
@@ -52,12 +57,34 @@ bool opt_trace_exceptions;
 bool opt_trace_bytecode;
 bool opt_trace_compile;
 
+bool method_matches_regex(struct vm_method *vmm)
+{
+       if (!opt_trace_method)
+               return true;
+
+       int err;
+
+       char *signature;
+       err = asprintf(&signature, "%s.%s%s",
+               vmm->class->name, vmm->name, vmm->type);
+       if (err == -1)
+               die("asprintf");
+
+       err = regexec(&method_trace_regex, signature, 0, NULL, 0);
+       free(signature);
+
+       return err == 0;
+}
+
 void trace_method(struct compilation_unit *cu)
 {
        struct vm_method *method = cu->method;
        unsigned char *p;
        unsigned int i, j;
 
+       if (!method_matches_regex(method))
+               return;
+
        trace_printf("\nTRACE: %s.%s%s\n",
                method->class->name, method->name, method->type);
 
@@ -94,6 +121,9 @@ void trace_cfg(struct compilation_unit *cu)
 {
        struct basic_block *bb;
 
+       if (!cu_matches_regex(cu))
+               return;
+
        trace_printf("Control Flow Graph:\n\n");
        trace_printf("  #:\t\tRange\t\tSuccessors\t\tPredecessors\n");
 
@@ -140,6 +170,9 @@ void trace_tree_ir(struct compilation_unit *cu)
        struct statement *stmt;
        struct string *str;
 
+       if (!cu_matches_regex(cu))
+               return;
+
        trace_printf("High-Level Intermediate Representation (HIR):\n\n");
 
        for_each_basic_block(bb, &cu->bb_list) {
@@ -161,6 +194,9 @@ void trace_lir(struct compilation_unit *cu)
        struct string *str;
        struct insn *insn;
 
+       if (!cu_matches_regex(cu))
+               return;
+
        trace_printf("Low-Level Intermediate Representation (LIR):\n\n");
 
        trace_printf("Bytecode   LIR\n");
@@ -272,6 +308,9 @@ void trace_liveness(struct compilation_unit *cu)
        unsigned long offset;
        struct insn *insn;
 
+       if (!cu_matches_regex(cu))
+               return;
+
        trace_printf("Liveness:\n\n");
 
        trace_printf("Legend: (U) In use, (-) Fixed register, (*) Non-fixed 
register\n\n");
@@ -299,6 +338,9 @@ void trace_regalloc(struct compilation_unit *cu)
 {
        struct var_info *var;
 
+       if (!cu_matches_regex(cu))
+               return;
+
        trace_printf("Register Allocation:\n\n");
 
        for_each_variable(var, cu->var_infos) {
@@ -336,6 +378,9 @@ void trace_machine_code(struct compilation_unit *cu)
 {
        void *start, *end;
 
+       if (!cu_matches_regex(cu))
+               return;
+
        trace_printf("Disassembler Listing:\n\n");
 
        start  = buffer_ptr(cu->objcode);
@@ -376,6 +421,9 @@ static void print_gc_map(struct compilation_unit *cu, 
struct insn *insn)
 
 void trace_gc_maps(struct compilation_unit *cu)
 {
+       if (!cu_matches_regex(cu))
+               return;
+
        trace_printf("GC Map:\n\n");
 
        struct basic_block *bb;
@@ -396,6 +444,9 @@ void trace_gc_maps(struct compilation_unit *cu)
 
 void trace_magic_trampoline(struct compilation_unit *cu)
 {
+       if (!cu_matches_regex(cu))
+               return;
+
        trace_printf("jit_magic_trampoline: ret0=%p, ret1=%p: %s.%s #%d\n",
               __builtin_return_address(1),
               __builtin_return_address(2),
@@ -632,6 +683,9 @@ static void trace_return_address(struct jit_stack_frame 
*frame)
 void trace_invoke(struct compilation_unit *cu)
 {
        struct vm_method *vmm = cu->method;
+       if (!method_matches_regex(vmm))
+               return;
+
        struct vm_class *vmc = vmm->class;
 
        trace_printf("trace invoke: %s.%s%s\n", vmc->name, vmm->name,
@@ -659,6 +713,9 @@ void trace_exception(struct compilation_unit *cu, struct 
jit_stack_frame *frame,
        struct vm_object *msg;
        int dummy;
 
+       if (!cu_matches_regex(cu))
+               return;
+
        vmm = cu->method;
        vmc = vmm->class;
 
@@ -686,6 +743,9 @@ void trace_exception(struct compilation_unit *cu, struct 
jit_stack_frame *frame,
 void trace_exception_handler(struct compilation_unit *cu,
                             unsigned char *ptr)
 {
+       if (!cu_matches_regex(cu))
+               return;
+
        trace_printf("\taction\t: jump to handler at %p\n", ptr);
        trace_printf("\t\t  (");
        print_source_and_line(cu, ptr);
@@ -724,6 +784,9 @@ void trace_exception_unwind_to_native(struct 
jit_stack_frame *frame)
 
 void trace_bytecode(struct vm_method *method)
 {
+       if (!method_matches_regex(method))
+               return;
+
        trace_printf("Code:\n");
        bytecode_disassemble(method->class,
                             method->code_attribute.code,
@@ -740,6 +803,9 @@ void trace_return_value(struct vm_method *vmm, unsigned 
long long value)
        enum vm_type type;
        int dummy;
 
+       if (!method_matches_regex(vmm))
+               return;
+
        dummy = 0;
        type = method_return_type(vmm);
 
diff --git a/vm/jato.c b/vm/jato.c
index 2882ee9..924ee24 100644
--- a/vm/jato.c
+++ b/vm/jato.c
@@ -27,6 +27,7 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <regex.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -903,7 +904,6 @@ static void handle_classpath(const char *arg)
 enum operation {
        OPERATION_MAIN_CLASS,
        OPERATION_JAR_FILE,
-       OPERATION_METHOD_TRACE,
 };
 
 static enum operation operation = OPERATION_MAIN_CLASS;
@@ -911,10 +911,6 @@ static enum operation operation = OPERATION_MAIN_CLASS;
 static char *classname;
 static struct vm_jar *jar_file;
 
-static char *method_trace_class_name;
-static char *method_trace_method_name;
-static char *method_trace_method_type;
-
 static void handle_jar(const char *arg)
 {
        operation = OPERATION_JAR_FILE;
@@ -955,25 +951,19 @@ static void handle_perf(void)
 /* @arg must be in the format package/name/Class.method(Lsignature;)V */
 static void handle_trace_method(const char *arg)
 {
-       char *next;
-
-       operation = OPERATION_METHOD_TRACE;
-
-       next = strchr(arg, '.');
-       if (!next)
-               usage(stderr, EXIT_FAILURE);
+       opt_trace_method = true;
 
-       method_trace_class_name = strndup(arg, next - arg);
+       int err = regcomp(&method_trace_regex, arg, REG_EXTENDED | REG_NOSUB);
+       if (err) {
+               unsigned int size = regerror(err, &method_trace_regex, NULL, 0);
+               char *errbuf = malloc(size);
+               regerror(err, &method_trace_regex, errbuf, size);
 
-       arg = next + 1;
-       next = strchr(arg, '(');
-       if (!next)
-               usage(stderr, EXIT_FAILURE);
-
-       method_trace_method_name = strndup(arg, next - arg);
+               fprintf(stderr, "error: regcomp: %s\n", errbuf);
+               free(errbuf);
 
-       arg = next;
-       method_trace_method_type = strdup(arg);
+               exit(EXIT_FAILURE);
+       }
 }
 
 static void handle_trace_asm(void)
@@ -1254,33 +1244,6 @@ do_jar_file(void)
        return do_main_class();
 }
 
-static int
-do_method_trace(void)
-{
-       struct vm_object *loader;
-
-       loader = get_system_class_loader();
-       if (!loader)
-               return -1;
-
-       struct vm_class *vmc =
-               classloader_load(loader, method_trace_class_name);
-       if (!vmc) {
-               NOT_IMPLEMENTED;
-               return -1;
-       }
-
-       struct vm_method *vmm = vm_class_get_method_recursive(vmc,
-               method_trace_method_name, method_trace_method_type);
-       if (!vmm) {
-               NOT_IMPLEMENTED;
-               return -1;
-       }
-
-       compile(vmm->compilation_unit);
-       return 0;
-}
-
 struct gnu_classpath_config {
        const char *glibj;
        const char *lib;
@@ -1375,10 +1338,6 @@ main(int argc, char *argv[])
        case OPERATION_JAR_FILE:
                do_jar_file();
                break;
-       case OPERATION_METHOD_TRACE:
-               do_method_trace();
-               exit(EXIT_SUCCESS);
-               break;
        }
 
 out_check_exception:
-- 
1.6.0.4


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Jatovm-devel mailing list
Jatovm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jatovm-devel

Reply via email to