39 |
39 |
40 //---------------------------------------------------------------------- |
40 //---------------------------------------------------------------------- |
41 // ps_prochandle cleanup helper functions |
41 // ps_prochandle cleanup helper functions |
42 |
42 |
43 // close all file descriptors |
43 // close all file descriptors |
44 static void close_elf_files(struct ps_prochandle* ph) { |
44 static void close_files(struct ps_prochandle* ph) { |
45 lib_info* lib = NULL; |
45 lib_info* lib = NULL; |
46 |
46 |
47 // close core file descriptor |
47 // close core file descriptor |
48 if (ph->core->core_fd >= 0) |
48 if (ph->core->core_fd >= 0) |
49 close(ph->core->core_fd); |
49 close(ph->core->core_fd); |
50 |
50 |
51 // close exec file descriptor |
51 // close exec file descriptor |
52 if (ph->core->exec_fd >= 0) |
52 if (ph->core->exec_fd >= 0) |
53 close(ph->core->exec_fd); |
53 close(ph->core->exec_fd); |
54 |
54 |
55 // close interp file descriptor |
55 // close interp file descriptor |
56 if (ph->core->interp_fd >= 0) |
56 if (ph->core->interp_fd >= 0) |
57 close(ph->core->interp_fd); |
57 close(ph->core->interp_fd); |
58 |
58 |
59 // close class share archive file |
59 // close class share archive file |
60 if (ph->core->classes_jsa_fd >= 0) |
60 if (ph->core->classes_jsa_fd >= 0) |
61 close(ph->core->classes_jsa_fd); |
61 close(ph->core->classes_jsa_fd); |
62 |
62 |
63 // close all library file descriptors |
63 // close all library file descriptors |
64 lib = ph->libs; |
64 lib = ph->libs; |
65 while (lib) { |
65 while (lib) { |
66 int fd = lib->fd; |
66 int fd = lib->fd; |
67 if (fd >= 0 && fd != ph->core->exec_fd) close(fd); |
67 if (fd >= 0 && fd != ph->core->exec_fd) { |
68 lib = lib->next; |
68 close(fd); |
69 } |
69 } |
|
70 lib = lib->next; |
|
71 } |
70 } |
72 } |
71 |
73 |
72 // clean all map_info stuff |
74 // clean all map_info stuff |
73 static void destroy_map_info(struct ps_prochandle* ph) { |
75 static void destroy_map_info(struct ps_prochandle* ph) { |
74 map_info* map = ph->core->maps; |
76 map_info* map = ph->core->maps; |
75 while (map) { |
77 while (map) { |
76 map_info* next = map->next; |
78 map_info* next = map->next; |
77 free(map); |
79 free(map); |
78 map = next; |
80 map = next; |
79 } |
81 } |
80 |
82 |
81 if (ph->core->map_array) { |
83 if (ph->core->map_array) { |
82 free(ph->core->map_array); |
84 free(ph->core->map_array); |
83 } |
85 } |
84 |
86 |
85 // Part of the class sharing workaround |
87 // Part of the class sharing workaround |
86 map = ph->core->class_share_maps; |
88 map = ph->core->class_share_maps; |
87 while (map) { |
89 while (map) { |
88 map_info* next = map->next; |
90 map_info* next = map->next; |
89 free(map); |
91 free(map); |
90 map = next; |
92 map = next; |
91 } |
93 } |
92 } |
94 } |
93 |
95 |
94 // ps_prochandle operations |
96 // ps_prochandle operations |
95 static void core_release(struct ps_prochandle* ph) { |
97 static void core_release(struct ps_prochandle* ph) { |
96 if (ph->core) { |
98 if (ph->core) { |
97 close_elf_files(ph); |
99 close_files(ph); |
98 destroy_map_info(ph); |
100 destroy_map_info(ph); |
99 free(ph->core); |
101 free(ph->core); |
100 } |
102 } |
101 } |
103 } |
102 |
104 |
103 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { |
105 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) { |
104 map_info* map; |
106 map_info* map; |
105 if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { |
107 if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) { |
106 print_debug("can't allocate memory for map_info\n"); |
108 print_debug("can't allocate memory for map_info\n"); |
107 return NULL; |
109 return NULL; |
108 } |
110 } |
109 |
111 |
110 // initialize map |
112 // initialize map |
111 map->fd = fd; |
113 map->fd = fd; |
112 map->offset = offset; |
114 map->offset = offset; |
113 map->vaddr = vaddr; |
115 map->vaddr = vaddr; |
114 map->memsz = memsz; |
116 map->memsz = memsz; |
115 return map; |
117 return map; |
116 } |
118 } |
117 |
119 |
118 // add map info with given fd, offset, vaddr and memsz |
120 // add map info with given fd, offset, vaddr and memsz |
119 static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, |
121 static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset, |
120 uintptr_t vaddr, size_t memsz) { |
122 uintptr_t vaddr, size_t memsz) { |
121 map_info* map; |
123 map_info* map; |
122 if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { |
124 if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) { |
123 return NULL; |
125 return NULL; |
124 } |
126 } |
125 |
127 |
126 // add this to map list |
128 // add this to map list |
127 map->next = ph->core->maps; |
129 map->next = ph->core->maps; |
128 ph->core->maps = map; |
130 ph->core->maps = map; |
129 ph->core->num_maps++; |
131 ph->core->num_maps++; |
130 |
132 |
131 return map; |
133 return map; |
132 } |
134 } |
133 |
135 |
134 // Part of the class sharing workaround |
136 // Part of the class sharing workaround |
135 static void add_class_share_map_info(struct ps_prochandle* ph, off_t offset, |
137 static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset, |
136 uintptr_t vaddr, size_t memsz) { |
138 uintptr_t vaddr, size_t memsz) { |
137 map_info* map; |
139 map_info* map; |
138 if ((map = allocate_init_map(ph->core->classes_jsa_fd, |
140 if ((map = allocate_init_map(ph->core->classes_jsa_fd, |
139 offset, vaddr, memsz)) == NULL) { |
141 offset, vaddr, memsz)) == NULL) { |
140 return; |
142 return NULL; |
141 } |
143 } |
142 |
144 |
143 map->next = ph->core->class_share_maps; |
145 map->next = ph->core->class_share_maps; |
144 ph->core->class_share_maps = map; |
146 ph->core->class_share_maps = map; |
|
147 return map; |
145 } |
148 } |
146 |
149 |
147 // Return the map_info for the given virtual address. We keep a sorted |
150 // Return the map_info for the given virtual address. We keep a sorted |
148 // array of pointers in ph->map_array, so we can binary search. |
151 // array of pointers in ph->map_array, so we can binary search. |
149 static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) |
152 static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) { |
150 { |
153 int mid, lo = 0, hi = ph->core->num_maps - 1; |
151 int mid, lo = 0, hi = ph->core->num_maps - 1; |
154 map_info *mp; |
152 map_info *mp; |
155 |
153 |
156 while (hi - lo > 1) { |
154 while (hi - lo > 1) { |
157 mid = (lo + hi) / 2; |
155 mid = (lo + hi) / 2; |
158 if (addr >= ph->core->map_array[mid]->vaddr) { |
156 if (addr >= ph->core->map_array[mid]->vaddr) |
159 lo = mid; |
157 lo = mid; |
160 } else { |
158 else |
161 hi = mid; |
159 hi = mid; |
162 } |
160 } |
163 } |
161 |
164 |
162 if (addr < ph->core->map_array[hi]->vaddr) |
165 if (addr < ph->core->map_array[hi]->vaddr) { |
163 mp = ph->core->map_array[lo]; |
166 mp = ph->core->map_array[lo]; |
164 else |
167 } else { |
165 mp = ph->core->map_array[hi]; |
168 mp = ph->core->map_array[hi]; |
166 |
169 } |
167 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) |
170 |
|
171 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { |
|
172 return (mp); |
|
173 } |
|
174 |
|
175 |
|
176 // Part of the class sharing workaround |
|
177 // Unfortunately, we have no way of detecting -Xshare state. |
|
178 // Check out the share maps atlast, if we don't find anywhere. |
|
179 // This is done this way so to avoid reading share pages |
|
180 // ahead of other normal maps. For eg. with -Xshare:off we don't |
|
181 // want to prefer class sharing data to data from core. |
|
182 mp = ph->core->class_share_maps; |
|
183 if (mp) { |
|
184 print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr); |
|
185 } |
|
186 while (mp) { |
|
187 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { |
|
188 print_debug("located map_info at 0x%lx from class share maps\n", addr); |
168 return (mp); |
189 return (mp); |
169 |
190 } |
170 |
191 mp = mp->next; |
171 // Part of the class sharing workaround |
192 } |
172 // Unfortunately, we have no way of detecting -Xshare state. |
193 |
173 // Check out the share maps atlast, if we don't find anywhere. |
194 print_debug("can't locate map_info at 0x%lx\n", addr); |
174 // This is done this way so to avoid reading share pages |
195 return (NULL); |
175 // ahead of other normal maps. For eg. with -Xshare:off we don't |
|
176 // want to prefer class sharing data to data from core. |
|
177 mp = ph->core->class_share_maps; |
|
178 if (mp) { |
|
179 print_debug("can't locate map_info at 0x%lx, trying class share maps\n", |
|
180 addr); |
|
181 } |
|
182 while (mp) { |
|
183 if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) { |
|
184 print_debug("located map_info at 0x%lx from class share maps\n", |
|
185 addr); |
|
186 return (mp); |
|
187 } |
|
188 mp = mp->next; |
|
189 } |
|
190 |
|
191 print_debug("can't locate map_info at 0x%lx\n", addr); |
|
192 return (NULL); |
|
193 } |
196 } |
194 |
197 |
195 //--------------------------------------------------------------- |
198 //--------------------------------------------------------------- |
196 // Part of the class sharing workaround: |
199 // Part of the class sharing workaround: |
197 // |
200 // |
224 char* _base; // copy-on-write base address |
227 char* _base; // copy-on-write base address |
225 size_t _capacity; // for validity checking |
228 size_t _capacity; // for validity checking |
226 size_t _used; // for setting space top on read |
229 size_t _used; // for setting space top on read |
227 |
230 |
228 // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with |
231 // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with |
229 // the C type matching the C++ bool type on any given platform. For |
232 // the C type matching the C++ bool type on any given platform. |
230 // Hotspot on Linux we assume the corresponding C type is char but |
233 // We assume the corresponding C type is char but licensees |
231 // licensees on Linux versions may need to adjust the type of these fields. |
234 // may need to adjust the type of these fields. |
232 char _read_only; // read only space? |
235 char _read_only; // read only space? |
233 char _allow_exec; // executable code in space? |
236 char _allow_exec; // executable code in space? |
234 |
237 |
235 } _space[NUM_SHARED_MAPS]; |
238 } _space[NUM_SHARED_MAPS]; |
236 |
239 |
237 // Ignore the rest of the FileMapHeader. We don't need those fields here. |
240 // Ignore the rest of the FileMapHeader. We don't need those fields here. |
238 }; |
241 }; |
239 |
242 |
240 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) { |
243 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) { |
241 jboolean i; |
244 jboolean i; |
242 if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { |
245 if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) { |
243 *pvalue = i; |
246 *pvalue = i; |
244 return true; |
247 return true; |
245 } else { |
248 } else { |
246 return false; |
249 return false; |
247 } |
250 } |
248 } |
251 } |
249 |
252 |
250 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { |
253 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) { |
251 uintptr_t uip; |
254 uintptr_t uip; |
252 if (ps_pdread(ph, (psaddr_t) addr, &uip, sizeof(uip)) == PS_OK) { |
255 if (ps_pdread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) { |
253 *pvalue = uip; |
256 *pvalue = uip; |
254 return true; |
257 return true; |
255 } else { |
258 } else { |
256 return false; |
259 return false; |
257 } |
260 } |
258 } |
261 } |
259 |
262 |
260 // used to read strings from debuggee |
263 // used to read strings from debuggee |
261 static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { |
264 static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) { |
262 size_t i = 0; |
265 size_t i = 0; |
263 char c = ' '; |
266 char c = ' '; |
264 |
267 |
265 while (c != '\0') { |
268 while (c != '\0') { |
266 if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) |
269 if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) { |
267 return false; |
270 return false; |
268 if (i < size - 1) |
271 } |
269 buf[i] = c; |
272 if (i < size - 1) { |
270 else // smaller buffer |
273 buf[i] = c; |
271 return false; |
274 } else { |
272 i++; addr++; |
275 // smaller buffer |
273 } |
276 return false; |
274 |
277 } |
275 buf[i] = '\0'; |
278 i++; addr++; |
276 return true; |
279 } |
|
280 |
|
281 buf[i] = '\0'; |
|
282 return true; |
277 } |
283 } |
278 |
284 |
279 #define USE_SHARED_SPACES_SYM "UseSharedSpaces" |
285 #define USE_SHARED_SPACES_SYM "UseSharedSpaces" |
280 // mangled name of Arguments::SharedArchivePath |
286 // mangled name of Arguments::SharedArchivePath |
281 #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" |
287 #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE" |
|
288 #define LIBJVM_NAME "/libjvm.so" |
282 |
289 |
283 static bool init_classsharing_workaround(struct ps_prochandle* ph) { |
290 static bool init_classsharing_workaround(struct ps_prochandle* ph) { |
284 lib_info* lib = ph->libs; |
291 lib_info* lib = ph->libs; |
285 while (lib != NULL) { |
292 while (lib != NULL) { |
286 // we are iterating over shared objects from the core dump. look for |
293 // we are iterating over shared objects from the core dump. look for |
287 // libjvm.so. |
294 // libjvm.so. |
288 const char *jvm_name = 0; |
295 const char *jvm_name = 0; |
289 if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0) { |
296 if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) { |
290 char classes_jsa[PATH_MAX]; |
297 char classes_jsa[PATH_MAX]; |
291 struct FileMapHeader header; |
298 struct FileMapHeader header; |
292 size_t n = 0; |
299 int fd = -1; |
293 int fd = -1, m = 0; |
300 int m = 0; |
294 uintptr_t base = 0, useSharedSpacesAddr = 0; |
301 size_t n = 0; |
295 uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; |
302 uintptr_t base = 0, useSharedSpacesAddr = 0; |
296 jboolean useSharedSpaces = 0; |
303 uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0; |
297 map_info* mi = 0; |
304 jboolean useSharedSpaces = 0; |
298 |
305 map_info* mi = 0; |
299 memset(classes_jsa, 0, sizeof(classes_jsa)); |
306 |
300 jvm_name = lib->name; |
307 memset(classes_jsa, 0, sizeof(classes_jsa)); |
301 useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); |
308 jvm_name = lib->name; |
302 if (useSharedSpacesAddr == 0) { |
309 useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM); |
303 print_debug("can't lookup 'UseSharedSpaces' flag\n"); |
310 if (useSharedSpacesAddr == 0) { |
304 return false; |
311 print_debug("can't lookup 'UseSharedSpaces' flag\n"); |
305 } |
312 return false; |
306 |
313 } |
307 // Hotspot vm types are not exported to build this library. So |
314 |
308 // using equivalent type jboolean to read the value of |
315 // Hotspot vm types are not exported to build this library. So |
309 // UseSharedSpaces which is same as hotspot type "bool". |
316 // using equivalent type jboolean to read the value of |
310 if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { |
317 // UseSharedSpaces which is same as hotspot type "bool". |
311 print_debug("can't read the value of 'UseSharedSpaces' flag\n"); |
318 if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) { |
312 return false; |
319 print_debug("can't read the value of 'UseSharedSpaces' flag\n"); |
313 } |
320 return false; |
314 |
321 } |
315 if ((int)useSharedSpaces == 0) { |
322 |
316 print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); |
323 if ((int)useSharedSpaces == 0) { |
317 return true; |
324 print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n"); |
318 } |
325 return true; |
319 |
326 } |
320 sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); |
327 |
321 if (sharedArchivePathAddrAddr == 0) { |
328 sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM); |
322 print_debug("can't lookup shared archive path symbol\n"); |
329 if (sharedArchivePathAddrAddr == 0) { |
323 return false; |
330 print_debug("can't lookup shared archive path symbol\n"); |
324 } |
331 return false; |
325 |
332 } |
326 if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { |
333 |
327 print_debug("can't read shared archive path pointer\n"); |
334 if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) { |
328 return false; |
335 print_debug("can't read shared archive path pointer\n"); |
329 } |
336 return false; |
330 |
337 } |
331 if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { |
338 |
332 print_debug("can't read shared archive path value\n"); |
339 if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) { |
333 return false; |
340 print_debug("can't read shared archive path value\n"); |
334 } |
341 return false; |
335 |
342 } |
336 print_debug("looking for %s\n", classes_jsa); |
343 |
337 // open the class sharing archive file |
344 print_debug("looking for %s\n", classes_jsa); |
338 fd = pathmap_open(classes_jsa); |
345 // open the class sharing archive file |
339 if (fd < 0) { |
346 fd = pathmap_open(classes_jsa); |
340 print_debug("can't open %s!\n", classes_jsa); |
347 if (fd < 0) { |
341 ph->core->classes_jsa_fd = -1; |
348 print_debug("can't open %s!\n", classes_jsa); |
342 return false; |
349 ph->core->classes_jsa_fd = -1; |
343 } else { |
350 return false; |
344 print_debug("opened %s\n", classes_jsa); |
351 } else { |
345 } |
352 print_debug("opened %s\n", classes_jsa); |
346 |
353 } |
347 // read FileMapHeader from the file |
354 |
348 memset(&header, 0, sizeof(struct FileMapHeader)); |
355 // read FileMapHeader from the file |
349 if ((n = read(fd, &header, sizeof(struct FileMapHeader))) |
356 memset(&header, 0, sizeof(struct FileMapHeader)); |
350 != sizeof(struct FileMapHeader)) { |
357 if ((n = read(fd, &header, sizeof(struct FileMapHeader))) |
351 print_debug("can't read shared archive file map header from %s\n", classes_jsa); |
358 != sizeof(struct FileMapHeader)) { |
352 close(fd); |
359 print_debug("can't read shared archive file map header from %s\n", classes_jsa); |
353 return false; |
360 close(fd); |
354 } |
361 return false; |
355 |
362 } |
356 // check file magic |
363 |
357 if (header._magic != 0xf00baba2) { |
364 // check file magic |
358 print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n", |
365 if (header._magic != 0xf00baba2) { |
359 classes_jsa, header._magic); |
366 print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n", |
360 close(fd); |
367 classes_jsa, header._magic); |
361 return false; |
368 close(fd); |
362 } |
369 return false; |
363 |
370 } |
364 // check version |
371 |
365 if (header._version != CURRENT_ARCHIVE_VERSION) { |
372 // check version |
366 print_debug("%s has wrong shared archive file version %d, expecting %d\n", |
373 if (header._version != CURRENT_ARCHIVE_VERSION) { |
367 classes_jsa, header._version, CURRENT_ARCHIVE_VERSION); |
374 print_debug("%s has wrong shared archive file version %d, expecting %d\n", |
368 close(fd); |
375 classes_jsa, header._version, CURRENT_ARCHIVE_VERSION); |
369 return false; |
376 close(fd); |
370 } |
377 return false; |
371 |
378 } |
372 ph->core->classes_jsa_fd = fd; |
379 |
373 // add read-only maps from classes.jsa to the list of maps |
380 ph->core->classes_jsa_fd = fd; |
374 for (m = 0; m < NUM_SHARED_MAPS; m++) { |
381 // add read-only maps from classes.jsa to the list of maps |
375 if (header._space[m]._read_only) { |
382 for (m = 0; m < NUM_SHARED_MAPS; m++) { |
376 base = (uintptr_t) header._space[m]._base; |
383 if (header._space[m]._read_only) { |
377 // no need to worry about the fractional pages at-the-end. |
384 base = (uintptr_t) header._space[m]._base; |
378 // possible fractional pages are handled by core_read_data. |
385 // no need to worry about the fractional pages at-the-end. |
379 add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, |
386 // possible fractional pages are handled by core_read_data. |
380 base, (size_t) header._space[m]._used); |
387 add_class_share_map_info(ph, (off_t) header._space[m]._file_offset, |
381 print_debug("added a share archive map at 0x%lx\n", base); |
388 base, (size_t) header._space[m]._used); |
382 } |
389 print_debug("added a share archive map at 0x%lx\n", base); |
383 } |
390 } |
384 return true; |
391 } |
385 } |
392 return true; |
386 lib = lib->next; |
393 } |
387 } |
394 lib = lib->next; |
388 return true; |
395 } |
|
396 return true; |
389 } |
397 } |
390 |
398 |
391 |
399 |
392 //--------------------------------------------------------------------------- |
400 //--------------------------------------------------------------------------- |
393 // functions to handle map_info |
401 // functions to handle map_info |
394 |
402 |
395 // Order mappings based on virtual address. We use this function as the |
403 // Order mappings based on virtual address. We use this function as the |
396 // callback for sorting the array of map_info pointers. |
404 // callback for sorting the array of map_info pointers. |
397 static int core_cmp_mapping(const void *lhsp, const void *rhsp) |
405 static int core_cmp_mapping(const void *lhsp, const void *rhsp) |
398 { |
406 { |
399 const map_info *lhs = *((const map_info **)lhsp); |
407 const map_info *lhs = *((const map_info **)lhsp); |
400 const map_info *rhs = *((const map_info **)rhsp); |
408 const map_info *rhs = *((const map_info **)rhsp); |
401 |
409 |
402 if (lhs->vaddr == rhs->vaddr) |
410 if (lhs->vaddr == rhs->vaddr) { |
403 return (0); |
411 return (0); |
404 |
412 } |
405 return (lhs->vaddr < rhs->vaddr ? -1 : 1); |
413 |
|
414 return (lhs->vaddr < rhs->vaddr ? -1 : 1); |
406 } |
415 } |
407 |
416 |
408 // we sort map_info by starting virtual address so that we can do |
417 // we sort map_info by starting virtual address so that we can do |
409 // binary search to read from an address. |
418 // binary search to read from an address. |
410 static bool sort_map_array(struct ps_prochandle* ph) { |
419 static bool sort_map_array(struct ps_prochandle* ph) { |
411 size_t num_maps = ph->core->num_maps; |
420 size_t num_maps = ph->core->num_maps; |
412 map_info* map = ph->core->maps; |
421 map_info* map = ph->core->maps; |
413 int i = 0; |
422 int i = 0; |
414 |
423 |
415 // allocate map_array |
424 // allocate map_array |
416 map_info** array; |
425 map_info** array; |
417 if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { |
426 if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) { |
418 print_debug("can't allocate memory for map array\n"); |
427 print_debug("can't allocate memory for map array\n"); |
419 return false; |
428 return false; |
420 } |
429 } |
421 |
430 |
422 // add maps to array |
431 // add maps to array |
423 while (map) { |
432 while (map) { |
424 array[i] = map; |
433 array[i] = map; |
425 i++; |
434 i++; |
426 map = map->next; |
435 map = map->next; |
427 } |
436 } |
428 |
437 |
429 // sort is called twice. If this is second time, clear map array |
438 // sort is called twice. If this is second time, clear map array |
430 if (ph->core->map_array) free(ph->core->map_array); |
439 if (ph->core->map_array) { |
431 ph->core->map_array = array; |
440 free(ph->core->map_array); |
432 // sort the map_info array by base virtual address. |
441 } |
433 qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), |
442 |
434 core_cmp_mapping); |
443 ph->core->map_array = array; |
435 |
444 // sort the map_info array by base virtual address. |
436 // print map |
445 qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*), |
437 if (is_debug()) { |
446 core_cmp_mapping); |
438 int j = 0; |
447 |
439 print_debug("---- sorted virtual address map ----\n"); |
448 // print map |
440 for (j = 0; j < ph->core->num_maps; j++) { |
449 if (is_debug()) { |
441 print_debug("base = 0x%lx\tsize = %zu\n", ph->core->map_array[j]->vaddr, |
450 int j = 0; |
442 ph->core->map_array[j]->memsz); |
451 print_debug("---- sorted virtual address map ----\n"); |
443 } |
452 for (j = 0; j < ph->core->num_maps; j++) { |
444 } |
453 print_debug("base = 0x%lx\tsize = %zu\n", ph->core->map_array[j]->vaddr, |
445 |
454 ph->core->map_array[j]->memsz); |
446 return true; |
455 } |
|
456 } |
|
457 |
|
458 return true; |
447 } |
459 } |
448 |
460 |
449 #ifndef MIN |
461 #ifndef MIN |
450 #define MIN(x, y) (((x) < (y))? (x): (y)) |
462 #define MIN(x, y) (((x) < (y))? (x): (y)) |
451 #endif |
463 #endif |
830 #define LINK_MAP_NEXT_OFFSET offsetof(struct link_map, l_next) |
847 #define LINK_MAP_NEXT_OFFSET offsetof(struct link_map, l_next) |
831 |
848 |
832 // read shared library info from runtime linker's data structures. |
849 // read shared library info from runtime linker's data structures. |
833 // This work is done by librtlb_db in Solaris |
850 // This work is done by librtlb_db in Solaris |
834 static bool read_shared_lib_info(struct ps_prochandle* ph) { |
851 static bool read_shared_lib_info(struct ps_prochandle* ph) { |
835 uintptr_t addr = ph->core->dynamic_addr; |
852 uintptr_t addr = ph->core->dynamic_addr; |
836 uintptr_t debug_base; |
853 uintptr_t debug_base; |
837 uintptr_t first_link_map_addr; |
854 uintptr_t first_link_map_addr; |
838 uintptr_t ld_base_addr; |
855 uintptr_t ld_base_addr; |
839 uintptr_t link_map_addr; |
856 uintptr_t link_map_addr; |
840 uintptr_t lib_base_diff; |
857 uintptr_t lib_base_diff; |
841 uintptr_t lib_base; |
858 uintptr_t lib_base; |
842 uintptr_t lib_name_addr; |
859 uintptr_t lib_name_addr; |
843 char lib_name[BUF_SIZE]; |
860 char lib_name[BUF_SIZE]; |
844 ELF_DYN dyn; |
861 ELF_DYN dyn; |
845 ELF_EHDR elf_ehdr; |
862 ELF_EHDR elf_ehdr; |
846 int lib_fd; |
863 int lib_fd; |
847 |
864 |
848 // _DYNAMIC has information of the form |
865 // _DYNAMIC has information of the form |
849 // [tag] [data] [tag] [data] ..... |
866 // [tag] [data] [tag] [data] ..... |
850 // Both tag and data are pointer sized. |
867 // Both tag and data are pointer sized. |
851 // We look for dynamic info with DT_DEBUG. This has shared object info. |
868 // We look for dynamic info with DT_DEBUG. This has shared object info. |
852 // refer to struct r_debug in link.h |
869 // refer to struct r_debug in link.h |
853 |
870 |
854 dyn.d_tag = DT_NULL; |
871 dyn.d_tag = DT_NULL; |
855 while (dyn.d_tag != DT_DEBUG) { |
872 while (dyn.d_tag != DT_DEBUG) { |
856 if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { |
873 if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) { |
857 print_debug("can't read debug info from _DYNAMIC\n"); |
874 print_debug("can't read debug info from _DYNAMIC\n"); |
858 return false; |
875 return false; |
859 } |
876 } |
860 addr += sizeof(ELF_DYN); |
877 addr += sizeof(ELF_DYN); |
861 } |
878 } |
862 |
879 |
863 // we have got Dyn entry with DT_DEBUG |
880 // we have got Dyn entry with DT_DEBUG |
864 debug_base = dyn.d_un.d_ptr; |
881 debug_base = dyn.d_un.d_ptr; |
865 // at debug_base we have struct r_debug. This has first link map in r_map field |
882 // at debug_base we have struct r_debug. This has first link map in r_map field |
866 if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, |
883 if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET, |
867 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { |
884 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) { |
868 print_debug("can't read first link map address\n"); |
885 print_debug("can't read first link map address\n"); |
|
886 return false; |
|
887 } |
|
888 |
|
889 // read ld_base address from struct r_debug |
|
890 if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, |
|
891 sizeof(uintptr_t)) != PS_OK) { |
|
892 print_debug("can't read ld base address\n"); |
|
893 return false; |
|
894 } |
|
895 ph->core->ld_base_addr = ld_base_addr; |
|
896 |
|
897 print_debug("interpreter base address is 0x%lx\n", ld_base_addr); |
|
898 |
|
899 // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so) |
|
900 if (read_interp_segments(ph) != true) { |
869 return false; |
901 return false; |
870 } |
902 } |
871 |
903 |
872 // read ld_base address from struct r_debug |
904 // after adding interpreter (ld.so) mappings sort again |
873 if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr, |
905 if (sort_map_array(ph) != true) { |
874 sizeof(uintptr_t)) != PS_OK) { |
906 return false; |
875 print_debug("can't read ld base address\n"); |
907 } |
876 return false; |
|
877 } |
|
878 ph->core->ld_base_addr = ld_base_addr; |
|
879 |
|
880 print_debug("interpreter base address is 0x%lx\n", ld_base_addr); |
|
881 |
|
882 // now read segments from interp (i.e ld.so or ld-linux.so) |
|
883 if (read_interp_segments(ph) != true) |
|
884 return false; |
|
885 |
|
886 // after adding interpreter (ld.so) mappings sort again |
|
887 if (sort_map_array(ph) != true) |
|
888 return false; |
|
889 |
908 |
890 print_debug("first link map is at 0x%lx\n", first_link_map_addr); |
909 print_debug("first link map is at 0x%lx\n", first_link_map_addr); |
891 |
910 |
892 link_map_addr = first_link_map_addr; |
911 link_map_addr = first_link_map_addr; |
893 while (link_map_addr != 0) { |
912 while (link_map_addr != 0) { |
948 // continue with other libraries... |
967 // continue with other libraries... |
949 } |
968 } |
950 } |
969 } |
951 } |
970 } |
952 |
971 |
953 // read next link_map address |
972 // read next link_map address |
954 if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, |
973 if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET, |
955 &link_map_addr, sizeof(uintptr_t)) != PS_OK) { |
974 &link_map_addr, sizeof(uintptr_t)) != PS_OK) { |
956 print_debug("can't read next link in link_map\n"); |
975 print_debug("can't read next link in link_map\n"); |
957 return false; |
976 return false; |
958 } |
977 } |
959 } |
978 } |
960 |
979 |
961 return true; |
980 return true; |
962 } |
981 } |
963 |
982 |
964 // the one and only one exposed stuff from this file |
983 // the one and only one exposed stuff from this file |
965 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { |
984 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) { |
966 ELF_EHDR core_ehdr; |
985 ELF_EHDR core_ehdr; |
967 ELF_EHDR exec_ehdr; |
986 ELF_EHDR exec_ehdr; |
968 ELF_EHDR lib_ehdr; |
987 ELF_EHDR lib_ehdr; |
969 |
988 |
970 struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); |
989 struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle)); |
971 if (ph == NULL) { |
990 if (ph == NULL) { |
972 print_debug("can't allocate ps_prochandle\n"); |
991 print_debug("can't allocate ps_prochandle\n"); |
973 return NULL; |
992 return NULL; |
974 } |
993 } |
975 |
994 |
976 if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { |
995 if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) { |
977 free(ph); |
996 free(ph); |
978 print_debug("can't allocate ps_prochandle\n"); |
997 print_debug("can't allocate ps_prochandle\n"); |
979 return NULL; |
998 return NULL; |
980 } |
999 } |
981 |
1000 |
982 // initialize ph |
1001 // initialize ph |
983 ph->ops = &core_ops; |
1002 ph->ops = &core_ops; |
984 ph->core->core_fd = -1; |
1003 ph->core->core_fd = -1; |
985 ph->core->exec_fd = -1; |
1004 ph->core->exec_fd = -1; |
986 ph->core->interp_fd = -1; |
1005 ph->core->interp_fd = -1; |
987 |
1006 |
988 // open the core file |
1007 // open the core file |
989 if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { |
1008 if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) { |
990 print_debug("can't open core file\n"); |
1009 print_debug("can't open core file\n"); |
991 goto err; |
1010 goto err; |
992 } |
1011 } |
993 |
1012 |
994 // read core file ELF header |
1013 // read core file ELF header |
995 if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { |
1014 if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) { |
996 print_debug("core file is not a valid ELF ET_CORE file\n"); |
1015 print_debug("core file is not a valid ELF ET_CORE file\n"); |
997 goto err; |
1016 goto err; |
998 } |
1017 } |
999 |
1018 |
1000 if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { |
1019 if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) { |
1001 print_debug("can't open executable file\n"); |
1020 print_debug("can't open executable file\n"); |
1002 goto err; |
1021 goto err; |
1003 } |
1022 } |
1004 |
1023 |
1005 if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { |
1024 if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) { |
1006 print_debug("executable file is not a valid ELF ET_EXEC file\n"); |
1025 print_debug("executable file is not a valid ELF ET_EXEC file\n"); |
1007 goto err; |
1026 goto err; |
1008 } |
1027 } |
1009 |
1028 |
1010 // process core file segments |
1029 // process core file segments |
1011 if (read_core_segments(ph, &core_ehdr) != true) |
1030 if (read_core_segments(ph, &core_ehdr) != true) { |
1012 goto err; |
1031 goto err; |
1013 |
1032 } |
1014 // process exec file segments |
1033 |
1015 if (read_exec_segments(ph, &exec_ehdr) != true) |
1034 // process exec file segments |
1016 goto err; |
1035 if (read_exec_segments(ph, &exec_ehdr) != true) { |
1017 |
1036 goto err; |
1018 // exec file is also treated like a shared object for symbol search |
1037 } |
1019 if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, |
1038 |
1020 (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) |
1039 // exec file is also treated like a shared object for symbol search |
1021 goto err; |
1040 if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd, |
1022 |
1041 (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) { |
1023 // allocate and sort maps into map_array, we need to do this |
1042 goto err; |
1024 // here because read_shared_lib_info needs to read from debuggee |
1043 } |
1025 // address space |
1044 |
1026 if (sort_map_array(ph) != true) |
1045 // allocate and sort maps into map_array, we need to do this |
1027 goto err; |
1046 // here because read_shared_lib_info needs to read from debuggee |
1028 |
1047 // address space |
1029 if (read_shared_lib_info(ph) != true) |
1048 if (sort_map_array(ph) != true) { |
1030 goto err; |
1049 goto err; |
1031 |
1050 } |
1032 // sort again because we have added more mappings from shared objects |
1051 |
1033 if (sort_map_array(ph) != true) |
1052 if (read_shared_lib_info(ph) != true) { |
1034 goto err; |
1053 goto err; |
1035 |
1054 } |
1036 if (init_classsharing_workaround(ph) != true) |
1055 |
1037 goto err; |
1056 // sort again because we have added more mappings from shared objects |
1038 |
1057 if (sort_map_array(ph) != true) { |
1039 return ph; |
1058 goto err; |
|
1059 } |
|
1060 |
|
1061 if (init_classsharing_workaround(ph) != true) { |
|
1062 goto err; |
|
1063 } |
|
1064 |
|
1065 return ph; |
1040 |
1066 |
1041 err: |
1067 err: |
1042 Prelease(ph); |
1068 Prelease(ph); |
1043 return NULL; |
1069 return NULL; |
1044 } |
1070 } |