1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include <jni.h>
  26 #include <unistd.h>
  27 #include <fcntl.h>
  28 #include <string.h>
  29 #include <stdlib.h>
  30 #include <stddef.h>
  31 #include "libproc_impl.h"
  32 
  33 #ifdef __APPLE__
  34 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
  35 #endif
  36 
  37 // This file has the libproc implementation to read core files.
  38 // For live processes, refer to ps_proc.c. Portions of this is adapted
  39 // /modelled after Solaris libproc.so (in particular Pcore.c)
  40 
  41 //----------------------------------------------------------------------
  42 // ps_prochandle cleanup helper functions
  43 
  44 // close all file descriptors
  45 static void close_files(struct ps_prochandle* ph) {
  46   lib_info* lib = NULL;
  47 
  48   // close core file descriptor
  49   if (ph->core->core_fd >= 0)
  50     close(ph->core->core_fd);
  51 
  52   // close exec file descriptor
  53   if (ph->core->exec_fd >= 0)
  54     close(ph->core->exec_fd);
  55 
  56   // close interp file descriptor
  57   if (ph->core->interp_fd >= 0)
  58     close(ph->core->interp_fd);
  59 
  60   // close class share archive file
  61   if (ph->core->classes_jsa_fd >= 0)
  62     close(ph->core->classes_jsa_fd);
  63 
  64   // close all library file descriptors
  65   lib = ph->libs;
  66   while (lib) {
  67     int fd = lib->fd;
  68     if (fd >= 0 && fd != ph->core->exec_fd) {
  69       close(fd);
  70     }
  71     lib = lib->next;
  72   }
  73 }
  74 
  75 // clean all map_info stuff
  76 static void destroy_map_info(struct ps_prochandle* ph) {
  77   map_info* map = ph->core->maps;
  78   while (map) {
  79     map_info* next = map->next;
  80     free(map);
  81     map = next;
  82   }
  83 
  84   if (ph->core->map_array) {
  85     free(ph->core->map_array);
  86   }
  87 
  88   // Part of the class sharing workaround
  89   map = ph->core->class_share_maps;
  90   while (map) {
  91     map_info* next = map->next;
  92     free(map);
  93     map = next;
  94   }
  95 }
  96 
  97 // ps_prochandle operations
  98 static void core_release(struct ps_prochandle* ph) {
  99   if (ph->core) {
 100     close_files(ph);
 101     destroy_map_info(ph);
 102     free(ph->core);
 103   }
 104 }
 105 
 106 static map_info* allocate_init_map(int fd, off_t offset, uintptr_t vaddr, size_t memsz) {
 107   map_info* map;
 108   if ( (map = (map_info*) calloc(1, sizeof(map_info))) == NULL) {
 109     print_debug("can't allocate memory for map_info\n");
 110     return NULL;
 111   }
 112 
 113   // initialize map
 114   map->fd     = fd;
 115   map->offset = offset;
 116   map->vaddr  = vaddr;
 117   map->memsz  = memsz;
 118   return map;
 119 }
 120 
 121 // add map info with given fd, offset, vaddr and memsz
 122 static map_info* add_map_info(struct ps_prochandle* ph, int fd, off_t offset,
 123                              uintptr_t vaddr, size_t memsz) {
 124   map_info* map;
 125   if ((map = allocate_init_map(fd, offset, vaddr, memsz)) == NULL) {
 126     return NULL;
 127   }
 128 
 129   // add this to map list
 130   map->next  = ph->core->maps;
 131   ph->core->maps   = map;
 132   ph->core->num_maps++;
 133 
 134   return map;
 135 }
 136 
 137 // Part of the class sharing workaround
 138 static map_info* add_class_share_map_info(struct ps_prochandle* ph, off_t offset,
 139                              uintptr_t vaddr, size_t memsz) {
 140   map_info* map;
 141   if ((map = allocate_init_map(ph->core->classes_jsa_fd,
 142                                offset, vaddr, memsz)) == NULL) {
 143     return NULL;
 144   }
 145 
 146   map->next = ph->core->class_share_maps;
 147   ph->core->class_share_maps = map;
 148   return map;
 149 }
 150 
 151 // Return the map_info for the given virtual address.  We keep a sorted
 152 // array of pointers in ph->map_array, so we can binary search.
 153 static map_info* core_lookup(struct ps_prochandle *ph, uintptr_t addr) {
 154   int mid, lo = 0, hi = ph->core->num_maps - 1;
 155   map_info *mp;
 156 
 157   while (hi - lo > 1) {
 158     mid = (lo + hi) / 2;
 159     if (addr >= ph->core->map_array[mid]->vaddr) {
 160       lo = mid;
 161     } else {
 162       hi = mid;
 163     }
 164   }
 165 
 166   if (addr < ph->core->map_array[hi]->vaddr) {
 167     mp = ph->core->map_array[lo];
 168   } else {
 169     mp = ph->core->map_array[hi];
 170   }
 171 
 172   if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
 173     return (mp);
 174   }
 175 
 176 
 177   // Part of the class sharing workaround
 178   // Unfortunately, we have no way of detecting -Xshare state.
 179   // Check out the share maps atlast, if we don't find anywhere.
 180   // This is done this way so to avoid reading share pages
 181   // ahead of other normal maps. For eg. with -Xshare:off we don't
 182   // want to prefer class sharing data to data from core.
 183   mp = ph->core->class_share_maps;
 184   if (mp) {
 185     print_debug("can't locate map_info at 0x%lx, trying class share maps\n", addr);
 186   }
 187   while (mp) {
 188     if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
 189       print_debug("located map_info at 0x%lx from class share maps\n", addr);
 190       return (mp);
 191     }
 192     mp = mp->next;
 193   }
 194 
 195   print_debug("can't locate map_info at 0x%lx\n", addr);
 196   return (NULL);
 197 }
 198 
 199 //---------------------------------------------------------------
 200 // Part of the class sharing workaround:
 201 //
 202 // With class sharing, pages are mapped from classes.jsa file.
 203 // The read-only class sharing pages are mapped as MAP_SHARED,
 204 // PROT_READ pages. These pages are not dumped into core dump.
 205 // With this workaround, these pages are read from classes.jsa.
 206 
 207 // FIXME: !HACK ALERT!
 208 // The format of sharing achive file header is needed to read shared heap
 209 // file mappings. For now, I am hard coding portion of FileMapHeader here.
 210 // Refer to filemap.hpp.
 211 
 212 // FileMapHeader describes the shared space data in the file to be
 213 // mapped.  This structure gets written to a file.  It is not a class,
 214 // so that the compilers don't add any compiler-private data to it.
 215 
 216 #define NUM_SHARED_MAPS 9
 217 
 218 // Refer to FileMapInfo::_current_version in filemap.hpp
 219 #define CURRENT_ARCHIVE_VERSION 3
 220 
 221 typedef unsigned char* address;
 222 typedef uintptr_t      uintx;
 223 typedef intptr_t       intx;
 224 
 225 
 226 struct FileMapHeader {
 227   int     _magic;                   // identify file type.
 228   int     _crc;                     // header crc checksum.
 229   int     _version;                 // (from enum, above.)
 230   size_t  _alignment;               // how shared archive should be aligned
 231   int     _obj_alignment;           // value of ObjectAlignmentInBytes
 232   address _narrow_oop_base;         // compressed oop encoding base
 233   int     _narrow_oop_shift;        // compressed oop encoding shift
 234   bool    _compact_strings;         // value of CompactStrings
 235   uintx   _max_heap_size;           // java max heap size during dumping
 236   int     _narrow_oop_mode;         // compressed oop encoding mode
 237   int     _narrow_klass_shift;      // save narrow klass base and shift
 238   address _narrow_klass_base;
 239   char*   _misc_data_patching_start;
 240   char*   _read_only_tables_start;
 241   address _cds_i2i_entry_code_buffers;
 242   size_t  _cds_i2i_entry_code_buffers_size;
 243   size_t  _core_spaces_size;        // number of bytes allocated by the core spaces
 244                                     // (mc, md, ro, rw and od).
 245 
 246 
 247   struct space_info {
 248     int     _crc;          // crc checksum of the current space
 249     size_t  _file_offset;  // sizeof(this) rounded to vm page size
 250     union {
 251       char*  _base;        // copy-on-write base address
 252       intx   _offset;      // offset from the compressed oop encoding base, only used
 253                            // by archive heap space
 254     } _addr;
 255     size_t _used;          // for setting space top on read
 256     // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with
 257     // the C type matching the C++ bool type on any given platform.
 258     // We assume the corresponding C type is char but licensees
 259     // may need to adjust the type of these fields.
 260     char   _read_only;     // read only space?
 261     char   _allow_exec;    // executable code in space?
 262   } _space[NUM_SHARED_MAPS];
 263 
 264   // Ignore the rest of the FileMapHeader. We don't need those fields here.
 265 };
 266 
 267 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) {
 268   jboolean i;
 269   if (ps_pread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) {
 270     *pvalue = i;
 271     return true;
 272   } else {
 273     return false;
 274   }
 275 }
 276 
 277 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
 278   uintptr_t uip;
 279   if (ps_pread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) {
 280     *pvalue = uip;
 281     return true;
 282   } else {
 283     return false;
 284   }
 285 }
 286 
 287 // used to read strings from debuggee
 288 static bool read_string(struct ps_prochandle* ph, uintptr_t addr, char* buf, size_t size) {
 289   size_t i = 0;
 290   char  c = ' ';
 291 
 292   while (c != '\0') {
 293     if (ps_pread(ph, (psaddr_t) addr, &c, sizeof(char)) != PS_OK) {
 294       return false;
 295     }
 296     if (i < size - 1) {
 297       buf[i] = c;
 298     } else {
 299       // smaller buffer
 300       return false;
 301     }
 302     i++; addr++;
 303   }
 304   buf[i] = '\0';
 305   return true;
 306 }
 307 
 308 #ifdef __APPLE__
 309 #define USE_SHARED_SPACES_SYM "_UseSharedSpaces"
 310 // mangled name of Arguments::SharedArchivePath
 311 #define SHARED_ARCHIVE_PATH_SYM "_ZN9Arguments17SharedArchivePathE"
 312 #define LIBJVM_NAME "/libjvm.dylib"
 313 #else
 314 #define USE_SHARED_SPACES_SYM "UseSharedSpaces"
 315 // mangled name of Arguments::SharedArchivePath
 316 #define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE"
 317 #define LIBJVM_NAME "/libjvm.so"
 318 #endif // __APPLE_
 319 
 320 static bool init_classsharing_workaround(struct ps_prochandle* ph) {
 321   int m;
 322   size_t n;
 323   lib_info* lib = ph->libs;
 324   while (lib != NULL) {
 325     // we are iterating over shared objects from the core dump. look for
 326     // libjvm.so.
 327     const char *jvm_name = 0;
 328     if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) {
 329       char classes_jsa[PATH_MAX];
 330       struct FileMapHeader header;
 331       int fd = -1;
 332       uintptr_t base = 0, useSharedSpacesAddr = 0;
 333       uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
 334       jboolean useSharedSpaces = 0;
 335 
 336       memset(classes_jsa, 0, sizeof(classes_jsa));
 337       jvm_name = lib->name;
 338       useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM);
 339       if (useSharedSpacesAddr == 0) {
 340         print_debug("can't lookup 'UseSharedSpaces' flag\n");
 341         return false;
 342       }
 343 
 344       // Hotspot vm types are not exported to build this library. So
 345       // using equivalent type jboolean to read the value of
 346       // UseSharedSpaces which is same as hotspot type "bool".
 347       if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
 348         print_debug("can't read the value of 'UseSharedSpaces' flag\n");
 349         return false;
 350       }
 351 
 352       if ((int)useSharedSpaces == 0) {
 353         print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
 354         return true;
 355       }
 356 
 357       sharedArchivePathAddrAddr = lookup_symbol(ph, jvm_name, SHARED_ARCHIVE_PATH_SYM);
 358       if (sharedArchivePathAddrAddr == 0) {
 359         print_debug("can't lookup shared archive path symbol\n");
 360         return false;
 361       }
 362 
 363       if (read_pointer(ph, sharedArchivePathAddrAddr, &sharedArchivePathAddr) != true) {
 364         print_debug("can't read shared archive path pointer\n");
 365         return false;
 366       }
 367 
 368       if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) {
 369         print_debug("can't read shared archive path value\n");
 370         return false;
 371       }
 372 
 373       print_debug("looking for %s\n", classes_jsa);
 374       // open the class sharing archive file
 375       fd = pathmap_open(classes_jsa);
 376       if (fd < 0) {
 377         print_debug("can't open %s!\n", classes_jsa);
 378         ph->core->classes_jsa_fd = -1;
 379         return false;
 380       } else {
 381         print_debug("opened %s\n", classes_jsa);
 382       }
 383 
 384       // read FileMapHeader from the file
 385       memset(&header, 0, sizeof(struct FileMapHeader));
 386       if ((n = read(fd, &header, sizeof(struct FileMapHeader)))
 387            != sizeof(struct FileMapHeader)) {
 388         print_debug("can't read shared archive file map header from %s\n", classes_jsa);
 389         close(fd);
 390         return false;
 391       }
 392 
 393       // check file magic
 394       if (header._magic != 0xf00baba2) {
 395         print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n",
 396                      classes_jsa, header._magic);
 397         close(fd);
 398         return false;
 399       }
 400 
 401       // check version
 402       if (header._version != CURRENT_ARCHIVE_VERSION) {
 403         print_debug("%s has wrong shared archive file version %d, expecting %d\n",
 404                      classes_jsa, header._version, CURRENT_ARCHIVE_VERSION);
 405         close(fd);
 406         return false;
 407       }
 408 
 409       ph->core->classes_jsa_fd = fd;
 410       // add read-only maps from classes.jsa to the list of maps
 411       for (m = 0; m < NUM_SHARED_MAPS; m++) {
 412         if (header._space[m]._read_only) {
 413           base = (uintptr_t) header._space[m]._addr._base;
 414           // no need to worry about the fractional pages at-the-end.
 415           // possible fractional pages are handled by core_read_data.
 416           add_class_share_map_info(ph, (off_t) header._space[m]._file_offset,
 417                                    base, (size_t) header._space[m]._used);
 418           print_debug("added a share archive map at 0x%lx\n", base);
 419         }
 420       }
 421       return true;
 422    }
 423    lib = lib->next;
 424   }
 425   return true;
 426 }
 427 
 428 //---------------------------------------------------------------------------
 429 // functions to handle map_info
 430 
 431 // Order mappings based on virtual address.  We use this function as the
 432 // callback for sorting the array of map_info pointers.
 433 static int core_cmp_mapping(const void *lhsp, const void *rhsp)
 434 {
 435   const map_info *lhs = *((const map_info **)lhsp);
 436   const map_info *rhs = *((const map_info **)rhsp);
 437 
 438   if (lhs->vaddr == rhs->vaddr) {
 439     return (0);
 440   }
 441 
 442   return (lhs->vaddr < rhs->vaddr ? -1 : 1);
 443 }
 444 
 445 // we sort map_info by starting virtual address so that we can do
 446 // binary search to read from an address.
 447 static bool sort_map_array(struct ps_prochandle* ph) {
 448   size_t num_maps = ph->core->num_maps;
 449   map_info* map = ph->core->maps;
 450   int i = 0;
 451 
 452   // allocate map_array
 453   map_info** array;
 454   if ( (array = (map_info**) malloc(sizeof(map_info*) * num_maps)) == NULL) {
 455     print_debug("can't allocate memory for map array\n");
 456     return false;
 457   }
 458 
 459   // add maps to array
 460   while (map) {
 461     array[i] = map;
 462     i++;
 463     map = map->next;
 464   }
 465 
 466   // sort is called twice. If this is second time, clear map array
 467   if (ph->core->map_array) {
 468     free(ph->core->map_array);
 469   }
 470   ph->core->map_array = array;
 471   // sort the map_info array by base virtual address.
 472   qsort(ph->core->map_array, ph->core->num_maps, sizeof (map_info*),
 473         core_cmp_mapping);
 474 
 475   // print map
 476   if (is_debug()) {
 477     int j = 0;
 478     print_debug("---- sorted virtual address map ----\n");
 479     for (j = 0; j < ph->core->num_maps; j++) {
 480       print_debug("base = 0x%lx\tsize = %d\n", ph->core->map_array[j]->vaddr,
 481                   ph->core->map_array[j]->memsz);
 482     }
 483   }
 484 
 485   return true;
 486 }
 487 
 488 #ifndef MIN
 489 #define MIN(x, y) (((x) < (y))? (x): (y))
 490 #endif
 491 
 492 static bool core_read_data(struct ps_prochandle* ph, uintptr_t addr, char *buf, size_t size) {
 493    ssize_t resid = size;
 494    int page_size=sysconf(_SC_PAGE_SIZE);
 495    while (resid != 0) {
 496       map_info *mp = core_lookup(ph, addr);
 497       uintptr_t mapoff;
 498       ssize_t len, rem;
 499       off_t off;
 500       int fd;
 501 
 502       if (mp == NULL) {
 503          break;  /* No mapping for this address */
 504       }
 505 
 506       fd = mp->fd;
 507       mapoff = addr - mp->vaddr;
 508       len = MIN(resid, mp->memsz - mapoff);
 509       off = mp->offset + mapoff;
 510 
 511       if ((len = pread(fd, buf, len, off)) <= 0) {
 512          break;
 513       }
 514 
 515       resid -= len;
 516       addr += len;
 517       buf = (char *)buf + len;
 518 
 519       // mappings always start at page boundary. But, may end in fractional
 520       // page. fill zeros for possible fractional page at the end of a mapping.
 521       rem = mp->memsz % page_size;
 522       if (rem > 0) {
 523          rem = page_size - rem;
 524          len = MIN(resid, rem);
 525          resid -= len;
 526          addr += len;
 527          // we are not assuming 'buf' to be zero initialized.
 528          memset(buf, 0, len);
 529          buf += len;
 530       }
 531    }
 532 
 533    if (resid) {
 534       print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n",
 535               size, addr, resid);
 536       return false;
 537    } else {
 538       return true;
 539    }
 540 }
 541 
 542 // null implementation for write
 543 static bool core_write_data(struct ps_prochandle* ph,
 544                              uintptr_t addr, const char *buf , size_t size) {
 545    return false;
 546 }
 547 
 548 static bool core_get_lwp_regs(struct ps_prochandle* ph, lwpid_t lwp_id,
 549                           struct reg* regs) {
 550    // for core we have cached the lwp regs after segment parsed
 551    sa_thread_info* thr = ph->threads;
 552    while (thr) {
 553      if (thr->lwp_id == lwp_id) {
 554        memcpy(regs, &thr->regs, sizeof(struct reg));
 555        return true;
 556      }
 557      thr = thr->next;
 558    }
 559    return false;
 560 }
 561 
 562 static bool core_get_lwp_info(struct ps_prochandle *ph, lwpid_t id, void *info) {
 563    print_debug("core_get_lwp_info not implemented\n");
 564    return false;
 565 }
 566 
 567 static ps_prochandle_ops core_ops = {
 568    .release=  core_release,
 569    .p_pread=  core_read_data,
 570    .p_pwrite= core_write_data,
 571    .get_lwp_regs= core_get_lwp_regs,
 572    .get_lwp_info= core_get_lwp_info
 573 };
 574 
 575 // from this point, mainly two blocks divided by def __APPLE__
 576 // one for Macosx, the other for regular Bsd
 577 
 578 #ifdef __APPLE__
 579 
 580 void print_thread(sa_thread_info *threadinfo) {
 581   print_debug("thread added: %d\n", threadinfo->lwp_id);
 582   print_debug("registers:\n");
 583   print_debug("  r_r15: 0x%" PRIx64 "\n", threadinfo->regs.r_r15);
 584   print_debug("  r_r14: 0x%" PRIx64 "\n", threadinfo->regs.r_r14);
 585   print_debug("  r_r13: 0x%" PRIx64 "\n", threadinfo->regs.r_r13);
 586   print_debug("  r_r12: 0x%" PRIx64 "\n", threadinfo->regs.r_r12);
 587   print_debug("  r_r11: 0x%" PRIx64 "\n", threadinfo->regs.r_r11);
 588   print_debug("  r_r10: 0x%" PRIx64 "\n", threadinfo->regs.r_r10);
 589   print_debug("  r_r9:  0x%" PRIx64 "\n", threadinfo->regs.r_r9);
 590   print_debug("  r_r8:  0x%" PRIx64 "\n", threadinfo->regs.r_r8);
 591   print_debug("  r_rdi: 0x%" PRIx64 "\n", threadinfo->regs.r_rdi);
 592   print_debug("  r_rsi: 0x%" PRIx64 "\n", threadinfo->regs.r_rsi);
 593   print_debug("  r_rbp: 0x%" PRIx64 "\n", threadinfo->regs.r_rbp);
 594   print_debug("  r_rbx: 0x%" PRIx64 "\n", threadinfo->regs.r_rbx);
 595   print_debug("  r_rdx: 0x%" PRIx64 "\n", threadinfo->regs.r_rdx);
 596   print_debug("  r_rcx: 0x%" PRIx64 "\n", threadinfo->regs.r_rcx);
 597   print_debug("  r_rax: 0x%" PRIx64 "\n", threadinfo->regs.r_rax);
 598   print_debug("  r_fs:  0x%" PRIx32 "\n", threadinfo->regs.r_fs);
 599   print_debug("  r_gs:  0x%" PRIx32 "\n", threadinfo->regs.r_gs);
 600   print_debug("  r_rip  0x%" PRIx64 "\n", threadinfo->regs.r_rip);
 601   print_debug("  r_cs:  0x%" PRIx64 "\n", threadinfo->regs.r_cs);
 602   print_debug("  r_rsp: 0x%" PRIx64 "\n", threadinfo->regs.r_rsp);
 603   print_debug("  r_rflags: 0x%" PRIx64 "\n", threadinfo->regs.r_rflags);
 604 }
 605 
 606 // read all segments64 commands from core file
 607 // read all thread commands from core file
 608 static bool read_core_segments(struct ps_prochandle* ph) {
 609   int i = 0;
 610   int num_threads = 0;
 611   int fd = ph->core->core_fd;
 612   off_t offset = 0;
 613   mach_header_64      fhead;
 614   load_command        lcmd;
 615   segment_command_64  segcmd;
 616   // thread_command      thrcmd;
 617 
 618   lseek(fd, offset, SEEK_SET);
 619   if(read(fd, (void *)&fhead, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
 620      goto err;
 621   }
 622   print_debug("total commands: %d\n", fhead.ncmds);
 623   offset += sizeof(mach_header_64);
 624   for (i = 0; i < fhead.ncmds; i++) {
 625     lseek(fd, offset, SEEK_SET);
 626     if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
 627       goto err;
 628     }
 629     offset += lcmd.cmdsize;    // next command position
 630     if (lcmd.cmd == LC_SEGMENT_64) {
 631       lseek(fd, -sizeof(load_command), SEEK_CUR);
 632       if (read(fd, (void *)&segcmd, sizeof(segment_command_64)) != sizeof(segment_command_64)) {
 633         print_debug("failed to read LC_SEGMENT_64 i = %d!\n", i);
 634         goto err;
 635       }
 636       if (add_map_info(ph, fd, segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize) == NULL) {
 637         print_debug("Failed to add map_info at i = %d\n", i);
 638         goto err;
 639       }
 640       print_debug("segment added: %" PRIu64 " 0x%" PRIx64 " %d\n",
 641                    segcmd.fileoff, segcmd.vmaddr, segcmd.vmsize);
 642     } else if (lcmd.cmd == LC_THREAD || lcmd.cmd == LC_UNIXTHREAD) {
 643       typedef struct thread_fc {
 644         uint32_t  flavor;
 645         uint32_t  count;
 646       } thread_fc;
 647       thread_fc fc;
 648       uint32_t size = sizeof(load_command);
 649       while (size < lcmd.cmdsize) {
 650         if (read(fd, (void *)&fc, sizeof(thread_fc)) != sizeof(thread_fc)) {
 651           printf("Reading flavor, count failed.\n");
 652           goto err;
 653         }
 654         size += sizeof(thread_fc);
 655         if (fc.flavor == x86_THREAD_STATE) {
 656           x86_thread_state_t thrstate;
 657           if (read(fd, (void *)&thrstate, sizeof(x86_thread_state_t)) != sizeof(x86_thread_state_t)) {
 658             printf("Reading flavor, count failed.\n");
 659             goto err;
 660           }
 661           size += sizeof(x86_thread_state_t);
 662           // create thread info list, update lwp_id later
 663           sa_thread_info* newthr = add_thread_info(ph, (pthread_t) -1, (lwpid_t) num_threads++);
 664           if (newthr == NULL) {
 665             printf("create thread_info failed\n");
 666             goto err;
 667           }
 668 
 669           // note __DARWIN_UNIX03 depengs on other definitions
 670 #if __DARWIN_UNIX03
 671 #define get_register_v(regst, regname) \
 672   regst.uts.ts64.__##regname
 673 #else
 674 #define get_register_v(regst, regname) \
 675   regst.uts.ts64.##regname
 676 #endif // __DARWIN_UNIX03
 677           newthr->regs.r_rax = get_register_v(thrstate, rax);
 678           newthr->regs.r_rbx = get_register_v(thrstate, rbx);
 679           newthr->regs.r_rcx = get_register_v(thrstate, rcx);
 680           newthr->regs.r_rdx = get_register_v(thrstate, rdx);
 681           newthr->regs.r_rdi = get_register_v(thrstate, rdi);
 682           newthr->regs.r_rsi = get_register_v(thrstate, rsi);
 683           newthr->regs.r_rbp = get_register_v(thrstate, rbp);
 684           newthr->regs.r_rsp = get_register_v(thrstate, rsp);
 685           newthr->regs.r_r8  = get_register_v(thrstate, r8);
 686           newthr->regs.r_r9  = get_register_v(thrstate, r9);
 687           newthr->regs.r_r10 = get_register_v(thrstate, r10);
 688           newthr->regs.r_r11 = get_register_v(thrstate, r11);
 689           newthr->regs.r_r12 = get_register_v(thrstate, r12);
 690           newthr->regs.r_r13 = get_register_v(thrstate, r13);
 691           newthr->regs.r_r14 = get_register_v(thrstate, r14);
 692           newthr->regs.r_r15 = get_register_v(thrstate, r15);
 693           newthr->regs.r_rip = get_register_v(thrstate, rip);
 694           newthr->regs.r_rflags = get_register_v(thrstate, rflags);
 695           newthr->regs.r_cs  = get_register_v(thrstate, cs);
 696           newthr->regs.r_fs  = get_register_v(thrstate, fs);
 697           newthr->regs.r_gs  = get_register_v(thrstate, gs);
 698           print_thread(newthr);
 699         } else if (fc.flavor == x86_FLOAT_STATE) {
 700           x86_float_state_t flstate;
 701           if (read(fd, (void *)&flstate, sizeof(x86_float_state_t)) != sizeof(x86_float_state_t)) {
 702             print_debug("Reading flavor, count failed.\n");
 703             goto err;
 704           }
 705           size += sizeof(x86_float_state_t);
 706         } else if (fc.flavor == x86_EXCEPTION_STATE) {
 707           x86_exception_state_t excpstate;
 708           if (read(fd, (void *)&excpstate, sizeof(x86_exception_state_t)) != sizeof(x86_exception_state_t)) {
 709             printf("Reading flavor, count failed.\n");
 710             goto err;
 711           }
 712           size += sizeof(x86_exception_state_t);
 713         }
 714       }
 715     }
 716   }
 717   return true;
 718 err:
 719   return false;
 720 }
 721 
 722 /**local function **/
 723 bool exists(const char *fname) {
 724   return access(fname, F_OK) == 0;
 725 }
 726 
 727 // we check: 1. lib
 728 //           2. lib/server
 729 //           3. jre/lib
 730 //           4. jre/lib/server
 731 // from: 1. exe path
 732 //       2. JAVA_HOME
 733 //       3. DYLD_LIBRARY_PATH
 734 static bool get_real_path(struct ps_prochandle* ph, char *rpath) {
 735   /** check if they exist in JAVA ***/
 736   char* execname = ph->core->exec_path;
 737   char  filepath[4096];
 738   char* filename = strrchr(rpath, '/');               // like /libjvm.dylib
 739   if (filename == NULL) {
 740     return false;
 741   }
 742 
 743   char* posbin = strstr(execname, "/bin/java");
 744   if (posbin != NULL) {
 745     memcpy(filepath, execname, posbin - execname);    // not include trailing '/'
 746     filepath[posbin - execname] = '\0';
 747   } else {
 748     char* java_home = getenv("JAVA_HOME");
 749     if (java_home != NULL) {
 750       strcpy(filepath, java_home);
 751     } else {
 752       char* dyldpath = getenv("DYLD_LIBRARY_PATH");
 753       char* dypath = strtok(dyldpath, ":");
 754       while (dypath != NULL) {
 755         strcpy(filepath, dypath);
 756         strcat(filepath, filename);
 757         if (exists(filepath)) {
 758            strcpy(rpath, filepath);
 759            return true;
 760         }
 761         dypath = strtok(dyldpath, ":");
 762       }
 763       // not found
 764       return false;
 765     }
 766   }
 767   // for exec and java_home, jdkpath now is filepath
 768   size_t filepath_base_size = strlen(filepath);
 769 
 770   // first try /lib/ and /lib/server
 771   strcat(filepath, "/lib");
 772   strcat(filepath, filename);
 773   if (exists(filepath)) {
 774     strcpy(rpath, filepath);
 775     return true;
 776   }
 777   char* pos = strstr(filepath, filename);    // like /libjvm.dylib
 778   *pos = '\0';
 779   strcat(filepath, "/server");
 780   strcat(filepath, filename);
 781   if (exists(filepath)) {
 782     strcpy(rpath, filepath);
 783     return true;
 784   }
 785 
 786   // then try /jre/lib/ and /jre/lib/server
 787   filepath[filepath_base_size] = '\0';
 788   strcat(filepath, "/jre/lib");
 789   strcat(filepath, filename);
 790   if (exists(filepath)) {
 791     strcpy(rpath, filepath);
 792     return true;
 793   }
 794   pos = strstr(filepath, filename);
 795   *pos = '\0';
 796   strcat(filepath, "/server");
 797   strcat(filepath, filename);
 798   if (exists(filepath)) {
 799     strcpy(rpath, filepath);
 800     return true;
 801   }
 802 
 803   return false;
 804 }
 805 
 806 static bool read_shared_lib_info(struct ps_prochandle* ph) {
 807   static int pagesize = 0;
 808   int fd = ph->core->core_fd;
 809   int i = 0, j;
 810   uint32_t  v;
 811   mach_header_64 header;        // used to check if a file header in segment
 812   load_command lcmd;
 813   dylib_command dylibcmd;
 814 
 815   char name[BUF_SIZE];  // use to store name
 816 
 817   if (pagesize == 0) {
 818     pagesize = getpagesize();
 819     print_debug("page size is %d\n", pagesize);
 820   }
 821   for (j = 0; j < ph->core->num_maps; j++) {
 822     map_info *iter = ph->core->map_array[j];   // head
 823     off_t fpos = iter->offset;
 824     if (iter->fd != fd) {
 825       // only search core file!
 826       continue;
 827     }
 828     print_debug("map_info %d: vmaddr = 0x%016" PRIx64 "  fileoff = %" PRIu64 "  vmsize = %" PRIu64 "\n",
 829                            j, iter->vaddr, iter->offset, iter->memsz);
 830     lseek(fd, fpos, SEEK_SET);
 831     // we assume .dylib loaded at segment address --- which is true for JVM libraries
 832     // multiple files may be loaded in one segment.
 833     // if first word is not a magic word, means this segment does not contain lib file.
 834     if (read(fd, (void *)&v, sizeof(uint32_t)) == sizeof(uint32_t)) {
 835       if (v != MH_MAGIC_64) {
 836         continue;
 837       }
 838     } else {
 839       // may be encountered last map, which is not readable
 840       continue;
 841     }
 842     while (ltell(fd) - iter->offset < iter->memsz) {
 843       lseek(fd, fpos, SEEK_SET);
 844       if (read(fd, (void *)&v, sizeof(uint32_t)) != sizeof(uint32_t)) {
 845         break;
 846       }
 847       if (v != MH_MAGIC_64) {
 848         fpos = (ltell(fd) + pagesize -1)/pagesize * pagesize;
 849         continue;
 850       }
 851       lseek(fd, -sizeof(uint32_t), SEEK_CUR);
 852       // this is the file begining to core file.
 853       if (read(fd, (void *)&header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
 854         goto err;
 855       }
 856       fpos = ltell(fd);
 857 
 858       // found a mach-o file in this segment
 859       for (i = 0; i < header.ncmds; i++) {
 860         // read commands in this "file"
 861         // LC_ID_DYLIB is the file itself for a .dylib
 862         lseek(fd, fpos, SEEK_SET);
 863         if (read(fd, (void *)&lcmd, sizeof(load_command)) != sizeof(load_command)) {
 864           return false;   // error
 865         }
 866         fpos += lcmd.cmdsize;  // next command position
 867         // make sure still within seg size.
 868         if (fpos  - lcmd.cmdsize - iter->offset > iter->memsz) {
 869           print_debug("Warning: out of segement limit: %ld \n", fpos  - lcmd.cmdsize - iter->offset);
 870           break;  // no need to iterate all commands
 871         }
 872         if (lcmd.cmd == LC_ID_DYLIB) {
 873           lseek(fd, -sizeof(load_command), SEEK_CUR);
 874           if (read(fd, (void *)&dylibcmd, sizeof(dylib_command)) != sizeof(dylib_command)) {
 875             return false;
 876           }
 877           /**** name stored at dylib_command.dylib.name.offset, is a C string  */
 878           lseek(fd, dylibcmd.dylib.name.offset - sizeof(dylib_command), SEEK_CUR);
 879           int j = 0;
 880           while (j < BUF_SIZE) {
 881             read(fd, (void *)(name + j), sizeof(char));
 882             if (name[j] == '\0') break;
 883             j++;
 884           }
 885           print_debug("%s\n", name);
 886           // changed name from @rpath/xxxx.dylib to real path
 887           if (strrchr(name, '@')) {
 888             get_real_path(ph, name);
 889             print_debug("get_real_path returned: %s\n", name);
 890           }
 891           add_lib_info(ph, name, iter->vaddr);
 892           break;
 893         }
 894       }
 895       // done with the file, advanced to next page to search more files
 896       fpos = (ltell(fd) + pagesize - 1) / pagesize * pagesize;
 897     }
 898   }
 899   return true;
 900 err:
 901   return false;
 902 }
 903 
 904 bool read_macho64_header(int fd, mach_header_64* core_header) {
 905   bool is_macho = false;
 906   if (fd < 0) return false;
 907   off_t pos = ltell(fd);
 908   lseek(fd, 0, SEEK_SET);
 909   if (read(fd, (void *)core_header, sizeof(mach_header_64)) != sizeof(mach_header_64)) {
 910     is_macho = false;
 911   } else {
 912     is_macho = (core_header->magic ==  MH_MAGIC_64 || core_header->magic ==  MH_CIGAM_64);
 913   }
 914   lseek(fd, pos, SEEK_SET);
 915   return is_macho;
 916 }
 917 
 918 // the one and only one exposed stuff from this file
 919 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
 920   mach_header_64 core_header;
 921   mach_header_64 exec_header;
 922 
 923   struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
 924   if (ph == NULL) {
 925     print_debug("cant allocate ps_prochandle\n");
 926     return NULL;
 927   }
 928 
 929   if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
 930     free(ph);
 931     print_debug("can't allocate ps_prochandle\n");
 932     return NULL;
 933   }
 934 
 935   // initialize ph
 936   ph->ops = &core_ops;
 937   ph->core->core_fd   = -1;
 938   ph->core->exec_fd   = -1;
 939   ph->core->interp_fd = -1;
 940 
 941   print_debug("exec: %s   core: %s", exec_file, core_file);
 942 
 943   strncpy(ph->core->exec_path, exec_file, sizeof(ph->core->exec_path));
 944 
 945   // open the core file
 946   if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
 947     print_error("can't open core file\n");
 948     goto err;
 949   }
 950 
 951   // read core file header
 952   if (read_macho64_header(ph->core->core_fd, &core_header) != true || core_header.filetype != MH_CORE) {
 953     print_debug("core file is not a valid Mach-O file\n");
 954     goto err;
 955   }
 956 
 957   if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
 958     print_error("can't open executable file\n");
 959     goto err;
 960   }
 961 
 962   if (read_macho64_header(ph->core->exec_fd, &exec_header) != true ||
 963                           exec_header.filetype != MH_EXECUTE) {
 964     print_error("executable file is not a valid Mach-O file\n");
 965     goto err;
 966   }
 967 
 968   // process core file segments
 969   if (read_core_segments(ph) != true) {
 970     print_error("failed to read core segments\n");
 971     goto err;
 972   }
 973 
 974   // allocate and sort maps into map_array, we need to do this
 975   // here because read_shared_lib_info needs to read from debuggee
 976   // address space
 977   if (sort_map_array(ph) != true) {
 978     print_error("failed to sort segment map array\n");
 979     goto err;
 980   }
 981 
 982   if (read_shared_lib_info(ph) != true) {
 983     print_error("failed to read libraries\n");
 984     goto err;
 985   }
 986 
 987   // sort again because we have added more mappings from shared objects
 988   if (sort_map_array(ph) != true) {
 989     print_error("failed to sort segment map array\n");
 990     goto err;
 991   }
 992 
 993   if (init_classsharing_workaround(ph) != true) {
 994     print_error("failed to workaround classshareing\n");
 995     goto err;
 996   }
 997 
 998   print_debug("Leave Pgrab_core\n");
 999   return ph;
