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