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
46 #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
51 /* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
52 however. This requires changing the ID in place when we go from !using_threads
53 to using_threads, immediately.
55 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
56 the same as the LWP ID. */
58 struct inferior_list all_processes
;
60 /* FIXME this is a bit of a hack, and could be removed. */
63 /* FIXME make into a target method? */
66 static void linux_resume_one_process (struct inferior_list_entry
*entry
,
67 int step
, int signal
, siginfo_t
*info
);
68 static void linux_resume (struct thread_resume
*resume_info
);
69 static void stop_all_processes (void);
70 static int linux_wait_for_event (struct thread_info
*child
);
72 struct pending_signals
76 struct pending_signals
*prev
;
79 #define PTRACE_ARG3_TYPE long
80 #define PTRACE_XFER_TYPE long
82 #ifdef HAVE_LINUX_REGSETS
83 static int use_regsets_p
= 1;
86 #define pid_of(proc) ((proc)->head.id)
88 /* FIXME: Delete eventually. */
89 #define inferior_pid (pid_of (get_thread_process (current_inferior)))
91 /* This function should only be called if the process got a SIGTRAP.
92 The SIGTRAP could mean several things.
94 On i386, where decr_pc_after_break is non-zero:
95 If we were single-stepping this process using PTRACE_SINGLESTEP,
96 we will get only the one SIGTRAP (even if the instruction we
97 stepped over was a breakpoint). The value of $eip will be the
99 If we continue the process using PTRACE_CONT, we will get a
100 SIGTRAP when we hit a breakpoint. The value of $eip will be
101 the instruction after the breakpoint (i.e. needs to be
102 decremented). If we report the SIGTRAP to GDB, we must also
103 report the undecremented PC. If we cancel the SIGTRAP, we
104 must resume at the decremented PC.
106 (Presumably, not yet tested) On a non-decr_pc_after_break machine
107 with hardware or kernel single-step:
108 If we single-step over a breakpoint instruction, our PC will
109 point at the following instruction. If we continue and hit a
110 breakpoint instruction, our PC will point at the breakpoint
116 CORE_ADDR stop_pc
= (*the_low_target
.get_pc
) ();
118 if (get_thread_process (current_inferior
)->stepping
)
121 return stop_pc
- the_low_target
.decr_pc_after_break
;
125 add_process (unsigned long pid
)
127 struct process_info
*process
;
129 process
= (struct process_info
*) malloc (sizeof (*process
));
130 memset (process
, 0, sizeof (*process
));
132 process
->head
.id
= pid
;
134 /* Default to tid == lwpid == pid. */
136 process
->lwpid
= pid
;
138 add_inferior_to_list (&all_processes
, &process
->head
);
143 /* Start an inferior process and returns its pid.
144 ALLARGS is a vector of program-name and args. */
147 linux_create_inferior (char *program
, char **allargs
)
152 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
158 perror_with_name ("fork");
162 ptrace (PTRACE_TRACEME
, 0, 0, 0);
164 signal (__SIGRTMIN
+ 1, SIG_DFL
);
168 execvp (program
, allargs
);
170 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
176 new_process
= add_process (pid
);
177 add_thread (pid
, new_process
, pid
);
182 /* Attach to an inferior process. */
185 linux_attach_lwp (unsigned long pid
, unsigned long tid
)
187 struct process_info
*new_process
;
189 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
191 fprintf (stderr
, "Cannot attach to process %ld: %s (%d)\n", pid
,
192 strerror (errno
), errno
);
195 /* If we fail to attach to an LWP, just return. */
201 new_process
= (struct process_info
*) add_process (pid
);
202 add_thread (tid
, new_process
, pid
);
204 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
205 brings it to a halt. We should ignore that SIGSTOP and resume the process
206 (unless this is the first process, in which case the flag will be cleared
209 On the other hand, if we are currently trying to stop all threads, we
210 should treat the new thread as if we had sent it a SIGSTOP. This works
211 because we are guaranteed that add_process added us to the end of the
212 list, and so the new thread has not yet reached wait_for_sigstop (but
214 if (! stopping_threads
)
215 new_process
->stop_expected
= 1;
219 linux_attach (unsigned long pid
)
221 struct process_info
*process
;
223 linux_attach_lwp (pid
, pid
);
225 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
226 process
= (struct process_info
*) find_inferior_id (&all_processes
, pid
);
227 process
->stop_expected
= 0;
232 /* Kill the inferior process. Make us have no inferior. */
235 linux_kill_one_process (struct inferior_list_entry
*entry
)
237 struct thread_info
*thread
= (struct thread_info
*) entry
;
238 struct process_info
*process
= get_thread_process (thread
);
241 /* We avoid killing the first thread here, because of a Linux kernel (at
242 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
243 the children get a chance to be reaped, it will remain a zombie
245 if (entry
== all_threads
.head
)
250 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
252 /* Make sure it died. The loop is most likely unnecessary. */
253 wstat
= linux_wait_for_event (thread
);
254 } while (WIFSTOPPED (wstat
));
260 struct thread_info
*thread
= (struct thread_info
*) all_threads
.head
;
261 struct process_info
*process
;
267 for_each_inferior (&all_threads
, linux_kill_one_process
);
269 /* See the comment in linux_kill_one_process. We did not kill the first
270 thread in the list, so do so now. */
271 process
= get_thread_process (thread
);
274 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
276 /* Make sure it died. The loop is most likely unnecessary. */
277 wstat
= linux_wait_for_event (thread
);
278 } while (WIFSTOPPED (wstat
));
282 linux_detach_one_process (struct inferior_list_entry
*entry
)
284 struct thread_info
*thread
= (struct thread_info
*) entry
;
285 struct process_info
*process
= get_thread_process (thread
);
287 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
293 for_each_inferior (&all_threads
, linux_detach_one_process
);
300 extern unsigned long signal_pid
;
304 ret
= waitpid (signal_pid
, &status
, 0);
305 if (WIFEXITED (status
) || WIFSIGNALED (status
))
307 } while (ret
!= -1 || errno
!= ECHILD
);
310 /* Return nonzero if the given thread is still alive. */
312 linux_thread_alive (unsigned long tid
)
314 if (find_inferior_id (&all_threads
, tid
) != NULL
)
320 /* Return nonzero if this process stopped at a breakpoint which
321 no longer appears to be inserted. Also adjust the PC
322 appropriately to resume where the breakpoint used to be. */
324 check_removed_breakpoint (struct process_info
*event_child
)
327 struct thread_info
*saved_inferior
;
329 if (event_child
->pending_is_breakpoint
== 0)
333 fprintf (stderr
, "Checking for breakpoint.\n");
335 saved_inferior
= current_inferior
;
336 current_inferior
= get_process_thread (event_child
);
338 stop_pc
= get_stop_pc ();
340 /* If the PC has changed since we stopped, then we shouldn't do
341 anything. This happens if, for instance, GDB handled the
342 decr_pc_after_break subtraction itself. */
343 if (stop_pc
!= event_child
->pending_stop_pc
)
346 fprintf (stderr
, "Ignoring, PC was changed.\n");
348 event_child
->pending_is_breakpoint
= 0;
349 current_inferior
= saved_inferior
;
353 /* If the breakpoint is still there, we will report hitting it. */
354 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
357 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
358 current_inferior
= saved_inferior
;
363 fprintf (stderr
, "Removed breakpoint.\n");
365 /* For decr_pc_after_break targets, here is where we perform the
366 decrement. We go immediately from this function to resuming,
367 and can not safely call get_stop_pc () again. */
368 if (the_low_target
.set_pc
!= NULL
)
369 (*the_low_target
.set_pc
) (stop_pc
);
371 /* We consumed the pending SIGTRAP. */
372 event_child
->pending_is_breakpoint
= 0;
373 event_child
->status_pending_p
= 0;
374 event_child
->status_pending
= 0;
376 current_inferior
= saved_inferior
;
380 /* Return 1 if this process has an interesting status pending. This function
381 may silently resume an inferior process. */
383 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
385 struct process_info
*process
= (struct process_info
*) entry
;
387 if (process
->status_pending_p
)
388 if (check_removed_breakpoint (process
))
390 /* This thread was stopped at a breakpoint, and the breakpoint
391 is now gone. We were told to continue (or step...) all threads,
392 so GDB isn't trying to single-step past this breakpoint.
393 So instead of reporting the old SIGTRAP, pretend we got to
394 the breakpoint just after it was removed instead of just
395 before; resume the process. */
396 linux_resume_one_process (&process
->head
, 0, 0, NULL
);
400 return process
->status_pending_p
;
404 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
407 int to_wait_for
= -1;
410 to_wait_for
= (*childp
)->lwpid
;
414 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
419 perror_with_name ("waitpid");
424 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
429 perror_with_name ("waitpid (WCLONE)");
438 && (!WIFSTOPPED (*wstatp
)
439 || (WSTOPSIG (*wstatp
) != 32
440 && WSTOPSIG (*wstatp
) != 33)))
441 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
443 if (to_wait_for
== -1)
444 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
446 (*childp
)->stopped
= 1;
447 (*childp
)->pending_is_breakpoint
= 0;
449 (*childp
)->last_status
= *wstatp
;
452 && WIFSTOPPED (*wstatp
))
454 current_inferior
= (struct thread_info
*)
455 find_inferior_id (&all_threads
, (*childp
)->tid
);
456 /* For testing only; i386_stop_pc prints out a diagnostic. */
457 if (the_low_target
.get_pc
!= NULL
)
463 linux_wait_for_event (struct thread_info
*child
)
466 struct process_info
*event_child
;
469 /* Check for a process with a pending status. */
470 /* It is possible that the user changed the pending task's registers since
471 it stopped. We correctly handle the change of PC if we hit a breakpoint
472 (in check_removed_breakpoint); signals should be reported anyway. */
475 event_child
= (struct process_info
*)
476 find_inferior (&all_processes
, status_pending_p
, NULL
);
477 if (debug_threads
&& event_child
)
478 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
482 event_child
= get_thread_process (child
);
483 if (event_child
->status_pending_p
484 && check_removed_breakpoint (event_child
))
488 if (event_child
!= NULL
)
490 if (event_child
->status_pending_p
)
493 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
494 event_child
->lwpid
, event_child
->status_pending
);
495 wstat
= event_child
->status_pending
;
496 event_child
->status_pending_p
= 0;
497 event_child
->status_pending
= 0;
498 current_inferior
= get_process_thread (event_child
);
503 /* We only enter this loop if no process has a pending wait status. Thus
504 any action taken in response to a wait status inside this loop is
505 responding as soon as we detect the status, not after any pending
512 event_child
= get_thread_process (child
);
514 linux_wait_for_process (&event_child
, &wstat
);
516 if (event_child
== NULL
)
517 error ("event from unknown child");
519 current_inferior
= (struct thread_info
*)
520 find_inferior_id (&all_threads
, event_child
->tid
);
522 /* Check for thread exit. */
523 if (using_threads
&& ! WIFSTOPPED (wstat
))
526 fprintf (stderr
, "Thread %ld (LWP %ld) exiting\n",
527 event_child
->tid
, event_child
->head
.id
);
529 /* If the last thread is exiting, just return. */
530 if (all_threads
.head
== all_threads
.tail
)
533 dead_thread_notify (event_child
->tid
);
535 remove_inferior (&all_processes
, &event_child
->head
);
537 remove_thread (current_inferior
);
538 current_inferior
= (struct thread_info
*) all_threads
.head
;
540 /* If we were waiting for this particular child to do something...
541 well, it did something. */
545 /* Wait for a more interesting event. */
550 && WIFSTOPPED (wstat
)
551 && WSTOPSIG (wstat
) == SIGSTOP
552 && event_child
->stop_expected
)
555 fprintf (stderr
, "Expected stop.\n");
556 event_child
->stop_expected
= 0;
557 linux_resume_one_process (&event_child
->head
,
558 event_child
->stepping
, 0, NULL
);
562 /* If GDB is not interested in this signal, don't stop other
563 threads, and don't report it to GDB. Just resume the
564 inferior right away. We do this for threading-related
565 signals as well as any that GDB specifically requested
566 we ignore. But never ignore SIGSTOP if we sent it
568 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
570 if (WIFSTOPPED (wstat
)
571 && ((using_threads
&& (WSTOPSIG (wstat
) == __SIGRTMIN
572 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
573 || (pass_signals
[target_signal_from_host (WSTOPSIG (wstat
))]
574 && (WSTOPSIG (wstat
) != SIGSTOP
575 || !event_child
->sigstop_sent
))))
577 siginfo_t info
, *info_p
;
580 fprintf (stderr
, "Ignored signal %d for %ld (LWP %ld).\n",
581 WSTOPSIG (wstat
), event_child
->tid
,
582 event_child
->head
.id
);
584 if (ptrace (PTRACE_GETSIGINFO
, event_child
->lwpid
, 0, &info
) == 0)
588 linux_resume_one_process (&event_child
->head
,
589 event_child
->stepping
,
590 WSTOPSIG (wstat
), info_p
);
594 /* If this event was not handled above, and is not a SIGTRAP, report
596 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
599 /* If this target does not support breakpoints, we simply report the
600 SIGTRAP; it's of no concern to us. */
601 if (the_low_target
.get_pc
== NULL
)
604 stop_pc
= get_stop_pc ();
606 /* bp_reinsert will only be set if we were single-stepping.
607 Notice that we will resume the process after hitting
608 a gdbserver breakpoint; single-stepping to/over one
609 is not supported (yet). */
610 if (event_child
->bp_reinsert
!= 0)
613 fprintf (stderr
, "Reinserted breakpoint.\n");
614 reinsert_breakpoint (event_child
->bp_reinsert
);
615 event_child
->bp_reinsert
= 0;
617 /* Clear the single-stepping flag and SIGTRAP as we resume. */
618 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
623 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
625 if (check_breakpoints (stop_pc
) != 0)
627 /* We hit one of our own breakpoints. We mark it as a pending
628 breakpoint, so that check_removed_breakpoint () will do the PC
629 adjustment for us at the appropriate time. */
630 event_child
->pending_is_breakpoint
= 1;
631 event_child
->pending_stop_pc
= stop_pc
;
633 /* Now we need to put the breakpoint back. We continue in the event
634 loop instead of simply replacing the breakpoint right away,
635 in order to not lose signals sent to the thread that hit the
636 breakpoint. Unfortunately this increases the window where another
637 thread could sneak past the removed breakpoint. For the current
638 use of server-side breakpoints (thread creation) this is
639 acceptable; but it needs to be considered before this breakpoint
640 mechanism can be used in more general ways. For some breakpoints
641 it may be necessary to stop all other threads, but that should
642 be avoided where possible.
644 If breakpoint_reinsert_addr is NULL, that means that we can
645 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
646 mark it for reinsertion, and single-step.
648 Otherwise, call the target function to figure out where we need
649 our temporary breakpoint, create it, and continue executing this
651 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
653 event_child
->bp_reinsert
= stop_pc
;
654 uninsert_breakpoint (stop_pc
);
655 linux_resume_one_process (&event_child
->head
, 1, 0, NULL
);
659 reinsert_breakpoint_by_bp
660 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
661 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
667 /* If we were single-stepping, we definitely want to report the
668 SIGTRAP. The single-step operation has completed, so also
669 clear the stepping flag; in general this does not matter,
670 because the SIGTRAP will be reported to the client, which
671 will give us a new action for this thread, but clear it for
672 consistency anyway. It's safe to clear the stepping flag
673 because the only consumer of get_stop_pc () after this point
674 is check_removed_breakpoint, and pending_is_breakpoint is not
675 set. It might be wiser to use a step_completed flag instead. */
676 if (event_child
->stepping
)
678 event_child
->stepping
= 0;
682 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
683 Check if it is a breakpoint, and if so mark the process information
684 accordingly. This will handle both the necessary fiddling with the
685 PC on decr_pc_after_break targets and suppressing extra threads
686 hitting a breakpoint if two hit it at once and then GDB removes it
687 after the first is reported. Arguably it would be better to report
688 multiple threads hitting breakpoints simultaneously, but the current
689 remote protocol does not allow this. */
690 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
692 event_child
->pending_is_breakpoint
= 1;
693 event_child
->pending_stop_pc
= stop_pc
;
703 /* Wait for process, returns status. */
706 linux_wait (char *status
)
709 struct thread_info
*child
= NULL
;
712 /* If we were only supposed to resume one thread, only wait for
713 that thread - if it's still alive. If it died, however - which
714 can happen if we're coming from the thread death case below -
715 then we need to make sure we restart the other threads. We could
716 pick a thread at random or restart all; restarting all is less
718 if (cont_thread
!= 0 && cont_thread
!= -1)
720 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
723 /* No stepping, no signal - unless one is pending already, of course. */
726 struct thread_resume resume_info
;
727 resume_info
.thread
= -1;
728 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
729 linux_resume (&resume_info
);
735 w
= linux_wait_for_event (child
);
736 stop_all_processes ();
739 /* If we are waiting for a particular child, and it exited,
740 linux_wait_for_event will return its exit status. Similarly if
741 the last child exited. If this is not the last child, however,
742 do not report it as exited until there is a 'thread exited' response
743 available in the remote protocol. Instead, just wait for another event.
744 This should be safe, because if the thread crashed we will already
745 have reported the termination signal to GDB; that should stop any
746 in-progress stepping operations, etc.
748 Report the exit status of the last thread to exit. This matches
749 LinuxThreads' behavior. */
751 if (all_threads
.head
== all_threads
.tail
)
755 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
758 free (all_processes
.head
);
759 all_processes
.head
= all_processes
.tail
= NULL
;
760 return WEXITSTATUS (w
);
762 else if (!WIFSTOPPED (w
))
764 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
767 free (all_processes
.head
);
768 all_processes
.head
= all_processes
.tail
= NULL
;
769 return target_signal_from_host (WTERMSIG (w
));
779 return target_signal_from_host (WSTOPSIG (w
));
782 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
783 thread groups are in use, we need to use tkill. */
786 kill_lwp (unsigned long lwpid
, int signo
)
788 static int tkill_failed
;
795 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
803 return kill (lwpid
, signo
);
807 send_sigstop (struct inferior_list_entry
*entry
)
809 struct process_info
*process
= (struct process_info
*) entry
;
811 if (process
->stopped
)
814 /* If we already have a pending stop signal for this process, don't
816 if (process
->stop_expected
)
818 process
->stop_expected
= 0;
823 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
825 kill_lwp (process
->head
.id
, SIGSTOP
);
826 process
->sigstop_sent
= 1;
830 wait_for_sigstop (struct inferior_list_entry
*entry
)
832 struct process_info
*process
= (struct process_info
*) entry
;
833 struct thread_info
*saved_inferior
, *thread
;
835 unsigned long saved_tid
;
837 if (process
->stopped
)
840 saved_inferior
= current_inferior
;
841 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
842 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
844 wstat
= linux_wait_for_event (thread
);
846 /* If we stopped with a non-SIGSTOP signal, save it for later
847 and record the pending SIGSTOP. If the process exited, just
849 if (WIFSTOPPED (wstat
)
850 && WSTOPSIG (wstat
) != SIGSTOP
)
853 fprintf (stderr
, "Stopped with non-sigstop signal\n");
854 process
->status_pending_p
= 1;
855 process
->status_pending
= wstat
;
856 process
->stop_expected
= 1;
859 if (linux_thread_alive (saved_tid
))
860 current_inferior
= saved_inferior
;
864 fprintf (stderr
, "Previously current thread died.\n");
866 /* Set a valid thread as current. */
867 set_desired_inferior (0);
872 stop_all_processes (void)
874 stopping_threads
= 1;
875 for_each_inferior (&all_processes
, send_sigstop
);
876 for_each_inferior (&all_processes
, wait_for_sigstop
);
877 stopping_threads
= 0;
880 /* Resume execution of the inferior process.
881 If STEP is nonzero, single-step it.
882 If SIGNAL is nonzero, give it that signal. */
885 linux_resume_one_process (struct inferior_list_entry
*entry
,
886 int step
, int signal
, siginfo_t
*info
)
888 struct process_info
*process
= (struct process_info
*) entry
;
889 struct thread_info
*saved_inferior
;
891 if (process
->stopped
== 0)
894 /* If we have pending signals or status, and a new signal, enqueue the
895 signal. Also enqueue the signal if we are waiting to reinsert a
896 breakpoint; it will be picked up again below. */
898 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
899 || process
->bp_reinsert
!= 0))
901 struct pending_signals
*p_sig
;
902 p_sig
= malloc (sizeof (*p_sig
));
903 p_sig
->prev
= process
->pending_signals
;
904 p_sig
->signal
= signal
;
906 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
908 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
909 process
->pending_signals
= p_sig
;
912 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
915 saved_inferior
= current_inferior
;
916 current_inferior
= get_process_thread (process
);
919 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
920 step
? "step" : "continue", signal
,
921 process
->stop_expected
? "expected" : "not expected");
923 /* This bit needs some thinking about. If we get a signal that
924 we must report while a single-step reinsert is still pending,
925 we often end up resuming the thread. It might be better to
926 (ew) allow a stack of pending events; then we could be sure that
927 the reinsert happened right away and not lose any signals.
929 Making this stack would also shrink the window in which breakpoints are
930 uninserted (see comment in linux_wait_for_process) but not enough for
931 complete correctness, so it won't solve that problem. It may be
932 worthwhile just to solve this one, however. */
933 if (process
->bp_reinsert
!= 0)
936 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
938 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
941 /* Postpone any pending signal. It was enqueued above. */
945 check_removed_breakpoint (process
);
947 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
949 fprintf (stderr
, " ");
950 (*the_low_target
.get_pc
) ();
953 /* If we have pending signals, consume one unless we are trying to reinsert
955 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
957 struct pending_signals
**p_sig
;
959 p_sig
= &process
->pending_signals
;
960 while ((*p_sig
)->prev
!= NULL
)
961 p_sig
= &(*p_sig
)->prev
;
963 signal
= (*p_sig
)->signal
;
964 if ((*p_sig
)->info
.si_signo
!= 0)
965 ptrace (PTRACE_SETSIGINFO
, process
->lwpid
, 0, &(*p_sig
)->info
);
971 regcache_invalidate_one ((struct inferior_list_entry
*)
972 get_process_thread (process
));
974 process
->stopped
= 0;
975 process
->stepping
= step
;
976 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
978 current_inferior
= saved_inferior
;
980 perror_with_name ("ptrace");
983 static struct thread_resume
*resume_ptr
;
985 /* This function is called once per thread. We look up the thread
986 in RESUME_PTR, and mark the thread with a pointer to the appropriate
989 This algorithm is O(threads * resume elements), but resume elements
990 is small (and will remain small at least until GDB supports thread
993 linux_set_resume_request (struct inferior_list_entry
*entry
)
995 struct process_info
*process
;
996 struct thread_info
*thread
;
999 thread
= (struct thread_info
*) entry
;
1000 process
= get_thread_process (thread
);
1003 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
1006 process
->resume
= &resume_ptr
[ndx
];
1009 /* This function is called once per thread. We check the thread's resume
1010 request, which will tell us whether to resume, step, or leave the thread
1011 stopped; and what signal, if any, it should be sent. For threads which
1012 we aren't explicitly told otherwise, we preserve the stepping flag; this
1013 is used for stepping over gdbserver-placed breakpoints. */
1016 linux_continue_one_thread (struct inferior_list_entry
*entry
)
1018 struct process_info
*process
;
1019 struct thread_info
*thread
;
1022 thread
= (struct thread_info
*) entry
;
1023 process
= get_thread_process (thread
);
1025 if (process
->resume
->leave_stopped
)
1028 if (process
->resume
->thread
== -1)
1029 step
= process
->stepping
|| process
->resume
->step
;
1031 step
= process
->resume
->step
;
1033 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
, NULL
);
1035 process
->resume
= NULL
;
1038 /* This function is called once per thread. We check the thread's resume
1039 request, which will tell us whether to resume, step, or leave the thread
1040 stopped; and what signal, if any, it should be sent. We queue any needed
1041 signals, since we won't actually resume. We already have a pending event
1042 to report, so we don't need to preserve any step requests; they should
1043 be re-issued if necessary. */
1046 linux_queue_one_thread (struct inferior_list_entry
*entry
)
1048 struct process_info
*process
;
1049 struct thread_info
*thread
;
1051 thread
= (struct thread_info
*) entry
;
1052 process
= get_thread_process (thread
);
1054 if (process
->resume
->leave_stopped
)
1057 /* If we have a new signal, enqueue the signal. */
1058 if (process
->resume
->sig
!= 0)
1060 struct pending_signals
*p_sig
;
1061 p_sig
= malloc (sizeof (*p_sig
));
1062 p_sig
->prev
= process
->pending_signals
;
1063 p_sig
->signal
= process
->resume
->sig
;
1064 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1066 /* If this is the same signal we were previously stopped by,
1067 make sure to queue its siginfo. We can ignore the return
1068 value of ptrace; if it fails, we'll skip
1069 PTRACE_SETSIGINFO. */
1070 if (WIFSTOPPED (process
->last_status
)
1071 && WSTOPSIG (process
->last_status
) == process
->resume
->sig
)
1072 ptrace (PTRACE_GETSIGINFO
, process
->lwpid
, 0, &p_sig
->info
);
1074 process
->pending_signals
= p_sig
;
1077 process
->resume
= NULL
;
1080 /* Set DUMMY if this process has an interesting status pending. */
1082 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1084 struct process_info
*process
= (struct process_info
*) entry
;
1086 /* Processes which will not be resumed are not interesting, because
1087 we might not wait for them next time through linux_wait. */
1088 if (process
->resume
->leave_stopped
)
1091 /* If this thread has a removed breakpoint, we won't have any
1092 events to report later, so check now. check_removed_breakpoint
1093 may clear status_pending_p. We avoid calling check_removed_breakpoint
1094 for any thread that we are not otherwise going to resume - this
1095 lets us preserve stopped status when two threads hit a breakpoint.
1096 GDB removes the breakpoint to single-step a particular thread
1097 past it, then re-inserts it and resumes all threads. We want
1098 to report the second thread without resuming it in the interim. */
1099 if (process
->status_pending_p
)
1100 check_removed_breakpoint (process
);
1102 if (process
->status_pending_p
)
1103 * (int *) flag_p
= 1;
1109 linux_resume (struct thread_resume
*resume_info
)
1113 /* Yes, the use of a global here is rather ugly. */
1114 resume_ptr
= resume_info
;
1116 for_each_inferior (&all_threads
, linux_set_resume_request
);
1118 /* If there is a thread which would otherwise be resumed, which
1119 has a pending status, then don't resume any threads - we can just
1120 report the pending status. Make sure to queue any signals
1121 that would otherwise be sent. */
1123 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1128 fprintf (stderr
, "Not resuming, pending status\n");
1130 fprintf (stderr
, "Resuming, no pending status\n");
1134 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1139 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1143 #ifdef HAVE_LINUX_USRREGS
1146 register_addr (int regnum
)
1150 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1151 error ("Invalid register number %d.", regnum
);
1153 addr
= the_low_target
.regmap
[regnum
];
1158 /* Fetch one register. */
1160 fetch_register (int regno
)
1166 if (regno
>= the_low_target
.num_regs
)
1168 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1171 regaddr
= register_addr (regno
);
1174 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1175 & - sizeof (PTRACE_XFER_TYPE
);
1176 buf
= alloca (size
);
1177 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1180 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1181 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1182 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1185 /* Warning, not error, in case we are attached; sometimes the
1186 kernel doesn't let us at the registers. */
1187 char *err
= strerror (errno
);
1188 char *msg
= alloca (strlen (err
) + 128);
1189 sprintf (msg
, "reading register %d: %s", regno
, err
);
1194 if (the_low_target
.left_pad_xfer
1195 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1196 supply_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1197 - register_size (regno
)));
1199 supply_register (regno
, buf
);
1204 /* Fetch all registers, or just one, from the child process. */
1206 usr_fetch_inferior_registers (int regno
)
1208 if (regno
== -1 || regno
== 0)
1209 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1210 fetch_register (regno
);
1212 fetch_register (regno
);
1215 /* Store our register values back into the inferior.
1216 If REGNO is -1, do this for all registers.
1217 Otherwise, REGNO specifies which register (so we can save time). */
1219 usr_store_inferior_registers (int regno
)
1227 if (regno
>= the_low_target
.num_regs
)
1230 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1233 regaddr
= register_addr (regno
);
1237 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1238 & - sizeof (PTRACE_XFER_TYPE
);
1239 buf
= alloca (size
);
1240 memset (buf
, 0, size
);
1241 if (the_low_target
.left_pad_xfer
1242 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1243 collect_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1244 - register_size (regno
)));
1246 collect_register (regno
, buf
);
1247 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1250 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1251 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1254 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1256 char *err
= strerror (errno
);
1257 char *msg
= alloca (strlen (err
) + 128);
1258 sprintf (msg
, "writing register %d: %s",
1264 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1268 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1269 usr_store_inferior_registers (regno
);
1271 #endif /* HAVE_LINUX_USRREGS */
1275 #ifdef HAVE_LINUX_REGSETS
1278 regsets_fetch_inferior_registers ()
1280 struct regset_info
*regset
;
1281 int saw_general_regs
= 0;
1283 regset
= target_regsets
;
1285 while (regset
->size
>= 0)
1290 if (regset
->size
== 0)
1296 buf
= malloc (regset
->size
);
1297 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1302 /* If we get EIO on the first regset, do not try regsets again.
1303 If we get EIO on a later regset, disable that regset. */
1304 if (regset
== target_regsets
)
1318 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1323 else if (regset
->type
== GENERAL_REGS
)
1324 saw_general_regs
= 1;
1325 regset
->store_function (buf
);
1328 if (saw_general_regs
)
1335 regsets_store_inferior_registers ()
1337 struct regset_info
*regset
;
1338 int saw_general_regs
= 0;
1340 regset
= target_regsets
;
1342 while (regset
->size
>= 0)
1347 if (regset
->size
== 0)
1353 buf
= malloc (regset
->size
);
1355 /* First fill the buffer with the current register set contents,
1356 in case there are any items in the kernel's regset that are
1357 not in gdbserver's regcache. */
1358 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1362 /* Then overlay our cached registers on that. */
1363 regset
->fill_function (buf
);
1365 /* Only now do we write the register set. */
1366 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1373 /* If we get EIO on the first regset, do not try regsets again.
1374 If we get EIO on a later regset, disable that regset. */
1375 if (regset
== target_regsets
)
1388 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1391 else if (regset
->type
== GENERAL_REGS
)
1392 saw_general_regs
= 1;
1396 if (saw_general_regs
)
1403 #endif /* HAVE_LINUX_REGSETS */
1407 linux_fetch_registers (int regno
)
1409 #ifdef HAVE_LINUX_REGSETS
1412 if (regsets_fetch_inferior_registers () == 0)
1416 #ifdef HAVE_LINUX_USRREGS
1417 usr_fetch_inferior_registers (regno
);
1422 linux_store_registers (int regno
)
1424 #ifdef HAVE_LINUX_REGSETS
1427 if (regsets_store_inferior_registers () == 0)
1431 #ifdef HAVE_LINUX_USRREGS
1432 usr_store_inferior_registers (regno
);
1437 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1438 to debugger memory starting at MYADDR. */
1441 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1444 /* Round starting address down to longword boundary. */
1445 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1446 /* Round ending address up; get number of longwords that makes. */
1448 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1449 / sizeof (PTRACE_XFER_TYPE
);
1450 /* Allocate buffer of that many longwords. */
1451 register PTRACE_XFER_TYPE
*buffer
1452 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1454 /* Read all the longwords */
1455 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1458 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1463 /* Copy appropriate bytes out of the buffer. */
1464 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1469 /* Copy LEN bytes of data from debugger memory at MYADDR
1470 to inferior's memory at MEMADDR.
1471 On failure (cannot write the inferior)
1472 returns the value of errno. */
1475 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
1478 /* Round starting address down to longword boundary. */
1479 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1480 /* Round ending address up; get number of longwords that makes. */
1482 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1483 /* Allocate buffer of that many longwords. */
1484 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1489 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1492 /* Fill start and end extra bytes of buffer with existing memory data. */
1494 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1495 (PTRACE_ARG3_TYPE
) addr
, 0);
1500 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1501 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1502 * sizeof (PTRACE_XFER_TYPE
)),
1506 /* Copy data to be written over corresponding part of buffer */
1508 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1510 /* Write the entire buffer. */
1512 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1515 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1524 linux_look_up_symbols (void)
1526 #ifdef USE_THREAD_DB
1530 using_threads
= thread_db_init ();
1535 linux_request_interrupt (void)
1537 extern unsigned long signal_pid
;
1539 if (cont_thread
!= 0 && cont_thread
!= -1)
1541 struct process_info
*process
;
1543 process
= get_thread_process (current_inferior
);
1544 kill_lwp (process
->lwpid
, SIGINT
);
1547 kill_lwp (signal_pid
, SIGINT
);
1550 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1551 to debugger memory starting at MYADDR. */
1554 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
1556 char filename
[PATH_MAX
];
1559 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1561 fd
= open (filename
, O_RDONLY
);
1565 if (offset
!= (CORE_ADDR
) 0
1566 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1569 n
= read (fd
, myaddr
, len
);
1576 /* These watchpoint related wrapper functions simply pass on the function call
1577 if the target has registered a corresponding function. */
1580 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1582 if (the_low_target
.insert_watchpoint
!= NULL
)
1583 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1585 /* Unsupported (see target.h). */
1590 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1592 if (the_low_target
.remove_watchpoint
!= NULL
)
1593 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1595 /* Unsupported (see target.h). */
1600 linux_stopped_by_watchpoint (void)
1602 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1603 return the_low_target
.stopped_by_watchpoint ();
1609 linux_stopped_data_address (void)
1611 if (the_low_target
.stopped_data_address
!= NULL
)
1612 return the_low_target
.stopped_data_address ();
1617 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
1618 #if defined(__mcoldfire__)
1619 /* These should really be defined in the kernel's ptrace.h header. */
1620 #define PT_TEXT_ADDR 49*4
1621 #define PT_DATA_ADDR 50*4
1622 #define PT_TEXT_END_ADDR 51*4
1625 /* Under uClinux, programs are loaded at non-zero offsets, which we need
1626 to tell gdb about. */
1629 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
1631 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1632 unsigned long text
, text_end
, data
;
1633 int pid
= get_thread_process (current_inferior
)->head
.id
;
1637 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
1638 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
1639 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
1643 /* Both text and data offsets produced at compile-time (and so
1644 used by gdb) are relative to the beginning of the program,
1645 with the data segment immediately following the text segment.
1646 However, the actual runtime layout in memory may put the data
1647 somewhere else, so when we send gdb a data base-address, we
1648 use the real data base address and subtract the compile-time
1649 data base-address from it (which is just the length of the
1650 text segment). BSS immediately follows data in both
1653 *data_p
= data
- (text_end
- text
);
1663 linux_arch_string (void)
1665 return the_low_target
.arch_string
;
1668 static struct target_ops linux_target_ops
= {
1669 linux_create_inferior
,
1677 linux_fetch_registers
,
1678 linux_store_registers
,
1681 linux_look_up_symbols
,
1682 linux_request_interrupt
,
1684 linux_insert_watchpoint
,
1685 linux_remove_watchpoint
,
1686 linux_stopped_by_watchpoint
,
1687 linux_stopped_data_address
,
1688 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
1693 #ifdef USE_THREAD_DB
1694 thread_db_get_tls_address
,
1702 linux_init_signals ()
1704 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1705 to find what the cancel signal actually is. */
1706 signal (__SIGRTMIN
+1, SIG_IGN
);
1710 initialize_low (void)
1713 set_target_ops (&linux_target_ops
);
1714 set_breakpoint_data (the_low_target
.breakpoint
,
1715 the_low_target
.breakpoint_len
);
1717 linux_init_signals ();