1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
23 #include "linux-low.h"
27 #include <sys/param.h>
29 #include <sys/ptrace.h>
32 #include <sys/ioctl.h>
38 #include <sys/syscall.h>
40 #ifndef PTRACE_GETSIGINFO
41 # define PTRACE_GETSIGINFO 0x4202
42 # define PTRACE_SETSIGINFO 0x4203
45 /* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
46 however. This requires changing the ID in place when we go from !using_threads
47 to using_threads, immediately.
49 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
50 the same as the LWP ID. */
52 struct inferior_list all_processes
;
54 /* FIXME this is a bit of a hack, and could be removed. */
57 /* FIXME make into a target method? */
60 static void linux_resume_one_process (struct inferior_list_entry
*entry
,
61 int step
, int signal
, siginfo_t
*info
);
62 static void linux_resume (struct thread_resume
*resume_info
);
63 static void stop_all_processes (void);
64 static int linux_wait_for_event (struct thread_info
*child
);
66 struct pending_signals
70 struct pending_signals
*prev
;
73 #define PTRACE_ARG3_TYPE long
74 #define PTRACE_XFER_TYPE long
76 #ifdef HAVE_LINUX_REGSETS
77 static int use_regsets_p
= 1;
80 int debug_threads
= 0;
82 #define pid_of(proc) ((proc)->head.id)
84 /* FIXME: Delete eventually. */
85 #define inferior_pid (pid_of (get_thread_process (current_inferior)))
87 /* This function should only be called if the process got a SIGTRAP.
88 The SIGTRAP could mean several things.
90 On i386, where decr_pc_after_break is non-zero:
91 If we were single-stepping this process using PTRACE_SINGLESTEP,
92 we will get only the one SIGTRAP (even if the instruction we
93 stepped over was a breakpoint). The value of $eip will be the
95 If we continue the process using PTRACE_CONT, we will get a
96 SIGTRAP when we hit a breakpoint. The value of $eip will be
97 the instruction after the breakpoint (i.e. needs to be
98 decremented). If we report the SIGTRAP to GDB, we must also
99 report the undecremented PC. If we cancel the SIGTRAP, we
100 must resume at the decremented PC.
102 (Presumably, not yet tested) On a non-decr_pc_after_break machine
103 with hardware or kernel single-step:
104 If we single-step over a breakpoint instruction, our PC will
105 point at the following instruction. If we continue and hit a
106 breakpoint instruction, our PC will point at the breakpoint
112 CORE_ADDR stop_pc
= (*the_low_target
.get_pc
) ();
114 if (get_thread_process (current_inferior
)->stepping
)
117 return stop_pc
- the_low_target
.decr_pc_after_break
;
121 add_process (unsigned long pid
)
123 struct process_info
*process
;
125 process
= (struct process_info
*) malloc (sizeof (*process
));
126 memset (process
, 0, sizeof (*process
));
128 process
->head
.id
= pid
;
130 /* Default to tid == lwpid == pid. */
132 process
->lwpid
= pid
;
134 add_inferior_to_list (&all_processes
, &process
->head
);
139 /* Start an inferior process and returns its pid.
140 ALLARGS is a vector of program-name and args. */
143 linux_create_inferior (char *program
, char **allargs
)
148 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
154 perror_with_name ("fork");
158 ptrace (PTRACE_TRACEME
, 0, 0, 0);
160 signal (__SIGRTMIN
+ 1, SIG_DFL
);
164 execv (program
, allargs
);
166 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
172 new_process
= add_process (pid
);
173 add_thread (pid
, new_process
, pid
);
178 /* Attach to an inferior process. */
181 linux_attach_lwp (unsigned long pid
, unsigned long tid
)
183 struct process_info
*new_process
;
185 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
187 fprintf (stderr
, "Cannot attach to process %ld: %s (%d)\n", pid
,
188 strerror (errno
), errno
);
191 /* If we fail to attach to an LWP, just return. */
197 new_process
= (struct process_info
*) add_process (pid
);
198 add_thread (tid
, new_process
, pid
);
200 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
201 brings it to a halt. We should ignore that SIGSTOP and resume the process
202 (unless this is the first process, in which case the flag will be cleared
205 On the other hand, if we are currently trying to stop all threads, we
206 should treat the new thread as if we had sent it a SIGSTOP. This works
207 because we are guaranteed that add_process added us to the end of the
208 list, and so the new thread has not yet reached wait_for_sigstop (but
210 if (! stopping_threads
)
211 new_process
->stop_expected
= 1;
215 linux_attach (unsigned long pid
)
217 struct process_info
*process
;
219 linux_attach_lwp (pid
, pid
);
221 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
222 process
= (struct process_info
*) find_inferior_id (&all_processes
, pid
);
223 process
->stop_expected
= 0;
228 /* Kill the inferior process. Make us have no inferior. */
231 linux_kill_one_process (struct inferior_list_entry
*entry
)
233 struct thread_info
*thread
= (struct thread_info
*) entry
;
234 struct process_info
*process
= get_thread_process (thread
);
237 /* We avoid killing the first thread here, because of a Linux kernel (at
238 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
239 the children get a chance to be reaped, it will remain a zombie
241 if (entry
== all_threads
.head
)
246 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
248 /* Make sure it died. The loop is most likely unnecessary. */
249 wstat
= linux_wait_for_event (thread
);
250 } while (WIFSTOPPED (wstat
));
256 struct thread_info
*thread
= (struct thread_info
*) all_threads
.head
;
257 struct process_info
*process
;
263 for_each_inferior (&all_threads
, linux_kill_one_process
);
265 /* See the comment in linux_kill_one_process. We did not kill the first
266 thread in the list, so do so now. */
267 process
= get_thread_process (thread
);
270 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
272 /* Make sure it died. The loop is most likely unnecessary. */
273 wstat
= linux_wait_for_event (thread
);
274 } while (WIFSTOPPED (wstat
));
278 linux_detach_one_process (struct inferior_list_entry
*entry
)
280 struct thread_info
*thread
= (struct thread_info
*) entry
;
281 struct process_info
*process
= get_thread_process (thread
);
283 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
289 for_each_inferior (&all_threads
, linux_detach_one_process
);
292 /* Return nonzero if the given thread is still alive. */
294 linux_thread_alive (unsigned long tid
)
296 if (find_inferior_id (&all_threads
, tid
) != NULL
)
302 /* Return nonzero if this process stopped at a breakpoint which
303 no longer appears to be inserted. Also adjust the PC
304 appropriately to resume where the breakpoint used to be. */
306 check_removed_breakpoint (struct process_info
*event_child
)
309 struct thread_info
*saved_inferior
;
311 if (event_child
->pending_is_breakpoint
== 0)
315 fprintf (stderr
, "Checking for breakpoint.\n");
317 saved_inferior
= current_inferior
;
318 current_inferior
= get_process_thread (event_child
);
320 stop_pc
= get_stop_pc ();
322 /* If the PC has changed since we stopped, then we shouldn't do
323 anything. This happens if, for instance, GDB handled the
324 decr_pc_after_break subtraction itself. */
325 if (stop_pc
!= event_child
->pending_stop_pc
)
328 fprintf (stderr
, "Ignoring, PC was changed.\n");
330 event_child
->pending_is_breakpoint
= 0;
331 current_inferior
= saved_inferior
;
335 /* If the breakpoint is still there, we will report hitting it. */
336 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
339 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
340 current_inferior
= saved_inferior
;
345 fprintf (stderr
, "Removed breakpoint.\n");
347 /* For decr_pc_after_break targets, here is where we perform the
348 decrement. We go immediately from this function to resuming,
349 and can not safely call get_stop_pc () again. */
350 if (the_low_target
.set_pc
!= NULL
)
351 (*the_low_target
.set_pc
) (stop_pc
);
353 /* We consumed the pending SIGTRAP. */
354 event_child
->pending_is_breakpoint
= 0;
355 event_child
->status_pending_p
= 0;
356 event_child
->status_pending
= 0;
358 current_inferior
= saved_inferior
;
362 /* Return 1 if this process has an interesting status pending. This function
363 may silently resume an inferior process. */
365 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
367 struct process_info
*process
= (struct process_info
*) entry
;
369 if (process
->status_pending_p
)
370 if (check_removed_breakpoint (process
))
372 /* This thread was stopped at a breakpoint, and the breakpoint
373 is now gone. We were told to continue (or step...) all threads,
374 so GDB isn't trying to single-step past this breakpoint.
375 So instead of reporting the old SIGTRAP, pretend we got to
376 the breakpoint just after it was removed instead of just
377 before; resume the process. */
378 linux_resume_one_process (&process
->head
, 0, 0, NULL
);
382 return process
->status_pending_p
;
386 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
389 int to_wait_for
= -1;
392 to_wait_for
= (*childp
)->lwpid
;
396 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
401 perror_with_name ("waitpid");
406 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
411 perror_with_name ("waitpid (WCLONE)");
420 && (!WIFSTOPPED (*wstatp
)
421 || (WSTOPSIG (*wstatp
) != 32
422 && WSTOPSIG (*wstatp
) != 33)))
423 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
425 if (to_wait_for
== -1)
426 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
428 (*childp
)->stopped
= 1;
429 (*childp
)->pending_is_breakpoint
= 0;
431 (*childp
)->last_status
= *wstatp
;
434 && WIFSTOPPED (*wstatp
))
436 current_inferior
= (struct thread_info
*)
437 find_inferior_id (&all_threads
, (*childp
)->tid
);
438 /* For testing only; i386_stop_pc prints out a diagnostic. */
439 if (the_low_target
.get_pc
!= NULL
)
445 linux_wait_for_event (struct thread_info
*child
)
448 struct process_info
*event_child
;
451 /* Check for a process with a pending status. */
452 /* It is possible that the user changed the pending task's registers since
453 it stopped. We correctly handle the change of PC if we hit a breakpoint
454 (in check_removed_breakpoint); signals should be reported anyway. */
457 event_child
= (struct process_info
*)
458 find_inferior (&all_processes
, status_pending_p
, NULL
);
459 if (debug_threads
&& event_child
)
460 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
464 event_child
= get_thread_process (child
);
465 if (event_child
->status_pending_p
466 && check_removed_breakpoint (event_child
))
470 if (event_child
!= NULL
)
472 if (event_child
->status_pending_p
)
475 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
476 event_child
->lwpid
, event_child
->status_pending
);
477 wstat
= event_child
->status_pending
;
478 event_child
->status_pending_p
= 0;
479 event_child
->status_pending
= 0;
480 current_inferior
= get_process_thread (event_child
);
485 /* We only enter this loop if no process has a pending wait status. Thus
486 any action taken in response to a wait status inside this loop is
487 responding as soon as we detect the status, not after any pending
494 event_child
= get_thread_process (child
);
496 linux_wait_for_process (&event_child
, &wstat
);
498 if (event_child
== NULL
)
499 error ("event from unknown child");
501 current_inferior
= (struct thread_info
*)
502 find_inferior_id (&all_threads
, event_child
->tid
);
504 /* Check for thread exit. */
505 if (using_threads
&& ! WIFSTOPPED (wstat
))
508 fprintf (stderr
, "Thread %ld (LWP %ld) exiting\n",
509 event_child
->tid
, event_child
->head
.id
);
511 /* If the last thread is exiting, just return. */
512 if (all_threads
.head
== all_threads
.tail
)
515 dead_thread_notify (event_child
->tid
);
517 remove_inferior (&all_processes
, &event_child
->head
);
519 remove_thread (current_inferior
);
520 current_inferior
= (struct thread_info
*) all_threads
.head
;
522 /* If we were waiting for this particular child to do something...
523 well, it did something. */
527 /* Wait for a more interesting event. */
532 && WIFSTOPPED (wstat
)
533 && WSTOPSIG (wstat
) == SIGSTOP
534 && event_child
->stop_expected
)
537 fprintf (stderr
, "Expected stop.\n");
538 event_child
->stop_expected
= 0;
539 linux_resume_one_process (&event_child
->head
,
540 event_child
->stepping
, 0, NULL
);
544 /* If GDB is not interested in this signal, don't stop other
545 threads, and don't report it to GDB. Just resume the
546 inferior right away. We do this for threading-related
547 signals as well as any that GDB specifically requested
548 we ignore. But never ignore SIGSTOP if we sent it
550 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
552 if (WIFSTOPPED (wstat
)
553 && ((using_threads
&& (WSTOPSIG (wstat
) == __SIGRTMIN
554 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
555 || (pass_signals
[target_signal_from_host (WSTOPSIG (wstat
))]
556 && (WSTOPSIG (wstat
) != SIGSTOP
557 || !event_child
->sigstop_sent
))))
559 siginfo_t info
, *info_p
;
562 fprintf (stderr
, "Ignored signal %d for %ld (LWP %ld).\n",
563 WSTOPSIG (wstat
), event_child
->tid
,
564 event_child
->head
.id
);
566 if (ptrace (PTRACE_GETSIGINFO
, event_child
->lwpid
, 0, &info
) == 0)
570 linux_resume_one_process (&event_child
->head
,
571 event_child
->stepping
,
572 WSTOPSIG (wstat
), info_p
);
576 /* If this event was not handled above, and is not a SIGTRAP, report
578 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
581 /* If this target does not support breakpoints, we simply report the
582 SIGTRAP; it's of no concern to us. */
583 if (the_low_target
.get_pc
== NULL
)
586 stop_pc
= get_stop_pc ();
588 /* bp_reinsert will only be set if we were single-stepping.
589 Notice that we will resume the process after hitting
590 a gdbserver breakpoint; single-stepping to/over one
591 is not supported (yet). */
592 if (event_child
->bp_reinsert
!= 0)
595 fprintf (stderr
, "Reinserted breakpoint.\n");
596 reinsert_breakpoint (event_child
->bp_reinsert
);
597 event_child
->bp_reinsert
= 0;
599 /* Clear the single-stepping flag and SIGTRAP as we resume. */
600 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
605 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
607 if (check_breakpoints (stop_pc
) != 0)
609 /* We hit one of our own breakpoints. We mark it as a pending
610 breakpoint, so that check_removed_breakpoint () will do the PC
611 adjustment for us at the appropriate time. */
612 event_child
->pending_is_breakpoint
= 1;
613 event_child
->pending_stop_pc
= stop_pc
;
615 /* Now we need to put the breakpoint back. We continue in the event
616 loop instead of simply replacing the breakpoint right away,
617 in order to not lose signals sent to the thread that hit the
618 breakpoint. Unfortunately this increases the window where another
619 thread could sneak past the removed breakpoint. For the current
620 use of server-side breakpoints (thread creation) this is
621 acceptable; but it needs to be considered before this breakpoint
622 mechanism can be used in more general ways. For some breakpoints
623 it may be necessary to stop all other threads, but that should
624 be avoided where possible.
626 If breakpoint_reinsert_addr is NULL, that means that we can
627 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
628 mark it for reinsertion, and single-step.
630 Otherwise, call the target function to figure out where we need
631 our temporary breakpoint, create it, and continue executing this
633 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
635 event_child
->bp_reinsert
= stop_pc
;
636 uninsert_breakpoint (stop_pc
);
637 linux_resume_one_process (&event_child
->head
, 1, 0, NULL
);
641 reinsert_breakpoint_by_bp
642 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
643 linux_resume_one_process (&event_child
->head
, 0, 0, NULL
);
649 /* If we were single-stepping, we definitely want to report the
650 SIGTRAP. The single-step operation has completed, so also
651 clear the stepping flag; in general this does not matter,
652 because the SIGTRAP will be reported to the client, which
653 will give us a new action for this thread, but clear it for
654 consistency anyway. It's safe to clear the stepping flag
655 because the only consumer of get_stop_pc () after this point
656 is check_removed_breakpoint, and pending_is_breakpoint is not
657 set. It might be wiser to use a step_completed flag instead. */
658 if (event_child
->stepping
)
660 event_child
->stepping
= 0;
664 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
665 Check if it is a breakpoint, and if so mark the process information
666 accordingly. This will handle both the necessary fiddling with the
667 PC on decr_pc_after_break targets and suppressing extra threads
668 hitting a breakpoint if two hit it at once and then GDB removes it
669 after the first is reported. Arguably it would be better to report
670 multiple threads hitting breakpoints simultaneously, but the current
671 remote protocol does not allow this. */
672 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
674 event_child
->pending_is_breakpoint
= 1;
675 event_child
->pending_stop_pc
= stop_pc
;
685 /* Wait for process, returns status. */
688 linux_wait (char *status
)
691 struct thread_info
*child
= NULL
;
694 /* If we were only supposed to resume one thread, only wait for
695 that thread - if it's still alive. If it died, however - which
696 can happen if we're coming from the thread death case below -
697 then we need to make sure we restart the other threads. We could
698 pick a thread at random or restart all; restarting all is less
700 if (cont_thread
!= 0 && cont_thread
!= -1)
702 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
705 /* No stepping, no signal - unless one is pending already, of course. */
708 struct thread_resume resume_info
;
709 resume_info
.thread
= -1;
710 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
711 linux_resume (&resume_info
);
717 w
= linux_wait_for_event (child
);
718 stop_all_processes ();
721 /* If we are waiting for a particular child, and it exited,
722 linux_wait_for_event will return its exit status. Similarly if
723 the last child exited. If this is not the last child, however,
724 do not report it as exited until there is a 'thread exited' response
725 available in the remote protocol. Instead, just wait for another event.
726 This should be safe, because if the thread crashed we will already
727 have reported the termination signal to GDB; that should stop any
728 in-progress stepping operations, etc.
730 Report the exit status of the last thread to exit. This matches
731 LinuxThreads' behavior. */
733 if (all_threads
.head
== all_threads
.tail
)
737 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
740 free (all_processes
.head
);
741 all_processes
.head
= all_processes
.tail
= NULL
;
742 return WEXITSTATUS (w
);
744 else if (!WIFSTOPPED (w
))
746 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
749 free (all_processes
.head
);
750 all_processes
.head
= all_processes
.tail
= NULL
;
751 return target_signal_from_host (WTERMSIG (w
));
761 return target_signal_from_host (WSTOPSIG (w
));
764 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
765 thread groups are in use, we need to use tkill. */
768 kill_lwp (unsigned long lwpid
, int signo
)
770 static int tkill_failed
;
777 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
785 return kill (lwpid
, signo
);
789 send_sigstop (struct inferior_list_entry
*entry
)
791 struct process_info
*process
= (struct process_info
*) entry
;
793 if (process
->stopped
)
796 /* If we already have a pending stop signal for this process, don't
798 if (process
->stop_expected
)
800 process
->stop_expected
= 0;
805 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
807 kill_lwp (process
->head
.id
, SIGSTOP
);
808 process
->sigstop_sent
= 1;
812 wait_for_sigstop (struct inferior_list_entry
*entry
)
814 struct process_info
*process
= (struct process_info
*) entry
;
815 struct thread_info
*saved_inferior
, *thread
;
817 unsigned long saved_tid
;
819 if (process
->stopped
)
822 saved_inferior
= current_inferior
;
823 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
824 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
826 wstat
= linux_wait_for_event (thread
);
828 /* If we stopped with a non-SIGSTOP signal, save it for later
829 and record the pending SIGSTOP. If the process exited, just
831 if (WIFSTOPPED (wstat
)
832 && WSTOPSIG (wstat
) != SIGSTOP
)
835 fprintf (stderr
, "Stopped with non-sigstop signal\n");
836 process
->status_pending_p
= 1;
837 process
->status_pending
= wstat
;
838 process
->stop_expected
= 1;
841 if (linux_thread_alive (saved_tid
))
842 current_inferior
= saved_inferior
;
846 fprintf (stderr
, "Previously current thread died.\n");
848 /* Set a valid thread as current. */
849 set_desired_inferior (0);
854 stop_all_processes (void)
856 stopping_threads
= 1;
857 for_each_inferior (&all_processes
, send_sigstop
);
858 for_each_inferior (&all_processes
, wait_for_sigstop
);
859 stopping_threads
= 0;
862 /* Resume execution of the inferior process.
863 If STEP is nonzero, single-step it.
864 If SIGNAL is nonzero, give it that signal. */
867 linux_resume_one_process (struct inferior_list_entry
*entry
,
868 int step
, int signal
, siginfo_t
*info
)
870 struct process_info
*process
= (struct process_info
*) entry
;
871 struct thread_info
*saved_inferior
;
873 if (process
->stopped
== 0)
876 /* If we have pending signals or status, and a new signal, enqueue the
877 signal. Also enqueue the signal if we are waiting to reinsert a
878 breakpoint; it will be picked up again below. */
880 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
881 || process
->bp_reinsert
!= 0))
883 struct pending_signals
*p_sig
;
884 p_sig
= malloc (sizeof (*p_sig
));
885 p_sig
->prev
= process
->pending_signals
;
886 p_sig
->signal
= signal
;
888 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
890 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
891 process
->pending_signals
= p_sig
;
894 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
897 saved_inferior
= current_inferior
;
898 current_inferior
= get_process_thread (process
);
901 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
902 step
? "step" : "continue", signal
,
903 process
->stop_expected
? "expected" : "not expected");
905 /* This bit needs some thinking about. If we get a signal that
906 we must report while a single-step reinsert is still pending,
907 we often end up resuming the thread. It might be better to
908 (ew) allow a stack of pending events; then we could be sure that
909 the reinsert happened right away and not lose any signals.
911 Making this stack would also shrink the window in which breakpoints are
912 uninserted (see comment in linux_wait_for_process) but not enough for
913 complete correctness, so it won't solve that problem. It may be
914 worthwhile just to solve this one, however. */
915 if (process
->bp_reinsert
!= 0)
918 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
920 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
923 /* Postpone any pending signal. It was enqueued above. */
927 check_removed_breakpoint (process
);
929 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
931 fprintf (stderr
, " ");
932 (*the_low_target
.get_pc
) ();
935 /* If we have pending signals, consume one unless we are trying to reinsert
937 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
939 struct pending_signals
**p_sig
;
941 p_sig
= &process
->pending_signals
;
942 while ((*p_sig
)->prev
!= NULL
)
943 p_sig
= &(*p_sig
)->prev
;
945 signal
= (*p_sig
)->signal
;
946 if ((*p_sig
)->info
.si_signo
!= 0)
947 ptrace (PTRACE_SETSIGINFO
, process
->lwpid
, 0, &(*p_sig
)->info
);
953 regcache_invalidate_one ((struct inferior_list_entry
*)
954 get_process_thread (process
));
956 process
->stopped
= 0;
957 process
->stepping
= step
;
958 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
960 current_inferior
= saved_inferior
;
962 perror_with_name ("ptrace");
965 static struct thread_resume
*resume_ptr
;
967 /* This function is called once per thread. We look up the thread
968 in RESUME_PTR, and mark the thread with a pointer to the appropriate
971 This algorithm is O(threads * resume elements), but resume elements
972 is small (and will remain small at least until GDB supports thread
975 linux_set_resume_request (struct inferior_list_entry
*entry
)
977 struct process_info
*process
;
978 struct thread_info
*thread
;
981 thread
= (struct thread_info
*) entry
;
982 process
= get_thread_process (thread
);
985 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
988 process
->resume
= &resume_ptr
[ndx
];
991 /* This function is called once per thread. We check the thread's resume
992 request, which will tell us whether to resume, step, or leave the thread
993 stopped; and what signal, if any, it should be sent. For threads which
994 we aren't explicitly told otherwise, we preserve the stepping flag; this
995 is used for stepping over gdbserver-placed breakpoints. */
998 linux_continue_one_thread (struct inferior_list_entry
*entry
)
1000 struct process_info
*process
;
1001 struct thread_info
*thread
;
1004 thread
= (struct thread_info
*) entry
;
1005 process
= get_thread_process (thread
);
1007 if (process
->resume
->leave_stopped
)
1010 if (process
->resume
->thread
== -1)
1011 step
= process
->stepping
|| process
->resume
->step
;
1013 step
= process
->resume
->step
;
1015 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
, NULL
);
1017 process
->resume
= NULL
;
1020 /* This function is called once per thread. We check the thread's resume
1021 request, which will tell us whether to resume, step, or leave the thread
1022 stopped; and what signal, if any, it should be sent. We queue any needed
1023 signals, since we won't actually resume. We already have a pending event
1024 to report, so we don't need to preserve any step requests; they should
1025 be re-issued if necessary. */
1028 linux_queue_one_thread (struct inferior_list_entry
*entry
)
1030 struct process_info
*process
;
1031 struct thread_info
*thread
;
1033 thread
= (struct thread_info
*) entry
;
1034 process
= get_thread_process (thread
);
1036 if (process
->resume
->leave_stopped
)
1039 /* If we have a new signal, enqueue the signal. */
1040 if (process
->resume
->sig
!= 0)
1042 struct pending_signals
*p_sig
;
1043 p_sig
= malloc (sizeof (*p_sig
));
1044 p_sig
->prev
= process
->pending_signals
;
1045 p_sig
->signal
= process
->resume
->sig
;
1046 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1048 /* If this is the same signal we were previously stopped by,
1049 make sure to queue its siginfo. We can ignore the return
1050 value of ptrace; if it fails, we'll skip
1051 PTRACE_SETSIGINFO. */
1052 if (WIFSTOPPED (process
->last_status
)
1053 && WSTOPSIG (process
->last_status
) == process
->resume
->sig
)
1054 ptrace (PTRACE_GETSIGINFO
, process
->lwpid
, 0, &p_sig
->info
);
1056 process
->pending_signals
= p_sig
;
1059 process
->resume
= NULL
;
1062 /* Set DUMMY if this process has an interesting status pending. */
1064 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1066 struct process_info
*process
= (struct process_info
*) entry
;
1068 /* Processes which will not be resumed are not interesting, because
1069 we might not wait for them next time through linux_wait. */
1070 if (process
->resume
->leave_stopped
)
1073 /* If this thread has a removed breakpoint, we won't have any
1074 events to report later, so check now. check_removed_breakpoint
1075 may clear status_pending_p. We avoid calling check_removed_breakpoint
1076 for any thread that we are not otherwise going to resume - this
1077 lets us preserve stopped status when two threads hit a breakpoint.
1078 GDB removes the breakpoint to single-step a particular thread
1079 past it, then re-inserts it and resumes all threads. We want
1080 to report the second thread without resuming it in the interim. */
1081 if (process
->status_pending_p
)
1082 check_removed_breakpoint (process
);
1084 if (process
->status_pending_p
)
1085 * (int *) flag_p
= 1;
1091 linux_resume (struct thread_resume
*resume_info
)
1095 /* Yes, the use of a global here is rather ugly. */
1096 resume_ptr
= resume_info
;
1098 for_each_inferior (&all_threads
, linux_set_resume_request
);
1100 /* If there is a thread which would otherwise be resumed, which
1101 has a pending status, then don't resume any threads - we can just
1102 report the pending status. Make sure to queue any signals
1103 that would otherwise be sent. */
1105 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1110 fprintf (stderr
, "Not resuming, pending status\n");
1112 fprintf (stderr
, "Resuming, no pending status\n");
1116 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1121 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1125 #ifdef HAVE_LINUX_USRREGS
1128 register_addr (int regnum
)
1132 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1133 error ("Invalid register number %d.", regnum
);
1135 addr
= the_low_target
.regmap
[regnum
];
1140 /* Fetch one register. */
1142 fetch_register (int regno
)
1148 if (regno
>= the_low_target
.num_regs
)
1150 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1153 regaddr
= register_addr (regno
);
1156 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1157 & - sizeof (PTRACE_XFER_TYPE
);
1158 buf
= alloca (size
);
1159 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1162 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1163 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1164 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1167 /* Warning, not error, in case we are attached; sometimes the
1168 kernel doesn't let us at the registers. */
1169 char *err
= strerror (errno
);
1170 char *msg
= alloca (strlen (err
) + 128);
1171 sprintf (msg
, "reading register %d: %s", regno
, err
);
1176 if (the_low_target
.left_pad_xfer
1177 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1178 supply_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1179 - register_size (regno
)));
1181 supply_register (regno
, buf
);
1186 /* Fetch all registers, or just one, from the child process. */
1188 usr_fetch_inferior_registers (int regno
)
1190 if (regno
== -1 || regno
== 0)
1191 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1192 fetch_register (regno
);
1194 fetch_register (regno
);
1197 /* Store our register values back into the inferior.
1198 If REGNO is -1, do this for all registers.
1199 Otherwise, REGNO specifies which register (so we can save time). */
1201 usr_store_inferior_registers (int regno
)
1209 if (regno
>= the_low_target
.num_regs
)
1212 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1215 regaddr
= register_addr (regno
);
1219 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1220 & - sizeof (PTRACE_XFER_TYPE
);
1221 buf
= alloca (size
);
1222 memset (buf
, 0, size
);
1223 if (the_low_target
.left_pad_xfer
1224 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1225 collect_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1226 - register_size (regno
)));
1228 collect_register (regno
, buf
);
1229 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1232 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1233 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1236 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1238 char *err
= strerror (errno
);
1239 char *msg
= alloca (strlen (err
) + 128);
1240 sprintf (msg
, "writing register %d: %s",
1246 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1250 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1251 usr_store_inferior_registers (regno
);
1253 #endif /* HAVE_LINUX_USRREGS */
1257 #ifdef HAVE_LINUX_REGSETS
1260 regsets_fetch_inferior_registers ()
1262 struct regset_info
*regset
;
1263 int saw_general_regs
= 0;
1265 regset
= target_regsets
;
1267 while (regset
->size
>= 0)
1272 if (regset
->size
== 0)
1278 buf
= malloc (regset
->size
);
1279 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1284 /* If we get EIO on the first regset, do not try regsets again.
1285 If we get EIO on a later regset, disable that regset. */
1286 if (regset
== target_regsets
)
1300 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1305 else if (regset
->type
== GENERAL_REGS
)
1306 saw_general_regs
= 1;
1307 regset
->store_function (buf
);
1310 if (saw_general_regs
)
1317 regsets_store_inferior_registers ()
1319 struct regset_info
*regset
;
1320 int saw_general_regs
= 0;
1322 regset
= target_regsets
;
1324 while (regset
->size
>= 0)
1329 if (regset
->size
== 0)
1335 buf
= malloc (regset
->size
);
1337 /* First fill the buffer with the current register set contents,
1338 in case there are any items in the kernel's regset that are
1339 not in gdbserver's regcache. */
1340 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1344 /* Then overlay our cached registers on that. */
1345 regset
->fill_function (buf
);
1347 /* Only now do we write the register set. */
1348 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1355 /* If we get EIO on the first regset, do not try regsets again.
1356 If we get EIO on a later regset, disable that regset. */
1357 if (regset
== target_regsets
)
1370 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1373 else if (regset
->type
== GENERAL_REGS
)
1374 saw_general_regs
= 1;
1378 if (saw_general_regs
)
1385 #endif /* HAVE_LINUX_REGSETS */
1389 linux_fetch_registers (int regno
)
1391 #ifdef HAVE_LINUX_REGSETS
1394 if (regsets_fetch_inferior_registers () == 0)
1398 #ifdef HAVE_LINUX_USRREGS
1399 usr_fetch_inferior_registers (regno
);
1404 linux_store_registers (int regno
)
1406 #ifdef HAVE_LINUX_REGSETS
1409 if (regsets_store_inferior_registers () == 0)
1413 #ifdef HAVE_LINUX_USRREGS
1414 usr_store_inferior_registers (regno
);
1419 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1420 to debugger memory starting at MYADDR. */
1423 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1426 /* Round starting address down to longword boundary. */
1427 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1428 /* Round ending address up; get number of longwords that makes. */
1430 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1431 / sizeof (PTRACE_XFER_TYPE
);
1432 /* Allocate buffer of that many longwords. */
1433 register PTRACE_XFER_TYPE
*buffer
1434 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1436 /* Read all the longwords */
1437 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1440 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1445 /* Copy appropriate bytes out of the buffer. */
1446 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1451 /* Copy LEN bytes of data from debugger memory at MYADDR
1452 to inferior's memory at MEMADDR.
1453 On failure (cannot write the inferior)
1454 returns the value of errno. */
1457 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
1460 /* Round starting address down to longword boundary. */
1461 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1462 /* Round ending address up; get number of longwords that makes. */
1464 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1465 /* Allocate buffer of that many longwords. */
1466 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1471 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1474 /* Fill start and end extra bytes of buffer with existing memory data. */
1476 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1477 (PTRACE_ARG3_TYPE
) addr
, 0);
1482 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1483 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1484 * sizeof (PTRACE_XFER_TYPE
)),
1488 /* Copy data to be written over corresponding part of buffer */
1490 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1492 /* Write the entire buffer. */
1494 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1497 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1506 linux_look_up_symbols (void)
1508 #ifdef USE_THREAD_DB
1512 using_threads
= thread_db_init ();
1517 linux_send_signal (int signum
)
1519 extern unsigned long signal_pid
;
1521 if (cont_thread
!= 0 && cont_thread
!= -1)
1523 struct process_info
*process
;
1525 process
= get_thread_process (current_inferior
);
1526 kill_lwp (process
->lwpid
, signum
);
1529 kill_lwp (signal_pid
, signum
);
1532 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1533 to debugger memory starting at MYADDR. */
1536 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
1538 char filename
[PATH_MAX
];
1541 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1543 fd
= open (filename
, O_RDONLY
);
1547 if (offset
!= (CORE_ADDR
) 0
1548 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1551 n
= read (fd
, myaddr
, len
);
1558 /* These watchpoint related wrapper functions simply pass on the function call
1559 if the target has registered a corresponding function. */
1562 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1564 if (the_low_target
.insert_watchpoint
!= NULL
)
1565 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1567 /* Unsupported (see target.h). */
1572 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1574 if (the_low_target
.remove_watchpoint
!= NULL
)
1575 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1577 /* Unsupported (see target.h). */
1582 linux_stopped_by_watchpoint (void)
1584 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1585 return the_low_target
.stopped_by_watchpoint ();
1591 linux_stopped_data_address (void)
1593 if (the_low_target
.stopped_data_address
!= NULL
)
1594 return the_low_target
.stopped_data_address ();
1599 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
1600 #if defined(__mcoldfire__)
1601 /* These should really be defined in the kernel's ptrace.h header. */
1602 #define PT_TEXT_ADDR 49*4
1603 #define PT_DATA_ADDR 50*4
1604 #define PT_TEXT_END_ADDR 51*4
1607 /* Under uClinux, programs are loaded at non-zero offsets, which we need
1608 to tell gdb about. */
1611 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
1613 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1614 unsigned long text
, text_end
, data
;
1615 int pid
= get_thread_process (current_inferior
)->head
.id
;
1619 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
1620 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
1621 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
1625 /* Both text and data offsets produced at compile-time (and so
1626 used by gdb) are relative to the beginning of the program,
1627 with the data segment immediately following the text segment.
1628 However, the actual runtime layout in memory may put the data
1629 somewhere else, so when we send gdb a data base-address, we
1630 use the real data base address and subtract the compile-time
1631 data base-address from it (which is just the length of the
1632 text segment). BSS immediately follows data in both
1635 *data_p
= data
- (text_end
- text
);
1645 linux_arch_string (void)
1647 return the_low_target
.arch_string
;
1650 static struct target_ops linux_target_ops
= {
1651 linux_create_inferior
,
1658 linux_fetch_registers
,
1659 linux_store_registers
,
1662 linux_look_up_symbols
,
1665 linux_insert_watchpoint
,
1666 linux_remove_watchpoint
,
1667 linux_stopped_by_watchpoint
,
1668 linux_stopped_data_address
,
1669 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
1674 #ifdef USE_THREAD_DB
1675 thread_db_get_tls_address
,
1683 linux_init_signals ()
1685 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1686 to find what the cancel signal actually is. */
1687 signal (__SIGRTMIN
+1, SIG_IGN
);
1691 initialize_low (void)
1694 set_target_ops (&linux_target_ops
);
1695 set_breakpoint_data (the_low_target
.breakpoint
,
1696 the_low_target
.breakpoint_len
);
1698 linux_init_signals ();