author | dl |
Thu, 20 Dec 2012 13:44:06 +0000 | |
changeset 14914 | 69bfd88e3ea1 |
parent 11763 | b206e4b3186e |
child 15734 | 68f78eb98356 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
11763
b206e4b3186e
7009098: SA cannot open core files larger than 2GB on Linux 32-bit
poonam
parents:
7662
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
1 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5045
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5045
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5045
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
#include <stdarg.h> |
|
25 |
#include <stdio.h> |
|
26 |
#include <stdlib.h> |
|
27 |
#include <string.h> |
|
28 |
#include <fcntl.h> |
|
29 |
#include <thread_db.h> |
|
30 |
#include "libproc_impl.h" |
|
31 |
||
32 |
static const char* alt_root = NULL; |
|
33 |
static int alt_root_len = -1; |
|
34 |
||
35 |
#define SA_ALTROOT "SA_ALTROOT" |
|
36 |
||
37 |
static void init_alt_root() { |
|
38 |
if (alt_root_len == -1) { |
|
39 |
alt_root = getenv(SA_ALTROOT); |
|
40 |
if (alt_root) { |
|
41 |
alt_root_len = strlen(alt_root); |
|
42 |
} else { |
|
43 |
alt_root_len = 0; |
|
44 |
} |
|
45 |
} |
|
46 |
} |
|
47 |
||
48 |
int pathmap_open(const char* name) { |
|
49 |
int fd; |
|
50 |
char alt_path[PATH_MAX + 1]; |
|
51 |
||
52 |
init_alt_root(); |
|
53 |
||
54 |
if (alt_root_len > 0) { |
|
55 |
strcpy(alt_path, alt_root); |
|
56 |
strcat(alt_path, name); |
|
57 |
fd = open(alt_path, O_RDONLY); |
|
58 |
if (fd >= 0) { |
|
59 |
print_debug("path %s substituted for %s\n", alt_path, name); |
|
60 |
return fd; |
|
61 |
} |
|
62 |
||
63 |
if (strrchr(name, '/')) { |
|
64 |
strcpy(alt_path, alt_root); |
|
65 |
strcat(alt_path, strrchr(name, '/')); |
|
66 |
fd = open(alt_path, O_RDONLY); |
|
67 |
if (fd >= 0) { |
|
68 |
print_debug("path %s substituted for %s\n", alt_path, name); |
|
69 |
return fd; |
|
70 |
} |
|
71 |
} |
|
11763
b206e4b3186e
7009098: SA cannot open core files larger than 2GB on Linux 32-bit
poonam
parents:
7662
diff
changeset
|
72 |
} else { |
b206e4b3186e
7009098: SA cannot open core files larger than 2GB on Linux 32-bit
poonam
parents:
7662
diff
changeset
|
73 |
fd = open(name, O_RDONLY); |
b206e4b3186e
7009098: SA cannot open core files larger than 2GB on Linux 32-bit
poonam
parents:
7662
diff
changeset
|
74 |
if (fd >= 0) { |
b206e4b3186e
7009098: SA cannot open core files larger than 2GB on Linux 32-bit
poonam
parents:
7662
diff
changeset
|
75 |
return fd; |
b206e4b3186e
7009098: SA cannot open core files larger than 2GB on Linux 32-bit
poonam
parents:
7662
diff
changeset
|
76 |
} |
1 | 77 |
} |
78 |
||
79 |
return -1; |
|
80 |
} |
|
81 |
||
82 |
static bool _libsaproc_debug; |
|
83 |
||
84 |
void print_debug(const char* format,...) { |
|
85 |
if (_libsaproc_debug) { |
|
86 |
va_list alist; |
|
87 |
||
88 |
va_start(alist, format); |
|
89 |
fputs("libsaproc DEBUG: ", stderr); |
|
90 |
vfprintf(stderr, format, alist); |
|
91 |
va_end(alist); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
bool is_debug() { |
|
96 |
return _libsaproc_debug; |
|
97 |
} |
|
98 |
||
99 |
// initialize libproc |
|
100 |
bool init_libproc(bool debug) { |
|
101 |
// init debug mode |
|
102 |
_libsaproc_debug = debug; |
|
103 |
||
104 |
// initialize the thread_db library |
|
105 |
if (td_init() != TD_OK) { |
|
106 |
print_debug("libthread_db's td_init failed\n"); |
|
107 |
return false; |
|
108 |
} |
|
109 |
||
110 |
return true; |
|
111 |
} |
|
112 |
||
113 |
static void destroy_lib_info(struct ps_prochandle* ph) { |
|
114 |
lib_info* lib = ph->libs; |
|
115 |
while (lib) { |
|
116 |
lib_info *next = lib->next; |
|
117 |
if (lib->symtab) { |
|
118 |
destroy_symtab(lib->symtab); |
|
119 |
} |
|
120 |
free(lib); |
|
121 |
lib = next; |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
static void destroy_thread_info(struct ps_prochandle* ph) { |
|
126 |
thread_info* thr = ph->threads; |
|
127 |
while (thr) { |
|
128 |
thread_info *next = thr->next; |
|
129 |
free(thr); |
|
130 |
thr = next; |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
// ps_prochandle cleanup |
|
135 |
||
136 |
// ps_prochandle cleanup |
|
137 |
void Prelease(struct ps_prochandle* ph) { |
|
138 |
// do the "derived class" clean-up first |
|
139 |
ph->ops->release(ph); |
|
140 |
destroy_lib_info(ph); |
|
141 |
destroy_thread_info(ph); |
|
142 |
free(ph); |
|
143 |
} |
|
144 |
||
145 |
lib_info* add_lib_info(struct ps_prochandle* ph, const char* libname, uintptr_t base) { |
|
146 |
return add_lib_info_fd(ph, libname, -1, base); |
|
147 |
} |
|
148 |
||
149 |
lib_info* add_lib_info_fd(struct ps_prochandle* ph, const char* libname, int fd, uintptr_t base) { |
|
150 |
lib_info* newlib; |
|
151 |
||
152 |
if ( (newlib = (lib_info*) calloc(1, sizeof(struct lib_info))) == NULL) { |
|
153 |
print_debug("can't allocate memory for lib_info\n"); |
|
154 |
return NULL; |
|
155 |
} |
|
156 |
||
157 |
strncpy(newlib->name, libname, sizeof(newlib->name)); |
|
158 |
newlib->base = base; |
|
159 |
||
160 |
if (fd == -1) { |
|
161 |
if ( (newlib->fd = pathmap_open(newlib->name)) < 0) { |
|
162 |
print_debug("can't open shared object %s\n", newlib->name); |
|
163 |
free(newlib); |
|
164 |
return NULL; |
|
165 |
} |
|
166 |
} else { |
|
167 |
newlib->fd = fd; |
|
168 |
} |
|
169 |
||
170 |
// check whether we have got an ELF file. /proc/<pid>/map |
|
171 |
// gives out all file mappings and not just shared objects |
|
172 |
if (is_elf_file(newlib->fd) == false) { |
|
173 |
close(newlib->fd); |
|
174 |
free(newlib); |
|
175 |
return NULL; |
|
176 |
} |
|
177 |
||
5045
59dd2a304f1d
6932270: Allow Java's ELF symtab reader to use separate debuginfo files
never
parents:
1
diff
changeset
|
178 |
newlib->symtab = build_symtab(newlib->fd, libname); |
1 | 179 |
if (newlib->symtab == NULL) { |
180 |
print_debug("symbol table build failed for %s\n", newlib->name); |
|
181 |
} |
|
182 |
||
183 |
// even if symbol table building fails, we add the lib_info. |
|
184 |
// This is because we may need to read from the ELF file for core file |
|
185 |
// address read functionality. lookup_symbol checks for NULL symtab. |
|
186 |
if (ph->libs) { |
|
187 |
ph->lib_tail->next = newlib; |
|
188 |
ph->lib_tail = newlib; |
|
189 |
} else { |
|
190 |
ph->libs = ph->lib_tail = newlib; |
|
191 |
} |
|
192 |
ph->num_libs++; |
|
193 |
||
194 |
return newlib; |
|
195 |
} |
|
196 |
||
197 |
// lookup for a specific symbol |
|
198 |
uintptr_t lookup_symbol(struct ps_prochandle* ph, const char* object_name, |
|
199 |
const char* sym_name) { |
|
200 |
// ignore object_name. search in all libraries |
|
201 |
// FIXME: what should we do with object_name?? The library names are obtained |
|
202 |
// by parsing /proc/<pid>/maps, which may not be the same as object_name. |
|
203 |
// What we need is a utility to map object_name to real file name, something |
|
204 |
// dlopen() does by looking at LD_LIBRARY_PATH and /etc/ld.so.cache. For |
|
205 |
// now, we just ignore object_name and do a global search for the symbol. |
|
206 |
||
207 |
lib_info* lib = ph->libs; |
|
208 |
while (lib) { |
|
209 |
if (lib->symtab) { |
|
210 |
uintptr_t res = search_symbol(lib->symtab, lib->base, sym_name, NULL); |
|
211 |
if (res) return res; |
|
212 |
} |
|
213 |
lib = lib->next; |
|
214 |
} |
|
215 |
||
216 |
print_debug("lookup failed for symbol '%s' in obj '%s'\n", |
|
217 |
sym_name, object_name); |
|
218 |
return (uintptr_t) NULL; |
|
219 |
} |
|
220 |
||
221 |
||
222 |
const char* symbol_for_pc(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* poffset) { |
|
223 |
const char* res = NULL; |
|
224 |
lib_info* lib = ph->libs; |
|
225 |
while (lib) { |
|
226 |
if (lib->symtab && addr >= lib->base) { |
|
227 |
res = nearest_symbol(lib->symtab, addr - lib->base, poffset); |
|
228 |
if (res) return res; |
|
229 |
} |
|
230 |
lib = lib->next; |
|
231 |
} |
|
232 |
return NULL; |
|
233 |
} |
|
234 |
||
235 |
// add a thread to ps_prochandle |
|
236 |
thread_info* add_thread_info(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) { |
|
237 |
thread_info* newthr; |
|
238 |
if ( (newthr = (thread_info*) calloc(1, sizeof(thread_info))) == NULL) { |
|
239 |
print_debug("can't allocate memory for thread_info\n"); |
|
240 |
return NULL; |
|
241 |
} |
|
242 |
||
243 |
// initialize thread info |
|
244 |
newthr->pthread_id = pthread_id; |
|
245 |
newthr->lwp_id = lwp_id; |
|
246 |
||
247 |
// add new thread to the list |
|
248 |
newthr->next = ph->threads; |
|
249 |
ph->threads = newthr; |
|
250 |
ph->num_threads++; |
|
251 |
return newthr; |
|
252 |
} |
|
253 |
||
254 |
||
255 |
// struct used for client data from thread_db callback |
|
256 |
struct thread_db_client_data { |
|
257 |
struct ps_prochandle* ph; |
|
258 |
thread_info_callback callback; |
|
259 |
}; |
|
260 |
||
261 |
// callback function for libthread_db |
|
262 |
static int thread_db_callback(const td_thrhandle_t *th_p, void *data) { |
|
263 |
struct thread_db_client_data* ptr = (struct thread_db_client_data*) data; |
|
264 |
td_thrinfo_t ti; |
|
265 |
td_err_e err; |
|
266 |
||
267 |
memset(&ti, 0, sizeof(ti)); |
|
268 |
err = td_thr_get_info(th_p, &ti); |
|
269 |
if (err != TD_OK) { |
|
270 |
print_debug("libthread_db : td_thr_get_info failed, can't get thread info\n"); |
|
271 |
return err; |
|
272 |
} |
|
273 |
||
274 |
print_debug("thread_db : pthread %d (lwp %d)\n", ti.ti_tid, ti.ti_lid); |
|
275 |
||
276 |
if (ptr->callback(ptr->ph, ti.ti_tid, ti.ti_lid) != true) |
|
277 |
return TD_ERR; |
|
278 |
||
279 |
return TD_OK; |
|
280 |
} |
|
281 |
||
282 |
// read thread_info using libthread_db |
|
283 |
bool read_thread_info(struct ps_prochandle* ph, thread_info_callback cb) { |
|
284 |
struct thread_db_client_data mydata; |
|
285 |
td_thragent_t* thread_agent = NULL; |
|
286 |
if (td_ta_new(ph, &thread_agent) != TD_OK) { |
|
287 |
print_debug("can't create libthread_db agent\n"); |
|
288 |
return false; |
|
289 |
} |
|
290 |
||
291 |
mydata.ph = ph; |
|
292 |
mydata.callback = cb; |
|
293 |
||
294 |
// we use libthread_db iterator to iterate thru list of threads. |
|
295 |
if (td_ta_thr_iter(thread_agent, thread_db_callback, &mydata, |
|
296 |
TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY, |
|
297 |
TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS) != TD_OK) { |
|
298 |
td_ta_delete(thread_agent); |
|
299 |
return false; |
|
300 |
} |
|
301 |
||
302 |
// delete thread agent |
|
303 |
td_ta_delete(thread_agent); |
|
304 |
return true; |
|
305 |
} |
|
306 |
||
307 |
||
308 |
// get number of threads |
|
309 |
int get_num_threads(struct ps_prochandle* ph) { |
|
310 |
return ph->num_threads; |
|
311 |
} |
|
312 |
||
313 |
// get lwp_id of n'th thread |
|
314 |
lwpid_t get_lwp_id(struct ps_prochandle* ph, int index) { |
|
315 |
int count = 0; |
|
316 |
thread_info* thr = ph->threads; |
|
317 |
while (thr) { |
|
318 |
if (count == index) { |
|
319 |
return thr->lwp_id; |
|
320 |
} |
|
321 |
count++; |
|
322 |
thr = thr->next; |
|
323 |
} |
|
324 |
return -1; |
|
325 |
} |
|
326 |
||
327 |
// get regs for a given lwp |
|
328 |
bool get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, struct user_regs_struct* regs) { |
|
329 |
return ph->ops->get_lwp_regs(ph, lwp_id, regs); |
|
330 |
} |
|
331 |
||
332 |
// get number of shared objects |
|
333 |
int get_num_libs(struct ps_prochandle* ph) { |
|
334 |
return ph->num_libs; |
|
335 |
} |
|
336 |
||
337 |
// get name of n'th solib |
|
338 |
const char* get_lib_name(struct ps_prochandle* ph, int index) { |
|
339 |
int count = 0; |
|
340 |
lib_info* lib = ph->libs; |
|
341 |
while (lib) { |
|
342 |
if (count == index) { |
|
343 |
return lib->name; |
|
344 |
} |
|
345 |
count++; |
|
346 |
lib = lib->next; |
|
347 |
} |
|
348 |
return NULL; |
|
349 |
} |
|
350 |
||
351 |
// get base address of a lib |
|
352 |
uintptr_t get_lib_base(struct ps_prochandle* ph, int index) { |
|
353 |
int count = 0; |
|
354 |
lib_info* lib = ph->libs; |
|
355 |
while (lib) { |
|
356 |
if (count == index) { |
|
357 |
return lib->base; |
|
358 |
} |
|
359 |
count++; |
|
360 |
lib = lib->next; |
|
361 |
} |
|
362 |
return (uintptr_t)NULL; |
|
363 |
} |
|
364 |
||
365 |
bool find_lib(struct ps_prochandle* ph, const char *lib_name) { |
|
366 |
lib_info *p = ph->libs; |
|
367 |
while (p) { |
|
368 |
if (strcmp(p->name, lib_name) == 0) { |
|
369 |
return true; |
|
370 |
} |
|
371 |
p = p->next; |
|
372 |
} |
|
373 |
return false; |
|
374 |
} |
|
375 |
||
376 |
//-------------------------------------------------------------------------- |
|
377 |
// proc service functions |
|
378 |
||
379 |
// get process id |
|
380 |
pid_t ps_getpid(struct ps_prochandle *ph) { |
|
381 |
return ph->pid; |
|
382 |
} |
|
383 |
||
384 |
// ps_pglobal_lookup() looks up the symbol sym_name in the symbol table |
|
385 |
// of the load object object_name in the target process identified by ph. |
|
386 |
// It returns the symbol's value as an address in the target process in |
|
387 |
// *sym_addr. |
|
388 |
||
389 |
ps_err_e ps_pglobal_lookup(struct ps_prochandle *ph, const char *object_name, |
|
390 |
const char *sym_name, psaddr_t *sym_addr) { |
|
391 |
*sym_addr = (psaddr_t) lookup_symbol(ph, object_name, sym_name); |
|
392 |
return (*sym_addr ? PS_OK : PS_NOSYM); |
|
393 |
} |
|
394 |
||
395 |
// read "size" bytes info "buf" from address "addr" |
|
396 |
ps_err_e ps_pdread(struct ps_prochandle *ph, psaddr_t addr, |
|
397 |
void *buf, size_t size) { |
|
398 |
return ph->ops->p_pread(ph, (uintptr_t) addr, buf, size)? PS_OK: PS_ERR; |
|
399 |
} |
|
400 |
||
401 |
// write "size" bytes of data to debuggee at address "addr" |
|
402 |
ps_err_e ps_pdwrite(struct ps_prochandle *ph, psaddr_t addr, |
|
403 |
const void *buf, size_t size) { |
|
404 |
return ph->ops->p_pwrite(ph, (uintptr_t)addr, buf, size)? PS_OK: PS_ERR; |
|
405 |
} |
|
406 |
||
407 |
// ------------------------------------------------------------------------ |
|
408 |
// Functions below this point are not yet implemented. They are here only |
|
409 |
// to make the linker happy. |
|
410 |
||
411 |
ps_err_e ps_lsetfpregs(struct ps_prochandle *ph, lwpid_t lid, const prfpregset_t *fpregs) { |
|
412 |
print_debug("ps_lsetfpregs not implemented\n"); |
|
413 |
return PS_OK; |
|
414 |
} |
|
415 |
||
416 |
ps_err_e ps_lsetregs(struct ps_prochandle *ph, lwpid_t lid, const prgregset_t gregset) { |
|
417 |
print_debug("ps_lsetregs not implemented\n"); |
|
418 |
return PS_OK; |
|
419 |
} |
|
420 |
||
421 |
ps_err_e ps_lgetfpregs(struct ps_prochandle *ph, lwpid_t lid, prfpregset_t *fpregs) { |
|
422 |
print_debug("ps_lgetfpregs not implemented\n"); |
|
423 |
return PS_OK; |
|
424 |
} |
|
425 |
||
426 |
ps_err_e ps_lgetregs(struct ps_prochandle *ph, lwpid_t lid, prgregset_t gregset) { |
|
427 |
print_debug("ps_lgetfpregs not implemented\n"); |
|
428 |
return PS_OK; |
|
429 |
} |
|
430 |
||
431 |
// new libthread_db of NPTL seem to require this symbol |
|
432 |
ps_err_e ps_get_thread_area() { |
|
433 |
print_debug("ps_get_thread_area not implemented\n"); |
|
434 |
return PS_OK; |
|
435 |
} |