Remove unnecessary function prototypes.
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
1 /* Native-dependent code for GNU/Linux i386.
2
3 Copyright (C) 1999-2017 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 "inferior.h"
22 #include "gdbcore.h"
23 #include "regcache.h"
24 #include "elf/common.h"
25 #include "nat/gdb_ptrace.h"
26 #include <sys/uio.h>
27 #include "gregset.h"
28 #include "gdb_proc_service.h"
29
30 #include "i386-linux-nat.h"
31 #include "i387-tdep.h"
32 #include "i386-tdep.h"
33 #include "i386-linux-tdep.h"
34 #include "x86-xstate.h"
35
36 #include "linux-nat.h"
37 #include "x86-linux-nat.h"
38 #include "nat/linux-ptrace.h"
39 #include "inf-ptrace.h"
40
41 /* The register sets used in GNU/Linux ELF core-dumps are identical to
42 the register sets in `struct user' that is used for a.out
43 core-dumps, and is also used by `ptrace'. The corresponding types
44 are `elf_gregset_t' for the general-purpose registers (with
45 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
46 for the floating-point registers.
47
48 Those types used to be available under the names `gregset_t' and
49 `fpregset_t' too, and this file used those names in the past. But
50 those names are now used for the register sets used in the
51 `mcontext_t' type, and have a different size and layout. */
52
53 /* Which ptrace request retrieves which registers?
54 These apply to the corresponding SET requests as well. */
55
56 #define GETREGS_SUPPLIES(regno) \
57 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
58
59 #define GETFPXREGS_SUPPLIES(regno) \
60 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
61
62 #define GETXSTATEREGS_SUPPLIES(regno) \
63 (I386_ST0_REGNUM <= (regno) && (regno) < I386_PKEYS_NUM_REGS)
64
65 /* Does the current host support the GETREGS request? */
66 int have_ptrace_getregs =
67 #ifdef HAVE_PTRACE_GETREGS
68 1
69 #else
70 0
71 #endif
72 ;
73
74 /* Does the current host support the GETFPXREGS request? The header
75 file may or may not define it, and even if it is defined, the
76 kernel will return EIO if it's running on a pre-SSE processor.
77
78 My instinct is to attach this to some architecture- or
79 target-specific data structure, but really, a particular GDB
80 process can only run on top of one kernel at a time. So it's okay
81 for this to be a simple variable. */
82 int have_ptrace_getfpxregs =
83 #ifdef HAVE_PTRACE_GETFPXREGS
84 -1
85 #else
86 0
87 #endif
88 ;
89 \f
90
91 /* Accessing registers through the U area, one at a time. */
92
93 /* Fetch one register. */
94
95 static void
96 fetch_register (struct regcache *regcache, int regno)
97 {
98 pid_t tid;
99 int val;
100
101 gdb_assert (!have_ptrace_getregs);
102 if (i386_linux_gregset_reg_offset[regno] == -1)
103 {
104 regcache_raw_supply (regcache, regno, NULL);
105 return;
106 }
107
108 tid = get_ptrace_pid (regcache_get_ptid (regcache));
109
110 errno = 0;
111 val = ptrace (PTRACE_PEEKUSER, tid,
112 i386_linux_gregset_reg_offset[regno], 0);
113 if (errno != 0)
114 error (_("Couldn't read register %s (#%d): %s."),
115 gdbarch_register_name (get_regcache_arch (regcache), regno),
116 regno, safe_strerror (errno));
117
118 regcache_raw_supply (regcache, regno, &val);
119 }
120
121 /* Store one register. */
122
123 static void
124 store_register (const struct regcache *regcache, int regno)
125 {
126 pid_t tid;
127 int val;
128
129 gdb_assert (!have_ptrace_getregs);
130 if (i386_linux_gregset_reg_offset[regno] == -1)
131 return;
132
133 tid = get_ptrace_pid (regcache_get_ptid (regcache));
134
135 errno = 0;
136 regcache_raw_collect (regcache, regno, &val);
137 ptrace (PTRACE_POKEUSER, tid,
138 i386_linux_gregset_reg_offset[regno], val);
139 if (errno != 0)
140 error (_("Couldn't write register %s (#%d): %s."),
141 gdbarch_register_name (get_regcache_arch (regcache), regno),
142 regno, safe_strerror (errno));
143 }
144 \f
145
146 /* Transfering the general-purpose registers between GDB, inferiors
147 and core files. */
148
149 /* Fill GDB's register array with the general-purpose register values
150 in *GREGSETP. */
151
152 void
153 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
154 {
155 const gdb_byte *regp = (const gdb_byte *) gregsetp;
156 int i;
157
158 for (i = 0; i < I386_NUM_GREGS; i++)
159 regcache_raw_supply (regcache, i,
160 regp + i386_linux_gregset_reg_offset[i]);
161
162 if (I386_LINUX_ORIG_EAX_REGNUM
163 < gdbarch_num_regs (get_regcache_arch (regcache)))
164 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
165 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
166 }
167
168 /* Fill register REGNO (if it is a general-purpose register) in
169 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
170 do this for all registers. */
171
172 void
173 fill_gregset (const struct regcache *regcache,
174 elf_gregset_t *gregsetp, int regno)
175 {
176 gdb_byte *regp = (gdb_byte *) gregsetp;
177 int i;
178
179 for (i = 0; i < I386_NUM_GREGS; i++)
180 if (regno == -1 || regno == i)
181 regcache_raw_collect (regcache, i,
182 regp + i386_linux_gregset_reg_offset[i]);
183
184 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
185 && I386_LINUX_ORIG_EAX_REGNUM
186 < gdbarch_num_regs (get_regcache_arch (regcache)))
187 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
188 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
189 }
190
191 #ifdef HAVE_PTRACE_GETREGS
192
193 /* Fetch all general-purpose registers from process/thread TID and
194 store their values in GDB's register array. */
195
196 static void
197 fetch_regs (struct regcache *regcache, int tid)
198 {
199 elf_gregset_t regs;
200 elf_gregset_t *regs_p = &regs;
201
202 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
203 {
204 if (errno == EIO)
205 {
206 /* The kernel we're running on doesn't support the GETREGS
207 request. Reset `have_ptrace_getregs'. */
208 have_ptrace_getregs = 0;
209 return;
210 }
211
212 perror_with_name (_("Couldn't get registers"));
213 }
214
215 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
216 }
217
218 /* Store all valid general-purpose registers in GDB's register array
219 into the process/thread specified by TID. */
220
221 static void
222 store_regs (const struct regcache *regcache, int tid, int regno)
223 {
224 elf_gregset_t regs;
225
226 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
227 perror_with_name (_("Couldn't get registers"));
228
229 fill_gregset (regcache, &regs, regno);
230
231 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
232 perror_with_name (_("Couldn't write registers"));
233 }
234
235 #else
236
237 static void fetch_regs (struct regcache *regcache, int tid) {}
238 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
239
240 #endif
241 \f
242
243 /* Transfering floating-point registers between GDB, inferiors and cores. */
244
245 /* Fill GDB's register array with the floating-point register values in
246 *FPREGSETP. */
247
248 void
249 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
250 {
251 i387_supply_fsave (regcache, -1, fpregsetp);
252 }
253
254 /* Fill register REGNO (if it is a floating-point register) in
255 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
256 do this for all registers. */
257
258 void
259 fill_fpregset (const struct regcache *regcache,
260 elf_fpregset_t *fpregsetp, int regno)
261 {
262 i387_collect_fsave (regcache, regno, fpregsetp);
263 }
264
265 #ifdef HAVE_PTRACE_GETREGS
266
267 /* Fetch all floating-point registers from process/thread TID and store
268 thier values in GDB's register array. */
269
270 static void
271 fetch_fpregs (struct regcache *regcache, int tid)
272 {
273 elf_fpregset_t fpregs;
274
275 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
276 perror_with_name (_("Couldn't get floating point status"));
277
278 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
279 }
280
281 /* Store all valid floating-point registers in GDB's register array
282 into the process/thread specified by TID. */
283
284 static void
285 store_fpregs (const struct regcache *regcache, int tid, int regno)
286 {
287 elf_fpregset_t fpregs;
288
289 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
290 perror_with_name (_("Couldn't get floating point status"));
291
292 fill_fpregset (regcache, &fpregs, regno);
293
294 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
295 perror_with_name (_("Couldn't write floating point status"));
296 }
297
298 #else
299
300 static void
301 fetch_fpregs (struct regcache *regcache, int tid)
302 {
303 }
304
305 static void
306 store_fpregs (const struct regcache *regcache, int tid, int regno)
307 {
308 }
309
310 #endif
311 \f
312
313 /* Transfering floating-point and SSE registers to and from GDB. */
314
315 /* Fetch all registers covered by the PTRACE_GETREGSET request from
316 process/thread TID and store their values in GDB's register array.
317 Return non-zero if successful, zero otherwise. */
318
319 static int
320 fetch_xstateregs (struct regcache *regcache, int tid)
321 {
322 char xstateregs[X86_XSTATE_MAX_SIZE];
323 struct iovec iov;
324
325 if (have_ptrace_getregset != TRIBOOL_TRUE)
326 return 0;
327
328 iov.iov_base = xstateregs;
329 iov.iov_len = sizeof(xstateregs);
330 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
331 &iov) < 0)
332 perror_with_name (_("Couldn't read extended state status"));
333
334 i387_supply_xsave (regcache, -1, xstateregs);
335 return 1;
336 }
337
338 /* Store all valid registers in GDB's register array covered by the
339 PTRACE_SETREGSET request into the process/thread specified by TID.
340 Return non-zero if successful, zero otherwise. */
341
342 static int
343 store_xstateregs (const struct regcache *regcache, int tid, int regno)
344 {
345 char xstateregs[X86_XSTATE_MAX_SIZE];
346 struct iovec iov;
347
348 if (have_ptrace_getregset != TRIBOOL_TRUE)
349 return 0;
350
351 iov.iov_base = xstateregs;
352 iov.iov_len = sizeof(xstateregs);
353 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
354 &iov) < 0)
355 perror_with_name (_("Couldn't read extended state status"));
356
357 i387_collect_xsave (regcache, regno, xstateregs, 0);
358
359 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
360 (int) &iov) < 0)
361 perror_with_name (_("Couldn't write extended state status"));
362
363 return 1;
364 }
365
366 #ifdef HAVE_PTRACE_GETFPXREGS
367
368 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
369 process/thread TID and store their values in GDB's register array.
370 Return non-zero if successful, zero otherwise. */
371
372 static int
373 fetch_fpxregs (struct regcache *regcache, int tid)
374 {
375 elf_fpxregset_t fpxregs;
376
377 if (! have_ptrace_getfpxregs)
378 return 0;
379
380 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
381 {
382 if (errno == EIO)
383 {
384 have_ptrace_getfpxregs = 0;
385 return 0;
386 }
387
388 perror_with_name (_("Couldn't read floating-point and SSE registers"));
389 }
390
391 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
392 return 1;
393 }
394
395 /* Store all valid registers in GDB's register array covered by the
396 PTRACE_SETFPXREGS request into the process/thread specified by TID.
397 Return non-zero if successful, zero otherwise. */
398
399 static int
400 store_fpxregs (const struct regcache *regcache, int tid, int regno)
401 {
402 elf_fpxregset_t fpxregs;
403
404 if (! have_ptrace_getfpxregs)
405 return 0;
406
407 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
408 {
409 if (errno == EIO)
410 {
411 have_ptrace_getfpxregs = 0;
412 return 0;
413 }
414
415 perror_with_name (_("Couldn't read floating-point and SSE registers"));
416 }
417
418 i387_collect_fxsave (regcache, regno, &fpxregs);
419
420 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
421 perror_with_name (_("Couldn't write floating-point and SSE registers"));
422
423 return 1;
424 }
425
426 #else
427
428 static int
429 fetch_fpxregs (struct regcache *regcache, int tid)
430 {
431 return 0;
432 }
433
434 static int
435 store_fpxregs (const struct regcache *regcache, int tid, int regno)
436 {
437 return 0;
438 }
439
440 #endif /* HAVE_PTRACE_GETFPXREGS */
441 \f
442
443 /* Transferring arbitrary registers between GDB and inferior. */
444
445 /* Fetch register REGNO from the child process. If REGNO is -1, do
446 this for all registers (including the floating point and SSE
447 registers). */
448
449 static void
450 i386_linux_fetch_inferior_registers (struct target_ops *ops,
451 struct regcache *regcache, int regno)
452 {
453 pid_t tid;
454
455 /* Use the old method of peeking around in `struct user' if the
456 GETREGS request isn't available. */
457 if (!have_ptrace_getregs)
458 {
459 int i;
460
461 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
462 if (regno == -1 || regno == i)
463 fetch_register (regcache, i);
464
465 return;
466 }
467
468 tid = get_ptrace_pid (regcache_get_ptid (regcache));
469
470 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
471 transfers more registers in one system call, and we'll cache the
472 results. But remember that fetch_fpxregs can fail, and return
473 zero. */
474 if (regno == -1)
475 {
476 fetch_regs (regcache, tid);
477
478 /* The call above might reset `have_ptrace_getregs'. */
479 if (!have_ptrace_getregs)
480 {
481 i386_linux_fetch_inferior_registers (ops, regcache, regno);
482 return;
483 }
484
485 if (fetch_xstateregs (regcache, tid))
486 return;
487 if (fetch_fpxregs (regcache, tid))
488 return;
489 fetch_fpregs (regcache, tid);
490 return;
491 }
492
493 if (GETREGS_SUPPLIES (regno))
494 {
495 fetch_regs (regcache, tid);
496 return;
497 }
498
499 if (GETXSTATEREGS_SUPPLIES (regno))
500 {
501 if (fetch_xstateregs (regcache, tid))
502 return;
503 }
504
505 if (GETFPXREGS_SUPPLIES (regno))
506 {
507 if (fetch_fpxregs (regcache, tid))
508 return;
509
510 /* Either our processor or our kernel doesn't support the SSE
511 registers, so read the FP registers in the traditional way,
512 and fill the SSE registers with dummy values. It would be
513 more graceful to handle differences in the register set using
514 gdbarch. Until then, this will at least make things work
515 plausibly. */
516 fetch_fpregs (regcache, tid);
517 return;
518 }
519
520 internal_error (__FILE__, __LINE__,
521 _("Got request for bad register number %d."), regno);
522 }
523
524 /* Store register REGNO back into the child process. If REGNO is -1,
525 do this for all registers (including the floating point and SSE
526 registers). */
527 static void
528 i386_linux_store_inferior_registers (struct target_ops *ops,
529 struct regcache *regcache, int regno)
530 {
531 pid_t tid;
532
533 /* Use the old method of poking around in `struct user' if the
534 SETREGS request isn't available. */
535 if (!have_ptrace_getregs)
536 {
537 int i;
538
539 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
540 if (regno == -1 || regno == i)
541 store_register (regcache, i);
542
543 return;
544 }
545
546 tid = get_ptrace_pid (regcache_get_ptid (regcache));
547
548 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
549 transfers more registers in one system call. But remember that
550 store_fpxregs can fail, and return zero. */
551 if (regno == -1)
552 {
553 store_regs (regcache, tid, regno);
554 if (store_xstateregs (regcache, tid, regno))
555 return;
556 if (store_fpxregs (regcache, tid, regno))
557 return;
558 store_fpregs (regcache, tid, regno);
559 return;
560 }
561
562 if (GETREGS_SUPPLIES (regno))
563 {
564 store_regs (regcache, tid, regno);
565 return;
566 }
567
568 if (GETXSTATEREGS_SUPPLIES (regno))
569 {
570 if (store_xstateregs (regcache, tid, regno))
571 return;
572 }
573
574 if (GETFPXREGS_SUPPLIES (regno))
575 {
576 if (store_fpxregs (regcache, tid, regno))
577 return;
578
579 /* Either our processor or our kernel doesn't support the SSE
580 registers, so just write the FP registers in the traditional
581 way. */
582 store_fpregs (regcache, tid, regno);
583 return;
584 }
585
586 internal_error (__FILE__, __LINE__,
587 _("Got request to store bad register number %d."), regno);
588 }
589 \f
590
591 /* Called by libthread_db. Returns a pointer to the thread local
592 storage (or its descriptor). */
593
594 ps_err_e
595 ps_get_thread_area (struct ps_prochandle *ph,
596 lwpid_t lwpid, int idx, void **base)
597 {
598 unsigned int base_addr;
599 ps_err_e result;
600
601 result = x86_linux_get_thread_area (lwpid, (void *) idx, &base_addr);
602
603 if (result == PS_OK)
604 *(int *) base = base_addr;
605
606 return result;
607 }
608 \f
609
610 /* The instruction for a GNU/Linux system call is:
611 int $0x80
612 or 0xcd 0x80. */
613
614 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
615
616 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
617
618 /* The system call number is stored in the %eax register. */
619 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
620
621 /* We are specifically interested in the sigreturn and rt_sigreturn
622 system calls. */
623
624 #ifndef SYS_sigreturn
625 #define SYS_sigreturn 0x77
626 #endif
627 #ifndef SYS_rt_sigreturn
628 #define SYS_rt_sigreturn 0xad
629 #endif
630
631 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
632 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
633
634 /* Resume execution of the inferior process.
635 If STEP is nonzero, single-step it.
636 If SIGNAL is nonzero, give it that signal. */
637
638 static void
639 i386_linux_resume (struct target_ops *ops,
640 ptid_t ptid, int step, enum gdb_signal signal)
641 {
642 int pid = ptid_get_lwp (ptid);
643 int request;
644
645 if (catch_syscall_enabled () > 0)
646 request = PTRACE_SYSCALL;
647 else
648 request = PTRACE_CONT;
649
650 if (step)
651 {
652 struct regcache *regcache = get_thread_regcache (ptid);
653 struct gdbarch *gdbarch = get_regcache_arch (regcache);
654 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
655 ULONGEST pc;
656 gdb_byte buf[LINUX_SYSCALL_LEN];
657
658 request = PTRACE_SINGLESTEP;
659
660 regcache_cooked_read_unsigned (regcache,
661 gdbarch_pc_regnum (gdbarch), &pc);
662
663 /* Returning from a signal trampoline is done by calling a
664 special system call (sigreturn or rt_sigreturn, see
665 i386-linux-tdep.c for more information). This system call
666 restores the registers that were saved when the signal was
667 raised, including %eflags. That means that single-stepping
668 won't work. Instead, we'll have to modify the signal context
669 that's about to be restored, and set the trace flag there. */
670
671 /* First check if PC is at a system call. */
672 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
673 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
674 {
675 ULONGEST syscall;
676 regcache_cooked_read_unsigned (regcache,
677 LINUX_SYSCALL_REGNUM, &syscall);
678
679 /* Then check the system call number. */
680 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
681 {
682 ULONGEST sp, addr;
683 unsigned long int eflags;
684
685 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
686 if (syscall == SYS_rt_sigreturn)
687 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
688 + 20;
689 else
690 addr = sp;
691
692 /* Set the trace flag in the context that's about to be
693 restored. */
694 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
695 read_memory (addr, (gdb_byte *) &eflags, 4);
696 eflags |= 0x0100;
697 write_memory (addr, (gdb_byte *) &eflags, 4);
698 }
699 }
700 }
701
702 if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1)
703 perror_with_name (("ptrace"));
704 }
705
706 void
707 _initialize_i386_linux_nat (void)
708 {
709 /* Create a generic x86 GNU/Linux target. */
710 struct target_ops *t = x86_linux_create_target ();
711
712 /* Override the default ptrace resume method. */
713 t->to_resume = i386_linux_resume;
714
715 /* Add our register access methods. */
716 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
717 t->to_store_registers = i386_linux_store_inferior_registers;
718
719 /* Add the target. */
720 x86_linux_add_target (t);
721 }
This page took 0.084081 seconds and 5 git commands to generate.