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