annotate agent/src/os/linux/ps_proc.c @ 50:485d403e94e1

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