jvmti.cc 70.9 KB
Newer Older
1 2
// jvmti.cc - JVMTI implementation

3
/* Copyright (C) 2006, 2007, 2010 Free Software Foundation
4 5 6 7 8 9 10 11

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

#include <config.h>
12
#include <platform.h>
13 14

#include <jvm.h>
15 16
#include <java-threads.h>
#include <java-gc.h>
Keith Seitz committed
17
#include <java-interp.h>
18
#include <jvmti.h>
19
#include "jvmti-int.h"
20

21 22
#include <gcj/method.h>

23 24
#include <gnu/classpath/SystemProperties.h>
#include <gnu/gcj/runtime/BootClassLoader.h>
Keith Seitz committed
25 26 27
#include <gnu/gcj/jvmti/Breakpoint.h>
#include <gnu/gcj/jvmti/BreakpointManager.h>

28 29
#include <java/lang/Class.h>
#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/Thread$State.h>
34 35 36 37 38 39
#include <java/lang/Throwable.h>
#include <java/lang/VMClassLoader.h>
#include <java/lang/reflect/Field.h>
#include <java/lang/reflect/Modifier.h>
#include <java/util/Collection.h>
#include <java/util/HashMap.h>
40 41
#include <java/util/concurrent/locks/Lock.h>
#include <java/util/concurrent/locks/ReentrantReadWriteLock.h>
42 43
#include <java/net/URL.h>

44 45 46
static void check_enabled_events (void);
static void check_enabled_event (jvmtiEvent);

47 48
namespace JVMTI
{
49 50 51 52
  // Is JVMTI enabled? (i.e., any jvmtiEnv created?)
  bool enabled;

  // Event notifications
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  bool VMInit = false;
  bool VMDeath = false;
  bool ThreadStart = false;
  bool ThreadEnd = false;
  bool ClassFileLoadHook = false;
  bool ClassLoad = false;
  bool ClassPrepare = false;
  bool VMStart = false;
  bool Exception = false;
  bool ExceptionCatch = false;
  bool SingleStep = false;
  bool FramePop = false;
  bool Breakpoint = false;
  bool FieldAccess = false;
  bool FieldModification = false;
  bool MethodEntry = false;
  bool MethodExit = false;
  bool NativeMethodBind = false;
  bool CompiledMethodLoad = false;
  bool CompiledMethodUnload = false;
  bool DynamicCodeGenerated = false;
  bool DataDumpRequest = false;
  bool reserved72 = false;
  bool MonitorWait = false;
  bool MonitorWaited = false;
  bool MonitorContendedEnter = false;
  bool MonitorContendedEntered = false;
  bool reserved77 = false;
  bool reserved78 = false;
  bool reserved79 = false;
  bool reserved80 = false;
  bool GarbageCollectionStart = false;
  bool GarbageCollectionFinish = false;
  bool ObjectFree = false;
  bool VMObjectAlloc = false;
};

90 91 92 93 94 95 96
extern struct JNINativeInterface _Jv_JNIFunctions;

struct _Jv_rawMonitorID
{
  _Jv_Mutex_t mutex;
  _Jv_ConditionVariable_t condition;
};
97

98 99 100 101 102 103 104 105 106 107
/* A simple linked list of all JVMTI environments. Since
   events must be delivered to environments in the order
   in which the environments were created, new environments
   are added to the end of the list. */
struct jvmti_env_list
{
  jvmtiEnv *env;
  struct jvmti_env_list *next;
};
static struct jvmti_env_list *_jvmtiEnvironments = NULL;
108 109
static java::util::concurrent::locks::
ReentrantReadWriteLock *_envListLock = NULL;
110 111 112
#define FOREACH_ENVIRONMENT(Ele) \
  for (Ele = _jvmtiEnvironments; Ele != NULL; Ele = Ele->next)

113 114
// Some commonly-used checks

115
#define THREAD_DEFAULT_TO_CURRENT(Ajthread)		\
116 117
  do							\
    {							\
118 119
      if (Ajthread == NULL)				\
	Ajthread = java::lang::Thread::currentThread ();	\
120 121
    }							\
  while (0)
122

123
#define THREAD_CHECK_VALID(Athread)					\
124 125
  do									\
    {									\
126
      if (!java::lang::Thread::class$.isAssignableFrom (&(Athread->class$))) \
127 128 129 130
	return JVMTI_ERROR_INVALID_THREAD;				\
    }									\
  while (0)

131
#define THREAD_CHECK_IS_ALIVE(Athread)	     \
132 133
  do					     \
    {					     \
134
      if (!Athread->isAlive ())		     \
135 136 137
	return JVMTI_ERROR_THREAD_NOT_ALIVE; \
    }					     \
  while (0)
138

139 140 141 142
// FIXME: if current phase is not set in Phases,
// return JVMTI_ERROR_WRONG_PHASE
#define REQUIRE_PHASE(Env, Phases)

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#define NULL_CHECK(Ptr)				\
  do						\
    {						\
      if (Ptr == NULL)				\
	return JVMTI_ERROR_NULL_POINTER;	\
    }						\
  while (0)

#define ILLEGAL_ARGUMENT(Cond)			\
  do						\
    {						\
      if ((Cond))				\
	return JVMTI_ERROR_ILLEGAL_ARGUMENT;	\
    }						\
  while (0)
158

159 160 161 162 163 164 165 166 167 168 169 170
#define CHECK_FOR_NATIVE_METHOD(AjmethodID)	\
  do					\
    {					\
      jboolean is_native;		\
      jvmtiError jerr = env->IsMethodNative (AjmethodID, &is_native);	\
      if (jerr != JVMTI_ERROR_NONE)					\
        return jerr;							\
      if (is_native)							\
        return JVMTI_ERROR_NATIVE_METHOD;			        \
    }									\
  while (0)

171
static jvmtiError JNICALL
172 173 174 175 176
_Jv_JVMTI_SuspendThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
{
  using namespace java::lang;

  THREAD_DEFAULT_TO_CURRENT (thread);
177 178
  THREAD_CHECK_VALID (thread);
  THREAD_CHECK_IS_ALIVE (thread);
179

180
  _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
181 182 183 184
  _Jv_SuspendThread (data);
  return JVMTI_ERROR_NONE;
}

185
static jvmtiError JNICALL
186 187 188 189 190
_Jv_JVMTI_ResumeThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
{
  using namespace java::lang;

  THREAD_DEFAULT_TO_CURRENT (thread);
191 192
  THREAD_CHECK_VALID (thread);
  THREAD_CHECK_IS_ALIVE (thread);
193

194
  _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
195 196 197 198
  _Jv_ResumeThread (data);
  return JVMTI_ERROR_NONE;
}

199 200 201 202 203 204 205 206 207
static jvmtiError JNICALL
_Jv_JVMTI_InterruptThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
{
  using namespace java::lang;

  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  // FIXME: capability handling?  'can_signal_thread'
  if (thread == NULL)
    return JVMTI_ERROR_INVALID_THREAD;
208

209 210 211
  THREAD_CHECK_VALID (thread);
  THREAD_CHECK_IS_ALIVE (thread);
  thread->interrupt();
212 213 214
  return JVMTI_ERROR_NONE;
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
// This method performs the common tasks to get and set variables of all types.
// It is called by the _Jv_JVMTI_Get/SetLocalInt/Object/.... methods.
static jvmtiError
getLocalFrame (jvmtiEnv *env, jthread thread, jint depth, jint slot, char type,
               _Jv_InterpFrame **iframe)
{
  using namespace java::lang;
   
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
   
  ILLEGAL_ARGUMENT (depth < 0);
  
  THREAD_DEFAULT_TO_CURRENT (thread);
  THREAD_CHECK_VALID (thread);
  THREAD_CHECK_IS_ALIVE (thread);
  
  _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
  
  for (int i = 0; i < depth; i++)
    {    
      frame = frame->next;
    
      if (frame == NULL)
        return JVMTI_ERROR_NO_MORE_FRAMES; 
    }
  
  if (frame->frame_type == frame_native)
    return JVMTI_ERROR_OPAQUE_FRAME;
  
  jint max_locals;
  jvmtiError jerr = env->GetMaxLocals (reinterpret_cast<jmethodID> 
                                         (frame->self->get_method ()),
                                       &max_locals);
  if (jerr != JVMTI_ERROR_NONE)
    return jerr; 
  
  _Jv_InterpFrame *tmp_iframe = reinterpret_cast<_Jv_InterpFrame *> (frame);
  
  // The second slot taken up by a long type is marked as type 'x' meaning it
  // is not valid for access since it holds only the 4 low bytes of the value.
  if (tmp_iframe->locals_type[slot] == 'x')
    return JVMTI_ERROR_INVALID_SLOT;
  
  if (tmp_iframe->locals_type[slot] != type)
    return JVMTI_ERROR_TYPE_MISMATCH;
  
  // Check for invalid slots, if the type is a long type, we must check that
  // the next slot is valid as well.
  if (slot < 0 || slot >= max_locals 
      || ((type == 'l' || type == 'd') && slot + 1 >= max_locals))
    return JVMTI_ERROR_INVALID_SLOT;
  
  *iframe = tmp_iframe;
  
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_GetLocalObject (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                          jobject *value)
{
  NULL_CHECK (value);

  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'o', &frame);
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
  *value = frame->locals[slot].o;
  
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_SetLocalObject (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                          jobject value)
{
  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'o', &frame);
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
  frame->locals[slot].o = value;

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_GetLocalInt (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                       jint *value)
{
  NULL_CHECK (value);
  
  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'i', &frame);

  if (jerr != JVMTI_ERROR_NONE)
    return jerr;

  *value = frame->locals[slot].i;

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_SetLocalInt (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                       jint value)
{
  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'i', &frame);
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
  frame->locals[slot].i = value;

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_GetLocalLong (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                        jlong *value)
{
  NULL_CHECK (value);

  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'l', &frame);
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;

#if SIZEOF_VOID_P==8
  *value = frame->locals[slot].l;
#else
  _Jv_word2 val;
  val.ia[0] = frame->locals[slot].ia[0];
  val.ia[1] = frame->locals[slot + 1].ia[0];
  *value = val.l;
#endif
  
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_SetLocalLong (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                        jlong value)
{
  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'l', &frame);
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;

#if SIZEOF_VOID_P==8
  frame->locals[slot].l = value;
#else
  _Jv_word2 val;
	val.l = value;
	frame->locals[slot].ia[0] = val.ia[0];
	frame->locals[slot + 1].ia[0] = val.ia[1];
#endif

  return JVMTI_ERROR_NONE;
}


static jvmtiError JNICALL
_Jv_JVMTI_GetLocalFloat (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                         jfloat *value)
{
  NULL_CHECK (value);

  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'f', &frame);
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
  *value = frame->locals[slot].f;

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_SetLocalFloat (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                         jfloat value)
{
  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'f', &frame);
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
  frame->locals[slot].f = value;

  return JVMTI_ERROR_NONE;
}


