[native x86 GNU/Linux] Access debug register mirror from the corresponding process.
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
1 /* Native-dependent code for GNU/Linux i386.
2
3 Copyright (C) 1999-2013 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 "i386-nat.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "regset.h"
26 #include "target.h"
27 #include "linux-nat.h"
28
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
31 #include "elf/common.h"
32 #include <sys/uio.h>
33 #include <sys/ptrace.h>
34 #include <sys/user.h>
35 #include <sys/procfs.h>
36
37 #ifdef HAVE_SYS_REG_H
38 #include <sys/reg.h>
39 #endif
40
41 #ifndef ORIG_EAX
42 #define ORIG_EAX -1
43 #endif
44
45 #ifdef HAVE_SYS_DEBUGREG_H
46 #include <sys/debugreg.h>
47 #endif
48
49 /* Prototypes for supply_gregset etc. */
50 #include "gregset.h"
51
52 #include "i387-tdep.h"
53 #include "i386-tdep.h"
54 #include "i386-linux-tdep.h"
55
56 /* Defines ps_err_e, struct ps_prochandle. */
57 #include "gdb_proc_service.h"
58
59 #include "i386-xstate.h"
60
61 #ifndef PTRACE_GETREGSET
62 #define PTRACE_GETREGSET 0x4204
63 #endif
64
65 #ifndef PTRACE_SETREGSET
66 #define PTRACE_SETREGSET 0x4205
67 #endif
68
69 /* Per-thread arch-specific data we want to keep. */
70
71 struct arch_lwp_info
72 {
73 /* Non-zero if our copy differs from what's recorded in the thread. */
74 int debug_registers_changed;
75 };
76
77 /* Does the current host support PTRACE_GETREGSET? */
78 static int have_ptrace_getregset = -1;
79 \f
80
81 /* The register sets used in GNU/Linux ELF core-dumps are identical to
82 the register sets in `struct user' that is used for a.out
83 core-dumps, and is also used by `ptrace'. The corresponding types
84 are `elf_gregset_t' for the general-purpose registers (with
85 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
86 for the floating-point registers.
87
88 Those types used to be available under the names `gregset_t' and
89 `fpregset_t' too, and this file used those names in the past. But
90 those names are now used for the register sets used in the
91 `mcontext_t' type, and have a different size and layout. */
92
93 /* Which ptrace request retrieves which registers?
94 These apply to the corresponding SET requests as well. */
95
96 #define GETREGS_SUPPLIES(regno) \
97 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
98
99 #define GETFPXREGS_SUPPLIES(regno) \
100 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
101
102 #define GETXSTATEREGS_SUPPLIES(regno) \
103 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX_NUM_REGS)
104
105 /* Does the current host support the GETREGS request? */
106 int have_ptrace_getregs =
107 #ifdef HAVE_PTRACE_GETREGS
108 1
109 #else
110 0
111 #endif
112 ;
113
114 /* Does the current host support the GETFPXREGS request? The header
115 file may or may not define it, and even if it is defined, the
116 kernel will return EIO if it's running on a pre-SSE processor.
117
118 My instinct is to attach this to some architecture- or
119 target-specific data structure, but really, a particular GDB
120 process can only run on top of one kernel at a time. So it's okay
121 for this to be a simple variable. */
122 int have_ptrace_getfpxregs =
123 #ifdef HAVE_PTRACE_GETFPXREGS
124 -1
125 #else
126 0
127 #endif
128 ;
129 \f
130
131 /* Accessing registers through the U area, one at a time. */
132
133 /* Fetch one register. */
134
135 static void
136 fetch_register (struct regcache *regcache, int regno)
137 {
138 int tid;
139 int val;
140
141 gdb_assert (!have_ptrace_getregs);
142 if (i386_linux_gregset_reg_offset[regno] == -1)
143 {
144 regcache_raw_supply (regcache, regno, NULL);
145 return;
146 }
147
148 /* GNU/Linux LWP ID's are process ID's. */
149 tid = TIDGET (inferior_ptid);
150 if (tid == 0)
151 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
152
153 errno = 0;
154 val = ptrace (PTRACE_PEEKUSER, tid,
155 i386_linux_gregset_reg_offset[regno], 0);
156 if (errno != 0)
157 error (_("Couldn't read register %s (#%d): %s."),
158 gdbarch_register_name (get_regcache_arch (regcache), regno),
159 regno, safe_strerror (errno));
160
161 regcache_raw_supply (regcache, regno, &val);
162 }
163
164 /* Store one register. */
165
166 static void
167 store_register (const struct regcache *regcache, int regno)
168 {
169 int tid;
170 int val;
171
172 gdb_assert (!have_ptrace_getregs);
173 if (i386_linux_gregset_reg_offset[regno] == -1)
174 return;
175
176 /* GNU/Linux LWP ID's are process ID's. */
177 tid = TIDGET (inferior_ptid);
178 if (tid == 0)
179 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
180
181 errno = 0;
182 regcache_raw_collect (regcache, regno, &val);
183 ptrace (PTRACE_POKEUSER, tid,
184 i386_linux_gregset_reg_offset[regno], val);
185 if (errno != 0)
186 error (_("Couldn't write register %s (#%d): %s."),
187 gdbarch_register_name (get_regcache_arch (regcache), regno),
188 regno, safe_strerror (errno));
189 }
190 \f
191
192 /* Transfering the general-purpose registers between GDB, inferiors
193 and core files. */
194
195 /* Fill GDB's register array with the general-purpose register values
196 in *GREGSETP. */
197
198 void
199 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
200 {
201 const gdb_byte *regp = (const gdb_byte *) gregsetp;
202 int i;
203
204 for (i = 0; i < I386_NUM_GREGS; i++)
205 regcache_raw_supply (regcache, i,
206 regp + i386_linux_gregset_reg_offset[i]);
207
208 if (I386_LINUX_ORIG_EAX_REGNUM
209 < gdbarch_num_regs (get_regcache_arch (regcache)))
210 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
211 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
212 }
213
214 /* Fill register REGNO (if it is a general-purpose register) in
215 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
216 do this for all registers. */
217
218 void
219 fill_gregset (const struct regcache *regcache,
220 elf_gregset_t *gregsetp, int regno)
221 {
222 gdb_byte *regp = (gdb_byte *) gregsetp;
223 int i;
224
225 for (i = 0; i < I386_NUM_GREGS; i++)
226 if (regno == -1 || regno == i)
227 regcache_raw_collect (regcache, i,
228 regp + i386_linux_gregset_reg_offset[i]);
229
230 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
231 && I386_LINUX_ORIG_EAX_REGNUM
232 < gdbarch_num_regs (get_regcache_arch (regcache)))
233 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM, regp
234 + i386_linux_gregset_reg_offset[I386_LINUX_ORIG_EAX_REGNUM]);
235 }
236
237 #ifdef HAVE_PTRACE_GETREGS
238
239 /* Fetch all general-purpose registers from process/thread TID and
240 store their values in GDB's register array. */
241
242 static void
243 fetch_regs (struct regcache *regcache, int tid)
244 {
245 elf_gregset_t regs;
246 elf_gregset_t *regs_p = &regs;
247
248 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
249 {
250 if (errno == EIO)
251 {
252 /* The kernel we're running on doesn't support the GETREGS
253 request. Reset `have_ptrace_getregs'. */
254 have_ptrace_getregs = 0;
255 return;
256 }
257
258 perror_with_name (_("Couldn't get registers"));
259 }
260
261 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
262 }
263
264 /* Store all valid general-purpose registers in GDB's register array
265 into the process/thread specified by TID. */
266
267 static void
268 store_regs (const struct regcache *regcache, int tid, int regno)
269 {
270 elf_gregset_t regs;
271
272 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
273 perror_with_name (_("Couldn't get registers"));
274
275 fill_gregset (regcache, &regs, regno);
276
277 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
278 perror_with_name (_("Couldn't write registers"));
279 }
280
281 #else
282
283 static void fetch_regs (struct regcache *regcache, int tid) {}
284 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
285
286 #endif
287 \f
288
289 /* Transfering floating-point registers between GDB, inferiors and cores. */
290
291 /* Fill GDB's register array with the floating-point register values in
292 *FPREGSETP. */
293
294 void
295 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
296 {
297 i387_supply_fsave (regcache, -1, fpregsetp);
298 }
299
300 /* Fill register REGNO (if it is a floating-point register) in
301 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
302 do this for all registers. */
303
304 void
305 fill_fpregset (const struct regcache *regcache,
306 elf_fpregset_t *fpregsetp, int regno)
307 {
308 i387_collect_fsave (regcache, regno, fpregsetp);
309 }
310
311 #ifdef HAVE_PTRACE_GETREGS
312
313 /* Fetch all floating-point registers from process/thread TID and store
314 thier values in GDB's register array. */
315
316 static void
317 fetch_fpregs (struct regcache *regcache, int tid)
318 {
319 elf_fpregset_t fpregs;
320
321 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
322 perror_with_name (_("Couldn't get floating point status"));
323
324 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
325 }
326
327 /* Store all valid floating-point registers in GDB's register array
328 into the process/thread specified by TID. */
329
330 static void
331 store_fpregs (const struct regcache *regcache, int tid, int regno)
332 {
333 elf_fpregset_t fpregs;
334
335 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
336 perror_with_name (_("Couldn't get floating point status"));
337
338 fill_fpregset (regcache, &fpregs, regno);
339
340 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
341 perror_with_name (_("Couldn't write floating point status"));
342 }
343
344 #else
345
346 static void
347 fetch_fpregs (struct regcache *regcache, int tid)
348 {
349 }
350
351 static void
352 store_fpregs (const struct regcache *regcache, int tid, int regno)
353 {
354 }
355
356 #endif
357 \f
358
359 /* Transfering floating-point and SSE registers to and from GDB. */
360
361 /* Fetch all registers covered by the PTRACE_GETREGSET request from
362 process/thread TID and store their values in GDB's register array.
363 Return non-zero if successful, zero otherwise. */
364
365 static int
366 fetch_xstateregs (struct regcache *regcache, int tid)
367 {
368 char xstateregs[I386_XSTATE_MAX_SIZE];
369 struct iovec iov;
370
371 if (!have_ptrace_getregset)
372 return 0;
373
374 iov.iov_base = xstateregs;
375 iov.iov_len = sizeof(xstateregs);
376 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
377 &iov) < 0)
378 perror_with_name (_("Couldn't read extended state status"));
379
380 i387_supply_xsave (regcache, -1, xstateregs);
381 return 1;
382 }
383
384 /* Store all valid registers in GDB's register array covered by the
385 PTRACE_SETREGSET request into the process/thread specified by TID.
386 Return non-zero if successful, zero otherwise. */
387
388 static int
389 store_xstateregs (const struct regcache *regcache, int tid, int regno)
390 {
391 char xstateregs[I386_XSTATE_MAX_SIZE];
392 struct iovec iov;
393
394 if (!have_ptrace_getregset)
395 return 0;
396
397 iov.iov_base = xstateregs;
398 iov.iov_len = sizeof(xstateregs);
399 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
400 &iov) < 0)
401 perror_with_name (_("Couldn't read extended state status"));
402
403 i387_collect_xsave (regcache, regno, xstateregs, 0);
404
405 if (ptrace (PTRACE_SETREGSET, tid, (unsigned int) NT_X86_XSTATE,
406 (int) &iov) < 0)
407 perror_with_name (_("Couldn't write extended state status"));
408
409 return 1;
410 }
411
412 #ifdef HAVE_PTRACE_GETFPXREGS
413
414 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
415 process/thread TID and store their values in GDB's register array.
416 Return non-zero if successful, zero otherwise. */
417
418 static int
419 fetch_fpxregs (struct regcache *regcache, int tid)
420 {
421 elf_fpxregset_t fpxregs;
422
423 if (! have_ptrace_getfpxregs)
424 return 0;
425
426 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
427 {
428 if (errno == EIO)
429 {
430 have_ptrace_getfpxregs = 0;
431 return 0;
432 }
433
434 perror_with_name (_("Couldn't read floating-point and SSE registers"));
435 }
436
437 i387_supply_fxsave (regcache, -1, (const elf_fpxregset_t *) &fpxregs);
438 return 1;
439 }
440
441 /* Store all valid registers in GDB's register array covered by the
442 PTRACE_SETFPXREGS request into the process/thread specified by TID.
443 Return non-zero if successful, zero otherwise. */
444
445 static int
446 store_fpxregs (const struct regcache *regcache, int tid, int regno)
447 {
448 elf_fpxregset_t fpxregs;
449
450 if (! have_ptrace_getfpxregs)
451 return 0;
452
453 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
454 {
455 if (errno == EIO)
456 {
457 have_ptrace_getfpxregs = 0;
458 return 0;
459 }
460
461 perror_with_name (_("Couldn't read floating-point and SSE registers"));
462 }
463
464 i387_collect_fxsave (regcache, regno, &fpxregs);
465
466 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
467 perror_with_name (_("Couldn't write floating-point and SSE registers"));
468
469 return 1;
470 }
471
472 #else
473
474 static int
475 fetch_fpxregs (struct regcache *regcache, int tid)
476 {
477 return 0;
478 }
479
480 static int
481 store_fpxregs (const struct regcache *regcache, int tid, int regno)
482 {
483 return 0;
484 }
485
486 #endif /* HAVE_PTRACE_GETFPXREGS */
487 \f
488
489 /* Transferring arbitrary registers between GDB and inferior. */
490
491 /* Fetch register REGNO from the child process. If REGNO is -1, do
492 this for all registers (including the floating point and SSE
493 registers). */
494
495 static void
496 i386_linux_fetch_inferior_registers (struct target_ops *ops,
497 struct regcache *regcache, int regno)
498 {
499 int tid;
500
501 /* Use the old method of peeking around in `struct user' if the
502 GETREGS request isn't available. */
503 if (!have_ptrace_getregs)
504 {
505 int i;
506
507 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
508 if (regno == -1 || regno == i)
509 fetch_register (regcache, i);
510
511 return;
512 }
513
514 /* GNU/Linux LWP ID's are process ID's. */
515 tid = TIDGET (inferior_ptid);
516 if (tid == 0)
517 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
518
519 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
520 transfers more registers in one system call, and we'll cache the
521 results. But remember that fetch_fpxregs can fail, and return
522 zero. */
523 if (regno == -1)
524 {
525 fetch_regs (regcache, tid);
526
527 /* The call above might reset `have_ptrace_getregs'. */
528 if (!have_ptrace_getregs)
529 {
530 i386_linux_fetch_inferior_registers (ops, regcache, regno);
531 return;
532 }
533
534 if (fetch_xstateregs (regcache, tid))
535 return;
536 if (fetch_fpxregs (regcache, tid))
537 return;
538 fetch_fpregs (regcache, tid);
539 return;
540 }
541
542 if (GETREGS_SUPPLIES (regno))
543 {
544 fetch_regs (regcache, tid);
545 return;
546 }
547
548 if (GETXSTATEREGS_SUPPLIES (regno))
549 {
550 if (fetch_xstateregs (regcache, tid))
551 return;
552 }
553
554 if (GETFPXREGS_SUPPLIES (regno))
555 {
556 if (fetch_fpxregs (regcache, tid))
557 return;
558
559 /* Either our processor or our kernel doesn't support the SSE
560 registers, so read the FP registers in the traditional way,
561 and fill the SSE registers with dummy values. It would be
562 more graceful to handle differences in the register set using
563 gdbarch. Until then, this will at least make things work
564 plausibly. */
565 fetch_fpregs (regcache, tid);
566 return;
567 }
568
569 internal_error (__FILE__, __LINE__,
570 _("Got request for bad register number %d."), regno);
571 }
572
573 /* Store register REGNO back into the child process. If REGNO is -1,
574 do this for all registers (including the floating point and SSE
575 registers). */
576 static void
577 i386_linux_store_inferior_registers (struct target_ops *ops,
578 struct regcache *regcache, int regno)
579 {
580 int tid;
581
582 /* Use the old method of poking around in `struct user' if the
583 SETREGS request isn't available. */
584 if (!have_ptrace_getregs)
585 {
586 int i;
587
588 for (i = 0; i < gdbarch_num_regs (get_regcache_arch (regcache)); i++)
589 if (regno == -1 || regno == i)
590 store_register (regcache, i);
591
592 return;
593 }
594
595 /* GNU/Linux LWP ID's are process ID's. */
596 tid = TIDGET (inferior_ptid);
597 if (tid == 0)
598 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
599
600 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
601 transfers more registers in one system call. But remember that
602 store_fpxregs can fail, and return zero. */
603 if (regno == -1)
604 {
605 store_regs (regcache, tid, regno);
606 if (store_xstateregs (regcache, tid, regno))
607 return;
608 if (store_fpxregs (regcache, tid, regno))
609 return;
610 store_fpregs (regcache, tid, regno);
611 return;
612 }
613
614 if (GETREGS_SUPPLIES (regno))
615 {
616 store_regs (regcache, tid, regno);
617 return;
618 }
619
620 if (GETXSTATEREGS_SUPPLIES (regno))
621 {
622 if (store_xstateregs (regcache, tid, regno))
623 return;
624 }
625
626 if (GETFPXREGS_SUPPLIES (regno))
627 {
628 if (store_fpxregs (regcache, tid, regno))
629 return;
630
631 /* Either our processor or our kernel doesn't support the SSE
632 registers, so just write the FP registers in the traditional
633 way. */
634 store_fpregs (regcache, tid, regno);
635 return;
636 }
637
638 internal_error (__FILE__, __LINE__,
639 _("Got request to store bad register number %d."), regno);
640 }
641 \f
642
643 /* Support for debug registers. */
644
645 /* Get debug register REGNUM value from only the one LWP of PTID. */
646
647 static unsigned long
648 i386_linux_dr_get (ptid_t ptid, int regnum)
649 {
650 int tid;
651 unsigned long value;
652
653 tid = TIDGET (ptid);
654 if (tid == 0)
655 tid = PIDGET (ptid);
656
657 errno = 0;
658 value = ptrace (PTRACE_PEEKUSER, tid,
659 offsetof (struct user, u_debugreg[regnum]), 0);
660 if (errno != 0)
661 perror_with_name (_("Couldn't read debug register"));
662
663 return value;
664 }
665
666 /* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
667
668 static void
669 i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
670 {
671 int tid;
672
673 tid = TIDGET (ptid);
674 if (tid == 0)
675 tid = PIDGET (ptid);
676
677 errno = 0;
678 ptrace (PTRACE_POKEUSER, tid,
679 offsetof (struct user, u_debugreg[regnum]), value);
680 if (errno != 0)
681 perror_with_name (_("Couldn't write debug register"));
682 }
683
684 /* Return the inferior's debug register REGNUM. */
685
686 static CORE_ADDR
687 i386_linux_dr_get_addr (int regnum)
688 {
689 /* DR6 and DR7 are retrieved with some other way. */
690 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
691
692 return i386_linux_dr_get (inferior_ptid, regnum);
693 }
694
695 /* Return the inferior's DR7 debug control register. */
696
697 static unsigned long
698 i386_linux_dr_get_control (void)
699 {
700 return i386_linux_dr_get (inferior_ptid, DR_CONTROL);
701 }
702
703 /* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
704
705 static unsigned long
706 i386_linux_dr_get_status (void)
707 {
708 return i386_linux_dr_get (inferior_ptid, DR_STATUS);
709 }
710
711 /* Callback for iterate_over_lwps. Update the debug registers of
712 LWP. */
713
714 static int
715 update_debug_registers_callback (struct lwp_info *lwp, void *arg)
716 {
717 if (lwp->arch_private == NULL)
718 lwp->arch_private = XCNEW (struct arch_lwp_info);
719
720 /* The actual update is done later just before resuming the lwp, we
721 just mark that the registers need updating. */
722 lwp->arch_private->debug_registers_changed = 1;
723
724 /* If the lwp isn't stopped, force it to momentarily pause, so we
725 can update its debug registers. */
726 if (!lwp->stopped)
727 linux_stop_lwp (lwp);
728
729 /* Continue the iteration. */
730 return 0;
731 }
732
733 /* Set DR_CONTROL to ADDR in all LWPs of the current inferior. */
734
735 static void
736 i386_linux_dr_set_control (unsigned long control)
737 {
738 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
739
740 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
741 }
742
743 /* Set address REGNUM (zero based) to ADDR in all LWPs of the current
744 inferior. */
745
746 static void
747 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
748 {
749 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
750
751 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
752
753 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
754 }
755
756 /* Called when resuming a thread.
757 If the debug regs have changed, update the thread's copies. */
758
759 static void
760 i386_linux_prepare_to_resume (struct lwp_info *lwp)
761 {
762 int clear_status = 0;
763
764 /* NULL means this is the main thread still going through the shell,
765 or, no watchpoint has been set yet. In that case, there's
766 nothing to do. */
767 if (lwp->arch_private == NULL)
768 return;
769
770 if (lwp->arch_private->debug_registers_changed)
771 {
772 struct i386_debug_reg_state *state
773 = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
774 int i;
775
776 /* See amd64_linux_prepare_to_resume for Linux kernel note on
777 i386_linux_dr_set calls ordering. */
778
779 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
780 if (state->dr_ref_count[i] > 0)
781 {
782 i386_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);
783
784 /* If we're setting a watchpoint, any change the inferior
785 had done itself to the debug registers needs to be
786 discarded, otherwise, i386_stopped_data_address can get
787 confused. */
788 clear_status = 1;
789 }
790
791 i386_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);
792
793 lwp->arch_private->debug_registers_changed = 0;
794 }
795
796 if (clear_status || lwp->stopped_by_watchpoint)
797 i386_linux_dr_set (lwp->ptid, DR_STATUS, 0);
798 }
799
800 static void
801 i386_linux_new_thread (struct lwp_info *lp)
802 {
803 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
804
805 info->debug_registers_changed = 1;
806
807 lp->arch_private = info;
808 }
809
810 /* linux_nat_new_fork hook. */
811
812 static void
813 i386_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
814 {
815 pid_t parent_pid;
816 struct i386_debug_reg_state *parent_state;
817 struct i386_debug_reg_state *child_state;
818
819 /* NULL means no watchpoint has ever been set in the parent. In
820 that case, there's nothing to do. */
821 if (parent->arch_private == NULL)
822 return;
823
824 /* Linux kernel before 2.6.33 commit
825 72f674d203cd230426437cdcf7dd6f681dad8b0d
826 will inherit hardware debug registers from parent
827 on fork/vfork/clone. Newer Linux kernels create such tasks with
828 zeroed debug registers.
829
830 GDB core assumes the child inherits the watchpoints/hw
831 breakpoints of the parent, and will remove them all from the
832 forked off process. Copy the debug registers mirrors into the
833 new process so that all breakpoints and watchpoints can be
834 removed together. The debug registers mirror will become zeroed
835 in the end before detaching the forked off process, thus making
836 this compatible with older Linux kernels too. */
837
838 parent_pid = ptid_get_pid (parent->ptid);
839 parent_state = i386_debug_reg_state (parent_pid);
840 child_state = i386_debug_reg_state (child_pid);
841 *child_state = *parent_state;
842 }
843
844 \f
845
846 /* Called by libthread_db. Returns a pointer to the thread local
847 storage (or its descriptor). */
848
849 ps_err_e
850 ps_get_thread_area (const struct ps_prochandle *ph,
851 lwpid_t lwpid, int idx, void **base)
852 {
853 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
854 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
855 4 byte integers in size: `entry_number', `base_addr', `limit',
856 and a bunch of status bits.
857
858 The values returned by this ptrace call should be part of the
859 regcache buffer, and ps_get_thread_area should channel its
860 request through the regcache. That way remote targets could
861 provide the value using the remote protocol and not this direct
862 call.
863
864 Is this function needed? I'm guessing that the `base' is the
865 address of a descriptor that libthread_db uses to find the
866 thread local address base that GDB needs. Perhaps that
867 descriptor is defined by the ABI. Anyway, given that
868 libthread_db calls this function without prompting (gdb
869 requesting tls base) I guess it needs info in there anyway. */
870 unsigned int desc[4];
871 gdb_assert (sizeof (int) == 4);
872
873 #ifndef PTRACE_GET_THREAD_AREA
874 #define PTRACE_GET_THREAD_AREA 25
875 #endif
876
877 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
878 (void *) idx, (unsigned long) &desc) < 0)
879 return PS_ERR;
880
881 *(int *)base = desc[1];
882 return PS_OK;
883 }
884 \f
885
886 /* The instruction for a GNU/Linux system call is:
887 int $0x80
888 or 0xcd 0x80. */
889
890 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
891
892 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
893
894 /* The system call number is stored in the %eax register. */
895 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
896
897 /* We are specifically interested in the sigreturn and rt_sigreturn
898 system calls. */
899
900 #ifndef SYS_sigreturn
901 #define SYS_sigreturn 0x77
902 #endif
903 #ifndef SYS_rt_sigreturn
904 #define SYS_rt_sigreturn 0xad
905 #endif
906
907 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
908 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
909
910 /* Resume execution of the inferior process.
911 If STEP is nonzero, single-step it.
912 If SIGNAL is nonzero, give it that signal. */
913
914 static void
915 i386_linux_resume (struct target_ops *ops,
916 ptid_t ptid, int step, enum gdb_signal signal)
917 {
918 int pid = PIDGET (ptid);
919
920 int request;
921
922 if (catch_syscall_enabled () > 0)
923 request = PTRACE_SYSCALL;
924 else
925 request = PTRACE_CONT;
926
927 if (step)
928 {
929 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
930 struct gdbarch *gdbarch = get_regcache_arch (regcache);
931 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
932 ULONGEST pc;
933 gdb_byte buf[LINUX_SYSCALL_LEN];
934
935 request = PTRACE_SINGLESTEP;
936
937 regcache_cooked_read_unsigned (regcache,
938 gdbarch_pc_regnum (gdbarch), &pc);
939
940 /* Returning from a signal trampoline is done by calling a
941 special system call (sigreturn or rt_sigreturn, see
942 i386-linux-tdep.c for more information). This system call
943 restores the registers that were saved when the signal was
944 raised, including %eflags. That means that single-stepping
945 won't work. Instead, we'll have to modify the signal context
946 that's about to be restored, and set the trace flag there. */
947
948 /* First check if PC is at a system call. */
949 if (target_read_memory (pc, buf, LINUX_SYSCALL_LEN) == 0
950 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
951 {
952 ULONGEST syscall;
953 regcache_cooked_read_unsigned (regcache,
954 LINUX_SYSCALL_REGNUM, &syscall);
955
956 /* Then check the system call number. */
957 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
958 {
959 ULONGEST sp, addr;
960 unsigned long int eflags;
961
962 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
963 if (syscall == SYS_rt_sigreturn)
964 addr = read_memory_unsigned_integer (sp + 8, 4, byte_order)
965 + 20;
966 else
967 addr = sp;
968
969 /* Set the trace flag in the context that's about to be
970 restored. */
971 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
972 read_memory (addr, (gdb_byte *) &eflags, 4);
973 eflags |= 0x0100;
974 write_memory (addr, (gdb_byte *) &eflags, 4);
975 }
976 }
977 }
978
979 if (ptrace (request, pid, 0, gdb_signal_to_host (signal)) == -1)
980 perror_with_name (("ptrace"));
981 }
982
983 static void (*super_post_startup_inferior) (ptid_t ptid);
984
985 static void
986 i386_linux_child_post_startup_inferior (ptid_t ptid)
987 {
988 i386_cleanup_dregs ();
989 super_post_startup_inferior (ptid);
990 }
991
992 /* Get Linux/x86 target description from running target. */
993
994 static const struct target_desc *
995 i386_linux_read_description (struct target_ops *ops)
996 {
997 int tid;
998 static uint64_t xcr0;
999
1000 /* GNU/Linux LWP ID's are process ID's. */
1001 tid = TIDGET (inferior_ptid);
1002 if (tid == 0)
1003 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
1004
1005 #ifdef HAVE_PTRACE_GETFPXREGS
1006 if (have_ptrace_getfpxregs == -1)
1007 {
1008 elf_fpxregset_t fpxregs;
1009
1010 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
1011 {
1012 have_ptrace_getfpxregs = 0;
1013 have_ptrace_getregset = 0;
1014 return tdesc_i386_mmx_linux;
1015 }
1016 }
1017 #endif
1018
1019 if (have_ptrace_getregset == -1)
1020 {
1021 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
1022 struct iovec iov;
1023
1024 iov.iov_base = xstateregs;
1025 iov.iov_len = sizeof (xstateregs);
1026
1027 /* Check if PTRACE_GETREGSET works. */
1028 if (ptrace (PTRACE_GETREGSET, tid, (unsigned int) NT_X86_XSTATE,
1029 &iov) < 0)
1030 have_ptrace_getregset = 0;
1031 else
1032 {
1033 have_ptrace_getregset = 1;
1034
1035 /* Get XCR0 from XSAVE extended state. */
1036 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
1037 / sizeof (long long))];
1038 }
1039 }
1040
1041 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
1042 if (have_ptrace_getregset
1043 && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK)
1044 return tdesc_i386_avx_linux;
1045 else
1046 return tdesc_i386_linux;
1047 }
1048
1049 /* -Wmissing-prototypes */
1050 extern initialize_file_ftype _initialize_i386_linux_nat;
1051
1052 void
1053 _initialize_i386_linux_nat (void)
1054 {
1055 struct target_ops *t;
1056
1057 /* Fill in the generic GNU/Linux methods. */
1058 t = linux_target ();
1059
1060 i386_use_watchpoints (t);
1061
1062 i386_dr_low.set_control = i386_linux_dr_set_control;
1063 i386_dr_low.set_addr = i386_linux_dr_set_addr;
1064 i386_dr_low.get_addr = i386_linux_dr_get_addr;
1065 i386_dr_low.get_status = i386_linux_dr_get_status;
1066 i386_dr_low.get_control = i386_linux_dr_get_control;
1067 i386_set_debug_register_length (4);
1068
1069 /* Override the default ptrace resume method. */
1070 t->to_resume = i386_linux_resume;
1071
1072 /* Override the GNU/Linux inferior startup hook. */
1073 super_post_startup_inferior = t->to_post_startup_inferior;
1074 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
1075
1076 /* Add our register access methods. */
1077 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
1078 t->to_store_registers = i386_linux_store_inferior_registers;
1079
1080 t->to_read_description = i386_linux_read_description;
1081
1082 /* Register the target. */
1083 linux_nat_add_target (t);
1084 linux_nat_set_new_thread (t, i386_linux_new_thread);
1085 linux_nat_set_new_fork (t, i386_linux_new_fork);
1086 linux_nat_set_forget_process (t, i386_forget_process);
1087 linux_nat_set_prepare_to_resume (t, i386_linux_prepare_to_resume);
1088 }
This page took 0.052183 seconds and 5 git commands to generate.