[英]I want to print tensor value by lldb
我想打印buf_中的Buffer成员变量,也就是说我想p *(tensorflow::Buffer*)buf_
打印class Buffer中的成员变量。
tensorflow, 1.10.1 中的代码。
Class 关系:class TensorBuffer 是 tensor.h 中的基础 class,Buffer 是模板并在 tensor.h 中派生出 ZA2F2ED4F8DCECC2CBBDZ11。
以下是lldb的output:
frame #0: 0x0000000175abffc0
libtensorflow_framework.so`tensorflow::Tensor::Tensor(this=0x000070000cadd308, a=0x00007fd91ea61500, type=DT_STRING, shape=0x000070000cadd2f0) at tensor.cc:726:3
723 CHECK_NOTNULL(a);
724 if (shape_.num_elements() > 0 || a->ShouldAllocateEmptyTensors()) {
725 CASES(type, buf_ = new Buffer<T>(a, shape.num_elements()));
-> 726 }
(lldb) p buf_
(tensorflow::TensorBuffer *) $17 = 0x00007fd91e927c40
(lldb) p *(Buffer<std::__1::string>*)buf_
error: use of undeclared identifier 'Buffer'
error: expected '(' for function-style cast or type construction
error: expected expression
(lldb) p *(tensorflow::Buffer<std::__1::string>*)buf_
error: no member named 'Buffer' in namespace 'tensorflow'
error: expected '(' for function-style cast or type construction
error: expected expression
第 725 行解码:
switch(type)
case DataTypeToEnum<string>::value :
{
typedef string T;
buf_ = new Buffer<T>(a, shape.num_elements(), allocation_attr);
}
是的,这是从调试信息重建模板类型的问题。 DWARF 调试信息格式没有模板的抽象表示。 它只记录实例化的模板(即没有抽象的std::vector<T>
而只有std::vector<int>
、 vector<std::string>
等。
例如,从一堆特定的实例中组成一个基本的“std::string”类型,clang 需要进行转换,这并不是 lldb 被教导要做的事情,结果证明是相当棘手的。
您可以通过为要打印的类型引入 typedef 来临时解决此问题,例如:
(lldb) source list -l 1
1 #include <vector>
2 #include <string>
3 #include <stdio.h>
4
5 int
6 main()
7 {
8 using VecType = std::vector<std::string>;
9 std::vector<std::string> my_vec = {"string", "other string"};
10 void *hidden = (void *) &my_vec;
(lldb)
11 VecType *revealed = (VecType *) hidden;
12 return 0;
13 }
14
15
(lldb) expr *(std::vector<std::string> *) hidden
error: no member named 'vector' in namespace 'std'
error: expected '(' for function-style cast or type construction
error: expected expression
(lldb) expr *(VecType *) hidden
(VecType) $0 = size=2 {
[0] = "string"
[1] = "other string"
}
这显然不是一个很好的解决方案,因为您必须在代码中引入这些 typedef 才能在调试器中使用它们。 此外,您不仅需要定义它们,还需要使用它们,否则编译器不会将它们发送到调试信息中。 因此,如果我省略了上面的第 11 行,我会得到:
(lldb) expr *(VecType *) hidden
error: use of undeclared identifier 'VecType'
我们可以使用张量的 data() function 来解决这个问题。
例如p (std::__1::string *)(tensor->buf_->data())
以及为什么我们可以在 lldb 中使用 data() function?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.