1 /* -*- Mode: C++; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
13 #if defined(_WIN32) || defined(_WIN64)
15 #include <libloaderapi.h>
16 #elif defined(__APPLE__)
17 #include <mach-o/dyld.h>
19 #elif defined(__linux__) || defined(__linux)
22 #elif defined(__FreeBSD__)
24 #include <sys/sysctl.h>
29 #include "squirreljme.h"
31 #define RUNTIME_CLASSNAME "cc/squirreljme/emulator/EmulatedRuntimeShelf"
33 #define RUNTIME_MEMORYPROFILE_DESC "()I"
34 #define RUNTIME_SYSTEMENV_DESC "(Ljava/lang/String;)Ljava/lang/String;"
35 #define RUNTIME_VMDESCRIPTION_DESC "(I)Ljava/lang/String;"
36 #define RUNTIME_VMSTATISTIC_DESC "(I)J"
38 JNIEXPORT
void JNICALL
Impl_mle_RuntimeShelf_garbageCollect(
39 JNIEnv
* env
, jclass classy
)
44 JNIEXPORT jint JNICALL
Impl_mle_RuntimeShelf_lineEnding(
45 JNIEnv
* env
, jclass classy
)
54 JNIEXPORT jstring JNICALL
Impl_mle_RuntimeShelf_vmDescription(
55 JNIEnv
* env
, jclass classy
, jint id
)
57 #define NATIVE_EXEC_PATH_LEN 768
58 char fileName
[NATIVE_EXEC_PATH_LEN
];
60 #if defined(__APPLE__)
62 #elif defined(__FreeBSD__)
66 #elif defined(__sun) || defined(__illumos__)
70 // Executable path of the VM binary (EXECUTABLE_PATH)
74 memset(fileName
, 0, sizeof(fileName
));
76 // Use native system APIs to get this information
77 #if defined(_WIN32) || defined(_WIN64)
78 GetModuleFileNameA(NULL
, fileName
, NATIVE_EXEC_PATH_LEN
);
79 #elif defined(__APPLE__)
80 fileNameLen
= NATIVE_EXEC_PATH_LEN
;
81 _NSGetExecutablePath(fileName
, &fileNameLen
);
82 #elif defined(__linux__) || defined(__linux)
83 readlink("/proc/self/exe", fileName
, NATIVE_EXEC_PATH_LEN
);
84 #elif defined(__NetBSD__) || defined(__DragonFly__)
85 readlink("/proc/curproc/file", fileName
, NATIVE_EXEC_PATH_LEN
);
86 #elif defined(__FreeBSD__)
87 memset(&statBuf
, 0, sizeof(statBuf
));
88 if (stat("/proc/curproc/file", &statBuf
) == 0)
89 readlink("/proc/curproc/file", fileName
, NATIVE_EXEC_PATH_LEN
);
92 // Setup systemctl input
93 sysCtlInput
[0] = CTL_KERN
;
94 sysCtlInput
[1] = KERN_PROC
;
95 sysCtlInput
[2] = KERN_PROC_PATHNAME
;
99 fileNameLen
= NATIVE_EXEC_PATH_LEN
;
100 sysctl(mib
, 4, fileName
, &fileNameLen
, NULL
, 0);
102 #elif defined(__sun) || defined(__illumos__)
105 strncpy(fileName
, bip
, NATIVE_EXEC_PATH_LEN
- 1);
108 // Convert to Java String if Valid
109 if (fileName
[0] != 0)
111 fileName
[NATIVE_EXEC_PATH_LEN
- 1] = 0;
112 return (*env
)->NewStringUTF(env
, fileName
);
116 return (jstring
)forwardCallStaticObject(env
, RUNTIME_CLASSNAME
,
117 "vmDescription", RUNTIME_VMDESCRIPTION_DESC
,
119 #undef NATIVE_EXEC_PATH_LEN
122 JNIEXPORT jlong JNICALL
Impl_mle_RuntimeShelf_vmStatistic(
123 JNIEnv
* env
, jclass classy
, jint id
)
125 return forwardCallStaticLong(env
, RUNTIME_CLASSNAME
,
126 "vmStatistic", RUNTIME_VMSTATISTIC_DESC
,
130 JNIEXPORT jint JNICALL
Impl_mle_RuntimeShelf_memoryProfile(
131 JNIEnv
* env
, jclass classy
)
133 // The value is normal
137 JNIEXPORT jint JNICALL
Impl_mle_RuntimeShelf_phoneModel(
138 JNIEnv
* env
, jclass classy
)
140 // Just be a generic device here
144 JNIEXPORT jobject JNICALL
Impl_mle_RuntimeShelf_systemEnv(
145 JNIEnv
* env
, jclass classy
, jstring key
)
147 return forwardCallStaticObject(env
, RUNTIME_CLASSNAME
,
148 "systemEnv", RUNTIME_SYSTEMENV_DESC
,
152 JNIEXPORT jint JNICALL
Impl_mle_RuntimeShelf_vmType(
153 JNIEnv
* env
, jclass classy
)
155 // The value 1 is Java SE type
159 static const JNINativeMethod mleRuntimeMethods
[] =
161 {"garbageCollect", "()V", (void*)Impl_mle_RuntimeShelf_garbageCollect
},
162 {"lineEnding", "()I", (void*)Impl_mle_RuntimeShelf_lineEnding
},
163 {"memoryProfile", RUNTIME_MEMORYPROFILE_DESC
, (void*)Impl_mle_RuntimeShelf_memoryProfile
},
164 {"phoneModel", "()I", (void*)Impl_mle_RuntimeShelf_phoneModel
},
165 {"systemEnv", RUNTIME_SYSTEMENV_DESC
, (void*)Impl_mle_RuntimeShelf_systemEnv
},
166 {"vmDescription", RUNTIME_VMDESCRIPTION_DESC
, (void*)Impl_mle_RuntimeShelf_vmDescription
},
167 {"vmStatistic", RUNTIME_VMSTATISTIC_DESC
, (void*)Impl_mle_RuntimeShelf_vmStatistic
},
168 {"vmType", "()I", (void*)Impl_mle_RuntimeShelf_vmType
},
171 jint JNICALL
mleRuntimeInit(JNIEnv
* env
, jclass classy
)
173 return (*env
)->RegisterNatives(env
,
174 (*env
)->FindClass(env
, "cc/squirreljme/jvm/mle/RuntimeShelf"),
175 mleRuntimeMethods
, sizeof(mleRuntimeMethods
) /
176 sizeof(JNINativeMethod
));