1 /*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include <jni.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <elf.h>
32 #include <link.h>
33 #include "libproc_impl.h"
34 #include "salibelf.h"
35
36 // This file has the libproc implementation to read core files.
37 // For live processes, refer to ps_proc.c. Portions of this is adapted
38 // /modelled after Solaris libproc.so (in particular Pcore.c)
39
40 //----------------------------------------------------------------------
41 // ps_prochandle cleanup helper functions
42
43 // close all file descriptors
44 static void close_elf_files(struct ps_prochandle* ph) {
45 lib_info* lib = NULL;
46
47 // close core file descriptor
48 if (ph->core->core_fd >= 0)
49 close(ph->core->core_fd);
50
51 // close exec file descriptor
52 if (ph->core->exec_fd >= 0)
53 close(ph->core->exec_fd);
54
55 // close interp file descriptor
56 if (ph->core->interp_fd >= 0)
57 close(ph->core->interp_fd);
58
59 // close class share archive file
60 if (ph->core->classes_jsa_fd >= 0)
61 close(ph->core->classes_jsa_fd);
62
63 // close all library file descriptors
64 lib = ph->libs;
65 while (lib) {
66 int fd = lib->fd;
67 if (fd >= 0 && fd != ph->core->exec_fd) close(fd);
68 lib = lib->next;
69 }
70 }
71
72 // clean all map_info stuff
73 static void destroy_map_info(struct ps_prochandle* ph) {
74 map_info* map = ph->core->maps;
75 while (map) {
76 map_info* next = map->next;
77 free(map);
78 map = next;
79 }
80
81 if (ph->core->map_array) {
82 free(ph->core->map_array);
83 }
84
85 // Part of the class sharing workaround
86 map = ph->core->class_share_maps;
87 while (map) {
88 map_info* next = map->next;
89 free(map);
90 map = next;
91 }
92 }
93
94 // ps_prochandle operations
95 static void core_release(struct ps_prochandle* ph) {
96 if (ph->core) {
97 close_elf_files(ph);
98 destroy_map_info(ph);
99 free(ph->core);
100 }
101 }
102
103 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) {
104 map_info* map;
105 if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) {
106 print_debug("can't allocate memory for map_info\n");
107 return NULL;
108 }
109
110 // initialize map
111 map->fd = fd;
112 map->offset = offset;
113 map->vaddr = vaddr;
114 map->memsz = memsz;
115 return map;
116 }
117
137 map_info* map;
138 if ((map = allocate_init_map(ph->core->classes_jsa_fd,
139 offset, vaddr, memsz)) == NULL) {
140 return NULL;
141 }
142
143 map->next = ph->core->class_share_maps;
144 ph->core->class_share_maps = map;
145 return map;
146 }
147
148 // Return the map_info for the given virtual address. We keep a sorted
149 // array of pointers in ph->map_array, so we can binary search.
150 static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr)
151 {
152 int mid, lo = 0, hi = ph->core->num_maps - 1;
153 map_info *mp;
154
155 while (hi - lo > 1) {
156 mid = (lo + hi) / 2;
157 if (addr >= ph->core->map_array[mid]->vaddr)
158 lo = mid;
159 else
160 hi = mid;
161 }
162
163 if (addr < ph->core->map_array[hi]->vaddr)
164 mp = ph->core->map_array[lo];
165 else
166 mp = ph->core->map_array[hi];
167
168 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz)
169 return (mp);
170
171
172 // Part of the class sharing workaround
173 // Unfortunately, we have no way of detecting -Xshare state.
174 // Check out the share maps atlast, if we don't find anywhere.
175 // This is done this way so to avoid reading share pages
176 // ahead of other normal maps. For eg. with -Xshare:off we don't
177 // want to prefer class sharing data to data from core.
178 mp = ph->core->class_share_maps;
179 if (mp) {
180 print_debug("can't locate map_info at 0x%lx, trying class share maps\n",
181 addr);
182 }
183 while (mp) {
184 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
185 print_debug("located map_info at 0x%lx from class share maps\n",
186 addr);
187 return (mp);
188 }
189 mp = mp->next;
190 }
191
192 print_debug("can't locate map_info at 0x%lx\n", addr);
193 return (NULL);
194 }
195
196 //---------------------------------------------------------------
197 // Part of the class sharing workaround:
198 //
199 // With class sharing, pages are mapped from classes[_g].jsa file.
200 // The read-only class sharing pages are mapped as MAP_SHARED,
201 // PROT_READ pages. These pages are not dumped into core dump.
202 // With this workaround, these pages are read from classes[_g].jsa.
203
204 // FIXME: !HACK ALERT!
205 // The format of sharing achive file header is needed to read shared heap
206 // file mappings. For now, I am hard coding portion of FileMapHeader here.
233 char _read_only; // read only space?
234 char _allow_exec; // executable code in space?
235
236 } _space[NUM_SHARED_MAPS];
237
238 // Ignore the rest of the FileMapHeader. We don't need those fields here.
239 };
240
241 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) {
242 jboolean i;
243 if (ps_pread(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_pread(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_pread(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 jboolean useSharedSpaces = 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
308 // Hotspot vm types are not exported to build this library. So
309 // using equivalent type jboolean to read the value of
310 // UseSharedSpaces which is same as hotspot type "bool".
311 if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
312 print_debug("can't read the value of 'UseSharedSpaces' flag\n");
313 return false;
314 }
315
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++) {
442 print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr,
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 reg* 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 reg));
515 return true;
516 }
517 thr = thr->next;
518 }
519 return false;
520 }
521
522 static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo) {
523 print_debug("core_get_lwp_info not implemented\n");
524 return false;
525 }
526
527 static ps_prochandle_ops core_ops = {
528 .release= core_release,
529 .p_pread= core_read_data,
530 .p_pwrite= core_write_data,
531 .get_lwp_regs= core_get_lwp_regs,
532 .get_lwp_info= core_get_lwp_info
533 };
534
535 // read regs and create thread from NT_PRSTATUS entries from core file
536 static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) {
537 // we have to read prstatus_t from buf
538 // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t");
539 prstatus_t* prstat = (prstatus_t*) buf;
540 thread_info* newthr;
541 print_debug("got integer regset for lwp %d\n", prstat->pr_pid);
542 // we set pthread_t to -1 for core dump
543 if((newthr = add_thread_info(ph, (pthread_t) -1, prstat->pr_pid)) == NULL)
544 return false;
545
546 // copy regs
547 memcpy(&newthr->regs, &prstat->pr_reg, sizeof(struct reg));
548
549 if (is_debug()) {
550 print_debug("integer regset\n");
551 #ifdef i386
552 // print the regset
553 print_debug("\teax = 0x%x\n", newthr->regs.r_eax);
554 print_debug("\tebx = 0x%x\n", newthr->regs.r_ebx);
555 print_debug("\tecx = 0x%x\n", newthr->regs.r_ecx);
556 print_debug("\tedx = 0x%x\n", newthr->regs.r_edx);
557 print_debug("\tesp = 0x%x\n", newthr->regs.r_esp);
558 print_debug("\tebp = 0x%x\n", newthr->regs.r_ebp);
559 print_debug("\tesi = 0x%x\n", newthr->regs.r_esi);
560 print_debug("\tedi = 0x%x\n", newthr->regs.r_edi);
615 // an Elf{32/64}_Nhdr structure describing its type and size.
616 if ( (buf = (char*) malloc(size)) == NULL) {
617 print_debug("can't allocate memory for reading core notes\n");
618 goto err;
619 }
620
621 // read notes into buffer
622 if (read(ph->core->core_fd, buf, size) != size) {
623 print_debug("failed to read notes, core file must have been truncated\n");
624 goto err;
625 }
626
627 p = buf;
628 while (p < buf + size) {
629 ELF_NHDR* notep = (ELF_NHDR*) p;
630 char* descdata = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4);
631 print_debug("Note header with n_type = %d and n_descsz = %u\n",
632 notep->n_type, notep->n_descsz);
633
634 if (notep->n_type == NT_PRSTATUS) {
635 if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true)
636 return false;
637 }
638 p = descdata + ROUNDUP(notep->n_descsz, 4);
639 }
640
641 free(buf);
642 return true;
643
644 err:
645 if (buf) free(buf);
646 return false;
647 }
648
649 // read all segments from core file
650 static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
651 int i = 0;
652 ELF_PHDR* phbuf = NULL;
653 ELF_PHDR* core_php = NULL;
654
655 if ((phbuf = read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL)
656 return false;
657
664 * Difference b/w Solaris PT_NOTE and BSD PT_NOTE:
665 *
666 * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
667 * contains /proc structs in the pre-2.6 unstructured /proc format. the last
668 * PT_NOTE has data in new /proc format.
669 *
670 * In Solaris, there is only one pstatus (process status). pstatus contains
671 * integer register set among other stuff. For each LWP, we have one lwpstatus
672 * entry that has integer regset for that LWP.
673 *
674 * Linux threads are actually 'clone'd processes. To support core analysis
675 * of "multithreaded" process, Linux creates more than one pstatus (called
676 * "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one
677 * "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular
678 * function "elf_core_dump".
679 */
680
681 for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) {
682 switch (core_php->p_type) {
683 case PT_NOTE:
684 if (core_handle_note(ph, core_php) != true) goto err;
685 break;
686
687 case PT_LOAD: {
688 if (core_php->p_filesz != 0) {
689 if (add_map_info(ph, ph->core->core_fd, core_php->p_offset,
690 core_php->p_vaddr, core_php->p_filesz) == NULL) goto err;
691 }
692 break;
693 }
694 }
695
696 core_php++;
697 }
698
699 free(phbuf);
700 return true;
701 err:
702 free(phbuf);
703 return false;
704 }
783 }
784
785 // from PT_DYNAMIC we want to read address of first link_map addr
786 case PT_DYNAMIC: {
787 ph->core->dynamic_addr = exec_php->p_vaddr;
788 print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
789 break;
790 }
791
792 } // switch
793 exec_php++;
794 } // for
795
796 free(phbuf);
797 return true;
798 err:
799 free(phbuf);
800 return false;
801 }
802
803
804 #define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug, r_map)
805 #define LD_BASE_OFFSET offsetof(struct r_debug, r_ldbase)
806 #define LINK_MAP_ADDR_OFFSET offsetof(struct link_map, l_addr)
807 #define LINK_MAP_NAME_OFFSET offsetof(struct link_map, l_name)
808 #define LINK_MAP_NEXT_OFFSET offsetof(struct link_map, l_next)
809
810 // read shared library info from runtime linker's data structures.
811 // This work is done by librtlb_db in Solaris
812 static bool read_shared_lib_info(struct ps_prochandle* ph) {
813 uintptr_t addr = ph->core->dynamic_addr;
814 uintptr_t debug_base;
815 uintptr_t first_link_map_addr;
816 uintptr_t ld_base_addr;
817 uintptr_t link_map_addr;
818 uintptr_t lib_base_diff;
819 uintptr_t lib_base;
820 uintptr_t lib_name_addr;
821 char lib_name[BUF_SIZE];
822 ELF_DYN dyn;
823 ELF_EHDR elf_ehdr;
832 dyn.d_tag = DT_NULL;
833 while (dyn.d_tag != DT_DEBUG) {
834 if (ps_pread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) {
835 print_debug("can't read debug info from _DYNAMIC\n");
836 return false;
837 }
838 addr += sizeof(ELF_DYN);
839 }
840
841 // we have got Dyn entry with DT_DEBUG
842 debug_base = dyn.d_un.d_ptr;
843 // at debug_base we have struct r_debug. This has first link map in r_map field
844 if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET,
845 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) {
846 print_debug("can't read first link map address\n");
847 return false;
848 }
849
850 // read ld_base address from struct r_debug
851 // XXX: There is no r_ldbase member on BSD
852 /*
853 if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
854 sizeof(uintptr_t)) != PS_OK) {
855 print_debug("can't read ld base address\n");
856 return false;
857 }
858 ph->core->ld_base_addr = ld_base_addr;
859 */
860 ph->core->ld_base_addr = 0;
861
862 print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
863
864 // now read segments from interp (i.e ld-elf.so.1)
865 if (read_interp_segments(ph) != true)
866 return false;
867
868 // after adding interpreter (ld.so) mappings sort again
869 if (sort_map_array(ph) != true)
870 return false;
871
872 print_debug("first link map is at 0x%lx\n", first_link_map_addr);
873
874 link_map_addr = first_link_map_addr;
875 while (link_map_addr != 0) {
876 // read library base address of the .so. Note that even though <sys/link.h> calls
877 // link_map->l_addr as "base address", this is * not * really base virtual
878 // address of the shared object. This is actually the difference b/w the virtual
879 // address mentioned in shared object and the actual virtual base where runtime
930 }
931
932 // read next link_map address
933 if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET,
934 &link_map_addr, sizeof(uintptr_t)) != PS_OK) {
935 print_debug("can't read next link in link_map\n");
936 return false;
937 }
938 }
939
940 return true;
941 }
942
943 // the one and only one exposed stuff from this file
944 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
945 ELF_EHDR core_ehdr;
946 ELF_EHDR exec_ehdr;
947
948 struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
949 if (ph == NULL) {
950 print_debug("can't allocate ps_prochandle\n");
951 return NULL;
952 }
953
954 if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
955 free(ph);
956 print_debug("can't allocate ps_prochandle\n");
957 return NULL;
958 }
959
960 // initialize ph
961 ph->ops = &core_ops;
962 ph->core->core_fd = -1;
963 ph->core->exec_fd = -1;
964 ph->core->interp_fd = -1;
965
966 // open the core file
967 if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
968 print_debug("can't open core file\n");
969 goto err;
970 }
971
972 // read core file ELF header
973 if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) {
974 print_debug("core file is not a valid ELF ET_CORE file\n");
975 goto err;
976 }
977
978 if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
979 print_debug("can't open executable file\n");
980 goto err;
981 }
982
983 if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) {
984 print_debug("executable file is not a valid ELF ET_EXEC file\n");
985 goto err;
997 if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd,
998 (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL)
999 goto err;
1000
1001 // allocate and sort maps into map_array, we need to do this
1002 // here because read_shared_lib_info needs to read from debuggee
1003 // address space
1004 if (sort_map_array(ph) != true)
1005 goto err;
1006
1007 if (read_shared_lib_info(ph) != true)
1008 goto err;
1009
1010 // sort again because we have added more mappings from shared objects
1011 if (sort_map_array(ph) != true)
1012 goto err;
1013
1014 if (init_classsharing_workaround(ph) != true)
1015 goto err;
1016
1017 return ph;
1018
1019 err:
1020 Prelease(ph);
1021 return NULL;
1022 }
|
1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include <jni.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include "libproc_impl.h"
32
33 #ifdef __APPLE__
34 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
35 #endif
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
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);
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) {
68 close(fd);
69 }
70 lib = lib->next;
71 }
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) {
78 map_info* next = map->next;
79 free(map);
80 map = next;
81 }
82
83 if (ph->core->map_array) {
84 free(ph->core->map_array);
85 }
86
87 // Part of the class sharing workaround
88 map = ph->core->class_share_maps;
89 while (map) {
90 map_info* next = map->next;
91 free(map);
92 map = next;
93 }
94 }
95
96 // ps_prochandle operations
97 static void core_release(struct ps_prochandle* ph) {
98 if (ph->core) {
99 close_files(ph);
100 destroy_map_info(ph);
101 free(ph->core);
102 }
103 }
104
105 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) {
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 }
111
112 // initialize map
113 map->fd = fd;
114 map->offset = offset;
115 map->vaddr = vaddr;
116 map->memsz = memsz;
117 return map;
118 }
119
139 map_info* map;
140 if ((map = allocate_init_map(ph->core->classes_jsa_fd,
141 offset, vaddr, memsz)) == NULL) {
142 return NULL;
143 }
144
145 map->next = ph->core->class_share_maps;
146 ph->core->class_share_maps = map;
147 return map;
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 {
154 int mid, lo = 0, hi = ph->core->num_maps - 1;
155 map_info *mp;
156
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 }
165
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 }
171
172 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
173 return (mp);
174 }
175
176
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 }
194
195 print_debug("can't locate map_info at 0x%lx\n", addr);
196 return (NULL);
197 }
198
199 //---------------------------------------------------------------
200 // Part of the class sharing workaround:
201 //
202 // With class sharing, pages are mapped from classes[_g].jsa file.
203 // The read-only class sharing pages are mapped as MAP_SHARED,
204 // PROT_READ pages. These pages are not dumped into core dump.
205 // With this workaround, these pages are read from classes[_g].jsa.
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.
236 char _read_only; // read only space?
237 char _allow_exec; // executable code in space?
238
239 } _space[NUM_SHARED_MAPS];
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) {
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 }
252 }
253
254 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
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 }
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) {
266 size_t i = 0;
267 char c = ' ';
268
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;
283 }
284
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
290 #define USE_SHARED_SPACES_SYM "UseSharedSpaces"
291 // mangled name of Arguments::SharedArchivePath
292 #define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE"
293 #endif // __APPLE_
294
295 static bool init_classsharing_workaround(struct ps_prochandle* ph) {
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
301 // libjvm[_g].so.
302 const char *jvm_name = 0;
303 #ifdef __APPLE__
304 if ((jvm_name = strstr(lib->name, "/libjvm.dylib")) != 0 ||
305 (jvm_name = strstr(lib->name, "/libjvm_g.dylib")) != 0)
306 #else
307 if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0 ||
308 (jvm_name = strstr(lib->name, "/libjvm_g.so")) != 0)
309 #endif // __APPLE__
310 {
311 char classes_jsa[PATH_MAX];
312 struct FileMapHeader header;
313 int fd = -1;
314 uintptr_t base = 0, useSharedSpacesAddr = 0;
315 uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
316 jboolean useSharedSpaces = 0;
317
318 memset(classes_jsa, 0, sizeof(classes_jsa));
319 jvm_name = lib->name;
320 useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM);
321 if (useSharedSpacesAddr == 0) {
322 print_debug("can't lookup 'UseSharedSpaces' flag\n");
323 return false;
324 }
325
326 // Hotspot vm types are not exported to build this library. So
327 // using equivalent type jboolean to read the value of
328 // UseSharedSpaces which is same as hotspot type "bool".
329 if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
330 print_debug("can't read the value of 'UseSharedSpaces' flag\n");
331 return false;
332 }
333
390
391 ph->core->classes_jsa_fd = fd;
392 // add read-only maps from classes[_g].jsa to the list of maps
393 for (m = 0; m < NUM_SHARED_MAPS; m++) {
394 if (header._space[m]._read_only) {
395 base = (uintptr_t) header._space[m]._base;
396 // no need to worry about the fractional pages at-the-end.
397 // possible fractional pages are handled by core_read_data.
398 add_class_share_map_info(ph, (off_t) header._space[m]._file_offset,
399 base, (size_t) header._space[m]._used);
400 print_debug("added a share archive map at 0x%lx\n", base);
401 }
402 }
403 return true;
404 }
405 lib = lib->next;
406 }
407 return true;
408 }
409
410 //---------------------------------------------------------------------------
411 // functions to handle map_info
412
413 // Order mappings based on virtual address. We use this function as the
414 // callback for sorting the array of map_info pointers.
415 static int core_cmp_mapping(const void *lhsp, const void *rhsp)
416 {
417 const map_info *lhs = *((const map_info **)lhsp);
418 const map_info *rhs = *((const map_info **)rhsp);
419
420 if (lhs->vaddr == rhs->vaddr) {
421 return (0);
422 }
423
424 return (lhs->vaddr < rhs->vaddr ? -1 : 1);
425 }
426
427 // we sort map_info by starting virtual address so that we can do
428 // binary search to read from an address.
429 static bool sort_map_array(struct ps_prochandle* ph) {
430 size_t num_maps = ph->core->num_maps;
431 map_info* map = ph->core->maps;
432 int i = 0;
433
434 // allocate map_array
435 map_info** array;
436 if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) {
437 print_debug("can't allocate memory for map array\n");
438 return false;
439 }
440
441 // add maps to array
442 while (map) {
443 array[i] = map;
444 i++;
445 map = map->next;
446 }
447
448 // sort is called twice. If this is second time, clear map array
449 if (ph->core->map_array) {
450 free(ph->core->map_array);
451 }
452 ph->core->map_array = array;
453 // sort the map_info array by base virtual address.
454 qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
455 core_cmp_mapping);
456
457 // print map
458 if (is_debug()) {
459 int j = 0;
460 print_debug("---- sorted virtual address map ----\n");
461 for (j = 0; j < ph->core->num_maps; j++) {
462 print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr,
463 ph->core->map_array[j]->memsz);
464 }
465 }
466
467 return true;
468 }
469
470 #ifndef MIN
471 #define MIN(x, y) (((x) < (y))? (x): (y))
472 #endif
473
474 static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
475 ssize_t resid = size;
476 int page_size=sysconf(_SC_PAGE_SIZE);
477 while (resid != 0) {
478 map_info *mp = core_lookup(ph, addr);
479 uintptr_t mapoff;
480 ssize_t len, rem;
481 off_t off;
482 int fd;
483
484 if (mp == NULL) {
485 break; /* No mapping for this address */
486 }
487
488 fd = mp->fd;
489 mapoff = addr - mp->vaddr;
490 len = MIN(resid, mp->memsz - mapoff);
491 off = mp->offset + mapoff;
492
493 if ((len = pread(fd, buf, len, off)) <= 0) {
494 break;
495 }
496
497 resid -= len;
498 addr += len;
499 buf = (char *)buf + len;
500
501 // mappings always start at page boundary. But, may end in fractional
502 // page. fill zeros for possible fractional page at the end of a mapping.
503 rem = mp->memsz % page_size;
504 if (rem > 0) {
505 rem = page_size - rem;
506 len = MIN(resid, rem);
507 resid -= len;
508 addr += len;
509 // we are not assuming 'buf' to be zero initialized.
510 memset(buf, 0, len);
511 buf += len;
512 }
513 }
514
515 if (resid) {
516 print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n",
517 size, addr, resid);
518 return false;
519 } else {
520 return true;
521 }
522 }
523
524 // null implementation for write
525 static bool core_write_data(struct ps_prochandle* ph,
526 uintptr_t addr, const char *buf , size_t size) {
527 return false;
528 }
529
530 static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id,
531 struct reg* regs) {
532 // for core we have cached the lwp regs after segment parsed
533 sa_thread_info* thr = ph->threads;
534 while (thr) {
535 if (thr->lwp_id == lwp_id) {
536 memcpy(regs, &thr->regs, sizeof(struct reg));
537 return true;
538 }
539 thr = thr->next;
540 }
541 return false;
542 }
543
544 static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t id, void *info) {
545 print_debug("core_get_lwp_info not implemented\n");
546 return false;
547 }
548
549 static ps_prochandle_ops core_ops = {
550 .release= core_release,
551 .p_pread= core_read_data,
552 .p_pwrite= core_write_data,
553 .get_lwp_regs= core_get_lwp_regs,
554 .get_lwp_info= core_get_lwp_info
555 };
556
557 // from this point, mainly two blocks divided by def __APPLE__
558 // one for Macosx, the other for regular Bsd
559
560 #ifdef __APPLE__
561
562 void print_thread(sa_thread_info *threadinfo) {
563 print_debug("thread added: %d\n", threadinfo->lwp_id);
564 print_debug("registers:\n");
565 print_debug(" r_r15: 0x%" PRIx64 "\n", threadinfo->regs.r_r15);
566 print_debug(" r_r14: 0x%" PRIx64 "\n", threadinfo->regs.r_r14);
567 print_debug(" r_r13: 0x%" PRIx64 "\n", threadinfo->regs.r_r13);
568 print_debug(" r_r12: 0x%" PRIx64 "\n", threadinfo->regs.r_r12);
569 print_debug(" r_r11: 0x%" PRIx64 "\n", threadinfo->regs.r_r11);
570 print_debug(" r_r10: 0x%" PRIx64 "\n", threadinfo->regs.r_r10);
571 print_debug(" r_r9: 0x%" PRIx64 "\n", threadinfo->regs.r_r9);
572 print_debug(" r_r8: 0x%" PRIx64 "\n", threadinfo->regs.r_r8);
573 print_debug(" r_rdi: 0x%" PRIx64 "\n", threadinfo->regs.r_rdi);
574 print_debug(" r_rsi: 0x%" PRIx64 "\n", threadinfo->regs.r_rsi);
575 print_debug(" r_rbp: 0x%" PRIx64 "\n", threadinfo->regs.r_rbp);
576 print_debug(" r_rbx: 0x%" PRIx64 "\n", threadinfo->regs.r_rbx);
577 print_debug(" r_rdx: 0x%" PRIx64 "\n", threadinfo->regs.r_rdx);
578 print_debug(" r_rcx: 0x%" PRIx64 "\n", threadinfo->regs.r_rcx);
579 print_debug(" r_rax: 0x%" PRIx64 "\n", threadinfo->regs.r_rax);
580 print_debug(" r_fs: 0x%" PRIx32 "\n", threadinfo->regs.r_fs);
581 print_debug(" r_gs: 0x%" PRIx32 "\n", threadinfo->regs.r_gs);
582 print_debug(" r_rip 0x%" PRIx64 "\n", threadinfo->regs.r_rip);
583 print_debug(" r_cs: 0x%" PRIx64 "\n", threadinfo->regs.r_cs);
584 print_debug(" r_rsp: 0x%" PRIx64 "\n", threadinfo->regs.r_rsp);
585 print_debug(" r_rflags: 0x%" PRIx64 "\n", threadinfo->regs.r_rflags);
586 }
587
588 // read all segments64 commands from core file
589 // read all thread commands from core file
590 static bool read_core_segments(struct ps_prochandle* ph) {
591 int i = 0;
592 int num_threads = 0;
593 int fd = ph->core->core_fd;
594 off_t offset = 0;
595 mach_header_64 fhead;
596 load_command lcmd;
597 segment_command_64 segcmd;
598 // thread_command thrcmd;
599
600 lseek(fd, offset, SEEK_SET);
601 if(read(fd, (void *)&fhead, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
602 goto err;
603 }
604 print_debug("total commands: %d\n", fhead.ncmds);
605 offset += sizeof(mach_header_64);
606 for (i = 0; i < fhead.ncmds; i++) {
607 lseek(fd, offset, SEEK_SET);
608 if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
609 goto err;
610 }
611 offset += lcmd.cmdsize; // next command position
612 if (lcmd.cmd == LC_SEGMENT_64) {
613 lseek(fd, -sizeof(load_command), SEEK_CUR);
614 if (read(fd, (void *)&segcmd, sizeof(segment_command_64)) != sizeof(segment_command_64)) {
615 print_debug("failed to read LC_SEGMENT_64 i = %d!\n", i);
616 goto err;
617 }
618 if (add_map_info(ph, fd, segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize) == NULL) {
619 print_debug("Failed to add map_info at i = %d\n", i);
620 goto err;
621 }
622 print_debug("segment added: %" PRIu64 " 0x%" PRIx64 " %d\n",
623 segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize);
624 } else if (lcmd.cmd == LC_THREAD || lcmd.cmd == LC_UNIXTHREAD) {
625 typedef struct thread_fc {
626 uint32_t flavor;
627 uint32_t count;
628 } thread_fc;
629 thread_fc fc;
630 uint32_t size = sizeof(load_command);
631 while (size < lcmd.cmdsize) {
632 if (read(fd, (void *)&fc, sizeof(thread_fc)) != sizeof(thread_fc)) {
633 printf("Reading flavor, count failed.\n");
634 goto err;
635 }
636 size += sizeof(thread_fc);
637 if (fc.flavor == x86_THREAD_STATE) {
638 x86_thread_state_t thrstate;
639 if (read(fd, (void *)&thrstate, sizeof(x86_thread_state_t)) != sizeof(x86_thread_state_t)) {
640 printf("Reading flavor, count failed.\n");
641 goto err;
642 }
643 size += sizeof(x86_thread_state_t);
644 // create thread info list, update lwp_id later
645 sa_thread_info* newthr = add_thread_info(ph, (pthread_t) -1, (lwpid_t) num_threads++);
646 if (newthr == NULL) {
647 printf("create thread_info failed\n");
648 goto err;
649 }
650
651 // note __DARWIN_UNIX03 depengs on other definitions
652 #if __DARWIN_UNIX03
653 #define get_register_v(regst, regname) \
654 regst.uts.ts64.__##regname
655 #else
656 #define get_register_v(regst, regname) \
657 regst.uts.ts64.##regname
658 #endif // __DARWIN_UNIX03
659 newthr->regs.r_rax = get_register_v(thrstate, rax);
660 newthr->regs.r_rbx = get_register_v(thrstate, rbx);
661 newthr->regs.r_rcx = get_register_v(thrstate, rcx);
662 newthr->regs.r_rdx = get_register_v(thrstate, rdx);
663 newthr->regs.r_rdi = get_register_v(thrstate, rdi);
664 newthr->regs.r_rsi = get_register_v(thrstate, rsi);
665 newthr->regs.r_rbp = get_register_v(thrstate, rbp);
666 newthr->regs.r_rsp = get_register_v(thrstate, rsp);
667 newthr->regs.r_r8 = get_register_v(thrstate, r8);
668 newthr->regs.r_r9 = get_register_v(thrstate, r9);
669 newthr->regs.r_r10 = get_register_v(thrstate, r10);
670 newthr->regs.r_r11 = get_register_v(thrstate, r11);
671 newthr->regs.r_r12 = get_register_v(thrstate, r12);
672 newthr->regs.r_r13 = get_register_v(thrstate, r13);
673 newthr->regs.r_r14 = get_register_v(thrstate, r14);
674 newthr->regs.r_r15 = get_register_v(thrstate, r15);
675 newthr->regs.r_rip = get_register_v(thrstate, rip);
676 newthr->regs.r_rflags = get_register_v(thrstate, rflags);
677 newthr->regs.r_cs = get_register_v(thrstate, cs);
678 newthr->regs.r_fs = get_register_v(thrstate, fs);
679 newthr->regs.r_gs = get_register_v(thrstate, gs);
680 print_thread(newthr);
681 } else if (fc.flavor == x86_FLOAT_STATE) {
682 x86_float_state_t flstate;
683 if (read(fd, (void *)&flstate, sizeof(x86_float_state_t)) != sizeof(x86_float_state_t)) {
684 print_debug("Reading flavor, count failed.\n");
685 goto err;
686 }
687 size += sizeof(x86_float_state_t);
688 } else if (fc.flavor == x86_EXCEPTION_STATE) {
689 x86_exception_state_t excpstate;
690 if (read(fd, (void *)&excpstate, sizeof(x86_exception_state_t)) != sizeof(x86_exception_state_t)) {
691 printf("Reading flavor, count failed.\n");
692 goto err;
693 }
694 size += sizeof(x86_exception_state_t);
695 }
696 }
697 }
698 }
699 return true;
700 err:
701 return false;
702 }
703
704 /**local function **/
705 bool exists(const char *fname)
706 {
707 int fd;
708 if ((fd = open(fname, O_RDONLY)) > 0) {
709 close(fd);
710 return true;
711 }
712 return false;
713 }
714
715 // we check: 1. lib
716 // 2. lib/server
717 // 3. jre/lib
718 // 4. jre/lib/server
719 // from: 1. exe path
720 // 2. JAVA_HOME
721 // 3. DYLD_LIBRARY_PATH
722 static bool get_real_path(struct ps_prochandle* ph, char *rpath) {
723 /** check if they exist in JAVA ***/
724 char* execname = ph->core->exec_path;
725 char filepath[4096];
726 char* filename = strrchr(rpath, '/'); // like /libjvm.dylib
727 if (filename == NULL) {
728 return false;
729 }
730
731 char* posbin = strstr(execname, "/bin/java");
732 if (posbin != NULL) {
733 memcpy(filepath, execname, posbin - execname); // not include trailing '/'
734 filepath[posbin - execname] = '\0';
735 } else {
736 char* java_home = getenv("JAVA_HOME");
737 if (java_home != NULL) {
738 strcpy(filepath, java_home);
739 } else {
740 char* dyldpath = getenv("DYLD_LIBRARY_PATH");
741 char* dypath = strtok(dyldpath, ":");
742 while (dypath != NULL) {
743 strcpy(filepath, dypath);
744 strcat(filepath, filename);
745 if (exists(filepath)) {
746 strcpy(rpath, filepath);
747 return true;
748 }
749 dypath = strtok(dyldpath, ":");
750 }
751 // not found
752 return false;
753 }
754 }
755 // for exec and java_home, jdkpath now is filepath
756 size_t filepath_base_size = strlen(filepath);
757
758 // first try /lib/ and /lib/server
759 strcat(filepath, "/lib");
760 strcat(filepath, filename);
761 if (exists(filepath)) {
762 strcpy(rpath, filepath);
763 return true;
764 }
765 char* pos = strstr(filepath, filename); // like /libjvm.dylib
766 *pos = '\0';
767 strcat(filepath, "/server");
768 strcat(filepath, filename);
769 if (exists(filepath)) {
770 strcpy(rpath, filepath);
771 return true;
772 }
773
774
775 // then try /jre/lib/ and /jre/lib/server
776 filepath[filepath_base_size] = '\0';
777 strcat(filepath, "/jre/lib");
778 strcat(filepath, filename);
779 if (exists(filepath)) {
780 strcpy(rpath, filepath);
781 return true;
782 }
783 pos = strstr(filepath, filename);
784 *pos = '\0';
785 strcat(filepath, "/server");
786 strcat(filepath, filename);
787 if (exists(filepath)) {
788 strcpy(rpath, filepath);
789 return true;
790 }
791
792 return false;
793 }
794
795 static bool read_shared_lib_info(struct ps_prochandle* ph) {
796 static int pagesize = 0;
797 int fd = ph->core->core_fd;
798 int i = 0, j;
799 uint32_t v;
800 mach_header_64 header; // used to check if a file header in segment
801 load_command lcmd;
802 dylib_command dylibcmd;
803
804 char name[BUF_SIZE]; // use to store name
805
806 if (pagesize == 0) {
807 pagesize = getpagesize();
808 print_debug("page size is %d\n", pagesize);
809 }
810 for (j = 0; j < ph->core->num_maps; j++) {
811 map_info *iter = ph->core->map_array[j]; // head
812 off_t fpos = iter->offset;
813 if (iter->fd != fd) {
814 // only search core file!
815 continue;
816 }
817 print_debug("map_info %d: vmaddr = 0x%016" PRIx64 " fileoff = %" PRIu64 " vmsize = %" PRIu64 "\n",
818 j, iter->vaddr, iter->offset, iter->memsz);
819 lseek(fd, fpos, SEEK_SET);
820 // we assume .dylib loaded at segment address --- which is true for JVM libraries
821 // multiple files may be loaded in one segment.
822 // if first word is not a magic word, means this segment does not contain lib file.
823 if (read(fd, (void *)&v, sizeof(uint32_t)) == sizeof(uint32_t)) {
824 if (v != MH_MAGIC_64) {
825 continue;
826 }
827 } else {
828 // may be encountered last map, which is not readable
829 continue;
830 }
831 while (ltell(fd) - iter->offset < iter->memsz) {
832 lseek(fd, fpos, SEEK_SET);
833 if (read(fd, (void *)&v, sizeof(uint32_t)) != sizeof(uint32_t)) {
834 break;
835 }
836 if (v != MH_MAGIC_64) {
837 fpos = (ltell(fd) + pagesize -1)/pagesize * pagesize;
838 continue;
839 }
840 lseek(fd, -sizeof(uint32_t), SEEK_CUR);
841 // this is the file begining to core file.
842 if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
843 goto err;
844 }
845 fpos = ltell(fd);
846
847 // found a mach-o file in this segment
848 for (i = 0; i < header.ncmds; i++) {
849 // read commands in this "file"
850 // LC_ID_DYLIB is the file itself for a .dylib
851 lseek(fd, fpos, SEEK_SET);
852 if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
853 return false; // error
854 }
855 fpos += lcmd.cmdsize; // next command position
856 // make sure still within seg size.
857 if (fpos - lcmd.cmdsize - iter->offset > iter->memsz) {
858 print_debug("Warning: out of segement limit: %ld \n", fpos - lcmd.cmdsize - iter->offset);
859 break; // no need to iterate all commands
860 }
861 if (lcmd.cmd == LC_ID_DYLIB) {
862 lseek(fd, -sizeof(load_command), SEEK_CUR);
863 if (read(fd, (void *)&dylibcmd, sizeof(dylib_command)) != sizeof(dylib_command)) {
864 return false;
865 }
866 /**** name stored at dylib_command.dylib.name.offset, is a C string */
867 lseek(fd, dylibcmd.dylib.name.offset - sizeof(dylib_command), SEEK_CUR);
868 int j = 0;
869 while (j < BUF_SIZE) {
870 read(fd, (void *)(name + j), sizeof(char));
871 if (name[j] == '\0') break;
872 j++;
873 }
874 print_debug("%s\n", name);
875 // changed name from @rpath/xxxx.dylib to real path
876 if (strrchr(name, '@')) {
877 get_real_path(ph, name);
878 print_debug("get_real_path returned: %s\n", name);
879 }
880 add_lib_info(ph, name, iter->vaddr);
881 break;
882 }
883 }
884 // done with the file, advanced to next page to search more files
885 fpos = (ltell(fd) + pagesize - 1) / pagesize * pagesize;
886 }
887 }
888 return true;
889 err:
890 return false;
891 }
892
893 bool read_macho64_header(int fd, mach_header_64* core_header) {
894 bool is_macho = false;
895 if (fd < 0) return false;
896 off_t pos = ltell(fd);
897 lseek(fd, 0, SEEK_SET);
898 if (read(fd, (void *)core_header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
899 is_macho = false;
900 } else {
901 is_macho = (core_header->magic == MH_MAGIC_64 || core_header->magic == MH_CIGAM_64);
902 }
903 lseek(fd, pos, SEEK_SET);
904 return is_macho;
905 }
906
907 // the one and only one exposed stuff from this file
908 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
909 mach_header_64 core_header;
910 mach_header_64 exec_header;
911
912 struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
913 if (ph == NULL) {
914 print_debug("cant allocate ps_prochandle\n");
915 return NULL;
916 }
917
918 if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
919 free(ph);
920 print_debug("can't allocate ps_prochandle\n");
921 return NULL;
922 }
923
924 // initialize ph
925 ph->ops = &core_ops;
926 ph->core->core_fd = -1;
927 ph->core->exec_fd = -1;
928 ph->core->interp_fd = -1;
929
930 print_debug("exec: %s core: %s", exec_file, core_file);
931
932 strncpy(ph->core->exec_path, exec_file, sizeof(ph->core->exec_path));
933
934 // open the core file
935 if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
936 print_error("can't open core file\n");
937 goto err;
938 }
939
940 // read core file header
941 if (read_macho64_header(ph->core->core_fd, &core_header) != true || core_header.filetype != MH_CORE) {
942 print_debug("core file is not a valid Mach-O file\n");
943 goto err;
944 }
945
946 if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
947 print_error("can't open executable file\n");
948 goto err;
949 }
950
951 if (read_macho64_header(ph->core->exec_fd, &exec_header) != true ||
952 exec_header.filetype != MH_EXECUTE) {
953 print_error("executable file is not a valid Mach-O file\n");
954 goto err;
955 }
956
957 // process core file segments
958 if (read_core_segments(ph) != true) {
959 print_error("failed to read core segments\n");
960 goto err;
961 }
962
963 // allocate and sort maps into map_array, we need to do this
964 // here because read_shared_lib_info needs to read from debuggee
965 // address space
966 if (sort_map_array(ph) != true) {
967 print_error("failed to sort segment map array\n");
968 goto err;
969 }
970
971 if (read_shared_lib_info(ph) != true) {
972 print_error("failed to read libraries\n");
973 goto err;
974 }
975
976 // sort again because we have added more mappings from shared objects
977 if (sort_map_array(ph) != true) {
978 print_error("failed to sort segment map array\n");
979 goto err;
980 }
981
982 if (init_classsharing_workaround(ph) != true) {
983 print_error("failed to workaround classshareing\n");
984 goto err;
985 }
986
987 print_debug("Leave Pgrab_core\n");
988 return ph;
989
990 err:
991 Prelease(ph);
992 return NULL;
993 }
994
995 #else // __APPLE__ (none macosx)
996
997 // read regs and create thread from core file
998 static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) {
999 // we have to read prstatus_t from buf
1000 // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t");
1001 prstatus_t* prstat = (prstatus_t*) buf;
1002 sa_thread_info* newthr;
1003 print_debug("got integer regset for lwp %d\n", prstat->pr_pid);
1004 // we set pthread_t to -1 for core dump
1005 if((newthr = add_thread_info(ph, (pthread_t) -1, prstat->pr_pid)) == NULL)
1006 return false;
1007
1008 // copy regs
1009 memcpy(&newthr->regs, &prstat->pr_reg, sizeof(struct reg));
1010
1011 if (is_debug()) {
1012 print_debug("integer regset\n");
1013 #ifdef i386
1014 // print the regset
1015 print_debug("\teax = 0x%x\n", newthr->regs.r_eax);
1016 print_debug("\tebx = 0x%x\n", newthr->regs.r_ebx);
1017 print_debug("\tecx = 0x%x\n", newthr->regs.r_ecx);
1018 print_debug("\tedx = 0x%x\n", newthr->regs.r_edx);
1019 print_debug("\tesp = 0x%x\n", newthr->regs.r_esp);
1020 print_debug("\tebp = 0x%x\n", newthr->regs.r_ebp);
1021 print_debug("\tesi = 0x%x\n", newthr->regs.r_esi);
1022 print_debug("\tedi = 0x%x\n", newthr->regs.r_edi);
1077 // an Elf{32/64}_Nhdr structure describing its type and size.
1078 if ( (buf = (char*) malloc(size)) == NULL) {
1079 print_debug("can't allocate memory for reading core notes\n");
1080 goto err;
1081 }
1082
1083 // read notes into buffer
1084 if (read(ph->core->core_fd, buf, size) != size) {
1085 print_debug("failed to read notes, core file must have been truncated\n");
1086 goto err;
1087 }
1088
1089 p = buf;
1090 while (p < buf + size) {
1091 ELF_NHDR* notep = (ELF_NHDR*) p;
1092 char* descdata = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4);
1093 print_debug("Note header with n_type = %d and n_descsz = %u\n",
1094 notep->n_type, notep->n_descsz);
1095
1096 if (notep->n_type == NT_PRSTATUS) {
1097 if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) {
1098 return false;
1099 }
1100 }
1101 p = descdata + ROUNDUP(notep->n_descsz, 4);
1102 }
1103
1104 free(buf);
1105 return true;
1106
1107 err:
1108 if (buf) free(buf);
1109 return false;
1110 }
1111
1112 // read all segments from core file
1113 static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
1114 int i = 0;
1115 ELF_PHDR* phbuf = NULL;
1116 ELF_PHDR* core_php = NULL;
1117
1118 if ((phbuf = read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL)
1119 return false;
1120
1127 * Difference b/w Solaris PT_NOTE and BSD PT_NOTE:
1128 *
1129 * In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
1130 * contains /proc structs in the pre-2.6 unstructured /proc format. the last
1131 * PT_NOTE has data in new /proc format.
1132 *
1133 * In Solaris, there is only one pstatus (process status). pstatus contains
1134 * integer register set among other stuff. For each LWP, we have one lwpstatus
1135 * entry that has integer regset for that LWP.
1136 *
1137 * Linux threads are actually 'clone'd processes. To support core analysis
1138 * of "multithreaded" process, Linux creates more than one pstatus (called
1139 * "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one
1140 * "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular
1141 * function "elf_core_dump".
1142 */
1143
1144 for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) {
1145 switch (core_php->p_type) {
1146 case PT_NOTE:
1147 if (core_handle_note(ph, core_php) != true) {
1148 goto err;
1149 }
1150 break;
1151
1152 case PT_LOAD: {
1153 if (core_php->p_filesz != 0) {
1154 if (add_map_info(ph, ph->core->core_fd, core_php->p_offset,
1155 core_php->p_vaddr, core_php->p_filesz) == NULL) goto err;
1156 }
1157 break;
1158 }
1159 }
1160
1161 core_php++;
1162 }
1163
1164 free(phbuf);
1165 return true;
1166 err:
1167 free(phbuf);
1168 return false;
1169 }
1248 }
1249
1250 // from PT_DYNAMIC we want to read address of first link_map addr
1251 case PT_DYNAMIC: {
1252 ph->core->dynamic_addr = exec_php->p_vaddr;
1253 print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
1254 break;
1255 }
1256
1257 } // switch
1258 exec_php++;
1259 } // for
1260
1261 free(phbuf);
1262 return true;
1263 err:
1264 free(phbuf);
1265 return false;
1266 }
1267
1268 #define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug, r_map)
1269 #define LD_BASE_OFFSET offsetof(struct r_debug, r_ldbase)
1270 #define LINK_MAP_ADDR_OFFSET offsetof(struct link_map, l_addr)
1271 #define LINK_MAP_NAME_OFFSET offsetof(struct link_map, l_name)
1272 #define LINK_MAP_NEXT_OFFSET offsetof(struct link_map, l_next)
1273
1274 // read shared library info from runtime linker's data structures.
1275 // This work is done by librtlb_db in Solaris
1276 static bool read_shared_lib_info(struct ps_prochandle* ph) {
1277 uintptr_t addr = ph->core->dynamic_addr;
1278 uintptr_t debug_base;
1279 uintptr_t first_link_map_addr;
1280 uintptr_t ld_base_addr;
1281 uintptr_t link_map_addr;
1282 uintptr_t lib_base_diff;
1283 uintptr_t lib_base;
1284 uintptr_t lib_name_addr;
1285 char lib_name[BUF_SIZE];
1286 ELF_DYN dyn;
1287 ELF_EHDR elf_ehdr;
1296 dyn.d_tag = DT_NULL;
1297 while (dyn.d_tag != DT_DEBUG) {
1298 if (ps_pread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) {
1299 print_debug("can't read debug info from _DYNAMIC\n");
1300 return false;
1301 }
1302 addr += sizeof(ELF_DYN);
1303 }
1304
1305 // we have got Dyn entry with DT_DEBUG
1306 debug_base = dyn.d_un.d_ptr;
1307 // at debug_base we have struct r_debug. This has first link map in r_map field
1308 if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET,
1309 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) {
1310 print_debug("can't read first link map address\n");
1311 return false;
1312 }
1313
1314 // read ld_base address from struct r_debug
1315 // XXX: There is no r_ldbase member on BSD
1316 /*
1317 if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
1318 sizeof(uintptr_t)) != PS_OK) {
1319 print_debug("can't read ld base address\n");
1320 return false;
1321 }
1322 ph->core->ld_base_addr = ld_base_addr;
1323 */
1324 ph->core->ld_base_addr = 0;
1325
1326 print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
1327
1328 // now read segments from interp (i.e ld-elf.so.1)
1329 if (read_interp_segments(ph) != true)
1330 return false;
1331
1332 // after adding interpreter (ld.so) mappings sort again
1333 if (sort_map_array(ph) != true)
1334 return false;
1335
1336 print_debug("first link map is at 0x%lx\n", first_link_map_addr);
1337
1338 link_map_addr = first_link_map_addr;
1339 while (link_map_addr != 0) {
1340 // read library base address of the .so. Note that even though <sys/link.h> calls
1341 // link_map->l_addr as "base address", this is * not * really base virtual
1342 // address of the shared object. This is actually the difference b/w the virtual
1343 // address mentioned in shared object and the actual virtual base where runtime
1394 }
1395
1396 // read next link_map address
1397 if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET,
1398 &link_map_addr, sizeof(uintptr_t)) != PS_OK) {
1399 print_debug("can't read next link in link_map\n");
1400 return false;
1401 }
1402 }
1403
1404 return true;
1405 }
1406
1407 // the one and only one exposed stuff from this file
1408 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
1409 ELF_EHDR core_ehdr;
1410 ELF_EHDR exec_ehdr;
1411
1412 struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
1413 if (ph == NULL) {
1414 print_debug("cant allocate ps_prochandle\n");
1415 return NULL;
1416 }
1417
1418 if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
1419 free(ph);
1420 print_debug("can't allocate ps_prochandle\n");
1421 return NULL;
1422 }
1423
1424 // initialize ph
1425 ph->ops = &core_ops;
1426 ph->core->core_fd = -1;
1427 ph->core->exec_fd = -1;
1428 ph->core->interp_fd = -1;
1429
1430 print_debug("exec: %s core: %s", exec_file, core_file);
1431
1432 // open the core file
1433 if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
1434 print_debug("can't open core file\n");
1435 goto err;
1436 }
1437
1438 // read core file ELF header
1439 if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) {
1440 print_debug("core file is not a valid ELF ET_CORE file\n");
1441 goto err;
1442 }
1443
1444 if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
1445 print_debug("can't open executable file\n");
1446 goto err;
1447 }
1448
1449 if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) {
1450 print_debug("executable file is not a valid ELF ET_EXEC file\n");
1451 goto err;
1463 if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd,
1464 (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL)
1465 goto err;
1466
1467 // allocate and sort maps into map_array, we need to do this
1468 // here because read_shared_lib_info needs to read from debuggee
1469 // address space
1470 if (sort_map_array(ph) != true)
1471 goto err;
1472
1473 if (read_shared_lib_info(ph) != true)
1474 goto err;
1475
1476 // sort again because we have added more mappings from shared objects
1477 if (sort_map_array(ph) != true)
1478 goto err;
1479
1480 if (init_classsharing_workaround(ph) != true)
1481 goto err;
1482
1483 print_debug("Leave Pgrab_core\n");
1484 return ph;
1485
1486 err:
1487 Prelease(ph);
1488 return NULL;
1489 }
1490
1491 #endif // __APPLE__
|