如何使用 std::string 创建条件断点
假设我有这个函数:
std::string Func1(std::string myString)
{
//do some string processing
std::string newString = Func2(myString)
return newString;
}
当 newString
有特定值时,如何设置条件中断? (不更改源)
设置条件 newString == "my value"
不起作用。断点被禁用,并出现错误未找到重载运算符
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Visual Studio 2010/2012 中有一种更简单的方法。
要完成您在 ANSI 中寻找的内容,请使用以下内容:
在 unicode 中(如果 newString 是 unicode)请使用以下内容:
除了比较之外,您还可以做更多的事情,您可以在此处阅读更多相关信息:
http://blogs.msdn.com/b/habibh/archive/2009/07/07/new-visual-studio-debugger-2010-feature-for-cc-developers-using-string-functions- in-conditional-breakpoints.aspx
There is a much easier way in Visual Studio 2010/2012.
To accomplish what you are looking for in ANSI use this:
And in unicode (if newString were unicode) use this:
There are more things you can do than just a compare, you can read more about it here:
http://blogs.msdn.com/b/habibh/archive/2009/07/07/new-visual-studio-debugger-2010-feature-for-c-c-developers-using-string-functions-in-conditional-breakpoints.aspx
在VS2017中,我能够将条件设置为:
In VS2017, I was able to set the condition as:
一些搜索未能找到任何方法来做到这一点。建议的替代方案是将测试放入代码中并添加标准断点:
或者从单个字符比较构建测试。即使查看字符串中的单个字符也有点冒险;在 Visual Studio 2005 中,我必须深入研究成员变量,例如
这些方法都不是很令人满意。我们应该能够更好地访问标准库的普遍功能。
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:
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
Neither of these approaches is very satisfactory. We should have better access to a ubiquitous feature of the Standard Library.
在VS2017中你可以这样做
In VS2017 you can do
虽然我必须使用类似于 Brad 的答案的方法来解决这个问题(加上使用 DebugBreak() 从代码中直接中断),有时编辑/重新编译/重新运行一些代码要么太耗时,要么根本不可能。
幸运的是,显然可以深入了解 std::string 类的实际成员。 此处提到了一种方法 - 尽管他调用了 VS2010具体来说,您仍然可以在早期版本中手动访问单个字符。因此,如果您使用的是 2010,则可以使用漂亮的
strcmp()
函数等 (更多信息),但是如果你像我一样,仍然有 2008 年或更早的版本,你可以通过设置断点条件来想出一个破烂的、糟糕的、但实用的替代方案,例如:如果 strVar 中的前三个字符是“abc”,则中断。当然,您可以继续使用额外的字符。丑陋..但它刚刚节省了我一些时间。
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.
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: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.
VS2012:
我只是使用了下面的条件,因为
newString._Bx._Ptr
(如 OBWANDO 的回答) 引用了非法内存并且它起作用了......
VS2012:
I just used the condition below because
newString._Bx._Ptr
( as in OBWANDO's answer ) referenced illegal memoryand it worked...
@OBWANDO(几乎)有解决方案,但正如多个评论正确指出的那样,实际缓冲区取决于字符串大小;我认为 16 是门槛。在适当的缓冲区上对 strcmp 进行大小检查是可行的。
或者
@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.
or
尝试在
ubuntu18.04
下的gdb8.1
中使用strcmp
,但它不起作用:根据此答案,
strcmp
,是一个特殊的IFUNC,可以这样设置条件:太丑了,不想再做第二次了。
这个答案提供了一个更好的解决方案,它使用std::string::compare :
Tried to use
strcmp
ingdb8.1
underubuntu18.04
, but it doesn't work:According to this answer,
strcmp
, is a special IFUNC, one can setup condition like this:It's pretty ugly, don't want to do it the second time.
This answer gives a much better solution, it use std::string::compare :
在VS2015中你可以这样做
In VS2015 you can do
比较字符串比比较字符效果更好。
这种方法可行,但使用起来很不方便,而且容易出错。
Comparing string works better than comparing characters
This works, but is very inconvenient to use and error prone.
您可以使用
c_str()
将其转换为 ac 字符串,如下所示:$_streq(myStr.c_str(), "foo")
You could convert it into a c string using
c_str()
like so:$_streq(myStr.c_str(), "foo")
要在 std::string 中设置条件断点,您需要将其设置在 std::string 的实际内部成员上。您在监视窗口中看到的内容已被简化。
您可以使用
,!
后缀在监视窗口中显示变量的真实结构。在您的示例中:对于 MSVC 2015 – 2019,您可以使用:
对于不超过 15 个字符的字符串:
对于(甚至历史上)较长的字符串:
注意:
通用条件需要输入两次测试值和三次变量名称:
注意:如果您使用
std::wstring
,请使用wcscmp
而不是strcmp
代码>.查找有关 C++ 小字符串优化的更多信息 https://vorbrodt.blog/ 2019/03/30/sso-of-stdstring/ 包含用于查找字符串内部缓冲区大小的示例代码。
所有 std:string 和 std::wstring 单行版本,方便复制粘贴:
所有上述复制/粘贴示例均在 MSVC 版本 16.9.10 和 Windows 10 程序上进行了测试。
To set a conditional breakpoint in std::string you need to set it on real internal members of std::string. What you see on watch window is simplified.
You can display real structure of a variable in the watch window by using
,!
suffix. In your example:For MSVC 2015 – 2019 you can use:
For string that were never longer than 15 characters:
For (even historically) longer strings:
Beware:
Universal condition needs to put the test value twice and variable name three times:
Notes: use
wcscmp
instead ofstrcmp
if you are working withstd::wstring
.Find more info on small string optimization in C++ https://vorbrodt.blog/2019/03/30/sso-of-stdstring/ includes sample code to find size of string's internal buffer.
All std:string and std::wstring single line versions for your copy paste convenience:
All above copy/paste samples tested on MSVC version 16.9.10 and program for Windows 10.