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
))
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 /* Normally we will get the pending SIGSTOP. But in some cases
174 we might get another signal delivered to the group first.
175 If we do, be sure not to lose it. */
176 if (WSTOPSIG (status
) == SIGSTOP
)
178 if (stopping_threads
)
179 new_process
->stopped
= 1;
181 ptrace (PTRACE_CONT
, new_pid
, 0, 0);
185 new_process
->stop_expected
= 1;
186 if (stopping_threads
)
188 new_process
->stopped
= 1;
189 new_process
->status_pending_p
= 1;
190 new_process
->status_pending
= status
;
193 /* Pass the signal on. This is what GDB does - except
194 shouldn't we really report it instead? */
195 ptrace (PTRACE_CONT
, new_pid
, 0, WSTOPSIG (status
));
198 /* Always resume the current thread. If we are stopping
199 threads, it will have a pending SIGSTOP; we may as well
201 linux_resume_one_process (&event_child
->head
,
202 event_child
->stepping
, 0, NULL
);
206 /* This function should only be called if the process got a SIGTRAP.
207 The SIGTRAP could mean several things.
209 On i386, where decr_pc_after_break is non-zero:
210 If we were single-stepping this process using PTRACE_SINGLESTEP,
211 we will get only the one SIGTRAP (even if the instruction we
212 stepped over was a breakpoint). The value of $eip will be the
214 If we continue the process using PTRACE_CONT, we will get a
215 SIGTRAP when we hit a breakpoint. The value of $eip will be
216 the instruction after the breakpoint (i.e. needs to be
217 decremented). If we report the SIGTRAP to GDB, we must also
218 report the undecremented PC. If we cancel the SIGTRAP, we
219 must resume at the decremented PC.
221 (Presumably, not yet tested) On a non-decr_pc_after_break machine
222 with hardware or kernel single-step:
223 If we single-step over a breakpoint instruction, our PC will
224 point at the following instruction. If we continue and hit a
225 breakpoint instruction, our PC will point at the breakpoint
231 CORE_ADDR stop_pc
= (*the_low_target
.get_pc
) ();
233 if (get_thread_process (current_inferior
)->stepping
)
236 return stop_pc
- the_low_target
.decr_pc_after_break
;
240 add_process (unsigned long pid
)
242 struct process_info
*process
;
244 process
= (struct process_info
*) malloc (sizeof (*process
));
245 memset (process
, 0, sizeof (*process
));
247 process
->head
.id
= pid
;
248 process
->lwpid
= pid
;
250 add_inferior_to_list (&all_processes
, &process
->head
);
255 /* Start an inferior process and returns its pid.
256 ALLARGS is a vector of program-name and args. */
259 linux_create_inferior (char *program
, char **allargs
)
264 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
270 perror_with_name ("fork");
274 ptrace (PTRACE_TRACEME
, 0, 0, 0);
276 signal (__SIGRTMIN
+ 1, SIG_DFL
);
280 execv (program
, allargs
);
282 execvp (program
, allargs
);
284 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
290 new_process
= add_process (pid
);
291 add_thread (pid
, new_process
, pid
);
292 must_set_ptrace_flags
= 1;
297 /* Attach to an inferior process. */
300 linux_attach_lwp (unsigned long pid
)
302 struct process_info
*new_process
;
304 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
306 fprintf (stderr
, "Cannot attach to process %ld: %s (%d)\n", pid
,
307 strerror (errno
), errno
);
310 /* If we fail to attach to an LWP, just return. */
311 if (all_threads
.head
== NULL
)
316 ptrace (PTRACE_SETOPTIONS
, pid
, 0, PTRACE_O_TRACECLONE
);
318 new_process
= (struct process_info
*) add_process (pid
);
319 add_thread (pid
, new_process
, pid
);
320 new_thread_notify (thread_id_to_gdb_id (new_process
->lwpid
));
322 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
323 brings it to a halt. We should ignore that SIGSTOP and resume the process
324 (unless this is the first process, in which case the flag will be cleared
327 On the other hand, if we are currently trying to stop all threads, we
328 should treat the new thread as if we had sent it a SIGSTOP. This works
329 because we are guaranteed that add_process added us to the end of the
330 list, and so the new thread has not yet reached wait_for_sigstop (but
332 if (! stopping_threads
)
333 new_process
->stop_expected
= 1;
337 linux_attach (unsigned long pid
)
339 struct process_info
*process
;
341 linux_attach_lwp (pid
);
343 /* Don't ignore the initial SIGSTOP if we just attached to this process.
344 It will be collected by wait shortly. */
345 process
= (struct process_info
*) find_inferior_id (&all_processes
, pid
);
346 process
->stop_expected
= 0;
351 /* Kill the inferior process. Make us have no inferior. */
354 linux_kill_one_process (struct inferior_list_entry
*entry
)
356 struct thread_info
*thread
= (struct thread_info
*) entry
;
357 struct process_info
*process
= get_thread_process (thread
);
360 /* We avoid killing the first thread here, because of a Linux kernel (at
361 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
362 the children get a chance to be reaped, it will remain a zombie
364 if (entry
== all_threads
.head
)
369 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
371 /* Make sure it died. The loop is most likely unnecessary. */
372 wstat
= linux_wait_for_event (thread
);
373 } while (WIFSTOPPED (wstat
));
379 struct thread_info
*thread
= (struct thread_info
*) all_threads
.head
;
380 struct process_info
*process
;
386 for_each_inferior (&all_threads
, linux_kill_one_process
);
388 /* See the comment in linux_kill_one_process. We did not kill the first
389 thread in the list, so do so now. */
390 process
= get_thread_process (thread
);
393 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
395 /* Make sure it died. The loop is most likely unnecessary. */
396 wstat
= linux_wait_for_event (thread
);
397 } while (WIFSTOPPED (wstat
));
401 linux_detach_one_process (struct inferior_list_entry
*entry
)
403 struct thread_info
*thread
= (struct thread_info
*) entry
;
404 struct process_info
*process
= get_thread_process (thread
);
406 /* Make sure the process isn't stopped at a breakpoint that's
408 check_removed_breakpoint (process
);
410 /* If this process is stopped but is expecting a SIGSTOP, then make
411 sure we take care of that now. This isn't absolutely guaranteed
412 to collect the SIGSTOP, but is fairly likely to. */
413 if (process
->stop_expected
)
415 /* Clear stop_expected, so that the SIGSTOP will be reported. */
416 process
->stop_expected
= 0;
417 if (process
->stopped
)
418 linux_resume_one_process (&process
->head
, 0, 0, NULL
);
419 linux_wait_for_event (thread
);
422 /* Flush any pending changes to the process's registers. */
423 regcache_invalidate_one ((struct inferior_list_entry
*)
424 get_process_thread (process
));
426 /* Finally, let it resume. */
427 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
433 delete_all_breakpoints ();
434 for_each_inferior (&all_threads
, linux_detach_one_process
);
442 extern unsigned long signal_pid
;
446 ret
= waitpid (signal_pid
, &status
, 0);
447 if (WIFEXITED (status
) || WIFSIGNALED (status
))
449 } while (ret
!= -1 || errno
!= ECHILD
);
452 /* Return nonzero if the given thread is still alive. */
454 linux_thread_alive (unsigned long lwpid
)
456 if (find_inferior_id (&all_threads
, lwpid
) != NULL
)
462 /* Return nonzero if this process stopped at a breakpoint which
463 no longer appears to be inserted. Also adjust the PC
464 appropriately to resume where the breakpoint used to be. */
466 check_removed_breakpoint (struct process_info
*event_child
)
469 struct thread_info
*saved_inferior
;
471 if (event_child
->pending_is_breakpoint
== 0)
475 fprintf (stderr
, "Checking for breakpoint in process %ld.\n",
478 saved_inferior
= current_inferior
;
479 current_inferior
= get_process_thread (event_child
);
481 stop_pc
= get_stop_pc ();
483 /* If the PC has changed since we stopped, then we shouldn't do
484 anything. This happens if, for instance, GDB handled the
485 decr_pc_after_break subtraction itself. */
486 if (stop_pc
!= event_child
->pending_stop_pc
)
489 fprintf (stderr
, "Ignoring, PC was changed. Old PC was 0x%08llx\n",
490 event_child
->pending_stop_pc
);
492 event_child
->pending_is_breakpoint
= 0;
493 current_inferior
= saved_inferior
;
497 /* If the breakpoint is still there, we will report hitting it. */
498 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
501 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
502 current_inferior
= saved_inferior
;
507 fprintf (stderr
, "Removed breakpoint.\n");
509 /* For decr_pc_after_break targets, here is where we perform the
510 decrement. We go immediately from this function to resuming,
511 and can not safely call get_stop_pc () again. */
512 if (the_low_target
.set_pc
!= NULL
)
513 (*the_low_target
.set_pc
) (stop_pc
);
515 /* We consumed the pending SIGTRAP. */
516 event_child
->pending_is_breakpoint
= 0;
517 event_child
->status_pending_p
= 0;
518 event_child
->status_pending
= 0;
520 current_inferior
= saved_inferior
;
524 /* Return 1 if this process has an interesting status pending. This function
525 may silently resume an inferior process. */
527 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
529 struct process_info
*process
= (struct process_info
*) entry
;
531 if (process
->status_pending_p
)
532 if (check_removed_breakpoint (process
))
534 /* This thread was stopped at a breakpoint, and the breakpoint
535 is now gone. We were told to continue (or step...) all threads,
536 so GDB isn't trying to single-step past this breakpoint.
537 So instead of reporting the old SIGTRAP, pretend we got to
538 the breakpoint just after it was removed instead of just
539 before; resume the process. */
540 linux_resume_one_process (&process
->head
, 0, 0, NULL
);
544 return process
->status_pending_p
;
548 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
551 int to_wait_for
= -1;
554 to_wait_for
= (*childp
)->lwpid
;
559 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
564 perror_with_name ("waitpid");
569 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
574 perror_with_name ("waitpid (WCLONE)");
583 && (!WIFSTOPPED (*wstatp
)
584 || (WSTOPSIG (*wstatp
) != 32
585 && WSTOPSIG (*wstatp
) != 33)))
586 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
588 if (to_wait_for
== -1)
589 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
591 /* If we didn't find a process, one of two things presumably happened:
592 - A process we started and then detached from has exited. Ignore it.
593 - A process we are controlling has forked and the new child's stop
594 was reported to us by the kernel. Save its PID. */
595 if (*childp
== NULL
&& WIFSTOPPED (*wstatp
))
597 add_pid_to_list (&stopped_pids
, ret
);
600 else if (*childp
== NULL
)
603 (*childp
)->stopped
= 1;
604 (*childp
)->pending_is_breakpoint
= 0;
606 (*childp
)->last_status
= *wstatp
;
609 && WIFSTOPPED (*wstatp
))
611 current_inferior
= (struct thread_info
*)
612 find_inferior_id (&all_threads
, (*childp
)->lwpid
);
613 /* For testing only; i386_stop_pc prints out a diagnostic. */
614 if (the_low_target
.get_pc
!= NULL
)
620 linux_wait_for_event (struct thread_info
*child
)
623 struct process_info
*event_child
;
626 /* Check for a process with a pending status. */
627 /* It is possible that the user changed the pending task's registers since
628 it stopped. We correctly handle the change of PC if we hit a breakpoint
629 (in check_removed_breakpoint); signals should be reported anyway. */
632 event_child
= (struct process_info
*)
633 find_inferior (&all_processes
, status_pending_p
, NULL
);
634 if (debug_threads
&& event_child
)
635 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
639 event_child
= get_thread_process (child
);
640 if (event_child
->status_pending_p
641 && check_removed_breakpoint (event_child
))
645 if (event_child
!= NULL
)
647 if (event_child
->status_pending_p
)
650 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
651 event_child
->lwpid
, event_child
->status_pending
);
652 wstat
= event_child
->status_pending
;
653 event_child
->status_pending_p
= 0;
654 event_child
->status_pending
= 0;
655 current_inferior
= get_process_thread (event_child
);
660 /* We only enter this loop if no process has a pending wait status. Thus
661 any action taken in response to a wait status inside this loop is
662 responding as soon as we detect the status, not after any pending
669 event_child
= get_thread_process (child
);
671 linux_wait_for_process (&event_child
, &wstat
);
673 if (event_child
== NULL
)
674 error ("event from unknown child");
676 current_inferior
= (struct thread_info
*)
677 find_inferior_id (&all_threads
, event_child
->lwpid
);
679 /* Check for thread exit. */
680 if (! WIFSTOPPED (wstat
))
683 fprintf (stderr
, "LWP %ld exiting\n", event_child
->head
.id
);
685 /* If the last thread is exiting, just return. */
686 if (all_threads
.head
== all_threads
.tail
)
689 dead_thread_notify (thread_id_to_gdb_id (event_child
->lwpid
));
691 remove_inferior (&all_processes
, &event_child
->head
);
693 remove_thread (current_inferior
);
694 current_inferior
= (struct thread_info
*) all_threads
.head
;
696 /* If we were waiting for this particular child to do something...
697 well, it did something. */
701 /* Wait for a more interesting event. */
705 if (WIFSTOPPED (wstat
)
706 && WSTOPSIG (wstat
) == SIGSTOP
707 && event_child
->stop_expected
)
710 fprintf (stderr
, "Expected stop.\n");
711 event_child
->stop_expected
= 0;
712 linux_resume_one_process (&event_child
->head
,
713 event_child
->stepping
, 0, NULL
);
717 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
720 handle_extended_wait (event_child
, wstat
);
724 /* If GDB is not interested in this signal, don't stop other
725 threads, and don't report it to GDB. Just resume the
726 inferior right away. We do this for threading-related
727 signals as well as any that GDB specifically requested we
728 ignore. But never ignore SIGSTOP if we sent it ourselves,
729 and do not ignore signals when stepping - they may require
730 special handling to skip the signal handler. */
731 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
733 if (WIFSTOPPED (wstat
)
734 && !event_child
->stepping
737 (thread_db_active
&& (WSTOPSIG (wstat
) == __SIGRTMIN
738 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
741 (pass_signals
[target_signal_from_host (WSTOPSIG (wstat
))]
742 && (WSTOPSIG (wstat
) != SIGSTOP
|| !stopping_threads
))))
744 siginfo_t info
, *info_p
;
747 fprintf (stderr
, "Ignored signal %d for LWP %ld.\n",
748 WSTOPSIG (wstat
), event_child
->head
.id
);
750 if (ptrace (PTRACE_GETSIGINFO
, event_child
->lwpid
, 0, &info
) == 0)
754 linux_resume_one_process (&event_child
->head
,
755 event_child
->stepping
,
756 WSTOPSIG (wstat
), info_p
);
760 /* If this event was not handled above, and is not a SIGTRAP, report
762 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
765 /* If this target does not support breakpoints, we simply report the
766 SIGTRAP; it's of no concern to us. */
767 if (the_low_target
.get_pc
== NULL
)
770 stop_pc
= get_stop_pc ();
772 /* bp_reinsert will only be set if we were single-stepping.
773 Notice that we will resume the process after hitting
774 a gdbserver breakpoint; single-stepping to/over one
775 is not supported (yet). */
776 if (event_child
->bp_reinsert
!= 0)
779 fprintf (stderr
, "Reinserted breakpoint.\n");
780 reinsert_breakpoint (event_child
->bp_reinsert
);
781 event_child
->bp_reinsert
= 0;
783 /* Clear the single-stepping flag and SIGTRAP as we resume. */
784 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
789 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
791 if (check_breakpoints (stop_pc
) != 0)
793 /* We hit one of our own breakpoints. We mark it as a pending
794 breakpoint, so that check_removed_breakpoint () will do the PC
795 adjustment for us at the appropriate time. */
796 event_child
->pending_is_breakpoint
= 1;
797 event_child
->pending_stop_pc
= stop_pc
;
799 /* Now we need to put the breakpoint back. We continue in the event
800 loop instead of simply replacing the breakpoint right away,
801 in order to not lose signals sent to the thread that hit the
802 breakpoint. Unfortunately this increases the window where another
803 thread could sneak past the removed breakpoint. For the current
804 use of server-side breakpoints (thread creation) this is
805 acceptable; but it needs to be considered before this breakpoint
806 mechanism can be used in more general ways. For some breakpoints
807 it may be necessary to stop all other threads, but that should
808 be avoided where possible.
810 If breakpoint_reinsert_addr is NULL, that means that we can
811 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
812 mark it for reinsertion, and single-step.
814 Otherwise, call the target function to figure out where we need
815 our temporary breakpoint, create it, and continue executing this
817 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
819 event_child
->bp_reinsert
= stop_pc
;
820 uninsert_breakpoint (stop_pc
);
821 linux_resume_one_process (&event_child
->head
, 1, 0, NULL
);
825 reinsert_breakpoint_by_bp
826 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
827 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
833 /* If we were single-stepping, we definitely want to report the
834 SIGTRAP. The single-step operation has completed, so also
835 clear the stepping flag; in general this does not matter,
836 because the SIGTRAP will be reported to the client, which
837 will give us a new action for this thread, but clear it for
838 consistency anyway. It's safe to clear the stepping flag
839 because the only consumer of get_stop_pc () after this point
840 is check_removed_breakpoint, and pending_is_breakpoint is not
841 set. It might be wiser to use a step_completed flag instead. */
842 if (event_child
->stepping
)
844 event_child
->stepping
= 0;
848 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
849 Check if it is a breakpoint, and if so mark the process information
850 accordingly. This will handle both the necessary fiddling with the
851 PC on decr_pc_after_break targets and suppressing extra threads
852 hitting a breakpoint if two hit it at once and then GDB removes it
853 after the first is reported. Arguably it would be better to report
854 multiple threads hitting breakpoints simultaneously, but the current
855 remote protocol does not allow this. */
856 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
858 event_child
->pending_is_breakpoint
= 1;
859 event_child
->pending_stop_pc
= stop_pc
;
869 /* Wait for process, returns status. */
872 linux_wait (char *status
)
875 struct thread_info
*child
= NULL
;
878 /* If we were only supposed to resume one thread, only wait for
879 that thread - if it's still alive. If it died, however - which
880 can happen if we're coming from the thread death case below -
881 then we need to make sure we restart the other threads. We could
882 pick a thread at random or restart all; restarting all is less
884 if (cont_thread
!= 0 && cont_thread
!= -1)
886 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
889 /* No stepping, no signal - unless one is pending already, of course. */
892 struct thread_resume resume_info
;
893 resume_info
.thread
= -1;
894 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
895 linux_resume (&resume_info
);
901 w
= linux_wait_for_event (child
);
902 stop_all_processes ();
905 if (must_set_ptrace_flags
)
907 ptrace (PTRACE_SETOPTIONS
, inferior_pid
, 0, PTRACE_O_TRACECLONE
);
908 must_set_ptrace_flags
= 0;
911 /* If we are waiting for a particular child, and it exited,
912 linux_wait_for_event will return its exit status. Similarly if
913 the last child exited. If this is not the last child, however,
914 do not report it as exited until there is a 'thread exited' response
915 available in the remote protocol. Instead, just wait for another event.
916 This should be safe, because if the thread crashed we will already
917 have reported the termination signal to GDB; that should stop any
918 in-progress stepping operations, etc.
920 Report the exit status of the last thread to exit. This matches
921 LinuxThreads' behavior. */
923 if (all_threads
.head
== all_threads
.tail
)
927 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
930 free (all_processes
.head
);
931 all_processes
.head
= all_processes
.tail
= NULL
;
932 return WEXITSTATUS (w
);
934 else if (!WIFSTOPPED (w
))
936 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
939 free (all_processes
.head
);
940 all_processes
.head
= all_processes
.tail
= NULL
;
941 return target_signal_from_host (WTERMSIG (w
));
951 return target_signal_from_host (WSTOPSIG (w
));
954 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
955 thread groups are in use, we need to use tkill. */
958 kill_lwp (unsigned long lwpid
, int signo
)
960 static int tkill_failed
;
967 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
975 return kill (lwpid
, signo
);
979 send_sigstop (struct inferior_list_entry
*entry
)
981 struct process_info
*process
= (struct process_info
*) entry
;
983 if (process
->stopped
)
986 /* If we already have a pending stop signal for this process, don't
988 if (process
->stop_expected
)
991 fprintf (stderr
, "Have pending sigstop for process %ld\n",
994 /* We clear the stop_expected flag so that wait_for_sigstop
995 will receive the SIGSTOP event (instead of silently resuming and
996 waiting again). It'll be reset below. */
997 process
->stop_expected
= 0;
1002 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
1004 kill_lwp (process
->head
.id
, SIGSTOP
);
1008 wait_for_sigstop (struct inferior_list_entry
*entry
)
1010 struct process_info
*process
= (struct process_info
*) entry
;
1011 struct thread_info
*saved_inferior
, *thread
;
1013 unsigned long saved_tid
;
1015 if (process
->stopped
)
1018 saved_inferior
= current_inferior
;
1019 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
1020 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
1022 wstat
= linux_wait_for_event (thread
);
1024 /* If we stopped with a non-SIGSTOP signal, save it for later
1025 and record the pending SIGSTOP. If the process exited, just
1027 if (WIFSTOPPED (wstat
)
1028 && WSTOPSIG (wstat
) != SIGSTOP
)
1031 fprintf (stderr
, "LWP %ld stopped with non-sigstop status %06x\n",
1032 process
->lwpid
, wstat
);
1033 process
->status_pending_p
= 1;
1034 process
->status_pending
= wstat
;
1035 process
->stop_expected
= 1;
1038 if (linux_thread_alive (saved_tid
))
1039 current_inferior
= saved_inferior
;
1043 fprintf (stderr
, "Previously current thread died.\n");
1045 /* Set a valid thread as current. */
1046 set_desired_inferior (0);
1051 stop_all_processes (void)
1053 stopping_threads
= 1;
1054 for_each_inferior (&all_processes
, send_sigstop
);
1055 for_each_inferior (&all_processes
, wait_for_sigstop
);
1056 stopping_threads
= 0;
1059 /* Resume execution of the inferior process.
1060 If STEP is nonzero, single-step it.
1061 If SIGNAL is nonzero, give it that signal. */
1064 linux_resume_one_process (struct inferior_list_entry
*entry
,
1065 int step
, int signal
, siginfo_t
*info
)
1067 struct process_info
*process
= (struct process_info
*) entry
;
1068 struct thread_info
*saved_inferior
;
1070 if (process
->stopped
== 0)
1073 /* If we have pending signals or status, and a new signal, enqueue the
1074 signal. Also enqueue the signal if we are waiting to reinsert a
1075 breakpoint; it will be picked up again below. */
1077 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
1078 || process
->bp_reinsert
!= 0))
1080 struct pending_signals
*p_sig
;
1081 p_sig
= malloc (sizeof (*p_sig
));
1082 p_sig
->prev
= process
->pending_signals
;
1083 p_sig
->signal
= signal
;
1085 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1087 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
1088 process
->pending_signals
= p_sig
;
1091 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
1094 saved_inferior
= current_inferior
;
1095 current_inferior
= get_process_thread (process
);
1098 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
1099 step
? "step" : "continue", signal
,
1100 process
->stop_expected
? "expected" : "not expected");
1102 /* This bit needs some thinking about. If we get a signal that
1103 we must report while a single-step reinsert is still pending,
1104 we often end up resuming the thread. It might be better to
1105 (ew) allow a stack of pending events; then we could be sure that
1106 the reinsert happened right away and not lose any signals.
1108 Making this stack would also shrink the window in which breakpoints are
1109 uninserted (see comment in linux_wait_for_process) but not enough for
1110 complete correctness, so it won't solve that problem. It may be
1111 worthwhile just to solve this one, however. */
1112 if (process
->bp_reinsert
!= 0)
1115 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
1117 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
1120 /* Postpone any pending signal. It was enqueued above. */
1124 check_removed_breakpoint (process
);
1126 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
1128 fprintf (stderr
, " ");
1129 (*the_low_target
.get_pc
) ();
1132 /* If we have pending signals, consume one unless we are trying to reinsert
1134 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
1136 struct pending_signals
**p_sig
;
1138 p_sig
= &process
->pending_signals
;
1139 while ((*p_sig
)->prev
!= NULL
)
1140 p_sig
= &(*p_sig
)->prev
;
1142 signal
= (*p_sig
)->signal
;
1143 if ((*p_sig
)->info
.si_signo
!= 0)
1144 ptrace (PTRACE_SETSIGINFO
, process
->lwpid
, 0, &(*p_sig
)->info
);
1150 regcache_invalidate_one ((struct inferior_list_entry
*)
1151 get_process_thread (process
));
1153 process
->stopped
= 0;
1154 process
->stepping
= step
;
1155 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
1157 current_inferior
= saved_inferior
;
1159 perror_with_name ("ptrace");
1162 static struct thread_resume
*resume_ptr
;
1164 /* This function is called once per thread. We look up the thread
1165 in RESUME_PTR, and mark the thread with a pointer to the appropriate
1168 This algorithm is O(threads * resume elements), but resume elements
1169 is small (and will remain small at least until GDB supports thread
1172 linux_set_resume_request (struct inferior_list_entry
*entry
)
1174 struct process_info
*process
;
1175 struct thread_info
*thread
;
1178 thread
= (struct thread_info
*) entry
;
1179 process
= get_thread_process (thread
);
1182 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
1185 process
->resume
= &resume_ptr
[ndx
];
1188 /* This function is called once per thread. We check the thread's resume
1189 request, which will tell us whether to resume, step, or leave the thread
1190 stopped; and what signal, if any, it should be sent. For threads which
1191 we aren't explicitly told otherwise, we preserve the stepping flag; this
1192 is used for stepping over gdbserver-placed breakpoints. */
1195 linux_continue_one_thread (struct inferior_list_entry
*entry
)
1197 struct process_info
*process
;
1198 struct thread_info
*thread
;
1201 thread
= (struct thread_info
*) entry
;
1202 process
= get_thread_process (thread
);
1204 if (process
->resume
->leave_stopped
)
1207 if (process
->resume
->thread
== -1)
1208 step
= process
->stepping
|| process
->resume
->step
;
1210 step
= process
->resume
->step
;
1212 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
, NULL
);
1214 process
->resume
= NULL
;
1217 /* This function is called once per thread. We check the thread's resume
1218 request, which will tell us whether to resume, step, or leave the thread
1219 stopped; and what signal, if any, it should be sent. We queue any needed
1220 signals, since we won't actually resume. We already have a pending event
1221 to report, so we don't need to preserve any step requests; they should
1222 be re-issued if necessary. */
1225 linux_queue_one_thread (struct inferior_list_entry
*entry
)
1227 struct process_info
*process
;
1228 struct thread_info
*thread
;
1230 thread
= (struct thread_info
*) entry
;
1231 process
= get_thread_process (thread
);
1233 if (process
->resume
->leave_stopped
)
1236 /* If we have a new signal, enqueue the signal. */
1237 if (process
->resume
->sig
!= 0)
1239 struct pending_signals
*p_sig
;
1240 p_sig
= malloc (sizeof (*p_sig
));
1241 p_sig
->prev
= process
->pending_signals
;
1242 p_sig
->signal
= process
->resume
->sig
;
1243 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1245 /* If this is the same signal we were previously stopped by,
1246 make sure to queue its siginfo. We can ignore the return
1247 value of ptrace; if it fails, we'll skip
1248 PTRACE_SETSIGINFO. */
1249 if (WIFSTOPPED (process
->last_status
)
1250 && WSTOPSIG (process
->last_status
) == process
->resume
->sig
)
1251 ptrace (PTRACE_GETSIGINFO
, process
->lwpid
, 0, &p_sig
->info
);
1253 process
->pending_signals
= p_sig
;
1256 process
->resume
= NULL
;
1259 /* Set DUMMY if this process has an interesting status pending. */
1261 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1263 struct process_info
*process
= (struct process_info
*) entry
;
1265 /* Processes which will not be resumed are not interesting, because
1266 we might not wait for them next time through linux_wait. */
1267 if (process
->resume
->leave_stopped
)
1270 /* If this thread has a removed breakpoint, we won't have any
1271 events to report later, so check now. check_removed_breakpoint
1272 may clear status_pending_p. We avoid calling check_removed_breakpoint
1273 for any thread that we are not otherwise going to resume - this
1274 lets us preserve stopped status when two threads hit a breakpoint.
1275 GDB removes the breakpoint to single-step a particular thread
1276 past it, then re-inserts it and resumes all threads. We want
1277 to report the second thread without resuming it in the interim. */
1278 if (process
->status_pending_p
)
1279 check_removed_breakpoint (process
);
1281 if (process
->status_pending_p
)
1282 * (int *) flag_p
= 1;
1288 linux_resume (struct thread_resume
*resume_info
)
1292 /* Yes, the use of a global here is rather ugly. */
1293 resume_ptr
= resume_info
;
1295 for_each_inferior (&all_threads
, linux_set_resume_request
);
1297 /* If there is a thread which would otherwise be resumed, which
1298 has a pending status, then don't resume any threads - we can just
1299 report the pending status. Make sure to queue any signals
1300 that would otherwise be sent. */
1302 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1307 fprintf (stderr
, "Not resuming, pending status\n");
1309 fprintf (stderr
, "Resuming, no pending status\n");
1313 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1318 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1322 #ifdef HAVE_LINUX_USRREGS
1325 register_addr (int regnum
)
1329 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1330 error ("Invalid register number %d.", regnum
);
1332 addr
= the_low_target
.regmap
[regnum
];
1337 /* Fetch one register. */
1339 fetch_register (int regno
)
1345 if (regno
>= the_low_target
.num_regs
)
1347 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1350 regaddr
= register_addr (regno
);
1353 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1354 & - sizeof (PTRACE_XFER_TYPE
);
1355 buf
= alloca (size
);
1356 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1359 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1360 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1361 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1364 /* Warning, not error, in case we are attached; sometimes the
1365 kernel doesn't let us at the registers. */
1366 char *err
= strerror (errno
);
1367 char *msg
= alloca (strlen (err
) + 128);
1368 sprintf (msg
, "reading register %d: %s", regno
, err
);
1373 if (the_low_target
.left_pad_xfer
1374 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1375 supply_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1376 - register_size (regno
)));
1378 supply_register (regno
, buf
);
1383 /* Fetch all registers, or just one, from the child process. */
1385 usr_fetch_inferior_registers (int regno
)
1387 if (regno
== -1 || regno
== 0)
1388 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1389 fetch_register (regno
);
1391 fetch_register (regno
);
1394 /* Store our register values back into the inferior.
1395 If REGNO is -1, do this for all registers.
1396 Otherwise, REGNO specifies which register (so we can save time). */
1398 usr_store_inferior_registers (int regno
)
1406 if (regno
>= the_low_target
.num_regs
)
1409 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1412 regaddr
= register_addr (regno
);
1416 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1417 & - sizeof (PTRACE_XFER_TYPE
);
1418 buf
= alloca (size
);
1419 memset (buf
, 0, size
);
1420 if (the_low_target
.left_pad_xfer
1421 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1422 collect_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1423 - register_size (regno
)));
1425 collect_register (regno
, buf
);
1426 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1429 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1430 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1433 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1435 char *err
= strerror (errno
);
1436 char *msg
= alloca (strlen (err
) + 128);
1437 sprintf (msg
, "writing register %d: %s",
1443 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1447 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1448 usr_store_inferior_registers (regno
);
1450 #endif /* HAVE_LINUX_USRREGS */
1454 #ifdef HAVE_LINUX_REGSETS
1457 regsets_fetch_inferior_registers ()
1459 struct regset_info
*regset
;
1460 int saw_general_regs
= 0;
1462 regset
= target_regsets
;
1464 while (regset
->size
>= 0)
1469 if (regset
->size
== 0)
1475 buf
= malloc (regset
->size
);
1476 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1481 /* If we get EIO on the first regset, do not try regsets again.
1482 If we get EIO on a later regset, disable that regset. */
1483 if (regset
== target_regsets
)
1497 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1502 else if (regset
->type
== GENERAL_REGS
)
1503 saw_general_regs
= 1;
1504 regset
->store_function (buf
);
1507 if (saw_general_regs
)
1514 regsets_store_inferior_registers ()
1516 struct regset_info
*regset
;
1517 int saw_general_regs
= 0;
1519 regset
= target_regsets
;
1521 while (regset
->size
>= 0)
1526 if (regset
->size
== 0)
1532 buf
= malloc (regset
->size
);
1534 /* First fill the buffer with the current register set contents,
1535 in case there are any items in the kernel's regset that are
1536 not in gdbserver's regcache. */
1537 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1541 /* Then overlay our cached registers on that. */
1542 regset
->fill_function (buf
);
1544 /* Only now do we write the register set. */
1545 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1552 /* If we get EIO on the first regset, do not try regsets again.
1553 If we get EIO on a later regset, disable that regset. */
1554 if (regset
== target_regsets
)
1567 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1570 else if (regset
->type
== GENERAL_REGS
)
1571 saw_general_regs
= 1;
1575 if (saw_general_regs
)
1582 #endif /* HAVE_LINUX_REGSETS */
1586 linux_fetch_registers (int regno
)
1588 #ifdef HAVE_LINUX_REGSETS
1591 if (regsets_fetch_inferior_registers () == 0)
1595 #ifdef HAVE_LINUX_USRREGS
1596 usr_fetch_inferior_registers (regno
);
1601 linux_store_registers (int regno
)
1603 #ifdef HAVE_LINUX_REGSETS
1606 if (regsets_store_inferior_registers () == 0)
1610 #ifdef HAVE_LINUX_USRREGS
1611 usr_store_inferior_registers (regno
);
1616 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1617 to debugger memory starting at MYADDR. */
1620 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1623 /* Round starting address down to longword boundary. */
1624 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1625 /* Round ending address up; get number of longwords that makes. */
1627 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1628 / sizeof (PTRACE_XFER_TYPE
);
1629 /* Allocate buffer of that many longwords. */
1630 register PTRACE_XFER_TYPE
*buffer
1631 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1635 /* Try using /proc. Don't bother for one word. */
1636 if (len
>= 3 * sizeof (long))
1638 /* We could keep this file open and cache it - possibly one per
1639 thread. That requires some juggling, but is even faster. */
1640 sprintf (filename
, "/proc/%ld/mem", inferior_pid
);
1641 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
1645 /* If pread64 is available, use it. It's faster if the kernel
1646 supports it (only one syscall), and it's 64-bit safe even on
1647 32-bit platforms (for instance, SPARC debugging a SPARC64
1650 if (pread64 (fd
, myaddr
, len
, memaddr
) != len
)
1652 if (lseek (fd
, memaddr
, SEEK_SET
) == -1 || read (fd
, memaddr
, len
) != len
)
1664 /* Read all the longwords */
1665 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1668 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1673 /* Copy appropriate bytes out of the buffer. */
1674 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1679 /* Copy LEN bytes of data from debugger memory at MYADDR
1680 to inferior's memory at MEMADDR.
1681 On failure (cannot write the inferior)
1682 returns the value of errno. */
1685 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
1688 /* Round starting address down to longword boundary. */
1689 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1690 /* Round ending address up; get number of longwords that makes. */
1692 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1693 /* Allocate buffer of that many longwords. */
1694 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1699 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1702 /* Fill start and end extra bytes of buffer with existing memory data. */
1704 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1705 (PTRACE_ARG3_TYPE
) addr
, 0);
1710 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1711 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1712 * sizeof (PTRACE_XFER_TYPE
)),
1716 /* Copy data to be written over corresponding part of buffer */
1718 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1720 /* Write the entire buffer. */
1722 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1725 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1733 static int linux_supports_tracefork_flag
;
1735 /* A helper function for linux_test_for_tracefork, called after fork (). */
1738 linux_tracefork_child (void)
1740 ptrace (PTRACE_TRACEME
, 0, 0, 0);
1741 kill (getpid (), SIGSTOP
);
1746 /* Wrapper function for waitpid which handles EINTR. */
1749 my_waitpid (int pid
, int *status
, int flags
)
1754 ret
= waitpid (pid
, status
, flags
);
1756 while (ret
== -1 && errno
== EINTR
);
1761 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
1762 sure that we can enable the option, and that it had the desired
1766 linux_test_for_tracefork (void)
1768 int child_pid
, ret
, status
;
1771 linux_supports_tracefork_flag
= 0;
1773 child_pid
= fork ();
1774 if (child_pid
== -1)
1775 perror_with_name ("fork");
1778 linux_tracefork_child ();
1780 ret
= my_waitpid (child_pid
, &status
, 0);
1782 perror_with_name ("waitpid");
1783 else if (ret
!= child_pid
)
1784 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
1785 if (! WIFSTOPPED (status
))
1786 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
1788 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0, PTRACE_O_TRACEFORK
);
1791 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
1794 warning ("linux_test_for_tracefork: failed to kill child");
1798 ret
= my_waitpid (child_pid
, &status
, 0);
1799 if (ret
!= child_pid
)
1800 warning ("linux_test_for_tracefork: failed to wait for killed child");
1801 else if (!WIFSIGNALED (status
))
1802 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
1803 "killed child", status
);
1808 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
1810 warning ("linux_test_for_tracefork: failed to resume child");
1812 ret
= my_waitpid (child_pid
, &status
, 0);
1814 if (ret
== child_pid
&& WIFSTOPPED (status
)
1815 && status
>> 16 == PTRACE_EVENT_FORK
)
1818 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
1819 if (ret
== 0 && second_pid
!= 0)
1823 linux_supports_tracefork_flag
= 1;
1824 my_waitpid (second_pid
, &second_status
, 0);
1825 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
1827 warning ("linux_test_for_tracefork: failed to kill second child");
1828 my_waitpid (second_pid
, &status
, 0);
1832 warning ("linux_test_for_tracefork: unexpected result from waitpid "
1833 "(%d, status 0x%x)", ret
, status
);
1837 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
1839 warning ("linux_test_for_tracefork: failed to kill child");
1840 my_waitpid (child_pid
, &status
, 0);
1842 while (WIFSTOPPED (status
));
1847 linux_look_up_symbols (void)
1849 #ifdef USE_THREAD_DB
1850 if (thread_db_active
)
1853 thread_db_active
= thread_db_init (!linux_supports_tracefork_flag
);
1858 linux_request_interrupt (void)
1860 extern unsigned long signal_pid
;
1862 if (cont_thread
!= 0 && cont_thread
!= -1)
1864 struct process_info
*process
;
1866 process
= get_thread_process (current_inferior
);
1867 kill_lwp (process
->lwpid
, SIGINT
);
1870 kill_lwp (signal_pid
, SIGINT
);
1873 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1874 to debugger memory starting at MYADDR. */
1877 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
1879 char filename
[PATH_MAX
];
1882 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1884 fd
= open (filename
, O_RDONLY
);
1888 if (offset
!= (CORE_ADDR
) 0
1889 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1892 n
= read (fd
, myaddr
, len
);
1899 /* These watchpoint related wrapper functions simply pass on the function call
1900 if the target has registered a corresponding function. */
1903 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1905 if (the_low_target
.insert_watchpoint
!= NULL
)
1906 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1908 /* Unsupported (see target.h). */
1913 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1915 if (the_low_target
.remove_watchpoint
!= NULL
)
1916 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1918 /* Unsupported (see target.h). */
1923 linux_stopped_by_watchpoint (void)
1925 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1926 return the_low_target
.stopped_by_watchpoint ();
1932 linux_stopped_data_address (void)
1934 if (the_low_target
.stopped_data_address
!= NULL
)
1935 return the_low_target
.stopped_data_address ();
1940 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
1941 #if defined(__mcoldfire__)
1942 /* These should really be defined in the kernel's ptrace.h header. */
1943 #define PT_TEXT_ADDR 49*4
1944 #define PT_DATA_ADDR 50*4
1945 #define PT_TEXT_END_ADDR 51*4
1948 /* Under uClinux, programs are loaded at non-zero offsets, which we need
1949 to tell gdb about. */
1952 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
1954 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1955 unsigned long text
, text_end
, data
;
1956 int pid
= get_thread_process (current_inferior
)->head
.id
;
1960 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
1961 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
1962 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
1966 /* Both text and data offsets produced at compile-time (and so
1967 used by gdb) are relative to the beginning of the program,
1968 with the data segment immediately following the text segment.
1969 However, the actual runtime layout in memory may put the data
1970 somewhere else, so when we send gdb a data base-address, we
1971 use the real data base address and subtract the compile-time
1972 data base-address from it (which is just the length of the
1973 text segment). BSS immediately follows data in both
1976 *data_p
= data
- (text_end
- text
);
1986 linux_arch_string (void)
1988 return the_low_target
.arch_string
;
1991 static struct target_ops linux_target_ops
= {
1992 linux_create_inferior
,
2000 linux_fetch_registers
,
2001 linux_store_registers
,
2004 linux_look_up_symbols
,
2005 linux_request_interrupt
,
2007 linux_insert_watchpoint
,
2008 linux_remove_watchpoint
,
2009 linux_stopped_by_watchpoint
,
2010 linux_stopped_data_address
,
2011 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
2016 #ifdef USE_THREAD_DB
2017 thread_db_get_tls_address
,
2025 linux_init_signals ()
2027 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
2028 to find what the cancel signal actually is. */
2029 signal (__SIGRTMIN
+1, SIG_IGN
);
2033 initialize_low (void)
2035 thread_db_active
= 0;
2036 set_target_ops (&linux_target_ops
);
2037 set_breakpoint_data (the_low_target
.breakpoint
,
2038 the_low_target
.breakpoint_len
);
2040 linux_init_signals ();
2041 linux_test_for_tracefork ();