Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2#include <linux/compiler.h>
  3#include <linux/string.h>
  4#include <sys/types.h>
  5#include <stdio.h>
  6#include <string.h>
  7#include <stdlib.h>
  8#include <err.h>
  9#include <jvmti.h>
 10#ifdef HAVE_JVMTI_CMLR
 11#include <jvmticmlr.h>
 12#endif
 13#include <limits.h>
 14
 15#include "jvmti_agent.h"
 16
 17static int has_line_numbers;
 18void *jvmti_agent;
 19
 20static void print_error(jvmtiEnv *jvmti, const char *msg, jvmtiError ret)
 21{
 22	char *err_msg = NULL;
 23	jvmtiError err;
 24	err = (*jvmti)->GetErrorName(jvmti, ret, &err_msg);
 25	if (err == JVMTI_ERROR_NONE) {
 26		warnx("%s failed with %s", msg, err_msg);
 27		(*jvmti)->Deallocate(jvmti, (unsigned char *)err_msg);
 28	} else {
 29		warnx("%s failed with an unknown error %d", msg, ret);
 30	}
 31}
 32
 33#ifdef HAVE_JVMTI_CMLR
 34static jvmtiError
 35do_get_line_numbers(jvmtiEnv *jvmti, void *pc, jmethodID m, jint bci,
 36		    jvmti_line_info_t *tab, jint *nr)
 37{
 38	jint i, lines = 0;
 39	jint nr_lines = 0;
 40	jvmtiLineNumberEntry *loc_tab = NULL;
 41	jvmtiError ret;
 42
 43	ret = (*jvmti)->GetLineNumberTable(jvmti, m, &nr_lines, &loc_tab);
 44	if (ret != JVMTI_ERROR_NONE) {
 45		print_error(jvmti, "GetLineNumberTable", ret);
 46		return ret;
 47	}
 48
 49	for (i = 0; i < nr_lines; i++) {
 50		if (loc_tab[i].start_location < bci) {
 51			tab[lines].pc = (unsigned long)pc;
 52			tab[lines].line_number = loc_tab[i].line_number;
 53			tab[lines].discrim = 0; /* not yet used */
 54			tab[lines].methodID = m;
 55			lines++;
 56		} else {
 57			break;
 58		}
 59	}
 60	(*jvmti)->Deallocate(jvmti, (unsigned char *)loc_tab);
 61	*nr = lines;
 62	return JVMTI_ERROR_NONE;
 63}
 64
 65static jvmtiError
 66get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **tab, int *nr_lines)
 67{
 68	const jvmtiCompiledMethodLoadRecordHeader *hdr;
 69	jvmtiCompiledMethodLoadInlineRecord *rec;
 70	jvmtiLineNumberEntry *lne = NULL;
 71	PCStackInfo *c;
 72	jint nr, ret;
 73	int nr_total = 0;
 74	int i, lines_total = 0;
 75
 76	if (!(tab && nr_lines))
 77		return JVMTI_ERROR_NULL_POINTER;
 78
 79	/*
 80	 * Phase 1 -- get the number of lines necessary
 81	 */
 82	for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
 83		if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
 84			rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
 85			for (i = 0; i < rec->numpcs; i++) {
 86				c = rec->pcinfo + i;
 87				nr = 0;
 88				/*
 89				 * unfortunately, need a tab to get the number of lines!
 90				 */
 91				ret = (*jvmti)->GetLineNumberTable(jvmti, c->methods[0], &nr, &lne);
 92				if (ret == JVMTI_ERROR_NONE) {
 93					/* free what was allocated for nothing */
 94					(*jvmti)->Deallocate(jvmti, (unsigned char *)lne);
 95					nr_total += (int)nr;
 96				} else {
 97					print_error(jvmti, "GetLineNumberTable", ret);
 98				}
 99			}
100		}
101	}
102
103	if (nr_total == 0)
104		return JVMTI_ERROR_NOT_FOUND;
105
106	/*
107	 * Phase 2 -- allocate big enough line table
108	 */
109	*tab = malloc(nr_total * sizeof(**tab));
110	if (!*tab)
111		return JVMTI_ERROR_OUT_OF_MEMORY;
112
113	for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
114		if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
115			rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
116			for (i = 0; i < rec->numpcs; i++) {
117				c = rec->pcinfo + i;
118				nr = 0;
119				ret = do_get_line_numbers(jvmti, c->pc,
120							  c->methods[0],
121							  c->bcis[0],
122							  *tab + lines_total,
123							  &nr);
124				if (ret == JVMTI_ERROR_NONE)
125					lines_total += nr;
126			}
127		}
128	}
129	*nr_lines = lines_total;
130	return JVMTI_ERROR_NONE;
131}
132#else /* HAVE_JVMTI_CMLR */
133
134static jvmtiError
135get_line_numbers(jvmtiEnv *jvmti __maybe_unused, const void *compile_info __maybe_unused,
136		 jvmti_line_info_t **tab __maybe_unused, int *nr_lines __maybe_unused)
137{
138	return JVMTI_ERROR_NONE;
139}
140#endif /* HAVE_JVMTI_CMLR */
141
142static void
143copy_class_filename(const char * class_sign, const char * file_name, char * result, size_t max_length)
144{
145	/*
146	* Assume path name is class hierarchy, this is a common practice with Java programs
147	*/
148	if (*class_sign == 'L') {
149		int j, i = 0;
150		char *p = strrchr(class_sign, '/');
151		if (p) {
152			/* drop the 'L' prefix and copy up to the final '/' */
153			for (i = 0; i < (p - class_sign); i++)
154				result[i] = class_sign[i+1];
155		}
156		/*
157		* append file name, we use loops and not string ops to avoid modifying
158		* class_sign which is used later for the symbol name
159		*/
160		for (j = 0; i < (max_length - 1) && file_name && j < strlen(file_name); j++, i++)
161			result[i] = file_name[j];
162
163		result[i] = '\0';
164	} else {
165		/* fallback case */
166		strlcpy(result, file_name, max_length);
 
167	}
168}
169
170static jvmtiError
171get_source_filename(jvmtiEnv *jvmti, jmethodID methodID, char ** buffer)
172{
173	jvmtiError ret;
174	jclass decl_class;
175	char *file_name = NULL;
176	char *class_sign = NULL;
177	char fn[PATH_MAX];
178	size_t len;
179
180	ret = (*jvmti)->GetMethodDeclaringClass(jvmti, methodID, &decl_class);
181	if (ret != JVMTI_ERROR_NONE) {
182		print_error(jvmti, "GetMethodDeclaringClass", ret);
183		return ret;
184	}
185
186	ret = (*jvmti)->GetSourceFileName(jvmti, decl_class, &file_name);
187	if (ret != JVMTI_ERROR_NONE) {
188		print_error(jvmti, "GetSourceFileName", ret);
189		return ret;
190	}
191
192	ret = (*jvmti)->GetClassSignature(jvmti, decl_class, &class_sign, NULL);
193	if (ret != JVMTI_ERROR_NONE) {
194		print_error(jvmti, "GetClassSignature", ret);
195		goto free_file_name_error;
196	}
197
198	copy_class_filename(class_sign, file_name, fn, PATH_MAX);
199	len = strlen(fn);
200	*buffer = malloc((len + 1) * sizeof(char));
201	if (!*buffer) {
202		print_error(jvmti, "GetClassSignature", ret);
203		ret = JVMTI_ERROR_OUT_OF_MEMORY;
204		goto free_class_sign_error;
205	}
206	strcpy(*buffer, fn);
207	ret = JVMTI_ERROR_NONE;
208
209free_class_sign_error:
210	(*jvmti)->Deallocate(jvmti, (unsigned char *)class_sign);
211free_file_name_error:
212	(*jvmti)->Deallocate(jvmti, (unsigned char *)file_name);
213
214	return ret;
215}
216
217static jvmtiError
218fill_source_filenames(jvmtiEnv *jvmti, int nr_lines,
219		      const jvmti_line_info_t * line_tab,
220		      char ** file_names)
221{
222	int index;
223	jvmtiError ret;
224
225	for (index = 0; index < nr_lines; ++index) {
226		ret = get_source_filename(jvmti, line_tab[index].methodID, &(file_names[index]));
227		if (ret != JVMTI_ERROR_NONE)
228			return ret;
229	}
230
231	return JVMTI_ERROR_NONE;
232}
233
234static void JNICALL
235compiled_method_load_cb(jvmtiEnv *jvmti,
236			jmethodID method,
237			jint code_size,
238			void const *code_addr,
239			jint map_length,
240			jvmtiAddrLocationMap const *map,
241			const void *compile_info)
242{
243	jvmti_line_info_t *line_tab = NULL;
244	char ** line_file_names = NULL;
245	jclass decl_class;
246	char *class_sign = NULL;
247	char *func_name = NULL;
248	char *func_sign = NULL;
249	char *file_name = NULL;
250	char fn[PATH_MAX];
251	uint64_t addr = (uint64_t)(uintptr_t)code_addr;
252	jvmtiError ret;
253	int nr_lines = 0; /* in line_tab[] */
254	size_t len;
255	int output_debug_info = 0;
256
257	ret = (*jvmti)->GetMethodDeclaringClass(jvmti, method,
258						&decl_class);
259	if (ret != JVMTI_ERROR_NONE) {
260		print_error(jvmti, "GetMethodDeclaringClass", ret);
261		return;
262	}
263
264	if (has_line_numbers && map && map_length) {
265		ret = get_line_numbers(jvmti, compile_info, &line_tab, &nr_lines);
266		if (ret != JVMTI_ERROR_NONE) {
267			warnx("jvmti: cannot get line table for method");
268			nr_lines = 0;
269		} else if (nr_lines > 0) {
270			line_file_names = malloc(sizeof(char*) * nr_lines);
271			if (!line_file_names) {
272				warnx("jvmti: cannot allocate space for line table method names");
273			} else {
274				memset(line_file_names, 0, sizeof(char*) * nr_lines);
275				ret = fill_source_filenames(jvmti, nr_lines, line_tab, line_file_names);
276				if (ret != JVMTI_ERROR_NONE) {
277					warnx("jvmti: fill_source_filenames failed");
278				} else {
279					output_debug_info = 1;
280				}
281			}
282		}
283	}
284
285	ret = (*jvmti)->GetSourceFileName(jvmti, decl_class, &file_name);
286	if (ret != JVMTI_ERROR_NONE) {
287		print_error(jvmti, "GetSourceFileName", ret);
288		goto error;
289	}
290
291	ret = (*jvmti)->GetClassSignature(jvmti, decl_class,
292					  &class_sign, NULL);
293	if (ret != JVMTI_ERROR_NONE) {
294		print_error(jvmti, "GetClassSignature", ret);
295		goto error;
296	}
297
298	ret = (*jvmti)->GetMethodName(jvmti, method, &func_name,
299				      &func_sign, NULL);
300	if (ret != JVMTI_ERROR_NONE) {
301		print_error(jvmti, "GetMethodName", ret);
302		goto error;
303	}
304
305	copy_class_filename(class_sign, file_name, fn, PATH_MAX);
306
307	/*
308	 * write source line info record if we have it
309	 */
310	if (output_debug_info)
311		if (jvmti_write_debug_info(jvmti_agent, addr, nr_lines, line_tab, (const char * const *) line_file_names))
312			warnx("jvmti: write_debug_info() failed");
313
314	len = strlen(func_name) + strlen(class_sign) + strlen(func_sign) + 2;
315	{
316		char str[len];
317		snprintf(str, len, "%s%s%s", class_sign, func_name, func_sign);
318
319		if (jvmti_write_code(jvmti_agent, str, addr, code_addr, code_size))
320			warnx("jvmti: write_code() failed");
321	}
322error:
323	(*jvmti)->Deallocate(jvmti, (unsigned char *)func_name);
324	(*jvmti)->Deallocate(jvmti, (unsigned char *)func_sign);
325	(*jvmti)->Deallocate(jvmti, (unsigned char *)class_sign);
326	(*jvmti)->Deallocate(jvmti, (unsigned char *)file_name);
327	free(line_tab);
328	while (line_file_names && (nr_lines > 0)) {
329	    if (line_file_names[nr_lines - 1]) {
330	        free(line_file_names[nr_lines - 1]);
331	    }
332	    nr_lines -= 1;
333	}
334	free(line_file_names);
335}
336
337static void JNICALL
338code_generated_cb(jvmtiEnv *jvmti,
339		  char const *name,
340		  void const *code_addr,
341		  jint code_size)
342{
343	uint64_t addr = (uint64_t)(unsigned long)code_addr;
344	int ret;
345
346	ret = jvmti_write_code(jvmti_agent, name, addr, code_addr, code_size);
347	if (ret)
348		warnx("jvmti: write_code() failed for code_generated");
349}
350
351JNIEXPORT jint JNICALL
352Agent_OnLoad(JavaVM *jvm, char *options, void *reserved __maybe_unused)
353{
354	jvmtiEventCallbacks cb;
355	jvmtiCapabilities caps1;
356	jvmtiJlocationFormat format;
357	jvmtiEnv *jvmti = NULL;
358	jint ret;
359
360	jvmti_agent = jvmti_open();
361	if (!jvmti_agent) {
362		warnx("jvmti: open_agent failed");
363		return -1;
364	}
365
366	/*
367	 * Request a JVMTI interface version 1 environment
368	 */
369	ret = (*jvm)->GetEnv(jvm, (void *)&jvmti, JVMTI_VERSION_1);
370	if (ret != JNI_OK) {
371		warnx("jvmti: jvmti version 1 not supported");
372		return -1;
373	}
374
375	/*
376	 * acquire method_load capability, we require it
377	 * request line numbers (optional)
378	 */
379	memset(&caps1, 0, sizeof(caps1));
380	caps1.can_generate_compiled_method_load_events = 1;
381
382	ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
383	if (ret != JVMTI_ERROR_NONE) {
384		print_error(jvmti, "AddCapabilities", ret);
385		return -1;
386	}
387	ret = (*jvmti)->GetJLocationFormat(jvmti, &format);
388        if (ret == JVMTI_ERROR_NONE && format == JVMTI_JLOCATION_JVMBCI) {
389                memset(&caps1, 0, sizeof(caps1));
390                caps1.can_get_line_numbers = 1;
391                caps1.can_get_source_file_name = 1;
392		ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
393                if (ret == JVMTI_ERROR_NONE)
394                        has_line_numbers = 1;
395        } else if (ret != JVMTI_ERROR_NONE)
396		print_error(jvmti, "GetJLocationFormat", ret);
397
398
399	memset(&cb, 0, sizeof(cb));
400
401	cb.CompiledMethodLoad   = compiled_method_load_cb;
402	cb.DynamicCodeGenerated = code_generated_cb;
403
404	ret = (*jvmti)->SetEventCallbacks(jvmti, &cb, sizeof(cb));
405	if (ret != JVMTI_ERROR_NONE) {
406		print_error(jvmti, "SetEventCallbacks", ret);
407		return -1;
408	}
409
410	ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
411			JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
412	if (ret != JVMTI_ERROR_NONE) {
413		print_error(jvmti, "SetEventNotificationMode(METHOD_LOAD)", ret);
414		return -1;
415	}
416
417	ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
418			JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
419	if (ret != JVMTI_ERROR_NONE) {
420		print_error(jvmti, "SetEventNotificationMode(CODE_GENERATED)", ret);
421		return -1;
422	}
423	return 0;
424}
425
426JNIEXPORT void JNICALL
427Agent_OnUnload(JavaVM *jvm __maybe_unused)
428{
429	int ret;
430
431	ret = jvmti_close(jvmti_agent);
432	if (ret)
433		errx(1, "Error: op_close_agent()");
434}
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2#include <linux/compiler.h>
 
  3#include <sys/types.h>
  4#include <stdio.h>
  5#include <string.h>
  6#include <stdlib.h>
  7#include <err.h>
  8#include <jvmti.h>
 
  9#include <jvmticmlr.h>
 
 10#include <limits.h>
 11
 12#include "jvmti_agent.h"
 13
 14static int has_line_numbers;
 15void *jvmti_agent;
 16
 17static void print_error(jvmtiEnv *jvmti, const char *msg, jvmtiError ret)
 18{
 19	char *err_msg = NULL;
 20	jvmtiError err;
 21	err = (*jvmti)->GetErrorName(jvmti, ret, &err_msg);
 22	if (err == JVMTI_ERROR_NONE) {
 23		warnx("%s failed with %s", msg, err_msg);
 24		(*jvmti)->Deallocate(jvmti, (unsigned char *)err_msg);
 25	} else {
 26		warnx("%s failed with an unknown error %d", msg, ret);
 27	}
 28}
 29
 
 30static jvmtiError
 31do_get_line_numbers(jvmtiEnv *jvmti, void *pc, jmethodID m, jint bci,
 32		    jvmti_line_info_t *tab, jint *nr)
 33{
 34	jint i, lines = 0;
 35	jint nr_lines = 0;
 36	jvmtiLineNumberEntry *loc_tab = NULL;
 37	jvmtiError ret;
 38
 39	ret = (*jvmti)->GetLineNumberTable(jvmti, m, &nr_lines, &loc_tab);
 40	if (ret != JVMTI_ERROR_NONE) {
 41		print_error(jvmti, "GetLineNumberTable", ret);
 42		return ret;
 43	}
 44
 45	for (i = 0; i < nr_lines; i++) {
 46		if (loc_tab[i].start_location < bci) {
 47			tab[lines].pc = (unsigned long)pc;
 48			tab[lines].line_number = loc_tab[i].line_number;
 49			tab[lines].discrim = 0; /* not yet used */
 50			tab[lines].methodID = m;
 51			lines++;
 52		} else {
 53			break;
 54		}
 55	}
 56	(*jvmti)->Deallocate(jvmti, (unsigned char *)loc_tab);
 57	*nr = lines;
 58	return JVMTI_ERROR_NONE;
 59}
 60
 61static jvmtiError
 62get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **tab, int *nr_lines)
 63{
 64	const jvmtiCompiledMethodLoadRecordHeader *hdr;
 65	jvmtiCompiledMethodLoadInlineRecord *rec;
 66	jvmtiLineNumberEntry *lne = NULL;
 67	PCStackInfo *c;
 68	jint nr, ret;
 69	int nr_total = 0;
 70	int i, lines_total = 0;
 71
 72	if (!(tab && nr_lines))
 73		return JVMTI_ERROR_NULL_POINTER;
 74
 75	/*
 76	 * Phase 1 -- get the number of lines necessary
 77	 */
 78	for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
 79		if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
 80			rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
 81			for (i = 0; i < rec->numpcs; i++) {
 82				c = rec->pcinfo + i;
 83				nr = 0;
 84				/*
 85				 * unfortunately, need a tab to get the number of lines!
 86				 */
 87				ret = (*jvmti)->GetLineNumberTable(jvmti, c->methods[0], &nr, &lne);
 88				if (ret == JVMTI_ERROR_NONE) {
 89					/* free what was allocated for nothing */
 90					(*jvmti)->Deallocate(jvmti, (unsigned char *)lne);
 91					nr_total += (int)nr;
 92				} else {
 93					print_error(jvmti, "GetLineNumberTable", ret);
 94				}
 95			}
 96		}
 97	}
 98
 99	if (nr_total == 0)
