【问题标题】:cast a pointer to a type in lldb将指针强制转换为 lldb 中的类型
【发布时间】:2018-05-04 03:43:17
【问题描述】:

我想调试第三方c++ lib,是否可以将指针强制转换为可打印类型?

我试过了

(lldb) expr static_cast<AGInfo*>(0x0000002fcdccc060)

但它显示错误

error: cannot cast from type 'long' to pointer type 'mxnet::Imperative::AGInfo *'

有什么办法吗?

谢谢

【问题讨论】:

    标签: c++ pointers casting lldb


    【解决方案1】:

    lldb 使用 clang 作为其表达式解析器,因此它非常严格地遵循 C++,只进行了一些修改。 clang 不允许你做你在源代码中尝试的事情:

     > cat foo.cpp
    struct Something
    {
      int first;
      int second;
    };
    
    int
    main()
    {
      Something mySomething = {10, 30};
      long ptr_val = (long) &mySomething;
      Something *some_ptr = static_cast<Something *>(ptr_val);
      return some_ptr->first;
    }
     > clang++ -g -O0 -o foo foo.cpp
    foo.cpp:12:25: error: cannot cast from type 'long' to pointer type 'Something *'
      Something *some_ptr = static_cast<Something *>(ptr_val);
                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    

    所以它在 lldb 中也不起作用。

    幸运的是,C++ 在 C 风格的转换中没有那么严格,所以相同的代码但有:

      Something *some_ptr = (Something *) ptr_val;
    

    在实际源代码中编译,并将在 lldb 表达式解析器中工作。

    【讨论】:

      猜你喜欢
      • 2021-05-08
      • 2021-09-20
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多