/*
* 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 "salibproc.h"
#ifndef SOLARIS_11_B159_OR_LATER
#endif
#include <thread_db.h>
#include <strings.h>
#include <limits.h>
#include <demangle.h>
#include <stdarg.h>
#include <stdlib.h>
#include <errno.h>
#define THROW_NEW_DEBUGGER_EXCEPTION_(str, value) { throwNewDebuggerException(env, str); return value; }
// debug modes
static int _libsaproc_debug = 0;
#ifndef SOLARIS_11_B159_OR_LATER
static bool _Pstack_iter_debug = false;
if (_Pstack_iter_debug) {
}
}
#endif // !SOLARIS_11_B159_OR_LATER
if (_libsaproc_debug) {
}
}
struct Debugger {
};
};
};
/*
* Portions of user thread level detail gathering code is from pstack source
* code. See pstack.c in Solaris 2.8 user commands source code.
*/
}
// JNI ids for some fields, methods
// libproc handler pointer
// libthread.so dlopen handle, thread agent ptr and function pointers
// reg index fields
// part of the class sharing workaround
// method ids
/*
* Functions we need from libthread_db
*/
typedef td_err_e
(*p_td_init_t)(void);
typedef td_err_e
typedef td_err_e
typedef td_err_e
td_thr_state_e, int, sigset_t *, unsigned);
typedef td_err_e
typedef td_err_e
typedef td_err_e
static void
// release libthread_db agent, if we had created
if (p_td_thragent_t != 0 && p_td_ta_delete != 0) {
}
// dlclose libthread_db.so
if (libthread_db_handle != 0) {
}
}
// clear libthread_db stuff
// release ptr to ps_prochandle
if (p_ps_prochandle != 0L) {
}
// part of the class sharing workaround
if (classes_jsa_fd != -1) {
struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID);
}
}
}
// Is it okay to ignore libthread_db failure? Set env var to ignore
// libthread_db failure. You can still debug, but will miss threads
// related functionality.
if (sa_ignore_threaddb) { \
return; \
} else { \
}
if (sa_ignore_threaddb) { \
return ret; \
} else { \
}
static void init_alt_root() {
if (alt_root_len == -1) {
if (alt_root)
else
alt_root_len = 0;
}
}
// This function is a complete substitute for the open system call
// since it's also used to override open calls from libproc to
// implement as a pathmap style facility for the SA. If libproc
// starts using other interfaces then this might have to extended to
// cover other calls.
if (_libsaproc_debug) {
}
if (alt_root_len > 0) {
if (fd >= 0) {
if (_libsaproc_debug) {
}
return fd;
}
if (fd >= 0) {
if (_libsaproc_debug) {
}
return fd;
}
}
}
}
{
}
}
if (_libsaproc_debug) {
}
if (alt_root_len > 0) {
if (_libsaproc_debug && handle) {
}
if (_libsaproc_debug && handle) {
}
}
}
}
if (_libsaproc_debug) {
}
return handle;
}
// libproc and libthread_db callback functions
extern "C" {
static int
return (0);
/*
* We found a libthread.
* dlopen() the matching libthread_db and get the thread agent handle.
*/
s2 += 3;
} else {
#ifdef _LP64
/*
* The victim process is 32-bit, we are 64-bit.
* We have to find the 64-bit version of libthread_db
* that matches the victim's 32-bit version of libthread.
*/
s2 += 3;
s2 += 3;
#else
return (0);
#endif /* _LP64 */
}
void* libthread_db_handle = 0;
}
void* tmpPtr = 0;
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_init!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_new!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_delete!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_thr_iter!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_get_info!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_ta_map_id2thr!", 0);
}
if (tmpPtr == 0) {
HANDLE_THREADDB_FAILURE_("dlsym failed on td_thr_getgregs!", 0);
}
return 1;
}
static int
p_td_thr_get_info_t p_td_thr_get_info = (p_td_thr_get_info_t) env->GetLongField(this_obj, p_td_thr_get_info_ID);
return (0);
jobject threadProxy = env->CallObjectMethod(this_obj, getThreadForThreadId_ID, (jlong)(uintptr_t) thrinfo.ti_tid);
CHECK_EXCEPTION_(1);
CHECK_EXCEPTION_(1);
return 0;
}
static int
if (obj_name) {
CHECK_EXCEPTION_(1);
CHECK_EXCEPTION_(1);
CHECK_EXCEPTION_(1);
}
return 0;
}
// Pstack_iter() proc_stack_f callback prior to Nevada-B159
static int
CHECK_EXCEPTION_(1);
}
return 0;
}
// Pstack_iter() proc_stack_f callback in Nevada-B159 or later
/*ARGSUSED*/
static int
}
// part of the class sharing workaround
// 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 {
// Ignore the rest of the FileMapHeader. We don't need those fields here.
};
static bool
jboolean i;
*pvalue = i;
return true;
} else {
return false;
}
}
static bool
return true;
} else {
return false;
}
}
static bool
size_t i = 0;
while (ch != '\0') {
return false;
if (i < size - 1) {
} else { // smaller buffer
return false;
}
i++; addr++;
}
buf[i] = '\0';
return true;
}
// mangled symbol name for Arguments::SharedArchivePath
static int
const char* jvm_name = 0;
} else {
return 0;
}
// initialize classes[_g].jsa file descriptor field.
// check whether class sharing is on by reading variable "UseSharedSpaces"
if (useSharedSpacesAddr == 0) {
}
// read the value of the flag "UseSharedSpaces"
// Since hotspot types are not available to build this library. So
// equivalent type "jboolean" is used to read the value of "UseSharedSpaces"
// which is same as hotspot type "bool".
} else if ((int)value == 0) {
print_debug("UseSharedSpaces is false, assuming -Xshare:off!\n");
return 1;
}
if (sharedArchivePathAddrAddr == 0) {
print_debug("can't find symbol 'Arguments::SharedArchivePath'\n");
}
print_debug("can't find read pointer 'Arguments::SharedArchivePath'\n");
}
print_debug("can't find read 'Arguments::SharedArchivePath' value\n");
}
// open the classes[_g].jsa
if (fd < 0) {
} else {
}
// parse classes[_g].jsa
}
// read FileMapHeader
if (n != sizeof(struct FileMapHeader)) {
}
// check file magic
}
// check version
}
if (_libsaproc_debug) {
for (int m = 0; m < NUM_SHARED_MAPS; m++) {
print_debug("shared file offset %d mapped at 0x%lx, size = %ld, read only? = %d\n",
}
}
// FIXME: For now, omitting other checks such as VM version etc.
// store class archive file fd and map header in debugger object fields
return 1;
}
} // extern "C"
// error messages for proc_arg_grab failure codes. The messages are
// modified versions of comments against corresponding #defines in
// libproc.h.
static const char* proc_arg_grab_errmsgs[] = {
"",
/* G_NOPROC */ "No such process",
/* G_NOCORE */ "No such core file",
/* G_NOPROCORCORE */ "No such process or core",
/* G_NOEXEC */ "Cannot locate executable file",
/* G_ZOMB */ "Zombie processs",
/* G_PERM */ "No permission to attach",
/* G_BUSY */ "Another process has already attached",
/* G_SYS */ "System process - can not attach",
/* G_SELF */ "Process is self - can't debug myself!",
/* G_INTR */ "Interrupt received while grabbing",
/* G_LP64 */ "debuggee is 64 bit, use java -d64 for debugger",
/* G_FORMAT */ "File is not an ELF format core file - corrupted core?",
/* G_ELF */ "Libelf error while parsing an ELF file",
/* G_NOTE */ "Required PT_NOTE Phdr not present - corrupted core?",
};
int gcode;
// some older versions of libproc.so crash when trying to attach 32 bit
// debugger to 64 bit core file. check and throw error.
#ifndef _LP64
if (errno) {
// core file
int core_fd;
THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 64 bit, use java -d64 for debugger");
}
}
// all other conditions are handled by libproc.so.
}
#endif
struct ps_prochandle* ph = proc_arg_grab(cmdLine_cstr, (isProcess? PR_ARG_PIDS : PR_ARG_CORES), PGRAB_FORCE, &gcode);
if (! ph) {
} else {
perror("libsaproc DEBUG: ");
}
if (isProcess) {
THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to process!");
} else {
THROW_NEW_DEBUGGER_EXCEPTION("Not able to attach to core file!");
}
}
}
// even though libproc.so supports 64 bit debugger and 32 bit debuggee, we don't
// support such cross-bit-debugging. check for that combination and throw error.
#ifdef _LP64
int data_model;
THROW_NEW_DEBUGGER_EXCEPTION("can't determine debuggee data model (ILP32? or LP64?)");
}
if (data_model == PR_MODEL_ILP32) {
THROW_NEW_DEBUGGER_EXCEPTION("debuggee is 32 bit, use 32 bit java for debugger");
}
#endif
if (! isProcess) {
/*
* With class sharing, shared perm. gen heap is allocated in with MAP_SHARED|PROT_READ.
* These pages are mapped from the file "classes[_g].jsa". MAP_SHARED pages are not dumped
* in Solaris core.To read shared heap pages, we have to read classes[_g].jsa file.
*/
if (exception) {
env->ExceptionClear();
return;
}
}
/*
* Iterate over the process mappings looking
* for libthread and then dlopen the appropriate
* libthread_db and get function pointers.
*/
if (exception) {
env->ExceptionClear();
if (!sa_ignore_threaddb) {
}
return;
}
// init libthread_db and create thread_db agent
if (p_td_init == 0) {
if (!sa_ignore_threaddb) {
}
HANDLE_THREADDB_FAILURE("Did not find libthread in target process/core!");
}
if (!sa_ignore_threaddb) {
}
HANDLE_THREADDB_FAILURE("Can't initialize thread_db!");
}
if (!sa_ignore_threaddb) {
}
HANDLE_THREADDB_FAILURE("Can't create thread_db agent!");
}
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: attach0
* Description: process detach
*/
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: attach0
* Description: core file detach
*/
JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
// ignore executable file name, libproc.so can detect a.out name anyway.
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: detach0
* Signature: ()V
*/
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: getRemoteProcessAddressSize0
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getRemoteProcessAddressSize0
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: getPageSize0
* Signature: ()I
*/
/*
We are not yet attached to a java process or core file. getPageSize is called from
the constructor of ProcDebuggerLocal. The following won't work!
jlong p_ps_prochandle;
p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
CHECK_EXCEPTION_(-1);
struct ps_prochandle* prochandle = (struct ps_prochandle*) p_ps_prochandle;
return (Pstate(prochandle) == PS_DEAD) ? Pgetauxval(prochandle, AT_PAGESZ)
: getpagesize();
So even though core may have been generated with a different page size settings, for now
call getpagesize.
*/
return getpagesize();
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: getThreadIntegerRegisterSet0
* Signature: (J)[J
* Description: get gregset for a given thread specified by thread id
*/
JNIEXPORT jlongArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_getThreadIntegerRegisterSet0
// map the thread id to thread handle
p_td_ta_map_id2thr_t p_td_ta_map_id2thr = (p_td_ta_map_id2thr_t) env->GetLongField(this_obj, p_td_ta_map_id2thr_ID);
if (p_td_thragent_t == 0) {
return 0;
}
THROW_NEW_DEBUGGER_EXCEPTION_("can't map thread id to thread handle!", 0);
}
p_td_thr_getgregs_t p_td_thr_getgregs = (p_td_thr_getgregs_t) env->GetLongField(this_obj, p_td_thr_getgregs_ID);
CHECK_EXCEPTION_(0);
for (int i = 0; i < NPRGREG; i++) {
}
return res;
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: fillThreadList0
*/
if (p_td_thragent_t == 0) {
return;
}
p_td_ta_thr_iter_t p_td_ta_thr_iter = (p_td_ta_thr_iter_t) env->GetLongField(this_obj, p_td_ta_thr_iter_ID);
}
#ifndef SOLARIS_11_B159_OR_LATER
// building on Nevada-B158 or earlier so more hoops to jump through
#endif
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: fillCFrameList0
* Description: fills CFrame list for a given thread
*/
CHECK_EXCEPTION_(0);
for (int i = 0; i < NPRGREG; i++) {
}
CHECK_EXCEPTION_(0);
#ifdef SOLARIS_11_B159_OR_LATER
// building on Nevada-B159 or later so use the new callback
#else
// building on Nevada-B158 or earlier so figure out which callback to use
if (has_newer_Pstack_iter) {
// Since we're building on Nevada-B158 or earlier, we have to
// cast wrapper_fill_cframe_list to make the compiler happy.
} else {
}
#endif // SOLARIS_11_B159_OR_LATER
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: fillLoadObjectList0
*/
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: readBytesFromProcess0
* Signature: (JJ)[B
*/
JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0
CHECK_EXCEPTION_(0);
CHECK_EXCEPTION_(0);
// part of the class sharing workaround. try shared heap area
struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID);
// walk through the shared mappings -- we just have 4 of them.
// so, linear walking is okay.
for (int m = 0; m < NUM_SHARED_MAPS; m++) {
// We can skip the non-read-only maps. These are mapped as MAP_PRIVATE
// and hence will be read by libproc. Besides, the file copy may be
// stale because the process might have modified those pages.
// the given address falls in this shared heap area
// If more data is asked than actually mapped from file, we need to zero fill
// till the end-of-page boundary. But, java array new does that for us. we just
// need to read as much as data available.
#define MIN2(x, y) (((x) < (y))? (x) : (y))
if (bytesRead != bytesToRead) {
print_debug("shared map read failed\n");
return jbyteArray(0);
} else {
print_debug("shared map read succeeded\n");
return array;
}
} // is in current map
} // is read only map
} // for shared maps
} // classes_jsa_fd != -1
return jbyteArray(0);
} else {
return array;
}
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: writeBytesToProcess0
* Signature: (JJ[B)V
* Description: write bytes into debugger process
*/
THROW_NEW_DEBUGGER_EXCEPTION("Process write failed!");
}
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: suspend0
* Signature: ()V
*/
// for now don't check return value. revisit this again.
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: resume0
* Signature: ()V
*/
// for now don't check return value. revisit this again.
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: lookupByName0
* Description: symbol lookup by name
*/
if (objectName != NULL) {
CHECK_EXCEPTION_(0);
} else {
}
CHECK_EXCEPTION_(0);
if (symbol_addr == 0) {
}
if (objectName_cstr != PR_OBJ_EVERY) {
}
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: lookupByAddress0
* Description: lookup symbol name for a given address
*/
if (res != 0) { // failed
return 0;
}
CHECK_EXCEPTION_(0);
}
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: demangle0
*/
} else {
}
return res;
}
#ifndef SOLARIS_11_B159_OR_LATER
// Determine if the OS we're running on has the newer version
// of libproc's Pstack_iter.
//
// Set env var PSTACK_ITER_DEBUG=true to debug this logic.
// Set env var PSTACK_ITER_DEBUG_RELEASE to simulate a 'release' value.
// Set env var PSTACK_ITER_DEBUG_VERSION to simulate a 'version' value.
//
// frankenputer 'uname -r -v': 5.10 Generic_141445-09
// jurassic 'uname -r -v': 5.11 snv_164
// lonepeak 'uname -r -v': 5.11 snv_127
//
static bool done_set = false;
if (done_set) {
// already set has_newer_Pstack_iter
return;
}
THROW_NEW_DEBUGGER_EXCEPTION("uname() failed!");
}
if (_Pstack_iter_debug) {
}
}
}
// the major number corresponds to the old SunOS major number
if (major >= 6) {
dprintf_2("release is SunOS 6 or later\n");
has_newer_Pstack_iter = true;
done_set = true;
return;
}
if (major < 5) {
dprintf_2("release is SunOS 4 or earlier\n");
done_set = true;
return;
}
// some SunOS 5.* build so now check for Solaris versions
int minor = 0;
// release is major.minor format
}
if (minor <= 10) {
dprintf_2("release is Solaris 10 or earlier\n");
done_set = true;
return;
} else if (minor >= 12) {
dprintf_2("release is Solaris 12 or later\n");
has_newer_Pstack_iter = true;
done_set = true;
return;
}
// some Solaris 11 build so now check for internal build numbers
dprintf_2("release is Solaris 11 post-GA or later\n");
has_newer_Pstack_iter = true;
done_set = true;
return;
}
// version begins with "snv_" so a pre-GA build of Solaris 11
if (build >= 159) {
dprintf_2("release is Nevada-B159 or later\n");
has_newer_Pstack_iter = true;
} else {
dprintf_2("release is Nevada-B158 or earlier\n");
}
done_set = true;
}
#endif // !SOLARIS_11_B159_OR_LATER
/*
* Class: sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
* Method: initIDs
* Signature: ()V
* Description: get JNI ids for fields and methods of ProcDebuggerLocal class
*/
if (_libsaproc_debug) {
// propagate debug mode to libproc.so
}
if (libproc_handle == 0)
THROW_NEW_DEBUGGER_EXCEPTION("can't load libproc.so, if you are using Solaris 5.7 or below, copy libproc.so from 5.8!");
#ifndef SOLARIS_11_B159_OR_LATER
#endif
"getThreadForThreadId", "(J)Lsun/jvm/hotspot/debugger/ThreadProxy;");
"createSenderFrame", "(Lsun/jvm/hotspot/debugger/proc/ProcCFrame;JJ)Lsun/jvm/hotspot/debugger/proc/ProcCFrame;");
// part of the class sharing workaround
}