1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "linux-low.h"
25 #include <sys/param.h>
27 #include <sys/ptrace.h>
30 #include <sys/ioctl.h>
36 #include <sys/syscall.h>
38 #ifndef PTRACE_GETSIGINFO
39 # define PTRACE_GETSIGINFO 0x4202
40 # define PTRACE_SETSIGINFO 0x4203
47 /* If the system headers did not provide the constants, hard-code the normal
49 #ifndef PTRACE_EVENT_FORK
51 #define PTRACE_SETOPTIONS 0x4200
52 #define PTRACE_GETEVENTMSG 0x4201
54 /* options set using PTRACE_SETOPTIONS */
55 #define PTRACE_O_TRACESYSGOOD 0x00000001
56 #define PTRACE_O_TRACEFORK 0x00000002
57 #define PTRACE_O_TRACEVFORK 0x00000004
58 #define PTRACE_O_TRACECLONE 0x00000008
59 #define PTRACE_O_TRACEEXEC 0x00000010
60 #define PTRACE_O_TRACEVFORKDONE 0x00000020
61 #define PTRACE_O_TRACEEXIT 0x00000040
63 /* Wait extended result codes for the above trace options. */
64 #define PTRACE_EVENT_FORK 1
65 #define PTRACE_EVENT_VFORK 2
66 #define PTRACE_EVENT_CLONE 3
67 #define PTRACE_EVENT_EXEC 4
68 #define PTRACE_EVENT_VFORK_DONE 5
69 #define PTRACE_EVENT_EXIT 6
71 #endif /* PTRACE_EVENT_FORK */
73 /* We can't always assume that this flag is available, but all systems
74 with the ptrace event handlers also have __WALL, so it's safe to use
77 #define __WALL 0x40000000 /* Wait for any child. */
81 #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
86 /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
87 representation of the thread ID.
89 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
90 the same as the LWP ID. */
92 struct inferior_list all_processes
;
94 /* A list of all unknown processes which receive stop signals. Some other
95 process will presumably claim each of these as forked children
98 struct inferior_list stopped_pids
;
100 /* FIXME this is a bit of a hack, and could be removed. */
101 int stopping_threads
;
103 /* FIXME make into a target method? */
104 int using_threads
= 1;
105 static int thread_db_active
;
107 static int must_set_ptrace_flags
;
109 static void linux_resume_one_process (struct inferior_list_entry
*entry
,
110 int step
, int signal
, siginfo_t
*info
);
111 static void linux_resume (struct thread_resume
*resume_info
);
112 static void stop_all_processes (void);
113 static int linux_wait_for_event (struct thread_info
*child
);
114 static int check_removed_breakpoint (struct process_info
*event_child
);
115 static void *add_process (unsigned long pid
);
117 struct pending_signals
121 struct pending_signals
*prev
;
124 #define PTRACE_ARG3_TYPE long
125 #define PTRACE_XFER_TYPE long
127 #ifdef HAVE_LINUX_REGSETS
128 static int use_regsets_p
= 1;
131 #define pid_of(proc) ((proc)->head.id)
133 /* FIXME: Delete eventually. */
134 #define inferior_pid (pid_of (get_thread_process (current_inferior)))
137 handle_extended_wait (struct process_info
*event_child
, int wstat
)
139 int event
= wstat
>> 16;
140 struct process_info
*new_process
;
142 if (event
== PTRACE_EVENT_CLONE
)
144 unsigned long new_pid
;
147 ptrace (PTRACE_GETEVENTMSG
, inferior_pid
, 0, &new_pid
);
149 /* If we haven't already seen the new PID stop, wait for it now. */
150 if (! pull_pid_from_list (&stopped_pids
, new_pid
))
152 /* The new child has a pending SIGSTOP. We can't affect it until it
153 hits the SIGSTOP, but we're already attached. */
156 ret
= waitpid (new_pid
, &status
, __WALL
);
157 } while (ret
== -1 && errno
== EINTR
);
160 perror_with_name ("waiting for new child");
161 else if (ret
!= new_pid
)
162 warning ("wait returned unexpected PID %d", ret
);
163 else if (!WIFSTOPPED (status
) || WSTOPSIG (status
) != SIGSTOP
)
164 warning ("wait returned unexpected status 0x%x", status
);
167 ptrace (PTRACE_SETOPTIONS
, new_pid
, 0, PTRACE_O_TRACECLONE
);
169 new_process
= (struct process_info
*) add_process (new_pid
);
170 add_thread (new_pid
, new_process
, new_pid
);
171 new_thread_notify (thread_id_to_gdb_id (new_process
->lwpid
));
173 if (stopping_threads
)
174 new_process
->stopped
= 1;
176 ptrace (PTRACE_CONT
, new_pid
, 0, 0);
178 /* Always resume the current thread. If we are stopping
179 threads, it will have a pending SIGSTOP; we may as well
181 linux_resume_one_process (&event_child
->head
,
182 event_child
->stepping
, 0, NULL
);
186 /* This function should only be called if the process got a SIGTRAP.
187 The SIGTRAP could mean several things.
189 On i386, where decr_pc_after_break is non-zero:
190 If we were single-stepping this process using PTRACE_SINGLESTEP,
191 we will get only the one SIGTRAP (even if the instruction we
192 stepped over was a breakpoint). The value of $eip will be the
194 If we continue the process using PTRACE_CONT, we will get a
195 SIGTRAP when we hit a breakpoint. The value of $eip will be
196 the instruction after the breakpoint (i.e. needs to be
197 decremented). If we report the SIGTRAP to GDB, we must also
198 report the undecremented PC. If we cancel the SIGTRAP, we
199 must resume at the decremented PC.
201 (Presumably, not yet tested) On a non-decr_pc_after_break machine
202 with hardware or kernel single-step:
203 If we single-step over a breakpoint instruction, our PC will
204 point at the following instruction. If we continue and hit a
205 breakpoint instruction, our PC will point at the breakpoint
211 CORE_ADDR stop_pc
= (*the_low_target
.get_pc
) ();
213 if (get_thread_process (current_inferior
)->stepping
)
216 return stop_pc
- the_low_target
.decr_pc_after_break
;
220 add_process (unsigned long pid
)
222 struct process_info
*process
;
224 process
= (struct process_info
*) malloc (sizeof (*process
));
225 memset (process
, 0, sizeof (*process
));
227 process
->head
.id
= pid
;
228 process
->lwpid
= pid
;
230 add_inferior_to_list (&all_processes
, &process
->head
);
235 /* Start an inferior process and returns its pid.
236 ALLARGS is a vector of program-name and args. */
239 linux_create_inferior (char *program
, char **allargs
)
244 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
250 perror_with_name ("fork");
254 ptrace (PTRACE_TRACEME
, 0, 0, 0);
256 signal (__SIGRTMIN
+ 1, SIG_DFL
);
260 execv (program
, allargs
);
262 execvp (program
, allargs
);
264 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
270 new_process
= add_process (pid
);
271 add_thread (pid
, new_process
, pid
);
272 must_set_ptrace_flags
= 1;
277 /* Attach to an inferior process. */
280 linux_attach_lwp (unsigned long pid
)
282 struct process_info
*new_process
;
284 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
286 fprintf (stderr
, "Cannot attach to process %ld: %s (%d)\n", pid
,
287 strerror (errno
), errno
);
290 /* If we fail to attach to an LWP, just return. */
291 if (all_threads
.head
== NULL
)
296 ptrace (PTRACE_SETOPTIONS
, pid
, 0, PTRACE_O_TRACECLONE
);
298 new_process
= (struct process_info
*) add_process (pid
);
299 add_thread (pid
, new_process
, pid
);
300 new_thread_notify (thread_id_to_gdb_id (new_process
->lwpid
));
302 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
303 brings it to a halt. We should ignore that SIGSTOP and resume the process
304 (unless this is the first process, in which case the flag will be cleared
307 On the other hand, if we are currently trying to stop all threads, we
308 should treat the new thread as if we had sent it a SIGSTOP. This works
309 because we are guaranteed that add_process added us to the end of the
310 list, and so the new thread has not yet reached wait_for_sigstop (but
312 if (! stopping_threads
)
313 new_process
->stop_expected
= 1;
317 linux_attach (unsigned long pid
)
319 struct process_info
*process
;
321 linux_attach_lwp (pid
);
323 /* Don't ignore the initial SIGSTOP if we just attached to this process.
324 It will be collected by wait shortly. */
325 process
= (struct process_info
*) find_inferior_id (&all_processes
, pid
);
326 process
->stop_expected
= 0;
331 /* Kill the inferior process. Make us have no inferior. */
334 linux_kill_one_process (struct inferior_list_entry
*entry
)
336 struct thread_info
*thread
= (struct thread_info
*) entry
;
337 struct process_info
*process
= get_thread_process (thread
);
340 /* We avoid killing the first thread here, because of a Linux kernel (at
341 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
342 the children get a chance to be reaped, it will remain a zombie
344 if (entry
== all_threads
.head
)
349 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
351 /* Make sure it died. The loop is most likely unnecessary. */
352 wstat
= linux_wait_for_event (thread
);
353 } while (WIFSTOPPED (wstat
));
359 struct thread_info
*thread
= (struct thread_info
*) all_threads
.head
;
360 struct process_info
*process
;
366 for_each_inferior (&all_threads
, linux_kill_one_process
);
368 /* See the comment in linux_kill_one_process. We did not kill the first
369 thread in the list, so do so now. */
370 process
= get_thread_process (thread
);
373 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
375 /* Make sure it died. The loop is most likely unnecessary. */
376 wstat
= linux_wait_for_event (thread
);
377 } while (WIFSTOPPED (wstat
));
381 linux_detach_one_process (struct inferior_list_entry
*entry
)
383 struct thread_info
*thread
= (struct thread_info
*) entry
;
384 struct process_info
*process
= get_thread_process (thread
);
386 /* Make sure the process isn't stopped at a breakpoint that's
388 check_removed_breakpoint (process
);
390 /* If this process is stopped but is expecting a SIGSTOP, then make
391 sure we take care of that now. This isn't absolutely guaranteed
392 to collect the SIGSTOP, but is fairly likely to. */
393 if (process
->stop_expected
)
395 /* Clear stop_expected, so that the SIGSTOP will be reported. */
396 process
->stop_expected
= 0;
397 if (process
->stopped
)
398 linux_resume_one_process (&process
->head
, 0, 0, NULL
);
399 linux_wait_for_event (thread
);
402 /* Flush any pending changes to the process's registers. */
403 regcache_invalidate_one ((struct inferior_list_entry
*)
404 get_process_thread (process
));
406 /* Finally, let it resume. */
407 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
413 delete_all_breakpoints ();
414 for_each_inferior (&all_threads
, linux_detach_one_process
);
422 extern unsigned long signal_pid
;
426 ret
= waitpid (signal_pid
, &status
, 0);
427 if (WIFEXITED (status
) || WIFSIGNALED (status
))
429 } while (ret
!= -1 || errno
!= ECHILD
);
432 /* Return nonzero if the given thread is still alive. */
434 linux_thread_alive (unsigned long lwpid
)
436 if (find_inferior_id (&all_threads
, lwpid
) != NULL
)
442 /* Return nonzero if this process stopped at a breakpoint which
443 no longer appears to be inserted. Also adjust the PC
444 appropriately to resume where the breakpoint used to be. */
446 check_removed_breakpoint (struct process_info
*event_child
)
449 struct thread_info
*saved_inferior
;
451 if (event_child
->pending_is_breakpoint
== 0)
455 fprintf (stderr
, "Checking for breakpoint in process %ld.\n",
458 saved_inferior
= current_inferior
;
459 current_inferior
= get_process_thread (event_child
);
461 stop_pc
= get_stop_pc ();
463 /* If the PC has changed since we stopped, then we shouldn't do
464 anything. This happens if, for instance, GDB handled the
465 decr_pc_after_break subtraction itself. */
466 if (stop_pc
!= event_child
->pending_stop_pc
)
469 fprintf (stderr
, "Ignoring, PC was changed. Old PC was 0x%08llx\n",
470 event_child
->pending_stop_pc
);
472 event_child
->pending_is_breakpoint
= 0;
473 current_inferior
= saved_inferior
;
477 /* If the breakpoint is still there, we will report hitting it. */
478 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
481 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
482 current_inferior
= saved_inferior
;
487 fprintf (stderr
, "Removed breakpoint.\n");
489 /* For decr_pc_after_break targets, here is where we perform the
490 decrement. We go immediately from this function to resuming,
491 and can not safely call get_stop_pc () again. */
492 if (the_low_target
.set_pc
!= NULL
)
493 (*the_low_target
.set_pc
) (stop_pc
);
495 /* We consumed the pending SIGTRAP. */
496 event_child
->pending_is_breakpoint
= 0;
497 event_child
->status_pending_p
= 0;
498 event_child
->status_pending
= 0;
500 current_inferior
= saved_inferior
;
504 /* Return 1 if this process has an interesting status pending. This function
505 may silently resume an inferior process. */
507 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
509 struct process_info
*process
= (struct process_info
*) entry
;
511 if (process
->status_pending_p
)
512 if (check_removed_breakpoint (process
))
514 /* This thread was stopped at a breakpoint, and the breakpoint
515 is now gone. We were told to continue (or step...) all threads,
516 so GDB isn't trying to single-step past this breakpoint.
517 So instead of reporting the old SIGTRAP, pretend we got to
518 the breakpoint just after it was removed instead of just
519 before; resume the process. */
520 linux_resume_one_process (&process
->head
, 0, 0, NULL
);
524 return process
->status_pending_p
;
528 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
531 int to_wait_for
= -1;
534 to_wait_for
= (*childp
)->lwpid
;
539 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
544 perror_with_name ("waitpid");
549 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
554 perror_with_name ("waitpid (WCLONE)");
563 && (!WIFSTOPPED (*wstatp
)
564 || (WSTOPSIG (*wstatp
) != 32
565 && WSTOPSIG (*wstatp
) != 33)))
566 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
568 if (to_wait_for
== -1)
569 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
571 /* If we didn't find a process, one of two things presumably happened:
572 - A process we started and then detached from has exited. Ignore it.
573 - A process we are controlling has forked and the new child's stop
574 was reported to us by the kernel. Save its PID. */
575 if (*childp
== NULL
&& WIFSTOPPED (*wstatp
))
577 add_pid_to_list (&stopped_pids
, ret
);
580 else if (*childp
== NULL
)
583 (*childp
)->stopped
= 1;
584 (*childp
)->pending_is_breakpoint
= 0;
586 (*childp
)->last_status
= *wstatp
;
589 && WIFSTOPPED (*wstatp
))
591 current_inferior
= (struct thread_info
*)
592 find_inferior_id (&all_threads
, (*childp
)->lwpid
);
593 /* For testing only; i386_stop_pc prints out a diagnostic. */
594 if (the_low_target
.get_pc
!= NULL
)
600 linux_wait_for_event (struct thread_info
*child
)
603 struct process_info
*event_child
;
606 /* Check for a process with a pending status. */
607 /* It is possible that the user changed the pending task's registers since
608 it stopped. We correctly handle the change of PC if we hit a breakpoint
609 (in check_removed_breakpoint); signals should be reported anyway. */
612 event_child
= (struct process_info
*)
613 find_inferior (&all_processes
, status_pending_p
, NULL
);
614 if (debug_threads
&& event_child
)
615 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
619 event_child
= get_thread_process (child
);
620 if (event_child
->status_pending_p
621 && check_removed_breakpoint (event_child
))
625 if (event_child
!= NULL
)
627 if (event_child
->status_pending_p
)
630 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
631 event_child
->lwpid
, event_child
->status_pending
);
632 wstat
= event_child
->status_pending
;
633 event_child
->status_pending_p
= 0;
634 event_child
->status_pending
= 0;
635 current_inferior
= get_process_thread (event_child
);
640 /* We only enter this loop if no process has a pending wait status. Thus
641 any action taken in response to a wait status inside this loop is
642 responding as soon as we detect the status, not after any pending
649 event_child
= get_thread_process (child
);
651 linux_wait_for_process (&event_child
, &wstat
);
653 if (event_child
== NULL
)
654 error ("event from unknown child");
656 current_inferior
= (struct thread_info
*)
657 find_inferior_id (&all_threads
, event_child
->lwpid
);
659 /* Check for thread exit. */
660 if (! WIFSTOPPED (wstat
))
663 fprintf (stderr
, "LWP %ld exiting\n", event_child
->head
.id
);
665 /* If the last thread is exiting, just return. */
666 if (all_threads
.head
== all_threads
.tail
)
669 dead_thread_notify (thread_id_to_gdb_id (event_child
->lwpid
));
671 remove_inferior (&all_processes
, &event_child
->head
);
673 remove_thread (current_inferior
);
674 current_inferior
= (struct thread_info
*) all_threads
.head
;
676 /* If we were waiting for this particular child to do something...
677 well, it did something. */
681 /* Wait for a more interesting event. */
685 if (WIFSTOPPED (wstat
)
686 && WSTOPSIG (wstat
) == SIGSTOP
687 && event_child
->stop_expected
)
690 fprintf (stderr
, "Expected stop.\n");
691 event_child
->stop_expected
= 0;
692 linux_resume_one_process (&event_child
->head
,
693 event_child
->stepping
, 0, NULL
);
697 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
700 handle_extended_wait (event_child
, wstat
);
704 /* If GDB is not interested in this signal, don't stop other
705 threads, and don't report it to GDB. Just resume the
706 inferior right away. We do this for threading-related
707 signals as well as any that GDB specifically requested we
708 ignore. But never ignore SIGSTOP if we sent it ourselves,
709 and do not ignore signals when stepping - they may require
710 special handling to skip the signal handler. */
711 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
713 if (WIFSTOPPED (wstat
)
714 && !event_child
->stepping
717 (thread_db_active
&& (WSTOPSIG (wstat
) == __SIGRTMIN
718 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
721 (pass_signals
[target_signal_from_host (WSTOPSIG (wstat
))]
722 && (WSTOPSIG (wstat
) != SIGSTOP
|| !stopping_threads
))))
724 siginfo_t info
, *info_p
;
727 fprintf (stderr
, "Ignored signal %d for LWP %ld.\n",
728 WSTOPSIG (wstat
), event_child
->head
.id
);
730 if (ptrace (PTRACE_GETSIGINFO
, event_child
->lwpid
, 0, &info
) == 0)
734 linux_resume_one_process (&event_child
->head
,
735 event_child
->stepping
,
736 WSTOPSIG (wstat
), info_p
);
740 /* If this event was not handled above, and is not a SIGTRAP, report
742 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
745 /* If this target does not support breakpoints, we simply report the
746 SIGTRAP; it's of no concern to us. */
747 if (the_low_target
.get_pc
== NULL
)
750 stop_pc
= get_stop_pc ();
752 /* bp_reinsert will only be set if we were single-stepping.
753 Notice that we will resume the process after hitting
754 a gdbserver breakpoint; single-stepping to/over one
755 is not supported (yet). */
756 if (event_child
->bp_reinsert
!= 0)
759 fprintf (stderr
, "Reinserted breakpoint.\n");
760 reinsert_breakpoint (event_child
->bp_reinsert
);
761 event_child
->bp_reinsert
= 0;
763 /* Clear the single-stepping flag and SIGTRAP as we resume. */
764 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
769 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
771 if (check_breakpoints (stop_pc
) != 0)
773 /* We hit one of our own breakpoints. We mark it as a pending
774 breakpoint, so that check_removed_breakpoint () will do the PC
775 adjustment for us at the appropriate time. */
776 event_child
->pending_is_breakpoint
= 1;
777 event_child
->pending_stop_pc
= stop_pc
;
779 /* Now we need to put the breakpoint back. We continue in the event
780 loop instead of simply replacing the breakpoint right away,
781 in order to not lose signals sent to the thread that hit the
782 breakpoint. Unfortunately this increases the window where another
783 thread could sneak past the removed breakpoint. For the current
784 use of server-side breakpoints (thread creation) this is
785 acceptable; but it needs to be considered before this breakpoint
786 mechanism can be used in more general ways. For some breakpoints
787 it may be necessary to stop all other threads, but that should
788 be avoided where possible.
790 If breakpoint_reinsert_addr is NULL, that means that we can
791 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
792 mark it for reinsertion, and single-step.
794 Otherwise, call the target function to figure out where we need
795 our temporary breakpoint, create it, and continue executing this
797 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
799 event_child
->bp_reinsert
= stop_pc
;
800 uninsert_breakpoint (stop_pc
);
801 linux_resume_one_process (&event_child
->head
, 1, 0, NULL
);
805 reinsert_breakpoint_by_bp
806 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
807 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
813 /* If we were single-stepping, we definitely want to report the
814 SIGTRAP. The single-step operation has completed, so also
815 clear the stepping flag; in general this does not matter,
816 because the SIGTRAP will be reported to the client, which
817 will give us a new action for this thread, but clear it for
818 consistency anyway. It's safe to clear the stepping flag
819 because the only consumer of get_stop_pc () after this point
820 is check_removed_breakpoint, and pending_is_breakpoint is not
821 set. It might be wiser to use a step_completed flag instead. */
822 if (event_child
->stepping
)
824 event_child
->stepping
= 0;
828 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
829 Check if it is a breakpoint, and if so mark the process information
830 accordingly. This will handle both the necessary fiddling with the
831 PC on decr_pc_after_break targets and suppressing extra threads
832 hitting a breakpoint if two hit it at once and then GDB removes it
833 after the first is reported. Arguably it would be better to report
834 multiple threads hitting breakpoints simultaneously, but the current
835 remote protocol does not allow this. */
836 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
838 event_child
->pending_is_breakpoint
= 1;
839 event_child
->pending_stop_pc
= stop_pc
;
849 /* Wait for process, returns status. */
852 linux_wait (char *status
)
855 struct thread_info
*child
= NULL
;
858 /* If we were only supposed to resume one thread, only wait for
859 that thread - if it's still alive. If it died, however - which
860 can happen if we're coming from the thread death case below -
861 then we need to make sure we restart the other threads. We could
862 pick a thread at random or restart all; restarting all is less
864 if (cont_thread
!= 0 && cont_thread
!= -1)
866 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
869 /* No stepping, no signal - unless one is pending already, of course. */
872 struct thread_resume resume_info
;
873 resume_info
.thread
= -1;
874 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
875 linux_resume (&resume_info
);
881 w
= linux_wait_for_event (child
);
882 stop_all_processes ();
885 if (must_set_ptrace_flags
)
887 ptrace (PTRACE_SETOPTIONS
, inferior_pid
, 0, PTRACE_O_TRACECLONE
);
888 must_set_ptrace_flags
= 0;
891 /* If we are waiting for a particular child, and it exited,
892 linux_wait_for_event will return its exit status. Similarly if
893 the last child exited. If this is not the last child, however,
894 do not report it as exited until there is a 'thread exited' response
895 available in the remote protocol. Instead, just wait for another event.
896 This should be safe, because if the thread crashed we will already
897 have reported the termination signal to GDB; that should stop any
898 in-progress stepping operations, etc.
900 Report the exit status of the last thread to exit. This matches
901 LinuxThreads' behavior. */
903 if (all_threads
.head
== all_threads
.tail
)
907 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
910 free (all_processes
.head
);
911 all_processes
.head
= all_processes
.tail
= NULL
;
912 return WEXITSTATUS (w
);
914 else if (!WIFSTOPPED (w
))
916 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
919 free (all_processes
.head
);
920 all_processes
.head
= all_processes
.tail
= NULL
;
921 return target_signal_from_host (WTERMSIG (w
));
931 return target_signal_from_host (WSTOPSIG (w
));
934 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
935 thread groups are in use, we need to use tkill. */
938 kill_lwp (unsigned long lwpid
, int signo
)
940 static int tkill_failed
;
947 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
955 return kill (lwpid
, signo
);
959 send_sigstop (struct inferior_list_entry
*entry
)
961 struct process_info
*process
= (struct process_info
*) entry
;
963 if (process
->stopped
)
966 /* If we already have a pending stop signal for this process, don't
968 if (process
->stop_expected
)
971 fprintf (stderr
, "Have pending sigstop for process %ld\n",
974 /* We clear the stop_expected flag so that wait_for_sigstop
975 will receive the SIGSTOP event (instead of silently resuming and
976 waiting again). It'll be reset below. */
977 process
->stop_expected
= 0;
982 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
984 kill_lwp (process
->head
.id
, SIGSTOP
);
988 wait_for_sigstop (struct inferior_list_entry
*entry
)
990 struct process_info
*process
= (struct process_info
*) entry
;
991 struct thread_info
*saved_inferior
, *thread
;
993 unsigned long saved_tid
;
995 if (process
->stopped
)
998 saved_inferior
= current_inferior
;
999 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
1000 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
1002 wstat
= linux_wait_for_event (thread
);
1004 /* If we stopped with a non-SIGSTOP signal, save it for later
1005 and record the pending SIGSTOP. If the process exited, just
1007 if (WIFSTOPPED (wstat
)
1008 && WSTOPSIG (wstat
) != SIGSTOP
)
1011 fprintf (stderr
, "LWP %ld stopped with non-sigstop status %06x\n",
1012 process
->lwpid
, wstat
);
1013 process
->status_pending_p
= 1;
1014 process
->status_pending
= wstat
;
1015 process
->stop_expected
= 1;
1018 if (linux_thread_alive (saved_tid
))
1019 current_inferior
= saved_inferior
;
1023 fprintf (stderr
, "Previously current thread died.\n");
1025 /* Set a valid thread as current. */
1026 set_desired_inferior (0);
1031 stop_all_processes (void)
1033 stopping_threads
= 1;
1034 for_each_inferior (&all_processes
, send_sigstop
);
1035 for_each_inferior (&all_processes
, wait_for_sigstop
);
1036 stopping_threads
= 0;
1039 /* Resume execution of the inferior process.
1040 If STEP is nonzero, single-step it.
1041 If SIGNAL is nonzero, give it that signal. */
1044 linux_resume_one_process (struct inferior_list_entry
*entry
,
1045 int step
, int signal
, siginfo_t
*info
)
1047 struct process_info
*process
= (struct process_info
*) entry
;
1048 struct thread_info
*saved_inferior
;
1050 if (process
->stopped
== 0)
1053 /* If we have pending signals or status, and a new signal, enqueue the
1054 signal. Also enqueue the signal if we are waiting to reinsert a
1055 breakpoint; it will be picked up again below. */
1057 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
1058 || process
->bp_reinsert
!= 0))
1060 struct pending_signals
*p_sig
;
1061 p_sig
= malloc (sizeof (*p_sig
));
1062 p_sig
->prev
= process
->pending_signals
;
1063 p_sig
->signal
= signal
;
1065 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1067 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
1068 process
->pending_signals
= p_sig
;
1071 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
1074 saved_inferior
= current_inferior
;
1075 current_inferior
= get_process_thread (process
);
1078 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
1079 step
? "step" : "continue", signal
,
1080 process
->stop_expected
? "expected" : "not expected");
1082 /* This bit needs some thinking about. If we get a signal that
1083 we must report while a single-step reinsert is still pending,
1084 we often end up resuming the thread. It might be better to
1085 (ew) allow a stack of pending events; then we could be sure that
1086 the reinsert happened right away and not lose any signals.
1088 Making this stack would also shrink the window in which breakpoints are
1089 uninserted (see comment in linux_wait_for_process) but not enough for
1090 complete correctness, so it won't solve that problem. It may be
1091 worthwhile just to solve this one, however. */
1092 if (process
->bp_reinsert
!= 0)
1095 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
1097 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
1100 /* Postpone any pending signal. It was enqueued above. */
1104 check_removed_breakpoint (process
);
1106 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
1108 fprintf (stderr
, " ");
1109 (*the_low_target
.get_pc
) ();
1112 /* If we have pending signals, consume one unless we are trying to reinsert
1114 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
1116 struct pending_signals
**p_sig
;
1118 p_sig
= &process
->pending_signals
;
1119 while ((*p_sig
)->prev
!= NULL
)
1120 p_sig
= &(*p_sig
)->prev
;
1122 signal
= (*p_sig
)->signal
;
1123 if ((*p_sig
)->info
.si_signo
!= 0)
1124 ptrace (PTRACE_SETSIGINFO
, process
->lwpid
, 0, &(*p_sig
)->info
);
1130 regcache_invalidate_one ((struct inferior_list_entry
*)
1131 get_process_thread (process
));
1133 process
->stopped
= 0;
1134 process
->stepping
= step
;
1135 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
1137 current_inferior
= saved_inferior
;
1139 perror_with_name ("ptrace");
1142 static struct thread_resume
*resume_ptr
;
1144 /* This function is called once per thread. We look up the thread
1145 in RESUME_PTR, and mark the thread with a pointer to the appropriate
1148 This algorithm is O(threads * resume elements), but resume elements
1149 is small (and will remain small at least until GDB supports thread
1152 linux_set_resume_request (struct inferior_list_entry
*entry
)
1154 struct process_info
*process
;
1155 struct thread_info
*thread
;
1158 thread
= (struct thread_info
*) entry
;
1159 process
= get_thread_process (thread
);
1162 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
1165 process
->resume
= &resume_ptr
[ndx
];
1168 /* This function is called once per thread. We check the thread's resume
1169 request, which will tell us whether to resume, step, or leave the thread
1170 stopped; and what signal, if any, it should be sent. For threads which
1171 we aren't explicitly told otherwise, we preserve the stepping flag; this
1172 is used for stepping over gdbserver-placed breakpoints. */
1175 linux_continue_one_thread (struct inferior_list_entry
*entry
)
1177 struct process_info
*process
;
1178 struct thread_info
*thread
;
1181 thread
= (struct thread_info
*) entry
;
1182 process
= get_thread_process (thread
);
1184 if (process
->resume
->leave_stopped
)
1187 if (process
->resume
->thread
== -1)
1188 step
= process
->stepping
|| process
->resume
->step
;
1190 step
= process
->resume
->step
;
1192 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
, NULL
);
1194 process
->resume
= NULL
;
1197 /* This function is called once per thread. We check the thread's resume
1198 request, which will tell us whether to resume, step, or leave the thread
1199 stopped; and what signal, if any, it should be sent. We queue any needed
1200 signals, since we won't actually resume. We already have a pending event
1201 to report, so we don't need to preserve any step requests; they should
1202 be re-issued if necessary. */
1205 linux_queue_one_thread (struct inferior_list_entry
*entry
)
1207 struct process_info
*process
;
1208 struct thread_info
*thread
;
1210 thread
= (struct thread_info
*) entry
;
1211 process
= get_thread_process (thread
);
1213 if (process
->resume
->leave_stopped
)
1216 /* If we have a new signal, enqueue the signal. */
1217 if (process
->resume
->sig
!= 0)
1219 struct pending_signals
*p_sig
;
1220 p_sig
= malloc (sizeof (*p_sig
));
1221 p_sig
->prev
= process
->pending_signals
;
1222 p_sig
->signal
= process
->resume
->sig
;
1223 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1225 /* If this is the same signal we were previously stopped by,
1226 make sure to queue its siginfo. We can ignore the return
1227 value of ptrace; if it fails, we'll skip
1228 PTRACE_SETSIGINFO. */
1229 if (WIFSTOPPED (process
->last_status
)
1230 && WSTOPSIG (process
->last_status
) == process
->resume
->sig
)
1231 ptrace (PTRACE_GETSIGINFO
, process
->lwpid
, 0, &p_sig
->info
);
1233 process
->pending_signals
= p_sig
;
1236 process
->resume
= NULL
;
1239 /* Set DUMMY if this process has an interesting status pending. */
1241 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1243 struct process_info
*process
= (struct process_info
*) entry
;
1245 /* Processes which will not be resumed are not interesting, because
1246 we might not wait for them next time through linux_wait. */
1247 if (process
->resume
->leave_stopped
)
1250 /* If this thread has a removed breakpoint, we won't have any
1251 events to report later, so check now. check_removed_breakpoint
1252 may clear status_pending_p. We avoid calling check_removed_breakpoint
1253 for any thread that we are not otherwise going to resume - this
1254 lets us preserve stopped status when two threads hit a breakpoint.
1255 GDB removes the breakpoint to single-step a particular thread
1256 past it, then re-inserts it and resumes all threads. We want
1257 to report the second thread without resuming it in the interim. */
1258 if (process
->status_pending_p
)
1259 check_removed_breakpoint (process
);
1261 if (process
->status_pending_p
)
1262 * (int *) flag_p
= 1;
1268 linux_resume (struct thread_resume
*resume_info
)
1272 /* Yes, the use of a global here is rather ugly. */
1273 resume_ptr
= resume_info
;
1275 for_each_inferior (&all_threads
, linux_set_resume_request
);
1277 /* If there is a thread which would otherwise be resumed, which
1278 has a pending status, then don't resume any threads - we can just
1279 report the pending status. Make sure to queue any signals
1280 that would otherwise be sent. */
1282 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1287 fprintf (stderr
, "Not resuming, pending status\n");
1289 fprintf (stderr
, "Resuming, no pending status\n");
1293 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1298 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1302 #ifdef HAVE_LINUX_USRREGS
1305 register_addr (int regnum
)
1309 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1310 error ("Invalid register number %d.", regnum
);
1312 addr
= the_low_target
.regmap
[regnum
];
1317 /* Fetch one register. */
1319 fetch_register (int regno
)
1325 if (regno
>= the_low_target
.num_regs
)
1327 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1330 regaddr
= register_addr (regno
);
1333 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1334 & - sizeof (PTRACE_XFER_TYPE
);
1335 buf
= alloca (size
);
1336 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1339 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1340 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1341 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1344 /* Warning, not error, in case we are attached; sometimes the
1345 kernel doesn't let us at the registers. */
1346 char *err
= strerror (errno
);
1347 char *msg
= alloca (strlen (err
) + 128);
1348 sprintf (msg
, "reading register %d: %s", regno
, err
);
1353 if (the_low_target
.left_pad_xfer
1354 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1355 supply_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1356 - register_size (regno
)));
1358 supply_register (regno
, buf
);
1363 /* Fetch all registers, or just one, from the child process. */
1365 usr_fetch_inferior_registers (int regno
)
1367 if (regno
== -1 || regno
== 0)
1368 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1369 fetch_register (regno
);
1371 fetch_register (regno
);
1374 /* Store our register values back into the inferior.
1375 If REGNO is -1, do this for all registers.
1376 Otherwise, REGNO specifies which register (so we can save time). */
1378 usr_store_inferior_registers (int regno
)
1386 if (regno
>= the_low_target
.num_regs
)
1389 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1392 regaddr
= register_addr (regno
);
1396 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1397 & - sizeof (PTRACE_XFER_TYPE
);
1398 buf
= alloca (size
);
1399 memset (buf
, 0, size
);
1400 if (the_low_target
.left_pad_xfer
1401 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1402 collect_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1403 - register_size (regno
)));
1405 collect_register (regno
, buf
);
1406 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1409 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1410 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1413 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1415 char *err
= strerror (errno
);
1416 char *msg
= alloca (strlen (err
) + 128);
1417 sprintf (msg
, "writing register %d: %s",
1423 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1427 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1428 usr_store_inferior_registers (regno
);
1430 #endif /* HAVE_LINUX_USRREGS */
1434 #ifdef HAVE_LINUX_REGSETS
1437 regsets_fetch_inferior_registers ()
1439 struct regset_info
*regset
;
1440 int saw_general_regs
= 0;
1442 regset
= target_regsets
;
1444 while (regset
->size
>= 0)
1449 if (regset
->size
== 0)
1455 buf
= malloc (regset
->size
);
1456 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1461 /* If we get EIO on the first regset, do not try regsets again.
1462 If we get EIO on a later regset, disable that regset. */
1463 if (regset
== target_regsets
)
1477 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1482 else if (regset
->type
== GENERAL_REGS
)
1483 saw_general_regs
= 1;
1484 regset
->store_function (buf
);
1487 if (saw_general_regs
)
1494 regsets_store_inferior_registers ()
1496 struct regset_info
*regset
;
1497 int saw_general_regs
= 0;
1499 regset
= target_regsets
;
1501 while (regset
->size
>= 0)
1506 if (regset
->size
== 0)
1512 buf
= malloc (regset
->size
);
1514 /* First fill the buffer with the current register set contents,
1515 in case there are any items in the kernel's regset that are
1516 not in gdbserver's regcache. */
1517 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1521 /* Then overlay our cached registers on that. */
1522 regset
->fill_function (buf
);
1524 /* Only now do we write the register set. */
1525 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1532 /* If we get EIO on the first regset, do not try regsets again.
1533 If we get EIO on a later regset, disable that regset. */
1534 if (regset
== target_regsets
)
1547 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1550 else if (regset
->type
== GENERAL_REGS
)
1551 saw_general_regs
= 1;
1555 if (saw_general_regs
)
1562 #endif /* HAVE_LINUX_REGSETS */
1566 linux_fetch_registers (int regno
)
1568 #ifdef HAVE_LINUX_REGSETS
1571 if (regsets_fetch_inferior_registers () == 0)
1575 #ifdef HAVE_LINUX_USRREGS
1576 usr_fetch_inferior_registers (regno
);
1581 linux_store_registers (int regno
)
1583 #ifdef HAVE_LINUX_REGSETS
1586 if (regsets_store_inferior_registers () == 0)
1590 #ifdef HAVE_LINUX_USRREGS
1591 usr_store_inferior_registers (regno
);
1596 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1597 to debugger memory starting at MYADDR. */
1600 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1603 /* Round starting address down to longword boundary. */
1604 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1605 /* Round ending address up; get number of longwords that makes. */
1607 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1608 / sizeof (PTRACE_XFER_TYPE
);
1609 /* Allocate buffer of that many longwords. */
1610 register PTRACE_XFER_TYPE
*buffer
1611 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1615 /* Try using /proc. Don't bother for one word. */
1616 if (len
>= 3 * sizeof (long))
1618 /* We could keep this file open and cache it - possibly one per
1619 thread. That requires some juggling, but is even faster. */
1620 sprintf (filename
, "/proc/%ld/mem", inferior_pid
);
1621 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
1625 /* If pread64 is available, use it. It's faster if the kernel
1626 supports it (only one syscall), and it's 64-bit safe even on
1627 32-bit platforms (for instance, SPARC debugging a SPARC64
1630 if (pread64 (fd
, myaddr
, len
, memaddr
) != len
)
1632 if (lseek (fd
, memaddr
, SEEK_SET
) == -1 || read (fd
, memaddr
, len
) != len
)
1644 /* Read all the longwords */
1645 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1648 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1653 /* Copy appropriate bytes out of the buffer. */
1654 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1659 /* Copy LEN bytes of data from debugger memory at MYADDR
1660 to inferior's memory at MEMADDR.
1661 On failure (cannot write the inferior)
1662 returns the value of errno. */
1665 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
1668 /* Round starting address down to longword boundary. */
1669 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1670 /* Round ending address up; get number of longwords that makes. */
1672 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1673 /* Allocate buffer of that many longwords. */
1674 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1679 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1682 /* Fill start and end extra bytes of buffer with existing memory data. */
1684 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1685 (PTRACE_ARG3_TYPE
) addr
, 0);
1690 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1691 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1692 * sizeof (PTRACE_XFER_TYPE
)),
1696 /* Copy data to be written over corresponding part of buffer */
1698 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1700 /* Write the entire buffer. */
1702 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1705 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1713 static int linux_supports_tracefork_flag
;
1715 /* A helper function for linux_test_for_tracefork, called after fork (). */
1718 linux_tracefork_child (void)
1720 ptrace (PTRACE_TRACEME
, 0, 0, 0);
1721 kill (getpid (), SIGSTOP
);
1726 /* Wrapper function for waitpid which handles EINTR. */
1729 my_waitpid (int pid
, int *status
, int flags
)
1734 ret
= waitpid (pid
, status
, flags
);
1736 while (ret
== -1 && errno
== EINTR
);
1741 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
1742 sure that we can enable the option, and that it had the desired
1746 linux_test_for_tracefork (void)
1748 int child_pid
, ret
, status
;
1751 linux_supports_tracefork_flag
= 0;
1753 child_pid
= fork ();
1754 if (child_pid
== -1)
1755 perror_with_name ("fork");
1758 linux_tracefork_child ();
1760 ret
= my_waitpid (child_pid
, &status
, 0);
1762 perror_with_name ("waitpid");
1763 else if (ret
!= child_pid
)
1764 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
1765 if (! WIFSTOPPED (status
))
1766 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
1768 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
1771 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
1774 warning ("linux_test_for_tracefork: failed to kill child");
1778 ret
= my_waitpid (child_pid
, &status
, 0);
1779 if (ret
!= child_pid
)
1780 warning ("linux_test_for_tracefork: failed to wait for killed child");
1781 else if (!WIFSIGNALED (status
))
1782 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
1783 "killed child", status
);
1788 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
1790 warning ("linux_test_for_tracefork: failed to resume child");
1792 ret
= my_waitpid (child_pid
, &status
, 0);
1794 if (ret
== child_pid
&& WIFSTOPPED (status
)
1795 && status
>> 16 == PTRACE_EVENT_FORK
)
1798 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
1799 if (ret
== 0 && second_pid
!= 0)
1803 linux_supports_tracefork_flag
= 1;
1804 my_waitpid (second_pid
, &second_status
, 0);
1805 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
1807 warning ("linux_test_for_tracefork: failed to kill second child");
1808 my_waitpid (second_pid
, &status
, 0);
1812 warning ("linux_test_for_tracefork: unexpected result from waitpid "
1813 "(%d, status 0x%x)", ret
, status
);
1817 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
1819 warning ("linux_test_for_tracefork: failed to kill child");
1820 my_waitpid (child_pid
, &status
, 0);
1822 while (WIFSTOPPED (status
));
1827 linux_look_up_symbols (void)
1829 #ifdef USE_THREAD_DB
1830 if (thread_db_active
)
1833 thread_db_active
= thread_db_init (!linux_supports_tracefork_flag
);
1838 linux_request_interrupt (void)
1840 extern unsigned long signal_pid
;
1842 if (cont_thread
!= 0 && cont_thread
!= -1)
1844 struct process_info
*process
;
1846 process
= get_thread_process (current_inferior
);
1847 kill_lwp (process
->lwpid
, SIGINT
);
1850 kill_lwp (signal_pid
, SIGINT
);
1853 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1854 to debugger memory starting at MYADDR. */
1857 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
1859 char filename
[PATH_MAX
];
1862 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1864 fd
= open (filename
, O_RDONLY
);
1868 if (offset
!= (CORE_ADDR
) 0
1869 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1872 n
= read (fd
, myaddr
, len
);
1879 /* These watchpoint related wrapper functions simply pass on the function call
1880 if the target has registered a corresponding function. */
1883 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1885 if (the_low_target
.insert_watchpoint
!= NULL
)
1886 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1888 /* Unsupported (see target.h). */
1893 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1895 if (the_low_target
.remove_watchpoint
!= NULL
)
1896 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1898 /* Unsupported (see target.h). */
1903 linux_stopped_by_watchpoint (void)
1905 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1906 return the_low_target
.stopped_by_watchpoint ();
1912 linux_stopped_data_address (void)
1914 if (the_low_target
.stopped_data_address
!= NULL
)
1915 return the_low_target
.stopped_data_address ();
1920 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
1921 #if defined(__mcoldfire__)
1922 /* These should really be defined in the kernel's ptrace.h header. */
1923 #define PT_TEXT_ADDR 49*4
1924 #define PT_DATA_ADDR 50*4
1925 #define PT_TEXT_END_ADDR 51*4
1928 /* Under uClinux, programs are loaded at non-zero offsets, which we need
1929 to tell gdb about. */
1932 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
1934 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1935 unsigned long text
, text_end
, data
;
1936 int pid
= get_thread_process (current_inferior
)->head
.id
;
1940 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
1941 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
1942 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
1946 /* Both text and data offsets produced at compile-time (and so
1947 used by gdb) are relative to the beginning of the program,
1948 with the data segment immediately following the text segment.
1949 However, the actual runtime layout in memory may put the data
1950 somewhere else, so when we send gdb a data base-address, we
1951 use the real data base address and subtract the compile-time
1952 data base-address from it (which is just the length of the
1953 text segment). BSS immediately follows data in both
1956 *data_p
= data
- (text_end
- text
);
1966 linux_arch_string (void)
1968 return the_low_target
.arch_string
;
1971 static struct target_ops linux_target_ops
= {
1972 linux_create_inferior
,
1980 linux_fetch_registers
,
1981 linux_store_registers
,
1984 linux_look_up_symbols
,
1985 linux_request_interrupt
,
1987 linux_insert_watchpoint
,
1988 linux_remove_watchpoint
,
1989 linux_stopped_by_watchpoint
,
1990 linux_stopped_data_address
,
1991 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
1996 #ifdef USE_THREAD_DB
1997 thread_db_get_tls_address
,
2005 linux_init_signals ()
2007 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
2008 to find what the cancel signal actually is. */
2009 signal (__SIGRTMIN
+1, SIG_IGN
);
2013 initialize_low (void)
2015 thread_db_active
= 0;
2016 set_target_ops (&linux_target_ops
);
2017 set_breakpoint_data (the_low_target
.breakpoint
,
2018 the_low_target
.breakpoint_len
);
2020 linux_init_signals ();
2021 linux_test_for_tracefork ();