[2/3] gdb/breakpoint: set the condition exp after parsing the condition successfully

Message ID abbaf608231cfc4e65471f4efa115854dc455d52.1593438119.git.tankut.baris.aktemur@intel.com
State New
Headers show
Series
  • Prevent bad conditions from putting breakpoints into broken state
Related show

Commit Message

Jose E. Marchesi via Gdb-patches June 29, 2020, 1:48 p.m.
In 'set_breakpoint_condition', GDB resets the condition expressions
before parsing the condition input by the user.  This leads to the
problem of losing the condition expressions if the new condition
does not parse successfully and is thus rejected.

For instance:

  $ gdb ./test
  Reading symbols from ./test...
  (gdb) start
  Temporary breakpoint 1 at 0x114e: file test.c, line 4.
  Starting program: test

  Temporary breakpoint 1, main () at test.c:4
  4         int a = 10;
  (gdb) break 5
  Breakpoint 2 at 0x555555555155: file test.c, line 5.

Now define a condition that would evaluate to false.  Next, attempt
to overwrite that with an invalid condition:

  (gdb) cond 2 a == 999
  (gdb) cond 2 gibberish
  No symbol "gibberish" in current context.
  (gdb) info breakpoints
  Num     Type           Disp Enb Address            What
  2       breakpoint     keep y   0x0000555555555155 in main at test.c:5
          stop only if a == 999

It appears as if the bad condition is successfully rejected.  But if we
resume the program, we see that we hit the breakpoint although the condition
would evaluate to false.

  (gdb) continue
  Continuing.

  Breakpoint 2, main () at test.c:5
  5         a = a + 1; /* break-here */

Fix the problem by not resetting the condition expressions before
parsing the condition input.

Suppose the fix is applied.  A similar problem could occur if the
condition is valid, but has "junk" at the end.  In this case, parsing
succeeds, but an error is raised immediately after.  It is too late,
though; the condition expression is already updated.

For instance:

  $ gdb ./test
  Reading symbols from ./test...
  (gdb) start
  Temporary breakpoint 1 at 0x114e: file test.c, line 4.
  Starting program: test

  Temporary breakpoint 1, main () at test.c:4
  4         int a = 10;
  (gdb) break 5
  Breakpoint 2 at 0x555555555155: file test.c, line 5.
  (gdb) cond 2 a == 999
  (gdb) cond 2 a == 10 if
  Junk at end of expression
  (gdb) info breakpoints
  Num     Type           Disp Enb Address            What
  2       breakpoint     keep y   0x0000555555555155 in main at test.c:5
          stop only if a == 999
  (gdb) c
  Continuing.

  Breakpoint 2, main () at test.c:5
  5         a = a + 1; /* break-here */
  (gdb)

We should not have hit the breakpoint because the condition would
evaluate to false.

Fix this problem by updating the condition expression of the breakpoint
after parsing the input successfully and checking that there is no
remaining junk.

gdb/ChangeLog:
2020-06-29  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

	* breakpoint.c (set_breakpoint_condition): Update the condition
	expressions after checking that the input condition string parses
	successfully and does not contain junk at the end.

gdb/testsuite/ChangeLog:
2020-06-29  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

	* gdb.base/condbreak-bad.exp: Extend the test with scenarios
	that attempt to overwrite and existing condition with a condition
	that fails parsing and also with a condition that parses fine
	but contains junk at the end.
---
 gdb/breakpoint.c                         | 46 +++++++------
 gdb/testsuite/gdb.base/condbreak-bad.exp | 88 ++++++++++++++++++++++++
 2 files changed, 112 insertions(+), 22 deletions(-)

-- 
2.17.1

Comments

Simon Marchi July 22, 2020, 1:21 p.m. | #1
On 2020-06-29 9:48 a.m., Tankut Baris Aktemur via Gdb-patches wrote:
> In 'set_breakpoint_condition', GDB resets the condition expressions