static jvmtiError JNICALL
_Jv_JVMTI_GetLocalDouble (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                          jdouble *value)
{
  NULL_CHECK (value);

  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'd', &frame);
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
#if SIZEOF_VOID_P==8
  *value = frame->locals[slot].d;
#else
  _Jv_word2 val;
  val.ia[0] = frame->locals[slot].ia[0];
  val.ia[1] = frame->locals[slot + 1].ia[0];
  *value = val.d;
#endif

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_SetLocalDouble (jvmtiEnv *env, jthread thread, jint depth, jint slot,
                          jdouble value)
{
  _Jv_InterpFrame *frame;
  jvmtiError jerr = getLocalFrame (env, thread, depth, slot, 'd', &frame);
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
    
#if SIZEOF_VOID_P==8
  frame->locals[slot].d = value;
#else
  _Jv_word2 val;
  val.d = value;
  frame->locals[slot].ia[0] = val.ia[0];
  frame->locals[slot + 1].ia[0] = val.ia[1]; 
#endif

  return JVMTI_ERROR_NONE;
}

462
static jvmtiError JNICALL
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
_Jv_JVMTI_GetAllThreads(MAYBE_UNUSED jvmtiEnv *env, jint *thread_cnt,
                        jthread **threads)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  NULL_CHECK (thread_cnt);
  NULL_CHECK (threads);
   
  using namespace java::lang;
   
  ThreadGroup *root_grp = ThreadGroup::root;
  jint estimate = root_grp->activeCount ();

  JArray<Thread *> *thr_arr;

  // Allocate some extra space since threads can be created between calls
  try
    { 
480 481 482
      thr_arr = reinterpret_cast<JArray<Thread *> *> (JvNewObjectArray 
						      ((estimate * 2),
						       &Thread::class$, NULL));
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    }
  catch (java::lang::OutOfMemoryError *err)
    {
      return JVMTI_ERROR_OUT_OF_MEMORY;
    }
    
  *thread_cnt = root_grp->enumerate (thr_arr);
   
  jvmtiError jerr = env->Allocate ((jlong) ((*thread_cnt) * sizeof (jthread)),
                                   (unsigned char **) threads);
 
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
   
  // Transfer the threads to the result array
  jthread *tmp_arr = reinterpret_cast<jthread *> (elements (thr_arr));
 
  memcpy ((*threads), tmp_arr, (*thread_cnt));
   
  return JVMTI_ERROR_NONE;
}

505
static jvmtiError JNICALL
506
_Jv_JVMTI_GetFrameCount (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
507
                         jint *frame_count)
508 509 510 511 512 513 514 515
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  
  NULL_CHECK (frame_count);
	
  using namespace java::lang;
  
  THREAD_DEFAULT_TO_CURRENT (thread);
516 517
  THREAD_CHECK_VALID (thread);
  THREAD_CHECK_IS_ALIVE (thread);
518
   
519
  _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
520
  (*frame_count) = frame->depth ();
521 522 523 524
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
_Jv_JVMTI_GetThreadState (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
			  jint *thread_state_ptr)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);

  THREAD_DEFAULT_TO_CURRENT (thread);
  THREAD_CHECK_VALID (thread);
  NULL_CHECK (thread_state_ptr);

  jint state = 0;
  if (thread->isAlive ())
    {
      state |= JVMTI_THREAD_STATE_ALIVE;

      _Jv_Thread_t *data = _Jv_ThreadGetData (thread);
      if (_Jv_IsThreadSuspended (data))
	state |= JVMTI_THREAD_STATE_SUSPENDED;

      if (thread->isInterrupted ())
	state |= JVMTI_THREAD_STATE_INTERRUPTED;

      _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
      if (frame != NULL && frame->frame_type == frame_native)
	state |= JVMTI_THREAD_STATE_IN_NATIVE;

      using namespace java::lang;
      Thread$State *ts = thread->getState ();
      if (ts == Thread$State::RUNNABLE)
	state |= JVMTI_THREAD_STATE_RUNNABLE;
      else if (ts == Thread$State::BLOCKED)
	state |= JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER;
      else if (ts == Thread$State::TIMED_WAITING
	       || ts == Thread$State::WAITING)
	{
	  state |= JVMTI_THREAD_STATE_WAITING;
	  state |= ((ts == Thread$State::WAITING)
		    ? JVMTI_THREAD_STATE_WAITING_INDEFINITELY
		    : JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT);

	  /* FIXME: We don't have a way to tell
	     the caller why the thread is suspended,
	     i.e., JVMTI_THREAD_STATE_SLEEPING,
	     JVMTI_THREAD_STATE_PARKED, and
	     JVMTI_THREAD_STATE_IN_OBJECT_WAIT
	     are never set. */
	}
    }
  else
    {
      using namespace java::lang;
      Thread$State *ts = thread->getState ();
      if (ts == Thread$State::TERMINATED)
	state |= JVMTI_THREAD_STATE_TERMINATED;
    }

  *thread_state_ptr = state;
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
585 586 587 588 589 590
_Jv_JVMTI_CreateRawMonitor (MAYBE_UNUSED jvmtiEnv *env, const char *name,
			    jrawMonitorID *result)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
  NULL_CHECK (name);
  NULL_CHECK (result);
