Mon, 28 Jul 2014 15:06:38 -0700
8051344: JVM crashed in Compile::start() during method parsing w/ UseRTMDeopt turned on
Summary: call rtm_deopt() only if there were no compilation bailouts before.
Reviewed-by: kvn
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/types.h>
31 #include <sys/wait.h>
32 #include <sys/ptrace.h>
33 #include "libproc_impl.h"
35 #if defined(x86_64) && !defined(amd64)
36 #define amd64 1
37 #endif
39 #ifndef __WALL
40 #define __WALL 0x40000000 // Copied from /usr/include/linux/wait.h
41 #endif
43 // This file has the libproc implementation specific to live process
44 // For core files, refer to ps_core.c
46 static inline uintptr_t align(uintptr_t ptr, size_t size) {
47 return (ptr & ~(size - 1));
48 }
50 // ---------------------------------------------
51 // ptrace functions
52 // ---------------------------------------------
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.
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));
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 }
79 words = (end_addr - aligned_addr) / sizeof(long);
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 }
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 }
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 }
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
127 #if defined(_LP64) && defined(PTRACE_GETREGS64)
128 #define PTRACE_GETREGS_REQ PTRACE_GETREGS64
129 #elif defined(PTRACE_GETREGS)
130 #define PTRACE_GETREGS_REQ PTRACE_GETREGS
131 #elif defined(PT_GETREGS)
132 #define PTRACE_GETREGS_REQ PT_GETREGS
133 #endif
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
146 }
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 }
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 }
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 {
212 return ptrace_waitpid(pid);
213 }
214 }
216 // -------------------------------------------------------
217 // functions for obtaining library information
218 // -------------------------------------------------------
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;
232 i = 0;
234 // skipping leading blanks
235 while(*str&&*str==delim) str++;
237 while(*str&&i<n){
238 ptrs[i++] = str;
239 while(*str&&*str!=delim) str++;
240 while(*str&&*str==delim) *(str++) = new_delim;
241 }
243 return i;
244 }
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 }
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 }
264 static bool read_lib_info(struct ps_prochandle* ph) {
265 char fname[32];
266 char buf[256];
267 FILE *fp = NULL;
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 }
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;
282 #ifdef _LP64
283 sscanf(word[0], "%lx", &base);
284 #else
285 sscanf(word[0], "%x", &base);
286 #endif
287 if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL)
288 continue; // ignore, add_lib_info prints error
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 }
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 }
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 }
319 static void process_cleanup(struct ps_prochandle* ph) {
320 detach_all_pids(ph);
321 }
323 static ps_prochandle_ops process_ops = {
324 .release= process_cleanup,
325 .p_pread= process_read_data,
326 .p_pwrite= process_write_data,
327 .get_lwp_regs= process_get_lwp_regs
328 };
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;
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 }
340 if (ptrace_attach(pid) != true) {
341 free(ph);
342 return NULL;
343 }
345 // initialize ps_prochandle
346 ph->pid = pid;
348 // initialize vtable
349 ph->ops = &process_ops;
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);
356 // read thread info
357 read_thread_info(ph, add_new_thread);
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 }