100		return JVMTI_ERROR_NOT_FOUND;
101
102	/*
103	 * Phase 2 -- allocate big enough line table
104	 */
105	*tab = malloc(nr_total * sizeof(**tab));
106	if (!*tab)
107		return JVMTI_ERROR_OUT_OF_MEMORY;
108
109	for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
110		if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
111			rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
112			for (i = 0; i < rec->numpcs; i++) {
113				c = rec->pcinfo + i;
114				nr = 0;
115				ret = do_get_line_numbers(jvmti, c->pc,
116							  c->methods[0],
117							  c->bcis[0],
118							  *tab + lines_total,
119							  &nr);
120				if (ret == JVMTI_ERROR_NONE)
121					lines_total += nr;
122			}
123		}
124	}
125	*nr_lines = lines_total;
126	return JVMTI_ERROR_NONE;
127}
 
 
 
 
 
 
 
 
 
128
129static void
130copy_class_filename(const char * class_sign, const char * file_name, char * result, size_t max_length)
131{
132	/*
133	* Assume path name is class hierarchy, this is a common practice with Java programs
134	*/
135	if (*class_sign == 'L') {
136		int j, i = 0;
137		char *p = strrchr(class_sign, '/');
138		if (p) {
139			/* drop the 'L' prefix and copy up to the final '/' */
140			for (i = 0; i < (p - class_sign); i++)
141				result[i] = class_sign[i+1];
142		}
143		/*
144		* append file name, we use loops and not string ops to avoid modifying
145		* class_sign which is used later for the symbol name
146		*/
147		for (j = 0; i < (max_length - 1) && file_name && j < strlen(file_name); j++, i++)
148			result[i] = file_name[j];
149
150		result[i] = '\0';
151	} else {
152		/* fallback case */
153		size_t file_name_len = strlen(file_name);
154		strncpy(result, file_name, file_name_len < max_length ? file_name_len : max_length);
155	}
156}
157
158static jvmtiError
159get_source_filename(jvmtiEnv *jvmti, jmethodID methodID, char ** buffer)
160{
161	jvmtiError ret;
162	jclass decl_class;
163	char *file_name = NULL;
164	char *class_sign = NULL;
165	char fn[PATH_MAX];
166	size_t len;
167
168	ret = (*jvmti)->GetMethodDeclaringClass(jvmti, methodID, &decl_class);
169	if (ret != JVMTI_ERROR_NONE) {
170		print_error(jvmti, "GetMethodDeclaringClass", ret);
171		return ret;
172	}
173
174	ret = (*jvmti)->GetSourceFileName(jvmti, decl_class, &file_name);
175	if (ret != JVMTI_ERROR_NONE) {
176		print_error(jvmti, "GetSourceFileName", ret);
177		return ret;
178	}
179
180	ret = (*jvmti)->GetClassSignature(jvmti, decl_class, &class_sign, NULL);
181	if (ret != JVMTI_ERROR_NONE) {
182		print_error(jvmti, "GetClassSignature", ret);
183		goto free_file_name_error;
184	}
185
186	copy_class_filename(class_sign, file_name, fn, PATH_MAX);
187	len = strlen(fn);
188	*buffer = malloc((len + 1) * sizeof(char));
189	if (!*buffer) {
190		print_error(jvmti, "GetClassSignature", ret);
191		ret = JVMTI_ERROR_OUT_OF_MEMORY;
192		goto free_class_sign_error;
193	}
194	strcpy(*buffer, fn);
195	ret = JVMTI_ERROR_NONE;
196
197free_class_sign_error:
198	(*jvmti)->Deallocate(jvmti, (unsigned char *)class_sign);
199free_file_name_error:
200	(*jvmti)->Deallocate(jvmti, (unsigned char *)file_name);
201
202	return ret;
203}
204
205static jvmtiError
206fill_source_filenames(jvmtiEnv *jvmti, int nr_lines,
207		      const jvmti_line_info_t * line_tab,
208		      char ** file_names)
209{
210	int index;
211	jvmtiError ret;
212
213	for (index = 0; index < nr_lines; ++index) {
214		ret = get_source_filename(jvmti, line_tab[index].methodID, &(file_names[index]));
215		if (ret != JVMTI_ERROR_NONE)
216			return ret;
217	}
218
219	return JVMTI_ERROR_NONE;
220}
221
222static void JNICALL
223compiled_method_load_cb(jvmtiEnv *jvmti,
224			jmethodID method,
225			jint code_size,
226			void const *code_addr,
227			jint map_length,
228			jvmtiAddrLocationMap const *map,
229			const void *compile_info)
230{
231	jvmti_line_info_t *line_tab = NULL;
232	char ** line_file_names = NULL;
233	jclass decl_class;
234	char *class_sign = NULL;
235	char *func_name = NULL;
236	char *func_sign = NULL;
237	char *file_name = NULL;
238	char fn[PATH_MAX];
239	uint64_t addr = (uint64_t)(uintptr_t)code_addr;
240	jvmtiError ret;
241	int nr_lines = 0; /* in line_tab[] */
242	size_t len;
243	int output_debug_info = 0;
244
245	ret = (*jvmti)->GetMethodDeclaringClass(jvmti, method,
246						&decl_class);
247	if (ret != JVMTI_ERROR_NONE) {
248		print_error(jvmti, "GetMethodDeclaringClass", ret);
249		return;
250	}
251
252	if (has_line_numbers && map && map_length) {
253		ret = get_line_numbers(jvmti, compile_info, &line_tab, &nr_lines);
254		if (ret != JVMTI_ERROR_NONE) {
255			warnx("jvmti: cannot get line table for method");
256			nr_lines = 0;
257		} else if (nr_lines > 0) {
258			line_file_names = malloc(sizeof(char*) * nr_lines);
259			if (!line_file_names) {
260				warnx("jvmti: cannot allocate space for line table method names");
261			} else {
262				memset(line_file_names, 0, sizeof(char*) * nr_lines);
263				ret = fill_source_filenames(jvmti, nr_lines, line_tab, line_file_names);
264				if (ret != JVMTI_ERROR_NONE) {
265					warnx("jvmti: fill_source_filenames failed");
266				} else {
267					output_debug_info = 1;
268				}
269			}
270		}
271	}
272
273	ret = (*jvmti)->GetSourceFileName(jvmti, decl_class, &file_name);
274	if (ret != JVMTI_ERROR_NONE) {
275		print_error(jvmti, "GetSourceFileName", ret);
276		goto error;
277	}
278
279	ret = (*jvmti)->GetClassSignature(jvmti, decl_class,
280					  &class_sign, NULL);
281	if (ret != JVMTI_ERROR_NONE) {
282		print_error(jvmti, "GetClassSignature", ret);
283		goto error;
284	}
285
286	ret = (*jvmti)->GetMethodName(jvmti, method, &func_name,
287				      &func_sign, NULL);
288	if (ret != JVMTI_ERROR_NONE) {
289		print_error(jvmti, "GetMethodName", ret);
290		goto error;
291	}
292
293	copy_class_filename(class_sign, file_name, fn, PATH_MAX);
294
295	/*
296	 * write source line info record if we have it
297	 */
298	if (output_debug_info)
299		if (jvmti_write_debug_info(jvmti_agent, addr, nr_lines, line_tab, (const char * const *) line_file_names))
300			warnx("jvmti: write_debug_info() failed");
301
302	len = strlen(func_name) + strlen(class_sign) + strlen(func_sign) + 2;
303	{
304		char str[len];
305		snprintf(str, len, "%s%s%s", class_sign, func_name, func_sign);
306
307		if (jvmti_write_code(jvmti_agent, str, addr, code_addr, code_size))
308			warnx("jvmti: write_code() failed");
309	}
310error:
311	(*jvmti)->Deallocate(jvmti, (unsigned char *)func_name);
312	(*jvmti)->Deallocate(jvmti, (unsigned char *)func_sign);
313	(*jvmti)->Deallocate(jvmti, (unsigned char *)class_sign);
314	(*jvmti)->Deallocate(jvmti, (unsigned char *)file_name);
315	free(line_tab);
316	while (line_file_names && (nr_lines > 0)) {
317	    if (line_file_names[nr_lines - 1]) {
318	        free(line_file_names[nr_lines - 1]);
319	    }
320	    nr_lines -= 1;
321	}
322	free(line_file_names);
323}
324
325static void JNICALL
326code_generated_cb(jvmtiEnv *jvmti,
327		  char const *name,
328		  void const *code_addr,
329		  jint code_size)
330{
331	uint64_t addr = (uint64_t)(unsigned long)code_addr;
332	int ret;
333
334	ret = jvmti_write_code(jvmti_agent, name, addr, code_addr, code_size);
335	if (ret)
336		warnx("jvmti: write_code() failed for code_generated");
337}
338
339JNIEXPORT jint JNICALL
340Agent_OnLoad(JavaVM *jvm, char *options, void *reserved __maybe_unused)
341{
342	jvmtiEventCallbacks cb;
343	jvmtiCapabilities caps1;
344	jvmtiJlocationFormat format;
345	jvmtiEnv *jvmti = NULL;
346	jint ret;
347
348	jvmti_agent = jvmti_open();
349	if (!jvmti_agent) {
350		warnx("jvmti: open_agent failed");
351		return -1;
352	}
353
354	/*
355	 * Request a JVMTI interface version 1 environment
356	 */
357	ret = (*jvm)->GetEnv(jvm, (void *)&jvmti, JVMTI_VERSION_1);
358	if (ret != JNI_OK) {
359		warnx("jvmti: jvmti version 1 not supported");
360		return -1;
361	}
362
363	/*
364	 * acquire method_load capability, we require it
365	 * request line numbers (optional)
366	 */
367	memset(&caps1, 0, sizeof(caps1));
368	caps1.can_generate_compiled_method_load_events = 1;
369
370	ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
371	if (ret != JVMTI_ERROR_NONE) {
372		print_error(jvmti, "AddCapabilities", ret);
373		return -1;
374	}
375	ret = (*jvmti)->GetJLocationFormat(jvmti, &format);
376        if (ret == JVMTI_ERROR_NONE && format == JVMTI_JLOCATION_JVMBCI) {
377                memset(&caps1, 0, sizeof(caps1));
378                caps1.can_get_line_numbers = 1;
379                caps1.can_get_source_file_name = 1;
380		ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
381                if (ret == JVMTI_ERROR_NONE)
382                        has_line_numbers = 1;
383        } else if (ret != JVMTI_ERROR_NONE)
384		print_error(jvmti, "GetJLocationFormat", ret);
385
386
387	memset(&cb, 0, sizeof(cb));
388
389	cb.CompiledMethodLoad   = compiled_method_load_cb;
390	cb.DynamicCodeGenerated = code_generated_cb;
391
392	ret = (*jvmti)->SetEventCallbacks(jvmti, &cb, sizeof(cb));
393	if (ret != JVMTI_ERROR_NONE) {
394		print_error(jvmti, "SetEventCallbacks", ret);
395		return -1;
396	}
397
398	ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
399			JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
400	if (ret != JVMTI_ERROR_NONE) {
401		print_error(jvmti, "SetEventNotificationMode(METHOD_LOAD)", ret);
402		return -1;
403	}
404
405	ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
406			JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
407	if (ret != JVMTI_ERROR_NONE) {
408		print_error(jvmti, "SetEventNotificationMode(CODE_GENERATED)", ret);
409		return -1;
410	}
411	return 0;
412}
413
414JNIEXPORT void JNICALL
415Agent_OnUnload(JavaVM *jvm __maybe_unused)
416{
417	int ret;
418
419	ret = jvmti_close(jvmti_agent);
420	if (ret)
421		errx(1, "Error: op_close_agent()");
422}