agent/src/os/linux/ps_proc.c

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 5797
f2512d89ad0c
parent 0
f90c822e73f8
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial