< prev index next >

src/jdk.hotspot.agent/solaris/native/libsaproc/saproc.cpp

Print this page


   1 /*
   2  * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


 479 // Pstack_iter() proc_stack_f callback in Nevada-B159 or later
 480 /*ARGSUSED*/
 481 static int
 482 wrapper_fill_cframe_list(void *cd, const prgregset_t regs, uint_t argc,
 483                          const long *argv, int frame_flags, int sig) {
 484   return(fill_cframe_list(cd, regs, argc, argv));
 485 }
 486 
 487 // part of the class sharing workaround
 488 
 489 // FIXME: !!HACK ALERT!!
 490 
 491 // The format of sharing achive file header is needed to read shared heap
 492 // file mappings. For now, I am hard coding portion of FileMapHeader here.
 493 // Refer to filemap.hpp.
 494 
 495 // FileMapHeader describes the shared space data in the file to be
 496 // mapped.  This structure gets written to a file.  It is not a class, so
 497 // that the compilers don't add any compiler-private data to it.
 498 
 499 const int NUM_SHARED_MAPS = 4;
 500 
 501 // Refer to FileMapInfo::_current_version in filemap.hpp
 502 const int CURRENT_ARCHIVE_VERSION = 1;




 503 
 504 struct FileMapHeader {
 505  int   _magic;              // identify file type.

 506  int   _version;            // (from enum, above.)
 507  size_t _alignment;         // how shared archive should be aligned














 508 
 509 
 510  struct space_info {
 511    int    _file_offset;     // sizeof(this) rounded to vm page size


 512    char*  _base;            // copy-on-write base address
 513    size_t _capacity;        // for validity checking


 514    size_t _used;            // for setting space top on read
 515 
 516    bool   _read_only;       // read only space?
 517    bool   _allow_exec;      // executable code in space?
 518 


 519  } _space[NUM_SHARED_MAPS];
 520 
 521  // Ignore the rest of the FileMapHeader. We don't need those fields here.
 522 };
 523 
 524 static bool
 525 read_jboolean(struct ps_prochandle* ph, psaddr_t addr, jboolean* pvalue) {
 526   jboolean i;
 527   if (ps_pread(ph, addr, &i, sizeof(i)) == PS_OK) {
 528     *pvalue = i;
 529     return true;
 530   } else {
 531     return false;
 532   }
 533 }
 534 
 535 static bool
 536 read_pointer(struct ps_prochandle* ph, psaddr_t addr, uintptr_t* pvalue) {
 537   uintptr_t uip;
 538   if (ps_pread(ph, addr, &uip, sizeof(uip)) == PS_OK) {
 539     *pvalue = uip;
 540     return true;
 541   } else {


 660     sprintf(errMsg, "%s has bad shared archive magic 0x%x, expecting 0xf00baba2",
 661                    classes_jsa, pheader->_magic);
 662     close(fd);
 663     free(pheader);
 664     THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1);
 665   }
 666 
 667   // check version
 668   if (pheader->_version != CURRENT_ARCHIVE_VERSION) {
 669     char errMsg[ERR_MSG_SIZE];
 670     sprintf(errMsg, "%s has wrong shared archive version %d, expecting %d",
 671                    classes_jsa, pheader->_version, CURRENT_ARCHIVE_VERSION);
 672     close(fd);
 673     free(pheader);
 674     THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1);
 675   }
 676 
 677   if (_libsaproc_debug) {
 678     for (int m = 0; m < NUM_SHARED_MAPS; m++) {
 679        print_debug("shared file offset %d mapped at 0x%lx, size = %ld, read only? = %d\n",
 680           pheader->_space[m]._file_offset, pheader->_space[m]._base,
 681           pheader->_space[m]._used, pheader->_space[m]._read_only);
 682     }
 683   }
 684 
 685   // FIXME: For now, omitting other checks such as VM version etc.
 686 
 687   // store class archive file fd and map header in debugger object fields
 688   dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, fd);
 689   dbg->env->SetLongField(this_obj, p_file_map_header_ID, (jlong)(uintptr_t) pheader);
 690   return 1;
 691 }
 692 
 693 } // extern "C"
 694 
 695 // error messages for proc_arg_grab failure codes. The messages are
 696 // modified versions of comments against corresponding #defines in
 697 // libproc.h.
 698 static const char* proc_arg_grab_errmsgs[] = {
 699                       "",
 700  /* G_NOPROC */       "No such process",


1041 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0
1042   (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes) {
1043 
1044   jbyteArray array = env->NewByteArray(numBytes);
1045   CHECK_EXCEPTION_(0);
1046   jboolean isCopy;
1047   jbyte* bufPtr = env->GetByteArrayElements(array, &isCopy);
1048   CHECK_EXCEPTION_(0);
1049 
1050   jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
1051   ps_err_e ret = ps_pread((struct ps_prochandle*) p_ps_prochandle,
1052                        (psaddr_t)address, bufPtr, (size_t)numBytes);
1053 
1054   if (ret != PS_OK) {
1055     // part of the class sharing workaround. try shared heap area
1056     int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID);
1057     if (classes_jsa_fd != -1 && address != (jlong)0) {
1058       print_debug("read failed at 0x%lx, attempting shared heap area\n", (long) address);
1059 
1060       struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID);
1061       // walk through the shared mappings -- we just have 4 of them.
1062       // so, linear walking is okay.
1063       for (int m = 0; m < NUM_SHARED_MAPS; m++) {
1064 
1065         // We can skip the non-read-only maps. These are mapped as MAP_PRIVATE
1066         // and hence will be read by libproc. Besides, the file copy may be
1067         // stale because the process might have modified those pages.
1068         if (pheader->_space[m]._read_only) {
1069           jlong baseAddress = (jlong) (uintptr_t) pheader->_space[m]._base;
1070           size_t usedSize = pheader->_space[m]._used;
1071           if (address >= baseAddress && address < (baseAddress + usedSize)) {
1072             // the given address falls in this shared heap area
1073             print_debug("found shared map at 0x%lx\n", (long) baseAddress);
1074 
1075 
1076             // If more data is asked than actually mapped from file, we need to zero fill
1077             // till the end-of-page boundary. But, java array new does that for us. we just
1078             // need to read as much as data available.
1079 
1080 #define MIN2(x, y) (((x) < (y))? (x) : (y))
1081 
1082             jlong diff = address - baseAddress;
1083             jlong bytesToRead = MIN2(numBytes, usedSize - diff);
1084             off_t offset = pheader->_space[m]._file_offset  + off_t(diff);
1085             ssize_t bytesRead = pread(classes_jsa_fd, bufPtr, bytesToRead, offset);
1086             if (bytesRead != bytesToRead) {
1087               env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT);
1088               print_debug("shared map read failed\n");
1089               return jbyteArray(0);


   1 /*
   2  * Copyright (c) 2002, 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  *


 479 // Pstack_iter() proc_stack_f callback in Nevada-B159 or later
 480 /*ARGSUSED*/
 481 static int
 482 wrapper_fill_cframe_list(void *cd, const prgregset_t regs, uint_t argc,
 483                          const long *argv, int frame_flags, int sig) {
 484   return(fill_cframe_list(cd, regs, argc, argv));
 485 }
 486 
 487 // part of the class sharing workaround
 488 
 489 // FIXME: !!HACK ALERT!!
 490 
 491 // The format of sharing achive file header is needed to read shared heap
 492 // file mappings. For now, I am hard coding portion of FileMapHeader here.
 493 // Refer to filemap.hpp.
 494 
 495 // FileMapHeader describes the shared space data in the file to be
 496 // mapped.  This structure gets written to a file.  It is not a class, so
 497 // that the compilers don't add any compiler-private data to it.
 498 
 499 const int NUM_SHARED_MAPS = 9;
 500 
 501 // Refer to FileMapInfo::_current_version in filemap.hpp
 502 const int CURRENT_ARCHIVE_VERSION = 3;
 503 
 504 typedef unsigned char* address;
 505 typedef uintptr_t      uintx;
 506 typedef intptr_t       intx;
 507 
 508 struct FileMapHeader {
 509   int     _magic;                   // identify file type.
 510   int     _crc;                     // header crc checksum.
 511   int     _version;                 // (from enum, above.)
 512   size_t  _alignment;               // how shared archive should be aligned
 513   int     _obj_alignment;           // value of ObjectAlignmentInBytes
 514   address _narrow_oop_base;         // compressed oop encoding base
 515   int     _narrow_oop_shift;        // compressed oop encoding shift
 516   bool    _compact_strings;         // value of CompactStrings
 517   uintx   _max_heap_size;           // java max heap size during dumping
 518   int     _narrow_oop_mode;         // compressed oop encoding mode
 519   int     _narrow_klass_shift;      // save narrow klass base and shift
 520   address _narrow_klass_base;
 521   char*   _misc_data_patching_start;
 522   char*   _read_only_tables_start;
 523   address _cds_i2i_entry_code_buffers;
 524   size_t  _cds_i2i_entry_code_buffers_size;
 525   size_t  _core_spaces_size;        // number of bytes allocated by the core spaces
 526                                     // (mc, md, ro, rw and od).
 527 
 528 
 529   struct space_info {
 530     int     _crc;          // crc checksum of the current space
 531     size_t  _file_offset;  // sizeof(this) rounded to vm page size
 532     union {
 533       char*  _base;        // copy-on-write base address
 534       intx   _offset;      // offset from the compressed oop encoding base, only used
 535                            // by archive heap space
 536     } _addr;
 537     size_t _used;          // for setting space top on read
 538     // 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with
 539     // the C type matching the C++ bool type on any given platform.
 540     // We assume the corresponding C type is char but licensees
 541     // may need to adjust the type of these fields.
 542     char   _read_only;     // read only space?
 543     char   _allow_exec;    // executable code in space?
 544   } _space[NUM_SHARED_MAPS];
 545 
 546 // Ignore the rest of the FileMapHeader. We don't need those fields here.
 547 };
 548 
 549 static bool
 550 read_jboolean(struct ps_prochandle* ph, psaddr_t addr, jboolean* pvalue) {
 551   jboolean i;
 552   if (ps_pread(ph, addr, &i, sizeof(i)) == PS_OK) {
 553     *pvalue = i;
 554     return true;
 555   } else {
 556     return false;
 557   }
 558 }
 559 
 560 static bool
 561 read_pointer(struct ps_prochandle* ph, psaddr_t addr, uintptr_t* pvalue) {
 562   uintptr_t uip;
 563   if (ps_pread(ph, addr, &uip, sizeof(uip)) == PS_OK) {
 564     *pvalue = uip;
 565     return true;
 566   } else {


 685     sprintf(errMsg, "%s has bad shared archive magic 0x%x, expecting 0xf00baba2",
 686                    classes_jsa, pheader->_magic);
 687     close(fd);
 688     free(pheader);
 689     THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1);
 690   }
 691 
 692   // check version
 693   if (pheader->_version != CURRENT_ARCHIVE_VERSION) {
 694     char errMsg[ERR_MSG_SIZE];
 695     sprintf(errMsg, "%s has wrong shared archive version %d, expecting %d",
 696                    classes_jsa, pheader->_version, CURRENT_ARCHIVE_VERSION);
 697     close(fd);
 698     free(pheader);
 699     THROW_NEW_DEBUGGER_EXCEPTION_(errMsg, 1);
 700   }
 701 
 702   if (_libsaproc_debug) {
 703     for (int m = 0; m < NUM_SHARED_MAPS; m++) {
 704        print_debug("shared file offset %d mapped at 0x%lx, size = %ld, read only? = %d\n",
 705           pheader->_space[m]._file_offset, pheader->_space[m]._addr._base,
 706           pheader->_space[m]._used, pheader->_space[m]._read_only);
 707     }
 708   }
 709 
 710   // FIXME: For now, omitting other checks such as VM version etc.
 711 
 712   // store class archive file fd and map header in debugger object fields
 713   dbg->env->SetIntField(this_obj, classes_jsa_fd_ID, fd);
 714   dbg->env->SetLongField(this_obj, p_file_map_header_ID, (jlong)(uintptr_t) pheader);
 715   return 1;
 716 }
 717 
 718 } // extern "C"
 719 
 720 // error messages for proc_arg_grab failure codes. The messages are
 721 // modified versions of comments against corresponding #defines in
 722 // libproc.h.
 723 static const char* proc_arg_grab_errmsgs[] = {
 724                       "",
 725  /* G_NOPROC */       "No such process",


1066 JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0
1067   (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes) {
1068 
1069   jbyteArray array = env->NewByteArray(numBytes);
1070   CHECK_EXCEPTION_(0);
1071   jboolean isCopy;
1072   jbyte* bufPtr = env->GetByteArrayElements(array, &isCopy);
1073   CHECK_EXCEPTION_(0);
1074 
1075   jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
1076   ps_err_e ret = ps_pread((struct ps_prochandle*) p_ps_prochandle,
1077                        (psaddr_t)address, bufPtr, (size_t)numBytes);
1078 
1079   if (ret != PS_OK) {
1080     // part of the class sharing workaround. try shared heap area
1081     int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID);
1082     if (classes_jsa_fd != -1 && address != (jlong)0) {
1083       print_debug("read failed at 0x%lx, attempting shared heap area\n", (long) address);
1084 
1085       struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID);
1086       // walk through the shared mappings -- we just have 9 of them.
1087       // so, linear walking is okay.
1088       for (int m = 0; m < NUM_SHARED_MAPS; m++) {
1089 
1090         // We can skip the non-read-only maps. These are mapped as MAP_PRIVATE
1091         // and hence will be read by libproc. Besides, the file copy may be
1092         // stale because the process might have modified those pages.
1093         if (pheader->_space[m]._read_only) {
1094           jlong baseAddress = (jlong) (uintptr_t) pheader->_space[m]._addr._base;
1095           size_t usedSize = pheader->_space[m]._used;
1096           if (address >= baseAddress && address < (baseAddress + usedSize)) {
1097             // the given address falls in this shared heap area
1098             print_debug("found shared map at 0x%lx\n", (long) baseAddress);
1099 
1100 
1101             // If more data is asked than actually mapped from file, we need to zero fill
1102             // till the end-of-page boundary. But, java array new does that for us. we just
1103             // need to read as much as data available.
1104 
1105 #define MIN2(x, y) (((x) < (y))? (x) : (y))
1106 
1107             jlong diff = address - baseAddress;
1108             jlong bytesToRead = MIN2(numBytes, usedSize - diff);
1109             off_t offset = pheader->_space[m]._file_offset  + off_t(diff);
1110             ssize_t bytesRead = pread(classes_jsa_fd, bufPtr, bytesToRead, offset);
1111             if (bytesRead != bytesToRead) {
1112               env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT);
1113               print_debug("shared map read failed\n");
1114               return jbyteArray(0);


< prev index next >