1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-1996, 1998-2012 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "linux-low.h"
21 #include "linux-osdata.h"
26 #include <sys/param.h>
27 #include <sys/ptrace.h>
28 #include "linux-ptrace.h"
29 #include "linux-procfs.h"
31 #include <sys/ioctl.h>
37 #include <sys/syscall.h>
41 #include <sys/types.h>
47 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
48 then ELFMAG0 will have been defined. If it didn't get included by
49 gdb_proc_service.h then including it will likely introduce a duplicate
50 definition of elf_fpregset_t. */
55 #define SPUFS_MAGIC 0x23c9b64e
58 #ifdef HAVE_PERSONALITY
59 # include <sys/personality.h>
60 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
61 # define ADDR_NO_RANDOMIZE 0x0040000
70 #define W_STOPCODE(sig) ((sig) << 8 | 0x7f)
73 /* This is the kernel's hard limit. Not to be confused with
80 #if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
85 #ifndef HAVE_ELF32_AUXV_T
86 /* Copied from glibc's elf.h. */
89 uint32_t a_type
; /* Entry type */
92 uint32_t a_val
; /* Integer value */
93 /* We use to have pointer elements added here. We cannot do that,
94 though, since it does not work when using 32-bit definitions
95 on 64-bit platforms and vice versa. */
100 #ifndef HAVE_ELF64_AUXV_T
101 /* Copied from glibc's elf.h. */
104 uint64_t a_type
; /* Entry type */
107 uint64_t a_val
; /* Integer value */
108 /* We use to have pointer elements added here. We cannot do that,
109 though, since it does not work when using 32-bit definitions
110 on 64-bit platforms and vice versa. */
115 /* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
116 representation of the thread ID.
118 ``all_lwps'' is keyed by the process ID - which on Linux is (presently)
119 the same as the LWP ID.
121 ``all_processes'' is keyed by the "overall process ID", which
122 GNU/Linux calls tgid, "thread group ID". */
124 struct inferior_list all_lwps
;
126 /* A list of all unknown processes which receive stop signals. Some
127 other process will presumably claim each of these as forked
128 children momentarily. */
130 struct simple_pid_list
132 /* The process ID. */
135 /* The status as reported by waitpid. */
139 struct simple_pid_list
*next
;
141 struct simple_pid_list
*stopped_pids
;
143 /* Trivial list manipulation functions to keep track of a list of new
144 stopped processes. */
147 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
149 struct simple_pid_list
*new_pid
= xmalloc (sizeof (struct simple_pid_list
));
152 new_pid
->status
= status
;
153 new_pid
->next
= *listp
;
158 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
160 struct simple_pid_list
**p
;
162 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
163 if ((*p
)->pid
== pid
)
165 struct simple_pid_list
*next
= (*p
)->next
;
167 *statusp
= (*p
)->status
;
175 /* FIXME this is a bit of a hack, and could be removed. */
176 int stopping_threads
;
178 /* FIXME make into a target method? */
179 int using_threads
= 1;
181 /* True if we're presently stabilizing threads (moving them out of
183 static int stabilizing_threads
;
185 /* This flag is true iff we've just created or attached to our first
186 inferior but it has not stopped yet. As soon as it does, we need
187 to call the low target's arch_setup callback. Doing this only on
188 the first inferior avoids reinializing the architecture on every
189 inferior, and avoids messing with the register caches of the
190 already running inferiors. NOTE: this assumes all inferiors under
191 control of gdbserver have the same architecture. */
192 static int new_inferior
;
194 static void linux_resume_one_lwp (struct lwp_info
*lwp
,
195 int step
, int signal
, siginfo_t
*info
);
196 static void linux_resume (struct thread_resume
*resume_info
, size_t n
);
197 static void stop_all_lwps (int suspend
, struct lwp_info
*except
);
198 static void unstop_all_lwps (int unsuspend
, struct lwp_info
*except
);
199 static int linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
);
200 static void *add_lwp (ptid_t ptid
);
201 static int linux_stopped_by_watchpoint (void);
202 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
203 static void proceed_all_lwps (void);
204 static int finish_step_over (struct lwp_info
*lwp
);
205 static CORE_ADDR
get_stop_pc (struct lwp_info
*lwp
);
206 static int kill_lwp (unsigned long lwpid
, int signo
);
207 static void linux_enable_event_reporting (int pid
);
209 /* True if the low target can hardware single-step. Such targets
210 don't need a BREAKPOINT_REINSERT_ADDR callback. */
213 can_hardware_single_step (void)
215 return (the_low_target
.breakpoint_reinsert_addr
== NULL
);
218 /* True if the low target supports memory breakpoints. If so, we'll
219 have a GET_PC implementation. */
222 supports_breakpoints (void)
224 return (the_low_target
.get_pc
!= NULL
);
227 /* Returns true if this target can support fast tracepoints. This
228 does not mean that the in-process agent has been loaded in the
232 supports_fast_tracepoints (void)
234 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
237 struct pending_signals
241 struct pending_signals
*prev
;
244 #define PTRACE_ARG3_TYPE void *
245 #define PTRACE_ARG4_TYPE void *
246 #define PTRACE_XFER_TYPE long
248 #ifdef HAVE_LINUX_REGSETS
249 static char *disabled_regsets
;
250 static int num_regsets
;
253 /* The read/write ends of the pipe registered as waitable file in the
255 static int linux_event_pipe
[2] = { -1, -1 };
257 /* True if we're currently in async mode. */
258 #define target_is_async_p() (linux_event_pipe[0] != -1)
260 static void send_sigstop (struct lwp_info
*lwp
);
261 static void wait_for_sigstop (struct inferior_list_entry
*entry
);
263 /* Return non-zero if HEADER is a 64-bit ELF file. */
266 elf_64_header_p (const Elf64_Ehdr
*header
)
268 return (header
->e_ident
[EI_MAG0
] == ELFMAG0
269 && header
->e_ident
[EI_MAG1
] == ELFMAG1
270 && header
->e_ident
[EI_MAG2
] == ELFMAG2
271 && header
->e_ident
[EI_MAG3
] == ELFMAG3
272 && header
->e_ident
[EI_CLASS
] == ELFCLASS64
);
275 /* Return non-zero if FILE is a 64-bit ELF file,
276 zero if the file is not a 64-bit ELF file,
277 and -1 if the file is not accessible or doesn't exist. */
280 elf_64_file_p (const char *file
)
285 fd
= open (file
, O_RDONLY
);
289 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
296 return elf_64_header_p (&header
);
299 /* Accepts an integer PID; Returns true if the executable PID is
300 running is a 64-bit ELF file.. */
303 linux_pid_exe_is_elf_64_file (int pid
)
305 char file
[MAXPATHLEN
];
307 sprintf (file
, "/proc/%d/exe", pid
);
308 return elf_64_file_p (file
);
312 delete_lwp (struct lwp_info
*lwp
)
314 remove_thread (get_lwp_thread (lwp
));
315 remove_inferior (&all_lwps
, &lwp
->head
);
316 free (lwp
->arch_private
);
320 /* Add a process to the common process list, and set its private
323 static struct process_info
*
324 linux_add_process (int pid
, int attached
)
326 struct process_info
*proc
;
328 /* Is this the first process? If so, then set the arch. */
329 if (all_processes
.head
== NULL
)
332 proc
= add_process (pid
, attached
);
333 proc
->private = xcalloc (1, sizeof (*proc
->private));
335 if (the_low_target
.new_process
!= NULL
)
336 proc
->private->arch_private
= the_low_target
.new_process ();
341 /* Wrapper function for waitpid which handles EINTR, and emulates
342 __WALL for systems where that is not available. */
345 my_waitpid (int pid
, int *status
, int flags
)
350 fprintf (stderr
, "my_waitpid (%d, 0x%x)\n", pid
, flags
);
354 sigset_t block_mask
, org_mask
, wake_mask
;
357 wnohang
= (flags
& WNOHANG
) != 0;
358 flags
&= ~(__WALL
| __WCLONE
);
361 /* Block all signals while here. This avoids knowing about
362 LinuxThread's signals. */
363 sigfillset (&block_mask
);
364 sigprocmask (SIG_BLOCK
, &block_mask
, &org_mask
);
366 /* ... except during the sigsuspend below. */
367 sigemptyset (&wake_mask
);
371 /* Since all signals are blocked, there's no need to check
373 ret
= waitpid (pid
, status
, flags
);
376 if (ret
== -1 && out_errno
!= ECHILD
)
381 if (flags
& __WCLONE
)
383 /* We've tried both flavors now. If WNOHANG is set,
384 there's nothing else to do, just bail out. */
389 fprintf (stderr
, "blocking\n");
391 /* Block waiting for signals. */
392 sigsuspend (&wake_mask
);
398 sigprocmask (SIG_SETMASK
, &org_mask
, NULL
);
403 ret
= waitpid (pid
, status
, flags
);
404 while (ret
== -1 && errno
== EINTR
);
409 fprintf (stderr
, "my_waitpid (%d, 0x%x): status(%x), %d\n",
410 pid
, flags
, status
? *status
: -1, ret
);
416 /* Handle a GNU/Linux extended wait response. If we see a clone
417 event, we need to add the new LWP to our list (and not report the
418 trap to higher layers). */
421 handle_extended_wait (struct lwp_info
*event_child
, int wstat
)
423 int event
= wstat
>> 16;
424 struct lwp_info
*new_lwp
;
426 if (event
== PTRACE_EVENT_CLONE
)
429 unsigned long new_pid
;
432 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_child
), 0, &new_pid
);
434 /* If we haven't already seen the new PID stop, wait for it now. */
435 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
437 /* The new child has a pending SIGSTOP. We can't affect it until it
438 hits the SIGSTOP, but we're already attached. */
440 ret
= my_waitpid (new_pid
, &status
, __WALL
);
443 perror_with_name ("waiting for new child");
444 else if (ret
!= new_pid
)
445 warning ("wait returned unexpected PID %d", ret
);
446 else if (!WIFSTOPPED (status
))
447 warning ("wait returned unexpected status 0x%x", status
);
450 linux_enable_event_reporting (new_pid
);
452 ptid
= ptid_build (pid_of (event_child
), new_pid
, 0);
453 new_lwp
= (struct lwp_info
*) add_lwp (ptid
);
454 add_thread (ptid
, new_lwp
);
456 /* Either we're going to immediately resume the new thread
457 or leave it stopped. linux_resume_one_lwp is a nop if it
458 thinks the thread is currently running, so set this first
459 before calling linux_resume_one_lwp. */
460 new_lwp
->stopped
= 1;
462 /* Normally we will get the pending SIGSTOP. But in some cases
463 we might get another signal delivered to the group first.
464 If we do get another signal, be sure not to lose it. */
465 if (WSTOPSIG (status
) == SIGSTOP
)
467 if (stopping_threads
)
468 new_lwp
->stop_pc
= get_stop_pc (new_lwp
);
470 linux_resume_one_lwp (new_lwp
, 0, 0, NULL
);
474 new_lwp
->stop_expected
= 1;
476 if (stopping_threads
)
478 new_lwp
->stop_pc
= get_stop_pc (new_lwp
);
479 new_lwp
->status_pending_p
= 1;
480 new_lwp
->status_pending
= status
;
483 /* Pass the signal on. This is what GDB does - except
484 shouldn't we really report it instead? */
485 linux_resume_one_lwp (new_lwp
, 0, WSTOPSIG (status
), NULL
);
488 /* Always resume the current thread. If we are stopping
489 threads, it will have a pending SIGSTOP; we may as well
491 linux_resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
495 /* Return the PC as read from the regcache of LWP, without any
499 get_pc (struct lwp_info
*lwp
)
501 struct thread_info
*saved_inferior
;
502 struct regcache
*regcache
;
505 if (the_low_target
.get_pc
== NULL
)
508 saved_inferior
= current_inferior
;
509 current_inferior
= get_lwp_thread (lwp
);
511 regcache
= get_thread_regcache (current_inferior
, 1);
512 pc
= (*the_low_target
.get_pc
) (regcache
);
515 fprintf (stderr
, "pc is 0x%lx\n", (long) pc
);
517 current_inferior
= saved_inferior
;
521 /* This function should only be called if LWP got a SIGTRAP.
522 The SIGTRAP could mean several things.
524 On i386, where decr_pc_after_break is non-zero:
525 If we were single-stepping this process using PTRACE_SINGLESTEP,
526 we will get only the one SIGTRAP (even if the instruction we
527 stepped over was a breakpoint). The value of $eip will be the
529 If we continue the process using PTRACE_CONT, we will get a
530 SIGTRAP when we hit a breakpoint. The value of $eip will be
531 the instruction after the breakpoint (i.e. needs to be
532 decremented). If we report the SIGTRAP to GDB, we must also
533 report the undecremented PC. If we cancel the SIGTRAP, we
534 must resume at the decremented PC.
536 (Presumably, not yet tested) On a non-decr_pc_after_break machine
537 with hardware or kernel single-step:
538 If we single-step over a breakpoint instruction, our PC will
539 point at the following instruction. If we continue and hit a
540 breakpoint instruction, our PC will point at the breakpoint
544 get_stop_pc (struct lwp_info
*lwp
)
548 if (the_low_target
.get_pc
== NULL
)
551 stop_pc
= get_pc (lwp
);
553 if (WSTOPSIG (lwp
->last_status
) == SIGTRAP
555 && !lwp
->stopped_by_watchpoint
556 && lwp
->last_status
>> 16 == 0)
557 stop_pc
-= the_low_target
.decr_pc_after_break
;
560 fprintf (stderr
, "stop pc is 0x%lx\n", (long) stop_pc
);
566 add_lwp (ptid_t ptid
)
568 struct lwp_info
*lwp
;
570 lwp
= (struct lwp_info
*) xmalloc (sizeof (*lwp
));
571 memset (lwp
, 0, sizeof (*lwp
));
575 if (the_low_target
.new_thread
!= NULL
)
576 lwp
->arch_private
= the_low_target
.new_thread ();
578 add_inferior_to_list (&all_lwps
, &lwp
->head
);
583 /* Start an inferior process and returns its pid.
584 ALLARGS is a vector of program-name and args. */
587 linux_create_inferior (char *program
, char **allargs
)
589 #ifdef HAVE_PERSONALITY
590 int personality_orig
= 0, personality_set
= 0;
592 struct lwp_info
*new_lwp
;
596 #ifdef HAVE_PERSONALITY
597 if (disable_randomization
)
600 personality_orig
= personality (0xffffffff);
601 if (errno
== 0 && !(personality_orig
& ADDR_NO_RANDOMIZE
))
604 personality (personality_orig
| ADDR_NO_RANDOMIZE
);
606 if (errno
!= 0 || (personality_set
607 && !(personality (0xffffffff) & ADDR_NO_RANDOMIZE
)))
608 warning ("Error disabling address space randomization: %s",
613 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
619 perror_with_name ("fork");
623 ptrace (PTRACE_TRACEME
, 0, 0, 0);
625 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
626 signal (__SIGRTMIN
+ 1, SIG_DFL
);
631 /* If gdbserver is connected to gdb via stdio, redirect the inferior's
632 stdout to stderr so that inferior i/o doesn't corrupt the connection.
633 Also, redirect stdin to /dev/null. */
634 if (remote_connection_is_stdio ())
637 open ("/dev/null", O_RDONLY
);
639 if (write (2, "stdin/stdout redirected\n",
640 sizeof ("stdin/stdout redirected\n") - 1) < 0)
641 /* Errors ignored. */;
644 execv (program
, allargs
);
646 execvp (program
, allargs
);
648 fprintf (stderr
, "Cannot exec %s: %s.\n", program
,
654 #ifdef HAVE_PERSONALITY
658 personality (personality_orig
);
660 warning ("Error restoring address space randomization: %s",
665 linux_add_process (pid
, 0);
667 ptid
= ptid_build (pid
, pid
, 0);
668 new_lwp
= add_lwp (ptid
);
669 add_thread (ptid
, new_lwp
);
670 new_lwp
->must_set_ptrace_flags
= 1;
675 /* Attach to an inferior process. */
678 linux_attach_lwp_1 (unsigned long lwpid
, int initial
)
681 struct lwp_info
*new_lwp
;
683 if (ptrace (PTRACE_ATTACH
, lwpid
, 0, 0) != 0)
685 struct buffer buffer
;
689 /* If we fail to attach to an LWP, just warn. */
690 fprintf (stderr
, "Cannot attach to lwp %ld: %s (%d)\n", lwpid
,
691 strerror (errno
), errno
);
696 /* If we fail to attach to a process, report an error. */
697 buffer_init (&buffer
);
698 linux_ptrace_attach_warnings (lwpid
, &buffer
);
699 buffer_grow_str0 (&buffer
, "");
700 error ("%sCannot attach to lwp %ld: %s (%d)", buffer_finish (&buffer
),
701 lwpid
, strerror (errno
), errno
);
705 /* If lwp is the tgid, we handle adding existing threads later.
706 Otherwise we just add lwp without bothering about any other
708 ptid
= ptid_build (lwpid
, lwpid
, 0);
711 /* Note that extracting the pid from the current inferior is
712 safe, since we're always called in the context of the same
713 process as this new thread. */
714 int pid
= pid_of (get_thread_lwp (current_inferior
));
715 ptid
= ptid_build (pid
, lwpid
, 0);
718 new_lwp
= (struct lwp_info
*) add_lwp (ptid
);
719 add_thread (ptid
, new_lwp
);
721 /* We need to wait for SIGSTOP before being able to make the next
722 ptrace call on this LWP. */
723 new_lwp
->must_set_ptrace_flags
= 1;
725 if (linux_proc_pid_is_stopped (lwpid
))
729 "Attached to a stopped process\n");
731 /* The process is definitely stopped. It is in a job control
732 stop, unless the kernel predates the TASK_STOPPED /
733 TASK_TRACED distinction, in which case it might be in a
734 ptrace stop. Make sure it is in a ptrace stop; from there we
735 can kill it, signal it, et cetera.
737 First make sure there is a pending SIGSTOP. Since we are
738 already attached, the process can not transition from stopped
739 to running without a PTRACE_CONT; so we know this signal will
740 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
741 probably already in the queue (unless this kernel is old
742 enough to use TASK_STOPPED for ptrace stops); but since
743 SIGSTOP is not an RT signal, it can only be queued once. */
744 kill_lwp (lwpid
, SIGSTOP
);
746 /* Finally, resume the stopped process. This will deliver the
747 SIGSTOP (or a higher priority signal, just like normal
748 PTRACE_ATTACH), which we'll catch later on. */
749 ptrace (PTRACE_CONT
, lwpid
, 0, 0);
752 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
755 There are several cases to consider here:
757 1) gdbserver has already attached to the process and is being notified
758 of a new thread that is being created.
759 In this case we should ignore that SIGSTOP and resume the
760 process. This is handled below by setting stop_expected = 1,
761 and the fact that add_thread sets last_resume_kind ==
764 2) This is the first thread (the process thread), and we're attaching
765 to it via attach_inferior.
766 In this case we want the process thread to stop.
767 This is handled by having linux_attach set last_resume_kind ==
768 resume_stop after we return.
770 If the pid we are attaching to is also the tgid, we attach to and
771 stop all the existing threads. Otherwise, we attach to pid and
772 ignore any other threads in the same group as this pid.
774 3) GDB is connecting to gdbserver and is requesting an enumeration of all
776 In this case we want the thread to stop.
777 FIXME: This case is currently not properly handled.
778 We should wait for the SIGSTOP but don't. Things work apparently
779 because enough time passes between when we ptrace (ATTACH) and when
780 gdb makes the next ptrace call on the thread.
782 On the other hand, if we are currently trying to stop all threads, we
783 should treat the new thread as if we had sent it a SIGSTOP. This works
784 because we are guaranteed that the add_lwp call above added us to the
785 end of the list, and so the new thread has not yet reached
786 wait_for_sigstop (but will). */
787 new_lwp
->stop_expected
= 1;
791 linux_attach_lwp (unsigned long lwpid
)
793 linux_attach_lwp_1 (lwpid
, 0);
796 /* Attach to PID. If PID is the tgid, attach to it and all
800 linux_attach (unsigned long pid
)
802 /* Attach to PID. We will check for other threads
804 linux_attach_lwp_1 (pid
, 1);
805 linux_add_process (pid
, 1);
809 struct thread_info
*thread
;
811 /* Don't ignore the initial SIGSTOP if we just attached to this
812 process. It will be collected by wait shortly. */
813 thread
= find_thread_ptid (ptid_build (pid
, pid
, 0));
814 thread
->last_resume_kind
= resume_stop
;
817 if (linux_proc_get_tgid (pid
) == pid
)
822 sprintf (pathname
, "/proc/%ld/task", pid
);
824 dir
= opendir (pathname
);
828 fprintf (stderr
, "Could not open /proc/%ld/task.\n", pid
);
833 /* At this point we attached to the tgid. Scan the task for
836 int new_threads_found
;
840 while (iterations
< 2)
842 new_threads_found
= 0;
843 /* Add all the other threads. While we go through the
844 threads, new threads may be spawned. Cycle through
845 the list of threads until we have done two iterations without
846 finding new threads. */
847 while ((dp
= readdir (dir
)) != NULL
)
850 lwp
= strtoul (dp
->d_name
, NULL
, 10);
852 /* Is this a new thread? */
854 && find_thread_ptid (ptid_build (pid
, lwp
, 0)) == NULL
)
856 linux_attach_lwp_1 (lwp
, 0);
861 Found and attached to new lwp %ld\n", lwp
);
865 if (!new_threads_found
)
886 second_thread_of_pid_p (struct inferior_list_entry
*entry
, void *args
)
888 struct counter
*counter
= args
;
890 if (ptid_get_pid (entry
->id
) == counter
->pid
)
892 if (++counter
->count
> 1)
900 last_thread_of_process_p (struct thread_info
*thread
)
902 ptid_t ptid
= ((struct inferior_list_entry
*)thread
)->id
;
903 int pid
= ptid_get_pid (ptid
);
904 struct counter counter
= { pid
, 0 };
906 return (find_inferior (&all_threads
,
907 second_thread_of_pid_p
, &counter
) == NULL
);
913 linux_kill_one_lwp (struct lwp_info
*lwp
)
915 int pid
= lwpid_of (lwp
);
917 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
918 there is no signal context, and ptrace(PTRACE_KILL) (or
919 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
920 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
921 alternative is to kill with SIGKILL. We only need one SIGKILL
922 per process, not one for each thread. But since we still support
923 linuxthreads, and we also support debugging programs using raw
924 clone without CLONE_THREAD, we send one for each thread. For
925 years, we used PTRACE_KILL only, so we're being a bit paranoid
926 about some old kernels where PTRACE_KILL might work better
927 (dubious if there are any such, but that's why it's paranoia), so
928 we try SIGKILL first, PTRACE_KILL second, and so we're fine
935 "LKL: kill (SIGKILL) %s, 0, 0 (%s)\n",
936 target_pid_to_str (ptid_of (lwp
)),
937 errno
? strerror (errno
) : "OK");
940 ptrace (PTRACE_KILL
, pid
, 0, 0);
943 "LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
944 target_pid_to_str (ptid_of (lwp
)),
945 errno
? strerror (errno
) : "OK");
948 /* Callback for `find_inferior'. Kills an lwp of a given process,
949 except the leader. */
952 kill_one_lwp_callback (struct inferior_list_entry
*entry
, void *args
)
954 struct thread_info
*thread
= (struct thread_info
*) entry
;
955 struct lwp_info
*lwp
= get_thread_lwp (thread
);
957 int pid
= * (int *) args
;
959 if (ptid_get_pid (entry
->id
) != pid
)
962 /* We avoid killing the first thread here, because of a Linux kernel (at
963 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
964 the children get a chance to be reaped, it will remain a zombie
967 if (lwpid_of (lwp
) == pid
)
970 fprintf (stderr
, "lkop: is last of process %s\n",
971 target_pid_to_str (entry
->id
));
977 linux_kill_one_lwp (lwp
);
979 /* Make sure it died. The loop is most likely unnecessary. */
980 pid
= linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
981 } while (pid
> 0 && WIFSTOPPED (wstat
));
989 struct process_info
*process
;
990 struct lwp_info
*lwp
;
994 process
= find_process_pid (pid
);
998 /* If we're killing a running inferior, make sure it is stopped
999 first, as PTRACE_KILL will not work otherwise. */
1000 stop_all_lwps (0, NULL
);
1002 find_inferior (&all_threads
, kill_one_lwp_callback
, &pid
);
1004 /* See the comment in linux_kill_one_lwp. We did not kill the first
1005 thread in the list, so do so now. */
1006 lwp
= find_lwp_pid (pid_to_ptid (pid
));
1011 fprintf (stderr
, "lk_1: cannot find lwp %ld, for pid: %d\n",
1012 lwpid_of (lwp
), pid
);
1017 fprintf (stderr
, "lk_1: killing lwp %ld, for pid: %d\n",
1018 lwpid_of (lwp
), pid
);
1022 linux_kill_one_lwp (lwp
);
1024 /* Make sure it died. The loop is most likely unnecessary. */
1025 lwpid
= linux_wait_for_event (lwp
->head
.id
, &wstat
, __WALL
);
1026 } while (lwpid
> 0 && WIFSTOPPED (wstat
));
1029 the_target
->mourn (process
);
1031 /* Since we presently can only stop all lwps of all processes, we
1032 need to unstop lwps of other processes. */
1033 unstop_all_lwps (0, NULL
);
1037 /* Get pending signal of THREAD, for detaching purposes. This is the
1038 signal the thread last stopped for, which we need to deliver to the
1039 thread when detaching, otherwise, it'd be suppressed/lost. */
1042 get_detach_signal (struct thread_info
*thread
)
1044 enum target_signal signo
= TARGET_SIGNAL_0
;
1046 struct lwp_info
*lp
= get_thread_lwp (thread
);
1048 if (lp
->status_pending_p
)
1049 status
= lp
->status_pending
;
1052 /* If the thread had been suspended by gdbserver, and it stopped
1053 cleanly, then it'll have stopped with SIGSTOP. But we don't
1054 want to deliver that SIGSTOP. */
1055 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1056 || thread
->last_status
.value
.sig
== TARGET_SIGNAL_0
)
1059 /* Otherwise, we may need to deliver the signal we
1061 status
= lp
->last_status
;
1064 if (!WIFSTOPPED (status
))
1068 "GPS: lwp %s hasn't stopped: no pending signal\n",
1069 target_pid_to_str (ptid_of (lp
)));
1073 /* Extended wait statuses aren't real SIGTRAPs. */
1074 if (WSTOPSIG (status
) == SIGTRAP
&& status
>> 16 != 0)
1078 "GPS: lwp %s had stopped with extended "
1079 "status: no pending signal\n",
1080 target_pid_to_str (ptid_of (lp
)));
1084 signo
= target_signal_from_host (WSTOPSIG (status
));
1086 if (program_signals_p
&& !program_signals
[signo
])
1090 "GPS: lwp %s had signal %s, but it is in nopass state\n",
1091 target_pid_to_str (ptid_of (lp
)),
1092 target_signal_to_string (signo
));
1095 else if (!program_signals_p
1096 /* If we have no way to know which signals GDB does not
1097 want to have passed to the program, assume
1098 SIGTRAP/SIGINT, which is GDB's default. */
1099 && (signo
== TARGET_SIGNAL_TRAP
|| signo
== TARGET_SIGNAL_INT
))
1103 "GPS: lwp %s had signal %s, "
1104 "but we don't know if we should pass it. Default to not.\n",
1105 target_pid_to_str (ptid_of (lp
)),
1106 target_signal_to_string (signo
));
1113 "GPS: lwp %s has pending signal %s: delivering it.\n",
1114 target_pid_to_str (ptid_of (lp
)),
1115 target_signal_to_string (signo
));
1117 return WSTOPSIG (status
);
1122 linux_detach_one_lwp (struct inferior_list_entry
*entry
, void *args
)
1124 struct thread_info
*thread
= (struct thread_info
*) entry
;
1125 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1126 int pid
= * (int *) args
;
1129 if (ptid_get_pid (entry
->id
) != pid
)
1132 /* If there is a pending SIGSTOP, get rid of it. */
1133 if (lwp
->stop_expected
)
1137 "Sending SIGCONT to %s\n",
1138 target_pid_to_str (ptid_of (lwp
)));
1140 kill_lwp (lwpid_of (lwp
), SIGCONT
);
1141 lwp
->stop_expected
= 0;
1144 /* Flush any pending changes to the process's registers. */
1145 regcache_invalidate_one ((struct inferior_list_entry
*)
1146 get_lwp_thread (lwp
));
1148 /* Pass on any pending signal for this thread. */
1149 sig
= get_detach_signal (thread
);
1151 /* Finally, let it resume. */
1152 if (the_low_target
.prepare_to_resume
!= NULL
)
1153 the_low_target
.prepare_to_resume (lwp
);
1154 if (ptrace (PTRACE_DETACH
, lwpid_of (lwp
), 0, sig
) < 0)
1155 error (_("Can't detach %s: %s"),
1156 target_pid_to_str (ptid_of (lwp
)),
1164 linux_detach (int pid
)
1166 struct process_info
*process
;
1168 process
= find_process_pid (pid
);
1169 if (process
== NULL
)
1172 /* Stop all threads before detaching. First, ptrace requires that
1173 the thread is stopped to sucessfully detach. Second, thread_db
1174 may need to uninstall thread event breakpoints from memory, which
1175 only works with a stopped process anyway. */
1176 stop_all_lwps (0, NULL
);
1178 #ifdef USE_THREAD_DB
1179 thread_db_detach (process
);
1182 /* Stabilize threads (move out of jump pads). */
1183 stabilize_threads ();
1185 find_inferior (&all_threads
, linux_detach_one_lwp
, &pid
);
1187 the_target
->mourn (process
);
1189 /* Since we presently can only stop all lwps of all processes, we
1190 need to unstop lwps of other processes. */
1191 unstop_all_lwps (0, NULL
);
1195 /* Remove all LWPs that belong to process PROC from the lwp list. */
1198 delete_lwp_callback (struct inferior_list_entry
*entry
, void *proc
)
1200 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
1201 struct process_info
*process
= proc
;
1203 if (pid_of (lwp
) == pid_of (process
))
1210 linux_mourn (struct process_info
*process
)
1212 struct process_info_private
*priv
;
1214 #ifdef USE_THREAD_DB
1215 thread_db_mourn (process
);
1218 find_inferior (&all_lwps
, delete_lwp_callback
, process
);
1220 /* Freeing all private data. */
1221 priv
= process
->private;
1222 free (priv
->arch_private
);
1224 process
->private = NULL
;
1226 remove_process (process
);
1230 linux_join (int pid
)
1235 ret
= my_waitpid (pid
, &status
, 0);
1236 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1238 } while (ret
!= -1 || errno
!= ECHILD
);
1241 /* Return nonzero if the given thread is still alive. */
1243 linux_thread_alive (ptid_t ptid
)
1245 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1247 /* We assume we always know if a thread exits. If a whole process
1248 exited but we still haven't been able to report it to GDB, we'll
1249 hold on to the last lwp of the dead process. */
1256 /* Return 1 if this lwp has an interesting status pending. */
1258 status_pending_p_callback (struct inferior_list_entry
*entry
, void *arg
)
1260 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
1261 ptid_t ptid
= * (ptid_t
*) arg
;
1262 struct thread_info
*thread
;
1264 /* Check if we're only interested in events from a specific process
1266 if (!ptid_equal (minus_one_ptid
, ptid
)
1267 && ptid_get_pid (ptid
) != ptid_get_pid (lwp
->head
.id
))
1270 thread
= get_lwp_thread (lwp
);
1272 /* If we got a `vCont;t', but we haven't reported a stop yet, do
1273 report any status pending the LWP may have. */
1274 if (thread
->last_resume_kind
== resume_stop
1275 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
1278 return lwp
->status_pending_p
;
1282 same_lwp (struct inferior_list_entry
*entry
, void *data
)
1284 ptid_t ptid
= *(ptid_t
*) data
;
1287 if (ptid_get_lwp (ptid
) != 0)
1288 lwp
= ptid_get_lwp (ptid
);
1290 lwp
= ptid_get_pid (ptid
);
1292 if (ptid_get_lwp (entry
->id
) == lwp
)
1299 find_lwp_pid (ptid_t ptid
)
1301 return (struct lwp_info
*) find_inferior (&all_lwps
, same_lwp
, &ptid
);
1304 static struct lwp_info
*
1305 linux_wait_for_lwp (ptid_t ptid
, int *wstatp
, int options
)
1308 int to_wait_for
= -1;
1309 struct lwp_info
*child
= NULL
;
1312 fprintf (stderr
, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid
));
1314 if (ptid_equal (ptid
, minus_one_ptid
))
1315 to_wait_for
= -1; /* any child */
1317 to_wait_for
= ptid_get_lwp (ptid
); /* this lwp only */
1323 ret
= my_waitpid (to_wait_for
, wstatp
, options
);
1324 if (ret
== 0 || (ret
== -1 && errno
== ECHILD
&& (options
& WNOHANG
)))
1327 perror_with_name ("waitpid");
1330 && (!WIFSTOPPED (*wstatp
)
1331 || (WSTOPSIG (*wstatp
) != 32
1332 && WSTOPSIG (*wstatp
) != 33)))
1333 fprintf (stderr
, "Got an event from %d (%x)\n", ret
, *wstatp
);
1335 child
= find_lwp_pid (pid_to_ptid (ret
));
1337 /* If we didn't find a process, one of two things presumably happened:
1338 - A process we started and then detached from has exited. Ignore it.
1339 - A process we are controlling has forked and the new child's stop
1340 was reported to us by the kernel. Save its PID. */
1341 if (child
== NULL
&& WIFSTOPPED (*wstatp
))
1343 add_to_pid_list (&stopped_pids
, ret
, *wstatp
);
1346 else if (child
== NULL
)
1351 child
->last_status
= *wstatp
;
1353 /* Architecture-specific setup after inferior is running.
1354 This needs to happen after we have attached to the inferior
1355 and it is stopped for the first time, but before we access
1356 any inferior registers. */
1359 the_low_target
.arch_setup ();
1360 #ifdef HAVE_LINUX_REGSETS
1361 memset (disabled_regsets
, 0, num_regsets
);
1366 /* Fetch the possibly triggered data watchpoint info and store it in
1369 On some archs, like x86, that use debug registers to set
1370 watchpoints, it's possible that the way to know which watched
1371 address trapped, is to check the register that is used to select
1372 which address to watch. Problem is, between setting the
1373 watchpoint and reading back which data address trapped, the user
1374 may change the set of watchpoints, and, as a consequence, GDB
1375 changes the debug registers in the inferior. To avoid reading
1376 back a stale stopped-data-address when that happens, we cache in
1377 LP the fact that a watchpoint trapped, and the corresponding data
1378 address, as soon as we see CHILD stop with a SIGTRAP. If GDB
1379 changes the debug registers meanwhile, we have the cached data we
1382 if (WIFSTOPPED (*wstatp
) && WSTOPSIG (*wstatp
) == SIGTRAP
)
1384 if (the_low_target
.stopped_by_watchpoint
== NULL
)
1386 child
->stopped_by_watchpoint
= 0;
1390 struct thread_info
*saved_inferior
;
1392 saved_inferior
= current_inferior
;
1393 current_inferior
= get_lwp_thread (child
);
1395 child
->stopped_by_watchpoint
1396 = the_low_target
.stopped_by_watchpoint ();
1398 if (child
->stopped_by_watchpoint
)
1400 if (the_low_target
.stopped_data_address
!= NULL
)
1401 child
->stopped_data_address
1402 = the_low_target
.stopped_data_address ();
1404 child
->stopped_data_address
= 0;
1407 current_inferior
= saved_inferior
;
1411 /* Store the STOP_PC, with adjustment applied. This depends on the
1412 architecture being defined already (so that CHILD has a valid
1413 regcache), and on LAST_STATUS being set (to check for SIGTRAP or
1415 if (WIFSTOPPED (*wstatp
))
1416 child
->stop_pc
= get_stop_pc (child
);
1419 && WIFSTOPPED (*wstatp
)
1420 && the_low_target
.get_pc
!= NULL
)
1422 struct thread_info
*saved_inferior
= current_inferior
;
1423 struct regcache
*regcache
;
1426 current_inferior
= get_lwp_thread (child
);
1427 regcache
= get_thread_regcache (current_inferior
, 1);
1428 pc
= (*the_low_target
.get_pc
) (regcache
);
1429 fprintf (stderr
, "linux_wait_for_lwp: pc is 0x%lx\n", (long) pc
);
1430 current_inferior
= saved_inferior
;
1436 /* This function should only be called if the LWP got a SIGTRAP.
1438 Handle any tracepoint steps or hits. Return true if a tracepoint
1439 event was handled, 0 otherwise. */
1442 handle_tracepoints (struct lwp_info
*lwp
)
1444 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1445 int tpoint_related_event
= 0;
1447 /* If this tracepoint hit causes a tracing stop, we'll immediately
1448 uninsert tracepoints. To do this, we temporarily pause all
1449 threads, unpatch away, and then unpause threads. We need to make
1450 sure the unpausing doesn't resume LWP too. */
1453 /* And we need to be sure that any all-threads-stopping doesn't try
1454 to move threads out of the jump pads, as it could deadlock the
1455 inferior (LWP could be in the jump pad, maybe even holding the
1458 /* Do any necessary step collect actions. */
1459 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1461 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1463 /* See if we just hit a tracepoint and do its main collect
1465 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1469 gdb_assert (lwp
->suspended
== 0);
1470 gdb_assert (!stabilizing_threads
|| lwp
->collecting_fast_tracepoint
);
1472 if (tpoint_related_event
)
1475 fprintf (stderr
, "got a tracepoint event\n");
1482 /* Convenience wrapper. Returns true if LWP is presently collecting a
1486 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
1487 struct fast_tpoint_collect_status
*status
)
1489 CORE_ADDR thread_area
;
1491 if (the_low_target
.get_thread_area
== NULL
)
1494 /* Get the thread area address. This is used to recognize which
1495 thread is which when tracing with the in-process agent library.
1496 We don't read anything from the address, and treat it as opaque;
1497 it's the address itself that we assume is unique per-thread. */
1498 if ((*the_low_target
.get_thread_area
) (lwpid_of (lwp
), &thread_area
) == -1)
1501 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
1504 /* The reason we resume in the caller, is because we want to be able
1505 to pass lwp->status_pending as WSTAT, and we need to clear
1506 status_pending_p before resuming, otherwise, linux_resume_one_lwp
1507 refuses to resume. */
1510 maybe_move_out_of_jump_pad (struct lwp_info
*lwp
, int *wstat
)
1512 struct thread_info
*saved_inferior
;
1514 saved_inferior
= current_inferior
;
1515 current_inferior
= get_lwp_thread (lwp
);
1518 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
1519 && supports_fast_tracepoints ()
1520 && agent_loaded_p ())
1522 struct fast_tpoint_collect_status status
;
1527 Checking whether LWP %ld needs to move out of the jump pad.\n",
1530 r
= linux_fast_tracepoint_collecting (lwp
, &status
);
1533 || (WSTOPSIG (*wstat
) != SIGILL
1534 && WSTOPSIG (*wstat
) != SIGFPE
1535 && WSTOPSIG (*wstat
) != SIGSEGV
1536 && WSTOPSIG (*wstat
) != SIGBUS
))
1538 lwp
->collecting_fast_tracepoint
= r
;
1542 if (r
== 1 && lwp
->exit_jump_pad_bkpt
== NULL
)
1544 /* Haven't executed the original instruction yet.
1545 Set breakpoint there, and wait till it's hit,
1546 then single-step until exiting the jump pad. */
1547 lwp
->exit_jump_pad_bkpt
1548 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
1553 Checking whether LWP %ld needs to move out of the jump pad...it does\n",
1555 current_inferior
= saved_inferior
;
1562 /* If we get a synchronous signal while collecting, *and*
1563 while executing the (relocated) original instruction,
1564 reset the PC to point at the tpoint address, before
1565 reporting to GDB. Otherwise, it's an IPA lib bug: just
1566 report the signal to GDB, and pray for the best. */
1568 lwp
->collecting_fast_tracepoint
= 0;
1571 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
1572 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
1575 struct regcache
*regcache
;
1577 /* The si_addr on a few signals references the address
1578 of the faulting instruction. Adjust that as
1580 if ((WSTOPSIG (*wstat
) == SIGILL
1581 || WSTOPSIG (*wstat
) == SIGFPE
1582 || WSTOPSIG (*wstat
) == SIGBUS
1583 || WSTOPSIG (*wstat
) == SIGSEGV
)
1584 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
), 0, &info
) == 0
1585 /* Final check just to make sure we don't clobber
1586 the siginfo of non-kernel-sent signals. */
1587 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
1589 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
1590 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
), 0, &info
);
1593 regcache
= get_thread_regcache (get_lwp_thread (lwp
), 1);
1594 (*the_low_target
.set_pc
) (regcache
, status
.tpoint_addr
);
1595 lwp
->stop_pc
= status
.tpoint_addr
;
1597 /* Cancel any fast tracepoint lock this thread was
1599 force_unlock_trace_buffer ();
1602 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
1606 "Cancelling fast exit-jump-pad: removing bkpt. "
1607 "stopping all threads momentarily.\n");
1609 stop_all_lwps (1, lwp
);
1610 cancel_breakpoints ();
1612 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
1613 lwp
->exit_jump_pad_bkpt
= NULL
;
1615 unstop_all_lwps (1, lwp
);
1617 gdb_assert (lwp
->suspended
>= 0);
1624 Checking whether LWP %ld needs to move out of the jump pad...no\n",
1627 current_inferior
= saved_inferior
;
1631 /* Enqueue one signal in the "signals to report later when out of the
1635 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1637 struct pending_signals
*p_sig
;
1641 Deferring signal %d for LWP %ld.\n", WSTOPSIG (*wstat
), lwpid_of (lwp
));
1645 struct pending_signals
*sig
;
1647 for (sig
= lwp
->pending_signals_to_report
;
1651 " Already queued %d\n",
1654 fprintf (stderr
, " (no more currently queued signals)\n");
1657 /* Don't enqueue non-RT signals if they are already in the deferred
1658 queue. (SIGSTOP being the easiest signal to see ending up here
1660 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
1662 struct pending_signals
*sig
;
1664 for (sig
= lwp
->pending_signals_to_report
;
1668 if (sig
->signal
== WSTOPSIG (*wstat
))
1672 "Not requeuing already queued non-RT signal %d"
1681 p_sig
= xmalloc (sizeof (*p_sig
));
1682 p_sig
->prev
= lwp
->pending_signals_to_report
;
1683 p_sig
->signal
= WSTOPSIG (*wstat
);
1684 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
1685 ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
), 0, &p_sig
->info
);
1687 lwp
->pending_signals_to_report
= p_sig
;
1690 /* Dequeue one signal from the "signals to report later when out of
1691 the jump pad" list. */
1694 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
1696 if (lwp
->pending_signals_to_report
!= NULL
)
1698 struct pending_signals
**p_sig
;
1700 p_sig
= &lwp
->pending_signals_to_report
;
1701 while ((*p_sig
)->prev
!= NULL
)
1702 p_sig
= &(*p_sig
)->prev
;
1704 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
1705 if ((*p_sig
)->info
.si_signo
!= 0)
1706 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
), 0, &(*p_sig
)->info
);
1711 fprintf (stderr
, "Reporting deferred signal %d for LWP %ld.\n",
1712 WSTOPSIG (*wstat
), lwpid_of (lwp
));
1716 struct pending_signals
*sig
;
1718 for (sig
= lwp
->pending_signals_to_report
;
1722 " Still queued %d\n",
1725 fprintf (stderr
, " (no more queued signals)\n");
1734 /* Arrange for a breakpoint to be hit again later. We don't keep the
1735 SIGTRAP status and don't forward the SIGTRAP signal to the LWP. We
1736 will handle the current event, eventually we will resume this LWP,
1737 and this breakpoint will trap again. */
1740 cancel_breakpoint (struct lwp_info
*lwp
)
1742 struct thread_info
*saved_inferior
;
1744 /* There's nothing to do if we don't support breakpoints. */
1745 if (!supports_breakpoints ())
1748 /* breakpoint_at reads from current inferior. */
1749 saved_inferior
= current_inferior
;
1750 current_inferior
= get_lwp_thread (lwp
);
1752 if ((*the_low_target
.breakpoint_at
) (lwp
->stop_pc
))
1756 "CB: Push back breakpoint for %s\n",
1757 target_pid_to_str (ptid_of (lwp
)));
1759 /* Back up the PC if necessary. */
1760 if (the_low_target
.decr_pc_after_break
)
1762 struct regcache
*regcache
1763 = get_thread_regcache (current_inferior
, 1);
1764 (*the_low_target
.set_pc
) (regcache
, lwp
->stop_pc
);
1767 current_inferior
= saved_inferior
;
1774 "CB: No breakpoint found at %s for [%s]\n",
1775 paddress (lwp
->stop_pc
),
1776 target_pid_to_str (ptid_of (lwp
)));
1779 current_inferior
= saved_inferior
;
1783 /* When the event-loop is doing a step-over, this points at the thread
1785 ptid_t step_over_bkpt
;
1787 /* Wait for an event from child PID. If PID is -1, wait for any
1788 child. Store the stop status through the status pointer WSTAT.
1789 OPTIONS is passed to the waitpid call. Return 0 if no child stop
1790 event was found and OPTIONS contains WNOHANG. Return the PID of
1791 the stopped child otherwise. */
1794 linux_wait_for_event (ptid_t ptid
, int *wstat
, int options
)
1796 struct lwp_info
*event_child
, *requested_child
;
1800 requested_child
= NULL
;
1802 /* Check for a lwp with a pending status. */
1804 if (ptid_equal (ptid
, minus_one_ptid
) || ptid_is_pid (ptid
))
1806 event_child
= (struct lwp_info
*)
1807 find_inferior (&all_lwps
, status_pending_p_callback
, &ptid
);
1808 if (debug_threads
&& event_child
)
1809 fprintf (stderr
, "Got a pending child %ld\n", lwpid_of (event_child
));
1813 requested_child
= find_lwp_pid (ptid
);
1815 if (!stopping_threads
1816 && requested_child
->status_pending_p
1817 && requested_child
->collecting_fast_tracepoint
)
1819 enqueue_one_deferred_signal (requested_child
,
1820 &requested_child
->status_pending
);
1821 requested_child
->status_pending_p
= 0;
1822 requested_child
->status_pending
= 0;
1823 linux_resume_one_lwp (requested_child
, 0, 0, NULL
);
1826 if (requested_child
->suspended
1827 && requested_child
->status_pending_p
)
1828 fatal ("requesting an event out of a suspended child?");
1830 if (requested_child
->status_pending_p
)
1831 event_child
= requested_child
;
1834 if (event_child
!= NULL
)
1837 fprintf (stderr
, "Got an event from pending child %ld (%04x)\n",
1838 lwpid_of (event_child
), event_child
->status_pending
);
1839 *wstat
= event_child
->status_pending
;
1840 event_child
->status_pending_p
= 0;
1841 event_child
->status_pending
= 0;
1842 current_inferior
= get_lwp_thread (event_child
);
1843 return lwpid_of (event_child
);
1846 if (ptid_is_pid (ptid
))
1848 /* A request to wait for a specific tgid. This is not possible
1849 with waitpid, so instead, we wait for any child, and leave
1850 children we're not interested in right now with a pending
1851 status to report later. */
1852 wait_ptid
= minus_one_ptid
;
1857 /* We only enter this loop if no process has a pending wait status. Thus
1858 any action taken in response to a wait status inside this loop is
1859 responding as soon as we detect the status, not after any pending
1863 event_child
= linux_wait_for_lwp (wait_ptid
, wstat
, options
);
1865 if ((options
& WNOHANG
) && event_child
== NULL
)
1868 fprintf (stderr
, "WNOHANG set, no event found\n");
1872 if (event_child
== NULL
)
1873 error ("event from unknown child");
1875 if (ptid_is_pid (ptid
)
1876 && ptid_get_pid (ptid
) != ptid_get_pid (ptid_of (event_child
)))
1878 if (! WIFSTOPPED (*wstat
))
1879 mark_lwp_dead (event_child
, *wstat
);
1882 event_child
->status_pending_p
= 1;
1883 event_child
->status_pending
= *wstat
;
1888 current_inferior
= get_lwp_thread (event_child
);
1890 /* Check for thread exit. */
1891 if (! WIFSTOPPED (*wstat
))
1894 fprintf (stderr
, "LWP %ld exiting\n", lwpid_of (event_child
));
1896 /* If the last thread is exiting, just return. */
1897 if (last_thread_of_process_p (current_inferior
))
1900 fprintf (stderr
, "LWP %ld is last lwp of process\n",
1901 lwpid_of (event_child
));
1902 return lwpid_of (event_child
);
1907 current_inferior
= (struct thread_info
*) all_threads
.head
;
1909 fprintf (stderr
, "Current inferior is now %ld\n",
1910 lwpid_of (get_thread_lwp (current_inferior
)));
1914 current_inferior
= NULL
;
1916 fprintf (stderr
, "Current inferior is now <NULL>\n");
1919 /* If we were waiting for this particular child to do something...
1920 well, it did something. */
1921 if (requested_child
!= NULL
)
1923 int lwpid
= lwpid_of (event_child
);
1925 /* Cancel the step-over operation --- the thread that
1926 started it is gone. */
1927 if (finish_step_over (event_child
))
1928 unstop_all_lwps (1, event_child
);
1929 delete_lwp (event_child
);
1933 delete_lwp (event_child
);
1935 /* Wait for a more interesting event. */
1939 if (event_child
->must_set_ptrace_flags
)
1941 linux_enable_event_reporting (lwpid_of (event_child
));
1942 event_child
->must_set_ptrace_flags
= 0;
1945 if (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) == SIGTRAP
1946 && *wstat
>> 16 != 0)
1948 handle_extended_wait (event_child
, *wstat
);
1952 if (WIFSTOPPED (*wstat
)
1953 && WSTOPSIG (*wstat
) == SIGSTOP
1954 && event_child
->stop_expected
)
1959 fprintf (stderr
, "Expected stop.\n");
1960 event_child
->stop_expected
= 0;
1962 should_stop
= (current_inferior
->last_resume_kind
== resume_stop
1963 || stopping_threads
);
1967 linux_resume_one_lwp (event_child
,
1968 event_child
->stepping
, 0, NULL
);
1973 return lwpid_of (event_child
);
1980 /* Count the LWP's that have had events. */
1983 count_events_callback (struct inferior_list_entry
*entry
, void *data
)
1985 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
1986 struct thread_info
*thread
= get_lwp_thread (lp
);
1989 gdb_assert (count
!= NULL
);
1991 /* Count only resumed LWPs that have a SIGTRAP event pending that
1992 should be reported to GDB. */
1993 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
1994 && thread
->last_resume_kind
!= resume_stop
1995 && lp
->status_pending_p
1996 && WIFSTOPPED (lp
->status_pending
)
1997 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
1998 && !breakpoint_inserted_here (lp
->stop_pc
))
2004 /* Select the LWP (if any) that is currently being single-stepped. */
2007 select_singlestep_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
2009 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
2010 struct thread_info
*thread
= get_lwp_thread (lp
);
2012 if (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2013 && thread
->last_resume_kind
== resume_step
2014 && lp
->status_pending_p
)
2020 /* Select the Nth LWP that has had a SIGTRAP event that should be
2024 select_event_lwp_callback (struct inferior_list_entry
*entry
, void *data
)
2026 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
2027 struct thread_info
*thread
= get_lwp_thread (lp
);
2028 int *selector
= data
;
2030 gdb_assert (selector
!= NULL
);
2032 /* Select only resumed LWPs that have a SIGTRAP event pending. */
2033 if (thread
->last_resume_kind
!= resume_stop
2034 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2035 && lp
->status_pending_p
2036 && WIFSTOPPED (lp
->status_pending
)
2037 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
2038 && !breakpoint_inserted_here (lp
->stop_pc
))
2039 if ((*selector
)-- == 0)
2046 cancel_breakpoints_callback (struct inferior_list_entry
*entry
, void *data
)
2048 struct lwp_info
*lp
= (struct lwp_info
*) entry
;
2049 struct thread_info
*thread
= get_lwp_thread (lp
);
2050 struct lwp_info
*event_lp
= data
;
2052 /* Leave the LWP that has been elected to receive a SIGTRAP alone. */
2056 /* If a LWP other than the LWP that we're reporting an event for has
2057 hit a GDB breakpoint (as opposed to some random trap signal),
2058 then just arrange for it to hit it again later. We don't keep
2059 the SIGTRAP status and don't forward the SIGTRAP signal to the
2060 LWP. We will handle the current event, eventually we will resume
2061 all LWPs, and this one will get its breakpoint trap again.
2063 If we do not do this, then we run the risk that the user will
2064 delete or disable the breakpoint, but the LWP will have already
2067 if (thread
->last_resume_kind
!= resume_stop
2068 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2069 && lp
->status_pending_p
2070 && WIFSTOPPED (lp
->status_pending
)
2071 && WSTOPSIG (lp
->status_pending
) == SIGTRAP
2073 && !lp
->stopped_by_watchpoint
2074 && cancel_breakpoint (lp
))
2075 /* Throw away the SIGTRAP. */
2076 lp
->status_pending_p
= 0;
2082 linux_cancel_breakpoints (void)
2084 find_inferior (&all_lwps
, cancel_breakpoints_callback
, NULL
);
2087 /* Select one LWP out of those that have events pending. */
2090 select_event_lwp (struct lwp_info
**orig_lp
)
2093 int random_selector
;
2094 struct lwp_info
*event_lp
;
2096 /* Give preference to any LWP that is being single-stepped. */
2098 = (struct lwp_info
*) find_inferior (&all_lwps
,
2099 select_singlestep_lwp_callback
, NULL
);
2100 if (event_lp
!= NULL
)
2104 "SEL: Select single-step %s\n",
2105 target_pid_to_str (ptid_of (event_lp
)));
2109 /* No single-stepping LWP. Select one at random, out of those
2110 which have had SIGTRAP events. */
2112 /* First see how many SIGTRAP events we have. */
2113 find_inferior (&all_lwps
, count_events_callback
, &num_events
);
2115 /* Now randomly pick a LWP out of those that have had a SIGTRAP. */
2116 random_selector
= (int)
2117 ((num_events
* (double) rand ()) / (RAND_MAX
+ 1.0));
2119 if (debug_threads
&& num_events
> 1)
2121 "SEL: Found %d SIGTRAP events, selecting #%d\n",
2122 num_events
, random_selector
);
2124 event_lp
= (struct lwp_info
*) find_inferior (&all_lwps
,
2125 select_event_lwp_callback
,
2129 if (event_lp
!= NULL
)
2131 /* Switch the event LWP. */
2132 *orig_lp
= event_lp
;
2136 /* Decrement the suspend count of an LWP. */
2139 unsuspend_one_lwp (struct inferior_list_entry
*entry
, void *except
)
2141 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2143 /* Ignore EXCEPT. */
2149 gdb_assert (lwp
->suspended
>= 0);
2153 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2157 unsuspend_all_lwps (struct lwp_info
*except
)
2159 find_inferior (&all_lwps
, unsuspend_one_lwp
, except
);
2162 static void move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
);
2163 static int stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
,
2165 static int lwp_running (struct inferior_list_entry
*entry
, void *data
);
2166 static ptid_t
linux_wait_1 (ptid_t ptid
,
2167 struct target_waitstatus
*ourstatus
,
2168 int target_options
);
2170 /* Stabilize threads (move out of jump pads).
2172 If a thread is midway collecting a fast tracepoint, we need to
2173 finish the collection and move it out of the jump pad before
2174 reporting the signal.
2176 This avoids recursion while collecting (when a signal arrives
2177 midway, and the signal handler itself collects), which would trash
2178 the trace buffer. In case the user set a breakpoint in a signal
2179 handler, this avoids the backtrace showing the jump pad, etc..
2180 Most importantly, there are certain things we can't do safely if
2181 threads are stopped in a jump pad (or in its callee's). For
2184 - starting a new trace run. A thread still collecting the
2185 previous run, could trash the trace buffer when resumed. The trace
2186 buffer control structures would have been reset but the thread had
2187 no way to tell. The thread could even midway memcpy'ing to the
2188 buffer, which would mean that when resumed, it would clobber the
2189 trace buffer that had been set for a new run.
2191 - we can't rewrite/reuse the jump pads for new tracepoints
2192 safely. Say you do tstart while a thread is stopped midway while
2193 collecting. When the thread is later resumed, it finishes the
2194 collection, and returns to the jump pad, to execute the original
2195 instruction that was under the tracepoint jump at the time the
2196 older run had been started. If the jump pad had been rewritten
2197 since for something else in the new run, the thread would now
2198 execute the wrong / random instructions. */
2201 linux_stabilize_threads (void)
2203 struct thread_info
*save_inferior
;
2204 struct lwp_info
*lwp_stuck
;
2207 = (struct lwp_info
*) find_inferior (&all_lwps
,
2208 stuck_in_jump_pad_callback
, NULL
);
2209 if (lwp_stuck
!= NULL
)
2212 fprintf (stderr
, "can't stabilize, LWP %ld is stuck in jump pad\n",
2213 lwpid_of (lwp_stuck
));
2217 save_inferior
= current_inferior
;
2219 stabilizing_threads
= 1;
2222 for_each_inferior (&all_lwps
, move_out_of_jump_pad_callback
);
2224 /* Loop until all are stopped out of the jump pads. */
2225 while (find_inferior (&all_lwps
, lwp_running
, NULL
) != NULL
)
2227 struct target_waitstatus ourstatus
;
2228 struct lwp_info
*lwp
;
2231 /* Note that we go through the full wait even loop. While
2232 moving threads out of jump pad, we need to be able to step
2233 over internal breakpoints and such. */
2234 linux_wait_1 (minus_one_ptid
, &ourstatus
, 0);
2236 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
2238 lwp
= get_thread_lwp (current_inferior
);
2243 if (ourstatus
.value
.sig
!= TARGET_SIGNAL_0
2244 || current_inferior
->last_resume_kind
== resume_stop
)
2246 wstat
= W_STOPCODE (target_signal_to_host (ourstatus
.value
.sig
));
2247 enqueue_one_deferred_signal (lwp
, &wstat
);
2252 find_inferior (&all_lwps
, unsuspend_one_lwp
, NULL
);
2254 stabilizing_threads
= 0;
2256 current_inferior
= save_inferior
;
2261 = (struct lwp_info
*) find_inferior (&all_lwps
,
2262 stuck_in_jump_pad_callback
, NULL
);
2263 if (lwp_stuck
!= NULL
)
2264 fprintf (stderr
, "couldn't stabilize, LWP %ld got stuck in jump pad\n",
2265 lwpid_of (lwp_stuck
));
2269 /* Wait for process, returns status. */
2272 linux_wait_1 (ptid_t ptid
,
2273 struct target_waitstatus
*ourstatus
, int target_options
)
2276 struct lwp_info
*event_child
;
2279 int step_over_finished
;
2280 int bp_explains_trap
;
2281 int maybe_internal_trap
;
2285 /* Translate generic target options into linux options. */
2287 if (target_options
& TARGET_WNOHANG
)
2291 bp_explains_trap
= 0;
2293 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2295 /* If we were only supposed to resume one thread, only wait for
2296 that thread - if it's still alive. If it died, however - which
2297 can happen if we're coming from the thread death case below -
2298 then we need to make sure we restart the other threads. We could
2299 pick a thread at random or restart all; restarting all is less
2302 && !ptid_equal (cont_thread
, null_ptid
)
2303 && !ptid_equal (cont_thread
, minus_one_ptid
))
2305 struct thread_info
*thread
;
2307 thread
= (struct thread_info
*) find_inferior_id (&all_threads
,
2310 /* No stepping, no signal - unless one is pending already, of course. */
2313 struct thread_resume resume_info
;
2314 resume_info
.thread
= minus_one_ptid
;
2315 resume_info
.kind
= resume_continue
;
2316 resume_info
.sig
= 0;
2317 linux_resume (&resume_info
, 1);
2323 if (ptid_equal (step_over_bkpt
, null_ptid
))
2324 pid
= linux_wait_for_event (ptid
, &w
, options
);
2328 fprintf (stderr
, "step_over_bkpt set [%s], doing a blocking wait\n",
2329 target_pid_to_str (step_over_bkpt
));
2330 pid
= linux_wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
2333 if (pid
== 0) /* only if TARGET_WNOHANG */
2336 event_child
= get_thread_lwp (current_inferior
);
2338 /* If we are waiting for a particular child, and it exited,
2339 linux_wait_for_event will return its exit status. Similarly if
2340 the last child exited. If this is not the last child, however,
2341 do not report it as exited until there is a 'thread exited' response
2342 available in the remote protocol. Instead, just wait for another event.
2343 This should be safe, because if the thread crashed we will already
2344 have reported the termination signal to GDB; that should stop any
2345 in-progress stepping operations, etc.
2347 Report the exit status of the last thread to exit. This matches
2348 LinuxThreads' behavior. */
2350 if (last_thread_of_process_p (current_inferior
))
2352 if (WIFEXITED (w
) || WIFSIGNALED (w
))
2356 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
2357 ourstatus
->value
.integer
= WEXITSTATUS (w
);
2361 "\nChild exited with retcode = %x \n",
2366 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
2367 ourstatus
->value
.sig
= target_signal_from_host (WTERMSIG (w
));
2371 "\nChild terminated with signal = %x \n",
2376 return ptid_of (event_child
);
2381 if (!WIFSTOPPED (w
))
2385 /* If this event was not handled before, and is not a SIGTRAP, we
2386 report it. SIGILL and SIGSEGV are also treated as traps in case
2387 a breakpoint is inserted at the current PC. If this target does
2388 not support internal breakpoints at all, we also report the
2389 SIGTRAP without further processing; it's of no concern to us. */
2391 = (supports_breakpoints ()
2392 && (WSTOPSIG (w
) == SIGTRAP
2393 || ((WSTOPSIG (w
) == SIGILL
2394 || WSTOPSIG (w
) == SIGSEGV
)
2395 && (*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))));
2397 if (maybe_internal_trap
)
2399 /* Handle anything that requires bookkeeping before deciding to
2400 report the event or continue waiting. */
2402 /* First check if we can explain the SIGTRAP with an internal
2403 breakpoint, or if we should possibly report the event to GDB.
2404 Do this before anything that may remove or insert a
2406 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
2408 /* We have a SIGTRAP, possibly a step-over dance has just
2409 finished. If so, tweak the state machine accordingly,
2410 reinsert breakpoints and delete any reinsert (software
2411 single-step) breakpoints. */
2412 step_over_finished
= finish_step_over (event_child
);
2414 /* Now invoke the callbacks of any internal breakpoints there. */
2415 check_breakpoints (event_child
->stop_pc
);
2417 /* Handle tracepoint data collecting. This may overflow the
2418 trace buffer, and cause a tracing stop, removing
2420 trace_event
= handle_tracepoints (event_child
);
2422 if (bp_explains_trap
)
2424 /* If we stepped or ran into an internal breakpoint, we've
2425 already handled it. So next time we resume (from this
2426 PC), we should step over it. */
2428 fprintf (stderr
, "Hit a gdbserver breakpoint.\n");
2430 if (breakpoint_here (event_child
->stop_pc
))
2431 event_child
->need_step_over
= 1;
2436 /* We have some other signal, possibly a step-over dance was in
2437 progress, and it should be cancelled too. */
2438 step_over_finished
= finish_step_over (event_child
);
2441 /* We have all the data we need. Either report the event to GDB, or
2442 resume threads and keep waiting for more. */
2444 /* If we're collecting a fast tracepoint, finish the collection and
2445 move out of the jump pad before delivering a signal. See
2446 linux_stabilize_threads. */
2449 && WSTOPSIG (w
) != SIGTRAP
2450 && supports_fast_tracepoints ()
2451 && agent_loaded_p ())
2455 "Got signal %d for LWP %ld. Check if we need "
2456 "to defer or adjust it.\n",
2457 WSTOPSIG (w
), lwpid_of (event_child
));
2459 /* Allow debugging the jump pad itself. */
2460 if (current_inferior
->last_resume_kind
!= resume_step
2461 && maybe_move_out_of_jump_pad (event_child
, &w
))
2463 enqueue_one_deferred_signal (event_child
, &w
);
2467 "Signal %d for LWP %ld deferred (in jump pad)\n",
2468 WSTOPSIG (w
), lwpid_of (event_child
));
2470 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
2475 if (event_child
->collecting_fast_tracepoint
)
2479 LWP %ld was trying to move out of the jump pad (%d). \
2480 Check if we're already there.\n",
2481 lwpid_of (event_child
),
2482 event_child
->collecting_fast_tracepoint
);
2486 event_child
->collecting_fast_tracepoint
2487 = linux_fast_tracepoint_collecting (event_child
, NULL
);
2489 if (event_child
->collecting_fast_tracepoint
!= 1)
2491 /* No longer need this breakpoint. */
2492 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
2496 "No longer need exit-jump-pad bkpt; removing it."
2497 "stopping all threads momentarily.\n");
2499 /* Other running threads could hit this breakpoint.
2500 We don't handle moribund locations like GDB does,
2501 instead we always pause all threads when removing
2502 breakpoints, so that any step-over or
2503 decr_pc_after_break adjustment is always taken
2504 care of while the breakpoint is still
2506 stop_all_lwps (1, event_child
);
2507 cancel_breakpoints ();
2509 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
2510 event_child
->exit_jump_pad_bkpt
= NULL
;
2512 unstop_all_lwps (1, event_child
);
2514 gdb_assert (event_child
->suspended
>= 0);
2518 if (event_child
->collecting_fast_tracepoint
== 0)
2522 "fast tracepoint finished "
2523 "collecting successfully.\n");
2525 /* We may have a deferred signal to report. */
2526 if (dequeue_one_deferred_signal (event_child
, &w
))
2529 fprintf (stderr
, "dequeued one signal.\n");
2534 fprintf (stderr
, "no deferred signals.\n");
2536 if (stabilizing_threads
)
2538 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
2539 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
2540 return ptid_of (event_child
);
2546 /* Check whether GDB would be interested in this event. */
2548 /* If GDB is not interested in this signal, don't stop other
2549 threads, and don't report it to GDB. Just resume the inferior
2550 right away. We do this for threading-related signals as well as
2551 any that GDB specifically requested we ignore. But never ignore
2552 SIGSTOP if we sent it ourselves, and do not ignore signals when
2553 stepping - they may require special handling to skip the signal
2555 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
2558 && current_inferior
->last_resume_kind
!= resume_step
2560 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
2561 (current_process ()->private->thread_db
!= NULL
2562 && (WSTOPSIG (w
) == __SIGRTMIN
2563 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
2566 (pass_signals
[target_signal_from_host (WSTOPSIG (w
))]
2567 && !(WSTOPSIG (w
) == SIGSTOP
2568 && current_inferior
->last_resume_kind
== resume_stop
))))
2570 siginfo_t info
, *info_p
;
2573 fprintf (stderr
, "Ignored signal %d for LWP %ld.\n",
2574 WSTOPSIG (w
), lwpid_of (event_child
));
2576 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (event_child
), 0, &info
) == 0)
2580 linux_resume_one_lwp (event_child
, event_child
->stepping
,
2581 WSTOPSIG (w
), info_p
);
2585 /* If GDB wanted this thread to single step, we always want to
2586 report the SIGTRAP, and let GDB handle it. Watchpoints should
2587 always be reported. So should signals we can't explain. A
2588 SIGTRAP we can't explain could be a GDB breakpoint --- we may or
2589 not support Z0 breakpoints. If we do, we're be able to handle
2590 GDB breakpoints on top of internal breakpoints, by handling the
2591 internal breakpoint and still reporting the event to GDB. If we
2592 don't, we're out of luck, GDB won't see the breakpoint hit. */
2593 report_to_gdb
= (!maybe_internal_trap
2594 || current_inferior
->last_resume_kind
== resume_step
2595 || event_child
->stopped_by_watchpoint
2596 || (!step_over_finished
2597 && !bp_explains_trap
&& !trace_event
)
2598 || (gdb_breakpoint_here (event_child
->stop_pc
)
2599 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)));
2601 /* We found no reason GDB would want us to stop. We either hit one
2602 of our own breakpoints, or finished an internal step GDB
2603 shouldn't know about. */
2608 if (bp_explains_trap
)
2609 fprintf (stderr
, "Hit a gdbserver breakpoint.\n");
2610 if (step_over_finished
)
2611 fprintf (stderr
, "Step-over finished.\n");
2613 fprintf (stderr
, "Tracepoint event.\n");
2616 /* We're not reporting this breakpoint to GDB, so apply the
2617 decr_pc_after_break adjustment to the inferior's regcache
2620 if (the_low_target
.set_pc
!= NULL
)
2622 struct regcache
*regcache
2623 = get_thread_regcache (get_lwp_thread (event_child
), 1);
2624 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
2627 /* We may have finished stepping over a breakpoint. If so,
2628 we've stopped and suspended all LWPs momentarily except the
2629 stepping one. This is where we resume them all again. We're
2630 going to keep waiting, so use proceed, which handles stepping
2631 over the next breakpoint. */
2633 fprintf (stderr
, "proceeding all threads.\n");
2635 if (step_over_finished
)
2636 unsuspend_all_lwps (event_child
);
2638 proceed_all_lwps ();
2644 if (current_inferior
->last_resume_kind
== resume_step
)
2645 fprintf (stderr
, "GDB wanted to single-step, reporting event.\n");
2646 if (event_child
->stopped_by_watchpoint
)
2647 fprintf (stderr
, "Stopped by watchpoint.\n");
2648 if (gdb_breakpoint_here (event_child
->stop_pc
))
2649 fprintf (stderr
, "Stopped by GDB breakpoint.\n");
2651 fprintf (stderr
, "Hit a non-gdbserver trap event.\n");
2654 /* Alright, we're going to report a stop. */
2656 if (!non_stop
&& !stabilizing_threads
)
2658 /* In all-stop, stop all threads. */
2659 stop_all_lwps (0, NULL
);
2661 /* If we're not waiting for a specific LWP, choose an event LWP
2662 from among those that have had events. Giving equal priority
2663 to all LWPs that have had events helps prevent
2665 if (ptid_equal (ptid
, minus_one_ptid
))
2667 event_child
->status_pending_p
= 1;
2668 event_child
->status_pending
= w
;
2670 select_event_lwp (&event_child
);
2672 event_child
->status_pending_p
= 0;
2673 w
= event_child
->status_pending
;
2676 /* Now that we've selected our final event LWP, cancel any
2677 breakpoints in other LWPs that have hit a GDB breakpoint.
2678 See the comment in cancel_breakpoints_callback to find out
2680 find_inferior (&all_lwps
, cancel_breakpoints_callback
, event_child
);
2682 /* If we were going a step-over, all other threads but the stepping one
2683 had been paused in start_step_over, with their suspend counts
2684 incremented. We don't want to do a full unstop/unpause, because we're
2685 in all-stop mode (so we want threads stopped), but we still need to
2686 unsuspend the other threads, to decrement their `suspended' count
2688 if (step_over_finished
)
2689 unsuspend_all_lwps (event_child
);
2691 /* Stabilize threads (move out of jump pads). */
2692 stabilize_threads ();
2696 /* If we just finished a step-over, then all threads had been
2697 momentarily paused. In all-stop, that's fine, we want
2698 threads stopped by now anyway. In non-stop, we need to
2699 re-resume threads that GDB wanted to be running. */
2700 if (step_over_finished
)
2701 unstop_all_lwps (1, event_child
);
2704 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
2706 if (current_inferior
->last_resume_kind
== resume_stop
2707 && WSTOPSIG (w
) == SIGSTOP
)
2709 /* A thread that has been requested to stop by GDB with vCont;t,
2710 and it stopped cleanly, so report as SIG0. The use of
2711 SIGSTOP is an implementation detail. */
2712 ourstatus
->value
.sig
= TARGET_SIGNAL_0
;
2714 else if (current_inferior
->last_resume_kind
== resume_stop
2715 && WSTOPSIG (w
) != SIGSTOP
)
2717 /* A thread that has been requested to stop by GDB with vCont;t,
2718 but, it stopped for other reasons. */
2719 ourstatus
->value
.sig
= target_signal_from_host (WSTOPSIG (w
));
2723 ourstatus
->value
.sig
= target_signal_from_host (WSTOPSIG (w
));
2726 gdb_assert (ptid_equal (step_over_bkpt
, null_ptid
));
2729 fprintf (stderr
, "linux_wait ret = %s, %d, %d\n",
2730 target_pid_to_str (ptid_of (event_child
)),
2732 ourstatus
->value
.sig
);
2734 return ptid_of (event_child
);
2737 /* Get rid of any pending event in the pipe. */
2739 async_file_flush (void)
2745 ret
= read (linux_event_pipe
[0], &buf
, 1);
2746 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
2749 /* Put something in the pipe, so the event loop wakes up. */
2751 async_file_mark (void)
2755 async_file_flush ();
2758 ret
= write (linux_event_pipe
[1], "+", 1);
2759 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
2761 /* Ignore EAGAIN. If the pipe is full, the event loop will already
2762 be awakened anyway. */
2766 linux_wait (ptid_t ptid
,
2767 struct target_waitstatus
*ourstatus
, int target_options
)
2772 fprintf (stderr
, "linux_wait: [%s]\n", target_pid_to_str (ptid
));
2774 /* Flush the async file first. */
2775 if (target_is_async_p ())
2776 async_file_flush ();
2778 event_ptid
= linux_wait_1 (ptid
, ourstatus
, target_options
);
2780 /* If at least one stop was reported, there may be more. A single
2781 SIGCHLD can signal more than one child stop. */
2782 if (target_is_async_p ()
2783 && (target_options
& TARGET_WNOHANG
) != 0
2784 && !ptid_equal (event_ptid
, null_ptid
))
2790 /* Send a signal to an LWP. */
2793 kill_lwp (unsigned long lwpid
, int signo
)
2795 /* Use tkill, if possible, in case we are using nptl threads. If tkill
2796 fails, then we are not using nptl threads and we should be using kill. */
2800 static int tkill_failed
;
2807 ret
= syscall (__NR_tkill
, lwpid
, signo
);
2808 if (errno
!= ENOSYS
)
2815 return kill (lwpid
, signo
);
2819 linux_stop_lwp (struct lwp_info
*lwp
)
2825 send_sigstop (struct lwp_info
*lwp
)
2829 pid
= lwpid_of (lwp
);
2831 /* If we already have a pending stop signal for this process, don't
2833 if (lwp
->stop_expected
)
2836 fprintf (stderr
, "Have pending sigstop for lwp %d\n", pid
);
2842 fprintf (stderr
, "Sending sigstop to lwp %d\n", pid
);
2844 lwp
->stop_expected
= 1;
2845 kill_lwp (pid
, SIGSTOP
);
2849 send_sigstop_callback (struct inferior_list_entry
*entry
, void *except
)
2851 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2853 /* Ignore EXCEPT. */
2864 /* Increment the suspend count of an LWP, and stop it, if not stopped
2867 suspend_and_send_sigstop_callback (struct inferior_list_entry
*entry
,
2870 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2872 /* Ignore EXCEPT. */
2878 return send_sigstop_callback (entry
, except
);
2882 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
2884 /* It's dead, really. */
2887 /* Store the exit status for later. */
2888 lwp
->status_pending_p
= 1;
2889 lwp
->status_pending
= wstat
;
2891 /* Prevent trying to stop it. */
2894 /* No further stops are expected from a dead lwp. */
2895 lwp
->stop_expected
= 0;
2899 wait_for_sigstop (struct inferior_list_entry
*entry
)
2901 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2902 struct thread_info
*saved_inferior
;
2911 fprintf (stderr
, "wait_for_sigstop: LWP %ld already stopped\n",
2916 saved_inferior
= current_inferior
;
2917 if (saved_inferior
!= NULL
)
2918 saved_tid
= ((struct inferior_list_entry
*) saved_inferior
)->id
;
2920 saved_tid
= null_ptid
; /* avoid bogus unused warning */
2922 ptid
= lwp
->head
.id
;
2925 fprintf (stderr
, "wait_for_sigstop: pulling one event\n");
2927 pid
= linux_wait_for_event (ptid
, &wstat
, __WALL
);
2929 /* If we stopped with a non-SIGSTOP signal, save it for later
2930 and record the pending SIGSTOP. If the process exited, just
2932 if (WIFSTOPPED (wstat
))
2935 fprintf (stderr
, "LWP %ld stopped with signal %d\n",
2936 lwpid_of (lwp
), WSTOPSIG (wstat
));
2938 if (WSTOPSIG (wstat
) != SIGSTOP
)
2941 fprintf (stderr
, "LWP %ld stopped with non-sigstop status %06x\n",
2942 lwpid_of (lwp
), wstat
);
2944 lwp
->status_pending_p
= 1;
2945 lwp
->status_pending
= wstat
;
2951 fprintf (stderr
, "Process %d exited while stopping LWPs\n", pid
);
2953 lwp
= find_lwp_pid (pid_to_ptid (pid
));
2956 /* Leave this status pending for the next time we're able to
2957 report it. In the mean time, we'll report this lwp as
2958 dead to GDB, so GDB doesn't try to read registers and
2959 memory from it. This can only happen if this was the
2960 last thread of the process; otherwise, PID is removed
2961 from the thread tables before linux_wait_for_event
2963 mark_lwp_dead (lwp
, wstat
);
2967 if (saved_inferior
== NULL
|| linux_thread_alive (saved_tid
))
2968 current_inferior
= saved_inferior
;
2972 fprintf (stderr
, "Previously current thread died.\n");
2976 /* We can't change the current inferior behind GDB's back,
2977 otherwise, a subsequent command may apply to the wrong
2979 current_inferior
= NULL
;
2983 /* Set a valid thread as current. */
2984 set_desired_inferior (0);
2989 /* Returns true if LWP ENTRY is stopped in a jump pad, and we can't
2990 move it out, because we need to report the stop event to GDB. For
2991 example, if the user puts a breakpoint in the jump pad, it's
2992 because she wants to debug it. */
2995 stuck_in_jump_pad_callback (struct inferior_list_entry
*entry
, void *data
)
2997 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
2998 struct thread_info
*thread
= get_lwp_thread (lwp
);
3000 gdb_assert (lwp
->suspended
== 0);
3001 gdb_assert (lwp
->stopped
);
3003 /* Allow debugging the jump pad, gdb_collect, etc.. */
3004 return (supports_fast_tracepoints ()
3005 && agent_loaded_p ()
3006 && (gdb_breakpoint_here (lwp
->stop_pc
)
3007 || lwp
->stopped_by_watchpoint
3008 || thread
->last_resume_kind
== resume_step
)
3009 && linux_fast_tracepoint_collecting (lwp
, NULL
));
3013 move_out_of_jump_pad_callback (struct inferior_list_entry
*entry
)
3015 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3016 struct thread_info
*thread
= get_lwp_thread (lwp
);
3019 gdb_assert (lwp
->suspended
== 0);
3020 gdb_assert (lwp
->stopped
);
3022 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
3024 /* Allow debugging the jump pad, gdb_collect, etc. */
3025 if (!gdb_breakpoint_here (lwp
->stop_pc
)
3026 && !lwp
->stopped_by_watchpoint
3027 && thread
->last_resume_kind
!= resume_step
3028 && maybe_move_out_of_jump_pad (lwp
, wstat
))
3032 "LWP %ld needs stabilizing (in jump pad)\n",
3037 lwp
->status_pending_p
= 0;
3038 enqueue_one_deferred_signal (lwp
, wstat
);
3042 "Signal %d for LWP %ld deferred "
3044 WSTOPSIG (*wstat
), lwpid_of (lwp
));
3047 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
3054 lwp_running (struct inferior_list_entry
*entry
, void *data
)
3056 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3065 /* Stop all lwps that aren't stopped yet, except EXCEPT, if not NULL.
3066 If SUSPEND, then also increase the suspend count of every LWP,
3070 stop_all_lwps (int suspend
, struct lwp_info
*except
)
3072 stopping_threads
= 1;
3075 find_inferior (&all_lwps
, suspend_and_send_sigstop_callback
, except
);
3077 find_inferior (&all_lwps
, send_sigstop_callback
, except
);
3078 for_each_inferior (&all_lwps
, wait_for_sigstop
);
3079 stopping_threads
= 0;
3082 /* Resume execution of the inferior process.
3083 If STEP is nonzero, single-step it.
3084 If SIGNAL is nonzero, give it that signal. */
3087 linux_resume_one_lwp (struct lwp_info
*lwp
,
3088 int step
, int signal
, siginfo_t
*info
)
3090 struct thread_info
*saved_inferior
;
3091 int fast_tp_collecting
;
3093 if (lwp
->stopped
== 0)
3096 fast_tp_collecting
= lwp
->collecting_fast_tracepoint
;
3098 gdb_assert (!stabilizing_threads
|| fast_tp_collecting
);
3100 /* Cancel actions that rely on GDB not changing the PC (e.g., the
3101 user used the "jump" command, or "set $pc = foo"). */
3102 if (lwp
->stop_pc
!= get_pc (lwp
))
3104 /* Collecting 'while-stepping' actions doesn't make sense
3106 release_while_stepping_state_list (get_lwp_thread (lwp
));
3109 /* If we have pending signals or status, and a new signal, enqueue the
3110 signal. Also enqueue the signal if we are waiting to reinsert a
3111 breakpoint; it will be picked up again below. */
3113 && (lwp
->status_pending_p
3114 || lwp
->pending_signals
!= NULL
3115 || lwp
->bp_reinsert
!= 0
3116 || fast_tp_collecting
))
3118 struct pending_signals
*p_sig
;
3119 p_sig
= xmalloc (sizeof (*p_sig
));
3120 p_sig
->prev
= lwp
->pending_signals
;
3121 p_sig
->signal
= signal
;
3123 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
3125 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
3126 lwp
->pending_signals
= p_sig
;
3129 if (lwp
->status_pending_p
)
3132 fprintf (stderr
, "Not resuming lwp %ld (%s, signal %d, stop %s);"
3133 " has pending status\n",
3134 lwpid_of (lwp
), step
? "step" : "continue", signal
,
3135 lwp
->stop_expected
? "expected" : "not expected");
3139 saved_inferior
= current_inferior
;
3140 current_inferior
= get_lwp_thread (lwp
);
3143 fprintf (stderr
, "Resuming lwp %ld (%s, signal %d, stop %s)\n",
3144 lwpid_of (lwp
), step
? "step" : "continue", signal
,
3145 lwp
->stop_expected
? "expected" : "not expected");
3147 /* This bit needs some thinking about. If we get a signal that
3148 we must report while a single-step reinsert is still pending,
3149 we often end up resuming the thread. It might be better to
3150 (ew) allow a stack of pending events; then we could be sure that
3151 the reinsert happened right away and not lose any signals.
3153 Making this stack would also shrink the window in which breakpoints are
3154 uninserted (see comment in linux_wait_for_lwp) but not enough for
3155 complete correctness, so it won't solve that problem. It may be
3156 worthwhile just to solve this one, however. */
3157 if (lwp
->bp_reinsert
!= 0)
3160 fprintf (stderr
, " pending reinsert at 0x%s\n",
3161 paddress (lwp
->bp_reinsert
));
3163 if (lwp
->bp_reinsert
!= 0 && can_hardware_single_step ())
3165 if (fast_tp_collecting
== 0)
3168 fprintf (stderr
, "BAD - reinserting but not stepping.\n");
3170 fprintf (stderr
, "BAD - reinserting and suspended(%d).\n",
3177 /* Postpone any pending signal. It was enqueued above. */
3181 if (fast_tp_collecting
== 1)
3185 lwp %ld wants to get out of fast tracepoint jump pad (exit-jump-pad-bkpt)\n",
3188 /* Postpone any pending signal. It was enqueued above. */
3191 else if (fast_tp_collecting
== 2)
3195 lwp %ld wants to get out of fast tracepoint jump pad single-stepping\n",
3198 if (can_hardware_single_step ())
3201 fatal ("moving out of jump pad single-stepping"
3202 " not implemented on this target");
3204 /* Postpone any pending signal. It was enqueued above. */
3208 /* If we have while-stepping actions in this thread set it stepping.
3209 If we have a signal to deliver, it may or may not be set to
3210 SIG_IGN, we don't know. Assume so, and allow collecting
3211 while-stepping into a signal handler. A possible smart thing to
3212 do would be to set an internal breakpoint at the signal return
3213 address, continue, and carry on catching this while-stepping
3214 action only when that breakpoint is hit. A future
3216 if (get_lwp_thread (lwp
)->while_stepping
!= NULL
3217 && can_hardware_single_step ())
3221 "lwp %ld has a while-stepping action -> forcing step.\n",
3226 if (debug_threads
&& the_low_target
.get_pc
!= NULL
)
3228 struct regcache
*regcache
= get_thread_regcache (current_inferior
, 1);
3229 CORE_ADDR pc
= (*the_low_target
.get_pc
) (regcache
);
3230 fprintf (stderr
, " resuming from pc 0x%lx\n", (long) pc
);
3233 /* If we have pending signals, consume one unless we are trying to
3234 reinsert a breakpoint or we're trying to finish a fast tracepoint
3236 if (lwp
->pending_signals
!= NULL
3237 && lwp
->bp_reinsert
== 0
3238 && fast_tp_collecting
== 0)
3240 struct pending_signals
**p_sig
;
3242 p_sig
= &lwp
->pending_signals
;
3243 while ((*p_sig
)->prev
!= NULL
)
3244 p_sig
= &(*p_sig
)->prev
;
3246 signal
= (*p_sig
)->signal
;
3247 if ((*p_sig
)->info
.si_signo
!= 0)
3248 ptrace (PTRACE_SETSIGINFO
, lwpid_of (lwp
), 0, &(*p_sig
)->info
);
3254 if (the_low_target
.prepare_to_resume
!= NULL
)
3255 the_low_target
.prepare_to_resume (lwp
);
3257 regcache_invalidate_one ((struct inferior_list_entry
*)
3258 get_lwp_thread (lwp
));
3261 lwp
->stopped_by_watchpoint
= 0;
3262 lwp
->stepping
= step
;
3263 ptrace (step
? PTRACE_SINGLESTEP
: PTRACE_CONT
, lwpid_of (lwp
), 0,
3264 /* Coerce to a uintptr_t first to avoid potential gcc warning
3265 of coercing an 8 byte integer to a 4 byte pointer. */
3266 (PTRACE_ARG4_TYPE
) (uintptr_t) signal
);
3268 current_inferior
= saved_inferior
;
3271 /* ESRCH from ptrace either means that the thread was already
3272 running (an error) or that it is gone (a race condition). If
3273 it's gone, we will get a notification the next time we wait,
3274 so we can ignore the error. We could differentiate these
3275 two, but it's tricky without waiting; the thread still exists
3276 as a zombie, so sending it signal 0 would succeed. So just
3281 perror_with_name ("ptrace");
3285 struct thread_resume_array
3287 struct thread_resume
*resume
;
3291 /* This function is called once per thread. We look up the thread
3292 in RESUME_PTR, and mark the thread with a pointer to the appropriate
3295 This algorithm is O(threads * resume elements), but resume elements
3296 is small (and will remain small at least until GDB supports thread
3299 linux_set_resume_request (struct inferior_list_entry
*entry
, void *arg
)
3301 struct lwp_info
*lwp
;
3302 struct thread_info
*thread
;
3304 struct thread_resume_array
*r
;
3306 thread
= (struct thread_info
*) entry
;
3307 lwp
= get_thread_lwp (thread
);
3310 for (ndx
= 0; ndx
< r
->n
; ndx
++)
3312 ptid_t ptid
= r
->resume
[ndx
].thread
;
3313 if (ptid_equal (ptid
, minus_one_ptid
)
3314 || ptid_equal (ptid
, entry
->id
)
3315 || (ptid_is_pid (ptid
)
3316 && (ptid_get_pid (ptid
) == pid_of (lwp
)))
3317 || (ptid_get_lwp (ptid
) == -1
3318 && (ptid_get_pid (ptid
) == pid_of (lwp
))))
3320 if (r
->resume
[ndx
].kind
== resume_stop
3321 && thread
->last_resume_kind
== resume_stop
)
3324 fprintf (stderr
, "already %s LWP %ld at GDB's request\n",
3325 thread
->last_status
.kind
== TARGET_WAITKIND_STOPPED
3333 lwp
->resume
= &r
->resume
[ndx
];
3334 thread
->last_resume_kind
= lwp
->resume
->kind
;
3336 /* If we had a deferred signal to report, dequeue one now.
3337 This can happen if LWP gets more than one signal while
3338 trying to get out of a jump pad. */
3340 && !lwp
->status_pending_p
3341 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
3343 lwp
->status_pending_p
= 1;
3347 "Dequeueing deferred signal %d for LWP %ld, "
3348 "leaving status pending.\n",
3349 WSTOPSIG (lwp
->status_pending
), lwpid_of (lwp
));
3356 /* No resume action for this thread. */
3363 /* Set *FLAG_P if this lwp has an interesting status pending. */
3365 resume_status_pending_p (struct inferior_list_entry
*entry
, void *flag_p
)
3367 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3369 /* LWPs which will not be resumed are not interesting, because
3370 we might not wait for them next time through linux_wait. */
3371 if (lwp
->resume
== NULL
)
3374 if (lwp
->status_pending_p
)
3375 * (int *) flag_p
= 1;
3380 /* Return 1 if this lwp that GDB wants running is stopped at an
3381 internal breakpoint that we need to step over. It assumes that any
3382 required STOP_PC adjustment has already been propagated to the
3383 inferior's regcache. */
3386 need_step_over_p (struct inferior_list_entry
*entry
, void *dummy
)
3388 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3389 struct thread_info
*thread
;
3390 struct thread_info
*saved_inferior
;
3393 /* LWPs which will not be resumed are not interesting, because we
3394 might not wait for them next time through linux_wait. */
3400 "Need step over [LWP %ld]? Ignoring, not stopped\n",
3405 thread
= get_lwp_thread (lwp
);
3407 if (thread
->last_resume_kind
== resume_stop
)
3411 "Need step over [LWP %ld]? Ignoring, should remain stopped\n",
3416 gdb_assert (lwp
->suspended
>= 0);
3422 "Need step over [LWP %ld]? Ignoring, suspended\n",
3427 if (!lwp
->need_step_over
)
3431 "Need step over [LWP %ld]? No\n", lwpid_of (lwp
));
3434 if (lwp
->status_pending_p
)
3438 "Need step over [LWP %ld]? Ignoring, has pending status.\n",
3443 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
3447 /* If the PC has changed since we stopped, then don't do anything,
3448 and let the breakpoint/tracepoint be hit. This happens if, for
3449 instance, GDB handled the decr_pc_after_break subtraction itself,
3450 GDB is OOL stepping this thread, or the user has issued a "jump"
3451 command, or poked thread's registers herself. */
3452 if (pc
!= lwp
->stop_pc
)
3456 "Need step over [LWP %ld]? Cancelling, PC was changed. "
3457 "Old stop_pc was 0x%s, PC is now 0x%s\n",
3458 lwpid_of (lwp
), paddress (lwp
->stop_pc
), paddress (pc
));
3460 lwp
->need_step_over
= 0;
3464 saved_inferior
= current_inferior
;
3465 current_inferior
= thread
;
3467 /* We can only step over breakpoints we know about. */
3468 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
3470 /* Don't step over a breakpoint that GDB expects to hit
3471 though. If the condition is being evaluated on the target's side
3472 and it evaluate to false, step over this breakpoint as well. */
3473 if (gdb_breakpoint_here (pc
)
3474 && gdb_condition_true_at_breakpoint (pc
))
3478 "Need step over [LWP %ld]? yes, but found"
3479 " GDB breakpoint at 0x%s; skipping step over\n",
3480 lwpid_of (lwp
), paddress (pc
));
3482 current_inferior
= saved_inferior
;
3489 "Need step over [LWP %ld]? yes, "
3490 "found breakpoint at 0x%s\n",
3491 lwpid_of (lwp
), paddress (pc
));
3493 /* We've found an lwp that needs stepping over --- return 1 so
3494 that find_inferior stops looking. */
3495 current_inferior
= saved_inferior
;
3497 /* If the step over is cancelled, this is set again. */
3498 lwp
->need_step_over
= 0;
3503 current_inferior
= saved_inferior
;
3507 "Need step over [LWP %ld]? No, no breakpoint found at 0x%s\n",
3508 lwpid_of (lwp
), paddress (pc
));
3513 /* Start a step-over operation on LWP. When LWP stopped at a
3514 breakpoint, to make progress, we need to remove the breakpoint out
3515 of the way. If we let other threads run while we do that, they may
3516 pass by the breakpoint location and miss hitting it. To avoid
3517 that, a step-over momentarily stops all threads while LWP is
3518 single-stepped while the breakpoint is temporarily uninserted from
3519 the inferior. When the single-step finishes, we reinsert the
3520 breakpoint, and let all threads that are supposed to be running,
3523 On targets that don't support hardware single-step, we don't
3524 currently support full software single-stepping. Instead, we only
3525 support stepping over the thread event breakpoint, by asking the
3526 low target where to place a reinsert breakpoint. Since this
3527 routine assumes the breakpoint being stepped over is a thread event
3528 breakpoint, it usually assumes the return address of the current
3529 function is a good enough place to set the reinsert breakpoint. */
3532 start_step_over (struct lwp_info
*lwp
)
3534 struct thread_info
*saved_inferior
;
3540 "Starting step-over on LWP %ld. Stopping all threads\n",
3543 stop_all_lwps (1, lwp
);
3544 gdb_assert (lwp
->suspended
== 0);
3547 fprintf (stderr
, "Done stopping all threads for step-over.\n");
3549 /* Note, we should always reach here with an already adjusted PC,
3550 either by GDB (if we're resuming due to GDB's request), or by our
3551 caller, if we just finished handling an internal breakpoint GDB
3552 shouldn't care about. */
3555 saved_inferior
= current_inferior
;
3556 current_inferior
= get_lwp_thread (lwp
);
3558 lwp
->bp_reinsert
= pc
;
3559 uninsert_breakpoints_at (pc
);
3560 uninsert_fast_tracepoint_jumps_at (pc
);
3562 if (can_hardware_single_step ())
3568 CORE_ADDR raddr
= (*the_low_target
.breakpoint_reinsert_addr
) ();
3569 set_reinsert_breakpoint (raddr
);
3573 current_inferior
= saved_inferior
;
3575 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
3577 /* Require next event from this LWP. */
3578 step_over_bkpt
= lwp
->head
.id
;
3582 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
3583 start_step_over, if still there, and delete any reinsert
3584 breakpoints we've set, on non hardware single-step targets. */
3587 finish_step_over (struct lwp_info
*lwp
)
3589 if (lwp
->bp_reinsert
!= 0)
3592 fprintf (stderr
, "Finished step over.\n");
3594 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
3595 may be no breakpoint to reinsert there by now. */
3596 reinsert_breakpoints_at (lwp
->bp_reinsert
);
3597 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
3599 lwp
->bp_reinsert
= 0;
3601 /* Delete any software-single-step reinsert breakpoints. No
3602 longer needed. We don't have to worry about other threads
3603 hitting this trap, and later not being able to explain it,
3604 because we were stepping over a breakpoint, and we hold all
3605 threads but LWP stopped while doing that. */
3606 if (!can_hardware_single_step ())
3607 delete_reinsert_breakpoints ();
3609 step_over_bkpt
= null_ptid
;
3616 /* This function is called once per thread. We check the thread's resume
3617 request, which will tell us whether to resume, step, or leave the thread
3618 stopped; and what signal, if any, it should be sent.
3620 For threads which we aren't explicitly told otherwise, we preserve
3621 the stepping flag; this is used for stepping over gdbserver-placed
3624 If pending_flags was set in any thread, we queue any needed
3625 signals, since we won't actually resume. We already have a pending
3626 event to report, so we don't need to preserve any step requests;
3627 they should be re-issued if necessary. */
3630 linux_resume_one_thread (struct inferior_list_entry
*entry
, void *arg
)
3632 struct lwp_info
*lwp
;
3633 struct thread_info
*thread
;
3635 int leave_all_stopped
= * (int *) arg
;
3638 thread
= (struct thread_info
*) entry
;
3639 lwp
= get_thread_lwp (thread
);
3641 if (lwp
->resume
== NULL
)
3644 if (lwp
->resume
->kind
== resume_stop
)
3647 fprintf (stderr
, "resume_stop request for LWP %ld\n", lwpid_of (lwp
));
3652 fprintf (stderr
, "stopping LWP %ld\n", lwpid_of (lwp
));
3654 /* Stop the thread, and wait for the event asynchronously,
3655 through the event loop. */
3661 fprintf (stderr
, "already stopped LWP %ld\n",
3664 /* The LWP may have been stopped in an internal event that
3665 was not meant to be notified back to GDB (e.g., gdbserver
3666 breakpoint), so we should be reporting a stop event in
3669 /* If the thread already has a pending SIGSTOP, this is a
3670 no-op. Otherwise, something later will presumably resume
3671 the thread and this will cause it to cancel any pending
3672 operation, due to last_resume_kind == resume_stop. If
3673 the thread already has a pending status to report, we
3674 will still report it the next time we wait - see
3675 status_pending_p_callback. */
3677 /* If we already have a pending signal to report, then
3678 there's no need to queue a SIGSTOP, as this means we're
3679 midway through moving the LWP out of the jumppad, and we
3680 will report the pending signal as soon as that is
3682 if (lwp
->pending_signals_to_report
== NULL
)
3686 /* For stop requests, we're done. */
3688 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
3692 /* If this thread which is about to be resumed has a pending status,
3693 then don't resume any threads - we can just report the pending
3694 status. Make sure to queue any signals that would otherwise be
3695 sent. In all-stop mode, we do this decision based on if *any*
3696 thread has a pending status. If there's a thread that needs the
3697 step-over-breakpoint dance, then don't resume any other thread
3698 but that particular one. */
3699 leave_pending
= (lwp
->status_pending_p
|| leave_all_stopped
);
3704 fprintf (stderr
, "resuming LWP %ld\n", lwpid_of (lwp
));
3706 step
= (lwp
->resume
->kind
== resume_step
);
3707 linux_resume_one_lwp (lwp
, step
, lwp
->resume
->sig
, NULL
);
3712 fprintf (stderr
, "leaving LWP %ld stopped\n", lwpid_of (lwp
));
3714 /* If we have a new signal, enqueue the signal. */
3715 if (lwp
->resume
->sig
!= 0)
3717 struct pending_signals
*p_sig
;
3718 p_sig
= xmalloc (sizeof (*p_sig
));
3719 p_sig
->prev
= lwp
->pending_signals
;
3720 p_sig
->signal
= lwp
->resume
->sig
;
3721 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
3723 /* If this is the same signal we were previously stopped by,
3724 make sure to queue its siginfo. We can ignore the return
3725 value of ptrace; if it fails, we'll skip
3726 PTRACE_SETSIGINFO. */
3727 if (WIFSTOPPED (lwp
->last_status
)
3728 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
)
3729 ptrace (PTRACE_GETSIGINFO
, lwpid_of (lwp
), 0, &p_sig
->info
);
3731 lwp
->pending_signals
= p_sig
;
3735 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
3741 linux_resume (struct thread_resume
*resume_info
, size_t n
)
3743 struct thread_resume_array array
= { resume_info
, n
};
3744 struct lwp_info
*need_step_over
= NULL
;
3746 int leave_all_stopped
;
3748 find_inferior (&all_threads
, linux_set_resume_request
, &array
);
3750 /* If there is a thread which would otherwise be resumed, which has
3751 a pending status, then don't resume any threads - we can just
3752 report the pending status. Make sure to queue any signals that
3753 would otherwise be sent. In non-stop mode, we'll apply this
3754 logic to each thread individually. We consume all pending events
3755 before considering to start a step-over (in all-stop). */
3758 find_inferior (&all_lwps
, resume_status_pending_p
, &any_pending
);
3760 /* If there is a thread which would otherwise be resumed, which is
3761 stopped at a breakpoint that needs stepping over, then don't
3762 resume any threads - have it step over the breakpoint with all
3763 other threads stopped, then resume all threads again. Make sure
3764 to queue any signals that would otherwise be delivered or
3766 if (!any_pending
&& supports_breakpoints ())
3768 = (struct lwp_info
*) find_inferior (&all_lwps
,
3769 need_step_over_p
, NULL
);
3771 leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
3775 if (need_step_over
!= NULL
)
3776 fprintf (stderr
, "Not resuming all, need step over\n");
3777 else if (any_pending
)
3779 "Not resuming, all-stop and found "
3780 "an LWP with pending status\n");
3782 fprintf (stderr
, "Resuming, no pending status or step over needed\n");
3785 /* Even if we're leaving threads stopped, queue all signals we'd
3786 otherwise deliver. */
3787 find_inferior (&all_threads
, linux_resume_one_thread
, &leave_all_stopped
);
3790 start_step_over (need_step_over
);
3793 /* This function is called once per thread. We check the thread's
3794 last resume request, which will tell us whether to resume, step, or
3795 leave the thread stopped. Any signal the client requested to be
3796 delivered has already been enqueued at this point.
3798 If any thread that GDB wants running is stopped at an internal
3799 breakpoint that needs stepping over, we start a step-over operation
3800 on that particular thread, and leave all others stopped. */
3803 proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
3805 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3806 struct thread_info
*thread
;
3814 "proceed_one_lwp: lwp %ld\n", lwpid_of (lwp
));
3819 fprintf (stderr
, " LWP %ld already running\n", lwpid_of (lwp
));
3823 thread
= get_lwp_thread (lwp
);
3825 if (thread
->last_resume_kind
== resume_stop
3826 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
3829 fprintf (stderr
, " client wants LWP to remain %ld stopped\n",
3834 if (lwp
->status_pending_p
)
3837 fprintf (stderr
, " LWP %ld has pending status, leaving stopped\n",
3842 gdb_assert (lwp
->suspended
>= 0);
3847 fprintf (stderr
, " LWP %ld is suspended\n", lwpid_of (lwp
));
3851 if (thread
->last_resume_kind
== resume_stop
3852 && lwp
->pending_signals_to_report
== NULL
3853 && lwp
->collecting_fast_tracepoint
== 0)
3855 /* We haven't reported this LWP as stopped yet (otherwise, the
3856 last_status.kind check above would catch it, and we wouldn't
3857 reach here. This LWP may have been momentarily paused by a
3858 stop_all_lwps call while handling for example, another LWP's
3859 step-over. In that case, the pending expected SIGSTOP signal
3860 that was queued at vCont;t handling time will have already
3861 been consumed by wait_for_sigstop, and so we need to requeue
3862 another one here. Note that if the LWP already has a SIGSTOP
3863 pending, this is a no-op. */
3867 "Client wants LWP %ld to stop. "
3868 "Making sure it has a SIGSTOP pending\n",
3874 step
= thread
->last_resume_kind
== resume_step
;
3875 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
3880 unsuspend_and_proceed_one_lwp (struct inferior_list_entry
*entry
, void *except
)
3882 struct lwp_info
*lwp
= (struct lwp_info
*) entry
;
3888 gdb_assert (lwp
->suspended
>= 0);
3890 return proceed_one_lwp (entry
, except
);
3893 /* When we finish a step-over, set threads running again. If there's
3894 another thread that may need a step-over, now's the time to start
3895 it. Eventually, we'll move all threads past their breakpoints. */
3898 proceed_all_lwps (void)
3900 struct lwp_info
*need_step_over
;
3902 /* If there is a thread which would otherwise be resumed, which is
3903 stopped at a breakpoint that needs stepping over, then don't
3904 resume any threads - have it step over the breakpoint with all
3905 other threads stopped, then resume all threads again. */
3907 if (supports_breakpoints ())
3910 = (struct lwp_info
*) find_inferior (&all_lwps
,
3911 need_step_over_p
, NULL
);
3913 if (need_step_over
!= NULL
)
3916 fprintf (stderr
, "proceed_all_lwps: found "
3917 "thread %ld needing a step-over\n",
3918 lwpid_of (need_step_over
));
3920 start_step_over (need_step_over
);
3926 fprintf (stderr
, "Proceeding, no step-over needed\n");
3928 find_inferior (&all_lwps
, proceed_one_lwp
, NULL
);
3931 /* Stopped LWPs that the client wanted to be running, that don't have
3932 pending statuses, are set to run again, except for EXCEPT, if not
3933 NULL. This undoes a stop_all_lwps call. */
3936 unstop_all_lwps (int unsuspend
, struct lwp_info
*except
)
3942 "unstopping all lwps, except=(LWP %ld)\n", lwpid_of (except
));
3945 "unstopping all lwps\n");
3949 find_inferior (&all_lwps
, unsuspend_and_proceed_one_lwp
, except
);
3951 find_inferior (&all_lwps
, proceed_one_lwp
, except
);
3955 #ifdef HAVE_LINUX_REGSETS
3957 #define use_linux_regsets 1
3960 regsets_fetch_inferior_registers (struct regcache
*regcache
)
3962 struct regset_info
*regset
;
3963 int saw_general_regs
= 0;
3967 regset
= target_regsets
;
3969 pid
= lwpid_of (get_thread_lwp (current_inferior
));
3970 while (regset
->size
>= 0)
3975 if (regset
->size
== 0 || disabled_regsets
[regset
- target_regsets
])
3981 buf
= xmalloc (regset
->size
);
3983 nt_type
= regset
->nt_type
;
3987 iov
.iov_len
= regset
->size
;
3988 data
= (void *) &iov
;
3994 res
= ptrace (regset
->get_request
, pid
, nt_type
, data
);
3996 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4002 /* If we get EIO on a regset, do not try it again for
4004 disabled_regsets
[regset
- target_regsets
] = 1;
4011 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
4016 else if (regset
->type
== GENERAL_REGS
)
4017 saw_general_regs
= 1;
4018 regset
->store_function (regcache
, buf
);
4022 if (saw_general_regs
)
4029 regsets_store_inferior_registers (struct regcache
*regcache
)
4031 struct regset_info
*regset
;
4032 int saw_general_regs
= 0;
4036 regset
= target_regsets
;
4038 pid
= lwpid_of (get_thread_lwp (current_inferior
));
4039 while (regset
->size
>= 0)
4044 if (regset
->size
== 0 || disabled_regsets
[regset
- target_regsets
])
4050 buf
= xmalloc (regset
->size
);
4052 /* First fill the buffer with the current register set contents,
4053 in case there are any items in the kernel's regset that are
4054 not in gdbserver's regcache. */
4056 nt_type
= regset
->nt_type
;
4060 iov
.iov_len
= regset
->size
;
4061 data
= (void *) &iov
;
4067 res
= ptrace (regset
->get_request
, pid
, nt_type
, data
);
4069 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4074 /* Then overlay our cached registers on that. */
4075 regset
->fill_function (regcache
, buf
);
4077 /* Only now do we write the register set. */
4079 res
= ptrace (regset
->set_request
, pid
, nt_type
, data
);
4081 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
4089 /* If we get EIO on a regset, do not try it again for
4091 disabled_regsets
[regset
- target_regsets
] = 1;
4095 else if (errno
== ESRCH
)
4097 /* At this point, ESRCH should mean the process is
4098 already gone, in which case we simply ignore attempts
4099 to change its registers. See also the related
4100 comment in linux_resume_one_lwp. */
4106 perror ("Warning: ptrace(regsets_store_inferior_registers)");
4109 else if (regset
->type
== GENERAL_REGS
)
4110 saw_general_regs
= 1;
4114 if (saw_general_regs
)
4120 #else /* !HAVE_LINUX_REGSETS */
4122 #define use_linux_regsets 0
4123 #define regsets_fetch_inferior_registers(regcache) 1
4124 #define regsets_store_inferior_registers(regcache) 1
4128 /* Return 1 if register REGNO is supported by one of the regset ptrace
4129 calls or 0 if it has to be transferred individually. */
4132 linux_register_in_regsets (int regno
)
4134 unsigned char mask
= 1 << (regno
% 8);
4135 size_t index
= regno
/ 8;
4137 return (use_linux_regsets
4138 && (the_low_target
.regset_bitmap
== NULL
4139 || (the_low_target
.regset_bitmap
[index
] & mask
) != 0));
4142 #ifdef HAVE_LINUX_USRREGS
4145 register_addr (int regnum
)
4149 if (regnum
< 0 || regnum
>= the_low_target
.num_regs
)
4150 error ("Invalid register number %d.", regnum
);
4152 addr
= the_low_target
.regmap
[regnum
];
4157 /* Fetch one register. */
4159 fetch_register (struct regcache
*regcache
, int regno
)
4166 if (regno
>= the_low_target
.num_regs
)
4168 if ((*the_low_target
.cannot_fetch_register
) (regno
))
4171 regaddr
= register_addr (regno
);
4175 size
= ((register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4176 & -sizeof (PTRACE_XFER_TYPE
));
4177 buf
= alloca (size
);
4179 pid
= lwpid_of (get_thread_lwp (current_inferior
));
4180 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
4183 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
4184 ptrace (PTRACE_PEEKUSER
, pid
,
4185 /* Coerce to a uintptr_t first to avoid potential gcc warning
4186 of coercing an 8 byte integer to a 4 byte pointer. */
4187 (PTRACE_ARG3_TYPE
) (uintptr_t) regaddr
, 0);
4188 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
4190 error ("reading register %d: %s", regno
, strerror (errno
));
4193 if (the_low_target
.supply_ptrace_register
)
4194 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
4196 supply_register (regcache
, regno
, buf
);
4199 /* Store one register. */
4201 store_register (struct regcache
*regcache
, int regno
)
4208 if (regno
>= the_low_target
.num_regs
)
4210 if ((*the_low_target
.cannot_store_register
) (regno
))
4213 regaddr
= register_addr (regno
);
4217 size
= ((register_size (regno
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4218 & -sizeof (PTRACE_XFER_TYPE
));
4219 buf
= alloca (size
);
4220 memset (buf
, 0, size
);
4222 if (the_low_target
.collect_ptrace_register
)
4223 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
4225 collect_register (regcache
, regno
, buf
);
4227 pid
= lwpid_of (get_thread_lwp (current_inferior
));
4228 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
4231 ptrace (PTRACE_POKEUSER
, pid
,
4232 /* Coerce to a uintptr_t first to avoid potential gcc warning
4233 about coercing an 8 byte integer to a 4 byte pointer. */
4234 (PTRACE_ARG3_TYPE
) (uintptr_t) regaddr
,
4235 (PTRACE_ARG4_TYPE
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
4238 /* At this point, ESRCH should mean the process is
4239 already gone, in which case we simply ignore attempts
4240 to change its registers. See also the related
4241 comment in linux_resume_one_lwp. */
4245 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
4246 error ("writing register %d: %s", regno
, strerror (errno
));
4248 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
4252 /* Fetch all registers, or just one, from the child process.
4253 If REGNO is -1, do this for all registers, skipping any that are
4254 assumed to have been retrieved by regsets_fetch_inferior_registers,
4255 unless ALL is non-zero.
4256 Otherwise, REGNO specifies which register (so we can save time). */
4258 usr_fetch_inferior_registers (struct regcache
*regcache
, int regno
, int all
)
4262 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
4263 if (all
|| !linux_register_in_regsets (regno
))
4264 fetch_register (regcache
, regno
);
4267 fetch_register (regcache
, regno
);
4270 /* Store our register values back into the inferior.
4271 If REGNO is -1, do this for all registers, skipping any that are
4272 assumed to have been saved by regsets_store_inferior_registers,
4273 unless ALL is non-zero.
4274 Otherwise, REGNO specifies which register (so we can save time). */
4276 usr_store_inferior_registers (struct regcache
*regcache
, int regno
, int all
)
4280 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
4281 if (all
|| !linux_register_in_regsets (regno
))
4282 store_register (regcache
, regno
);
4285 store_register (regcache
, regno
);
4288 #else /* !HAVE_LINUX_USRREGS */
4290 #define usr_fetch_inferior_registers(regcache, regno, all) do {} while (0)
4291 #define usr_store_inferior_registers(regcache, regno, all) do {} while (0)
4297 linux_fetch_registers (struct regcache
*regcache
, int regno
)
4304 if (the_low_target
.fetch_register
!= NULL
)
4305 for (regno
= 0; regno
< the_low_target
.num_regs
; regno
++)
4306 (*the_low_target
.fetch_register
) (regcache
, regno
);
4308 all
= regsets_fetch_inferior_registers (regcache
);
4309 usr_fetch_inferior_registers (regcache
, -1, all
);
4313 if (the_low_target
.fetch_register
!= NULL
4314 && (*the_low_target
.fetch_register
) (regcache
, regno
))
4317 use_regsets
= linux_register_in_regsets (regno
);
4319 all
= regsets_fetch_inferior_registers (regcache
);
4320 if (!use_regsets
|| all
)
4321 usr_fetch_inferior_registers (regcache
, regno
, 1);
4326 linux_store_registers (struct regcache
*regcache
, int regno
)
4333 all
= regsets_store_inferior_registers (regcache
);
4334 usr_store_inferior_registers (regcache
, regno
, all
);
4338 use_regsets
= linux_register_in_regsets (regno
);
4340 all
= regsets_store_inferior_registers (regcache
);
4341 if (!use_regsets
|| all
)
4342 usr_store_inferior_registers (regcache
, regno
, 1);
4347 /* Copy LEN bytes from inferior's memory starting at MEMADDR
4348 to debugger memory starting at MYADDR. */
4351 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
4354 /* Round starting address down to longword boundary. */
4355 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
4356 /* Round ending address up; get number of longwords that makes. */
4358 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4359 / sizeof (PTRACE_XFER_TYPE
);
4360 /* Allocate buffer of that many longwords. */
4361 register PTRACE_XFER_TYPE
*buffer
4362 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
4365 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4367 /* Try using /proc. Don't bother for one word. */
4368 if (len
>= 3 * sizeof (long))
4370 /* We could keep this file open and cache it - possibly one per
4371 thread. That requires some juggling, but is even faster. */
4372 sprintf (filename
, "/proc/%d/mem", pid
);
4373 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
4377 /* If pread64 is available, use it. It's faster if the kernel
4378 supports it (only one syscall), and it's 64-bit safe even on
4379 32-bit platforms (for instance, SPARC debugging a SPARC64
4382 if (pread64 (fd
, myaddr
, len
, memaddr
) != len
)
4384 if (lseek (fd
, memaddr
, SEEK_SET
) == -1 || read (fd
, myaddr
, len
) != len
)
4396 /* Read all the longwords */
4397 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
4400 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4401 about coercing an 8 byte integer to a 4 byte pointer. */
4402 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
4403 (PTRACE_ARG3_TYPE
) (uintptr_t) addr
, 0);
4408 /* Copy appropriate bytes out of the buffer. */
4410 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
4416 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
4417 memory at MEMADDR. On failure (cannot write to the inferior)
4418 returns the value of errno. */
4421 linux_write_memory (CORE_ADDR memaddr
, const unsigned char *myaddr
, int len
)
4424 /* Round starting address down to longword boundary. */
4425 register CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
4426 /* Round ending address up; get number of longwords that makes. */
4428 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
4429 / sizeof (PTRACE_XFER_TYPE
);
4431 /* Allocate buffer of that many longwords. */
4432 register PTRACE_XFER_TYPE
*buffer
= (PTRACE_XFER_TYPE
*)
4433 alloca (count
* sizeof (PTRACE_XFER_TYPE
));
4435 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4439 /* Dump up to four bytes. */
4440 unsigned int val
= * (unsigned int *) myaddr
;
4446 val
= val
& 0xffffff;
4447 fprintf (stderr
, "Writing %0*x to 0x%08lx\n", 2 * ((len
< 4) ? len
: 4),
4448 val
, (long)memaddr
);
4451 /* Fill start and end extra bytes of buffer with existing memory data. */
4454 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
4455 about coercing an 8 byte integer to a 4 byte pointer. */
4456 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
4457 (PTRACE_ARG3_TYPE
) (uintptr_t) addr
, 0);
4465 = ptrace (PTRACE_PEEKTEXT
, pid
,
4466 /* Coerce to a uintptr_t first to avoid potential gcc warning
4467 about coercing an 8 byte integer to a 4 byte pointer. */
4468 (PTRACE_ARG3_TYPE
) (uintptr_t) (addr
+ (count
- 1)
4469 * sizeof (PTRACE_XFER_TYPE
)),
4475 /* Copy data to be written over corresponding part of buffer. */
4477 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
4480 /* Write the entire buffer. */
4482 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
4485 ptrace (PTRACE_POKETEXT
, pid
,
4486 /* Coerce to a uintptr_t first to avoid potential gcc warning
4487 about coercing an 8 byte integer to a 4 byte pointer. */
4488 (PTRACE_ARG3_TYPE
) (uintptr_t) addr
,
4489 (PTRACE_ARG4_TYPE
) buffer
[i
]);
4497 /* Non-zero if the kernel supports PTRACE_O_TRACEFORK. */
4498 static int linux_supports_tracefork_flag
;
4501 linux_enable_event_reporting (int pid
)
4503 if (!linux_supports_tracefork_flag
)
4506 ptrace (PTRACE_SETOPTIONS
, pid
, 0, (PTRACE_ARG4_TYPE
) PTRACE_O_TRACECLONE
);
4509 /* Helper functions for linux_test_for_tracefork, called via clone (). */
4512 linux_tracefork_grandchild (void *arg
)
4517 #define STACK_SIZE 4096
4520 linux_tracefork_child (void *arg
)
4522 ptrace (PTRACE_TRACEME
, 0, 0, 0);
4523 kill (getpid (), SIGSTOP
);
4525 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
4528 linux_tracefork_grandchild (NULL
);
4530 #else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4533 __clone2 (linux_tracefork_grandchild
, arg
, STACK_SIZE
,
4534 CLONE_VM
| SIGCHLD
, NULL
);
4536 clone (linux_tracefork_grandchild
, (char *) arg
+ STACK_SIZE
,
4537 CLONE_VM
| SIGCHLD
, NULL
);
4540 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4545 /* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
4546 sure that we can enable the option, and that it had the desired
4550 linux_test_for_tracefork (void)
4552 int child_pid
, ret
, status
;
4554 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4555 char *stack
= xmalloc (STACK_SIZE
* 4);
4556 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4558 linux_supports_tracefork_flag
= 0;
4560 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
4562 child_pid
= fork ();
4564 linux_tracefork_child (NULL
);
4566 #else /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4568 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
4570 child_pid
= __clone2 (linux_tracefork_child
, stack
, STACK_SIZE
,
4571 CLONE_VM
| SIGCHLD
, stack
+ STACK_SIZE
* 2);
4572 #else /* !__ia64__ */
4573 child_pid
= clone (linux_tracefork_child
, stack
+ STACK_SIZE
,
4574 CLONE_VM
| SIGCHLD
, stack
+ STACK_SIZE
* 2);
4575 #endif /* !__ia64__ */
4577 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4579 if (child_pid
== -1)
4580 perror_with_name ("clone");
4582 ret
= my_waitpid (child_pid
, &status
, 0);
4584 perror_with_name ("waitpid");
4585 else if (ret
!= child_pid
)
4586 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret
);
4587 if (! WIFSTOPPED (status
))
4588 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status
);
4590 ret
= ptrace (PTRACE_SETOPTIONS
, child_pid
, 0,
4591 (PTRACE_ARG4_TYPE
) PTRACE_O_TRACEFORK
);
4594 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
4597 warning ("linux_test_for_tracefork: failed to kill child");
4601 ret
= my_waitpid (child_pid
, &status
, 0);
4602 if (ret
!= child_pid
)
4603 warning ("linux_test_for_tracefork: failed to wait for killed child");
4604 else if (!WIFSIGNALED (status
))
4605 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
4606 "killed child", status
);
4611 ret
= ptrace (PTRACE_CONT
, child_pid
, 0, 0);
4613 warning ("linux_test_for_tracefork: failed to resume child");
4615 ret
= my_waitpid (child_pid
, &status
, 0);
4617 if (ret
== child_pid
&& WIFSTOPPED (status
)
4618 && status
>> 16 == PTRACE_EVENT_FORK
)
4621 ret
= ptrace (PTRACE_GETEVENTMSG
, child_pid
, 0, &second_pid
);
4622 if (ret
== 0 && second_pid
!= 0)
4626 linux_supports_tracefork_flag
= 1;
4627 my_waitpid (second_pid
, &second_status
, 0);
4628 ret
= ptrace (PTRACE_KILL
, second_pid
, 0, 0);
4630 warning ("linux_test_for_tracefork: failed to kill second child");
4631 my_waitpid (second_pid
, &status
, 0);
4635 warning ("linux_test_for_tracefork: unexpected result from waitpid "
4636 "(%d, status 0x%x)", ret
, status
);
4640 ret
= ptrace (PTRACE_KILL
, child_pid
, 0, 0);
4642 warning ("linux_test_for_tracefork: failed to kill child");
4643 my_waitpid (child_pid
, &status
, 0);
4645 while (WIFSTOPPED (status
));
4647 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4649 #endif /* defined(__UCLIBC__) && defined(HAS_NOMMU) */
4654 linux_look_up_symbols (void)
4656 #ifdef USE_THREAD_DB
4657 struct process_info
*proc
= current_process ();
4659 if (proc
->private->thread_db
!= NULL
)
4662 /* If the kernel supports tracing forks then it also supports tracing
4663 clones, and then we don't need to use the magic thread event breakpoint
4664 to learn about threads. */
4665 thread_db_init (!linux_supports_tracefork_flag
);
4670 linux_request_interrupt (void)
4672 extern unsigned long signal_pid
;
4674 if (!ptid_equal (cont_thread
, null_ptid
)
4675 && !ptid_equal (cont_thread
, minus_one_ptid
))
4677 struct lwp_info
*lwp
;
4680 lwp
= get_thread_lwp (current_inferior
);
4681 lwpid
= lwpid_of (lwp
);
4682 kill_lwp (lwpid
, SIGINT
);
4685 kill_lwp (signal_pid
, SIGINT
);
4688 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
4689 to debugger memory starting at MYADDR. */
4692 linux_read_auxv (CORE_ADDR offset
, unsigned char *myaddr
, unsigned int len
)
4694 char filename
[PATH_MAX
];
4696 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4698 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
4700 fd
= open (filename
, O_RDONLY
);
4704 if (offset
!= (CORE_ADDR
) 0
4705 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
4708 n
= read (fd
, myaddr
, len
);
4715 /* These breakpoint and watchpoint related wrapper functions simply
4716 pass on the function call if the target has registered a
4717 corresponding function. */
4720 linux_insert_point (char type
, CORE_ADDR addr
, int len
)
4722 if (the_low_target
.insert_point
!= NULL
)
4723 return the_low_target
.insert_point (type
, addr
, len
);
4725 /* Unsupported (see target.h). */
4730 linux_remove_point (char type
, CORE_ADDR addr
, int len
)
4732 if (the_low_target
.remove_point
!= NULL
)
4733 return the_low_target
.remove_point (type
, addr
, len
);
4735 /* Unsupported (see target.h). */
4740 linux_stopped_by_watchpoint (void)
4742 struct lwp_info
*lwp
= get_thread_lwp (current_inferior
);
4744 return lwp
->stopped_by_watchpoint
;
4748 linux_stopped_data_address (void)
4750 struct lwp_info
*lwp
= get_thread_lwp (current_inferior
);
4752 return lwp
->stopped_data_address
;
4755 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
4756 #if defined(__mcoldfire__)
4757 /* These should really be defined in the kernel's ptrace.h header. */
4758 #define PT_TEXT_ADDR 49*4
4759 #define PT_DATA_ADDR 50*4
4760 #define PT_TEXT_END_ADDR 51*4
4762 #define PT_TEXT_ADDR 220
4763 #define PT_TEXT_END_ADDR 224
4764 #define PT_DATA_ADDR 228
4765 #elif defined(__TMS320C6X__)
4766 #define PT_TEXT_ADDR (0x10000*4)
4767 #define PT_DATA_ADDR (0x10004*4)
4768 #define PT_TEXT_END_ADDR (0x10008*4)
4771 /* Under uClinux, programs are loaded at non-zero offsets, which we need
4772 to tell gdb about. */
4775 linux_read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
4777 #if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
4778 unsigned long text
, text_end
, data
;
4779 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
4783 text
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_ADDR
, 0);
4784 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_TEXT_END_ADDR
, 0);
4785 data
= ptrace (PTRACE_PEEKUSER
, pid
, (long)PT_DATA_ADDR
, 0);
4789 /* Both text and data offsets produced at compile-time (and so
4790 used by gdb) are relative to the beginning of the program,
4791 with the data segment immediately following the text segment.
4792 However, the actual runtime layout in memory may put the data
4793 somewhere else, so when we send gdb a data base-address, we
4794 use the real data base address and subtract the compile-time
4795 data base-address from it (which is just the length of the
4796 text segment). BSS immediately follows data in both
4799 *data_p
= data
- (text_end
- text
);
4809 linux_qxfer_osdata (const char *annex
,
4810 unsigned char *readbuf
, unsigned const char *writebuf
,
4811 CORE_ADDR offset
, int len
)
4813 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
4816 /* Convert a native/host siginfo object, into/from the siginfo in the
4817 layout of the inferiors' architecture. */
4820 siginfo_fixup (siginfo_t
*siginfo
, void *inf_siginfo
, int direction
)
4824 if (the_low_target
.siginfo_fixup
!= NULL
)
4825 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
4827 /* If there was no callback, or the callback didn't do anything,
4828 then just do a straight memcpy. */
4832 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
4834 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
4839 linux_xfer_siginfo (const char *annex
, unsigned char *readbuf
,
4840 unsigned const char *writebuf
, CORE_ADDR offset
, int len
)
4844 char inf_siginfo
[sizeof (siginfo_t
)];
4846 if (current_inferior
== NULL
)
4849 pid
= lwpid_of (get_thread_lwp (current_inferior
));
4852 fprintf (stderr
, "%s siginfo for lwp %d.\n",
4853 readbuf
!= NULL
? "Reading" : "Writing",
4856 if (offset
>= sizeof (siginfo
))
4859 if (ptrace (PTRACE_GETSIGINFO
, pid
, 0, &siginfo
) != 0)
4862 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
4863 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
4864 inferior with a 64-bit GDBSERVER should look the same as debugging it
4865 with a 32-bit GDBSERVER, we need to convert it. */
4866 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
4868 if (offset
+ len
> sizeof (siginfo
))
4869 len
= sizeof (siginfo
) - offset
;
4871 if (readbuf
!= NULL
)
4872 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
4875 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
4877 /* Convert back to ptrace layout before flushing it out. */
4878 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
4880 if (ptrace (PTRACE_SETSIGINFO
, pid
, 0, &siginfo
) != 0)
4887 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
4888 so we notice when children change state; as the handler for the
4889 sigsuspend in my_waitpid. */
4892 sigchld_handler (int signo
)
4894 int old_errno
= errno
;
4900 /* fprintf is not async-signal-safe, so call write
4902 if (write (2, "sigchld_handler\n",
4903 sizeof ("sigchld_handler\n") - 1) < 0)
4904 break; /* just ignore */
4908 if (target_is_async_p ())
4909 async_file_mark (); /* trigger a linux_wait */
4915 linux_supports_non_stop (void)
4921 linux_async (int enable
)
4923 int previous
= (linux_event_pipe
[0] != -1);
4926 fprintf (stderr
, "linux_async (%d), previous=%d\n",
4929 if (previous
!= enable
)
4932 sigemptyset (&mask
);
4933 sigaddset (&mask
, SIGCHLD
);
4935 sigprocmask (SIG_BLOCK
, &mask
, NULL
);
4939 if (pipe (linux_event_pipe
) == -1)
4940 fatal ("creating event pipe failed.");
4942 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
4943 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
4945 /* Register the event loop handler. */
4946 add_file_handler (linux_event_pipe
[0],
4947 handle_target_event
, NULL
);
4949 /* Always trigger a linux_wait. */
4954 delete_file_handler (linux_event_pipe
[0]);
4956 close (linux_event_pipe
[0]);
4957 close (linux_event_pipe
[1]);
4958 linux_event_pipe
[0] = -1;
4959 linux_event_pipe
[1] = -1;
4962 sigprocmask (SIG_UNBLOCK
, &mask
, NULL
);
4969 linux_start_non_stop (int nonstop
)
4971 /* Register or unregister from event-loop accordingly. */
4972 linux_async (nonstop
);
4977 linux_supports_multi_process (void)
4983 linux_supports_disable_randomization (void)
4985 #ifdef HAVE_PERSONALITY
4993 linux_supports_agent (void)
4998 /* Enumerate spufs IDs for process PID. */
5000 spu_enumerate_spu_ids (long pid
, unsigned char *buf
, CORE_ADDR offset
, int len
)
5006 struct dirent
*entry
;
5008 sprintf (path
, "/proc/%ld/fd", pid
);
5009 dir
= opendir (path
);
5014 while ((entry
= readdir (dir
)) != NULL
)
5020 fd
= atoi (entry
->d_name
);
5024 sprintf (path
, "/proc/%ld/fd/%d", pid
, fd
);
5025 if (stat (path
, &st
) != 0)
5027 if (!S_ISDIR (st
.st_mode
))
5030 if (statfs (path
, &stfs
) != 0)
5032 if (stfs
.f_type
!= SPUFS_MAGIC
)
5035 if (pos
>= offset
&& pos
+ 4 <= offset
+ len
)
5037 *(unsigned int *)(buf
+ pos
- offset
) = fd
;
5047 /* Implements the to_xfer_partial interface for the TARGET_OBJECT_SPU
5048 object type, using the /proc file system. */
5050 linux_qxfer_spu (const char *annex
, unsigned char *readbuf
,
5051 unsigned const char *writebuf
,
5052 CORE_ADDR offset
, int len
)
5054 long pid
= lwpid_of (get_thread_lwp (current_inferior
));
5059 if (!writebuf
&& !readbuf
)
5067 return spu_enumerate_spu_ids (pid
, readbuf
, offset
, len
);
5070 sprintf (buf
, "/proc/%ld/fd/%s", pid
, annex
);
5071 fd
= open (buf
, writebuf
? O_WRONLY
: O_RDONLY
);
5076 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5083 ret
= write (fd
, writebuf
, (size_t) len
);
5085 ret
= read (fd
, readbuf
, (size_t) len
);
5091 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
5092 struct target_loadseg
5094 /* Core address to which the segment is mapped. */
5096 /* VMA recorded in the program header. */
5098 /* Size of this segment in memory. */
5102 # if defined PT_GETDSBT
5103 struct target_loadmap
5105 /* Protocol version number, must be zero. */
5107 /* Pointer to the DSBT table, its size, and the DSBT index. */
5108 unsigned *dsbt_table
;
5109 unsigned dsbt_size
, dsbt_index
;
5110 /* Number of segments in this map. */
5112 /* The actual memory map. */
5113 struct target_loadseg segs
[/*nsegs*/];
5115 # define LINUX_LOADMAP PT_GETDSBT
5116 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
5117 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
5119 struct target_loadmap
5121 /* Protocol version number, must be zero. */
5123 /* Number of segments in this map. */
5125 /* The actual memory map. */
5126 struct target_loadseg segs
[/*nsegs*/];
5128 # define LINUX_LOADMAP PTRACE_GETFDPIC
5129 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
5130 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
5134 linux_read_loadmap (const char *annex
, CORE_ADDR offset
,
5135 unsigned char *myaddr
, unsigned int len
)
5137 int pid
= lwpid_of (get_thread_lwp (current_inferior
));
5139 struct target_loadmap
*data
= NULL
;
5140 unsigned int actual_length
, copy_length
;
5142 if (strcmp (annex
, "exec") == 0)
5143 addr
= (int) LINUX_LOADMAP_EXEC
;
5144 else if (strcmp (annex
, "interp") == 0)
5145 addr
= (int) LINUX_LOADMAP_INTERP
;
5149 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
5155 actual_length
= sizeof (struct target_loadmap
)
5156 + sizeof (struct target_loadseg
) * data
->nsegs
;
5158 if (offset
< 0 || offset
> actual_length
)
5161 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
5162 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
5166 # define linux_read_loadmap NULL
5167 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
5170 linux_process_qsupported (const char *query
)
5172 if (the_low_target
.process_qsupported
!= NULL
)
5173 the_low_target
.process_qsupported (query
);
5177 linux_supports_tracepoints (void)
5179 if (*the_low_target
.supports_tracepoints
== NULL
)
5182 return (*the_low_target
.supports_tracepoints
) ();
5186 linux_read_pc (struct regcache
*regcache
)
5188 if (the_low_target
.get_pc
== NULL
)
5191 return (*the_low_target
.get_pc
) (regcache
);
5195 linux_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
5197 gdb_assert (the_low_target
.set_pc
!= NULL
);
5199 (*the_low_target
.set_pc
) (regcache
, pc
);
5203 linux_thread_stopped (struct thread_info
*thread
)
5205 return get_thread_lwp (thread
)->stopped
;
5208 /* This exposes stop-all-threads functionality to other modules. */
5211 linux_pause_all (int freeze
)
5213 stop_all_lwps (freeze
, NULL
);
5216 /* This exposes unstop-all-threads functionality to other gdbserver
5220 linux_unpause_all (int unfreeze
)
5222 unstop_all_lwps (unfreeze
, NULL
);
5226 linux_prepare_to_access_memory (void)
5228 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5231 linux_pause_all (1);
5236 linux_done_accessing_memory (void)
5238 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
5241 linux_unpause_all (1);
5245 linux_install_fast_tracepoint_jump_pad (CORE_ADDR tpoint
, CORE_ADDR tpaddr
,
5246 CORE_ADDR collector
,
5249 CORE_ADDR
*jump_entry
,
5250 CORE_ADDR
*trampoline
,
5251 ULONGEST
*trampoline_size
,
5252 unsigned char *jjump_pad_insn
,
5253 ULONGEST
*jjump_pad_insn_size
,
5254 CORE_ADDR
*adjusted_insn_addr
,
5255 CORE_ADDR
*adjusted_insn_addr_end
,
5258 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
5259 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
5260 jump_entry
, trampoline
, trampoline_size
,
5261 jjump_pad_insn
, jjump_pad_insn_size
,
5262 adjusted_insn_addr
, adjusted_insn_addr_end
,
5266 static struct emit_ops
*
5267 linux_emit_ops (void)
5269 if (the_low_target
.emit_ops
!= NULL
)
5270 return (*the_low_target
.emit_ops
) ();
5276 linux_get_min_fast_tracepoint_insn_len (void)
5278 return (*the_low_target
.get_min_fast_tracepoint_insn_len
) ();
5281 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
5284 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
5285 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
5287 char filename
[PATH_MAX
];
5289 const int auxv_size
= is_elf64
5290 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
5291 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
5293 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5295 fd
= open (filename
, O_RDONLY
);
5301 while (read (fd
, buf
, auxv_size
) == auxv_size
5302 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
5306 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
5308 switch (aux
->a_type
)
5311 *phdr_memaddr
= aux
->a_un
.a_val
;
5314 *num_phdr
= aux
->a_un
.a_val
;
5320 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
5322 switch (aux
->a_type
)
5325 *phdr_memaddr
= aux
->a_un
.a_val
;
5328 *num_phdr
= aux
->a_un
.a_val
;
5336 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
5338 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
5339 "phdr_memaddr = %ld, phdr_num = %d",
5340 (long) *phdr_memaddr
, *num_phdr
);
5347 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
5350 get_dynamic (const int pid
, const int is_elf64
)
5352 CORE_ADDR phdr_memaddr
, relocation
;
5354 unsigned char *phdr_buf
;
5355 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
5357 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
5360 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
5361 phdr_buf
= alloca (num_phdr
* phdr_size
);
5363 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
5366 /* Compute relocation: it is expected to be 0 for "regular" executables,
5367 non-zero for PIE ones. */
5369 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
5372 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5374 if (p
->p_type
== PT_PHDR
)
5375 relocation
= phdr_memaddr
- p
->p_vaddr
;
5379 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5381 if (p
->p_type
== PT_PHDR
)
5382 relocation
= phdr_memaddr
- p
->p_vaddr
;
5385 if (relocation
== -1)
5387 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
5388 any real world executables, including PIE executables, have always
5389 PT_PHDR present. PT_PHDR is not present in some shared libraries or
5390 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
5391 or present DT_DEBUG anyway (fpc binaries are statically linked).
5393 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
5395 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
5400 for (i
= 0; i
< num_phdr
; i
++)
5404 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5406 if (p
->p_type
== PT_DYNAMIC
)
5407 return p
->p_vaddr
+ relocation
;
5411 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
5413 if (p
->p_type
== PT_DYNAMIC
)
5414 return p
->p_vaddr
+ relocation
;
5421 /* Return &_r_debug in the inferior, or -1 if not present. Return value
5422 can be 0 if the inferior does not yet have the library list initialized.
5423 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
5424 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
5427 get_r_debug (const int pid
, const int is_elf64
)
5429 CORE_ADDR dynamic_memaddr
;
5430 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
5431 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
5434 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
5435 if (dynamic_memaddr
== 0)
5438 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
5442 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
5446 unsigned char buf
[sizeof (Elf64_Xword
)];
5450 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
5452 if (linux_read_memory (dyn
->d_un
.d_val
,
5453 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
5459 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
5460 map
= dyn
->d_un
.d_val
;
5462 if (dyn
->d_tag
== DT_NULL
)
5467 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
5471 unsigned char buf
[sizeof (Elf32_Word
)];
5475 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
5477 if (linux_read_memory (dyn
->d_un
.d_val
,
5478 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
5484 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
5485 map
= dyn
->d_un
.d_val
;
5487 if (dyn
->d_tag
== DT_NULL
)
5491 dynamic_memaddr
+= dyn_size
;
5497 /* Read one pointer from MEMADDR in the inferior. */
5500 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
5504 /* Go through a union so this works on either big or little endian
5505 hosts, when the inferior's pointer size is smaller than the size
5506 of CORE_ADDR. It is assumed the inferior's endianness is the
5507 same of the superior's. */
5510 CORE_ADDR core_addr
;
5515 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
5518 if (ptr_size
== sizeof (CORE_ADDR
))
5519 *ptr
= addr
.core_addr
;
5520 else if (ptr_size
== sizeof (unsigned int))
5523 gdb_assert_not_reached ("unhandled pointer size");
5528 struct link_map_offsets
5530 /* Offset and size of r_debug.r_version. */
5531 int r_version_offset
;
5533 /* Offset and size of r_debug.r_map. */
5536 /* Offset to l_addr field in struct link_map. */
5539 /* Offset to l_name field in struct link_map. */
5542 /* Offset to l_ld field in struct link_map. */
5545 /* Offset to l_next field in struct link_map. */
5548 /* Offset to l_prev field in struct link_map. */
5552 /* Construct qXfer:libraries-svr4:read reply. */
5555 linux_qxfer_libraries_svr4 (const char *annex
, unsigned char *readbuf
,
5556 unsigned const char *writebuf
,
5557 CORE_ADDR offset
, int len
)
5560 unsigned document_len
;
5561 struct process_info_private
*const priv
= current_process ()->private;
5562 char filename
[PATH_MAX
];
5565 static const struct link_map_offsets lmo_32bit_offsets
=
5567 0, /* r_version offset. */
5568 4, /* r_debug.r_map offset. */
5569 0, /* l_addr offset in link_map. */
5570 4, /* l_name offset in link_map. */
5571 8, /* l_ld offset in link_map. */
5572 12, /* l_next offset in link_map. */
5573 16 /* l_prev offset in link_map. */
5576 static const struct link_map_offsets lmo_64bit_offsets
=
5578 0, /* r_version offset. */
5579 8, /* r_debug.r_map offset. */
5580 0, /* l_addr offset in link_map. */
5581 8, /* l_name offset in link_map. */
5582 16, /* l_ld offset in link_map. */
5583 24, /* l_next offset in link_map. */
5584 32 /* l_prev offset in link_map. */
5586 const struct link_map_offsets
*lmo
;
5588 if (writebuf
!= NULL
)
5590 if (readbuf
== NULL
)
5593 pid
= lwpid_of (get_thread_lwp (current_inferior
));
5594 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
5595 is_elf64
= elf_64_file_p (filename
);
5596 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
5598 if (priv
->r_debug
== 0)
5599 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
5601 if (priv
->r_debug
== (CORE_ADDR
) -1 || priv
->r_debug
== 0)
5603 document
= xstrdup ("<library-list-svr4 version=\"1.0\"/>\n");
5607 int allocated
= 1024;
5609 const int ptr_size
= is_elf64
? 8 : 4;
5610 CORE_ADDR lm_addr
, lm_prev
, l_name
, l_addr
, l_ld
, l_next
, l_prev
;
5611 int r_version
, header_done
= 0;
5613 document
= xmalloc (allocated
);
5614 strcpy (document
, "<library-list-svr4 version=\"1.0\"");
5615 p
= document
+ strlen (document
);
5618 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
5619 (unsigned char *) &r_version
,
5620 sizeof (r_version
)) != 0
5623 warning ("unexpected r_debug version %d", r_version
);
5627 if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
5628 &lm_addr
, ptr_size
) != 0)
5630 warning ("unable to read r_map from 0x%lx",
5631 (long) priv
->r_debug
+ lmo
->r_map_offset
);
5636 while (read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
5637 &l_name
, ptr_size
) == 0
5638 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
5639 &l_addr
, ptr_size
) == 0
5640 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
5641 &l_ld
, ptr_size
) == 0
5642 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
5643 &l_prev
, ptr_size
) == 0
5644 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
5645 &l_next
, ptr_size
) == 0)
5647 unsigned char libname
[PATH_MAX
];
5649 if (lm_prev
!= l_prev
)
5651 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
5652 (long) lm_prev
, (long) l_prev
);
5656 /* Not checking for error because reading may stop before
5657 we've got PATH_MAX worth of characters. */
5659 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
5660 libname
[sizeof (libname
) - 1] = '\0';
5661 if (libname
[0] != '\0')
5663 /* 6x the size for xml_escape_text below. */
5664 size_t len
= 6 * strlen ((char *) libname
);
5669 /* Terminate `<library-list-svr4'. */
5674 while (allocated
< p
- document
+ len
+ 200)
5676 /* Expand to guarantee sufficient storage. */
5677 uintptr_t document_len
= p
- document
;
5679 document
= xrealloc (document
, 2 * allocated
);
5681 p
= document
+ document_len
;
5684 name
= xml_escape_text ((char *) libname
);
5685 p
+= sprintf (p
, "<library name=\"%s\" lm=\"0x%lx\" "
5686 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
5687 name
, (unsigned long) lm_addr
,
5688 (unsigned long) l_addr
, (unsigned long) l_ld
);
5691 else if (lm_prev
== 0)
5693 sprintf (p
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
5706 /* Empty list; terminate `<library-list-svr4'. */
5710 strcpy (p
, "</library-list-svr4>");
5713 document_len
= strlen (document
);
5714 if (offset
< document_len
)
5715 document_len
-= offset
;
5718 if (len
> document_len
)
5721 memcpy (readbuf
, document
+ offset
, len
);
5727 static struct target_ops linux_target_ops
= {
5728 linux_create_inferior
,
5737 linux_fetch_registers
,
5738 linux_store_registers
,
5739 linux_prepare_to_access_memory
,
5740 linux_done_accessing_memory
,
5743 linux_look_up_symbols
,
5744 linux_request_interrupt
,
5748 linux_stopped_by_watchpoint
,
5749 linux_stopped_data_address
,
5750 #if defined(__UCLIBC__) && defined(HAS_NOMMU)
5755 #ifdef USE_THREAD_DB
5756 thread_db_get_tls_address
,
5761 hostio_last_error_from_errno
,
5764 linux_supports_non_stop
,
5766 linux_start_non_stop
,
5767 linux_supports_multi_process
,
5768 #ifdef USE_THREAD_DB
5769 thread_db_handle_monitor_command
,
5773 linux_common_core_of_thread
,
5775 linux_process_qsupported
,
5776 linux_supports_tracepoints
,
5779 linux_thread_stopped
,
5783 linux_cancel_breakpoints
,
5784 linux_stabilize_threads
,
5785 linux_install_fast_tracepoint_jump_pad
,
5787 linux_supports_disable_randomization
,
5788 linux_get_min_fast_tracepoint_insn_len
,
5789 linux_qxfer_libraries_svr4
,
5790 linux_supports_agent
,
5794 linux_init_signals ()
5796 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
5797 to find what the cancel signal actually is. */
5798 #ifndef __ANDROID__ /* Bionic doesn't use SIGRTMIN the way glibc does. */
5799 signal (__SIGRTMIN
+1, SIG_IGN
);
5804 initialize_low (void)
5806 struct sigaction sigchld_action
;
5807 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
5808 set_target_ops (&linux_target_ops
);
5809 set_breakpoint_data (the_low_target
.breakpoint
,
5810 the_low_target
.breakpoint_len
);
5811 linux_init_signals ();
5812 linux_test_for_tracefork ();
5813 #ifdef HAVE_LINUX_REGSETS
5814 for (num_regsets
= 0; target_regsets
[num_regsets
].size
>= 0; num_regsets
++)
5816 disabled_regsets
= xmalloc (num_regsets
);
5819 sigchld_action
.sa_handler
= sigchld_handler
;
5820 sigemptyset (&sigchld_action
.sa_mask
);
5821 sigchld_action
.sa_flags
= SA_RESTART
;
5822 sigaction (SIGCHLD
, &sigchld_action
, NULL
);