agent/src/os/linux/ps_core.c

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 5961
1bee3014cf2a
parent 0
f90c822e73f8
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

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

mercurial