1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "linux-low.h"
28 #include <sys/param.h>
30 #include <sys/ptrace.h>
33 #include <sys/ioctl.h>
39 #include <sys/syscall.h>
41 /* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
42 however. This requires changing the ID in place when we go from !using_threads
43 to using_threads, immediately.
45 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
46 the same as the LWP ID. */
48 struct inferior_list all_processes
;
50 /* FIXME this is a bit of a hack, and could be removed. */
53 /* FIXME make into a target method? */
56 static void linux_resume_one_process (struct inferior_list_entry
*entry
,
57 int step
, int signal
);
58 static void linux_resume (struct thread_resume
*resume_info
);
59 static void stop_all_processes (void);
60 static int linux_wait_for_event (struct thread_info
*child
);
62 struct pending_signals
65 struct pending_signals
*prev
;
68 #define PTRACE_ARG3_TYPE long
69 #define PTRACE_XFER_TYPE long
71 #ifdef HAVE_LINUX_REGSETS
72 static int use_regsets_p
= 1;
75 int debug_threads
= 0;
77 #define pid_of(proc) ((proc)->head.id)
79 /* FIXME: Delete eventually. */
80 #define inferior_pid (pid_of (get_thread_process (current_inferior)))
82 /* This function should only be called if the process got a SIGTRAP.
83 The SIGTRAP could mean several things.
85 On i386, where decr_pc_after_break is non-zero:
86 If we were single-stepping this process using PTRACE_SINGLESTEP,
87 we will get only the one SIGTRAP (even if the instruction we
88 stepped over was a breakpoint). The value of $eip will be the
90 If we continue the process using PTRACE_CONT, we will get a
91 SIGTRAP when we hit a breakpoint. The value of $eip will be
92 the instruction after the breakpoint (i.e. needs to be
93 decremented). If we report the SIGTRAP to GDB, we must also
94 report the undecremented PC. If we cancel the SIGTRAP, we
95 must resume at the decremented PC.
97 (Presumably, not yet tested) On a non-decr_pc_after_break machine
98 with hardware or kernel single-step:
99 If we single-step over a breakpoint instruction, our PC will
100 point at the following instruction. If we continue and hit a
101 breakpoint instruction, our PC will point at the breakpoint
107 CORE_ADDR stop_pc
= (*the_low_target
.get_pc
) ();
109 if (get_thread_process (current_inferior
)->stepping
)
112 return stop_pc
- the_low_target
.decr_pc_after_break
;
116 add_process (unsigned long pid
)
118 struct process_info
*process
;
120 process
= (struct process_info
*) malloc (sizeof (*process
));
121 memset (process
, 0, sizeof (*process
));
123 process
->head
.id
= pid
;
125 /* Default to tid == lwpid == pid. */
127 process
->lwpid
= pid
;
129 add_inferior_to_list (&all_processes
, &process
->head
);
134 /* Start an inferior process and returns its pid.
135 ALLARGS is a vector of program-name and args. */
138 linux_create_inferior (char *program
, char **allargs
)
145 perror_with_name ("fork");
149 ptrace (PTRACE_TRACEME
, 0, 0, 0);
151 signal (__SIGRTMIN
+ 1, SIG_DFL
);
155 execv (program
, allargs
);
157 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
163 new_process
= add_process (pid
);
164 add_thread (pid
, new_process
, pid
);
169 /* Attach to an inferior process. */
172 linux_attach_lwp (unsigned long pid
, unsigned long tid
)
174 struct process_info
*new_process
;
176 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
178 fprintf (stderr
, "Cannot attach to process %ld: %s (%d)\n", pid
,
179 strerror (errno
), errno
);
182 /* If we fail to attach to an LWP, just return. */
188 new_process
= (struct process_info
*) add_process (pid
);
189 add_thread (tid
, new_process
, pid
);
191 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
192 brings it to a halt. We should ignore that SIGSTOP and resume the process
193 (unless this is the first process, in which case the flag will be cleared
196 On the other hand, if we are currently trying to stop all threads, we
197 should treat the new thread as if we had sent it a SIGSTOP. This works
198 because we are guaranteed that add_process added us to the end of the
199 list, and so the new thread has not yet reached wait_for_sigstop (but
201 if (! stopping_threads
)
202 new_process
->stop_expected
= 1;
206 linux_attach (unsigned long pid
)
208 struct process_info
*process
;
210 linux_attach_lwp (pid
, pid
);
212 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
213 process
= (struct process_info
*) find_inferior_id (&all_processes
, pid
);
214 process
->stop_expected
= 0;
219 /* Kill the inferior process. Make us have no inferior. */
222 linux_kill_one_process (struct inferior_list_entry
*entry
)
224 struct thread_info
*thread
= (struct thread_info
*) entry
;
225 struct process_info
*process
= get_thread_process (thread
);
228 /* We avoid killing the first thread here, because of a Linux kernel (at
229 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
230 the children get a chance to be reaped, it will remain a zombie
232 if (entry
== all_threads
.head
)
237 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
239 /* Make sure it died. The loop is most likely unnecessary. */
240 wstat
= linux_wait_for_event (thread
);
241 } while (WIFSTOPPED (wstat
));
247 struct thread_info
*thread
= (struct thread_info
*) all_threads
.head
;
248 struct process_info
*process
= get_thread_process (thread
);
251 for_each_inferior (&all_threads
, linux_kill_one_process
);
253 /* See the comment in linux_kill_one_process. We did not kill the first
254 thread in the list, so do so now. */
257 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
259 /* Make sure it died. The loop is most likely unnecessary. */
260 wstat
= linux_wait_for_event (thread
);
261 } while (WIFSTOPPED (wstat
));
265 linux_detach_one_process (struct inferior_list_entry
*entry
)
267 struct thread_info
*thread
= (struct thread_info
*) entry
;
268 struct process_info
*process
= get_thread_process (thread
);
270 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
276 for_each_inferior (&all_threads
, linux_detach_one_process
);
279 /* Return nonzero if the given thread is still alive. */
281 linux_thread_alive (unsigned long tid
)
283 if (find_inferior_id (&all_threads
, tid
) != NULL
)
289 /* Return nonzero if this process stopped at a breakpoint which
290 no longer appears to be inserted. Also adjust the PC
291 appropriately to resume where the breakpoint used to be. */
293 check_removed_breakpoint (struct process_info
*event_child
)
296 struct thread_info
*saved_inferior
;
298 if (event_child
->pending_is_breakpoint
== 0)
302 fprintf (stderr
, "Checking for breakpoint.\n");
304 saved_inferior
= current_inferior
;
305 current_inferior
= get_process_thread (event_child
);
307 stop_pc
= get_stop_pc ();
309 /* If the PC has changed since we stopped, then we shouldn't do
310 anything. This happens if, for instance, GDB handled the
311 decr_pc_after_break subtraction itself. */
312 if (stop_pc
!= event_child
->pending_stop_pc
)
315 fprintf (stderr
, "Ignoring, PC was changed.\n");
317 event_child
->pending_is_breakpoint
= 0;
318 current_inferior
= saved_inferior
;
322 /* If the breakpoint is still there, we will report hitting it. */
323 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
326 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
327 current_inferior
= saved_inferior
;
332 fprintf (stderr
, "Removed breakpoint.\n");
334 /* For decr_pc_after_break targets, here is where we perform the
335 decrement. We go immediately from this function to resuming,
336 and can not safely call get_stop_pc () again. */
337 if (the_low_target
.set_pc
!= NULL
)
338 (*the_low_target
.set_pc
) (stop_pc
);
340 /* We consumed the pending SIGTRAP. */
341 event_child
->pending_is_breakpoint
= 0;
342 event_child
->status_pending_p
= 0;
343 event_child
->status_pending
= 0;
345 current_inferior
= saved_inferior
;
349 /* Return 1 if this process has an interesting status pending. This function
350 may silently resume an inferior process. */
352 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
354 struct process_info
*process
= (struct process_info
*) entry
;
356 if (process
->status_pending_p
)
357 if (check_removed_breakpoint (process
))
359 /* This thread was stopped at a breakpoint, and the breakpoint
360 is now gone. We were told to continue (or step...) all threads,
361 so GDB isn't trying to single-step past this breakpoint.
362 So instead of reporting the old SIGTRAP, pretend we got to
363 the breakpoint just after it was removed instead of just
364 before; resume the process. */
365 linux_resume_one_process (&process
->head
, 0, 0);
369 return process
->status_pending_p
;
373 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
376 int to_wait_for
= -1;
379 to_wait_for
= (*childp
)->lwpid
;
383 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
388 perror_with_name ("waitpid");
393 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
398 perror_with_name ("waitpid (WCLONE)");
407 && (!WIFSTOPPED (*wstatp
)
408 || (WSTOPSIG (*wstatp
) != 32
409 && WSTOPSIG (*wstatp
) != 33)))
410 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
412 if (to_wait_for
== -1)
413 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
415 (*childp
)->stopped
= 1;
416 (*childp
)->pending_is_breakpoint
= 0;
419 && WIFSTOPPED (*wstatp
))
421 current_inferior
= (struct thread_info
*)
422 find_inferior_id (&all_threads
, (*childp
)->tid
);
423 /* For testing only; i386_stop_pc prints out a diagnostic. */
424 if (the_low_target
.get_pc
!= NULL
)
430 linux_wait_for_event (struct thread_info
*child
)
433 struct process_info
*event_child
;
436 /* Check for a process with a pending status. */
437 /* It is possible that the user changed the pending task's registers since
438 it stopped. We correctly handle the change of PC if we hit a breakpoint
439 (in check_removed_breakpoint); signals should be reported anyway. */
442 event_child
= (struct process_info
*)
443 find_inferior (&all_processes
, status_pending_p
, NULL
);
444 if (debug_threads
&& event_child
)
445 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
449 event_child
= get_thread_process (child
);
450 if (event_child
->status_pending_p
451 && check_removed_breakpoint (event_child
))
455 if (event_child
!= NULL
)
457 if (event_child
->status_pending_p
)
460 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
461 event_child
->lwpid
, event_child
->status_pending
);
462 wstat
= event_child
->status_pending
;
463 event_child
->status_pending_p
= 0;
464 event_child
->status_pending
= 0;
465 current_inferior
= get_process_thread (event_child
);
470 /* We only enter this loop if no process has a pending wait status. Thus
471 any action taken in response to a wait status inside this loop is
472 responding as soon as we detect the status, not after any pending
479 event_child
= get_thread_process (child
);
481 linux_wait_for_process (&event_child
, &wstat
);
483 if (event_child
== NULL
)
484 error ("event from unknown child");
486 current_inferior
= (struct thread_info
*)
487 find_inferior_id (&all_threads
, event_child
->tid
);
491 /* Check for thread exit. */
492 if (! WIFSTOPPED (wstat
))
495 fprintf (stderr
, "Thread %ld (LWP %ld) exiting\n",
496 event_child
->tid
, event_child
->head
.id
);
498 /* If the last thread is exiting, just return. */
499 if (all_threads
.head
== all_threads
.tail
)
502 dead_thread_notify (event_child
->tid
);
504 remove_inferior (&all_processes
, &event_child
->head
);
506 remove_thread (current_inferior
);
507 current_inferior
= (struct thread_info
*) all_threads
.head
;
509 /* If we were waiting for this particular child to do something...
510 well, it did something. */
514 /* Wait for a more interesting event. */
518 if (WIFSTOPPED (wstat
)
519 && WSTOPSIG (wstat
) == SIGSTOP
520 && event_child
->stop_expected
)
523 fprintf (stderr
, "Expected stop.\n");
524 event_child
->stop_expected
= 0;
525 linux_resume_one_process (&event_child
->head
,
526 event_child
->stepping
, 0);
530 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
532 if (WIFSTOPPED (wstat
)
533 && (WSTOPSIG (wstat
) == __SIGRTMIN
534 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
537 fprintf (stderr
, "Ignored signal %d for %ld (LWP %ld).\n",
538 WSTOPSIG (wstat
), event_child
->tid
,
539 event_child
->head
.id
);
540 linux_resume_one_process (&event_child
->head
,
541 event_child
->stepping
,
547 /* If this event was not handled above, and is not a SIGTRAP, report
549 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
552 /* If this target does not support breakpoints, we simply report the
553 SIGTRAP; it's of no concern to us. */
554 if (the_low_target
.get_pc
== NULL
)
557 stop_pc
= get_stop_pc ();
559 /* bp_reinsert will only be set if we were single-stepping.
560 Notice that we will resume the process after hitting
561 a gdbserver breakpoint; single-stepping to/over one
562 is not supported (yet). */
563 if (event_child
->bp_reinsert
!= 0)
566 fprintf (stderr
, "Reinserted breakpoint.\n");
567 reinsert_breakpoint (event_child
->bp_reinsert
);
568 event_child
->bp_reinsert
= 0;
570 /* Clear the single-stepping flag and SIGTRAP as we resume. */
571 linux_resume_one_process (&event_child
->head
, 0, 0);
576 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
578 if (check_breakpoints (stop_pc
) != 0)
580 /* We hit one of our own breakpoints. We mark it as a pending
581 breakpoint, so that check_removed_breakpoint () will do the PC
582 adjustment for us at the appropriate time. */
583 event_child
->pending_is_breakpoint
= 1;
584 event_child
->pending_stop_pc
= stop_pc
;
586 /* Now we need to put the breakpoint back. We continue in the event
587 loop instead of simply replacing the breakpoint right away,
588 in order to not lose signals sent to the thread that hit the
589 breakpoint. Unfortunately this increases the window where another
590 thread could sneak past the removed breakpoint. For the current
591 use of server-side breakpoints (thread creation) this is
592 acceptable; but it needs to be considered before this breakpoint
593 mechanism can be used in more general ways. For some breakpoints
594 it may be necessary to stop all other threads, but that should
595 be avoided where possible.
597 If breakpoint_reinsert_addr is NULL, that means that we can
598 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
599 mark it for reinsertion, and single-step.
601 Otherwise, call the target function to figure out where we need
602 our temporary breakpoint, create it, and continue executing this
604 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
606 event_child
->bp_reinsert
= stop_pc
;
607 uninsert_breakpoint (stop_pc
);
608 linux_resume_one_process (&event_child
->head
, 1, 0);
612 reinsert_breakpoint_by_bp
613 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
614 linux_resume_one_process (&event_child
->head
, 0, 0);
620 /* If we were single-stepping, we definitely want to report the
621 SIGTRAP. The single-step operation has completed, so also
622 clear the stepping flag; in general this does not matter,
623 because the SIGTRAP will be reported to the client, which
624 will give us a new action for this thread, but clear it for
625 consistency anyway. It's safe to clear the stepping flag
626 because the only consumer of get_stop_pc () after this point
627 is check_removed_breakpoint, and pending_is_breakpoint is not
628 set. It might be wiser to use a step_completed flag instead. */
629 if (event_child
->stepping
)
631 event_child
->stepping
= 0;
635 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
636 Check if it is a breakpoint, and if so mark the process information
637 accordingly. This will handle both the necessary fiddling with the
638 PC on decr_pc_after_break targets and suppressing extra threads
639 hitting a breakpoint if two hit it at once and then GDB removes it
640 after the first is reported. Arguably it would be better to report
641 multiple threads hitting breakpoints simultaneously, but the current
642 remote protocol does not allow this. */
643 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
645 event_child
->pending_is_breakpoint
= 1;
646 event_child
->pending_stop_pc
= stop_pc
;
656 /* Wait for process, returns status. */
659 linux_wait (char *status
)
662 struct thread_info
*child
= NULL
;
665 /* If we were only supposed to resume one thread, only wait for
666 that thread - if it's still alive. If it died, however - which
667 can happen if we're coming from the thread death case below -
668 then we need to make sure we restart the other threads. We could
669 pick a thread at random or restart all; restarting all is less
671 if (cont_thread
!= 0 && cont_thread
!= -1)
673 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
676 /* No stepping, no signal - unless one is pending already, of course. */
679 struct thread_resume resume_info
;
680 resume_info
.thread
= -1;
681 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
682 linux_resume (&resume_info
);
688 w
= linux_wait_for_event (child
);
689 stop_all_processes ();
692 /* If we are waiting for a particular child, and it exited,
693 linux_wait_for_event will return its exit status. Similarly if
694 the last child exited. If this is not the last child, however,
695 do not report it as exited until there is a 'thread exited' response
696 available in the remote protocol. Instead, just wait for another event.
697 This should be safe, because if the thread crashed we will already
698 have reported the termination signal to GDB; that should stop any
699 in-progress stepping operations, etc.
701 Report the exit status of the last thread to exit. This matches
702 LinuxThreads' behavior. */
704 if (all_threads
.head
== all_threads
.tail
)
708 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
711 free (all_processes
.head
);
712 all_processes
.head
= all_processes
.tail
= NULL
;
713 return ((unsigned char) WEXITSTATUS (w
));
715 else if (!WIFSTOPPED (w
))
717 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
720 free (all_processes
.head
);
721 all_processes
.head
= all_processes
.tail
= NULL
;
722 return ((unsigned char) WTERMSIG (w
));
732 return ((unsigned char) WSTOPSIG (w
));
735 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
736 thread groups are in use, we need to use tkill. */
739 kill_lwp (unsigned long lwpid
, int signo
)
741 static int tkill_failed
;
748 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
756 return kill (lwpid
, signo
);
760 send_sigstop (struct inferior_list_entry
*entry
)
762 struct process_info
*process
= (struct process_info
*) entry
;
764 if (process
->stopped
)
767 /* If we already have a pending stop signal for this process, don't
769 if (process
->stop_expected
)
771 process
->stop_expected
= 0;
776 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
778 kill_lwp (process
->head
.id
, SIGSTOP
);
779 process
->sigstop_sent
= 1;
783 wait_for_sigstop (struct inferior_list_entry
*entry
)
785 struct process_info
*process
= (struct process_info
*) entry
;
786 struct thread_info
*saved_inferior
, *thread
;
788 unsigned long saved_tid
;
790 if (process
->stopped
)
793 saved_inferior
= current_inferior
;
794 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
795 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
797 wstat
= linux_wait_for_event (thread
);
799 /* If we stopped with a non-SIGSTOP signal, save it for later
800 and record the pending SIGSTOP. If the process exited, just
802 if (WIFSTOPPED (wstat
)
803 && WSTOPSIG (wstat
) != SIGSTOP
)
806 fprintf (stderr
, "Stopped with non-sigstop signal\n");
807 process
->status_pending_p
= 1;
808 process
->status_pending
= wstat
;
809 process
->stop_expected
= 1;
812 if (linux_thread_alive (saved_tid
))
813 current_inferior
= saved_inferior
;
817 fprintf (stderr
, "Previously current thread died.\n");
819 /* Set a valid thread as current. */
820 set_desired_inferior (0);
825 stop_all_processes (void)
827 stopping_threads
= 1;
828 for_each_inferior (&all_processes
, send_sigstop
);
829 for_each_inferior (&all_processes
, wait_for_sigstop
);
830 stopping_threads
= 0;
833 /* Resume execution of the inferior process.
834 If STEP is nonzero, single-step it.
835 If SIGNAL is nonzero, give it that signal. */
838 linux_resume_one_process (struct inferior_list_entry
*entry
,
839 int step
, int signal
)
841 struct process_info
*process
= (struct process_info
*) entry
;
842 struct thread_info
*saved_inferior
;
844 if (process
->stopped
== 0)
847 /* If we have pending signals or status, and a new signal, enqueue the
848 signal. Also enqueue the signal if we are waiting to reinsert a
849 breakpoint; it will be picked up again below. */
851 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
852 || process
->bp_reinsert
!= 0))
854 struct pending_signals
*p_sig
;
855 p_sig
= malloc (sizeof (*p_sig
));
856 p_sig
->prev
= process
->pending_signals
;
857 p_sig
->signal
= signal
;
858 process
->pending_signals
= p_sig
;
861 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
864 saved_inferior
= current_inferior
;
865 current_inferior
= get_process_thread (process
);
868 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
869 step
? "step" : "continue", signal
,
870 process
->stop_expected
? "expected" : "not expected");
872 /* This bit needs some thinking about. If we get a signal that
873 we must report while a single-step reinsert is still pending,
874 we often end up resuming the thread. It might be better to
875 (ew) allow a stack of pending events; then we could be sure that
876 the reinsert happened right away and not lose any signals.
878 Making this stack would also shrink the window in which breakpoints are
879 uninserted (see comment in linux_wait_for_process) but not enough for
880 complete correctness, so it won't solve that problem. It may be
881 worthwhile just to solve this one, however. */
882 if (process
->bp_reinsert
!= 0)
885 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
887 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
890 /* Postpone any pending signal. It was enqueued above. */
894 check_removed_breakpoint (process
);
896 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
898 fprintf (stderr
, " ");
899 (long) (*the_low_target
.get_pc
) ();
902 /* If we have pending signals, consume one unless we are trying to reinsert
904 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
906 struct pending_signals
**p_sig
;
908 p_sig
= &process
->pending_signals
;
909 while ((*p_sig
)->prev
!= NULL
)
910 p_sig
= &(*p_sig
)->prev
;
912 signal
= (*p_sig
)->signal
;
917 regcache_invalidate_one ((struct inferior_list_entry
*)
918 get_process_thread (process
));
920 process
->stopped
= 0;
921 process
->stepping
= step
;
922 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
924 current_inferior
= saved_inferior
;
926 perror_with_name ("ptrace");
929 static struct thread_resume
*resume_ptr
;
931 /* This function is called once per thread. We look up the thread
932 in RESUME_PTR, and mark the thread with a pointer to the appropriate
935 This algorithm is O(threads * resume elements), but resume elements
936 is small (and will remain small at least until GDB supports thread
939 linux_set_resume_request (struct inferior_list_entry
*entry
)
941 struct process_info
*process
;
942 struct thread_info
*thread
;
945 thread
= (struct thread_info
*) entry
;
946 process
= get_thread_process (thread
);
949 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
952 process
->resume
= &resume_ptr
[ndx
];
955 /* This function is called once per thread. We check the thread's resume
956 request, which will tell us whether to resume, step, or leave the thread
957 stopped; and what signal, if any, it should be sent. For threads which
958 we aren't explicitly told otherwise, we preserve the stepping flag; this
959 is used for stepping over gdbserver-placed breakpoints. */
962 linux_continue_one_thread (struct inferior_list_entry
*entry
)
964 struct process_info
*process
;
965 struct thread_info
*thread
;
968 thread
= (struct thread_info
*) entry
;
969 process
= get_thread_process (thread
);
971 if (process
->resume
->leave_stopped
)
974 if (process
->resume
->thread
== -1)
975 step
= process
->stepping
|| process
->resume
->step
;
977 step
= process
->resume
->step
;
979 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
);
981 process
->resume
= NULL
;
984 /* This function is called once per thread. We check the thread's resume
985 request, which will tell us whether to resume, step, or leave the thread
986 stopped; and what signal, if any, it should be sent. We queue any needed
987 signals, since we won't actually resume. We already have a pending event
988 to report, so we don't need to preserve any step requests; they should
989 be re-issued if necessary. */
992 linux_queue_one_thread (struct inferior_list_entry
*entry
)
994 struct process_info
*process
;
995 struct thread_info
*thread
;
997 thread
= (struct thread_info
*) entry
;
998 process
= get_thread_process (thread
);
1000 if (process
->resume
->leave_stopped
)
1003 /* If we have a new signal, enqueue the signal. */
1004 if (process
->resume
->sig
!= 0)
1006 struct pending_signals
*p_sig
;
1007 p_sig
= malloc (sizeof (*p_sig
));
1008 p_sig
->prev
= process
->pending_signals
;
1009 p_sig
->signal
= process
->resume
->sig
;
1010 process
->pending_signals
= p_sig
;
1013 process
->resume
= NULL
;
1016 /* Set DUMMY if this process has an interesting status pending. */
1018 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1020 struct process_info
*process
= (struct process_info
*) entry
;
1022 /* Processes which will not be resumed are not interesting, because
1023 we might not wait for them next time through linux_wait. */
1024 if (process
->resume
->leave_stopped
)
1027 /* If this thread has a removed breakpoint, we won't have any
1028 events to report later, so check now. check_removed_breakpoint
1029 may clear status_pending_p. We avoid calling check_removed_breakpoint
1030 for any thread that we are not otherwise going to resume - this
1031 lets us preserve stopped status when two threads hit a breakpoint.
1032 GDB removes the breakpoint to single-step a particular thread
1033 past it, then re-inserts it and resumes all threads. We want
1034 to report the second thread without resuming it in the interim. */
1035 if (process
->status_pending_p
)
1036 check_removed_breakpoint (process
);
1038 if (process
->status_pending_p
)
1039 * (int *) flag_p
= 1;
1045 linux_resume (struct thread_resume
*resume_info
)
1049 /* Yes, the use of a global here is rather ugly. */
1050 resume_ptr
= resume_info
;
1052 for_each_inferior (&all_threads
, linux_set_resume_request
);
1054 /* If there is a thread which would otherwise be resumed, which
1055 has a pending status, then don't resume any threads - we can just
1056 report the pending status. Make sure to queue any signals
1057 that would otherwise be sent. */
1059 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1064 fprintf (stderr
, "Not resuming, pending status\n");
1066 fprintf (stderr
, "Resuming, no pending status\n");
1070 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1075 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1079 #ifdef HAVE_LINUX_USRREGS
1082 register_addr (int regnum
)
1086 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1087 error ("Invalid register number %d.", regnum
);
1089 addr
= the_low_target
.regmap
[regnum
];
1094 /* Fetch one register. */
1096 fetch_register (int regno
)
1102 if (regno
>= the_low_target
.num_regs
)
1104 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1107 regaddr
= register_addr (regno
);
1110 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1111 & - sizeof (PTRACE_XFER_TYPE
);
1112 buf
= alloca (size
);
1113 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1116 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1117 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1118 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1121 /* Warning, not error, in case we are attached; sometimes the
1122 kernel doesn't let us at the registers. */
1123 char *err
= strerror (errno
);
1124 char *msg
= alloca (strlen (err
) + 128);
1125 sprintf (msg
, "reading register %d: %s", regno
, err
);
1130 if (the_low_target
.left_pad_xfer
1131 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1132 supply_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1133 - register_size (regno
)));
1135 supply_register (regno
, buf
);
1140 /* Fetch all registers, or just one, from the child process. */
1142 usr_fetch_inferior_registers (int regno
)
1144 if (regno
== -1 || regno
== 0)
1145 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1146 fetch_register (regno
);
1148 fetch_register (regno
);
1151 /* Store our register values back into the inferior.
1152 If REGNO is -1, do this for all registers.
1153 Otherwise, REGNO specifies which register (so we can save time). */
1155 usr_store_inferior_registers (int regno
)
1163 if (regno
>= the_low_target
.num_regs
)
1166 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1169 regaddr
= register_addr (regno
);
1173 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1174 & - sizeof (PTRACE_XFER_TYPE
);
1175 buf
= alloca (size
);
1176 memset (buf
, 0, size
);
1177 if (the_low_target
.left_pad_xfer
1178 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1179 collect_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1180 - register_size (regno
)));
1182 collect_register (regno
, buf
);
1183 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1186 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1187 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1190 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1192 char *err
= strerror (errno
);
1193 char *msg
= alloca (strlen (err
) + 128);
1194 sprintf (msg
, "writing register %d: %s",
1200 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1204 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1205 usr_store_inferior_registers (regno
);
1207 #endif /* HAVE_LINUX_USRREGS */
1211 #ifdef HAVE_LINUX_REGSETS
1214 regsets_fetch_inferior_registers ()
1216 struct regset_info
*regset
;
1217 int saw_general_regs
= 0;
1219 regset
= target_regsets
;
1221 while (regset
->size
>= 0)
1226 if (regset
->size
== 0)
1232 buf
= malloc (regset
->size
);
1233 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1238 /* If we get EIO on the first regset, do not try regsets again.
1239 If we get EIO on a later regset, disable that regset. */
1240 if (regset
== target_regsets
)
1254 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1259 else if (regset
->type
== GENERAL_REGS
)
1260 saw_general_regs
= 1;
1261 regset
->store_function (buf
);
1264 if (saw_general_regs
)
1271 regsets_store_inferior_registers ()
1273 struct regset_info
*regset
;
1274 int saw_general_regs
= 0;
1276 regset
= target_regsets
;
1278 while (regset
->size
>= 0)
1283 if (regset
->size
== 0)
1289 buf
= malloc (regset
->size
);
1291 /* First fill the buffer with the current register set contents,
1292 in case there are any items in the kernel's regset that are
1293 not in gdbserver's regcache. */
1294 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1298 /* Then overlay our cached registers on that. */
1299 regset
->fill_function (buf
);
1301 /* Only now do we write the register set. */
1302 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1309 /* If we get EIO on the first regset, do not try regsets again.
1310 If we get EIO on a later regset, disable that regset. */
1311 if (regset
== target_regsets
)
1324 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1327 else if (regset
->type
== GENERAL_REGS
)
1328 saw_general_regs
= 1;
1332 if (saw_general_regs
)
1339 #endif /* HAVE_LINUX_REGSETS */
1343 linux_fetch_registers (int regno
)
1345 #ifdef HAVE_LINUX_REGSETS
1348 if (regsets_fetch_inferior_registers () == 0)
1352 #ifdef HAVE_LINUX_USRREGS
1353 usr_fetch_inferior_registers (regno
);
1358 linux_store_registers (int regno
)
1360 #ifdef HAVE_LINUX_REGSETS
1363 if (regsets_store_inferior_registers () == 0)
1367 #ifdef HAVE_LINUX_USRREGS
1368 usr_store_inferior_registers (regno
);
1373 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1374 to debugger memory starting at MYADDR. */
1377 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1380 /* Round starting address down to longword boundary. */
1381 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1382 /* Round ending address up; get number of longwords that makes. */
1384 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1385 / sizeof (PTRACE_XFER_TYPE
);
1386 /* Allocate buffer of that many longwords. */
1387 register PTRACE_XFER_TYPE
*buffer
1388 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1390 /* Read all the longwords */
1391 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1394 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1399 /* Copy appropriate bytes out of the buffer. */
1400 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1405 /* Copy LEN bytes of data from debugger memory at MYADDR
1406 to inferior's memory at MEMADDR.
1407 On failure (cannot write the inferior)
1408 returns the value of errno. */
1411 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
1414 /* Round starting address down to longword boundary. */
1415 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1416 /* Round ending address up; get number of longwords that makes. */
1418 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1419 /* Allocate buffer of that many longwords. */
1420 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1425 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1428 /* Fill start and end extra bytes of buffer with existing memory data. */
1430 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1431 (PTRACE_ARG3_TYPE
) addr
, 0);
1436 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1437 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1438 * sizeof (PTRACE_XFER_TYPE
)),
1442 /* Copy data to be written over corresponding part of buffer */
1444 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1446 /* Write the entire buffer. */
1448 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1451 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1460 linux_look_up_symbols (void)
1462 #ifdef USE_THREAD_DB
1466 using_threads
= thread_db_init ();
1471 linux_send_signal (int signum
)
1473 extern unsigned long signal_pid
;
1475 if (cont_thread
!= 0 && cont_thread
!= -1)
1477 struct process_info
*process
;
1479 process
= get_thread_process (current_inferior
);
1480 kill_lwp (process
->lwpid
, signum
);
1483 kill_lwp (signal_pid
, signum
);
1486 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1487 to debugger memory starting at MYADDR. */
1490 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
1492 char filename
[PATH_MAX
];
1495 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1497 fd
= open (filename
, O_RDONLY
);
1501 if (offset
!= (CORE_ADDR
) 0
1502 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1505 n
= read (fd
, myaddr
, len
);
1512 /* These watchpoint related wrapper functions simply pass on the function call
1513 if the target has registered a corresponding function. */
1516 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1518 if (the_low_target
.insert_watchpoint
!= NULL
)
1519 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1521 /* Unsupported (see target.h). */
1526 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1528 if (the_low_target
.remove_watchpoint
!= NULL
)
1529 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1531 /* Unsupported (see target.h). */
1536 linux_stopped_by_watchpoint (void)
1538 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1539 return the_low_target
.stopped_by_watchpoint ();
1545 linux_stopped_data_address (void)
1547 if (the_low_target
.stopped_data_address
!= NULL
)
1548 return the_low_target
.stopped_data_address ();
1553 static struct target_ops linux_target_ops
= {
1554 linux_create_inferior
,
1561 linux_fetch_registers
,
1562 linux_store_registers
,
1565 linux_look_up_symbols
,
1568 linux_insert_watchpoint
,
1569 linux_remove_watchpoint
,
1570 linux_stopped_by_watchpoint
,
1571 linux_stopped_data_address
,
1575 linux_init_signals ()
1577 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1578 to find what the cancel signal actually is. */
1579 signal (__SIGRTMIN
+1, SIG_IGN
);
1583 initialize_low (void)
1586 set_target_ops (&linux_target_ops
);
1587 set_breakpoint_data (the_low_target
.breakpoint
,
1588 the_low_target
.breakpoint_len
);
1590 linux_init_signals ();