src/jdk.hotspot.agent/linux/native/libsaproc/ps_core.c
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58541 8bc609fcd691
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53379
e47074d2d8cc 8181313: SA: Remove libthread_db dependency on Linux
ysuenaga
parents: 51788
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5341
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include <jni.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include <unistd.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
#include <fcntl.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
#include <stdlib.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#include <stddef.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
#include <elf.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
#include <link.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
#include "libproc_impl.h"
58541
8bc609fcd691 8231986: [SA] Consolidate parts of the Linux and MacOSX versions of ps_core.c
iklam
parents: 55400
diff changeset
    34
#include "ps_core_common.h"
53379
e47074d2d8cc 8181313: SA: Remove libthread_db dependency on Linux
ysuenaga
parents: 51788
diff changeset
    35
#include "proc_service.h"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
#include "salibelf.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// This file has the libproc implementation to read core files.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// For live processes, refer to ps_proc.c. Portions of this is adapted
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// /modelled after Solaris libproc.so (in particular Pcore.c)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
//---------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
// functions to handle map_info
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
// Order mappings based on virtual address.  We use this function as the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
// callback for sorting the array of map_info pointers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
static int core_cmp_mapping(const void *lhsp, const void *rhsp)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
{
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    50
  const map_info *lhs = *((const map_info **)lhsp);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    51
  const map_info *rhs = *((const map_info **)rhsp);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    53
  if (lhs->vaddr == rhs->vaddr) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    54
    return (0);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    55
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    57
  return (lhs->vaddr < rhs->vaddr ? -1 : 1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
// we sort map_info by starting virtual address so that we can do
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// binary search to read from an address.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
static bool sort_map_array(struct ps_prochandle* ph) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    63
  size_t num_maps = ph->core->num_maps;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    64
  map_info* map = ph->core->maps;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    65
  int i = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    67
  // allocate map_array
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    68
  map_info** array;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    69
  if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    70
    print_debug("can't allocate memory for map array\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    71
    return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    72
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    74
  // add maps to array
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    75
  while (map) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    76
    array[i] = map;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    77
    i++;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    78
    map = map->next;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    79
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    81
  // sort is called twice. If this is second time, clear map array
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    82
  if (ph->core->map_array) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    83
    free(ph->core->map_array);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    84
  }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    85
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    86
  ph->core->map_array = array;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    87
  // sort the map_info array by base virtual address.
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    88
  qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    89
        core_cmp_mapping);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    91
  // print map
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    92
  if (is_debug()) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    93
    int j = 0;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    94
    print_debug("---- sorted virtual address map ----\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    95
    for (j = 0; j < ph->core->num_maps; j++) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    96
      print_debug("base = 0x%lx\tsize = %zu\n", ph->core->map_array[j]->vaddr,
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    97
                  ph->core->map_array[j]->memsz);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    98
    }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
    99
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   101
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
#ifndef MIN
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
#define MIN(x, y) (((x) < (y))? (x): (y))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
   ssize_t resid = size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
   int page_size=sysconf(_SC_PAGE_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
   while (resid != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
      map_info *mp = core_lookup(ph, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      uintptr_t mapoff;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      ssize_t len, rem;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
      off_t off;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
      int fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   118
      if (mp == NULL) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
         break;  /* No mapping for this address */
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   120
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      fd = mp->fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      mapoff = addr - mp->vaddr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      len = MIN(resid, mp->memsz - mapoff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      off = mp->offset + mapoff;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   127
      if ((len = pread(fd, buf, len, off)) <= 0) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
         break;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   129
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
      resid -= len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      addr += len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
      buf = (char *)buf + len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      // mappings always start at page boundary. But, may end in fractional
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
      // page. fill zeros for possible fractional page at the end of a mapping.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
      rem = mp->memsz % page_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      if (rem > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
         rem = page_size - rem;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
         len = MIN(resid, rem);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
         resid -= len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
         addr += len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
         // we are not assuming 'buf' to be zero initialized.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
         memset(buf, 0, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
         buf += len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
   if (resid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
              size, addr, resid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
   } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
// null implementation for write
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
static bool core_write_data(struct ps_prochandle* ph,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
                             uintptr_t addr, const char *buf , size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
                          struct user_regs_struct* regs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
   // for core we have cached the lwp regs from NOTE section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
   thread_info* thr = ph->threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
   while (thr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
     if (thr->lwp_id == lwp_id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
       memcpy(regs, &thr->regs, sizeof(struct user_regs_struct));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
       return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
     thr = thr->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
static ps_prochandle_ops core_ops = {
223
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   179
   .release=  core_release,
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   180
   .p_pread=  core_read_data,
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   181
   .p_pwrite= core_write_data,
5c3b023117d9 6452081: 3/4 Allow for Linux builds with Sun Studio Linux compilers
dcubed
parents: 1
diff changeset
   182
   .get_lwp_regs= core_get_lwp_regs
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
// read regs and create thread from NT_PRSTATUS entries from core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
   // we have to read prstatus_t from buf
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
   // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
   prstatus_t* prstat = (prstatus_t*) buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
   thread_info* newthr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
   print_debug("got integer regset for lwp %d\n", prstat->pr_pid);
53379
e47074d2d8cc 8181313: SA: Remove libthread_db dependency on Linux
ysuenaga
parents: 51788
diff changeset
   192
   if((newthr = add_thread_info(ph, prstat->pr_pid)) == NULL)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
   // copy regs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
   memcpy(&newthr->regs, prstat->pr_reg, sizeof(struct user_regs_struct));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
   if (is_debug()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
      print_debug("integer regset\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
#ifdef i386
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
      // print the regset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
      print_debug("\teax = 0x%x\n", newthr->regs.eax);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
      print_debug("\tebx = 0x%x\n", newthr->regs.ebx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
      print_debug("\tecx = 0x%x\n", newthr->regs.ecx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
      print_debug("\tedx = 0x%x\n", newthr->regs.edx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
      print_debug("\tesp = 0x%x\n", newthr->regs.esp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
      print_debug("\tebp = 0x%x\n", newthr->regs.ebp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
      print_debug("\tesi = 0x%x\n", newthr->regs.esi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
      print_debug("\tedi = 0x%x\n", newthr->regs.edi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
      print_debug("\teip = 0x%x\n", newthr->regs.eip);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
#if defined(amd64) || defined(x86_64)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
      // print the regset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
      print_debug("\tr15 = 0x%lx\n", newthr->regs.r15);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
      print_debug("\tr14 = 0x%lx\n", newthr->regs.r14);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
      print_debug("\tr13 = 0x%lx\n", newthr->regs.r13);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
      print_debug("\tr12 = 0x%lx\n", newthr->regs.r12);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
      print_debug("\trbp = 0x%lx\n", newthr->regs.rbp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
      print_debug("\trbx = 0x%lx\n", newthr->regs.rbx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      print_debug("\tr11 = 0x%lx\n", newthr->regs.r11);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
      print_debug("\tr10 = 0x%lx\n", newthr->regs.r10);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
      print_debug("\tr9 = 0x%lx\n", newthr->regs.r9);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
      print_debug("\tr8 = 0x%lx\n", newthr->regs.r8);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
      print_debug("\trax = 0x%lx\n", newthr->regs.rax);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
      print_debug("\trcx = 0x%lx\n", newthr->regs.rcx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
      print_debug("\trdx = 0x%lx\n", newthr->regs.rdx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
      print_debug("\trsi = 0x%lx\n", newthr->regs.rsi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
      print_debug("\trdi = 0x%lx\n", newthr->regs.rdi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
      print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
      print_debug("\trip = 0x%lx\n", newthr->regs.rip);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      print_debug("\tcs = 0x%lx\n", newthr->regs.cs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      print_debug("\teflags = 0x%lx\n", newthr->regs.eflags);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
      print_debug("\trsp = 0x%lx\n", newthr->regs.rsp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
      print_debug("\tss = 0x%lx\n", newthr->regs.ss);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      print_debug("\tds = 0x%lx\n", newthr->regs.ds);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
      print_debug("\tes = 0x%lx\n", newthr->regs.es);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      print_debug("\tfs = 0x%lx\n", newthr->regs.fs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
      print_debug("\tgs = 0x%lx\n", newthr->regs.gs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
#define ROUNDUP(x, y)  ((((x)+((y)-1))/(y))*(y))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
// read NT_PRSTATUS entries from core NOTE segment
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
   char* buf = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
   char* p = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
   size_t size = note_phdr->p_filesz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
   // we are interested in just prstatus entries. we will ignore the rest.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
   // Advance the seek pointer to the start of the PT_NOTE data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
   if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
      print_debug("failed to lseek to PT_NOTE data\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
   // Now process the PT_NOTE structures.  Each one is preceded by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
   // an Elf{32/64}_Nhdr structure describing its type and size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
   if ( (buf = (char*) malloc(size)) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
      print_debug("can't allocate memory for reading core notes\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
   // read notes into buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
   if (read(ph->core->core_fd, buf, size) != size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
      print_debug("failed to read notes, core file must have been truncated\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
   p = buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
   while (p < buf + size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
      ELF_NHDR* notep = (ELF_NHDR*) p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
      char* descdata  = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
      print_debug("Note header with n_type = %d and n_descsz = %u\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
                                   notep->n_type, notep->n_descsz);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
      if (notep->n_type == NT_PRSTATUS) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   284
        if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   285
          return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   286
        }
43950
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   287
      } else if (notep->n_type == NT_AUXV) {
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   288
        // Get first segment from entry point
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   289
        ELF_AUXV *auxv = (ELF_AUXV *)descdata;
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   290
        while (auxv->a_type != AT_NULL) {
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   291
          if (auxv->a_type == AT_ENTRY) {
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   292
            // Set entry point address to address of dynamic section.
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   293
            // We will adjust it in read_exec_segments().
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   294
            ph->core->dynamic_addr = auxv->a_un.a_val;
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   295
            break;
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   296
          }
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   297
          auxv++;
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   298
        }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
      p = descdata + ROUNDUP(notep->n_descsz, 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
   free(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
err:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
   if (buf) free(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
// read all segments from core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
   int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
   ELF_PHDR* phbuf = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
   ELF_PHDR* core_php = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
   if ((phbuf =  read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
   /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
    * Now iterate through the program headers in the core file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
    * We're interested in two types of Phdrs: PT_NOTE (which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
    * contains a set of saved /proc structures), and PT_LOAD (which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
    * represents a memory mapping from the process's address space).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    *
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   326
    * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    *     In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    *     contains /proc structs in the pre-2.6 unstructured /proc format. the last
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    *     PT_NOTE has data in new /proc format.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    *     In Solaris, there is only one pstatus (process status). pstatus contains
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    *     integer register set among other stuff. For each LWP, we have one lwpstatus
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    *     entry that has integer regset for that LWP.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    *     Linux threads are actually 'clone'd processes. To support core analysis
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    *     of "multithreaded" process, Linux creates more than one pstatus (called
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    *     "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    *     "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
    *     function "elf_core_dump".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
      switch (core_php->p_type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
         case PT_NOTE:
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   346
            if (core_handle_note(ph, core_php) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   347
              goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   348
            }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
         case PT_LOAD: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
            if (core_php->p_filesz != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
               if (add_map_info(ph, ph->core->core_fd, core_php->p_offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
                  core_php->p_vaddr, core_php->p_filesz) == NULL) goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
      core_php++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
   free(phbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
err:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
   free(phbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
// read segments of a shared object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) {
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   372
  int i = 0;
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   373
  ELF_PHDR* phbuf;
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   374
  ELF_PHDR* lib_php = NULL;
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   375
21069
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   376
  int page_size = sysconf(_SC_PAGE_SIZE);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   378
  if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) {
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   379
    return false;
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   380
  }
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   381
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   382
  // we want to process only PT_LOAD segments that are not writable.
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   383
  // i.e., text segments. The read/write/exec (data) segments would
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   384
  // have been already added from core file segments.
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   385
  for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) {
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   386
    if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) {
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   387
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   388
      uintptr_t target_vaddr = lib_php->p_vaddr + lib_base;
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   389
      map_info *existing_map = core_lookup(ph, target_vaddr);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   391
      if (existing_map == NULL){
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   392
        if (add_map_info(ph, lib_fd, lib_php->p_offset,
21069
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   393
                          target_vaddr, lib_php->p_memsz) == NULL) {
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   394
          goto err;
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   395
        }
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   396
      } else {
21069
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   397
        // Coredump stores value of p_memsz elf field
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   398
        // rounded up to page boundary.
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   399
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   400
        if ((existing_map->memsz != page_size) &&
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   401
            (existing_map->fd != lib_fd) &&
21069
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   402
            (ROUNDUP(existing_map->memsz, page_size) != ROUNDUP(lib_php->p_memsz, page_size))) {
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   403
21069
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   404
          print_debug("address conflict @ 0x%lx (existing map size = %ld, size = %ld, flags = %d)\n",
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   405
                        target_vaddr, existing_map->memsz, lib_php->p_memsz, lib_php->p_flags);
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   406
          goto err;
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   407
        }
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   408
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   409
        /* replace PT_LOAD segment with library segment */
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   410
        print_debug("overwrote with new address mapping (memsz %ld -> %ld)\n",
21069
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   411
                     existing_map->memsz, ROUNDUP(lib_php->p_memsz, page_size));
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   412
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   413
        existing_map->fd = lib_fd;
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   414
        existing_map->offset = lib_php->p_offset;
21069
728330d2593a 8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file
dsamersoff
parents: 20375
diff changeset
   415
        existing_map->memsz = ROUNDUP(lib_php->p_memsz, page_size);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
      }
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   417
    }
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   418
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   419
    lib_php++;
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   420
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   422
  free(phbuf);
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   423
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
err:
20068
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   425
  free(phbuf);
e31661417973 7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
dsamersoff
parents: 17310
diff changeset
   426
  return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
// process segments from interpreter (ld.so or ld-linux.so)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
static bool read_interp_segments(struct ps_prochandle* ph) {
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   431
  ELF_EHDR interp_ehdr;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   433
  if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) {
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   434
    print_debug("interpreter is not a valid ELF file\n");
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   435
    return false;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   436
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   438
  if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) {
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   439
    print_debug("can't read segments of interpreter\n");
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   440
    return false;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   441
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   443
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
// process segments of a a.out
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) {
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   448
  int i = 0;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   449
  ELF_PHDR* phbuf = NULL;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   450
  ELF_PHDR* exec_php = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   452
  if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL) {
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   453
    return false;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   454
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   456
  for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) {
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   457
    switch (exec_php->p_type) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   459
      // add mappings for PT_LOAD segments
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   460
    case PT_LOAD: {
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   461
      // add only non-writable segments of non-zero filesz
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   462
      if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) {
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   463
        if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   464
      }
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   465
      break;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   466
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   468
    // read the interpreter and it's segments
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   469
    case PT_INTERP: {
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   470
      char interp_name[BUF_SIZE + 1];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   472
      // BUF_SIZE is PATH_MAX + NAME_MAX + 1.
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   473
      if (exec_php->p_filesz > BUF_SIZE) {
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   474
        goto err;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   475
      }
51788
c0f9161f591e 8210836: Build fails with warn_unused_result in openjdk/src/jdk.hotspot.agent/linux/native/libsaproc/ps_core.c
jgeorge
parents: 51477
diff changeset
   476
      if (pread(ph->core->exec_fd, interp_name,
c0f9161f591e 8210836: Build fails with warn_unused_result in openjdk/src/jdk.hotspot.agent/linux/native/libsaproc/ps_core.c
jgeorge
parents: 51477
diff changeset
   477
                exec_php->p_filesz, exec_php->p_offset) != exec_php->p_filesz) {
c0f9161f591e 8210836: Build fails with warn_unused_result in openjdk/src/jdk.hotspot.agent/linux/native/libsaproc/ps_core.c
jgeorge
parents: 51477
diff changeset
   478
        print_debug("Unable to read in the ELF interpreter\n");
c0f9161f591e 8210836: Build fails with warn_unused_result in openjdk/src/jdk.hotspot.agent/linux/native/libsaproc/ps_core.c
jgeorge
parents: 51477
diff changeset
   479
        goto err;
c0f9161f591e 8210836: Build fails with warn_unused_result in openjdk/src/jdk.hotspot.agent/linux/native/libsaproc/ps_core.c
jgeorge
parents: 51477
diff changeset
   480
      }
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   481
      interp_name[exec_php->p_filesz] = '\0';
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   482
      print_debug("ELF interpreter %s\n", interp_name);
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   483
      // read interpreter segments as well
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   484
      if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) {
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   485
        print_debug("can't open runtime loader\n");
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   486
        goto err;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   487
      }
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   488
      break;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   489
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   491
    // from PT_DYNAMIC we want to read address of first link_map addr
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   492
    case PT_DYNAMIC: {
43950
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   493
      if (exec_ehdr->e_type == ET_EXEC) {
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   494
        ph->core->dynamic_addr = exec_php->p_vaddr;
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   495
      } else { // ET_DYN
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   496
        // dynamic_addr has entry point of executable.
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   497
        // Thus we should substract it.
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   498
        ph->core->dynamic_addr += exec_php->p_vaddr - exec_ehdr->e_entry;
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   499
      }
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   500
      print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   501
      break;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   502
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   504
    } // switch
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   505
    exec_php++;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   506
  } // for
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
33794
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   508
  free(phbuf);
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   509
  return true;
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   510
 err:
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   511
  free(phbuf);
41ef3dc95179 8140482: Various minor code improvements (runtime)
goetz
parents: 21069
diff changeset
   512
  return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
#define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug,  r_map)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
#define LD_BASE_OFFSET        offsetof(struct r_debug,  r_ldbase)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
#define LINK_MAP_ADDR_OFFSET  offsetof(struct link_map, l_addr)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
#define LINK_MAP_NAME_OFFSET  offsetof(struct link_map, l_name)
55400
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   520
#define LINK_MAP_LD_OFFSET    offsetof(struct link_map, l_ld)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
#define LINK_MAP_NEXT_OFFSET  offsetof(struct link_map, l_next)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
55400
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   523
// Calculate the load address of shared library
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   524
// on prelink-enabled environment.
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   525
//
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   526
// In case of GDB, it would be calculated by offset of link_map.l_ld
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   527
// and the address of .dynamic section.
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   528
// See GDB implementation: lm_addr_check @ solib-svr4.c
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   529
static uintptr_t calc_prelinked_load_address(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* elf_ehdr, uintptr_t link_map_addr) {
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   530
  ELF_PHDR *phbuf;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   531
  uintptr_t lib_ld;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   532
  uintptr_t lib_dyn_addr = 0L;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   533
  uintptr_t load_addr;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   534
  int i;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   535
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   536
  phbuf = read_program_header_table(lib_fd, elf_ehdr);
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   537
  if (phbuf == NULL) {
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   538
    print_debug("can't read program header of shared object\n");
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   539
    return 0L;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   540
  }
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   541
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   542
  // Get the address of .dynamic section from shared library.
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   543
  for (i = 0; i < elf_ehdr->e_phnum; i++) {
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   544
    if (phbuf[i].p_type == PT_DYNAMIC) {
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   545
      lib_dyn_addr = phbuf[i].p_vaddr;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   546
      break;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   547
    }
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   548
  }
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   549
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   550
  free(phbuf);
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   551
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   552
  if (ps_pdread(ph, (psaddr_t)link_map_addr + LINK_MAP_LD_OFFSET,
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   553
               &lib_ld, sizeof(uintptr_t)) != PS_OK) {
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   554
    print_debug("can't read address of dynamic section in shared object\n");
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   555
    return 0L;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   556
  }
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   557
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   558
  // Return the load address which is calculated by the address of .dynamic
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   559
  // and link_map.l_ld .
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   560
  load_addr = lib_ld - lib_dyn_addr;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   561
  print_debug("lib_ld = 0x%lx, lib_dyn_addr = 0x%lx -> lib_base_diff = 0x%lx\n", lib_ld, lib_dyn_addr, load_addr);
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   562
  return load_addr;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   563
}
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   564
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
// read shared library info from runtime linker's data structures.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
// This work is done by librtlb_db in Solaris
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
static bool read_shared_lib_info(struct ps_prochandle* ph) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   568
  uintptr_t addr = ph->core->dynamic_addr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   569
  uintptr_t debug_base;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   570
  uintptr_t first_link_map_addr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   571
  uintptr_t ld_base_addr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   572
  uintptr_t link_map_addr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   573
  uintptr_t lib_base_diff;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   574
  uintptr_t lib_base;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   575
  uintptr_t lib_name_addr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   576
  char lib_name[BUF_SIZE];
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   577
  ELF_DYN dyn;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   578
  ELF_EHDR elf_ehdr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   579
  int lib_fd;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   581
  // _DYNAMIC has information of the form
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   582
  //         [tag] [data] [tag] [data] .....
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   583
  // Both tag and data are pointer sized.
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   584
  // We look for dynamic info with DT_DEBUG. This has shared object info.
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   585
  // refer to struct r_debug in link.h
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   586
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   587
  dyn.d_tag = DT_NULL;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   588
  while (dyn.d_tag != DT_DEBUG) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   589
    if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   590
      print_debug("can't read debug info from _DYNAMIC\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   591
      return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   592
    }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   593
    addr += sizeof(ELF_DYN);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   594
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   596
  // we have got Dyn entry with DT_DEBUG
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   597
  debug_base = dyn.d_un.d_ptr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   598
  // at debug_base we have struct r_debug. This has first link map in r_map field
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   599
  if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET,
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
                 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   601
    print_debug("can't read first link map address\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   602
    return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   603
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   605
  // read ld_base address from struct r_debug
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   606
  if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
                 sizeof(uintptr_t)) != PS_OK) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   608
    print_debug("can't read ld base address\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   609
    return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   610
  }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   611
  ph->core->ld_base_addr = ld_base_addr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   612
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   613
  print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   615
  // 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: 20068
diff changeset
   616
  if (read_interp_segments(ph) != true) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
      return false;
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   618
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   620
  // after adding interpreter (ld.so) mappings sort again
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   621
  if (sort_map_array(ph) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   622
    return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   623
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
   print_debug("first link map is at 0x%lx\n", first_link_map_addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
   link_map_addr = first_link_map_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
   while (link_map_addr != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
      // read library base address of the .so. Note that even though <sys/link.h> calls
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
      // link_map->l_addr as "base address",  this is * not * really base virtual
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
      // address of the shared object. This is actually the difference b/w the virtual
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
      // address mentioned in shared object and the actual virtual base where runtime
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
      // linker loaded it. We use "base diff" in read_lib_segments call below.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
      if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
                   &lib_base_diff, sizeof(uintptr_t)) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
         print_debug("can't read shared object base address diff\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
         return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
      // read address of the name
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
      if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
                    &lib_name_addr, sizeof(uintptr_t)) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
         print_debug("can't read address of shared object name\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
         return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
      // read name of the shared object
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 2105
diff changeset
   649
      lib_name[0] = '\0';
290a02b4adb3 6945219: minor SA fixes
never
parents: 2105
diff changeset
   650
      if (lib_name_addr != 0 &&
290a02b4adb3 6945219: minor SA fixes
never
parents: 2105
diff changeset
   651
          read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
         print_debug("can't read shared object name\n");
5341
290a02b4adb3 6945219: minor SA fixes
never
parents: 2105
diff changeset
   653
         // don't let failure to read the name stop opening the file.  If something is really wrong
290a02b4adb3 6945219: minor SA fixes
never
parents: 2105
diff changeset
   654
         // it will fail later.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
      if (lib_name[0] != '\0') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
         // ignore empty lib names
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
         lib_fd = pathmap_open(lib_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
         if (lib_fd < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
            print_debug("can't open shared object %s\n", lib_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
            // continue with other libraries...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
            if (read_elf_header(lib_fd, &elf_ehdr)) {
55400
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   666
               if (lib_base_diff == 0x0L) {
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   667
                 lib_base_diff = calc_prelinked_load_address(ph, lib_fd, &elf_ehdr, link_map_addr);
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   668
                 if (lib_base_diff == 0x0L) {
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   669
                   close(lib_fd);
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   670
                   return false;
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   671
                 }
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   672
               }
da554fdb51d0 8225636: SA can't handle prelinked libraries
ysuenaga
parents: 53379
diff changeset
   673
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
               lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
               print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
                           lib_name, lib_base, lib_base_diff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
               // while adding library mappings we need to use "base difference".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
               if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
                  print_debug("can't read shared object's segments\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
                  close(lib_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
                  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
               }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
               add_lib_info_fd(ph, lib_name, lib_fd, lib_base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
               // Map info is added for the library (lib_name) so
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
               // we need to re-sort it before calling the p_pdread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
               if (sort_map_array(ph) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
                  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
               print_debug("can't read ELF header for shared object %s\n", lib_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
               close(lib_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
               // continue with other libraries...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   696
    // read next link_map address
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   697
    if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET,
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   698
                   &link_map_addr, sizeof(uintptr_t)) != PS_OK) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   699
      print_debug("can't read next link in link_map\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   700
      return false;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   701
    }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   702
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   704
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
// the one and only one exposed stuff from this file
49440
396ea30afbd5 8200178: Remove mapfiles for JDK native libraries
ihse
parents: 47216
diff changeset
   708
JNIEXPORT struct ps_prochandle* JNICALL
396ea30afbd5 8200178: Remove mapfiles for JDK native libraries
ihse
parents: 47216
diff changeset
   709
Pgrab_core(const char* exec_file, const char* core_file) {
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   710
  ELF_EHDR core_ehdr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   711
  ELF_EHDR exec_ehdr;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   712
  ELF_EHDR lib_ehdr;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   714
  struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   715
  if (ph == NULL) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   716
    print_debug("can't allocate ps_prochandle\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   717
    return NULL;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   718
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   720
  if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   721
    free(ph);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   722
    print_debug("can't allocate ps_prochandle\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   723
    return NULL;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   724
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   726
  // initialize ph
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   727
  ph->ops = &core_ops;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   728
  ph->core->core_fd   = -1;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   729
  ph->core->exec_fd   = -1;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   730
  ph->core->interp_fd = -1;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   732
  // open the core file
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   733
  if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   734
    print_debug("can't open core file\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   735
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   736
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   738
  // read core file ELF header
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   739
  if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   740
    print_debug("core file is not a valid ELF ET_CORE file\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   741
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   742
  }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   743
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   744
  if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   745
    print_debug("can't open executable file\n");
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   746
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   747
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
43950
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   749
  if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true ||
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   750
      ((exec_ehdr.e_type != ET_EXEC) && (exec_ehdr.e_type != ET_DYN))) {
5e482ff92cf5 8173941: SA does not work if executable is DSO
ysuenaga
parents: 35217
diff changeset
   751
    print_debug("executable file is not a valid ELF file\n");
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   752
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   753
  }
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   754
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   755
  // process core file segments
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   756
  if (read_core_segments(ph, &core_ehdr) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   757
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   758
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   760
  // process exec file segments
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   761
  if (read_exec_segments(ph, &exec_ehdr) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   762
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   763
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   765
  // exec file is also treated like a shared object for symbol search
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   766
  if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd,
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   767
                      (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: 20068
diff changeset
   768
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   769
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   771
  // allocate and sort maps into map_array, we need to do this
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   772
  // here because read_shared_lib_info needs to read from debuggee
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   773
  // address space
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   774
  if (sort_map_array(ph) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   775
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   776
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   778
  if (read_shared_lib_info(ph) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   779
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   780
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   782
  // sort again because we have added more mappings from shared objects
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   783
  if (sort_map_array(ph) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   784
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   785
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   787
  if (init_classsharing_workaround(ph) != true) {
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   788
    goto err;
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   789
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   791
  return ph;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
err:
20375
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   794
  Prelease(ph);
9e82f2c36626 8025250: SA: Sync linux and bsd versions of ps_core file
dsamersoff
parents: 20068
diff changeset
   795
  return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
}