-
Notifications
You must be signed in to change notification settings - Fork 814
/
javaApi.cpp
184 lines (153 loc) · 5.91 KB
/
javaApi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright The async-profiler authors
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <string.h>
#include "asprof.h"
#include "incbin.h"
#include "javaApi.h"
#include "os.h"
#include "profiler.h"
#include "vmStructs.h"
INCLUDE_HELPER_CLASS(SERVER_NAME, SERVER_CLASS, "one/profiler/Server")
static void throwNew(JNIEnv* env, const char* exception_class, const char* message) {
jclass cls = env->FindClass(exception_class);
if (cls != NULL) {
env->ThrowNew(cls, message);
}
}
extern "C" DLLEXPORT void JNICALL
Java_one_profiler_AsyncProfiler_start0(JNIEnv* env, jobject unused, jstring event, jlong interval, jboolean reset) {
Arguments args;
const char* event_str = env->GetStringUTFChars(event, NULL);
if (strcmp(event_str, EVENT_ALLOC) == 0) {
args._alloc = interval > 0 ? interval : 0;
} else if (strcmp(event_str, EVENT_LOCK) == 0) {
args._lock = interval > 0 ? interval : 0;
} else {
args._event = event_str;
args._interval = interval;
}
Error error = Profiler::instance()->start(args, reset);
env->ReleaseStringUTFChars(event, event_str);
if (error) {
throwNew(env, "java/lang/IllegalStateException", error.message());
}
}
extern "C" DLLEXPORT void JNICALL
Java_one_profiler_AsyncProfiler_stop0(JNIEnv* env, jobject unused) {
Error error = Profiler::instance()->stop();
if (error) {
throwNew(env, "java/lang/IllegalStateException", error.message());
}
}
extern "C" DLLEXPORT jstring JNICALL
Java_one_profiler_AsyncProfiler_execute0(JNIEnv* env, jobject unused, jstring command) {
Arguments args;
const char* command_str = env->GetStringUTFChars(command, NULL);
Error error = args.parse(command_str);
env->ReleaseStringUTFChars(command, command_str);
if (error) {
throwNew(env, "java/lang/IllegalArgumentException", error.message());
return NULL;
}
Log::open(args);
if (!args.hasOutputFile()) {
BufferWriter out;
error = Profiler::instance()->runInternal(args, out);
if (!error) {
out << '\0';
if (out.size() >= 0x3fffffff) {
throwNew(env, "java/lang/IllegalStateException", "Output exceeds string size limit");
return NULL;
}
return env->NewStringUTF(out.buf());
}
} else {
FileWriter out(args.file());
if (!out.is_open()) {
throwNew(env, "java/io/IOException", strerror(errno));
return NULL;
}
error = Profiler::instance()->runInternal(args, out);
if (!error) {
return env->NewStringUTF("OK");
}
}
throwNew(env, "java/lang/IllegalStateException", error.message());
return NULL;
}
extern "C" DLLEXPORT jlong JNICALL
Java_one_profiler_AsyncProfiler_getSamples(JNIEnv* env, jobject unused) {
return (jlong)Profiler::instance()->total_samples();
}
extern "C" DLLEXPORT void JNICALL
Java_one_profiler_AsyncProfiler_filterThread0(JNIEnv* env, jobject unused, jthread thread, jboolean enable) {
int thread_id;
if (thread == NULL) {
thread_id = OS::threadId();
} else if ((thread_id = VMThread::nativeThreadId(env, thread)) < 0) {
return;
}
ThreadFilter* thread_filter = Profiler::instance()->threadFilter();
if (enable) {
thread_filter->add(thread_id);
} else {
thread_filter->remove(thread_id);
}
}
#define F(name, sig) {(char*)#name, (char*)sig, (void*)Java_one_profiler_AsyncProfiler_##name}
static const JNINativeMethod profiler_natives[] = {
F(start0, "(Ljava/lang/String;JZ)V"),
F(stop0, "()V"),
F(execute0, "(Ljava/lang/String;)Ljava/lang/String;"),
F(getSamples, "()J"),
F(filterThread0, "(Ljava/lang/Thread;Z)V"),
};
static const JNINativeMethod* execute0 = &profiler_natives[2];
#undef F
// Since AsyncProfiler class can be renamed or moved to another package (shaded),
// we look for the actual class in the stack trace.
void JavaAPI::registerNatives(jvmtiEnv* jvmti, JNIEnv* jni) {
jvmtiFrameInfo frame[10];
jint frame_count;
if (jvmti->GetStackTrace(NULL, 0, sizeof(frame) / sizeof(frame[0]), frame, &frame_count) != 0) {
return;
}
jclass System = jni->FindClass("java/lang/System");
jmethodID load = jni->GetStaticMethodID(System, "load", "(Ljava/lang/String;)V");
jmethodID loadLibrary = jni->GetStaticMethodID(System, "loadLibrary", "(Ljava/lang/String;)V");
// Look for System.load() or System.loadLibrary() method in the stack trace.
// The next frame will belong to AsyncProfiler class.
for (int i = 0; i < frame_count - 1; i++) {
if (frame[i].method == load || frame[i].method == loadLibrary) {
jclass profiler_class;
if (jvmti->GetMethodDeclaringClass(frame[i + 1].method, &profiler_class) == 0) {
for (int j = 0; j < sizeof(profiler_natives) / sizeof(JNINativeMethod); j++) {
jni->RegisterNatives(profiler_class, &profiler_natives[j], 1);
}
}
break;
}
}
jni->ExceptionClear();
}
bool JavaAPI::startHttpServer(jvmtiEnv* jvmti, JNIEnv* jni, const char* address) {
jclass handler = jni->FindClass("com/sun/net/httpserver/HttpHandler");
jobject loader;
if (handler != NULL && jvmti->GetClassLoader(handler, &loader) == 0) {
jclass cls = jni->DefineClass(SERVER_NAME, loader, (const jbyte*)SERVER_CLASS, INCBIN_SIZEOF(SERVER_CLASS));
if (cls != NULL && jni->RegisterNatives(cls, execute0, 1) == 0) {
jmethodID method = jni->GetStaticMethodID(cls, "start", "(Ljava/lang/String;)V");
if (method != NULL) {
jni->CallStaticVoidMethod(cls, method, jni->NewStringUTF(address));
if (!jni->ExceptionCheck()) {
return true;
}
}
}
}
jni->ExceptionDescribe();
return false;
}