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