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