staticchar *launchOnInit = NULL; /* launch this app during init */ static jboolean suspendOnInit = JNI_TRUE; /* suspend all app threads after init */ static jboolean dopause = JNI_FALSE; /* pause for debugger attach */ static jboolean docoredump = JNI_FALSE; /* core dump on exit */ staticchar *logfile = NULL; /* Name of logfile (if logging) */ staticunsigned logflags = 0; /* Log flags */
staticchar *names; /* strings derived from OnLoad options */
static jboolean allowStartViaJcmd = JNI_FALSE; /* if true we allow the debugging to be started via a jcmd */ static jboolean startedViaJcmd = JNI_FALSE; /* if false, we have not yet started debugging via a jcmd */
/* 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;
}
/* See if it's already loaded */ if ( gdata!=NULL && gdata->isLoaded==JNI_TRUE ) {
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? */ if ( gdata!=NULL && gdata->vmDead ) {
ERROR_MESSAGE(("JDWP unable to load, VM died")); return JNI_ERR;
}
/* Get global data area */
gdata = get_gdata(); if (gdata == NULL) {
ERROR_MESSAGE(("JDWP unable to allocate memory")); return JNI_ERR;
}
gdata->isLoaded = JNI_TRUE;
/* Get the JVMTI Env, IMPORTANT: Do this first! For jvmtiAllocate(). */
error = JVM_FUNC_PTR(vm,GetEnv)
(vm, (void **)&(gdata->jvmti), JVMTI_VERSION_1); if (error != JNI_OK) {
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));
forceExit(1); /* Kill entire process, no core dump */
}
/* Check to make sure the version of jvmti.h we compiled with *matchestheruntimeversionweareusing.
*/
jvmtiCompileTimeMajorVersion = ( JVMTI_VERSION & JVMTI_VERSION_MASK_MAJOR )
>> JVMTI_VERSION_SHIFT_MAJOR;
jvmtiCompileTimeMinorVersion = ( JVMTI_VERSION & JVMTI_VERSION_MASK_MINOR )
>> JVMTI_VERSION_SHIFT_MINOR;
jvmtiCompileTimeMicroVersion = ( JVMTI_VERSION & JVMTI_VERSION_MASK_MICRO )
>> JVMTI_VERSION_SHIFT_MICRO;
/* Check for compatibility */ if ( !compatible_versions(jvmtiMajorVersion(), jvmtiMinorVersion(),
jvmtiCompileTimeMajorVersion, jvmtiCompileTimeMinorVersion) ) {
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].",
jvmtiMajorVersion(),
jvmtiMinorVersion(),
jvmtiMicroVersion(),
jvmtiCompileTimeMajorVersion,
jvmtiCompileTimeMinorVersion,
jvmtiCompileTimeMicroVersion));
/* Do not let VM get a fatal error, we don't want a core dump here. */
forceExit(1); /* Kill entire process, no core dump wanted */
}
/* 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. */
forceExit(1); /* Kill entire process, no core dump wanted */
}
LOG_MISC(("Onload: %s", options));
/* Get potential capabilities */
(void)memset(&potential_capabilities,0,sizeof(potential_capabilities));
error = JVMTI_FUNC_PTR(gdata->jvmti,GetPotentialCapabilities)
(gdata->jvmti, &potential_capabilities); if (error != JVMTI_ERROR_NONE) {
ERROR_MESSAGE(("JDWP unable to get potential JVMTI capabilities: %s(%d)",
jvmtiErrorText(error), error)); return JNI_ERR;
}
/* Fill in ones that we must have */
(void)memset(&needed_capabilities,0,sizeof(needed_capabilities));
needed_capabilities.can_access_local_variables = 1;
needed_capabilities.can_generate_single_step_events = 1;
needed_capabilities.can_generate_exception_events = 1;
needed_capabilities.can_generate_frame_pop_events = 1;
needed_capabilities.can_generate_breakpoint_events = 1;
needed_capabilities.can_suspend = 1;
needed_capabilities.can_generate_method_entry_events = 1;
needed_capabilities.can_generate_method_exit_events = 1;
needed_capabilities.can_generate_garbage_collection_events = 1;
needed_capabilities.can_maintain_original_method_order = 1;
needed_capabilities.can_generate_monitor_events = 1;
needed_capabilities.can_tag_objects = 1; if (gdata->vthreadsSupported) {
needed_capabilities.can_support_virtual_threads = 1;
}
/* And what potential ones that would be nice to have */
needed_capabilities.can_force_early_return
= potential_capabilities.can_force_early_return;
needed_capabilities.can_generate_field_modification_events
= potential_capabilities.can_generate_field_modification_events;
needed_capabilities.can_generate_field_access_events
= potential_capabilities.can_generate_field_access_events;
needed_capabilities.can_get_bytecodes
= potential_capabilities.can_get_bytecodes;
needed_capabilities.can_get_synthetic_attribute
= potential_capabilities.can_get_synthetic_attribute;
needed_capabilities.can_get_owned_monitor_info
= potential_capabilities.can_get_owned_monitor_info;
needed_capabilities.can_get_current_contended_monitor
= potential_capabilities.can_get_current_contended_monitor;
needed_capabilities.can_get_monitor_info
= potential_capabilities.can_get_monitor_info;
needed_capabilities.can_pop_frame
= potential_capabilities.can_pop_frame;
needed_capabilities.can_redefine_classes
= potential_capabilities.can_redefine_classes;
needed_capabilities.can_redefine_any_class
= potential_capabilities.can_redefine_any_class;
needed_capabilities.can_get_owned_monitor_stack_depth_info
= potential_capabilities.can_get_owned_monitor_stack_depth_info;
needed_capabilities.can_get_constant_pool
= potential_capabilities.can_get_constant_pool;
{
needed_capabilities.can_get_source_debug_extension = 1;
needed_capabilities.can_get_source_file_name = 1;
needed_capabilities.can_get_line_numbers = 1;
needed_capabilities.can_signal_thread
= potential_capabilities.can_signal_thread;
}
/* Add the capabilities */
error = JVMTI_FUNC_PTR(gdata->jvmti,AddCapabilities)
(gdata->jvmti, &needed_capabilities); if (error != JVMTI_ERROR_NONE) {
ERROR_MESSAGE(("JDWP unable to get necessary JVMTI capabilities."));
forceExit(1); /* Kill entire process, no core dump wanted */
}
/* Initialize event number mapping tables */
eventIndexInit();
/* Set the initial JVMTI event notifications */
error = set_event_notification(JVMTI_ENABLE, EI_VM_DEATH); if (error != JVMTI_ERROR_NONE) { return JNI_ERR;
}
error = set_event_notification(JVMTI_ENABLE, EI_VM_INIT); if (error != JVMTI_ERROR_NONE) { return JNI_ERR;
} if (initOnUncaught || (initOnException != NULL)) {
error = set_event_notification(JVMTI_ENABLE, EI_EXCEPTION); if (error != JVMTI_ERROR_NONE) { return JNI_ERR;
}
}
/* Set callbacks just for 3 functions */
(void)memset(&(gdata->callbacks),0,sizeof(gdata->callbacks));
gdata->callbacks.VMInit = &cbEarlyVMInit;
gdata->callbacks.VMDeath = &cbEarlyVMDeath;
gdata->callbacks.Exception = &cbEarlyException;
error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventCallbacks)
(gdata->jvmti, &(gdata->callbacks), sizeof(gdata->callbacks)); if (error != JVMTI_ERROR_NONE) {
ERROR_MESSAGE(("JDWP unable to set JVMTI event callbacks: %s(%d)",
jvmtiErrorText(error), error)); return JNI_ERR;
}
/* Cleanup, but make sure VM is alive before using JNI, and *makesureJVMTIenvironmentisokbeforedeallocating *memoryallocatedthroughJVMTI,whichallofitis.
*/
/* *Closetransportbeforeexit
*/ if (transport_is_open()) {
transport_close();
}
}
if ( gdata->vmDead ) {
EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM dead at initial Exception event");
} if (!vmInitialized) {
LOG_MISC(("VM is not initialized yet")); return;
}
LOG_MISC(("Initializing on uncaught exception"));
initialize(env, thread, EI_EXCEPTION);
} elseif (initOnException != NULL) {
jclass clazz;
/* Get class of exception thrown */
clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, exception); if ( clazz != NULL ) { char *signature = NULL; /* initing on throw, check */
error = classSignature(clazz, &signature, NULL);
LOG_MISC(("Checking specific exception: looking for %s, got %s",
initOnException, signature)); if ( (error==JVMTI_ERROR_NONE) &&
(strcmp(signature, initOnException) == 0)) {
LOG_MISC(("Initializing on specific exception"));
initialize(env, thread, EI_EXCEPTION);
} else {
error = AGENT_ERROR_INTERNAL; /* Just to cause restore */
} if ( signature != NULL ) {
jvmtiDeallocate(signature);
}
} else {
error = AGENT_ERROR_INTERNAL; /* Just to cause restore */
}
/* If initialize didn't happen, we need to restore things */ if ( error != JVMTI_ERROR_NONE ) { /* *Restoreexceptionstatefrombeforecallbackcall
*/
LOG_MISC(("No initialization, didn't find right exception")); if (currentException != NULL) {
JNI_FUNC_PTR(env,Throw)(env, currentException);
} else {
JNI_FUNC_PTR(env,ExceptionClear)(env);
}
}
/* All process exit() calls come from here */ void
forceExit(int exit_code)
{ /* make sure the transport is closed down before we exit() */
transport_close(); exit(exit_code);
}
/* All JVM fatal error exits lead here (e.g. we need to kill the VM). */ staticvoid
jniFatalError(JNIEnv *env, constchar *msg, jvmtiError error, int exit_code)
{
JavaVM *vm; char buf[512];
gdata->vmDead = JNI_TRUE; if ( msg==NULL )
msg = "UNKNOWN REASON";
vm = gdata->jvm; if ( env==NULL && vm!=NULL ) {
jint rc = (*((*vm)->GetEnv))(vm, (void **)&env, JNI_VERSION_1_2); if (rc != JNI_OK ) {
env = NULL;
}
} if ( error != JVMTI_ERROR_NONE ) {
(void)snprintf(buf, sizeof(buf), "JDWP %s, jvmtiError=%s(%d)",
msg, jvmtiErrorText(error), error);
} else {
(void)snprintf(buf, sizeof(buf), "JDWP %s", msg);
} if (env != NULL) {
(*((*env)->FatalError))(env, buf);
} else { /* Should rarely ever reach here, means VM is really dead */
print_message(stderr, "ERROR: JDWP: ", "\n", "Can't call JNI FatalError(NULL, \"%s\")", buf);
}
forceExit(exit_code);
}
staticint
get_tok(char **src, char *buf, int buflen, char sep)
{ 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 */ return0;
}
staticvoid
printUsage(void)
{
TTY_MESSAGE(( " Java Debugger JDWP Agent Library\n" " --------------------------------\n" "\n" " (See the \"VM Invocation Options\" section of the JPDA\n" " \"Connection and Invocation Details\" document for more information.)\n" "\n" "jdwp usage: java " AGENTLIB "=[help]|[<option>=<value>, ...]\n" "\n" "Option Name and Value Description Default\n" "--------------------- ----------- -------\n" "suspend=y|n wait on startup? y\n" "transport=<name> transport spec none\n" "address=<listen/attach address> transport spec \"\"\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" "timeout=<timeout value> for listen/attach in milliseconds n\n" "includevirtualthreads=y|n List of all threads includes virtual threads as well as platform threads.\n" " Virtual threads are a preview feature of the Java platform.\n" " n\n" "mutf8=y|n output modified utf-8 n\n" "quiet=y|n control over terminal messages n\n"));
TTY_MESSAGE(( "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" " java " AGENTLIB "=transport=dt_socket,address=localhost:8000 ...\n" " - Using sockets listen for a debugger to attach:\n" " java " AGENTLIB "=transport=dt_socket,server=y,suspend=y ...\n" "\n" "Notes\n" "-----\n" " - A timeout value of 0 (the default) is no timeout.\n" "\n" "Warnings\n" "--------\n" " - The older " XRUN " interface can still be used, but will be removed in\n" " a future release, for example:\n" " java " XRUN ":[help]|[<option>=<value>, ...]\n"
));
TTY_MESSAGE(( "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" " " XRUN " or " AGENTLIB " command line option.\n"
));
#endif
}
static jboolean checkAddress(void *bagItem, void *arg)
{
TransportSpec *spec = (TransportSpec *)bagItem; if (spec->address == NULL) {
ERROR_MESSAGE(("JDWP Non-server transport %s must have a connection " "address specified through the 'address=' option",
spec->name)); return JNI_FALSE;
} else { return JNI_TRUE;
}
}
/* atexit() callback */ staticvoid
atexit_finish_logging(void)
{ /* Normal exit(0) (not _exit()) may only reach here */
finish_logging(); /* Only first call matters */
}
/* Options being NULL will end up being an error. */ if (options == NULL) {
options = "";
}
/* Check for "help" BEFORE we add any environmental settings */ if ((strcmp(options, "help")) == 0) {
printUsage();
forceExit(0); /* Kill entire process, no core dump wanted */
}
/* These buffers are never freed */
{ char *envOptions;
/* Setup logging now */ if ( logfile!=NULL ) {
setup_logging(logfile, logflags);
(void)atexit(&atexit_finish_logging);
}
if (bagSize(transports) == 0) {
errmsg = "no transport specified"; goto bad_option_with_errmsg;
}
/* *TODO:Removewhenmultipletransportsareallowed.(replacewith *checkbelow.
*/ if (bagSize(transports) > 1) {
errmsg = "multiple transports are not supported in this release"; goto bad_option_with_errmsg;
}
/* All normal exit doors lead here */ void
debugInit_exit(jvmtiError error, constchar *msg)
{ enum exit_codes { EXIT_NO_ERRORS = 0, EXIT_JVMTI_ERROR = 1, EXIT_TRANSPORT_ERROR = 2 };
// Release commandLoop vmDeathLock if necessary
commandLoop_exitVmDeathLockOnError();
// Prepare to exit. Log error and finish logging
LOG_MISC(("Exiting with error %s(%d): %s", jvmtiErrorText(error), error,
((msg == NULL) ? "" : msg)));
// coredump requested by command line. Keep JVMTI data dirty if (error != JVMTI_ERROR_NONE && docoredump) {
LOG_MISC(("Dumping core as requested by command line"));
finish_logging();
abort();
}
finish_logging();
// Cleanup the JVMTI if we have one if (gdata != NULL) {
gdata->vmDead = JNI_TRUE; if (gdata->jvmti != NULL) { // Dispose of jvmti (gdata->jvmti becomes NULL)
disposeEnvironment(gdata->jvmti);
}
}
// We are here with no errors. Kill entire process and exit with zero exit code if (error == JVMTI_ERROR_NONE) {
forceExit(EXIT_NO_ERRORS); return;
}
// No transport initialized. // As we don't have any details here exiting with separate exit code if (error == AGENT_ERROR_TRANSPORT_INIT) {
forceExit(EXIT_TRANSPORT_ERROR); return;
}
// We have JVMTI error. Call hotspot jni_FatalError handler
jniFatalError(NULL, msg, error, EXIT_JVMTI_ERROR);
// hotspot calls os:abort() so we should never reach code below, // but guard against possible hotspot changes
// Last chance to die, this kills the entire process.
forceExit(EXIT_JVMTI_ERROR);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.