* x86-64-tdep.h (sys/reg.h, x86_64_regmap): Moved to x86-64-linux-nat.c
[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"
28#include "i387-nat.h"
29#include "gdb_assert.h"
30#include "x86-64-tdep.h"
31
32#include <sys/ptrace.h>
33#include <sys/debugreg.h>
34#include <sys/syscall.h>
35#include <sys/procfs.h>
33a0a2ac
ML
36#include <sys/reg.h>
37
38/* Mapping between the general-purpose registers in `struct user'
39 format and GDB's register array layout. */
40
41static int x86_64_regmap[] = {
42 RAX, RDX, RCX, RBX,
43 RSI, RDI, RBP, RSP,
44 R8, R9, R10, R11,
45 R12, R13, R14, R15,
46 RIP, EFLAGS
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;
89 ptrace (PT_WRITE_U, tid,
90 offsetof (struct user, u_debugreg[regnum]), value);
91 if (errno != 0)
92 perror_with_name ("Couldn't write debug register");
93}
94
95void
96x86_64_linux_dr_set_control (unsigned long control)
97{
98 x86_64_linux_dr_set (DR_CONTROL, control);
99}
100
101void
102x86_64_linux_dr_set_addr (int regnum, CORE_ADDR addr)
103{
104 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
105
106 x86_64_linux_dr_set (DR_FIRSTADDR + regnum, addr);
107}
108
109void
110x86_64_linux_dr_reset_addr (int regnum)
111{
112 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
113
114 x86_64_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
115}
116
117unsigned long
118x86_64_linux_dr_get_status (void)
119{
120 return x86_64_linux_dr_get (DR_STATUS);
121}
122\f
123
a4b6fc86
AC
124/* The register sets used in GNU/Linux ELF core-dumps are identical to
125 the register sets used by `ptrace'. */
53e95fcf
JS
126
127#define GETREGS_SUPPLIES(regno) \
128 (0 <= (regno) && (regno) <= 17)
129#define GETFPREGS_SUPPLIES(regno) \
130 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
131
132#define PTRACE_XFER_TYPE unsigned long
133\f
134
135/* Transfering the general-purpose registers between GDB, inferiors
136 and core files. */
137
138/* Fill GDB's register array with the general-purpose register values
139 in *GREGSETP. */
140
141void
142supply_gregset (elf_gregset_t * gregsetp)
143{
144 elf_greg_t *regp = (elf_greg_t *) gregsetp;
145 int i;
146
147 for (i = 0; i < X86_64_NUM_GREGS; i++)
148 supply_register (i, (char *) (regp + x86_64_regmap[i]));
149}
150
151/* Fill register REGNO (if it is a general-purpose register) in
152 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
153 do this for all registers. */
154
155void
156fill_gregset (elf_gregset_t * gregsetp, int regno)
157{
158 elf_greg_t *regp = (elf_greg_t *) gregsetp;
159 int i;
160
161 for (i = 0; i < X86_64_NUM_GREGS; i++)
162 if ((regno == -1 || regno == i))
94cd915f 163 read_register_gen (i, regp + x86_64_regmap[i]);
53e95fcf
JS
164}
165
166/* Fetch all general-purpose registers from process/thread TID and
167 store their values in GDB's register array. */
168
169static void
170fetch_regs (int tid)
171{
172 elf_gregset_t regs;
173
174 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
175 perror_with_name ("Couldn't get registers");
176
177 supply_gregset (&regs);
178}
179
180/* Store all valid general-purpose registers in GDB's register array
181 into the process/thread specified by TID. */
182
183static void
184store_regs (int tid, int regno)
185{
186 elf_gregset_t regs;
187
188 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
189 perror_with_name ("Couldn't get registers");
190
191 fill_gregset (&regs, regno);
192
193 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
194 perror_with_name ("Couldn't write registers");
195}
196\f
197
198/* Transfering floating-point registers between GDB, inferiors and cores. */
199
200/* Fill GDB's register array with the floating-point register values in
201 *FPREGSETP. */
202
203void
204supply_fpregset (elf_fpregset_t * fpregsetp)
205{
206 i387_supply_fxsave ((char *) fpregsetp);
207}
208
209/* Fill register REGNO (if it is a floating-point register) in
210 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
211 do this for all registers. */
212
213void
214fill_fpregset (elf_fpregset_t * fpregsetp, int regno)
215{
216 i387_fill_fxsave ((char *) fpregsetp, regno);
217}
218
219/* Fetch all floating-point registers from process/thread TID and store
220 thier values in GDB's register array. */
221
222static void
223fetch_fpregs (int tid)
224{
225 elf_fpregset_t fpregs;
226
227 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
228 perror_with_name ("Couldn't get floating point status");
229
230 supply_fpregset (&fpregs);
231}
232
233/* Store all valid floating-point registers in GDB's register array
234 into the process/thread specified by TID. */
235
236static void
237store_fpregs (int tid, int regno)
238{
239 elf_fpregset_t fpregs;
240
241 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
242 perror_with_name ("Couldn't get floating point status");
243
244 fill_fpregset (&fpregs, regno);
245
246 if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
247 perror_with_name ("Couldn't write floating point status");
248}
249\f
250
251/* Transferring arbitrary registers between GDB and inferior. */
252
253/* Fetch register REGNO from the child process. If REGNO is -1, do
254 this for all registers (including the floating point and SSE
255 registers). */
256
257void
258fetch_inferior_registers (int regno)
259{
260 int tid;
261
a4b6fc86 262 /* GNU/Linux LWP ID's are process ID's. */
53e95fcf
JS
263 if ((tid = TIDGET (inferior_ptid)) == 0)
264 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
265
266 if (regno == -1)
267 {
268 fetch_regs (tid);
269 fetch_fpregs (tid);
270 return;
271 }
272
273 if (GETREGS_SUPPLIES (regno))
274 {
275 fetch_regs (tid);
276 return;
277 }
278
279 if (GETFPREGS_SUPPLIES (regno))
280 {
281 fetch_fpregs (tid);
282 return;
283 }
284
285 internal_error (__FILE__, __LINE__,
286 "Got request for bad register number %d.", regno);
287}
288
289/* Store register REGNO back into the child process. If REGNO is -1,
290 do this for all registers (including the floating point and SSE
291 registers). */
292void
293store_inferior_registers (int regno)
294{
295 int tid;
296
a4b6fc86 297 /* GNU/Linux LWP ID's are process ID's. */
53e95fcf
JS
298 if ((tid = TIDGET (inferior_ptid)) == 0)
299 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
300
301 if (regno == -1)
302 {
303 store_regs (tid, regno);
304 store_fpregs (tid, regno);
305 return;
306 }
307
308 if (GETREGS_SUPPLIES (regno))
309 {
310 store_regs (tid, regno);
311 return;
312 }
313
314 if (GETFPREGS_SUPPLIES (regno))
315 {
316 store_fpregs (tid, regno);
317 return;
318 }
319
320 internal_error (__FILE__, __LINE__,
321 "Got request to store bad register number %d.", regno);
322}
323\f
324
325static const unsigned char linux_syscall[] = { 0x0f, 0x05 };
326
327#define LINUX_SYSCALL_LEN (sizeof linux_syscall)
328
329/* The system call number is stored in the %rax register. */
330#define LINUX_SYSCALL_REGNUM 0 /* %rax */
331
332/* We are specifically interested in the sigreturn and rt_sigreturn
333 system calls. */
334
335#ifndef SYS_sigreturn
336#define SYS_sigreturn __NR_sigreturn
337#endif
338#ifndef SYS_rt_sigreturn
339#define SYS_rt_sigreturn __NR_rt_sigreturn
340#endif
341
342/* Offset to saved processor flags, from <asm/sigcontext.h>. */
343#define LINUX_SIGCONTEXT_EFLAGS_OFFSET (152)
344/* Offset to saved processor registers from <asm/ucontext.h> */
345#define LINUX_UCONTEXT_SIGCONTEXT_OFFSET (36)
346
347/* Resume execution of the inferior process.
348 If STEP is nonzero, single-step it.
349 If SIGNAL is nonzero, give it that signal. */
350
351void
352child_resume (ptid_t ptid, int step, enum target_signal signal)
353{
354 int pid = PIDGET (ptid);
355 int request = PTRACE_CONT;
356
357 if (pid == -1)
358 /* Resume all threads. */
359 /* I think this only gets used in the non-threaded case, where "resume
360 all threads" and "resume inferior_ptid" are the same. */
361 pid = PIDGET (inferior_ptid);
362
363 if (step)
364 {
365 CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid));
366 unsigned char buf[LINUX_SYSCALL_LEN];
367
368 request = PTRACE_SINGLESTEP;
369
370 /* Returning from a signal trampoline is done by calling a
371 special system call (sigreturn or rt_sigreturn, see
372 i386-linux-tdep.c for more information). This system call
373 restores the registers that were saved when the signal was
374 raised, including %eflags. That means that single-stepping
375 won't work. Instead, we'll have to modify the signal context
376 that's about to be restored, and set the trace flag there. */
377
378 /* First check if PC is at a system call. */
379 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
380 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
381 {
382 int syscall =
383 read_register_pid (LINUX_SYSCALL_REGNUM, pid_to_ptid (pid));
384
385 /* Then check the system call number. */
386 if (syscall == SYS_rt_sigreturn)
387 {
388 CORE_ADDR sp = read_register (SP_REGNUM);
389 CORE_ADDR addr = sp;
390 unsigned long int eflags;
391
392 addr +=
393 sizeof (struct siginfo) + LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
394
395 /* Set the trace flag in the context that's about to be
396 restored. */
397 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
398 read_memory (addr, (char *) &eflags, 8);
399 eflags |= 0x0100;
400 write_memory (addr, (char *) &eflags, 8);
401 }
402 }
403 }
404
405 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
406 perror_with_name ("ptrace");
407}
408\f
409
410/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
411 to debugger memory starting at MYADDR. Copy to inferior if
412 WRITE is nonzero. TARGET is ignored.
413
414 Returns the length copied, which is either the LEN argument or zero.
415 This xfer function does not do partial moves, since child_ops
416 doesn't allow memory operations to cross below us in the target stack
417 anyway. */
418
419int
420child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
0a65a603 421 struct mem_attrib *attrib, struct target_ops *target)
53e95fcf
JS
422{
423 register int i;
424 /* Round starting address down to longword boundary. */
425 register CORE_ADDR addr = memaddr & -sizeof (PTRACE_XFER_TYPE);
426 /* Round ending address up; get number of longwords that makes. */
427 register int count
428 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
429 / sizeof (PTRACE_XFER_TYPE);
430 /* Allocate buffer of that many longwords. */
94cd915f
MS
431 /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe
432 because it uses alloca to allocate a buffer of arbitrary size.
433 For very large xfers, this could crash GDB's stack. */
53e95fcf
JS
434 register PTRACE_XFER_TYPE *buffer
435 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
436
437 if (write)
438 {
439 /* Fill start and end extra bytes of buffer with existing memory data. */
440 if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE))
441 {
442 /* Need part of initial word -- fetch it. */
443 ptrace (PT_READ_I, PIDGET (inferior_ptid),
444 (PTRACE_ARG3_TYPE) addr, buffer);
445 }
446
447 if (count > 1) /* FIXME, avoid if even boundary */
448 {
449 ptrace (PT_READ_I, PIDGET (inferior_ptid),
450 ((PTRACE_ARG3_TYPE)
451 (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))),
452 buffer + count - 1);
453 }
454
455 /* Copy data to be written over corresponding part of buffer */
456
457 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
458 myaddr, len);
459
460 /* Write the entire buffer. */
461
462 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
463 {
464 errno = 0;
465 ptrace (PT_WRITE_D, PIDGET (inferior_ptid),
466 (PTRACE_ARG3_TYPE) addr, buffer[i]);
467 if (errno)
468 {
469 /* Using the appropriate one (I or D) is necessary for
470 Gould NP1, at least. */
471 errno = 0;
472 ptrace (PT_WRITE_I, PIDGET (inferior_ptid),
473 (PTRACE_ARG3_TYPE) addr, buffer[i]);
474 }
475 if (errno)
476 return 0;
477 }
478#ifdef CLEAR_INSN_CACHE
479 CLEAR_INSN_CACHE ();
480#endif
481 }
482 else
483 {
484 /* Read all the longwords */
485 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
486 {
487 errno = 0;
488 ptrace (PT_READ_I, PIDGET (inferior_ptid),
489 (PTRACE_ARG3_TYPE) addr, buffer + i);
490 if (errno)
491 return 0;
492 }
493
494 /* Copy appropriate bytes out of the buffer. */
495 memcpy (myaddr,
496 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
497 len);
498 }
499 return len;
500}
501
502/* Interpreting register set info found in core files. */
503
504/* Provide registers to GDB from a core file.
505
506 CORE_REG_SECT points to an array of bytes, which are the contents
507 of a `note' from a core file which BFD thinks might contain
508 register contents. CORE_REG_SIZE is its size.
509
510 WHICH says which register set corelow suspects this is:
511 0 --- the general-purpose register set, in elf_gregset_t format
512 2 --- the floating-point register set, in elf_fpregset_t format
513
a4b6fc86 514 REG_ADDR isn't used on GNU/Linux. */
53e95fcf
JS
515
516static void
517fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
518 int which, CORE_ADDR reg_addr)
519{
520 elf_gregset_t gregset;
521 elf_fpregset_t fpregset;
522 switch (which)
523 {
524 case 0:
525 if (core_reg_size != sizeof (gregset))
526 warning ("Wrong size gregset in core file.");
527 else
528 {
529 memcpy (&gregset, core_reg_sect, sizeof (gregset));
530 supply_gregset (&gregset);
531 }
532 break;
533
534 case 2:
535 if (core_reg_size != sizeof (fpregset))
536 warning ("Wrong size fpregset in core file.");
537 else
538 {
539 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
540 supply_fpregset (&fpregset);
541 }
542 break;
543
544 default:
545 /* We've covered all the kinds of registers we know about here,
546 so this must be something we wouldn't know what to do with
547 anyway. Just ignore it. */
548 break;
549 }
550}
551
a4b6fc86 552/* Register that we are able to handle GNU/Linux ELF core file formats. */
53e95fcf
JS
553
554static struct core_fns linux_elf_core_fns = {
555 bfd_target_elf_flavour, /* core_flavour */
556 default_check_format, /* check_format */
557 default_core_sniffer, /* core_sniffer */
558 fetch_core_registers, /* core_read_registers */
559 NULL /* next */
560};
561\f
562
563#if !defined (offsetof)
564#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
565#endif
566
567/* Record the value of the debug control register. */
568static long debug_control_mirror;
569
570/* Record which address associates with which register. */
571static CORE_ADDR address_lookup[DR_LASTADDR - DR_FIRSTADDR + 1];
572
b7c4cbf8
AJ
573/* Return the address of register REGNUM. BLOCKEND is the value of
574 u.u_ar0, which should point to the registers. */
575CORE_ADDR
576x86_64_register_u_addr (CORE_ADDR blockend, int regnum)
577{
578 struct user u;
579 CORE_ADDR fpstate;
580 CORE_ADDR ubase;
581 ubase = blockend;
582 if (IS_FP_REGNUM(regnum))
583 {
584 fpstate = ubase + ((char *) &u.i387.st_space - (char *) &u);
585 return (fpstate + 16 * (regnum - FP0_REGNUM));
586 }
587 else if (IS_SSE_REGNUM(regnum))
588 {
589 fpstate = ubase + ((char *) &u.i387.xmm_space - (char *) &u);
590 return (fpstate + 16 * (regnum - XMM0_REGNUM));
591 }
592 else
593 return (ubase + 8 * x86_64_regmap[regnum]);
594}
595
53e95fcf
JS
596void
597_initialize_x86_64_linux_nat (void)
598{
599 add_core_fns (&linux_elf_core_fns);
600}
This page took 0.093482 seconds and 4 git commands to generate.