简体   繁体   中英

How to navigate glibc source?

I'm trying to learn the glibc source, and I've found navigation to be quite formidable. I'm not referring to the code itself, but simply finding it: it seems to be a maze of macros and wrappers, such that just finding the actual code I want is quite tough.

Not only system dependent things, like setjmp , but even portable functions like fprintf : in each case, I struggle to find the true definition in C. It's easy to find the start, but it's usually an empty shell wrapping defines and macros.

This feels like a modern day equivalent of goto statements, with the spaghetti problem all over again.

How can I navigate glibc and find the actual implementation for lib functions?


Update

As an example, try looking up the definition of hidden_def in glibc. It's a macro taking you to hidden_def1 , which is a macro taking you to hidden_def2 , which is a macro taking you to hidden_asm , which is a macro taking you to hidden_asm1 , at which point...

Moreover, each of these macros is defined in several different files, with other #define s controlling which definition is actually invoked.

This is not unusual: it seems to be de rigeur throughout the source code. How does anyone follow it? How do the GNU developers follow it?

How can I navigate glibc and find the actual implementation for lib functions?

Type the function in the search bar at https://code.woboq.org/userspace/glibc or at https://github.com/bminor/glibc . Navigate the results manually until you find the definition.

If you want to index the project locally, use cscope, ctags, GLOBAL tags or clangd to index the project and then use that tools specific interface to search for the definition.

As an example, try looking up the definition of hidden_def in glibc

Type hidden_def glibc into google. My first hit is woboq.org https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#550 .

I use firefox. I type ctrl+f and type hidden_def . Type Enter until I find # define hidden_def at https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#550 .

Then select __hidden_ver1 and type ctrl+c ctrl+f and ctrl+v and search for it. In the web browser. I type enter until I find https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#540 . __hidden_ver2 is just below on line 542.

For most cases all you need is a browser, google, coboq.org and github.org.

It's a macro taking you to hidden_def1

There are no such macros as you mentioned, at least at the version hosted at woboq.org.

How does anyone follow it?

While IDE is a powerful help, each project is unique and requires different settings, that take time to figure out. Mostly browsing the source code is grep (or faster alternatives, like ag , very useful for big projects like glibc) and going through the result list.

Not only system dependent things, like setjmp

Developers are (shoudl be :) sane people - in most cases a function named setjmp will be in a file named setjmp.c . or setjmp.S . Or in the same directory as setjmp.h . Or inside directory named stdlib or setjmp .

Type setjmp in github search bar. https://github.com/bminor/glibc/search?q=setjmp You see there are multiple definitions for each architecture powerpc s390 etc. But files are all named setjmp . Go back. Type "Go to file" on https://github.com/bminor/glibc . Search for a file named x86/setjmp . There are 3 implementations, the most standard one seems to be https://github.com/bminor/glibc/blob/master/sysdeps/x86_64/setjmp.S .

even portable functions like fprintf

As above, saerch for file named fprintf . You quickly find https://github.com/bminor/glibc/blob/master/stdio-common/fprintf.c .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM