* linux-nat.c (linux_nat_new_thread): New variable.
[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
4 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 "inferior.h"
23 #include "gdbcore.h"
24 #include "regcache.h"
25 #include "target.h"
26 #include "linux-nat.h"
27
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include <sys/ptrace.h>
31 #include <sys/user.h>
32 #include <sys/procfs.h>
33
34 #ifdef HAVE_SYS_REG_H
35 #include <sys/reg.h>
36 #endif
37
38 #ifndef ORIG_EAX
39 #define ORIG_EAX -1
40 #endif
41
42 #ifdef HAVE_SYS_DEBUGREG_H
43 #include <sys/debugreg.h>
44 #endif
45
46 #ifndef DR_FIRSTADDR
47 #define DR_FIRSTADDR 0
48 #endif
49
50 #ifndef DR_LASTADDR
51 #define DR_LASTADDR 3
52 #endif
53
54 #ifndef DR_STATUS
55 #define DR_STATUS 6
56 #endif
57
58 #ifndef DR_CONTROL
59 #define DR_CONTROL 7
60 #endif
61
62 /* Prototypes for supply_gregset etc. */
63 #include "gregset.h"
64
65 #include "i387-tdep.h"
66 #include "i386-tdep.h"
67 #include "i386-linux-tdep.h"
68
69 /* Defines ps_err_e, struct ps_prochandle. */
70 #include "gdb_proc_service.h"
71 \f
72
73 /* The register sets used in GNU/Linux ELF core-dumps are identical to
74 the register sets in `struct user' that is used for a.out
75 core-dumps, and is also used by `ptrace'. The corresponding types
76 are `elf_gregset_t' for the general-purpose registers (with
77 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
78 for the floating-point registers.
79
80 Those types used to be available under the names `gregset_t' and
81 `fpregset_t' too, and this file used those names in the past. But
82 those names are now used for the register sets used in the
83 `mcontext_t' type, and have a different size and layout. */
84
85 /* Mapping between the general-purpose registers in `struct user'
86 format and GDB's register array layout. */
87 static int regmap[] =
88 {
89 EAX, ECX, EDX, EBX,
90 UESP, EBP, ESI, EDI,
91 EIP, EFL, CS, SS,
92 DS, ES, FS, GS,
93 -1, -1, -1, -1, /* st0, st1, st2, st3 */
94 -1, -1, -1, -1, /* st4, st5, st6, st7 */
95 -1, -1, -1, -1, /* fctrl, fstat, ftag, fiseg */
96 -1, -1, -1, -1, /* fioff, foseg, fooff, fop */
97 -1, -1, -1, -1, /* xmm0, xmm1, xmm2, xmm3 */
98 -1, -1, -1, -1, /* xmm4, xmm5, xmm6, xmm6 */
99 -1, /* mxcsr */
100 ORIG_EAX
101 };
102
103 /* Which ptrace request retrieves which registers?
104 These apply to the corresponding SET requests as well. */
105
106 #define GETREGS_SUPPLIES(regno) \
107 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
108
109 #define GETFPXREGS_SUPPLIES(regno) \
110 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
111
112 /* Does the current host support the GETREGS request? */
113 int have_ptrace_getregs =
114 #ifdef HAVE_PTRACE_GETREGS
115 1
116 #else
117 0
118 #endif
119 ;
120
121 /* Does the current host support the GETFPXREGS request? The header
122 file may or may not define it, and even if it is defined, the
123 kernel will return EIO if it's running on a pre-SSE processor.
124
125 My instinct is to attach this to some architecture- or
126 target-specific data structure, but really, a particular GDB
127 process can only run on top of one kernel at a time. So it's okay
128 for this to be a simple variable. */
129 int have_ptrace_getfpxregs =
130 #ifdef HAVE_PTRACE_GETFPXREGS
131 1
132 #else
133 0
134 #endif
135 ;
136 \f
137
138 /* Accessing registers through the U area, one at a time. */
139
140 /* Fetch one register. */
141
142 static void
143 fetch_register (struct regcache *regcache, int regno)
144 {
145 int tid;
146 int val;
147
148 gdb_assert (!have_ptrace_getregs);
149 if (regmap[regno] == -1)
150 {
151 regcache_raw_supply (regcache, regno, NULL);
152 return;
153 }
154
155 /* GNU/Linux LWP ID's are process ID's. */
156 tid = TIDGET (inferior_ptid);
157 if (tid == 0)
158 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
159
160 errno = 0;
161 val = ptrace (PTRACE_PEEKUSER, tid, 4 * regmap[regno], 0);
162 if (errno != 0)
163 error (_("Couldn't read register %s (#%d): %s."),
164 gdbarch_register_name (current_gdbarch, regno),
165 regno, safe_strerror (errno));
166
167 regcache_raw_supply (regcache, regno, &val);
168 }
169
170 /* Store one register. */
171
172 static void
173 store_register (const struct regcache *regcache, int regno)
174 {
175 int tid;
176 int val;
177
178 gdb_assert (!have_ptrace_getregs);
179 if (regmap[regno] == -1)
180 return;
181
182 /* GNU/Linux LWP ID's are process ID's. */
183 tid = TIDGET (inferior_ptid);
184 if (tid == 0)
185 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
186
187 errno = 0;
188 regcache_raw_collect (regcache, regno, &val);
189 ptrace (PTRACE_POKEUSER, tid, 4 * regmap[regno], val);
190 if (errno != 0)
191 error (_("Couldn't write register %s (#%d): %s."),
192 gdbarch_register_name (current_gdbarch, regno),
193 regno, safe_strerror (errno));
194 }
195 \f
196
197 /* Transfering the general-purpose registers between GDB, inferiors
198 and core files. */
199
200 /* Fill GDB's register array with the general-purpose register values
201 in *GREGSETP. */
202
203 void
204 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
205 {
206 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
207 int i;
208
209 for (i = 0; i < I386_NUM_GREGS; i++)
210 regcache_raw_supply (regcache, i, regp + regmap[i]);
211
212 if (I386_LINUX_ORIG_EAX_REGNUM < gdbarch_num_regs (current_gdbarch))
213 regcache_raw_supply (regcache, I386_LINUX_ORIG_EAX_REGNUM,
214 regp + ORIG_EAX);
215 }
216
217 /* Fill register REGNO (if it is a general-purpose register) in
218 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
219 do this for all registers. */
220
221 void
222 fill_gregset (const struct regcache *regcache,
223 elf_gregset_t *gregsetp, int regno)
224 {
225 elf_greg_t *regp = (elf_greg_t *) gregsetp;
226 int i;
227
228 for (i = 0; i < I386_NUM_GREGS; i++)
229 if (regno == -1 || regno == i)
230 regcache_raw_collect (regcache, i, regp + regmap[i]);
231
232 if ((regno == -1 || regno == I386_LINUX_ORIG_EAX_REGNUM)
233 && I386_LINUX_ORIG_EAX_REGNUM < gdbarch_num_regs (current_gdbarch))
234 regcache_raw_collect (regcache, I386_LINUX_ORIG_EAX_REGNUM,
235 regp + ORIG_EAX);
236 }
237
238 #ifdef HAVE_PTRACE_GETREGS
239
240 /* Fetch all general-purpose registers from process/thread TID and
241 store their values in GDB's register array. */
242
243 static void
244 fetch_regs (struct regcache *regcache, int tid)
245 {
246 elf_gregset_t regs;
247 elf_gregset_t *regs_p = &regs;
248
249 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
250 {
251 if (errno == EIO)
252 {
253 /* The kernel we're running on doesn't support the GETREGS
254 request. Reset `have_ptrace_getregs'. */
255 have_ptrace_getregs = 0;
256 return;
257 }
258
259 perror_with_name (_("Couldn't get registers"));
260 }
261
262 supply_gregset (regcache, (const elf_gregset_t *) regs_p);
263 }
264
265 /* Store all valid general-purpose registers in GDB's register array
266 into the process/thread specified by TID. */
267
268 static void
269 store_regs (const struct regcache *regcache, int tid, int regno)
270 {
271 elf_gregset_t regs;
272
273 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
274 perror_with_name (_("Couldn't get registers"));
275
276 fill_gregset (regcache, &regs, regno);
277
278 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
279 perror_with_name (_("Couldn't write registers"));
280 }
281
282 #else
283
284 static void fetch_regs (struct regcache *regcache, int tid) {}
285 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
286
287 #endif
288 \f
289
290 /* Transfering floating-point registers between GDB, inferiors and cores. */
291
292 /* Fill GDB's register array with the floating-point register values in
293 *FPREGSETP. */
294
295 void
296 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
297 {
298 i387_supply_fsave (regcache, -1, fpregsetp);
299 }
300
301 /* Fill register REGNO (if it is a floating-point register) in
302 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
303 do this for all registers. */
304
305 void
306 fill_fpregset (const struct regcache *regcache,
307 elf_fpregset_t *fpregsetp, int regno)
308 {
309 i387_collect_fsave (regcache, regno, fpregsetp);
310 }
311
312 #ifdef HAVE_PTRACE_GETREGS
313
314 /* Fetch all floating-point registers from process/thread TID and store
315 thier values in GDB's register array. */
316
317 static void
318 fetch_fpregs (struct regcache *regcache, int tid)
319 {
320 elf_fpregset_t fpregs;
321
322 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
323 perror_with_name (_("Couldn't get floating point status"));
324
325 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
326 }
327
328 /* Store all valid floating-point registers in GDB's register array
329 into the process/thread specified by TID. */
330
331 static void
332 store_fpregs (const struct regcache *regcache, int tid, int regno)
333 {
334 elf_fpregset_t fpregs;
335
336 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
337 perror_with_name (_("Couldn't get floating point status"));
338
339 fill_fpregset (regcache, &fpregs, regno);
340
341 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
342 perror_with_name (_("Couldn't write floating point status"));
343 }
344
345 #else
346
347 static void fetch_fpregs (struct regcache *regcache, int tid) {}
348 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
349
350 #endif
351 \f
352
353 /* Transfering floating-point and SSE registers to and from GDB. */
354
355 #ifdef HAVE_PTRACE_GETFPXREGS
356
357 /* Fill GDB's register array with the floating-point and SSE register
358 values in *FPXREGSETP. */
359
360 void
361 supply_fpxregset (struct regcache *regcache,
362 const elf_fpxregset_t *fpxregsetp)
363 {
364 i387_supply_fxsave (regcache, -1, fpxregsetp);
365 }
366
367 /* Fill register REGNO (if it is a floating-point or SSE register) in
368 *FPXREGSETP with the value in GDB's register array. If REGNO is
369 -1, do this for all registers. */
370
371 void
372 fill_fpxregset (const struct regcache *regcache,
373 elf_fpxregset_t *fpxregsetp, int regno)
374 {
375 i387_collect_fxsave (regcache, regno, fpxregsetp);
376 }
377
378 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
379 process/thread TID and store their values in GDB's register array.
380 Return non-zero if successful, zero otherwise. */
381
382 static int
383 fetch_fpxregs (struct regcache *regcache, int tid)
384 {
385 elf_fpxregset_t fpxregs;
386
387 if (! have_ptrace_getfpxregs)
388 return 0;
389
390 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
391 {
392 if (errno == EIO)
393 {
394 have_ptrace_getfpxregs = 0;
395 return 0;
396 }
397
398 perror_with_name (_("Couldn't read floating-point and SSE registers"));
399 }
400
401 supply_fpxregset (regcache, (const elf_fpxregset_t *) &fpxregs);
402 return 1;
403 }
404
405 /* Store all valid registers in GDB's register array covered by the
406 PTRACE_SETFPXREGS request into the process/thread specified by TID.
407 Return non-zero if successful, zero otherwise. */
408
409 static int
410 store_fpxregs (const struct regcache *regcache, int tid, int regno)
411 {
412 elf_fpxregset_t fpxregs;
413
414 if (! have_ptrace_getfpxregs)
415 return 0;
416
417 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
418 {
419 if (errno == EIO)
420 {
421 have_ptrace_getfpxregs = 0;
422 return 0;
423 }
424
425 perror_with_name (_("Couldn't read floating-point and SSE registers"));
426 }
427
428 fill_fpxregset (regcache, &fpxregs, regno);
429
430 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
431 perror_with_name (_("Couldn't write floating-point and SSE registers"));
432
433 return 1;
434 }
435
436 #else
437
438 static int fetch_fpxregs (struct regcache *regcache, int tid) { return 0; }
439 static int store_fpxregs (const struct regcache *regcache, int tid, int regno) { return 0; }
440
441 #endif /* HAVE_PTRACE_GETFPXREGS */
442 \f
443
444 /* Transferring arbitrary registers between GDB and inferior. */
445
446 /* Fetch register REGNO from the child process. If REGNO is -1, do
447 this for all registers (including the floating point and SSE
448 registers). */
449
450 static void
451 i386_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
452 {
453 int tid;
454
455 /* Use the old method of peeking around in `struct user' if the
456 GETREGS request isn't available. */
457 if (!have_ptrace_getregs)
458 {
459 int i;
460
461 for (i = 0; i < gdbarch_num_regs (current_gdbarch); i++)
462 if (regno == -1 || regno == i)
463 fetch_register (regcache, i);
464
465 return;
466 }
467
468 /* GNU/Linux LWP ID's are process ID's. */
469 tid = TIDGET (inferior_ptid);
470 if (tid == 0)
471 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
472
473 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
474 transfers more registers in one system call, and we'll cache the
475 results. But remember that fetch_fpxregs can fail, and return
476 zero. */
477 if (regno == -1)
478 {
479 fetch_regs (regcache, tid);
480
481 /* The call above might reset `have_ptrace_getregs'. */
482 if (!have_ptrace_getregs)
483 {
484 i386_linux_fetch_inferior_registers (regcache, regno);
485 return;
486 }
487
488 if (fetch_fpxregs (regcache, tid))
489 return;
490 fetch_fpregs (regcache, tid);
491 return;
492 }
493
494 if (GETREGS_SUPPLIES (regno))
495 {
496 fetch_regs (regcache, tid);
497 return;
498 }
499
500 if (GETFPXREGS_SUPPLIES (regno))
501 {
502 if (fetch_fpxregs (regcache, tid))
503 return;
504
505 /* Either our processor or our kernel doesn't support the SSE
506 registers, so read the FP registers in the traditional way,
507 and fill the SSE registers with dummy values. It would be
508 more graceful to handle differences in the register set using
509 gdbarch. Until then, this will at least make things work
510 plausibly. */
511 fetch_fpregs (regcache, tid);
512 return;
513 }
514
515 internal_error (__FILE__, __LINE__,
516 _("Got request for bad register number %d."), regno);
517 }
518
519 /* Store register REGNO back into the child process. If REGNO is -1,
520 do this for all registers (including the floating point and SSE
521 registers). */
522 static void
523 i386_linux_store_inferior_registers (struct regcache *regcache, int regno)
524 {
525 int tid;
526
527 /* Use the old method of poking around in `struct user' if the
528 SETREGS request isn't available. */
529 if (!have_ptrace_getregs)
530 {
531 int i;
532
533 for (i = 0; i < gdbarch_num_regs (current_gdbarch); i++)
534 if (regno == -1 || regno == i)
535 store_register (regcache, i);
536
537 return;
538 }
539
540 /* GNU/Linux LWP ID's are process ID's. */
541 tid = TIDGET (inferior_ptid);
542 if (tid == 0)
543 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
544
545 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
546 transfers more registers in one system call. But remember that
547 store_fpxregs can fail, and return zero. */
548 if (regno == -1)
549 {
550 store_regs (regcache, tid, regno);
551 if (store_fpxregs (regcache, tid, regno))
552 return;
553 store_fpregs (regcache, tid, regno);
554 return;
555 }
556
557 if (GETREGS_SUPPLIES (regno))
558 {
559 store_regs (regcache, tid, regno);
560 return;
561 }
562
563 if (GETFPXREGS_SUPPLIES (regno))
564 {
565 if (store_fpxregs (regcache, tid, regno))
566 return;
567
568 /* Either our processor or our kernel doesn't support the SSE
569 registers, so just write the FP registers in the traditional
570 way. */
571 store_fpregs (regcache, tid, regno);
572 return;
573 }
574
575 internal_error (__FILE__, __LINE__,
576 _("Got request to store bad register number %d."), regno);
577 }
578 \f
579
580 /* Support for debug registers. */
581
582 static unsigned long i386_linux_dr[DR_CONTROL + 1];
583
584 static unsigned long
585 i386_linux_dr_get (ptid_t ptid, int regnum)
586 {
587 int tid;
588 unsigned long value;
589
590 tid = TIDGET (ptid);
591 if (tid == 0)
592 tid = PIDGET (ptid);
593
594 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
595 ptrace call fails breaks debugging remote targets. The correct
596 way to fix this is to add the hardware breakpoint and watchpoint
597 stuff to the target vector. For now, just return zero if the
598 ptrace call fails. */
599 errno = 0;
600 value = ptrace (PTRACE_PEEKUSER, tid,
601 offsetof (struct user, u_debugreg[regnum]), 0);
602 if (errno != 0)
603 #if 0
604 perror_with_name (_("Couldn't read debug register"));
605 #else
606 return 0;
607 #endif
608
609 return value;
610 }
611
612 static void
613 i386_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
614 {
615 int tid;
616
617 tid = TIDGET (ptid);
618 if (tid == 0)
619 tid = PIDGET (ptid);
620
621 errno = 0;
622 ptrace (PTRACE_POKEUSER, tid,
623 offsetof (struct user, u_debugreg[regnum]), value);
624 if (errno != 0)
625 perror_with_name (_("Couldn't write debug register"));
626 }
627
628 void
629 i386_linux_dr_set_control (unsigned long control)
630 {
631 struct lwp_info *lp;
632 ptid_t ptid;
633
634 i386_linux_dr[DR_CONTROL] = control;
635 ALL_LWPS (lp, ptid)
636 i386_linux_dr_set (ptid, DR_CONTROL, control);
637 }
638
639 void
640 i386_linux_dr_set_addr (int regnum, CORE_ADDR addr)
641 {
642 struct lwp_info *lp;
643 ptid_t ptid;
644
645 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
646
647 i386_linux_dr[DR_FIRSTADDR + regnum] = addr;
648 ALL_LWPS (lp, ptid)
649 i386_linux_dr_set (ptid, DR_FIRSTADDR + regnum, addr);
650 }
651
652 void
653 i386_linux_dr_reset_addr (int regnum)
654 {
655 i386_linux_dr_set_addr (regnum, 0);
656 }
657
658 unsigned long
659 i386_linux_dr_get_status (void)
660 {
661 return i386_linux_dr_get (inferior_ptid, DR_STATUS);
662 }
663
664 static void
665 i386_linux_new_thread (ptid_t ptid)
666 {
667 int i;
668
669 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
670 i386_linux_dr_set (ptid, i, i386_linux_dr[i]);
671
672 i386_linux_dr_set (ptid, DR_CONTROL, i386_linux_dr[DR_CONTROL]);
673 }
674 \f
675
676 /* Called by libthread_db. Returns a pointer to the thread local
677 storage (or its descriptor). */
678
679 ps_err_e
680 ps_get_thread_area (const struct ps_prochandle *ph,
681 lwpid_t lwpid, int idx, void **base)
682 {
683 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
684 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
685 4 byte integers in size: `entry_number', `base_addr', `limit',
686 and a bunch of status bits.
687
688 The values returned by this ptrace call should be part of the
689 regcache buffer, and ps_get_thread_area should channel its
690 request through the regcache. That way remote targets could
691 provide the value using the remote protocol and not this direct
692 call.
693
694 Is this function needed? I'm guessing that the `base' is the
695 address of a a descriptor that libthread_db uses to find the
696 thread local address base that GDB needs. Perhaps that
697 descriptor is defined by the ABI. Anyway, given that
698 libthread_db calls this function without prompting (gdb
699 requesting tls base) I guess it needs info in there anyway. */
700 unsigned int desc[4];
701 gdb_assert (sizeof (int) == 4);
702
703 #ifndef PTRACE_GET_THREAD_AREA
704 #define PTRACE_GET_THREAD_AREA 25
705 #endif
706
707 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid,
708 (void *) idx, (unsigned long) &desc) < 0)
709 return PS_ERR;
710
711 *(int *)base = desc[1];
712 return PS_OK;
713 }
714 \f
715
716 /* The instruction for a GNU/Linux system call is:
717 int $0x80
718 or 0xcd 0x80. */
719
720 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
721
722 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
723
724 /* The system call number is stored in the %eax register. */
725 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
726
727 /* We are specifically interested in the sigreturn and rt_sigreturn
728 system calls. */
729
730 #ifndef SYS_sigreturn
731 #define SYS_sigreturn 0x77
732 #endif
733 #ifndef SYS_rt_sigreturn
734 #define SYS_rt_sigreturn 0xad
735 #endif
736
737 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
738 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
739
740 /* Resume execution of the inferior process.
741 If STEP is nonzero, single-step it.
742 If SIGNAL is nonzero, give it that signal. */
743
744 static void
745 i386_linux_resume (ptid_t ptid, int step, enum target_signal signal)
746 {
747 int pid = PIDGET (ptid);
748
749 int request = PTRACE_CONT;
750
751 if (step)
752 {
753 struct regcache *regcache = get_thread_regcache (pid_to_ptid (pid));
754 ULONGEST pc;
755 gdb_byte buf[LINUX_SYSCALL_LEN];
756
757 request = PTRACE_SINGLESTEP;
758
759 regcache_cooked_read_unsigned (regcache,
760 gdbarch_pc_regnum (current_gdbarch), &pc);
761
762 /* Returning from a signal trampoline is done by calling a
763 special system call (sigreturn or rt_sigreturn, see
764 i386-linux-tdep.c for more information). This system call
765 restores the registers that were saved when the signal was
766 raised, including %eflags. That means that single-stepping
767 won't work. Instead, we'll have to modify the signal context
768 that's about to be restored, and set the trace flag there. */
769
770 /* First check if PC is at a system call. */
771 if (read_memory_nobpt (pc, buf, LINUX_SYSCALL_LEN) == 0
772 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
773 {
774 ULONGEST syscall;
775 regcache_cooked_read_unsigned (regcache,
776 LINUX_SYSCALL_REGNUM, &syscall);
777
778 /* Then check the system call number. */
779 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
780 {
781 ULONGEST sp, addr;
782 unsigned long int eflags;
783
784 regcache_cooked_read_unsigned (regcache, I386_ESP_REGNUM, &sp);
785 if (syscall == SYS_rt_sigreturn)
786 addr = read_memory_integer (sp + 8, 4) + 20;
787 else
788 addr = sp;
789
790 /* Set the trace flag in the context that's about to be
791 restored. */
792 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
793 read_memory (addr, (gdb_byte *) &eflags, 4);
794 eflags |= 0x0100;
795 write_memory (addr, (gdb_byte *) &eflags, 4);
796 }
797 }
798 }
799
800 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
801 perror_with_name (("ptrace"));
802 }
803
804 static void (*super_post_startup_inferior) (ptid_t ptid);
805
806 static void
807 i386_linux_child_post_startup_inferior (ptid_t ptid)
808 {
809 i386_cleanup_dregs ();
810 super_post_startup_inferior (ptid);
811 }
812
813 void
814 _initialize_i386_linux_nat (void)
815 {
816 struct target_ops *t;
817
818 /* Fill in the generic GNU/Linux methods. */
819 t = linux_target ();
820
821 /* Override the default ptrace resume method. */
822 t->to_resume = i386_linux_resume;
823
824 /* Override the GNU/Linux inferior startup hook. */
825 super_post_startup_inferior = t->to_post_startup_inferior;
826 t->to_post_startup_inferior = i386_linux_child_post_startup_inferior;
827
828 /* Add our register access methods. */
829 t->to_fetch_registers = i386_linux_fetch_inferior_registers;
830 t->to_store_registers = i386_linux_store_inferior_registers;
831
832 /* Register the target. */
833 linux_nat_add_target (t);
834 linux_nat_set_new_thread (t, i386_linux_new_thread);
835 }
This page took 0.103727 seconds and 4 git commands to generate.