src/jdk.hotspot.agent/linux/native/libsaproc/ps_proc.c
author jgeorge
Thu, 13 Dec 2018 13:03:26 +0530
changeset 53004 b9d34a97a4be
parent 51203 220c9188db4f
child 53379 e47074d2d8cc
permissions -rw-r--r--
8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching Summary: While doing a ptrace attach, do not attach to threads which are in the process of exiting or are zombies -- skip these threads. Reviewed-by: jcbeyler, ysuenaga
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>
51203
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
    31
#include <dirent.h>
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
    32
#include <ctype.h>
20295
a5dd1b071c32 8025613: clang: remove -Wno-unused-value
twisti
parents: 15734
diff changeset
    33
#include <sys/types.h>
a5dd1b071c32 8025613: clang: remove -Wno-unused-value
twisti
parents: 15734
diff changeset
    34
#include <sys/wait.h>
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#include <sys/ptrace.h>
28924
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
    36
#include <sys/uio.h>
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
#include "libproc_impl.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
#if defined(x86_64) && !defined(amd64)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
#define amd64 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
#ifndef __WALL
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
#define __WALL          0x40000000  // Copied from /usr/include/linux/wait.h
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
// This file has the libproc implementation specific to live process
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
// For core files, refer to ps_core.c
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
    50
