Home | History | Annotate | Line # | Download | only in gdb.base
      1 # Copyright 2020-2024 Free Software Foundation, Inc.
      2 
      3 # This program is free software; you can redistribute it and/or modify
      4 # it under the terms of the GNU General Public License as published by
      5 # the Free Software Foundation; either version 3 of the License, or
      6 # (at your option) any later version.
      7 #
      8 # This program is distributed in the hope that it will be useful,
      9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11 # GNU General Public License for more details.
     12 #
     13 # You should have received a copy of the GNU General Public License
     14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15 
     16 # Test defining bad conditions for breakpoints.
     17 
     18 standard_testfile
     19 
     20 if {[prepare_for_testing "failed to prepare" ${binfile} ${srcfile}]} {
     21     return
     22 }
     23 
     24 set bp_location [gdb_get_line_number "break-here"]
     25 gdb_breakpoint "$bp_location"
     26 set bpnum [get_integer_valueof "\$bpnum" 0 "get bpnum"]
     27 
     28 # Define a 'bad' condition.  The breakpoint should stay unconditional.
     29 gdb_test "cond $bpnum gibberish" \
     30     "No symbol \"gibberish\" in current context." \
     31     "attempt a bad condition"
     32 
     33 set fill "\[^\r\n\]*"
     34 
     35 gdb_test "info break" \
     36     [multi_line \
     37 	 "Num${fill}What" \
     38 	 "${decimal}${fill}breakpoint${fill}keep y${fill}:${bp_location}"] \
     39     "breakpoint is unconditional"
     40 
     41 # Now define a valid condition.  Attempt to override that with a 'bad'
     42 # condition again.  The condition should be preserved.
     43 with_test_prefix "with run" {
     44     gdb_test_no_output "cond $bpnum a == 10"
     45 
     46     gdb_test "cond $bpnum gibberish" \
     47 	"No symbol \"gibberish\" in current context." \
     48 	"attempt a bad condition"
     49 
     50     gdb_test "info break" \
     51 	[multi_line \
     52 	     "Num${fill}What" \
     53 	     "${decimal}${fill}breakpoint${fill}keep y${fill}:${bp_location}" \
     54 	     "${fill}stop only if a == 10${fill}"] \
     55 	"breakpoint condition is preserved"
     56 
     57     # Run the code.  We should hit the breakpoint, because the
     58     # condition evaluates to true.
     59 
     60     gdb_run_cmd
     61     gdb_test "" ".*reakpoint .*, main .*${srcfile}.*" "run to the bp"
     62 }
     63 
     64 # Restart.  Repeat the test above after the program has started.
     65 # This is needed to check a scenario where the breakpoints are no
     66 # longer re-inserted due to solib events.  Note that runto_main
     67 # deletes the breakpoints.
     68 with_test_prefix "with continue 1" {
     69     if {![runto_main]} {
     70 	return -1
     71     }
     72 
     73     gdb_breakpoint "$bp_location"
     74     set bpnum [get_integer_valueof "\$bpnum" 0 "get bpnum"]
     75 
     76     gdb_test_no_output "cond $bpnum a == 10"
     77 
     78     gdb_test "cond $bpnum gibberish" \
     79 	"No symbol \"gibberish\" in current context." \
     80 	"attempt a bad condition"
     81 
     82     # Resume.  We should hit the breakpoint, because the
     83     # condition evaluates to true.
     84     gdb_continue_to_breakpoint "${srcfile}:${bp_location}"
     85 }
     86 
     87 # Repeat with a condition that evaluates to false.
     88 with_test_prefix "with continue 2" {
     89     if {![runto_main]} {
     90 	return -1
     91     }
     92 
     93     gdb_breakpoint "$bp_location"
     94     set bpnum [get_integer_valueof "\$bpnum" 0 "get bpnum"]
     95 
     96     gdb_test_no_output "cond $bpnum a == 999"
     97 
     98     gdb_test "cond $bpnum gibberish" \
     99 	"No symbol \"gibberish\" in current context." \
    100 	"attempt a bad condition"
    101 
    102     # Resume.  We should *not* hit the breakpoint, because the
    103     # condition evaluates to false.
    104     gdb_continue_to_end
    105 }
    106 
    107 # Repeat with a condition that contains junk at the end.
    108 with_test_prefix "with junk" {
    109     if {![runto_main]} {
    110 	return -1
    111     }
    112 
    113     gdb_breakpoint "$bp_location"
    114     set bpnum [get_integer_valueof "\$bpnum" 0 "get bpnum"]
    115 
    116     gdb_test_no_output "cond $bpnum a == 999"
    117 
    118     gdb_test "cond $bpnum a == 10 if" \
    119 	"Junk at end of expression" \
    120 	"attempt a bad condition"
    121 
    122     # Resume.  We should *not* hit the breakpoint, because the
    123     # condition evaluates to false.
    124     gdb_continue_to_end
    125 }
    126