c++ - 获取调用函数调用的C++程序中的所有行号

标签 c++ gdb

例如,我有一个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/

相关文章:

c++ - Visual Studio 2005 中的 "Attach to Process "

c++ - 为什么 operator = 不从模板类继承

c++ - Boost Serialize 向后兼容性问题

arrays - 使用 gdb 时,如何在 Pascal 中打印数组中的部分值

c++ - 你如何模拟加速计时器的时间?

c++ - abs vs std::abs,引用说明了什么?

c - 这个内存地址包含什么?

linux - 从伪 tty 中分离一个 linux 进程,但保持 tty 运行?

memory - 这是内存地址吗?

gdb - gdb 的 stepi 命令帮助