> before parsing the condition input by the user.  This leads to the

> problem of losing the condition expressions if the new condition

> does not parse successfully and is thus rejected.

> 

> For instance:

> 

>   $ gdb ./test

>   Reading symbols from ./test...

>   (gdb) start

>   Temporary breakpoint 1 at 0x114e: file test.c, line 4.

>   Starting program: test

> 

>   Temporary breakpoint 1, main () at test.c:4

>   4         int a = 10;

>   (gdb) break 5

>   Breakpoint 2 at 0x555555555155: file test.c, line 5.

> 

> Now define a condition that would evaluate to false.  Next, attempt

> to overwrite that with an invalid condition:

> 

>   (gdb) cond 2 a == 999

>   (gdb) cond 2 gibberish

>   No symbol "gibberish" in current context.

>   (gdb) info breakpoints

>   Num     Type           Disp Enb Address            What

>   2       breakpoint     keep y   0x0000555555555155 in main at test.c:5

>           stop only if a == 999

> 

> It appears as if the bad condition is successfully rejected.  But if we

> resume the program, we see that we hit the breakpoint although the condition

> would evaluate to false.

> 

>   (gdb) continue

>   Continuing.

> 

>   Breakpoint 2, main () at test.c:5

>   5         a = a + 1; /* break-here */

> 

> Fix the problem by not resetting the condition expressions before

> parsing the condition input.

> 

> Suppose the fix is applied.  A similar problem could occur if the

> condition is valid, but has "junk" at the end.  In this case, parsing

> succeeds, but an error is raised immediately after.  It is too late,

> though; the condition expression is already updated.

> 

> For instance:

> 

>   $ gdb ./test

>   Reading symbols from ./test...

>   (gdb) start

>   Temporary breakpoint 1 at 0x114e: file test.c, line 4.

>   Starting program: test

> 

>   Temporary breakpoint 1, main () at test.c:4

>   4         int a = 10;

>   (gdb) break 5

>   Breakpoint 2 at 0x555555555155: file test.c, line 5.

>   (gdb) cond 2 a == 999

>   (gdb) cond 2 a == 10 if

>   Junk at end of expression

>   (gdb) info breakpoints

>   Num     Type           Disp Enb Address            What

>   2       breakpoint     keep y   0x0000555555555155 in main at test.c:5

>           stop only if a == 999

>   (gdb) c

>   Continuing.

> 

>   Breakpoint 2, main () at test.c:5

>   5         a = a + 1; /* break-here */

>   (gdb)

> 

> We should not have hit the breakpoint because the condition would

> evaluate to false.

> 

> Fix this problem by updating the condition expression of the breakpoint

> after parsing the input successfully and checking that there is no

> remaining junk.

> 

> gdb/ChangeLog:

> 2020-06-29  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

> 

> 	* breakpoint.c (set_breakpoint_condition): Update the condition

> 	expressions after checking that the input condition string parses

> 	successfully and does not contain junk at the end.

> 

> gdb/testsuite/ChangeLog:

> 2020-06-29  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

> 

> 	* gdb.base/condbreak-bad.exp: Extend the test with scenarios

> 	that attempt to overwrite and existing condition with a condition

> 	that fails parsing and also with a condition that parses fine

> 	but contains junk at the end.

> ---

>  gdb/breakpoint.c                         | 46 +++++++------

>  gdb/testsuite/gdb.base/condbreak-bad.exp | 88 ++++++++++++++++++++++++

>  2 files changed, 112 insertions(+), 22 deletions(-)

> 

> diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c

> index 1fc2d1b8966..abda470fe21 100644

> --- a/gdb/breakpoint.c

> +++ b/gdb/breakpoint.c

> @@ -834,30 +834,30 @@ void

>  set_breakpoint_condition (struct breakpoint *b, const char *exp,

>  			  int from_tty)

