< prev index next >

src/jdk.hotspot.agent/macosx/native/libsaproc/ps_core.c

Print this page




  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include <jni.h>
  26 #include <unistd.h>
  27 #include <fcntl.h>
  28 #include <string.h>
  29 #include <stdlib.h>
  30 #include <stddef.h>
  31 #include "libproc_impl.h"

  32 
  33 #ifdef __APPLE__
  34 #include "sun_jvm_hotspot_debugger_amd64_AMD64ThreadContext.h"
  35 #endif
  36 
  37 // This file has the libproc implementation to read core files.
  38 // For live processes, refer to ps_proc.c. Portions of this is adapted
  39 // /modelled after Solaris libproc.so (in particular Pcore.c)
  40 
  41 //----------------------------------------------------------------------
  42 // ps_prochandle cleanup helper functions
  43 
  44 // close all file descriptors
  45 static void close_files(struct ps_prochandle* ph) {
  46   lib_info* lib = NULL;
  47 
  48   // close core file descriptor
  49   if (ph->core->core_fd >= 0)
  50     close(ph->core->core_fd);
  51 


 187   while (mp) {
 188     if (addr >= mp->vaddr && addr < mp->vaddr + mp->memsz) {
 189       print_debug("located map_info at 0x%lx from class share maps\n", addr);
 190       return (mp);
 191     }
 192     mp = mp->next;
 193   }
 194 
 195   print_debug("can't locate map_info at 0x%lx\n", addr);
 196   return (NULL);
 197 }
 198 
 199 //---------------------------------------------------------------
 200 // Part of the class sharing workaround:
 201 //
 202 // With class sharing, pages are mapped from classes.jsa file.
 203 // The read-only class sharing pages are mapped as MAP_SHARED,
 204 // PROT_READ pages. These pages are not dumped into core dump.
 205 // With this workaround, these pages are read from classes.jsa.
 206 
 207 // FIXME: !HACK ALERT!
 208 // The format of sharing achive file header is needed to read shared heap
 209 // file mappings. For now, I am hard coding portion of FileMapHeader here.
 210 // Refer to filemap.hpp.
 211 
 212 // FileMapHeader describes the shared space data in the file to be
 213 // mapped.  This structure gets written to a file.  It is not a class,
 214 // so that the compilers don't add any compiler-private data to it.
 215 
 216 #define NUM_SHARED_MAPS 9
 217 
 218 // Refer to FileMapInfo::_current_version in filemap.hpp
 219 #define CURRENT_ARCHIVE_VERSION 3
 220 
 221 typedef unsigned char* address;
 222 typedef uintptr_t      uintx;
 223 typedef intptr_t       intx;
 224 
 225 
 226 struct FileMapHeader {
 227   int     _magic;                   // identify file type.
 228   int     _crc;                     // header crc checksum.
 229   int     _version;                 // (from enum, above.)
 230   size_t  _alignment;               // how shared archive should be aligned
 231   int     _obj_alignment;           // value of ObjectAlignmentInBytes
 232   address _narrow_oop_base;         // compressed oop encoding base
 233   int     _narrow_oop_shift;        // compressed oop encoding shift
 234   bool    _compact_strings;         // value of CompactStrings
 235   uintx   _max_heap_size;           // java max heap size during dumping
 236   int     _narrow_oop_mode;         // compressed oop encoding mode
 237   int     _narrow_klass_shift;      // save narrow klass base and shift
 238   address _narrow_klass_base;
 239   char*   _misc_data_patching_start;
 240   char*   _read_only_tables_start;
 241   address _cds_i2i_entry_code_buffers;
 242   size_t  _cds_i2i_entry_code_buffers_size;
 243   size_t  _core_spaces_size;        // number of bytes allocated by the core spaces
 244                                     // (mc, md, ro, rw and od).
 245 
 246 
 247   struct space_info {
 248     int     _crc;          // crc checksum of the current space
 249     size_t  _file_offset;  // sizeof(this) rounded to vm page size
 250     union {
 251       char*  _base;        // copy-on-write base address
 252       intx   _offset;      // offset from the compressed oop encoding base, only used
 253                            // by archive heap space
 254     } _addr;
 255     size_t _used;          // for setting space top on read
 256     // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with
 257     // the C type matching the C++ bool type on any given platform.
 258     // We assume the corresponding C type is char but licensees
 259     // may need to adjust the type of these fields.
 260     char   _read_only;     // read only space?
 261     char   _allow_exec;    // executable code in space?
 262   } _space[NUM_SHARED_MAPS];
 263 
 264   // Ignore the rest of the FileMapHeader. We don't need those fields here.
 265 };
 266 
 267 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) {
 268   jboolean i;
 269   if (ps_pread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) {
 270     *pvalue = i;
 271     return true;
 272   } else {
 273     return false;
 274   }
 275 }
 276 
 277 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
 278   uintptr_t uip;
 279   if (ps_pread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) {
 280     *pvalue = uip;
 281     return true;
 282   } else {
 283     return false;
 284   }
 285 }
 286 


 309 #define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE"
 310 
 311 #ifdef __APPLE__
 312 #define USE_SHARED_SPACES_SYM "_UseSharedSpaces"
 313 #define LIBJVM_NAME "/libjvm.dylib"
 314 #else
 315 #define USE_SHARED_SPACES_SYM "UseSharedSpaces"
 316 #define LIBJVM_NAME "/libjvm.so"
 317 #endif // __APPLE_
 318 
 319 static bool init_classsharing_workaround(struct ps_prochandle* ph) {
 320   int m;
 321   size_t n;
 322   lib_info* lib = ph->libs;
 323   while (lib != NULL) {
 324     // we are iterating over shared objects from the core dump. look for
 325     // libjvm.so.
 326     const char *jvm_name = 0;
 327     if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) {
 328       char classes_jsa[PATH_MAX];
 329       struct FileMapHeader header;
 330       int fd = -1;
 331       uintptr_t base = 0, useSharedSpacesAddr = 0;
 332       uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
 333       jboolean useSharedSpaces = 0;
 334 
 335       memset(classes_jsa, 0, sizeof(classes_jsa));
 336       jvm_name = lib->name;
 337       useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM);
 338       if (useSharedSpacesAddr == 0) {
 339         print_debug("can't lookup 'UseSharedSpaces' flag\n");
 340         return false;
 341       }
 342 
 343       // Hotspot vm types are not exported to build this library. So
 344       // using equivalent type jboolean to read the value of
 345       // UseSharedSpaces which is same as hotspot type "bool".
 346       if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
 347         print_debug("can't read the value of 'UseSharedSpaces' flag\n");
 348         return false;
 349       }


 363         print_debug("can't read shared archive path pointer\n");
 364         return false;
 365       }
 366 
 367       if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) {
 368         print_debug("can't read shared archive path value\n");
 369         return false;
 370       }
 371 
 372       print_debug("looking for %s\n", classes_jsa);
 373       // open the class sharing archive file
 374       fd = pathmap_open(classes_jsa);
 375       if (fd < 0) {
 376         print_debug("can't open %s!\n", classes_jsa);
 377         ph->core->classes_jsa_fd = -1;
 378         return false;
 379       } else {
 380         print_debug("opened %s\n", classes_jsa);
 381       }
 382 
 383       // read FileMapHeader from the file
 384       memset(&header, 0, sizeof(struct FileMapHeader));
 385       if ((n = read(fd, &header, sizeof(struct FileMapHeader)))
 386            != sizeof(struct FileMapHeader)) {
 387         print_debug("can't read shared archive file map header from %s\n", classes_jsa);
 388         close(fd);
 389         return false;
 390       }
 391 
 392       // check file magic
 393       if (header._magic != 0xf00baba2) {
 394         print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n",
 395                      classes_jsa, header._magic);
 396         close(fd);
 397         return false;
 398       }
 399 
 400       // check version
 401       if (header._version != CURRENT_ARCHIVE_VERSION) {
 402         print_debug("%s has wrong shared archive file version %d, expecting %d\n",
 403                      classes_jsa, header._version, CURRENT_ARCHIVE_VERSION);
 404         close(fd);
 405         return false;
 406       }
 407 
 408       ph->core->classes_jsa_fd = fd;
 409       // add read-only maps from classes.jsa to the list of maps
 410       for (m = 0; m < NUM_SHARED_MAPS; m++) {
 411         if (header._space[m]._read_only) {
 412           base = (uintptr_t) header._space[m]._addr._base;
 413           // no need to worry about the fractional pages at-the-end.
 414           // possible fractional pages are handled by core_read_data.
 415           add_class_share_map_info(ph, (off_t) header._space[m]._file_offset,
 416                                    base, (size_t) header._space[m]._used);
 417           print_debug("added a share archive map at 0x%lx\n", base);
 418         }
 419       }
 420       return true;
 421    }
 422    lib = lib->next;
 423   }
 424   return true;
 425 }
 426 
 427 //---------------------------------------------------------------------------
 428 // functions to handle map_info
 429 
 430 // Order mappings based on virtual address.  We use this function as the




  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 "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 


 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 static bool read_jboolean(struct ps_prochandle* ph, uintptr_t addr, jboolean* pvalue) {
 209   jboolean i;
 210   if (ps_pread(ph, (psaddr_t) addr, &i, sizeof(i)) == PS_OK) {
 211     *pvalue = i;
 212     return true;
 213   } else {
 214     return false;
 215   }
 216 }
 217 
 218 static bool read_pointer(struct ps_prochandle* ph, uintptr_t addr, uintptr_t* pvalue) {
 219   uintptr_t uip;
 220   if (ps_pread(ph, (psaddr_t) addr, (char *)&uip, sizeof(uip)) == PS_OK) {
 221     *pvalue = uip;
 222     return true;
 223   } else {
 224     return false;
 225   }
 226 }
 227 


 250 #define SHARED_ARCHIVE_PATH_SYM "__ZN9Arguments17SharedArchivePathE"
 251 
 252 #ifdef __APPLE__
 253 #define USE_SHARED_SPACES_SYM "_UseSharedSpaces"
 254 #define LIBJVM_NAME "/libjvm.dylib"
 255 #else
 256 #define USE_SHARED_SPACES_SYM "UseSharedSpaces"
 257 #define LIBJVM_NAME "/libjvm.so"
 258 #endif // __APPLE_
 259 
 260 static bool init_classsharing_workaround(struct ps_prochandle* ph) {
 261   int m;
 262   size_t n;
 263   lib_info* lib = ph->libs;
 264   while (lib != NULL) {
 265     // we are iterating over shared objects from the core dump. look for
 266     // libjvm.so.
 267     const char *jvm_name = 0;
 268     if ((jvm_name = strstr(lib->name, LIBJVM_NAME)) != 0) {
 269       char classes_jsa[PATH_MAX];
 270       CDSFileMapHeaderBase header;
 271       int fd = -1;
 272       uintptr_t base = 0, useSharedSpacesAddr = 0;
 273       uintptr_t sharedArchivePathAddrAddr = 0, sharedArchivePathAddr = 0;
 274       jboolean useSharedSpaces = 0;
 275 
 276       memset(classes_jsa, 0, sizeof(classes_jsa));
 277       jvm_name = lib->name;
 278       useSharedSpacesAddr = lookup_symbol(ph, jvm_name, USE_SHARED_SPACES_SYM);
 279       if (useSharedSpacesAddr == 0) {
 280         print_debug("can't lookup 'UseSharedSpaces' flag\n");
 281         return false;
 282       }
 283 
 284       // Hotspot vm types are not exported to build this library. So
 285       // using equivalent type jboolean to read the value of
 286       // UseSharedSpaces which is same as hotspot type "bool".
 287       if (read_jboolean(ph, useSharedSpacesAddr, &useSharedSpaces) != true) {
 288         print_debug("can't read the value of 'UseSharedSpaces' flag\n");
 289         return false;
 290       }


 304         print_debug("can't read shared archive path pointer\n");
 305         return false;
 306       }
 307 
 308       if (read_string(ph, sharedArchivePathAddr, classes_jsa, sizeof(classes_jsa)) != true) {
 309         print_debug("can't read shared archive path value\n");
 310         return false;
 311       }
 312 
 313       print_debug("looking for %s\n", classes_jsa);
 314       // open the class sharing archive file
 315       fd = pathmap_open(classes_jsa);
 316       if (fd < 0) {
 317         print_debug("can't open %s!\n", classes_jsa);
 318         ph->core->classes_jsa_fd = -1;
 319         return false;
 320       } else {
 321         print_debug("opened %s\n", classes_jsa);
 322       }
 323 
 324       // read CDSFileMapHeaderBase from the file
 325       memset(&header, 0, sizeof(CDSFileMapHeaderBase));
 326       if ((n = read(fd, &header, sizeof(CDSFileMapHeaderBase)))
 327            != sizeof(CDSFileMapHeaderBase)) {
 328         print_debug("can't read shared archive file map header from %s\n", classes_jsa);
 329         close(fd);
 330         return false;
 331       }
 332 
 333       // check file magic
 334       if (header._magic != CDS_ARCHIVE_MAGIC) {
 335         print_debug("%s has bad shared archive file magic number 0x%x, expecting 0x%x\n",
 336                     classes_jsa, header._magic, CDS_ARCHIVE_MAGIC);
 337         close(fd);
 338         return false;
 339       }
 340 
 341       // check version
 342       if (header._version != CURRENT_CDS_ARCHIVE_VERSION) {
 343         print_debug("%s has wrong shared archive file version %d, expecting %d\n",
 344                      classes_jsa, header._version, CURRENT_CDS_ARCHIVE_VERSION);
 345         close(fd);
 346         return false;
 347       }
 348 
 349       ph->core->classes_jsa_fd = fd;
 350       // add read-only maps from classes.jsa to the list of maps
 351       for (m = 0; m < NUM_CDS_REGIONS; m++) {
 352         if (header._space[m]._read_only) {
 353           base = (uintptr_t) header._space[m]._addr._base;
 354           // no need to worry about the fractional pages at-the-end.
 355           // possible fractional pages are handled by core_read_data.
 356           add_class_share_map_info(ph, (off_t) header._space[m]._file_offset,
 357                                    base, (size_t) header._space[m]._used);
 358           print_debug("added a share archive map at 0x%lx\n", base);
 359         }
 360       }
 361       return true;
 362    }
 363    lib = lib->next;
 364   }
 365   return true;
 366 }
 367 
 368 //---------------------------------------------------------------------------
 369 // functions to handle map_info
 370 
 371 // Order mappings based on virtual address.  We use this function as the


< prev index next >