Mercurial > hg > graal-jvmci-8
annotate agent/src/os/linux/ps_proc.c @ 24234:ea6f94ab283b default tip
Added tag jvmci-0.36 for changeset 8128b98d4736
author | Gilles Duboscq <gilles.m.duboscq@oracle.com> |
---|---|
date | Mon, 18 Sep 2017 18:49:45 +0200 |
parents | d06b64fc150f |
children |
rev | line source |
---|---|
0 | 1 /* |
8058 | 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
0 | 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 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
196
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
196
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
196
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
8058 | 28 #include <signal.h> |
0 | 29 #include <errno.h> |
22838 | 30 #include <elf.h> |
12329 | 31 #include <sys/types.h> |
32 #include <sys/wait.h> | |
0 | 33 #include <sys/ptrace.h> |
22838 | 34 #include <sys/uio.h> |
0 | 35 #include "libproc_impl.h" |
36 | |
37 #if defined(x86_64) && !defined(amd64) | |
38 #define amd64 1 | |
39 #endif | |
40 | |
41 #ifndef __WALL | |
42 #define __WALL 0x40000000 // Copied from /usr/include/linux/wait.h | |
43 #endif | |
44 | |
45 // This file has the libproc implementation specific to live process | |
46 // For core files, refer to ps_core.c | |
47 | |
48 static inline uintptr_t align(uintptr_t ptr, size_t size) { | |
49 return (ptr & ~(size - 1)); | |
50 } | |
51 | |
52 // --------------------------------------------- | |
53 // ptrace functions | |
54 // --------------------------------------------- | |
55 | |
56 // read "size" bytes of data from "addr" within the target process. | |
57 // unlike the standard ptrace() function, process_read_data() can handle | |
58 // unaligned address - alignment check, if required, should be done | |
59 // before calling process_read_data. | |
60 | |
61 static bool process_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) { | |
62 long rslt; | |
63 size_t i, words; | |
64 uintptr_t end_addr = addr + size; | |
65 uintptr_t aligned_addr = align(addr, sizeof(long)); | |
66 | |
67 if (aligned_addr != addr) { | |
68 char *ptr = (char *)&rslt; | |
69 errno = 0; | |
70 rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0); | |
71 if (errno) { | |
72 print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr); | |
73 return false; | |
74 } | |
75 for (; aligned_addr != addr; aligned_addr++, ptr++); | |
76 for (; ((intptr_t)aligned_addr % sizeof(long)) && aligned_addr < end_addr; | |
77 aligned_addr++) | |
78 *(buf++) = *(ptr++); | |
79 } | |
80 | |
81 words = (end_addr - aligned_addr) / sizeof(long); | |
82 | |
83 // assert((intptr_t)aligned_addr % sizeof(long) == 0); | |
84 for (i = 0; i < words; i++) { | |
85 errno = 0; | |
86 rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0); | |
87 if (errno) { | |
88 print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr); | |
89 return false; | |
90 } | |
91 *(long *)buf = rslt; | |
92 buf += sizeof(long); | |
93 aligned_addr += sizeof(long); | |
94 } | |
95 | |
96 if (aligned_addr != end_addr) { | |
97 char *ptr = (char *)&rslt; | |
98 errno = 0; | |
99 rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0); | |
100 if (errno) { | |
101 print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr); | |
102 return false; | |
103 } | |
104 for (; aligned_addr != end_addr; aligned_addr++) | |
105 *(buf++) = *(ptr++); | |
106 } | |
107 return true; | |
108 } | |
109 | |
110 // null implementation for write | |
111 static bool process_write_data(struct ps_prochandle* ph, | |
112 uintptr_t addr, const char *buf , size_t size) { | |
113 return false; | |
114 } | |
115 | |
116 // "user" should be a pointer to a user_regs_struct | |
117 static bool process_get_lwp_regs(struct ps_prochandle* ph, pid_t pid, struct user_regs_struct *user) { | |
118 // we have already attached to all thread 'pid's, just use ptrace call | |
119 // to get regset now. Note that we don't cache regset upfront for processes. | |
120 // Linux on x86 and sparc are different. On x86 ptrace(PTRACE_GETREGS, ...) | |
121 // uses pointer from 4th argument and ignores 3rd argument. On sparc it uses | |
122 // pointer from 3rd argument and ignores 4th argument | |
123 #if defined(sparc) || defined(sparcv9) | |
124 #define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, addr, data) | |
125 #else | |
126 #define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, data, addr) | |
127 #endif | |
128 | |
1990 | 129 #if defined(_LP64) && defined(PTRACE_GETREGS64) |
0 | 130 #define PTRACE_GETREGS_REQ PTRACE_GETREGS64 |
1990 | 131 #elif defined(PTRACE_GETREGS) |
132 #define PTRACE_GETREGS_REQ PTRACE_GETREGS | |
133 #elif defined(PT_GETREGS) | |
134 #define PTRACE_GETREGS_REQ PT_GETREGS | |
0 | 135 #endif |
136 | |
137 #ifdef PTRACE_GETREGS_REQ | |
138 if (ptrace_getregs(PTRACE_GETREGS_REQ, pid, user, NULL) < 0) { | |
139 print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid); | |
140 return false; | |
141 } | |
142 return true; | |
22838 | 143 #elif defined(PTRACE_GETREGSET) |
144 struct iovec iov; | |
145 iov.iov_base = user; | |
146 iov.iov_len = sizeof(*user); | |
147 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void*) &iov) < 0) { | |
148 print_debug("ptrace(PTRACE_GETREGSET, ...) failed for lwp %d\n", pid); | |
149 return false; | |
150 } | |
151 return true; | |
0 | 152 #else |
153 print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n"); | |
154 return false; | |
155 #endif | |
156 | |
157 } | |
158 | |
8058 | 159 static bool ptrace_continue(pid_t pid, int signal) { |
160 // pass the signal to the process so we don't swallow it | |
161 if (ptrace(PTRACE_CONT, pid, NULL, signal) < 0) { | |
162 print_debug("ptrace(PTRACE_CONT, ..) failed for %d\n", pid); | |
163 return false; | |
164 } | |
165 return true; | |
166 } | |
167 | |
168 // waits until the ATTACH has stopped the process | |
169 // by signal SIGSTOP | |
170 static bool ptrace_waitpid(pid_t pid) { | |
171 int ret; | |
172 int status; | |
173 while (true) { | |
174 // Wait for debuggee to stop. | |
175 ret = waitpid(pid, &status, 0); | |
176 if (ret == -1 && errno == ECHILD) { | |
177 // try cloned process. | |
178 ret = waitpid(pid, &status, __WALL); | |
179 } | |
180 if (ret >= 0) { | |
181 if (WIFSTOPPED(status)) { | |
182 // Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP | |
183 // will still be pending and delivered when the process is DETACHED and the process | |
184 // will go to sleep. | |
185 if (WSTOPSIG(status) == SIGSTOP) { | |
186 // Debuggee stopped by SIGSTOP. | |
187 return true; | |
188 } | |
189 if (!ptrace_continue(pid, WSTOPSIG(status))) { | |
190 print_error("Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status)); | |
191 return false; | |
192 } | |
193 } else { | |
194 print_debug("waitpid(): Child process exited/terminated (status = 0x%x)\n", status); | |
195 return false; | |
196 } | |
197 } else { | |
198 switch (errno) { | |
199 case EINTR: | |
200 continue; | |
201 break; | |
202 case ECHILD: | |
203 print_debug("waitpid() failed. Child process pid (%d) does not exist \n", pid); | |
204 break; | |
205 case EINVAL: | |
206 print_debug("waitpid() failed. Invalid options argument.\n"); | |
207 break; | |
208 default: | |
209 print_debug("waitpid() failed. Unexpected error %d\n",errno); | |
210 break; | |
211 } | |
212 return false; | |
213 } | |
214 } | |
215 } | |
216 | |
0 | 217 // attach to a process/thread specified by "pid" |
23475
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
218 static bool ptrace_attach(pid_t pid, char* err_buf, size_t err_buf_len) { |
0 | 219 if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) { |
23475
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
220 char buf[200]; |
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
221 char* msg = strerror_r(errno, buf, sizeof(buf)); |
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
222 snprintf(err_buf, err_buf_len, "ptrace(PTRACE_ATTACH, ..) failed for %d: %s", pid, msg); |
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
223 print_debug("%s\n", err_buf); |
0 | 224 return false; |
225 } else { | |
8058 | 226 return ptrace_waitpid(pid); |
0 | 227 } |
228 } | |
229 | |
230 // ------------------------------------------------------- | |
231 // functions for obtaining library information | |
232 // ------------------------------------------------------- | |
233 | |
234 /* | |
235 * splits a string _str_ into substrings with delimiter _delim_ by replacing old * delimiters with _new_delim_ (ideally, '\0'). the address of each substring | |
236 * is stored in array _ptrs_ as the return value. the maximum capacity of _ptrs_ * array is specified by parameter _n_. | |
237 * RETURN VALUE: total number of substrings (always <= _n_) | |
238 * NOTE: string _str_ is modified if _delim_!=_new_delim_ | |
239 */ | |
240 static int split_n_str(char * str, int n, char ** ptrs, char delim, char new_delim) | |
241 { | |
242 int i; | |
243 for(i = 0; i < n; i++) ptrs[i] = NULL; | |
244 if (str == NULL || n < 1 ) return 0; | |
245 | |
246 i = 0; | |
247 | |
248 // skipping leading blanks | |
249 while(*str&&*str==delim) str++; | |
250 | |
251 while(*str&&i<n){ | |
252 ptrs[i++] = str; | |
253 while(*str&&*str!=delim) str++; | |
254 while(*str&&*str==delim) *(str++) = new_delim; | |
255 } | |
256 | |
257 return i; | |
258 } | |
259 | |
260 /* | |
261 * fgets without storing '\n' at the end of the string | |
262 */ | |
263 static char * fgets_no_cr(char * buf, int n, FILE *fp) | |
264 { | |
265 char * rslt = fgets(buf, n, fp); | |
266 if (rslt && buf && *buf){ | |
267 char *p = strchr(buf, '\0'); | |
268 if (*--p=='\n') *p='\0'; | |
269 } | |
270 return rslt; | |
271 } | |
272 | |
273 // callback for read_thread_info | |
274 static bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) { | |
275 return add_thread_info(ph, pthread_id, lwp_id) != NULL; | |
276 } | |
277 | |
278 static bool read_lib_info(struct ps_prochandle* ph) { | |
279 char fname[32]; | |
280 char buf[256]; | |
281 FILE *fp = NULL; | |
282 | |
283 sprintf(fname, "/proc/%d/maps", ph->pid); | |
284 fp = fopen(fname, "r"); | |
285 if (fp == NULL) { | |
286 print_debug("can't open /proc/%d/maps file\n", ph->pid); | |
287 return false; | |
288 } | |
289 | |
290 while(fgets_no_cr(buf, 256, fp)){ | |
291 char * word[6]; | |
292 int nwords = split_n_str(buf, 6, word, ' ', '\0'); | |
293 if (nwords > 5 && find_lib(ph, word[5]) == false) { | |
294 intptr_t base; | |
295 lib_info* lib; | |
1681
126ea7725993
6953477: Increase portability and flexibility of building Hotspot
bobv
parents:
1552
diff
changeset
|
296 #ifdef _LP64 |
0 | 297 sscanf(word[0], "%lx", &base); |
1681
126ea7725993
6953477: Increase portability and flexibility of building Hotspot
bobv
parents:
1552
diff
changeset
|
298 #else |
126ea7725993
6953477: Increase portability and flexibility of building Hotspot
bobv
parents:
1552
diff
changeset
|
299 sscanf(word[0], "%x", &base); |
126ea7725993
6953477: Increase portability and flexibility of building Hotspot
bobv
parents:
1552
diff
changeset
|
300 #endif |
0 | 301 if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL) |
302 continue; // ignore, add_lib_info prints error | |
303 | |
304 // we don't need to keep the library open, symtab is already | |
305 // built. Only for core dump we need to keep the fd open. | |
306 close(lib->fd); | |
307 lib->fd = -1; | |
308 } | |
309 } | |
310 fclose(fp); | |
311 return true; | |
312 } | |
313 | |
314 // detach a given pid | |
315 static bool ptrace_detach(pid_t pid) { | |
316 if (pid && ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) { | |
317 print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid); | |
318 return false; | |
319 } else { | |
320 return true; | |
321 } | |
322 } | |
323 | |
324 // detach all pids of a ps_prochandle | |
325 static void detach_all_pids(struct ps_prochandle* ph) { | |
326 thread_info* thr = ph->threads; | |
327 while (thr) { | |
328 ptrace_detach(thr->lwp_id); | |
329 thr = thr->next; | |
330 } | |
331 } | |
332 | |
333 static void process_cleanup(struct ps_prochandle* ph) { | |
334 detach_all_pids(ph); | |
335 } | |
336 | |
337 static ps_prochandle_ops process_ops = { | |
50
485d403e94e1
6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents:
0
diff
changeset
|
338 .release= process_cleanup, |
485d403e94e1
6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents:
0
diff
changeset
|
339 .p_pread= process_read_data, |
485d403e94e1
6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents:
0
diff
changeset
|
340 .p_pwrite= process_write_data, |
485d403e94e1
6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents:
0
diff
changeset
|
341 .get_lwp_regs= process_get_lwp_regs |
0 | 342 }; |
343 | |
344 // attach to the process. One and only one exposed stuff | |
23475
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
345 struct ps_prochandle* Pgrab(pid_t pid, char* err_buf, size_t err_buf_len) { |
0 | 346 struct ps_prochandle* ph = NULL; |
347 thread_info* thr = NULL; | |
348 | |
349 if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) { | |
23475
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
350 snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle"); |
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
351 print_debug("%s\n", err_buf); |
0 | 352 return NULL; |
353 } | |
354 | |
23475
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
355 if (ptrace_attach(pid, err_buf, err_buf_len) != true) { |
0 | 356 free(ph); |
357 return NULL; | |
358 } | |
359 | |
360 // initialize ps_prochandle | |
361 ph->pid = pid; | |
362 | |
363 // initialize vtable | |
364 ph->ops = &process_ops; | |
365 | |
366 // read library info and symbol tables, must do this before attaching threads, | |
367 // as the symbols in the pthread library will be used to figure out | |
368 // the list of threads within the same process. | |
369 read_lib_info(ph); | |
370 | |
371 // read thread info | |
372 read_thread_info(ph, add_new_thread); | |
373 | |
374 // attach to the threads | |
375 thr = ph->threads; | |
376 while (thr) { | |
377 // don't attach to the main thread again | |
23475
d06b64fc150f
8145099: Better error message when SA can't attach to a process
sla
parents:
22838
diff
changeset
|
378 if (ph->pid != thr->lwp_id && ptrace_attach(thr->lwp_id, err_buf, err_buf_len) != true) { |
0 | 379 // even if one attach fails, we get return NULL |
380 Prelease(ph); | |
381 return NULL; | |
382 } | |
383 thr = thr->next; | |
384 } | |
385 return ph; | |
386 } |