10 #include "jvmti_agent.h"
12 static int has_line_numbers;
16 do_get_line_numbers(jvmtiEnv *jvmti, void *pc, jmethodID m, jint bci,
17 jvmti_line_info_t *tab, jint *nr)
21 jvmtiLineNumberEntry *loc_tab = NULL;
24 ret = (*jvmti)->GetLineNumberTable(jvmti, m, &nr_lines, &loc_tab);
25 if (ret != JVMTI_ERROR_NONE)
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 */
38 (*jvmti)->Deallocate(jvmti, (unsigned char *)loc_tab);
40 return JVMTI_ERROR_NONE;
44 get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **tab, int *nr_lines)
46 const jvmtiCompiledMethodLoadRecordHeader *hdr;
47 jvmtiCompiledMethodLoadInlineRecord *rec;
48 jvmtiLineNumberEntry *lne = NULL;
52 int i, lines_total = 0;
54 if (!(tab && nr_lines))
55 return JVMTI_ERROR_NULL_POINTER;
58 * Phase 1 -- get the number of lines necessary
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++) {
67 * unfortunately, need a tab to get the number of lines!
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);
80 return JVMTI_ERROR_NOT_FOUND;
83 * Phase 2 -- allocate big enough line table
85 *tab = malloc(nr_total * sizeof(**tab));
87 return JVMTI_ERROR_OUT_OF_MEMORY;
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++) {
95 ret = do_get_line_numbers(jvmti, c->pc,
100 if (ret == JVMTI_ERROR_NONE)
105 *nr_lines = lines_total;
106 return JVMTI_ERROR_NONE;
110 compiled_method_load_cb(jvmtiEnv *jvmti,
113 void const *code_addr,
115 jvmtiAddrLocationMap const *map,
116 const void *compile_info)
118 jvmti_line_info_t *line_tab = NULL;
120 char *class_sign = NULL;
121 char *func_name = NULL;
122 char *func_sign = NULL;
123 char *file_name= NULL;
125 uint64_t addr = (uint64_t)(uintptr_t)code_addr;
127 int nr_lines = 0; /* in line_tab[] */
130 ret = (*jvmti)->GetMethodDeclaringClass(jvmti, method,
132 if (ret != JVMTI_ERROR_NONE) {
133 warnx("jvmti: cannot get declaring class");
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");
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);
151 ret = (*jvmti)->GetClassSignature(jvmti, decl_class,
153 if (ret != JVMTI_ERROR_NONE) {
154 warnx("jvmti: getclassignature failed");
158 ret = (*jvmti)->GetMethodName(jvmti, method, &func_name,
160 if (ret != JVMTI_ERROR_NONE) {
161 warnx("jvmti: failed getmethodname");
166 * Assume path name is class hierarchy, this is a common practice with Java programs
168 if (*class_sign == 'L') {
170 char *p = strrchr(class_sign, '/');
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];
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
180 for (j = 0; i < (PATH_MAX - 1) && file_name && j < strlen(file_name); j++, i++)
181 fn[i] = file_name[j];
185 strcpy(fn, file_name);
188 * write source line info record if we have it
190 if (jvmti_write_debug_info(jvmti_agent, addr, fn, line_tab, nr_lines))
191 warnx("jvmti: write_debug_info() failed");
193 len = strlen(func_name) + strlen(class_sign) + strlen(func_sign) + 2;
196 snprintf(str, len, "%s%s%s", class_sign, func_name, func_sign);
198 if (jvmti_write_code(jvmti_agent, str, addr, code_addr, code_size))
199 warnx("jvmti: write_code() failed");
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);
210 code_generated_cb(jvmtiEnv *jvmti,
212 void const *code_addr,
215 uint64_t addr = (uint64_t)(unsigned long)code_addr;
218 ret = jvmti_write_code(jvmti_agent, name, addr, code_addr, code_size);
220 warnx("jvmti: write_code() failed for code_generated");
223 JNIEXPORT jint JNICALL
224 Agent_OnLoad(JavaVM *jvm, char *options, void *reserved __unused)
226 jvmtiEventCallbacks cb;
227 jvmtiCapabilities caps1;
228 jvmtiJlocationFormat format;
229 jvmtiEnv *jvmti = NULL;
232 jvmti_agent = jvmti_open();
234 warnx("jvmti: open_agent failed");
239 * Request a JVMTI interface version 1 environment
241 ret = (*jvm)->GetEnv(jvm, (void *)&jvmti, JVMTI_VERSION_1);
243 warnx("jvmti: jvmti version 1 not supported");
248 * acquire method_load capability, we require it
249 * request line numbers (optional)
251 memset(&caps1, 0, sizeof(caps1));
252 caps1.can_generate_compiled_method_load_events = 1;
254 ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
255 if (ret != JVMTI_ERROR_NONE) {
256 warnx("jvmti: acquire compiled_method capability failed");
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;
269 memset(&cb, 0, sizeof(cb));
271 cb.CompiledMethodLoad = compiled_method_load_cb;
272 cb.DynamicCodeGenerated = code_generated_cb;
274 ret = (*jvmti)->SetEventCallbacks(jvmti, &cb, sizeof(cb));
275 if (ret != JVMTI_ERROR_NONE) {
276 warnx("jvmti: cannot set event callbacks");
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");
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");
296 JNIEXPORT void JNICALL
297 Agent_OnUnload(JavaVM *jvm __unused)
301 ret = jvmti_close(jvmti_agent);
303 errx(1, "Error: op_close_agent()");