agent/src/os/linux/ps_core.c

Tue, 20 Apr 2010 13:26:33 -0700

author
never
date
Tue, 20 Apr 2010 13:26:33 -0700
changeset 1820
bc32f286fae0
parent 1014
0fbdb4381b99
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6945219: minor SA fixes
Reviewed-by: twisti

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

mercurial