查找定义C函数的头文件

时间:2022-08-08 15:07:20

Shouldn't be hard, right? Right?

应该不难,对吗?对?

I am currently trawling the OpenAFS codebase to find the header definition of pioctl. I've thrown everything I've got at it: checked ctags, grepped the source code for pioctl, etc. The closest I've got to a lead is the fact that there's a file pioctl_nt.h that contains the definition, except it's not actually what I want because none of the userspace code directly includes it, and it's Windows specific.

我目前正在搜索OpenAFS代码库以查找pioctl的头定义。我已经抛出了我所拥有的一切:检查了ctags,grepped pioctl的源代码等等。最接近我的领导是有一个文件pioctl_nt.h包含定义,除了它的实际上并不是我想要的,因为没有任何用户空间代码直接包含它,而且它是Windows特定的。

Now, I'm not expecting you to go and download the OpenAFS codebase and find the header file for me. I am curious, though: what are your techniques for finding the header file you need when everything else fails? What are the worst case scenarios that could cause a grep for pioctl in the codebase to not actually come up with anything that looks like a function definition?

现在,我不希望你去下载OpenAFS代码库并为我找到头文件。不过我很好奇:当其他所有方法都失败时,您找到所需头文件的技巧是什么?什么是最糟糕的情况可能会导致代码库中的pioctl的grep实际上没有出现任何看起来像函数定义的东西?

I should also note that I have access to two independent userspace programs that have done it properly, so in theory I could do an O(n) search for the function. But none of the header files pop out to me, and n is large...

我还应该注意到,我可以访问两个已经正确完成它的独立用户空间程序,因此理论上我可以对该函数进行O(n)搜索。但是没有一个头文件弹出给我,而且n很大......

Edit: The immediate issue has been resolved: pioctl() is defined implicitly, as shown by this:

编辑:即时问题已得到解决:pioctl()是隐式定义的,如下所示:

AFS.xs:2796: error: implicit declaration of function ‘pioctl’

6 个解决方案

#1


If grep -r and ctags are failing, then it's probably being defined as the result of some nasty macro(s). You can try making the simplest possible file that calls pioctl() and compiles successfully, and then preprocessing it to see what happens:

如果grep -r和ctags失败,那么它可能被定义为一些令人讨厌的宏的结果。您可以尝试制作调用pioctl()并成功编译的最简单的文件,然后对其进行预处理以查看发生的情况:

gcc -E test.c -o test.i
grep pioctl -C10 test.i

#2


There are compiler options to show the preprocessor output. Try those? In a horrible pinch where my head was completely empty of any possible definition the -E option (in most c compilers) does nothing but spew out the the preprocessed code.

有编译器选项来显示预处理器输出。试试那些?在一个可怕的捏,我的头完全没有任何可能的定义-E选项(在大多数c编译器中)除了喷出预处理的代码之外什么都不做。

Per requested information: Normally I just capture a compile of the file in question as it is output on the screen do a quick copy and paste and put the -E right after the compiler invocation. The result will spew preprocessor output to the screen so redirect it to a file. Look through that file as all of the macros and silly things are already taken care of.

每个请求的信息:通常我只是捕获有问题的文件的编译,因为它在屏幕上输出快速复制并粘贴并在编译器调用后立即放入-E。结果将预处理器输出喷射到屏幕,以便将其重定向到文件。查看该文件,因为所有的宏和愚蠢的事情都已经处理好了。

Worst case scenarios:

最坏的情况:

  • K&R style prototypes
  • K&R风格原型

  • Macros are hiding the definition
  • 宏正在隐藏定义

  • Implicit Declaration (per your answer)
  • 隐含声明(根据你的答案)

#3


Have you considered using cscope (available from SourceForge)?

您是否考虑过使用cscope(可从SourceForge获得)?

I use it on some fairly significant code sets (25,000+ files, ranging up to about 20,000 lines in a file) with good success. It takes a while to derive the file list (5-10 minutes) and longer (20-30 minutes) to build the cross-reference on an ancient Sun E450, but I find the results useful.

我在一些相当重要的代码集(25,000多个文件,文件中最多约20,000行)上使用它并取得了很好的成功。在古老的Sun E450上构建交叉引用需要一段时间才能获得文件列表(5-10分钟)和更长时间(20-30分钟),但我发现结果很有用。


On an almost equally ancient Mac (dual 1GHz PPC 32-bit processors), cscope run on the OpenAFS (1.5.59) source code comes up with quite a lot of places where the function is declared, sometimes inline in code, sometimes in headers. It took a few minutes to scan the 4949 files, generating a 58 MB cscope.out file.

