Files
binutils-gdb/gdb/testsuite/gdb.base/condbreak-bad.exp
Tankut Baris Aktemur 4c55e97025 gdb/breakpoint: set the condition exp after parsing the condition successfully
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-07-30  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-07-30  Tankut Baris Aktemur  <tankut.baris.aktemur@intel.com>

	* gdb.base/condbreak-bad.exp: Extend the test with scenarios
	that attempt to overwrite an existing condition with a condition
	that fails parsing and also with a condition that parses fine
	but contains junk at the end.
2020-07-30 19:23:38 +02:00

129 lines
3.8 KiB
Plaintext

# Copyright 2020 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Test defining bad conditions for breakpoints.
standard_testfile
if {[prepare_for_testing "failed to prepare" ${binfile} ${srcfile}]} {
return
}
set bp_location [gdb_get_line_number "break-here"]
gdb_breakpoint "$bp_location"
set bpnum [get_integer_valueof "\$bpnum" 0 "get bpnum"]
# Define a 'bad' condition. The breakpoint should stay unconditional.
gdb_test "cond $bpnum gibberish" \
"No symbol \"gibberish\" in current context." \
"attempt a bad condition"
set fill "\[^\r\n\]*"
gdb_test "info break" \
[multi_line \
"Num${fill}What" \
"${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
}