从lldb调用带有字符串参数的函数:取2

Invoking function with string argument from lldb: take 2

本文关键字:函数 参数 字符串 lldb 调用      更新时间:2023-10-16

有时,我似乎不能使用lldb来调用带有字符串参数的函数。在这个问题中,用lldb调用带字符串参数的函数:如何?,我给出了一个简单的测试文件来说明这个问题。用户回答了这个问题,以显示在这种情况下修改测试文件将如何解决问题。

但是对于更一般的情况,我仍然无法从lldb调用带有字符串参数的函数。

要设置这个,我需要显示两个文件。第一个文件就像另一个问题一样,是一个简单的类lldbtest,它定义了一个接受字符串参数的构造函数。然后,另一个文件定义一个函数,其中调试器将中断。

第一个文件(不包含#include)如下所示:

 struct lldbtest{
   int bar=5;
   lldbtest();
   lldbtest(int foo);
   lldbtest(string &fum);
 };
 lldbtest::lldbtest() { bar=6; }
 lldbtest::lldbtest(int) { bar=7; }
 lldbtest::lldbtest(string&) { bar=8; }
 int main(){
   string name="fum";
   lldbtest x,y(3);
   cout<<x.bar<<y.bar<<endl;
   int zz=nothing();
   return 0;
 }

第二个文件看起来像这样:

 using namespace std;
 int nothing(){
   cout<<"hello there"<<endl;
   int n=0,m=1,r=2;
   return m+r+n;
 }

现在,当我在第二个文件的返回语句中设置断点时,并求值:

p lldbtest(string("bar"))

我得到一个很长的错误信息,很难在这里发布,但看起来像这样:

error: no matching conversion for functional-style cast from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'lldbtest'
 note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'const lldbtest' for 1st argument
note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'lldbtest' for 1st argument
note: candidate constructor not viable: no known conversion from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'int' for 1st argument
note: candidate constructor not viable: expects an l-value for 1st argument
note: candidate constructor not viable: requires 0 arguments, but 1 was provided
error: 1 errors parsing expression

哇,错误信息比代码还长。

无论如何,我的问题是:我怎么能调用函数/构造函数与字符串参数从lldb,从一个文件以外的构造函数定义/声明,在这里?

编译用c++ -g -std=c++11。我使用这些相同的选项来链接目标文件,在Mac上也使用g++。

临时变量不能绑定到左值引用

问题是临时 string ("bar")不能绑定到左值引用,这是您希望使用的构造函数作为参数;换句话说,在这种情况下,lldb没有直接问题。

lldbtest::lldbtest(string &fum); // cannot be called with a temporary

要解决这个问题,您可以更改构造函数,或引入一个新的构造函数,它接受string const&string,这将适用于p lldbtest(string("bar"))期间传递的临时:

lldbtest::lldbtest(string const&fum); // can bind to a temporary
lldbtest::lldbtest(string fum2);      // will copy the argument, safe to use with
                                      // lvalue and rvalues (temporaries)

如果你试图在main中使用同一行代码,你将看到编译器不会接受你正在尝试做的事情:

lldbtest temporary (string ("bar")); // illegal, no suitable overload found

一个小例子

可以用下面的代码片段进一步解释:

void func (std::string&) {
  // ...
}
int main () {
  func (std::string ("bar"));
}

<一口>

foo.cpp: In function ‘int main()’:
foo.cpp:8:28: error: invalid initialization of non-const reference of type ‘std::string&
                     {aka std::basic_string<char>&}’ from an rvalue of type ‘std::string
                     {aka std::basic_string<char>}’
   func (std::string ("bar"));
                            ^
foo.cpp:3:6: note: in passing argument 1 of ‘void func(std::string&)’
 void func (std::string&) {