1 /*
2  * Copyright (c) 2002, 2019, 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 #if defined(sparc) || defined(sparcv9)
42 #include "sun_jvm_hotspot_debugger_sparc_SPARCThreadContext.h"
43 #endif
44 
45 #if defined(ppc64) || defined(ppc64le)
46 #include "sun_jvm_hotspot_debugger_ppc64_PPC64ThreadContext.h"
47 #endif
48 
49 #ifdef aarch64
50 #include "sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext.h"
51 #endif
52 
53 static jfieldID p_ps_prochandle_ID = 0;
54 static jfieldID threadList_ID = 0;
55 static jfieldID loadObjectList_ID = 0;
56 
57 static jmethodID createClosestSymbol_ID = 0;
58 static jmethodID createLoadObject_ID = 0;
59 static jmethodID getThreadForThreadId_ID = 0;
60 static jmethodID listAdd_ID = 0;
61 
62 /*
63  * SA_ALTROOT environment variable.
64  * This memory holds env string for putenv(3).
65  */
66 static char *saaltroot = NULL;
67 
68 #define CHECK_EXCEPTION_(value) if ((*env)->ExceptionOccurred(env)) { return value; }
69 #define CHECK_EXCEPTION if ((*env)->ExceptionOccurred(env)) { return;}
70 #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throw_new_debugger_exception(env, str); return value; }
71 #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throw_new_debugger_exception(env, str); return;}
72 
throw_new_debugger_exception(JNIEnv * env,const char * errMsg)73 void throw_new_debugger_exception(JNIEnv* env, const char* errMsg) {
74   jclass clazz;
75   clazz = (*env)->FindClass(env, "sun/jvm/hotspot/debugger/DebuggerException");
76   CHECK_EXCEPTION;
77   (*env)->ThrowNew(env, clazz, errMsg);
78 }
79 
get_proc_handle(JNIEnv * env,jobject this_obj)80 struct ps_prochandle* get_proc_handle(JNIEnv* env, jobject this_obj) {
81   jlong ptr = (*env)->GetLongField(env, this_obj, p_ps_prochandle_ID);
82   return (struct ps_prochandle*)(intptr_t)ptr;
83 }
84 
85 /*
86  * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
87  * Method:    init0
88  * Signature: ()V
89  */
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0(JNIEnv * env,jclass cls)90 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_init0
91   (JNIEnv *env, jclass cls) {
92   jclass listClass;
93 
94   if (init_libproc(getenv("LIBSAPROC_DEBUG") != NULL) != true) {
95      THROW_NEW_DEBUGGER_EXCEPTION("can't initialize libproc");
96   }
97 
98   // fields we use
99   p_ps_prochandle_ID = (*env)->GetFieldID(env, cls, "p_ps_prochandle", "J");
100   CHECK_EXCEPTION;
101   threadList_ID = (*env)->GetFieldID(env, cls, "threadList", "Ljava/util/List;");
102   CHECK_EXCEPTION;
103   loadObjectList_ID = (*env)->GetFieldID(env, cls, "loadObjectList", "Ljava/util/List;");
104   CHECK_EXCEPTION;
105 
106   // methods we use
107   createClosestSymbol_ID = (*env)->GetMethodID(env, cls, "createClosestSymbol",
108                     "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;");
109   CHECK_EXCEPTION;
110   createLoadObject_ID = (*env)->GetMethodID(env, cls, "createLoadObject",
111                     "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;");
112   CHECK_EXCEPTION;
113   getThreadForThreadId_ID = (*env)->GetMethodID(env, cls, "getThreadForThreadId",
114                                                      "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;");
115   CHECK_EXCEPTION;
116   // java.util.List method we call
117   listClass = (*env)->FindClass(env, "java/util/List");
118   CHECK_EXCEPTION;
119   listAdd_ID = (*env)->GetMethodID(env, listClass, "add", "(Ljava/lang/Object;)Z");
120   CHECK_EXCEPTION;
121 }
122 
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getAddressSize(JNIEnv * env,jclass cls)123 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getAddressSize
124   (JNIEnv *env, jclass cls)
125 {
126 #ifdef _LP64
127  return 8;
128 #else
129  return 4;
130 #endif
131 
132 }
133 
134 
fillThreadsAndLoadObjects(JNIEnv * env,jobject this_obj,struct ps_prochandle * ph)135 static void fillThreadsAndLoadObjects(JNIEnv* env, jobject this_obj, struct ps_prochandle* ph) {
136   int n = 0, i = 0;
137 
138   // add threads
139   n = get_num_threads(ph);
140   for (i = 0; i < n; i++) {
141     jobject thread;
142     jobject threadList;
143     lwpid_t lwpid;
144 
145     lwpid = get_lwp_id(ph, i);
146     thread = (*env)->CallObjectMethod(env, this_obj, getThreadForThreadId_ID,
147                                       (jlong)lwpid);
148     CHECK_EXCEPTION;
149     threadList = (*env)->GetObjectField(env, this_obj, threadList_ID);
150     CHECK_EXCEPTION;
151     (*env)->CallBooleanMethod(env, threadList, listAdd_ID, thread);
152     CHECK_EXCEPTION;
153   }
154 
155   // add load objects
156   n = get_num_libs(ph);
157   for (i = 0; i < n; i++) {
158      uintptr_t base;
159      const char* name;
160      jobject loadObject;
161      jobject loadObjectList;
162      jstring str;
163 
164      base = get_lib_base(ph, i);
165      name = get_lib_name(ph, i);
166 
167      str = (*env)->NewStringUTF(env, name);
168      CHECK_EXCEPTION;
169      loadObject = (*env)->CallObjectMethod(env, this_obj, createLoadObject_ID, str, (jlong)0, (jlong)base);
170      CHECK_EXCEPTION;
171      loadObjectList = (*env)->GetObjectField(env, this_obj, loadObjectList_ID);
172      CHECK_EXCEPTION;
173      (*env)->CallBooleanMethod(env, loadObjectList, listAdd_ID, loadObject);
174      CHECK_EXCEPTION;
175   }
176 }
177 
178 
179 /*
180  * Verify that a named ELF binary file (core or executable) has the same
181  * bitness as ourselves.
182  * Throw an exception if there is a mismatch or other problem.
183  *
184  * If we proceed using a mismatched debugger/debuggee, the best to hope
185  * for is a missing symbol, the worst is a crash searching for debug symbols.
186  */
verifyBitness(JNIEnv * env,const char * binaryName)187 void verifyBitness(JNIEnv *env, const char *binaryName) {
188   int fd = open(binaryName, O_RDONLY);
189   if (fd < 0) {
190     THROW_NEW_DEBUGGER_EXCEPTION("cannot open binary file");
191   }
192   unsigned char elf_ident[EI_NIDENT];
193   int i = read(fd, &elf_ident, sizeof(elf_ident));
194   close(fd);
195 
196   if (i < 0) {
197     THROW_NEW_DEBUGGER_EXCEPTION("cannot read binary file");
198   }
199 #ifndef _LP64
200   if (elf_ident[EI_CLASS] == ELFCLASS64) {
201     THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use 64-bit java for debugger");
202   }
203 #else
204   if (elf_ident[EI_CLASS] != ELFCLASS64) {
205     THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger");
206   }
207 #endif
208 }
209 
210 
211 /*
212  * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
213  * Method:    setSAAltRoot0
214  * Signature: (Ljava/lang/String;)V
215  */
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_setSAAltRoot0(JNIEnv * env,jobject this_obj,jstring altroot)216 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_setSAAltRoot0
217   (JNIEnv *env, jobject this_obj, jstring altroot) {
218   if (saaltroot != NULL) {
219     free(saaltroot);
220   }
221   const char *path = (*env)->GetStringUTFChars(env, altroot, JNI_FALSE);
222   /*
223    * `saaltroot` is used for putenv().
224    * So we need to keep this memory.
225    */
226   static const char *PREFIX = "SA_ALTROOT=";
227   size_t len = strlen(PREFIX) + strlen(path) + 1;
228   saaltroot = (char *)malloc(len);
229   snprintf(saaltroot, len, "%s%s", PREFIX, path);
230   putenv(saaltroot);
231   (*env)->ReleaseStringUTFChars(env, altroot, path);
232 }
233 
234 /*
235  * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
236  * Method:    attach0
237  * Signature: (I)V
238  */
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I(JNIEnv * env,jobject this_obj,jint jpid)239 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__I
240   (JNIEnv *env, jobject this_obj, jint jpid) {
241 
242   char err_buf[200];
243   struct ps_prochandle* ph;
244   if ((ph = Pgrab(jpid, err_buf, sizeof(err_buf))) == NULL) {
245     char msg[230];
246     snprintf(msg, sizeof(msg), "Can't attach to the process: %s", err_buf);
247     THROW_NEW_DEBUGGER_EXCEPTION(msg);
248   }
249   (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
250   fillThreadsAndLoadObjects(env, this_obj, ph);
251 }
252 
253 /*
254  * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
255  * Method:    attach0
256  * Signature: (Ljava/lang/String;Ljava/lang/String;)V
257  */
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2(JNIEnv * env,jobject this_obj,jstring execName,jstring coreName)258 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
259   (JNIEnv *env, jobject this_obj, jstring execName, jstring coreName) {
260   const char *execName_cstr = NULL;
261   const char *coreName_cstr = NULL;
262   jboolean isCopy;
263   struct ps_prochandle* ph;
264 
265   execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
266   CHECK_EXCEPTION;
267   coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
268   if ((*env)->ExceptionOccurred(env)) {
269     goto cleanup;
270   }
271 
272   verifyBitness(env, execName_cstr);
273   if ((*env)->ExceptionOccurred(env)) {
274     goto cleanup;
275   }
276 
277   if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
278     (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
279     (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr);
280     THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
281   }
282   (*env)->SetLongField(env, this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
283   fillThreadsAndLoadObjects(env, this_obj, ph);
284 
285 cleanup:
286   if (execName_cstr != NULL) { (*env)->ReleaseStringUTFChars(env, execName, execName_cstr); }
287   if (coreName_cstr != NULL) { (*env)->ReleaseStringUTFChars(env, coreName, coreName_cstr); }
288 }
289 
290 /*
291  * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
292  * Method:    detach0
293  * Signature: ()V
294  */
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0(JNIEnv * env,jobject this_obj)295 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_detach0
296   (JNIEnv *env, jobject this_obj) {
297   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
298   if (ph != NULL) {
299      Prelease(ph);
300   }
301   if (saaltroot != NULL) {
302     free(saaltroot);
303     saaltroot = NULL;
304   }
305 }
306 
307 /*
308  * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
309  * Method:    lookupByName0
310  * Signature: (Ljava/lang/String;Ljava/lang/String;)J
311  */
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0(JNIEnv * env,jobject this_obj,jstring objectName,jstring symbolName)312 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByName0
313   (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) {
314   const char *objectName_cstr, *symbolName_cstr;
315   jlong addr;
316   jboolean isCopy;
317   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
318 
319   objectName_cstr = NULL;
320   if (objectName != NULL) {
321     objectName_cstr = (*env)->GetStringUTFChars(env, objectName, &isCopy);
322     CHECK_EXCEPTION_(0);
323   }
324   symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy);
325   if ((*env)->ExceptionOccurred(env)) {
326     if (objectName_cstr != NULL) {
327       (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
328     }
329     return 0;
330   }
331 
332   addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
333 
334   if (objectName_cstr != NULL) {
335     (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
336   }
337   (*env)->ReleaseStringUTFChars(env, symbolName, symbolName_cstr);
338   return addr;
339 }
340 
341 /*
342  * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
343  * Method:    lookupByAddress0
344  * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;
345  */
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByAddress0(JNIEnv * env,jobject this_obj,jlong addr)346 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_lookupByAddress0
347   (JNIEnv *env, jobject this_obj, jlong addr) {
348   uintptr_t offset;
349   jobject obj;
350   jstring str;
351   const char* sym = NULL;
352 
353   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
354   sym = symbol_for_pc(ph, (uintptr_t) addr, &offset);
355   if (sym == NULL) return 0;
356   str = (*env)->NewStringUTF(env, sym);
357   CHECK_EXCEPTION_(NULL);
358   obj = (*env)->CallObjectMethod(env, this_obj, createClosestSymbol_ID, str, (jlong)offset);
359   CHECK_EXCEPTION_(NULL);
360   return obj;
361 }
362 
363 /*
364  * Class:     sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal
365  * Method:    readBytesFromProcess0
366  * Signature: (JJ)Lsun/jvm/hotspot/debugger/ReadResult;
367  */
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0(JNIEnv * env,jobject this_obj,jlong addr,jlong numBytes)368 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_readBytesFromProcess0
369   (JNIEnv *env, jobject this_obj, jlong addr, jlong numBytes) {
370 
371   jboolean isCopy;
372   jbyteArray array;
373   jbyte *bufPtr;
374   ps_err_e err;
375 
376   array = (*env)->NewByteArray(env, numBytes);
377   CHECK_EXCEPTION_(0);
378   bufPtr = (*env)->GetByteArrayElements(env, array, &isCopy);
379   CHECK_EXCEPTION_(0);
380 
381 #ifdef __DragonFly__
382   err = PS_ERR; /* XXX unsupported */
383 #else
384   err = ps_pread(get_proc_handle(env, this_obj), (psaddr_t) (uintptr_t)addr, bufPtr, numBytes);
385 #endif
386   (*env)->ReleaseByteArrayElements(env, array, bufPtr, 0);
387   return (err == PS_OK)? array : 0;
388 }
389 
390 #if defined(i386) || defined(amd64) || defined(sparc) || defined(sparcv9) | defined(ppc64) || defined(ppc64le) || defined(aarch64)
Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0(JNIEnv * env,jobject this_obj,jint lwp_id)391 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_getThreadIntegerRegisterSet0
392   (JNIEnv *env, jobject this_obj, jint lwp_id) {
393 
394   struct reg gregs;
395   jboolean isCopy;
396   jlongArray array;
397   jlong *regs;
398 
399   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
400   if (get_lwp_regs(ph, lwp_id, &gregs) != true) {
401      THROW_NEW_DEBUGGER_EXCEPTION_("get_thread_regs failed for a lwp", 0);
402   }
403 
404 #undef NPRGREG
405 #ifdef i386
406 #define NPRGREG sun_jvm_hotspot_debugger_x86_X86ThreadContext_NPRGREG
407 #endif
408 #ifdef amd64
409 #define NPRGREG sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_NPRGREG
410 #endif
411 #if defined(sparc) || defined(sparcv9)
412 #define NPRGREG sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_NPRGREG
413 #endif
414 #if defined(ppc64) || defined(ppc64le)
415 #define NPRGREG sun_jvm_hotspot_debugger_ppc64_PPC64ThreadContext_NPRGREG
416 #endif
417 #ifdef aarch64
418 #define NPRGREG sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext_NPRGREG
419 #endif
420 
421   array = (*env)->NewLongArray(env, NPRGREG);
422   CHECK_EXCEPTION_(0);
423   regs = (*env)->GetLongArrayElements(env, array, &isCopy);
424 
425 #undef REG_INDEX
426 
427 #ifdef i386
428 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_x86_X86ThreadContext_##reg
429 
430   regs[REG_INDEX(GS)]  = (uintptr_t) gregs.r_gs;
431   regs[REG_INDEX(FS)]  = (uintptr_t) gregs.r_fs;
432   regs[REG_INDEX(ES)]  = (uintptr_t) gregs.r_es;
433   regs[REG_INDEX(DS)]  = (uintptr_t) gregs.r_ds;
434   regs[REG_INDEX(EDI)] = (uintptr_t) gregs.r_edi;
435   regs[REG_INDEX(ESI)] = (uintptr_t) gregs.r_esi;
436   regs[REG_INDEX(FP)] = (uintptr_t) gregs.r_ebp;
437   regs[REG_INDEX(SP)] = (uintptr_t) gregs.r_isp;
438   regs[REG_INDEX(EBX)] = (uintptr_t) gregs.r_ebx;
439   regs[REG_INDEX(EDX)] = (uintptr_t) gregs.r_edx;
440   regs[REG_INDEX(ECX)] = (uintptr_t) gregs.r_ecx;
441   regs[REG_INDEX(EAX)] = (uintptr_t) gregs.r_eax;
442   regs[REG_INDEX(PC)] = (uintptr_t) gregs.r_eip;
443   regs[REG_INDEX(CS)]  = (uintptr_t) gregs.r_cs;
444   regs[REG_INDEX(SS)]  = (uintptr_t) gregs.r_ss;
445 
446 #endif /* i386 */
447 
448 #ifdef amd64
449 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext_##reg
450 
451   regs[REG_INDEX(R15)] = gregs.r_r15;
452   regs[REG_INDEX(R14)] = gregs.r_r14;
453   regs[REG_INDEX(R13)] = gregs.r_r13;
454   regs[REG_INDEX(R12)] = gregs.r_r12;
455   regs[REG_INDEX(RBP)] = gregs.r_rbp;
456   regs[REG_INDEX(RBX)] = gregs.r_rbx;
457   regs[REG_INDEX(R11)] = gregs.r_r11;
458   regs[REG_INDEX(R10)] = gregs.r_r10;
459   regs[REG_INDEX(R9)] = gregs.r_r9;
460   regs[REG_INDEX(R8)] = gregs.r_r8;
461   regs[REG_INDEX(RAX)] = gregs.r_rax;
462   regs[REG_INDEX(RCX)] = gregs.r_rcx;
463   regs[REG_INDEX(RDX)] = gregs.r_rdx;
464   regs[REG_INDEX(RSI)] = gregs.r_rsi;
465   regs[REG_INDEX(RDI)] = gregs.r_rdi;
466   regs[REG_INDEX(RIP)] = gregs.r_rip;
467   regs[REG_INDEX(CS)] = gregs.r_cs;
468   regs[REG_INDEX(RSP)] = gregs.r_rsp;
469   regs[REG_INDEX(SS)] = gregs.r_ss;
470 #if defined(__FreeBSD__) || defined(__DragonFly__)
471 #include <machine/sysarch.h>
472   void **fs_base = NULL, **gs_base = NULL;
473   amd64_get_fsbase(fs_base);
474   amd64_get_gsbase(gs_base);
475 
476   regs[REG_INDEX(FSBASE)] = (long) *fs_base;
477   regs[REG_INDEX(GSBASE)] = (long) *gs_base;
478 #ifndef __DragonFly__
479   regs[REG_INDEX(DS)] = gregs.r_ds;
480   regs[REG_INDEX(ES)] = gregs.r_es;
481   regs[REG_INDEX(FS)] = gregs.r_fs;
482   regs[REG_INDEX(GS)] = gregs.r_gs;
483 #endif
484 #endif /* __FreeBSD__ */
485 
486 #endif /* amd64 */
487 
488 #if defined(sparc) || defined(sparcv9)
489 
490 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_sparc_SPARCThreadContext_##reg
491 
492 #ifdef _LP64
493   regs[REG_INDEX(R_PSR)] = gregs.tstate;
494   regs[REG_INDEX(R_PC)]  = gregs.tpc;
495   regs[REG_INDEX(R_nPC)] = gregs.tnpc;
496   regs[REG_INDEX(R_Y)]   = gregs.y;
497 #else
498   regs[REG_INDEX(R_PSR)] = gregs.psr;
499   regs[REG_INDEX(R_PC)]  = gregs.pc;
500   regs[REG_INDEX(R_nPC)] = gregs.npc;
501   regs[REG_INDEX(R_Y)]   = gregs.y;
502 #endif
503   regs[REG_INDEX(R_G0)]  =            0 ;
504   regs[REG_INDEX(R_G1)]  = gregs.u_regs[0];
505   regs[REG_INDEX(R_G2)]  = gregs.u_regs[1];
506   regs[REG_INDEX(R_G3)]  = gregs.u_regs[2];
507   regs[REG_INDEX(R_G4)]  = gregs.u_regs[3];
508   regs[REG_INDEX(R_G5)]  = gregs.u_regs[4];
509   regs[REG_INDEX(R_G6)]  = gregs.u_regs[5];
510   regs[REG_INDEX(R_G7)]  = gregs.u_regs[6];
511   regs[REG_INDEX(R_O0)]  = gregs.u_regs[7];
512   regs[REG_INDEX(R_O1)]  = gregs.u_regs[8];
513   regs[REG_INDEX(R_O2)]  = gregs.u_regs[ 9];
514   regs[REG_INDEX(R_O3)]  = gregs.u_regs[10];
515   regs[REG_INDEX(R_O4)]  = gregs.u_regs[11];
516   regs[REG_INDEX(R_O5)]  = gregs.u_regs[12];
517   regs[REG_INDEX(R_O6)]  = gregs.u_regs[13];
518   regs[REG_INDEX(R_O7)]  = gregs.u_regs[14];
519 #endif /* sparc */
520 #if defined(ppc64) || defined(ppc64le)
521 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_ppc64_PPC64ThreadContext_##reg
522 
523   regs[REG_INDEX(LR)] = gregs.lr;
524   regs[REG_INDEX(PC)] = gregs.pc;
525   regs[REG_INDEX(R0)]  = gregs.fixreg[0];
526   regs[REG_INDEX(R1)]  = gregs.fixreg[1];
527   regs[REG_INDEX(R2)]  = gregs.fixreg[2];
528   regs[REG_INDEX(R3)]  = gregs.fixreg[3];
529   regs[REG_INDEX(R4)]  = gregs.fixreg[4];
530   regs[REG_INDEX(R5)]  = gregs.fixreg[5];
531   regs[REG_INDEX(R6)]  = gregs.fixreg[6];
532   regs[REG_INDEX(R7)]  = gregs.fixreg[7];
533   regs[REG_INDEX(R8)]  = gregs.fixreg[8];
534   regs[REG_INDEX(R9)]  = gregs.fixreg[9];
535   regs[REG_INDEX(R10)] = gregs.fixreg[10];
536   regs[REG_INDEX(R11)] = gregs.fixreg[11];
537   regs[REG_INDEX(R12)] = gregs.fixreg[12];
538   regs[REG_INDEX(R13)] = gregs.fixreg[13];
539   regs[REG_INDEX(R14)] = gregs.fixreg[14];
540   regs[REG_INDEX(R15)] = gregs.fixreg[15];
541   regs[REG_INDEX(R16)] = gregs.fixreg[16];
542   regs[REG_INDEX(R17)] = gregs.fixreg[17];
543   regs[REG_INDEX(R18)] = gregs.fixreg[18];
544   regs[REG_INDEX(R19)] = gregs.fixreg[19];
545   regs[REG_INDEX(R20)] = gregs.fixreg[20];
546   regs[REG_INDEX(R21)] = gregs.fixreg[21];
547   regs[REG_INDEX(R22)] = gregs.fixreg[22];
548   regs[REG_INDEX(R23)] = gregs.fixreg[23];
549   regs[REG_INDEX(R24)] = gregs.fixreg[24];
550   regs[REG_INDEX(R25)] = gregs.fixreg[25];
551   regs[REG_INDEX(R26)] = gregs.fixreg[26];
552   regs[REG_INDEX(R27)] = gregs.fixreg[27];
553   regs[REG_INDEX(R28)] = gregs.fixreg[28];
554   regs[REG_INDEX(R29)] = gregs.fixreg[29];
555   regs[REG_INDEX(R30)] = gregs.fixreg[30];
556   regs[REG_INDEX(R31)] = gregs.fixreg[31];
557 
558 #endif /* ppc64 */
559 #if defined(aarch64)
560 
561 #define REG_INDEX(reg) sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext_##reg
562 
563   {
564     int i;
565     for (i = 0; i < 31; i++)
566       regs[i] = gregs.x[i];
567     regs[REG_INDEX(SP)] = gregs.sp;
568     regs[REG_INDEX(PC)] = gregs.elr;
569   }
570 #endif /* aarch64 */
571 
572 
573   (*env)->ReleaseLongArrayElements(env, array, regs, JNI_COMMIT);
574   return array;
575 }
576 #endif
577