-
Notifications
You must be signed in to change notification settings - Fork 134
/
coffeejni.c
185 lines (163 loc) · 5.73 KB
/
coffeejni.c
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
/* CoffeeCatch, a tiny native signal handler/catcher for JNI code.
* (especially for Android/Dalvik)
*
* Copyright (c) 2013, Xavier Roche (http://www.httrack.com/)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef COFFEECATCH_JNI_H
#define COFFEECATCH_JNI_H
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <jni.h>
#include <assert.h>
#include "coffeecatch.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct t_bt_fun {
JNIEnv* env;
jclass cls;
jclass cls_ste;
jmethodID cons_ste;
jobjectArray elements;
size_t size;
size_t index;
} t_bt_fun;
static char* bt_print(const char *function, uintptr_t offset) {
if (function != NULL) {
char buffer[256];
snprintf(buffer, sizeof(buffer), "%s:%p", function, (void*) offset);
return strdup(buffer);
} else {
return "<unknown>";
}
}
static char* bt_addr(uintptr_t addr) {
char buffer[32];
snprintf(buffer, sizeof(buffer), "%p", (void*) addr);
return strdup(buffer);
}
#define IS_VALID_CLASS_CHAR(C) ( \
((C) >= 'a' && (C) <= 'z') \
|| ((C) >= 'A' && (C) <= 'Z') \
|| ((C) >= '0' && (C) <= '9') \
|| (C) == '_' \
)
static char* bt_module(const char *module) {
if (module != NULL) {
size_t i;
char *copy;
if (*module == '/') {
module++;
}
copy = strdup(module);
/* Pseudo-java-class. */
for(i = 0; copy[i] != '\0'; i++) {
if (copy[i] == '/') {
copy[i] = '.';
} else if (!IS_VALID_CLASS_CHAR(copy[i])) {
copy[i] = '_';
}
}
return copy;
} else {
return "<unknown>";
}
}
static void bt_fun(void *arg, const char *module, uintptr_t addr,
const char *function, uintptr_t offset) {
t_bt_fun *const t = (t_bt_fun*) arg;
JNIEnv*const env = t->env;
jstring declaringClass = (*env)->NewStringUTF(env, bt_module(module));
jstring methodName = (*env)->NewStringUTF(env, bt_addr(addr));
jstring fileName = (*env)->NewStringUTF(env, bt_print(function, offset));
const int lineNumber = function != NULL ? 0 : -2; /* "-2" is "inside JNI code" */
jobject trace = (*env)->NewObject(env, t->cls_ste, t->cons_ste,
declaringClass, methodName, fileName,
lineNumber);
if (t->index < t->size) {
(*t->env)->SetObjectArrayElement(t->env, t->elements, t->index++, trace);
}
}
void coffeecatch_throw_exception(JNIEnv* env) {
jclass cls = (*env)->FindClass(env, "java/lang/Error");
jclass cls_ste = (*env)->FindClass(env, "java/lang/StackTraceElement");
jmethodID cons = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;)V");
jmethodID cons_cause = (*env)->GetMethodID(env, cls, "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V");
jmethodID cons_ste = (*env)->GetMethodID(env, cls_ste, "<init>",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
jmethodID meth_sste = (*env)->GetMethodID(env, cls, "setStackTrace",
"([Ljava/lang/StackTraceElement;)V");
/* Exception message. */
const char*const message = coffeecatch_get_message();
jstring str = (*env)->NewStringUTF(env, strdup(message));
/* Final exception. */
jthrowable exception;
/* Add pseudo-stack trace. */
const ssize_t bt_size = coffeecatch_get_backtrace_size();
assert(cls != NULL);
assert(cls_ste != NULL);
assert(cons != NULL);
assert(cons_cause != NULL);
assert(cons_ste != NULL);
assert(meth_sste != NULL);
assert(message != NULL);
assert(str != NULL);
/* Can we produce a stack trace ? */
if (bt_size > 0) {
/* Create secondary exception. */
jthrowable cause = (jthrowable) (*env)->NewObject(env, cls, cons, str);
/* Stack trace. */
jobjectArray elements =
(*env)->NewObjectArray(env, bt_size, cls_ste, NULL);
if (elements != NULL) {
t_bt_fun t;
t.env = env;
t.cls = cls;
t.cls_ste = cls_ste;
t.cons_ste = cons_ste;
t.elements = elements;
t.index = 0;
t.size = bt_size;
coffeecatch_get_backtrace_info(bt_fun, &t);
(*env)->CallVoidMethod(env, cause, meth_sste, elements);
}
/* Primary exception */
exception = (jthrowable) (*env)->NewObject(env, cls, cons_cause, str, cause);
} else {
/* Simple exception */
exception = (jthrowable) (*env)->NewObject(env, cls, cons, str);
}
/* Throw exception. */
if (exception != NULL) {
(*env)->Throw(env, exception);
} else {
(*env)->ThrowNew(env, cls, strdup(message));
}
}
#ifdef __cplusplus
}
#endif
#endif