src/jdk.hotspot.agent/macosx/native/libsaproc/ps_core.c
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 58912 44d60fc2dd8a
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
     1
/*
58541
8bc609fcd691 8231986: [SA] Consolidate parts of the Linux and MacOSX versions of ps_core.c
iklam
parents: 55069
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
10565
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 <jni.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    26
#include <unistd.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    27
#include <fcntl.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 <stdlib.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    30
#include <stddef.h>
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    31
#include "libproc_impl.h"
58541
8bc609fcd691 8231986: [SA] Consolidate parts of the Linux and MacOSX versions of ps_core.c
iklam
parents: 55069
diff changeset
    32
#include "ps_core_common.h"
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    33
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    34
#ifdef __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    35
#include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    36
#endif
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    37
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    38
// This file has the libproc implementation to read core files.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    39
// For live processes, refer to ps_proc.c. Portions of this is adapted
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    40
// /modelled after Solaris libproc.so (in particular Pcore.c)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    41
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    42
//---------------------------------------------------------------------------
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    43
// functions to handle map_info
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    44
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    45
// Order mappings based on virtual address.  We use this function as the
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    46
// callback for sorting the array of map_info pointers.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    47
static int core_cmp_mapping(const void *lhsp, const void *rhsp)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    48
{
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    49
  const map_info *lhs = *((const map_info **)lhsp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    50
  const map_info *rhs = *((const map_info **)rhsp);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    51
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    52
  if (lhs->vaddr == rhs->vaddr) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    53
    return (0);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    54
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    55
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    56
  return (lhs->vaddr < rhs->vaddr ? -1 : 1);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    57
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    58
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    59
// we sort map_info by starting virtual address so that we can do
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    60
// binary search to read from an address.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    61
static bool sort_map_array(struct ps_prochandle* ph) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    62
  size_t num_maps = ph->core->num_maps;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    63
  map_info* map = ph->core->maps;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    64
  int i = 0;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    65
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    66
  // allocate map_array
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    67
  map_info** array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    68
  if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
    69
    print_debug("can't allocate memory for map array\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
    70
    return false;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    71
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    72
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    73
  // add maps to array
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    74
  while (map) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    75
    array[i] = map;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    76
    i++;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    77
    map = map->next;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    78
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    79
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    80
  // sort is called twice. If this is second time, clear map array
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    81
  if (ph->core->map_array) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    82
    free(ph->core->map_array);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    83
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    84
  ph->core->map_array = array;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    85
  // sort the map_info array by base virtual address.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    86
  qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
    87
        core_cmp_mapping);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    88
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    89
  // print map
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    90
  if (is_debug()) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    91
    int j = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    92
    print_debug("---- sorted virtual address map ----\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    93
    for (j = 0; j < ph->core->num_maps; j++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    94
      print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr,
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
    95
                  ph->core->map_array[j]->memsz);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    96
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    97
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
    98
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
    99
  return true;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   100
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   101
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   102
#ifndef MIN
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   103
#define MIN(x, y) (((x) < (y))? (x): (y))
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   104
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   105
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   106
static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   107
   ssize_t resid = size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   108
   int page_size=sysconf(_SC_PAGE_SIZE);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   109
   while (resid != 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   110
      map_info *mp = core_lookup(ph, addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   111
      uintptr_t mapoff;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   112
      ssize_t len, rem;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   113
      off_t off;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   114
      int fd;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   115
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   116
      if (mp == NULL) {
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   117
         break;  /* No mapping for this address */
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   118
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   119
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   120
      fd = mp->fd;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   121
      mapoff = addr - mp->vaddr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   122
      len = MIN(resid, mp->memsz - mapoff);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   123
      off = mp->offset + mapoff;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   124
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   125
      if ((len = pread(fd, buf, len, off)) <= 0) {
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   126
         break;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   127
      }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   128
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   129
      resid -= len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   130
      addr += len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   131
      buf = (char *)buf + len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   132
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   133
      // mappings always start at page boundary. But, may end in fractional
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   134
      // page. fill zeros for possible fractional page at the end of a mapping.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   135
      rem = mp->memsz % page_size;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   136
      if (rem > 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   137
         rem = page_size - rem;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   138
         len = MIN(resid, rem);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   139
         resid -= len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   140
         addr += len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   141
         // we are not assuming 'buf' to be zero initialized.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   142
         memset(buf, 0, len);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   143
         buf += len;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   144
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   145
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   146
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   147
   if (resid) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   148
      print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n",
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   149
              size, addr, resid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   150
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   151
   } else {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   152
      return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   153
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   154
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   155
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   156
// null implementation for write
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   157
static bool core_write_data(struct ps_prochandle* ph,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   158
                             uintptr_t addr, const char *buf , size_t size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   159
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   160
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   161
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   162
static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   163
                          struct reg* regs) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   164
   // for core we have cached the lwp regs after segment parsed
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   165
   sa_thread_info* thr = ph->threads;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   166
   while (thr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   167
     if (thr->lwp_id == lwp_id) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   168
       memcpy(regs, &thr->regs, sizeof(struct reg));
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   169
       return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   170
     }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   171
     thr = thr->next;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   172
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   173
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   174
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   175
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   176
static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t id, void *info) {
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   177
   print_debug("core_get_lwp_info not implemented\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   178
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   179
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   180
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   181
static ps_prochandle_ops core_ops = {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   182
   .release=  core_release,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   183
   .p_pread=  core_read_data,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   184
   .p_pwrite= core_write_data,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   185
   .get_lwp_regs= core_get_lwp_regs,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   186
   .get_lwp_info= core_get_lwp_info
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   187
};
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   188
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   189
// from this point, mainly two blocks divided by def __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   190
// one for Macosx, the other for regular Bsd
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   191
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   192
#ifdef __APPLE__
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   193
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   194
void print_thread(sa_thread_info *threadinfo) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   195
  print_debug("thread added: %d\n", threadinfo->lwp_id);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   196
  print_debug("registers:\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   197
  print_debug("  r_r15: 0x%" PRIx64 "\n", threadinfo->regs.r_r15);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   198
  print_debug("  r_r14: 0x%" PRIx64 "\n", threadinfo->regs.r_r14);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   199
  print_debug("  r_r13: 0x%" PRIx64 "\n", threadinfo->regs.r_r13);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   200
  print_debug("  r_r12: 0x%" PRIx64 "\n", threadinfo->regs.r_r12);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   201
  print_debug("  r_r11: 0x%" PRIx64 "\n", threadinfo->regs.r_r11);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   202
  print_debug("  r_r10: 0x%" PRIx64 "\n", threadinfo->regs.r_r10);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   203
  print_debug("  r_r9:  0x%" PRIx64 "\n", threadinfo->regs.r_r9);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   204
  print_debug("  r_r8:  0x%" PRIx64 "\n", threadinfo->regs.r_r8);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   205
  print_debug("  r_rdi: 0x%" PRIx64 "\n", threadinfo->regs.r_rdi);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   206
  print_debug("  r_rsi: 0x%" PRIx64 "\n", threadinfo->regs.r_rsi);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   207
  print_debug("  r_rbp: 0x%" PRIx64 "\n", threadinfo->regs.r_rbp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   208
  print_debug("  r_rbx: 0x%" PRIx64 "\n", threadinfo->regs.r_rbx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   209
  print_debug("  r_rdx: 0x%" PRIx64 "\n", threadinfo->regs.r_rdx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   210
  print_debug("  r_rcx: 0x%" PRIx64 "\n", threadinfo->regs.r_rcx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   211
  print_debug("  r_rax: 0x%" PRIx64 "\n", threadinfo->regs.r_rax);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   212
  print_debug("  r_fs:  0x%" PRIx32 "\n", threadinfo->regs.r_fs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   213
  print_debug("  r_gs:  0x%" PRIx32 "\n", threadinfo->regs.r_gs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   214
  print_debug("  r_rip  0x%" PRIx64 "\n", threadinfo->regs.r_rip);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   215
  print_debug("  r_cs:  0x%" PRIx64 "\n", threadinfo->regs.r_cs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   216
  print_debug("  r_rsp: 0x%" PRIx64 "\n", threadinfo->regs.r_rsp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   217
  print_debug("  r_rflags: 0x%" PRIx64 "\n", threadinfo->regs.r_rflags);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   218
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   219
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   220
// read all segments64 commands from core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   221
// read all thread commands from core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   222
static bool read_core_segments(struct ps_prochandle* ph) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   223
  int i = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   224
  int num_threads = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   225
  int fd = ph->core->core_fd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   226
  off_t offset = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   227
  mach_header_64      fhead;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   228
  load_command        lcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   229
  segment_command_64  segcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   230
  // thread_command      thrcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   231
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   232
  lseek(fd, offset, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   233
  if(read(fd, (void *)&fhead, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   234
     goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   235
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   236
  print_debug("total commands: %d\n", fhead.ncmds);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   237
  offset += sizeof(mach_header_64);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   238
  for (i = 0; i < fhead.ncmds; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   239
    lseek(fd, offset, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   240
    if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   241
      goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   242
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   243
    offset += lcmd.cmdsize;    // next command position
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   244
    if (lcmd.cmd == LC_SEGMENT_64) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   245
      lseek(fd, -sizeof(load_command), SEEK_CUR);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   246
      if (read(fd, (void *)&segcmd, sizeof(segment_command_64)) != sizeof(segment_command_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   247
        print_debug("failed to read LC_SEGMENT_64 i = %d!\n", i);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   248
        goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   249
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   250
      if (add_map_info(ph, fd, segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   251
        print_debug("Failed to add map_info at i = %d\n", i);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   252
        goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   253
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   254
      print_debug("segment added: %" PRIu64 " 0x%" PRIx64 " %d\n",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   255
                   segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   256
    } else if (lcmd.cmd == LC_THREAD || lcmd.cmd == LC_UNIXTHREAD) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   257
      typedef struct thread_fc {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   258
        uint32_t  flavor;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   259
        uint32_t  count;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   260
      } thread_fc;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   261
      thread_fc fc;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   262
      uint32_t size = sizeof(load_command);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   263
      while (size < lcmd.cmdsize) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   264
        if (read(fd, (void *)&fc, sizeof(thread_fc)) != sizeof(thread_fc)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   265
          printf("Reading flavor, count failed.\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   266
          goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   267
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   268
        size += sizeof(thread_fc);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   269
        if (fc.flavor == x86_THREAD_STATE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   270
          x86_thread_state_t thrstate;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   271
          if (read(fd, (void *)&thrstate, sizeof(x86_thread_state_t)) != sizeof(x86_thread_state_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   272
            printf("Reading flavor, count failed.\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   273
            goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   274
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   275
          size += sizeof(x86_thread_state_t);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   276
          // create thread info list, update lwp_id later
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   277
          sa_thread_info* newthr = add_thread_info(ph, (pthread_t) -1, (lwpid_t) num_threads++);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   278
          if (newthr == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   279
            printf("create thread_info failed\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   280
            goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   281
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   282
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   283
          // note __DARWIN_UNIX03 depengs on other definitions
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   284
#if __DARWIN_UNIX03
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   285
#define get_register_v(regst, regname) \
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   286
  regst.uts.ts64.__##regname
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   287
#else
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   288
#define get_register_v(regst, regname) \
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   289
  regst.uts.ts64.##regname
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   290
#endif // __DARWIN_UNIX03
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   291
          newthr->regs.r_rax = get_register_v(thrstate, rax);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   292
          newthr->regs.r_rbx = get_register_v(thrstate, rbx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   293
          newthr->regs.r_rcx = get_register_v(thrstate, rcx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   294
          newthr->regs.r_rdx = get_register_v(thrstate, rdx);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   295
          newthr->regs.r_rdi = get_register_v(thrstate, rdi);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   296
          newthr->regs.r_rsi = get_register_v(thrstate, rsi);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   297
          newthr->regs.r_rbp = get_register_v(thrstate, rbp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   298
          newthr->regs.r_rsp = get_register_v(thrstate, rsp);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   299
          newthr->regs.r_r8  = get_register_v(thrstate, r8);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   300
          newthr->regs.r_r9  = get_register_v(thrstate, r9);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   301
          newthr->regs.r_r10 = get_register_v(thrstate, r10);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   302
          newthr->regs.r_r11 = get_register_v(thrstate, r11);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   303
          newthr->regs.r_r12 = get_register_v(thrstate, r12);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   304
          newthr->regs.r_r13 = get_register_v(thrstate, r13);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   305
          newthr->regs.r_r14 = get_register_v(thrstate, r14);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   306
          newthr->regs.r_r15 = get_register_v(thrstate, r15);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   307
          newthr->regs.r_rip = get_register_v(thrstate, rip);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   308
          newthr->regs.r_rflags = get_register_v(thrstate, rflags);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   309
          newthr->regs.r_cs  = get_register_v(thrstate, cs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   310
          newthr->regs.r_fs  = get_register_v(thrstate, fs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   311
          newthr->regs.r_gs  = get_register_v(thrstate, gs);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   312
          print_thread(newthr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   313
        } else if (fc.flavor == x86_FLOAT_STATE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   314
          x86_float_state_t flstate;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   315
          if (read(fd, (void *)&flstate, sizeof(x86_float_state_t)) != sizeof(x86_float_state_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   316
            print_debug("Reading flavor, count failed.\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   317
            goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   318
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   319
          size += sizeof(x86_float_state_t);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   320
        } else if (fc.flavor == x86_EXCEPTION_STATE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   321
          x86_exception_state_t excpstate;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   322
          if (read(fd, (void *)&excpstate, sizeof(x86_exception_state_t)) != sizeof(x86_exception_state_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   323
            printf("Reading flavor, count failed.\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   324
            goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   325
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   326
          size += sizeof(x86_exception_state_t);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   327
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   328
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   329
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   330
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   331
  return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   332
err:
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   333
  return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   334
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   335
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   336
/**local function **/
42549
154099b110df 8169344: Potential open file descriptor in exists() of hotspot/agent/src/os/bsd/ps_core.c
dsamersoff
parents: 35217
diff changeset
   337
bool exists(const char *fname) {
154099b110df 8169344: Potential open file descriptor in exists() of hotspot/agent/src/os/bsd/ps_core.c
dsamersoff
parents: 35217
diff changeset
   338
  return access(fname, F_OK) == 0;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   339
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   340
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   341
// we check: 1. lib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   342
//           2. lib/server
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   343
//           3. jre/lib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   344
//           4. jre/lib/server
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   345
// from: 1. exe path
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   346
//       2. JAVA_HOME
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   347
//       3. DYLD_LIBRARY_PATH
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   348
static bool get_real_path(struct ps_prochandle* ph, char *rpath) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   349
  /** check if they exist in JAVA ***/
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   350
  char* execname = ph->core->exec_path;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   351
  char  filepath[4096];
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   352
  char* filename = strrchr(rpath, '/');               // like /libjvm.dylib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   353
  if (filename == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   354
    return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   355
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   356
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   357
  char* posbin = strstr(execname, "/bin/java");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   358
  if (posbin != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   359
    memcpy(filepath, execname, posbin - execname);    // not include trailing '/'
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   360
    filepath[posbin - execname] = '\0';
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   361
  } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   362
    char* java_home = getenv("JAVA_HOME");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   363
    if (java_home != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   364
      strcpy(filepath, java_home);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   365
    } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   366
      char* dyldpath = getenv("DYLD_LIBRARY_PATH");
58912
44d60fc2dd8a 8232973: Potential infinite loop in macOS hotspot agent
stooke
parents: 58541
diff changeset
   367
      char* save_ptr;
44d60fc2dd8a 8232973: Potential infinite loop in macOS hotspot agent
stooke
parents: 58541
diff changeset
   368
      char* dypath = strtok_r(dyldpath, ":", &save_ptr);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   369
      while (dypath != NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   370
        strcpy(filepath, dypath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   371
        strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   372
        if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   373
           strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   374
           return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   375
        }
58912
44d60fc2dd8a 8232973: Potential infinite loop in macOS hotspot agent
stooke
parents: 58541
diff changeset
   376
        dypath = strtok_r(NULL, ":", &save_ptr);
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   377
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   378
      // not found
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   379
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   380
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   381
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   382
  // for exec and java_home, jdkpath now is filepath
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   383
  size_t filepath_base_size = strlen(filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   384
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   385
  // first try /lib/ and /lib/server
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   386
  strcat(filepath, "/lib");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   387
  strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   388
  if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   389
    strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   390
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   391
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   392
  char* pos = strstr(filepath, filename);    // like /libjvm.dylib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   393
  *pos = '\0';
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   394
  strcat(filepath, "/server");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   395
  strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   396
  if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   397
    strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   398
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   399
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   400
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   401
  // then try /jre/lib/ and /jre/lib/server
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   402
  filepath[filepath_base_size] = '\0';
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   403
  strcat(filepath, "/jre/lib");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   404
  strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   405
  if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   406
    strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   407
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   408
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   409
  pos = strstr(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   410
  *pos = '\0';
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   411
  strcat(filepath, "/server");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   412
  strcat(filepath, filename);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   413
  if (exists(filepath)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   414
    strcpy(rpath, filepath);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   415
    return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   416
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   417
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   418
  return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   419
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   420
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   421
static bool read_shared_lib_info(struct ps_prochandle* ph) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   422
  static int pagesize = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   423
  int fd = ph->core->core_fd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   424
  int i = 0, j;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   425
  uint32_t  v;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   426
  mach_header_64 header;        // used to check if a file header in segment
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   427
  load_command lcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   428
  dylib_command dylibcmd;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   429
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   430
  char name[BUF_SIZE];  // use to store name
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   431
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   432
  if (pagesize == 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   433
    pagesize = getpagesize();
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   434
    print_debug("page size is %d\n", pagesize);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   435
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   436
  for (j = 0; j < ph->core->num_maps; j++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   437
    map_info *iter = ph->core->map_array[j];   // head
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   438
    off_t fpos = iter->offset;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   439
    if (iter->fd != fd) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   440
      // only search core file!
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   441
      continue;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   442
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   443
    print_debug("map_info %d: vmaddr = 0x%016" PRIx64 "  fileoff = %" PRIu64 "  vmsize = %" PRIu64 "\n",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   444
                           j, iter->vaddr, iter->offset, iter->memsz);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   445
    lseek(fd, fpos, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   446
    // we assume .dylib loaded at segment address --- which is true for JVM libraries
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   447
    // multiple files may be loaded in one segment.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   448
    // if first word is not a magic word, means this segment does not contain lib file.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   449
    if (read(fd, (void *)&v, sizeof(uint32_t)) == sizeof(uint32_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   450
      if (v != MH_MAGIC_64) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   451
        continue;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   452
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   453
    } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   454
      // may be encountered last map, which is not readable
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   455
      continue;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   456
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   457
    while (ltell(fd) - iter->offset < iter->memsz) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   458
      lseek(fd, fpos, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   459
      if (read(fd, (void *)&v, sizeof(uint32_t)) != sizeof(uint32_t)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   460
        break;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   461
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   462
      if (v != MH_MAGIC_64) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   463
        fpos = (ltell(fd) + pagesize -1)/pagesize * pagesize;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   464
        continue;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   465
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   466
      lseek(fd, -sizeof(uint32_t), SEEK_CUR);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   467
      // this is the file begining to core file.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   468
      if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   469
        goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   470
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   471
      fpos = ltell(fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   472
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   473
      // found a mach-o file in this segment
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   474
      for (i = 0; i < header.ncmds; i++) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   475
        // read commands in this "file"
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   476
        // LC_ID_DYLIB is the file itself for a .dylib
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   477
        lseek(fd, fpos, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   478
        if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   479
          return false;   // error
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   480
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   481
        fpos += lcmd.cmdsize;  // next command position
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   482
        // make sure still within seg size.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   483
        if (fpos  - lcmd.cmdsize - iter->offset > iter->memsz) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   484
          print_debug("Warning: out of segement limit: %ld \n", fpos  - lcmd.cmdsize - iter->offset);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   485
          break;  // no need to iterate all commands
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   486
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   487
        if (lcmd.cmd == LC_ID_DYLIB) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   488
          lseek(fd, -sizeof(load_command), SEEK_CUR);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   489
          if (read(fd, (void *)&dylibcmd, sizeof(dylib_command)) != sizeof(dylib_command)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   490
            return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   491
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   492
          /**** name stored at dylib_command.dylib.name.offset, is a C string  */
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   493
          lseek(fd, dylibcmd.dylib.name.offset - sizeof(dylib_command), SEEK_CUR);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   494
          int j = 0;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   495
          while (j < BUF_SIZE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   496
            read(fd, (void *)(name + j), sizeof(char));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   497
            if (name[j] == '\0') break;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   498
            j++;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   499
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   500
          print_debug("%s\n", name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   501
          // changed name from @rpath/xxxx.dylib to real path
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   502
          if (strrchr(name, '@')) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   503
            get_real_path(ph, name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   504
            print_debug("get_real_path returned: %s\n", name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   505
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   506
          add_lib_info(ph, name, iter->vaddr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   507
          break;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   508
        }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   509
      }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   510
      // done with the file, advanced to next page to search more files
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   511
      fpos = (ltell(fd) + pagesize - 1) / pagesize * pagesize;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   512
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   513
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   514
  return true;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   515
err:
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   516
  return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   517
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   518
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   519
bool read_macho64_header(int fd, mach_header_64* core_header) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   520
  bool is_macho = false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   521
  if (fd < 0) return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   522
  off_t pos = ltell(fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   523
  lseek(fd, 0, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   524
  if (read(fd, (void *)core_header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   525
    is_macho = false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   526
  } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   527
    is_macho = (core_header->magic ==  MH_MAGIC_64 || core_header->magic ==  MH_CIGAM_64);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   528
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   529
  lseek(fd, pos, SEEK_SET);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   530
  return is_macho;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   531
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   532
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   533
// the one and only one exposed stuff from this file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   534
struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   535
  mach_header_64 core_header;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   536
  mach_header_64 exec_header;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   537
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   538
  struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   539
  if (ph == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   540
    print_debug("cant allocate ps_prochandle\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   541
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   542
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   543
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   544
  if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   545
    free(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   546
    print_debug("can't allocate ps_prochandle\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   547
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   548
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   549
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   550
  // initialize ph
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   551
  ph->ops = &core_ops;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   552
  ph->core->core_fd   = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   553
  ph->core->exec_fd   = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   554
  ph->core->interp_fd = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   555
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   556
  print_debug("exec: %s   core: %s", exec_file, core_file);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   557
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   558
  strncpy(ph->core->exec_path, exec_file, sizeof(ph->core->exec_path));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   559
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   560
  // open the core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   561
  if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   562
    print_error("can't open core file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   563
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   564
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   565
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   566
  // read core file header
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   567
  if (read_macho64_header(ph->core->core_fd, &core_header) != true || core_header.filetype != MH_CORE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   568
    print_debug("core file is not a valid Mach-O file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   569
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   570
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   571
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   572
  if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   573
    print_error("can't open executable file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   574
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   575
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   576
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   577
  if (read_macho64_header(ph->core->exec_fd, &exec_header) != true ||
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   578
                          exec_header.filetype != MH_EXECUTE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   579
    print_error("executable file is not a valid Mach-O file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   580
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   581
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   582
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   583
  // process core file segments
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   584
  if (read_core_segments(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   585
    print_error("failed to read core segments\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   586
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   587
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   588
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   589
  // allocate and sort maps into map_array, we need to do this
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   590
  // here because read_shared_lib_info needs to read from debuggee
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   591
  // address space
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   592
  if (sort_map_array(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   593
    print_error("failed to sort segment map array\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   594
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   595
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   596
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   597
  if (read_shared_lib_info(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   598
    print_error("failed to read libraries\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   599
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   600
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   601
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   602
  // sort again because we have added more mappings from shared objects
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   603
  if (sort_map_array(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   604
    print_error("failed to sort segment map array\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   605
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   606
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   607
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   608
  if (init_classsharing_workaround(ph) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   609
    print_error("failed to workaround classshareing\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   610
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   611
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   612
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   613
  print_debug("Leave Pgrab_core\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   614
  return ph;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   615
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   616
err:
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   617
  Prelease(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   618
  return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   619
}
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   620
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   621
#else // __APPLE__ (none macosx)
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   622
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   623
// read regs and create thread from core file
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   624
static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   625
   // we have to read prstatus_t from buf
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   626
   // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   627
   prstatus_t* prstat = (prstatus_t*) buf;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   628
   sa_thread_info* newthr;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   629
   print_debug("got integer regset for lwp %d\n", prstat->pr_pid);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   630
   // we set pthread_t to -1 for core dump
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   631
   if((newthr = add_thread_info(ph, (pthread_t) -1,  prstat->pr_pid)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   632
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   633
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   634
   // copy regs
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   635
   memcpy(&newthr->regs, &prstat->pr_reg, sizeof(struct reg));
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   636
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   637
   if (is_debug()) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   638
      print_debug("integer regset\n");
55069
6e0786d8451b 8224796: C code is not compiled correctly due to undefined "i386"
shade
parents: 51477
diff changeset
   639
#if defined(i586) || defined(i386)
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   640
      // print the regset
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   641
      print_debug("\teax = 0x%x\n", newthr->regs.r_eax);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   642
      print_debug("\tebx = 0x%x\n", newthr->regs.r_ebx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   643
      print_debug("\tecx = 0x%x\n", newthr->regs.r_ecx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   644
      print_debug("\tedx = 0x%x\n", newthr->regs.r_edx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   645
      print_debug("\tesp = 0x%x\n", newthr->regs.r_esp);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   646
      print_debug("\tebp = 0x%x\n", newthr->regs.r_ebp);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   647
      print_debug("\tesi = 0x%x\n", newthr->regs.r_esi);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   648
      print_debug("\tedi = 0x%x\n", newthr->regs.r_edi);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   649
      print_debug("\teip = 0x%x\n", newthr->regs.r_eip);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   650
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   651
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   652
#if defined(amd64) || defined(x86_64)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   653
      // print the regset
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   654
      print_debug("\tr15 = 0x%lx\n", newthr->regs.r_r15);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   655
      print_debug("\tr14 = 0x%lx\n", newthr->regs.r_r14);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   656
      print_debug("\tr13 = 0x%lx\n", newthr->regs.r_r13);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   657
      print_debug("\tr12 = 0x%lx\n", newthr->regs.r_r12);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   658
      print_debug("\trbp = 0x%lx\n", newthr->regs.r_rbp);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   659
      print_debug("\trbx = 0x%lx\n", newthr->regs.r_rbx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   660
      print_debug("\tr11 = 0x%lx\n", newthr->regs.r_r11);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   661
      print_debug("\tr10 = 0x%lx\n", newthr->regs.r_r10);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   662
      print_debug("\tr9 = 0x%lx\n", newthr->regs.r_r9);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   663
      print_debug("\tr8 = 0x%lx\n", newthr->regs.r_r8);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   664
      print_debug("\trax = 0x%lx\n", newthr->regs.r_rax);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   665
      print_debug("\trcx = 0x%lx\n", newthr->regs.r_rcx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   666
      print_debug("\trdx = 0x%lx\n", newthr->regs.r_rdx);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   667
      print_debug("\trsi = 0x%lx\n", newthr->regs.r_rsi);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   668
      print_debug("\trdi = 0x%lx\n", newthr->regs.r_rdi);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   669
      //print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   670
      print_debug("\trip = 0x%lx\n", newthr->regs.r_rip);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   671
      print_debug("\tcs = 0x%lx\n", newthr->regs.r_cs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   672
      //print_debug("\teflags = 0x%lx\n", newthr->regs.eflags);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   673
      print_debug("\trsp = 0x%lx\n", newthr->regs.r_rsp);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   674
      print_debug("\tss = 0x%lx\n", newthr->regs.r_ss);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   675
      //print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   676
      //print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   677
      //print_debug("\tds = 0x%lx\n", newthr->regs.ds);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   678
      //print_debug("\tes = 0x%lx\n", newthr->regs.es);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   679
      //print_debug("\tfs = 0x%lx\n", newthr->regs.fs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   680
      //print_debug("\tgs = 0x%lx\n", newthr->regs.gs);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   681
#endif
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   682
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   683
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   684
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   685
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   686
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   687
#define ROUNDUP(x, y)  ((((x)+((y)-1))/(y))*(y))
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   688
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   689
// read NT_PRSTATUS entries from core NOTE segment
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   690
static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   691
   char* buf = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   692
   char* p = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   693
   size_t size = note_phdr->p_filesz;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   694
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   695
   // we are interested in just prstatus entries. we will ignore the rest.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   696
   // Advance the seek pointer to the start of the PT_NOTE data
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   697
   if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   698
      print_debug("failed to lseek to PT_NOTE data\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   699
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   700
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   701
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   702
   // Now process the PT_NOTE structures.  Each one is preceded by
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   703
   // an Elf{32/64}_Nhdr structure describing its type and size.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   704
   if ( (buf = (char*) malloc(size)) == NULL) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   705
      print_debug("can't allocate memory for reading core notes\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   706
      goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   707
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   708
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   709
   // read notes into buffer
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   710
   if (read(ph->core->core_fd, buf, size) != size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   711
      print_debug("failed to read notes, core file must have been truncated\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   712
      goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   713
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   714
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   715
   p = buf;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   716
   while (p < buf + size) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   717
      ELF_NHDR* notep = (ELF_NHDR*) p;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   718
      char* descdata  = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   719
      print_debug("Note header with n_type = %d and n_descsz = %u\n",
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   720
                                   notep->n_type, notep->n_descsz);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   721
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   722
      if (notep->n_type == NT_PRSTATUS) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   723
        if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   724
          return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   725
        }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   726
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   727
      p = descdata + ROUNDUP(notep->n_descsz, 4);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   728
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   729
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   730
   free(buf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   731
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   732
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   733
err:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   734
   if (buf) free(buf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   735
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   736
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   737
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   738
// read all segments from core file
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   739
static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   740
   int i = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   741
   ELF_PHDR* phbuf = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   742
   ELF_PHDR* core_php = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   743
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   744
   if ((phbuf =  read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   745
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   746
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   747
   /*
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   748
    * Now iterate through the program headers in the core file.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   749
    * We're interested in two types of Phdrs: PT_NOTE (which
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   750
    * contains a set of saved /proc structures), and PT_LOAD (which
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   751
    * represents a memory mapping from the process's address space).
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   752
    *
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   753
    * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE:
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   754
    *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   755
    *     In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   756
    *     contains /proc structs in the pre-2.6 unstructured /proc format. the last
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   757
    *     PT_NOTE has data in new /proc format.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   758
    *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   759
    *     In Solaris, there is only one pstatus (process status). pstatus contains
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   760
    *     integer register set among other stuff. For each LWP, we have one lwpstatus
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   761
    *     entry that has integer regset for that LWP.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   762
    *
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   763
    *     Linux threads are actually 'clone'd processes. To support core analysis
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   764
    *     of "multithreaded" process, Linux creates more than one pstatus (called
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   765
    *     "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   766
    *     "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   767
    *     function "elf_core_dump".
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   768
    */
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   769
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   770
    for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   771
      switch (core_php->p_type) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   772
         case PT_NOTE:
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   773
            if (core_handle_note(ph, core_php) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   774
              goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   775
            }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   776
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   777
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   778
         case PT_LOAD: {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   779
            if (core_php->p_filesz != 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   780
               if (add_map_info(ph, ph->core->core_fd, core_php->p_offset,
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   781
                  core_php->p_vaddr, core_php->p_filesz) == NULL) goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   782
            }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   783
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   784
         }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   785
      }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   786
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   787
      core_php++;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   788
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   789
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   790
   free(phbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   791
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   792
err:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   793
   free(phbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   794
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   795
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   796
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   797
// read segments of a shared object
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   798
static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   799
  int i = 0;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   800
  ELF_PHDR* phbuf;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   801
  ELF_PHDR* lib_php = NULL;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   802
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   803
  int page_size=sysconf(_SC_PAGE_SIZE);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   804
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   805
  if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   806
    return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   807
  }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   808
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   809
  // we want to process only PT_LOAD segments that are not writable.
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   810
  // i.e., text segments. The read/write/exec (data) segments would
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   811
  // have been already added from core file segments.
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   812
  for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   813
    if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   814
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   815
      uintptr_t target_vaddr = lib_php->p_vaddr + lib_base;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   816
      map_info *existing_map = core_lookup(ph, target_vaddr);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   817
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   818
      if (existing_map == NULL){
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   819
        if (add_map_info(ph, lib_fd, lib_php->p_offset,
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   820
                          target_vaddr, lib_php->p_filesz) == NULL) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   821
          goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   822
        }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   823
      } else {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   824
        if ((existing_map->memsz != page_size) &&
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   825
            (existing_map->fd != lib_fd) &&
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   826
            (existing_map->memsz != lib_php->p_filesz)){
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   827
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   828
          print_debug("address conflict @ 0x%lx (size = %ld, flags = %d\n)",
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   829
                        target_vaddr, lib_php->p_filesz, lib_php->p_flags);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   830
          goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   831
        }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   832
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   833
        /* replace PT_LOAD segment with library segment */
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   834
        print_debug("overwrote with new address mapping (memsz %ld -> %ld)\n",
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   835
                     existing_map->memsz, lib_php->p_filesz);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   836
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   837
        existing_map->fd = lib_fd;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   838
        existing_map->offset = lib_php->p_offset;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   839
        existing_map->memsz = lib_php->p_filesz;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   840
      }
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   841
    }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   842
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   843
    lib_php++;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   844
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   845
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   846
  free(phbuf);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   847
  return true;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   848
err:
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   849
  free(phbuf);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   850
  return false;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   851
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   852
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   853
// process segments from interpreter (ld.so or ld-linux.so or ld-elf.so)
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   854
static bool read_interp_segments(struct ps_prochandle* ph) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   855
   ELF_EHDR interp_ehdr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   856
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   857
   if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   858
       print_debug("interpreter is not a valid ELF file\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   859
       return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   860
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   861
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   862
   if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   863
       print_debug("can't read segments of interpreter\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   864
       return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   865
   }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   866
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   867
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   868
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   869
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   870
// process segments of a a.out
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   871
static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   872
   int i = 0;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   873
   ELF_PHDR* phbuf = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   874
   ELF_PHDR* exec_php = NULL;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   875
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   876
   if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   877
      return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   878
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   879
   for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   880
      switch (exec_php->p_type) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   881
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   882
         // add mappings for PT_LOAD segments
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   883
         case PT_LOAD: {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   884
            // add only non-writable segments of non-zero filesz
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   885
            if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   886
               if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   887
            }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   888
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   889
         }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   890
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   891
         // read the interpreter and it's segments
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   892
         case PT_INTERP: {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   893
            char interp_name[BUF_SIZE];
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   894
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   895
            pread(ph->core->exec_fd, interp_name, MIN(exec_php->p_filesz, BUF_SIZE), exec_php->p_offset);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   896
            print_debug("ELF interpreter %s\n", interp_name);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   897
            // read interpreter segments as well
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   898
            if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   899
               print_debug("can't open runtime loader\n");
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   900
               goto err;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   901
            }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   902
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   903
         }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   904
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   905
         // from PT_DYNAMIC we want to read address of first link_map addr
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   906
         case PT_DYNAMIC: {
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   907
            ph->core->dynamic_addr = exec_php->p_vaddr;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   908
            print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   909
            break;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   910
         }
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   911
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   912
      } // switch
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   913
      exec_php++;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   914
   } // for
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   915
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   916
   free(phbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   917
   return true;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   918
err:
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   919
   free(phbuf);
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   920
   return false;
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   921
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   922
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   923
#define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug,  r_map)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   924
#define LD_BASE_OFFSET        offsetof(struct r_debug,  r_ldbase)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   925
#define LINK_MAP_ADDR_OFFSET  offsetof(struct link_map, l_addr)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   926
#define LINK_MAP_NAME_OFFSET  offsetof(struct link_map, l_name)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   927
#define LINK_MAP_NEXT_OFFSET  offsetof(struct link_map, l_next)
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   928
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   929
// read shared library info from runtime linker's data structures.
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   930
// This work is done by librtlb_db in Solaris
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   931
static bool read_shared_lib_info(struct ps_prochandle* ph) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   932
  uintptr_t addr = ph->core->dynamic_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   933
  uintptr_t debug_base;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   934
  uintptr_t first_link_map_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   935
  uintptr_t ld_base_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   936
  uintptr_t link_map_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   937
  uintptr_t lib_base_diff;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   938
  uintptr_t lib_base;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   939
  uintptr_t lib_name_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   940
  char lib_name[BUF_SIZE];
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   941
  ELF_DYN dyn;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   942
  ELF_EHDR elf_ehdr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   943
  int lib_fd;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   944
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   945
  // _DYNAMIC has information of the form
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   946
  //         [tag] [data] [tag] [data] .....
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   947
  // Both tag and data are pointer sized.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   948
  // We look for dynamic info with DT_DEBUG. This has shared object info.
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   949
  // refer to struct r_debug in link.h
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   950
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   951
  dyn.d_tag = DT_NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   952
  while (dyn.d_tag != DT_DEBUG) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   953
    if (ps_pread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   954
      print_debug("can't read debug info from _DYNAMIC\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   955
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   956
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   957
    addr += sizeof(ELF_DYN);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   958
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   959
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   960
  // we have got Dyn entry with DT_DEBUG
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   961
  debug_base = dyn.d_un.d_ptr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   962
  // at debug_base we have struct r_debug. This has first link map in r_map field
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   963
  if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET,
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   964
                 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   965
    print_debug("can't read first link map address\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   966
    return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   967
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   968
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   969
  // read ld_base address from struct r_debug
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   970
#if 0  // There is no r_ldbase member on BSD
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   971
  if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   972
                  sizeof(uintptr_t)) != PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   973
    print_debug("can't read ld base address\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   974
    return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   975
  }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   976
  ph->core->ld_base_addr = ld_base_addr;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   977
#else
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   978
  ph->core->ld_base_addr = 0;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   979
#endif
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   980
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   981
  print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   982
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   983
  // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so)
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   984
  if (read_interp_segments(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   985
    return false;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   986
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   987
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   988
  // after adding interpreter (ld.so) mappings sort again
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   989
  if (sort_map_array(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   990
    return false;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
   991
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   992
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   993
  print_debug("first link map is at 0x%lx\n", first_link_map_addr);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
   994
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   995
  link_map_addr = first_link_map_addr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   996
  while (link_map_addr != 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   997
    // read library base address of the .so. Note that even though <sys/link.h> calls
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   998
    // link_map->l_addr as "base address",  this is * not * really base virtual
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
   999
    // address of the shared object. This is actually the difference b/w the virtual
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1000
    // address mentioned in shared object and the actual virtual base where runtime
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1001
    // linker loaded it. We use "base diff" in read_lib_segments call below.
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1002
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1003
    if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET,
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1004
                 &lib_base_diff, sizeof(uintptr_t)) != PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1005
      print_debug("can't read shared object base address diff\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1006
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1007
    }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1008
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1009
    // read address of the name
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1010
    if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET,
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1011
                  &lib_name_addr, sizeof(uintptr_t)) != PS_OK) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1012
      print_debug("can't read address of shared object name\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1013
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1014
    }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1015
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1016
    // read name of the shared object
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1017
    if (read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1018
      print_debug("can't read shared object name\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1019
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1020
    }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1021
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1022
    if (lib_name[0] != '\0') {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1023
      // ignore empty lib names
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1024
      lib_fd = pathmap_open(lib_name);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1025
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1026
      if (lib_fd < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1027
        print_debug("can't open shared object %s\n", lib_name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1028
        // continue with other libraries...
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1029
      } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1030
        if (read_elf_header(lib_fd, &elf_ehdr)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1031
          lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1032
          print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n",
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1033
                       lib_name, lib_base, lib_base_diff);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1034
          // while adding library mappings we need to use "base difference".
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1035
          if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1036
            print_debug("can't read shared object's segments\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1037
            close(lib_fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1038
            return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1039
          }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1040
          add_lib_info_fd(ph, lib_name, lib_fd, lib_base);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1041
          // Map info is added for the library (lib_name) so
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1042
          // we need to re-sort it before calling the p_pdread.
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1043
          if (sort_map_array(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1044
            return false;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1045
          }
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1046
        } else {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1047
          print_debug("can't read ELF header for shared object %s\n", lib_name);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1048
          close(lib_fd);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1049
          // continue with other libraries...
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1050
        }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1051
      }
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1052
    }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1053
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1054
    // read next link_map address
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1055
    if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET,
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1056
                  &link_map_addr, sizeof(uintptr_t)) != PS_OK) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1057
      print_debug("can't read next link in link_map\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1058
      return false;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1059
    }
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1060
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1061
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1062
  return true;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1063
}
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1064
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1065
// the one and only one exposed stuff from this file
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1066
struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1067
  ELF_EHDR core_ehdr;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1068
  ELF_EHDR exec_ehdr;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1069
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1070
  struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1071
  if (ph == NULL) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1072
    print_debug("can't allocate ps_prochandle\n");
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1073
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1074
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1075
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1076
  if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1077
    free(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1078
    print_debug("can't allocate ps_prochandle\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1079
    return NULL;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1080
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1081
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1082
  // initialize ph
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1083
  ph->ops = &core_ops;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1084
  ph->core->core_fd   = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1085
  ph->core->exec_fd   = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1086
  ph->core->interp_fd = -1;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1087
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1088
  print_debug("exec: %s   core: %s", exec_file, core_file);
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1089
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1090
  // open the core file
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1091
  if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1092
    print_debug("can't open core file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1093
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1094
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1095
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1096
  // read core file ELF header
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1097
  if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1098
    print_debug("core file is not a valid ELF ET_CORE file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1099
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1100
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1101
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1102
  if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1103
    print_debug("can't open executable file\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1104
    goto err;
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1105
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1106
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1107
  if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1108
    print_debug("executable file is not a valid ELF ET_EXEC file\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1109
    goto err;
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1110
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1111
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1112
  // process core file segments
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1113
  if (read_core_segments(ph, &core_ehdr) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1114
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1115
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1116
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1117
  // process exec file segments
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1118
  if (read_exec_segments(ph, &exec_ehdr) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1119
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1120
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1121
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1122
  // exec file is also treated like a shared object for symbol search
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1123
  if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd,
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1124
                      (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1125
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1126
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1127
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1128
  // allocate and sort maps into map_array, we need to do this
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1129
  // here because read_shared_lib_info needs to read from debuggee
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1130
  // address space
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1131
  if (sort_map_array(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1132
    goto err;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1133
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1134
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1135
  if (read_shared_lib_info(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1136
    goto err;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1137
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1138
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1139
  // sort again because we have added more mappings from shared objects
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1140
  if (sort_map_array(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1141
    goto err;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1142
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1143
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1144
  if (init_classsharing_workaround(ph) != true) {
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1145
    goto err;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 17310
diff changeset
  1146
  }
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1147
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1148
  print_debug("Leave Pgrab_core\n");
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1149
  return ph;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1150
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1151
err:
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1152
  Prelease(ph);
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1153
  return NULL;
10565
dc90c239f4ec 7089790: integrate bsd-port changes
never
parents:
diff changeset
  1154
}
16351
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1155
032b310a3e2f 8003348: SA can not read core file on OS
minqi
parents: 13728
diff changeset
  1156
#endif // __APPLE__