typedef enum {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
    51
  ATTACH_SUCCESS,
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
    52
  ATTACH_FAIL,
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
    53
  ATTACH_THREAD_DEAD
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
    54
} attach_state_t;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
    55
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
static inline uintptr_t align(uintptr_t ptr, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  return (ptr & ~(size - 1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
// ---------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// ptrace functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
// ---------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
// read "size" bytes of data from "addr" within the target process.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
// unlike the standard ptrace() function, process_read_data() can handle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
// unaligned address - alignment check, if required, should be done
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
// before calling process_read_data.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
static bool process_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  long rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  size_t i, words;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  uintptr_t end_addr = addr + size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  uintptr_t aligned_addr = align(addr, sizeof(long));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  if (aligned_addr != addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    char *ptr = (char *)&rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
    errno = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    if (errno) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
      print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    for (; aligned_addr != addr; aligned_addr++, ptr++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    for (; ((intptr_t)aligned_addr % sizeof(long)) && aligned_addr < end_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
        aligned_addr++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
       *(buf++) = *(ptr++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  words = (end_addr - aligned_addr) / sizeof(long);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  // assert((intptr_t)aligned_addr % sizeof(long) == 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  for (i = 0; i < words; i++) {
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
    *(long *)buf = rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    buf += sizeof(long);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    aligned_addr += sizeof(long);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  if (aligned_addr != end_addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    char *ptr = (char *)&rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    errno = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    rslt = ptrace(PTRACE_PEEKDATA, ph->pid, aligned_addr, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    if (errno) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
      print_debug("ptrace(PTRACE_PEEKDATA, ..) failed for %d bytes @ %lx\n", size, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    for (; aligned_addr != end_addr; aligned_addr++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
       *(buf++) = *(ptr++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
// null implementation for write
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
static bool process_write_data(struct ps_prochandle* ph,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
                             uintptr_t addr, const char *buf , size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
// "user" should be a pointer to a user_regs_struct
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
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
   126
  // we have already attached to all thread 'pid's, just use ptrace call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  // to get regset now. Note that we don't cache regset upfront for processes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
// Linux on x86 and sparc are different.  On x86 ptrace(PTRACE_GETREGS, ...)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
// uses pointer from 4th argument and ignores 3rd argument.  On sparc it uses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
// pointer from 3rd argument and ignores 4th argument
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
#if defined(sparc) || defined(sparcv9)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
#define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, addr, data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
#define ptrace_getregs(request, pid, addr, data) ptrace(request, pid, data, addr)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
7415
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   137
#if defined(_LP64) && defined(PTRACE_GETREGS64)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
#define PTRACE_GETREGS_REQ PTRACE_GETREGS64
7415
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   139
#elif defined(PTRACE_GETREGS)
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   140
#define PTRACE_GETREGS_REQ PTRACE_GETREGS
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   141
#elif defined(PT_GETREGS)
e7c6833aac14 7003789: PTRACE_GETREGS problems with SA on Linux.
kevinw
parents: 6176
diff changeset
   142
#define PTRACE_GETREGS_REQ PT_GETREGS
1
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
#ifdef PTRACE_GETREGS_REQ
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
 if (ptrace_getregs(PTRACE_GETREGS_REQ, pid, user, NULL) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
   print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
 }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
 return true;
28924
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   151
#elif defined(PTRACE_GETREGSET)
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   152
 struct iovec iov;
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   153
 iov.iov_base = user;
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   154
 iov.iov_len = sizeof(*user);
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   155
 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void*) &iov) < 0) {
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   156
   print_debug("ptrace(PTRACE_GETREGSET, ...) failed for lwp %d\n", pid);
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   157
   return false;
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   158
 }
8943e95a2fb3 8069030: support new PTRACE_GETREGSET
dlong
parents: 25062
diff changeset
   159
 return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
 print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
 return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   167
static bool ptrace_continue(pid_t pid, int signal) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   168
  // pass the signal to the process so we don't swallow it
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   169
  if (ptrace(PTRACE_CONT, pid, NULL, signal) < 0) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   170
    print_debug("ptrace(PTRACE_CONT, ..) failed for %d\n", pid);
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   171
    return false;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   172
  }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   173
  return true;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   174
}
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   175
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   176
// waits until the ATTACH has stopped the process
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   177
// by signal SIGSTOP
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   178
static attach_state_t ptrace_waitpid(pid_t pid) {
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   179
  int ret;
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   180
  int status;
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   181
  errno = 0;
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   182
  while (true) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   183
    // Wait for debuggee to stop.
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   184
    ret = waitpid(pid, &status, 0);
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   185
    if (ret == -1 && errno == ECHILD) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   186
      // try cloned process.
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   187
      ret = waitpid(pid, &status, __WALL);
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 (ret >= 0) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   190
      if (WIFSTOPPED(status)) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   191
        // 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
   192
        // 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
   193
        // will go to sleep.
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   194
        if (WSTOPSIG(status) == SIGSTOP) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   195
          // Debuggee stopped by SIGSTOP.
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   196
          return ATTACH_SUCCESS;
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   197
        }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   198
        if (!ptrace_continue(pid, WSTOPSIG(status))) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   199
          print_error("Failed to correctly attach to VM. VM might HANG! [PTRACE_CONT failed, stopped by %d]\n", WSTOPSIG(status));
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   200
          return ATTACH_FAIL;
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   201
        }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   202
      } else {
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   203
        print_debug("waitpid(): Child process %d exited/terminated (status = 0x%x)\n", pid, status);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   204
        return ATTACH_THREAD_DEAD;
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   205
      }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   206
    } else {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   207
      switch (errno) {
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   208
        case EINTR:
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   209
          continue;
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
        case ECHILD:
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   212
          print_debug("waitpid() failed. Child process pid (%d) does not exist \n", pid);
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   213
          return ATTACH_THREAD_DEAD;
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   214
        case EINVAL:
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   215
          print_error("waitpid() failed. Invalid options argument.\n");
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   216
          return ATTACH_FAIL;
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   217
        default:
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   218
          print_error("waitpid() failed. Unexpected error %d\n", errno);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   219
          return ATTACH_FAIL;
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   220
      }
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   221
    } // else
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   222
  } // while
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   223
}
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   224
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   225
// checks the state of the thread/process specified by "pid", by reading
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   226
// in the 'State:' value from the /proc/<pid>/status file. From the proc
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   227
// man page, "Current state of the process. One of "R (running)",
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   228
// "S (sleeping)", "D (disk sleep)", "T (stopped)", "T (tracing stop)",
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   229
// "Z (zombie)", or "X (dead)"." Assumes that the thread is dead if we
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   230
// don't find the status file or if the status is 'X' or 'Z'.
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   231
static bool process_doesnt_exist(pid_t pid) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   232
  char fname[32];
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   233
  char buf[30];
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   234
  FILE *fp = NULL;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   235
  const char state_string[] = "State:";
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   236
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   237
  sprintf(fname, "/proc/%d/status", pid);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   238
  fp = fopen(fname, "r");
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   239
  if (fp == NULL) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   240
    print_debug("can't open /proc/%d/status file\n", pid);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   241
    // Assume the thread does not exist anymore.
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   242
    return true;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   243
  }
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   244
  bool found_state = false;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   245
  size_t state_len = strlen(state_string);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   246
  while (fgets(buf, sizeof(buf), fp) != NULL) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   247
    char *state = NULL;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   248
    if (strncmp (buf, state_string, state_len) == 0) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   249
      found_state = true;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   250
      state = buf + state_len;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   251
      // Skip the spaces
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   252
      while (isspace(*state)) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   253
        state++;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   254
      }
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   255
      // A state value of 'X' indicates that the thread is dead. 'Z'
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   256
      // indicates that the thread is a zombie.
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   257
      if (*state == 'X' || *state == 'Z') {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   258
        fclose (fp);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   259
        return true;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   260
      }
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   261
      break;
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   262
    }
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   263
  }
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   264
  // If the state value is not 'X' or 'Z', the thread exists.
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   265
  if (!found_state) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   266
    // We haven't found the line beginning with 'State:'.
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   267
    // Assuming the thread exists.
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   268
    print_error("Could not find the 'State:' string in the /proc/%d/status file\n", pid);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   269
  }
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   270
  fclose (fp);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   271
  return false;
