简体   繁体   English

如何导航 glibc 源代码?

[英]How to navigate glibc source?

I'm trying to learn the glibc source, and I've found navigation to be quite formidable.我正在尝试学习 glibc 源代码,我发现导航非常强大。 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.不仅是系统相关的东西,比如setjmp ,甚至是可移植的函数,比如fprintf :在每种情况下,我都很难在 C 中找到真正的定义。很容易找到开始,但它通常是一个空壳包装定义和宏。

This feels like a modern day equivalent of goto statements, with the spaghetti problem all over again.这感觉就像现代版的 goto 语句,意大利面问题又来了。

How can I navigate glibc and find the actual implementation for lib functions?如何导航 glibc 并找到 lib 函数的实际实现?


Update更新

As an example, try looking up the definition of hidden_def in glibc.例如,尝试在 glibc 中hidden_def的定义。 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...这是一个带你到hidden_def1的宏,这是一个带你到hidden_def2的宏,这是一个带你到hidden_asm的宏,这是一个带你到hidden_asm1的宏,此时......

Moreover, each of these macros is defined in several different files, with other #define s controlling which definition is actually invoked.此外,这些宏中的每一个都定义在几个不同的文件中,其他#define控制实际调用哪个定义。

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? GNU 开发人员如何遵循它?

How can I navigate glibc and find the actual implementation for lib functions?如何导航 glibc 并找到 lib 函数的实际实现?

Type the function in the search bar at https://code.woboq.org/userspace/glibc or at https://github.com/bminor/glibc .https://code.woboq.org/userspace/glibchttps://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.如果要在本地索引项目,请使用 cscope、ctags、GLOBAL 标签或 clangd 来索引项目,然后使用该工具特定的界面来搜索定义。

As an example, try looking up the definition of hidden_def in glibc例如,尝试在 glibc 中查找 hidden_​​def 的定义

Type hidden_def glibc into google.在 google 中hidden_def glibc My first hit is woboq.org https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#550 .我的第一个点击是 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 .我输入ctrl+f并输入hidden_def Type Enter until I find # define hidden_def at https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#550 .输入 Enter 直到我在https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#550找到# define hidden_def

Then select __hidden_ver1 and type ctrl+c ctrl+f and ctrl+v and search for it.然后选择__hidden_ver1并键入ctrl+c ctrl+fctrl+v并搜索它。 In the web browser.在网络浏览器中。 I type enter until I find https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#540 .我输入 enter 直到找到https://code.woboq.org/userspace/glibc/include/libc-symbols.h.html#540 __hidden_ver2 is just below on line 542. __hidden_ver2就在第 542 行的下方。

For most cases all you need is a browser, google, coboq.org and github.org.大多数情况下,您只需要一个浏览器、google、coboq.org 和 github.org。

It's a macro taking you to hidden_def1这是一个带你到 hidden_​​def1 的宏

There are no such macros as you mentioned, at least at the version hosted at woboq.org.没有你提到的这样的宏,至少在 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.虽然IDE是一个强大的帮助,但每个项目都是独一无二的,需要不同的设置,这需要时间来弄清楚。 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.主要浏览源代码是grep (或更快的替代品,如ag ,对于像 glibc 这样的大项目非常有用)并浏览结果列表。

Not only system dependent things, like setjmp不仅是系统相关的东西,比如 setjmp

Developers are (shoudl be :) sane people - in most cases a function named setjmp will be in a file named setjmp.c .开发人员是(应该是:) 理智的人 - 在大多数情况下,名为setjmp的函数将位于名为setjmp.c的文件中。 or setjmp.S .setjmp.S Or in the same directory as setjmp.h .或者在与setjmp.h相同的目录中。 Or inside directory named stdlib or setjmp .或者在名为stdlibsetjmp目录中。

Type setjmp in github search bar.在 github 搜索栏中输入setjmp 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 . https://github.com/bminor/glibc/search?q=setjmp你看到每个架构powerpc s390等都有多个定义。但是文件都被命名为setjmp Go back.回去。 Type "Go to file" on https://github.com/bminor/glibc .https://github.com/bminor/glibc上输入“转到文件”。 Search for a file named x86/setjmp .搜索名为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 .有 3 种实现,最标准的一种似乎是https://github.com/bminor/glibc/blob/master/sysdeps/x86_64/setjmp.S

even portable functions like fprintf甚至像 fprintf 这样的便携功能

As above, saerch for file named fprintf .如上所述,搜索名为 fprintf 的文件。 You quickly find https://github.com/bminor/glibc/blob/master/stdio-common/fprintf.c .你很快就能找到https://github.com/bminor/glibc/blob/master/stdio-common/fprintf.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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