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