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