|
Tools / 获取 c 文件函数名的几种方法 目录: 1. 方法1: grep 配合正则表达式2. 方法2: nm 类的调试工具 3. 方法3: 开源软件 ctags 4. 使用 list function 的方法简单分析 Linux Input 子系统 5. 相关参考 1. 方法1: grep 配合正则表达式 1) private 函数: $ grep -E '^(\w+ )?\w+ (\w+ )?\w+( )?\(.*)' SDL_events.c -o | sort | grep static | sortstatic 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 函数: $ grep -E '^(\w+ )?\w+ (\w+ )?\w+( )?\(.*)' SDL_events.c -o | sort | grep -v static | sortint 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) 优点:
4) 缺点:
2. 方法2: nm 类的调试工具 1) 什么是 nm? nm 用于检查二进制文件 (包括库,编译后的目标模块,共享目标文件,和独立可执行文件),并显示这些文件的内容,或存储在其中的元信息,特别是符号表。 输出格式:
另外,objdump -t 和 readelf -s 也可以查看目标文件的符号表。 2) 例子: 全局/静态 变量: 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); 静态函数: static int static_function(int x, int y){ int local_automatic_var; local_automatic_var = x + y; return local_automatic_var; } 全局函数: 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 函数: $ gcc -c c_obj.c$ nm c_obj.o | grep " t " | cut -d' ' -f3 static_function 4) public 函数: $ gcc -c c_obj.c$ nm c_obj.o | grep " T " | cut -d' ' -f3 global_function main non_mangled_function 5) 优点:
6) 缺点:
3. 方法3: 开源软件 Ctags 1) 什么是 ctags? Ctags 支持下列的编程语言: 2) private 函数: $ ctags -x SDL_events.c | grep function | tr -s ' ' | cut -d' ' -f5- | grep static | sortstatic 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 函数: $ ctags -x SDL_events.c | grep function | tr -s ' ' | cut -d' ' -f5- | grep -v static | sortint 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) 优点:
5) 缺点:
6) 我有必要了解 ctags 的内部实现吗?
4. 使用 list function 的方法简单分析 Linux Input 子系统 先是发散思维,Linux input 的整体框架大致如下:
然后慢慢专注在自己关心的内容上,例如我想多了解一下input core的内容,我就去查看它提供了什么 API: $ ctags -x input.c | grep function | tr -s ' ' | cut -d' ' -f5- | grep -v staticstruct 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) 然后就可以重点去阅读上述函数了。 5. 相关参考
|
微信公众号
手机版