【问题标题】:I want to print tensor value by lldb我想通过 lldb 打印张量值
【发布时间】:2019-10-24 04:20:14
【问题描述】:

enter image description here

我想打印buf_中的Buffer成员变量,也就是说, 我想p *(tensorflow::Buffer*)buf_ 打印 Buffer 类中的成员变量。

张量流中的代码,1.10.1。

类关系:类TensorBuffer是tensor.h中的基类,Buffer是tensor.cc中的模板和派生类

以下是lldb的输出:

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);

}

【问题讨论】:

  • 我重新安装 lldb 9.0.0,但有同样的问题。

标签: c++ tensorflow lldb


【解决方案1】:

是的,这是从调试信息重构模板类型的问题。 DWARF 调试信息格式没有模板的抽象表示。它只记录实例化的模板(即没有抽象的std::vector&lt;T&gt;,而只有std::vector&lt;int&gt;vector&lt;std::string&gt;等。

例如,从一堆特定的实例中构建一个基本的“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'

【讨论】:

  • 我尝试查找using xxx = Buffer&lt;string&gt;,但没有找到相关代码。
【解决方案2】:

我们可以使用张量的 data() 函数来解决这个问题。

例如p (std::__1::string *)(tensor-&gt;buf_-&gt;data()) 以及为什么我们可以在 lldb 中使用 data() 函数?

【讨论】:

    猜你喜欢
    • 2017-01-14
    • 2019-08-13
    • 2019-06-15
    • 2021-12-16
    • 2023-01-11
    • 2016-07-10
    • 2016-07-06
    • 1970-01-01
    • 2017-09-12
    相关资源
    最近更新 更多