1 # Copyright 2022-2023 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test patching executables and core files, with "set write on".
20 if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
24 # Check that we can patch an executable.
26 with_test_prefix "exec" {
29 gdb_test_no_output "set write on"
33 gdb_test "p extern_global" " = 1" "read original value"
34 gdb_test "p extern_global = 2" " = 2" "modify value"
35 gdb_test "p extern_global" " = 2" "value modified"
37 clean_restart $binfile
39 gdb_test "p extern_global" " = 2" "value modified persisted"
42 # Check that we can patch a core file.
44 # Generate a core file.
45 with_test_prefix "gcore" {
46 clean_restart $binfile
52 # Extract the value at PC, and add 1, letting it wrap if
53 # necessary. This is the value we will poke into memory.
55 gdb_test_multiple "p/x (*(unsigned char *) \$pc) + 1" "compute poke value" {
56 -re -wrap " = ($hex)" {
57 set poke_value $expect_out(1,string)
61 if {$poke_value == ""} {
65 set corefile [standard_output_file gcore.test]
66 set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"]
67 if {!$core_supported} {
72 # Load it into GDB with "set write on", and change the instruction at
74 with_test_prefix "load core, write on" {
75 # Don't load a binary, we want to make sure we're patching the
76 # core, not the executable.
79 gdb_test_no_output "set write on"
81 set core_loaded [gdb_core_cmd "$corefile" "re-load generated corefile"]
82 if { $core_loaded == -1 } {
86 gdb_test "p/x *(unsigned char *) \$pc = $poke_value" \
89 gdb_test "p/x *(unsigned char *) \$pc" \
94 # Re-load it into a new GDB session, now with "set write off", and
95 # confirm the value change persisted.
96 with_test_prefix "re-load modified core" {
97 # Don't load a binary, we want to make sure we've patched the
98 # core, not the executable.
101 set core_loaded [gdb_core_cmd "$corefile" "re-load generated corefile"]
102 gdb_assert { $core_loaded != -1 } "core re-loaded"
104 gdb_test "p/x *(unsigned char *) \$pc" \
106 "value modified persisted"