591 592 593
  *result = (jrawMonitorID) _Jv_MallocUnchecked (sizeof (_Jv_rawMonitorID));
  if (*result == NULL)
    return JVMTI_ERROR_OUT_OF_MEMORY;
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
  _Jv_MutexInit (&(*result)->mutex);
  _Jv_CondInit (&(*result)->condition);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_DestroyRawMonitor (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
  // Note we have no better way of knowing whether this object is
  // really a raw monitor.
  if (monitor == NULL)
    return JVMTI_ERROR_INVALID_MONITOR;
  // FIXME: perform checks on monitor, release it if this thread owns
  // it.
#ifdef _Jv_HaveMutexDestroy
  _Jv_MutexDestroy (&monitor->mutex);
#endif
  _Jv_Free (monitor);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_RawMonitorEnter (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
{
  if (monitor == NULL)
    return JVMTI_ERROR_INVALID_MONITOR;
  _Jv_MutexLock (&monitor->mutex);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_RawMonitorExit (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
{
  if (monitor == NULL)
    return JVMTI_ERROR_INVALID_MONITOR;
  if (_Jv_MutexUnlock (&monitor->mutex))
    return JVMTI_ERROR_NOT_MONITOR_OWNER;
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
636 637
_Jv_JVMTI_RawMonitorWait (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor,
			  jlong millis)
638 639 640
{
  if (monitor == NULL)
    return JVMTI_ERROR_INVALID_MONITOR;
641
  int r = _Jv_CondWait (&monitor->condition, &monitor->mutex, millis, 0);
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
  if (r == _JV_NOT_OWNER)
    return JVMTI_ERROR_NOT_MONITOR_OWNER;
  if (r == _JV_INTERRUPTED)
    return JVMTI_ERROR_INTERRUPT;
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_RawMonitorNotify (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor)
{
  if (monitor == NULL)
    return JVMTI_ERROR_INVALID_MONITOR;
  if (_Jv_CondNotify (&monitor->condition, &monitor->mutex) == _JV_NOT_OWNER)
    return JVMTI_ERROR_NOT_MONITOR_OWNER;
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_RawMonitorNotifyAll (MAYBE_UNUSED jvmtiEnv *env,
			       jrawMonitorID monitor)
{
  if (monitor == NULL)
    return JVMTI_ERROR_INVALID_MONITOR;
  if (_Jv_CondNotifyAll (&monitor->condition, &monitor->mutex)
      == _JV_NOT_OWNER)
    return JVMTI_ERROR_NOT_MONITOR_OWNER;
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
Keith Seitz committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
_Jv_JVMTI_SetBreakpoint (jvmtiEnv *env, jmethodID method, jlocation location)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);

  using namespace gnu::gcj::jvmti;
  Breakpoint *bp
    = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
					location);
  if (bp == NULL)
    {
      jclass klass;
      jvmtiError err = env->GetMethodDeclaringClass (method, &klass);
      if (err != JVMTI_ERROR_NONE)
	return err;

      if (!_Jv_IsInterpretedClass (klass))
	return JVMTI_ERROR_INVALID_CLASS;

      _Jv_MethodBase *base = _Jv_FindInterpreterMethod (klass, method);
      if (base == NULL)
	return JVMTI_ERROR_INVALID_METHODID;

      jint flags;
      err = env->GetMethodModifiers (method, &flags);
      if (err != JVMTI_ERROR_NONE)
	return err;

      if (flags & java::lang::reflect::Modifier::NATIVE)
	return JVMTI_ERROR_NATIVE_METHOD;

      _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (base);
      if (imeth->get_insn (location) == NULL)
	return JVMTI_ERROR_INVALID_LOCATION;

      // Now the breakpoint can be safely installed
      bp = BreakpointManager::newBreakpoint (reinterpret_cast<jlong> (method),
					     location);
    }
  else
    {
      // Duplicate breakpoints are not permitted by JVMTI
      return JVMTI_ERROR_DUPLICATE;
    }

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_ClearBreakpoint (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
			   jlocation location)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);

  using namespace gnu::gcj::jvmti;

  Breakpoint *bp
    = BreakpointManager::getBreakpoint (reinterpret_cast<jlong> (method),
					location);
  if (bp == NULL)
    return JVMTI_ERROR_NOT_FOUND;

  BreakpointManager::deleteBreakpoint (reinterpret_cast<jlong> (method), location);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
738 739 740
_Jv_JVMTI_Allocate (MAYBE_UNUSED jvmtiEnv *env, jlong size,
		    unsigned char **result)
{
741
  ILLEGAL_ARGUMENT (size < 0);
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
  NULL_CHECK (result);
  if (size == 0)
    *result = NULL;
  else
    {
      *result = (unsigned char *) _Jv_MallocUnchecked (size);
      if (*result == NULL)
	return JVMTI_ERROR_OUT_OF_MEMORY;
    }
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_Deallocate (MAYBE_UNUSED jvmtiEnv *env, unsigned char *mem)
{
  if (mem != NULL)
    _Jv_Free (mem);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
_Jv_JVMTI_GetClassStatus (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			  jint *status_ptr)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  NULL_CHECK (status_ptr);
  if (klass == NULL)
    return JVMTI_ERROR_INVALID_CLASS;

  if (klass->isArray ())
    *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
  else if (klass->isPrimitive ())
    *status_ptr  = JVMTI_CLASS_STATUS_PRIMITIVE;
  else
    {
      jbyte state = _Jv_GetClassState (klass);
      *status_ptr = 0;
      if (state >= JV_STATE_LINKED)
	(*status_ptr) |= JVMTI_CLASS_STATUS_VERIFIED;
      if (state >= JV_STATE_PREPARED)
	(*status_ptr) |= JVMTI_CLASS_STATUS_PREPARED;
      if (state == JV_STATE_ERROR || state == JV_STATE_PHANTOM)
	(*status_ptr) |= JVMTI_CLASS_STATUS_ERROR;
      else if (state == JV_STATE_DONE)
	(*status_ptr) |= JVMTI_CLASS_STATUS_INITIALIZED;
    }

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
793 794
_Jv_JVMTI_GetClassModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			     jint *mods)
795 796 797 798 799 800 801 802 803 804 805
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  // Don't bother checking KLASS' type.
  if (klass == NULL)
    return JVMTI_ERROR_INVALID_CLASS;
  NULL_CHECK (mods);
  *mods = klass->getModifiers();
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
806 807
_Jv_JVMTI_GetClassMethods (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			   jint *count_ptr, jmethodID **methods_ptr)
808 809 810 811 812 813 814 815 816 817
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  // FIXME: capability can_maintain_original_method_order
  // Don't bother checking KLASS' type.
  if (klass == NULL)
    return JVMTI_ERROR_INVALID_CLASS;
  NULL_CHECK (count_ptr);
  NULL_CHECK (methods_ptr);
  *count_ptr = JvNumMethods(klass);

818 819 820 821 822
  *methods_ptr
    = (jmethodID *) _Jv_MallocUnchecked (*count_ptr * sizeof (jmethodID));
  if (*methods_ptr == NULL)
    return JVMTI_ERROR_OUT_OF_MEMORY;

823 824 825 826 827 828 829 830 831
  jmethodID start = JvGetFirstMethod (klass);
  for (jint i = 0; i < *count_ptr; ++i)
    // FIXME: correct?
    (*methods_ptr)[i] = start + i;

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
832 833
_Jv_JVMTI_IsInterface (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
		       jboolean *result)
834 835 836 837 838 839 840 841 842 843
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (klass == NULL)
    return JVMTI_ERROR_INVALID_CLASS;
  NULL_CHECK (result);
  *result = klass->isInterface();
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
844 845
_Jv_JVMTI_IsArrayClass (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			jboolean *result)
846 847 848 849 850 851 852 853 854 855
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (klass == NULL)
    return JVMTI_ERROR_INVALID_CLASS;
  NULL_CHECK (result);
  *result = klass->isArray();
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
856 857
_Jv_JVMTI_GetClassLoader (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			  jobject *result)
858 859 860 861 862 863 864 865 866 867
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (klass == NULL)
    return JVMTI_ERROR_INVALID_CLASS;
  NULL_CHECK (result);
  *result = klass->getClassLoaderInternal();
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
868 869
_Jv_JVMTI_GetObjectHashCode (MAYBE_UNUSED jvmtiEnv *env, jobject obj,
			     jint *result)
870 871 872 873 874 875 876 877 878 879
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (obj == NULL)
    return JVMTI_ERROR_INVALID_OBJECT;
  NULL_CHECK (result);
  *result = _Jv_HashCode (obj);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
880 881
_Jv_JVMTI_GetFieldModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			     jfieldID field, jint *result)
882 883 884 885 886 887 888 889 890 891 892 893
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (klass == NULL)
    return JVMTI_ERROR_INVALID_CLASS;
  if (field == NULL)
    return JVMTI_ERROR_INVALID_FIELDID;
  NULL_CHECK (result);
  *result = field->getModifiers();
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
894 895
_Jv_JVMTI_IsFieldSynthetic (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			    jfieldID field, jboolean *result)
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (klass == NULL)
    return JVMTI_ERROR_INVALID_CLASS;
  if (field == NULL)
    return JVMTI_ERROR_INVALID_FIELDID;
  NULL_CHECK (result);

  // FIXME: capability can_get_synthetic_attribute
  *result = ((field->getModifiers() & java::lang::reflect::Modifier::SYNTHETIC)
	     != 0);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
_Jv_JVMTI_GetMethodName (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
			 char **name_ptr, char **signature_ptr,
			 char **generic_ptr)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);

  if (method == NULL)
    return JVMTI_ERROR_INVALID_METHODID;

  if (name_ptr != NULL)
    {
      int len = static_cast<int> (method->name->len ());
      *name_ptr = (char *) _Jv_MallocUnchecked (len + 1);
      if (*name_ptr == NULL)
	return JVMTI_ERROR_OUT_OF_MEMORY;
      strncpy (*name_ptr, method->name->chars (), len);
      (*name_ptr)[len] = '\0';
    }

  if (signature_ptr != NULL)
    {
      int len = static_cast<int> (method->signature->len ());
      *signature_ptr = (char *) _Jv_MallocUnchecked (len + 1);
      if (*signature_ptr == NULL)
	{
	  if (name_ptr != NULL)
	    _Jv_Free (*name_ptr);
	  return JVMTI_ERROR_OUT_OF_MEMORY;
	}
      strncpy (*signature_ptr, method->signature->chars (), len);
      (*signature_ptr)[len] = '\0';
    }

  if (generic_ptr != NULL)
    {
      *generic_ptr = NULL;
    }

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
953 954
_Jv_JVMTI_GetMethodModifiers (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
			      jint *result)
955 956 957 958 959 960 961 962 963 964 965 966
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (method == NULL)
    return JVMTI_ERROR_INVALID_METHODID;
  NULL_CHECK (result);

  // FIXME: mask off some internal bits...
  *result = method->accflags;
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
_Jv_JVMTI_GetLineNumberTable (jvmtiEnv *env, jmethodID method,
			      jint *entry_count_ptr,
			      jvmtiLineNumberEntry **table_ptr)
{
  NULL_CHECK (entry_count_ptr);
  NULL_CHECK (table_ptr);

  jclass klass;
  jvmtiError jerr = env->GetMethodDeclaringClass (method, &klass);
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;

  _Jv_MethodBase *base = _Jv_FindInterpreterMethod (klass, method);
  if (base == NULL)
    return JVMTI_ERROR_INVALID_METHODID;

  if (java::lang::reflect::Modifier::isNative (method->accflags)
      || !_Jv_IsInterpretedClass (klass))
    return JVMTI_ERROR_NATIVE_METHOD;

  _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> (base);
  jlong start, end;
  jintArray lines = NULL;
  jlongArray indices = NULL;
  imeth->get_line_table (start, end, lines, indices);
  if (lines == NULL)
    return JVMTI_ERROR_ABSENT_INFORMATION;

  jvmtiLineNumberEntry *table;
  jsize len = lines->length * sizeof (jvmtiLineNumberEntry);
  table = (jvmtiLineNumberEntry *) _Jv_MallocUnchecked (len);
  if (table == NULL)
    return JVMTI_ERROR_OUT_OF_MEMORY;
  
  jint *line = elements (lines);
  jlong *index = elements (indices);
  for (int i = 0; i < lines->length; ++i)
    {
      table[i].start_location = index[i];
      table[i].line_number = line[i];
    }

  *table_ptr = table;
  *entry_count_ptr = lines->length;
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
_Jv_JVMTI_GetLocalVariableTable (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
                                 jint *num_locals,
                                 jvmtiLocalVariableEntry **locals)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  NULL_CHECK (num_locals);
  NULL_CHECK (locals);
  
  CHECK_FOR_NATIVE_METHOD(method);
  
  jclass klass;
  jvmtiError jerr = env->GetMethodDeclaringClass (method, &klass);
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;

  _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> 
                              (_Jv_FindInterpreterMethod (klass, method));
  
  if (imeth == NULL)
    return JVMTI_ERROR_INVALID_METHODID;
  
  jerr = env->GetMaxLocals (method, num_locals);
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
  jerr = env->Allocate (static_cast<jlong> 
                          ((*num_locals) * sizeof (jvmtiLocalVariableEntry)),
                        reinterpret_cast<unsigned char **> (locals));
  
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
  //the slot in the methods local_var_table to get
  int table_slot = 0;
  char *name;
  char *sig;
  char *generic_sig;
  
  while (table_slot < *num_locals 
         && imeth->get_local_var_table (&name, &sig, &generic_sig,
                                 &((((*locals)[table_slot].start_location))),
                                 &((*locals)[table_slot].length), 
                                 &((*locals)[table_slot].slot),
                                 table_slot) 
            >= 0)
    {
1061
      char **str_ptr = &(*locals)[table_slot].name;
1062
      jerr = env->Allocate (static_cast<jlong> (strlen (name) + 1),
1063
                             reinterpret_cast<unsigned char **> (str_ptr));
1064 1065 1066
      if (jerr != JVMTI_ERROR_NONE)
        return jerr;
      strcpy ((*locals)[table_slot].name, name);
1067 1068 1069 1070
      
      str_ptr = &(*locals)[table_slot].signature;
      jerr = env->Allocate (static_cast<jlong> (strlen (sig) + 1),
                               reinterpret_cast<unsigned char **> (str_ptr));
1071 1072 1073
      if (jerr != JVMTI_ERROR_NONE)
        return jerr;
      strcpy ((*locals)[table_slot].signature, sig);
1074 1075 1076 1077
      
      str_ptr = &(*locals)[table_slot].generic_signature;
      jerr = env->Allocate (static_cast<jlong> (strlen (generic_sig) + 1),
                               reinterpret_cast<unsigned char **> (str_ptr));
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
      if (jerr != JVMTI_ERROR_NONE)
        return jerr;
      strcpy ((*locals)[table_slot].generic_signature, generic_sig);
      
      table_slot++;
    }

  if (table_slot == 0)
    return JVMTI_ERROR_ABSENT_INFORMATION;
  
  // If there are double or long variables in the table, the the table will be
  // smaller than the max number of slots, so correct for this here.
  if ((table_slot) < *num_locals)
    *num_locals = table_slot;
  
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1097 1098
_Jv_JVMTI_IsMethodNative (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
			  jboolean *result)
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (method == NULL)
    return JVMTI_ERROR_INVALID_METHODID;
  NULL_CHECK (result);

  *result = ((method->accflags & java::lang::reflect::Modifier::NATIVE) != 0);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1110 1111
_Jv_JVMTI_IsMethodSynthetic (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
			     jboolean *result)
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (method == NULL)
    return JVMTI_ERROR_INVALID_METHODID;
  NULL_CHECK (result);

  // FIXME capability can_get_synthetic_attribute

  *result = ((method->accflags & java::lang::reflect::Modifier::SYNTHETIC)
	     != 0);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1126
_Jv_JVMTI_GetMaxLocals (jvmtiEnv *env, jmethodID method, jint *max_locals)
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  NULL_CHECK (max_locals);
  
  CHECK_FOR_NATIVE_METHOD (method);
  
  jclass klass;
  jvmtiError jerr = env->GetMethodDeclaringClass (method, &klass);
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;

  _Jv_InterpMethod *imeth = reinterpret_cast<_Jv_InterpMethod *> 
                              (_Jv_FindInterpreterMethod (klass, method));
    
  if (imeth == NULL)
    return JVMTI_ERROR_INVALID_METHODID;
  
  *max_locals = imeth->get_max_locals ();
  
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
_Jv_JVMTI_GetArgumentsSize (jvmtiEnv *env, jmethodID method, jint *size)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  NULL_CHECK (size);
  
  CHECK_FOR_NATIVE_METHOD (method);
  
  jvmtiError jerr;
  char *sig;
  jint num_slots = 0;
  
  jerr = env->GetMethodName (method, NULL, &sig, NULL);
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
  // If the method is non-static add a slot for the "this" pointer.
  if ((method->accflags & java::lang::reflect::Modifier::STATIC) == 0)
    num_slots++;
  
  for (int i = 0; sig[i] != ')'; i++)
    {
      if (sig[i] == 'Z' || sig[i] == 'B' || sig[i] == 'C' || sig[i] == 'S'
          || sig[i] == 'I' || sig[i] == 'F')
        num_slots++;
      else if (sig[i] == 'J' || sig[i] == 'D')
1175 1176
        {
          // If this is an array of wide types it uses a single slot
1177
          if (i > 0 && sig[i - 1] == '[')
1178 1179 1180 1181
            num_slots++;
          else
            num_slots += 2;
        }
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
      else if (sig[i] == 'L')
        {
          num_slots++;
          while (sig[i] != ';')
            i++;
        }
    }
  
  *size = num_slots;
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
_Jv_JVMTI_GetMethodDeclaringClass (MAYBE_UNUSED jvmtiEnv *env,
				   jmethodID method,
				   jclass *declaring_class_ptr)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  NULL_CHECK (declaring_class_ptr);

  jclass klass = _Jv_GetMethodDeclaringClass (method);
  if (klass != NULL)
    {
      *declaring_class_ptr = klass;
      return JVMTI_ERROR_NONE;
    }

  return JVMTI_ERROR_INVALID_METHODID;
}

static jvmtiError JNICALL
1213
_Jv_JVMTI_GetClassLoaderClasses (MAYBE_UNUSED jvmtiEnv *env,
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
				 jobject init_loader,
				 jint *count_ptr,
				 jclass **result_ptr)
{
  using namespace java::lang;
  using namespace java::util;

  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  NULL_CHECK (count_ptr);
  NULL_CHECK (result_ptr);

  ClassLoader *loader = (ClassLoader *) init_loader;
  if (loader == NULL)
    loader = VMClassLoader::bootLoader;

  Collection *values = loader->loadedClasses->values();
  jobjectArray array = values->toArray();
  *count_ptr = array->length;
  jobject *elts = elements (array);
1233 1234 1235 1236 1237
  jclass *result
    = (jclass *) _Jv_MallocUnchecked (*count_ptr * sizeof (jclass));
  if (result == NULL)
    return JVMTI_ERROR_OUT_OF_MEMORY;

1238 1239 1240 1241 1242 1243 1244 1245 1246
  // FIXME: JNI references...
  memcpy (result, elts, *count_ptr * sizeof (jclass));

  *result_ptr = result;

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
_Jv_JVMTI_GetStackTrace (MAYBE_UNUSED jvmtiEnv *env, jthread thread,
                         jint start_depth, jint max_frames,
                         jvmtiFrameInfo *frames, jint *frame_count)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);

  ILLEGAL_ARGUMENT (max_frames < 0);
  
  NULL_CHECK (frames);
  NULL_CHECK (frame_count);
	
  using namespace java::lang;
  
  THREAD_DEFAULT_TO_CURRENT (thread);
1261 1262
  THREAD_CHECK_VALID (thread);
  THREAD_CHECK_IS_ALIVE (thread);
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    
  jvmtiError jerr = env->GetFrameCount (thread, frame_count);
  if (jerr != JVMTI_ERROR_NONE)
    return jerr;
  
  // start_depth can be either a positive number, indicating the depth of the
  // stack at which to begin the trace, or a negative number indicating the
  // number of frames at the bottom of the stack to exclude.  These checks
  // ensure that it is a valid value in either case
  
  ILLEGAL_ARGUMENT (start_depth >= (*frame_count));
  ILLEGAL_ARGUMENT (start_depth < (-(*frame_count)));
  
1276
  _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308

  // If start_depth is negative use this to determine at what depth to start
  // the trace by adding it to the length of the call stack.  This allows the
  // use of the same frame "discarding" mechanism as for a positive start_depth
  if (start_depth < 0)
    start_depth = *frame_count + start_depth;
  
  // If start_depth > 0 "remove" start_depth frames from the beginning
  // of the stack before beginning the trace by moving along the frame list.
  while (start_depth > 0)
    {
      frame = frame->next;
      start_depth--;
      (*frame_count)--;
    }
  
  // Now check to see if the array supplied by the agent is large enough to
  // hold frame_count frames, after adjustment for start_depth.
  if ((*frame_count) > max_frames)
    (*frame_count) = max_frames;
  
  for (int i = 0; i < (*frame_count); i++)
    {
      frames[i].method = frame->self->get_method ();
      
      // Set the location in the frame, native frames have location = -1
      if (frame->frame_type == frame_interpreter)
        {
          _Jv_InterpMethod *imeth 
            = static_cast<_Jv_InterpMethod *> (frame->self);
          _Jv_InterpFrame *interp_frame 
            = static_cast<_Jv_InterpFrame *> (frame);
1309
          frames[i].location = imeth->insn_index (interp_frame->get_pc ());
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
        }
      else
        frames[i].location = -1;
        
      frame = frame->next;
    }
    
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1321
_Jv_JVMTI_ForceGarbageCollection (MAYBE_UNUSED jvmtiEnv *env)
1322 1323 1324 1325 1326 1327 1328
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  _Jv_RunGC();
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1329
_Jv_JVMTI_SetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
1330 1331 1332 1333 1334 1335 1336 1337 1338
			       const jniNativeInterface *function_table)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  NULL_CHECK (function_table);
  memcpy (&_Jv_JNIFunctions, function_table, sizeof (jniNativeInterface));
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1339
_Jv_JVMTI_GetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
1340 1341 1342 1343 1344
			       jniNativeInterface **function_table)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  NULL_CHECK (function_table);
  *function_table
1345 1346 1347
    = (jniNativeInterface *) _Jv_MallocUnchecked (sizeof (jniNativeInterface));
  if (*function_table == NULL)
    return JVMTI_ERROR_OUT_OF_MEMORY;
1348 1349 1350
  memcpy (*function_table, &_Jv_JNIFunctions, sizeof (jniNativeInterface));
  return JVMTI_ERROR_NONE;
}
1351

1352
static jvmtiError JNICALL
1353 1354
_Jv_JVMTI_DisposeEnvironment (jvmtiEnv *env)
{
1355 1356 1357 1358 1359 1360
  NULL_CHECK (env);

  if (_jvmtiEnvironments == NULL)
    return JVMTI_ERROR_INVALID_ENVIRONMENT;
  else
    {
1361
      _envListLock->writeLock ()->lock ();
1362 1363
      if (_jvmtiEnvironments->env == env)
	{
1364
	  struct jvmti_env_list *next = _jvmtiEnvironments->next;
1365
	  _Jv_Free (_jvmtiEnvironments);
1366
	  _jvmtiEnvironments = next;
1367 1368 1369 1370 1371 1372 1373
	}
      else
	{
	  struct jvmti_env_list *e = _jvmtiEnvironments; 
	  while (e->next != NULL && e->next->env != env)
	    e = e->next;
	  if (e->next == NULL)
1374 1375 1376 1377
	    {
	      _envListLock->writeLock ()->unlock ();
	      return JVMTI_ERROR_INVALID_ENVIRONMENT;
	    }
1378 1379 1380 1381 1382

	  struct jvmti_env_list *next = e->next->next;
	  _Jv_Free (e->next);
	  e->next = next;
	}
1383
      _envListLock->writeLock ()->unlock ();
1384 1385
    }

1386
  _Jv_Free (env);
1387 1388 1389

  check_enabled_events ();

1390 1391 1392
  return JVMTI_ERROR_NONE;
}

1393
static jvmtiError JNICALL
1394
_Jv_JVMTI_GetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
			     char **result)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
  NULL_CHECK (property);
  NULL_CHECK (result);

  jstring name = JvNewStringUTF(property);
  jstring result_str = gnu::classpath::SystemProperties::getProperty(name);

  if (result_str == NULL)
    return JVMTI_ERROR_NOT_AVAILABLE;

  int len = JvGetStringUTFLength (result_str);
