如何使用 icc 编译器检查 gdb 中 std::vector 的内容?

发布于 2024-07-09 15:10:59 字数 406 浏览 5 评论 0原文

我想检查 gdb 中 std::vector 的内容,但我无法访问 _M_impl 因为我使用的是 icc,而不是 gcc,我该怎么做? 为了简单起见,我们假设它是一个 std::vector 。

这里有一个非常好的答案 但如果我使用 icc,这不起作用,错误消息是“没有名为 _M_impl 的成员或方法”。 这里似乎有一个不错的调试工具集,但它也依赖于 _M_impl 。

I want to examine the contents of a std::vector in gdb but I don't have access to _M_impl because I'm using icc, not gcc, how do I do it? Let's say it's a std::vector for the sake of simplicity.

There is a very nice answer here but this doesn't work if I use icc, the error message is "There is no member or method named _M_impl". There appears to be a nice debug toolset here but it also relies on _M_impl.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不乱于心 2024-07-16 15:10:59

不确定这是否适用于你的向量,但它对我有用。

#include <string>
#include <vector>

int main() {
    std::vector<std::string> vec;
    vec.push_back("Hello");
    vec.push_back("world");
    vec.push_back("!");
    return 0;
}

数据库:

(gdb) break source.cpp:8
(gdb) run
(gdb) p vec.begin()
$1 = {
   _M_current = 0x300340
}
(gdb) p $1._M_current->c_str()
$2 = 0x3002fc "Hello"
(gdb) p $1._M_current +1
$3 = (string *) 0x300344
(gdb) p $3->c_str()
$4 = 0x30032c "world"

Not sure this will work with your vector, but it worked for me.

#include <string>
#include <vector>

int main() {
    std::vector<std::string> vec;
    vec.push_back("Hello");
    vec.push_back("world");
    vec.push_back("!");
    return 0;
}

gdb:

(gdb) break source.cpp:8
(gdb) run
(gdb) p vec.begin()
$1 = {
   _M_current = 0x300340
}
(gdb) p $1._M_current->c_str()
$2 = 0x3002fc "Hello"
(gdb) p $1._M_current +1
$3 = (string *) 0x300344
(gdb) p $3->c_str()
$4 = 0x30032c "world"
还如梦归 2024-07-16 15:10:59

通常,当我在调试器中处理容器类时,我会构建对元素的引用作为局部变量,因此很容易在调试器中看到,而无需在容器实现中进行修改。

这是一个人为的例子。

vector<WeirdStructure>  myWeird;

/* push back a lot of stuff into the vector */ 

size_t z;
for (z = 0; z < myWeird.size(); z++)
{
    WeirdStructure& weird = myWeird[z];

    /* at this point weird is directly observable by the debugger */ 

    /* your code to manipulate weird goes here */  
}

这是我使用的习语。

Generally when I deal with the container classes in a debugger, I build a reference to the element, as a local variable, so it is easy to see in the debugger, without mucking about in the container implementation.

Here is a contrived example.

vector<WeirdStructure>  myWeird;

/* push back a lot of stuff into the vector */ 

size_t z;
for (z = 0; z < myWeird.size(); z++)
{
    WeirdStructure& weird = myWeird[z];

    /* at this point weird is directly observable by the debugger */ 

    /* your code to manipulate weird goes here */  
}

That is the idiom I use.

坐在坟头思考人生 2024-07-16 15:10:59

std::vector 模板保证数据连续存储。 如果获取前面元素的地址(例如,&v[0]),则可以通过 C 样式数组访问向量中的任何其他元素。 这并不要求您的调试器可以使用 STL 的源代码。


在搞乱了一些之后,看起来 v.front()v.begin() 很可能是内联的,而 GDB 没有找到它们。 我会继续寻找,但就我个人而言,我会简单地将行 int* i = &v[0] 添加到源文件中,然后在 i 上使用 GDB 命令调试时。 请注意,编译器可以自由删除死代码。 您可能需要输出 i 的值来避免这种情况,或者干脆不启动优化。

The std::vector template guarantees the data is stored contiguously. If you take the address of the front element (say, &v[0], for instance), you can access any other element in the vector through a C-style array. That doesn't require you to have the source code of the STL available to your debugger.


After messing with this some, it appears that v.front() and v.begin() are likely inlined and GDB isn't finding them. I'll keep looking, but personally I would simply add the line int* i = &v[0] to the source file, and then use GDB commands on i while debugging. Note that the compiler is free to remove that dead code. You may need to output the value of i to avoid that, or simply not crank up optimizations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文