agent/src/os/linux/ps_core.c

Mon, 28 Jul 2014 15:06:38 -0700

author
fzhinkin
date
Mon, 28 Jul 2014 15:06:38 -0700
changeset 6997
dbb05f6d93c4
parent 5961
1bee3014cf2a
child 6876
710a3c8b516e
child 8719
f89cf87d867d
permissions
-rw-r--r--

8051344: JVM crashed in Compile::start() during method parsing w/ UseRTMDeopt turned on
Summary: call rtm_deopt() only if there were no compilation bailouts before.
Reviewed-by: kvn

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

mercurial