1408 1409 1410
  *result = (char *) _Jv_MallocUnchecked (len + 1);
  if (*result == NULL)
    return JVMTI_ERROR_OUT_OF_MEMORY;
1411 1412 1413 1414 1415 1416 1417
  JvGetStringUTFRegion (result_str, 0, result_str->length(), *result);
  (*result)[len] = '\0';

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1418
_Jv_JVMTI_SetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
			     const char *value)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD);

  NULL_CHECK (property);
  if (value == NULL)
    {
      // FIXME: When would a property not be writeable?
      return JVMTI_ERROR_NONE;
    }

  jstring prop_str = JvNewStringUTF(property);
  jstring value_str = JvNewStringUTF(value);
  gnu::classpath::SystemProperties::setProperty(prop_str, value_str);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_GetTime (MAYBE_UNUSED jvmtiEnv *env, jlong *nanos_ptr)
{
  NULL_CHECK (nanos_ptr);
  *nanos_ptr = _Jv_platform_nanotime();
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_GetAvailableProcessors (MAYBE_UNUSED jvmtiEnv *env,
				  jint *nprocessors_ptr)
{
  NULL_CHECK (nprocessors_ptr);
#ifdef _SC_NPROCESSORS_ONLN
  *nprocessors_ptr = sysconf(_SC_NPROCESSORS_ONLN);
#else
  *nprocessors_ptr = 1;
#endif
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1458 1459
_Jv_JVMTI_AddToBootstrapClassLoaderSearch (MAYBE_UNUSED jvmtiEnv *env,
					   const char *segment)
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
{
  using namespace java::lang;
  using namespace java::net;
  using namespace gnu::gcj::runtime;

  REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD);
  NULL_CHECK (segment);

  jstring str_segment = JvNewStringUTF(segment);
  URL *url;
  try
    {
      url = new URL(JvNewStringUTF("file"), NULL, str_segment);
    }
  catch (jthrowable ignore)
    {
      return JVMTI_ERROR_ILLEGAL_ARGUMENT;
    }

  BootClassLoader *loader = VMClassLoader::bootLoader;
  // Don't call this too early.
  // assert (loader != NULL);
  loader->addURL(url);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_SetVerboseFlag (MAYBE_UNUSED jvmtiEnv *env, jvmtiVerboseFlag flag,
			  jboolean value)
{
  switch (flag)
    {
    case JVMTI_VERBOSE_OTHER:
    case JVMTI_VERBOSE_GC:
    case JVMTI_VERBOSE_JNI:
      // Ignore.
      break;
    case JVMTI_VERBOSE_CLASS:
      gcj::verbose_class_flag = value;
      break;
    default:
      return JVMTI_ERROR_ILLEGAL_ARGUMENT;
    }
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
1507 1508
_Jv_JVMTI_GetObjectSize (MAYBE_UNUSED jvmtiEnv *env, jobject object,
			 jlong *result)
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  if (object == NULL)
    return JVMTI_ERROR_INVALID_OBJECT;
  NULL_CHECK (result);

  jclass klass = object->getClass();
  if (klass->isArray())
    {
      jclass comp = klass->getComponentType();
      jint base
1520 1521
	= (jint) (_Jv_uintptr_t) _Jv_GetArrayElementFromElementType(NULL,
								    klass->getComponentType());
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
      // FIXME: correct for primitive types?
      jint compSize = comp->size();
      __JArray *array = (__JArray *) object;
      *result = base + array->length * compSize;
    }
  else
    {
      // Note that if OBJECT is a String then it may (if
      // str->data==str) take more space.  Do we care?
      *result = klass->size();
    }
  return JVMTI_ERROR_NONE;
}

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
/* An event is enabled only if it has both an event handler
   and it is enabled in the environment. */
static void
check_enabled_event (jvmtiEvent type)
{
  bool *enabled;
  int offset;

#define GET_OFFSET(Event)				\
  do							\
    {							\
      enabled = &JVMTI::Event;				\
      offset = offsetof (jvmtiEventCallbacks, Event);	\
    }							\
  while (0)

  switch (type)
    {
    case JVMTI_EVENT_VM_INIT:
      GET_OFFSET (VMInit);
      break;

    case JVMTI_EVENT_VM_DEATH:
      GET_OFFSET (VMDeath);
      break;

    case JVMTI_EVENT_THREAD_START:
      GET_OFFSET (ThreadStart);
      break;

    case JVMTI_EVENT_THREAD_END:
      GET_OFFSET (ThreadEnd);
      break;

    case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
      GET_OFFSET (ClassFileLoadHook);
      break;

    case JVMTI_EVENT_CLASS_LOAD:
      GET_OFFSET (ClassLoad);
      break;

    case JVMTI_EVENT_CLASS_PREPARE:
      GET_OFFSET (ClassPrepare);
      break;

    case JVMTI_EVENT_VM_START:
      GET_OFFSET (VMStart);
      break;

    case JVMTI_EVENT_EXCEPTION:
      GET_OFFSET (Exception);
      break;

    case JVMTI_EVENT_EXCEPTION_CATCH:
      GET_OFFSET (ExceptionCatch);
      break;

    case JVMTI_EVENT_SINGLE_STEP:
      GET_OFFSET (SingleStep);
      break;

    case JVMTI_EVENT_FRAME_POP:
      GET_OFFSET (FramePop);
      break;

    case JVMTI_EVENT_BREAKPOINT:
      GET_OFFSET (Breakpoint);
      break;

    case JVMTI_EVENT_FIELD_ACCESS:
      GET_OFFSET (FieldAccess);
      break;

    case JVMTI_EVENT_FIELD_MODIFICATION:
      GET_OFFSET (FieldModification);
      break;

    case JVMTI_EVENT_METHOD_ENTRY:
      GET_OFFSET (MethodEntry);
      break;

    case JVMTI_EVENT_METHOD_EXIT:
      GET_OFFSET (MethodExit);
      break;

    case JVMTI_EVENT_NATIVE_METHOD_BIND:
      GET_OFFSET (NativeMethodBind);
      break;

    case JVMTI_EVENT_COMPILED_METHOD_LOAD:
      GET_OFFSET (CompiledMethodLoad);
      break;

    case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
      GET_OFFSET (CompiledMethodUnload);
      break;

    case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
      GET_OFFSET (DynamicCodeGenerated);
      break;

    case JVMTI_EVENT_DATA_DUMP_REQUEST:
      GET_OFFSET (DataDumpRequest);
      break;

    case JVMTI_EVENT_MONITOR_WAIT:
      GET_OFFSET (MonitorWait);
      break;

    case JVMTI_EVENT_MONITOR_WAITED:
      GET_OFFSET (MonitorWaited);
      break;

    case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
      GET_OFFSET (MonitorContendedEnter);
      break;

    case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
      GET_OFFSET (MonitorContendedEntered);
      break;

    case JVMTI_EVENT_GARBAGE_COLLECTION_START:
      GET_OFFSET (GarbageCollectionStart);
      break;

    case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
      GET_OFFSET (GarbageCollectionFinish);
      break;

    case JVMTI_EVENT_OBJECT_FREE:
      GET_OFFSET (ObjectFree);
      break;

    case JVMTI_EVENT_VM_OBJECT_ALLOC:
      GET_OFFSET (VMObjectAlloc);
      break;

    default:
      fprintf (stderr,
	       "libgcj: check_enabled_event for unknown JVMTI event (%d)\n",
	       (int) type);
      return;
    }
#undef GET_OFFSET

  int index = EVENT_INDEX (type); // safe since caller checks this

1684
  if (_jvmtiEnvironments != NULL)
1685
    {
1686 1687 1688
      _envListLock->readLock ()->lock ();
      struct jvmti_env_list *e;
      FOREACH_ENVIRONMENT (e)
1689
	{
1690 1691 1692 1693 1694 1695 1696 1697 1698
	  char *addr
	    = reinterpret_cast<char *> (&e->env->callbacks) + offset;
	  void **callback = reinterpret_cast<void **> (addr);
	  if (e->env->enabled[index] && *callback != NULL)
	    {
	      *enabled = true;
	      _envListLock->readLock ()->unlock ();
	      return;
	    }
1699
	}
1700 1701

      _envListLock->readLock ()->unlock ();
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
    }

  *enabled = false;
}

static void
check_enabled_events ()
{
  check_enabled_event (JVMTI_EVENT_VM_INIT);
  check_enabled_event (JVMTI_EVENT_VM_DEATH);
  check_enabled_event (JVMTI_EVENT_THREAD_START);
  check_enabled_event (JVMTI_EVENT_THREAD_END);
  check_enabled_event (JVMTI_EVENT_CLASS_FILE_LOAD_HOOK);
  check_enabled_event (JVMTI_EVENT_CLASS_LOAD);
  check_enabled_event (JVMTI_EVENT_CLASS_PREPARE);
  check_enabled_event (JVMTI_EVENT_VM_START);
  check_enabled_event (JVMTI_EVENT_EXCEPTION);
  check_enabled_event (JVMTI_EVENT_EXCEPTION_CATCH);
  check_enabled_event (JVMTI_EVENT_SINGLE_STEP);
  check_enabled_event (JVMTI_EVENT_FRAME_POP);
  check_enabled_event (JVMTI_EVENT_BREAKPOINT);
  check_enabled_event (JVMTI_EVENT_FIELD_ACCESS);
  check_enabled_event (JVMTI_EVENT_FIELD_MODIFICATION);
  check_enabled_event (JVMTI_EVENT_METHOD_ENTRY);
  check_enabled_event (JVMTI_EVENT_METHOD_EXIT);
  check_enabled_event (JVMTI_EVENT_NATIVE_METHOD_BIND);
  check_enabled_event (JVMTI_EVENT_COMPILED_METHOD_LOAD);
  check_enabled_event (JVMTI_EVENT_COMPILED_METHOD_UNLOAD);
  check_enabled_event (JVMTI_EVENT_DYNAMIC_CODE_GENERATED);
  check_enabled_event (JVMTI_EVENT_DATA_DUMP_REQUEST);
  check_enabled_event (JVMTI_EVENT_MONITOR_WAIT);
  check_enabled_event (JVMTI_EVENT_MONITOR_WAITED);
  check_enabled_event (JVMTI_EVENT_MONITOR_CONTENDED_ENTER);
  check_enabled_event (JVMTI_EVENT_MONITOR_CONTENDED_ENTERED);
  check_enabled_event (JVMTI_EVENT_GARBAGE_COLLECTION_START);
  check_enabled_event (JVMTI_EVENT_GARBAGE_COLLECTION_FINISH);
  check_enabled_event (JVMTI_EVENT_OBJECT_FREE);
  check_enabled_event (JVMTI_EVENT_VM_OBJECT_ALLOC);
}

static jvmtiError JNICALL
_Jv_JVMTI_SetEventNotificationMode (jvmtiEnv *env, jvmtiEventMode mode,
				    jvmtiEvent type, jthread event_thread, ...)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);

  if (event_thread != NULL)
    {
1750 1751
      THREAD_CHECK_VALID (event_thread);
      THREAD_CHECK_IS_ALIVE (event_thread);
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
    }

  bool enabled;
  switch (mode)
    {
    case JVMTI_DISABLE:
      enabled = false;
      break;
    case JVMTI_ENABLE:
      enabled = true;
      break;

    default:
      return JVMTI_ERROR_ILLEGAL_ARGUMENT;
    }

  switch (type)
    {
    case JVMTI_EVENT_VM_INIT:
    case JVMTI_EVENT_VM_DEATH:
    case JVMTI_EVENT_THREAD_START:
    case JVMTI_EVENT_VM_START:
    case JVMTI_EVENT_COMPILED_METHOD_LOAD:
    case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
    case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
    case JVMTI_EVENT_DATA_DUMP_REQUEST:
      ILLEGAL_ARGUMENT (event_thread != NULL);
      break;

    case JVMTI_EVENT_THREAD_END:
    case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
    case JVMTI_EVENT_CLASS_LOAD:
    case JVMTI_EVENT_CLASS_PREPARE:
    case JVMTI_EVENT_EXCEPTION:
    case JVMTI_EVENT_EXCEPTION_CATCH:
    case JVMTI_EVENT_SINGLE_STEP:
    case JVMTI_EVENT_FRAME_POP:
    case JVMTI_EVENT_BREAKPOINT:
    case JVMTI_EVENT_FIELD_ACCESS:
    case JVMTI_EVENT_FIELD_MODIFICATION:
    case JVMTI_EVENT_METHOD_ENTRY:
    case JVMTI_EVENT_METHOD_EXIT:
    case JVMTI_EVENT_NATIVE_METHOD_BIND:
    case JVMTI_EVENT_MONITOR_WAIT:
    case JVMTI_EVENT_MONITOR_WAITED:
    case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
    case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
    case JVMTI_EVENT_GARBAGE_COLLECTION_START:
    case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
    case JVMTI_EVENT_OBJECT_FREE:
    case JVMTI_EVENT_VM_OBJECT_ALLOC:
      break;

    default:
      return JVMTI_ERROR_INVALID_EVENT_TYPE;
    }

  env->thread[EVENT_INDEX(type)] = event_thread;
  env->enabled[EVENT_INDEX(type)] = enabled;
  check_enabled_event (type);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_SetEventCallbacks (jvmtiEnv *env,
			     const jvmtiEventCallbacks *callbacks,
			     jint size_of_callbacks)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_ONLOAD | JVMTI_PHASE_LIVE);
  ILLEGAL_ARGUMENT (size_of_callbacks < 0);

  // Copy the list of callbacks into the environment
  memcpy (&env->callbacks, callbacks, sizeof (jvmtiEventCallbacks));

  /* Check which events are now enabeld (JVMTI makes no requirements
     about the order in which SetEventCallbacks and SetEventNotifications
     are called. So we must check all events here. */
  check_enabled_events ();

  return JVMTI_ERROR_NONE;
}

