在分析内核源码的时候,会碰到各种奇怪的宏定义,凭自己脑子分析,感觉完全转不过来,不得不感叹C语言确实强大。
废话少说,下面给出一个实例
分析uboot的时候,遇到这种宏,完全懵逼
下面给出具体的解决过程
1、找到在哪里引用这个宏的
2、将宏定义和引用宏的代码放到一个C文件
3、gcc xxx.c -E -o xxx.i进行预处理
源码是这样的
/* 遇到不会解析的宏如何做? */ struct cmd_tbl_s { char *name; /* Command Name */ int maxargs; /* maximum number of arguments */ int repeatable; /* autorepeat allowed? */ /* Implementation function */ int (*cmd)(struct cmd_tbl_s *, int, int, char *[]); char *usage; /* Usage message (short) */ char *help; /* Help message (long) */ /* do auto completion on the arguments */ int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]); }; typedef struct cmd_tbl_s cmd_tbl_t; #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \ cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help} int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { int rcode = 0; if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1; if (parse_string_outer(getenv("bootcmd"), FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1; return rcode; } U_BOOT_CMD( boot, 1, 1, do_bootd, "boot - boot default, i.e., run 'bootcmd'\n", NULL ); int main(void) { return 0; }
预处理之后的代码
# 1 "test.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 1 "<command-line>" 2 # 1 "test.c" struct cmd_tbl_s { char *name; int maxargs; int repeatable; int (*cmd)(struct cmd_tbl_s *, int, int, char *[]); char *usage; char *help; int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]); }; typedef struct cmd_tbl_s cmd_tbl_t; int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { int rcode = 0; if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1; if (parse_string_outer(getenv("bootcmd"), FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1; return rcode; } cmd_tbl_t __u_boot_cmd_boot Struct_Section = {"boot", 1, 1, do_bootd, "boot - boot default, i.e., run 'bootcmd'\n", NULL} ; int main(void) { return 0; }