473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

conditional breakpoints in gdb (c++)

Hi,
I am having a lot of trouble setting conditional breakpoints in gdb,
here is a simple example...

#include<string>
#include<iostream>
using namespace std;

void func(string& s){
cout << s << endl;
}

int main()
{
string a[] ={ "A", "B", "C" };
for (int i=0; i < 3 ; i++)
func(a[i]);
}
~
I am trying to break at the cout (line # 6)
in func(string&) only if s == "C", but gdb disregards it no matter
what i try to do, here it goes

(gdb) b 6 if s == "C" //does not work breaks for "A", "B", "C"
(gdb) b 6 if ( (s== "C") 0) //breaks for all "A", "B", "C"

(gdb) b 6 if s.operator==("C") 0
(gdb) b 6 if s.operator==(s,"C") 0
Error in testing breakpoint condition:
There is no member or method named operator.

(gdb) b 6 if (strcmp(s.c_str() , "C") == 0 )
No symbol "strcmp" in current context.

The only way i can possibly think of is to add source lines and clean
them up later like
if ( s == "C" ) break;
and set a breakpoint at that line!!!
There has to be a better way to do this...what am i missing here..

Thx
Digz

Mar 24 '07 #1
4 9276
On 2007-03-24 15:47, digz wrote:
Hi,
I am having a lot of trouble setting conditional breakpoints in gdb,
here is a simple example...

#include<string>
#include<iostream>
using namespace std;

void func(string& s){
cout << s << endl;
}

int main()
{
string a[] ={ "A", "B", "C" };
for (int i=0; i < 3 ; i++)
func(a[i]);
}
~
I am trying to break at the cout (line # 6)
in func(string&) only if s == "C", but gdb disregards it no matter
what i try to do, here it goes

(gdb) b 6 if s == "C" //does not work breaks for "A", "B", "C"
(gdb) b 6 if ( (s== "C") 0) //breaks for all "A", "B", "C"

(gdb) b 6 if s.operator==("C") 0
(gdb) b 6 if s.operator==(s,"C") 0
Error in testing breakpoint condition:
There is no member or method named operator.

(gdb) b 6 if (strcmp(s.c_str() , "C") == 0 )
No symbol "strcmp" in current context.

The only way i can possibly think of is to add source lines and clean
them up later like
if ( s == "C" ) break;
and set a breakpoint at that line!!!
There has to be a better way to do this...what am i missing here..
This is off-topic here since it concerns 1) debugging, which is not
defined in the C++ standard and 2) a specific implementation, next time
try a group for your debugger, gnu.gdb comes to mind.

I've no personal experience with this kind of stuff under gdb, but in
VS2005 I notices a severe performance degradation when trying to set a
condition on a breakpoint, my guess is that the debugger braked on the
specific line each time it was executed, performed the check and if it
was false resumed execution. So I would insert a bit of code that
performed the check if I were you.

--
Erik Wikström
Mar 24 '07 #2
digz wrote:
Hi,
I am having a lot of trouble setting conditional breakpoints in gdb,
here is a simple example...

#include<string>
#include<iostream>
using namespace std;

void func(string& s){
cout << s << endl;
}

