1 /*
2 * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoaderExt.hpp"
27 #include "classfile/javaClasses.inline.hpp"
28 #include "classfile/stringTable.hpp"
29 #include "classfile/modules.hpp"
30 #include "classfile/systemDictionary.hpp"
31 #include "classfile/vmSymbols.hpp"
32 #include "interpreter/bytecodeStream.hpp"
33 #include "interpreter/interpreter.hpp"
34 #include "jfr/jfrEvents.hpp"
35 #include "jvmtifiles/jvmtiEnv.hpp"
36 #include "logging/log.hpp"
37 #include "logging/logConfiguration.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "memory/universe.hpp"
40 #include "oops/instanceKlass.hpp"
41 #include "oops/objArrayOop.inline.hpp"
42 #include "oops/oop.inline.hpp"
43 #include "prims/jniCheck.hpp"
44 #include "prims/jvm_misc.hpp"
45 #include "prims/jvmtiAgentThread.hpp"
46 #include "prims/jvmtiClassFileReconstituter.hpp"
47 #include "prims/jvmtiCodeBlobEvents.hpp"
48 #include "prims/jvmtiExtensions.hpp"
49 #include "prims/jvmtiGetLoadedClasses.hpp"
50 #include "prims/jvmtiImpl.hpp"
51 #include "prims/jvmtiManageCapabilities.hpp"
52 #include "prims/jvmtiRawMonitor.hpp"
53 #include "prims/jvmtiRedefineClasses.hpp"
54 #include "prims/jvmtiTagMap.hpp"
55 #include "prims/jvmtiThreadState.inline.hpp"
56 #include "prims/jvmtiUtil.hpp"
57 #include "runtime/arguments.hpp"
58 #include "runtime/deoptimization.hpp"
59 #include "runtime/fieldDescriptor.inline.hpp"
60 #include "runtime/handles.inline.hpp"
61 #include "runtime/interfaceSupport.inline.hpp"
62 #include "runtime/javaCalls.hpp"
63 #include "runtime/jfieldIDWorkaround.hpp"
64 #include "runtime/jniHandles.inline.hpp"
65 #include "runtime/objectMonitor.inline.hpp"
66 #include "runtime/osThread.hpp"
67 #include "runtime/reflectionUtils.hpp"
68 #include "runtime/signature.hpp"
69 #include "runtime/thread.inline.hpp"
70 #include "runtime/threadHeapSampler.hpp"
71 #include "runtime/threadSMR.hpp"
72 #include "runtime/timerTrace.hpp"
73 #include "runtime/vframe.inline.hpp"
74 #include "runtime/vmThread.hpp"
75 #include "services/threadService.hpp"
76 #include "utilities/exceptions.hpp"
77 #include "utilities/preserveException.hpp"
78 #include "utilities/utf8.hpp"
79
80
81 #define FIXLATER 0 // REMOVE this when completed.
82
83 // FIXLATER: hook into JvmtiTrace
84 #define TraceJVMTICalls false
85
86 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
87 }
88
89 JvmtiEnv::~JvmtiEnv() {
90 }
91
92 JvmtiEnv*
93 JvmtiEnv::create_a_jvmti(jint version) {
94 return new JvmtiEnv(version);
95 }
96
97 // VM operation class to copy jni function table at safepoint.
98 // More than one java threads or jvmti agents may be reading/
99 // modifying jni function tables. To reduce the risk of bad
100 // interaction b/w these threads it is copied at safepoint.
101 class VM_JNIFunctionTableCopier : public VM_Operation {
102 private:
103 const struct JNINativeInterface_ *_function_table;
104 public:
105 VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
106 _function_table = func_tbl;
107 };
108
109 VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
110 void doit() {
111 copy_jni_function_table(_function_table);
112 };
113 };
114
115 //
116 // Do not change the "prefix" marker below, everything above it is copied
117 // unchanged into the filled stub, everything below is controlled by the
118 // stub filler (only method bodies are carried forward, and then only for
119 // functionality still in the spec).
120 //
121 // end file prefix
122
123 //
124 // Memory Management functions
125 //
126
127 // mem_ptr - pre-checked for NULL
128 jvmtiError
129 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
130 return allocate(size, mem_ptr);
131 } /* end Allocate */
132
133
134 // mem - NULL is a valid value, must be checked
135 jvmtiError
136 JvmtiEnv::Deallocate(unsigned char* mem) {
137 return deallocate(mem);
138 } /* end Deallocate */
139
140 // Threads_lock NOT held, java_thread not protected by lock
141 // java_thread - pre-checked
142 // data - NULL is a valid value, must be checked
143 jvmtiError
144 JvmtiEnv::SetThreadLocalStorage(JavaThread* java_thread, const void* data) {
145 JvmtiThreadState* state = java_thread->jvmti_thread_state();
146 if (state == NULL) {
147 if (data == NULL) {
148 // leaving state unset same as data set to NULL
149 return JVMTI_ERROR_NONE;
150 }
151 // otherwise, create the state
152 state = JvmtiThreadState::state_for(java_thread);
153 if (state == NULL) {
154 return JVMTI_ERROR_THREAD_NOT_ALIVE;
155 }
156 }
157 state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
158 return JVMTI_ERROR_NONE;
159 } /* end SetThreadLocalStorage */
160
161
162 // Threads_lock NOT held
163 // thread - NOT pre-checked
164 // data_ptr - pre-checked for NULL
165 jvmtiError
166 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
167 JavaThread* current_thread = JavaThread::current();
168 if (thread == NULL) {
169 JvmtiThreadState* state = current_thread->jvmti_thread_state();
170 *data_ptr = (state == NULL) ? NULL :
171 state->env_thread_state(this)->get_agent_thread_local_storage_data();
172 } else {
173 // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
174 // the thread to _thread_in_vm. However, when the TLS for a thread
175 // other than the current thread is required we need to transition
176 // from native so as to resolve the jthread.
177
178 ThreadInVMfromNative __tiv(current_thread);
179 VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
180 debug_only(VMNativeEntryWrapper __vew;)
181
182 JavaThread* java_thread = NULL;
183 ThreadsListHandle tlh(current_thread);
184 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
185 if (err != JVMTI_ERROR_NONE) {
186 return err;
187 }
188
189 JvmtiThreadState* state = java_thread->jvmti_thread_state();
190 *data_ptr = (state == NULL) ? NULL :
191 state->env_thread_state(this)->get_agent_thread_local_storage_data();
192 }
193 return JVMTI_ERROR_NONE;
194 } /* end GetThreadLocalStorage */
195
196 //
197 // Module functions
198 //
199
200 // module_count_ptr - pre-checked for NULL
201 // modules_ptr - pre-checked for NULL
202 jvmtiError
203 JvmtiEnv::GetAllModules(jint* module_count_ptr, jobject** modules_ptr) {
204 JvmtiModuleClosure jmc;
205
206 return jmc.get_all_modules(this, module_count_ptr, modules_ptr);
207 } /* end GetAllModules */
208
209
210 // class_loader - NULL is a valid value, must be pre-checked
211 // package_name - pre-checked for NULL
212 // module_ptr - pre-checked for NULL
213 jvmtiError
214 JvmtiEnv::GetNamedModule(jobject class_loader, const char* package_name, jobject* module_ptr) {
215 JavaThread* THREAD = JavaThread::current(); // pass to macros
216 ResourceMark rm(THREAD);
217
218 Handle h_loader (THREAD, JNIHandles::resolve(class_loader));
219 // Check that loader is a subclass of java.lang.ClassLoader.
220 if (h_loader.not_null() && !java_lang_ClassLoader::is_subclass(h_loader->klass())) {
221 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
222 }
223 jobject module = Modules::get_named_module(h_loader, package_name, THREAD);
224 if (HAS_PENDING_EXCEPTION) {
225 CLEAR_PENDING_EXCEPTION;
226 return JVMTI_ERROR_INTERNAL; // unexpected exception
227 }
228 *module_ptr = module;
229 return JVMTI_ERROR_NONE;
230 } /* end GetNamedModule */
231
232
233 // module - pre-checked for NULL
234 // to_module - pre-checked for NULL
235 jvmtiError
236 JvmtiEnv::AddModuleReads(jobject module, jobject to_module) {
237 JavaThread* THREAD = JavaThread::current();
238
239 // check module
240 Handle h_module(THREAD, JNIHandles::resolve(module));
241 if (!java_lang_Module::is_instance(h_module())) {
242 return JVMTI_ERROR_INVALID_MODULE;
243 }
244 // check to_module
245 Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
246 if (!java_lang_Module::is_instance(h_to_module())) {
247 return JVMTI_ERROR_INVALID_MODULE;
248 }
249 return JvmtiExport::add_module_reads(h_module, h_to_module, THREAD);
250 } /* end AddModuleReads */
251
252
253 // module - pre-checked for NULL
254 // pkg_name - pre-checked for NULL
255 // to_module - pre-checked for NULL
256 jvmtiError
257 JvmtiEnv::AddModuleExports(jobject module, const char* pkg_name, jobject to_module) {
258 JavaThread* THREAD = JavaThread::current();
259 Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
260
261 // check module
262 Handle h_module(THREAD, JNIHandles::resolve(module));
263 if (!java_lang_Module::is_instance(h_module())) {
264 return JVMTI_ERROR_INVALID_MODULE;
265 }
266 // check to_module
267 Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
268 if (!java_lang_Module::is_instance(h_to_module())) {
269 return JVMTI_ERROR_INVALID_MODULE;
270 }
271 return JvmtiExport::add_module_exports(h_module, h_pkg, h_to_module, THREAD);
272 } /* end AddModuleExports */
273
274
275 // module - pre-checked for NULL
276 // pkg_name - pre-checked for NULL
277 // to_module - pre-checked for NULL
278 jvmtiError
279 JvmtiEnv::AddModuleOpens(jobject module, const char* pkg_name, jobject to_module) {
280 JavaThread* THREAD = JavaThread::current();
281 Handle h_pkg = java_lang_String::create_from_str(pkg_name, THREAD);
282
283 // check module
284 Handle h_module(THREAD, JNIHandles::resolve(module));
285 if (!java_lang_Module::is_instance(h_module())) {
286 return JVMTI_ERROR_INVALID_MODULE;
287 }
288 // check to_module
289 Handle h_to_module(THREAD, JNIHandles::resolve(to_module));
290 if (!java_lang_Module::is_instance(h_to_module())) {
291 return JVMTI_ERROR_INVALID_MODULE;
292 }
293 return JvmtiExport::add_module_opens(h_module, h_pkg, h_to_module, THREAD);
294 } /* end AddModuleOpens */
295
296
297 // module - pre-checked for NULL
298 // service - pre-checked for NULL
299 jvmtiError
300 JvmtiEnv::AddModuleUses(jobject module, jclass service) {
301 JavaThread* THREAD = JavaThread::current();
302
303 // check module
304 Handle h_module(THREAD, JNIHandles::resolve(module));
305 if (!java_lang_Module::is_instance(h_module())) {
306 return JVMTI_ERROR_INVALID_MODULE;
307 }
308 // check service
309 Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
310 if (!java_lang_Class::is_instance(h_service()) ||
311 java_lang_Class::is_primitive(h_service())) {
312 return JVMTI_ERROR_INVALID_CLASS;
313 }
314 return JvmtiExport::add_module_uses(h_module, h_service, THREAD);
315 } /* end AddModuleUses */
316
317
318 // module - pre-checked for NULL
319 // service - pre-checked for NULL
320 // impl_class - pre-checked for NULL
321 jvmtiError
322 JvmtiEnv::AddModuleProvides(jobject module, jclass service, jclass impl_class) {
323 JavaThread* THREAD = JavaThread::current();
324
325 // check module
326 Handle h_module(THREAD, JNIHandles::resolve(module));
327 if (!java_lang_Module::is_instance(h_module())) {
328 return JVMTI_ERROR_INVALID_MODULE;
329 }
330 // check service
331 Handle h_service(THREAD, JNIHandles::resolve_external_guard(service));
332 if (!java_lang_Class::is_instance(h_service()) ||
333 java_lang_Class::is_primitive(h_service())) {
334 return JVMTI_ERROR_INVALID_CLASS;
335 }
336 // check impl_class
337 Handle h_impl_class(THREAD, JNIHandles::resolve_external_guard(impl_class));
338 if (!java_lang_Class::is_instance(h_impl_class()) ||
339 java_lang_Class::is_primitive(h_impl_class())) {
340 return JVMTI_ERROR_INVALID_CLASS;
341 }
342 return JvmtiExport::add_module_provides(h_module, h_service, h_impl_class, THREAD);
343 } /* end AddModuleProvides */
344
345 // module - pre-checked for NULL
346 // is_modifiable_class_ptr - pre-checked for NULL
347 jvmtiError
348 JvmtiEnv::IsModifiableModule(jobject module, jboolean* is_modifiable_module_ptr) {
349 JavaThread* THREAD = JavaThread::current();
350
351 // check module
352 Handle h_module(THREAD, JNIHandles::resolve(module));
353 if (!java_lang_Module::is_instance(h_module())) {
354 return JVMTI_ERROR_INVALID_MODULE;
355 }
356
357 *is_modifiable_module_ptr = JNI_TRUE;
358 return JVMTI_ERROR_NONE;
359 } /* end IsModifiableModule */
360
361
362 //
363 // Class functions
364 //
365
366 // class_count_ptr - pre-checked for NULL
367 // classes_ptr - pre-checked for NULL
368 jvmtiError
369 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
370 return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
371 } /* end GetLoadedClasses */
372
373
374 // initiating_loader - NULL is a valid value, must be checked
375 // class_count_ptr - pre-checked for NULL
376 // classes_ptr - pre-checked for NULL
377 jvmtiError
378 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
379 return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
380 class_count_ptr, classes_ptr);
381 } /* end GetClassLoaderClasses */
382
383 // k_mirror - may be primitive, this must be checked
384 // is_modifiable_class_ptr - pre-checked for NULL
385 jvmtiError
386 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
387 *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
388 JNI_TRUE : JNI_FALSE;
389 return JVMTI_ERROR_NONE;
390 } /* end IsModifiableClass */
391
392 // class_count - pre-checked to be greater than or equal to 0
393 // classes - pre-checked for NULL
394 jvmtiError
395 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
396 //TODO: add locking
397
398 int index;
399 JavaThread* current_thread = JavaThread::current();
400 ResourceMark rm(current_thread);
401
402 jvmtiClassDefinition* class_definitions =
403 NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
404 NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
405
406 for (index = 0; index < class_count; index++) {
407 HandleMark hm(current_thread);
408
409 jclass jcls = classes[index];
410 oop k_mirror = JNIHandles::resolve_external_guard(jcls);
411 if (k_mirror == NULL) {
412 return JVMTI_ERROR_INVALID_CLASS;
413 }
414 if (!k_mirror->is_a(SystemDictionary::Class_klass())) {
415 return JVMTI_ERROR_INVALID_CLASS;
416 }
417
418 if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
419 return JVMTI_ERROR_UNMODIFIABLE_CLASS;
420 }
421
422 Klass* klass = java_lang_Class::as_Klass(k_mirror);
423
424 jint status = klass->jvmti_class_status();
425 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
426 return JVMTI_ERROR_INVALID_CLASS;
427 }
428
429 InstanceKlass* ik = InstanceKlass::cast(klass);
430 if (ik->get_cached_class_file_bytes() == NULL) {
431 // Not cached, we need to reconstitute the class file from the
432 // VM representation. We don't attach the reconstituted class
433 // bytes to the InstanceKlass here because they have not been
434 // validated and we're not at a safepoint.
435 JvmtiClassFileReconstituter reconstituter(ik);
436 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
437 return reconstituter.get_error();
438 }
439
440 class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
441 class_definitions[index].class_bytes = (unsigned char*)
442 reconstituter.class_file_bytes();
443 } else {
444 // it is cached, get it from the cache
445 class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
446 class_definitions[index].class_bytes = ik->get_cached_class_file_bytes();
447 }
448 class_definitions[index].klass = jcls;
449 }
450 EventRetransformClasses event;
451 VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
452 VMThread::execute(&op);
453 jvmtiError error = op.check_error();
454 if (error == JVMTI_ERROR_NONE) {
455 event.set_classCount(class_count);
456 event.set_redefinitionId(op.id());
457 event.commit();
458 }
459 return error;
460 } /* end RetransformClasses */
461
462
463 // class_count - pre-checked to be greater than or equal to 0
464 // class_definitions - pre-checked for NULL
465 jvmtiError
466 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
467 //TODO: add locking
468 EventRedefineClasses event;
469 VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
470 VMThread::execute(&op);
471 jvmtiError error = op.check_error();
472 if (error == JVMTI_ERROR_NONE) {
473 event.set_classCount(class_count);
474 event.set_redefinitionId(op.id());
475 event.commit();
476 }
477 return error;
478 } /* end RedefineClasses */
479
480
481 //
482 // Object functions
483 //
484
485 // size_ptr - pre-checked for NULL
486 jvmtiError
487 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
488 oop mirror = JNIHandles::resolve_external_guard(object);
489 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
490 *size_ptr = (jlong)Universe::heap()->obj_size(mirror) * wordSize;
491 return JVMTI_ERROR_NONE;
492 } /* end GetObjectSize */
493
494 //
495 // Method functions
496 //
497
498 // prefix - NULL is a valid value, must be checked
499 jvmtiError
500 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
501 return prefix == NULL?
502 SetNativeMethodPrefixes(0, NULL) :
503 SetNativeMethodPrefixes(1, (char**)&prefix);
504 } /* end SetNativeMethodPrefix */
505
506
507 // prefix_count - pre-checked to be greater than or equal to 0
508 // prefixes - pre-checked for NULL
509 jvmtiError
510 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
511 // Have to grab JVMTI thread state lock to be sure that some thread
512 // isn't accessing the prefixes at the same time we are setting them.
513 // No locks during VM bring-up.
514 if (Threads::number_of_threads() == 0) {
515 return set_native_method_prefixes(prefix_count, prefixes);
516 } else {
517 MutexLocker mu(JvmtiThreadState_lock);
518 return set_native_method_prefixes(prefix_count, prefixes);
519 }
520 } /* end SetNativeMethodPrefixes */
521
522 //
523 // Event Management functions
524 //
525
526 // callbacks - NULL is a valid value, must be checked
527 // size_of_callbacks - pre-checked to be greater than or equal to 0
528 jvmtiError
529 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
530 JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
531 return JVMTI_ERROR_NONE;
532 } /* end SetEventCallbacks */
533
534
535 // event_thread - NULL is a valid value, must be checked
536 jvmtiError
537 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread, ...) {
538 if (event_thread == NULL) {
539 // Can be called at Agent_OnLoad() time with event_thread == NULL
540 // when Thread::current() does not work yet so we cannot create a
541 // ThreadsListHandle that is common to both thread-specific and
542 // global code paths.
543
544 // event_type must be valid
545 if (!JvmtiEventController::is_valid_event_type(event_type)) {
546 return JVMTI_ERROR_INVALID_EVENT_TYPE;
547 }
548
549 bool enabled = (mode == JVMTI_ENABLE);
550
551 // assure that needed capabilities are present
552 if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
553 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
554 }
555
556 if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
557 record_class_file_load_hook_enabled();
558 }
559
560 JvmtiEventController::set_user_enabled(this, (JavaThread*) NULL, event_type, enabled);
561 } else {
562 // We have a specified event_thread.
563 JavaThread* java_thread = NULL;
564 ThreadsListHandle tlh;
565 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), event_thread, &java_thread, NULL);
566 if (err != JVMTI_ERROR_NONE) {
567 return err;
568 }
569
570 // event_type must be valid
571 if (!JvmtiEventController::is_valid_event_type(event_type)) {
572 return JVMTI_ERROR_INVALID_EVENT_TYPE;
573 }
574
575 // global events cannot be controlled at thread level.
576 if (JvmtiEventController::is_global_event(event_type)) {
577 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
578 }
579
580 bool enabled = (mode == JVMTI_ENABLE);
581
582 // assure that needed capabilities are present
583 if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
584 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
585 }
586
587 if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
588 record_class_file_load_hook_enabled();
589 }
590 JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
591 }
592
593 return JVMTI_ERROR_NONE;
594 } /* end SetEventNotificationMode */
595
596 //
597 // Capability functions
598 //
599
600 // capabilities_ptr - pre-checked for NULL
601 jvmtiError
602 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
603 JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
604 get_prohibited_capabilities(),
605 capabilities_ptr);
606 return JVMTI_ERROR_NONE;
607 } /* end GetPotentialCapabilities */
608
609
610 // capabilities_ptr - pre-checked for NULL
611 jvmtiError
612 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
613 return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
614 get_prohibited_capabilities(),
615 capabilities_ptr,
616 get_capabilities());
617 } /* end AddCapabilities */
618
619
620 // capabilities_ptr - pre-checked for NULL
621 jvmtiError
622 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
623 JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
624 return JVMTI_ERROR_NONE;
625 } /* end RelinquishCapabilities */
626
627
628 // capabilities_ptr - pre-checked for NULL
629 jvmtiError
630 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
631 JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
632 return JVMTI_ERROR_NONE;
633 } /* end GetCapabilities */
634
635 //
636 // Class Loader Search functions
637 //
638
639 // segment - pre-checked for NULL
640 jvmtiError
641 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
642 jvmtiPhase phase = get_phase();
643 if (phase == JVMTI_PHASE_ONLOAD) {
644 Arguments::append_sysclasspath(segment);
645 return JVMTI_ERROR_NONE;
646 } else if (use_version_1_0_semantics()) {
647 // This JvmtiEnv requested version 1.0 semantics and this function
648 // is only allowed in the ONLOAD phase in version 1.0 so we need to
649 // return an error here.
650 return JVMTI_ERROR_WRONG_PHASE;
651 } else if (phase == JVMTI_PHASE_LIVE) {
652 // The phase is checked by the wrapper that called this function,
653 // but this thread could be racing with the thread that is
654 // terminating the VM so we check one more time.
655
656 // create the zip entry
657 ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, true);
658 if (zip_entry == NULL) {
659 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
660 }
661
662 // lock the loader
663 Thread* thread = Thread::current();
664 HandleMark hm;
665 Handle loader_lock = Handle(thread, SystemDictionary::system_loader_lock());
666
667 ObjectLocker ol(loader_lock, thread);
668
669 // add the jar file to the bootclasspath
670 log_info(class, load)("opened: %s", zip_entry->name());
671 #if INCLUDE_CDS
672 ClassLoaderExt::append_boot_classpath(zip_entry);
673 #else
674 ClassLoader::add_to_boot_append_entries(zip_entry);
675 #endif
676 return JVMTI_ERROR_NONE;
677 } else {
678 return JVMTI_ERROR_WRONG_PHASE;
679 }
680
681 } /* end AddToBootstrapClassLoaderSearch */
682
683
684 // segment - pre-checked for NULL
685 jvmtiError
686 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
687 jvmtiPhase phase = get_phase();
688
689 if (phase == JVMTI_PHASE_ONLOAD) {
690 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
691 if (strcmp("java.class.path", p->key()) == 0) {
692 p->append_value(segment);
693 break;
694 }
695 }
696 return JVMTI_ERROR_NONE;
697 } else if (phase == JVMTI_PHASE_LIVE) {
698 // The phase is checked by the wrapper that called this function,
699 // but this thread could be racing with the thread that is
700 // terminating the VM so we check one more time.
701 HandleMark hm;
702
703 // create the zip entry (which will open the zip file and hence
704 // check that the segment is indeed a zip file).
705 ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
706 if (zip_entry == NULL) {
707 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
708 }
709 delete zip_entry; // no longer needed
710
711 // lock the loader
712 Thread* THREAD = Thread::current();
713 Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
714
715 ObjectLocker ol(loader, THREAD);
716
717 // need the path as java.lang.String
718 Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
719 if (HAS_PENDING_EXCEPTION) {
720 CLEAR_PENDING_EXCEPTION;
721 return JVMTI_ERROR_INTERNAL;
722 }
723
724 // Invoke the appendToClassPathForInstrumentation method - if the method
725 // is not found it means the loader doesn't support adding to the class path
726 // in the live phase.
727 {
728 JavaValue res(T_VOID);
729 JavaCalls::call_special(&res,
730 loader,
731 loader->klass(),
732 vmSymbols::appendToClassPathForInstrumentation_name(),
733 vmSymbols::appendToClassPathForInstrumentation_signature(),
734 path,
735 THREAD);
736 if (HAS_PENDING_EXCEPTION) {
737 Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
738 CLEAR_PENDING_EXCEPTION;
739
740 if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
741 return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
742 } else {
743 return JVMTI_ERROR_INTERNAL;
744 }
745 }
746 }
747
748 return JVMTI_ERROR_NONE;
749 } else {
750 return JVMTI_ERROR_WRONG_PHASE;
751 }
752 } /* end AddToSystemClassLoaderSearch */
753
754 //
755 // General functions
756 //
757
758 // phase_ptr - pre-checked for NULL
759 jvmtiError
760 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
761 *phase_ptr = phase();
762 return JVMTI_ERROR_NONE;
763 } /* end GetPhase */
764
765
766 jvmtiError
767 JvmtiEnv::DisposeEnvironment() {
768 dispose();
769 return JVMTI_ERROR_NONE;
770 } /* end DisposeEnvironment */
771
772
773 // data - NULL is a valid value, must be checked
774 jvmtiError
775 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
776 set_env_local_storage(data);
777 return JVMTI_ERROR_NONE;
778 } /* end SetEnvironmentLocalStorage */
779
780
781 // data_ptr - pre-checked for NULL
782 jvmtiError
783 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
784 *data_ptr = (void*)get_env_local_storage();
785 return JVMTI_ERROR_NONE;
786 } /* end GetEnvironmentLocalStorage */
787
788 // version_ptr - pre-checked for NULL
789 jvmtiError
790 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
791 *version_ptr = JVMTI_VERSION;
792 return JVMTI_ERROR_NONE;
793 } /* end GetVersionNumber */
794
795
796 // name_ptr - pre-checked for NULL
797 jvmtiError
798 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
799 if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
800 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
801 }
802 const char *name = JvmtiUtil::error_name(error);
803 if (name == NULL) {
804 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
805 }
806 size_t len = strlen(name) + 1;
807 jvmtiError err = allocate(len, (unsigned char**)name_ptr);
808 if (err == JVMTI_ERROR_NONE) {
809 memcpy(*name_ptr, name, len);
810 }
811 return err;
812 } /* end GetErrorName */
813
814
815 jvmtiError
816 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
817 LogLevelType level = value == 0 ? LogLevel::Off : LogLevel::Info;
818 switch (flag) {
819 case JVMTI_VERBOSE_OTHER:
820 // ignore
821 break;
822 case JVMTI_VERBOSE_CLASS:
823 LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
824 LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
825 break;
826 case JVMTI_VERBOSE_GC:
827 LogConfiguration::configure_stdout(level, true, LOG_TAGS(gc));
828 break;
829 case JVMTI_VERBOSE_JNI:
830 level = value == 0 ? LogLevel::Off : LogLevel::Debug;
831 LogConfiguration::configure_stdout(level, true, LOG_TAGS(jni, resolve));
832 break;
833 default:
834 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
835 };
836 return JVMTI_ERROR_NONE;
837 } /* end SetVerboseFlag */
838
839
840 // format_ptr - pre-checked for NULL
841 jvmtiError
842 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
843 *format_ptr = JVMTI_JLOCATION_JVMBCI;
844 return JVMTI_ERROR_NONE;
845 } /* end GetJLocationFormat */
846
847 //
848 // Thread functions
849 //
850
851 // Threads_lock NOT held
852 // thread - NOT pre-checked
853 // thread_state_ptr - pre-checked for NULL
854 jvmtiError
855 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
856 JavaThread* current_thread = JavaThread::current();
857 JavaThread* java_thread = NULL;
858 oop thread_oop = NULL;
859 ThreadsListHandle tlh(current_thread);
860
861 if (thread == NULL) {
862 java_thread = current_thread;
863 thread_oop = java_thread->threadObj();
864
865 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
866 return JVMTI_ERROR_INVALID_THREAD;
867 }
868 } else {
869 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
870 if (err != JVMTI_ERROR_NONE) {
871 // We got an error code so we don't have a JavaThread *, but
872 // only return an error from here if we didn't get a valid
873 // thread_oop.
874 if (thread_oop == NULL) {
875 return err;
876 }
877 // We have a valid thread_oop so we can return some thread state.
878 }
879 }
880
881 // get most state bits
882 jint state = (jint)java_lang_Thread::get_thread_status(thread_oop);
883
884 if (java_thread != NULL) {
885 // We have a JavaThread* so add more state bits.
886 JavaThreadState jts = java_thread->thread_state();
887
888 if (java_thread->is_being_ext_suspended()) {
889 state |= JVMTI_THREAD_STATE_SUSPENDED;
890 }
891 if (jts == _thread_in_native) {
892 state |= JVMTI_THREAD_STATE_IN_NATIVE;
893 }
894 if (java_thread->is_interrupted(false)) {
895 state |= JVMTI_THREAD_STATE_INTERRUPTED;
896 }
897 }
898
899 *thread_state_ptr = state;
900 return JVMTI_ERROR_NONE;
901 } /* end GetThreadState */
902
903
904 // thread_ptr - pre-checked for NULL
905 jvmtiError
906 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
907 JavaThread* current_thread = JavaThread::current();
908 *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
909 return JVMTI_ERROR_NONE;
910 } /* end GetCurrentThread */
911
912
913 // threads_count_ptr - pre-checked for NULL
914 // threads_ptr - pre-checked for NULL
915 jvmtiError
916 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
917 int nthreads = 0;
918 Handle *thread_objs = NULL;
919 ResourceMark rm;
920 HandleMark hm;
921
922 // enumerate threads (including agent threads)
923 ThreadsListEnumerator tle(Thread::current(), true);
924 nthreads = tle.num_threads();
925 *threads_count_ptr = nthreads;
926
927 if (nthreads == 0) {
928 *threads_ptr = NULL;
929 return JVMTI_ERROR_NONE;
930 }
931
932 thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
933 NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
934
935 for (int i = 0; i < nthreads; i++) {
936 thread_objs[i] = Handle(tle.get_threadObj(i));
937 }
938
939 jthread *jthreads = new_jthreadArray(nthreads, thread_objs);
940 NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
941
942 *threads_ptr = jthreads;
943 return JVMTI_ERROR_NONE;
944 } /* end GetAllThreads */
945
946
947 // Threads_lock NOT held, java_thread not protected by lock
948 // java_thread - pre-checked
949 jvmtiError
950 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
951 // don't allow hidden thread suspend request.
952 if (java_thread->is_hidden_from_external_view()) {
953 return (JVMTI_ERROR_NONE);
954 }
955
956 {
957 MutexLocker ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
958 if (java_thread->is_external_suspend()) {
959 // don't allow nested external suspend requests.
960 return (JVMTI_ERROR_THREAD_SUSPENDED);
961 }
962 if (java_thread->is_exiting()) { // thread is in the process of exiting
963 return (JVMTI_ERROR_THREAD_NOT_ALIVE);
964 }
965 java_thread->set_external_suspend();
966 }
967
968 if (!JvmtiSuspendControl::suspend(java_thread)) {
969 // the thread was in the process of exiting
970 return (JVMTI_ERROR_THREAD_NOT_ALIVE);
971 }
972 return JVMTI_ERROR_NONE;
973 } /* end SuspendThread */
974
975
976 // request_count - pre-checked to be greater than or equal to 0
977 // request_list - pre-checked for NULL
978 // results - pre-checked for NULL
979 jvmtiError
980 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
981 int needSafepoint = 0; // > 0 if we need a safepoint
982 ThreadsListHandle tlh;
983 for (int i = 0; i < request_count; i++) {
984 JavaThread *java_thread = NULL;
985 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
986 if (err != JVMTI_ERROR_NONE) {
987 results[i] = err;
988 continue;
989 }
990 // don't allow hidden thread suspend request.
991 if (java_thread->is_hidden_from_external_view()) {
992 results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
993 continue;
994 }
995
996 {
997 MutexLocker ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
998 if (java_thread->is_external_suspend()) {
999 // don't allow nested external suspend requests.
1000 results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
1001 continue;
1002 }
1003 if (java_thread->is_exiting()) { // thread is in the process of exiting
1004 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
1005 continue;
1006 }
1007 java_thread->set_external_suspend();
1008 }
1009 if (java_thread->thread_state() == _thread_in_native) {
1010 // We need to try and suspend native threads here. Threads in
1011 // other states will self-suspend on their next transition.
1012 if (!JvmtiSuspendControl::suspend(java_thread)) {
1013 // The thread was in the process of exiting. Force another
1014 // safepoint to make sure that this thread transitions.
1015 needSafepoint++;
1016 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
1017 continue;
1018 }
1019 } else {
1020 needSafepoint++;
1021 }
1022 results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
1023 }
1024 if (needSafepoint > 0) {
1025 VM_ThreadsSuspendJVMTI tsj;
1026 VMThread::execute(&tsj);
1027 }
1028 // per-thread suspend results returned via results parameter
1029 return JVMTI_ERROR_NONE;
1030 } /* end SuspendThreadList */
1031
1032
1033 // Threads_lock NOT held, java_thread not protected by lock
1034 // java_thread - pre-checked
1035 jvmtiError
1036 JvmtiEnv::ResumeThread(JavaThread* java_thread) {
1037 // don't allow hidden thread resume request.
1038 if (java_thread->is_hidden_from_external_view()) {
1039 return JVMTI_ERROR_NONE;
1040 }
1041
1042 if (!java_thread->is_being_ext_suspended()) {
1043 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1044 }
1045
1046 if (!JvmtiSuspendControl::resume(java_thread)) {
1047 return JVMTI_ERROR_INTERNAL;
1048 }
1049 return JVMTI_ERROR_NONE;
1050 } /* end ResumeThread */
1051
1052
1053 // request_count - pre-checked to be greater than or equal to 0
1054 // request_list - pre-checked for NULL
1055 // results - pre-checked for NULL
1056 jvmtiError
1057 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
1058 ThreadsListHandle tlh;
1059 for (int i = 0; i < request_count; i++) {
1060 JavaThread* java_thread = NULL;
1061 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), request_list[i], &java_thread, NULL);
1062 if (err != JVMTI_ERROR_NONE) {
1063 results[i] = err;
1064 continue;
1065 }
1066 // don't allow hidden thread resume request.
1067 if (java_thread->is_hidden_from_external_view()) {
1068 results[i] = JVMTI_ERROR_NONE; // indicate successful resume
1069 continue;
1070 }
1071 if (!java_thread->is_being_ext_suspended()) {
1072 results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1073 continue;
1074 }
1075
1076 if (!JvmtiSuspendControl::resume(java_thread)) {
1077 results[i] = JVMTI_ERROR_INTERNAL;
1078 continue;
1079 }
1080
1081 results[i] = JVMTI_ERROR_NONE; // indicate successful resume
1082 }
1083 // per-thread resume results returned via results parameter
1084 return JVMTI_ERROR_NONE;
1085 } /* end ResumeThreadList */
1086
1087
1088 // Threads_lock NOT held, java_thread not protected by lock
1089 // java_thread - pre-checked
1090 jvmtiError
1091 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
1092 oop e = JNIHandles::resolve_external_guard(exception);
1093 NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
1094
1095 JavaThread::send_async_exception(java_thread->threadObj(), e);
1096
1097 return JVMTI_ERROR_NONE;
1098
1099 } /* end StopThread */
1100
1101
1102 // Threads_lock NOT held
1103 // thread - NOT pre-checked
1104 jvmtiError
1105 JvmtiEnv::InterruptThread(jthread thread) {
1106 JavaThread* current_thread = JavaThread::current();
1107 JavaThread* java_thread = NULL;
1108 ThreadsListHandle tlh(current_thread);
1109 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
1110 if (err != JVMTI_ERROR_NONE) {
1111 return err;
1112 }
1113 // Really this should be a Java call to Thread.interrupt to ensure the same
1114 // semantics, however historically this has not been done for some reason.
1115 // So we continue with that (which means we don't interact with any Java-level
1116 // Interruptible object) but we must set the Java-level interrupted state.
1117 java_lang_Thread::set_interrupted(JNIHandles::resolve(thread), true);
1118 java_thread->interrupt();
1119
1120 return JVMTI_ERROR_NONE;
1121 } /* end InterruptThread */
1122
1123
1124 // Threads_lock NOT held
1125 // thread - NOT pre-checked
1126 // info_ptr - pre-checked for NULL
1127 jvmtiError
1128 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
1129 ResourceMark rm;
1130 HandleMark hm;
1131
1132 JavaThread* current_thread = JavaThread::current();
1133 ThreadsListHandle tlh(current_thread);
1134
1135 // if thread is NULL the current thread is used
1136 oop thread_oop = NULL;
1137 if (thread == NULL) {
1138 thread_oop = current_thread->threadObj();
1139 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
1140 return JVMTI_ERROR_INVALID_THREAD;
1141 }
1142 } else {
1143 JavaThread* java_thread = NULL;
1144 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1145 if (err != JVMTI_ERROR_NONE) {
1146 // We got an error code so we don't have a JavaThread *, but
1147 // only return an error from here if we didn't get a valid
1148 // thread_oop.
1149 if (thread_oop == NULL) {
1150 return err;
1151 }
1152 // We have a valid thread_oop so we can return some thread info.
1153 }
1154 }
1155
1156 Handle thread_obj(current_thread, thread_oop);
1157 Handle name;
1158 ThreadPriority priority;
1159 Handle thread_group;
1160 Handle context_class_loader;
1161 bool is_daemon;
1162
1163 name = Handle(current_thread, java_lang_Thread::name(thread_obj()));
1164 priority = java_lang_Thread::priority(thread_obj());
1165 thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
1166 is_daemon = java_lang_Thread::is_daemon(thread_obj());
1167
1168 oop loader = java_lang_Thread::context_class_loader(thread_obj());
1169 context_class_loader = Handle(current_thread, loader);
1170
1171 { const char *n;
1172
1173 if (name() != NULL) {
1174 n = java_lang_String::as_utf8_string(name());
1175 } else {
1176 int utf8_length = 0;
1177 n = UNICODE::as_utf8((jchar*) NULL, utf8_length);
1178 }
1179
1180 info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
1181 if (info_ptr->name == NULL)
1182 return JVMTI_ERROR_OUT_OF_MEMORY;
1183
1184 strcpy(info_ptr->name, n);
1185 }
1186 info_ptr->is_daemon = is_daemon;
1187 info_ptr->priority = priority;
1188
1189 info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
1190 jni_reference(context_class_loader);
1191 info_ptr->thread_group = jni_reference(thread_group);
1192
1193 return JVMTI_ERROR_NONE;
1194 } /* end GetThreadInfo */
1195
1196
1197 // Threads_lock NOT held, java_thread not protected by lock
1198 // java_thread - pre-checked
1199 // owned_monitor_count_ptr - pre-checked for NULL
1200 // owned_monitors_ptr - pre-checked for NULL
1201 jvmtiError
1202 JvmtiEnv::GetOwnedMonitorInfo(JavaThread* java_thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
1203 jvmtiError err = JVMTI_ERROR_NONE;
1204 JavaThread* calling_thread = JavaThread::current();
1205
1206 // growable array of jvmti monitors info on the C-heap
1207 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1208 new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1209
1210 // It is only safe to perform the direct operation on the current
1211 // thread. All other usage needs to use a vm-safepoint-op for safety.
1212 if (java_thread == calling_thread) {
1213 err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1214 } else {
1215 // JVMTI get monitors info at safepoint. Do not require target thread to
1216 // be suspended.
1217 VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1218 VMThread::execute(&op);
1219 err = op.result();
1220 }
1221 jint owned_monitor_count = owned_monitors_list->length();
1222 if (err == JVMTI_ERROR_NONE) {
1223 if ((err = allocate(owned_monitor_count * sizeof(jobject *),
1224 (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
1225 // copy into the returned array
1226 for (int i = 0; i < owned_monitor_count; i++) {
1227 (*owned_monitors_ptr)[i] =
1228 ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1229 }
1230 *owned_monitor_count_ptr = owned_monitor_count;
1231 }
1232 }
1233 // clean up.
1234 for (int i = 0; i < owned_monitor_count; i++) {
1235 deallocate((unsigned char*)owned_monitors_list->at(i));
1236 }
1237 delete owned_monitors_list;
1238
1239 return err;
1240 } /* end GetOwnedMonitorInfo */
1241
1242
1243 // Threads_lock NOT held, java_thread not protected by lock
1244 // java_thread - pre-checked
1245 // monitor_info_count_ptr - pre-checked for NULL
1246 // monitor_info_ptr - pre-checked for NULL
1247 jvmtiError
1248 JvmtiEnv::GetOwnedMonitorStackDepthInfo(JavaThread* java_thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1249 jvmtiError err = JVMTI_ERROR_NONE;
1250 JavaThread* calling_thread = JavaThread::current();
1251
1252 // growable array of jvmti monitors info on the C-heap
1253 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1254 new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1255
1256 // It is only safe to perform the direct operation on the current
1257 // thread. All other usage needs to use a vm-safepoint-op for safety.
1258 if (java_thread == calling_thread) {
1259 err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1260 } else {
1261 // JVMTI get owned monitors info at safepoint. Do not require target thread to
1262 // be suspended.
1263 VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1264 VMThread::execute(&op);
1265 err = op.result();
1266 }
1267
1268 jint owned_monitor_count = owned_monitors_list->length();
1269 if (err == JVMTI_ERROR_NONE) {
1270 if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1271 (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1272 // copy to output array.
1273 for (int i = 0; i < owned_monitor_count; i++) {
1274 (*monitor_info_ptr)[i].monitor =
1275 ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1276 (*monitor_info_ptr)[i].stack_depth =
1277 ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1278 }
1279 }
1280 *monitor_info_count_ptr = owned_monitor_count;
1281 }
1282
1283 // clean up.
1284 for (int i = 0; i < owned_monitor_count; i++) {
1285 deallocate((unsigned char*)owned_monitors_list->at(i));
1286 }
1287 delete owned_monitors_list;
1288
1289 return err;
1290 } /* end GetOwnedMonitorStackDepthInfo */
1291
1292
1293 // Threads_lock NOT held, java_thread not protected by lock
1294 // java_thread - pre-checked
1295 // monitor_ptr - pre-checked for NULL
1296 jvmtiError
1297 JvmtiEnv::GetCurrentContendedMonitor(JavaThread* java_thread, jobject* monitor_ptr) {
1298 jvmtiError err = JVMTI_ERROR_NONE;
1299 JavaThread* calling_thread = JavaThread::current();
1300
1301 // It is only safe to perform the direct operation on the current
1302 // thread. All other usage needs to use a vm-safepoint-op for safety.
1303 if (java_thread == calling_thread) {
1304 err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1305 } else {
1306 // get contended monitor information at safepoint.
1307 VM_GetCurrentContendedMonitor op(this, calling_thread, java_thread, monitor_ptr);
1308 VMThread::execute(&op);
1309 err = op.result();
1310 }
1311 return err;
1312 } /* end GetCurrentContendedMonitor */
1313
1314
1315 // Threads_lock NOT held
1316 // thread - NOT pre-checked
1317 // proc - pre-checked for NULL
1318 // arg - NULL is a valid value, must be checked
1319 jvmtiError
1320 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1321 JavaThread* current_thread = JavaThread::current();
1322
1323 JavaThread* java_thread = NULL;
1324 oop thread_oop = NULL;
1325 ThreadsListHandle tlh(current_thread);
1326 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, &thread_oop);
1327 if (err != JVMTI_ERROR_NONE) {
1328 // We got an error code so we don't have a JavaThread *, but
1329 // only return an error from here if we didn't get a valid
1330 // thread_oop.
1331 if (thread_oop == NULL) {
1332 return err;
1333 }
1334 // We have a valid thread_oop.
1335 }
1336
1337 if (java_thread != NULL) {
1338 // 'thread' refers to an existing JavaThread.
1339 return JVMTI_ERROR_INVALID_THREAD;
1340 }
1341
1342 if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1343 return JVMTI_ERROR_INVALID_PRIORITY;
1344 }
1345
1346 Handle thread_hndl(current_thread, thread_oop);
1347 {
1348 MutexLocker mu(current_thread, Threads_lock); // grab Threads_lock
1349
1350 JvmtiAgentThread *new_thread = new JvmtiAgentThread(this, proc, arg);
1351
1352 // At this point it may be possible that no osthread was created for the
1353 // JavaThread due to lack of memory.
1354 if (new_thread == NULL || new_thread->osthread() == NULL) {
1355 if (new_thread != NULL) {
1356 new_thread->smr_delete();
1357 }
1358 return JVMTI_ERROR_OUT_OF_MEMORY;
1359 }
1360
1361 java_lang_Thread::set_thread(thread_hndl(), new_thread);
1362 java_lang_Thread::set_priority(thread_hndl(), (ThreadPriority)priority);
1363 java_lang_Thread::set_daemon(thread_hndl());
1364
1365 new_thread->set_threadObj(thread_hndl());
1366 Threads::add(new_thread);
1367 Thread::start(new_thread);
1368 } // unlock Threads_lock
1369
1370 return JVMTI_ERROR_NONE;
1371 } /* end RunAgentThread */
1372
1373 //
1374 // Thread Group functions
1375 //
1376
1377 // group_count_ptr - pre-checked for NULL
1378 // groups_ptr - pre-checked for NULL
1379 jvmtiError
1380 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1381 JavaThread* current_thread = JavaThread::current();
1382
1383 // Only one top level thread group now.
1384 *group_count_ptr = 1;
1385
1386 // Allocate memory to store global-refs to the thread groups.
1387 // Assume this area is freed by caller.
1388 *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1389
1390 NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1391
1392 // Convert oop to Handle, then convert Handle to global-ref.
1393 {
1394 HandleMark hm(current_thread);
1395 Handle system_thread_group(current_thread, Universe::system_thread_group());
1396 *groups_ptr[0] = jni_reference(system_thread_group);
1397 }
1398
1399 return JVMTI_ERROR_NONE;
1400 } /* end GetTopThreadGroups */
1401
1402
1403 // info_ptr - pre-checked for NULL
1404 jvmtiError
1405 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1406 ResourceMark rm;
1407 HandleMark hm;
1408
1409 JavaThread* current_thread = JavaThread::current();
1410
1411 Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1412 NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1413
1414 const char* name;
1415 Handle parent_group;
1416 bool is_daemon;
1417 ThreadPriority max_priority;
1418
1419 name = java_lang_ThreadGroup::name(group_obj());
1420 parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1421 is_daemon = java_lang_ThreadGroup::is_daemon(group_obj());
1422 max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1423
1424 info_ptr->is_daemon = is_daemon;
1425 info_ptr->max_priority = max_priority;
1426 info_ptr->parent = jni_reference(parent_group);
1427
1428 if (name != NULL) {
1429 info_ptr->name = (char*)jvmtiMalloc(strlen(name)+1);
1430 NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1431 strcpy(info_ptr->name, name);
1432 } else {
1433 info_ptr->name = NULL;
1434 }
1435
1436 return JVMTI_ERROR_NONE;
1437 } /* end GetThreadGroupInfo */
1438
1439
1440 // thread_count_ptr - pre-checked for NULL
1441 // threads_ptr - pre-checked for NULL
1442 // group_count_ptr - pre-checked for NULL
1443 // groups_ptr - pre-checked for NULL
1444 jvmtiError
1445 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1446 JavaThread* current_thread = JavaThread::current();
1447 oop group_obj = (oop) JNIHandles::resolve_external_guard(group);
1448 NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1449
1450 Handle *thread_objs = NULL;
1451 Handle *group_objs = NULL;
1452 int nthreads = 0;
1453 int ngroups = 0;
1454 int hidden_threads = 0;
1455
1456 ResourceMark rm(current_thread);
1457 HandleMark hm(current_thread);
1458
1459 Handle group_hdl(current_thread, group_obj);
1460
1461 { // Cannot allow thread or group counts to change.
1462 ObjectLocker ol(group_hdl, current_thread);
1463
1464 nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
1465 ngroups = java_lang_ThreadGroup::ngroups(group_hdl());
1466
1467 if (nthreads > 0) {
1468 ThreadsListHandle tlh(current_thread);
1469 objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
1470 assert(nthreads <= threads->length(), "too many threads");
1471 thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
1472 for (int i = 0, j = 0; i < nthreads; i++) {
1473 oop thread_obj = threads->obj_at(i);
1474 assert(thread_obj != NULL, "thread_obj is NULL");
1475 JavaThread *java_thread = NULL;
1476 jvmtiError err = JvmtiExport::cv_oop_to_JavaThread(tlh.list(), thread_obj, &java_thread);
1477 if (err == JVMTI_ERROR_NONE) {
1478 // Have a valid JavaThread*.
1479 if (java_thread->is_hidden_from_external_view()) {
1480 // Filter out hidden java threads.
1481 hidden_threads++;
1482 continue;
1483 }
1484 } else {
1485 // We couldn't convert thread_obj into a JavaThread*.
1486 if (err == JVMTI_ERROR_INVALID_THREAD) {
1487 // The thread_obj does not refer to a java.lang.Thread object
1488 // so skip it.
1489 hidden_threads++;
1490 continue;
1491 }
1492 // We have a valid thread_obj, but no JavaThread*; the caller
1493 // can still have limited use for the thread_obj.
1494 }
1495 thread_objs[j++] = Handle(current_thread, thread_obj);
1496 }
1497 nthreads -= hidden_threads;
1498 } // ThreadsListHandle is destroyed here.
1499
1500 if (ngroups > 0) {
1501 objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
1502 assert(ngroups <= groups->length(), "too many groups");
1503 group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
1504 for (int i = 0; i < ngroups; i++) {
1505 oop group_obj = groups->obj_at(i);
1506 assert(group_obj != NULL, "group_obj != NULL");
1507 group_objs[i] = Handle(current_thread, group_obj);
1508 }
1509 }
1510 } // ThreadGroup unlocked here
1511
1512 *group_count_ptr = ngroups;
1513 *thread_count_ptr = nthreads;
1514 *threads_ptr = new_jthreadArray(nthreads, thread_objs);
1515 *groups_ptr = new_jthreadGroupArray(ngroups, group_objs);
1516 if ((nthreads > 0) && (*threads_ptr == NULL)) {
1517 return JVMTI_ERROR_OUT_OF_MEMORY;
1518 }
1519 if ((ngroups > 0) && (*groups_ptr == NULL)) {
1520 return JVMTI_ERROR_OUT_OF_MEMORY;
1521 }
1522
1523 return JVMTI_ERROR_NONE;
1524 } /* end GetThreadGroupChildren */
1525
1526
1527 //
1528 // Stack Frame functions
1529 //
1530
1531 // Threads_lock NOT held, java_thread not protected by lock
1532 // java_thread - pre-checked
1533 // max_frame_count - pre-checked to be greater than or equal to 0
1534 // frame_buffer - pre-checked for NULL
1535 // count_ptr - pre-checked for NULL
1536 jvmtiError
1537 JvmtiEnv::GetStackTrace(JavaThread* java_thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1538 jvmtiError err = JVMTI_ERROR_NONE;
1539
1540 // It is only safe to perform the direct operation on the current
1541 // thread. All other usage needs to use a vm-safepoint-op for safety.
1542 if (java_thread == JavaThread::current()) {
1543 err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1544 } else {
1545 // JVMTI get stack trace at safepoint. Do not require target thread to
1546 // be suspended.
1547 VM_GetStackTrace op(this, java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1548 VMThread::execute(&op);
1549 err = op.result();
1550 }
1551
1552 return err;
1553 } /* end GetStackTrace */
1554
1555
1556 // max_frame_count - pre-checked to be greater than or equal to 0
1557 // stack_info_ptr - pre-checked for NULL
1558 // thread_count_ptr - pre-checked for NULL
1559 jvmtiError
1560 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1561 jvmtiError err = JVMTI_ERROR_NONE;
1562 JavaThread* calling_thread = JavaThread::current();
1563
1564 // JVMTI get stack traces at safepoint.
1565 VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1566 VMThread::execute(&op);
1567 *thread_count_ptr = op.final_thread_count();
1568 *stack_info_ptr = op.stack_info();
1569 err = op.result();
1570 return err;
1571 } /* end GetAllStackTraces */
1572
1573
1574 // thread_count - pre-checked to be greater than or equal to 0
1575 // thread_list - pre-checked for NULL
1576 // max_frame_count - pre-checked to be greater than or equal to 0
1577 // stack_info_ptr - pre-checked for NULL
1578 jvmtiError
1579 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1580 jvmtiError err = JVMTI_ERROR_NONE;
1581 // JVMTI get stack traces at safepoint.
1582 VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1583 VMThread::execute(&op);
1584 err = op.result();
1585 if (err == JVMTI_ERROR_NONE) {
1586 *stack_info_ptr = op.stack_info();
1587 }
1588 return err;
1589 } /* end GetThreadListStackTraces */
1590
1591
1592 // Threads_lock NOT held, java_thread not protected by lock
1593 // java_thread - pre-checked
1594 // count_ptr - pre-checked for NULL
1595 jvmtiError
1596 JvmtiEnv::GetFrameCount(JavaThread* java_thread, jint* count_ptr) {
1597 jvmtiError err = JVMTI_ERROR_NONE;
1598
1599 // retrieve or create JvmtiThreadState.
1600 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1601 if (state == NULL) {
1602 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1603 }
1604
1605 // It is only safe to perform the direct operation on the current
1606 // thread. All other usage needs to use a vm-safepoint-op for safety.
1607 if (java_thread == JavaThread::current()) {
1608 err = get_frame_count(state, count_ptr);
1609 } else {
1610 // get java stack frame count at safepoint.
1611 VM_GetFrameCount op(this, state, count_ptr);
1612 VMThread::execute(&op);
1613 err = op.result();
1614 }
1615 return err;
1616 } /* end GetFrameCount */
1617
1618
1619 // Threads_lock NOT held, java_thread not protected by lock
1620 // java_thread - pre-checked
1621 jvmtiError
1622 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1623 JavaThread* current_thread = JavaThread::current();
1624 HandleMark hm(current_thread);
1625 uint32_t debug_bits = 0;
1626
1627 // retrieve or create the state
1628 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1629 if (state == NULL) {
1630 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1631 }
1632
1633 // Check if java_thread is fully suspended
1634 if (!java_thread->is_thread_fully_suspended(true /* wait for suspend completion */, &debug_bits)) {
1635 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1636 }
1637 // Check to see if a PopFrame was already in progress
1638 if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
1639 // Probably possible for JVMTI clients to trigger this, but the
1640 // JPDA backend shouldn't allow this to happen
1641 return JVMTI_ERROR_INTERNAL;
1642 }
1643
1644 {
1645 // Was workaround bug
1646 // 4812902: popFrame hangs if the method is waiting at a synchronize
1647 // Catch this condition and return an error to avoid hanging.
1648 // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
1649 OSThread* osThread = java_thread->osthread();
1650 if (osThread->get_state() == MONITOR_WAIT) {
1651 return JVMTI_ERROR_OPAQUE_FRAME;
1652 }
1653 }
1654
1655 {
1656 ResourceMark rm(current_thread);
1657 // Check if there are more than one Java frame in this thread, that the top two frames
1658 // are Java (not native) frames, and that there is no intervening VM frame
1659 int frame_count = 0;
1660 bool is_interpreted[2];
1661 intptr_t *frame_sp[2];
1662 // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
1663 for (vframeStream vfs(java_thread, true); !vfs.at_end(); vfs.next()) {
1664 methodHandle mh(current_thread, vfs.method());
1665 if (mh->is_native()) return(JVMTI_ERROR_OPAQUE_FRAME);
1666 is_interpreted[frame_count] = vfs.is_interpreted_frame();
1667 frame_sp[frame_count] = vfs.frame_id();
1668 if (++frame_count > 1) break;
1669 }
1670 if (frame_count < 2) {
1671 // We haven't found two adjacent non-native Java frames on the top.
1672 // There can be two situations here:
1673 // 1. There are no more java frames
1674 // 2. Two top java frames are separated by non-java native frames
1675 if(vframeFor(java_thread, 1) == NULL) {
1676 return JVMTI_ERROR_NO_MORE_FRAMES;
1677 } else {
1678 // Intervening non-java native or VM frames separate java frames.
1679 // Current implementation does not support this. See bug #5031735.
1680 // In theory it is possible to pop frames in such cases.
1681 return JVMTI_ERROR_OPAQUE_FRAME;
1682 }
1683 }
1684
1685 // If any of the top 2 frames is a compiled one, need to deoptimize it
1686 for (int i = 0; i < 2; i++) {
1687 if (!is_interpreted[i]) {
1688 Deoptimization::deoptimize_frame(java_thread, frame_sp[i]);
1689 }
1690 }
1691
1692 // Update the thread state to reflect that the top frame is popped
1693 // so that cur_stack_depth is maintained properly and all frameIDs
1694 // are invalidated.
1695 // The current frame will be popped later when the suspended thread
1696 // is resumed and right before returning from VM to Java.
1697 // (see call_VM_base() in assembler_<cpu>.cpp).
1698
1699 // It's fine to update the thread state here because no JVMTI events
1700 // shall be posted for this PopFrame.
1701
1702 // It is only safe to perform the direct operation on the current
1703 // thread. All other usage needs to use a vm-safepoint-op for safety.
1704 if (java_thread == JavaThread::current()) {
1705 state->update_for_pop_top_frame();
1706 } else {
1707 VM_UpdateForPopTopFrame op(state);
1708 VMThread::execute(&op);
1709 jvmtiError err = op.result();
1710 if (err != JVMTI_ERROR_NONE) {
1711 return err;
1712 }
1713 }
1714
1715 java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
1716 // Set pending step flag for this popframe and it is cleared when next
1717 // step event is posted.
1718 state->set_pending_step_for_popframe();
1719 }
1720
1721 return JVMTI_ERROR_NONE;
1722 } /* end PopFrame */
1723
1724
1725 // Threads_lock NOT held, java_thread not protected by lock
1726 // java_thread - pre-checked
1727 // java_thread - unchecked
1728 // depth - pre-checked as non-negative
1729 // method_ptr - pre-checked for NULL
1730 // location_ptr - pre-checked for NULL
1731 jvmtiError
1732 JvmtiEnv::GetFrameLocation(JavaThread* java_thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1733 jvmtiError err = JVMTI_ERROR_NONE;
1734
1735 // It is only safe to perform the direct operation on the current
1736 // thread. All other usage needs to use a vm-safepoint-op for safety.
1737 if (java_thread == JavaThread::current()) {
1738 err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
1739 } else {
1740 // JVMTI get java stack frame location at safepoint.
1741 VM_GetFrameLocation op(this, java_thread, depth, method_ptr, location_ptr);
1742 VMThread::execute(&op);
1743 err = op.result();
1744 }
1745 return err;
1746 } /* end GetFrameLocation */
1747
1748
1749 // Threads_lock NOT held, java_thread not protected by lock
1750 // java_thread - pre-checked
1751 // java_thread - unchecked
1752 // depth - pre-checked as non-negative
1753 jvmtiError
1754 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
1755 jvmtiError err = JVMTI_ERROR_NONE;
1756 ResourceMark rm;
1757 uint32_t debug_bits = 0;
1758
1759 JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
1760 if (state == NULL) {
1761 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1762 }
1763
1764 if (!java_thread->is_thread_fully_suspended(true, &debug_bits)) {
1765 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1766 }
1767
1768 if (TraceJVMTICalls) {
1769 JvmtiSuspendControl::print();
1770 }
1771
1772 vframe *vf = vframeFor(java_thread, depth);
1773 if (vf == NULL) {
1774 return JVMTI_ERROR_NO_MORE_FRAMES;
1775 }
1776
1777 if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
1778 return JVMTI_ERROR_OPAQUE_FRAME;
1779 }
1780
1781 assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
1782
1783 // It is only safe to perform the direct operation on the current
1784 // thread. All other usage needs to use a vm-safepoint-op for safety.
1785 if (java_thread == JavaThread::current()) {
1786 int frame_number = state->count_frames() - depth;
1787 state->env_thread_state(this)->set_frame_pop(frame_number);
1788 } else {
1789 VM_SetFramePop op(this, state, depth);
1790 VMThread::execute(&op);
1791 err = op.result();
1792 }
1793 return err;
1794 } /* end NotifyFramePop */
1795
1796
1797 //
1798 // Force Early Return functions
1799 //
1800
1801 // Threads_lock NOT held, java_thread not protected by lock
1802 // java_thread - pre-checked
1803 jvmtiError
1804 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
1805 jvalue val;
1806 val.l = value;
1807 return force_early_return(java_thread, val, atos);
1808 } /* end ForceEarlyReturnObject */
1809
1810
1811 // Threads_lock NOT held, java_thread not protected by lock
1812 // java_thread - pre-checked
1813 jvmtiError
1814 JvmtiEnv::ForceEarlyReturnInt(JavaThread* java_thread, jint value) {
1815 jvalue val;
1816 val.i = value;
1817 return force_early_return(java_thread, val, itos);
1818 } /* end ForceEarlyReturnInt */
1819
1820
1821 // Threads_lock NOT held, java_thread not protected by lock
1822 // java_thread - pre-checked
1823 jvmtiError
1824 JvmtiEnv::ForceEarlyReturnLong(JavaThread* java_thread, jlong value) {
1825 jvalue val;
1826 val.j = value;
1827 return force_early_return(java_thread, val, ltos);
1828 } /* end ForceEarlyReturnLong */
1829
1830
1831 // Threads_lock NOT held, java_thread not protected by lock
1832 // java_thread - pre-checked
1833 jvmtiError
1834 JvmtiEnv::ForceEarlyReturnFloat(JavaThread* java_thread, jfloat value) {
1835 jvalue val;
1836 val.f = value;
1837 return force_early_return(java_thread, val, ftos);
1838 } /* end ForceEarlyReturnFloat */
1839
1840
1841 // Threads_lock NOT held, java_thread not protected by lock
1842 // java_thread - pre-checked
1843 jvmtiError
1844 JvmtiEnv::ForceEarlyReturnDouble(JavaThread* java_thread, jdouble value) {
1845 jvalue val;
1846 val.d = value;
1847 return force_early_return(java_thread, val, dtos);
1848 } /* end ForceEarlyReturnDouble */
1849
1850
1851 // Threads_lock NOT held, java_thread not protected by lock
1852 // java_thread - pre-checked
1853 jvmtiError
1854 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
1855 jvalue val;
1856 val.j = 0L;
1857 return force_early_return(java_thread, val, vtos);
1858 } /* end ForceEarlyReturnVoid */
1859
1860
1861 //
1862 // Heap functions
1863 //
1864
1865 // klass - NULL is a valid value, must be checked
1866 // initial_object - NULL is a valid value, must be checked
1867 // callbacks - pre-checked for NULL
1868 // user_data - NULL is a valid value, must be checked
1869 jvmtiError
1870 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1871 // check klass if provided
1872 Klass* k = NULL;
1873 if (klass != NULL) {
1874 oop k_mirror = JNIHandles::resolve_external_guard(klass);
1875 if (k_mirror == NULL) {
1876 return JVMTI_ERROR_INVALID_CLASS;
1877 }
1878 if (java_lang_Class::is_primitive(k_mirror)) {
1879 return JVMTI_ERROR_NONE;
1880 }
1881 k = java_lang_Class::as_Klass(k_mirror);
1882 if (klass == NULL) {
1883 return JVMTI_ERROR_INVALID_CLASS;
1884 }
1885 }
1886
1887 if (initial_object != NULL) {
1888 oop init_obj = JNIHandles::resolve_external_guard(initial_object);
1889 if (init_obj == NULL) {
1890 return JVMTI_ERROR_INVALID_OBJECT;
1891 }
1892 }
1893
1894 Thread *thread = Thread::current();
1895 HandleMark hm(thread);
1896
1897 TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1898 JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
1899 return JVMTI_ERROR_NONE;
1900 } /* end FollowReferences */
1901
1902
1903 // klass - NULL is a valid value, must be checked
1904 // callbacks - pre-checked for NULL
1905 // user_data - NULL is a valid value, must be checked
1906 jvmtiError
1907 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1908 // check klass if provided
1909 Klass* k = NULL;
1910 if (klass != NULL) {
1911 oop k_mirror = JNIHandles::resolve_external_guard(klass);
1912 if (k_mirror == NULL) {
1913 return JVMTI_ERROR_INVALID_CLASS;
1914 }
1915 if (java_lang_Class::is_primitive(k_mirror)) {
1916 return JVMTI_ERROR_NONE;
1917 }
1918 k = java_lang_Class::as_Klass(k_mirror);
1919 if (k == NULL) {
1920 return JVMTI_ERROR_INVALID_CLASS;
1921 }
1922 }
1923
1924 TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1925 JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
1926 return JVMTI_ERROR_NONE;
1927 } /* end IterateThroughHeap */
1928
1929
1930 // tag_ptr - pre-checked for NULL
1931 jvmtiError
1932 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1933 oop o = JNIHandles::resolve_external_guard(object);
1934 NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1935 *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1936 return JVMTI_ERROR_NONE;
1937 } /* end GetTag */
1938
1939
1940 jvmtiError
1941 JvmtiEnv::SetTag(jobject object, jlong tag) {
1942 oop o = JNIHandles::resolve_external_guard(object);
1943 NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1944 JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1945 return JVMTI_ERROR_NONE;
1946 } /* end SetTag */
1947
1948
1949 // tag_count - pre-checked to be greater than or equal to 0
1950 // tags - pre-checked for NULL
1951 // count_ptr - pre-checked for NULL
1952 // object_result_ptr - NULL is a valid value, must be checked
1953 // tag_result_ptr - NULL is a valid value, must be checked
1954 jvmtiError
1955 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
1956 TraceTime t("GetObjectsWithTags", TRACETIME_LOG(Debug, jvmti, objecttagging));
1957 return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
1958 } /* end GetObjectsWithTags */
1959
1960
1961 jvmtiError
1962 JvmtiEnv::ForceGarbageCollection() {
1963 Universe::heap()->collect(GCCause::_jvmti_force_gc);
1964 return JVMTI_ERROR_NONE;
1965 } /* end ForceGarbageCollection */
1966
1967
1968 //
1969 // Heap (1.0) functions
1970 //
1971
1972 // object_reference_callback - pre-checked for NULL
1973 // user_data - NULL is a valid value, must be checked
1974 jvmtiError
1975 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
1976 oop o = JNIHandles::resolve_external_guard(object);
1977 NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1978 JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
1979 return JVMTI_ERROR_NONE;
1980 } /* end IterateOverObjectsReachableFromObject */
1981
1982
1983 // heap_root_callback - NULL is a valid value, must be checked
1984 // stack_ref_callback - NULL is a valid value, must be checked
1985 // object_ref_callback - NULL is a valid value, must be checked
1986 // user_data - NULL is a valid value, must be checked
1987 jvmtiError
1988 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1989 TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
1990 JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1991 return JVMTI_ERROR_NONE;
1992 } /* end IterateOverReachableObjects */
1993
1994
1995 // heap_object_callback - pre-checked for NULL
1996 // user_data - NULL is a valid value, must be checked
1997 jvmtiError
1998 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1999 TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
2000 Thread *thread = Thread::current();
2001 HandleMark hm(thread);
2002 JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, NULL, heap_object_callback, user_data);
2003 return JVMTI_ERROR_NONE;
2004 } /* end IterateOverHeap */
2005
2006
2007 // k_mirror - may be primitive, this must be checked
2008 // heap_object_callback - pre-checked for NULL
2009 // user_data - NULL is a valid value, must be checked
2010 jvmtiError
2011 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
2012 if (java_lang_Class::is_primitive(k_mirror)) {
2013 // DO PRIMITIVE CLASS PROCESSING
2014 return JVMTI_ERROR_NONE;
2015 }
2016 Klass* klass = java_lang_Class::as_Klass(k_mirror);
2017 if (klass == NULL) {
2018 return JVMTI_ERROR_INVALID_CLASS;
2019 }
2020 TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
2021 JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
2022 return JVMTI_ERROR_NONE;
2023 } /* end IterateOverInstancesOfClass */
2024
2025
2026 //
2027 // Local Variable functions
2028 //
2029
2030 // Threads_lock NOT held, java_thread not protected by lock
2031 // java_thread - pre-checked
2032 // java_thread - unchecked
2033 // depth - pre-checked as non-negative
2034 // value_ptr - pre-checked for NULL
2035 jvmtiError
2036 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {
2037 JavaThread* current_thread = JavaThread::current();
2038 // rm object is created to clean up the javaVFrame created in
2039 // doit_prologue(), but after doit() is finished with it.
2040 ResourceMark rm(current_thread);
2041
2042 VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
2043 VMThread::execute(&op);
2044 jvmtiError err = op.result();
2045 if (err != JVMTI_ERROR_NONE) {
2046 return err;
2047 } else {
2048 *value_ptr = op.value().l;
2049 return JVMTI_ERROR_NONE;
2050 }
2051 } /* end GetLocalObject */
2052
2053 // Threads_lock NOT held, java_thread not protected by lock
2054 // java_thread - pre-checked
2055 // java_thread - unchecked
2056 // depth - pre-checked as non-negative
2057 // value - pre-checked for NULL
2058 jvmtiError
2059 JvmtiEnv::GetLocalInstance(JavaThread* java_thread, jint depth, jobject* value_ptr){
2060 JavaThread* current_thread = JavaThread::current();
2061 // rm object is created to clean up the javaVFrame created in
2062 // doit_prologue(), but after doit() is finished with it.
2063 ResourceMark rm(current_thread);
2064
2065 VM_GetReceiver op(java_thread, current_thread, depth);
2066 VMThread::execute(&op);
2067 jvmtiError err = op.result();
2068 if (err != JVMTI_ERROR_NONE) {
2069 return err;
2070 } else {
2071 *value_ptr = op.value().l;
2072 return JVMTI_ERROR_NONE;
2073 }
2074 } /* end GetLocalInstance */
2075
2076
2077 // Threads_lock NOT held, java_thread not protected by lock
2078 // java_thread - pre-checked
2079 // java_thread - unchecked
2080 // depth - pre-checked as non-negative
2081 // value_ptr - pre-checked for NULL
2082 jvmtiError
2083 JvmtiEnv::GetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint* value_ptr) {
2084 // rm object is created to clean up the javaVFrame created in
2085 // doit_prologue(), but after doit() is finished with it.
2086 ResourceMark rm;
2087
2088 VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
2089 VMThread::execute(&op);
2090 *value_ptr = op.value().i;
2091 return op.result();
2092 } /* end GetLocalInt */
2093
2094
2095 // Threads_lock NOT held, java_thread not protected by lock
2096 // java_thread - pre-checked
2097 // java_thread - unchecked
2098 // depth - pre-checked as non-negative
2099 // value_ptr - pre-checked for NULL
2100 jvmtiError
2101 JvmtiEnv::GetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong* value_ptr) {
2102 // rm object is created to clean up the javaVFrame created in
2103 // doit_prologue(), but after doit() is finished with it.
2104 ResourceMark rm;
2105
2106 VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
2107 VMThread::execute(&op);
2108 *value_ptr = op.value().j;
2109 return op.result();
2110 } /* end GetLocalLong */
2111
2112
2113 // Threads_lock NOT held, java_thread not protected by lock
2114 // java_thread - pre-checked
2115 // java_thread - unchecked
2116 // depth - pre-checked as non-negative
2117 // value_ptr - pre-checked for NULL
2118 jvmtiError
2119 JvmtiEnv::GetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat* value_ptr) {
2120 // rm object is created to clean up the javaVFrame created in
2121 // doit_prologue(), but after doit() is finished with it.
2122 ResourceMark rm;
2123
2124 VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
2125 VMThread::execute(&op);
2126 *value_ptr = op.value().f;
2127 return op.result();
2128 } /* end GetLocalFloat */
2129
2130
2131 // Threads_lock NOT held, java_thread not protected by lock
2132 // java_thread - pre-checked
2133 // java_thread - unchecked
2134 // depth - pre-checked as non-negative
2135 // value_ptr - pre-checked for NULL
2136 jvmtiError
2137 JvmtiEnv::GetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble* value_ptr) {
2138 // rm object is created to clean up the javaVFrame created in
2139 // doit_prologue(), but after doit() is finished with it.
2140 ResourceMark rm;
2141
2142 VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
2143 VMThread::execute(&op);
2144 *value_ptr = op.value().d;
2145 return op.result();
2146 } /* end GetLocalDouble */
2147
2148
2149 // Threads_lock NOT held, java_thread not protected by lock
2150 // java_thread - pre-checked
2151 // java_thread - unchecked
2152 // depth - pre-checked as non-negative
2153 jvmtiError
2154 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
2155 // rm object is created to clean up the javaVFrame created in
2156 // doit_prologue(), but after doit() is finished with it.
2157 ResourceMark rm;
2158 jvalue val;
2159 val.l = value;
2160 VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
2161 VMThread::execute(&op);
2162 return op.result();
2163 } /* end SetLocalObject */
2164
2165
2166 // Threads_lock NOT held, java_thread not protected by lock
2167 // java_thread - pre-checked
2168 // java_thread - unchecked
2169 // depth - pre-checked as non-negative
2170 jvmtiError
2171 JvmtiEnv::SetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint value) {
2172 // rm object is created to clean up the javaVFrame created in
2173 // doit_prologue(), but after doit() is finished with it.
2174 ResourceMark rm;
2175 jvalue val;
2176 val.i = value;
2177 VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val);
2178 VMThread::execute(&op);
2179 return op.result();
2180 } /* end SetLocalInt */
2181
2182
2183 // Threads_lock NOT held, java_thread not protected by lock
2184 // java_thread - pre-checked
2185 // java_thread - unchecked
2186 // depth - pre-checked as non-negative
2187 jvmtiError
2188 JvmtiEnv::SetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong value) {
2189 // rm object is created to clean up the javaVFrame created in
2190 // doit_prologue(), but after doit() is finished with it.
2191 ResourceMark rm;
2192 jvalue val;
2193 val.j = value;
2194 VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val);
2195 VMThread::execute(&op);
2196 return op.result();
2197 } /* end SetLocalLong */
2198
2199
2200 // Threads_lock NOT held, java_thread not protected by lock
2201 // java_thread - pre-checked
2202 // java_thread - unchecked
2203 // depth - pre-checked as non-negative
2204 jvmtiError
2205 JvmtiEnv::SetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat value) {
2206 // rm object is created to clean up the javaVFrame created in
2207 // doit_prologue(), but after doit() is finished with it.
2208 ResourceMark rm;
2209 jvalue val;
2210 val.f = value;
2211 VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val);
2212 VMThread::execute(&op);
2213 return op.result();
2214 } /* end SetLocalFloat */
2215
2216
2217 // Threads_lock NOT held, java_thread not protected by lock
2218 // java_thread - pre-checked
2219 // java_thread - unchecked
2220 // depth - pre-checked as non-negative
2221 jvmtiError
2222 JvmtiEnv::SetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble value) {
2223 // rm object is created to clean up the javaVFrame created in
2224 // doit_prologue(), but after doit() is finished with it.
2225 ResourceMark rm;
2226 jvalue val;
2227 val.d = value;
2228 VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val);
2229 VMThread::execute(&op);
2230 return op.result();
2231 } /* end SetLocalDouble */
2232
2233
2234 //
2235 // Breakpoint functions
2236 //
2237
2238 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2239 jvmtiError
2240 JvmtiEnv::SetBreakpoint(Method* method_oop, jlocation location) {
2241 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2242 if (location < 0) { // simple invalid location check first
2243 return JVMTI_ERROR_INVALID_LOCATION;
2244 }
2245 // verify that the breakpoint is not past the end of the method
2246 if (location >= (jlocation) method_oop->code_size()) {
2247 return JVMTI_ERROR_INVALID_LOCATION;
2248 }
2249
2250 ResourceMark rm;
2251 JvmtiBreakpoint bp(method_oop, location);
2252 JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2253 if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
2254 return JVMTI_ERROR_DUPLICATE;
2255
2256 if (TraceJVMTICalls) {
2257 jvmti_breakpoints.print();
2258 }
2259
2260 return JVMTI_ERROR_NONE;
2261 } /* end SetBreakpoint */
2262
2263
2264 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2265 jvmtiError
2266 JvmtiEnv::ClearBreakpoint(Method* method_oop, jlocation location) {
2267 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2268
2269 if (location < 0) { // simple invalid location check first
2270 return JVMTI_ERROR_INVALID_LOCATION;
2271 }
2272
2273 // verify that the breakpoint is not past the end of the method
2274 if (location >= (jlocation) method_oop->code_size()) {
2275 return JVMTI_ERROR_INVALID_LOCATION;
2276 }
2277
2278 JvmtiBreakpoint bp(method_oop, location);
2279
2280 JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
2281 if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
2282 return JVMTI_ERROR_NOT_FOUND;
2283
2284 if (TraceJVMTICalls) {
2285 jvmti_breakpoints.print();
2286 }
2287
2288 return JVMTI_ERROR_NONE;
2289 } /* end ClearBreakpoint */
2290
2291
2292 //
2293 // Watched Field functions
2294 //
2295
2296 jvmtiError
2297 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2298 // make sure we haven't set this watch before
2299 if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
2300 fdesc_ptr->set_is_field_access_watched(true);
2301
2302 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
2303
2304 return JVMTI_ERROR_NONE;
2305 } /* end SetFieldAccessWatch */
2306
2307
2308 jvmtiError
2309 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
2310 // make sure we have a watch to clear
2311 if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
2312 fdesc_ptr->set_is_field_access_watched(false);
2313
2314 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
2315
2316 return JVMTI_ERROR_NONE;
2317 } /* end ClearFieldAccessWatch */
2318
2319
2320 jvmtiError
2321 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2322 // make sure we haven't set this watch before
2323 if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2324 fdesc_ptr->set_is_field_modification_watched(true);
2325
2326 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2327
2328 return JVMTI_ERROR_NONE;
2329 } /* end SetFieldModificationWatch */
2330
2331
2332 jvmtiError
2333 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2334 // make sure we have a watch to clear
2335 if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
2336 fdesc_ptr->set_is_field_modification_watched(false);
2337
2338 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
2339
2340 return JVMTI_ERROR_NONE;
2341 } /* end ClearFieldModificationWatch */
2342
2343 //
2344 // Class functions
2345 //
2346
2347
2348 // k_mirror - may be primitive, this must be checked
2349 // signature_ptr - NULL is a valid value, must be checked
2350 // generic_ptr - NULL is a valid value, must be checked
2351 jvmtiError
2352 JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
2353 ResourceMark rm;
2354 bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
2355 Klass* k = NULL;
2356 if (!isPrimitive) {
2357 k = java_lang_Class::as_Klass(k_mirror);
2358 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2359 }
2360 if (signature_ptr != NULL) {
2361 char* result = NULL;
2362 if (isPrimitive) {
2363 char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
2364 result = (char*) jvmtiMalloc(2);
2365 result[0] = tchar;
2366 result[1] = '\0';
2367 } else {
2368 const char* class_sig = k->signature_name();
2369 result = (char *) jvmtiMalloc(strlen(class_sig)+1);
2370 strcpy(result, class_sig);
2371 }
2372 *signature_ptr = result;
2373 }
2374 if (generic_ptr != NULL) {
2375 *generic_ptr = NULL;
2376 if (!isPrimitive && k->is_instance_klass()) {
2377 Symbol* soo = InstanceKlass::cast(k)->generic_signature();
2378 if (soo != NULL) {
2379 const char *gen_sig = soo->as_C_string();
2380 if (gen_sig != NULL) {
2381 char* gen_result;
2382 jvmtiError err = allocate(strlen(gen_sig) + 1,
2383 (unsigned char **)&gen_result);
2384 if (err != JVMTI_ERROR_NONE) {
2385 return err;
2386 }
2387 strcpy(gen_result, gen_sig);
2388 *generic_ptr = gen_result;
2389 }
2390 }
2391 }
2392 }
2393 return JVMTI_ERROR_NONE;
2394 } /* end GetClassSignature */
2395
2396
2397 // k_mirror - may be primitive, this must be checked
2398 // status_ptr - pre-checked for NULL
2399 jvmtiError
2400 JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
2401 jint result = 0;
2402 if (java_lang_Class::is_primitive(k_mirror)) {
2403 result |= JVMTI_CLASS_STATUS_PRIMITIVE;
2404 } else {
2405 Klass* k = java_lang_Class::as_Klass(k_mirror);
2406 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2407 result = k->jvmti_class_status();
2408 }
2409 *status_ptr = result;
2410
2411 return JVMTI_ERROR_NONE;
2412 } /* end GetClassStatus */
2413
2414
2415 // k_mirror - may be primitive, this must be checked
2416 // source_name_ptr - pre-checked for NULL
2417 jvmtiError
2418 JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
2419 if (java_lang_Class::is_primitive(k_mirror)) {
2420 return JVMTI_ERROR_ABSENT_INFORMATION;
2421 }
2422 Klass* k_klass = java_lang_Class::as_Klass(k_mirror);
2423 NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
2424
2425 if (!k_klass->is_instance_klass()) {
2426 return JVMTI_ERROR_ABSENT_INFORMATION;
2427 }
2428
2429 Symbol* sfnOop = InstanceKlass::cast(k_klass)->source_file_name();
2430 NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
2431 {
2432 JavaThread* current_thread = JavaThread::current();
2433 ResourceMark rm(current_thread);
2434 const char* sfncp = (const char*) sfnOop->as_C_string();
2435 *source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
2436 strcpy(*source_name_ptr, sfncp);
2437 }
2438
2439 return JVMTI_ERROR_NONE;
2440 } /* end GetSourceFileName */
2441
2442
2443 // k_mirror - may be primitive, this must be checked
2444 // modifiers_ptr - pre-checked for NULL
2445 jvmtiError
2446 JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
2447 JavaThread* current_thread = JavaThread::current();
2448 jint result = 0;
2449 if (!java_lang_Class::is_primitive(k_mirror)) {
2450 Klass* k = java_lang_Class::as_Klass(k_mirror);
2451 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2452 result = k->compute_modifier_flags(current_thread);
2453 JavaThread* THREAD = current_thread; // pass to macros
2454 if (HAS_PENDING_EXCEPTION) {
2455 CLEAR_PENDING_EXCEPTION;
2456 return JVMTI_ERROR_INTERNAL;
2457 };
2458
2459 // Reset the deleted ACC_SUPER bit ( deleted in compute_modifier_flags()).
2460 if(k->is_super()) {
2461 result |= JVM_ACC_SUPER;
2462 }
2463 } else {
2464 result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
2465 }
2466 *modifiers_ptr = result;
2467
2468 return JVMTI_ERROR_NONE;
2469 } /* end GetClassModifiers */
2470
2471
2472 // k_mirror - may be primitive, this must be checked
2473 // method_count_ptr - pre-checked for NULL
2474 // methods_ptr - pre-checked for NULL
2475 jvmtiError
2476 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2477 JavaThread* current_thread = JavaThread::current();
2478 HandleMark hm(current_thread);
2479
2480 if (java_lang_Class::is_primitive(k_mirror)) {
2481 *method_count_ptr = 0;
2482 *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2483 return JVMTI_ERROR_NONE;
2484 }
2485 Klass* k = java_lang_Class::as_Klass(k_mirror);
2486 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2487
2488 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2489 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2490 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2491 }
2492
2493 if (!k->is_instance_klass()) {
2494 *method_count_ptr = 0;
2495 *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2496 return JVMTI_ERROR_NONE;
2497 }
2498 InstanceKlass* ik = InstanceKlass::cast(k);
2499 // Allocate the result and fill it in
2500 int result_length = ik->methods()->length();
2501 jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2502 int index;
2503 bool jmethodids_found = true;
2504
2505 if (JvmtiExport::can_maintain_original_method_order()) {
2506 // Use the original method ordering indices stored in the class, so we can emit
2507 // jmethodIDs in the order they appeared in the class file
2508 for (index = 0; index < result_length; index++) {
2509 Method* m = ik->methods()->at(index);
2510 int original_index = ik->method_ordering()->at(index);
2511 assert(original_index >= 0 && original_index < result_length, "invalid original method index");
2512 jmethodID id;
2513 if (jmethodids_found) {
2514 id = m->find_jmethod_id_or_null();
2515 if (id == NULL) {
2516 // If we find an uninitialized value, make sure there is
2517 // enough space for all the uninitialized values we might
2518 // find.
2519 ik->ensure_space_for_methodids(index);
2520 jmethodids_found = false;
2521 id = m->jmethod_id();
2522 }
2523 } else {
2524 id = m->jmethod_id();
2525 }
2526 result_list[original_index] = id;
2527 }
2528 } else {
2529 // otherwise just copy in any order
2530 for (index = 0; index < result_length; index++) {
2531 Method* m = ik->methods()->at(index);
2532 jmethodID id;
2533 if (jmethodids_found) {
2534 id = m->find_jmethod_id_or_null();
2535 if (id == NULL) {
2536 // If we find an uninitialized value, make sure there is
2537 // enough space for all the uninitialized values we might
2538 // find.
2539 ik->ensure_space_for_methodids(index);
2540 jmethodids_found = false;
2541 id = m->jmethod_id();
2542 }
2543 } else {
2544 id = m->jmethod_id();
2545 }
2546 result_list[index] = id;
2547 }
2548 }
2549 // Fill in return value.
2550 *method_count_ptr = result_length;
2551 *methods_ptr = result_list;
2552
2553 return JVMTI_ERROR_NONE;
2554 } /* end GetClassMethods */
2555
2556
2557 // k_mirror - may be primitive, this must be checked
2558 // field_count_ptr - pre-checked for NULL
2559 // fields_ptr - pre-checked for NULL
2560 jvmtiError
2561 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2562 if (java_lang_Class::is_primitive(k_mirror)) {
2563 *field_count_ptr = 0;
2564 *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2565 return JVMTI_ERROR_NONE;
2566 }
2567 JavaThread* current_thread = JavaThread::current();
2568 HandleMark hm(current_thread);
2569 Klass* k = java_lang_Class::as_Klass(k_mirror);
2570 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2571
2572 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2573 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2574 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2575 }
2576
2577 if (!k->is_instance_klass()) {
2578 *field_count_ptr = 0;
2579 *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2580 return JVMTI_ERROR_NONE;
2581 }
2582
2583
2584 InstanceKlass* ik = InstanceKlass::cast(k);
2585
2586 int result_count = 0;
2587 // First, count the fields.
2588 FilteredFieldStream flds(ik, true, true);
2589 result_count = flds.field_count();
2590
2591 // Allocate the result and fill it in
2592 jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
2593 // The JVMTI spec requires fields in the order they occur in the class file,
2594 // this is the reverse order of what FieldStream hands out.
2595 int id_index = (result_count - 1);
2596
2597 for (FilteredFieldStream src_st(ik, true, true); !src_st.eos(); src_st.next()) {
2598 result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
2599 ik, src_st.offset(),
2600 src_st.access_flags().is_static());
2601 }
2602 assert(id_index == -1, "just checking");
2603 // Fill in the results
2604 *field_count_ptr = result_count;
2605 *fields_ptr = result_list;
2606
2607 return JVMTI_ERROR_NONE;
2608 } /* end GetClassFields */
2609
2610
2611 // k_mirror - may be primitive, this must be checked
2612 // interface_count_ptr - pre-checked for NULL
2613 // interfaces_ptr - pre-checked for NULL
2614 jvmtiError
2615 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2616 {
2617 if (java_lang_Class::is_primitive(k_mirror)) {
2618 *interface_count_ptr = 0;
2619 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2620 return JVMTI_ERROR_NONE;
2621 }
2622 JavaThread* current_thread = JavaThread::current();
2623 HandleMark hm(current_thread);
2624 Klass* k = java_lang_Class::as_Klass(k_mirror);
2625 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2626
2627 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2628 if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2629 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2630
2631 if (!k->is_instance_klass()) {
2632 *interface_count_ptr = 0;
2633 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2634 return JVMTI_ERROR_NONE;
2635 }
2636
2637 Array<InstanceKlass*>* interface_list = InstanceKlass::cast(k)->local_interfaces();
2638 const int result_length = (interface_list == NULL ? 0 : interface_list->length());
2639 jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2640 for (int i_index = 0; i_index < result_length; i_index += 1) {
2641 InstanceKlass* klass_at = interface_list->at(i_index);
2642 assert(klass_at->is_klass(), "interfaces must be Klass*s");
2643 assert(klass_at->is_interface(), "interfaces must be interfaces");
2644 oop mirror_at = klass_at->java_mirror();
2645 Handle handle_at = Handle(current_thread, mirror_at);
2646 result_list[i_index] = (jclass) jni_reference(handle_at);
2647 }
2648 *interface_count_ptr = result_length;
2649 *interfaces_ptr = result_list;
2650 }
2651
2652 return JVMTI_ERROR_NONE;
2653 } /* end GetImplementedInterfaces */
2654
2655
2656 // k_mirror - may be primitive, this must be checked
2657 // minor_version_ptr - pre-checked for NULL
2658 // major_version_ptr - pre-checked for NULL
2659 jvmtiError
2660 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2661 if (java_lang_Class::is_primitive(k_mirror)) {
2662 return JVMTI_ERROR_ABSENT_INFORMATION;
2663 }
2664 Klass* klass = java_lang_Class::as_Klass(k_mirror);
2665
2666 jint status = klass->jvmti_class_status();
2667 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2668 return JVMTI_ERROR_INVALID_CLASS;
2669 }
2670 if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2671 return JVMTI_ERROR_ABSENT_INFORMATION;
2672 }
2673
2674 InstanceKlass* ik = InstanceKlass::cast(klass);
2675 *minor_version_ptr = ik->minor_version();
2676 *major_version_ptr = ik->major_version();
2677
2678 return JVMTI_ERROR_NONE;
2679 } /* end GetClassVersionNumbers */
2680
2681
2682 // k_mirror - may be primitive, this must be checked
2683 // constant_pool_count_ptr - pre-checked for NULL
2684 // constant_pool_byte_count_ptr - pre-checked for NULL
2685 // constant_pool_bytes_ptr - pre-checked for NULL
2686 jvmtiError
2687 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2688 if (java_lang_Class::is_primitive(k_mirror)) {
2689 return JVMTI_ERROR_ABSENT_INFORMATION;
2690 }
2691
2692 Klass* klass = java_lang_Class::as_Klass(k_mirror);
2693 Thread *thread = Thread::current();
2694 ResourceMark rm(thread);
2695
2696 jint status = klass->jvmti_class_status();
2697 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2698 return JVMTI_ERROR_INVALID_CLASS;
2699 }
2700 if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2701 return JVMTI_ERROR_ABSENT_INFORMATION;
2702 }
2703
2704 InstanceKlass* ik = InstanceKlass::cast(klass);
2705 JvmtiConstantPoolReconstituter reconstituter(ik);
2706 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2707 return reconstituter.get_error();
2708 }
2709
2710 unsigned char *cpool_bytes;
2711 int cpool_size = reconstituter.cpool_size();
2712 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2713 return reconstituter.get_error();
2714 }
2715 jvmtiError res = allocate(cpool_size, &cpool_bytes);
2716 if (res != JVMTI_ERROR_NONE) {
2717 return res;
2718 }
2719 reconstituter.copy_cpool_bytes(cpool_bytes);
2720 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2721 return reconstituter.get_error();
2722 }
2723
2724 constantPoolHandle constants(thread, ik->constants());
2725 *constant_pool_count_ptr = constants->length();
2726 *constant_pool_byte_count_ptr = cpool_size;
2727 *constant_pool_bytes_ptr = cpool_bytes;
2728
2729 return JVMTI_ERROR_NONE;
2730 } /* end GetConstantPool */
2731
2732
2733 // k_mirror - may be primitive, this must be checked
2734 // is_interface_ptr - pre-checked for NULL
2735 jvmtiError
2736 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2737 {
2738 bool result = false;
2739 if (!java_lang_Class::is_primitive(k_mirror)) {
2740 Klass* k = java_lang_Class::as_Klass(k_mirror);
2741 if (k != NULL && k->is_interface()) {
2742 result = true;
2743 }
2744 }
2745 *is_interface_ptr = result;
2746 }
2747
2748 return JVMTI_ERROR_NONE;
2749 } /* end IsInterface */
2750
2751
2752 // k_mirror - may be primitive, this must be checked
2753 // is_array_class_ptr - pre-checked for NULL
2754 jvmtiError
2755 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
2756 {
2757 bool result = false;
2758 if (!java_lang_Class::is_primitive(k_mirror)) {
2759 Klass* k = java_lang_Class::as_Klass(k_mirror);
2760 if (k != NULL && k->is_array_klass()) {
2761 result = true;
2762 }
2763 }
2764 *is_array_class_ptr = result;
2765 }
2766
2767 return JVMTI_ERROR_NONE;
2768 } /* end IsArrayClass */
2769
2770
2771 // k_mirror - may be primitive, this must be checked
2772 // classloader_ptr - pre-checked for NULL
2773 jvmtiError
2774 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
2775 {
2776 if (java_lang_Class::is_primitive(k_mirror)) {
2777 *classloader_ptr = (jclass) jni_reference(Handle());
2778 return JVMTI_ERROR_NONE;
2779 }
2780 JavaThread* current_thread = JavaThread::current();
2781 HandleMark hm(current_thread);
2782 Klass* k = java_lang_Class::as_Klass(k_mirror);
2783 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2784
2785 oop result_oop = k->class_loader();
2786 if (result_oop == NULL) {
2787 *classloader_ptr = (jclass) jni_reference(Handle());
2788 return JVMTI_ERROR_NONE;
2789 }
2790 Handle result_handle = Handle(current_thread, result_oop);
2791 jclass result_jnihandle = (jclass) jni_reference(result_handle);
2792 *classloader_ptr = result_jnihandle;
2793 }
2794 return JVMTI_ERROR_NONE;
2795 } /* end GetClassLoader */
2796
2797
2798 // k_mirror - may be primitive, this must be checked
2799 // source_debug_extension_ptr - pre-checked for NULL
2800 jvmtiError
2801 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
2802 {
2803 if (java_lang_Class::is_primitive(k_mirror)) {
2804 return JVMTI_ERROR_ABSENT_INFORMATION;
2805 }
2806 Klass* k = java_lang_Class::as_Klass(k_mirror);
2807 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2808 if (!k->is_instance_klass()) {
2809 return JVMTI_ERROR_ABSENT_INFORMATION;
2810 }
2811 const char* sde = InstanceKlass::cast(k)->source_debug_extension();
2812 NULL_CHECK(sde, JVMTI_ERROR_ABSENT_INFORMATION);
2813
2814 {
2815 *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sde)+1);
2816 strcpy(*source_debug_extension_ptr, sde);
2817 }
2818 }
2819
2820 return JVMTI_ERROR_NONE;
2821 } /* end GetSourceDebugExtension */
2822
2823 //
2824 // Object functions
2825 //
2826
2827 // hash_code_ptr - pre-checked for NULL
2828 jvmtiError
2829 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
2830 oop mirror = JNIHandles::resolve_external_guard(object);
2831 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
2832 NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
2833
2834 {
2835 jint result = (jint) mirror->identity_hash();
2836 *hash_code_ptr = result;
2837 }
2838 return JVMTI_ERROR_NONE;
2839 } /* end GetObjectHashCode */
2840
2841
2842 // info_ptr - pre-checked for NULL
2843 jvmtiError
2844 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
2845 JavaThread* calling_thread = JavaThread::current();
2846 jvmtiError err = get_object_monitor_usage(calling_thread, object, info_ptr);
2847 if (err == JVMTI_ERROR_THREAD_NOT_SUSPENDED) {
2848 // Some of the critical threads were not suspended. go to a safepoint and try again
2849 VM_GetObjectMonitorUsage op(this, calling_thread, object, info_ptr);
2850 VMThread::execute(&op);
2851 err = op.result();
2852 }
2853 return err;
2854 } /* end GetObjectMonitorUsage */
2855
2856
2857 //
2858 // Field functions
2859 //
2860
2861 // name_ptr - NULL is a valid value, must be checked
2862 // signature_ptr - NULL is a valid value, must be checked
2863 // generic_ptr - NULL is a valid value, must be checked
2864 jvmtiError
2865 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2866 JavaThread* current_thread = JavaThread::current();
2867 ResourceMark rm(current_thread);
2868 if (name_ptr == NULL) {
2869 // just don't return the name
2870 } else {
2871 const char* fieldName = fdesc_ptr->name()->as_C_string();
2872 *name_ptr = (char*) jvmtiMalloc(strlen(fieldName) + 1);
2873 if (*name_ptr == NULL)
2874 return JVMTI_ERROR_OUT_OF_MEMORY;
2875 strcpy(*name_ptr, fieldName);
2876 }
2877 if (signature_ptr== NULL) {
2878 // just don't return the signature
2879 } else {
2880 const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
2881 *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
2882 if (*signature_ptr == NULL)
2883 return JVMTI_ERROR_OUT_OF_MEMORY;
2884 strcpy(*signature_ptr, fieldSignature);
2885 }
2886 if (generic_ptr != NULL) {
2887 *generic_ptr = NULL;
2888 Symbol* soop = fdesc_ptr->generic_signature();
2889 if (soop != NULL) {
2890 const char* gen_sig = soop->as_C_string();
2891 if (gen_sig != NULL) {
2892 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2893 if (err != JVMTI_ERROR_NONE) {
2894 return err;
2895 }
2896 strcpy(*generic_ptr, gen_sig);
2897 }
2898 }
2899 }
2900 return JVMTI_ERROR_NONE;
2901 } /* end GetFieldName */
2902
2903
2904 // declaring_class_ptr - pre-checked for NULL
2905 jvmtiError
2906 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
2907
2908 *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
2909 return JVMTI_ERROR_NONE;
2910 } /* end GetFieldDeclaringClass */
2911
2912
2913 // modifiers_ptr - pre-checked for NULL
2914 jvmtiError
2915 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
2916
2917 AccessFlags resultFlags = fdesc_ptr->access_flags();
2918 jint result = resultFlags.as_int();
2919 *modifiers_ptr = result;
2920
2921 return JVMTI_ERROR_NONE;
2922 } /* end GetFieldModifiers */
2923
2924
2925 // is_synthetic_ptr - pre-checked for NULL
2926 jvmtiError
2927 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
2928 *is_synthetic_ptr = fdesc_ptr->is_synthetic();
2929 return JVMTI_ERROR_NONE;
2930 } /* end IsFieldSynthetic */
2931
2932
2933 //
2934 // Method functions
2935 //
2936
2937 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2938 // name_ptr - NULL is a valid value, must be checked
2939 // signature_ptr - NULL is a valid value, must be checked
2940 // generic_ptr - NULL is a valid value, must be checked
2941 jvmtiError
2942 JvmtiEnv::GetMethodName(Method* method_oop, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2943 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2944 JavaThread* current_thread = JavaThread::current();
2945
2946 ResourceMark rm(current_thread); // get the utf8 name and signature
2947 if (name_ptr == NULL) {
2948 // just don't return the name
2949 } else {
2950 const char* utf8_name = (const char *) method_oop->name()->as_utf8();
2951 *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
2952 strcpy(*name_ptr, utf8_name);
2953 }
2954 if (signature_ptr == NULL) {
2955 // just don't return the signature
2956 } else {
2957 const char* utf8_signature = (const char *) method_oop->signature()->as_utf8();
2958 *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
2959 strcpy(*signature_ptr, utf8_signature);
2960 }
2961
2962 if (generic_ptr != NULL) {
2963 *generic_ptr = NULL;
2964 Symbol* soop = method_oop->generic_signature();
2965 if (soop != NULL) {
2966 const char* gen_sig = soop->as_C_string();
2967 if (gen_sig != NULL) {
2968 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2969 if (err != JVMTI_ERROR_NONE) {
2970 return err;
2971 }
2972 strcpy(*generic_ptr, gen_sig);
2973 }
2974 }
2975 }
2976 return JVMTI_ERROR_NONE;
2977 } /* end GetMethodName */
2978
2979
2980 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2981 // declaring_class_ptr - pre-checked for NULL
2982 jvmtiError
2983 JvmtiEnv::GetMethodDeclaringClass(Method* method_oop, jclass* declaring_class_ptr) {
2984 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2985 (*declaring_class_ptr) = get_jni_class_non_null(method_oop->method_holder());
2986 return JVMTI_ERROR_NONE;
2987 } /* end GetMethodDeclaringClass */
2988
2989
2990 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2991 // modifiers_ptr - pre-checked for NULL
2992 jvmtiError
2993 JvmtiEnv::GetMethodModifiers(Method* method_oop, jint* modifiers_ptr) {
2994 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2995 (*modifiers_ptr) = method_oop->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2996 return JVMTI_ERROR_NONE;
2997 } /* end GetMethodModifiers */
2998
2999
3000 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3001 // max_ptr - pre-checked for NULL
3002 jvmtiError
3003 JvmtiEnv::GetMaxLocals(Method* method_oop, jint* max_ptr) {
3004 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3005 // get max stack
3006 (*max_ptr) = method_oop->max_locals();
3007 return JVMTI_ERROR_NONE;
3008 } /* end GetMaxLocals */
3009
3010
3011 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3012 // size_ptr - pre-checked for NULL
3013 jvmtiError
3014 JvmtiEnv::GetArgumentsSize(Method* method_oop, jint* size_ptr) {
3015 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3016 // get size of arguments
3017
3018 (*size_ptr) = method_oop->size_of_parameters();
3019 return JVMTI_ERROR_NONE;
3020 } /* end GetArgumentsSize */
3021
3022
3023 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3024 // entry_count_ptr - pre-checked for NULL
3025 // table_ptr - pre-checked for NULL
3026 jvmtiError
3027 JvmtiEnv::GetLineNumberTable(Method* method_oop, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
3028 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3029 if (!method_oop->has_linenumber_table()) {
3030 return (JVMTI_ERROR_ABSENT_INFORMATION);
3031 }
3032
3033 // The line number table is compressed so we don't know how big it is until decompressed.
3034 // Decompression is really fast so we just do it twice.
3035
3036 // Compute size of table
3037 jint num_entries = 0;
3038 CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
3039 while (stream.read_pair()) {
3040 num_entries++;
3041 }
3042 jvmtiLineNumberEntry *jvmti_table =
3043 (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
3044
3045 // Fill jvmti table
3046 if (num_entries > 0) {
3047 int index = 0;
3048 CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
3049 while (stream.read_pair()) {
3050 jvmti_table[index].start_location = (jlocation) stream.bci();
3051 jvmti_table[index].line_number = (jint) stream.line();
3052 index++;
3053 }
3054 assert(index == num_entries, "sanity check");
3055 }
3056
3057 // Set up results
3058 (*entry_count_ptr) = num_entries;
3059 (*table_ptr) = jvmti_table;
3060
3061 return JVMTI_ERROR_NONE;
3062 } /* end GetLineNumberTable */
3063
3064
3065 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3066 // start_location_ptr - pre-checked for NULL
3067 // end_location_ptr - pre-checked for NULL
3068 jvmtiError
3069 JvmtiEnv::GetMethodLocation(Method* method_oop, jlocation* start_location_ptr, jlocation* end_location_ptr) {
3070
3071 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3072 // get start and end location
3073 (*end_location_ptr) = (jlocation) (method_oop->code_size() - 1);
3074 if (method_oop->code_size() == 0) {
3075 // there is no code so there is no start location
3076 (*start_location_ptr) = (jlocation)(-1);
3077 } else {
3078 (*start_location_ptr) = (jlocation)(0);
3079 }
3080
3081 return JVMTI_ERROR_NONE;
3082 } /* end GetMethodLocation */
3083
3084
3085 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3086 // entry_count_ptr - pre-checked for NULL
3087 // table_ptr - pre-checked for NULL
3088 jvmtiError
3089 JvmtiEnv::GetLocalVariableTable(Method* method_oop, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
3090
3091 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3092 JavaThread* current_thread = JavaThread::current();
3093
3094 // does the klass have any local variable information?
3095 InstanceKlass* ik = method_oop->method_holder();
3096 if (!ik->access_flags().has_localvariable_table()) {
3097 return (JVMTI_ERROR_ABSENT_INFORMATION);
3098 }
3099
3100 ConstantPool* constants = method_oop->constants();
3101 NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
3102
3103 // in the vm localvariable table representation, 6 consecutive elements in the table
3104 // represent a 6-tuple of shorts
3105 // [start_pc, length, name_index, descriptor_index, signature_index, index]
3106 jint num_entries = method_oop->localvariable_table_length();
3107 jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
3108 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
3109
3110 if (num_entries > 0) {
3111 LocalVariableTableElement* table = method_oop->localvariable_table_start();
3112 for (int i = 0; i < num_entries; i++) {
3113 // get the 5 tuple information from the vm table
3114 jlocation start_location = (jlocation) table[i].start_bci;
3115 jint length = (jint) table[i].length;
3116 int name_index = (int) table[i].name_cp_index;
3117 int signature_index = (int) table[i].descriptor_cp_index;
3118 int generic_signature_index = (int) table[i].signature_cp_index;
3119 jint slot = (jint) table[i].slot;
3120
3121 // get utf8 name and signature
3122 char *name_buf = NULL;
3123 char *sig_buf = NULL;
3124 char *gen_sig_buf = NULL;
3125 {
3126 ResourceMark rm(current_thread);
3127
3128 const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
3129 name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
3130 strcpy(name_buf, utf8_name);
3131
3132 const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
3133 sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
3134 strcpy(sig_buf, utf8_signature);
3135
3136 if (generic_signature_index > 0) {
3137 const char *utf8_gen_sign = (const char *)
3138 constants->symbol_at(generic_signature_index)->as_utf8();
3139 gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
3140 strcpy(gen_sig_buf, utf8_gen_sign);
3141 }
3142 }
3143
3144 // fill in the jvmti local variable table
3145 jvmti_table[i].start_location = start_location;
3146 jvmti_table[i].length = length;
3147 jvmti_table[i].name = name_buf;
3148 jvmti_table[i].signature = sig_buf;
3149 jvmti_table[i].generic_signature = gen_sig_buf;
3150 jvmti_table[i].slot = slot;
3151 }
3152 }
3153
3154 // set results
3155 (*entry_count_ptr) = num_entries;
3156 (*table_ptr) = jvmti_table;
3157
3158 return JVMTI_ERROR_NONE;
3159 } /* end GetLocalVariableTable */
3160
3161
3162 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3163 // bytecode_count_ptr - pre-checked for NULL
3164 // bytecodes_ptr - pre-checked for NULL
3165 jvmtiError
3166 JvmtiEnv::GetBytecodes(Method* method_oop, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
3167 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3168
3169 HandleMark hm;
3170 methodHandle method(Thread::current(), method_oop);
3171 jint size = (jint)method->code_size();
3172 jvmtiError err = allocate(size, bytecodes_ptr);
3173 if (err != JVMTI_ERROR_NONE) {
3174 return err;
3175 }
3176
3177 (*bytecode_count_ptr) = size;
3178 // get byte codes
3179 JvmtiClassFileReconstituter::copy_bytecodes(method, *bytecodes_ptr);
3180
3181 return JVMTI_ERROR_NONE;
3182 } /* end GetBytecodes */
3183
3184
3185 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3186 // is_native_ptr - pre-checked for NULL
3187 jvmtiError
3188 JvmtiEnv::IsMethodNative(Method* method_oop, jboolean* is_native_ptr) {
3189 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3190 (*is_native_ptr) = method_oop->is_native();
3191 return JVMTI_ERROR_NONE;
3192 } /* end IsMethodNative */
3193
3194
3195 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3196 // is_synthetic_ptr - pre-checked for NULL
3197 jvmtiError
3198 JvmtiEnv::IsMethodSynthetic(Method* method_oop, jboolean* is_synthetic_ptr) {
3199 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
3200 (*is_synthetic_ptr) = method_oop->is_synthetic();
3201 return JVMTI_ERROR_NONE;
3202 } /* end IsMethodSynthetic */
3203
3204
3205 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
3206 // is_obsolete_ptr - pre-checked for NULL
3207 jvmtiError
3208 JvmtiEnv::IsMethodObsolete(Method* method_oop, jboolean* is_obsolete_ptr) {
3209 if (use_version_1_0_semantics() &&
3210 get_capabilities()->can_redefine_classes == 0) {
3211 // This JvmtiEnv requested version 1.0 semantics and this function
3212 // requires the can_redefine_classes capability in version 1.0 so
3213 // we need to return an error here.
3214 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3215 }
3216
3217 if (method_oop == NULL || method_oop->is_obsolete()) {
3218 *is_obsolete_ptr = true;
3219 } else {
3220 *is_obsolete_ptr = false;
3221 }
3222 return JVMTI_ERROR_NONE;
3223 } /* end IsMethodObsolete */
3224
3225 //
3226 // Raw Monitor functions
3227 //
3228
3229 // name - pre-checked for NULL
3230 // monitor_ptr - pre-checked for NULL
3231 jvmtiError
3232 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
3233 JvmtiRawMonitor* rmonitor = new JvmtiRawMonitor(name);
3234 NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
3235
3236 *monitor_ptr = (jrawMonitorID)rmonitor;
3237
3238 return JVMTI_ERROR_NONE;
3239 } /* end CreateRawMonitor */
3240
3241
3242 // rmonitor - pre-checked for validity
3243 jvmtiError
3244 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
3245 if (Threads::number_of_threads() == 0) {
3246 // Remove this monitor from pending raw monitors list
3247 // if it has entered in onload or start phase.
3248 JvmtiPendingMonitors::destroy(rmonitor);
3249 } else {
3250 Thread* thread = Thread::current();
3251 if (rmonitor->owner() == thread) {
3252 // The caller owns this monitor which we are about to destroy.
3253 // We exit the underlying synchronization object so that the
3254 // "delete monitor" call below can work without an assertion
3255 // failure on systems that don't like destroying synchronization
3256 // objects that are locked.
3257 int r;
3258 int recursion = rmonitor->recursions();
3259 for (int i = 0; i <= recursion; i++) {
3260 r = rmonitor->raw_exit(thread);
3261 assert(r == JvmtiRawMonitor::M_OK, "raw_exit should have worked");
3262 if (r != JvmtiRawMonitor::M_OK) { // robustness
3263 return JVMTI_ERROR_INTERNAL;
3264 }
3265 }
3266 }
3267 if (rmonitor->owner() != NULL) {
3268 // The caller is trying to destroy a monitor that is locked by
3269 // someone else. While this is not forbidden by the JVMTI
3270 // spec, it will cause an assertion failure on systems that don't
3271 // like destroying synchronization objects that are locked.
3272 // We indicate a problem with the error return (and leak the
3273 // monitor's memory).
3274 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3275 }
3276 }
3277
3278 delete rmonitor;
3279
3280 return JVMTI_ERROR_NONE;
3281 } /* end DestroyRawMonitor */
3282
3283
3284 // rmonitor - pre-checked for validity
3285 jvmtiError
3286 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
3287 if (Threads::number_of_threads() == 0) {
3288 // No JavaThreads exist so JvmtiRawMonitor enter cannot be
3289 // used, add this raw monitor to the pending list.
3290 // The pending monitors will be actually entered when
3291 // the VM is setup.
3292 // See transition_pending_raw_monitors in create_vm()
3293 // in thread.cpp.
3294 JvmtiPendingMonitors::enter(rmonitor);
3295 } else {
3296 Thread* thread = Thread::current();
3297 if (thread->is_Java_thread()) {
3298 JavaThread* current_thread = (JavaThread*)thread;
3299
3300 /* Transition to thread_blocked without entering vm state */
3301 /* This is really evil. Normally you can't undo _thread_blocked */
3302 /* transitions like this because it would cause us to miss a */
3303 /* safepoint but since the thread was already in _thread_in_native */
3304 /* the thread is not leaving a safepoint safe state and it will */
3305 /* block when it tries to return from native. We can't safepoint */
3306 /* block in here because we could deadlock the vmthread. Blech. */
3307
3308 JavaThreadState state = current_thread->thread_state();
3309 assert(state == _thread_in_native, "Must be _thread_in_native");
3310 // frame should already be walkable since we are in native
3311 assert(!current_thread->has_last_Java_frame() ||
3312 current_thread->frame_anchor()->walkable(), "Must be walkable");
3313 current_thread->set_thread_state(_thread_blocked);
3314
3315 rmonitor->raw_enter(current_thread);
3316 // restore state, still at a safepoint safe state
3317 current_thread->set_thread_state(state);
3318 } else {
3319 rmonitor->raw_enter(thread);
3320 }
3321 }
3322 return JVMTI_ERROR_NONE;
3323 } /* end RawMonitorEnter */
3324
3325
3326 // rmonitor - pre-checked for validity
3327 jvmtiError
3328 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3329 jvmtiError err = JVMTI_ERROR_NONE;
3330
3331 if (Threads::number_of_threads() == 0) {
3332 // No JavaThreads exist so just remove this monitor from the pending list.
3333 // Bool value from exit is false if rmonitor is not in the list.
3334 if (!JvmtiPendingMonitors::exit(rmonitor)) {
3335 err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3336 }
3337 } else {
3338 Thread* thread = Thread::current();
3339 int r = rmonitor->raw_exit(thread);
3340 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3341 err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3342 }
3343 }
3344 return err;
3345 } /* end RawMonitorExit */
3346
3347
3348 // rmonitor - pre-checked for validity
3349 jvmtiError
3350 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3351 Thread* thread = Thread::current();
3352 int r = rmonitor->raw_wait(millis, thread);
3353
3354 switch (r) {
3355 case JvmtiRawMonitor::M_INTERRUPTED:
3356 return JVMTI_ERROR_INTERRUPT;
3357 case JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE:
3358 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3359 default:
3360 return JVMTI_ERROR_NONE;
3361 }
3362 } /* end RawMonitorWait */
3363
3364
3365 // rmonitor - pre-checked for validity
3366 jvmtiError
3367 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3368 Thread* thread = Thread::current();
3369 int r = rmonitor->raw_notify(thread);
3370
3371 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3372 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3373 }
3374 return JVMTI_ERROR_NONE;
3375 } /* end RawMonitorNotify */
3376
3377
3378 // rmonitor - pre-checked for validity
3379 jvmtiError
3380 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3381 Thread* thread = Thread::current();
3382 int r = rmonitor->raw_notifyAll(thread);
3383
3384 if (r == JvmtiRawMonitor::M_ILLEGAL_MONITOR_STATE) {
3385 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3386 }
3387 return JVMTI_ERROR_NONE;
3388 } /* end RawMonitorNotifyAll */
3389
3390
3391 //
3392 // JNI Function Interception functions
3393 //
3394
3395
3396 // function_table - pre-checked for NULL
3397 jvmtiError
3398 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3399 // Copy jni function table at safepoint.
3400 VM_JNIFunctionTableCopier copier(function_table);
3401 VMThread::execute(&copier);
3402
3403 return JVMTI_ERROR_NONE;
3404 } /* end SetJNIFunctionTable */
3405
3406
3407 // function_table - pre-checked for NULL
3408 jvmtiError
3409 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3410 *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3411 if (*function_table == NULL)
3412 return JVMTI_ERROR_OUT_OF_MEMORY;
3413 memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3414 return JVMTI_ERROR_NONE;
3415 } /* end GetJNIFunctionTable */
3416
3417
3418 //
3419 // Event Management functions
3420 //
3421
3422 jvmtiError
3423 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3424 // can only generate two event types
3425 if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3426 event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3427 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3428 }
3429
3430 // for compiled_method_load events we must check that the environment
3431 // has the can_generate_compiled_method_load_events capability.
3432 if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3433 if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3434 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3435 }
3436 return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3437 } else {
3438 return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3439 }
3440
3441 } /* end GenerateEvents */
3442
3443
3444 //
3445 // Extension Mechanism functions
3446 //
3447
3448 // extension_count_ptr - pre-checked for NULL
3449 // extensions - pre-checked for NULL
3450 jvmtiError
3451 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3452 return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3453 } /* end GetExtensionFunctions */
3454
3455
3456 // extension_count_ptr - pre-checked for NULL
3457 // extensions - pre-checked for NULL
3458 jvmtiError
3459 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3460 return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3461 } /* end GetExtensionEvents */
3462
3463
3464 // callback - NULL is a valid value, must be checked
3465 jvmtiError
3466 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3467 return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3468 } /* end SetExtensionEventCallback */
3469
3470 //
3471 // Timers functions
3472 //
3473
3474 // info_ptr - pre-checked for NULL
3475 jvmtiError
3476 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3477 os::current_thread_cpu_time_info(info_ptr);
3478 return JVMTI_ERROR_NONE;
3479 } /* end GetCurrentThreadCpuTimerInfo */
3480
3481
3482 // nanos_ptr - pre-checked for NULL
3483 jvmtiError
3484 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3485 *nanos_ptr = os::current_thread_cpu_time();
3486 return JVMTI_ERROR_NONE;
3487 } /* end GetCurrentThreadCpuTime */
3488
3489
3490 // info_ptr - pre-checked for NULL
3491 jvmtiError
3492 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3493 os::thread_cpu_time_info(info_ptr);
3494 return JVMTI_ERROR_NONE;
3495 } /* end GetThreadCpuTimerInfo */
3496
3497
3498 // Threads_lock NOT held, java_thread not protected by lock
3499 // java_thread - pre-checked
3500 // nanos_ptr - pre-checked for NULL
3501 jvmtiError
3502 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
3503 *nanos_ptr = os::thread_cpu_time(java_thread);
3504 return JVMTI_ERROR_NONE;
3505 } /* end GetThreadCpuTime */
3506
3507
3508 // info_ptr - pre-checked for NULL
3509 jvmtiError
3510 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3511 os::javaTimeNanos_info(info_ptr);
3512 return JVMTI_ERROR_NONE;
3513 } /* end GetTimerInfo */
3514
3515
3516 // nanos_ptr - pre-checked for NULL
3517 jvmtiError
3518 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3519 *nanos_ptr = os::javaTimeNanos();
3520 return JVMTI_ERROR_NONE;
3521 } /* end GetTime */
3522
3523
3524 // processor_count_ptr - pre-checked for NULL
3525 jvmtiError
3526 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3527 *processor_count_ptr = os::active_processor_count();
3528 return JVMTI_ERROR_NONE;
3529 } /* end GetAvailableProcessors */
3530
3531 jvmtiError
3532 JvmtiEnv::SetHeapSamplingInterval(jint sampling_interval) {
3533 if (sampling_interval < 0) {
3534 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3535 }
3536 ThreadHeapSampler::set_sampling_interval(sampling_interval);
3537 return JVMTI_ERROR_NONE;
3538 } /* end SetHeapSamplingInterval */
3539
3540 //
3541 // System Properties functions
3542 //
3543
3544 // count_ptr - pre-checked for NULL
3545 // property_ptr - pre-checked for NULL
3546 jvmtiError
3547 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3548 jvmtiError err = JVMTI_ERROR_NONE;
3549
3550 // Get the number of readable properties.
3551 *count_ptr = Arguments::PropertyList_readable_count(Arguments::system_properties());
3552
3553 // Allocate memory to hold the exact number of readable properties.
3554 err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3555 if (err != JVMTI_ERROR_NONE) {
3556 return err;
3557 }
3558 int readable_count = 0;
3559 // Loop through the system properties until all the readable properties are found.
3560 for (SystemProperty* p = Arguments::system_properties(); p != NULL && readable_count < *count_ptr; p = p->next()) {
3561 if (p->is_readable()) {
3562 const char *key = p->key();
3563 char **tmp_value = *property_ptr+readable_count;
3564 readable_count++;
3565 err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3566 if (err == JVMTI_ERROR_NONE) {
3567 strcpy(*tmp_value, key);
3568 } else {
3569 // clean up previously allocated memory.
3570 for (int j = 0; j < readable_count; j++) {
3571 Deallocate((unsigned char*)*property_ptr+j);
3572 }
3573 Deallocate((unsigned char*)property_ptr);
3574 break;
3575 }
3576 }
3577 }
3578 assert(err != JVMTI_ERROR_NONE || readable_count == *count_ptr, "Bad readable property count");
3579 return err;
3580 } /* end GetSystemProperties */
3581
3582
3583 // property - pre-checked for NULL
3584 // value_ptr - pre-checked for NULL
3585 jvmtiError
3586 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3587 jvmtiError err = JVMTI_ERROR_NONE;
3588 const char *value;
3589
3590 // Return JVMTI_ERROR_NOT_AVAILABLE if property is not readable or doesn't exist.
3591 value = Arguments::PropertyList_get_readable_value(Arguments::system_properties(), property);
3592 if (value == NULL) {
3593 err = JVMTI_ERROR_NOT_AVAILABLE;
3594 } else {
3595 err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3596 if (err == JVMTI_ERROR_NONE) {
3597 strcpy(*value_ptr, value);
3598 }
3599 }
3600 return err;
3601 } /* end GetSystemProperty */
3602
3603
3604 // property - pre-checked for NULL
3605 // value - NULL is a valid value, must be checked
3606 jvmtiError
3607 JvmtiEnv::SetSystemProperty(const char* property, const char* value_ptr) {
3608 jvmtiError err =JVMTI_ERROR_NOT_AVAILABLE;
3609
3610 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
3611 if (strcmp(property, p->key()) == 0) {
3612 if (p->set_writeable_value(value_ptr)) {
3613 err = JVMTI_ERROR_NONE;
3614 }
3615 }
3616 }
3617 return err;
3618 } /* end SetSystemProperty */