>  {

> -  if (is_watchpoint (b))

> -    {

> -      struct watchpoint *w = (struct watchpoint *) b;

> -

> -      w->cond_exp.reset ();

> -    }

> -  else

> +  if (*exp == 0)

>      {

> -      struct bp_location *loc;

> +      xfree (b->cond_string);

> +      b->cond_string = nullptr;

>  

> -      for (loc = b->loc; loc; loc = loc->next)

> +      if (is_watchpoint (b))

>  	{

> -	  loc->cond.reset ();

> +	  struct watchpoint *w = (struct watchpoint *) b;

>  

> -	  /* No need to free the condition agent expression

> -	     bytecode (if we have one).  We will handle this

> -	     when we go through update_global_location_list.  */

> +	  w->cond_exp.reset ();

>  	}

> -    }

> +      else

> +	{

> +	  struct bp_location *loc;

>  

> -  if (*exp == 0)

> -    {

> -      xfree (b->cond_string);

> -      b->cond_string = nullptr;

> +	  for (loc = b->loc; loc; loc = loc->next)

> +	    {

> +	      loc->cond.reset ();

> +

> +	      /* No need to free the condition agent expression

> +		 bytecode (if we have one).  We will handle this

> +		 when we go through update_global_location_list.  */

> +	    }

> +	}


Since you touch this, might as well declare the `bp_location *loc` in the for loop
and use `loc != nullptr`.

Otherwise, this LGTM.

Simon
Simon Marchi July 22, 2020, 1:28 p.m. | #2
On 2020-07-22 9:21 a.m., Simon Marchi wrote:
> On 2020-06-29 9:48 a.m., Tankut Baris Aktemur via Gdb-patches wrote:

>> In 'set_breakpoint_condition', GDB resets the condition expressions

>> before parsing the condition input by the user.  This leads to the

>> problem of losing the condition expressions if the new condition

>> does not parse successfully and is thus rejected.

>>

>> For instance:

>>

>>   $ gdb ./test

>>   Reading symbols from ./test...

>>   (gdb) start

>>   Temporary breakpoint 1 at 0x114e: file test.c, line 4.

>>   Starting program: test

>>

>>   Temporary breakpoint 1, main () at test.c:4

>>   4         int a = 10;

>>   (gdb) break 5

>>   Breakpoint 2 at 0x555555555155: file test.c, line 5.

>>

>> Now define a condition that would evaluate to false.  Next, attempt

>> to overwrite that with an invalid condition:

>>

>>   (gdb) cond 2 a == 999

>>   (gdb) cond 2 gibberish

>>   No symbol "gibberish" in current context.

>>   (gdb) info breakpoints

>>   Num     Type           Disp Enb Address            What

>>   2       breakpoint     keep y   0x0000555555555155 in main at test.c:5

>>           stop only if a == 999

>>

>> It appears as if the bad condition is successfully rejected.  But if we

>> resume the program, we see that we hit the breakpoint although the condition

>> would evaluate to false.

>>

>>   (gdb) continue

>>   Continuing.

>>

>>   Breakpoint 2, main () at test.c:5

>>   5         a = a + 1; /* break-here */

>>

>> Fix the problem by not resetting the condition expressions before

>> parsing the condition input.

>>

>> Suppose the fix is applied.  A similar problem could occur if the

>> condition is valid, but has "junk" at the end.  In this case, parsing

>> succeeds, but an error is raised immediately after.  It is too late,

>> though; the condition expression is already updated.

>>

>> For instance:

>>

>>   $ gdb ./test

>>   Reading symbols from ./test...

>>   (gdb) start

>>   Temporary breakpoint 1 at 0x114e: file test.c, line 4.

>>   Starting program: test

>>

>>   Temporary breakpoint 1, main () at test.c:4

>>   4         int a = 10;

>>   (gdb) break 5

>>   Breakpoint 2 at 0x555555555155: file test.c, line 5.

>>   (gdb) cond 2 a == 999

>>   (gdb) cond 2 a == 10 if

>>   Junk at end of expression

>>   (gdb) info breakpoints

>>   Num     Type           Disp Enb Address            What

>>   2       breakpoint     keep y   0x0000555555555155 in main at test.c:5

>>           stop only if a == 999

>>   (gdb) c

>>   Continuing.

>>

>>   Breakpoint 2, main () at test.c:5

>>   5         a = a + 1; /* break-here */

>>   (gdb)

>>

>> We should not have hit the breakpoint because the condition would

>> evaluate to false.

>>

>> Fix this problem by updating the condition expression of the breakpoint

>> after parsing the input successfully and checking that there is no

>> remaining junk.

>>

>> gdb/ChangeLog:

>> 2020-06-29  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

>>

>> 	* breakpoint.c (set_breakpoint_condition): Update the condition

>> 	expressions after checking that the input condition string parses

>> 	successfully and does not contain junk at the end.

>>

>> gdb/testsuite/ChangeLog:

>> 2020-06-29  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

>>

>> 	* gdb.base/condbreak-bad.exp: Extend the test with scenarios

>> 	that attempt to overwrite and existing condition with a condition

>> 	that fails parsing and also with a condition that parses fine

>> 	but contains junk at the end.

>> ---

>>  gdb/breakpoint.c                         | 46 +++++++------

>>  gdb/testsuite/gdb.base/condbreak-bad.exp | 88 ++++++++++++++++++++++++

>>  2 files changed, 112 insertions(+), 22 deletions(-)

>>

>> diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c

>> index 1fc2d1b8966..abda470fe21 100644

>> --- a/gdb/breakpoint.c

>> +++ b/gdb/breakpoint.c

>> @@ -834,30 +834,30 @@ void

>>  set_breakpoint_condition (struct breakpoint *b, const char *exp,

>>  			  int from_tty)

>>  {

>> -  if (is_watchpoint (b))

>> -    {

>> -      struct watchpoint *w = (struct watchpoint *) b;

>> -

>> -      w->cond_exp.reset ();

>> -    }

>> -  else

>> +  if (*exp == 0)

>>      {

>> -      struct bp_location *loc;

>> +      xfree (b->cond_string);

>> +      b->cond_string = nullptr;

>>  

>> -      for (loc = b->loc; loc; loc = loc->next)

>> +      if (is_watchpoint (b))

>>  	{

>> -	  loc->cond.reset ();

>> +	  struct watchpoint *w = (struct watchpoint *) b;

>>  

>> -	  /* No need to free the condition agent expression

>> -	     bytecode (if we have one).  We will handle this

>> -	     when we go through update_global_location_list.  */

>> +	  w->cond_exp.reset ();

>>  	}

>> -    }

>> +      else

>> +	{

>> +	  struct bp_location *loc;

>>  

>> -  if (*exp == 0)

>> -    {

>> -      xfree (b->cond_string);

>> -      b->cond_string = nullptr;

>> +	  for (loc = b->loc; loc; loc = loc->next)

>> +	    {

>> +	      loc->cond.reset ();

>> +

>> +	      /* No need to free the condition agent expression

>> +		 bytecode (if we have one).  We will handle this

>> +		 when we go through update_global_location_list.  */

>> +	    }

>> +	}

> 

> Since you touch this, might as well declare the `bp_location *loc` in the for loop

> and use `loc != nullptr`.


Again, this is taken care of in the next patch, so forget it :).

Although, in the breakpoint case, when we have:

	  for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)
	    {
	      const char *arg = exp;
	      expression_up new_exp
		= parse_exp_1 (&arg, loc->address,
			       block_for_pc (loc->address), 0);
	      if (*arg != 0)
		error (_("Junk at end of expression"));
	      loc->cond = std::move (new_exp);
	    }

Doesn't that mean that if the expression succeeds to parse for one location and then
fails to parse for another location, we'll have updated one location and not the other?

How does that work (or should work) when we have a multi-location breakpoint and the
condition only makes sense in one of the locations?

Simon
Jose E. Marchesi via Gdb-patches July 22, 2020, 3:29 p.m. | #3
On Wednesday, July 22, 2020 3:28 PM, Simon Marchi wrote:
> Although, in the breakpoint case, when we have:

> 

> 	  for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)

> 	    {

> 	      const char *arg = exp;

> 	      expression_up new_exp

> 		= parse_exp_1 (&arg, loc->address,

> 			       block_for_pc (loc->address), 0);

> 	      if (*arg != 0)

> 		error (_("Junk at end of expression"));

> 	      loc->cond = std::move (new_exp);

> 	    }

> 

> Doesn't that mean that if the expression succeeds to parse for one location and then

> fails to parse for another location, we'll have updated one location and not the other?


Ahh, yes.  The diff for the part above should have been:

          struct bp_location *loc;

+         /* Parse and set condition expressions.  We make two passes.
+            In the first, we parse the condition string to see if it
+            is valid in all locations.  If so, the condition would be
+            accepted.  So we go ahead and set the locations'
+            conditions.  In case a failing case is found, we throw
+            the error and the condition string will be rejected.
+            This two-pass approach is taken to avoid setting the
+            state of locations in case of a reject.  */
+         for (loc = b->loc; loc; loc = loc->next)
+           {
+             arg = exp;
+             parse_exp_1 (&arg, loc->address,
+                          block_for_pc (loc->address), 0);
+             if (*arg != 0)
+               error (_("Junk at end of expression"));
+           }
+
+         /* If we reach here, the condition is valid at all locations.  */
          for (loc = b->loc; loc; loc = loc->next)
            {
              arg = exp;
              loc->cond =
                parse_exp_1 (&arg, loc->address,
                             block_for_pc (loc->address), 0);
-             if (*arg)
-               error (_("Junk at end of expression"));
            }

> How does that work (or should work) when we have a multi-location breakpoint and the

> condition only makes sense in one of the locations?


I'm in fact working on a follow-up patch on this topic, where the two-pass approach above
is used (hence I forgot to include it in this series).

Currently, GDB expects the condition to be valid at all locations.  The patch that I'll
soon post proposes to accept the condition if there exist locations where it's valid.
The locations where the condition is invalid are disabled.  But in the current state, the
condition has to make sense at all locations.

Thanks
-Baris


Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
Simon Marchi July 22, 2020, 4:06 p.m. | #4
On 2020-07-22 11:29 a.m., Aktemur, Tankut Baris wrote:
> On Wednesday, July 22, 2020 3:28 PM, Simon Marchi wrote:

>> Although, in the breakpoint case, when we have:

>>

>> 	  for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)

