1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002
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 /* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
39 however. This requires changing the ID in place when we go from !using_threads
40 to using_threads, immediately.
42 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
43 the same as the LWP ID. */
45 struct inferior_list all_processes
;
47 /* FIXME this is a bit of a hack, and could be removed. */
50 /* FIXME make into a target method? */
53 static void linux_resume_one_process (struct inferior_list_entry
*entry
,
54 int step
, int signal
);
55 static void linux_resume (int step
, int signal
);
56 static void stop_all_processes (void);
57 static int linux_wait_for_event (struct thread_info
*child
);
59 struct pending_signals
62 struct pending_signals
*prev
;
65 #define PTRACE_ARG3_TYPE long
66 #define PTRACE_XFER_TYPE long
68 #ifdef HAVE_LINUX_REGSETS
69 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 (int 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 (int pid
, int tid
)
173 struct process_info
*new_process
;
175 if (ptrace (PTRACE_ATTACH
, pid
, 0, 0) != 0)
177 fprintf (stderr
, "Cannot attach to process %d: %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 (int 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
);
229 ptrace (PTRACE_KILL
, pid_of (process
), 0, 0);
231 /* Make sure it died. The loop is most likely unnecessary. */
232 wstat
= linux_wait_for_event (thread
);
233 } while (WIFSTOPPED (wstat
));
239 for_each_inferior (&all_threads
, linux_kill_one_process
);
243 linux_detach_one_process (struct inferior_list_entry
*entry
)
245 struct thread_info
*thread
= (struct thread_info
*) entry
;
246 struct process_info
*process
= get_thread_process (thread
);
248 ptrace (PTRACE_DETACH
, pid_of (process
), 0, 0);
254 for_each_inferior (&all_threads
, linux_detach_one_process
);
257 /* Return nonzero if the given thread is still alive. */
259 linux_thread_alive (int tid
)
261 if (find_inferior_id (&all_threads
, tid
) != NULL
)
267 /* Return nonzero if this process stopped at a breakpoint which
268 no longer appears to be inserted. Also adjust the PC
269 appropriately to resume where the breakpoint used to be. */
271 check_removed_breakpoint (struct process_info
*event_child
)
274 struct thread_info
*saved_inferior
;
276 if (event_child
->pending_is_breakpoint
== 0)
280 fprintf (stderr
, "Checking for breakpoint.\n");
282 saved_inferior
= current_inferior
;
283 current_inferior
= get_process_thread (event_child
);
285 stop_pc
= get_stop_pc ();
287 /* If the PC has changed since we stopped, then we shouldn't do
288 anything. This happens if, for instance, GDB handled the
289 decr_pc_after_break subtraction itself. */
290 if (stop_pc
!= event_child
->pending_stop_pc
)
293 fprintf (stderr
, "Ignoring, PC was changed.\n");
295 event_child
->pending_is_breakpoint
= 0;
296 current_inferior
= saved_inferior
;
300 /* If the breakpoint is still there, we will report hitting it. */
301 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
304 fprintf (stderr
, "Ignoring, breakpoint is still present.\n");
305 current_inferior
= saved_inferior
;
310 fprintf (stderr
, "Removed breakpoint.\n");
312 /* For decr_pc_after_break targets, here is where we perform the
313 decrement. We go immediately from this function to resuming,
314 and can not safely call get_stop_pc () again. */
315 if (the_low_target
.set_pc
!= NULL
)
316 (*the_low_target
.set_pc
) (stop_pc
);
318 /* We consumed the pending SIGTRAP. */
319 event_child
->status_pending_p
= 0;
320 event_child
->status_pending
= 0;
322 current_inferior
= saved_inferior
;
326 /* Return 1 if this process has an interesting status pending. This function
327 may silently resume an inferior process. */
329 status_pending_p (struct inferior_list_entry
*entry
, void *dummy
)
331 struct process_info
*process
= (struct process_info
*) entry
;
333 if (process
->status_pending_p
)
334 if (check_removed_breakpoint (process
))
336 /* This thread was stopped at a breakpoint, and the breakpoint
337 is now gone. We were told to continue (or step...) all threads,
338 so GDB isn't trying to single-step past this breakpoint.
339 So instead of reporting the old SIGTRAP, pretend we got to
340 the breakpoint just after it was removed instead of just
341 before; resume the process. */
342 linux_resume_one_process (&process
->head
, 0, 0);
346 return process
->status_pending_p
;
350 linux_wait_for_process (struct process_info
**childp
, int *wstatp
)
353 int to_wait_for
= -1;
356 to_wait_for
= (*childp
)->lwpid
;
360 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
);
365 perror_with_name ("waitpid");
370 ret
= waitpid (to_wait_for
, wstatp
, WNOHANG
| __WCLONE
);
375 perror_with_name ("waitpid (WCLONE)");
384 && (!WIFSTOPPED (*wstatp
)
385 || (WSTOPSIG (*wstatp
) != 32
386 && WSTOPSIG (*wstatp
) != 33)))
387 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
389 if (to_wait_for
== -1)
390 *childp
= (struct process_info
*) find_inferior_id (&all_processes
, ret
);
392 (*childp
)->stopped
= 1;
393 (*childp
)->pending_is_breakpoint
= 0;
396 && WIFSTOPPED (*wstatp
))
398 current_inferior
= (struct thread_info
*)
399 find_inferior_id (&all_threads
, (*childp
)->tid
);
400 /* For testing only; i386_stop_pc prints out a diagnostic. */
401 if (the_low_target
.get_pc
!= NULL
)
407 linux_wait_for_event (struct thread_info
*child
)
410 struct process_info
*event_child
;
413 /* Check for a process with a pending status. */
414 /* It is possible that the user changed the pending task's registers since
415 it stopped. We correctly handle the change of PC if we hit a breakpoint
416 (in check_removed_breakpoint); signals should be reported anyway. */
419 event_child
= (struct process_info
*)
420 find_inferior (&all_processes
, status_pending_p
, NULL
);
421 if (debug_threads
&& event_child
)
422 fprintf (stderr
, "Got a pending child %d\n", event_child
->lwpid
);
426 event_child
= get_thread_process (child
);
427 if (event_child
->status_pending_p
428 && check_removed_breakpoint (event_child
))
432 if (event_child
!= NULL
)
434 if (event_child
->status_pending_p
)
437 fprintf (stderr
, "Got an event from pending child %d (%04x)\n",
438 event_child
->lwpid
, event_child
->status_pending
);
439 wstat
= event_child
->status_pending
;
440 event_child
->status_pending_p
= 0;
441 event_child
->status_pending
= 0;
442 current_inferior
= get_process_thread (event_child
);
447 /* We only enter this loop if no process has a pending wait status. Thus
448 any action taken in response to a wait status inside this loop is
449 responding as soon as we detect the status, not after any pending
456 event_child
= get_thread_process (child
);
458 linux_wait_for_process (&event_child
, &wstat
);
460 if (event_child
== NULL
)
461 error ("event from unknown child");
463 current_inferior
= (struct thread_info
*)
464 find_inferior_id (&all_threads
, event_child
->tid
);
468 /* Check for thread exit. */
469 if (! WIFSTOPPED (wstat
))
472 fprintf (stderr
, "Thread %d (LWP %d) exiting\n",
473 event_child
->tid
, event_child
->head
.id
);
475 /* If the last thread is exiting, just return. */
476 if (all_threads
.head
== all_threads
.tail
)
479 dead_thread_notify (event_child
->tid
);
481 remove_inferior (&all_processes
, &event_child
->head
);
483 remove_thread (current_inferior
);
484 current_inferior
= (struct thread_info
*) all_threads
.head
;
486 /* If we were waiting for this particular child to do something...
487 well, it did something. */
491 /* Wait for a more interesting event. */
495 if (WIFSTOPPED (wstat
)
496 && WSTOPSIG (wstat
) == SIGSTOP
497 && event_child
->stop_expected
)
500 fprintf (stderr
, "Expected stop.\n");
501 event_child
->stop_expected
= 0;
502 linux_resume_one_process (&event_child
->head
,
503 event_child
->stepping
, 0);
507 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
509 if (WIFSTOPPED (wstat
)
510 && (WSTOPSIG (wstat
) == __SIGRTMIN
511 || WSTOPSIG (wstat
) == __SIGRTMIN
+ 1))
514 fprintf (stderr
, "Ignored signal %d for %d (LWP %d).\n",
515 WSTOPSIG (wstat
), event_child
->tid
,
516 event_child
->head
.id
);
517 linux_resume_one_process (&event_child
->head
,
518 event_child
->stepping
,
524 /* If this event was not handled above, and is not a SIGTRAP, report
526 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGTRAP
)
529 /* If this target does not support breakpoints, we simply report the
530 SIGTRAP; it's of no concern to us. */
531 if (the_low_target
.get_pc
== NULL
)
534 stop_pc
= get_stop_pc ();
536 /* bp_reinsert will only be set if we were single-stepping.
537 Notice that we will resume the process after hitting
538 a gdbserver breakpoint; single-stepping to/over one
539 is not supported (yet). */
540 if (event_child
->bp_reinsert
!= 0)
543 fprintf (stderr
, "Reinserted breakpoint.\n");
544 reinsert_breakpoint (event_child
->bp_reinsert
);
545 event_child
->bp_reinsert
= 0;
547 /* Clear the single-stepping flag and SIGTRAP as we resume. */
548 linux_resume_one_process (&event_child
->head
, 0, 0);
553 fprintf (stderr
, "Hit a (non-reinsert) breakpoint.\n");
555 if (check_breakpoints (stop_pc
) != 0)
557 /* We hit one of our own breakpoints. We mark it as a pending
558 breakpoint, so that check_removed_breakpoint () will do the PC
559 adjustment for us at the appropriate time. */
560 event_child
->pending_is_breakpoint
= 1;
561 event_child
->pending_stop_pc
= stop_pc
;
563 /* Now we need to put the breakpoint back. We continue in the event
564 loop instead of simply replacing the breakpoint right away,
565 in order to not lose signals sent to the thread that hit the
566 breakpoint. Unfortunately this increases the window where another
567 thread could sneak past the removed breakpoint. For the current
568 use of server-side breakpoints (thread creation) this is
569 acceptable; but it needs to be considered before this breakpoint
570 mechanism can be used in more general ways. For some breakpoints
571 it may be necessary to stop all other threads, but that should
572 be avoided where possible.
574 If breakpoint_reinsert_addr is NULL, that means that we can
575 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
576 mark it for reinsertion, and single-step.
578 Otherwise, call the target function to figure out where we need
579 our temporary breakpoint, create it, and continue executing this
581 if (the_low_target
.breakpoint_reinsert_addr
== NULL
)
583 event_child
->bp_reinsert
= stop_pc
;
584 uninsert_breakpoint (stop_pc
);
585 linux_resume_one_process (&event_child
->head
, 1, 0);
589 reinsert_breakpoint_by_bp
590 (stop_pc
, (*the_low_target
.breakpoint_reinsert_addr
) ());
591 linux_resume_one_process (&event_child
->head
, 0, 0);
597 /* If we were single-stepping, we definitely want to report the
598 SIGTRAP. The single-step operation has completed, so also
599 clear the stepping flag; in general this does not matter,
600 because the SIGTRAP will be reported to the client, which
601 will give us a new action for this thread, but clear it for
602 consistency anyway. It's safe to clear the stepping flag
603 because the only consumer of get_stop_pc () after this point
604 is check_removed_breakpoint, and pending_is_breakpoint is not
605 set. It might be wiser to use a step_completed flag instead. */
606 if (event_child
->stepping
)
608 event_child
->stepping
= 0;
612 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
613 Check if it is a breakpoint, and if so mark the process information
614 accordingly. This will handle both the necessary fiddling with the
615 PC on decr_pc_after_break targets and suppressing extra threads
616 hitting a breakpoint if two hit it at once and then GDB removes it
617 after the first is reported. Arguably it would be better to report
618 multiple threads hitting breakpoints simultaneously, but the current
619 remote protocol does not allow this. */
620 if ((*the_low_target
.breakpoint_at
) (stop_pc
))
622 event_child
->pending_is_breakpoint
= 1;
623 event_child
->pending_stop_pc
= stop_pc
;
633 /* Wait for process, returns status. */
636 linux_wait (char *status
)
639 struct thread_info
*child
= NULL
;
642 /* If we were only supposed to resume one thread, only wait for
643 that thread - if it's still alive. If it died, however - which
644 can happen if we're coming from the thread death case below -
645 then we need to make sure we restart the other threads. We could
646 pick a thread at random or restart all; restarting all is less
650 child
= (struct thread_info
*) find_inferior_id (&all_threads
,
653 /* No stepping, no signal - unless one is pending already, of course. */
659 w
= linux_wait_for_event (child
);
660 stop_all_processes ();
663 /* If we are waiting for a particular child, and it exited,
664 linux_wait_for_event will return its exit status. Similarly if
665 the last child exited. If this is not the last child, however,
666 do not report it as exited until there is a 'thread exited' response
667 available in the remote protocol. Instead, just wait for another event.
668 This should be safe, because if the thread crashed we will already
669 have reported the termination signal to GDB; that should stop any
670 in-progress stepping operations, etc.
672 Report the exit status of the last thread to exit. This matches
673 LinuxThreads' behavior. */
675 if (all_threads
.head
== all_threads
.tail
)
679 fprintf (stderr
, "\nChild exited with retcode = %x \n", WEXITSTATUS (w
));
682 return ((unsigned char) WEXITSTATUS (w
));
684 else if (!WIFSTOPPED (w
))
686 fprintf (stderr
, "\nChild terminated with signal = %x \n", WTERMSIG (w
));
689 return ((unsigned char) WTERMSIG (w
));
699 return ((unsigned char) WSTOPSIG (w
));
703 send_sigstop (struct inferior_list_entry
*entry
)
705 struct process_info
*process
= (struct process_info
*) entry
;
707 if (process
->stopped
)
710 /* If we already have a pending stop signal for this process, don't
712 if (process
->stop_expected
)
714 process
->stop_expected
= 0;
719 fprintf (stderr
, "Sending sigstop to process %d\n", process
->head
.id
);
721 kill (process
->head
.id
, SIGSTOP
);
722 process
->sigstop_sent
= 1;
726 wait_for_sigstop (struct inferior_list_entry
*entry
)
728 struct process_info
*process
= (struct process_info
*) entry
;
729 struct thread_info
*saved_inferior
, *thread
;
730 int wstat
, saved_tid
;
732 if (process
->stopped
)
735 saved_inferior
= current_inferior
;
736 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
737 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
739 wstat
= linux_wait_for_event (thread
);
741 /* If we stopped with a non-SIGSTOP signal, save it for later
742 and record the pending SIGSTOP. If the process exited, just
744 if (WIFSTOPPED (wstat
)
745 && WSTOPSIG (wstat
) != SIGSTOP
)
748 fprintf (stderr
, "Stopped with non-sigstop signal\n");
749 process
->status_pending_p
= 1;
750 process
->status_pending
= wstat
;
751 process
->stop_expected
= 1;
754 if (linux_thread_alive (saved_tid
))
755 current_inferior
= saved_inferior
;
759 fprintf (stderr
, "Previously current thread died.\n");
761 /* Set a valid thread as current. */
762 set_desired_inferior (0);
767 stop_all_processes (void)
769 stopping_threads
= 1;
770 for_each_inferior (&all_processes
, send_sigstop
);
771 for_each_inferior (&all_processes
, wait_for_sigstop
);
772 stopping_threads
= 0;
775 /* Resume execution of the inferior process.
776 If STEP is nonzero, single-step it.
777 If SIGNAL is nonzero, give it that signal. */
780 linux_resume_one_process (struct inferior_list_entry
*entry
,
781 int step
, int signal
)
783 struct process_info
*process
= (struct process_info
*) entry
;
784 struct thread_info
*saved_inferior
;
786 if (process
->stopped
== 0)
789 /* If we have pending signals or status, and a new signal, enqueue the
790 signal. Also enqueue the signal if we are waiting to reinsert a
791 breakpoint; it will be picked up again below. */
793 && (process
->status_pending_p
|| process
->pending_signals
!= NULL
794 || process
->bp_reinsert
!= 0))
796 struct pending_signals
*p_sig
;
797 p_sig
= malloc (sizeof (*p_sig
));
798 p_sig
->prev
= process
->pending_signals
;
799 p_sig
->signal
= signal
;
800 process
->pending_signals
= p_sig
;
803 if (process
->status_pending_p
&& !check_removed_breakpoint (process
))
806 saved_inferior
= current_inferior
;
807 current_inferior
= get_process_thread (process
);
810 fprintf (stderr
, "Resuming process %d (%s, signal %d, stop %s)\n", inferior_pid
,
811 step
? "step" : "continue", signal
,
812 process
->stop_expected
? "expected" : "not expected");
814 /* This bit needs some thinking about. If we get a signal that
815 we must report while a single-step reinsert is still pending,
816 we often end up resuming the thread. It might be better to
817 (ew) allow a stack of pending events; then we could be sure that
818 the reinsert happened right away and not lose any signals.
820 Making this stack would also shrink the window in which breakpoints are
821 uninserted (see comment in linux_wait_for_process) but not enough for
822 complete correctness, so it won't solve that problem. It may be
823 worthwhile just to solve this one, however. */
824 if (process
->bp_reinsert
!= 0)
827 fprintf (stderr
, " pending reinsert at %08lx", (long)process
->bp_reinsert
);
829 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
832 /* Postpone any pending signal. It was enqueued above. */
836 check_removed_breakpoint (process
);
838 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
840 fprintf (stderr
, " ");
841 (long) (*the_low_target
.get_pc
) ();
844 /* If we have pending signals, consume one unless we are trying to reinsert
846 if (process
->pending_signals
!= NULL
&& process
->bp_reinsert
== 0)
848 struct pending_signals
**p_sig
;
850 p_sig
= &process
->pending_signals
;
851 while ((*p_sig
)->prev
!= NULL
)
852 p_sig
= &(*p_sig
)->prev
;
854 signal
= (*p_sig
)->signal
;
859 regcache_invalidate_one ((struct inferior_list_entry
*)
860 get_process_thread (process
));
862 process
->stopped
= 0;
863 process
->stepping
= step
;
864 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, process
->lwpid
, 0, signal
);
866 current_inferior
= saved_inferior
;
868 perror_with_name ("ptrace");
871 /* This function is called once per process other than the first
872 one. The first process we are told the signal to continue
873 with, and whether to step or continue; for all others, any
874 existing signals will be marked in status_pending_p to be
875 reported momentarily, and we preserve the stepping flag. */
877 linux_continue_one_process (struct inferior_list_entry
*entry
)
879 struct process_info
*process
;
881 process
= (struct process_info
*) entry
;
882 linux_resume_one_process (entry
, process
->stepping
, 0);
886 linux_resume (int step
, int signal
)
888 struct process_info
*process
;
890 process
= get_thread_process (current_inferior
);
892 /* If the current process has a status pending, this signal will
893 be enqueued and sent later. */
894 linux_resume_one_process (&process
->head
, step
, signal
);
896 if (cont_thread
== 0 || cont_thread
== -1)
897 for_each_inferior (&all_processes
, linux_continue_one_process
);
900 #ifdef HAVE_LINUX_USRREGS
903 register_addr (int regnum
)
907 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
908 error ("Invalid register number %d.", regnum
);
910 addr
= the_low_target
.regmap
[regnum
];
915 /* Fetch one register. */
917 fetch_register (int regno
)
923 if (regno
>= the_low_target
.num_regs
)
925 if ((*the_low_target
.cannot_fetch_register
) (regno
))
928 regaddr
= register_addr (regno
);
931 buf
= alloca (register_size (regno
));
932 for (i
= 0; i
< register_size (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
935 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
936 ptrace (PTRACE_PEEKUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
, 0);
937 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
940 /* Warning, not error, in case we are attached; sometimes the
941 kernel doesn't let us at the registers. */
942 char *err
= strerror (errno
);
943 char *msg
= alloca (strlen (err
) + 128);
944 sprintf (msg
, "reading register %d: %s", regno
, err
);
949 supply_register (regno
, buf
);
954 /* Fetch all registers, or just one, from the child process. */
956 usr_fetch_inferior_registers (int regno
)
958 if (regno
== -1 || regno
== 0)
959 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
960 fetch_register (regno
);
962 fetch_register (regno
);
965 /* Store our register values back into the inferior.
966 If REGNO is -1, do this for all registers.
967 Otherwise, REGNO specifies which register (so we can save time). */
969 usr_store_inferior_registers (int regno
)
977 if (regno
>= the_low_target
.num_regs
)
980 if ((*the_low_target
.cannot_store_register
) (regno
) == 1)
983 regaddr
= register_addr (regno
);
987 buf
= alloca (register_size (regno
));
988 collect_register (regno
, buf
);
989 for (i
= 0; i
< register_size (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
992 ptrace (PTRACE_POKEUSER
, inferior_pid
, (PTRACE_ARG3_TYPE
) regaddr
,
993 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
996 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
998 char *err
= strerror (errno
);
999 char *msg
= alloca (strlen (err
) + 128);
1000 sprintf (msg
, "writing register %d: %s",
1006 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
1010 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
1011 usr_store_inferior_registers (regno
);
1013 #endif /* HAVE_LINUX_USRREGS */
1017 #ifdef HAVE_LINUX_REGSETS
1020 regsets_fetch_inferior_registers ()
1022 struct regset_info
*regset
;
1024 regset
= target_regsets
;
1026 while (regset
->size
>= 0)
1031 if (regset
->size
== 0)
1037 buf
= malloc (regset
->size
);
1038 res
= ptrace (regset
->get_request
, inferior_pid
, 0, buf
);
1043 /* If we get EIO on the first regset, do not try regsets again.
1044 If we get EIO on a later regset, disable that regset. */
1045 if (regset
== target_regsets
)
1059 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
1064 regset
->store_function (buf
);
1071 regsets_store_inferior_registers ()
1073 struct regset_info
*regset
;
1075 regset
= target_regsets
;
1077 while (regset
->size
>= 0)
1082 if (regset
->size
== 0)
1088 buf
= malloc (regset
->size
);
1089 regset
->fill_function (buf
);
1090 res
= ptrace (regset
->set_request
, inferior_pid
, 0, buf
);
1095 /* If we get EIO on the first regset, do not try regsets again.
1096 If we get EIO on a later regset, disable that regset. */
1097 if (regset
== target_regsets
)
1110 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1119 #endif /* HAVE_LINUX_REGSETS */
1123 linux_fetch_registers (int regno
)
1125 #ifdef HAVE_LINUX_REGSETS
1128 if (regsets_fetch_inferior_registers () == 0)
1132 #ifdef HAVE_LINUX_USRREGS
1133 usr_fetch_inferior_registers (regno
);
1138 linux_store_registers (int regno
)
1140 #ifdef HAVE_LINUX_REGSETS
1143 if (regsets_store_inferior_registers () == 0)
1147 #ifdef HAVE_LINUX_USRREGS
1148 usr_store_inferior_registers (regno
);
1153 /* Copy LEN bytes from inferior's memory starting at MEMADDR
1154 to debugger memory starting at MYADDR. */
1157 linux_read_memory (CORE_ADDR memaddr
, char *myaddr
, int len
)
1160 /* Round starting address down to longword boundary. */
1161 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1162 /* Round ending address up; get number of longwords that makes. */
1164 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
1165 / sizeof (PTRACE_XFER_TYPE
);
1166 /* Allocate buffer of that many longwords. */
1167 register PTRACE_XFER_TYPE
*buffer
1168 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1170 /* Read all the longwords */
1171 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1173 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, 0);
1176 /* Copy appropriate bytes out of the buffer. */
1177 memcpy (myaddr
, (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), len
);
1180 /* Copy LEN bytes of data from debugger memory at MYADDR
1181 to inferior's memory at MEMADDR.
1182 On failure (cannot write the inferior)
1183 returns the value of errno. */
1186 linux_write_memory (CORE_ADDR memaddr
, const char *myaddr
, int len
)
1189 /* Round starting address down to longword boundary. */
1190 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
1191 /* Round ending address up; get number of longwords that makes. */
1193 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1) / sizeof (PTRACE_XFER_TYPE
);
1194 /* Allocate buffer of that many longwords. */
1195 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
1200 fprintf (stderr
, "Writing %02x to %08lx\n", (unsigned)myaddr
[0], (long)memaddr
);
1203 /* Fill start and end extra bytes of buffer with existing memory data. */
1205 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1206 (PTRACE_ARG3_TYPE
) addr
, 0);
1211 = ptrace (PTRACE_PEEKTEXT
, inferior_pid
,
1212 (PTRACE_ARG3_TYPE
) (addr
+ (count
- 1)
1213 * sizeof (PTRACE_XFER_TYPE
)),
1217 /* Copy data to be written over corresponding part of buffer */
1219 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)), myaddr
, len
);
1221 /* Write the entire buffer. */
1223 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
1226 ptrace (PTRACE_POKETEXT
, inferior_pid
, (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
1235 linux_look_up_symbols (void)
1237 #ifdef USE_THREAD_DB
1241 using_threads
= thread_db_init ();
1246 linux_send_signal (int signum
)
1248 extern int signal_pid
;
1250 if (cont_thread
> 0)
1252 struct process_info
*process
;
1254 process
= get_thread_process (current_inferior
);
1255 kill (process
->lwpid
, signum
);
1258 kill (signal_pid
, signum
);
1262 static struct target_ops linux_target_ops
= {
1263 linux_create_inferior
,
1270 linux_fetch_registers
,
1271 linux_store_registers
,
1274 linux_look_up_symbols
,
1279 linux_init_signals ()
1281 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1282 to find what the cancel signal actually is. */
1283 signal (__SIGRTMIN
+1, SIG_IGN
);
1287 initialize_low (void)
1290 set_target_ops (&linux_target_ops
);
1291 set_breakpoint_data (the_low_target
.breakpoint
,
1292 the_low_target
.breakpoint_len
);
1294 linux_init_signals ();