agent/src/os/linux/ps_proc.c

Wed, 13 Feb 2013 09:46:19 +0100

author
rbackman
date
Wed, 13 Feb 2013 09:46:19 +0100
changeset 4599
2394a89e89f4
parent 2384
0a8e0d4345b3
child 5797
f2512d89ad0c
permissions
-rw-r--r--

8008088: SA can hang the VM
Reviewed-by: mgronlun, sla, dholmes

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

mercurial