在 gdb 中,我可以调用一些类函数,但其他函数"cannot be resolved" .为什么?

In gdb, I can call some class functions, but others "cannot be resolved". Why?

本文关键字:cannot 函数 be resolved 为什么 其他 类函数 我可以 gdb 调用      更新时间:2023-10-16

我还没有在共享指针上工作过。我只知道这个概念。我正在尝试调试以下c++类中的函数,该类存储XML文件(通过xerces库读取)的数据。

// header file
class ParamNode;
typedef boost::shared_ptr<ParamNode> PtrParamNode;
class ParamNode : public boost::enable_shared_from_this<ParamNode> {
public:
   ...
   typedef enum { DEFAULT, EX, PASS, INSERT, APPEND } ActionType;
   bool hasChildren() const;
   PtrParamNode GetChildren();
   PtrParamNode Get(const std::string&  name, ActionType = DEFAULT );
protected:
   ....
   ActionType defaultAction_;
}

现在,如果我正在调试一段代码,其中我有一个指向类ParamNode的指针实例,它被称为paramNode_

PtrParamNode paramNode_;
// read xml file with xerces
paramNode_ = xerces->CreateParamNodeInstance();
// now, the xml data is stored in paramNode_.
std::string probGeo;
// this works in the code, but not in (gdb)!!
paramNode_->Get("domain")->GetValue("gt",probGeo);
cout << probGeo << endl; // <-- breakpoint HERE

使用gdb我正在检查paramNode_对象:

(gdb) p paramNode_
$29 = {px = 0x295df70, pn = {pi_ = 0x2957ac0}}
(gdb) p *paramNode_.px
$30 = {
  <boost::enable_shared_from_this<mainclass::ParamNode>> = {weak_this_ = {px = 0x295df70, pn = {pi_ = 0x2957ac0}}}, 
  _vptr.ParamNode = 0x20d5ad0 <vtable for mainclass::ParamNode+16>, 
  ...
  name_= {...}, 
  children_ = {size_ = 6, capacity_ = 8, data_ = 0x2969798},
  defaultAction_ = mainclass::ParamNode::EX, }

并打印其成员:

(gdb) ptype *paramNode_.px
type = class mainclass::ParamNode : public boost::enable_shared_from_this<mainclass::ParamNode> {
  protected:
    ...
    mainclass::ParamNode::ActionType defaultAction_;
  public:
    bool HasChildren(void) const;
    mainclass::PtrParamNode GetChildren(void);
    mainclass::PtrParamNode Get(const std::string &, mainclass::ParamNode::ActionType);

然而,我只能调用HasChildrenGetChildren函数,而从gdb调用Get会导致错误:

(gdb) p (paramNode_.px)->HasChildren()
 $7 = true
 (gdb) p (paramNode_.px)->GetChildren()
 $8 = (mainclass::ParamNodeList &) @0x295dfb8: {
   size_ = 6, 
   capacity_ = 8, 
   data_ = 0x29697a8
  }
(gdb) p (paramNode_.px)->Get("domain")
 Cannot resolve method mainclass::ParamNode::Get to any overloaded instance
(gdb) set overload-resolution off
(gdb) p (paramNode_.px)->Get("domain")
 One of the arguments you tried to pass to Get could not be converted to what the function wants.
(gdb) p (paramNode_.px)->Get("domain", (paramNode_.px).defaultAction_)
 One of the arguments you tried to pass to Get could not be converted to what the function wants.

在代码中,执行Get("domain")函数工作得很好。为什么呢?由于我对共享指针的知识有限,如果您在回答中包含解释,我将不胜感激。

gdb不是编译器,它不会为您做(不那么)好的用户定义类型转换。如果你想调用一个需要string的函数,你需要给它一个string,而不是const char*

不幸的是,gdb不能在命令行上为您构造std::string,同样因为它不是编译器,对象创建也不是简单的函数调用。

因此,您必须在程序中添加一个小辅助函数,它将接受const char*并返回std::string&。注意这里的引用。它不能按值返回,因为这样gdb将无法通过const引用传递结果(它不是编译器!)可以选择返回对静态对象的引用,也可以返回对在堆上分配的对象的引用。在后一种情况下,它会泄漏内存,但这不是什么大问题,因为该函数无论如何都只能从调试器调用。

std::string& SSS (const char* s)
{
    return *(new std::string(s));
}

gdb

gdb> p (paramNode_.px)->Get(SSS("domain"))

在这种情况下,我刚刚发出命令

set overload-resolution off

对上一个答案的补充——

gdb可能最终会学习如何进行这样的转换。现在不行;但是在改进对c++表达式解析的支持方面有积极的工作。

gdb也不理解默认参数。这在一定程度上是gdb的错误;但也有一部分是g++中的错误,它不会将它们发射到DWARF中。我认为DWARF甚至没有定义一种方式来发出非平凡的默认参数。

的答案是伟大的,但为了避免不得不编辑代码和重新编译,看看在GDB中创建c++字符串给出的解决方案。

在该解决方案中,他们演示了在堆上为std::string分配空间,然后初始化std::string以传递给他们想从gdb调用的function