hotspot/agent/src/os/linux/ps_core.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 223 5c3b023117d9
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
#include "salibelf.h"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// This file has the libproc implementation to read core files.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// For live processes, refer to ps_proc.c. Portions of this is adapted
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// /modelled after Solaris libproc.so (in particular Pcore.c)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
//----------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// ps_prochandle cleanup helper functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
// close all file descriptors
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
static void close_elf_files(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
   lib_info* lib = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
   // close core file descriptor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
   if (ph->core->core_fd >= 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
     close(ph->core->core_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
   // close exec file descriptor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
   if (ph->core->exec_fd >= 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
     close(ph->core->exec_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
   // close interp file descriptor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
   if (ph->core->interp_fd >= 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
     close(ph->core->interp_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
   // close class share archive file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
   if (ph->core->classes_jsa_fd >= 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
     close(ph->core->classes_jsa_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
   // close all library file descriptors
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
   lib = ph->libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
   while (lib) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
      int fd = lib->fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
      if (fd >= 0 && fd != ph->core->exec_fd) close(fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
      lib = lib->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
// clean all map_info stuff
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
static void destroy_map_info(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  map_info* map = ph->core->maps;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  while (map) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
     map_info* next = map->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
     free(map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
     map = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  if (ph->core->map_array) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
     free(ph->core->map_array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  // Part of the class sharing workaround
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  map = ph->core->class_share_maps;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  while (map) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
     map_info* next = map->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
     free(map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
     map = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
// ps_prochandle operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
static void core_release(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
   if (ph->core) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      close_elf_files(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      destroy_map_info(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      free(ph->core);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
   map_info* map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
   if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
      print_debug("can't allocate memory for map_info\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
   // initialize map
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
   map->fd     = fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
   map->offset = offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
   map->vaddr  = vaddr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
   map->memsz  = memsz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
   return map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
// add map info with given fd, offset, vaddr and memsz
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
                             uintptr_t vaddr, size_t memsz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
   map_info* map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
   if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
   // add this to map list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
   map->next  = ph->core->maps;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
   ph->core->maps   = map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
   ph->core->num_maps++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
   return map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
// Part of the class sharing workaround
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                             uintptr_t vaddr, size_t memsz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
   map_info* map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
   if ((map = allocate_init_map(ph->core->classes_jsa_fd,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
                                offset, vaddr, memsz)) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
   map->next = ph->core->class_share_maps;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
   ph->core->class_share_maps = map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
// Return the map_info for the given virtual address.  We keep a sorted
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
// array of pointers in ph->map_array, so we can binary search.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
   int mid, lo = 0, hi = ph->core->num_maps - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
   map_info *mp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
   while (hi - lo > 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
     mid = (lo + hi) / 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
      if (addr >= ph->core->map_array[mid]->vaddr)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
         lo = mid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
      else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
         hi = mid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
   if (addr < ph->core->map_array[hi]->vaddr)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      mp = ph->core->map_array[lo];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
   else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      mp = ph->core->map_array[hi];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
   if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
      return (mp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
   // Part of the class sharing workaround
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
   // Unfortunately, we have no way of detecting -Xshare state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
   // Check out the share maps atlast, if we don't find anywhere.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
   // This is done this way so to avoid reading share pages
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
   // ahead of other normal maps. For eg. with -Xshare:off we don't
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
   // want to prefer class sharing data to data from core.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
   mp = ph->core->class_share_maps;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
   if (mp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
      print_debug("can't locate map_info at 0x%lx, trying class share maps\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
             addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
   while (mp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
         print_debug("located map_info at 0x%lx from class share maps\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
                  addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
         return (mp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
      mp = mp->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
   print_debug("can't locate map_info at 0x%lx\n", addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
   return (NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
//---------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
// Part of the class sharing workaround:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
// With class sharing, pages are mapped from classes[_g].jsa file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
// The read-only class sharing pages are mapped as MAP_SHARED,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
// PROT_READ pages. These pages are not dumped into core dump.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
// With this workaround, these pages are read from classes[_g].jsa.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
// FIXME: !HACK ALERT!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
// The format of sharing achive file header is needed to read shared heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
// file mappings. For now, I am hard coding portion of FileMapHeader here.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
// Refer to filemap.hpp.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
// FileMapHeader describes the shared space data in the file to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
// mapped.  This structure gets written to a file.  It is not a class,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
// so that the compilers don't add any compiler-private data to it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
// Refer to CompactingPermGenGen::n_regions in compactingPermGenGen.hpp
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
#define NUM_SHARED_MAPS 4
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
// Refer to FileMapInfo::_current_version in filemap.hpp
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
#define CURRENT_ARCHIVE_VERSION 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
struct FileMapHeader {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  int   _magic;              // identify file type.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  int   _version;            // (from enum, above.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  size_t _alignment;         // how shared archive should be aligned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  struct space_info {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    int    _file_offset;     // sizeof(this) rounded to vm page size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    char*  _base;            // copy-on-write base address
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    size_t _capacity;        // for validity checking
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    size_t _used;            // for setting space top on read
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    // the C type matching the C++ bool type on any given platform. For
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    // Hotspot on Linux we assume the corresponding C type is char but
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    // licensees on Linux versions may need to adjust the type of these fields.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    char   _read_only;       // read only space?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    char   _allow_exec;      // executable code in space?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  } _space[NUM_SHARED_MAPS]; // was _space[CompactingPermGenGen::n_regions];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  // Ignore the rest of the FileMapHeader. We don't need those fields here.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
static bool read_int(struct ps_prochandle* ph, uintptr_t addr, int* pvalue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
   int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
   if (ps_pdread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
      *pvalue = i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
   } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
   uintptr_t uip;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
   if (ps_pdread(ph, (psaddr_t) addr, &uip, sizeof(uip)) == PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
      *pvalue = uip;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
   } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
// used to read strings from debuggee
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
   size_t i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
   char  c = ' ';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
   while (c != '\0') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
     if (ps_pdread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
         return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
      if (i < size - 1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
         buf[i] = c;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
      else // smaller buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
         return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
      i++; addr++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
   buf[i] = '\0';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
#define USE_SHARED_SPACES_SYM "UseSharedSpaces"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
// mangled name of Arguments::SharedArchivePath
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
#define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
static bool init_classsharing_workaround(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
   lib_info* lib = ph->libs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
   while (lib != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
      // we are iterating over shared objects from the core dump. look for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
      // libjvm[_g].so.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      const char *jvm_name = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
      if ((jvm_name = strstr(lib->name, "/libjvm.so")) != 0 ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
          (jvm_name = strstr(lib->name, "/libjvm_g.so")) != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
         char classes_jsa[PATH_MAX];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
         struct FileMapHeader header;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
         size_t n = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
         int fd = -1, m = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
         uintptr_t base = 0, useSharedSpacesAddr = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
         uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
         int useSharedSpaces = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
         map_info* mi = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
         memset(classes_jsa, 0, sizeof(classes_jsa));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
         jvm_name = lib->name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
         useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
         if (useSharedSpacesAddr == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
            print_debug("can't lookup 'UseSharedSpaces' flag\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
         if (read_int(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
            print_debug("can't read the value of 'UseSharedSpaces' flag\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
         if (useSharedSpaces == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
            print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
            return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
         sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
         if (sharedArchivePathAddrAddr == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
            print_debug("can't lookup shared archive path symbol\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
         if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
            print_debug("can't read shared archive path pointer\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
         if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
            print_debug("can't read shared archive path value\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
         print_debug("looking for %s\n", classes_jsa);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
         // open the class sharing archive file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
         fd = pathmap_open(classes_jsa);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
         if (fd < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
            print_debug("can't open %s!\n", classes_jsa);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
            ph->core->classes_jsa_fd = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
            print_debug("opened %s\n", classes_jsa);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
         // read FileMapHeader from the file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
         memset(&header, 0, sizeof(struct FileMapHeader));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
         if ((n = read(fd, &header, sizeof(struct FileMapHeader)))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
              != sizeof(struct FileMapHeader)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
            print_debug("can't read shared archive file map header from %s\n", classes_jsa);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
            close(fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
         // check file magic
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
         if (header._magic != 0xf00baba2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
            print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
                        classes_jsa, header._magic);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
            close(fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
         // check version
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
         if (header._version != CURRENT_ARCHIVE_VERSION) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
            print_debug("%s has wrong shared archive file version %d, expecting %d\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
                        classes_jsa, header._version, CURRENT_ARCHIVE_VERSION);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
            close(fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
         ph->core->classes_jsa_fd = fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
         // add read-only maps from classes[_g].jsa to the list of maps
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
         for (m = 0; m < NUM_SHARED_MAPS; m++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
            if (header._space[m]._read_only) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
               base = (uintptr_t) header._space[m]._base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
               // no need to worry about the fractional pages at-the-end.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
               // possible fractional pages are handled by core_read_data.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
               add_class_share_map_info(ph, (off_t) header._space[m]._file_offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
                         base, (size_t) header._space[m]._used);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
               print_debug("added a share archive map at 0x%lx\n", base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
         return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
      lib = lib->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
//---------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
// functions to handle map_info
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
// Order mappings based on virtual address.  We use this function as the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
// callback for sorting the array of map_info pointers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
static int core_cmp_mapping(const void *lhsp, const void *rhsp)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
   const map_info *lhs = *((const map_info **)lhsp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
   const map_info *rhs = *((const map_info **)rhsp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
   if (lhs->vaddr == rhs->vaddr)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
      return (0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
   return (lhs->vaddr < rhs->vaddr ? -1 : 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
// we sort map_info by starting virtual address so that we can do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
// binary search to read from an address.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
static bool sort_map_array(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
   size_t num_maps = ph->core->num_maps;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
   map_info* map = ph->core->maps;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
   int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
   // allocate map_array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
   map_info** array;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
   if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
      print_debug("can't allocate memory for map array\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
   // add maps to array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
   while (map) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
      array[i] = map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
      i++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
      map = map->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
   // sort is called twice. If this is second time, clear map array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
   if (ph->core->map_array) free(ph->core->map_array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
   ph->core->map_array = array;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
   // sort the map_info array by base virtual address.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
   qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
            core_cmp_mapping);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
   // print map
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
   if (is_debug()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
      int j = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
      print_debug("---- sorted virtual address map ----\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
      for (j = 0; j < ph->core->num_maps; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
        print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
                                         ph->core->map_array[j]->memsz);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
#ifndef MIN
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
#define MIN(x, y) (((x) < (y))? (x): (y))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
   ssize_t resid = size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
   int page_size=sysconf(_SC_PAGE_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
   while (resid != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
      map_info *mp = core_lookup(ph, addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
      uintptr_t mapoff;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
      ssize_t len, rem;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
      off_t off;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
      int fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
      if (mp == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
         break;  /* No mapping for this address */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
      fd = mp->fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
      mapoff = addr - mp->vaddr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
      len = MIN(resid, mp->memsz - mapoff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
      off = mp->offset + mapoff;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
      if ((len = pread(fd, buf, len, off)) <= 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
         break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
      resid -= len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
      addr += len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
      buf = (char *)buf + len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
      // mappings always start at page boundary. But, may end in fractional
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
      // page. fill zeros for possible fractional page at the end of a mapping.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
      rem = mp->memsz % page_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
      if (rem > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
         rem = page_size - rem;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
         len = MIN(resid, rem);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
         resid -= len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
         addr += len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
         // we are not assuming 'buf' to be zero initialized.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
         memset(buf, 0, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
         buf += len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
   if (resid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
      print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
              size, addr, resid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
   } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
// null implementation for write
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
static bool core_write_data(struct ps_prochandle* ph,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
                             uintptr_t addr, const char *buf , size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
                          struct user_regs_struct* regs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
   // for core we have cached the lwp regs from NOTE section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
   thread_info* thr = ph->threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
   while (thr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
     if (thr->lwp_id == lwp_id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
       memcpy(regs, &thr->regs, sizeof(struct user_regs_struct));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
       return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
     }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
     thr = thr->next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
static ps_prochandle_ops core_ops = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
   release:  core_release,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
   p_pread:  core_read_data,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
   p_pwrite: core_write_data,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
   get_lwp_regs: core_get_lwp_regs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
// read regs and create thread from NT_PRSTATUS entries from core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
   // we have to read prstatus_t from buf
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
   // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
   prstatus_t* prstat = (prstatus_t*) buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
   thread_info* newthr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
   print_debug("got integer regset for lwp %d\n", prstat->pr_pid);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
   // we set pthread_t to -1 for core dump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
   if((newthr = add_thread_info(ph, (pthread_t) -1,  prstat->pr_pid)) == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
   // copy regs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
   memcpy(&newthr->regs, prstat->pr_reg, sizeof(struct user_regs_struct));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
   if (is_debug()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
      print_debug("integer regset\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
#ifdef i386
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
      // print the regset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
      print_debug("\teax = 0x%x\n", newthr->regs.eax);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
      print_debug("\tebx = 0x%x\n", newthr->regs.ebx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
      print_debug("\tecx = 0x%x\n", newthr->regs.ecx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
      print_debug("\tedx = 0x%x\n", newthr->regs.edx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
      print_debug("\tesp = 0x%x\n", newthr->regs.esp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
      print_debug("\tebp = 0x%x\n", newthr->regs.ebp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
      print_debug("\tesi = 0x%x\n", newthr->regs.esi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
      print_debug("\tedi = 0x%x\n", newthr->regs.edi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
      print_debug("\teip = 0x%x\n", newthr->regs.eip);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
#if defined(amd64) || defined(x86_64)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
      // print the regset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
      print_debug("\tr15 = 0x%lx\n", newthr->regs.r15);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
      print_debug("\tr14 = 0x%lx\n", newthr->regs.r14);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
      print_debug("\tr13 = 0x%lx\n", newthr->regs.r13);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
      print_debug("\tr12 = 0x%lx\n", newthr->regs.r12);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
      print_debug("\trbp = 0x%lx\n", newthr->regs.rbp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
      print_debug("\trbx = 0x%lx\n", newthr->regs.rbx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
      print_debug("\tr11 = 0x%lx\n", newthr->regs.r11);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
      print_debug("\tr10 = 0x%lx\n", newthr->regs.r10);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
      print_debug("\tr9 = 0x%lx\n", newthr->regs.r9);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
      print_debug("\tr8 = 0x%lx\n", newthr->regs.r8);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
      print_debug("\trax = 0x%lx\n", newthr->regs.rax);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
      print_debug("\trcx = 0x%lx\n", newthr->regs.rcx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
      print_debug("\trdx = 0x%lx\n", newthr->regs.rdx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
      print_debug("\trsi = 0x%lx\n", newthr->regs.rsi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
      print_debug("\trdi = 0x%lx\n", newthr->regs.rdi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
      print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
      print_debug("\trip = 0x%lx\n", newthr->regs.rip);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
      print_debug("\tcs = 0x%lx\n", newthr->regs.cs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
      print_debug("\teflags = 0x%lx\n", newthr->regs.eflags);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
      print_debug("\trsp = 0x%lx\n", newthr->regs.rsp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
      print_debug("\tss = 0x%lx\n", newthr->regs.ss);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
      print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
      print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
      print_debug("\tds = 0x%lx\n", newthr->regs.ds);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
      print_debug("\tes = 0x%lx\n", newthr->regs.es);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
      print_debug("\tfs = 0x%lx\n", newthr->regs.fs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
      print_debug("\tgs = 0x%lx\n", newthr->regs.gs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
#define ROUNDUP(x, y)  ((((x)+((y)-1))/(y))*(y))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
// read NT_PRSTATUS entries from core NOTE segment
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
   char* buf = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
   char* p = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
   size_t size = note_phdr->p_filesz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
   // we are interested in just prstatus entries. we will ignore the rest.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
   // Advance the seek pointer to the start of the PT_NOTE data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
   if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
      print_debug("failed to lseek to PT_NOTE data\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
   // Now process the PT_NOTE structures.  Each one is preceded by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
   // an Elf{32/64}_Nhdr structure describing its type and size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
   if ( (buf = (char*) malloc(size)) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
      print_debug("can't allocate memory for reading core notes\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
   // read notes into buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
   if (read(ph->core->core_fd, buf, size) != size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
      print_debug("failed to read notes, core file must have been truncated\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
   p = buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
   while (p < buf + size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
      ELF_NHDR* notep = (ELF_NHDR*) p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
      char* descdata  = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
      print_debug("Note header with n_type = %d and n_descsz = %u\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
                                   notep->n_type, notep->n_descsz);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
      if (notep->n_type == NT_PRSTATUS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
         if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
      p = descdata + ROUNDUP(notep->n_descsz, 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
   free(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
err:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
   if (buf) free(buf);
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 all segments from core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
   int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
   ELF_PHDR* phbuf = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
   ELF_PHDR* core_php = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
   if ((phbuf =  read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
   /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
    * Now iterate through the program headers in the core file.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
    * We're interested in two types of Phdrs: PT_NOTE (which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
    * contains a set of saved /proc structures), and PT_LOAD (which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
    * represents a memory mapping from the process's address space).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
    *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
    * Difference b/w Solaris PT_NOTE and Linux PT_NOTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
    *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
    *     In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
    *     contains /proc structs in the pre-2.6 unstructured /proc format. the last
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
    *     PT_NOTE has data in new /proc format.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
    *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
    *     In Solaris, there is only one pstatus (process status). pstatus contains
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
    *     integer register set among other stuff. For each LWP, we have one lwpstatus
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
    *     entry that has integer regset for that LWP.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
    *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
    *     Linux threads are actually 'clone'd processes. To support core analysis
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
    *     of "multithreaded" process, Linux creates more than one pstatus (called
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
    *     "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
    *     "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
    *     function "elf_core_dump".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
    */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
    for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
      switch (core_php->p_type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
         case PT_NOTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
            if (core_handle_note(ph, core_php) != true) goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
         case PT_LOAD: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
            if (core_php->p_filesz != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
               if (add_map_info(ph, ph->core->core_fd, core_php->p_offset,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
                  core_php->p_vaddr, core_php->p_filesz) == NULL) goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
      core_php++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
   free(phbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
err:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
   free(phbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
// read segments of a shared object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
   int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
   ELF_PHDR* phbuf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
   ELF_PHDR* lib_php = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
   if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
   // we want to process only PT_LOAD segments that are not writable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
   // i.e., text segments. The read/write/exec (data) segments would
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
   // have been already added from core file segments.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
   for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
      if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
         if (add_map_info(ph, lib_fd, lib_php->p_offset, lib_php->p_vaddr + lib_base, lib_php->p_filesz) == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
            goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
      lib_php++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
   free(phbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
err:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
   free(phbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
// process segments from interpreter (ld.so or ld-linux.so)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
static bool read_interp_segments(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
   ELF_EHDR interp_ehdr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
   if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
       print_debug("interpreter is not a valid ELF file\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
       return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
   if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
       print_debug("can't read segments of interpreter\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
       return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
// process segments of a a.out
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
   int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
   ELF_PHDR* phbuf = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
   ELF_PHDR* exec_php = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
   if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
   for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
      switch (exec_php->p_type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
         // add mappings for PT_LOAD segments
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
         case PT_LOAD: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
            // add only non-writable segments of non-zero filesz
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
            if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
               if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
         // read the interpreter and it's segments
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
         case PT_INTERP: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
            char interp_name[BUF_SIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
            pread(ph->core->exec_fd, interp_name, MIN(exec_php->p_filesz, BUF_SIZE), exec_php->p_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
            print_debug("ELF interpreter %s\n", interp_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
            // read interpreter segments as well
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
            if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
               print_debug("can't open runtime loader\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
               goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
         // from PT_DYNAMIC we want to read address of first link_map addr
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
         case PT_DYNAMIC: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
            ph->core->dynamic_addr = exec_php->p_vaddr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
            print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
      } // switch
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
      exec_php++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
   } // for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
   free(phbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
err:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
   free(phbuf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
   return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
#define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug,  r_map)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
#define LD_BASE_OFFSET        offsetof(struct r_debug,  r_ldbase)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
#define LINK_MAP_ADDR_OFFSET  offsetof(struct link_map, l_addr)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
#define LINK_MAP_NAME_OFFSET  offsetof(struct link_map, l_name)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
#define LINK_MAP_NEXT_OFFSET  offsetof(struct link_map, l_next)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
// read shared library info from runtime linker's data structures.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
// This work is done by librtlb_db in Solaris
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
static bool read_shared_lib_info(struct ps_prochandle* ph) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
   uintptr_t addr = ph->core->dynamic_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
   uintptr_t debug_base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
   uintptr_t first_link_map_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
   uintptr_t ld_base_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
   uintptr_t link_map_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
   uintptr_t lib_base_diff;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
   uintptr_t lib_base;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
   uintptr_t lib_name_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
   char lib_name[BUF_SIZE];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
   ELF_DYN dyn;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
   ELF_EHDR elf_ehdr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
   int lib_fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
   // _DYNAMIC has information of the form
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
   //         [tag] [data] [tag] [data] .....
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
   // Both tag and data are pointer sized.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
   // We look for dynamic info with DT_DEBUG. This has shared object info.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
   // refer to struct r_debug in link.h
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
   dyn.d_tag = DT_NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
   while (dyn.d_tag != DT_DEBUG) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
      if (ps_pdread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
         print_debug("can't read debug info from _DYNAMIC\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
         return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
      addr += sizeof(ELF_DYN);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
   // we have got Dyn entry with DT_DEBUG
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
   debug_base = dyn.d_un.d_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
   // at debug_base we have struct r_debug. This has first link map in r_map field
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
   if (ps_pdread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
                 &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
      print_debug("can't read first link map address\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
   // read ld_base address from struct r_debug
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
   if (ps_pdread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
                 sizeof(uintptr_t)) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
      print_debug("can't read ld base address\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
   ph->core->ld_base_addr = ld_base_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
   print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
   // now read segments from interp (i.e ld.so or ld-linux.so)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
   if (read_interp_segments(ph) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
   // after adding interpreter (ld.so) mappings sort again
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
   if (sort_map_array(ph) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
   print_debug("first link map is at 0x%lx\n", first_link_map_addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
   link_map_addr = first_link_map_addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
   while (link_map_addr != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
      // read library base address of the .so. Note that even though <sys/link.h> calls
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
      // link_map->l_addr as "base address",  this is * not * really base virtual
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
      // address of the shared object. This is actually the difference b/w the virtual
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
      // address mentioned in shared object and the actual virtual base where runtime
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
      // linker loaded it. We use "base diff" in read_lib_segments call below.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
      if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
                   &lib_base_diff, sizeof(uintptr_t)) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
         print_debug("can't read shared object base address diff\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
         return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
      // read address of the name
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
      if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
                    &lib_name_addr, sizeof(uintptr_t)) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
         print_debug("can't read address of shared object name\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
         return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
      // read name of the shared object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
      if (read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
         print_debug("can't read shared object name\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
         return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
      if (lib_name[0] != '\0') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
         // ignore empty lib names
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
         lib_fd = pathmap_open(lib_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
         if (lib_fd < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
            print_debug("can't open shared object %s\n", lib_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
            // continue with other libraries...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
            if (read_elf_header(lib_fd, &elf_ehdr)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
               lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
               print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
                           lib_name, lib_base, lib_base_diff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
               // while adding library mappings we need to use "base difference".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
               if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
                  print_debug("can't read shared object's segments\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
                  close(lib_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
                  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
               }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
               add_lib_info_fd(ph, lib_name, lib_fd, lib_base);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
               // Map info is added for the library (lib_name) so
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
               // we need to re-sort it before calling the p_pdread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
               if (sort_map_array(ph) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
                  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
               print_debug("can't read ELF header for shared object %s\n", lib_name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
               close(lib_fd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
               // continue with other libraries...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
      // read next link_map address
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
      if (ps_pdread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
                        &link_map_addr, sizeof(uintptr_t)) != PS_OK) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
         print_debug("can't read next link in link_map\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
         return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
   return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
// the one and only one exposed stuff from this file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
   ELF_EHDR core_ehdr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
   ELF_EHDR exec_ehdr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
   ELF_EHDR lib_ehdr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
   struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
   if (ph == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
      print_debug("can't allocate ps_prochandle\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
   if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
      free(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
      print_debug("can't allocate ps_prochandle\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
      return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
   // initialize ph
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
   ph->ops = &core_ops;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
   ph->core->core_fd   = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
   ph->core->exec_fd   = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
   ph->core->interp_fd = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
   // open the core file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
   if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
      print_debug("can't open core file\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
   // read core file ELF header
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
   if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
      print_debug("core file is not a valid ELF ET_CORE file\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
   if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
      print_debug("can't open executable file\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
   if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
      print_debug("executable file is not a valid ELF ET_EXEC file\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
   // process core file segments
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
   if (read_core_segments(ph, &core_ehdr) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
   // process exec file segments
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
   if (read_exec_segments(ph, &exec_ehdr) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
   // exec file is also treated like a shared object for symbol search
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
   if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
                       (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
   // allocate and sort maps into map_array, we need to do this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
   // here because read_shared_lib_info needs to read from debuggee
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
   // address space
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
   if (sort_map_array(ph) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
   if (read_shared_lib_info(ph) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
   // sort again because we have added more mappings from shared objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
   if (sort_map_array(ph) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
   if (init_classsharing_workaround(ph) != true)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
      goto err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
   return ph;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
err:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
   Prelease(ph);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
   return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
}