>> 	    {

>> 	      const char *arg = exp;

>> 	      expression_up new_exp

>> 		= parse_exp_1 (&arg, loc->address,

>> 			       block_for_pc (loc->address), 0);

>> 	      if (*arg != 0)

>> 		error (_("Junk at end of expression"));

>> 	      loc->cond = std::move (new_exp);

>> 	    }

>>

>> Doesn't that mean that if the expression succeeds to parse for one location and then

>> fails to parse for another location, we'll have updated one location and not the other?

> 

> Ahh, yes.  The diff for the part above should have been:

> 

>           struct bp_location *loc;

> 

> +         /* Parse and set condition expressions.  We make two passes.

> +            In the first, we parse the condition string to see if it

> +            is valid in all locations.  If so, the condition would be

> +            accepted.  So we go ahead and set the locations'

> +            conditions.  In case a failing case is found, we throw

> +            the error and the condition string will be rejected.

> +            This two-pass approach is taken to avoid setting the

> +            state of locations in case of a reject.  */

> +         for (loc = b->loc; loc; loc = loc->next)

> +           {

> +             arg = exp;

> +             parse_exp_1 (&arg, loc->address,

> +                          block_for_pc (loc->address), 0);

> +             if (*arg != 0)

> +               error (_("Junk at end of expression"));

> +           }

