Mac下怎么用Clion debug openJdk8?

还记得前面编译还了的java - v 有个错误,我们来试一下用Clion debug一下

1.打开Clion ,导入项目


打开 clion,选择 File->ImportProject,选择到 ../openjdk-8/hotspot 目录。

2.配置Run


导入成功以后,会出现一个默认的Run,修改掉Run 里面的配置,去掉build,我们不需要build。Target 不用变,Executable需要改成我们自己编译的./bin/java。
2019-05-12 12.14.47 AM.png2019-05-12 12.15.18 AM.png
clion 导入源码之后遇到头文件找不到的问题,可以在 CMakeLists.txt 里面添加一些根路径,


可以在CMakeLists.txt里面添加,还有不存在的头文件,可以直接添加到src/share/vm/precompiled/precompiled.hpp,从里面找

  • CMakeLists
1
2
3
4
include_directories(./src/share/vm)
include_directories(./src/cpu/x86/vm)
include_directories(./src/share/vm/precompiled)
include_directories(./src/share/vm/utilities)
  • src/share/vm/precompiled/precompiled.hpp
1
2
3
4
5
6
7
8
# include <cstdlib>
# include <cstdint>
# include <cstring>
# include "register_x86.hpp"
# include "assembler_x86.hpp"
# include "globalDefinitions.hpp"
# include "globalDefinitions_x86.hpp"
# include "assembler_x86.hpp"

3.选择断点,点击debug

  • 先打开core dump 查看调用栈,确定断点
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Stack: [0x00007000043c8000,0x00007000044c8000],  sp=0x00007000044c7c70,  free space=1023k
    Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
    V [libjvm.dylib+0xa2e268] PerfData::~PerfData()+0x8
    V [libjvm.dylib+0xa2ee3d] PerfDataManager::destroy()+0x6d
    V [libjvm.dylib+0xa313ed] perfMemory_exit()+0x3d
    V [libjvm.dylib+0x6718cd] exit_globals()+0x1d
    V [libjvm.dylib+0xb744bc] Threads::destroy_vm()+0x19c
    V [libjvm.dylib+0x740e62] jni_DestroyJavaVM+0x182
    C [java+0x3abc] JavaMain+0x28c
    C [libsystem_pthread.dylib+0x3661] _pthread_body+0x154
    C [libsystem_pthread.dylib+0x350d] _pthread_body+0x0
    C [libsystem_pthread.dylib+0x2bf9] thread_start+0xd
  • 刚开始可能会出现2019-05-12 12.22.18 AM.png
  • 可以在点击 LLDB,输入 process handle SIGSEGV --stop=false 即可,这里告诉编译器忽略错误

  • 然后点击resume,绿色的三角
  • 命中断点,就后面的正常debug2019-05-12 12.06.29 AM.png

  • 可看到这个函数执行都这里就是crash了,所以之前的编译错误,可以忽略这里的错误达到正常结束的目的。
  • 2019-05-12 12.06.08 AM.png
  • 这是什么错Exception: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)