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