1834
static jvmtiError JNICALL
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
_Jv_JVMTI_GetErrorName (MAYBE_UNUSED jvmtiEnv *env, jvmtiError error,
			char **name_ptr)
{
  NULL_CHECK (name_ptr);

  const char *name;
  switch (error)
    {
    case JVMTI_ERROR_NONE:
      name = "none";
      break;

    case JVMTI_ERROR_NULL_POINTER:
      name = "null pointer";
      break;

    case JVMTI_ERROR_OUT_OF_MEMORY:
      name = "out of memory";
      break;

    case JVMTI_ERROR_ACCESS_DENIED:
      name = "access denied";
      break;

    case JVMTI_ERROR_WRONG_PHASE:
      name = "wrong phase";
      break;

    case JVMTI_ERROR_INTERNAL:
      name = "internal error";
      break;

    case JVMTI_ERROR_UNATTACHED_THREAD:
      name = "unattached thread";
      break;

    case JVMTI_ERROR_INVALID_ENVIRONMENT:
      name = "invalid environment";
      break;

    case JVMTI_ERROR_INVALID_PRIORITY:
      name = "invalid priority";
      break;

    case JVMTI_ERROR_THREAD_NOT_SUSPENDED:
      name = "thread not suspended";
      break;

    case JVMTI_ERROR_THREAD_SUSPENDED:
      name = "thread suspended";
      break;

    case JVMTI_ERROR_THREAD_NOT_ALIVE:
      name = "thread not alive";
      break;

    case JVMTI_ERROR_CLASS_NOT_PREPARED:
      name = "class not prepared";
      break;

    case JVMTI_ERROR_NO_MORE_FRAMES:
      name = "no more frames";
      break;

    case JVMTI_ERROR_OPAQUE_FRAME:
      name = "opaque frame";
      break;

    case JVMTI_ERROR_DUPLICATE:
      name = "duplicate";
      break;

    case JVMTI_ERROR_NOT_FOUND:
      name = "not found";
      break;

    case JVMTI_ERROR_NOT_MONITOR_OWNER:
      name = "not monitor owner";
      break;

    case JVMTI_ERROR_INTERRUPT:
      name = "interrupted";
      break;

    case JVMTI_ERROR_UNMODIFIABLE_CLASS:
      name = "unmodifiable class";
      break;

    case JVMTI_ERROR_NOT_AVAILABLE:
      name = "not available";
      break;

    case JVMTI_ERROR_ABSENT_INFORMATION:
      name = "absent information";
      break;

    case JVMTI_ERROR_INVALID_EVENT_TYPE:
      name = "invalid event type";
      break;

    case JVMTI_ERROR_NATIVE_METHOD:
      name = "native method";
      break;

    case JVMTI_ERROR_INVALID_THREAD:
      name = "invalid thread";
      break;

    case JVMTI_ERROR_INVALID_THREAD_GROUP:
      name = "invalid thread group";
      break;

    case JVMTI_ERROR_INVALID_OBJECT:
      name = "invalid object";
      break;

    case JVMTI_ERROR_INVALID_CLASS:
      name = "invalid class";
      break;

    case JVMTI_ERROR_INVALID_METHODID:
      name = "invalid method ID";
      break;

    case JVMTI_ERROR_INVALID_LOCATION:
      name = "invalid location";
      break;

    case JVMTI_ERROR_INVALID_FIELDID:
      name = "invalid field ID";
      break;

    case JVMTI_ERROR_TYPE_MISMATCH:
      name = "type mismatch";
      break;

    case JVMTI_ERROR_INVALID_SLOT:
      name = "invalid slot";
      break;

    case JVMTI_ERROR_INVALID_MONITOR:
      name = "invalid monitor";
      break;

    case JVMTI_ERROR_INVALID_CLASS_FORMAT:
      name = "invalid class format";
      break;

    case JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION:
      name = "circular class definition";
      break;

    case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED:
      name = "unsupported redefinition: method added";
      break;

    case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED:
      name = "unsupported redefinition: schema changed";
      break;

    case JVMTI_ERROR_INVALID_TYPESTATE:
      name = "invalid type state";
      break;

    case JVMTI_ERROR_FAILS_VERIFICATION:
      name = "fails verification";
      break;

    case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED:
      name = "unsupported redefinition: hierarchy changed";
      break;

    case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED:
      name = "unsupported redefinition: method deleted";
      break;

    case JVMTI_ERROR_UNSUPPORTED_VERSION:
      name = "unsupported version";
      break;

    case JVMTI_ERROR_NAMES_DONT_MATCH:
      name = "names do not match";
      break;

    case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED:
      name = "unsupported redefinition: class modifiers changed";
      break;

    case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED:
      name = "unsupported redefinition: method modifiers changed";
      break;

    case JVMTI_ERROR_MUST_POSSESS_CAPABILITY:
      name = "must possess capability";
      break;

    case JVMTI_ERROR_ILLEGAL_ARGUMENT:
      name = "illegal argument";
      break;

    default:
      return JVMTI_ERROR_ILLEGAL_ARGUMENT;
    }

  *name_ptr = (char *) _Jv_MallocUnchecked (strlen (name) + 1);
  if (*name_ptr == NULL)
    return JVMTI_ERROR_OUT_OF_MEMORY;

  strcpy (*name_ptr, name);
  return JVMTI_ERROR_NONE;
}

