首页 > 解决方案 > 如何在 lldb 中打印一系列 std::vector 元素?

问题描述

如何在 lldb 中打印 std::vector 中介于 1000 - 1073 之间的条目。

例如在以下代码中:

   1
   2    #include <numeric>
   3    #include <vector>
   4
   5    using namespace std;
   6
   7    int main() {
   8      vector<int> v(100000);
   9      std::iota(v.begin(), v.end(), 3);
-> 10     return 0;
   11   }
(lldb)

我想看看 v[1000] - v[1073] 中有什么

标签: lldb

解决方案


lldb 变量打印中没有子范围运算符。但是您可以使用 Python API 非常简单地完成这类事情。例如:

(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> for i in range(2,6):
...     print(lldb.frame.GetValueForVariablePath("int_vec[%d]"%(i)))
... 
(int) [2] = 3
(int) [3] = 4
(int) [4] = 5
(int) [5] = 6

您也可以编写一个小命令来轻松完成此操作。看:

https://lldb.llvm.org/use/python-reference.html#create-a-new-lldb-command-using-a-python-function

有关这样做的详细信息,以及:

https://lldb.llvm.org/python_reference/index.html

对 Python API 的一般参考。


推荐阅读