Tools / 獲取 c 文件函數(shù)名的幾種方法
目錄:
1. 方法1: grep 配合正則表達式
2. 方法2: nm 類的調(diào)試工具
3. 方法3: 開源軟件 ctags
4. 使用 list function 的方法簡單分析 Linux Input 子系統(tǒng)
5. 相關(guān)參考
1. 方法1: grep 配合正則表達式
1) private 函數(shù):
$ grep -E '^(\w+ )?\w+ (\w+ )?\w+( )?\(.*)' SDL_events.c -o | sort | grep static | sort
static int SDL_AddEvent(SDL_Event *event)
static int SDLCALL SDL_GobbleEvents(void *unused)
static int SDL_CutEvent(int spot)
static int SDL_StartEventThread(Uint32 flags)
static void SDL_StopEventThread(void)
2) public 函數(shù):
$ grep -E '^(\w+ )?\w+ (\w+ )?\w+( )?\(.*)' SDL_events.c -o | sort | grep -v static | sort
int SDL_PollEvent (SDL_Event *event)
...
int SDL_WaitEvent (SDL_Event *event)
SDL_EventFilter SDL_GetEventFilter(void)
Uint32 SDL_EventThreadID(void)
Uint8 SDL_EventState (Uint8 type, int state)
void SDL_Lock_EventThread(void)
...
void SDL_Unlock_EventThread(void)
3) 優(yōu)點:
-
通用性強,基本所有的 Linux 系統(tǒng)都會有 grep 命令;
-
可以看到函數(shù)的完整定義;
4) 缺點:
-
某些情景下無法完全匹配所有的函數(shù)定義,例如將函數(shù)定義分 2 行來寫的情況。
2. 方法2: nm 類的調(diào)試工具
1) 什么是 nm?
nm 用于檢查二進制文件 (包括庫,編譯后的目標(biāo)模塊,共享目標(biāo)文件,和獨立可執(zhí)行文件),并顯示這些文件的內(nèi)容,或存儲在其中的元信息,特別是符號表。
輸出格式:
-
默認(rèn)按符號字母序排序;
-
大寫字母表示全局性,小寫字母表示局部性;
-
U 表示此符號未定義,C 表示此符號是公共的,T/t 表示此符號在代碼段中,D/d 的表示此符號在初始化數(shù)據(jù)段中,B/b 表示此符號在BSS數(shù)據(jù)段中;
另外,objdump -t 和 readelf -s 也可以查看目標(biāo)文件的符號表。
2) 例子:
下面的例子展示了變量和函數(shù)在全局、局部、外部、靜態(tài)、自動和初始化的不同情況下的不同符號類型。
全局/靜態(tài) 變量:
int global_var;
int global_var_init = 26;
static int static_var;
static int static_var_init = 25;
extern int extern_var;
extern int extern_function(int);
靜態(tài)函數(shù):
static int static_function(int x, int y)
{
int local_automatic_var;
local_automatic_var = x + y;
return local_automatic_var;
}
全局函數(shù):
int global_function(int p)
{
static int local_static_var;
static int local_static_var_init=5;
local_static_var = static_function(local_static_var_init, p);
return local_static_var;
}
int main(int argc, char** argv)
{
static_var = 1;
global_var = global_function(2);
extern_var = extern_function(3);
return 0;
}
#ifdef __cplusplus
extern "C"
#endif
void non_mangled_function(void)
{
// I do nothing
}
3) private 函數(shù):
$ gcc -c c_obj.c
$ nm c_obj.o | grep " t " | cut -d' ' -f3
static_function
4) public 函數(shù):
$ gcc -c c_obj.c
$ nm c_obj.o | grep " T " | cut -d' ' -f3
global_function
main
non_mangled_function
5) 優(yōu)點:
-
可以看到整個文件的所有調(diào)試信息,不僅僅是函數(shù)列表;
-
同時適用于C 和 C++;
-
呈現(xiàn)的是編譯后的結(jié)果,更加準(zhǔn)確可靠,例如不用關(guān)心源碼中宏定義是否打開。
6) 缺點:
-
只能對編譯后的目標(biāo)文件進行查看;
-
目標(biāo)文件如果被 strip 后,則無法使用該方法。
-
無法看到完整的函數(shù)定義;
3. 方法3: 開源軟件 Ctags
1) 什么是 ctags?
Ctags 是一個用于從程序源代碼樹產(chǎn)生索引文件(或tag文件),從而便于文本編輯器來實現(xiàn)快速定位的實用工具。在產(chǎn)生的 tag 文件中,每一個 tag 的入口指向了一個編程語言的對象。這個對象可以是變量定義、函數(shù)、類或其他的物件。
Ctags 支持下列的編程語言:
匯編,AWK, ASP, BETA, Bourne/Korn/Zsh Shell, C, C++, COBOL, Eiffel, Fortran, Java, Lisp, Lua, Make, Pascal, Perl, PHP, Python, REXX, Ruby, S-Lang, Scheme, Tcl, Vim, and YACC。
2) private 函數(shù):
$ ctags -x SDL_events.c | grep function | tr -s ' ' | cut -d' ' -f5- | grep static | sort
static int SDL_AddEvent(SDL_Event *event)
static int SDLCALL SDL_GobbleEvents(void *unused)
static int SDL_CutEvent(int spot)
static int SDL_StartEventThread(Uint32 flags)
static void SDL_StopEventThread(void)
3) public 函數(shù):
$ ctags -x SDL_events.c | grep function | tr -s ' ' | cut -d' ' -f5- | grep -v static | sort
int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action,
int SDL_PollEvent (SDL_Event *event)
...
int SDL_WaitEvent (SDL_Event *event)
SDL_EventFilter SDL_GetEventFilter(void)
...
void SDL_Lock_EventThread(void)
...
void SDL_Unlock_EventThread(void)
4) 優(yōu)點:
-
支持多語言;
-
可以看到函數(shù)的完整定義;
-
Ctags 不是編譯器也不是預(yù)處理器,它的解析能力是有限的;
5) 缺點:
-
分2行定義的函數(shù)無法完整顯示:
int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action,
6) 我有必要了解 ctags 的內(nèi)部實現(xiàn)嗎?
-
并不十分清楚 Ctags 使用何種技術(shù)來解析內(nèi)容,估計包括正則表達式、詞法分析、語法分析。日后如果工作對上述知識點有需求的話,應(yīng)該也是一個很不錯的學(xué)習(xí)資料,暫時不深入學(xué)習(xí),
4. 使用 list function 的方法簡單分析 Linux Input 子系統(tǒng)
先是發(fā)散思維,Linux input 的整體框架大致如下:
然后慢慢專注在自己關(guān)心的內(nèi)容上,例如我想多了解一下input core的內(nèi)容,我就去查看它提供了什么 API:
$ ctags -x input.c | grep function | tr -s ' ' | cut -d' ' -f5- | grep -v static
struct input_dev *devm_input_allocate_device(struct device *dev)
void input_alloc_absinfo(struct input_dev *dev)
struct input_dev *input_allocate_device(void)
...
void input_free_device(struct input_dev *dev)
void input_free_minor(unsigned int minor)
...
bool input_match_device_id(const struct input_dev *dev,
int input_open_device(struct input_handle *handle)
int input_register_device(struct input_dev *dev)
int input_register_handle(struct input_handle *handle)
int input_register_handler(struct input_handler *handler)
void input_release_device(struct input_handle *handle)
...
void input_unregister_device(struct input_dev *dev)
void input_unregister_handle(struct input_handle *handle)
void input_unregister_handler(struct input_handler *handler)
然后就可以重點去閱讀上述函數(shù)了。
5. 相關(guān)參考
-
nm wiki
-
Ctags wiki
-END-
推薦閱讀
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點,不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!