annotate agent/src/os/linux/ps_proc.c @ 17991:a07a3a29df67

8046408: Build failure from multiple ptrace.h Summary: prefer <sys/ptrace.h> over <linux/ptrace.h> Reviewed-by: sla, mikael Contributed-by: kim.barrett@oracle.com
author jwilhelm
date Fri, 13 Jun 2014 17:07:39 -0400
parents f2512d89ad0c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
8058
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
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
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 #include <stdio.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #include <stdlib.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
27 #include <string.h>
8058
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
28 #include <signal.h>
0
a61af66fc99e Initial load
duke
parents:
diff changeset
29 #include <errno.h>
12329
f2512d89ad0c 8025613: clang: remove -Wno-unused-value
twisti
parents: 8058
diff changeset
30 #include <sys/types.h>
f2512d89ad0c 8025613: clang: remove -Wno-unused-value
twisti
parents: 8058
diff changeset
31 #include <sys/wait.h>
0
a61af66fc99e Initial load
duke
parents:
diff changeset
32 #include <sys/ptrace.h>
a61af66fc99e Initial load
duke
parents:
diff changeset
33 #include "libproc_impl.h"
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 #if defined(x86_64) && !defined(amd64)
a61af66fc99e Initial load
duke
parents:
diff changeset
36 #define amd64 1
a61af66fc99e Initial load
duke
parents:
diff changeset
37 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 #ifndef __WALL
a61af66fc99e Initial load
duke
parents:
diff changeset
40 #define __WALL 0x40000000 // Copied from /usr/include/linux/wait.h
a61af66fc99e Initial load
duke
parents:
diff changeset
41 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
42
a61af66fc99e Initial load
duke
parents:
diff changeset
43 // This file has the libproc implementation specific to live process
a61af66fc99e Initial load
duke
parents:
diff changeset
44 // For core files, refer to ps_core.c
a61af66fc99e Initial load
duke
parents:
diff changeset
45
a61af66fc99e Initial load
duke
parents:
diff changeset
46 static inline uintptr_t align(uintptr_t ptr, size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
47 return (ptr & ~(size - 1));
a61af66fc99e Initial load
duke
parents:
diff changeset
48 }
a61af66fc99e Initial load
duke
parents:
diff changeset
49
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // ---------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
51 // ptrace functions
a61af66fc99e Initial load
duke
parents:
diff changeset
52 // ---------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 // read "size" bytes of data from "addr" within the target process.
a61af66fc99e Initial load
duke
parents:
diff changeset
55 // unlike the standard ptrace() function, process_read_data() can handle
a61af66fc99e Initial load
duke
parents:
diff changeset
56 // unaligned address - alignment check, if required, should be done
a61af66fc99e Initial load
duke
parents:
diff changeset
57 // before calling process_read_data.
a61af66fc99e Initial load
duke
parents:
diff changeset
58
a61af66fc99e Initial load
duke
parents:
diff changeset
59 static bool process_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
60 long rslt;
a61af66fc99e Initial load
duke
parents:
diff changeset
61 size_t i, words;
a61af66fc99e Initial load
duke
parents:
diff changeset
62 uintptr_t end_addr = addr + size;
a61af66fc99e Initial load
duke
parents:
diff changeset
63 uintptr_t aligned_addr = align(addr, sizeof(long));
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 if (aligned_addr != addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 char *ptr = (char *)&rslt;
a61af66fc99e Initial load
duke
parents:
diff changeset
67 errno = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
68 rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
69 if (errno) {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
a61af66fc99e Initial load
duke
parents:
diff changeset
71 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 }
a61af66fc99e Initial load
duke
parents:
diff changeset
73 for (; aligned_addr != addr; aligned_addr++, ptr++);
a61af66fc99e Initial load
duke
parents:
diff changeset
74 for (; ((intptr_t)aligned_addr % sizeof(long)) && aligned_addr < end_addr;
a61af66fc99e Initial load
duke
parents:
diff changeset
75 aligned_addr++)
a61af66fc99e Initial load
duke
parents:
diff changeset
76 *(buf++) = *(ptr++);
a61af66fc99e Initial load
duke
parents:
diff changeset
77 }
a61af66fc99e Initial load
duke
parents:
diff changeset
78
a61af66fc99e Initial load
duke
parents:
diff changeset
79 words = (end_addr - aligned_addr) / sizeof(long);
a61af66fc99e Initial load
duke
parents:
diff changeset
80
a61af66fc99e Initial load
duke
parents:
diff changeset
81 // assert((intptr_t)aligned_addr % sizeof(long) == 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
82 for (i = 0; i < words; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 errno = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
84 rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
85 if (errno) {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
a61af66fc99e Initial load
duke
parents:
diff changeset
87 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89 *(long *)buf = rslt;
a61af66fc99e Initial load
duke
parents:
diff changeset
90 buf += sizeof(long);
a61af66fc99e Initial load
duke
parents:
diff changeset
91 aligned_addr += sizeof(long);
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94 if (aligned_addr != end_addr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 char *ptr = (char *)&rslt;
a61af66fc99e Initial load
duke
parents:
diff changeset
96 errno = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
98 if (errno) {
a61af66fc99e Initial load
duke
parents:
diff changeset
99 print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
a61af66fc99e Initial load
duke
parents:
diff changeset
100 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102 for (; aligned_addr != end_addr; aligned_addr++)
a61af66fc99e Initial load
duke
parents:
diff changeset
103 *(buf++) = *(ptr++);
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
106 }
a61af66fc99e Initial load
duke
parents:
diff changeset
107
a61af66fc99e Initial load
duke
parents:
diff changeset
108 // null implementation for write
a61af66fc99e Initial load
duke
parents:
diff changeset
109 static bool process_write_data(struct ps_prochandle* ph,
a61af66fc99e Initial load
duke
parents:
diff changeset
110 uintptr_t addr, const char *buf , size_t size) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
112 }
a61af66fc99e Initial load
duke
parents:
diff changeset
113
a61af66fc99e Initial load
duke
parents:
diff changeset
114 // "user" should be a pointer to a user_regs_struct
a61af66fc99e Initial load
duke
parents:
diff changeset
115 static bool process_get_lwp_regs(struct ps_prochandle* ph, pid_t pid, struct user_regs_struct *user) {
a61af66fc99e Initial load
duke
parents:
diff changeset
116 // we have already attached to all thread 'pid's, just use ptrace call
a61af66fc99e Initial load
duke
parents:
diff changeset
117 // to get regset now. Note that we don't cache regset upfront for processes.
a61af66fc99e Initial load
duke
parents:
diff changeset
118 // Linux on x86 and sparc are different. On x86 ptrace(PTRACE_GETREGS, ...)
a61af66fc99e Initial load
duke
parents:
diff changeset
119 // uses pointer from 4th argument and ignores 3rd argument. On sparc it uses
a61af66fc99e Initial load
duke
parents:
diff changeset
120 // pointer from 3rd argument and ignores 4th argument
a61af66fc99e Initial load
duke
parents:
diff changeset
121 #if defined(sparc) || defined(sparcv9)
a61af66fc99e Initial load
duke
parents:
diff changeset
122 #define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, addr, data)
a61af66fc99e Initial load
duke
parents:
diff changeset
123 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
124 #define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, data, addr)
a61af66fc99e Initial load
duke
parents:
diff changeset
125 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
126
1990
401fbd7ff77c 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 1681
diff changeset
127 #if defined(_LP64) && defined(PTRACE_GETREGS64)
0
a61af66fc99e Initial load
duke
parents:
diff changeset
128 #define PTRACE_GETREGS_REQ PTRACE_GETREGS64
1990
401fbd7ff77c 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 1681
diff changeset
129 #elif defined(PTRACE_GETREGS)
401fbd7ff77c 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 1681
diff changeset
130 #define PTRACE_GETREGS_REQ PTRACE_GETREGS
401fbd7ff77c 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 1681
diff changeset
131 #elif defined(PT_GETREGS)
401fbd7ff77c 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 1681
diff changeset
132 #define PTRACE_GETREGS_REQ PT_GETREGS
0
a61af66fc99e Initial load
duke
parents:
diff changeset
133 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 #ifdef PTRACE_GETREGS_REQ
a61af66fc99e Initial load
duke
parents:
diff changeset
136 if (ptrace_getregs(PTRACE_GETREGS_REQ, pid, user, NULL) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
138 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
141 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
142 print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
143 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
144 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
145
a61af66fc99e Initial load
duke
parents:
diff changeset
146 }
a61af66fc99e Initial load
duke
parents:
diff changeset
147
8058
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
148 static bool ptrace_continue(pid_t pid, int signal) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
149 // pass the signal to the process so we don't swallow it
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
150 if (ptrace(PTRACE_CONT, pid, NULL, signal) < 0) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
151 print_debug("ptrace(PTRACE_CONT, ..) failed for %d\n", pid);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
152 return false;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
153 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
154 return true;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
155 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
156
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
157 // waits until the ATTACH has stopped the process
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
158 // by signal SIGSTOP
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
159 static bool ptrace_waitpid(pid_t pid) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
160 int ret;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
161 int status;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
162 while (true) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
163 // Wait for debuggee to stop.
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
164 ret = waitpid(pid, &status, 0);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
165 if (ret == -1 && errno == ECHILD) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
166 // try cloned process.
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
167 ret = waitpid(pid, &status, __WALL);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
168 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
169 if (ret >= 0) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
170 if (WIFSTOPPED(status)) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
171 // Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
172 // will still be pending and delivered when the process is DETACHED and the process
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
173 // will go to sleep.
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
174 if (WSTOPSIG(status) == SIGSTOP) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
175 // Debuggee stopped by SIGSTOP.
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
176 return true;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
177 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
178 if (!ptrace_continue(pid, WSTOPSIG(status))) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
179 print_error("Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
180 return false;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
181 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
182 } else {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
183 print_debug("waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
184 return false;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
185 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
186 } else {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
187 switch (errno) {
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
188 case EINTR:
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
189 continue;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
190 break;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
191 case ECHILD:
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
192 print_debug("waitpid() failed. Child process pid (%d) does not exist \n", pid);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
193 break;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
194 case EINVAL:
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
195 print_debug("waitpid() failed. Invalid options argument.\n");
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
196 break;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
197 default:
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
198 print_debug("waitpid() failed. Unexpected error %d\n",errno);
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
199 break;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
200 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
201 return false;
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
202 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
203 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
204 }
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
205
0
a61af66fc99e Initial load
duke
parents:
diff changeset
206 // attach to a process/thread specified by "pid"
a61af66fc99e Initial load
duke
parents:
diff changeset
207 static bool ptrace_attach(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
209 print_debug("ptrace(PTRACE_ATTACH, ..) failed for %d\n", pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
210 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
211 } else {
8058
2394a89e89f4 8008088: SA can hang the VM
rbackman
parents: 2042
diff changeset
212 return ptrace_waitpid(pid);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
213 }
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215
a61af66fc99e Initial load
duke
parents:
diff changeset
216 // -------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
217 // functions for obtaining library information
a61af66fc99e Initial load
duke
parents:
diff changeset
218 // -------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
219
a61af66fc99e Initial load
duke
parents:
diff changeset
220 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
221 * splits a string _str_ into substrings with delimiter _delim_ by replacing old * delimiters with _new_delim_ (ideally, '\0'). the address of each substring
a61af66fc99e Initial load
duke
parents:
diff changeset
222 * is stored in array _ptrs_ as the return value. the maximum capacity of _ptrs_ * array is specified by parameter _n_.
a61af66fc99e Initial load
duke
parents:
diff changeset
223 * RETURN VALUE: total number of substrings (always <= _n_)
a61af66fc99e Initial load
duke
parents:
diff changeset
224 * NOTE: string _str_ is modified if _delim_!=_new_delim_
a61af66fc99e Initial load
duke
parents:
diff changeset
225 */
a61af66fc99e Initial load
duke
parents:
diff changeset
226 static int split_n_str(char * str, int n, char ** ptrs, char delim, char new_delim)
a61af66fc99e Initial load
duke
parents:
diff changeset
227 {
a61af66fc99e Initial load
duke
parents:
diff changeset
228 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
229 for(i = 0; i < n; i++) ptrs[i] = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
230 if (str == NULL || n < 1 ) return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
233
a61af66fc99e Initial load
duke
parents:
diff changeset
234 // skipping leading blanks
a61af66fc99e Initial load
duke
parents:
diff changeset
235 while(*str&&*str==delim) str++;
a61af66fc99e Initial load
duke
parents:
diff changeset
236
a61af66fc99e Initial load
duke
parents:
diff changeset
237 while(*str&&i<n){
a61af66fc99e Initial load
duke
parents:
diff changeset
238 ptrs[i++] = str;
a61af66fc99e Initial load
duke
parents:
diff changeset
239 while(*str&&*str!=delim) str++;
a61af66fc99e Initial load
duke
parents:
diff changeset
240 while(*str&&*str==delim) *(str++) = new_delim;
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243 return i;
a61af66fc99e Initial load
duke
parents:
diff changeset
244 }
a61af66fc99e Initial load
duke
parents:
diff changeset
245
a61af66fc99e Initial load
duke
parents:
diff changeset
246 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
247 * fgets without storing '\n' at the end of the string
a61af66fc99e Initial load
duke
parents:
diff changeset
248 */
a61af66fc99e Initial load
duke
parents:
diff changeset
249 static char * fgets_no_cr(char * buf, int n, FILE *fp)
a61af66fc99e Initial load
duke
parents:
diff changeset
250 {
a61af66fc99e Initial load
duke
parents:
diff changeset
251 char * rslt = fgets(buf, n, fp);
a61af66fc99e Initial load
duke
parents:
diff changeset
252 if (rslt && buf && *buf){
a61af66fc99e Initial load
duke
parents:
diff changeset
253 char *p = strchr(buf, '\0');
a61af66fc99e Initial load
duke
parents:
diff changeset
254 if (*--p=='\n') *p='\0';
a61af66fc99e Initial load
duke
parents:
diff changeset
255 }
a61af66fc99e Initial load
duke
parents:
diff changeset
256 return rslt;
a61af66fc99e Initial load
duke
parents:
diff changeset
257 }
a61af66fc99e Initial load
duke
parents:
diff changeset
258
a61af66fc99e Initial load
duke
parents:
diff changeset
259 // callback for read_thread_info
a61af66fc99e Initial load
duke
parents:
diff changeset
260 static bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
a61af66fc99e Initial load
duke
parents:
diff changeset
261 return add_thread_info(ph, pthread_id, lwp_id) != NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
262 }
a61af66fc99e Initial load
duke
parents:
diff changeset
263
a61af66fc99e Initial load
duke
parents:
diff changeset
264 static bool read_lib_info(struct ps_prochandle* ph) {
a61af66fc99e Initial load
duke
parents:
diff changeset
265 char fname[32];
a61af66fc99e Initial load
duke
parents:
diff changeset
266 char buf[256];
a61af66fc99e Initial load
duke
parents:
diff changeset
267 FILE *fp = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
268
a61af66fc99e Initial load
duke
parents:
diff changeset
269 sprintf(fname, "/proc/%d/maps", ph->pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
270 fp = fopen(fname, "r");
a61af66fc99e Initial load
duke
parents:
diff changeset
271 if (fp == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
272 print_debug("can't open /proc/%d/maps file\n", ph->pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
273 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
274 }
a61af66fc99e Initial load
duke
parents:
diff changeset
275
a61af66fc99e Initial load
duke
parents:
diff changeset
276 while(fgets_no_cr(buf, 256, fp)){
a61af66fc99e Initial load
duke
parents:
diff changeset
277 char * word[6];
a61af66fc99e Initial load
duke
parents:
diff changeset
278 int nwords = split_n_str(buf, 6, word, ' ', '\0');
a61af66fc99e Initial load
duke
parents:
diff changeset
279 if (nwords > 5 && find_lib(ph, word[5]) == false) {
a61af66fc99e Initial load
duke
parents:
diff changeset
280 intptr_t base;
a61af66fc99e Initial load
duke
parents:
diff changeset
281 lib_info* lib;
1681
126ea7725993 6953477: Increase portability and flexibility of building Hotspot
bobv
parents: 1552
diff changeset
282 #ifdef _LP64
0
a61af66fc99e Initial load
duke
parents:
diff changeset
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
a61af66fc99e Initial load
duke
parents:
diff changeset
287 if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL)
a61af66fc99e Initial load
duke
parents:
diff changeset
288 continue; // ignore, add_lib_info prints error
a61af66fc99e Initial load
duke
parents:
diff changeset
289
a61af66fc99e Initial load
duke
parents:
diff changeset
290 // we don't need to keep the library open, symtab is already
a61af66fc99e Initial load
duke
parents:
diff changeset
291 // built. Only for core dump we need to keep the fd open.
a61af66fc99e Initial load
duke
parents:
diff changeset
292 close(lib->fd);
a61af66fc99e Initial load
duke
parents:
diff changeset
293 lib->fd = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
294 }
a61af66fc99e Initial load
duke
parents:
diff changeset
295 }
a61af66fc99e Initial load
duke
parents:
diff changeset
296 fclose(fp);
a61af66fc99e Initial load
duke
parents:
diff changeset
297 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
298 }
a61af66fc99e Initial load
duke
parents:
diff changeset
299
a61af66fc99e Initial load
duke
parents:
diff changeset
300 // detach a given pid
a61af66fc99e Initial load
duke
parents:
diff changeset
301 static bool ptrace_detach(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
302 if (pid && ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid);
a61af66fc99e Initial load
duke
parents:
diff changeset
304 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
305 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
306 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
307 }
a61af66fc99e Initial load
duke
parents:
diff changeset
308 }
a61af66fc99e Initial load
duke
parents:
diff changeset
309
a61af66fc99e Initial load
duke
parents:
diff changeset
310 // detach all pids of a ps_prochandle
a61af66fc99e Initial load
duke
parents:
diff changeset
311 static void detach_all_pids(struct ps_prochandle* ph) {
a61af66fc99e Initial load
duke
parents:
diff changeset
312 thread_info* thr = ph->threads;
a61af66fc99e Initial load
duke
parents:
diff changeset
313 while (thr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 ptrace_detach(thr->lwp_id);
a61af66fc99e Initial load
duke
parents:
diff changeset
315 thr = thr->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
316 }
a61af66fc99e Initial load
duke
parents:
diff changeset
317 }
a61af66fc99e Initial load
duke
parents:
diff changeset
318
a61af66fc99e Initial load
duke
parents:
diff changeset
319 static void process_cleanup(struct ps_prochandle* ph) {
a61af66fc99e Initial load
duke
parents:
diff changeset
320 detach_all_pids(ph);
a61af66fc99e Initial load
duke
parents:
diff changeset
321 }
a61af66fc99e Initial load
duke
parents:
diff changeset
322
a61af66fc99e Initial load
duke
parents:
diff changeset
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
a61af66fc99e Initial load
duke
parents:
diff changeset
328 };
a61af66fc99e Initial load
duke
parents:
diff changeset
329
a61af66fc99e Initial load
duke
parents:
diff changeset
330 // attach to the process. One and only one exposed stuff
a61af66fc99e Initial load
duke
parents:
diff changeset
331 struct ps_prochandle* Pgrab(pid_t pid) {
a61af66fc99e Initial load
duke
parents:
diff changeset
332 struct ps_prochandle* ph = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
333 thread_info* thr = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
334
a61af66fc99e Initial load
duke
parents:
diff changeset
335 if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
336 print_debug("can't allocate memory for ps_prochandle\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
337 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
338 }
a61af66fc99e Initial load
duke
parents:
diff changeset
339
a61af66fc99e Initial load
duke
parents:
diff changeset
340 if (ptrace_attach(pid) != true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 free(ph);
a61af66fc99e Initial load
duke
parents:
diff changeset
342 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
343 }
a61af66fc99e Initial load
duke
parents:
diff changeset
344
a61af66fc99e Initial load
duke
parents:
diff changeset
345 // initialize ps_prochandle
a61af66fc99e Initial load
duke
parents:
diff changeset
346 ph->pid = pid;
a61af66fc99e Initial load
duke
parents:
diff changeset
347
a61af66fc99e Initial load
duke
parents:
diff changeset
348 // initialize vtable
a61af66fc99e Initial load
duke
parents:
diff changeset
349 ph->ops = &process_ops;
a61af66fc99e Initial load
duke
parents:
diff changeset
350
a61af66fc99e Initial load
duke
parents:
diff changeset
351 // read library info and symbol tables, must do this before attaching threads,
a61af66fc99e Initial load
duke
parents:
diff changeset
352 // as the symbols in the pthread library will be used to figure out
a61af66fc99e Initial load
duke
parents:
diff changeset
353 // the list of threads within the same process.
a61af66fc99e Initial load
duke
parents:
diff changeset
354 read_lib_info(ph);
a61af66fc99e Initial load
duke
parents:
diff changeset
355
a61af66fc99e Initial load
duke
parents:
diff changeset
356 // read thread info
a61af66fc99e Initial load
duke
parents:
diff changeset
357 read_thread_info(ph, add_new_thread);
a61af66fc99e Initial load
duke
parents:
diff changeset
358
a61af66fc99e Initial load
duke
parents:
diff changeset
359 // attach to the threads
a61af66fc99e Initial load
duke
parents:
diff changeset
360 thr = ph->threads;
a61af66fc99e Initial load
duke
parents:
diff changeset
361 while (thr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // don't attach to the main thread again
a61af66fc99e Initial load
duke
parents:
diff changeset
363 if (ph->pid != thr->lwp_id && ptrace_attach(thr->lwp_id) != true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
364 // even if one attach fails, we get return NULL
a61af66fc99e Initial load
duke
parents:
diff changeset
365 Prelease(ph);
a61af66fc99e Initial load
duke
parents:
diff changeset
366 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
367 }
a61af66fc99e Initial load
duke
parents:
diff changeset
368 thr = thr->next;
a61af66fc99e Initial load
duke
parents:
diff changeset
369 }
a61af66fc99e Initial load
duke
parents:
diff changeset
370 return ph;
a61af66fc99e Initial load
duke
parents:
diff changeset
371 }