* ada-exp.y (yyname, yyrule): Remap global variables that appear
[deliverable/binutils-gdb.git] / gdb / x86-64-linux-nat.c
CommitLineData
a4b6fc86 1/* Native-dependent code for GNU/Linux x86-64.
0a65a603
AC
2
3 Copyright 2001, 2002 Free Software Foundation, Inc.
4
53e95fcf
JS
5 Contributed by Jiri Smid, SuSE Labs.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24#include "defs.h"
25#include "inferior.h"
26#include "gdbcore.h"
27#include "regcache.h"
53e95fcf
JS
28#include "gdb_assert.h"
29#include "x86-64-tdep.h"
30
31#include <sys/ptrace.h>
32#include <sys/debugreg.h>
33#include <sys/syscall.h>
34#include <sys/procfs.h>
33a0a2ac
ML
35#include <sys/reg.h>
36
37/* Mapping between the general-purpose registers in `struct user'
38 format and GDB's register array layout. */
39
40static int x86_64_regmap[] = {
de220d0f 41 RAX, RBX, RCX, RDX,
33a0a2ac
ML
42 RSI, RDI, RBP, RSP,
43 R8, R9, R10, R11,
44 R12, R13, R14, R15,
7b3fabf0 45 RIP, EFLAGS,
de220d0f 46 DS, ES, FS, GS
33a0a2ac 47};
53e95fcf
JS
48
49static unsigned long
50x86_64_linux_dr_get (int regnum)
51{
52 int tid;
53 unsigned long value;
54
55 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
56 multi-threaded processes here. For now, pretend there is just
57 one thread. */
58 tid = PIDGET (inferior_ptid);
59
60 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
61 ptrace call fails breaks debugging remote targets. The correct
62 way to fix this is to add the hardware breakpoint and watchpoint
63 stuff to the target vectore. For now, just return zero if the
64 ptrace call fails. */
65 errno = 0;
66 value = ptrace (PT_READ_U, tid,
67 offsetof (struct user, u_debugreg[regnum]), 0);
68 if (errno != 0)
69#if 0
70 perror_with_name ("Couldn't read debug register");
71#else
72 return 0;
73#endif
74
75 return value;
76}
77
78static void
79x86_64_linux_dr_set (int regnum, unsigned long value)
80{
81 int tid;
82
83 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
84 multi-threaded processes here. For now, pretend there is just
85 one thread. */
86 tid = PIDGET (inferior_ptid);
87
88 errno = 0;
7b3fabf0 89 ptrace (PT_WRITE_U, tid, offsetof (struct user, u_debugreg[regnum]), value);
53e95fcf
JS
90 if (errno != 0)
91 perror_with_name ("Couldn't write debug register");
92}
93
94void
95x86_64_linux_dr_set_control (unsigned long control)
96{
97 x86_64_linux_dr_set (DR_CONTROL, control);
98}
99
100void
101x86_64_linux_dr_set_addr (int regnum, CORE_ADDR addr)
102{
103 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
104
105 x86_64_linux_dr_set (DR_FIRSTADDR + regnum, addr);
106}
107
108void
109x86_64_linux_dr_reset_addr (int regnum)
110{
111 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
112
113 x86_64_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
114}
115
116unsigned long
117x86_64_linux_dr_get_status (void)
118{
119 return x86_64_linux_dr_get (DR_STATUS);
120}
121\f
122
a4b6fc86
AC
123/* The register sets used in GNU/Linux ELF core-dumps are identical to
124 the register sets used by `ptrace'. */
53e95fcf
JS
125
126#define GETREGS_SUPPLIES(regno) \
de220d0f 127 (0 <= (regno) && (regno) < x86_64_num_gregs)
53e95fcf
JS
128#define GETFPREGS_SUPPLIES(regno) \
129 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
53e95fcf
JS
130\f
131
132/* Transfering the general-purpose registers between GDB, inferiors
133 and core files. */
134
135/* Fill GDB's register array with the general-purpose register values
136 in *GREGSETP. */
137
138void
139supply_gregset (elf_gregset_t * gregsetp)
140{
141 elf_greg_t *regp = (elf_greg_t *) gregsetp;
142 int i;
143
de220d0f 144 for (i = 0; i < x86_64_num_gregs; i++)
53e95fcf
JS
145 supply_register (i, (char *) (regp + x86_64_regmap[i]));
146}
147
148/* Fill register REGNO (if it is a general-purpose register) in
149 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
150 do this for all registers. */
151
152void
153fill_gregset (elf_gregset_t * gregsetp, int regno)
154{
155 elf_greg_t *regp = (elf_greg_t *) gregsetp;
156 int i;
157
de220d0f 158 for (i = 0; i < x86_64_num_gregs; i++)
53e95fcf 159 if ((regno == -1 || regno == i))
91fd20f7 160 read_register_gen (i, (char *) (regp + x86_64_regmap[i]));
53e95fcf
JS
161}
162
163/* Fetch all general-purpose registers from process/thread TID and
164 store their values in GDB's register array. */
165
166static void
167fetch_regs (int tid)
168{
169 elf_gregset_t regs;
170
171 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
7b3fabf0 172 perror_with_name ("Couldn't get registers");
53e95fcf
JS
173
174 supply_gregset (&regs);
175}
176
177/* Store all valid general-purpose registers in GDB's register array
178 into the process/thread specified by TID. */
179
180static void
181store_regs (int tid, int regno)
182{
183 elf_gregset_t regs;
184
185 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
186 perror_with_name ("Couldn't get registers");
187
188 fill_gregset (&regs, regno);
189
190 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
191 perror_with_name ("Couldn't write registers");
192}
193\f
194
195/* Transfering floating-point registers between GDB, inferiors and cores. */
196
8dda9770
ML
197static void *
198x86_64_fxsave_offset (elf_fpregset_t * fxsave, int regnum)
199{
1cf877ad 200 const char *reg_name;
8dda9770
ML
201 int reg_index;
202
203 gdb_assert (x86_64_num_gregs - 1 < regnum && regnum < x86_64_num_regs);
204
1cf877ad 205 reg_name = x86_64_register_name (regnum);
8dda9770
ML
206
207 if (reg_name[0] == 's' && reg_name[1] == 't')
208 {
209 reg_index = reg_name[2] - '0';
210 return &fxsave->st_space[reg_index * 2];
211 }
212
213 if (reg_name[0] == 'x' && reg_name[1] == 'm' && reg_name[2] == 'm')
214 {
215 reg_index = reg_name[3] - '0';
216 return &fxsave->xmm_space[reg_index * 4];
217 }
218
219 if (strcmp (reg_name, "mxcsr") == 0)
220 return &fxsave->mxcsr;
221
222 return NULL;
223}
224
225/* Fill GDB's register array with the floating-point and SSE register
226 values in *FXSAVE. This function masks off any of the reserved
227 bits in *FXSAVE. */
53e95fcf
JS
228
229void
8dda9770 230supply_fpregset (elf_fpregset_t * fxsave)
53e95fcf 231{
8dda9770
ML
232 int i, reg_st0, reg_mxcsr;
233
1cf877ad
ML
234 reg_st0 = x86_64_register_number ("st0");
235 reg_mxcsr = x86_64_register_number ("mxcsr");
8dda9770
ML
236
237 gdb_assert (reg_st0 > 0 && reg_mxcsr > reg_st0);
238
239 for (i = reg_st0; i <= reg_mxcsr; i++)
240 supply_register (i, x86_64_fxsave_offset (fxsave, i));
53e95fcf
JS
241}
242
8dda9770
ML
243/* Fill register REGNUM (if it is a floating-point or SSE register) in
244 *FXSAVE with the value in GDB's register array. If REGNUM is -1, do
245 this for all registers. This function doesn't touch any of the
246 reserved bits in *FXSAVE. */
53e95fcf
JS
247
248void
8dda9770 249fill_fpregset (elf_fpregset_t * fxsave, int regnum)
53e95fcf 250{
8dda9770
ML
251 int i, last_regnum = MXCSR_REGNUM;
252 void *ptr;
253
254 if (gdbarch_tdep (current_gdbarch)->num_xmm_regs == 0)
255 last_regnum = FOP_REGNUM;
256
257 for (i = FP0_REGNUM; i <= last_regnum; i++)
258 if (regnum == -1 || regnum == i)
259 {
260 ptr = x86_64_fxsave_offset (fxsave, i);
261 if (ptr)
262 regcache_collect (i, ptr);
263 }
53e95fcf
JS
264}
265
266/* Fetch all floating-point registers from process/thread TID and store
267 thier values in GDB's register array. */
268
269static void
270fetch_fpregs (int tid)
271{
272 elf_fpregset_t fpregs;
273
274 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
275 perror_with_name ("Couldn't get floating point status");
276
277 supply_fpregset (&fpregs);
278}
279
280/* Store all valid floating-point registers in GDB's register array
281 into the process/thread specified by TID. */
282
283static void
284store_fpregs (int tid, int regno)
285{
286 elf_fpregset_t fpregs;
287
288 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
289 perror_with_name ("Couldn't get floating point status");
290
291 fill_fpregset (&fpregs, regno);
292
293 if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
294 perror_with_name ("Couldn't write floating point status");
295}
296\f
297
298/* Transferring arbitrary registers between GDB and inferior. */
299
300/* Fetch register REGNO from the child process. If REGNO is -1, do
301 this for all registers (including the floating point and SSE
302 registers). */
303
304void
305fetch_inferior_registers (int regno)
306{
307 int tid;
308
a4b6fc86 309 /* GNU/Linux LWP ID's are process ID's. */
53e95fcf
JS
310 if ((tid = TIDGET (inferior_ptid)) == 0)
311 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
312
313 if (regno == -1)
314 {
315 fetch_regs (tid);
316 fetch_fpregs (tid);
317 return;
318 }
319
320 if (GETREGS_SUPPLIES (regno))
321 {
322 fetch_regs (tid);
323 return;
324 }
325
326 if (GETFPREGS_SUPPLIES (regno))
327 {
328 fetch_fpregs (tid);
329 return;
330 }
331
332 internal_error (__FILE__, __LINE__,
333 "Got request for bad register number %d.", regno);
334}
335
336/* Store register REGNO back into the child process. If REGNO is -1,
337 do this for all registers (including the floating point and SSE
338 registers). */
339void
340store_inferior_registers (int regno)
341{
342 int tid;
343
a4b6fc86 344 /* GNU/Linux LWP ID's are process ID's. */
53e95fcf
JS
345 if ((tid = TIDGET (inferior_ptid)) == 0)
346 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
347
348 if (regno == -1)
349 {
350 store_regs (tid, regno);
351 store_fpregs (tid, regno);
352 return;
353 }
354
355 if (GETREGS_SUPPLIES (regno))
356 {
357 store_regs (tid, regno);
358 return;
359 }
360
361 if (GETFPREGS_SUPPLIES (regno))
362 {
363 store_fpregs (tid, regno);
364 return;
365 }
366
367 internal_error (__FILE__, __LINE__,
368 "Got request to store bad register number %d.", regno);
369}
370\f
371
372static const unsigned char linux_syscall[] = { 0x0f, 0x05 };
373
374#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
375
376/* The system call number is stored in the %rax register. */
377#define LINUX_SYSCALL_REGNUM 0 /* %rax */
378
379/* We are specifically interested in the sigreturn and rt_sigreturn
380 system calls. */
381
382#ifndef SYS_sigreturn
383#define SYS_sigreturn __NR_sigreturn
384#endif
385#ifndef SYS_rt_sigreturn
386#define SYS_rt_sigreturn __NR_rt_sigreturn
387#endif
388
389/* Offset to saved processor flags, from <asm/sigcontext.h>. */
390#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (152)
391/* Offset to saved processor registers from <asm/ucontext.h> */
392#define LINUX_UCONTEXT_SIGCONTEXT_OFFSET (36)
393
53e95fcf 394/* Interpreting register set info found in core files. */
53e95fcf
JS
395/* Provide registers to GDB from a core file.
396
397 CORE_REG_SECT points to an array of bytes, which are the contents
398 of a `note' from a core file which BFD thinks might contain
399 register contents. CORE_REG_SIZE is its size.
400
401 WHICH says which register set corelow suspects this is:
402 0 --- the general-purpose register set, in elf_gregset_t format
403 2 --- the floating-point register set, in elf_fpregset_t format
404
a4b6fc86 405 REG_ADDR isn't used on GNU/Linux. */
53e95fcf
JS
406
407static void
408fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
409 int which, CORE_ADDR reg_addr)
410{
411 elf_gregset_t gregset;
412 elf_fpregset_t fpregset;
413 switch (which)
414 {
415 case 0:
416 if (core_reg_size != sizeof (gregset))
417 warning ("Wrong size gregset in core file.");
418 else
419 {
420 memcpy (&gregset, core_reg_sect, sizeof (gregset));
421 supply_gregset (&gregset);
422 }
423 break;
424
425 case 2:
426 if (core_reg_size != sizeof (fpregset))
427 warning ("Wrong size fpregset in core file.");
428 else
429 {
430 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
431 supply_fpregset (&fpregset);
432 }
433 break;
434
435 default:
436 /* We've covered all the kinds of registers we know about here,
437 so this must be something we wouldn't know what to do with
438 anyway. Just ignore it. */
439 break;
440 }
441}
442
a4b6fc86 443/* Register that we are able to handle GNU/Linux ELF core file formats. */
53e95fcf
JS
444
445static struct core_fns linux_elf_core_fns = {
446 bfd_target_elf_flavour, /* core_flavour */
447 default_check_format, /* check_format */
448 default_core_sniffer, /* core_sniffer */
449 fetch_core_registers, /* core_read_registers */
450 NULL /* next */
451};
452\f
453
454#if !defined (offsetof)
455#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
456#endif
457
b7c4cbf8
AJ
458/* Return the address of register REGNUM. BLOCKEND is the value of
459 u.u_ar0, which should point to the registers. */
460CORE_ADDR
461x86_64_register_u_addr (CORE_ADDR blockend, int regnum)
462{
463 struct user u;
464 CORE_ADDR fpstate;
465 CORE_ADDR ubase;
466 ubase = blockend;
7b3fabf0 467 if (IS_FP_REGNUM (regnum))
b7c4cbf8
AJ
468 {
469 fpstate = ubase + ((char *) &u.i387.st_space - (char *) &u);
470 return (fpstate + 16 * (regnum - FP0_REGNUM));
471 }
7b3fabf0 472 else if (IS_SSE_REGNUM (regnum))
b7c4cbf8
AJ
473 {
474 fpstate = ubase + ((char *) &u.i387.xmm_space - (char *) &u);
475 return (fpstate + 16 * (regnum - XMM0_REGNUM));
476 }
477 else
478 return (ubase + 8 * x86_64_regmap[regnum]);
479}
480
53e95fcf
JS
481void
482_initialize_x86_64_linux_nat (void)
483{
484 add_core_fns (&linux_elf_core_fns);
485}
8cfda98c
ML
486
487int
488kernel_u_size (void)
489{
490 return (sizeof (struct user));
491}
This page took 0.148101 seconds and 4 git commands to generate.