如何使用 std::string 创建条件断点

发布于 2024-08-10 22:24:40 字数 326 浏览 6 评论 0 原文

假设我有这个函数:

std::string Func1(std::string myString)
{
   //do some string processing 
   std::string newString = Func2(myString)
   return newString;  
}

newString 有特定值时,如何设置条件中断? (不更改源)

设置条件 newString == "my value" 不起作用。断点被禁用,并出现错误未找到重载运算符

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 newString has a specific value? (without changing the source)

Setting the condition newString == "my value" didn't work. The breakpoints were disabled with an error overloaded operator not found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

寄离 2024-08-17 22:24:40

Visual Studio 2010/2012 中有一种更简单的方法。

要完成您在 ANSI 中寻找的内容,请使用以下内容:

strcmp(newString._Bx._Ptr,"my value")==0 

在 unicode 中(如果 newString 是 unicode)请使用以下内容:

wcscmp(newString._Bx._Ptr, L"my value")==0 

除了比较之外,您还可以做更多的事情,您可以在此处阅读更多相关信息:

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:

strcmp(newString._Bx._Ptr,"my value")==0 

And in unicode (if newString were unicode) use this:

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:

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

柏拉图鍀咏恒 2024-08-17 22:24:40

在VS2017中,我能够将条件设置为:

strcmp(&newString[0], "my value") == 0

In VS2017, I was able to set the condition as:

strcmp(&newString[0], "my value") == 0
晨敛清荷 2024-08-17 22:24:40

一些搜索未能找到任何方法来做到这一点。建议的替代方案是将测试放入代码中并添加标准断点:

if (myStr == "xyz")
{
    // Set breakpoint here
}

或者从单个字符比较构建测试。即使查看字符串中的单个字符也有点冒险;在 Visual Studio 2005 中,我必须深入研究成员变量,例如

myStr._Bx._Buf[0] == 'x' && myStr._Bx._Buf[1] == 'y' && myStr._Bx._Buf[2] == 'z'

这些方法都不是很令人满意。我们应该能够更好地访问标准库的普遍功能。

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

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.

再见回来 2024-08-17 22:24:40

在VS2017中你可以这样做

strcmp(newString._Mypair._Myval2._Bx._Buf,"myvalue")==0

In VS2017 you can do

strcmp(newString._Mypair._Myval2._Bx._Buf,"myvalue")==0
汹涌人海 2024-08-17 22:24:40

虽然我必须使用类似于 Brad 的答案的方法来解决这个问题(加上使用 DebugBreak() 从代码中直接中断),有时编辑/重新编译/重新运行一些代码要么太耗时,要么根本不可能。

幸运的是,显然可以深入了解 std::string 类的实际成员。 此处提到了一种方法 - 尽管他调用了 VS2010具体来说,您仍然可以在早期版本中手动访问单个字符。因此,如果您使用的是 2010,则可以使用漂亮的 strcmp() 函数等 (更多信息),但是如果你像我一样,仍然有 2008 年或更早的版本,你可以通过设置断点条件来想出一个破烂的、糟糕的、但实用的替代方案,例如:

strVar._Bx._Ptr[0] == 'a' && strVar._Bx._Ptr[1] == 'b' &&
   strVar._Bx._Ptr[2] == 'c'

如果 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:

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.

输什么也不输骨气 2024-08-17 22:24:40

VS2012:

我只是使用了下面的条件,因为 newString._Bx._Ptr (如 OBWANDO 的回答) 引用了非法内存

strcmp( newString._Bx._Buf, "my value")==0

并且它起作用了......

VS2012:

