/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include <jni.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <elf.h>
#include <link.h>
#include "libproc_impl.h"
#include "salibelf.h"
// This file has the libproc implementation to read core files.
// For live processes, refer to ps_proc.c. Portions of this is adapted
//----------------------------------------------------------------------
// ps_prochandle cleanup helper functions
// close all file descriptors
// close core file descriptor
// close exec file descriptor
// close interp file descriptor
// close class share archive file
// close all library file descriptors
while (lib) {
}
}
// clean all map_info stuff
while (map) {
}
}
// Part of the class sharing workaround
while (map) {
}
}
// ps_prochandle operations
}
}
print_debug("can't allocate memory for map_info\n");
return NULL;
}
// initialize map
return map;
}
// add map info with given fd, offset, vaddr and memsz
return NULL;
}
// add this to map list
return map;
}
// Part of the class sharing workaround
return NULL;
}
}
// Return the map_info for the given virtual address. We keep a sorted
// array of pointers in ph->map_array, so we can binary search.
{
else
}
else
return (mp);
// Part of the class sharing workaround
// Unfortunately, we have no way of detecting -Xshare state.
// Check out the share maps atlast, if we don't find anywhere.
// This is done this way so to avoid reading share pages
// ahead of other normal maps. For eg. with -Xshare:off we don't
// want to prefer class sharing data to data from core.
if (mp) {
print_debug("can't locate map_info at 0x%lx, trying class share maps\n",
addr);
}
while (mp) {
print_debug("located map_info at 0x%lx from class share maps\n",
addr);
return (mp);
}
}
return (NULL);
}
//---------------------------------------------------------------
// Part of the class sharing workaround:
//
// With class sharing, pages are mapped from classes[_g].jsa file.
// The read-only class sharing pages are mapped as MAP_SHARED,
// PROT_READ pages. These pages are not dumped into core dump.
// With this workaround, these pages are read from classes[_g].jsa.
// FIXME: !HACK ALERT!
// The format of sharing achive file header is needed to read shared heap
// file mappings. For now, I am hard coding portion of FileMapHeader here.
// Refer to filemap.hpp.
// FileMapHeader describes the shared space data in the file to be
// mapped. This structure gets written to a file. It is not a class,
// so that the compilers don't add any compiler-private data to it.
// Refer to CompactingPermGenGen::n_regions in compactingPermGenGen.hpp
// Refer to FileMapInfo::_current_version in filemap.hpp
struct FileMapHeader {
struct space_info {
// 4991491 NOTICE These are C++ bool's in filemap.hpp and must match up with
// the C type matching the C++ bool type on any given platform. For
// Hotspot on Linux we assume the corresponding C type is char but
// licensees on Linux versions may need to adjust the type of these fields.
// Ignore the rest of the FileMapHeader. We don't need those fields here.
};
jboolean i;
*pvalue = i;
return true;
} else {
return false;
}
}
return true;
} else {
return false;
}
}
// used to read strings from debuggee
size_t i = 0;
char c = ' ';
while (c != '\0') {
return false;
if (i < size - 1)
buf[i] = c;
else // smaller buffer
return false;
i++; addr++;
}
buf[i] = '\0';
return true;
}
// mangled name of Arguments::SharedArchivePath
// we are iterating over shared objects from the core dump. look for
// libjvm[_g].so.
const char *jvm_name = 0;
size_t n = 0;
if (useSharedSpacesAddr == 0) {
print_debug("can't lookup 'UseSharedSpaces' flag\n");
return false;
}
// Hotspot vm types are not exported to build this library. So
// using equivalent type jboolean to read the value of
// UseSharedSpaces which is same as hotspot type "bool".
print_debug("can't read the value of 'UseSharedSpaces' flag\n");
return false;
}
if ((int)useSharedSpaces == 0) {
print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
return true;
}
if (sharedArchivePathAddrAddr == 0) {
print_debug("can't lookup shared archive path symbol\n");
return false;
}
print_debug("can't read shared archive path pointer\n");
return false;
}
print_debug("can't read shared archive path value\n");
return false;
}
// open the class sharing archive file
if (fd < 0) {
return false;
} else {
}
// read FileMapHeader from the file
!= sizeof(struct FileMapHeader)) {
return false;
}
// check file magic
print_debug("%s has bad shared archive file magic number 0x%x, expecing 0xf00baba2\n",
return false;
}
// check version
print_debug("%s has wrong shared archive file version %d, expecting %d\n",
return false;
}
// add read-only maps from classes[_g].jsa to the list of maps
for (m = 0; m < NUM_SHARED_MAPS; m++) {
// no need to worry about the fractional pages at-the-end.
// possible fractional pages are handled by core_read_data.
}
}
return true;
}
}
return true;
}
//---------------------------------------------------------------------------
// functions to handle map_info
// Order mappings based on virtual address. We use this function as the
// callback for sorting the array of map_info pointers.
{
return (0);
}
// we sort map_info by starting virtual address so that we can do
// binary search to read from an address.
int i = 0;
// allocate map_array
print_debug("can't allocate memory for map array\n");
return false;
}
// add maps to array
while (map) {
i++;
}
// sort is called twice. If this is second time, clear map array
// sort the map_info array by base virtual address.
// print map
if (is_debug()) {
int j = 0;
print_debug("---- sorted virtual address map ----\n");
}
}
return true;
}
#ifndef MIN
#define MIN(x, y) (((x) < (y))? (x): (y))
#endif
while (resid != 0) {
int fd;
break; /* No mapping for this address */
break;
// mappings always start at page boundary. But, may end in fractional
// page. fill zeros for possible fractional page at the end of a mapping.
if (rem > 0) {
// we are not assuming 'buf' to be zero initialized.
}
}
if (resid) {
print_debug("core read failed for %d byte(s) @ 0x%lx (%d more bytes)\n",
return false;
} else {
return true;
}
}
// null implementation for write
return false;
}
struct user_regs_struct* regs) {
// for core we have cached the lwp regs from NOTE section
while (thr) {
return true;
}
}
return false;
}
};
// read regs and create thread from NT_PRSTATUS entries from core file
// we have to read prstatus_t from buf
// assert(nbytes == sizeof(prstaus_t), "size mismatch on prstatus_t");
// we set pthread_t to -1 for core dump
return false;
// copy regs
if (is_debug()) {
print_debug("integer regset\n");
#ifdef i386
// print the regset
#endif
// print the regset
#endif
}
return true;
}
// read NT_PRSTATUS entries from core NOTE segment
char* p = NULL;
// we are interested in just prstatus entries. we will ignore the rest.
// Advance the seek pointer to the start of the PT_NOTE data
print_debug("failed to lseek to PT_NOTE data\n");
return false;
}
// Now process the PT_NOTE structures. Each one is preceded by
// an Elf{32/64}_Nhdr structure describing its type and size.
print_debug("can't allocate memory for reading core notes\n");
goto err;
}
// read notes into buffer
print_debug("failed to read notes, core file must have been truncated\n");
goto err;
}
p = buf;
print_debug("Note header with n_type = %d and n_descsz = %u\n",
return false;
}
}
return true;
err:
return false;
}
// read all segments from core file
int i = 0;
return false;
/*
* Now iterate through the program headers in the core file.
* We're interested in two types of Phdrs: PT_NOTE (which
* contains a set of saved /proc structures), and PT_LOAD (which
* represents a memory mapping from the process's address space).
*
* Difference b/w Solaris PT_NOTE and Linux PT_NOTE:
*
* In Solaris there are two PT_NOTE segments the first PT_NOTE (if present)
* contains /proc structs in the pre-2.6 unstructured /proc format. the last
* PT_NOTE has data in new /proc format.
*
* In Solaris, there is only one pstatus (process status). pstatus contains
* integer register set among other stuff. For each LWP, we have one lwpstatus
* entry that has integer regset for that LWP.
*
* Linux threads are actually 'clone'd processes. To support core analysis
* of "multithreaded" process, Linux creates more than one pstatus (called
* "prstatus") entry in PT_NOTE. Each prstatus entry has integer regset for one
* "thread". Please refer to Linux kernel src file 'fs/binfmt_elf.c', in particular
* function "elf_core_dump".
*/
case PT_NOTE:
break;
case PT_LOAD: {
}
break;
}
}
core_php++;
}
return true;
err:
return false;
}
// read segments of a shared object
static bool read_lib_segments(struct ps_prochandle* ph, int lib_fd, ELF_EHDR* lib_ehdr, uintptr_t lib_base) {
int i = 0;
return false;
// we want to process only PT_LOAD segments that are not writable.
// have been already added from core file segments.
if (add_map_info(ph, lib_fd, lib_php->p_offset, lib_php->p_vaddr + lib_base, lib_php->p_filesz) == NULL)
goto err;
}
lib_php++;
}
return true;
err:
return false;
}
// process segments from interpreter (ld.so or ld-linux.so)
print_debug("interpreter is not a valid ELF file\n");
return false;
}
print_debug("can't read segments of interpreter\n");
return false;
}
return true;
}
// process segments of a a.out
int i = 0;
return false;
// add mappings for PT_LOAD segments
case PT_LOAD: {
// add only non-writable segments of non-zero filesz
if (add_map_info(ph, ph->core->exec_fd, exec_php->p_offset, exec_php->p_vaddr, exec_php->p_filesz) == NULL) goto err;
}
break;
}
// read the interpreter and it's segments
case PT_INTERP: {
// read interpreter segments as well
print_debug("can't open runtime loader\n");
goto err;
}
break;
}
// from PT_DYNAMIC we want to read address of first link_map addr
case PT_DYNAMIC: {
break;
}
} // switch
exec_php++;
} // for
return true;
err:
return false;
}
// read shared library info from runtime linker's data structures.
// This work is done by librtlb_db in Solaris
int lib_fd;
// _DYNAMIC has information of the form
// [tag] [data] [tag] [data] .....
// Both tag and data are pointer sized.
// We look for dynamic info with DT_DEBUG. This has shared object info.
// refer to struct r_debug in link.h
print_debug("can't read debug info from _DYNAMIC\n");
return false;
}
}
// we have got Dyn entry with DT_DEBUG
// at debug_base we have struct r_debug. This has first link map in r_map field
print_debug("can't read first link map address\n");
return false;
}
// read ld_base address from struct r_debug
print_debug("can't read ld base address\n");
return false;
}
// now read segments from interp (i.e ld.so or ld-linux.so)
if (read_interp_segments(ph) != true)
return false;
// after adding interpreter (ld.so) mappings sort again
if (sort_map_array(ph) != true)
return false;
while (link_map_addr != 0) {
// link_map->l_addr as "base address", this is * not * really base virtual
// address of the shared object. This is actually the difference b/w the virtual
// address mentioned in shared object and the actual virtual base where runtime
// linker loaded it. We use "base diff" in read_lib_segments call below.
print_debug("can't read shared object base address diff\n");
return false;
}
// read address of the name
print_debug("can't read address of shared object name\n");
return false;
}
// read name of the shared object
lib_name[0] = '\0';
if (lib_name_addr != 0 &&
print_debug("can't read shared object name\n");
// don't let failure to read the name stop opening the file. If something is really wrong
// it will fail later.
}
if (lib_name[0] != '\0') {
// ignore empty lib names
if (lib_fd < 0) {
// continue with other libraries...
} else {
print_debug("reading library %s @ 0x%lx [ 0x%lx ]\n",
// while adding library mappings we need to use "base difference".
print_debug("can't read shared object's segments\n");
return false;
}
// Map info is added for the library (lib_name) so
// we need to re-sort it before calling the p_pdread.
if (sort_map_array(ph) != true)
return false;
} else {
// continue with other libraries...
}
}
}
// read next link_map address
print_debug("can't read next link in link_map\n");
return false;
}
}
return true;
}
// the one and only one exposed stuff from this file
print_debug("can't allocate ps_prochandle\n");
return NULL;
}
print_debug("can't allocate ps_prochandle\n");
return NULL;
}
// initialize ph
// open the core file
print_debug("can't open core file\n");
goto err;
}
// read core file ELF header
print_debug("core file is not a valid ELF ET_CORE file\n");
goto err;
}
print_debug("can't open executable file\n");
goto err;
}
print_debug("executable file is not a valid ELF ET_EXEC file\n");
goto err;
}
// process core file segments
goto err;
// process exec file segments
goto err;
// exec file is also treated like a shared object for symbol search
goto err;
// allocate and sort maps into map_array, we need to do this
// here because read_shared_lib_info needs to read from debuggee
// address space
if (sort_map_array(ph) != true)
goto err;
if (read_shared_lib_info(ph) != true)
goto err;
// sort again because we have added more mappings from shared objects
if (sort_map_array(ph) != true)
goto err;
if (init_classsharing_workaround(ph) != true)
goto err;
return ph;
err:
return NULL;
}