src/jdk.hotspot.agent/linux/native/libsaproc/ps_proc.c
author ihse
Wed, 28 Mar 2018 23:56:08 +0200
changeset 49440 396ea30afbd5
parent 47216 71c04702a3d5
child 51203 220c9188db4f
child 56721 01b558efd286
permissions -rw-r--r--
8200178: Remove mapfiles for JDK native libraries Reviewed-by: erikj, alanb, mchung, prr, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
49440
396ea30afbd5 8200178: Remove mapfiles for JDK native libraries
ihse
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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>
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
    28
#include <signal.h>
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
#include <errno.h>
28924
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
    30
#include <elf.h>
20295
a5dd1b071c32 8025613: clang: remove -Wno-unused-value
twisti
parents: 15734
diff changeset
    31
#include <sys/types.h>
a5dd1b071c32 8025613: clang: remove -Wno-unused-value
twisti
parents: 15734
diff changeset
    32
#include <sys/wait.h>
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
#include <sys/ptrace.h>
28924
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
    34
#include <sys/uio.h>
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#include "libproc_impl.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
#if defined(x86_64) && !defined(amd64)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
#define amd64 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
#ifndef __WALL
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
#define __WALL          0x40000000  // Copied from /usr/include/linux/wait.h
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
// This file has the libproc implementation specific to live process
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
// For core files, refer to ps_core.c
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
static inline uintptr_t align(uintptr_t ptr, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  return (ptr & ~(size - 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
// ---------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
// ptrace functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
// ---------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
// read "size" bytes of data from "addr" within the target process.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
// unlike the standard ptrace() function, process_read_data() can handle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
// unaligned address - alignment check, if required, should be done
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
// before calling process_read_data.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
static bool process_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  long rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  size_t i, words;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  uintptr_t end_addr = addr + size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  uintptr_t aligned_addr = align(addr, sizeof(long));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  if (aligned_addr != addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    char *ptr = (char *)&rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    errno = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    if (errno) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
      print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    for (; aligned_addr != addr; aligned_addr++, ptr++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    for (; ((intptr_t)aligned_addr % sizeof(long)) && aligned_addr < end_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
        aligned_addr++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
       *(buf++) = *(ptr++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  words = (end_addr - aligned_addr) / sizeof(long);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  // assert((intptr_t)aligned_addr % sizeof(long) == 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  for (i = 0; i < words; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    errno = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    if (errno) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    *(long *)buf = rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    buf += sizeof(long);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    aligned_addr += sizeof(long);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  if (aligned_addr != end_addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    char *ptr = (char *)&rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    errno = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    if (errno) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
      print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    for (; aligned_addr != end_addr; aligned_addr++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
       *(buf++) = *(ptr++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
// null implementation for write
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
static bool process_write_data(struct ps_prochandle* ph,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
                             uintptr_t addr, const char *buf , size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
// "user" should be a pointer to a user_regs_struct
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
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
   118
  // we have already attached to all thread 'pid's, just use ptrace call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  // to get regset now. Note that we don't cache regset upfront for processes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
// Linux on x86 and sparc are different.  On x86 ptrace(PTRACE_GETREGS, ...)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
// uses pointer from 4th argument and ignores 3rd argument.  On sparc it uses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
// pointer from 3rd argument and ignores 4th argument
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
#if defined(sparc) || defined(sparcv9)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
#define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, addr, data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
#define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, data, addr)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
7415
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   129
#if defined(_LP64) && defined(PTRACE_GETREGS64)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
#define PTRACE_GETREGS_REQ PTRACE_GETREGS64
7415
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   131
#elif defined(PTRACE_GETREGS)
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   132
#define PTRACE_GETREGS_REQ PTRACE_GETREGS
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   133
#elif defined(PT_GETREGS)
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   134
#define PTRACE_GETREGS_REQ PT_GETREGS
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
#ifdef PTRACE_GETREGS_REQ
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
 if (ptrace_getregs(PTRACE_GETREGS_REQ, pid, user, NULL) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
   print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
 }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
 return true;
28924
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   143
#elif defined(PTRACE_GETREGSET)
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   144
 struct iovec iov;
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   145
 iov.iov_base = user;
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   146
 iov.iov_len = sizeof(*user);
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   147
 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void*) &iov) < 0) {
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   148
   print_debug("ptrace(PTRACE_GETREGSET, ...) failed for lwp %d\n", pid);
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   149
   return false;
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   150
 }
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   151
 return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
 print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
 return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   159
static bool ptrace_continue(pid_t pid, int signal) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   160
  // pass the signal to the process so we don't swallow it
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   161
  if (ptrace(PTRACE_CONT, pid, NULL, signal) < 0) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   162
    print_debug("ptrace(PTRACE_CONT, ..) failed for %d\n", pid);
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   163
    return false;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   164
  }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   165
  return true;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   166
}
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   167
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   168
// waits until the ATTACH has stopped the process
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   169
// by signal SIGSTOP
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   170
static bool ptrace_waitpid(pid_t pid) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   171
  int ret;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   172
  int status;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   173
  while (true) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   174
    // Wait for debuggee to stop.
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   175
    ret = waitpid(pid, &status, 0);
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   176
    if (ret == -1 && errno == ECHILD) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   177
      // try cloned process.
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   178
      ret = waitpid(pid, &status, __WALL);
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   179
    }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   180
    if (ret >= 0) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   181
      if (WIFSTOPPED(status)) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   182
        // Any signal will stop the thread, make sure it is SIGSTOP. Otherwise SIGSTOP
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   183
        // will still be pending and delivered when the process is DETACHED and the process
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   184
        // will go to sleep.
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   185
        if (WSTOPSIG(status) == SIGSTOP) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   186
          // Debuggee stopped by SIGSTOP.
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   187
          return true;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   188
        }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   189
        if (!ptrace_continue(pid, WSTOPSIG(status))) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   190
          print_error("Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   191
          return false;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   192
        }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   193
      } else {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   194
        print_debug("waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   195
        return false;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   196
      }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   197
    } else {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   198
      switch (errno) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   199
        case EINTR:
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   200
          continue;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   201
          break;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   202
        case ECHILD:
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   203
          print_debug("waitpid() failed. Child process pid (%d) does not exist \n", pid);
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   204
          break;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   205
        case EINVAL:
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   206
          print_debug("waitpid() failed. Invalid options argument.\n");
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   207
          break;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   208
        default:
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   209
          print_debug("waitpid() failed. Unexpected error %d\n",errno);
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   210
          break;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   211
      }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   212
      return false;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   213
    }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   214
  }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   215
}
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   216
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
// attach to a process/thread specified by "pid"
35049
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   218
static bool ptrace_attach(pid_t pid, char* err_buf, size_t err_buf_len) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
35049
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   220
    char buf[200];
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   221
    char* msg = strerror_r(errno, buf, sizeof(buf));
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   222
    snprintf(err_buf, err_buf_len, "ptrace(PTRACE_ATTACH, ..) failed for %d: %s", pid, msg);
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   223
    print_debug("%s\n", err_buf);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  } else {
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   226
    return ptrace_waitpid(pid);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
// -------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
// functions for obtaining library information
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
// -------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
 * 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
   236
 * 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
   237
 * RETURN VALUE: total number of substrings (always <= _n_)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
 * NOTE: string _str_ is modified if _delim_!=_new_delim_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
static int split_n_str(char * str, int n, char ** ptrs, char delim, char new_delim)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
   int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
   for(i = 0; i < n; i++) ptrs[i] = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
   if (str == NULL || n < 1 ) return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
   i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
   // skipping leading blanks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
   while(*str&&*str==delim) str++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
   while(*str&&i<n){
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
     ptrs[i++] = str;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
     while(*str&&*str!=delim) str++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
     while(*str&&*str==delim) *(str++) = new_delim;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
   return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
 * fgets without storing '\n' at the end of the string
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
static char * fgets_no_cr(char * buf, int n, FILE *fp)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
   char * rslt = fgets(buf, n, fp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
   if (rslt && buf && *buf){
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
       char *p = strchr(buf, '\0');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
       if (*--p=='\n') *p='\0';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
   return rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
// callback for read_thread_info
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
static bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  return add_thread_info(ph, pthread_id, lwp_id) != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
static bool read_lib_info(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  char fname[32];
25062
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   280
  char buf[PATH_MAX];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  FILE *fp = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  sprintf(fname, "/proc/%d/maps", ph->pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  fp = fopen(fname, "r");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  if (fp == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    print_debug("can't open /proc/%d/maps file\n", ph->pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
25062
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   290
  while(fgets_no_cr(buf, PATH_MAX, fp)){
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   291
    char * word[7];
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   292
    int nwords = split_n_str(buf, 7, word, ' ', '\0');
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   293
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   294
    if (nwords < 6) {
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   295
      // not a shared library entry. ignore.
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   296
      continue;
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   297
    }
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   298
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   299
    // SA does not handle the lines with patterns:
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   300
    //   "[stack]", "[heap]", "[vdso]", "[vsyscall]", etc.
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   301
    if (word[5][0] == '[') {
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   302
        // not a shared library entry. ignore.
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   303
        continue;
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   304
    }
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   305
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   306
    if (nwords > 6) {
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   307
      // prelink altered mapfile when the program is running.
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   308
      // Entries like one below have to be skipped
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   309
      //  /lib64/libc-2.15.so (deleted)
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   310
      // SO name in entries like one below have to be stripped.
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   311
      //  /lib64/libpthread-2.15.so.#prelink#.EECVts
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   312
      char *s = strstr(word[5],".#prelink#");
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   313
      if (s == NULL) {
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   314
        // No prelink keyword. skip deleted library
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   315
        print_debug("skip shared object %s deleted by prelink\n", word[5]);
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   316
        continue;
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   317
      }
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   318
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   319
      // Fall through
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   320
      print_debug("rectifying shared object name %s changed by prelink\n", word[5]);
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   321
      *s = 0;
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   322
    }
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   323
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   324
    if (find_lib(ph, word[5]) == false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
       intptr_t base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
       lib_info* lib;
6176
4d9030fe341f 6953477: Increase portability and flexibility of building Hotspot
bobv
parents: 5547
diff changeset
   327
#ifdef _LP64
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
       sscanf(word[0], "%lx", &base);
6176
4d9030fe341f 6953477: Increase portability and flexibility of building Hotspot
bobv
parents: 5547
diff changeset
   329
#else
4d9030fe341f 6953477: Increase portability and flexibility of building Hotspot
bobv
parents: 5547
diff changeset
   330
       sscanf(word[0], "%x", &base);
4d9030fe341f 6953477: Increase portability and flexibility of building Hotspot
bobv
parents: 5547
diff changeset
   331
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
       if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
          continue; // ignore, add_lib_info prints error
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
       // we don't need to keep the library open, symtab is already
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
       // built. Only for core dump we need to keep the fd open.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
       close(lib->fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
       lib->fd = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
  fclose(fp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
// detach a given pid
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
static bool ptrace_detach(pid_t pid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  if (pid && ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
// detach all pids of a ps_prochandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
static void detach_all_pids(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  thread_info* thr = ph->threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  while (thr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
     ptrace_detach(thr->lwp_id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
     thr = thr->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
static void process_cleanup(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  detach_all_pids(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
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
   369
  .release=  process_cleanup,
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   370
  .p_pread=  process_read_data,
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   371
  .p_pwrite= process_write_data,
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   372
  .get_lwp_regs= process_get_lwp_regs
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
// attach to the process. One and only one exposed stuff
49440
396ea30afbd5 8200178: Remove mapfiles for JDK native libraries
ihse
parents: 47216
diff changeset
   376
JNIEXPORT struct ps_prochandle* JNICALL
396ea30afbd5 8200178: Remove mapfiles for JDK native libraries
ihse
parents: 47216
diff changeset
   377
Pgrab(pid_t pid, char* err_buf, size_t err_buf_len) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  struct ps_prochandle* ph = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  thread_info* thr = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
  if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
35049
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   382
     snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle");
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   383
     print_debug("%s\n", err_buf);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
     return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
35049
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   387
  if (ptrace_attach(pid, err_buf, err_buf_len) != true) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
     free(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
     return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  // initialize ps_prochandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  ph->pid = pid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  // initialize vtable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  ph->ops = &process_ops;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  // read library info and symbol tables, must do this before attaching threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
  // as the symbols in the pthread library will be used to figure out
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  // the list of threads within the same process.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  read_lib_info(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  // read thread info
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  read_thread_info(ph, add_new_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  // attach to the threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  thr = ph->threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  while (thr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
     // don't attach to the main thread again
35049
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   410
    if (ph->pid != thr->lwp_id && ptrace_attach(thr->lwp_id, err_buf, err_buf_len) != true) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
        // even if one attach fails, we get return NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
        Prelease(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
        return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
     thr = thr->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  return ph;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
}