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