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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
23 #include "linux-low.h"
27 #include <sys/param.h>
29 #include <sys/ptrace.h>
32 #include <sys/ioctl.h>
38 #include <sys/syscall.h>
40 #ifndef PTRACE_GETSIGINFO
41 # define PTRACE_GETSIGINFO 0x4202
42 # define PTRACE_SETSIGINFO 0x4203
45 /* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
46 however. This requires changing the ID in place when we go from !using_threads
47 to using_threads, immediately.
49 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
50 the same as the LWP ID. */
52 struct inferior_list all_processes
;
54 /* FIXME this is a bit of a hack, and could be removed. */
57 /* FIXME make into a target method? */
60 static void linux_resume_one_process (struct inferior_list_entry
*entry
,
61 int step
, int signal
, siginfo_t
*info
);
62 static void linux_resume (struct thread_resume
*resume_info
);
63 static void stop_all_processes (void);
64 static int linux_wait_for_event (struct thread_info
*child
);
66 struct pending_signals
70 struct pending_signals
*prev
;
73 #define PTRACE_ARG3_TYPE long
74 #define PTRACE_XFER_TYPE long
76 #ifdef HAVE_LINUX_REGSETS
77 static int use_regsets_p
= 1;
80 #define pid_of(proc) ((proc)->head.id)
82 /* FIXME: Delete eventually. */
83 #define inferior_pid (pid_of (get_thread_process (current_inferior)))
85 /* This function should only be called if the process got a SIGTRAP.
86 The SIGTRAP could mean several things.
88 On i386, where decr_pc_after_break is non-zero:
89 If we were single-stepping this process using PTRACE_SINGLESTEP,
90 we will get only the one SIGTRAP (even if the instruction we
91 stepped over was a breakpoint). The value of $eip will be the
93 If we continue the process using PTRACE_CONT, we will get a
94 SIGTRAP when we hit a breakpoint. The value of $eip will be
95 the instruction after the breakpoint (i.e. needs to be
96 decremented). If we report the SIGTRAP to GDB, we must also
97 report the undecremented PC. If we cancel the SIGTRAP, we
98 must resume at the decremented PC.
100 (Presumably, not yet tested) On a non-decr_pc_after_break machine
101 with hardware or kernel single-step:
102 If we single-step over a breakpoint instruction, our PC will
103 point at the following instruction. If we continue and hit a
104 breakpoint instruction, our PC will point at the breakpoint
110 CORE_ADDR stop_pc
= (*the_low_target
.get_pc
) ();
112 if (get_thread_process (current_inferior
)->stepping
)
115 return stop_pc
- the_low_target
.decr_pc_after_break
;
119 add_process (unsigned long pid
)
121 struct process_info
*process
;
123 process
= (struct process_info
*) malloc (sizeof (*process
));
124 memset (process
, 0, sizeof (*process
));
126 process
->head
.id
= pid
;
128 /* Default to tid == lwpid == pid. */
130 process
->lwpid
= pid
;
132 add_inferior_to_list (&all_processes
, &process
->head
);
137 /* Start an inferior process and returns its pid.
138 ALLARGS is a vector of program-name and args. */
141 linux_create_inferior (char *program
, char **allargs
)
146 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
152 perror_with_name ("fork");
156 ptrace (PTRACE_TRACEME
, 0, 0, 0);
158 signal (__SIGRTMIN
+ 1, SIG_DFL
);
162 execv (program
, allargs
);
164 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
170 new_process
= add_process (pid
);
171 add_thread (pid
, new_process
, pid
);
176 /* Attach to an inferior process. */
179 linux_attach_lwp (unsigned long pid
, unsigned long tid
)
181 struct process_info
*new_process
;
183 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
185 fprintf (stderr
, "Cannot attach to process %ld: %s (%d)\n", pid
,
186 strerror (errno
), errno
);
189 /* If we fail to attach to an LWP, just return. */
195 new_process
= (struct process_info
*) add_process (pid
);
196 add_thread (tid
, new_process
, pid
);
198 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
199 brings it to a halt. We should ignore that SIGSTOP and resume the process
200 (unless this is the first process, in which case the flag will be cleared
203 On the other hand, if we are currently trying to stop all threads, we
204 should treat the new thread as if we had sent it a SIGSTOP. This works
205 because we are guaranteed that add_process added us to the end of the
206 list, and so the new thread has not yet reached wait_for_sigstop (but
208 if (! stopping_threads
)
209 new_process
->stop_expected
= 1;
213 linux_attach (unsigned long pid
)
215 struct process_info
*process
;
217 linux_attach_lwp (pid
, pid
);
219 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
220 process
= (struct process_info
*) find_inferior_id (&all_processes
, pid
);
221 process
->stop_expected
= 0;
226 /* Kill the inferior process. Make us have no inferior. */
229 linux_kill_one_process (struct inferior_list_entry
*entry
)
231 struct thread_info
*thread
= (struct thread_info
*) entry
;
232 struct process_info
*process
= get_thread_process (thread
);
235 /* We avoid killing the first thread here, because of a Linux kernel (at
236 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
237 the children get a chance to be reaped, it will remain a zombie
239 if (entry
== all_threads
.head
)
244 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
246 /* Make sure it died. The loop is most likely unnecessary. */
247 wstat
= linux_wait_for_event (thread
);
248 } while (WIFSTOPPED (wstat
));
254 struct thread_info
*thread
= (struct thread_info
*) all_threads
.head
;
255 struct process_info
*process
;
261 for_each_inferior (&all_threads
, linux_kill_one_process
);
263 /* See the comment in linux_kill_one_process. We did not kill the first
264 thread in the list, so do so now. */
265 process
= get_thread_process (thread
);
268 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
270 /* Make sure it died. The loop is most likely unnecessary. */
271 wstat
= linux_wait_for_event (thread
);
272 } while (WIFSTOPPED (wstat
));
276 linux_detach_one_process (struct inferior_list_entry
*entry
)
278 struct thread_info
*thread
= (struct thread_info
*) entry
;
279 struct process_info
*process
= get_thread_process (thread
);
281 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
287 for_each_inferior (&all_threads
, linux_detach_one_process
);
290 /* Return nonzero if the given thread is still alive. */
292 linux_thread_alive (unsigned long tid
)
294 if (find_inferior_id (&all_threads
, tid
) != NULL
)
300 /* Return nonzero if this process stopped at a breakpoint which
301 no longer appears to be inserted. Also adjust the PC
302 appropriately to resume where the breakpoint used to be. */
304 check_removed_breakpoint (struct process_info
*event_child
)
307 struct thread_info
*saved_inferior
;
309 if (event_child
->pending_is_breakpoint
== 0)
313 fprintf (stderr
, "Checking for breakpoint.\n");
315 saved_inferior
= current_inferior
;
316 current_inferior
= get_process_thread (event_child
);
318 stop_pc
= get_stop_pc ();
320 /* If the PC has changed since we stopped, then we shouldn't do
321 anything. This happens if, for instance, GDB handled the
322 decr_pc_after_break subtraction itself. */
323 if (stop_pc
!= event_child
->pending_stop_pc
)
326 fprintf (stderr
, "Ignoring, PC was changed.\n");
328 event_child
->pending_is_breakpoint
= 0;
329 current_inferior
= saved_inferior
;
333 /* If the breakpoint is still there, we will report hitting it. */
334 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
337 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
338 current_inferior
= saved_inferior
;
343 fprintf (stderr
, "Removed breakpoint.\n");
345 /* For decr_pc_after_break targets, here is where we perform the
346 decrement. We go immediately from this function to resuming,
347 and can not safely call get_stop_pc () again. */
348 if (the_low_target
.set_pc
!= NULL
)
349 (*the_low_target
.set_pc
) (stop_pc
);
351 /* We consumed the pending SIGTRAP. */
352 event_child
->pending_is_breakpoint
= 0;
353 event_child
->status_pending_p
= 0;
354 event_child
->status_pending
= 0;
356 current_inferior
= saved_inferior
;
360 /* Return 1 if this process has an interesting status pending. This function
361 may silently resume an inferior process. */
363 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
365 struct process_info
*process
= (struct process_info
*) entry
;
367 if (process
->status_pending_p
)
368 if (check_removed_breakpoint (process
))
370 /* This thread was stopped at a breakpoint, and the breakpoint
371 is now gone. We were told to continue (or step...) all threads,
372 so GDB isn't trying to single-step past this breakpoint.
373 So instead of reporting the old SIGTRAP, pretend we got to
374 the breakpoint just after it was removed instead of just
375 before; resume the process. */
376 linux_resume_one_process (&process
->head
, 0, 0, NULL
);
380 return process
->status_pending_p
;
384 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
387 int to_wait_for
= -1;
390 to_wait_for
= (*childp
)->lwpid
;
394 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
399 perror_with_name ("waitpid");
404 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
409 perror_with_name ("waitpid (WCLONE)");
418 && (!WIFSTOPPED (*wstatp
)
419 || (WSTOPSIG (*wstatp
) != 32
420 && WSTOPSIG (*wstatp
) != 33)))
421 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
423 if (to_wait_for
== -1)
424 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
426 (*childp
)->stopped
= 1;
427 (*childp
)->pending_is_breakpoint
= 0;
429 (*childp
)->last_status
= *wstatp
;
432 && WIFSTOPPED (*wstatp
))
434 current_inferior
= (struct thread_info
*)
435 find_inferior_id (&all_threads
, (*childp
)->tid
);
436 /* For testing only; i386_stop_pc prints out a diagnostic. */
437 if (the_low_target
.get_pc
!= NULL
)
443 linux_wait_for_event (struct thread_info
*child
)
446 struct process_info
*event_child
;
449 /* Check for a process with a pending status. */
450 /* It is possible that the user changed the pending task's registers since
451 it stopped. We correctly handle the change of PC if we hit a breakpoint
452 (in check_removed_breakpoint); signals should be reported anyway. */
455 event_child
= (struct process_info
*)
456 find_inferior (&all_processes
, status_pending_p
, NULL
);
457 if (debug_threads
&& event_child
)
458 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
462 event_child
= get_thread_process (child
);
463 if (event_child
->status_pending_p
464 && check_removed_breakpoint (event_child
))
468 if (event_child
!= NULL
)
470 if (event_child
->status_pending_p
)
473 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
474 event_child
->lwpid
, event_child
->status_pending
);
475 wstat
= event_child
->status_pending
;
476 event_child
->status_pending_p
= 0;
477 event_child
->status_pending
= 0;
478 current_inferior
= get_process_thread (event_child
);
483 /* We only enter this loop if no process has a pending wait status. Thus
484 any action taken in response to a wait status inside this loop is
485 responding as soon as we detect the status, not after any pending
492 event_child
= get_thread_process (child
);
494 linux_wait_for_process (&event_child
, &wstat
);
496 if (event_child
== NULL
)
497 error ("event from unknown child");
499 current_inferior
= (struct thread_info
*)
500 find_inferior_id (&all_threads
, event_child
->tid
);
502 /* Check for thread exit. */
503 if (using_threads
&& ! WIFSTOPPED (wstat
))
506 fprintf (stderr
, "Thread %ld (LWP %ld) exiting\n",
507 event_child
->tid
, event_child
->head
.id
);
509 /* If the last thread is exiting, just return. */
510 if (all_threads
.head
== all_threads
.tail
)
513 dead_thread_notify (event_child
->tid
);
515 remove_inferior (&all_processes
, &event_child
->head
);
517 remove_thread (current_inferior
);
518 current_inferior
= (struct thread_info
*) all_threads
.head
;
520 /* If we were waiting for this particular child to do something...
521 well, it did something. */
525 /* Wait for a more interesting event. */
530 && WIFSTOPPED (wstat
)
531 && WSTOPSIG (wstat
) == SIGSTOP
532 && event_child
->stop_expected
)
535 fprintf (stderr
, "Expected stop.\n");
536 event_child
->stop_expected
= 0;
537 linux_resume_one_process (&event_child
->head
,
538 event_child
->stepping
, 0, NULL
);
542 /* If GDB is not interested in this signal, don't stop other
543 threads, and don't report it to GDB. Just resume the
544 inferior right away. We do this for threading-related
545 signals as well as any that GDB specifically requested
546 we ignore. But never ignore SIGSTOP if we sent it
548 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
550 if (WIFSTOPPED (wstat
)
551 && ((using_threads
&& (WSTOPSIG (wstat
) == __SIGRTMIN
552 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
553 || (pass_signals
[target_signal_from_host (WSTOPSIG (wstat
))]
554 && (WSTOPSIG (wstat
) != SIGSTOP
555 || !event_child
->sigstop_sent
))))
557 siginfo_t info
, *info_p
;
560 fprintf (stderr
, "Ignored signal %d for %ld (LWP %ld).\n",
561 WSTOPSIG (wstat
), event_child
->tid
,
562 event_child
->head
.id
);
564 if (ptrace (PTRACE_GETSIGINFO
, event_child
->lwpid
, 0, &info
) == 0)
568 linux_resume_one_process (&event_child
->head
,
569 event_child
->stepping
,
570 WSTOPSIG (wstat
), info_p
);
574 /* If this event was not handled above, and is not a SIGTRAP, report
576 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
579 /* If this target does not support breakpoints, we simply report the
580 SIGTRAP; it's of no concern to us. */
581 if (the_low_target
.get_pc
== NULL
)
584 stop_pc
= get_stop_pc ();
586 /* bp_reinsert will only be set if we were single-stepping.
587 Notice that we will resume the process after hitting
588 a gdbserver breakpoint; single-stepping to/over one
589 is not supported (yet). */
590 if (event_child
->bp_reinsert
!= 0)
593 fprintf (stderr
, "Reinserted breakpoint.\n");
594 reinsert_breakpoint (event_child
->bp_reinsert
);
595 event_child
->bp_reinsert
= 0;
597 /* Clear the single-stepping flag and SIGTRAP as we resume. */
598 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
603 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
605 if (check_breakpoints (stop_pc
) != 0)
607 /* We hit one of our own breakpoints. We mark it as a pending
608 breakpoint, so that check_removed_breakpoint () will do the PC
609 adjustment for us at the appropriate time. */
610 event_child
->pending_is_breakpoint
= 1;
611 event_child
->pending_stop_pc
= stop_pc
;
613 /* Now we need to put the breakpoint back. We continue in the event
614 loop instead of simply replacing the breakpoint right away,
615 in order to not lose signals sent to the thread that hit the
616 breakpoint. Unfortunately this increases the window where another
617 thread could sneak past the removed breakpoint. For the current
618 use of server-side breakpoints (thread creation) this is
619 acceptable; but it needs to be considered before this breakpoint
620 mechanism can be used in more general ways. For some breakpoints
621 it may be necessary to stop all other threads, but that should
622 be avoided where possible.
624 If breakpoint_reinsert_addr is NULL, that means that we can
625 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
626 mark it for reinsertion, and single-step.
628 Otherwise, call the target function to figure out where we need
629 our temporary breakpoint, create it, and continue executing this
631 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
633 event_child
->bp_reinsert
= stop_pc
;
634 uninsert_breakpoint (stop_pc
);
635 linux_resume_one_process (&event_child
->head
, 1, 0, NULL
);
639 reinsert_breakpoint_by_bp
640 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
641 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
647 /* If we were single-stepping, we definitely want to report the
648 SIGTRAP. The single-step operation has completed, so also
649 clear the stepping flag; in general this does not matter,
650 because the SIGTRAP will be reported to the client, which
651 will give us a new action for this thread, but clear it for
652 consistency anyway. It's safe to clear the stepping flag
653 because the only consumer of get_stop_pc () after this point
654 is check_removed_breakpoint, and pending_is_breakpoint is not
655 set. It might be wiser to use a step_completed flag instead. */
656 if (event_child
->stepping
)
658 event_child
->stepping
= 0;
662 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
663 Check if it is a breakpoint, and if so mark the process information
664 accordingly. This will handle both the necessary fiddling with the
665 PC on decr_pc_after_break targets and suppressing extra threads
666 hitting a breakpoint if two hit it at once and then GDB removes it
667 after the first is reported. Arguably it would be better to report
668 multiple threads hitting breakpoints simultaneously, but the current
669 remote protocol does not allow this. */
670 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
672 event_child
->pending_is_breakpoint
= 1;
673 event_child
->pending_stop_pc
= stop_pc
;
683 /* Wait for process, returns status. */
686 linux_wait (char *status
)
689 struct thread_info
*child
= NULL
;
692 /* If we were only supposed to resume one thread, only wait for
693 that thread - if it's still alive. If it died, however - which
694 can happen if we're coming from the thread death case below -
695 then we need to make sure we restart the other threads. We could
696 pick a thread at random or restart all; restarting all is less
698 if (cont_thread
!= 0 && cont_thread
!= -1)
700 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
703 /* No stepping, no signal - unless one is pending already, of course. */
706 struct thread_resume resume_info
;
707 resume_info
.thread
= -1;
708 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
709 linux_resume (&resume_info
);
715 w
= linux_wait_for_event (child
);
716 stop_all_processes ();
719 /* If we are waiting for a particular child, and it exited,
720 linux_wait_for_event will return its exit status. Similarly if
721 the last child exited. If this is not the last child, however,
722 do not report it as exited until there is a 'thread exited' response
723 available in the remote protocol. Instead, just wait for another event.
724 This should be safe, because if the thread crashed we will already
725 have reported the termination signal to GDB; that should stop any
726 in-progress stepping operations, etc.
728 Report the exit status of the last thread to exit. This matches
729 LinuxThreads' behavior. */
731 if (all_threads
.head
== all_threads
.tail
)
735 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
738 free (all_processes
.head
);
739 all_processes
.head
= all_processes
.tail
= NULL
;
740 return WEXITSTATUS (w
);
742 else if (!WIFSTOPPED (w
))
744 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
747 free (all_processes
.head
);
748 all_processes
.head
= all_processes
.tail
= NULL
;
749 return target_signal_from_host (WTERMSIG (w
));
759 return target_signal_from_host (WSTOPSIG (w
));
762 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
763 thread groups are in use, we need to use tkill. */
766 kill_lwp (unsigned long lwpid
, int signo
)
768 static int tkill_failed
;
775 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
783 return kill (lwpid
, signo
);
787 send_sigstop (struct inferior_list_entry
*entry
)
789 struct process_info
*process
= (struct process_info
*) entry
;
791 if (process
->stopped
)
794 /* If we already have a pending stop signal for this process, don't
796 if (process
->stop_expected
)
798 process
->stop_expected
= 0;
803 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
805 kill_lwp (process
->head
.id
, SIGSTOP
);
806 process
->sigstop_sent
= 1;
810 wait_for_sigstop (struct inferior_list_entry
*entry
)
812 struct process_info
*process
= (struct process_info
*) entry
;
813 struct thread_info
*saved_inferior
, *thread
;
815 unsigned long saved_tid
;
817 if (process
->stopped
)
820 saved_inferior
= current_inferior
;
821 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
822 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
824 wstat
= linux_wait_for_event (thread
);
826 /* If we stopped with a non-SIGSTOP signal, save it for later
827 and record the pending SIGSTOP. If the process exited, just
829 if (WIFSTOPPED (wstat
)
830 && WSTOPSIG (wstat
) != SIGSTOP
)
833 fprintf (stderr
, "Stopped with non-sigstop signal\n");
834 process
->status_pending_p
= 1;
835 process
->status_pending
= wstat
;
836 process
->stop_expected
= 1;
839 if (linux_thread_alive (saved_tid
))
840 current_inferior
= saved_inferior
;
844 fprintf (stderr
, "Previously current thread died.\n");
846 /* Set a valid thread as current. */
847 set_desired_inferior (0);
852 stop_all_processes (void)
854 stopping_threads
= 1;
855 for_each_inferior (&all_processes
, send_sigstop
);
856 for_each_inferior (&all_processes
, wait_for_sigstop
);
857 stopping_threads
= 0;
860 /* Resume execution of the inferior process.
861 If STEP is nonzero, single-step it.
862 If SIGNAL is nonzero, give it that signal. */
865 linux_resume_one_process (struct inferior_list_entry
*entry
,
866 int step
, int signal
, siginfo_t
*info
)
868 struct process_info
*process
= (struct process_info
*) entry
;
869 struct thread_info
*saved_inferior
;
871 if (process
->stopped
== 0)
874 /* If we have pending signals or status, and a new signal, enqueue the
875 signal. Also enqueue the signal if we are waiting to reinsert a
876 breakpoint; it will be picked up again below. */
878 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
879 || process
->bp_reinsert
!= 0))
881 struct pending_signals
*p_sig
;
882 p_sig
= malloc (sizeof (*p_sig
));
883 p_sig
->prev
= process
->pending_signals
;
884 p_sig
->signal
= signal
;
886 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
888 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
889 process
->pending_signals
= p_sig
;
892 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
895 saved_inferior
= current_inferior
;
896 current_inferior
= get_process_thread (process
);
899 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
900 step
? "step" : "continue", signal
,
901 process
->stop_expected
? "expected" : "not expected");
903 /* This bit needs some thinking about. If we get a signal that
904 we must report while a single-step reinsert is still pending,
905 we often end up resuming the thread. It might be better to
906 (ew) allow a stack of pending events; then we could be sure that
907 the reinsert happened right away and not lose any signals.
909 Making this stack would also shrink the window in which breakpoints are
910 uninserted (see comment in linux_wait_for_process) but not enough for
911 complete correctness, so it won't solve that problem. It may be
912 worthwhile just to solve this one, however. */
913 if (process
->bp_reinsert
!= 0)
916 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
918 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
921 /* Postpone any pending signal. It was enqueued above. */
925 check_removed_breakpoint (process
);
927 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
929 fprintf (stderr
, " ");
930 (*the_low_target
.get_pc
) ();
933 /* If we have pending signals, consume one unless we are trying to reinsert
935 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
937 struct pending_signals
**p_sig
;
939 p_sig
= &process
->pending_signals
;
940 while ((*p_sig
)->prev
!= NULL
)
941 p_sig
= &(*p_sig
)->prev
;
943 signal
= (*p_sig
)->signal
;
944 if ((*p_sig
)->info
.si_signo
!= 0)
945 ptrace (PTRACE_SETSIGINFO
, process
->lwpid
, 0, &(*p_sig
)->info
);
951 regcache_invalidate_one ((struct inferior_list_entry
*)
952 get_process_thread (process
));
954 process
->stopped
= 0;
955 process
->stepping
= step
;
956 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
958 current_inferior
= saved_inferior
;
960 perror_with_name ("ptrace");
963 static struct thread_resume
*resume_ptr
;
965 /* This function is called once per thread. We look up the thread
966 in RESUME_PTR, and mark the thread with a pointer to the appropriate
969 This algorithm is O(threads * resume elements), but resume elements
970 is small (and will remain small at least until GDB supports thread
973 linux_set_resume_request (struct inferior_list_entry
*entry
)
975 struct process_info
*process
;
976 struct thread_info
*thread
;
979 thread
= (struct thread_info
*) entry
;
980 process
= get_thread_process (thread
);
983 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
986 process
->resume
= &resume_ptr
[ndx
];
989 /* This function is called once per thread. We check the thread's resume
990 request, which will tell us whether to resume, step, or leave the thread
991 stopped; and what signal, if any, it should be sent. For threads which
992 we aren't explicitly told otherwise, we preserve the stepping flag; this
993 is used for stepping over gdbserver-placed breakpoints. */
996 linux_continue_one_thread (struct inferior_list_entry
*entry
)
998 struct process_info
*process
;
999 struct thread_info
*thread
;
1002 thread
= (struct thread_info
*) entry
;
1003 process
= get_thread_process (thread
);
1005 if (process
->resume
->leave_stopped
)
1008 if (process
->resume
->thread
== -1)
1009 step
= process
->stepping
|| process
->resume
->step
;
1011 step
= process
->resume
->step
;
1013 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
, NULL
);
1015 process
->resume
= NULL
;
1018 /* This function is called once per thread. We check the thread's resume
1019 request, which will tell us whether to resume, step, or leave the thread
1020 stopped; and what signal, if any, it should be sent. We queue any needed
1021 signals, since we won't actually resume. We already have a pending event
1022 to report, so we don't need to preserve any step requests; they should
1023 be re-issued if necessary. */
1026 linux_queue_one_thread (struct inferior_list_entry
*entry
)
1028 struct process_info
*process
;
1029 struct thread_info
*thread
;
1031 thread
= (struct thread_info
*) entry
;
1032 process
= get_thread_process (thread
);
1034 if (process
->resume
->leave_stopped
)
1037 /* If we have a new signal, enqueue the signal. */
1038 if (process
->resume
->sig
!= 0)
1040 struct pending_signals
*p_sig
;
1041 p_sig
= malloc (sizeof (*p_sig
));
1042 p_sig
->prev
= process
->pending_signals
;
1043 p_sig
->signal
= process
->resume
->sig
;
1044 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1046 /* If this is the same signal we were previously stopped by,
1047 make sure to queue its siginfo. We can ignore the return
1048 value of ptrace; if it fails, we'll skip
1049 PTRACE_SETSIGINFO. */
1050 if (WIFSTOPPED (process
->last_status
)
1051 && WSTOPSIG (process
->last_status
) == process
->resume
->sig
)
1052 ptrace (PTRACE_GETSIGINFO
, process
->lwpid
, 0, &p_sig
->info
);
1054 process
->pending_signals
= p_sig
;
1057 process
->resume
= NULL
;
1060 /* Set DUMMY if this process has an interesting status pending. */
1062 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1064 struct process_info
*process
= (struct process_info
*) entry
;
1066 /* Processes which will not be resumed are not interesting, because
1067 we might not wait for them next time through linux_wait. */
1068 if (process
->resume
->leave_stopped
)
1071 /* If this thread has a removed breakpoint, we won't have any
1072 events to report later, so check now. check_removed_breakpoint
1073 may clear status_pending_p. We avoid calling check_removed_breakpoint
1074 for any thread that we are not otherwise going to resume - this
1075 lets us preserve stopped status when two threads hit a breakpoint.
1076 GDB removes the breakpoint to single-step a particular thread
1077 past it, then re-inserts it and resumes all threads. We want
1078 to report the second thread without resuming it in the interim. */
1079 if (process
->status_pending_p
)
1080 check_removed_breakpoint (process
);
1082 if (process
->status_pending_p
)
1083 * (int *) flag_p
= 1;
1089 linux_resume (struct thread_resume
*resume_info
)
1093 /* Yes, the use of a global here is rather ugly. */
1094 resume_ptr
= resume_info
;
1096 for_each_inferior (&all_threads
, linux_set_resume_request
);
1098 /* If there is a thread which would otherwise be resumed, which
1099 has a pending status, then don't resume any threads - we can just
1100 report the pending status. Make sure to queue any signals
1101 that would otherwise be sent. */
1103 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1108 fprintf (stderr
, "Not resuming, pending status\n");
1110 fprintf (stderr
, "Resuming, no pending status\n");
1114 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1119 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1123 #ifdef HAVE_LINUX_USRREGS
1126 register_addr (int regnum
)
1130 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1131 error ("Invalid register number %d.", regnum
);
1133 addr
= the_low_target
.regmap
[regnum
];
1138 /* Fetch one register. */
1140 fetch_register (int regno
)
1146 if (regno
>= the_low_target
.num_regs
)
1148 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1151 regaddr
= register_addr (regno
);
1154 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1155 & - sizeof (PTRACE_XFER_TYPE
);
1156 buf
= alloca (size
);
1157 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1160 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1161 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1162 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1165 /* Warning, not error, in case we are attached; sometimes the
1166 kernel doesn't let us at the registers. */
1167 char *err
= strerror (errno
);
1168 char *msg
= alloca (strlen (err
) + 128);
1169 sprintf (msg
, "reading register %d: %s", regno
, err
);
1174 if (the_low_target
.left_pad_xfer
1175 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1176 supply_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1177 - register_size (regno
)));
1179 supply_register (regno
, buf
);
1184 /* Fetch all registers, or just one, from the child process. */
1186 usr_fetch_inferior_registers (int regno
)
1188 if (regno
== -1 || regno
== 0)
1189 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1190 fetch_register (regno
);
1192 fetch_register (regno
);
1195 /* Store our register values back into the inferior.
1196 If REGNO is -1, do this for all registers.
1197 Otherwise, REGNO specifies which register (so we can save time). */
1199 usr_store_inferior_registers (int regno
)
1207 if (regno
>= the_low_target
.num_regs
)
1210 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1213 regaddr
= register_addr (regno
);
1217 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1218 & - sizeof (PTRACE_XFER_TYPE
);
1219 buf
= alloca (size
);
1220 memset (buf
, 0, size
);
1221 if (the_low_target
.left_pad_xfer
1222 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1223 collect_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1224 - register_size (regno
)));
1226 collect_register (regno
, buf
);
1227 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1230 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1231 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1234 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1236 char *err
= strerror (errno
);
1237 char *msg
= alloca (strlen (err
) + 128);
1238 sprintf (msg
, "writing register %d: %s",
1244 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1248 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1249 usr_store_inferior_registers (regno
);
1251 #endif /* HAVE_LINUX_USRREGS */
1255 #ifdef HAVE_LINUX_REGSETS
1258 regsets_fetch_inferior_registers ()
1260 struct regset_info
*regset
;
1261 int saw_general_regs
= 0;
1263 regset
= target_regsets
;
1265 while (regset
->size
>= 0)
1270 if (regset
->size
== 0)
1276 buf
= malloc (regset
->size
);
1277 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1282 /* If we get EIO on the first regset, do not try regsets again.
1283 If we get EIO on a later regset, disable that regset. */
1284 if (regset
== target_regsets
)
1298 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1303 else if (regset
->type
== GENERAL_REGS
)
1304 saw_general_regs
= 1;
1305 regset
->store_function (buf
);
1308 if (saw_general_regs
)
1315 regsets_store_inferior_registers ()
1317 struct regset_info
*regset
;
1318 int saw_general_regs
= 0;
1320 regset
= target_regsets
;
1322 while (regset
->size
>= 0)
1327 if (regset
->size
== 0)
1333 buf
= malloc (regset
->size
);
1335 /* First fill the buffer with the current register set contents,
1336 in case there are any items in the kernel's regset that are
1337 not in gdbserver's regcache. */
1338 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1342 /* Then overlay our cached registers on that. */
1343 regset
->fill_function (buf
);
1345 /* Only now do we write the register set. */
1346 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1353 /* If we get EIO on the first regset, do not try regsets again.
1354 If we get EIO on a later regset, disable that regset. */
1355 if (regset
== target_regsets
)
1368 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1371 else if (regset
->type
== GENERAL_REGS
)
1372 saw_general_regs
= 1;
1376 if (saw_general_regs
)
1383 #endif /* HAVE_LINUX_REGSETS */
1387 linux_fetch_registers (int regno
)
1389 #ifdef HAVE_LINUX_REGSETS
1392 if (regsets_fetch_inferior_registers () == 0)
1396 #ifdef HAVE_LINUX_USRREGS
1397 usr_fetch_inferior_registers (regno
);
1402 linux_store_registers (int regno
)
1404 #ifdef HAVE_LINUX_REGSETS
1407 if (regsets_store_inferior_registers () == 0)
1411 #ifdef HAVE_LINUX_USRREGS
1412 usr_store_inferior_registers (regno
);
1417 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1418 to debugger memory starting at MYADDR. */
1421 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1424 /* Round starting address down to longword boundary. */
1425 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1426 /* Round ending address up; get number of longwords that makes. */
1428 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1429 / sizeof (PTRACE_XFER_TYPE
);
1430 /* Allocate buffer of that many longwords. */
1431 register PTRACE_XFER_TYPE
*buffer
1432 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1434 /* Read all the longwords */
1435 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1438 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1443 /* Copy appropriate bytes out of the buffer. */
1444 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1449 /* Copy LEN bytes of data from debugger memory at MYADDR
1450 to inferior's memory at MEMADDR.
1451 On failure (cannot write the inferior)
1452 returns the value of errno. */
1455 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
1458 /* Round starting address down to longword boundary. */
1459 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1460 /* Round ending address up; get number of longwords that makes. */
1462 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1463 /* Allocate buffer of that many longwords. */
1464 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1469 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1472 /* Fill start and end extra bytes of buffer with existing memory data. */
1474 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1475 (PTRACE_ARG3_TYPE
) addr
, 0);
1480 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1481 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1482 * sizeof (PTRACE_XFER_TYPE
)),
1486 /* Copy data to be written over corresponding part of buffer */
1488 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1490 /* Write the entire buffer. */
1492 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1495 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1504 linux_look_up_symbols (void)
1506 #ifdef USE_THREAD_DB
1510 using_threads
= thread_db_init ();
1515 linux_request_interrupt (void)
1517 extern unsigned long signal_pid
;
1519 if (cont_thread
!= 0 && cont_thread
!= -1)
1521 struct process_info
*process
;
1523 process
= get_thread_process (current_inferior
);
1524 kill_lwp (process
->lwpid
, SIGINT
);
1527 kill_lwp (signal_pid
, SIGINT
);
1530 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1531 to debugger memory starting at MYADDR. */
1534 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
1536 char filename
[PATH_MAX
];
1539 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1541 fd
= open (filename
, O_RDONLY
);
1545 if (offset
!= (CORE_ADDR
) 0
1546 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1549 n
= read (fd
, myaddr
, len
);
1556 /* These watchpoint related wrapper functions simply pass on the function call
1557 if the target has registered a corresponding function. */
1560 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1562 if (the_low_target
.insert_watchpoint
!= NULL
)
1563 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1565 /* Unsupported (see target.h). */
1570 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1572 if (the_low_target
.remove_watchpoint
!= NULL
)
1573 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1575 /* Unsupported (see target.h). */
1580 linux_stopped_by_watchpoint (void)
1582 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1583 return the_low_target
.stopped_by_watchpoint ();
1589 linux_stopped_data_address (void)
1591 if (the_low_target
.stopped_data_address
!= NULL
)
1592 return the_low_target
.stopped_data_address ();
1597 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
1598 #if defined(__mcoldfire__)
1599 /* These should really be defined in the kernel's ptrace.h header. */
1600 #define PT_TEXT_ADDR 49*4
1601 #define PT_DATA_ADDR 50*4
1602 #define PT_TEXT_END_ADDR 51*4
1605 /* Under uClinux, programs are loaded at non-zero offsets, which we need
1606 to tell gdb about. */
1609 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
1611 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1612 unsigned long text
, text_end
, data
;
1613 int pid
= get_thread_process (current_inferior
)->head
.id
;
1617 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
1618 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
1619 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
1623 /* Both text and data offsets produced at compile-time (and so
1624 used by gdb) are relative to the beginning of the program,
1625 with the data segment immediately following the text segment.
1626 However, the actual runtime layout in memory may put the data
1627 somewhere else, so when we send gdb a data base-address, we
1628 use the real data base address and subtract the compile-time
1629 data base-address from it (which is just the length of the
1630 text segment). BSS immediately follows data in both
1633 *data_p
= data
- (text_end
- text
);
1643 linux_arch_string (void)
1645 return the_low_target
.arch_string
;
1648 static struct target_ops linux_target_ops
= {
1649 linux_create_inferior
,
1656 linux_fetch_registers
,
1657 linux_store_registers
,
1660 linux_look_up_symbols
,
1661 linux_request_interrupt
,
1663 linux_insert_watchpoint
,
1664 linux_remove_watchpoint
,
1665 linux_stopped_by_watchpoint
,
1666 linux_stopped_data_address
,
1667 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
1672 #ifdef USE_THREAD_DB
1673 thread_db_get_tls_address
,
1681 linux_init_signals ()
1683 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1684 to find what the cancel signal actually is. */
1685 signal (__SIGRTMIN
+1, SIG_IGN
);
1689 initialize_low (void)
1692 set_target_ops (&linux_target_ops
);
1693 set_breakpoint_data (the_low_target
.breakpoint
,
1694 the_low_target
.breakpoint_len
);
1696 linux_init_signals ();