|
1 /* |
|
2 * Copyright 2002-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 #include "salibproc.h" |
|
26 #include "sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal.h" |
|
27 #include <thread_db.h> |
|
28 #include <strings.h> |
|
29 #include <limits.h> |
|
30 #include <demangle.h> |
|
31 #include <stdarg.h> |
|
32 #include <stdlib.h> |
|
33 #include <errno.h> |
|
34 |
|
35 #define CHECK_EXCEPTION_(value) if(env->ExceptionOccurred()) { return value; } |
|
36 #define CHECK_EXCEPTION if(env->ExceptionOccurred()) { return;} |
|
37 #define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throwNewDebuggerException(env, str); return value; } |
|
38 #define THROW_NEW_DEBUGGER_EXCEPTION(str) { throwNewDebuggerException(env, str); return;} |
|
39 |
|
40 #define SYMBOL_BUF_SIZE 256 |
|
41 #define ERR_MSG_SIZE (PATH_MAX + 256) |
|
42 |
|
43 // debug mode |
|
44 static int _libsaproc_debug = 0; |
|
45 |
|
46 static void print_debug(const char* format,...) { |
|
47 if (_libsaproc_debug) { |
|
48 va_list alist; |
|
49 |
|
50 va_start(alist, format); |
|
51 fputs("libsaproc DEBUG: ", stderr); |
|
52 vfprintf(stderr, format, alist); |
|
53 va_end(alist); |
|
54 } |
|
55 } |
|
56 |
|
57 struct Debugger { |
|
58 JNIEnv* env; |
|
59 jobject this_obj; |
|
60 }; |
|
61 |
|
62 struct DebuggerWithObject : Debugger { |
|
63 jobject obj; |
|
64 }; |
|
65 |
|
66 struct DebuggerWith2Objects : DebuggerWithObject { |
|
67 jobject obj2; |
|
68 }; |
|
69 |
|
70 /* |
|
71 * Portions of user thread level detail gathering code is from pstack source |
|
72 * code. See pstack.c in Solaris 2.8 user commands source code. |
|
73 */ |
|
74 |
|
75 static void throwNewDebuggerException(JNIEnv* env, const char* errMsg) { |
|
76 env->ThrowNew(env->FindClass("sun/jvm/hotspot/debugger/DebuggerException"), errMsg); |
|
77 } |
|
78 |
|
79 // JNI ids for some fields, methods |
|
80 |
|
81 // libproc handler pointer |
|
82 static jfieldID p_ps_prochandle_ID = 0; |
|
83 |
|
84 // libthread.so dlopen handle, thread agent ptr and function pointers |
|
85 static jfieldID libthread_db_handle_ID = 0; |
|
86 static jfieldID p_td_thragent_t_ID = 0; |
|
87 static jfieldID p_td_init_ID = 0; |
|
88 static jfieldID p_td_ta_new_ID = 0; |
|
89 static jfieldID p_td_ta_delete_ID = 0; |
|
90 static jfieldID p_td_ta_thr_iter_ID = 0; |
|
91 static jfieldID p_td_thr_get_info_ID = 0; |
|
92 static jfieldID p_td_ta_map_id2thr_ID = 0; |
|
93 static jfieldID p_td_thr_getgregs_ID = 0; |
|
94 |
|
95 // reg index fields |
|
96 static jfieldID pcRegIndex_ID = 0; |
|
97 static jfieldID fpRegIndex_ID = 0; |
|
98 |
|
99 // part of the class sharing workaround |
|
100 static jfieldID classes_jsa_fd_ID = 0; |
|
101 static jfieldID p_file_map_header_ID = 0; |
|
102 |
|
103 // method ids |
|
104 |
|
105 static jmethodID getThreadForThreadId_ID = 0; |
|
106 static jmethodID createSenderFrame_ID = 0; |
|
107 static jmethodID createLoadObject_ID = 0; |
|
108 static jmethodID createClosestSymbol_ID = 0; |
|
109 static jmethodID listAdd_ID = 0; |
|
110 |
|
111 /* |
|
112 * Functions we need from libthread_db |
|
113 */ |
|
114 typedef td_err_e |
|
115 (*p_td_init_t)(void); |
|
116 typedef td_err_e |
|
117 (*p_td_ta_new_t)(void *, td_thragent_t **); |
|
118 typedef td_err_e |
|
119 (*p_td_ta_delete_t)(td_thragent_t *); |
|
120 typedef td_err_e |
|
121 (*p_td_ta_thr_iter_t)(const td_thragent_t *, td_thr_iter_f *, void *, |
|
122 td_thr_state_e, int, sigset_t *, unsigned); |
|
123 typedef td_err_e |
|
124 (*p_td_thr_get_info_t)(const td_thrhandle_t *, td_thrinfo_t *); |
|
125 typedef td_err_e |
|
126 (*p_td_ta_map_id2thr_t)(const td_thragent_t *, thread_t, td_thrhandle_t *); |
|
127 typedef td_err_e |
|
128 (*p_td_thr_getgregs_t)(const td_thrhandle_t *, prgregset_t); |
|
129 |
|
130 static void |
|
131 clear_libthread_db_ptrs(JNIEnv* env, jobject this_obj) { |
|
132 // release libthread_db agent, if we had created |
|
133 p_td_ta_delete_t p_td_ta_delete = 0; |
|
134 p_td_ta_delete = (p_td_ta_delete_t) env->GetLongField(this_obj, p_td_ta_delete_ID); |
|
135 |
|
136 td_thragent_t *p_td_thragent_t = 0; |
|
137 p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); |
|
138 if (p_td_thragent_t != 0 && p_td_ta_delete != 0) { |
|
139 p_td_ta_delete(p_td_thragent_t); |
|
140 } |
|
141 |
|
142 // dlclose libthread_db.so |
|
143 void* libthread_db_handle = (void*) env->GetLongField(this_obj, libthread_db_handle_ID); |
|
144 if (libthread_db_handle != 0) { |
|
145 dlclose(libthread_db_handle); |
|
146 } |
|
147 |
|
148 env->SetLongField(this_obj, libthread_db_handle_ID, (jlong)0); |
|
149 env->SetLongField(this_obj, p_td_init_ID, (jlong)0); |
|
150 env->SetLongField(this_obj, p_td_ta_new_ID, (jlong)0); |
|
151 env->SetLongField(this_obj, p_td_ta_delete_ID, (jlong)0); |
|
152 env->SetLongField(this_obj, p_td_ta_thr_iter_ID, (jlong)0); |
|
153 env->SetLongField(this_obj, p_td_thr_get_info_ID, (jlong)0); |
|
154 env->SetLongField(this_obj, p_td_ta_map_id2thr_ID, (jlong)0); |
|
155 env->SetLongField(this_obj, p_td_thr_getgregs_ID, (jlong)0); |
|
156 } |
|
157 |
|
158 |
|
159 static void detach_internal(JNIEnv* env, jobject this_obj) { |
|
160 // clear libthread_db stuff |
|
161 clear_libthread_db_ptrs(env, this_obj); |
|
162 |
|
163 // release ptr to ps_prochandle |
|
164 jlong p_ps_prochandle; |
|
165 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
166 if (p_ps_prochandle != 0L) { |
|
167 Prelease((struct ps_prochandle*) p_ps_prochandle, PRELEASE_CLEAR); |
|
168 } |
|
169 |
|
170 // part of the class sharing workaround |
|
171 int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID); |
|
172 if (classes_jsa_fd != -1) { |
|
173 close(classes_jsa_fd); |
|
174 struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID); |
|
175 if (pheader != NULL) { |
|
176 free(pheader); |
|
177 } |
|
178 } |
|
179 } |
|
180 |
|
181 // Is it okay to ignore libthread_db failure? Set env var to ignore |
|
182 // libthread_db failure. You can still debug, but will miss threads |
|
183 // related functionality. |
|
184 static bool sa_ignore_threaddb = (getenv("SA_IGNORE_THREADDB") != 0); |
|
185 |
|
186 #define HANDLE_THREADDB_FAILURE(msg) \ |
|
187 if (sa_ignore_threaddb) { \ |
|
188 printf("libsaproc WARNING: %s\n", msg); \ |
|
189 return; \ |
|
190 } else { \ |
|
191 THROW_NEW_DEBUGGER_EXCEPTION(msg); \ |
|
192 } |
|
193 |
|
194 #define HANDLE_THREADDB_FAILURE_(msg, ret) \ |
|
195 if (sa_ignore_threaddb) { \ |
|
196 printf("libsaproc WARNING: %s\n", msg); \ |
|
197 return ret; \ |
|
198 } else { \ |
|
199 THROW_NEW_DEBUGGER_EXCEPTION_(msg, ret); \ |
|
200 } |
|
201 |
|
202 static const char * alt_root = NULL; |
|
203 static int alt_root_len = -1; |
|
204 |
|
205 #define SA_ALTROOT "SA_ALTROOT" |
|
206 |
|
207 static void init_alt_root() { |
|
208 if (alt_root_len == -1) { |
|
209 alt_root = getenv(SA_ALTROOT); |
|
210 if (alt_root) |
|
211 alt_root_len = strlen(alt_root); |
|
212 else |
|
213 alt_root_len = 0; |
|
214 } |
|
215 } |
|
216 |
|
217 static int find_file_hook(const char * name, int elf_checksum) { |
|
218 init_alt_root(); |
|
219 |
|
220 if (_libsaproc_debug) { |
|
221 printf("libsaproc DEBUG: find_file_hook %s 0x%x\n", name, elf_checksum); |
|
222 } |
|
223 |
|
224 if (alt_root_len > 0) { |
|
225 int fd = -1; |
|
226 char alt_path[PATH_MAX+1]; |
|
227 |
|
228 strcpy(alt_path, alt_root); |
|
229 strcat(alt_path, name); |
|
230 fd = open(alt_path, O_RDONLY); |
|
231 if (fd >= 0) { |
|
232 if (_libsaproc_debug) { |
|
233 printf("libsaproc DEBUG: find_file_hook substituted %s\n", alt_path); |
|
234 } |
|
235 return fd; |
|
236 } |
|
237 |
|
238 if (strrchr(name, '/')) { |
|
239 strcpy(alt_path, alt_root); |
|
240 strcat(alt_path, strrchr(name, '/')); |
|
241 fd = open(alt_path, O_RDONLY); |
|
242 if (fd >= 0) { |
|
243 if (_libsaproc_debug) { |
|
244 printf("libsaproc DEBUG: find_file_hook substituted %s\n", alt_path); |
|
245 } |
|
246 return fd; |
|
247 } |
|
248 } |
|
249 } |
|
250 return -1; |
|
251 } |
|
252 |
|
253 static int pathmap_open(const char* name) { |
|
254 int fd = open(name, O_RDONLY); |
|
255 if (fd < 0) { |
|
256 fd = find_file_hook(name, 0); |
|
257 } |
|
258 return fd; |
|
259 } |
|
260 |
|
261 static void * pathmap_dlopen(const char * name, int mode) { |
|
262 init_alt_root(); |
|
263 |
|
264 if (_libsaproc_debug) { |
|
265 printf("libsaproc DEBUG: pathmap_dlopen %s\n", name); |
|
266 } |
|
267 |
|
268 void * handle = NULL; |
|
269 if (alt_root_len > 0) { |
|
270 char alt_path[PATH_MAX+1]; |
|
271 strcpy(alt_path, alt_root); |
|
272 strcat(alt_path, name); |
|
273 handle = dlopen(alt_path, mode); |
|
274 if (_libsaproc_debug && handle) { |
|
275 printf("libsaproc DEBUG: pathmap_dlopen substituted %s\n", alt_path); |
|
276 } |
|
277 |
|
278 if (handle == NULL && strrchr(name, '/')) { |
|
279 strcpy(alt_path, alt_root); |
|
280 strcat(alt_path, strrchr(name, '/')); |
|
281 handle = dlopen(alt_path, mode); |
|
282 if (_libsaproc_debug && handle) { |
|
283 printf("libsaproc DEBUG: pathmap_dlopen substituted %s\n", alt_path); |
|
284 } |
|
285 } |
|
286 } |
|
287 if (handle == NULL) { |
|
288 handle = dlopen(name, mode); |
|
289 } |
|
290 if (_libsaproc_debug) { |
|
291 printf("libsaproc DEBUG: pathmap_dlopen %s return 0x%x\n", name, handle); |
|
292 } |
|
293 return handle; |
|
294 } |
|
295 |
|
296 // libproc and libthread_db callback functions |
|
297 |
|
298 extern "C" { |
|
299 |
|
300 static int |
|
301 init_libthread_db_ptrs(void *cd, const prmap_t *pmp, const char *object_name) { |
|
302 Debugger* dbg = (Debugger*) cd; |
|
303 JNIEnv* env = dbg->env; |
|
304 jobject this_obj = dbg->this_obj; |
|
305 struct ps_prochandle* ph = (struct ps_prochandle*) env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
306 |
|
307 char *s1 = 0, *s2 = 0; |
|
308 char libthread_db[PATH_MAX]; |
|
309 |
|
310 if (strstr(object_name, "/libthread.so.") == NULL) |
|
311 return (0); |
|
312 |
|
313 /* |
|
314 * We found a libthread. |
|
315 * dlopen() the matching libthread_db and get the thread agent handle. |
|
316 */ |
|
317 if (Pstatus(ph)->pr_dmodel == PR_MODEL_NATIVE) { |
|
318 (void) strcpy(libthread_db, object_name); |
|
319 s1 = (char*) strstr(object_name, ".so."); |
|
320 s2 = (char*) strstr(libthread_db, ".so."); |
|
321 (void) strcpy(s2, "_db"); |
|
322 s2 += 3; |
|
323 (void) strcpy(s2, s1); |
|
324 } else { |
|
325 #ifdef _LP64 |
|
326 /* |
|
327 * The victim process is 32-bit, we are 64-bit. |
|
328 * We have to find the 64-bit version of libthread_db |
|
329 * that matches the victim's 32-bit version of libthread. |
|
330 */ |
|
331 (void) strcpy(libthread_db, object_name); |
|
332 s1 = (char*) strstr(object_name, "/libthread.so."); |
|
333 s2 = (char*) strstr(libthread_db, "/libthread.so."); |
|
334 (void) strcpy(s2, "/64"); |
|
335 s2 += 3; |
|
336 (void) strcpy(s2, s1); |
|
337 s1 = (char*) strstr(s1, ".so."); |
|
338 s2 = (char*) strstr(s2, ".so."); |
|
339 (void) strcpy(s2, "_db"); |
|
340 s2 += 3; |
|
341 (void) strcpy(s2, s1); |
|
342 #else |
|
343 return (0); |
|
344 #endif /* _LP64 */ |
|
345 } |
|
346 |
|
347 void* libthread_db_handle = 0; |
|
348 if ((libthread_db_handle = pathmap_dlopen(libthread_db, RTLD_LAZY|RTLD_LOCAL)) == NULL) { |
|
349 char errMsg[PATH_MAX + 256]; |
|
350 sprintf(errMsg, "Can't load %s!", libthread_db); |
|
351 HANDLE_THREADDB_FAILURE_(errMsg, 0); |
|
352 } |
|
353 env->SetLongField(this_obj, libthread_db_handle_ID, (jlong)(uintptr_t)libthread_db_handle); |
|
354 |
|
355 void* tmpPtr = 0; |
|
356 tmpPtr = dlsym(libthread_db_handle, "td_init"); |
|
357 if (tmpPtr == 0) { |
|
358 HANDLE_THREADDB_FAILURE_("dlsym failed on td_init!", 0); |
|
359 } |
|
360 env->SetLongField(this_obj, p_td_init_ID, (jlong)(uintptr_t) tmpPtr); |
|
361 |
|
362 tmpPtr =dlsym(libthread_db_handle, "td_ta_new"); |
|
363 if (tmpPtr == 0) { |
|
364 HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_new!", 0); |
|
365 } |
|
366 env->SetLongField(this_obj, p_td_ta_new_ID, (jlong)(uintptr_t) tmpPtr); |
|
367 |
|
368 tmpPtr = dlsym(libthread_db_handle, "td_ta_delete"); |
|
369 if (tmpPtr == 0) { |
|
370 HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_delete!", 0); |
|
371 } |
|
372 env->SetLongField(this_obj, p_td_ta_delete_ID, (jlong)(uintptr_t) tmpPtr); |
|
373 |
|
374 tmpPtr = dlsym(libthread_db_handle, "td_ta_thr_iter"); |
|
375 if (tmpPtr == 0) { |
|
376 HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_thr_iter!", 0); |
|
377 } |
|
378 env->SetLongField(this_obj, p_td_ta_thr_iter_ID, (jlong)(uintptr_t) tmpPtr); |
|
379 |
|
380 tmpPtr = dlsym(libthread_db_handle, "td_thr_get_info"); |
|
381 if (tmpPtr == 0) { |
|
382 HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_get_info!", 0); |
|
383 } |
|
384 env->SetLongField(this_obj, p_td_thr_get_info_ID, (jlong)(uintptr_t) tmpPtr); |
|
385 |
|
386 tmpPtr = dlsym(libthread_db_handle, "td_ta_map_id2thr"); |
|
387 if (tmpPtr == 0) { |
|
388 HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_map_id2thr!", 0); |
|
389 } |
|
390 env->SetLongField(this_obj, p_td_ta_map_id2thr_ID, (jlong)(uintptr_t) tmpPtr); |
|
391 |
|
392 tmpPtr = dlsym(libthread_db_handle, "td_thr_getgregs"); |
|
393 if (tmpPtr == 0) { |
|
394 HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_getgregs!", 0); |
|
395 } |
|
396 env->SetLongField(this_obj, p_td_thr_getgregs_ID, (jlong)(uintptr_t) tmpPtr); |
|
397 |
|
398 return 1; |
|
399 } |
|
400 |
|
401 static int |
|
402 fill_thread_list(const td_thrhandle_t *p_td_thragent_t, void* cd) { |
|
403 DebuggerWithObject* dbgo = (DebuggerWithObject*) cd; |
|
404 JNIEnv* env = dbgo->env; |
|
405 jobject this_obj = dbgo->this_obj; |
|
406 jobject list = dbgo->obj; |
|
407 |
|
408 td_thrinfo_t thrinfo; |
|
409 p_td_thr_get_info_t p_td_thr_get_info = (p_td_thr_get_info_t) env->GetLongField(this_obj, p_td_thr_get_info_ID); |
|
410 |
|
411 if (p_td_thr_get_info(p_td_thragent_t, &thrinfo) != TD_OK) |
|
412 return (0); |
|
413 |
|
414 jobject threadProxy = env->CallObjectMethod(this_obj, getThreadForThreadId_ID, (jlong)(uintptr_t) thrinfo.ti_tid); |
|
415 CHECK_EXCEPTION_(1); |
|
416 env->CallBooleanMethod(list, listAdd_ID, threadProxy); |
|
417 CHECK_EXCEPTION_(1); |
|
418 return 0; |
|
419 } |
|
420 |
|
421 static int |
|
422 fill_load_object_list(void *cd, const prmap_t* pmp, const char* obj_name) { |
|
423 |
|
424 if (obj_name) { |
|
425 DebuggerWithObject* dbgo = (DebuggerWithObject*) cd; |
|
426 JNIEnv* env = dbgo->env; |
|
427 jobject this_obj = dbgo->this_obj; |
|
428 jobject list = dbgo->obj; |
|
429 |
|
430 jstring objectName = env->NewStringUTF(obj_name); |
|
431 CHECK_EXCEPTION_(1); |
|
432 |
|
433 jlong mapSize = (jlong) pmp->pr_size; |
|
434 jobject sharedObject = env->CallObjectMethod(this_obj, createLoadObject_ID, |
|
435 objectName, mapSize, (jlong)(uintptr_t)pmp->pr_vaddr); |
|
436 CHECK_EXCEPTION_(1); |
|
437 env->CallBooleanMethod(list, listAdd_ID, sharedObject); |
|
438 CHECK_EXCEPTION_(1); |
|
439 } |
|
440 |
|
441 return 0; |
|
442 } |
|
443 |
|
444 static int |
|
445 fill_cframe_list(void *cd, const prgregset_t regs, uint_t argc, const long *argv) { |
|
446 DebuggerWith2Objects* dbgo2 = (DebuggerWith2Objects*) cd; |
|
447 JNIEnv* env = dbgo2->env; |
|
448 jobject this_obj = dbgo2->this_obj; |
|
449 jobject curFrame = dbgo2->obj2; |
|
450 |
|
451 jint pcRegIndex = env->GetIntField(this_obj, pcRegIndex_ID); |
|
452 jint fpRegIndex = env->GetIntField(this_obj, fpRegIndex_ID); |
|
453 |
|
454 jlong pc = (jlong) (uintptr_t) regs[pcRegIndex]; |
|
455 jlong fp = (jlong) (uintptr_t) regs[fpRegIndex]; |
|
456 |
|
457 dbgo2->obj2 = env->CallObjectMethod(this_obj, createSenderFrame_ID, |
|
458 curFrame, pc, fp); |
|
459 CHECK_EXCEPTION_(1); |
|
460 if (dbgo2->obj == 0) { |
|
461 dbgo2->obj = dbgo2->obj2; |
|
462 } |
|
463 return 0; |
|
464 } |
|
465 |
|
466 // part of the class sharing workaround |
|
467 |
|
468 // FIXME: !!HACK ALERT!! |
|
469 |
|
470 // The format of sharing achive file header is needed to read shared heap |
|
471 // file mappings. For now, I am hard coding portion of FileMapHeader here. |
|
472 // Refer to filemap.hpp. |
|
473 |
|
474 // FileMapHeader describes the shared space data in the file to be |
|
475 // mapped. This structure gets written to a file. It is not a class, so |
|
476 // that the compilers don't add any compiler-private data to it. |
|
477 |
|
478 // Refer to CompactingPermGenGen::n_regions in compactingPermGenGen.hpp |
|
479 const int NUM_SHARED_MAPS = 4; |
|
480 |
|
481 // Refer to FileMapInfo::_current_version in filemap.hpp |
|
482 const int CURRENT_ARCHIVE_VERSION = 1; |
|
483 |
|
484 struct FileMapHeader { |
|
485 int _magic; // identify file type. |
|
486 int _version; // (from enum, above.) |
|
487 size_t _alignment; // how shared archive should be aligned |
|
488 |
|
489 |
|
490 struct space_info { |
|
491 int _file_offset; // sizeof(this) rounded to vm page size |
|
492 char* _base; // copy-on-write base address |
|
493 size_t _capacity; // for validity checking |
|
494 size_t _used; // for setting space top on read |
|
495 |
|
496 bool _read_only; // read only space? |
|
497 bool _allow_exec; // executable code in space? |
|
498 |
|
499 } _space[NUM_SHARED_MAPS]; // was _space[CompactingPermGenGen::n_regions]; |
|
500 |
|
501 // Ignore the rest of the FileMapHeader. We don't need those fields here. |
|
502 }; |
|
503 |
|
504 static bool |
|
505 read_int(struct ps_prochandle* ph, psaddr_t addr, int* pvalue) { |
|
506 int i; |
|
507 if (ps_pread(ph, addr, &i, sizeof(i)) == PS_OK) { |
|
508 *pvalue = i; |
|
509 return true; |
|
510 } else { |
|
511 return false; |
|
512 } |
|
513 } |
|
514 |
|
515 static bool |
|
516 read_pointer(struct ps_prochandle* ph, psaddr_t addr, uintptr_t* pvalue) { |
|
517 uintptr_t uip; |
|
518 if (ps_pread(ph, addr, &uip, sizeof(uip)) == PS_OK) { |
|
519 *pvalue = uip; |
|
520 return true; |
|
521 } else { |
|
522 return false; |
|
523 } |
|
524 } |
|
525 |
|
526 static bool |
|
527 read_string(struct ps_prochandle* ph, psaddr_t addr, char* buf, size_t size) { |
|
528 char ch = ' '; |
|
529 size_t i = 0; |
|
530 |
|
531 while (ch != '\0') { |
|
532 if (ps_pread(ph, addr, &ch, sizeof(ch)) != PS_OK) |
|
533 return false; |
|
534 |
|
535 if (i < size - 1) { |
|
536 buf[i] = ch; |
|
537 } else { // smaller buffer |
|
538 return false; |
|
539 } |
|
540 |
|
541 i++; addr++; |
|
542 } |
|
543 |
|
544 buf[i] = '\0'; |
|
545 return true; |
|
546 } |
|
547 |
|
548 #define USE_SHARED_SPACES_SYM "UseSharedSpaces" |
|
549 // mangled symbol name for Arguments::SharedArchivePath |
|
550 #define SHARED_ARCHIVE_PATH_SYM "__1cJArgumentsRSharedArchivePath_" |
|
551 |
|
552 static int |
|
553 init_classsharing_workaround(void *cd, const prmap_t* pmap, const char* obj_name) { |
|
554 Debugger* dbg = (Debugger*) cd; |
|
555 JNIEnv* env = dbg->env; |
|
556 jobject this_obj = dbg->this_obj; |
|
557 const char* jvm_name = 0; |
|
558 if ((jvm_name = strstr(obj_name, "libjvm.so")) != NULL || |
|
559 (jvm_name = strstr(obj_name, "libjvm_g.so")) != NULL) { |
|
560 jvm_name = obj_name; |
|
561 } else { |
|
562 return 0; |
|
563 } |
|
564 |
|
565 struct ps_prochandle* ph = (struct ps_prochandle*) env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
566 |
|
567 // initialize classes[_g].jsa file descriptor field. |
|
568 dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, -1); |
|
569 |
|
570 // check whether class sharing is on by reading variable "UseSharedSpaces" |
|
571 psaddr_t useSharedSpacesAddr = 0; |
|
572 ps_pglobal_lookup(ph, jvm_name, USE_SHARED_SPACES_SYM, &useSharedSpacesAddr); |
|
573 if (useSharedSpacesAddr == 0) { |
|
574 THROW_NEW_DEBUGGER_EXCEPTION_("can't find 'UseSharedSpaces' flag\n", 1); |
|
575 } |
|
576 |
|
577 // read the value of the flag "UseSharedSpaces" |
|
578 int value = 0; |
|
579 if (read_int(ph, useSharedSpacesAddr, &value) != true) { |
|
580 THROW_NEW_DEBUGGER_EXCEPTION_("can't read 'UseSharedSpaces' flag", 1); |
|
581 } else if (value == 0) { |
|
582 print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); |
|
583 return 1; |
|
584 } |
|
585 |
|
586 char classes_jsa[PATH_MAX]; |
|
587 psaddr_t sharedArchivePathAddrAddr = 0; |
|
588 ps_pglobal_lookup(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM, &sharedArchivePathAddrAddr); |
|
589 if (sharedArchivePathAddrAddr == 0) { |
|
590 print_debug("can't find symbol 'Arguments::SharedArchivePath'\n"); |
|
591 THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1); |
|
592 } |
|
593 |
|
594 uintptr_t sharedArchivePathAddr = 0; |
|
595 if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { |
|
596 print_debug("can't find read pointer 'Arguments::SharedArchivePath'\n"); |
|
597 THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1); |
|
598 } |
|
599 |
|
600 if (read_string(ph, (psaddr_t)sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { |
|
601 print_debug("can't find read 'Arguments::SharedArchivePath' value\n"); |
|
602 THROW_NEW_DEBUGGER_EXCEPTION_("can't get shared archive path from debuggee", 1); |
|
603 } |
|
604 |
|
605 print_debug("looking for %s\n", classes_jsa); |
|
606 |
|
607 // open the classes[_g].jsa |
|
608 int fd = pathmap_open(classes_jsa); |
|
609 if (fd < 0) { |
|
610 char errMsg[ERR_MSG_SIZE]; |
|
611 sprintf(errMsg, "can't open shared archive file %s", classes_jsa); |
|
612 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); |
|
613 } else { |
|
614 print_debug("opened shared archive file %s\n", classes_jsa); |
|
615 } |
|
616 |
|
617 // parse classes[_g].jsa |
|
618 struct FileMapHeader* pheader = (struct FileMapHeader*) malloc(sizeof(struct FileMapHeader)); |
|
619 if (pheader == NULL) { |
|
620 close(fd); |
|
621 THROW_NEW_DEBUGGER_EXCEPTION_("can't allocate memory for shared file map header", 1); |
|
622 } |
|
623 |
|
624 memset(pheader, 0, sizeof(struct FileMapHeader)); |
|
625 // read FileMapHeader |
|
626 size_t n = read(fd, pheader, sizeof(struct FileMapHeader)); |
|
627 if (n != sizeof(struct FileMapHeader)) { |
|
628 free(pheader); |
|
629 close(fd); |
|
630 char errMsg[ERR_MSG_SIZE]; |
|
631 sprintf(errMsg, "unable to read shared archive file map header from %s", classes_jsa); |
|
632 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); |
|
633 } |
|
634 |
|
635 // check file magic |
|
636 if (pheader->_magic != 0xf00baba2) { |
|
637 free(pheader); |
|
638 close(fd); |
|
639 char errMsg[ERR_MSG_SIZE]; |
|
640 sprintf(errMsg, "%s has bad shared archive magic 0x%x, expecting 0xf00baba2", |
|
641 classes_jsa, pheader->_magic); |
|
642 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); |
|
643 } |
|
644 |
|
645 // check version |
|
646 if (pheader->_version != CURRENT_ARCHIVE_VERSION) { |
|
647 free(pheader); |
|
648 close(fd); |
|
649 char errMsg[ERR_MSG_SIZE]; |
|
650 sprintf(errMsg, "%s has wrong shared archive version %d, expecting %d", |
|
651 classes_jsa, pheader->_version, CURRENT_ARCHIVE_VERSION); |
|
652 THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1); |
|
653 } |
|
654 |
|
655 if (_libsaproc_debug) { |
|
656 for (int m = 0; m < NUM_SHARED_MAPS; m++) { |
|
657 print_debug("shared file offset %d mapped at 0x%lx, size = %ld, read only? = %d\n", |
|
658 pheader->_space[m]._file_offset, pheader->_space[m]._base, |
|
659 pheader->_space[m]._used, pheader->_space[m]._read_only); |
|
660 } |
|
661 } |
|
662 |
|
663 // FIXME: For now, omitting other checks such as VM version etc. |
|
664 |
|
665 // store class archive file fd and map header in debugger object fields |
|
666 dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, fd); |
|
667 dbg->env->SetLongField(this_obj, p_file_map_header_ID, (jlong)(uintptr_t) pheader); |
|
668 return 1; |
|
669 } |
|
670 |
|
671 } // extern "C" |
|
672 |
|
673 // error messages for proc_arg_grab failure codes. The messages are |
|
674 // modified versions of comments against corresponding #defines in |
|
675 // libproc.h. |
|
676 static const char* proc_arg_grab_errmsgs[] = { |
|
677 "", |
|
678 /* G_NOPROC */ "No such process", |
|
679 /* G_NOCORE */ "No such core file", |
|
680 /* G_NOPROCORCORE */ "No such process or core", |
|
681 /* G_NOEXEC */ "Cannot locate executable file", |
|
682 /* G_ZOMB */ "Zombie processs", |
|
683 /* G_PERM */ "No permission to attach", |
|
684 /* G_BUSY */ "Another process has already attached", |
|
685 /* G_SYS */ "System process - can not attach", |
|
686 /* G_SELF */ "Process is self - can't debug myself!", |
|
687 /* G_INTR */ "Interrupt received while grabbing", |
|
688 /* G_LP64 */ "debuggee is 64 bit, use java -d64 for debugger", |
|
689 /* G_FORMAT */ "File is not an ELF format core file - corrupted core?", |
|
690 /* G_ELF */ "Libelf error while parsing an ELF file", |
|
691 /* G_NOTE */ "Required PT_NOTE Phdr not present - corrupted core?", |
|
692 }; |
|
693 |
|
694 static void attach_internal(JNIEnv* env, jobject this_obj, jstring cmdLine, jboolean isProcess) { |
|
695 jboolean isCopy; |
|
696 int gcode; |
|
697 const char* cmdLine_cstr = env->GetStringUTFChars(cmdLine, &isCopy); |
|
698 CHECK_EXCEPTION; |
|
699 |
|
700 // some older versions of libproc.so crash when trying to attach 32 bit |
|
701 // debugger to 64 bit core file. check and throw error. |
|
702 #ifndef _LP64 |
|
703 atoi(cmdLine_cstr); |
|
704 if (errno) { |
|
705 // core file |
|
706 int core_fd; |
|
707 if ((core_fd = open64(cmdLine_cstr, O_RDONLY)) >= 0) { |
|
708 Elf32_Ehdr e32; |
|
709 if (pread64(core_fd, &e32, sizeof (e32), 0) == sizeof (e32) && |
|
710 memcmp(&e32.e_ident[EI_MAG0], ELFMAG, SELFMAG) == 0 && |
|
711 e32.e_type == ET_CORE && e32.e_ident[EI_CLASS] == ELFCLASS64) { |
|
712 close(core_fd); |
|
713 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use java -d64 for debugger"); |
|
714 } |
|
715 close(core_fd); |
|
716 } |
|
717 // all other conditions are handled by libproc.so. |
|
718 } |
|
719 #endif |
|
720 |
|
721 // connect to process/core |
|
722 struct ps_prochandle* ph = proc_arg_grab(cmdLine_cstr, (isProcess? PR_ARG_PIDS : PR_ARG_CORES), PGRAB_FORCE, &gcode); |
|
723 env->ReleaseStringUTFChars(cmdLine, cmdLine_cstr); |
|
724 if (! ph) { |
|
725 if (gcode > 0 && gcode < sizeof(proc_arg_grab_errmsgs)/sizeof(const char*)) { |
|
726 char errMsg[ERR_MSG_SIZE]; |
|
727 sprintf(errMsg, "Attach failed : %s", proc_arg_grab_errmsgs[gcode]); |
|
728 THROW_NEW_DEBUGGER_EXCEPTION(errMsg); |
|
729 } else { |
|
730 if (_libsaproc_debug && gcode == G_STRANGE) { |
|
731 perror("libsaproc DEBUG: "); |
|
732 } |
|
733 if (isProcess) { |
|
734 THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to process!"); |
|
735 } else { |
|
736 THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to core file!"); |
|
737 } |
|
738 } |
|
739 } |
|
740 |
|
741 // even though libproc.so supports 64 bit debugger and 32 bit debuggee, we don't |
|
742 // support such cross-bit-debugging. check for that combination and throw error. |
|
743 #ifdef _LP64 |
|
744 int data_model; |
|
745 if (ps_pdmodel(ph, &data_model) != PS_OK) { |
|
746 Prelease(ph, PRELEASE_CLEAR); |
|
747 THROW_NEW_DEBUGGER_EXCEPTION("can't determine debuggee data model (ILP32? or LP64?)"); |
|
748 } |
|
749 if (data_model == PR_MODEL_ILP32) { |
|
750 Prelease(ph, PRELEASE_CLEAR); |
|
751 THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger"); |
|
752 } |
|
753 #endif |
|
754 |
|
755 env->SetLongField(this_obj, p_ps_prochandle_ID, (jlong)(uintptr_t)ph); |
|
756 |
|
757 Debugger dbg; |
|
758 dbg.env = env; |
|
759 dbg.this_obj = this_obj; |
|
760 jthrowable exception = 0; |
|
761 if (! isProcess) { |
|
762 /* |
|
763 * With class sharing, shared perm. gen heap is allocated in with MAP_SHARED|PROT_READ. |
|
764 * These pages are mapped from the file "classes[_g].jsa". MAP_SHARED pages are not dumped |
|
765 * in Solaris core.To read shared heap pages, we have to read classes[_g].jsa file. |
|
766 */ |
|
767 Pobject_iter(ph, init_classsharing_workaround, &dbg); |
|
768 exception = env->ExceptionOccurred(); |
|
769 if (exception) { |
|
770 env->ExceptionClear(); |
|
771 detach_internal(env, this_obj); |
|
772 env->Throw(exception); |
|
773 return; |
|
774 } |
|
775 } |
|
776 |
|
777 /* |
|
778 * Iterate over the process mappings looking |
|
779 * for libthread and then dlopen the appropriate |
|
780 * libthread_db and get function pointers. |
|
781 */ |
|
782 Pobject_iter(ph, init_libthread_db_ptrs, &dbg); |
|
783 exception = env->ExceptionOccurred(); |
|
784 if (exception) { |
|
785 env->ExceptionClear(); |
|
786 if (!sa_ignore_threaddb) { |
|
787 detach_internal(env, this_obj); |
|
788 env->Throw(exception); |
|
789 } |
|
790 return; |
|
791 } |
|
792 |
|
793 // init libthread_db and create thread_db agent |
|
794 p_td_init_t p_td_init = (p_td_init_t) env->GetLongField(this_obj, p_td_init_ID); |
|
795 if (p_td_init == 0) { |
|
796 if (!sa_ignore_threaddb) { |
|
797 detach_internal(env, this_obj); |
|
798 } |
|
799 HANDLE_THREADDB_FAILURE("Did not find libthread in target process/core!"); |
|
800 } |
|
801 |
|
802 if (p_td_init() != TD_OK) { |
|
803 if (!sa_ignore_threaddb) { |
|
804 detach_internal(env, this_obj); |
|
805 } |
|
806 HANDLE_THREADDB_FAILURE("Can't initialize thread_db!"); |
|
807 } |
|
808 |
|
809 p_td_ta_new_t p_td_ta_new = (p_td_ta_new_t) env->GetLongField(this_obj, p_td_ta_new_ID); |
|
810 |
|
811 td_thragent_t *p_td_thragent_t = 0; |
|
812 if (p_td_ta_new(ph, &p_td_thragent_t) != TD_OK) { |
|
813 if (!sa_ignore_threaddb) { |
|
814 detach_internal(env, this_obj); |
|
815 } |
|
816 HANDLE_THREADDB_FAILURE("Can't create thread_db agent!"); |
|
817 } |
|
818 env->SetLongField(this_obj, p_td_thragent_t_ID, (jlong)(uintptr_t) p_td_thragent_t); |
|
819 |
|
820 } |
|
821 |
|
822 /* |
|
823 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
824 * Method: attach0 |
|
825 * Signature: (Ljava/lang/String;)V |
|
826 * Description: process detach |
|
827 */ |
|
828 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2 |
|
829 (JNIEnv *env, jobject this_obj, jstring pid) { |
|
830 attach_internal(env, this_obj, pid, JNI_TRUE); |
|
831 } |
|
832 |
|
833 /* |
|
834 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
835 * Method: attach0 |
|
836 * Signature: (Ljava/lang/String;Ljava/lang/String;)V |
|
837 * Description: core file detach |
|
838 */ |
|
839 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2 |
|
840 (JNIEnv *env, jobject this_obj, jstring executable, jstring corefile) { |
|
841 // ignore executable file name, libproc.so can detect a.out name anyway. |
|
842 attach_internal(env, this_obj, corefile, JNI_FALSE); |
|
843 } |
|
844 |
|
845 |
|
846 /* |
|
847 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
848 * Method: detach0 |
|
849 * Signature: ()V |
|
850 * Description: process/core file detach |
|
851 */ |
|
852 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_detach0 |
|
853 (JNIEnv *env, jobject this_obj) { |
|
854 detach_internal(env, this_obj); |
|
855 } |
|
856 |
|
857 /* |
|
858 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
859 * Method: getRemoteProcessAddressSize0 |
|
860 * Signature: ()I |
|
861 * Description: get process/core address size |
|
862 */ |
|
863 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getRemoteProcessAddressSize0 |
|
864 (JNIEnv *env, jobject this_obj) { |
|
865 jlong p_ps_prochandle; |
|
866 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
867 int data_model = PR_MODEL_ILP32; |
|
868 ps_pdmodel((struct ps_prochandle*) p_ps_prochandle, &data_model); |
|
869 print_debug("debuggee is %d bit\n", data_model == PR_MODEL_ILP32? 32 : 64); |
|
870 return (jint) data_model == PR_MODEL_ILP32? 32 : 64; |
|
871 } |
|
872 |
|
873 /* |
|
874 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
875 * Method: getPageSize0 |
|
876 * Signature: ()I |
|
877 * Description: get process/core page size |
|
878 */ |
|
879 JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getPageSize0 |
|
880 (JNIEnv *env, jobject this_obj) { |
|
881 |
|
882 /* |
|
883 We are not yet attached to a java process or core file. getPageSize is called from |
|
884 the constructor of ProcDebuggerLocal. The following won't work! |
|
885 |
|
886 jlong p_ps_prochandle; |
|
887 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
888 CHECK_EXCEPTION_(-1); |
|
889 struct ps_prochandle* prochandle = (struct ps_prochandle*) p_ps_prochandle; |
|
890 return (Pstate(prochandle) == PS_DEAD) ? Pgetauxval(prochandle, AT_PAGESZ) |
|
891 : getpagesize(); |
|
892 |
|
893 So even though core may have been generated with a different page size settings, for now |
|
894 call getpagesize. |
|
895 */ |
|
896 |
|
897 return getpagesize(); |
|
898 } |
|
899 |
|
900 /* |
|
901 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
902 * Method: getThreadIntegerRegisterSet0 |
|
903 * Signature: (J)[J |
|
904 * Description: get gregset for a given thread specified by thread id |
|
905 */ |
|
906 JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getThreadIntegerRegisterSet0 |
|
907 (JNIEnv *env, jobject this_obj, jlong tid) { |
|
908 // map the thread id to thread handle |
|
909 p_td_ta_map_id2thr_t p_td_ta_map_id2thr = (p_td_ta_map_id2thr_t) env->GetLongField(this_obj, p_td_ta_map_id2thr_ID); |
|
910 |
|
911 td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); |
|
912 if (p_td_thragent_t == 0) { |
|
913 return 0; |
|
914 } |
|
915 |
|
916 td_thrhandle_t thr_handle; |
|
917 if (p_td_ta_map_id2thr(p_td_thragent_t, (thread_t) tid, &thr_handle) != TD_OK) { |
|
918 THROW_NEW_DEBUGGER_EXCEPTION_("can't map thread id to thread handle!", 0); |
|
919 } |
|
920 |
|
921 p_td_thr_getgregs_t p_td_thr_getgregs = (p_td_thr_getgregs_t) env->GetLongField(this_obj, p_td_thr_getgregs_ID); |
|
922 prgregset_t gregs; |
|
923 p_td_thr_getgregs(&thr_handle, gregs); |
|
924 |
|
925 jlongArray res = env->NewLongArray(NPRGREG); |
|
926 CHECK_EXCEPTION_(0); |
|
927 jboolean isCopy; |
|
928 jlong* ptr = env->GetLongArrayElements(res, &isCopy); |
|
929 for (int i = 0; i < NPRGREG; i++) { |
|
930 ptr[i] = (jlong) (uintptr_t) gregs[i]; |
|
931 } |
|
932 env->ReleaseLongArrayElements(res, ptr, JNI_COMMIT); |
|
933 return res; |
|
934 } |
|
935 |
|
936 /* |
|
937 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
938 * Method: fillThreadList0 |
|
939 * Signature: (Ljava/util/List;)V |
|
940 * Description: fills thread list of the debuggee process/core |
|
941 */ |
|
942 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillThreadList0 |
|
943 (JNIEnv *env, jobject this_obj, jobject list) { |
|
944 |
|
945 td_thragent_t* p_td_thragent_t = (td_thragent_t*) env->GetLongField(this_obj, p_td_thragent_t_ID); |
|
946 if (p_td_thragent_t == 0) { |
|
947 return; |
|
948 } |
|
949 |
|
950 p_td_ta_thr_iter_t p_td_ta_thr_iter = (p_td_ta_thr_iter_t) env->GetLongField(this_obj, p_td_ta_thr_iter_ID); |
|
951 |
|
952 DebuggerWithObject dbgo; |
|
953 dbgo.env = env; |
|
954 dbgo.this_obj = this_obj; |
|
955 dbgo.obj = list; |
|
956 |
|
957 p_td_ta_thr_iter(p_td_thragent_t, fill_thread_list, &dbgo, |
|
958 TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS); |
|
959 } |
|
960 |
|
961 /* |
|
962 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
963 * Method: fillCFrameList0 |
|
964 * Signature: ([J)Lsun/jvm/hotspot/debugger/proc/ProcCFrame; |
|
965 * Description: fills CFrame list for a given thread |
|
966 */ |
|
967 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillCFrameList0 |
|
968 (JNIEnv *env, jobject this_obj, jlongArray regsArray) { |
|
969 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
970 |
|
971 DebuggerWith2Objects dbgo2; |
|
972 dbgo2.env = env; |
|
973 dbgo2.this_obj = this_obj; |
|
974 dbgo2.obj = NULL; |
|
975 dbgo2.obj2 = NULL; |
|
976 |
|
977 jboolean isCopy; |
|
978 jlong* ptr = env->GetLongArrayElements(regsArray, &isCopy); |
|
979 CHECK_EXCEPTION_(0); |
|
980 |
|
981 prgregset_t gregs; |
|
982 for (int i = 0; i < NPRGREG; i++) { |
|
983 gregs[i] = (uintptr_t) ptr[i]; |
|
984 } |
|
985 |
|
986 env->ReleaseLongArrayElements(regsArray, ptr, JNI_ABORT); |
|
987 CHECK_EXCEPTION_(0); |
|
988 Pstack_iter((struct ps_prochandle*) p_ps_prochandle, gregs, fill_cframe_list, &dbgo2); |
|
989 return dbgo2.obj; |
|
990 } |
|
991 |
|
992 /* |
|
993 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
994 * Method: fillLoadObjectList0 |
|
995 * Signature: (Ljava/util/List;)V |
|
996 * Description: fills shared objects of the debuggee process/core |
|
997 */ |
|
998 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_fillLoadObjectList0 |
|
999 (JNIEnv *env, jobject this_obj, jobject list) { |
|
1000 DebuggerWithObject dbgo; |
|
1001 dbgo.env = env; |
|
1002 dbgo.this_obj = this_obj; |
|
1003 dbgo.obj = list; |
|
1004 |
|
1005 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
1006 Pobject_iter((struct ps_prochandle*) p_ps_prochandle, fill_load_object_list, &dbgo); |
|
1007 } |
|
1008 |
|
1009 /* |
|
1010 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
1011 * Method: readBytesFromProcess0 |
|
1012 * Signature: (JJ)[B |
|
1013 * Description: read bytes from debuggee process/core |
|
1014 */ |
|
1015 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0 |
|
1016 (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes) { |
|
1017 |
|
1018 jbyteArray array = env->NewByteArray(numBytes); |
|
1019 CHECK_EXCEPTION_(0); |
|
1020 jboolean isCopy; |
|
1021 jbyte* bufPtr = env->GetByteArrayElements(array, &isCopy); |
|
1022 CHECK_EXCEPTION_(0); |
|
1023 |
|
1024 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
1025 ps_err_e ret = ps_pread((struct ps_prochandle*) p_ps_prochandle, |
|
1026 (psaddr_t)address, bufPtr, (size_t)numBytes); |
|
1027 |
|
1028 if (ret != PS_OK) { |
|
1029 // part of the class sharing workaround. try shared heap area |
|
1030 int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID); |
|
1031 if (classes_jsa_fd != -1 && address != (jlong)0) { |
|
1032 print_debug("read failed at 0x%lx, attempting shared heap area\n", (long) address); |
|
1033 |
|
1034 struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID); |
|
1035 // walk through the shared mappings -- we just have 4 of them. |
|
1036 // so, linear walking is okay. |
|
1037 for (int m = 0; m < NUM_SHARED_MAPS; m++) { |
|
1038 |
|
1039 // We can skip the non-read-only maps. These are mapped as MAP_PRIVATE |
|
1040 // and hence will be read by libproc. Besides, the file copy may be |
|
1041 // stale because the process might have modified those pages. |
|
1042 if (pheader->_space[m]._read_only) { |
|
1043 jlong baseAddress = (jlong) (uintptr_t) pheader->_space[m]._base; |
|
1044 size_t usedSize = pheader->_space[m]._used; |
|
1045 if (address >= baseAddress && address < (baseAddress + usedSize)) { |
|
1046 // the given address falls in this shared heap area |
|
1047 print_debug("found shared map at 0x%lx\n", (long) baseAddress); |
|
1048 |
|
1049 |
|
1050 // If more data is asked than actually mapped from file, we need to zero fill |
|
1051 // till the end-of-page boundary. But, java array new does that for us. we just |
|
1052 // need to read as much as data available. |
|
1053 |
|
1054 #define MIN2(x, y) (((x) < (y))? (x) : (y)) |
|
1055 |
|
1056 jlong diff = address - baseAddress; |
|
1057 jlong bytesToRead = MIN2(numBytes, usedSize - diff); |
|
1058 off_t offset = pheader->_space[m]._file_offset + off_t(diff); |
|
1059 ssize_t bytesRead = pread(classes_jsa_fd, bufPtr, bytesToRead, offset); |
|
1060 if (bytesRead != bytesToRead) { |
|
1061 env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT); |
|
1062 print_debug("shared map read failed\n"); |
|
1063 return jbyteArray(0); |
|
1064 } else { |
|
1065 print_debug("shared map read succeeded\n"); |
|
1066 env->ReleaseByteArrayElements(array, bufPtr, 0); |
|
1067 return array; |
|
1068 } |
|
1069 } // is in current map |
|
1070 } // is read only map |
|
1071 } // for shared maps |
|
1072 } // classes_jsa_fd != -1 |
|
1073 env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT); |
|
1074 return jbyteArray(0); |
|
1075 } else { |
|
1076 env->ReleaseByteArrayElements(array, bufPtr, 0); |
|
1077 return array; |
|
1078 } |
|
1079 } |
|
1080 |
|
1081 /* |
|
1082 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
1083 * Method: writeBytesToProcess0 |
|
1084 * Signature: (JJ[B)V |
|
1085 * Description: write bytes into debugger process |
|
1086 */ |
|
1087 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_writeBytesToProcess0 |
|
1088 (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes, jbyteArray data) { |
|
1089 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
1090 jboolean isCopy; |
|
1091 jbyte* ptr = env->GetByteArrayElements(data, &isCopy); |
|
1092 CHECK_EXCEPTION; |
|
1093 |
|
1094 if (ps_pwrite((struct ps_prochandle*) p_ps_prochandle, address, ptr, numBytes) != PS_OK) { |
|
1095 env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); |
|
1096 THROW_NEW_DEBUGGER_EXCEPTION("Process write failed!"); |
|
1097 } |
|
1098 |
|
1099 env->ReleaseByteArrayElements(data, ptr, JNI_ABORT); |
|
1100 } |
|
1101 |
|
1102 /* |
|
1103 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
1104 * Method: suspend0 |
|
1105 * Signature: ()V |
|
1106 */ |
|
1107 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_suspend0 |
|
1108 (JNIEnv *env, jobject this_obj) { |
|
1109 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
1110 // for now don't check return value. revisit this again. |
|
1111 Pstop((struct ps_prochandle*) p_ps_prochandle, 1000); |
|
1112 } |
|
1113 |
|
1114 /* |
|
1115 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
1116 * Method: resume0 |
|
1117 * Signature: ()V |
|
1118 */ |
|
1119 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_resume0 |
|
1120 (JNIEnv *env, jobject this_obj) { |
|
1121 jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
1122 // for now don't check return value. revisit this again. |
|
1123 Psetrun((struct ps_prochandle*) p_ps_prochandle, 0, PRCFAULT|PRSTOP); |
|
1124 } |
|
1125 |
|
1126 /* |
|
1127 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
1128 * Method: lookupByName0 |
|
1129 * Signature: (Ljava/lang/String;Ljava/lang/String;)J |
|
1130 * Description: symbol lookup by name |
|
1131 */ |
|
1132 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByName0 |
|
1133 (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) { |
|
1134 jlong p_ps_prochandle; |
|
1135 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
1136 |
|
1137 jboolean isCopy; |
|
1138 const char* objectName_cstr = NULL; |
|
1139 if (objectName != NULL) { |
|
1140 objectName_cstr = env->GetStringUTFChars(objectName, &isCopy); |
|
1141 CHECK_EXCEPTION_(0); |
|
1142 } else { |
|
1143 objectName_cstr = PR_OBJ_EVERY; |
|
1144 } |
|
1145 |
|
1146 const char* symbolName_cstr = env->GetStringUTFChars(symbolName, &isCopy); |
|
1147 CHECK_EXCEPTION_(0); |
|
1148 |
|
1149 psaddr_t symbol_addr = (psaddr_t) 0; |
|
1150 ps_pglobal_lookup((struct ps_prochandle*) p_ps_prochandle, objectName_cstr, |
|
1151 symbolName_cstr, &symbol_addr); |
|
1152 |
|
1153 if (symbol_addr == 0) { |
|
1154 print_debug("lookup for %s in %s failed\n", symbolName_cstr, objectName_cstr); |
|
1155 } |
|
1156 |
|
1157 if (objectName_cstr != PR_OBJ_EVERY) { |
|
1158 env->ReleaseStringUTFChars(objectName, objectName_cstr); |
|
1159 } |
|
1160 env->ReleaseStringUTFChars(symbolName, symbolName_cstr); |
|
1161 return (jlong) (uintptr_t) symbol_addr; |
|
1162 } |
|
1163 |
|
1164 /* |
|
1165 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
1166 * Method: lookupByAddress0 |
|
1167 * Signature: (J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol; |
|
1168 * Description: lookup symbol name for a given address |
|
1169 */ |
|
1170 JNIEXPORT jobject JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_lookupByAddress0 |
|
1171 (JNIEnv *env, jobject this_obj, jlong address) { |
|
1172 jlong p_ps_prochandle; |
|
1173 p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID); |
|
1174 |
|
1175 char nameBuf[SYMBOL_BUF_SIZE + 1]; |
|
1176 GElf_Sym sym; |
|
1177 int res = Plookup_by_addr((struct ps_prochandle*) p_ps_prochandle, (uintptr_t) address, |
|
1178 nameBuf, sizeof(nameBuf), &sym); |
|
1179 if (res != 0) { // failed |
|
1180 return 0; |
|
1181 } |
|
1182 |
|
1183 jstring resSym = env->NewStringUTF(nameBuf); |
|
1184 CHECK_EXCEPTION_(0); |
|
1185 |
|
1186 return env->CallObjectMethod(this_obj, createClosestSymbol_ID, resSym, (address - sym.st_value)); |
|
1187 } |
|
1188 |
|
1189 /* |
|
1190 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
1191 * Method: demangle0 |
|
1192 * Signature: (Ljava/lang/String;)Ljava/lang/String; |
|
1193 */ |
|
1194 JNIEXPORT jstring JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_demangle0 |
|
1195 (JNIEnv *env, jobject this_object, jstring name) { |
|
1196 jboolean isCopy; |
|
1197 const char* ptr = env->GetStringUTFChars(name, &isCopy); |
|
1198 char buf[2*SYMBOL_BUF_SIZE + 1]; |
|
1199 jstring res = 0; |
|
1200 if (cplus_demangle((char*) ptr, buf, sizeof(buf)) != DEMANGLE_ESPACE) { |
|
1201 res = env->NewStringUTF(buf); |
|
1202 } else { |
|
1203 res = name; |
|
1204 } |
|
1205 env->ReleaseStringUTFChars(name, ptr); |
|
1206 return res; |
|
1207 } |
|
1208 |
|
1209 typedef int (*find_file_hook_t)(const char *, int elf_checksum); |
|
1210 |
|
1211 /* |
|
1212 * Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal |
|
1213 * Method: initIDs |
|
1214 * Signature: ()V |
|
1215 * Description: get JNI ids for fields and methods of ProcDebuggerLocal class |
|
1216 */ |
|
1217 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_initIDs |
|
1218 (JNIEnv *env, jclass clazz) { |
|
1219 _libsaproc_debug = getenv("LIBSAPROC_DEBUG") != NULL; |
|
1220 if (_libsaproc_debug) { |
|
1221 // propagate debug mode to libproc.so |
|
1222 static const char* var = "LIBPROC_DEBUG=1"; |
|
1223 putenv((char*)var); |
|
1224 } |
|
1225 |
|
1226 void* libproc_handle = dlopen("libproc.so", RTLD_LAZY | RTLD_GLOBAL); |
|
1227 if (libproc_handle == 0) |
|
1228 THROW_NEW_DEBUGGER_EXCEPTION("can't load libproc.so, if you are using Solaris 5.7 or below, copy libproc.so from 5.8!"); |
|
1229 |
|
1230 // If possible, set shared object find file hook. |
|
1231 void (*set_hook)(find_file_hook_t) = (void(*)(find_file_hook_t))dlsym(libproc_handle, "Pset_find_file_hook"); |
|
1232 if (set_hook) { |
|
1233 // we found find file hook symbol, set up our hook function. |
|
1234 set_hook(find_file_hook); |
|
1235 } else if (getenv(SA_ALTROOT)) { |
|
1236 printf("libsaproc WARNING: %s set, but can't set file hook. " \ |
|
1237 "Did you use right version of libproc.so?\n", SA_ALTROOT); |
|
1238 } |
|
1239 |
|
1240 p_ps_prochandle_ID = env->GetFieldID(clazz, "p_ps_prochandle", "J"); |
|
1241 CHECK_EXCEPTION; |
|
1242 |
|
1243 libthread_db_handle_ID = env->GetFieldID(clazz, "libthread_db_handle", "J"); |
|
1244 CHECK_EXCEPTION; |
|
1245 |
|
1246 p_td_thragent_t_ID = env->GetFieldID(clazz, "p_td_thragent_t", "J"); |
|
1247 CHECK_EXCEPTION; |
|
1248 |
|
1249 p_td_init_ID = env->GetFieldID(clazz, "p_td_init", "J"); |
|
1250 CHECK_EXCEPTION; |
|
1251 |
|
1252 p_td_ta_new_ID = env->GetFieldID(clazz, "p_td_ta_new", "J"); |
|
1253 CHECK_EXCEPTION; |
|
1254 |
|
1255 p_td_ta_delete_ID = env->GetFieldID(clazz, "p_td_ta_delete", "J"); |
|
1256 CHECK_EXCEPTION; |
|
1257 |
|
1258 p_td_ta_thr_iter_ID = env->GetFieldID(clazz, "p_td_ta_thr_iter", "J"); |
|
1259 CHECK_EXCEPTION; |
|
1260 |
|
1261 p_td_thr_get_info_ID = env->GetFieldID(clazz, "p_td_thr_get_info", "J"); |
|
1262 CHECK_EXCEPTION; |
|
1263 |
|
1264 p_td_ta_map_id2thr_ID = env->GetFieldID(clazz, "p_td_ta_map_id2thr", "J"); |
|
1265 CHECK_EXCEPTION; |
|
1266 |
|
1267 p_td_thr_getgregs_ID = env->GetFieldID(clazz, "p_td_thr_getgregs", "J"); |
|
1268 CHECK_EXCEPTION; |
|
1269 |
|
1270 getThreadForThreadId_ID = env->GetMethodID(clazz, |
|
1271 "getThreadForThreadId", "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;"); |
|
1272 CHECK_EXCEPTION; |
|
1273 |
|
1274 pcRegIndex_ID = env->GetFieldID(clazz, "pcRegIndex", "I"); |
|
1275 CHECK_EXCEPTION; |
|
1276 |
|
1277 fpRegIndex_ID = env->GetFieldID(clazz, "fpRegIndex", "I"); |
|
1278 CHECK_EXCEPTION; |
|
1279 |
|
1280 createSenderFrame_ID = env->GetMethodID(clazz, |
|
1281 "createSenderFrame", "(Lsun/jvm/hotspot/debugger/proc/ProcCFrame;JJ)Lsun/jvm/hotspot/debugger/proc/ProcCFrame;"); |
|
1282 CHECK_EXCEPTION; |
|
1283 |
|
1284 createLoadObject_ID = env->GetMethodID(clazz, |
|
1285 "createLoadObject", "(Ljava/lang/String;JJ)Lsun/jvm/hotspot/debugger/cdbg/LoadObject;"); |
|
1286 CHECK_EXCEPTION; |
|
1287 |
|
1288 createClosestSymbol_ID = env->GetMethodID(clazz, |
|
1289 "createClosestSymbol", "(Ljava/lang/String;J)Lsun/jvm/hotspot/debugger/cdbg/ClosestSymbol;"); |
|
1290 CHECK_EXCEPTION; |
|
1291 |
|
1292 listAdd_ID = env->GetMethodID(env->FindClass("java/util/List"), "add", "(Ljava/lang/Object;)Z"); |
|
1293 CHECK_EXCEPTION; |
|
1294 |
|
1295 // part of the class sharing workaround |
|
1296 classes_jsa_fd_ID = env->GetFieldID(clazz, "classes_jsa_fd", "I"); |
|
1297 CHECK_EXCEPTION; |
|
1298 p_file_map_header_ID = env->GetFieldID(clazz, "p_file_map_header", "J"); |
|
1299 CHECK_EXCEPTION; |
|
1300 } |