]>
Commit | Line | Data |
---|---|---|
213516ef | 1 | # Copyright 2022-2023 Free Software Foundation, Inc. |
169692ce PA |
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 patching executables and core files, with "set write on". | |
17 | ||
18 | standard_testfile | |
19 | ||
20 | if {[build_executable "failed to prepare" $testfile $srcfile debug]} { | |
21 | return -1 | |
22 | } | |
23 | ||
24 | # Check that we can patch an executable. | |
25 | ||
26 | with_test_prefix "exec" { | |
27 | clean_restart | |
28 | ||
29 | gdb_test_no_output "set write on" | |
30 | ||
31 | gdb_load $binfile | |
32 | ||
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" | |
36 | ||
37 | clean_restart $binfile | |
38 | ||
39 | gdb_test "p extern_global" " = 2" "value modified persisted" | |
40 | } | |
41 | ||
42 | # Check that we can patch a core file. | |
43 | ||
44 | # Generate a core file. | |
45 | with_test_prefix "gcore" { | |
46 | clean_restart $binfile | |
47 | ||
48 | if {![runto_main]} { | |
49 | return | |
50 | } | |
51 | ||
52 | # Extract the value at PC, and add 1, letting it wrap if | |
53 | # necessary. This is the value we will poke into memory. | |
54 | set poke_value "" | |
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) | |
58 | pass $gdb_test_name | |
59 | } | |
60 | } | |
61 | if {$poke_value == ""} { | |
62 | return | |
63 | } | |
64 | ||
65 | set corefile [standard_output_file gcore.test] | |
66 | set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"] | |
67 | if {!$core_supported} { | |
68 | return | |
69 | } | |
70 | } | |
71 | ||
72 | # Load it into GDB with "set write on", and change the instruction at | |
73 | # PC. | |
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. | |
77 | clean_restart | |
78 | ||
79 | gdb_test_no_output "set write on" | |
80 | ||
81 | set core_loaded [gdb_core_cmd "$corefile" "re-load generated corefile"] | |
82 | if { $core_loaded == -1 } { | |
83 | return | |
84 | } | |
85 | ||
86 | gdb_test "p/x *(unsigned char *) \$pc = $poke_value" \ | |
87 | " = $poke_value" \ | |
88 | "poke value" | |
89 | gdb_test "p/x *(unsigned char *) \$pc" \ | |
90 | " = $poke_value" \ | |
91 | "value modified" | |
92 | } | |
93 | ||
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. | |
99 | clean_restart | |
100 | ||
101 | set core_loaded [gdb_core_cmd "$corefile" "re-load generated corefile"] | |
102 | gdb_assert { $core_loaded != -1 } "core re-loaded" | |
103 | ||
104 | gdb_test "p/x *(unsigned char *) \$pc" \ | |
105 | " = $poke_value" \ | |
106 | "value modified persisted" | |
107 | } |