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
)
143 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
149 perror_with_name ("fork");
153 ptrace (PTRACE_TRACEME
, 0, 0, 0);
155 signal (__SIGRTMIN
+ 1, SIG_DFL
);
159 execv (program
, allargs
);
161 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
167 new_process
= add_process (pid
);
168 add_thread (pid
, new_process
, pid
);
173 /* Attach to an inferior process. */
176 linux_attach_lwp (unsigned long pid
, unsigned long tid
)
178 struct process_info
*new_process
;
180 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
182 fprintf (stderr
, "Cannot attach to process %ld: %s (%d)\n", pid
,
183 strerror (errno
), errno
);
186 /* If we fail to attach to an LWP, just return. */
192 new_process
= (struct process_info
*) add_process (pid
);
193 add_thread (tid
, new_process
, pid
);
195 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
196 brings it to a halt. We should ignore that SIGSTOP and resume the process
197 (unless this is the first process, in which case the flag will be cleared
200 On the other hand, if we are currently trying to stop all threads, we
201 should treat the new thread as if we had sent it a SIGSTOP. This works
202 because we are guaranteed that add_process added us to the end of the
203 list, and so the new thread has not yet reached wait_for_sigstop (but
205 if (! stopping_threads
)
206 new_process
->stop_expected
= 1;
210 linux_attach (unsigned long pid
)
212 struct process_info
*process
;
214 linux_attach_lwp (pid
, pid
);
216 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
217 process
= (struct process_info
*) find_inferior_id (&all_processes
, pid
);
218 process
->stop_expected
= 0;
223 /* Kill the inferior process. Make us have no inferior. */
226 linux_kill_one_process (struct inferior_list_entry
*entry
)
228 struct thread_info
*thread
= (struct thread_info
*) entry
;
229 struct process_info
*process
= get_thread_process (thread
);
232 /* We avoid killing the first thread here, because of a Linux kernel (at
233 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
234 the children get a chance to be reaped, it will remain a zombie
236 if (entry
== all_threads
.head
)
241 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
243 /* Make sure it died. The loop is most likely unnecessary. */
244 wstat
= linux_wait_for_event (thread
);
245 } while (WIFSTOPPED (wstat
));
251 struct thread_info
*thread
= (struct thread_info
*) all_threads
.head
;
252 struct process_info
*process
= get_thread_process (thread
);
255 for_each_inferior (&all_threads
, linux_kill_one_process
);
257 /* See the comment in linux_kill_one_process. We did not kill the first
258 thread in the list, so do so now. */
261 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
263 /* Make sure it died. The loop is most likely unnecessary. */
264 wstat
= linux_wait_for_event (thread
);
265 } while (WIFSTOPPED (wstat
));
269 linux_detach_one_process (struct inferior_list_entry
*entry
)
271 struct thread_info
*thread
= (struct thread_info
*) entry
;
272 struct process_info
*process
= get_thread_process (thread
);
274 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
280 for_each_inferior (&all_threads
, linux_detach_one_process
);
283 /* Return nonzero if the given thread is still alive. */
285 linux_thread_alive (unsigned long tid
)
287 if (find_inferior_id (&all_threads
, tid
) != NULL
)
293 /* Return nonzero if this process stopped at a breakpoint which
294 no longer appears to be inserted. Also adjust the PC
295 appropriately to resume where the breakpoint used to be. */
297 check_removed_breakpoint (struct process_info
*event_child
)
300 struct thread_info
*saved_inferior
;
302 if (event_child
->pending_is_breakpoint
== 0)
306 fprintf (stderr
, "Checking for breakpoint.\n");
308 saved_inferior
= current_inferior
;
309 current_inferior
= get_process_thread (event_child
);
311 stop_pc
= get_stop_pc ();
313 /* If the PC has changed since we stopped, then we shouldn't do
314 anything. This happens if, for instance, GDB handled the
315 decr_pc_after_break subtraction itself. */
316 if (stop_pc
!= event_child
->pending_stop_pc
)
319 fprintf (stderr
, "Ignoring, PC was changed.\n");
321 event_child
->pending_is_breakpoint
= 0;
322 current_inferior
= saved_inferior
;
326 /* If the breakpoint is still there, we will report hitting it. */
327 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
330 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
331 current_inferior
= saved_inferior
;
336 fprintf (stderr
, "Removed breakpoint.\n");
338 /* For decr_pc_after_break targets, here is where we perform the
339 decrement. We go immediately from this function to resuming,
340 and can not safely call get_stop_pc () again. */
341 if (the_low_target
.set_pc
!= NULL
)
342 (*the_low_target
.set_pc
) (stop_pc
);
344 /* We consumed the pending SIGTRAP. */
345 event_child
->pending_is_breakpoint
= 0;
346 event_child
->status_pending_p
= 0;
347 event_child
->status_pending
= 0;
349 current_inferior
= saved_inferior
;
353 /* Return 1 if this process has an interesting status pending. This function
354 may silently resume an inferior process. */
356 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
358 struct process_info
*process
= (struct process_info
*) entry
;
360 if (process
->status_pending_p
)
361 if (check_removed_breakpoint (process
))
363 /* This thread was stopped at a breakpoint, and the breakpoint
364 is now gone. We were told to continue (or step...) all threads,
365 so GDB isn't trying to single-step past this breakpoint.
366 So instead of reporting the old SIGTRAP, pretend we got to
367 the breakpoint just after it was removed instead of just
368 before; resume the process. */
369 linux_resume_one_process (&process
->head
, 0, 0);
373 return process
->status_pending_p
;
377 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
380 int to_wait_for
= -1;
383 to_wait_for
= (*childp
)->lwpid
;
387 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
392 perror_with_name ("waitpid");
397 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
402 perror_with_name ("waitpid (WCLONE)");
411 && (!WIFSTOPPED (*wstatp
)
412 || (WSTOPSIG (*wstatp
) != 32
413 && WSTOPSIG (*wstatp
) != 33)))
414 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
416 if (to_wait_for
== -1)
417 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
419 (*childp
)->stopped
= 1;
420 (*childp
)->pending_is_breakpoint
= 0;
423 && WIFSTOPPED (*wstatp
))
425 current_inferior
= (struct thread_info
*)
426 find_inferior_id (&all_threads
, (*childp
)->tid
);
427 /* For testing only; i386_stop_pc prints out a diagnostic. */
428 if (the_low_target
.get_pc
!= NULL
)
434 linux_wait_for_event (struct thread_info
*child
)
437 struct process_info
*event_child
;
440 /* Check for a process with a pending status. */
441 /* It is possible that the user changed the pending task's registers since
442 it stopped. We correctly handle the change of PC if we hit a breakpoint
443 (in check_removed_breakpoint); signals should be reported anyway. */
446 event_child
= (struct process_info
*)
447 find_inferior (&all_processes
, status_pending_p
, NULL
);
448 if (debug_threads
&& event_child
)
449 fprintf (stderr
, "Got a pending child %ld\n", event_child
->lwpid
);
453 event_child
= get_thread_process (child
);
454 if (event_child
->status_pending_p
455 && check_removed_breakpoint (event_child
))
459 if (event_child
!= NULL
)
461 if (event_child
->status_pending_p
)
464 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
465 event_child
->lwpid
, event_child
->status_pending
);
466 wstat
= event_child
->status_pending
;
467 event_child
->status_pending_p
= 0;
468 event_child
->status_pending
= 0;
469 current_inferior
= get_process_thread (event_child
);
474 /* We only enter this loop if no process has a pending wait status. Thus
475 any action taken in response to a wait status inside this loop is
476 responding as soon as we detect the status, not after any pending
483 event_child
= get_thread_process (child
);
485 linux_wait_for_process (&event_child
, &wstat
);
487 if (event_child
== NULL
)
488 error ("event from unknown child");
490 current_inferior
= (struct thread_info
*)
491 find_inferior_id (&all_threads
, event_child
->tid
);
495 /* Check for thread exit. */
496 if (! WIFSTOPPED (wstat
))
499 fprintf (stderr
, "Thread %ld (LWP %ld) exiting\n",
500 event_child
->tid
, event_child
->head
.id
);
502 /* If the last thread is exiting, just return. */
503 if (all_threads
.head
== all_threads
.tail
)
506 dead_thread_notify (event_child
->tid
);
508 remove_inferior (&all_processes
, &event_child
->head
);
510 remove_thread (current_inferior
);
511 current_inferior
= (struct thread_info
*) all_threads
.head
;
513 /* If we were waiting for this particular child to do something...
514 well, it did something. */
518 /* Wait for a more interesting event. */
522 if (WIFSTOPPED (wstat
)
523 && WSTOPSIG (wstat
) == SIGSTOP
524 && event_child
->stop_expected
)
527 fprintf (stderr
, "Expected stop.\n");
528 event_child
->stop_expected
= 0;
529 linux_resume_one_process (&event_child
->head
,
530 event_child
->stepping
, 0);
534 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
536 if (WIFSTOPPED (wstat
)
537 && (WSTOPSIG (wstat
) == __SIGRTMIN
538 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
541 fprintf (stderr
, "Ignored signal %d for %ld (LWP %ld).\n",
542 WSTOPSIG (wstat
), event_child
->tid
,
543 event_child
->head
.id
);
544 linux_resume_one_process (&event_child
->head
,
545 event_child
->stepping
,
551 /* If this event was not handled above, and is not a SIGTRAP, report
553 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
556 /* If this target does not support breakpoints, we simply report the
557 SIGTRAP; it's of no concern to us. */
558 if (the_low_target
.get_pc
== NULL
)
561 stop_pc
= get_stop_pc ();
563 /* bp_reinsert will only be set if we were single-stepping.
564 Notice that we will resume the process after hitting
565 a gdbserver breakpoint; single-stepping to/over one
566 is not supported (yet). */
567 if (event_child
->bp_reinsert
!= 0)
570 fprintf (stderr
, "Reinserted breakpoint.\n");
571 reinsert_breakpoint (event_child
->bp_reinsert
);
572 event_child
->bp_reinsert
= 0;
574 /* Clear the single-stepping flag and SIGTRAP as we resume. */
575 linux_resume_one_process (&event_child
->head
, 0, 0);
580 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
582 if (check_breakpoints (stop_pc
) != 0)
584 /* We hit one of our own breakpoints. We mark it as a pending
585 breakpoint, so that check_removed_breakpoint () will do the PC
586 adjustment for us at the appropriate time. */
587 event_child
->pending_is_breakpoint
= 1;
588 event_child
->pending_stop_pc
= stop_pc
;
590 /* Now we need to put the breakpoint back. We continue in the event
591 loop instead of simply replacing the breakpoint right away,
592 in order to not lose signals sent to the thread that hit the
593 breakpoint. Unfortunately this increases the window where another
594 thread could sneak past the removed breakpoint. For the current
595 use of server-side breakpoints (thread creation) this is
596 acceptable; but it needs to be considered before this breakpoint
597 mechanism can be used in more general ways. For some breakpoints
598 it may be necessary to stop all other threads, but that should
599 be avoided where possible.
601 If breakpoint_reinsert_addr is NULL, that means that we can
602 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
603 mark it for reinsertion, and single-step.
605 Otherwise, call the target function to figure out where we need
606 our temporary breakpoint, create it, and continue executing this
608 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
610 event_child
->bp_reinsert
= stop_pc
;
611 uninsert_breakpoint (stop_pc
);
612 linux_resume_one_process (&event_child
->head
, 1, 0);
616 reinsert_breakpoint_by_bp
617 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
618 linux_resume_one_process (&event_child
->head
, 0, 0);
624 /* If we were single-stepping, we definitely want to report the
625 SIGTRAP. The single-step operation has completed, so also
626 clear the stepping flag; in general this does not matter,
627 because the SIGTRAP will be reported to the client, which
628 will give us a new action for this thread, but clear it for
629 consistency anyway. It's safe to clear the stepping flag
630 because the only consumer of get_stop_pc () after this point
631 is check_removed_breakpoint, and pending_is_breakpoint is not
632 set. It might be wiser to use a step_completed flag instead. */
633 if (event_child
->stepping
)
635 event_child
->stepping
= 0;
639 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
640 Check if it is a breakpoint, and if so mark the process information
641 accordingly. This will handle both the necessary fiddling with the
642 PC on decr_pc_after_break targets and suppressing extra threads
643 hitting a breakpoint if two hit it at once and then GDB removes it
644 after the first is reported. Arguably it would be better to report
645 multiple threads hitting breakpoints simultaneously, but the current
646 remote protocol does not allow this. */
647 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
649 event_child
->pending_is_breakpoint
= 1;
650 event_child
->pending_stop_pc
= stop_pc
;
660 /* Wait for process, returns status. */
663 linux_wait (char *status
)
666 struct thread_info
*child
= NULL
;
669 /* If we were only supposed to resume one thread, only wait for
670 that thread - if it's still alive. If it died, however - which
671 can happen if we're coming from the thread death case below -
672 then we need to make sure we restart the other threads. We could
673 pick a thread at random or restart all; restarting all is less
675 if (cont_thread
!= 0 && cont_thread
!= -1)
677 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
680 /* No stepping, no signal - unless one is pending already, of course. */
683 struct thread_resume resume_info
;
684 resume_info
.thread
= -1;
685 resume_info
.step
= resume_info
.sig
= resume_info
.leave_stopped
= 0;
686 linux_resume (&resume_info
);
692 w
= linux_wait_for_event (child
);
693 stop_all_processes ();
696 /* If we are waiting for a particular child, and it exited,
697 linux_wait_for_event will return its exit status. Similarly if
698 the last child exited. If this is not the last child, however,
699 do not report it as exited until there is a 'thread exited' response
700 available in the remote protocol. Instead, just wait for another event.
701 This should be safe, because if the thread crashed we will already
702 have reported the termination signal to GDB; that should stop any
703 in-progress stepping operations, etc.
705 Report the exit status of the last thread to exit. This matches
706 LinuxThreads' behavior. */
708 if (all_threads
.head
== all_threads
.tail
)
712 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
715 free (all_processes
.head
);
716 all_processes
.head
= all_processes
.tail
= NULL
;
717 return ((unsigned char) WEXITSTATUS (w
));
719 else if (!WIFSTOPPED (w
))
721 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
724 free (all_processes
.head
);
725 all_processes
.head
= all_processes
.tail
= NULL
;
726 return ((unsigned char) WTERMSIG (w
));
736 return ((unsigned char) WSTOPSIG (w
));
739 /* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
740 thread groups are in use, we need to use tkill. */
743 kill_lwp (unsigned long lwpid
, int signo
)
745 static int tkill_failed
;
752 int ret
= syscall (SYS_tkill
, lwpid
, signo
);
760 return kill (lwpid
, signo
);
764 send_sigstop (struct inferior_list_entry
*entry
)
766 struct process_info
*process
= (struct process_info
*) entry
;
768 if (process
->stopped
)
771 /* If we already have a pending stop signal for this process, don't
773 if (process
->stop_expected
)
775 process
->stop_expected
= 0;
780 fprintf (stderr
, "Sending sigstop to process %ld\n", process
->head
.id
);
782 kill_lwp (process
->head
.id
, SIGSTOP
);
783 process
->sigstop_sent
= 1;
787 wait_for_sigstop (struct inferior_list_entry
*entry
)
789 struct process_info
*process
= (struct process_info
*) entry
;
790 struct thread_info
*saved_inferior
, *thread
;
792 unsigned long saved_tid
;
794 if (process
->stopped
)
797 saved_inferior
= current_inferior
;
798 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
799 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
801 wstat
= linux_wait_for_event (thread
);
803 /* If we stopped with a non-SIGSTOP signal, save it for later
804 and record the pending SIGSTOP. If the process exited, just
806 if (WIFSTOPPED (wstat
)
807 && WSTOPSIG (wstat
) != SIGSTOP
)
810 fprintf (stderr
, "Stopped with non-sigstop signal\n");
811 process
->status_pending_p
= 1;
812 process
->status_pending
= wstat
;
813 process
->stop_expected
= 1;
816 if (linux_thread_alive (saved_tid
))
817 current_inferior
= saved_inferior
;
821 fprintf (stderr
, "Previously current thread died.\n");
823 /* Set a valid thread as current. */
824 set_desired_inferior (0);
829 stop_all_processes (void)
831 stopping_threads
= 1;
832 for_each_inferior (&all_processes
, send_sigstop
);
833 for_each_inferior (&all_processes
, wait_for_sigstop
);
834 stopping_threads
= 0;
837 /* Resume execution of the inferior process.
838 If STEP is nonzero, single-step it.
839 If SIGNAL is nonzero, give it that signal. */
842 linux_resume_one_process (struct inferior_list_entry
*entry
,
843 int step
, int signal
)
845 struct process_info
*process
= (struct process_info
*) entry
;
846 struct thread_info
*saved_inferior
;
848 if (process
->stopped
== 0)
851 /* If we have pending signals or status, and a new signal, enqueue the
852 signal. Also enqueue the signal if we are waiting to reinsert a
853 breakpoint; it will be picked up again below. */
855 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
856 || process
->bp_reinsert
!= 0))
858 struct pending_signals
*p_sig
;
859 p_sig
= malloc (sizeof (*p_sig
));
860 p_sig
->prev
= process
->pending_signals
;
861 p_sig
->signal
= signal
;
862 process
->pending_signals
= p_sig
;
865 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
868 saved_inferior
= current_inferior
;
869 current_inferior
= get_process_thread (process
);
872 fprintf (stderr
, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid
,
873 step
? "step" : "continue", signal
,
874 process
->stop_expected
? "expected" : "not expected");
876 /* This bit needs some thinking about. If we get a signal that
877 we must report while a single-step reinsert is still pending,
878 we often end up resuming the thread. It might be better to
879 (ew) allow a stack of pending events; then we could be sure that
880 the reinsert happened right away and not lose any signals.
882 Making this stack would also shrink the window in which breakpoints are
883 uninserted (see comment in linux_wait_for_process) but not enough for
884 complete correctness, so it won't solve that problem. It may be
885 worthwhile just to solve this one, however. */
886 if (process
->bp_reinsert
!= 0)
889 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
891 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
894 /* Postpone any pending signal. It was enqueued above. */
898 check_removed_breakpoint (process
);
900 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
902 fprintf (stderr
, " ");
903 (*the_low_target
.get_pc
) ();
906 /* If we have pending signals, consume one unless we are trying to reinsert
908 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
910 struct pending_signals
**p_sig
;
912 p_sig
= &process
->pending_signals
;
913 while ((*p_sig
)->prev
!= NULL
)
914 p_sig
= &(*p_sig
)->prev
;
916 signal
= (*p_sig
)->signal
;
921 regcache_invalidate_one ((struct inferior_list_entry
*)
922 get_process_thread (process
));
924 process
->stopped
= 0;
925 process
->stepping
= step
;
926 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
928 current_inferior
= saved_inferior
;
930 perror_with_name ("ptrace");
933 static struct thread_resume
*resume_ptr
;
935 /* This function is called once per thread. We look up the thread
936 in RESUME_PTR, and mark the thread with a pointer to the appropriate
939 This algorithm is O(threads * resume elements), but resume elements
940 is small (and will remain small at least until GDB supports thread
943 linux_set_resume_request (struct inferior_list_entry
*entry
)
945 struct process_info
*process
;
946 struct thread_info
*thread
;
949 thread
= (struct thread_info
*) entry
;
950 process
= get_thread_process (thread
);
953 while (resume_ptr
[ndx
].thread
!= -1 && resume_ptr
[ndx
].thread
!= entry
->id
)
956 process
->resume
= &resume_ptr
[ndx
];
959 /* This function is called once per thread. We check the thread's resume
960 request, which will tell us whether to resume, step, or leave the thread
961 stopped; and what signal, if any, it should be sent. For threads which
962 we aren't explicitly told otherwise, we preserve the stepping flag; this
963 is used for stepping over gdbserver-placed breakpoints. */
966 linux_continue_one_thread (struct inferior_list_entry
*entry
)
968 struct process_info
*process
;
969 struct thread_info
*thread
;
972 thread
= (struct thread_info
*) entry
;
973 process
= get_thread_process (thread
);
975 if (process
->resume
->leave_stopped
)
978 if (process
->resume
->thread
== -1)
979 step
= process
->stepping
|| process
->resume
->step
;
981 step
= process
->resume
->step
;
983 linux_resume_one_process (&process
->head
, step
, process
->resume
->sig
);
985 process
->resume
= NULL
;
988 /* This function is called once per thread. We check the thread's resume
989 request, which will tell us whether to resume, step, or leave the thread
990 stopped; and what signal, if any, it should be sent. We queue any needed
991 signals, since we won't actually resume. We already have a pending event
992 to report, so we don't need to preserve any step requests; they should
993 be re-issued if necessary. */
996 linux_queue_one_thread (struct inferior_list_entry
*entry
)
998 struct process_info
*process
;
999 struct thread_info
*thread
;
1001 thread
= (struct thread_info
*) entry
;
1002 process
= get_thread_process (thread
);
1004 if (process
->resume
->leave_stopped
)
1007 /* If we have a new signal, enqueue the signal. */
1008 if (process
->resume
->sig
!= 0)
1010 struct pending_signals
*p_sig
;
1011 p_sig
= malloc (sizeof (*p_sig
));
1012 p_sig
->prev
= process
->pending_signals
;
1013 p_sig
->signal
= process
->resume
->sig
;
1014 process
->pending_signals
= p_sig
;
1017 process
->resume
= NULL
;
1020 /* Set DUMMY if this process has an interesting status pending. */
1022 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
1024 struct process_info
*process
= (struct process_info
*) entry
;
1026 /* Processes which will not be resumed are not interesting, because
1027 we might not wait for them next time through linux_wait. */
1028 if (process
->resume
->leave_stopped
)
1031 /* If this thread has a removed breakpoint, we won't have any
1032 events to report later, so check now. check_removed_breakpoint
1033 may clear status_pending_p. We avoid calling check_removed_breakpoint
1034 for any thread that we are not otherwise going to resume - this
1035 lets us preserve stopped status when two threads hit a breakpoint.
1036 GDB removes the breakpoint to single-step a particular thread
1037 past it, then re-inserts it and resumes all threads. We want
1038 to report the second thread without resuming it in the interim. */
1039 if (process
->status_pending_p
)
1040 check_removed_breakpoint (process
);
1042 if (process
->status_pending_p
)
1043 * (int *) flag_p
= 1;
1049 linux_resume (struct thread_resume
*resume_info
)
1053 /* Yes, the use of a global here is rather ugly. */
1054 resume_ptr
= resume_info
;
1056 for_each_inferior (&all_threads
, linux_set_resume_request
);
1058 /* If there is a thread which would otherwise be resumed, which
1059 has a pending status, then don't resume any threads - we can just
1060 report the pending status. Make sure to queue any signals
1061 that would otherwise be sent. */
1063 find_inferior (&all_processes
, resume_status_pending_p
, &pending_flag
);
1068 fprintf (stderr
, "Not resuming, pending status\n");
1070 fprintf (stderr
, "Resuming, no pending status\n");
1074 for_each_inferior (&all_threads
, linux_queue_one_thread
);
1079 for_each_inferior (&all_threads
, linux_continue_one_thread
);
1083 #ifdef HAVE_LINUX_USRREGS
1086 register_addr (int regnum
)
1090 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
1091 error ("Invalid register number %d.", regnum
);
1093 addr
= the_low_target
.regmap
[regnum
];
1098 /* Fetch one register. */
1100 fetch_register (int regno
)
1106 if (regno
>= the_low_target
.num_regs
)
1108 if ((*the_low_target
.cannot_fetch_register
) (regno
))
1111 regaddr
= register_addr (regno
);
1114 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1115 & - sizeof (PTRACE_XFER_TYPE
);
1116 buf
= alloca (size
);
1117 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1120 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
1121 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
1122 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1125 /* Warning, not error, in case we are attached; sometimes the
1126 kernel doesn't let us at the registers. */
1127 char *err
= strerror (errno
);
1128 char *msg
= alloca (strlen (err
) + 128);
1129 sprintf (msg
, "reading register %d: %s", regno
, err
);
1134 if (the_low_target
.left_pad_xfer
1135 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1136 supply_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1137 - register_size (regno
)));
1139 supply_register (regno
, buf
);
1144 /* Fetch all registers, or just one, from the child process. */
1146 usr_fetch_inferior_registers (int regno
)
1148 if (regno
== -1 || regno
== 0)
1149 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1150 fetch_register (regno
);
1152 fetch_register (regno
);
1155 /* Store our register values back into the inferior.
1156 If REGNO is -1, do this for all registers.
1157 Otherwise, REGNO specifies which register (so we can save time). */
1159 usr_store_inferior_registers (int regno
)
1167 if (regno
>= the_low_target
.num_regs
)
1170 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
1173 regaddr
= register_addr (regno
);
1177 size
= (register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1178 & - sizeof (PTRACE_XFER_TYPE
);
1179 buf
= alloca (size
);
1180 memset (buf
, 0, size
);
1181 if (the_low_target
.left_pad_xfer
1182 && register_size (regno
) < sizeof (PTRACE_XFER_TYPE
))
1183 collect_register (regno
, (buf
+ sizeof (PTRACE_XFER_TYPE
)
1184 - register_size (regno
)));
1186 collect_register (regno
, buf
);
1187 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
1190 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
1191 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
1194 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
1196 char *err
= strerror (errno
);
1197 char *msg
= alloca (strlen (err
) + 128);
1198 sprintf (msg
, "writing register %d: %s",
1204 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1208 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1209 usr_store_inferior_registers (regno
);
1211 #endif /* HAVE_LINUX_USRREGS */
1215 #ifdef HAVE_LINUX_REGSETS
1218 regsets_fetch_inferior_registers ()
1220 struct regset_info
*regset
;
1221 int saw_general_regs
= 0;
1223 regset
= target_regsets
;
1225 while (regset
->size
>= 0)
1230 if (regset
->size
== 0)
1236 buf
= malloc (regset
->size
);
1237 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1242 /* If we get EIO on the first regset, do not try regsets again.
1243 If we get EIO on a later regset, disable that regset. */
1244 if (regset
== target_regsets
)
1258 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
1263 else if (regset
->type
== GENERAL_REGS
)
1264 saw_general_regs
= 1;
1265 regset
->store_function (buf
);
1268 if (saw_general_regs
)
1275 regsets_store_inferior_registers ()
1277 struct regset_info
*regset
;
1278 int saw_general_regs
= 0;
1280 regset
= target_regsets
;
1282 while (regset
->size
>= 0)
1287 if (regset
->size
== 0)
1293 buf
= malloc (regset
->size
);
1295 /* First fill the buffer with the current register set contents,
1296 in case there are any items in the kernel's regset that are
1297 not in gdbserver's regcache. */
1298 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1302 /* Then overlay our cached registers on that. */
1303 regset
->fill_function (buf
);
1305 /* Only now do we write the register set. */
1306 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1313 /* If we get EIO on the first regset, do not try regsets again.
1314 If we get EIO on a later regset, disable that regset. */
1315 if (regset
== target_regsets
)
1328 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1331 else if (regset
->type
== GENERAL_REGS
)
1332 saw_general_regs
= 1;
1336 if (saw_general_regs
)
1343 #endif /* HAVE_LINUX_REGSETS */
1347 linux_fetch_registers (int regno
)
1349 #ifdef HAVE_LINUX_REGSETS
1352 if (regsets_fetch_inferior_registers () == 0)
1356 #ifdef HAVE_LINUX_USRREGS
1357 usr_fetch_inferior_registers (regno
);
1362 linux_store_registers (int regno
)
1364 #ifdef HAVE_LINUX_REGSETS
1367 if (regsets_store_inferior_registers () == 0)
1371 #ifdef HAVE_LINUX_USRREGS
1372 usr_store_inferior_registers (regno
);
1377 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1378 to debugger memory starting at MYADDR. */
1381 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
1384 /* Round starting address down to longword boundary. */
1385 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1386 /* Round ending address up; get number of longwords that makes. */
1388 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1389 / sizeof (PTRACE_XFER_TYPE
);
1390 /* Allocate buffer of that many longwords. */
1391 register PTRACE_XFER_TYPE
*buffer
1392 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1394 /* Read all the longwords */
1395 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1398 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1403 /* Copy appropriate bytes out of the buffer. */
1404 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1409 /* Copy LEN bytes of data from debugger memory at MYADDR
1410 to inferior's memory at MEMADDR.
1411 On failure (cannot write the inferior)
1412 returns the value of errno. */
1415 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
1418 /* Round starting address down to longword boundary. */
1419 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1420 /* Round ending address up; get number of longwords that makes. */
1422 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1423 /* Allocate buffer of that many longwords. */
1424 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1429 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1432 /* Fill start and end extra bytes of buffer with existing memory data. */
1434 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1435 (PTRACE_ARG3_TYPE
) addr
, 0);
1440 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1441 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1442 * sizeof (PTRACE_XFER_TYPE
)),
1446 /* Copy data to be written over corresponding part of buffer */
1448 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1450 /* Write the entire buffer. */
1452 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1455 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1464 linux_look_up_symbols (void)
1466 #ifdef USE_THREAD_DB
1470 using_threads
= thread_db_init ();
1475 linux_send_signal (int signum
)
1477 extern unsigned long signal_pid
;
1479 if (cont_thread
!= 0 && cont_thread
!= -1)
1481 struct process_info
*process
;
1483 process
= get_thread_process (current_inferior
);
1484 kill_lwp (process
->lwpid
, signum
);
1487 kill_lwp (signal_pid
, signum
);
1490 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1491 to debugger memory starting at MYADDR. */
1494 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
1496 char filename
[PATH_MAX
];
1499 snprintf (filename
, sizeof filename
, "/proc/%ld/auxv", inferior_pid
);
1501 fd
= open (filename
, O_RDONLY
);
1505 if (offset
!= (CORE_ADDR
) 0
1506 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
1509 n
= read (fd
, myaddr
, len
);
1516 /* These watchpoint related wrapper functions simply pass on the function call
1517 if the target has registered a corresponding function. */
1520 linux_insert_watchpoint (char type
, CORE_ADDR addr
, int len
)
1522 if (the_low_target
.insert_watchpoint
!= NULL
)
1523 return the_low_target
.insert_watchpoint (type
, addr
, len
);
1525 /* Unsupported (see target.h). */
1530 linux_remove_watchpoint (char type
, CORE_ADDR addr
, int len
)
1532 if (the_low_target
.remove_watchpoint
!= NULL
)
1533 return the_low_target
.remove_watchpoint (type
, addr
, len
);
1535 /* Unsupported (see target.h). */
1540 linux_stopped_by_watchpoint (void)
1542 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
1543 return the_low_target
.stopped_by_watchpoint ();
1549 linux_stopped_data_address (void)
1551 if (the_low_target
.stopped_data_address
!= NULL
)
1552 return the_low_target
.stopped_data_address ();
1557 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
1558 #if defined(__mcoldfire__)
1559 /* These should really be defined in the kernel's ptrace.h header. */
1560 #define PT_TEXT_ADDR 49*4
1561 #define PT_DATA_ADDR 50*4
1562 #define PT_TEXT_END_ADDR 51*4
1565 /* Under uClinux, programs are loaded at non-zero offsets, which we need
1566 to tell gdb about. */
1569 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
1571 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1572 unsigned long text
, text_end
, data
;
1573 int pid
= get_thread_process (current_inferior
)->head
.id
;
1577 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
1578 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
1579 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
1583 /* Both text and data offsets produced at compile-time (and so
1584 used by gdb) are relative to the beginning of the program,
1585 with the data segment immediately following the text segment.
1586 However, the actual runtime layout in memory may put the data
1587 somewhere else, so when we send gdb a data base-address, we
1588 use the real data base address and subtract the compile-time
1589 data base-address from it (which is just the length of the
1590 text segment). BSS immediately follows data in both
1593 *data_p
= data
- (text_end
- text
);
1602 static struct target_ops linux_target_ops
= {
1603 linux_create_inferior
,
1610 linux_fetch_registers
,
1611 linux_store_registers
,
1614 linux_look_up_symbols
,
1617 linux_insert_watchpoint
,
1618 linux_remove_watchpoint
,
1619 linux_stopped_by_watchpoint
,
1620 linux_stopped_data_address
,
1621 #if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
1627 linux_init_signals ()
1629 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1630 to find what the cancel signal actually is. */
1631 signal (__SIGRTMIN
+1, SIG_IGN
);
1635 initialize_low (void)
1638 set_target_ops (&linux_target_ops
);
1639 set_breakpoint_data (the_low_target
.breakpoint
,
1640 the_low_target
.breakpoint_len
);
1642 linux_init_signals ();