gcc/ChangeLog:
[official-gcc.git] / libjava / jvmti.cc
blobe14bd7c50056c72dbbc2edf33949851bb1bf612f
1 // jvmti.cc - JVMTI implementation
3 /* Copyright (C) 2006, 2007 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 #include <config.h>
12 #include <platform.h>
14 #include <jvm.h>
15 #include <java-threads.h>
16 #include <java-gc.h>
17 #include <java-interp.h>
18 #include <jvmti.h>
19 #include "jvmti-int.h"
21 #include <gcj/method.h>
23 #include <gnu/classpath/SystemProperties.h>
24 #include <gnu/gcj/runtime/BootClassLoader.h>
25 #include <gnu/gcj/jvmti/Breakpoint.h>
26 #include <gnu/gcj/jvmti/BreakpointManager.h>
28 #include <java/lang/Class.h>
29 #include <java/lang/ClassLoader.h>
30 #include <java/lang/OutOfMemoryError.h>
31 #include <java/lang/Thread.h>
32 #include <java/lang/ThreadGroup.h>
33 #include <java/lang/Throwable.h>
34 #include <java/lang/VMClassLoader.h>
35 #include <java/lang/reflect/Field.h>
36 #include <java/lang/reflect/Modifier.h>
37 #include <java/util/Collection.h>
38 #include <java/util/HashMap.h>
39 #include <java/util/concurrent/locks/Lock.h>
40 #include <java/util/concurrent/locks/ReentrantReadWriteLock.h>
41 #include <java/net/URL.h>
43 static void check_enabled_events (void);
44 static void check_enabled_event (jvmtiEvent);
46 namespace JVMTI
48 // Is JVMTI enabled? (i.e., any jvmtiEnv created?)
49 bool enabled;
51 // Event notifications
52 bool VMInit = false;
53 bool VMDeath = false;
54 bool ThreadStart = false;
55 bool ThreadEnd = false;
56 bool ClassFileLoadHook = false;
57 bool ClassLoad = false;
58 bool ClassPrepare = false;
59 bool VMStart = false;
60 bool Exception = false;
61 bool ExceptionCatch = false;
62 bool SingleStep = false;
63 bool FramePop = false;
64 bool Breakpoint = false;
65 bool FieldAccess = false;
66 bool FieldModification = false;
67 bool MethodEntry = false;
68 bool MethodExit = false;
69 bool NativeMethodBind = false;
70 bool CompiledMethodLoad = false;
71 bool CompiledMethodUnload = false;
72 bool DynamicCodeGenerated = false;
73 bool DataDumpRequest = false;
74 bool reserved72 = false;
75 bool MonitorWait = false;
76 bool MonitorWaited = false;
77 bool MonitorContendedEnter = false;
78 bool MonitorContendedEntered = false;
79 bool reserved77 = false;
80 bool reserved78 = false;
81 bool reserved79 = false;
82 bool reserved80 = false;
83 bool GarbageCollectionStart = false;
84 bool GarbageCollectionFinish = false;
85 bool ObjectFree = false;
86 bool VMObjectAlloc = false;
89 extern struct JNINativeInterface _Jv_JNIFunctions;
91 struct _Jv_rawMonitorID
93 _Jv_Mutex_t mutex;
94 _Jv_ConditionVariable_t condition;
97 /* A simple linked list of all JVMTI environments. Since
98 events must be delivered to environments in the order
99 in which the environments were created, new environments
100 are added to the end of the list. */
101 struct jvmti_env_list
103 jvmtiEnv *env;
104 struct jvmti_env_list *next;
106 static struct jvmti_env_list *_jvmtiEnvironments = NULL;
107 static java::util::concurrent::locks::
108 ReentrantReadWriteLock *_envListLock = NULL;
109 #define FOREACH_ENVIRONMENT(Ele) \
110 for (Ele = _jvmtiEnvironments; Ele != NULL; Ele = Ele->next)
112 // Some commonly-used checks
114 #define THREAD_DEFAULT_TO_CURRENT(Ajthread) \
115 do \
117 if (Ajthread == NULL) \
118 Ajthread = java::lang::Thread::currentThread (); \
120 while (0)
122 #define THREAD_CHECK_VALID(Athread) \
123 do \
125 if (!java::lang::Thread::class$.isAssignableFrom (&(Athread->class$))) \
126 return JVMTI_ERROR_INVALID_THREAD; \
128 while (0)
130 #define THREAD_CHECK_IS_ALIVE(Athread) \
131 do \
133 if (!Athread->isAlive ()) \
134 return JVMTI_ERROR_THREAD_NOT_ALIVE; \
136 while (0)
138 // FIXME: if current phase is not set in Phases,
139 // return JVMTI_ERROR_WRONG_PHASE
140 #define REQUIRE_PHASE(Env, Phases)
142 #define NULL_CHECK(Ptr) \
143 do \
145 if (Ptr == NULL) \
146 return JVMTI_ERROR_NULL_POINTER; \
148 while (0)
150 #define ILLEGAL_ARGUMENT(Cond) \
151 do \
153 if ((Cond)) \
154 return JVMTI_ERROR_ILLEGAL_ARGUMENT; \
156 while (0)
158 static jvmtiError JNICALL
159 _Jv_JVMTI_SuspendThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
161 using namespace java::lang;
163 THREAD_DEFAULT_TO_CURRENT (thread);
164 THREAD_CHECK_VALID (thread);
165 THREAD_CHECK_IS_ALIVE (thread);
167 _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
168 _Jv_SuspendThread (data);
169 return JVMTI_ERROR_NONE;
172 static jvmtiError JNICALL
173 _Jv_JVMTI_ResumeThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
175 using namespace java::lang;
177 THREAD_DEFAULT_TO_CURRENT (thread);
178 THREAD_CHECK_VALID (thread);
179 THREAD_CHECK_IS_ALIVE (thread);
181 _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
182 _Jv_ResumeThread (data);
183 return JVMTI_ERROR_NONE;
186 static jvmtiError JNICALL
187 _Jv_JVMTI_InterruptThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
189 using namespace java::lang;
191 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
192 // FIXME: capability handling? 'can_signal_thread'
193 if (thread == NULL)
194 return JVMTI_ERROR_INVALID_THREAD;
196 THREAD_CHECK_VALID (thread);
197 THREAD_CHECK_IS_ALIVE (thread);
198 thread->interrupt();
199 return JVMTI_ERROR_NONE;
202 static jvmtiError JNICALL
203 _Jv_JVMTI_GetAllThreads(MAYBE_UNUSED jvmtiEnv *env, jint *thread_cnt,
204 jthread **threads)
206 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
207 NULL_CHECK (thread_cnt);
208 NULL_CHECK (threads);
210 using namespace java::lang;
212 ThreadGroup *root_grp = ThreadGroup::root;
213 jint estimate = root_grp->activeCount ();
215 JArray<Thread *> *thr_arr;
217 // Allocate some extra space since threads can be created between calls
220 thr_arr = reinterpret_cast<JArray<Thread *> *> (JvNewObjectArray
221 ((estimate * 2),
222 &Thread::class$, NULL));
224 catch (java::lang::OutOfMemoryError *err)
226 return JVMTI_ERROR_OUT_OF_MEMORY;
229 *thread_cnt = root_grp->enumerate (thr_arr);
231 jvmtiError jerr = env->Allocate ((jlong) ((*thread_cnt) * sizeof (jthread)),
232 (unsigned char **) threads);
234 if (jerr != JVMTI_ERROR_NONE)
235 return jerr;
237 // Transfer the threads to the result array
238 jthread *tmp_arr = reinterpret_cast<jthread *> (elements (thr_arr));
240 memcpy ((*threads), tmp_arr, (*thread_cnt));
242 return JVMTI_ERROR_NONE;
245 static jvmtiError JNICALL
246 _Jv_JVMTI_GetFrameCount (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
247 jint* frame_count)
249 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
251 NULL_CHECK (frame_count);
253 using namespace java::lang;
255 THREAD_DEFAULT_TO_CURRENT (thread);
257 Thread *thr = reinterpret_cast<Thread *> (thread);
258 THREAD_CHECK_VALID (thr);
259 THREAD_CHECK_IS_ALIVE (thr);
261 _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thr->frame);
262 (*frame_count) = 0;
264 while (frame != NULL)
266 (*frame_count)++;
267 frame = frame->next;
270 return JVMTI_ERROR_NONE;
273 static jvmtiError JNICALL
274 _Jv_JVMTI_CreateRawMonitor (MAYBE_UNUSED jvmtiEnv *env, const char *name,
275 jrawMonitorID *result)
277 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
278 NULL_CHECK (name);
279 NULL_CHECK (result);
280 *result = (jrawMonitorID) _Jv_MallocUnchecked (sizeof (_Jv_rawMonitorID));
281 if (*result == NULL)
282 return JVMTI_ERROR_OUT_OF_MEMORY;
283 _Jv_MutexInit (&(*result)->mutex);
284 _Jv_CondInit (&(*result)->condition);
285 return JVMTI_ERROR_NONE;
288 static jvmtiError JNICALL
289 _Jv_JVMTI_DestroyRawMonitor (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
291 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
292 // Note we have no better way of knowing whether this object is
293 // really a raw monitor.
294 if (monitor == NULL)
295 return JVMTI_ERROR_INVALID_MONITOR;
296 // FIXME: perform checks on monitor, release it if this thread owns
297 // it.
298 #ifdef _Jv_HaveMutexDestroy
299 _Jv_MutexDestroy (&monitor->mutex);
300 #endif
301 _Jv_Free (monitor);
302 return JVMTI_ERROR_NONE;
305 static jvmtiError JNICALL
306 _Jv_JVMTI_RawMonitorEnter (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
308 if (monitor == NULL)
309 return JVMTI_ERROR_INVALID_MONITOR;
310 _Jv_MutexLock (&monitor->mutex);
311 return JVMTI_ERROR_NONE;
314 static jvmtiError JNICALL
315 _Jv_JVMTI_RawMonitorExit (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
317 if (monitor == NULL)
318 return JVMTI_ERROR_INVALID_MONITOR;
319 if (_Jv_MutexUnlock (&monitor->mutex))
320 return JVMTI_ERROR_NOT_MONITOR_OWNER;
321 return JVMTI_ERROR_NONE;
324 static jvmtiError JNICALL
325 _Jv_JVMTI_RawMonitorWait (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor,
326 jlong millis)
328 if (monitor == NULL)
329 return JVMTI_ERROR_INVALID_MONITOR;
330 int r = _Jv_CondWait (&monitor->condition, &monitor->mutex, millis, 0);
331 if (r == _JV_NOT_OWNER)
332 return JVMTI_ERROR_NOT_MONITOR_OWNER;
333 if (r == _JV_INTERRUPTED)
334 return JVMTI_ERROR_INTERRUPT;
335 return JVMTI_ERROR_NONE;
338 static jvmtiError JNICALL
339 _Jv_JVMTI_RawMonitorNotify (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
341 if (monitor == NULL)
342 return JVMTI_ERROR_INVALID_MONITOR;
343 if (_Jv_CondNotify (&monitor->condition, &monitor->mutex) == _JV_NOT_OWNER)
344 return JVMTI_ERROR_NOT_MONITOR_OWNER;
345 return JVMTI_ERROR_NONE;
348 static jvmtiError JNICALL
349 _Jv_JVMTI_RawMonitorNotifyAll (MAYBE_UNUSED jvmtiEnv *env,
350 jrawMonitorID monitor)
352 if (monitor == NULL)
353 return JVMTI_ERROR_INVALID_MONITOR;
354 if (_Jv_CondNotifyAll (&monitor->condition, &monitor->mutex)
355 == _JV_NOT_OWNER)
356 return JVMTI_ERROR_NOT_MONITOR_OWNER;
357 return JVMTI_ERROR_NONE;
360 static jvmtiError JNICALL
361 _Jv_JVMTI_SetBreakpoint (jvmtiEnv *env, jmethodID method, jlocation location)
363 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
365 using namespace gnu::gcj::jvmti;
366 Breakpoint *bp
367 = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
368 location);
369 if (bp == NULL)
371 jclass klass;
372 jvmtiError err = env->GetMethodDeclaringClass (method, &klass);
373 if (err != JVMTI_ERROR_NONE)
374 return err;
376 if (!_Jv_IsInterpretedClass (klass))
377 return JVMTI_ERROR_INVALID_CLASS;
379 _Jv_MethodBase *base = _Jv_FindInterpreterMethod (klass, method);
380 if (base == NULL)
381 return JVMTI_ERROR_INVALID_METHODID;
383 jint flags;
384 err = env->GetMethodModifiers (method, &flags);
385 if (err != JVMTI_ERROR_NONE)
386 return err;
388 if (flags & java::lang::reflect::Modifier::NATIVE)
389 return JVMTI_ERROR_NATIVE_METHOD;
391 _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (base);
392 if (imeth->get_insn (location) == NULL)
393 return JVMTI_ERROR_INVALID_LOCATION;
395 // Now the breakpoint can be safely installed
396 bp = BreakpointManager::newBreakpoint (reinterpret_cast<jlong> (method),
397 location);
399 else
401 // Duplicate breakpoints are not permitted by JVMTI
402 return JVMTI_ERROR_DUPLICATE;
405 return JVMTI_ERROR_NONE;
408 static jvmtiError JNICALL
409 _Jv_JVMTI_ClearBreakpoint (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
410 jlocation location)
412 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
414 using namespace gnu::gcj::jvmti;
416 Breakpoint *bp
417 = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
418 location);
419 if (bp == NULL)
420 return JVMTI_ERROR_NOT_FOUND;
422 BreakpointManager::deleteBreakpoint (reinterpret_cast<jlong> (method), location);
423 return JVMTI_ERROR_NONE;
426 static jvmtiError JNICALL
427 _Jv_JVMTI_Allocate (MAYBE_UNUSED jvmtiEnv *env, jlong size,
428 unsigned char **result)
430 ILLEGAL_ARGUMENT (size < 0);
431 NULL_CHECK (result);
432 if (size == 0)
433 *result = NULL;
434 else
436 *result = (unsigned char *) _Jv_MallocUnchecked (size);
437 if (*result == NULL)
438 return JVMTI_ERROR_OUT_OF_MEMORY;
440 return JVMTI_ERROR_NONE;
443 static jvmtiError JNICALL
444 _Jv_JVMTI_Deallocate (MAYBE_UNUSED jvmtiEnv *env, unsigned char *mem)
446 if (mem != NULL)
447 _Jv_Free (mem);
448 return JVMTI_ERROR_NONE;
451 static jvmtiError JNICALL
452 _Jv_JVMTI_GetClassStatus (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
453 jint *status_ptr)
455 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
456 NULL_CHECK (status_ptr);
457 if (klass == NULL)
458 return JVMTI_ERROR_INVALID_CLASS;
460 if (klass->isArray ())
461 *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
462 else if (klass->isPrimitive ())
463 *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
464 else
466 jbyte state = _Jv_GetClassState (klass);
467 *status_ptr = 0;
468 if (state >= JV_STATE_LINKED)
469 (*status_ptr) |= JVMTI_CLASS_STATUS_VERIFIED;
470 if (state >= JV_STATE_PREPARED)
471 (*status_ptr) |= JVMTI_CLASS_STATUS_PREPARED;
472 if (state == JV_STATE_ERROR || state == JV_STATE_PHANTOM)
473 (*status_ptr) |= JVMTI_CLASS_STATUS_ERROR;
474 else if (state == JV_STATE_DONE)
475 (*status_ptr) |= JVMTI_CLASS_STATUS_INITIALIZED;
478 return JVMTI_ERROR_NONE;
481 static jvmtiError JNICALL
482 _Jv_JVMTI_GetClassModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
483 jint *mods)
485 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
486 // Don't bother checking KLASS' type.
487 if (klass == NULL)
488 return JVMTI_ERROR_INVALID_CLASS;
489 NULL_CHECK (mods);
490 *mods = klass->getModifiers();
491 return JVMTI_ERROR_NONE;
494 static jvmtiError JNICALL
495 _Jv_JVMTI_GetClassMethods (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
496 jint *count_ptr, jmethodID **methods_ptr)
498 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
499 // FIXME: capability can_maintain_original_method_order
500 // Don't bother checking KLASS' type.
501 if (klass == NULL)
502 return JVMTI_ERROR_INVALID_CLASS;
503 NULL_CHECK (count_ptr);
504 NULL_CHECK (methods_ptr);
505 *count_ptr = JvNumMethods(klass);
507 *methods_ptr
508 = (jmethodID *) _Jv_MallocUnchecked (*count_ptr * sizeof (jmethodID));
509 if (*methods_ptr == NULL)
510 return JVMTI_ERROR_OUT_OF_MEMORY;
512 jmethodID start = JvGetFirstMethod (klass);
513 for (jint i = 0; i < *count_ptr; ++i)
514 // FIXME: correct?
515 (*methods_ptr)[i] = start + i;
517 return JVMTI_ERROR_NONE;
520 static jvmtiError JNICALL
521 _Jv_JVMTI_IsInterface (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
522 jboolean *result)
524 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
525 if (klass == NULL)
526 return JVMTI_ERROR_INVALID_CLASS;
527 NULL_CHECK (result);
528 *result = klass->isInterface();
529 return JVMTI_ERROR_NONE;
532 static jvmtiError JNICALL
533 _Jv_JVMTI_IsArrayClass (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
534 jboolean *result)
536 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
537 if (klass == NULL)
538 return JVMTI_ERROR_INVALID_CLASS;
539 NULL_CHECK (result);
540 *result = klass->isArray();
541 return JVMTI_ERROR_NONE;
544 static jvmtiError JNICALL
545 _Jv_JVMTI_GetClassLoader (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
546 jobject *result)
548 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
549 if (klass == NULL)
550 return JVMTI_ERROR_INVALID_CLASS;
551 NULL_CHECK (result);
552 *result = klass->getClassLoaderInternal();
553 return JVMTI_ERROR_NONE;
556 static jvmtiError JNICALL
557 _Jv_JVMTI_GetObjectHashCode (MAYBE_UNUSED jvmtiEnv *env, jobject obj,
558 jint *result)
560 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
561 if (obj == NULL)
562 return JVMTI_ERROR_INVALID_OBJECT;
563 NULL_CHECK (result);
564 *result = _Jv_HashCode (obj);
565 return JVMTI_ERROR_NONE;
568 static jvmtiError JNICALL
569 _Jv_JVMTI_GetFieldModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
570 jfieldID field, jint *result)
572 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
573 if (klass == NULL)
574 return JVMTI_ERROR_INVALID_CLASS;
575 if (field == NULL)
576 return JVMTI_ERROR_INVALID_FIELDID;
577 NULL_CHECK (result);
578 *result = field->getModifiers();
579 return JVMTI_ERROR_NONE;
582 static jvmtiError JNICALL
583 _Jv_JVMTI_IsFieldSynthetic (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
584 jfieldID field, jboolean *result)
586 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
587 if (klass == NULL)
588 return JVMTI_ERROR_INVALID_CLASS;
589 if (field == NULL)
590 return JVMTI_ERROR_INVALID_FIELDID;
591 NULL_CHECK (result);
593 // FIXME: capability can_get_synthetic_attribute
594 *result = ((field->getModifiers() & java::lang::reflect::Modifier::SYNTHETIC)
595 != 0);
596 return JVMTI_ERROR_NONE;
599 static jvmtiError JNICALL
600 _Jv_JVMTI_GetMethodName (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
601 char **name_ptr, char **signature_ptr,
602 char **generic_ptr)
604 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
606 if (method == NULL)
607 return JVMTI_ERROR_INVALID_METHODID;
609 if (name_ptr != NULL)
611 int len = static_cast<int> (method->name->len ());
612 *name_ptr = (char *) _Jv_MallocUnchecked (len + 1);
613 if (*name_ptr == NULL)
614 return JVMTI_ERROR_OUT_OF_MEMORY;
615 strncpy (*name_ptr, method->name->chars (), len);
616 (*name_ptr)[len] = '\0';
619 if (signature_ptr != NULL)
621 int len = static_cast<int> (method->signature->len ());
622 *signature_ptr = (char *) _Jv_MallocUnchecked (len + 1);
623 if (*signature_ptr == NULL)
625 if (name_ptr != NULL)
626 _Jv_Free (*name_ptr);
627 return JVMTI_ERROR_OUT_OF_MEMORY;
629 strncpy (*signature_ptr, method->signature->chars (), len);
630 (*signature_ptr)[len] = '\0';
633 if (generic_ptr != NULL)
635 *generic_ptr = NULL;
638 return JVMTI_ERROR_NONE;
641 static jvmtiError JNICALL
642 _Jv_JVMTI_GetMethodModifiers (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
643 jint *result)
645 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
646 if (method == NULL)
647 return JVMTI_ERROR_INVALID_METHODID;
648 NULL_CHECK (result);
650 // FIXME: mask off some internal bits...
651 *result = method->accflags;
652 return JVMTI_ERROR_NONE;
655 static jvmtiError JNICALL
656 _Jv_JVMTI_GetLineNumberTable (jvmtiEnv *env, jmethodID method,
657 jint *entry_count_ptr,
658 jvmtiLineNumberEntry **table_ptr)
660 NULL_CHECK (entry_count_ptr);
661 NULL_CHECK (table_ptr);
663 jclass klass;
664 jvmtiError jerr = env->GetMethodDeclaringClass (method, &klass);
665 if (jerr != JVMTI_ERROR_NONE)
666 return jerr;
668 _Jv_MethodBase *base = _Jv_FindInterpreterMethod (klass, method);
669 if (base == NULL)
670 return JVMTI_ERROR_INVALID_METHODID;
672 if (java::lang::reflect::Modifier::isNative (method->accflags)
673 || !_Jv_IsInterpretedClass (klass))
674 return JVMTI_ERROR_NATIVE_METHOD;
676 _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (base);
677 jlong start, end;
678 jintArray lines = NULL;
679 jlongArray indices = NULL;
680 imeth->get_line_table (start, end, lines, indices);
681 if (lines == NULL)
682 return JVMTI_ERROR_ABSENT_INFORMATION;
684 jvmtiLineNumberEntry *table;
685 jsize len = lines->length * sizeof (jvmtiLineNumberEntry);
686 table = (jvmtiLineNumberEntry *) _Jv_MallocUnchecked (len);
687 if (table == NULL)
688 return JVMTI_ERROR_OUT_OF_MEMORY;
690 jint *line = elements (lines);
691 jlong *index = elements (indices);
692 for (int i = 0; i < lines->length; ++i)
694 table[i].start_location = index[i];
695 table[i].line_number = line[i];
698 *table_ptr = table;
699 *entry_count_ptr = lines->length;
700 return JVMTI_ERROR_NONE;
703 static jvmtiError JNICALL
704 _Jv_JVMTI_IsMethodNative (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
705 jboolean *result)
707 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
708 if (method == NULL)
709 return JVMTI_ERROR_INVALID_METHODID;
710 NULL_CHECK (result);
712 *result = ((method->accflags & java::lang::reflect::Modifier::NATIVE) != 0);
713 return JVMTI_ERROR_NONE;
716 static jvmtiError JNICALL
717 _Jv_JVMTI_IsMethodSynthetic (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
718 jboolean *result)
720 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
721 if (method == NULL)
722 return JVMTI_ERROR_INVALID_METHODID;
723 NULL_CHECK (result);
725 // FIXME capability can_get_synthetic_attribute
727 *result = ((method->accflags & java::lang::reflect::Modifier::SYNTHETIC)
728 != 0);
729 return JVMTI_ERROR_NONE;
732 static jvmtiError JNICALL
733 _Jv_JVMTI_GetMethodDeclaringClass (MAYBE_UNUSED jvmtiEnv *env,
734 jmethodID method,
735 jclass *declaring_class_ptr)
737 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
738 NULL_CHECK (declaring_class_ptr);
740 jclass klass = _Jv_GetMethodDeclaringClass (method);
741 if (klass != NULL)
743 *declaring_class_ptr = klass;
744 return JVMTI_ERROR_NONE;
747 return JVMTI_ERROR_INVALID_METHODID;
750 static jvmtiError JNICALL
751 _Jv_JVMTI_GetClassLoaderClasses (MAYBE_UNUSED jvmtiEnv *env,
752 jobject init_loader,
753 jint *count_ptr,
754 jclass **result_ptr)
756 using namespace java::lang;
757 using namespace java::util;
759 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
760 NULL_CHECK (count_ptr);
761 NULL_CHECK (result_ptr);
763 ClassLoader *loader = (ClassLoader *) init_loader;
764 if (loader == NULL)
765 loader = VMClassLoader::bootLoader;
767 Collection *values = loader->loadedClasses->values();
768 jobjectArray array = values->toArray();
769 *count_ptr = array->length;
770 jobject *elts = elements (array);
771 jclass *result
772 = (jclass *) _Jv_MallocUnchecked (*count_ptr * sizeof (jclass));
773 if (result == NULL)
774 return JVMTI_ERROR_OUT_OF_MEMORY;
776 // FIXME: JNI references...
777 memcpy (result, elts, *count_ptr * sizeof (jclass));
779 *result_ptr = result;
781 return JVMTI_ERROR_NONE;
784 static jvmtiError JNICALL
785 _Jv_JVMTI_GetStackTrace (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
786 jint start_depth, jint max_frames,
787 jvmtiFrameInfo *frames, jint *frame_count)
789 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
791 ILLEGAL_ARGUMENT (max_frames < 0);
793 NULL_CHECK (frames);
794 NULL_CHECK (frame_count);
796 using namespace java::lang;
798 THREAD_DEFAULT_TO_CURRENT (thread);
800 Thread *thr = reinterpret_cast<Thread *> (thread);
801 THREAD_CHECK_VALID (thr);
802 THREAD_CHECK_IS_ALIVE (thr);
804 jvmtiError jerr = env->GetFrameCount (thread, frame_count);
805 if (jerr != JVMTI_ERROR_NONE)
806 return jerr;
808 // start_depth can be either a positive number, indicating the depth of the
809 // stack at which to begin the trace, or a negative number indicating the
810 // number of frames at the bottom of the stack to exclude. These checks
811 // ensure that it is a valid value in either case
813 ILLEGAL_ARGUMENT (start_depth >= (*frame_count));
814 ILLEGAL_ARGUMENT (start_depth < (-(*frame_count)));
816 _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thr->frame);
818 // If start_depth is negative use this to determine at what depth to start
819 // the trace by adding it to the length of the call stack. This allows the
820 // use of the same frame "discarding" mechanism as for a positive start_depth
821 if (start_depth < 0)
822 start_depth = *frame_count + start_depth;
824 // If start_depth > 0 "remove" start_depth frames from the beginning
825 // of the stack before beginning the trace by moving along the frame list.
826 while (start_depth > 0)
828 frame = frame->next;
829 start_depth--;
830 (*frame_count)--;
833 // Now check to see if the array supplied by the agent is large enough to
834 // hold frame_count frames, after adjustment for start_depth.
835 if ((*frame_count) > max_frames)
836 (*frame_count) = max_frames;
838 for (int i = 0; i < (*frame_count); i++)
840 frames[i].method = frame->self->get_method ();
842 // Set the location in the frame, native frames have location = -1
843 if (frame->frame_type == frame_interpreter)
845 _Jv_InterpMethod *imeth
846 = static_cast<_Jv_InterpMethod *> (frame->self);
847 _Jv_InterpFrame *interp_frame
848 = static_cast<_Jv_InterpFrame *> (frame);
849 frames[i].location = imeth->insn_index (interp_frame->pc);
851 else
852 frames[i].location = -1;
854 frame = frame->next;
857 return JVMTI_ERROR_NONE;
860 static jvmtiError JNICALL
861 _Jv_JVMTI_ForceGarbageCollection (MAYBE_UNUSED jvmtiEnv *env)
863 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
864 _Jv_RunGC();
865 return JVMTI_ERROR_NONE;
868 static jvmtiError JNICALL
869 _Jv_JVMTI_SetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
870 const jniNativeInterface *function_table)
872 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
873 NULL_CHECK (function_table);
874 memcpy (&_Jv_JNIFunctions, function_table, sizeof (jniNativeInterface));
875 return JVMTI_ERROR_NONE;
878 static jvmtiError JNICALL
879 _Jv_JVMTI_GetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
880 jniNativeInterface **function_table)
882 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
883 NULL_CHECK (function_table);
884 *function_table
885 = (jniNativeInterface *) _Jv_MallocUnchecked (sizeof (jniNativeInterface));
886 if (*function_table == NULL)
887 return JVMTI_ERROR_OUT_OF_MEMORY;
888 memcpy (*function_table, &_Jv_JNIFunctions, sizeof (jniNativeInterface));
889 return JVMTI_ERROR_NONE;
892 static jvmtiError JNICALL
893 _Jv_JVMTI_DisposeEnvironment (jvmtiEnv *env)
895 NULL_CHECK (env);
897 if (_jvmtiEnvironments == NULL)
898 return JVMTI_ERROR_INVALID_ENVIRONMENT;
899 else
901 _envListLock->writeLock ()->lock ();
902 if (_jvmtiEnvironments->env == env)
904 struct jvmti_env_list *next = _jvmtiEnvironments->next;
905 _Jv_Free (_jvmtiEnvironments);
906 _jvmtiEnvironments = next;
908 else
910 struct jvmti_env_list *e = _jvmtiEnvironments;
911 while (e->next != NULL && e->next->env != env)
912 e = e->next;
913 if (e->next == NULL)
915 _envListLock->writeLock ()->unlock ();
916 return JVMTI_ERROR_INVALID_ENVIRONMENT;
919 struct jvmti_env_list *next = e->next->next;
920 _Jv_Free (e->next);
921 e->next = next;
923 _envListLock->writeLock ()->unlock ();
926 _Jv_Free (env);
928 check_enabled_events ();
930 return JVMTI_ERROR_NONE;
933 static jvmtiError JNICALL
934 _Jv_JVMTI_GetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
935 char **result)
937 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
938 NULL_CHECK (property);
939 NULL_CHECK (result);
941 jstring name = JvNewStringUTF(property);
942 jstring result_str = gnu::classpath::SystemProperties::getProperty(name);
944 if (result_str == NULL)
945 return JVMTI_ERROR_NOT_AVAILABLE;
947 int len = JvGetStringUTFLength (result_str);
948 *result = (char *) _Jv_MallocUnchecked (len + 1);
949 if (*result == NULL)
950 return JVMTI_ERROR_OUT_OF_MEMORY;
951 JvGetStringUTFRegion (result_str, 0, result_str->length(), *result);
952 (*result)[len] = '\0';
954 return JVMTI_ERROR_NONE;
957 static jvmtiError JNICALL
958 _Jv_JVMTI_SetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
959 const char *value)
961 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD);
963 NULL_CHECK (property);
964 if (value == NULL)
966 // FIXME: When would a property not be writeable?
967 return JVMTI_ERROR_NONE;
970 jstring prop_str = JvNewStringUTF(property);
971 jstring value_str = JvNewStringUTF(value);
972 gnu::classpath::SystemProperties::setProperty(prop_str, value_str);
973 return JVMTI_ERROR_NONE;
976 static jvmtiError JNICALL
977 _Jv_JVMTI_GetTime (MAYBE_UNUSED jvmtiEnv *env, jlong *nanos_ptr)
979 NULL_CHECK (nanos_ptr);
980 *nanos_ptr = _Jv_platform_nanotime();
981 return JVMTI_ERROR_NONE;
984 static jvmtiError JNICALL
985 _Jv_JVMTI_GetAvailableProcessors (MAYBE_UNUSED jvmtiEnv *env,
986 jint *nprocessors_ptr)
988 NULL_CHECK (nprocessors_ptr);
989 #ifdef _SC_NPROCESSORS_ONLN
990 *nprocessors_ptr = sysconf(_SC_NPROCESSORS_ONLN);
991 #else
992 *nprocessors_ptr = 1;
993 #endif
994 return JVMTI_ERROR_NONE;
997 static jvmtiError JNICALL
998 _Jv_JVMTI_AddToBootstrapClassLoaderSearch (MAYBE_UNUSED jvmtiEnv *env,
999 const char *segment)
1001 using namespace java::lang;
1002 using namespace java::net;
1003 using namespace gnu::gcj::runtime;
1005 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD);
1006 NULL_CHECK (segment);
1008 jstring str_segment = JvNewStringUTF(segment);
1009 URL *url;
1012 url = new URL(JvNewStringUTF("file"), NULL, str_segment);
1014 catch (jthrowable ignore)
1016 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1019 BootClassLoader *loader = VMClassLoader::bootLoader;
1020 // Don't call this too early.
1021 // assert (loader != NULL);
1022 loader->addURL(url);
1023 return JVMTI_ERROR_NONE;
1026 static jvmtiError JNICALL
1027 _Jv_JVMTI_SetVerboseFlag (MAYBE_UNUSED jvmtiEnv *env, jvmtiVerboseFlag flag,
1028 jboolean value)
1030 switch (flag)
1032 case JVMTI_VERBOSE_OTHER:
1033 case JVMTI_VERBOSE_GC:
1034 case JVMTI_VERBOSE_JNI:
1035 // Ignore.
1036 break;
1037 case JVMTI_VERBOSE_CLASS:
1038 gcj::verbose_class_flag = value;
1039 break;
1040 default:
1041 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1043 return JVMTI_ERROR_NONE;
1046 static jvmtiError JNICALL
1047 _Jv_JVMTI_GetObjectSize (MAYBE_UNUSED jvmtiEnv *env, jobject object,
1048 jlong *result)
1050 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
1051 if (object == NULL)
1052 return JVMTI_ERROR_INVALID_OBJECT;
1053 NULL_CHECK (result);
1055 jclass klass = object->getClass();
1056 if (klass->isArray())
1058 jclass comp = klass->getComponentType();
1059 jint base
1060 = (jint) (_Jv_uintptr_t) _Jv_GetArrayElementFromElementType(NULL,
1061 klass->getComponentType());
1062 // FIXME: correct for primitive types?
1063 jint compSize = comp->size();
1064 __JArray *array = (__JArray *) object;
1065 *result = base + array->length * compSize;
1067 else
1069 // Note that if OBJECT is a String then it may (if
1070 // str->data==str) take more space. Do we care?
1071 *result = klass->size();
1073 return JVMTI_ERROR_NONE;
1076 /* An event is enabled only if it has both an event handler
1077 and it is enabled in the environment. */
1078 static void
1079 check_enabled_event (jvmtiEvent type)
1081 bool *enabled;
1082 int offset;
1084 #define GET_OFFSET(Event) \
1085 do \
1087 enabled = &JVMTI::Event; \
1088 offset = offsetof (jvmtiEventCallbacks, Event); \
1090 while (0)
1092 switch (type)
1094 case JVMTI_EVENT_VM_INIT:
1095 GET_OFFSET (VMInit);
1096 break;
1098 case JVMTI_EVENT_VM_DEATH:
1099 GET_OFFSET (VMDeath);
1100 break;
1102 case JVMTI_EVENT_THREAD_START:
1103 GET_OFFSET (ThreadStart);
1104 break;
1106 case JVMTI_EVENT_THREAD_END:
1107 GET_OFFSET (ThreadEnd);
1108 break;
1110 case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
1111 GET_OFFSET (ClassFileLoadHook);
1112 break;
1114 case JVMTI_EVENT_CLASS_LOAD:
1115 GET_OFFSET (ClassLoad);
1116 break;
1118 case JVMTI_EVENT_CLASS_PREPARE:
1119 GET_OFFSET (ClassPrepare);
1120 break;
1122 case JVMTI_EVENT_VM_START:
1123 GET_OFFSET (VMStart);
1124 break;
1126 case JVMTI_EVENT_EXCEPTION:
1127 GET_OFFSET (Exception);
1128 break;
1130 case JVMTI_EVENT_EXCEPTION_CATCH:
1131 GET_OFFSET (ExceptionCatch);
1132 break;
1134 case JVMTI_EVENT_SINGLE_STEP:
1135 GET_OFFSET (SingleStep);
1136 break;
1138 case JVMTI_EVENT_FRAME_POP:
1139 GET_OFFSET (FramePop);
1140 break;
1142 case JVMTI_EVENT_BREAKPOINT:
1143 GET_OFFSET (Breakpoint);
1144 break;
1146 case JVMTI_EVENT_FIELD_ACCESS:
1147 GET_OFFSET (FieldAccess);
1148 break;
1150 case JVMTI_EVENT_FIELD_MODIFICATION:
1151 GET_OFFSET (FieldModification);
1152 break;
1154 case JVMTI_EVENT_METHOD_ENTRY:
1155 GET_OFFSET (MethodEntry);
1156 break;
1158 case JVMTI_EVENT_METHOD_EXIT:
1159 GET_OFFSET (MethodExit);
1160 break;
1162 case JVMTI_EVENT_NATIVE_METHOD_BIND:
1163 GET_OFFSET (NativeMethodBind);
1164 break;
1166 case JVMTI_EVENT_COMPILED_METHOD_LOAD:
1167 GET_OFFSET (CompiledMethodLoad);
1168 break;
1170 case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
1171 GET_OFFSET (CompiledMethodUnload);
1172 break;
1174 case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
1175 GET_OFFSET (DynamicCodeGenerated);
1176 break;
1178 case JVMTI_EVENT_DATA_DUMP_REQUEST:
1179 GET_OFFSET (DataDumpRequest);
1180 break;
1182 case JVMTI_EVENT_MONITOR_WAIT:
1183 GET_OFFSET (MonitorWait);
1184 break;
1186 case JVMTI_EVENT_MONITOR_WAITED:
1187 GET_OFFSET (MonitorWaited);
1188 break;
1190 case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
1191 GET_OFFSET (MonitorContendedEnter);
1192 break;
1194 case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
1195 GET_OFFSET (MonitorContendedEntered);
1196 break;
1198 case JVMTI_EVENT_GARBAGE_COLLECTION_START:
1199 GET_OFFSET (GarbageCollectionStart);
1200 break;
1202 case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
1203 GET_OFFSET (GarbageCollectionFinish);
1204 break;
1206 case JVMTI_EVENT_OBJECT_FREE:
1207 GET_OFFSET (ObjectFree);
1208 break;
1210 case JVMTI_EVENT_VM_OBJECT_ALLOC:
1211 GET_OFFSET (VMObjectAlloc);
1212 break;
1214 default:
1215 fprintf (stderr,
1216 "libgcj: check_enabled_event for unknown JVMTI event (%d)\n",
1217 (int) type);
1218 return;
1220 #undef GET_OFFSET
1222 int index = EVENT_INDEX (type); // safe since caller checks this
1224 if (_jvmtiEnvironments != NULL)
1226 _envListLock->readLock ()->lock ();
1227 struct jvmti_env_list *e;
1228 FOREACH_ENVIRONMENT (e)
1230 char *addr
1231 = reinterpret_cast<char *> (&e->env->callbacks) + offset;
1232 void **callback = reinterpret_cast<void **> (addr);
1233 if (e->env->enabled[index] && *callback != NULL)
1235 *enabled = true;
1236 _envListLock->readLock ()->unlock ();
1237 return;
1241 _envListLock->readLock ()->unlock ();
1244 *enabled = false;
1247 static void
1248 check_enabled_events ()
1250 check_enabled_event (JVMTI_EVENT_VM_INIT);
1251 check_enabled_event (JVMTI_EVENT_VM_DEATH);
1252 check_enabled_event (JVMTI_EVENT_THREAD_START);
1253 check_enabled_event (JVMTI_EVENT_THREAD_END);
1254 check_enabled_event (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK);
1255 check_enabled_event (JVMTI_EVENT_CLASS_LOAD);
1256 check_enabled_event (JVMTI_EVENT_CLASS_PREPARE);
1257 check_enabled_event (JVMTI_EVENT_VM_START);
1258 check_enabled_event (JVMTI_EVENT_EXCEPTION);
1259 check_enabled_event (JVMTI_EVENT_EXCEPTION_CATCH);
1260 check_enabled_event (JVMTI_EVENT_SINGLE_STEP);
1261 check_enabled_event (JVMTI_EVENT_FRAME_POP);
1262 check_enabled_event (JVMTI_EVENT_BREAKPOINT);
1263 check_enabled_event (JVMTI_EVENT_FIELD_ACCESS);
1264 check_enabled_event (JVMTI_EVENT_FIELD_MODIFICATION);
1265 check_enabled_event (JVMTI_EVENT_METHOD_ENTRY);
1266 check_enabled_event (JVMTI_EVENT_METHOD_EXIT);
1267 check_enabled_event (JVMTI_EVENT_NATIVE_METHOD_BIND);
1268 check_enabled_event (JVMTI_EVENT_COMPILED_METHOD_LOAD);
1269 check_enabled_event (JVMTI_EVENT_COMPILED_METHOD_UNLOAD);
1270 check_enabled_event (JVMTI_EVENT_DYNAMIC_CODE_GENERATED);
1271 check_enabled_event (JVMTI_EVENT_DATA_DUMP_REQUEST);
1272 check_enabled_event (JVMTI_EVENT_MONITOR_WAIT);
1273 check_enabled_event (JVMTI_EVENT_MONITOR_WAITED);
1274 check_enabled_event (JVMTI_EVENT_MONITOR_CONTENDED_ENTER);
1275 check_enabled_event (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED);
1276 check_enabled_event (JVMTI_EVENT_GARBAGE_COLLECTION_START);
1277 check_enabled_event (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH);
1278 check_enabled_event (JVMTI_EVENT_OBJECT_FREE);
1279 check_enabled_event (JVMTI_EVENT_VM_OBJECT_ALLOC);
1282 static jvmtiError JNICALL
1283 _Jv_JVMTI_SetEventNotificationMode (jvmtiEnv *env, jvmtiEventMode mode,
1284 jvmtiEvent type, jthread event_thread, ...)
1286 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
1288 if (event_thread != NULL)
1290 THREAD_CHECK_VALID (event_thread);
1291 THREAD_CHECK_IS_ALIVE (event_thread);
1294 bool enabled;
1295 switch (mode)
1297 case JVMTI_DISABLE:
1298 enabled = false;
1299 break;
1300 case JVMTI_ENABLE:
1301 enabled = true;
1302 break;
1304 default:
1305 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1308 switch (type)
1310 case JVMTI_EVENT_VM_INIT:
1311 case JVMTI_EVENT_VM_DEATH:
1312 case JVMTI_EVENT_THREAD_START:
1313 case JVMTI_EVENT_VM_START:
1314 case JVMTI_EVENT_COMPILED_METHOD_LOAD:
1315 case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
1316 case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
1317 case JVMTI_EVENT_DATA_DUMP_REQUEST:
1318 ILLEGAL_ARGUMENT (event_thread != NULL);
1319 break;
1321 case JVMTI_EVENT_THREAD_END:
1322 case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
1323 case JVMTI_EVENT_CLASS_LOAD:
1324 case JVMTI_EVENT_CLASS_PREPARE:
1325 case JVMTI_EVENT_EXCEPTION:
1326 case JVMTI_EVENT_EXCEPTION_CATCH:
1327 case JVMTI_EVENT_SINGLE_STEP:
1328 case JVMTI_EVENT_FRAME_POP:
1329 case JVMTI_EVENT_BREAKPOINT:
1330 case JVMTI_EVENT_FIELD_ACCESS:
1331 case JVMTI_EVENT_FIELD_MODIFICATION:
1332 case JVMTI_EVENT_METHOD_ENTRY:
1333 case JVMTI_EVENT_METHOD_EXIT:
1334 case JVMTI_EVENT_NATIVE_METHOD_BIND:
1335 case JVMTI_EVENT_MONITOR_WAIT:
1336 case JVMTI_EVENT_MONITOR_WAITED:
1337 case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
1338 case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
1339 case JVMTI_EVENT_GARBAGE_COLLECTION_START:
1340 case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
1341 case JVMTI_EVENT_OBJECT_FREE:
1342 case JVMTI_EVENT_VM_OBJECT_ALLOC:
1343 break;
1345 default:
1346 return JVMTI_ERROR_INVALID_EVENT_TYPE;
1349 env->thread[EVENT_INDEX(type)] = event_thread;
1350 env->enabled[EVENT_INDEX(type)] = enabled;
1351 check_enabled_event (type);
1352 return JVMTI_ERROR_NONE;
1355 static jvmtiError JNICALL
1356 _Jv_JVMTI_SetEventCallbacks (jvmtiEnv *env,
1357 const jvmtiEventCallbacks *callbacks,
1358 jint size_of_callbacks)
1360 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
1361 ILLEGAL_ARGUMENT (size_of_callbacks < 0);
1363 // Copy the list of callbacks into the environment
1364 memcpy (&env->callbacks, callbacks, sizeof (jvmtiEventCallbacks));
1366 /* Check which events are now enabeld (JVMTI makes no requirements
1367 about the order in which SetEventCallbacks and SetEventNotifications
1368 are called. So we must check all events here. */
1369 check_enabled_events ();
1371 return JVMTI_ERROR_NONE;
1374 static jvmtiError JNICALL
1375 _Jv_JVMTI_GetErrorName (MAYBE_UNUSED jvmtiEnv *env, jvmtiError error,
1376 char **name_ptr)
1378 NULL_CHECK (name_ptr);
1380 const char *name;
1381 switch (error)
1383 case JVMTI_ERROR_NONE:
1384 name = "none";
1385 break;
1387 case JVMTI_ERROR_NULL_POINTER:
1388 name = "null pointer";
1389 break;
1391 case JVMTI_ERROR_OUT_OF_MEMORY:
1392 name = "out of memory";
1393 break;
1395 case JVMTI_ERROR_ACCESS_DENIED:
1396 name = "access denied";
1397 break;
1399 case JVMTI_ERROR_WRONG_PHASE:
1400 name = "wrong phase";
1401 break;
1403 case JVMTI_ERROR_INTERNAL:
1404 name = "internal error";
1405 break;
1407 case JVMTI_ERROR_UNATTACHED_THREAD:
1408 name = "unattached thread";
1409 break;
1411 case JVMTI_ERROR_INVALID_ENVIRONMENT:
1412 name = "invalid environment";
1413 break;
1415 case JVMTI_ERROR_INVALID_PRIORITY:
1416 name = "invalid priority";
1417 break;
1419 case JVMTI_ERROR_THREAD_NOT_SUSPENDED:
1420 name = "thread not suspended";
1421 break;
1423 case JVMTI_ERROR_THREAD_SUSPENDED:
1424 name = "thread suspended";
1425 break;
1427 case JVMTI_ERROR_THREAD_NOT_ALIVE:
1428 name = "thread not alive";
1429 break;
1431 case JVMTI_ERROR_CLASS_NOT_PREPARED:
1432 name = "class not prepared";
1433 break;
1435 case JVMTI_ERROR_NO_MORE_FRAMES:
1436 name = "no more frames";
1437 break;
1439 case JVMTI_ERROR_OPAQUE_FRAME:
1440 name = "opaque frame";
1441 break;
1443 case JVMTI_ERROR_DUPLICATE:
1444 name = "duplicate";
1445 break;
1447 case JVMTI_ERROR_NOT_FOUND:
1448 name = "not found";
1449 break;
1451 case JVMTI_ERROR_NOT_MONITOR_OWNER:
1452 name = "not monitor owner";
1453 break;
1455 case JVMTI_ERROR_INTERRUPT:
1456 name = "interrupted";
1457 break;
1459 case JVMTI_ERROR_UNMODIFIABLE_CLASS:
1460 name = "unmodifiable class";
1461 break;
1463 case JVMTI_ERROR_NOT_AVAILABLE:
1464 name = "not available";
1465 break;
1467 case JVMTI_ERROR_ABSENT_INFORMATION:
1468 name = "absent information";
1469 break;
1471 case JVMTI_ERROR_INVALID_EVENT_TYPE:
1472 name = "invalid event type";
1473 break;
1475 case JVMTI_ERROR_NATIVE_METHOD:
1476 name = "native method";
1477 break;
1479 case JVMTI_ERROR_INVALID_THREAD:
1480 name = "invalid thread";
1481 break;
1483 case JVMTI_ERROR_INVALID_THREAD_GROUP:
1484 name = "invalid thread group";
1485 break;
1487 case JVMTI_ERROR_INVALID_OBJECT:
1488 name = "invalid object";
1489 break;
1491 case JVMTI_ERROR_INVALID_CLASS:
1492 name = "invalid class";
1493 break;
1495 case JVMTI_ERROR_INVALID_METHODID:
1496 name = "invalid method ID";
1497 break;
1499 case JVMTI_ERROR_INVALID_LOCATION:
1500 name = "invalid location";
1501 break;
1503 case JVMTI_ERROR_INVALID_FIELDID:
1504 name = "invalid field ID";
1505 break;
1507 case JVMTI_ERROR_TYPE_MISMATCH:
1508 name = "type mismatch";
1509 break;
1511 case JVMTI_ERROR_INVALID_SLOT:
1512 name = "invalid slot";
1513 break;
1515 case JVMTI_ERROR_INVALID_MONITOR:
1516 name = "invalid monitor";
1517 break;
1519 case JVMTI_ERROR_INVALID_CLASS_FORMAT:
1520 name = "invalid class format";
1521 break;
1523 case JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION:
1524 name = "circular class definition";
1525 break;
1527 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED:
1528 name = "unsupported redefinition: method added";
1529 break;
1531 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED:
1532 name = "unsupported redefinition: schema changed";
1533 break;
1535 case JVMTI_ERROR_INVALID_TYPESTATE:
1536 name = "invalid type state";
1537 break;
1539 case JVMTI_ERROR_FAILS_VERIFICATION:
1540 name = "fails verification";
1541 break;
1543 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED:
1544 name = "unsupported redefinition: hierarchy changed";
1545 break;
1547 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED:
1548 name = "unsupported redefinition: method deleted";
1549 break;
1551 case JVMTI_ERROR_UNSUPPORTED_VERSION:
1552 name = "unsupported version";
1553 break;
1555 case JVMTI_ERROR_NAMES_DONT_MATCH:
1556 name = "names do not match";
1557 break;
1559 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED:
1560 name = "unsupported redefinition: class modifiers changed";
1561 break;
1563 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED:
1564 name = "unsupported redefinition: method modifiers changed";
1565 break;
1567 case JVMTI_ERROR_MUST_POSSESS_CAPABILITY:
1568 name = "must possess capability";
1569 break;
1571 case JVMTI_ERROR_ILLEGAL_ARGUMENT:
1572 name = "illegal argument";
1573 break;
1575 default:
1576 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1579 *name_ptr = (char *) _Jv_MallocUnchecked (strlen (name) + 1);
1580 if (*name_ptr == NULL)
1581 return JVMTI_ERROR_OUT_OF_MEMORY;
1583 strcpy (*name_ptr, name);
1584 return JVMTI_ERROR_NONE;
1587 #define RESERVED NULL
1588 #define UNIMPLEMENTED NULL
1590 struct _Jv_jvmtiEnv _Jv_JVMTI_Interface =
1592 RESERVED, // reserved1
1593 _Jv_JVMTI_SetEventNotificationMode, // SetEventNotificationMode
1594 RESERVED, // reserved3
1595 _Jv_JVMTI_GetAllThreads, // GetAllThreads
1596 _Jv_JVMTI_SuspendThread, // SuspendThread
1597 _Jv_JVMTI_ResumeThread, // ResumeThread
1598 UNIMPLEMENTED, // StopThread
1599 _Jv_JVMTI_InterruptThread, // InterruptThread
1600 UNIMPLEMENTED, // GetThreadInfo
1601 UNIMPLEMENTED, // GetOwnedMonitorInfo
1602 UNIMPLEMENTED, // GetCurrentContendedMonitor
1603 UNIMPLEMENTED, // RunAgentThread
1604 UNIMPLEMENTED, // GetTopThreadGroups
1605 UNIMPLEMENTED, // GetThreadGroupInfo
1606 UNIMPLEMENTED, // GetThreadGroupChildren
1607 _Jv_JVMTI_GetFrameCount, // GetFrameCount
1608 UNIMPLEMENTED, // GetThreadState
1609 RESERVED, // reserved18
1610 UNIMPLEMENTED, // GetFrameLocation
1611 UNIMPLEMENTED, // NotifyPopFrame
1612 UNIMPLEMENTED, // GetLocalObject
1613 UNIMPLEMENTED, // GetLocalInt
1614 UNIMPLEMENTED, // GetLocalLong
1615 UNIMPLEMENTED, // GetLocalFloat
1616 UNIMPLEMENTED, // GetLocalDouble
1617 UNIMPLEMENTED, // SetLocalObject
1618 UNIMPLEMENTED, // SetLocalInt
1619 UNIMPLEMENTED, // SetLocalLong
1620 UNIMPLEMENTED, // SetLocalFloat
1621 UNIMPLEMENTED, // SetLocalDouble
1622 _Jv_JVMTI_CreateRawMonitor, // CreateRawMonitor
1623 _Jv_JVMTI_DestroyRawMonitor, // DestroyRawMonitor
1624 _Jv_JVMTI_RawMonitorEnter, // RawMonitorEnter
1625 _Jv_JVMTI_RawMonitorExit, // RawMonitorExit
1626 _Jv_JVMTI_RawMonitorWait, // RawMonitorWait
1627 _Jv_JVMTI_RawMonitorNotify, // RawMonitorNotify
1628 _Jv_JVMTI_RawMonitorNotifyAll, // RawMonitorNotifyAll
1629 _Jv_JVMTI_SetBreakpoint, // SetBreakpoint
1630 _Jv_JVMTI_ClearBreakpoint, // ClearBreakpoint
1631 RESERVED, // reserved40
1632 UNIMPLEMENTED, // SetFieldAccessWatch
1633 UNIMPLEMENTED, // ClearFieldAccessWatch
1634 UNIMPLEMENTED, // SetFieldModificationWatch
1635 UNIMPLEMENTED, // ClearFieldModificationWatch
1636 RESERVED, // reserved45
1637 _Jv_JVMTI_Allocate, // Allocate
1638 _Jv_JVMTI_Deallocate, // Deallocate
1639 UNIMPLEMENTED, // GetClassSignature
1640 _Jv_JVMTI_GetClassStatus, // GetClassStatus
1641 UNIMPLEMENTED, // GetSourceFileName
1642 _Jv_JVMTI_GetClassModifiers, // GetClassModifiers
1643 _Jv_JVMTI_GetClassMethods, // GetClassMethods
1644 UNIMPLEMENTED, // GetClassFields
1645 UNIMPLEMENTED, // GetImplementedInterfaces
1646 _Jv_JVMTI_IsInterface, // IsInterface
1647 _Jv_JVMTI_IsArrayClass, // IsArrayClass
1648 _Jv_JVMTI_GetClassLoader, // GetClassLoader
1649 _Jv_JVMTI_GetObjectHashCode, // GetObjectHashCode
1650 UNIMPLEMENTED, // GetObjectMonitorUsage
1651 UNIMPLEMENTED, // GetFieldName
1652 UNIMPLEMENTED, // GetFieldDeclaringClass
1653 _Jv_JVMTI_GetFieldModifiers, // GetFieldModifiers
1654 _Jv_JVMTI_IsFieldSynthetic, // IsFieldSynthetic
1655 _Jv_JVMTI_GetMethodName, // GetMethodName
1656 _Jv_JVMTI_GetMethodDeclaringClass, // GetMethodDeclaringClass
1657 _Jv_JVMTI_GetMethodModifiers, // GetMethodModifers
1658 RESERVED, // reserved67
1659 UNIMPLEMENTED, // GetMaxLocals
1660 UNIMPLEMENTED, // GetArgumentsSize
1661 _Jv_JVMTI_GetLineNumberTable, // GetLineNumberTable
1662 UNIMPLEMENTED, // GetMethodLocation
1663 UNIMPLEMENTED, // GetLocalVariableTable
1664 RESERVED, // reserved73
1665 RESERVED, // reserved74
1666 UNIMPLEMENTED, // GetBytecodes
1667 _Jv_JVMTI_IsMethodNative, // IsMethodNative
1668 _Jv_JVMTI_IsMethodSynthetic, // IsMethodSynthetic
1669 UNIMPLEMENTED, // GetLoadedClasses
1670 _Jv_JVMTI_GetClassLoaderClasses, // GetClassLoaderClasses
1671 UNIMPLEMENTED, // PopFrame
1672 RESERVED, // reserved81
1673 RESERVED, // reserved82
1674 RESERVED, // reserved83
1675 RESERVED, // reserved84
1676 RESERVED, // reserved85
1677 RESERVED, // reserved86
1678 UNIMPLEMENTED, // RedefineClasses
1679 UNIMPLEMENTED, // GetVersionNumber
1680 UNIMPLEMENTED, // GetCapabilities
1681 UNIMPLEMENTED, // GetSourceDebugExtension
1682 UNIMPLEMENTED, // IsMethodObsolete
1683 UNIMPLEMENTED, // SuspendThreadList
1684 UNIMPLEMENTED, // ResumeThreadList
1685 RESERVED, // reserved94
1686 RESERVED, // reserved95
1687 RESERVED, // reserved96
1688 RESERVED, // reserved97
1689 RESERVED, // reserved98
1690 RESERVED, // reserved99
1691 UNIMPLEMENTED, // GetAllStackTraces
1692 UNIMPLEMENTED, // GetThreadListStackTraces
1693 UNIMPLEMENTED, // GetThreadLocalStorage
1694 UNIMPLEMENTED, // SetThreadLocalStorage
1695 _Jv_JVMTI_GetStackTrace, // GetStackTrace
1696 RESERVED, // reserved105
1697 UNIMPLEMENTED, // GetTag
1698 UNIMPLEMENTED, // SetTag
1699 _Jv_JVMTI_ForceGarbageCollection, // ForceGarbageCollection
1700 UNIMPLEMENTED, // IterateOverObjectsReachable
1701 UNIMPLEMENTED, // IterateOverReachableObjects
1702 UNIMPLEMENTED, // IterateOverHeap
1703 UNIMPLEMENTED, // IterateOverInstanceOfClass
1704 RESERVED, // reserved113
1705 UNIMPLEMENTED, // GetObjectsWithTags
1706 RESERVED, // reserved115
1707 RESERVED, // reserved116
1708 RESERVED, // reserved117
1709 RESERVED, // reserved118
1710 RESERVED, // reserved119
1711 _Jv_JVMTI_SetJNIFunctionTable, // SetJNIFunctionTable
1712 _Jv_JVMTI_GetJNIFunctionTable, // GetJNIFunctionTable
1713 _Jv_JVMTI_SetEventCallbacks, // SetEventCallbacks
1714 UNIMPLEMENTED, // GenerateEvents
1715 UNIMPLEMENTED, // GetExtensionFunctions
1716 UNIMPLEMENTED, // GetExtensionEvents
1717 UNIMPLEMENTED, // SetExtensionEventCallback
1718 _Jv_JVMTI_DisposeEnvironment, // DisposeEnvironment
1719 _Jv_JVMTI_GetErrorName, // GetErrorName
1720 UNIMPLEMENTED, // GetJLocationFormat
1721 UNIMPLEMENTED, // GetSystemProperties
1722 _Jv_JVMTI_GetSystemProperty, // GetSystemProperty
1723 _Jv_JVMTI_SetSystemProperty, // SetSystemProperty
1724 UNIMPLEMENTED, // GetPhase
1725 UNIMPLEMENTED, // GetCurrentThreadCpuTimerInfo
1726 UNIMPLEMENTED, // GetCurrentThreadCpuTime
1727 UNIMPLEMENTED, // GetThreadCpuTimerInfo
1728 UNIMPLEMENTED, // GetThreadCpuTime
1729 UNIMPLEMENTED, // GetTimerInfo
1730 _Jv_JVMTI_GetTime, // GetTime
1731 UNIMPLEMENTED, // GetPotentialCapabilities
1732 RESERVED, // reserved141
1733 UNIMPLEMENTED, // AddCapabilities
1734 UNIMPLEMENTED, // RelinquishCapabilities
1735 _Jv_JVMTI_GetAvailableProcessors, // GetAvailableProcessors
1736 RESERVED, // reserved145
1737 RESERVED, // reserved146
1738 UNIMPLEMENTED, // GetEnvironmentLocalStorage
1739 UNIMPLEMENTED, // SetEnvironmentLocalStorage
1740 _Jv_JVMTI_AddToBootstrapClassLoaderSearch, // AddToBootstrapClassLoaderSearch
1741 _Jv_JVMTI_SetVerboseFlag, // SetVerboseFlag
1742 RESERVED, // reserved151
1743 RESERVED, // reserved152
1744 RESERVED, // reserved153
1745 _Jv_JVMTI_GetObjectSize // GetObjectSize
1748 _Jv_JVMTIEnv *
1749 _Jv_GetJVMTIEnv (void)
1751 _Jv_JVMTIEnv *env
1752 = (_Jv_JVMTIEnv *) _Jv_MallocUnchecked (sizeof (_Jv_JVMTIEnv));
1753 env->p = &_Jv_JVMTI_Interface;
1754 struct jvmti_env_list *element
1755 = (struct jvmti_env_list *) _Jv_MallocUnchecked (sizeof (struct jvmti_env_list));
1756 element->env = env;
1757 element->next = NULL;
1759 _envListLock->writeLock ()->lock ();
1760 if (_jvmtiEnvironments == NULL)
1761 _jvmtiEnvironments = element;
1762 else
1764 struct jvmti_env_list *e;
1765 for (e = _jvmtiEnvironments; e->next != NULL; e = e->next)
1767 e->next = element;
1769 _envListLock->writeLock ()->unlock ();
1771 /* Mark JVMTI active. This is used to force the interpreter
1772 to use either debugging or non-debugging code. Once JVMTI
1773 has been enabled, the non-debug interpreter cannot be used. */
1774 JVMTI::enabled = true;
1775 return env;
1778 void
1779 _Jv_JVMTI_Init ()
1781 _jvmtiEnvironments = NULL;
1782 _envListLock
1783 = new java::util::concurrent::locks::ReentrantReadWriteLock ();
1785 // No environments, so this should set all JVMTI:: members to false
1786 check_enabled_events ();
1789 static void
1790 post_event (jvmtiEnv *env, jvmtiEvent type, jthread event_thread, va_list args)
1792 #define ARG(Type,Name) Type Name = (Type) va_arg (args, Type)
1794 #define GET_BOOLEAN_ARG(Name) \
1795 ARG (int, b); \
1796 jboolean Name = (b == 0) ? false : true
1798 #define GET_CHAR_ARG(Name) \
1799 ARG (int, c); \
1800 char Name = static_cast<char> (c)
1802 switch (type)
1804 case JVMTI_EVENT_VM_INIT:
1805 if (env->callbacks.VMInit != NULL)
1807 ARG (JNIEnv *, jni_env);
1808 env->callbacks.VMInit (env, jni_env, event_thread);
1810 break;
1812 case JVMTI_EVENT_VM_DEATH:
1813 if (env->callbacks.VMDeath != NULL)
1815 ARG (JNIEnv *, jni_env);
1816 env->callbacks.VMDeath (env, jni_env);
1818 break;
1820 case JVMTI_EVENT_THREAD_START:
1821 if (env->callbacks.ThreadStart != NULL)
1823 ARG (JNIEnv *, jni_env);
1824 env->callbacks.ThreadStart (env, jni_env, event_thread);
1826 break;
1828 case JVMTI_EVENT_THREAD_END:
1829 if (env->callbacks.ThreadEnd != NULL)
1831 ARG (JNIEnv *, jni_env);
1832 env->callbacks.ThreadEnd (env, jni_env, event_thread);
1834 break;
1836 case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
1837 if (env->callbacks.ClassFileLoadHook != NULL)
1839 ARG (JNIEnv *, jni_env);
1840 ARG (jclass, class_being_redefined);
1841 ARG (jobject, loader);
1842 ARG (const char *, name);
1843 ARG (jobject, protection_domain);
1844 ARG (jint, class_data_len);
1845 ARG (const unsigned char *, class_data);
1846 ARG (jint *, new_class_data_len);
1847 ARG (unsigned char **, new_class_data);
1848 env->callbacks.ClassFileLoadHook (env, jni_env,
1849 class_being_redefined, loader,
1850 name, protection_domain,
1851 class_data_len, class_data,
1852 new_class_data_len,
1853 new_class_data);
1855 break;
1857 case JVMTI_EVENT_CLASS_LOAD:
1858 if (env->callbacks.ClassLoad != NULL)
1860 ARG (JNIEnv *, jni_env);
1861 ARG (jclass, klass);
1862 env->callbacks.ClassLoad (env, jni_env, event_thread, klass);
1864 break;
1866 case JVMTI_EVENT_CLASS_PREPARE:
1867 if (env->callbacks.ClassPrepare != NULL)
1869 ARG (JNIEnv *, jni_env);
1870 ARG (jclass, klass);
1871 env->callbacks.ClassPrepare (env, jni_env, event_thread, klass);
1873 break;
1875 case JVMTI_EVENT_VM_START:
1876 if (env->callbacks.VMStart != NULL)
1878 ARG (JNIEnv *, jni_env);
1879 env->callbacks.VMStart (env, jni_env);
1881 break;
1883 case JVMTI_EVENT_EXCEPTION:
1884 if (env->callbacks.Exception != NULL)
1886 ARG (JNIEnv *, jni_env);
1887 ARG (jmethodID, method);
1888 ARG (jlocation, location);
1889 ARG (jobject, exception);
1890 ARG (jmethodID, catch_method);
1891 ARG (jlocation, catch_location);
1892 env->callbacks.Exception (env, jni_env, event_thread, method,
1893 location, exception, catch_method,
1894 catch_location);
1896 break;
1898 case JVMTI_EVENT_EXCEPTION_CATCH:
1899 if (env->callbacks.ExceptionCatch != NULL)
1901 ARG (JNIEnv *, jni_env);
1902 ARG (jmethodID, method);
1903 ARG (jlocation, location);
1904 ARG (jobject, exception);
1905 env->callbacks.ExceptionCatch (env, jni_env, event_thread, method,
1906 location, exception);
1908 break;
1910 case JVMTI_EVENT_SINGLE_STEP:
1911 if (env->callbacks.SingleStep != NULL)
1913 ARG (JNIEnv *, jni_env);
1914 ARG (jmethodID, method);
1915 ARG (jlocation, location);
1916 env->callbacks.SingleStep (env, jni_env, event_thread, method,
1917 location);
1919 break;
1921 case JVMTI_EVENT_FRAME_POP:
1922 if (env->callbacks.FramePop != NULL)
1924 ARG (JNIEnv *, jni_env);
1925 ARG (jmethodID, method);
1926 GET_BOOLEAN_ARG (was_popped_by_exception);
1927 env->callbacks.FramePop (env, jni_env, event_thread, method,
1928 was_popped_by_exception);
1930 break;
1932 case JVMTI_EVENT_BREAKPOINT:
1933 if (env->callbacks.Breakpoint != NULL)
1935 ARG (JNIEnv *, jni_env);
1936 ARG (jmethodID, method);
1937 ARG (jlocation, location);
1938 env->callbacks.Breakpoint (env, jni_env, event_thread, method,
1939 location);
1941 break;
1943 case JVMTI_EVENT_FIELD_ACCESS:
1944 if (env->callbacks.FieldAccess != NULL)
1946 ARG (JNIEnv *, jni_env);
1947 ARG (jmethodID, method);
1948 ARG (jlocation, location);
1949 ARG (jclass, field_class);
1950 ARG (jobject, object);
1951 ARG (jfieldID, field);
1952 env->callbacks.FieldAccess (env, jni_env, event_thread, method,
1953 location, field_class, object, field);
1955 break;
1957 case JVMTI_EVENT_FIELD_MODIFICATION:
1958 if (env->callbacks.FieldModification != NULL)
1960 ARG (JNIEnv *, jni_env);
1961 ARG (jmethodID, method);
1962 ARG (jlocation, location);
1963 ARG (jclass, field_class);
1964 ARG (jobject, object);
1965 ARG (jfieldID, field);
1966 GET_CHAR_ARG (signature_type);
1967 ARG (jvalue, new_value);
1968 env->callbacks.FieldModification (env, jni_env, event_thread, method,
1969 location, field_class, object,
1970 field, signature_type, new_value);
1972 break;
1974 case JVMTI_EVENT_METHOD_ENTRY:
1975 if (env->callbacks.MethodEntry != NULL)
1977 ARG (JNIEnv *, jni_env);
1978 ARG (jmethodID, method);
1979 env->callbacks.MethodEntry (env, jni_env, event_thread, method);
1981 break;
1983 case JVMTI_EVENT_METHOD_EXIT:
1984 if (env->callbacks.MethodExit != NULL)
1986 ARG (JNIEnv *, jni_env);
1987 ARG (jmethodID, method);
1988 GET_BOOLEAN_ARG (was_popped_by_exception);
1989 ARG (jvalue, return_value);
1990 env->callbacks.MethodExit (env, jni_env, event_thread, method,
1991 was_popped_by_exception, return_value);
1993 break;
1995 case JVMTI_EVENT_NATIVE_METHOD_BIND:
1996 if (env->callbacks.NativeMethodBind != NULL)
1998 ARG (JNIEnv *, jni_env);
1999 ARG (jmethodID, method);
2000 ARG (void *, address);
2001 ARG (void **, new_address_ptr);
2002 env->callbacks.NativeMethodBind (env, jni_env, event_thread, method,
2003 address, new_address_ptr);
2005 break;
2007 case JVMTI_EVENT_COMPILED_METHOD_LOAD:
2008 if (env->callbacks.CompiledMethodLoad != NULL)
2010 ARG (jmethodID, method);
2011 ARG (jint, code_size);
2012 ARG (const void *, code_addr);
2013 ARG (jint, map_length);
2014 ARG (const jvmtiAddrLocationMap *, map);
2015 ARG (const void *, compile_info);
2016 env->callbacks.CompiledMethodLoad (env, method, code_size, code_addr,
2017 map_length, map, compile_info);
2019 break;
2021 case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
2022 if (env->callbacks.CompiledMethodUnload != NULL)
2024 ARG (jmethodID, method);
2025 ARG (const void *, code_addr);
2026 env->callbacks.CompiledMethodUnload (env, method, code_addr);
2028 break;
2030 case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
2031 if (env->callbacks.DynamicCodeGenerated != NULL)
2033 ARG (const char *, name);
2034 ARG (const void *, address);
2035 ARG (jint, length);
2036 env->callbacks.DynamicCodeGenerated (env, name, address, length);
2038 break;
2040 case JVMTI_EVENT_DATA_DUMP_REQUEST:
2041 if (env->callbacks.DataDumpRequest != NULL)
2043 env->callbacks.DataDumpRequest (env);
2045 break;
2047 case JVMTI_EVENT_MONITOR_WAIT:
2048 if (env->callbacks.MonitorWait != NULL)
2050 ARG (JNIEnv *, jni_env);
2051 ARG (jobject, object);
2052 ARG (jlong, timeout);
2053 env->callbacks.MonitorWait (env, jni_env, event_thread, object,
2054 timeout);
2056 break;
2058 case JVMTI_EVENT_MONITOR_WAITED:
2059 if (env->callbacks.MonitorWaited != NULL)
2061 ARG (JNIEnv *, jni_env);
2062 ARG (jobject, object);
2063 GET_BOOLEAN_ARG (timed_out);
2064 env->callbacks.MonitorWaited (env, jni_env, event_thread, object,
2065 timed_out);
2067 break;
2069 case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
2070 if (env->callbacks.MonitorContendedEnter != NULL)
2072 ARG (JNIEnv *, jni_env);
2073 ARG (jobject, object);
2074 env->callbacks.MonitorContendedEnter (env, jni_env, event_thread,
2075 object);
2077 break;
2079 case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
2080 if (env->callbacks.MonitorContendedEntered != NULL)
2082 ARG (JNIEnv *, jni_env);
2083 ARG (jobject, object);
2084 env->callbacks.MonitorContendedEntered (env, jni_env, event_thread,
2085 object);
2087 break;
2089 case JVMTI_EVENT_GARBAGE_COLLECTION_START:
2090 if (env->callbacks.GarbageCollectionStart != NULL)
2092 env->callbacks.GarbageCollectionStart (env);
2094 break;
2096 case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
2097 if (env->callbacks.GarbageCollectionFinish != NULL)
2099 env->callbacks.GarbageCollectionFinish (env);
2101 break;
2103 case JVMTI_EVENT_OBJECT_FREE:
2104 if (env->callbacks.ObjectFree != NULL)
2106 ARG (jlong, tag);
2107 env->callbacks.ObjectFree (env, tag);
2109 break;
2111 case JVMTI_EVENT_VM_OBJECT_ALLOC:
2112 if (env->callbacks.VMObjectAlloc != NULL)
2114 ARG (JNIEnv *, jni_env);
2115 ARG (jobject, object);
2116 ARG (jclass, object_class);
2117 ARG (jlong, size);
2118 env->callbacks.VMObjectAlloc (env, jni_env, event_thread,
2119 object, object_class, size);
2121 break;
2123 default:
2124 fprintf (stderr, "libgcj: post of unknown JVMTI event (%d)\n",
2125 (int) type);
2126 break;
2128 va_end (args);
2129 #undef ARG
2130 #undef GET_BOOLEAN_ARG
2131 #undef GET_CHAR_ARG
2134 /* Post an event to requesting JVMTI environments
2136 * This function should not be called without consulting the
2137 * JVMTI_REQUESTED_EVENT macro first (for speed). It does no real
2138 * harm (other than kill speed), since this function will still
2139 * only send the event if it was properly requested by an environment.
2141 void
2142 _Jv_JVMTI_PostEvent (jvmtiEvent type, jthread event_thread, ...)
2144 va_list args;
2145 va_start (args, event_thread);
2147 _envListLock->readLock ()->lock ();
2148 struct jvmti_env_list *e;
2149 FOREACH_ENVIRONMENT (e)
2151 /* Events are only posted if the event was explicitly enabled,
2152 it has a registered event handler, and the event thread
2153 matches (either globally or restricted to a specific thread).
2154 Here we check all but the event handler, which will be handled
2155 by post_event. */
2156 if (e->env->enabled[EVENT_INDEX(type)]
2157 && (e->env->thread[EVENT_INDEX(type)] == NULL
2158 || e->env->thread[EVENT_INDEX(type)] == event_thread))
2160 post_event (e->env, type, event_thread, args);
2163 _envListLock->readLock ()->unlock ();
2164 va_end (args);