1 /* Generic GNU/Linux target using traditional ptrace register access.
3 Copyright (C) 1988-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "linux-nat-trad.h"
23 #include "nat/gdb_ptrace.h"
24 #include "inf-ptrace.h"
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);
30 /* Fetch register REGNUM from the inferior. */
33 inf_ptrace_fetch_register (struct regcache
*regcache
, int regnum
)
35 struct gdbarch
*gdbarch
= regcache
->arch ();
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
))
47 regcache_raw_supply (regcache
, regnum
, NULL
);
51 pid
= get_ptrace_pid (regcache_get_ptid (regcache
));
53 size
= register_size (gdbarch
, regnum
);
54 gdb_assert ((size
% sizeof (PTRACE_TYPE_RET
)) == 0);
55 buf
= (PTRACE_TYPE_RET
*) alloca (size
);
57 /* Read the register contents from the inferior a chunk at a time. */
58 for (i
= 0; i
< size
/ sizeof (PTRACE_TYPE_RET
); i
++)
61 buf
[i
] = ptrace (PT_READ_U
, pid
, (PTRACE_TYPE_ARG3
)(uintptr_t)addr
, 0);
63 error (_("Couldn't read register %s (#%d): %s."),
64 gdbarch_register_name (gdbarch
, regnum
),
65 regnum
, safe_strerror (errno
));
67 addr
+= sizeof (PTRACE_TYPE_RET
);
69 regcache_raw_supply (regcache
, regnum
, buf
);
72 /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
76 inf_ptrace_fetch_registers (struct target_ops
*ops
,
77 struct regcache
*regcache
, int regnum
)
81 regnum
< gdbarch_num_regs (regcache
->arch ());
83 inf_ptrace_fetch_register (regcache
, regnum
);
85 inf_ptrace_fetch_register (regcache
, regnum
);
88 /* Store register REGNUM into the inferior. */
91 inf_ptrace_store_register (const struct regcache
*regcache
, int regnum
)
93 struct gdbarch
*gdbarch
= regcache
->arch ();
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
))
106 pid
= get_ptrace_pid (regcache_get_ptid (regcache
));
108 size
= register_size (gdbarch
, regnum
);
109 gdb_assert ((size
% sizeof (PTRACE_TYPE_RET
)) == 0);
110 buf
= (PTRACE_TYPE_RET
*) alloca (size
);
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
++)
117 ptrace (PT_WRITE_U
, pid
, (PTRACE_TYPE_ARG3
)(uintptr_t)addr
, buf
[i
]);
119 error (_("Couldn't write register %s (#%d): %s."),
120 gdbarch_register_name (gdbarch
, regnum
),
121 regnum
, safe_strerror (errno
));
123 addr
+= sizeof (PTRACE_TYPE_RET
);
127 /* Store register REGNUM back into the inferior. If REGNUM is -1, do
128 this for all registers. */
131 inf_ptrace_store_registers (struct target_ops
*ops
,
132 struct regcache
*regcache
, int regnum
)
136 regnum
< gdbarch_num_regs (regcache
->arch ());
138 inf_ptrace_store_register (regcache
, regnum
);
140 inf_ptrace_store_register (regcache
, regnum
);
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. */
148 linux_trad_target (CORE_ADDR (*register_u_offset
)(struct gdbarch
*, int, int))
150 struct target_ops
*t
= inf_ptrace_target();
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
;
157 linux_target_install_ops (t
);