/*
* 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 <ctype.h>
#include "util.h"
#include "commonRef.h"
#include "debugDispatch.h"
#include "eventHandler.h"
#include "eventHelper.h"
#include "threadControl.h"
#include "stepControl.h"
#include "transport.h"
#include "classTrack.h"
#include "debugLoop.h"
#include "bag.h"
#include "invoker.h"
#include "sys.h"
/* How the options get to OnLoad: */
/* Debug version defaults */
#ifdef DEBUG
#else
#endif
/*
* Options set through the OnLoad options string. All of these values
* are set once at VM startup and never reset.
*/
/*
* Elements of the transports bag
*/
typedef struct TransportSpec {
char *name;
char *address;
long timeout;
/*
* Forward Refs
*/
/*
* Phase 1: Initial load.
*
* OnLoad is called by the VM immediately after the back-end
* library is loaded. We can do very little in this function since
* the VM has not completed initialization. So, we parse the JDWP
* options and set up a simple initial event callbacks for JVMTI events.
* When a triggering event occurs, that callback will begin debugger initialization.
*/
/* Get a static area to hold the Global Data */
static BackendGlobalData *
get_gdata(void)
{
static BackendGlobalData s;
(void)memset(&s, 0, sizeof(BackendGlobalData));
return &s;
}
static jvmtiError
{
if (error != JVMTI_ERROR_NONE) {
ERROR_MESSAGE(("JDWP unable to configure initial JVMTI event %s: %s(%d)",
}
return error;
}
typedef struct {
int major;
int minor;
} version_type;
typedef struct {
/*
* List of explicitly compatible JVMTI versions, specified as
* { runtime version, compile-time version } pairs. -1 is a wildcard.
*/
/*
* FIXUP: Allow version 0 to be compatible with anything
* Special check for FCS of 1.0.
*/
{ { 0, -1 }, { -1, -1 } },
{ { -1, -1 }, { 0, -1 } },
/*
* 1.2 is runtime compatible with 1.1 -- just make sure to check the
* version before using any new 1.2 features
*/
{ { 1, 1 }, { 1, 2 } }
};
/* Logic to determine JVMTI version compatibility */
static jboolean
{
/*
* First check to see if versions are explicitly compatible via the
* list specified above.
*/
int i;
for (i = 0; i < nof_compatible_versions; ++i) {
return JNI_TRUE;
}
}
return major_runtime == major_compiletime &&
}
/* OnLoad startup:
* Returning JNI_ERR will cause the java_g VM to core dump, be careful.
*/
{
/* See if it's already loaded */
ERROR_MESSAGE(("Cannot load this JVM TI agent twice, check your java command line for duplicate jdwp options."));
return JNI_ERR;
}
/* If gdata is defined and the VM died, why are we here? */
ERROR_MESSAGE(("JDWP unable to load, VM died"));
return JNI_ERR;
}
/* Get global data area */
ERROR_MESSAGE(("JDWP unable to allocate memory"));
return JNI_ERR;
}
/* Start filling in gdata */
/* Get the JVMTI Env, IMPORTANT: Do this first! For jvmtiAllocate(). */
ERROR_MESSAGE(("JDWP unable to access JVMTI Version 1 (0x%x),"
" is your J2SE a 1.5 or newer version?"
" JNIEnv's GetEnv() returned %d",
JVMTI_VERSION_1, error));
}
/* Check to make sure the version of jvmti.h we compiled with
* matches the runtime version we are using.
*/
/* Check for compatibility */
ERROR_MESSAGE(("This jdwp native library will not work with this VM's "
"version of JVMTI (%d.%d.%d), it needs JVMTI %d.%d[.%d].",
/* Do not let VM get a fatal error, we don't want a core dump here. */
}
&boot_path);
/* Npt and Utf function init */
ERROR_MESSAGE(("JDWP: unable to initialize NPT library"));
return JNI_ERR;
}
ERROR_MESSAGE(("JDWP: UTF function initialization failed"));
return JNI_ERR;
}
/* Parse input options */
if (!parseOptions(options)) {
/* No message necessary, should have been printed out already */
/* Do not let VM get a fatal error, we don't want a core dump here. */
}
/* Get potential capabilities */
if (error != JVMTI_ERROR_NONE) {
ERROR_MESSAGE(("JDWP unable to get potential JVMTI capabilities: %s(%d)",
return JNI_ERR;
}
/* Fill in ones that we must have */
/* And what potential ones that would be nice to have */
{
}
/* Add the capabilities */
if (error != JVMTI_ERROR_NONE) {
ERROR_MESSAGE(("JDWP unable to get necessary JVMTI capabilities."));
}
/* Initialize event number mapping tables */
/* Set the initial JVMTI event notifications */
if (error != JVMTI_ERROR_NONE) {
return JNI_ERR;
}
if (error != JVMTI_ERROR_NONE) {
return JNI_ERR;
}
if (error != JVMTI_ERROR_NONE) {
return JNI_ERR;
}
}
/* Set callbacks just for 3 functions */
if (error != JVMTI_ERROR_NONE) {
ERROR_MESSAGE(("JDWP unable to set JVMTI event callbacks: %s(%d)",
return JNI_ERR;
}
LOG_MISC(("OnLoad: DONE"));
return JNI_OK;
}
{
/* Cleanup, but make sure VM is alive before using JNI, and
* make sure JVMTI environment is ok before deallocating
* memory allocated through JVMTI, which all of it is.
*/
/*
* Close transport before exit
*/
if (transport_is_open()) {
}
}
/*
* Phase 2: Initial events. Phase 2 consists of waiting for the
* event that triggers full initialization. Under normal circumstances
* (initOnStartup == TRUE) this is the JVMTI_EVENT_VM_INIT event.
* Otherwise, we delay initialization until the app throws a
* particular exception. The triggering event invokes
* the bulk of the initialization, including creation of threads and
* monitors, transport setup, and installation of a new event callback which
* handles the complete set of events.
*
* Since the triggering event comes in on an application thread, some of the
* initialization is difficult to do here. Specifically, this thread along
* with all other app threads may need to be suspended until a debugger
* connects. These kinds of tasks are left to the third phase which is
* invoked by one of the spawned debugger threads, the event handler.
*/
/*
* Wait for a triggering event; then kick off debugger
* initialization. A different event callback will be installed by
* debugger initialization, and this function will not be called
* again.
*/
/*
* TO DO: Decide whether we need to protect this code with
* a lock. It might be too early to create a monitor safely (?).
*/
static void JNICALL
{
LOG_CB(("cbEarlyVMInit"));
}
if (initOnStartup)
LOG_MISC(("END cbEarlyVMInit"));
}
static void
{
if ( error == JVMTI_ERROR_MUST_POSSESS_CAPABILITY )
/* What should error return say? */
if (error != JVMTI_ERROR_NONE) {
ERROR_MESSAGE(("JDWP unable to dispose of JVMTI environment: %s(%d)",
}
}
static void JNICALL
{
LOG_CB(("cbEarlyVMDeath"));
}
LOG_MISC(("END cbEarlyVMDeath"));
}
static void JNICALL
{
}
if (!vmInitialized) {
LOG_MISC(("VM is not initialized yet"));
return;
}
/*
* We want to preserve any current exception that might get wiped
* out during event handling (e.g. JNI calls). We have to rely on
* space for the local reference on the current frame because
* doing a PushLocalFrame here might itself generate an exception.
*/
LOG_MISC(("Initializing on uncaught exception"));
} else if (initOnException != NULL) {
/* Get class of exception thrown */
/* initing on throw, check */
LOG_MISC(("Checking specific exception: looking for %s, got %s",
if ( (error==JVMTI_ERROR_NONE) &&
LOG_MISC(("Initializing on specific exception"));
} else {
}
}
} else {
}
/* If initialize didn't happen, we need to restore things */
if ( error != JVMTI_ERROR_NONE ) {
/*
* Restore exception state from before callback call
*/
LOG_MISC(("No initialization, didn't find right exception"));
if (currentException != NULL) {
} else {
}
}
}
LOG_MISC(("END cbEarlyException"));
}
typedef struct EnumerateArg {
} EnumerateArg;
static jboolean
{
LOG_MISC(("Begin startTransport"));
ERROR_MESSAGE(("JDWP Transport %s failed to initialize, %s(%d)",
} else {
/* (Don't overwrite any previous error) */
enumArg->startCount++;
}
LOG_MISC(("End startTransport"));
return JNI_TRUE; /* Always continue, even if there was an error */
}
static void
signalInitComplete(void)
{
/*
* Initialization is complete
*/
LOG_MISC(("signal initialization complete"));
}
/*
* Determine if initialization is complete.
*/
debugInit_isInitComplete(void)
{
return initComplete;
}
/*
* Wait for all initialization to complete.
*/
void
{
while (!initComplete) {
}
}
/* All process exit() calls come from here */
void
{
/* make sure the transport is closed down before we exit() */
}
/* All JVM fatal error exits lead here (e.g. we need to kill the VM). */
static void
{
msg = "UNKNOWN REASON";
}
}
if ( error != JVMTI_ERROR_NONE ) {
} else {
}
} else {
/* Should rarely ever reach here, means VM is really dead */
"Can't call JNI FatalError(NULL, \"%s\")", buf);
}
}
/*
* Initialize debugger back end modules
*/
static void
{
LOG_MISC(("Begin initialize()"));
currentSessionID = 0;
}
/* Turn off the initial JVMTI event notifications */
if (error != JVMTI_ERROR_NONE) {
}
if (error != JVMTI_ERROR_NONE) {
}
if (error != JVMTI_ERROR_NONE) {
}
/* Remove initial event callbacks */
if (error != JVMTI_ERROR_NONE) {
}
/*
* Initialize transports
*/
arg.startCount = 0;
/*
* Exit with an error only if
* 1) none of the transports was successfully started, and
* 2) the application has not yet started running
*/
(arg.startCount == 0) &&
}
if (triggering_ei == EI_VM_INIT) {
LOG_MISC(("triggering_ei == EI_VM_INIT"));
} else {
/*
* TO DO: Kludgy way of getting the triggering event to the
* just-attached debugger. It would be nice to make this a little
* cleaner. There is also a race condition where other events
* can get in the queue (from other not-yet-suspended threads)
* before this one does. (Also need to handle allocation error below?)
*/
LOG_MISC(("triggering_ei != EI_VM_INIT"));
}
}
LOG_MISC(("End initialize()"));
}
/*
* Restore all static data to the initialized state so that another
* debugger can connect properly later.
*/
void
{
LOG_MISC(("debugInit_reset() beginning"));
util_reset();
/*
* If this is a server, we are now ready to accept another connection.
* If it's a client, then we've cleaned up some (more should be added
* later) and we're done.
*/
if (isServer) {
arg.startCount = 0;
} else {
signalInitComplete(); /* Why? */
}
LOG_MISC(("debugInit_reset() completed."));
}
char *
debugInit_launchOnInit(void)
{
return launchOnInit;
}
debugInit_suspendOnInit(void)
{
return suspendOnInit;
}
/*
* code below is shamelessly swiped from hprof.
*/
static int
{
int i;
char *p = *src;
for (i = 0; i < buflen; i++) {
if (p[i] == 0 || p[i] == sep) {
buf[i] = 0;
if (p[i] == sep) {
i++;
}
*src += i;
return i;
}
buf[i] = p[i];
}
/* overflow */
return 0;
}
static void
printUsage(void)
{
" Java Debugger JDWP Agent Library\n"
" --------------------------------\n"
"\n"
" (see http://java.sun.com/products/jpda for more information)\n"
"\n"
"\n"
"Option Name and Value Description Default\n"
"--------------------- ----------- -------\n"
"suspend=y|n wait on startup? y\n"
"transport=<name> transport spec none\n"
"server=y|n listen for debugger? n\n"
"launch=<command line> run debugger on event none\n"
"onthrow=<exception name> debug on throw none\n"
"onuncaught=y|n debug on any uncaught? n\n"
"mutf8=y|n output modified utf-8 n\n"
"quiet=y|n control over terminal messages n\n"
"\n"
"Obsolete Options\n"
"----------------\n"
"strict=y|n\n"
"stdalloc=y|n\n"
"\n"
"Examples\n"
"--------\n"
" - Using sockets connect to a debugger at a specific address:\n"
" - Using sockets listen for a debugger to attach:\n"
"\n"
"Notes\n"
"-----\n"
" - A timeout value of 0 (the default) is no timeout.\n"
"\n"
"Warnings\n"
"--------\n"
" a future release, for example:\n"
));
#ifdef DEBUG
"\n"
"Debugging Options Description Default\n"
"----------------- ----------- -------\n"
"pause=y|n pause to debug PID n\n"
"coredump=y|n coredump at exit n\n"
"errorexit=y|n exit on any error n\n"
"logfile=filename name of log file none\n"
"logflags=flags log flags (bitmask) none\n"
" JVM calls = 0x001\n"
" JNI calls = 0x002\n"
" JVMTI calls = 0x004\n"
" misc events = 0x008\n"
" step logs = 0x010\n"
" locations = 0x020\n"
" callbacks = 0x040\n"
" errors = 0x080\n"
" everything = 0xfff\n"
"debugflags=flags debug flags (bitmask) none\n"
" USE_ITERATE_THROUGH_HEAP 0x01\n"
"\n"
"Environment Variables\n"
"---------------------\n"
"_JAVA_JDWP_OPTIONS\n"
" Options can be added externally via this environment variable.\n"
" Anything contained in it will get a comma prepended to it (if needed),\n"
" then it will be added to the end of the options supplied via the\n"
));
#endif
}
{
ERROR_MESSAGE(("JDWP Non-server transport %s must have a connection "
"address specified through the 'address=' option",
return JNI_FALSE;
} else {
return JNI_TRUE;
}
}
static char *
{
char *combinedOptions;
/*
* Allocate enough space for both strings and
* comma in between.
*/
if (combinedOptions == NULL) {
return NULL;
}
return combinedOptions;
}
static jboolean
{
/*LINTED*/
return JNI_TRUE;
return JNI_TRUE;
}
}
return JNI_FALSE;
}
/* atexit() callback */
static void
atexit_finish_logging(void)
{
/* Normal exit(0) (not _exit()) may only reach here */
finish_logging(0); /* Only first call matters */
}
static jboolean
{
char *end;
char *current;
int length;
char *str;
char *errmsg;
/* Set defaults */
/* Options being NULL will end up being an error. */
options = "";
}
/* Check for "help" BEFORE we add any environmental settings */
printUsage();
forceExit(0); /* Kill entire process, no core dump wanted */
}
/* These buffers are never freed */
{
char *envOptions;
/*
* Add environmentally specified options.
*/
if (envOptions != NULL) {
}
}
/*
* Allocate a buffer for names derived from option strings. It should
* never be longer than the original options string itself.
* Also keep a copy of the options in gdata->options.
*/
}
}
if (transports == NULL) {
}
}
while (*str) {
/*LINTED*/
goto syntax_error;
}
/*LINTED*/
goto syntax_error;
}
if (currentTransport == NULL) {
errmsg = "address specified without transport";
goto bad_option_with_errmsg;
}
/*LINTED*/
goto syntax_error;
}
if (currentTransport == NULL) {
errmsg = "timeout specified without transport";
goto bad_option_with_errmsg;
}
/*LINTED*/
goto syntax_error;
}
/*LINTED*/
goto syntax_error;
}
/* Read class name and convert in place to a signature */
*current = 'L';
/*LINTED*/
goto syntax_error;
}
while (*current != '\0') {
if (*current == '.') {
*current = '/';
}
current++;
}
*current++ = ';';
*current++ = '\0';
/*LINTED*/
goto syntax_error;
}
} else {
goto syntax_error;
}
goto syntax_error;
}
if ( dopause ) {
do_pause();
}
goto syntax_error;
}
goto syntax_error;
}
errmsg = "The exitpause option removed, use -XX:OnError";
goto bad_option_with_errmsg;
errmsg = "The precrash option removed, use -XX:OnError";
goto bad_option_with_errmsg;
/*LINTED*/
goto syntax_error;
}
/*LINTED*/
goto syntax_error;
}
/*LINTED*/
/*LINTED*/
goto syntax_error;
}
/*LINTED*/
goto syntax_error;
}
goto syntax_error;
}
goto syntax_error;
}
goto syntax_error;
}
goto syntax_error;
}
goto syntax_error;
}
goto syntax_error;
}
} else {
goto syntax_error;
}
}
/* Setup logging now */
(void)atexit(&atexit_finish_logging);
}
if (bagSize(transports) == 0) {
errmsg = "no transport specified";
goto bad_option_with_errmsg;
}
/*
* TO DO: Remove when multiple transports are allowed. (replace with
* check below.
*/
errmsg = "multiple transports are not supported in this release";
goto bad_option_with_errmsg;
}
if (!isServer) {
if (!specified) {
/* message already printed */
goto bad_option_no_msg;
}
}
/*
* The user has selected to wait for an exception before init happens
*/
if (launchOnInit == NULL) {
/*
* suboption, so it is an error if user did not
* provide one.
*/
errmsg = "Specify launch=<command line> when using onthrow or onuncaught suboption";
goto bad_option_with_errmsg;
}
}
return JNI_TRUE;
return JNI_FALSE;
return JNI_FALSE;
return JNI_FALSE;
}
/* All normal exit doors lead here */
void
{
int exit_code = 0;
/* Pick an error code */
if ( error != JVMTI_ERROR_NONE ) {
exit_code = 1;
if ( docoredump ) {
abort();
}
}
msg = "";
}
/* Let's try and cleanup the JVMTI, if we even have one */
/* Dispose of jvmti (gdata->jvmti becomes NULL) */
}
/* Finish up logging. We reach here if JDWP is doing the exiting. */
/* Let's give the JNI a FatalError if non-exit 0, which is historic way */
if ( exit_code != 0 ) {
}
/* Last chance to die, this kills the entire process. */
}