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