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