1000 
1001 err:
1002   Prelease(ph);
1003   return NULL;
1004 }
1005 
1006 #else // __APPLE__ (none macosx)
1007 
1008 // read regs and create thread from core file
1009 static bool core_handle_prstatus(struct ps_prochandle* ph, const char* buf, size_t nbytes) {
1010    // we have to read prstatus_t from buf
1011    // assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t");
1012    prstatus_t* prstat = (prstatus_t*) buf;
1013    sa_thread_info* newthr;
1014    print_debug("got integer regset for lwp %d\n", prstat->pr_pid);
1015    // we set pthread_t to -1 for core dump
1016    if((newthr = add_thread_info(ph, (pthread_t) -1,  prstat->pr_pid)) == NULL)
1017       return false;
1018 
1019    // copy regs
1020    memcpy(&newthr->regs, &prstat->pr_reg, sizeof(struct reg));
1021 
1022    if (is_debug()) {
1023       print_debug("integer regset\n");
1024 #ifdef i386
1025       // print the regset
1026       print_debug("\teax = 0x%x\n", newthr->regs.r_eax);
1027       print_debug("\tebx = 0x%x\n", newthr->regs.r_ebx);
1028       print_debug("\tecx = 0x%x\n", newthr->regs.r_ecx);
1029       print_debug("\tedx = 0x%x\n", newthr->regs.r_edx);
1030       print_debug("\tesp = 0x%x\n", newthr->regs.r_esp);
1031       print_debug("\tebp = 0x%x\n", newthr->regs.r_ebp);
1032       print_debug("\tesi = 0x%x\n", newthr->regs.r_esi);
1033       print_debug("\tedi = 0x%x\n", newthr->regs.r_edi);
1034       print_debug("\teip = 0x%x\n", newthr->regs.r_eip);
1035 #endif
1036 
1037 #if defined(amd64) || defined(x86_64)
1038       // print the regset
1039       print_debug("\tr15 = 0x%lx\n", newthr->regs.r_r15);
1040       print_debug("\tr14 = 0x%lx\n", newthr->regs.r_r14);
1041       print_debug("\tr13 = 0x%lx\n", newthr->regs.r_r13);
1042       print_debug("\tr12 = 0x%lx\n", newthr->regs.r_r12);
1043       print_debug("\trbp = 0x%lx\n", newthr->regs.r_rbp);
1044       print_debug("\trbx = 0x%lx\n", newthr->regs.r_rbx);
1045       print_debug("\tr11 = 0x%lx\n", newthr->regs.r_r11);
1046       print_debug("\tr10 = 0x%lx\n", newthr->regs.r_r10);
1047       print_debug("\tr9 = 0x%lx\n", newthr->regs.r_r9);
1048       print_debug("\tr8 = 0x%lx\n", newthr->regs.r_r8);
1049       print_debug("\trax = 0x%lx\n", newthr->regs.r_rax);
1050       print_debug("\trcx = 0x%lx\n", newthr->regs.r_rcx);
1051       print_debug("\trdx = 0x%lx\n", newthr->regs.r_rdx);
1052       print_debug("\trsi = 0x%lx\n", newthr->regs.r_rsi);
1053       print_debug("\trdi = 0x%lx\n", newthr->regs.r_rdi);
1054       //print_debug("\torig_rax = 0x%lx\n", newthr->regs.orig_rax);
1055       print_debug("\trip = 0x%lx\n", newthr->regs.r_rip);
1056       print_debug("\tcs = 0x%lx\n", newthr->regs.r_cs);
1057       //print_debug("\teflags = 0x%lx\n", newthr->regs.eflags);
1058       print_debug("\trsp = 0x%lx\n", newthr->regs.r_rsp);
1059       print_debug("\tss = 0x%lx\n", newthr->regs.r_ss);
1060       //print_debug("\tfs_base = 0x%lx\n", newthr->regs.fs_base);
1061       //print_debug("\tgs_base = 0x%lx\n", newthr->regs.gs_base);
1062       //print_debug("\tds = 0x%lx\n", newthr->regs.ds);
1063       //print_debug("\tes = 0x%lx\n", newthr->regs.es);
1064       //print_debug("\tfs = 0x%lx\n", newthr->regs.fs);
1065       //print_debug("\tgs = 0x%lx\n", newthr->regs.gs);
1066 #endif
1067    }
1068 
1069    return true;
1070 }
1071 
1072 #define ROUNDUP(x, y)  ((((x)+((y)-1))/(y))*(y))
1073 
1074 // read NT_PRSTATUS entries from core NOTE segment
1075 static bool core_handle_note(struct ps_prochandle* ph, ELF_PHDR* note_phdr) {
1076    char* buf = NULL;
1077    char* p = NULL;
1078    size_t size = note_phdr->p_filesz;
1079 
1080    // we are interested in just prstatus entries. we will ignore the rest.
1081    // Advance the seek pointer to the start of the PT_NOTE data
1082    if (lseek(ph->core->core_fd, note_phdr->p_offset, SEEK_SET) == (off_t)-1) {
1083       print_debug("failed to lseek to PT_NOTE data\n");
1084       return false;
1085    }
1086 
1087    // Now process the PT_NOTE structures.  Each one is preceded by
1088    // an Elf{32/64}_Nhdr structure describing its type and size.
1089    if ( (buf = (char*) malloc(size)) == NULL) {
1090       print_debug("can't allocate memory for reading core notes\n");
1091       goto err;
1092    }
1093 
1094    // read notes into buffer
1095    if (read(ph->core->core_fd, buf, size) != size) {
1096       print_debug("failed to read notes, core file must have been truncated\n");
1097       goto err;
1098    }
1099 
1100    p = buf;
1101    while (p < buf + size) {
1102       ELF_NHDR* notep = (ELF_NHDR*) p;
1103       char* descdata  = p + sizeof(ELF_NHDR) + ROUNDUP(notep->n_namesz, 4);
1104       print_debug("Note header with n_type = %d and n_descsz = %u\n",
1105                                    notep->n_type, notep->n_descsz);
1106 
1107       if (notep->n_type == NT_PRSTATUS) {
1108         if (core_handle_prstatus(ph, descdata, notep->n_descsz) != true) {
1109           return false;
1110         }
1111       }
1112       p = descdata + ROUNDUP(notep->n_descsz, 4);
1113    }
1114 
1115    free(buf);
1116    return true;
1117 
1118 err:
1119    if (buf) free(buf);
1120    return false;
1121 }
1122 
1123 // read all segments from core file
1124 static bool read_core_segments(struct ps_prochandle* ph, ELF_EHDR* core_ehdr) {
1125    int i = 0;
1126    ELF_PHDR* phbuf = NULL;
1127    ELF_PHDR* core_php = NULL;
1128 
1129    if ((phbuf =  read_program_header_table(ph->core->core_fd, core_ehdr)) == NULL)
1130       return false;
1131 
1132    /*
1133     * Now iterate through the program headers in the core file.
1134     * We're interested in two types of Phdrs: PT_NOTE (which
1135     * contains a set of saved /proc structures), and PT_LOAD (which
1136     * represents a memory mapping from the process's address space).
1137     *
1138     * Difference b/w Solaris PT_NOTE and Linux/BSD PT_NOTE:
1139     *
1140     *     In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
1141     *     contains /proc structs in the pre-2.6 unstructured /proc format. the last
1142     *     PT_NOTE has data in new /proc format.
1143     *
1144     *     In Solaris, there is only one pstatus (process status). pstatus contains
1145     *     integer register set among other stuff. For each LWP, we have one lwpstatus
1146     *     entry that has integer regset for that LWP.
1147     *
1148     *     Linux threads are actually 'clone'd processes. To support core analysis
1149     *     of "multithreaded" process, Linux creates more than one pstatus (called
1150     *     "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one
1151     *     "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular
1152     *     function "elf_core_dump".
1153     */
1154 
1155     for (core_php = phbuf, i = 0; i < core_ehdr->e_phnum; i++) {
1156       switch (core_php->p_type) {
1157          case PT_NOTE:
1158             if (core_handle_note(ph, core_php) != true) {
1159               goto err;
1160             }
1161             break;
1162 
1163          case PT_LOAD: {
1164             if (core_php->p_filesz != 0) {
1165                if (add_map_info(ph, ph->core->core_fd, core_php->p_offset,
1166                   core_php->p_vaddr, core_php->p_filesz) == NULL) goto err;
1167             }
1168             break;
1169          }
1170       }
1171 
1172       core_php++;
1173    }
1174 
1175    free(phbuf);
1176    return true;
1177 err:
1178    free(phbuf);
1179    return false;
1180 }
1181 
1182 // read segments of a shared object
1183 static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) {
1184   int i = 0;
1185   ELF_PHDR* phbuf;
1186   ELF_PHDR* lib_php = NULL;
1187 
1188   int page_size=sysconf(_SC_PAGE_SIZE);
1189 
1190   if ((phbuf = read_program_header_table(lib_fd, lib_ehdr)) == NULL) {
1191     return false;
1192   }
1193 
1194   // we want to process only PT_LOAD segments that are not writable.
1195   // i.e., text segments. The read/write/exec (data) segments would
1196   // have been already added from core file segments.
1197   for (lib_php = phbuf, i = 0; i < lib_ehdr->e_phnum; i++) {
1198     if ((lib_php->p_type == PT_LOAD) && !(lib_php->p_flags & PF_W) && (lib_php->p_filesz != 0)) {
1199 
1200       uintptr_t target_vaddr = lib_php->p_vaddr + lib_base;
1201       map_info *existing_map = core_lookup(ph, target_vaddr);
1202 
1203       if (existing_map == NULL){
1204         if (add_map_info(ph, lib_fd, lib_php->p_offset,
1205                           target_vaddr, lib_php->p_filesz) == NULL) {
1206           goto err;
1207         }
1208       } else {
1209         if ((existing_map->memsz != page_size) &&
1210             (existing_map->fd != lib_fd) &&
1211             (existing_map->memsz != lib_php->p_filesz)){
1212 
1213           print_debug("address conflict @ 0x%lx (size = %ld, flags = %d\n)",
1214                         target_vaddr, lib_php->p_filesz, lib_php->p_flags);
1215           goto err;
1216         }
1217 
1218         /* replace PT_LOAD segment with library segment */
1219         print_debug("overwrote with new address mapping (memsz %ld -> %ld)\n",
1220                      existing_map->memsz, lib_php->p_filesz);
1221 
1222         existing_map->fd = lib_fd;
1223         existing_map->offset = lib_php->p_offset;
1224         existing_map->memsz = lib_php->p_filesz;
1225       }
1226     }
1227 
1228     lib_php++;
1229   }
1230 
1231   free(phbuf);
1232   return true;
1233 err:
1234   free(phbuf);
1235   return false;
1236 }
1237 
1238 // process segments from interpreter (ld.so or ld-linux.so or ld-elf.so)
1239 static bool read_interp_segments(struct ps_prochandle* ph) {
1240    ELF_EHDR interp_ehdr;
1241 
1242    if (read_elf_header(ph->core->interp_fd, &interp_ehdr) != true) {
1243        print_debug("interpreter is not a valid ELF file\n");
1244        return false;
1245    }
1246 
1247    if (read_lib_segments(ph, ph->core->interp_fd, &interp_ehdr, ph->core->ld_base_addr) != true) {
1248        print_debug("can't read segments of interpreter\n");
1249        return false;
1250    }
1251 
1252    return true;
1253 }
1254 
1255 // process segments of a a.out
1256 static bool read_exec_segments(struct ps_prochandle* ph, ELF_EHDR* exec_ehdr) {
1257    int i = 0;
1258    ELF_PHDR* phbuf = NULL;
1259    ELF_PHDR* exec_php = NULL;
1260 
1261    if ((phbuf = read_program_header_table(ph->core->exec_fd, exec_ehdr)) == NULL)
1262       return false;
1263 
1264    for (exec_php = phbuf, i = 0; i < exec_ehdr->e_phnum; i++) {
1265       switch (exec_php->p_type) {
1266 
1267          // add mappings for PT_LOAD segments
1268          case PT_LOAD: {
1269             // add only non-writable segments of non-zero filesz
1270             if (!(exec_php->p_flags & PF_W) && exec_php->p_filesz != 0) {
1271                if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
1272             }
1273             break;
1274          }
1275 
1276          // read the interpreter and it's segments
1277          case PT_INTERP: {
1278             char interp_name[BUF_SIZE];
1279 
1280             pread(ph->core->exec_fd, interp_name, MIN(exec_php->p_filesz, BUF_SIZE), exec_php->p_offset);
1281             print_debug("ELF interpreter %s\n", interp_name);
1282             // read interpreter segments as well
1283             if ((ph->core->interp_fd = pathmap_open(interp_name)) < 0) {
1284                print_debug("can't open runtime loader\n");
1285                goto err;
1286             }
1287             break;
1288          }
1289 
1290          // from PT_DYNAMIC we want to read address of first link_map addr
1291          case PT_DYNAMIC: {
1292             ph->core->dynamic_addr = exec_php->p_vaddr;
1293             print_debug("address of _DYNAMIC is 0x%lx\n", ph->core->dynamic_addr);
1294             break;
1295          }
1296 
1297       } // switch
1298       exec_php++;
1299    } // for
1300 
1301    free(phbuf);
1302    return true;
1303 err:
1304    free(phbuf);
1305    return false;
1306 }
1307 
1308 #define FIRST_LINK_MAP_OFFSET offsetof(struct r_debug,  r_map)
1309 #define LD_BASE_OFFSET        offsetof(struct r_debug,  r_ldbase)
1310 #define LINK_MAP_ADDR_OFFSET  offsetof(struct link_map, l_addr)
1311 #define LINK_MAP_NAME_OFFSET  offsetof(struct link_map, l_name)
1312 #define LINK_MAP_NEXT_OFFSET  offsetof(struct link_map, l_next)
1313 
1314 // read shared library info from runtime linker's data structures.
1315 // This work is done by librtlb_db in Solaris
1316 static bool read_shared_lib_info(struct ps_prochandle* ph) {
1317   uintptr_t addr = ph->core->dynamic_addr;
1318   uintptr_t debug_base;
1319   uintptr_t first_link_map_addr;
1320   uintptr_t ld_base_addr;
1321   uintptr_t link_map_addr;
1322   uintptr_t lib_base_diff;
1323   uintptr_t lib_base;
1324   uintptr_t lib_name_addr;
1325   char lib_name[BUF_SIZE];
1326   ELF_DYN dyn;
1327   ELF_EHDR elf_ehdr;
1328   int lib_fd;
1329 
1330   // _DYNAMIC has information of the form
1331   //         [tag] [data] [tag] [data] .....
1332   // Both tag and data are pointer sized.
1333   // We look for dynamic info with DT_DEBUG. This has shared object info.
1334   // refer to struct r_debug in link.h
1335 
1336   dyn.d_tag = DT_NULL;
1337   while (dyn.d_tag != DT_DEBUG) {
1338     if (ps_pread(ph, (psaddr_t) addr, &dyn, sizeof(ELF_DYN)) != PS_OK) {
1339       print_debug("can't read debug info from _DYNAMIC\n");
1340       return false;
1341     }
1342     addr += sizeof(ELF_DYN);
1343   }
1344 
1345   // we have got Dyn entry with DT_DEBUG
1346   debug_base = dyn.d_un.d_ptr;
1347   // at debug_base we have struct r_debug. This has first link map in r_map field
1348   if (ps_pread(ph, (psaddr_t) debug_base + FIRST_LINK_MAP_OFFSET,
1349                  &first_link_map_addr, sizeof(uintptr_t)) != PS_OK) {
1350     print_debug("can't read first link map address\n");
1351     return false;
1352   }
1353 
1354   // read ld_base address from struct r_debug
1355 #if 0  // There is no r_ldbase member on BSD
1356   if (ps_pread(ph, (psaddr_t) debug_base + LD_BASE_OFFSET, &ld_base_addr,
1357                   sizeof(uintptr_t)) != PS_OK) {
1358     print_debug("can't read ld base address\n");
1359     return false;
1360   }
1361   ph->core->ld_base_addr = ld_base_addr;
1362 #else
1363   ph->core->ld_base_addr = 0;
1364 #endif
1365 
1366   print_debug("interpreter base address is 0x%lx\n", ld_base_addr);
1367 
1368   // now read segments from interp (i.e ld.so or ld-linux.so or ld-elf.so)
1369   if (read_interp_segments(ph) != true) {
1370     return false;
1371   }
1372 
1373   // after adding interpreter (ld.so) mappings sort again
1374   if (sort_map_array(ph) != true) {
1375     return false;
1376   }
1377 
1378   print_debug("first link map is at 0x%lx\n", first_link_map_addr);
1379 
1380   link_map_addr = first_link_map_addr;
1381   while (link_map_addr != 0) {
1382     // read library base address of the .so. Note that even though <sys/link.h> calls
1383     // link_map->l_addr as "base address",  this is * not * really base virtual
1384     // address of the shared object. This is actually the difference b/w the virtual
1385     // address mentioned in shared object and the actual virtual base where runtime
1386     // linker loaded it. We use "base diff" in read_lib_segments call below.
1387 
1388     if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_ADDR_OFFSET,
1389                  &lib_base_diff, sizeof(uintptr_t)) != PS_OK) {
1390       print_debug("can't read shared object base address diff\n");
1391       return false;
1392     }
1393 
1394     // read address of the name
1395     if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NAME_OFFSET,
1396                   &lib_name_addr, sizeof(uintptr_t)) != PS_OK) {
1397       print_debug("can't read address of shared object name\n");
1398       return false;
1399     }
1400 
1401     // read name of the shared object
1402     if (read_string(ph, (uintptr_t) lib_name_addr, lib_name, sizeof(lib_name)) != true) {
1403       print_debug("can't read shared object name\n");
1404       return false;
1405     }
1406 
1407     if (lib_name[0] != '\0') {
1408       // ignore empty lib names
1409       lib_fd = pathmap_open(lib_name);
1410 
1411       if (lib_fd < 0) {
1412         print_debug("can't open shared object %s\n", lib_name);
1413         // continue with other libraries...
1414       } else {
1415         if (read_elf_header(lib_fd, &elf_ehdr)) {
1416           lib_base = lib_base_diff + find_base_address(lib_fd, &elf_ehdr);
1417           print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n",
1418                        lib_name, lib_base, lib_base_diff);
1419           // while adding library mappings we need to use "base difference".
1420           if (! read_lib_segments(ph, lib_fd, &elf_ehdr, lib_base_diff)) {
1421             print_debug("can't read shared object's segments\n");
1422             close(lib_fd);
1423             return false;
1424           }
1425           add_lib_info_fd(ph, lib_name, lib_fd, lib_base);
1426           // Map info is added for the library (lib_name) so
1427           // we need to re-sort it before calling the p_pdread.
1428           if (sort_map_array(ph) != true) {
1429             return false;
1430           }
1431         } else {
1432           print_debug("can't read ELF header for shared object %s\n", lib_name);
1433           close(lib_fd);
1434           // continue with other libraries...
1435         }
1436       }
1437     }
1438 
1439     // read next link_map address
1440     if (ps_pread(ph, (psaddr_t) link_map_addr + LINK_MAP_NEXT_OFFSET,
1441                   &link_map_addr, sizeof(uintptr_t)) != PS_OK) {
1442       print_debug("can't read next link in link_map\n");
1443       return false;
1444     }
1445   }
1446 
1447   return true;
1448 }
1449 
1450 // the one and only one exposed stuff from this file
1451 struct ps_prochandle* Pgrab_core(const char* exec_file, const char* core_file) {
1452   ELF_EHDR core_ehdr;
1453   ELF_EHDR exec_ehdr;
1454 
1455   struct ps_prochandle* ph = (struct ps_prochandle*) calloc(1, sizeof(struct ps_prochandle));
1456   if (ph == NULL) {
1457     print_debug("can't allocate ps_prochandle\n");
1458     return NULL;
1459   }
1460 
1461   if ((ph->core = (struct core_data*) calloc(1, sizeof(struct core_data))) == NULL) {
1462     free(ph);
1463     print_debug("can't allocate ps_prochandle\n");
1464     return NULL;
1465   }
1466 
1467   // initialize ph
1468   ph->ops = &core_ops;
1469   ph->core->core_fd   = -1;
1470   ph->core->exec_fd   = -1;
1471   ph->core->interp_fd = -1;
1472 
1473   print_debug("exec: %s   core: %s", exec_file, core_file);
1474 
1475   // open the core file
1476   if ((ph->core->core_fd = open(core_file, O_RDONLY)) < 0) {
1477     print_debug("can't open core file\n");
1478     goto err;
1479   }
1480 
1481   // read core file ELF header
1482   if (read_elf_header(ph->core->core_fd, &core_ehdr) != true || core_ehdr.e_type != ET_CORE) {
1483     print_debug("core file is not a valid ELF ET_CORE file\n");
1484     goto err;
1485   }
1486 
1487   if ((ph->core->exec_fd = open(exec_file, O_RDONLY)) < 0) {
1488     print_debug("can't open executable file\n");
1489     goto err;
1490   }
1491 
1492   if (read_elf_header(ph->core->exec_fd, &exec_ehdr) != true || exec_ehdr.e_type != ET_EXEC) {
1493     print_debug("executable file is not a valid ELF ET_EXEC file\n");
1494     goto err;
1495   }
1496 
1497   // process core file segments
1498   if (read_core_segments(ph, &core_ehdr) != true) {
1499     goto err;
1500   }
1501 
1502   // process exec file segments
1503   if (read_exec_segments(ph, &exec_ehdr) != true) {
1504     goto err;
1505   }
1506 
1507   // exec file is also treated like a shared object for symbol search
1508   if (add_lib_info_fd(ph, exec_file, ph->core->exec_fd,
1509                       (uintptr_t)0 + find_base_address(ph->core->exec_fd, &exec_ehdr)) == NULL) {
1510     goto err;
1511   }
1512 
1513   // allocate and sort maps into map_array, we need to do this
1514   // here because read_shared_lib_info needs to read from debuggee
1515   // address space
1516   if (sort_map_array(ph) != true) {
1517     goto err;
1518   }
1519 
1520   if (read_shared_lib_info(ph) != true) {
1521     goto err;
1522   }
1523 
1524   // sort again because we have added more mappings from shared objects
1525   if (sort_map_array(ph) != true) {
1526     goto err;
1527   }
1528 
1529   if (init_classsharing_workaround(ph) != true) {
1530     goto err;
1531   }
1532 
1533   print_debug("Leave Pgrab_core\n");
1534   return ph;
1535 
1536 err:
1537   Prelease(ph);
1538   return NULL;
1539 }
1540 
1541 #endif // __APPLE__