]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/linux-nat-trad.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / linux-nat-trad.c
CommitLineData
6798487f
PA
1/* Generic GNU/Linux target using traditional ptrace register access.
2
b811d2c2 3 Copyright (C) 1988-2020 Free Software Foundation, Inc.
6798487f
PA
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "linux-nat-trad.h"
22
23#include "nat/gdb_ptrace.h"
24#include "inf-ptrace.h"
2208ee91 25#include "gdbarch.h"
6798487f 26
6798487f
PA
27/* Fetch register REGNUM from the inferior. */
28
f6ac5f3d
PA
29void
30linux_nat_trad_target::fetch_register (struct regcache *regcache, int regnum)
6798487f
PA
31{
32 struct gdbarch *gdbarch = regcache->arch ();
1d761124 33 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6798487f 34 CORE_ADDR addr;
1d761124 35 gdb_byte *buf;
6798487f 36 size_t size;
6798487f
PA
37 pid_t pid;
38 int i;
39
40 /* This isn't really an address, but ptrace thinks of it as one. */
f6ac5f3d 41 addr = register_u_offset (gdbarch, regnum, 0);
6798487f
PA
42 if (addr == (CORE_ADDR)-1
43 || gdbarch_cannot_fetch_register (gdbarch, regnum))
44 {
73e1c03f 45 regcache->raw_supply (regnum, NULL);
6798487f
PA
46 return;
47 }
48
222312d3 49 pid = get_ptrace_pid (regcache->ptid ());
6798487f
PA
50
51 size = register_size (gdbarch, regnum);
1d761124 52 buf = (gdb_byte *) alloca (size);
6798487f
PA
53
54 /* Read the register contents from the inferior a chunk at a time. */
1d761124 55 for (i = 0; i < size; i += sizeof (PTRACE_TYPE_RET))
6798487f 56 {
1d761124
MR
57 size_t chunk = std::min (sizeof (PTRACE_TYPE_RET), size - i);
58 PTRACE_TYPE_RET val;
59
6798487f 60 errno = 0;
1d761124 61 val = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3) (uintptr_t) addr, 0);
6798487f
PA
62 if (errno != 0)
63 error (_("Couldn't read register %s (#%d): %s."),
64 gdbarch_register_name (gdbarch, regnum),
65 regnum, safe_strerror (errno));
1d761124 66 store_unsigned_integer (buf + i, chunk, byte_order, val);
6798487f
PA
67
68 addr += sizeof (PTRACE_TYPE_RET);
69 }
73e1c03f 70 regcache->raw_supply (regnum, buf);
6798487f
PA
71}
72
73/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
74 for all registers. */
75
f6ac5f3d
PA
76void
77linux_nat_trad_target::fetch_registers (struct regcache *regcache, int regnum)
6798487f
PA
78{
79 if (regnum == -1)
80 for (regnum = 0;
81 regnum < gdbarch_num_regs (regcache->arch ());
82 regnum++)
f6ac5f3d 83 fetch_register (regcache, regnum);
6798487f 84 else
f6ac5f3d 85 fetch_register (regcache, regnum);
6798487f
PA
86}
87
88/* Store register REGNUM into the inferior. */
89
f6ac5f3d
PA
90void
91linux_nat_trad_target::store_register (const struct regcache *regcache,
92 int regnum)
6798487f
PA
93{
94 struct gdbarch *gdbarch = regcache->arch ();
1d761124 95 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6798487f
PA
96 CORE_ADDR addr;
97 size_t size;
1d761124 98 gdb_byte *buf;
6798487f
PA
99 pid_t pid;
100 int i;
101
102 /* This isn't really an address, but ptrace thinks of it as one. */
f6ac5f3d 103 addr = register_u_offset (gdbarch, regnum, 1);
6798487f
PA
104 if (addr == (CORE_ADDR)-1
105 || gdbarch_cannot_store_register (gdbarch, regnum))
106 return;
107
222312d3 108 pid = get_ptrace_pid (regcache->ptid ());
6798487f
PA
109
110 size = register_size (gdbarch, regnum);
1d761124 111 buf = (gdb_byte *) alloca (size);
6798487f
PA
112
113 /* Write the register contents into the inferior a chunk at a time. */
34a79281 114 regcache->raw_collect (regnum, buf);
1d761124 115 for (i = 0; i < size; i += sizeof (PTRACE_TYPE_RET))
6798487f 116 {
1d761124
MR
117 size_t chunk = std::min (sizeof (PTRACE_TYPE_RET), size - i);
118 PTRACE_TYPE_RET val;
119
120 val = extract_unsigned_integer (buf + i, chunk, byte_order);
6798487f 121 errno = 0;
1d761124 122 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3) (uintptr_t) addr, val);
6798487f
PA
123 if (errno != 0)
124 error (_("Couldn't write register %s (#%d): %s."),
125 gdbarch_register_name (gdbarch, regnum),
126 regnum, safe_strerror (errno));
127
128 addr += sizeof (PTRACE_TYPE_RET);
129 }
130}
131
132/* Store register REGNUM back into the inferior. If REGNUM is -1, do
133 this for all registers. */
134
f6ac5f3d
PA
135void
136linux_nat_trad_target::store_registers (struct regcache *regcache, int regnum)
6798487f
PA
137{
138 if (regnum == -1)
139 for (regnum = 0;
140 regnum < gdbarch_num_regs (regcache->arch ());
141 regnum++)
f6ac5f3d 142 store_register (regcache, regnum);
6798487f 143 else
f6ac5f3d 144 store_register (regcache, regnum);
6798487f 145}