Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/linux/native/libsaproc/DwarfParser.cpp
41149 views
1
/*
2
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2020, 2021, NTT DATA.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*
24
*/
25
26
#include <jni.h>
27
28
#include "dwarf.hpp"
29
#include "libproc.h"
30
31
#define CHECK_EXCEPTION if (env->ExceptionOccurred()) { return; }
32
33
static jfieldID p_dwarf_context_ID = 0;
34
static jint sa_RAX = -1;
35
static jint sa_RDX = -1;
36
static jint sa_RCX = -1;
37
static jint sa_RBX = -1;
38
static jint sa_RSI = -1;
39
static jint sa_RDI = -1;
40
static jint sa_RBP = -1;
41
static jint sa_RSP = -1;
42
static jint sa_R8 = -1;
43
static jint sa_R9 = -1;
44
static jint sa_R10 = -1;
45
static jint sa_R11 = -1;
46
static jint sa_R12 = -1;
47
static jint sa_R13 = -1;
48
static jint sa_R14 = -1;
49
static jint sa_R15 = -1;
50
51
static jlong get_dwarf_context(JNIEnv *env, jobject obj) {
52
return env->GetLongField(obj, p_dwarf_context_ID);
53
}
54
55
#define SET_REG(env, reg, reg_cls) \
56
jfieldID reg##_ID = env->GetStaticFieldID(reg_cls, #reg, "I"); \
57
CHECK_EXCEPTION \
58
sa_##reg = env->GetStaticIntField(reg_cls, reg##_ID); \
59
CHECK_EXCEPTION
60
61
/*
62
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
63
* Method: init0
64
* Signature: ()V
65
*/
66
extern "C"
67
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_init0
68
(JNIEnv *env, jclass this_cls) {
69
jclass cls = env->FindClass("sun/jvm/hotspot/debugger/linux/amd64/DwarfParser");
70
CHECK_EXCEPTION
71
p_dwarf_context_ID = env->GetFieldID(cls, "p_dwarf_context", "J");
72
CHECK_EXCEPTION
73
74
jclass reg_cls = env->FindClass("sun/jvm/hotspot/debugger/amd64/AMD64ThreadContext");
75
CHECK_EXCEPTION
76
SET_REG(env, RAX, reg_cls);
77
SET_REG(env, RDX, reg_cls);
78
SET_REG(env, RCX, reg_cls);
79
SET_REG(env, RBX, reg_cls);
80
SET_REG(env, RSI, reg_cls);
81
SET_REG(env, RDI, reg_cls);
82
SET_REG(env, RBP, reg_cls);
83
SET_REG(env, RSP, reg_cls);
84
SET_REG(env, R8, reg_cls);
85
SET_REG(env, R9, reg_cls);
86
SET_REG(env, R10, reg_cls);
87
SET_REG(env, R11, reg_cls);
88
SET_REG(env, R12, reg_cls);
89
SET_REG(env, R13, reg_cls);
90
SET_REG(env, R14, reg_cls);
91
SET_REG(env, R15, reg_cls);
92
}
93
94
/*
95
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
96
* Method: createDwarfContext
97
* Signature: (J)J
98
*/
99
extern "C"
100
JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_createDwarfContext
101
(JNIEnv *env, jclass this_cls, jlong lib) {
102
DwarfParser *parser = new DwarfParser(reinterpret_cast<lib_info *>(lib));
103
if (!parser->is_parseable()) {
104
jclass ex_cls = env->FindClass("sun/jvm/hotspot/debugger/DebuggerException");
105
if (!env->ExceptionOccurred()) {
106
env->ThrowNew(ex_cls, "DWARF not found");
107
}
108
delete parser;
109
return 0L;
110
}
111
112
return reinterpret_cast<jlong>(parser);
113
}
114
115
/*
116
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
117
* Method: destroyDwarfContext
118
* Signature: (J)V
119
*/
120
extern "C"
121
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_destroyDwarfContext
122
(JNIEnv *env, jclass this_cls, jlong context) {
123
DwarfParser *parser = reinterpret_cast<DwarfParser *>(context);
124
delete parser;
125
}
126
127
/*
128
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
129
* Method: isIn0
130
* Signature: (J)Z
131
*/
132
extern "C"
133
JNIEXPORT jboolean JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_isIn0
134
(JNIEnv *env, jobject this_obj, jlong pc) {
135
DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));
136
return static_cast<jboolean>(parser->is_in(pc));
137
}
138
139
/*
140
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
141
* Method: processDwarf0
142
* Signature: (J)V
143
*/
144
extern "C"
145
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_processDwarf0
146
(JNIEnv *env, jobject this_obj, jlong pc) {
147
DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));
148
if (!parser->process_dwarf(pc)) {
149
jclass ex_cls = env->FindClass("sun/jvm/hotspot/debugger/DebuggerException");
150
if (!env->ExceptionOccurred()) {
151
env->ThrowNew(ex_cls, "Could not find PC in DWARF");
152
}
153
return;
154
}
155
}
156
157
/*
158
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
159
* Method: getCFARegister
160
* Signature: ()I
161
*/
162
extern "C"
163
JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_getCFARegister
164
(JNIEnv *env, jobject this_obj) {
165
DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));
166
switch (parser->get_cfa_register()) {
167
case RAX: return sa_RAX;
168
case RDX: return sa_RDX;
169
case RCX: return sa_RCX;
170
case RBX: return sa_RBX;
171
case RSI: return sa_RSI;
172
case RDI: return sa_RDI;
173
case RBP: return sa_RBP;
174
case RSP: return sa_RSP;
175
case R8: return sa_R8;
176
case R9: return sa_R9;
177
case R10: return sa_R10;
178
case R11: return sa_R11;
179
case R12: return sa_R12;
180
case R13: return sa_R13;
181
case R14: return sa_R14;
182
case R15: return sa_R15;
183
default: return -1;
184
}
185
}
186
187
/*
188
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
189
* Method: getCFAOffset
190
* Signature: ()I
191
*/
192
extern "C"
193
JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_getCFAOffset
194
(JNIEnv *env, jobject this_obj) {
195
DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));
196
return parser->get_cfa_offset();
197
}
198
199
/*
200
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
201
* Method: getReturnAddressOffsetFromCFA
202
* Signature: ()I
203
*/
204
extern "C"
205
JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_getReturnAddressOffsetFromCFA
206
(JNIEnv *env, jobject this_obj) {
207
DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));
208
return parser->get_ra_cfa_offset();
209
}
210
211
/*
212
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
213
* Method: getBasePointerOffsetFromCFA
214
* Signature: ()I
215
*/
216
extern "C"
217
JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_getBasePointerOffsetFromCFA
218
(JNIEnv *env, jobject this_obj) {
219
DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));
220
return parser->get_bp_cfa_offset();
221
}
222
223
/*
224
* Class: sun_jvm_hotspot_debugger_linux_amd64_DwarfParser
225
* Method: isBPOffsetAvailable
226
* Signature: ()Z
227
*/
228
extern "C"
229
JNIEXPORT jboolean JNICALL Java_sun_jvm_hotspot_debugger_linux_amd64_DwarfParser_isBPOffsetAvailable
230
(JNIEnv *env, jobject this_obj) {
231
DwarfParser *parser = reinterpret_cast<DwarfParser *>(get_dwarf_context(env, this_obj));
232
return parser->is_bp_offset_available();
233
}
234
235
236