Make inf_ptrace_trad Linux-only, move to separate file
[deliverable/binutils-gdb.git] / gdb / linux-nat-trad.c
1 /* Generic GNU/Linux target using traditional ptrace register access.
2
3 Copyright (C) 1988-2018 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
26 /* Pointer to a function that returns the offset within the user area
27 where a particular register is stored. */
28 static CORE_ADDR (*inf_ptrace_register_u_offset)(struct gdbarch *, int, int);
29
30 /* Fetch register REGNUM from the inferior. */
31
32 static void
33 inf_ptrace_fetch_register (struct regcache *regcache, int regnum)
34 {
35 struct gdbarch *gdbarch = regcache->arch ();
36 CORE_ADDR addr;
37 size_t size;
38 PTRACE_TYPE_RET *buf;
39 pid_t pid;
40 int i;
41
42 /* This isn't really an address, but ptrace thinks of it as one. */
43 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 0);
44 if (addr == (CORE_ADDR)-1
45 || gdbarch_cannot_fetch_register (gdbarch, regnum))
46 {
47 regcache_raw_supply (regcache, regnum, NULL);
48 return;
49 }
50
51 pid = get_ptrace_pid (regcache_get_ptid (regcache));
52
53 size = register_size (gdbarch, regnum);
54 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
55 buf = (PTRACE_TYPE_RET *) alloca (size);
56
57 /* Read the register contents from the inferior a chunk at a time. */
58 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
59 {
60 errno = 0;
61 buf[i] = 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."),
64 gdbarch_register_name (gdbarch, regnum),
65 regnum, safe_strerror (errno));
66
67 addr += sizeof (PTRACE_TYPE_RET);
68 }
69 regcache_raw_supply (regcache, regnum, buf);
70 }
71
72 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
73 for all registers. */
74
75 static void
76 inf_ptrace_fetch_registers (struct target_ops *ops,
77 struct regcache *regcache, int regnum)
78 {
79 if (regnum == -1)
80 for (regnum = 0;
81 regnum < gdbarch_num_regs (regcache->arch ());
82 regnum++)
83 inf_ptrace_fetch_register (regcache, regnum);
84 else
85 inf_ptrace_fetch_register (regcache, regnum);
86 }
87
88 /* Store register REGNUM into the inferior. */
89
90 static void
91 inf_ptrace_store_register (const struct regcache *regcache, int regnum)
92 {
93 struct gdbarch *gdbarch = regcache->arch ();
94 CORE_ADDR addr;
95 size_t size;
96 PTRACE_TYPE_RET *buf;
97 pid_t pid;
98 int i;
99
100 /* This isn't really an address, but ptrace thinks of it as one. */
101 addr = inf_ptrace_register_u_offset (gdbarch, regnum, 1);
102 if (addr == (CORE_ADDR)-1
103 || gdbarch_cannot_store_register (gdbarch, regnum))
104 return;
105
106 pid = get_ptrace_pid (regcache_get_ptid (regcache));
107
108 size = register_size (gdbarch, regnum);
109 gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
110 buf = (PTRACE_TYPE_RET *) alloca (size);
111
112 /* Write the register contents into the inferior a chunk at a time. */
113 regcache_raw_collect (regcache, regnum, buf);
114 for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
115 {
116 errno = 0;
117 ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)(uintptr_t)addr, buf[i]);
118 if (errno != 0)
119 error (_("Couldn't write register %s (#%d): %s."),
120 gdbarch_register_name (gdbarch, regnum),
121 regnum, safe_strerror (errno));
122
123 addr += sizeof (PTRACE_TYPE_RET);
124 }
125 }
126
127 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
128 this for all registers. */
129
130 static void
131 inf_ptrace_store_registers (struct target_ops *ops,
132 struct regcache *regcache, int regnum)
133 {
134 if (regnum == -1)
135 for (regnum = 0;
136 regnum < gdbarch_num_regs (regcache->arch ());
137 regnum++)
138 inf_ptrace_store_register (regcache, regnum);
139 else
140 inf_ptrace_store_register (regcache, regnum);
141 }
142
143 /* Create a "traditional" Linux/ptrace target. REGISTER_U_OFFSET
144 should be a function returning the offset within the user area
145 where a particular register is stored. */
146
147 struct target_ops *
148 linux_trad_target (CORE_ADDR (*register_u_offset)(struct gdbarch *, int, int))
149 {
150 struct target_ops *t = inf_ptrace_target();
151
152 gdb_assert (register_u_offset);
153 inf_ptrace_register_u_offset = register_u_offset;
154 t->to_fetch_registers = inf_ptrace_fetch_registers;
155 t->to_store_registers = inf_ptrace_store_registers;
156
157 linux_target_install_ops (t);
158
159 return t;
160 }
This page took 0.035266 seconds and 5 git commands to generate.