Cscope簡單使用教學

    Cscope在過年這幾天,花了一點時間來瞭解。發覺在Linux底下要trace code這是一定要學的工具,他能夠讓你更快速的找到你要的Symbol、Struct或Function的位置。底下簡單的說明使用Cscope的流程:

1.  Create Project Directory
2.  Create Reference file
3.  Generate the Cscope database and Execute the Cscope
4.  Search Symbol、Function、Text and global definition
5.  Exit




我用xf86-video-intel-2.14.0.tar.gz來做範例,檔案可以從下面的網址來下載。


一、Create Project Directory

         $ cd $HOME
         $ mkdir $HOME/xf86-video-intel-2.14.0
         $ cd xf86-video-intel-2.14.0
         $ wget  http://xorg.freedesktop.org/releases/individual/driver/xf86-video-intel-2.14.0.tar.gz
         $ tar zxvf  xf86-video-intel-2.14.0.tar.gz

二、Create Reference file
         
         $ find $HOME/xf86-video-intel-2.14.0/xf86-video-intel-2.14.0/src/ -name '*.h' > cscope.files 
         $ find $HOME/xf86-video-intel-2.14.0/xf86-video-intel-2.14.0/src/ -name '*.c' >> cscope.files 
         $ find /usr/include/X11/ -name '*.h' >> cscope.files
         $ find /usr/include/xorg/ -name '*.h' >> cscope.files
         
         P.S. /usr/include/X11/ 跟 /usr/include/xorg/ 因為Xorg的Driver有包含到所以我也加入Reference file中
            
三、Generate the Cscope database and Execute the Cscope
         
         $ cscope -b -q
         $ cscope

         P.S  -b 意思是 Build the cross-reference, -q意思是 Enable  fast  symbol  lookup  via an inverted index

四、Search Symbol、Function、Text and global definition

         
           你可以用上下左右鍵來選擇你要搜尋的symbol、function和text。確定好要用拿種搜尋就輸入關鍵字
例如:我在Find this C symbol 找 _ExaOffscreenArea ,如果有找到任何相符和的symbol就跟下圖一樣,Cscope會將檔案名稱、位置還有其他一些資訊給列出來。

            這時你可以用上下左右鍵來選擇你要看的檔案,再看Enter鍵Cscope預設會用vim or vi去打開這個檔案,並且帶到你要搜尋的symbol的位置。
            
            如果要離開就輸入 :q 就可以了。如果離開後,要繼續搜尋別的symbol可以按TAB鍵跳到下面搜尋的部份,如果要在回到上面搜尋的結果也是一樣按TAB鍵



五、Exit
       
        如果不想再繼續了按 Ctrl + d 離開這個程式

P.S.  如果你想要再增加或更新資料庫的話,記的重複二跟三的步驟就可以了。

留言

熱門文章