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