在几乎同样古老的Mac(双1GHz PPC 32位处理器)上,在OpenAFS(1.5.59)源代码上运行的cscope会出现很多声明函数的地方,有时在代码中内联,有时在标题中。扫描4949文件花了几分钟,生成一个58 MB的cscope.out文件。

  • openafs-1.5.59/src/sys/sys_prototypes.h
  • openafs-1.5.59/src/aklog/aklog_main.c (along with comment "Why doesn't AFS provide these prototypes?")
  • openafs-1.5.59 / src / aklog / aklog_main.c(以及评论“为什么AFS不提供这些原型?”)

  • openafs-1.5.59/src/sys/pioctl_nt.h
  • openafs-1.5.59/src/auth/ktc.c includes a define for PIOCTL
  • openafs-1.5.59 / src / auth / ktc.c包含PIOCTL的定义

  • openafs-1.5.59/src/sys/pioctl_nt.c provides an implementation of it
  • openafs-1.5.59 / src / sys / pioctl_nt.c提供了它的实现

  • openafs-1.5.59/src/sys/rmtsysc.c provides an implementation of it (and sometimes afs_pioctl() instead)
  • openafs-1.5.59 / src / sys / rmtsysc.c提供了它的实现(有时候是afs_pioctl())

The rest of the 184 instances found seem to be uses of the function, or documentation references, or release notes, change logs, and the like.

发现的184个实例中的其余部分似乎是函数,文档引用或发行说明,更改日志等的使用。

#4


The current working theory that we've decided on, after poking at the preprocessor and not finding anything either, is that OpenAFS is letting the compiler infer the prototype of the function, since it returns an integer and takes pointer, integer, pointer, integer as its parameters. I'll be dealing with this by merely defining it myself.

我们决定在预处理器上找不到任何东西后,我们已经决定的当前工作原理是OpenAFS让编译器推断出函数的原型,因为它返回一个整数并取指针,整数,指针,整数作为其参数。我将通过自己定义它来解决这个问题。

Edit: Excellent! I've found the smoking gun:

编辑:太棒了!我找到了吸烟枪:

AFS.xs:2796: error: implicit declaration of function ‘pioctl’

#5


While the original general question has been answered, if anyone arrives at this page wondering where to find a header file that defines pioctl:

虽然原始的一般问题已得到解答,但如果有人到达此页面,想知道在哪里找到定义pioctl的头文件:

In current releases of OpenAFS (1.6.7), a protoype for pioctl is defined in sys_prototypes.h. But that the time that this question was originally asked, that file did not exist, and there was no prototype for pioctl visible from outside the OpenAFS code tree.

在当前版本的OpenAFS(1.6.7)中,pioctl的原型在sys_prototypes.h中定义。但是这个问题最初被问到的时间,该文件不存在,并且没有从OpenAFS代码树外部可见的pioctl原型。

However, most users of pioctl probably want, or are at least okay with using, lpioctl ("local" pioctl), which always issues a syscall on the local machine. There is a prototype for this in afssyscalls.h (and these days, also sys_prototypes.h).

然而,pioctl的大多数用户可能想要或者至少可以使用lpioctl(“local”pioctl),它总是在本地机器上发出系统调用。 afssyscalls.h中有一个原型(现在也是sys_prototypes.h)。

The easiest option these days, though, is just to use libkopenafs. For that, include kopenafs.h, use the function k_pioctl, and link against -lkopenafs. That tends to be a much more convenient interface than trying to link with OpenAFS libsys and other stuff.

这些天最简单的选择就是使用libkopenafs。为此,包括kopenafs.h,使用函数k_pioctl,并链接-lkopenafs。与尝试与OpenAFS libsys和其他东西链接相比,这往往是一个更方便的界面。

#6


Doesn't it usually say in the man page synopsis?

难道它通常在手册页简介中说出来吗?

#1


If grep -r and ctags are failing, then it's probably being defined as the result of some nasty macro(s). You can try making the simplest possible file that calls pioctl() and compiles successfully, and then preprocessing it to see what happens:

如果grep -r和ctags失败,那么它可能被定义为一些令人讨厌的宏的结果。您可以尝试制作调用pioctl()并成功编译的最简单的文件,然后对其进行预处理以查看发生的情况:

gcc -E test.c -o test.i
grep pioctl -C10 test.i

#2


There are compiler options to show the preprocessor output. Try those? In a horrible pinch where my head was completely empty of any possible definition the -E option (in most c compilers) does nothing but spew out the the preprocessed code.

有编译器选项来显示预处理器输出。试试那些?在一个可怕的捏,我的头完全没有任何可能的定义-E选项(在大多数c编译器中)除了喷出预处理的代码之外什么都不做。

Per requested information: Normally I just capture a compile of the file in question as it is output on the screen do a quick copy and paste and put the -E right after the compiler invocation. The result will spew preprocessor output to the screen so redirect it to a file. Look through that file as all of the macros and silly things are already taken care of.

每个请求的信息:通常我只是捕获有问题的文件的编译,因为它在屏幕上输出快速复制并粘贴并在编译器调用后立即放入-E。结果将预处理器输出喷射到屏幕,以便将其重定向到文件。查看该文件,因为所有的宏和愚蠢的事情都已经处理好了。

Worst case scenarios:

