我想通过lldb打印张量值

问题描述 投票:0回答:2

enter image description here

我想在buf_中打印Buffer成员变量,换句话说,我想p *(tensorflow::Buffer*)buf_打印类Buffer中的成员变量。

tensorflow中的代码,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)在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);

}
c++ tensorflow lldb
2个回答
0
投票

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

例如,从一堆特定的实例中组成clang进行强制转换所需的基本“ std :: string”类型,这并不是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'

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.