Linux人必看:读代码,找定义
作为powerlinux系统测试人员,经常会遇到要通过阅读代码查找问题的情况。在linux上大家一般会使用ctags与vim或emacs结合进行代码阅读。本文介绍一个找出预编译指令分支中所有函数定义的小心得。
ctags会生成一个代码中函数、变量等等标识符的索引文件。vim/emacs根据此文件可以很快地跳到相关函数、变量的定义处。这大大方便了代码的阅读。linux kernel的为此专门提供了“tags”这个target来生成kernel的ctags索引文件。如
nutlp1:/usr/src/linux-3.0.76-0.11 # make tags
GEN tags
然后用vim打开一个源文件,把光标移到想要查找其定义的函数/变量上,按“g+ctrl+]”,如果该标识符有唯一定义,vim就会自动跳到那里。否则vim会列出所有的位置让用选择。
但是最近遇到了一个特别的情况。
#ifdef CONFIG_HAVE_MEMORYLESS_NODES
/*
* N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
* It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
* Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem().
*/
DECLARE_PER_CPU(int, _numa_mem_);
#ifndef set_numa_mem
static inline void set_numa_mem(int node)
{
percpu_write(_numa_mem_, node);
}
#endif
#ifndef numa_mem_id
/* Returns the number of the nearest Node with memory */
static inline int numa_mem_id(void)
{
return __this_cpu_read(_numa_mem_);
}
#endif
#ifndef cpu_to_mem
static inline int cpu_to_mem(int cpu)
{
return per_cpu(_numa_mem_, cpu);
}
#endif
#ifndef set_cpu_numa_mem
static inline void set_cpu_numa_mem(int cpu, int node)
{
per_cpu(_numa_mem_, cpu) = node;
}
#endif
#else /* !CONFIG_HAVE_MEMORYLESS_NODES */
#ifndef numa_mem_id
/* Returns the number of the nearest Node with memory */
static inline int numa_mem_id(void)
{
return numa_node_id();
}
#endif
#ifndef cpu_to_mem
static inline int cpu_to_mem(int cpu)
{
return cpu_to_node(cpu);
}
#endif
复制代码
想查找numa_mem_id的定义,这里有两个不同的定义,分别在各自不同的预编译分支中。vim只把我带到了第一个定义处,但实际上系统使用的是恰恰是第二个定义。
这是因为ctags生成的索引使用一个vim中的搜索命令来标识位置:
$ grep numa_mem_id tags
numa_mem_id include/linux/topology.h /^static inline int numa_mem_id(void)$/;" f
此处的“/^static inline int numa_mem_id(void)$/”便是。这样作的好处是即便源文件有了些许的增减,不用重新构筑索引,还是能找到定义。坏处就是处理不了这种在同一个文件中有两个定义的情况。
全部内容点击查看
ctags会生成一个代码中函数、变量等等标识符的索引文件。vim/emacs根据此文件可以很快地跳到相关函数、变量的定义处。这大大方便了代码的阅读。linux kernel的为此专门提供了“tags”这个target来生成kernel的ctags索引文件。如
nutlp1:/usr/src/linux-3.0.76-0.11 # make tags
GEN tags
然后用vim打开一个源文件,把光标移到想要查找其定义的函数/变量上,按“g+ctrl+]”,如果该标识符有唯一定义,vim就会自动跳到那里。否则vim会列出所有的位置让用选择。
但是最近遇到了一个特别的情况。
#ifdef CONFIG_HAVE_MEMORYLESS_NODES
/*
* N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
* It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
* Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem().
*/
DECLARE_PER_CPU(int, _numa_mem_);
#ifndef set_numa_mem
static inline void set_numa_mem(int node)
{
percpu_write(_numa_mem_, node);
}
#endif
#ifndef numa_mem_id
/* Returns the number of the nearest Node with memory */
static inline int numa_mem_id(void)
{
return __this_cpu_read(_numa_mem_);
}
#endif
#ifndef cpu_to_mem
static inline int cpu_to_mem(int cpu)
{
return per_cpu(_numa_mem_, cpu);
}
#endif
#ifndef set_cpu_numa_mem
static inline void set_cpu_numa_mem(int cpu, int node)
{
per_cpu(_numa_mem_, cpu) = node;
}
#endif
#else /* !CONFIG_HAVE_MEMORYLESS_NODES */
#ifndef numa_mem_id
/* Returns the number of the nearest Node with memory */
static inline int numa_mem_id(void)
{
return numa_node_id();
}
#endif
#ifndef cpu_to_mem
static inline int cpu_to_mem(int cpu)
{
return cpu_to_node(cpu);
}
#endif
复制代码
想查找numa_mem_id的定义,这里有两个不同的定义,分别在各自不同的预编译分支中。vim只把我带到了第一个定义处,但实际上系统使用的是恰恰是第二个定义。
这是因为ctags生成的索引使用一个vim中的搜索命令来标识位置:
$ grep numa_mem_id tags
numa_mem_id include/linux/topology.h /^static inline int numa_mem_id(void)$/;" f
此处的“/^static inline int numa_mem_id(void)$/”便是。这样作的好处是即便源文件有了些许的增减,不用重新构筑索引,还是能找到定义。坏处就是处理不了这种在同一个文件中有两个定义的情况。
全部内容点击查看
...全文
相关推荐
Linux找不到ifconfig命令
### Linux找不到ifconfig命令在Linux环境中,`ifconfig`是一个非常重要的网络配置工具,用于显示或更改网络接口的配置信息。
如何查看逻辑读和物理读块数?如何解决?【Linux】
ok,以上就是我这期的Bug修复内容啦,如果还想查找更多解决方案,你可以看看我专门收集Bug及提供解决方案的专栏「Bug调优」,都是实战中碰到的Bug,希望对你有所帮助。到此,咱们下期拜拜。码字不易,如果这篇文章对你有所帮助,帮忙给bugj菌来个,您的支持就是我坚持写作分享知识点传播技术的最大动力。「猿圈奇妙屋」;
Linux:阻塞/非阻塞读终端
阻塞读终端:block指当串口输入缓冲区没有数据的时候,read函数将会阻塞在这里,移植到串口输入缓冲区中有数据可读取,read读到了需要的字节数之后,返回值为读到的字节数 #include <unistd.h> #include <stdlib.h> int main(void) { char buf[10]; int n; n = read(STDIN_FILE...
怎样读Linux内核源代码.pdf
"《怎样读Linux内核源代码》是一篇深入探讨Linux内核分析方法的文章。Linux作为一个开源操作系统,其源码的公开性吸引着无数开发者进行研究和学习。对于电脑爱好者和程序员来说,解析和理解Linu
找工作的程序员必懂的Linux.docx
对于找工作的程序员来说,掌握 Linux 技能至关重要,因为许多互联网应用和服务都部署在 Linux 平台上。


Power Linux
