Mercurial > hg > truffle
comparison agent/src/os/bsd/ps_core.c @ 3960:f08d439fab8c
7089790: integrate bsd-port changes
Reviewed-by: kvn, twisti, jrose
Contributed-by: Kurt Miller <kurt@intricatesoftware.com>, Greg Lewis <glewis@eyesbeyond.com>, Jung-uk Kim <jkim@freebsd.org>, Christos Zoulas <christos@zoulas.com>, Landon Fuller <landonf@plausible.coop>, The FreeBSD Foundation <board@freebsdfoundation.org>, Michael Franz <mvfranz@gmail.com>, Roger Hoover <rhoover@apple.com>, Alexander Strange <astrange@apple.com>
author | never |
---|---|
date | Sun, 25 Sep 2011 16:03:29 -0700 |
parents | |
children | da91efe96a93 |
comparison
equal
deleted
inserted
replaced
3959:eda6988c0d81 | 3960:f08d439fab8c |
---|---|
1 /* | |
2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 * | |
23 */ | |
24 | |
25 #include <jni.h> | |
26 #include <unistd.h> | |
27 #include <fcntl.h> | |
28 #include <string.h> | |
29 #include <stdlib.h> | |
30 #include <stddef.h> | |
31 #include <elf.h> | |
32 #include <link.h> | |
33 #include "libproc_impl.h" | |
34 #include "salibelf.h" | |
35 | |
36 // This file has the libproc implementation to read core files. | |
37 // For live processes, refer to ps_proc.c. Portions of this is adapted | |
38 // /modelled after Solaris libproc.so (in particular Pcore.c) | |
39 | |
40 //---------------------------------------------------------------------- | |
41 // ps_prochandle cleanup helper functions | |
42 | |
43 // close all file descriptors | |
44 static void close_elf_files(struct ps_prochandle* ph) { | |
45 lib_info* lib = NULL; | |
46 | |
47 // close core file descriptor | |
48 if (ph->core->core_fd >= 0) | |
49 close(ph->core->core_fd); | |
50 | |
51 // close exec file descriptor | |
52 if (ph->core->exec_fd >= 0) | |
53 close(ph->core->exec_fd); | |
54 | |
55 // close interp file descriptor | |
56 if (ph->core->interp_fd >= 0) | |
57 close(ph->core->interp_fd); | |
58 | |
59 // close class share archive file | |
60 if (ph->core->classes_jsa_fd >= 0) | |
61 close(ph->core->classes_jsa_fd); | |
62 | |
63 // close all library file descriptors | |
64 lib = ph->libs; | |
65 while (lib) { | |
66 int fd = lib->fd; | |
67 if (fd >= 0 && fd != ph->core->exec_fd) close(fd); | |
68 lib = lib->next; | |
69 } | |
70 } | |
71 | |
72 // clean all map_info stuff | |
73 static void destroy_map_info(struct ps_prochandle* ph) { | |
74 map_info* map = ph->core->maps; | |
75 while (map) { | |
76 map_info* next = map->next; | |
77 free(map); | |
78 map = next; | |
79 } | |
80 | |
81 if (ph->core->map_array) { | |
82 free(ph->core->map_array); | |
83 } | |
84 | |
85 // Part of the class sharing workaround | |
86 map = ph->core->class_share_maps; | |
87 while (map) { | |
88 map_info* next = map->next; | |
89 free(map); | |
90 map = next; | |
91 } | |
92 } | |
93 | |
94 // ps_prochandle operations | |
95 static void core_release(struct ps_prochandle* ph) { | |
96 if (ph->core) { | |
97 close_elf_files(ph); | |
98 destroy_map_info(ph); | |
99 free(ph->core); | |
100 } | |
101 } | |
102 | |
103 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { | |
104 map_info* map; | |
105 if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { | |
106 print_debug("can't allocate memory for map_info\n"); | |
107 return NULL; | |
108 } | |
109 | |
110 // initialize map | |
111 map->fd = fd; | |
112 map->offset = offset; | |
113 map->vaddr = vaddr; | |
114 map->memsz = memsz; | |
115 return map; | |
116 } | |
117 | |
118 // add map info with given fd, offset, vaddr and memsz | |
119 static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, | |
120 uintptr_t vaddr, size_t memsz) { | |
121 map_info* map; | |
122 if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { | |
123 return NULL; | |
124 } | |
125 | |
126 // add this to map list | |
127 map->next = ph->core->maps; | |
128 ph->core->maps = map; | |
129 ph->core->num_maps++; | |
130 | |
131 return map; | |
132 } | |
133 | |
134 // Part of the class sharing workaround | |
135 static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset, | |
136 uintptr_t vaddr, size_t memsz) { | |
137 map_info* map; | |
138 if ((map = allocate_init_map(ph->core->classes_jsa_fd, | |
139 offset, vaddr, memsz)) == NULL) { | |
140 return NULL; | |
141 } | |
142 | |
143 map->next = ph->core->class_share_maps; | |
144 ph->core->class_share_maps = map; | |
145 return map; | |
146 } | |
147 | |
148 // Return the map_info for the given virtual address. We keep a sorted | |
149 // array of pointers in ph->map_array, so we can binary search. | |
150 static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) | |
151 { | |
152 int mid, lo = 0, hi = ph->core->num_maps - 1; | |
153 map_info *mp; | |
154 | |
155 while (hi - lo > 1) { | |
156 mid = (lo + hi) / 2; | |
157 if (addr >= ph->core->map_array[mid]->vaddr) | |
158 lo = mid; | |
159 else | |
160 hi = mid; | |
161 } | |
162 | |
163 if (addr < ph->core->map_array[hi]->vaddr) | |
164 mp = ph->core->map_array[lo]; | |
165 else | |
166 mp = ph->core->map_array[hi]; | |
167 | |
168 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) | |
169 return (mp); | |
170 | |
171 | |
172 // Part of the class sharing workaround | |
173 // Unfortunately, we have no way of detecting -Xshare state. | |
174 // Check out the share maps atlast, if we don't find anywhere. | |
175 // This is done this way so to avoid reading share pages | |
176 // ahead of other normal maps. For eg. with -Xshare:off we don't | |
177 // want to prefer class sharing data to data from core. | |
178 mp = ph->core->class_share_maps; | |
179 if (mp) { | |
180 print_debug("can't locate map_info at 0x%lx, trying class share maps\n", | |
181 addr); | |
182 } | |
183 while (mp) { | |
184 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { | |
185 print_debug("located map_info at 0x%lx from class share maps\n", | |
186 addr); | |
187 return (mp); | |
188 } | |
189 mp = mp->next; | |
190 } | |
191 | |
192 print_debug("can't locate map_info at 0x%lx\n", addr); | |
193 return (NULL); | |
194 } | |
195 | |
196 //--------------------------------------------------------------- | |
197 // Part of the class sharing workaround: | |
198 // | |
199 // With class sharing, pages are mapped from classes[_g].jsa file. | |
200 // The read-only class sharing pages are mapped as MAP_SHARED, | |
201 // PROT_READ pages. These pages are not dumped into core dump. | |
202 // With this workaround, these pages are read from classes[_g].jsa. | |
203 | |
204 // FIXME: !HACK ALERT! | |
205 // The format of sharing achive file header is needed to read shared heap | |
206 // file mappings. For now, I am hard coding portion of FileMapHeader here. | |
207 // Refer to filemap.hpp. | |
208 | |
209 // FileMapHeader describes the shared space data in the file to be | |
210 // mapped. This structure gets written to a file. It is not a class, | |
211 // so that the compilers don't add any compiler-private data to it. | |
212 | |
213 // Refer to CompactingPermGenGen::n_regions in compactingPermGenGen.hpp | |
214 #define NUM_SHARED_MAPS 4 | |
215 | |
216 // Refer to FileMapInfo::_current_version in filemap.hpp | |
217 #define CURRENT_ARCHIVE_VERSION 1 | |
218 | |
219 struct FileMapHeader { | |
220 int _magic; // identify file type. | |
221 int _version; // (from enum, above.) | |
222 size_t _alignment; // how shared archive should be aligned | |
223 | |
224 struct space_info { | |
225 int _file_offset; // sizeof(this) rounded to vm page size | |
226 char* _base; // copy-on-write base address | |
227 size_t _capacity; // for validity checking | |
228 size_t _used; // for setting space top on read | |
229 | |
230 // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with | |
231 // the C type matching the C++ bool type on any given platform. For | |
232 // Hotspot on BSD we assume the corresponding C type is char but | |
233 // licensees on BSD versions may need to adjust the type of these fields. | |
234 char _read_only; // read only space? | |
235 char _allow_exec; // executable code in space? | |
236 | |
237 } _space[NUM_SHARED_MAPS]; // was _space[CompactingPermGenGen::n_regions]; | |
238 | |
239 // Ignore the rest of the FileMapHeader. We don't need those fields here. | |
240 }; | |
241 | |
242 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) { | |
243 jboolean i; | |
244 if (ps_pread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { | |
245 *pvalue = i; | |
246 return true; | |
247 } else { | |
248 return false; | |
249 } | |
250 } | |
251 | |
252 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { | |
253 uintptr_t uip; | |
254 if (ps_pread(ph, (psaddr_t) addr, &uip, sizeof(uip)) == PS_OK) { | |
255 *pvalue = uip; | |
256 return true; | |
257 } else { | |
258 return false; | |
259 } | |
260 } | |
261 | |
262 // used to read strings from debuggee | |
263 static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { | |
264 size_t i = 0; | |
265 char c = ' '; | |
266 | |
267 while (c != '\0') { | |
268 if (ps_pread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) | |
269 return false; | |
270 if (i < size - 1) | |
271 buf[i] = c; | |
272 else // smaller buffer | |
273 return false; | |
274 i++; addr++; | |
275 } | |
276 | |
277 buf[i] = '\0'; | |
278 return true; | |
279 } | |
280 | |
281 #define USE_SHARED_SPACES_SYM "UseSharedSpaces" | |
282 // mangled name of Arguments::SharedArchivePath | |
283 #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" | |
284 | |
285 static bool init_classsharing_workaround(struct ps_prochandle* ph) { | |
286 lib_info* lib = ph->libs; | |
287 while (lib != NULL) { | |
288 // we are iterating over shared objects from the core dump. look for | |
289 // libjvm[_g].so. | |
290 const char *jvm_name = 0; | |
291 if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0 || | |
292 (jvm_name = strstr(lib->name, "/libjvm_g.so")) != 0) { | |
293 char classes_jsa[PATH_MAX]; | |
294 struct FileMapHeader header; | |
295 size_t n = 0; | |
296 int fd = -1, m = 0; | |
297 uintptr_t base = 0, useSharedSpacesAddr = 0; | |
298 uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; | |
299 jboolean useSharedSpaces = 0; | |
300 | |
301 memset(classes_jsa, 0, sizeof(classes_jsa)); | |
302 jvm_name = lib->name; | |
303 useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); | |
304 if (useSharedSpacesAddr == 0) { | |
305 print_debug("can't lookup 'UseSharedSpaces' flag\n"); | |
306 return false; | |
307 } | |
308 | |
309 // Hotspot vm types are not exported to build this library. So | |
310 // using equivalent type jboolean to read the value of | |
311 // UseSharedSpaces which is same as hotspot type "bool". | |
312 if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { | |
313 print_debug("can't read the value of 'UseSharedSpaces' flag\n"); | |
314 return false; | |
315 } | |
316 | |
317 if ((int)useSharedSpaces == 0) { | |
318 print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); | |
319 return true; | |
320 } | |
321 | |
322 sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); | |
323 if (sharedArchivePathAddrAddr == 0) { | |
324 print_debug("can't lookup shared archive path symbol\n"); | |
325 return false; | |
326 } | |
327 | |
328 if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { | |
329 print_debug("can't read shared archive path pointer\n"); | |
330 return false; | |
331 } | |
332 | |
333 if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { | |
334 print_debug("can't read shared archive path value\n"); | |
335 return false; | |
336 } | |
337 | |
338 print_debug("looking for %s\n", classes_jsa); | |
339 // open the class sharing archive file | |
340 fd = pathmap_open(classes_jsa); | |
341 if (fd < 0) { | |
342 print_debug("can't open %s!\n", classes_jsa); | |
343 ph->core->classes_jsa_fd = -1; | |
344 return false; | |
345 } else { | |
346 print_debug("opened %s\n", classes_jsa); | |
347 } | |
348 | |
349 // read FileMapHeader from the file | |
350 memset(&header, 0, sizeof(struct FileMapHeader)); | |
351 if ((n = read(fd, &header, sizeof(struct FileMapHeader))) | |
352 != sizeof(struct FileMapHeader)) { | |
353 print_debug("can't read shared archive file map header from %s\n", classes_jsa); | |
354 close(fd); | |
355 return false; | |
356 } | |
357 | |
358 // check file magic | |
359 if (header._magic != 0xf00baba2) { | |
360 print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n", | |
361 classes_jsa, header._magic); | |
362 close(fd); | |
363 return false; | |
364 } | |
365 | |
366 // check version | |
367 if (header._version != CURRENT_ARCHIVE_VERSION) { | |
368 print_debug("%s has wrong shared archive file version %d, expecting %d\n", | |
369 classes_jsa, header._version, CURRENT_ARCHIVE_VERSION); | |
370 close(fd); | |
371 return false; | |
372 } | |
373 | |
374 ph->core->classes_jsa_fd = fd; | |
375 // add read-only maps from classes[_g].jsa to the list of maps | |
376 for (m = 0; m < NUM_SHARED_MAPS; m++) { | |
377 if (header._space[m]._read_only) { | |
378 base = (uintptr_t) header._space[m]._base; | |
379 // no need to worry about the fractional pages at-the-end. | |
380 // possible fractional pages are handled by core_read_data. | |
381 add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, | |
382 base, (size_t) header._space[m]._used); | |
383 print_debug("added a share archive map at 0x%lx\n", base); | |
384 } | |
385 } | |
386 return true; | |
387 } | |
388 lib = lib->next; | |
389 } | |
390 return true; | |
391 } | |
392 | |
393 | |
394 //--------------------------------------------------------------------------- | |
395 // functions to handle map_info | |
396 | |
397 // Order mappings based on virtual address. We use this function as the | |
398 // callback for sorting the array of map_info pointers. | |
399 static int core_cmp_mapping(const void *lhsp, const void *rhsp) | |
400 { | |
401 const map_info *lhs = *((const map_info **)lhsp); | |
402 const map_info *rhs = *((const map_info **)rhsp); | |
403 | |
404 if (lhs->vaddr == rhs->vaddr) | |
405 return (0); | |
406 | |
407 return (lhs->vaddr < rhs->vaddr ? -1 : 1); | |
408 } | |
409 | |
410 // we sort map_info by starting virtual address so that we can do | |
411 // binary search to read from an address. | |
412 static bool sort_map_array(struct ps_prochandle* ph) { | |
413 size_t num_maps = ph->core->num_maps; | |
414 map_info* map = ph->core->maps; | |
415 int i = 0; | |
416 | |
417 // allocate map_array | |
418 map_info** array; | |
419 if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { | |
420 print_debug("can't allocate memory for map array\n"); | |
421 return false; | |
422 } | |
423 | |
424 // add maps to array | |
425 while (map) { | |
426 array[i] = map; | |
427 i++; | |
428 map = map->next; | |
429 } | |
430 | |
431 // sort is called twice. If this is second time, clear map array | |
432 if (ph->core->map_array) free(ph->core->map_array); | |
433 ph->core->map_array = array; | |
434 // sort the map_info array by base virtual address. | |
435 qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), | |
436 core_cmp_mapping); | |
437 | |
438 // print map | |
439 if (is_debug()) { | |
440 int j = 0; | |
441 print_debug("---- sorted virtual address map ----\n"); | |
442 for (j = 0; j < ph->core->num_maps; j++) { | |
443 print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr, | |
444 ph->core->map_array[j]->memsz); | |
445 } | |
446 } | |
447 | |
448 return true; | |
449 } | |
450 | |
451 #ifndef MIN | |
452 #define MIN(x, y) (((x) < (y))? (x): (y)) | |
453 #endif | |
454 | |
455 static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) { | |
456 ssize_t resid = size; | |
457 int page_size=sysconf(_SC_PAGE_SIZE); | |
458 while (resid != 0) { | |
459 map_info *mp = core_lookup(ph, addr); | |
460 uintptr_t mapoff; | |
461 ssize_t len, rem; | |
462 off_t off; | |
463 int fd; | |
464 | |
465 if (mp == NULL) | |
466 break; /* No mapping for this address */ | |
467 | |
468 fd = mp->fd; | |
469 mapoff = addr - mp->vaddr; | |
470 len = MIN(resid, mp->memsz - mapoff); | |
471 off = mp->offset + mapoff; | |
472 | |
473 if ((len = pread(fd, buf, len, off)) <= 0) | |
474 break; | |
475 | |
476 resid -= len; | |
477 addr += len; | |
478 buf = (char *)buf + len; | |
479 | |
480 // mappings always start at page boundary. But, may end in fractional | |
481 // page. fill zeros for possible fractional page at the end of a mapping. | |
482 rem = mp->memsz % page_size; | |
483 if (rem > 0) { | |
484 rem = page_size - rem; | |
485 len = MIN(resid, rem); | |
486 resid -= len; | |
487 addr += len; | |
488 // we are not assuming 'buf' to be zero initialized. | |
489 memset(buf, 0, len); | |
490 buf += len; | |
491 } | |
492 } | |
493 | |
494 if (resid) { | |
495 print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n", | |
496 size, addr, resid); | |
497 return false; | |
498 } else { | |
499 return true; | |
500 } | |
501 } | |
502 | |
503 // null implementation for write | |
504 static bool core_write_data(struct ps_prochandle* ph, | |
505 uintptr_t addr, const char *buf , size_t size) { | |
506 return false; | |
507 } | |
508 | |
509 static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, | |
510 struct reg* regs) { | |
511 // for core we have cached the lwp regs from NOTE section | |
512 thread_info* thr = ph->threads; | |
513 while (thr) { | |
514 if (thr->lwp_id == lwp_id) { | |
515 memcpy(regs, &thr->regs, sizeof(struct reg)); | |
516 return true; | |
517 } | |
518 thr = thr->next; | |
519 } | |
520 return false; | |
521 } | |
522 | |
523 static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo) { | |
524 print_debug("core_get_lwp_info not implemented\n"); | |
525 return false; | |
526 } | |
527 | |
528 static ps_prochandle_ops core_ops = { | |
529 .release= core_release, | |
530 .p_pread= core_read_data, | |
531 .p_pwrite= core_write_data, | |
532 .get_lwp_regs= core_get_lwp_regs, | |
533 .get_lwp_info= core_get_lwp_info | |
534 }; | |
535 | |
536 // read regs and create thread from NT_PRSTATUS entries from core file | |
537 static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) { | |
538 // we have to read prstatus_t from buf | |
539 // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t"); | |
540 prstatus_t* prstat = (prstatus_t*) buf; | |
541 thread_info* newthr; | |
542 print_debug("got integer regset for lwp %d\n", prstat->pr_pid); | |
543 // we set pthread_t to -1 for core dump | |
544 if((newthr = add_thread_info(ph, (pthread_t) -1, prstat->pr_pid)) == NULL) | |
545 return false; | |
546 | |
547 // copy regs | |
548 memcpy(&newthr->regs, &prstat->pr_reg, sizeof(struct reg)); | |
549 | |
550 if (is_debug()) { | |
551 print_debug("integer regset\n"); | |
552 #ifdef i386 | |
553 // print the regset | |
554 print_debug("\teax = 0x%x\n", newthr->regs.r_eax); | |
555 print_debug("\tebx = 0x%x\n", newthr->regs.r_ebx); | |
556 print_debug("\tecx = 0x%x\n", newthr->regs.r_ecx); | |
557 print_debug("\tedx = 0x%x\n", newthr->regs.r_edx); | |
558 print_debug("\tesp = 0x%x\n", newthr->regs.r_esp); | |
559 print_debug("\tebp = 0x%x\n", newthr->regs.r_ebp); | |
560 print_debug("\tesi = 0x%x\n", newthr->regs.r_esi); | |
561 print_debug("\tedi = 0x%x\n", newthr->regs.r_edi); | |
562 print_debug("\teip = 0x%x\n", newthr->regs.r_eip); | |
563 #endif | |
564 | |
565 #if defined(amd64) || defined(x86_64) | |
566 // print the regset | |
567 print_debug("\tr15 = 0x%lx\n", newthr->regs.r_r15); | |
568 print_debug("\tr14 = 0x%lx\n", newthr->regs.r_r14); | |
569 print_debug("\tr13 = 0x%lx\n", newthr->regs.r_r13); | |
570 print_debug("\tr12 = 0x%lx\n", newthr->regs.r_r12); | |
571 print_debug("\trbp = 0x%lx\n", newthr->regs.r_rbp); | |
572 print_debug("\trbx = 0x%lx\n", newthr->regs.r_rbx); | |
573 print_debug("\tr11 = 0x%lx\n", newthr->regs.r_r11); | |
574 print_debug("\tr10 = 0x%lx\n", newthr->regs.r_r10); | |
575 print_debug("\tr9 = 0x%lx\n", newthr->regs.r_r9); | |
576 print_debug("\tr8 = 0x%lx\n", newthr->regs.r_r8); | |
577 print_debug("\trax = 0x%lx\n", newthr->regs.r_rax); | |
578 print_debug("\trcx = 0x%lx\n", newthr->regs.r_rcx); | |
579 print_debug("\trdx = 0x%lx\n", newthr->regs.r_rdx); | |
580 print_debug("\trsi = 0x%lx\n", newthr->regs.r_rsi); | |
581 print_debug("\trdi = 0x%lx\n", newthr->regs.r_rdi); | |
582 //print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax); | |
583 print_debug("\trip = 0x%lx\n", newthr->regs.r_rip); | |
584 print_debug("\tcs = 0x%lx\n", newthr->regs.r_cs); | |
585 //print_debug("\teflags = 0x%lx\n", newthr->regs.eflags); | |
586 print_debug("\trsp = 0x%lx\n", newthr->regs.r_rsp); | |
587 print_debug("\tss = 0x%lx\n", newthr->regs.r_ss); | |
588 //print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base); | |
589 //print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base); | |
590 //print_debug("\tds = 0x%lx\n", newthr->regs.ds); | |
591 //print_debug("\tes = 0x%lx\n", newthr->regs.es); | |
592 //print_debug("\tfs = 0x%lx\n", newthr->regs.fs); | |
593 //print_debug("\tgs = 0x%lx\n", newthr->regs.gs); | |
594 #endif | |
595 } | |
596 | |
597 return true; | |
598 } | |
599 | |
600 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y)) | |
601 | |
602 // read NT_PRSTATUS entries from core NOTE segment | |
603 static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) { | |
604 char* buf = NULL; | |
605 char* p = NULL; | |
606 size_t size = note_phdr->p_filesz; | |
607 | |
608 // we are interested in just prstatus entries. we will ignore the rest. | |
609 // Advance the seek pointer to the start of the PT_NOTE data | |
610 if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) { | |
611 print_debug("failed to lseek to PT_NOTE data\n"); | |
612 return false; | |
613 } | |
614 | |
615 // Now process the PT_NOTE structures. Each one is preceded by | |
616 // an Elf{32/64}_Nhdr structure describing its type and size. | |
617 if ( (buf = (char*) malloc(size)) == NULL) { | |
618 print_debug("can't allocate memory for reading core notes\n"); | |
619 goto err; | |
620 } | |
621 | |
622 // read notes into buffer | |
623 if (read(ph->core->core_fd, buf, size) != size) { | |
624 print_debug("failed to read notes, core file must have been truncated\n"); | |
625 goto err; | |
626 } | |
627 | |
628 p = buf; | |
629 while (p < buf + size) { | |
630 ELF_NHDR* notep = (ELF_NHDR*) p; | |
631 char* descdata = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4); | |
632 print_debug("Note header with n_type = %d and n_descsz = %u\n", | |
633 notep->n_type, notep->n_descsz); | |
634 | |
635 if (notep->n_type == NT_PRSTATUS) { | |
636 if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) | |
637 return false; | |
638 } | |
639 p = descdata + ROUNDUP(notep->n_descsz, 4); | |
640 } | |
641 | |
642 free(buf); | |
643 return true; | |
644 | |
645 err: | |
646 if (buf) free(buf); | |
647 return false; | |
648 } | |
649 | |
650 // read all segments from core file | |
651 static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) { | |
652 int i = 0; | |
653 ELF_PHDR* phbuf = NULL; | |
654 ELF_PHDR* core_php = NULL; | |
655 | |
656 if ((phbuf = read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL) | |
657 return false; | |
658 | |
659 /* | |
660 * Now iterate through the program headers in the core file. | |
661 * We're interested in two types of Phdrs: PT_NOTE (which | |
662 * contains a set of saved /proc structures), and PT_LOAD (which | |
663 * represents a memory mapping from the process's address space). | |
664 * | |
665 * Difference b/w Solaris PT_NOTE and BSD PT_NOTE: | |
666 * | |
667 * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present) | |
668 * contains /proc structs in the pre-2.6 unstructured /proc format. the last | |
669 * PT_NOTE has data in new /proc format. | |
670 * | |
671 * In Solaris, there is only one pstatus (process status). pstatus contains | |
672 * integer register set among other stuff. For each LWP, we have one lwpstatus | |
673 * entry that has integer regset for that LWP. | |
674 * | |
675 * Linux threads are actually 'clone'd processes. To support core analysis | |
676 * of "multithreaded" process, Linux creates more than one pstatus (called | |
677 * "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one | |
678 * "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular | |
679 * function "elf_core_dump". | |
680 */ | |
681 | |
682 for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) { | |
683 switch (core_php->p_type) { | |
684 case PT_NOTE: | |
685 if (core_handle_note(ph, core_php) != true) goto err; | |
686 break; | |
687 | |
688 case PT_LOAD: { | |
689 if (core_php->p_filesz != 0) { | |
690 if (add_map_info(ph, ph->core->core_fd, core_php->p_offset, | |
691 core_php->p_vaddr, core_php->p_filesz) == NULL) goto err; | |
692 } | |
693 break; | |
694 } | |
695 } | |
696 | |
697 core_php++; | |
698 } | |
699 | |
700 free(phbuf); | |
701 return true; | |
702 err: | |
703 free(phbuf); | |
704 return false; | |
705 } | |
706 | |
707 // read segments of a shared object | |
708 static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) { | |
709 int i = 0; | |
710 ELF_PHDR* phbuf; | |
711 ELF_PHDR* lib_php = NULL; | |
712 | |
713 if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) | |
714 return false; | |
715 | |
716 // we want to process only PT_LOAD segments that are not writable. | |
717 // i.e., text segments. The read/write/exec (data) segments would | |
718 // have been already added from core file segments. | |
719 for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) { | |
720 if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) { | |
721 if (add_map_info(ph, lib_fd, lib_php->p_offset, lib_php->p_vaddr + lib_base, lib_php->p_filesz) == NULL) | |
722 goto err; | |
723 } | |
724 lib_php++; | |
725 } | |
726 | |
727 free(phbuf); | |
728 return true; | |
729 err: | |
730 free(phbuf); | |
731 return false; | |
732 } | |
733 | |
734 // process segments from interpreter (ld-elf.so.1) | |
735 static bool read_interp_segments(struct ps_prochandle* ph) { | |
736 ELF_EHDR interp_ehdr; | |
737 | |
738 if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) { | |
739 print_debug("interpreter is not a valid ELF file\n"); | |
740 return false; | |
741 } | |
742 | |
743 if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) { | |
744 print_debug("can't read segments of interpreter\n"); | |
745 return false; | |
746 } | |
747 | |
748 return true; | |
749 } | |
750 | |
751 // process segments of a a.out | |
752 static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) { | |
753 int i = 0; | |
754 ELF_PHDR* phbuf = NULL; | |
755 ELF_PHDR* exec_php = NULL; | |
756 | |
757 if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL) | |
758 return false; | |
759 | |
760 for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) { | |
761 switch (exec_php->p_type) { | |
762 | |
763 // add mappings for PT_LOAD segments | |
764 case PT_LOAD: { | |
765 // add only non-writable segments of non-zero filesz | |
766 if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) { | |
767 if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err; | |
768 } | |
769 break; | |
770 } | |
771 | |
772 // read the interpreter and it's segments | |
773 case PT_INTERP: { | |
774 char interp_name[BUF_SIZE]; | |
775 | |
776 pread(ph->core->exec_fd, interp_name, MIN(exec_php->p_filesz, BUF_SIZE), exec_php->p_offset); | |
777 print_debug("ELF interpreter %s\n", interp_name); | |
778 // read interpreter segments as well | |
779 if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) { | |
780 print_debug("can't open runtime loader\n"); | |
781 goto err; | |
782 } | |
783 break; | |
784 } | |
785 | |
786 // from PT_DYNAMIC we want to read address of first link_map addr | |
787 case PT_DYNAMIC: { | |
788 ph->core->dynamic_addr = exec_php->p_vaddr; | |
789 print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr); | |
790 break; | |
791 } | |
792 | |
793 } // switch | |
794 exec_php++; | |
795 } // for | |
796 | |
797 free(phbuf); | |
798 return true; | |
799 err: | |
800 free(phbuf); | |
801 return false; | |
802 } | |
803 | |
804 | |
805 #define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug, r_map) | |
806 #define LD_BASE_OFFSET offsetof(struct r_debug, r_ldbase) | |
807 #define LINK_MAP_ADDR_OFFSET offsetof(struct link_map, l_addr) | |
808 #define LINK_MAP_NAME_OFFSET offsetof(struct link_map, l_name) | |
809 #define LINK_MAP_NEXT_OFFSET offsetof(struct link_map, l_next) | |
810 | |
811 // read shared library info from runtime linker's data structures. | |
812 // This work is done by librtlb_db in Solaris | |
813 static bool read_shared_lib_info(struct ps_prochandle* ph) { | |
814 uintptr_t addr = ph->core->dynamic_addr; | |
815 uintptr_t debug_base; | |
816 uintptr_t first_link_map_addr; | |
817 uintptr_t ld_base_addr; | |
818 uintptr_t link_map_addr; | |
819 uintptr_t lib_base_diff; | |
820 uintptr_t lib_base; | |
821 uintptr_t lib_name_addr; | |
822 char lib_name[BUF_SIZE]; | |
823 ELF_DYN dyn; | |
824 ELF_EHDR elf_ehdr; | |
825 int lib_fd; | |
826 | |
827 // _DYNAMIC has information of the form | |
828 // [tag] [data] [tag] [data] ..... | |
829 // Both tag and data are pointer sized. | |
830 // We look for dynamic info with DT_DEBUG. This has shared object info. | |
831 // refer to struct r_debug in link.h | |
832 | |
833 dyn.d_tag = DT_NULL; | |
834 while (dyn.d_tag != DT_DEBUG) { | |
835 if (ps_pread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { | |
836 print_debug("can't read debug info from _DYNAMIC\n"); | |
837 return false; | |
838 } | |
839 addr += sizeof(ELF_DYN); | |
840 } | |
841 | |
842 // we have got Dyn entry with DT_DEBUG | |
843 debug_base = dyn.d_un.d_ptr; | |
844 // at debug_base we have struct r_debug. This has first link map in r_map field | |
845 if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, | |
846 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { | |
847 print_debug("can't read first link map address\n"); | |
848 return false; | |
849 } | |
850 | |
851 // read ld_base address from struct r_debug | |
852 // XXX: There is no r_ldbase member on BSD | |
853 /* | |
854 if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, | |
855 sizeof(uintptr_t)) != PS_OK) { | |
856 print_debug("can't read ld base address\n"); | |
857 return false; | |
858 } | |
859 ph->core->ld_base_addr = ld_base_addr; | |
860 */ | |
861 ph->core->ld_base_addr = 0; | |
862 | |
863 print_debug("interpreter base address is 0x%lx\n", ld_base_addr); | |
864 | |
865 // now read segments from interp (i.e ld-elf.so.1) | |
866 if (read_interp_segments(ph) != true) | |
867 return false; | |
868 | |
869 // after adding interpreter (ld.so) mappings sort again | |
870 if (sort_map_array(ph) != true) | |
871 return false; | |
872 | |
873 print_debug("first link map is at 0x%lx\n", first_link_map_addr); | |
874 | |
875 link_map_addr = first_link_map_addr; | |
876 while (link_map_addr != 0) { | |
877 // read library base address of the .so. Note that even though <sys/link.h> calls | |
878 // link_map->l_addr as "base address", this is * not * really base virtual | |
879 // address of the shared object. This is actually the difference b/w the virtual | |
880 // address mentioned in shared object and the actual virtual base where runtime | |
881 // linker loaded it. We use "base diff" in read_lib_segments call below. | |
882 | |
883 if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET, | |
884 &lib_base_diff, sizeof(uintptr_t)) != PS_OK) { | |
885 print_debug("can't read shared object base address diff\n"); | |
886 return false; | |
887 } | |
888 | |
889 // read address of the name | |
890 if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET, | |
891 &lib_name_addr, sizeof(uintptr_t)) != PS_OK) { | |
892 print_debug("can't read address of shared object name\n"); | |
893 return false; | |
894 } | |
895 | |
896 // read name of the shared object | |
897 if (read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) { | |
898 print_debug("can't read shared object name\n"); | |
899 return false; | |
900 } | |
901 | |
902 if (lib_name[0] != '\0') { | |
903 // ignore empty lib names | |
904 lib_fd = pathmap_open(lib_name); | |
905 | |
906 if (lib_fd < 0) { | |
907 print_debug("can't open shared object %s\n", lib_name); | |
908 // continue with other libraries... | |
909 } else { | |
910 if (read_elf_header(lib_fd, &elf_ehdr)) { | |
911 lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr); | |
912 print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n", | |
913 lib_name, lib_base, lib_base_diff); | |
914 // while adding library mappings we need to use "base difference". | |
915 if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) { | |
916 print_debug("can't read shared object's segments\n"); | |
917 close(lib_fd); | |
918 return false; | |
919 } | |
920 add_lib_info_fd(ph, lib_name, lib_fd, lib_base); | |
921 // Map info is added for the library (lib_name) so | |
922 // we need to re-sort it before calling the p_pdread. | |
923 if (sort_map_array(ph) != true) | |
924 return false; | |
925 } else { | |
926 print_debug("can't read ELF header for shared object %s\n", lib_name); | |
927 close(lib_fd); | |
928 // continue with other libraries... | |
929 } | |
930 } | |
931 } | |
932 | |
933 // read next link_map address | |
934 if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, | |
935 &link_map_addr, sizeof(uintptr_t)) != PS_OK) { | |
936 print_debug("can't read next link in link_map\n"); | |
937 return false; | |
938 } | |
939 } | |
940 | |
941 return true; | |
942 } | |
943 | |
944 // the one and only one exposed stuff from this file | |
945 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { | |
946 ELF_EHDR core_ehdr; | |
947 ELF_EHDR exec_ehdr; | |
948 | |
949 struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); | |
950 if (ph == NULL) { | |
951 print_debug("can't allocate ps_prochandle\n"); | |
952 return NULL; | |
953 } | |
954 | |
955 if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { | |
956 free(ph); | |
957 print_debug("can't allocate ps_prochandle\n"); | |
958 return NULL; | |
959 } | |
960 | |
961 // initialize ph | |
962 ph->ops = &core_ops; | |
963 ph->core->core_fd = -1; | |
964 ph->core->exec_fd = -1; | |
965 ph->core->interp_fd = -1; | |
966 | |
967 // open the core file | |
968 if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { | |
969 print_debug("can't open core file\n"); | |
970 goto err; | |
971 } | |
972 | |
973 // read core file ELF header | |
974 if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { | |
975 print_debug("core file is not a valid ELF ET_CORE file\n"); | |
976 goto err; | |
977 } | |
978 | |
979 if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { | |
980 print_debug("can't open executable file\n"); | |
981 goto err; | |
982 } | |
983 | |
984 if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { | |
985 print_debug("executable file is not a valid ELF ET_EXEC file\n"); | |
986 goto err; | |
987 } | |
988 | |
989 // process core file segments | |
990 if (read_core_segments(ph, &core_ehdr) != true) | |
991 goto err; | |
992 | |
993 // process exec file segments | |
994 if (read_exec_segments(ph, &exec_ehdr) != true) | |
995 goto err; | |
996 | |
997 // exec file is also treated like a shared object for symbol search | |
998 if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, | |
999 (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) | |
1000 goto err; | |
1001 | |
1002 // allocate and sort maps into map_array, we need to do this | |
1003 // here because read_shared_lib_info needs to read from debuggee | |
1004 // address space | |
1005 if (sort_map_array(ph) != true) | |
1006 goto err; | |
1007 | |
1008 if (read_shared_lib_info(ph) != true) | |
1009 goto err; | |
1010 | |
1011 // sort again because we have added more mappings from shared objects | |
1012 if (sort_map_array(ph) != true) | |
1013 goto err; | |
1014 | |
1015 if (init_classsharing_workaround(ph) != true) | |
1016 goto err; | |
1017 | |
1018 return ph; | |
1019 | |
1020 err: | |
1021 Prelease(ph); | |
1022 return NULL; | |
1023 } |