Linux Audio

Check our new training course

Loading...
v4.6
  1#include <sys/types.h>
  2#include <stdio.h>
  3#include <string.h>
  4#include <stdlib.h>
  5#include <err.h>
  6#include <jvmti.h>
  7#include <jvmticmlr.h>
  8#include <limits.h>
  9
 10#include "jvmti_agent.h"
 11
 12static int has_line_numbers;
 13void *jvmti_agent;
 14
 
 
 
 
 
 
 
 
 
 
 
 
 
 15static jvmtiError
 16do_get_line_numbers(jvmtiEnv *jvmti, void *pc, jmethodID m, jint bci,
 17		    jvmti_line_info_t *tab, jint *nr)
 18{
 19	jint i, lines = 0;
 20	jint nr_lines = 0;
 21	jvmtiLineNumberEntry *loc_tab = NULL;
 22	jvmtiError ret;
 23
 24	ret = (*jvmti)->GetLineNumberTable(jvmti, m, &nr_lines, &loc_tab);
 25	if (ret != JVMTI_ERROR_NONE)
 
 26		return ret;
 
 27
 28	for (i = 0; i < nr_lines; i++) {
 29		if (loc_tab[i].start_location < bci) {
 30			tab[lines].pc = (unsigned long)pc;
 31			tab[lines].line_number = loc_tab[i].line_number;
 32			tab[lines].discrim = 0; /* not yet used */
 33			lines++;
 34		} else {
 35			break;
 36		}
 37	}
 38	(*jvmti)->Deallocate(jvmti, (unsigned char *)loc_tab);
 39	*nr = lines;
 40	return JVMTI_ERROR_NONE;
 41}
 42
 43static jvmtiError
 44get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **tab, int *nr_lines)
 45{
 46	const jvmtiCompiledMethodLoadRecordHeader *hdr;
 47	jvmtiCompiledMethodLoadInlineRecord *rec;
 48	jvmtiLineNumberEntry *lne = NULL;
 49	PCStackInfo *c;
 50	jint nr, ret;
 51	int nr_total = 0;
 52	int i, lines_total = 0;
 53
 54	if (!(tab && nr_lines))
 55		return JVMTI_ERROR_NULL_POINTER;
 56
 57	/*
 58	 * Phase 1 -- get the number of lines necessary
 59	 */
 60	for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
 61		if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
 62			rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
 63			for (i = 0; i < rec->numpcs; i++) {
 64				c = rec->pcinfo + i;
 65				nr = 0;
 66				/*
 67				 * unfortunately, need a tab to get the number of lines!
 68				 */
 69				ret = (*jvmti)->GetLineNumberTable(jvmti, c->methods[0], &nr, &lne);
 70				if (ret == JVMTI_ERROR_NONE) {
 71					/* free what was allocated for nothing */
 72					(*jvmti)->Deallocate(jvmti, (unsigned char *)lne);
 73					nr_total += (int)nr;
 
 
 74				}
 75			}
 76		}
 77	}
 78
 79	if (nr_total == 0)
 80		return JVMTI_ERROR_NOT_FOUND;
 81
 82	/*
 83	 * Phase 2 -- allocate big enough line table
 84	 */
 85	*tab = malloc(nr_total * sizeof(**tab));
 86	if (!*tab)
 87		return JVMTI_ERROR_OUT_OF_MEMORY;
 88
 89	for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
 90		if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
 91			rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
 92			for (i = 0; i < rec->numpcs; i++) {
 93				c = rec->pcinfo + i;
 94				nr = 0;
 95				ret = do_get_line_numbers(jvmti, c->pc,
 96							  c->methods[0],
 97							  c->bcis[0],
 98							  *tab + lines_total,
 99							  &nr);
100				if (ret == JVMTI_ERROR_NONE)
101					lines_total += nr;
102			}
103		}
104	}
105	*nr_lines = lines_total;
106	return JVMTI_ERROR_NONE;
107}
108
109static void JNICALL
110compiled_method_load_cb(jvmtiEnv *jvmti,
111			jmethodID method,
112			jint code_size,
113			void const *code_addr,
114			jint map_length,
115			jvmtiAddrLocationMap const *map,
116			const void *compile_info)
117{
118	jvmti_line_info_t *line_tab = NULL;
119	jclass decl_class;
120	char *class_sign = NULL;
121	char *func_name = NULL;
122	char *func_sign = NULL;
123	char *file_name= NULL;
124	char fn[PATH_MAX];
125	uint64_t addr = (uint64_t)(uintptr_t)code_addr;
126	jvmtiError ret;
127	int nr_lines = 0; /* in line_tab[] */
128	size_t len;
129
130	ret = (*jvmti)->GetMethodDeclaringClass(jvmti, method,
131						&decl_class);
132	if (ret != JVMTI_ERROR_NONE) {
133		warnx("jvmti: cannot get declaring class");
134		return;
135	}
136
137	if (has_line_numbers && map && map_length) {
138		ret = get_line_numbers(jvmti, compile_info, &line_tab, &nr_lines);
139		if (ret != JVMTI_ERROR_NONE) {
140			warnx("jvmti: cannot get line table for method");
141			nr_lines = 0;
142		}
143	}
144
145	ret = (*jvmti)->GetSourceFileName(jvmti, decl_class, &file_name);
146	if (ret != JVMTI_ERROR_NONE) {
147		warnx("jvmti: cannot get source filename ret=%d", ret);
148		goto error;
149	}
150
151	ret = (*jvmti)->GetClassSignature(jvmti, decl_class,
152					  &class_sign, NULL);
153	if (ret != JVMTI_ERROR_NONE) {
154		warnx("jvmti: getclassignature failed");
155		goto error;
156	}
157
158	ret = (*jvmti)->GetMethodName(jvmti, method, &func_name,
159				      &func_sign, NULL);
160	if (ret != JVMTI_ERROR_NONE) {
161		warnx("jvmti: failed getmethodname");
162		goto error;
163	}
164
165	/*
166	 * Assume path name is class hierarchy, this is a common practice with Java programs
167	 */
168	if (*class_sign == 'L') {
169		int j, i = 0;
170		char *p = strrchr(class_sign, '/');
171		if (p) {
172			/* drop the 'L' prefix and copy up to the final '/' */
173			for (i = 0; i < (p - class_sign); i++)
174				fn[i] = class_sign[i+1];
175		}
176		/*
177		 * append file name, we use loops and not string ops to avoid modifying
178		 * class_sign which is used later for the symbol name
179		 */
180		for (j = 0; i < (PATH_MAX - 1) && file_name && j < strlen(file_name); j++, i++)
181			fn[i] = file_name[j];
182		fn[i] = '\0';
183	} else {
184		/* fallback case */
185		strcpy(fn, file_name);
186	}
187	/*
188	 * write source line info record if we have it
189	 */
190	if (jvmti_write_debug_info(jvmti_agent, addr, fn, line_tab, nr_lines))
191		warnx("jvmti: write_debug_info() failed");
192
193	len = strlen(func_name) + strlen(class_sign) + strlen(func_sign) + 2;
194	{
195		char str[len];
196		snprintf(str, len, "%s%s%s", class_sign, func_name, func_sign);
197
198		if (jvmti_write_code(jvmti_agent, str, addr, code_addr, code_size))
199			warnx("jvmti: write_code() failed");
200	}
201error:
202	(*jvmti)->Deallocate(jvmti, (unsigned char *)func_name);
203	(*jvmti)->Deallocate(jvmti, (unsigned char *)func_sign);
204	(*jvmti)->Deallocate(jvmti, (unsigned char *)class_sign);
205	(*jvmti)->Deallocate(jvmti, (unsigned char *)file_name);
206	free(line_tab);
207}
208
209static void JNICALL
210code_generated_cb(jvmtiEnv *jvmti,
211		  char const *name,
212		  void const *code_addr,
213		  jint code_size)
214{
215	uint64_t addr = (uint64_t)(unsigned long)code_addr;
216	int ret;
217
218	ret = jvmti_write_code(jvmti_agent, name, addr, code_addr, code_size);
219	if (ret)
220		warnx("jvmti: write_code() failed for code_generated");
221}
222
223JNIEXPORT jint JNICALL
224Agent_OnLoad(JavaVM *jvm, char *options, void *reserved __unused)
225{
226	jvmtiEventCallbacks cb;
227	jvmtiCapabilities caps1;
228	jvmtiJlocationFormat format;
229	jvmtiEnv *jvmti = NULL;
230	jint ret;
231
232	jvmti_agent = jvmti_open();
233	if (!jvmti_agent) {
234		warnx("jvmti: open_agent failed");
235		return -1;
236	}
237
238	/*
239	 * Request a JVMTI interface version 1 environment
240	 */
241	ret = (*jvm)->GetEnv(jvm, (void *)&jvmti, JVMTI_VERSION_1);
242	if (ret != JNI_OK) {
243		warnx("jvmti: jvmti version 1 not supported");
244		return -1;
245	}
246
247	/*
248	 * acquire method_load capability, we require it
249	 * request line numbers (optional)
250	 */
251	memset(&caps1, 0, sizeof(caps1));
252	caps1.can_generate_compiled_method_load_events = 1;
253
254	ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
255	if (ret != JVMTI_ERROR_NONE) {
256		warnx("jvmti: acquire compiled_method capability failed");
257		return -1;
258	}
259	ret = (*jvmti)->GetJLocationFormat(jvmti, &format);
260        if (ret == JVMTI_ERROR_NONE && format == JVMTI_JLOCATION_JVMBCI) {
261                memset(&caps1, 0, sizeof(caps1));
262                caps1.can_get_line_numbers = 1;
263                caps1.can_get_source_file_name = 1;
264		ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
265                if (ret == JVMTI_ERROR_NONE)
266                        has_line_numbers = 1;
267        }
 
 
268
269	memset(&cb, 0, sizeof(cb));
270
271	cb.CompiledMethodLoad   = compiled_method_load_cb;
272	cb.DynamicCodeGenerated = code_generated_cb;
273
274	ret = (*jvmti)->SetEventCallbacks(jvmti, &cb, sizeof(cb));
275	if (ret != JVMTI_ERROR_NONE) {
276		warnx("jvmti: cannot set event callbacks");
277		return -1;
278	}
279
280	ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
281			JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
282	if (ret != JVMTI_ERROR_NONE) {
283		warnx("jvmti: setnotification failed for method_load");
284		return -1;
285	}
286
287	ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
288			JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
289	if (ret != JVMTI_ERROR_NONE) {
290		warnx("jvmti: setnotification failed on code_generated");
291		return -1;
292	}
293	return 0;
294}
295
296JNIEXPORT void JNICALL
297Agent_OnUnload(JavaVM *jvm __unused)
298{
299	int ret;
300
301	ret = jvmti_close(jvmti_agent);
302	if (ret)
303		errx(1, "Error: op_close_agent()");
304}
v4.10.11
  1#include <sys/types.h>
  2#include <stdio.h>
  3#include <string.h>
  4#include <stdlib.h>
  5#include <err.h>
  6#include <jvmti.h>
  7#include <jvmticmlr.h>
  8#include <limits.h>
  9
 10#include "jvmti_agent.h"
 11
 12static int has_line_numbers;
 13void *jvmti_agent;
 14
 15static void print_error(jvmtiEnv *jvmti, const char *msg, jvmtiError ret)
 16{
 17	char *err_msg = NULL;
 18	jvmtiError err;
 19	err = (*jvmti)->GetErrorName(jvmti, ret, &err_msg);
 20	if (err == JVMTI_ERROR_NONE) {
 21		warnx("%s failed with %s", msg, err_msg);
 22		(*jvmti)->Deallocate(jvmti, (unsigned char *)err_msg);
 23	} else {
 24		warnx("%s failed with an unknown error %d", msg, ret);
 25	}
 26}
 27
 28static jvmtiError
 29do_get_line_numbers(jvmtiEnv *jvmti, void *pc, jmethodID m, jint bci,
 30		    jvmti_line_info_t *tab, jint *nr)
 31{
 32	jint i, lines = 0;
 33	jint nr_lines = 0;
 34	jvmtiLineNumberEntry *loc_tab = NULL;
 35	jvmtiError ret;
 36
 37	ret = (*jvmti)->GetLineNumberTable(jvmti, m, &nr_lines, &loc_tab);
 38	if (ret != JVMTI_ERROR_NONE) {
 39		print_error(jvmti, "GetLineNumberTable", ret);
 40		return ret;
 41	}
 42
 43	for (i = 0; i < nr_lines; i++) {
 44		if (loc_tab[i].start_location < bci) {
 45			tab[lines].pc = (unsigned long)pc;
 46			tab[lines].line_number = loc_tab[i].line_number;
 47			tab[lines].discrim = 0; /* not yet used */
 48			lines++;
 49		} else {
 50			break;
 51		}
 52	}
 53	(*jvmti)->Deallocate(jvmti, (unsigned char *)loc_tab);
 54	*nr = lines;
 55	return JVMTI_ERROR_NONE;
 56}
 57
 58static jvmtiError
 59get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **tab, int *nr_lines)
 60{
 61	const jvmtiCompiledMethodLoadRecordHeader *hdr;
 62	jvmtiCompiledMethodLoadInlineRecord *rec;
 63	jvmtiLineNumberEntry *lne = NULL;
 64	PCStackInfo *c;
 65	jint nr, ret;
 66	int nr_total = 0;
 67	int i, lines_total = 0;
 68
 69	if (!(tab && nr_lines))
 70		return JVMTI_ERROR_NULL_POINTER;
 71
 72	/*
 73	 * Phase 1 -- get the number of lines necessary
 74	 */
 75	for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
 76		if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
 77			rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
 78			for (i = 0; i < rec->numpcs; i++) {
 79				c = rec->pcinfo + i;
 80				nr = 0;
 81				/*
 82				 * unfortunately, need a tab to get the number of lines!
 83				 */
 84				ret = (*jvmti)->GetLineNumberTable(jvmti, c->methods[0], &nr, &lne);
 85				if (ret == JVMTI_ERROR_NONE) {
 86					/* free what was allocated for nothing */
 87					(*jvmti)->Deallocate(jvmti, (unsigned char *)lne);
 88					nr_total += (int)nr;
 89				} else {
 90					print_error(jvmti, "GetLineNumberTable", ret);
 91				}
 92			}
 93		}
 94	}
 95
 96	if (nr_total == 0)
 97		return JVMTI_ERROR_NOT_FOUND;
 98
 99	/*
100	 * Phase 2 -- allocate big enough line table
101	 */
102	*tab = malloc(nr_total * sizeof(**tab));
103	if (!*tab)
104		return JVMTI_ERROR_OUT_OF_MEMORY;
105
106	for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
107		if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
108			rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
109			for (i = 0; i < rec->numpcs; i++) {
110				c = rec->pcinfo + i;
111				nr = 0;
112				ret = do_get_line_numbers(jvmti, c->pc,
113							  c->methods[0],
114							  c->bcis[0],
115							  *tab + lines_total,
116							  &nr);
117				if (ret == JVMTI_ERROR_NONE)
118					lines_total += nr;
119			}
120		}
121	}
122	*nr_lines = lines_total;
123	return JVMTI_ERROR_NONE;
124}
125
126static void JNICALL
127compiled_method_load_cb(jvmtiEnv *jvmti,
128			jmethodID method,
129			jint code_size,
130			void const *code_addr,
131			jint map_length,
132			jvmtiAddrLocationMap const *map,
133			const void *compile_info)
134{
135	jvmti_line_info_t *line_tab = NULL;
136	jclass decl_class;
137	char *class_sign = NULL;
138	char *func_name = NULL;
139	char *func_sign = NULL;
140	char *file_name= NULL;
141	char fn[PATH_MAX];
142	uint64_t addr = (uint64_t)(uintptr_t)code_addr;
143	jvmtiError ret;
144	int nr_lines = 0; /* in line_tab[] */
145	size_t len;
146
147	ret = (*jvmti)->GetMethodDeclaringClass(jvmti, method,
148						&decl_class);
149	if (ret != JVMTI_ERROR_NONE) {
150		print_error(jvmti, "GetMethodDeclaringClass", ret);
151		return;
152	}
153
154	if (has_line_numbers && map && map_length) {
155		ret = get_line_numbers(jvmti, compile_info, &line_tab, &nr_lines);
156		if (ret != JVMTI_ERROR_NONE) {
157			warnx("jvmti: cannot get line table for method");
158			nr_lines = 0;
159		}
160	}
161
162	ret = (*jvmti)->GetSourceFileName(jvmti, decl_class, &file_name);
163	if (ret != JVMTI_ERROR_NONE) {
164		print_error(jvmti, "GetSourceFileName", ret);
165		goto error;
166	}
167
168	ret = (*jvmti)->GetClassSignature(jvmti, decl_class,
169					  &class_sign, NULL);
170	if (ret != JVMTI_ERROR_NONE) {
171		print_error(jvmti, "GetClassSignature", ret);
172		goto error;
173	}
174
175	ret = (*jvmti)->GetMethodName(jvmti, method, &func_name,
176				      &func_sign, NULL);
177	if (ret != JVMTI_ERROR_NONE) {
178		print_error(jvmti, "GetMethodName", ret);
179		goto error;
180	}
181
182	/*
183	 * Assume path name is class hierarchy, this is a common practice with Java programs
184	 */
185	if (*class_sign == 'L') {
186		int j, i = 0;
187		char *p = strrchr(class_sign, '/');
188		if (p) {
189			/* drop the 'L' prefix and copy up to the final '/' */
190			for (i = 0; i < (p - class_sign); i++)
191				fn[i] = class_sign[i+1];
192		}
193		/*
194		 * append file name, we use loops and not string ops to avoid modifying
195		 * class_sign which is used later for the symbol name
196		 */
197		for (j = 0; i < (PATH_MAX - 1) && file_name && j < strlen(file_name); j++, i++)
198			fn[i] = file_name[j];
199		fn[i] = '\0';
200	} else {
201		/* fallback case */
202		strcpy(fn, file_name);
203	}
204	/*
205	 * write source line info record if we have it
206	 */
207	if (jvmti_write_debug_info(jvmti_agent, addr, fn, line_tab, nr_lines))
208		warnx("jvmti: write_debug_info() failed");
209
210	len = strlen(func_name) + strlen(class_sign) + strlen(func_sign) + 2;
211	{
212		char str[len];
213		snprintf(str, len, "%s%s%s", class_sign, func_name, func_sign);
214
215		if (jvmti_write_code(jvmti_agent, str, addr, code_addr, code_size))
216			warnx("jvmti: write_code() failed");
217	}
218error:
219	(*jvmti)->Deallocate(jvmti, (unsigned char *)func_name);
220	(*jvmti)->Deallocate(jvmti, (unsigned char *)func_sign);
221	(*jvmti)->Deallocate(jvmti, (unsigned char *)class_sign);
222	(*jvmti)->Deallocate(jvmti, (unsigned char *)file_name);
223	free(line_tab);
224}
225
226static void JNICALL
227code_generated_cb(jvmtiEnv *jvmti,
228		  char const *name,
229		  void const *code_addr,
230		  jint code_size)
231{
232	uint64_t addr = (uint64_t)(unsigned long)code_addr;
233	int ret;
234
235	ret = jvmti_write_code(jvmti_agent, name, addr, code_addr, code_size);
236	if (ret)
237		warnx("jvmti: write_code() failed for code_generated");
238}
239
240JNIEXPORT jint JNICALL
241Agent_OnLoad(JavaVM *jvm, char *options, void *reserved __unused)
242{
243	jvmtiEventCallbacks cb;
244	jvmtiCapabilities caps1;
245	jvmtiJlocationFormat format;
246	jvmtiEnv *jvmti = NULL;
247	jint ret;
248
249	jvmti_agent = jvmti_open();
250	if (!jvmti_agent) {
251		warnx("jvmti: open_agent failed");
252		return -1;
253	}
254
255	/*
256	 * Request a JVMTI interface version 1 environment
257	 */
258	ret = (*jvm)->GetEnv(jvm, (void *)&jvmti, JVMTI_VERSION_1);
259	if (ret != JNI_OK) {
260		warnx("jvmti: jvmti version 1 not supported");
261		return -1;
262	}
263
264	/*
265	 * acquire method_load capability, we require it
266	 * request line numbers (optional)
267	 */
268	memset(&caps1, 0, sizeof(caps1));
269	caps1.can_generate_compiled_method_load_events = 1;
270
271	ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
272	if (ret != JVMTI_ERROR_NONE) {
273		print_error(jvmti, "AddCapabilities", ret);
274		return -1;
275	}
276	ret = (*jvmti)->GetJLocationFormat(jvmti, &format);
277        if (ret == JVMTI_ERROR_NONE && format == JVMTI_JLOCATION_JVMBCI) {
278                memset(&caps1, 0, sizeof(caps1));
279                caps1.can_get_line_numbers = 1;
280                caps1.can_get_source_file_name = 1;
281		ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
282                if (ret == JVMTI_ERROR_NONE)
283                        has_line_numbers = 1;
284        } else if (ret != JVMTI_ERROR_NONE)
285		print_error(jvmti, "GetJLocationFormat", ret);
286
287
288	memset(&cb, 0, sizeof(cb));
289
290	cb.CompiledMethodLoad   = compiled_method_load_cb;
291	cb.DynamicCodeGenerated = code_generated_cb;
292
293	ret = (*jvmti)->SetEventCallbacks(jvmti, &cb, sizeof(cb));
294	if (ret != JVMTI_ERROR_NONE) {
295		print_error(jvmti, "SetEventCallbacks", ret);
296		return -1;
297	}
298
299	ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
300			JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
301	if (ret != JVMTI_ERROR_NONE) {
302		print_error(jvmti, "SetEventNotificationMode(METHOD_LOAD)", ret);
303		return -1;
304	}
305
306	ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
307			JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
308	if (ret != JVMTI_ERROR_NONE) {
309		print_error(jvmti, "SetEventNotificationMode(CODE_GENERATED)", ret);
310		return -1;
311	}
312	return 0;
313}
314
315JNIEXPORT void JNICALL
316Agent_OnUnload(JavaVM *jvm __unused)
317{
318	int ret;
319
320	ret = jvmti_close(jvmti_agent);
321	if (ret)
322		errx(1, "Error: op_close_agent()");
323}