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,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "linux-low.h"
28 #include <sys/param.h>
30 #include <sys/ptrace.h>
33 #include <sys/ioctl.h>
39 #include <sys/syscall.h>
41 #ifndef PTRACE_GETSIGINFO
42 # define PTRACE_GETSIGINFO 0x4202
43 # define PTRACE_SETSIGINFO 0x4203
46 /* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
47 however. This requires changing the ID in place when we go from !using_threads
48 to using_threads, immediately.
50 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
51 the same as the LWP ID. */
53 struct inferior_list all_processes
;
55 /* FIXME this is a bit of a hack, and could be removed. */
58 /* FIXME make into a target method? */
61 static void linux_resume_one_process (struct inferior_list_entry
*entry
,
62 int step
, int signal
, siginfo_t
*info
);
63 static void linux_resume (struct thread_resume
*resume_info
);
64 static void stop_all_processes (void);
65 static int linux_wait_for_event (struct thread_info
*child
);
67 struct pending_signals
71 struct pending_signals
*prev
;
74 #define PTRACE_ARG3_TYPE long
75 #define PTRACE_XFER_TYPE long
77 #ifdef HAVE_LINUX_REGSETS
78 static int use_regsets_p
= 1;
81 int debug_threads
= 0;
83 #define pid_of(proc) ((proc)->head.id)
85 /* FIXME: Delete eventually. */
86 #define inferior_pid (pid_of (get_thread_process (current_inferior)))
88 /* This function should only be called if the process got a SIGTRAP.
89 The SIGTRAP could mean several things.
91 On i386, where decr_pc_after_break is non-zero:
92 If we were single-stepping this process using PTRACE_SINGLESTEP,
93 we will get only the one SIGTRAP (even if the instruction we
94 stepped over was a breakpoint). The value of $eip will be the
96 If we continue the process using PTRACE_CONT, we will get a
97 SIGTRAP when we hit a breakpoint. The value of $eip will be
98 the instruction after the breakpoint (i.e. needs to be
99 decremented). If we report the SIGTRAP to GDB, we must also
100 report the undecremented PC. If we cancel the SIGTRAP, we
101 must resume at the decremented PC.
103 (Presumably, not yet tested) On a non-decr_pc_after_break machine
104 with hardware or kernel single-step:
105 If we single-step over a breakpoint instruction, our PC will
106 point at the following instruction. If we continue and hit a
107 breakpoint instruction, our PC will point at the breakpoint
113 CORE_ADDR stop_pc
= (*the_low_target
.get_pc
) ();
115 if (get_thread_process (current_inferior
)->stepping
)
118 return stop_pc
- the_low_target
.decr_pc_after_break
;
122 add_process (unsigned long pid
)
124 struct process_info
*process
;
126 process
= (struct process_info
*) malloc (sizeof (*process
));
127 memset (process
, 0, sizeof (*process
));
129 process
->head
.id
= pid
;
131 /* Default to tid == lwpid == pid. */
133 process
->lwpid
= pid
;
135 add_inferior_to_list (&all_processes
, &process
->head
);
140 /* Start an inferior process and returns its pid.
141 ALLARGS is a vector of program-name and args. */
144 linux_create_inferior (char *program
, char **allargs
)
149 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
155 perror_with_name ("fork");
159 ptrace (PTRACE_TRACEME
, 0, 0, 0);
161 signal (__SIGRTMIN
+ 1, SIG_DFL
);
165 execv (program
, allargs
);
167 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
173 new_process
= add_process (pid
);
174 add_thread (pid
, new_process
, pid
);
179 /* Attach to an inferior process. */
182 linux_attach_lwp (unsigned long pid
, unsigned long tid
)
184 struct process_info
*new_process
;
186 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
188 fprintf (stderr
, "Cannot attach to process %ld: %s (%d)\n", pid
,
189 strerror (errno
), errno
);
192 /* If we fail to attach to an LWP, just return. */
198 new_process
= (struct process_info
*) add_process (pid
);
199 add_thread (tid
, new_process
, pid
);
201 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
202 brings it to a halt. We should ignore that SIGSTOP and resume the process
203 (unless this is the first process, in which case the flag will be cleared
206 On the other hand, if we are currently trying to stop all threads, we
207 should treat the new thread as if we had sent it a SIGSTOP. This works
208 because we are guaranteed that add_process added us to the end of the
209 list, and so the new thread has not yet reached wait_for_sigstop (but
211 if (! stopping_threads
)
212 new_process
->stop_expected
= 1;
216 linux_attach (unsigned long pid
)
218 struct process_info
*process
;
220 linux_attach_lwp (pid
, pid
);
222 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
223 process
= (struct process_info
*) find_inferior_id (&all_processes
, pid
);
224 process
->stop_expected
= 0;
229 /* Kill the inferior process. Make us have no inferior. */
232 linux_kill_one_process (struct inferior_list_entry
*entry
)
234 struct thread_info
*thread
= (struct thread_info
*) entry
;
235 struct process_info
*process
= get_thread_process (thread
);
238 /* We avoid killing the first thread here, because of a Linux kernel (at
239 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
240 the children get a chance to be reaped, it will remain a zombie
242 if (entry
== all_threads
.head
)
247 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
249 /* Make sure it died. The loop is most likely unnecessary. */
250 wstat
= linux_wait_for_event (thread
);
251 } while (WIFSTOPPED (wstat
));
257 struct thread_info
*thread
= (struct thread_info
*) all_threads
.head
;
258 struct process_info
*process
= get_thread_process (thread
);
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. */
267 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
269 /* Make sure it died. The loop is most likely unnecessary. */
270 wstat
= linux_wait_for_event (thread
);
271 } while (WIFSTOPPED (wstat
));
275 linux_detach_one_process (struct inferior_list_entry
*entry
)
277 struct thread_info
*thread
= (struct thread_info
*) entry
;
278 struct process_info
*process
= get_thread_process (thread
);
280 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
286 for_each_inferior (&all_threads
, linux_detach_one_process
);
289 /* Return nonzero if the given thread is still alive. */
291 linux_thread_alive (unsigned long tid
)
293 if (find_inferior_id (&all_threads
, tid
) != NULL
)
299 /* Return nonzero if this process stopped at a breakpoint which
300 no longer appears to be inserted. Also adjust the PC
301 appropriately to resume where the breakpoint used to be. */
303 check_removed_breakpoint (struct process_info
*event_child
)
306 struct thread_info
*saved_inferior
;
308 if (event_child
->pending_is_breakpoint
== 0)
312 fprintf (stderr
, "Checking for breakpoint.\n");
314 saved_inferior
= current_inferior
;
315 current_inferior
= get_process_thread (event_child
);
317 stop_pc
= get_stop_pc ();
319 /* If the PC has changed since we stopped, then we shouldn't do
320 anything. This happens if, for instance, GDB handled the
321 decr_pc_after_break subtraction itself. */
322 if (stop_pc
!= event_child
->pending_stop_pc
)
325 fprintf (stderr
, "Ignoring, PC was changed.\n");
327 event_child
->pending_is_breakpoint
= 0;
328 current_inferior
= saved_inferior
;
332 /* If the breakpoint is still there, we will report hitting it. */
333 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
336 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
337 current_inferior
= saved_inferior
;
342 fprintf (stderr
, "Removed breakpoint.\n");
344 /* For decr_pc_after_break targets, here is where we perform the
345 decrement. We go immediately from this function to resuming,
346 and can not safely call get_stop_pc () again. */
347 if (the_low_target
.set_pc
!= NULL
)
348 (*the_low_target
.set_pc
) (stop_pc
);
350 /* We consumed the pending SIGTRAP. */
351 event_child
->pending_is_breakpoint
= 0;
352 event_child
->status_pending_p
= 0;
353 event_child
->status_pending
= 0;
355 current_inferior
= saved_inferior
;
359 /* Return 1 if this process has an interesting status pending. This function
360 may silently resume an inferior process. */
362 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
364 struct process_info
*process
= (struct process_info
*) entry
;
366 if (process
->status_pending_p
)
367 if (check_removed_breakpoint (process
))
369 /* This thread was stopped at a breakpoint, and the breakpoint
370 is now gone. We were told to continue (or step...) all threads,
371 so GDB isn't trying to single-step past this breakpoint.
372 So instead of reporting the old SIGTRAP, pretend we got to
373 the breakpoint just after it was removed instead of just
374 before; resume the process. */
375 linux_resume_one_process (&process
->head
, 0, 0, NULL
);
379 return process
->status_pending_p
;
383 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
386 int to_wait_for
= -1;
389 to_wait_for
= (*childp
)->lwpid
;
393 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
398 perror_with_name ("waitpid");
403 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
408 perror_with_name ("waitpid (WCLONE)");
417 && (!WIFSTOPPED (*wstatp
)
418 || (WSTOPSIG (*wstatp
) != 32
419 && WSTOPSIG (*wstatp
) != 33)))
420 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
422 if (to_wait_for
== -1)
423 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
425 (*childp
)->stopped
= 1;
426 (*childp
)->pending_is_breakpoint
= 0;
428 (*childp
)->last_status
= *wstatp
;
431 && WIFSTOPPED (*wstatp
))
433 current_inferior
= (struct thread_info
*)
434 find_inferior_id (&all_threads
, (*childp
)->tid
);
435 /* For testing only; i386_stop_pc prints out a diagnostic. */
436 if (the_low_target
.get_pc
!= NULL
)
442 linux_wait_for_event (struct thread_info
*child
)
445 struct process_info
*event_child
;
448 /* Check for a process with a pending status. */
449 /* It is possible that the user changed the pending task's registers since
450 it stopped. We correctly handle the change of PC if we hit a breakpoint
451 (in check_removed_breakpoint); signals should be reported anyway. */
454 event_child
= (struct process_info
*)
455 find_inferior (&all_processes
, status_pending_p
, NULL
);
456 if (debug_threads
&& event_child
)
457 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
461 event_child
= get_thread_process (child
);
462 if (event_child
->status_pending_p
463 && check_removed_breakpoint (event_child
))
467 if (event_child
!= NULL
)
469 if (event_child
->status_pending_p
)
472 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
473 event_child
->lwpid
, event_child
->status_pending
);
474 wstat
= event_child
->status_pending
;
475 event_child
->status_pending_p
= 0;
476 event_child
->status_pending
= 0;
477 current_inferior
= get_process_thread (event_child
);
482 /* We only enter this loop if no process has a pending wait status. Thus
483 any action taken in response to a wait status inside this loop is
484 responding as soon as we detect the status, not after any pending
491 event_child
= get_thread_process (child
);
493 linux_wait_for_process (&event_child
, &wstat
);
495 if (event_child
== NULL
)
496 error ("event from unknown child");
498 current_inferior
= (struct thread_info
*)
499 find_inferior_id (&all_threads
, event_child
->tid
);
501 /* Check for thread exit. */
502 if (using_threads
&& ! WIFSTOPPED (wstat
))
505 fprintf (stderr
, "Thread %ld (LWP %ld) exiting\n",
506 event_child
->tid
, event_child
->head
.id
);
508 /* If the last thread is exiting, just return. */
509 if (all_threads
.head
== all_threads
.tail
)
512 dead_thread_notify (event_child
->tid
);
514 remove_inferior (&all_processes
, &event_child
->head
);
516 remove_thread (current_inferior
);
517 current_inferior
= (struct thread_info
*) all_threads
.head
;
519 /* If we were waiting for this particular child to do something...
520 well, it did something. */
524 /* Wait for a more interesting event. */
529 && WIFSTOPPED (wstat
)
530 && WSTOPSIG (wstat
) == SIGSTOP
531 && event_child
->stop_expected
)
534 fprintf (stderr
, "Expected stop.\n");
535 event_child
->stop_expected
= 0;
536 linux_resume_one_process (&event_child
->head
,
537 event_child
->stepping
, 0, NULL
);
541 /* If GDB is not interested in this signal, don't stop other
542 threads, and don't report it to GDB. Just resume the
543 inferior right away. We do this for threading-related
544 signals as well as any that GDB specifically requested
545 we ignore. But never ignore SIGSTOP if we sent it
547 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
549 if (WIFSTOPPED (wstat
)
550 && ((using_threads
&& (WSTOPSIG (wstat
) == __SIGRTMIN
551 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
552 || (pass_signals
[target_signal_from_host (WSTOPSIG (wstat
))]
553 && (WSTOPSIG (wstat
) != SIGSTOP
554 || !event_child
->sigstop_sent
))))
556 siginfo_t info
, *info_p
;
559 fprintf (stderr
, "Ignored signal %d for %ld (LWP %ld).\n",
560 WSTOPSIG (wstat
), event_child
->tid
,
561 event_child
->head
.id
);
563 if (ptrace (PTRACE_GETSIGINFO
, event_child
->lwpid
, 0, &info
) == 0)
567 linux_resume_one_process (&event_child
->head
,
568 event_child
->stepping
,
569 WSTOPSIG (wstat
), info_p
);
573 /* If this event was not handled above, and is not a SIGTRAP, report
575 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
578 /* If this target does not support breakpoints, we simply report the
579 SIGTRAP; it's of no concern to us. */
580 if (the_low_target
.get_pc
== NULL
)
583 stop_pc
= get_stop_pc ();
585 /* bp_reinsert will only be set if we were single-stepping.
586 Notice that we will resume the process after hitting
587 a gdbserver breakpoint; single-stepping to/over one
588 is not supported (yet). */
589 if (event_child
->bp_reinsert
!= 0)
592 fprintf (stderr
, "Reinserted breakpoint.\n");
593 reinsert_breakpoint (event_child
->bp_reinsert
);
594 event_child
->bp_reinsert
= 0;
596 /* Clear the single-stepping flag and SIGTRAP as we resume. */
597 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
602 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
604 if (check_breakpoints (stop_pc
) != 0)
606 /* We hit one of our own breakpoints. We mark it as a pending
607 breakpoint, so that check_removed_breakpoint () will do the PC
608 adjustment for us at the appropriate time. */
609 event_child
->pending_is_breakpoint
= 1;
610 event_child
->pending_stop_pc
= stop_pc
;
612 /* Now we need to put the breakpoint back. We continue in the event
613 loop instead of simply replacing the breakpoint right away,
614 in order to not lose signals sent to the thread that hit the
615 breakpoint. Unfortunately this increases the window where another
616 thread could sneak past the removed breakpoint. For the current
617 use of server-side breakpoints (thread creation) this is
618 acceptable; but it needs to be considered before this breakpoint
619 mechanism can be used in more general ways. For some breakpoints
620 it may be necessary to stop all other threads, but that should
621 be avoided where possible.
623 If breakpoint_reinsert_addr is NULL, that means that we can
624 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
625 mark it for reinsertion, and single-step.
627 Otherwise, call the target function to figure out where we need
628 our temporary breakpoint, create it, and continue executing this
630 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
632 event_child
->bp_reinsert
= stop_pc
;
633 uninsert_breakpoint (stop_pc
);
634 linux_resume_one_process (&event_child
->head
, 1, 0, NULL
);
638 reinsert_breakpoint_by_bp
639 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
640 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
646 /* If we were single-stepping, we definitely want to report the
647 SIGTRAP. The single-step operation has completed, so also
648 clear the stepping flag; in general this does not matter,
649 because the SIGTRAP will be reported to the client, which
650 will give us a new action for this thread, but clear it for
651 consistency anyway. It's safe to clear the stepping flag
652 because the only consumer of get_stop_pc () after this point
653 is check_removed_breakpoint, and pending_is_breakpoint is not
654 set. It might be wiser to use a step_completed flag instead. */
655 if (event_child
->stepping
)
657 event_child
->stepping
= 0;
661 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
662 Check if it is a breakpoint, and if so mark the process information
663 accordingly. This will handle both the necessary fiddling with the
664 PC on decr_pc_after_break targets and suppressing extra threads
665 hitting a breakpoint if two hit it at once and then GDB removes it
666 after the first is reported. Arguably it would be better to report
667 multiple threads hitting breakpoints simultaneously, but the current
668 remote protocol does not allow this. */
669 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
671 event_child
->pending_is_breakpoint
= 1;
672 event_child
->pending_stop_pc
= stop_pc
;
682 /* Wait for process, returns status. */
685 linux_wait (char *status
)
688 struct thread_info
*child
= NULL
;
691 /* If we were only supposed to resume one thread, only wait for
692 that thread - if it's still alive. If it died, however - which
693 can happen if we're coming from the thread death case below -
694 then we need to make sure we restart the other threads. We could
695 pick a thread at random or restart all; restarting all is less
697 if (cont_thread
!= 0 && cont_thread
!= -1)
699 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
702 /* No stepping, no signal - unless one is pending already, of course. */
705 struct thread_resume resume_info
;
706 resume_info
.thread
= -1;
707 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
708 linux_resume (&resume_info
);
714 w
= linux_wait_for_event (child
);
715 stop_all_processes ();
718 /* If we are waiting for a particular child, and it exited,
719 linux_wait_for_event will return its exit status. Similarly if
720 the last child exited. If this is not the last child, however,
721 do not report it as exited until there is a 'thread exited' response
722 available in the remote protocol. Instead, just wait for another event.
723 This should be safe, because if the thread crashed we will already
724 have reported the termination signal to GDB; that should stop any
725 in-progress stepping operations, etc.
727 Report the exit status of the last thread to exit. This matches
728 LinuxThreads' behavior. */
730 if (all_threads
.head
== all_threads
.tail
)
734 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
737 free (all_processes
.head
);
738 all_processes
.head
= all_processes
.tail
= NULL
;
739 return WEXITSTATUS (w
);
741 else if (!WIFSTOPPED (w
))
743 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
746 free (all_processes
.head
);
747 all_processes
.head
= all_processes
.tail
= NULL
;
748 return target_signal_from_host (WTERMSIG (w
));
758 return target_signal_from_host (WSTOPSIG (w
));
761 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
762 thread groups are in use, we need to use tkill. */
765 kill_lwp (unsigned long lwpid
, int signo
)
767 static int tkill_failed
;
774 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
782 return kill (lwpid
, signo
);
786 send_sigstop (struct inferior_list_entry
*entry
)
788 struct process_info
*process
= (struct process_info
*) entry
;
790 if (process
->stopped
)
793 /* If we already have a pending stop signal for this process, don't
795 if (process
->stop_expected
)
797 process
->stop_expected
= 0;
802 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
804 kill_lwp (process
->head
.id
, SIGSTOP
);
805 process
->sigstop_sent
= 1;
809 wait_for_sigstop (struct inferior_list_entry
*entry
)
811 struct process_info
*process
= (struct process_info
*) entry
;
812 struct thread_info
*saved_inferior
, *thread
;
814 unsigned long saved_tid
;
816 if (process
->stopped
)
819 saved_inferior
= current_inferior
;
820 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
821 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
823 wstat
= linux_wait_for_event (thread
);
825 /* If we stopped with a non-SIGSTOP signal, save it for later
826 and record the pending SIGSTOP. If the process exited, just
828 if (WIFSTOPPED (wstat
)
829 && WSTOPSIG (wstat
) != SIGSTOP
)
832 fprintf (stderr
, "Stopped with non-sigstop signal\n");
833 process
->status_pending_p
= 1;
834 process
->status_pending
= wstat
;
835 process
->stop_expected
= 1;
838 if (linux_thread_alive (saved_tid
))
839 current_inferior
= saved_inferior
;
843 fprintf (stderr
, "Previously current thread died.\n");
845 /* Set a valid thread as current. */
846 set_desired_inferior (0);
851 stop_all_processes (void)
853 stopping_threads
= 1;
854 for_each_inferior (&all_processes
, send_sigstop
);
855 for_each_inferior (&all_processes
, wait_for_sigstop
);
856 stopping_threads
= 0;
859 /* Resume execution of the inferior process.
860 If STEP is nonzero, single-step it.
861 If SIGNAL is nonzero, give it that signal. */
864 linux_resume_one_process (struct inferior_list_entry
*entry
,
865 int step
, int signal
, siginfo_t
*info
)
867 struct process_info
*process
= (struct process_info
*) entry
;
868 struct thread_info
*saved_inferior
;
870 if (process
->stopped
== 0)
873 /* If we have pending signals or status, and a new signal, enqueue the
874 signal. Also enqueue the signal if we are waiting to reinsert a
875 breakpoint; it will be picked up again below. */
877 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
878 || process
->bp_reinsert
!= 0))
880 struct pending_signals
*p_sig
;
881 p_sig
= malloc (sizeof (*p_sig
));
882 p_sig
->prev
= process
->pending_signals
;
883 p_sig
->signal
= signal
;
885 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
887 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
888 process
->pending_signals
= p_sig
;
891 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
894 saved_inferior
= current_inferior
;
895 current_inferior
= get_process_thread (process
);
898 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
899 step
? "step" : "continue", signal
,
900 process
->stop_expected
? "expected" : "not expected");
902 /* This bit needs some thinking about. If we get a signal that
903 we must report while a single-step reinsert is still pending,
904 we often end up resuming the thread. It might be better to
905 (ew) allow a stack of pending events; then we could be sure that
906 the reinsert happened right away and not lose any signals.
908 Making this stack would also shrink the window in which breakpoints are
909 uninserted (see comment in linux_wait_for_process) but not enough for
910 complete correctness, so it won't solve that problem. It may be
911 worthwhile just to solve this one, however. */
912 if (process
->bp_reinsert
!= 0)
915 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
917 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
920 /* Postpone any pending signal. It was enqueued above. */
924 check_removed_breakpoint (process
);
926 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
928 fprintf (stderr
, " ");
929 (*the_low_target
.get_pc
) ();
932 /* If we have pending signals, consume one unless we are trying to reinsert
934 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
936 struct pending_signals
**p_sig
;
938 p_sig
= &process
->pending_signals
;
939 while ((*p_sig
)->prev
!= NULL
)
940 p_sig
= &(*p_sig
)->prev
;
942 signal
= (*p_sig
)->signal
;
943 if ((*p_sig
)->info
.si_signo
!= 0)
944 ptrace (PTRACE_SETSIGINFO
, process
->lwpid
, 0, &(*p_sig
)->info
);
950 regcache_invalidate_one ((struct inferior_list_entry
*)
951 get_process_thread (process
));
953 process
->stopped
= 0;
954 process
->stepping
= step
;
955 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
957 current_inferior
= saved_inferior
;
959 perror_with_name ("ptrace");
962 static struct thread_resume
*resume_ptr
;
964 /* This function is called once per thread. We look up the thread
965 in RESUME_PTR, and mark the thread with a pointer to the appropriate
968 This algorithm is O(threads * resume elements), but resume elements
969 is small (and will remain small at least until GDB supports thread
972 linux_set_resume_request (struct inferior_list_entry
*entry
)
974 struct process_info
*process
;
975 struct thread_info
*thread
;
978 thread
= (struct thread_info
*) entry
;
979 process
= get_thread_process (thread
);
982 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
985 process
->resume
= &resume_ptr
[ndx
];
988 /* This function is called once per thread. We check the thread's resume
989 request, which will tell us whether to resume, step, or leave the thread
990 stopped; and what signal, if any, it should be sent. For threads which
991 we aren't explicitly told otherwise, we preserve the stepping flag; this
992 is used for stepping over gdbserver-placed breakpoints. */
995 linux_continue_one_thread (struct inferior_list_entry
*entry
)
997 struct process_info
*process
;
998 struct thread_info
*thread
;
1001 thread
= (struct thread_info
*) entry
;
1002 process
= get_thread_process (thread
);
1004 if (process
->resume
->leave_stopped
)
1007 if (process
->resume
->thread
== -1)
1008 step
= process
->stepping
|| process
->resume
->step
;
1010 step
= process
->resume
->step
;
1012 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
, NULL
);
1014 process
->resume
= NULL
;
1017 /* This function is called once per thread. We check the thread's resume
1018 request, which will tell us whether to resume, step, or leave the thread
1019 stopped; and what signal, if any, it should be sent. We queue any needed
1020 signals, since we won't actually resume. We already have a pending event
1021 to report, so we don't need to preserve any step requests; they should
1022 be re-issued if necessary. */
1025 linux_queue_one_thread (struct inferior_list_entry
*entry
)
1027 struct process_info
*process
;
1028 struct thread_info
*thread
;
1030 thread
= (struct thread_info
*) entry
;
1031 process
= get_thread_process (thread
);
1033 if (process
->resume
->leave_stopped
)
1036 /* If we have a new signal, enqueue the signal. */
1037 if (process
->resume
->sig
!= 0)
1039 struct pending_signals
*p_sig
;
1040 p_sig
= malloc (sizeof (*p_sig
));
1041 p_sig
->prev
= process
->pending_signals
;
1042 p_sig
->signal
= process
->resume
->sig
;
1043 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1045 /* If this is the same signal we were previously stopped by,
1046 make sure to queue its siginfo. We can ignore the return
1047 value of ptrace; if it fails, we'll skip
1048 PTRACE_SETSIGINFO. */
1049 if (WIFSTOPPED (process
->last_status
)
1050 && WSTOPSIG (process
->last_status
) == process
->resume
->sig
)
1051 ptrace (PTRACE_GETSIGINFO
, process
->lwpid
, 0, &p_sig
->info
);
1053 process
->pending_signals
= p_sig
;
1056 process
->resume
= NULL
;
1059 /* Set DUMMY if this process has an interesting status pending. */
1061 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1063 struct process_info
*process
= (struct process_info
*) entry
;
1065 /* Processes which will not be resumed are not interesting, because
1066 we might not wait for them next time through linux_wait. */
1067 if (process
->resume
->leave_stopped
)
1070 /* If this thread has a removed breakpoint, we won't have any
1071 events to report later, so check now. check_removed_breakpoint
1072 may clear status_pending_p. We avoid calling check_removed_breakpoint
1073 for any thread that we are not otherwise going to resume - this
1074 lets us preserve stopped status when two threads hit a breakpoint.
1075 GDB removes the breakpoint to single-step a particular thread
1076 past it, then re-inserts it and resumes all threads. We want
1077 to report the second thread without resuming it in the interim. */
1078 if (process
->status_pending_p
)
1079 check_removed_breakpoint (process
);
1081 if (process
->status_pending_p
)
1082 * (int *) flag_p
= 1;
1088 linux_resume (struct thread_resume
*resume_info
)
1092 /* Yes, the use of a global here is rather ugly. */
1093 resume_ptr
= resume_info
;
1095 for_each_inferior (&all_threads
, linux_set_resume_request
);
1097 /* If there is a thread which would otherwise be resumed, which
1098 has a pending status, then don't resume any threads - we can just
1099 report the pending status. Make sure to queue any signals
1100 that would otherwise be sent. */
1102 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1107 fprintf (stderr
, "Not resuming, pending status\n");
1109 fprintf (stderr
, "Resuming, no pending status\n");
1113 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1118 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1122 #ifdef HAVE_LINUX_USRREGS
1125 register_addr (int regnum
)
1129 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1130 error ("Invalid register number %d.", regnum
);
1132 addr
= the_low_target
.regmap
[regnum
];
1137 /* Fetch one register. */
1139 fetch_register (int regno
)
1145 if (regno
>= the_low_target
.num_regs
)
1147 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1150 regaddr
= register_addr (regno
);
1153 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1154 & - sizeof (PTRACE_XFER_TYPE
);
1155 buf
= alloca (size
);
1156 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1159 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1160 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1161 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1164 /* Warning, not error, in case we are attached; sometimes the
1165 kernel doesn't let us at the registers. */
1166 char *err
= strerror (errno
);
1167 char *msg
= alloca (strlen (err
) + 128);
1168 sprintf (msg
, "reading register %d: %s", regno
, err
);
1173 if (the_low_target
.left_pad_xfer
1174 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1175 supply_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1176 - register_size (regno
)));
1178 supply_register (regno
, buf
);
1183 /* Fetch all registers, or just one, from the child process. */
1185 usr_fetch_inferior_registers (int regno
)
1187 if (regno
== -1 || regno
== 0)
1188 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1189 fetch_register (regno
);
1191 fetch_register (regno
);
1194 /* Store our register values back into the inferior.
1195 If REGNO is -1, do this for all registers.
1196 Otherwise, REGNO specifies which register (so we can save time). */
1198 usr_store_inferior_registers (int regno
)
1206 if (regno
>= the_low_target
.num_regs
)
1209 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1212 regaddr
= register_addr (regno
);
1216 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1217 & - sizeof (PTRACE_XFER_TYPE
);
1218 buf
= alloca (size
);
1219 memset (buf
, 0, size
);
1220 if (the_low_target
.left_pad_xfer
1221 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1222 collect_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1223 - register_size (regno
)));
1225 collect_register (regno
, buf
);
1226 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1229 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1230 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1233 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1235 char *err
= strerror (errno
);
1236 char *msg
= alloca (strlen (err
) + 128);
1237 sprintf (msg
, "writing register %d: %s",
1243 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1247 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1248 usr_store_inferior_registers (regno
);
1250 #endif /* HAVE_LINUX_USRREGS */
1254 #ifdef HAVE_LINUX_REGSETS
1257 regsets_fetch_inferior_registers ()
1259 struct regset_info
*regset
;
1260 int saw_general_regs
= 0;
1262 regset
= target_regsets
;
1264 while (regset
->size
>= 0)
1269 if (regset
->size
== 0)
1275 buf
= malloc (regset
->size
);
1276 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1281 /* If we get EIO on the first regset, do not try regsets again.
1282 If we get EIO on a later regset, disable that regset. */
1283 if (regset
== target_regsets
)
1297 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1302 else if (regset
->type
== GENERAL_REGS
)
1303 saw_general_regs
= 1;
1304 regset
->store_function (buf
);
1307 if (saw_general_regs
)
1314 regsets_store_inferior_registers ()
1316 struct regset_info
*regset
;
1317 int saw_general_regs
= 0;
1319 regset
= target_regsets
;
1321 while (regset
->size
>= 0)
1326 if (regset
->size
== 0)
1332 buf
= malloc (regset
->size
);
1334 /* First fill the buffer with the current register set contents,
1335 in case there are any items in the kernel's regset that are
1336 not in gdbserver's regcache. */
1337 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1341 /* Then overlay our cached registers on that. */
1342 regset
->fill_function (buf
);
1344 /* Only now do we write the register set. */
1345 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1352 /* If we get EIO on the first regset, do not try regsets again.
1353 If we get EIO on a later regset, disable that regset. */
1354 if (regset
== target_regsets
)
1367 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1370 else if (regset
->type
== GENERAL_REGS
)
1371 saw_general_regs
= 1;
1375 if (saw_general_regs
)
1382 #endif /* HAVE_LINUX_REGSETS */
1386 linux_fetch_registers (int regno
)
1388 #ifdef HAVE_LINUX_REGSETS
1391 if (regsets_fetch_inferior_registers () == 0)
1395 #ifdef HAVE_LINUX_USRREGS
1396 usr_fetch_inferior_registers (regno
);
1401 linux_store_registers (int regno
)
1403 #ifdef HAVE_LINUX_REGSETS
1406 if (regsets_store_inferior_registers () == 0)
1410 #ifdef HAVE_LINUX_USRREGS
1411 usr_store_inferior_registers (regno
);
1416 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1417 to debugger memory starting at MYADDR. */
1420 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1423 /* Round starting address down to longword boundary. */
1424 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1425 /* Round ending address up; get number of longwords that makes. */
1427 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1428 / sizeof (PTRACE_XFER_TYPE
);
1429 /* Allocate buffer of that many longwords. */
1430 register PTRACE_XFER_TYPE
*buffer
1431 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1433 /* Read all the longwords */
1434 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1437 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1442 /* Copy appropriate bytes out of the buffer. */
1443 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1448 /* Copy LEN bytes of data from debugger memory at MYADDR
1449 to inferior's memory at MEMADDR.
1450 On failure (cannot write the inferior)
1451 returns the value of errno. */
1454 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
1457 /* Round starting address down to longword boundary. */
1458 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1459 /* Round ending address up; get number of longwords that makes. */
1461 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1462 /* Allocate buffer of that many longwords. */
1463 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1468 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1471 /* Fill start and end extra bytes of buffer with existing memory data. */
1473 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1474 (PTRACE_ARG3_TYPE
) addr
, 0);
1479 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1480 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1481 * sizeof (PTRACE_XFER_TYPE
)),
1485 /* Copy data to be written over corresponding part of buffer */
1487 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1489 /* Write the entire buffer. */
1491 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1494 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1503 linux_look_up_symbols (void)
1505 #ifdef USE_THREAD_DB
1509 using_threads
= thread_db_init ();
1514 linux_send_signal (int signum
)
1516 extern unsigned long signal_pid
;
1518 if (cont_thread
!= 0 && cont_thread
!= -1)
1520 struct process_info
*process
;
1522 process
= get_thread_process (current_inferior
);
1523 kill_lwp (process
->lwpid
, signum
);
1526 kill_lwp (signal_pid
, signum
);
1529 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1530 to debugger memory starting at MYADDR. */
1533 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
1535 char filename
[PATH_MAX
];
1538 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1540 fd
= open (filename
, O_RDONLY
);
1544 if (offset
!= (CORE_ADDR
) 0
1545 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1548 n
= read (fd
, myaddr
, len
);
1555 /* These watchpoint related wrapper functions simply pass on the function call
1556 if the target has registered a corresponding function. */
1559 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1561 if (the_low_target
.insert_watchpoint
!= NULL
)
1562 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1564 /* Unsupported (see target.h). */
1569 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1571 if (the_low_target
.remove_watchpoint
!= NULL
)
1572 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1574 /* Unsupported (see target.h). */
1579 linux_stopped_by_watchpoint (void)
1581 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1582 return the_low_target
.stopped_by_watchpoint ();
1588 linux_stopped_data_address (void)
1590 if (the_low_target
.stopped_data_address
!= NULL
)
1591 return the_low_target
.stopped_data_address ();
1596 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
1597 #if defined(__mcoldfire__)
1598 /* These should really be defined in the kernel's ptrace.h header. */
1599 #define PT_TEXT_ADDR 49*4
1600 #define PT_DATA_ADDR 50*4
1601 #define PT_TEXT_END_ADDR 51*4
1604 /* Under uClinux, programs are loaded at non-zero offsets, which we need
1605 to tell gdb about. */
1608 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
1610 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1611 unsigned long text
, text_end
, data
;
1612 int pid
= get_thread_process (current_inferior
)->head
.id
;
1616 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
1617 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
1618 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
1622 /* Both text and data offsets produced at compile-time (and so
1623 used by gdb) are relative to the beginning of the program,
1624 with the data segment immediately following the text segment.
1625 However, the actual runtime layout in memory may put the data
1626 somewhere else, so when we send gdb a data base-address, we
1627 use the real data base address and subtract the compile-time
1628 data base-address from it (which is just the length of the
1629 text segment). BSS immediately follows data in both
1632 *data_p
= data
- (text_end
- text
);
1641 static struct target_ops linux_target_ops
= {
1642 linux_create_inferior
,
1649 linux_fetch_registers
,
1650 linux_store_registers
,
1653 linux_look_up_symbols
,
1656 linux_insert_watchpoint
,
1657 linux_remove_watchpoint
,
1658 linux_stopped_by_watchpoint
,
1659 linux_stopped_data_address
,
1660 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
1665 #ifdef USE_THREAD_DB
1666 thread_db_get_tls_address
,
1673 linux_init_signals ()
1675 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1676 to find what the cancel signal actually is. */
1677 signal (__SIGRTMIN
+1, SIG_IGN
);
1681 initialize_low (void)
1684 set_target_ops (&linux_target_ops
);
1685 set_breakpoint_data (the_low_target
.breakpoint
,
1686 the_low_target
.breakpoint_len
);
1688 linux_init_signals ();