blob: aea7b1fe85aa8b83bc89332eba80c20e580dd982 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03002#include <linux/compiler.h>
Stephane Eranian209045a2015-11-30 10:02:22 +01003#include <sys/types.h>
4#include <stdio.h>
5#include <string.h>
6#include <stdlib.h>
7#include <err.h>
8#include <jvmti.h>
Jiri Olsadd1d0042018-11-21 16:43:41 +01009#ifdef HAVE_JVMTI_CMLR
Stephane Eranian598b7c62015-11-30 10:02:23 +010010#include <jvmticmlr.h>
Jiri Olsadd1d0042018-11-21 16:43:41 +010011#endif
Stephane Eranian209045a2015-11-30 10:02:22 +010012#include <limits.h>
13
14#include "jvmti_agent.h"
15
16static int has_line_numbers;
17void *jvmti_agent;
18
Stephane Eraniancdd75e32016-10-13 03:59:35 -070019static void print_error(jvmtiEnv *jvmti, const char *msg, jvmtiError ret)
20{
21 char *err_msg = NULL;
22 jvmtiError err;
23 err = (*jvmti)->GetErrorName(jvmti, ret, &err_msg);
24 if (err == JVMTI_ERROR_NONE) {
25 warnx("%s failed with %s", msg, err_msg);
26 (*jvmti)->Deallocate(jvmti, (unsigned char *)err_msg);
27 } else {
28 warnx("%s failed with an unknown error %d", msg, ret);
29 }
30}
31
Jiri Olsadd1d0042018-11-21 16:43:41 +010032#ifdef HAVE_JVMTI_CMLR
Stephane Eranian598b7c62015-11-30 10:02:23 +010033static jvmtiError
34do_get_line_numbers(jvmtiEnv *jvmti, void *pc, jmethodID m, jint bci,
35 jvmti_line_info_t *tab, jint *nr)
36{
37 jint i, lines = 0;
38 jint nr_lines = 0;
39 jvmtiLineNumberEntry *loc_tab = NULL;
40 jvmtiError ret;
41
42 ret = (*jvmti)->GetLineNumberTable(jvmti, m, &nr_lines, &loc_tab);
Stephane Eraniancdd75e32016-10-13 03:59:35 -070043 if (ret != JVMTI_ERROR_NONE) {
44 print_error(jvmti, "GetLineNumberTable", ret);
Stephane Eranian598b7c62015-11-30 10:02:23 +010045 return ret;
Stephane Eraniancdd75e32016-10-13 03:59:35 -070046 }
Stephane Eranian598b7c62015-11-30 10:02:23 +010047
48 for (i = 0; i < nr_lines; i++) {
49 if (loc_tab[i].start_location < bci) {
50 tab[lines].pc = (unsigned long)pc;
51 tab[lines].line_number = loc_tab[i].line_number;
52 tab[lines].discrim = 0; /* not yet used */
Ben Gaineyca58d7e2017-11-22 18:25:41 -060053 tab[lines].methodID = m;
Stephane Eranian598b7c62015-11-30 10:02:23 +010054 lines++;
55 } else {
56 break;
57 }
58 }
59 (*jvmti)->Deallocate(jvmti, (unsigned char *)loc_tab);
60 *nr = lines;
61 return JVMTI_ERROR_NONE;
62}
63
64static jvmtiError
65get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **tab, int *nr_lines)
66{
67 const jvmtiCompiledMethodLoadRecordHeader *hdr;
68 jvmtiCompiledMethodLoadInlineRecord *rec;
69 jvmtiLineNumberEntry *lne = NULL;
70 PCStackInfo *c;
71 jint nr, ret;
72 int nr_total = 0;
73 int i, lines_total = 0;
74
75 if (!(tab && nr_lines))
76 return JVMTI_ERROR_NULL_POINTER;
77
78 /*
79 * Phase 1 -- get the number of lines necessary
80 */
81 for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
82 if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
83 rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
84 for (i = 0; i < rec->numpcs; i++) {
85 c = rec->pcinfo + i;
86 nr = 0;
87 /*
88 * unfortunately, need a tab to get the number of lines!
89 */
90 ret = (*jvmti)->GetLineNumberTable(jvmti, c->methods[0], &nr, &lne);
91 if (ret == JVMTI_ERROR_NONE) {
92 /* free what was allocated for nothing */
93 (*jvmti)->Deallocate(jvmti, (unsigned char *)lne);
94 nr_total += (int)nr;
Stephane Eraniancdd75e32016-10-13 03:59:35 -070095 } else {
96 print_error(jvmti, "GetLineNumberTable", ret);
Stephane Eranian598b7c62015-11-30 10:02:23 +010097 }
98 }
99 }
100 }
101
102 if (nr_total == 0)
103 return JVMTI_ERROR_NOT_FOUND;
104
105 /*
106 * Phase 2 -- allocate big enough line table
107 */
108 *tab = malloc(nr_total * sizeof(**tab));
109 if (!*tab)
110 return JVMTI_ERROR_OUT_OF_MEMORY;
111
112 for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
113 if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
114 rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
115 for (i = 0; i < rec->numpcs; i++) {
116 c = rec->pcinfo + i;
117 nr = 0;
118 ret = do_get_line_numbers(jvmti, c->pc,
119 c->methods[0],
120 c->bcis[0],
121 *tab + lines_total,
122 &nr);
123 if (ret == JVMTI_ERROR_NONE)
124 lines_total += nr;
125 }
126 }
127 }
128 *nr_lines = lines_total;
129 return JVMTI_ERROR_NONE;
130}
Jiri Olsadd1d0042018-11-21 16:43:41 +0100131#else /* HAVE_JVMTI_CMLR */
132
133static jvmtiError
134get_line_numbers(jvmtiEnv *jvmti __maybe_unused, const void *compile_info __maybe_unused,
135 jvmti_line_info_t **tab __maybe_unused, int *nr_lines __maybe_unused)
136{
137 return JVMTI_ERROR_NONE;
138}
139#endif /* HAVE_JVMTI_CMLR */
Stephane Eranian598b7c62015-11-30 10:02:23 +0100140
Ben Gaineyca58d7e2017-11-22 18:25:41 -0600141static void
142copy_class_filename(const char * class_sign, const char * file_name, char * result, size_t max_length)
143{
144 /*
145 * Assume path name is class hierarchy, this is a common practice with Java programs
146 */
147 if (*class_sign == 'L') {
148 int j, i = 0;
149 char *p = strrchr(class_sign, '/');
150 if (p) {
151 /* drop the 'L' prefix and copy up to the final '/' */
152 for (i = 0; i < (p - class_sign); i++)
153 result[i] = class_sign[i+1];
154 }
155 /*
156 * append file name, we use loops and not string ops to avoid modifying
157 * class_sign which is used later for the symbol name
158 */
159 for (j = 0; i < (max_length - 1) && file_name && j < strlen(file_name); j++, i++)
160 result[i] = file_name[j];
161
162 result[i] = '\0';
163 } else {
164 /* fallback case */
165 size_t file_name_len = strlen(file_name);
166 strncpy(result, file_name, file_name_len < max_length ? file_name_len : 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
Stephane Eranian209045a2015-11-30 10:02:22 +0100234static 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,
Stephane Eranian598b7c62015-11-30 10:02:23 +0100241 const void *compile_info)
Stephane Eranian209045a2015-11-30 10:02:22 +0100242{
Stephane Eranian598b7c62015-11-30 10:02:23 +0100243 jvmti_line_info_t *line_tab = NULL;
Ben Gaineyca58d7e2017-11-22 18:25:41 -0600244 char ** line_file_names = NULL;
Stephane Eranian209045a2015-11-30 10:02:22 +0100245 jclass decl_class;
246 char *class_sign = NULL;
247 char *func_name = NULL;
248 char *func_sign = NULL;
Ben Gaineyca58d7e2017-11-22 18:25:41 -0600249 char *file_name = NULL;
Stephane Eranian209045a2015-11-30 10:02:22 +0100250 char fn[PATH_MAX];
251 uint64_t addr = (uint64_t)(uintptr_t)code_addr;
252 jvmtiError ret;
Stephane Eranian598b7c62015-11-30 10:02:23 +0100253 int nr_lines = 0; /* in line_tab[] */
Stephane Eranian209045a2015-11-30 10:02:22 +0100254 size_t len;
Ben Gaineyca58d7e2017-11-22 18:25:41 -0600255 int output_debug_info = 0;
Stephane Eranian209045a2015-11-30 10:02:22 +0100256
257 ret = (*jvmti)->GetMethodDeclaringClass(jvmti, method,
258 &decl_class);
259 if (ret != JVMTI_ERROR_NONE) {
Stephane Eraniancdd75e32016-10-13 03:59:35 -0700260 print_error(jvmti, "GetMethodDeclaringClass", ret);
Stephane Eranian209045a2015-11-30 10:02:22 +0100261 return;
262 }
263
264 if (has_line_numbers && map && map_length) {
Stephane Eranian598b7c62015-11-30 10:02:23 +0100265 ret = get_line_numbers(jvmti, compile_info, &line_tab, &nr_lines);
Stephane Eranian209045a2015-11-30 10:02:22 +0100266 if (ret != JVMTI_ERROR_NONE) {
267 warnx("jvmti: cannot get line table for method");
Stephane Eranian598b7c62015-11-30 10:02:23 +0100268 nr_lines = 0;
Ben Gaineyca58d7e2017-11-22 18:25:41 -0600269 } 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 }
Stephane Eranian209045a2015-11-30 10:02:22 +0100282 }
283 }
284
Stephane Eranian598b7c62015-11-30 10:02:23 +0100285 ret = (*jvmti)->GetSourceFileName(jvmti, decl_class, &file_name);
286 if (ret != JVMTI_ERROR_NONE) {
Stephane Eraniancdd75e32016-10-13 03:59:35 -0700287 print_error(jvmti, "GetSourceFileName", ret);
Stephane Eranian598b7c62015-11-30 10:02:23 +0100288 goto error;
289 }
290
Stephane Eranian209045a2015-11-30 10:02:22 +0100291 ret = (*jvmti)->GetClassSignature(jvmti, decl_class,
292 &class_sign, NULL);
293 if (ret != JVMTI_ERROR_NONE) {
Stephane Eraniancdd75e32016-10-13 03:59:35 -0700294 print_error(jvmti, "GetClassSignature", ret);
Stephane Eranian209045a2015-11-30 10:02:22 +0100295 goto error;
296 }
297
298 ret = (*jvmti)->GetMethodName(jvmti, method, &func_name,
299 &func_sign, NULL);
300 if (ret != JVMTI_ERROR_NONE) {
Stephane Eraniancdd75e32016-10-13 03:59:35 -0700301 print_error(jvmti, "GetMethodName", ret);
Stephane Eranian209045a2015-11-30 10:02:22 +0100302 goto error;
303 }
304
Ben Gaineyca58d7e2017-11-22 18:25:41 -0600305 copy_class_filename(class_sign, file_name, fn, PATH_MAX);
306
Stephane Eranian209045a2015-11-30 10:02:22 +0100307 /*
308 * write source line info record if we have it
309 */
Ben Gaineyca58d7e2017-11-22 18:25:41 -0600310 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");
Stephane Eranian209045a2015-11-30 10:02:22 +0100313
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);
Stephane Eranian598b7c62015-11-30 10:02:23 +0100318
Stephane Eranian209045a2015-11-30 10:02:22 +0100319 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);
Stephane Eranian209045a2015-11-30 10:02:22 +0100326 (*jvmti)->Deallocate(jvmti, (unsigned char *)file_name);
Stephane Eranian598b7c62015-11-30 10:02:23 +0100327 free(line_tab);
Ben Gaineyca58d7e2017-11-22 18:25:41 -0600328 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);
Stephane Eranian209045a2015-11-30 10:02:22 +0100335}
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
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -0300352Agent_OnLoad(JavaVM *jvm, char *options, void *reserved __maybe_unused)
Stephane Eranian209045a2015-11-30 10:02:22 +0100353{
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) {
Stephane Eraniancdd75e32016-10-13 03:59:35 -0700384 print_error(jvmti, "AddCapabilities", ret);
Stephane Eranian209045a2015-11-30 10:02:22 +0100385 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;
Stephane Eraniancdd75e32016-10-13 03:59:35 -0700395 } else if (ret != JVMTI_ERROR_NONE)
396 print_error(jvmti, "GetJLocationFormat", ret);
397
Stephane Eranian209045a2015-11-30 10:02:22 +0100398
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) {
Stephane Eraniancdd75e32016-10-13 03:59:35 -0700406 print_error(jvmti, "SetEventCallbacks", ret);
Stephane Eranian209045a2015-11-30 10:02:22 +0100407 return -1;
408 }
409
410 ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
411 JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
412 if (ret != JVMTI_ERROR_NONE) {
Stephane Eraniancdd75e32016-10-13 03:59:35 -0700413 print_error(jvmti, "SetEventNotificationMode(METHOD_LOAD)", ret);
Stephane Eranian209045a2015-11-30 10:02:22 +0100414 return -1;
415 }
416
417 ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
418 JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
419 if (ret != JVMTI_ERROR_NONE) {
Stephane Eraniancdd75e32016-10-13 03:59:35 -0700420 print_error(jvmti, "SetEventNotificationMode(CODE_GENERATED)", ret);
Stephane Eranian209045a2015-11-30 10:02:22 +0100421 return -1;
422 }
423 return 0;
424}
425
426JNIEXPORT void JNICALL
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -0300427Agent_OnUnload(JavaVM *jvm __maybe_unused)
Stephane Eranian209045a2015-11-30 10:02:22 +0100428{
429 int ret;
430
431 ret = jvmti_close(jvmti_agent);
432 if (ret)
433 errx(1, "Error: op_close_agent()");
434}