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