how to create conditional breakpoint with std::string

纵饮孤独 提交于 2019-11-27 05:10:22

问题


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 a condition newString == "my value"

didn't work the breakpoints got disabled with an error "overloaded operator not found"


回答1:


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.




回答2:


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




回答3:


In VS2017 you can do

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



回答4:


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.




回答5:


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




回答6:


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

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



回答7:


@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



回答8:


In VS2015 you can do

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



回答9:


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'



回答10:


You could convert it into a c string using c_str() like so:

$_streq(myStr.c_str(), "foo")



来源:https://stackoverflow.com/questions/1740858/how-to-create-conditional-breakpoint-with-stdstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!