本文主要介紹如何編譯Redis
項(xiàng)目并在JetBrains CLion
(以下簡(jiǎn)稱CLion
)中運(yùn)行/調(diào)試肖粮。
0. 開始之前
JSON Compilation Database
CLion
默認(rèn)支持CMake
構(gòu)建的項(xiàng)目童番,但Redis
項(xiàng)目是基于Make
構(gòu)建的黄刚。對(duì)于使用Make
構(gòu)建的項(xiàng)目表制,CLion
仍然可以通過Compilation Database
來導(dǎo)入項(xiàng)目蚓挤,而不用將其修改為CMake
項(xiàng)目蟹但。同時(shí)也能通過Compilation Database
實(shí)現(xiàn)代碼的分析逊笆、跳轉(zhuǎn)等功能癣缅,這對(duì)于我們進(jìn)行代碼調(diào)試很有幫助厨剪。
有關(guān)于Compilation Database
的介紹可以參考這幾篇文章:
-
Clang
官方的Compilation Database
介紹頁(yè)面:JSON Compilation Database Format Specification — Clang 11 documentation - 這篇文章介紹了在不同場(chǎng)景下生成
Compilation database
的各種工具:Compilation database — Sarcasm notebook -
CLion
的幫助頁(yè)面簡(jiǎn)要介紹了Compilation Database
哄酝,以及在CLion
的使用:Compilation Database - Help | CLion
對(duì)于基于Make
構(gòu)建的Redis
項(xiàng)目來說,有一些工具可以生成Compilation Database
祷膳,比如下面幾個(gè):
- https://github.com/rizsotto/Bear
- https://github.com/rizsotto/scan-build
- https://github.com/nickdiego/compiledb
通常默認(rèn)生成的Compilation Database
是一個(gè)compile_commands.json
文件陶衅。
1. 下載Redis源碼
git clone git@github.com:redis-io/redis.git
cd redis
# 可以切換至指定版本對(duì)應(yīng)分支
git checkout 5.0
2. 編譯構(gòu)建Redis
Redis
基于Make
構(gòu)建,執(zhí)行make
命令即可完成構(gòu)建直晨。Redis
默認(rèn)使用-O2
級(jí)別優(yōu)化万哪,可以使用make noopt
來編譯以關(guān)閉優(yōu)化,獲得更多的調(diào)試信息抡秆。
我們需要使用工具來生成Compilation Database
奕巍,以便于導(dǎo)入CLion
。在不同系統(tǒng)上各個(gè)工具安裝情況可能略有不同儒士,下面分別介紹Ubuntu 16.04
和MacOS 10.15
上編譯Redis
并生成Compilation Database
的方法的止。
Ubuntu 16.04
使用Bear工具
# 下載Bear工具
sudo apt-get install bear
# 編譯構(gòu)建,并使用Bear工具生成 Compilation Database
bear make noopt
使用compiledb工具
# 安裝pip
sudo apt-get install python-pip
# pip安裝 compiledb
pip install compiledb
# 編譯構(gòu)建着撩,并使用compiledb工具生成 Compilation Database
compiledb make noopt
MacOS 10.15
使用Bear工具
在MacOS
上使用Bear工具需要關(guān)閉SIP诅福,關(guān)閉方法是進(jìn)入恢復(fù)模式執(zhí)行csrutil disable
命令,然后重啟拖叙,詳細(xì)操作方法請(qǐng)自行搜索氓润。
我們需要用到Homebrew
來安裝Bear
,如果沒有安裝Homebrew
需要先安裝Homebrew
薯鳍,安裝方法:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
開始編譯Redis
:
# 下載Bear工具
brew install bear
# 編譯構(gòu)建咖气,并使用Bear工具生成 Compilation Database
bear make noopt
如果遇到adlist.c:32:10: fatal error: 'stdlib.h' file not found
報(bào)錯(cuò),可以先執(zhí)行以下命令后重試:
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之后可能還需要設(shè)置環(huán)境變量
開始編譯Redis
:
# pip安裝 compiledb
pip install compiledb
# 編譯構(gòu)建,并使用compiledb工具生成 Compilation Database
compiledb make noopt
編譯完成之后可以檢驗(yàn)以下是否成功:
$ ./src/redis-server --version
Redis server v=5.0.8 sha=1f7d08b7:1 malloc=libc bits=64 build=3530fd9b48a55c7f
同時(shí)根目錄下應(yīng)該會(huì)有一個(gè)不為空的compile_commands.json
文件斩松。
導(dǎo)入CLion并調(diào)試
對(duì)于如何在CLion
中管理基于Make
構(gòu)建的項(xiàng)目伶唯,CLion
的官方幫助文檔中有詳細(xì)的介紹:
Managing Makefile Projects。參考上述文檔惧盹,本小節(jié)簡(jiǎn)要介紹了如何在CLion
中導(dǎo)入Redis
項(xiàng)目并運(yùn)行/調(diào)試的方法乳幸。
下載安裝
CLion
,并安裝Makefile Support
插件钧椰。建議使用最新版本粹断,較老的版本是不支持Make
構(gòu)建的項(xiàng)目的。-
導(dǎo)入項(xiàng)目演侯。打開
CLion
姿染,選擇Open Or Import
,選擇項(xiàng)目目錄中的compile_commands.json
文件,彈出框選擇Open as Project
悬赏,等待文件索引完成狡汉。
image -
創(chuàng)建自定義
Build Target
。點(diǎn)擊File
菜單欄闽颇,Settings | Build, Execution, Deployment | Custom Build Targets
盾戴,點(diǎn)擊+
新建一個(gè)Target
。-
Name
:Target
的名字兵多,之后在創(chuàng)建Run/Debug
配置的時(shí)候會(huì)看到這個(gè)名字 - 點(diǎn)擊
Build
或者Clean
右邊的三點(diǎn)尖啡,彈出框中點(diǎn)擊+
新建兩個(gè)External Tool
配置如下:
第一個(gè)配置如下,用來指定構(gòu)建指令剩膘,Program 和 Arguments 共同構(gòu)成了所要執(zhí)行的命令 "make noopt"
第二個(gè)配置如下衅斩,用來清理構(gòu)建輸出,Program 和 Arguments 共同構(gòu)成了所要執(zhí)行的命令 "make clean"Name: make Program: make Arguments: noopt Working directory: $ProjectFileDir$
Name: make clean Program: make Arguments: clean Working directory: $ProjectFileDir$
-
ToolChain
選擇Default
怠褐;Build
選擇make
(上面創(chuàng)建的第一個(gè)External Tool
)畏梆;Clean
選擇make clean
(上面創(chuàng)建的第二個(gè)External Tool
)
[圖片上傳失敗...(image-eba2c8-1597664420762)]
-
-
創(chuàng)建自定義的
Run/Debug configuration
。點(diǎn)擊Run
菜單欄,Edit Configurations
奈懒, 點(diǎn)擊+
奠涌,選擇Custom Build Application
,配置如下:# Name:Configure 的名稱 Name: redis # Target:選擇上一步創(chuàng)建的 “Custom Build Target” Target: redis # Executable:程序執(zhí)行入口磷杏,也就是需要調(diào)試的程序 Executable: 這里我們調(diào)試Redis Server溜畅,選擇`{source_root}/src/redis-server`。 # Program arguments: 與 “Executable” 配合使用极祸,指定其參數(shù) Program arguments: 這里我們選擇"$ProjectFileDir$/redis.conf"作為配置文件啟動(dòng)Redis服務(wù)器
Executable
和Program arguments
可以根據(jù)需要調(diào)試的信息自行設(shè)置慈格。如果不想每次運(yùn)行/調(diào)試前都執(zhí)行
Build
操作(在這里就是Make
構(gòu)建過程),可以在編輯頁(yè)下方Before launch
框中刪除Build
條目贿肩。
[圖片上傳失敗...(image-bc48d4-1597664420762)] 點(diǎn)擊
Run
/Debug
開始運(yùn)行/調(diào)試峦椰。