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