| 1 | /* Copyright (C) 1986-2019 Free Software Foundation, Inc. |
| 2 | |
| 3 | This file is part of GDB. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include "defs.h" |
| 19 | #include "gdbcmd.h" |
| 20 | #include "regcache.h" |
| 21 | #include "common/def-vector.h" |
| 22 | #include "valprint.h" |
| 23 | #include "remote.h" |
| 24 | #include "reggroups.h" |
| 25 | #include "target.h" |
| 26 | |
| 27 | /* Dump registers from regcache, used for dumping raw registers and |
| 28 | cooked registers. */ |
| 29 | |
| 30 | class register_dump_regcache : public register_dump |
| 31 | { |
| 32 | public: |
| 33 | register_dump_regcache (regcache *regcache, bool dump_pseudo) |
| 34 | : register_dump (regcache->arch ()), m_regcache (regcache), |
| 35 | m_dump_pseudo (dump_pseudo) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | protected: |
| 40 | void dump_reg (ui_file *file, int regnum) override |
| 41 | { |
| 42 | if (regnum < 0) |
| 43 | { |
| 44 | if (m_dump_pseudo) |
| 45 | fprintf_unfiltered (file, "Cooked value"); |
| 46 | else |
| 47 | fprintf_unfiltered (file, "Raw value"); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | if (regnum < gdbarch_num_regs (m_gdbarch) || m_dump_pseudo) |
| 52 | { |
| 53 | auto size = register_size (m_gdbarch, regnum); |
| 54 | |
| 55 | if (size == 0) |
| 56 | return; |
| 57 | |
| 58 | gdb::def_vector<gdb_byte> buf (size); |
| 59 | auto status = m_regcache->cooked_read (regnum, buf.data ()); |
| 60 | |
| 61 | if (status == REG_UNKNOWN) |
| 62 | fprintf_unfiltered (file, "<invalid>"); |
| 63 | else if (status == REG_UNAVAILABLE) |
| 64 | fprintf_unfiltered (file, "<unavailable>"); |
| 65 | else |
| 66 | { |
| 67 | print_hex_chars (file, buf.data (), size, |
| 68 | gdbarch_byte_order (m_gdbarch), true); |
| 69 | } |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | /* Just print "<cooked>" for pseudo register when |
| 74 | regcache_dump_raw. */ |
| 75 | fprintf_unfiltered (file, "<cooked>"); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | regcache *m_regcache; |
| 82 | |
| 83 | /* Dump pseudo registers or not. */ |
| 84 | const bool m_dump_pseudo; |
| 85 | }; |
| 86 | |
| 87 | /* Dump from reg_buffer, used when there is no thread or |
| 88 | registers. */ |
| 89 | |
| 90 | class register_dump_reg_buffer : public register_dump, reg_buffer |
| 91 | { |
| 92 | public: |
| 93 | register_dump_reg_buffer (gdbarch *gdbarch, bool dump_pseudo) |
| 94 | : register_dump (gdbarch), reg_buffer (gdbarch, dump_pseudo) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | void dump_reg (ui_file *file, int regnum) override |
| 100 | { |
| 101 | if (regnum < 0) |
| 102 | { |
| 103 | if (m_has_pseudo) |
| 104 | fprintf_unfiltered (file, "Cooked value"); |
| 105 | else |
| 106 | fprintf_unfiltered (file, "Raw value"); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | if (regnum < gdbarch_num_regs (m_gdbarch) || m_has_pseudo) |
| 111 | { |
| 112 | auto size = register_size (m_gdbarch, regnum); |
| 113 | |
| 114 | if (size == 0) |
| 115 | return; |
| 116 | |
| 117 | auto status = get_register_status (regnum); |
| 118 | |
| 119 | gdb_assert (status != REG_VALID); |
| 120 | |
| 121 | if (status == REG_UNKNOWN) |
| 122 | fprintf_unfiltered (file, "<invalid>"); |
| 123 | else |
| 124 | fprintf_unfiltered (file, "<unavailable>"); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | /* Just print "<cooked>" for pseudo register when |
| 129 | regcache_dump_raw. */ |
| 130 | fprintf_unfiltered (file, "<cooked>"); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | /* For "maint print registers". */ |
| 137 | |
| 138 | class register_dump_none : public register_dump |
| 139 | { |
| 140 | public: |
| 141 | register_dump_none (gdbarch *arch) |
| 142 | : register_dump (arch) |
| 143 | {} |
| 144 | |
| 145 | protected: |
| 146 | void dump_reg (ui_file *file, int regnum) override |
| 147 | {} |
| 148 | }; |
| 149 | |
| 150 | /* For "maint print remote-registers". */ |
| 151 | |
| 152 | class register_dump_remote : public register_dump |
| 153 | { |
| 154 | public: |
| 155 | register_dump_remote (gdbarch *arch) |
| 156 | : register_dump (arch) |
| 157 | {} |
| 158 | |
| 159 | protected: |
| 160 | void dump_reg (ui_file *file, int regnum) override |
| 161 | { |
| 162 | if (regnum < 0) |
| 163 | { |
| 164 | fprintf_unfiltered (file, "Rmt Nr g/G Offset"); |
| 165 | } |
| 166 | else if (regnum < gdbarch_num_regs (m_gdbarch)) |
| 167 | { |
| 168 | int pnum, poffset; |
| 169 | |
| 170 | if (remote_register_number_and_offset (m_gdbarch, regnum, |
| 171 | &pnum, &poffset)) |
| 172 | fprintf_unfiltered (file, "%7d %11d", pnum, poffset); |
| 173 | } |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | /* For "maint print register-groups". */ |
| 178 | |
| 179 | class register_dump_groups : public register_dump |
| 180 | { |
| 181 | public: |
| 182 | register_dump_groups (gdbarch *arch) |
| 183 | : register_dump (arch) |
| 184 | {} |
| 185 | |
| 186 | protected: |
| 187 | void dump_reg (ui_file *file, int regnum) override |
| 188 | { |
| 189 | if (regnum < 0) |
| 190 | fprintf_unfiltered (file, "Groups"); |
| 191 | else |
| 192 | { |
| 193 | const char *sep = ""; |
| 194 | struct reggroup *group; |
| 195 | |
| 196 | for (group = reggroup_next (m_gdbarch, NULL); |
| 197 | group != NULL; |
| 198 | group = reggroup_next (m_gdbarch, group)) |
| 199 | { |
| 200 | if (gdbarch_register_reggroup_p (m_gdbarch, regnum, group)) |
| 201 | { |
| 202 | fprintf_unfiltered (file, |
| 203 | "%s%s", sep, reggroup_name (group)); |
| 204 | sep = ","; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | enum regcache_dump_what |
| 212 | { |
| 213 | regcache_dump_none, regcache_dump_raw, |
| 214 | regcache_dump_cooked, regcache_dump_groups, |
| 215 | regcache_dump_remote |
| 216 | }; |
| 217 | |
| 218 | static void |
| 219 | regcache_print (const char *args, enum regcache_dump_what what_to_dump) |
| 220 | { |
| 221 | /* Where to send output. */ |
| 222 | stdio_file file; |
| 223 | ui_file *out; |
| 224 | |
| 225 | if (args == NULL) |
| 226 | out = gdb_stdout; |
| 227 | else |
| 228 | { |
| 229 | if (!file.open (args, "w")) |
| 230 | perror_with_name (_("maintenance print architecture")); |
| 231 | out = &file; |
| 232 | } |
| 233 | |
| 234 | std::unique_ptr<register_dump> dump; |
| 235 | std::unique_ptr<regcache> regs; |
| 236 | gdbarch *gdbarch; |
| 237 | |
| 238 | if (target_has_registers) |
| 239 | gdbarch = get_current_regcache ()->arch (); |
| 240 | else |
| 241 | gdbarch = target_gdbarch (); |
| 242 | |
| 243 | switch (what_to_dump) |
| 244 | { |
| 245 | case regcache_dump_none: |
| 246 | dump.reset (new register_dump_none (gdbarch)); |
| 247 | break; |
| 248 | case regcache_dump_remote: |
| 249 | dump.reset (new register_dump_remote (gdbarch)); |
| 250 | break; |
| 251 | case regcache_dump_groups: |
| 252 | dump.reset (new register_dump_groups (gdbarch)); |
| 253 | break; |
| 254 | case regcache_dump_raw: |
| 255 | case regcache_dump_cooked: |
| 256 | { |
| 257 | auto dump_pseudo = (what_to_dump == regcache_dump_cooked); |
| 258 | |
| 259 | if (target_has_registers) |
| 260 | dump.reset (new register_dump_regcache (get_current_regcache (), |
| 261 | dump_pseudo)); |
| 262 | else |
| 263 | { |
| 264 | /* For the benefit of "maint print registers" & co when |
| 265 | debugging an executable, allow dumping a regcache even when |
| 266 | there is no thread selected / no registers. */ |
| 267 | dump.reset (new register_dump_reg_buffer (target_gdbarch (), |
| 268 | dump_pseudo)); |
| 269 | } |
| 270 | } |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | dump->dump (out); |
| 275 | } |
| 276 | |
| 277 | static void |
| 278 | maintenance_print_registers (const char *args, int from_tty) |
| 279 | { |
| 280 | regcache_print (args, regcache_dump_none); |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | maintenance_print_raw_registers (const char *args, int from_tty) |
| 285 | { |
| 286 | regcache_print (args, regcache_dump_raw); |
| 287 | } |
| 288 | |
| 289 | static void |
| 290 | maintenance_print_cooked_registers (const char *args, int from_tty) |
| 291 | { |
| 292 | regcache_print (args, regcache_dump_cooked); |
| 293 | } |
| 294 | |
| 295 | static void |
| 296 | maintenance_print_register_groups (const char *args, int from_tty) |
| 297 | { |
| 298 | regcache_print (args, regcache_dump_groups); |
| 299 | } |
| 300 | |
| 301 | static void |
| 302 | maintenance_print_remote_registers (const char *args, int from_tty) |
| 303 | { |
| 304 | regcache_print (args, regcache_dump_remote); |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | _initialize_regcache_dump (void) |
| 309 | { |
| 310 | add_cmd ("registers", class_maintenance, maintenance_print_registers, |
| 311 | _("Print the internal register configuration.\n" |
| 312 | "Takes an optional file parameter."), &maintenanceprintlist); |
| 313 | add_cmd ("raw-registers", class_maintenance, |
| 314 | maintenance_print_raw_registers, |
| 315 | _("Print the internal register configuration " |
| 316 | "including raw values.\n" |
| 317 | "Takes an optional file parameter."), &maintenanceprintlist); |
| 318 | add_cmd ("cooked-registers", class_maintenance, |
| 319 | maintenance_print_cooked_registers, |
| 320 | _("Print the internal register configuration " |
| 321 | "including cooked values.\n" |
| 322 | "Takes an optional file parameter."), &maintenanceprintlist); |
| 323 | add_cmd ("register-groups", class_maintenance, |
| 324 | maintenance_print_register_groups, |
| 325 | _("Print the internal register configuration " |
| 326 | "including each register's group.\n" |
| 327 | "Takes an optional file parameter."), |
| 328 | &maintenanceprintlist); |
| 329 | add_cmd ("remote-registers", class_maintenance, |
| 330 | maintenance_print_remote_registers, _("\ |
| 331 | Print the internal register configuration including each register's\n\ |
| 332 | remote register number and buffer offset in the g/G packets.\n\ |
| 333 | Takes an optional file parameter."), |
| 334 | &maintenanceprintlist); |
| 335 | } |