GDB (xrefs)
Loading...
Searching...
No Matches
linux-nat-trad.c
Go to the documentation of this file.
1/* Generic GNU/Linux target using traditional ptrace register access.
2
3 Copyright (C) 1988-2023 Free Software Foundation, Inc.
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"
25#include "gdbarch.h"
26
27/* Fetch register REGNUM from the inferior. */
28
29void
31{
32 struct gdbarch *gdbarch = regcache->arch ();
33 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
34 CORE_ADDR addr;
35 gdb_byte *buf;
36 size_t size;
37 pid_t pid;
38 int i;
39
40 /* This isn't really an address, but ptrace thinks of it as one. */
41 addr = register_u_offset (gdbarch, regnum, 0);
42 if (addr == (CORE_ADDR)-1
44 {
45 regcache->raw_supply (regnum, NULL);
46 return;
47 }
48
50
52 buf = (gdb_byte *) alloca (size);
53
54 /* Read the register contents from the inferior a chunk at a time. */
55 for (i = 0; i < size; i += sizeof (PTRACE_TYPE_RET))
56 {
57 size_t chunk = std::min (sizeof (PTRACE_TYPE_RET), size - i);
59
60 errno = 0;
61 val = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3) (uintptr_t) addr, 0);
62 if (errno != 0)
63 error (_("Couldn't read register %s (#%d): %s."),
65 regnum, safe_strerror (errno));
66 store_unsigned_integer (buf + i, chunk, byte_order, val);
67
68 addr += sizeof (PTRACE_TYPE_RET);
69 }
71}
72
73/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
74 for all registers. */
75
76void
87
88/* Store register REGNUM into the inferior. */
89
90void
92 int regnum)
93{
94 struct gdbarch *gdbarch = regcache->arch ();
95 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
96 CORE_ADDR addr;
97 size_t size;
98 gdb_byte *buf;
99 pid_t pid;
100 int i;
101
102 /* This isn't really an address, but ptrace thinks of it as one. */
103 addr = register_u_offset (gdbarch, regnum, 1);
104 if (addr == (CORE_ADDR)-1
106 return;
107
109
111 buf = (gdb_byte *) alloca (size);
112
113 /* Write the register contents into the inferior a chunk at a time. */
115 for (i = 0; i < size; i += sizeof (PTRACE_TYPE_RET))
116 {
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);
121 errno = 0;
122 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3) (uintptr_t) addr, val);
123 if (errno != 0)
124 error (_("Couldn't write register %s (#%d): %s."),
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
135void
137{
138 if (regnum == -1)
139 for (regnum = 0;
141 regnum++)
143 else
145}
int regnum
virtual CORE_ADDR register_u_offset(struct gdbarch *gdbarch, int regnum, int store)=0
void fetch_registers(struct regcache *, int) override
void store_registers(struct regcache *, int) override
void fetch_register(struct regcache *regcache, int regnum)
void store_register(const struct regcache *regcache, int regnum)
gdbarch * arch() const
Definition regcache.c:231
void raw_collect(int regnum, void *buf) const override
Definition regcache.c:1127
void raw_supply(int regnum, const void *buf) override
Definition regcache.c:1062
ptid_t ptid() const
Definition regcache.h:408
#define PTRACE_TYPE_RET
Definition config.h:675
static void store_unsigned_integer(gdb_byte *addr, int len, enum bfd_endian byte_order, ULONGEST val)
Definition defs.h:515
static ULONGEST extract_unsigned_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition defs.h:480
#define PT_WRITE_U
Definition gdb_ptrace.h:67
#define ptrace(request, pid, addr, data)
Definition gdb_ptrace.h:141
#define PT_READ_U
Definition gdb_ptrace.h:55
int gdbarch_cannot_store_register(struct gdbarch *gdbarch, int regnum)
Definition gdbarch.c:2419
int gdbarch_cannot_fetch_register(struct gdbarch *gdbarch, int regnum)
Definition gdbarch.c:2402
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition gdbarch.c:1396
const char * gdbarch_register_name(struct gdbarch *gdbarch, int regnr)
Definition gdbarch.c:2173
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition gdbarch.c:1930
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition gnu-nat.c:1791
size_t size
Definition go32-nat.c:239
pid_t get_ptrace_pid(ptid_t ptid)
Definition inf-ptrace.c:238
#define PTRACE_TYPE_ARG3
int register_size(struct gdbarch *gdbarch, int regnum)
Definition regcache.c:170