📄 startjvm.c
字号:
/* * Copyright (c) 1998-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. * * Use is subject to license terms. *//*========================================================================= * KVM *========================================================================= * SYSTEM: KVM * SUBSYSTEM: Main Java program * FILE: StartJVM.c * OVERVIEW: System initialization and start of JVM * AUTHOR: Antero Taivalsaari, Sun Labs * Edited by Doug Simon 11/1998 * Frank Yellin * Bill Pittore (Java-level debugging) *=======================================================================*//*========================================================================= * Include files *=======================================================================*/#include "global.h"/*========================================================================= * Functions *=======================================================================*//*========================================================================= * FUNCTION: readCommandLineArguments() * TYPE: constructor (kind of) * OVERVIEW: Read the extra command line arguments provided * by the user and construct a string array containing * the arguments. Note: before executing 'main' the * string array must be pushed onto the operand stack. * INTERFACE: * parameters: command line parameters * returns: string array containing the command line arguments *=======================================================================*/static ARRAYreadCommandLineArguments(int argc, char* argv[]){ /* Get the number of command line arguments */ ARRAY_CLASS arrayClass = getArrayClass(1, JavaLangString, 0); ARRAY stringArray; int numberOfArguments = argc; int argCount = (numberOfArguments > 0) ? numberOfArguments : 0; int i; /* Create a string array for the arguments */ START_TEMPORARY_ROOTS IS_TEMPORARY_ROOT(stringArray, instantiateArray(arrayClass, argCount)); for (i = 0; i < numberOfArguments; i++) { stringArray->data[i].cellp = (cell *)instantiateString(argv[i], strlen(argv[i])); } END_TEMPORARY_ROOTS return stringArray;}/*========================================================================= * FUNCTION: loadMainClass() * TYPE: private operation * OVERVIEW: The main class is loaded in a slightly different * way than the other classes. We need to make sure * that the given main class is not an array class. * Also, we need to replace all the '.' characters * in the class name with '/' characters. * INTERFACE: * parameters: class name * returns: the main class *=======================================================================*/static INSTANCE_CLASSloadMainClass(char* className){ /* Make sure this is not an array class */ if (*className == '[') return NULL; /* Replace all the '.' characters in the class name with '/' chars */ replaceLetters(className, '.', '/'); return (INSTANCE_CLASS)getClass(className);}/*========================================================================= * FUNCTION: KVM_Start * TYPE: private operation * OVERVIEW: Initialize everything. This operation is called * when the VM is launched. * INTERFACE: * parameters: command line parameters * returns: zero if everything went well, non-zero otherwise *=======================================================================*/int KVM_Start(int argc, char* argv[]){ INSTANCE_CLASS mainClass; volatile int returnValue = 0; /* Needed to make compiler happy */ ERROR_TRY { /* If ROMIZING and RELOCATABLE_ROM */ //CreateROMImage();//空的#if ASYNCHRONOUS_NATIVE_FUNCTIONS /* Initialize asynchronous I/O system */ InitalizeAsynchronousIO();#endif /* Initialize all the essential runtime structures of the VM */ InitializeNativeCode();
//InitializeVM();//空的 /* Initialize global variables */ //InitializeGlobals();//空函数 /* Initialize profiling variables */ //InitializeProfiling();//空的 /* Initialize the memory system */ InitializeMemoryManagement(); /* Initialize internal hash tables */ // InitializeHashtables(); /* Initialize inline caching structures */ InitializeInlineCaching(); /* Initialize the class loading interface */ InitializeClassLoading(); /* Load and initialize the Java system classes needed by the VM */ InitializeJavaSystemClasses(); /* Initialize the class file verifier */ //InitializeVerifier(); /* Initialize the event handling system */ InitializeEvents(); /* Load the main application class */ mainClass = loadMainClass(argv[0]); if (!mainClass) { sprintf(str_buffer, KVM_MSG_CLASS_NOT_FOUND_1STRPARAM, argv[0]); AlertUser(str_buffer); returnValue = 1; } else { /* Parse command line arguments */ ARRAY arguments = readCommandLineArguments(argc - 1, argv + 1); /* Now that we have a main class, initialize */ /* the multithreading system */ InitializeThreading(mainClass, arguments); /* Instances of these classes may have been created without * explicitly executing the "new" instruction. Thus we have * to make sure they are initialized. */#if ENABLE_JAVA_DEBUGGER /* Prepare the VM for Java-level debugging */ if (debuggerActive) { InitDebugger(); }#endif /* These are pushed onto the stack. So JavaLangClass is * the first class that is initialized. */ initializeClass(JavaLangOutOfMemoryError); initializeClass(JavaLangSystem); initializeClass(JavaLangString); initializeClass(JavaLangThread); initializeClass(JavaLangClass);#if ENABLE_JAVA_DEBUGGER /* Prepare the VM for Java-level debugging */ if (vmDebugReady) { setEvent_VMInit(); if (CurrentThread == NIL) { CurrentThread = removeFirstRunnableThread(); /* make sure xp_globals are synched with thread data */ loadExecutionEnvironment(CurrentThread); } sendAllClassPrepares(); }#endif /* ENABLE_JAVA_DEBUGGER */ /* Start the interpreter */ Interpret(); } } ERROR_CATCH (error) { returnValue = error; } ERROR_END_CATCH return returnValue;
/*
1.从 main (int argc, char* argv[])开始
分析argv,如果在命令行中包含了java堆的大小,则保存堆大小;随后从命令行获取类路径(即要运行的class路径)。
2.调用StartJVM(argc, argv)函数,开始kvm的运行。
虚拟机对运行中的错误处理采用了下面的方式:
ERROR_TRY {
虚拟机运行代码;
}ERROR_CATCH(error) {
错误处理代码;
}ERROR_END_CATCH
在虚拟机运行代码出现任何运行期异常时都会调用ERROR_THROW函数(在GLOBAL.H定义),ERROR_THROW将跳转到错误处理代码部分,由错误处理代码决定虚拟机的走向。
以下函数都属于"虚拟机运行代码"部分:
(1).CreateROMImage()
这个函数主要是为了加快KVM启动的速度,在解析指令之前载入JAVA的系统类库。
要执行这个函数必须先定义了ROMIZING以及RELOCATABLE_ROM这两个宏,这都是
允许预载入的选项,后一个是表示可以重新载入系统类。一般我们可以不选这两
个宏(将其定义为0即可)
(2).#if ASYNCHRONOUS_NATIVE_FUNCTIONS
// Initialize asynchronous I/O system //
InitalizeAsynchronousIO();//在虚拟机启动以前要保IOCB(I/O控制块)为空,如果#endif //有其他的I/O活动,虚拟机等待这个线程结束
syhnjs
发表时间:2004-3-15 16:32:33 第1楼
--------------------------------------------------------------------------------
(3).InitializeNativeCode();//初始化Native方法,与具体平台有关的函数,在
vmextra目录下有这个函数
(4).InitializeVM();//初始化虚拟机的运行期数据
(5).InitializeGlobals();初始化所有的全局变量
(6).InitializeMemoryManagement()//初始化内存管理单元;创建java堆以及虚拟机
内存块链表的头指针
(7).InitializeHashtables()//调用createHashTable(HASHTABLE *tablePtr, int
bucketCount)创建哈西表到指针tablePtr所指的位
置,bucketCount是指哈西表所应占的kvm内存块数目
(8).InitializeInlineCaching()//初始化内嵌缓冲(为了加速虚拟机指令的查找开辟
的内存区域)。
(9).InitializeClassLoading()//根据命令行的classpath参数,设置类载入器,(此
函数与平台有关)
(10).InitializeJavaSystemClasses()//载入系统类
(11).InitializeVerifier()//初始化字节码检验器
(12).InitializeEvents()//初始化虚拟机线程事务系统
(13).mainClass = (INSTANCE_CLASS)getClass(replaceLetters(argv[0], '.', '/'))
//得到main方法
(14).ARRAY arguments = readCommandLineArguments(argc - 1, argv + 1)
//获取命令行的参数
(15).InitializeThreading(mainClass, arguments)//准备运行
(16).initializeClass(JavaLangOutOfMemoryError);
//初始化相应的系统类
initializeClass(JavaLangSystem);
initializeClass(JavaLangString);
initializeClass(JavaLangThread);
initializeClass(JavaLangClass);
(17).Interpret()//开始解释
*/}/*========================================================================= * FUNCTION: KVM_Cleanup * TYPE: private operation * OVERVIEW: Clean up everything. This operation is called * when the VM is shut down. * INTERFACE: * parameters: <none> * returns: <nothing> *=======================================================================*/void KVM_Cleanup(){#if ENABLE_JAVA_DEBUGGER if (vmDebugReady) { setEvent_VMDeath(); CloseDebugger(); clearAllBreakpoints();
}#endif //FinalizeVM(); FinalizeInlineCaching(); FinalizeNativeCode(); FinalizeJavaSystemClasses(); FinalizeClassLoading(); FinalizeMemoryManagement(); DestroyROMImage(); FinalizeHashtables();}/*========================================================================= * FUNCTION: StartJVM * TYPE: public global operation * OVERVIEW: Boots up the virtual machine and executes 'main'. * INTERFACE: * parameters: command line arguments * returns: zero if everything went fine, non-zero otherwise. *=======================================================================*/int StartJVM(int argc, char* argv[]){ volatile int returnValue = 0; /* Ensure that we have a class to run */ if (argc <= 0 || argv[0] == NULL) { AlertUser(KVM_MSG_MUST_PROVIDE_CLASS_NAME); return -1; } returnValue = KVM_Start(argc, argv); KVM_Cleanup(); return returnValue;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -