agent/src/os/linux/ps_core.c

Wed, 02 Jul 2008 12:55:16 -0700

author
xdono
date
Wed, 02 Jul 2008 12:55:16 -0700
changeset 631
d1605aabd0a1
parent 485
485d403e94e1
child 964
8db2b3e46c38
permissions
-rw-r--r--

6719955: Update copyright year
Summary: Update copyright year for files that have been modified in 2008
Reviewed-by: ohair, tbell

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

mercurial