当前位置:   article > 正文

sssssssssss67

sssssssssss67
  1. class VM_GetStackTrace : public VM_Operation {
  2. private:
  3. JvmtiEnv *_env;
  4. JavaThread *_java_thread;
  5. jint _start_depth;
  6. jint _max_count;
  7. jvmtiFrameInfo *_frame_buffer;
  8. jint *_count_ptr;
  9. jvmtiError _result;
  10. public:
  11. VM_GetStackTrace(JvmtiEnv *env, JavaThread *java_thread,
  12. jint start_depth, jint max_count,
  13. jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
  14. _env = env;
  15. _java_thread = java_thread;
  16. _start_depth = start_depth;
  17. _max_count = max_count;
  18. _frame_buffer = frame_buffer;
  19. _count_ptr = count_ptr;
  20. }
  21. jvmtiError result() { return _result; }
  22. VMOp_Type type() const { return VMOp_GetStackTrace; }
  23. void doit() {
  24. _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
  25. if (Threads::includes(_java_thread) && !_java_thread->is_exiting()
  26. && _java_thread->threadObj() != NULL) {
  27. _result = ((JvmtiEnvBase *)_env)->get_stack_trace(_java_thread,
  28. _start_depth, _max_count,
  29. _frame_buffer, _count_ptr);
  30. }
  31. }
  32. };
  33. struct StackInfoNode;
  34. class VM_GetMultipleStackTraces : public VM_Operation {
  35. private:
  36. JvmtiEnv *_env;
  37. jint _max_frame_count;
  38. jvmtiStackInfo *_stack_info;
  39. jvmtiError _result;
  40. int _frame_count_total;
  41. struct StackInfoNode *_head;
  42. JvmtiEnvBase *env() { return (JvmtiEnvBase *)_env; }
  43. jint max_frame_count() { return _max_frame_count; }
  44. struct StackInfoNode *head() { return _head; }
  45. void set_head(StackInfoNode *head) { _head = head; }
  46. protected:
  47. void set_result(jvmtiError result) { _result = result; }
  48. void fill_frames(jthread jt, JavaThread *thr, oop thread_oop);
  49. void allocate_and_fill_stacks(jint thread_count);
  50. public:
  51. VM_GetMultipleStackTraces(JvmtiEnv *env, jint max_frame_count) {
  52. _env = env;
  53. _max_frame_count = max_frame_count;
  54. _frame_count_total = 0;
  55. _head = NULL;
  56. _result = JVMTI_ERROR_NONE;
  57. }
  58. VMOp_Type type() const { return VMOp_GetMultipleStackTraces; }
  59. jvmtiStackInfo *stack_info() { return _stack_info; }
  60. jvmtiError result() { return _result; }
  61. };
  62. class VM_GetAllStackTraces : public VM_GetMultipleStackTraces {
  63. private:
  64. JavaThread *_calling_thread;
  65. jint _final_thread_count;
  66. public:
  67. VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread,
  68. jint max_frame_count)
  69. : VM_GetMultipleStackTraces(env, max_frame_count) {
  70. _calling_thread = calling_thread;
  71. }
  72. VMOp_Type type() const { return VMOp_GetAllStackTraces; }
  73. void doit();
  74. jint final_thread_count() { return _final_thread_count; }
  75. };
  76. class VM_GetThreadListStackTraces : public VM_GetMultipleStackTraces {
  77. private:
  78. jint _thread_count;
  79. const jthread* _thread_list;
  80. public:
  81. VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count)
  82. : VM_GetMultipleStackTraces(env, max_frame_count) {
  83. _thread_count = thread_count;
  84. _thread_list = thread_list;
  85. }
  86. VMOp_Type type() const { return VMOp_GetThreadListStackTraces; }
  87. void doit();
  88. };
  89. class VM_GetFrameCount : public VM_Operation {
  90. private:
  91. JvmtiEnv *_env;
  92. JvmtiThreadState *_state;
  93. jint *_count_ptr;
  94. jvmtiError _result;
  95. public:
  96. VM_GetFrameCount(JvmtiEnv *env, JvmtiThreadState *state, jint *count_ptr) {
  97. _env = env;
  98. _state = state;
  99. _count_ptr = count_ptr;
  100. }
  101. VMOp_Type type() const { return VMOp_GetFrameCount; }
  102. jvmtiError result() { return _result; }
  103. void doit() {
  104. _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
  105. }
  106. };
  107. class VM_GetFrameLocation : public VM_Operation {
  108. private:
  109. JvmtiEnv *_env;
  110. JavaThread* _java_thread;
  111. jint _depth;
  112. jmethodID* _method_ptr;
  113. jlocation* _location_ptr;
  114. jvmtiError _result;
  115. public:
  116. VM_GetFrameLocation(JvmtiEnv *env, JavaThread* java_thread, jint depth,
  117. jmethodID* method_ptr, jlocation* location_ptr) {
  118. _env = env;
  119. _java_thread = java_thread;
  120. _depth = depth;
  121. _method_ptr = method_ptr;
  122. _location_ptr = location_ptr;
  123. }
  124. VMOp_Type type() const { return VMOp_GetFrameLocation; }
  125. jvmtiError result() { return _result; }
  126. void doit() {
  127. _result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth,
  128. _method_ptr, _location_ptr);
  129. }
  130. };
  131. class ResourceTracker : public StackObj {
  132. private:
  133. JvmtiEnv* _env;
  134. GrowableArray<unsigned char*> *_allocations;
  135. bool _failed;
  136. public:
  137. ResourceTracker(JvmtiEnv* env);
  138. ~ResourceTracker();
  139. jvmtiError allocate(jlong size, unsigned char** mem_ptr);
  140. unsigned char* allocate(jlong size);
  141. char* strdup(const char* str);
  142. };
  143. class JvmtiMonitorClosure: public MonitorClosure {
  144. private:
  145. JavaThread *_java_thread;
  146. JavaThread *_calling_thread;
  147. GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list;
  148. jvmtiError _error;
  149. JvmtiEnvBase *_env;
  150. public:
  151. JvmtiMonitorClosure(JavaThread* thread, JavaThread *calling_thread,
  152. GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors,
  153. JvmtiEnvBase *env) {
  154. _java_thread = thread;
  155. _calling_thread = calling_thread;
  156. _owned_monitors_list = owned_monitors;
  157. _error = JVMTI_ERROR_NONE;
  158. _env = env;
  159. }
  160. void do_monitor(ObjectMonitor* mon);
  161. jvmtiError error() { return _error;}
  162. };
  163. #endif // SHARE_VM_PRIMS_JVMTIENVBASE_HPP
  164. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiEnvFill.java
  165. import java.io.*;
  166. import java.util.*;
  167. class jvmtiEnvFill {
  168. public static void main(String[] args) throws IOException {
  169. if (args.length != 3) {
  170. System.err.println("usage: <filledFile> <stubFile> <resultFile>");
  171. System.exit(1);
  172. }
  173. String filledFN = args[0];
  174. String stubFN = args[1];
  175. String resultFN = args[2];
  176. SourceFile filledSF = new SourceFile(filledFN);
  177. SourceFile stubSF = new SourceFile(stubFN);
  178. stubSF.fill(filledSF);
  179. PrintWriter out = new PrintWriter(new FileWriter(resultFN));
  180. stubSF.output(out);
  181. out.close();
  182. }
  183. }
  184. class SourceFile {
  185. static final String endFilePrefix = "// end file prefix";
  186. static final String functionPrefix = "JvmtiEnv::";
  187. final String fn;
  188. LineNumberReader in;
  189. String line;
  190. List<String> top = new ArrayList<String>();
  191. List<String> before = new ArrayList<String>();
  192. boolean inFilePrefix = true;
  193. List<Function> functions = new ArrayList<Function>();
  194. Map<String, Function> functionMap = new HashMap<String, Function>();
  195. class Function {
  196. String name;
  197. String args;
  198. String compareArgs;
  199. List comment;
  200. List<String> body = new ArrayList<String>();
  201. Function() throws IOException {
  202. line = in.readLine();
  203. String trimmed = line.trim();
  204. if (!trimmed.startsWith(functionPrefix)) {
  205. error("expected '" + functionPrefix + "'");
  206. }
  207. int index = trimmed.indexOf('(', functionPrefix.length());
  208. if (index == -1) {
  209. error("missing open paren");
  210. }
  211. name = trimmed.substring(functionPrefix.length(), index);
  212. int index2 = trimmed.indexOf(')', index);
  213. if (index2 == -1) {
  214. error("missing close paren - must be on same line");
  215. }
  216. args = trimmed.substring(index+1, index2);
  217. compareArgs = args.replaceAll("\\s", "");
  218. String tail = trimmed.substring(index2+1).trim();
  219. if (!tail.equals("{")) {
  220. error("function declaration first line must end with open bracket '{', instead got '" +
  221. tail + "'");
  222. }
  223. while(true) {
  224. line = in.readLine();
  225. if (line == null) {
  226. line = ""; // so error does not look wierd
  227. error("unexpected end of file");
  228. }
  229. if (line.startsWith("}")) {
  230. break;
  231. }
  232. body.add(line);
  233. }
  234. String expected = "} /* end " + name + " */";
  235. trimmed = line.replaceAll("\\s","");
  236. if (!trimmed.equals(expected.replaceAll("\\s",""))) {
  237. error("function end is malformed - should be: " + expected);
  238. }
  239. comment = before;
  240. before = new ArrayList<String>();
  241. }
  242. void remove() {
  243. functionMap.remove(name);
  244. }
  245. String fileName() {
  246. return fn;
  247. }
  248. void fill(Function filledFunc) {
  249. if (filledFunc == null) {
  250. System.err.println("Warning: function " + name + " missing from filled file");
  251. body.add(0, " /*** warning: function added and not filled in ***/");
  252. } else {
  253. int fbsize = filledFunc.body.size();
  254. int bsize = body.size();
  255. if (fbsize > bsize || !body.subList(bsize-fbsize,bsize).equals(filledFunc.body)) {
  256. body = filledFunc.body;
  257. if (!compareArgs.equals(filledFunc.compareArgs)) {
  258. System.err.println("Warning: function " + name +
  259. ": filled and stub arguments differ");
  260. System.err.println(" old (filled): " + filledFunc.args);
  261. System.err.println(" new (stub): " + args);
  262. body.add(0, " /*** warning: arguments changed, were: " +
  263. filledFunc.args + " ***/");
  264. }
  265. }
  266. filledFunc.remove(); // mark used
  267. }
  268. }
  269. void output(PrintWriter out) {
  270. Iterator it = comment.iterator();
  271. while (it.hasNext()) {
  272. out.println(it.next());
  273. }
  274. out.println("jvmtiError");
  275. out.print(functionPrefix);
  276. out.print(name);
  277. out.print('(');
  278. out.print(args);
  279. out.println(") {");
  280. it = body.iterator();
  281. while (it.hasNext()) {
  282. out.println(it.next());
  283. }
  284. out.print("} /* end ");
  285. out.print(name);
  286. out.println(" */");
  287. }
  288. }
  289. SourceFile(String fn) throws IOException {
  290. this.fn = fn;
  291. Reader reader = new FileReader(fn);
  292. in = new LineNumberReader(reader);
  293. while (readGaps()) {
  294. Function func = new Function();
  295. functionMap.put(func.name, func);
  296. functions.add(func);
  297. }
  298. in.close();
  299. }
  300. void error(String msg) {
  301. System.err.println("Fatal error parsing file: " + fn);
  302. System.err.println("Line number: " + in.getLineNumber());
  303. System.err.println("Error message: " + msg);
  304. System.err.println("Source line: " + line);
  305. System.exit(1);
  306. }
  307. boolean readGaps() throws IOException {
  308. while(true) {
  309. line = in.readLine();
  310. if (line == null) {
  311. return false; // end of file
  312. }
  313. if (!inFilePrefix && line.startsWith("}")) {
  314. error("unexpected close bracket in first column, outside of function.\n");
  315. }
  316. String trimmed = line.trim();
  317. if (line.startsWith("jvmtiError")) {
  318. if (trimmed.equals("jvmtiError")) {
  319. if (inFilePrefix) {
  320. error("unexpected 'jvmtiError' line in file prefix.\n" +
  321. "is '" + endFilePrefix + "'... line missing?");
  322. }
  323. return true; // beginning of a function
  324. } else {
  325. error("extra characters at end of 'jvmtiError'");
  326. }
  327. }
  328. if (inFilePrefix) {
  329. top.add(line);
  330. } else {
  331. trimmed = line.trim();
  332. if (!trimmed.equals("") && !trimmed.startsWith("//") && !trimmed.startsWith("#")) {
  333. error("only comments and blank lines allowed between functions");
  334. }
  335. before.add(line);
  336. }
  337. if (line.replaceAll("\\s","").toLowerCase().startsWith(endFilePrefix.replaceAll("\\s",""))) {
  338. if (!inFilePrefix) {
  339. error("excess '" + endFilePrefix + "'");
  340. }
  341. inFilePrefix = false;
  342. }
  343. }
  344. }
  345. void fill(SourceFile filledSF) {
  346. top = filledSF.top;
  347. Iterator it = functions.iterator();
  348. while (it.hasNext()) {
  349. Function stubFunc = (Function)(it.next());
  350. Function filledFunc = (Function)filledSF.functionMap.get(stubFunc.name);
  351. stubFunc.fill(filledFunc);
  352. }
  353. if (filledSF.functionMap.size() > 0) {
  354. System.err.println("Warning: the following functions were present in the " +
  355. "filled file but missing in the stub file and thus not copied:");
  356. it = filledSF.functionMap.values().iterator();
  357. while (it.hasNext()) {
  358. System.err.println(" " + ((Function)(it.next())).name);
  359. }
  360. }
  361. }
  362. void output(PrintWriter out) {
  363. Iterator it = top.iterator();
  364. while (it.hasNext()) {
  365. out.println(it.next());
  366. }
  367. it = functions.iterator();
  368. while (it.hasNext()) {
  369. Function stubFunc = (Function)(it.next());
  370. stubFunc.output(out);
  371. }
  372. }
  373. }
  374. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiEnvThreadState.cpp
  375. #include "precompiled.hpp"
  376. #include "classfile/systemDictionary.hpp"
  377. #include "interpreter/interpreter.hpp"
  378. #include "jvmtifiles/jvmtiEnv.hpp"
  379. #include "memory/resourceArea.hpp"
  380. #include "prims/jvmtiEnvThreadState.hpp"
  381. #include "prims/jvmtiEventController.inline.hpp"
  382. #include "prims/jvmtiImpl.hpp"
  383. #include "runtime/handles.hpp"
  384. #include "runtime/handles.inline.hpp"
  385. #include "runtime/interfaceSupport.hpp"
  386. #include "runtime/javaCalls.hpp"
  387. #include "runtime/signature.hpp"
  388. #include "runtime/vframe.hpp"
  389. #include "runtime/vm_operations.hpp"
  390. #ifndef PRODUCT
  391. void JvmtiFramePop::print() {
  392. tty->print_cr("_frame_number=%d", _frame_number);
  393. }
  394. #endif
  395. void
  396. JvmtiFramePops::set(JvmtiFramePop& fp) {
  397. if (_pops->find(fp.frame_number()) < 0) {
  398. _pops->append(fp.frame_number());
  399. }
  400. }
  401. void
  402. JvmtiFramePops::clear(JvmtiFramePop& fp) {
  403. assert(_pops->length() > 0, "No more frame pops");
  404. _pops->remove(fp.frame_number());
  405. }
  406. int
  407. JvmtiFramePops::clear_to(JvmtiFramePop& fp) {
  408. int cleared = 0;
  409. int index = 0;
  410. while (index < _pops->length()) {
  411. JvmtiFramePop pop = JvmtiFramePop(_pops->at(index));
  412. if (pop.above_on_stack(fp)) {
  413. _pops->remove_at(index);
  414. ++cleared;
  415. } else {
  416. ++index;
  417. }
  418. }
  419. return cleared;
  420. }
  421. JvmtiFramePops::JvmtiFramePops() {
  422. _pops = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int> (2, true);
  423. }
  424. JvmtiFramePops::~JvmtiFramePops() {
  425. delete _pops;
  426. }
  427. #ifndef PRODUCT
  428. void JvmtiFramePops::print() {
  429. ResourceMark rm;
  430. int n = _pops->length();
  431. for (int i=0; i<n; i++) {
  432. JvmtiFramePop fp = JvmtiFramePop(_pops->at(i));
  433. tty->print("%d: ", i);
  434. fp.print();
  435. tty->cr();
  436. }
  437. }
  438. #endif
  439. JvmtiEnvThreadState::JvmtiEnvThreadState(JavaThread *thread, JvmtiEnvBase *env) :
  440. _event_enable() {
  441. _thread = thread;
  442. _env = (JvmtiEnv*)env;
  443. _next = NULL;
  444. _frame_pops = NULL;
  445. _current_bci = 0;
  446. _current_method_id = NULL;
  447. _breakpoint_posted = false;
  448. _single_stepping_posted = false;
  449. _agent_thread_local_storage_data = NULL;
  450. }
  451. JvmtiEnvThreadState::~JvmtiEnvThreadState() {
  452. delete _frame_pops;
  453. _frame_pops = NULL;
  454. }
  455. void JvmtiEnvThreadState::compare_and_set_current_location(Method* new_method,
  456. address new_location, jvmtiEvent event) {
  457. int new_bci = new_location - new_method->code_base();
  458. jmethodID new_method_id = new_method->jmethod_id();
  459. if (_current_bci == new_bci && _current_method_id == new_method_id) {
  460. switch (event) {
  461. case JVMTI_EVENT_BREAKPOINT:
  462. _breakpoint_posted = _breakpoint_posted && _single_stepping_posted;
  463. break;
  464. case JVMTI_EVENT_SINGLE_STEP:
  465. _single_stepping_posted = true;
  466. break;
  467. default:
  468. assert(false, "invalid event value passed");
  469. break;
  470. }
  471. return;
  472. }
  473. set_current_location(new_method_id, new_bci);
  474. _breakpoint_posted = false;
  475. _single_stepping_posted = false;
  476. }
  477. JvmtiFramePops* JvmtiEnvThreadState::get_frame_pops() {
  478. #ifdef ASSERT
  479. uint32_t debug_bits = 0;
  480. #endif
  481. assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
  482. "frame pop data only accessible from same thread or while suspended");
  483. if (_frame_pops == NULL) {
  484. _frame_pops = new JvmtiFramePops();
  485. assert(_frame_pops != NULL, "_frame_pops != NULL");
  486. }
  487. return _frame_pops;
  488. }
  489. bool JvmtiEnvThreadState::has_frame_pops() {
  490. return _frame_pops == NULL? false : (_frame_pops->length() > 0);
  491. }
  492. void JvmtiEnvThreadState::set_frame_pop(int frame_number) {
  493. #ifdef ASSERT
  494. uint32_t debug_bits = 0;
  495. #endif
  496. assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
  497. "frame pop data only accessible from same thread or while suspended");
  498. JvmtiFramePop fpop(frame_number);
  499. JvmtiEventController::set_frame_pop(this, fpop);
  500. }
  501. void JvmtiEnvThreadState::clear_frame_pop(int frame_number) {
  502. #ifdef ASSERT
  503. uint32_t debug_bits = 0;
  504. #endif
  505. assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
  506. "frame pop data only accessible from same thread or while suspended");
  507. JvmtiFramePop fpop(frame_number);
  508. JvmtiEventController::clear_frame_pop(this, fpop);
  509. }
  510. void JvmtiEnvThreadState::clear_to_frame_pop(int frame_number) {
  511. #ifdef ASSERT
  512. uint32_t debug_bits = 0;
  513. #endif
  514. assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
  515. "frame pop data only accessible from same thread or while suspended");
  516. JvmtiFramePop fpop(frame_number);
  517. JvmtiEventController::clear_to_frame_pop(this, fpop);
  518. }
  519. bool JvmtiEnvThreadState::is_frame_pop(int cur_frame_number) {
  520. #ifdef ASSERT
  521. uint32_t debug_bits = 0;
  522. #endif
  523. assert(get_thread() == Thread::current() || JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits),
  524. "frame pop data only accessible from same thread or while suspended");
  525. if (!get_thread()->is_interp_only_mode() || _frame_pops == NULL) {
  526. return false;
  527. }
  528. JvmtiFramePop fp(cur_frame_number);
  529. return get_frame_pops()->contains(fp);
  530. }
  531. class VM_GetCurrentLocation : public VM_Operation {
  532. private:
  533. JavaThread *_thread;
  534. jmethodID _method_id;
  535. int _bci;
  536. public:
  537. VM_GetCurrentLocation(JavaThread *thread) {
  538. _thread = thread;
  539. }
  540. VMOp_Type type() const { return VMOp_GetCurrentLocation; }
  541. void doit() {
  542. ResourceMark rmark; // _thread != Thread::current()
  543. RegisterMap rm(_thread, false);
  544. if (!_thread->is_exiting() && _thread->has_last_Java_frame()) {
  545. javaVFrame* vf = _thread->last_java_vframe(&rm);
  546. assert(vf != NULL, "must have last java frame");
  547. Method* method = vf->method();
  548. _method_id = method->jmethod_id();
  549. _bci = vf->bci();
  550. } else {
  551. _method_id = (jmethodID)NULL;
  552. _bci = 0;
  553. }
  554. }
  555. void get_current_location(jmethodID *method_id, int *bci) {
  556. }
  557. };
  558. void JvmtiEnvThreadState::reset_current_location(jvmtiEvent event_type, bool enabled) {
  559. assert(event_type == JVMTI_EVENT_SINGLE_STEP || event_type == JVMTI_EVENT_BREAKPOINT,
  560. "must be single-step or breakpoint event");
  561. if (enabled) {
  562. if (event_type == JVMTI_EVENT_SINGLE_STEP && _thread->has_last_Java_frame()) {
  563. jmethodID method_id;
  564. int bci;
  565. VM_GetCurrentLocation op(_thread);
  566. VMThread::execute(&op);
  567. op.get_current_location(&method_id, &bci);
  568. set_current_location(method_id, bci);
  569. }
  570. } else if (event_type == JVMTI_EVENT_SINGLE_STEP || !is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
  571. clear_current_location();
  572. }
  573. }
  574. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiEnvThreadState.hpp
  575. #ifndef SHARE_VM_PRIMS_JVMTIENVTHREADSTATE_HPP
  576. #define SHARE_VM_PRIMS_JVMTIENVTHREADSTATE_HPP
  577. #include "jvmtifiles/jvmti.h"
  578. #include "memory/allocation.hpp"
  579. #include "memory/allocation.inline.hpp"
  580. #include "oops/instanceKlass.hpp"
  581. #include "prims/jvmtiEventController.hpp"
  582. #include "utilities/globalDefinitions.hpp"
  583. #include "utilities/growableArray.hpp"
  584. class JvmtiEnv;
  585. class JvmtiFramePop VALUE_OBJ_CLASS_SPEC {
  586. private:
  587. int _frame_number;
  588. public:
  589. JvmtiFramePop() {}
  590. JvmtiFramePop(int frame_number) {
  591. assert(frame_number >= 0, "invalid frame number");
  592. _frame_number = frame_number;
  593. }
  594. int frame_number() { return _frame_number; }
  595. int above_on_stack(JvmtiFramePop& other) { return _frame_number > other._frame_number; }
  596. void print() PRODUCT_RETURN;
  597. };
  598. class JvmtiFramePops : public CHeapObj<mtInternal> {
  599. private:
  600. GrowableArray<int>* _pops;
  601. friend class JvmtiEventControllerPrivate;
  602. void set(JvmtiFramePop& fp);
  603. void clear(JvmtiFramePop& fp);
  604. int clear_to(JvmtiFramePop& fp);
  605. public:
  606. JvmtiFramePops();
  607. ~JvmtiFramePops();
  608. bool contains(JvmtiFramePop& fp) { return _pops->contains(fp.frame_number()); }
  609. int length() { return _pops->length(); }
  610. void print() PRODUCT_RETURN;
  611. };
  612. class JvmtiEnvThreadState : public CHeapObj<mtInternal> {
  613. private:
  614. friend class JvmtiEnv;
  615. JavaThread *_thread;
  616. JvmtiEnv *_env;
  617. JvmtiEnvThreadState *_next;
  618. jmethodID _current_method_id;
  619. int _current_bci;
  620. bool _breakpoint_posted;
  621. bool _single_stepping_posted;
  622. JvmtiEnvThreadEventEnable _event_enable;
  623. void *_agent_thread_local_storage_data; // per env and per thread agent allocated data.
  624. JvmtiFramePops *_frame_pops;
  625. inline void set_current_location(jmethodID method_id, int bci) {
  626. _current_method_id = method_id;
  627. _current_bci = bci;
  628. }
  629. friend class JvmtiEnvThreadStateIterator;
  630. JvmtiEnvThreadState* next() { return _next; }
  631. friend class JvmtiThreadState;
  632. void set_next(JvmtiEnvThreadState* link) { _next = link; }
  633. public:
  634. JvmtiEnvThreadState(JavaThread *thread, JvmtiEnvBase *env);
  635. ~JvmtiEnvThreadState();
  636. bool is_enabled(jvmtiEvent event_type) { return _event_enable.is_enabled(event_type); }
  637. JvmtiEnvThreadEventEnable *event_enable() { return &_event_enable; }
  638. void *get_agent_thread_local_storage_data() { return _agent_thread_local_storage_data; }
  639. void set_agent_thread_local_storage_data (void *data) { _agent_thread_local_storage_data = data; }
  640. void compare_and_set_current_location(Method* method, address location, jvmtiEvent event);
  641. void clear_current_location() { set_current_location((jmethodID)NULL, 0); }
  642. void reset_current_location(jvmtiEvent event, bool enabled);
  643. inline void set_breakpoint_posted() { _breakpoint_posted = true; }
  644. inline void set_single_stepping_posted() {
  645. _single_stepping_posted = true;
  646. }
  647. inline bool breakpoint_posted() { return _breakpoint_posted; }
  648. inline bool single_stepping_posted() {
  649. return _single_stepping_posted;
  650. }
  651. inline JavaThread *get_thread() { return _thread; }
  652. inline JvmtiEnv *get_env() { return _env; }
  653. JvmtiFramePops* get_frame_pops();
  654. bool has_frame_pops();
  655. bool is_frame_pop(int cur_stack_depth);
  656. void set_frame_pop(int frame_number);
  657. void clear_frame_pop(int frame_number);
  658. void clear_to_frame_pop(int frame_number);
  659. };
  660. #endif // SHARE_VM_PRIMS_JVMTIENVTHREADSTATE_HPP
  661. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiEventController.cpp
  662. #include "precompiled.hpp"
  663. #include "interpreter/interpreter.hpp"
  664. #include "jvmtifiles/jvmtiEnv.hpp"
  665. #include "memory/resourceArea.hpp"
  666. #include "prims/jvmtiEventController.hpp"
  667. #include "prims/jvmtiEventController.inline.hpp"
  668. #include "prims/jvmtiExport.hpp"
  669. #include "prims/jvmtiImpl.hpp"
  670. #include "prims/jvmtiThreadState.inline.hpp"
  671. #include "runtime/frame.hpp"
  672. #include "runtime/thread.hpp"
  673. #include "runtime/vframe.hpp"
  674. #include "runtime/vframe_hp.hpp"
  675. #include "runtime/vmThread.hpp"
  676. #include "runtime/vm_operations.hpp"
  677. PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  678. #ifdef JVMTI_TRACE
  679. #define EC_TRACE(out) do { \
  680. if (JvmtiTrace::trace_event_controller()) { \
  681. SafeResourceMark rm; \
  682. tty->print_cr out; \
  683. } \
  684. } while (0)
  685. #else
  686. #define EC_TRACE(out)
  687. #endif /*JVMTI_TRACE */
  688. static const jlong SINGLE_STEP_BIT = (((jlong)1) << (JVMTI_EVENT_SINGLE_STEP - TOTAL_MIN_EVENT_TYPE_VAL));
  689. static const jlong FRAME_POP_BIT = (((jlong)1) << (JVMTI_EVENT_FRAME_POP - TOTAL_MIN_EVENT_TYPE_VAL));
  690. static const jlong BREAKPOINT_BIT = (((jlong)1) << (JVMTI_EVENT_BREAKPOINT - TOTAL_MIN_EVENT_TYPE_VAL));
  691. static const jlong FIELD_ACCESS_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_ACCESS - TOTAL_MIN_EVENT_TYPE_VAL));
  692. static const jlong FIELD_MODIFICATION_BIT = (((jlong)1) << (JVMTI_EVENT_FIELD_MODIFICATION - TOTAL_MIN_EVENT_TYPE_VAL));
  693. static const jlong METHOD_ENTRY_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_ENTRY - TOTAL_MIN_EVENT_TYPE_VAL));
  694. static const jlong METHOD_EXIT_BIT = (((jlong)1) << (JVMTI_EVENT_METHOD_EXIT - TOTAL_MIN_EVENT_TYPE_VAL));
  695. static const jlong CLASS_FILE_LOAD_HOOK_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK - TOTAL_MIN_EVENT_TYPE_VAL));
  696. static const jlong NATIVE_METHOD_BIND_BIT = (((jlong)1) << (JVMTI_EVENT_NATIVE_METHOD_BIND - TOTAL_MIN_EVENT_TYPE_VAL));
  697. static const jlong VM_START_BIT = (((jlong)1) << (JVMTI_EVENT_VM_START - TOTAL_MIN_EVENT_TYPE_VAL));
  698. static const jlong VM_INIT_BIT = (((jlong)1) << (JVMTI_EVENT_VM_INIT - TOTAL_MIN_EVENT_TYPE_VAL));
  699. static const jlong VM_DEATH_BIT = (((jlong)1) << (JVMTI_EVENT_VM_DEATH - TOTAL_MIN_EVENT_TYPE_VAL));
  700. static const jlong CLASS_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  701. static const jlong CLASS_PREPARE_BIT = (((jlong)1) << (JVMTI_EVENT_CLASS_PREPARE - TOTAL_MIN_EVENT_TYPE_VAL));
  702. static const jlong THREAD_START_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_START - TOTAL_MIN_EVENT_TYPE_VAL));
  703. static const jlong THREAD_END_BIT = (((jlong)1) << (JVMTI_EVENT_THREAD_END - TOTAL_MIN_EVENT_TYPE_VAL));
  704. static const jlong EXCEPTION_THROW_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION - TOTAL_MIN_EVENT_TYPE_VAL));
  705. static const jlong EXCEPTION_CATCH_BIT = (((jlong)1) << (JVMTI_EVENT_EXCEPTION_CATCH - TOTAL_MIN_EVENT_TYPE_VAL));
  706. static const jlong MONITOR_CONTENDED_ENTER_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTER - TOTAL_MIN_EVENT_TYPE_VAL));
  707. static const jlong MONITOR_CONTENDED_ENTERED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED - TOTAL_MIN_EVENT_TYPE_VAL));
  708. static const jlong MONITOR_WAIT_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAIT - TOTAL_MIN_EVENT_TYPE_VAL));
  709. static const jlong MONITOR_WAITED_BIT = (((jlong)1) << (JVMTI_EVENT_MONITOR_WAITED - TOTAL_MIN_EVENT_TYPE_VAL));
  710. static const jlong DYNAMIC_CODE_GENERATED_BIT = (((jlong)1) << (JVMTI_EVENT_DYNAMIC_CODE_GENERATED - TOTAL_MIN_EVENT_TYPE_VAL));
  711. static const jlong DATA_DUMP_BIT = (((jlong)1) << (JVMTI_EVENT_DATA_DUMP_REQUEST - TOTAL_MIN_EVENT_TYPE_VAL));
  712. static const jlong COMPILED_METHOD_LOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_LOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  713. static const jlong COMPILED_METHOD_UNLOAD_BIT = (((jlong)1) << (JVMTI_EVENT_COMPILED_METHOD_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  714. static const jlong GARBAGE_COLLECTION_START_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_START - TOTAL_MIN_EVENT_TYPE_VAL));
  715. static const jlong GARBAGE_COLLECTION_FINISH_BIT = (((jlong)1) << (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH - TOTAL_MIN_EVENT_TYPE_VAL));
  716. static const jlong OBJECT_FREE_BIT = (((jlong)1) << (JVMTI_EVENT_OBJECT_FREE - TOTAL_MIN_EVENT_TYPE_VAL));
  717. static const jlong RESOURCE_EXHAUSTED_BIT = (((jlong)1) << (JVMTI_EVENT_RESOURCE_EXHAUSTED - TOTAL_MIN_EVENT_TYPE_VAL));
  718. static const jlong VM_OBJECT_ALLOC_BIT = (((jlong)1) << (JVMTI_EVENT_VM_OBJECT_ALLOC - TOTAL_MIN_EVENT_TYPE_VAL));
  719. static const jlong CLASS_UNLOAD_BIT = (((jlong)1) << (EXT_EVENT_CLASS_UNLOAD - TOTAL_MIN_EVENT_TYPE_VAL));
  720. static const jlong MONITOR_BITS = MONITOR_CONTENDED_ENTER_BIT | MONITOR_CONTENDED_ENTERED_BIT |
  721. MONITOR_WAIT_BIT | MONITOR_WAITED_BIT;
  722. static const jlong EXCEPTION_BITS = EXCEPTION_THROW_BIT | EXCEPTION_CATCH_BIT;
  723. static const jlong INTERP_EVENT_BITS = SINGLE_STEP_BIT | METHOD_ENTRY_BIT | METHOD_EXIT_BIT |
  724. FRAME_POP_BIT | FIELD_ACCESS_BIT | FIELD_MODIFICATION_BIT;
  725. static const jlong THREAD_FILTERED_EVENT_BITS = INTERP_EVENT_BITS | EXCEPTION_BITS | MONITOR_BITS |
  726. BREAKPOINT_BIT | CLASS_LOAD_BIT | CLASS_PREPARE_BIT | THREAD_END_BIT;
  727. static const jlong NEED_THREAD_LIFE_EVENTS = THREAD_FILTERED_EVENT_BITS | THREAD_START_BIT;
  728. static const jlong EARLY_EVENT_BITS = CLASS_FILE_LOAD_HOOK_BIT |
  729. VM_START_BIT | VM_INIT_BIT | VM_DEATH_BIT | NATIVE_METHOD_BIND_BIT |
  730. THREAD_START_BIT | THREAD_END_BIT |
  731. DYNAMIC_CODE_GENERATED_BIT;
  732. static const jlong GLOBAL_EVENT_BITS = ~THREAD_FILTERED_EVENT_BITS;
  733. static const jlong SHOULD_POST_ON_EXCEPTIONS_BITS = EXCEPTION_BITS | METHOD_EXIT_BIT | FRAME_POP_BIT;
  734. JvmtiEventEnabled::JvmtiEventEnabled() {
  735. clear();
  736. }
  737. void JvmtiEventEnabled::clear() {
  738. _enabled_bits = 0;
  739. #ifndef PRODUCT
  740. _init_guard = JEE_INIT_GUARD;
  741. #endif
  742. }
  743. void JvmtiEventEnabled::set_enabled(jvmtiEvent event_type, bool enabled) {
  744. jlong bits = get_bits();
  745. jlong mask = bit_for(event_type);
  746. if (enabled) {
  747. bits |= mask;
  748. } else {
  749. bits &= ~mask;
  750. }
  751. set_bits(bits);
  752. }
  753. JvmtiEnvThreadEventEnable::JvmtiEnvThreadEventEnable() {
  754. _event_user_enabled.clear();
  755. _event_enabled.clear();
  756. }
  757. JvmtiEnvThreadEventEnable::~JvmtiEnvThreadEventEnable() {
  758. _event_user_enabled.clear();
  759. _event_enabled.clear();
  760. }
  761. JvmtiThreadEventEnable::JvmtiThreadEventEnable() {
  762. _event_enabled.clear();
  763. }
  764. JvmtiThreadEventEnable::~JvmtiThreadEventEnable() {
  765. _event_enabled.clear();
  766. }
  767. JvmtiEnvEventEnable::JvmtiEnvEventEnable() {
  768. _event_user_enabled.clear();
  769. _event_callback_enabled.clear();
  770. _event_enabled.clear();
  771. }
  772. JvmtiEnvEventEnable::~JvmtiEnvEventEnable() {
  773. _event_user_enabled.clear();
  774. _event_callback_enabled.clear();
  775. _event_enabled.clear();
  776. }
  777. class VM_EnterInterpOnlyMode : public VM_Operation {
  778. private:
  779. JvmtiThreadState *_state;
  780. public:
  781. VM_EnterInterpOnlyMode(JvmtiThreadState *state);
  782. bool allow_nested_vm_operations() const { return true; }
  783. VMOp_Type type() const { return VMOp_EnterInterpOnlyMode; }
  784. void doit();
  785. bool can_be_deoptimized(vframe* vf) {
  786. return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());
  787. }
  788. };
  789. VM_EnterInterpOnlyMode::VM_EnterInterpOnlyMode(JvmtiThreadState *state)
  790. : _state(state)
  791. {
  792. }
  793. void VM_EnterInterpOnlyMode::doit() {
  794. _state->invalidate_cur_stack_depth();
  795. _state->enter_interp_only_mode();
  796. JavaThread *thread = _state->get_thread();
  797. if (thread->has_last_Java_frame()) {
  798. int num_marked = 0;
  799. ResourceMark resMark;
  800. RegisterMap rm(thread, false);
  801. for (vframe* vf = thread->last_java_vframe(&rm); vf; vf = vf->sender()) {
  802. if (can_be_deoptimized(vf)) {
  803. ((compiledVFrame*) vf)->code()->mark_for_deoptimization();
  804. ++num_marked;
  805. }
  806. }
  807. if (num_marked > 0) {
  808. VM_Deoptimize op;
  809. VMThread::execute(&op);
  810. }
  811. }
  812. }
  813. class VM_ChangeSingleStep : public VM_Operation {
  814. private:
  815. bool _on;
  816. public:
  817. VM_ChangeSingleStep(bool on);
  818. VMOp_Type type() const { return VMOp_ChangeSingleStep; }
  819. bool allow_nested_vm_operations() const { return true; }
  820. void doit(); // method definition is after definition of JvmtiEventControllerPrivate because of scoping
  821. };
  822. VM_ChangeSingleStep::VM_ChangeSingleStep(bool on)
  823. : _on(on != 0)
  824. {
  825. }
  826. class JvmtiEventControllerPrivate : public AllStatic {
  827. static bool _initialized;
  828. public:
  829. static void set_should_post_single_step(bool on);
  830. static void enter_interp_only_mode(JvmtiThreadState *state);
  831. static void leave_interp_only_mode(JvmtiThreadState *state);
  832. static void recompute_enabled();
  833. static jlong recompute_env_enabled(JvmtiEnvBase* env);
  834. static jlong recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state);
  835. static jlong recompute_thread_enabled(JvmtiThreadState *state);
  836. static void event_init();
  837. static void set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
  838. jvmtiEvent event_type, bool enabled);
  839. static void set_event_callbacks(JvmtiEnvBase *env,
  840. const jvmtiEventCallbacks* callbacks,
  841. jint size_of_callbacks);
  842. static void set_extension_event_callback(JvmtiEnvBase *env,
  843. jint extension_event_index,
  844. jvmtiExtensionEvent callback);
  845. static void set_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
  846. static void clear_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
  847. static void clear_to_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
  848. static void change_field_watch(jvmtiEvent event_type, bool added);
  849. static void thread_started(JavaThread *thread);
  850. static void thread_ended(JavaThread *thread);
  851. static void env_initialize(JvmtiEnvBase *env);
  852. static void env_dispose(JvmtiEnvBase *env);
  853. static void vm_start();
  854. static void vm_init();
  855. static void vm_death();
  856. static void trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed);
  857. static void trace_changed(jlong now_enabled, jlong changed);
  858. };
  859. bool JvmtiEventControllerPrivate::_initialized = false;
  860. void JvmtiEventControllerPrivate::set_should_post_single_step(bool on) {
  861. JvmtiExport::set_should_post_single_step(on);
  862. }
  863. void VM_ChangeSingleStep::doit() {
  864. JvmtiEventControllerPrivate::set_should_post_single_step(_on);
  865. if (_on) {
  866. Interpreter::notice_safepoints();
  867. }
  868. }
  869. void JvmtiEventControllerPrivate::enter_interp_only_mode(JvmtiThreadState *state) {
  870. EC_TRACE(("JVMTI [%s] # Entering interpreter only mode",
  871. JvmtiTrace::safe_get_thread_name(state->get_thread())));
  872. VM_EnterInterpOnlyMode op(state);
  873. VMThread::execute(&op);
  874. }
  875. void
  876. JvmtiEventControllerPrivate::leave_interp_only_mode(JvmtiThreadState *state) {
  877. EC_TRACE(("JVMTI [%s] # Leaving interpreter only mode",
  878. JvmtiTrace::safe_get_thread_name(state->get_thread())));
  879. state->leave_interp_only_mode();
  880. }
  881. void
  882. JvmtiEventControllerPrivate::trace_changed(JvmtiThreadState *state, jlong now_enabled, jlong changed) {
  883. #ifdef JVMTI_TRACE
  884. if (JvmtiTrace::trace_event_controller()) {
  885. SafeResourceMark rm;
  886. for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
  887. jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
  888. if (changed & bit) {
  889. tty->print_cr("JVMTI [%s] # %s event %s",
  890. JvmtiTrace::safe_get_thread_name(state->get_thread()),
  891. (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
  892. }
  893. }
  894. }
  895. #endif /*JVMTI_TRACE */
  896. }
  897. void
  898. JvmtiEventControllerPrivate::trace_changed(jlong now_enabled, jlong changed) {
  899. #ifdef JVMTI_TRACE
  900. if (JvmtiTrace::trace_event_controller()) {
  901. SafeResourceMark rm;
  902. for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
  903. jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
  904. if (changed & bit) {
  905. tty->print_cr("JVMTI [-] # %s event %s",
  906. (now_enabled & bit)? "Enabling" : "Disabling", JvmtiTrace::event_name((jvmtiEvent)ei));
  907. }
  908. }
  909. }
  910. #endif /*JVMTI_TRACE */
  911. }
  912. jlong
  913. JvmtiEventControllerPrivate::recompute_env_enabled(JvmtiEnvBase* env) {
  914. jlong was_enabled = env->env_event_enable()->_event_enabled.get_bits();
  915. jlong now_enabled =
  916. env->env_event_enable()->_event_callback_enabled.get_bits() &
  917. env->env_event_enable()->_event_user_enabled.get_bits();
  918. switch (JvmtiEnv::get_phase()) {
  919. case JVMTI_PHASE_PRIMORDIAL:
  920. case JVMTI_PHASE_ONLOAD:
  921. now_enabled &= (EARLY_EVENT_BITS & ~THREAD_FILTERED_EVENT_BITS);
  922. break;
  923. case JVMTI_PHASE_START:
  924. now_enabled &= EARLY_EVENT_BITS;
  925. break;
  926. case JVMTI_PHASE_LIVE:
  927. break;
  928. case JVMTI_PHASE_DEAD:
  929. now_enabled = 0;
  930. break;
  931. default:
  932. assert(false, "no other phases - sanity check");
  933. break;
  934. }
  935. env->env_event_enable()->_event_enabled.set_bits(now_enabled);
  936. trace_changed(now_enabled, (now_enabled ^ was_enabled) & ~THREAD_FILTERED_EVENT_BITS);
  937. return now_enabled;
  938. }
  939. jlong
  940. JvmtiEventControllerPrivate::recompute_env_thread_enabled(JvmtiEnvThreadState* ets, JvmtiThreadState* state) {
  941. JvmtiEnv *env = ets->get_env();
  942. jlong was_enabled = ets->event_enable()->_event_enabled.get_bits();
  943. jlong now_enabled = THREAD_FILTERED_EVENT_BITS &
  944. env->env_event_enable()->_event_callback_enabled.get_bits() &
  945. (env->env_event_enable()->_event_user_enabled.get_bits() |
  946. ets->event_enable()->_event_user_enabled.get_bits());
  947. if (!ets->has_frame_pops()) {
  948. now_enabled &= ~FRAME_POP_BIT;
  949. }
  950. if (*((int *)JvmtiExport::get_field_access_count_addr()) == 0) {
  951. now_enabled &= ~FIELD_ACCESS_BIT;
  952. }
  953. if (*((int *)JvmtiExport::get_field_modification_count_addr()) == 0) {
  954. now_enabled &= ~FIELD_MODIFICATION_BIT;
  955. }
  956. switch (JvmtiEnv::get_phase()) {
  957. case JVMTI_PHASE_DEAD:
  958. now_enabled = 0;
  959. break;
  960. }
  961. if (now_enabled != was_enabled) {
  962. ets->event_enable()->_event_enabled.set_bits(now_enabled);
  963. jlong changed = now_enabled ^ was_enabled;
  964. if (changed & SINGLE_STEP_BIT) {
  965. ets->reset_current_location(JVMTI_EVENT_SINGLE_STEP, (now_enabled & SINGLE_STEP_BIT) != 0);
  966. }
  967. if (changed & BREAKPOINT_BIT) {
  968. ets->reset_current_location(JVMTI_EVENT_BREAKPOINT, (now_enabled & BREAKPOINT_BIT) != 0);
  969. }
  970. trace_changed(state, now_enabled, changed);
  971. }
  972. return now_enabled;
  973. }
  974. jlong
  975. JvmtiEventControllerPrivate::recompute_thread_enabled(JvmtiThreadState *state) {
  976. if (state == NULL) {
  977. return (jlong)0;
  978. }
  979. jlong was_any_env_enabled = state->thread_event_enable()->_event_enabled.get_bits();
  980. jlong any_env_enabled = 0;
  981. {
  982. JvmtiEnvThreadStateIterator it(state);
  983. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  984. any_env_enabled |= recompute_env_thread_enabled(ets, state);
  985. }
  986. }
  987. if (any_env_enabled != was_any_env_enabled) {
  988. state->thread_event_enable()->_event_enabled.set_bits(any_env_enabled);
  989. bool should_be_interp = (any_env_enabled & INTERP_EVENT_BITS) != 0;
  990. bool is_now_interp = state->is_interp_only_mode();
  991. if (should_be_interp != is_now_interp) {
  992. if (should_be_interp) {
  993. enter_interp_only_mode(state);
  994. } else {
  995. leave_interp_only_mode(state);
  996. }
  997. }
  998. bool should_post_on_exceptions = (any_env_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0;
  999. state->set_should_post_on_exceptions(should_post_on_exceptions);
  1000. }
  1001. return any_env_enabled;
  1002. }
  1003. void
  1004. JvmtiEventControllerPrivate::recompute_enabled() {
  1005. assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
  1006. jlong was_any_env_thread_enabled = JvmtiEventController::_universal_global_event_enabled.get_bits();
  1007. jlong any_env_thread_enabled = 0;
  1008. EC_TRACE(("JVMTI [-] # recompute enabled - before %llx", was_any_env_thread_enabled));
  1009. JvmtiEnvIterator it;
  1010. for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
  1011. any_env_thread_enabled |= recompute_env_enabled(env);
  1012. }
  1013. if ( (any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) != 0 &&
  1014. (was_any_env_thread_enabled & THREAD_FILTERED_EVENT_BITS) == 0) {
  1015. assert(JvmtiEnv::is_vm_live() || (JvmtiEnv::get_phase()==JVMTI_PHASE_START),
  1016. "thread filtered events should not be enabled when VM not in start or live phase");
  1017. {
  1018. MutexLocker mu(Threads_lock); //hold the Threads_lock for the iteration
  1019. for (JavaThread *tp = Threads::first(); tp != NULL; tp = tp->next()) {
  1020. JvmtiThreadState::state_for_while_locked(tp); // create the thread state if missing
  1021. }
  1022. }// release Threads_lock
  1023. }
  1024. for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
  1025. any_env_thread_enabled |= recompute_thread_enabled(state);
  1026. }
  1027. jlong delta = any_env_thread_enabled ^ was_any_env_thread_enabled;
  1028. if (delta != 0) {
  1029. JvmtiExport::set_should_post_field_access((any_env_thread_enabled & FIELD_ACCESS_BIT) != 0);
  1030. JvmtiExport::set_should_post_field_modification((any_env_thread_enabled & FIELD_MODIFICATION_BIT) != 0);
  1031. JvmtiExport::set_should_post_class_load((any_env_thread_enabled & CLASS_LOAD_BIT) != 0);
  1032. JvmtiExport::set_should_post_class_file_load_hook((any_env_thread_enabled & CLASS_FILE_LOAD_HOOK_BIT) != 0);
  1033. JvmtiExport::set_should_post_native_method_bind((any_env_thread_enabled & NATIVE_METHOD_BIND_BIT) != 0);
  1034. JvmtiExport::set_should_post_dynamic_code_generated((any_env_thread_enabled & DYNAMIC_CODE_GENERATED_BIT) != 0);
  1035. JvmtiExport::set_should_post_data_dump((any_env_thread_enabled & DATA_DUMP_BIT) != 0);
  1036. JvmtiExport::set_should_post_class_prepare((any_env_thread_enabled & CLASS_PREPARE_BIT) != 0);
  1037. JvmtiExport::set_should_post_class_unload((any_env_thread_enabled & CLASS_UNLOAD_BIT) != 0);
  1038. JvmtiExport::set_should_post_monitor_contended_enter((any_env_thread_enabled & MONITOR_CONTENDED_ENTER_BIT) != 0);
  1039. JvmtiExport::set_should_post_monitor_contended_entered((any_env_thread_enabled & MONITOR_CONTENDED_ENTERED_BIT) != 0);
  1040. JvmtiExport::set_should_post_monitor_wait((any_env_thread_enabled & MONITOR_WAIT_BIT) != 0);
  1041. JvmtiExport::set_should_post_monitor_waited((any_env_thread_enabled & MONITOR_WAITED_BIT) != 0);
  1042. JvmtiExport::set_should_post_garbage_collection_start((any_env_thread_enabled & GARBAGE_COLLECTION_START_BIT) != 0);
  1043. JvmtiExport::set_should_post_garbage_collection_finish((any_env_thread_enabled & GARBAGE_COLLECTION_FINISH_BIT) != 0);
  1044. JvmtiExport::set_should_post_object_free((any_env_thread_enabled & OBJECT_FREE_BIT) != 0);
  1045. JvmtiExport::set_should_post_resource_exhausted((any_env_thread_enabled & RESOURCE_EXHAUSTED_BIT) != 0);
  1046. JvmtiExport::set_should_post_compiled_method_load((any_env_thread_enabled & COMPILED_METHOD_LOAD_BIT) != 0);
  1047. JvmtiExport::set_should_post_compiled_method_unload((any_env_thread_enabled & COMPILED_METHOD_UNLOAD_BIT) != 0);
  1048. JvmtiExport::set_should_post_vm_object_alloc((any_env_thread_enabled & VM_OBJECT_ALLOC_BIT) != 0);
  1049. JvmtiExport::set_should_post_thread_life((any_env_thread_enabled & NEED_THREAD_LIFE_EVENTS) != 0);
  1050. if (delta & SINGLE_STEP_BIT) {
  1051. switch (JvmtiEnv::get_phase()) {
  1052. case JVMTI_PHASE_DEAD:
  1053. break;
  1054. case JVMTI_PHASE_LIVE: {
  1055. VM_ChangeSingleStep op((any_env_thread_enabled & SINGLE_STEP_BIT) != 0);
  1056. VMThread::execute(&op);
  1057. break;
  1058. }
  1059. default:
  1060. assert(false, "should never come here before live phase");
  1061. break;
  1062. }
  1063. }
  1064. JvmtiEventController::_universal_global_event_enabled.set_bits(any_env_thread_enabled);
  1065. JvmtiExport::set_should_post_on_exceptions((any_env_thread_enabled & SHOULD_POST_ON_EXCEPTIONS_BITS) != 0);
  1066. }
  1067. EC_TRACE(("JVMTI [-] # recompute enabled - after %llx", any_env_thread_enabled));
  1068. }
  1069. void
  1070. JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
  1071. assert(thread->is_Java_thread(), "Must be JavaThread");
  1072. assert(thread == Thread::current(), "must be current thread");
  1073. assert(JvmtiEnvBase::environments_might_exist(), "to enter event controller, JVM TI environments must exist");
  1074. EC_TRACE(("JVMTI [%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));
  1075. if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
  1076. MutexLocker mu(JvmtiThreadState_lock);
  1077. JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
  1078. if (state != NULL) { // skip threads with no JVMTI thread state
  1079. recompute_thread_enabled(state);
  1080. }
  1081. }
  1082. }
  1083. void
  1084. JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) {
  1085. assert(JvmtiThreadState_lock->is_locked(), "sanity check");
  1086. EC_TRACE(("JVMTI [%s] # thread ended", JvmtiTrace::safe_get_thread_name(thread)));
  1087. JvmtiThreadState *state = thread->jvmti_thread_state();
  1088. assert(state != NULL, "else why are we here?");
  1089. delete state;
  1090. }
  1091. void JvmtiEventControllerPrivate::set_event_callbacks(JvmtiEnvBase *env,
  1092. const jvmtiEventCallbacks* callbacks,
  1093. jint size_of_callbacks) {
  1094. assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
  1095. EC_TRACE(("JVMTI [*] # set event callbacks"));
  1096. env->set_event_callbacks(callbacks, size_of_callbacks);
  1097. jlong enabled_bits = 0;
  1098. for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
  1099. jvmtiEvent evt_t = (jvmtiEvent)ei;
  1100. if (env->has_callback(evt_t)) {
  1101. enabled_bits |= JvmtiEventEnabled::bit_for(evt_t);
  1102. }
  1103. }
  1104. env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
  1105. recompute_enabled();
  1106. }
  1107. void
  1108. JvmtiEventControllerPrivate::set_extension_event_callback(JvmtiEnvBase *env,
  1109. jint extension_event_index,
  1110. jvmtiExtensionEvent callback)
  1111. {
  1112. assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
  1113. EC_TRACE(("JVMTI [*] # set extension event callback"));
  1114. assert(extension_event_index >= (jint)EXT_MIN_EVENT_TYPE_VAL &&
  1115. extension_event_index <= (jint)EXT_MAX_EVENT_TYPE_VAL, "sanity check");
  1116. jvmtiEvent event_type = (jvmtiEvent)extension_event_index;
  1117. bool enabling = (callback != NULL) && (env->is_valid());
  1118. env->env_event_enable()->set_user_enabled(event_type, enabling);
  1119. jvmtiExtEventCallbacks* ext_callbacks = env->ext_callbacks();
  1120. switch (extension_event_index) {
  1121. case EXT_EVENT_CLASS_UNLOAD :
  1122. ext_callbacks->ClassUnload = callback;
  1123. break;
  1124. default:
  1125. ShouldNotReachHere();
  1126. }
  1127. jlong enabled_bits = env->env_event_enable()->_event_callback_enabled.get_bits();
  1128. jlong bit_for = JvmtiEventEnabled::bit_for(event_type);
  1129. if (enabling) {
  1130. enabled_bits |= bit_for;
  1131. } else {
  1132. enabled_bits &= ~bit_for;
  1133. }
  1134. env->env_event_enable()->_event_callback_enabled.set_bits(enabled_bits);
  1135. recompute_enabled();
  1136. }
  1137. void
  1138. JvmtiEventControllerPrivate::env_initialize(JvmtiEnvBase *env) {
  1139. assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
  1140. EC_TRACE(("JVMTI [*] # env initialize"));
  1141. if (JvmtiEnvBase::is_vm_live()) {
  1142. event_init();
  1143. }
  1144. env->initialize();
  1145. for (JvmtiThreadState *state = JvmtiThreadState::first(); state != NULL; state = state->next()) {
  1146. state->add_env(env);
  1147. assert((JvmtiEnv*)(state->env_thread_state(env)->get_env()) == env, "sanity check");
  1148. }
  1149. JvmtiEventControllerPrivate::recompute_enabled();
  1150. }
  1151. void
  1152. JvmtiEventControllerPrivate::env_dispose(JvmtiEnvBase *env) {
  1153. assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
  1154. EC_TRACE(("JVMTI [*] # env dispose"));
  1155. set_event_callbacks(env, NULL, 0);
  1156. for (jint extension_event_index = EXT_MIN_EVENT_TYPE_VAL;
  1157. extension_event_index <= EXT_MAX_EVENT_TYPE_VAL;
  1158. ++extension_event_index) {
  1159. set_extension_event_callback(env, extension_event_index, NULL);
  1160. }
  1161. env->env_dispose();
  1162. }
  1163. void
  1164. JvmtiEventControllerPrivate::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
  1165. jvmtiEvent event_type, bool enabled) {
  1166. assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
  1167. EC_TRACE(("JVMTI [%s] # user %s event %s",
  1168. thread==NULL? "ALL": JvmtiTrace::safe_get_thread_name(thread),
  1169. enabled? "enabled" : "disabled", JvmtiTrace::event_name(event_type)));
  1170. if (thread == NULL) {
  1171. env->env_event_enable()->set_user_enabled(event_type, enabled);
  1172. } else {
  1173. JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
  1174. if (state != NULL) {
  1175. state->env_thread_state(env)->event_enable()->set_user_enabled(event_type, enabled);
  1176. }
  1177. }
  1178. recompute_enabled();
  1179. }
  1180. void
  1181. JvmtiEventControllerPrivate::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
  1182. EC_TRACE(("JVMTI [%s] # set frame pop - frame=%d",
  1183. JvmtiTrace::safe_get_thread_name(ets->get_thread()),
  1184. fpop.frame_number() ));
  1185. ets->get_frame_pops()->set(fpop);
  1186. recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
  1187. }
  1188. void
  1189. JvmtiEventControllerPrivate::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
  1190. EC_TRACE(("JVMTI [%s] # clear frame pop - frame=%d",
  1191. JvmtiTrace::safe_get_thread_name(ets->get_thread()),
  1192. fpop.frame_number() ));
  1193. ets->get_frame_pops()->clear(fpop);
  1194. recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
  1195. }
  1196. void
  1197. JvmtiEventControllerPrivate::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
  1198. int cleared_cnt = ets->get_frame_pops()->clear_to(fpop);
  1199. EC_TRACE(("JVMTI [%s] # clear to frame pop - frame=%d, count=%d",
  1200. JvmtiTrace::safe_get_thread_name(ets->get_thread()),
  1201. fpop.frame_number(),
  1202. cleared_cnt ));
  1203. if (cleared_cnt > 0) {
  1204. recompute_thread_enabled(ets->get_thread()->jvmti_thread_state());
  1205. }
  1206. }
  1207. void
  1208. JvmtiEventControllerPrivate::change_field_watch(jvmtiEvent event_type, bool added) {
  1209. int *count_addr;
  1210. switch (event_type) {
  1211. case JVMTI_EVENT_FIELD_MODIFICATION:
  1212. count_addr = (int *)JvmtiExport::get_field_modification_count_addr();
  1213. break;
  1214. case JVMTI_EVENT_FIELD_ACCESS:
  1215. count_addr = (int *)JvmtiExport::get_field_access_count_addr();
  1216. break;
  1217. default:
  1218. assert(false, "incorrect event");
  1219. return;
  1220. }
  1221. EC_TRACE(("JVMTI [-] # change field watch - %s %s count=%d",
  1222. event_type==JVMTI_EVENT_FIELD_MODIFICATION? "modification" : "access",
  1223. added? "add" : "remove",
  1224. if (added) {
  1225. (*count_addr)++;
  1226. if (*count_addr == 1) {
  1227. recompute_enabled();
  1228. }
  1229. } else {
  1230. if (*count_addr > 0) {
  1231. (*count_addr)--;
  1232. if (*count_addr == 0) {
  1233. recompute_enabled();
  1234. }
  1235. } else {
  1236. assert(false, "field watch out of phase");
  1237. }
  1238. }
  1239. }
  1240. void
  1241. JvmtiEventControllerPrivate::event_init() {
  1242. assert(JvmtiThreadState_lock->is_locked(), "sanity check");
  1243. if (_initialized) {
  1244. return;
  1245. }
  1246. EC_TRACE(("JVMTI [-] # VM live"));
  1247. #ifdef ASSERT
  1248. for (int ei = JVMTI_MIN_EVENT_TYPE_VAL; ei <= JVMTI_MAX_EVENT_TYPE_VAL; ++ei) {
  1249. jlong bit = JvmtiEventEnabled::bit_for((jvmtiEvent)ei);
  1250. assert(((THREAD_FILTERED_EVENT_BITS & bit) != 0) == JvmtiUtil::event_threaded(ei),
  1251. "thread filtered event list does not match");
  1252. }
  1253. #endif
  1254. _initialized = true;
  1255. }
  1256. void
  1257. JvmtiEventControllerPrivate::vm_start() {
  1258. JvmtiEventControllerPrivate::recompute_enabled();
  1259. }
  1260. void
  1261. JvmtiEventControllerPrivate::vm_init() {
  1262. event_init();
  1263. JvmtiEventControllerPrivate::recompute_enabled();
  1264. }
  1265. void
  1266. JvmtiEventControllerPrivate::vm_death() {
  1267. JvmtiEventControllerPrivate::recompute_enabled();
  1268. }
  1269. JvmtiEventEnabled JvmtiEventController::_universal_global_event_enabled;
  1270. bool
  1271. JvmtiEventController::is_global_event(jvmtiEvent event_type) {
  1272. assert(is_valid_event_type(event_type), "invalid event type");
  1273. jlong bit_for = ((jlong)1) << (event_type - TOTAL_MIN_EVENT_TYPE_VAL);
  1274. return((bit_for & GLOBAL_EVENT_BITS)!=0);
  1275. }
  1276. void
  1277. JvmtiEventController::set_user_enabled(JvmtiEnvBase *env, JavaThread *thread, jvmtiEvent event_type, bool enabled) {
  1278. if (Threads::number_of_threads() == 0) {
  1279. JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
  1280. } else {
  1281. MutexLocker mu(JvmtiThreadState_lock);
  1282. JvmtiEventControllerPrivate::set_user_enabled(env, thread, event_type, enabled);
  1283. }
  1284. }
  1285. void
  1286. JvmtiEventController::set_event_callbacks(JvmtiEnvBase *env,
  1287. const jvmtiEventCallbacks* callbacks,
  1288. jint size_of_callbacks) {
  1289. if (Threads::number_of_threads() == 0) {
  1290. JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
  1291. } else {
  1292. MutexLocker mu(JvmtiThreadState_lock);
  1293. JvmtiEventControllerPrivate::set_event_callbacks(env, callbacks, size_of_callbacks);
  1294. }
  1295. }
  1296. void
  1297. JvmtiEventController::set_extension_event_callback(JvmtiEnvBase *env,
  1298. jint extension_event_index,
  1299. jvmtiExtensionEvent callback) {
  1300. if (Threads::number_of_threads() == 0) {
  1301. JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
  1302. } else {
  1303. MutexLocker mu(JvmtiThreadState_lock);
  1304. JvmtiEventControllerPrivate::set_extension_event_callback(env, extension_event_index, callback);
  1305. }
  1306. }
  1307. void
  1308. JvmtiEventController::set_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
  1309. MutexLocker mu(JvmtiThreadState_lock);
  1310. JvmtiEventControllerPrivate::set_frame_pop(ets, fpop);
  1311. }
  1312. void
  1313. JvmtiEventController::clear_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
  1314. MutexLocker mu(JvmtiThreadState_lock);
  1315. JvmtiEventControllerPrivate::clear_frame_pop(ets, fpop);
  1316. }
  1317. void
  1318. JvmtiEventController::clear_to_frame_pop(JvmtiEnvThreadState *ets, JvmtiFramePop fpop) {
  1319. MutexLocker mu(JvmtiThreadState_lock);
  1320. JvmtiEventControllerPrivate::clear_to_frame_pop(ets, fpop);
  1321. }
  1322. void
  1323. JvmtiEventController::change_field_watch(jvmtiEvent event_type, bool added) {
  1324. MutexLocker mu(JvmtiThreadState_lock);
  1325. JvmtiEventControllerPrivate::change_field_watch(event_type, added);
  1326. }
  1327. void
  1328. JvmtiEventController::thread_started(JavaThread *thread) {
  1329. JvmtiEventControllerPrivate::thread_started(thread);
  1330. }
  1331. void
  1332. JvmtiEventController::thread_ended(JavaThread *thread) {
  1333. JvmtiEventControllerPrivate::thread_ended(thread);
  1334. }
  1335. void
  1336. JvmtiEventController::env_initialize(JvmtiEnvBase *env) {
  1337. if (Threads::number_of_threads() == 0) {
  1338. JvmtiEventControllerPrivate::env_initialize(env);
  1339. } else {
  1340. MutexLocker mu(JvmtiThreadState_lock);
  1341. JvmtiEventControllerPrivate::env_initialize(env);
  1342. }
  1343. }
  1344. void
  1345. JvmtiEventController::env_dispose(JvmtiEnvBase *env) {
  1346. if (Threads::number_of_threads() == 0) {
  1347. JvmtiEventControllerPrivate::env_dispose(env);
  1348. } else {
  1349. MutexLocker mu(JvmtiThreadState_lock);
  1350. JvmtiEventControllerPrivate::env_dispose(env);
  1351. }
  1352. }
  1353. void
  1354. JvmtiEventController::vm_start() {
  1355. if (JvmtiEnvBase::environments_might_exist()) {
  1356. MutexLocker mu(JvmtiThreadState_lock);
  1357. JvmtiEventControllerPrivate::vm_start();
  1358. }
  1359. }
  1360. void
  1361. JvmtiEventController::vm_init() {
  1362. if (JvmtiEnvBase::environments_might_exist()) {
  1363. MutexLocker mu(JvmtiThreadState_lock);
  1364. JvmtiEventControllerPrivate::vm_init();
  1365. }
  1366. }
  1367. void
  1368. JvmtiEventController::vm_death() {
  1369. if (JvmtiEnvBase::environments_might_exist()) {
  1370. MutexLocker mu(JvmtiThreadState_lock);
  1371. JvmtiEventControllerPrivate::vm_death();
  1372. }
  1373. }
  1374. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiEventController.hpp
  1375. #ifndef SHARE_VM_PRIMS_JVMTIEVENTCONTROLLER_HPP
  1376. #define SHARE_VM_PRIMS_JVMTIEVENTCONTROLLER_HPP
  1377. #include "jvmtifiles/jvmti.h"
  1378. #include "memory/allocation.hpp"
  1379. #include "memory/allocation.inline.hpp"
  1380. #include "utilities/globalDefinitions.hpp"
  1381. class JvmtiEventControllerPrivate;
  1382. class JvmtiEventController;
  1383. class JvmtiEnvThreadState;
  1384. class JvmtiFramePop;
  1385. class JvmtiEnvBase;
  1386. typedef enum {
  1387. EXT_EVENT_CLASS_UNLOAD = JVMTI_MIN_EVENT_TYPE_VAL-1,
  1388. EXT_MIN_EVENT_TYPE_VAL = EXT_EVENT_CLASS_UNLOAD,
  1389. EXT_MAX_EVENT_TYPE_VAL = EXT_EVENT_CLASS_UNLOAD
  1390. } jvmtiExtEvent;
  1391. typedef struct {
  1392. jvmtiExtensionEvent ClassUnload;
  1393. } jvmtiExtEventCallbacks;
  1394. const int TOTAL_MIN_EVENT_TYPE_VAL = EXT_MIN_EVENT_TYPE_VAL;
  1395. const int TOTAL_MAX_EVENT_TYPE_VAL = JVMTI_MAX_EVENT_TYPE_VAL;
  1396. class JvmtiEventEnabled VALUE_OBJ_CLASS_SPEC {
  1397. private:
  1398. friend class JvmtiEventControllerPrivate;
  1399. jlong _enabled_bits;
  1400. #ifndef PRODUCT
  1401. enum {
  1402. JEE_INIT_GUARD = 0xEAD0
  1403. } _init_guard;
  1404. #endif
  1405. static jlong bit_for(jvmtiEvent event_type);
  1406. jlong get_bits();
  1407. void set_bits(jlong bits);
  1408. public:
  1409. JvmtiEventEnabled();
  1410. void clear();
  1411. bool is_enabled(jvmtiEvent event_type);
  1412. void set_enabled(jvmtiEvent event_type, bool enabled);
  1413. };
  1414. class JvmtiEnvThreadEventEnable VALUE_OBJ_CLASS_SPEC {
  1415. private:
  1416. friend class JvmtiEventControllerPrivate;
  1417. JvmtiEventEnabled _event_user_enabled;
  1418. JvmtiEventEnabled _event_enabled;
  1419. public:
  1420. JvmtiEnvThreadEventEnable();
  1421. ~JvmtiEnvThreadEventEnable();
  1422. bool is_enabled(jvmtiEvent event_type);
  1423. void set_user_enabled(jvmtiEvent event_type, bool enabled);
  1424. };
  1425. class JvmtiThreadEventEnable VALUE_OBJ_CLASS_SPEC {
  1426. private:
  1427. friend class JvmtiEventControllerPrivate;
  1428. JvmtiEventEnabled _event_enabled;
  1429. public:
  1430. JvmtiThreadEventEnable();
  1431. ~JvmtiThreadEventEnable();
  1432. bool is_enabled(jvmtiEvent event_type);
  1433. };
  1434. class JvmtiEnvEventEnable VALUE_OBJ_CLASS_SPEC {
  1435. private:
  1436. friend class JvmtiEventControllerPrivate;
  1437. JvmtiEventEnabled _event_user_enabled;
  1438. JvmtiEventEnabled _event_callback_enabled;
  1439. JvmtiEventEnabled _event_enabled;
  1440. public:
  1441. JvmtiEnvEventEnable();
  1442. ~JvmtiEnvEventEnable();
  1443. bool is_enabled(jvmtiEvent event_type);
  1444. void set_user_enabled(jvmtiEvent event_type, bool enabled);
  1445. };
  1446. class JvmtiEventController : AllStatic {
  1447. private:
  1448. friend class JvmtiEventControllerPrivate;
  1449. static JvmtiEventEnabled _universal_global_event_enabled;
  1450. public:
  1451. static bool is_enabled(jvmtiEvent event_type);
  1452. static bool is_global_event(jvmtiEvent event_type);
  1453. static bool is_valid_event_type(jvmtiEvent event_type) {
  1454. return ((int)event_type >= TOTAL_MIN_EVENT_TYPE_VAL)
  1455. && ((int)event_type <= TOTAL_MAX_EVENT_TYPE_VAL);
  1456. }
  1457. static void set_user_enabled(JvmtiEnvBase *env, JavaThread *thread,
  1458. jvmtiEvent event_type, bool enabled);
  1459. static void set_event_callbacks(JvmtiEnvBase *env,
  1460. const jvmtiEventCallbacks* callbacks,
  1461. jint size_of_callbacks);
  1462. static void set_extension_event_callback(JvmtiEnvBase* env,
  1463. jint extension_event_index,
  1464. jvmtiExtensionEvent callback);
  1465. static void set_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
  1466. static void clear_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
  1467. static void clear_to_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
  1468. static void change_field_watch(jvmtiEvent event_type, bool added);
  1469. static void thread_started(JavaThread *thread);
  1470. static void thread_ended(JavaThread *thread);
  1471. static void env_initialize(JvmtiEnvBase *env);
  1472. static void env_dispose(JvmtiEnvBase *env);
  1473. static void vm_start();
  1474. static void vm_init();
  1475. static void vm_death();
  1476. };
  1477. #endif // SHARE_VM_PRIMS_JVMTIEVENTCONTROLLER_HPP
  1478. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiEventController.inline.hpp
  1479. #ifndef SHARE_VM_PRIMS_JVMTIEVENTCONTROLLER_INLINE_HPP
  1480. #define SHARE_VM_PRIMS_JVMTIEVENTCONTROLLER_INLINE_HPP
  1481. #include "prims/jvmtiEventController.hpp"
  1482. #include "prims/jvmtiImpl.hpp"
  1483. #include "prims/jvmtiUtil.hpp"
  1484. inline jlong JvmtiEventEnabled::bit_for(jvmtiEvent event_type) {
  1485. assert(JvmtiEventController::is_valid_event_type(event_type), "invalid event type");
  1486. return ((jlong)1) << (event_type - TOTAL_MIN_EVENT_TYPE_VAL);
  1487. }
  1488. inline jlong JvmtiEventEnabled::get_bits() {
  1489. assert(_init_guard == JEE_INIT_GUARD, "enable bits uninitialized or corrupted");
  1490. return _enabled_bits;
  1491. }
  1492. inline void JvmtiEventEnabled::set_bits(jlong bits) {
  1493. assert(_init_guard == JEE_INIT_GUARD, "enable bits uninitialized or corrupted on set");
  1494. _enabled_bits = bits;
  1495. }
  1496. inline bool JvmtiEventEnabled::is_enabled(jvmtiEvent event_type) {
  1497. return (bit_for(event_type) & get_bits()) != 0;
  1498. }
  1499. inline bool JvmtiEnvThreadEventEnable::is_enabled(jvmtiEvent event_type) {
  1500. assert(JvmtiUtil::event_threaded(event_type), "Only thread filtered events should be tested here");
  1501. return _event_enabled.is_enabled(event_type);
  1502. }
  1503. inline void JvmtiEnvThreadEventEnable::set_user_enabled(jvmtiEvent event_type, bool enabled) {
  1504. _event_user_enabled.set_enabled(event_type, enabled);
  1505. }
  1506. inline bool JvmtiThreadEventEnable::is_enabled(jvmtiEvent event_type) {
  1507. assert(JvmtiUtil::event_threaded(event_type), "Only thread filtered events should be tested here");
  1508. return _event_enabled.is_enabled(event_type);
  1509. }
  1510. inline bool JvmtiEnvEventEnable::is_enabled(jvmtiEvent event_type) {
  1511. assert(!JvmtiUtil::event_threaded(event_type), "Only non thread filtered events should be tested here");
  1512. return _event_enabled.is_enabled(event_type);
  1513. }
  1514. inline void JvmtiEnvEventEnable::set_user_enabled(jvmtiEvent event_type, bool enabled) {
  1515. _event_user_enabled.set_enabled(event_type, enabled);
  1516. }
  1517. inline bool JvmtiEventController::is_enabled(jvmtiEvent event_type) {
  1518. return _universal_global_event_enabled.is_enabled(event_type);
  1519. }
  1520. #endif // SHARE_VM_PRIMS_JVMTIEVENTCONTROLLER_INLINE_HPP
  1521. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiExport.cpp
  1522. #include "precompiled.hpp"
  1523. #include "classfile/systemDictionary.hpp"
  1524. #include "code/nmethod.hpp"
  1525. #include "code/pcDesc.hpp"
  1526. #include "code/scopeDesc.hpp"
  1527. #include "interpreter/interpreter.hpp"
  1528. #include "jvmtifiles/jvmtiEnv.hpp"
  1529. #include "memory/resourceArea.hpp"
  1530. #include "oops/objArrayKlass.hpp"
  1531. #include "oops/objArrayOop.hpp"
  1532. #include "prims/jvmtiCodeBlobEvents.hpp"
  1533. #include "prims/jvmtiEventController.hpp"
  1534. #include "prims/jvmtiEventController.inline.hpp"
  1535. #include "prims/jvmtiExport.hpp"
  1536. #include "prims/jvmtiImpl.hpp"
  1537. #include "prims/jvmtiManageCapabilities.hpp"
  1538. #include "prims/jvmtiRawMonitor.hpp"
  1539. #include "prims/jvmtiTagMap.hpp"
  1540. #include "prims/jvmtiThreadState.inline.hpp"
  1541. #include "prims/jvmtiRedefineClasses.hpp"
  1542. #include "runtime/arguments.hpp"
  1543. #include "runtime/handles.hpp"
  1544. #include "runtime/interfaceSupport.hpp"
  1545. #include "runtime/objectMonitor.hpp"
  1546. #include "runtime/objectMonitor.inline.hpp"
  1547. #include "runtime/thread.inline.hpp"
  1548. #include "runtime/vframe.hpp"
  1549. #include "services/attachListener.hpp"
  1550. #include "services/serviceUtil.hpp"
  1551. #include "utilities/macros.hpp"
  1552. #if INCLUDE_ALL_GCS
  1553. #include "gc_implementation/parallelScavenge/psMarkSweep.hpp"
  1554. #endif // INCLUDE_ALL_GCS
  1555. PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  1556. #ifdef JVMTI_TRACE
  1557. #define EVT_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_SENT) != 0) { SafeResourceMark rm; tty->print_cr out; }
  1558. #define EVT_TRIG_TRACE(evt,out) if ((JvmtiTrace::event_trace_flags(evt) & JvmtiTrace::SHOW_EVENT_TRIGGER) != 0) { SafeResourceMark rm; tty->print_cr out; }
  1559. #else
  1560. #define EVT_TRIG_TRACE(evt,out)
  1561. #define EVT_TRACE(evt,out)
  1562. #endif
  1563. class JvmtiJavaThreadEventTransition : StackObj {
  1564. private:
  1565. ResourceMark _rm;
  1566. ThreadToNativeFromVM _transition;
  1567. HandleMark _hm;
  1568. public:
  1569. JvmtiJavaThreadEventTransition(JavaThread *thread) :
  1570. _rm(),
  1571. _transition(thread),
  1572. _hm(thread) {};
  1573. };
  1574. class JvmtiThreadEventTransition : StackObj {
  1575. private:
  1576. ResourceMark _rm;
  1577. HandleMark _hm;
  1578. JavaThreadState _saved_state;
  1579. JavaThread *_jthread;
  1580. public:
  1581. JvmtiThreadEventTransition(Thread *thread) : _rm(), _hm() {
  1582. if (thread->is_Java_thread()) {
  1583. _jthread = (JavaThread *)thread;
  1584. _saved_state = _jthread->thread_state();
  1585. if (_saved_state == _thread_in_Java) {
  1586. ThreadStateTransition::transition_from_java(_jthread, _thread_in_native);
  1587. } else {
  1588. ThreadStateTransition::transition(_jthread, _saved_state, _thread_in_native);
  1589. }
  1590. } else {
  1591. _jthread = NULL;
  1592. }
  1593. }
  1594. ~JvmtiThreadEventTransition() {
  1595. if (_jthread != NULL)
  1596. ThreadStateTransition::transition_from_native(_jthread, _saved_state);
  1597. }
  1598. };
  1599. class JvmtiEventMark : public StackObj {
  1600. private:
  1601. JavaThread *_thread;
  1602. JNIEnv* _jni_env;
  1603. bool _exception_detected;
  1604. bool _exception_caught;
  1605. #if 0
  1606. JNIHandleBlock* _hblock;
  1607. #endif
  1608. public:
  1609. JvmtiEventMark(JavaThread *thread) : _thread(thread),
  1610. _jni_env(thread->jni_environment()) {
  1611. #if 0
  1612. _hblock = thread->active_handles();
  1613. _hblock->clear_thoroughly(); // so we can be safe
  1614. #else
  1615. JvmtiThreadState *state = thread->jvmti_thread_state();
  1616. if (state != NULL) {
  1617. _exception_detected = state->is_exception_detected();
  1618. _exception_caught = state->is_exception_caught();
  1619. } else {
  1620. _exception_detected = false;
  1621. _exception_caught = false;
  1622. }
  1623. JNIHandleBlock* old_handles = thread->active_handles();
  1624. JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
  1625. assert(new_handles != NULL, "should not be NULL");
  1626. new_handles->set_pop_frame_link(old_handles);
  1627. thread->set_active_handles(new_handles);
  1628. #endif
  1629. assert(thread == JavaThread::current(), "thread must be current!");
  1630. thread->frame_anchor()->make_walkable(thread);
  1631. };
  1632. ~JvmtiEventMark() {
  1633. #if 0
  1634. _hblock->clear(); // for consistency with future correct behavior
  1635. #else
  1636. JNIHandleBlock* old_handles = _thread->active_handles();
  1637. JNIHandleBlock* new_handles = old_handles->pop_frame_link();
  1638. assert(new_handles != NULL, "should not be NULL");
  1639. _thread->set_active_handles(new_handles);
  1640. old_handles->set_pop_frame_link(NULL);
  1641. JNIHandleBlock::release_block(old_handles, _thread); // may block
  1642. #endif
  1643. JvmtiThreadState* state = _thread->jvmti_thread_state();
  1644. if (state != NULL) {
  1645. if (_exception_detected) {
  1646. state->set_exception_detected();
  1647. }
  1648. if (_exception_caught) {
  1649. state->set_exception_caught();
  1650. }
  1651. }
  1652. }
  1653. #if 0
  1654. jobject to_jobject(oop obj) { return obj == NULL? NULL : _hblock->allocate_handle_fast(obj); }
  1655. #else
  1656. jobject to_jobject(oop obj) { return JNIHandles::make_local(_thread,obj); }
  1657. #endif
  1658. jclass to_jclass(Klass* klass) { return (klass == NULL ? NULL : (jclass)to_jobject(klass->java_mirror())); }
  1659. jmethodID to_jmethodID(methodHandle method) { return method->jmethod_id(); }
  1660. JNIEnv* jni_env() { return _jni_env; }
  1661. };
  1662. class JvmtiThreadEventMark : public JvmtiEventMark {
  1663. private:
  1664. jthread _jt;
  1665. public:
  1666. JvmtiThreadEventMark(JavaThread *thread) :
  1667. JvmtiEventMark(thread) {
  1668. _jt = (jthread)(to_jobject(thread->threadObj()));
  1669. };
  1670. jthread jni_thread() { return _jt; }
  1671. };
  1672. class JvmtiClassEventMark : public JvmtiThreadEventMark {
  1673. private:
  1674. jclass _jc;
  1675. public:
  1676. JvmtiClassEventMark(JavaThread *thread, Klass* klass) :
  1677. JvmtiThreadEventMark(thread) {
  1678. _jc = to_jclass(klass);
  1679. };
  1680. jclass jni_class() { return _jc; }
  1681. };
  1682. class JvmtiMethodEventMark : public JvmtiThreadEventMark {
  1683. private:
  1684. jmethodID _mid;
  1685. public:
  1686. JvmtiMethodEventMark(JavaThread *thread, methodHandle method) :
  1687. JvmtiThreadEventMark(thread),
  1688. _mid(to_jmethodID(method)) {};
  1689. jmethodID jni_methodID() { return _mid; }
  1690. };
  1691. class JvmtiLocationEventMark : public JvmtiMethodEventMark {
  1692. private:
  1693. jlocation _loc;
  1694. public:
  1695. JvmtiLocationEventMark(JavaThread *thread, methodHandle method, address location) :
  1696. JvmtiMethodEventMark(thread, method),
  1697. _loc(location - method->code_base()) {};
  1698. jlocation location() { return _loc; }
  1699. };
  1700. class JvmtiExceptionEventMark : public JvmtiLocationEventMark {
  1701. private:
  1702. jobject _exc;
  1703. public:
  1704. JvmtiExceptionEventMark(JavaThread *thread, methodHandle method, address location, Handle exception) :
  1705. JvmtiLocationEventMark(thread, method, location),
  1706. _exc(to_jobject(exception())) {};
  1707. jobject exception() { return _exc; }
  1708. };
  1709. class JvmtiClassFileLoadEventMark : public JvmtiThreadEventMark {
  1710. private:
  1711. const char *_class_name;
  1712. jobject _jloader;
  1713. jobject _protection_domain;
  1714. jclass _class_being_redefined;
  1715. public:
  1716. JvmtiClassFileLoadEventMark(JavaThread *thread, Symbol* name,
  1717. Handle class_loader, Handle prot_domain, KlassHandle *class_being_redefined) : JvmtiThreadEventMark(thread) {
  1718. _class_name = name != NULL? name->as_utf8() : NULL;
  1719. _jloader = (jobject)to_jobject(class_loader());
  1720. _protection_domain = (jobject)to_jobject(prot_domain());
  1721. if (class_being_redefined == NULL) {
  1722. _class_being_redefined = NULL;
  1723. } else {
  1724. _class_being_redefined = (jclass)to_jclass((*class_being_redefined)());
  1725. }
  1726. };
  1727. const char *class_name() {
  1728. return _class_name;
  1729. }
  1730. jobject jloader() {
  1731. return _jloader;
  1732. }
  1733. jobject protection_domain() {
  1734. return _protection_domain;
  1735. }
  1736. jclass class_being_redefined() {
  1737. return _class_being_redefined;
  1738. }
  1739. };
  1740. int JvmtiExport::_field_access_count = 0;
  1741. int JvmtiExport::_field_modification_count = 0;
  1742. bool JvmtiExport::_can_access_local_variables = false;
  1743. bool JvmtiExport::_can_hotswap_or_post_breakpoint = false;
  1744. bool JvmtiExport::_can_modify_any_class = false;
  1745. bool JvmtiExport::_can_walk_any_space = false;
  1746. bool JvmtiExport::_has_redefined_a_class = false;
  1747. bool JvmtiExport::_all_dependencies_are_recorded = false;
  1748. address JvmtiExport::get_field_access_count_addr() {
  1749. return (address)(&_field_access_count);
  1750. }
  1751. address JvmtiExport::get_field_modification_count_addr() {
  1752. return (address)(&_field_modification_count);
  1753. }
  1754. jint
  1755. JvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) {
  1756. int major, minor, micro;
  1757. decode_version_values(version, &major, &minor, &micro);
  1758. switch (major) {
  1759. case 1:
  1760. switch (minor) {
  1761. case 0: // version 1.0.<micro> is recognized
  1762. case 1: // version 1.1.<micro> is recognized
  1763. case 2: // version 1.2.<micro> is recognized
  1764. break;
  1765. default:
  1766. return JNI_EVERSION; // unsupported minor version number
  1767. }
  1768. break;
  1769. default:
  1770. return JNI_EVERSION; // unsupported major version number
  1771. }
  1772. if (JvmtiEnv::get_phase() == JVMTI_PHASE_LIVE) {
  1773. JavaThread* current_thread = (JavaThread*) ThreadLocalStorage::thread();
  1774. ThreadInVMfromNative __tiv(current_thread);
  1775. VM_ENTRY_BASE(jvmtiEnv*, JvmtiExport::get_jvmti_interface, current_thread)
  1776. debug_only(VMNativeEntryWrapper __vew;)
  1777. JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version);
  1778. return JNI_OK;
  1779. } else if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
  1780. JvmtiEnv *jvmti_env = JvmtiEnv::create_a_jvmti(version);
  1781. return JNI_OK;
  1782. } else {
  1783. return JNI_EDETACHED;
  1784. }
  1785. }
  1786. void
  1787. JvmtiExport::decode_version_values(jint version, int * major, int * minor,
  1788. int * micro) {
  1789. }
  1790. void JvmtiExport::enter_primordial_phase() {
  1791. JvmtiEnvBase::set_phase(JVMTI_PHASE_PRIMORDIAL);
  1792. }
  1793. void JvmtiExport::enter_start_phase() {
  1794. JvmtiManageCapabilities::recompute_always_capabilities();
  1795. JvmtiEnvBase::set_phase(JVMTI_PHASE_START);
  1796. }
  1797. void JvmtiExport::enter_onload_phase() {
  1798. JvmtiEnvBase::set_phase(JVMTI_PHASE_ONLOAD);
  1799. }
  1800. void JvmtiExport::enter_live_phase() {
  1801. JvmtiEnvBase::set_phase(JVMTI_PHASE_LIVE);
  1802. }
  1803. void JvmtiExport::post_vm_start() {
  1804. EVT_TRIG_TRACE(JVMTI_EVENT_VM_START, ("JVMTI Trg VM start event triggered" ));
  1805. JvmtiEventController::vm_start();
  1806. JvmtiEnvIterator it;
  1807. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  1808. if (env->is_enabled(JVMTI_EVENT_VM_START)) {
  1809. EVT_TRACE(JVMTI_EVENT_VM_START, ("JVMTI Evt VM start event sent" ));
  1810. JavaThread *thread = JavaThread::current();
  1811. JvmtiThreadEventMark jem(thread);
  1812. JvmtiJavaThreadEventTransition jet(thread);
  1813. jvmtiEventVMStart callback = env->callbacks()->VMStart;
  1814. if (callback != NULL) {
  1815. (*callback)(env->jvmti_external(), jem.jni_env());
  1816. }
  1817. }
  1818. }
  1819. }
  1820. void JvmtiExport::post_vm_initialized() {
  1821. EVT_TRIG_TRACE(JVMTI_EVENT_VM_INIT, ("JVMTI Trg VM init event triggered" ));
  1822. JvmtiEventController::vm_init();
  1823. JvmtiEnvIterator it;
  1824. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  1825. if (env->is_enabled(JVMTI_EVENT_VM_INIT)) {
  1826. EVT_TRACE(JVMTI_EVENT_VM_INIT, ("JVMTI Evt VM init event sent" ));
  1827. JavaThread *thread = JavaThread::current();
  1828. JvmtiThreadEventMark jem(thread);
  1829. JvmtiJavaThreadEventTransition jet(thread);
  1830. jvmtiEventVMInit callback = env->callbacks()->VMInit;
  1831. if (callback != NULL) {
  1832. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
  1833. }
  1834. }
  1835. }
  1836. }
  1837. void JvmtiExport::post_vm_death() {
  1838. EVT_TRIG_TRACE(JVMTI_EVENT_VM_DEATH, ("JVMTI Trg VM death event triggered" ));
  1839. JvmtiEnvIterator it;
  1840. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  1841. if (env->is_enabled(JVMTI_EVENT_VM_DEATH)) {
  1842. EVT_TRACE(JVMTI_EVENT_VM_DEATH, ("JVMTI Evt VM death event sent" ));
  1843. JavaThread *thread = JavaThread::current();
  1844. JvmtiEventMark jem(thread);
  1845. JvmtiJavaThreadEventTransition jet(thread);
  1846. jvmtiEventVMDeath callback = env->callbacks()->VMDeath;
  1847. if (callback != NULL) {
  1848. (*callback)(env->jvmti_external(), jem.jni_env());
  1849. }
  1850. }
  1851. }
  1852. JvmtiEnvBase::set_phase(JVMTI_PHASE_DEAD);
  1853. JvmtiEventController::vm_death();
  1854. }
  1855. char**
  1856. JvmtiExport::get_all_native_method_prefixes(int* count_ptr) {
  1857. if (Threads::number_of_threads() == 0 || SafepointSynchronize::is_at_safepoint()) {
  1858. return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr);
  1859. } else {
  1860. MutexLocker mu(JvmtiThreadState_lock);
  1861. return JvmtiEnvBase::get_all_native_method_prefixes(count_ptr);
  1862. }
  1863. }
  1864. class JvmtiClassFileLoadHookPoster : public StackObj {
  1865. private:
  1866. Symbol* _h_name;
  1867. Handle _class_loader;
  1868. Handle _h_protection_domain;
  1869. unsigned char ** _data_ptr;
  1870. unsigned char ** _end_ptr;
  1871. JavaThread * _thread;
  1872. jint _curr_len;
  1873. unsigned char * _curr_data;
  1874. JvmtiEnv * _curr_env;
  1875. JvmtiCachedClassFileData ** _cached_class_file_ptr;
  1876. JvmtiThreadState * _state;
  1877. KlassHandle * _h_class_being_redefined;
  1878. JvmtiClassLoadKind _load_kind;
  1879. public:
  1880. inline JvmtiClassFileLoadHookPoster(Symbol* h_name, Handle class_loader,
  1881. Handle h_protection_domain,
  1882. unsigned char **data_ptr, unsigned char **end_ptr,
  1883. JvmtiCachedClassFileData **cache_ptr) {
  1884. _h_name = h_name;
  1885. _class_loader = class_loader;
  1886. _h_protection_domain = h_protection_domain;
  1887. _data_ptr = data_ptr;
  1888. _end_ptr = end_ptr;
  1889. _thread = JavaThread::current();
  1890. _curr_len = *end_ptr - *data_ptr;
  1891. _curr_data = *data_ptr;
  1892. _curr_env = NULL;
  1893. _cached_class_file_ptr = cache_ptr;
  1894. _state = _thread->jvmti_thread_state();
  1895. if (_state != NULL) {
  1896. _h_class_being_redefined = _state->get_class_being_redefined();
  1897. _load_kind = _state->get_class_load_kind();
  1898. _state->clear_class_being_redefined();
  1899. } else {
  1900. _h_class_being_redefined = (KlassHandle *) NULL;
  1901. _load_kind = jvmti_class_load_kind_load;
  1902. }
  1903. }
  1904. void post() {
  1905. post_all_envs();
  1906. copy_modified_data();
  1907. }
  1908. private:
  1909. void post_all_envs() {
  1910. if (_load_kind != jvmti_class_load_kind_retransform) {
  1911. JvmtiEnvIterator it;
  1912. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  1913. if (!env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) {
  1914. post_to_env(env, false);
  1915. }
  1916. }
  1917. }
  1918. JvmtiEnvIterator it;
  1919. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  1920. if (env->is_retransformable() && env->is_enabled(JVMTI_EVENT_CLASS_FILE_LOAD_HOOK)) {
  1921. post_to_env(env, true);
  1922. }
  1923. }
  1924. }
  1925. void post_to_env(JvmtiEnv* env, bool caching_needed) {
  1926. unsigned char *new_data = NULL;
  1927. jint new_len = 0;
  1928. JvmtiClassFileLoadEventMark jem(_thread, _h_name, _class_loader,
  1929. _h_protection_domain,
  1930. _h_class_being_redefined);
  1931. JvmtiJavaThreadEventTransition jet(_thread);
  1932. JNIEnv* jni_env = (JvmtiEnv::get_phase() == JVMTI_PHASE_PRIMORDIAL)?
  1933. NULL : jem.jni_env();
  1934. jvmtiEventClassFileLoadHook callback = env->callbacks()->ClassFileLoadHook;
  1935. if (callback != NULL) {
  1936. (*callback)(env->jvmti_external(), jni_env,
  1937. jem.class_being_redefined(),
  1938. jem.jloader(), jem.class_name(),
  1939. jem.protection_domain(),
  1940. _curr_len, _curr_data,
  1941. &new_len, &new_data);
  1942. }
  1943. if (new_data != NULL) {
  1944. if (caching_needed && *_cached_class_file_ptr == NULL) {
  1945. JvmtiCachedClassFileData *p;
  1946. p = (JvmtiCachedClassFileData *)os::malloc(
  1947. offset_of(JvmtiCachedClassFileData, data) + _curr_len, mtInternal);
  1948. if (p == NULL) {
  1949. vm_exit_out_of_memory(offset_of(JvmtiCachedClassFileData, data) + _curr_len,
  1950. OOM_MALLOC_ERROR,
  1951. "unable to allocate cached copy of original class bytes");
  1952. }
  1953. p->length = _curr_len;
  1954. memcpy(p->data, _curr_data, _curr_len);
  1955. }
  1956. if (_curr_data != *_data_ptr) {
  1957. _curr_env->Deallocate(_curr_data);
  1958. }
  1959. _curr_data = new_data;
  1960. _curr_len = new_len;
  1961. _curr_env = env;
  1962. }
  1963. }
  1964. void copy_modified_data() {
  1965. if (_curr_data != *_data_ptr) {
  1966. memcpy(*_data_ptr, _curr_data, _curr_len);
  1967. _curr_env->Deallocate(_curr_data);
  1968. }
  1969. }
  1970. };
  1971. bool JvmtiExport::_should_post_class_file_load_hook = false;
  1972. void JvmtiExport::post_class_file_load_hook(Symbol* h_name,
  1973. Handle class_loader,
  1974. Handle h_protection_domain,
  1975. unsigned char **data_ptr,
  1976. unsigned char **end_ptr,
  1977. JvmtiCachedClassFileData **cache_ptr) {
  1978. JvmtiClassFileLoadHookPoster poster(h_name, class_loader,
  1979. h_protection_domain,
  1980. data_ptr, end_ptr,
  1981. cache_ptr);
  1982. poster.post();
  1983. }
  1984. void JvmtiExport::report_unsupported(bool on) {
  1985. if (on) {
  1986. vm_exit_during_initialization("Java Kernel does not support JVMTI.");
  1987. }
  1988. }
  1989. static inline Klass* oop_to_klass(oop obj) {
  1990. Klass* k = obj->klass();
  1991. if (k == SystemDictionary::Class_klass()) {
  1992. if (!java_lang_Class::is_primitive(obj)) {
  1993. k = java_lang_Class::as_Klass(obj);
  1994. assert(k != NULL, "class for non-primitive mirror must exist");
  1995. }
  1996. }
  1997. return k;
  1998. }
  1999. class JvmtiVMObjectAllocEventMark : public JvmtiClassEventMark {
  2000. private:
  2001. jobject _jobj;
  2002. jlong _size;
  2003. public:
  2004. JvmtiVMObjectAllocEventMark(JavaThread *thread, oop obj) : JvmtiClassEventMark(thread, oop_to_klass(obj)) {
  2005. _jobj = (jobject)to_jobject(obj);
  2006. _size = obj->size() * wordSize;
  2007. };
  2008. jobject jni_jobject() { return _jobj; }
  2009. jlong size() { return _size; }
  2010. };
  2011. class JvmtiCompiledMethodLoadEventMark : public JvmtiMethodEventMark {
  2012. private:
  2013. jint _code_size;
  2014. const void *_code_data;
  2015. jint _map_length;
  2016. jvmtiAddrLocationMap *_map;
  2017. const void *_compile_info;
  2018. public:
  2019. JvmtiCompiledMethodLoadEventMark(JavaThread *thread, nmethod *nm, void* compile_info_ptr = NULL)
  2020. : JvmtiMethodEventMark(thread,methodHandle(thread, nm->method())) {
  2021. _code_data = nm->insts_begin();
  2022. _code_size = nm->insts_size();
  2023. _compile_info = compile_info_ptr; // Set void pointer of compiledMethodLoad Event. Default value is NULL.
  2024. JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &_map, &_map_length);
  2025. }
  2026. ~JvmtiCompiledMethodLoadEventMark() {
  2027. FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, _map, mtInternal);
  2028. }
  2029. jint code_size() { return _code_size; }
  2030. const void *code_data() { return _code_data; }
  2031. jint map_length() { return _map_length; }
  2032. const jvmtiAddrLocationMap* map() { return _map; }
  2033. const void *compile_info() { return _compile_info; }
  2034. };
  2035. class JvmtiMonitorEventMark : public JvmtiThreadEventMark {
  2036. private:
  2037. jobject _jobj;
  2038. public:
  2039. JvmtiMonitorEventMark(JavaThread *thread, oop object)
  2040. : JvmtiThreadEventMark(thread){
  2041. _jobj = to_jobject(object);
  2042. }
  2043. jobject jni_object() { return _jobj; }
  2044. };
  2045. void JvmtiExport::post_compiled_method_unload(
  2046. jmethodID method, const void *code_begin) {
  2047. JavaThread* thread = JavaThread::current();
  2048. EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
  2049. ("JVMTI [%s] method compile unload event triggered",
  2050. JvmtiTrace::safe_get_thread_name(thread)));
  2051. JvmtiEnvIterator it;
  2052. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  2053. if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_UNLOAD)) {
  2054. EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_UNLOAD,
  2055. ("JVMTI [%s] class compile method unload event sent jmethodID " PTR_FORMAT,
  2056. JvmtiTrace::safe_get_thread_name(thread), method));
  2057. ResourceMark rm(thread);
  2058. JvmtiEventMark jem(thread);
  2059. JvmtiJavaThreadEventTransition jet(thread);
  2060. jvmtiEventCompiledMethodUnload callback = env->callbacks()->CompiledMethodUnload;
  2061. if (callback != NULL) {
  2062. (*callback)(env->jvmti_external(), method, code_begin);
  2063. }
  2064. }
  2065. }
  2066. }
  2067. void JvmtiExport::post_raw_breakpoint(JavaThread *thread, Method* method, address location) {
  2068. HandleMark hm(thread);
  2069. methodHandle mh(thread, method);
  2070. JvmtiThreadState *state = thread->jvmti_thread_state();
  2071. if (state == NULL) {
  2072. return;
  2073. }
  2074. EVT_TRIG_TRACE(JVMTI_EVENT_BREAKPOINT, ("JVMTI [%s] Trg Breakpoint triggered",
  2075. JvmtiTrace::safe_get_thread_name(thread)));
  2076. JvmtiEnvThreadStateIterator it(state);
  2077. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2078. ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_BREAKPOINT);
  2079. if (!ets->breakpoint_posted() && ets->is_enabled(JVMTI_EVENT_BREAKPOINT)) {
  2080. ThreadState old_os_state = thread->osthread()->get_state();
  2081. thread->osthread()->set_state(BREAKPOINTED);
  2082. EVT_TRACE(JVMTI_EVENT_BREAKPOINT, ("JVMTI [%s] Evt Breakpoint sent %s.%s @ %d",
  2083. JvmtiTrace::safe_get_thread_name(thread),
  2084. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2085. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
  2086. location - mh()->code_base() ));
  2087. JvmtiEnv *env = ets->get_env();
  2088. JvmtiLocationEventMark jem(thread, mh, location);
  2089. JvmtiJavaThreadEventTransition jet(thread);
  2090. jvmtiEventBreakpoint callback = env->callbacks()->Breakpoint;
  2091. if (callback != NULL) {
  2092. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  2093. jem.jni_methodID(), jem.location());
  2094. }
  2095. ets->set_breakpoint_posted();
  2096. thread->osthread()->set_state(old_os_state);
  2097. }
  2098. }
  2099. }
  2100. bool JvmtiExport::_can_get_source_debug_extension = false;
  2101. bool JvmtiExport::_can_maintain_original_method_order = false;
  2102. bool JvmtiExport::_can_post_interpreter_events = false;
  2103. bool JvmtiExport::_can_post_on_exceptions = false;
  2104. bool JvmtiExport::_can_post_breakpoint = false;
  2105. bool JvmtiExport::_can_post_field_access = false;
  2106. bool JvmtiExport::_can_post_field_modification = false;
  2107. bool JvmtiExport::_can_post_method_entry = false;
  2108. bool JvmtiExport::_can_post_method_exit = false;
  2109. bool JvmtiExport::_can_pop_frame = false;
  2110. bool JvmtiExport::_can_force_early_return = false;
  2111. bool JvmtiExport::_should_post_single_step = false;
  2112. bool JvmtiExport::_should_post_field_access = false;
  2113. bool JvmtiExport::_should_post_field_modification = false;
  2114. bool JvmtiExport::_should_post_class_load = false;
  2115. bool JvmtiExport::_should_post_class_prepare = false;
  2116. bool JvmtiExport::_should_post_class_unload = false;
  2117. bool JvmtiExport::_should_post_thread_life = false;
  2118. bool JvmtiExport::_should_clean_up_heap_objects = false;
  2119. bool JvmtiExport::_should_post_native_method_bind = false;
  2120. bool JvmtiExport::_should_post_dynamic_code_generated = false;
  2121. bool JvmtiExport::_should_post_data_dump = false;
  2122. bool JvmtiExport::_should_post_compiled_method_load = false;
  2123. bool JvmtiExport::_should_post_compiled_method_unload = false;
  2124. bool JvmtiExport::_should_post_monitor_contended_enter = false;
  2125. bool JvmtiExport::_should_post_monitor_contended_entered = false;
  2126. bool JvmtiExport::_should_post_monitor_wait = false;
  2127. bool JvmtiExport::_should_post_monitor_waited = false;
  2128. bool JvmtiExport::_should_post_garbage_collection_start = false;
  2129. bool JvmtiExport::_should_post_garbage_collection_finish = false;
  2130. bool JvmtiExport::_should_post_object_free = false;
  2131. bool JvmtiExport::_should_post_resource_exhausted = false;
  2132. bool JvmtiExport::_should_post_vm_object_alloc = false;
  2133. bool JvmtiExport::_should_post_on_exceptions = false;
  2134. void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, address location) {
  2135. assert(JvmtiExport::should_post_single_step(), "must be single stepping");
  2136. HandleMark hm(thread);
  2137. methodHandle mh(thread, method);
  2138. JvmtiThreadState *state = thread->jvmti_thread_state();
  2139. if (state == NULL) {
  2140. return;
  2141. }
  2142. EVT_TRIG_TRACE(JVMTI_EVENT_SINGLE_STEP, ("JVMTI [%s] Trg Single Step triggered",
  2143. JvmtiTrace::safe_get_thread_name(thread)));
  2144. if (!state->hide_single_stepping()) {
  2145. if (state->is_pending_step_for_popframe()) {
  2146. state->process_pending_step_for_popframe();
  2147. }
  2148. if (state->is_pending_step_for_earlyret()) {
  2149. state->process_pending_step_for_earlyret();
  2150. }
  2151. JvmtiExport::post_single_step(thread, mh(), location);
  2152. }
  2153. }
  2154. void JvmtiExport::expose_single_stepping(JavaThread *thread) {
  2155. JvmtiThreadState *state = thread->jvmti_thread_state();
  2156. if (state != NULL) {
  2157. state->clear_hide_single_stepping();
  2158. }
  2159. }
  2160. bool JvmtiExport::hide_single_stepping(JavaThread *thread) {
  2161. JvmtiThreadState *state = thread->jvmti_thread_state();
  2162. if (state != NULL && state->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
  2163. state->set_hide_single_stepping();
  2164. return true;
  2165. } else {
  2166. return false;
  2167. }
  2168. }
  2169. void JvmtiExport::post_class_load(JavaThread *thread, Klass* klass) {
  2170. HandleMark hm(thread);
  2171. KlassHandle kh(thread, klass);
  2172. EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_LOAD, ("JVMTI [%s] Trg Class Load triggered",
  2173. JvmtiTrace::safe_get_thread_name(thread)));
  2174. JvmtiThreadState* state = thread->jvmti_thread_state();
  2175. if (state == NULL) {
  2176. return;
  2177. }
  2178. JvmtiEnvThreadStateIterator it(state);
  2179. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2180. if (ets->is_enabled(JVMTI_EVENT_CLASS_LOAD)) {
  2181. EVT_TRACE(JVMTI_EVENT_CLASS_LOAD, ("JVMTI [%s] Evt Class Load sent %s",
  2182. JvmtiTrace::safe_get_thread_name(thread),
  2183. kh()==NULL? "NULL" : kh()->external_name() ));
  2184. JvmtiEnv *env = ets->get_env();
  2185. JvmtiClassEventMark jem(thread, kh());
  2186. JvmtiJavaThreadEventTransition jet(thread);
  2187. jvmtiEventClassLoad callback = env->callbacks()->ClassLoad;
  2188. if (callback != NULL) {
  2189. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
  2190. }
  2191. }
  2192. }
  2193. }
  2194. void JvmtiExport::post_class_prepare(JavaThread *thread, Klass* klass) {
  2195. HandleMark hm(thread);
  2196. KlassHandle kh(thread, klass);
  2197. EVT_TRIG_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("JVMTI [%s] Trg Class Prepare triggered",
  2198. JvmtiTrace::safe_get_thread_name(thread)));
  2199. JvmtiThreadState* state = thread->jvmti_thread_state();
  2200. if (state == NULL) {
  2201. return;
  2202. }
  2203. JvmtiEnvThreadStateIterator it(state);
  2204. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2205. if (ets->is_enabled(JVMTI_EVENT_CLASS_PREPARE)) {
  2206. EVT_TRACE(JVMTI_EVENT_CLASS_PREPARE, ("JVMTI [%s] Evt Class Prepare sent %s",
  2207. JvmtiTrace::safe_get_thread_name(thread),
  2208. kh()==NULL? "NULL" : kh()->external_name() ));
  2209. JvmtiEnv *env = ets->get_env();
  2210. JvmtiClassEventMark jem(thread, kh());
  2211. JvmtiJavaThreadEventTransition jet(thread);
  2212. jvmtiEventClassPrepare callback = env->callbacks()->ClassPrepare;
  2213. if (callback != NULL) {
  2214. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_class());
  2215. }
  2216. }
  2217. }
  2218. }
  2219. void JvmtiExport::post_class_unload(Klass* klass) {
  2220. Thread *thread = Thread::current();
  2221. HandleMark hm(thread);
  2222. KlassHandle kh(thread, klass);
  2223. EVT_TRIG_TRACE(EXT_EVENT_CLASS_UNLOAD, ("JVMTI [?] Trg Class Unload triggered" ));
  2224. if (JvmtiEventController::is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
  2225. assert(thread->is_VM_thread(), "wrong thread");
  2226. JavaThread *real_thread =
  2227. (JavaThread *)((VMThread *)thread)->vm_operation()->calling_thread();
  2228. JvmtiEnvIterator it;
  2229. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  2230. if (env->is_enabled((jvmtiEvent)EXT_EVENT_CLASS_UNLOAD)) {
  2231. EVT_TRACE(EXT_EVENT_CLASS_UNLOAD, ("JVMTI [?] Evt Class Unload sent %s",
  2232. kh()==NULL? "NULL" : kh()->external_name() ));
  2233. JNIEnv* jni_env = real_thread->jni_environment();
  2234. jthread jt = (jthread)JNIHandles::make_local(real_thread, real_thread->threadObj());
  2235. jclass jk = (jclass)JNIHandles::make_local(real_thread, kh()->java_mirror());
  2236. JavaThreadState prev_state = real_thread->thread_state();
  2237. assert(((Thread *)real_thread)->is_ConcurrentGC_thread() ||
  2238. (real_thread->is_Java_thread() && prev_state == _thread_blocked),
  2239. "should be ConcurrentGCThread or JavaThread at safepoint");
  2240. real_thread->set_thread_state(_thread_in_native);
  2241. jvmtiExtensionEvent callback = env->ext_callbacks()->ClassUnload;
  2242. if (callback != NULL) {
  2243. (*callback)(env->jvmti_external(), jni_env, jt, jk);
  2244. }
  2245. assert(real_thread->thread_state() == _thread_in_native,
  2246. "JavaThread should be in native");
  2247. real_thread->set_thread_state(prev_state);
  2248. JNIHandles::destroy_local(jk);
  2249. JNIHandles::destroy_local(jt);
  2250. }
  2251. }
  2252. }
  2253. }
  2254. void JvmtiExport::post_thread_start(JavaThread *thread) {
  2255. assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
  2256. EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_START, ("JVMTI [%s] Trg Thread Start event triggered",
  2257. JvmtiTrace::safe_get_thread_name(thread)));
  2258. JvmtiEventController::thread_started(thread);
  2259. if (JvmtiEventController::is_enabled(JVMTI_EVENT_THREAD_START) &&
  2260. !thread->is_hidden_from_external_view()) {
  2261. JvmtiEnvIterator it;
  2262. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  2263. if (env->is_enabled(JVMTI_EVENT_THREAD_START)) {
  2264. EVT_TRACE(JVMTI_EVENT_THREAD_START, ("JVMTI [%s] Evt Thread Start event sent",
  2265. JvmtiTrace::safe_get_thread_name(thread) ));
  2266. JvmtiThreadEventMark jem(thread);
  2267. JvmtiJavaThreadEventTransition jet(thread);
  2268. jvmtiEventThreadStart callback = env->callbacks()->ThreadStart;
  2269. if (callback != NULL) {
  2270. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
  2271. }
  2272. }
  2273. }
  2274. }
  2275. }
  2276. void JvmtiExport::post_thread_end(JavaThread *thread) {
  2277. EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_END, ("JVMTI [%s] Trg Thread End event triggered",
  2278. JvmtiTrace::safe_get_thread_name(thread)));
  2279. JvmtiThreadState *state = thread->jvmti_thread_state();
  2280. if (state == NULL) {
  2281. return;
  2282. }
  2283. if (state->is_enabled(JVMTI_EVENT_THREAD_END) &&
  2284. !thread->is_hidden_from_external_view()) {
  2285. JvmtiEnvThreadStateIterator it(state);
  2286. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2287. if (ets->is_enabled(JVMTI_EVENT_THREAD_END)) {
  2288. EVT_TRACE(JVMTI_EVENT_THREAD_END, ("JVMTI [%s] Evt Thread End event sent",
  2289. JvmtiTrace::safe_get_thread_name(thread) ));
  2290. JvmtiEnv *env = ets->get_env();
  2291. JvmtiThreadEventMark jem(thread);
  2292. JvmtiJavaThreadEventTransition jet(thread);
  2293. jvmtiEventThreadEnd callback = env->callbacks()->ThreadEnd;
  2294. if (callback != NULL) {
  2295. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread());
  2296. }
  2297. }
  2298. }
  2299. }
  2300. }
  2301. void JvmtiExport::post_object_free(JvmtiEnv* env, jlong tag) {
  2302. assert(SafepointSynchronize::is_at_safepoint(), "must be executed at safepoint");
  2303. assert(env->is_enabled(JVMTI_EVENT_OBJECT_FREE), "checking");
  2304. EVT_TRIG_TRACE(JVMTI_EVENT_OBJECT_FREE, ("JVMTI [?] Trg Object Free triggered" ));
  2305. EVT_TRACE(JVMTI_EVENT_OBJECT_FREE, ("JVMTI [?] Evt Object Free sent"));
  2306. jvmtiEventObjectFree callback = env->callbacks()->ObjectFree;
  2307. if (callback != NULL) {
  2308. (*callback)(env->jvmti_external(), tag);
  2309. }
  2310. }
  2311. void JvmtiExport::post_resource_exhausted(jint resource_exhausted_flags, const char* description) {
  2312. EVT_TRIG_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("JVMTI Trg resource exhausted event triggered" ));
  2313. JvmtiEnvIterator it;
  2314. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  2315. if (env->is_enabled(JVMTI_EVENT_RESOURCE_EXHAUSTED)) {
  2316. EVT_TRACE(JVMTI_EVENT_RESOURCE_EXHAUSTED, ("JVMTI Evt resource exhausted event sent" ));
  2317. JavaThread *thread = JavaThread::current();
  2318. JvmtiThreadEventMark jem(thread);
  2319. JvmtiJavaThreadEventTransition jet(thread);
  2320. jvmtiEventResourceExhausted callback = env->callbacks()->ResourceExhausted;
  2321. if (callback != NULL) {
  2322. (*callback)(env->jvmti_external(), jem.jni_env(),
  2323. resource_exhausted_flags, NULL, description);
  2324. }
  2325. }
  2326. }
  2327. }
  2328. void JvmtiExport::post_method_entry(JavaThread *thread, Method* method, frame current_frame) {
  2329. HandleMark hm(thread);
  2330. methodHandle mh(thread, method);
  2331. EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("JVMTI [%s] Trg Method Entry triggered %s.%s",
  2332. JvmtiTrace::safe_get_thread_name(thread),
  2333. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2334. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
  2335. JvmtiThreadState* state = thread->jvmti_thread_state();
  2336. if (state == NULL || !state->is_interp_only_mode()) {
  2337. return;
  2338. }
  2339. state->incr_cur_stack_depth();
  2340. if (state->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) {
  2341. JvmtiEnvThreadStateIterator it(state);
  2342. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2343. if (ets->is_enabled(JVMTI_EVENT_METHOD_ENTRY)) {
  2344. EVT_TRACE(JVMTI_EVENT_METHOD_ENTRY, ("JVMTI [%s] Evt Method Entry sent %s.%s",
  2345. JvmtiTrace::safe_get_thread_name(thread),
  2346. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2347. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
  2348. JvmtiEnv *env = ets->get_env();
  2349. JvmtiMethodEventMark jem(thread, mh);
  2350. JvmtiJavaThreadEventTransition jet(thread);
  2351. jvmtiEventMethodEntry callback = env->callbacks()->MethodEntry;
  2352. if (callback != NULL) {
  2353. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_methodID());
  2354. }
  2355. }
  2356. }
  2357. }
  2358. }
  2359. void JvmtiExport::post_method_exit(JavaThread *thread, Method* method, frame current_frame) {
  2360. HandleMark hm(thread);
  2361. methodHandle mh(thread, method);
  2362. EVT_TRIG_TRACE(JVMTI_EVENT_METHOD_EXIT, ("JVMTI [%s] Trg Method Exit triggered %s.%s",
  2363. JvmtiTrace::safe_get_thread_name(thread),
  2364. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2365. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
  2366. JvmtiThreadState *state = thread->jvmti_thread_state();
  2367. if (state == NULL || !state->is_interp_only_mode()) {
  2368. return;
  2369. }
  2370. bool exception_exit = state->is_exception_detected() && !state->is_exception_caught();
  2371. if (state->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
  2372. Handle result;
  2373. jvalue value;
  2374. value.j = 0L;
  2375. if (!exception_exit) {
  2376. oop oop_result;
  2377. BasicType type = current_frame.interpreter_frame_result(&oop_result, &value);
  2378. if (type == T_OBJECT || type == T_ARRAY) {
  2379. result = Handle(thread, oop_result);
  2380. }
  2381. }
  2382. JvmtiEnvThreadStateIterator it(state);
  2383. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2384. if (ets->is_enabled(JVMTI_EVENT_METHOD_EXIT)) {
  2385. EVT_TRACE(JVMTI_EVENT_METHOD_EXIT, ("JVMTI [%s] Evt Method Exit sent %s.%s",
  2386. JvmtiTrace::safe_get_thread_name(thread),
  2387. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2388. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
  2389. JvmtiEnv *env = ets->get_env();
  2390. JvmtiMethodEventMark jem(thread, mh);
  2391. if (result.not_null()) {
  2392. value.l = JNIHandles::make_local(thread, result());
  2393. }
  2394. JvmtiJavaThreadEventTransition jet(thread);
  2395. jvmtiEventMethodExit callback = env->callbacks()->MethodExit;
  2396. if (callback != NULL) {
  2397. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  2398. jem.jni_methodID(), exception_exit, value);
  2399. }
  2400. }
  2401. }
  2402. }
  2403. if (state->is_enabled(JVMTI_EVENT_FRAME_POP)) {
  2404. JvmtiEnvThreadStateIterator it(state);
  2405. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2406. int cur_frame_number = state->cur_stack_depth();
  2407. if (ets->is_frame_pop(cur_frame_number)) {
  2408. if (ets->is_enabled(JVMTI_EVENT_FRAME_POP)) {
  2409. EVT_TRACE(JVMTI_EVENT_FRAME_POP, ("JVMTI [%s] Evt Frame Pop sent %s.%s",
  2410. JvmtiTrace::safe_get_thread_name(thread),
  2411. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2412. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string() ));
  2413. JvmtiEnv *env = ets->get_env();
  2414. JvmtiMethodEventMark jem(thread, mh);
  2415. JvmtiJavaThreadEventTransition jet(thread);
  2416. jvmtiEventFramePop callback = env->callbacks()->FramePop;
  2417. if (callback != NULL) {
  2418. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  2419. jem.jni_methodID(), exception_exit);
  2420. }
  2421. }
  2422. ets->clear_frame_pop(cur_frame_number);
  2423. }
  2424. }
  2425. }
  2426. #ifdef AARCH64
  2427. state->invalidate_cur_stack_depth();
  2428. #else
  2429. state->decr_cur_stack_depth();
  2430. #endif
  2431. }
  2432. void JvmtiExport::post_single_step(JavaThread *thread, Method* method, address location) {
  2433. HandleMark hm(thread);
  2434. methodHandle mh(thread, method);
  2435. JvmtiThreadState *state = thread->jvmti_thread_state();
  2436. if (state == NULL) {
  2437. return;
  2438. }
  2439. JvmtiEnvThreadStateIterator it(state);
  2440. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2441. ets->compare_and_set_current_location(mh(), location, JVMTI_EVENT_SINGLE_STEP);
  2442. if (!ets->single_stepping_posted() && ets->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
  2443. EVT_TRACE(JVMTI_EVENT_SINGLE_STEP, ("JVMTI [%s] Evt Single Step sent %s.%s @ %d",
  2444. JvmtiTrace::safe_get_thread_name(thread),
  2445. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2446. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
  2447. location - mh()->code_base() ));
  2448. JvmtiEnv *env = ets->get_env();
  2449. JvmtiLocationEventMark jem(thread, mh, location);
  2450. JvmtiJavaThreadEventTransition jet(thread);
  2451. jvmtiEventSingleStep callback = env->callbacks()->SingleStep;
  2452. if (callback != NULL) {
  2453. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  2454. jem.jni_methodID(), jem.location());
  2455. }
  2456. ets->set_single_stepping_posted();
  2457. }
  2458. }
  2459. }
  2460. void JvmtiExport::post_exception_throw(JavaThread *thread, Method* method, address location, oop exception) {
  2461. HandleMark hm(thread);
  2462. methodHandle mh(thread, method);
  2463. Handle exception_handle(thread, exception);
  2464. JvmtiThreadState *state = thread->jvmti_thread_state();
  2465. if (state == NULL) {
  2466. return;
  2467. }
  2468. EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION, ("JVMTI [%s] Trg Exception thrown triggered",
  2469. JvmtiTrace::safe_get_thread_name(thread)));
  2470. if (!state->is_exception_detected()) {
  2471. state->set_exception_detected();
  2472. JvmtiEnvThreadStateIterator it(state);
  2473. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2474. if (ets->is_enabled(JVMTI_EVENT_EXCEPTION) && (exception != NULL)) {
  2475. EVT_TRACE(JVMTI_EVENT_EXCEPTION,
  2476. ("JVMTI [%s] Evt Exception thrown sent %s.%s @ %d",
  2477. JvmtiTrace::safe_get_thread_name(thread),
  2478. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2479. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
  2480. location - mh()->code_base() ));
  2481. JvmtiEnv *env = ets->get_env();
  2482. JvmtiExceptionEventMark jem(thread, mh, location, exception_handle);
  2483. EXCEPTION_MARK;
  2484. bool should_repeat;
  2485. vframeStream st(thread);
  2486. assert(!st.at_end(), "cannot be at end");
  2487. Method* current_method = NULL;
  2488. methodHandle current_mh = methodHandle(thread, current_method);
  2489. int current_bci = -1;
  2490. do {
  2491. current_method = st.method();
  2492. current_mh = methodHandle(thread, current_method);
  2493. current_bci = st.bci();
  2494. do {
  2495. should_repeat = false;
  2496. KlassHandle eh_klass(thread, exception_handle()->klass());
  2497. current_bci = Method::fast_exception_handler_bci_for(
  2498. current_mh, eh_klass, current_bci, THREAD);
  2499. if (HAS_PENDING_EXCEPTION) {
  2500. exception_handle = Handle(thread, PENDING_EXCEPTION);
  2501. CLEAR_PENDING_EXCEPTION;
  2502. should_repeat = true;
  2503. }
  2504. } while (should_repeat && (current_bci != -1));
  2505. st.next();
  2506. } while ((current_bci < 0) && (!st.at_end()));
  2507. jmethodID catch_jmethodID;
  2508. if (current_bci < 0) {
  2509. catch_jmethodID = 0;
  2510. current_bci = 0;
  2511. } else {
  2512. catch_jmethodID = jem.to_jmethodID(current_mh);
  2513. }
  2514. JvmtiJavaThreadEventTransition jet(thread);
  2515. jvmtiEventException callback = env->callbacks()->Exception;
  2516. if (callback != NULL) {
  2517. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  2518. jem.jni_methodID(), jem.location(),
  2519. jem.exception(),
  2520. catch_jmethodID, current_bci);
  2521. }
  2522. }
  2523. }
  2524. }
  2525. state->invalidate_cur_stack_depth();
  2526. }
  2527. void JvmtiExport::notice_unwind_due_to_exception(JavaThread *thread, Method* method, address location, oop exception, bool in_handler_frame) {
  2528. HandleMark hm(thread);
  2529. methodHandle mh(thread, method);
  2530. Handle exception_handle(thread, exception);
  2531. JvmtiThreadState *state = thread->jvmti_thread_state();
  2532. if (state == NULL) {
  2533. return;
  2534. }
  2535. EVT_TRIG_TRACE(JVMTI_EVENT_EXCEPTION_CATCH,
  2536. ("JVMTI [%s] Trg unwind_due_to_exception triggered %s.%s @ %s%d - %s",
  2537. JvmtiTrace::safe_get_thread_name(thread),
  2538. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2539. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
  2540. location==0? "no location:" : "",
  2541. location==0? 0 : location - mh()->code_base(),
  2542. in_handler_frame? "in handler frame" : "not handler frame" ));
  2543. if (state->is_exception_detected()) {
  2544. state->invalidate_cur_stack_depth();
  2545. if (!in_handler_frame) {
  2546. if(state->is_interp_only_mode()) {
  2547. JvmtiExport::post_method_exit(thread, method, thread->last_frame());
  2548. state->invalidate_cur_stack_depth();
  2549. }
  2550. } else {
  2551. assert(location != NULL, "must be a known location");
  2552. assert(!state->is_exception_caught(), "exception must not be caught yet.");
  2553. state->set_exception_caught();
  2554. JvmtiEnvThreadStateIterator it(state);
  2555. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2556. if (ets->is_enabled(JVMTI_EVENT_EXCEPTION_CATCH) && (exception_handle() != NULL)) {
  2557. EVT_TRACE(JVMTI_EVENT_EXCEPTION_CATCH,
  2558. ("JVMTI [%s] Evt ExceptionCatch sent %s.%s @ %d",
  2559. JvmtiTrace::safe_get_thread_name(thread),
  2560. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2561. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
  2562. location - mh()->code_base() ));
  2563. JvmtiEnv *env = ets->get_env();
  2564. JvmtiExceptionEventMark jem(thread, mh, location, exception_handle);
  2565. JvmtiJavaThreadEventTransition jet(thread);
  2566. jvmtiEventExceptionCatch callback = env->callbacks()->ExceptionCatch;
  2567. if (callback != NULL) {
  2568. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  2569. jem.jni_methodID(), jem.location(),
  2570. jem.exception());
  2571. }
  2572. }
  2573. }
  2574. }
  2575. }
  2576. }
  2577. oop JvmtiExport::jni_GetField_probe(JavaThread *thread, jobject jobj, oop obj,
  2578. Klass* klass, jfieldID fieldID, bool is_static) {
  2579. if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) {
  2580. post_field_access_by_jni(thread, obj, klass, fieldID, is_static);
  2581. if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
  2582. }
  2583. return obj;
  2584. }
  2585. oop JvmtiExport::jni_GetField_probe_nh(JavaThread *thread, jobject jobj, oop obj,
  2586. Klass* klass, jfieldID fieldID, bool is_static) {
  2587. if (*((int *)get_field_access_count_addr()) > 0 && thread->has_last_Java_frame()) {
  2588. ResetNoHandleMark rnhm;
  2589. post_field_access_by_jni(thread, obj, klass, fieldID, is_static);
  2590. if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
  2591. }
  2592. return obj;
  2593. }
  2594. void JvmtiExport::post_field_access_by_jni(JavaThread *thread, oop obj,
  2595. Klass* klass, jfieldID fieldID, bool is_static) {
  2596. assert(thread->has_last_Java_frame(), "must be called with a Java context");
  2597. ResourceMark rm;
  2598. fieldDescriptor fd;
  2599. bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd);
  2600. assert(valid_fieldID == true,"post_field_access_by_jni called with invalid fieldID");
  2601. if (!valid_fieldID) return;
  2602. if (!fd.is_field_access_watched()) return;
  2603. HandleMark hm(thread);
  2604. KlassHandle h_klass(thread, klass);
  2605. Handle h_obj;
  2606. if (!is_static) {
  2607. assert(obj != NULL, "non-static needs an object");
  2608. h_obj = Handle(thread, obj);
  2609. }
  2610. post_field_access(thread,
  2611. thread->last_frame().interpreter_frame_method(),
  2612. thread->last_frame().interpreter_frame_bcp(),
  2613. h_klass, h_obj, fieldID);
  2614. }
  2615. void JvmtiExport::post_field_access(JavaThread *thread, Method* method,
  2616. address location, KlassHandle field_klass, Handle object, jfieldID field) {
  2617. HandleMark hm(thread);
  2618. methodHandle mh(thread, method);
  2619. JvmtiThreadState *state = thread->jvmti_thread_state();
  2620. if (state == NULL) {
  2621. return;
  2622. }
  2623. EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("JVMTI [%s] Trg Field Access event triggered",
  2624. JvmtiTrace::safe_get_thread_name(thread)));
  2625. JvmtiEnvThreadStateIterator it(state);
  2626. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2627. if (ets->is_enabled(JVMTI_EVENT_FIELD_ACCESS)) {
  2628. EVT_TRACE(JVMTI_EVENT_FIELD_ACCESS, ("JVMTI [%s] Evt Field Access event sent %s.%s @ %d",
  2629. JvmtiTrace::safe_get_thread_name(thread),
  2630. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2631. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
  2632. location - mh()->code_base() ));
  2633. JvmtiEnv *env = ets->get_env();
  2634. JvmtiLocationEventMark jem(thread, mh, location);
  2635. jclass field_jclass = jem.to_jclass(field_klass());
  2636. jobject field_jobject = jem.to_jobject(object());
  2637. JvmtiJavaThreadEventTransition jet(thread);
  2638. jvmtiEventFieldAccess callback = env->callbacks()->FieldAccess;
  2639. if (callback != NULL) {
  2640. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  2641. jem.jni_methodID(), jem.location(),
  2642. field_jclass, field_jobject, field);
  2643. }
  2644. }
  2645. }
  2646. }
  2647. oop JvmtiExport::jni_SetField_probe(JavaThread *thread, jobject jobj, oop obj,
  2648. Klass* klass, jfieldID fieldID, bool is_static,
  2649. char sig_type, jvalue *value) {
  2650. if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) {
  2651. post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value);
  2652. if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
  2653. }
  2654. return obj;
  2655. }
  2656. oop JvmtiExport::jni_SetField_probe_nh(JavaThread *thread, jobject jobj, oop obj,
  2657. Klass* klass, jfieldID fieldID, bool is_static,
  2658. char sig_type, jvalue *value) {
  2659. if (*((int *)get_field_modification_count_addr()) > 0 && thread->has_last_Java_frame()) {
  2660. ResetNoHandleMark rnhm;
  2661. post_field_modification_by_jni(thread, obj, klass, fieldID, is_static, sig_type, value);
  2662. if (jobj != NULL) return JNIHandles::resolve_non_null(jobj);
  2663. }
  2664. return obj;
  2665. }
  2666. void JvmtiExport::post_field_modification_by_jni(JavaThread *thread, oop obj,
  2667. Klass* klass, jfieldID fieldID, bool is_static,
  2668. char sig_type, jvalue *value) {
  2669. assert(thread->has_last_Java_frame(), "must be called with Java context");
  2670. ResourceMark rm;
  2671. fieldDescriptor fd;
  2672. bool valid_fieldID = JvmtiEnv::get_field_descriptor(klass, fieldID, &fd);
  2673. assert(valid_fieldID == true,"post_field_modification_by_jni called with invalid fieldID");
  2674. if (!valid_fieldID) return;
  2675. if (!fd.is_field_modification_watched()) return;
  2676. HandleMark hm(thread);
  2677. Handle h_obj;
  2678. if (!is_static) {
  2679. assert(obj != NULL, "non-static needs an object");
  2680. h_obj = Handle(thread, obj);
  2681. }
  2682. KlassHandle h_klass(thread, klass);
  2683. post_field_modification(thread,
  2684. thread->last_frame().interpreter_frame_method(),
  2685. thread->last_frame().interpreter_frame_bcp(),
  2686. h_klass, h_obj, fieldID, sig_type, value);
  2687. }
  2688. void JvmtiExport::post_raw_field_modification(JavaThread *thread, Method* method,
  2689. address location, KlassHandle field_klass, Handle object, jfieldID field,
  2690. char sig_type, jvalue *value) {
  2691. if (sig_type == 'I' || sig_type == 'Z' || sig_type == 'B' || sig_type == 'C' || sig_type == 'S') {
  2692. fieldDescriptor fd;
  2693. bool found = JvmtiEnv::get_field_descriptor(field_klass(), field, &fd);
  2694. if (found) {
  2695. jint ival = value->i;
  2696. switch (fd.field_type()) {
  2697. case T_BOOLEAN:
  2698. sig_type = 'Z';
  2699. value->i = 0; // clear it
  2700. value->z = (jboolean)ival;
  2701. break;
  2702. case T_BYTE:
  2703. sig_type = 'B';
  2704. value->i = 0; // clear it
  2705. value->b = (jbyte)ival;
  2706. break;
  2707. case T_CHAR:
  2708. sig_type = 'C';
  2709. value->i = 0; // clear it
  2710. value->c = (jchar)ival;
  2711. break;
  2712. case T_SHORT:
  2713. sig_type = 'S';
  2714. value->i = 0; // clear it
  2715. value->s = (jshort)ival;
  2716. break;
  2717. case T_INT:
  2718. break;
  2719. default:
  2720. ShouldNotReachHere();
  2721. break;
  2722. }
  2723. }
  2724. }
  2725. assert(sig_type != '[', "array should have sig_type == 'L'");
  2726. bool handle_created = false;
  2727. if (sig_type == 'L') {
  2728. handle_created = true;
  2729. value->l = (jobject)JNIHandles::make_local(thread, (oop)value->l);
  2730. }
  2731. post_field_modification(thread, method, location, field_klass, object, field, sig_type, value);
  2732. if (handle_created) {
  2733. JNIHandles::destroy_local(value->l);
  2734. }
  2735. }
  2736. void JvmtiExport::post_field_modification(JavaThread *thread, Method* method,
  2737. address location, KlassHandle field_klass, Handle object, jfieldID field,
  2738. char sig_type, jvalue *value_ptr) {
  2739. HandleMark hm(thread);
  2740. methodHandle mh(thread, method);
  2741. JvmtiThreadState *state = thread->jvmti_thread_state();
  2742. if (state == NULL) {
  2743. return;
  2744. }
  2745. EVT_TRIG_TRACE(JVMTI_EVENT_FIELD_MODIFICATION,
  2746. ("JVMTI [%s] Trg Field Modification event triggered",
  2747. JvmtiTrace::safe_get_thread_name(thread)));
  2748. JvmtiEnvThreadStateIterator it(state);
  2749. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  2750. if (ets->is_enabled(JVMTI_EVENT_FIELD_MODIFICATION)) {
  2751. EVT_TRACE(JVMTI_EVENT_FIELD_MODIFICATION,
  2752. ("JVMTI [%s] Evt Field Modification event sent %s.%s @ %d",
  2753. JvmtiTrace::safe_get_thread_name(thread),
  2754. (mh() == NULL) ? "NULL" : mh()->klass_name()->as_C_string(),
  2755. (mh() == NULL) ? "NULL" : mh()->name()->as_C_string(),
  2756. location - mh()->code_base() ));
  2757. JvmtiEnv *env = ets->get_env();
  2758. JvmtiLocationEventMark jem(thread, mh, location);
  2759. jclass field_jclass = jem.to_jclass(field_klass());
  2760. jobject field_jobject = jem.to_jobject(object());
  2761. JvmtiJavaThreadEventTransition jet(thread);
  2762. jvmtiEventFieldModification callback = env->callbacks()->FieldModification;
  2763. if (callback != NULL) {
  2764. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  2765. jem.jni_methodID(), jem.location(),
  2766. field_jclass, field_jobject, field, sig_type, *value_ptr);
  2767. }
  2768. }
  2769. }
  2770. }
  2771. void JvmtiExport::post_native_method_bind(Method* method, address* function_ptr) {
  2772. JavaThread* thread = JavaThread::current();
  2773. assert(thread->thread_state() == _thread_in_vm, "must be in vm state");
  2774. HandleMark hm(thread);
  2775. methodHandle mh(thread, method);
  2776. EVT_TRIG_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("JVMTI [%s] Trg Native Method Bind event triggered",
  2777. JvmtiTrace::safe_get_thread_name(thread)));
  2778. if (JvmtiEventController::is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) {
  2779. JvmtiEnvIterator it;
  2780. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  2781. if (env->is_enabled(JVMTI_EVENT_NATIVE_METHOD_BIND)) {
  2782. EVT_TRACE(JVMTI_EVENT_NATIVE_METHOD_BIND, ("JVMTI [%s] Evt Native Method Bind event sent",
  2783. JvmtiTrace::safe_get_thread_name(thread) ));
  2784. JvmtiMethodEventMark jem(thread, mh);
  2785. JvmtiJavaThreadEventTransition jet(thread);
  2786. JNIEnv* jni_env = JvmtiEnv::get_phase() == JVMTI_PHASE_PRIMORDIAL? NULL : jem.jni_env();
  2787. jvmtiEventNativeMethodBind callback = env->callbacks()->NativeMethodBind;
  2788. if (callback != NULL) {
  2789. (*callback)(env->jvmti_external(), jni_env, jem.jni_thread(),
  2790. jem.jni_methodID(), (void*)(*function_ptr), (void**)function_ptr);
  2791. }
  2792. }
  2793. }
  2794. }
  2795. }
  2796. jvmtiCompiledMethodLoadInlineRecord* create_inline_record(nmethod* nm) {
  2797. jint numstackframes = 0;
  2798. jvmtiCompiledMethodLoadInlineRecord* record = (jvmtiCompiledMethodLoadInlineRecord*)NEW_RESOURCE_OBJ(jvmtiCompiledMethodLoadInlineRecord);
  2799. record->header.kind = JVMTI_CMLR_INLINE_INFO;
  2800. record->header.next = NULL;
  2801. record->header.majorinfoversion = JVMTI_CMLR_MAJOR_VERSION_1;
  2802. record->header.minorinfoversion = JVMTI_CMLR_MINOR_VERSION_0;
  2803. record->numpcs = 0;
  2804. for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) {
  2805. if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue;
  2806. record->numpcs++;
  2807. }
  2808. record->pcinfo = (PCStackInfo*)(NEW_RESOURCE_ARRAY(PCStackInfo, record->numpcs));
  2809. int scope = 0;
  2810. for(PcDesc* p = nm->scopes_pcs_begin(); p < nm->scopes_pcs_end(); p++) {
  2811. if(p->scope_decode_offset() == DebugInformationRecorder::serialized_null) continue;
  2812. void* pc_address = (void*)p->real_pc(nm);
  2813. assert(pc_address != NULL, "pc_address must be non-null");
  2814. record->pcinfo[scope].pc = pc_address;
  2815. numstackframes=0;
  2816. for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) {
  2817. numstackframes++;
  2818. }
  2819. assert(numstackframes != 0, "numstackframes must be nonzero.");
  2820. record->pcinfo[scope].methods = (jmethodID *)NEW_RESOURCE_ARRAY(jmethodID, numstackframes);
  2821. record->pcinfo[scope].bcis = (jint *)NEW_RESOURCE_ARRAY(jint, numstackframes);
  2822. record->pcinfo[scope].numstackframes = numstackframes;
  2823. int stackframe = 0;
  2824. for(ScopeDesc* sd = nm->scope_desc_at(p->real_pc(nm));sd != NULL;sd = sd->sender()) {
  2825. assert(sd->method() != NULL, "sd->method() cannot be null.");
  2826. record->pcinfo[scope].methods[stackframe] = sd->method()->jmethod_id();
  2827. record->pcinfo[scope].bcis[stackframe] = sd->bci();
  2828. stackframe++;
  2829. }
  2830. scope++;
  2831. }
  2832. return record;
  2833. }
  2834. void JvmtiExport::post_compiled_method_load(nmethod *nm) {
  2835. JavaThread* thread = JavaThread::current();
  2836. EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
  2837. ("JVMTI [%s] method compile load event triggered",
  2838. JvmtiTrace::safe_get_thread_name(thread)));
  2839. JvmtiEnvIterator it;
  2840. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  2841. if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) {
  2842. EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
  2843. ("JVMTI [%s] class compile method load event sent %s.%s ",
  2844. JvmtiTrace::safe_get_thread_name(thread),
  2845. (nm->method() == NULL) ? "NULL" : nm->method()->klass_name()->as_C_string(),
  2846. (nm->method() == NULL) ? "NULL" : nm->method()->name()->as_C_string()));
  2847. ResourceMark rm(thread);
  2848. HandleMark hm(thread);
  2849. jvmtiCompiledMethodLoadInlineRecord* inlinerecord = create_inline_record(nm);
  2850. JvmtiCompiledMethodLoadEventMark jem(thread, nm, inlinerecord);
  2851. JvmtiJavaThreadEventTransition jet(thread);
  2852. jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad;
  2853. if (callback != NULL) {
  2854. (*callback)(env->jvmti_external(), jem.jni_methodID(),
  2855. jem.code_size(), jem.code_data(), jem.map_length(),
  2856. jem.map(), jem.compile_info());
  2857. }
  2858. }
  2859. }
  2860. }
  2861. void JvmtiExport::post_compiled_method_load(JvmtiEnv* env, const jmethodID method, const jint length,
  2862. const void *code_begin, const jint map_length,
  2863. const jvmtiAddrLocationMap* map)
  2864. {
  2865. JavaThread* thread = JavaThread::current();
  2866. EVT_TRIG_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
  2867. ("JVMTI [%s] method compile load event triggered (by GenerateEvents)",
  2868. JvmtiTrace::safe_get_thread_name(thread)));
  2869. if (env->is_enabled(JVMTI_EVENT_COMPILED_METHOD_LOAD)) {
  2870. EVT_TRACE(JVMTI_EVENT_COMPILED_METHOD_LOAD,
  2871. ("JVMTI [%s] class compile method load event sent (by GenerateEvents), jmethodID=" PTR_FORMAT,
  2872. JvmtiTrace::safe_get_thread_name(thread), method));
  2873. JvmtiEventMark jem(thread);
  2874. JvmtiJavaThreadEventTransition jet(thread);
  2875. jvmtiEventCompiledMethodLoad callback = env->callbacks()->CompiledMethodLoad;
  2876. if (callback != NULL) {
  2877. (*callback)(env->jvmti_external(), method,
  2878. length, code_begin, map_length,
  2879. map, NULL);
  2880. }
  2881. }
  2882. }
  2883. void JvmtiExport::post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) {
  2884. assert(name != NULL && name[0] != '\0', "sanity check");
  2885. JavaThread* thread = JavaThread::current();
  2886. ThreadInVMfromUnknown __tiv;
  2887. EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
  2888. ("JVMTI [%s] method dynamic code generated event triggered",
  2889. JvmtiTrace::safe_get_thread_name(thread)));
  2890. JvmtiEnvIterator it;
  2891. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  2892. if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) {
  2893. EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
  2894. ("JVMTI [%s] dynamic code generated event sent for %s",
  2895. JvmtiTrace::safe_get_thread_name(thread), name));
  2896. JvmtiEventMark jem(thread);
  2897. JvmtiJavaThreadEventTransition jet(thread);
  2898. jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char));
  2899. jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated;
  2900. if (callback != NULL) {
  2901. (*callback)(env->jvmti_external(), name, (void*)code_begin, length);
  2902. }
  2903. }
  2904. }
  2905. }
  2906. void JvmtiExport::post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) {
  2907. jvmtiPhase phase = JvmtiEnv::get_phase();
  2908. if (phase == JVMTI_PHASE_PRIMORDIAL || phase == JVMTI_PHASE_START) {
  2909. post_dynamic_code_generated_internal(name, code_begin, code_end);
  2910. } else {
  2911. MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
  2912. JvmtiDeferredEvent event = JvmtiDeferredEvent::dynamic_code_generated_event(
  2913. name, code_begin, code_end);
  2914. JvmtiDeferredEventQueue::enqueue(event);
  2915. }
  2916. }
  2917. void JvmtiExport::post_dynamic_code_generated(JvmtiEnv* env, const char *name,
  2918. const void *code_begin, const void *code_end)
  2919. {
  2920. JavaThread* thread = JavaThread::current();
  2921. EVT_TRIG_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
  2922. ("JVMTI [%s] dynamic code generated event triggered (by GenerateEvents)",
  2923. JvmtiTrace::safe_get_thread_name(thread)));
  2924. if (env->is_enabled(JVMTI_EVENT_DYNAMIC_CODE_GENERATED)) {
  2925. EVT_TRACE(JVMTI_EVENT_DYNAMIC_CODE_GENERATED,
  2926. ("JVMTI [%s] dynamic code generated event sent for %s",
  2927. JvmtiTrace::safe_get_thread_name(thread), name));
  2928. JvmtiEventMark jem(thread);
  2929. JvmtiJavaThreadEventTransition jet(thread);
  2930. jint length = (jint)pointer_delta(code_end, code_begin, sizeof(char));
  2931. jvmtiEventDynamicCodeGenerated callback = env->callbacks()->DynamicCodeGenerated;
  2932. if (callback != NULL) {
  2933. (*callback)(env->jvmti_external(), name, (void*)code_begin, length);
  2934. }
  2935. }
  2936. }
  2937. void JvmtiExport::post_dynamic_code_generated_while_holding_locks(const char* name,
  2938. address code_begin, address code_end)
  2939. {
  2940. JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
  2941. guarantee(state != NULL, "attempt to register stub via an exiting thread");
  2942. JvmtiDynamicCodeEventCollector* collector = state->get_dynamic_code_event_collector();
  2943. guarantee(collector != NULL, "attempt to register stub without event collector");
  2944. collector->register_stub(name, code_begin, code_end);
  2945. }
  2946. void JvmtiExport::record_vm_internal_object_allocation(oop obj) {
  2947. Thread* thread = ThreadLocalStorage::thread();
  2948. if (thread != NULL && thread->is_Java_thread()) {
  2949. No_Safepoint_Verifier no_sfpt;
  2950. JvmtiThreadState *state = ((JavaThread*)thread)->jvmti_thread_state();
  2951. if (state != NULL ) {
  2952. JvmtiVMObjectAllocEventCollector *collector;
  2953. collector = state->get_vm_object_alloc_event_collector();
  2954. if (collector != NULL && collector->is_enabled()) {
  2955. if (obj->klass() != SystemDictionary::Class_klass()) {
  2956. collector->record_allocation(obj);
  2957. }
  2958. }
  2959. }
  2960. }
  2961. }
  2962. void JvmtiExport::post_garbage_collection_finish() {
  2963. Thread *thread = Thread::current(); // this event is posted from VM-Thread.
  2964. EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
  2965. ("JVMTI [%s] garbage collection finish event triggered",
  2966. JvmtiTrace::safe_get_thread_name(thread)));
  2967. JvmtiEnvIterator it;
  2968. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  2969. if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)) {
  2970. EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,
  2971. ("JVMTI [%s] garbage collection finish event sent ",
  2972. JvmtiTrace::safe_get_thread_name(thread)));
  2973. JvmtiThreadEventTransition jet(thread);
  2974. jvmtiEventGarbageCollectionFinish callback = env->callbacks()->GarbageCollectionFinish;
  2975. if (callback != NULL) {
  2976. (*callback)(env->jvmti_external());
  2977. }
  2978. }
  2979. }
  2980. }
  2981. void JvmtiExport::post_garbage_collection_start() {
  2982. Thread* thread = Thread::current(); // this event is posted from vm-thread.
  2983. EVT_TRIG_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
  2984. ("JVMTI [%s] garbage collection start event triggered",
  2985. JvmtiTrace::safe_get_thread_name(thread)));
  2986. JvmtiEnvIterator it;
  2987. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  2988. if (env->is_enabled(JVMTI_EVENT_GARBAGE_COLLECTION_START)) {
  2989. EVT_TRACE(JVMTI_EVENT_GARBAGE_COLLECTION_START,
  2990. ("JVMTI [%s] garbage collection start event sent ",
  2991. JvmtiTrace::safe_get_thread_name(thread)));
  2992. JvmtiThreadEventTransition jet(thread);
  2993. jvmtiEventGarbageCollectionStart callback = env->callbacks()->GarbageCollectionStart;
  2994. if (callback != NULL) {
  2995. (*callback)(env->jvmti_external());
  2996. }
  2997. }
  2998. }
  2999. }
  3000. void JvmtiExport::post_data_dump() {
  3001. Thread *thread = Thread::current();
  3002. EVT_TRIG_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST,
  3003. ("JVMTI [%s] data dump request event triggered",
  3004. JvmtiTrace::safe_get_thread_name(thread)));
  3005. JvmtiEnvIterator it;
  3006. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  3007. if (env->is_enabled(JVMTI_EVENT_DATA_DUMP_REQUEST)) {
  3008. EVT_TRACE(JVMTI_EVENT_DATA_DUMP_REQUEST,
  3009. ("JVMTI [%s] data dump request event sent ",
  3010. JvmtiTrace::safe_get_thread_name(thread)));
  3011. JvmtiThreadEventTransition jet(thread);
  3012. jvmtiEventDataDumpRequest callback = env->callbacks()->DataDumpRequest;
  3013. if (callback != NULL) {
  3014. (*callback)(env->jvmti_external());
  3015. }
  3016. }
  3017. }
  3018. }
  3019. void JvmtiExport::post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) {
  3020. oop object = (oop)obj_mntr->object();
  3021. if (!ServiceUtil::visible_oop(object)) {
  3022. return;
  3023. }
  3024. JvmtiThreadState *state = thread->jvmti_thread_state();
  3025. if (state == NULL) {
  3026. return;
  3027. }
  3028. HandleMark hm(thread);
  3029. Handle h(thread, object);
  3030. EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
  3031. ("JVMTI [%s] montior contended enter event triggered",
  3032. JvmtiTrace::safe_get_thread_name(thread)));
  3033. JvmtiEnvThreadStateIterator it(state);
  3034. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  3035. if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTER)) {
  3036. EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
  3037. ("JVMTI [%s] monitor contended enter event sent",
  3038. JvmtiTrace::safe_get_thread_name(thread)));
  3039. JvmtiMonitorEventMark jem(thread, h());
  3040. JvmtiEnv *env = ets->get_env();
  3041. JvmtiThreadEventTransition jet(thread);
  3042. jvmtiEventMonitorContendedEnter callback = env->callbacks()->MonitorContendedEnter;
  3043. if (callback != NULL) {
  3044. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object());
  3045. }
  3046. }
  3047. }
  3048. }
  3049. void JvmtiExport::post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) {
  3050. oop object = (oop)obj_mntr->object();
  3051. if (!ServiceUtil::visible_oop(object)) {
  3052. return;
  3053. }
  3054. JvmtiThreadState *state = thread->jvmti_thread_state();
  3055. if (state == NULL) {
  3056. return;
  3057. }
  3058. HandleMark hm(thread);
  3059. Handle h(thread, object);
  3060. EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
  3061. ("JVMTI [%s] montior contended entered event triggered",
  3062. JvmtiTrace::safe_get_thread_name(thread)));
  3063. JvmtiEnvThreadStateIterator it(state);
  3064. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  3065. if (ets->is_enabled(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED)) {
  3066. EVT_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
  3067. ("JVMTI [%s] monitor contended enter event sent",
  3068. JvmtiTrace::safe_get_thread_name(thread)));
  3069. JvmtiMonitorEventMark jem(thread, h());
  3070. JvmtiEnv *env = ets->get_env();
  3071. JvmtiThreadEventTransition jet(thread);
  3072. jvmtiEventMonitorContendedEntered callback = env->callbacks()->MonitorContendedEntered;
  3073. if (callback != NULL) {
  3074. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(), jem.jni_object());
  3075. }
  3076. }
  3077. }
  3078. }
  3079. void JvmtiExport::post_monitor_wait(JavaThread *thread, oop object,
  3080. jlong timeout) {
  3081. JvmtiThreadState *state = thread->jvmti_thread_state();
  3082. if (state == NULL) {
  3083. return;
  3084. }
  3085. HandleMark hm(thread);
  3086. Handle h(thread, object);
  3087. EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAIT,
  3088. ("JVMTI [%s] montior wait event triggered",
  3089. JvmtiTrace::safe_get_thread_name(thread)));
  3090. JvmtiEnvThreadStateIterator it(state);
  3091. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  3092. if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAIT)) {
  3093. EVT_TRACE(JVMTI_EVENT_MONITOR_WAIT,
  3094. ("JVMTI [%s] monitor wait event sent ",
  3095. JvmtiTrace::safe_get_thread_name(thread)));
  3096. JvmtiMonitorEventMark jem(thread, h());
  3097. JvmtiEnv *env = ets->get_env();
  3098. JvmtiThreadEventTransition jet(thread);
  3099. jvmtiEventMonitorWait callback = env->callbacks()->MonitorWait;
  3100. if (callback != NULL) {
  3101. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  3102. jem.jni_object(), timeout);
  3103. }
  3104. }
  3105. }
  3106. }
  3107. void JvmtiExport::post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) {
  3108. oop object = (oop)obj_mntr->object();
  3109. if (!ServiceUtil::visible_oop(object)) {
  3110. return;
  3111. }
  3112. JvmtiThreadState *state = thread->jvmti_thread_state();
  3113. if (state == NULL) {
  3114. return;
  3115. }
  3116. HandleMark hm(thread);
  3117. Handle h(thread, object);
  3118. EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAITED,
  3119. ("JVMTI [%s] montior waited event triggered",
  3120. JvmtiTrace::safe_get_thread_name(thread)));
  3121. JvmtiEnvThreadStateIterator it(state);
  3122. for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
  3123. if (ets->is_enabled(JVMTI_EVENT_MONITOR_WAITED)) {
  3124. EVT_TRACE(JVMTI_EVENT_MONITOR_WAITED,
  3125. ("JVMTI [%s] monitor waited event sent ",
  3126. JvmtiTrace::safe_get_thread_name(thread)));
  3127. JvmtiMonitorEventMark jem(thread, h());
  3128. JvmtiEnv *env = ets->get_env();
  3129. JvmtiThreadEventTransition jet(thread);
  3130. jvmtiEventMonitorWaited callback = env->callbacks()->MonitorWaited;
  3131. if (callback != NULL) {
  3132. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  3133. jem.jni_object(), timed_out);
  3134. }
  3135. }
  3136. }
  3137. }
  3138. void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
  3139. EVT_TRIG_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("JVMTI [%s] Trg vm object alloc triggered",
  3140. JvmtiTrace::safe_get_thread_name(thread)));
  3141. if (object == NULL) {
  3142. return;
  3143. }
  3144. HandleMark hm(thread);
  3145. Handle h(thread, object);
  3146. JvmtiEnvIterator it;
  3147. for (JvmtiEnv* env = it.first(); env != NULL; env = it.next(env)) {
  3148. if (env->is_enabled(JVMTI_EVENT_VM_OBJECT_ALLOC)) {
  3149. EVT_TRACE(JVMTI_EVENT_VM_OBJECT_ALLOC, ("JVMTI [%s] Evt vmobject alloc sent %s",
  3150. JvmtiTrace::safe_get_thread_name(thread),
  3151. object==NULL? "NULL" : java_lang_Class::as_Klass(object)->external_name()));
  3152. JvmtiVMObjectAllocEventMark jem(thread, h());
  3153. JvmtiJavaThreadEventTransition jet(thread);
  3154. jvmtiEventVMObjectAlloc callback = env->callbacks()->VMObjectAlloc;
  3155. if (callback != NULL) {
  3156. (*callback)(env->jvmti_external(), jem.jni_env(), jem.jni_thread(),
  3157. jem.jni_jobject(), jem.jni_class(), jem.size());
  3158. }
  3159. }
  3160. }
  3161. }
  3162. void JvmtiExport::cleanup_thread(JavaThread* thread) {
  3163. assert(JavaThread::current() == thread, "thread is not current");
  3164. MutexLocker mu(JvmtiThreadState_lock);
  3165. if (thread->jvmti_thread_state() != NULL) {
  3166. JvmtiEventController::thread_ended(thread);
  3167. }
  3168. }
  3169. void JvmtiExport::clear_detected_exception(JavaThread* thread) {
  3170. assert(JavaThread::current() == thread, "thread is not current");
  3171. JvmtiThreadState* state = thread->jvmti_thread_state();
  3172. if (state != NULL) {
  3173. state->clear_exception_detected();
  3174. }
  3175. }
  3176. void JvmtiExport::oops_do(OopClosure* f) {
  3177. JvmtiCurrentBreakpoints::oops_do(f);
  3178. JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(f);
  3179. }
  3180. void JvmtiExport::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
  3181. JvmtiTagMap::weak_oops_do(is_alive, f);
  3182. }
  3183. void JvmtiExport::gc_epilogue() {
  3184. JvmtiCurrentBreakpoints::gc_epilogue();
  3185. }
  3186. void JvmtiExport::transition_pending_onload_raw_monitors() {
  3187. JvmtiPendingMonitors::transition_raw_monitors();
  3188. }
  3189. extern "C" {
  3190. typedef jint (JNICALL *OnAttachEntry_t)(JavaVM*, char *, void *);
  3191. }
  3192. jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) {
  3193. char ebuf[1024];
  3194. char buffer[JVM_MAXPATHLEN];
  3195. void* library = NULL;
  3196. jint result = JNI_ERR;
  3197. const char *on_attach_symbols[] = AGENT_ONATTACH_SYMBOLS;
  3198. size_t num_symbol_entries = ARRAY_SIZE(on_attach_symbols);
  3199. const char* agent = op->arg(0);
  3200. const char* absParam = op->arg(1);
  3201. const char* options = op->arg(2);
  3202. bool is_absolute_path = (absParam != NULL) && (strcmp(absParam,"true")==0);
  3203. AgentLibrary *agent_lib = new AgentLibrary(agent, options, is_absolute_path, NULL);
  3204. if (!os::find_builtin_agent(agent_lib, on_attach_symbols, num_symbol_entries)) {
  3205. if (is_absolute_path) {
  3206. library = os::dll_load(agent, ebuf, sizeof ebuf);
  3207. } else {
  3208. if (os::dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(),
  3209. agent)) {
  3210. library = os::dll_load(buffer, ebuf, sizeof ebuf);
  3211. }
  3212. if (library == NULL) {
  3213. char ns[1] = {0};
  3214. if (os::dll_build_name(buffer, sizeof(buffer), ns, agent)) {
  3215. library = os::dll_load(buffer, ebuf, sizeof ebuf);
  3216. }
  3217. }
  3218. }
  3219. if (library != NULL) {
  3220. agent_lib->set_os_lib(library);
  3221. agent_lib->set_valid();
  3222. }
  3223. }
  3224. if (agent_lib->valid()) {
  3225. OnAttachEntry_t on_attach_entry = NULL;
  3226. on_attach_entry = CAST_TO_FN_PTR(OnAttachEntry_t,
  3227. os::find_agent_function(agent_lib, false, on_attach_symbols, num_symbol_entries));
  3228. if (on_attach_entry == NULL) {
  3229. if (!agent_lib->is_static_lib()) {
  3230. os::dll_unload(library);
  3231. }
  3232. delete agent_lib;
  3233. } else {
  3234. JavaThread* THREAD = JavaThread::current();
  3235. {
  3236. extern struct JavaVM_ main_vm;
  3237. JvmtiThreadEventMark jem(THREAD);
  3238. JvmtiJavaThreadEventTransition jet(THREAD);
  3239. result = (*on_attach_entry)(&main_vm, (char*)options, NULL);
  3240. }
  3241. if (HAS_PENDING_EXCEPTION) {
  3242. CLEAR_PENDING_EXCEPTION;
  3243. }
  3244. if (result == JNI_OK) {
  3245. Arguments::add_loaded_agent(agent_lib);
  3246. } else {
  3247. delete agent_lib;
  3248. }
  3249. st->print_cr("%d", result);
  3250. result = JNI_OK;
  3251. }
  3252. }
  3253. return result;
  3254. }
  3255. void JvmtiEventCollector::setup_jvmti_thread_state() {
  3256. JvmtiThreadState* state = JvmtiThreadState::state_for(JavaThread::current());
  3257. guarantee(state != NULL, "exiting thread called setup_jvmti_thread_state");
  3258. if (is_vm_object_alloc_event()) {
  3259. _prev = state->get_vm_object_alloc_event_collector();
  3260. state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)this);
  3261. } else if (is_dynamic_code_event()) {
  3262. _prev = state->get_dynamic_code_event_collector();
  3263. state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)this);
  3264. }
  3265. }
  3266. void JvmtiEventCollector::unset_jvmti_thread_state() {
  3267. JvmtiThreadState* state = JavaThread::current()->jvmti_thread_state();
  3268. if (state != NULL) {
  3269. if (is_vm_object_alloc_event()) {
  3270. if (state->get_vm_object_alloc_event_collector() == this) {
  3271. state->set_vm_object_alloc_event_collector((JvmtiVMObjectAllocEventCollector *)_prev);
  3272. } else {
  3273. }
  3274. } else {
  3275. if (is_dynamic_code_event()) {
  3276. if (state->get_dynamic_code_event_collector() == this) {
  3277. state->set_dynamic_code_event_collector((JvmtiDynamicCodeEventCollector *)_prev);
  3278. } else {
  3279. }
  3280. }
  3281. }
  3282. }
  3283. }
  3284. JvmtiDynamicCodeEventCollector::JvmtiDynamicCodeEventCollector() : _code_blobs(NULL) {
  3285. if (JvmtiExport::should_post_dynamic_code_generated()) {
  3286. setup_jvmti_thread_state();
  3287. }
  3288. }
  3289. JvmtiDynamicCodeEventCollector::~JvmtiDynamicCodeEventCollector() {
  3290. assert(!JavaThread::current()->owns_locks(), "all locks must be released to post deferred events");
  3291. if (_code_blobs != NULL) {
  3292. for (int i=0; i<_code_blobs->length(); i++) {
  3293. JvmtiCodeBlobDesc* blob = _code_blobs->at(i);
  3294. JvmtiExport::post_dynamic_code_generated(blob->name(), blob->code_begin(), blob->code_end());
  3295. FreeHeap(blob);
  3296. }
  3297. delete _code_blobs;
  3298. }
  3299. unset_jvmti_thread_state();
  3300. }
  3301. void JvmtiDynamicCodeEventCollector::register_stub(const char* name, address start, address end) {
  3302. if (_code_blobs == NULL) {
  3303. _code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(1,true);
  3304. }
  3305. _code_blobs->append(new JvmtiCodeBlobDesc(name, start, end));
  3306. }
  3307. JvmtiVMObjectAllocEventCollector::JvmtiVMObjectAllocEventCollector() : _allocated(NULL) {
  3308. if (JvmtiExport::should_post_vm_object_alloc()) {
  3309. _enable = true;
  3310. setup_jvmti_thread_state();
  3311. } else {
  3312. _enable = false;
  3313. }
  3314. }
  3315. JvmtiVMObjectAllocEventCollector::~JvmtiVMObjectAllocEventCollector() {
  3316. if (_allocated != NULL) {
  3317. set_enabled(false);
  3318. for (int i = 0; i < _allocated->length(); i++) {
  3319. oop obj = _allocated->at(i);
  3320. if (ServiceUtil::visible_oop(obj)) {
  3321. JvmtiExport::post_vm_object_alloc(JavaThread::current(), obj);
  3322. }
  3323. }
  3324. delete _allocated;
  3325. }
  3326. unset_jvmti_thread_state();
  3327. }
  3328. void JvmtiVMObjectAllocEventCollector::record_allocation(oop obj) {
  3329. assert(is_enabled(), "VM object alloc event collector is not enabled");
  3330. if (_allocated == NULL) {
  3331. _allocated = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(1, true);
  3332. }
  3333. _allocated->push(obj);
  3334. }
  3335. void JvmtiVMObjectAllocEventCollector::oops_do(OopClosure* f) {
  3336. if (_allocated != NULL) {
  3337. for(int i=_allocated->length() - 1; i >= 0; i--) {
  3338. if (_allocated->at(i) != NULL) {
  3339. f->do_oop(_allocated->adr_at(i));
  3340. }
  3341. }
  3342. }
  3343. }
  3344. void JvmtiVMObjectAllocEventCollector::oops_do_for_all_threads(OopClosure* f) {
  3345. if (!JvmtiEnv::environments_might_exist()) {
  3346. return;
  3347. }
  3348. for (JavaThread *jthr = Threads::first(); jthr != NULL; jthr = jthr->next()) {
  3349. JvmtiThreadState *state = jthr->jvmti_thread_state();
  3350. if (state != NULL) {
  3351. JvmtiVMObjectAllocEventCollector *collector;
  3352. collector = state->get_vm_object_alloc_event_collector();
  3353. while (collector != NULL) {
  3354. collector->oops_do(f);
  3355. collector = (JvmtiVMObjectAllocEventCollector *)collector->get_prev();
  3356. }
  3357. }
  3358. }
  3359. }
  3360. NoJvmtiVMObjectAllocMark::NoJvmtiVMObjectAllocMark() : _collector(NULL) {
  3361. if (!JvmtiExport::should_post_vm_object_alloc()) {
  3362. return;
  3363. }
  3364. Thread* thread = ThreadLocalStorage::thread();
  3365. if (thread != NULL && thread->is_Java_thread()) {
  3366. JavaThread* current_thread = (JavaThread*)thread;
  3367. JvmtiThreadState *state = current_thread->jvmti_thread_state();
  3368. if (state != NULL) {
  3369. JvmtiVMObjectAllocEventCollector *collector;
  3370. collector = state->get_vm_object_alloc_event_collector();
  3371. if (collector != NULL && collector->is_enabled()) {
  3372. _collector = collector;
  3373. _collector->set_enabled(false);
  3374. }
  3375. }
  3376. }
  3377. }
  3378. NoJvmtiVMObjectAllocMark::~NoJvmtiVMObjectAllocMark() {
  3379. if (was_enabled()) {
  3380. _collector->set_enabled(true);
  3381. }
  3382. };
  3383. JvmtiGCMarker::JvmtiGCMarker() {
  3384. if (!JvmtiEnv::environments_might_exist()) {
  3385. return;
  3386. }
  3387. if (JvmtiExport::should_post_garbage_collection_start()) {
  3388. JvmtiExport::post_garbage_collection_start();
  3389. }
  3390. if (SafepointSynchronize::is_at_safepoint()) {
  3391. JvmtiEnvBase::check_for_periodic_clean_up();
  3392. }
  3393. }
  3394. JvmtiGCMarker::~JvmtiGCMarker() {
  3395. if (!JvmtiEnv::environments_might_exist()) {
  3396. return;
  3397. }
  3398. if (JvmtiExport::should_post_garbage_collection_finish()) {
  3399. JvmtiExport::post_garbage_collection_finish();
  3400. }
  3401. }
  3402. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiExport.hpp
  3403. #ifndef SHARE_VM_PRIMS_JVMTIEXPORT_HPP
  3404. #define SHARE_VM_PRIMS_JVMTIEXPORT_HPP
  3405. #include "jvmtifiles/jvmti.h"
  3406. #include "memory/allocation.hpp"
  3407. #include "memory/iterator.hpp"
  3408. #include "oops/oop.hpp"
  3409. #include "oops/oopsHierarchy.hpp"
  3410. #include "runtime/frame.hpp"
  3411. #include "runtime/handles.hpp"
  3412. #include "utilities/globalDefinitions.hpp"
  3413. #include "utilities/growableArray.hpp"
  3414. #include "utilities/macros.hpp"
  3415. #include "code/jvmticmlr.h"
  3416. class JvmtiEventControllerPrivate;
  3417. class JvmtiManageCapabilities;
  3418. class JvmtiEnv;
  3419. class JvmtiThreadState;
  3420. class AttachOperation;
  3421. #define JVMTI_SUPPORT_FLAG(key) \
  3422. private: \
  3423. static bool _##key; \
  3424. public: \
  3425. inline static void set_##key(bool on) { \
  3426. JVMTI_ONLY(_##key = (on != 0)); \
  3427. NOT_JVMTI(report_unsupported(on)); \
  3428. } \
  3429. inline static bool key() { \
  3430. JVMTI_ONLY(return _##key); \
  3431. NOT_JVMTI(return false); \
  3432. }
  3433. class JvmtiExport : public AllStatic {
  3434. friend class VMStructs;
  3435. friend class CompileReplay;
  3436. private:
  3437. #if INCLUDE_JVMTI
  3438. static int _field_access_count;
  3439. static int _field_modification_count;
  3440. static bool _can_access_local_variables;
  3441. static bool _can_hotswap_or_post_breakpoint;
  3442. static bool _can_modify_any_class;
  3443. static bool _can_walk_any_space;
  3444. #endif // INCLUDE_JVMTI
  3445. JVMTI_SUPPORT_FLAG(can_get_source_debug_extension)
  3446. JVMTI_SUPPORT_FLAG(can_maintain_original_method_order)
  3447. JVMTI_SUPPORT_FLAG(can_post_interpreter_events)
  3448. JVMTI_SUPPORT_FLAG(can_post_on_exceptions)
  3449. JVMTI_SUPPORT_FLAG(can_post_breakpoint)
  3450. JVMTI_SUPPORT_FLAG(can_post_field_access)
  3451. JVMTI_SUPPORT_FLAG(can_post_field_modification)
  3452. JVMTI_SUPPORT_FLAG(can_post_method_entry)
  3453. JVMTI_SUPPORT_FLAG(can_post_method_exit)
  3454. JVMTI_SUPPORT_FLAG(can_pop_frame)
  3455. JVMTI_SUPPORT_FLAG(can_force_early_return)
  3456. friend class JvmtiEventControllerPrivate; // should only modify these flags
  3457. JVMTI_SUPPORT_FLAG(should_post_single_step)
  3458. JVMTI_SUPPORT_FLAG(should_post_field_access)
  3459. JVMTI_SUPPORT_FLAG(should_post_field_modification)
  3460. JVMTI_SUPPORT_FLAG(should_post_class_load)
  3461. JVMTI_SUPPORT_FLAG(should_post_class_prepare)
  3462. JVMTI_SUPPORT_FLAG(should_post_class_unload)
  3463. JVMTI_SUPPORT_FLAG(should_post_native_method_bind)
  3464. JVMTI_SUPPORT_FLAG(should_post_compiled_method_load)
  3465. JVMTI_SUPPORT_FLAG(should_post_compiled_method_unload)
  3466. JVMTI_SUPPORT_FLAG(should_post_dynamic_code_generated)
  3467. JVMTI_SUPPORT_FLAG(should_post_monitor_contended_enter)
  3468. JVMTI_SUPPORT_FLAG(should_post_monitor_contended_entered)
  3469. JVMTI_SUPPORT_FLAG(should_post_monitor_wait)
  3470. JVMTI_SUPPORT_FLAG(should_post_monitor_waited)
  3471. JVMTI_SUPPORT_FLAG(should_post_data_dump)
  3472. JVMTI_SUPPORT_FLAG(should_post_garbage_collection_start)
  3473. JVMTI_SUPPORT_FLAG(should_post_garbage_collection_finish)
  3474. JVMTI_SUPPORT_FLAG(should_post_on_exceptions)
  3475. JVMTI_SUPPORT_FLAG(should_post_thread_life)
  3476. JVMTI_SUPPORT_FLAG(should_post_object_free)
  3477. JVMTI_SUPPORT_FLAG(should_post_resource_exhausted)
  3478. JVMTI_SUPPORT_FLAG(should_clean_up_heap_objects)
  3479. JVMTI_SUPPORT_FLAG(should_post_vm_object_alloc)
  3480. static void report_unsupported(bool on);
  3481. friend class JvmtiManageCapabilities;
  3482. inline static void set_can_modify_any_class(bool on) {
  3483. JVMTI_ONLY(_can_modify_any_class = (on != 0);)
  3484. }
  3485. inline static void set_can_access_local_variables(bool on) {
  3486. JVMTI_ONLY(_can_access_local_variables = (on != 0);)
  3487. }
  3488. inline static void set_can_hotswap_or_post_breakpoint(bool on) {
  3489. JVMTI_ONLY(_can_hotswap_or_post_breakpoint = (on != 0);)
  3490. }
  3491. inline static void set_can_walk_any_space(bool on) {
  3492. JVMTI_ONLY(_can_walk_any_space = (on != 0);)
  3493. }
  3494. enum {
  3495. JVMTI_VERSION_MASK = 0x70000000,
  3496. JVMTI_VERSION_VALUE = 0x30000000,
  3497. JVMDI_VERSION_VALUE = 0x20000000
  3498. };
  3499. static void post_field_modification(JavaThread *thread, Method* method, address location,
  3500. KlassHandle field_klass, Handle object, jfieldID field,
  3501. char sig_type, jvalue *value);
  3502. static void post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) NOT_JVMTI_RETURN;
  3503. private:
  3504. friend class JvmtiCodeBlobEvents;
  3505. static void post_compiled_method_load(JvmtiEnv* env, const jmethodID method, const jint length,
  3506. const void *code_begin, const jint map_length,
  3507. const jvmtiAddrLocationMap* map) NOT_JVMTI_RETURN;
  3508. static void post_dynamic_code_generated(JvmtiEnv* env, const char *name, const void *code_begin,
  3509. const void *code_end) NOT_JVMTI_RETURN;
  3510. static bool _has_redefined_a_class;
  3511. friend class VM_RedefineClasses;
  3512. inline static void set_has_redefined_a_class() {
  3513. JVMTI_ONLY(_has_redefined_a_class = true;)
  3514. }
  3515. static bool _all_dependencies_are_recorded;
  3516. public:
  3517. inline static bool has_redefined_a_class() {
  3518. JVMTI_ONLY(return _has_redefined_a_class);
  3519. NOT_JVMTI(return false);
  3520. }
  3521. inline static bool all_dependencies_are_recorded() {
  3522. return _all_dependencies_are_recorded;
  3523. }
  3524. inline static void set_all_dependencies_are_recorded(bool on) {
  3525. _all_dependencies_are_recorded = (on != 0);
  3526. }
  3527. static void enter_onload_phase() NOT_JVMTI_RETURN;
  3528. static void enter_primordial_phase() NOT_JVMTI_RETURN;
  3529. static void enter_start_phase() NOT_JVMTI_RETURN;
  3530. static void enter_live_phase() NOT_JVMTI_RETURN;
  3531. inline static bool can_modify_any_class() {
  3532. JVMTI_ONLY(return _can_modify_any_class);
  3533. NOT_JVMTI(return false);
  3534. }
  3535. inline static bool can_access_local_variables() {
  3536. JVMTI_ONLY(return _can_access_local_variables);
  3537. NOT_JVMTI(return false);
  3538. }
  3539. inline static bool can_hotswap_or_post_breakpoint() {
  3540. JVMTI_ONLY(return _can_hotswap_or_post_breakpoint);
  3541. NOT_JVMTI(return false);
  3542. }
  3543. inline static bool can_walk_any_space() {
  3544. JVMTI_ONLY(return _can_walk_any_space);
  3545. NOT_JVMTI(return false);
  3546. }
  3547. static address get_field_access_count_addr() NOT_JVMTI_RETURN_(0);
  3548. static address get_field_modification_count_addr() NOT_JVMTI_RETURN_(0);
  3549. static bool is_jvmti_version(jint version) {
  3550. JVMTI_ONLY(return (version & JVMTI_VERSION_MASK) == JVMTI_VERSION_VALUE);
  3551. NOT_JVMTI(return false);
  3552. }
  3553. static bool is_jvmdi_version(jint version) {
  3554. JVMTI_ONLY(return (version & JVMTI_VERSION_MASK) == JVMDI_VERSION_VALUE);
  3555. NOT_JVMTI(return false);
  3556. }
  3557. static jint get_jvmti_interface(JavaVM *jvm, void **penv, jint version) NOT_JVMTI_RETURN_(0);
  3558. static void decode_version_values(jint version, int * major, int * minor,
  3559. int * micro) NOT_JVMTI_RETURN;
  3560. static void at_single_stepping_point(JavaThread *thread, Method* method, address location) NOT_JVMTI_RETURN;
  3561. static void expose_single_stepping(JavaThread *thread) NOT_JVMTI_RETURN;
  3562. static bool hide_single_stepping(JavaThread *thread) NOT_JVMTI_RETURN_(false);
  3563. static void post_vm_start () NOT_JVMTI_RETURN;
  3564. static void post_vm_initialized () NOT_JVMTI_RETURN;
  3565. static void post_vm_death () NOT_JVMTI_RETURN;
  3566. static void post_single_step (JavaThread *thread, Method* method, address location) NOT_JVMTI_RETURN;
  3567. static void post_raw_breakpoint (JavaThread *thread, Method* method, address location) NOT_JVMTI_RETURN;
  3568. static void post_exception_throw (JavaThread *thread, Method* method, address location, oop exception) NOT_JVMTI_RETURN;
  3569. static void notice_unwind_due_to_exception (JavaThread *thread, Method* method, address location, oop exception, bool in_handler_frame) NOT_JVMTI_RETURN;
  3570. static oop jni_GetField_probe (JavaThread *thread, jobject jobj,
  3571. oop obj, Klass* klass, jfieldID fieldID, bool is_static)
  3572. NOT_JVMTI_RETURN_(NULL);
  3573. static oop jni_GetField_probe_nh (JavaThread *thread, jobject jobj,
  3574. oop obj, Klass* klass, jfieldID fieldID, bool is_static)
  3575. NOT_JVMTI_RETURN_(NULL);
  3576. static void post_field_access_by_jni (JavaThread *thread, oop obj,
  3577. Klass* klass, jfieldID fieldID, bool is_static) NOT_JVMTI_RETURN;
  3578. static void post_field_access (JavaThread *thread, Method* method,
  3579. address location, KlassHandle field_klass, Handle object, jfieldID field) NOT_JVMTI_RETURN;
  3580. static oop jni_SetField_probe (JavaThread *thread, jobject jobj,
  3581. oop obj, Klass* klass, jfieldID fieldID, bool is_static, char sig_type,
  3582. jvalue *value) NOT_JVMTI_RETURN_(NULL);
  3583. static oop jni_SetField_probe_nh (JavaThread *thread, jobject jobj,
  3584. oop obj, Klass* klass, jfieldID fieldID, bool is_static, char sig_type,
  3585. jvalue *value) NOT_JVMTI_RETURN_(NULL);
  3586. static void post_field_modification_by_jni(JavaThread *thread, oop obj,
  3587. Klass* klass, jfieldID fieldID, bool is_static, char sig_type,
  3588. jvalue *value);
  3589. static void post_raw_field_modification(JavaThread *thread, Method* method,
  3590. address location, KlassHandle field_klass, Handle object, jfieldID field,
  3591. char sig_type, jvalue *value) NOT_JVMTI_RETURN;
  3592. static void post_method_entry (JavaThread *thread, Method* method, frame current_frame) NOT_JVMTI_RETURN;
  3593. static void post_method_exit (JavaThread *thread, Method* method, frame current_frame) NOT_JVMTI_RETURN;
  3594. static void post_class_load (JavaThread *thread, Klass* klass) NOT_JVMTI_RETURN;
  3595. static void post_class_unload (Klass* klass) NOT_JVMTI_RETURN;
  3596. static void post_class_prepare (JavaThread *thread, Klass* klass) NOT_JVMTI_RETURN;
  3597. static void post_thread_start (JavaThread *thread) NOT_JVMTI_RETURN;
  3598. static void post_thread_end (JavaThread *thread) NOT_JVMTI_RETURN;
  3599. static bool _should_post_class_file_load_hook;
  3600. inline static void set_should_post_class_file_load_hook(bool on) { _should_post_class_file_load_hook = on; }
  3601. inline static bool should_post_class_file_load_hook() {
  3602. JVMTI_ONLY(return _should_post_class_file_load_hook);
  3603. NOT_JVMTI(return false;)
  3604. }
  3605. static void post_class_file_load_hook(Symbol* h_name, Handle class_loader,
  3606. Handle h_protection_domain,
  3607. unsigned char **data_ptr, unsigned char **end_ptr,
  3608. JvmtiCachedClassFileData **cache_ptr) NOT_JVMTI_RETURN;
  3609. static void post_native_method_bind(Method* method, address* function_ptr) NOT_JVMTI_RETURN;
  3610. static void post_compiled_method_load(nmethod *nm) NOT_JVMTI_RETURN;
  3611. static void post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) NOT_JVMTI_RETURN;
  3612. static void post_compiled_method_unload(jmethodID mid, const void *code_begin) NOT_JVMTI_RETURN;
  3613. static void post_dynamic_code_generated_while_holding_locks(const char* name, address code_begin, address code_end) NOT_JVMTI_RETURN;
  3614. static void post_garbage_collection_finish() NOT_JVMTI_RETURN;
  3615. static void post_garbage_collection_start() NOT_JVMTI_RETURN;
  3616. static void post_data_dump() NOT_JVMTI_RETURN;
  3617. static void post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) NOT_JVMTI_RETURN;
  3618. static void post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) NOT_JVMTI_RETURN;
  3619. static void post_monitor_wait(JavaThread *thread, oop obj, jlong timeout) NOT_JVMTI_RETURN;
  3620. static void post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) NOT_JVMTI_RETURN;
  3621. static void post_object_free(JvmtiEnv* env, jlong tag) NOT_JVMTI_RETURN;
  3622. static void post_resource_exhausted(jint resource_exhausted_flags, const char* detail) NOT_JVMTI_RETURN;
  3623. static void record_vm_internal_object_allocation(oop object) NOT_JVMTI_RETURN;
  3624. static void post_vm_object_alloc(JavaThread *thread, oop object) NOT_JVMTI_RETURN;
  3625. inline static void vm_object_alloc_event_collector(oop object) {
  3626. if (should_post_vm_object_alloc()) {
  3627. record_vm_internal_object_allocation(object);
  3628. }
  3629. }
  3630. inline static void post_array_size_exhausted() {
  3631. if (should_post_resource_exhausted()) {
  3632. post_resource_exhausted(JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
  3633. "Requested array size exceeds VM limit");
  3634. }
  3635. }
  3636. static void cleanup_thread (JavaThread* thread) NOT_JVMTI_RETURN;
  3637. static void clear_detected_exception (JavaThread* thread) NOT_JVMTI_RETURN;
  3638. static void oops_do(OopClosure* f) NOT_JVMTI_RETURN;
  3639. static void weak_oops_do(BoolObjectClosure* b, OopClosure* f) NOT_JVMTI_RETURN;
  3640. static void gc_epilogue() NOT_JVMTI_RETURN;
  3641. static void transition_pending_onload_raw_monitors() NOT_JVMTI_RETURN;
  3642. static jint load_agent_library(AttachOperation* op, outputStream* out) NOT_JVMTI_RETURN_(JNI_ERR);
  3643. static char** get_all_native_method_prefixes(int* count_ptr) NOT_JVMTI_RETURN_(NULL);
  3644. };
  3645. class JvmtiCodeBlobDesc : public CHeapObj<mtInternal> {
  3646. private:
  3647. char _name[64];
  3648. address _code_begin;
  3649. address _code_end;
  3650. public:
  3651. JvmtiCodeBlobDesc(const char *name, address code_begin, address code_end) {
  3652. assert(name != NULL, "all code blobs must be named");
  3653. strncpy(_name, name, sizeof(_name) - 1);
  3654. _name[sizeof(_name)-1] = '\0';
  3655. _code_begin = code_begin;
  3656. _code_end = code_end;
  3657. }
  3658. char* name() { return _name; }
  3659. address code_begin() { return _code_begin; }
  3660. address code_end() { return _code_end; }
  3661. };
  3662. class JvmtiEventCollector : public StackObj {
  3663. private:
  3664. JvmtiEventCollector* _prev; // Save previous one to support nested event collector.
  3665. public:
  3666. void setup_jvmti_thread_state(); // Set this collector in current thread.
  3667. void unset_jvmti_thread_state(); // Reset previous collector in current thread.
  3668. virtual bool is_dynamic_code_event() { return false; }
  3669. virtual bool is_vm_object_alloc_event(){ return false; }
  3670. JvmtiEventCollector *get_prev() { return _prev; }
  3671. };
  3672. class JvmtiDynamicCodeEventCollector : public JvmtiEventCollector {
  3673. private:
  3674. GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected code blob events
  3675. friend class JvmtiExport;
  3676. void register_stub(const char* name, address start, address end);
  3677. public:
  3678. JvmtiDynamicCodeEventCollector() NOT_JVMTI_RETURN;
  3679. ~JvmtiDynamicCodeEventCollector() NOT_JVMTI_RETURN;
  3680. bool is_dynamic_code_event() { return true; }
  3681. };
  3682. class JvmtiVMObjectAllocEventCollector : public JvmtiEventCollector {
  3683. private:
  3684. GrowableArray<oop>* _allocated; // field to record vm internally allocated object oop.
  3685. bool _enable; // This flag is enabled in constructor and disabled
  3686. void oops_do(OopClosure* f);
  3687. friend class JvmtiExport;
  3688. inline void record_allocation(oop obj);
  3689. static void oops_do_for_all_threads(OopClosure* f);
  3690. public:
  3691. JvmtiVMObjectAllocEventCollector() NOT_JVMTI_RETURN;
  3692. ~JvmtiVMObjectAllocEventCollector() NOT_JVMTI_RETURN;
  3693. bool is_vm_object_alloc_event() { return true; }
  3694. bool is_enabled() { return _enable; }
  3695. void set_enabled(bool on) { _enable = on; }
  3696. };
  3697. class NoJvmtiVMObjectAllocMark : public StackObj {
  3698. private:
  3699. JvmtiVMObjectAllocEventCollector *_collector;
  3700. bool was_enabled() { return _collector != NULL; }
  3701. public:
  3702. NoJvmtiVMObjectAllocMark() NOT_JVMTI_RETURN;
  3703. ~NoJvmtiVMObjectAllocMark() NOT_JVMTI_RETURN;
  3704. };
  3705. class JvmtiGCMarker : public StackObj {
  3706. public:
  3707. JvmtiGCMarker() NOT_JVMTI_RETURN;
  3708. ~JvmtiGCMarker() NOT_JVMTI_RETURN;
  3709. };
  3710. class JvmtiHideSingleStepping : public StackObj {
  3711. private:
  3712. bool _single_step_hidden;
  3713. JavaThread * _thread;
  3714. public:
  3715. JvmtiHideSingleStepping(JavaThread * thread) {
  3716. assert(thread != NULL, "sanity check");
  3717. _single_step_hidden = false;
  3718. _thread = thread;
  3719. if (JvmtiExport::should_post_single_step()) {
  3720. _single_step_hidden = JvmtiExport::hide_single_stepping(_thread);
  3721. }
  3722. }
  3723. ~JvmtiHideSingleStepping() {
  3724. if (_single_step_hidden) {
  3725. JvmtiExport::expose_single_stepping(_thread);
  3726. }
  3727. }
  3728. };
  3729. #endif // SHARE_VM_PRIMS_JVMTIEXPORT_HPP
  3730. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiExtensions.cpp
  3731. #include "precompiled.hpp"
  3732. #include "prims/jvmtiExport.hpp"
  3733. #include "prims/jvmtiExtensions.hpp"
  3734. GrowableArray<jvmtiExtensionFunctionInfo*>* JvmtiExtensions::_ext_functions;
  3735. GrowableArray<jvmtiExtensionEventInfo*>* JvmtiExtensions::_ext_events;
  3736. static jvmtiError JNICALL IsClassUnloadingEnabled(const jvmtiEnv* env, jboolean* enabled, ...) {
  3737. if (enabled == NULL) {
  3738. return JVMTI_ERROR_NULL_POINTER;
  3739. }
  3740. return JVMTI_ERROR_NONE;
  3741. }
  3742. void JvmtiExtensions::register_extensions() {
  3743. _ext_functions = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiExtensionFunctionInfo*>(1,true);
  3744. _ext_events = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiExtensionEventInfo*>(1,true);
  3745. static jvmtiParamInfo func_params[] = {
  3746. { (char*)"IsClassUnloadingEnabled", JVMTI_KIND_OUT, JVMTI_TYPE_JBOOLEAN, JNI_FALSE }
  3747. };
  3748. static jvmtiExtensionFunctionInfo ext_func = {
  3749. (jvmtiExtensionFunction)IsClassUnloadingEnabled,
  3750. (char*)"com.sun.hotspot.functions.IsClassUnloadingEnabled",
  3751. (char*)"Tell if class unloading is enabled (-noclassgc)",
  3752. sizeof(func_params)/sizeof(func_params[0]),
  3753. func_params,
  3754. 0, // no non-universal errors
  3755. NULL
  3756. };
  3757. _ext_functions->append(&ext_func);
  3758. static jvmtiParamInfo event_params[] = {
  3759. { (char*)"JNI Environment", JVMTI_KIND_IN, JVMTI_TYPE_JNIENV, JNI_FALSE },
  3760. { (char*)"Thread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE },
  3761. { (char*)"Class", JVMTI_KIND_IN, JVMTI_TYPE_JCLASS, JNI_FALSE }
  3762. };
  3763. static jvmtiExtensionEventInfo ext_event = {
  3764. EXT_EVENT_CLASS_UNLOAD,
  3765. (char*)"com.sun.hotspot.events.ClassUnload",
  3766. (char*)"CLASS_UNLOAD event",
  3767. sizeof(event_params)/sizeof(event_params[0]),
  3768. event_params
  3769. };
  3770. _ext_events->append(&ext_event);
  3771. }
  3772. jvmtiError JvmtiExtensions::get_functions(JvmtiEnv* env,
  3773. jint* extension_count_ptr,
  3774. jvmtiExtensionFunctionInfo** extensions)
  3775. {
  3776. guarantee(_ext_functions != NULL, "registration not done");
  3777. ResourceTracker rt(env);
  3778. jvmtiExtensionFunctionInfo* ext_funcs;
  3779. jvmtiError err = rt.allocate(_ext_functions->length() *
  3780. sizeof(jvmtiExtensionFunctionInfo),
  3781. (unsigned char**)&ext_funcs);
  3782. if (err != JVMTI_ERROR_NONE) {
  3783. return err;
  3784. }
  3785. for (int i=0; i<_ext_functions->length(); i++ ) {
  3786. ext_funcs[i].func = _ext_functions->at(i)->func;
  3787. char *id = _ext_functions->at(i)->id;
  3788. err = rt.allocate(strlen(id)+1, (unsigned char**)&(ext_funcs[i].id));
  3789. if (err != JVMTI_ERROR_NONE) {
  3790. return err;
  3791. }
  3792. strcpy(ext_funcs[i].id, id);
  3793. char *desc = _ext_functions->at(i)->short_description;
  3794. err = rt.allocate(strlen(desc)+1,
  3795. (unsigned char**)&(ext_funcs[i].short_description));
  3796. if (err != JVMTI_ERROR_NONE) {
  3797. return err;
  3798. }
  3799. strcpy(ext_funcs[i].short_description, desc);
  3800. jint param_count = _ext_functions->at(i)->param_count;
  3801. ext_funcs[i].param_count = param_count;
  3802. if (param_count == 0) {
  3803. ext_funcs[i].params = NULL;
  3804. } else {
  3805. err = rt.allocate(param_count*sizeof(jvmtiParamInfo),
  3806. (unsigned char**)&(ext_funcs[i].params));
  3807. if (err != JVMTI_ERROR_NONE) {
  3808. return err;
  3809. }
  3810. jvmtiParamInfo* src_params = _ext_functions->at(i)->params;
  3811. jvmtiParamInfo* dst_params = ext_funcs[i].params;
  3812. for (int j=0; j<param_count; j++) {
  3813. err = rt.allocate(strlen(src_params[j].name)+1,
  3814. (unsigned char**)&(dst_params[j].name));
  3815. if (err != JVMTI_ERROR_NONE) {
  3816. return err;
  3817. }
  3818. strcpy(dst_params[j].name, src_params[j].name);
  3819. dst_params[j].kind = src_params[j].kind;
  3820. dst_params[j].base_type = src_params[j].base_type;
  3821. dst_params[j].null_ok = src_params[j].null_ok;
  3822. }
  3823. }
  3824. jint error_count = _ext_functions->at(i)->error_count;
  3825. ext_funcs[i].error_count = error_count;
  3826. if (error_count == 0) {
  3827. ext_funcs[i].errors = NULL;
  3828. } else {
  3829. err = rt.allocate(error_count*sizeof(jvmtiError),
  3830. (unsigned char**)&(ext_funcs[i].errors));
  3831. if (err != JVMTI_ERROR_NONE) {
  3832. return err;
  3833. }
  3834. memcpy(ext_funcs[i].errors, _ext_functions->at(i)->errors,
  3835. error_count*sizeof(jvmtiError));
  3836. }
  3837. }
  3838. return JVMTI_ERROR_NONE;
  3839. }
  3840. jvmtiError JvmtiExtensions::get_events(JvmtiEnv* env,
  3841. jint* extension_count_ptr,
  3842. jvmtiExtensionEventInfo** extensions)
  3843. {
  3844. guarantee(_ext_events != NULL, "registration not done");
  3845. ResourceTracker rt(env);
  3846. jvmtiExtensionEventInfo* ext_events;
  3847. jvmtiError err = rt.allocate(_ext_events->length() * sizeof(jvmtiExtensionEventInfo),
  3848. (unsigned char**)&ext_events);
  3849. if (err != JVMTI_ERROR_NONE) {
  3850. return err;
  3851. }
  3852. for (int i=0; i<_ext_events->length(); i++ ) {
  3853. ext_events[i].extension_event_index = _ext_events->at(i)->extension_event_index;
  3854. char *id = _ext_events->at(i)->id;
  3855. err = rt.allocate(strlen(id)+1, (unsigned char**)&(ext_events[i].id));
  3856. if (err != JVMTI_ERROR_NONE) {
  3857. return err;
  3858. }
  3859. strcpy(ext_events[i].id, id);
  3860. char *desc = _ext_events->at(i)->short_description;
  3861. err = rt.allocate(strlen(desc)+1,
  3862. (unsigned char**)&(ext_events[i].short_description));
  3863. if (err != JVMTI_ERROR_NONE) {
  3864. return err;
  3865. }
  3866. strcpy(ext_events[i].short_description, desc);
  3867. jint param_count = _ext_events->at(i)->param_count;
  3868. ext_events[i].param_count = param_count;
  3869. if (param_count == 0) {
  3870. ext_events[i].params = NULL;
  3871. } else {
  3872. err = rt.allocate(param_count*sizeof(jvmtiParamInfo),
  3873. (unsigned char**)&(ext_events[i].params));
  3874. if (err != JVMTI_ERROR_NONE) {
  3875. return err;
  3876. }
  3877. jvmtiParamInfo* src_params = _ext_events->at(i)->params;
  3878. jvmtiParamInfo* dst_params = ext_events[i].params;
  3879. for (int j=0; j<param_count; j++) {
  3880. err = rt.allocate(strlen(src_params[j].name)+1,
  3881. (unsigned char**)&(dst_params[j].name));
  3882. if (err != JVMTI_ERROR_NONE) {
  3883. return err;
  3884. }
  3885. strcpy(dst_params[j].name, src_params[j].name);
  3886. dst_params[j].kind = src_params[j].kind;
  3887. dst_params[j].base_type = src_params[j].base_type;
  3888. dst_params[j].null_ok = src_params[j].null_ok;
  3889. }
  3890. }
  3891. }
  3892. return JVMTI_ERROR_NONE;
  3893. }
  3894. jvmtiError JvmtiExtensions::set_event_callback(JvmtiEnv* env,
  3895. jint extension_event_index,
  3896. jvmtiExtensionEvent callback)
  3897. {
  3898. guarantee(_ext_events != NULL, "registration not done");
  3899. jvmtiExtensionEventInfo* event = NULL;
  3900. if (_ext_events != NULL) {
  3901. for (int i=0; i<_ext_events->length(); i++ ) {
  3902. if (_ext_events->at(i)->extension_event_index == extension_event_index) {
  3903. event = _ext_events->at(i);
  3904. break;
  3905. }
  3906. }
  3907. }
  3908. if (event == NULL) {
  3909. return JVMTI_ERROR_ILLEGAL_ARGUMENT;
  3910. }
  3911. JvmtiEventController::set_extension_event_callback(env, extension_event_index,
  3912. callback);
  3913. return JVMTI_ERROR_NONE;
  3914. }
  3915. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiExtensions.hpp
  3916. #ifndef SHARE_VM_PRIMS_JVMTIEXTENSIONS_HPP
  3917. #define SHARE_VM_PRIMS_JVMTIEXTENSIONS_HPP
  3918. #include "jvmtifiles/jvmti.h"
  3919. #include "jvmtifiles/jvmtiEnv.hpp"
  3920. #include "memory/allocation.hpp"
  3921. class JvmtiExtensions : public AllStatic {
  3922. private:
  3923. static GrowableArray<jvmtiExtensionFunctionInfo*>* _ext_functions;
  3924. static GrowableArray<jvmtiExtensionEventInfo*>* _ext_events;
  3925. public:
  3926. static void register_extensions();
  3927. static jvmtiError get_functions(JvmtiEnv* env, jint* extension_count_ptr,
  3928. jvmtiExtensionFunctionInfo** extensions);
  3929. static jvmtiError get_events(JvmtiEnv* env, jint* extension_count_ptr,
  3930. jvmtiExtensionEventInfo** extensions);
  3931. static jvmtiError set_event_callback(JvmtiEnv* env, jint extension_event_index,
  3932. jvmtiExtensionEvent callback);
  3933. };
  3934. #endif // SHARE_VM_PRIMS_JVMTIEXTENSIONS_HPP
  3935. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiGen.java
  3936. import javax.xml.parsers.DocumentBuilder;
  3937. import javax.xml.parsers.DocumentBuilderFactory;
  3938. import javax.xml.parsers.FactoryConfigurationError;
  3939. import javax.xml.parsers.ParserConfigurationException;
  3940. import org.xml.sax.SAXException;
  3941. import org.xml.sax.SAXParseException;
  3942. import org.w3c.dom.Document;
  3943. import org.w3c.dom.DOMException;
  3944. import javax.xml.transform.Transformer;
  3945. import javax.xml.transform.TransformerException;
  3946. import javax.xml.transform.TransformerFactory;
  3947. import javax.xml.transform.TransformerConfigurationException;
  3948. import javax.xml.transform.dom.DOMSource;
  3949. import javax.xml.transform.stream.StreamSource;
  3950. import javax.xml.transform.stream.StreamResult;
  3951. import java.io.*;
  3952. public class jvmtiGen
  3953. {
  3954. private static void showUsage() {
  3955. System.err.println("usage:");
  3956. System.err.println(" java jvmtiGen " +
  3957. "-IN <input XML file name> " +
  3958. "-XSL <XSL file> " +
  3959. "-OUT <output file name> " +
  3960. "[-PARAM <name> <expression> ...]");
  3961. System.exit(0); // There is no returning from showUsage()
  3962. }
  3963. static Document document;
  3964. public static void main (String argv [])
  3965. {
  3966. String inFileName=null;
  3967. String xslFileName=null;
  3968. String outFileName=null;
  3969. java.util.Vector<String> params=new java.util.Vector<String>();
  3970. for (int ii = 0; ii < argv.length; ii++) {
  3971. if (argv[ii].equals("-IN")) {
  3972. inFileName = argv[++ii];
  3973. } else if (argv[ii].equals("-XSL")) {
  3974. xslFileName = argv[++ii];
  3975. } else if (argv[ii].equals("-OUT")) {
  3976. outFileName = argv[++ii];
  3977. } else if (argv[ii].equals("-PARAM")) {
  3978. if (ii + 2 < argv.length) {
  3979. String name = argv[++ii];
  3980. params.addElement(name);
  3981. String expression = argv[++ii];
  3982. params.addElement(expression);
  3983. } else {
  3984. showUsage();
  3985. }
  3986. } else {
  3987. showUsage();
  3988. }
  3989. }
  3990. if (inFileName==null || xslFileName==null || outFileName==null){
  3991. showUsage();
  3992. }
  3993. final String parserProperty =
  3994. "javax.xml.transform.TransformerFactory";
  3995. final String workaroundParser =
  3996. "org.apache.xalan.processor.TransformerFactoryImpl";
  3997. try {
  3998. java.lang.Class cls = java.lang.Class.forName(workaroundParser);
  3999. System.setProperty(parserProperty, workaroundParser);
  4000. System.out.println("Info: jvmtiGen using " + parserProperty +
  4001. " = " + workaroundParser);
  4002. } catch (ClassNotFoundException cnfex) {
  4003. }
  4004. DocumentBuilderFactory factory =
  4005. DocumentBuilderFactory.newInstance();
  4006. factory.setNamespaceAware(true);
  4007. factory.setValidating(true);
  4008. factory.setXIncludeAware(true);
  4009. try {
  4010. File datafile = new File(inFileName);
  4011. File stylesheet = new File(xslFileName);
  4012. DocumentBuilder builder = factory.newDocumentBuilder();
  4013. document = builder.parse(datafile);
  4014. TransformerFactory tFactory =
  4015. TransformerFactory.newInstance();
  4016. StreamSource stylesource = new StreamSource(stylesheet);
  4017. Transformer transformer = tFactory.newTransformer(stylesource);
  4018. for (int ii = 0; ii < params.size(); ii += 2){
  4019. transformer.setParameter((String) params.elementAt(ii),
  4020. (String) params.elementAt(ii + 1));
  4021. }
  4022. DOMSource source = new DOMSource(document);
  4023. PrintStream ps = new PrintStream( new FileOutputStream(outFileName));
  4024. StreamResult result = new StreamResult(ps);
  4025. transformer.transform(source, result);
  4026. } catch (TransformerConfigurationException tce) {
  4027. System.out.println ("\n** Transformer Factory error");
  4028. System.out.println(" " + tce.getMessage() );
  4029. Throwable x = tce;
  4030. if (tce.getException() != null)
  4031. x = tce.getException();
  4032. x.printStackTrace();
  4033. } catch (TransformerException te) {
  4034. System.out.println ("\n** Transformation error");
  4035. System.out.println(" " + te.getMessage() );
  4036. Throwable x = te;
  4037. if (te.getException() != null)
  4038. x = te.getException();
  4039. x.printStackTrace();
  4040. } catch (SAXException sxe) {
  4041. Exception x = sxe;
  4042. if (sxe.getException() != null)
  4043. x = sxe.getException();
  4044. x.printStackTrace();
  4045. } catch (ParserConfigurationException pce) {
  4046. pce.printStackTrace();
  4047. } catch (IOException ioe) {
  4048. ioe.printStackTrace();
  4049. }
  4050. } // main
  4051. }
  4052. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiGetLoadedClasses.cpp
  4053. #include "precompiled.hpp"
  4054. #include "classfile/systemDictionary.hpp"
  4055. #include "memory/universe.inline.hpp"
  4056. #include "prims/jvmtiGetLoadedClasses.hpp"
  4057. #include "runtime/thread.hpp"
  4058. #if INCLUDE_ALL_GCS
  4059. #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
  4060. #endif
  4061. class LoadedClassesClosure : public KlassClosure {
  4062. private:
  4063. Stack<jclass, mtInternal> _classStack;
  4064. JvmtiEnv* _env;
  4065. static void ensure_klass_alive(oop o) {
  4066. #if INCLUDE_ALL_GCS
  4067. if (UseG1GC && o != NULL) {
  4068. G1SATBCardTableModRefBS::enqueue(o);
  4069. }
  4070. #endif
  4071. }
  4072. public:
  4073. LoadedClassesClosure(JvmtiEnv* env) {
  4074. _env = env;
  4075. }
  4076. void do_klass(Klass* k) {
  4077. _classStack.push((jclass) _env->jni_reference(k->java_mirror()));
  4078. ensure_klass_alive(k->java_mirror());
  4079. }
  4080. int extract(jclass* result_list) {
  4081. int count = (int)_classStack.size();
  4082. int i = count;
  4083. while (!_classStack.is_empty()) {
  4084. result_list[--i] = _classStack.pop();
  4085. }
  4086. return count;
  4087. }
  4088. int get_count() {
  4089. return (int)_classStack.size();
  4090. }
  4091. };
  4092. class JvmtiGetLoadedClassesClosure : public StackObj {
  4093. private:
  4094. jobject _initiatingLoader;
  4095. int _count;
  4096. Handle* _list;
  4097. int _index;
  4098. private:
  4099. static JvmtiGetLoadedClassesClosure* get_this() {
  4100. JvmtiGetLoadedClassesClosure* result = NULL;
  4101. JavaThread* thread = JavaThread::current();
  4102. result = thread->get_jvmti_get_loaded_classes_closure();
  4103. return result;
  4104. }
  4105. static void set_this(JvmtiGetLoadedClassesClosure* that) {
  4106. JavaThread* thread = JavaThread::current();
  4107. thread->set_jvmti_get_loaded_classes_closure(that);
  4108. }
  4109. public:
  4110. JvmtiGetLoadedClassesClosure() {
  4111. JvmtiGetLoadedClassesClosure* that = get_this();
  4112. assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
  4113. _initiatingLoader = NULL;
  4114. _count = 0;
  4115. _list = NULL;
  4116. _index = 0;
  4117. set_this(this);
  4118. }
  4119. JvmtiGetLoadedClassesClosure(jobject initiatingLoader) {
  4120. JvmtiGetLoadedClassesClosure* that = get_this();
  4121. assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
  4122. _initiatingLoader = initiatingLoader;
  4123. _count = 0;
  4124. _list = NULL;
  4125. _index = 0;
  4126. set_this(this);
  4127. }
  4128. ~JvmtiGetLoadedClassesClosure() {
  4129. JvmtiGetLoadedClassesClosure* that = get_this();
  4130. assert(that != NULL, "JvmtiGetLoadedClassesClosure not found");
  4131. set_this(NULL);
  4132. _initiatingLoader = NULL;
  4133. _count = 0;
  4134. if (_list != NULL) {
  4135. FreeHeap(_list);
  4136. _list = NULL;
  4137. }
  4138. _index = 0;
  4139. }
  4140. jobject get_initiatingLoader() {
  4141. return _initiatingLoader;
  4142. }
  4143. int get_count() {
  4144. return _count;
  4145. }
  4146. void set_count(int value) {
  4147. _count = value;
  4148. }
  4149. Handle* get_list() {
  4150. return _list;
  4151. }
  4152. void set_list(Handle* value) {
  4153. _list = value;
  4154. }
  4155. int get_index() {
  4156. return _index;
  4157. }
  4158. void set_index(int value) {
  4159. _index = value;
  4160. }
  4161. Handle get_element(int index) {
  4162. if ((_list != NULL) && (index < _count)) {
  4163. return _list[index];
  4164. } else {
  4165. assert(false, "empty get_element");
  4166. return Handle();
  4167. }
  4168. }
  4169. void set_element(int index, Handle value) {
  4170. if ((_list != NULL) && (index < _count)) {
  4171. _list[index] = value;
  4172. } else {
  4173. assert(false, "bad set_element");
  4174. }
  4175. }
  4176. bool available() {
  4177. return (_list != NULL);
  4178. }
  4179. #ifdef ASSERT
  4180. void check(int limit) {
  4181. for (int i = 0; i < limit; i += 1) {
  4182. assert(Universe::heap()->is_in(get_element(i)()), "check fails");
  4183. }
  4184. }
  4185. #endif
  4186. void allocate() {
  4187. _list = NEW_C_HEAP_ARRAY(Handle, _count, mtInternal);
  4188. assert(_list != NULL, "Out of memory");
  4189. if (_list == NULL) {
  4190. _count = 0;
  4191. }
  4192. }
  4193. void extract(JvmtiEnv *env, jclass* result) {
  4194. for (int index = 0; index < _count; index += 1) {
  4195. result[index] = (jclass) env->jni_reference(get_element(index));
  4196. }
  4197. }
  4198. static void increment_with_loader(Klass* k, ClassLoaderData* loader_data) {
  4199. JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
  4200. oop class_loader = loader_data->class_loader();
  4201. if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
  4202. for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
  4203. that->set_count(that->get_count() + 1);
  4204. }
  4205. }
  4206. }
  4207. static void prim_array_increment_with_loader(Klass* array, ClassLoaderData* loader_data) {
  4208. JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
  4209. oop class_loader = loader_data->class_loader();
  4210. if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
  4211. that->set_count(that->get_count() + 1);
  4212. }
  4213. }
  4214. static void add_with_loader(Klass* k, ClassLoaderData* loader_data) {
  4215. JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
  4216. if (that->available()) {
  4217. oop class_loader = loader_data->class_loader();
  4218. if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
  4219. for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
  4220. oop mirror = l->java_mirror();
  4221. that->set_element(that->get_index(), mirror);
  4222. that->set_index(that->get_index() + 1);
  4223. }
  4224. }
  4225. }
  4226. }
  4227. static void increment_for_basic_type_arrays(Klass* k) {
  4228. JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
  4229. assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
  4230. for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
  4231. that->set_count(that->get_count() + 1);
  4232. }
  4233. }
  4234. static void add_for_basic_type_arrays(Klass* k) {
  4235. JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
  4236. assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
  4237. assert(that->available(), "no list");
  4238. for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
  4239. oop mirror = l->java_mirror();
  4240. that->set_element(that->get_index(), mirror);
  4241. that->set_index(that->get_index() + 1);
  4242. }
  4243. }
  4244. };
  4245. jvmtiError
  4246. JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
  4247. LoadedClassesClosure closure(env);
  4248. {
  4249. MutexLocker ma(MultiArray_lock);
  4250. ClassLoaderDataGraph::loaded_classes_do(&closure);
  4251. }
  4252. jclass* result_list;
  4253. jvmtiError error = env->Allocate(closure.get_count() * sizeof(jclass),
  4254. (unsigned char**)&result_list);
  4255. if (error == JVMTI_ERROR_NONE) {
  4256. int count = closure.extract(result_list);
  4257. }
  4258. return error;
  4259. }
  4260. jvmtiError
  4261. JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,
  4262. jint* classCountPtr, jclass** classesPtr) {
  4263. JvmtiGetLoadedClassesClosure closure(initiatingLoader);
  4264. {
  4265. MutexLocker ma(MultiArray_lock);
  4266. MutexLocker sd(SystemDictionary_lock);
  4267. SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment_with_loader);
  4268. Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment_for_basic_type_arrays);
  4269. closure.allocate();
  4270. SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add_with_loader);
  4271. Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add_for_basic_type_arrays);
  4272. }
  4273. jclass* result_list;
  4274. jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),
  4275. (unsigned char**)&result_list);
  4276. if (err != JVMTI_ERROR_NONE) {
  4277. return err;
  4278. }
  4279. closure.extract(env, result_list);
  4280. return JVMTI_ERROR_NONE;
  4281. }
  4282. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiGetLoadedClasses.hpp
  4283. #ifndef SHARE_VM_PRIMS_JVMTIGETLOADEDCLASSES_HPP
  4284. #define SHARE_VM_PRIMS_JVMTIGETLOADEDCLASSES_HPP
  4285. #include "jvmtifiles/jvmtiEnv.hpp"
  4286. class JvmtiGetLoadedClasses : AllStatic {
  4287. public:
  4288. static jvmtiError getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr);
  4289. static jvmtiError getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,
  4290. jint* classCountPtr, jclass** classesPtr);
  4291. };
  4292. #endif // SHARE_VM_PRIMS_JVMTIGETLOADEDCLASSES_HPP
  4293. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiImpl.cpp
  4294. #include "precompiled.hpp"
  4295. #include "classfile/systemDictionary.hpp"
  4296. #include "interpreter/interpreter.hpp"
  4297. #include "interpreter/oopMapCache.hpp"
  4298. #include "jvmtifiles/jvmtiEnv.hpp"
  4299. #include "memory/resourceArea.hpp"
  4300. #include "oops/instanceKlass.hpp"
  4301. #include "prims/jvmtiAgentThread.hpp"
  4302. #include "prims/jvmtiEventController.inline.hpp"
  4303. #include "prims/jvmtiImpl.hpp"
  4304. #include "prims/jvmtiRedefineClasses.hpp"
  4305. #include "runtime/atomic.hpp"
  4306. #include "runtime/deoptimization.hpp"
  4307. #include "runtime/handles.hpp"
  4308. #include "runtime/handles.inline.hpp"
  4309. #include "runtime/interfaceSupport.hpp"
  4310. #include "runtime/javaCalls.hpp"
  4311. #include "runtime/os.hpp"
  4312. #include "runtime/serviceThread.hpp"
  4313. #include "runtime/signature.hpp"
  4314. #include "runtime/thread.inline.hpp"
  4315. #include "runtime/vframe.hpp"
  4316. #include "runtime/vframe_hp.hpp"
  4317. #include "runtime/vm_operations.hpp"
  4318. #include "utilities/exceptions.hpp"
  4319. JvmtiAgentThread::JvmtiAgentThread(JvmtiEnv* env, jvmtiStartFunction start_fn, const void *start_arg)
  4320. : JavaThread(start_function_wrapper) {
  4321. _env = env;
  4322. _start_fn = start_fn;
  4323. _start_arg = start_arg;
  4324. }
  4325. void
  4326. JvmtiAgentThread::start_function_wrapper(JavaThread *thread, TRAPS) {
  4327. assert(thread->is_Java_thread(), "debugger thread should be a Java Thread");
  4328. assert(thread == JavaThread::current(), "sanity check");
  4329. JvmtiAgentThread *dthread = (JvmtiAgentThread *)thread;
  4330. dthread->call_start_function();
  4331. }
  4332. void
  4333. JvmtiAgentThread::call_start_function() {
  4334. ThreadToNativeFromVM transition(this);
  4335. _start_fn(_env->jvmti_external(), jni_environment(), (void*)_start_arg);
  4336. }
  4337. void GrowableCache::recache() {
  4338. int len = _elements->length();
  4339. FREE_C_HEAP_ARRAY(address, _cache, mtInternal);
  4340. _cache = NEW_C_HEAP_ARRAY(address,len+1, mtInternal);
  4341. for (int i=0; i<len; i++) {
  4342. _cache[i] = _elements->at(i)->getCacheValue();
  4343. if (_cache[i] == NULL) {
  4344. assert(false, "cannot recache NULL elements");
  4345. remove(i);
  4346. return;
  4347. }
  4348. }
  4349. _cache[len] = NULL;
  4350. _listener_fun(_this_obj,_cache);
  4351. }
  4352. bool GrowableCache::equals(void* v, GrowableElement *e2) {
  4353. GrowableElement *e1 = (GrowableElement *) v;
  4354. assert(e1 != NULL, "e1 != NULL");
  4355. assert(e2 != NULL, "e2 != NULL");
  4356. return e1->equals(e2);
  4357. }
  4358. GrowableCache::GrowableCache() {
  4359. _this_obj = NULL;
  4360. _listener_fun = NULL;
  4361. _elements = NULL;
  4362. _cache = NULL;
  4363. }
  4364. GrowableCache::~GrowableCache() {
  4365. clear();
  4366. delete _elements;
  4367. FREE_C_HEAP_ARRAY(address, _cache, mtInternal);
  4368. }
  4369. void GrowableCache::initialize(void *this_obj, void listener_fun(void *, address*) ) {
  4370. _this_obj = this_obj;
  4371. _listener_fun = listener_fun;
  4372. _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<GrowableElement*>(5,true);
  4373. recache();
  4374. }
  4375. int GrowableCache::length() {
  4376. return _elements->length();
  4377. }
  4378. GrowableElement* GrowableCache::at(int index) {
  4379. GrowableElement *e = (GrowableElement *) _elements->at(index);
  4380. assert(e != NULL, "e != NULL");
  4381. return e;
  4382. }
  4383. int GrowableCache::find(GrowableElement* e) {
  4384. return _elements->find(e, GrowableCache::equals);
  4385. }
  4386. void GrowableCache::append(GrowableElement* e) {
  4387. GrowableElement *new_e = e->clone();
  4388. _elements->append(new_e);
  4389. recache();
  4390. }
  4391. void GrowableCache::insert(GrowableElement* e) {
  4392. GrowableElement *new_e = e->clone();
  4393. _elements->append(new_e);
  4394. int n = length()-2;
  4395. for (int i=n; i>=0; i--) {
  4396. GrowableElement *e1 = _elements->at(i);
  4397. GrowableElement *e2 = _elements->at(i+1);
  4398. if (e2->lessThan(e1)) {
  4399. _elements->at_put(i+1, e1);
  4400. _elements->at_put(i, e2);
  4401. }
  4402. }
  4403. recache();
  4404. }
  4405. void GrowableCache::remove (int index) {
  4406. GrowableElement *e = _elements->at(index);
  4407. assert(e != NULL, "e != NULL");
  4408. _elements->remove(e);
  4409. delete e;
  4410. recache();
  4411. }
  4412. void GrowableCache::clear() {
  4413. int len = _elements->length();
  4414. for (int i=0; i<len; i++) {
  4415. delete _elements->at(i);
  4416. }
  4417. _elements->clear();
  4418. recache();
  4419. }
  4420. void GrowableCache::oops_do(OopClosure* f) {
  4421. int len = _elements->length();
  4422. for (int i=0; i<len; i++) {
  4423. GrowableElement *e = _elements->at(i);
  4424. e->oops_do(f);
  4425. }
  4426. }
  4427. void GrowableCache::metadata_do(void f(Metadata*)) {
  4428. int len = _elements->length();
  4429. for (int i=0; i<len; i++) {
  4430. GrowableElement *e = _elements->at(i);
  4431. e->metadata_do(f);
  4432. }
  4433. }
  4434. void GrowableCache::gc_epilogue() {
  4435. int len = _elements->length();
  4436. for (int i=0; i<len; i++) {
  4437. _cache[i] = _elements->at(i)->getCacheValue();
  4438. }
  4439. }
  4440. JvmtiBreakpoint::JvmtiBreakpoint() {
  4441. _method = NULL;
  4442. _bci = 0;
  4443. _class_holder = NULL;
  4444. }
  4445. JvmtiBreakpoint::JvmtiBreakpoint(Method* m_method, jlocation location) {
  4446. _method = m_method;
  4447. _class_holder = _method->method_holder()->klass_holder();
  4448. #ifdef CHECK_UNHANDLED_OOPS
  4449. Thread::current()->allow_unhandled_oop(&_class_holder);
  4450. #endif // CHECK_UNHANDLED_OOPS
  4451. assert(_method != NULL, "_method != NULL");
  4452. _bci = (int) location;
  4453. assert(_bci >= 0, "_bci >= 0");
  4454. }
  4455. void JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) {
  4456. _method = bp._method;
  4457. _bci = bp._bci;
  4458. _class_holder = bp._class_holder;
  4459. }
  4460. bool JvmtiBreakpoint::lessThan(JvmtiBreakpoint& bp) {
  4461. Unimplemented();
  4462. return false;
  4463. }
  4464. bool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) {
  4465. return _method == bp._method
  4466. && _bci == bp._bci;
  4467. }
  4468. bool JvmtiBreakpoint::is_valid() {
  4469. return _method != NULL &&
  4470. _bci >= 0;
  4471. }
  4472. address JvmtiBreakpoint::getBcp() {
  4473. return _method->bcp_from(_bci);
  4474. }
  4475. void JvmtiBreakpoint::each_method_version_do(method_action meth_act) {
  4476. ((Method*)_method->*meth_act)(_bci);
  4477. Thread *thread = Thread::current();
  4478. instanceKlassHandle ikh = instanceKlassHandle(thread, _method->method_holder());
  4479. Symbol* m_name = _method->name();
  4480. Symbol* m_signature = _method->signature();
  4481. for (InstanceKlass* pv_node = ikh->previous_versions();
  4482. pv_node != NULL;
  4483. pv_node = pv_node->previous_versions()) {
  4484. Array<Method*>* methods = pv_node->methods();
  4485. for (int i = methods->length() - 1; i >= 0; i--) {
  4486. Method* method = methods->at(i);
  4487. if (method->is_running_emcp() &&
  4488. method->name() == m_name &&
  4489. method->signature() == m_signature) {
  4490. RC_TRACE(0x00000800, ("%sing breakpoint in %s(%s)",
  4491. meth_act == &Method::set_breakpoint ? "sett" : "clear",
  4492. method->name()->as_C_string(),
  4493. method->signature()->as_C_string()));
  4494. (method->*meth_act)(_bci);
  4495. break;
  4496. }
  4497. }
  4498. }
  4499. }
  4500. void JvmtiBreakpoint::set() {
  4501. each_method_version_do(&Method::set_breakpoint);
  4502. }
  4503. void JvmtiBreakpoint::clear() {
  4504. each_method_version_do(&Method::clear_breakpoint);
  4505. }
  4506. void JvmtiBreakpoint::print() {
  4507. #ifndef PRODUCT
  4508. const char *class_name = (_method == NULL) ? "NULL" : _method->klass_name()->as_C_string();
  4509. const char *method_name = (_method == NULL) ? "NULL" : _method->name()->as_C_string();
  4510. tty->print("Breakpoint(%s,%s,%d,%p)",class_name, method_name, _bci, getBcp());
  4511. #endif
  4512. }
  4513. void VM_ChangeBreakpoints::doit() {
  4514. switch (_operation) {
  4515. case SET_BREAKPOINT:
  4516. _breakpoints->set_at_safepoint(*_bp);
  4517. break;
  4518. case CLEAR_BREAKPOINT:
  4519. _breakpoints->clear_at_safepoint(*_bp);
  4520. break;
  4521. default:
  4522. assert(false, "Unknown operation");
  4523. }
  4524. }
  4525. void VM_ChangeBreakpoints::oops_do(OopClosure* f) {
  4526. if (_bp != NULL) {
  4527. _bp->oops_do(f);
  4528. }
  4529. }
  4530. void VM_ChangeBreakpoints::metadata_do(void f(Metadata*)) {
  4531. if (_bp != NULL) {
  4532. _bp->metadata_do(f);
  4533. }
  4534. }
  4535. JvmtiBreakpoints::JvmtiBreakpoints(void listener_fun(void *,address *)) {
  4536. _bps.initialize(this,listener_fun);
  4537. }
  4538. JvmtiBreakpoints:: ~JvmtiBreakpoints() {}
  4539. void JvmtiBreakpoints::oops_do(OopClosure* f) {
  4540. _bps.oops_do(f);
  4541. }
  4542. void JvmtiBreakpoints::metadata_do(void f(Metadata*)) {
  4543. _bps.metadata_do(f);
  4544. }
  4545. void JvmtiBreakpoints::gc_epilogue() {
  4546. _bps.gc_epilogue();
  4547. }
  4548. void JvmtiBreakpoints::print() {
  4549. #ifndef PRODUCT
  4550. ResourceMark rm;
  4551. int n = _bps.length();
  4552. for (int i=0; i<n; i++) {
  4553. JvmtiBreakpoint& bp = _bps.at(i);
  4554. tty->print("%d: ", i);
  4555. bp.print();
  4556. tty->cr();
  4557. }
  4558. #endif
  4559. }
  4560. void JvmtiBreakpoints::set_at_safepoint(JvmtiBreakpoint& bp) {
  4561. assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
  4562. int i = _bps.find(bp);
  4563. if (i == -1) {
  4564. _bps.append(bp);
  4565. bp.set();
  4566. }
  4567. }
  4568. void JvmtiBreakpoints::clear_at_safepoint(JvmtiBreakpoint& bp) {
  4569. assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
  4570. int i = _bps.find(bp);
  4571. if (i != -1) {
  4572. _bps.remove(i);
  4573. bp.clear();
  4574. }
  4575. }
  4576. int JvmtiBreakpoints::length() { return _bps.length(); }
  4577. int JvmtiBreakpoints::set(JvmtiBreakpoint& bp) {
  4578. if ( _bps.find(bp) != -1) {
  4579. return JVMTI_ERROR_DUPLICATE;
  4580. }
  4581. VM_ChangeBreakpoints set_breakpoint(VM_ChangeBreakpoints::SET_BREAKPOINT, &bp);
  4582. VMThread::execute(&set_breakpoint);
  4583. return JVMTI_ERROR_NONE;
  4584. }
  4585. int JvmtiBreakpoints::clear(JvmtiBreakpoint& bp) {
  4586. if ( _bps.find(bp) == -1) {
  4587. return JVMTI_ERROR_NOT_FOUND;
  4588. }
  4589. VM_ChangeBreakpoints clear_breakpoint(VM_ChangeBreakpoints::CLEAR_BREAKPOINT, &bp);
  4590. VMThread::execute(&clear_breakpoint);
  4591. return JVMTI_ERROR_NONE;
  4592. }
  4593. void JvmtiBreakpoints::clearall_in_class_at_safepoint(Klass* klass) {
  4594. bool changed = true;
  4595. while (changed) {
  4596. int len = _bps.length();
  4597. changed = false;
  4598. for (int i = 0; i < len; i++) {
  4599. JvmtiBreakpoint& bp = _bps.at(i);
  4600. if (bp.method()->method_holder() == klass) {
  4601. bp.clear();
  4602. _bps.remove(i);
  4603. changed = true;
  4604. break;
  4605. }
  4606. }
  4607. }
  4608. }
  4609. JvmtiBreakpoints *JvmtiCurrentBreakpoints::_jvmti_breakpoints = NULL;
  4610. address * JvmtiCurrentBreakpoints::_breakpoint_list = NULL;
  4611. JvmtiBreakpoints& JvmtiCurrentBreakpoints::get_jvmti_breakpoints() {
  4612. if (_jvmti_breakpoints != NULL) return (*_jvmti_breakpoints);
  4613. _jvmti_breakpoints = new JvmtiBreakpoints(listener_fun);
  4614. assert(_jvmti_breakpoints != NULL, "_jvmti_breakpoints != NULL");
  4615. return (*_jvmti_breakpoints);
  4616. }
  4617. void JvmtiCurrentBreakpoints::listener_fun(void *this_obj, address *cache) {
  4618. JvmtiBreakpoints *this_jvmti = (JvmtiBreakpoints *) this_obj;
  4619. assert(this_jvmti != NULL, "this_jvmti != NULL");
  4620. debug_only(int n = this_jvmti->length(););
  4621. assert(cache[n] == NULL, "cache must be NULL terminated");
  4622. set_breakpoint_list(cache);
  4623. }
  4624. void JvmtiCurrentBreakpoints::oops_do(OopClosure* f) {
  4625. if (_jvmti_breakpoints != NULL) {
  4626. _jvmti_breakpoints->oops_do(f);
  4627. }
  4628. }
  4629. void JvmtiCurrentBreakpoints::metadata_do(void f(Metadata*)) {
  4630. if (_jvmti_breakpoints != NULL) {
  4631. _jvmti_breakpoints->metadata_do(f);
  4632. }
  4633. }
  4634. void JvmtiCurrentBreakpoints::gc_epilogue() {
  4635. if (_jvmti_breakpoints != NULL) {
  4636. _jvmti_breakpoints->gc_epilogue();
  4637. }
  4638. }
  4639. VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type)
  4640. : _thread(thread)
  4641. , _calling_thread(NULL)
  4642. , _depth(depth)
  4643. , _index(index)
  4644. , _type(type)
  4645. , _set(false)
  4646. , _jvf(NULL)
  4647. , _result(JVMTI_ERROR_NONE)
  4648. {
  4649. }
  4650. VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type, jvalue value)
  4651. : _thread(thread)
  4652. , _calling_thread(NULL)
  4653. , _depth(depth)
  4654. , _index(index)
  4655. , _type(type)
  4656. , _value(value)
  4657. , _set(true)
  4658. , _jvf(NULL)
  4659. , _result(JVMTI_ERROR_NONE)
  4660. {
  4661. }
  4662. VM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, int index)
  4663. : _thread(thread)
  4664. , _calling_thread(calling_thread)
  4665. , _depth(depth)
  4666. , _index(index)
  4667. , _type(T_OBJECT)
  4668. , _set(false)
  4669. , _jvf(NULL)
  4670. , _result(JVMTI_ERROR_NONE)
  4671. {
  4672. }
  4673. vframe *VM_GetOrSetLocal::get_vframe() {
  4674. if (!_thread->has_last_Java_frame()) {
  4675. return NULL;
  4676. }
  4677. RegisterMap reg_map(_thread);
  4678. vframe *vf = _thread->last_java_vframe(&reg_map);
  4679. int d = 0;
  4680. while ((vf != NULL) && (d < _depth)) {
  4681. vf = vf->java_sender();
  4682. d++;
  4683. }
  4684. return vf;
  4685. }
  4686. javaVFrame *VM_GetOrSetLocal::get_java_vframe() {
  4687. vframe* vf = get_vframe();
  4688. if (vf == NULL) {
  4689. _result = JVMTI_ERROR_NO_MORE_FRAMES;
  4690. return NULL;
  4691. }
  4692. javaVFrame *jvf = (javaVFrame*)vf;
  4693. if (!vf->is_java_frame()) {
  4694. _result = JVMTI_ERROR_OPAQUE_FRAME;
  4695. return NULL;
  4696. }
  4697. return jvf;
  4698. }
  4699. bool VM_GetOrSetLocal::is_assignable(const char* ty_sign, Klass* klass, Thread* thread) {
  4700. assert(ty_sign != NULL, "type signature must not be NULL");
  4701. assert(thread != NULL, "thread must not be NULL");
  4702. assert(klass != NULL, "klass must not be NULL");
  4703. int len = (int) strlen(ty_sign);
  4704. if (ty_sign[0] == 'L' && ty_sign[len-1] == ';') { // Need pure class/interface name
  4705. ty_sign++;
  4706. len -= 2;
  4707. }
  4708. TempNewSymbol ty_sym = SymbolTable::new_symbol(ty_sign, len, thread);
  4709. if (klass->name() == ty_sym) {
  4710. return true;
  4711. }
  4712. int super_depth = klass->super_depth();
  4713. int idx;
  4714. for (idx = 0; idx < super_depth; idx++) {
  4715. if (klass->primary_super_of_depth(idx)->name() == ty_sym) {
  4716. return true;
  4717. }
  4718. }
  4719. Array<Klass*>* sec_supers = klass->secondary_supers();
  4720. for (idx = 0; idx < sec_supers->length(); idx++) {
  4721. if (((Klass*) sec_supers->at(idx))->name() == ty_sym) {
  4722. return true;
  4723. }
  4724. }
  4725. return false;
  4726. }
  4727. bool VM_GetOrSetLocal::check_slot_type(javaVFrame* jvf) {
  4728. Method* method_oop = jvf->method();
  4729. if (!method_oop->has_localvariable_table()) {
  4730. jint extra_slot = (_type == T_LONG || _type == T_DOUBLE) ? 1 : 0;
  4731. if (_index < 0 || _index + extra_slot >= method_oop->max_locals()) {
  4732. _result = JVMTI_ERROR_INVALID_SLOT;
  4733. return false;
  4734. }
  4735. return true;
  4736. }
  4737. jint num_entries = method_oop->localvariable_table_length();
  4738. if (num_entries == 0) {
  4739. _result = JVMTI_ERROR_INVALID_SLOT;
  4740. return false; // There are no slots
  4741. }
  4742. int signature_idx = -1;
  4743. int vf_bci = jvf->bci();
  4744. LocalVariableTableElement* table = method_oop->localvariable_table_start();
  4745. for (int i = 0; i < num_entries; i++) {
  4746. int start_bci = table[i].start_bci;
  4747. int end_bci = start_bci + table[i].length;
  4748. if (_index == (jint) table[i].slot && start_bci <= vf_bci && vf_bci <= end_bci) {
  4749. signature_idx = (int) table[i].descriptor_cp_index;
  4750. break;
  4751. }
  4752. }
  4753. if (signature_idx == -1) {
  4754. _result = JVMTI_ERROR_INVALID_SLOT;
  4755. return false; // Incorrect slot index
  4756. }
  4757. Symbol* sign_sym = method_oop->constants()->symbol_at(signature_idx);
  4758. const char* signature = (const char *) sign_sym->as_utf8();
  4759. BasicType slot_type = char2type(signature[0]);
  4760. switch (slot_type) {
  4761. case T_BYTE:
  4762. case T_SHORT:
  4763. case T_CHAR:
  4764. case T_BOOLEAN:
  4765. slot_type = T_INT;
  4766. break;
  4767. case T_ARRAY:
  4768. slot_type = T_OBJECT;
  4769. break;
  4770. };
  4771. if (_type != slot_type) {
  4772. _result = JVMTI_ERROR_TYPE_MISMATCH;
  4773. return false;
  4774. }
  4775. jobject jobj = _value.l;
  4776. if (_set && slot_type == T_OBJECT && jobj != NULL) { // NULL reference is allowed
  4777. JavaThread* cur_thread = JavaThread::current();
  4778. HandleMark hm(cur_thread);
  4779. Handle obj = Handle(cur_thread, JNIHandles::resolve_external_guard(jobj));
  4780. NULL_CHECK(obj, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
  4781. KlassHandle ob_kh = KlassHandle(cur_thread, obj->klass());
  4782. NULL_CHECK(ob_kh, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
  4783. if (!is_assignable(signature, ob_kh(), cur_thread)) {
  4784. _result = JVMTI_ERROR_TYPE_MISMATCH;
  4785. return false;
  4786. }
  4787. }
  4788. return true;
  4789. }
  4790. static bool can_be_deoptimized(vframe* vf) {
  4791. return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());
  4792. }
  4793. bool VM_GetOrSetLocal::doit_prologue() {
  4794. _jvf = get_java_vframe();
  4795. NULL_CHECK(_jvf, false);
  4796. if (_jvf->method()->is_native()) {
  4797. if (getting_receiver() && !_jvf->method()->is_static()) {
  4798. return true;
  4799. } else {
  4800. _result = JVMTI_ERROR_OPAQUE_FRAME;
  4801. return false;
  4802. }
  4803. }
  4804. if (!check_slot_type(_jvf)) {
  4805. return false;
  4806. }
  4807. return true;
  4808. }
  4809. void VM_GetOrSetLocal::doit() {
  4810. InterpreterOopMap oop_mask;
  4811. _jvf->method()->mask_for(_jvf->bci(), &oop_mask);
  4812. if (oop_mask.is_dead(_index)) {
  4813. _result = JVMTI_ERROR_INVALID_SLOT;
  4814. return;
  4815. }
  4816. if (_set) {
  4817. if (can_be_deoptimized(_jvf)) {
  4818. Deoptimization::deoptimize_frame(_jvf->thread(), _jvf->fr().id());
  4819. if (_type == T_OBJECT) {
  4820. _value.l = (jobject) (JNIHandles::resolve_external_guard(_value.l));
  4821. }
  4822. _jvf = get_java_vframe();
  4823. ((compiledVFrame*)_jvf)->update_local(_type, _index, _value);
  4824. return;
  4825. }
  4826. StackValueCollection *locals = _jvf->locals();
  4827. HandleMark hm;
  4828. switch (_type) {
  4829. case T_INT: locals->set_int_at (_index, _value.i); break;
  4830. case T_LONG: locals->set_long_at (_index, _value.j); break;
  4831. case T_FLOAT: locals->set_float_at (_index, _value.f); break;
  4832. case T_DOUBLE: locals->set_double_at(_index, _value.d); break;
  4833. case T_OBJECT: {
  4834. Handle ob_h(JNIHandles::resolve_external_guard(_value.l));
  4835. locals->set_obj_at (_index, ob_h);
  4836. break;
  4837. }
  4838. default: ShouldNotReachHere();
  4839. }
  4840. _jvf->set_locals(locals);
  4841. } else {
  4842. if (_jvf->method()->is_native() && _jvf->is_compiled_frame()) {
  4843. assert(getting_receiver(), "Can only get here when getting receiver");
  4844. oop receiver = _jvf->fr().get_native_receiver();
  4845. _value.l = JNIHandles::make_local(_calling_thread, receiver);
  4846. } else {
  4847. StackValueCollection *locals = _jvf->locals();
  4848. if (locals->at(_index)->type() == T_CONFLICT) {
  4849. memset(&_value, 0, sizeof(_value));
  4850. _value.l = NULL;
  4851. return;
  4852. }
  4853. switch (_type) {
  4854. case T_INT: _value.i = locals->int_at (_index); break;
  4855. case T_LONG: _value.j = locals->long_at (_index); break;
  4856. case T_FLOAT: _value.f = locals->float_at (_index); break;
  4857. case T_DOUBLE: _value.d = locals->double_at(_index); break;
  4858. case T_OBJECT: {
  4859. oop obj = locals->obj_at(_index)();
  4860. _value.l = JNIHandles::make_local(_calling_thread, obj);
  4861. break;
  4862. }
  4863. default: ShouldNotReachHere();
  4864. }
  4865. }
  4866. }
  4867. }
  4868. bool VM_GetOrSetLocal::allow_nested_vm_operations() const {
  4869. return true; // May need to deoptimize
  4870. }
  4871. VM_GetReceiver::VM_GetReceiver(
  4872. JavaThread* thread, JavaThread* caller_thread, jint depth)
  4873. : VM_GetOrSetLocal(thread, caller_thread, depth, 0) {}
  4874. bool JvmtiSuspendControl::suspend(JavaThread *java_thread) {
  4875. java_thread->java_suspend();
  4876. if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
  4877. return false;
  4878. }
  4879. return true;
  4880. }
  4881. bool JvmtiSuspendControl::resume(JavaThread *java_thread) {
  4882. assert(java_thread->is_being_ext_suspended(), "thread should be suspended");
  4883. {
  4884. MutexLocker ml(Threads_lock);
  4885. java_thread->java_resume();
  4886. }
  4887. return true;
  4888. }
  4889. void JvmtiSuspendControl::print() {
  4890. #ifndef PRODUCT
  4891. MutexLocker mu(Threads_lock);
  4892. ResourceMark rm;
  4893. tty->print("Suspended Threads: [");
  4894. for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) {
  4895. #ifdef JVMTI_TRACE
  4896. const char *name = JvmtiTrace::safe_get_thread_name(thread);
  4897. #else
  4898. const char *name = "";
  4899. #endif /*JVMTI_TRACE */
  4900. tty->print("%s(%c ", name, thread->is_being_ext_suspended() ? 'S' : '_');
  4901. if (!thread->has_last_Java_frame()) {
  4902. tty->print("no stack");
  4903. }
  4904. tty->print(") ");
  4905. }
  4906. tty->print_cr("]");
  4907. #endif
  4908. }
  4909. JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_load_event(
  4910. nmethod* nm) {
  4911. JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_LOAD);
  4912. event._event_data.compiled_method_load = nm;
  4913. nmethodLocker::lock_nmethod(nm);
  4914. return event;
  4915. }
  4916. JvmtiDeferredEvent JvmtiDeferredEvent::compiled_method_unload_event(
  4917. nmethod* nm, jmethodID id, const void* code) {
  4918. JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_COMPILED_METHOD_UNLOAD);
  4919. event._event_data.compiled_method_unload.nm = nm;
  4920. event._event_data.compiled_method_unload.method_id = id;
  4921. event._event_data.compiled_method_unload.code_begin = code;
  4922. nmethodLocker::lock_nmethod(nm, true /* zombie_ok */);
  4923. return event;
  4924. }
  4925. JvmtiDeferredEvent JvmtiDeferredEvent::dynamic_code_generated_event(
  4926. const char* name, const void* code_begin, const void* code_end) {
  4927. JvmtiDeferredEvent event = JvmtiDeferredEvent(TYPE_DYNAMIC_CODE_GENERATED);
  4928. event._event_data.dynamic_code_generated.name = os::strdup(name);
  4929. event._event_data.dynamic_code_generated.code_begin = code_begin;
  4930. event._event_data.dynamic_code_generated.code_end = code_end;
  4931. return event;
  4932. }
  4933. void JvmtiDeferredEvent::post() {
  4934. assert(ServiceThread::is_service_thread(Thread::current()),
  4935. "Service thread must post enqueued events");
  4936. switch(_type) {
  4937. case TYPE_COMPILED_METHOD_LOAD: {
  4938. nmethod* nm = _event_data.compiled_method_load;
  4939. JvmtiExport::post_compiled_method_load(nm);
  4940. nmethodLocker::unlock_nmethod(nm);
  4941. break;
  4942. }
  4943. case TYPE_COMPILED_METHOD_UNLOAD: {
  4944. nmethod* nm = _event_data.compiled_method_unload.nm;
  4945. JvmtiExport::post_compiled_method_unload(
  4946. _event_data.compiled_method_unload.method_id,
  4947. _event_data.compiled_method_unload.code_begin);
  4948. nmethodLocker::unlock_nmethod(nm);
  4949. break;
  4950. }
  4951. case TYPE_DYNAMIC_CODE_GENERATED: {
  4952. JvmtiExport::post_dynamic_code_generated_internal(
  4953. (_event_data.dynamic_code_generated.name == NULL)
  4954. ? "unknown_code" : _event_data.dynamic_code_generated.name,
  4955. _event_data.dynamic_code_generated.code_begin,
  4956. _event_data.dynamic_code_generated.code_end);
  4957. if (_event_data.dynamic_code_generated.name != NULL) {
  4958. os::free((void *)_event_data.dynamic_code_generated.name);
  4959. }
  4960. break;
  4961. }
  4962. default:
  4963. ShouldNotReachHere();
  4964. }
  4965. }
  4966. JvmtiDeferredEventQueue::QueueNode* JvmtiDeferredEventQueue::_queue_tail = NULL;
  4967. JvmtiDeferredEventQueue::QueueNode* JvmtiDeferredEventQueue::_queue_head = NULL;
  4968. volatile JvmtiDeferredEventQueue::QueueNode*
  4969. JvmtiDeferredEventQueue::_pending_list = NULL;
  4970. bool JvmtiDeferredEventQueue::has_events() {
  4971. assert(Service_lock->owned_by_self(), "Must own Service_lock");
  4972. return _queue_head != NULL || _pending_list != NULL;
  4973. }
  4974. void JvmtiDeferredEventQueue::enqueue(const JvmtiDeferredEvent& event) {
  4975. assert(Service_lock->owned_by_self(), "Must own Service_lock");
  4976. process_pending_events();
  4977. QueueNode* node = new QueueNode(event);
  4978. if (_queue_tail == NULL) {
  4979. _queue_tail = _queue_head = node;
  4980. } else {
  4981. assert(_queue_tail->next() == NULL, "Must be the last element in the list");
  4982. _queue_tail->set_next(node);
  4983. _queue_tail = node;
  4984. }
  4985. Service_lock->notify_all();
  4986. assert((_queue_head == NULL) == (_queue_tail == NULL),
  4987. "Inconsistent queue markers");
  4988. }
  4989. JvmtiDeferredEvent JvmtiDeferredEventQueue::dequeue() {
  4990. assert(Service_lock->owned_by_self(), "Must own Service_lock");
  4991. process_pending_events();
  4992. assert(_queue_head != NULL, "Nothing to dequeue");
  4993. if (_queue_head == NULL) {
  4994. return JvmtiDeferredEvent();
  4995. }
  4996. QueueNode* node = _queue_head;
  4997. _queue_head = _queue_head->next();
  4998. if (_queue_head == NULL) {
  4999. _queue_tail = NULL;
  5000. }
  5001. assert((_queue_head == NULL) == (_queue_tail == NULL),
  5002. "Inconsistent queue markers");
  5003. JvmtiDeferredEvent event = node->event();
  5004. delete node;
  5005. return event;
  5006. }
  5007. void JvmtiDeferredEventQueue::add_pending_event(
  5008. const JvmtiDeferredEvent& event) {
  5009. QueueNode* node = new QueueNode(event);
  5010. bool success = false;
  5011. QueueNode* prev_value = (QueueNode*)_pending_list;
  5012. do {
  5013. node->set_next(prev_value);
  5014. prev_value = (QueueNode*)Atomic::cmpxchg_ptr(
  5015. (void*)node, (volatile void*)&_pending_list, (void*)node->next());
  5016. } while (prev_value != node->next());
  5017. }
  5018. void JvmtiDeferredEventQueue::process_pending_events() {
  5019. assert(Service_lock->owned_by_self(), "Must own Service_lock");
  5020. if (_pending_list != NULL) {
  5021. QueueNode* head =
  5022. (QueueNode*)Atomic::xchg_ptr(NULL, (volatile void*)&_pending_list);
  5023. assert((_queue_head == NULL) == (_queue_tail == NULL),
  5024. "Inconsistent queue markers");
  5025. if (head != NULL) {
  5026. QueueNode* new_tail = head;
  5027. QueueNode* new_head = NULL;
  5028. QueueNode* prev = new_tail;
  5029. QueueNode* node = new_tail->next();
  5030. new_tail->set_next(NULL);
  5031. while (node != NULL) {
  5032. QueueNode* next = node->next();
  5033. node->set_next(prev);
  5034. prev = node;
  5035. node = next;
  5036. }
  5037. new_head = prev;
  5038. if (_queue_tail != NULL) {
  5039. _queue_tail->set_next(new_head);
  5040. } else { // _queue_head == NULL
  5041. _queue_head = new_head;
  5042. }
  5043. _queue_tail = new_tail;
  5044. }
  5045. }
  5046. }
  5047. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiImpl.hpp
  5048. #ifndef SHARE_VM_PRIMS_JVMTIIMPL_HPP
  5049. #define SHARE_VM_PRIMS_JVMTIIMPL_HPP
  5050. #include "classfile/systemDictionary.hpp"
  5051. #include "jvmtifiles/jvmti.h"
  5052. #include "oops/objArrayOop.hpp"
  5053. #include "prims/jvmtiEnvThreadState.hpp"
  5054. #include "prims/jvmtiEventController.hpp"
  5055. #include "prims/jvmtiTrace.hpp"
  5056. #include "prims/jvmtiUtil.hpp"
  5057. #include "runtime/stackValueCollection.hpp"
  5058. #include "runtime/vm_operations.hpp"
  5059. class JvmtiBreakpoint;
  5060. class JvmtiBreakpoints;
  5061. class GrowableElement : public CHeapObj<mtInternal> {
  5062. public:
  5063. virtual ~GrowableElement() {}
  5064. virtual address getCacheValue() =0;
  5065. virtual bool equals(GrowableElement* e) =0;
  5066. virtual bool lessThan(GrowableElement *e)=0;
  5067. virtual GrowableElement *clone() =0;
  5068. virtual void oops_do(OopClosure* f) =0;
  5069. virtual void metadata_do(void f(Metadata*)) =0;
  5070. };
  5071. class GrowableCache VALUE_OBJ_CLASS_SPEC {
  5072. private:
  5073. void *_this_obj;
  5074. GrowableArray<GrowableElement *> *_elements;
  5075. address *_cache;
  5076. void (*_listener_fun)(void *, address*);
  5077. static bool equals(void *, GrowableElement *);
  5078. void recache();
  5079. public:
  5080. GrowableCache();
  5081. ~GrowableCache();
  5082. void initialize(void *this_obj, void listener_fun(void *, address*) );
  5083. int length();
  5084. GrowableElement* at(int index);
  5085. int find(GrowableElement* e);
  5086. void append(GrowableElement* e);
  5087. void insert(GrowableElement* e);
  5088. void remove (int index);
  5089. void clear();
  5090. void oops_do(OopClosure* f);
  5091. void metadata_do(void f(Metadata*));
  5092. void gc_epilogue();
  5093. };
  5094. class JvmtiBreakpointCache : public CHeapObj<mtInternal> {
  5095. private:
  5096. GrowableCache _cache;
  5097. public:
  5098. JvmtiBreakpointCache() {}
  5099. ~JvmtiBreakpointCache() {}
  5100. void initialize(void *this_obj, void listener_fun(void *, address*) ) {
  5101. _cache.initialize(this_obj,listener_fun);
  5102. }
  5103. int length() { return _cache.length(); }
  5104. JvmtiBreakpoint& at(int index) { return (JvmtiBreakpoint&) *(_cache.at(index)); }
  5105. int find(JvmtiBreakpoint& e) { return _cache.find((GrowableElement *) &e); }
  5106. void append(JvmtiBreakpoint& e) { _cache.append((GrowableElement *) &e); }
  5107. void remove (int index) { _cache.remove(index); }
  5108. void clear() { _cache.clear(); }
  5109. void oops_do(OopClosure* f) { _cache.oops_do(f); }
  5110. void metadata_do(void f(Metadata*)) { _cache.metadata_do(f); }
  5111. void gc_epilogue() { _cache.gc_epilogue(); }
  5112. };
  5113. typedef void (Method::*method_action)(int _bci);
  5114. class JvmtiBreakpoint : public GrowableElement {
  5115. private:
  5116. Method* _method;
  5117. int _bci;
  5118. Bytecodes::Code _orig_bytecode;
  5119. oop _class_holder; // keeps _method memory from being deallocated
  5120. public:
  5121. JvmtiBreakpoint();
  5122. JvmtiBreakpoint(Method* m_method, jlocation location);
  5123. bool equals(JvmtiBreakpoint& bp);
  5124. bool lessThan(JvmtiBreakpoint &bp);
  5125. void copy(JvmtiBreakpoint& bp);
  5126. bool is_valid();
  5127. address getBcp();
  5128. void each_method_version_do(method_action meth_act);
  5129. void set();
  5130. void clear();
  5131. void print();
  5132. Method* method() { return _method; }
  5133. address getCacheValue() { return getBcp(); }
  5134. bool lessThan(GrowableElement* e) { Unimplemented(); return false; }
  5135. bool equals(GrowableElement* e) { return equals((JvmtiBreakpoint&) *e); }
  5136. void oops_do(OopClosure* f) {
  5137. f->do_oop(&_class_holder);
  5138. }
  5139. void metadata_do(void f(Metadata*)) {
  5140. f(_method);
  5141. }
  5142. GrowableElement *clone() {
  5143. JvmtiBreakpoint *bp = new JvmtiBreakpoint();
  5144. bp->copy(*this);
  5145. return bp;
  5146. }
  5147. };
  5148. class JvmtiBreakpoints : public CHeapObj<mtInternal> {
  5149. private:
  5150. JvmtiBreakpointCache _bps;
  5151. friend class VM_ChangeBreakpoints;
  5152. void set_at_safepoint(JvmtiBreakpoint& bp);
  5153. void clear_at_safepoint(JvmtiBreakpoint& bp);
  5154. static void do_element(GrowableElement *e);
  5155. public:
  5156. JvmtiBreakpoints(void listener_fun(void *, address *));
  5157. ~JvmtiBreakpoints();
  5158. int length();
  5159. void oops_do(OopClosure* f);
  5160. void metadata_do(void f(Metadata*));
  5161. void print();
  5162. int set(JvmtiBreakpoint& bp);
  5163. int clear(JvmtiBreakpoint& bp);
  5164. void clearall_in_class_at_safepoint(Klass* klass);
  5165. void gc_epilogue();
  5166. };
  5167. class JvmtiCurrentBreakpoints : public AllStatic {
  5168. private:
  5169. static JvmtiBreakpoints *_jvmti_breakpoints;
  5170. static address *_breakpoint_list;
  5171. static inline void set_breakpoint_list(address *breakpoint_list) { _breakpoint_list = breakpoint_list; }
  5172. static inline address *get_breakpoint_list() { return _breakpoint_list; }
  5173. static void listener_fun(void *this_obj, address *cache);
  5174. public:
  5175. static void initialize();
  5176. static void destroy();
  5177. static JvmtiBreakpoints& get_jvmti_breakpoints();
  5178. static inline bool is_breakpoint(address bcp);
  5179. static void oops_do(OopClosure* f);
  5180. static void metadata_do(void f(Metadata*)) NOT_JVMTI_RETURN;
  5181. static void gc_epilogue();
  5182. };
  5183. bool JvmtiCurrentBreakpoints::is_breakpoint(address bcp) {
  5184. address *bps = get_breakpoint_list();
  5185. if (bps == NULL) return false;
  5186. for ( ; (*bps) != NULL; bps++) {
  5187. if ((*bps) == bcp) return true;
  5188. }
  5189. return false;
  5190. }
  5191. class VM_ChangeBreakpoints : public VM_Operation {
  5192. private:
  5193. JvmtiBreakpoints* _breakpoints;
  5194. int _operation;
  5195. JvmtiBreakpoint* _bp;
  5196. public:
  5197. enum { SET_BREAKPOINT=0, CLEAR_BREAKPOINT=1 };
  5198. VM_ChangeBreakpoints(int operation, JvmtiBreakpoint *bp) {
  5199. JvmtiBreakpoints& current_bps = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
  5200. _breakpoints = &current_bps;
  5201. _bp = bp;
  5202. _operation = operation;
  5203. assert(bp != NULL, "bp != NULL");
  5204. }
  5205. VMOp_Type type() const { return VMOp_ChangeBreakpoints; }
  5206. void doit();
  5207. void oops_do(OopClosure* f);
  5208. void metadata_do(void f(Metadata*));
  5209. };
  5210. class VM_GetOrSetLocal : public VM_Operation {
  5211. protected:
  5212. JavaThread* _thread;
  5213. JavaThread* _calling_thread;
  5214. jint _depth;
  5215. jint _index;
  5216. BasicType _type;
  5217. jvalue _value;
  5218. javaVFrame* _jvf;
  5219. bool _set;
  5220. virtual bool getting_receiver() const { return false; }
  5221. jvmtiError _result;
  5222. vframe* get_vframe();
  5223. javaVFrame* get_java_vframe();
  5224. bool check_slot_type(javaVFrame* vf);
  5225. public:
  5226. VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type);
  5227. VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, jvalue value);
  5228. VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth,
  5229. int index);
  5230. VMOp_Type type() const { return VMOp_GetOrSetLocal; }
  5231. jvalue value() { return _value; }
  5232. jvmtiError result() { return _result; }
  5233. bool doit_prologue();
  5234. void doit();
  5235. bool allow_nested_vm_operations() const;
  5236. const char* name() const { return "get/set locals"; }
  5237. static bool is_assignable(const char* ty_sign, Klass* klass, Thread* thread);
  5238. };
  5239. class VM_GetReceiver : public VM_GetOrSetLocal {
  5240. protected:
  5241. virtual bool getting_receiver() const { return true; }
  5242. public:
  5243. VM_GetReceiver(JavaThread* thread, JavaThread* calling_thread, jint depth);
  5244. const char* name() const { return "get receiver"; }
  5245. };
  5246. class JvmtiSuspendControl : public AllStatic {
  5247. public:
  5248. static bool suspend(JavaThread *java_thread);
  5249. static bool resume(JavaThread *java_thread);
  5250. static void print();
  5251. };
  5252. class JvmtiDeferredEvent VALUE_OBJ_CLASS_SPEC {
  5253. friend class JvmtiDeferredEventQueue;
  5254. private:
  5255. typedef enum {
  5256. TYPE_NONE,
  5257. TYPE_COMPILED_METHOD_LOAD,
  5258. TYPE_COMPILED_METHOD_UNLOAD,
  5259. TYPE_DYNAMIC_CODE_GENERATED
  5260. } Type;
  5261. Type _type;
  5262. union {
  5263. nmethod* compiled_method_load;
  5264. struct {
  5265. nmethod* nm;
  5266. jmethodID method_id;
  5267. const void* code_begin;
  5268. } compiled_method_unload;
  5269. struct {
  5270. const char* name;
  5271. const void* code_begin;
  5272. const void* code_end;
  5273. } dynamic_code_generated;
  5274. } _event_data;
  5275. JvmtiDeferredEvent(Type t) : _type(t) {}
  5276. public:
  5277. JvmtiDeferredEvent() : _type(TYPE_NONE) {}
  5278. static JvmtiDeferredEvent compiled_method_load_event(nmethod* nm)
  5279. NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
  5280. static JvmtiDeferredEvent compiled_method_unload_event(nmethod* nm,
  5281. jmethodID id, const void* code) NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
  5282. static JvmtiDeferredEvent dynamic_code_generated_event(
  5283. const char* name, const void* begin, const void* end)
  5284. NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
  5285. void post() NOT_JVMTI_RETURN;
  5286. };
  5287. class JvmtiDeferredEventQueue : AllStatic {
  5288. friend class JvmtiDeferredEvent;
  5289. private:
  5290. class QueueNode : public CHeapObj<mtInternal> {
  5291. private:
  5292. JvmtiDeferredEvent _event;
  5293. QueueNode* _next;
  5294. public:
  5295. QueueNode(const JvmtiDeferredEvent& event)
  5296. : _event(event), _next(NULL) {}
  5297. const JvmtiDeferredEvent& event() const { return _event; }
  5298. QueueNode* next() const { return _next; }
  5299. void set_next(QueueNode* next) { _next = next; }
  5300. };
  5301. static QueueNode* _queue_head; // Hold Service_lock to access
  5302. static QueueNode* _queue_tail; // Hold Service_lock to access
  5303. static volatile QueueNode* _pending_list; // Uses CAS for read/update
  5304. static void process_pending_events() NOT_JVMTI_RETURN;
  5305. public:
  5306. static bool has_events() NOT_JVMTI_RETURN_(false);
  5307. static void enqueue(const JvmtiDeferredEvent& event) NOT_JVMTI_RETURN;
  5308. static JvmtiDeferredEvent dequeue() NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
  5309. static void add_pending_event(const JvmtiDeferredEvent&) NOT_JVMTI_RETURN;
  5310. };
  5311. #define NULL_CHECK(X, Y) if ((X) == NULL) { return (Y); }
  5312. #endif // SHARE_VM_PRIMS_JVMTIIMPL_HPP
  5313. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiManageCapabilities.cpp
  5314. #include "precompiled.hpp"
  5315. #include "jvmtifiles/jvmtiEnv.hpp"
  5316. #include "prims/jvmtiExport.hpp"
  5317. #include "prims/jvmtiManageCapabilities.hpp"
  5318. static const jint CAPA_SIZE = (JVMTI_INTERNAL_CAPABILITY_COUNT + 7) / 8;
  5319. jvmtiCapabilities JvmtiManageCapabilities::always_capabilities;
  5320. jvmtiCapabilities JvmtiManageCapabilities::onload_capabilities;
  5321. jvmtiCapabilities JvmtiManageCapabilities::always_solo_capabilities;
  5322. jvmtiCapabilities JvmtiManageCapabilities::onload_solo_capabilities;
  5323. jvmtiCapabilities JvmtiManageCapabilities::always_solo_remaining_capabilities;
  5324. jvmtiCapabilities JvmtiManageCapabilities::onload_solo_remaining_capabilities;
  5325. jvmtiCapabilities JvmtiManageCapabilities::acquired_capabilities;
  5326. void JvmtiManageCapabilities::initialize() {
  5327. always_capabilities = init_always_capabilities();
  5328. if (JvmtiEnv::get_phase() != JVMTI_PHASE_ONLOAD) {
  5329. recompute_always_capabilities();
  5330. }
  5331. onload_capabilities = init_onload_capabilities();
  5332. always_solo_capabilities = init_always_solo_capabilities();
  5333. onload_solo_capabilities = init_onload_solo_capabilities();
  5334. always_solo_remaining_capabilities = init_always_solo_capabilities();
  5335. onload_solo_remaining_capabilities = init_onload_solo_capabilities();
  5336. memset(&acquired_capabilities, 0, sizeof(acquired_capabilities));
  5337. }
  5338. void JvmtiManageCapabilities::recompute_always_capabilities() {
  5339. if (!UseSharedSpaces) {
  5340. jvmtiCapabilities jc = always_capabilities;
  5341. jc.can_generate_all_class_hook_events = 1;
  5342. always_capabilities = jc;
  5343. }
  5344. }
  5345. jvmtiCapabilities JvmtiManageCapabilities::init_always_capabilities() {
  5346. jvmtiCapabilities jc;
  5347. memset(&jc, 0, sizeof(jc));
  5348. jc.can_get_bytecodes = 1;
  5349. jc.can_signal_thread = 1;
  5350. jc.can_get_source_file_name = 1;
  5351. jc.can_get_line_numbers = 1;
  5352. jc.can_get_synthetic_attribute = 1;
  5353. jc.can_get_monitor_info = 1;
  5354. jc.can_get_constant_pool = 1;
  5355. jc.can_generate_monitor_events = 1;
  5356. jc.can_generate_garbage_collection_events = 1;
  5357. jc.can_generate_compiled_method_load_events = 1;
  5358. jc.can_generate_native_method_bind_events = 1;
  5359. jc.can_generate_vm_object_alloc_events = 1;
  5360. if (os::is_thread_cpu_time_supported()) {
  5361. jc.can_get_current_thread_cpu_time = 1;
  5362. jc.can_get_thread_cpu_time = 1;
  5363. }
  5364. jc.can_redefine_classes = 1;
  5365. jc.can_redefine_any_class = 1;
  5366. jc.can_retransform_classes = 1;
  5367. jc.can_retransform_any_class = 1;
  5368. jc.can_set_native_method_prefix = 1;
  5369. jc.can_tag_objects = 1;
  5370. jc.can_generate_object_free_events = 1;
  5371. jc.can_generate_resource_exhaustion_heap_events = 1;
  5372. jc.can_generate_resource_exhaustion_threads_events = 1;
  5373. return jc;
  5374. }
  5375. jvmtiCapabilities JvmtiManageCapabilities::init_onload_capabilities() {
  5376. jvmtiCapabilities jc;
  5377. memset(&jc, 0, sizeof(jc));
  5378. #ifndef ZERO
  5379. jc.can_pop_frame = 1;
  5380. jc.can_force_early_return = 1;
  5381. #endif // !ZERO
  5382. jc.can_get_source_debug_extension = 1;
  5383. jc.can_access_local_variables = 1;
  5384. jc.can_maintain_original_method_order = 1;
  5385. jc.can_generate_all_class_hook_events = 1;
  5386. jc.can_generate_single_step_events = 1;
  5387. jc.can_generate_exception_events = 1;
  5388. jc.can_generate_frame_pop_events = 1;
  5389. jc.can_generate_method_entry_events = 1;
  5390. jc.can_generate_method_exit_events = 1;
  5391. jc.can_get_owned_monitor_info = 1;
  5392. jc.can_get_owned_monitor_stack_depth_info = 1;
  5393. jc.can_get_current_contended_monitor = 1;
  5394. jc.can_tag_objects = 1; // TODO: this should have been removed
  5395. jc.can_generate_object_free_events = 1; // TODO: this should have been removed
  5396. return jc;
  5397. }
  5398. jvmtiCapabilities JvmtiManageCapabilities::init_always_solo_capabilities() {
  5399. jvmtiCapabilities jc;
  5400. memset(&jc, 0, sizeof(jc));
  5401. jc.can_suspend = 1;
  5402. return jc;
  5403. }
  5404. jvmtiCapabilities JvmtiManageCapabilities::init_onload_solo_capabilities() {
  5405. jvmtiCapabilities jc;
  5406. memset(&jc, 0, sizeof(jc));
  5407. jc.can_generate_field_modification_events = 1;
  5408. jc.can_generate_field_access_events = 1;
  5409. jc.can_generate_breakpoint_events = 1;
  5410. return jc;
  5411. }
  5412. jvmtiCapabilities *JvmtiManageCapabilities::either(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
  5413. jvmtiCapabilities *result) {
  5414. char *ap = (char *)a;
  5415. char *bp = (char *)b;
  5416. char *resultp = (char *)result;
  5417. for (int i = 0; i < CAPA_SIZE; ++i) {
  5418. }
  5419. return result;
  5420. }
  5421. jvmtiCapabilities *JvmtiManageCapabilities::both(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
  5422. jvmtiCapabilities *result) {
  5423. char *ap = (char *)a;
  5424. char *bp = (char *)b;
  5425. char *resultp = (char *)result;
  5426. for (int i = 0; i < CAPA_SIZE; ++i) {
  5427. }
  5428. return result;
  5429. }
  5430. jvmtiCapabilities *JvmtiManageCapabilities::exclude(const jvmtiCapabilities *a, const jvmtiCapabilities *b,
  5431. jvmtiCapabilities *result) {
  5432. char *ap = (char *)a;
  5433. char *bp = (char *)b;
  5434. char *resultp = (char *)result;
  5435. for (int i = 0; i < CAPA_SIZE; ++i) {
  5436. }
  5437. return result;
  5438. }
  5439. bool JvmtiManageCapabilities::has_some(const jvmtiCapabilities *a) {
  5440. char *ap = (char *)a;
  5441. for (int i = 0; i < CAPA_SIZE; ++i) {
  5442. if (*ap++ != 0) {
  5443. return true;
  5444. }
  5445. }
  5446. return false;
  5447. }
  5448. void JvmtiManageCapabilities::copy_capabilities(const jvmtiCapabilities *from, jvmtiCapabilities *to) {
  5449. char *ap = (char *)from;
  5450. char *resultp = (char *)to;
  5451. for (int i = 0; i < CAPA_SIZE; ++i) {
  5452. }
  5453. }
  5454. void JvmtiManageCapabilities::get_potential_capabilities(const jvmtiCapabilities *current,
  5455. const jvmtiCapabilities *prohibited,
  5456. jvmtiCapabilities *result) {
  5457. exclude(&always_capabilities, prohibited, result);
  5458. either(result, current, result);
  5459. either(result, &always_solo_remaining_capabilities, result);
  5460. if (JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
  5461. either(result, &onload_capabilities, result);
  5462. either(result, &onload_solo_remaining_capabilities, result);
  5463. }
  5464. }
  5465. jvmtiError JvmtiManageCapabilities::add_capabilities(const jvmtiCapabilities *current,
  5466. const jvmtiCapabilities *prohibited,
  5467. const jvmtiCapabilities *desired,
  5468. jvmtiCapabilities *result) {
  5469. jvmtiCapabilities temp;
  5470. get_potential_capabilities(current, prohibited, &temp);
  5471. if (has_some(exclude(desired, &temp, &temp))) {
  5472. return JVMTI_ERROR_NOT_AVAILABLE;
  5473. }
  5474. either(&acquired_capabilities, desired, &acquired_capabilities);
  5475. both(&onload_capabilities, desired, &temp);
  5476. either(&always_capabilities, &temp, &always_capabilities);
  5477. exclude(&onload_capabilities, &temp, &onload_capabilities);
  5478. both(&onload_solo_capabilities, desired, &temp);
  5479. either(&always_solo_capabilities, &temp, &always_solo_capabilities);
  5480. exclude(&onload_solo_capabilities, &temp, &onload_solo_capabilities);
  5481. exclude(&always_solo_remaining_capabilities, desired, &always_solo_remaining_capabilities);
  5482. exclude(&onload_solo_remaining_capabilities, desired, &onload_solo_remaining_capabilities);
  5483. either(current, desired, result);
  5484. update();
  5485. return JVMTI_ERROR_NONE;
  5486. }
  5487. void JvmtiManageCapabilities::relinquish_capabilities(const jvmtiCapabilities *current,
  5488. const jvmtiCapabilities *unwanted,
  5489. jvmtiCapabilities *result) {
  5490. jvmtiCapabilities to_trash;
  5491. jvmtiCapabilities temp;
  5492. both(current, unwanted, &to_trash);
  5493. either(&always_solo_remaining_capabilities, both(&always_solo_capabilities, &to_trash, &temp),
  5494. &always_solo_remaining_capabilities);
  5495. either(&onload_solo_remaining_capabilities, both(&onload_solo_capabilities, &to_trash, &temp),
  5496. &onload_solo_remaining_capabilities);
  5497. update();
  5498. exclude(current, unwanted, result);
  5499. }
  5500. void JvmtiManageCapabilities::update() {
  5501. jvmtiCapabilities avail;
  5502. either(&always_capabilities, &always_solo_capabilities, &avail);
  5503. bool interp_events =
  5504. avail.can_generate_field_access_events ||
  5505. avail.can_generate_field_modification_events ||
  5506. avail.can_generate_single_step_events ||
  5507. avail.can_generate_frame_pop_events ||
  5508. avail.can_generate_method_entry_events ||
  5509. avail.can_generate_method_exit_events;
  5510. bool enter_all_methods =
  5511. interp_events ||
  5512. avail.can_generate_breakpoint_events;
  5513. if (enter_all_methods) {
  5514. UseFastEmptyMethods = false;
  5515. UseFastAccessorMethods = false;
  5516. }
  5517. if (avail.can_generate_breakpoint_events) {
  5518. RewriteFrequentPairs = false;
  5519. }
  5520. if ((avail.can_redefine_classes || avail.can_retransform_classes) &&
  5521. JvmtiEnv::get_phase() == JVMTI_PHASE_ONLOAD) {
  5522. JvmtiExport::set_all_dependencies_are_recorded(true);
  5523. }
  5524. JvmtiExport::set_can_get_source_debug_extension(avail.can_get_source_debug_extension);
  5525. JvmtiExport::set_can_maintain_original_method_order(avail.can_maintain_original_method_order);
  5526. JvmtiExport::set_can_post_interpreter_events(interp_events);
  5527. JvmtiExport::set_can_hotswap_or_post_breakpoint(
  5528. avail.can_generate_breakpoint_events ||
  5529. avail.can_redefine_classes ||
  5530. avail.can_retransform_classes);
  5531. JvmtiExport::set_can_modify_any_class(
  5532. avail.can_generate_breakpoint_events ||
  5533. avail.can_generate_all_class_hook_events);
  5534. JvmtiExport::set_can_walk_any_space(
  5535. avail.can_tag_objects); // disable sharing in onload phase
  5536. JvmtiExport::set_can_access_local_variables(
  5537. avail.can_access_local_variables ||
  5538. avail.can_generate_breakpoint_events ||
  5539. avail.can_generate_frame_pop_events);
  5540. JvmtiExport::set_can_post_on_exceptions(
  5541. avail.can_generate_exception_events ||
  5542. avail.can_generate_frame_pop_events ||
  5543. avail.can_generate_method_exit_events);
  5544. JvmtiExport::set_can_post_breakpoint(avail.can_generate_breakpoint_events);
  5545. JvmtiExport::set_can_post_field_access(avail.can_generate_field_access_events);
  5546. JvmtiExport::set_can_post_field_modification(avail.can_generate_field_modification_events);
  5547. JvmtiExport::set_can_post_method_entry(avail.can_generate_method_entry_events);
  5548. JvmtiExport::set_can_post_method_exit(avail.can_generate_method_exit_events ||
  5549. avail.can_generate_frame_pop_events);
  5550. JvmtiExport::set_can_pop_frame(avail.can_pop_frame);
  5551. JvmtiExport::set_can_force_early_return(avail.can_force_early_return);
  5552. JvmtiExport::set_should_clean_up_heap_objects(avail.can_generate_breakpoint_events);
  5553. }
  5554. #ifndef PRODUCT
  5555. void JvmtiManageCapabilities:: print(const jvmtiCapabilities* cap) {
  5556. tty->print_cr("----- capabilities -----");
  5557. if (cap->can_tag_objects)
  5558. tty->print_cr("can_tag_objects");
  5559. if (cap->can_generate_field_modification_events)
  5560. tty->print_cr("can_generate_field_modification_events");
  5561. if (cap->can_generate_field_access_events)
  5562. tty->print_cr("can_generate_field_access_events");
  5563. if (cap->can_get_bytecodes)
  5564. tty->print_cr("can_get_bytecodes");
  5565. if (cap->can_get_synthetic_attribute)
  5566. tty->print_cr("can_get_synthetic_attribute");
  5567. if (cap->can_get_owned_monitor_info)
  5568. tty->print_cr("can_get_owned_monitor_info");
  5569. if (cap->can_get_current_contended_monitor)
  5570. tty->print_cr("can_get_current_contended_monitor");
  5571. if (cap->can_get_monitor_info)
  5572. tty->print_cr("can_get_monitor_info");
  5573. if (cap->can_get_constant_pool)
  5574. tty->print_cr("can_get_constant_pool");
  5575. if (cap->can_pop_frame)
  5576. tty->print_cr("can_pop_frame");
  5577. if (cap->can_force_early_return)
  5578. tty->print_cr("can_force_early_return");
  5579. if (cap->can_redefine_classes)
  5580. tty->print_cr("can_redefine_classes");
  5581. if (cap->can_retransform_classes)
  5582. tty->print_cr("can_retransform_classes");
  5583. if (cap->can_signal_thread)
  5584. tty->print_cr("can_signal_thread");
  5585. if (cap->can_get_source_file_name)
  5586. tty->print_cr("can_get_source_file_name");
  5587. if (cap->can_get_line_numbers)
  5588. tty->print_cr("can_get_line_numbers");
  5589. if (cap->can_get_source_debug_extension)
  5590. tty->print_cr("can_get_source_debug_extension");
  5591. if (cap->can_access_local_variables)
  5592. tty->print_cr("can_access_local_variables");
  5593. if (cap->can_maintain_original_method_order)
  5594. tty->print_cr("can_maintain_original_method_order");
  5595. if (cap->can_generate_single_step_events)
  5596. tty->print_cr("can_generate_single_step_events");
  5597. if (cap->can_generate_exception_events)
  5598. tty->print_cr("can_generate_exception_events");
  5599. if (cap->can_generate_frame_pop_events)
  5600. tty->print_cr("can_generate_frame_pop_events");
  5601. if (cap->can_generate_breakpoint_events)
  5602. tty->print_cr("can_generate_breakpoint_events");
  5603. if (cap->can_suspend)
  5604. tty->print_cr("can_suspend");
  5605. if (cap->can_redefine_any_class )
  5606. tty->print_cr("can_redefine_any_class");
  5607. if (cap->can_retransform_any_class )
  5608. tty->print_cr("can_retransform_any_class");
  5609. if (cap->can_get_current_thread_cpu_time)
  5610. tty->print_cr("can_get_current_thread_cpu_time");
  5611. if (cap->can_get_thread_cpu_time)
  5612. tty->print_cr("can_get_thread_cpu_time");
  5613. if (cap->can_generate_method_entry_events)
  5614. tty->print_cr("can_generate_method_entry_events");
  5615. if (cap->can_generate_method_exit_events)
  5616. tty->print_cr("can_generate_method_exit_events");
  5617. if (cap->can_generate_all_class_hook_events)
  5618. tty->print_cr("can_generate_all_class_hook_events");
  5619. if (cap->can_generate_compiled_method_load_events)
  5620. tty->print_cr("can_generate_compiled_method_load_events");
  5621. if (cap->can_generate_monitor_events)
  5622. tty->print_cr("can_generate_monitor_events");
  5623. if (cap->can_generate_vm_object_alloc_events)
  5624. tty->print_cr("can_generate_vm_object_alloc_events");
  5625. if (cap->can_generate_native_method_bind_events)
  5626. tty->print_cr("can_generate_native_method_bind_events");
  5627. if (cap->can_generate_garbage_collection_events)
  5628. tty->print_cr("can_generate_garbage_collection_events");
  5629. if (cap->can_generate_object_free_events)
  5630. tty->print_cr("can_generate_object_free_events");
  5631. if (cap->can_generate_resource_exhaustion_heap_events)
  5632. tty->print_cr("can_generate_resource_exhaustion_heap_events");
  5633. if (cap->can_generate_resource_exhaustion_threads_events)
  5634. tty->print_cr("can_generate_resource_exhaustion_threads_events");
  5635. }
  5636. #endif
  5637. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiManageCapabilities.hpp
  5638. #ifndef SHARE_VM_PRIMS_JVMTIMANAGECAPABILITIES_HPP
  5639. #define SHARE_VM_PRIMS_JVMTIMANAGECAPABILITIES_HPP
  5640. #include "jvmtifiles/jvmti.h"
  5641. #include "memory/allocation.hpp"
  5642. class JvmtiManageCapabilities : public AllStatic {
  5643. private:
  5644. static jvmtiCapabilities always_capabilities;
  5645. static jvmtiCapabilities onload_capabilities;
  5646. static jvmtiCapabilities always_solo_capabilities;
  5647. static jvmtiCapabilities onload_solo_capabilities;
  5648. static jvmtiCapabilities always_solo_remaining_capabilities;
  5649. static jvmtiCapabilities onload_solo_remaining_capabilities;
  5650. static jvmtiCapabilities acquired_capabilities;
  5651. static jvmtiCapabilities *either(const jvmtiCapabilities *a, const jvmtiCapabilities *b, jvmtiCapabilities *result);
  5652. static jvmtiCapabilities *both(const jvmtiCapabilities *a, const jvmtiCapabilities *b, jvmtiCapabilities *result);
  5653. static jvmtiCapabilities *exclude(const jvmtiCapabilities *a, const jvmtiCapabilities *b, jvmtiCapabilities *result);
  5654. static bool has_some(const jvmtiCapabilities *a);
  5655. static void update();
  5656. static jvmtiCapabilities init_always_capabilities();
  5657. static jvmtiCapabilities init_onload_capabilities();
  5658. static jvmtiCapabilities init_always_solo_capabilities();
  5659. static jvmtiCapabilities init_onload_solo_capabilities();
  5660. public:
  5661. static void initialize();
  5662. static void recompute_always_capabilities();
  5663. static void get_potential_capabilities(const jvmtiCapabilities *current,
  5664. const jvmtiCapabilities *prohibited,
  5665. jvmtiCapabilities *result);
  5666. static jvmtiError add_capabilities(const jvmtiCapabilities *current,
  5667. const jvmtiCapabilities *prohibited,
  5668. const jvmtiCapabilities *desired,
  5669. jvmtiCapabilities *result);
  5670. static void relinquish_capabilities(const jvmtiCapabilities *current,
  5671. const jvmtiCapabilities *unwanted,
  5672. jvmtiCapabilities *result);
  5673. static void copy_capabilities(const jvmtiCapabilities *from, jvmtiCapabilities *to);
  5674. #ifndef PRODUCT
  5675. static void print(const jvmtiCapabilities* caps);
  5676. #endif
  5677. };
  5678. #endif // SHARE_VM_PRIMS_JVMTIMANAGECAPABILITIES_HPP
  5679. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiRawMonitor.cpp
  5680. #include "precompiled.hpp"
  5681. #include "prims/jvmtiRawMonitor.hpp"
  5682. #include "runtime/interfaceSupport.hpp"
  5683. #include "runtime/orderAccess.inline.hpp"
  5684. #include "runtime/thread.inline.hpp"
  5685. GrowableArray<JvmtiRawMonitor*> *JvmtiPendingMonitors::_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiRawMonitor*>(1,true);
  5686. void JvmtiPendingMonitors::transition_raw_monitors() {
  5687. assert((Threads::number_of_threads()==1),
  5688. "Java thread has not created yet or more than one java thread \
  5689. is running. Raw monitor transition will not work");
  5690. JavaThread *current_java_thread = JavaThread::current();
  5691. assert(current_java_thread->thread_state() == _thread_in_vm, "Must be in vm");
  5692. {
  5693. ThreadBlockInVM __tbivm(current_java_thread);
  5694. for(int i=0; i< count(); i++) {
  5695. JvmtiRawMonitor *rmonitor = monitors()->at(i);
  5696. int r = rmonitor->raw_enter(current_java_thread);
  5697. assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
  5698. }
  5699. }
  5700. dispose();
  5701. }
  5702. JvmtiRawMonitor::JvmtiRawMonitor(const char *name) {
  5703. #ifdef ASSERT
  5704. _name = strcpy(NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtInternal), name);
  5705. #else
  5706. _name = NULL;
  5707. #endif
  5708. _magic = JVMTI_RM_MAGIC;
  5709. }
  5710. JvmtiRawMonitor::~JvmtiRawMonitor() {
  5711. #ifdef ASSERT
  5712. FreeHeap(_name);
  5713. #endif
  5714. _magic = 0;
  5715. }
  5716. bool
  5717. JvmtiRawMonitor::is_valid() {
  5718. int value = 0;
  5719. switch (sizeof(_magic)) {
  5720. case 2:
  5721. value = Bytes::get_native_u2((address)&_magic);
  5722. break;
  5723. case 4:
  5724. value = Bytes::get_native_u4((address)&_magic);
  5725. break;
  5726. case 8:
  5727. value = Bytes::get_native_u8((address)&_magic);
  5728. break;
  5729. default:
  5730. guarantee(false, "_magic field is an unexpected size");
  5731. }
  5732. return value == JVMTI_RM_MAGIC;
  5733. }
  5734. int JvmtiRawMonitor::SimpleEnter (Thread * Self) {
  5735. for (;;) {
  5736. if (Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
  5737. return OS_OK ;
  5738. }
  5739. ObjectWaiter Node (Self) ;
  5740. Self->_ParkEvent->reset() ; // strictly optional
  5741. Node.TState = ObjectWaiter::TS_ENTER ;
  5742. RawMonitor_lock->lock_without_safepoint_check() ;
  5743. Node._next = _EntryList ;
  5744. _EntryList = &Node ;
  5745. OrderAccess::fence() ;
  5746. if (_owner == NULL && Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
  5747. _EntryList = Node._next ;
  5748. RawMonitor_lock->unlock() ;
  5749. return OS_OK ;
  5750. }
  5751. RawMonitor_lock->unlock() ;
  5752. while (Node.TState == ObjectWaiter::TS_ENTER) {
  5753. Self->_ParkEvent->park() ;
  5754. }
  5755. }
  5756. }
  5757. int JvmtiRawMonitor::SimpleExit (Thread * Self) {
  5758. guarantee (_owner == Self, "invariant") ;
  5759. OrderAccess::release_store_ptr (&_owner, NULL) ;
  5760. OrderAccess::fence() ;
  5761. if (_EntryList == NULL) return OS_OK ;
  5762. ObjectWaiter * w ;
  5763. RawMonitor_lock->lock_without_safepoint_check() ;
  5764. w = _EntryList ;
  5765. if (w != NULL) {
  5766. _EntryList = w->_next ;
  5767. }
  5768. RawMonitor_lock->unlock() ;
  5769. if (w != NULL) {
  5770. guarantee (w ->TState == ObjectWaiter::TS_ENTER, "invariant") ;
  5771. ParkEvent * ev = w->_event ;
  5772. w->TState = ObjectWaiter::TS_RUN ;
  5773. OrderAccess::fence() ;
  5774. ev->unpark() ;
  5775. }
  5776. return OS_OK ;
  5777. }
  5778. int JvmtiRawMonitor::SimpleWait (Thread * Self, jlong millis) {
  5779. guarantee (_owner == Self , "invariant") ;
  5780. guarantee (_recursions == 0, "invariant") ;
  5781. ObjectWaiter Node (Self) ;
  5782. Node._notified = 0 ;
  5783. Node.TState = ObjectWaiter::TS_WAIT ;
  5784. RawMonitor_lock->lock_without_safepoint_check() ;
  5785. Node._next = _WaitSet ;
  5786. _WaitSet = &Node ;
  5787. RawMonitor_lock->unlock() ;
  5788. SimpleExit (Self) ;
  5789. guarantee (_owner != Self, "invariant") ;
  5790. int ret = OS_OK ;
  5791. if (millis <= 0) {
  5792. Self->_ParkEvent->park();
  5793. } else {
  5794. ret = Self->_ParkEvent->park(millis);
  5795. }
  5796. if (Node.TState == ObjectWaiter::TS_WAIT) {
  5797. RawMonitor_lock->lock_without_safepoint_check() ;
  5798. if (Node.TState == ObjectWaiter::TS_WAIT) {
  5799. ObjectWaiter * p ;
  5800. ObjectWaiter * q = NULL ;
  5801. for (p = _WaitSet ; p != &Node; p = p->_next) {
  5802. q = p ;
  5803. }
  5804. guarantee (p == &Node, "invariant") ;
  5805. if (q == NULL) {
  5806. guarantee (p == _WaitSet, "invariant") ;
  5807. _WaitSet = p->_next ;
  5808. } else {
  5809. guarantee (p == q->_next, "invariant") ;
  5810. q->_next = p->_next ;
  5811. }
  5812. Node.TState = ObjectWaiter::TS_RUN ;
  5813. }
  5814. RawMonitor_lock->unlock() ;
  5815. }
  5816. guarantee (Node.TState == ObjectWaiter::TS_RUN, "invariant") ;
  5817. SimpleEnter (Self) ;
  5818. guarantee (_owner == Self, "invariant") ;
  5819. guarantee (_recursions == 0, "invariant") ;
  5820. return ret ;
  5821. }
  5822. int JvmtiRawMonitor::SimpleNotify (Thread * Self, bool All) {
  5823. guarantee (_owner == Self, "invariant") ;
  5824. if (_WaitSet == NULL) return OS_OK ;
  5825. ParkEvent * ev = NULL ; // consider using a small auto array ...
  5826. RawMonitor_lock->lock_without_safepoint_check() ;
  5827. for (;;) {
  5828. ObjectWaiter * w = _WaitSet ;
  5829. if (w == NULL) break ;
  5830. _WaitSet = w->_next ;
  5831. if (ev != NULL) { ev->unpark(); ev = NULL; }
  5832. ev = w->_event ;
  5833. OrderAccess::loadstore() ;
  5834. w->TState = ObjectWaiter::TS_RUN ;
  5835. OrderAccess::storeload();
  5836. if (!All) break ;
  5837. }
  5838. RawMonitor_lock->unlock() ;
  5839. if (ev != NULL) ev->unpark();
  5840. return OS_OK ;
  5841. }
  5842. int JvmtiRawMonitor::raw_enter(TRAPS) {
  5843. TEVENT (raw_enter) ;
  5844. void * Contended ;
  5845. JavaThread * jt = (JavaThread *)THREAD;
  5846. if (THREAD->is_Java_thread()) {
  5847. jt->SR_lock()->lock_without_safepoint_check();
  5848. while (jt->is_external_suspend()) {
  5849. jt->SR_lock()->unlock();
  5850. jt->java_suspend_self();
  5851. jt->SR_lock()->lock_without_safepoint_check();
  5852. }
  5853. Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
  5854. jt->SR_lock()->unlock();
  5855. } else {
  5856. Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
  5857. }
  5858. if (Contended == THREAD) {
  5859. _recursions ++ ;
  5860. return OM_OK ;
  5861. }
  5862. if (Contended == NULL) {
  5863. guarantee (_owner == THREAD, "invariant") ;
  5864. guarantee (_recursions == 0, "invariant") ;
  5865. return OM_OK ;
  5866. }
  5867. THREAD->set_current_pending_monitor(this);
  5868. if (!THREAD->is_Java_thread()) {
  5869. assert(THREAD->is_VM_thread(), "must be VM thread");
  5870. SimpleEnter (THREAD) ;
  5871. } else {
  5872. guarantee (jt->thread_state() == _thread_blocked, "invariant") ;
  5873. for (;;) {
  5874. jt->set_suspend_equivalent();
  5875. SimpleEnter (THREAD) ;
  5876. if (!jt->handle_special_suspend_equivalent_condition()) break ;
  5877. SimpleExit (THREAD) ;
  5878. jt->java_suspend_self();
  5879. }
  5880. assert(_owner == THREAD, "Fatal error with monitor owner!");
  5881. assert(_recursions == 0, "Fatal error with monitor recursions!");
  5882. }
  5883. THREAD->set_current_pending_monitor(NULL);
  5884. guarantee (_recursions == 0, "invariant") ;
  5885. return OM_OK;
  5886. }
  5887. int JvmtiRawMonitor::raw_exit(TRAPS) {
  5888. TEVENT (raw_exit) ;
  5889. if (THREAD != _owner) {
  5890. return OM_ILLEGAL_MONITOR_STATE;
  5891. }
  5892. if (_recursions > 0) {
  5893. --_recursions ;
  5894. return OM_OK ;
  5895. }
  5896. void * List = _EntryList ;
  5897. SimpleExit (THREAD) ;
  5898. return OM_OK;
  5899. }
  5900. int JvmtiRawMonitor::raw_wait(jlong millis, bool interruptible, TRAPS) {
  5901. TEVENT (raw_wait) ;
  5902. if (THREAD != _owner) {
  5903. return OM_ILLEGAL_MONITOR_STATE;
  5904. }
  5905. THREAD->_ParkEvent->reset() ;
  5906. OrderAccess::fence() ;
  5907. if (interruptible && Thread::is_interrupted(THREAD, true)) {
  5908. return OM_INTERRUPTED;
  5909. }
  5910. intptr_t save = _recursions ;
  5911. _recursions = 0 ;
  5912. _waiters ++ ;
  5913. if (THREAD->is_Java_thread()) {
  5914. guarantee (((JavaThread *) THREAD)->thread_state() == _thread_blocked, "invariant") ;
  5915. ((JavaThread *)THREAD)->set_suspend_equivalent();
  5916. }
  5917. int rv = SimpleWait (THREAD, millis) ;
  5918. _recursions = save ;
  5919. _waiters -- ;
  5920. guarantee (THREAD == _owner, "invariant") ;
  5921. if (THREAD->is_Java_thread()) {
  5922. JavaThread * jSelf = (JavaThread *) THREAD ;
  5923. for (;;) {
  5924. if (!jSelf->handle_special_suspend_equivalent_condition()) break ;
  5925. SimpleExit (THREAD) ;
  5926. jSelf->java_suspend_self();
  5927. SimpleEnter (THREAD) ;
  5928. jSelf->set_suspend_equivalent() ;
  5929. }
  5930. }
  5931. guarantee (THREAD == _owner, "invariant") ;
  5932. if (interruptible && Thread::is_interrupted(THREAD, true)) {
  5933. return OM_INTERRUPTED;
  5934. }
  5935. return OM_OK ;
  5936. }
  5937. int JvmtiRawMonitor::raw_notify(TRAPS) {
  5938. TEVENT (raw_notify) ;
  5939. if (THREAD != _owner) {
  5940. return OM_ILLEGAL_MONITOR_STATE;
  5941. }
  5942. SimpleNotify (THREAD, false) ;
  5943. return OM_OK;
  5944. }
  5945. int JvmtiRawMonitor::raw_notifyAll(TRAPS) {
  5946. TEVENT (raw_notifyAll) ;
  5947. if (THREAD != _owner) {
  5948. return OM_ILLEGAL_MONITOR_STATE;
  5949. }
  5950. SimpleNotify (THREAD, true) ;
  5951. return OM_OK;
  5952. }
  5953. C:\hotspot-69087d08d473\src\share\vm/prims/jvmtiRawMonitor.hpp
  5954. #ifndef SHARE_VM_PRIMS_JVMTIRAWMONITOR_HPP
  5955. #define SHARE_VM_PRIMS_JVMTIRAWMONITOR_HPP
  5956. #include "runtime/objectMonitor.hpp"
  5957. #include "utilities/growableArray.hpp"
  5958. class JvmtiRawMonitor : public ObjectMonitor {
  5959. private:
  5960. int _magic;
  5961. char * _name;
  5962. enum { JVMTI_RM_MAGIC = (int)(('T' << 24) | ('I' << 16) | ('R' << 8) | 'M') };
  5963. int SimpleEnter (Thread * Self) ;
  5964. int SimpleExit (Thread * Self) ;
  5965. int SimpleWait (Thread * Self, jlong millis) ;
  5966. int SimpleNotify (Thread * Self, bool All) ;
  5967. public:
  5968. JvmtiRawMonitor(const char *name);
  5969. ~JvmtiRawMonitor();
  5970. int raw_enter(TRAPS);
  5971. int raw_exit(TRAPS);
  5972. int raw_wait(jlong millis, bool interruptable, TRAPS);
  5973. int raw_notify(TRAPS);
  5974. int raw_notifyAll(TRAPS);
  5975. int magic() { return _magic; }
  5976. const char *get_name() { return _name; }
  5977. bool is_valid();
  5978. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/450799
推荐阅读
相关标签
  

闽ICP备14008679号