Hello World调试Hotspot

2020/06/29 16:26
阅读数 66

本地安装GDB

brew install gdb

➜  ~  gdb --version
GNU gdb (GDB) 9.1
Copyright (C) 2020 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.

除了这个,在Mac系统系统里面还要配置证书相关的操作。

按入下步骤创建代码签名的证书:

  1. 打开 Keychain Access 应用程序(/Applications/Utilities/Keychain Access.app)
  2. 执行菜单 钥匙串访问 -> 证书助理 -> 创建证书
  3. 填写如下信息:
    • 名称:gdb_codesign
    • 身份类型:自签名根证书
    • 证书类型:代码签名
    • 钩选:让我覆盖这些默认设置 http://static.cyblogs.com/QQ20200524-221553@2x.jpg
  4. 一路确定,直到指定证书位置的步骤,选择系统 http://static.cyblogs.com/QQ20200524-221644@2x.jpg
  5. 点击“创建”,会提示用输入系统登录密码,创建完成
  6. 钥匙串访问程序中,选择左侧栏的系统我的证书,找到你刚刚创建的gdb_codesign证书并双击打开证书信息窗口,展开信任项,设置使用此证书时:始终信任
  7. 关闭证书信息窗口,系统会再次要求输入系统登录密码。

因为我现在的系统是MacOS Catania,是在 Mojave (10.14) 之后的系统。所以还需要创建一个配置文件gdb-entitlement.xml,其内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.cs.debugger</key>
    <true/>
</dict>
</plist>
</pre>

最后执行命令:

➜  Desktop  codesign --entitlements gdb-entitlement.xml -fs gdb_codesign $(which gdb)

终端中 gdb 断点进入源码调试 hotspot

编译class
# 在我的桌面创建一个Test.java文件
vim Test.java
public class Test{
    public static void main(String[] args){
        System.out.println("hello world !");
    }
}

找到我对应的openjdk8的build地址

/Users/chenyuan/Workspaces/Openjdk/openjdk8/build/macosx-x86_64-normal-server-release/jdk

利用javacjava命令运行Test.java文件

➜  Desktop  /Users/chenyuan/Workspaces/Openjdk/openjdk8/build/macosx-x86_64-normal-server-release/jdk/bin/javac Test.java
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGILL (0x4) at pc=0x000000010267c7eb, pid=89116, tid=0x0000000000004f03
#
# JRE version: OpenJDK Runtime Environment (8.0) (build 1.8.0-internal-chenyuan_2020_05_24_03_17-b00)
# Java VM: OpenJDK 64-Bit Server VM (25.71-b00 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.dylib+0x47c7eb]  PerfDataManager::destroy()+0xab
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/chenyuan/Desktop/hs_err_pid89116.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#

[error occurred during error reporting , id 0x4]

[1]    89116 abort       Test.java
➜  Desktop  ll
total 112
-rw-r--r--  1 chenyuan  staff   415B May 24 21:33 Test.class
-rw-r--r--  1 chenyuan  staff   116B May 24 21:31 Test.java
-rw-r--r--  1 chenyuan  staff    46K May 24 21:33 hs_err_pid89116.log
➜  Desktop  /Users/chenyuan/Workspaces/Openjdk/openjdk8/build/macosx-x86_64-normal-server-release/jdk/bin/java Test
hello world !  ------- 牛逼的打印,无敌的HelloWorkd
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGILL (0x4) at pc=0x000000010be7c7eb, pid=89512, tid=0x0000000000002403
#
# JRE version: OpenJDK Runtime Environment (8.0) (build 1.8.0-internal-chenyuan_2020_05_24_03_17-b00)
# Java VM: OpenJDK 64-Bit Server VM (25.71-b00 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.dylib+0x47c7eb]  PerfDataManager::destroy()+0xab
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/chenyuan/Desktop/hs_err_pid89512.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#

[error occurred during error reporting , id 0x4]