2047 2048 2049
#define RESERVED NULL
#define UNIMPLEMENTED NULL

2050 2051 2052
struct _Jv_jvmtiEnv _Jv_JVMTI_Interface =
{
  RESERVED,			// reserved1
2053
  _Jv_JVMTI_SetEventNotificationMode, // SetEventNotificationMode
2054
  RESERVED,			// reserved3
2055
  _Jv_JVMTI_GetAllThreads,		// GetAllThreads
2056 2057
  _Jv_JVMTI_SuspendThread,	// SuspendThread
  _Jv_JVMTI_ResumeThread,	// ResumeThread
2058
  UNIMPLEMENTED,		// StopThread
2059
  _Jv_JVMTI_InterruptThread,	// InterruptThread
2060 2061 2062 2063 2064 2065 2066
  UNIMPLEMENTED,		// GetThreadInfo
  UNIMPLEMENTED,		// GetOwnedMonitorInfo
  UNIMPLEMENTED,		// GetCurrentContendedMonitor
  UNIMPLEMENTED,		// RunAgentThread
  UNIMPLEMENTED,		// GetTopThreadGroups
  UNIMPLEMENTED,		// GetThreadGroupInfo
  UNIMPLEMENTED,		// GetThreadGroupChildren
2067
  _Jv_JVMTI_GetFrameCount,		// GetFrameCount
2068
  _Jv_JVMTI_GetThreadState,	// GetThreadState
2069 2070 2071
  RESERVED,			// reserved18
  UNIMPLEMENTED,		// GetFrameLocation
  UNIMPLEMENTED,		// NotifyPopFrame
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
  _Jv_JVMTI_GetLocalObject,		// GetLocalObject
  _Jv_JVMTI_GetLocalInt,		// GetLocalInt
  _Jv_JVMTI_GetLocalLong,		// GetLocalLong
  _Jv_JVMTI_GetLocalFloat,		// GetLocalFloat
  _Jv_JVMTI_GetLocalDouble,		// GetLocalDouble
  _Jv_JVMTI_SetLocalObject,		// SetLocalObject
  _Jv_JVMTI_SetLocalInt,		// SetLocalInt
  _Jv_JVMTI_SetLocalLong,		// SetLocalLong
  _Jv_JVMTI_SetLocalFloat,		// SetLocalFloat
  _Jv_JVMTI_SetLocalDouble,		// SetLocalDouble
2082 2083 2084 2085 2086 2087 2088
  _Jv_JVMTI_CreateRawMonitor,	// CreateRawMonitor
  _Jv_JVMTI_DestroyRawMonitor,	// DestroyRawMonitor
  _Jv_JVMTI_RawMonitorEnter,	// RawMonitorEnter
  _Jv_JVMTI_RawMonitorExit,	// RawMonitorExit
  _Jv_JVMTI_RawMonitorWait,	// RawMonitorWait
  _Jv_JVMTI_RawMonitorNotify,	// RawMonitorNotify
  _Jv_JVMTI_RawMonitorNotifyAll, // RawMonitorNotifyAll
Keith Seitz committed
2089 2090
  _Jv_JVMTI_SetBreakpoint,	// SetBreakpoint
  _Jv_JVMTI_ClearBreakpoint,	// ClearBreakpoint
2091 2092 2093 2094 2095 2096
  RESERVED,			// reserved40
  UNIMPLEMENTED,		// SetFieldAccessWatch
  UNIMPLEMENTED,		// ClearFieldAccessWatch
  UNIMPLEMENTED,		// SetFieldModificationWatch
  UNIMPLEMENTED,		// ClearFieldModificationWatch
  RESERVED,			// reserved45
2097 2098
  _Jv_JVMTI_Allocate,		// Allocate
  _Jv_JVMTI_Deallocate,		// Deallocate
2099
  UNIMPLEMENTED,		// GetClassSignature
2100
  _Jv_JVMTI_GetClassStatus,	// GetClassStatus
2101
  UNIMPLEMENTED,		// GetSourceFileName
2102 2103
  _Jv_JVMTI_GetClassModifiers,	// GetClassModifiers
  _Jv_JVMTI_GetClassMethods,	// GetClassMethods
2104 2105
  UNIMPLEMENTED,		// GetClassFields
  UNIMPLEMENTED,		// GetImplementedInterfaces
2106 2107 2108 2109
  _Jv_JVMTI_IsInterface,	// IsInterface
  _Jv_JVMTI_IsArrayClass,	// IsArrayClass
  _Jv_JVMTI_GetClassLoader,	// GetClassLoader
  _Jv_JVMTI_GetObjectHashCode,	// GetObjectHashCode
2110 2111 2112
  UNIMPLEMENTED,		// GetObjectMonitorUsage
  UNIMPLEMENTED,		// GetFieldName
  UNIMPLEMENTED,		// GetFieldDeclaringClass
2113 2114
  _Jv_JVMTI_GetFieldModifiers,	// GetFieldModifiers
  _Jv_JVMTI_IsFieldSynthetic,	// IsFieldSynthetic
2115
  _Jv_JVMTI_GetMethodName,	// GetMethodName
2116
  _Jv_JVMTI_GetMethodDeclaringClass,  // GetMethodDeclaringClass
2117
  _Jv_JVMTI_GetMethodModifiers,	// GetMethodModifers
2118
  RESERVED,			// reserved67
2119
  _Jv_JVMTI_GetMaxLocals,		// GetMaxLocals
2120
  _Jv_JVMTI_GetArgumentsSize,		// GetArgumentsSize
2121
  _Jv_JVMTI_GetLineNumberTable,	// GetLineNumberTable
2122
  UNIMPLEMENTED,		// GetMethodLocation
2123
  _Jv_JVMTI_GetLocalVariableTable,		// GetLocalVariableTable
2124 2125 2126
  RESERVED,			// reserved73
  RESERVED,			// reserved74
  UNIMPLEMENTED,		// GetBytecodes
2127 2128
  _Jv_JVMTI_IsMethodNative,	// IsMethodNative
  _Jv_JVMTI_IsMethodSynthetic,	// IsMethodSynthetic
2129
  UNIMPLEMENTED,		// GetLoadedClasses
2130
  _Jv_JVMTI_GetClassLoaderClasses, // GetClassLoaderClasses
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
  UNIMPLEMENTED,		// PopFrame
  RESERVED,			// reserved81
  RESERVED,			// reserved82
  RESERVED,			// reserved83
  RESERVED,			// reserved84
  RESERVED,			// reserved85
  RESERVED,			// reserved86
  UNIMPLEMENTED,		// RedefineClasses
  UNIMPLEMENTED,		// GetVersionNumber
  UNIMPLEMENTED,		// GetCapabilities
  UNIMPLEMENTED,		// GetSourceDebugExtension
  UNIMPLEMENTED,		// IsMethodObsolete
  UNIMPLEMENTED,		// SuspendThreadList
  UNIMPLEMENTED,		// ResumeThreadList
  RESERVED,			// reserved94
  RESERVED,			// reserved95
  RESERVED,			// reserved96
  RESERVED,			// reserved97
  RESERVED,			// reserved98
  RESERVED,			// reserved99
  UNIMPLEMENTED,		// GetAllStackTraces
  UNIMPLEMENTED,		// GetThreadListStackTraces
  UNIMPLEMENTED,		// GetThreadLocalStorage
  UNIMPLEMENTED,		// SetThreadLocalStorage
2155
  _Jv_JVMTI_GetStackTrace,		// GetStackTrace
2156 2157 2158
  RESERVED,			// reserved105
  UNIMPLEMENTED,		// GetTag
  UNIMPLEMENTED,		// SetTag
2159
  _Jv_JVMTI_ForceGarbageCollection, // ForceGarbageCollection
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
  UNIMPLEMENTED,		// IterateOverObjectsReachable
  UNIMPLEMENTED,		// IterateOverReachableObjects
  UNIMPLEMENTED,		// IterateOverHeap
  UNIMPLEMENTED,		// IterateOverInstanceOfClass
  RESERVED,			// reserved113
  UNIMPLEMENTED,		// GetObjectsWithTags
  RESERVED,			// reserved115
  RESERVED,			// reserved116
  RESERVED,			// reserved117
  RESERVED,			// reserved118
  RESERVED,			// reserved119
2171 2172
  _Jv_JVMTI_SetJNIFunctionTable, // SetJNIFunctionTable
  _Jv_JVMTI_GetJNIFunctionTable, // GetJNIFunctionTable
2173
  _Jv_JVMTI_SetEventCallbacks,	// SetEventCallbacks
2174 2175 2176 2177
  UNIMPLEMENTED,		// GenerateEvents
  UNIMPLEMENTED,		// GetExtensionFunctions
  UNIMPLEMENTED,		// GetExtensionEvents
  UNIMPLEMENTED,		// SetExtensionEventCallback
2178
  _Jv_JVMTI_DisposeEnvironment,	// DisposeEnvironment
2179
  _Jv_JVMTI_GetErrorName,	// GetErrorName
2180 2181
  UNIMPLEMENTED,		// GetJLocationFormat
  UNIMPLEMENTED,		// GetSystemProperties
2182 2183
  _Jv_JVMTI_GetSystemProperty,	// GetSystemProperty
  _Jv_JVMTI_SetSystemProperty,	// SetSystemProperty
2184 2185 2186 2187 2188 2189
  UNIMPLEMENTED,		// GetPhase
  UNIMPLEMENTED,		// GetCurrentThreadCpuTimerInfo
  UNIMPLEMENTED,		// GetCurrentThreadCpuTime
  UNIMPLEMENTED,		// GetThreadCpuTimerInfo
  UNIMPLEMENTED,		// GetThreadCpuTime
  UNIMPLEMENTED,		// GetTimerInfo
2190
  _Jv_JVMTI_GetTime,		// GetTime
2191 2192 2193 2194
  UNIMPLEMENTED,		// GetPotentialCapabilities
  RESERVED,			// reserved141
  UNIMPLEMENTED,		// AddCapabilities
  UNIMPLEMENTED,		// RelinquishCapabilities
2195
  _Jv_JVMTI_GetAvailableProcessors, // GetAvailableProcessors
2196 2197 2198 2199
  RESERVED,			// reserved145
  RESERVED,			// reserved146
  UNIMPLEMENTED,		// GetEnvironmentLocalStorage
  UNIMPLEMENTED,		// SetEnvironmentLocalStorage
2200 2201
  _Jv_JVMTI_AddToBootstrapClassLoaderSearch, // AddToBootstrapClassLoaderSearch
  _Jv_JVMTI_SetVerboseFlag,	// SetVerboseFlag
2202 2203 2204
  RESERVED,			// reserved151
  RESERVED,			// reserved152
  RESERVED,			// reserved153
2205
  _Jv_JVMTI_GetObjectSize	// GetObjectSize
2206 2207 2208 2209 2210 2211 2212
};

