hotspot/agent/src/os/bsd/ps_proc.c
author minqi
Mon, 24 Sep 2012 12:44:00 -0700
changeset 13873 7b72e3873785
parent 10565 dc90c239f4ec
child 15734 68f78eb98356
permissions -rw-r--r--
6879063: SA should use hsdis for disassembly Summary: We should in SA to use hsdis for it like the JVM does to replace the current java based disassembler. Reviewed-by: twisti, jrose, sla Contributed-by: yumin.qi@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     1
/*
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     4
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     8
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    13
 * accompanied this code).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    14
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    18
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    21
 * questions.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    22
 *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    23
 */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    24
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    25
#include <limits.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    26
#include <stdio.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    27
#include <stdlib.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    28
#include <string.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    29
#include <errno.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    30
#include <sys/types.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    31
#include <sys/wait.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    32
#include <sys/ptrace.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    33
#include <sys/param.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    34
#include <sys/user.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    35
#include <elf.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    36
#include <sys/elf_common.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    37
#include <sys/link_elf.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    38
#include <libutil.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    39
#include "libproc_impl.h"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    40
#include "elfmacros.h"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    41
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    42
// This file has the libproc implementation specific to live process
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    43
// For core files, refer to ps_core.c
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    44
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    45
static inline uintptr_t align(uintptr_t ptr, size_t size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    46
  return (ptr & ~(size - 1));
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    47
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    48
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    49
// ---------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    50
// ptrace functions
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    51
// ---------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    52
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    53
// read "size" bytes of data from "addr" within the target process.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    54
// unlike the standard ptrace() function, process_read_data() can handle
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    55
// unaligned address - alignment check, if required, should be done
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    56
// before calling process_read_data.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    57
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    58
static bool process_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    59
  int rslt;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    60
  size_t i, words;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    61
  uintptr_t end_addr = addr + size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    62
  uintptr_t aligned_addr = align(addr, sizeof(int));
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    63
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    64
  if (aligned_addr != addr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    65
    char *ptr = (char *)&rslt;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    66
    errno = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    67
    rslt = ptrace(PT_READ_D, ph->pid, (caddr_t) aligned_addr, 0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    68
    if (errno) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    69
      print_debug("ptrace(PT_READ_D, ..) failed for %d bytes @ %lx\n", size, addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    70
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    71
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    72
    for (; aligned_addr != addr; aligned_addr++, ptr++);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    73
    for (; ((intptr_t)aligned_addr % sizeof(int)) && aligned_addr < end_addr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    74
        aligned_addr++)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    75
       *(buf++) = *(ptr++);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    76
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    77
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    78
  words = (end_addr - aligned_addr) / sizeof(int);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    79
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    80
  // assert((intptr_t)aligned_addr % sizeof(int) == 0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    81
  for (i = 0; i < words; i++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    82
    errno = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    83
    rslt = ptrace(PT_READ_D, ph->pid, (caddr_t) aligned_addr, 0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    84
    if (errno) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    85
      print_debug("ptrace(PT_READ_D, ..) failed for %d bytes @ %lx\n", size, addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    86
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    87
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    88
    *(int *)buf = rslt;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    89
    buf += sizeof(int);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    90
    aligned_addr += sizeof(int);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    91
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    92
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    93
  if (aligned_addr != end_addr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    94
    char *ptr = (char *)&rslt;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    95
    errno = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    96
    rslt = ptrace(PT_READ_D, ph->pid, (caddr_t) aligned_addr, 0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    97
    if (errno) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    98
      print_debug("ptrace(PT_READ_D, ..) failed for %d bytes @ %lx\n", size, addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    99
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   100
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   101
    for (; aligned_addr != end_addr; aligned_addr++)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   102
       *(buf++) = *(ptr++);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   103
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   104
  return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   105
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   106
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   107
// null implementation for write
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   108
static bool process_write_data(struct ps_prochandle* ph,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   109
                             uintptr_t addr, const char *buf , size_t size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   110
  return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   111
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   112
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   113
// "user" should be a pointer to a reg
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   114
static bool process_get_lwp_regs(struct ps_prochandle* ph, pid_t pid, struct reg *user) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   115
  // we have already attached to all thread 'pid's, just use ptrace call
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   116
  // to get regset now. Note that we don't cache regset upfront for processes.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   117
 if (ptrace(PT_GETREGS, pid, (caddr_t) user, 0) < 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   118
   print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   119
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   120
 }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   121
 return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   122
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   123
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   124
// fill in ptrace_lwpinfo for lid
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   125
static bool process_get_lwp_info(struct ps_prochandle *ph, lwpid_t lwp_id, void *linfo) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   126
  errno = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   127
  ptrace(PT_LWPINFO, lwp_id, linfo, sizeof(struct ptrace_lwpinfo));
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   128
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   129
  return (errno == 0)? true: false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   130
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   131
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   132
// attach to a process/thread specified by "pid"
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   133
static bool ptrace_attach(pid_t pid) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   134
  if (ptrace(PT_ATTACH, pid, NULL, 0) < 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   135
    print_debug("ptrace(PTRACE_ATTACH, ..) failed for %d\n", pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   136
    return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   137
  } else {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   138
    int ret;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   139
    int status;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   140
    do {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   141
      // Wait for debuggee to stop.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   142
      ret = waitpid(pid, &status, 0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   143
      if (ret >= 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   144
        if (WIFSTOPPED(status)) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   145
          // Debuggee stopped.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   146
          return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   147
        } else {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   148
          print_debug("waitpid(): Child process exited/terminated (status = 0x%x)\n", status);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   149
          return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   150
        }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   151
      } else {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   152
        switch (errno) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   153
          case EINTR:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   154
            continue;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   155
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   156
          case ECHILD:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   157
            print_debug("waitpid() failed. Child process pid (%d) does not exist \n", pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   158
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   159
          case EINVAL:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   160
            print_debug("waitpid() failed. Invalid options argument.\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   161
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   162
          default:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   163
            print_debug("waitpid() failed. Unexpected error %d\n",errno);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   164
        }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   165
        return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   166
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   167
    } while(true);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   168
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   169
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   170
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   171
// -------------------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   172
// functions for obtaining library information
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   173
// -------------------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   174
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   175
// callback for read_thread_info
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   176
static bool add_new_thread(struct ps_prochandle* ph, pthread_t pthread_id, lwpid_t lwp_id) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   177
  return add_thread_info(ph, pthread_id, lwp_id) != NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   178
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   179
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   180
#if defined(__FreeBSD__) && __FreeBSD_version < 701000
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   181
/*
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   182
 * TEXT_START_ADDR from binutils/ld/emulparams/<arch_spec>.sh
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   183
 * Not the most robust but good enough.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   184
 */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   185
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   186
#if defined(amd64) || defined(x86_64)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   187
#define TEXT_START_ADDR 0x400000
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   188
#elif defined(i386)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   189
#define TEXT_START_ADDR 0x8048000
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   190
#else
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   191
#error TEXT_START_ADDR not defined
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   192
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   193
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   194
#define BUF_SIZE (PATH_MAX + NAME_MAX + 1)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   195
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   196
uintptr_t linkmap_addr(struct ps_prochandle *ph) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   197
  uintptr_t ehdr_addr, phdr_addr, dyn_addr, dmap_addr, lmap_addr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   198
  ELF_EHDR ehdr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   199
  ELF_PHDR *phdrs, *phdr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   200
  ELF_DYN *dyns, *dyn;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   201
  struct r_debug dmap;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   202
  unsigned long hdrs_size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   203
  unsigned int i;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   204
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   205
  /* read ELF_EHDR at TEXT_START_ADDR and validate */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   206
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   207
  ehdr_addr = (uintptr_t)TEXT_START_ADDR;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   208
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   209
  if (process_read_data(ph, ehdr_addr, (char *)&ehdr, sizeof(ehdr)) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   210
    print_debug("process_read_data failed for ehdr_addr %p\n", ehdr_addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   211
    return (0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   212
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   213
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   214
  if (!IS_ELF(ehdr) ||
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   215
        ehdr.e_ident[EI_CLASS] != ELF_TARG_CLASS ||
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   216
        ehdr.e_ident[EI_DATA] != ELF_TARG_DATA ||
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   217
        ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   218
        ehdr.e_phentsize != sizeof(ELF_PHDR) ||
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   219
        ehdr.e_version != ELF_TARG_VER ||
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   220
        ehdr.e_machine != ELF_TARG_MACH) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   221
    print_debug("not an ELF_EHDR at %p\n", ehdr_addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   222
    return (0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   223
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   224
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   225
  /* allocate space for all ELF_PHDR's and read */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   226
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   227
  phdr_addr = ehdr_addr + ehdr.e_phoff;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   228
  hdrs_size = ehdr.e_phnum * sizeof(ELF_PHDR);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   229
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   230
  if ((phdrs = malloc(hdrs_size)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   231
    return (0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   232
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   233
  if (process_read_data(ph, phdr_addr, (char *)phdrs, hdrs_size) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   234
    print_debug("process_read_data failed for phdr_addr %p\n", phdr_addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   235
    return (0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   236
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   237
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   238
  /* find PT_DYNAMIC section */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   239
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   240
  for (i = 0, phdr = phdrs; i < ehdr.e_phnum; i++, phdr++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   241
    if (phdr->p_type == PT_DYNAMIC)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   242
      break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   243
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   244
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   245
  if (i >= ehdr.e_phnum) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   246
    print_debug("PT_DYNAMIC section not found!\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   247
    free(phdrs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   248
    return (0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   249
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   250
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   251
  /* allocate space and read in ELF_DYN headers */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   252
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   253
  dyn_addr = phdr->p_vaddr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   254
  hdrs_size = phdr->p_memsz;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   255
  free(phdrs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   256
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   257
  if ((dyns = malloc(hdrs_size)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   258
    return (0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   259
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   260
  if (process_read_data(ph, dyn_addr, (char *)dyns, hdrs_size) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   261
    print_debug("process_read_data failed for dyn_addr %p\n", dyn_addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   262
    free(dyns);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   263
    return (0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   264
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   265
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   266
  /* find DT_DEBUG */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   267
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   268
  dyn = dyns;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   269
  while (dyn->d_tag != DT_DEBUG && dyn->d_tag != DT_NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   270
    dyn++;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   271
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   272
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   273
  if (dyn->d_tag != DT_DEBUG) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   274
    print_debug("failed to find DT_DEBUG\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   275
    free(dyns);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   276
    return (0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   277
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   278
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   279
  /* read struct r_debug into dmap */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   280
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   281
  dmap_addr = (uintptr_t)dyn->d_un.d_ptr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   282
  free(dyns);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   283
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   284
  if (process_read_data(ph, dmap_addr, (char *)&dmap, sizeof(dmap)) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   285
    print_debug("process_read_data failed for dmap_addr %p\n", dmap_addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   286
    return (0);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   287
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   288
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   289
  lmap_addr = (uintptr_t)dmap.r_map;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   290
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   291
  return (lmap_addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   292
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   293
#endif // __FreeBSD__ && __FreeBSD_version < 701000
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   294
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   295
static bool read_lib_info(struct ps_prochandle* ph) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   296
#if defined(__FreeBSD__) && __FreeBSD_version >= 701000
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   297
  struct kinfo_vmentry *freep, *kve;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   298
  int i, cnt;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   299
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   300
  freep = kinfo_getvmmap(ph->pid, &cnt);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   301
  if (freep == NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   302
      print_debug("can't get vm map for pid\n", ph->pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   303
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   304
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   305
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   306
  for (i = 0; i < cnt; i++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   307
    kve = &freep[i];
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   308
    if ((kve->kve_flags & KVME_FLAG_COW) &&
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   309
        kve->kve_path != NULL &&
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   310
        strlen(kve->kve_path) > 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   311
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   312
      if (find_lib(ph, kve->kve_path) == false) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   313
        lib_info* lib;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   314
        if ((lib = add_lib_info(ph, kve->kve_path,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   315
                                (uintptr_t) kve->kve_start)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   316
          continue; // ignore, add_lib_info prints error
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   317
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   318
        // we don't need to keep the library open, symtab is already
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   319
        // built. Only for core dump we need to keep the fd open.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   320
        close(lib->fd);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   321
        lib->fd = -1;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   322
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   323
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   324
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   325
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   326
  free(freep);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   327
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   328
  return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   329
#else
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   330
  char *l_name;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   331
  struct link_map *lmap;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   332
  uintptr_t lmap_addr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   333
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   334
  if ((l_name = malloc(BUF_SIZE)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   335
    return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   336
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   337
  if ((lmap = malloc(sizeof(*lmap))) == NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   338
    free(l_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   339
    return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   340
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   341
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   342
  lmap_addr = linkmap_addr(ph);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   343
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   344
  if (lmap_addr == 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   345
    free(l_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   346
    free(lmap);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   347
    return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   348
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   349
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   350
  do {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   351
    if (process_read_data(ph, lmap_addr, (char *)lmap, sizeof(*lmap)) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   352
      print_debug("process_read_data failed for lmap_addr %p\n", lmap_addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   353
      free (l_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   354
      free (lmap);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   355
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   356
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   357
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   358
    if (process_read_data(ph, (uintptr_t)lmap->l_name, l_name,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   359
        BUF_SIZE) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   360
      print_debug("process_read_data failed for lmap->l_name %p\n",
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   361
          lmap->l_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   362
      free (l_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   363
      free (lmap);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   364
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   365
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   366
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   367
    if (find_lib(ph, l_name) == false) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   368
      lib_info* lib;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   369
      if ((lib = add_lib_info(ph, l_name,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   370
                              (uintptr_t) lmap->l_addr)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   371
        continue; // ignore, add_lib_info prints error
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   372
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   373
      // we don't need to keep the library open, symtab is already
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   374
      // built. Only for core dump we need to keep the fd open.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   375
      close(lib->fd);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   376
      lib->fd = -1;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   377
    }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   378
    lmap_addr = (uintptr_t)lmap->l_next;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   379
  } while (lmap->l_next != NULL);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   380
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   381
  free (l_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   382
  free (lmap);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   383
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   384
  return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   385
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   386
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   387
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   388
// detach a given pid
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   389
static bool ptrace_detach(pid_t pid) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   390
  if (pid && ptrace(PT_DETACH, pid, (caddr_t)1, 0) < 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   391
    print_debug("ptrace(PTRACE_DETACH, ..) failed for %d\n", pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   392
    return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   393
  } else {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   394
    return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   395
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   396
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   397
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   398
static void process_cleanup(struct ps_prochandle* ph) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   399
  ptrace_detach(ph->pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   400
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   401
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   402
static ps_prochandle_ops process_ops = {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   403
  .release=  process_cleanup,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   404
  .p_pread=  process_read_data,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   405
  .p_pwrite= process_write_data,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   406
  .get_lwp_regs= process_get_lwp_regs,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   407
  .get_lwp_info= process_get_lwp_info
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   408
};
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   409
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   410
// attach to the process. One and only one exposed stuff
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   411
struct ps_prochandle* Pgrab(pid_t pid) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   412
  struct ps_prochandle* ph = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   413
  thread_info* thr = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   414
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   415
  if ( (ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle))) == NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   416
     print_debug("can't allocate memory for ps_prochandle\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   417
     return NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   418
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   419
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   420
  if (ptrace_attach(pid) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   421
     free(ph);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   422
     return NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   423
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   424
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   425
  // initialize ps_prochandle
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   426
  ph->pid = pid;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   427
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   428
  // initialize vtable
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   429
  ph->ops = &process_ops;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   430
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   431
  // read library info and symbol tables, must do this before attaching threads,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   432
  // as the symbols in the pthread library will be used to figure out
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   433
  // the list of threads within the same process.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   434
  if (read_lib_info(ph) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   435
     ptrace_detach(pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   436
     free(ph);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   437
     return NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   438
  }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   439
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   440
  // read thread info
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   441
  read_thread_info(ph, add_new_thread);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   442
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   443
  return ph;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   444
}