最坏的情况:

  • K&R style prototypes
  • K&R风格原型

  • Macros are hiding the definition
  • 宏正在隐藏定义

  • Implicit Declaration (per your answer)
  • 隐含声明(根据你的答案)

#3


Have you considered using cscope (available from SourceForge)?

您是否考虑过使用cscope(可从SourceForge获得)?

I use it on some fairly significant code sets (25,000+ files, ranging up to about 20,000 lines in a file) with good success. It takes a while to derive the file list (5-10 minutes) and longer (20-30 minutes) to build the cross-reference on an ancient Sun E450, but I find the results useful.

我在一些相当重要的代码集(25,000多个文件,文件中最多约20,000行)上使用它并取得了很好的成功。在古老的Sun E450上构建交叉引用需要一段时间才能获得文件列表(5-10分钟)和更长时间(20-30分钟),但我发现结果很有用。


On an almost equally ancient Mac (dual 1GHz PPC 32-bit processors), cscope run on the OpenAFS (1.5.59) source code comes up with quite a lot of places where the function is declared, sometimes inline in code, sometimes in headers. It took a few minutes to scan the 4949 files, generating a 58 MB cscope.out file.

在几乎同样古老的Mac(双1GHz PPC 32位处理器)上,在OpenAFS(1.5.59)源代码上运行的cscope会出现很多声明函数的地方,有时在代码中内联,有时在标题中。扫描4949文件花了几分钟,生成一个58 MB的cscope.out文件。

  • openafs-1.5.59/src/sys/sys_prototypes.h
  • openafs-1.5.59/src/aklog/aklog_main.c (along with comment "Why doesn't AFS provide these prototypes?")
  • openafs-1.5.59 / src / aklog / aklog_main.c(以及评论“为什么AFS不提供这些原型?”)

  • openafs-1.5.59/src/sys/pioctl_nt.h
  • openafs-1.5.59/src/auth/ktc.c includes a define for PIOCTL
  • openafs-1.5.59 / src / auth / ktc.c包含PIOCTL的定义

  • openafs-1.5.59/src/sys/pioctl_nt.c provides an implementation of it
  • openafs-1.5.59 / src / sys / pioctl_nt.c提供了它的实现

  • openafs-1.5.59/src/sys/rmtsysc.c provides an implementation of it (and sometimes afs_pioctl() instead)
  • openafs-1.5.59 / src / sys / rmtsysc.c提供了它的实现(有时候是afs_pioctl())

The rest of the 184 instances found seem to be uses of the function, or documentation references, or release notes, change logs, and the like.

发现的184个实例中的其余部分似乎是函数,文档引用或发行说明,更改日志等的使用。

#4


The current working theory that we've decided on, after poking at the preprocessor and not finding anything either, is that OpenAFS is letting the compiler infer the prototype of the function, since it returns an integer and takes pointer, integer, pointer, integer as its parameters. I'll be dealing with this by merely defining it myself.

我们决定在预处理器上找不到任何东西后,我们已经决定的当前工作原理是OpenAFS让编译器推断出函数的原型,因为它返回一个整数并取指针,整数,指针,整数作为其参数。我将通过自己定义它来解决这个问题。

Edit: Excellent! I've found the smoking gun:

编辑:太棒了!我找到了吸烟枪:

AFS.xs:2796: error: implicit declaration of function ‘pioctl’

#5


While the original general question has been answered, if anyone arrives at this page wondering where to find a header file that defines pioctl:

虽然原始的一般问题已得到解答,但如果有人到达此页面,想知道在哪里找到定义pioctl的头文件:

In current releases of OpenAFS (1.6.7), a protoype for pioctl is defined in sys_prototypes.h. But that the time that this question was originally asked, that file did not exist, and there was no prototype for pioctl visible from outside the OpenAFS code tree.

在当前版本的OpenAFS(1.6.7)中,pioctl的原型在sys_prototypes.h中定义。但是这个问题最初被问到的时间,该文件不存在,并且没有从OpenAFS代码树外部可见的pioctl原型。

However, most users of pioctl probably want, or are at least okay with using, lpioctl ("local" pioctl), which always issues a syscall on the local machine. There is a prototype for this in afssyscalls.h (and these days, also sys_prototypes.h).

然而,pioctl的大多数用户可能想要或者至少可以使用lpioctl(“local”pioctl),它总是在本地机器上发出系统调用。 afssyscalls.h中有一个原型(现在也是sys_prototypes.h)。

The easiest option these days, though, is just to use libkopenafs. For that, include kopenafs.h, use the function k_pioctl, and link against -lkopenafs. That tends to be a much more convenient interface than trying to link with OpenAFS libsys and other stuff.

这些天最简单的选择就是使用libkopenafs。为此,包括kopenafs.h,使用函数k_pioctl,并链接-lkopenafs。与尝试与OpenAFS libsys和其他东西链接相比,这往往是一个更方便的界面。

#6


Doesn't it usually say in the man page synopsis?

难道它通常在手册页简介中说出来吗?