在Linux上使用netbeans调试C/C++代码时,是否可以像Visual Studio在Windows上一样"介入"本机C运行时库(以查看malloc()等的源代码?
如果没有,任何Linux IDE都可以吗?
malloc
和许多函数都有编译器的特定实现。通常您无法以这种方式访问它们的源代码。例如,在 gcc/g++ 中,malloc
以 <cstdlib>
形式声明,并在.dll
文件中作为外部函数实现。
在Visual Studio中,你可以浏览一些声明,看到一些奇怪的代码,但它们只是一些高级代码来调用真正的malloc
。你看不到malloc
的真正实现。
例如,在我步入malloc
后的测试中,我看到了下面的代码,它只是对内部函数的调用,等等......最后你什么也看不到:
extern "C" _CRTIMP void * __cdecl malloc (
size_t nSize
)
{
void *res = _nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0);
RTCCALLBACK(_RTC_Allocate_hook, (res, nSize, 0));
return res;
}
您可以在 Debian 或 Ubuntu(或派生)发行版上安装 libc6-dbg
(或 libc-dbg
)软件包。
然后使用 set debug-file-directory 命令 gdb
由于Linux是自由软件,你可以研究malloc
的源代码;你的发行版可能使用了GNU libc的一些修补变体;你也可以研究MUSL libc,它的源代码对我来说似乎更具可读性。
FWIW, malloc(3) 肯定在使用像 mmap(2) 这样的系统调用......
在许多发行版上,您可以从源代码重建打包的软件(例如,使用 apt-build
...
但是,如果您是新手,我不建议您重建libc
因为它是几乎所有应用程序的核心部分!