_Jv_JVMTIEnv *
_Jv_GetJVMTIEnv (void)
{
  _Jv_JVMTIEnv *env
    = (_Jv_JVMTIEnv *) _Jv_MallocUnchecked (sizeof (_Jv_JVMTIEnv));
2213
  memset (env, 0, sizeof (_Jv_JVMTIEnv));
2214
  env->p = &_Jv_JVMTI_Interface;
2215 2216 2217 2218
  struct jvmti_env_list *element
    = (struct jvmti_env_list *) _Jv_MallocUnchecked (sizeof (struct jvmti_env_list));
  element->env = env;
  element->next = NULL;
2219

2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
  _envListLock->writeLock ()->lock ();
  if (_jvmtiEnvironments == NULL)
    _jvmtiEnvironments = element;
  else
    {
      struct jvmti_env_list *e;
      for (e = _jvmtiEnvironments; e->next != NULL; e = e->next)
	;
      e->next = element;
    }
  _envListLock->writeLock ()->unlock ();
2231

2232 2233 2234 2235
  /* Mark JVMTI active. This is used to force the interpreter
     to use either debugging or non-debugging code. Once JVMTI
     has been enabled, the non-debug interpreter cannot be used. */
  JVMTI::enabled = true;
2236 2237
  return env;
}
2238 2239 2240 2241 2242

void
_Jv_JVMTI_Init ()
{
  _jvmtiEnvironments = NULL;
2243 2244
  _envListLock
    = new java::util::concurrent::locks::ReentrantReadWriteLock ();
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607

  // No environments, so this should set all JVMTI:: members to false
  check_enabled_events ();
}