> +

> +         /* If we reach here, the condition is valid at all locations.  */

>           for (loc = b->loc; loc; loc = loc->next)

>             {

>               arg = exp;

>               loc->cond =

>                 parse_exp_1 (&arg, loc->address,

>                              block_for_pc (loc->address), 0);

> -             if (*arg)

> -               error (_("Junk at end of expression"));

>             }

> 

>> How does that work (or should work) when we have a multi-location breakpoint and the

>> condition only makes sense in one of the locations?

> 

> I'm in fact working on a follow-up patch on this topic, where the two-pass approach above

> is used (hence I forgot to include it in this series).

> 

> Currently, GDB expects the condition to be valid at all locations.  The patch that I'll

> soon post proposes to accept the condition if there exist locations where it's valid.

> The locations where the condition is invalid are disabled.  But in the current state, the

> condition has to make sense at all locations.


Ok, so do you want to wait and post everything together, or do still want to consider
merging this one on its own, since it's still a step forward?

Simon
Jose E. Marchesi via Gdb-patches July 23, 2020, 7:11 a.m. | #5
On Wednesday, July 22, 2020 6:06 PM, Simon Marchi wrote:
> On 2020-07-22 11:29 a.m., Aktemur, Tankut Baris wrote:

> > On Wednesday, July 22, 2020 3:28 PM, Simon Marchi wrote:

