例如,我有一个C++程序:
#include <iostream>
#include<vector>
int main() {
int a =0;
//A vector of size 10 with all values as 1
std::vector<std::size_t> v(10, 1);
assert(v.size() == 10);
return 0;
}
有没有办法找到调用函数调用的行号:
因此,我会在构造 vector v 的位置以及调用 vector 的 size() 函数时添加行号。
接受任何编程语言的实用程序,但优先考虑使用 gdb 解决方案来查找函数调用。
最佳答案
I would line numbers where vector v is constructed and when size() function of vector is invoked.
在 GDB 中没有简单的方法可以做到这一点,但您可以使用 objdump -d
获取 CALL
的地址指示。示例:使用您的程序,添加缺少的 #include <assert.h>
并构建它:
$ gcc -g t.cc -fno-pie -no-pie
$ objdump -dC a.out | grep 'call.*>::vector'
4011da: e8 f9 00 00 00 callq 4012d8 <std::vector<unsigned long, std::allocator<unsigned long> >::vector(unsigned long, unsigned long const&, std::allocator<unsigned long> const&)>
$ objdump -dC a.out | grep 'call.*>::size'
4011f2: e8 8f 01 00 00 callq 401386 <std::vector<unsigned long, std::allocator<unsigned long> >::size() const>
现在您知道了 CALL
的地址说明,您可以使用 addr2line
将它们翻译成函数/文件/行:
$ addr2line -fe a.out 4011da 4011f2
main
/tmp/t.cc:9
main
/tmp/t.cc:10 (discriminator 1)
关于c++ - 获取调用函数调用的C++程序中的所有行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58107407/