# operation.  (Note that we don't have any of these watchpoints
 # trigger.)
 
-if [target_info exists gdb,no_hardware_watchpoints] {
-    unsupported "no target support"
-    return
-}
-
 standard_testfile
 
 if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
 
 gdb_breakpoint $srcline
 gdb_continue_to_breakpoint "stepi line"
+set cur_addr [get_pc]
 
 # The test tries various sequences of different types of watchpoints.
 # Probe for support first.
+proc build_cmds_list {} {
+    global gdb_prompt
+
+    # So we get an immediate warning/error if the target doesn't support a
+    # given watchpoint type.
+    gdb_test_no_output "set breakpoint always-inserted on" \
+       "Set breakpoints always inserted while building cmds list"
+
+    # The list of supported commands.  Below we'll probe for support and
+    # add elements to this list.
+    set cmds {}
+
+    foreach cmd {"watch" "awatch" "rwatch"} {
+       set test $cmd
+       gdb_test_multiple "$cmd buf.byte\[0\]" $test {
+           -re "You may have requested too many.*$gdb_prompt $" {
+               unsupported $test
+           }
+           -re "Target does not support.*$gdb_prompt $" {
+               unsupported $test
+           }
+           -re "Can't set read/access watchpoint when hardware watchpoints are disabled.*$gdb_prompt $" {
+               unsupported $test
+           }
+           -re "$gdb_prompt $" {
+               pass $test
+               lappend cmds $cmd
+           }
+       }
 
-# So we get an immediate warning/error if the target doesn't support a
-# given watchpoint type.
-gdb_test_no_output "set breakpoint always-inserted on"
-
-# The list of supported commands.  Below we'll probe for support and
-# add elements to this list.
-set cmds {}
+       delete_breakpoints
+    }
 
-foreach cmd {"watch" "awatch" "rwatch"} {
-    set test $cmd
-    gdb_test_multiple "$cmd buf.byte\[0\]" $test {
+    set test "hbreak"
+    gdb_test_multiple "hbreak main" $test {
        -re "You may have requested too many.*$gdb_prompt $" {
            unsupported $test
        }
-       -re "Target does not support.*$gdb_prompt $" {
+       -re "No hardware breakpoint support.*$gdb_prompt $" {
            unsupported $test
        }
        -re "$gdb_prompt $" {
            pass $test
-           lappend cmds $cmd
+           lappend cmds "hbreak"
        }
     }
 
-   delete_breakpoints
-}
+    delete_breakpoints
 
-set test "hbreak"
-gdb_test_multiple "hbreak main" $test {
-    -re "You may have requested too many.*$gdb_prompt $" {
-       unsupported $test
-    }
-    -re "No hardware breakpoint support.*$gdb_prompt $" {
-       unsupported $test
-    }
-    -re "$gdb_prompt $" {
-       pass $test
-       lappend cmds "hbreak"
-    }
+    return $cmds
 }
 
-delete_breakpoints
-
-set cur_addr [get_pc]
-
 # Return true if the memory range [buf.byte + OFFSET, +WIDTH] can be
 # monitored by CMD, otherwise return false.
 
 
 # Watch WIDTH bytes at BASE + OFFSET.  CMD specifices the specific
 # type of watchpoint to use.  If CMD is "hbreak", WIDTH is ignored.
+# The HW_WP_P flag tells us if hardware watchpoints are enabled or
+# not.
 
-proc watch_command {cmd base offset width} {
+proc watch_command {cmd base offset width hw_wp_p} {
     global srcfile srcline hex
 
     if {$cmd == "hbreak"} {
        gdb_test "hbreak $expr" "Hardware assisted breakpoint \[0-9\]+ at $hex"
     } elseif {$cmd == "watch"} {
        set expr "*(buf.byte + $base + $offset)@$width"
+
+       if { ! $hw_wp_p } {
+           set wp_prefix "Watchpoint"
+       } else {
+           set wp_prefix "Hardware watchpoint"
+       }
+
        gdb_test "$cmd $expr" \
-           "Hardware watchpoint \[0-9\]+: [string_to_regexp $expr]"
+           "${wp_prefix} \[0-9\]+: [string_to_regexp $expr]"
     } elseif {$cmd == "awatch"} {
        set expr "*(buf.byte + $base + $offset)@$width"
        gdb_test "$cmd $expr" \
     }
 }
 
-# Run test proper.  See intro for description.
+# Run the watchpoint tests (see the description at the top for details), the
+# HW_WP_P flag tells us if hardware watchpoints are enabled or not.
+proc run_watchpoints_tests {hw_wp_p} {
 
-foreach always_inserted {"off" "on" } {
-    gdb_test_no_output "set breakpoint always-inserted $always_inserted"
-    foreach cmd1 $cmds {
-       foreach cmd2 $cmds {
-           for {set width 1} {$width < 4} {incr width} {
+    set cmds [build_cmds_list]
 
-               if {$cmd1 == "hbreak" && $cmd2 == "hbreak" && $width > 1} {
-                   # hbreak ignores WIDTH, no use testing more than
-                   # once.
-                   continue
-               }
+    foreach always_inserted {"off" "on" } {
+       gdb_test_no_output "set breakpoint always-inserted $always_inserted"
+       foreach cmd1 $cmds {
+           foreach cmd2 $cmds {
+               for {set width 1} {$width < 4} {incr width} {
 
-               for {set x 0} {$x < 4} {incr x} {
-
-                   if { ![valid_addr_p $cmd1 $x $width]
-                        || ![valid_addr_p $cmd2 $x+1 $width] } {
-                       # Skip tests if requested address or length
-                       # of breakpoint or watchpoint don't meet
-                       # target or kernel requirements.
+                   if {$cmd1 == "hbreak" && $cmd2 == "hbreak" \
+                           && $width > 1} {
+                       # hbreak ignores WIDTH, no use testing more than
+                       # once.
                        continue
                    }
 
-                   set prefix "always-inserted $always_inserted: "
-                   append prefix "$cmd1 x $cmd2: "
-                   with_test_prefix "$prefix: width $width, iter $x" {
-                       with_test_prefix "base + 0" {
-                           watch_command $cmd1 $x 0 $width
-                           stepi
-                           gdb_test_no_output "delete \$bpnum"
+                   for {set x 0} {$x < 4} {incr x} {
+
+                       if { ![valid_addr_p $cmd1 $x $width]
+                            || ![valid_addr_p $cmd2 $x+1 $width] } {
+                           # Skip tests if requested address or length
+                           # of breakpoint or watchpoint don't meet
+                           # target or kernel requirements.
+                           continue
                        }
-                       with_test_prefix "base + 1" {
-                           watch_command $cmd2 $x 1 $width
-                           stepi
-                           gdb_test_no_output "delete \$bpnum"
+
+                       set prefix "always-inserted $always_inserted: "
+                       append prefix "$cmd1 x $cmd2: "
+                       with_test_prefix "$prefix: width $width, iter $x" {
+                           with_test_prefix "base + 0" {
+                               watch_command $cmd1 $x 0 $width $hw_wp_p
+                               stepi
+                               gdb_test_no_output "delete \$bpnum"
+                           }
+                           with_test_prefix "base + 1" {
+                               watch_command $cmd2 $x 1 $width $hw_wp_p
+                               stepi
+                               gdb_test_no_output "delete \$bpnum"
+                           }
                        }
                    }
                }
        }
     }
 }
+
+# Based on HW_WP_P set whether hardware watchpoints can be used or
+# not, then call RUN_WATCHPOINTS_TESTS.
+proc setup_and_run_watchpoints_tests { hw_wp_p } {
+    if {$hw_wp_p} {
+       set prefix "hw-watch"
+    } else {
+       set prefix "sw-watch"
+    }
+
+    with_test_prefix $prefix {
+       gdb_test_no_output "set can-use-hw-watchpoints ${hw_wp_p}"
+
+       run_watchpoints_tests $hw_wp_p
+    }
+}
+
+# Run tests with hardware watchpoints disabled, then again with them
+# enabled (if this target supports hardware watchpoints).
+if { ![target_info exists gdb,no_hardware_watchpoints]} {
+    # Run test with H/W enabled.
+    setup_and_run_watchpoints_tests 1
+}
+
+# Run test with H/W disabled
+setup_and_run_watchpoints_tests 0