agent/src/os/linux/ps_core.c

Sat, 01 Sep 2012 13:25:18 -0400

author
coleenp
date
Sat, 01 Sep 2012 13:25:18 -0400
changeset 4037
da91efe96a93
parent 3722
29ee40a082d3
child 4709
255c0a4cb4eb
permissions
-rw-r--r--

6964458: Reimplement class meta-data storage to use native memory
Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>

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

mercurial