annotate agent/src/os/linux/ps_core.c @ 6725:da91efe96a93

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