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