使用CLion調試Redis

本文主要介紹如何編譯Redis項目並在JetBrains CLion(如下簡稱CLion)中運行/調試。html

0. 開始以前

JSON Compilation Database

CLion默認支持CMake構建的項目,但Redis項目是基於Make構建的。對於使用Make構建的項目,CLion仍然能夠經過Compilation Database來導入項目,而不用將其修改成CMake項目。同時也能經過Compilation Database實現代碼的分析、跳轉等功能,這對於咱們進行代碼調試頗有幫助。python

有關於Compilation Database的介紹能夠參考這幾篇文章:git

對於基於Make構建的Redis項目來講,有一些工具能夠生成Compilation Database,好比下面幾個:github

一般默認生成的Compilation Database是一個compile_commands.json文件。redis

1. 下載Redis源碼

git clone git@github.com:redis-io/redis.git
cd redis
# 能夠切換至指定版本對應分支
git checkout 5.0
複製代碼

2. 編譯構建Redis

Redis基於Make構建,執行make命令便可完成構建。Redis 默認使用-O2級別優化,可使用make noopt來編譯以關閉優化,得到更多的調試信息。json


咱們須要使用工具來生成Compilation Database,以便於導入CLion。在不一樣系統上各個工具安裝狀況可能略有不一樣,下面分別介紹Ubuntu 16.04MacOS 10.15上編譯Redis並生成Compilation Database的方法。bootstrap

Ubuntu 16.04

使用Bear工具

# 下載Bear工具
sudo apt-get install bear
# 編譯構建,並使用Bear工具生成 Compilation Database
bear make noopt
複製代碼

使用compiledb工具

# 安裝pip
sudo apt-get install python-pip
# pip安裝 compiledb
pip install compiledb
# 編譯構建,並使用compiledb工具生成 Compilation Database
compiledb make noopt
複製代碼

MacOS 10.15

使用Bear工具

MacOS上使用Bear工具須要關閉SIP,關閉方法是進入恢復模式執行csrutil disable命令,而後重啓,詳細操做方法請自行搜索。bash

咱們須要用到Homebrew來安裝Bear,若是沒有安裝Homebrew須要先安裝Homebrew,安裝方法:服務器

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
複製代碼

開始編譯Rediscurl

# 下載Bear工具
brew install bear
# 編譯構建,並使用Bear工具生成 Compilation Database
bear make noopt
複製代碼

若是遇到adlist.c:32:10: fatal error: 'stdlib.h' file not found報錯,能夠先執行如下命令後重試:

sudo mount -uw /
sudo cp -R /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include /usr
複製代碼

使用compiledb工具

咱們須要使用pip來安裝compiledb,若是沒有安裝pip須要先安裝pip,安裝方法:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
# 安裝pip以後可能還須要設置環境變量
複製代碼

開始編譯Redis

# pip安裝 compiledb
pip install compiledb
# 編譯構建,並使用compiledb工具生成 Compilation Database
compiledb make noopt
複製代碼

編譯完成以後能夠檢驗如下是否成功:

$ ./src/redis-server --version
Redis server v=5.0.8 sha=1f7d08b7:1 malloc=libc bits=64 build=3530fd9b48a55c7f
複製代碼

同時根目錄下應該會有一個不爲空的compile_commands.json文件。

導入CLion並調試

對於如何在CLion中管理基於Make構建的項目,CLion的官方幫助文檔中有詳細的介紹: Managing Makefile Projects。參考上述文檔,本小節簡要介紹瞭如何在CLion中導入Redis項目並運行/調試的方法。

  1. 下載安裝CLion,並安裝Makefile Support插件。建議使用最新版本,較老的版本是不支持Make構建的項目的。

  2. 導入項目。打開CLion,選擇Open Or Import,選擇項目目錄中的compile_commands.json文件,彈出框選擇Open as Project,等待文件索引完成。

  3. 建立自定義Build Target。點擊File菜單欄,Settings | Build, Execution, Deployment | Custom Build Targets,點擊+新建一個Target

    • NameTarget的名字,以後在建立Run/Debug配置的時候會看到這個名字
    • 點擊Build或者Clean右邊的三點,彈出框中點擊+新建兩個External Tool配置以下: 第一個配置以下,用來指定構建指令,Program 和 Arguments 共同構成了所要執行的命令 "make noopt"
      Name: make
      Program: make
      Arguments: noopt
      Working directory: $ProjectFileDir$
      複製代碼
      第二個配置以下,用來清理構建輸出,Program 和 Arguments 共同構成了所要執行的命令 "make clean"
      Name: make clean
      Program: make
      Arguments: clean
      Working directory: $ProjectFileDir$
      複製代碼
    • ToolChain選擇DefaultBuild選擇make(上面建立的第一個External Tool);Clean選擇make clean(上面建立的第二個External Tool
  4. 建立自定義的Run/Debug configuration。點擊Run菜單欄,Edit Configurations, 點擊+,選擇Custom Build Application,配置以下:

    # Name:Configure 的名稱
    Name: redis
    # Target:選擇上一步建立的 「Custom Build Target」
    Target: redis
    # Executable:程序執行入口,也就是須要調試的程序
    Executable: 這裏咱們調試Redis Server,選擇`{source_root}/src/redis-server`。
    # Program arguments: 與 「Executable」 配合使用,指定其參數
    Program arguments: 這裏咱們選擇"$ProjectFileDir$/redis.conf"做爲配置文件啓動Redis服務器
    複製代碼

    ExecutableProgram arguments能夠根據須要調試的信息自行設置。

    若是不想每次運行/調試前都執行Build操做(在這裏就是Make構建過程),能夠在編輯頁下方Before launch框中刪除Build條目。

  5. 點擊Run/Debug開始運行/調試。

參考

  1. Redis debugging guide
  2. nickdiego/compiledb: Tool for generating Clang's JSON Compilation Database files for make-based build systems.
  3. Managing Makefile Projects
  4. Dealing with Makefile Projects in CLion: Status Update
相關文章
相關標籤/搜索