forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.cpp
More file actions
270 lines (238 loc) · 10.1 KB
/
lib.cpp
File metadata and controls
270 lines (238 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <iostream>
#include "lib.h"
const std::string LEVEL_STRINGS[] = {
"TRACE",
"DEBUG",
"INFO",
"WARN",
"ERROR"
};
Level LOG_LEVEL = WARN;
void log(Level level, std::string message) {
if (level >= LOG_LEVEL) {
std::cerr << LEVEL_STRINGS[level] << " [Sentry Agent]: " << message << std::endl;
}
}
static jint throwException(JNIEnv *env, const char *name, const char *message) {
jclass clazz;
clazz = env->FindClass(name);
return env->ThrowNew(clazz, message);
}
static jobject getLocalValue(jvmtiEnv* jvmti, JNIEnv *env, jthread thread, jint depth,
jvmtiLocalVariableEntry *table, int index) {
jobject result;
jint i_val;
jfloat f_val;
jdouble d_val;
jlong j_val;
jvmtiError jvmti_error;
jclass reflect_class;
jmethodID value_of;
switch (table[index].signature[0]) {
case '[': // Array
case 'L': // Object
jvmti_error = jvmti->GetLocalObject(thread, depth, table[index].slot, &result);
if (jvmti_error != JVMTI_ERROR_NONE || result == nullptr) {
return nullptr;
}
break;
case 'J': // long
jvmti_error = jvmti->GetLocalLong(thread, depth, table[index].slot, &j_val);
break;
case 'F': // float
jvmti_error = jvmti->GetLocalFloat(thread, depth, table[index].slot, &f_val);
break;
case 'D': // double
jvmti_error = jvmti->GetLocalDouble(thread, depth, table[index].slot, &d_val);
break;
case 'I': // int
case 'S': // short
case 'C': // char
case 'B': // byte
case 'Z': // boolean
jvmti_error = jvmti->GetLocalInt(thread, depth, table[index].slot, &i_val);
break;
// error type
default:
return nullptr;
}
if (jvmti_error != JVMTI_ERROR_NONE) {
return nullptr;
}
switch (table[index].signature[0]) {
case 'J': // long
reflect_class = env->FindClass("java/lang/Long");
value_of = env->GetStaticMethodID(reflect_class, "valueOf", "(J)Ljava/lang/Long;");
result = env->CallStaticObjectMethod(reflect_class, value_of, j_val);
break;
case 'F': // float
reflect_class = env->FindClass("java/lang/Float");
value_of = env->GetStaticMethodID(reflect_class, "valueOf", "(F)Ljava/lang/Float;");
result = env->CallStaticObjectMethod(reflect_class, value_of, f_val);
break;
case 'D': // double
reflect_class = env->FindClass("java/lang/Double");
value_of = env->GetStaticMethodID(reflect_class, "valueOf", "(D)Ljava/lang/Double;");
result = env->CallStaticObjectMethod(reflect_class, value_of, d_val);
break;
// INTEGER TYPES
case 'I': // int
reflect_class = env->FindClass("java/lang/Integer");
value_of = env->GetStaticMethodID(reflect_class, "valueOf", "(I)Ljava/lang/Integer;");
result = env->CallStaticObjectMethod(reflect_class, value_of, i_val);
break;
case 'S': // short
reflect_class = env->FindClass("java/lang/Short");
value_of = env->GetStaticMethodID(reflect_class, "valueOf", "(S)Ljava/lang/Short;");
result = env->CallStaticObjectMethod(reflect_class, value_of, i_val);
break;
case 'C': // char
reflect_class = env->FindClass("java/lang/Character");
value_of = env->GetStaticMethodID(reflect_class, "valueOf", "(C)Ljava/lang/Character;");
result = env->CallStaticObjectMethod(reflect_class, value_of, i_val);
break;
case 'B': // byte
reflect_class = env->FindClass("java/lang/Byte");
value_of = env->GetStaticMethodID(reflect_class, "valueOf", "(B)Ljava/lang/Byte;");
result = env->CallStaticObjectMethod(reflect_class, value_of, i_val);
break;
case 'Z': // boolean
reflect_class = env->FindClass("java/lang/Boolean");
value_of = env->GetStaticMethodID(reflect_class, "valueOf", "(Z)Ljava/lang/Boolean;");
result = env->CallStaticObjectMethod(reflect_class, value_of, i_val);
break;
default: // jobject
break;
}
return result;
}
static void makeLocalVariable(jvmtiEnv* jvmti, JNIEnv *env, jthread thread,
jint depth, jclass local_class, jmethodID live_ctor,
jlocation location, jobjectArray locals,
jvmtiLocalVariableEntry *table, int index) {
jstring name;
jobject value;
jobject local;
name = env->NewStringUTF(table[index].name);
if (location >= table[index].start_location && location <= (table[index].start_location + table[index].length)) {
value = getLocalValue(jvmti, env, thread, depth, table, index);
local = env->NewObject(local_class, live_ctor, name, value);
} else {
// dead object, use null
local = nullptr;
}
env->SetObjectArrayElement(locals, index, local);
}
static jobject makeFrameObject(jvmtiEnv* jvmti, JNIEnv *env, jmethodID method, jobjectArray locals) {
jvmtiError jvmti_error;
jclass method_class;
jobject frame_method;
jclass frame_class;
jmethodID ctor;
jvmti_error = jvmti->GetMethodDeclaringClass(method, &method_class);
if (jvmti_error != JVMTI_ERROR_NONE) {
throwException(env, "java/lang/RuntimeException", "Could not get the declaring class of the method.");
return nullptr;
}
frame_method = env->ToReflectedMethod(method_class, method, (jboolean) true);
if (frame_method == nullptr) {
return nullptr; // ToReflectedMethod raised an exception
}
frame_class = env->FindClass("io/sentry/jvmti/Frame");
if (frame_class == nullptr) {
return nullptr;
}
ctor = env->GetMethodID(frame_class, "<init>",
"(Ljava/lang/reflect/Method;[Lio/sentry/jvmti/Frame$LocalVariable;)V");
if (ctor == nullptr) {
return nullptr;
}
return env->NewObject(frame_class, ctor, frame_method, locals);
}
static jobject buildFrame(jvmtiEnv* jvmti, JNIEnv *env, jthread thread, jint depth,
jmethodID method, jlocation location) {
jvmtiError jvmti_error;
jvmtiLocalVariableEntry *local_var_table;
jint num_entries;
jobject value_ptr;
jobjectArray locals;
jclass local_class;
jmethodID live_ctor;
int i;
value_ptr = nullptr;
jvmti_error = jvmti->GetLocalVariableTable(method, &num_entries, &local_var_table);
if (jvmti_error != JVMTI_ERROR_NONE) {
locals = nullptr;
switch(jvmti_error) {
// Pass cases
case JVMTI_ERROR_ABSENT_INFORMATION:
case JVMTI_ERROR_NATIVE_METHOD:
break;
// Error cases
case JVMTI_ERROR_MUST_POSSESS_CAPABILITY:
throwException(env, "java/lang/RuntimeException", "The access_local_variables capability is not enabled.");
return nullptr;
case JVMTI_ERROR_INVALID_METHODID:
throwException(env, "java/lang/IllegalArgumentException", "Illegal jmethodID.");
return nullptr;
case JVMTI_ERROR_NULL_POINTER:
throwException(env, "java/lang/NullPointerException", "Passed null to GetLocalVariableTable().");
return nullptr;
default:
throwException(env, "java/lang/RuntimeException", "Unknown JVMTI Error.");
return nullptr;
}
} else {
local_class = env->FindClass("io/sentry/jvmti/Frame$LocalVariable");
live_ctor = env->GetMethodID(local_class, "<init>", "(Ljava/lang/String;Ljava/lang/Object;)V");
locals = env->NewObjectArray(num_entries, local_class, nullptr);
for (i = 0; i < num_entries; i++) {
makeLocalVariable(jvmti, env, thread, depth, local_class, live_ctor, location, locals, local_var_table, i);
}
jvmti->Deallocate((unsigned char *) local_var_table);
}
jvmti_error = jvmti->GetLocalObject(thread, depth, 0, &value_ptr);
if (jvmti_error != JVMTI_ERROR_NONE) {
value_ptr = nullptr;
}
return makeFrameObject(jvmti, env, method, locals);
}
jobjectArray buildStackTraceFrames(jvmtiEnv* jvmti, JNIEnv *env, jthread thread,
jint start_depth, jint num_frames) {
log(TRACE, "buildStackTraceFrames called.");
jvmtiFrameInfo* frames;
jclass result_class;
jint num_frames_returned;
jvmtiError jvmti_error;
jobjectArray result;
jobject frame;
jvmti_error = jvmti->Allocate(num_frames * (int)sizeof(jvmtiFrameInfo), (unsigned char **) &frames);
if (jvmti_error != JVMTI_ERROR_NONE) {
throwException(env, "java/lang/RuntimeException", "Could not allocate frame buffer.");
return nullptr;
}
jvmti_error = jvmti->GetStackTrace(thread, start_depth, num_frames, frames, &num_frames_returned);
if (jvmti_error != JVMTI_ERROR_NONE) {
jvmti->Deallocate((unsigned char *)frames);
throwException(env, "java/lang/RuntimeException", "Could not get stack trace.");
return nullptr;
}
result_class = env->FindClass("io/sentry/jvmti/Frame");
result = env->NewObjectArray(num_frames_returned, result_class, nullptr);
if (result == nullptr) {
jvmti->Deallocate((unsigned char *) frames);
return nullptr; // OutOfMemory
}
for (int i = 0; i < num_frames_returned; i++) {
frame = buildFrame(jvmti, env, thread, start_depth + i, frames[i].method, frames[i].location);
if (frame == nullptr) {
jvmti->Deallocate((unsigned char *) frames);
throwException(env, "java/lang/RuntimeException", "Error accessing frame object.");
return nullptr;
}
env->SetObjectArrayElement(result, i, frame);
}
jvmti->Deallocate((unsigned char *) frames);
log(TRACE, "buildStackTraceFrames exit.");
return result;
}