public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* conditional breakpoints for strings
@ 2007-10-24  6:51 Anitha Boyapati
  2007-10-24 11:53 ` Daniel Jacobowitz
  0 siblings, 1 reply; 7+ messages in thread
From: Anitha Boyapati @ 2007-10-24  6:51 UTC (permalink / raw)
  To: gdb


Hi,

Here's a sample program in C++ being debugged in GDB. It tries to stop if 
string matches "hello" in a breakpoint. Now I am getting a segmentation 
fault. But the same works fine for numerical comparisions(the code is 
slightly changed as int i = 10 and a conditional BP is set if i==10). Is 
this a bug or did I do something wrong ?

gdb --version
GNU gdb Red Hat Linux (5.3.90-0.20030710.40rh)
This GDB was configured as "i386-redhat-linux-gnu".
-----------------------------------------------------

 (gdb) li
1       #include <iostream>
2
3       using namespace std;
4
5       int main() {
6       string s("hello");
7       return 1;
8       }
(gdb) b hello.cc:7
Breakpoint 1 at 0x80486b0: file hello.cc, line 7.
(gdb) condition 1 ( s == "hello" )
(gdb) r
Starting program: /home/anithab/util/hello
Segmentation fault

-- 
Regards,
Anitha B
@S A N K H Y A

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional breakpoints for strings
  2007-10-24  6:51 conditional breakpoints for strings Anitha Boyapati
@ 2007-10-24 11:53 ` Daniel Jacobowitz
  2007-10-24 12:22   ` Anitha Boyapati
  0 siblings, 1 reply; 7+ messages in thread
From: Daniel Jacobowitz @ 2007-10-24 11:53 UTC (permalink / raw)
  To: Anitha Boyapati; +Cc: gdb

On Wed, Oct 24, 2007 at 12:27:38PM +0530, Anitha Boyapati wrote:
> 
> Hi,
> 
> Here's a sample program in C++ being debugged in GDB. It tries to stop if 
> string matches "hello" in a breakpoint. Now I am getting a segmentation 
> fault. But the same works fine for numerical comparisions(the code is 
> slightly changed as int i = 10 and a conditional BP is set if i==10). Is 
> this a bug or did I do something wrong ?

You probably want to use at least one temporary variable to do this
sort of thing.  GDB evaluates C++ expressions with user defined
operators by calling the operators, and it evaluates strings by
calling malloc in the program.  I believe there are three function
calls in s == "hello" - one for malloc(6), one for char * to string
conversion, and one to operator==.

I see that it's GDB segfaulting, not your program.  If this still
happens with a newer version of GDB, we could look into it.  But
I get:

Error in testing breakpoint condition:
Invalid cast.

GDB probably doesn't support the char* -> string constructor.  That's
one of the parts of C++ that's very hard to support in the debugger.

-- 
Daniel Jacobowitz
CodeSourcery

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional breakpoints for strings
  2007-10-24 11:53 ` Daniel Jacobowitz
@ 2007-10-24 12:22   ` Anitha Boyapati
  2007-10-24 12:36     ` Daniel Jacobowitz
  0 siblings, 1 reply; 7+ messages in thread
From: Anitha Boyapati @ 2007-10-24 12:22 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: gdb


Hi,

On Wed, 24 Oct 2007, Daniel Jacobowitz wrote:

> You probably want to use at least one temporary variable to do this
> sort of thing.  GDB evaluates C++ expressions with user defined

I did it with (strcmp(...) == 0). It worked  that way probably because 
strcmp() takes care of memory alloc and type casting issues. I think this 
is fine for me now. Thanks.

> operators by calling the operators, and it evaluates strings by
> calling malloc in the program.  I believe there are three function
> calls in s == "hello" - one for malloc(6), one for char * to string
> conversion, and one to operator==.
> 
> I see that it's GDB segfaulting, not your program.  If this still
> happens with a newer version of GDB, we could look into it.  But
> I get:
> 
> Error in testing breakpoint condition:
> Invalid cast.
> 
> GDB probably doesn't support the char* -> string constructor.  That's
> one of the parts of C++ that's very hard to support in the debugger.
> 
> 

This is quite interesting. Maybe I would just look into its internals.
Generally speaking, why is this char*->string so hard ? 

-- 
Regards,
Anitha B
@S A N K H Y A

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional breakpoints for strings
  2007-10-24 12:22   ` Anitha Boyapati
@ 2007-10-24 12:36     ` Daniel Jacobowitz
  2007-10-24 13:05       ` Anitha Boyapati
  2007-10-24 14:26       ` Dave Korn
  0 siblings, 2 replies; 7+ messages in thread
From: Daniel Jacobowitz @ 2007-10-24 12:36 UTC (permalink / raw)
  To: Anitha Boyapati; +Cc: gdb

