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