summaryrefslogtreecommitdiff
path: root/java/method.c
blob: 35e602242134dd7b57ea74d793e2b3072b154d94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <string.h>

#include "method.h"
#include "config.h"
#include "misc.h"

static bool handle_method (jvmtiEnv *jvmti, JNIEnv* jni, jthread thread,
                          jmethodID method, char **object, char **method_name,
                          char **class_name)
{
   jvmtiError error;
   char *class_sig = NULL, object_buf[SIZE_OBJECT_BUF];
   jclass klass;
   bool success = FALSE;
   
   *object = *method_name = *class_name = NULL;
   
   if ((error =
        (*jvmti)->GetMethodName (jvmti, method, method_name, NULL, NULL))
       != JVMTI_ERROR_NONE)
      jvmti_error (jvmti, error, "GetMethodName");
   else if ((error =
             (*jvmti)->GetMethodDeclaringClass (jvmti, method, &klass))
            != JVMTI_ERROR_NONE)
      jvmti_error (jvmti, error, "GetMethodDeclaringClass");
   else if ((error =
             (*jvmti)->GetClassSignature (jvmti, klass, &class_sig, NULL))
            != JVMTI_ERROR_NONE)
      jvmti_error (jvmti, error, "GetClassSignature");
   else {
      if ((*class_name = get_class_name_from_sig (class_sig, TRUE)) &&
          include_class (*class_name)) {
         jint numlocals;
         jvmtiLocalVariableEntry *locals;
         if ((error = (*jvmti)->GetLocalVariableTable (jvmti, method,
                                                       &numlocals, &locals))
             != JVMTI_ERROR_NONE)
            jvmti_error (jvmti, error, "GetLocalVariableTable");
         else {
            jint j, this_slot = -1;
            for (j = 0; j < numlocals && this_slot == -1; j++)
               if (strcmp (locals[j].name, "this") == 0)
                  this_slot = locals[j].slot;

            jobject this_obj;
            
            if (this_slot == -1) {
               this_obj = NULL;
               success = TRUE;
            } else {
               if ((error = (*jvmti)->GetLocalObject (jvmti, thread, 0,
                                                      this_slot, &this_obj))
                   != JVMTI_ERROR_NONE) {
                  jvmti_error (jvmti, error, "GetLocalObject");
               } else
                  success = TRUE;
            }

            if (success) {
               fill_object_buf (jni, object_buf, this_obj);
               *object = strdup (object_buf);
            }
         }
      }
   }

   jvmti_dealloc (jvmti, class_sig);

   return success;
}

static void handle_method_free (jvmtiEnv *jvmti, char *object,
                                char *method_name, char *class_name)
{
   simple_free (object);
   jvmti_dealloc (jvmti, method_name);
   simple_free (class_name);
}

void JNICALL method_entry (jvmtiEnv *jvmti, JNIEnv* jni, jthread thread,
                           jmethodID method)
{
   char *object, *method_name, *class_name;

   if (handle_method (jvmti, jni, thread, method, &object, &method_name,
                      &class_name)) {
      if (strcmp (method_name, "<init>") == 0)
         RTFL_OBJ_PRINT ("create", "s:s", object, class_name);
      else
         RTFL_OBJ_PRINT ("enter", "s:s:d:s:",
                         object, "", 0, method_name, class_name);
   }

   handle_method_free (jvmti, object, method_name, class_name);
}

void JNICALL method_exit (jvmtiEnv *jvmti, JNIEnv* jni, jthread thread,
                          jmethodID method, jboolean was_popped_by_exception,
                          jvalue return_value)
{
   char *object, *method_name, *class_name;

   if (handle_method (jvmti, jni, thread, method, &object, &method_name,
                      &class_name)) {
      if (strcmp (method_name, "<init>") != 0)
         // Hopefully, this is the correct method.
         RTFL_OBJ_PRINT ("leave", "s", object);
   }

   handle_method_free (jvmti, object, method_name, class_name);
}