I just used the condition below because newString._Bx._Ptr ( as in OBWANDO's answer ) referenced illegal memory

strcmp( newString._Bx._Buf, "my value")==0

and it worked...

清风挽心 2024-08-17 22:24:40

@OBWANDO(几乎)有解决方案,但正如多个评论正确指出的那样,实际缓冲区取决于字符串大小;我认为 16 是门槛。在适当的缓冲区上对 strcmp 进行大小检查是可行的。

newString._Mysize < 16 && strcmp(newString._Bx._Buf, "test value") == 0

或者

newString._Mysize >= 16 && strcmp(newString._Bx._Ptr, "ultra super long test value") == 0

@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.

newString._Mysize < 16 && strcmp(newString._Bx._Buf, "test value") == 0

or

newString._Mysize >= 16 && strcmp(newString._Bx._Ptr, "ultra super long test value") == 0
彼岸花似海 2024-08-17 22:24:40

尝试在ubuntu18.04下的gdb8.1中使用strcmp,但它不起作用:

(ins)(gdb) p strcmp("a", "b")
$20 = (int (*)(const char *, const char *)) 0x7ffff5179d60 <__strcmp_ssse3>

根据此答案strcmp,是一个特殊的IFUNC,可以这样设置条件:

condition 1 __strcmp_ssse3(camera->_name.c_str(), "ping")==0

太丑了,不想再做第二次了。

这个答案提供了一个更好的解决方案,它使用std::string::compare :

condition 1 camera->_name.compare("ping") == 0

Tried to use strcmp in gdb8.1 under ubuntu18.04, but it doesn't work:

(ins)(gdb) p strcmp("a", "b")
$20 = (int (*)(const char *, const char *)) 0x7ffff5179d60 <__strcmp_ssse3>

According to this answer, strcmp, is a special IFUNC, one can setup condition like this:

condition 1 __strcmp_ssse3(camera->_name.c_str(), "ping")==0

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 :

condition 1 camera->_name.compare("ping") == 0
神经大条 2024-08-17 22:24:40

在VS2015中你可以这样做

newstring[0]=='x' && newString[1]=='y' && newString[2]=='z'

In VS2015 you can do

newstring[0]=='x' && newString[1]=='y' && newString[2]=='z'
才能让你更想念 2024-08-17 22:24:40

比较字符串比比较字符效果更好。

strcmp(name._Mypair._Myval2._Bx._Buf, "foo")==0

这种方法可行,但使用起来很不方便,而且容易出错。

name._Mypair._Myval2._Bx._Buf[0] == 'f' && 
name._Mypair._Myval2._Bx._Buf[1] == '0' && 
name._Mypair._Myval2._Bx._Buf[2] == '0'

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'
帅气称霸 2024-08-17 22:24:40

您可以使用 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")

浪漫人生路 2024-08-17 22:24:40

要在 std::string 中设置条件断点,您需要将其设置在 std::string 的实际内部成员上。您在监视窗口中看到的内容已被简化。

您可以使用 ,! 后缀在监视窗口中显示变量的真实结构。在您的示例中:

newString,!

对于 MSVC 2015 – 2019,您可以使用:

对于不超过 15 个字符的字符串:

(newString._Mypair._Myval2._Myres < 16) ?
    strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 : 
    false

对于(甚至历史上)较长的字符串:

(newString._Mypair._Myval2._Myres < 16) ? false : 
     strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0

注意:

  • 变量名称每个条件都写了两次!
  • 您需要在单行上完整表达。使用下面的复制粘贴版本。

通用条件需要输入两次测试值和三次变量名称:

(newString._Mypair._Myval2._Myres < 16) ? 
    strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 : 
    strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0

注意:如果您使用 std::wstring,请使用 wcscmp 而不是 strcmp代码>.

查找有关 C++ 小字符串优化的更多信息 https://vorbrodt.blog/ 2019/03/30/sso-of-stdstring/ 包含用于查找字符串内部缓冲区大小的示例代码。

所有 std:string 和 std::wstring 单行版本,方便复制粘贴:

(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 : false

(newString._Mypair._Myval2._Myres < 16) ? false : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0

(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0


(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"short") == 0 : false

(newString._Mypair._Myval2._Myres < 16) ? false : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_str_value_longer_than_16_chars") == 0

(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"My_test_string") == 0 : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_string") == 0

所有上述复制/粘贴示例均在 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:

newString,!

For MSVC 2015 – 2019 you can use:

For string that were never longer than 15 characters:

(newString._Mypair._Myval2._Myres < 16) ?
    strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 : 
    false

For (even historically) longer strings:

(newString._Mypair._Myval2._Myres < 16) ? false : 
     strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0

Beware:

  • The variable name is written twice in each condition!
  • You need whole expression on single line. Use the copy-paste versions bellow.

Universal condition needs to put the test value twice and variable name three times:

(newString._Mypair._Myval2._Myres < 16) ? 
    strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 : 
    strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0

Notes: use wcscmp instead of strcmp if you are working with std::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:

(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "short") == 0 : false

(newString._Mypair._Myval2._Myres < 16) ? false : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_str_value_longer_than_16_chars") == 0

(newString._Mypair._Myval2._Myres < 16) ? strcmp(newString._Mypair._Myval2._Bx._Buf, "My_test_string") == 0 : strcmp(newString._Mypair._Myval2._Bx._Ptr, "My_test_string") == 0


(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"short") == 0 : false

(newString._Mypair._Myval2._Myres < 16) ? false : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_str_value_longer_than_16_chars") == 0

(newString._Mypair._Myval2._Myres < 16) ? wcscmp(newString._Mypair._Myval2._Bx._Buf, L"My_test_string") == 0 : wcscmp(newString._Mypair._Myval2._Bx._Ptr, L"My_test_string") == 0

All above copy/paste samples tested on MSVC version 16.9.10 and program for Windows 10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文