15734
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   272
}
68f78eb98356 8008088: SA can hang the VM
rbackman
parents: 7662
diff changeset
   273
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
// attach to a process/thread specified by "pid"
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   275
static attach_state_t ptrace_attach(pid_t pid, char* err_buf, size_t err_buf_len) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   276
  errno = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  if (ptrace(PTRACE_ATTACH, pid, NULL, NULL) < 0) {
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   278
    if (errno == EPERM || errno == ESRCH) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   279
      // Check if the process/thread is exiting or is a zombie
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   280
      if (process_doesnt_exist(pid)) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   281
        print_debug("Thread with pid %d does not exist\n", pid);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   282
        return ATTACH_THREAD_DEAD;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   283
      }
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   284
    }
35049
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   285
    char buf[200];
5a30aa672689 8145099: Better error message when SA can't attach to a process
sla
parents: 28924
diff changeset
   286
    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
   287
    snprintf(err_buf, err_buf_len, "ptrace(PTRACE_ATTACH, ..) failed for %d: %s", pid, msg);
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   288
    print_error("%s\n", err_buf);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   289
    return ATTACH_FAIL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  } else {
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   291
    attach_state_t wait_ret = ptrace_waitpid(pid);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   292
    if (wait_ret == ATTACH_THREAD_DEAD) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   293
      print_debug("Thread with pid %d does not exist\n", pid);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   294
    }
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   295
    return wait_ret;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