> >> Although, in the breakpoint case, when we have:

> >>

> >> 	  for (bp_location *loc = b->loc; loc != nullptr; loc = loc->next)

> >> 	    {

> >> 	      const char *arg = exp;

> >> 	      expression_up new_exp

> >> 		= parse_exp_1 (&arg, loc->address,

> >> 			       block_for_pc (loc->address), 0);

> >> 	      if (*arg != 0)

> >> 		error (_("Junk at end of expression"));

> >> 	      loc->cond = std::move (new_exp);

> >> 	    }

> >>

> >> Doesn't that mean that if the expression succeeds to parse for one location and then

> >> fails to parse for another location, we'll have updated one location and not the other?

> >

> > Ahh, yes.  The diff for the part above should have been:

> >

> >           struct bp_location *loc;

> >

> > +         /* Parse and set condition expressions.  We make two passes.

> > +            In the first, we parse the condition string to see if it

> > +            is valid in all locations.  If so, the condition would be

> > +            accepted.  So we go ahead and set the locations'

> > +            conditions.  In case a failing case is found, we throw

> > +            the error and the condition string will be rejected.

> > +            This two-pass approach is taken to avoid setting the

> > +            state of locations in case of a reject.  */

> > +         for (loc = b->loc; loc; loc = loc->next)

> > +           {

> > +             arg = exp;

> > +             parse_exp_1 (&arg, loc->address,

> > +                          block_for_pc (loc->address), 0);

> > +             if (*arg != 0)

> > +               error (_("Junk at end of expression"));

> > +           }

> > +

> > +         /* If we reach here, the condition is valid at all locations.  */

> >           for (loc = b->loc; loc; loc = loc->next)

> >             {

> >               arg = exp;

> >               loc->cond =

> >                 parse_exp_1 (&arg, loc->address,

> >                              block_for_pc (loc->address), 0);

> > -             if (*arg)

> > -               error (_("Junk at end of expression"));

> >             }

> >

> >> How does that work (or should work) when we have a multi-location breakpoint and the

> >> condition only makes sense in one of the locations?

> >

> > I'm in fact working on a follow-up patch on this topic, where the two-pass approach above

> > is used (hence I forgot to include it in this series).

> >

