Daily bump.
[official-gcc.git] / libjava / jvmti.cc
blobc9c7e7ba731a9893543c5489159a1b1277ae2486
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/Object.h>
31 #include <java/lang/OutOfMemoryError.h>
32 #include <java/lang/Thread.h>
33 #include <java/lang/ThreadGroup.h>
34 #include <java/lang/Throwable.h>
35 #include <java/lang/VMClassLoader.h>
36 #include <java/lang/reflect/Field.h>
37 #include <java/lang/reflect/Modifier.h>
38 #include <java/util/Collection.h>
39 #include <java/util/HashMap.h>
40 #include <java/net/URL.h>
42 static void check_enabled_events (void);
43 static void check_enabled_event (jvmtiEvent);
45 namespace JVMTI
47 bool VMInit = false;
48 bool VMDeath = false;
49 bool ThreadStart = false;
50 bool ThreadEnd = false;
51 bool ClassFileLoadHook = false;
52 bool ClassLoad = false;
53 bool ClassPrepare = false;
54 bool VMStart = false;
55 bool Exception = false;
56 bool ExceptionCatch = false;
57 bool SingleStep = false;
58 bool FramePop = false;
59 bool Breakpoint = false;
60 bool FieldAccess = false;
61 bool FieldModification = false;
62 bool MethodEntry = false;
63 bool MethodExit = false;
64 bool NativeMethodBind = false;
65 bool CompiledMethodLoad = false;
66 bool CompiledMethodUnload = false;
67 bool DynamicCodeGenerated = false;
68 bool DataDumpRequest = false;
69 bool reserved72 = false;
70 bool MonitorWait = false;
71 bool MonitorWaited = false;
72 bool MonitorContendedEnter = false;
73 bool MonitorContendedEntered = false;
74 bool reserved77 = false;
75 bool reserved78 = false;
76 bool reserved79 = false;
77 bool reserved80 = false;
78 bool GarbageCollectionStart = false;
79 bool GarbageCollectionFinish = false;
80 bool ObjectFree = false;
81 bool VMObjectAlloc = false;
84 extern struct JNINativeInterface _Jv_JNIFunctions;
86 struct _Jv_rawMonitorID
88 _Jv_Mutex_t mutex;
89 _Jv_ConditionVariable_t condition;
92 /* A simple linked list of all JVMTI environments. Since
93 events must be delivered to environments in the order
94 in which the environments were created, new environments
95 are added to the end of the list. */
96 struct jvmti_env_list
98 jvmtiEnv *env;
99 struct jvmti_env_list *next;
101 static struct jvmti_env_list *_jvmtiEnvironments = NULL;
102 static java::lang::Object *_envListLock = NULL;
103 #define FOREACH_ENVIRONMENT(Ele) \
104 for (Ele = _jvmtiEnvironments; Ele != NULL; Ele = Ele->next)
106 // Some commonly-used checks
108 #define THREAD_DEFAULT_TO_CURRENT(Ajthread) \
109 do \
111 if (Ajthread == NULL) \
112 Ajthread = java::lang::Thread::currentThread (); \
114 while (0)
116 #define THREAD_CHECK_VALID(Athread) \
117 do \
119 if (!java::lang::Thread::class$.isAssignableFrom (&(Athread->class$))) \
120 return JVMTI_ERROR_INVALID_THREAD; \
122 while (0)
124 #define THREAD_CHECK_IS_ALIVE(Athread) \
125 do \
127 if (!Athread->isAlive ()) \
128 return JVMTI_ERROR_THREAD_NOT_ALIVE; \
130 while (0)
132 // FIXME: if current phase is not set in Phases,
133 // return JVMTI_ERROR_WRONG_PHASE
134 #define REQUIRE_PHASE(Env, Phases)
136 #define NULL_CHECK(Ptr) \
137 do \
139 if (Ptr == NULL) \
140 return JVMTI_ERROR_NULL_POINTER; \
142 while (0)
144 #define ILLEGAL_ARGUMENT(Cond) \
145 do \
147 if ((Cond)) \
148 return JVMTI_ERROR_ILLEGAL_ARGUMENT; \
150 while (0)
152 static jvmtiError JNICALL
153 _Jv_JVMTI_SuspendThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
155 using namespace java::lang;
157 THREAD_DEFAULT_TO_CURRENT (thread);
158 THREAD_CHECK_VALID (thread);
159 THREAD_CHECK_IS_ALIVE (thread);
161 _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
162 _Jv_SuspendThread (data);
163 return JVMTI_ERROR_NONE;
166 static jvmtiError JNICALL
167 _Jv_JVMTI_ResumeThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
169 using namespace java::lang;
171 THREAD_DEFAULT_TO_CURRENT (thread);
172 THREAD_CHECK_VALID (thread);
173 THREAD_CHECK_IS_ALIVE (thread);
175 _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
176 _Jv_ResumeThread (data);
177 return JVMTI_ERROR_NONE;
180 static jvmtiError JNICALL
181 _Jv_JVMTI_InterruptThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
183 using namespace java::lang;
185 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
186 // FIXME: capability handling? 'can_signal_thread'
187 if (thread == NULL)
188 return JVMTI_ERROR_INVALID_THREAD;
190 THREAD_CHECK_VALID (thread);
191 THREAD_CHECK_IS_ALIVE (thread);
192 thread->interrupt();
193 return JVMTI_ERROR_NONE;
196 static jvmtiError JNICALL
197 _Jv_JVMTI_GetAllThreads(MAYBE_UNUSED jvmtiEnv *env, jint *thread_cnt,
198 jthread **threads)
200 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
201 NULL_CHECK (thread_cnt);
202 NULL_CHECK (threads);
204 using namespace java::lang;
206 ThreadGroup *root_grp = ThreadGroup::root;
207 jint estimate = root_grp->activeCount ();
209 JArray<Thread *> *thr_arr;
211 // Allocate some extra space since threads can be created between calls
214 thr_arr = reinterpret_cast<JArray<Thread *> *> (JvNewObjectArray
215 ((estimate * 2),
216 &Thread::class$, NULL));
218 catch (java::lang::OutOfMemoryError *err)
220 return JVMTI_ERROR_OUT_OF_MEMORY;
223 *thread_cnt = root_grp->enumerate (thr_arr);
225 jvmtiError jerr = env->Allocate ((jlong) ((*thread_cnt) * sizeof (jthread)),
226 (unsigned char **) threads);
228 if (jerr != JVMTI_ERROR_NONE)
229 return jerr;
231 // Transfer the threads to the result array
232 jthread *tmp_arr = reinterpret_cast<jthread *> (elements (thr_arr));
234 memcpy ((*threads), tmp_arr, (*thread_cnt));
236 return JVMTI_ERROR_NONE;
239 static jvmtiError JNICALL
240 _Jv_JVMTI_GetFrameCount (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
241 jint* frame_count)
243 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
245 NULL_CHECK (frame_count);
247 using namespace java::lang;
249 THREAD_DEFAULT_TO_CURRENT (thread);
251 Thread *thr = reinterpret_cast<Thread *> (thread);
252 THREAD_CHECK_VALID (thr);
253 THREAD_CHECK_IS_ALIVE (thr);
255 _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thr->frame);
256 (*frame_count) = 0;
258 while (frame != NULL)
260 (*frame_count)++;
261 frame = frame->next;
264 return JVMTI_ERROR_NONE;
267 static jvmtiError JNICALL
268 _Jv_JVMTI_CreateRawMonitor (MAYBE_UNUSED jvmtiEnv *env, const char *name,
269 jrawMonitorID *result)
271 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
272 NULL_CHECK (name);
273 NULL_CHECK (result);
274 *result = (jrawMonitorID) _Jv_MallocUnchecked (sizeof (_Jv_rawMonitorID));
275 if (*result == NULL)
276 return JVMTI_ERROR_OUT_OF_MEMORY;
277 _Jv_MutexInit (&(*result)->mutex);
278 _Jv_CondInit (&(*result)->condition);
279 return JVMTI_ERROR_NONE;
282 static jvmtiError JNICALL
283 _Jv_JVMTI_DestroyRawMonitor (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
285 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
286 // Note we have no better way of knowing whether this object is
287 // really a raw monitor.
288 if (monitor == NULL)
289 return JVMTI_ERROR_INVALID_MONITOR;
290 // FIXME: perform checks on monitor, release it if this thread owns
291 // it.
292 #ifdef _Jv_HaveMutexDestroy
293 _Jv_MutexDestroy (&monitor->mutex);
294 #endif
295 _Jv_Free (monitor);
296 return JVMTI_ERROR_NONE;
299 static jvmtiError JNICALL
300 _Jv_JVMTI_RawMonitorEnter (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
302 if (monitor == NULL)
303 return JVMTI_ERROR_INVALID_MONITOR;
304 _Jv_MutexLock (&monitor->mutex);
305 return JVMTI_ERROR_NONE;
308 static jvmtiError JNICALL
309 _Jv_JVMTI_RawMonitorExit (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
311 if (monitor == NULL)
312 return JVMTI_ERROR_INVALID_MONITOR;
313 if (_Jv_MutexUnlock (&monitor->mutex))
314 return JVMTI_ERROR_NOT_MONITOR_OWNER;
315 return JVMTI_ERROR_NONE;
318 static jvmtiError JNICALL
319 _Jv_JVMTI_RawMonitorWait (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor,
320 jlong millis)
322 if (monitor == NULL)
323 return JVMTI_ERROR_INVALID_MONITOR;
324 int r = _Jv_CondWait (&monitor->condition, &monitor->mutex, millis, 0);
325 if (r == _JV_NOT_OWNER)
326 return JVMTI_ERROR_NOT_MONITOR_OWNER;
327 if (r == _JV_INTERRUPTED)
328 return JVMTI_ERROR_INTERRUPT;
329 return JVMTI_ERROR_NONE;
332 static jvmtiError JNICALL
333 _Jv_JVMTI_RawMonitorNotify (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
335 if (monitor == NULL)
336 return JVMTI_ERROR_INVALID_MONITOR;
337 if (_Jv_CondNotify (&monitor->condition, &monitor->mutex) == _JV_NOT_OWNER)
338 return JVMTI_ERROR_NOT_MONITOR_OWNER;
339 return JVMTI_ERROR_NONE;
342 static jvmtiError JNICALL
343 _Jv_JVMTI_RawMonitorNotifyAll (MAYBE_UNUSED jvmtiEnv *env,
344 jrawMonitorID monitor)
346 if (monitor == NULL)
347 return JVMTI_ERROR_INVALID_MONITOR;
348 if (_Jv_CondNotifyAll (&monitor->condition, &monitor->mutex)
349 == _JV_NOT_OWNER)
350 return JVMTI_ERROR_NOT_MONITOR_OWNER;
351 return JVMTI_ERROR_NONE;
354 static jvmtiError JNICALL
355 _Jv_JVMTI_SetBreakpoint (jvmtiEnv *env, jmethodID method, jlocation location)
357 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
359 using namespace gnu::gcj::jvmti;
360 Breakpoint *bp
361 = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
362 location);
363 if (bp == NULL)
365 jclass klass;
366 jvmtiError err = env->GetMethodDeclaringClass (method, &klass);
367 if (err != JVMTI_ERROR_NONE)
368 return err;
370 if (!_Jv_IsInterpretedClass (klass))
371 return JVMTI_ERROR_INVALID_CLASS;
373 _Jv_MethodBase *base = _Jv_FindInterpreterMethod (klass, method);
374 if (base == NULL)
375 return JVMTI_ERROR_INVALID_METHODID;
377 jint flags;
378 err = env->GetMethodModifiers (method, &flags);
379 if (err != JVMTI_ERROR_NONE)
380 return err;
382 if (flags & java::lang::reflect::Modifier::NATIVE)
383 return JVMTI_ERROR_NATIVE_METHOD;
385 _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (base);
386 if (imeth->get_insn (location) == NULL)
387 return JVMTI_ERROR_INVALID_LOCATION;
389 // Now the breakpoint can be safely installed
390 bp = BreakpointManager::newBreakpoint (reinterpret_cast<jlong> (method),
391 location);
393 else
395 // Duplicate breakpoints are not permitted by JVMTI
396 return JVMTI_ERROR_DUPLICATE;
399 return JVMTI_ERROR_NONE;
402 static jvmtiError JNICALL
403 _Jv_JVMTI_ClearBreakpoint (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
404 jlocation location)
406 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
408 using namespace gnu::gcj::jvmti;
410 Breakpoint *bp
411 = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
412 location);
413 if (bp == NULL)
414 return JVMTI_ERROR_NOT_FOUND;
416 BreakpointManager::deleteBreakpoint (reinterpret_cast<jlong> (method), location);
417 return JVMTI_ERROR_NONE;
420 static jvmtiError JNICALL
421 _Jv_JVMTI_Allocate (MAYBE_UNUSED jvmtiEnv *env, jlong size,
422 unsigned char **result)
424 ILLEGAL_ARGUMENT (size < 0);
425 NULL_CHECK (result);
426 if (size == 0)
427 *result = NULL;
428 else
430 *result = (unsigned char *) _Jv_MallocUnchecked (size);
431 if (*result == NULL)
432 return JVMTI_ERROR_OUT_OF_MEMORY;
434 return JVMTI_ERROR_NONE;
437 static jvmtiError JNICALL
438 _Jv_JVMTI_Deallocate (MAYBE_UNUSED jvmtiEnv *env, unsigned char *mem)
440 if (mem != NULL)
441 _Jv_Free (mem);
442 return JVMTI_ERROR_NONE;
445 static jvmtiError JNICALL
446 _Jv_JVMTI_GetClassStatus (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
447 jint *status_ptr)
449 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
450 NULL_CHECK (status_ptr);
451 if (klass == NULL)
452 return JVMTI_ERROR_INVALID_CLASS;
454 if (klass->isArray ())
455 *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
456 else if (klass->isPrimitive ())
457 *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
458 else
460 jbyte state = _Jv_GetClassState (klass);
461 *status_ptr = 0;
462 if (state >= JV_STATE_LINKED)
463 (*status_ptr) |= JVMTI_CLASS_STATUS_VERIFIED;
464 if (state >= JV_STATE_PREPARED)
465 (*status_ptr) |= JVMTI_CLASS_STATUS_PREPARED;
466 if (state == JV_STATE_ERROR || state == JV_STATE_PHANTOM)
467 (*status_ptr) |= JVMTI_CLASS_STATUS_ERROR;
468 else if (state == JV_STATE_DONE)
469 (*status_ptr) |= JVMTI_CLASS_STATUS_INITIALIZED;
472 return JVMTI_ERROR_NONE;
475 static jvmtiError JNICALL
476 _Jv_JVMTI_GetClassModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
477 jint *mods)
479 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
480 // Don't bother checking KLASS' type.
481 if (klass == NULL)
482 return JVMTI_ERROR_INVALID_CLASS;
483 NULL_CHECK (mods);
484 *mods = klass->getModifiers();
485 return JVMTI_ERROR_NONE;
488 static jvmtiError JNICALL
489 _Jv_JVMTI_GetClassMethods (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
490 jint *count_ptr, jmethodID **methods_ptr)
492 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
493 // FIXME: capability can_maintain_original_method_order
494 // Don't bother checking KLASS' type.
495 if (klass == NULL)
496 return JVMTI_ERROR_INVALID_CLASS;
497 NULL_CHECK (count_ptr);
498 NULL_CHECK (methods_ptr);
499 *count_ptr = JvNumMethods(klass);
501 *methods_ptr
502 = (jmethodID *) _Jv_MallocUnchecked (*count_ptr * sizeof (jmethodID));
503 if (*methods_ptr == NULL)
504 return JVMTI_ERROR_OUT_OF_MEMORY;
506 jmethodID start = JvGetFirstMethod (klass);
507 for (jint i = 0; i < *count_ptr; ++i)
508 // FIXME: correct?
509 (*methods_ptr)[i] = start + i;
511 return JVMTI_ERROR_NONE;
514 static jvmtiError JNICALL
515 _Jv_JVMTI_IsInterface (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
516 jboolean *result)
518 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
519 if (klass == NULL)
520 return JVMTI_ERROR_INVALID_CLASS;
521 NULL_CHECK (result);
522 *result = klass->isInterface();
523 return JVMTI_ERROR_NONE;
526 static jvmtiError JNICALL
527 _Jv_JVMTI_IsArrayClass (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
528 jboolean *result)
530 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
531 if (klass == NULL)
532 return JVMTI_ERROR_INVALID_CLASS;
533 NULL_CHECK (result);
534 *result = klass->isArray();
535 return JVMTI_ERROR_NONE;
538 static jvmtiError JNICALL
539 _Jv_JVMTI_GetClassLoader (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
540 jobject *result)
542 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
543 if (klass == NULL)
544 return JVMTI_ERROR_INVALID_CLASS;
545 NULL_CHECK (result);
546 *result = klass->getClassLoaderInternal();
547 return JVMTI_ERROR_NONE;
550 static jvmtiError JNICALL
551 _Jv_JVMTI_GetObjectHashCode (MAYBE_UNUSED jvmtiEnv *env, jobject obj,
552 jint *result)
554 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
555 if (obj == NULL)
556 return JVMTI_ERROR_INVALID_OBJECT;
557 NULL_CHECK (result);
558 *result = _Jv_HashCode (obj);
559 return JVMTI_ERROR_NONE;
562 static jvmtiError JNICALL
563 _Jv_JVMTI_GetFieldModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
564 jfieldID field, jint *result)
566 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
567 if (klass == NULL)
568 return JVMTI_ERROR_INVALID_CLASS;
569 if (field == NULL)
570 return JVMTI_ERROR_INVALID_FIELDID;
571 NULL_CHECK (result);
572 *result = field->getModifiers();
573 return JVMTI_ERROR_NONE;
576 static jvmtiError JNICALL
577 _Jv_JVMTI_IsFieldSynthetic (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
578 jfieldID field, jboolean *result)
580 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
581 if (klass == NULL)
582 return JVMTI_ERROR_INVALID_CLASS;
583 if (field == NULL)
584 return JVMTI_ERROR_INVALID_FIELDID;
585 NULL_CHECK (result);
587 // FIXME: capability can_get_synthetic_attribute
588 *result = ((field->getModifiers() & java::lang::reflect::Modifier::SYNTHETIC)
589 != 0);
590 return JVMTI_ERROR_NONE;
593 static jvmtiError JNICALL
594 _Jv_JVMTI_GetMethodName (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
595 char **name_ptr, char **signature_ptr,
596 char **generic_ptr)
598 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
600 if (method == NULL)
601 return JVMTI_ERROR_INVALID_METHODID;
603 if (name_ptr != NULL)
605 int len = static_cast<int> (method->name->len ());
606 *name_ptr = (char *) _Jv_MallocUnchecked (len + 1);
607 if (*name_ptr == NULL)
608 return JVMTI_ERROR_OUT_OF_MEMORY;
609 strncpy (*name_ptr, method->name->chars (), len);
610 (*name_ptr)[len] = '\0';
613 if (signature_ptr != NULL)
615 int len = static_cast<int> (method->signature->len ());
616 *signature_ptr = (char *) _Jv_MallocUnchecked (len + 1);
617 if (*signature_ptr == NULL)
619 if (name_ptr != NULL)
620 _Jv_Free (*name_ptr);
621 return JVMTI_ERROR_OUT_OF_MEMORY;
623 strncpy (*signature_ptr, method->signature->chars (), len);
624 (*signature_ptr)[len] = '\0';
627 if (generic_ptr != NULL)
629 *generic_ptr = NULL;
632 return JVMTI_ERROR_NONE;
635 static jvmtiError JNICALL
636 _Jv_JVMTI_GetMethodModifiers (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
637 jint *result)
639 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
640 if (method == NULL)
641 return JVMTI_ERROR_INVALID_METHODID;
642 NULL_CHECK (result);
644 // FIXME: mask off some internal bits...
645 *result = method->accflags;
646 return JVMTI_ERROR_NONE;
649 static jvmtiError JNICALL
650 _Jv_JVMTI_GetLineNumberTable (jvmtiEnv *env, jmethodID method,
651 jint *entry_count_ptr,
652 jvmtiLineNumberEntry **table_ptr)
654 NULL_CHECK (entry_count_ptr);
655 NULL_CHECK (table_ptr);
657 jclass klass;
658 jvmtiError jerr = env->GetMethodDeclaringClass (method, &klass);
659 if (jerr != JVMTI_ERROR_NONE)
660 return jerr;
662 _Jv_MethodBase *base = _Jv_FindInterpreterMethod (klass, method);
663 if (base == NULL)
664 return JVMTI_ERROR_INVALID_METHODID;
666 if (java::lang::reflect::Modifier::isNative (method->accflags)
667 || !_Jv_IsInterpretedClass (klass))
668 return JVMTI_ERROR_NATIVE_METHOD;
670 _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (base);
671 jlong start, end;
672 jintArray lines = NULL;
673 jlongArray indices = NULL;
674 imeth->get_line_table (start, end, lines, indices);
675 if (lines == NULL)
676 return JVMTI_ERROR_ABSENT_INFORMATION;
678 jvmtiLineNumberEntry *table;
679 jsize len = lines->length * sizeof (jvmtiLineNumberEntry);
680 table = (jvmtiLineNumberEntry *) _Jv_MallocUnchecked (len);
681 if (table == NULL)
682 return JVMTI_ERROR_OUT_OF_MEMORY;
684 jint *line = elements (lines);
685 jlong *index = elements (indices);
686 for (int i = 0; i < lines->length; ++i)
688 table[i].start_location = index[i];
689 table[i].line_number = line[i];
692 *table_ptr = table;
693 *entry_count_ptr = lines->length;
694 return JVMTI_ERROR_NONE;
697 static jvmtiError JNICALL
698 _Jv_JVMTI_IsMethodNative (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
699 jboolean *result)
701 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
702 if (method == NULL)
703 return JVMTI_ERROR_INVALID_METHODID;
704 NULL_CHECK (result);
706 *result = ((method->accflags & java::lang::reflect::Modifier::NATIVE) != 0);
707 return JVMTI_ERROR_NONE;
710 static jvmtiError JNICALL
711 _Jv_JVMTI_IsMethodSynthetic (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
712 jboolean *result)
714 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
715 if (method == NULL)
716 return JVMTI_ERROR_INVALID_METHODID;
717 NULL_CHECK (result);
719 // FIXME capability can_get_synthetic_attribute
721 *result = ((method->accflags & java::lang::reflect::Modifier::SYNTHETIC)
722 != 0);
723 return JVMTI_ERROR_NONE;
726 static jvmtiError JNICALL
727 _Jv_JVMTI_GetMethodDeclaringClass (MAYBE_UNUSED jvmtiEnv *env,
728 jmethodID method,
729 jclass *declaring_class_ptr)
731 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
732 NULL_CHECK (declaring_class_ptr);
734 jclass klass = _Jv_GetMethodDeclaringClass (method);
735 if (klass != NULL)
737 *declaring_class_ptr = klass;
738 return JVMTI_ERROR_NONE;
741 return JVMTI_ERROR_INVALID_METHODID;
744 static jvmtiError JNICALL
745 _Jv_JVMTI_GetClassLoaderClasses (MAYBE_UNUSED jvmtiEnv *env,
746 jobject init_loader,
747 jint *count_ptr,
748 jclass **result_ptr)
750 using namespace java::lang;
751 using namespace java::util;
753 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
754 NULL_CHECK (count_ptr);
755 NULL_CHECK (result_ptr);
757 ClassLoader *loader = (ClassLoader *) init_loader;
758 if (loader == NULL)
759 loader = VMClassLoader::bootLoader;
761 Collection *values = loader->loadedClasses->values();
762 jobjectArray array = values->toArray();
763 *count_ptr = array->length;
764 jobject *elts = elements (array);
765 jclass *result
766 = (jclass *) _Jv_MallocUnchecked (*count_ptr * sizeof (jclass));
767 if (result == NULL)
768 return JVMTI_ERROR_OUT_OF_MEMORY;
770 // FIXME: JNI references...
771 memcpy (result, elts, *count_ptr * sizeof (jclass));
773 *result_ptr = result;
775 return JVMTI_ERROR_NONE;
778 static jvmtiError JNICALL
779 _Jv_JVMTI_GetStackTrace (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
780 jint start_depth, jint max_frames,
781 jvmtiFrameInfo *frames, jint *frame_count)
783 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
785 ILLEGAL_ARGUMENT (max_frames < 0);
787 NULL_CHECK (frames);
788 NULL_CHECK (frame_count);
790 using namespace java::lang;
792 THREAD_DEFAULT_TO_CURRENT (thread);
794 Thread *thr = reinterpret_cast<Thread *> (thread);
795 THREAD_CHECK_VALID (thr);
796 THREAD_CHECK_IS_ALIVE (thr);
798 jvmtiError jerr = env->GetFrameCount (thread, frame_count);
799 if (jerr != JVMTI_ERROR_NONE)
800 return jerr;
802 // start_depth can be either a positive number, indicating the depth of the
803 // stack at which to begin the trace, or a negative number indicating the
804 // number of frames at the bottom of the stack to exclude. These checks
805 // ensure that it is a valid value in either case
807 ILLEGAL_ARGUMENT (start_depth >= (*frame_count));
808 ILLEGAL_ARGUMENT (start_depth < (-(*frame_count)));
810 _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thr->frame);
812 // If start_depth is negative use this to determine at what depth to start
813 // the trace by adding it to the length of the call stack. This allows the
814 // use of the same frame "discarding" mechanism as for a positive start_depth
815 if (start_depth < 0)
816 start_depth = *frame_count + start_depth;
818 // If start_depth > 0 "remove" start_depth frames from the beginning
819 // of the stack before beginning the trace by moving along the frame list.
820 while (start_depth > 0)
822 frame = frame->next;
823 start_depth--;
824 (*frame_count)--;
827 // Now check to see if the array supplied by the agent is large enough to
828 // hold frame_count frames, after adjustment for start_depth.
829 if ((*frame_count) > max_frames)
830 (*frame_count) = max_frames;
832 for (int i = 0; i < (*frame_count); i++)
834 frames[i].method = frame->self->get_method ();
836 // Set the location in the frame, native frames have location = -1
837 if (frame->frame_type == frame_interpreter)
839 _Jv_InterpMethod *imeth
840 = static_cast<_Jv_InterpMethod *> (frame->self);
841 _Jv_InterpFrame *interp_frame
842 = static_cast<_Jv_InterpFrame *> (frame);
843 frames[i].location = imeth->insn_index (interp_frame->pc);
845 else
846 frames[i].location = -1;
848 frame = frame->next;
851 return JVMTI_ERROR_NONE;
854 static jvmtiError JNICALL
855 _Jv_JVMTI_ForceGarbageCollection (MAYBE_UNUSED jvmtiEnv *env)
857 REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
858 _Jv_RunGC();
859 return JVMTI_ERROR_NONE;
862 static jvmtiError JNICALL
863 _Jv_JVMTI_SetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
864 const jniNativeInterface *function_table)
866 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
867 NULL_CHECK (function_table);
868 memcpy (&_Jv_JNIFunctions, function_table, sizeof (jniNativeInterface));
869 return JVMTI_ERROR_NONE;
872 static jvmtiError JNICALL
873 _Jv_JVMTI_GetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
874 jniNativeInterface **function_table)
876 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
877 NULL_CHECK (function_table);
878 *function_table
879 = (jniNativeInterface *) _Jv_MallocUnchecked (sizeof (jniNativeInterface));
880 if (*function_table == NULL)
881 return JVMTI_ERROR_OUT_OF_MEMORY;
882 memcpy (*function_table, &_Jv_JNIFunctions, sizeof (jniNativeInterface));
883 return JVMTI_ERROR_NONE;
886 static jvmtiError JNICALL
887 _Jv_JVMTI_DisposeEnvironment (jvmtiEnv *env)
889 NULL_CHECK (env);
891 if (_jvmtiEnvironments == NULL)
892 return JVMTI_ERROR_INVALID_ENVIRONMENT;
893 else
895 JvSynchronize dummy (_envListLock);
896 if (_jvmtiEnvironments->env == env)
898 struct jvmti_env_list *next = _jvmtiEnvironments->next;
899 _Jv_Free (_jvmtiEnvironments);
900 _jvmtiEnvironments = next;
902 else
904 struct jvmti_env_list *e = _jvmtiEnvironments;
905 while (e->next != NULL && e->next->env != env)
906 e = e->next;
907 if (e->next == NULL)
908 return JVMTI_ERROR_INVALID_ENVIRONMENT;
910 struct jvmti_env_list *next = e->next->next;
911 _Jv_Free (e->next);
912 e->next = next;
916 _Jv_Free (env);
918 check_enabled_events ();
920 return JVMTI_ERROR_NONE;
923 static jvmtiError JNICALL
924 _Jv_JVMTI_GetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
925 char **result)
927 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
928 NULL_CHECK (property);
929 NULL_CHECK (result);
931 jstring name = JvNewStringUTF(property);
932 jstring result_str = gnu::classpath::SystemProperties::getProperty(name);
934 if (result_str == NULL)
935 return JVMTI_ERROR_NOT_AVAILABLE;
937 int len = JvGetStringUTFLength (result_str);
938 *result = (char *) _Jv_MallocUnchecked (len + 1);
939 if (*result == NULL)
940 return JVMTI_ERROR_OUT_OF_MEMORY;
941 JvGetStringUTFRegion (result_str, 0, result_str->length(), *result);
942 (*result)[len] = '\0';
944 return JVMTI_ERROR_NONE;
947 static jvmtiError JNICALL
948 _Jv_JVMTI_SetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
949 const char *value)
951 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD);
953 NULL_CHECK (property);
954 if (value == NULL)
956 // FIXME: When would a property not be writeable?
957 return JVMTI_ERROR_NONE;
960 jstring prop_str = JvNewStringUTF(property);
961 jstring value_str = JvNewStringUTF(value);
962 gnu::classpath::SystemProperties::setProperty(prop_str, value_str);
963 return JVMTI_ERROR_NONE;
966 static jvmtiError JNICALL
967 _Jv_JVMTI_GetTime (MAYBE_UNUSED jvmtiEnv *env, jlong *nanos_ptr)
969 NULL_CHECK (nanos_ptr);
970 *nanos_ptr = _Jv_platform_nanotime();
971 return JVMTI_ERROR_NONE;
974 static jvmtiError JNICALL
975 _Jv_JVMTI_GetAvailableProcessors (MAYBE_UNUSED jvmtiEnv *env,
976 jint *nprocessors_ptr)
978 NULL_CHECK (nprocessors_ptr);
979 #ifdef _SC_NPROCESSORS_ONLN
980 *nprocessors_ptr = sysconf(_SC_NPROCESSORS_ONLN);
981 #else
982 *nprocessors_ptr = 1;
983 #endif
984 return JVMTI_ERROR_NONE;
987 static jvmtiError JNICALL
988 _Jv_JVMTI_AddToBootstrapClassLoaderSearch (MAYBE_UNUSED jvmtiEnv *env,
989 const char *segment)
991 using namespace java::lang;
992 using namespace java::net;
993 using namespace gnu::gcj::runtime;
995 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD);
996 NULL_CHECK (segment);
998 jstring str_segment = JvNewStringUTF(segment);
999 URL *url;
1002 url = new URL(JvNewStringUTF("file"), NULL, str_segment);
1004 catch (jthrowable ignore)
1006 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1009 BootClassLoader *loader = VMClassLoader::bootLoader;
1010 // Don't call this too early.
1011 // assert (loader != NULL);
1012 loader->addURL(url);
1013 return JVMTI_ERROR_NONE;
1016 static jvmtiError JNICALL
1017 _Jv_JVMTI_SetVerboseFlag (MAYBE_UNUSED jvmtiEnv *env, jvmtiVerboseFlag flag,
1018 jboolean value)
1020 switch (flag)
1022 case JVMTI_VERBOSE_OTHER:
1023 case JVMTI_VERBOSE_GC:
1024 case JVMTI_VERBOSE_JNI:
1025 // Ignore.
1026 break;
1027 case JVMTI_VERBOSE_CLASS:
1028 gcj::verbose_class_flag = value;
1029 break;
1030 default:
1031 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1033 return JVMTI_ERROR_NONE;
1036 static jvmtiError JNICALL
1037 _Jv_JVMTI_GetObjectSize (MAYBE_UNUSED jvmtiEnv *env, jobject object,
1038 jlong *result)
1040 REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
1041 if (object == NULL)
1042 return JVMTI_ERROR_INVALID_OBJECT;
1043 NULL_CHECK (result);
1045 jclass klass = object->getClass();
1046 if (klass->isArray())
1048 jclass comp = klass->getComponentType();
1049 jint base
1050 = (jint) (_Jv_uintptr_t) _Jv_GetArrayElementFromElementType(NULL,
1051 klass->getComponentType());
1052 // FIXME: correct for primitive types?
1053 jint compSize = comp->size();
1054 __JArray *array = (__JArray *) object;
1055 *result = base + array->length * compSize;
1057 else
1059 // Note that if OBJECT is a String then it may (if
1060 // str->data==str) take more space. Do we care?
1061 *result = klass->size();
1063 return JVMTI_ERROR_NONE;
1066 /* An event is enabled only if it has both an event handler
1067 and it is enabled in the environment. */
1068 static void
1069 check_enabled_event (jvmtiEvent type)
1071 bool *enabled;
1072 int offset;
1074 #define GET_OFFSET(Event) \
1075 do \
1077 enabled = &JVMTI::Event; \
1078 offset = offsetof (jvmtiEventCallbacks, Event); \
1080 while (0)
1082 switch (type)
1084 case JVMTI_EVENT_VM_INIT:
1085 GET_OFFSET (VMInit);
1086 break;
1088 case JVMTI_EVENT_VM_DEATH:
1089 GET_OFFSET (VMDeath);
1090 break;
1092 case JVMTI_EVENT_THREAD_START:
1093 GET_OFFSET (ThreadStart);
1094 break;
1096 case JVMTI_EVENT_THREAD_END:
1097 GET_OFFSET (ThreadEnd);
1098 break;
1100 case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
1101 GET_OFFSET (ClassFileLoadHook);
1102 break;
1104 case JVMTI_EVENT_CLASS_LOAD:
1105 GET_OFFSET (ClassLoad);
1106 break;
1108 case JVMTI_EVENT_CLASS_PREPARE:
1109 GET_OFFSET (ClassPrepare);
1110 break;
1112 case JVMTI_EVENT_VM_START:
1113 GET_OFFSET (VMStart);
1114 break;
1116 case JVMTI_EVENT_EXCEPTION:
1117 GET_OFFSET (Exception);
1118 break;
1120 case JVMTI_EVENT_EXCEPTION_CATCH:
1121 GET_OFFSET (ExceptionCatch);
1122 break;
1124 case JVMTI_EVENT_SINGLE_STEP:
1125 GET_OFFSET (SingleStep);
1126 break;
1128 case JVMTI_EVENT_FRAME_POP:
1129 GET_OFFSET (FramePop);
1130 break;
1132 case JVMTI_EVENT_BREAKPOINT:
1133 GET_OFFSET (Breakpoint);
1134 break;
1136 case JVMTI_EVENT_FIELD_ACCESS:
1137 GET_OFFSET (FieldAccess);
1138 break;
1140 case JVMTI_EVENT_FIELD_MODIFICATION:
1141 GET_OFFSET (FieldModification);
1142 break;
1144 case JVMTI_EVENT_METHOD_ENTRY:
1145 GET_OFFSET (MethodEntry);
1146 break;
1148 case JVMTI_EVENT_METHOD_EXIT:
1149 GET_OFFSET (MethodExit);
1150 break;
1152 case JVMTI_EVENT_NATIVE_METHOD_BIND:
1153 GET_OFFSET (NativeMethodBind);
1154 break;
1156 case JVMTI_EVENT_COMPILED_METHOD_LOAD:
1157 GET_OFFSET (CompiledMethodLoad);
1158 break;
1160 case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
1161 GET_OFFSET (CompiledMethodUnload);
1162 break;
1164 case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
1165 GET_OFFSET (DynamicCodeGenerated);
1166 break;
1168 case JVMTI_EVENT_DATA_DUMP_REQUEST:
1169 GET_OFFSET (DataDumpRequest);
1170 break;
1172 case JVMTI_EVENT_MONITOR_WAIT:
1173 GET_OFFSET (MonitorWait);
1174 break;
1176 case JVMTI_EVENT_MONITOR_WAITED:
1177 GET_OFFSET (MonitorWaited);
1178 break;
1180 case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
1181 GET_OFFSET (MonitorContendedEnter);
1182 break;
1184 case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
1185 GET_OFFSET (MonitorContendedEntered);
1186 break;
1188 case JVMTI_EVENT_GARBAGE_COLLECTION_START:
1189 GET_OFFSET (GarbageCollectionStart);
1190 break;
1192 case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
1193 GET_OFFSET (GarbageCollectionFinish);
1194 break;
1196 case JVMTI_EVENT_OBJECT_FREE:
1197 GET_OFFSET (ObjectFree);
1198 break;
1200 case JVMTI_EVENT_VM_OBJECT_ALLOC:
1201 GET_OFFSET (VMObjectAlloc);
1202 break;
1204 default:
1205 fprintf (stderr,
1206 "libgcj: check_enabled_event for unknown JVMTI event (%d)\n",
1207 (int) type);
1208 return;
1210 #undef GET_OFFSET
1212 int index = EVENT_INDEX (type); // safe since caller checks this
1214 JvSynchronize dummy (_envListLock);
1215 struct jvmti_env_list *e;
1216 FOREACH_ENVIRONMENT (e)
1218 char *addr
1219 = reinterpret_cast<char *> (&e->env->callbacks) + offset;
1220 void **callback = reinterpret_cast<void **> (addr);
1221 if (e->env->enabled[index] && *callback != NULL)
1223 *enabled = true;
1224 return;
1228 *enabled = false;
1231 static void
1232 check_enabled_events ()
1234 check_enabled_event (JVMTI_EVENT_VM_INIT);
1235 check_enabled_event (JVMTI_EVENT_VM_DEATH);
1236 check_enabled_event (JVMTI_EVENT_THREAD_START);
1237 check_enabled_event (JVMTI_EVENT_THREAD_END);
1238 check_enabled_event (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK);
1239 check_enabled_event (JVMTI_EVENT_CLASS_LOAD);
1240 check_enabled_event (JVMTI_EVENT_CLASS_PREPARE);
1241 check_enabled_event (JVMTI_EVENT_VM_START);
1242 check_enabled_event (JVMTI_EVENT_EXCEPTION);
1243 check_enabled_event (JVMTI_EVENT_EXCEPTION_CATCH);
1244 check_enabled_event (JVMTI_EVENT_SINGLE_STEP);
1245 check_enabled_event (JVMTI_EVENT_FRAME_POP);
1246 check_enabled_event (JVMTI_EVENT_BREAKPOINT);
1247 check_enabled_event (JVMTI_EVENT_FIELD_ACCESS);
1248 check_enabled_event (JVMTI_EVENT_FIELD_MODIFICATION);
1249 check_enabled_event (JVMTI_EVENT_METHOD_ENTRY);
1250 check_enabled_event (JVMTI_EVENT_METHOD_EXIT);
1251 check_enabled_event (JVMTI_EVENT_NATIVE_METHOD_BIND);
1252 check_enabled_event (JVMTI_EVENT_COMPILED_METHOD_LOAD);
1253 check_enabled_event (JVMTI_EVENT_COMPILED_METHOD_UNLOAD);
1254 check_enabled_event (JVMTI_EVENT_DYNAMIC_CODE_GENERATED);
1255 check_enabled_event (JVMTI_EVENT_DATA_DUMP_REQUEST);
1256 check_enabled_event (JVMTI_EVENT_MONITOR_WAIT);
1257 check_enabled_event (JVMTI_EVENT_MONITOR_WAITED);
1258 check_enabled_event (JVMTI_EVENT_MONITOR_CONTENDED_ENTER);
1259 check_enabled_event (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED);
1260 check_enabled_event (JVMTI_EVENT_GARBAGE_COLLECTION_START);
1261 check_enabled_event (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH);
1262 check_enabled_event (JVMTI_EVENT_OBJECT_FREE);
1263 check_enabled_event (JVMTI_EVENT_VM_OBJECT_ALLOC);
1266 static jvmtiError JNICALL
1267 _Jv_JVMTI_SetEventNotificationMode (jvmtiEnv *env, jvmtiEventMode mode,
1268 jvmtiEvent type, jthread event_thread, ...)
1270 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
1272 if (event_thread != NULL)
1274 THREAD_CHECK_VALID (event_thread);
1275 THREAD_CHECK_IS_ALIVE (event_thread);
1278 bool enabled;
1279 switch (mode)
1281 case JVMTI_DISABLE:
1282 enabled = false;
1283 break;
1284 case JVMTI_ENABLE:
1285 enabled = true;
1286 break;
1288 default:
1289 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1292 switch (type)
1294 case JVMTI_EVENT_VM_INIT:
1295 case JVMTI_EVENT_VM_DEATH:
1296 case JVMTI_EVENT_THREAD_START:
1297 case JVMTI_EVENT_VM_START:
1298 case JVMTI_EVENT_COMPILED_METHOD_LOAD:
1299 case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
1300 case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
1301 case JVMTI_EVENT_DATA_DUMP_REQUEST:
1302 ILLEGAL_ARGUMENT (event_thread != NULL);
1303 break;
1305 case JVMTI_EVENT_THREAD_END:
1306 case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
1307 case JVMTI_EVENT_CLASS_LOAD:
1308 case JVMTI_EVENT_CLASS_PREPARE:
1309 case JVMTI_EVENT_EXCEPTION:
1310 case JVMTI_EVENT_EXCEPTION_CATCH:
1311 case JVMTI_EVENT_SINGLE_STEP:
1312 case JVMTI_EVENT_FRAME_POP:
1313 case JVMTI_EVENT_BREAKPOINT:
1314 case JVMTI_EVENT_FIELD_ACCESS:
1315 case JVMTI_EVENT_FIELD_MODIFICATION:
1316 case JVMTI_EVENT_METHOD_ENTRY:
1317 case JVMTI_EVENT_METHOD_EXIT:
1318 case JVMTI_EVENT_NATIVE_METHOD_BIND:
1319 case JVMTI_EVENT_MONITOR_WAIT:
1320 case JVMTI_EVENT_MONITOR_WAITED:
1321 case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
1322 case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
1323 case JVMTI_EVENT_GARBAGE_COLLECTION_START:
1324 case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
1325 case JVMTI_EVENT_OBJECT_FREE:
1326 case JVMTI_EVENT_VM_OBJECT_ALLOC:
1327 break;
1329 default:
1330 return JVMTI_ERROR_INVALID_EVENT_TYPE;
1333 env->thread[EVENT_INDEX(type)] = event_thread;
1334 env->enabled[EVENT_INDEX(type)] = enabled;
1335 check_enabled_event (type);
1336 return JVMTI_ERROR_NONE;
1339 static jvmtiError JNICALL
1340 _Jv_JVMTI_SetEventCallbacks (jvmtiEnv *env,
1341 const jvmtiEventCallbacks *callbacks,
1342 jint size_of_callbacks)
1344 REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
1345 ILLEGAL_ARGUMENT (size_of_callbacks < 0);
1347 // Copy the list of callbacks into the environment
1348 memcpy (&env->callbacks, callbacks, sizeof (jvmtiEventCallbacks));
1350 /* Check which events are now enabeld (JVMTI makes no requirements
1351 about the order in which SetEventCallbacks and SetEventNotifications
1352 are called. So we must check all events here. */
1353 check_enabled_events ();
1355 return JVMTI_ERROR_NONE;
1358 static jvmtiError JNICALL
1359 _Jv_JVMTI_GetErrorName (MAYBE_UNUSED jvmtiEnv *env, jvmtiError error,
1360 char **name_ptr)
1362 NULL_CHECK (name_ptr);
1364 const char *name;
1365 switch (error)
1367 case JVMTI_ERROR_NONE:
1368 name = "none";
1369 break;
1371 case JVMTI_ERROR_NULL_POINTER:
1372 name = "null pointer";
1373 break;
1375 case JVMTI_ERROR_OUT_OF_MEMORY:
1376 name = "out of memory";
1377 break;
1379 case JVMTI_ERROR_ACCESS_DENIED:
1380 name = "access denied";
1381 break;
1383 case JVMTI_ERROR_WRONG_PHASE:
1384 name = "wrong phase";
1385 break;
1387 case JVMTI_ERROR_INTERNAL:
1388 name = "internal error";
1389 break;
1391 case JVMTI_ERROR_UNATTACHED_THREAD:
1392 name = "unattached thread";
1393 break;
1395 case JVMTI_ERROR_INVALID_ENVIRONMENT:
1396 name = "invalid environment";
1397 break;
1399 case JVMTI_ERROR_INVALID_PRIORITY:
1400 name = "invalid priority";
1401 break;
1403 case JVMTI_ERROR_THREAD_NOT_SUSPENDED:
1404 name = "thread not suspended";
1405 break;
1407 case JVMTI_ERROR_THREAD_SUSPENDED:
1408 name = "thread suspended";
1409 break;
1411 case JVMTI_ERROR_THREAD_NOT_ALIVE:
1412 name = "thread not alive";
1413 break;
1415 case JVMTI_ERROR_CLASS_NOT_PREPARED:
1416 name = "class not prepared";
1417 break;
1419 case JVMTI_ERROR_NO_MORE_FRAMES:
1420 name = "no more frames";
1421 break;
1423 case JVMTI_ERROR_OPAQUE_FRAME:
1424 name = "opaque frame";
1425 break;
1427 case JVMTI_ERROR_DUPLICATE:
1428 name = "duplicate";
1429 break;
1431 case JVMTI_ERROR_NOT_FOUND:
1432 name = "not found";
1433 break;
1435 case JVMTI_ERROR_NOT_MONITOR_OWNER:
1436 name = "not monitor owner";
1437 break;
1439 case JVMTI_ERROR_INTERRUPT:
1440 name = "interrupted";
1441 break;
1443 case JVMTI_ERROR_UNMODIFIABLE_CLASS:
1444 name = "unmodifiable class";
1445 break;
1447 case JVMTI_ERROR_NOT_AVAILABLE:
1448 name = "not available";
1449 break;
1451 case JVMTI_ERROR_ABSENT_INFORMATION:
1452 name = "absent information";
1453 break;
1455 case JVMTI_ERROR_INVALID_EVENT_TYPE:
1456 name = "invalid event type";
1457 break;
1459 case JVMTI_ERROR_NATIVE_METHOD:
1460 name = "native method";
1461 break;
1463 case JVMTI_ERROR_INVALID_THREAD:
1464 name = "invalid thread";
1465 break;
1467 case JVMTI_ERROR_INVALID_THREAD_GROUP:
1468 name = "invalid thread group";
1469 break;
1471 case JVMTI_ERROR_INVALID_OBJECT:
1472 name = "invalid object";
1473 break;
1475 case JVMTI_ERROR_INVALID_CLASS:
1476 name = "invalid class";
1477 break;
1479 case JVMTI_ERROR_INVALID_METHODID:
1480 name = "invalid method ID";
1481 break;
1483 case JVMTI_ERROR_INVALID_LOCATION:
1484 name = "invalid location";
1485 break;
1487 case JVMTI_ERROR_INVALID_FIELDID:
1488 name = "invalid field ID";
1489 break;
1491 case JVMTI_ERROR_TYPE_MISMATCH:
1492 name = "type mismatch";
1493 break;
1495 case JVMTI_ERROR_INVALID_SLOT:
1496 name = "invalid slot";
1497 break;
1499 case JVMTI_ERROR_INVALID_MONITOR:
1500 name = "invalid monitor";
1501 break;
1503 case JVMTI_ERROR_INVALID_CLASS_FORMAT:
1504 name = "invalid class format";
1505 break;
1507 case JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION:
1508 name = "circular class definition";
1509 break;
1511 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED:
1512 name = "unsupported redefinition: method added";
1513 break;
1515 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED:
1516 name = "unsupported redefinition: schema changed";
1517 break;
1519 case JVMTI_ERROR_INVALID_TYPESTATE:
1520 name = "invalid type state";
1521 break;
1523 case JVMTI_ERROR_FAILS_VERIFICATION:
1524 name = "fails verification";
1525 break;
1527 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED:
1528 name = "unsupported redefinition: hierarchy changed";
1529 break;
1531 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED:
1532 name = "unsupported redefinition: method deleted";
1533 break;
1535 case JVMTI_ERROR_UNSUPPORTED_VERSION:
1536 name = "unsupported version";
1537 break;
1539 case JVMTI_ERROR_NAMES_DONT_MATCH:
1540 name = "names do not match";
1541 break;
1543 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED:
1544 name = "unsupported redefinition: class modifiers changed";
1545 break;
1547 case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED:
1548 name = "unsupported redefinition: method modifiers changed";
1549 break;
1551 case JVMTI_ERROR_MUST_POSSESS_CAPABILITY:
1552 name = "must possess capability";
1553 break;
1555 case JVMTI_ERROR_ILLEGAL_ARGUMENT:
1556 name = "illegal argument";
1557 break;
1559 default:
1560 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1563 *name_ptr = (char *) _Jv_MallocUnchecked (strlen (name) + 1);
1564 if (*name_ptr == NULL)
1565 return JVMTI_ERROR_OUT_OF_MEMORY;
1567 strcpy (*name_ptr, name);
1568 return JVMTI_ERROR_NONE;
1571 #define RESERVED NULL
1572 #define UNIMPLEMENTED NULL
1574 struct _Jv_jvmtiEnv _Jv_JVMTI_Interface =
1576 RESERVED, // reserved1
1577 _Jv_JVMTI_SetEventNotificationMode, // SetEventNotificationMode
1578 RESERVED, // reserved3
1579 _Jv_JVMTI_GetAllThreads, // GetAllThreads
1580 _Jv_JVMTI_SuspendThread, // SuspendThread
1581 _Jv_JVMTI_ResumeThread, // ResumeThread
1582 UNIMPLEMENTED, // StopThread
1583 _Jv_JVMTI_InterruptThread, // InterruptThread
1584 UNIMPLEMENTED, // GetThreadInfo
1585 UNIMPLEMENTED, // GetOwnedMonitorInfo
1586 UNIMPLEMENTED, // GetCurrentContendedMonitor
1587 UNIMPLEMENTED, // RunAgentThread
1588 UNIMPLEMENTED, // GetTopThreadGroups
1589 UNIMPLEMENTED, // GetThreadGroupInfo
1590 UNIMPLEMENTED, // GetThreadGroupChildren
1591 _Jv_JVMTI_GetFrameCount, // GetFrameCount
1592 UNIMPLEMENTED, // GetThreadState
1593 RESERVED, // reserved18
1594 UNIMPLEMENTED, // GetFrameLocation
1595 UNIMPLEMENTED, // NotifyPopFrame
1596 UNIMPLEMENTED, // GetLocalObject
1597 UNIMPLEMENTED, // GetLocalInt
1598 UNIMPLEMENTED, // GetLocalLong
1599 UNIMPLEMENTED, // GetLocalFloat
1600 UNIMPLEMENTED, // GetLocalDouble
1601 UNIMPLEMENTED, // SetLocalObject
1602 UNIMPLEMENTED, // SetLocalInt
1603 UNIMPLEMENTED, // SetLocalLong
1604 UNIMPLEMENTED, // SetLocalFloat
1605 UNIMPLEMENTED, // SetLocalDouble
1606 _Jv_JVMTI_CreateRawMonitor, // CreateRawMonitor
1607 _Jv_JVMTI_DestroyRawMonitor, // DestroyRawMonitor
1608 _Jv_JVMTI_RawMonitorEnter, // RawMonitorEnter
1609 _Jv_JVMTI_RawMonitorExit, // RawMonitorExit
1610 _Jv_JVMTI_RawMonitorWait, // RawMonitorWait
1611 _Jv_JVMTI_RawMonitorNotify, // RawMonitorNotify
1612 _Jv_JVMTI_RawMonitorNotifyAll, // RawMonitorNotifyAll
1613 _Jv_JVMTI_SetBreakpoint, // SetBreakpoint
1614 _Jv_JVMTI_ClearBreakpoint, // ClearBreakpoint
1615 RESERVED, // reserved40
1616 UNIMPLEMENTED, // SetFieldAccessWatch
1617 UNIMPLEMENTED, // ClearFieldAccessWatch
1618 UNIMPLEMENTED, // SetFieldModificationWatch
1619 UNIMPLEMENTED, // ClearFieldModificationWatch
1620 RESERVED, // reserved45
1621 _Jv_JVMTI_Allocate, // Allocate
1622 _Jv_JVMTI_Deallocate, // Deallocate
1623 UNIMPLEMENTED, // GetClassSignature
1624 _Jv_JVMTI_GetClassStatus, // GetClassStatus
1625 UNIMPLEMENTED, // GetSourceFileName
1626 _Jv_JVMTI_GetClassModifiers, // GetClassModifiers
1627 _Jv_JVMTI_GetClassMethods, // GetClassMethods
1628 UNIMPLEMENTED, // GetClassFields
1629 UNIMPLEMENTED, // GetImplementedInterfaces
1630 _Jv_JVMTI_IsInterface, // IsInterface
1631 _Jv_JVMTI_IsArrayClass, // IsArrayClass
1632 _Jv_JVMTI_GetClassLoader, // GetClassLoader
1633 _Jv_JVMTI_GetObjectHashCode, // GetObjectHashCode
1634 UNIMPLEMENTED, // GetObjectMonitorUsage
1635 UNIMPLEMENTED, // GetFieldName
1636 UNIMPLEMENTED, // GetFieldDeclaringClass
1637 _Jv_JVMTI_GetFieldModifiers, // GetFieldModifiers
1638 _Jv_JVMTI_IsFieldSynthetic, // IsFieldSynthetic
1639 _Jv_JVMTI_GetMethodName, // GetMethodName
1640 _Jv_JVMTI_GetMethodDeclaringClass, // GetMethodDeclaringClass
1641 _Jv_JVMTI_GetMethodModifiers, // GetMethodModifers
1642 RESERVED, // reserved67
1643 UNIMPLEMENTED, // GetMaxLocals
1644 UNIMPLEMENTED, // GetArgumentsSize
1645 _Jv_JVMTI_GetLineNumberTable, // GetLineNumberTable
1646 UNIMPLEMENTED, // GetMethodLocation
1647 UNIMPLEMENTED, // GetLocalVariableTable
1648 RESERVED, // reserved73
1649 RESERVED, // reserved74
1650 UNIMPLEMENTED, // GetBytecodes
1651 _Jv_JVMTI_IsMethodNative, // IsMethodNative
1652 _Jv_JVMTI_IsMethodSynthetic, // IsMethodSynthetic
1653 UNIMPLEMENTED, // GetLoadedClasses
1654 _Jv_JVMTI_GetClassLoaderClasses, // GetClassLoaderClasses
1655 UNIMPLEMENTED, // PopFrame
1656 RESERVED, // reserved81
1657 RESERVED, // reserved82
1658 RESERVED, // reserved83
1659 RESERVED, // reserved84
1660 RESERVED, // reserved85
1661 RESERVED, // reserved86
1662 UNIMPLEMENTED, // RedefineClasses
1663 UNIMPLEMENTED, // GetVersionNumber
1664 UNIMPLEMENTED, // GetCapabilities
1665 UNIMPLEMENTED, // GetSourceDebugExtension
1666 UNIMPLEMENTED, // IsMethodObsolete
1667 UNIMPLEMENTED, // SuspendThreadList
1668 UNIMPLEMENTED, // ResumeThreadList
1669 RESERVED, // reserved94
1670 RESERVED, // reserved95
1671 RESERVED, // reserved96
1672 RESERVED, // reserved97
1673 RESERVED, // reserved98
1674 RESERVED, // reserved99
1675 UNIMPLEMENTED, // GetAllStackTraces
1676 UNIMPLEMENTED, // GetThreadListStackTraces
1677 UNIMPLEMENTED, // GetThreadLocalStorage
1678 UNIMPLEMENTED, // SetThreadLocalStorage
1679 _Jv_JVMTI_GetStackTrace, // GetStackTrace
1680 RESERVED, // reserved105
1681 UNIMPLEMENTED, // GetTag
1682 UNIMPLEMENTED, // SetTag
1683 _Jv_JVMTI_ForceGarbageCollection, // ForceGarbageCollection
1684 UNIMPLEMENTED, // IterateOverObjectsReachable
1685 UNIMPLEMENTED, // IterateOverReachableObjects
1686 UNIMPLEMENTED, // IterateOverHeap
1687 UNIMPLEMENTED, // IterateOverInstanceOfClass
1688 RESERVED, // reserved113
1689 UNIMPLEMENTED, // GetObjectsWithTags
1690 RESERVED, // reserved115
1691 RESERVED, // reserved116
1692 RESERVED, // reserved117
1693 RESERVED, // reserved118
1694 RESERVED, // reserved119
1695 _Jv_JVMTI_SetJNIFunctionTable, // SetJNIFunctionTable
1696 _Jv_JVMTI_GetJNIFunctionTable, // GetJNIFunctionTable
1697 _Jv_JVMTI_SetEventCallbacks, // SetEventCallbacks
1698 UNIMPLEMENTED, // GenerateEvents
1699 UNIMPLEMENTED, // GetExtensionFunctions
1700 UNIMPLEMENTED, // GetExtensionEvents
1701 UNIMPLEMENTED, // SetExtensionEventCallback
1702 _Jv_JVMTI_DisposeEnvironment, // DisposeEnvironment
1703 _Jv_JVMTI_GetErrorName, // GetErrorName
1704 UNIMPLEMENTED, // GetJLocationFormat
1705 UNIMPLEMENTED, // GetSystemProperties
1706 _Jv_JVMTI_GetSystemProperty, // GetSystemProperty
1707 _Jv_JVMTI_SetSystemProperty, // SetSystemProperty
1708 UNIMPLEMENTED, // GetPhase
1709 UNIMPLEMENTED, // GetCurrentThreadCpuTimerInfo
1710 UNIMPLEMENTED, // GetCurrentThreadCpuTime
1711 UNIMPLEMENTED, // GetThreadCpuTimerInfo
1712 UNIMPLEMENTED, // GetThreadCpuTime
1713 UNIMPLEMENTED, // GetTimerInfo
1714 _Jv_JVMTI_GetTime, // GetTime
1715 UNIMPLEMENTED, // GetPotentialCapabilities
1716 RESERVED, // reserved141
1717 UNIMPLEMENTED, // AddCapabilities
1718 UNIMPLEMENTED, // RelinquishCapabilities
1719 _Jv_JVMTI_GetAvailableProcessors, // GetAvailableProcessors
1720 RESERVED, // reserved145
1721 RESERVED, // reserved146
1722 UNIMPLEMENTED, // GetEnvironmentLocalStorage
1723 UNIMPLEMENTED, // SetEnvironmentLocalStorage
1724 _Jv_JVMTI_AddToBootstrapClassLoaderSearch, // AddToBootstrapClassLoaderSearch
1725 _Jv_JVMTI_SetVerboseFlag, // SetVerboseFlag
1726 RESERVED, // reserved151
1727 RESERVED, // reserved152
1728 RESERVED, // reserved153
1729 _Jv_JVMTI_GetObjectSize // GetObjectSize
1732 _Jv_JVMTIEnv *
1733 _Jv_GetJVMTIEnv (void)
1735 _Jv_JVMTIEnv *env
1736 = (_Jv_JVMTIEnv *) _Jv_MallocUnchecked (sizeof (_Jv_JVMTIEnv));
1737 env->p = &_Jv_JVMTI_Interface;
1740 JvSynchronize dummy (_envListLock);
1741 struct jvmti_env_list *element
1742 = (struct jvmti_env_list *) _Jv_MallocUnchecked (sizeof (struct jvmti_env_list));
1743 element->env = env;
1744 element->next = NULL;
1746 if (_jvmtiEnvironments == NULL)
1747 _jvmtiEnvironments = element;
1748 else
1750 struct jvmti_env_list *e;
1751 for (e = _jvmtiEnvironments; e->next != NULL; e = e->next)
1753 e->next = element;
1757 return env;
1760 void
1761 _Jv_JVMTI_Init ()
1763 _jvmtiEnvironments = NULL;
1764 _envListLock = new java::lang::Object ();
1766 // No environments, so this should set all JVMTI:: members to false
1767 check_enabled_events ();
1770 static void
1771 post_event (jvmtiEnv *env, jvmtiEvent type, jthread event_thread, va_list args)
1773 #define ARG(Type,Name) Type Name = (Type) va_arg (args, Type)
1775 #define GET_BOOLEAN_ARG(Name) \
1776 ARG (int, b); \
1777 jboolean Name = (b == 0) ? false : true
1779 #define GET_CHAR_ARG(Name) \
1780 ARG (int, c); \
1781 char Name = static_cast<char> (c)
1783 switch (type)
1785 case JVMTI_EVENT_VM_INIT:
1786 if (env->callbacks.VMInit != NULL)
1788 ARG (JNIEnv *, jni_env);
1789 env->callbacks.VMInit (env, jni_env, event_thread);
1791 break;
1793 case JVMTI_EVENT_VM_DEATH:
1794 if (env->callbacks.VMDeath != NULL)
1796 ARG (JNIEnv *, jni_env);
1797 env->callbacks.VMDeath (env, jni_env);
1799 break;
1801 case JVMTI_EVENT_THREAD_START:
1802 if (env->callbacks.ThreadStart != NULL)
1804 ARG (JNIEnv *, jni_env);
1805 env->callbacks.ThreadStart (env, jni_env, event_thread);
1807 break;
1809 case JVMTI_EVENT_THREAD_END:
1810 if (env->callbacks.ThreadEnd != NULL)
1812 ARG (JNIEnv *, jni_env);
1813 env->callbacks.ThreadEnd (env, jni_env, event_thread);
1815 break;
1817 case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
1818 if (env->callbacks.ClassFileLoadHook != NULL)
1820 ARG (JNIEnv *, jni_env);
1821 ARG (jclass, class_being_redefined);
1822 ARG (jobject, loader);
1823 ARG (const char *, name);
1824 ARG (jobject, protection_domain);
1825 ARG (jint, class_data_len);
1826 ARG (const unsigned char *, class_data);
1827 ARG (jint *, new_class_data_len);
1828 ARG (unsigned char **, new_class_data);
1829 env->callbacks.ClassFileLoadHook (env, jni_env,
1830 class_being_redefined, loader,
1831 name, protection_domain,
1832 class_data_len, class_data,
1833 new_class_data_len,
1834 new_class_data);
1836 break;
1838 case JVMTI_EVENT_CLASS_LOAD:
1839 if (env->callbacks.ClassLoad != NULL)
1841 ARG (JNIEnv *, jni_env);
1842 ARG (jclass, klass);
1843 env->callbacks.ClassLoad (env, jni_env, event_thread, klass);
1845 break;
1847 case JVMTI_EVENT_CLASS_PREPARE:
1848 if (env->callbacks.ClassPrepare != NULL)
1850 ARG (JNIEnv *, jni_env);
1851 ARG (jclass, klass);
1852 env->callbacks.ClassPrepare (env, jni_env, event_thread, klass);
1854 break;
1856 case JVMTI_EVENT_VM_START:
1857 if (env->callbacks.VMStart != NULL)
1859 ARG (JNIEnv *, jni_env);
1860 env->callbacks.VMStart (env, jni_env);
1862 break;
1864 case JVMTI_EVENT_EXCEPTION:
1865 if (env->callbacks.Exception != NULL)
1867 ARG (JNIEnv *, jni_env);
1868 ARG (jmethodID, method);
1869 ARG (jlocation, location);
1870 ARG (jobject, exception);
1871 ARG (jmethodID, catch_method);
1872 ARG (jlocation, catch_location);
1873 env->callbacks.Exception (env, jni_env, event_thread, method,
1874 location, exception, catch_method,
1875 catch_location);
1877 break;
1879 case JVMTI_EVENT_EXCEPTION_CATCH:
1880 if (env->callbacks.ExceptionCatch != NULL)
1882 ARG (JNIEnv *, jni_env);
1883 ARG (jmethodID, method);
1884 ARG (jlocation, location);
1885 ARG (jobject, exception);
1886 env->callbacks.ExceptionCatch (env, jni_env, event_thread, method,
1887 location, exception);
1889 break;
1891 case JVMTI_EVENT_SINGLE_STEP:
1892 if (env->callbacks.SingleStep != NULL)
1894 ARG (JNIEnv *, jni_env);
1895 ARG (jmethodID, method);
1896 ARG (jlocation, location);
1897 env->callbacks.SingleStep (env, jni_env, event_thread, method,
1898 location);
1900 break;
1902 case JVMTI_EVENT_FRAME_POP:
1903 if (env->callbacks.FramePop != NULL)
1905 ARG (JNIEnv *, jni_env);
1906 ARG (jmethodID, method);
1907 GET_BOOLEAN_ARG (was_popped_by_exception);
1908 env->callbacks.FramePop (env, jni_env, event_thread, method,
1909 was_popped_by_exception);
1911 break;
1913 case JVMTI_EVENT_BREAKPOINT:
1914 if (env->callbacks.Breakpoint != NULL)
1916 ARG (JNIEnv *, jni_env);
1917 ARG (jmethodID, method);
1918 ARG (jlocation, location);
1919 env->callbacks.Breakpoint (env, jni_env, event_thread, method,
1920 location);
1922 break;
1924 case JVMTI_EVENT_FIELD_ACCESS:
1925 if (env->callbacks.FieldAccess != NULL)
1927 ARG (JNIEnv *, jni_env);
1928 ARG (jmethodID, method);
1929 ARG (jlocation, location);
1930 ARG (jclass, field_class);
1931 ARG (jobject, object);
1932 ARG (jfieldID, field);
1933 env->callbacks.FieldAccess (env, jni_env, event_thread, method,
1934 location, field_class, object, field);
1936 break;
1938 case JVMTI_EVENT_FIELD_MODIFICATION:
1939 if (env->callbacks.FieldModification != NULL)
1941 ARG (JNIEnv *, jni_env);
1942 ARG (jmethodID, method);
1943 ARG (jlocation, location);
1944 ARG (jclass, field_class);
1945 ARG (jobject, object);
1946 ARG (jfieldID, field);
1947 GET_CHAR_ARG (signature_type);
1948 ARG (jvalue, new_value);
1949 env->callbacks.FieldModification (env, jni_env, event_thread, method,
1950 location, field_class, object,
1951 field, signature_type, new_value);
1953 break;
1955 case JVMTI_EVENT_METHOD_ENTRY:
1956 if (env->callbacks.MethodEntry != NULL)
1958 ARG (JNIEnv *, jni_env);
1959 ARG (jmethodID, method);
1960 env->callbacks.MethodEntry (env, jni_env, event_thread, method);
1962 break;
1964 case JVMTI_EVENT_METHOD_EXIT:
1965 if (env->callbacks.MethodExit != NULL)
1967 ARG (JNIEnv *, jni_env);
1968 ARG (jmethodID, method);
1969 GET_BOOLEAN_ARG (was_popped_by_exception);
1970 ARG (jvalue, return_value);
1971 env->callbacks.MethodExit (env, jni_env, event_thread, method,
1972 was_popped_by_exception, return_value);
1974 break;
1976 case JVMTI_EVENT_NATIVE_METHOD_BIND:
1977 if (env->callbacks.NativeMethodBind != NULL)
1979 ARG (JNIEnv *, jni_env);
1980 ARG (jmethodID, method);
1981 ARG (void *, address);
1982 ARG (void **, new_address_ptr);
1983 env->callbacks.NativeMethodBind (env, jni_env, event_thread, method,
1984 address, new_address_ptr);
1986 break;
1988 case JVMTI_EVENT_COMPILED_METHOD_LOAD:
1989 if (env->callbacks.CompiledMethodLoad != NULL)
1991 ARG (jmethodID, method);
1992 ARG (jint, code_size);
1993 ARG (const void *, code_addr);
1994 ARG (jint, map_length);
1995 ARG (const jvmtiAddrLocationMap *, map);
1996 ARG (const void *, compile_info);
1997 env->callbacks.CompiledMethodLoad (env, method, code_size, code_addr,
1998 map_length, map, compile_info);
2000 break;
2002 case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
2003 if (env->callbacks.CompiledMethodUnload != NULL)
2005 ARG (jmethodID, method);
2006 ARG (const void *, code_addr);
2007 env->callbacks.CompiledMethodUnload (env, method, code_addr);
2009 break;
2011 case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
2012 if (env->callbacks.DynamicCodeGenerated != NULL)
2014 ARG (const char *, name);
2015 ARG (const void *, address);
2016 ARG (jint, length);
2017 env->callbacks.DynamicCodeGenerated (env, name, address, length);
2019 break;
2021 case JVMTI_EVENT_DATA_DUMP_REQUEST:
2022 if (env->callbacks.DataDumpRequest != NULL)
2024 env->callbacks.DataDumpRequest (env);
2026 break;
2028 case JVMTI_EVENT_MONITOR_WAIT:
2029 if (env->callbacks.MonitorWait != NULL)
2031 ARG (JNIEnv *, jni_env);
2032 ARG (jobject, object);
2033 ARG (jlong, timeout);
2034 env->callbacks.MonitorWait (env, jni_env, event_thread, object,
2035 timeout);
2037 break;
2039 case JVMTI_EVENT_MONITOR_WAITED:
2040 if (env->callbacks.MonitorWaited != NULL)
2042 ARG (JNIEnv *, jni_env);
2043 ARG (jobject, object);
2044 GET_BOOLEAN_ARG (timed_out);
2045 env->callbacks.MonitorWaited (env, jni_env, event_thread, object,
2046 timed_out);
2048 break;
2050 case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
2051 if (env->callbacks.MonitorContendedEnter != NULL)
2053 ARG (JNIEnv *, jni_env);
2054 ARG (jobject, object);
2055 env->callbacks.MonitorContendedEnter (env, jni_env, event_thread,
2056 object);
2058 break;
2060 case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
2061 if (env->callbacks.MonitorContendedEntered != NULL)
2063 ARG (JNIEnv *, jni_env);
2064 ARG (jobject, object);
2065 env->callbacks.MonitorContendedEntered (env, jni_env, event_thread,
2066 object);
2068 break;
2070 case JVMTI_EVENT_GARBAGE_COLLECTION_START:
2071 if (env->callbacks.GarbageCollectionStart != NULL)
2073 env->callbacks.GarbageCollectionStart (env);
2075 break;
2077 case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
2078 if (env->callbacks.GarbageCollectionFinish != NULL)
2080 env->callbacks.GarbageCollectionFinish (env);
2082 break;
2084 case JVMTI_EVENT_OBJECT_FREE:
2085 if (env->callbacks.ObjectFree != NULL)
2087 ARG (jlong, tag);
2088 env->callbacks.ObjectFree (env, tag);
2090 break;
2092 case JVMTI_EVENT_VM_OBJECT_ALLOC:
2093 if (env->callbacks.VMObjectAlloc != NULL)
2095 ARG (JNIEnv *, jni_env);
2096 ARG (jobject, object);
2097 ARG (jclass, object_class);
2098 ARG (jlong, size);
2099 env->callbacks.VMObjectAlloc (env, jni_env, event_thread,
2100 object, object_class, size);
2102 break;
2104 default:
2105 fprintf (stderr, "libgcj: post of unknown JVMTI event (%d)\n",
2106 (int) type);
2107 break;
2109 va_end (args);
2110 #undef ARG
2111 #undef GET_BOOLEAN_ARG
2112 #undef GET_CHAR_ARG
2115 /* Post an event to requesting JVMTI environments
2117 * This function should not be called without consulting the
2118 * JVMTI_REQUESTED_EVENT macro first (for speed). It does no real
2119 * harm (other than kill speed), since this function will still
2120 * only send the event if it was properly requested by an environment.
2122 void
2123 _Jv_JVMTI_PostEvent (jvmtiEvent type, jthread event_thread, ...)
2125 va_list args;
2126 va_start (args, event_thread);
2128 JvSynchronize dummy (_envListLock);
2129 struct jvmti_env_list *e;
2130 FOREACH_ENVIRONMENT (e)
2132 /* Events are only posted if the event was explicitly enabled,
2133 it has a registered event handler, and the event thread
2134 matches (either globally or restricted to a specific thread).
2135 Here we check all but the event handler, which will be handled
2136 by post_event. */
2137 if (e->env->enabled[EVENT_INDEX(type)]
2138 && (e->env->thread[EVENT_INDEX(type)] == NULL
2139 || e->env->thread[EVENT_INDEX(type)] == event_thread))
2141 post_event (e->env, type, event_thread, args);
2145 va_end (args);