// -------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
// functions for obtaining library information
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
// -------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
 * 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
   305
 * 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
   306
 * RETURN VALUE: total number of substrings (always <= _n_)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
 * NOTE: string _str_ is modified if _delim_!=_new_delim_
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
static int split_n_str(char * str, int n, char ** ptrs, char delim, char new_delim)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
   int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
   for(i = 0; i < n; i++) ptrs[i] = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
   if (str == NULL || n < 1 ) return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
   i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
   // skipping leading blanks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
   while(*str&&*str==delim) str++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
   while(*str&&i<n){
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
     ptrs[i++] = str;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
     while(*str&&*str!=delim) str++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
     while(*str&&*str==delim) *(str++) = new_delim;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
   return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
 * fgets without storing '\n' at the end of the string
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
static char * fgets_no_cr(char * buf, int n, FILE *fp)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
   char * rslt = fgets(buf, n, fp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
   if (rslt && buf && *buf){
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
       char *p = strchr(buf, '\0');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
       if (*--p=='\n') *p='\0';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
   return rslt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
// callback for read_thread_info
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
static bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  return add_thread_info(ph, pthread_id, lwp_id) != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
static bool read_lib_info(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  char fname[32];
25062
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   349
  char buf[PATH_MAX];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  FILE *fp = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  sprintf(fname, "/proc/%d/maps", ph->pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  fp = fopen(fname, "r");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  if (fp == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
    print_debug("can't open /proc/%d/maps file\n", ph->pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
25062
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   359
  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
   360
    char * word[7];
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   361
    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
   362
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   363
    if (nwords < 6) {
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   364
      // not a shared library entry. ignore.
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   365
      continue;
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   366
    }
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   367
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   368
    // 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
   369
    //   "[stack]", "[heap]", "[vdso]", "[vsyscall]", etc.
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   370
    if (word[5][0] == '[') {
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   371
        // not a shared library entry. ignore.
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   372
        continue;
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   373
    }
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   374
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   375
    if (nwords > 6) {
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   376
      // 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
   377
      // 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
   378
      //  /lib64/libc-2.15.so (deleted)
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   379
      // 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
   380
      //  /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
   381
      char *s = strstr(word[5],".#prelink#");
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   382
      if (s == NULL) {
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   383
        // No prelink keyword. skip deleted library
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   384
        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
   385
        continue;
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   386
      }
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   387
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   388
      // Fall through
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   389
      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
   390
      *s = 0;
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   391
    }
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   392
d38c0d15b3f7 8038392: Generating prelink cache breaks JAVA &#39;jinfo&#39; utility normal behaviour
dsamersoff
parents: 20295
diff changeset
   393
    if (find_lib(ph, word[5]) == false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
       intptr_t base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
       lib_info* lib;
6176
4d9030fe341f 6953477: Increase portability and flexibility of building Hotspot
bobv
parents: 5547
diff changeset
   396
#ifdef _LP64
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
       sscanf(word[0], "%lx", &base);
6176
4d9030fe341f 6953477: Increase portability and flexibility of building Hotspot
bobv
parents: 5547
diff changeset
   398
#else
4d9030fe341f 6953477: Increase portability and flexibility of building Hotspot
bobv
parents: 5547
diff changeset
   399
       sscanf(word[0], "%x", &base);
4d9030fe341f 6953477: Increase portability and flexibility of building Hotspot
bobv
parents: 5547
diff changeset
   400
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
       if ((lib = add_lib_info(ph, word[5], (uintptr_t)base)) == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
          continue; // ignore, add_lib_info prints error
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
       // we don't need to keep the library open, symtab is already
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
       // built. Only for core dump we need to keep the fd open.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
       close(lib->fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
       lib->fd = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  fclose(fp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
// detach a given pid
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
static bool ptrace_detach(pid_t pid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  if (pid && ptrace(PTRACE_DETACH, pid, NULL, NULL) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
    print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
// detach all pids of a ps_prochandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
static void detach_all_pids(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  thread_info* thr = ph->threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  while (thr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
     ptrace_detach(thr->lwp_id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
     thr = thr->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
static void process_cleanup(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  detach_all_pids(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
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
   438
  .release=  process_cleanup,
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   439
  .p_pread=  process_read_data,
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   440
  .p_pwrite= process_write_data,
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   441
  .get_lwp_regs= process_get_lwp_regs
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
// attach to the process. One and only one exposed stuff
49440
396ea30afbd5 8200178: Remove mapfiles for JDK native libraries
ihse
parents: 47216
diff changeset
   445
JNIEXPORT struct ps_prochandle* JNICALL
51203
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   446
Pgrab(pid_t pid, char* err_buf, size_t err_buf_len, bool is_in_container) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  struct ps_prochandle* ph = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  thread_info* thr = NULL;
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   449
  attach_state_t attach_status = ATTACH_SUCCESS;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   452
    snprintf(err_buf, err_buf_len, "can't allocate memory for ps_prochandle");
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   453
    print_debug("%s\n", err_buf);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   454
    return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   457
  if ((attach_status = ptrace_attach(pid, err_buf, err_buf_len)) != ATTACH_SUCCESS) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   458
    if (attach_status == ATTACH_THREAD_DEAD) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   459
       print_error("The process with pid %d does not exist.\n", pid);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   460
    }
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   461
    free(ph);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   462
    return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  // initialize ps_prochandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
  ph->pid = pid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  // initialize vtable
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  ph->ops = &process_ops;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  // read library info and symbol tables, must do this before attaching threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  // as the symbols in the pthread library will be used to figure out
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  // the list of threads within the same process.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  read_lib_info(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  // read thread info
51203
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   477
  if (is_in_container) {
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   478
    /*
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   479
     * If the process is running in the container, SA scans all tasks in
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   480
     * /proc/<PID>/task to read all threads info.
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   481
     */
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   482
    char taskpath[PATH_MAX];
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   483
    DIR *dirp;
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   484
    struct dirent *entry;
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   485
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   486
    snprintf(taskpath, PATH_MAX, "/proc/%d/task", ph->pid);
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   487
    dirp = opendir(taskpath);
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   488
    int lwp_id;
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   489
    while ((entry = readdir(dirp)) != NULL) {
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   490
      if (*entry->d_name == '.') {
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   491
        continue;
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   492
      }
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   493
      lwp_id = atoi(entry->d_name);
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   494
      if (lwp_id == ph->pid) {
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   495
        continue;
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   496
      }
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   497
      add_new_thread(ph, -1, lwp_id);
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   498
    }
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   499
    closedir(dirp);
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   500
  } else {
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   501
    read_thread_info(ph, add_new_thread);
220c9188db4f 8205992: jhsdb cannot attach to Java processes running in Docker containers
ysuenaga
parents: 49440
diff changeset
   502
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  // attach to the threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
  thr = ph->threads;
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   506
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  while (thr) {
53004
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   508
    thread_info* current_thr = thr;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   509
    thr = thr->next;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   510
    // don't attach to the main thread again
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   511
    if (ph->pid != current_thr->lwp_id) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   512
      if ((attach_status = ptrace_attach(current_thr->lwp_id, err_buf, err_buf_len)) != ATTACH_SUCCESS) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   513
        if (attach_status == ATTACH_THREAD_DEAD) {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   514
          // Remove this thread from the threads list
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   515
          delete_thread_info(ph, current_thr);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   516
        }
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   517
        else {
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   518
          Prelease(ph);
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   519
          return NULL;
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   520
        } // ATTACH_THREAD_DEAD
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   521
      } // !ATTACH_SUCCESS
b9d34a97a4be 8202884: SA: Attach/detach might fail on Linux if debugee application create/destroy threads during attaching
jgeorge
parents: 51203
diff changeset
   522
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
  return ph;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
}