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_enable_all(jvmtiEnv * jvmtiLocate)32 void jrst_enable_all(jvmtiEnv *jvmtiLocate)
33 {
34 	jvmtiError error;
35 
36 	/*Method && Exception*/
37 	methodsTrace = true;
38 
39 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, (jthread)NULL);
40 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_EXCEPTION>");
41 
42 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, (jthread)NULL);
43 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_FRAME_POP>");
44 
45 
46 	/*MemoryAllocation*/
47 	memoryTrace  = true;
48 
49 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_OBJECT_FREE, (jthread)NULL);
50 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_OBJECT_FREE>");
51 
52 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, (jthread)NULL);
53 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_GARBAGE_COLLECTION_START>");
54 
55 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, (jthread)NULL);
56 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_GARBAGE_COLLECTION_FINISH>");
57 
58 	/*Monitor*/
59 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, (jthread)NULL);
60 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_MONITOR_CONTENDED_ENTER>");
61 
62 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, (jthread)NULL);
63 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_MONITOR_CONTENDED_ENTERED>");
64 
65 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAIT, (jthread)NULL);
66 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_MONITOR_WAIT>");
67 
68 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAITED, (jthread)NULL);
69 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_MONITOR_WAITED>");
70 
71 }
72 
jrst_threads(jvmtiEnv * jvmtiLocate)73 void jrst_threads(jvmtiEnv *jvmtiLocate)
74 {
75 	jvmtiError error;
76 
77 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_THREAD_START, (jthread)NULL);
78 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_THREAD_START>");
79 
80 	error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_THREAD_END, (jthread)NULL);
81 	jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_THREAD_END>");
82 
83 }
84 
jrst_read_events_enable(jvmtiEnv * jvmtiLocate)85 void jrst_read_events_enable(jvmtiEnv *jvmtiLocate)
86 {
87 	FILE *file;
88 	jvmtiError error;
89 	char line[MAX_LINE_EVENTS];
90 	char buffer[MAX_LINE_EVENTS];
91 	int i, j;
92 
93 	if(eventsOptionName[0] == '\0'){
94 		return;
95 	}
96 	file=fopen(eventsOptionName, "r");
97 	if(file == NULL){
98 		printf("\nCannot open the file\n");
99 		exit(2);
100 	}
101 
102 	bzero(buffer, MAX_LINE_EVENTS);
103 	bzero(line, MAX_LINE_EVENTS);
104 	fgets(line, MAX_LINE_EVENTS, file);
105 	while(!feof(file)){
106 		line[strlen(line) - 1]='\0';
107 
108 		for(i=0; line[i] == ' ' || line[i] == '\t'; i++);
109 		if(line[i] != '\0' && line[i] != '#'){
110 			for(j=0 ; line[i] != '\0' && line[i] != ' ' && line[i] != '\t'; i++, j++){
111 				buffer[j]=line[i];
112 			}
113 			buffer[j]='\0';
114 
115 			if(!strcmp(buffer,"all")){
116 				printf("[JRastro] Enable All Traces\n");
117 				jrst_enable_all(jvmtiLocate);
118 				fclose(file);
119 				return;
120 
121 			}else if(!strcmp(buffer,"NoTraces")){
122 				printf("[JRastro] No Enable Traces\n");
123 				traces=false;
124 				fclose(file);
125 				return;
126 
127 			}else if(!strcmp(buffer,"Method")){
128 				methodsTrace = true;
129 			/*Comentei para ficar apenas o Method*/
130 			//}else if(!strcmp(buffer,"Exception")){
131 			//	methodsTrace = true;
132 
133 				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, (jthread)NULL);
134 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_EXCEPTION>");
135 
136 //				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION_CATCH, (jthread)NULL);
137 //				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_EXCEPTION_CATCH>");
138 
139 				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, (jthread)NULL);
140 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_FRAME_POP>");
141 
142 			}else if(!strcmp(buffer,"MemoryAllocation")){
143 				memoryTrace  = true;
144 
145 				/*error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, (jthread)NULL);
146 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_VM_OBJECT_ALLOC>");*/
147 
148 				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_OBJECT_FREE, (jthread)NULL);
149 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_OBJECT_FREE>");
150 
151 				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, (jthread)NULL);
152 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_GARBAGE_COLLECTION_START>");
153 
154 				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, (jthread)NULL);
155 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_GARBAGE_COLLECTION_FINISH>");
156 
157 			}else if(!strcmp(buffer,"Monitor")){
158 				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, (jthread)NULL);
159 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_MONITOR_CONTENDED_ENTER>");
160 
161 				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, (jthread)NULL);
162 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_MONITOR_CONTENDED_ENTERED>");
163 
164 				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAIT, (jthread)NULL);
165 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_MONITOR_WAIT>");
166 
167 				error=(*jvmtiLocate)->SetEventNotificationMode(jvmtiLocate, JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAITED, (jthread)NULL);
168 				jrst_check_error(jvmtiLocate, error, "Cannot set event notification <JVMTI_EVENT_MONITOR_WAITED>");
169 
170 			}
171 
172 		}
173 
174 		bzero(line, MAX_LINE_EVENTS);
175 		bzero(buffer, MAX_LINE_EVENTS);
176 		fgets(line, MAX_LINE_EVENTS, file);
177 
178 	}
179 	fclose(file);
180 }
181 
jrst_read_class_methods_enable()182 void jrst_read_class_methods_enable()
183 {
184 	FILE *file;
185 	char line[MAX_LINE];
186 	char class[MAX_LINE];
187 	char method[MAX_LINE];
188 	char type;
189 	int i = 0, j = 0;
190 	list_t *new_list = NULL;
191 	bool all = true;
192 
193 	if(methodsOptionName[0] == '\0'){
194 		return;
195 	}
196 
197 	file=fopen(methodsOptionName, "r");
198 	if(file == NULL){
199 		printf("\nCannot open the file\n");
200 		exit(2);
201 	}
202 	strcpy(class, "*");
203 	strcpy(method, "*");
204 	bzero(line, MAX_LINE);
205 	fgets(line, MAX_LINE, file);
206 	while(!feof(file)){
207 		line[strlen(line) - 1]='\0';
208 
209 		for(i=0; line[i] == ' ' || line[i] == '\t'; i++);
210 		while(line[i] != '\0' && line[i] != '#'){
211 			type = line[i];
212 			i++;
213 			if(type == 'C' || type == 'c'){
214 				if(new_list != NULL){
215 					hash_insert(&h_options, (hash_key_t)class, (hash_data_t)new_list);
216 				}
217 				all = false;
218 				new_list = NULL;
219 				new_list = (list_t *) malloc(sizeof(list_t));
220 				if(new_list == NULL){
221 					printf("[JRastro ERROR]: Cannot malloc list\n");
222 					exit(3);
223 				}
224 				list_initialize(new_list, list_copy_string, list_cmp_string, list_destroy_string);
225 				bzero(class, MAX_LINE);
226 				for(; line[i] == ' ' || line[i] == '\t'; i++);
227 				for(j=0 ; line[i] != '\0' && line[i] != ' ' && line[i] != '\t'; i++, j++){
228 					class[j]=line[i];
229 				}
230 				class[j]='\0';
231 				if(strcmp(class, "*") == 0){
232 					all = true;
233 				}
234 				for(; line[i] == ' ' || line[i] == '\t'; i++);
235 			}else if(type == 'M' || type == 'm'){
236 				bzero(method, MAX_LINE);
237 				for(; line[i] == ' ' || line[i] == '\t'; i++);
238 				for(j=0 ; line[i] != '\0' && line[i] != ' ' && line[i] != '\t'; i++, j++){
239 					method[j]=line[i];
240 				}
241 				method[j]='\0';
242 				if(all){
243 					if(strcmp(method, "*") == 0){
244 						printf("[JRastro] Enable All Classes and Methods\n");
245 						tracesAll=true;
246 						fclose(file);
247 						return;
248 					}
249 				}
250 				if(new_list == NULL){
251 					new_list = (list_t *) malloc(sizeof(list_t));
252 					if(new_list == NULL){
253 						printf("[JRastro ERROR]: Cannot malloc list\n");
254 						exit(3);
255 					}
256 					list_initialize(new_list, list_copy_string, list_cmp_string, list_destroy_string);
257 				}
258 				list_insert_after(new_list, NULL, (list_data_t)method);
259 				for(; line[i] == ' ' || line[i] == '\t'; i++);
260 			}else{
261 				printf("\n[JRastro ERROR]:Type desconhecido\n");
262 				exit(2);
263 
264 			}
265 		}
266 		bzero(line, MAX_LINE);
267 		fgets(line, MAX_LINE, file);
268 	}
269 	if(new_list != NULL){
270 		hash_insert(&h_options, (hash_key_t)class, (hash_data_t)new_list);
271 	}
272 
273 	fclose(file);
274 }
275 
276 /*funcao que le as opcoes recebidas pela funcao "Agent_OnLoad"*/
jrst_read_names_options(char * options)277 void jrst_read_names_options(char *options)
278 {
279 	int count=0, count2=0;
280 
281 	if(options == NULL){
282 		eventsOptionName[0] = '\0';
283 		methodsOptionName[0] = '\0';
284 		return;
285 	}
286 
287 	bzero(eventsOptionName, MAX_NAME_OPTIONS);
288 	bzero(methodsOptionName, MAX_NAME_OPTIONS);
289 	for(count=0;options[count] != ',' && options[count] != '\0';count++){
290 		eventsOptionName[count]=options[count];
291 	}
292 	eventsOptionName[count]='\0';
293 
294 	if(options[count] != '\0'){
295 		count++;
296 		for(count2 = 0; options[count] != '\0';count++,count2++){
297 			methodsOptionName[count2]=options[count];
298 		}
299 		methodsOptionName[count2]='\0';
300 	}else{
301 		methodsOptionName[0]='\0';
302 	}
303 }
304