debuginit.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 1,343 行 · 第 1/3 页

C
1,343
字号
/* * @(#)debugInit.c	1.50 06/10/25 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation.  *  * This program 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 at /legal/license.txt).  *  * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.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"/* How the options get to OnLoad: */#define XDEBUG "-Xdebug"#define XRUN "-Xrunjdwp"#define AGENTLIB "-agentlib:jdwp"/* Debug version defaults */#ifdef DEBUG    #define DEFAULT_ASSERT_ON           JNI_TRUE    #define DEFAULT_ASSERT_FATAL        JNI_TRUE    #define DEFAULT_LOGFILE             "jdwp.log"#else    #define DEFAULT_ASSERT_ON           JNI_FALSE    #define DEFAULT_ASSERT_FATAL        JNI_FALSE    #define DEFAULT_LOGFILE             NULL#endifstatic jboolean vmInitialized;static jrawMonitorID initMonitor;static jboolean initComplete;static jbyte currentSessionID; /* * Options set through the OnLoad options string. All of these values * are set once at VM startup and never reset.  */static jboolean isServer = JNI_FALSE;     /* Listens for connecting debuggers? */static jboolean isStrict = JNI_FALSE;     /* Unused */static jboolean useStandardAlloc = JNI_FALSE;  /* Use standard malloc/free? */static struct bag *transports;            /* of TransportSpec */static jboolean initOnStartup = JNI_TRUE;   /* init immediately */static char *initOnException = NULL;        /* init when this exception thrown */static jboolean initOnUncaught = JNI_FALSE; /* init when uncaught exc thrown */static char *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 */static char *logfile = NULL;                /* Name of logfile (if logging) */static unsigned logflags = 0;               /* Log flags */static char *names;                         /* strings derived from OnLoad options *//* * Elements of the transports bag */typedef struct TransportSpec {    char *name;    char *address;    long timeout;} TransportSpec;/* * Forward Refs */static void JNICALL cbEarlyVMInit(jvmtiEnv*, JNIEnv *, jthread);static void JNICALL cbEarlyVMDeath(jvmtiEnv*, JNIEnv *);static void JNICALL cbEarlyException(jvmtiEnv*, JNIEnv *,             jthread, jmethodID, jlocation, jobject, jmethodID, jlocation);static void initialize(JNIEnv *env, jthread thread, EventIndex triggering_ei);static jboolean parseOptions(char *str);/* * 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 jvmtiErrorset_event_notification(jvmtiEventMode mode, EventIndex ei){    jvmtiError error;     error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventNotificationMode)                (gdata->jvmti, mode, eventIndex2jvmti(ei), NULL);    if (error != JVMTI_ERROR_NONE) {        ERROR_MESSAGE(("JDWP unable to configure initial JVMTI event %s: %s(%d)",                    eventText(ei), jvmtiErrorText(error), error));    }    return error;}/* Logic to determine JVMTI version compatibility */static jbooleancompatible_versions(jint major_runtime,     jint minor_runtime,                    jint major_compiletime, jint minor_compiletime){#if 1 /* FIXUP: We allow version 0 to be compatible with anything */    /* Special check for FCS of 1.0. */    if ( major_runtime == 0 || major_compiletime == 0 ) {        return JNI_TRUE;    }#endif    /* Runtime major version must match. */    if ( major_runtime != major_compiletime ) {        return JNI_FALSE;    }    /* Runtime minor version must be >= the version compiled with. */    if ( minor_runtime < minor_compiletime ) {        return JNI_FALSE;    }    /* Assumed compatible */    return JNI_TRUE;}/* OnLoad startup: *   Returning JNI_ERR will cause the java_g VM to core dump, be careful.  */jintonLoadX(JavaVM *vm, char *options, void *reserved) {    jvmtiError error;    jvmtiCapabilities needed_capabilities;    jvmtiCapabilities potential_capabilities;    jint              jvmtiCompileTimeMajorVersion;    jint              jvmtiCompileTimeMinorVersion;    jint              jvmtiCompileTimeMicroVersion;    /* 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;    /* Start filling in gdata */    gdata->jvm = vm;    vmInitialized = JNI_FALSE;    gdata->vmDead = JNI_FALSE;    /* Npt and Utf function init */    NPT_INITIALIZE(&(gdata->npt), NPT_VERSION, NULL);    if (gdata->npt == NULL) {        ERROR_MESSAGE(("JDWP: unable to initialize NPT library"));        return JNI_ERR;    }    gdata->npt->utf = (gdata->npt->utfInitialize)(NULL);    if (gdata->npt->utf == NULL) {        ERROR_MESSAGE(("JDWP: UTF function initialization failed"));        return JNI_ERR;    }    /* 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     *      matches the runtime version we are using.     */    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;       /* 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;    }        LOG_MISC(("OnLoad: DONE"));    return JNI_OK;}JNIEXPORT jint JNICALL JVM_OnLoad(JavaVM *vm, char *options, void *reserved) {  return onLoadX(vm, options, reserved);}JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {  return onLoadX(vm, options, reserved);}/* * 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 JNICALLcbEarlyVMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {    LOG_CB(("cbEarlyVMInit"));    if ( gdata->vmDead ) {        EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM dead at VM_INIT time");    }    if (initOnStartup)        initialize(env, thread, EI_VM_INIT);    vmInitialized = JNI_TRUE;    LOG_MISC(("END cbEarlyVMInit"));}static voiddisposeEnvironment(jvmtiEnv *jvmti_env){    jvmtiError error;        error = JVMTI_FUNC_PTR(jvmti_env,DisposeEnvironment)(jvmti_env);    if ( error == JVMTI_ERROR_MUST_POSSESS_CAPABILITY )         error = JVMTI_ERROR_NONE;  /* NOTE: FIXUP when JVMTI has disposeEnv */    /* What should error return say? */    if (error != JVMTI_ERROR_NONE) {        ERROR_MESSAGE(("JDWP unable to dispose of JVMTI environment: %s(%d)",                        jvmtiErrorText(error), error));    }    gdata->jvmti = NULL;}static void JNICALLcbEarlyVMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) {    LOG_CB(("cbEarlyVMDeath"));    if ( gdata->vmDead ) {        EXIT_ERROR(AGENT_ERROR_INTERNAL,"VM died more than once");    }    disposeEnvironment(jvmti_env);    gdata->jvmti = NULL;    gdata->jvm = NULL;    gdata->vmDead = JNI_TRUE;    LOG_MISC(("END cbEarlyVMDeath"));}static void JNICALLcbEarlyException(jvmtiEnv *jvmti_env, JNIEnv *env, 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?