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