Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/agent/src/os/bsd/BsdDebuggerLocal.c
38833 views
1
/*
2
* Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#include <stdlib.h>
26
#include <jni.h>
27
#include "libproc.h"
28
29
#if defined(x86_64) && !defined(amd64)
30
#define amd64 1
31
#endif
32
33
#ifdef i386
34
#include "sun_jvm_hotspot_debugger_x86_X86ThreadContext.h"
35
#endif
36
37
#ifdef amd64
38
#include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
39
#endif
40
41
#ifdef aarch64
42
#include "sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext.h"
43
#endif
44
45
#if defined(sparc) || defined(sparcv9)
46
#include "sun_jvm_hotspot_debugger_sparc_SPARCThreadContext.h"
47
#endif
48
49
static jfieldID p_ps_prochandle_ID = 0;
50
static jfieldID threadList_ID = 0;
51
static jfieldID loadObjectList_ID = 0;
52
53
static jmethodID createClosestSymbol_ID = 0;
54
static jmethodID createLoadObject_ID = 0;
55
static jmethodID getThreadForThreadId_ID = 0;
56
static jmethodID listAdd_ID = 0;
57
58
#define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
59
#define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
60
#define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; }
61
#define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;}
62
63
static void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
64
(*env)->ThrowNew(env, (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException"), errMsg);
65
}
66
67
static struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) {
68
jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID);
69
return (struct ps_prochandle*)(intptr_t)ptr;
70
}
71
72
/*
73
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
74
* Method: init0
75
* Signature: ()V
76
*/
77
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0
78
(JNIEnv *env, jclass cls) {
79
jclass listClass;
80
81
if (init_libproc(getenv("LIBSAPROC_DEBUG") != NULL) != true) {
82
THROW_NEW_DEBUGGER_EXCEPTION("can't initialize libproc");
83
}
84
85
// fields we use
86
p_ps_prochandle_ID = (*env)->GetFieldID(env, cls, "p_ps_prochandle", "J");
87
CHECK_EXCEPTION;
88
threadList_ID = (*env)->GetFieldID(env, cls, "threadList", "Ljava/util/List;");
89
CHECK_EXCEPTION;
90
loadObjectList_ID = (*env)->GetFieldID(env, cls, "loadObjectList", "Ljava/util/List;");
91
CHECK_EXCEPTION;
92
93
// methods we use
94
createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol",
95
"(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;");
96
CHECK_EXCEPTION;
97
createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject",
98
"(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;");
99
CHECK_EXCEPTION;
100
getThreadForThreadId_ID = (*env)->GetMethodID(env, cls, "getThreadForThreadId",
101
"(J)Lsun/jvm/hotspot/debugger/ThreadProxy;");
102
CHECK_EXCEPTION;
103
// java.util.List method we call
104
listClass = (*env)->FindClass(env, "java/util/List");
105
CHECK_EXCEPTION;
106
listAdd_ID = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z");
107
CHECK_EXCEPTION;
108
}
109
110
JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getAddressSize
111
(JNIEnv *env, jclass cls)
112
{
113
#ifdef _LP64
114
return 8;
115
#else
116
return 4;
117
#endif
118
119
}
120
121
122
static void fillThreadsAndLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
123
int n = 0, i = 0;
124
125
// add threads
126
n = get_num_threads(ph);
127
for (i = 0; i < n; i++) {
128
jobject thread;
129
jobject threadList;
130
lwpid_t lwpid;
131
132
lwpid = get_lwp_id(ph, i);
133
thread = (*env)->CallObjectMethod(env, this_obj, getThreadForThreadId_ID,
134
(jlong)lwpid);
135
CHECK_EXCEPTION;
136
threadList = (*env)->GetObjectField(env, this_obj, threadList_ID);
137
CHECK_EXCEPTION;
138
(*env)->CallBooleanMethod(env, threadList, listAdd_ID, thread);
139
CHECK_EXCEPTION;
140
}
141
142
// add load objects
143
n = get_num_libs(ph);
144
for (i = 0; i < n; i++) {
145
uintptr_t base;
146
const char* name;
147
jobject loadObject;
148
jobject loadObjectList;
149
150
base = get_lib_base(ph, i);
151
name = get_lib_name(ph, i);
152
loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID,
153
(*env)->NewStringUTF(env, name), (jlong)0, (jlong)base);
154
CHECK_EXCEPTION;
155
loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID);
156
CHECK_EXCEPTION;
157
(*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject);
158
CHECK_EXCEPTION;
159
}
160
}
161
162
/*
163
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
164
* Method: attach0
165
* Signature: (I)V
166
*/
167
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I
168
(JNIEnv *env, jobject this_obj, jint jpid) {
169
170
struct ps_prochandle* ph;
171
if ( (ph = Pgrab(jpid)) == NULL) {
172
THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the process");
173
}
174
(*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
175
fillThreadsAndLoadObjects(env, this_obj, ph);
176
}
177
178
/*
179
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
180
* Method: attach0
181
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
182
*/
183
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
184
(JNIEnv *env, jobject this_obj, jstring execName, jstring coreName) {
185
const char *execName_cstr;
186
const char *coreName_cstr;
187
jboolean isCopy;
188
struct ps_prochandle* ph;
189
190
execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
191
CHECK_EXCEPTION;
192
coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
193
CHECK_EXCEPTION;
194
195
if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
196
(*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
197
(*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
198
THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
199
}
200
(*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
201
(*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
202
(*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
203
fillThreadsAndLoadObjects(env, this_obj, ph);
204
}
205
206
/*
207
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
208
* Method: detach0
209
* Signature: ()V
210
*/
211
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0
212
(JNIEnv *env, jobject this_obj) {
213
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
214
if (ph != NULL) {
215
Prelease(ph);
216
}
217
}
218
219
/*
220
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
221
* Method: lookupByName0
222
* Signature: (Ljava/lang/String;Ljava/lang/String;)J
223
*/
224
JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0
225
(JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) {
226
const char *objectName_cstr, *symbolName_cstr;
227
jlong addr;
228
jboolean isCopy;
229
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
230
231
objectName_cstr = NULL;
232
if (objectName != NULL) {
233
objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy);
234
CHECK_EXCEPTION_(0);
235
}
236
symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy);
237
CHECK_EXCEPTION_(0);
238
239
addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
240
241
if (objectName_cstr != NULL) {
242
(*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
243
}
244
(*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr);
245
return addr;
246
}
247
248
/*
249
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
250
* Method: lookupByAddress0
251
* Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
252
*/
253
JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByAddress0
254
(JNIEnv *env, jobject this_obj, jlong addr) {
255
uintptr_t offset;
256
const char* sym = NULL;
257
258
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
259
sym = symbol_for_pc(ph, (uintptr_t) addr, &offset);
260
if (sym == NULL) return 0;
261
return (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID,
262
(*env)->NewStringUTF(env, sym), (jlong)offset);
263
}
264
265
/*
266
* Class: sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
267
* Method: readBytesFromProcess0
268
* Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult;
269
*/
270
JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0
271
(JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) {
272
273
jboolean isCopy;
274
jbyteArray array;
275
jbyte *bufPtr;
276
ps_err_e err;
277
278
array = (*env)->NewByteArray(env, numBytes);
279
CHECK_EXCEPTION_(0);
280
bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
281
CHECK_EXCEPTION_(0);
282
283
err = ps_pread(get_proc_handle(env, this_obj), (psaddr_t) (uintptr_t)addr, bufPtr, numBytes);
284
(*env)->ReleaseByteArrayElements(env, array, bufPtr, 0);
285
return (err == PS_OK)? array : 0;
286
}
287
288
JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0
289
(JNIEnv *env, jobject this_obj, jint lwp_id) {
290
291
struct reg gregs;
292
jboolean isCopy;
293
jlongArray array;
294
jlong *regs;
295
296
struct ps_prochandle* ph = get_proc_handle(env, this_obj);
297
if (get_lwp_regs(ph, lwp_id, &gregs) != true) {
298
THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0);
299
}
300
301
#undef NPRGREG
302
#ifdef i386
303
#define NPRGREG sun_jvm_hotspot_debugger_x86_X86ThreadContext_NPRGREG
304
#endif
305
#ifdef ia64
306
#define NPRGREG IA64_REG_COUNT
307
#endif
308
#ifdef amd64
309
#define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
310
#endif
311
#ifdef aarch64
312
#define NPRGREG sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext_NPRGREG
313
#endif
314
#if defined(sparc) || defined(sparcv9)
315
#define NPRGREG sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_NPRGREG
316
#endif
317
318
array = (*env)->NewLongArray(env, NPRGREG);
319
CHECK_EXCEPTION_(0);
320
regs = (*env)->GetLongArrayElements(env, array, &isCopy);
321
322
#undef REG_INDEX
323
324
#ifdef i386
325
#define REG_INDEX(reg) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##reg
326
327
regs[REG_INDEX(GS)] = (uintptr_t) gregs.r_gs;
328
regs[REG_INDEX(FS)] = (uintptr_t) gregs.r_fs;
329
regs[REG_INDEX(ES)] = (uintptr_t) gregs.r_es;
330
regs[REG_INDEX(DS)] = (uintptr_t) gregs.r_ds;
331
regs[REG_INDEX(EDI)] = (uintptr_t) gregs.r_edi;
332
regs[REG_INDEX(ESI)] = (uintptr_t) gregs.r_esi;
333
regs[REG_INDEX(FP)] = (uintptr_t) gregs.r_ebp;
334
regs[REG_INDEX(SP)] = (uintptr_t) gregs.r_isp;
335
regs[REG_INDEX(EBX)] = (uintptr_t) gregs.r_ebx;
336
regs[REG_INDEX(EDX)] = (uintptr_t) gregs.r_edx;
337
regs[REG_INDEX(ECX)] = (uintptr_t) gregs.r_ecx;
338
regs[REG_INDEX(EAX)] = (uintptr_t) gregs.r_eax;
339
regs[REG_INDEX(PC)] = (uintptr_t) gregs.r_eip;
340
regs[REG_INDEX(CS)] = (uintptr_t) gregs.r_cs;
341
regs[REG_INDEX(SS)] = (uintptr_t) gregs.r_ss;
342
343
#endif /* i386 */
344
345
#if ia64
346
regs = (*env)->GetLongArrayElements(env, array, &isCopy);
347
int i;
348
for (i = 0; i < NPRGREG; i++ ) {
349
regs[i] = 0xDEADDEAD;
350
}
351
#endif /* ia64 */
352
353
#ifdef amd64
354
#define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
355
356
regs[REG_INDEX(R15)] = gregs.r_r15;
357
regs[REG_INDEX(R14)] = gregs.r_r14;
358
regs[REG_INDEX(R13)] = gregs.r_r13;
359
regs[REG_INDEX(R12)] = gregs.r_r12;
360
regs[REG_INDEX(RBP)] = gregs.r_rbp;
361
regs[REG_INDEX(RBX)] = gregs.r_rbx;
362
regs[REG_INDEX(R11)] = gregs.r_r11;
363
regs[REG_INDEX(R10)] = gregs.r_r10;
364
regs[REG_INDEX(R9)] = gregs.r_r9;
365
regs[REG_INDEX(R8)] = gregs.r_r8;
366
regs[REG_INDEX(RAX)] = gregs.r_rax;
367
regs[REG_INDEX(RCX)] = gregs.r_rcx;
368
regs[REG_INDEX(RDX)] = gregs.r_rdx;
369
regs[REG_INDEX(RSI)] = gregs.r_rsi;
370
regs[REG_INDEX(RDI)] = gregs.r_rdi;
371
regs[REG_INDEX(RIP)] = gregs.r_rip;
372
regs[REG_INDEX(CS)] = gregs.r_cs;
373
regs[REG_INDEX(RSP)] = gregs.r_rsp;
374
regs[REG_INDEX(SS)] = gregs.r_ss;
375
// regs[REG_INDEX(FSBASE)] = gregs.fs_base;
376
// regs[REG_INDEX(GSBASE)] = gregs.gs_base;
377
// regs[REG_INDEX(DS)] = gregs.ds;
378
// regs[REG_INDEX(ES)] = gregs.es;
379
// regs[REG_INDEX(FS)] = gregs.fs;
380
// regs[REG_INDEX(GS)] = gregs.gs;
381
382
#endif /* amd64 */
383
384
#if defined(sparc) || defined(sparcv9)
385
386
#define REG_INDEX(reg) sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_##reg
387
388
#ifdef _LP64
389
regs[REG_INDEX(R_PSR)] = gregs.tstate;
390
regs[REG_INDEX(R_PC)] = gregs.tpc;
391
regs[REG_INDEX(R_nPC)] = gregs.tnpc;
392
regs[REG_INDEX(R_Y)] = gregs.y;
393
#else
394
regs[REG_INDEX(R_PSR)] = gregs.psr;
395
regs[REG_INDEX(R_PC)] = gregs.pc;
396
regs[REG_INDEX(R_nPC)] = gregs.npc;
397
regs[REG_INDEX(R_Y)] = gregs.y;
398
#endif
399
regs[REG_INDEX(R_G0)] = 0 ;
400
regs[REG_INDEX(R_G1)] = gregs.u_regs[0];
401
regs[REG_INDEX(R_G2)] = gregs.u_regs[1];
402
regs[REG_INDEX(R_G3)] = gregs.u_regs[2];
403
regs[REG_INDEX(R_G4)] = gregs.u_regs[3];
404
regs[REG_INDEX(R_G5)] = gregs.u_regs[4];
405
regs[REG_INDEX(R_G6)] = gregs.u_regs[5];
406
regs[REG_INDEX(R_G7)] = gregs.u_regs[6];
407
regs[REG_INDEX(R_O0)] = gregs.u_regs[7];
408
regs[REG_INDEX(R_O1)] = gregs.u_regs[8];
409
regs[REG_INDEX(R_O2)] = gregs.u_regs[ 9];
410
regs[REG_INDEX(R_O3)] = gregs.u_regs[10];
411
regs[REG_INDEX(R_O4)] = gregs.u_regs[11];
412
regs[REG_INDEX(R_O5)] = gregs.u_regs[12];
413
regs[REG_INDEX(R_O6)] = gregs.u_regs[13];
414
regs[REG_INDEX(R_O7)] = gregs.u_regs[14];
415
#endif /* sparc */
416
417
#if defined(aarch64)
418
#define REG_INDEX(reg) sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext_##reg
419
{
420
int i;
421
for (i = 0; i < 31; i++)
422
regs[i] = gregs.x[i];
423
regs[REG_INDEX(SP)] = gregs.sp;
424
regs[REG_INDEX(PC)] = gregs.elr;
425
}
426
#endif /* aarch64 */
427
428
(*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT);
429
return array;
430
}
431
432