annotate agent/src/os/solaris/proc/saproc.cpp @ 579:0fbdb4381b99

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