libjvmti.c 11 KB

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