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