3

I want to browse the source code in of the symbols in my header files in /usr/include/ . For example, in netdb.h, there's a function named getaddrinfo(...). I want to know where the source code is stored at least by using the shell.

I will be more appreciate if you tell me how to do it specifically in Emacs and/or Cscope.

1
  • Please accept whatever answer fits your needs... (Your question has been understood in different ways). Dec 2, 2011 at 11:51

7 Answers 7

3

The definition (with the function body) of standard C library's functions is (usually on Linux) inside the GNU Libc. Several functions (those in the section 2 of man pages for syscalls, with syscall numbers listed in <asm/unistd.h>) are tiny wrappers to system calls, so the actual work is done inside the linux kernel whose source is on kernel.org. For instance, write(2) is a system call (mostly done in the kernel) which may be used by the printf(3) library function (whose code is in GNU Libc). You could install GNU Libc source code and use ctags to find symbols there.

The declaration of standard C library's functions are in header files under /usr/include, so utilities like ctags can help you find them.

6
  • 1
    Thanks. I upvoted everyone, since every answer is useful for me. I will try your answer soon after getting the source.
    – Amumu
    Dec 2, 2011 at 12:24
  • So, where's the actual source code for glibc stored then? In the /src, there's only source of the linux kernel.
    – Amumu
    Dec 2, 2011 at 12:39
  • I gave you a link for Glibc. On Debian and Ubuntu distributions (& others), you could also install the source code of relevant packages. ftp.gnu.org/gnu/glibc Dec 2, 2011 at 12:41
  • One more question: How can i know which header file belongs to which lib? I've just looked at the netdb.h and the comment suggests it belongs to glibc. Is there other way rather than using comment?
    – Amumu
    Dec 2, 2011 at 12:48
  • 1
    The packaging system tells you which package brings a file: on Debian or Ubuntu dpkg -S /usr/include/netdb.h should tell you. Dec 2, 2011 at 12:51
2

You can use c-tags.

C-] - go to definition
C-T - Jump back from the definition.
C-W C-] - Open the definition in a horizontal split

2
  • Well I want to browse the definition but not int the same directory but else where. Like in the case /usr/include/netdb.h , the source code ought to be elsewhere. I tried your method but it doesn't seem to be able to find the source. How can I add the search path?
    – Amumu
    Dec 2, 2011 at 8:53
  • If you want to look at the source of getaddrinfo you need to have to source of GNU Libc. See my answer for more. Dec 2, 2011 at 10:04
2

Have a look at the following text (which comes from xcscope.el) if you plan to use cscope in Emacs.

* Keybindings:

All keybindings use the "C-c s" prefix, but are usable only while
editing a source file, or in the cscope results buffer:

    C-c s s         Find symbol.
    C-c s d         Find global definition.
    C-c s g         Find global definition (alternate binding).
    C-c s G         Find global definition without prompting.
    C-c s c         Find functions calling a function.
    C-c s C         Find called functions (list functions called from a function).
    C-c s t         Find text string.
    C-c s e         Find egrep pattern.
    C-c s f         Find a file.
    C-c s i         Find files #including a file.

You can find more information in that file. The following link might also be helpful:

Before finding a symbol using Cscope, you should first create an index by C-c s I at the root directory of your code base, for example, a folder called foo. Then two files will be generated. Cscope will find all the source file contained in foo recursively, and create a list called cscope.files. Then it will use this list to create an index for all the symbols in each file, and store this information in file cscope.out. After that, just set the initial directory of cscope to foo by pressing C-c s a, telling Cscope where to find cscope.out. Then the key bindings mentioned above should work.

3
  • I got the same problem described in my comment written for Als.
    – Amumu
    Dec 2, 2011 at 8:54
  • Can you give an example on how to browse getaddrinfo(...) in netdb.h using etags or cscope. I tried it on Linux kernel source, and can't seem to find its name.
    – Amumu
    Dec 2, 2011 at 9:49
  • getaddrinfo is not a system call. It is implemented in GNU Libc and uses system services (like DNS...) Dec 2, 2011 at 10:06
1

As said before you could use etags to generate tags for the specified sources.

i.e.

find /usr/include/ -type f -name \*.h -exec etags --append -o INCLUDE_TAGS {\} \;

This will create a file named INCLUDE_TAGS which contains the location of every definition in the headers located in /usr/include/.

Then you can use it with M-. and when it asks for the TAGS file use INCLUDE_TAGS. It takes some times the first time but after it's fast.

However you'll just jump to the definition in the header not in the source, if you want to do so you'll have to download the sources and generate its TAGS.

1

It's curious that no one else mentions it, but... in order to view the source code, you have to have it. On most Unices (all but Linux), such sources are proprietary; to have access to them, you need a special license, or to work for the firm. In the case of Linux, the sources are publicly available, but typically won't be installed on the machine. If it's your machine, and you have the DVD used to install the system, the sources should be on it, usually in a separate package. If you don't have the installation DVD, or you can't install packages on the system (the case of most people using Linux professionally), you'll have to download them from the internet somewhere.

3
  • Thanks. I have the source code on my machine. But I searched wrong place for the definitions, as Basile Starynkevitch stated.
    – Amumu
    Dec 2, 2011 at 12:23
  • So, where's the actual source code for glibc stored then? In the /src, there's only source of the linux kernel. My OpenSUSE got glibc source installed through the package manager, and I downloaded linux source from www.kernel.org.
    – Amumu
    Dec 2, 2011 at 12:40
  • @Amumu I'm not sure (and I don't currently have a Linux machines with sources installed to check). I think that if the sources are installed through the package loader, as a source package, then they'll be somewhere under /src; if not, you should be able to find them at the gnu archives, download them, and put them where ever you want. (ftp.gnu.org/glibc has gzipped sources for many versions, for example.) Dec 2, 2011 at 15:28
0

More up to date way is to follow the methods in this guide that contains several methods to jump around source tree, and works with large projects that have several dozens of thousands of files.

0

If you are using emacs and etags, you can make TAGS file including header declaration.

$> etags -e -L {file_list} --c-kind=+px

p means prototype and x means external variable.

Then you can see a function's definition and prototype definition in header to hit M-.

Recent emacs has xref feature, so xref-find-definition will show 2 line for definition and prototype in header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.