int main()
{
string a[] ={ "A", "B", "C" };
for (int i=0; i < 3 ; i++)
func(a[i]);
}
~
I am trying to break at the cout (line # 6)
in func(string&) only if s == "C", but gdb disregards it no matter
what i try to do, here it goes

(gdb) b 6 if s == "C" //does not work breaks for "A", "B", "C"
(gdb) b 6 if ( (s== "C") 0) //breaks for all "A", "B", "C"

(gdb) b 6 if s.operator==("C") 0
(gdb) b 6 if s.operator==(s,"C") 0
Error in testing breakpoint condition:
There is no member or method named operator.
IIRC, the function needs to be put in quotes, or the gdb parser will not
parse it in the way you expect:

b 6 if s."operator=="("C")

but you must have used that operator or it would not have been put in
the resulting executable. Also, I'm not sure if gdb will generate "C"
(an array of two bytes) on the fly.
(gdb) b 6 if (strcmp(s.c_str() , "C") == 0 )
No symbol "strcmp" in current context.

The only way i can possibly think of is to add source lines and clean
them up later like
if ( s == "C" ) break;
The break keyword doesn't cause the gdb to break. Actually, I'm not
sure what it would do in this context. Maybe go out one scope level?
Dunno.

If you want to do something like you are suggesting, IMHO, it would
'easier' to do like this:

BREAK(s == "C");

then have in a header file somewhere:
#if !defined BREAK_NO_CHECK
# define BREAK(x) ((void)((x)?(brk()):0))
#else
# define BREAK(x) ((void)0)
#endif

and in a source file somewhere:
#if !defined BREAK_NO_CHECK
// Need something so that it doesn't get optimised out if optimisation
// are on.
// Not sure if you can make inline as I don't know what gdb will be
// able to break on the call then.
int brk() { int a=0; ++a; return a; }

#endif

and then use the gdb command:

b brk

Once the break has been tripped, use gdb 'finish' command to let the
brk() function finish and return to the caller.

NOTE: the way I defined BREAK() allows for you to use it pretty much
anywhere, though these would be a bit obscure, such as:
int a=3, b=(BREAK(a=3), 3);

// NOTE: BREAK() always returns a non-zero (i.e. TRUE) value.
for (int i=3; i<9, BREAK(i==4); ++i) {
//...
}

fn(3, (BREAK(b==3),b), 3);

Though usually, you would use it in a more normal statement syntax as I
initially described.

Hope this helps.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 24 '07 #3
Adrian Hawryluk wrote:
digz wrote:
>Hi,
I am having a lot of trouble setting conditional breakpoints in gdb,
here is a simple example...

#include<string>
#include<iostream>
using namespace std;

void func(string& s){
cout << s << endl;
}

int main()
{
string a[] ={ "A", "B", "C" };
for (int i=0; i < 3 ; i++)
func(a[i]);
}
~
I am trying to break at the cout (line # 6)
in func(string&) only if s == "C", but gdb disregards it no matter
what i try to do, here it goes

(gdb) b 6 if s == "C" //does not work breaks for "A", "B", "C"
(gdb) b 6 if ( (s== "C") 0) //breaks for all "A", "B", "C"

(gdb) b 6 if s.operator==("C") 0
(gdb) b 6 if s.operator==(s,"C") 0
Error in testing breakpoint condition:
There is no member or method named operator.

IIRC, the function needs to be put in quotes, or the gdb parser will not
parse it in the way you expect:

b 6 if s."operator=="("C")

but you must have used that operator or it would not have been put in
the resulting executable. Also, I'm not sure if gdb will generate "C"
(an array of two bytes) on the fly.
>(gdb) b 6 if (strcmp(s.c_str() , "C") == 0 )
No symbol "strcmp" in current context.

The only way i can possibly think of is to add source lines and clean
them up later like
if ( s == "C" ) break;

The break keyword doesn't cause the gdb to break. Actually, I'm not
sure what it would do in this context. Maybe go out one scope level?
Dunno.

If you want to do something like you are suggesting, IMHO, it would
'easier' to do like this:

BREAK(s == "C");

then have in a header file somewhere:
#if !defined BREAK_NO_CHECK
# define BREAK(x) ((void)((x)?(brk()):0))
#else
# define BREAK(x) ((void)0)
#endif

and in a source file somewhere:
#if !defined BREAK_NO_CHECK
// Need something so that it doesn't get optimised out if optimisation
// are on.
// Not sure if you can make inline as I don't know what gdb will be
// able to break on the call then.
Er, now that I think about that last comment RE inline some more,
inlineing from within a source file will probably only affect things
referring to brk() within the source file after the definition. So the
comment is moot.
int brk() { int a=0; ++a; return a; }

#endif

and then use the gdb command:

b brk

Once the break has been tripped, use gdb 'finish' command to let the
brk() function finish and return to the caller.

NOTE: the way I defined BREAK() allows for you to use it pretty much
anywhere, though these would be a bit obscure, such as:
int a=3, b=(BREAK(a=3), 3);

// NOTE: BREAK() always returns a non-zero (i.e. TRUE) value.
for (int i=3; i<9, BREAK(i==4); ++i) {
//...
}

fn(3, (BREAK(b==3),b), 3);

Though usually, you would use it in a more normal statement syntax as I
initially described.
I forgot to mention, you must define BREAK_NO_CHECK in your project to
strip out the excess code in the compiled binary. Make sure that the
condition you pass to BREAK has no side-effects, or you _will_ have trouble.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 24 '07 #4
On Mar 24, 12:18 pm, Adrian Hawryluk <adrian.hawryluk-at-
gmail....@nospam.comwrote:
digz wrote:
Hi,
I am having a lot of trouble settingconditionalbreakpointsingdb,
here is a simple example...
#include<string>
#include<iostream>
using namespace std;
void func(string& s){
cout << s << endl;
}
int main()
{
string a[] ={ "A", "B", "C" };
for (int i=0; i < 3 ; i++)
func(a[i]);
}
~
I am trying to break at the cout (line # 6)
in func(string&) only if s == "C", butgdbdisregards it no matter
what i try to do, here it goes
(gdb) b 6 if s == "C" //does not work breaks for "A", "B", "C"
(gdb) b 6 if ( (s== "C") 0) //breaks for all "A", "B", "C"
(gdb) b 6 if s.operator==("C") 0
(gdb) b 6 if s.operator==(s,"C") 0
Error in testing breakpoint condition:
There is no member or method named operator.

IIRC, the function needs to be put in quotes, or thegdbparser will not
parse it in the way you expect:

b 6 if s."operator=="("C")

but you must have used that operator or it would not have been put in
the resulting executable. Also, I'm not sure ifgdbwill generate "C"
(an array of two bytes) on the fly.
(gdb) b 6 if (strcmp(s.c_str() , "C") == 0 )
No symbol "strcmp" in current context.
The only way i can possibly think of is to add source lines and clean
them up later like
if ( s == "C" ) break;
say this is line# 7 in the sample code
>
The break keyword doesn't cause the gdb to break. Actually, I'm not
sure what it would do in this context. Maybe go out one scope level?
Dunno.
I meant set a breakpoint in the line number where such ifs
would be used ,it could as well be :
if ( s== "C" ) cout << "found it" ;
as in the example say the "if statement" is in line no 7 in the source
so...
(gdb) break 7
would only break if the condition s==C is satisfied ,the debug macros
you suggest seem to be an overkill..
i was essentially trying to do non intrusive debugging ,without
changing the source, but that seems not likely.
even if we had to introduce operator== somewhere so its in the symbol
table it would mean changing source recompiling etc..
Apologies for Off Topic Posting :(
Thx
Digz


Digz

Mar 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christopher J. Bottaro | last post by:
I have a script with a class in it: class Class: def f(x, y): # do something I start up the debugger like this: python /usr/lib/python2.3/pdb.py myscript.py I want to set a conditional...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
1
by: Brian Henry | last post by:
How do you do conditional break points in .net? say i want to only break if a value changes or if its equal a certain state or value... any examples of this? thanks!
2
by: MSK | last post by:
Hi, Continued to my earlier post regaring "Breakpoints are not getting hit" , I have comeup with more input this time.. Kindly give me some idea. I am a newbie to .NET, recently I installed...
1
by: jeem | last post by:
I am using ActiveState Komodo 3.5 to work on a large python 2.4 application with an extensive UI... I am attempting to debug the application and am setting breakpoints in 4 different *.py files.....
5
by: venner | last post by:
I'm having an issue with an ASP.NET website after upgrading to ASP.NET 2.0. The website makes use of a central authentication service (CAS) provided at the university I work for. Each page checks...
5
by: Jerry Spence1 | last post by:
In VB6 you could set a breakpoint when a variable had changed value. I can't find this in VB2005. Is it there somewhere? -Jerry
1
by: Jeff | last post by:
Hey How can I get a list of breakpoints in Visual Studio 2005, while the loaded solution is in Edit mode (not running or not debugging)... I'm trying to learn thousands of uncommented C# code...
2
by: =?Utf-8?B?TG9zdCBJbiBUaGUgV29vZHM=?= | last post by:
I've seen a ton of posts about similar problems, but none of the solutions were effective. I've been maintaining this VB code for 3 years, and this is the first time this has happened. It seems...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.