49149830e8495b56b01ac77a43d397699e874bf2
[deliverable/binutils-gdb.git] / gdb / i386-linux-nat.c
1 /* Native-dependent code for Linux/x86.
2 Copyright 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24
25 /* For i386_linux_skip_solib_resolver. */
26 #include "symtab.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29
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 /* Prototypes for supply_gregset etc. */
39 #include "gregset.h"
40
41 /* Prototypes for i387_supply_fsave etc. */
42 #include "i387-nat.h"
43
44 /* Prototypes for local functions. */
45 static void dummy_sse_values (void);
46
47 /* On Linux, threads are implemented as pseudo-processes, in which
48 case we may be tracing more than one process at a time. In that
49 case, inferior_pid will contain the main process ID and the
50 individual thread (process) ID mashed together. These macros are
51 used to separate them out. These definitions should be overridden
52 if thread support is included. */
53
54 #if !defined (PIDGET) /* Default definition for PIDGET/TIDGET. */
55 #define PIDGET(PID) PID
56 #define TIDGET(PID) 0
57 #endif
58 \f
59
60 /* The register sets used in Linux ELF core-dumps are identical to the
61 register sets in `struct user' that is used for a.out core-dumps,
62 and is also used by `ptrace'. The corresponding types are
63 `elf_gregset_t' for the general-purpose registers (with
64 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
65 for the floating-point registers.
66
67 Those types used to be available under the names `gregset_t' and
68 `fpregset_t' too, and this file used those names in the past. But
69 those names are now used for the register sets used in the
70 `mcontext_t' type, and have a different size and layout. */
71
72 /* Mapping between the general-purpose registers in `struct user'
73 format and GDB's register array layout. */
74 static int regmap[] =
75 {
76 EAX, ECX, EDX, EBX,
77 UESP, EBP, ESI, EDI,
78 EIP, EFL, CS, SS,
79 DS, ES, FS, GS
80 };
81
82 /* Which ptrace request retrieves which registers?
83 These apply to the corresponding SET requests as well. */
84 #define GETREGS_SUPPLIES(regno) \
85 (0 <= (regno) && (regno) <= 15)
86 #define GETFPREGS_SUPPLIES(regno) \
87 (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
88 #define GETFPXREGS_SUPPLIES(regno) \
89 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
90
91 /* Does the current host support the GETREGS request? */
92 int have_ptrace_getregs =
93 #ifdef HAVE_PTRACE_GETREGS
94 1
95 #else
96 0
97 #endif
98 ;
99
100 /* Does the current host support the GETFPXREGS request? The header
101 file may or may not define it, and even if it is defined, the
102 kernel will return EIO if it's running on a pre-SSE processor.
103
104 My instinct is to attach this to some architecture- or
105 target-specific data structure, but really, a particular GDB
106 process can only run on top of one kernel at a time. So it's okay
107 for this to be a simple variable. */
108 int have_ptrace_getfpxregs =
109 #ifdef HAVE_PTRACE_GETFPXREGS
110 1
111 #else
112 0
113 #endif
114 ;
115 \f
116
117 /* Fetching registers directly from the U area, one at a time. */
118
119 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
120 The problem is that we define FETCH_INFERIOR_REGISTERS since we
121 want to use our own versions of {fetch,store}_inferior_registers
122 that use the GETREGS request. This means that the code in
123 `infptrace.c' is #ifdef'd out. But we need to fall back on that
124 code when GDB is running on top of a kernel that doesn't support
125 the GETREGS request. I want to avoid changing `infptrace.c' right
126 now. */
127
128 #ifndef PT_READ_U
129 #define PT_READ_U PTRACE_PEEKUSR
130 #endif
131 #ifndef PT_WRITE_U
132 #define PT_WRITE_U PTRACE_POKEUSR
133 #endif
134
135 /* Default the type of the ptrace transfer to int. */
136 #ifndef PTRACE_XFER_TYPE
137 #define PTRACE_XFER_TYPE int
138 #endif
139
140 /* Registers we shouldn't try to fetch. */
141 #if !defined (CANNOT_FETCH_REGISTER)
142 #define CANNOT_FETCH_REGISTER(regno) 0
143 #endif
144
145 /* Fetch one register. */
146
147 static void
148 fetch_register (int regno)
149 {
150 /* This isn't really an address. But ptrace thinks of it as one. */
151 CORE_ADDR regaddr;
152 char mess[128]; /* For messages */
153 register int i;
154 unsigned int offset; /* Offset of registers within the u area. */
155 char buf[MAX_REGISTER_RAW_SIZE];
156 int tid;
157
158 if (CANNOT_FETCH_REGISTER (regno))
159 {
160 memset (buf, '\0', REGISTER_RAW_SIZE (regno)); /* Supply zeroes */
161 supply_register (regno, buf);
162 return;
163 }
164
165 /* Overload thread id onto process id */
166 if ((tid = TIDGET (inferior_pid)) == 0)
167 tid = inferior_pid; /* no thread id, just use process id */
168
169 offset = U_REGS_OFFSET;
170
171 regaddr = register_addr (regno, offset);
172 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
173 {
174 errno = 0;
175 *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
176 (PTRACE_ARG3_TYPE) regaddr, 0);
177 regaddr += sizeof (PTRACE_XFER_TYPE);
178 if (errno != 0)
179 {
180 sprintf (mess, "reading register %s (#%d)",
181 REGISTER_NAME (regno), regno);
182 perror_with_name (mess);
183 }
184 }
185 supply_register (regno, buf);
186 }
187
188 /* Fetch register values from the inferior.
189 If REGNO is negative, do this for all registers.
190 Otherwise, REGNO specifies which register (so we can save time). */
191
192 void
193 old_fetch_inferior_registers (int regno)
194 {
195 if (regno >= 0)
196 {
197 fetch_register (regno);
198 }
199 else
200 {
201 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
202 {
203 fetch_register (regno);
204 }
205 }
206 }
207
208 /* Registers we shouldn't try to store. */
209 #if !defined (CANNOT_STORE_REGISTER)
210 #define CANNOT_STORE_REGISTER(regno) 0
211 #endif
212
213 /* Store one register. */
214
215 static void
216 store_register (int regno)
217 {
218 /* This isn't really an address. But ptrace thinks of it as one. */
219 CORE_ADDR regaddr;
220 char mess[128]; /* For messages */
221 register int i;
222 unsigned int offset; /* Offset of registers within the u area. */
223 int tid;
224
225 if (CANNOT_STORE_REGISTER (regno))
226 {
227 return;
228 }
229
230 /* Overload thread id onto process id */
231 if ((tid = TIDGET (inferior_pid)) == 0)
232 tid = inferior_pid; /* no thread id, just use process id */
233
234 offset = U_REGS_OFFSET;
235
236 regaddr = register_addr (regno, offset);
237 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
238 {
239 errno = 0;
240 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
241 *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
242 regaddr += sizeof (PTRACE_XFER_TYPE);
243 if (errno != 0)
244 {
245 sprintf (mess, "writing register %s (#%d)",
246 REGISTER_NAME (regno), regno);
247 perror_with_name (mess);
248 }
249 }
250 }
251
252 /* Store our register values back into the inferior.
253 If REGNO is negative, do this for all registers.
254 Otherwise, REGNO specifies which register (so we can save time). */
255
256 void
257 old_store_inferior_registers (int regno)
258 {
259 if (regno >= 0)
260 {
261 store_register (regno);
262 }
263 else
264 {
265 for (regno = 0; regno < ARCH_NUM_REGS; regno++)
266 {
267 store_register (regno);
268 }
269 }
270 }
271 \f
272
273 /* Transfering the general-purpose registers between GDB, inferiors
274 and core files. */
275
276 /* Fill GDB's register array with the genereal-purpose register values
277 in *GREGSETP. */
278
279 void
280 supply_gregset (elf_gregset_t *gregsetp)
281 {
282 elf_greg_t *regp = (elf_greg_t *) gregsetp;
283 int i;
284
285 for (i = 0; i < NUM_GREGS; i++)
286 supply_register (i, (char *) (regp + regmap[i]));
287 }
288
289 /* Fill register REGNO (if it is a general-purpose register) in
290 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
291 do this for all registers. */
292
293 void
294 fill_gregset (elf_gregset_t *gregsetp, int regno)
295 {
296 elf_greg_t *regp = (elf_greg_t *) gregsetp;
297 int i;
298
299 for (i = 0; i < NUM_GREGS; i++)
300 if ((regno == -1 || regno == i))
301 *(regp + regmap[i]) = *(elf_greg_t *) &registers[REGISTER_BYTE (i)];
302 }
303
304 #ifdef HAVE_PTRACE_GETREGS
305
306 /* Fetch all general-purpose registers from process/thread TID and
307 store their values in GDB's register array. */
308
309 static void
310 fetch_regs (int tid)
311 {
312 elf_gregset_t regs;
313
314 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
315 {
316 if (errno == EIO)
317 {
318 /* The kernel we're running on doesn't support the GETREGS
319 request. Reset `have_ptrace_getregs'. */
320 have_ptrace_getregs = 0;
321 return;
322 }
323
324 perror_with_name ("Couldn't get registers");
325 }
326
327 supply_gregset (&regs);
328 }
329
330 /* Store all valid general-purpose registers in GDB's register array
331 into the process/thread specified by TID. */
332
333 static void
334 store_regs (int tid, int regno)
335 {
336 elf_gregset_t regs;
337
338 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
339 perror_with_name ("Couldn't get registers");
340
341 fill_gregset (&regs, regno);
342
343 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
344 perror_with_name ("Couldn't write registers");
345 }
346
347 #else
348
349 static void fetch_regs (int tid) {}
350 static void store_regs (int tid, int regno) {}
351
352 #endif
353 \f
354
355 /* Transfering floating-point registers between GDB, inferiors and cores. */
356
357 /* Fill GDB's register array with the floating-point register values in
358 *FPREGSETP. */
359
360 void
361 supply_fpregset (elf_fpregset_t *fpregsetp)
362 {
363 i387_supply_fsave ((char *) fpregsetp);
364 dummy_sse_values ();
365 }
366
367 /* Fill register REGNO (if it is a floating-point register) in
368 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
369 do this for all registers. */
370
371 void
372 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
373 {
374 i387_fill_fsave ((char *) fpregsetp, regno);
375 }
376
377 #ifdef HAVE_PTRACE_GETREGS
378
379 /* Fetch all floating-point registers from process/thread TID and store
380 thier values in GDB's register array. */
381
382 static void
383 fetch_fpregs (int tid)
384 {
385 elf_fpregset_t fpregs;
386
387 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
388 perror_with_name ("Couldn't get floating point status");
389
390 supply_fpregset (&fpregs);
391 }
392
393 /* Store all valid floating-point registers in GDB's register array
394 into the process/thread specified by TID. */
395
396 static void
397 store_fpregs (int tid, int regno)
398 {
399 elf_fpregset_t fpregs;
400
401 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
402 perror_with_name ("Couldn't get floating point status");
403
404 fill_fpregset (&fpregs, regno);
405
406 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
407 perror_with_name ("Couldn't write floating point status");
408 }
409
410 #else
411
412 static void fetch_fpregs (int tid) {}
413 static void store_fpregs (int tid, int regno) {}
414
415 #endif
416 \f
417
418 /* Transfering floating-point and SSE registers to and from GDB. */
419
420 #ifdef HAVE_PTRACE_GETFPXREGS
421
422 /* Fill GDB's register array with the floating-point and SSE register
423 values in *FPXREGSETP. */
424
425 static void
426 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
427 {
428 i387_supply_fxsave ((char *) fpxregsetp);
429 }
430
431 /* Fill register REGNO (if it is a floating-point or SSE register) in
432 *FPXREGSETP with the value in GDB's register array. If REGNO is
433 -1, do this for all registers. */
434
435 static void
436 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
437 {
438 i387_fill_fxsave ((char *) fpxregsetp, regno);
439 }
440
441 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
442 process/thread TID and store their values in GDB's register array.
443 Return non-zero if successful, zero otherwise. */
444
445 static int
446 fetch_fpxregs (int tid)
447 {
448 elf_fpxregset_t fpxregs;
449
450 if (! have_ptrace_getfpxregs)
451 return 0;
452
453 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
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 supply_fpxregset (&fpxregs);
465 return 1;
466 }
467
468 /* Store all valid registers in GDB's register array covered by the
469 PTRACE_SETFPXREGS request into the process/thread specified by TID.
470 Return non-zero if successful, zero otherwise. */
471
472 static int
473 store_fpxregs (int tid, int regno)
474 {
475 elf_fpxregset_t fpxregs;
476
477 if (! have_ptrace_getfpxregs)
478 return 0;
479
480 if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
481 perror_with_name ("Couldn't read floating-point and SSE registers");
482
483 fill_fpxregset (&fpxregs, regno);
484
485 if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
486 perror_with_name ("Couldn't write floating-point and SSE registers");
487
488 return 1;
489 }
490
491 /* Fill the XMM registers in the register array with dummy values. For
492 cases where we don't have access to the XMM registers. I think
493 this is cleaner than printing a warning. For a cleaner solution,
494 we should gdbarchify the i386 family. */
495
496 static void
497 dummy_sse_values (void)
498 {
499 /* C doesn't have a syntax for NaN's, so write it out as an array of
500 longs. */
501 static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
502 static long mxcsr = 0x1f80;
503 int reg;
504
505 for (reg = 0; reg < 8; reg++)
506 supply_register (XMM0_REGNUM + reg, (char *) dummy);
507 supply_register (MXCSR_REGNUM, (char *) &mxcsr);
508 }
509
510 #else
511
512 static int fetch_fpxregs (int tid) { return 0; }
513 static int store_fpxregs (int tid, int regno) { return 0; }
514 static void dummy_sse_values (void) {}
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 void
526 fetch_inferior_registers (int regno)
527 {
528 int tid;
529
530 /* Use the old method of peeking around in `struct user' if the
531 GETREGS request isn't available. */
532 if (! have_ptrace_getregs)
533 {
534 old_fetch_inferior_registers (regno);
535 return;
536 }
537
538 /* Linux LWP ID's are process ID's. */
539 if ((tid = TIDGET (inferior_pid)) == 0)
540 tid = inferior_pid; /* Not a threaded program. */
541
542 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
543 transfers more registers in one system call, and we'll cache the
544 results. But remember that fetch_fpxregs can fail, and return
545 zero. */
546 if (regno == -1)
547 {
548 fetch_regs (tid);
549
550 /* The call above might reset `have_ptrace_getregs'. */
551 if (! have_ptrace_getregs)
552 {
553 old_fetch_inferior_registers (-1);
554 return;
555 }
556
557 if (fetch_fpxregs (tid))
558 return;
559 fetch_fpregs (tid);
560 return;
561 }
562
563 if (GETREGS_SUPPLIES (regno))
564 {
565 fetch_regs (tid);
566 return;
567 }
568
569 if (GETFPXREGS_SUPPLIES (regno))
570 {
571 if (fetch_fpxregs (tid))
572 return;
573
574 /* Either our processor or our kernel doesn't support the SSE
575 registers, so read the FP registers in the traditional way,
576 and fill the SSE registers with dummy values. It would be
577 more graceful to handle differences in the register set using
578 gdbarch. Until then, this will at least make things work
579 plausibly. */
580 fetch_fpregs (tid);
581 return;
582 }
583
584 internal_error ("Got request for bad register number %d.", regno);
585 }
586
587 /* Store register REGNO back into the child process. If REGNO is -1,
588 do this for all registers (including the floating point and SSE
589 registers). */
590 void
591 store_inferior_registers (int regno)
592 {
593 int tid;
594
595 /* Use the old method of poking around in `struct user' if the
596 SETREGS request isn't available. */
597 if (! have_ptrace_getregs)
598 {
599 old_store_inferior_registers (regno);
600 return;
601 }
602
603 /* Linux LWP ID's are process ID's. */
604 if ((tid = TIDGET (inferior_pid)) == 0)
605 tid = inferior_pid; /* Not a threaded program. */
606
607 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
608 transfers more registers in one system call. But remember that
609 store_fpxregs can fail, and return zero. */
610 if (regno == -1)
611 {
612 store_regs (tid, regno);
613 if (store_fpxregs (tid, regno))
614 return;
615 store_fpregs (tid, regno);
616 return;
617 }
618
619 if (GETREGS_SUPPLIES (regno))
620 {
621 store_regs (tid, regno);
622 return;
623 }
624
625 if (GETFPXREGS_SUPPLIES (regno))
626 {
627 if (store_fpxregs (tid, regno))
628 return;
629
630 /* Either our processor or our kernel doesn't support the SSE
631 registers, so just write the FP registers in the traditional
632 way. */
633 store_fpregs (tid, regno);
634 return;
635 }
636
637 internal_error ("Got request to store bad register number %d.", regno);
638 }
639 \f
640
641 /* Interpreting register set info found in core files. */
642
643 /* Provide registers to GDB from a core file.
644
645 (We can't use the generic version of this function in
646 core-regset.c, because Linux has *three* different kinds of
647 register set notes. core-regset.c would have to call
648 supply_fpxregset, which most platforms don't have.)
649
650 CORE_REG_SECT points to an array of bytes, which are the contents
651 of a `note' from a core file which BFD thinks might contain
652 register contents. CORE_REG_SIZE is its size.
653
654 WHICH says which register set corelow suspects this is:
655 0 --- the general-purpose register set, in elf_gregset_t format
656 2 --- the floating-point register set, in elf_fpregset_t format
657 3 --- the extended floating-point register set, in elf_fpxregset_t format
658
659 REG_ADDR isn't used on Linux. */
660
661 static void
662 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
663 int which, CORE_ADDR reg_addr)
664 {
665 elf_gregset_t gregset;
666 elf_fpregset_t fpregset;
667
668 switch (which)
669 {
670 case 0:
671 if (core_reg_size != sizeof (gregset))
672 warning ("Wrong size gregset in core file.");
673 else
674 {
675 memcpy (&gregset, core_reg_sect, sizeof (gregset));
676 supply_gregset (&gregset);
677 }
678 break;
679
680 case 2:
681 if (core_reg_size != sizeof (fpregset))
682 warning ("Wrong size fpregset in core file.");
683 else
684 {
685 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
686 supply_fpregset (&fpregset);
687 }
688 break;
689
690 #ifdef HAVE_PTRACE_GETFPXREGS
691 {
692 elf_fpxregset_t fpxregset;
693
694 case 3:
695 if (core_reg_size != sizeof (fpxregset))
696 warning ("Wrong size fpxregset in core file.");
697 else
698 {
699 memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
700 supply_fpxregset (&fpxregset);
701 }
702 break;
703 }
704 #endif
705
706 default:
707 /* We've covered all the kinds of registers we know about here,
708 so this must be something we wouldn't know what to do with
709 anyway. Just ignore it. */
710 break;
711 }
712 }
713 \f
714
715 /* The instruction for a Linux system call is:
716 int $0x80
717 or 0xcd 0x80. */
718
719 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
720
721 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
722
723 /* The system call number is stored in the %eax register. */
724 #define LINUX_SYSCALL_REGNUM 0 /* %eax */
725
726 /* We are specifically interested in the sigreturn and rt_sigreturn
727 system calls. */
728
729 #ifndef SYS_sigreturn
730 #define SYS_sigreturn 0x77
731 #endif
732 #ifndef SYS_rt_sigreturn
733 #define SYS_rt_sigreturn 0xad
734 #endif
735
736 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
737 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
738
739 /* Resume execution of the inferior process.
740 If STEP is nonzero, single-step it.
741 If SIGNAL is nonzero, give it that signal. */
742
743 void
744 child_resume (int pid, int step, enum target_signal signal)
745 {
746 int request = PTRACE_CONT;
747
748 if (pid == -1)
749 /* Resume all threads. */
750 /* I think this only gets used in the non-threaded case, where "resume
751 all threads" and "resume inferior_pid" are the same. */
752 pid = inferior_pid;
753
754 if (step)
755 {
756 CORE_ADDR pc = read_pc_pid (pid);
757 unsigned char buf[LINUX_SYSCALL_LEN];
758
759 request = PTRACE_SINGLESTEP;
760
761 /* Returning from a signal trampoline is done by calling a
762 special system call (sigreturn or rt_sigreturn, see
763 i386-linux-tdep.c for more information). This system call
764 restores the registers that were saved when the signal was
765 raised, including %eflags. That means that single-stepping
766 won't work. Instead, we'll have to modify the signal context
767 that's about to be restored, and set the trace flag there. */
768
769 /* First check if PC is at a system call. */
770 if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
771 && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
772 {
773 int syscall = read_register_pid (LINUX_SYSCALL_REGNUM, pid);
774
775 /* Then check the system call number. */
776 if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
777 {
778 CORE_ADDR sp = read_register (SP_REGNUM);
779 CORE_ADDR addr = sp;
780 unsigned long int eflags;
781
782 if (syscall == SYS_rt_sigreturn)
783 addr = read_memory_integer (sp + 8, 4) + 20;
784
785 /* Set the trace flag in the context that's about to be
786 restored. */
787 addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
788 read_memory (addr, (char *) &eflags, 4);
789 eflags |= 0x0100;
790 write_memory (addr, (char *) &eflags, 4);
791 }
792 }
793 }
794
795 if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
796 perror_with_name ("ptrace");
797 }
798 \f
799
800 /* Calling functions in shared libraries. */
801 /* FIXME: kettenis/2000-03-05: Doesn't this belong in a
802 target-dependent file? The function
803 `i386_linux_skip_solib_resolver' is mentioned in
804 `config/i386/tm-linux.h'. */
805
806 /* Find the minimal symbol named NAME, and return both the minsym
807 struct and its objfile. This probably ought to be in minsym.c, but
808 everything there is trying to deal with things like C++ and
809 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
810 be considered too special-purpose for general consumption. */
811
812 static struct minimal_symbol *
813 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
814 {
815 struct objfile *objfile;
816
817 ALL_OBJFILES (objfile)
818 {
819 struct minimal_symbol *msym;
820
821 ALL_OBJFILE_MSYMBOLS (objfile, msym)
822 {
823 if (SYMBOL_NAME (msym)
824 && STREQ (SYMBOL_NAME (msym), name))
825 {
826 *objfile_p = objfile;
827 return msym;
828 }
829 }
830 }
831
832 return 0;
833 }
834
835 static CORE_ADDR
836 skip_hurd_resolver (CORE_ADDR pc)
837 {
838 /* The HURD dynamic linker is part of the GNU C library, so many
839 GNU/Linux distributions use it. (All ELF versions, as far as I
840 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
841 which calls "fixup" to patch the PLT, and then passes control to
842 the function.
843
844 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
845 the same objfile. If we are at the entry point of `fixup', then
846 we set a breakpoint at the return address (at the top of the
847 stack), and continue.
848
849 It's kind of gross to do all these checks every time we're
850 called, since they don't change once the executable has gotten
851 started. But this is only a temporary hack --- upcoming versions
852 of Linux will provide a portable, efficient interface for
853 debugging programs that use shared libraries. */
854
855 struct objfile *objfile;
856 struct minimal_symbol *resolver
857 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
858
859 if (resolver)
860 {
861 struct minimal_symbol *fixup
862 = lookup_minimal_symbol ("fixup", 0, objfile);
863
864 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
865 return (SAVED_PC_AFTER_CALL (get_current_frame ()));
866 }
867
868 return 0;
869 }
870
871 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
872 This function:
873 1) decides whether a PLT has sent us into the linker to resolve
874 a function reference, and
875 2) if so, tells us where to set a temporary breakpoint that will
876 trigger when the dynamic linker is done. */
877
878 CORE_ADDR
879 i386_linux_skip_solib_resolver (CORE_ADDR pc)
880 {
881 CORE_ADDR result;
882
883 /* Plug in functions for other kinds of resolvers here. */
884 result = skip_hurd_resolver (pc);
885 if (result)
886 return result;
887
888 return 0;
889 }
890 \f
891
892 /* Register that we are able to handle Linux ELF core file formats. */
893
894 static struct core_fns linux_elf_core_fns =
895 {
896 bfd_target_elf_flavour, /* core_flavour */
897 default_check_format, /* check_format */
898 default_core_sniffer, /* core_sniffer */
899 fetch_core_registers, /* core_read_registers */
900 NULL /* next */
901 };
902
903 void
904 _initialize_i386_linux_nat (void)
905 {
906 add_core_fns (&linux_elf_core_fns);
907 }
This page took 0.04978 seconds and 4 git commands to generate.