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 execv (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
);
296 /* Return nonzero if the given thread is still alive. */
298 linux_thread_alive (unsigned long tid
)
300 if (find_inferior_id (&all_threads
, tid
) != NULL
)
306 /* Return nonzero if this process stopped at a breakpoint which
307 no longer appears to be inserted. Also adjust the PC
308 appropriately to resume where the breakpoint used to be. */
310 check_removed_breakpoint (struct process_info
*event_child
)
313 struct thread_info
*saved_inferior
;
315 if (event_child
->pending_is_breakpoint
== 0)
319 fprintf (stderr
, "Checking for breakpoint.\n");
321 saved_inferior
= current_inferior
;
322 current_inferior
= get_process_thread (event_child
);
324 stop_pc
= get_stop_pc ();
326 /* If the PC has changed since we stopped, then we shouldn't do
327 anything. This happens if, for instance, GDB handled the
328 decr_pc_after_break subtraction itself. */
329 if (stop_pc
!= event_child
->pending_stop_pc
)
332 fprintf (stderr
, "Ignoring, PC was changed.\n");
334 event_child
->pending_is_breakpoint
= 0;
335 current_inferior
= saved_inferior
;
339 /* If the breakpoint is still there, we will report hitting it. */
340 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
343 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
344 current_inferior
= saved_inferior
;
349 fprintf (stderr
, "Removed breakpoint.\n");
351 /* For decr_pc_after_break targets, here is where we perform the
352 decrement. We go immediately from this function to resuming,
353 and can not safely call get_stop_pc () again. */
354 if (the_low_target
.set_pc
!= NULL
)
355 (*the_low_target
.set_pc
) (stop_pc
);
357 /* We consumed the pending SIGTRAP. */
358 event_child
->pending_is_breakpoint
= 0;
359 event_child
->status_pending_p
= 0;
360 event_child
->status_pending
= 0;
362 current_inferior
= saved_inferior
;
366 /* Return 1 if this process has an interesting status pending. This function
367 may silently resume an inferior process. */
369 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
371 struct process_info
*process
= (struct process_info
*) entry
;
373 if (process
->status_pending_p
)
374 if (check_removed_breakpoint (process
))
376 /* This thread was stopped at a breakpoint, and the breakpoint
377 is now gone. We were told to continue (or step...) all threads,
378 so GDB isn't trying to single-step past this breakpoint.
379 So instead of reporting the old SIGTRAP, pretend we got to
380 the breakpoint just after it was removed instead of just
381 before; resume the process. */
382 linux_resume_one_process (&process
->head
, 0, 0, NULL
);
386 return process
->status_pending_p
;
390 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
393 int to_wait_for
= -1;
396 to_wait_for
= (*childp
)->lwpid
;
400 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
405 perror_with_name ("waitpid");
410 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
415 perror_with_name ("waitpid (WCLONE)");
424 && (!WIFSTOPPED (*wstatp
)
425 || (WSTOPSIG (*wstatp
) != 32
426 && WSTOPSIG (*wstatp
) != 33)))
427 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
429 if (to_wait_for
== -1)
430 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
432 (*childp
)->stopped
= 1;
433 (*childp
)->pending_is_breakpoint
= 0;
435 (*childp
)->last_status
= *wstatp
;
438 && WIFSTOPPED (*wstatp
))
440 current_inferior
= (struct thread_info
*)
441 find_inferior_id (&all_threads
, (*childp
)->tid
);
442 /* For testing only; i386_stop_pc prints out a diagnostic. */
443 if (the_low_target
.get_pc
!= NULL
)
449 linux_wait_for_event (struct thread_info
*child
)
452 struct process_info
*event_child
;
455 /* Check for a process with a pending status. */
456 /* It is possible that the user changed the pending task's registers since
457 it stopped. We correctly handle the change of PC if we hit a breakpoint
458 (in check_removed_breakpoint); signals should be reported anyway. */
461 event_child
= (struct process_info
*)
462 find_inferior (&all_processes
, status_pending_p
, NULL
);
463 if (debug_threads
&& event_child
)
464 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
468 event_child
= get_thread_process (child
);
469 if (event_child
->status_pending_p
470 && check_removed_breakpoint (event_child
))
474 if (event_child
!= NULL
)
476 if (event_child
->status_pending_p
)
479 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
480 event_child
->lwpid
, event_child
->status_pending
);
481 wstat
= event_child
->status_pending
;
482 event_child
->status_pending_p
= 0;
483 event_child
->status_pending
= 0;
484 current_inferior
= get_process_thread (event_child
);
489 /* We only enter this loop if no process has a pending wait status. Thus
490 any action taken in response to a wait status inside this loop is
491 responding as soon as we detect the status, not after any pending
498 event_child
= get_thread_process (child
);
500 linux_wait_for_process (&event_child
, &wstat
);
502 if (event_child
== NULL
)
503 error ("event from unknown child");
505 current_inferior
= (struct thread_info
*)
506 find_inferior_id (&all_threads
, event_child
->tid
);
508 /* Check for thread exit. */
509 if (using_threads
&& ! WIFSTOPPED (wstat
))
512 fprintf (stderr
, "Thread %ld (LWP %ld) exiting\n",
513 event_child
->tid
, event_child
->head
.id
);
515 /* If the last thread is exiting, just return. */
516 if (all_threads
.head
== all_threads
.tail
)
519 dead_thread_notify (event_child
->tid
);
521 remove_inferior (&all_processes
, &event_child
->head
);
523 remove_thread (current_inferior
);
524 current_inferior
= (struct thread_info
*) all_threads
.head
;
526 /* If we were waiting for this particular child to do something...
527 well, it did something. */
531 /* Wait for a more interesting event. */
536 && WIFSTOPPED (wstat
)
537 && WSTOPSIG (wstat
) == SIGSTOP
538 && event_child
->stop_expected
)
541 fprintf (stderr
, "Expected stop.\n");
542 event_child
->stop_expected
= 0;
543 linux_resume_one_process (&event_child
->head
,
544 event_child
->stepping
, 0, NULL
);
548 /* If GDB is not interested in this signal, don't stop other
549 threads, and don't report it to GDB. Just resume the
550 inferior right away. We do this for threading-related
551 signals as well as any that GDB specifically requested
552 we ignore. But never ignore SIGSTOP if we sent it
554 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
556 if (WIFSTOPPED (wstat
)
557 && ((using_threads
&& (WSTOPSIG (wstat
) == __SIGRTMIN
558 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
559 || (pass_signals
[target_signal_from_host (WSTOPSIG (wstat
))]
560 && (WSTOPSIG (wstat
) != SIGSTOP
561 || !event_child
->sigstop_sent
))))
563 siginfo_t info
, *info_p
;
566 fprintf (stderr
, "Ignored signal %d for %ld (LWP %ld).\n",
567 WSTOPSIG (wstat
), event_child
->tid
,
568 event_child
->head
.id
);
570 if (ptrace (PTRACE_GETSIGINFO
, event_child
->lwpid
, 0, &info
) == 0)
574 linux_resume_one_process (&event_child
->head
,
575 event_child
->stepping
,
576 WSTOPSIG (wstat
), info_p
);
580 /* If this event was not handled above, and is not a SIGTRAP, report
582 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
585 /* If this target does not support breakpoints, we simply report the
586 SIGTRAP; it's of no concern to us. */
587 if (the_low_target
.get_pc
== NULL
)
590 stop_pc
= get_stop_pc ();
592 /* bp_reinsert will only be set if we were single-stepping.
593 Notice that we will resume the process after hitting
594 a gdbserver breakpoint; single-stepping to/over one
595 is not supported (yet). */
596 if (event_child
->bp_reinsert
!= 0)
599 fprintf (stderr
, "Reinserted breakpoint.\n");
600 reinsert_breakpoint (event_child
->bp_reinsert
);
601 event_child
->bp_reinsert
= 0;
603 /* Clear the single-stepping flag and SIGTRAP as we resume. */
604 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
609 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
611 if (check_breakpoints (stop_pc
) != 0)
613 /* We hit one of our own breakpoints. We mark it as a pending
614 breakpoint, so that check_removed_breakpoint () will do the PC
615 adjustment for us at the appropriate time. */
616 event_child
->pending_is_breakpoint
= 1;
617 event_child
->pending_stop_pc
= stop_pc
;
619 /* Now we need to put the breakpoint back. We continue in the event
620 loop instead of simply replacing the breakpoint right away,
621 in order to not lose signals sent to the thread that hit the
622 breakpoint. Unfortunately this increases the window where another
623 thread could sneak past the removed breakpoint. For the current
624 use of server-side breakpoints (thread creation) this is
625 acceptable; but it needs to be considered before this breakpoint
626 mechanism can be used in more general ways. For some breakpoints
627 it may be necessary to stop all other threads, but that should
628 be avoided where possible.
630 If breakpoint_reinsert_addr is NULL, that means that we can
631 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
632 mark it for reinsertion, and single-step.
634 Otherwise, call the target function to figure out where we need
635 our temporary breakpoint, create it, and continue executing this
637 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
639 event_child
->bp_reinsert
= stop_pc
;
640 uninsert_breakpoint (stop_pc
);
641 linux_resume_one_process (&event_child
->head
, 1, 0, NULL
);
645 reinsert_breakpoint_by_bp
646 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
647 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
653 /* If we were single-stepping, we definitely want to report the
654 SIGTRAP. The single-step operation has completed, so also
655 clear the stepping flag; in general this does not matter,
656 because the SIGTRAP will be reported to the client, which
657 will give us a new action for this thread, but clear it for
658 consistency anyway. It's safe to clear the stepping flag
659 because the only consumer of get_stop_pc () after this point
660 is check_removed_breakpoint, and pending_is_breakpoint is not
661 set. It might be wiser to use a step_completed flag instead. */
662 if (event_child
->stepping
)
664 event_child
->stepping
= 0;
668 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
669 Check if it is a breakpoint, and if so mark the process information
670 accordingly. This will handle both the necessary fiddling with the
671 PC on decr_pc_after_break targets and suppressing extra threads
672 hitting a breakpoint if two hit it at once and then GDB removes it
673 after the first is reported. Arguably it would be better to report
674 multiple threads hitting breakpoints simultaneously, but the current
675 remote protocol does not allow this. */
676 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
678 event_child
->pending_is_breakpoint
= 1;
679 event_child
->pending_stop_pc
= stop_pc
;
689 /* Wait for process, returns status. */
692 linux_wait (char *status
)
695 struct thread_info
*child
= NULL
;
698 /* If we were only supposed to resume one thread, only wait for
699 that thread - if it's still alive. If it died, however - which
700 can happen if we're coming from the thread death case below -
701 then we need to make sure we restart the other threads. We could
702 pick a thread at random or restart all; restarting all is less
704 if (cont_thread
!= 0 && cont_thread
!= -1)
706 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
709 /* No stepping, no signal - unless one is pending already, of course. */
712 struct thread_resume resume_info
;
713 resume_info
.thread
= -1;
714 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
715 linux_resume (&resume_info
);
721 w
= linux_wait_for_event (child
);
722 stop_all_processes ();
725 /* If we are waiting for a particular child, and it exited,
726 linux_wait_for_event will return its exit status. Similarly if
727 the last child exited. If this is not the last child, however,
728 do not report it as exited until there is a 'thread exited' response
729 available in the remote protocol. Instead, just wait for another event.
730 This should be safe, because if the thread crashed we will already
731 have reported the termination signal to GDB; that should stop any
732 in-progress stepping operations, etc.
734 Report the exit status of the last thread to exit. This matches
735 LinuxThreads' behavior. */
737 if (all_threads
.head
== all_threads
.tail
)
741 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
744 free (all_processes
.head
);
745 all_processes
.head
= all_processes
.tail
= NULL
;
746 return WEXITSTATUS (w
);
748 else if (!WIFSTOPPED (w
))
750 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
753 free (all_processes
.head
);
754 all_processes
.head
= all_processes
.tail
= NULL
;
755 return target_signal_from_host (WTERMSIG (w
));
765 return target_signal_from_host (WSTOPSIG (w
));
768 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
769 thread groups are in use, we need to use tkill. */
772 kill_lwp (unsigned long lwpid
, int signo
)
774 static int tkill_failed
;
781 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
789 return kill (lwpid
, signo
);
793 send_sigstop (struct inferior_list_entry
*entry
)
795 struct process_info
*process
= (struct process_info
*) entry
;
797 if (process
->stopped
)
800 /* If we already have a pending stop signal for this process, don't
802 if (process
->stop_expected
)
804 process
->stop_expected
= 0;
809 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
811 kill_lwp (process
->head
.id
, SIGSTOP
);
812 process
->sigstop_sent
= 1;
816 wait_for_sigstop (struct inferior_list_entry
*entry
)
818 struct process_info
*process
= (struct process_info
*) entry
;
819 struct thread_info
*saved_inferior
, *thread
;
821 unsigned long saved_tid
;
823 if (process
->stopped
)
826 saved_inferior
= current_inferior
;
827 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
828 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
830 wstat
= linux_wait_for_event (thread
);
832 /* If we stopped with a non-SIGSTOP signal, save it for later
833 and record the pending SIGSTOP. If the process exited, just
835 if (WIFSTOPPED (wstat
)
836 && WSTOPSIG (wstat
) != SIGSTOP
)
839 fprintf (stderr
, "Stopped with non-sigstop signal\n");
840 process
->status_pending_p
= 1;
841 process
->status_pending
= wstat
;
842 process
->stop_expected
= 1;
845 if (linux_thread_alive (saved_tid
))
846 current_inferior
= saved_inferior
;
850 fprintf (stderr
, "Previously current thread died.\n");
852 /* Set a valid thread as current. */
853 set_desired_inferior (0);
858 stop_all_processes (void)
860 stopping_threads
= 1;
861 for_each_inferior (&all_processes
, send_sigstop
);
862 for_each_inferior (&all_processes
, wait_for_sigstop
);
863 stopping_threads
= 0;
866 /* Resume execution of the inferior process.
867 If STEP is nonzero, single-step it.
868 If SIGNAL is nonzero, give it that signal. */
871 linux_resume_one_process (struct inferior_list_entry
*entry
,
872 int step
, int signal
, siginfo_t
*info
)
874 struct process_info
*process
= (struct process_info
*) entry
;
875 struct thread_info
*saved_inferior
;
877 if (process
->stopped
== 0)
880 /* If we have pending signals or status, and a new signal, enqueue the
881 signal. Also enqueue the signal if we are waiting to reinsert a
882 breakpoint; it will be picked up again below. */
884 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
885 || process
->bp_reinsert
!= 0))
887 struct pending_signals
*p_sig
;
888 p_sig
= malloc (sizeof (*p_sig
));
889 p_sig
->prev
= process
->pending_signals
;
890 p_sig
->signal
= signal
;
892 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
894 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
895 process
->pending_signals
= p_sig
;
898 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
901 saved_inferior
= current_inferior
;
902 current_inferior
= get_process_thread (process
);
905 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
906 step
? "step" : "continue", signal
,
907 process
->stop_expected
? "expected" : "not expected");
909 /* This bit needs some thinking about. If we get a signal that
910 we must report while a single-step reinsert is still pending,
911 we often end up resuming the thread. It might be better to
912 (ew) allow a stack of pending events; then we could be sure that
913 the reinsert happened right away and not lose any signals.
915 Making this stack would also shrink the window in which breakpoints are
916 uninserted (see comment in linux_wait_for_process) but not enough for
917 complete correctness, so it won't solve that problem. It may be
918 worthwhile just to solve this one, however. */
919 if (process
->bp_reinsert
!= 0)
922 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
924 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
927 /* Postpone any pending signal. It was enqueued above. */
931 check_removed_breakpoint (process
);
933 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
935 fprintf (stderr
, " ");
936 (*the_low_target
.get_pc
) ();
939 /* If we have pending signals, consume one unless we are trying to reinsert
941 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
943 struct pending_signals
**p_sig
;
945 p_sig
= &process
->pending_signals
;
946 while ((*p_sig
)->prev
!= NULL
)
947 p_sig
= &(*p_sig
)->prev
;
949 signal
= (*p_sig
)->signal
;
950 if ((*p_sig
)->info
.si_signo
!= 0)
951 ptrace (PTRACE_SETSIGINFO
, process
->lwpid
, 0, &(*p_sig
)->info
);
957 regcache_invalidate_one ((struct inferior_list_entry
*)
958 get_process_thread (process
));
960 process
->stopped
= 0;
961 process
->stepping
= step
;
962 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
964 current_inferior
= saved_inferior
;
966 perror_with_name ("ptrace");
969 static struct thread_resume
*resume_ptr
;
971 /* This function is called once per thread. We look up the thread
972 in RESUME_PTR, and mark the thread with a pointer to the appropriate
975 This algorithm is O(threads * resume elements), but resume elements
976 is small (and will remain small at least until GDB supports thread
979 linux_set_resume_request (struct inferior_list_entry
*entry
)
981 struct process_info
*process
;
982 struct thread_info
*thread
;
985 thread
= (struct thread_info
*) entry
;
986 process
= get_thread_process (thread
);
989 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
992 process
->resume
= &resume_ptr
[ndx
];
995 /* This function is called once per thread. We check the thread's resume
996 request, which will tell us whether to resume, step, or leave the thread
997 stopped; and what signal, if any, it should be sent. For threads which
998 we aren't explicitly told otherwise, we preserve the stepping flag; this
999 is used for stepping over gdbserver-placed breakpoints. */
1002 linux_continue_one_thread (struct inferior_list_entry
*entry
)
1004 struct process_info
*process
;
1005 struct thread_info
*thread
;
1008 thread
= (struct thread_info
*) entry
;
1009 process
= get_thread_process (thread
);
1011 if (process
->resume
->leave_stopped
)
1014 if (process
->resume
->thread
== -1)
1015 step
= process
->stepping
|| process
->resume
->step
;
1017 step
= process
->resume
->step
;
1019 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
, NULL
);
1021 process
->resume
= NULL
;
1024 /* This function is called once per thread. We check the thread's resume
1025 request, which will tell us whether to resume, step, or leave the thread
1026 stopped; and what signal, if any, it should be sent. We queue any needed
1027 signals, since we won't actually resume. We already have a pending event
1028 to report, so we don't need to preserve any step requests; they should
1029 be re-issued if necessary. */
1032 linux_queue_one_thread (struct inferior_list_entry
*entry
)
1034 struct process_info
*process
;
1035 struct thread_info
*thread
;
1037 thread
= (struct thread_info
*) entry
;
1038 process
= get_thread_process (thread
);
1040 if (process
->resume
->leave_stopped
)
1043 /* If we have a new signal, enqueue the signal. */
1044 if (process
->resume
->sig
!= 0)
1046 struct pending_signals
*p_sig
;
1047 p_sig
= malloc (sizeof (*p_sig
));
1048 p_sig
->prev
= process
->pending_signals
;
1049 p_sig
->signal
= process
->resume
->sig
;
1050 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1052 /* If this is the same signal we were previously stopped by,
1053 make sure to queue its siginfo. We can ignore the return
1054 value of ptrace; if it fails, we'll skip
1055 PTRACE_SETSIGINFO. */
1056 if (WIFSTOPPED (process
->last_status
)
1057 && WSTOPSIG (process
->last_status
) == process
->resume
->sig
)
1058 ptrace (PTRACE_GETSIGINFO
, process
->lwpid
, 0, &p_sig
->info
);
1060 process
->pending_signals
= p_sig
;
1063 process
->resume
= NULL
;
1066 /* Set DUMMY if this process has an interesting status pending. */
1068 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1070 struct process_info
*process
= (struct process_info
*) entry
;
1072 /* Processes which will not be resumed are not interesting, because
1073 we might not wait for them next time through linux_wait. */
1074 if (process
->resume
->leave_stopped
)
1077 /* If this thread has a removed breakpoint, we won't have any
1078 events to report later, so check now. check_removed_breakpoint
1079 may clear status_pending_p. We avoid calling check_removed_breakpoint
1080 for any thread that we are not otherwise going to resume - this
1081 lets us preserve stopped status when two threads hit a breakpoint.
1082 GDB removes the breakpoint to single-step a particular thread
1083 past it, then re-inserts it and resumes all threads. We want
1084 to report the second thread without resuming it in the interim. */
1085 if (process
->status_pending_p
)
1086 check_removed_breakpoint (process
);
1088 if (process
->status_pending_p
)
1089 * (int *) flag_p
= 1;
1095 linux_resume (struct thread_resume
*resume_info
)
1099 /* Yes, the use of a global here is rather ugly. */
1100 resume_ptr
= resume_info
;
1102 for_each_inferior (&all_threads
, linux_set_resume_request
);
1104 /* If there is a thread which would otherwise be resumed, which
1105 has a pending status, then don't resume any threads - we can just
1106 report the pending status. Make sure to queue any signals
1107 that would otherwise be sent. */
1109 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1114 fprintf (stderr
, "Not resuming, pending status\n");
1116 fprintf (stderr
, "Resuming, no pending status\n");
1120 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1125 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1129 #ifdef HAVE_LINUX_USRREGS
1132 register_addr (int regnum
)
1136 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1137 error ("Invalid register number %d.", regnum
);
1139 addr
= the_low_target
.regmap
[regnum
];
1144 /* Fetch one register. */
1146 fetch_register (int regno
)
1152 if (regno
>= the_low_target
.num_regs
)
1154 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1157 regaddr
= register_addr (regno
);
1160 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1161 & - sizeof (PTRACE_XFER_TYPE
);
1162 buf
= alloca (size
);
1163 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1166 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1167 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1168 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1171 /* Warning, not error, in case we are attached; sometimes the
1172 kernel doesn't let us at the registers. */
1173 char *err
= strerror (errno
);
1174 char *msg
= alloca (strlen (err
) + 128);
1175 sprintf (msg
, "reading register %d: %s", regno
, err
);
1180 if (the_low_target
.left_pad_xfer
1181 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1182 supply_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1183 - register_size (regno
)));
1185 supply_register (regno
, buf
);
1190 /* Fetch all registers, or just one, from the child process. */
1192 usr_fetch_inferior_registers (int regno
)
1194 if (regno
== -1 || regno
== 0)
1195 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1196 fetch_register (regno
);
1198 fetch_register (regno
);
1201 /* Store our register values back into the inferior.
1202 If REGNO is -1, do this for all registers.
1203 Otherwise, REGNO specifies which register (so we can save time). */
1205 usr_store_inferior_registers (int regno
)
1213 if (regno
>= the_low_target
.num_regs
)
1216 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1219 regaddr
= register_addr (regno
);
1223 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1224 & - sizeof (PTRACE_XFER_TYPE
);
1225 buf
= alloca (size
);
1226 memset (buf
, 0, size
);
1227 if (the_low_target
.left_pad_xfer
1228 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1229 collect_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1230 - register_size (regno
)));
1232 collect_register (regno
, buf
);
1233 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1236 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1237 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1240 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1242 char *err
= strerror (errno
);
1243 char *msg
= alloca (strlen (err
) + 128);
1244 sprintf (msg
, "writing register %d: %s",
1250 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1254 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1255 usr_store_inferior_registers (regno
);
1257 #endif /* HAVE_LINUX_USRREGS */
1261 #ifdef HAVE_LINUX_REGSETS
1264 regsets_fetch_inferior_registers ()
1266 struct regset_info
*regset
;
1267 int saw_general_regs
= 0;
1269 regset
= target_regsets
;
1271 while (regset
->size
>= 0)
1276 if (regset
->size
== 0)
1282 buf
= malloc (regset
->size
);
1283 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1288 /* If we get EIO on the first regset, do not try regsets again.
1289 If we get EIO on a later regset, disable that regset. */
1290 if (regset
== target_regsets
)
1304 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1309 else if (regset
->type
== GENERAL_REGS
)
1310 saw_general_regs
= 1;
1311 regset
->store_function (buf
);
1314 if (saw_general_regs
)
1321 regsets_store_inferior_registers ()
1323 struct regset_info
*regset
;
1324 int saw_general_regs
= 0;
1326 regset
= target_regsets
;
1328 while (regset
->size
>= 0)
1333 if (regset
->size
== 0)
1339 buf
= malloc (regset
->size
);
1341 /* First fill the buffer with the current register set contents,
1342 in case there are any items in the kernel's regset that are
1343 not in gdbserver's regcache. */
1344 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1348 /* Then overlay our cached registers on that. */
1349 regset
->fill_function (buf
);
1351 /* Only now do we write the register set. */
1352 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1359 /* If we get EIO on the first regset, do not try regsets again.
1360 If we get EIO on a later regset, disable that regset. */
1361 if (regset
== target_regsets
)
1374 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1377 else if (regset
->type
== GENERAL_REGS
)
1378 saw_general_regs
= 1;
1382 if (saw_general_regs
)
1389 #endif /* HAVE_LINUX_REGSETS */
1393 linux_fetch_registers (int regno
)
1395 #ifdef HAVE_LINUX_REGSETS
1398 if (regsets_fetch_inferior_registers () == 0)
1402 #ifdef HAVE_LINUX_USRREGS
1403 usr_fetch_inferior_registers (regno
);
1408 linux_store_registers (int regno
)
1410 #ifdef HAVE_LINUX_REGSETS
1413 if (regsets_store_inferior_registers () == 0)
1417 #ifdef HAVE_LINUX_USRREGS
1418 usr_store_inferior_registers (regno
);
1423 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1424 to debugger memory starting at MYADDR. */
1427 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1430 /* Round starting address down to longword boundary. */
1431 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1432 /* Round ending address up; get number of longwords that makes. */
1434 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1435 / sizeof (PTRACE_XFER_TYPE
);
1436 /* Allocate buffer of that many longwords. */
1437 register PTRACE_XFER_TYPE
*buffer
1438 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1440 /* Read all the longwords */
1441 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1444 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1449 /* Copy appropriate bytes out of the buffer. */
1450 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1455 /* Copy LEN bytes of data from debugger memory at MYADDR
1456 to inferior's memory at MEMADDR.
1457 On failure (cannot write the inferior)
1458 returns the value of errno. */
1461 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
1464 /* Round starting address down to longword boundary. */
1465 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1466 /* Round ending address up; get number of longwords that makes. */
1468 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1469 /* Allocate buffer of that many longwords. */
1470 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1475 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1478 /* Fill start and end extra bytes of buffer with existing memory data. */
1480 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1481 (PTRACE_ARG3_TYPE
) addr
, 0);
1486 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1487 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1488 * sizeof (PTRACE_XFER_TYPE
)),
1492 /* Copy data to be written over corresponding part of buffer */
1494 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1496 /* Write the entire buffer. */
1498 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1501 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1510 linux_look_up_symbols (void)
1512 #ifdef USE_THREAD_DB
1516 using_threads
= thread_db_init ();
1521 linux_request_interrupt (void)
1523 extern unsigned long signal_pid
;
1525 if (cont_thread
!= 0 && cont_thread
!= -1)
1527 struct process_info
*process
;
1529 process
= get_thread_process (current_inferior
);
1530 kill_lwp (process
->lwpid
, SIGINT
);
1533 kill_lwp (signal_pid
, SIGINT
);
1536 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1537 to debugger memory starting at MYADDR. */
1540 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
1542 char filename
[PATH_MAX
];
1545 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1547 fd
= open (filename
, O_RDONLY
);
1551 if (offset
!= (CORE_ADDR
) 0
1552 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1555 n
= read (fd
, myaddr
, len
);
1562 /* These watchpoint related wrapper functions simply pass on the function call
1563 if the target has registered a corresponding function. */
1566 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1568 if (the_low_target
.insert_watchpoint
!= NULL
)
1569 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1571 /* Unsupported (see target.h). */
1576 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1578 if (the_low_target
.remove_watchpoint
!= NULL
)
1579 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1581 /* Unsupported (see target.h). */
1586 linux_stopped_by_watchpoint (void)
1588 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1589 return the_low_target
.stopped_by_watchpoint ();
1595 linux_stopped_data_address (void)
1597 if (the_low_target
.stopped_data_address
!= NULL
)
1598 return the_low_target
.stopped_data_address ();
1603 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
1604 #if defined(__mcoldfire__)
1605 /* These should really be defined in the kernel's ptrace.h header. */
1606 #define PT_TEXT_ADDR 49*4
1607 #define PT_DATA_ADDR 50*4
1608 #define PT_TEXT_END_ADDR 51*4
1611 /* Under uClinux, programs are loaded at non-zero offsets, which we need
1612 to tell gdb about. */
1615 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
1617 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1618 unsigned long text
, text_end
, data
;
1619 int pid
= get_thread_process (current_inferior
)->head
.id
;
1623 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
1624 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
1625 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
1629 /* Both text and data offsets produced at compile-time (and so
1630 used by gdb) are relative to the beginning of the program,
1631 with the data segment immediately following the text segment.
1632 However, the actual runtime layout in memory may put the data
1633 somewhere else, so when we send gdb a data base-address, we
1634 use the real data base address and subtract the compile-time
1635 data base-address from it (which is just the length of the
1636 text segment). BSS immediately follows data in both
1639 *data_p
= data
- (text_end
- text
);
1649 linux_arch_string (void)
1651 return the_low_target
.arch_string
;
1654 static struct target_ops linux_target_ops
= {
1655 linux_create_inferior
,
1662 linux_fetch_registers
,
1663 linux_store_registers
,
1666 linux_look_up_symbols
,
1667 linux_request_interrupt
,
1669 linux_insert_watchpoint
,
1670 linux_remove_watchpoint
,
1671 linux_stopped_by_watchpoint
,
1672 linux_stopped_data_address
,
1673 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
1678 #ifdef USE_THREAD_DB
1679 thread_db_get_tls_address
,
1687 linux_init_signals ()
1689 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1690 to find what the cancel signal actually is. */
1691 signal (__SIGRTMIN
+1, SIG_IGN
);
1695 initialize_low (void)
1698 set_target_ops (&linux_target_ops
);
1699 set_breakpoint_data (the_low_target
.breakpoint
,
1700 the_low_target
.breakpoint_len
);
1702 linux_init_signals ();