vscode调试OpenJDK

vscode调试OpenJDK

环境

  • 系统: ubuntu 18.10
  • 编辑器: vscode
  • JDK版本 openjdk8u
  • bootstrap jdk版本: openjdk8

vscode 比较轻量级,使用简便,缺点是遇到没有调试信息的代码会弹提示,不能像IDE那样显示汇编.

下载Openjdk

各个版本都在这里:

OpenJDK Mercurial Repositorieshg.openjdk.java.net

我们选择jdk8u.

hg clone https://hg.openjdk.java.net/jdk8u/jdk8u openjdk
chmod u+x get_source.sh 
./get_source.sh

hg命令需要事先安装Mercurial.

sudo apt install mercurial

如果下载缓慢可以设置代理,在/etc/mercurial/hgrc文件里加入:

# system-wide mercurial configuration file  
# See hgrc(5) for more information
[http_proxy]
host=host:port
[https_proxy]
host=host:port

编译

编译jdk8需要jdk7或者jdk8作为bootstrap jdk,如果版本不对可以通过修改环境变量或者加上configure参数--with-boot-jdk参数指定.

java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-1ubuntu0.18.10.1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

配置:

chmod u+x configure
./configure --with-debug-level=slowdebug --with-native-debug-symbols=internal

--with-debug-level=slowdebug参数是为了显示更多调试信息.hotspot编译之后是一个名为libjvm.so的动态库,在java命令里通过dlopen加载,如果不加上--with-native-debug-symbols=internal,需要手动解压jdk/lib/amd64/server里的libjvm.debuginfo,或者指定动态库路径,调试起来会很复杂.

将hotspot/make/linux/makefiles/gcc.make中的WARNINGS_ARE_ERRORS = -Werror改为WARNINGS_ARE_ERRORS = -w,不然出现warning就会出现如下异常.

cc1plus: all warnings being treated as errors

编译:

make all

完成

build/linux-x86_64-normal-server-slowdebug/jdk/bin/java -version
openjdk version "1.8.0-internal-debug"
OpenJDK Runtime Environment (build 1.8.0-internal-debug-kraken_2018_11_18_00_46-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00-debug, mixed mode)

调试

打开VSCode, File -> OpenFolder选中hotspot文件夹,点击左侧虫虫图标,点击add configuration选中c/c++ (gdb) Launch,修改lauch.json文件.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/../build/linux-x86_64-normal-server-slowdebug/jdk/bin/java",
            "args": ["-cp","~/Desktop","Hello"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

主要修改的是program和args,program设置为jdk/bin/java,args设置为编译好的class,断点打在jni.cpp文件下的JNI_CreateJavaVM函数上,这里是jvm的入口,大概就是这样.

发布于 2018-11-18

文章被以下专栏收录