libjvmti.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include "jvmti_agent.h"
  15. static int has_line_numbers;
  16. void *jvmti_agent;
  17. static 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. #ifdef HAVE_JVMTI_CMLR
  30. static jvmtiError
  31. do_get_line_number(jvmtiEnv *jvmti, void *pc, jmethodID m, jint bci,
  32. jvmti_line_info_t *tab)
  33. {
  34. jint i, nr_lines = 0;
  35. jvmtiLineNumberEntry *loc_tab = NULL;
  36. jvmtiError ret;
  37. jint src_line = -1;
  38. ret = (*jvmti)->GetLineNumberTable(jvmti, m, &nr_lines, &loc_tab);
  39. if (ret == JVMTI_ERROR_ABSENT_INFORMATION || ret == JVMTI_ERROR_NATIVE_METHOD) {
  40. /* No debug information for this method */
  41. return ret;
  42. } else if (ret != JVMTI_ERROR_NONE) {
  43. print_error(jvmti, "GetLineNumberTable", ret);
  44. return ret;
  45. }
  46. for (i = 0; i < nr_lines && loc_tab[i].start_location <= bci; i++) {
  47. src_line = i;
  48. }
  49. if (src_line != -1) {
  50. tab->pc = (unsigned long)pc;
  51. tab->line_number = loc_tab[src_line].line_number;
  52. tab->discrim = 0; /* not yet used */
  53. tab->methodID = m;
  54. ret = JVMTI_ERROR_NONE;
  55. } else {
  56. ret = JVMTI_ERROR_ABSENT_INFORMATION;
  57. }
  58. (*jvmti)->Deallocate(jvmti, (unsigned char *)loc_tab);
  59. return ret;
  60. }
  61. static jvmtiError
  62. get_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. PCStackInfo *c;
  67. jint ret;
  68. int nr_total = 0;
  69. int i, lines_total = 0;
  70. if (!(tab && nr_lines))
  71. return JVMTI_ERROR_NULL_POINTER;
  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. nr_total += rec->numpcs;
  79. }
  80. }
  81. if (nr_total == 0)
  82. return JVMTI_ERROR_NOT_FOUND;
  83. /*
  84. * Phase 2 -- allocate big enough line table
  85. */
  86. *tab = malloc(nr_total * sizeof(**tab));
  87. if (!*tab)
  88. 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++) {
  93. c = rec->pcinfo + i;
  94. /*
  95. * c->methods is the stack of inlined method calls
  96. * at c->pc. [0] is the leaf method. Caller frames
  97. * are ignored at the moment.
  98. */
  99. ret = do_get_line_number(jvmti, c->pc,
  100. c->methods[0],
  101. c->bcis[0],
  102. *tab + lines_total);
  103. if (ret == JVMTI_ERROR_NONE)
  104. lines_total++;
  105. }
  106. }
  107. }
  108. *nr_lines = lines_total;
  109. return JVMTI_ERROR_NONE;
  110. }
  111. #else /* HAVE_JVMTI_CMLR */
  112. static jvmtiError
  113. get_line_numbers(jvmtiEnv *jvmti __maybe_unused, const void *compile_info __maybe_unused,
  114. jvmti_line_info_t **tab __maybe_unused, int *nr_lines __maybe_unused)
  115. {
  116. return JVMTI_ERROR_NONE;
  117. }
  118. #endif /* HAVE_JVMTI_CMLR */
  119. static void
  120. copy_class_filename(const char * class_sign, const char * file_name, char * result, size_t max_length)
  121. {
  122. /*
  123. * Assume path name is class hierarchy, this is a common practice with Java programs
  124. */
  125. if (*class_sign == 'L') {
  126. size_t j, i = 0;
  127. const char *p = strrchr(class_sign, '/');
  128. if (p) {
  129. /* drop the 'L' prefix and copy up to the final '/' */
  130. for (i = 0; i < (size_t)(p - class_sign); i++)
  131. result[i] = class_sign[i+1];
  132. }
  133. /*
  134. * append file name, we use loops and not string ops to avoid modifying
  135. * class_sign which is used later for the symbol name
  136. */
  137. for (j = 0; i < (max_length - 1) && file_name && j < strlen(file_name); j++, i++)
  138. result[i] = file_name[j];
  139. result[i] = '\0';
  140. } else {
  141. /* fallback case */
  142. strlcpy(result, file_name, max_length);
  143. }
  144. }
  145. static jvmtiError
  146. get_source_filename(jvmtiEnv *jvmti, jmethodID methodID, char ** buffer)
  147. {
  148. jvmtiError ret;
  149. jclass decl_class;
  150. char *file_name = NULL;
  151. char *class_sign = NULL;
  152. char fn[PATH_MAX];
  153. size_t len;
  154. ret = (*jvmti)->GetMethodDeclaringClass(jvmti, methodID, &decl_class);
  155. if (ret != JVMTI_ERROR_NONE) {
  156. print_error(jvmti, "GetMethodDeclaringClass", ret);
  157. return ret;
  158. }
  159. ret = (*jvmti)->GetSourceFileName(jvmti, decl_class, &file_name);
  160. if (ret != JVMTI_ERROR_NONE) {
  161. print_error(jvmti, "GetSourceFileName", ret);
  162. return ret;
  163. }
  164. ret = (*jvmti)->GetClassSignature(jvmti, decl_class, &class_sign, NULL);
  165. if (ret != JVMTI_ERROR_NONE) {
  166. print_error(jvmti, "GetClassSignature", ret);
  167. goto free_file_name_error;
  168. }
  169. copy_class_filename(class_sign, file_name, fn, PATH_MAX);
  170. len = strlen(fn);
  171. *buffer = malloc((len + 1) * sizeof(char));
  172. if (!*buffer) {
  173. print_error(jvmti, "GetClassSignature", ret);
  174. ret = JVMTI_ERROR_OUT_OF_MEMORY;
  175. goto free_class_sign_error;
  176. }
  177. strcpy(*buffer, fn);
  178. ret = JVMTI_ERROR_NONE;
  179. free_class_sign_error:
  180. (*jvmti)->Deallocate(jvmti, (unsigned char *)class_sign);
  181. free_file_name_error:
  182. (*jvmti)->Deallocate(jvmti, (unsigned char *)file_name);
  183. return ret;
  184. }
  185. static jvmtiError
  186. fill_source_filenames(jvmtiEnv *jvmti, int nr_lines,
  187. const jvmti_line_info_t * line_tab,
  188. char ** file_names)
  189. {
  190. int index;
  191. jvmtiError ret;
  192. for (index = 0; index < nr_lines; ++index) {
  193. ret = get_source_filename(jvmti, line_tab[index].methodID, &(file_names[index]));
  194. if (ret != JVMTI_ERROR_NONE)
  195. return ret;
  196. }
  197. return JVMTI_ERROR_NONE;
  198. }
  199. static void JNICALL
  200. compiled_method_load_cb(jvmtiEnv *jvmti,
  201. jmethodID method,
  202. jint code_size,
  203. void const *code_addr,
  204. jint map_length,
  205. jvmtiAddrLocationMap const *map,
  206. const void *compile_info)
  207. {
  208. jvmti_line_info_t *line_tab = NULL;
  209. char ** line_file_names = NULL;
  210. jclass decl_class;
  211. char *class_sign = NULL;
  212. char *func_name = NULL;
  213. char *func_sign = NULL;
  214. uint64_t addr = (uint64_t)(uintptr_t)code_addr;
  215. jvmtiError ret;
  216. int nr_lines = 0; /* in line_tab[] */
  217. size_t len;
  218. int output_debug_info = 0;
  219. ret = (*jvmti)->GetMethodDeclaringClass(jvmti, method,
  220. &decl_class);
  221. if (ret != JVMTI_ERROR_NONE) {
  222. print_error(jvmti, "GetMethodDeclaringClass", ret);
  223. return;
  224. }
  225. if (has_line_numbers && map && map_length) {
  226. ret = get_line_numbers(jvmti, compile_info, &line_tab, &nr_lines);
  227. if (ret != JVMTI_ERROR_NONE) {
  228. if (ret != JVMTI_ERROR_NOT_FOUND) {
  229. warnx("jvmti: cannot get line table for method");
  230. }
  231. nr_lines = 0;
  232. } else if (nr_lines > 0) {
  233. line_file_names = malloc(sizeof(char*) * nr_lines);
  234. if (!line_file_names) {
  235. warnx("jvmti: cannot allocate space for line table method names");
  236. } else {
  237. memset(line_file_names, 0, sizeof(char*) * nr_lines);
  238. ret = fill_source_filenames(jvmti, nr_lines, line_tab, line_file_names);
  239. if (ret != JVMTI_ERROR_NONE) {
  240. warnx("jvmti: fill_source_filenames failed");
  241. } else {
  242. output_debug_info = 1;
  243. }
  244. }
  245. }
  246. }
  247. ret = (*jvmti)->GetClassSignature(jvmti, decl_class,
  248. &class_sign, NULL);
  249. if (ret != JVMTI_ERROR_NONE) {
  250. print_error(jvmti, "GetClassSignature", ret);
  251. goto error;
  252. }
  253. ret = (*jvmti)->GetMethodName(jvmti, method, &func_name,
  254. &func_sign, NULL);
  255. if (ret != JVMTI_ERROR_NONE) {
  256. print_error(jvmti, "GetMethodName", ret);
  257. goto error;
  258. }
  259. /*
  260. * write source line info record if we have it
  261. */
  262. if (output_debug_info)
  263. if (jvmti_write_debug_info(jvmti_agent, addr, nr_lines, line_tab, (const char * const *) line_file_names))
  264. warnx("jvmti: write_debug_info() failed");
  265. len = strlen(func_name) + strlen(class_sign) + strlen(func_sign) + 2;
  266. {
  267. char str[len];
  268. snprintf(str, len, "%s%s%s", class_sign, func_name, func_sign);
  269. if (jvmti_write_code(jvmti_agent, str, addr, code_addr, code_size))
  270. warnx("jvmti: write_code() failed");
  271. }
  272. error:
  273. (*jvmti)->Deallocate(jvmti, (unsigned char *)func_name);
  274. (*jvmti)->Deallocate(jvmti, (unsigned char *)func_sign);
  275. (*jvmti)->Deallocate(jvmti, (unsigned char *)class_sign);
  276. free(line_tab);
  277. while (line_file_names && (nr_lines > 0)) {
  278. if (line_file_names[nr_lines - 1]) {
  279. free(line_file_names[nr_lines - 1]);
  280. }
  281. nr_lines -= 1;
  282. }
  283. free(line_file_names);
  284. }
  285. static void JNICALL
  286. code_generated_cb(jvmtiEnv *jvmti,
  287. char const *name,
  288. void const *code_addr,
  289. jint code_size)
  290. {
  291. uint64_t addr = (uint64_t)(unsigned long)code_addr;
  292. int ret;
  293. ret = jvmti_write_code(jvmti_agent, name, addr, code_addr, code_size);
  294. if (ret)
  295. warnx("jvmti: write_code() failed for code_generated");
  296. }
  297. JNIEXPORT jint JNICALL
  298. Agent_OnLoad(JavaVM *jvm, char *options, void *reserved __maybe_unused)
  299. {
  300. jvmtiEventCallbacks cb;
  301. jvmtiCapabilities caps1;
  302. jvmtiJlocationFormat format;
  303. jvmtiEnv *jvmti = NULL;
  304. jint ret;
  305. jvmti_agent = jvmti_open();
  306. if (!jvmti_agent) {
  307. warnx("jvmti: open_agent failed");
  308. return -1;
  309. }
  310. /*
  311. * Request a JVMTI interface version 1 environment
  312. */
  313. ret = (*jvm)->GetEnv(jvm, (void *)&jvmti, JVMTI_VERSION_1);
  314. if (ret != JNI_OK) {
  315. warnx("jvmti: jvmti version 1 not supported");
  316. return -1;
  317. }
  318. /*
  319. * acquire method_load capability, we require it
  320. * request line numbers (optional)
  321. */
  322. memset(&caps1, 0, sizeof(caps1));
  323. caps1.can_generate_compiled_method_load_events = 1;
  324. ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
  325. if (ret != JVMTI_ERROR_NONE) {
  326. print_error(jvmti, "AddCapabilities", ret);
  327. return -1;
  328. }
  329. ret = (*jvmti)->GetJLocationFormat(jvmti, &format);
  330. if (ret == JVMTI_ERROR_NONE && format == JVMTI_JLOCATION_JVMBCI) {
  331. memset(&caps1, 0, sizeof(caps1));
  332. caps1.can_get_line_numbers = 1;
  333. caps1.can_get_source_file_name = 1;
  334. ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
  335. if (ret == JVMTI_ERROR_NONE)
  336. has_line_numbers = 1;
  337. } else if (ret != JVMTI_ERROR_NONE)
  338. print_error(jvmti, "GetJLocationFormat", ret);
  339. memset(&cb, 0, sizeof(cb));
  340. cb.CompiledMethodLoad = compiled_method_load_cb;
  341. cb.DynamicCodeGenerated = code_generated_cb;
  342. ret = (*jvmti)->SetEventCallbacks(jvmti, &cb, sizeof(cb));
  343. if (ret != JVMTI_ERROR_NONE) {
  344. print_error(jvmti, "SetEventCallbacks", ret);
  345. return -1;
  346. }
  347. ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
  348. JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
  349. if (ret != JVMTI_ERROR_NONE) {
  350. print_error(jvmti, "SetEventNotificationMode(METHOD_LOAD)", ret);
  351. return -1;
  352. }
  353. ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
  354. JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
  355. if (ret != JVMTI_ERROR_NONE) {
  356. print_error(jvmti, "SetEventNotificationMode(CODE_GENERATED)", ret);
  357. return -1;
  358. }
  359. return 0;
  360. }
  361. JNIEXPORT void JNICALL
  362. Agent_OnUnload(JavaVM *jvm __maybe_unused)
  363. {
  364. int ret;
  365. ret = jvmti_close(jvmti_agent);
  366. if (ret)
  367. errx(1, "Error: op_close_agent()");
  368. }