ubuntu16下编译调试openjdk8

一、下载源码:

https://pan.baidu.com/s/1MPjDhvoV8NMxzNx8Qz0wVw

二、安装openjdk7(boot jdk)

    sudo add-apt-repository ppa:openjdk-r/ppa  
    sudo apt-get update   
    sudo apt-get install openjdk-7-jdk

三、安装部分依赖包

sudo apt-get install libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev
sudo apt-get install libcups2-dev
sudo apt-get install libfreetype6-dev
sudo apt-get install libasound2-dev
sudo apt-get ccache

四、进入openjdk 主目录

sh configure --with-debug-level=slowdebug --enable-debug-symbols ZIP_DEBUGINFO_FILES=0
export LANG=C
unset CLASSPATH
unset JAVA_HOME

五、

sudo make all CONF=linux-x86_64-normal-server-slowdebug ZIP_DEBUGINFO_FILES=0

编译成功后

openjdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin
./java -version

六、问题解决
1、如果出现寻找boot jdk目录的错误

checking headful support... include support for both headful and headless
configure: error: The path of JAVA_HOME_PROCESSED, which resolves as "/usr/lib/jvm/java-1.7.0-openjdk", is not found.
configure exiting with result code 1

解决方式:

cd /usr/lib/jvm
 mv java-1.7.0-openjdk-amd64 java-1.7.0-openjdk
echo $PATH
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-1.7.0-openjdk/bin #根据上一步的返回值,只修改jdk的目录

2 、内核版本问题

*** This OS is not supported: Linux wang-ThinkPad-T430 4.13.0-43-generic #48~16.04.1-Ubuntu SMP Thu May 17 12:56:46 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
/root/study/openjdk-8u40-src-b25-10_feb_2015/openjdk/hotspot/make/linux/Makefile:238: recipe for target 'check_os_version' failed
make[5]: *** [check_os_version] Error 1

解决方式:

 vi hotspot/make/linux/Makefile
SUPPORTED_OS_VERSION = 2.4% 2.5% 2.6% 3% 4% #增加4%
OS_VERSION := $(shell uname -r)
EMPTY_IF_NOT_SUPPORTED = $(filter $(SUPPORTED_OS_VERSION),$(OS_VERSION))

七、使用gdb调试
新建一个Main.java文件

public class Main{  
  public static void main(String[] args){  
    System.out.println("hello world !");  
  }  
} 

编译运行测试

cd /root/study/openjdk-8u40-src-b25-10_feb_2015/openjdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin
./javac Main.java
./java Main
exprot LD_LIBRARY_PATH=/root/study/openjdk-8u40-src-b25-10_feb_2015/openjdk/build/linux-x86_64-normal-server-slowdebug/hotspot/linux_amd64_compiler2/debug
gdb --args ./java Main

#进入gdb, 打断点, 然后运行
(gdb) break thread.cpp:219
(gdb) run

#可以看到下面的输出, 进入到了断点
Starting program: /home/haogrgr/opt/myopenjdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin/java Main
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff7fda700 (LWP 14194)]
[Switching to Thread 0x7ffff7fda700 (LWP 14194)]

Breakpoint 1, Thread::Thread (this=0x7ffff000c000)
    at /home/haogrgr/opt/myopenjdk/hotspot/src/share/vm/runtime/thread.cpp:219
219   set_stack_base(NULL);

#看看当前的代码
(gdb) l ##注意是小写的L

#输出
214 // JavaThread
215 
216 
217 Thread::Thread() {
218   // stack and get_thread
219   set_stack_base(NULL);
220   set_stack_size(0);
221   set_self_raw_id(0);
222   set_lgrp_id(-1);
223

八、在Eclipse中调试代码
1)下载安装Eclipse C/C++ 版

2)导入hotspot工程

  ~进入Eclipse,    选择导入import工程  -> C/C++ -> Existing Code as Makefile Project   ->  单击 Next   -> 进入工程配置界面

         Project Name设为hotspot  

         Existing Code Location是/root/study/openjdk-8u40-src-b25-10_feb_2015/openjdk/hotspot

         Toolchain那里选Linux GCC,然后按Finish. 

  ~把/hotspot/src目录加入源码索引方便代码搜索,    右键hotspot工程  -> properties -> C/C++ General -> Paths and Symbols -> Source Location选项卡 

                                                                              -> 选择Add Folder 吧 hotspot/src目录加入

  ~设置启动类,    右键工程 -> Debug As -> Debug Configurations -> 右键左边的C/C++ Application -> New -> 进入Main选项卡

   在选项卡中:

          C/C++ Application     选择   /root/study/openjdk-8u40-src-b25-10_feb_2015/openjdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin/java

          因为我们这里不再eclipse里面编译,所以这里选上 Disable auto build

   然后切换到Arguments选项卡, 输入Java的参数, 我们这里运行上面的Main类, 于是这里填上   Main   也就是我们要执行的Java程序

   然后切换到Environment选项卡, 添加变量


          LD_LIBRARY_PATH=/root/study/openjdk-8u40-src-b25-10_feb_2015/openjdk/build/linux-x86_64-normal-server-slowdebug/hotspot/linux_amd64_compiler2/debug

          JAVA_HOME=/root/study/openjdk-8u40-src-b25-10_feb_2015/openjdk/build/linux-x86_64-normal-server-slowdebug/jdk/

          CLASSPATH=.:/root/study/openjdk-8u40-src-b25-10_feb_2015/openjdk/build/linux-x86_64-normal-server-slowdebug/jdk/bin        (因为我的Main类在/bin目录下)

   3)点击下面的Apply保存

   4)在eclipse中找到thread.cpp, 在219行打个断点

   5)Debug运行上面配置的Debug, 可以看到顺利调到了断点

你可能感兴趣的:(java)