* configure.ac (AC_CHECK_FUNCS): Added read, write.
[cacao.git] / src / native / vm / sun / jvm.c
1 /* src/native/vm/sun/jvm.c - HotSpot JVM interface functions
2
3    Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #define PRINTJVM 0
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <ltdl.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #if defined(HAVE_SYS_IOCTL_H)
41 #define BSD_COMP /* Get FIONREAD on Solaris2 */
42 #include <sys/ioctl.h>
43 #endif
44
45 #include <sys/socket.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48
49 #include "vm/types.h"
50
51 #include "mm/memory.h"
52
53 #include "native/jni.h"
54 #include "native/llni.h"
55 #include "native/native.h"
56
57 #include "native/include/java_lang_AssertionStatusDirectives.h"
58 #include "native/include/java_lang_String.h"            /* required by j.l.CL */
59 #include "native/include/java_nio_ByteBuffer.h"         /* required by j.l.CL */
60 #include "native/include/java_lang_ClassLoader.h"        /* required by j.l.C */
61 #include "native/include/java_lang_StackTraceElement.h"
62 #include "native/include/java_lang_Throwable.h"
63 #include "native/include/java_security_ProtectionDomain.h"
64 #include "native/include/java_lang_Integer.h"
65 #include "native/include/java_lang_Long.h"
66 #include "native/include/java_lang_Short.h"
67 #include "native/include/java_lang_Byte.h"
68 #include "native/include/java_lang_Character.h"
69 #include "native/include/java_lang_Boolean.h"
70 #include "native/include/java_lang_Float.h"
71 #include "native/include/java_lang_Double.h"
72
73 #if defined(ENABLE_ANNOTATIONS)
74 #include "native/include/sun_reflect_ConstantPool.h"
75 #endif
76
77 #include "native/vm/java_lang_Class.h"
78 #include "native/vm/java_lang_ClassLoader.h"
79 #include "native/vm/java_lang_Runtime.h"
80 #include "native/vm/java_lang_Thread.h"
81 #include "native/vm/java_lang_reflect_Constructor.h"
82 #include "native/vm/java_lang_reflect_Method.h"
83 #include "native/vm/reflect.h"
84
85 #include "threads/lock-common.h"
86 #include "threads/threads-common.h"
87
88 #include "toolbox/logging.h"
89 #include "toolbox/list.h"
90
91 #include "vm/array.h"
92
93 #if defined(ENABLE_ASSERTION)
94 #include "vm/assertion.h"
95 #endif
96
97 #include "vm/builtin.h"
98 #include "vm/exceptions.h"
99 #include "vm/global.h"
100 #include "vm/initialize.h"
101 #include "vm/package.h"
102 #include "vm/primitive.h"
103 #include "vm/properties.h"
104 #include "vm/resolve.h"
105 #include "vm/signallocal.h"
106 #include "vm/stringlocal.h"
107 #include "vm/vm.h"
108
109 #include "vm/jit/stacktrace.h"
110
111 #include "vmcore/classcache.h"
112 #include "vmcore/options.h"
113 #include "vmcore/system.h"
114
115
116 /* debugging macros ***********************************************************/
117
118 #if !defined(NDEBUG)
119
120 # define TRACEJVMCALLS(...)                                             \
121     do {                                                                                \
122         if (opt_TraceJVMCalls || opt_TraceJVMCallsVerbose) {                            \
123             log_println(__VA_ARGS__);                   \
124         }                                                                               \
125     } while (0)
126
127 # define TRACEJVMCALLSVERBOSE(x)                                \
128     do {                                                                                \
129         if (opt_TraceJVMCallsVerbose) {                 \
130             log_println x;                                              \
131         }                                                                               \
132     } while (0)
133
134 # define PRINTJVMWARNINGS(...)
135 /*     do { \ */
136 /*         if (opt_PrintJVMWarnings) { \ */
137 /*             log_println(__VA_ARGS__); \ */
138 /*         } \ */
139 /*     } while (0) */
140
141 #else
142
143 # define TRACEJVMCALLS(...)
144 # define TRACEJVMCALLSVERBOSE(x)
145 # define PRINTJVMWARNINGS(...)
146
147 #endif
148
149
150 typedef struct {
151     /* Naming convention of RE build version string: n.n.n[_uu[c]][-<identifier>]-bxx */
152     unsigned int jvm_version;   /* Consists of major, minor, micro (n.n.n) */
153                                 /* and build number (xx) */
154     unsigned int update_version : 8;         /* Update release version (uu) */
155     unsigned int special_update_version : 8; /* Special update release version (c) */
156     unsigned int reserved1 : 16; 
157     unsigned int reserved2; 
158
159     /* The following bits represents JVM supports that JDK has dependency on.
160      * JDK can use these bits to determine which JVM version
161      * and support it has to maintain runtime compatibility.
162      *
163      * When a new bit is added in a minor or update release, make sure
164      * the new bit is also added in the main/baseline.
165      */
166     unsigned int is_attachable : 1;
167     unsigned int : 31;
168     unsigned int : 32;
169     unsigned int : 32;
170 } jvm_version_info;
171
172
173 /*
174  * A structure used to a capture exception table entry in a Java method.
175  */
176 typedef struct {
177     jint start_pc;
178     jint end_pc;
179     jint handler_pc;
180     jint catchType;
181 } JVM_ExceptionTableEntryType;
182
183
184 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
185 {
186         if ((intptr_t) count <= 0)
187                 return -1;
188
189         return vsnprintf(str, count, fmt, args);
190 }
191
192
193 int jio_snprintf(char *str, size_t count, const char *fmt, ...)
194 {
195         va_list ap;
196         int     len;
197
198         va_start(ap, fmt);
199         len = jio_vsnprintf(str, count, fmt, ap);
200         va_end(ap);
201
202         return len;
203 }
204
205
206 int jio_fprintf(FILE* f, const char *fmt, ...)
207 {
208         log_println("jio_fprintf: IMPLEMENT ME!");
209 }
210
211
212 int jio_vfprintf(FILE* f, const char *fmt, va_list args)
213 {
214         log_println("jio_vfprintf: IMPLEMENT ME!");
215 }
216
217
218 int jio_printf(const char *fmt, ...)
219 {
220         log_println("jio_printf: IMPLEMENT ME!");
221 }
222
223
224 /* JVM_GetInterfaceVersion */
225
226 jint JVM_GetInterfaceVersion()
227 {
228         /* This is defined in hotspot/src/share/vm/prims/jvm.h */
229
230 #define JVM_INTERFACE_VERSION 4
231
232         return JVM_INTERFACE_VERSION;
233 }
234
235
236 /* JVM_CurrentTimeMillis */
237
238 jlong JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored)
239 {
240         TRACEJVMCALLS("JVM_CurrentTimeMillis(env=%p, ignored=%p)", env, ignored);
241
242         return (jlong) builtin_currenttimemillis();
243 }
244
245
246 /* JVM_NanoTime */
247
248 jlong JVM_NanoTime(JNIEnv *env, jclass ignored)
249 {
250         TRACEJVMCALLS("JVM_NanoTime(env=%p, ignored=%p)", env, ignored);
251
252         return (jlong) builtin_nanotime();
253 }
254
255
256 /* JVM_ArrayCopy */
257
258 void JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos, jobject dst, jint dst_pos, jint length)
259 {
260         java_handle_t *s;
261         java_handle_t *d;
262
263         s = (java_handle_t *) src;
264         d = (java_handle_t *) dst;
265
266         TRACEJVMCALLSVERBOSE(("JVM_ArrayCopy(env=%p, ignored=%p, src=%p, src_pos=%d, dst=%p, dst_pos=%d, length=%d)", env, ignored, src, src_pos, dst, dst_pos, length));
267
268         builtin_arraycopy(s, src_pos, d, dst_pos, length);
269 }
270
271
272 /* JVM_InitProperties */
273
274 jobject JVM_InitProperties(JNIEnv *env, jobject properties)
275 {
276         java_handle_t *h;
277
278         TRACEJVMCALLS("JVM_InitProperties(env=%p, properties=%p)", env, properties);
279
280         h = (java_handle_t *) properties;
281
282         properties_system_add_all(h);
283
284         return properties;
285 }
286
287
288 /* JVM_Exit */
289
290 void JVM_Exit(jint code)
291 {
292         log_println("JVM_Exit: IMPLEMENT ME!");
293 }
294
295
296 /* JVM_Halt */
297
298 void JVM_Halt(jint code)
299 {
300         TRACEJVMCALLS("JVM_Halt(code=%d)", code);
301
302 /*      vm_exit(code); */
303         vm_shutdown(code);
304 }
305
306
307 /* JVM_OnExit(void (*func)) */
308
309 void JVM_OnExit(void (*func)(void))
310 {
311         log_println("JVM_OnExit(void (*func): IMPLEMENT ME!");
312 }
313
314
315 /* JVM_GC */
316
317 void JVM_GC(void)
318 {
319         TRACEJVMCALLS("JVM_GC()");
320
321         gc_call();
322 }
323
324
325 /* JVM_MaxObjectInspectionAge */
326
327 jlong JVM_MaxObjectInspectionAge(void)
328 {
329         log_println("JVM_MaxObjectInspectionAge: IMPLEMENT ME!");
330 }
331
332
333 /* JVM_TraceInstructions */
334
335 void JVM_TraceInstructions(jboolean on)
336 {
337         log_println("JVM_TraceInstructions: IMPLEMENT ME!");
338 }
339
340
341 /* JVM_TraceMethodCalls */
342
343 void JVM_TraceMethodCalls(jboolean on)
344 {
345         log_println("JVM_TraceMethodCalls: IMPLEMENT ME!");
346 }
347
348
349 /* JVM_TotalMemory */
350
351 jlong JVM_TotalMemory(void)
352 {
353         TRACEJVMCALLS("JVM_TotalMemory()");
354
355         return gc_get_heap_size();
356 }
357
358
359 /* JVM_FreeMemory */
360
361 jlong JVM_FreeMemory(void)
362 {
363         TRACEJVMCALLS("JVM_FreeMemory()");
364
365         return gc_get_free_bytes();
366 }
367
368
369 /* JVM_MaxMemory */
370
371 jlong JVM_MaxMemory(void)
372 {
373         TRACEJVMCALLS("JVM_MaxMemory()");
374
375         return gc_get_max_heap_size();
376 }
377
378
379 /* JVM_ActiveProcessorCount */
380
381 jint JVM_ActiveProcessorCount(void)
382 {
383         TRACEJVMCALLS("JVM_ActiveProcessorCount()");
384
385         return system_processors_online();
386 }
387
388
389 /* JVM_FillInStackTrace */
390
391 void JVM_FillInStackTrace(JNIEnv *env, jobject receiver)
392 {
393         java_lang_Throwable     *o;
394         java_handle_bytearray_t *ba;
395
396         TRACEJVMCALLS("JVM_FillInStackTrace(env=%p, receiver=%p)", env, receiver);
397
398         o = (java_lang_Throwable *) receiver;
399
400         ba = stacktrace_get();
401
402         if (ba == NULL)
403                 return;
404
405         LLNI_field_set_ref(o, backtrace, ba);
406 }
407
408
409 /* JVM_PrintStackTrace */
410
411 void JVM_PrintStackTrace(JNIEnv *env, jobject receiver, jobject printable)
412 {
413         log_println("JVM_PrintStackTrace: IMPLEMENT ME!");
414 }
415
416
417 /* JVM_GetStackTraceDepth */
418
419 jint JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable)
420 {
421         java_lang_Throwable     *t;
422         java_handle_bytearray_t *ba;
423         stacktrace_t            *st;
424         int32_t                  depth;
425
426         TRACEJVMCALLS("JVM_GetStackTraceDepth(env=%p, throwable=%p)", env, throwable);
427
428         if (throwable == NULL) {
429                 exceptions_throw_nullpointerexception();
430                 return 0;
431         }
432
433         t = (java_lang_Throwable *) throwable;
434
435         LLNI_field_get_ref(t, backtrace, ba);
436
437         if (ba == NULL)
438                 return 0;
439
440         /* We need a critical section here as the stacktrace structure is
441            mapped onto a Java byte-array. */
442
443         LLNI_CRITICAL_START;
444
445         st = (stacktrace_t *) LLNI_array_data(ba);
446
447         depth = st->length;
448
449         LLNI_CRITICAL_END;
450
451         return depth;
452 }
453
454
455 /* JVM_GetStackTraceElement */
456
457 jobject JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index)
458 {
459         java_lang_Throwable         *t;
460         java_handle_bytearray_t     *ba;
461         stacktrace_t                *st;
462         stacktrace_entry_t          *ste;
463         codeinfo                    *code;
464         methodinfo                  *m;
465         classinfo                   *c;
466         java_lang_StackTraceElement *o;
467         java_lang_String            *declaringclass;
468         java_lang_String            *filename;
469         int32_t                      linenumber;
470
471         TRACEJVMCALLS("JVM_GetStackTraceElement(env=%p, throwable=%p, index=%d)", env, throwable, index);
472
473         t = (java_lang_Throwable *) throwable;
474
475         LLNI_field_get_ref(t, backtrace, ba);
476
477         /* FIXME critical section */
478
479         st = (stacktrace_t *) LLNI_array_data(ba);
480
481         if ((index < 0) || (index >= st->length)) {
482                 /* XXX This should be an IndexOutOfBoundsException (check this
483                    again). */
484
485                 exceptions_throw_arrayindexoutofboundsexception();
486                 return NULL;
487         }
488
489         /* Get the stacktrace entry. */
490
491         ste = &(st->entries[index]);
492
493         /* Get the codeinfo, methodinfo and classinfo. */
494
495         code = ste->code;
496         m    = code->m;
497         c    = m->class;
498
499         /* allocate a new StackTraceElement */
500
501         o = (java_lang_StackTraceElement *)
502                 builtin_new(class_java_lang_StackTraceElement);
503
504         if (o == NULL)
505                 return NULL;
506
507         /* get filename */
508
509         if (!(m->flags & ACC_NATIVE)) {
510                 if (c->sourcefile != NULL)
511                         filename = (java_lang_String *) javastring_new(c->sourcefile);
512                 else
513                         filename = NULL;
514         }
515         else
516                 filename = NULL;
517
518         /* get line number */
519
520         if (m->flags & ACC_NATIVE) {
521                 linenumber = -2;
522         }
523         else {
524                 /* FIXME The linenumbertable_linenumber_for_pc could change
525                    the methodinfo pointer when hitting an inlined method. */
526
527                 linenumber = linenumbertable_linenumber_for_pc(&m, code, ste->pc);
528                 linenumber = (linenumber == 0) ? -1 : linenumber;
529         }
530
531         /* get declaring class name */
532
533         declaringclass = _Jv_java_lang_Class_getName(LLNI_classinfo_wrap(c));
534
535         /* fill the java.lang.StackTraceElement element */
536
537         /* FIXME critical section */
538
539         o->declaringClass = declaringclass;
540         o->methodName     = (java_lang_String *) javastring_new(m->name);
541         o->fileName       = filename;
542         o->lineNumber     = linenumber;
543
544         return (jobject) o;
545 }
546
547
548 /* JVM_IHashCode */
549
550 jint JVM_IHashCode(JNIEnv* env, jobject handle)
551 {
552         TRACEJVMCALLS("JVM_IHashCode(env=%p, jobject=%p)", env, handle);
553
554         return (jint) ((ptrint) handle);
555 }
556
557
558 /* JVM_MonitorWait */
559
560 void JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms)
561 {
562 #if defined(ENABLE_THREADS)
563         java_handle_t *o;
564 #endif
565
566         TRACEJVMCALLS("JVM_MonitorWait(env=%p, handle=%p, ms=%ld)", env, handle, ms);
567     if (ms < 0) {
568 /*              exceptions_throw_illegalargumentexception("argument out of range"); */
569                 exceptions_throw_illegalargumentexception();
570                 return;
571         }
572
573 #if defined(ENABLE_THREADS)
574         o = (java_handle_t *) handle;
575
576         lock_wait_for_object(o, ms, 0);
577 #endif
578 }
579
580
581 /* JVM_MonitorNotify */
582
583 void JVM_MonitorNotify(JNIEnv* env, jobject handle)
584 {
585 #if defined(ENABLE_THREADS)
586         java_handle_t *o;
587 #endif
588
589         TRACEJVMCALLS("JVM_MonitorNotify(env=%p, handle=%p)", env, handle);
590
591 #if defined(ENABLE_THREADS)
592         o = (java_handle_t *) handle;
593
594         lock_notify_object(o);
595 #endif
596 }
597
598
599 /* JVM_MonitorNotifyAll */
600
601 void JVM_MonitorNotifyAll(JNIEnv* env, jobject handle)
602 {
603 #if defined(ENABLE_THREADS)
604         java_handle_t *o;
605 #endif
606
607         TRACEJVMCALLS("JVM_MonitorNotifyAll(env=%p, handle=%p)", env, handle);
608
609 #if defined(ENABLE_THREADS)
610         o = (java_handle_t *) handle;
611
612         lock_notify_all_object(o);
613 #endif
614 }
615
616
617 /* JVM_Clone */
618
619 jobject JVM_Clone(JNIEnv* env, jobject handle)
620 {
621         TRACEJVMCALLS("JVM_Clone(env=%p, handle=%p)", env, handle);
622
623         return (jobject) builtin_clone(env, (java_handle_t *) handle);
624 }
625
626
627 /* JVM_InitializeCompiler  */
628
629 void JVM_InitializeCompiler (JNIEnv *env, jclass compCls)
630 {
631         log_println("JVM_InitializeCompiler : IMPLEMENT ME!");
632 }
633
634
635 /* JVM_IsSilentCompiler */
636
637 jboolean JVM_IsSilentCompiler(JNIEnv *env, jclass compCls)
638 {
639         log_println("JVM_IsSilentCompiler: IMPLEMENT ME!");
640 }
641
642
643 /* JVM_CompileClass */
644
645 jboolean JVM_CompileClass(JNIEnv *env, jclass compCls, jclass cls)
646 {
647         log_println("JVM_CompileClass: IMPLEMENT ME!");
648 }
649
650
651 /* JVM_CompileClasses */
652
653 jboolean JVM_CompileClasses(JNIEnv *env, jclass cls, jstring jname)
654 {
655         log_println("JVM_CompileClasses: IMPLEMENT ME!");
656 }
657
658
659 /* JVM_CompilerCommand */
660
661 jobject JVM_CompilerCommand(JNIEnv *env, jclass compCls, jobject arg)
662 {
663         log_println("JVM_CompilerCommand: IMPLEMENT ME!");
664 }
665
666
667 /* JVM_EnableCompiler */
668
669 void JVM_EnableCompiler(JNIEnv *env, jclass compCls)
670 {
671         TRACEJVMCALLS("JVM_EnableCompiler(env=%p, compCls=%p)", env, compCls);
672         PRINTJVMWARNINGS("JVM_EnableCompiler not supported");
673 }
674
675
676 /* JVM_DisableCompiler */
677
678 void JVM_DisableCompiler(JNIEnv *env, jclass compCls)
679 {
680         TRACEJVMCALLS("JVM_DisableCompiler(env=%p, compCls=%p)", env, compCls);
681         PRINTJVMWARNINGS("JVM_DisableCompiler not supported");
682 }
683
684
685 /* JVM_GetLastErrorString */
686
687 jint JVM_GetLastErrorString(char *buf, int len)
688 {
689         const char *s;
690         int n;
691
692     if (errno == 0) {
693                 return 0;
694     }
695         else {
696                 s = strerror(errno);
697                 n = strlen(s);
698
699                 if (n >= len)
700                         n = len - 1;
701
702                 strncpy(buf, s, n);
703
704                 buf[n] = '\0';
705
706                 return n;
707     }
708 }
709
710
711 /* JVM_NativePath */
712
713 char *JVM_NativePath(char *path)
714 {
715         TRACEJVMCALLS("JVM_NativePath(path=%s)", path);
716
717         /* XXX is this correct? */
718
719         return path;
720 }
721
722
723 /* JVM_GetCallerClass */
724
725 jclass JVM_GetCallerClass(JNIEnv* env, int depth)
726 {
727         java_handle_objectarray_t *oa;
728
729         TRACEJVMCALLS("JVM_GetCallerClass(env=%p, depth=%d)", env, depth);
730
731         oa = stacktrace_getClassContext();
732
733         if (oa == NULL)
734                 return NULL;
735
736         if (oa->header.size < depth)
737                 return NULL;
738
739         return (jclass) oa->data[depth - 1];
740
741 }
742
743
744 /* JVM_FindPrimitiveClass */
745
746 jclass JVM_FindPrimitiveClass(JNIEnv* env, const char* s)
747 {
748         classinfo *c;
749         utf       *u;
750
751         TRACEJVMCALLS("JVM_FindPrimitiveClass(env=%p, s=%s)", env, s);
752
753         u = utf_new_char(s);
754         c = primitive_class_get_by_name(u);
755
756         return (jclass) LLNI_classinfo_wrap(c);
757 }
758
759
760 /* JVM_ResolveClass */
761
762 void JVM_ResolveClass(JNIEnv* env, jclass cls)
763 {
764         TRACEJVMCALLS("JVM_ResolveClass(env=%p, cls=%p)", env, cls);
765         PRINTJVMWARNINGS("JVM_ResolveClass not implemented");
766 }
767
768
769 /* JVM_FindClassFromClassLoader */
770
771 jclass JVM_FindClassFromClassLoader(JNIEnv* env, const char* name, jboolean init, jobject loader, jboolean throwError)
772 {
773         classinfo   *c;
774         utf         *u;
775         classloader *cl;
776
777         TRACEJVMCALLS("JVM_FindClassFromClassLoader(name=%s, init=%d, loader=%p, throwError=%d)", name, init, loader, throwError);
778
779         u  = utf_new_char(name);
780         cl = loader_hashtable_classloader_add((java_handle_t *) loader);
781
782         c = load_class_from_classloader(u, cl);
783
784         if (c == NULL)
785                 return NULL;
786
787         if (init)
788                 if (!(c->state & CLASS_INITIALIZED))
789                         if (!initialize_class(c))
790                                 return NULL;
791
792         return (jclass) LLNI_classinfo_wrap(c);
793 }
794
795
796 /* JVM_FindClassFromClass */
797
798 jclass JVM_FindClassFromClass(JNIEnv *env, const char *name, jboolean init, jclass from)
799 {
800         log_println("JVM_FindClassFromClass: IMPLEMENT ME!");
801 }
802
803
804 /* JVM_DefineClass */
805
806 jclass JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd)
807 {
808         log_println("JVM_DefineClass: IMPLEMENT ME!");
809 }
810
811
812 /* JVM_DefineClassWithSource */
813
814 jclass JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source)
815 {
816         classinfo   *c;
817         utf         *u;
818         classloader *cl;
819
820         TRACEJVMCALLS("JVM_DefineClassWithSource(env=%p, name=%s, loader=%p, buf=%p, len=%d, pd=%p, source=%s)", env, name, loader, buf, len, pd, source);
821
822         if (name != NULL)
823                 u = utf_new_char(name);
824         else
825                 u = NULL;
826
827         cl = loader_hashtable_classloader_add((java_handle_t *) loader);
828
829         /* XXX do something with source */
830
831         c = class_define(u, cl, len, (const uint8_t *) buf, (java_handle_t *) pd);
832
833         return (jclass) LLNI_classinfo_wrap(c);
834 }
835
836
837 /* JVM_FindLoadedClass */
838
839 jclass JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name)
840 {
841         classloader *cl;
842         utf         *u;
843         classinfo   *c;
844
845         TRACEJVMCALLS("JVM_FindLoadedClass(env=%p, loader=%p, name=%p)", env, loader, name);
846
847         cl = loader_hashtable_classloader_add((java_handle_t *) loader);
848
849         u = javastring_toutf((java_handle_t *) name, true);
850         c = classcache_lookup(cl, u);
851
852         return (jclass) LLNI_classinfo_wrap(c);
853 }
854
855
856 /* JVM_GetClassName */
857
858 jstring JVM_GetClassName(JNIEnv *env, jclass cls)
859 {
860         TRACEJVMCALLS("JVM_GetClassName(env=%p, cls=%p)", env, cls);
861
862         return (jstring) _Jv_java_lang_Class_getName((java_lang_Class *) cls);
863 }
864
865
866 /* JVM_GetClassInterfaces */
867
868 jobjectArray JVM_GetClassInterfaces(JNIEnv *env, jclass cls)
869 {
870         classinfo                 *c;
871         java_handle_objectarray_t *oa;
872
873         TRACEJVMCALLS("JVM_GetClassInterfaces(env=%p, cls=%p)", env, cls);
874
875         c = LLNI_classinfo_unwrap(cls);
876
877         oa = class_get_interfaces(c);
878
879         return (jobjectArray) oa;
880 }
881
882
883 /* JVM_GetClassLoader */
884
885 jobject JVM_GetClassLoader(JNIEnv *env, jclass cls)
886 {
887         classinfo   *c;
888         classloader *cl;
889
890         TRACEJVMCALLS("JVM_GetClassLoader(env=%p, cls=%p)", env, cls);
891
892         c  = LLNI_classinfo_unwrap(cls);
893         cl = class_get_classloader(c);
894
895         return (jobject) cl;
896 }
897
898
899 /* JVM_IsInterface */
900
901 jboolean JVM_IsInterface(JNIEnv *env, jclass cls)
902 {
903         classinfo *c;
904
905         TRACEJVMCALLS("JVM_IsInterface(env=%p, cls=%p)", env, cls);
906
907         c = LLNI_classinfo_unwrap(cls);
908
909         return class_is_interface(c);
910 }
911
912
913 /* JVM_GetClassSigners */
914
915 jobjectArray JVM_GetClassSigners(JNIEnv *env, jclass cls)
916 {
917         log_println("JVM_GetClassSigners: IMPLEMENT ME!");
918 }
919
920
921 /* JVM_SetClassSigners */
922
923 void JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers)
924 {
925         classinfo                 *c;
926         java_handle_objectarray_t *hoa;
927
928         TRACEJVMCALLS("JVM_SetClassSigners(env=%p, cls=%p, signers=%p)", env, cls, signers);
929
930         c = LLNI_classinfo_unwrap(cls);
931
932         hoa = (java_handle_objectarray_t *) signers;
933
934     /* This call is ignored for primitive types and arrays.  Signers
935            are only set once, ClassLoader.java, and thus shouldn't be
936            called with an array.  Only the bootstrap loader creates
937            arrays. */
938
939         if (class_is_primitive(c) || class_is_array(c))
940                 return;
941
942         LLNI_classinfo_field_set(c, signers, hoa);
943 }
944
945
946 /* JVM_GetProtectionDomain */
947
948 jobject JVM_GetProtectionDomain(JNIEnv *env, jclass cls)
949 {
950         classinfo *c;
951
952         TRACEJVMCALLS("JVM_GetProtectionDomain(env=%p, cls=%p)", env, cls);
953
954         c = LLNI_classinfo_unwrap(cls);
955
956         if (c == NULL) {
957                 exceptions_throw_nullpointerexception();
958                 return NULL;
959         }
960
961     /* Primitive types do not have a protection domain. */
962
963         if (class_is_primitive(c))
964                 return NULL;
965
966         return (jobject) c->protectiondomain;
967 }
968
969
970 /* JVM_SetProtectionDomain */
971
972 void JVM_SetProtectionDomain(JNIEnv *env, jclass cls, jobject protection_domain)
973 {
974         log_println("JVM_SetProtectionDomain: IMPLEMENT ME!");
975 }
976
977
978 /* JVM_DoPrivileged */
979
980 jobject JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException)
981 {
982         java_handle_t *o;
983         classinfo     *c;
984         methodinfo    *m;
985         java_handle_t *result;
986         java_handle_t *e;
987
988         TRACEJVMCALLS("JVM_DoPrivileged(env=%p, cls=%p, action=%p, context=%p, wrapException=%d)", env, cls, action, context, wrapException);
989
990         o = (java_handle_t *) action;
991         c = o->vftbl->class;
992
993         if (action == NULL) {
994                 exceptions_throw_nullpointerexception();
995                 return NULL;
996         }
997
998         /* lookup run() method (throw no exceptions) */
999
1000         m = class_resolveclassmethod(c, utf_run, utf_void__java_lang_Object, c,
1001                                                                  false);
1002
1003         if ((m == NULL) || !(m->flags & ACC_PUBLIC) || (m->flags & ACC_STATIC)) {
1004                 exceptions_throw_internalerror("No run method");
1005                 return NULL;
1006         }
1007
1008         /* XXX It seems something with a privileged stack needs to be done
1009            here. */
1010
1011         result = vm_call_method(m, o);
1012
1013         e = exceptions_get_and_clear_exception();
1014
1015         if (e != NULL) {
1016                 exceptions_throw_privilegedactionexception(e);
1017                 return NULL;
1018         }
1019
1020         return (jobject) result;
1021 }
1022
1023
1024 /* JVM_GetInheritedAccessControlContext */
1025
1026 jobject JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls)
1027 {
1028         log_println("JVM_GetInheritedAccessControlContext: IMPLEMENT ME!");
1029 }
1030
1031
1032 /* JVM_GetStackAccessControlContext */
1033
1034 jobject JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls)
1035 {
1036         TRACEJVMCALLS("JVM_GetStackAccessControlContext(env=%p, cls=%p): IMPLEMENT ME!", env, cls);
1037
1038         /* XXX All stuff I tested so far works without that function.  At
1039            some point we have to implement it, but I disable the output
1040            for now to make IcedTea happy. */
1041
1042         return NULL;
1043 }
1044
1045
1046 /* JVM_IsArrayClass */
1047
1048 jboolean JVM_IsArrayClass(JNIEnv *env, jclass cls)
1049 {
1050         classinfo *c;
1051
1052         TRACEJVMCALLS("JVM_IsArrayClass(env=%p, cls=%p)", env, cls);
1053
1054         c = LLNI_classinfo_unwrap(cls);
1055
1056         return class_is_array(c);
1057 }
1058
1059
1060 /* JVM_IsPrimitiveClass */
1061
1062 jboolean JVM_IsPrimitiveClass(JNIEnv *env, jclass cls)
1063 {
1064         classinfo *c;
1065
1066         TRACEJVMCALLS("JVM_IsPrimitiveClass(env=%p, cls=%p)", env, cls);
1067
1068         c = LLNI_classinfo_unwrap(cls);
1069
1070         return class_is_primitive(c);
1071 }
1072
1073
1074 /* JVM_GetComponentType */
1075
1076 jclass JVM_GetComponentType(JNIEnv *env, jclass cls)
1077 {
1078         classinfo *component;
1079         classinfo *c;
1080         
1081         TRACEJVMCALLS("JVM_GetComponentType(env=%p, cls=%p)", env, cls);
1082
1083         c = LLNI_classinfo_unwrap(cls);
1084         
1085         component = class_get_componenttype(c);
1086
1087         return (jclass) LLNI_classinfo_wrap(component);
1088 }
1089
1090
1091 /* JVM_GetClassModifiers */
1092
1093 jint JVM_GetClassModifiers(JNIEnv *env, jclass cls)
1094 {
1095         classinfo *c;
1096         int32_t    flags;
1097
1098         TRACEJVMCALLS("JVM_GetClassModifiers(env=%p, cls=%p)", env, cls);
1099
1100         c = LLNI_classinfo_unwrap(cls);
1101
1102         flags = class_get_modifiers(c, false);
1103
1104         return flags;
1105 }
1106
1107
1108 /* JVM_GetDeclaredClasses */
1109
1110 jobjectArray JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass)
1111 {
1112         classinfo                 *c;
1113         java_handle_objectarray_t *oa;
1114
1115         TRACEJVMCALLS("JVM_GetDeclaredClasses(env=%p, ofClass=%p)", env, ofClass);
1116
1117         c = LLNI_classinfo_unwrap(ofClass);
1118
1119         oa = class_get_declaredclasses(c, false);
1120
1121         return (jobjectArray) oa;
1122 }
1123
1124
1125 /* JVM_GetDeclaringClass */
1126
1127 jclass JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass)
1128 {
1129         classinfo *c;
1130         classinfo *dc;
1131
1132         TRACEJVMCALLS("JVM_GetDeclaringClass(env=%p, ofClass=%p)", env, ofClass);
1133
1134         c = LLNI_classinfo_unwrap(ofClass);
1135
1136         dc = class_get_declaringclass(c);
1137
1138         return (jclass) LLNI_classinfo_wrap(dc);
1139 }
1140
1141
1142 /* JVM_GetClassSignature */
1143
1144 jstring JVM_GetClassSignature(JNIEnv *env, jclass cls)
1145 {
1146         classinfo     *c;
1147         utf           *u;
1148         java_handle_t *s;
1149
1150         TRACEJVMCALLS("JVM_GetClassSignature(env=%p, cls=%p)", env, cls);
1151
1152         c = LLNI_classinfo_unwrap(cls);
1153
1154         /* Get the signature of the class. */
1155
1156         u = class_get_signature(c);
1157
1158         if (u == NULL)
1159                 return NULL;
1160
1161         /* Convert UTF-string to a Java-string. */
1162
1163         s = javastring_new(u);
1164
1165         return (jstring) s;
1166 }
1167
1168
1169 /* JVM_GetClassAnnotations */
1170
1171 jbyteArray JVM_GetClassAnnotations(JNIEnv *env, jclass cls)
1172 {
1173         classinfo               *c           = NULL; /* classinfo for 'cls'  */
1174         java_handle_bytearray_t *annotations = NULL; /* unparsed annotations */
1175
1176         TRACEJVMCALLS("JVM_GetClassAnnotations: cls=%p", cls);
1177
1178         if (cls == NULL) {
1179                 exceptions_throw_nullpointerexception();
1180                 return NULL;
1181         }
1182         
1183         c = LLNI_classinfo_unwrap(cls);
1184
1185         /* get annotations: */
1186         annotations = class_get_annotations(c);
1187
1188         return (jbyteArray)annotations;
1189 }
1190
1191
1192 /* JVM_GetFieldAnnotations */
1193
1194 jbyteArray JVM_GetFieldAnnotations(JNIEnv *env, jobject field)
1195 {
1196         java_lang_reflect_Field *rf = NULL; /* java.lang.reflect.Field for 'field' */
1197         java_handle_bytearray_t *ba = NULL; /* unparsed annotations */
1198
1199         TRACEJVMCALLS("JVM_GetFieldAnnotations: field=%p", field);
1200
1201         if (field == NULL) {
1202                 exceptions_throw_nullpointerexception();
1203                 return NULL;
1204         }
1205
1206         rf = (java_lang_reflect_Field*)field;
1207
1208         LLNI_field_get_ref(rf, annotations, ba);
1209
1210         return (jbyteArray)ba;
1211 }
1212
1213
1214 /* JVM_GetMethodAnnotations */
1215
1216 jbyteArray JVM_GetMethodAnnotations(JNIEnv *env, jobject method)
1217 {
1218         java_lang_reflect_Method *rm = NULL; /* java.lang.reflect.Method for 'method' */
1219         java_handle_bytearray_t  *ba = NULL; /* unparsed annotations */
1220
1221         TRACEJVMCALLS("JVM_GetMethodAnnotations: method=%p", method);
1222
1223         if (method == NULL) {
1224                 exceptions_throw_nullpointerexception();
1225                 return NULL;
1226         }
1227
1228         rm = (java_lang_reflect_Method*)method;
1229
1230         LLNI_field_get_ref(rm, annotations, ba);
1231
1232         return (jbyteArray)ba;
1233 }
1234
1235
1236 /* JVM_GetMethodDefaultAnnotationValue */
1237
1238 jbyteArray JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method)
1239 {
1240         java_lang_reflect_Method *rm = NULL; /* java.lang.reflect.Method for 'method' */
1241         java_handle_bytearray_t  *ba = NULL; /* unparsed annotation default value */
1242
1243         TRACEJVMCALLS("JVM_GetMethodDefaultAnnotationValue: method=%p", method);
1244
1245         if (method == NULL) {
1246                 exceptions_throw_nullpointerexception();
1247                 return NULL;
1248         }
1249
1250         rm = (java_lang_reflect_Method*)method;
1251
1252         LLNI_field_get_ref(rm, annotationDefault, ba);
1253
1254         return (jbyteArray)ba;
1255 }
1256
1257
1258 /* JVM_GetMethodParameterAnnotations */
1259
1260 jbyteArray JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method)
1261 {
1262         java_lang_reflect_Method *rm = NULL; /* java.lang.reflect.Method for 'method' */
1263         java_handle_bytearray_t  *ba = NULL; /* unparsed parameter annotations */
1264
1265         TRACEJVMCALLS("JVM_GetMethodParameterAnnotations: method=%p", method);
1266
1267         if (method == NULL) {
1268                 exceptions_throw_nullpointerexception();
1269                 return NULL;
1270         }
1271
1272         rm = (java_lang_reflect_Method*)method;
1273
1274         LLNI_field_get_ref(rm, parameterAnnotations, ba);
1275
1276         return (jbyteArray)ba;
1277 }
1278
1279
1280 /* JVM_GetClassDeclaredFields */
1281
1282 jobjectArray JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly)
1283 {
1284         TRACEJVMCALLS("JVM_GetClassDeclaredFields(env=%p, ofClass=%p, publicOnly=%d)", env, ofClass, publicOnly);
1285
1286         return (jobjectArray) _Jv_java_lang_Class_getDeclaredFields((java_lang_Class *) ofClass, publicOnly);
1287 }
1288
1289
1290 /* JVM_GetClassDeclaredMethods */
1291
1292 jobjectArray JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly)
1293 {
1294         TRACEJVMCALLS("JVM_GetClassDeclaredMethods(env=%p, ofClass=%p, publicOnly=%d)", env, ofClass, publicOnly);
1295
1296         return (jobjectArray) _Jv_java_lang_Class_getDeclaredMethods((java_lang_Class *) ofClass, publicOnly);
1297 }
1298
1299
1300 /* JVM_GetClassDeclaredConstructors */
1301
1302 jobjectArray JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly)
1303 {
1304         TRACEJVMCALLS("JVM_GetClassDeclaredConstructors(env=%p, ofClass=%p, publicOnly=%d)", env, ofClass, publicOnly);
1305
1306         return (jobjectArray) _Jv_java_lang_Class_getDeclaredConstructors((java_lang_Class *) ofClass, publicOnly);
1307 }
1308
1309
1310 /* JVM_GetClassAccessFlags */
1311
1312 jint JVM_GetClassAccessFlags(JNIEnv *env, jclass cls)
1313 {
1314         classinfo *c;
1315
1316         TRACEJVMCALLS("JVM_GetClassAccessFlags(env=%p, cls=%p)", env, cls);
1317
1318         c = LLNI_classinfo_unwrap(cls);
1319
1320         /* Primitive type classes have the correct access flags. */
1321
1322         return c->flags & ACC_CLASS_REFLECT_MASK;
1323 }
1324
1325
1326 /* JVM_GetClassConstantPool */
1327
1328 jobject JVM_GetClassConstantPool(JNIEnv *env, jclass cls)
1329 {
1330 #if defined(ENABLE_ANNOTATIONS)
1331         sun_reflect_ConstantPool *constantPool    = NULL;
1332                       /* constant pool object for the class refered by 'cls' */
1333         java_lang_Object         *constantPoolOop = (java_lang_Object*)cls;
1334                       /* constantPoolOop field of the constant pool object   */
1335
1336         TRACEJVMCALLS("JVM_GetClassConstantPool(env=%p, cls=%p)", env, cls);
1337
1338         constantPool = 
1339                 (sun_reflect_ConstantPool*)native_new_and_init(
1340                         class_sun_reflect_ConstantPool);
1341         
1342         if (constantPool == NULL) {
1343                 /* out of memory */
1344                 return NULL;
1345         }
1346         
1347         LLNI_field_set_ref(constantPool, constantPoolOop, constantPoolOop);
1348
1349         return (jobject)constantPool;
1350 #else
1351         log_println("JVM_GetClassConstantPool(env=%p, cls=%p): not implemented in this configuration!", env, cls);
1352         return NULL;
1353 #endif
1354 }
1355
1356
1357 /* JVM_ConstantPoolGetSize */
1358
1359 jint JVM_ConstantPoolGetSize(JNIEnv *env, jobject unused, jobject jcpool)
1360 {
1361         classinfo *c; /* classinfo of the class for which 'this' is the constant pool */
1362
1363         TRACEJVMCALLS("JVM_ConstantPoolGetSize(env=%p, unused=%p, jcpool=%p)", env, unused, jcpool);
1364
1365         c = LLNI_classinfo_unwrap(jcpool);
1366
1367         return c->cpcount;
1368 }
1369
1370
1371 /* JVM_ConstantPoolGetClassAt */
1372
1373 jclass JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1374 {
1375         constant_classref *ref;    /* classref to the class at constant pool index 'index' */
1376         classinfo         *c;      /* classinfo of the class for which 'this' is the constant pool */
1377         classinfo         *result; /* classinfo of the class at constant pool index 'index' */
1378
1379         TRACEJVMCALLS("JVM_ConstantPoolGetClassAt(env=%p, jcpool=%p, index=%d)", env, jcpool, index);
1380
1381         c = LLNI_classinfo_unwrap(jcpool);
1382
1383         ref = (constant_classref *) class_getconstant(c, index, CONSTANT_Class);
1384
1385         if (ref == NULL) {
1386                 exceptions_throw_illegalargumentexception();
1387                 return NULL;
1388         }
1389
1390         result = resolve_classref_eager(ref);
1391
1392         return (jclass) LLNI_classinfo_wrap(result);
1393 }
1394
1395
1396 /* JVM_ConstantPoolGetClassAtIfLoaded */
1397
1398 jclass JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1399 {
1400         constant_classref *ref;    /* classref to the class at constant pool index 'index' */
1401         classinfo         *c;      /* classinfo of the class for which 'this' is the constant pool */
1402         classinfo         *result; /* classinfo of the class at constant pool index 'index' */
1403
1404         TRACEJVMCALLS("JVM_ConstantPoolGetClassAtIfLoaded(env=%p, unused=%p, jcpool=%p, index=%d)", env, unused, jcpool, index);
1405
1406         c = LLNI_classinfo_unwrap(jcpool);
1407
1408         ref = (constant_classref *) class_getconstant(c, index, CONSTANT_Class);
1409
1410         if (ref == NULL) {
1411                 exceptions_throw_illegalargumentexception();
1412                 return NULL;
1413         }
1414         
1415         if (!resolve_classref(NULL, ref, resolveLazy, true, true, &result)) {
1416                 return NULL;
1417         }
1418
1419         if ((result == NULL) || !(result->state & CLASS_LOADED)) {
1420                 return NULL;
1421         }
1422         
1423         return (jclass) LLNI_classinfo_wrap(result);
1424 }
1425
1426
1427 /* JVM_ConstantPoolGetMethodAt */
1428
1429 jobject JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1430 {
1431         constant_FMIref *ref; /* reference to the method in constant pool at index 'index' */
1432         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1433         
1434         TRACEJVMCALLS("JVM_ConstantPoolGetMethodAt: jcpool=%p, index=%d", jcpool, index);
1435         
1436         cls = LLNI_classinfo_unwrap(jcpool);
1437         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Methodref);
1438         
1439         if (ref == NULL) {
1440                 exceptions_throw_illegalargumentexception();
1441                 return NULL;
1442         }
1443
1444         /* XXX: is that right? or do I have to use resolve_method_*? */
1445         return (jobject)reflect_method_new(ref->p.method);
1446 }
1447
1448
1449 /* JVM_ConstantPoolGetMethodAtIfLoaded */
1450
1451 jobject JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1452 {
1453         constant_FMIref *ref; /* reference to the method in constant pool at index 'index' */
1454         classinfo *c = NULL;  /* resolved declaring class of the method */
1455         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1456
1457         TRACEJVMCALLS("JVM_ConstantPoolGetMethodAtIfLoaded: jcpool=%p, index=%d", jcpool, index);
1458
1459         cls = LLNI_classinfo_unwrap(jcpool);
1460         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Methodref);
1461
1462         if (ref == NULL) {
1463                 exceptions_throw_illegalargumentexception();
1464                 return NULL;
1465         }
1466
1467         if (!resolve_classref(NULL, ref->p.classref, resolveLazy, true, true, &c)) {
1468                 return NULL;
1469         }
1470
1471         if (c == NULL || !(c->state & CLASS_LOADED)) {
1472                 return NULL;
1473         }
1474
1475         return (jobject)reflect_method_new(ref->p.method);
1476 }
1477
1478
1479 /* JVM_ConstantPoolGetFieldAt */
1480
1481 jobject JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1482 {
1483         constant_FMIref *ref; /* reference to the field in constant pool at index 'index' */
1484         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1485         
1486         TRACEJVMCALLS("JVM_ConstantPoolGetFieldAt: jcpool=%p, index=%d", jcpool, index);
1487
1488         cls = LLNI_classinfo_unwrap(jcpool);
1489         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Fieldref);
1490
1491         if (ref == NULL) {
1492                 exceptions_throw_illegalargumentexception();
1493                 return NULL;
1494         }
1495
1496         return (jobject)reflect_field_new(ref->p.field);
1497 }
1498
1499
1500 /* JVM_ConstantPoolGetFieldAtIfLoaded */
1501
1502 jobject JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1503 {
1504         constant_FMIref *ref; /* reference to the field in constant pool at index 'index' */
1505         classinfo *c;         /* resolved declaring class for the field */
1506         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1507
1508         TRACEJVMCALLS("JVM_ConstantPoolGetFieldAtIfLoaded: jcpool=%p, index=%d", jcpool, index);
1509
1510         cls = LLNI_classinfo_unwrap(jcpool);
1511         ref = (constant_FMIref*)class_getconstant(cls, index, CONSTANT_Fieldref);
1512
1513         if (ref == NULL) {
1514                 exceptions_throw_illegalargumentexception();
1515                 return NULL;
1516         }
1517
1518         if (!resolve_classref(NULL, ref->p.classref, resolveLazy, true, true, &c)) {
1519                 return NULL;
1520         }
1521
1522         if (c == NULL || !(c->state & CLASS_LOADED)) {
1523                 return NULL;
1524         }
1525
1526         return (jobject)reflect_field_new(ref->p.field);
1527 }
1528
1529
1530 /* JVM_ConstantPoolGetMemberRefInfoAt */
1531
1532 jobjectArray JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1533 {
1534         log_println("JVM_ConstantPoolGetMemberRefInfoAt: jcpool=%p, index=%d, IMPLEMENT ME!", jcpool, index);
1535
1536         /* TODO: implement. (this is yet unused be OpenJDK but, so very low priority) */
1537
1538         return NULL;
1539 }
1540
1541
1542 /* JVM_ConstantPoolGetIntAt */
1543
1544 jint JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1545 {
1546         constant_integer *ref; /* reference to the int value in constant pool at index 'index' */
1547         classinfo *cls;        /* classinfo of the class for which 'this' is the constant pool */
1548
1549         TRACEJVMCALLS("JVM_ConstantPoolGetIntAt: jcpool=%p, index=%d", jcpool, index);
1550
1551         cls = LLNI_classinfo_unwrap(jcpool);
1552         ref = (constant_integer*)class_getconstant(cls, index, CONSTANT_Integer);
1553
1554         if (ref == NULL) {
1555                 exceptions_throw_illegalargumentexception();
1556                 return 0;
1557         }
1558
1559         return ref->value;
1560 }
1561
1562
1563 /* JVM_ConstantPoolGetLongAt */
1564
1565 jlong JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1566 {
1567         constant_long *ref; /* reference to the long value in constant pool at index 'index' */
1568         classinfo *cls;     /* classinfo of the class for which 'this' is the constant pool */
1569
1570         TRACEJVMCALLS("JVM_ConstantPoolGetLongAt: jcpool=%p, index=%d", jcpool, index);
1571
1572         cls = LLNI_classinfo_unwrap(jcpool);
1573         ref = (constant_long*)class_getconstant(cls, index, CONSTANT_Long);
1574
1575         if (ref == NULL) {
1576                 exceptions_throw_illegalargumentexception();
1577                 return 0;
1578         }
1579
1580         return ref->value;
1581 }
1582
1583
1584 /* JVM_ConstantPoolGetFloatAt */
1585
1586 jfloat JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1587 {
1588         constant_float *ref; /* reference to the float value in constant pool at index 'index' */
1589         classinfo *cls;      /* classinfo of the class for which 'this' is the constant pool */
1590
1591         TRACEJVMCALLS("JVM_ConstantPoolGetFloatAt: jcpool=%p, index=%d", jcpool, index);
1592
1593         cls = LLNI_classinfo_unwrap(jcpool);
1594         ref = (constant_float*)class_getconstant(cls, index, CONSTANT_Float);
1595
1596         if (ref == NULL) {
1597                 exceptions_throw_illegalargumentexception();
1598                 return 0;
1599         }
1600
1601         return ref->value;
1602 }
1603
1604
1605 /* JVM_ConstantPoolGetDoubleAt */
1606
1607 jdouble JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1608 {
1609         constant_double *ref; /* reference to the double value in constant pool at index 'index' */
1610         classinfo *cls;       /* classinfo of the class for which 'this' is the constant pool */
1611
1612         TRACEJVMCALLS("JVM_ConstantPoolGetDoubleAt: jcpool=%p, index=%d", jcpool, index);
1613
1614         cls = LLNI_classinfo_unwrap(jcpool);
1615         ref = (constant_double*)class_getconstant(cls, index, CONSTANT_Double);
1616
1617         if (ref == NULL) {
1618                 exceptions_throw_illegalargumentexception();
1619                 return 0;
1620         }
1621
1622         return ref->value;
1623 }
1624
1625
1626 /* JVM_ConstantPoolGetStringAt */
1627
1628 jstring JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1629 {
1630         utf *ref;       /* utf object for the string in constant pool at index 'index' */
1631         classinfo *cls; /* classinfo of the class for which 'this' is the constant pool */
1632
1633         TRACEJVMCALLS("JVM_ConstantPoolGetStringAt: jcpool=%p, index=%d", jcpool, index);
1634         
1635         cls = LLNI_classinfo_unwrap(jcpool);
1636         ref = (utf*)class_getconstant(cls, index, CONSTANT_String);
1637
1638         if (ref == NULL) {
1639                 exceptions_throw_illegalargumentexception();
1640                 return NULL;
1641         }
1642
1643         /* XXX: I hope literalstring_new is the right Function. */
1644         return (jstring)literalstring_new(ref);
1645 }
1646
1647
1648 /* JVM_ConstantPoolGetUTF8At */
1649
1650 jstring JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject unused, jobject jcpool, jint index)
1651 {
1652         utf *ref; /* utf object for the utf8 data in constant pool at index 'index' */
1653         classinfo *cls; /* classinfo of the class for which 'this' is the constant pool */
1654
1655         TRACEJVMCALLS("JVM_ConstantPoolGetUTF8At: jcpool=%p, index=%d", jcpool, index);
1656
1657         cls = LLNI_classinfo_unwrap(jcpool);
1658         ref = (utf*)class_getconstant(cls, index, CONSTANT_Utf8);
1659
1660         if (ref == NULL) {
1661                 exceptions_throw_illegalargumentexception();
1662                 return NULL;
1663         }
1664
1665         /* XXX: I hope literalstring_new is the right Function. */
1666         return (jstring)literalstring_new(ref);
1667 }
1668
1669
1670 /* JVM_DesiredAssertionStatus */
1671
1672 jboolean JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls)
1673 {
1674 #if defined(ENABLE_ASSERTION)
1675         assertion_name_t  *item;
1676         classinfo         *c;
1677         jboolean           status;
1678         utf               *name;
1679
1680         TRACEJVMCALLS("JVM_DesiredAssertionStatus(env=%p, unused=%p, cls=%p)", env, unused, cls);
1681
1682         c = LLNI_classinfo_unwrap(cls);
1683
1684         if (c->classloader == NULL) {
1685                 status = (jboolean)assertion_system_enabled;
1686         }
1687         else {
1688                 status = (jboolean)assertion_user_enabled;
1689         }
1690
1691         if (list_assertion_names != NULL) {
1692                 item = (assertion_name_t *)list_first(list_assertion_names);
1693                 while (item != NULL) {
1694                         name = utf_new_char(item->name);
1695                         if (name == c->packagename) {
1696                                 status = (jboolean)item->enabled;
1697                         }
1698                         else if (name == c->name) {
1699                                 status = (jboolean)item->enabled;
1700                         }
1701
1702                         item = (assertion_name_t *)list_next(list_assertion_names, item);
1703                 }
1704         }
1705
1706         return status;
1707 #else
1708         return (jboolean)false;
1709 #endif
1710 }
1711
1712
1713 /* JVM_AssertionStatusDirectives */
1714
1715 jobject JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused)
1716 {
1717         classinfo                             *c;
1718         java_lang_AssertionStatusDirectives   *o;
1719         java_handle_objectarray_t             *classes;
1720         java_handle_objectarray_t             *packages;
1721         java_booleanarray_t                   *classEnabled;
1722         java_booleanarray_t                   *packageEnabled;
1723 #if defined(ENABLE_ASSERTION)
1724         assertion_name_t                      *item;
1725         java_handle_t                         *js;
1726         s4                                     i, j;
1727 #endif
1728
1729         TRACEJVMCALLS("JVM_AssertionStatusDirectives(env=%p, unused=%p)", env, unused);
1730
1731         c = load_class_bootstrap(utf_new_char("java/lang/AssertionStatusDirectives"));
1732
1733         if (c == NULL)
1734                 return NULL;
1735
1736         o = (java_lang_AssertionStatusDirectives *) builtin_new(c);
1737
1738         if (o == NULL)
1739                 return NULL;
1740
1741 #if defined(ENABLE_ASSERTION)
1742         classes = builtin_anewarray(assertion_class_count, class_java_lang_Object);
1743 #else
1744         classes = builtin_anewarray(0, class_java_lang_Object);
1745 #endif
1746         if (classes == NULL)
1747                 return NULL;
1748
1749 #if defined(ENABLE_ASSERTION)
1750         packages = builtin_anewarray(assertion_package_count, class_java_lang_Object);
1751 #else
1752         packages = builtin_anewarray(0, class_java_lang_Object);
1753 #endif
1754         if (packages == NULL)
1755                 return NULL;
1756         
1757 #if defined(ENABLE_ASSERTION)
1758         classEnabled = builtin_newarray_boolean(assertion_class_count);
1759 #else
1760         classEnabled = builtin_newarray_boolean(0);
1761 #endif
1762         if (classEnabled == NULL)
1763                 return NULL;
1764
1765 #if defined(ENABLE_ASSERTION)
1766         packageEnabled = builtin_newarray_boolean(assertion_package_count);
1767 #else
1768         packageEnabled = builtin_newarray_boolean(0);
1769 #endif
1770         if (packageEnabled == NULL)
1771                 return NULL;
1772
1773 #if defined(ENABLE_ASSERTION)
1774         /* initialize arrays */
1775
1776         if (list_assertion_names != NULL) {
1777                 i = 0;
1778                 j = 0;
1779                 
1780                 item = (assertion_name_t *)list_first(list_assertion_names);
1781                 while (item != NULL) {
1782                         js = javastring_new_from_ascii(item->name);
1783                         if (js == NULL) {
1784                                 return NULL;
1785                         }
1786
1787                         if (item->package == false) {
1788                                 classes->data[i] = js;
1789                                 classEnabled->data[i] = (jboolean) item->enabled;
1790                                 i += 1;
1791                         }
1792                         else {
1793                                 packages->data[j] = js;
1794                                 packageEnabled->data[j] = (jboolean) item->enabled;
1795                                 j += 1;
1796                         }
1797
1798                         item = (assertion_name_t *)list_next(list_assertion_names, item);
1799                 }
1800         }
1801 #endif
1802
1803         /* set instance fields */
1804
1805         o->classes  = classes;
1806         o->packages = packages;
1807         o->classEnabled = classEnabled;
1808         o->packageEnabled = packageEnabled;
1809
1810         return (jobject) o;
1811 }
1812
1813
1814 /* JVM_GetClassNameUTF */
1815
1816 const char* JVM_GetClassNameUTF(JNIEnv *env, jclass cls)
1817 {
1818         log_println("JVM_GetClassNameUTF: IMPLEMENT ME!");
1819 }
1820
1821
1822 /* JVM_GetClassCPTypes */
1823
1824 void JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types)
1825 {
1826         log_println("JVM_GetClassCPTypes: IMPLEMENT ME!");
1827 }
1828
1829
1830 /* JVM_GetClassCPEntriesCount */
1831
1832 jint JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls)
1833 {
1834         log_println("JVM_GetClassCPEntriesCount: IMPLEMENT ME!");
1835 }
1836
1837
1838 /* JVM_GetClassFieldsCount */
1839
1840 jint JVM_GetClassFieldsCount(JNIEnv *env, jclass cls)
1841 {
1842         log_println("JVM_GetClassFieldsCount: IMPLEMENT ME!");
1843 }
1844
1845
1846 /* JVM_GetClassMethodsCount */
1847
1848 jint JVM_GetClassMethodsCount(JNIEnv *env, jclass cls)
1849 {
1850         log_println("JVM_GetClassMethodsCount: IMPLEMENT ME!");
1851 }
1852
1853
1854 /* JVM_GetMethodIxExceptionIndexes */
1855
1856 void JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions)
1857 {
1858         log_println("JVM_GetMethodIxExceptionIndexes: IMPLEMENT ME!");
1859 }
1860
1861
1862 /* JVM_GetMethodIxExceptionsCount */
1863
1864 jint JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index)
1865 {
1866         log_println("JVM_GetMethodIxExceptionsCount: IMPLEMENT ME!");
1867 }
1868
1869
1870 /* JVM_GetMethodIxByteCode */
1871
1872 void JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code)
1873 {
1874         log_println("JVM_GetMethodIxByteCode: IMPLEMENT ME!");
1875 }
1876
1877
1878 /* JVM_GetMethodIxByteCodeLength */
1879
1880 jint JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index)
1881 {
1882         log_println("JVM_GetMethodIxByteCodeLength: IMPLEMENT ME!");
1883 }
1884
1885
1886 /* JVM_GetMethodIxExceptionTableEntry */
1887
1888 void JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry)
1889 {
1890         log_println("JVM_GetMethodIxExceptionTableEntry: IMPLEMENT ME!");
1891 }
1892
1893
1894 /* JVM_GetMethodIxExceptionTableLength */
1895
1896 jint JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index)
1897 {
1898         log_println("JVM_GetMethodIxExceptionTableLength: IMPLEMENT ME!");
1899 }
1900
1901
1902 /* JVM_GetMethodIxModifiers */
1903
1904 jint JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index)
1905 {
1906         log_println("JVM_GetMethodIxModifiers: IMPLEMENT ME!");
1907 }
1908
1909
1910 /* JVM_GetFieldIxModifiers */
1911
1912 jint JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index)
1913 {
1914         log_println("JVM_GetFieldIxModifiers: IMPLEMENT ME!");
1915 }
1916
1917
1918 /* JVM_GetMethodIxLocalsCount */
1919
1920 jint JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index)
1921 {
1922         log_println("JVM_GetMethodIxLocalsCount: IMPLEMENT ME!");
1923 }
1924
1925
1926 /* JVM_GetMethodIxArgsSize */
1927
1928 jint JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index)
1929 {
1930         log_println("JVM_GetMethodIxArgsSize: IMPLEMENT ME!");
1931 }
1932
1933
1934 /* JVM_GetMethodIxMaxStack */
1935
1936 jint JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index)
1937 {
1938         log_println("JVM_GetMethodIxMaxStack: IMPLEMENT ME!");
1939 }
1940
1941
1942 /* JVM_IsConstructorIx */
1943
1944 jboolean JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index)
1945 {
1946         log_println("JVM_IsConstructorIx: IMPLEMENT ME!");
1947 }
1948
1949
1950 /* JVM_GetMethodIxNameUTF */
1951
1952 const char* JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index)
1953 {
1954         log_println("JVM_GetMethodIxNameUTF: IMPLEMENT ME!");
1955 }
1956
1957
1958 /* JVM_GetMethodIxSignatureUTF */
1959
1960 const char* JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index)
1961 {
1962         log_println("JVM_GetMethodIxSignatureUTF: IMPLEMENT ME!");
1963 }
1964
1965
1966 /* JVM_GetCPFieldNameUTF */
1967
1968 const char* JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index)
1969 {
1970         log_println("JVM_GetCPFieldNameUTF: IMPLEMENT ME!");
1971 }
1972
1973
1974 /* JVM_GetCPMethodNameUTF */
1975
1976 const char* JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index)
1977 {
1978         log_println("JVM_GetCPMethodNameUTF: IMPLEMENT ME!");
1979 }
1980
1981
1982 /* JVM_GetCPMethodSignatureUTF */
1983
1984 const char* JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)
1985 {
1986         log_println("JVM_GetCPMethodSignatureUTF: IMPLEMENT ME!");
1987 }
1988
1989
1990 /* JVM_GetCPFieldSignatureUTF */
1991
1992 const char* JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)
1993 {
1994         log_println("JVM_GetCPFieldSignatureUTF: IMPLEMENT ME!");
1995 }
1996
1997
1998 /* JVM_GetCPClassNameUTF */
1999
2000 const char* JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)
2001 {
2002         log_println("JVM_GetCPClassNameUTF: IMPLEMENT ME!");
2003 }
2004
2005
2006 /* JVM_GetCPFieldClassNameUTF */
2007
2008 const char* JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)
2009 {
2010         log_println("JVM_GetCPFieldClassNameUTF: IMPLEMENT ME!");
2011 }
2012
2013
2014 /* JVM_GetCPMethodClassNameUTF */
2015
2016 const char* JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)
2017 {
2018         log_println("JVM_GetCPMethodClassNameUTF: IMPLEMENT ME!");
2019 }
2020
2021
2022 /* JVM_GetCPFieldModifiers */
2023
2024 jint JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)
2025 {
2026         log_println("JVM_GetCPFieldModifiers: IMPLEMENT ME!");
2027 }
2028
2029
2030 /* JVM_GetCPMethodModifiers */
2031
2032 jint JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)
2033 {
2034         log_println("JVM_GetCPMethodModifiers: IMPLEMENT ME!");
2035 }
2036
2037
2038 /* JVM_ReleaseUTF */
2039
2040 void JVM_ReleaseUTF(const char *utf)
2041 {
2042         log_println("JVM_ReleaseUTF: IMPLEMENT ME!");
2043 }
2044
2045
2046 /* JVM_IsSameClassPackage */
2047
2048 jboolean JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2)
2049 {
2050         log_println("JVM_IsSameClassPackage: IMPLEMENT ME!");
2051 }
2052
2053
2054 /* JVM_Open */
2055
2056 jint JVM_Open(const char *fname, jint flags, jint mode)
2057 {
2058         int result;
2059
2060         TRACEJVMCALLS("JVM_Open(fname=%s, flags=%d, mode=%d)", fname, flags, mode);
2061
2062         result = system_open(fname, flags, mode);
2063
2064         if (result >= 0) {
2065                 return result;
2066         }
2067         else {
2068                 switch(errno) {
2069                 case EEXIST:
2070                         /* XXX don't know what to do here */
2071 /*                      return JVM_EEXIST; */
2072                         return -1;
2073                 default:
2074                         return -1;
2075                 }
2076         }
2077 }
2078
2079
2080 /* JVM_Close */
2081
2082 jint JVM_Close(jint fd)
2083 {
2084         TRACEJVMCALLS("JVM_Close(fd=%d)", fd);
2085
2086         return system_close(fd);
2087 }
2088
2089
2090 /* JVM_Read */
2091
2092 jint JVM_Read(jint fd, char *buf, jint nbytes)
2093 {
2094         TRACEJVMCALLS("JVM_Read(fd=%d, buf=%p, nbytes=%d)", fd, buf, nbytes);
2095
2096         return system_read(fd, buf, nbytes);
2097 }
2098
2099
2100 /* JVM_Write */
2101
2102 jint JVM_Write(jint fd, char *buf, jint nbytes)
2103 {
2104         TRACEJVMCALLS("JVM_Write(fd=%d, buf=%s, nbytes=%d)", fd, buf, nbytes);
2105
2106         return system_write(fd, buf, nbytes);
2107 }
2108
2109
2110 /* JVM_Available */
2111
2112 jint JVM_Available(jint fd, jlong *pbytes)
2113 {
2114 #if defined(FIONREAD)
2115         int bytes;
2116         int result;
2117
2118         TRACEJVMCALLS("JVM_Available(fd=%d, pbytes=%p)", fd, pbytes);
2119
2120         *pbytes = 0;
2121
2122         result = ioctl(fd, FIONREAD, &bytes);
2123
2124         if (result < 0)
2125                 return 0;
2126
2127         *pbytes = bytes;
2128
2129         return 1;
2130 #else
2131 # error FIONREAD not defined
2132 #endif
2133 }
2134
2135
2136 /* JVM_Lseek */
2137
2138 jlong JVM_Lseek(jint fd, jlong offset, jint whence)
2139 {
2140         TRACEJVMCALLS("JVM_Lseek(fd=%d, offset=%ld, whence=%d)", fd, offset, whence);
2141
2142         return (jlong) lseek(fd, (off_t) offset, whence);
2143 }
2144
2145
2146 /* JVM_SetLength */
2147
2148 jint JVM_SetLength(jint fd, jlong length)
2149 {
2150         TRACEJVMCALLS("JVM_SetLength(fd=%d, length=%ld)", length);
2151
2152         return ftruncate(fd, length);
2153 }
2154
2155
2156 /* JVM_Sync */
2157
2158 jint JVM_Sync(jint fd)
2159 {
2160         TRACEJVMCALLS("JVM_Sync(fd=%d)", fd);
2161
2162         return fsync(fd);
2163 }
2164
2165
2166 /* JVM_StartThread */
2167
2168 void JVM_StartThread(JNIEnv* env, jobject jthread)
2169 {
2170         TRACEJVMCALLS("JVM_StartThread(env=%p, jthread=%p)", env, jthread);
2171
2172         _Jv_java_lang_Thread_start((java_lang_Thread *) jthread, 0);
2173 }
2174
2175
2176 /* JVM_StopThread */
2177
2178 void JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable)
2179 {
2180         log_println("JVM_StopThread: IMPLEMENT ME!");
2181 }
2182
2183
2184 /* JVM_IsThreadAlive */
2185
2186 jboolean JVM_IsThreadAlive(JNIEnv* env, jobject jthread)
2187 {
2188         threadobject *t;
2189         bool          equal;
2190         bool          result;
2191
2192         TRACEJVMCALLS("JVM_IsThreadAlive(env=%p, jthread=%p)", env, jthread);
2193
2194         /* XXX this is just a quick hack */
2195
2196         for (t = threads_list_first(); t != NULL; t = threads_list_next(t)) {
2197                 LLNI_equals(t->object, jthread, equal);
2198
2199                 if (equal == true)
2200                         break;
2201         }
2202
2203         /* The threadobject is null when a thread is created in Java. The
2204            priority is set later during startup. */
2205
2206         if (t == NULL)
2207                 return 0;
2208
2209         result = threads_thread_is_alive(t);
2210
2211         return result;
2212 }
2213
2214
2215 /* JVM_SuspendThread */
2216
2217 void JVM_SuspendThread(JNIEnv* env, jobject jthread)
2218 {
2219         log_println("JVM_SuspendThread: IMPLEMENT ME!");
2220 }
2221
2222
2223 /* JVM_ResumeThread */
2224
2225 void JVM_ResumeThread(JNIEnv* env, jobject jthread)
2226 {
2227         log_println("JVM_ResumeThread: IMPLEMENT ME!");
2228 }
2229
2230
2231 /* JVM_SetThreadPriority */
2232
2233 void JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio)
2234 {
2235         TRACEJVMCALLS("JVM_SetThreadPriority(env=%p, jthread=%p, prio=%d)", env, jthread, prio);
2236
2237         _Jv_java_lang_Thread_setPriority((java_lang_Thread *) jthread, prio);
2238 }
2239
2240
2241 /* JVM_Yield */
2242
2243 void JVM_Yield(JNIEnv *env, jclass threadClass)
2244 {
2245         TRACEJVMCALLS("JVM_Yield(env=%p, threadClass=%p)", env, threadClass);
2246
2247         threads_yield();
2248 }
2249
2250
2251 /* JVM_Sleep */
2252
2253 void JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis)
2254 {
2255         TRACEJVMCALLS("JVM_Sleep(env=%p, threadClass=%p, millis=%ld)", env, threadClass, millis);
2256
2257         _Jv_java_lang_Thread_sleep(millis);
2258 }
2259
2260
2261 /* JVM_CurrentThread */
2262
2263 jobject JVM_CurrentThread(JNIEnv* env, jclass threadClass)
2264 {
2265         TRACEJVMCALLSVERBOSE(("JVM_CurrentThread(env=%p, threadClass=%p)", env, threadClass));
2266
2267         return (jobject) _Jv_java_lang_Thread_currentThread();
2268 }
2269
2270
2271 /* JVM_CountStackFrames */
2272
2273 jint JVM_CountStackFrames(JNIEnv* env, jobject jthread)
2274 {
2275         log_println("JVM_CountStackFrames: IMPLEMENT ME!");
2276 }
2277
2278
2279 /* JVM_Interrupt */
2280
2281 void JVM_Interrupt(JNIEnv* env, jobject jthread)
2282 {
2283         log_println("JVM_Interrupt: IMPLEMENT ME!");
2284 }
2285
2286
2287 /* JVM_IsInterrupted */
2288
2289 jboolean JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted)
2290 {
2291         TRACEJVMCALLS("JVM_IsInterrupted(env=%p, jthread=%p, clear_interrupted=%d)", env, jthread, clear_interrupted);
2292
2293         /* XXX do something with clear_interrupted */
2294
2295         return _Jv_java_lang_Thread_isInterrupted((java_lang_Thread *) jthread);
2296 }
2297
2298
2299 /* JVM_HoldsLock */
2300
2301 jboolean JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj)
2302 {
2303         java_handle_t *h;
2304         bool           result;
2305
2306         TRACEJVMCALLS("JVM_HoldsLock(env=%p, threadClass=%p, obj=%p)", env, threadClass, obj);
2307
2308         h = (java_handle_t *) obj;
2309
2310         if (h == NULL) {
2311                 exceptions_throw_nullpointerexception();
2312                 return JNI_FALSE;
2313         }
2314
2315         result = lock_is_held_by_current_thread(h);
2316
2317         return result;
2318 }
2319
2320
2321 /* JVM_DumpAllStacks */
2322
2323 void JVM_DumpAllStacks(JNIEnv* env, jclass unused)
2324 {
2325         log_println("JVM_DumpAllStacks: IMPLEMENT ME!");
2326 }
2327
2328
2329 /* JVM_CurrentLoadedClass */
2330
2331 jclass JVM_CurrentLoadedClass(JNIEnv *env)
2332 {
2333         log_println("JVM_CurrentLoadedClass: IMPLEMENT ME!");
2334 }
2335
2336
2337 /* JVM_CurrentClassLoader */
2338
2339 jobject JVM_CurrentClassLoader(JNIEnv *env)
2340 {
2341     /* XXX if a method in a class in a trusted loader is in a
2342            doPrivileged, return NULL */
2343
2344         log_println("JVM_CurrentClassLoader: IMPLEMENT ME!");
2345 }
2346
2347
2348 /* JVM_GetClassContext */
2349
2350 jobjectArray JVM_GetClassContext(JNIEnv *env)
2351 {
2352         TRACEJVMCALLS("JVM_GetClassContext(env=%p)", env);
2353
2354         return (jobjectArray) stacktrace_getClassContext();
2355 }
2356
2357
2358 /* JVM_ClassDepth */
2359
2360 jint JVM_ClassDepth(JNIEnv *env, jstring name)
2361 {
2362         log_println("JVM_ClassDepth: IMPLEMENT ME!");
2363 }
2364
2365
2366 /* JVM_ClassLoaderDepth */
2367
2368 jint JVM_ClassLoaderDepth(JNIEnv *env)
2369 {
2370         log_println("JVM_ClassLoaderDepth: IMPLEMENT ME!");
2371 }
2372
2373
2374 /* JVM_GetSystemPackage */
2375
2376 jstring JVM_GetSystemPackage(JNIEnv *env, jstring name)
2377 {
2378         java_handle_t *s;
2379         utf *u;
2380         utf *result;
2381
2382         TRACEJVMCALLS("JVM_GetSystemPackage(env=%p, name=%p)", env, name);
2383
2384 /*      s = package_find(name); */
2385         u = javastring_toutf(name, false);
2386         result = package_find(u);
2387         if (result != NULL)
2388                 s = javastring_new(result);
2389         else
2390                 s = NULL;
2391
2392         return (jstring) s;
2393 }
2394
2395
2396 /* JVM_GetSystemPackages */
2397
2398 jobjectArray JVM_GetSystemPackages(JNIEnv *env)
2399 {
2400         log_println("JVM_GetSystemPackages: IMPLEMENT ME!");
2401 }
2402
2403
2404 /* JVM_AllocateNewObject */
2405
2406 jobject JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass)
2407 {
2408         log_println("JVM_AllocateNewObject: IMPLEMENT ME!");
2409 }
2410
2411
2412 /* JVM_AllocateNewArray */
2413
2414 jobject JVM_AllocateNewArray(JNIEnv *env, jobject obj, jclass currClass, jint length)
2415 {
2416         log_println("JVM_AllocateNewArray: IMPLEMENT ME!");
2417 }
2418
2419
2420 /* JVM_LatestUserDefinedLoader */
2421
2422 jobject JVM_LatestUserDefinedLoader(JNIEnv *env)
2423 {
2424         classloader *cl;
2425
2426         TRACEJVMCALLS("JVM_LatestUserDefinedLoader(env=%p)", env);
2427
2428         cl = stacktrace_first_nonnull_classloader();
2429
2430         return (jobject) cl;
2431 }
2432
2433
2434 /* JVM_LoadClass0 */
2435
2436 jclass JVM_LoadClass0(JNIEnv *env, jobject receiver, jclass currClass, jstring currClassName)
2437 {
2438         log_println("JVM_LoadClass0: IMPLEMENT ME!");
2439 }
2440
2441
2442 /* JVM_GetArrayLength */
2443
2444 jint JVM_GetArrayLength(JNIEnv *env, jobject arr)
2445 {
2446         java_handle_t *a;
2447
2448         TRACEJVMCALLS("JVM_GetArrayLength(arr=%p)", arr);
2449
2450         a = (java_handle_t *) arr;
2451
2452         return array_length_get(a);
2453 }
2454
2455
2456 /* JVM_GetArrayElement */
2457
2458 jobject JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index)
2459 {
2460         java_handle_t *a;
2461         java_handle_t *o;
2462
2463         TRACEJVMCALLS("JVM_GetArrayElement(env=%p, arr=%p, index=%d)", env, arr, index);
2464
2465         a = (java_handle_t *) arr;
2466
2467 /*      if (!class_is_array(a->objheader.vftbl->class)) { */
2468 /*              exceptions_throw_illegalargumentexception(); */
2469 /*              return NULL; */
2470 /*      } */
2471
2472         o = array_element_get(a, index);
2473
2474         return (jobject) o;
2475 }
2476
2477
2478 /* JVM_GetPrimitiveArrayElement */
2479
2480 jvalue JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode)
2481 {
2482         log_println("JVM_GetPrimitiveArrayElement: IMPLEMENT ME!");
2483 }
2484
2485
2486 /* JVM_SetArrayElement */
2487
2488 void JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val)
2489 {
2490         java_handle_t *a;
2491         java_handle_t *value;
2492
2493         TRACEJVMCALLS("JVM_SetArrayElement(env=%p, arr=%p, index=%d, val=%p)", env, arr, index, val);
2494
2495         a     = (java_handle_t *) arr;
2496         value = (java_handle_t *) val;
2497
2498         array_element_set(a, index, value);
2499 }
2500
2501
2502 /* JVM_SetPrimitiveArrayElement */
2503
2504 void JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode)
2505 {
2506         log_println("JVM_SetPrimitiveArrayElement: IMPLEMENT ME!");
2507 }
2508
2509
2510 /* JVM_NewArray */
2511
2512 jobject JVM_NewArray(JNIEnv *env, jclass eltClass, jint length)
2513 {
2514         classinfo                 *c;
2515         classinfo                 *pc;
2516         java_handle_t             *a;
2517         java_handle_objectarray_t *oa;
2518
2519         TRACEJVMCALLS("JVM_NewArray(env=%p, eltClass=%p, length=%d)", env, eltClass, length);
2520
2521         if (eltClass == NULL) {
2522                 exceptions_throw_nullpointerexception();
2523                 return NULL;
2524         }
2525
2526         /* NegativeArraySizeException is checked in builtin_newarray. */
2527
2528         c = LLNI_classinfo_unwrap(eltClass);
2529
2530         /* create primitive or object array */
2531
2532         if (class_is_primitive(c)) {
2533                 pc = primitive_arrayclass_get_by_name(c->name);
2534                 a = builtin_newarray(length, pc);
2535
2536                 return (jobject) a;
2537         }
2538         else {
2539                 oa = builtin_anewarray(length, c);
2540
2541                 return (jobject) oa;
2542         }
2543 }
2544
2545
2546 /* JVM_NewMultiArray */
2547
2548 jobject JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim)
2549 {
2550         classinfo                 *c;
2551         java_handle_intarray_t    *ia;
2552         int32_t                    length;
2553         long                      *dims;
2554         int32_t                    value;
2555         int32_t                    i;
2556         classinfo                 *ac;
2557         java_handle_objectarray_t *a;
2558
2559         TRACEJVMCALLS("JVM_NewMultiArray(env=%p, eltClass=%p, dim=%p)", env, eltClass, dim);
2560
2561         if (eltClass == NULL) {
2562                 exceptions_throw_nullpointerexception();
2563                 return NULL;
2564         }
2565
2566         /* NegativeArraySizeException is checked in builtin_newarray. */
2567
2568         c = LLNI_classinfo_unwrap(eltClass);
2569
2570         /* XXX This is just a quick hack to get it working. */
2571
2572         ia = (java_handle_intarray_t *) dim;
2573
2574         length = array_length_get(ia);
2575
2576         dims = MNEW(long, length);
2577
2578         for (i = 0; i < length; i++) {
2579                 value = LLNI_array_direct(ia, i);
2580                 dims[i] = (long) value;
2581         }
2582
2583         /* Create an array-class if necessary. */
2584
2585         if (class_is_primitive(c))
2586                 ac = primitive_arrayclass_get_by_name(c->name);
2587         else
2588                 ac = class_array_of(c, true);
2589
2590         if (ac == NULL)
2591                 return NULL;
2592
2593         a = builtin_multianewarray(length, ac, dims);
2594
2595         return (jobject) a;
2596 }
2597
2598
2599 /* JVM_InitializeSocketLibrary */
2600
2601 jint JVM_InitializeSocketLibrary()
2602 {
2603         log_println("JVM_InitializeSocketLibrary: IMPLEMENT ME!");
2604 }
2605
2606
2607 /* JVM_Socket */
2608
2609 jint JVM_Socket(jint domain, jint type, jint protocol)
2610 {
2611         TRACEJVMCALLS("JVM_Socket(domain=%d, type=%d, protocol=%d)", domain, type, protocol);
2612
2613         return socket(domain, type, protocol);
2614 }
2615
2616
2617 /* JVM_SocketClose */
2618
2619 jint JVM_SocketClose(jint fd)
2620 {
2621         TRACEJVMCALLS("JVM_SocketClose(fd=%d)", fd);
2622
2623         return close(fd);
2624 }
2625
2626
2627 /* JVM_SocketShutdown */
2628
2629 jint JVM_SocketShutdown(jint fd, jint howto)
2630 {
2631         TRACEJVMCALLS("JVM_SocketShutdown(fd=%d, howto=%d)", fd, howto);
2632
2633         return shutdown(fd, howto);
2634 }
2635
2636
2637 /* JVM_Recv */
2638
2639 jint JVM_Recv(jint fd, char *buf, jint nBytes, jint flags)
2640 {
2641         log_println("JVM_Recv: IMPLEMENT ME!");
2642 }
2643
2644
2645 /* JVM_Send */
2646
2647 jint JVM_Send(jint fd, char *buf, jint nBytes, jint flags)
2648 {
2649         log_println("JVM_Send: IMPLEMENT ME!");
2650 }
2651
2652
2653 /* JVM_Timeout */
2654
2655 jint JVM_Timeout(int fd, long timeout)
2656 {
2657         log_println("JVM_Timeout: IMPLEMENT ME!");
2658 }
2659
2660
2661 /* JVM_Listen */
2662
2663 jint JVM_Listen(jint fd, jint count)
2664 {
2665         TRACEJVMCALLS("JVM_Listen(fd=%d, count=%d)", fd, count);
2666
2667         return listen(fd, count);
2668 }
2669
2670
2671 /* JVM_Connect */
2672
2673 jint JVM_Connect(jint fd, struct sockaddr *him, jint len)
2674 {
2675         TRACEJVMCALLS("JVM_Connect(fd=%d, him=%p, len=%d)", fd, him, len);
2676
2677         return connect(fd, him, len);
2678 }
2679
2680
2681 /* JVM_Bind */
2682
2683 jint JVM_Bind(jint fd, struct sockaddr *him, jint len)
2684 {
2685         log_println("JVM_Bind: IMPLEMENT ME!");
2686 }
2687
2688
2689 /* JVM_Accept */
2690
2691 jint JVM_Accept(jint fd, struct sockaddr *him, jint *len)
2692 {
2693         TRACEJVMCALLS("JVM_Accept(fd=%d, him=%p, len=%p)", fd, him, len);
2694
2695         return accept(fd, him, (socklen_t *) len);
2696 }
2697
2698
2699 /* JVM_RecvFrom */
2700
2701 jint JVM_RecvFrom(jint fd, char *buf, int nBytes, int flags, struct sockaddr *from, int *fromlen)
2702 {
2703         log_println("JVM_RecvFrom: IMPLEMENT ME!");
2704 }
2705
2706
2707 /* JVM_GetSockName */
2708
2709 jint JVM_GetSockName(jint fd, struct sockaddr *him, int *len)
2710 {
2711         TRACEJVMCALLS("JVM_GetSockName(fd=%d, him=%p, len=%p)", fd, him, len);
2712
2713         return getsockname(fd, him, (socklen_t *) len);
2714 }
2715
2716
2717 /* JVM_SendTo */
2718
2719 jint JVM_SendTo(jint fd, char *buf, int len, int flags, struct sockaddr *to, int tolen)
2720 {
2721         log_println("JVM_SendTo: IMPLEMENT ME!");
2722 }
2723
2724
2725 /* JVM_SocketAvailable */
2726
2727 jint JVM_SocketAvailable(jint fd, jint *pbytes)
2728 {
2729 #if defined(FIONREAD)
2730         int bytes;
2731         int result;
2732
2733         TRACEJVMCALLS("JVM_SocketAvailable(fd=%d, pbytes=%p)", fd, pbytes);
2734
2735         *pbytes = 0;
2736
2737         result = ioctl(fd, FIONREAD, &bytes);
2738
2739         if (result < 0)
2740                 return 0;
2741
2742         *pbytes = bytes;
2743
2744         return 1;
2745 #else
2746 # error FIONREAD not defined
2747 #endif
2748 }
2749
2750
2751 /* JVM_GetSockOpt */
2752
2753 jint JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen)
2754 {
2755 #if defined(HAVE_GETSOCKOPT)
2756         TRACEJVMCALLS("JVM_GetSockOpt(fd=%d, level=%d, optname=%d, optval=%s, optlen=%p)", fd, level, optname, optval, optlen);
2757
2758         return getsockopt(fd, level, optname, optval, (socklen_t *) optlen);
2759 #else
2760 # error getsockopt not available
2761 #endif
2762 }
2763
2764
2765 /* JVM_SetSockOpt */
2766
2767 jint JVM_SetSockOpt(jint fd, int level, int optname, const char *optval, int optlen)
2768 {
2769 #if defined(HAVE_SETSOCKOPT)
2770         TRACEJVMCALLS("JVM_SetSockOpt(fd=%d, level=%d, optname=%d, optval=%s, optlen=%d)", fd, level, optname, optval, optlen);
2771
2772         return setsockopt(fd, level, optname, optval, optlen);
2773 #else
2774 # error setsockopt not available
2775 #endif
2776 }
2777
2778
2779 /* JVM_GetHostName */
2780
2781 int JVM_GetHostName(char* name, int namelen)
2782 {
2783         TRACEJVMCALLS("JVM_GetHostName(name=%s, namelen=%d)", name, namelen);
2784
2785         return gethostname(name, namelen);
2786 }
2787
2788
2789 /* JVM_GetHostByAddr */
2790
2791 struct hostent* JVM_GetHostByAddr(const char* name, int len, int type)
2792 {
2793         log_println("JVM_GetHostByAddr: IMPLEMENT ME!");
2794 }
2795
2796
2797 /* JVM_GetHostByName */
2798
2799 struct hostent* JVM_GetHostByName(char* name)
2800 {
2801         log_println("JVM_GetHostByName: IMPLEMENT ME!");
2802 }
2803
2804
2805 /* JVM_GetProtoByName */
2806
2807 struct protoent* JVM_GetProtoByName(char* name)
2808 {
2809         log_println("JVM_GetProtoByName: IMPLEMENT ME!");
2810 }
2811
2812
2813 /* JVM_LoadLibrary */
2814
2815 void *JVM_LoadLibrary(const char *name)
2816 {
2817         utf *u;
2818
2819         TRACEJVMCALLS("JVM_LoadLibrary(name=%s)", name);
2820
2821         u = utf_new_char(name);
2822
2823         return native_library_open(u);
2824 }
2825
2826
2827 /* JVM_UnloadLibrary */
2828
2829 void JVM_UnloadLibrary(void* handle)
2830 {
2831         TRACEJVMCALLS("JVM_UnloadLibrary(handle=%p)", handle);
2832
2833         native_library_close(handle);
2834 }
2835
2836
2837 /* JVM_FindLibraryEntry */
2838
2839 void *JVM_FindLibraryEntry(void *handle, const char *name)
2840 {
2841         lt_ptr symbol;
2842
2843         TRACEJVMCALLS("JVM_FindLibraryEntry(handle=%p, name=%s)", handle, name);
2844
2845         symbol = lt_dlsym(handle, name);
2846
2847         return symbol;
2848 }
2849
2850
2851 /* JVM_IsNaN */
2852
2853 jboolean JVM_IsNaN(jdouble a)
2854 {
2855         log_println("JVM_IsNaN: IMPLEMENT ME!");
2856 }
2857
2858
2859 /* JVM_IsSupportedJNIVersion */
2860
2861 jboolean JVM_IsSupportedJNIVersion(jint version)
2862 {
2863         TRACEJVMCALLS("JVM_IsSupportedJNIVersion(version=%d)", version);
2864
2865         return jni_version_check(version);
2866 }
2867
2868
2869 /* JVM_InternString */
2870
2871 jstring JVM_InternString(JNIEnv *env, jstring str)
2872 {
2873         TRACEJVMCALLS("JVM_InternString(env=%p, str=%p)", env, str);
2874
2875         return (jstring) javastring_intern((java_handle_t *) str);
2876 }
2877
2878
2879 /* JVM_RawMonitorCreate */
2880
2881 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void)
2882 {
2883         java_object_t *o;
2884
2885         TRACEJVMCALLS("JVM_RawMonitorCreate()");
2886
2887         o = NEW(java_object_t);
2888
2889         lock_init_object_lock(o);
2890
2891         return o;
2892 }
2893
2894
2895 /* JVM_RawMonitorDestroy */
2896
2897 JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon)
2898 {
2899         TRACEJVMCALLSlog_println("JVM_RawMonitorDestroy(mon=%p)", mon);
2900
2901         FREE(mon, java_object_t);
2902 }
2903
2904
2905 /* JVM_RawMonitorEnter */
2906
2907 JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon)
2908 {
2909         TRACEJVMCALLS("JVM_RawMonitorEnter(mon=%p)", mon);
2910
2911         (void) lock_monitor_enter((java_object_t *) mon);
2912
2913         return 0;
2914 }
2915
2916
2917 /* JVM_RawMonitorExit */
2918
2919 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon)
2920 {
2921         TRACEJVMCALLS("JVM_RawMonitorExit(mon=%p)", mon);
2922
2923         (void) lock_monitor_exit((java_object_t *) mon);
2924 }
2925
2926
2927 /* JVM_SetPrimitiveFieldValues */
2928
2929 void JVM_SetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)
2930 {
2931         log_println("JVM_SetPrimitiveFieldValues: IMPLEMENT ME!");
2932 }
2933
2934
2935 /* JVM_GetPrimitiveFieldValues */
2936
2937 void JVM_GetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)
2938 {
2939         log_println("JVM_GetPrimitiveFieldValues: IMPLEMENT ME!");
2940 }
2941
2942
2943 /* JVM_AccessVMBooleanFlag */
2944
2945 jboolean JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get)
2946 {
2947         log_println("JVM_AccessVMBooleanFlag: IMPLEMENT ME!");
2948 }
2949
2950
2951 /* JVM_AccessVMIntFlag */
2952
2953 jboolean JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get)
2954 {
2955         log_println("JVM_AccessVMIntFlag: IMPLEMENT ME!");
2956 }
2957
2958
2959 /* JVM_VMBreakPoint */
2960
2961 void JVM_VMBreakPoint(JNIEnv *env, jobject obj)
2962 {
2963         log_println("JVM_VMBreakPoint: IMPLEMENT ME!");
2964 }
2965
2966
2967 /* JVM_GetClassFields */
2968
2969 jobjectArray JVM_GetClassFields(JNIEnv *env, jclass cls, jint which)
2970 {
2971         log_println("JVM_GetClassFields: IMPLEMENT ME!");
2972 }
2973
2974
2975 /* JVM_GetClassMethods */
2976
2977 jobjectArray JVM_GetClassMethods(JNIEnv *env, jclass cls, jint which)
2978 {
2979         log_println("JVM_GetClassMethods: IMPLEMENT ME!");
2980 }
2981
2982
2983 /* JVM_GetClassConstructors */
2984
2985 jobjectArray JVM_GetClassConstructors(JNIEnv *env, jclass cls, jint which)
2986 {
2987         log_println("JVM_GetClassConstructors: IMPLEMENT ME!");
2988 }
2989
2990
2991 /* JVM_GetClassField */
2992
2993 jobject JVM_GetClassField(JNIEnv *env, jclass cls, jstring name, jint which)
2994 {
2995         log_println("JVM_GetClassField: IMPLEMENT ME!");
2996 }
2997
2998
2999 /* JVM_GetClassMethod */
3000
3001 jobject JVM_GetClassMethod(JNIEnv *env, jclass cls, jstring name, jobjectArray types, jint which)
3002 {
3003         log_println("JVM_GetClassMethod: IMPLEMENT ME!");
3004 }
3005
3006
3007 /* JVM_GetClassConstructor */
3008
3009 jobject JVM_GetClassConstructor(JNIEnv *env, jclass cls, jobjectArray types, jint which)
3010 {
3011         log_println("JVM_GetClassConstructor: IMPLEMENT ME!");
3012 }
3013
3014
3015 /* JVM_NewInstance */
3016
3017 jobject JVM_NewInstance(JNIEnv *env, jclass cls)
3018 {
3019         log_println("JVM_NewInstance: IMPLEMENT ME!");
3020 }
3021
3022
3023 /* JVM_GetField */
3024
3025 jobject JVM_GetField(JNIEnv *env, jobject field, jobject obj)
3026 {
3027         log_println("JVM_GetField: IMPLEMENT ME!");
3028 }
3029
3030
3031 /* JVM_GetPrimitiveField */
3032
3033 jvalue JVM_GetPrimitiveField(JNIEnv *env, jobject field, jobject obj, unsigned char wCode)
3034 {
3035         log_println("JVM_GetPrimitiveField: IMPLEMENT ME!");
3036 }
3037
3038
3039 /* JVM_SetField */
3040
3041 void JVM_SetField(JNIEnv *env, jobject field, jobject obj, jobject val)
3042 {
3043         log_println("JVM_SetField: IMPLEMENT ME!");
3044 }
3045
3046
3047 /* JVM_SetPrimitiveField */
3048
3049 void JVM_SetPrimitiveField(JNIEnv *env, jobject field, jobject obj, jvalue v, unsigned char vCode)
3050 {
3051         log_println("JVM_SetPrimitiveField: IMPLEMENT ME!");
3052 }
3053
3054
3055 /* JVM_InvokeMethod */
3056
3057 jobject JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0)
3058 {
3059         TRACEJVMCALLS("JVM_InvokeMethod(env=%p, method=%p, obj=%p, args0=%p)", env, method, obj, args0);
3060
3061         return (jobject) _Jv_java_lang_reflect_Method_invoke((java_lang_reflect_Method *) method, (java_lang_Object *) obj, (java_handle_objectarray_t *) args0);
3062 }
3063
3064
3065 /* JVM_NewInstanceFromConstructor */
3066
3067 jobject JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0)
3068 {
3069         TRACEJVMCALLS("JVM_NewInstanceFromConstructor(env=%p, c=%p, args0=%p)", env, c, args0);
3070
3071         return (jobject) _Jv_java_lang_reflect_Constructor_newInstance(env, (java_lang_reflect_Constructor *) c, (java_handle_objectarray_t *) args0);
3072 }
3073
3074
3075 /* JVM_SupportsCX8 */
3076
3077 jboolean JVM_SupportsCX8()
3078 {
3079         TRACEJVMCALLS("JVM_SupportsCX8()");
3080
3081         /* IMPLEMENT ME */
3082
3083         return 0;
3084 }
3085
3086
3087 /* JVM_CX8Field */
3088
3089 jboolean JVM_CX8Field(JNIEnv *env, jobject obj, jfieldID fid, jlong oldVal, jlong newVal)
3090 {
3091         log_println("JVM_CX8Field: IMPLEMENT ME!");
3092 }
3093
3094
3095 /* JVM_GetAllThreads */
3096
3097 jobjectArray JVM_GetAllThreads(JNIEnv *env, jclass dummy)
3098 {
3099         log_println("JVM_GetAllThreads: IMPLEMENT ME!");
3100 }
3101
3102
3103 /* JVM_DumpThreads */
3104
3105 jobjectArray JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads)
3106 {
3107         log_println("JVM_DumpThreads: IMPLEMENT ME!");
3108 }
3109
3110
3111 /* JVM_GetManagement */
3112
3113 void *JVM_GetManagement(jint version)
3114 {
3115         TRACEJVMCALLS("JVM_GetManagement(version=%d)", version);
3116
3117         /* TODO We current don't support the management interface. */
3118
3119         return NULL;
3120 }
3121
3122
3123 /* JVM_InitAgentProperties */
3124
3125 jobject JVM_InitAgentProperties(JNIEnv *env, jobject properties)
3126 {
3127         log_println("JVM_InitAgentProperties: IMPLEMENT ME!");
3128 }
3129
3130
3131 /* JVM_GetEnclosingMethodInfo */
3132
3133 jobjectArray JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass)
3134 {
3135         classinfo                 *c;
3136         methodinfo                *m;
3137         java_handle_objectarray_t *oa;
3138
3139         TRACEJVMCALLS("JVM_GetEnclosingMethodInfo(env=%p, ofClass=%p)", env, ofClass);
3140
3141         c = LLNI_classinfo_unwrap(ofClass);
3142
3143         if ((c == NULL) || class_is_primitive(c))
3144                 return NULL;
3145
3146         m = class_get_enclosingmethod(c);
3147
3148         if (m == NULL)
3149                 return NULL;
3150
3151         oa = builtin_anewarray(3, class_java_lang_Object);
3152
3153         if (oa == NULL)
3154                 return NULL;
3155
3156         array_objectarray_element_set(oa, 0, (java_handle_t *) LLNI_classinfo_wrap(m->class));
3157         array_objectarray_element_set(oa, 1, javastring_new(m->name));
3158         array_objectarray_element_set(oa, 2, javastring_new(m->descriptor));
3159
3160         return (jobjectArray) oa;
3161 }
3162
3163
3164 /* JVM_GetThreadStateValues */
3165
3166 jintArray JVM_GetThreadStateValues(JNIEnv* env, jint javaThreadState)
3167 {
3168         java_handle_intarray_t *ia;
3169
3170         TRACEJVMCALLS("JVM_GetThreadStateValues(env=%p, javaThreadState=%d)",
3171                                   env, javaThreadState);
3172
3173         /* If new thread states are added in future JDK and VM versions,
3174            this should check if the JDK version is compatible with thread
3175            states supported by the VM.  Return NULL if not compatible.
3176         
3177            This function must map the VM java_lang_Thread::ThreadStatus
3178            to the Java thread state that the JDK supports. */
3179
3180         switch (javaThreadState) {
3181     case THREAD_STATE_NEW:
3182                 ia = builtin_newarray_int(1);
3183
3184                 if (ia == NULL)
3185                         return NULL;
3186
3187                 array_intarray_element_set(ia, 0, THREAD_STATE_NEW);
3188                 break; 
3189
3190     case THREAD_STATE_RUNNABLE:
3191                 ia = builtin_newarray_int(1);
3192
3193                 if (ia == NULL)
3194                         return NULL;
3195
3196                 array_intarray_element_set(ia, 0, THREAD_STATE_RUNNABLE);
3197                 break; 
3198
3199     case THREAD_STATE_BLOCKED:
3200                 ia = builtin_newarray_int(1);
3201
3202                 if (ia == NULL)
3203                         return NULL;
3204
3205                 array_intarray_element_set(ia, 0, THREAD_STATE_BLOCKED);
3206                 break; 
3207
3208     case THREAD_STATE_WAITING:
3209                 ia = builtin_newarray_int(2);
3210
3211                 if (ia == NULL)
3212                         return NULL;
3213
3214                 array_intarray_element_set(ia, 0, THREAD_STATE_WAITING);
3215                 /* XXX Implement parked stuff. */
3216 /*              array_intarray_element_set(ia, 1, PARKED); */
3217                 break; 
3218
3219     case THREAD_STATE_TIMED_WAITING:
3220                 ia = builtin_newarray_int(3);
3221
3222                 if (ia == NULL)
3223                         return NULL;
3224
3225                 /* XXX Not sure about that one. */
3226 /*              array_intarray_element_set(ia, 0, SLEEPING); */
3227                 array_intarray_element_set(ia, 0, THREAD_STATE_TIMED_WAITING);
3228                 /* XXX Implement parked stuff. */
3229 /*              array_intarray_element_set(ia, 2, PARKED); */
3230                 break; 
3231
3232     case THREAD_STATE_TERMINATED:
3233                 ia = builtin_newarray_int(1);
3234
3235                 if (ia == NULL)
3236                         return NULL;
3237
3238                 array_intarray_element_set(ia, 0, THREAD_STATE_TERMINATED);
3239                 break; 
3240
3241     default:
3242                 /* Unknown state - probably incompatible JDK version */
3243                 return NULL;
3244         }
3245
3246         return (jintArray) ia;
3247 }
3248
3249
3250 /* JVM_GetThreadStateNames */
3251
3252 jobjectArray JVM_GetThreadStateNames(JNIEnv* env, jint javaThreadState, jintArray values)
3253 {
3254         java_handle_intarray_t    *ia;
3255         java_handle_objectarray_t *oa;
3256         java_object_t             *s;
3257
3258         TRACEJVMCALLS("JVM_GetThreadStateNames(env=%p, javaThreadState=%d, values=%p)",
3259                                   env, javaThreadState, values);
3260
3261         ia = (java_handle_intarray_t *) values;
3262
3263         /* If new thread states are added in future JDK and VM versions,
3264            this should check if the JDK version is compatible with thread
3265            states supported by the VM.  Return NULL if not compatible.
3266         
3267            This function must map the VM java_lang_Thread::ThreadStatus
3268            to the Java thread state that the JDK supports. */
3269
3270         if (values == NULL) {
3271                 exceptions_throw_nullpointerexception();
3272                 return NULL;
3273         }
3274
3275         switch (javaThreadState) {
3276     case THREAD_STATE_NEW:
3277                 assert(ia->header.size == 1 && ia->data[0] == THREAD_STATE_NEW);
3278
3279                 oa = builtin_anewarray(1, class_java_lang_String);
3280
3281                 if (oa == NULL)
3282                         return NULL;
3283
3284                 s = javastring_new(utf_new_char("NEW"));
3285
3286                 if (s == NULL)
3287                         return NULL;
3288
3289                 array_objectarray_element_set(oa, 0, s);
3290                 break; 
3291
3292     case THREAD_STATE_RUNNABLE:
3293                 oa = builtin_anewarray(1, class_java_lang_String);
3294
3295                 if (oa == NULL)
3296                         return NULL;
3297
3298                 s = javastring_new(utf_new_char("RUNNABLE"));
3299
3300                 if (s == NULL)
3301                         return NULL;
3302
3303                 array_objectarray_element_set(oa, 0, s);
3304                 break; 
3305
3306     case THREAD_STATE_BLOCKED:
3307                 oa = builtin_anewarray(1, class_java_lang_String);
3308
3309                 if (oa == NULL)
3310                         return NULL;
3311
3312                 s = javastring_new(utf_new_char("BLOCKED"));
3313
3314                 if (s == NULL)
3315                         return NULL;
3316
3317                 array_objectarray_element_set(oa, 0, s);
3318                 break; 
3319
3320     case THREAD_STATE_WAITING:
3321                 oa = builtin_anewarray(2, class_java_lang_String);
3322
3323                 if (oa == NULL)
3324                         return NULL;
3325
3326                 s = javastring_new(utf_new_char("WAITING.OBJECT_WAIT"));
3327 /*              s = javastring_new(utf_new_char("WAITING.PARKED")); */
3328
3329                 if (s == NULL)
3330                         return NULL;
3331
3332                 array_objectarray_element_set(oa, 0, s);
3333 /*              array_objectarray_element_set(oa, 1, s); */
3334                 break; 
3335
3336     case THREAD_STATE_TIMED_WAITING:
3337                 oa = builtin_anewarray(3, class_java_lang_String);
3338
3339                 if (oa == NULL)
3340                         return NULL;
3341
3342 /*              s = javastring_new(utf_new_char("TIMED_WAITING.SLEEPING")); */
3343                 s = javastring_new(utf_new_char("TIMED_WAITING.OBJECT_WAIT"));
3344 /*              s = javastring_new(utf_new_char("TIMED_WAITING.PARKED")); */
3345
3346                 if (s == NULL)
3347                         return NULL;
3348
3349 /*              array_objectarray_element_set(oa, 0, s); */
3350                 array_objectarray_element_set(oa, 0, s);
3351 /*              array_objectarray_element_set(oa, 2, s); */
3352                 break; 
3353
3354     case THREAD_STATE_TERMINATED:
3355                 oa = builtin_anewarray(1, class_java_lang_String);
3356
3357                 if (oa == NULL)
3358                         return NULL;
3359
3360                 s = javastring_new(utf_new_char("TERMINATED"));
3361
3362                 if (s == NULL)
3363                         return NULL;
3364
3365                 array_objectarray_element_set(oa, 0, s);
3366                 break; 
3367
3368         default:
3369                 /* Unknown state - probably incompatible JDK version */
3370                 return NULL;
3371         }
3372
3373         return (jobjectArray) oa;
3374 }
3375
3376
3377 /* JVM_GetVersionInfo */
3378
3379 void JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size)
3380 {
3381         log_println("JVM_GetVersionInfo: IMPLEMENT ME!");
3382 }
3383
3384
3385 /* OS: JVM_RegisterSignal */
3386
3387 void *JVM_RegisterSignal(jint sig, void *handler)
3388 {
3389         functionptr newHandler;
3390
3391         TRACEJVMCALLS("JVM_RegisterSignal(sig=%d, handler=%p)", sig, handler);
3392
3393         if (handler == (void *) 2)
3394                 newHandler = (functionptr) signal_thread_handler;
3395         else
3396                 newHandler = (functionptr) (uintptr_t) handler;
3397
3398         switch (sig) {
3399     case SIGILL:
3400     case SIGFPE:
3401     case SIGUSR1:
3402     case SIGSEGV:
3403                 /* These signals are already used by the VM. */
3404                 return (void *) -1;
3405
3406     case SIGQUIT:
3407                 /* This signal is used by the VM to dump thread stacks unless
3408                    ReduceSignalUsage is set, in which case the user is allowed
3409                    to set his own _native_ handler for this signal; thus, in
3410                    either case, we do not allow JVM_RegisterSignal to change
3411                    the handler. */
3412                 return (void *) -1;
3413
3414         case SIGHUP:
3415         case SIGINT:
3416         case SIGTERM:
3417                 break;
3418         }
3419
3420         signal_register_signal(sig, newHandler, 0);
3421
3422         /* XXX Should return old handler. */
3423
3424         return (void *) 2;
3425 }
3426
3427
3428 /* OS: JVM_RaiseSignal */
3429
3430 jboolean JVM_RaiseSignal(jint sig)
3431 {
3432         log_println("JVM_RaiseSignal: IMPLEMENT ME! sig=%s", sig);
3433         return false;
3434 }
3435
3436
3437 /* OS: JVM_FindSignal */
3438
3439 jint JVM_FindSignal(const char *name)
3440 {
3441         TRACEJVMCALLS("JVM_FindSignal(name=%s)", name);
3442
3443 #if defined(__LINUX__)
3444         if (strcmp(name, "HUP") == 0)
3445                 return SIGHUP;
3446
3447         if (strcmp(name, "INT") == 0)
3448                 return SIGINT;
3449
3450         if (strcmp(name, "TERM") == 0)
3451                 return SIGTERM;
3452 #else
3453 # error not implemented for this OS
3454 #endif
3455
3456         return -1;
3457 }
3458
3459
3460 /*
3461  * These are local overrides for various environment variables in Emacs.
3462  * Please do not remove this and leave it at the end of the file, where
3463  * Emacs will automagically detect them.
3464  * ---------------------------------------------------------------------
3465  * Local variables:
3466  * mode: c
3467  * indent-tabs-mode: t
3468  * c-basic-offset: 4
3469  * tab-width: 4
3470  * End:
3471  */