在eclipse中调试hotspot
前言
相信很多人在阅读源码的时候,都会习惯的去调试对应的代码以加深理解。阅读hotspot的源码也不例外。听说很多大牛都是直接用gdb来调试的,但是大部分的程序员还是习惯在IDE下进行调试,包括我在内。然后就捣鼓了一个下午,在eclipse CDT下调试hotspot成功,特此记录吧。
ps
1 下面所有的内容,都是基于你已经自己编译openjdk成功的基础上的,如果还没完成这一步,请参考
自己编译JDK的时候遇到的问题记录 这篇文章先自己编译JDK。
2 其实对于C C++程序员 来说,gdb是非常常用的调试工具。这里也顺便提一句如何使用gdb来调试。我编译的虚拟机的目标地址在
这也间接说明我的编译级别是jvmg.在这个目录下,有一个hotspot 脚本。直接运行,可以替代很多重复性的输入工作,具体如下
[[email protected] jvmg]$ ./hotspot -gdb Queens GNU gdb (GDB) Fedora 7.7.1-13.fc20 Copyright (C) 2014 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-redhat-linux-gnu". 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". Breakpoint 1 at 0x40431d: file /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/src/share/tools/launcher/java.c, line 1270. Missing separate debuginfo for /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/jvmg/libjvm.so Try: yum --enablerepo='*debug*' install /usr/lib/debug/.build-id/d6/a890fc719cc82de2a2c53a1dbd1a53f6d3f46b.debug [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". Using java runtime at: /home/zhenghui/software/jdk7/jdk1.7.0_25/jre [New Thread 0x7ffff7fdc700 (LWP 8156)] [Switching to Thread 0x7ffff7fdc700 (LWP 8156)] Breakpoint 1, InitializeJVM (pvm=0x7ffff7fdbe38, penv=0x7ffff7fdbe30, ifn=0x7ffff7fdbe40) at /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/src/share/tools/launcher/java.c:1270 1270 memset(&args, 0, sizeof(args)); Missing separate debuginfos, use: debuginfo-install glibc-2.18-12.fc20.x86_64 (gdb) info args pvm = 0x7ffff7fdbe38 penv = 0x7ffff7fdbe30 ifn = 0x7ffff7fdbe40 (gdb) info locals args = {version = -169313992, nOptions = 32767, options = 0x7ffff7fe0000, ignoreUnrecognized = 0 '\000'} r = 0 (gdb) list 1265 InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn) 1266 { 1267 JavaVMInitArgs args; 1268 jint r; 1269 1270 memset(&args, 0, sizeof(args)); 1271 args.version = JNI_VERSION_1_2; 1272 args.nOptions = numOptions; 1273 args.options = options; 1274 args.ignoreUnrecognized = JNI_FALSE;
正文
1 下载eclipse CDT
下载的链接是 http://www.eclipse.org/cdt/downloads.php 。这个没什么好说的,就是eclipse 支持 C C++的版本。
2 把项目导入eclipse
file->new->Makefile Project with Existing Code 对应的project name 可以自定义,对应的Existing Code location 选择openjdk目录下的hotspot目录,然后点击finish
3 设置build commond 和build location
项目右键-》properites ->C C++ Build 需要设置的值有两个,
1 把use default build commond 的勾去掉,在Buid Commond下输入
make -f Makefile clean jvmg ALT_BOOTDIR=/home/zhenghui/software/jdk7/jdk1.7.0_25 ARCH_DATA_MODEL=64 LANG=C
注意,ALT_BOOTDIR的地址是你在oracle下下载的已经编译好的jdk目录, 如果你的机器是64位的,加上ARCH_DATA_MODEL=64
2 把Build Location的地址改成 ${workspace_loc:/hotspot}/make 。eclipse会自动去找${workspace_loc:/hotspot}/make下的Makefile文件作为build的构建文件
在这步为止,你可以直接build project了。顺利的话,你可以在大概30~40min以后build完成,并看到让人激动人心的
make[3]: Leaving directory `/home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/fastdebug' cd linux_amd64_compiler2/fastdebug && ./test_gamma java version "1.7.0_25" Java(TM) SE Runtime Environment (build 1.7.0_25-b15) OpenJDK 64-Bit Server VM (build 24.0-b56-internal-fastdebug, mixed mode) 1. A1 B5 C8 D6 E3 F7 G2 H4 2. A1 B6 C8 D3 E7 F4 G2 H5 3. A1 B7 C4 D6 E8 F2 G5 H3 4. A1 B7 C5 D8 E2 F4 G6 H3 5. A2 B4 C6 D8 E3 F1 G7 H5 6. A2 B5 C7 D1 E3 F8 G6 H4 7. A2 B5 C7 D4 E1 F8 G6 H3 8. A2 B6 C1 D7 E4 F8 G3 H5 9. A2 B6 C8 D3 E1 F4 G7 H5 10. A2 B7 C3 D6 E8 F5 G1 H4 11. A2 B7 C5 D8 E1 F4 G6 H3 12. A2 B8 C6 D1 E3 F5 G7 H4 13. A3 B1 C7 D5 E8 F2 G4 H6 14. A3 B5 C2 D8 E1 F7 G4 H6 15. A3 B5 C2 D8 E6 F4 G7 H1 16. A3 B5 C7 D1 E4 F2 G8 H6 17. A3 B5 C8 D4 E1 F7 G2 H6 18. A3 B6 C2 D5 E8 F1 G7 H4 19. A3 B6 C2 D7 E1 F4 G8 H5 20. A3 B6 C2 D7 E5 F1 G8 H4 21. A3 B6 C4 D1 E8 F5 G7 H2 22. A3 B6 C4 D2 E8 F5 G7 H1 23. A3 B6 C8 D1 E4 F7 G5 H2 24. A3 B6 C8 D1 E5 F7 G2 H4 25. A3 B6 C8 D2 E4 F1 G7 H5 26. A3 B7 C2 D8 E5 F1 G4 H6 27. A3 B7 C2 D8 E6 F4 G1 H5 28. A3 B8 C4 D7 E1 F6 G2 H5 29. A4 B1 C5 D8 E2 F7 G3 H6 30. A4 B1 C5 D8 E6 F3 G7 H2 31. A4 B2 C5 D8 E6 F1 G3 H7 32. A4 B2 C7 D3 E6 F8 G1 H5 33. A4 B2 C7 D3 E6 F8 G5 H1 34. A4 B2 C7 D5 E1 F8 G6 H3 35. A4 B2 C8 D5 E7 F1 G3 H6 36. A4 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1 F8 G2 H5 76. A6 B4 C1 D5 E8 F2 G7 H3 77. A6 B4 C2 D8 E5 F7 G1 H3 78. A6 B4 C7 D1 E3 F5 G2 H8 79. A6 B4 C7 D1 E8 F2 G5 H3 80. A6 B8 C2 D4 E1 F7 G5 H3 81. A7 B1 C3 D8 E6 F4 G2 H5 82. A7 B2 C4 D1 E8 F5 G3 H6 83. A7 B2 C6 D3 E1 F4 G8 H5 84. A7 B3 C1 D6 E8 F5 G2 H4 85. A7 B3 C8 D2 E5 F1 G6 H4 86. A7 B4 C2 D5 E8 F1 G3 H6 87. A7 B4 C2 D8 E6 F1 G3 H5 88. A7 B5 C3 D1 E6 F8 G2 H4 89. A8 B2 C4 D1 E7 F5 G3 H6 90. A8 B2 C5 D3 E1 F7 G4 H6 91. A8 B3 C1 D6 E2 F5 G7 H4 92. A8 B4 C1 D3 E6 F2 G7 H5 Using java runtime at: /home/zhenghui/software/jdk7/jdk1.7.0_25/jre make[2]: Leaving directory `/home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux' make[1]: Leaving directory `/home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/make' cd /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/make; \ make BUILD_FLAVOR=fastdebug VM_TARGET=fastdebug1 generic_build1 INFO: ENABLE_FULL_DEBUG_SYMBOLS=1 INFO: /usr/bin/objcopy cmd found so will create .debuginfo files. INFO: STRIP_POLICY=min_strip INFO: ZIP_DEBUGINFO_FILES=1 make[1]: Entering directory `/home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/make' mkdir -p /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux No compiler1 (fastdebug1) for ARCH_DATA_MODEL=64 make[1]: Leaving directory `/home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/make' make BUILD_FLAVOR=fastdebug VM_SUBDIR=fastdebug \ EXPORT_SUBDIR=/fastdebug \ generic_export INFO: ENABLE_FULL_DEBUG_SYMBOLS=1 INFO: /usr/bin/objcopy cmd found so will create .debuginfo files. INFO: STRIP_POLICY=min_strip INFO: ZIP_DEBUGINFO_FILES=1 make[1]: Entering directory `/home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/make' cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_docs/jvmti.html /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/docs/platform/jvmti/jvmti.html cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/fastdebug/libjsig.so /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/libjsig.so cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/fastdebug/libjsig.diz /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/libjsig.diz rm -f /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/server/Xusage.txt.temp sed 's/\(separated by \)[;:]/\1:/g' /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/src/share/vm/Xusage.txt > /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/server/Xusage.txt.temp mv /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/server/Xusage.txt.temp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/server/Xusage.txt cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/fastdebug/libjvm.so /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/server/libjvm.so cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/fastdebug/libjvm.diz /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/server/libjvm.diz cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/fastdebug/libsaproc.so /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/libsaproc.so cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/generated/sa-jdi.jar /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/lib/sa-jdi.jar cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/fastdebug/libsaproc.diz /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/jre/lib/amd64/libsaproc.diz cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/generated/jvmtifiles/jvmti.h /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/include/jvmti.h cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/src/share/vm/code/jvmticmlr.h /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/include/jvmticmlr.h cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/src/share/vm/prims/jni.h /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/include/jni.h cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/src/cpu/x86/vm/jni_x86.h /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/include/linux/jni_md.h cp /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/src/share/vm/services/jmm.h /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/export-linux-amd64/fastdebug/include/jmm.h make[1]: Leaving directory `/home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/make' 20:51:06 Build Finished (took 29m:44s.877ms)
这说明在eclipse编译hotspot是成功了。下面,继续说如果配置调试。我们还是以openjdk中自带的N皇后问题的java代码(这个无所谓,自己写一个HelloWorld也是可以的)为例子。
4 新建application
Debug Configurations ->新建一个C C++ Application -》main-》C C++ Applications 输入 /home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/jvmg/gamma 。project 选择当前工程
配置Arguments 就是刚才说的Queens
设置Environment 新增两个环境变量分别是
CLASSPATH=.:/home/zhenghui/software/openjdk_eclipse/openjdk/hotspot/build/linux/linux_amd64_compiler2/jvmg
在CLASSPATH里加上jvmg目录是因为我们运行的测试类(Queens)在该目录下。
到这里为止,所有的设置都OK了。在java.c中main方法第一行设置一个断点,直接debug。看效果
bingo!