1 /*
   2  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include <stdio.h>
  26 #include <stdlib.h>
  27 #include <string.h>
  28 #include <signal.h>
  29 #include <errno.h>
  30 #include <elf.h>
  31 #include <dirent.h>
  32 #include <ctype.h>
  33 #include <sys/types.h>
  34 #include <sys/wait.h>
  35 #include <sys/ptrace.h>
  36 #include <sys/uio.h>
  37 #include "libproc_impl.h"
  38 
  39 #if defined(x86_64) && !defined(amd64)
  40 #define amd64 1
  41 #endif
  42 
  43 #ifndef __WALL
  44 #define __WALL          0x40000000  // Copied from /usr/include/linux/wait.h
  45 #endif
  46 
  47 // This file has the libproc implementation specific to live process
  48 // For core files, refer to ps_core.c
  49 
  50 typedef enum {
  51   ATTACH_SUCCESS,
  52   ATTACH_FAIL,
  53   ATTACH_THREAD_DEAD
  54 } attach_state_t;
  55 
  56 static inline uintptr_t align(uintptr_t ptr, size_t size) {
  57   return (ptr & ~(size - 1));
  58 }
  59 
  60 // ---------------------------------------------
  61 // ptrace functions
  62 // ---------------------------------------------
  63 
  64 // read "size" bytes of data from "addr" within the target process.
  65 // unlike the standard ptrace() function, process_read_data() can handle
  66 // unaligned address - alignment check, if required, should be done
  67 // before calling process_read_data.
  68 
  69 static bool process_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
  70   long rslt;
  71   size_t i, words;
  72   uintptr_t end_addr = addr + size;
  73   uintptr_t aligned_addr = align(addr, sizeof(long));
  74 
  75   if (aligned_addr != addr) {
  76     char *ptr = (char *)&rslt;
  77     errno = 0;
  78     rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
  79     if (errno) {
  80       print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
  81       return false;
  82     }
  83     for (; aligned_addr != addr; aligned_addr++, ptr++);
  84     for (; ((intptr_t)aligned_addr % sizeof(long)) && aligned_addr < end_addr;
  85         aligned_addr++)
  86        *(buf++) = *(ptr++);
  87   }
  88 
  89   words = (end_addr - aligned_addr) / sizeof(long);
  90 
  91   // assert((intptr_t)aligned_addr % sizeof(long) == 0);
  92   for (i = 0; i < words; i++) {
  93     errno = 0;
  94     rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
  95     if (errno) {
  96       print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
  97       return false;
  98     }
  99     *(long *)buf = rslt;
 100     buf += sizeof(long);
 101     aligned_addr += sizeof(long);
 102   }
 103 
 104   if (aligned_addr != end_addr) {
 105     char *ptr = (char *)&rslt;
 106     errno = 0;
 107     rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
 108     if (errno) {
 109       print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
 110       return false;
 111     }
 112     for (; aligned_addr != end_addr; aligned_addr++)
 113        *(buf++) = *(ptr++);
 114   }
 115   return true;
 116 }
 117 
 118 // null implementation for write
 119 static bool process_write_data(struct ps_prochandle* ph,
 120                              uintptr_t addr, const char *buf , size_t size) {
 121   return false;
 122 }
 123 
 124 // "user" should be a pointer to a user_regs_struct
 125 static bool process_get_lwp_regs(struct ps_prochandle* ph, pid_t pid, struct user_regs_struct *user) {
 126   // we have already attached to all thread 'pid's, just use ptrace call
 127   // to get regset now. Note that we don't cache regset upfront for processes.
 128 // Linux on x86 and sparc are different.  On x86 ptrace(PTRACE_GETREGS, ...)
 129 // uses pointer from 4th argument and ignores 3rd argument.  On sparc it uses
 130 // pointer from 3rd argument and ignores 4th argument
 131 #if defined(sparc) || defined(sparcv9)
 132 #define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, addr, data)
 133 #else
 134 #define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, data, addr)
 135 #endif
 136 
 137 #if defined(_LP64) && defined(PTRACE_GETREGS64)
 138 #define PTRACE_GETREGS_REQ PTRACE_GETREGS64
 139 #elif defined(PTRACE_GETREGS)
 140 #define PTRACE_GETREGS_REQ PTRACE_GETREGS
 141 #elif defined(PT_GETREGS)
 142 #define PTRACE_GETREGS_REQ PT_GETREGS
 143 #endif
 144 
 145 #ifdef PTRACE_GETREGS_REQ
 146  if (ptrace_getregs(PTRACE_GETREGS_REQ, pid, user, NULL) < 0) {
 147    print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid);
 148    return false;
 149  }
 150  return true;
 151 #elif defined(PTRACE_GETREGSET)
 152  struct iovec iov;
 153  iov.iov_base = user;
 154  iov.iov_len = sizeof(*user);
 155  if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void*) &iov) < 0) {
 156    print_debug("ptrace(PTRACE_GETREGSET, ...) failed for lwp %d\n", pid);
 157    return false;
 158  }
 159  return true;
 160 #else
 161  print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n");
 162  return false;
 163 #endif
 164 
 165 }
 166 
 167 static bool ptrace_continue(pid_t pid, int signal) {
 168   // pass the signal to the process so we don't swallow it
 169   if (ptrace(PTRACE_CONT, pid, NULL, signal) < 0) {
 170     print_debug("ptrace(PTRACE_CONT, ..) failed for %d\n", pid);
 171     return false;
 172   }
 173   return true;
 174 }
 175 
 176 // waits until the ATTACH has stopped the process
 177 // by signal SIGSTOP
 178 static attach_state_t ptrace_waitpid(pid_t pid) {
 179   int ret;
 180   int status;
 181   errno = 0;
 182   while (true) {
 183     // Wait for debuggee to stop.
 184     ret = waitpid(pid, &status, 0);
 185     if (ret == -1 && errno == ECHILD) {
 186       // try cloned process.
 187       ret = waitpid(pid, &status, __WALL);
 188     }
 189     if (ret >= 0) {
 190       if (WIFSTOPPED(status)) {
 191         // Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP
 192         // will still be pending and delivered when the process is DETACHED and the process
 193         // will go to sleep.
 194         if (WSTOPSIG(status) == SIGSTOP) {
 195           // Debuggee stopped by SIGSTOP.
 196           return ATTACH_SUCCESS;
 197         }
 198         if (!ptrace_continue(pid, WSTOPSIG(status))) {
 199           print_error("Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
 200           return ATTACH_FAIL;
 201         }
 202       } else {
 203         print_debug("waitpid(): Child process %d exited/terminated (status = 0x%x)\n", pid, status);
 204         return ATTACH_THREAD_DEAD;
 205       }
 206     } else {
 207       switch (errno) {
 208         case EINTR:
 209           continue;
 210           break;
 211         case ECHILD:
 212           print_debug("waitpid() failed. Child process pid (%d) does not exist \n", pid);
 213           return ATTACH_THREAD_DEAD;
 214         case EINVAL:
 215           print_error("waitpid() failed. Invalid options argument.\n");
 216           return ATTACH_FAIL;
 217         default:
 218           print_error("waitpid() failed. Unexpected error %d\n", errno);
 219           return ATTACH_FAIL;
 220       }
 221     } // else
 222   } // while
 223 }
 224 
 225 // checks the state of the thread/process specified by "pid", by reading
 226 // in the 'State:' value from the /proc/<pid>/status file. From the proc
 227 // man page, "Current state of the process. One of "R (running)",
 228 // "S (sleeping)", "D (disk sleep)", "T (stopped)", "T (tracing stop)",
 229 // "Z (zombie)", or "X (dead)"." Assumes that the thread is dead if we
 230 // don't find the status file or if the status is 'X' or 'Z'.
 231 static bool process_doesnt_exist(pid_t pid) {
 232   char fname[32];
 233   char buf[30];
 234   FILE *fp = NULL;
 235   const char state_string[] = "State:";
 236 
 237   sprintf(fname, "/proc/%d/status", pid);
 238   fp = fopen(fname, "r");
 239   if (fp == NULL) {
 240     print_debug("can't open /proc/%d/status file\n", pid);
 241     // Assume the thread does not exist anymore.
 242     return true;
 243   }
 244   bool found_state = false;
 245   size_t state_len = strlen(state_string);
 246   while (fgets(buf, sizeof(buf), fp) != NULL) {
 247     char *state = NULL;
 248     if (strncmp (buf, state_string, state_len) == 0) {
 249       found_state = true;
 250       state = buf + state_len;
 251       // Skip the spaces
 252       while (isspace(*state)) {
 253         state++;
 254       }
 255       // A state value of 'X' indicates that the thread is dead. 'Z'
 256       // indicates that the thread is a zombie.
 257       if (*state == 'X' || *state == 'Z') {
 258         fclose (fp);
 259         return true;
 260       }
 261       break;
 262     }
 263   }
 264   // If the state value is not 'X' or 'Z', the thread exists.
 265   if (!found_state) {
 266     // We haven't found the line beginning with 'State:'.
 267     // Assuming the thread exists.
 268     print_error("Could not find the 'State:' string in the /proc/%d/status file\n", pid);
 269   }
 270   fclose (fp);
 271   return false;
 272 }
 273 
 274 // attach to a process/thread specified by "pid"
 275 static attach_state_t ptrace_attach(pid_t pid, char* err_buf, size_t err_buf_len) {
 276   errno = 0;
 277   if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
 278     if (errno == EPERM || errno == ESRCH) {
 279       // Check if the process/thread is exiting or is a zombie
 280       if (process_doesnt_exist(pid)) {
 281         print_debug("Thread with pid %d does not exist\n", pid);
 282         return ATTACH_THREAD_DEAD;
 283       }
 284     }
 285     char buf[200];
 286     if (strerror_r(errno, buf, sizeof(buf) == 0)) {
 287       snprintf(err_buf, err_buf_len, "ptrace(PTRACE_ATTACH, ..) failed for %d: %s", pid, buf);
 288       print_error("%s\n", err_buf);
 289     }
 290     return ATTACH_FAIL;
 291   } else {
 292     attach_state_t wait_ret = ptrace_waitpid(pid);
 293     if (wait_ret == ATTACH_THREAD_DEAD) {
 294       print_debug("Thread with pid %d does not exist\n", pid);
 295     }
 296     return wait_ret;
 297   }
 298 }
 299 
 300 // -------------------------------------------------------
 301 // functions for obtaining library information
 302 // -------------------------------------------------------
 303 
 304 /*
 305  * splits a string _str_ into substrings with delimiter _delim_ by replacing old * delimiters with _new_delim_ (ideally, '\0'). the address of each substring
 306  * is stored in array _ptrs_ as the return value. the maximum capacity of _ptrs_ * array is specified by parameter _n_.
 307  * RETURN VALUE: total number of substrings (always <= _n_)
 308  * NOTE: string _str_ is modified if _delim_!=_new_delim_
 309  */
 310 static int split_n_str(char * str, int n, char ** ptrs, char delim, char new_delim)
 311 {
 312    int i;
 313    for(i = 0; i < n; i++) ptrs[i] = NULL;
 314    if (str == NULL || n < 1 ) return 0;
 315 
 316    i = 0;
 317 
 318    // skipping leading blanks
 319    while(*str&&*str==delim) str++;
 320 
 321    while(*str&&i<n){
 322      ptrs[i++] = str;
 323      while(*str&&*str!=delim) str++;
 324      while(*str&&*str==delim) *(str++) = new_delim;
 325    }
 326 
 327    return i;
 328 }
 329 
 330 /*
 331  * fgets without storing '\n' at the end of the string
 332  */
 333 static char * fgets_no_cr(char * buf, int n, FILE *fp)
 334 {
 335    char * rslt = fgets(buf, n, fp);
 336    if (rslt && buf && *buf){
 337        char *p = strchr(buf, '\0');
 338        if (*--p=='\n') *p='\0';
 339    }
 340    return rslt;
 341 }
 342 
 343 static bool read_lib_info(struct ps_prochandle* ph) {
 344   char fname[32];
 345   char buf[PATH_MAX];
 346   FILE *fp = NULL;
 347 
 348   sprintf(fname, "/proc/%d/maps", ph->pid);
 349   fp = fopen(fname, "r");
 350   if (fp == NULL) {
 351     print_debug("can't open /proc/%d/maps file\n", ph->pid);
 352     return false;
 353   }
 354 
 355   while(fgets_no_cr(buf, PATH_MAX, fp)){
 356     char * word[7];
 357     int nwords = split_n_str(buf, 7, word, ' ', '\0');
 358 
 359     if (nwords < 6) {
 360       // not a shared library entry. ignore.
 361       continue;
 362     }
 363 
 364     // SA does not handle the lines with patterns:
 365     //   "[stack]", "[heap]", "[vdso]", "[vsyscall]", etc.
 366     if (word[5][0] == '[') {
 367         // not a shared library entry. ignore.
 368         continue;
 369     }
 370 
 371     if (nwords > 6) {
 372       // prelink altered mapfile when the program is running.
 373       // Entries like one below have to be skipped
 374       //  /lib64/libc-2.15.so (deleted)
 375       // SO name in entries like one below have to be stripped.
 376       //  /lib64/libpthread-2.15.so.#prelink#.EECVts
 377       char *s = strstr(word[5],".#prelink#");
 378       if (s == NULL) {
 379         // No prelink keyword. skip deleted library
 380         print_debug("skip shared object %s deleted by prelink\n", word[5]);
 381         continue;
 382       }
 383 
 384       // Fall through
 385       print_debug("rectifying shared object name %s changed by prelink\n", word[5]);
 386       *s = 0;
 387     }
 388 
 389     if (find_lib(ph, word[5]) == false) {
 390        intptr_t base;
 391        lib_info* lib;
 392 #ifdef _LP64
 393        sscanf(word[0], "%lx", &base);
 394 #else
 395        sscanf(word[0], "%x", &base);
 396 #endif
 397        if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL)
 398           continue; // ignore, add_lib_info prints error
 399 
 400        // we don't need to keep the library open, symtab is already
 401        // built. Only for core dump we need to keep the fd open.
 402        close(lib->fd);
 403        lib->fd = -1;
 404     }
 405   }
 406   fclose(fp);
 407   return true;
 408 }
 409 
 410 // detach a given pid
 411 static bool ptrace_detach(pid_t pid) {
 412   if (pid && ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) {
 413     print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid);
 414     return false;
 415   } else {
 416     return true;
 417   }
 418 }
 419 
 420 // detach all pids of a ps_prochandle
 421 static void detach_all_pids(struct ps_prochandle* ph) {
 422   thread_info* thr = ph->threads;
 423   while (thr) {
 424      ptrace_detach(thr->lwp_id);
 425      thr = thr->next;
 426   }
 427 }
 428 
 429 static void process_cleanup(struct ps_prochandle* ph) {
 430   detach_all_pids(ph);
 431 }
 432 
 433 static ps_prochandle_ops process_ops = {
 434   .release=  process_cleanup,
 435   .p_pread=  process_read_data,
 436   .p_pwrite= process_write_data,
 437   .get_lwp_regs= process_get_lwp_regs
 438 };
 439 
 440 // attach to the process. One and only one exposed stuff
 441 JNIEXPORT struct ps_prochandle* JNICALL
 442 Pgrab(pid_t pid, char* err_buf, size_t err_buf_len) {
 443   struct ps_prochandle* ph = NULL;
 444   thread_info* thr = NULL;
 445   attach_state_t attach_status = ATTACH_SUCCESS;
 446 
 447   if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
 448     snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle");
 449     print_debug("%s\n", err_buf);
 450     return NULL;
 451   }
 452 
 453   if ((attach_status = ptrace_attach(pid, err_buf, err_buf_len)) != ATTACH_SUCCESS) {
 454     if (attach_status == ATTACH_THREAD_DEAD) {
 455        print_error("The process with pid %d does not exist.\n", pid);
 456     }
 457     free(ph);
 458     return NULL;
 459   }
 460 
 461   // initialize ps_prochandle
 462   ph->pid = pid;
 463   add_thread_info(ph, ph->pid);
 464 
 465   // initialize vtable
 466   ph->ops = &process_ops;
 467 
 468   // read library info and symbol tables, must do this before attaching threads,
 469   // as the symbols in the pthread library will be used to figure out
 470   // the list of threads within the same process.
 471   read_lib_info(ph);
 472 
 473   /*
 474    * Read thread info.
 475    * SA scans all tasks in /proc/<PID>/task to read all threads info.
 476    */
 477   char taskpath[PATH_MAX];
 478   DIR *dirp;
 479   struct dirent *entry;
 480 
 481   snprintf(taskpath, PATH_MAX, "/proc/%d/task", ph->pid);
 482   dirp = opendir(taskpath);
 483   int lwp_id;
 484   while ((entry = readdir(dirp)) != NULL) {
 485     if (*entry->d_name == '.') {
 486       continue;
 487     }
 488     lwp_id = atoi(entry->d_name);
 489     if (lwp_id == ph->pid) {
 490       continue;
 491     }
 492     if (!process_doesnt_exist(lwp_id)) {
 493       add_thread_info(ph, lwp_id);
 494     }
 495   }
 496   closedir(dirp);
 497 
 498   // attach to the threads
 499   thr = ph->threads;
 500 
 501   while (thr) {
 502     thread_info* current_thr = thr;
 503     thr = thr->next;
 504     // don't attach to the main thread again
 505     if (ph->pid != current_thr->lwp_id) {
 506       if ((attach_status = ptrace_attach(current_thr->lwp_id, err_buf, err_buf_len)) != ATTACH_SUCCESS) {
 507         if (attach_status == ATTACH_THREAD_DEAD) {
 508           // Remove this thread from the threads list
 509           delete_thread_info(ph, current_thr);
 510         }
 511         else {
 512           Prelease(ph);
 513           return NULL;
 514         } // ATTACH_THREAD_DEAD
 515       } // !ATTACH_SUCCESS
 516     }
 517   }
 518   return ph;
 519 }