visual-studio 如何使用 std::string 创建条件断点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1740858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create conditional breakpoint with std::string
提问by Eli
Suppose I have this function:
假设我有这个功能:
std::string Func1(std::string myString)
{
//do some string processing
std::string newString = Func2(myString)
return newString;
}
how do I set a conditional break when newStringhas a specific value ? (without changing the source)
newString具有特定值时如何设置条件中断?(不改变来源)
setting a condition newString == "my value"
设定条件 newString == "my value"
didn't work the breakpoints got disabled with an error "overloaded operator not found"
没有工作断点被禁用,并出现错误“未找到重载运算符”
采纳答案by Brad Payne
Some searching has failed to turn up any way to do this. Suggested alternatives are to put the test in your code and add a standard breakpoint:
一些搜索未能找到任何方法来做到这一点。建议的替代方法是将测试放入您的代码中并添加一个标准断点:
if (myStr == "xyz")
{
// Set breakpoint here
}
Or to build up your test from individual character comparisons. Even looking at individual characters in the string is a bit dicey; in Visual Studio 2005 I had to dig down into the member variables like
或者从单个字符的比较中建立您的测试。即使查看字符串中的单个字符也有点冒险。在 Visual Studio 2005 中,我不得不深入研究成员变量,例如
myStr._Bx._Buf[0] == 'x' && myStr._Bx._Buf[1] == 'y' && myStr._Bx._Buf[2] == 'z'
Neither of these approaches is very satisfactory. We should have better access to a ubiquitous feature of the Standard Library.
这两种方法都不是很令人满意。我们应该可以更好地访问标准库中无处不在的功能。
回答by OBWANDO
There is a much easier way in Visual Studio 2010/2012.
Visual Studio 2010/2012 中有更简单的方法。
To accomplish what you are looking for in ANSI use this:
要完成您在 ANSI 中寻找的内容,请使用以下命令:
strcmp(newString._Bx._Ptr,"my value")==0
And in unicode (if newString were unicode) use this:
在 unicode 中(如果 newString 是 unicode)使用这个:
wcscmp(newString._Bx._Ptr, L"my value")==0
There are more things you can do than just a compare, you can read more about it here:
除了比较之外,您还可以做更多的事情,您可以在此处阅读更多相关信息:
回答by Adriel Santos
In VS2017 you can do
在 VS2017 中你可以做
strcmp(newString._Mypair._Myval2._Bx._Buf,"myvalue")==0
回答by Doug Kavendek
While I've had to work around this using something similar to Brad's answer (plus using DebugBreak()to break right from the code), sometimes editing/recompiling/re-running a bit of code is either too time consuming or just plain impossible.
虽然我不得不使用类似于 Brad 的答案的东西来解决这个问题(加上使用DebugBreak()从代码中直接中断),但有时编辑/重新编译/重新运行一些代码要么太耗时,要么根本不可能.
Luckily, it's apparently possible to spelunk into the actual members of the std::string class. One way is mentioned here-- and though he calls out VS2010 specifically, you can still access individual chars manually in earlier versions. So if you're using 2010, you can just use the nice strcmp()functions and the like (more info), but if you're like me and still have 2008 or earlier, you can come up with a raggedy, terrible, but functional alternative by setting a breakpoint conditional something like:
幸运的是,显然可以深入了解 std::string 类的实际成员。这里提到了一种方法——尽管他特别提到了 VS2010,但您仍然可以在早期版本中手动访问单个字符。因此,如果您使用的是 2010,您可以只使用不错的strcmp()功能等(更多信息),但是如果您像我一样并且仍然使用 2008 或更早版本,您可以想出一个破烂、糟糕但功能强大的替代方案通过设置断点条件类似于:
strVar._Bx._Ptr[0] == 'a' && strVar._Bx._Ptr[1] == 'b' &&
strVar._Bx._Ptr[2] == 'c'
to break if the first three characters in strVar are "abc". You can keep going with additional chars, of course. Ugly.. but it's saved me a little time just now.
如果 strVar 中的前三个字符是“abc”,则中断。当然,您可以继续使用其他字符。丑..但它刚刚为我节省了一点时间。
回答by user2989573
VS2012:
VS2012:
I just used the condition below because newString._Bx._Ptr( as in OBWANDO's answer )referenced illegal memory
我只是使用下面的条件,因为newString._Bx._Ptr(如在 OBWANDO 的回答中)引用了非法内存
strcmp( newString._Bx._Buf, "my value")==0
and it worked...
它起作用了……
回答by Rai
In VS2017, I was able to set the condition as:
在 VS2017 中,我能够将条件设置为:
strcmp(&newString[0], "my value") == 0
回答by anik3th
@OBWANDO (almost) has the solution, but as multiple comments rightly point out, the actual buffer depends on the string size; I see 16 to be the threshold. Prepending a size check to the strcmp on the appropriate buffer works.
@OBWANDO(几乎)有解决方案,但正如多条评论正确指出的那样,实际缓冲区取决于字符串大小;我认为 16 是阈值。在适当的缓冲区上对 strcmp 进行大小检查是有效的。
newString._Mysize < 16 && strcmp(newString._Bx._Buf, "test value") == 0
or
或者
newString._Mysize >= 16 && strcmp(newString._Bx._Ptr, "ultra super long test value") == 0
回答by dedowsdi
Tried to use strcmpin gdb8.1under ubuntu18.04, but it doesn't work:
尝试strcmp在gdb8.1under 中使用ubuntu18.04,但不起作用:
(ins)(gdb) p strcmp("a", "b")
= (int (*)(const char *, const char *)) 0x7ffff5179d60 <__strcmp_ssse3>
According to this answer, strcmp, is a special IFUNC, one can setup condition like this:
根据这个答案, strcmp, 是一个特殊的IFUNC,可以这样设置条件:
condition 1 __strcmp_ssse3(camera->_name.c_str(), "ping")==0
It's pretty ugly, don't want to do it the second time.
太丑了,不想第二次了。
This answergives a much better solution, it use std::string::compare:
这个答案提供了一个更好的解决方案,它使用std::string::compare:
condition 1 camera->_name.compare("ping") == 0
回答by Jas
Comparing string works better than comparing characters
比较字符串比比较字符效果更好
strcmp(name._Mypair._Myval2._Bx._Buf, "foo")==0
This works, but is very inconvenient to use and error prone.
这有效,但使用起来非常不方便且容易出错。
name._Mypair._Myval2._Bx._Buf[0] == 'f' &&
name._Mypair._Myval2._Bx._Buf[1] == '0' &&
name._Mypair._Myval2._Bx._Buf[2] == '0'
回答by Clyde Bazile
You could convert it into a c string using c_str()like so:
您可以使用c_str()如下方式将其转换为 ac 字符串:
$_streq(myStr.c_str(), "foo")
$_streq(myStr.c_str(), "foo")

