查找定义 C 函数的头文件

发布于 2024-07-18 21:11:32 字数 574 浏览 5 评论 0原文

应该不难吧? 正确的?

我目前正在 OpenAFS 代码库中查找 pioctl 的标头定义。 我已经把所有的东西都扔了:检查了 ctags,grepped pioctl 的源代码,等等。我最接近的线索是有一个文件 pioctl_nt.h 包含定义,除了它实际上这不是我想要的,因为没有任何用户空间代码直接包含它,而且它是 Windows 特定的。

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

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

编辑: 直接问题已解决: pioctl() 是隐式定义的,如下所示:

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

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.

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?

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...

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寒冷纷飞旳雪 2024-07-25 21:11:32

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

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

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:

gcc -E test.c -o test.i
grep pioctl -C10 test.i
要走就滚别墨迹 2024-07-25 21:11:32

有编译器选项可以显示预处理器输出。 试试那些? 在可怕的紧要关头,我的头脑完全没有任何可能的定义,-E 选项(在大多数 C 编译器中)除了吐出预处理的代码之外什么也不做。

根据请求的信息:通常,我只是在屏幕上输出时捕获相关文件的编译,然后进行快速复制和粘贴,并将 -E 放在编译器调用之后。 结果会将预处理器输出喷到屏幕上,因此将其重定向到文件。 仔细查看该文件,因为所有宏和愚蠢的事情都已处理完毕。

最坏的情况:

  • K&R 风格原型
  • 宏隐藏了定义
  • 隐式声明(根据您的答案)

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.

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.

Worst case scenarios:

  • K&R style prototypes
  • Macros are hiding the definition
  • Implicit Declaration (per your answer)
忆梦 2024-07-25 21:11:32

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

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


在几乎同样古老的 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 (以及注释“为什么 AFS 不提供这些原型?”)
  • openafs-1.5.59/src/ sys/pioctl_nt.h
  • openafs-1.5.59/src/auth/ktc.c 包含 PIOCTL 的定义
  • openafs-1.5.59/src/sys/pioctl_nt.c 提供它的实现
  • openafs-1.5.59/src/sys /rmtsysc.c 提供了它的实现(有时是 afs_pioctl())。

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

Have you considered using cscope (available from 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.


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.

  • 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/sys/pioctl_nt.h
  • openafs-1.5.59/src/auth/ktc.c includes a define for PIOCTL
  • openafs-1.5.59/src/sys/pioctl_nt.c provides an implementation of it
  • openafs-1.5.59/src/sys/rmtsysc.c provides an implementation of it (and sometimes afs_pioctl() instead)

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.

属性 2024-07-25 21:11:32

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

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

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

不过,目前最简单的选择就是使用 libkopenafs。 为此,请包含 kopenafs.h,使用函数 k_pioctl,并链接到 -lkopenafs。 这往往是一个比尝试与 OpenAFS libsys 和其他东西链接更方便的接口。

While the original general question has been answered, if anyone arrives at this page wondering where to find a header file that defines 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.

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).

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.

执妄 2024-07-25 21:11:32

在研究了预处理器但也没有找到任何东西之后,我们决定当前的工作理论是 OpenAFS 让编译器推断函数的原型,因为它返回一个整数并接受指针、整数、指针、整数作为其参数。 我将通过自己定义来处理这个问题。

编辑:太棒了! 我找到了确凿的证据:

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

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.

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

AFS.xs:2796: error: implicit declaration of function ‘pioctl’
忘你却要生生世世 2024-07-25 21:11:32

手册页概要中通常不是这样说的吗?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文