Mercurial > hg > truffle
annotate agent/src/os/bsd/ps_core.c @ 14390:0f03ff49c720
8016491: PPC64 (part 2): Clean up PPC defines.
Summary: Distinguish PPC, PPC64 and PPC32. PPC should guard code needed on any PPC system. PPC32 and PPC64 should guard code needed in a port for the ppc64 or ppc32 instruction set.
Reviewed-by: kvn
author | goetz |
---|---|
date | Wed, 19 Jun 2013 12:29:30 +0200 |
parents | f6a055fcf47d |
children | 5705c7ee6dd7 |
rev | line source |
---|---|
3960 | 1 /* |
8750 | 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
3960 | 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 "libproc_impl.h" | |
8750 | 32 |
33 #ifdef __APPLE__ | |
34 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h" | |
35 #endif | |
3960 | 36 |
37 // This file has the libproc implementation to read core files. | |
38 // For live processes, refer to ps_proc.c. Portions of this is adapted | |
39 // /modelled after Solaris libproc.so (in particular Pcore.c) | |
40 | |
41 //---------------------------------------------------------------------- | |
42 // ps_prochandle cleanup helper functions | |
43 | |
44 // close all file descriptors | |
8750 | 45 static void close_files(struct ps_prochandle* ph) { |
46 lib_info* lib = NULL; | |
47 // close core file descriptor | |
48 if (ph->core->core_fd >= 0) | |
49 close(ph->core->core_fd); | |
3960 | 50 |
8750 | 51 // close exec file descriptor |
52 if (ph->core->exec_fd >= 0) | |
53 close(ph->core->exec_fd); | |
3960 | 54 |
8750 | 55 // close interp file descriptor |
56 if (ph->core->interp_fd >= 0) | |
57 close(ph->core->interp_fd); | |
3960 | 58 |
8750 | 59 // close class share archive file |
60 if (ph->core->classes_jsa_fd >= 0) | |
61 close(ph->core->classes_jsa_fd); | |
3960 | 62 |
8750 | 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) { | |
68 close(fd); | |
69 } | |
70 lib = lib->next; | |
71 } | |
3960 | 72 } |
73 | |
74 // clean all map_info stuff | |
75 static void destroy_map_info(struct ps_prochandle* ph) { | |
76 map_info* map = ph->core->maps; | |
77 while (map) { | |
8750 | 78 map_info* next = map->next; |
79 free(map); | |
80 map = next; | |
3960 | 81 } |
82 | |
83 if (ph->core->map_array) { | |
8750 | 84 free(ph->core->map_array); |
3960 | 85 } |
86 | |
87 // Part of the class sharing workaround | |
88 map = ph->core->class_share_maps; | |
89 while (map) { | |
8750 | 90 map_info* next = map->next; |
91 free(map); | |
92 map = next; | |
3960 | 93 } |
94 } | |
95 | |
96 // ps_prochandle operations | |
97 static void core_release(struct ps_prochandle* ph) { | |
8750 | 98 if (ph->core) { |
99 close_files(ph); | |
100 destroy_map_info(ph); | |
101 free(ph->core); | |
102 } | |
3960 | 103 } |
104 | |
105 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { | |
8750 | 106 map_info* map; |
107 if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { | |
108 print_debug("can't allocate memory for map_info\n"); | |
109 return NULL; | |
110 } | |
3960 | 111 |
8750 | 112 // initialize map |
113 map->fd = fd; | |
114 map->offset = offset; | |
115 map->vaddr = vaddr; | |
116 map->memsz = memsz; | |
117 return map; | |
3960 | 118 } |
119 | |
120 // add map info with given fd, offset, vaddr and memsz | |
121 static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, | |
122 uintptr_t vaddr, size_t memsz) { | |
8750 | 123 map_info* map; |
124 if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { | |
125 return NULL; | |
126 } | |
3960 | 127 |
8750 | 128 // add this to map list |
129 map->next = ph->core->maps; | |
130 ph->core->maps = map; | |
131 ph->core->num_maps++; | |
3960 | 132 |
8750 | 133 return map; |
3960 | 134 } |
135 | |
136 // Part of the class sharing workaround | |
137 static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset, | |
138 uintptr_t vaddr, size_t memsz) { | |
8750 | 139 map_info* map; |
140 if ((map = allocate_init_map(ph->core->classes_jsa_fd, | |
141 offset, vaddr, memsz)) == NULL) { | |
142 return NULL; | |
143 } | |
3960 | 144 |
8750 | 145 map->next = ph->core->class_share_maps; |
146 ph->core->class_share_maps = map; | |
147 return map; | |
3960 | 148 } |
149 | |
150 // Return the map_info for the given virtual address. We keep a sorted | |
151 // array of pointers in ph->map_array, so we can binary search. | |
152 static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) | |
153 { | |
8750 | 154 int mid, lo = 0, hi = ph->core->num_maps - 1; |
155 map_info *mp; | |
3960 | 156 |
8750 | 157 while (hi - lo > 1) { |
158 mid = (lo + hi) / 2; | |
159 if (addr >= ph->core->map_array[mid]->vaddr) { | |
160 lo = mid; | |
161 } else { | |
162 hi = mid; | |
163 } | |
164 } | |
3960 | 165 |
8750 | 166 if (addr < ph->core->map_array[hi]->vaddr) { |
167 mp = ph->core->map_array[lo]; | |
168 } else { | |
169 mp = ph->core->map_array[hi]; | |
170 } | |
3960 | 171 |
8750 | 172 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { |
173 return (mp); | |
174 } | |
3960 | 175 |
176 | |
8750 | 177 // Part of the class sharing workaround |
178 // Unfortunately, we have no way of detecting -Xshare state. | |
179 // Check out the share maps atlast, if we don't find anywhere. | |
180 // This is done this way so to avoid reading share pages | |
181 // ahead of other normal maps. For eg. with -Xshare:off we don't | |
182 // want to prefer class sharing data to data from core. | |
183 mp = ph->core->class_share_maps; | |
184 if (mp) { | |
185 print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr); | |
186 } | |
187 while (mp) { | |
188 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { | |
189 print_debug("located map_info at 0x%lx from class share maps\n", addr); | |
190 return (mp); | |
191 } | |
192 mp = mp->next; | |
193 } | |
3960 | 194 |
8750 | 195 print_debug("can't locate map_info at 0x%lx\n", addr); |
196 return (NULL); | |
3960 | 197 } |
198 | |
199 //--------------------------------------------------------------- | |
200 // Part of the class sharing workaround: | |
201 // | |
10229 | 202 // With class sharing, pages are mapped from classes.jsa file. |
3960 | 203 // The read-only class sharing pages are mapped as MAP_SHARED, |
204 // PROT_READ pages. These pages are not dumped into core dump. | |
10229 | 205 // With this workaround, these pages are read from classes.jsa. |
3960 | 206 |
207 // FIXME: !HACK ALERT! | |
208 // The format of sharing achive file header is needed to read shared heap | |
209 // file mappings. For now, I am hard coding portion of FileMapHeader here. | |
210 // Refer to filemap.hpp. | |
211 | |
212 // FileMapHeader describes the shared space data in the file to be | |
213 // mapped. This structure gets written to a file. It is not a class, | |
214 // so that the compilers don't add any compiler-private data to it. | |
215 | |
216 #define NUM_SHARED_MAPS 4 | |
217 | |
218 // Refer to FileMapInfo::_current_version in filemap.hpp | |
219 #define CURRENT_ARCHIVE_VERSION 1 | |
220 | |
221 struct FileMapHeader { | |
222 int _magic; // identify file type. | |
223 int _version; // (from enum, above.) | |
224 size_t _alignment; // how shared archive should be aligned | |
225 | |
226 struct space_info { | |
227 int _file_offset; // sizeof(this) rounded to vm page size | |
228 char* _base; // copy-on-write base address | |
229 size_t _capacity; // for validity checking | |
230 size_t _used; // for setting space top on read | |
231 | |
232 // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with | |
233 // the C type matching the C++ bool type on any given platform. For | |
234 // Hotspot on BSD we assume the corresponding C type is char but | |
235 // licensees on BSD versions may need to adjust the type of these fields. | |
236 char _read_only; // read only space? | |
237 char _allow_exec; // executable code in space? | |
238 | |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3960
diff
changeset
|
239 } _space[NUM_SHARED_MAPS]; |
3960 | 240 |
241 // Ignore the rest of the FileMapHeader. We don't need those fields here. | |
242 }; | |
243 | |
244 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) { | |
8750 | 245 jboolean i; |
246 if (ps_pread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { | |
247 *pvalue = i; | |
248 return true; | |
249 } else { | |
250 return false; | |
251 } | |
3960 | 252 } |
253 | |
254 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { | |
8750 | 255 uintptr_t uip; |
256 if (ps_pread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) { | |
257 *pvalue = uip; | |
258 return true; | |
259 } else { | |
260 return false; | |
261 } | |
3960 | 262 } |
263 | |
264 // used to read strings from debuggee | |
265 static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { | |
8750 | 266 size_t i = 0; |
267 char c = ' '; | |
3960 | 268 |
8750 | 269 while (c != '\0') { |
270 if (ps_pread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) { | |
271 return false; | |
272 } | |
273 if (i < size - 1) { | |
274 buf[i] = c; | |
275 } else { | |
276 // smaller buffer | |
277 return false; | |
278 } | |
279 i++; addr++; | |
280 } | |
281 buf[i] = '\0'; | |
282 return true; | |
3960 | 283 } |
284 | |
8750 | 285 #ifdef __APPLE__ |
286 #define USE_SHARED_SPACES_SYM "_UseSharedSpaces" | |
287 // mangled name of Arguments::SharedArchivePath | |
288 #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" | |
289 #else | |
3960 | 290 #define USE_SHARED_SPACES_SYM "UseSharedSpaces" |
291 // mangled name of Arguments::SharedArchivePath | |
8750 | 292 #define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE" |
293 #endif // __APPLE_ | |
3960 | 294 |
295 static bool init_classsharing_workaround(struct ps_prochandle* ph) { | |
8750 | 296 int m; |
297 size_t n; | |
298 lib_info* lib = ph->libs; | |
299 while (lib != NULL) { | |
300 // we are iterating over shared objects from the core dump. look for | |
10229 | 301 // libjvm.so. |
8750 | 302 const char *jvm_name = 0; |
303 #ifdef __APPLE__ | |
10229 | 304 if ((jvm_name = strstr(lib->name, "/libjvm.dylib")) != 0) |
8750 | 305 #else |
10229 | 306 if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0) |
8750 | 307 #endif // __APPLE__ |
308 { | |
309 char classes_jsa[PATH_MAX]; | |
310 struct FileMapHeader header; | |
311 int fd = -1; | |
312 uintptr_t base = 0, useSharedSpacesAddr = 0; | |
313 uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; | |
314 jboolean useSharedSpaces = 0; | |
3960 | 315 |
8750 | 316 memset(classes_jsa, 0, sizeof(classes_jsa)); |
317 jvm_name = lib->name; | |
318 useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); | |
319 if (useSharedSpacesAddr == 0) { | |
320 print_debug("can't lookup 'UseSharedSpaces' flag\n"); | |
321 return false; | |
322 } | |
3960 | 323 |
8750 | 324 // Hotspot vm types are not exported to build this library. So |
325 // using equivalent type jboolean to read the value of | |
326 // UseSharedSpaces which is same as hotspot type "bool". | |
327 if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { | |
328 print_debug("can't read the value of 'UseSharedSpaces' flag\n"); | |
329 return false; | |
330 } | |
3960 | 331 |
8750 | 332 if ((int)useSharedSpaces == 0) { |
333 print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); | |
334 return true; | |
335 } | |
3960 | 336 |
8750 | 337 sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); |
338 if (sharedArchivePathAddrAddr == 0) { | |
339 print_debug("can't lookup shared archive path symbol\n"); | |
340 return false; | |
341 } | |
3960 | 342 |
8750 | 343 if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { |
344 print_debug("can't read shared archive path pointer\n"); | |
345 return false; | |
346 } | |
3960 | 347 |
8750 | 348 if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { |
349 print_debug("can't read shared archive path value\n"); | |
350 return false; | |
351 } | |
3960 | 352 |
8750 | 353 print_debug("looking for %s\n", classes_jsa); |
354 // open the class sharing archive file | |
355 fd = pathmap_open(classes_jsa); | |
356 if (fd < 0) { | |
357 print_debug("can't open %s!\n", classes_jsa); | |
358 ph->core->classes_jsa_fd = -1; | |
359 return false; | |
360 } else { | |
361 print_debug("opened %s\n", classes_jsa); | |
362 } | |
3960 | 363 |
8750 | 364 // read FileMapHeader from the file |
365 memset(&header, 0, sizeof(struct FileMapHeader)); | |
366 if ((n = read(fd, &header, sizeof(struct FileMapHeader))) | |
367 != sizeof(struct FileMapHeader)) { | |
368 print_debug("can't read shared archive file map header from %s\n", classes_jsa); | |
369 close(fd); | |
370 return false; | |
371 } | |
3960 | 372 |
8750 | 373 // check file magic |
374 if (header._magic != 0xf00baba2) { | |
375 print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n", | |
376 classes_jsa, header._magic); | |
377 close(fd); | |
378 return false; | |
379 } | |
380 | |
381 // check version | |
382 if (header._version != CURRENT_ARCHIVE_VERSION) { | |
383 print_debug("%s has wrong shared archive file version %d, expecting %d\n", | |
384 classes_jsa, header._version, CURRENT_ARCHIVE_VERSION); | |
385 close(fd); | |
386 return false; | |
387 } | |
3960 | 388 |
8750 | 389 ph->core->classes_jsa_fd = fd; |
10229 | 390 // add read-only maps from classes.jsa to the list of maps |
8750 | 391 for (m = 0; m < NUM_SHARED_MAPS; m++) { |
392 if (header._space[m]._read_only) { | |
393 base = (uintptr_t) header._space[m]._base; | |
394 // no need to worry about the fractional pages at-the-end. | |
395 // possible fractional pages are handled by core_read_data. | |
396 add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, | |
397 base, (size_t) header._space[m]._used); | |
398 print_debug("added a share archive map at 0x%lx\n", base); | |
399 } | |
3960 | 400 } |
8750 | 401 return true; |
402 } | |
403 lib = lib->next; | |
404 } | |
405 return true; | |
3960 | 406 } |
407 | |
408 //--------------------------------------------------------------------------- | |
409 // functions to handle map_info | |
410 | |
411 // Order mappings based on virtual address. We use this function as the | |
412 // callback for sorting the array of map_info pointers. | |
413 static int core_cmp_mapping(const void *lhsp, const void *rhsp) | |
414 { | |
8750 | 415 const map_info *lhs = *((const map_info **)lhsp); |
416 const map_info *rhs = *((const map_info **)rhsp); | |
3960 | 417 |
8750 | 418 if (lhs->vaddr == rhs->vaddr) { |
419 return (0); | |
420 } | |
3960 | 421 |
8750 | 422 return (lhs->vaddr < rhs->vaddr ? -1 : 1); |
3960 | 423 } |
424 | |
425 // we sort map_info by starting virtual address so that we can do | |
426 // binary search to read from an address. | |
427 static bool sort_map_array(struct ps_prochandle* ph) { | |
8750 | 428 size_t num_maps = ph->core->num_maps; |
429 map_info* map = ph->core->maps; | |
430 int i = 0; | |
3960 | 431 |
8750 | 432 // allocate map_array |
433 map_info** array; | |
434 if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { | |
435 print_debug("can't allocate memory for map array\n"); | |
436 return false; | |
437 } | |
3960 | 438 |
8750 | 439 // add maps to array |
440 while (map) { | |
441 array[i] = map; | |
442 i++; | |
443 map = map->next; | |
444 } | |
3960 | 445 |
8750 | 446 // sort is called twice. If this is second time, clear map array |
447 if (ph->core->map_array) { | |
448 free(ph->core->map_array); | |
449 } | |
450 ph->core->map_array = array; | |
451 // sort the map_info array by base virtual address. | |
452 qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), | |
453 core_cmp_mapping); | |
3960 | 454 |
8750 | 455 // print map |
456 if (is_debug()) { | |
457 int j = 0; | |
458 print_debug("---- sorted virtual address map ----\n"); | |
459 for (j = 0; j < ph->core->num_maps; j++) { | |
460 print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr, | |
461 ph->core->map_array[j]->memsz); | |
462 } | |
463 } | |
3960 | 464 |
8750 | 465 return true; |
3960 | 466 } |
467 | |
468 #ifndef MIN | |
469 #define MIN(x, y) (((x) < (y))? (x): (y)) | |
470 #endif | |
471 | |
472 static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) { | |
473 ssize_t resid = size; | |
474 int page_size=sysconf(_SC_PAGE_SIZE); | |
475 while (resid != 0) { | |
476 map_info *mp = core_lookup(ph, addr); | |
477 uintptr_t mapoff; | |
478 ssize_t len, rem; | |
479 off_t off; | |
480 int fd; | |
481 | |
8750 | 482 if (mp == NULL) { |
3960 | 483 break; /* No mapping for this address */ |
8750 | 484 } |
3960 | 485 |
486 fd = mp->fd; | |
487 mapoff = addr - mp->vaddr; | |
488 len = MIN(resid, mp->memsz - mapoff); | |
489 off = mp->offset + mapoff; | |
490 | |
8750 | 491 if ((len = pread(fd, buf, len, off)) <= 0) { |
3960 | 492 break; |
8750 | 493 } |
3960 | 494 |
495 resid -= len; | |
496 addr += len; | |
497 buf = (char *)buf + len; | |
498 | |
499 // mappings always start at page boundary. But, may end in fractional | |
500 // page. fill zeros for possible fractional page at the end of a mapping. | |
501 rem = mp->memsz % page_size; | |
502 if (rem > 0) { | |
503 rem = page_size - rem; | |
504 len = MIN(resid, rem); | |
505 resid -= len; | |
506 addr += len; | |
507 // we are not assuming 'buf' to be zero initialized. | |
508 memset(buf, 0, len); | |
509 buf += len; | |
510 } | |
511 } | |
512 | |
513 if (resid) { | |
514 print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n", | |
515 size, addr, resid); | |
516 return false; | |
517 } else { | |
518 return true; | |
519 } | |
520 } | |
521 | |
522 // null implementation for write | |
523 static bool core_write_data(struct ps_prochandle* ph, | |
524 uintptr_t addr, const char *buf , size_t size) { | |
525 return false; | |
526 } | |
527 | |
528 static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id, | |
529 struct reg* regs) { | |
8750 | 530 // for core we have cached the lwp regs after segment parsed |
531 sa_thread_info* thr = ph->threads; | |
3960 | 532 while (thr) { |
533 if (thr->lwp_id == lwp_id) { | |
534 memcpy(regs, &thr->regs, sizeof(struct reg)); | |
535 return true; | |
536 } | |
537 thr = thr->next; | |
538 } | |
539 return false; | |
540 } | |
541 | |
8750 | 542 static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t id, void *info) { |
3960 | 543 print_debug("core_get_lwp_info not implemented\n"); |
544 return false; | |
545 } | |
546 | |
547 static ps_prochandle_ops core_ops = { | |
548 .release= core_release, | |
549 .p_pread= core_read_data, | |
550 .p_pwrite= core_write_data, | |
551 .get_lwp_regs= core_get_lwp_regs, | |
552 .get_lwp_info= core_get_lwp_info | |
553 }; | |
554 | |
8750 | 555 // from this point, mainly two blocks divided by def __APPLE__ |
556 // one for Macosx, the other for regular Bsd | |
557 | |
558 #ifdef __APPLE__ | |
559 | |
560 void print_thread(sa_thread_info *threadinfo) { | |
561 print_debug("thread added: %d\n", threadinfo->lwp_id); | |
562 print_debug("registers:\n"); | |
563 print_debug(" r_r15: 0x%" PRIx64 "\n", threadinfo->regs.r_r15); | |
564 print_debug(" r_r14: 0x%" PRIx64 "\n", threadinfo->regs.r_r14); | |
565 print_debug(" r_r13: 0x%" PRIx64 "\n", threadinfo->regs.r_r13); | |
566 print_debug(" r_r12: 0x%" PRIx64 "\n", threadinfo->regs.r_r12); | |
567 print_debug(" r_r11: 0x%" PRIx64 "\n", threadinfo->regs.r_r11); | |
568 print_debug(" r_r10: 0x%" PRIx64 "\n", threadinfo->regs.r_r10); | |
569 print_debug(" r_r9: 0x%" PRIx64 "\n", threadinfo->regs.r_r9); | |
570 print_debug(" r_r8: 0x%" PRIx64 "\n", threadinfo->regs.r_r8); | |
571 print_debug(" r_rdi: 0x%" PRIx64 "\n", threadinfo->regs.r_rdi); | |
572 print_debug(" r_rsi: 0x%" PRIx64 "\n", threadinfo->regs.r_rsi); | |
573 print_debug(" r_rbp: 0x%" PRIx64 "\n", threadinfo->regs.r_rbp); | |
574 print_debug(" r_rbx: 0x%" PRIx64 "\n", threadinfo->regs.r_rbx); | |
575 print_debug(" r_rdx: 0x%" PRIx64 "\n", threadinfo->regs.r_rdx); | |
576 print_debug(" r_rcx: 0x%" PRIx64 "\n", threadinfo->regs.r_rcx); | |
577 print_debug(" r_rax: 0x%" PRIx64 "\n", threadinfo->regs.r_rax); | |
578 print_debug(" r_fs: 0x%" PRIx32 "\n", threadinfo->regs.r_fs); | |
579 print_debug(" r_gs: 0x%" PRIx32 "\n", threadinfo->regs.r_gs); | |
580 print_debug(" r_rip 0x%" PRIx64 "\n", threadinfo->regs.r_rip); | |
581 print_debug(" r_cs: 0x%" PRIx64 "\n", threadinfo->regs.r_cs); | |
582 print_debug(" r_rsp: 0x%" PRIx64 "\n", threadinfo->regs.r_rsp); | |
583 print_debug(" r_rflags: 0x%" PRIx64 "\n", threadinfo->regs.r_rflags); | |
584 } | |
585 | |
586 // read all segments64 commands from core file | |
587 // read all thread commands from core file | |
588 static bool read_core_segments(struct ps_prochandle* ph) { | |
589 int i = 0; | |
590 int num_threads = 0; | |
591 int fd = ph->core->core_fd; | |
592 off_t offset = 0; | |
593 mach_header_64 fhead; | |
594 load_command lcmd; | |
595 segment_command_64 segcmd; | |
596 // thread_command thrcmd; | |
597 | |
598 lseek(fd, offset, SEEK_SET); | |
599 if(read(fd, (void *)&fhead, sizeof(mach_header_64)) != sizeof(mach_header_64)) { | |
600 goto err; | |
601 } | |
602 print_debug("total commands: %d\n", fhead.ncmds); | |
603 offset += sizeof(mach_header_64); | |
604 for (i = 0; i < fhead.ncmds; i++) { | |
605 lseek(fd, offset, SEEK_SET); | |
606 if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) { | |
607 goto err; | |
608 } | |
609 offset += lcmd.cmdsize; // next command position | |
610 if (lcmd.cmd == LC_SEGMENT_64) { | |
611 lseek(fd, -sizeof(load_command), SEEK_CUR); | |
612 if (read(fd, (void *)&segcmd, sizeof(segment_command_64)) != sizeof(segment_command_64)) { | |
613 print_debug("failed to read LC_SEGMENT_64 i = %d!\n", i); | |
614 goto err; | |
615 } | |
616 if (add_map_info(ph, fd, segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize) == NULL) { | |
617 print_debug("Failed to add map_info at i = %d\n", i); | |
618 goto err; | |
619 } | |
620 print_debug("segment added: %" PRIu64 " 0x%" PRIx64 " %d\n", | |
621 segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize); | |
622 } else if (lcmd.cmd == LC_THREAD || lcmd.cmd == LC_UNIXTHREAD) { | |
623 typedef struct thread_fc { | |
624 uint32_t flavor; | |
625 uint32_t count; | |
626 } thread_fc; | |
627 thread_fc fc; | |
628 uint32_t size = sizeof(load_command); | |
629 while (size < lcmd.cmdsize) { | |
630 if (read(fd, (void *)&fc, sizeof(thread_fc)) != sizeof(thread_fc)) { | |
631 printf("Reading flavor, count failed.\n"); | |
632 goto err; | |
633 } | |
634 size += sizeof(thread_fc); | |
635 if (fc.flavor == x86_THREAD_STATE) { | |
636 x86_thread_state_t thrstate; | |
637 if (read(fd, (void *)&thrstate, sizeof(x86_thread_state_t)) != sizeof(x86_thread_state_t)) { | |
638 printf("Reading flavor, count failed.\n"); | |
639 goto err; | |
640 } | |
641 size += sizeof(x86_thread_state_t); | |
642 // create thread info list, update lwp_id later | |
643 sa_thread_info* newthr = add_thread_info(ph, (pthread_t) -1, (lwpid_t) num_threads++); | |
644 if (newthr == NULL) { | |
645 printf("create thread_info failed\n"); | |
646 goto err; | |
647 } | |
648 | |
649 // note __DARWIN_UNIX03 depengs on other definitions | |
650 #if __DARWIN_UNIX03 | |
651 #define get_register_v(regst, regname) \ | |
652 regst.uts.ts64.__##regname | |
653 #else | |
654 #define get_register_v(regst, regname) \ | |
655 regst.uts.ts64.##regname | |
656 #endif // __DARWIN_UNIX03 | |
657 newthr->regs.r_rax = get_register_v(thrstate, rax); | |
658 newthr->regs.r_rbx = get_register_v(thrstate, rbx); | |
659 newthr->regs.r_rcx = get_register_v(thrstate, rcx); | |
660 newthr->regs.r_rdx = get_register_v(thrstate, rdx); | |
661 newthr->regs.r_rdi = get_register_v(thrstate, rdi); | |
662 newthr->regs.r_rsi = get_register_v(thrstate, rsi); | |
663 newthr->regs.r_rbp = get_register_v(thrstate, rbp); | |
664 newthr->regs.r_rsp = get_register_v(thrstate, rsp); | |
665 newthr->regs.r_r8 = get_register_v(thrstate, r8); | |
666 newthr->regs.r_r9 = get_register_v(thrstate, r9); | |
667 newthr->regs.r_r10 = get_register_v(thrstate, r10); | |
668 newthr->regs.r_r11 = get_register_v(thrstate, r11); | |
669 newthr->regs.r_r12 = get_register_v(thrstate, r12); | |
670 newthr->regs.r_r13 = get_register_v(thrstate, r13); | |
671 newthr->regs.r_r14 = get_register_v(thrstate, r14); | |
672 newthr->regs.r_r15 = get_register_v(thrstate, r15); | |
673 newthr->regs.r_rip = get_register_v(thrstate, rip); | |
674 newthr->regs.r_rflags = get_register_v(thrstate, rflags); | |
675 newthr->regs.r_cs = get_register_v(thrstate, cs); | |
676 newthr->regs.r_fs = get_register_v(thrstate, fs); | |
677 newthr->regs.r_gs = get_register_v(thrstate, gs); | |
678 print_thread(newthr); | |
679 } else if (fc.flavor == x86_FLOAT_STATE) { | |
680 x86_float_state_t flstate; | |
681 if (read(fd, (void *)&flstate, sizeof(x86_float_state_t)) != sizeof(x86_float_state_t)) { | |
682 print_debug("Reading flavor, count failed.\n"); | |
683 goto err; | |
684 } | |
685 size += sizeof(x86_float_state_t); | |
686 } else if (fc.flavor == x86_EXCEPTION_STATE) { | |
687 x86_exception_state_t excpstate; | |
688 if (read(fd, (void *)&excpstate, sizeof(x86_exception_state_t)) != sizeof(x86_exception_state_t)) { | |
689 printf("Reading flavor, count failed.\n"); | |
690 goto err; | |
691 } | |
692 size += sizeof(x86_exception_state_t); | |
693 } | |
694 } | |
695 } | |
696 } | |
697 return true; | |
698 err: | |
699 return false; | |
700 } | |
701 | |
702 /**local function **/ | |
703 bool exists(const char *fname) | |
704 { | |
705 int fd; | |
706 if ((fd = open(fname, O_RDONLY)) > 0) { | |
707 close(fd); | |
708 return true; | |
709 } | |
710 return false; | |
711 } | |
712 | |
713 // we check: 1. lib | |
714 // 2. lib/server | |
715 // 3. jre/lib | |
716 // 4. jre/lib/server | |
717 // from: 1. exe path | |
718 // 2. JAVA_HOME | |
719 // 3. DYLD_LIBRARY_PATH | |
720 static bool get_real_path(struct ps_prochandle* ph, char *rpath) { | |
721 /** check if they exist in JAVA ***/ | |
722 char* execname = ph->core->exec_path; | |
723 char filepath[4096]; | |
724 char* filename = strrchr(rpath, '/'); // like /libjvm.dylib | |
725 if (filename == NULL) { | |
726 return false; | |
727 } | |
728 | |
729 char* posbin = strstr(execname, "/bin/java"); | |
730 if (posbin != NULL) { | |
731 memcpy(filepath, execname, posbin - execname); // not include trailing '/' | |
732 filepath[posbin - execname] = '\0'; | |
733 } else { | |
734 char* java_home = getenv("JAVA_HOME"); | |
735 if (java_home != NULL) { | |
736 strcpy(filepath, java_home); | |
737 } else { | |
738 char* dyldpath = getenv("DYLD_LIBRARY_PATH"); | |
739 char* dypath = strtok(dyldpath, ":"); | |
740 while (dypath != NULL) { | |
741 strcpy(filepath, dypath); | |
742 strcat(filepath, filename); | |
743 if (exists(filepath)) { | |
744 strcpy(rpath, filepath); | |
745 return true; | |
746 } | |
747 dypath = strtok(dyldpath, ":"); | |
748 } | |
749 // not found | |
750 return false; | |
751 } | |
752 } | |
753 // for exec and java_home, jdkpath now is filepath | |
754 size_t filepath_base_size = strlen(filepath); | |
755 | |
756 // first try /lib/ and /lib/server | |
757 strcat(filepath, "/lib"); | |
758 strcat(filepath, filename); | |
759 if (exists(filepath)) { | |
760 strcpy(rpath, filepath); | |
761 return true; | |
762 } | |
763 char* pos = strstr(filepath, filename); // like /libjvm.dylib | |
764 *pos = '\0'; | |
765 strcat(filepath, "/server"); | |
766 strcat(filepath, filename); | |
767 if (exists(filepath)) { | |
768 strcpy(rpath, filepath); | |
769 return true; | |
770 } | |
771 | |
772 // then try /jre/lib/ and /jre/lib/server | |
773 filepath[filepath_base_size] = '\0'; | |
774 strcat(filepath, "/jre/lib"); | |
775 strcat(filepath, filename); | |
776 if (exists(filepath)) { | |
777 strcpy(rpath, filepath); | |
778 return true; | |
779 } | |
780 pos = strstr(filepath, filename); | |
781 *pos = '\0'; | |
782 strcat(filepath, "/server"); | |
783 strcat(filepath, filename); | |
784 if (exists(filepath)) { | |
785 strcpy(rpath, filepath); | |
786 return true; | |
787 } | |
788 | |
789 return false; | |
790 } | |
791 | |
792 static bool read_shared_lib_info(struct ps_prochandle* ph) { | |
793 static int pagesize = 0; | |
794 int fd = ph->core->core_fd; | |
795 int i = 0, j; | |
796 uint32_t v; | |
797 mach_header_64 header; // used to check if a file header in segment | |
798 load_command lcmd; | |
799 dylib_command dylibcmd; | |
800 | |
801 char name[BUF_SIZE]; // use to store name | |
802 | |
803 if (pagesize == 0) { | |
804 pagesize = getpagesize(); | |
805 print_debug("page size is %d\n", pagesize); | |
806 } | |
807 for (j = 0; j < ph->core->num_maps; j++) { | |
808 map_info *iter = ph->core->map_array[j]; // head | |
809 off_t fpos = iter->offset; | |
810 if (iter->fd != fd) { | |
811 // only search core file! | |
812 continue; | |
813 } | |
814 print_debug("map_info %d: vmaddr = 0x%016" PRIx64 " fileoff = %" PRIu64 " vmsize = %" PRIu64 "\n", | |
815 j, iter->vaddr, iter->offset, iter->memsz); | |
816 lseek(fd, fpos, SEEK_SET); | |
817 // we assume .dylib loaded at segment address --- which is true for JVM libraries | |
818 // multiple files may be loaded in one segment. | |
819 // if first word is not a magic word, means this segment does not contain lib file. | |
820 if (read(fd, (void *)&v, sizeof(uint32_t)) == sizeof(uint32_t)) { | |
821 if (v != MH_MAGIC_64) { | |
822 continue; | |
823 } | |
824 } else { | |
825 // may be encountered last map, which is not readable | |
826 continue; | |
827 } | |
828 while (ltell(fd) - iter->offset < iter->memsz) { | |
829 lseek(fd, fpos, SEEK_SET); | |
830 if (read(fd, (void *)&v, sizeof(uint32_t)) != sizeof(uint32_t)) { | |
831 break; | |
832 } | |
833 if (v != MH_MAGIC_64) { | |
834 fpos = (ltell(fd) + pagesize -1)/pagesize * pagesize; | |
835 continue; | |
836 } | |
837 lseek(fd, -sizeof(uint32_t), SEEK_CUR); | |
838 // this is the file begining to core file. | |
839 if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) { | |
840 goto err; | |
841 } | |
842 fpos = ltell(fd); | |
843 | |
844 // found a mach-o file in this segment | |
845 for (i = 0; i < header.ncmds; i++) { | |
846 // read commands in this "file" | |
847 // LC_ID_DYLIB is the file itself for a .dylib | |
848 lseek(fd, fpos, SEEK_SET); | |
849 if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) { | |
850 return false; // error | |
851 } | |
852 fpos += lcmd.cmdsize; // next command position | |
853 // make sure still within seg size. | |
854 if (fpos - lcmd.cmdsize - iter->offset > iter->memsz) { | |
855 print_debug("Warning: out of segement limit: %ld \n", fpos - lcmd.cmdsize - iter->offset); | |
856 break; // no need to iterate all commands | |
857 } | |
858 if (lcmd.cmd == LC_ID_DYLIB) { | |
859 lseek(fd, -sizeof(load_command), SEEK_CUR); | |
860 if (read(fd, (void *)&dylibcmd, sizeof(dylib_command)) != sizeof(dylib_command)) { | |
861 return false; | |
862 } | |
863 /**** name stored at dylib_command.dylib.name.offset, is a C string */ | |
864 lseek(fd, dylibcmd.dylib.name.offset - sizeof(dylib_command), SEEK_CUR); | |
865 int j = 0; | |
866 while (j < BUF_SIZE) { | |
867 read(fd, (void *)(name + j), sizeof(char)); | |
868 if (name[j] == '\0') break; | |
869 j++; | |
870 } | |
871 print_debug("%s\n", name); | |
872 // changed name from @rpath/xxxx.dylib to real path | |
873 if (strrchr(name, '@')) { | |
874 get_real_path(ph, name); | |
875 print_debug("get_real_path returned: %s\n", name); | |
876 } | |
877 add_lib_info(ph, name, iter->vaddr); | |
878 break; | |
879 } | |
880 } | |
881 // done with the file, advanced to next page to search more files | |
882 fpos = (ltell(fd) + pagesize - 1) / pagesize * pagesize; | |
883 } | |
884 } | |
885 return true; | |
886 err: | |
887 return false; | |
888 } | |
889 | |
890 bool read_macho64_header(int fd, mach_header_64* core_header) { | |
891 bool is_macho = false; | |
892 if (fd < 0) return false; | |
893 off_t pos = ltell(fd); | |
894 lseek(fd, 0, SEEK_SET); | |
895 if (read(fd, (void *)core_header, sizeof(mach_header_64)) != sizeof(mach_header_64)) { | |
896 is_macho = false; | |
897 } else { | |
898 is_macho = (core_header->magic == MH_MAGIC_64 || core_header->magic == MH_CIGAM_64); | |
899 } | |
900 lseek(fd, pos, SEEK_SET); | |
901 return is_macho; | |
902 } | |
903 | |
904 // the one and only one exposed stuff from this file | |
905 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { | |
906 mach_header_64 core_header; | |
907 mach_header_64 exec_header; | |
908 | |
909 struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); | |
910 if (ph == NULL) { | |
911 print_debug("cant allocate ps_prochandle\n"); | |
912 return NULL; | |
913 } | |
914 | |
915 if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { | |
916 free(ph); | |
917 print_debug("can't allocate ps_prochandle\n"); | |
918 return NULL; | |
919 } | |
920 | |
921 // initialize ph | |
922 ph->ops = &core_ops; | |
923 ph->core->core_fd = -1; | |
924 ph->core->exec_fd = -1; | |
925 ph->core->interp_fd = -1; | |
926 | |
927 print_debug("exec: %s core: %s", exec_file, core_file); | |
928 | |
929 strncpy(ph->core->exec_path, exec_file, sizeof(ph->core->exec_path)); | |
930 | |
931 // open the core file | |
932 if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { | |
933 print_error("can't open core file\n"); | |
934 goto err; | |
935 } | |
936 | |
937 // read core file header | |
938 if (read_macho64_header(ph->core->core_fd, &core_header) != true || core_header.filetype != MH_CORE) { | |
939 print_debug("core file is not a valid Mach-O file\n"); | |
940 goto err; | |
941 } | |
942 | |
943 if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { | |
944 print_error("can't open executable file\n"); | |
945 goto err; | |
946 } | |
947 | |
948 if (read_macho64_header(ph->core->exec_fd, &exec_header) != true || | |
949 exec_header.filetype != MH_EXECUTE) { | |
950 print_error("executable file is not a valid Mach-O file\n"); | |
951 goto err; | |
952 } | |
953 | |
954 // process core file segments | |
955 if (read_core_segments(ph) != true) { | |
956 print_error("failed to read core segments\n"); | |
957 goto err; | |
958 } | |
959 | |
960 // allocate and sort maps into map_array, we need to do this | |
961 // here because read_shared_lib_info needs to read from debuggee | |
962 // address space | |
963 if (sort_map_array(ph) != true) { | |
964 print_error("failed to sort segment map array\n"); | |
965 goto err; | |
966 } | |
967 | |
968 if (read_shared_lib_info(ph) != true) { | |
969 print_error("failed to read libraries\n"); | |
970 goto err; | |
971 } | |
972 | |
973 // sort again because we have added more mappings from shared objects | |
974 if (sort_map_array(ph) != true) { | |
975 print_error("failed to sort segment map array\n"); | |
976 goto err; | |
977 } | |
978 | |
979 if (init_classsharing_workaround(ph) != true) { | |
980 print_error("failed to workaround classshareing\n"); | |
981 goto err; | |
982 } | |
983 | |
984 print_debug("Leave Pgrab_core\n"); | |
985 return ph; | |
986 | |
987 err: | |
988 Prelease(ph); | |
989 return NULL; | |
990 } | |
991 | |
992 #else // __APPLE__ (none macosx) | |
993 | |
994 // read regs and create thread from core file | |
3960 | 995 static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) { |
996 // we have to read prstatus_t from buf | |
997 // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t"); | |
998 prstatus_t* prstat = (prstatus_t*) buf; | |
8750 | 999 sa_thread_info* newthr; |
3960 | 1000 print_debug("got integer regset for lwp %d\n", prstat->pr_pid); |
1001 // we set pthread_t to -1 for core dump | |
1002 if((newthr = add_thread_info(ph, (pthread_t) -1, prstat->pr_pid)) == NULL) | |
1003 return false; | |
1004 | |
1005 // copy regs | |
1006 memcpy(&newthr->regs, &prstat->pr_reg, sizeof(struct reg)); | |
1007 | |
1008 if (is_debug()) { | |
1009 print_debug("integer regset\n"); | |
1010 #ifdef i386 | |
1011 // print the regset | |
1012 print_debug("\teax = 0x%x\n", newthr->regs.r_eax); | |
1013 print_debug("\tebx = 0x%x\n", newthr->regs.r_ebx); | |
1014 print_debug("\tecx = 0x%x\n", newthr->regs.r_ecx); | |
1015 print_debug("\tedx = 0x%x\n", newthr->regs.r_edx); | |
1016 print_debug("\tesp = 0x%x\n", newthr->regs.r_esp); | |
1017 print_debug("\tebp = 0x%x\n", newthr->regs.r_ebp); | |
1018 print_debug("\tesi = 0x%x\n", newthr->regs.r_esi); | |
1019 print_debug("\tedi = 0x%x\n", newthr->regs.r_edi); | |
1020 print_debug("\teip = 0x%x\n", newthr->regs.r_eip); | |
1021 #endif | |
1022 | |
1023 #if defined(amd64) || defined(x86_64) | |
1024 // print the regset | |
1025 print_debug("\tr15 = 0x%lx\n", newthr->regs.r_r15); | |
1026 print_debug("\tr14 = 0x%lx\n", newthr->regs.r_r14); | |
1027 print_debug("\tr13 = 0x%lx\n", newthr->regs.r_r13); | |
1028 print_debug("\tr12 = 0x%lx\n", newthr->regs.r_r12); | |
1029 print_debug("\trbp = 0x%lx\n", newthr->regs.r_rbp); | |
1030 print_debug("\trbx = 0x%lx\n", newthr->regs.r_rbx); | |
1031 print_debug("\tr11 = 0x%lx\n", newthr->regs.r_r11); | |
1032 print_debug("\tr10 = 0x%lx\n", newthr->regs.r_r10); | |
1033 print_debug("\tr9 = 0x%lx\n", newthr->regs.r_r9); | |
1034 print_debug("\tr8 = 0x%lx\n", newthr->regs.r_r8); | |
1035 print_debug("\trax = 0x%lx\n", newthr->regs.r_rax); | |
1036 print_debug("\trcx = 0x%lx\n", newthr->regs.r_rcx); | |
1037 print_debug("\trdx = 0x%lx\n", newthr->regs.r_rdx); | |
1038 print_debug("\trsi = 0x%lx\n", newthr->regs.r_rsi); | |
1039 print_debug("\trdi = 0x%lx\n", newthr->regs.r_rdi); | |
1040 //print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax); | |
1041 print_debug("\trip = 0x%lx\n", newthr->regs.r_rip); | |
1042 print_debug("\tcs = 0x%lx\n", newthr->regs.r_cs); | |
1043 //print_debug("\teflags = 0x%lx\n", newthr->regs.eflags); | |
1044 print_debug("\trsp = 0x%lx\n", newthr->regs.r_rsp); | |
1045 print_debug("\tss = 0x%lx\n", newthr->regs.r_ss); | |
1046 //print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base); | |
1047 //print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base); | |
1048 //print_debug("\tds = 0x%lx\n", newthr->regs.ds); | |
1049 //print_debug("\tes = 0x%lx\n", newthr->regs.es); | |
1050 //print_debug("\tfs = 0x%lx\n", newthr->regs.fs); | |
1051 //print_debug("\tgs = 0x%lx\n", newthr->regs.gs); | |
1052 #endif | |
1053 } | |
1054 | |
1055 return true; | |
1056 } | |
1057 | |
1058 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y)) | |
1059 | |
1060 // read NT_PRSTATUS entries from core NOTE segment | |
1061 static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) { | |
1062 char* buf = NULL; | |
1063 char* p = NULL; | |
1064 size_t size = note_phdr->p_filesz; | |
1065 | |
1066 // we are interested in just prstatus entries. we will ignore the rest. | |
1067 // Advance the seek pointer to the start of the PT_NOTE data | |
1068 if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) { | |
1069 print_debug("failed to lseek to PT_NOTE data\n"); | |
1070 return false; | |
1071 } | |
1072 | |
1073 // Now process the PT_NOTE structures. Each one is preceded by | |
1074 // an Elf{32/64}_Nhdr structure describing its type and size. | |
1075 if ( (buf = (char*) malloc(size)) == NULL) { | |
1076 print_debug("can't allocate memory for reading core notes\n"); | |
1077 goto err; | |
1078 } | |
1079 | |
1080 // read notes into buffer | |
1081 if (read(ph->core->core_fd, buf, size) != size) { | |
1082 print_debug("failed to read notes, core file must have been truncated\n"); | |
1083 goto err; | |
1084 } | |
1085 | |
1086 p = buf; | |
1087 while (p < buf + size) { | |
1088 ELF_NHDR* notep = (ELF_NHDR*) p; | |
1089 char* descdata = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4); | |
1090 print_debug("Note header with n_type = %d and n_descsz = %u\n", | |
1091 notep->n_type, notep->n_descsz); | |
1092 | |
1093 if (notep->n_type == NT_PRSTATUS) { | |
8750 | 1094 if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) { |
3960 | 1095 return false; |
8750 | 1096 } |
3960 | 1097 } |
1098 p = descdata + ROUNDUP(notep->n_descsz, 4); | |
1099 } | |
1100 | |
1101 free(buf); | |
1102 return true; | |
1103 | |
1104 err: | |
1105 if (buf) free(buf); | |
1106 return false; | |
1107 } | |
1108 | |
1109 // read all segments from core file | |
1110 static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) { | |
1111 int i = 0; | |
1112 ELF_PHDR* phbuf = NULL; | |
1113 ELF_PHDR* core_php = NULL; | |
1114 | |
1115 if ((phbuf = read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL) | |
1116 return false; | |
1117 | |
1118 /* | |
1119 * Now iterate through the program headers in the core file. | |
1120 * We're interested in two types of Phdrs: PT_NOTE (which | |
1121 * contains a set of saved /proc structures), and PT_LOAD (which | |
1122 * represents a memory mapping from the process's address space). | |
1123 * | |
1124 * Difference b/w Solaris PT_NOTE and BSD PT_NOTE: | |
1125 * | |
1126 * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present) | |
1127 * contains /proc structs in the pre-2.6 unstructured /proc format. the last | |
1128 * PT_NOTE has data in new /proc format. | |
1129 * | |
1130 * In Solaris, there is only one pstatus (process status). pstatus contains | |
1131 * integer register set among other stuff. For each LWP, we have one lwpstatus | |
1132 * entry that has integer regset for that LWP. | |
1133 * | |
1134 * Linux threads are actually 'clone'd processes. To support core analysis | |
1135 * of "multithreaded" process, Linux creates more than one pstatus (called | |
1136 * "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one | |
1137 * "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular | |
1138 * function "elf_core_dump". | |
1139 */ | |
1140 | |
1141 for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) { | |
1142 switch (core_php->p_type) { | |
1143 case PT_NOTE: | |
8750 | 1144 if (core_handle_note(ph, core_php) != true) { |
1145 goto err; | |
1146 } | |
3960 | 1147 break; |
1148 | |
1149 case PT_LOAD: { | |
1150 if (core_php->p_filesz != 0) { | |
1151 if (add_map_info(ph, ph->core->core_fd, core_php->p_offset, | |
1152 core_php->p_vaddr, core_php->p_filesz) == NULL) goto err; | |
1153 } | |
1154 break; | |
1155 } | |
1156 } | |
1157 | |
1158 core_php++; | |
1159 } | |
1160 | |
1161 free(phbuf); | |
1162 return true; | |
1163 err: | |
1164 free(phbuf); | |
1165 return false; | |
1166 } | |
1167 | |
1168 // read segments of a shared object | |
1169 static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) { | |
1170 int i = 0; | |
1171 ELF_PHDR* phbuf; | |
1172 ELF_PHDR* lib_php = NULL; | |
1173 | |
1174 if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) | |
1175 return false; | |
1176 | |
1177 // we want to process only PT_LOAD segments that are not writable. | |
1178 // i.e., text segments. The read/write/exec (data) segments would | |
1179 // have been already added from core file segments. | |
1180 for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) { | |
1181 if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) { | |
1182 if (add_map_info(ph, lib_fd, lib_php->p_offset, lib_php->p_vaddr + lib_base, lib_php->p_filesz) == NULL) | |
1183 goto err; | |
1184 } | |
1185 lib_php++; | |
1186 } | |
1187 | |
1188 free(phbuf); | |
1189 return true; | |
1190 err: | |
1191 free(phbuf); | |
1192 return false; | |
1193 } | |
1194 | |
1195 // process segments from interpreter (ld-elf.so.1) | |
1196 static bool read_interp_segments(struct ps_prochandle* ph) { | |
1197 ELF_EHDR interp_ehdr; | |
1198 | |
1199 if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) { | |
1200 print_debug("interpreter is not a valid ELF file\n"); | |
1201 return false; | |
1202 } | |
1203 | |
1204 if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) { | |
1205 print_debug("can't read segments of interpreter\n"); | |
1206 return false; | |
1207 } | |
1208 | |
1209 return true; | |
1210 } | |
1211 | |
1212 // process segments of a a.out | |
1213 static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) { | |
1214 int i = 0; | |
1215 ELF_PHDR* phbuf = NULL; | |
1216 ELF_PHDR* exec_php = NULL; | |
1217 | |
1218 if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL) | |
1219 return false; | |
1220 | |
1221 for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) { | |
1222 switch (exec_php->p_type) { | |
1223 | |
1224 // add mappings for PT_LOAD segments | |
1225 case PT_LOAD: { | |
1226 // add only non-writable segments of non-zero filesz | |
1227 if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) { | |
1228 if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err; | |
1229 } | |
1230 break; | |
1231 } | |
1232 | |
1233 // read the interpreter and it's segments | |
1234 case PT_INTERP: { | |
1235 char interp_name[BUF_SIZE]; | |
1236 | |
1237 pread(ph->core->exec_fd, interp_name, MIN(exec_php->p_filesz, BUF_SIZE), exec_php->p_offset); | |
1238 print_debug("ELF interpreter %s\n", interp_name); | |
1239 // read interpreter segments as well | |
1240 if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) { | |
1241 print_debug("can't open runtime loader\n"); | |
1242 goto err; | |
1243 } | |
1244 break; | |
1245 } | |
1246 | |
1247 // from PT_DYNAMIC we want to read address of first link_map addr | |
1248 case PT_DYNAMIC: { | |
1249 ph->core->dynamic_addr = exec_php->p_vaddr; | |
1250 print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr); | |
1251 break; | |
1252 } | |
1253 | |
1254 } // switch | |
1255 exec_php++; | |
1256 } // for | |
1257 | |
1258 free(phbuf); | |
1259 return true; | |
1260 err: | |
1261 free(phbuf); | |
1262 return false; | |
1263 } | |
1264 | |
1265 #define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug, r_map) | |
1266 #define LD_BASE_OFFSET offsetof(struct r_debug, r_ldbase) | |
1267 #define LINK_MAP_ADDR_OFFSET offsetof(struct link_map, l_addr) | |
1268 #define LINK_MAP_NAME_OFFSET offsetof(struct link_map, l_name) | |
1269 #define LINK_MAP_NEXT_OFFSET offsetof(struct link_map, l_next) | |
1270 | |
1271 // read shared library info from runtime linker's data structures. | |
1272 // This work is done by librtlb_db in Solaris | |
1273 static bool read_shared_lib_info(struct ps_prochandle* ph) { | |
8750 | 1274 uintptr_t addr = ph->core->dynamic_addr; |
1275 uintptr_t debug_base; | |
1276 uintptr_t first_link_map_addr; | |
1277 uintptr_t ld_base_addr; | |
1278 uintptr_t link_map_addr; | |
1279 uintptr_t lib_base_diff; | |
1280 uintptr_t lib_base; | |
1281 uintptr_t lib_name_addr; | |
1282 char lib_name[BUF_SIZE]; | |
1283 ELF_DYN dyn; | |
1284 ELF_EHDR elf_ehdr; | |
1285 int lib_fd; | |
3960 | 1286 |
8750 | 1287 // _DYNAMIC has information of the form |
1288 // [tag] [data] [tag] [data] ..... | |
1289 // Both tag and data are pointer sized. | |
1290 // We look for dynamic info with DT_DEBUG. This has shared object info. | |
1291 // refer to struct r_debug in link.h | |
3960 | 1292 |
8750 | 1293 dyn.d_tag = DT_NULL; |
1294 while (dyn.d_tag != DT_DEBUG) { | |
1295 if (ps_pread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { | |
1296 print_debug("can't read debug info from _DYNAMIC\n"); | |
1297 return false; | |
1298 } | |
1299 addr += sizeof(ELF_DYN); | |
1300 } | |
3960 | 1301 |
8750 | 1302 // we have got Dyn entry with DT_DEBUG |
1303 debug_base = dyn.d_un.d_ptr; | |
1304 // at debug_base we have struct r_debug. This has first link map in r_map field | |
1305 if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, | |
1306 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { | |
1307 print_debug("can't read first link map address\n"); | |
1308 return false; | |
1309 } | |
3960 | 1310 |
8750 | 1311 // read ld_base address from struct r_debug |
1312 // XXX: There is no r_ldbase member on BSD | |
1313 /* | |
1314 if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, | |
1315 sizeof(uintptr_t)) != PS_OK) { | |
1316 print_debug("can't read ld base address\n"); | |
1317 return false; | |
1318 } | |
1319 ph->core->ld_base_addr = ld_base_addr; | |
1320 */ | |
1321 ph->core->ld_base_addr = 0; | |
3960 | 1322 |
8750 | 1323 print_debug("interpreter base address is 0x%lx\n", ld_base_addr); |
3960 | 1324 |
8750 | 1325 // now read segments from interp (i.e ld-elf.so.1) |
1326 if (read_interp_segments(ph) != true) | |
1327 return false; | |
3960 | 1328 |
8750 | 1329 // after adding interpreter (ld.so) mappings sort again |
1330 if (sort_map_array(ph) != true) | |
1331 return false; | |
3960 | 1332 |
8750 | 1333 print_debug("first link map is at 0x%lx\n", first_link_map_addr); |
3960 | 1334 |
8750 | 1335 link_map_addr = first_link_map_addr; |
1336 while (link_map_addr != 0) { | |
1337 // read library base address of the .so. Note that even though <sys/link.h> calls | |
1338 // link_map->l_addr as "base address", this is * not * really base virtual | |
1339 // address of the shared object. This is actually the difference b/w the virtual | |
1340 // address mentioned in shared object and the actual virtual base where runtime | |
1341 // linker loaded it. We use "base diff" in read_lib_segments call below. | |
3960 | 1342 |
8750 | 1343 if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET, |
1344 &lib_base_diff, sizeof(uintptr_t)) != PS_OK) { | |
1345 print_debug("can't read shared object base address diff\n"); | |
1346 return false; | |
1347 } | |
3960 | 1348 |
8750 | 1349 // read address of the name |
1350 if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET, | |
1351 &lib_name_addr, sizeof(uintptr_t)) != PS_OK) { | |
1352 print_debug("can't read address of shared object name\n"); | |
1353 return false; | |
1354 } | |
3960 | 1355 |
8750 | 1356 // read name of the shared object |
1357 if (read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) { | |
1358 print_debug("can't read shared object name\n"); | |
1359 return false; | |
1360 } | |
3960 | 1361 |
8750 | 1362 if (lib_name[0] != '\0') { |
1363 // ignore empty lib names | |
1364 lib_fd = pathmap_open(lib_name); | |
3960 | 1365 |
8750 | 1366 if (lib_fd < 0) { |
1367 print_debug("can't open shared object %s\n", lib_name); | |
1368 // continue with other libraries... | |
1369 } else { | |
1370 if (read_elf_header(lib_fd, &elf_ehdr)) { | |
1371 lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr); | |
1372 print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n", | |
1373 lib_name, lib_base, lib_base_diff); | |
1374 // while adding library mappings we need to use "base difference". | |
1375 if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) { | |
1376 print_debug("can't read shared object's segments\n"); | |
1377 close(lib_fd); | |
1378 return false; | |
1379 } | |
1380 add_lib_info_fd(ph, lib_name, lib_fd, lib_base); | |
1381 // Map info is added for the library (lib_name) so | |
1382 // we need to re-sort it before calling the p_pdread. | |
1383 if (sort_map_array(ph) != true) | |
1384 return false; | |
1385 } else { | |
1386 print_debug("can't read ELF header for shared object %s\n", lib_name); | |
1387 close(lib_fd); | |
1388 // continue with other libraries... | |
1389 } | |
3960 | 1390 } |
8750 | 1391 } |
3960 | 1392 |
8750 | 1393 // read next link_map address |
1394 if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, | |
1395 &link_map_addr, sizeof(uintptr_t)) != PS_OK) { | |
1396 print_debug("can't read next link in link_map\n"); | |
1397 return false; | |
1398 } | |
1399 } | |
3960 | 1400 |
8750 | 1401 return true; |
3960 | 1402 } |
1403 | |
1404 // the one and only one exposed stuff from this file | |
1405 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { | |
8750 | 1406 ELF_EHDR core_ehdr; |
1407 ELF_EHDR exec_ehdr; | |
3960 | 1408 |
8750 | 1409 struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); |
1410 if (ph == NULL) { | |
1411 print_debug("cant allocate ps_prochandle\n"); | |
1412 return NULL; | |
1413 } | |
3960 | 1414 |
8750 | 1415 if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { |
1416 free(ph); | |
1417 print_debug("can't allocate ps_prochandle\n"); | |
1418 return NULL; | |
1419 } | |
3960 | 1420 |
8750 | 1421 // initialize ph |
1422 ph->ops = &core_ops; | |
1423 ph->core->core_fd = -1; | |
1424 ph->core->exec_fd = -1; | |
1425 ph->core->interp_fd = -1; | |
1426 | |
1427 print_debug("exec: %s core: %s", exec_file, core_file); | |
3960 | 1428 |
8750 | 1429 // open the core file |
1430 if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { | |
1431 print_debug("can't open core file\n"); | |
1432 goto err; | |
1433 } | |
3960 | 1434 |
8750 | 1435 // read core file ELF header |
1436 if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { | |
1437 print_debug("core file is not a valid ELF ET_CORE file\n"); | |
1438 goto err; | |
1439 } | |
3960 | 1440 |
8750 | 1441 if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { |
1442 print_debug("can't open executable file\n"); | |
1443 goto err; | |
1444 } | |
3960 | 1445 |
8750 | 1446 if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { |
1447 print_debug("executable file is not a valid ELF ET_EXEC file\n"); | |
1448 goto err; | |
1449 } | |
3960 | 1450 |
8750 | 1451 // process core file segments |
1452 if (read_core_segments(ph, &core_ehdr) != true) | |
1453 goto err; | |
3960 | 1454 |
8750 | 1455 // process exec file segments |
1456 if (read_exec_segments(ph, &exec_ehdr) != true) | |
1457 goto err; | |
3960 | 1458 |
8750 | 1459 // exec file is also treated like a shared object for symbol search |
1460 if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, | |
1461 (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) | |
1462 goto err; | |
3960 | 1463 |
8750 | 1464 // allocate and sort maps into map_array, we need to do this |
1465 // here because read_shared_lib_info needs to read from debuggee | |
1466 // address space | |
1467 if (sort_map_array(ph) != true) | |
1468 goto err; | |
3960 | 1469 |
8750 | 1470 if (read_shared_lib_info(ph) != true) |
1471 goto err; | |
3960 | 1472 |
8750 | 1473 // sort again because we have added more mappings from shared objects |
1474 if (sort_map_array(ph) != true) | |
1475 goto err; | |
3960 | 1476 |
8750 | 1477 if (init_classsharing_workaround(ph) != true) |
1478 goto err; | |
3960 | 1479 |
8750 | 1480 print_debug("Leave Pgrab_core\n"); |
1481 return ph; | |
3960 | 1482 |
1483 err: | |
8750 | 1484 Prelease(ph); |
1485 return NULL; | |
3960 | 1486 } |
8750 | 1487 |
1488 #endif // __APPLE__ |