Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find header file that defines a C function

Tags:

c

header

openafs

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’
like image 449
Edward Z. Yang Avatar asked May 03 '09 04:05

Edward Z. Yang


People also ask

Where can I find C header files?

Most standard headers are stored in /usr/include . It looks like stdbool. h is stored somewhere else, and depends on which compiler you are using. For example, g++ stores it in /usr/include/c++/4.7.

Can header file contains function definition in C?

h” extension but in C, all the header files must necessarily end with the “. h” extension. A header file contains: Function definitions.

Which header file contains definition of scanf?

stdio. h is a header file which has the necessary information to include the input/output related functions in our program. Example printf, scanf etc. If we want to use printf or scanf function in our program, we should include the stdio.

Which header file contains the file manipulations function in C?

<ctype. h> This ANSI header file provides the declarations for functions that manipulate characters.


3 Answers

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
like image 181
Adam Rosenfield Avatar answered Oct 20 '22 08:10

Adam Rosenfield


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)
like image 37
ojblass Avatar answered Oct 20 '22 09:10

ojblass


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.

like image 1
Jonathan Leffler Avatar answered Oct 20 '22 09:10

Jonathan Leffler