前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hello World调试Hotspot

Hello World调试Hotspot

作者头像
chengcheng222e
发布2021-11-04 16:21:54
4020
发布2021-11-04 16:21:54
举报
文章被收录于专栏:简栈文化
本地安装GDB
代码语言:javascript
复制
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
    • 身份类型:自签名根证书
    • 证书类型:代码签名
    • 钩选:让我覆盖这些默认设置
  4. 一路确定,直到指定证书位置的步骤,选择系统
  1. 点击“创建”,会提示用输入系统登录密码,创建完成
  2. 钥匙串访问程序中,选择左侧栏的系统我的证书,找到你刚刚创建的gdb_codesign证书并双击打开证书信息窗口,展开信任项,设置使用此证书时:为始终信任
  3. 关闭证书信息窗口,系统会再次要求输入系统登录密码。

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

代码语言:javascript
复制
<?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>

最后执行命令:

代码语言:javascript
复制
➜  Desktop  codesign --entitlements gdb-entitlement.xml -fs gdb_codesign $(which gdb)
终端中 gdb 断点进入源码调试 hotspot
编译class
代码语言:javascript
复制
# 在我的桌面创建一个Test.java文件
vim Test.java
public class Test{
    public static void main(String[] args){
        System.out.println("hello world !");
    }
}

找到我对应的openjdk8的build地址

代码语言:javascript
复制
/Users/chenyuan/Workspaces/Openjdk/openjdk8/build/macosx-x86_64-normal-server-release/jdk

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

代码语言:javascript
复制
➜  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测试
代码语言:javascript
复制
➜  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

代码语言:javascript
复制
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

代码语言:javascript
复制
# 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

参考地址
  • https://segmentfault.com/q/1010000004136334
  • https://rqsir.github.io/2019/04/19/openjdk-8-使用Clion调试源码/

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 简栈文化 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 本地安装GDB
  • 终端中 gdb 断点进入源码调试 hotspot
    • 编译class
      • gdb测试
      • Clion中调试不香吗?
      • 参考地址
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档