On Wed, Oct 24, 2007 at 05:58:56PM +0530, Anitha Boyapati wrote:
> 
> Hi,
> 
> On Wed, 24 Oct 2007, Daniel Jacobowitz wrote:
> 
> > You probably want to use at least one temporary variable to do this
> > sort of thing.  GDB evaluates C++ expressions with user defined
> 
> I did it with (strcmp(...) == 0). It worked  that way probably because 
> strcmp() takes care of memory alloc and type casting issues. I think this 
> is fine for me now. Thanks.

If you expect the breakpoint to hit more than a few times, I still
recommend a temporary variable.

(gdb) set $str = "hello"
(gdb) cond 1 strcmp (s.whatever, $str) == 0

Otherwise you will call malloc at every breakpoint.

> This is quite interesting. Maybe I would just look into its internals.
> Generally speaking, why is this char*->string so hard ? 

Two parts.  One is that GDB does not know how to construct new
objects.  The other is that figuring out which constructors or
operators to call is complicated; do you convert std::string to
char * or char * to std::string, for instance.  The C++ language
standard has pages and pages of rules for this sort of thing.

-- 
Daniel Jacobowitz
CodeSourcery

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional breakpoints for strings
  2007-10-24 12:36     ` Daniel Jacobowitz
@ 2007-10-24 13:05       ` Anitha Boyapati
  2007-10-24 14:26       ` Dave Korn
  1 sibling, 0 replies; 7+ messages in thread
From: Anitha Boyapati @ 2007-10-24 13:05 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: gdb


Hi,

On Wed, 24 Oct 2007, Daniel Jacobowitz wrote:

> On Wed, Oct 24, 2007 at 05:58:56PM +0530, Anitha Boyapati wrote:
> > 
> > Hi,
> > 
> > On Wed, 24 Oct 2007, Daniel Jacobowitz wrote:
> > 
> > > You probably want to use at least one temporary variable to do this
> > > sort of thing.  GDB evaluates C++ expressions with user defined
> > 
> > I did it with (strcmp(...) == 0). It worked  that way probably because 
> > strcmp() takes care of memory alloc and type casting issues. I think this 
> > is fine for me now. Thanks.
> 
> If you expect the breakpoint to hit more than a few times, I still
> recommend a temporary variable.
> 
> (gdb) set $str = "hello"
> (gdb) cond 1 strcmp (s.whatever, $str) == 0
> 
> Otherwise you will call malloc at every breakpoint.

   Point taken.
> 
> > This is quite interesting. Maybe I would just look into its internals.
> > Generally speaking, why is this char*->string so hard ? 
> 
> Two parts.  One is that GDB does not know how to construct new
> objects.  The other is that figuring out which constructors or
> operators to call is complicated; do you convert std::string to
> char * or char * to std::string, for instance.  The C++ language
> standard has pages and pages of rules for this sort of thing.
> 
>  
   Thanks. That gives a basic idea.

-- 
Regards,
Anitha B
@S A N K H Y A

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: conditional breakpoints for strings
  2007-10-24 12:36     ` Daniel Jacobowitz
  2007-10-24 13:05       ` Anitha Boyapati
@ 2007-10-24 14:26       ` Dave Korn
  2007-10-24 14:35         ` 'Daniel Jacobowitz'
  1 sibling, 1 reply; 7+ messages in thread
From: Dave Korn @ 2007-10-24 14:26 UTC (permalink / raw)
  To: 'Daniel Jacobowitz', 'Anitha Boyapati'; +Cc: gdb

On 24 October 2007 13:36, Daniel Jacobowitz wrote:

>> This is quite interesting. Maybe I would just look into its internals.
>> Generally speaking, why is this char*->string so hard ?
> 
> Two parts.  One is that GDB does not know how to construct new
> objects.  The other is that figuring out which constructors or
> operators to call is complicated; do you convert std::string to
> char * or char * to std::string, for instance.  The C++ language
> standard has pages and pages of rules for this sort of thing.

  We probably want to get the compiler to do it for us then.  LTO might well
make this almost trivial, when it's ready.


    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: conditional breakpoints for strings
  2007-10-24 14:26       ` Dave Korn
@ 2007-10-24 14:35         ` 'Daniel Jacobowitz'
  0 siblings, 0 replies; 7+ messages in thread
From: 'Daniel Jacobowitz' @ 2007-10-24 14:35 UTC (permalink / raw)
  To: Dave Korn; +Cc: 'Anitha Boyapati', gdb

On Wed, Oct 24, 2007 at 03:26:37PM +0100, Dave Korn wrote:
>   We probably want to get the compiler to do it for us then.  LTO might well
> make this almost trivial, when it's ready.

Nothing like trivial, no; I've looked into this before.  Plus it
requires more information than is generally available in non-LTO
cases.

-- 
Daniel Jacobowitz
CodeSourcery

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-10-24 14:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-24  6:51 conditional breakpoints for strings Anitha Boyapati
2007-10-24 11:53 ` Daniel Jacobowitz
2007-10-24 12:22   ` Anitha Boyapati
2007-10-24 12:36     ` Daniel Jacobowitz
2007-10-24 13:05       ` Anitha Boyapati
2007-10-24 14:26       ` Dave Korn
2007-10-24 14:35         ` 'Daniel Jacobowitz'

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).