aboutsummaryrefslogtreecommitdiff
path: root/libjava/jvmti.cc
blob: b18ac68dc789007cc7b0e4923280215166137ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
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
585
586
587
588
589
590
591
592
593
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
636
637
638
639
640
641
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
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
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
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
1061
// jvmti.cc - JVMTI implementation

/* Copyright (C) 2006 Free Software Foundation

   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>
#include <platform.h>

#include <jvm.h>
#include <java-threads.h>
#include <java-gc.h>
#include <jvmti.h>

#include <gcj/method.h>

#include <gnu/classpath/SystemProperties.h>
#include <gnu/gcj/runtime/BootClassLoader.h>
#include <java/lang/Class.h>
#include <java/lang/ClassLoader.h>
#include <java/lang/Object.h>
#include <java/lang/Thread.h>
#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>
#include <java/net/URL.h>

extern struct JNINativeInterface _Jv_JNIFunctions;

struct _Jv_rawMonitorID
{
  _Jv_Mutex_t mutex;
  _Jv_ConditionVariable_t condition;
};

/* 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;
static java::lang::Object *_envListLock = NULL;
#define FOREACH_ENVIRONMENT(Ele) \
  for (Ele = _jvmtiEnvironments; Ele != NULL; Ele = Ele->next)

// Some commonly-used checks

#define THREAD_DEFAULT_TO_CURRENT(Ajthread)		\
  do							\
    {							\
      if (Ajthread == NULL)				\
	Ajthread = java::lang::Thread::currentThread ();	\
    }							\
  while (0)

#define THREAD_CHECK_VALID(Athread)					\
  do									\
    {									\
      if (!java::lang::Thread::class$.isAssignableFrom (&(Athread->class$))) \
	return JVMTI_ERROR_INVALID_THREAD;				\
    }									\
  while (0)

#define THREAD_CHECK_IS_ALIVE(Athread)	     \
  do					     \
    {					     \
      if (!Athread->isAlive ())		     \
	return JVMTI_ERROR_THREAD_NOT_ALIVE; \
    }					     \
  while (0)

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

#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)

static jvmtiError JNICALL
_Jv_JVMTI_SuspendThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
{
  using namespace java::lang;

  THREAD_DEFAULT_TO_CURRENT (thread);
 
  Thread *t = reinterpret_cast<Thread *> (thread);
  THREAD_CHECK_VALID (t);
  THREAD_CHECK_IS_ALIVE (t);

  _Jv_Thread_t *data = _Jv_ThreadGetData (t);
  _Jv_SuspendThread (data);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_ResumeThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
{
  using namespace java::lang;

  THREAD_DEFAULT_TO_CURRENT (thread);

  Thread *t = reinterpret_cast<Thread *> (thread);
  THREAD_CHECK_VALID (t);
  THREAD_CHECK_IS_ALIVE (t);

  _Jv_Thread_t *data = _Jv_ThreadGetData (t);
  _Jv_ResumeThread (data);
  return JVMTI_ERROR_NONE;
}

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;

  Thread *real_thread = reinterpret_cast<Thread *> (thread);
  THREAD_CHECK_VALID (real_thread);
  THREAD_CHECK_IS_ALIVE (real_thread);
  real_thread->interrupt();
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_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);
  *result = (jrawMonitorID) _Jv_Malloc (sizeof (_Jv_rawMonitorID));
  _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
_Jv_JVMTI_RawMonitorWait (MAYBE_UNUSED jvmtiEnv *env, jrawMonitorID monitor,
			  jlong millis)
{
  if (monitor == NULL)
    return JVMTI_ERROR_INVALID_MONITOR;
  int r = _Jv_CondWait (&monitor->condition, &monitor->mutex, millis, 0);
  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
_Jv_JVMTI_Allocate (MAYBE_UNUSED jvmtiEnv *env, jlong size,
		    unsigned char **result)
{
  ILLEGAL_ARGUMENT (size < 0);
  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
_Jv_JVMTI_GetClassModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			     jint *mods)
{
  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
_Jv_JVMTI_GetClassMethods (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			   jint *count_ptr, jmethodID **methods_ptr)
{
  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);

  *methods_ptr = (jmethodID *) _Jv_Malloc (*count_ptr * sizeof (jmethodID));
  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
_Jv_JVMTI_IsInterface (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
		       jboolean *result)
{
  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
_Jv_JVMTI_IsArrayClass (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			jboolean *result)
{
  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
_Jv_JVMTI_GetClassLoader (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			  jobject *result)
{
  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
_Jv_JVMTI_GetObjectHashCode (MAYBE_UNUSED jvmtiEnv *env, jobject obj,
			     jint *result)
{
  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
_Jv_JVMTI_GetFieldModifiers (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			     jfieldID field, jint *result)
{
  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
_Jv_JVMTI_IsFieldSynthetic (MAYBE_UNUSED jvmtiEnv *env, jclass klass,
			    jfieldID field, jboolean *result)
{
  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
_Jv_JVMTI_GetMethodModifiers (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
			      jint *result)
{
  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
_Jv_JVMTI_IsMethodNative (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
			  jboolean *result)
{
  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
_Jv_JVMTI_IsMethodSynthetic (MAYBE_UNUSED jvmtiEnv *env, jmethodID method,
			     jboolean *result)
{
  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
_Jv_JVMTI_GetClassLoaderClasses (MAYBE_UNUSED jvmtiEnv *env,
				 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);
  jclass *result = (jclass *) _Jv_Malloc (*count_ptr * sizeof (jclass));
  // FIXME: JNI references...
  memcpy (result, elts, *count_ptr * sizeof (jclass));

  *result_ptr = result;

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_ForceGarbageCollection (MAYBE_UNUSED jvmtiEnv *env)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_LIVE);
  _Jv_RunGC();
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_SetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
			       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
_Jv_JVMTI_GetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
			       jniNativeInterface **function_table)
{
  REQUIRE_PHASE (env, JVMTI_PHASE_START | JVMTI_PHASE_LIVE);
  NULL_CHECK (function_table);
  *function_table
    = (jniNativeInterface *) _Jv_Malloc (sizeof (jniNativeInterface));
  memcpy (*function_table, &_Jv_JNIFunctions, sizeof (jniNativeInterface));
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_DisposeEnvironment (jvmtiEnv *env)
{
  NULL_CHECK (env);

  if (_jvmtiEnvironments == NULL)
    return JVMTI_ERROR_INVALID_ENVIRONMENT;
  else
    {
      JvSynchronize dummy (_envListLock);
      if (_jvmtiEnvironments->env == env)
	{
	  struct jvmti_env_list *next = _jvmtiEnvironments->next;
	  _Jv_Free (_jvmtiEnvironments);
	  _jvmtiEnvironments = next;
	}
      else
	{
	  struct jvmti_env_list *e = _jvmtiEnvironments; 
	  while (e->next != NULL && e->next->env != env)
	    e = e->next;
	  if (e->next == NULL)
	    return JVMTI_ERROR_INVALID_ENVIRONMENT;

	  struct jvmti_env_list *next = e->next->next;
	  _Jv_Free (e->next);
	  e->next = next;
	}
    }

  _Jv_Free (env);
  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_GetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
			     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);
  *result = (char *) _Jv_Malloc (len + 1);
  JvGetStringUTFRegion (result_str, 0, result_str->length(), *result);
  (*result)[len] = '\0';

  return JVMTI_ERROR_NONE;
}

static jvmtiError JNICALL
_Jv_JVMTI_SetSystemProperty (MAYBE_UNUSED jvmtiEnv *env, const char *property,
			     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
_Jv_JVMTI_AddToBootstrapClassLoaderSearch (MAYBE_UNUSED jvmtiEnv *env,
					   const char *segment)
{
  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
_Jv_JVMTI_GetObjectSize (MAYBE_UNUSED jvmtiEnv *env, jobject object,
			 jlong *result)
{
  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
	= (jint) (_Jv_uintptr_t) _Jv_GetArrayElementFromElementType(NULL,
								    klass->getComponentType());
      // 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;
}

jvmtiError
_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;
}

#define RESERVED NULL
#define UNIMPLEMENTED NULL

struct _Jv_jvmtiEnv _Jv_JVMTI_Interface =
{
  RESERVED,			// reserved1
  UNIMPLEMENTED,		// SetEventNotification
  RESERVED,			// reserved3
  UNIMPLEMENTED,		// GetAllThreads
  _Jv_JVMTI_SuspendThread,	// SuspendThread
  _Jv_JVMTI_ResumeThread,	// ResumeThread
  UNIMPLEMENTED,		// StopThread
  _Jv_JVMTI_InterruptThread,	// InterruptThread
  UNIMPLEMENTED,		// GetThreadInfo
  UNIMPLEMENTED,		// GetOwnedMonitorInfo
  UNIMPLEMENTED,		// GetCurrentContendedMonitor
  UNIMPLEMENTED,		// RunAgentThread
  UNIMPLEMENTED,		// GetTopThreadGroups
  UNIMPLEMENTED,		// GetThreadGroupInfo
  UNIMPLEMENTED,		// GetThreadGroupChildren
  UNIMPLEMENTED,		// GetFrameCount
  UNIMPLEMENTED,		// GetThreadState
  RESERVED,			// reserved18
  UNIMPLEMENTED,		// GetFrameLocation
  UNIMPLEMENTED,		// NotifyPopFrame
  UNIMPLEMENTED,		// GetLocalObject
  UNIMPLEMENTED,		// GetLocalInt
  UNIMPLEMENTED,		// GetLocalLong
  UNIMPLEMENTED,		// GetLocalFloat
  UNIMPLEMENTED,		// GetLocalDouble
  UNIMPLEMENTED,		// SetLocalObject
  UNIMPLEMENTED,		// SetLocalInt
  UNIMPLEMENTED,		// SetLocalLong
  UNIMPLEMENTED,		// SetLocalFloat
  UNIMPLEMENTED,		// SetLocalDouble
  _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
  UNIMPLEMENTED,		// SetBreakpoint
  UNIMPLEMENTED,		// ClearBreakpoint
  RESERVED,			// reserved40
  UNIMPLEMENTED,		// SetFieldAccessWatch
  UNIMPLEMENTED,		// ClearFieldAccessWatch
  UNIMPLEMENTED,		// SetFieldModificationWatch
  UNIMPLEMENTED,		// ClearFieldModificationWatch
  RESERVED,			// reserved45
  _Jv_JVMTI_Allocate,		// Allocate
  _Jv_JVMTI_Deallocate,		// Deallocate
  UNIMPLEMENTED,		// GetClassSignature
  UNIMPLEMENTED,		// GetClassStatus
  UNIMPLEMENTED,		// GetSourceFileName
  _Jv_JVMTI_GetClassModifiers,	// GetClassModifiers
  _Jv_JVMTI_GetClassMethods,	// GetClassMethods
  UNIMPLEMENTED,		// GetClassFields
  UNIMPLEMENTED,		// GetImplementedInterfaces
  _Jv_JVMTI_IsInterface,	// IsInterface
  _Jv_JVMTI_IsArrayClass,	// IsArrayClass
  _Jv_JVMTI_GetClassLoader,	// GetClassLoader
  _Jv_JVMTI_GetObjectHashCode,	// GetObjectHashCode
  UNIMPLEMENTED,		// GetObjectMonitorUsage
  UNIMPLEMENTED,		// GetFieldName
  UNIMPLEMENTED,		// GetFieldDeclaringClass
  _Jv_JVMTI_GetFieldModifiers,	// GetFieldModifiers
  _Jv_JVMTI_IsFieldSynthetic,	// IsFieldSynthetic
  UNIMPLEMENTED,		// GetMethodName
  UNIMPLEMENTED,		// GetMethodDeclaringClass
  _Jv_JVMTI_GetMethodModifiers,	// GetMethodModifers
  RESERVED,			// reserved67
  UNIMPLEMENTED,		// GetMaxLocals
  UNIMPLEMENTED,		// GetArgumentsSize
  UNIMPLEMENTED,		// GetLineNumberTable
  UNIMPLEMENTED,		// GetMethodLocation
  UNIMPLEMENTED,		// GetLocalVariableTable
  RESERVED,			// reserved73
  RESERVED,			// reserved74
  UNIMPLEMENTED,		// GetBytecodes
  _Jv_JVMTI_IsMethodNative,	// IsMethodNative
  _Jv_JVMTI_IsMethodSynthetic,	// IsMethodSynthetic
  UNIMPLEMENTED,		// GetLoadedClasses
  _Jv_JVMTI_GetClassLoaderClasses, // GetClassLoaderClasses
  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
  UNIMPLEMENTED,		// GetStackTrace
  RESERVED,			// reserved105
  UNIMPLEMENTED,		// GetTag
  UNIMPLEMENTED,		// SetTag
  _Jv_JVMTI_ForceGarbageCollection, // ForceGarbageCollection
  UNIMPLEMENTED,		// IterateOverObjectsReachable
  UNIMPLEMENTED,		// IterateOverReachableObjects
  UNIMPLEMENTED,		// IterateOverHeap
  UNIMPLEMENTED,		// IterateOverInstanceOfClass
  RESERVED,			// reserved113
  UNIMPLEMENTED,		// GetObjectsWithTags
  RESERVED,			// reserved115
  RESERVED,			// reserved116
  RESERVED,			// reserved117
  RESERVED,			// reserved118
  RESERVED,			// reserved119
  _Jv_JVMTI_SetJNIFunctionTable, // SetJNIFunctionTable
  _Jv_JVMTI_GetJNIFunctionTable, // GetJNIFunctionTable
  UNIMPLEMENTED,		// SetEventCallbacks
  UNIMPLEMENTED,		// GenerateEvents
  UNIMPLEMENTED,		// GetExtensionFunctions
  UNIMPLEMENTED,		// GetExtensionEvents
  UNIMPLEMENTED,		// SetExtensionEventCallback
  _Jv_JVMTI_DisposeEnvironment,	// DisposeEnvironment
  _Jv_JVMTI_GetErrorName,	// GetErrorName
  UNIMPLEMENTED,		// GetJLocationFormat
  UNIMPLEMENTED,		// GetSystemProperties
  _Jv_JVMTI_GetSystemProperty,	// GetSystemProperty
  _Jv_JVMTI_SetSystemProperty,	// SetSystemProperty
  UNIMPLEMENTED,		// GetPhase
  UNIMPLEMENTED,		// GetCurrentThreadCpuTimerInfo
  UNIMPLEMENTED,		// GetCurrentThreadCpuTime
  UNIMPLEMENTED,		// GetThreadCpuTimerInfo
  UNIMPLEMENTED,		// GetThreadCpuTime
  UNIMPLEMENTED,		// GetTimerInfo
  _Jv_JVMTI_GetTime,		// GetTime
  UNIMPLEMENTED,		// GetPotentialCapabilities
  RESERVED,			// reserved141
  UNIMPLEMENTED,		// AddCapabilities
  UNIMPLEMENTED,		// RelinquishCapabilities
  _Jv_JVMTI_GetAvailableProcessors, // GetAvailableProcessors
  RESERVED,			// reserved145
  RESERVED,			// reserved146
  UNIMPLEMENTED,		// GetEnvironmentLocalStorage
  UNIMPLEMENTED,		// SetEnvironmentLocalStorage
  _Jv_JVMTI_AddToBootstrapClassLoaderSearch, // AddToBootstrapClassLoaderSearch
  _Jv_JVMTI_SetVerboseFlag,	// SetVerboseFlag
  RESERVED,			// reserved151
  RESERVED,			// reserved152
  RESERVED,			// reserved153
  _Jv_JVMTI_GetObjectSize	// GetObjectSize
};

_Jv_JVMTIEnv *
_Jv_GetJVMTIEnv (void)
{
  _Jv_JVMTIEnv *env
    = (_Jv_JVMTIEnv *) _Jv_MallocUnchecked (sizeof (_Jv_JVMTIEnv));
  env->p = &_Jv_JVMTI_Interface;

  {
    JvSynchronize dummy (_envListLock);
    struct jvmti_env_list *element
      = (struct jvmti_env_list *) _Jv_MallocUnchecked (sizeof (struct jvmti_env_list));
    element->env = env;
    element->next = NULL;

    if (_jvmtiEnvironments == NULL)
      _jvmtiEnvironments = element;
    else
      {
	struct jvmti_env_list *e;
	for (e = _jvmtiEnvironments; e->next != NULL; e = e->next)
	  ;
	e->next = element;
      }
  }

  return env;
}

void
_Jv_JVMTI_Init ()
{
  _jvmtiEnvironments = NULL;
  _envListLock = new java::lang::Object ();
}