1 /*
2     Copyright (c) 1998--2006 Benhur Stein
3 
4     This file is part of Paj�.
5 
6     Paj� is free software; you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License as published by the
8     Free Software Foundation; either version 2 of the License, or (at your
9     option) any later version.
10 
11     Paj� is distributed in the hope that it will be useful, but WITHOUT ANY
12     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13     FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14     for more details.
15 
16     You should have received a copy of the GNU Lesser General Public License
17     along with Paj�; if not, write to the Free Software Foundation, Inc.,
18 	51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
19 */
20 
21 
22 //////////////////////////////////////////////////
23 /*      Author: Geovani Ricardo Wiedenhoft      */
24 /*      Email: grw@inf.ufsm.br                  */
25 //////////////////////////////////////////////////
26 
27 
28 
29 #include "JRastro.h"
30 
31 
jrst_monitor_contended_enter(jvmtiEnv * jvmtiLocate,JNIEnv * jniEnv,jthread thread,jobject object)32 void JNICALL jrst_monitor_contended_enter(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jobject object)
33 {
34 
35 	jvmtiError error;
36 	jlong tag;
37 
38 	error = (*GET_JVMTI())->GetTag( GET_JVMTI(), object, &tag);
39 	jrst_check_error(GET_JVMTI(), error, "Cannot Get Tag");
40 
41 	trace_event_monitor_contended_enter(jvmtiLocate, thread, (int)tag);
42 
43 }
44 
jrst_monitor_contended_entered(jvmtiEnv * jvmtiLocate,JNIEnv * jniEnv,jthread thread,jobject object)45 void JNICALL jrst_monitor_contended_entered(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jobject object)
46 {
47 
48 	jvmtiError error;
49 	jlong tag;
50 
51 	error = (*GET_JVMTI())->GetTag( GET_JVMTI(), object, &tag);
52 	jrst_check_error(GET_JVMTI(), error, "Cannot Get Tag");
53 
54 	trace_event_monitor_contended_entered(jvmtiLocate, thread, (int)tag);
55 
56 }
57 
jrst_monitor_wait(jvmtiEnv * jvmtiLocate,JNIEnv * jniEnv,jthread thread,jobject object,jlong timeout)58 void JNICALL jrst_monitor_wait(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jobject object, jlong timeout)
59 {
60 
61 	jvmtiError error;
62 	jlong tag;
63 
64 	error = (*GET_JVMTI())->GetTag( GET_JVMTI(), object, &tag);
65 	jrst_check_error(GET_JVMTI(), error, "Cannot Get Tag");
66 
67 	trace_event_monitor_wait(jvmtiLocate, thread, (int)tag);
68 
69 
70 }
71 
jrst_monitor_waited(jvmtiEnv * jvmtiLocate,JNIEnv * jniEnv,jthread thread,jobject object,jboolean timed_out)72 void JNICALL jrst_monitor_waited(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jobject object, jboolean timed_out)
73 {
74 
75 	jvmtiError error;
76 	jlong tag;
77 
78 	error = (*GET_JVMTI())->GetTag( GET_JVMTI(), object, &tag);
79 	jrst_check_error(GET_JVMTI(), error, "Cannot Get Tag");
80 
81 	trace_event_monitor_waited(jvmtiLocate, thread, (int)tag);
82 
83 
84 }
85 
86 /*void JNICALL jrst_event_VMObject_alloc(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jobject object, jclass object_klass, jlong size){}*/
87 
88 
jrst_event_object_free(jvmtiEnv * jvmtiLocate,jlong tag)89 void JNICALL jrst_event_object_free(jvmtiEnv *jvmtiLocate, jlong tag)
90 {
91 
92 	trace_event_object_free(tag);
93 
94 }
95 
jrst_event_garbage_collection_start(jvmtiEnv * jvmtiLocate)96 void JNICALL jrst_event_garbage_collection_start(jvmtiEnv *jvmtiLocate)
97 {
98 
99 	trace_event_gc_start();
100 
101 }
102 
jrst_event_garbage_collection_finish(jvmtiEnv * jvmtiLocate)103 void JNICALL jrst_event_garbage_collection_finish(jvmtiEnv *jvmtiLocate)
104 {
105 
106 	trace_event_gc_finish();
107 
108 }
109 
110 
jrst_find_method_all_class(char * methodName)111 bool jrst_find_method_all_class(char *methodName)
112 {
113 	hash_data_t *data_list;
114 	list_t *allMethods;
115 
116 	data_list = hash_locate(&h_options, (hash_key_t)"*");
117 
118 	if(data_list != NULL){
119 		allMethods = *(list_t **)data_list;
120 		if(list_find(allMethods, (void *)methodName)){
121 			return true;
122 		}
123 	}
124 	return false;
125 }
126 
jrst_deallocate_class(jvmtiEnv * jvmtiLocate,char * classSignature,char * classGeneric)127 void jrst_deallocate_class(jvmtiEnv *jvmtiLocate, char *classSignature, char *classGeneric)
128 {
129 	jvmtiError error;
130 
131 	error = (*jvmtiLocate)->Deallocate(jvmtiLocate, (unsigned char *)classSignature);
132 	jrst_check_error(jvmtiLocate,error, "Cannot deallocate memory");
133 	error = (*jvmtiLocate)->Deallocate(jvmtiLocate, (unsigned char *)classGeneric);
134 	jrst_check_error(jvmtiLocate,error, "Cannot deallocate memory");
135 }
136 
jrst_find_method(jvmtiEnv * jvmtiLocate,jmethodID method,char * methodName)137 bool jrst_find_method(jvmtiEnv *jvmtiLocate, jmethodID method, char *methodName)
138 {
139 	jvmtiError error;
140 	jclass klass;
141 	char *className;
142 	char *classSignature;
143 	char *classGeneric;
144 	int size = 0;
145 	hash_data_t *data_list;
146 	list_t *methods;
147 
148 	error = (*jvmtiLocate)->GetMethodDeclaringClass(jvmtiLocate, method, &klass);
149 	jrst_check_error(jvmtiLocate,error, "Cannot Get Method Declaring Class");
150 
151 	error = (*jvmtiLocate)->GetClassSignature(jvmtiLocate, klass, &classSignature, &classGeneric);
152 	jrst_check_error(jvmtiLocate,error, "Cannot Get Class Signature");
153 
154 	/*Tirar da signature o caracter 'L'*/
155 	className = (char *)classSignature + 1;
156 	/* -1 Pois comeca em 0 o vetor*/
157 	size = strlen(className) - 1;
158 	/*Tira da signature o caracter ';'*/
159 	className[size] = '\0';
160 
161 	data_list = hash_locate(&h_options, (hash_key_t)className);
162 
163 	if(data_list != NULL){
164 		methods = *(list_t **)data_list;
165 		if(list_find(methods, (void *)"*") || list_find(methods, (void *)methodName)){
166 			jrst_deallocate_class(jvmtiLocate, classSignature, classGeneric);
167 			return true;
168 		}
169 	}
170 
171 	jrst_deallocate_class(jvmtiLocate, classSignature, classGeneric);
172 	return false;
173 }
174 
jrst_event_exception(jvmtiEnv * jvmtiLocate,JNIEnv * jniEnv,jthread thread,jmethodID method,jlocation location,jobject exception,jmethodID catch_method,jlocation catch_location)175 void JNICALL jrst_event_exception(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jmethodID method, jlocation location, jobject exception, jmethodID catch_method, jlocation catch_location)
176 {
177 
178 	jvmtiError error;
179 	jlong tag;
180 	char *methodName;
181 	char *methodSignature;
182 	char *methodGsignature;
183 
184 	error = (*GET_JVMTI())->GetTag( GET_JVMTI(), exception, &tag);
185 	jrst_check_error(GET_JVMTI(), error, "Cannot Get Tag");
186 
187 
188 	(*jvmtiLocate)->NotifyFramePop(jvmtiLocate, thread, 0);
189 	(*jvmtiLocate)->NotifyFramePop(jvmtiLocate, thread, 1);
190 
191 	error = (*jvmtiLocate)->GetMethodName(jvmtiLocate, method, &methodName, &methodSignature, &methodGsignature);
192 	jrst_check_error(jvmtiLocate,error, "Cannot read Method Name");
193 
194 	if(methodName == NULL){
195 		return;
196 	}
197 
198 	jrst_enter_critical_section(jvmtiLocate, gagent->monitor);
199 
200 	if(jrst_trace_methods() || jrst_find_method_all_class(methodName) || jrst_find_method(jvmtiLocate, method, methodName)){
201 		trace_event_exception(thread, (int)tag);
202 	}
203 
204 	jrst_exit_critical_section(jvmtiLocate, gagent->monitor);
205 
206 	error = (*jvmtiLocate)->Deallocate(jvmtiLocate, (unsigned char *)methodName);
207 	jrst_check_error(jvmtiLocate,error, "Cannot deallocate memory");
208 	error = (*jvmtiLocate)->Deallocate(jvmtiLocate, (unsigned char *)methodSignature);
209 	jrst_check_error(jvmtiLocate,error, "Cannot deallocate memory");
210 	error = (*jvmtiLocate)->Deallocate(jvmtiLocate, (unsigned char *)methodGsignature);
211 	jrst_check_error(jvmtiLocate,error, "Cannot deallocate memory");
212 
213 }
214 
215 
jrst_event_frame_pop(jvmtiEnv * jvmtiLocate,JNIEnv * jniEnv,jthread thread,jmethodID method,jboolean was_popped_by_exception)216 void JNICALL jrst_event_frame_pop(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jmethodID method, jboolean was_popped_by_exception)
217 {
218 
219 	if(!was_popped_by_exception){
220 		return;
221 	}
222 
223 	jvmtiError error;
224 	char *methodName;
225 	char *methodSignature;
226 	char *methodGsignature;
227 
228 	error = (*jvmtiLocate)->GetMethodName(jvmtiLocate, method, &methodName, &methodSignature, &methodGsignature);
229 	jrst_check_error(jvmtiLocate,error, "Cannot read Method Name");
230 
231 	if(methodName == NULL){
232 		return;
233 	}
234 
235 	error = (*jvmtiLocate)->NotifyFramePop(jvmtiLocate, thread, 1);
236 
237 //	if(strcmp(methodName, "findClass") != 0){
238 //	}
239 
240 	jrst_enter_critical_section(jvmtiLocate, gagent->monitor);
241 
242 	if(jrst_trace_methods() || jrst_find_method_all_class(methodName) || jrst_find_method(jvmtiLocate, method, methodName)){
243 		trace_event_method_exit_exception(thread);
244 	}
245 
246 	jrst_exit_critical_section(jvmtiLocate, gagent->monitor);
247 
248 	error = (*jvmtiLocate)->Deallocate(jvmtiLocate, (unsigned char *)methodName);
249 	jrst_check_error(jvmtiLocate,error, "Cannot deallocate memory");
250 	error = (*jvmtiLocate)->Deallocate(jvmtiLocate, (unsigned char *)methodSignature);
251 	jrst_check_error(jvmtiLocate,error, "Cannot deallocate memory");
252 	error = (*jvmtiLocate)->Deallocate(jvmtiLocate, (unsigned char *)methodGsignature);
253 	jrst_check_error(jvmtiLocate,error, "Cannot deallocate memory");
254 
255 }
256 
257 /*void JNICALL jrst_event_exception_catch(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jmethodID method, jlocation location, jobject exception){}*/
258 
259 /*void JNICALL jrst_event_method_load(jvmtiEnv *jvmtiLocate, jmethodID method, jint code_size, const void* code_addr, jint map_length, const jvmtiAddrLocationMap* map, const void* compile_info){}*/
260 
261 /*void JNICALL jrst_event_method_entry(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jmethodID method){}*/
262 
263 /*void JNICALL jrst_event_method_exit(jvmtiEnv *jvmtiLocate, JNIEnv* jniEnv, jthread thread, jmethodID method, jboolean was_popped_by_exception, jvalue return_value){}*/
264