Wed, 06 Jan 2010 22:21:39 -0800
Merge
1 /*
2 * Copyright 2003-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
26 # include "incls/_precompiled.incl"
27 # include "incls/_jvmtiEnv.cpp.incl"
30 #define FIXLATER 0 // REMOVE this when completed.
32 // FIXLATER: hook into JvmtiTrace
33 #define TraceJVMTICalls false
35 JvmtiEnv::JvmtiEnv(jint version) : JvmtiEnvBase(version) {
36 }
38 JvmtiEnv::~JvmtiEnv() {
39 }
41 JvmtiEnv*
42 JvmtiEnv::create_a_jvmti(jint version) {
43 return new JvmtiEnv(version);
44 }
46 // VM operation class to copy jni function table at safepoint.
47 // More than one java threads or jvmti agents may be reading/
48 // modifying jni function tables. To reduce the risk of bad
49 // interaction b/w these threads it is copied at safepoint.
50 class VM_JNIFunctionTableCopier : public VM_Operation {
51 private:
52 const struct JNINativeInterface_ *_function_table;
53 public:
54 VM_JNIFunctionTableCopier(const struct JNINativeInterface_ *func_tbl) {
55 _function_table = func_tbl;
56 };
58 VMOp_Type type() const { return VMOp_JNIFunctionTableCopier; }
59 void doit() {
60 copy_jni_function_table(_function_table);
61 };
62 };
64 //
65 // Do not change the "prefix" marker below, everything above it is copied
66 // unchanged into the filled stub, everything below is controlled by the
67 // stub filler (only method bodies are carried forward, and then only for
68 // functionality still in the spec).
69 //
70 // end file prefix
72 //
73 // Memory Management functions
74 //
76 // mem_ptr - pre-checked for NULL
77 jvmtiError
78 JvmtiEnv::Allocate(jlong size, unsigned char** mem_ptr) {
79 return allocate(size, mem_ptr);
80 } /* end Allocate */
83 // mem - NULL is a valid value, must be checked
84 jvmtiError
85 JvmtiEnv::Deallocate(unsigned char* mem) {
86 return deallocate(mem);
87 } /* end Deallocate */
89 // Threads_lock NOT held, java_thread not protected by lock
90 // java_thread - pre-checked
91 // data - NULL is a valid value, must be checked
92 jvmtiError
93 JvmtiEnv::SetThreadLocalStorage(JavaThread* java_thread, const void* data) {
94 JvmtiThreadState* state = java_thread->jvmti_thread_state();
95 if (state == NULL) {
96 if (data == NULL) {
97 // leaving state unset same as data set to NULL
98 return JVMTI_ERROR_NONE;
99 }
100 // otherwise, create the state
101 state = JvmtiThreadState::state_for(java_thread);
102 if (state == NULL) {
103 return JVMTI_ERROR_THREAD_NOT_ALIVE;
104 }
105 }
106 state->env_thread_state(this)->set_agent_thread_local_storage_data((void*)data);
107 return JVMTI_ERROR_NONE;
108 } /* end SetThreadLocalStorage */
111 // Threads_lock NOT held
112 // thread - NOT pre-checked
113 // data_ptr - pre-checked for NULL
114 jvmtiError
115 JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
116 JavaThread* current_thread = JavaThread::current();
117 if (thread == NULL) {
118 JvmtiThreadState* state = current_thread->jvmti_thread_state();
119 *data_ptr = (state == NULL) ? NULL :
120 state->env_thread_state(this)->get_agent_thread_local_storage_data();
121 } else {
123 // jvmti_GetThreadLocalStorage is "in native" and doesn't transition
124 // the thread to _thread_in_vm. However, when the TLS for a thread
125 // other than the current thread is required we need to transition
126 // from native so as to resolve the jthread.
128 ThreadInVMfromNative __tiv(current_thread);
129 __ENTRY(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
130 debug_only(VMNativeEntryWrapper __vew;)
132 oop thread_oop = JNIHandles::resolve_external_guard(thread);
133 if (thread_oop == NULL) {
134 return JVMTI_ERROR_INVALID_THREAD;
135 }
136 if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
137 return JVMTI_ERROR_INVALID_THREAD;
138 }
139 JavaThread* java_thread = java_lang_Thread::thread(thread_oop);
140 if (java_thread == NULL) {
141 return JVMTI_ERROR_THREAD_NOT_ALIVE;
142 }
143 JvmtiThreadState* state = java_thread->jvmti_thread_state();
144 *data_ptr = (state == NULL) ? NULL :
145 state->env_thread_state(this)->get_agent_thread_local_storage_data();
146 }
147 return JVMTI_ERROR_NONE;
148 } /* end GetThreadLocalStorage */
150 //
151 // Class functions
152 //
154 // class_count_ptr - pre-checked for NULL
155 // classes_ptr - pre-checked for NULL
156 jvmtiError
157 JvmtiEnv::GetLoadedClasses(jint* class_count_ptr, jclass** classes_ptr) {
158 return JvmtiGetLoadedClasses::getLoadedClasses(this, class_count_ptr, classes_ptr);
159 } /* end GetLoadedClasses */
162 // initiating_loader - NULL is a valid value, must be checked
163 // class_count_ptr - pre-checked for NULL
164 // classes_ptr - pre-checked for NULL
165 jvmtiError
166 JvmtiEnv::GetClassLoaderClasses(jobject initiating_loader, jint* class_count_ptr, jclass** classes_ptr) {
167 return JvmtiGetLoadedClasses::getClassLoaderClasses(this, initiating_loader,
168 class_count_ptr, classes_ptr);
169 } /* end GetClassLoaderClasses */
171 // k_mirror - may be primitive, this must be checked
172 // is_modifiable_class_ptr - pre-checked for NULL
173 jvmtiError
174 JvmtiEnv::IsModifiableClass(oop k_mirror, jboolean* is_modifiable_class_ptr) {
175 *is_modifiable_class_ptr = VM_RedefineClasses::is_modifiable_class(k_mirror)?
176 JNI_TRUE : JNI_FALSE;
177 return JVMTI_ERROR_NONE;
178 } /* end IsModifiableClass */
180 // class_count - pre-checked to be greater than or equal to 0
181 // classes - pre-checked for NULL
182 jvmtiError
183 JvmtiEnv::RetransformClasses(jint class_count, const jclass* classes) {
184 //TODO: add locking
186 int index;
187 JavaThread* current_thread = JavaThread::current();
188 ResourceMark rm(current_thread);
190 jvmtiClassDefinition* class_definitions =
191 NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
192 NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
194 for (index = 0; index < class_count; index++) {
195 HandleMark hm(current_thread);
197 jclass jcls = classes[index];
198 oop k_mirror = JNIHandles::resolve_external_guard(jcls);
199 if (k_mirror == NULL) {
200 return JVMTI_ERROR_INVALID_CLASS;
201 }
202 if (!k_mirror->is_a(SystemDictionary::Class_klass())) {
203 return JVMTI_ERROR_INVALID_CLASS;
204 }
206 if (java_lang_Class::is_primitive(k_mirror)) {
207 return JVMTI_ERROR_UNMODIFIABLE_CLASS;
208 }
210 klassOop k_oop = java_lang_Class::as_klassOop(k_mirror);
211 KlassHandle klass(current_thread, k_oop);
213 jint status = klass->jvmti_class_status();
214 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
215 return JVMTI_ERROR_INVALID_CLASS;
216 }
217 if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
218 return JVMTI_ERROR_UNMODIFIABLE_CLASS;
219 }
221 instanceKlassHandle ikh(current_thread, k_oop);
222 if (ikh->get_cached_class_file_bytes() == NULL) {
223 // not cached, we need to reconstitute the class file from VM representation
224 constantPoolHandle constants(current_thread, ikh->constants());
225 ObjectLocker ol(constants, current_thread); // lock constant pool while we query it
227 JvmtiClassFileReconstituter reconstituter(ikh);
228 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
229 return reconstituter.get_error();
230 }
232 class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
233 class_definitions[index].class_bytes = (unsigned char*)
234 reconstituter.class_file_bytes();
235 } else {
236 // it is cached, get it from the cache
237 class_definitions[index].class_byte_count = ikh->get_cached_class_file_len();
238 class_definitions[index].class_bytes = ikh->get_cached_class_file_bytes();
239 }
240 class_definitions[index].klass = jcls;
241 }
242 VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
243 VMThread::execute(&op);
244 return (op.check_error());
245 } /* end RetransformClasses */
248 // class_count - pre-checked to be greater than or equal to 0
249 // class_definitions - pre-checked for NULL
250 jvmtiError
251 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
252 //TODO: add locking
253 VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
254 VMThread::execute(&op);
255 return (op.check_error());
256 } /* end RedefineClasses */
259 //
260 // Object functions
261 //
263 // size_ptr - pre-checked for NULL
264 jvmtiError
265 JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
266 oop mirror = JNIHandles::resolve_external_guard(object);
267 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
269 if (mirror->klass() == SystemDictionary::Class_klass()) {
270 if (!java_lang_Class::is_primitive(mirror)) {
271 mirror = java_lang_Class::as_klassOop(mirror);
272 assert(mirror != NULL, "class for non-primitive mirror must exist");
273 }
274 }
276 *size_ptr = mirror->size() * wordSize;
277 return JVMTI_ERROR_NONE;
278 } /* end GetObjectSize */
280 //
281 // Method functions
282 //
284 // prefix - NULL is a valid value, must be checked
285 jvmtiError
286 JvmtiEnv::SetNativeMethodPrefix(const char* prefix) {
287 return prefix == NULL?
288 SetNativeMethodPrefixes(0, NULL) :
289 SetNativeMethodPrefixes(1, (char**)&prefix);
290 } /* end SetNativeMethodPrefix */
293 // prefix_count - pre-checked to be greater than or equal to 0
294 // prefixes - pre-checked for NULL
295 jvmtiError
296 JvmtiEnv::SetNativeMethodPrefixes(jint prefix_count, char** prefixes) {
297 // Have to grab JVMTI thread state lock to be sure that some thread
298 // isn't accessing the prefixes at the same time we are setting them.
299 // No locks during VM bring-up.
300 if (Threads::number_of_threads() == 0) {
301 return set_native_method_prefixes(prefix_count, prefixes);
302 } else {
303 MutexLocker mu(JvmtiThreadState_lock);
304 return set_native_method_prefixes(prefix_count, prefixes);
305 }
306 } /* end SetNativeMethodPrefixes */
308 //
309 // Event Management functions
310 //
312 // callbacks - NULL is a valid value, must be checked
313 // size_of_callbacks - pre-checked to be greater than or equal to 0
314 jvmtiError
315 JvmtiEnv::SetEventCallbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks) {
316 JvmtiEventController::set_event_callbacks(this, callbacks, size_of_callbacks);
317 return JVMTI_ERROR_NONE;
318 } /* end SetEventCallbacks */
321 // event_thread - NULL is a valid value, must be checked
322 jvmtiError
323 JvmtiEnv::SetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event_type, jthread event_thread, ...) {
324 JavaThread* java_thread = NULL;
325 if (event_thread != NULL) {
326 oop thread_oop = JNIHandles::resolve_external_guard(event_thread);
327 if (thread_oop == NULL) {
328 return JVMTI_ERROR_INVALID_THREAD;
329 }
330 if (!thread_oop->is_a(SystemDictionary::Thread_klass())) {
331 return JVMTI_ERROR_INVALID_THREAD;
332 }
333 java_thread = java_lang_Thread::thread(thread_oop);
334 if (java_thread == NULL) {
335 return JVMTI_ERROR_THREAD_NOT_ALIVE;
336 }
337 }
339 // event_type must be valid
340 if (!JvmtiEventController::is_valid_event_type(event_type)) {
341 return JVMTI_ERROR_INVALID_EVENT_TYPE;
342 }
344 // global events cannot be controlled at thread level.
345 if (java_thread != NULL && JvmtiEventController::is_global_event(event_type)) {
346 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
347 }
349 bool enabled = (mode == JVMTI_ENABLE);
351 // assure that needed capabilities are present
352 if (enabled && !JvmtiUtil::has_event_capability(event_type, get_capabilities())) {
353 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
354 }
356 if (event_type == JVMTI_EVENT_CLASS_FILE_LOAD_HOOK && enabled) {
357 record_class_file_load_hook_enabled();
358 }
359 JvmtiEventController::set_user_enabled(this, java_thread, event_type, enabled);
361 return JVMTI_ERROR_NONE;
362 } /* end SetEventNotificationMode */
364 //
365 // Capability functions
366 //
368 // capabilities_ptr - pre-checked for NULL
369 jvmtiError
370 JvmtiEnv::GetPotentialCapabilities(jvmtiCapabilities* capabilities_ptr) {
371 JvmtiManageCapabilities::get_potential_capabilities(get_capabilities(),
372 get_prohibited_capabilities(),
373 capabilities_ptr);
374 return JVMTI_ERROR_NONE;
375 } /* end GetPotentialCapabilities */
378 // capabilities_ptr - pre-checked for NULL
379 jvmtiError
380 JvmtiEnv::AddCapabilities(const jvmtiCapabilities* capabilities_ptr) {
381 return JvmtiManageCapabilities::add_capabilities(get_capabilities(),
382 get_prohibited_capabilities(),
383 capabilities_ptr,
384 get_capabilities());
385 } /* end AddCapabilities */
388 // capabilities_ptr - pre-checked for NULL
389 jvmtiError
390 JvmtiEnv::RelinquishCapabilities(const jvmtiCapabilities* capabilities_ptr) {
391 JvmtiManageCapabilities::relinquish_capabilities(get_capabilities(), capabilities_ptr, get_capabilities());
392 return JVMTI_ERROR_NONE;
393 } /* end RelinquishCapabilities */
396 // capabilities_ptr - pre-checked for NULL
397 jvmtiError
398 JvmtiEnv::GetCapabilities(jvmtiCapabilities* capabilities_ptr) {
399 JvmtiManageCapabilities::copy_capabilities(get_capabilities(), capabilities_ptr);
400 return JVMTI_ERROR_NONE;
401 } /* end GetCapabilities */
403 //
404 // Class Loader Search functions
405 //
407 // segment - pre-checked for NULL
408 jvmtiError
409 JvmtiEnv::AddToBootstrapClassLoaderSearch(const char* segment) {
410 jvmtiPhase phase = get_phase();
411 if (phase == JVMTI_PHASE_ONLOAD) {
412 Arguments::append_sysclasspath(segment);
413 return JVMTI_ERROR_NONE;
414 } else if (use_version_1_0_semantics()) {
415 // This JvmtiEnv requested version 1.0 semantics and this function
416 // is only allowed in the ONLOAD phase in version 1.0 so we need to
417 // return an error here.
418 return JVMTI_ERROR_WRONG_PHASE;
419 } else if (phase == JVMTI_PHASE_LIVE) {
420 // The phase is checked by the wrapper that called this function,
421 // but this thread could be racing with the thread that is
422 // terminating the VM so we check one more time.
424 // create the zip entry
425 ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment);
426 if (zip_entry == NULL) {
427 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
428 }
430 // lock the loader
431 Thread* thread = Thread::current();
432 HandleMark hm;
433 Handle loader_lock = Handle(thread, SystemDictionary::system_loader_lock());
435 ObjectLocker ol(loader_lock, thread);
437 // add the jar file to the bootclasspath
438 if (TraceClassLoading) {
439 tty->print_cr("[Opened %s]", zip_entry->name());
440 }
441 ClassLoader::add_to_list(zip_entry);
442 return JVMTI_ERROR_NONE;
443 } else {
444 return JVMTI_ERROR_WRONG_PHASE;
445 }
447 } /* end AddToBootstrapClassLoaderSearch */
450 // segment - pre-checked for NULL
451 jvmtiError
452 JvmtiEnv::AddToSystemClassLoaderSearch(const char* segment) {
453 jvmtiPhase phase = get_phase();
455 if (phase == JVMTI_PHASE_ONLOAD) {
456 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
457 if (strcmp("java.class.path", p->key()) == 0) {
458 p->append_value(segment);
459 break;
460 }
461 }
462 return JVMTI_ERROR_NONE;
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.
467 HandleMark hm;
469 // create the zip entry (which will open the zip file and hence
470 // check that the segment is indeed a zip file).
471 ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment);
472 if (zip_entry == NULL) {
473 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
474 }
475 delete zip_entry; // no longer needed
477 // lock the loader
478 Thread* THREAD = Thread::current();
479 Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
481 ObjectLocker ol(loader, THREAD);
483 // need the path as java.lang.String
484 Handle path = java_lang_String::create_from_str(segment, THREAD);
485 if (HAS_PENDING_EXCEPTION) {
486 CLEAR_PENDING_EXCEPTION;
487 return JVMTI_ERROR_INTERNAL;
488 }
490 instanceKlassHandle loader_ik(THREAD, loader->klass());
492 // Invoke the appendToClassPathForInstrumentation method - if the method
493 // is not found it means the loader doesn't support adding to the class path
494 // in the live phase.
495 {
496 JavaValue res(T_VOID);
497 JavaCalls::call_special(&res,
498 loader,
499 loader_ik,
500 vmSymbolHandles::appendToClassPathForInstrumentation_name(),
501 vmSymbolHandles::appendToClassPathForInstrumentation_signature(),
502 path,
503 THREAD);
504 if (HAS_PENDING_EXCEPTION) {
505 symbolOop ex_name = PENDING_EXCEPTION->klass()->klass_part()->name();
506 CLEAR_PENDING_EXCEPTION;
508 if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
509 return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
510 } else {
511 return JVMTI_ERROR_INTERNAL;
512 }
513 }
514 }
516 return JVMTI_ERROR_NONE;
517 } else {
518 return JVMTI_ERROR_WRONG_PHASE;
519 }
520 } /* end AddToSystemClassLoaderSearch */
522 //
523 // General functions
524 //
526 // phase_ptr - pre-checked for NULL
527 jvmtiError
528 JvmtiEnv::GetPhase(jvmtiPhase* phase_ptr) {
529 *phase_ptr = get_phase();
530 return JVMTI_ERROR_NONE;
531 } /* end GetPhase */
534 jvmtiError
535 JvmtiEnv::DisposeEnvironment() {
536 dispose();
537 return JVMTI_ERROR_NONE;
538 } /* end DisposeEnvironment */
541 // data - NULL is a valid value, must be checked
542 jvmtiError
543 JvmtiEnv::SetEnvironmentLocalStorage(const void* data) {
544 set_env_local_storage(data);
545 return JVMTI_ERROR_NONE;
546 } /* end SetEnvironmentLocalStorage */
549 // data_ptr - pre-checked for NULL
550 jvmtiError
551 JvmtiEnv::GetEnvironmentLocalStorage(void** data_ptr) {
552 *data_ptr = (void*)get_env_local_storage();
553 return JVMTI_ERROR_NONE;
554 } /* end GetEnvironmentLocalStorage */
556 // version_ptr - pre-checked for NULL
557 jvmtiError
558 JvmtiEnv::GetVersionNumber(jint* version_ptr) {
559 *version_ptr = JVMTI_VERSION;
560 return JVMTI_ERROR_NONE;
561 } /* end GetVersionNumber */
564 // name_ptr - pre-checked for NULL
565 jvmtiError
566 JvmtiEnv::GetErrorName(jvmtiError error, char** name_ptr) {
567 if (error < JVMTI_ERROR_NONE || error > JVMTI_ERROR_MAX) {
568 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
569 }
570 const char *name = JvmtiUtil::error_name(error);
571 if (name == NULL) {
572 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
573 }
574 size_t len = strlen(name) + 1;
575 jvmtiError err = allocate(len, (unsigned char**)name_ptr);
576 if (err == JVMTI_ERROR_NONE) {
577 memcpy(*name_ptr, name, len);
578 }
579 return err;
580 } /* end GetErrorName */
583 jvmtiError
584 JvmtiEnv::SetVerboseFlag(jvmtiVerboseFlag flag, jboolean value) {
585 switch (flag) {
586 case JVMTI_VERBOSE_OTHER:
587 // ignore
588 break;
589 case JVMTI_VERBOSE_CLASS:
590 TraceClassLoading = value != 0;
591 TraceClassUnloading = value != 0;
592 break;
593 case JVMTI_VERBOSE_GC:
594 PrintGC = value != 0;
595 TraceClassUnloading = value != 0;
596 break;
597 case JVMTI_VERBOSE_JNI:
598 PrintJNIResolving = value != 0;
599 break;
600 default:
601 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
602 };
603 return JVMTI_ERROR_NONE;
604 } /* end SetVerboseFlag */
607 // format_ptr - pre-checked for NULL
608 jvmtiError
609 JvmtiEnv::GetJLocationFormat(jvmtiJlocationFormat* format_ptr) {
610 *format_ptr = JVMTI_JLOCATION_JVMBCI;
611 return JVMTI_ERROR_NONE;
612 } /* end GetJLocationFormat */
614 #ifndef JVMTI_KERNEL
616 //
617 // Thread functions
618 //
620 // Threads_lock NOT held
621 // thread - NOT pre-checked
622 // thread_state_ptr - pre-checked for NULL
623 jvmtiError
624 JvmtiEnv::GetThreadState(jthread thread, jint* thread_state_ptr) {
625 jint state;
626 oop thread_oop;
627 JavaThread* thr;
629 if (thread == NULL) {
630 thread_oop = JavaThread::current()->threadObj();
631 } else {
632 thread_oop = JNIHandles::resolve_external_guard(thread);
633 }
635 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
636 return JVMTI_ERROR_INVALID_THREAD;
637 }
639 // get most state bits
640 state = (jint)java_lang_Thread::get_thread_status(thread_oop);
642 // add more state bits
643 thr = java_lang_Thread::thread(thread_oop);
644 if (thr != NULL) {
645 JavaThreadState jts = thr->thread_state();
647 if (thr->is_being_ext_suspended()) {
648 state |= JVMTI_THREAD_STATE_SUSPENDED;
649 }
650 if (jts == _thread_in_native) {
651 state |= JVMTI_THREAD_STATE_IN_NATIVE;
652 }
653 OSThread* osThread = thr->osthread();
654 if (osThread != NULL && osThread->interrupted()) {
655 state |= JVMTI_THREAD_STATE_INTERRUPTED;
656 }
657 }
659 *thread_state_ptr = state;
660 return JVMTI_ERROR_NONE;
661 } /* end GetThreadState */
664 // thread_ptr - pre-checked for NULL
665 jvmtiError
666 JvmtiEnv::GetCurrentThread(jthread* thread_ptr) {
667 JavaThread* current_thread = JavaThread::current();
668 *thread_ptr = (jthread)JNIHandles::make_local(current_thread, current_thread->threadObj());
669 return JVMTI_ERROR_NONE;
670 } /* end GetCurrentThread */
673 // threads_count_ptr - pre-checked for NULL
674 // threads_ptr - pre-checked for NULL
675 jvmtiError
676 JvmtiEnv::GetAllThreads(jint* threads_count_ptr, jthread** threads_ptr) {
677 int nthreads = 0;
678 Handle *thread_objs = NULL;
679 ResourceMark rm;
680 HandleMark hm;
682 // enumerate threads (including agent threads)
683 ThreadsListEnumerator tle(Thread::current(), true);
684 nthreads = tle.num_threads();
685 *threads_count_ptr = nthreads;
687 if (nthreads == 0) {
688 *threads_ptr = NULL;
689 return JVMTI_ERROR_NONE;
690 }
692 thread_objs = NEW_RESOURCE_ARRAY(Handle, nthreads);
693 NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
695 for (int i=0; i < nthreads; i++) {
696 thread_objs[i] = Handle(tle.get_threadObj(i));
697 }
699 // have to make global handles outside of Threads_lock
700 jthread *jthreads = new_jthreadArray(nthreads, thread_objs);
701 NULL_CHECK(jthreads, JVMTI_ERROR_OUT_OF_MEMORY);
703 *threads_ptr = jthreads;
704 return JVMTI_ERROR_NONE;
705 } /* end GetAllThreads */
708 // Threads_lock NOT held, java_thread not protected by lock
709 // java_thread - pre-checked
710 jvmtiError
711 JvmtiEnv::SuspendThread(JavaThread* java_thread) {
712 // don't allow hidden thread suspend request.
713 if (java_thread->is_hidden_from_external_view()) {
714 return (JVMTI_ERROR_NONE);
715 }
717 {
718 MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
719 if (java_thread->is_external_suspend()) {
720 // don't allow nested external suspend requests.
721 return (JVMTI_ERROR_THREAD_SUSPENDED);
722 }
723 if (java_thread->is_exiting()) { // thread is in the process of exiting
724 return (JVMTI_ERROR_THREAD_NOT_ALIVE);
725 }
726 java_thread->set_external_suspend();
727 }
729 if (!JvmtiSuspendControl::suspend(java_thread)) {
730 // the thread was in the process of exiting
731 return (JVMTI_ERROR_THREAD_NOT_ALIVE);
732 }
733 return JVMTI_ERROR_NONE;
734 } /* end SuspendThread */
737 // request_count - pre-checked to be greater than or equal to 0
738 // request_list - pre-checked for NULL
739 // results - pre-checked for NULL
740 jvmtiError
741 JvmtiEnv::SuspendThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
742 int needSafepoint = 0; // > 0 if we need a safepoint
743 for (int i = 0; i < request_count; i++) {
744 JavaThread *java_thread = get_JavaThread(request_list[i]);
745 if (java_thread == NULL) {
746 results[i] = JVMTI_ERROR_INVALID_THREAD;
747 continue;
748 }
749 // the thread has not yet run or has exited (not on threads list)
750 if (java_thread->threadObj() == NULL) {
751 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
752 continue;
753 }
754 if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
755 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
756 continue;
757 }
758 // don't allow hidden thread suspend request.
759 if (java_thread->is_hidden_from_external_view()) {
760 results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
761 continue;
762 }
764 {
765 MutexLockerEx ml(java_thread->SR_lock(), Mutex::_no_safepoint_check_flag);
766 if (java_thread->is_external_suspend()) {
767 // don't allow nested external suspend requests.
768 results[i] = JVMTI_ERROR_THREAD_SUSPENDED;
769 continue;
770 }
771 if (java_thread->is_exiting()) { // thread is in the process of exiting
772 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
773 continue;
774 }
775 java_thread->set_external_suspend();
776 }
777 if (java_thread->thread_state() == _thread_in_native) {
778 // We need to try and suspend native threads here. Threads in
779 // other states will self-suspend on their next transition.
780 if (!JvmtiSuspendControl::suspend(java_thread)) {
781 // The thread was in the process of exiting. Force another
782 // safepoint to make sure that this thread transitions.
783 needSafepoint++;
784 results[i] = JVMTI_ERROR_THREAD_NOT_ALIVE;
785 continue;
786 }
787 } else {
788 needSafepoint++;
789 }
790 results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
791 }
792 if (needSafepoint > 0) {
793 VM_ForceSafepoint vfs;
794 VMThread::execute(&vfs);
795 }
796 // per-thread suspend results returned via results parameter
797 return JVMTI_ERROR_NONE;
798 } /* end SuspendThreadList */
801 // Threads_lock NOT held, java_thread not protected by lock
802 // java_thread - pre-checked
803 jvmtiError
804 JvmtiEnv::ResumeThread(JavaThread* java_thread) {
805 // don't allow hidden thread resume request.
806 if (java_thread->is_hidden_from_external_view()) {
807 return JVMTI_ERROR_NONE;
808 }
810 if (!java_thread->is_being_ext_suspended()) {
811 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
812 }
814 if (!JvmtiSuspendControl::resume(java_thread)) {
815 return JVMTI_ERROR_INTERNAL;
816 }
817 return JVMTI_ERROR_NONE;
818 } /* end ResumeThread */
821 // request_count - pre-checked to be greater than or equal to 0
822 // request_list - pre-checked for NULL
823 // results - pre-checked for NULL
824 jvmtiError
825 JvmtiEnv::ResumeThreadList(jint request_count, const jthread* request_list, jvmtiError* results) {
826 for (int i = 0; i < request_count; i++) {
827 JavaThread *java_thread = get_JavaThread(request_list[i]);
828 if (java_thread == NULL) {
829 results[i] = JVMTI_ERROR_INVALID_THREAD;
830 continue;
831 }
832 // don't allow hidden thread resume request.
833 if (java_thread->is_hidden_from_external_view()) {
834 results[i] = JVMTI_ERROR_NONE; // indicate successful resume
835 continue;
836 }
837 if (!java_thread->is_being_ext_suspended()) {
838 results[i] = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
839 continue;
840 }
842 if (!JvmtiSuspendControl::resume(java_thread)) {
843 results[i] = JVMTI_ERROR_INTERNAL;
844 continue;
845 }
847 results[i] = JVMTI_ERROR_NONE; // indicate successful suspend
848 }
849 // per-thread resume results returned via results parameter
850 return JVMTI_ERROR_NONE;
851 } /* end ResumeThreadList */
854 // Threads_lock NOT held, java_thread not protected by lock
855 // java_thread - pre-checked
856 jvmtiError
857 JvmtiEnv::StopThread(JavaThread* java_thread, jobject exception) {
858 oop e = JNIHandles::resolve_external_guard(exception);
859 NULL_CHECK(e, JVMTI_ERROR_NULL_POINTER);
861 JavaThread::send_async_exception(java_thread->threadObj(), e);
863 return JVMTI_ERROR_NONE;
865 } /* end StopThread */
868 // Threads_lock NOT held
869 // thread - NOT pre-checked
870 jvmtiError
871 JvmtiEnv::InterruptThread(jthread thread) {
872 oop thread_oop = JNIHandles::resolve_external_guard(thread);
873 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
874 return JVMTI_ERROR_INVALID_THREAD;
876 JavaThread* current_thread = JavaThread::current();
878 // Todo: this is a duplicate of JVM_Interrupt; share code in future
879 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
880 MutexLockerEx ml(current_thread->threadObj() == thread_oop ? NULL : Threads_lock);
881 // We need to re-resolve the java_thread, since a GC might have happened during the
882 // acquire of the lock
884 JavaThread* java_thread = java_lang_Thread::thread(JNIHandles::resolve_external_guard(thread));
885 NULL_CHECK(java_thread, JVMTI_ERROR_THREAD_NOT_ALIVE);
887 Thread::interrupt(java_thread);
889 return JVMTI_ERROR_NONE;
890 } /* end InterruptThread */
893 // Threads_lock NOT held
894 // thread - NOT pre-checked
895 // info_ptr - pre-checked for NULL
896 jvmtiError
897 JvmtiEnv::GetThreadInfo(jthread thread, jvmtiThreadInfo* info_ptr) {
898 ResourceMark rm;
899 HandleMark hm;
901 JavaThread* current_thread = JavaThread::current();
903 // if thread is NULL the current thread is used
904 oop thread_oop;
905 if (thread == NULL) {
906 thread_oop = current_thread->threadObj();
907 } else {
908 thread_oop = JNIHandles::resolve_external_guard(thread);
909 }
910 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass()))
911 return JVMTI_ERROR_INVALID_THREAD;
913 Handle thread_obj(current_thread, thread_oop);
914 typeArrayHandle name;
915 ThreadPriority priority;
916 Handle thread_group;
917 Handle context_class_loader;
918 bool is_daemon;
920 { MutexLocker mu(Threads_lock);
922 name = typeArrayHandle(current_thread, java_lang_Thread::name(thread_obj()));
923 priority = java_lang_Thread::priority(thread_obj());
924 thread_group = Handle(current_thread, java_lang_Thread::threadGroup(thread_obj()));
925 is_daemon = java_lang_Thread::is_daemon(thread_obj());
927 oop loader = java_lang_Thread::context_class_loader(thread_obj());
928 context_class_loader = Handle(current_thread, loader);
929 }
930 { const char *n;
932 if (name() != NULL) {
933 n = UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length());
934 } else {
935 n = UNICODE::as_utf8(NULL, 0);
936 }
938 info_ptr->name = (char *) jvmtiMalloc(strlen(n)+1);
939 if (info_ptr->name == NULL)
940 return JVMTI_ERROR_OUT_OF_MEMORY;
942 strcpy(info_ptr->name, n);
943 }
944 info_ptr->is_daemon = is_daemon;
945 info_ptr->priority = priority;
947 info_ptr->context_class_loader = (context_class_loader.is_null()) ? NULL :
948 jni_reference(context_class_loader);
949 info_ptr->thread_group = jni_reference(thread_group);
951 return JVMTI_ERROR_NONE;
952 } /* end GetThreadInfo */
955 // Threads_lock NOT held, java_thread not protected by lock
956 // java_thread - pre-checked
957 // owned_monitor_count_ptr - pre-checked for NULL
958 // owned_monitors_ptr - pre-checked for NULL
959 jvmtiError
960 JvmtiEnv::GetOwnedMonitorInfo(JavaThread* java_thread, jint* owned_monitor_count_ptr, jobject** owned_monitors_ptr) {
961 jvmtiError err = JVMTI_ERROR_NONE;
962 JavaThread* calling_thread = JavaThread::current();
964 // growable array of jvmti monitors info on the C-heap
965 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
966 new (ResourceObj::C_HEAP) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
968 uint32_t debug_bits = 0;
969 if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
970 err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
971 } else {
972 // JVMTI get monitors info at safepoint. Do not require target thread to
973 // be suspended.
974 VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
975 VMThread::execute(&op);
976 err = op.result();
977 }
978 jint owned_monitor_count = owned_monitors_list->length();
979 if (err == JVMTI_ERROR_NONE) {
980 if ((err = allocate(owned_monitor_count * sizeof(jobject *),
981 (unsigned char**)owned_monitors_ptr)) == JVMTI_ERROR_NONE) {
982 // copy into the returned array
983 for (int i = 0; i < owned_monitor_count; i++) {
984 (*owned_monitors_ptr)[i] =
985 ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
986 }
987 *owned_monitor_count_ptr = owned_monitor_count;
988 }
989 }
990 // clean up.
991 for (int i = 0; i < owned_monitor_count; i++) {
992 deallocate((unsigned char*)owned_monitors_list->at(i));
993 }
994 delete owned_monitors_list;
996 return err;
997 } /* end GetOwnedMonitorInfo */
1000 // Threads_lock NOT held, java_thread not protected by lock
1001 // java_thread - pre-checked
1002 // monitor_info_count_ptr - pre-checked for NULL
1003 // monitor_info_ptr - pre-checked for NULL
1004 jvmtiError
1005 JvmtiEnv::GetOwnedMonitorStackDepthInfo(JavaThread* java_thread, jint* monitor_info_count_ptr, jvmtiMonitorStackDepthInfo** monitor_info_ptr) {
1006 jvmtiError err = JVMTI_ERROR_NONE;
1007 JavaThread* calling_thread = JavaThread::current();
1009 // growable array of jvmti monitors info on the C-heap
1010 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list =
1011 new (ResourceObj::C_HEAP) GrowableArray<jvmtiMonitorStackDepthInfo*>(1, true);
1013 uint32_t debug_bits = 0;
1014 if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
1015 err = get_owned_monitors(calling_thread, java_thread, owned_monitors_list);
1016 } else {
1017 // JVMTI get owned monitors info at safepoint. Do not require target thread to
1018 // be suspended.
1019 VM_GetOwnedMonitorInfo op(this, calling_thread, java_thread, owned_monitors_list);
1020 VMThread::execute(&op);
1021 err = op.result();
1022 }
1024 jint owned_monitor_count = owned_monitors_list->length();
1025 if (err == JVMTI_ERROR_NONE) {
1026 if ((err = allocate(owned_monitor_count * sizeof(jvmtiMonitorStackDepthInfo),
1027 (unsigned char**)monitor_info_ptr)) == JVMTI_ERROR_NONE) {
1028 // copy to output array.
1029 for (int i = 0; i < owned_monitor_count; i++) {
1030 (*monitor_info_ptr)[i].monitor =
1031 ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->monitor;
1032 (*monitor_info_ptr)[i].stack_depth =
1033 ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(i))->stack_depth;
1034 }
1035 }
1036 *monitor_info_count_ptr = owned_monitor_count;
1037 }
1039 // clean up.
1040 for (int i = 0; i < owned_monitor_count; i++) {
1041 deallocate((unsigned char*)owned_monitors_list->at(i));
1042 }
1043 delete owned_monitors_list;
1045 return err;
1046 } /* end GetOwnedMonitorStackDepthInfo */
1049 // Threads_lock NOT held, java_thread not protected by lock
1050 // java_thread - pre-checked
1051 // monitor_ptr - pre-checked for NULL
1052 jvmtiError
1053 JvmtiEnv::GetCurrentContendedMonitor(JavaThread* java_thread, jobject* monitor_ptr) {
1054 jvmtiError err = JVMTI_ERROR_NONE;
1055 uint32_t debug_bits = 0;
1056 JavaThread* calling_thread = JavaThread::current();
1057 if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
1058 err = get_current_contended_monitor(calling_thread, java_thread, monitor_ptr);
1059 } else {
1060 // get contended monitor information at safepoint.
1061 VM_GetCurrentContendedMonitor op(this, calling_thread, java_thread, monitor_ptr);
1062 VMThread::execute(&op);
1063 err = op.result();
1064 }
1065 return err;
1066 } /* end GetCurrentContendedMonitor */
1069 // Threads_lock NOT held
1070 // thread - NOT pre-checked
1071 // proc - pre-checked for NULL
1072 // arg - NULL is a valid value, must be checked
1073 jvmtiError
1074 JvmtiEnv::RunAgentThread(jthread thread, jvmtiStartFunction proc, const void* arg, jint priority) {
1075 oop thread_oop = JNIHandles::resolve_external_guard(thread);
1076 if (thread_oop == NULL || !thread_oop->is_a(SystemDictionary::Thread_klass())) {
1077 return JVMTI_ERROR_INVALID_THREAD;
1078 }
1079 if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
1080 return JVMTI_ERROR_INVALID_PRIORITY;
1081 }
1083 //Thread-self
1084 JavaThread* current_thread = JavaThread::current();
1086 Handle thread_hndl(current_thread, thread_oop);
1087 {
1088 MutexLocker mu(Threads_lock); // grab Threads_lock
1090 JvmtiAgentThread *new_thread = new JvmtiAgentThread(this, proc, arg);
1092 // At this point it may be possible that no osthread was created for the
1093 // JavaThread due to lack of memory.
1094 if (new_thread == NULL || new_thread->osthread() == NULL) {
1095 if (new_thread) delete new_thread;
1096 return JVMTI_ERROR_OUT_OF_MEMORY;
1097 }
1099 java_lang_Thread::set_thread(thread_hndl(), new_thread);
1100 java_lang_Thread::set_priority(thread_hndl(), (ThreadPriority)priority);
1101 java_lang_Thread::set_daemon(thread_hndl());
1103 new_thread->set_threadObj(thread_hndl());
1104 Threads::add(new_thread);
1105 Thread::start(new_thread);
1106 } // unlock Threads_lock
1108 return JVMTI_ERROR_NONE;
1109 } /* end RunAgentThread */
1111 //
1112 // Thread Group functions
1113 //
1115 // group_count_ptr - pre-checked for NULL
1116 // groups_ptr - pre-checked for NULL
1117 jvmtiError
1118 JvmtiEnv::GetTopThreadGroups(jint* group_count_ptr, jthreadGroup** groups_ptr) {
1119 JavaThread* current_thread = JavaThread::current();
1121 // Only one top level thread group now.
1122 *group_count_ptr = 1;
1124 // Allocate memory to store global-refs to the thread groups.
1125 // Assume this area is freed by caller.
1126 *groups_ptr = (jthreadGroup *) jvmtiMalloc((sizeof(jthreadGroup)) * (*group_count_ptr));
1128 NULL_CHECK(*groups_ptr, JVMTI_ERROR_OUT_OF_MEMORY);
1130 // Convert oop to Handle, then convert Handle to global-ref.
1131 {
1132 HandleMark hm(current_thread);
1133 Handle system_thread_group(current_thread, Universe::system_thread_group());
1134 *groups_ptr[0] = jni_reference(system_thread_group);
1135 }
1137 return JVMTI_ERROR_NONE;
1138 } /* end GetTopThreadGroups */
1141 // info_ptr - pre-checked for NULL
1142 jvmtiError
1143 JvmtiEnv::GetThreadGroupInfo(jthreadGroup group, jvmtiThreadGroupInfo* info_ptr) {
1144 ResourceMark rm;
1145 HandleMark hm;
1147 JavaThread* current_thread = JavaThread::current();
1149 Handle group_obj (current_thread, JNIHandles::resolve_external_guard(group));
1150 NULL_CHECK(group_obj(), JVMTI_ERROR_INVALID_THREAD_GROUP);
1152 typeArrayHandle name;
1153 Handle parent_group;
1154 bool is_daemon;
1155 ThreadPriority max_priority;
1157 { MutexLocker mu(Threads_lock);
1159 name = typeArrayHandle(current_thread,
1160 java_lang_ThreadGroup::name(group_obj()));
1161 parent_group = Handle(current_thread, java_lang_ThreadGroup::parent(group_obj()));
1162 is_daemon = java_lang_ThreadGroup::is_daemon(group_obj());
1163 max_priority = java_lang_ThreadGroup::maxPriority(group_obj());
1164 }
1166 info_ptr->is_daemon = is_daemon;
1167 info_ptr->max_priority = max_priority;
1168 info_ptr->parent = jni_reference(parent_group);
1170 if (name() != NULL) {
1171 const char* n = UNICODE::as_utf8((jchar*) name->base(T_CHAR), name->length());
1172 info_ptr->name = (char *)jvmtiMalloc(strlen(n)+1);
1173 NULL_CHECK(info_ptr->name, JVMTI_ERROR_OUT_OF_MEMORY);
1174 strcpy(info_ptr->name, n);
1175 } else {
1176 info_ptr->name = NULL;
1177 }
1179 return JVMTI_ERROR_NONE;
1180 } /* end GetThreadGroupInfo */
1183 // thread_count_ptr - pre-checked for NULL
1184 // threads_ptr - pre-checked for NULL
1185 // group_count_ptr - pre-checked for NULL
1186 // groups_ptr - pre-checked for NULL
1187 jvmtiError
1188 JvmtiEnv::GetThreadGroupChildren(jthreadGroup group, jint* thread_count_ptr, jthread** threads_ptr, jint* group_count_ptr, jthreadGroup** groups_ptr) {
1189 JavaThread* current_thread = JavaThread::current();
1190 oop group_obj = (oop) JNIHandles::resolve_external_guard(group);
1191 NULL_CHECK(group_obj, JVMTI_ERROR_INVALID_THREAD_GROUP);
1193 Handle *thread_objs = NULL;
1194 Handle *group_objs = NULL;
1195 int nthreads = 0;
1196 int ngroups = 0;
1197 int hidden_threads = 0;
1199 ResourceMark rm;
1200 HandleMark hm;
1202 Handle group_hdl(current_thread, group_obj);
1204 { MutexLocker mu(Threads_lock);
1206 nthreads = java_lang_ThreadGroup::nthreads(group_hdl());
1207 ngroups = java_lang_ThreadGroup::ngroups(group_hdl());
1209 if (nthreads > 0) {
1210 objArrayOop threads = java_lang_ThreadGroup::threads(group_hdl());
1211 assert(nthreads <= threads->length(), "too many threads");
1212 thread_objs = NEW_RESOURCE_ARRAY(Handle,nthreads);
1213 for (int i=0, j=0; i<nthreads; i++) {
1214 oop thread_obj = threads->obj_at(i);
1215 assert(thread_obj != NULL, "thread_obj is NULL");
1216 JavaThread *javathread = java_lang_Thread::thread(thread_obj);
1217 // Filter out hidden java threads.
1218 if (javathread != NULL && javathread->is_hidden_from_external_view()) {
1219 hidden_threads++;
1220 continue;
1221 }
1222 thread_objs[j++] = Handle(current_thread, thread_obj);
1223 }
1224 nthreads -= hidden_threads;
1225 }
1226 if (ngroups > 0) {
1227 objArrayOop groups = java_lang_ThreadGroup::groups(group_hdl());
1228 assert(ngroups <= groups->length(), "too many threads");
1229 group_objs = NEW_RESOURCE_ARRAY(Handle,ngroups);
1230 for (int i=0; i<ngroups; i++) {
1231 oop group_obj = groups->obj_at(i);
1232 assert(group_obj != NULL, "group_obj != NULL");
1233 group_objs[i] = Handle(current_thread, group_obj);
1234 }
1235 }
1236 }
1238 // have to make global handles outside of Threads_lock
1239 *group_count_ptr = ngroups;
1240 *thread_count_ptr = nthreads;
1241 *threads_ptr = new_jthreadArray(nthreads, thread_objs);
1242 *groups_ptr = new_jthreadGroupArray(ngroups, group_objs);
1243 if ((nthreads > 0) && (*threads_ptr == NULL)) {
1244 return JVMTI_ERROR_OUT_OF_MEMORY;
1245 }
1246 if ((ngroups > 0) && (*groups_ptr == NULL)) {
1247 return JVMTI_ERROR_OUT_OF_MEMORY;
1248 }
1250 return JVMTI_ERROR_NONE;
1251 } /* end GetThreadGroupChildren */
1254 //
1255 // Stack Frame functions
1256 //
1258 // Threads_lock NOT held, java_thread not protected by lock
1259 // java_thread - pre-checked
1260 // max_frame_count - pre-checked to be greater than or equal to 0
1261 // frame_buffer - pre-checked for NULL
1262 // count_ptr - pre-checked for NULL
1263 jvmtiError
1264 JvmtiEnv::GetStackTrace(JavaThread* java_thread, jint start_depth, jint max_frame_count, jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1265 jvmtiError err = JVMTI_ERROR_NONE;
1266 uint32_t debug_bits = 0;
1267 if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
1268 err = get_stack_trace(java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1269 } else {
1270 // JVMTI get stack trace at safepoint. Do not require target thread to
1271 // be suspended.
1272 VM_GetStackTrace op(this, java_thread, start_depth, max_frame_count, frame_buffer, count_ptr);
1273 VMThread::execute(&op);
1274 err = op.result();
1275 }
1277 return err;
1278 } /* end GetStackTrace */
1281 // max_frame_count - pre-checked to be greater than or equal to 0
1282 // stack_info_ptr - pre-checked for NULL
1283 // thread_count_ptr - pre-checked for NULL
1284 jvmtiError
1285 JvmtiEnv::GetAllStackTraces(jint max_frame_count, jvmtiStackInfo** stack_info_ptr, jint* thread_count_ptr) {
1286 jvmtiError err = JVMTI_ERROR_NONE;
1287 JavaThread* calling_thread = JavaThread::current();
1289 // JVMTI get stack traces at safepoint.
1290 VM_GetAllStackTraces op(this, calling_thread, max_frame_count);
1291 VMThread::execute(&op);
1292 *thread_count_ptr = op.final_thread_count();
1293 *stack_info_ptr = op.stack_info();
1294 err = op.result();
1295 return err;
1296 } /* end GetAllStackTraces */
1299 // thread_count - pre-checked to be greater than or equal to 0
1300 // thread_list - pre-checked for NULL
1301 // max_frame_count - pre-checked to be greater than or equal to 0
1302 // stack_info_ptr - pre-checked for NULL
1303 jvmtiError
1304 JvmtiEnv::GetThreadListStackTraces(jint thread_count, const jthread* thread_list, jint max_frame_count, jvmtiStackInfo** stack_info_ptr) {
1305 jvmtiError err = JVMTI_ERROR_NONE;
1306 // JVMTI get stack traces at safepoint.
1307 VM_GetThreadListStackTraces op(this, thread_count, thread_list, max_frame_count);
1308 VMThread::execute(&op);
1309 err = op.result();
1310 if (err == JVMTI_ERROR_NONE) {
1311 *stack_info_ptr = op.stack_info();
1312 }
1313 return err;
1314 } /* end GetThreadListStackTraces */
1317 // Threads_lock NOT held, java_thread not protected by lock
1318 // java_thread - pre-checked
1319 // count_ptr - pre-checked for NULL
1320 jvmtiError
1321 JvmtiEnv::GetFrameCount(JavaThread* java_thread, jint* count_ptr) {
1322 jvmtiError err = JVMTI_ERROR_NONE;
1324 // retrieve or create JvmtiThreadState.
1325 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1326 if (state == NULL) {
1327 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1328 }
1329 uint32_t debug_bits = 0;
1330 if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
1331 err = get_frame_count(state, count_ptr);
1332 } else {
1333 // get java stack frame count at safepoint.
1334 VM_GetFrameCount op(this, state, count_ptr);
1335 VMThread::execute(&op);
1336 err = op.result();
1337 }
1338 return err;
1339 } /* end GetFrameCount */
1342 // Threads_lock NOT held, java_thread not protected by lock
1343 // java_thread - pre-checked
1344 jvmtiError
1345 JvmtiEnv::PopFrame(JavaThread* java_thread) {
1346 JavaThread* current_thread = JavaThread::current();
1347 HandleMark hm(current_thread);
1348 uint32_t debug_bits = 0;
1350 // retrieve or create the state
1351 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
1352 if (state == NULL) {
1353 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1354 }
1356 // Check if java_thread is fully suspended
1357 if (!is_thread_fully_suspended(java_thread, true /* wait for suspend completion */, &debug_bits)) {
1358 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1359 }
1360 // Check to see if a PopFrame was already in progress
1361 if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
1362 // Probably possible for JVMTI clients to trigger this, but the
1363 // JPDA backend shouldn't allow this to happen
1364 return JVMTI_ERROR_INTERNAL;
1365 }
1367 {
1368 // Was workaround bug
1369 // 4812902: popFrame hangs if the method is waiting at a synchronize
1370 // Catch this condition and return an error to avoid hanging.
1371 // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
1372 OSThread* osThread = java_thread->osthread();
1373 if (osThread->get_state() == MONITOR_WAIT) {
1374 return JVMTI_ERROR_OPAQUE_FRAME;
1375 }
1376 }
1378 {
1379 ResourceMark rm(current_thread);
1380 // Check if there are more than one Java frame in this thread, that the top two frames
1381 // are Java (not native) frames, and that there is no intervening VM frame
1382 int frame_count = 0;
1383 bool is_interpreted[2];
1384 intptr_t *frame_sp[2];
1385 // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
1386 for (vframeStream vfs(java_thread, true); !vfs.at_end(); vfs.next()) {
1387 methodHandle mh(current_thread, vfs.method());
1388 if (mh->is_native()) return(JVMTI_ERROR_OPAQUE_FRAME);
1389 is_interpreted[frame_count] = vfs.is_interpreted_frame();
1390 frame_sp[frame_count] = vfs.frame_id();
1391 if (++frame_count > 1) break;
1392 }
1393 if (frame_count < 2) {
1394 // We haven't found two adjacent non-native Java frames on the top.
1395 // There can be two situations here:
1396 // 1. There are no more java frames
1397 // 2. Two top java frames are separated by non-java native frames
1398 if(vframeFor(java_thread, 1) == NULL) {
1399 return JVMTI_ERROR_NO_MORE_FRAMES;
1400 } else {
1401 // Intervening non-java native or VM frames separate java frames.
1402 // Current implementation does not support this. See bug #5031735.
1403 // In theory it is possible to pop frames in such cases.
1404 return JVMTI_ERROR_OPAQUE_FRAME;
1405 }
1406 }
1408 // If any of the top 2 frames is a compiled one, need to deoptimize it
1409 for (int i = 0; i < 2; i++) {
1410 if (!is_interpreted[i]) {
1411 VM_DeoptimizeFrame op(java_thread, frame_sp[i]);
1412 VMThread::execute(&op);
1413 }
1414 }
1416 // Update the thread state to reflect that the top frame is popped
1417 // so that cur_stack_depth is maintained properly and all frameIDs
1418 // are invalidated.
1419 // The current frame will be popped later when the suspended thread
1420 // is resumed and right before returning from VM to Java.
1421 // (see call_VM_base() in assembler_<cpu>.cpp).
1423 // It's fine to update the thread state here because no JVMTI events
1424 // shall be posted for this PopFrame.
1426 state->update_for_pop_top_frame();
1427 java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
1428 // Set pending step flag for this popframe and it is cleared when next
1429 // step event is posted.
1430 state->set_pending_step_for_popframe();
1431 }
1433 return JVMTI_ERROR_NONE;
1434 } /* end PopFrame */
1437 // Threads_lock NOT held, java_thread not protected by lock
1438 // java_thread - pre-checked
1439 // java_thread - unchecked
1440 // depth - pre-checked as non-negative
1441 // method_ptr - pre-checked for NULL
1442 // location_ptr - pre-checked for NULL
1443 jvmtiError
1444 JvmtiEnv::GetFrameLocation(JavaThread* java_thread, jint depth, jmethodID* method_ptr, jlocation* location_ptr) {
1445 jvmtiError err = JVMTI_ERROR_NONE;
1446 uint32_t debug_bits = 0;
1448 if (is_thread_fully_suspended(java_thread, true, &debug_bits)) {
1449 err = get_frame_location(java_thread, depth, method_ptr, location_ptr);
1450 } else {
1451 // JVMTI get java stack frame location at safepoint.
1452 VM_GetFrameLocation op(this, java_thread, depth, method_ptr, location_ptr);
1453 VMThread::execute(&op);
1454 err = op.result();
1455 }
1456 return err;
1457 } /* end GetFrameLocation */
1460 // Threads_lock NOT held, java_thread not protected by lock
1461 // java_thread - pre-checked
1462 // java_thread - unchecked
1463 // depth - pre-checked as non-negative
1464 jvmtiError
1465 JvmtiEnv::NotifyFramePop(JavaThread* java_thread, jint depth) {
1466 ResourceMark rm;
1467 uint32_t debug_bits = 0;
1469 JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
1470 if (state == NULL) {
1471 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1472 }
1474 if (!JvmtiEnv::is_thread_fully_suspended(java_thread, true, &debug_bits)) {
1475 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1476 }
1478 if (TraceJVMTICalls) {
1479 JvmtiSuspendControl::print();
1480 }
1482 vframe *vf = vframeFor(java_thread, depth);
1483 if (vf == NULL) {
1484 return JVMTI_ERROR_NO_MORE_FRAMES;
1485 }
1487 if (!vf->is_java_frame() || ((javaVFrame*) vf)->method()->is_native()) {
1488 return JVMTI_ERROR_OPAQUE_FRAME;
1489 }
1491 assert(vf->frame_pointer() != NULL, "frame pointer mustn't be NULL");
1493 int frame_number = state->count_frames() - depth;
1494 state->env_thread_state(this)->set_frame_pop(frame_number);
1496 return JVMTI_ERROR_NONE;
1497 } /* end NotifyFramePop */
1500 //
1501 // Force Early Return functions
1502 //
1504 // Threads_lock NOT held, java_thread not protected by lock
1505 // java_thread - pre-checked
1506 jvmtiError
1507 JvmtiEnv::ForceEarlyReturnObject(JavaThread* java_thread, jobject value) {
1508 jvalue val;
1509 val.l = value;
1510 return force_early_return(java_thread, val, atos);
1511 } /* end ForceEarlyReturnObject */
1514 // Threads_lock NOT held, java_thread not protected by lock
1515 // java_thread - pre-checked
1516 jvmtiError
1517 JvmtiEnv::ForceEarlyReturnInt(JavaThread* java_thread, jint value) {
1518 jvalue val;
1519 val.i = value;
1520 return force_early_return(java_thread, val, itos);
1521 } /* end ForceEarlyReturnInt */
1524 // Threads_lock NOT held, java_thread not protected by lock
1525 // java_thread - pre-checked
1526 jvmtiError
1527 JvmtiEnv::ForceEarlyReturnLong(JavaThread* java_thread, jlong value) {
1528 jvalue val;
1529 val.j = value;
1530 return force_early_return(java_thread, val, ltos);
1531 } /* end ForceEarlyReturnLong */
1534 // Threads_lock NOT held, java_thread not protected by lock
1535 // java_thread - pre-checked
1536 jvmtiError
1537 JvmtiEnv::ForceEarlyReturnFloat(JavaThread* java_thread, jfloat value) {
1538 jvalue val;
1539 val.f = value;
1540 return force_early_return(java_thread, val, ftos);
1541 } /* end ForceEarlyReturnFloat */
1544 // Threads_lock NOT held, java_thread not protected by lock
1545 // java_thread - pre-checked
1546 jvmtiError
1547 JvmtiEnv::ForceEarlyReturnDouble(JavaThread* java_thread, jdouble value) {
1548 jvalue val;
1549 val.d = value;
1550 return force_early_return(java_thread, val, dtos);
1551 } /* end ForceEarlyReturnDouble */
1554 // Threads_lock NOT held, java_thread not protected by lock
1555 // java_thread - pre-checked
1556 jvmtiError
1557 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
1558 jvalue val;
1559 val.j = 0L;
1560 return force_early_return(java_thread, val, vtos);
1561 } /* end ForceEarlyReturnVoid */
1564 //
1565 // Heap functions
1566 //
1568 // klass - NULL is a valid value, must be checked
1569 // initial_object - NULL is a valid value, must be checked
1570 // callbacks - pre-checked for NULL
1571 // user_data - NULL is a valid value, must be checked
1572 jvmtiError
1573 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1574 // check klass if provided
1575 klassOop k_oop = NULL;
1576 if (klass != NULL) {
1577 oop k_mirror = JNIHandles::resolve_external_guard(klass);
1578 if (k_mirror == NULL) {
1579 return JVMTI_ERROR_INVALID_CLASS;
1580 }
1581 if (java_lang_Class::is_primitive(k_mirror)) {
1582 return JVMTI_ERROR_NONE;
1583 }
1584 k_oop = java_lang_Class::as_klassOop(k_mirror);
1585 if (k_oop == NULL) {
1586 return JVMTI_ERROR_INVALID_CLASS;
1587 }
1588 }
1590 Thread *thread = Thread::current();
1591 HandleMark hm(thread);
1592 KlassHandle kh (thread, k_oop);
1594 TraceTime t("FollowReferences", TraceJVMTIObjectTagging);
1595 JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, kh, initial_object, callbacks, user_data);
1596 return JVMTI_ERROR_NONE;
1597 } /* end FollowReferences */
1600 // klass - NULL is a valid value, must be checked
1601 // callbacks - pre-checked for NULL
1602 // user_data - NULL is a valid value, must be checked
1603 jvmtiError
1604 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1605 // check klass if provided
1606 klassOop k_oop = NULL;
1607 if (klass != NULL) {
1608 oop k_mirror = JNIHandles::resolve_external_guard(klass);
1609 if (k_mirror == NULL) {
1610 return JVMTI_ERROR_INVALID_CLASS;
1611 }
1612 if (java_lang_Class::is_primitive(k_mirror)) {
1613 return JVMTI_ERROR_NONE;
1614 }
1615 k_oop = java_lang_Class::as_klassOop(k_mirror);
1616 if (k_oop == NULL) {
1617 return JVMTI_ERROR_INVALID_CLASS;
1618 }
1619 }
1621 Thread *thread = Thread::current();
1622 HandleMark hm(thread);
1623 KlassHandle kh (thread, k_oop);
1625 TraceTime t("IterateThroughHeap", TraceJVMTIObjectTagging);
1626 JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, kh, callbacks, user_data);
1627 return JVMTI_ERROR_NONE;
1628 } /* end IterateThroughHeap */
1631 // tag_ptr - pre-checked for NULL
1632 jvmtiError
1633 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1634 oop o = JNIHandles::resolve_external_guard(object);
1635 NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1636 *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1637 return JVMTI_ERROR_NONE;
1638 } /* end GetTag */
1641 jvmtiError
1642 JvmtiEnv::SetTag(jobject object, jlong tag) {
1643 oop o = JNIHandles::resolve_external_guard(object);
1644 NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1645 JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1646 return JVMTI_ERROR_NONE;
1647 } /* end SetTag */
1650 // tag_count - pre-checked to be greater than or equal to 0
1651 // tags - pre-checked for NULL
1652 // count_ptr - pre-checked for NULL
1653 // object_result_ptr - NULL is a valid value, must be checked
1654 // tag_result_ptr - NULL is a valid value, must be checked
1655 jvmtiError
1656 JvmtiEnv::GetObjectsWithTags(jint tag_count, const jlong* tags, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
1657 TraceTime t("GetObjectsWithTags", TraceJVMTIObjectTagging);
1658 return JvmtiTagMap::tag_map_for(this)->get_objects_with_tags((jlong*)tags, tag_count, count_ptr, object_result_ptr, tag_result_ptr);
1659 } /* end GetObjectsWithTags */
1662 jvmtiError
1663 JvmtiEnv::ForceGarbageCollection() {
1664 Universe::heap()->collect(GCCause::_jvmti_force_gc);
1665 return JVMTI_ERROR_NONE;
1666 } /* end ForceGarbageCollection */
1669 //
1670 // Heap (1.0) functions
1671 //
1673 // object_reference_callback - pre-checked for NULL
1674 // user_data - NULL is a valid value, must be checked
1675 jvmtiError
1676 JvmtiEnv::IterateOverObjectsReachableFromObject(jobject object, jvmtiObjectReferenceCallback object_reference_callback, const void* user_data) {
1677 oop o = JNIHandles::resolve_external_guard(object);
1678 NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1679 JvmtiTagMap::tag_map_for(this)->iterate_over_objects_reachable_from_object(object, object_reference_callback, user_data);
1680 return JVMTI_ERROR_NONE;
1681 } /* end IterateOverObjectsReachableFromObject */
1684 // heap_root_callback - NULL is a valid value, must be checked
1685 // stack_ref_callback - NULL is a valid value, must be checked
1686 // object_ref_callback - NULL is a valid value, must be checked
1687 // user_data - NULL is a valid value, must be checked
1688 jvmtiError
1689 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1690 TraceTime t("IterateOverReachableObjects", TraceJVMTIObjectTagging);
1691 JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1692 return JVMTI_ERROR_NONE;
1693 } /* end IterateOverReachableObjects */
1696 // heap_object_callback - pre-checked for NULL
1697 // user_data - NULL is a valid value, must be checked
1698 jvmtiError
1699 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1700 TraceTime t("IterateOverHeap", TraceJVMTIObjectTagging);
1701 Thread *thread = Thread::current();
1702 HandleMark hm(thread);
1703 JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, KlassHandle(), heap_object_callback, user_data);
1704 return JVMTI_ERROR_NONE;
1705 } /* end IterateOverHeap */
1708 // k_mirror - may be primitive, this must be checked
1709 // heap_object_callback - pre-checked for NULL
1710 // user_data - NULL is a valid value, must be checked
1711 jvmtiError
1712 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1713 if (java_lang_Class::is_primitive(k_mirror)) {
1714 // DO PRIMITIVE CLASS PROCESSING
1715 return JVMTI_ERROR_NONE;
1716 }
1717 klassOop k_oop = java_lang_Class::as_klassOop(k_mirror);
1718 if (k_oop == NULL) {
1719 return JVMTI_ERROR_INVALID_CLASS;
1720 }
1721 Thread *thread = Thread::current();
1722 HandleMark hm(thread);
1723 KlassHandle klass (thread, k_oop);
1724 TraceTime t("IterateOverInstancesOfClass", TraceJVMTIObjectTagging);
1725 JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
1726 return JVMTI_ERROR_NONE;
1727 } /* end IterateOverInstancesOfClass */
1730 //
1731 // Local Variable functions
1732 //
1734 // Threads_lock NOT held, java_thread not protected by lock
1735 // java_thread - pre-checked
1736 // java_thread - unchecked
1737 // depth - pre-checked as non-negative
1738 // value_ptr - pre-checked for NULL
1739 jvmtiError
1740 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {
1741 JavaThread* current_thread = JavaThread::current();
1742 // rm object is created to clean up the javaVFrame created in
1743 // doit_prologue(), but after doit() is finished with it.
1744 ResourceMark rm(current_thread);
1746 VM_GetOrSetLocal op(java_thread, current_thread, depth, slot);
1747 VMThread::execute(&op);
1748 jvmtiError err = op.result();
1749 if (err != JVMTI_ERROR_NONE) {
1750 return err;
1751 } else {
1752 *value_ptr = op.value().l;
1753 return JVMTI_ERROR_NONE;
1754 }
1755 } /* end GetLocalObject */
1758 // Threads_lock NOT held, java_thread not protected by lock
1759 // java_thread - pre-checked
1760 // java_thread - unchecked
1761 // depth - pre-checked as non-negative
1762 // value_ptr - pre-checked for NULL
1763 jvmtiError
1764 JvmtiEnv::GetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint* value_ptr) {
1765 // rm object is created to clean up the javaVFrame created in
1766 // doit_prologue(), but after doit() is finished with it.
1767 ResourceMark rm;
1769 VM_GetOrSetLocal op(java_thread, depth, slot, T_INT);
1770 VMThread::execute(&op);
1771 *value_ptr = op.value().i;
1772 return op.result();
1773 } /* end GetLocalInt */
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::GetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong* value_ptr) {
1783 // rm object is created to clean up the javaVFrame created in
1784 // doit_prologue(), but after doit() is finished with it.
1785 ResourceMark rm;
1787 VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG);
1788 VMThread::execute(&op);
1789 *value_ptr = op.value().j;
1790 return op.result();
1791 } /* end GetLocalLong */
1794 // Threads_lock NOT held, java_thread not protected by lock
1795 // java_thread - pre-checked
1796 // java_thread - unchecked
1797 // depth - pre-checked as non-negative
1798 // value_ptr - pre-checked for NULL
1799 jvmtiError
1800 JvmtiEnv::GetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat* value_ptr) {
1801 // rm object is created to clean up the javaVFrame created in
1802 // doit_prologue(), but after doit() is finished with it.
1803 ResourceMark rm;
1805 VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT);
1806 VMThread::execute(&op);
1807 *value_ptr = op.value().f;
1808 return op.result();
1809 } /* end GetLocalFloat */
1812 // Threads_lock NOT held, java_thread not protected by lock
1813 // java_thread - pre-checked
1814 // java_thread - unchecked
1815 // depth - pre-checked as non-negative
1816 // value_ptr - pre-checked for NULL
1817 jvmtiError
1818 JvmtiEnv::GetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble* value_ptr) {
1819 // rm object is created to clean up the javaVFrame created in
1820 // doit_prologue(), but after doit() is finished with it.
1821 ResourceMark rm;
1823 VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE);
1824 VMThread::execute(&op);
1825 *value_ptr = op.value().d;
1826 return op.result();
1827 } /* end GetLocalDouble */
1830 // Threads_lock NOT held, java_thread not protected by lock
1831 // java_thread - pre-checked
1832 // java_thread - unchecked
1833 // depth - pre-checked as non-negative
1834 jvmtiError
1835 JvmtiEnv::SetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject value) {
1836 // rm object is created to clean up the javaVFrame created in
1837 // doit_prologue(), but after doit() is finished with it.
1838 ResourceMark rm;
1839 jvalue val;
1840 val.l = value;
1841 VM_GetOrSetLocal op(java_thread, depth, slot, T_OBJECT, val);
1842 VMThread::execute(&op);
1843 return op.result();
1844 } /* end SetLocalObject */
1847 // Threads_lock NOT held, java_thread not protected by lock
1848 // java_thread - pre-checked
1849 // java_thread - unchecked
1850 // depth - pre-checked as non-negative
1851 jvmtiError
1852 JvmtiEnv::SetLocalInt(JavaThread* java_thread, jint depth, jint slot, jint value) {
1853 // rm object is created to clean up the javaVFrame created in
1854 // doit_prologue(), but after doit() is finished with it.
1855 ResourceMark rm;
1856 jvalue val;
1857 val.i = value;
1858 VM_GetOrSetLocal op(java_thread, depth, slot, T_INT, val);
1859 VMThread::execute(&op);
1860 return op.result();
1861 } /* end SetLocalInt */
1864 // Threads_lock NOT held, java_thread not protected by lock
1865 // java_thread - pre-checked
1866 // java_thread - unchecked
1867 // depth - pre-checked as non-negative
1868 jvmtiError
1869 JvmtiEnv::SetLocalLong(JavaThread* java_thread, jint depth, jint slot, jlong value) {
1870 // rm object is created to clean up the javaVFrame created in
1871 // doit_prologue(), but after doit() is finished with it.
1872 ResourceMark rm;
1873 jvalue val;
1874 val.j = value;
1875 VM_GetOrSetLocal op(java_thread, depth, slot, T_LONG, val);
1876 VMThread::execute(&op);
1877 return op.result();
1878 } /* end SetLocalLong */
1881 // Threads_lock NOT held, java_thread not protected by lock
1882 // java_thread - pre-checked
1883 // java_thread - unchecked
1884 // depth - pre-checked as non-negative
1885 jvmtiError
1886 JvmtiEnv::SetLocalFloat(JavaThread* java_thread, jint depth, jint slot, jfloat value) {
1887 // rm object is created to clean up the javaVFrame created in
1888 // doit_prologue(), but after doit() is finished with it.
1889 ResourceMark rm;
1890 jvalue val;
1891 val.f = value;
1892 VM_GetOrSetLocal op(java_thread, depth, slot, T_FLOAT, val);
1893 VMThread::execute(&op);
1894 return op.result();
1895 } /* end SetLocalFloat */
1898 // Threads_lock NOT held, java_thread not protected by lock
1899 // java_thread - pre-checked
1900 // java_thread - unchecked
1901 // depth - pre-checked as non-negative
1902 jvmtiError
1903 JvmtiEnv::SetLocalDouble(JavaThread* java_thread, jint depth, jint slot, jdouble value) {
1904 // rm object is created to clean up the javaVFrame created in
1905 // doit_prologue(), but after doit() is finished with it.
1906 ResourceMark rm;
1907 jvalue val;
1908 val.d = value;
1909 VM_GetOrSetLocal op(java_thread, depth, slot, T_DOUBLE, val);
1910 VMThread::execute(&op);
1911 return op.result();
1912 } /* end SetLocalDouble */
1915 //
1916 // Breakpoint functions
1917 //
1919 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
1920 jvmtiError
1921 JvmtiEnv::SetBreakpoint(methodOop method_oop, jlocation location) {
1922 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
1923 if (location < 0) { // simple invalid location check first
1924 return JVMTI_ERROR_INVALID_LOCATION;
1925 }
1926 // verify that the breakpoint is not past the end of the method
1927 if (location >= (jlocation) method_oop->code_size()) {
1928 return JVMTI_ERROR_INVALID_LOCATION;
1929 }
1931 ResourceMark rm;
1932 JvmtiBreakpoint bp(method_oop, location);
1933 JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
1934 if (jvmti_breakpoints.set(bp) == JVMTI_ERROR_DUPLICATE)
1935 return JVMTI_ERROR_DUPLICATE;
1937 if (TraceJVMTICalls) {
1938 jvmti_breakpoints.print();
1939 }
1941 return JVMTI_ERROR_NONE;
1942 } /* end SetBreakpoint */
1945 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
1946 jvmtiError
1947 JvmtiEnv::ClearBreakpoint(methodOop method_oop, jlocation location) {
1948 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
1950 if (location < 0) { // simple invalid location check first
1951 return JVMTI_ERROR_INVALID_LOCATION;
1952 }
1954 // verify that the breakpoint is not past the end of the method
1955 if (location >= (jlocation) method_oop->code_size()) {
1956 return JVMTI_ERROR_INVALID_LOCATION;
1957 }
1959 JvmtiBreakpoint bp(method_oop, location);
1961 JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
1962 if (jvmti_breakpoints.clear(bp) == JVMTI_ERROR_NOT_FOUND)
1963 return JVMTI_ERROR_NOT_FOUND;
1965 if (TraceJVMTICalls) {
1966 jvmti_breakpoints.print();
1967 }
1969 return JVMTI_ERROR_NONE;
1970 } /* end ClearBreakpoint */
1973 //
1974 // Watched Field functions
1975 //
1977 jvmtiError
1978 JvmtiEnv::SetFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
1979 // make sure we haven't set this watch before
1980 if (fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_DUPLICATE;
1981 fdesc_ptr->set_is_field_access_watched(true);
1982 update_klass_field_access_flag(fdesc_ptr);
1984 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, true);
1986 return JVMTI_ERROR_NONE;
1987 } /* end SetFieldAccessWatch */
1990 jvmtiError
1991 JvmtiEnv::ClearFieldAccessWatch(fieldDescriptor* fdesc_ptr) {
1992 // make sure we have a watch to clear
1993 if (!fdesc_ptr->is_field_access_watched()) return JVMTI_ERROR_NOT_FOUND;
1994 fdesc_ptr->set_is_field_access_watched(false);
1995 update_klass_field_access_flag(fdesc_ptr);
1997 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_ACCESS, false);
1999 return JVMTI_ERROR_NONE;
2000 } /* end ClearFieldAccessWatch */
2003 jvmtiError
2004 JvmtiEnv::SetFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2005 // make sure we haven't set this watch before
2006 if (fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_DUPLICATE;
2007 fdesc_ptr->set_is_field_modification_watched(true);
2008 update_klass_field_access_flag(fdesc_ptr);
2010 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, true);
2012 return JVMTI_ERROR_NONE;
2013 } /* end SetFieldModificationWatch */
2016 jvmtiError
2017 JvmtiEnv::ClearFieldModificationWatch(fieldDescriptor* fdesc_ptr) {
2018 // make sure we have a watch to clear
2019 if (!fdesc_ptr->is_field_modification_watched()) return JVMTI_ERROR_NOT_FOUND;
2020 fdesc_ptr->set_is_field_modification_watched(false);
2021 update_klass_field_access_flag(fdesc_ptr);
2023 JvmtiEventController::change_field_watch(JVMTI_EVENT_FIELD_MODIFICATION, false);
2025 return JVMTI_ERROR_NONE;
2026 } /* end ClearFieldModificationWatch */
2028 //
2029 // Class functions
2030 //
2033 // k_mirror - may be primitive, this must be checked
2034 // signature_ptr - NULL is a valid value, must be checked
2035 // generic_ptr - NULL is a valid value, must be checked
2036 jvmtiError
2037 JvmtiEnv::GetClassSignature(oop k_mirror, char** signature_ptr, char** generic_ptr) {
2038 ResourceMark rm;
2039 bool isPrimitive = java_lang_Class::is_primitive(k_mirror);
2040 klassOop k = NULL;
2041 if (!isPrimitive) {
2042 k = java_lang_Class::as_klassOop(k_mirror);
2043 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2044 }
2045 if (signature_ptr != NULL) {
2046 char* result = NULL;
2047 if (isPrimitive) {
2048 char tchar = type2char(java_lang_Class::primitive_type(k_mirror));
2049 result = (char*) jvmtiMalloc(2);
2050 result[0] = tchar;
2051 result[1] = '\0';
2052 } else {
2053 const char* class_sig = Klass::cast(k)->signature_name();
2054 result = (char *) jvmtiMalloc(strlen(class_sig)+1);
2055 strcpy(result, class_sig);
2056 }
2057 *signature_ptr = result;
2058 }
2059 if (generic_ptr != NULL) {
2060 *generic_ptr = NULL;
2061 if (!isPrimitive && Klass::cast(k)->oop_is_instance()) {
2062 symbolOop soo = instanceKlass::cast(k)->generic_signature();
2063 if (soo != NULL) {
2064 const char *gen_sig = soo->as_C_string();
2065 if (gen_sig != NULL) {
2066 char* gen_result;
2067 jvmtiError err = allocate(strlen(gen_sig) + 1,
2068 (unsigned char **)&gen_result);
2069 if (err != JVMTI_ERROR_NONE) {
2070 return err;
2071 }
2072 strcpy(gen_result, gen_sig);
2073 *generic_ptr = gen_result;
2074 }
2075 }
2076 }
2077 }
2078 return JVMTI_ERROR_NONE;
2079 } /* end GetClassSignature */
2082 // k_mirror - may be primitive, this must be checked
2083 // status_ptr - pre-checked for NULL
2084 jvmtiError
2085 JvmtiEnv::GetClassStatus(oop k_mirror, jint* status_ptr) {
2086 jint result = 0;
2087 if (java_lang_Class::is_primitive(k_mirror)) {
2088 result |= JVMTI_CLASS_STATUS_PRIMITIVE;
2089 } else {
2090 klassOop k = java_lang_Class::as_klassOop(k_mirror);
2091 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2092 result = Klass::cast(k)->jvmti_class_status();
2093 }
2094 *status_ptr = result;
2096 return JVMTI_ERROR_NONE;
2097 } /* end GetClassStatus */
2100 // k_mirror - may be primitive, this must be checked
2101 // source_name_ptr - pre-checked for NULL
2102 jvmtiError
2103 JvmtiEnv::GetSourceFileName(oop k_mirror, char** source_name_ptr) {
2104 if (java_lang_Class::is_primitive(k_mirror)) {
2105 return JVMTI_ERROR_ABSENT_INFORMATION;
2106 }
2107 klassOop k_klass = java_lang_Class::as_klassOop(k_mirror);
2108 NULL_CHECK(k_klass, JVMTI_ERROR_INVALID_CLASS);
2110 if (!Klass::cast(k_klass)->oop_is_instance()) {
2111 return JVMTI_ERROR_ABSENT_INFORMATION;
2112 }
2114 symbolOop sfnOop = instanceKlass::cast(k_klass)->source_file_name();
2115 NULL_CHECK(sfnOop, JVMTI_ERROR_ABSENT_INFORMATION);
2116 {
2117 JavaThread* current_thread = JavaThread::current();
2118 ResourceMark rm(current_thread);
2119 const char* sfncp = (const char*) sfnOop->as_C_string();
2120 *source_name_ptr = (char *) jvmtiMalloc(strlen(sfncp)+1);
2121 strcpy(*source_name_ptr, sfncp);
2122 }
2124 return JVMTI_ERROR_NONE;
2125 } /* end GetSourceFileName */
2128 // k_mirror - may be primitive, this must be checked
2129 // modifiers_ptr - pre-checked for NULL
2130 jvmtiError
2131 JvmtiEnv::GetClassModifiers(oop k_mirror, jint* modifiers_ptr) {
2132 JavaThread* current_thread = JavaThread::current();
2133 jint result = 0;
2134 if (!java_lang_Class::is_primitive(k_mirror)) {
2135 klassOop k = java_lang_Class::as_klassOop(k_mirror);
2136 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2137 assert((Klass::cast(k)->oop_is_instance() || Klass::cast(k)->oop_is_array()), "should be an instance or an array klass");
2138 result = Klass::cast(k)->compute_modifier_flags(current_thread);
2139 JavaThread* THREAD = current_thread; // pass to macros
2140 if (HAS_PENDING_EXCEPTION) {
2141 CLEAR_PENDING_EXCEPTION;
2142 return JVMTI_ERROR_INTERNAL;
2143 };
2145 // Reset the deleted ACC_SUPER bit ( deleted in compute_modifier_flags()).
2146 if(Klass::cast(k)->is_super()) {
2147 result |= JVM_ACC_SUPER;
2148 }
2149 } else {
2150 result = (JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
2151 }
2152 *modifiers_ptr = result;
2154 return JVMTI_ERROR_NONE;
2155 } /* end GetClassModifiers */
2158 // k_mirror - may be primitive, this must be checked
2159 // method_count_ptr - pre-checked for NULL
2160 // methods_ptr - pre-checked for NULL
2161 jvmtiError
2162 JvmtiEnv::GetClassMethods(oop k_mirror, jint* method_count_ptr, jmethodID** methods_ptr) {
2163 JavaThread* current_thread = JavaThread::current();
2164 HandleMark hm(current_thread);
2166 if (java_lang_Class::is_primitive(k_mirror)) {
2167 *method_count_ptr = 0;
2168 *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2169 return JVMTI_ERROR_NONE;
2170 }
2171 klassOop k = java_lang_Class::as_klassOop(k_mirror);
2172 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2174 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2175 if (!(Klass::cast(k)->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2176 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2177 }
2179 if (!Klass::cast(k)->oop_is_instance()) {
2180 *method_count_ptr = 0;
2181 *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2182 return JVMTI_ERROR_NONE;
2183 }
2184 instanceKlassHandle instanceK_h(current_thread, k);
2185 // Allocate the result and fill it in
2186 int result_length = instanceK_h->methods()->length();
2187 jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2188 int index;
2189 if (JvmtiExport::can_maintain_original_method_order()) {
2190 // Use the original method ordering indices stored in the class, so we can emit
2191 // jmethodIDs in the order they appeared in the class file
2192 for (index = 0; index < result_length; index++) {
2193 methodOop m = methodOop(instanceK_h->methods()->obj_at(index));
2194 int original_index = instanceK_h->method_ordering()->int_at(index);
2195 assert(original_index >= 0 && original_index < result_length, "invalid original method index");
2196 jmethodID id = m->jmethod_id();
2197 result_list[original_index] = id;
2198 }
2199 } else {
2200 // otherwise just copy in any order
2201 for (index = 0; index < result_length; index++) {
2202 methodOop m = methodOop(instanceK_h->methods()->obj_at(index));
2203 jmethodID id = m->jmethod_id();
2204 result_list[index] = id;
2205 }
2206 }
2207 // Fill in return value.
2208 *method_count_ptr = result_length;
2209 *methods_ptr = result_list;
2211 return JVMTI_ERROR_NONE;
2212 } /* end GetClassMethods */
2215 // k_mirror - may be primitive, this must be checked
2216 // field_count_ptr - pre-checked for NULL
2217 // fields_ptr - pre-checked for NULL
2218 jvmtiError
2219 JvmtiEnv::GetClassFields(oop k_mirror, jint* field_count_ptr, jfieldID** fields_ptr) {
2220 if (java_lang_Class::is_primitive(k_mirror)) {
2221 *field_count_ptr = 0;
2222 *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2223 return JVMTI_ERROR_NONE;
2224 }
2225 JavaThread* current_thread = JavaThread::current();
2226 HandleMark hm(current_thread);
2227 klassOop k = java_lang_Class::as_klassOop(k_mirror);
2228 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2230 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2231 if (!(Klass::cast(k)->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2232 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2233 }
2235 if (!Klass::cast(k)->oop_is_instance()) {
2236 *field_count_ptr = 0;
2237 *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2238 return JVMTI_ERROR_NONE;
2239 }
2242 instanceKlassHandle instanceK_h(current_thread, k);
2244 int result_count = 0;
2245 // First, count the fields.
2246 FilteredFieldStream flds(instanceK_h, true, true);
2247 result_count = flds.field_count();
2249 // Allocate the result and fill it in
2250 jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
2251 // The JVMTI spec requires fields in the order they occur in the class file,
2252 // this is the reverse order of what FieldStream hands out.
2253 int id_index = (result_count - 1);
2255 for (FilteredFieldStream src_st(instanceK_h, true, true); !src_st.eos(); src_st.next()) {
2256 result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
2257 instanceK_h, src_st.offset(),
2258 src_st.access_flags().is_static());
2259 }
2260 assert(id_index == -1, "just checking");
2261 // Fill in the results
2262 *field_count_ptr = result_count;
2263 *fields_ptr = result_list;
2265 return JVMTI_ERROR_NONE;
2266 } /* end GetClassFields */
2269 // k_mirror - may be primitive, this must be checked
2270 // interface_count_ptr - pre-checked for NULL
2271 // interfaces_ptr - pre-checked for NULL
2272 jvmtiError
2273 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2274 {
2275 if (java_lang_Class::is_primitive(k_mirror)) {
2276 *interface_count_ptr = 0;
2277 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2278 return JVMTI_ERROR_NONE;
2279 }
2280 JavaThread* current_thread = JavaThread::current();
2281 HandleMark hm(current_thread);
2282 klassOop k = java_lang_Class::as_klassOop(k_mirror);
2283 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2285 // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2286 if (!(Klass::cast(k)->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) ))
2287 return JVMTI_ERROR_CLASS_NOT_PREPARED;
2289 if (!Klass::cast(k)->oop_is_instance()) {
2290 *interface_count_ptr = 0;
2291 *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));
2292 return JVMTI_ERROR_NONE;
2293 }
2295 objArrayHandle interface_list(current_thread, instanceKlass::cast(k)->local_interfaces());
2296 const int result_length = (interface_list.is_null() ? 0 : interface_list->length());
2297 jclass* result_list = (jclass*) jvmtiMalloc(result_length * sizeof(jclass));
2298 for (int i_index = 0; i_index < result_length; i_index += 1) {
2299 oop oop_at = interface_list->obj_at(i_index);
2300 assert(oop_at->is_klass(), "interfaces must be klassOops");
2301 klassOop klassOop_at = klassOop(oop_at); // ???: is there a better way?
2302 assert(Klass::cast(klassOop_at)->is_interface(), "interfaces must be interfaces");
2303 oop mirror_at = Klass::cast(klassOop_at)->java_mirror();
2304 Handle handle_at = Handle(current_thread, mirror_at);
2305 result_list[i_index] = (jclass) jni_reference(handle_at);
2306 }
2307 *interface_count_ptr = result_length;
2308 *interfaces_ptr = result_list;
2309 }
2311 return JVMTI_ERROR_NONE;
2312 } /* end GetImplementedInterfaces */
2315 // k_mirror - may be primitive, this must be checked
2316 // minor_version_ptr - pre-checked for NULL
2317 // major_version_ptr - pre-checked for NULL
2318 jvmtiError
2319 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2320 if (java_lang_Class::is_primitive(k_mirror)) {
2321 return JVMTI_ERROR_ABSENT_INFORMATION;
2322 }
2323 klassOop k_oop = java_lang_Class::as_klassOop(k_mirror);
2324 Thread *thread = Thread::current();
2325 HandleMark hm(thread);
2326 KlassHandle klass(thread, k_oop);
2328 jint status = klass->jvmti_class_status();
2329 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2330 return JVMTI_ERROR_INVALID_CLASS;
2331 }
2332 if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2333 return JVMTI_ERROR_ABSENT_INFORMATION;
2334 }
2336 instanceKlassHandle ik(thread, k_oop);
2337 *minor_version_ptr = ik->minor_version();
2338 *major_version_ptr = ik->major_version();
2340 return JVMTI_ERROR_NONE;
2341 } /* end GetClassVersionNumbers */
2344 // k_mirror - may be primitive, this must be checked
2345 // constant_pool_count_ptr - pre-checked for NULL
2346 // constant_pool_byte_count_ptr - pre-checked for NULL
2347 // constant_pool_bytes_ptr - pre-checked for NULL
2348 jvmtiError
2349 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2350 if (java_lang_Class::is_primitive(k_mirror)) {
2351 return JVMTI_ERROR_ABSENT_INFORMATION;
2352 }
2354 klassOop k_oop = java_lang_Class::as_klassOop(k_mirror);
2355 Thread *thread = Thread::current();
2356 HandleMark hm(thread);
2357 ResourceMark rm(thread);
2358 KlassHandle klass(thread, k_oop);
2360 jint status = klass->jvmti_class_status();
2361 if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2362 return JVMTI_ERROR_INVALID_CLASS;
2363 }
2364 if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2365 return JVMTI_ERROR_ABSENT_INFORMATION;
2366 }
2368 instanceKlassHandle ikh(thread, k_oop);
2369 constantPoolHandle constants(thread, ikh->constants());
2370 ObjectLocker ol(constants, thread); // lock constant pool while we query it
2372 JvmtiConstantPoolReconstituter reconstituter(ikh);
2373 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2374 return reconstituter.get_error();
2375 }
2377 unsigned char *cpool_bytes;
2378 int cpool_size = reconstituter.cpool_size();
2379 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2380 return reconstituter.get_error();
2381 }
2382 jvmtiError res = allocate(cpool_size, &cpool_bytes);
2383 if (res != JVMTI_ERROR_NONE) {
2384 return res;
2385 }
2386 reconstituter.copy_cpool_bytes(cpool_bytes);
2387 if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2388 return reconstituter.get_error();
2389 }
2391 *constant_pool_count_ptr = constants->length();
2392 *constant_pool_byte_count_ptr = cpool_size;
2393 *constant_pool_bytes_ptr = cpool_bytes;
2395 return JVMTI_ERROR_NONE;
2396 } /* end GetConstantPool */
2399 // k_mirror - may be primitive, this must be checked
2400 // is_interface_ptr - pre-checked for NULL
2401 jvmtiError
2402 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2403 {
2404 bool result = false;
2405 if (!java_lang_Class::is_primitive(k_mirror)) {
2406 klassOop k = java_lang_Class::as_klassOop(k_mirror);
2407 if (k != NULL && Klass::cast(k)->is_interface()) {
2408 result = true;
2409 }
2410 }
2411 *is_interface_ptr = result;
2412 }
2414 return JVMTI_ERROR_NONE;
2415 } /* end IsInterface */
2418 // k_mirror - may be primitive, this must be checked
2419 // is_array_class_ptr - pre-checked for NULL
2420 jvmtiError
2421 JvmtiEnv::IsArrayClass(oop k_mirror, jboolean* is_array_class_ptr) {
2422 {
2423 bool result = false;
2424 if (!java_lang_Class::is_primitive(k_mirror)) {
2425 klassOop k = java_lang_Class::as_klassOop(k_mirror);
2426 if (k != NULL && Klass::cast(k)->oop_is_array()) {
2427 result = true;
2428 }
2429 }
2430 *is_array_class_ptr = result;
2431 }
2433 return JVMTI_ERROR_NONE;
2434 } /* end IsArrayClass */
2437 // k_mirror - may be primitive, this must be checked
2438 // classloader_ptr - pre-checked for NULL
2439 jvmtiError
2440 JvmtiEnv::GetClassLoader(oop k_mirror, jobject* classloader_ptr) {
2441 {
2442 if (java_lang_Class::is_primitive(k_mirror)) {
2443 *classloader_ptr = (jclass) jni_reference(Handle());
2444 return JVMTI_ERROR_NONE;
2445 }
2446 JavaThread* current_thread = JavaThread::current();
2447 HandleMark hm(current_thread);
2448 klassOop k = java_lang_Class::as_klassOop(k_mirror);
2449 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2451 oop result_oop = Klass::cast(k)->class_loader();
2452 if (result_oop == NULL) {
2453 *classloader_ptr = (jclass) jni_reference(Handle());
2454 return JVMTI_ERROR_NONE;
2455 }
2456 Handle result_handle = Handle(current_thread, result_oop);
2457 jclass result_jnihandle = (jclass) jni_reference(result_handle);
2458 *classloader_ptr = result_jnihandle;
2459 }
2460 return JVMTI_ERROR_NONE;
2461 } /* end GetClassLoader */
2464 // k_mirror - may be primitive, this must be checked
2465 // source_debug_extension_ptr - pre-checked for NULL
2466 jvmtiError
2467 JvmtiEnv::GetSourceDebugExtension(oop k_mirror, char** source_debug_extension_ptr) {
2468 {
2469 if (java_lang_Class::is_primitive(k_mirror)) {
2470 return JVMTI_ERROR_ABSENT_INFORMATION;
2471 }
2472 klassOop k = java_lang_Class::as_klassOop(k_mirror);
2473 NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2474 if (!Klass::cast(k)->oop_is_instance()) {
2475 return JVMTI_ERROR_ABSENT_INFORMATION;
2476 }
2477 symbolOop sdeOop = instanceKlass::cast(k)->source_debug_extension();
2478 NULL_CHECK(sdeOop, JVMTI_ERROR_ABSENT_INFORMATION);
2480 {
2481 JavaThread* current_thread = JavaThread::current();
2482 ResourceMark rm(current_thread);
2483 const char* sdecp = (const char*) sdeOop->as_C_string();
2484 *source_debug_extension_ptr = (char *) jvmtiMalloc(strlen(sdecp)+1);
2485 strcpy(*source_debug_extension_ptr, sdecp);
2486 }
2487 }
2489 return JVMTI_ERROR_NONE;
2490 } /* end GetSourceDebugExtension */
2492 //
2493 // Object functions
2494 //
2496 // hash_code_ptr - pre-checked for NULL
2497 jvmtiError
2498 JvmtiEnv::GetObjectHashCode(jobject object, jint* hash_code_ptr) {
2499 oop mirror = JNIHandles::resolve_external_guard(object);
2500 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
2501 NULL_CHECK(hash_code_ptr, JVMTI_ERROR_NULL_POINTER);
2503 {
2504 jint result = (jint) mirror->identity_hash();
2505 *hash_code_ptr = result;
2506 }
2507 return JVMTI_ERROR_NONE;
2508 } /* end GetObjectHashCode */
2511 // info_ptr - pre-checked for NULL
2512 jvmtiError
2513 JvmtiEnv::GetObjectMonitorUsage(jobject object, jvmtiMonitorUsage* info_ptr) {
2514 JavaThread* calling_thread = JavaThread::current();
2515 jvmtiError err = get_object_monitor_usage(calling_thread, object, info_ptr);
2516 if (err == JVMTI_ERROR_THREAD_NOT_SUSPENDED) {
2517 // Some of the critical threads were not suspended. go to a safepoint and try again
2518 VM_GetObjectMonitorUsage op(this, calling_thread, object, info_ptr);
2519 VMThread::execute(&op);
2520 err = op.result();
2521 }
2522 return err;
2523 } /* end GetObjectMonitorUsage */
2526 //
2527 // Field functions
2528 //
2530 // name_ptr - NULL is a valid value, must be checked
2531 // signature_ptr - NULL is a valid value, must be checked
2532 // generic_ptr - NULL is a valid value, must be checked
2533 jvmtiError
2534 JvmtiEnv::GetFieldName(fieldDescriptor* fdesc_ptr, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2535 JavaThread* current_thread = JavaThread::current();
2536 ResourceMark rm(current_thread);
2537 if (name_ptr == NULL) {
2538 // just don't return the name
2539 } else {
2540 const char* fieldName = fdesc_ptr->name()->as_C_string();
2541 *name_ptr = (char*) jvmtiMalloc(strlen(fieldName) + 1);
2542 if (*name_ptr == NULL)
2543 return JVMTI_ERROR_OUT_OF_MEMORY;
2544 strcpy(*name_ptr, fieldName);
2545 }
2546 if (signature_ptr== NULL) {
2547 // just don't return the signature
2548 } else {
2549 const char* fieldSignature = fdesc_ptr->signature()->as_C_string();
2550 *signature_ptr = (char*) jvmtiMalloc(strlen(fieldSignature) + 1);
2551 if (*signature_ptr == NULL)
2552 return JVMTI_ERROR_OUT_OF_MEMORY;
2553 strcpy(*signature_ptr, fieldSignature);
2554 }
2555 if (generic_ptr != NULL) {
2556 *generic_ptr = NULL;
2557 symbolOop soop = fdesc_ptr->generic_signature();
2558 if (soop != NULL) {
2559 const char* gen_sig = soop->as_C_string();
2560 if (gen_sig != NULL) {
2561 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2562 if (err != JVMTI_ERROR_NONE) {
2563 return err;
2564 }
2565 strcpy(*generic_ptr, gen_sig);
2566 }
2567 }
2568 }
2569 return JVMTI_ERROR_NONE;
2570 } /* end GetFieldName */
2573 // declaring_class_ptr - pre-checked for NULL
2574 jvmtiError
2575 JvmtiEnv::GetFieldDeclaringClass(fieldDescriptor* fdesc_ptr, jclass* declaring_class_ptr) {
2577 *declaring_class_ptr = get_jni_class_non_null(fdesc_ptr->field_holder());
2578 return JVMTI_ERROR_NONE;
2579 } /* end GetFieldDeclaringClass */
2582 // modifiers_ptr - pre-checked for NULL
2583 jvmtiError
2584 JvmtiEnv::GetFieldModifiers(fieldDescriptor* fdesc_ptr, jint* modifiers_ptr) {
2586 AccessFlags resultFlags = fdesc_ptr->access_flags();
2587 jint result = resultFlags.as_int();
2588 *modifiers_ptr = result;
2590 return JVMTI_ERROR_NONE;
2591 } /* end GetFieldModifiers */
2594 // is_synthetic_ptr - pre-checked for NULL
2595 jvmtiError
2596 JvmtiEnv::IsFieldSynthetic(fieldDescriptor* fdesc_ptr, jboolean* is_synthetic_ptr) {
2597 *is_synthetic_ptr = fdesc_ptr->is_synthetic();
2598 return JVMTI_ERROR_NONE;
2599 } /* end IsFieldSynthetic */
2602 //
2603 // Method functions
2604 //
2606 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2607 // name_ptr - NULL is a valid value, must be checked
2608 // signature_ptr - NULL is a valid value, must be checked
2609 // generic_ptr - NULL is a valid value, must be checked
2610 jvmtiError
2611 JvmtiEnv::GetMethodName(methodOop method_oop, char** name_ptr, char** signature_ptr, char** generic_ptr) {
2612 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2613 JavaThread* current_thread = JavaThread::current();
2615 ResourceMark rm(current_thread); // get the utf8 name and signature
2616 if (name_ptr == NULL) {
2617 // just don't return the name
2618 } else {
2619 const char* utf8_name = (const char *) method_oop->name()->as_utf8();
2620 *name_ptr = (char *) jvmtiMalloc(strlen(utf8_name)+1);
2621 strcpy(*name_ptr, utf8_name);
2622 }
2623 if (signature_ptr == NULL) {
2624 // just don't return the signature
2625 } else {
2626 const char* utf8_signature = (const char *) method_oop->signature()->as_utf8();
2627 *signature_ptr = (char *) jvmtiMalloc(strlen(utf8_signature) + 1);
2628 strcpy(*signature_ptr, utf8_signature);
2629 }
2631 if (generic_ptr != NULL) {
2632 *generic_ptr = NULL;
2633 symbolOop soop = method_oop->generic_signature();
2634 if (soop != NULL) {
2635 const char* gen_sig = soop->as_C_string();
2636 if (gen_sig != NULL) {
2637 jvmtiError err = allocate(strlen(gen_sig) + 1, (unsigned char **)generic_ptr);
2638 if (err != JVMTI_ERROR_NONE) {
2639 return err;
2640 }
2641 strcpy(*generic_ptr, gen_sig);
2642 }
2643 }
2644 }
2645 return JVMTI_ERROR_NONE;
2646 } /* end GetMethodName */
2649 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2650 // declaring_class_ptr - pre-checked for NULL
2651 jvmtiError
2652 JvmtiEnv::GetMethodDeclaringClass(methodOop method_oop, jclass* declaring_class_ptr) {
2653 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2654 (*declaring_class_ptr) = get_jni_class_non_null(method_oop->method_holder());
2655 return JVMTI_ERROR_NONE;
2656 } /* end GetMethodDeclaringClass */
2659 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2660 // modifiers_ptr - pre-checked for NULL
2661 jvmtiError
2662 JvmtiEnv::GetMethodModifiers(methodOop method_oop, jint* modifiers_ptr) {
2663 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2664 (*modifiers_ptr) = method_oop->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2665 return JVMTI_ERROR_NONE;
2666 } /* end GetMethodModifiers */
2669 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2670 // max_ptr - pre-checked for NULL
2671 jvmtiError
2672 JvmtiEnv::GetMaxLocals(methodOop method_oop, jint* max_ptr) {
2673 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2674 // get max stack
2675 (*max_ptr) = method_oop->max_locals();
2676 return JVMTI_ERROR_NONE;
2677 } /* end GetMaxLocals */
2680 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2681 // size_ptr - pre-checked for NULL
2682 jvmtiError
2683 JvmtiEnv::GetArgumentsSize(methodOop method_oop, jint* size_ptr) {
2684 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2685 // get size of arguments
2687 (*size_ptr) = method_oop->size_of_parameters();
2688 return JVMTI_ERROR_NONE;
2689 } /* end GetArgumentsSize */
2692 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2693 // entry_count_ptr - pre-checked for NULL
2694 // table_ptr - pre-checked for NULL
2695 jvmtiError
2696 JvmtiEnv::GetLineNumberTable(methodOop method_oop, jint* entry_count_ptr, jvmtiLineNumberEntry** table_ptr) {
2697 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2698 if (!method_oop->has_linenumber_table()) {
2699 return (JVMTI_ERROR_ABSENT_INFORMATION);
2700 }
2702 // The line number table is compressed so we don't know how big it is until decompressed.
2703 // Decompression is really fast so we just do it twice.
2705 // Compute size of table
2706 jint num_entries = 0;
2707 CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
2708 while (stream.read_pair()) {
2709 num_entries++;
2710 }
2711 jvmtiLineNumberEntry *jvmti_table =
2712 (jvmtiLineNumberEntry *)jvmtiMalloc(num_entries * (sizeof(jvmtiLineNumberEntry)));
2714 // Fill jvmti table
2715 if (num_entries > 0) {
2716 int index = 0;
2717 CompressedLineNumberReadStream stream(method_oop->compressed_linenumber_table());
2718 while (stream.read_pair()) {
2719 jvmti_table[index].start_location = (jlocation) stream.bci();
2720 jvmti_table[index].line_number = (jint) stream.line();
2721 index++;
2722 }
2723 assert(index == num_entries, "sanity check");
2724 }
2726 // Set up results
2727 (*entry_count_ptr) = num_entries;
2728 (*table_ptr) = jvmti_table;
2730 return JVMTI_ERROR_NONE;
2731 } /* end GetLineNumberTable */
2734 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2735 // start_location_ptr - pre-checked for NULL
2736 // end_location_ptr - pre-checked for NULL
2737 jvmtiError
2738 JvmtiEnv::GetMethodLocation(methodOop method_oop, jlocation* start_location_ptr, jlocation* end_location_ptr) {
2740 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2741 // get start and end location
2742 (*end_location_ptr) = (jlocation) (method_oop->code_size() - 1);
2743 if (method_oop->code_size() == 0) {
2744 // there is no code so there is no start location
2745 (*start_location_ptr) = (jlocation)(-1);
2746 } else {
2747 (*start_location_ptr) = (jlocation)(0);
2748 }
2750 return JVMTI_ERROR_NONE;
2751 } /* end GetMethodLocation */
2754 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2755 // entry_count_ptr - pre-checked for NULL
2756 // table_ptr - pre-checked for NULL
2757 jvmtiError
2758 JvmtiEnv::GetLocalVariableTable(methodOop method_oop, jint* entry_count_ptr, jvmtiLocalVariableEntry** table_ptr) {
2760 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2761 JavaThread* current_thread = JavaThread::current();
2763 // does the klass have any local variable information?
2764 instanceKlass* ik = instanceKlass::cast(method_oop->method_holder());
2765 if (!ik->access_flags().has_localvariable_table()) {
2766 return (JVMTI_ERROR_ABSENT_INFORMATION);
2767 }
2769 constantPoolOop constants = method_oop->constants();
2770 NULL_CHECK(constants, JVMTI_ERROR_ABSENT_INFORMATION);
2772 // in the vm localvariable table representation, 6 consecutive elements in the table
2773 // represent a 6-tuple of shorts
2774 // [start_pc, length, name_index, descriptor_index, signature_index, index]
2775 jint num_entries = method_oop->localvariable_table_length();
2776 jvmtiLocalVariableEntry *jvmti_table = (jvmtiLocalVariableEntry *)
2777 jvmtiMalloc(num_entries * (sizeof(jvmtiLocalVariableEntry)));
2779 if (num_entries > 0) {
2780 LocalVariableTableElement* table = method_oop->localvariable_table_start();
2781 for (int i = 0; i < num_entries; i++) {
2782 // get the 5 tuple information from the vm table
2783 jlocation start_location = (jlocation) table[i].start_bci;
2784 jint length = (jint) table[i].length;
2785 int name_index = (int) table[i].name_cp_index;
2786 int signature_index = (int) table[i].descriptor_cp_index;
2787 int generic_signature_index = (int) table[i].signature_cp_index;
2788 jint slot = (jint) table[i].slot;
2790 // get utf8 name and signature
2791 char *name_buf = NULL;
2792 char *sig_buf = NULL;
2793 char *gen_sig_buf = NULL;
2794 {
2795 ResourceMark rm(current_thread);
2797 const char *utf8_name = (const char *) constants->symbol_at(name_index)->as_utf8();
2798 name_buf = (char *) jvmtiMalloc(strlen(utf8_name)+1);
2799 strcpy(name_buf, utf8_name);
2801 const char *utf8_signature = (const char *) constants->symbol_at(signature_index)->as_utf8();
2802 sig_buf = (char *) jvmtiMalloc(strlen(utf8_signature)+1);
2803 strcpy(sig_buf, utf8_signature);
2805 if (generic_signature_index > 0) {
2806 const char *utf8_gen_sign = (const char *)
2807 constants->symbol_at(generic_signature_index)->as_utf8();
2808 gen_sig_buf = (char *) jvmtiMalloc(strlen(utf8_gen_sign)+1);
2809 strcpy(gen_sig_buf, utf8_gen_sign);
2810 }
2811 }
2813 // fill in the jvmti local variable table
2814 jvmti_table[i].start_location = start_location;
2815 jvmti_table[i].length = length;
2816 jvmti_table[i].name = name_buf;
2817 jvmti_table[i].signature = sig_buf;
2818 jvmti_table[i].generic_signature = gen_sig_buf;
2819 jvmti_table[i].slot = slot;
2820 }
2821 }
2823 // set results
2824 (*entry_count_ptr) = num_entries;
2825 (*table_ptr) = jvmti_table;
2827 return JVMTI_ERROR_NONE;
2828 } /* end GetLocalVariableTable */
2831 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2832 // bytecode_count_ptr - pre-checked for NULL
2833 // bytecodes_ptr - pre-checked for NULL
2834 jvmtiError
2835 JvmtiEnv::GetBytecodes(methodOop method_oop, jint* bytecode_count_ptr, unsigned char** bytecodes_ptr) {
2836 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2838 HandleMark hm;
2839 methodHandle method(method_oop);
2840 jint size = (jint)method->code_size();
2841 jvmtiError err = allocate(size, bytecodes_ptr);
2842 if (err != JVMTI_ERROR_NONE) {
2843 return err;
2844 }
2846 (*bytecode_count_ptr) = size;
2847 // get byte codes
2848 JvmtiClassFileReconstituter::copy_bytecodes(method, *bytecodes_ptr);
2850 return JVMTI_ERROR_NONE;
2851 } /* end GetBytecodes */
2854 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2855 // is_native_ptr - pre-checked for NULL
2856 jvmtiError
2857 JvmtiEnv::IsMethodNative(methodOop method_oop, jboolean* is_native_ptr) {
2858 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2859 (*is_native_ptr) = method_oop->is_native();
2860 return JVMTI_ERROR_NONE;
2861 } /* end IsMethodNative */
2864 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2865 // is_synthetic_ptr - pre-checked for NULL
2866 jvmtiError
2867 JvmtiEnv::IsMethodSynthetic(methodOop method_oop, jboolean* is_synthetic_ptr) {
2868 NULL_CHECK(method_oop, JVMTI_ERROR_INVALID_METHODID);
2869 (*is_synthetic_ptr) = method_oop->is_synthetic();
2870 return JVMTI_ERROR_NONE;
2871 } /* end IsMethodSynthetic */
2874 // method_oop - pre-checked for validity, but may be NULL meaning obsolete method
2875 // is_obsolete_ptr - pre-checked for NULL
2876 jvmtiError
2877 JvmtiEnv::IsMethodObsolete(methodOop method_oop, jboolean* is_obsolete_ptr) {
2878 if (use_version_1_0_semantics() &&
2879 get_capabilities()->can_redefine_classes == 0) {
2880 // This JvmtiEnv requested version 1.0 semantics and this function
2881 // requires the can_redefine_classes capability in version 1.0 so
2882 // we need to return an error here.
2883 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
2884 }
2886 if (method_oop == NULL || method_oop->is_obsolete()) {
2887 *is_obsolete_ptr = true;
2888 } else {
2889 *is_obsolete_ptr = false;
2890 }
2891 return JVMTI_ERROR_NONE;
2892 } /* end IsMethodObsolete */
2894 //
2895 // Raw Monitor functions
2896 //
2898 // name - pre-checked for NULL
2899 // monitor_ptr - pre-checked for NULL
2900 jvmtiError
2901 JvmtiEnv::CreateRawMonitor(const char* name, jrawMonitorID* monitor_ptr) {
2902 JvmtiRawMonitor* rmonitor = new JvmtiRawMonitor(name);
2903 NULL_CHECK(rmonitor, JVMTI_ERROR_OUT_OF_MEMORY);
2905 *monitor_ptr = (jrawMonitorID)rmonitor;
2907 return JVMTI_ERROR_NONE;
2908 } /* end CreateRawMonitor */
2911 // rmonitor - pre-checked for validity
2912 jvmtiError
2913 JvmtiEnv::DestroyRawMonitor(JvmtiRawMonitor * rmonitor) {
2914 if (Threads::number_of_threads() == 0) {
2915 // Remove this monitor from pending raw monitors list
2916 // if it has entered in onload or start phase.
2917 JvmtiPendingMonitors::destroy(rmonitor);
2918 } else {
2919 Thread* thread = Thread::current();
2920 if (rmonitor->is_entered(thread)) {
2921 // The caller owns this monitor which we are about to destroy.
2922 // We exit the underlying synchronization object so that the
2923 // "delete monitor" call below can work without an assertion
2924 // failure on systems that don't like destroying synchronization
2925 // objects that are locked.
2926 int r;
2927 intptr_t recursion = rmonitor->recursions();
2928 for (intptr_t i=0; i <= recursion; i++) {
2929 r = rmonitor->raw_exit(thread);
2930 assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
2931 if (r != ObjectMonitor::OM_OK) { // robustness
2932 return JVMTI_ERROR_INTERNAL;
2933 }
2934 }
2935 }
2936 if (rmonitor->owner() != NULL) {
2937 // The caller is trying to destroy a monitor that is locked by
2938 // someone else. While this is not forbidden by the JVMTI
2939 // spec, it will cause an assertion failure on systems that don't
2940 // like destroying synchronization objects that are locked.
2941 // We indicate a problem with the error return (and leak the
2942 // monitor's memory).
2943 return JVMTI_ERROR_NOT_MONITOR_OWNER;
2944 }
2945 }
2947 delete rmonitor;
2949 return JVMTI_ERROR_NONE;
2950 } /* end DestroyRawMonitor */
2953 // rmonitor - pre-checked for validity
2954 jvmtiError
2955 JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
2956 if (Threads::number_of_threads() == 0) {
2957 // No JavaThreads exist so ObjectMonitor enter cannot be
2958 // used, add this raw monitor to the pending list.
2959 // The pending monitors will be actually entered when
2960 // the VM is setup.
2961 // See transition_pending_raw_monitors in create_vm()
2962 // in thread.cpp.
2963 JvmtiPendingMonitors::enter(rmonitor);
2964 } else {
2965 int r;
2966 Thread* thread = Thread::current();
2968 if (thread->is_Java_thread()) {
2969 JavaThread* current_thread = (JavaThread*)thread;
2971 #ifdef PROPER_TRANSITIONS
2972 // Not really unknown but ThreadInVMfromNative does more than we want
2973 ThreadInVMfromUnknown __tiv;
2974 {
2975 ThreadBlockInVM __tbivm(current_thread);
2976 r = rmonitor->raw_enter(current_thread);
2977 }
2978 #else
2979 /* Transition to thread_blocked without entering vm state */
2980 /* This is really evil. Normally you can't undo _thread_blocked */
2981 /* transitions like this because it would cause us to miss a */
2982 /* safepoint but since the thread was already in _thread_in_native */
2983 /* the thread is not leaving a safepoint safe state and it will */
2984 /* block when it tries to return from native. We can't safepoint */
2985 /* block in here because we could deadlock the vmthread. Blech. */
2987 JavaThreadState state = current_thread->thread_state();
2988 assert(state == _thread_in_native, "Must be _thread_in_native");
2989 // frame should already be walkable since we are in native
2990 assert(!current_thread->has_last_Java_frame() ||
2991 current_thread->frame_anchor()->walkable(), "Must be walkable");
2992 current_thread->set_thread_state(_thread_blocked);
2994 r = rmonitor->raw_enter(current_thread);
2995 // restore state, still at a safepoint safe state
2996 current_thread->set_thread_state(state);
2998 #endif /* PROPER_TRANSITIONS */
2999 assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
3000 } else {
3001 if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3002 r = rmonitor->raw_enter(thread);
3003 } else {
3004 ShouldNotReachHere();
3005 }
3006 }
3008 if (r != ObjectMonitor::OM_OK) { // robustness
3009 return JVMTI_ERROR_INTERNAL;
3010 }
3011 }
3012 return JVMTI_ERROR_NONE;
3013 } /* end RawMonitorEnter */
3016 // rmonitor - pre-checked for validity
3017 jvmtiError
3018 JvmtiEnv::RawMonitorExit(JvmtiRawMonitor * rmonitor) {
3019 jvmtiError err = JVMTI_ERROR_NONE;
3021 if (Threads::number_of_threads() == 0) {
3022 // No JavaThreads exist so just remove this monitor from the pending list.
3023 // Bool value from exit is false if rmonitor is not in the list.
3024 if (!JvmtiPendingMonitors::exit(rmonitor)) {
3025 err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3026 }
3027 } else {
3028 int r;
3029 Thread* thread = Thread::current();
3031 if (thread->is_Java_thread()) {
3032 JavaThread* current_thread = (JavaThread*)thread;
3033 #ifdef PROPER_TRANSITIONS
3034 // Not really unknown but ThreadInVMfromNative does more than we want
3035 ThreadInVMfromUnknown __tiv;
3036 #endif /* PROPER_TRANSITIONS */
3037 r = rmonitor->raw_exit(current_thread);
3038 } else {
3039 if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3040 r = rmonitor->raw_exit(thread);
3041 } else {
3042 ShouldNotReachHere();
3043 }
3044 }
3046 if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3047 err = JVMTI_ERROR_NOT_MONITOR_OWNER;
3048 } else {
3049 assert(r == ObjectMonitor::OM_OK, "raw_exit should have worked");
3050 if (r != ObjectMonitor::OM_OK) { // robustness
3051 err = JVMTI_ERROR_INTERNAL;
3052 }
3053 }
3054 }
3055 return err;
3056 } /* end RawMonitorExit */
3059 // rmonitor - pre-checked for validity
3060 jvmtiError
3061 JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
3062 int r;
3063 Thread* thread = Thread::current();
3065 if (thread->is_Java_thread()) {
3066 JavaThread* current_thread = (JavaThread*)thread;
3067 #ifdef PROPER_TRANSITIONS
3068 // Not really unknown but ThreadInVMfromNative does more than we want
3069 ThreadInVMfromUnknown __tiv;
3070 {
3071 ThreadBlockInVM __tbivm(current_thread);
3072 r = rmonitor->raw_wait(millis, true, current_thread);
3073 }
3074 #else
3075 /* Transition to thread_blocked without entering vm state */
3076 /* This is really evil. Normally you can't undo _thread_blocked */
3077 /* transitions like this because it would cause us to miss a */
3078 /* safepoint but since the thread was already in _thread_in_native */
3079 /* the thread is not leaving a safepoint safe state and it will */
3080 /* block when it tries to return from native. We can't safepoint */
3081 /* block in here because we could deadlock the vmthread. Blech. */
3083 JavaThreadState state = current_thread->thread_state();
3084 assert(state == _thread_in_native, "Must be _thread_in_native");
3085 // frame should already be walkable since we are in native
3086 assert(!current_thread->has_last_Java_frame() ||
3087 current_thread->frame_anchor()->walkable(), "Must be walkable");
3088 current_thread->set_thread_state(_thread_blocked);
3090 r = rmonitor->raw_wait(millis, true, current_thread);
3091 // restore state, still at a safepoint safe state
3092 current_thread->set_thread_state(state);
3094 #endif /* PROPER_TRANSITIONS */
3095 } else {
3096 if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3097 r = rmonitor->raw_wait(millis, true, thread);
3098 } else {
3099 ShouldNotReachHere();
3100 }
3101 }
3103 switch (r) {
3104 case ObjectMonitor::OM_INTERRUPTED:
3105 return JVMTI_ERROR_INTERRUPT;
3106 case ObjectMonitor::OM_ILLEGAL_MONITOR_STATE:
3107 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3108 }
3109 assert(r == ObjectMonitor::OM_OK, "raw_wait should have worked");
3110 if (r != ObjectMonitor::OM_OK) { // robustness
3111 return JVMTI_ERROR_INTERNAL;
3112 }
3114 return JVMTI_ERROR_NONE;
3115 } /* end RawMonitorWait */
3118 // rmonitor - pre-checked for validity
3119 jvmtiError
3120 JvmtiEnv::RawMonitorNotify(JvmtiRawMonitor * rmonitor) {
3121 int r;
3122 Thread* thread = Thread::current();
3124 if (thread->is_Java_thread()) {
3125 JavaThread* current_thread = (JavaThread*)thread;
3126 // Not really unknown but ThreadInVMfromNative does more than we want
3127 ThreadInVMfromUnknown __tiv;
3128 r = rmonitor->raw_notify(current_thread);
3129 } else {
3130 if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3131 r = rmonitor->raw_notify(thread);
3132 } else {
3133 ShouldNotReachHere();
3134 }
3135 }
3137 if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3138 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3139 }
3140 assert(r == ObjectMonitor::OM_OK, "raw_notify should have worked");
3141 if (r != ObjectMonitor::OM_OK) { // robustness
3142 return JVMTI_ERROR_INTERNAL;
3143 }
3145 return JVMTI_ERROR_NONE;
3146 } /* end RawMonitorNotify */
3149 // rmonitor - pre-checked for validity
3150 jvmtiError
3151 JvmtiEnv::RawMonitorNotifyAll(JvmtiRawMonitor * rmonitor) {
3152 int r;
3153 Thread* thread = Thread::current();
3155 if (thread->is_Java_thread()) {
3156 JavaThread* current_thread = (JavaThread*)thread;
3157 ThreadInVMfromUnknown __tiv;
3158 r = rmonitor->raw_notifyAll(current_thread);
3159 } else {
3160 if (thread->is_VM_thread() || thread->is_ConcurrentGC_thread()) {
3161 r = rmonitor->raw_notifyAll(thread);
3162 } else {
3163 ShouldNotReachHere();
3164 }
3165 }
3167 if (r == ObjectMonitor::OM_ILLEGAL_MONITOR_STATE) {
3168 return JVMTI_ERROR_NOT_MONITOR_OWNER;
3169 }
3170 assert(r == ObjectMonitor::OM_OK, "raw_notifyAll should have worked");
3171 if (r != ObjectMonitor::OM_OK) { // robustness
3172 return JVMTI_ERROR_INTERNAL;
3173 }
3175 return JVMTI_ERROR_NONE;
3176 } /* end RawMonitorNotifyAll */
3179 //
3180 // JNI Function Interception functions
3181 //
3184 // function_table - pre-checked for NULL
3185 jvmtiError
3186 JvmtiEnv::SetJNIFunctionTable(const jniNativeInterface* function_table) {
3187 // Copy jni function table at safepoint.
3188 VM_JNIFunctionTableCopier copier(function_table);
3189 VMThread::execute(&copier);
3191 return JVMTI_ERROR_NONE;
3192 } /* end SetJNIFunctionTable */
3195 // function_table - pre-checked for NULL
3196 jvmtiError
3197 JvmtiEnv::GetJNIFunctionTable(jniNativeInterface** function_table) {
3198 *function_table=(jniNativeInterface*)jvmtiMalloc(sizeof(jniNativeInterface));
3199 if (*function_table == NULL)
3200 return JVMTI_ERROR_OUT_OF_MEMORY;
3201 memcpy(*function_table,(JavaThread::current())->get_jni_functions(),sizeof(jniNativeInterface));
3202 return JVMTI_ERROR_NONE;
3203 } /* end GetJNIFunctionTable */
3206 //
3207 // Event Management functions
3208 //
3210 jvmtiError
3211 JvmtiEnv::GenerateEvents(jvmtiEvent event_type) {
3212 // can only generate two event types
3213 if (event_type != JVMTI_EVENT_COMPILED_METHOD_LOAD &&
3214 event_type != JVMTI_EVENT_DYNAMIC_CODE_GENERATED) {
3215 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
3216 }
3218 // for compiled_method_load events we must check that the environment
3219 // has the can_generate_compiled_method_load_events capability.
3220 if (event_type == JVMTI_EVENT_COMPILED_METHOD_LOAD) {
3221 if (get_capabilities()->can_generate_compiled_method_load_events == 0) {
3222 return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
3223 }
3224 return JvmtiCodeBlobEvents::generate_compiled_method_load_events(this);
3225 } else {
3226 return JvmtiCodeBlobEvents::generate_dynamic_code_events(this);
3227 }
3229 } /* end GenerateEvents */
3232 //
3233 // Extension Mechanism functions
3234 //
3236 // extension_count_ptr - pre-checked for NULL
3237 // extensions - pre-checked for NULL
3238 jvmtiError
3239 JvmtiEnv::GetExtensionFunctions(jint* extension_count_ptr, jvmtiExtensionFunctionInfo** extensions) {
3240 return JvmtiExtensions::get_functions(this, extension_count_ptr, extensions);
3241 } /* end GetExtensionFunctions */
3244 // extension_count_ptr - pre-checked for NULL
3245 // extensions - pre-checked for NULL
3246 jvmtiError
3247 JvmtiEnv::GetExtensionEvents(jint* extension_count_ptr, jvmtiExtensionEventInfo** extensions) {
3248 return JvmtiExtensions::get_events(this, extension_count_ptr, extensions);
3249 } /* end GetExtensionEvents */
3252 // callback - NULL is a valid value, must be checked
3253 jvmtiError
3254 JvmtiEnv::SetExtensionEventCallback(jint extension_event_index, jvmtiExtensionEvent callback) {
3255 return JvmtiExtensions::set_event_callback(this, extension_event_index, callback);
3256 } /* end SetExtensionEventCallback */
3258 //
3259 // Timers functions
3260 //
3262 // info_ptr - pre-checked for NULL
3263 jvmtiError
3264 JvmtiEnv::GetCurrentThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3265 os::current_thread_cpu_time_info(info_ptr);
3266 return JVMTI_ERROR_NONE;
3267 } /* end GetCurrentThreadCpuTimerInfo */
3270 // nanos_ptr - pre-checked for NULL
3271 jvmtiError
3272 JvmtiEnv::GetCurrentThreadCpuTime(jlong* nanos_ptr) {
3273 *nanos_ptr = os::current_thread_cpu_time();
3274 return JVMTI_ERROR_NONE;
3275 } /* end GetCurrentThreadCpuTime */
3278 // info_ptr - pre-checked for NULL
3279 jvmtiError
3280 JvmtiEnv::GetThreadCpuTimerInfo(jvmtiTimerInfo* info_ptr) {
3281 os::thread_cpu_time_info(info_ptr);
3282 return JVMTI_ERROR_NONE;
3283 } /* end GetThreadCpuTimerInfo */
3286 // Threads_lock NOT held, java_thread not protected by lock
3287 // java_thread - pre-checked
3288 // nanos_ptr - pre-checked for NULL
3289 jvmtiError
3290 JvmtiEnv::GetThreadCpuTime(JavaThread* java_thread, jlong* nanos_ptr) {
3291 *nanos_ptr = os::thread_cpu_time(java_thread);
3292 return JVMTI_ERROR_NONE;
3293 } /* end GetThreadCpuTime */
3296 // info_ptr - pre-checked for NULL
3297 jvmtiError
3298 JvmtiEnv::GetTimerInfo(jvmtiTimerInfo* info_ptr) {
3299 os::javaTimeNanos_info(info_ptr);
3300 return JVMTI_ERROR_NONE;
3301 } /* end GetTimerInfo */
3304 // nanos_ptr - pre-checked for NULL
3305 jvmtiError
3306 JvmtiEnv::GetTime(jlong* nanos_ptr) {
3307 *nanos_ptr = os::javaTimeNanos();
3308 return JVMTI_ERROR_NONE;
3309 } /* end GetTime */
3312 // processor_count_ptr - pre-checked for NULL
3313 jvmtiError
3314 JvmtiEnv::GetAvailableProcessors(jint* processor_count_ptr) {
3315 *processor_count_ptr = os::active_processor_count();
3316 return JVMTI_ERROR_NONE;
3317 } /* end GetAvailableProcessors */
3319 //
3320 // System Properties functions
3321 //
3323 // count_ptr - pre-checked for NULL
3324 // property_ptr - pre-checked for NULL
3325 jvmtiError
3326 JvmtiEnv::GetSystemProperties(jint* count_ptr, char*** property_ptr) {
3327 jvmtiError err = JVMTI_ERROR_NONE;
3329 *count_ptr = Arguments::PropertyList_count(Arguments::system_properties());
3331 err = allocate(*count_ptr * sizeof(char *), (unsigned char **)property_ptr);
3332 if (err != JVMTI_ERROR_NONE) {
3333 return err;
3334 }
3335 int i = 0 ;
3336 for (SystemProperty* p = Arguments::system_properties(); p != NULL && i < *count_ptr; p = p->next(), i++) {
3337 const char *key = p->key();
3338 char **tmp_value = *property_ptr+i;
3339 err = allocate((strlen(key)+1) * sizeof(char), (unsigned char**)tmp_value);
3340 if (err == JVMTI_ERROR_NONE) {
3341 strcpy(*tmp_value, key);
3342 } else {
3343 // clean up previously allocated memory.
3344 for (int j=0; j<i; j++) {
3345 Deallocate((unsigned char*)*property_ptr+j);
3346 }
3347 Deallocate((unsigned char*)property_ptr);
3348 break;
3349 }
3350 }
3351 return err;
3352 } /* end GetSystemProperties */
3355 // property - pre-checked for NULL
3356 // value_ptr - pre-checked for NULL
3357 jvmtiError
3358 JvmtiEnv::GetSystemProperty(const char* property, char** value_ptr) {
3359 jvmtiError err = JVMTI_ERROR_NONE;
3360 const char *value;
3362 value = Arguments::PropertyList_get_value(Arguments::system_properties(), property);
3363 if (value == NULL) {
3364 err = JVMTI_ERROR_NOT_AVAILABLE;
3365 } else {
3366 err = allocate((strlen(value)+1) * sizeof(char), (unsigned char **)value_ptr);
3367 if (err == JVMTI_ERROR_NONE) {
3368 strcpy(*value_ptr, value);
3369 }
3370 }
3371 return err;
3372 } /* end GetSystemProperty */
3375 // property - pre-checked for NULL
3376 // value - NULL is a valid value, must be checked
3377 jvmtiError
3378 JvmtiEnv::SetSystemProperty(const char* property, const char* value) {
3379 jvmtiError err =JVMTI_ERROR_NOT_AVAILABLE;
3381 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
3382 if (strcmp(property, p->key()) == 0) {
3383 if (p->set_value((char *)value)) {
3384 err = JVMTI_ERROR_NONE;
3385 }
3386 }
3387 }
3388 return err;
3389 } /* end SetSystemProperty */
3391 #endif // !JVMTI_KERNEL