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