[1]    89512 abort       Test
gdb测试
➜  Desktop  gdb --args /Users/chenyuan/Workspaces/Openjdk/openjdk8/build/macosx-x86_64-normal-server-release/jdk/bin/java Test
GNU gdb (GDB) 9.1
Copyright (C) 2020 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-apple-darwin19.3.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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 /Users/chenyuan/Workspaces/Openjdk/openjdk8/build/macosx-x86_64-normal-server-release/jdk/bin/java...
(gdb) break init.cpp:95
No source file named init.cpp.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (init.cpp:95) pending.
(gdb) run
Starting program: /Users/chenyuan/Workspaces/Openjdk/openjdk8/build/macosx-x86_64-normal-server-release/jdk/bin/java Test
[New Thread 0x1803 of process 22052]
[New Thread 0x2503 of process 22052]
[New Thread 0x2403 of process 22052]
warning: unhandled dyld version (16)
[New Thread 0x180f of process 22052]
[New Thread 0x2303 of process 22052]

Thread 4 received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x180f of process 22052]
0x00000001040002b4 in ?? ()
(gdb) l
111	    // add one more to mark the end
112	    margv = (char **)JLI_MemAlloc((margc + 1) * (sizeof(char *)));
113	    {
114	        int i = 0;
115	        StdArg *stdargs = JLI_GetStdArgs();
116	        for (i = 0 ; i < margc ; i++) {
117	            margv[i] = stdargs[i].arg;
118	        }
119	        margv[i] = NULL;
120	    }
(gdb) quit
A debugging session is active.

	Inferior 1 [process 22052] will be killed.

Quit anyway? (y or n) y

我在这里发现l这里查看代码跟我debug的地方并不同,我就看看日志发现日志中当时有一个提示:No source file named init.cpp. 然后又找了一翻文章,找到这个时候当时编译的时候没有添加g参数。详细请看:https://blog.csdn.net/wenceng9/article/details/21372265 (我是在不想再重新编译一次了,因为想早点睡觉。哈哈~)

Clion中调试不香吗?

打开 clion,选择 File->ImportProject,选择到 /Users/chenyuan/Workspaces/Openjdk/openjdk8/hotspot 作为 jvm 源码的根目录,这里导入的过程无脑点击 next 即可

对于可能遇到的头文件不包含问题,解决如下:

clion 导入源码之后遇到头文件找不到的问题,而实际上这些头文件在源码里面是存在的,只不过在某些源文件里面是以相对路径的方式来搜索,可以在 CMakeLists.txt 里面添加一些根路径。

http://static.cyblogs.com/QQ20200524-223658@2x.jpg

include_directories(./src/share/vm)
include_directories(./src/cpu/x86/vm)
include_directories(./src/share/vm/precompiled)
include_directories(./src/share/vm/utilities)

另外,如果某些头文件依然找不到,可以手工导入,然后把导入的头文件加到 hotspot/src/share/vm/precompiled/precompiled.hpp 里,因为大多数源文件都会包含这个源文件

http://static.cyblogs.com/QQ20200524-223840@2x.jpg

# include <cstdlib>
# include <cstdint>
# include "register_x86.hpp"
# include "assembler_x86.hpp"
# include "globalDefinitions.hpp"
# include "globalDefinitions_x86.hpp"
# include "assembler_x86.hpp"
# include <stubRoutines_x86.hpp>

进入如下界面,添加 Application:openjdk8Execuable 中选择/Users/chenyuan/Workspaces/Openjdk/openjdk8/build/macosx-x86_64-normal-server-release/jdk/bin

http://static.cyblogs.com/QQ20200524-224033@2x.jpg

配置完成后,就可以执行openjdk8了。

http://static.cyblogs.com/QQ20200524-224305@2x.jpg

参考地址

如果大家喜欢我的文章,可以关注个人订阅号。欢迎随时留言、交流。如果想加入微信群的话一起讨论的话,请加管理员简栈文化-小助手(lastpass4u),他会拉你们进群。

简栈文化服务订阅号

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部