gdb 在c++中调试技巧,使能overload特性 -- set overload-resolution off

参考 https://stackoverflow.com/questions/16734783/in-gdb-i-can-call-some-class-functions-but-others-cannot-be-resolved-why

 a very simple code like std::size_t f(const std::string& s) { return s.length(); }, then in gdb p f("hello") and maybe p f(std::string("hello"))

The function f works in the code, however, (gdb) p f("hello") Attempt to take address of value not located in memory. (gdb) set overload-resolution on (gdb) p f("hsdflo") Cannot resolve function f to any overloaded instance. Using std:string in gdb resulted in an syntax error.

 

解决方法1

gdb is not a compiler, it will not do the (not-so-)nice user-defined type conversions for you. If you wish to call a function that wants a string, you need to give it a string, not a const char*.

Unfortunately, gdb cannot construct an std::string for you on the command line, again because it is not a compiler and object creation is not a simple function call.

So you will have to add a little helper function to your program, that would take a const char* and return an std::string&. Note the reference here. It cannot return by value, because then gdb will not be able to pass the result by const reference (it's not a compiler!) You can choose to return a reference to a static object, or to an object allocated on the heap. In the latter case it will leak memory, but this is not a big deal since the function is meant to be called only from the debugger anyway.

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

Then in gdb

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

should work.

 

解决方法2:success after giving the command

set overload-resolution off
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值