agent/src/os/linux/ps_core.c

Sun, 22 Sep 2013 18:49:09 +0400

author
dsamersoff
date
Sun, 22 Sep 2013 18:49:09 +0400
changeset 5758
1f42d3ec1759
parent 5061
f6a055fcf47d
child 5832
5705c7ee6dd7
permissions
-rw-r--r--

7133122: SA throws sun.jvm.hotspot.debugger.UnmappedAddressException when it should not
Summary: replace PT_LOAD segment with library segment when necessary
Reviewed-by: dholmes, sla

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

mercurial