> > Currently, GDB expects the condition to be valid at all locations.  The patch that I'll

> > soon post proposes to accept the condition if there exist locations where it's valid.

> > The locations where the condition is invalid are disabled.  But in the current state, the

> > condition has to make sense at all locations.

> 

> Ok, so do you want to wait and post everything together, or do still want to consider

> merging this one on its own, since it's still a step forward?


I'd like to merge this on its own.

Thanks
-Baris


Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
Jose E. Marchesi via Gdb-patches July 30, 2020, 10:56 a.m. | #6
On Thursday, July 23, 2020 9:11 AM, Aktemur, Tankut Baris wrote:
> On Wednesday, July 22, 2020 6:06 PM, Simon Marchi wrote:

> > Ok, so do you want to wait and post everything together, or do still want to consider

> > merging this one on its own, since it's still a step forward?

> 

> I'd like to merge this on its own.


Hi Simon,

OK to push the two-pass version?

Thanks
-Baris


Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
Simon Marchi July 30, 2020, 3:15 p.m. | #7
On 2020-07-30 6:56 a.m., Aktemur, Tankut Baris wrote:
> On Thursday, July 23, 2020 9:11 AM, Aktemur, Tankut Baris wrote:

>> On Wednesday, July 22, 2020 6:06 PM, Simon Marchi wrote:

>>> Ok, so do you want to wait and post everything together, or do still want to consider

>>> merging this one on its own, since it's still a step forward?

>>

>> I'd like to merge this on its own.

> 

> Hi Simon,

> 

> OK to push the two-pass version?

> 

> Thanks

> -Baris


Yes, go ahead.

Simon

