zl程序教程

您现在的位置是:首页 >  Java

当前栏目

java较新的版本OpenJdk19+GDB

2023-03-07 09:09:19 时间

前言: JVM(HotSpot)比CLR还早,本篇看下较新的版本OpenJdk19.

编译:

#apt-get insatll openjdk-18-jdk  //先下一个bootstrap引导后续
#unzip -x jdk-jdk-19-36.zip
#cd jdk-jdk-19-36
#.config
#make all

编译后的源码:jdk-jdk-19-36/src,编译后的地址:jdk-jdk-19-36/build/linux-x86_64-server-release/jdk/bin,进入查看下版本

#./java --version
openjdk 19-internal 2022-09-20
OpenJDK Runtime Environment (build 19-internal-adhoc.tang.jdk-jdk-19-36)
OpenJDK 64-Bit Server VM (build 19-internal-adhoc.tang.jdk-jdk-19-36, mixed mode)

java示例:

#cd jdk-jdk-19-36/build/linux-x86_64-server-release/jdk/bin
#vim javahello.java
public class javahello {
    public static void main(String[] args) {
         System.out.println("Hello World!");
    }
}
#./javac javahello.java
#./java  javahello
# ./java javahello
Hello World!

启动GDB

#cd jdk-jdk-19-36/build/linux-x86_64-server-release/jdk/bin
# gdb --args ./java javahello
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./java...
Reading symbols from /home/tang/jdk-jdk-19-36/build/linux-x86_64-server-release/jdk/bin/java.debuginfo...

下断点,然后运行

(gdb) b main
Breakpoint 1 at 0x1180: file src/java.base/share/native/launcher/main.c, line 98.
(gdb) r
Starting program: /home/tang/jdk-jdk-19-36/build/linux-x86_64-server-release/jdk/bin/java javahello
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, main (argc=2, argv=0x7fffffffdf48) at src/java.base/share/native/launcher/main.c:98
warning: Source file is more recent than executable.
98	{

可以看到 它停在了main.c文件里面,地址在 jdk-jdk-19-36/src/java.base/share/native/launcher/main.c:98。为了在GDB里面看到源码,运行以下命令:

cd jdk-jdk-19-36/build/linux-x86_64-server-release/jdk/bin
cp -r /home/tang/jdk-jdk-19-36/src/ .

源码 最后看下jvm源码启动main

cd jdk-jdk-19-36/build/linux-x86_64-server-release/jdk/bin/src/java.base/share/native/launcher
vim main.c

main(int argc, char **argv)
{
    int margc;
    char** margv;
    int jargc;
    char** jargv;
    const jboolean const_javaw = JNI_FALSE;
#endif /* JAVAW */
    {
        int i, main_jargc, extra_jargc;
        JLI_List list;

        main_jargc = (sizeof(const_jargs) / sizeof(char *)) > 1
            ? sizeof(const_jargs) / sizeof(char *)
            : 0; // ignore the null terminator index

        extra_jargc = (sizeof(const_extra_jargs) / sizeof(char *)) > 1
            ? sizeof(const_extra_jargs) / sizeof(char *)
            : 0; // ignore the null terminator index

        if (main_jargc > 0 && extra_jargc > 0) { // combine extra java args
            jargc = main_jargc + extra_jargc;
            list = JLI_List_new(jargc + 1);

            for (i = 0 ; i < extra_jargc; i++) {
                JLI_List_add(list, JLI_StringDup(const_extra_jargs[i]));
            }
                                                                   95,1          49%
}

结尾 作者:江湖评谈