comparison src/share/vm/prims/jvmtiEnv.cpp @ 0:a61af66fc99e jdk7-b24

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