Patch

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 1fc2d1b8966..abda470fe21 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -834,30 +834,30 @@  void
 set_breakpoint_condition (struct breakpoint *b, const char *exp,
 			  int from_tty)
 {
-  if (is_watchpoint (b))
-    {
-      struct watchpoint *w = (struct watchpoint *) b;
-
-      w->cond_exp.reset ();
-    }
-  else
+  if (*exp == 0)
     {
-      struct bp_location *loc;
+      xfree (b->cond_string);
+      b->cond_string = nullptr;
 
-      for (loc = b->loc; loc; loc = loc->next)
+      if (is_watchpoint (b))
 	{
-	  loc->cond.reset ();
+	  struct watchpoint *w = (struct watchpoint *) b;
 
-	  /* No need to free the condition agent expression
-	     bytecode (if we have one).  We will handle this
-	     when we go through update_global_location_list.  */
+	  w->cond_exp.reset ();
 	}
-    }
+      else
+	{
+	  struct bp_location *loc;
 
-  if (*exp == 0)
-    {
-      xfree (b->cond_string);
-      b->cond_string = nullptr;
+	  for (loc = b->loc; loc; loc = loc->next)
+	    {
+	      loc->cond.reset ();
+
+	      /* No need to free the condition agent expression
+		 bytecode (if we have one).  We will handle this
+		 when we go through update_global_location_list.  */
+	    }
+	}
 
       if (from_tty)
 	printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
@@ -872,9 +872,10 @@  set_breakpoint_condition (struct breakpoint *b, const char *exp,
 
 	  innermost_block_tracker tracker;
 	  arg = exp;
-	  w->cond_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
+	  expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
 	  if (*arg)
 	    error (_("Junk at end of expression"));
+	  w->cond_exp = std::move (new_exp);
 	  w->cond_exp_valid_block = tracker.block ();
 	}
       else
@@ -884,11 +885,12 @@  set_breakpoint_condition (struct breakpoint *b, const char *exp,
 	  for (loc = b->loc; loc; loc = loc->next)
 	    {
 	      arg = exp;
-	      loc->cond =
-		parse_exp_1 (&arg, loc->address,
-			     block_for_pc (loc->address), 0);
+	      expression_up new_exp
+		= parse_exp_1 (&arg, loc->address,
+			       block_for_pc (loc->address), 0);
 	      if (*arg)
 		error (_("Junk at end of expression"));
+	      loc->cond = std::move (new_exp);
 	    }
 	}
 
diff --git a/gdb/testsuite/gdb.base/condbreak-bad.exp b/gdb/testsuite/gdb.base/condbreak-bad.exp
index a01ba2a9340..84d32a0f15d 100644
--- a/gdb/testsuite/gdb.base/condbreak-bad.exp
+++ b/gdb/testsuite/gdb.base/condbreak-bad.exp
@@ -38,3 +38,91 @@  gdb_test "info break" \
 	 "${decimal}${fill}breakpoint${fill}keep y${fill}:${bp_location}"] \
     "breakpoint is unconditional"
 
+# Now define a valid condition.  Attempt to override that with a 'bad'
+# condition again.  The condition should be preserved.
+with_test_prefix "with run" {
+    gdb_test_no_output "cond $bpnum a == 10"
+
+    gdb_test "cond $bpnum gibberish" \
+	"No symbol \"gibberish\" in current context." \
+	"attempt a bad condition"
+
+    gdb_test "info break" \
+	[multi_line \
+	     "Num${fill}What" \
+	     "${decimal}${fill}breakpoint${fill}keep y${fill}:${bp_location}" \
+	     "${fill}stop only if a == 10${fill}"] \
+	"breakpoint condition is preserved"
+
+    # Run the code.  We should hit the breakpoint, because the
+    # condition evaluates to true.
+
+    gdb_run_cmd
+    gdb_test "" ".*reakpoint .*, main .*${srcfile}.*" "run to the bp"
+}
+
+# Restart.  Repeat the test above after the program has started.
+# This is needed to check a scenario where the breakpoints are no
+# longer re-inserted due to solib events.  Note that runto_main
+# deletes the breakpoints.
+with_test_prefix "with continue 1" {
+    if {![runto_main]} {
+	fail "could not run to main"
+	return -1
+    }
+
+    gdb_breakpoint "$bp_location"
+    set bpnum [get_integer_valueof "\$bpnum" 0 "get bpnum"]
+
+    gdb_test_no_output "cond $bpnum a == 10"
+
+    gdb_test "cond $bpnum gibberish" \
+	"No symbol \"gibberish\" in current context." \
+	"attempt a bad condition"
+
+    # Resume.  We should hit the breakpoint, because the
+    # condition evaluates to true.
+    gdb_continue_to_breakpoint "${srcfile}:${bp_location}"
+}
+
+# Repeat with a condition that evaluates to false.
+with_test_prefix "with continue 2" {
+    if {![runto_main]} {
+	fail "could not run to main"
+	return -1
+    }
+
+    gdb_breakpoint "$bp_location"
+    set bpnum [get_integer_valueof "\$bpnum" 0 "get bpnum"]
+
+    gdb_test_no_output "cond $bpnum a == 999"
+    
+    gdb_test "cond $bpnum gibberish" \
+	"No symbol \"gibberish\" in current context." \
+	"attempt a bad condition"
+
+    # Resume.  We should *not* hit the breakpoint, because the
+    # condition evaluates to false.
+    gdb_continue_to_end
+}
+
+# Repeat with a condition that contains junk at the end.
+with_test_prefix "with junk" {
+    if {![runto_main]} {
+	fail "could not run to main"
+	return -1
+    }
+
+    gdb_breakpoint "$bp_location"
+    set bpnum [get_integer_valueof "\$bpnum" 0 "get bpnum"]
+
+    gdb_test_no_output "cond $bpnum a == 999"
+
+    gdb_test "cond $bpnum a == 10 if" \
+	"Junk at end of expression" \
+	"attempt a bad condition"
+
+    # Resume.  We should *not* hit the breakpoint, because the
+    # condition evaluates to false.
+    gdb_continue_to_end
+}