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