static void
post_event (jvmtiEnv *env, jvmtiEvent type, jthread event_thread, va_list args)
{
#define ARG(Type,Name) Type Name = (Type) va_arg (args, Type)

#define GET_BOOLEAN_ARG(Name)			\
  ARG (int, b);					\
  jboolean Name = (b == 0) ? false : true

#define GET_CHAR_ARG(Name)			\
  ARG (int, c);					\
  char Name = static_cast<char> (c)

  switch (type)
    {
    case JVMTI_EVENT_VM_INIT:
      if (env->callbacks.VMInit != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  env->callbacks.VMInit (env, jni_env, event_thread);
	}
      break;

    case JVMTI_EVENT_VM_DEATH:
      if (env->callbacks.VMDeath != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  env->callbacks.VMDeath (env, jni_env);
	}
      break;

    case JVMTI_EVENT_THREAD_START:
      if (env->callbacks.ThreadStart != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  env->callbacks.ThreadStart (env, jni_env, event_thread);
	}
      break;

    case JVMTI_EVENT_THREAD_END:
      if (env->callbacks.ThreadEnd != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  env->callbacks.ThreadEnd (env, jni_env, event_thread);
	}
      break;

    case JVMTI_EVENT_CLASS_FILE_LOAD_HOOK:
      if (env->callbacks.ClassFileLoadHook != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jclass, class_being_redefined);
	  ARG (jobject, loader);
	  ARG (const char *, name);
	  ARG (jobject, protection_domain);
	  ARG (jint, class_data_len);
	  ARG (const unsigned char *, class_data);
	  ARG (jint *, new_class_data_len);
	  ARG (unsigned char **, new_class_data);
	  env->callbacks.ClassFileLoadHook (env, jni_env,
					    class_being_redefined, loader,
					    name, protection_domain,
					    class_data_len, class_data,
					    new_class_data_len,
					    new_class_data);
	}
      break;

    case JVMTI_EVENT_CLASS_LOAD:
      if (env->callbacks.ClassLoad != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jclass, klass);
	  env->callbacks.ClassLoad (env, jni_env, event_thread, klass);
	}
      break;

    case JVMTI_EVENT_CLASS_PREPARE:
      if (env->callbacks.ClassPrepare != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jclass, klass);
	  env->callbacks.ClassPrepare (env, jni_env, event_thread, klass);
	}
      break;

    case JVMTI_EVENT_VM_START:
      if (env->callbacks.VMStart != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  env->callbacks.VMStart (env, jni_env);
	}
      break;

    case JVMTI_EVENT_EXCEPTION:
      if (env->callbacks.Exception != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  ARG (jlocation, location);
	  ARG (jobject, exception);
	  ARG (jmethodID, catch_method);
	  ARG (jlocation, catch_location);
	  env->callbacks.Exception (env, jni_env, event_thread, method,
				    location, exception, catch_method,
				    catch_location);
	}
      break;

    case JVMTI_EVENT_EXCEPTION_CATCH:
      if (env->callbacks.ExceptionCatch != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  ARG (jlocation, location);
	  ARG (jobject, exception);
	  env->callbacks.ExceptionCatch (env, jni_env, event_thread, method,
					 location, exception);
	}
      break;

    case JVMTI_EVENT_SINGLE_STEP:
      if (env->callbacks.SingleStep != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  ARG (jlocation, location);
	  env->callbacks.SingleStep (env, jni_env, event_thread, method,
				     location);
	}
      break;

    case JVMTI_EVENT_FRAME_POP:
      if (env->callbacks.FramePop != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  GET_BOOLEAN_ARG (was_popped_by_exception);
	  env->callbacks.FramePop (env, jni_env, event_thread, method,
				   was_popped_by_exception);
	}
      break;

    case JVMTI_EVENT_BREAKPOINT:
      if (env->callbacks.Breakpoint != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  ARG (jlocation, location);
	  env->callbacks.Breakpoint (env, jni_env, event_thread, method,
				     location);
	}
      break;

    case JVMTI_EVENT_FIELD_ACCESS:
      if (env->callbacks.FieldAccess != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  ARG (jlocation, location);
	  ARG (jclass, field_class);
	  ARG (jobject, object);
	  ARG (jfieldID, field);
	  env->callbacks.FieldAccess (env, jni_env, event_thread, method,
				      location, field_class, object, field);
	}
      break;

    case JVMTI_EVENT_FIELD_MODIFICATION:
      if (env->callbacks.FieldModification != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  ARG (jlocation, location);
	  ARG (jclass, field_class);
	  ARG (jobject, object);
	  ARG (jfieldID, field);
	  GET_CHAR_ARG (signature_type);
	  ARG (jvalue, new_value);
	  env->callbacks.FieldModification (env, jni_env, event_thread, method,
					    location, field_class, object,
					    field, signature_type, new_value);
	}
      break;

    case JVMTI_EVENT_METHOD_ENTRY:
      if (env->callbacks.MethodEntry != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  env->callbacks.MethodEntry (env, jni_env, event_thread, method);
	}
      break;

    case JVMTI_EVENT_METHOD_EXIT:
      if (env->callbacks.MethodExit != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  GET_BOOLEAN_ARG (was_popped_by_exception);
	  ARG (jvalue, return_value);
	  env->callbacks.MethodExit (env, jni_env, event_thread, method,
				     was_popped_by_exception, return_value);
	}
      break;

    case JVMTI_EVENT_NATIVE_METHOD_BIND:
      if (env->callbacks.NativeMethodBind != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jmethodID, method);
	  ARG (void *, address);
	  ARG (void **, new_address_ptr);
	  env->callbacks.NativeMethodBind (env, jni_env, event_thread, method,
					   address, new_address_ptr);
	}
      break;

    case JVMTI_EVENT_COMPILED_METHOD_LOAD:
      if (env->callbacks.CompiledMethodLoad != NULL)
	{
	  ARG (jmethodID, method);
	  ARG (jint, code_size);
	  ARG (const void *, code_addr);
	  ARG (jint, map_length);
	  ARG (const jvmtiAddrLocationMap *, map);
	  ARG (const void *, compile_info);
	  env->callbacks.CompiledMethodLoad (env, method, code_size, code_addr,
					     map_length, map, compile_info);
	}
      break;

    case JVMTI_EVENT_COMPILED_METHOD_UNLOAD:
      if (env->callbacks.CompiledMethodUnload != NULL)
	{
	  ARG (jmethodID, method);
	  ARG (const void *, code_addr);
	  env->callbacks.CompiledMethodUnload (env, method, code_addr);
	}
      break;

    case JVMTI_EVENT_DYNAMIC_CODE_GENERATED:
      if (env->callbacks.DynamicCodeGenerated != NULL)
	{
	  ARG (const char *, name);
	  ARG (const void *, address);
	  ARG (jint, length);
	  env->callbacks.DynamicCodeGenerated (env, name, address, length);
	}
      break;

    case JVMTI_EVENT_DATA_DUMP_REQUEST:
      if (env->callbacks.DataDumpRequest != NULL)
	{
	  env->callbacks.DataDumpRequest (env);
	}
      break;

    case JVMTI_EVENT_MONITOR_WAIT:
      if (env->callbacks.MonitorWait != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jobject, object);
	  ARG (jlong, timeout);
	  env->callbacks.MonitorWait (env, jni_env, event_thread, object,
				      timeout);
	}
      break;

    case JVMTI_EVENT_MONITOR_WAITED:
      if (env->callbacks.MonitorWaited != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jobject, object);
	  GET_BOOLEAN_ARG (timed_out);
	  env->callbacks.MonitorWaited (env, jni_env, event_thread, object,
					timed_out);
	}
      break;

    case JVMTI_EVENT_MONITOR_CONTENDED_ENTER:
      if (env->callbacks.MonitorContendedEnter != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jobject, object);
	  env->callbacks.MonitorContendedEnter (env, jni_env, event_thread,
						object);
	}
      break;

    case JVMTI_EVENT_MONITOR_CONTENDED_ENTERED:
      if (env->callbacks.MonitorContendedEntered != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jobject, object);
	  env->callbacks.MonitorContendedEntered (env, jni_env, event_thread,
						  object);
	}
      break;

    case JVMTI_EVENT_GARBAGE_COLLECTION_START:
      if (env->callbacks.GarbageCollectionStart != NULL)
	{
	  env->callbacks.GarbageCollectionStart (env);
	}
      break;

    case JVMTI_EVENT_GARBAGE_COLLECTION_FINISH:
      if (env->callbacks.GarbageCollectionFinish != NULL)
	{
	  env->callbacks.GarbageCollectionFinish (env);
	}
      break;

    case JVMTI_EVENT_OBJECT_FREE:
      if (env->callbacks.ObjectFree != NULL)
	{
	  ARG (jlong, tag);
	  env->callbacks.ObjectFree (env, tag);
	}
      break;

    case JVMTI_EVENT_VM_OBJECT_ALLOC:
      if (env->callbacks.VMObjectAlloc != NULL)
	{
	  ARG (JNIEnv *, jni_env);
	  ARG (jobject, object);
	  ARG (jclass, object_class);
	  ARG (jlong, size);
	  env->callbacks.VMObjectAlloc (env, jni_env, event_thread,
					object, object_class, size);
	}
      break;

    default:
      fprintf (stderr, "libgcj: post of unknown JVMTI event (%d)\n",
	       (int) type);
      break;
    }
  va_end (args);
#undef ARG
#undef GET_BOOLEAN_ARG
#undef GET_CHAR_ARG
}

/* Post an event to requesting JVMTI environments
 *
 * This function should not be called without consulting the
 * JVMTI_REQUESTED_EVENT macro first (for speed). It does no real
 * harm (other than kill speed), since this function will still
 * only send the event if it was properly requested by an environment.
 */ 
void
_Jv_JVMTI_PostEvent (jvmtiEvent type, jthread event_thread, ...)
{
  va_list args;
  va_start (args, event_thread);

2608
  _envListLock->readLock ()->lock ();
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
  struct jvmti_env_list *e;
  FOREACH_ENVIRONMENT (e)
    {
      /* Events are only posted if the event was explicitly enabled,
	 it has a registered event handler, and the event thread
	 matches (either globally or restricted to a specific thread).
	 Here we check all but the event handler, which will be handled
	 by post_event. */
      if (e->env->enabled[EVENT_INDEX(type)]
	  && (e->env->thread[EVENT_INDEX(type)] == NULL
	      || e->env->thread[EVENT_INDEX(type)] == event_thread))
	{
	  post_event (e->env, type, event_thread, args);
	}
    }
2624
  _envListLock->readLock ()->unlock ();
2625
  va_end (args);
2626
}