1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 /* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
41 however. This requires changing the ID in place when we go from !using_threads
42 to using_threads, immediately.
44 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
45 the same as the LWP ID. */
47 struct inferior_list all_processes
;
49 /* FIXME this is a bit of a hack, and could be removed. */
52 /* FIXME make into a target method? */
55 static void linux_resume_one_process (struct inferior_list_entry
*entry
,
56 int step
, int signal
);
57 static void linux_resume (struct thread_resume
*resume_info
);
58 static void stop_all_processes (void);
59 static int linux_wait_for_event (struct thread_info
*child
);
61 struct pending_signals
64 struct pending_signals
*prev
;
67 #define PTRACE_ARG3_TYPE long
68 #define PTRACE_XFER_TYPE long
70 #ifdef HAVE_LINUX_REGSETS
71 static int use_regsets_p
= 1;
74 int debug_threads
= 0;
76 #define pid_of(proc) ((proc)->head.id)
78 /* FIXME: Delete eventually. */
79 #define inferior_pid (pid_of (get_thread_process (current_inferior)))
81 /* This function should only be called if the process got a SIGTRAP.
82 The SIGTRAP could mean several things.
84 On i386, where decr_pc_after_break is non-zero:
85 If we were single-stepping this process using PTRACE_SINGLESTEP,
86 we will get only the one SIGTRAP (even if the instruction we
87 stepped over was a breakpoint). The value of $eip will be the
89 If we continue the process using PTRACE_CONT, we will get a
90 SIGTRAP when we hit a breakpoint. The value of $eip will be
91 the instruction after the breakpoint (i.e. needs to be
92 decremented). If we report the SIGTRAP to GDB, we must also
93 report the undecremented PC. If we cancel the SIGTRAP, we
94 must resume at the decremented PC.
96 (Presumably, not yet tested) On a non-decr_pc_after_break machine
97 with hardware or kernel single-step:
98 If we single-step over a breakpoint instruction, our PC will
99 point at the following instruction. If we continue and hit a
100 breakpoint instruction, our PC will point at the breakpoint
106 CORE_ADDR stop_pc
= (*the_low_target
.get_pc
) ();
108 if (get_thread_process (current_inferior
)->stepping
)
111 return stop_pc
- the_low_target
.decr_pc_after_break
;
115 add_process (unsigned long pid
)
117 struct process_info
*process
;
119 process
= (struct process_info
*) malloc (sizeof (*process
));
120 memset (process
, 0, sizeof (*process
));
122 process
->head
.id
= pid
;
124 /* Default to tid == lwpid == pid. */
126 process
->lwpid
= pid
;
128 add_inferior_to_list (&all_processes
, &process
->head
);
133 /* Start an inferior process and returns its pid.
134 ALLARGS is a vector of program-name and args. */
137 linux_create_inferior (char *program
, char **allargs
)
144 perror_with_name ("fork");
148 ptrace (PTRACE_TRACEME
, 0, 0, 0);
150 signal (__SIGRTMIN
+ 1, SIG_DFL
);
154 execv (program
, allargs
);
156 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
162 new_process
= add_process (pid
);
163 add_thread (pid
, new_process
);
168 /* Attach to an inferior process. */
171 linux_attach_lwp (unsigned long pid
, unsigned long tid
)
173 struct process_info
*new_process
;
175 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
177 fprintf (stderr
, "Cannot attach to process %ld: %s (%d)\n", pid
,
178 strerror (errno
), errno
);
181 /* If we fail to attach to an LWP, just return. */
187 new_process
= (struct process_info
*) add_process (pid
);
188 add_thread (tid
, new_process
);
190 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
191 brings it to a halt. We should ignore that SIGSTOP and resume the process
192 (unless this is the first process, in which case the flag will be cleared
195 On the other hand, if we are currently trying to stop all threads, we
196 should treat the new thread as if we had sent it a SIGSTOP. This works
197 because we are guaranteed that add_process added us to the end of the
198 list, and so the new thread has not yet reached wait_for_sigstop (but
200 if (! stopping_threads
)
201 new_process
->stop_expected
= 1;
205 linux_attach (unsigned long pid
)
207 struct process_info
*process
;
209 linux_attach_lwp (pid
, pid
);
211 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
212 process
= (struct process_info
*) find_inferior_id (&all_processes
, pid
);
213 process
->stop_expected
= 0;
218 /* Kill the inferior process. Make us have no inferior. */
221 linux_kill_one_process (struct inferior_list_entry
*entry
)
223 struct thread_info
*thread
= (struct thread_info
*) entry
;
224 struct process_info
*process
= get_thread_process (thread
);
227 /* We avoid killing the first thread here, because of a Linux kernel (at
228 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
229 the children get a chance to be reaped, it will remain a zombie
231 if (entry
== all_threads
.head
)
236 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
238 /* Make sure it died. The loop is most likely unnecessary. */
239 wstat
= linux_wait_for_event (thread
);
240 } while (WIFSTOPPED (wstat
));
246 struct thread_info
*thread
= (struct thread_info
*) all_threads
.head
;
247 struct process_info
*process
= get_thread_process (thread
);
250 for_each_inferior (&all_threads
, linux_kill_one_process
);
252 /* See the comment in linux_kill_one_process. We did not kill the first
253 thread in the list, so do so now. */
256 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
258 /* Make sure it died. The loop is most likely unnecessary. */
259 wstat
= linux_wait_for_event (thread
);
260 } while (WIFSTOPPED (wstat
));
264 linux_detach_one_process (struct inferior_list_entry
*entry
)
266 struct thread_info
*thread
= (struct thread_info
*) entry
;
267 struct process_info
*process
= get_thread_process (thread
);
269 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
275 for_each_inferior (&all_threads
, linux_detach_one_process
);
278 /* Return nonzero if the given thread is still alive. */
280 linux_thread_alive (unsigned long tid
)
282 if (find_inferior_id (&all_threads
, tid
) != NULL
)
288 /* Return nonzero if this process stopped at a breakpoint which
289 no longer appears to be inserted. Also adjust the PC
290 appropriately to resume where the breakpoint used to be. */
292 check_removed_breakpoint (struct process_info
*event_child
)
295 struct thread_info
*saved_inferior
;
297 if (event_child
->pending_is_breakpoint
== 0)
301 fprintf (stderr
, "Checking for breakpoint.\n");
303 saved_inferior
= current_inferior
;
304 current_inferior
= get_process_thread (event_child
);
306 stop_pc
= get_stop_pc ();
308 /* If the PC has changed since we stopped, then we shouldn't do
309 anything. This happens if, for instance, GDB handled the
310 decr_pc_after_break subtraction itself. */
311 if (stop_pc
!= event_child
->pending_stop_pc
)
314 fprintf (stderr
, "Ignoring, PC was changed.\n");
316 event_child
->pending_is_breakpoint
= 0;
317 current_inferior
= saved_inferior
;
321 /* If the breakpoint is still there, we will report hitting it. */
322 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
325 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
326 current_inferior
= saved_inferior
;
331 fprintf (stderr
, "Removed breakpoint.\n");
333 /* For decr_pc_after_break targets, here is where we perform the
334 decrement. We go immediately from this function to resuming,
335 and can not safely call get_stop_pc () again. */
336 if (the_low_target
.set_pc
!= NULL
)
337 (*the_low_target
.set_pc
) (stop_pc
);
339 /* We consumed the pending SIGTRAP. */
340 event_child
->pending_is_breakpoint
= 0;
341 event_child
->status_pending_p
= 0;
342 event_child
->status_pending
= 0;
344 current_inferior
= saved_inferior
;
348 /* Return 1 if this process has an interesting status pending. This function
349 may silently resume an inferior process. */
351 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
353 struct process_info
*process
= (struct process_info
*) entry
;
355 if (process
->status_pending_p
)
356 if (check_removed_breakpoint (process
))
358 /* This thread was stopped at a breakpoint, and the breakpoint
359 is now gone. We were told to continue (or step...) all threads,
360 so GDB isn't trying to single-step past this breakpoint.
361 So instead of reporting the old SIGTRAP, pretend we got to
362 the breakpoint just after it was removed instead of just
363 before; resume the process. */
364 linux_resume_one_process (&process
->head
, 0, 0);
368 return process
->status_pending_p
;
372 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
375 int to_wait_for
= -1;
378 to_wait_for
= (*childp
)->lwpid
;
382 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
387 perror_with_name ("waitpid");
392 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
397 perror_with_name ("waitpid (WCLONE)");
406 && (!WIFSTOPPED (*wstatp
)
407 || (WSTOPSIG (*wstatp
) != 32
408 && WSTOPSIG (*wstatp
) != 33)))
409 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
411 if (to_wait_for
== -1)
412 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
414 (*childp
)->stopped
= 1;
415 (*childp
)->pending_is_breakpoint
= 0;
418 && WIFSTOPPED (*wstatp
))
420 current_inferior
= (struct thread_info
*)
421 find_inferior_id (&all_threads
, (*childp
)->tid
);
422 /* For testing only; i386_stop_pc prints out a diagnostic. */
423 if (the_low_target
.get_pc
!= NULL
)
429 linux_wait_for_event (struct thread_info
*child
)
432 struct process_info
*event_child
;
435 /* Check for a process with a pending status. */
436 /* It is possible that the user changed the pending task's registers since
437 it stopped. We correctly handle the change of PC if we hit a breakpoint
438 (in check_removed_breakpoint); signals should be reported anyway. */
441 event_child
= (struct process_info
*)
442 find_inferior (&all_processes
, status_pending_p
, NULL
);
443 if (debug_threads
&& event_child
)
444 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
448 event_child
= get_thread_process (child
);
449 if (event_child
->status_pending_p
450 && check_removed_breakpoint (event_child
))
454 if (event_child
!= NULL
)
456 if (event_child
->status_pending_p
)
459 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
460 event_child
->lwpid
, event_child
->status_pending
);
461 wstat
= event_child
->status_pending
;
462 event_child
->status_pending_p
= 0;
463 event_child
->status_pending
= 0;
464 current_inferior
= get_process_thread (event_child
);
469 /* We only enter this loop if no process has a pending wait status. Thus
470 any action taken in response to a wait status inside this loop is
471 responding as soon as we detect the status, not after any pending
478 event_child
= get_thread_process (child
);
480 linux_wait_for_process (&event_child
, &wstat
);
482 if (event_child
== NULL
)
483 error ("event from unknown child");
485 current_inferior
= (struct thread_info
*)
486 find_inferior_id (&all_threads
, event_child
->tid
);
490 /* Check for thread exit. */
491 if (! WIFSTOPPED (wstat
))
494 fprintf (stderr
, "Thread %ld (LWP %ld) exiting\n",
495 event_child
->tid
, event_child
->head
.id
);
497 /* If the last thread is exiting, just return. */
498 if (all_threads
.head
== all_threads
.tail
)
501 dead_thread_notify (event_child
->tid
);
503 remove_inferior (&all_processes
, &event_child
->head
);
505 remove_thread (current_inferior
);
506 current_inferior
= (struct thread_info
*) all_threads
.head
;
508 /* If we were waiting for this particular child to do something...
509 well, it did something. */
513 /* Wait for a more interesting event. */
517 if (WIFSTOPPED (wstat
)
518 && WSTOPSIG (wstat
) == SIGSTOP
519 && event_child
->stop_expected
)
522 fprintf (stderr
, "Expected stop.\n");
523 event_child
->stop_expected
= 0;
524 linux_resume_one_process (&event_child
->head
,
525 event_child
->stepping
, 0);
529 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
531 if (WIFSTOPPED (wstat
)
532 && (WSTOPSIG (wstat
) == __SIGRTMIN
533 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
536 fprintf (stderr
, "Ignored signal %d for %ld (LWP %ld).\n",
537 WSTOPSIG (wstat
), event_child
->tid
,
538 event_child
->head
.id
);
539 linux_resume_one_process (&event_child
->head
,
540 event_child
->stepping
,
546 /* If this event was not handled above, and is not a SIGTRAP, report
548 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
551 /* If this target does not support breakpoints, we simply report the
552 SIGTRAP; it's of no concern to us. */
553 if (the_low_target
.get_pc
== NULL
)
556 stop_pc
= get_stop_pc ();
558 /* bp_reinsert will only be set if we were single-stepping.
559 Notice that we will resume the process after hitting
560 a gdbserver breakpoint; single-stepping to/over one
561 is not supported (yet). */
562 if (event_child
->bp_reinsert
!= 0)
565 fprintf (stderr
, "Reinserted breakpoint.\n");
566 reinsert_breakpoint (event_child
->bp_reinsert
);
567 event_child
->bp_reinsert
= 0;
569 /* Clear the single-stepping flag and SIGTRAP as we resume. */
570 linux_resume_one_process (&event_child
->head
, 0, 0);
575 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
577 if (check_breakpoints (stop_pc
) != 0)
579 /* We hit one of our own breakpoints. We mark it as a pending
580 breakpoint, so that check_removed_breakpoint () will do the PC
581 adjustment for us at the appropriate time. */
582 event_child
->pending_is_breakpoint
= 1;
583 event_child
->pending_stop_pc
= stop_pc
;
585 /* Now we need to put the breakpoint back. We continue in the event
586 loop instead of simply replacing the breakpoint right away,
587 in order to not lose signals sent to the thread that hit the
588 breakpoint. Unfortunately this increases the window where another
589 thread could sneak past the removed breakpoint. For the current
590 use of server-side breakpoints (thread creation) this is
591 acceptable; but it needs to be considered before this breakpoint
592 mechanism can be used in more general ways. For some breakpoints
593 it may be necessary to stop all other threads, but that should
594 be avoided where possible.
596 If breakpoint_reinsert_addr is NULL, that means that we can
597 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
598 mark it for reinsertion, and single-step.
600 Otherwise, call the target function to figure out where we need
601 our temporary breakpoint, create it, and continue executing this
603 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
605 event_child
->bp_reinsert
= stop_pc
;
606 uninsert_breakpoint (stop_pc
);
607 linux_resume_one_process (&event_child
->head
, 1, 0);
611 reinsert_breakpoint_by_bp
612 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
613 linux_resume_one_process (&event_child
->head
, 0, 0);
619 /* If we were single-stepping, we definitely want to report the
620 SIGTRAP. The single-step operation has completed, so also
621 clear the stepping flag; in general this does not matter,
622 because the SIGTRAP will be reported to the client, which
623 will give us a new action for this thread, but clear it for
624 consistency anyway. It's safe to clear the stepping flag
625 because the only consumer of get_stop_pc () after this point
626 is check_removed_breakpoint, and pending_is_breakpoint is not
627 set. It might be wiser to use a step_completed flag instead. */
628 if (event_child
->stepping
)
630 event_child
->stepping
= 0;
634 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
635 Check if it is a breakpoint, and if so mark the process information
636 accordingly. This will handle both the necessary fiddling with the
637 PC on decr_pc_after_break targets and suppressing extra threads
638 hitting a breakpoint if two hit it at once and then GDB removes it
639 after the first is reported. Arguably it would be better to report
640 multiple threads hitting breakpoints simultaneously, but the current
641 remote protocol does not allow this. */
642 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
644 event_child
->pending_is_breakpoint
= 1;
645 event_child
->pending_stop_pc
= stop_pc
;
655 /* Wait for process, returns status. */
658 linux_wait (char *status
)
661 struct thread_info
*child
= NULL
;
664 /* If we were only supposed to resume one thread, only wait for
665 that thread - if it's still alive. If it died, however - which
666 can happen if we're coming from the thread death case below -
667 then we need to make sure we restart the other threads. We could
668 pick a thread at random or restart all; restarting all is less
672 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
675 /* No stepping, no signal - unless one is pending already, of course. */
678 struct thread_resume resume_info
;
679 resume_info
.thread
= -1;
680 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
681 linux_resume (&resume_info
);
687 w
= linux_wait_for_event (child
);
688 stop_all_processes ();
691 /* If we are waiting for a particular child, and it exited,
692 linux_wait_for_event will return its exit status. Similarly if
693 the last child exited. If this is not the last child, however,
694 do not report it as exited until there is a 'thread exited' response
695 available in the remote protocol. Instead, just wait for another event.
696 This should be safe, because if the thread crashed we will already
697 have reported the termination signal to GDB; that should stop any
698 in-progress stepping operations, etc.
700 Report the exit status of the last thread to exit. This matches
701 LinuxThreads' behavior. */
703 if (all_threads
.head
== all_threads
.tail
)
707 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
710 free (all_processes
.head
);
711 all_processes
.head
= all_processes
.tail
= NULL
;
712 return ((unsigned char) WEXITSTATUS (w
));
714 else if (!WIFSTOPPED (w
))
716 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
719 free (all_processes
.head
);
720 all_processes
.head
= all_processes
.tail
= NULL
;
721 return ((unsigned char) WTERMSIG (w
));
731 return ((unsigned char) WSTOPSIG (w
));
734 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
735 thread groups are in use, we need to use tkill. */
738 kill_lwp (unsigned long lwpid
, int signo
)
740 static int tkill_failed
;
747 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
755 return kill (lwpid
, signo
);
759 send_sigstop (struct inferior_list_entry
*entry
)
761 struct process_info
*process
= (struct process_info
*) entry
;
763 if (process
->stopped
)
766 /* If we already have a pending stop signal for this process, don't
768 if (process
->stop_expected
)
770 process
->stop_expected
= 0;
775 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
777 kill_lwp (process
->head
.id
, SIGSTOP
);
778 process
->sigstop_sent
= 1;
782 wait_for_sigstop (struct inferior_list_entry
*entry
)
784 struct process_info
*process
= (struct process_info
*) entry
;
785 struct thread_info
*saved_inferior
, *thread
;
787 unsigned long saved_tid
;
789 if (process
->stopped
)
792 saved_inferior
= current_inferior
;
793 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
794 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
796 wstat
= linux_wait_for_event (thread
);
798 /* If we stopped with a non-SIGSTOP signal, save it for later
799 and record the pending SIGSTOP. If the process exited, just
801 if (WIFSTOPPED (wstat
)
802 && WSTOPSIG (wstat
) != SIGSTOP
)
805 fprintf (stderr
, "Stopped with non-sigstop signal\n");
806 process
->status_pending_p
= 1;
807 process
->status_pending
= wstat
;
808 process
->stop_expected
= 1;
811 if (linux_thread_alive (saved_tid
))
812 current_inferior
= saved_inferior
;
816 fprintf (stderr
, "Previously current thread died.\n");
818 /* Set a valid thread as current. */
819 set_desired_inferior (0);
824 stop_all_processes (void)
826 stopping_threads
= 1;
827 for_each_inferior (&all_processes
, send_sigstop
);
828 for_each_inferior (&all_processes
, wait_for_sigstop
);
829 stopping_threads
= 0;
832 /* Resume execution of the inferior process.
833 If STEP is nonzero, single-step it.
834 If SIGNAL is nonzero, give it that signal. */
837 linux_resume_one_process (struct inferior_list_entry
*entry
,
838 int step
, int signal
)
840 struct process_info
*process
= (struct process_info
*) entry
;
841 struct thread_info
*saved_inferior
;
843 if (process
->stopped
== 0)
846 /* If we have pending signals or status, and a new signal, enqueue the
847 signal. Also enqueue the signal if we are waiting to reinsert a
848 breakpoint; it will be picked up again below. */
850 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
851 || process
->bp_reinsert
!= 0))
853 struct pending_signals
*p_sig
;
854 p_sig
= malloc (sizeof (*p_sig
));
855 p_sig
->prev
= process
->pending_signals
;
856 p_sig
->signal
= signal
;
857 process
->pending_signals
= p_sig
;
860 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
863 saved_inferior
= current_inferior
;
864 current_inferior
= get_process_thread (process
);
867 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
868 step
? "step" : "continue", signal
,
869 process
->stop_expected
? "expected" : "not expected");
871 /* This bit needs some thinking about. If we get a signal that
872 we must report while a single-step reinsert is still pending,
873 we often end up resuming the thread. It might be better to
874 (ew) allow a stack of pending events; then we could be sure that
875 the reinsert happened right away and not lose any signals.
877 Making this stack would also shrink the window in which breakpoints are
878 uninserted (see comment in linux_wait_for_process) but not enough for
879 complete correctness, so it won't solve that problem. It may be
880 worthwhile just to solve this one, however. */
881 if (process
->bp_reinsert
!= 0)
884 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
886 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
889 /* Postpone any pending signal. It was enqueued above. */
893 check_removed_breakpoint (process
);
895 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
897 fprintf (stderr
, " ");
898 (long) (*the_low_target
.get_pc
) ();
901 /* If we have pending signals, consume one unless we are trying to reinsert
903 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
905 struct pending_signals
**p_sig
;
907 p_sig
= &process
->pending_signals
;
908 while ((*p_sig
)->prev
!= NULL
)
909 p_sig
= &(*p_sig
)->prev
;
911 signal
= (*p_sig
)->signal
;
916 regcache_invalidate_one ((struct inferior_list_entry
*)
917 get_process_thread (process
));
919 process
->stopped
= 0;
920 process
->stepping
= step
;
921 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
923 current_inferior
= saved_inferior
;
925 perror_with_name ("ptrace");
928 static struct thread_resume
*resume_ptr
;
930 /* This function is called once per thread. We look up the thread
931 in RESUME_PTR, and mark the thread with a pointer to the appropriate
934 This algorithm is O(threads * resume elements), but resume elements
935 is small (and will remain small at least until GDB supports thread
938 linux_set_resume_request (struct inferior_list_entry
*entry
)
940 struct process_info
*process
;
941 struct thread_info
*thread
;
944 thread
= (struct thread_info
*) entry
;
945 process
= get_thread_process (thread
);
948 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
951 process
->resume
= &resume_ptr
[ndx
];
954 /* This function is called once per thread. We check the thread's resume
955 request, which will tell us whether to resume, step, or leave the thread
956 stopped; and what signal, if any, it should be sent. For threads which
957 we aren't explicitly told otherwise, we preserve the stepping flag; this
958 is used for stepping over gdbserver-placed breakpoints. */
961 linux_continue_one_thread (struct inferior_list_entry
*entry
)
963 struct process_info
*process
;
964 struct thread_info
*thread
;
967 thread
= (struct thread_info
*) entry
;
968 process
= get_thread_process (thread
);
970 if (process
->resume
->leave_stopped
)
973 if (process
->resume
->thread
== -1)
974 step
= process
->stepping
|| process
->resume
->step
;
976 step
= process
->resume
->step
;
978 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
);
980 process
->resume
= NULL
;
983 /* This function is called once per thread. We check the thread's resume
984 request, which will tell us whether to resume, step, or leave the thread
985 stopped; and what signal, if any, it should be sent. We queue any needed
986 signals, since we won't actually resume. We already have a pending event
987 to report, so we don't need to preserve any step requests; they should
988 be re-issued if necessary. */
991 linux_queue_one_thread (struct inferior_list_entry
*entry
)
993 struct process_info
*process
;
994 struct thread_info
*thread
;
996 thread
= (struct thread_info
*) entry
;
997 process
= get_thread_process (thread
);
999 if (process
->resume
->leave_stopped
)
1002 /* If we have a new signal, enqueue the signal. */
1003 if (process
->resume
->sig
!= 0)
1005 struct pending_signals
*p_sig
;
1006 p_sig
= malloc (sizeof (*p_sig
));
1007 p_sig
->prev
= process
->pending_signals
;
1008 p_sig
->signal
= process
->resume
->sig
;
1009 process
->pending_signals
= p_sig
;
1012 process
->resume
= NULL
;
1015 /* Set DUMMY if this process has an interesting status pending. */
1017 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1019 struct process_info
*process
= (struct process_info
*) entry
;
1021 /* Processes which will not be resumed are not interesting, because
1022 we might not wait for them next time through linux_wait. */
1023 if (process
->resume
->leave_stopped
)
1026 /* If this thread has a removed breakpoint, we won't have any
1027 events to report later, so check now. check_removed_breakpoint
1028 may clear status_pending_p. We avoid calling check_removed_breakpoint
1029 for any thread that we are not otherwise going to resume - this
1030 lets us preserve stopped status when two threads hit a breakpoint.
1031 GDB removes the breakpoint to single-step a particular thread
1032 past it, then re-inserts it and resumes all threads. We want
1033 to report the second thread without resuming it in the interim. */
1034 if (process
->status_pending_p
)
1035 check_removed_breakpoint (process
);
1037 if (process
->status_pending_p
)
1038 * (int *) flag_p
= 1;
1044 linux_resume (struct thread_resume
*resume_info
)
1048 /* Yes, the use of a global here is rather ugly. */
1049 resume_ptr
= resume_info
;
1051 for_each_inferior (&all_threads
, linux_set_resume_request
);
1053 /* If there is a thread which would otherwise be resumed, which
1054 has a pending status, then don't resume any threads - we can just
1055 report the pending status. Make sure to queue any signals
1056 that would otherwise be sent. */
1058 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1063 fprintf (stderr
, "Not resuming, pending status\n");
1065 fprintf (stderr
, "Resuming, no pending status\n");
1069 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1074 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1078 #ifdef HAVE_LINUX_USRREGS
1081 register_addr (int regnum
)
1085 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1086 error ("Invalid register number %d.", regnum
);
1088 addr
= the_low_target
.regmap
[regnum
];
1093 /* Fetch one register. */
1095 fetch_register (int regno
)
1101 if (regno
>= the_low_target
.num_regs
)
1103 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1106 regaddr
= register_addr (regno
);
1109 buf
= alloca (register_size (regno
));
1110 for (i
= 0; i
< register_size (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
1113 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1114 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1115 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1118 /* Warning, not error, in case we are attached; sometimes the
1119 kernel doesn't let us at the registers. */
1120 char *err
= strerror (errno
);
1121 char *msg
= alloca (strlen (err
) + 128);
1122 sprintf (msg
, "reading register %d: %s", regno
, err
);
1127 supply_register (regno
, buf
);
1132 /* Fetch all registers, or just one, from the child process. */
1134 usr_fetch_inferior_registers (int regno
)
1136 if (regno
== -1 || regno
== 0)
1137 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1138 fetch_register (regno
);
1140 fetch_register (regno
);
1143 /* Store our register values back into the inferior.
1144 If REGNO is -1, do this for all registers.
1145 Otherwise, REGNO specifies which register (so we can save time). */
1147 usr_store_inferior_registers (int regno
)
1155 if (regno
>= the_low_target
.num_regs
)
1158 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1161 regaddr
= register_addr (regno
);
1165 buf
= alloca (register_size (regno
));
1166 collect_register (regno
, buf
);
1167 for (i
= 0; i
< register_size (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
1170 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1171 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1174 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1176 char *err
= strerror (errno
);
1177 char *msg
= alloca (strlen (err
) + 128);
1178 sprintf (msg
, "writing register %d: %s",
1184 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1188 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1189 usr_store_inferior_registers (regno
);
1191 #endif /* HAVE_LINUX_USRREGS */
1195 #ifdef HAVE_LINUX_REGSETS
1198 regsets_fetch_inferior_registers ()
1200 struct regset_info
*regset
;
1202 regset
= target_regsets
;
1204 while (regset
->size
>= 0)
1209 if (regset
->size
== 0)
1215 buf
= malloc (regset
->size
);
1216 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1221 /* If we get EIO on the first regset, do not try regsets again.
1222 If we get EIO on a later regset, disable that regset. */
1223 if (regset
== target_regsets
)
1237 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1242 regset
->store_function (buf
);
1249 regsets_store_inferior_registers ()
1251 struct regset_info
*regset
;
1253 regset
= target_regsets
;
1255 while (regset
->size
>= 0)
1260 if (regset
->size
== 0)
1266 buf
= malloc (regset
->size
);
1267 regset
->fill_function (buf
);
1268 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1273 /* If we get EIO on the first regset, do not try regsets again.
1274 If we get EIO on a later regset, disable that regset. */
1275 if (regset
== target_regsets
)
1288 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1297 #endif /* HAVE_LINUX_REGSETS */
1301 linux_fetch_registers (int regno
)
1303 #ifdef HAVE_LINUX_REGSETS
1306 if (regsets_fetch_inferior_registers () == 0)
1310 #ifdef HAVE_LINUX_USRREGS
1311 usr_fetch_inferior_registers (regno
);
1316 linux_store_registers (int regno
)
1318 #ifdef HAVE_LINUX_REGSETS
1321 if (regsets_store_inferior_registers () == 0)
1325 #ifdef HAVE_LINUX_USRREGS
1326 usr_store_inferior_registers (regno
);
1331 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1332 to debugger memory starting at MYADDR. */
1335 linux_read_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
1338 /* Round starting address down to longword boundary. */
1339 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1340 /* Round ending address up; get number of longwords that makes. */
1342 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1343 / sizeof (PTRACE_XFER_TYPE
);
1344 /* Allocate buffer of that many longwords. */
1345 register PTRACE_XFER_TYPE
*buffer
1346 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1348 /* Read all the longwords */
1349 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1352 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1357 /* Copy appropriate bytes out of the buffer. */
1358 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1363 /* Copy LEN bytes of data from debugger memory at MYADDR
1364 to inferior's memory at MEMADDR.
1365 On failure (cannot write the inferior)
1366 returns the value of errno. */
1369 linux_write_memory (CORE_ADDR memaddr
, const char *myaddr
, int len
)
1372 /* Round starting address down to longword boundary. */
1373 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1374 /* Round ending address up; get number of longwords that makes. */
1376 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1377 /* Allocate buffer of that many longwords. */
1378 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1383 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1386 /* Fill start and end extra bytes of buffer with existing memory data. */
1388 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1389 (PTRACE_ARG3_TYPE
) addr
, 0);
1394 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1395 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1396 * sizeof (PTRACE_XFER_TYPE
)),
1400 /* Copy data to be written over corresponding part of buffer */
1402 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1404 /* Write the entire buffer. */
1406 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1409 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1418 linux_look_up_symbols (void)
1420 #ifdef USE_THREAD_DB
1424 using_threads
= thread_db_init ();
1429 linux_send_signal (int signum
)
1431 extern unsigned long signal_pid
;
1433 if (cont_thread
> 0)
1435 struct process_info
*process
;
1437 process
= get_thread_process (current_inferior
);
1438 kill_lwp (process
->lwpid
, signum
);
1441 kill_lwp (signal_pid
, signum
);
1444 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1445 to debugger memory starting at MYADDR. */
1448 linux_read_auxv (CORE_ADDR offset
, char *myaddr
, unsigned int len
)
1450 char filename
[PATH_MAX
];
1453 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1455 fd
= open (filename
, O_RDONLY
);
1459 if (offset
!= (CORE_ADDR
) 0
1460 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1463 n
= read (fd
, myaddr
, len
);
1470 /* These watchpoint related wrapper functions simply pass on the function call
1471 if the target has registered a corresponding function. */
1474 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1476 if (the_low_target
.insert_watchpoint
!= NULL
)
1477 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1479 /* Unsupported (see target.h). */
1484 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1486 if (the_low_target
.remove_watchpoint
!= NULL
)
1487 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1489 /* Unsupported (see target.h). */
1494 linux_stopped_by_watchpoint (void)
1496 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1497 return the_low_target
.stopped_by_watchpoint ();
1503 linux_stopped_data_address (void)
1505 if (the_low_target
.stopped_data_address
!= NULL
)
1506 return the_low_target
.stopped_data_address ();
1511 static struct target_ops linux_target_ops
= {
1512 linux_create_inferior
,
1519 linux_fetch_registers
,
1520 linux_store_registers
,
1523 linux_look_up_symbols
,
1526 linux_insert_watchpoint
,
1527 linux_remove_watchpoint
,
1528 linux_stopped_by_watchpoint
,
1529 linux_stopped_data_address
,
1533 linux_init_signals ()
1535 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1536 to find what the cancel signal actually is. */
1537 signal (__SIGRTMIN
+1, SIG_IGN
);
1541 initialize_low (void)
1544 set_target_ops (&linux_target_ops
);
1545 set_breakpoint_data (the_low_target
.breakpoint
,
1546 the_low_target
.breakpoint_len
);
1548 linux_init_signals ();