author | ysuenaga |
Fri, 27 Jul 2018 00:54:39 +0900 | |
changeset 51203 | 220c9188db4f |
parent 49440 | 396ea30afbd5 |
child 53004 | b9d34a97a4be |
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:
670
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
670
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:
670
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
25 |
#include <stdio.h> |
|
26 |
#include <stdlib.h> |
|
27 |
#include <string.h> |
|
15734 | 28 |
#include <signal.h> |
1 | 29 |
#include <errno.h> |
28924 | 30 |
#include <elf.h> |
51203
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
31 |
#include <dirent.h> |
20295 | 32 |
#include <sys/types.h> |
33 |
#include <sys/wait.h> |
|
1 | 34 |
#include <sys/ptrace.h> |
28924 | 35 |
#include <sys/uio.h> |
1 | 36 |
#include "libproc_impl.h" |
37 |
||
38 |
#if defined(x86_64) && !defined(amd64) |
|
39 |
#define amd64 1 |
|
40 |
#endif |
|
41 |
||
42 |
#ifndef __WALL |
|
43 |
#define __WALL 0x40000000 // Copied from /usr/include/linux/wait.h |
|
44 |
#endif |
|
45 |
||
46 |
// This file has the libproc implementation specific to live process |
|
47 |
// For core files, refer to ps_core.c |
|
48 |
||
49 |
static inline uintptr_t align(uintptr_t ptr, size_t size) { |
|
50 |
return (ptr & ~(size - 1)); |
|
51 |
} |
|
52 |
||
53 |
// --------------------------------------------- |
|
54 |
// ptrace functions |
|
55 |
// --------------------------------------------- |
|
56 |
||
57 |
// read "size" bytes of data from "addr" within the target process. |
|
58 |
// unlike the standard ptrace() function, process_read_data() can handle |
|
59 |
// unaligned address - alignment check, if required, should be done |
|
60 |
// before calling process_read_data. |
|
61 |
||
62 |
static bool process_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) { |
|
63 |
long rslt; |
|
64 |
size_t i, words; |
|
65 |
uintptr_t end_addr = addr + size; |
|
66 |
uintptr_t aligned_addr = align(addr, sizeof(long)); |
|
67 |
||
68 |
if (aligned_addr != addr) { |
|
69 |
char *ptr = (char *)&rslt; |
|
70 |
errno = 0; |
|
71 |
rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0); |
|
72 |
if (errno) { |
|
73 |
print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr); |
|
74 |
return false; |
|
75 |
} |
|
76 |
for (; aligned_addr != addr; aligned_addr++, ptr++); |
|
77 |
for (; ((intptr_t)aligned_addr % sizeof(long)) && aligned_addr < end_addr; |
|
78 |
aligned_addr++) |
|
79 |
*(buf++) = *(ptr++); |
|
80 |
} |
|
81 |
||
82 |
words = (end_addr - aligned_addr) / sizeof(long); |
|
83 |
||
84 |
// assert((intptr_t)aligned_addr % sizeof(long) == 0); |
|
85 |
for (i = 0; i < words; i++) { |
|
86 |
errno = 0; |
|
87 |
rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0); |
|
88 |
if (errno) { |
|
89 |
print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr); |
|
90 |
return false; |
|
91 |
} |
|
92 |
*(long *)buf = rslt; |
|
93 |
buf += sizeof(long); |
|
94 |
aligned_addr += sizeof(long); |
|
95 |
} |
|
96 |
||
97 |
if (aligned_addr != end_addr) { |
|
98 |
char *ptr = (char *)&rslt; |
|
99 |
errno = 0; |
|
100 |
rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0); |
|
101 |
if (errno) { |
|
102 |
print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr); |
|
103 |
return false; |
|
104 |
} |
|
105 |
for (; aligned_addr != end_addr; aligned_addr++) |
|
106 |
*(buf++) = *(ptr++); |
|
107 |
} |
|
108 |
return true; |
|
109 |
} |
|
110 |
||
111 |
// null implementation for write |
|
112 |
static bool process_write_data(struct ps_prochandle* ph, |
|
113 |
uintptr_t addr, const char *buf , size_t size) { |
|
114 |
return false; |
|
115 |
} |
|
116 |
||
117 |
// "user" should be a pointer to a user_regs_struct |
|
118 |
static bool process_get_lwp_regs(struct ps_prochandle* ph, pid_t pid, struct user_regs_struct *user) { |
|
119 |
// we have already attached to all thread 'pid's, just use ptrace call |
|
120 |
// to get regset now. Note that we don't cache regset upfront for processes. |
|
121 |
// Linux on x86 and sparc are different. On x86 ptrace(PTRACE_GETREGS, ...) |
|
122 |
// uses pointer from 4th argument and ignores 3rd argument. On sparc it uses |
|
123 |
// pointer from 3rd argument and ignores 4th argument |
|
124 |
#if defined(sparc) || defined(sparcv9) |
|
125 |
#define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, addr, data) |
|
126 |
#else |
|
127 |
#define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, data, addr) |
|
128 |
#endif |
|
129 |
||
7415 | 130 |
#if defined(_LP64) && defined(PTRACE_GETREGS64) |
1 | 131 |
#define PTRACE_GETREGS_REQ PTRACE_GETREGS64 |
7415 | 132 |
#elif defined(PTRACE_GETREGS) |
133 |
#define PTRACE_GETREGS_REQ PTRACE_GETREGS |
|
134 |
#elif defined(PT_GETREGS) |
|
135 |
#define PTRACE_GETREGS_REQ PT_GETREGS |
|
1 | 136 |
#endif |
137 |
||
138 |
#ifdef PTRACE_GETREGS_REQ |
|
139 |
if (ptrace_getregs(PTRACE_GETREGS_REQ, pid, user, NULL) < 0) { |
|
140 |
print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid); |
|
141 |
return false; |
|
142 |
} |
|
143 |
return true; |
|
28924 | 144 |
#elif defined(PTRACE_GETREGSET) |
145 |
struct iovec iov; |
|
146 |
iov.iov_base = user; |
|
147 |
iov.iov_len = sizeof(*user); |
|
148 |
if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void*) &iov) < 0) { |
|
149 |
print_debug("ptrace(PTRACE_GETREGSET, ...) failed for lwp %d\n", pid); |
|
150 |
return false; |
|
151 |
} |
|
152 |
return true; |
|
1 | 153 |
#else |
154 |
print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n"); |
|
155 |
return false; |
|
156 |
#endif |
|
157 |
||
158 |
} |
|
159 |
||
15734 | 160 |
static bool ptrace_continue(pid_t pid, int signal) { |
161 |
// pass the signal to the process so we don't swallow it |
|
162 |
if (ptrace(PTRACE_CONT, pid, NULL, signal) < 0) { |
|
163 |
print_debug("ptrace(PTRACE_CONT, ..) failed for %d\n", pid); |
|
164 |
return false; |
|
165 |
} |
|
166 |
return true; |
|
167 |
} |
|
168 |
||
169 |
// waits until the ATTACH has stopped the process |
|
170 |
// by signal SIGSTOP |
|
171 |
static bool ptrace_waitpid(pid_t pid) { |
|
172 |
int ret; |
|
173 |
int status; |
|
174 |
while (true) { |
|
175 |
// Wait for debuggee to stop. |
|
176 |
ret = waitpid(pid, &status, 0); |
|
177 |
if (ret == -1 && errno == ECHILD) { |
|
178 |
// try cloned process. |
|
179 |
ret = waitpid(pid, &status, __WALL); |
|
180 |
} |
|
181 |
if (ret >= 0) { |
|
182 |
if (WIFSTOPPED(status)) { |
|
183 |
// Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP |
|
184 |
// will still be pending and delivered when the process is DETACHED and the process |
|
185 |
// will go to sleep. |
|
186 |
if (WSTOPSIG(status) == SIGSTOP) { |
|
187 |
// Debuggee stopped by SIGSTOP. |
|
188 |
return true; |
|
189 |
} |
|
190 |
if (!ptrace_continue(pid, WSTOPSIG(status))) { |
|
191 |
print_error("Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status)); |
|
192 |
return false; |
|
193 |
} |
|
194 |
} else { |
|
195 |
print_debug("waitpid(): Child process exited/terminated (status = 0x%x)\n", status); |
|
196 |
return false; |
|
197 |
} |
|
198 |
} else { |
|
199 |
switch (errno) { |
|
200 |
case EINTR: |
|
201 |
continue; |
|
202 |
break; |
|
203 |
case ECHILD: |
|
204 |
print_debug("waitpid() failed. Child process pid (%d) does not exist \n", pid); |
|
205 |
break; |
|
206 |
case EINVAL: |
|
207 |
print_debug("waitpid() failed. Invalid options argument.\n"); |
|
208 |
break; |
|
209 |
default: |
|
210 |
print_debug("waitpid() failed. Unexpected error %d\n",errno); |
|
211 |
break; |
|
212 |
} |
|
213 |
return false; |
|
214 |
} |
|
215 |
} |
|
216 |
} |
|
217 |
||
1 | 218 |
// attach to a process/thread specified by "pid" |
35049
5a30aa672689
8145099: Better error message when SA can't attach to a process
sla
parents:
28924
diff
changeset
|
219 |
static bool ptrace_attach(pid_t pid, char* err_buf, size_t err_buf_len) { |
1 | 220 |
if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) { |
35049
5a30aa672689
8145099: Better error message when SA can't attach to a process
sla
parents:
28924
diff
changeset
|
221 |
char buf[200]; |
5a30aa672689
8145099: Better error message when SA can't attach to a process
sla
parents:
28924
diff
changeset
|
222 |
char* msg = strerror_r(errno, buf, sizeof(buf)); |
5a30aa672689
8145099: Better error message when SA can't attach to a process
sla
parents:
28924
diff
changeset
|
223 |
snprintf(err_buf, err_buf_len, "ptrace(PTRACE_ATTACH, ..) failed for %d: %s", pid, msg); |
5a30aa672689
8145099: Better error message when SA can't attach to a process
sla
parents:
28924
diff
changeset
|
224 |
print_debug("%s\n", err_buf); |
1 | 225 |
return false; |
226 |
} else { |
|
15734 | 227 |
return ptrace_waitpid(pid); |
1 | 228 |
} |
229 |
} |
|
230 |
||
231 |
// ------------------------------------------------------- |
|
232 |
// functions for obtaining library information |
|
233 |
// ------------------------------------------------------- |
|
234 |
||
235 |
/* |
|
236 |
* splits a string _str_ into substrings with delimiter _delim_ by replacing old * delimiters with _new_delim_ (ideally, '\0'). the address of each substring |
|
237 |
* is stored in array _ptrs_ as the return value. the maximum capacity of _ptrs_ * array is specified by parameter _n_. |
|
238 |
* RETURN VALUE: total number of substrings (always <= _n_) |
|
239 |
* NOTE: string _str_ is modified if _delim_!=_new_delim_ |
|
240 |
*/ |
|
241 |
static int split_n_str(char * str, int n, char ** ptrs, char delim, char new_delim) |
|
242 |
{ |
|
243 |
int i; |
|
244 |
for(i = 0; i < n; i++) ptrs[i] = NULL; |
|
245 |
if (str == NULL || n < 1 ) return 0; |
|
246 |
||
247 |
i = 0; |
|
248 |
||
249 |
// skipping leading blanks |
|
250 |
while(*str&&*str==delim) str++; |
|
251 |
||
252 |
while(*str&&i<n){ |
|
253 |
ptrs[i++] = str; |
|
254 |
while(*str&&*str!=delim) str++; |
|
255 |
while(*str&&*str==delim) *(str++) = new_delim; |
|
256 |
} |
|
257 |
||
258 |
return i; |
|
259 |
} |
|
260 |
||
261 |
/* |
|
262 |
* fgets without storing '\n' at the end of the string |
|
263 |
*/ |
|
264 |
static char * fgets_no_cr(char * buf, int n, FILE *fp) |
|
265 |
{ |
|
266 |
char * rslt = fgets(buf, n, fp); |
|
267 |
if (rslt && buf && *buf){ |
|
268 |
char *p = strchr(buf, '\0'); |
|
269 |
if (*--p=='\n') *p='\0'; |
|
270 |
} |
|
271 |
return rslt; |
|
272 |
} |
|
273 |
||
274 |
// callback for read_thread_info |
|
275 |
static bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) { |
|
276 |
return add_thread_info(ph, pthread_id, lwp_id) != NULL; |
|
277 |
} |
|
278 |
||
279 |
static bool read_lib_info(struct ps_prochandle* ph) { |
|
280 |
char fname[32]; |
|
25062
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
281 |
char buf[PATH_MAX]; |
1 | 282 |
FILE *fp = NULL; |
283 |
||
284 |
sprintf(fname, "/proc/%d/maps", ph->pid); |
|
285 |
fp = fopen(fname, "r"); |
|
286 |
if (fp == NULL) { |
|
287 |
print_debug("can't open /proc/%d/maps file\n", ph->pid); |
|
288 |
return false; |
|
289 |
} |
|
290 |
||
25062
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
291 |
while(fgets_no_cr(buf, PATH_MAX, fp)){ |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
292 |
char * word[7]; |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
293 |
int nwords = split_n_str(buf, 7, word, ' ', '\0'); |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
294 |
|
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
295 |
if (nwords < 6) { |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
296 |
// not a shared library entry. ignore. |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
297 |
continue; |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
298 |
} |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
299 |
|
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
300 |
// SA does not handle the lines with patterns: |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
301 |
// "[stack]", "[heap]", "[vdso]", "[vsyscall]", etc. |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
302 |
if (word[5][0] == '[') { |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
303 |
// not a shared library entry. ignore. |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
304 |
continue; |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
305 |
} |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
306 |
|
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
307 |
if (nwords > 6) { |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
308 |
// prelink altered mapfile when the program is running. |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
309 |
// Entries like one below have to be skipped |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
310 |
// /lib64/libc-2.15.so (deleted) |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
311 |
// SO name in entries like one below have to be stripped. |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
312 |
// /lib64/libpthread-2.15.so.#prelink#.EECVts |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
313 |
char *s = strstr(word[5],".#prelink#"); |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
314 |
if (s == NULL) { |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
315 |
// No prelink keyword. skip deleted library |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
316 |
print_debug("skip shared object %s deleted by prelink\n", word[5]); |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
317 |
continue; |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
318 |
} |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
319 |
|
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
320 |
// Fall through |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
321 |
print_debug("rectifying shared object name %s changed by prelink\n", word[5]); |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
322 |
*s = 0; |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
323 |
} |
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
324 |
|
d38c0d15b3f7
8038392: Generating prelink cache breaks JAVA 'jinfo' utility normal behaviour
dsamersoff
parents:
20295
diff
changeset
|
325 |
if (find_lib(ph, word[5]) == false) { |
1 | 326 |
intptr_t base; |
327 |
lib_info* lib; |
|
6176
4d9030fe341f
6953477: Increase portability and flexibility of building Hotspot
bobv
parents:
5547
diff
changeset
|
328 |
#ifdef _LP64 |
1 | 329 |
sscanf(word[0], "%lx", &base); |
6176
4d9030fe341f
6953477: Increase portability and flexibility of building Hotspot
bobv
parents:
5547
diff
changeset
|
330 |
#else |
4d9030fe341f
6953477: Increase portability and flexibility of building Hotspot
bobv
parents:
5547
diff
changeset
|
331 |
sscanf(word[0], "%x", &base); |
4d9030fe341f
6953477: Increase portability and flexibility of building Hotspot
bobv
parents:
5547
diff
changeset
|
332 |
#endif |
1 | 333 |
if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL) |
334 |
continue; // ignore, add_lib_info prints error |
|
335 |
||
336 |
// we don't need to keep the library open, symtab is already |
|
337 |
// built. Only for core dump we need to keep the fd open. |
|
338 |
close(lib->fd); |
|
339 |
lib->fd = -1; |
|
340 |
} |
|
341 |
} |
|
342 |
fclose(fp); |
|
343 |
return true; |
|
344 |
} |
|
345 |
||
346 |
// detach a given pid |
|
347 |
static bool ptrace_detach(pid_t pid) { |
|
348 |
if (pid && ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) { |
|
349 |
print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid); |
|
350 |
return false; |
|
351 |
} else { |
|
352 |
return true; |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
// detach all pids of a ps_prochandle |
|
357 |
static void detach_all_pids(struct ps_prochandle* ph) { |
|
358 |
thread_info* thr = ph->threads; |
|
359 |
while (thr) { |
|
360 |
ptrace_detach(thr->lwp_id); |
|
361 |
thr = thr->next; |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
static void process_cleanup(struct ps_prochandle* ph) { |
|
366 |
detach_all_pids(ph); |
|
367 |
} |
|
368 |
||
369 |
static ps_prochandle_ops process_ops = { |
|
223
5c3b023117d9
6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents:
1
diff
changeset
|
370 |
.release= process_cleanup, |
5c3b023117d9
6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents:
1
diff
changeset
|
371 |
.p_pread= process_read_data, |
5c3b023117d9
6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents:
1
diff
changeset
|
372 |
.p_pwrite= process_write_data, |
5c3b023117d9
6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents:
1
diff
changeset
|
373 |
.get_lwp_regs= process_get_lwp_regs |
1 | 374 |
}; |
375 |
||
376 |
// attach to the process. One and only one exposed stuff |
|
49440 | 377 |
JNIEXPORT struct ps_prochandle* JNICALL |
51203
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
378 |
Pgrab(pid_t pid, char* err_buf, size_t err_buf_len, bool is_in_container) { |
1 | 379 |
struct ps_prochandle* ph = NULL; |
380 |
thread_info* thr = NULL; |
|
381 |
||
382 |
if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) { |
|
35049
5a30aa672689
8145099: Better error message when SA can't attach to a process
sla
parents:
28924
diff
changeset
|
383 |
snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle"); |
5a30aa672689
8145099: Better error message when SA can't attach to a process
sla
parents:
28924
diff
changeset
|
384 |
print_debug("%s\n", err_buf); |
1 | 385 |
return NULL; |
386 |
} |
|
387 |
||
35049
5a30aa672689
8145099: Better error message when SA can't attach to a process
sla
parents:
28924
diff
changeset
|
388 |
if (ptrace_attach(pid, err_buf, err_buf_len) != true) { |
1 | 389 |
free(ph); |
390 |
return NULL; |
|
391 |
} |
|
392 |
||
393 |
// initialize ps_prochandle |
|
394 |
ph->pid = pid; |
|
395 |
||
396 |
// initialize vtable |
|
397 |
ph->ops = &process_ops; |
|
398 |
||
399 |
// read library info and symbol tables, must do this before attaching threads, |
|
400 |
// as the symbols in the pthread library will be used to figure out |
|
401 |
// the list of threads within the same process. |
|
402 |
read_lib_info(ph); |
|
403 |
||
404 |
// read thread info |
|
51203
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
405 |
if (is_in_container) { |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
406 |
/* |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
407 |
* If the process is running in the container, SA scans all tasks in |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
408 |
* /proc/<PID>/task to read all threads info. |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
409 |
*/ |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
410 |
char taskpath[PATH_MAX]; |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
411 |
DIR *dirp; |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
412 |
struct dirent *entry; |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
413 |
|
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
414 |
snprintf(taskpath, PATH_MAX, "/proc/%d/task", ph->pid); |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
415 |
dirp = opendir(taskpath); |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
416 |
int lwp_id; |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
417 |
while ((entry = readdir(dirp)) != NULL) { |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
418 |
if (*entry->d_name == '.') { |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
419 |
continue; |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
420 |
} |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
421 |
lwp_id = atoi(entry->d_name); |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
422 |
if (lwp_id == ph->pid) { |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
423 |
continue; |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
424 |
} |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
425 |
add_new_thread(ph, -1, lwp_id); |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
426 |
} |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
427 |
closedir(dirp); |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
428 |
} else { |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
429 |
read_thread_info(ph, add_new_thread); |
220c9188db4f
8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents:
49440
diff
changeset
|
430 |
} |
1 | 431 |
|
432 |
// attach to the threads |
|
433 |
thr = ph->threads; |
|
434 |
while (thr) { |
|
435 |
// don't attach to the main thread again |
|
35049
5a30aa672689
8145099: Better error message when SA can't attach to a process
sla
parents:
28924
diff
changeset
|
436 |
if (ph->pid != thr->lwp_id && ptrace_attach(thr->lwp_id, err_buf, err_buf_len) != true) { |
1 | 437 |
// even if one attach fails, we get return NULL |
438 |
Prelease(ph); |
|
439 |
return NULL; |
|
440 |
} |
|
441 |
thr = thr->next; |
|
442 |
} |
|
443 |
return ph; |
|
444 |
} |