1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2020 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 "nat/linux-osdata.h"
22 #include "gdbsupport/agent.h"
24 #include "gdbsupport/rsp-low.h"
25 #include "gdbsupport/signals-state-save-restore.h"
26 #include "nat/linux-nat.h"
27 #include "nat/linux-waitpid.h"
28 #include "gdbsupport/gdb_wait.h"
29 #include "nat/gdb_ptrace.h"
30 #include "nat/linux-ptrace.h"
31 #include "nat/linux-procfs.h"
32 #include "nat/linux-personality.h"
34 #include <sys/ioctl.h>
37 #include <sys/syscall.h>
41 #include <sys/types.h>
46 #include "gdbsupport/filestuff.h"
47 #include "tracepoint.h"
49 #include "gdbsupport/common-inferior.h"
50 #include "nat/fork-inferior.h"
51 #include "gdbsupport/environ.h"
52 #include "gdbsupport/gdb-sigmask.h"
53 #include "gdbsupport/scoped_restore.h"
55 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
56 then ELFMAG0 will have been defined. If it didn't get included by
57 gdb_proc_service.h then including it will likely introduce a duplicate
58 definition of elf_fpregset_t. */
61 #include "nat/linux-namespaces.h"
63 #ifdef HAVE_PERSONALITY
64 # include <sys/personality.h>
65 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
66 # define ADDR_NO_RANDOMIZE 0x0040000
78 /* Some targets did not define these ptrace constants from the start,
79 so gdbserver defines them locally here. In the future, these may
80 be removed after they are added to asm/ptrace.h. */
81 #if !(defined(PT_TEXT_ADDR) \
82 || defined(PT_DATA_ADDR) \
83 || defined(PT_TEXT_END_ADDR))
84 #if defined(__mcoldfire__)
85 /* These are still undefined in 3.10 kernels. */
86 #define PT_TEXT_ADDR 49*4
87 #define PT_DATA_ADDR 50*4
88 #define PT_TEXT_END_ADDR 51*4
89 /* BFIN already defines these since at least 2.6.32 kernels. */
91 #define PT_TEXT_ADDR 220
92 #define PT_TEXT_END_ADDR 224
93 #define PT_DATA_ADDR 228
94 /* These are still undefined in 3.10 kernels. */
95 #elif defined(__TMS320C6X__)
96 #define PT_TEXT_ADDR (0x10000*4)
97 #define PT_DATA_ADDR (0x10004*4)
98 #define PT_TEXT_END_ADDR (0x10008*4)
102 #if (defined(__UCLIBC__) \
103 && defined(HAS_NOMMU) \
104 && defined(PT_TEXT_ADDR) \
105 && defined(PT_DATA_ADDR) \
106 && defined(PT_TEXT_END_ADDR))
107 #define SUPPORTS_READ_OFFSETS
110 #ifdef HAVE_LINUX_BTRACE
111 # include "nat/linux-btrace.h"
112 # include "gdbsupport/btrace-common.h"
115 #ifndef HAVE_ELF32_AUXV_T
116 /* Copied from glibc's elf.h. */
119 uint32_t a_type
; /* Entry type */
122 uint32_t a_val
; /* Integer value */
123 /* We use to have pointer elements added here. We cannot do that,
124 though, since it does not work when using 32-bit definitions
125 on 64-bit platforms and vice versa. */
130 #ifndef HAVE_ELF64_AUXV_T
131 /* Copied from glibc's elf.h. */
134 uint64_t a_type
; /* Entry type */
137 uint64_t a_val
; /* Integer value */
138 /* We use to have pointer elements added here. We cannot do that,
139 though, since it does not work when using 32-bit definitions
140 on 64-bit platforms and vice versa. */
145 /* Does the current host support PTRACE_GETREGSET? */
146 int have_ptrace_getregset
= -1;
150 /* See nat/linux-nat.h. */
153 ptid_of_lwp (struct lwp_info
*lwp
)
155 return ptid_of (get_lwp_thread (lwp
));
158 /* See nat/linux-nat.h. */
161 lwp_set_arch_private_info (struct lwp_info
*lwp
,
162 struct arch_lwp_info
*info
)
164 lwp
->arch_private
= info
;
167 /* See nat/linux-nat.h. */
169 struct arch_lwp_info
*
170 lwp_arch_private_info (struct lwp_info
*lwp
)
172 return lwp
->arch_private
;
175 /* See nat/linux-nat.h. */
178 lwp_is_stopped (struct lwp_info
*lwp
)
183 /* See nat/linux-nat.h. */
185 enum target_stop_reason
186 lwp_stop_reason (struct lwp_info
*lwp
)
188 return lwp
->stop_reason
;
191 /* See nat/linux-nat.h. */
194 lwp_is_stepping (struct lwp_info
*lwp
)
196 return lwp
->stepping
;
199 /* A list of all unknown processes which receive stop signals. Some
200 other process will presumably claim each of these as forked
201 children momentarily. */
203 struct simple_pid_list
205 /* The process ID. */
208 /* The status as reported by waitpid. */
212 struct simple_pid_list
*next
;
214 struct simple_pid_list
*stopped_pids
;
216 /* Trivial list manipulation functions to keep track of a list of new
217 stopped processes. */
220 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
222 struct simple_pid_list
*new_pid
= XNEW (struct simple_pid_list
);
225 new_pid
->status
= status
;
226 new_pid
->next
= *listp
;
231 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
233 struct simple_pid_list
**p
;
235 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
236 if ((*p
)->pid
== pid
)
238 struct simple_pid_list
*next
= (*p
)->next
;
240 *statusp
= (*p
)->status
;
248 enum stopping_threads_kind
250 /* Not stopping threads presently. */
251 NOT_STOPPING_THREADS
,
253 /* Stopping threads. */
256 /* Stopping and suspending threads. */
257 STOPPING_AND_SUSPENDING_THREADS
260 /* This is set while stop_all_lwps is in effect. */
261 enum stopping_threads_kind stopping_threads
= NOT_STOPPING_THREADS
;
263 /* FIXME make into a target method? */
264 int using_threads
= 1;
266 /* True if we're presently stabilizing threads (moving them out of
268 static int stabilizing_threads
;
270 static void linux_resume_one_lwp (struct lwp_info
*lwp
,
271 int step
, int signal
, siginfo_t
*info
);
272 static void unsuspend_all_lwps (struct lwp_info
*except
);
273 static struct lwp_info
*add_lwp (ptid_t ptid
);
274 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
275 static int lwp_is_marked_dead (struct lwp_info
*lwp
);
276 static int finish_step_over (struct lwp_info
*lwp
);
277 static int kill_lwp (unsigned long lwpid
, int signo
);
278 static void enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
);
279 static int linux_low_ptrace_options (int attached
);
280 static int check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
);
281 static void proceed_one_lwp (thread_info
*thread
, lwp_info
*except
);
283 /* When the event-loop is doing a step-over, this points at the thread
285 ptid_t step_over_bkpt
;
287 /* True if the low target can hardware single-step. */
290 can_hardware_single_step (void)
292 if (the_low_target
.supports_hardware_single_step
!= NULL
)
293 return the_low_target
.supports_hardware_single_step ();
298 /* True if the low target can software single-step. Such targets
299 implement the GET_NEXT_PCS callback. */
302 can_software_single_step (void)
304 return (the_low_target
.get_next_pcs
!= NULL
);
307 /* True if the low target supports memory breakpoints. If so, we'll
308 have a GET_PC implementation. */
311 supports_breakpoints (void)
313 return (the_low_target
.get_pc
!= NULL
);
316 /* Returns true if this target can support fast tracepoints. This
317 does not mean that the in-process agent has been loaded in the
321 supports_fast_tracepoints (void)
323 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
326 /* True if LWP is stopped in its stepping range. */
329 lwp_in_step_range (struct lwp_info
*lwp
)
331 CORE_ADDR pc
= lwp
->stop_pc
;
333 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
336 struct pending_signals
340 struct pending_signals
*prev
;
343 /* The read/write ends of the pipe registered as waitable file in the
345 static int linux_event_pipe
[2] = { -1, -1 };
347 /* True if we're currently in async mode. */
348 #define target_is_async_p() (linux_event_pipe[0] != -1)
350 static void send_sigstop (struct lwp_info
*lwp
);
352 /* Return non-zero if HEADER is a 64-bit ELF file. */
355 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
357 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
358 && header
->e_ident
[EI_MAG1
] == ELFMAG1
359 && header
->e_ident
[EI_MAG2
] == ELFMAG2
360 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
362 *machine
= header
->e_machine
;
363 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
370 /* Return non-zero if FILE is a 64-bit ELF file,
371 zero if the file is not a 64-bit ELF file,
372 and -1 if the file is not accessible or doesn't exist. */
375 elf_64_file_p (const char *file
, unsigned int *machine
)
380 fd
= open (file
, O_RDONLY
);
384 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
391 return elf_64_header_p (&header
, machine
);
394 /* Accepts an integer PID; Returns true if the executable PID is
395 running is a 64-bit ELF file.. */
398 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
402 sprintf (file
, "/proc/%d/exe", pid
);
403 return elf_64_file_p (file
, machine
);
407 delete_lwp (struct lwp_info
*lwp
)
409 struct thread_info
*thr
= get_lwp_thread (lwp
);
412 debug_printf ("deleting %ld\n", lwpid_of (thr
));
416 if (the_low_target
.delete_thread
!= NULL
)
417 the_low_target
.delete_thread (lwp
->arch_private
);
419 gdb_assert (lwp
->arch_private
== NULL
);
424 /* Add a process to the common process list, and set its private
427 static struct process_info
*
428 linux_add_process (int pid
, int attached
)
430 struct process_info
*proc
;
432 proc
= add_process (pid
, attached
);
433 proc
->priv
= XCNEW (struct process_info_private
);
435 if (the_low_target
.new_process
!= NULL
)
436 proc
->priv
->arch_private
= the_low_target
.new_process ();
441 static CORE_ADDR
get_pc (struct lwp_info
*lwp
);
444 linux_process_target::arch_setup_thread (thread_info
*thread
)
446 struct thread_info
*saved_thread
;
448 saved_thread
= current_thread
;
449 current_thread
= thread
;
453 current_thread
= saved_thread
;
457 linux_process_target::handle_extended_wait (lwp_info
**orig_event_lwp
,
460 client_state
&cs
= get_client_state ();
461 struct lwp_info
*event_lwp
= *orig_event_lwp
;
462 int event
= linux_ptrace_get_extended_event (wstat
);
463 struct thread_info
*event_thr
= get_lwp_thread (event_lwp
);
464 struct lwp_info
*new_lwp
;
466 gdb_assert (event_lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
468 /* All extended events we currently use are mid-syscall. Only
469 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
470 you have to be using PTRACE_SEIZE to get that. */
471 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
473 if ((event
== PTRACE_EVENT_FORK
) || (event
== PTRACE_EVENT_VFORK
)
474 || (event
== PTRACE_EVENT_CLONE
))
477 unsigned long new_pid
;
480 /* Get the pid of the new lwp. */
481 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_thr
), (PTRACE_TYPE_ARG3
) 0,
484 /* If we haven't already seen the new PID stop, wait for it now. */
485 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
487 /* The new child has a pending SIGSTOP. We can't affect it until it
488 hits the SIGSTOP, but we're already attached. */
490 ret
= my_waitpid (new_pid
, &status
, __WALL
);
493 perror_with_name ("waiting for new child");
494 else if (ret
!= new_pid
)
495 warning ("wait returned unexpected PID %d", ret
);
496 else if (!WIFSTOPPED (status
))
497 warning ("wait returned unexpected status 0x%x", status
);
500 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
502 struct process_info
*parent_proc
;
503 struct process_info
*child_proc
;
504 struct lwp_info
*child_lwp
;
505 struct thread_info
*child_thr
;
506 struct target_desc
*tdesc
;
508 ptid
= ptid_t (new_pid
, new_pid
, 0);
512 debug_printf ("HEW: Got fork event from LWP %ld, "
514 ptid_of (event_thr
).lwp (),
518 /* Add the new process to the tables and clone the breakpoint
519 lists of the parent. We need to do this even if the new process
520 will be detached, since we will need the process object and the
521 breakpoints to remove any breakpoints from memory when we
522 detach, and the client side will access registers. */
523 child_proc
= linux_add_process (new_pid
, 0);
524 gdb_assert (child_proc
!= NULL
);
525 child_lwp
= add_lwp (ptid
);
526 gdb_assert (child_lwp
!= NULL
);
527 child_lwp
->stopped
= 1;
528 child_lwp
->must_set_ptrace_flags
= 1;
529 child_lwp
->status_pending_p
= 0;
530 child_thr
= get_lwp_thread (child_lwp
);
531 child_thr
->last_resume_kind
= resume_stop
;
532 child_thr
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
534 /* If we're suspending all threads, leave this one suspended
535 too. If the fork/clone parent is stepping over a breakpoint,
536 all other threads have been suspended already. Leave the
537 child suspended too. */
538 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
539 || event_lwp
->bp_reinsert
!= 0)
542 debug_printf ("HEW: leaving child suspended\n");
543 child_lwp
->suspended
= 1;
546 parent_proc
= get_thread_process (event_thr
);
547 child_proc
->attached
= parent_proc
->attached
;
549 if (event_lwp
->bp_reinsert
!= 0
550 && can_software_single_step ()
551 && event
== PTRACE_EVENT_VFORK
)
553 /* If we leave single-step breakpoints there, child will
554 hit it, so uninsert single-step breakpoints from parent
555 (and child). Once vfork child is done, reinsert
556 them back to parent. */
557 uninsert_single_step_breakpoints (event_thr
);
560 clone_all_breakpoints (child_thr
, event_thr
);
562 tdesc
= allocate_target_description ();
563 copy_target_description (tdesc
, parent_proc
->tdesc
);
564 child_proc
->tdesc
= tdesc
;
566 /* Clone arch-specific process data. */
567 if (the_low_target
.new_fork
!= NULL
)
568 the_low_target
.new_fork (parent_proc
, child_proc
);
570 /* Save fork info in the parent thread. */
571 if (event
== PTRACE_EVENT_FORK
)
572 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_FORKED
;
573 else if (event
== PTRACE_EVENT_VFORK
)
574 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORKED
;
576 event_lwp
->waitstatus
.value
.related_pid
= ptid
;
578 /* The status_pending field contains bits denoting the
579 extended event, so when the pending event is handled,
580 the handler will look at lwp->waitstatus. */
581 event_lwp
->status_pending_p
= 1;
582 event_lwp
->status_pending
= wstat
;
584 /* Link the threads until the parent event is passed on to
586 event_lwp
->fork_relative
= child_lwp
;
587 child_lwp
->fork_relative
= event_lwp
;
589 /* If the parent thread is doing step-over with single-step
590 breakpoints, the list of single-step breakpoints are cloned
591 from the parent's. Remove them from the child process.
592 In case of vfork, we'll reinsert them back once vforked
594 if (event_lwp
->bp_reinsert
!= 0
595 && can_software_single_step ())
597 /* The child process is forked and stopped, so it is safe
598 to access its memory without stopping all other threads
599 from other processes. */
600 delete_single_step_breakpoints (child_thr
);
602 gdb_assert (has_single_step_breakpoints (event_thr
));
603 gdb_assert (!has_single_step_breakpoints (child_thr
));
606 /* Report the event. */
611 debug_printf ("HEW: Got clone event "
612 "from LWP %ld, new child is LWP %ld\n",
613 lwpid_of (event_thr
), new_pid
);
615 ptid
= ptid_t (pid_of (event_thr
), new_pid
, 0);
616 new_lwp
= add_lwp (ptid
);
618 /* Either we're going to immediately resume the new thread
619 or leave it stopped. linux_resume_one_lwp is a nop if it
620 thinks the thread is currently running, so set this first
621 before calling linux_resume_one_lwp. */
622 new_lwp
->stopped
= 1;
624 /* If we're suspending all threads, leave this one suspended
625 too. If the fork/clone parent is stepping over a breakpoint,
626 all other threads have been suspended already. Leave the
627 child suspended too. */
628 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
629 || event_lwp
->bp_reinsert
!= 0)
630 new_lwp
->suspended
= 1;
632 /* Normally we will get the pending SIGSTOP. But in some cases
633 we might get another signal delivered to the group first.
634 If we do get another signal, be sure not to lose it. */
635 if (WSTOPSIG (status
) != SIGSTOP
)
637 new_lwp
->stop_expected
= 1;
638 new_lwp
->status_pending_p
= 1;
639 new_lwp
->status_pending
= status
;
641 else if (cs
.report_thread_events
)
643 new_lwp
->waitstatus
.kind
= TARGET_WAITKIND_THREAD_CREATED
;
644 new_lwp
->status_pending_p
= 1;
645 new_lwp
->status_pending
= status
;
649 thread_db_notice_clone (event_thr
, ptid
);
652 /* Don't report the event. */
655 else if (event
== PTRACE_EVENT_VFORK_DONE
)
657 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
659 if (event_lwp
->bp_reinsert
!= 0 && can_software_single_step ())
661 reinsert_single_step_breakpoints (event_thr
);
663 gdb_assert (has_single_step_breakpoints (event_thr
));
666 /* Report the event. */
669 else if (event
== PTRACE_EVENT_EXEC
&& cs
.report_exec_events
)
671 struct process_info
*proc
;
672 std::vector
<int> syscalls_to_catch
;
678 debug_printf ("HEW: Got exec event from LWP %ld\n",
679 lwpid_of (event_thr
));
682 /* Get the event ptid. */
683 event_ptid
= ptid_of (event_thr
);
684 event_pid
= event_ptid
.pid ();
686 /* Save the syscall list from the execing process. */
687 proc
= get_thread_process (event_thr
);
688 syscalls_to_catch
= std::move (proc
->syscalls_to_catch
);
690 /* Delete the execing process and all its threads. */
692 current_thread
= NULL
;
694 /* Create a new process/lwp/thread. */
695 proc
= linux_add_process (event_pid
, 0);
696 event_lwp
= add_lwp (event_ptid
);
697 event_thr
= get_lwp_thread (event_lwp
);
698 gdb_assert (current_thread
== event_thr
);
699 arch_setup_thread (event_thr
);
701 /* Set the event status. */
702 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXECD
;
703 event_lwp
->waitstatus
.value
.execd_pathname
704 = xstrdup (linux_proc_pid_to_exec_file (lwpid_of (event_thr
)));
706 /* Mark the exec status as pending. */
707 event_lwp
->stopped
= 1;
708 event_lwp
->status_pending_p
= 1;
709 event_lwp
->status_pending
= wstat
;
710 event_thr
->last_resume_kind
= resume_continue
;
711 event_thr
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
713 /* Update syscall state in the new lwp, effectively mid-syscall too. */
714 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
716 /* Restore the list to catch. Don't rely on the client, which is free
717 to avoid sending a new list when the architecture doesn't change.
718 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
719 proc
->syscalls_to_catch
= std::move (syscalls_to_catch
);
721 /* Report the event. */
722 *orig_event_lwp
= event_lwp
;
726 internal_error (__FILE__
, __LINE__
, _("unknown ptrace event %d"), event
);
729 /* Return the PC as read from the regcache of LWP, without any
733 get_pc (struct lwp_info
*lwp
)
735 struct thread_info
*saved_thread
;
736 struct regcache
*regcache
;
739 if (the_low_target
.get_pc
== NULL
)
742 saved_thread
= current_thread
;
743 current_thread
= get_lwp_thread (lwp
);
745 regcache
= get_thread_regcache (current_thread
, 1);
746 pc
= (*the_low_target
.get_pc
) (regcache
);
749 debug_printf ("pc is 0x%lx\n", (long) pc
);
751 current_thread
= saved_thread
;
755 /* This function should only be called if LWP got a SYSCALL_SIGTRAP.
756 Fill *SYSNO with the syscall nr trapped. */
759 get_syscall_trapinfo (struct lwp_info
*lwp
, int *sysno
)
761 struct thread_info
*saved_thread
;
762 struct regcache
*regcache
;
764 if (the_low_target
.get_syscall_trapinfo
== NULL
)
766 /* If we cannot get the syscall trapinfo, report an unknown
767 system call number. */
768 *sysno
= UNKNOWN_SYSCALL
;
772 saved_thread
= current_thread
;
773 current_thread
= get_lwp_thread (lwp
);
775 regcache
= get_thread_regcache (current_thread
, 1);
776 (*the_low_target
.get_syscall_trapinfo
) (regcache
, sysno
);
779 debug_printf ("get_syscall_trapinfo sysno %d\n", *sysno
);
781 current_thread
= saved_thread
;
784 static int check_stopped_by_watchpoint (struct lwp_info
*child
);
786 /* Called when the LWP stopped for a signal/trap. If it stopped for a
787 trap check what caused it (breakpoint, watchpoint, trace, etc.),
788 and save the result in the LWP's stop_reason field. If it stopped
789 for a breakpoint, decrement the PC if necessary on the lwp's
790 architecture. Returns true if we now have the LWP's stop PC. */
793 save_stop_reason (struct lwp_info
*lwp
)
796 CORE_ADDR sw_breakpoint_pc
;
797 struct thread_info
*saved_thread
;
798 #if USE_SIGTRAP_SIGINFO
802 if (the_low_target
.get_pc
== NULL
)
806 sw_breakpoint_pc
= pc
- the_low_target
.decr_pc_after_break
;
808 /* breakpoint_at reads from the current thread. */
809 saved_thread
= current_thread
;
810 current_thread
= get_lwp_thread (lwp
);
812 #if USE_SIGTRAP_SIGINFO
813 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
814 (PTRACE_TYPE_ARG3
) 0, &siginfo
) == 0)
816 if (siginfo
.si_signo
== SIGTRAP
)
818 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
)
819 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
821 /* The si_code is ambiguous on this arch -- check debug
823 if (!check_stopped_by_watchpoint (lwp
))
824 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
826 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
))
828 /* If we determine the LWP stopped for a SW breakpoint,
829 trust it. Particularly don't check watchpoint
830 registers, because at least on s390, we'd find
831 stopped-by-watchpoint as long as there's a watchpoint
833 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
835 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
837 /* This can indicate either a hardware breakpoint or
838 hardware watchpoint. Check debug registers. */
839 if (!check_stopped_by_watchpoint (lwp
))
840 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
842 else if (siginfo
.si_code
== TRAP_TRACE
)
844 /* We may have single stepped an instruction that
845 triggered a watchpoint. In that case, on some
846 architectures (such as x86), instead of TRAP_HWBKPT,
847 si_code indicates TRAP_TRACE, and we need to check
848 the debug registers separately. */
849 if (!check_stopped_by_watchpoint (lwp
))
850 lwp
->stop_reason
= TARGET_STOPPED_BY_SINGLE_STEP
;
855 /* We may have just stepped a breakpoint instruction. E.g., in
856 non-stop mode, GDB first tells the thread A to step a range, and
857 then the user inserts a breakpoint inside the range. In that
858 case we need to report the breakpoint PC. */
859 if ((!lwp
->stepping
|| lwp
->stop_pc
== sw_breakpoint_pc
)
860 && (*the_low_target
.breakpoint_at
) (sw_breakpoint_pc
))
861 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
863 if (hardware_breakpoint_inserted_here (pc
))
864 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
866 if (lwp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
)
867 check_stopped_by_watchpoint (lwp
);
870 if (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
)
874 struct thread_info
*thr
= get_lwp_thread (lwp
);
876 debug_printf ("CSBB: %s stopped by software breakpoint\n",
877 target_pid_to_str (ptid_of (thr
)));
880 /* Back up the PC if necessary. */
881 if (pc
!= sw_breakpoint_pc
)
883 struct regcache
*regcache
884 = get_thread_regcache (current_thread
, 1);
885 (*the_low_target
.set_pc
) (regcache
, sw_breakpoint_pc
);
888 /* Update this so we record the correct stop PC below. */
889 pc
= sw_breakpoint_pc
;
891 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
895 struct thread_info
*thr
= get_lwp_thread (lwp
);
897 debug_printf ("CSBB: %s stopped by hardware breakpoint\n",
898 target_pid_to_str (ptid_of (thr
)));
901 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
905 struct thread_info
*thr
= get_lwp_thread (lwp
);
907 debug_printf ("CSBB: %s stopped by hardware watchpoint\n",
908 target_pid_to_str (ptid_of (thr
)));
911 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
)
915 struct thread_info
*thr
= get_lwp_thread (lwp
);
917 debug_printf ("CSBB: %s stopped by trace\n",
918 target_pid_to_str (ptid_of (thr
)));
923 current_thread
= saved_thread
;
927 static struct lwp_info
*
928 add_lwp (ptid_t ptid
)
930 struct lwp_info
*lwp
;
932 lwp
= XCNEW (struct lwp_info
);
934 lwp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
936 lwp
->thread
= add_thread (ptid
, lwp
);
938 if (the_low_target
.new_thread
!= NULL
)
939 the_low_target
.new_thread (lwp
);
944 /* Callback to be used when calling fork_inferior, responsible for
945 actually initiating the tracing of the inferior. */
950 if (ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0,
951 (PTRACE_TYPE_ARG4
) 0) < 0)
952 trace_start_error_with_name ("ptrace");
954 if (setpgid (0, 0) < 0)
955 trace_start_error_with_name ("setpgid");
957 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
958 stdout to stderr so that inferior i/o doesn't corrupt the connection.
959 Also, redirect stdin to /dev/null. */
960 if (remote_connection_is_stdio ())
963 trace_start_error_with_name ("close");
964 if (open ("/dev/null", O_RDONLY
) < 0)
965 trace_start_error_with_name ("open");
967 trace_start_error_with_name ("dup2");
968 if (write (2, "stdin/stdout redirected\n",
969 sizeof ("stdin/stdout redirected\n") - 1) < 0)
971 /* Errors ignored. */;
976 /* Start an inferior process and returns its pid.
977 PROGRAM is the name of the program to be started, and PROGRAM_ARGS
978 are its arguments. */
981 linux_process_target::create_inferior (const char *program
,
982 const std::vector
<char *> &program_args
)
984 client_state
&cs
= get_client_state ();
985 struct lwp_info
*new_lwp
;
990 maybe_disable_address_space_randomization restore_personality
991 (cs
.disable_randomization
);
992 std::string str_program_args
= stringify_argv (program_args
);
994 pid
= fork_inferior (program
,
995 str_program_args
.c_str (),
996 get_environ ()->envp (), linux_ptrace_fun
,
997 NULL
, NULL
, NULL
, NULL
);
1000 linux_add_process (pid
, 0);
1002 ptid
= ptid_t (pid
, pid
, 0);
1003 new_lwp
= add_lwp (ptid
);
1004 new_lwp
->must_set_ptrace_flags
= 1;
1006 post_fork_inferior (pid
, program
);
1011 /* Implement the post_create_inferior target_ops method. */
1014 linux_process_target::post_create_inferior ()
1016 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
1020 if (lwp
->must_set_ptrace_flags
)
1022 struct process_info
*proc
= current_process ();
1023 int options
= linux_low_ptrace_options (proc
->attached
);
1025 linux_enable_event_reporting (lwpid_of (current_thread
), options
);
1026 lwp
->must_set_ptrace_flags
= 0;
1030 /* Attach to an inferior process. Returns 0 on success, ERRNO on
1034 linux_attach_lwp (ptid_t ptid
)
1036 struct lwp_info
*new_lwp
;
1037 int lwpid
= ptid
.lwp ();
1039 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0)
1043 new_lwp
= add_lwp (ptid
);
1045 /* We need to wait for SIGSTOP before being able to make the next
1046 ptrace call on this LWP. */
1047 new_lwp
->must_set_ptrace_flags
= 1;
1049 if (linux_proc_pid_is_stopped (lwpid
))
1052 debug_printf ("Attached to a stopped process\n");
1054 /* The process is definitely stopped. It is in a job control
1055 stop, unless the kernel predates the TASK_STOPPED /
1056 TASK_TRACED distinction, in which case it might be in a
1057 ptrace stop. Make sure it is in a ptrace stop; from there we
1058 can kill it, signal it, et cetera.
1060 First make sure there is a pending SIGSTOP. Since we are
1061 already attached, the process can not transition from stopped
1062 to running without a PTRACE_CONT; so we know this signal will
1063 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1064 probably already in the queue (unless this kernel is old
1065 enough to use TASK_STOPPED for ptrace stops); but since
1066 SIGSTOP is not an RT signal, it can only be queued once. */
1067 kill_lwp (lwpid
, SIGSTOP
);
1069 /* Finally, resume the stopped process. This will deliver the
1070 SIGSTOP (or a higher priority signal, just like normal
1071 PTRACE_ATTACH), which we'll catch later on. */
1072 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1075 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1076 brings it to a halt.
1078 There are several cases to consider here:
1080 1) gdbserver has already attached to the process and is being notified
1081 of a new thread that is being created.
1082 In this case we should ignore that SIGSTOP and resume the
1083 process. This is handled below by setting stop_expected = 1,
1084 and the fact that add_thread sets last_resume_kind ==
1087 2) This is the first thread (the process thread), and we're attaching
1088 to it via attach_inferior.
1089 In this case we want the process thread to stop.
1090 This is handled by having linux_attach set last_resume_kind ==
1091 resume_stop after we return.
1093 If the pid we are attaching to is also the tgid, we attach to and
1094 stop all the existing threads. Otherwise, we attach to pid and
1095 ignore any other threads in the same group as this pid.
1097 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1099 In this case we want the thread to stop.
1100 FIXME: This case is currently not properly handled.
1101 We should wait for the SIGSTOP but don't. Things work apparently
1102 because enough time passes between when we ptrace (ATTACH) and when
1103 gdb makes the next ptrace call on the thread.
1105 On the other hand, if we are currently trying to stop all threads, we
1106 should treat the new thread as if we had sent it a SIGSTOP. This works
1107 because we are guaranteed that the add_lwp call above added us to the
1108 end of the list, and so the new thread has not yet reached
1109 wait_for_sigstop (but will). */
1110 new_lwp
->stop_expected
= 1;
1115 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1116 already attached. Returns true if a new LWP is found, false
1120 attach_proc_task_lwp_callback (ptid_t ptid
)
1122 /* Is this a new thread? */
1123 if (find_thread_ptid (ptid
) == NULL
)
1125 int lwpid
= ptid
.lwp ();
1129 debug_printf ("Found new lwp %d\n", lwpid
);
1131 err
= linux_attach_lwp (ptid
);
1133 /* Be quiet if we simply raced with the thread exiting. EPERM
1134 is returned if the thread's task still exists, and is marked
1135 as exited or zombie, as well as other conditions, so in that
1136 case, confirm the status in /proc/PID/status. */
1138 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1142 debug_printf ("Cannot attach to lwp %d: "
1143 "thread is gone (%d: %s)\n",
1144 lwpid
, err
, safe_strerror (err
));
1150 = linux_ptrace_attach_fail_reason_string (ptid
, err
);
1152 warning (_("Cannot attach to lwp %d: %s"), lwpid
, reason
.c_str ());
1160 static void async_file_mark (void);
1162 /* Attach to PID. If PID is the tgid, attach to it and all
1166 linux_process_target::attach (unsigned long pid
)
1168 struct process_info
*proc
;
1169 struct thread_info
*initial_thread
;
1170 ptid_t ptid
= ptid_t (pid
, pid
, 0);
1173 proc
= linux_add_process (pid
, 1);
1175 /* Attach to PID. We will check for other threads
1177 err
= linux_attach_lwp (ptid
);
1180 remove_process (proc
);
1182 std::string reason
= linux_ptrace_attach_fail_reason_string (ptid
, err
);
1183 error ("Cannot attach to process %ld: %s", pid
, reason
.c_str ());
1186 /* Don't ignore the initial SIGSTOP if we just attached to this
1187 process. It will be collected by wait shortly. */
1188 initial_thread
= find_thread_ptid (ptid_t (pid
, pid
, 0));
1189 initial_thread
->last_resume_kind
= resume_stop
;
1191 /* We must attach to every LWP. If /proc is mounted, use that to
1192 find them now. On the one hand, the inferior may be using raw
1193 clone instead of using pthreads. On the other hand, even if it
1194 is using pthreads, GDB may not be connected yet (thread_db needs
1195 to do symbol lookups, through qSymbol). Also, thread_db walks
1196 structures in the inferior's address space to find the list of
1197 threads/LWPs, and those structures may well be corrupted. Note
1198 that once thread_db is loaded, we'll still use it to list threads
1199 and associate pthread info with each LWP. */
1200 linux_proc_attach_tgid_threads (pid
, attach_proc_task_lwp_callback
);
1202 /* GDB will shortly read the xml target description for this
1203 process, to figure out the process' architecture. But the target
1204 description is only filled in when the first process/thread in
1205 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1206 that now, otherwise, if GDB is fast enough, it could read the
1207 target description _before_ that initial stop. */
1210 struct lwp_info
*lwp
;
1212 ptid_t pid_ptid
= ptid_t (pid
);
1214 lwpid
= wait_for_event_filtered (pid_ptid
, pid_ptid
, &wstat
, __WALL
);
1215 gdb_assert (lwpid
> 0);
1217 lwp
= find_lwp_pid (ptid_t (lwpid
));
1219 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGSTOP
)
1221 lwp
->status_pending_p
= 1;
1222 lwp
->status_pending
= wstat
;
1225 initial_thread
->last_resume_kind
= resume_continue
;
1229 gdb_assert (proc
->tdesc
!= NULL
);
1236 last_thread_of_process_p (int pid
)
1238 bool seen_one
= false;
1240 thread_info
*thread
= find_thread (pid
, [&] (thread_info
*thr_arg
)
1244 /* This is the first thread of this process we see. */
1250 /* This is the second thread of this process we see. */
1255 return thread
== NULL
;
1261 linux_kill_one_lwp (struct lwp_info
*lwp
)
1263 struct thread_info
*thr
= get_lwp_thread (lwp
);
1264 int pid
= lwpid_of (thr
);
1266 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1267 there is no signal context, and ptrace(PTRACE_KILL) (or
1268 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1269 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1270 alternative is to kill with SIGKILL. We only need one SIGKILL
1271 per process, not one for each thread. But since we still support
1272 support debugging programs using raw clone without CLONE_THREAD,
1273 we send one for each thread. For years, we used PTRACE_KILL
1274 only, so we're being a bit paranoid about some old kernels where
1275 PTRACE_KILL might work better (dubious if there are any such, but
1276 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1277 second, and so we're fine everywhere. */
1280 kill_lwp (pid
, SIGKILL
);
1283 int save_errno
= errno
;
1285 debug_printf ("LKL: kill_lwp (SIGKILL) %s, 0, 0 (%s)\n",
1286 target_pid_to_str (ptid_of (thr
)),
1287 save_errno
? safe_strerror (save_errno
) : "OK");
1291 ptrace (PTRACE_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1294 int save_errno
= errno
;
1296 debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
1297 target_pid_to_str (ptid_of (thr
)),
1298 save_errno
? safe_strerror (save_errno
) : "OK");
1302 /* Kill LWP and wait for it to die. */
1305 kill_wait_lwp (struct lwp_info
*lwp
)
1307 struct thread_info
*thr
= get_lwp_thread (lwp
);
1308 int pid
= ptid_of (thr
).pid ();
1309 int lwpid
= ptid_of (thr
).lwp ();
1314 debug_printf ("kwl: killing lwp %d, for pid: %d\n", lwpid
, pid
);
1318 linux_kill_one_lwp (lwp
);
1320 /* Make sure it died. Notes:
1322 - The loop is most likely unnecessary.
1324 - We don't use wait_for_event as that could delete lwps
1325 while we're iterating over them. We're not interested in
1326 any pending status at this point, only in making sure all
1327 wait status on the kernel side are collected until the
1330 - We don't use __WALL here as the __WALL emulation relies on
1331 SIGCHLD, and killing a stopped process doesn't generate
1332 one, nor an exit status.
1334 res
= my_waitpid (lwpid
, &wstat
, 0);
1335 if (res
== -1 && errno
== ECHILD
)
1336 res
= my_waitpid (lwpid
, &wstat
, __WCLONE
);
1337 } while (res
> 0 && WIFSTOPPED (wstat
));
1339 /* Even if it was stopped, the child may have already disappeared.
1340 E.g., if it was killed by SIGKILL. */
1341 if (res
< 0 && errno
!= ECHILD
)
1342 perror_with_name ("kill_wait_lwp");
1345 /* Callback for `for_each_thread'. Kills an lwp of a given process,
1346 except the leader. */
1349 kill_one_lwp_callback (thread_info
*thread
, int pid
)
1351 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1353 /* We avoid killing the first thread here, because of a Linux kernel (at
1354 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1355 the children get a chance to be reaped, it will remain a zombie
1358 if (lwpid_of (thread
) == pid
)
1361 debug_printf ("lkop: is last of process %s\n",
1362 target_pid_to_str (thread
->id
));
1366 kill_wait_lwp (lwp
);
1370 linux_process_target::kill (process_info
*process
)
1372 int pid
= process
->pid
;
1374 /* If we're killing a running inferior, make sure it is stopped
1375 first, as PTRACE_KILL will not work otherwise. */
1376 stop_all_lwps (0, NULL
);
1378 for_each_thread (pid
, [&] (thread_info
*thread
)
1380 kill_one_lwp_callback (thread
, pid
);
1383 /* See the comment in linux_kill_one_lwp. We did not kill the first
1384 thread in the list, so do so now. */
1385 lwp_info
*lwp
= find_lwp_pid (ptid_t (pid
));
1390 debug_printf ("lk_1: cannot find lwp for pid: %d\n",
1394 kill_wait_lwp (lwp
);
1398 /* Since we presently can only stop all lwps of all processes, we
1399 need to unstop lwps of other processes. */
1400 unstop_all_lwps (0, NULL
);
1404 /* Get pending signal of THREAD, for detaching purposes. This is the
1405 signal the thread last stopped for, which we need to deliver to the
1406 thread when detaching, otherwise, it'd be suppressed/lost. */
1409 get_detach_signal (struct thread_info
*thread
)
1411 client_state
&cs
= get_client_state ();
1412 enum gdb_signal signo
= GDB_SIGNAL_0
;
1414 struct lwp_info
*lp
= get_thread_lwp (thread
);
1416 if (lp
->status_pending_p
)
1417 status
= lp
->status_pending
;
1420 /* If the thread had been suspended by gdbserver, and it stopped
1421 cleanly, then it'll have stopped with SIGSTOP. But we don't
1422 want to deliver that SIGSTOP. */
1423 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1424 || thread
->last_status
.value
.sig
== GDB_SIGNAL_0
)
1427 /* Otherwise, we may need to deliver the signal we
1429 status
= lp
->last_status
;
1432 if (!WIFSTOPPED (status
))
1435 debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1436 target_pid_to_str (ptid_of (thread
)));
1440 /* Extended wait statuses aren't real SIGTRAPs. */
1441 if (WSTOPSIG (status
) == SIGTRAP
&& linux_is_extended_waitstatus (status
))
1444 debug_printf ("GPS: lwp %s had stopped with extended "
1445 "status: no pending signal\n",
1446 target_pid_to_str (ptid_of (thread
)));
1450 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1452 if (cs
.program_signals_p
&& !cs
.program_signals
[signo
])
1455 debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1456 target_pid_to_str (ptid_of (thread
)),
1457 gdb_signal_to_string (signo
));
1460 else if (!cs
.program_signals_p
1461 /* If we have no way to know which signals GDB does not
1462 want to have passed to the program, assume
1463 SIGTRAP/SIGINT, which is GDB's default. */
1464 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1467 debug_printf ("GPS: lwp %s had signal %s, "
1468 "but we don't know if we should pass it. "
1469 "Default to not.\n",
1470 target_pid_to_str (ptid_of (thread
)),
1471 gdb_signal_to_string (signo
));
1477 debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1478 target_pid_to_str (ptid_of (thread
)),
1479 gdb_signal_to_string (signo
));
1481 return WSTOPSIG (status
);
1485 /* Detach from LWP. */
1488 linux_detach_one_lwp (struct lwp_info
*lwp
)
1490 struct thread_info
*thread
= get_lwp_thread (lwp
);
1494 /* If there is a pending SIGSTOP, get rid of it. */
1495 if (lwp
->stop_expected
)
1498 debug_printf ("Sending SIGCONT to %s\n",
1499 target_pid_to_str (ptid_of (thread
)));
1501 kill_lwp (lwpid_of (thread
), SIGCONT
);
1502 lwp
->stop_expected
= 0;
1505 /* Pass on any pending signal for this thread. */
1506 sig
= get_detach_signal (thread
);
1508 /* Preparing to resume may try to write registers, and fail if the
1509 lwp is zombie. If that happens, ignore the error. We'll handle
1510 it below, when detach fails with ESRCH. */
1513 /* Flush any pending changes to the process's registers. */
1514 regcache_invalidate_thread (thread
);
1516 /* Finally, let it resume. */
1517 if (the_low_target
.prepare_to_resume
!= NULL
)
1518 the_low_target
.prepare_to_resume (lwp
);
1520 catch (const gdb_exception_error
&ex
)
1522 if (!check_ptrace_stopped_lwp_gone (lwp
))
1526 lwpid
= lwpid_of (thread
);
1527 if (ptrace (PTRACE_DETACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0,
1528 (PTRACE_TYPE_ARG4
) (long) sig
) < 0)
1530 int save_errno
= errno
;
1532 /* We know the thread exists, so ESRCH must mean the lwp is
1533 zombie. This can happen if one of the already-detached
1534 threads exits the whole thread group. In that case we're
1535 still attached, and must reap the lwp. */
1536 if (save_errno
== ESRCH
)
1540 ret
= my_waitpid (lwpid
, &status
, __WALL
);
1543 warning (_("Couldn't reap LWP %d while detaching: %s"),
1544 lwpid
, safe_strerror (errno
));
1546 else if (!WIFEXITED (status
) && !WIFSIGNALED (status
))
1548 warning (_("Reaping LWP %d while detaching "
1549 "returned unexpected status 0x%x"),
1555 error (_("Can't detach %s: %s"),
1556 target_pid_to_str (ptid_of (thread
)),
1557 safe_strerror (save_errno
));
1560 else if (debug_threads
)
1562 debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)\n",
1563 target_pid_to_str (ptid_of (thread
)),
1570 /* Callback for for_each_thread. Detaches from non-leader threads of a
1574 linux_detach_lwp_callback (thread_info
*thread
)
1576 /* We don't actually detach from the thread group leader just yet.
1577 If the thread group exits, we must reap the zombie clone lwps
1578 before we're able to reap the leader. */
1579 if (thread
->id
.pid () == thread
->id
.lwp ())
1582 lwp_info
*lwp
= get_thread_lwp (thread
);
1583 linux_detach_one_lwp (lwp
);
1587 linux_process_target::detach (process_info
*process
)
1589 struct lwp_info
*main_lwp
;
1591 /* As there's a step over already in progress, let it finish first,
1592 otherwise nesting a stabilize_threads operation on top gets real
1594 complete_ongoing_step_over ();
1596 /* Stop all threads before detaching. First, ptrace requires that
1597 the thread is stopped to successfully detach. Second, thread_db
1598 may need to uninstall thread event breakpoints from memory, which
1599 only works with a stopped process anyway. */
1600 stop_all_lwps (0, NULL
);
1602 #ifdef USE_THREAD_DB
1603 thread_db_detach (process
);
1606 /* Stabilize threads (move out of jump pads). */
1607 target_stabilize_threads ();
1609 /* Detach from the clone lwps first. If the thread group exits just
1610 while we're detaching, we must reap the clone lwps before we're
1611 able to reap the leader. */
1612 for_each_thread (process
->pid
, linux_detach_lwp_callback
);
1614 main_lwp
= find_lwp_pid (ptid_t (process
->pid
));
1615 linux_detach_one_lwp (main_lwp
);
1619 /* Since we presently can only stop all lwps of all processes, we
1620 need to unstop lwps of other processes. */
1621 unstop_all_lwps (0, NULL
);
1625 /* Remove all LWPs that belong to process PROC from the lwp list. */
1628 linux_process_target::mourn (process_info
*process
)
1630 struct process_info_private
*priv
;
1632 #ifdef USE_THREAD_DB
1633 thread_db_mourn (process
);
1636 for_each_thread (process
->pid
, [] (thread_info
*thread
)
1638 delete_lwp (get_thread_lwp (thread
));
1641 /* Freeing all private data. */
1642 priv
= process
->priv
;
1643 if (the_low_target
.delete_process
!= NULL
)
1644 the_low_target
.delete_process (priv
->arch_private
);
1646 gdb_assert (priv
->arch_private
== NULL
);
1648 process
->priv
= NULL
;
1650 remove_process (process
);
1654 linux_process_target::join (int pid
)
1659 ret
= my_waitpid (pid
, &status
, 0);
1660 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1662 } while (ret
!= -1 || errno
!= ECHILD
);
1665 /* Return true if the given thread is still alive. */
1668 linux_process_target::thread_alive (ptid_t ptid
)
1670 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1672 /* We assume we always know if a thread exits. If a whole process
1673 exited but we still haven't been able to report it to GDB, we'll
1674 hold on to the last lwp of the dead process. */
1676 return !lwp_is_marked_dead (lwp
);
1681 /* Return 1 if this lwp still has an interesting status pending. If
1682 not (e.g., it had stopped for a breakpoint that is gone), return
1686 thread_still_has_status_pending_p (struct thread_info
*thread
)
1688 struct lwp_info
*lp
= get_thread_lwp (thread
);
1690 if (!lp
->status_pending_p
)
1693 if (thread
->last_resume_kind
!= resume_stop
1694 && (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1695 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
))
1697 struct thread_info
*saved_thread
;
1701 gdb_assert (lp
->last_status
!= 0);
1705 saved_thread
= current_thread
;
1706 current_thread
= thread
;
1708 if (pc
!= lp
->stop_pc
)
1711 debug_printf ("PC of %ld changed\n",
1716 #if !USE_SIGTRAP_SIGINFO
1717 else if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1718 && !(*the_low_target
.breakpoint_at
) (pc
))
1721 debug_printf ("previous SW breakpoint of %ld gone\n",
1725 else if (lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
1726 && !hardware_breakpoint_inserted_here (pc
))
1729 debug_printf ("previous HW breakpoint of %ld gone\n",
1735 current_thread
= saved_thread
;
1740 debug_printf ("discarding pending breakpoint status\n");
1741 lp
->status_pending_p
= 0;
1749 /* Returns true if LWP is resumed from the client's perspective. */
1752 lwp_resumed (struct lwp_info
*lwp
)
1754 struct thread_info
*thread
= get_lwp_thread (lwp
);
1756 if (thread
->last_resume_kind
!= resume_stop
)
1759 /* Did gdb send us a `vCont;t', but we haven't reported the
1760 corresponding stop to gdb yet? If so, the thread is still
1761 resumed/running from gdb's perspective. */
1762 if (thread
->last_resume_kind
== resume_stop
1763 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
1769 /* Return true if this lwp has an interesting status pending. */
1771 status_pending_p_callback (thread_info
*thread
, ptid_t ptid
)
1773 struct lwp_info
*lp
= get_thread_lwp (thread
);
1775 /* Check if we're only interested in events from a specific process
1776 or a specific LWP. */
1777 if (!thread
->id
.matches (ptid
))
1780 if (!lwp_resumed (lp
))
1783 if (lp
->status_pending_p
1784 && !thread_still_has_status_pending_p (thread
))
1786 linux_resume_one_lwp (lp
, lp
->stepping
, GDB_SIGNAL_0
, NULL
);
1790 return lp
->status_pending_p
;
1794 find_lwp_pid (ptid_t ptid
)
1796 thread_info
*thread
= find_thread ([&] (thread_info
*thr_arg
)
1798 int lwp
= ptid
.lwp () != 0 ? ptid
.lwp () : ptid
.pid ();
1799 return thr_arg
->id
.lwp () == lwp
;
1805 return get_thread_lwp (thread
);
1808 /* Return the number of known LWPs in the tgid given by PID. */
1815 for_each_thread (pid
, [&] (thread_info
*thread
)
1823 /* See nat/linux-nat.h. */
1826 iterate_over_lwps (ptid_t filter
,
1827 gdb::function_view
<iterate_over_lwps_ftype
> callback
)
1829 thread_info
*thread
= find_thread (filter
, [&] (thread_info
*thr_arg
)
1831 lwp_info
*lwp
= get_thread_lwp (thr_arg
);
1833 return callback (lwp
);
1839 return get_thread_lwp (thread
);
1842 /* Detect zombie thread group leaders, and "exit" them. We can't reap
1843 their exits until all other threads in the group have exited. */
1846 check_zombie_leaders (void)
1848 for_each_process ([] (process_info
*proc
) {
1849 pid_t leader_pid
= pid_of (proc
);
1850 struct lwp_info
*leader_lp
;
1852 leader_lp
= find_lwp_pid (ptid_t (leader_pid
));
1855 debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1856 "num_lwps=%d, zombie=%d\n",
1857 leader_pid
, leader_lp
!= NULL
, num_lwps (leader_pid
),
1858 linux_proc_pid_is_zombie (leader_pid
));
1860 if (leader_lp
!= NULL
&& !leader_lp
->stopped
1861 /* Check if there are other threads in the group, as we may
1862 have raced with the inferior simply exiting. */
1863 && !last_thread_of_process_p (leader_pid
)
1864 && linux_proc_pid_is_zombie (leader_pid
))
1866 /* A leader zombie can mean one of two things:
1868 - It exited, and there's an exit status pending
1869 available, or only the leader exited (not the whole
1870 program). In the latter case, we can't waitpid the
1871 leader's exit status until all other threads are gone.
1873 - There are 3 or more threads in the group, and a thread
1874 other than the leader exec'd. On an exec, the Linux
1875 kernel destroys all other threads (except the execing
1876 one) in the thread group, and resets the execing thread's
1877 tid to the tgid. No exit notification is sent for the
1878 execing thread -- from the ptracer's perspective, it
1879 appears as though the execing thread just vanishes.
1880 Until we reap all other threads except the leader and the
1881 execing thread, the leader will be zombie, and the
1882 execing thread will be in `D (disc sleep)'. As soon as
1883 all other threads are reaped, the execing thread changes
1884 it's tid to the tgid, and the previous (zombie) leader
1885 vanishes, giving place to the "new" leader. We could try
1886 distinguishing the exit and exec cases, by waiting once
1887 more, and seeing if something comes out, but it doesn't
1888 sound useful. The previous leader _does_ go away, and
1889 we'll re-add the new one once we see the exec event
1890 (which is just the same as what would happen if the
1891 previous leader did exit voluntarily before some other
1895 debug_printf ("CZL: Thread group leader %d zombie "
1896 "(it exited, or another thread execd).\n",
1899 delete_lwp (leader_lp
);
1904 /* Callback for `find_thread'. Returns the first LWP that is not
1908 not_stopped_callback (thread_info
*thread
, ptid_t filter
)
1910 if (!thread
->id
.matches (filter
))
1913 lwp_info
*lwp
= get_thread_lwp (thread
);
1915 return !lwp
->stopped
;
1918 /* Increment LWP's suspend count. */
1921 lwp_suspended_inc (struct lwp_info
*lwp
)
1925 if (debug_threads
&& lwp
->suspended
> 4)
1927 struct thread_info
*thread
= get_lwp_thread (lwp
);
1929 debug_printf ("LWP %ld has a suspiciously high suspend count,"
1930 " suspended=%d\n", lwpid_of (thread
), lwp
->suspended
);
1934 /* Decrement LWP's suspend count. */
1937 lwp_suspended_decr (struct lwp_info
*lwp
)
1941 if (lwp
->suspended
< 0)
1943 struct thread_info
*thread
= get_lwp_thread (lwp
);
1945 internal_error (__FILE__
, __LINE__
,
1946 "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread
),
1951 /* This function should only be called if the LWP got a SIGTRAP.
1953 Handle any tracepoint steps or hits. Return true if a tracepoint
1954 event was handled, 0 otherwise. */
1957 handle_tracepoints (struct lwp_info
*lwp
)
1959 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1960 int tpoint_related_event
= 0;
1962 gdb_assert (lwp
->suspended
== 0);
1964 /* If this tracepoint hit causes a tracing stop, we'll immediately
1965 uninsert tracepoints. To do this, we temporarily pause all
1966 threads, unpatch away, and then unpause threads. We need to make
1967 sure the unpausing doesn't resume LWP too. */
1968 lwp_suspended_inc (lwp
);
1970 /* And we need to be sure that any all-threads-stopping doesn't try
1971 to move threads out of the jump pads, as it could deadlock the
1972 inferior (LWP could be in the jump pad, maybe even holding the
1975 /* Do any necessary step collect actions. */
1976 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1978 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1980 /* See if we just hit a tracepoint and do its main collect
1982 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1984 lwp_suspended_decr (lwp
);
1986 gdb_assert (lwp
->suspended
== 0);
1987 gdb_assert (!stabilizing_threads
1988 || (lwp
->collecting_fast_tracepoint
1989 != fast_tpoint_collect_result::not_collecting
));
1991 if (tpoint_related_event
)
1994 debug_printf ("got a tracepoint event\n");
2001 /* Convenience wrapper. Returns information about LWP's fast tracepoint
2002 collection status. */
2004 static fast_tpoint_collect_result
2005 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
2006 struct fast_tpoint_collect_status
*status
)
2008 CORE_ADDR thread_area
;
2009 struct thread_info
*thread
= get_lwp_thread (lwp
);
2011 if (the_low_target
.get_thread_area
== NULL
)
2012 return fast_tpoint_collect_result::not_collecting
;
2014 /* Get the thread area address. This is used to recognize which
2015 thread is which when tracing with the in-process agent library.
2016 We don't read anything from the address, and treat it as opaque;
2017 it's the address itself that we assume is unique per-thread. */
2018 if ((*the_low_target
.get_thread_area
) (lwpid_of (thread
), &thread_area
) == -1)
2019 return fast_tpoint_collect_result::not_collecting
;
2021 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
2025 linux_process_target::maybe_move_out_of_jump_pad (lwp_info
*lwp
, int *wstat
)
2027 struct thread_info
*saved_thread
;
2029 saved_thread
= current_thread
;
2030 current_thread
= get_lwp_thread (lwp
);
2033 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
2034 && supports_fast_tracepoints ()
2035 && agent_loaded_p ())
2037 struct fast_tpoint_collect_status status
;
2040 debug_printf ("Checking whether LWP %ld needs to move out of the "
2042 lwpid_of (current_thread
));
2044 fast_tpoint_collect_result r
2045 = linux_fast_tracepoint_collecting (lwp
, &status
);
2048 || (WSTOPSIG (*wstat
) != SIGILL
2049 && WSTOPSIG (*wstat
) != SIGFPE
2050 && WSTOPSIG (*wstat
) != SIGSEGV
2051 && WSTOPSIG (*wstat
) != SIGBUS
))
2053 lwp
->collecting_fast_tracepoint
= r
;
2055 if (r
!= fast_tpoint_collect_result::not_collecting
)
2057 if (r
== fast_tpoint_collect_result::before_insn
2058 && lwp
->exit_jump_pad_bkpt
== NULL
)
2060 /* Haven't executed the original instruction yet.
2061 Set breakpoint there, and wait till it's hit,
2062 then single-step until exiting the jump pad. */
2063 lwp
->exit_jump_pad_bkpt
2064 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
2068 debug_printf ("Checking whether LWP %ld needs to move out of "
2069 "the jump pad...it does\n",
2070 lwpid_of (current_thread
));
2071 current_thread
= saved_thread
;
2078 /* If we get a synchronous signal while collecting, *and*
2079 while executing the (relocated) original instruction,
2080 reset the PC to point at the tpoint address, before
2081 reporting to GDB. Otherwise, it's an IPA lib bug: just
2082 report the signal to GDB, and pray for the best. */
2084 lwp
->collecting_fast_tracepoint
2085 = fast_tpoint_collect_result::not_collecting
;
2087 if (r
!= fast_tpoint_collect_result::not_collecting
2088 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
2089 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
2092 struct regcache
*regcache
;
2094 /* The si_addr on a few signals references the address
2095 of the faulting instruction. Adjust that as
2097 if ((WSTOPSIG (*wstat
) == SIGILL
2098 || WSTOPSIG (*wstat
) == SIGFPE
2099 || WSTOPSIG (*wstat
) == SIGBUS
2100 || WSTOPSIG (*wstat
) == SIGSEGV
)
2101 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
2102 (PTRACE_TYPE_ARG3
) 0, &info
) == 0
2103 /* Final check just to make sure we don't clobber
2104 the siginfo of non-kernel-sent signals. */
2105 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
2107 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
2108 ptrace (PTRACE_SETSIGINFO
, lwpid_of (current_thread
),
2109 (PTRACE_TYPE_ARG3
) 0, &info
);
2112 regcache
= get_thread_regcache (current_thread
, 1);
2113 (*the_low_target
.set_pc
) (regcache
, status
.tpoint_addr
);
2114 lwp
->stop_pc
= status
.tpoint_addr
;
2116 /* Cancel any fast tracepoint lock this thread was
2118 force_unlock_trace_buffer ();
2121 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
2124 debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
2125 "stopping all threads momentarily.\n");
2127 stop_all_lwps (1, lwp
);
2129 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
2130 lwp
->exit_jump_pad_bkpt
= NULL
;
2132 unstop_all_lwps (1, lwp
);
2134 gdb_assert (lwp
->suspended
>= 0);
2140 debug_printf ("Checking whether LWP %ld needs to move out of the "
2142 lwpid_of (current_thread
));
2144 current_thread
= saved_thread
;
2148 /* Enqueue one signal in the "signals to report later when out of the
2152 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2154 struct pending_signals
*p_sig
;
2155 struct thread_info
*thread
= get_lwp_thread (lwp
);
2158 debug_printf ("Deferring signal %d for LWP %ld.\n",
2159 WSTOPSIG (*wstat
), lwpid_of (thread
));
2163 struct pending_signals
*sig
;
2165 for (sig
= lwp
->pending_signals_to_report
;
2168 debug_printf (" Already queued %d\n",
2171 debug_printf (" (no more currently queued signals)\n");
2174 /* Don't enqueue non-RT signals if they are already in the deferred
2175 queue. (SIGSTOP being the easiest signal to see ending up here
2177 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
2179 struct pending_signals
*sig
;
2181 for (sig
= lwp
->pending_signals_to_report
;
2185 if (sig
->signal
== WSTOPSIG (*wstat
))
2188 debug_printf ("Not requeuing already queued non-RT signal %d"
2197 p_sig
= XCNEW (struct pending_signals
);
2198 p_sig
->prev
= lwp
->pending_signals_to_report
;
2199 p_sig
->signal
= WSTOPSIG (*wstat
);
2201 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2204 lwp
->pending_signals_to_report
= p_sig
;
2207 /* Dequeue one signal from the "signals to report later when out of
2208 the jump pad" list. */
2211 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2213 struct thread_info
*thread
= get_lwp_thread (lwp
);
2215 if (lwp
->pending_signals_to_report
!= NULL
)
2217 struct pending_signals
**p_sig
;
2219 p_sig
= &lwp
->pending_signals_to_report
;
2220 while ((*p_sig
)->prev
!= NULL
)
2221 p_sig
= &(*p_sig
)->prev
;
2223 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
2224 if ((*p_sig
)->info
.si_signo
!= 0)
2225 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2231 debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
2232 WSTOPSIG (*wstat
), lwpid_of (thread
));
2236 struct pending_signals
*sig
;
2238 for (sig
= lwp
->pending_signals_to_report
;
2241 debug_printf (" Still queued %d\n",
2244 debug_printf (" (no more queued signals)\n");
2253 /* Fetch the possibly triggered data watchpoint info and store it in
2256 On some archs, like x86, that use debug registers to set
2257 watchpoints, it's possible that the way to know which watched
2258 address trapped, is to check the register that is used to select
2259 which address to watch. Problem is, between setting the watchpoint
2260 and reading back which data address trapped, the user may change
2261 the set of watchpoints, and, as a consequence, GDB changes the
2262 debug registers in the inferior. To avoid reading back a stale
2263 stopped-data-address when that happens, we cache in LP the fact
2264 that a watchpoint trapped, and the corresponding data address, as
2265 soon as we see CHILD stop with a SIGTRAP. If GDB changes the debug
2266 registers meanwhile, we have the cached data we can rely on. */
2269 check_stopped_by_watchpoint (struct lwp_info
*child
)
2271 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
2273 struct thread_info
*saved_thread
;
2275 saved_thread
= current_thread
;
2276 current_thread
= get_lwp_thread (child
);
2278 if (the_low_target
.stopped_by_watchpoint ())
2280 child
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2282 if (the_low_target
.stopped_data_address
!= NULL
)
2283 child
->stopped_data_address
2284 = the_low_target
.stopped_data_address ();
2286 child
->stopped_data_address
= 0;
2289 current_thread
= saved_thread
;
2292 return child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2295 /* Return the ptrace options that we want to try to enable. */
2298 linux_low_ptrace_options (int attached
)
2300 client_state
&cs
= get_client_state ();
2304 options
|= PTRACE_O_EXITKILL
;
2306 if (cs
.report_fork_events
)
2307 options
|= PTRACE_O_TRACEFORK
;
2309 if (cs
.report_vfork_events
)
2310 options
|= (PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEVFORKDONE
);
2312 if (cs
.report_exec_events
)
2313 options
|= PTRACE_O_TRACEEXEC
;
2315 options
|= PTRACE_O_TRACESYSGOOD
;
2321 linux_process_target::filter_event (int lwpid
, int wstat
)
2323 client_state
&cs
= get_client_state ();
2324 struct lwp_info
*child
;
2325 struct thread_info
*thread
;
2326 int have_stop_pc
= 0;
2328 child
= find_lwp_pid (ptid_t (lwpid
));
2330 /* Check for stop events reported by a process we didn't already
2331 know about - anything not already in our LWP list.
2333 If we're expecting to receive stopped processes after
2334 fork, vfork, and clone events, then we'll just add the
2335 new one to our list and go back to waiting for the event
2336 to be reported - the stopped process might be returned
2337 from waitpid before or after the event is.
2339 But note the case of a non-leader thread exec'ing after the
2340 leader having exited, and gone from our lists (because
2341 check_zombie_leaders deleted it). The non-leader thread
2342 changes its tid to the tgid. */
2344 if (WIFSTOPPED (wstat
) && child
== NULL
&& WSTOPSIG (wstat
) == SIGTRAP
2345 && linux_ptrace_get_extended_event (wstat
) == PTRACE_EVENT_EXEC
)
2349 /* A multi-thread exec after we had seen the leader exiting. */
2352 debug_printf ("LLW: Re-adding thread group leader LWP %d"
2353 "after exec.\n", lwpid
);
2356 child_ptid
= ptid_t (lwpid
, lwpid
, 0);
2357 child
= add_lwp (child_ptid
);
2359 current_thread
= child
->thread
;
2362 /* If we didn't find a process, one of two things presumably happened:
2363 - A process we started and then detached from has exited. Ignore it.
2364 - A process we are controlling has forked and the new child's stop
2365 was reported to us by the kernel. Save its PID. */
2366 if (child
== NULL
&& WIFSTOPPED (wstat
))
2368 add_to_pid_list (&stopped_pids
, lwpid
, wstat
);
2371 else if (child
== NULL
)
2374 thread
= get_lwp_thread (child
);
2378 child
->last_status
= wstat
;
2380 /* Check if the thread has exited. */
2381 if ((WIFEXITED (wstat
) || WIFSIGNALED (wstat
)))
2384 debug_printf ("LLFE: %d exited.\n", lwpid
);
2386 if (finish_step_over (child
))
2388 /* Unsuspend all other LWPs, and set them back running again. */
2389 unsuspend_all_lwps (child
);
2392 /* If there is at least one more LWP, then the exit signal was
2393 not the end of the debugged application and should be
2394 ignored, unless GDB wants to hear about thread exits. */
2395 if (cs
.report_thread_events
2396 || last_thread_of_process_p (pid_of (thread
)))
2398 /* Since events are serialized to GDB core, and we can't
2399 report this one right now. Leave the status pending for
2400 the next time we're able to report it. */
2401 mark_lwp_dead (child
, wstat
);
2411 gdb_assert (WIFSTOPPED (wstat
));
2413 if (WIFSTOPPED (wstat
))
2415 struct process_info
*proc
;
2417 /* Architecture-specific setup after inferior is running. */
2418 proc
= find_process_pid (pid_of (thread
));
2419 if (proc
->tdesc
== NULL
)
2423 /* This needs to happen after we have attached to the
2424 inferior and it is stopped for the first time, but
2425 before we access any inferior registers. */
2426 arch_setup_thread (thread
);
2430 /* The process is started, but GDBserver will do
2431 architecture-specific setup after the program stops at
2432 the first instruction. */
2433 child
->status_pending_p
= 1;
2434 child
->status_pending
= wstat
;
2440 if (WIFSTOPPED (wstat
) && child
->must_set_ptrace_flags
)
2442 struct process_info
*proc
= find_process_pid (pid_of (thread
));
2443 int options
= linux_low_ptrace_options (proc
->attached
);
2445 linux_enable_event_reporting (lwpid
, options
);
2446 child
->must_set_ptrace_flags
= 0;
2449 /* Always update syscall_state, even if it will be filtered later. */
2450 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SYSCALL_SIGTRAP
)
2452 child
->syscall_state
2453 = (child
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2454 ? TARGET_WAITKIND_SYSCALL_RETURN
2455 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2459 /* Almost all other ptrace-stops are known to be outside of system
2460 calls, with further exceptions in handle_extended_wait. */
2461 child
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2464 /* Be careful to not overwrite stop_pc until save_stop_reason is
2466 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
2467 && linux_is_extended_waitstatus (wstat
))
2469 child
->stop_pc
= get_pc (child
);
2470 if (handle_extended_wait (&child
, wstat
))
2472 /* The event has been handled, so just return without
2478 if (linux_wstatus_maybe_breakpoint (wstat
))
2480 if (save_stop_reason (child
))
2485 child
->stop_pc
= get_pc (child
);
2487 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGSTOP
2488 && child
->stop_expected
)
2491 debug_printf ("Expected stop.\n");
2492 child
->stop_expected
= 0;
2494 if (thread
->last_resume_kind
== resume_stop
)
2496 /* We want to report the stop to the core. Treat the
2497 SIGSTOP as a normal event. */
2499 debug_printf ("LLW: resume_stop SIGSTOP caught for %s.\n",
2500 target_pid_to_str (ptid_of (thread
)));
2502 else if (stopping_threads
!= NOT_STOPPING_THREADS
)
2504 /* Stopping threads. We don't want this SIGSTOP to end up
2507 debug_printf ("LLW: SIGSTOP caught for %s "
2508 "while stopping threads.\n",
2509 target_pid_to_str (ptid_of (thread
)));
2514 /* This is a delayed SIGSTOP. Filter out the event. */
2516 debug_printf ("LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2517 child
->stepping
? "step" : "continue",
2518 target_pid_to_str (ptid_of (thread
)));
2520 linux_resume_one_lwp (child
, child
->stepping
, 0, NULL
);
2525 child
->status_pending_p
= 1;
2526 child
->status_pending
= wstat
;
2530 /* Return true if THREAD is doing hardware single step. */
2533 maybe_hw_step (struct thread_info
*thread
)
2535 if (can_hardware_single_step ())
2539 /* GDBserver must insert single-step breakpoint for software
2541 gdb_assert (has_single_step_breakpoints (thread
));
2546 /* Resume LWPs that are currently stopped without any pending status
2547 to report, but are resumed from the core's perspective. */
2550 resume_stopped_resumed_lwps (thread_info
*thread
)
2552 struct lwp_info
*lp
= get_thread_lwp (thread
);
2556 && !lp
->status_pending_p
2557 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2561 if (thread
->last_resume_kind
== resume_step
)
2562 step
= maybe_hw_step (thread
);
2565 debug_printf ("RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
2566 target_pid_to_str (ptid_of (thread
)),
2567 paddress (lp
->stop_pc
),
2570 linux_resume_one_lwp (lp
, step
, GDB_SIGNAL_0
, NULL
);
2575 linux_process_target::wait_for_event_filtered (ptid_t wait_ptid
,
2577 int *wstatp
, int options
)
2579 struct thread_info
*event_thread
;
2580 struct lwp_info
*event_child
, *requested_child
;
2581 sigset_t block_mask
, prev_mask
;
2584 /* N.B. event_thread points to the thread_info struct that contains
2585 event_child. Keep them in sync. */
2586 event_thread
= NULL
;
2588 requested_child
= NULL
;
2590 /* Check for a lwp with a pending status. */
2592 if (filter_ptid
== minus_one_ptid
|| filter_ptid
.is_pid ())
2594 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2596 return status_pending_p_callback (thread
, filter_ptid
);
2599 if (event_thread
!= NULL
)
2600 event_child
= get_thread_lwp (event_thread
);
2601 if (debug_threads
&& event_thread
)
2602 debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread
));
2604 else if (filter_ptid
!= null_ptid
)
2606 requested_child
= find_lwp_pid (filter_ptid
);
2608 if (stopping_threads
== NOT_STOPPING_THREADS
2609 && requested_child
->status_pending_p
2610 && (requested_child
->collecting_fast_tracepoint
2611 != fast_tpoint_collect_result::not_collecting
))
2613 enqueue_one_deferred_signal (requested_child
,
2614 &requested_child
->status_pending
);
2615 requested_child
->status_pending_p
= 0;
2616 requested_child
->status_pending
= 0;
2617 linux_resume_one_lwp (requested_child
, 0, 0, NULL
);
2620 if (requested_child
->suspended
2621 && requested_child
->status_pending_p
)
2623 internal_error (__FILE__
, __LINE__
,
2624 "requesting an event out of a"
2625 " suspended child?");
2628 if (requested_child
->status_pending_p
)
2630 event_child
= requested_child
;
2631 event_thread
= get_lwp_thread (event_child
);
2635 if (event_child
!= NULL
)
2638 debug_printf ("Got an event from pending child %ld (%04x)\n",
2639 lwpid_of (event_thread
), event_child
->status_pending
);
2640 *wstatp
= event_child
->status_pending
;
2641 event_child
->status_pending_p
= 0;
2642 event_child
->status_pending
= 0;
2643 current_thread
= event_thread
;
2644 return lwpid_of (event_thread
);
2647 /* But if we don't find a pending event, we'll have to wait.
2649 We only enter this loop if no process has a pending wait status.
2650 Thus any action taken in response to a wait status inside this
2651 loop is responding as soon as we detect the status, not after any
2654 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2655 all signals while here. */
2656 sigfillset (&block_mask
);
2657 gdb_sigmask (SIG_BLOCK
, &block_mask
, &prev_mask
);
2659 /* Always pull all events out of the kernel. We'll randomly select
2660 an event LWP out of all that have events, to prevent
2662 while (event_child
== NULL
)
2666 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2669 - If the thread group leader exits while other threads in the
2670 thread group still exist, waitpid(TGID, ...) hangs. That
2671 waitpid won't return an exit status until the other threads
2672 in the group are reaped.
2674 - When a non-leader thread execs, that thread just vanishes
2675 without reporting an exit (so we'd hang if we waited for it
2676 explicitly in that case). The exec event is reported to
2679 ret
= my_waitpid (-1, wstatp
, options
| WNOHANG
);
2682 debug_printf ("LWFE: waitpid(-1, ...) returned %d, %s\n",
2683 ret
, errno
? safe_strerror (errno
) : "ERRNO-OK");
2689 debug_printf ("LLW: waitpid %ld received %s\n",
2690 (long) ret
, status_to_str (*wstatp
));
2693 /* Filter all events. IOW, leave all events pending. We'll
2694 randomly select an event LWP out of all that have events
2696 filter_event (ret
, *wstatp
);
2697 /* Retry until nothing comes out of waitpid. A single
2698 SIGCHLD can indicate more than one child stopped. */
2702 /* Now that we've pulled all events out of the kernel, resume
2703 LWPs that don't have an interesting event to report. */
2704 if (stopping_threads
== NOT_STOPPING_THREADS
)
2705 for_each_thread (resume_stopped_resumed_lwps
);
2707 /* ... and find an LWP with a status to report to the core, if
2709 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2711 return status_pending_p_callback (thread
, filter_ptid
);
2714 if (event_thread
!= NULL
)
2716 event_child
= get_thread_lwp (event_thread
);
2717 *wstatp
= event_child
->status_pending
;
2718 event_child
->status_pending_p
= 0;
2719 event_child
->status_pending
= 0;
2723 /* Check for zombie thread group leaders. Those can't be reaped
2724 until all other threads in the thread group are. */
2725 check_zombie_leaders ();
2727 auto not_stopped
= [&] (thread_info
*thread
)
2729 return not_stopped_callback (thread
, wait_ptid
);
2732 /* If there are no resumed children left in the set of LWPs we
2733 want to wait for, bail. We can't just block in
2734 waitpid/sigsuspend, because lwps might have been left stopped
2735 in trace-stop state, and we'd be stuck forever waiting for
2736 their status to change (which would only happen if we resumed
2737 them). Even if WNOHANG is set, this return code is preferred
2738 over 0 (below), as it is more detailed. */
2739 if (find_thread (not_stopped
) == NULL
)
2742 debug_printf ("LLW: exit (no unwaited-for LWP)\n");
2743 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2747 /* No interesting event to report to the caller. */
2748 if ((options
& WNOHANG
))
2751 debug_printf ("WNOHANG set, no event found\n");
2753 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2757 /* Block until we get an event reported with SIGCHLD. */
2759 debug_printf ("sigsuspend'ing\n");
2761 sigsuspend (&prev_mask
);
2762 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2766 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2768 current_thread
= event_thread
;
2770 return lwpid_of (event_thread
);
2774 linux_process_target::wait_for_event (ptid_t ptid
, int *wstatp
, int options
)
2776 return wait_for_event_filtered (ptid
, ptid
, wstatp
, options
);
2779 /* Select one LWP out of those that have events pending. */
2782 select_event_lwp (struct lwp_info
**orig_lp
)
2784 struct thread_info
*event_thread
= NULL
;
2786 /* In all-stop, give preference to the LWP that is being
2787 single-stepped. There will be at most one, and it's the LWP that
2788 the core is most interested in. If we didn't do this, then we'd
2789 have to handle pending step SIGTRAPs somehow in case the core
2790 later continues the previously-stepped thread, otherwise we'd
2791 report the pending SIGTRAP, and the core, not having stepped the
2792 thread, wouldn't understand what the trap was for, and therefore
2793 would report it to the user as a random signal. */
2796 event_thread
= find_thread ([] (thread_info
*thread
)
2798 lwp_info
*lp
= get_thread_lwp (thread
);
2800 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2801 && thread
->last_resume_kind
== resume_step
2802 && lp
->status_pending_p
);
2805 if (event_thread
!= NULL
)
2808 debug_printf ("SEL: Select single-step %s\n",
2809 target_pid_to_str (ptid_of (event_thread
)));
2812 if (event_thread
== NULL
)
2814 /* No single-stepping LWP. Select one at random, out of those
2815 which have had events. */
2817 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2819 lwp_info
*lp
= get_thread_lwp (thread
);
2821 /* Only resumed LWPs that have an event pending. */
2822 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2823 && lp
->status_pending_p
);
2827 if (event_thread
!= NULL
)
2829 struct lwp_info
*event_lp
= get_thread_lwp (event_thread
);
2831 /* Switch the event LWP. */
2832 *orig_lp
= event_lp
;
2836 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2840 unsuspend_all_lwps (struct lwp_info
*except
)
2842 for_each_thread ([&] (thread_info
*thread
)
2844 lwp_info
*lwp
= get_thread_lwp (thread
);
2847 lwp_suspended_decr (lwp
);
2851 static bool stuck_in_jump_pad_callback (thread_info
*thread
);
2852 static bool lwp_running (thread_info
*thread
);
2854 /* Stabilize threads (move out of jump pads).
2856 If a thread is midway collecting a fast tracepoint, we need to
2857 finish the collection and move it out of the jump pad before
2858 reporting the signal.
2860 This avoids recursion while collecting (when a signal arrives
2861 midway, and the signal handler itself collects), which would trash
2862 the trace buffer. In case the user set a breakpoint in a signal
2863 handler, this avoids the backtrace showing the jump pad, etc..
2864 Most importantly, there are certain things we can't do safely if
2865 threads are stopped in a jump pad (or in its callee's). For
2868 - starting a new trace run. A thread still collecting the
2869 previous run, could trash the trace buffer when resumed. The trace
2870 buffer control structures would have been reset but the thread had
2871 no way to tell. The thread could even midway memcpy'ing to the
2872 buffer, which would mean that when resumed, it would clobber the
2873 trace buffer that had been set for a new run.
2875 - we can't rewrite/reuse the jump pads for new tracepoints
2876 safely. Say you do tstart while a thread is stopped midway while
2877 collecting. When the thread is later resumed, it finishes the
2878 collection, and returns to the jump pad, to execute the original
2879 instruction that was under the tracepoint jump at the time the
2880 older run had been started. If the jump pad had been rewritten
2881 since for something else in the new run, the thread would now
2882 execute the wrong / random instructions. */
2885 linux_process_target::stabilize_threads ()
2887 thread_info
*thread_stuck
= find_thread (stuck_in_jump_pad_callback
);
2889 if (thread_stuck
!= NULL
)
2892 debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
2893 lwpid_of (thread_stuck
));
2897 thread_info
*saved_thread
= current_thread
;
2899 stabilizing_threads
= 1;
2902 for_each_thread ([this] (thread_info
*thread
)
2904 move_out_of_jump_pad (thread
);
2907 /* Loop until all are stopped out of the jump pads. */
2908 while (find_thread (lwp_running
) != NULL
)
2910 struct target_waitstatus ourstatus
;
2911 struct lwp_info
*lwp
;
2914 /* Note that we go through the full wait even loop. While
2915 moving threads out of jump pad, we need to be able to step
2916 over internal breakpoints and such. */
2917 wait_1 (minus_one_ptid
, &ourstatus
, 0);
2919 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
2921 lwp
= get_thread_lwp (current_thread
);
2924 lwp_suspended_inc (lwp
);
2926 if (ourstatus
.value
.sig
!= GDB_SIGNAL_0
2927 || current_thread
->last_resume_kind
== resume_stop
)
2929 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.value
.sig
));
2930 enqueue_one_deferred_signal (lwp
, &wstat
);
2935 unsuspend_all_lwps (NULL
);
2937 stabilizing_threads
= 0;
2939 current_thread
= saved_thread
;
2943 thread_stuck
= find_thread (stuck_in_jump_pad_callback
);
2945 if (thread_stuck
!= NULL
)
2946 debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
2947 lwpid_of (thread_stuck
));
2951 /* Convenience function that is called when the kernel reports an
2952 event that is not passed out to GDB. */
2955 ignore_event (struct target_waitstatus
*ourstatus
)
2957 /* If we got an event, there may still be others, as a single
2958 SIGCHLD can indicate more than one child stopped. This forces
2959 another target_wait call. */
2962 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2966 /* Convenience function that is called when the kernel reports an exit
2967 event. This decides whether to report the event to GDB as a
2968 process exit event, a thread exit event, or to suppress the
2972 filter_exit_event (struct lwp_info
*event_child
,
2973 struct target_waitstatus
*ourstatus
)
2975 client_state
&cs
= get_client_state ();
2976 struct thread_info
*thread
= get_lwp_thread (event_child
);
2977 ptid_t ptid
= ptid_of (thread
);
2979 if (!last_thread_of_process_p (pid_of (thread
)))
2981 if (cs
.report_thread_events
)
2982 ourstatus
->kind
= TARGET_WAITKIND_THREAD_EXITED
;
2984 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2986 delete_lwp (event_child
);
2991 /* Returns 1 if GDB is interested in any event_child syscalls. */
2994 gdb_catching_syscalls_p (struct lwp_info
*event_child
)
2996 struct thread_info
*thread
= get_lwp_thread (event_child
);
2997 struct process_info
*proc
= get_thread_process (thread
);
2999 return !proc
->syscalls_to_catch
.empty ();
3002 /* Returns 1 if GDB is interested in the event_child syscall.
3003 Only to be called when stopped reason is SYSCALL_SIGTRAP. */
3006 gdb_catch_this_syscall_p (struct lwp_info
*event_child
)
3009 struct thread_info
*thread
= get_lwp_thread (event_child
);
3010 struct process_info
*proc
= get_thread_process (thread
);
3012 if (proc
->syscalls_to_catch
.empty ())
3015 if (proc
->syscalls_to_catch
[0] == ANY_SYSCALL
)
3018 get_syscall_trapinfo (event_child
, &sysno
);
3020 for (int iter
: proc
->syscalls_to_catch
)
3028 linux_process_target::wait_1 (ptid_t ptid
, target_waitstatus
*ourstatus
,
3031 client_state
&cs
= get_client_state ();
3033 struct lwp_info
*event_child
;
3036 int step_over_finished
;
3037 int bp_explains_trap
;
3038 int maybe_internal_trap
;
3047 debug_printf ("wait_1: [%s]\n", target_pid_to_str (ptid
));
3050 /* Translate generic target options into linux options. */
3052 if (target_options
& TARGET_WNOHANG
)
3055 bp_explains_trap
= 0;
3058 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3060 auto status_pending_p_any
= [&] (thread_info
*thread
)
3062 return status_pending_p_callback (thread
, minus_one_ptid
);
3065 auto not_stopped
= [&] (thread_info
*thread
)
3067 return not_stopped_callback (thread
, minus_one_ptid
);
3070 /* Find a resumed LWP, if any. */
3071 if (find_thread (status_pending_p_any
) != NULL
)
3073 else if (find_thread (not_stopped
) != NULL
)
3078 if (step_over_bkpt
== null_ptid
)
3079 pid
= wait_for_event (ptid
, &w
, options
);
3083 debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
3084 target_pid_to_str (step_over_bkpt
));
3085 pid
= wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
3088 if (pid
== 0 || (pid
== -1 && !any_resumed
))
3090 gdb_assert (target_options
& TARGET_WNOHANG
);
3094 debug_printf ("wait_1 ret = null_ptid, "
3095 "TARGET_WAITKIND_IGNORE\n");
3099 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3106 debug_printf ("wait_1 ret = null_ptid, "
3107 "TARGET_WAITKIND_NO_RESUMED\n");
3111 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3115 event_child
= get_thread_lwp (current_thread
);
3117 /* wait_for_event only returns an exit status for the last
3118 child of a process. Report it. */
3119 if (WIFEXITED (w
) || WIFSIGNALED (w
))
3123 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
3124 ourstatus
->value
.integer
= WEXITSTATUS (w
);
3128 debug_printf ("wait_1 ret = %s, exited with "
3130 target_pid_to_str (ptid_of (current_thread
)),
3137 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
3138 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (w
));
3142 debug_printf ("wait_1 ret = %s, terminated with "
3144 target_pid_to_str (ptid_of (current_thread
)),
3150 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3151 return filter_exit_event (event_child
, ourstatus
);
3153 return ptid_of (current_thread
);
3156 /* If step-over executes a breakpoint instruction, in the case of a
3157 hardware single step it means a gdb/gdbserver breakpoint had been
3158 planted on top of a permanent breakpoint, in the case of a software
3159 single step it may just mean that gdbserver hit the reinsert breakpoint.
3160 The PC has been adjusted by save_stop_reason to point at
3161 the breakpoint address.
3162 So in the case of the hardware single step advance the PC manually
3163 past the breakpoint and in the case of software single step advance only
3164 if it's not the single_step_breakpoint we are hitting.
3165 This avoids that a program would keep trapping a permanent breakpoint
3167 if (step_over_bkpt
!= null_ptid
3168 && event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3169 && (event_child
->stepping
3170 || !single_step_breakpoint_inserted_here (event_child
->stop_pc
)))
3172 int increment_pc
= 0;
3173 int breakpoint_kind
= 0;
3174 CORE_ADDR stop_pc
= event_child
->stop_pc
;
3176 breakpoint_kind
= breakpoint_kind_from_current_state (&stop_pc
);
3177 sw_breakpoint_from_kind (breakpoint_kind
, &increment_pc
);
3181 debug_printf ("step-over for %s executed software breakpoint\n",
3182 target_pid_to_str (ptid_of (current_thread
)));
3185 if (increment_pc
!= 0)
3187 struct regcache
*regcache
3188 = get_thread_regcache (current_thread
, 1);
3190 event_child
->stop_pc
+= increment_pc
;
3191 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
3193 if (!(*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))
3194 event_child
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
3198 /* If this event was not handled before, and is not a SIGTRAP, we
3199 report it. SIGILL and SIGSEGV are also treated as traps in case
3200 a breakpoint is inserted at the current PC. If this target does
3201 not support internal breakpoints at all, we also report the
3202 SIGTRAP without further processing; it's of no concern to us. */
3204 = (supports_breakpoints ()
3205 && (WSTOPSIG (w
) == SIGTRAP
3206 || ((WSTOPSIG (w
) == SIGILL
3207 || WSTOPSIG (w
) == SIGSEGV
)
3208 && (*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))));
3210 if (maybe_internal_trap
)
3212 /* Handle anything that requires bookkeeping before deciding to
3213 report the event or continue waiting. */
3215 /* First check if we can explain the SIGTRAP with an internal
3216 breakpoint, or if we should possibly report the event to GDB.
3217 Do this before anything that may remove or insert a
3219 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
3221 /* We have a SIGTRAP, possibly a step-over dance has just
3222 finished. If so, tweak the state machine accordingly,
3223 reinsert breakpoints and delete any single-step
3225 step_over_finished
= finish_step_over (event_child
);
3227 /* Now invoke the callbacks of any internal breakpoints there. */
3228 check_breakpoints (event_child
->stop_pc
);
3230 /* Handle tracepoint data collecting. This may overflow the
3231 trace buffer, and cause a tracing stop, removing
3233 trace_event
= handle_tracepoints (event_child
);
3235 if (bp_explains_trap
)
3238 debug_printf ("Hit a gdbserver breakpoint.\n");
3243 /* We have some other signal, possibly a step-over dance was in
3244 progress, and it should be cancelled too. */
3245 step_over_finished
= finish_step_over (event_child
);
3248 /* We have all the data we need. Either report the event to GDB, or
3249 resume threads and keep waiting for more. */
3251 /* If we're collecting a fast tracepoint, finish the collection and
3252 move out of the jump pad before delivering a signal. See
3253 linux_stabilize_threads. */
3256 && WSTOPSIG (w
) != SIGTRAP
3257 && supports_fast_tracepoints ()
3258 && agent_loaded_p ())
3261 debug_printf ("Got signal %d for LWP %ld. Check if we need "
3262 "to defer or adjust it.\n",
3263 WSTOPSIG (w
), lwpid_of (current_thread
));
3265 /* Allow debugging the jump pad itself. */
3266 if (current_thread
->last_resume_kind
!= resume_step
3267 && maybe_move_out_of_jump_pad (event_child
, &w
))
3269 enqueue_one_deferred_signal (event_child
, &w
);
3272 debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
3273 WSTOPSIG (w
), lwpid_of (current_thread
));
3275 linux_resume_one_lwp (event_child
, 0, 0, NULL
);
3279 return ignore_event (ourstatus
);
3283 if (event_child
->collecting_fast_tracepoint
3284 != fast_tpoint_collect_result::not_collecting
)
3287 debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
3288 "Check if we're already there.\n",
3289 lwpid_of (current_thread
),
3290 (int) event_child
->collecting_fast_tracepoint
);
3294 event_child
->collecting_fast_tracepoint
3295 = linux_fast_tracepoint_collecting (event_child
, NULL
);
3297 if (event_child
->collecting_fast_tracepoint
3298 != fast_tpoint_collect_result::before_insn
)
3300 /* No longer need this breakpoint. */
3301 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
3304 debug_printf ("No longer need exit-jump-pad bkpt; removing it."
3305 "stopping all threads momentarily.\n");
3307 /* Other running threads could hit this breakpoint.
3308 We don't handle moribund locations like GDB does,
3309 instead we always pause all threads when removing
3310 breakpoints, so that any step-over or
3311 decr_pc_after_break adjustment is always taken
3312 care of while the breakpoint is still
3314 stop_all_lwps (1, event_child
);
3316 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
3317 event_child
->exit_jump_pad_bkpt
= NULL
;
3319 unstop_all_lwps (1, event_child
);
3321 gdb_assert (event_child
->suspended
>= 0);
3325 if (event_child
->collecting_fast_tracepoint
3326 == fast_tpoint_collect_result::not_collecting
)
3329 debug_printf ("fast tracepoint finished "
3330 "collecting successfully.\n");
3332 /* We may have a deferred signal to report. */
3333 if (dequeue_one_deferred_signal (event_child
, &w
))
3336 debug_printf ("dequeued one signal.\n");
3341 debug_printf ("no deferred signals.\n");
3343 if (stabilizing_threads
)
3345 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3346 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3350 debug_printf ("wait_1 ret = %s, stopped "
3351 "while stabilizing threads\n",
3352 target_pid_to_str (ptid_of (current_thread
)));
3356 return ptid_of (current_thread
);
3362 /* Check whether GDB would be interested in this event. */
3364 /* Check if GDB is interested in this syscall. */
3366 && WSTOPSIG (w
) == SYSCALL_SIGTRAP
3367 && !gdb_catch_this_syscall_p (event_child
))
3371 debug_printf ("Ignored syscall for LWP %ld.\n",
3372 lwpid_of (current_thread
));
3375 linux_resume_one_lwp (event_child
, event_child
->stepping
,
3380 return ignore_event (ourstatus
);
3383 /* If GDB is not interested in this signal, don't stop other
3384 threads, and don't report it to GDB. Just resume the inferior
3385 right away. We do this for threading-related signals as well as
3386 any that GDB specifically requested we ignore. But never ignore
3387 SIGSTOP if we sent it ourselves, and do not ignore signals when
3388 stepping - they may require special handling to skip the signal
3389 handler. Also never ignore signals that could be caused by a
3392 && current_thread
->last_resume_kind
!= resume_step
3394 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3395 (current_process ()->priv
->thread_db
!= NULL
3396 && (WSTOPSIG (w
) == __SIGRTMIN
3397 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
3400 (cs
.pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
3401 && !(WSTOPSIG (w
) == SIGSTOP
3402 && current_thread
->last_resume_kind
== resume_stop
)
3403 && !linux_wstatus_maybe_breakpoint (w
))))
3405 siginfo_t info
, *info_p
;
3408 debug_printf ("Ignored signal %d for LWP %ld.\n",
3409 WSTOPSIG (w
), lwpid_of (current_thread
));
3411 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
3412 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
3417 if (step_over_finished
)
3419 /* We cancelled this thread's step-over above. We still
3420 need to unsuspend all other LWPs, and set them back
3421 running again while the signal handler runs. */
3422 unsuspend_all_lwps (event_child
);
3424 /* Enqueue the pending signal info so that proceed_all_lwps
3426 enqueue_pending_signal (event_child
, WSTOPSIG (w
), info_p
);
3428 proceed_all_lwps ();
3432 linux_resume_one_lwp (event_child
, event_child
->stepping
,
3433 WSTOPSIG (w
), info_p
);
3439 return ignore_event (ourstatus
);
3442 /* Note that all addresses are always "out of the step range" when
3443 there's no range to begin with. */
3444 in_step_range
= lwp_in_step_range (event_child
);
3446 /* If GDB wanted this thread to single step, and the thread is out
3447 of the step range, we always want to report the SIGTRAP, and let
3448 GDB handle it. Watchpoints should always be reported. So should
3449 signals we can't explain. A SIGTRAP we can't explain could be a
3450 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3451 do, we're be able to handle GDB breakpoints on top of internal
3452 breakpoints, by handling the internal breakpoint and still
3453 reporting the event to GDB. If we don't, we're out of luck, GDB
3454 won't see the breakpoint hit. If we see a single-step event but
3455 the thread should be continuing, don't pass the trap to gdb.
3456 That indicates that we had previously finished a single-step but
3457 left the single-step pending -- see
3458 complete_ongoing_step_over. */
3459 report_to_gdb
= (!maybe_internal_trap
3460 || (current_thread
->last_resume_kind
== resume_step
3462 || event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3464 && !bp_explains_trap
3466 && !step_over_finished
3467 && !(current_thread
->last_resume_kind
== resume_continue
3468 && event_child
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
))
3469 || (gdb_breakpoint_here (event_child
->stop_pc
)
3470 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
3471 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
))
3472 || event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
);
3474 run_breakpoint_commands (event_child
->stop_pc
);
3476 /* We found no reason GDB would want us to stop. We either hit one
3477 of our own breakpoints, or finished an internal step GDB
3478 shouldn't know about. */
3483 if (bp_explains_trap
)
3484 debug_printf ("Hit a gdbserver breakpoint.\n");
3485 if (step_over_finished
)
3486 debug_printf ("Step-over finished.\n");
3488 debug_printf ("Tracepoint event.\n");
3489 if (lwp_in_step_range (event_child
))
3490 debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
3491 paddress (event_child
->stop_pc
),
3492 paddress (event_child
->step_range_start
),
3493 paddress (event_child
->step_range_end
));
3496 /* We're not reporting this breakpoint to GDB, so apply the
3497 decr_pc_after_break adjustment to the inferior's regcache
3500 if (the_low_target
.set_pc
!= NULL
)
3502 struct regcache
*regcache
3503 = get_thread_regcache (current_thread
, 1);
3504 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
);
3507 if (step_over_finished
)
3509 /* If we have finished stepping over a breakpoint, we've
3510 stopped and suspended all LWPs momentarily except the
3511 stepping one. This is where we resume them all again.
3512 We're going to keep waiting, so use proceed, which
3513 handles stepping over the next breakpoint. */
3514 unsuspend_all_lwps (event_child
);
3518 /* Remove the single-step breakpoints if any. Note that
3519 there isn't single-step breakpoint if we finished stepping
3521 if (can_software_single_step ()
3522 && has_single_step_breakpoints (current_thread
))
3524 stop_all_lwps (0, event_child
);
3525 delete_single_step_breakpoints (current_thread
);
3526 unstop_all_lwps (0, event_child
);
3531 debug_printf ("proceeding all threads.\n");
3532 proceed_all_lwps ();
3537 return ignore_event (ourstatus
);
3542 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3545 = target_waitstatus_to_string (&event_child
->waitstatus
);
3547 debug_printf ("LWP %ld: extended event with waitstatus %s\n",
3548 lwpid_of (get_lwp_thread (event_child
)), str
.c_str ());
3550 if (current_thread
->last_resume_kind
== resume_step
)
3552 if (event_child
->step_range_start
== event_child
->step_range_end
)
3553 debug_printf ("GDB wanted to single-step, reporting event.\n");
3554 else if (!lwp_in_step_range (event_child
))
3555 debug_printf ("Out of step range, reporting event.\n");
3557 if (event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
3558 debug_printf ("Stopped by watchpoint.\n");
3559 else if (gdb_breakpoint_here (event_child
->stop_pc
))
3560 debug_printf ("Stopped by GDB breakpoint.\n");
3562 debug_printf ("Hit a non-gdbserver trap event.\n");
3565 /* Alright, we're going to report a stop. */
3567 /* Remove single-step breakpoints. */
3568 if (can_software_single_step ())
3570 /* Remove single-step breakpoints or not. It it is true, stop all
3571 lwps, so that other threads won't hit the breakpoint in the
3573 int remove_single_step_breakpoints_p
= 0;
3577 remove_single_step_breakpoints_p
3578 = has_single_step_breakpoints (current_thread
);
3582 /* In all-stop, a stop reply cancels all previous resume
3583 requests. Delete all single-step breakpoints. */
3585 find_thread ([&] (thread_info
*thread
) {
3586 if (has_single_step_breakpoints (thread
))
3588 remove_single_step_breakpoints_p
= 1;
3596 if (remove_single_step_breakpoints_p
)
3598 /* If we remove single-step breakpoints from memory, stop all lwps,
3599 so that other threads won't hit the breakpoint in the staled
3601 stop_all_lwps (0, event_child
);
3605 gdb_assert (has_single_step_breakpoints (current_thread
));
3606 delete_single_step_breakpoints (current_thread
);
3610 for_each_thread ([] (thread_info
*thread
){
3611 if (has_single_step_breakpoints (thread
))
3612 delete_single_step_breakpoints (thread
);
3616 unstop_all_lwps (0, event_child
);
3620 if (!stabilizing_threads
)
3622 /* In all-stop, stop all threads. */
3624 stop_all_lwps (0, NULL
);
3626 if (step_over_finished
)
3630 /* If we were doing a step-over, all other threads but
3631 the stepping one had been paused in start_step_over,
3632 with their suspend counts incremented. We don't want
3633 to do a full unstop/unpause, because we're in
3634 all-stop mode (so we want threads stopped), but we
3635 still need to unsuspend the other threads, to
3636 decrement their `suspended' count back. */
3637 unsuspend_all_lwps (event_child
);
3641 /* If we just finished a step-over, then all threads had
3642 been momentarily paused. In all-stop, that's fine,
3643 we want threads stopped by now anyway. In non-stop,
3644 we need to re-resume threads that GDB wanted to be
3646 unstop_all_lwps (1, event_child
);
3650 /* If we're not waiting for a specific LWP, choose an event LWP
3651 from among those that have had events. Giving equal priority
3652 to all LWPs that have had events helps prevent
3654 if (ptid
== minus_one_ptid
)
3656 event_child
->status_pending_p
= 1;
3657 event_child
->status_pending
= w
;
3659 select_event_lwp (&event_child
);
3661 /* current_thread and event_child must stay in sync. */
3662 current_thread
= get_lwp_thread (event_child
);
3664 event_child
->status_pending_p
= 0;
3665 w
= event_child
->status_pending
;
3669 /* Stabilize threads (move out of jump pads). */
3671 target_stabilize_threads ();
3675 /* If we just finished a step-over, then all threads had been
3676 momentarily paused. In all-stop, that's fine, we want
3677 threads stopped by now anyway. In non-stop, we need to
3678 re-resume threads that GDB wanted to be running. */
3679 if (step_over_finished
)
3680 unstop_all_lwps (1, event_child
);
3683 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3685 /* If the reported event is an exit, fork, vfork or exec, let
3688 /* Break the unreported fork relationship chain. */
3689 if (event_child
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
3690 || event_child
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
)
3692 event_child
->fork_relative
->fork_relative
= NULL
;
3693 event_child
->fork_relative
= NULL
;
3696 *ourstatus
= event_child
->waitstatus
;
3697 /* Clear the event lwp's waitstatus since we handled it already. */
3698 event_child
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3701 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3703 /* Now that we've selected our final event LWP, un-adjust its PC if
3704 it was a software breakpoint, and the client doesn't know we can
3705 adjust the breakpoint ourselves. */
3706 if (event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3707 && !cs
.swbreak_feature
)
3709 int decr_pc
= the_low_target
.decr_pc_after_break
;
3713 struct regcache
*regcache
3714 = get_thread_regcache (current_thread
, 1);
3715 (*the_low_target
.set_pc
) (regcache
, event_child
->stop_pc
+ decr_pc
);
3719 if (WSTOPSIG (w
) == SYSCALL_SIGTRAP
)
3721 get_syscall_trapinfo (event_child
,
3722 &ourstatus
->value
.syscall_number
);
3723 ourstatus
->kind
= event_child
->syscall_state
;
3725 else if (current_thread
->last_resume_kind
== resume_stop
3726 && WSTOPSIG (w
) == SIGSTOP
)
3728 /* A thread that has been requested to stop by GDB with vCont;t,
3729 and it stopped cleanly, so report as SIG0. The use of
3730 SIGSTOP is an implementation detail. */
3731 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3733 else if (current_thread
->last_resume_kind
== resume_stop
3734 && WSTOPSIG (w
) != SIGSTOP
)
3736 /* A thread that has been requested to stop by GDB with vCont;t,
3737 but, it stopped for other reasons. */
3738 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3740 else if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
)
3742 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3745 gdb_assert (step_over_bkpt
== null_ptid
);
3749 debug_printf ("wait_1 ret = %s, %d, %d\n",
3750 target_pid_to_str (ptid_of (current_thread
)),
3751 ourstatus
->kind
, ourstatus
->value
.sig
);
3755 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3756 return filter_exit_event (event_child
, ourstatus
);
3758 return ptid_of (current_thread
);
3761 /* Get rid of any pending event in the pipe. */
3763 async_file_flush (void)
3769 ret
= read (linux_event_pipe
[0], &buf
, 1);
3770 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
3773 /* Put something in the pipe, so the event loop wakes up. */
3775 async_file_mark (void)
3779 async_file_flush ();
3782 ret
= write (linux_event_pipe
[1], "+", 1);
3783 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
3785 /* Ignore EAGAIN. If the pipe is full, the event loop will already
3786 be awakened anyway. */
3790 linux_process_target::wait (ptid_t ptid
,
3791 target_waitstatus
*ourstatus
,
3796 /* Flush the async file first. */
3797 if (target_is_async_p ())
3798 async_file_flush ();
3802 event_ptid
= wait_1 (ptid
, ourstatus
, target_options
);
3804 while ((target_options
& TARGET_WNOHANG
) == 0
3805 && event_ptid
== null_ptid
3806 && ourstatus
->kind
== TARGET_WAITKIND_IGNORE
);
3808 /* If at least one stop was reported, there may be more. A single
3809 SIGCHLD can signal more than one child stop. */
3810 if (target_is_async_p ()
3811 && (target_options
& TARGET_WNOHANG
) != 0
3812 && event_ptid
!= null_ptid
)
3818 /* Send a signal to an LWP. */
3821 kill_lwp (unsigned long lwpid
, int signo
)
3826 ret
= syscall (__NR_tkill
, lwpid
, signo
);
3827 if (errno
== ENOSYS
)
3829 /* If tkill fails, then we are not using nptl threads, a
3830 configuration we no longer support. */
3831 perror_with_name (("tkill"));
3837 linux_stop_lwp (struct lwp_info
*lwp
)
3843 send_sigstop (struct lwp_info
*lwp
)
3847 pid
= lwpid_of (get_lwp_thread (lwp
));
3849 /* If we already have a pending stop signal for this process, don't
3851 if (lwp
->stop_expected
)
3854 debug_printf ("Have pending sigstop for lwp %d\n", pid
);
3860 debug_printf ("Sending sigstop to lwp %d\n", pid
);
3862 lwp
->stop_expected
= 1;
3863 kill_lwp (pid
, SIGSTOP
);
3867 send_sigstop (thread_info
*thread
, lwp_info
*except
)
3869 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3871 /* Ignore EXCEPT. */
3881 /* Increment the suspend count of an LWP, and stop it, if not stopped
3884 suspend_and_send_sigstop (thread_info
*thread
, lwp_info
*except
)
3886 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3888 /* Ignore EXCEPT. */
3892 lwp_suspended_inc (lwp
);
3894 send_sigstop (thread
, except
);
3898 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
3900 /* Store the exit status for later. */
3901 lwp
->status_pending_p
= 1;
3902 lwp
->status_pending
= wstat
;
3904 /* Store in waitstatus as well, as there's nothing else to process
3906 if (WIFEXITED (wstat
))
3908 lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXITED
;
3909 lwp
->waitstatus
.value
.integer
= WEXITSTATUS (wstat
);
3911 else if (WIFSIGNALED (wstat
))
3913 lwp
->waitstatus
.kind
= TARGET_WAITKIND_SIGNALLED
;
3914 lwp
->waitstatus
.value
.sig
= gdb_signal_from_host (WTERMSIG (wstat
));
3917 /* Prevent trying to stop it. */
3920 /* No further stops are expected from a dead lwp. */
3921 lwp
->stop_expected
= 0;
3924 /* Return true if LWP has exited already, and has a pending exit event
3925 to report to GDB. */
3928 lwp_is_marked_dead (struct lwp_info
*lwp
)
3930 return (lwp
->status_pending_p
3931 && (WIFEXITED (lwp
->status_pending
)
3932 || WIFSIGNALED (lwp
->status_pending
)));
3936 linux_process_target::wait_for_sigstop ()
3938 struct thread_info
*saved_thread
;
3943 saved_thread
= current_thread
;
3944 if (saved_thread
!= NULL
)
3945 saved_tid
= saved_thread
->id
;
3947 saved_tid
= null_ptid
; /* avoid bogus unused warning */
3950 debug_printf ("wait_for_sigstop: pulling events\n");
3952 /* Passing NULL_PTID as filter indicates we want all events to be
3953 left pending. Eventually this returns when there are no
3954 unwaited-for children left. */
3955 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
, __WALL
);
3956 gdb_assert (ret
== -1);
3958 if (saved_thread
== NULL
|| mythread_alive (saved_tid
))
3959 current_thread
= saved_thread
;
3963 debug_printf ("Previously current thread died.\n");
3965 /* We can't change the current inferior behind GDB's back,
3966 otherwise, a subsequent command may apply to the wrong
3968 current_thread
= NULL
;
3972 /* Returns true if THREAD is stopped in a jump pad, and we can't
3973 move it out, because we need to report the stop event to GDB. For
3974 example, if the user puts a breakpoint in the jump pad, it's
3975 because she wants to debug it. */
3978 stuck_in_jump_pad_callback (thread_info
*thread
)
3980 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3982 if (lwp
->suspended
!= 0)
3984 internal_error (__FILE__
, __LINE__
,
3985 "LWP %ld is suspended, suspended=%d\n",
3986 lwpid_of (thread
), lwp
->suspended
);
3988 gdb_assert (lwp
->stopped
);
3990 /* Allow debugging the jump pad, gdb_collect, etc.. */
3991 return (supports_fast_tracepoints ()
3992 && agent_loaded_p ()
3993 && (gdb_breakpoint_here (lwp
->stop_pc
)
3994 || lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3995 || thread
->last_resume_kind
== resume_step
)
3996 && (linux_fast_tracepoint_collecting (lwp
, NULL
)
3997 != fast_tpoint_collect_result::not_collecting
));
4001 linux_process_target::move_out_of_jump_pad (thread_info
*thread
)
4003 struct thread_info
*saved_thread
;
4004 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4007 if (lwp
->suspended
!= 0)
4009 internal_error (__FILE__
, __LINE__
,
4010 "LWP %ld is suspended, suspended=%d\n",
4011 lwpid_of (thread
), lwp
->suspended
);
4013 gdb_assert (lwp
->stopped
);
4015 /* For gdb_breakpoint_here. */
4016 saved_thread
= current_thread
;
4017 current_thread
= thread
;
4019 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
4021 /* Allow debugging the jump pad, gdb_collect, etc. */
4022 if (!gdb_breakpoint_here (lwp
->stop_pc
)
4023 && lwp
->stop_reason
!= TARGET_STOPPED_BY_WATCHPOINT
4024 && thread
->last_resume_kind
!= resume_step
4025 && maybe_move_out_of_jump_pad (lwp
, wstat
))
4028 debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
4033 lwp
->status_pending_p
= 0;
4034 enqueue_one_deferred_signal (lwp
, wstat
);
4037 debug_printf ("Signal %d for LWP %ld deferred "
4039 WSTOPSIG (*wstat
), lwpid_of (thread
));
4042 linux_resume_one_lwp (lwp
, 0, 0, NULL
);
4045 lwp_suspended_inc (lwp
);
4047 current_thread
= saved_thread
;
4051 lwp_running (thread_info
*thread
)
4053 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4055 if (lwp_is_marked_dead (lwp
))
4058 return !lwp
->stopped
;
4062 linux_process_target::stop_all_lwps (int suspend
, lwp_info
*except
)
4064 /* Should not be called recursively. */
4065 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
4070 debug_printf ("stop_all_lwps (%s, except=%s)\n",
4071 suspend
? "stop-and-suspend" : "stop",
4073 ? target_pid_to_str (ptid_of (get_lwp_thread (except
)))
4077 stopping_threads
= (suspend
4078 ? STOPPING_AND_SUSPENDING_THREADS
4079 : STOPPING_THREADS
);
4082 for_each_thread ([&] (thread_info
*thread
)
4084 suspend_and_send_sigstop (thread
, except
);
4087 for_each_thread ([&] (thread_info
*thread
)
4089 send_sigstop (thread
, except
);
4092 wait_for_sigstop ();
4093 stopping_threads
= NOT_STOPPING_THREADS
;
4097 debug_printf ("stop_all_lwps done, setting stopping_threads "
4098 "back to !stopping\n");
4103 /* Enqueue one signal in the chain of signals which need to be
4104 delivered to this process on next resume. */
4107 enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
)
4109 struct pending_signals
*p_sig
= XNEW (struct pending_signals
);
4111 p_sig
->prev
= lwp
->pending_signals
;
4112 p_sig
->signal
= signal
;
4114 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
4116 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
4117 lwp
->pending_signals
= p_sig
;
4120 /* Install breakpoints for software single stepping. */
4123 install_software_single_step_breakpoints (struct lwp_info
*lwp
)
4125 struct thread_info
*thread
= get_lwp_thread (lwp
);
4126 struct regcache
*regcache
= get_thread_regcache (thread
, 1);
4128 scoped_restore save_current_thread
= make_scoped_restore (¤t_thread
);
4130 current_thread
= thread
;
4131 std::vector
<CORE_ADDR
> next_pcs
= the_low_target
.get_next_pcs (regcache
);
4133 for (CORE_ADDR pc
: next_pcs
)
4134 set_single_step_breakpoint (pc
, current_ptid
);
4137 /* Single step via hardware or software single step.
4138 Return 1 if hardware single stepping, 0 if software single stepping
4139 or can't single step. */
4142 single_step (struct lwp_info
* lwp
)
4146 if (can_hardware_single_step ())
4150 else if (can_software_single_step ())
4152 install_software_single_step_breakpoints (lwp
);
4158 debug_printf ("stepping is not implemented on this target");
4164 /* The signal can be delivered to the inferior if we are not trying to
4165 finish a fast tracepoint collect. Since signal can be delivered in
4166 the step-over, the program may go to signal handler and trap again
4167 after return from the signal handler. We can live with the spurious
4171 lwp_signal_can_be_delivered (struct lwp_info
*lwp
)
4173 return (lwp
->collecting_fast_tracepoint
4174 == fast_tpoint_collect_result::not_collecting
);
4177 /* Resume execution of LWP. If STEP is nonzero, single-step it. If
4178 SIGNAL is nonzero, give it that signal. */
4181 linux_resume_one_lwp_throw (struct lwp_info
*lwp
,
4182 int step
, int signal
, siginfo_t
*info
)
4184 struct thread_info
*thread
= get_lwp_thread (lwp
);
4185 struct thread_info
*saved_thread
;
4187 struct process_info
*proc
= get_thread_process (thread
);
4189 /* Note that target description may not be initialised
4190 (proc->tdesc == NULL) at this point because the program hasn't
4191 stopped at the first instruction yet. It means GDBserver skips
4192 the extra traps from the wrapper program (see option --wrapper).
4193 Code in this function that requires register access should be
4194 guarded by proc->tdesc == NULL or something else. */
4196 if (lwp
->stopped
== 0)
4199 gdb_assert (lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
4201 fast_tpoint_collect_result fast_tp_collecting
4202 = lwp
->collecting_fast_tracepoint
;
4204 gdb_assert (!stabilizing_threads
4205 || (fast_tp_collecting
4206 != fast_tpoint_collect_result::not_collecting
));
4208 /* Cancel actions that rely on GDB not changing the PC (e.g., the
4209 user used the "jump" command, or "set $pc = foo"). */
4210 if (thread
->while_stepping
!= NULL
&& lwp
->stop_pc
!= get_pc (lwp
))
4212 /* Collecting 'while-stepping' actions doesn't make sense
4214 release_while_stepping_state_list (thread
);
4217 /* If we have pending signals or status, and a new signal, enqueue the
4218 signal. Also enqueue the signal if it can't be delivered to the
4219 inferior right now. */
4221 && (lwp
->status_pending_p
4222 || lwp
->pending_signals
!= NULL
4223 || !lwp_signal_can_be_delivered (lwp
)))
4225 enqueue_pending_signal (lwp
, signal
, info
);
4227 /* Postpone any pending signal. It was enqueued above. */
4231 if (lwp
->status_pending_p
)
4234 debug_printf ("Not resuming lwp %ld (%s, stop %s);"
4235 " has pending status\n",
4236 lwpid_of (thread
), step
? "step" : "continue",
4237 lwp
->stop_expected
? "expected" : "not expected");
4241 saved_thread
= current_thread
;
4242 current_thread
= thread
;
4244 /* This bit needs some thinking about. If we get a signal that
4245 we must report while a single-step reinsert is still pending,
4246 we often end up resuming the thread. It might be better to
4247 (ew) allow a stack of pending events; then we could be sure that
4248 the reinsert happened right away and not lose any signals.
4250 Making this stack would also shrink the window in which breakpoints are
4251 uninserted (see comment in linux_wait_for_lwp) but not enough for
4252 complete correctness, so it won't solve that problem. It may be
4253 worthwhile just to solve this one, however. */
4254 if (lwp
->bp_reinsert
!= 0)
4257 debug_printf (" pending reinsert at 0x%s\n",
4258 paddress (lwp
->bp_reinsert
));
4260 if (can_hardware_single_step ())
4262 if (fast_tp_collecting
== fast_tpoint_collect_result::not_collecting
)
4265 warning ("BAD - reinserting but not stepping.");
4267 warning ("BAD - reinserting and suspended(%d).",
4272 step
= maybe_hw_step (thread
);
4275 if (fast_tp_collecting
== fast_tpoint_collect_result::before_insn
)
4278 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4279 " (exit-jump-pad-bkpt)\n",
4282 else if (fast_tp_collecting
== fast_tpoint_collect_result::at_insn
)
4285 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4286 " single-stepping\n",
4289 if (can_hardware_single_step ())
4293 internal_error (__FILE__
, __LINE__
,
4294 "moving out of jump pad single-stepping"
4295 " not implemented on this target");
4299 /* If we have while-stepping actions in this thread set it stepping.
4300 If we have a signal to deliver, it may or may not be set to
4301 SIG_IGN, we don't know. Assume so, and allow collecting
4302 while-stepping into a signal handler. A possible smart thing to
4303 do would be to set an internal breakpoint at the signal return
4304 address, continue, and carry on catching this while-stepping
4305 action only when that breakpoint is hit. A future
4307 if (thread
->while_stepping
!= NULL
)
4310 debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
4313 step
= single_step (lwp
);
4316 if (proc
->tdesc
!= NULL
&& the_low_target
.get_pc
!= NULL
)
4318 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
4320 lwp
->stop_pc
= (*the_low_target
.get_pc
) (regcache
);
4324 debug_printf (" %s from pc 0x%lx\n", step
? "step" : "continue",
4325 (long) lwp
->stop_pc
);
4329 /* If we have pending signals, consume one if it can be delivered to
4331 if (lwp
->pending_signals
!= NULL
&& lwp_signal_can_be_delivered (lwp
))
4333 struct pending_signals
**p_sig
;
4335 p_sig
= &lwp
->pending_signals
;
4336 while ((*p_sig
)->prev
!= NULL
)
4337 p_sig
= &(*p_sig
)->prev
;
4339 signal
= (*p_sig
)->signal
;
4340 if ((*p_sig
)->info
.si_signo
!= 0)
4341 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
4349 debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
4350 lwpid_of (thread
), step
? "step" : "continue", signal
,
4351 lwp
->stop_expected
? "expected" : "not expected");
4353 if (the_low_target
.prepare_to_resume
!= NULL
)
4354 the_low_target
.prepare_to_resume (lwp
);
4356 regcache_invalidate_thread (thread
);
4358 lwp
->stepping
= step
;
4360 ptrace_request
= PTRACE_SINGLESTEP
;
4361 else if (gdb_catching_syscalls_p (lwp
))
4362 ptrace_request
= PTRACE_SYSCALL
;
4364 ptrace_request
= PTRACE_CONT
;
4365 ptrace (ptrace_request
,
4367 (PTRACE_TYPE_ARG3
) 0,
4368 /* Coerce to a uintptr_t first to avoid potential gcc warning
4369 of coercing an 8 byte integer to a 4 byte pointer. */
4370 (PTRACE_TYPE_ARG4
) (uintptr_t) signal
);
4372 current_thread
= saved_thread
;
4374 perror_with_name ("resuming thread");
4376 /* Successfully resumed. Clear state that no longer makes sense,
4377 and mark the LWP as running. Must not do this before resuming
4378 otherwise if that fails other code will be confused. E.g., we'd
4379 later try to stop the LWP and hang forever waiting for a stop
4380 status. Note that we must not throw after this is cleared,
4381 otherwise handle_zombie_lwp_error would get confused. */
4383 lwp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4386 /* Called when we try to resume a stopped LWP and that errors out. If
4387 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4388 or about to become), discard the error, clear any pending status
4389 the LWP may have, and return true (we'll collect the exit status
4390 soon enough). Otherwise, return false. */
4393 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
4395 struct thread_info
*thread
= get_lwp_thread (lp
);
4397 /* If we get an error after resuming the LWP successfully, we'd
4398 confuse !T state for the LWP being gone. */
4399 gdb_assert (lp
->stopped
);
4401 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4402 because even if ptrace failed with ESRCH, the tracee may be "not
4403 yet fully dead", but already refusing ptrace requests. In that
4404 case the tracee has 'R (Running)' state for a little bit
4405 (observed in Linux 3.18). See also the note on ESRCH in the
4406 ptrace(2) man page. Instead, check whether the LWP has any state
4407 other than ptrace-stopped. */
4409 /* Don't assume anything if /proc/PID/status can't be read. */
4410 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread
)) == 0)
4412 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4413 lp
->status_pending_p
= 0;
4419 /* Like linux_resume_one_lwp_throw, but no error is thrown if the LWP
4420 disappears while we try to resume it. */
4423 linux_resume_one_lwp (struct lwp_info
*lwp
,
4424 int step
, int signal
, siginfo_t
*info
)
4428 linux_resume_one_lwp_throw (lwp
, step
, signal
, info
);
4430 catch (const gdb_exception_error
&ex
)
4432 if (!check_ptrace_stopped_lwp_gone (lwp
))
4437 /* This function is called once per thread via for_each_thread.
4438 We look up which resume request applies to THREAD and mark it with a
4439 pointer to the appropriate resume request.
4441 This algorithm is O(threads * resume elements), but resume elements
4442 is small (and will remain small at least until GDB supports thread
4446 linux_set_resume_request (thread_info
*thread
, thread_resume
*resume
, size_t n
)
4448 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4450 for (int ndx
= 0; ndx
< n
; ndx
++)
4452 ptid_t ptid
= resume
[ndx
].thread
;
4453 if (ptid
== minus_one_ptid
4454 || ptid
== thread
->id
4455 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4457 || (ptid
.pid () == pid_of (thread
)
4459 || ptid
.lwp () == -1)))
4461 if (resume
[ndx
].kind
== resume_stop
4462 && thread
->last_resume_kind
== resume_stop
)
4465 debug_printf ("already %s LWP %ld at GDB's request\n",
4466 (thread
->last_status
.kind
4467 == TARGET_WAITKIND_STOPPED
)
4475 /* Ignore (wildcard) resume requests for already-resumed
4477 if (resume
[ndx
].kind
!= resume_stop
4478 && thread
->last_resume_kind
!= resume_stop
)
4481 debug_printf ("already %s LWP %ld at GDB's request\n",
4482 (thread
->last_resume_kind
4490 /* Don't let wildcard resumes resume fork children that GDB
4491 does not yet know are new fork children. */
4492 if (lwp
->fork_relative
!= NULL
)
4494 struct lwp_info
*rel
= lwp
->fork_relative
;
4496 if (rel
->status_pending_p
4497 && (rel
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
4498 || rel
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
))
4501 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4507 /* If the thread has a pending event that has already been
4508 reported to GDBserver core, but GDB has not pulled the
4509 event out of the vStopped queue yet, likewise, ignore the
4510 (wildcard) resume request. */
4511 if (in_queued_stop_replies (thread
->id
))
4514 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4519 lwp
->resume
= &resume
[ndx
];
4520 thread
->last_resume_kind
= lwp
->resume
->kind
;
4522 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
4523 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
4525 /* If we had a deferred signal to report, dequeue one now.
4526 This can happen if LWP gets more than one signal while
4527 trying to get out of a jump pad. */
4529 && !lwp
->status_pending_p
4530 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
4532 lwp
->status_pending_p
= 1;
4535 debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
4536 "leaving status pending.\n",
4537 WSTOPSIG (lwp
->status_pending
),
4545 /* No resume action for this thread. */
4549 /* find_thread callback for linux_resume. Return true if this lwp has an
4550 interesting status pending. */
4553 resume_status_pending_p (thread_info
*thread
)
4555 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4557 /* LWPs which will not be resumed are not interesting, because
4558 we might not wait for them next time through linux_wait. */
4559 if (lwp
->resume
== NULL
)
4562 return thread_still_has_status_pending_p (thread
);
4565 /* Return 1 if this lwp that GDB wants running is stopped at an
4566 internal breakpoint that we need to step over. It assumes that any
4567 required STOP_PC adjustment has already been propagated to the
4568 inferior's regcache. */
4571 need_step_over_p (thread_info
*thread
)
4573 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4574 struct thread_info
*saved_thread
;
4576 struct process_info
*proc
= get_thread_process (thread
);
4578 /* GDBserver is skipping the extra traps from the wrapper program,
4579 don't have to do step over. */
4580 if (proc
->tdesc
== NULL
)
4583 /* LWPs which will not be resumed are not interesting, because we
4584 might not wait for them next time through linux_wait. */
4589 debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
4594 if (thread
->last_resume_kind
== resume_stop
)
4597 debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
4603 gdb_assert (lwp
->suspended
>= 0);
4608 debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
4613 if (lwp
->status_pending_p
)
4616 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4622 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4626 /* If the PC has changed since we stopped, then don't do anything,
4627 and let the breakpoint/tracepoint be hit. This happens if, for
4628 instance, GDB handled the decr_pc_after_break subtraction itself,
4629 GDB is OOL stepping this thread, or the user has issued a "jump"
4630 command, or poked thread's registers herself. */
4631 if (pc
!= lwp
->stop_pc
)
4634 debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4635 "Old stop_pc was 0x%s, PC is now 0x%s\n",
4637 paddress (lwp
->stop_pc
), paddress (pc
));
4641 /* On software single step target, resume the inferior with signal
4642 rather than stepping over. */
4643 if (can_software_single_step ()
4644 && lwp
->pending_signals
!= NULL
4645 && lwp_signal_can_be_delivered (lwp
))
4648 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4655 saved_thread
= current_thread
;
4656 current_thread
= thread
;
4658 /* We can only step over breakpoints we know about. */
4659 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
4661 /* Don't step over a breakpoint that GDB expects to hit
4662 though. If the condition is being evaluated on the target's side
4663 and it evaluate to false, step over this breakpoint as well. */
4664 if (gdb_breakpoint_here (pc
)
4665 && gdb_condition_true_at_breakpoint (pc
)
4666 && gdb_no_commands_at_breakpoint (pc
))
4669 debug_printf ("Need step over [LWP %ld]? yes, but found"
4670 " GDB breakpoint at 0x%s; skipping step over\n",
4671 lwpid_of (thread
), paddress (pc
));
4673 current_thread
= saved_thread
;
4679 debug_printf ("Need step over [LWP %ld]? yes, "
4680 "found breakpoint at 0x%s\n",
4681 lwpid_of (thread
), paddress (pc
));
4683 /* We've found an lwp that needs stepping over --- return 1 so
4684 that find_thread stops looking. */
4685 current_thread
= saved_thread
;
4691 current_thread
= saved_thread
;
4694 debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
4696 lwpid_of (thread
), paddress (pc
));
4702 linux_process_target::start_step_over (lwp_info
*lwp
)
4704 struct thread_info
*thread
= get_lwp_thread (lwp
);
4705 struct thread_info
*saved_thread
;
4710 debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
4713 stop_all_lwps (1, lwp
);
4715 if (lwp
->suspended
!= 0)
4717 internal_error (__FILE__
, __LINE__
,
4718 "LWP %ld suspended=%d\n", lwpid_of (thread
),
4723 debug_printf ("Done stopping all threads for step-over.\n");
4725 /* Note, we should always reach here with an already adjusted PC,
4726 either by GDB (if we're resuming due to GDB's request), or by our
4727 caller, if we just finished handling an internal breakpoint GDB
4728 shouldn't care about. */
4731 saved_thread
= current_thread
;
4732 current_thread
= thread
;
4734 lwp
->bp_reinsert
= pc
;
4735 uninsert_breakpoints_at (pc
);
4736 uninsert_fast_tracepoint_jumps_at (pc
);
4738 step
= single_step (lwp
);
4740 current_thread
= saved_thread
;
4742 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
4744 /* Require next event from this LWP. */
4745 step_over_bkpt
= thread
->id
;
4748 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
4749 start_step_over, if still there, and delete any single-step
4750 breakpoints we've set, on non hardware single-step targets. */
4753 finish_step_over (struct lwp_info
*lwp
)
4755 if (lwp
->bp_reinsert
!= 0)
4757 struct thread_info
*saved_thread
= current_thread
;
4760 debug_printf ("Finished step over.\n");
4762 current_thread
= get_lwp_thread (lwp
);
4764 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4765 may be no breakpoint to reinsert there by now. */
4766 reinsert_breakpoints_at (lwp
->bp_reinsert
);
4767 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
4769 lwp
->bp_reinsert
= 0;
4771 /* Delete any single-step breakpoints. No longer needed. We
4772 don't have to worry about other threads hitting this trap,
4773 and later not being able to explain it, because we were
4774 stepping over a breakpoint, and we hold all threads but
4775 LWP stopped while doing that. */
4776 if (!can_hardware_single_step ())
4778 gdb_assert (has_single_step_breakpoints (current_thread
));
4779 delete_single_step_breakpoints (current_thread
);
4782 step_over_bkpt
= null_ptid
;
4783 current_thread
= saved_thread
;
4791 linux_process_target::complete_ongoing_step_over ()
4793 if (step_over_bkpt
!= null_ptid
)
4795 struct lwp_info
*lwp
;
4800 debug_printf ("detach: step over in progress, finish it first\n");
4802 /* Passing NULL_PTID as filter indicates we want all events to
4803 be left pending. Eventually this returns when there are no
4804 unwaited-for children left. */
4805 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
,
4807 gdb_assert (ret
== -1);
4809 lwp
= find_lwp_pid (step_over_bkpt
);
4811 finish_step_over (lwp
);
4812 step_over_bkpt
= null_ptid
;
4813 unsuspend_all_lwps (lwp
);
4817 /* This function is called once per thread. We check the thread's resume
4818 request, which will tell us whether to resume, step, or leave the thread
4819 stopped; and what signal, if any, it should be sent.
4821 For threads which we aren't explicitly told otherwise, we preserve
4822 the stepping flag; this is used for stepping over gdbserver-placed
4825 If pending_flags was set in any thread, we queue any needed
4826 signals, since we won't actually resume. We already have a pending
4827 event to report, so we don't need to preserve any step requests;
4828 they should be re-issued if necessary. */
4831 linux_resume_one_thread (thread_info
*thread
, bool leave_all_stopped
)
4833 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4836 if (lwp
->resume
== NULL
)
4839 if (lwp
->resume
->kind
== resume_stop
)
4842 debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread
));
4847 debug_printf ("stopping LWP %ld\n", lwpid_of (thread
));
4849 /* Stop the thread, and wait for the event asynchronously,
4850 through the event loop. */
4856 debug_printf ("already stopped LWP %ld\n",
4859 /* The LWP may have been stopped in an internal event that
4860 was not meant to be notified back to GDB (e.g., gdbserver
4861 breakpoint), so we should be reporting a stop event in
4864 /* If the thread already has a pending SIGSTOP, this is a
4865 no-op. Otherwise, something later will presumably resume
4866 the thread and this will cause it to cancel any pending
4867 operation, due to last_resume_kind == resume_stop. If
4868 the thread already has a pending status to report, we
4869 will still report it the next time we wait - see
4870 status_pending_p_callback. */
4872 /* If we already have a pending signal to report, then
4873 there's no need to queue a SIGSTOP, as this means we're
4874 midway through moving the LWP out of the jumppad, and we
4875 will report the pending signal as soon as that is
4877 if (lwp
->pending_signals_to_report
== NULL
)
4881 /* For stop requests, we're done. */
4883 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4887 /* If this thread which is about to be resumed has a pending status,
4888 then don't resume it - we can just report the pending status.
4889 Likewise if it is suspended, because e.g., another thread is
4890 stepping past a breakpoint. Make sure to queue any signals that
4891 would otherwise be sent. In all-stop mode, we do this decision
4892 based on if *any* thread has a pending status. If there's a
4893 thread that needs the step-over-breakpoint dance, then don't
4894 resume any other thread but that particular one. */
4895 leave_pending
= (lwp
->suspended
4896 || lwp
->status_pending_p
4897 || leave_all_stopped
);
4899 /* If we have a new signal, enqueue the signal. */
4900 if (lwp
->resume
->sig
!= 0)
4902 siginfo_t info
, *info_p
;
4904 /* If this is the same signal we were previously stopped by,
4905 make sure to queue its siginfo. */
4906 if (WIFSTOPPED (lwp
->last_status
)
4907 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
4908 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
),
4909 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
4914 enqueue_pending_signal (lwp
, lwp
->resume
->sig
, info_p
);
4920 debug_printf ("resuming LWP %ld\n", lwpid_of (thread
));
4922 proceed_one_lwp (thread
, NULL
);
4927 debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread
));
4930 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4935 linux_process_target::resume (thread_resume
*resume_info
, size_t n
)
4937 struct thread_info
*need_step_over
= NULL
;
4942 debug_printf ("linux_resume:\n");
4945 for_each_thread ([&] (thread_info
*thread
)
4947 linux_set_resume_request (thread
, resume_info
, n
);
4950 /* If there is a thread which would otherwise be resumed, which has
4951 a pending status, then don't resume any threads - we can just
4952 report the pending status. Make sure to queue any signals that
4953 would otherwise be sent. In non-stop mode, we'll apply this
4954 logic to each thread individually. We consume all pending events
4955 before considering to start a step-over (in all-stop). */
4956 bool any_pending
= false;
4958 any_pending
= find_thread (resume_status_pending_p
) != NULL
;
4960 /* If there is a thread which would otherwise be resumed, which is
4961 stopped at a breakpoint that needs stepping over, then don't
4962 resume any threads - have it step over the breakpoint with all
4963 other threads stopped, then resume all threads again. Make sure
4964 to queue any signals that would otherwise be delivered or
4966 if (!any_pending
&& supports_breakpoints ())
4967 need_step_over
= find_thread (need_step_over_p
);
4969 bool leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
4973 if (need_step_over
!= NULL
)
4974 debug_printf ("Not resuming all, need step over\n");
4975 else if (any_pending
)
4976 debug_printf ("Not resuming, all-stop and found "
4977 "an LWP with pending status\n");
4979 debug_printf ("Resuming, no pending status or step over needed\n");
4982 /* Even if we're leaving threads stopped, queue all signals we'd
4983 otherwise deliver. */
4984 for_each_thread ([&] (thread_info
*thread
)
4986 linux_resume_one_thread (thread
, leave_all_stopped
);
4990 start_step_over (get_thread_lwp (need_step_over
));
4994 debug_printf ("linux_resume done\n");
4998 /* We may have events that were pending that can/should be sent to
4999 the client now. Trigger a linux_wait call. */
5000 if (target_is_async_p ())
5004 /* This function is called once per thread. We check the thread's
5005 last resume request, which will tell us whether to resume, step, or
5006 leave the thread stopped. Any signal the client requested to be
5007 delivered has already been enqueued at this point.
5009 If any thread that GDB wants running is stopped at an internal
5010 breakpoint that needs stepping over, we start a step-over operation
5011 on that particular thread, and leave all others stopped. */
5014 proceed_one_lwp (thread_info
*thread
, lwp_info
*except
)
5016 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5023 debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread
));
5028 debug_printf (" LWP %ld already running\n", lwpid_of (thread
));
5032 if (thread
->last_resume_kind
== resume_stop
5033 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
5036 debug_printf (" client wants LWP to remain %ld stopped\n",
5041 if (lwp
->status_pending_p
)
5044 debug_printf (" LWP %ld has pending status, leaving stopped\n",
5049 gdb_assert (lwp
->suspended
>= 0);
5054 debug_printf (" LWP %ld is suspended\n", lwpid_of (thread
));
5058 if (thread
->last_resume_kind
== resume_stop
5059 && lwp
->pending_signals_to_report
== NULL
5060 && (lwp
->collecting_fast_tracepoint
5061 == fast_tpoint_collect_result::not_collecting
))
5063 /* We haven't reported this LWP as stopped yet (otherwise, the
5064 last_status.kind check above would catch it, and we wouldn't
5065 reach here. This LWP may have been momentarily paused by a
5066 stop_all_lwps call while handling for example, another LWP's
5067 step-over. In that case, the pending expected SIGSTOP signal
5068 that was queued at vCont;t handling time will have already
5069 been consumed by wait_for_sigstop, and so we need to requeue
5070 another one here. Note that if the LWP already has a SIGSTOP
5071 pending, this is a no-op. */
5074 debug_printf ("Client wants LWP %ld to stop. "
5075 "Making sure it has a SIGSTOP pending\n",
5081 if (thread
->last_resume_kind
== resume_step
)
5084 debug_printf (" stepping LWP %ld, client wants it stepping\n",
5087 /* If resume_step is requested by GDB, install single-step
5088 breakpoints when the thread is about to be actually resumed if
5089 the single-step breakpoints weren't removed. */
5090 if (can_software_single_step ()
5091 && !has_single_step_breakpoints (thread
))
5092 install_software_single_step_breakpoints (lwp
);
5094 step
= maybe_hw_step (thread
);
5096 else if (lwp
->bp_reinsert
!= 0)
5099 debug_printf (" stepping LWP %ld, reinsert set\n",
5102 step
= maybe_hw_step (thread
);
5107 linux_resume_one_lwp (lwp
, step
, 0, NULL
);
5111 unsuspend_and_proceed_one_lwp (thread_info
*thread
, lwp_info
*except
)
5113 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5118 lwp_suspended_decr (lwp
);
5120 proceed_one_lwp (thread
, except
);
5124 linux_process_target::proceed_all_lwps ()
5126 struct thread_info
*need_step_over
;
5128 /* If there is a thread which would otherwise be resumed, which is
5129 stopped at a breakpoint that needs stepping over, then don't
5130 resume any threads - have it step over the breakpoint with all
5131 other threads stopped, then resume all threads again. */
5133 if (supports_breakpoints ())
5135 need_step_over
= find_thread (need_step_over_p
);
5137 if (need_step_over
!= NULL
)
5140 debug_printf ("proceed_all_lwps: found "
5141 "thread %ld needing a step-over\n",
5142 lwpid_of (need_step_over
));
5144 start_step_over (get_thread_lwp (need_step_over
));
5150 debug_printf ("Proceeding, no step-over needed\n");
5152 for_each_thread ([] (thread_info
*thread
)
5154 proceed_one_lwp (thread
, NULL
);
5159 linux_process_target::unstop_all_lwps (int unsuspend
, lwp_info
*except
)
5165 debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
5166 lwpid_of (get_lwp_thread (except
)));
5168 debug_printf ("unstopping all lwps\n");
5172 for_each_thread ([&] (thread_info
*thread
)
5174 unsuspend_and_proceed_one_lwp (thread
, except
);
5177 for_each_thread ([&] (thread_info
*thread
)
5179 proceed_one_lwp (thread
, except
);
5184 debug_printf ("unstop_all_lwps done\n");
5190 #ifdef HAVE_LINUX_REGSETS
5192 #define use_linux_regsets 1
5194 /* Returns true if REGSET has been disabled. */
5197 regset_disabled (struct regsets_info
*info
, struct regset_info
*regset
)
5199 return (info
->disabled_regsets
!= NULL
5200 && info
->disabled_regsets
[regset
- info
->regsets
]);
5203 /* Disable REGSET. */
5206 disable_regset (struct regsets_info
*info
, struct regset_info
*regset
)
5210 dr_offset
= regset
- info
->regsets
;
5211 if (info
->disabled_regsets
== NULL
)
5212 info
->disabled_regsets
= (char *) xcalloc (1, info
->num_regsets
);
5213 info
->disabled_regsets
[dr_offset
] = 1;
5217 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
5218 struct regcache
*regcache
)
5220 struct regset_info
*regset
;
5221 int saw_general_regs
= 0;
5225 pid
= lwpid_of (current_thread
);
5226 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5231 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
5234 buf
= xmalloc (regset
->size
);
5236 nt_type
= regset
->nt_type
;
5240 iov
.iov_len
= regset
->size
;
5241 data
= (void *) &iov
;
5247 res
= ptrace (regset
->get_request
, pid
,
5248 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5250 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5255 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5257 /* If we get EIO on a regset, or an EINVAL and the regset is
5258 optional, do not try it again for this process mode. */
5259 disable_regset (regsets_info
, regset
);
5261 else if (errno
== ENODATA
)
5263 /* ENODATA may be returned if the regset is currently
5264 not "active". This can happen in normal operation,
5265 so suppress the warning in this case. */
5267 else if (errno
== ESRCH
)
5269 /* At this point, ESRCH should mean the process is
5270 already gone, in which case we simply ignore attempts
5271 to read its registers. */
5276 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
5283 if (regset
->type
== GENERAL_REGS
)
5284 saw_general_regs
= 1;
5285 regset
->store_function (regcache
, buf
);
5289 if (saw_general_regs
)
5296 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
5297 struct regcache
*regcache
)
5299 struct regset_info
*regset
;
5300 int saw_general_regs
= 0;
5304 pid
= lwpid_of (current_thread
);
5305 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5310 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
)
5311 || regset
->fill_function
== NULL
)
5314 buf
= xmalloc (regset
->size
);
5316 /* First fill the buffer with the current register set contents,
5317 in case there are any items in the kernel's regset that are
5318 not in gdbserver's regcache. */
5320 nt_type
= regset
->nt_type
;
5324 iov
.iov_len
= regset
->size
;
5325 data
= (void *) &iov
;
5331 res
= ptrace (regset
->get_request
, pid
,
5332 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5334 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5339 /* Then overlay our cached registers on that. */
5340 regset
->fill_function (regcache
, buf
);
5342 /* Only now do we write the register set. */
5344 res
= ptrace (regset
->set_request
, pid
,
5345 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5347 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
5354 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5356 /* If we get EIO on a regset, or an EINVAL and the regset is
5357 optional, do not try it again for this process mode. */
5358 disable_regset (regsets_info
, regset
);
5360 else if (errno
== ESRCH
)
5362 /* At this point, ESRCH should mean the process is
5363 already gone, in which case we simply ignore attempts
5364 to change its registers. See also the related
5365 comment in linux_resume_one_lwp. */
5371 perror ("Warning: ptrace(regsets_store_inferior_registers)");
5374 else if (regset
->type
== GENERAL_REGS
)
5375 saw_general_regs
= 1;
5378 if (saw_general_regs
)
5384 #else /* !HAVE_LINUX_REGSETS */
5386 #define use_linux_regsets 0
5387 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5388 #define regsets_store_inferior_registers(regsets_info, regcache) 1
5392 /* Return 1 if register REGNO is supported by one of the regset ptrace
5393 calls or 0 if it has to be transferred individually. */
5396 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
5398 unsigned char mask
= 1 << (regno
% 8);
5399 size_t index
= regno
/ 8;
5401 return (use_linux_regsets
5402 && (regs_info
->regset_bitmap
== NULL
5403 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
5406 #ifdef HAVE_LINUX_USRREGS
5409 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
5413 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
5414 error ("Invalid register number %d.", regnum
);
5416 addr
= usrregs
->regmap
[regnum
];
5421 /* Fetch one register. */
5423 fetch_register (const struct usrregs_info
*usrregs
,
5424 struct regcache
*regcache
, int regno
)
5431 if (regno
>= usrregs
->num_regs
)
5433 if ((*the_low_target
.cannot_fetch_register
) (regno
))
5436 regaddr
= register_addr (usrregs
, regno
);
5440 size
= ((register_size (regcache
->tdesc
, regno
)
5441 + sizeof (PTRACE_XFER_TYPE
) - 1)
5442 & -sizeof (PTRACE_XFER_TYPE
));
5443 buf
= (char *) alloca (size
);
5445 pid
= lwpid_of (current_thread
);
5446 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5449 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
5450 ptrace (PTRACE_PEEKUSER
, pid
,
5451 /* Coerce to a uintptr_t first to avoid potential gcc warning
5452 of coercing an 8 byte integer to a 4 byte pointer. */
5453 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
, (PTRACE_TYPE_ARG4
) 0);
5454 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5457 /* Mark register REGNO unavailable. */
5458 supply_register (regcache
, regno
, NULL
);
5463 if (the_low_target
.supply_ptrace_register
)
5464 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
5466 supply_register (regcache
, regno
, buf
);
5469 /* Store one register. */
5471 store_register (const struct usrregs_info
*usrregs
,
5472 struct regcache
*regcache
, int regno
)
5479 if (regno
>= usrregs
->num_regs
)
5481 if ((*the_low_target
.cannot_store_register
) (regno
))
5484 regaddr
= register_addr (usrregs
, regno
);
5488 size
= ((register_size (regcache
->tdesc
, regno
)
5489 + sizeof (PTRACE_XFER_TYPE
) - 1)
5490 & -sizeof (PTRACE_XFER_TYPE
));
5491 buf
= (char *) alloca (size
);
5492 memset (buf
, 0, size
);
5494 if (the_low_target
.collect_ptrace_register
)
5495 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
5497 collect_register (regcache
, regno
, buf
);
5499 pid
= lwpid_of (current_thread
);
5500 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5503 ptrace (PTRACE_POKEUSER
, pid
,
5504 /* Coerce to a uintptr_t first to avoid potential gcc warning
5505 about coercing an 8 byte integer to a 4 byte pointer. */
5506 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
,
5507 (PTRACE_TYPE_ARG4
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
5510 /* At this point, ESRCH should mean the process is
5511 already gone, in which case we simply ignore attempts
5512 to change its registers. See also the related
5513 comment in linux_resume_one_lwp. */
5517 if ((*the_low_target
.cannot_store_register
) (regno
) == 0)
5518 error ("writing register %d: %s", regno
, safe_strerror (errno
));
5520 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5524 /* Fetch all registers, or just one, from the child process.
5525 If REGNO is -1, do this for all registers, skipping any that are
5526 assumed to have been retrieved by regsets_fetch_inferior_registers,
5527 unless ALL is non-zero.
5528 Otherwise, REGNO specifies which register (so we can save time). */
5530 usr_fetch_inferior_registers (const struct regs_info
*regs_info
,
5531 struct regcache
*regcache
, int regno
, int all
)
5533 struct usrregs_info
*usr
= regs_info
->usrregs
;
5537 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5538 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5539 fetch_register (usr
, regcache
, regno
);
5542 fetch_register (usr
, regcache
, regno
);
5545 /* Store our register values back into the inferior.
5546 If REGNO is -1, do this for all registers, skipping any that are
5547 assumed to have been saved by regsets_store_inferior_registers,
5548 unless ALL is non-zero.
5549 Otherwise, REGNO specifies which register (so we can save time). */
5551 usr_store_inferior_registers (const struct regs_info
*regs_info
,
5552 struct regcache
*regcache
, int regno
, int all
)
5554 struct usrregs_info
*usr
= regs_info
->usrregs
;
5558 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5559 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5560 store_register (usr
, regcache
, regno
);
5563 store_register (usr
, regcache
, regno
);
5566 #else /* !HAVE_LINUX_USRREGS */
5568 #define usr_fetch_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
5569 #define usr_store_inferior_registers(regs_info, regcache, regno, all) do {} while (0)
5575 linux_process_target::fetch_registers (regcache
*regcache
, int regno
)
5579 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
5583 if (the_low_target
.fetch_register
!= NULL
5584 && regs_info
->usrregs
!= NULL
)
5585 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
5586 (*the_low_target
.fetch_register
) (regcache
, regno
);
5588 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
5589 if (regs_info
->usrregs
!= NULL
)
5590 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
5594 if (the_low_target
.fetch_register
!= NULL
5595 && (*the_low_target
.fetch_register
) (regcache
, regno
))
5598 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5600 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
5602 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5603 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
5608 linux_process_target::store_registers (regcache
*regcache
, int regno
)
5612 const struct regs_info
*regs_info
= (*the_low_target
.regs_info
) ();
5616 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5618 if (regs_info
->usrregs
!= NULL
)
5619 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
5623 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5625 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5627 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5628 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
5633 /* A wrapper for the read_memory target op. */
5636 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
5638 return the_target
->read_memory (memaddr
, myaddr
, len
);
5641 /* Copy LEN bytes from inferior's memory starting at MEMADDR
5642 to debugger memory starting at MYADDR. */
5645 linux_process_target::read_memory (CORE_ADDR memaddr
,
5646 unsigned char *myaddr
, int len
)
5648 int pid
= lwpid_of (current_thread
);
5649 PTRACE_XFER_TYPE
*buffer
;
5657 /* Try using /proc. Don't bother for one word. */
5658 if (len
>= 3 * sizeof (long))
5662 /* We could keep this file open and cache it - possibly one per
5663 thread. That requires some juggling, but is even faster. */
5664 sprintf (filename
, "/proc/%d/mem", pid
);
5665 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
5669 /* If pread64 is available, use it. It's faster if the kernel
5670 supports it (only one syscall), and it's 64-bit safe even on
5671 32-bit platforms (for instance, SPARC debugging a SPARC64
5674 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
5677 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
5678 bytes
= read (fd
, myaddr
, len
);
5685 /* Some data was read, we'll try to get the rest with ptrace. */
5695 /* Round starting address down to longword boundary. */
5696 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5697 /* Round ending address up; get number of longwords that makes. */
5698 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5699 / sizeof (PTRACE_XFER_TYPE
));
5700 /* Allocate buffer of that many longwords. */
5701 buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5703 /* Read all the longwords */
5705 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5707 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5708 about coercing an 8 byte integer to a 4 byte pointer. */
5709 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
5710 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5711 (PTRACE_TYPE_ARG4
) 0);
5717 /* Copy appropriate bytes out of the buffer. */
5720 i
*= sizeof (PTRACE_XFER_TYPE
);
5721 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
5723 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5730 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5731 memory at MEMADDR. On failure (cannot write to the inferior)
5732 returns the value of errno. Always succeeds if LEN is zero. */
5735 linux_process_target::write_memory (CORE_ADDR memaddr
,
5736 const unsigned char *myaddr
, int len
)
5739 /* Round starting address down to longword boundary. */
5740 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5741 /* Round ending address up; get number of longwords that makes. */
5743 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5744 / sizeof (PTRACE_XFER_TYPE
);
5746 /* Allocate buffer of that many longwords. */
5747 PTRACE_XFER_TYPE
*buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5749 int pid
= lwpid_of (current_thread
);
5753 /* Zero length write always succeeds. */
5759 /* Dump up to four bytes. */
5760 char str
[4 * 2 + 1];
5762 int dump
= len
< 4 ? len
: 4;
5764 for (i
= 0; i
< dump
; i
++)
5766 sprintf (p
, "%02x", myaddr
[i
]);
5771 debug_printf ("Writing %s to 0x%08lx in process %d\n",
5772 str
, (long) memaddr
, pid
);
5775 /* Fill start and end extra bytes of buffer with existing memory data. */
5778 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5779 about coercing an 8 byte integer to a 4 byte pointer. */
5780 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
5781 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5782 (PTRACE_TYPE_ARG4
) 0);
5790 = ptrace (PTRACE_PEEKTEXT
, pid
,
5791 /* Coerce to a uintptr_t first to avoid potential gcc warning
5792 about coercing an 8 byte integer to a 4 byte pointer. */
5793 (PTRACE_TYPE_ARG3
) (uintptr_t) (addr
+ (count
- 1)
5794 * sizeof (PTRACE_XFER_TYPE
)),
5795 (PTRACE_TYPE_ARG4
) 0);
5800 /* Copy data to be written over corresponding part of buffer. */
5802 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5805 /* Write the entire buffer. */
5807 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5810 ptrace (PTRACE_POKETEXT
, pid
,
5811 /* Coerce to a uintptr_t first to avoid potential gcc warning
5812 about coercing an 8 byte integer to a 4 byte pointer. */
5813 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5814 (PTRACE_TYPE_ARG4
) buffer
[i
]);
5823 linux_process_target::look_up_symbols ()
5825 #ifdef USE_THREAD_DB
5826 struct process_info
*proc
= current_process ();
5828 if (proc
->priv
->thread_db
!= NULL
)
5836 linux_process_target::request_interrupt ()
5838 /* Send a SIGINT to the process group. This acts just like the user
5839 typed a ^C on the controlling terminal. */
5840 ::kill (-signal_pid
, SIGINT
);
5844 linux_process_target::supports_read_auxv ()
5849 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5850 to debugger memory starting at MYADDR. */
5853 linux_process_target::read_auxv (CORE_ADDR offset
, unsigned char *myaddr
,
5856 char filename
[PATH_MAX
];
5858 int pid
= lwpid_of (current_thread
);
5860 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5862 fd
= open (filename
, O_RDONLY
);
5866 if (offset
!= (CORE_ADDR
) 0
5867 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5870 n
= read (fd
, myaddr
, len
);
5877 /* These breakpoint and watchpoint related wrapper functions simply
5878 pass on the function call if the target has registered a
5879 corresponding function. */
5882 linux_process_target::supports_z_point_type (char z_type
)
5884 return (the_low_target
.supports_z_point_type
!= NULL
5885 && the_low_target
.supports_z_point_type (z_type
));
5889 linux_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5890 int size
, raw_breakpoint
*bp
)
5892 if (type
== raw_bkpt_type_sw
)
5893 return insert_memory_breakpoint (bp
);
5894 else if (the_low_target
.insert_point
!= NULL
)
5895 return the_low_target
.insert_point (type
, addr
, size
, bp
);
5897 /* Unsupported (see target.h). */
5902 linux_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5903 int size
, raw_breakpoint
*bp
)
5905 if (type
== raw_bkpt_type_sw
)
5906 return remove_memory_breakpoint (bp
);
5907 else if (the_low_target
.remove_point
!= NULL
)
5908 return the_low_target
.remove_point (type
, addr
, size
, bp
);
5910 /* Unsupported (see target.h). */
5914 /* Implement the stopped_by_sw_breakpoint target_ops
5918 linux_process_target::stopped_by_sw_breakpoint ()
5920 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5922 return (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
);
5925 /* Implement the supports_stopped_by_sw_breakpoint target_ops
5929 linux_process_target::supports_stopped_by_sw_breakpoint ()
5931 return USE_SIGTRAP_SIGINFO
;
5934 /* Implement the stopped_by_hw_breakpoint target_ops
5938 linux_process_target::stopped_by_hw_breakpoint ()
5940 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5942 return (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
);
5945 /* Implement the supports_stopped_by_hw_breakpoint target_ops
5949 linux_process_target::supports_stopped_by_hw_breakpoint ()
5951 return USE_SIGTRAP_SIGINFO
;
5954 /* Implement the supports_hardware_single_step target_ops method. */
5957 linux_process_target::supports_hardware_single_step ()
5959 return can_hardware_single_step ();
5963 linux_process_target::supports_software_single_step ()
5965 return can_software_single_step ();
5969 linux_process_target::stopped_by_watchpoint ()
5971 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5973 return lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
5977 linux_process_target::stopped_data_address ()
5979 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5981 return lwp
->stopped_data_address
;
5984 /* This is only used for targets that define PT_TEXT_ADDR,
5985 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5986 the target has different ways of acquiring this information, like
5990 linux_process_target::supports_read_offsets ()
5992 #ifdef SUPPORTS_READ_OFFSETS
5999 /* Under uClinux, programs are loaded at non-zero offsets, which we need
6000 to tell gdb about. */
6003 linux_process_target::read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
6005 #ifdef SUPPORTS_READ_OFFSETS
6006 unsigned long text
, text_end
, data
;
6007 int pid
= lwpid_of (current_thread
);
6011 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_ADDR
,
6012 (PTRACE_TYPE_ARG4
) 0);
6013 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_END_ADDR
,
6014 (PTRACE_TYPE_ARG4
) 0);
6015 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_DATA_ADDR
,
6016 (PTRACE_TYPE_ARG4
) 0);
6020 /* Both text and data offsets produced at compile-time (and so
6021 used by gdb) are relative to the beginning of the program,
6022 with the data segment immediately following the text segment.
6023 However, the actual runtime layout in memory may put the data
6024 somewhere else, so when we send gdb a data base-address, we
6025 use the real data base address and subtract the compile-time
6026 data base-address from it (which is just the length of the
6027 text segment). BSS immediately follows data in both
6030 *data_p
= data
- (text_end
- text
);
6036 gdb_assert_not_reached ("target op read_offsets not supported");
6041 linux_process_target::supports_get_tls_address ()
6043 #ifdef USE_THREAD_DB
6051 linux_process_target::get_tls_address (thread_info
*thread
,
6053 CORE_ADDR load_module
,
6056 #ifdef USE_THREAD_DB
6057 return thread_db_get_tls_address (thread
, offset
, load_module
, address
);
6064 linux_process_target::supports_qxfer_osdata ()
6070 linux_process_target::qxfer_osdata (const char *annex
,
6071 unsigned char *readbuf
,
6072 unsigned const char *writebuf
,
6073 CORE_ADDR offset
, int len
)
6075 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
6078 /* Convert a native/host siginfo object, into/from the siginfo in the
6079 layout of the inferiors' architecture. */
6082 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
6086 if (the_low_target
.siginfo_fixup
!= NULL
)
6087 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
6089 /* If there was no callback, or the callback didn't do anything,
6090 then just do a straight memcpy. */
6094 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
6096 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
6101 linux_process_target::supports_qxfer_siginfo ()
6107 linux_process_target::qxfer_siginfo (const char *annex
,
6108 unsigned char *readbuf
,
6109 unsigned const char *writebuf
,
6110 CORE_ADDR offset
, int len
)
6114 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
6116 if (current_thread
== NULL
)
6119 pid
= lwpid_of (current_thread
);
6122 debug_printf ("%s siginfo for lwp %d.\n",
6123 readbuf
!= NULL
? "Reading" : "Writing",
6126 if (offset
>= sizeof (siginfo
))
6129 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6132 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
6133 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
6134 inferior with a 64-bit GDBSERVER should look the same as debugging it
6135 with a 32-bit GDBSERVER, we need to convert it. */
6136 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
6138 if (offset
+ len
> sizeof (siginfo
))
6139 len
= sizeof (siginfo
) - offset
;
6141 if (readbuf
!= NULL
)
6142 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
6145 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
6147 /* Convert back to ptrace layout before flushing it out. */
6148 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
6150 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6157 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
6158 so we notice when children change state; as the handler for the
6159 sigsuspend in my_waitpid. */
6162 sigchld_handler (int signo
)
6164 int old_errno
= errno
;
6170 /* Use the async signal safe debug function. */
6171 if (debug_write ("sigchld_handler\n",
6172 sizeof ("sigchld_handler\n") - 1) < 0)
6173 break; /* just ignore */
6177 if (target_is_async_p ())
6178 async_file_mark (); /* trigger a linux_wait */
6184 linux_process_target::supports_non_stop ()
6190 linux_process_target::async (bool enable
)
6192 bool previous
= target_is_async_p ();
6195 debug_printf ("linux_async (%d), previous=%d\n",
6198 if (previous
!= enable
)
6201 sigemptyset (&mask
);
6202 sigaddset (&mask
, SIGCHLD
);
6204 gdb_sigmask (SIG_BLOCK
, &mask
, NULL
);
6208 if (pipe (linux_event_pipe
) == -1)
6210 linux_event_pipe
[0] = -1;
6211 linux_event_pipe
[1] = -1;
6212 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6214 warning ("creating event pipe failed.");
6218 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
6219 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
6221 /* Register the event loop handler. */
6222 add_file_handler (linux_event_pipe
[0],
6223 handle_target_event
, NULL
);
6225 /* Always trigger a linux_wait. */
6230 delete_file_handler (linux_event_pipe
[0]);
6232 close (linux_event_pipe
[0]);
6233 close (linux_event_pipe
[1]);
6234 linux_event_pipe
[0] = -1;
6235 linux_event_pipe
[1] = -1;
6238 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6245 linux_process_target::start_non_stop (bool nonstop
)
6247 /* Register or unregister from event-loop accordingly. */
6248 target_async (nonstop
);
6250 if (target_is_async_p () != (nonstop
!= false))
6257 linux_process_target::supports_multi_process ()
6262 /* Check if fork events are supported. */
6265 linux_process_target::supports_fork_events ()
6267 return linux_supports_tracefork ();
6270 /* Check if vfork events are supported. */
6273 linux_process_target::supports_vfork_events ()
6275 return linux_supports_tracefork ();
6278 /* Check if exec events are supported. */
6281 linux_process_target::supports_exec_events ()
6283 return linux_supports_traceexec ();
6286 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
6287 ptrace flags for all inferiors. This is in case the new GDB connection
6288 doesn't support the same set of events that the previous one did. */
6291 linux_process_target::handle_new_gdb_connection ()
6293 /* Request that all the lwps reset their ptrace options. */
6294 for_each_thread ([] (thread_info
*thread
)
6296 struct lwp_info
*lwp
= get_thread_lwp (thread
);
6300 /* Stop the lwp so we can modify its ptrace options. */
6301 lwp
->must_set_ptrace_flags
= 1;
6302 linux_stop_lwp (lwp
);
6306 /* Already stopped; go ahead and set the ptrace options. */
6307 struct process_info
*proc
= find_process_pid (pid_of (thread
));
6308 int options
= linux_low_ptrace_options (proc
->attached
);
6310 linux_enable_event_reporting (lwpid_of (thread
), options
);
6311 lwp
->must_set_ptrace_flags
= 0;
6317 linux_process_target::handle_monitor_command (char *mon
)
6319 #ifdef USE_THREAD_DB
6320 return thread_db_handle_monitor_command (mon
);
6327 linux_process_target::core_of_thread (ptid_t ptid
)
6329 return linux_common_core_of_thread (ptid
);
6333 linux_process_target::supports_disable_randomization ()
6335 #ifdef HAVE_PERSONALITY
6343 linux_process_target::supports_agent ()
6349 linux_process_target::supports_range_stepping ()
6351 if (can_software_single_step ())
6353 if (*the_low_target
.supports_range_stepping
== NULL
)
6356 return (*the_low_target
.supports_range_stepping
) ();
6360 linux_process_target::supports_pid_to_exec_file ()
6366 linux_process_target::pid_to_exec_file (int pid
)
6368 return linux_proc_pid_to_exec_file (pid
);
6372 linux_process_target::supports_multifs ()
6378 linux_process_target::multifs_open (int pid
, const char *filename
,
6379 int flags
, mode_t mode
)
6381 return linux_mntns_open_cloexec (pid
, filename
, flags
, mode
);
6385 linux_process_target::multifs_unlink (int pid
, const char *filename
)
6387 return linux_mntns_unlink (pid
, filename
);
6391 linux_process_target::multifs_readlink (int pid
, const char *filename
,
6392 char *buf
, size_t bufsiz
)
6394 return linux_mntns_readlink (pid
, filename
, buf
, bufsiz
);
6397 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
6398 struct target_loadseg
6400 /* Core address to which the segment is mapped. */
6402 /* VMA recorded in the program header. */
6404 /* Size of this segment in memory. */
6408 # if defined PT_GETDSBT
6409 struct target_loadmap
6411 /* Protocol version number, must be zero. */
6413 /* Pointer to the DSBT table, its size, and the DSBT index. */
6414 unsigned *dsbt_table
;
6415 unsigned dsbt_size
, dsbt_index
;
6416 /* Number of segments in this map. */
6418 /* The actual memory map. */
6419 struct target_loadseg segs
[/*nsegs*/];
6421 # define LINUX_LOADMAP PT_GETDSBT
6422 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6423 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6425 struct target_loadmap
6427 /* Protocol version number, must be zero. */
6429 /* Number of segments in this map. */
6431 /* The actual memory map. */
6432 struct target_loadseg segs
[/*nsegs*/];
6434 # define LINUX_LOADMAP PTRACE_GETFDPIC
6435 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6436 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6440 linux_process_target::supports_read_loadmap ()
6446 linux_process_target::read_loadmap (const char *annex
, CORE_ADDR offset
,
6447 unsigned char *myaddr
, unsigned int len
)
6449 int pid
= lwpid_of (current_thread
);
6451 struct target_loadmap
*data
= NULL
;
6452 unsigned int actual_length
, copy_length
;
6454 if (strcmp (annex
, "exec") == 0)
6455 addr
= (int) LINUX_LOADMAP_EXEC
;
6456 else if (strcmp (annex
, "interp") == 0)
6457 addr
= (int) LINUX_LOADMAP_INTERP
;
6461 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
6467 actual_length
= sizeof (struct target_loadmap
)
6468 + sizeof (struct target_loadseg
) * data
->nsegs
;
6470 if (offset
< 0 || offset
> actual_length
)
6473 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
6474 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
6477 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6480 linux_process_target::process_qsupported (char **features
, int count
)
6482 if (the_low_target
.process_qsupported
!= NULL
)
6483 the_low_target
.process_qsupported (features
, count
);
6487 linux_process_target::supports_catch_syscall ()
6489 return (the_low_target
.get_syscall_trapinfo
!= NULL
6490 && linux_supports_tracesysgood ());
6494 linux_process_target::get_ipa_tdesc_idx ()
6496 if (the_low_target
.get_ipa_tdesc_idx
== NULL
)
6499 return (*the_low_target
.get_ipa_tdesc_idx
) ();
6503 linux_process_target::supports_tracepoints ()
6505 if (*the_low_target
.supports_tracepoints
== NULL
)
6508 return (*the_low_target
.supports_tracepoints
) ();
6512 linux_process_target::read_pc (regcache
*regcache
)
6514 if (the_low_target
.get_pc
== NULL
)
6517 return (*the_low_target
.get_pc
) (regcache
);
6521 linux_process_target::write_pc (regcache
*regcache
, CORE_ADDR pc
)
6523 gdb_assert (the_low_target
.set_pc
!= NULL
);
6525 (*the_low_target
.set_pc
) (regcache
, pc
);
6529 linux_process_target::supports_thread_stopped ()
6535 linux_process_target::thread_stopped (thread_info
*thread
)
6537 return get_thread_lwp (thread
)->stopped
;
6540 /* This exposes stop-all-threads functionality to other modules. */
6543 linux_process_target::pause_all (bool freeze
)
6545 stop_all_lwps (freeze
, NULL
);
6548 /* This exposes unstop-all-threads functionality to other gdbserver
6552 linux_process_target::unpause_all (bool unfreeze
)
6554 unstop_all_lwps (unfreeze
, NULL
);
6558 linux_process_target::prepare_to_access_memory ()
6560 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6563 target_pause_all (true);
6568 linux_process_target::done_accessing_memory ()
6570 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6573 target_unpause_all (true);
6577 linux_process_target::supports_fast_tracepoints ()
6579 return the_low_target
.install_fast_tracepoint_jump_pad
!= nullptr;
6583 linux_process_target::install_fast_tracepoint_jump_pad
6584 (CORE_ADDR tpoint
, CORE_ADDR tpaddr
, CORE_ADDR collector
,
6585 CORE_ADDR lockaddr
, ULONGEST orig_size
, CORE_ADDR
*jump_entry
,
6586 CORE_ADDR
*trampoline
, ULONGEST
*trampoline_size
,
6587 unsigned char *jjump_pad_insn
, ULONGEST
*jjump_pad_insn_size
,
6588 CORE_ADDR
*adjusted_insn_addr
, CORE_ADDR
*adjusted_insn_addr_end
,
6591 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
6592 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
6593 jump_entry
, trampoline
, trampoline_size
,
6594 jjump_pad_insn
, jjump_pad_insn_size
,
6595 adjusted_insn_addr
, adjusted_insn_addr_end
,
6600 linux_process_target::emit_ops ()
6602 if (the_low_target
.emit_ops
!= NULL
)
6603 return (*the_low_target
.emit_ops
) ();
6609 linux_process_target::get_min_fast_tracepoint_insn_len ()
6611 return (*the_low_target
.get_min_fast_tracepoint_insn_len
) ();
6614 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6617 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
6618 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
6620 char filename
[PATH_MAX
];
6622 const int auxv_size
= is_elf64
6623 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
6624 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
6626 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
6628 fd
= open (filename
, O_RDONLY
);
6634 while (read (fd
, buf
, auxv_size
) == auxv_size
6635 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
6639 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
6641 switch (aux
->a_type
)
6644 *phdr_memaddr
= aux
->a_un
.a_val
;
6647 *num_phdr
= aux
->a_un
.a_val
;
6653 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
6655 switch (aux
->a_type
)
6658 *phdr_memaddr
= aux
->a_un
.a_val
;
6661 *num_phdr
= aux
->a_un
.a_val
;
6669 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
6671 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6672 "phdr_memaddr = %ld, phdr_num = %d",
6673 (long) *phdr_memaddr
, *num_phdr
);
6680 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6683 get_dynamic (const int pid
, const int is_elf64
)
6685 CORE_ADDR phdr_memaddr
, relocation
;
6687 unsigned char *phdr_buf
;
6688 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
6690 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
6693 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
6694 phdr_buf
= (unsigned char *) alloca (num_phdr
* phdr_size
);
6696 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
6699 /* Compute relocation: it is expected to be 0 for "regular" executables,
6700 non-zero for PIE ones. */
6702 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
6705 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6707 if (p
->p_type
== PT_PHDR
)
6708 relocation
= phdr_memaddr
- p
->p_vaddr
;
6712 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6714 if (p
->p_type
== PT_PHDR
)
6715 relocation
= phdr_memaddr
- p
->p_vaddr
;
6718 if (relocation
== -1)
6720 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6721 any real world executables, including PIE executables, have always
6722 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6723 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6724 or present DT_DEBUG anyway (fpc binaries are statically linked).
6726 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6728 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6733 for (i
= 0; i
< num_phdr
; i
++)
6737 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6739 if (p
->p_type
== PT_DYNAMIC
)
6740 return p
->p_vaddr
+ relocation
;
6744 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6746 if (p
->p_type
== PT_DYNAMIC
)
6747 return p
->p_vaddr
+ relocation
;
6754 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6755 can be 0 if the inferior does not yet have the library list initialized.
6756 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6757 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6760 get_r_debug (const int pid
, const int is_elf64
)
6762 CORE_ADDR dynamic_memaddr
;
6763 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
6764 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
6767 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
6768 if (dynamic_memaddr
== 0)
6771 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
6775 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
6776 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6780 unsigned char buf
[sizeof (Elf64_Xword
)];
6784 #ifdef DT_MIPS_RLD_MAP
6785 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6787 if (linux_read_memory (dyn
->d_un
.d_val
,
6788 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6793 #endif /* DT_MIPS_RLD_MAP */
6794 #ifdef DT_MIPS_RLD_MAP_REL
6795 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6797 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6798 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6803 #endif /* DT_MIPS_RLD_MAP_REL */
6805 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6806 map
= dyn
->d_un
.d_val
;
6808 if (dyn
->d_tag
== DT_NULL
)
6813 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
6814 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6818 unsigned char buf
[sizeof (Elf32_Word
)];
6822 #ifdef DT_MIPS_RLD_MAP
6823 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6825 if (linux_read_memory (dyn
->d_un
.d_val
,
6826 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6831 #endif /* DT_MIPS_RLD_MAP */
6832 #ifdef DT_MIPS_RLD_MAP_REL
6833 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6835 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6836 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6841 #endif /* DT_MIPS_RLD_MAP_REL */
6843 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6844 map
= dyn
->d_un
.d_val
;
6846 if (dyn
->d_tag
== DT_NULL
)
6850 dynamic_memaddr
+= dyn_size
;
6856 /* Read one pointer from MEMADDR in the inferior. */
6859 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
6863 /* Go through a union so this works on either big or little endian
6864 hosts, when the inferior's pointer size is smaller than the size
6865 of CORE_ADDR. It is assumed the inferior's endianness is the
6866 same of the superior's. */
6869 CORE_ADDR core_addr
;
6874 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
6877 if (ptr_size
== sizeof (CORE_ADDR
))
6878 *ptr
= addr
.core_addr
;
6879 else if (ptr_size
== sizeof (unsigned int))
6882 gdb_assert_not_reached ("unhandled pointer size");
6888 linux_process_target::supports_qxfer_libraries_svr4 ()
6893 struct link_map_offsets
6895 /* Offset and size of r_debug.r_version. */
6896 int r_version_offset
;
6898 /* Offset and size of r_debug.r_map. */
6901 /* Offset to l_addr field in struct link_map. */
6904 /* Offset to l_name field in struct link_map. */
6907 /* Offset to l_ld field in struct link_map. */
6910 /* Offset to l_next field in struct link_map. */
6913 /* Offset to l_prev field in struct link_map. */
6917 /* Construct qXfer:libraries-svr4:read reply. */
6920 linux_process_target::qxfer_libraries_svr4 (const char *annex
,
6921 unsigned char *readbuf
,
6922 unsigned const char *writebuf
,
6923 CORE_ADDR offset
, int len
)
6925 struct process_info_private
*const priv
= current_process ()->priv
;
6926 char filename
[PATH_MAX
];
6929 static const struct link_map_offsets lmo_32bit_offsets
=
6931 0, /* r_version offset. */
6932 4, /* r_debug.r_map offset. */
6933 0, /* l_addr offset in link_map. */
6934 4, /* l_name offset in link_map. */
6935 8, /* l_ld offset in link_map. */
6936 12, /* l_next offset in link_map. */
6937 16 /* l_prev offset in link_map. */
6940 static const struct link_map_offsets lmo_64bit_offsets
=
6942 0, /* r_version offset. */
6943 8, /* r_debug.r_map offset. */
6944 0, /* l_addr offset in link_map. */
6945 8, /* l_name offset in link_map. */
6946 16, /* l_ld offset in link_map. */
6947 24, /* l_next offset in link_map. */
6948 32 /* l_prev offset in link_map. */
6950 const struct link_map_offsets
*lmo
;
6951 unsigned int machine
;
6953 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
6954 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
6955 int header_done
= 0;
6957 if (writebuf
!= NULL
)
6959 if (readbuf
== NULL
)
6962 pid
= lwpid_of (current_thread
);
6963 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
6964 is_elf64
= elf_64_file_p (filename
, &machine
);
6965 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
6966 ptr_size
= is_elf64
? 8 : 4;
6968 while (annex
[0] != '\0')
6974 sep
= strchr (annex
, '=');
6978 name_len
= sep
- annex
;
6979 if (name_len
== 5 && startswith (annex
, "start"))
6981 else if (name_len
== 4 && startswith (annex
, "prev"))
6985 annex
= strchr (sep
, ';');
6992 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
6999 if (priv
->r_debug
== 0)
7000 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
7002 /* We failed to find DT_DEBUG. Such situation will not change
7003 for this inferior - do not retry it. Report it to GDB as
7004 E01, see for the reasons at the GDB solib-svr4.c side. */
7005 if (priv
->r_debug
== (CORE_ADDR
) -1)
7008 if (priv
->r_debug
!= 0)
7010 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
7011 (unsigned char *) &r_version
,
7012 sizeof (r_version
)) != 0
7015 warning ("unexpected r_debug version %d", r_version
);
7017 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
7018 &lm_addr
, ptr_size
) != 0)
7020 warning ("unable to read r_map from 0x%lx",
7021 (long) priv
->r_debug
+ lmo
->r_map_offset
);
7026 std::string document
= "<library-list-svr4 version=\"1.0\"";
7029 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
7030 &l_name
, ptr_size
) == 0
7031 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
7032 &l_addr
, ptr_size
) == 0
7033 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
7034 &l_ld
, ptr_size
) == 0
7035 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
7036 &l_prev
, ptr_size
) == 0
7037 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
7038 &l_next
, ptr_size
) == 0)
7040 unsigned char libname
[PATH_MAX
];
7042 if (lm_prev
!= l_prev
)
7044 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
7045 (long) lm_prev
, (long) l_prev
);
7049 /* Ignore the first entry even if it has valid name as the first entry
7050 corresponds to the main executable. The first entry should not be
7051 skipped if the dynamic loader was loaded late by a static executable
7052 (see solib-svr4.c parameter ignore_first). But in such case the main
7053 executable does not have PT_DYNAMIC present and this function already
7054 exited above due to failed get_r_debug. */
7056 string_appendf (document
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
7059 /* Not checking for error because reading may stop before
7060 we've got PATH_MAX worth of characters. */
7062 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
7063 libname
[sizeof (libname
) - 1] = '\0';
7064 if (libname
[0] != '\0')
7068 /* Terminate `<library-list-svr4'. */
7073 string_appendf (document
, "<library name=\"");
7074 xml_escape_text_append (&document
, (char *) libname
);
7075 string_appendf (document
, "\" lm=\"0x%lx\" "
7076 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
7077 (unsigned long) lm_addr
, (unsigned long) l_addr
,
7078 (unsigned long) l_ld
);
7088 /* Empty list; terminate `<library-list-svr4'. */
7092 document
+= "</library-list-svr4>";
7094 int document_len
= document
.length ();
7095 if (offset
< document_len
)
7096 document_len
-= offset
;
7099 if (len
> document_len
)
7102 memcpy (readbuf
, document
.data () + offset
, len
);
7107 #ifdef HAVE_LINUX_BTRACE
7109 btrace_target_info
*
7110 linux_process_target::enable_btrace (ptid_t ptid
,
7111 const btrace_config
*conf
)
7113 return linux_enable_btrace (ptid
, conf
);
7116 /* See to_disable_btrace target method. */
7119 linux_process_target::disable_btrace (btrace_target_info
*tinfo
)
7121 enum btrace_error err
;
7123 err
= linux_disable_btrace (tinfo
);
7124 return (err
== BTRACE_ERR_NONE
? 0 : -1);
7127 /* Encode an Intel Processor Trace configuration. */
7130 linux_low_encode_pt_config (struct buffer
*buffer
,
7131 const struct btrace_data_pt_config
*config
)
7133 buffer_grow_str (buffer
, "<pt-config>\n");
7135 switch (config
->cpu
.vendor
)
7138 buffer_xml_printf (buffer
, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
7139 "model=\"%u\" stepping=\"%u\"/>\n",
7140 config
->cpu
.family
, config
->cpu
.model
,
7141 config
->cpu
.stepping
);
7148 buffer_grow_str (buffer
, "</pt-config>\n");
7151 /* Encode a raw buffer. */
7154 linux_low_encode_raw (struct buffer
*buffer
, const gdb_byte
*data
,
7160 /* We use hex encoding - see gdbsupport/rsp-low.h. */
7161 buffer_grow_str (buffer
, "<raw>\n");
7167 elem
[0] = tohex ((*data
>> 4) & 0xf);
7168 elem
[1] = tohex (*data
++ & 0xf);
7170 buffer_grow (buffer
, elem
, 2);
7173 buffer_grow_str (buffer
, "</raw>\n");
7176 /* See to_read_btrace target method. */
7179 linux_process_target::read_btrace (btrace_target_info
*tinfo
,
7181 enum btrace_read_type type
)
7183 struct btrace_data btrace
;
7184 enum btrace_error err
;
7186 err
= linux_read_btrace (&btrace
, tinfo
, type
);
7187 if (err
!= BTRACE_ERR_NONE
)
7189 if (err
== BTRACE_ERR_OVERFLOW
)
7190 buffer_grow_str0 (buffer
, "E.Overflow.");
7192 buffer_grow_str0 (buffer
, "E.Generic Error.");
7197 switch (btrace
.format
)
7199 case BTRACE_FORMAT_NONE
:
7200 buffer_grow_str0 (buffer
, "E.No Trace.");
7203 case BTRACE_FORMAT_BTS
:
7204 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7205 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7207 for (const btrace_block
&block
: *btrace
.variant
.bts
.blocks
)
7208 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
7209 paddress (block
.begin
), paddress (block
.end
));
7211 buffer_grow_str0 (buffer
, "</btrace>\n");
7214 case BTRACE_FORMAT_PT
:
7215 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7216 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7217 buffer_grow_str (buffer
, "<pt>\n");
7219 linux_low_encode_pt_config (buffer
, &btrace
.variant
.pt
.config
);
7221 linux_low_encode_raw (buffer
, btrace
.variant
.pt
.data
,
7222 btrace
.variant
.pt
.size
);
7224 buffer_grow_str (buffer
, "</pt>\n");
7225 buffer_grow_str0 (buffer
, "</btrace>\n");
7229 buffer_grow_str0 (buffer
, "E.Unsupported Trace Format.");
7236 /* See to_btrace_conf target method. */
7239 linux_process_target::read_btrace_conf (const btrace_target_info
*tinfo
,
7242 const struct btrace_config
*conf
;
7244 buffer_grow_str (buffer
, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
7245 buffer_grow_str (buffer
, "<btrace-conf version=\"1.0\">\n");
7247 conf
= linux_btrace_conf (tinfo
);
7250 switch (conf
->format
)
7252 case BTRACE_FORMAT_NONE
:
7255 case BTRACE_FORMAT_BTS
:
7256 buffer_xml_printf (buffer
, "<bts");
7257 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->bts
.size
);
7258 buffer_xml_printf (buffer
, " />\n");
7261 case BTRACE_FORMAT_PT
:
7262 buffer_xml_printf (buffer
, "<pt");
7263 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->pt
.size
);
7264 buffer_xml_printf (buffer
, "/>\n");
7269 buffer_grow_str0 (buffer
, "</btrace-conf>\n");
7272 #endif /* HAVE_LINUX_BTRACE */
7274 /* See nat/linux-nat.h. */
7277 current_lwp_ptid (void)
7279 return ptid_of (current_thread
);
7282 /* Implementation of the target_ops method "breakpoint_kind_from_pc". */
7285 linux_process_target::breakpoint_kind_from_pc (CORE_ADDR
*pcptr
)
7287 if (the_low_target
.breakpoint_kind_from_pc
!= NULL
)
7288 return (*the_low_target
.breakpoint_kind_from_pc
) (pcptr
);
7290 return process_stratum_target::breakpoint_kind_from_pc (pcptr
);
7293 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
7296 linux_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
7298 gdb_assert (the_low_target
.sw_breakpoint_from_kind
!= NULL
);
7300 return (*the_low_target
.sw_breakpoint_from_kind
) (kind
, size
);
7303 /* Implementation of the target_ops method
7304 "breakpoint_kind_from_current_state". */
7307 linux_process_target::breakpoint_kind_from_current_state (CORE_ADDR
*pcptr
)
7309 if (the_low_target
.breakpoint_kind_from_current_state
!= NULL
)
7310 return (*the_low_target
.breakpoint_kind_from_current_state
) (pcptr
);
7312 return breakpoint_kind_from_pc (pcptr
);
7316 linux_process_target::thread_name (ptid_t thread
)
7318 return linux_proc_tid_get_name (thread
);
7323 linux_process_target::thread_handle (ptid_t ptid
, gdb_byte
**handle
,
7326 return thread_db_thread_handle (ptid
, handle
, handle_len
);
7330 /* Default implementation of linux_target_ops method "set_pc" for
7331 32-bit pc register which is literally named "pc". */
7334 linux_set_pc_32bit (struct regcache
*regcache
, CORE_ADDR pc
)
7336 uint32_t newpc
= pc
;
7338 supply_register_by_name (regcache
, "pc", &newpc
);
7341 /* Default implementation of linux_target_ops method "get_pc" for
7342 32-bit pc register which is literally named "pc". */
7345 linux_get_pc_32bit (struct regcache
*regcache
)
7349 collect_register_by_name (regcache
, "pc", &pc
);
7351 debug_printf ("stop pc is 0x%" PRIx32
"\n", pc
);
7355 /* Default implementation of linux_target_ops method "set_pc" for
7356 64-bit pc register which is literally named "pc". */
7359 linux_set_pc_64bit (struct regcache
*regcache
, CORE_ADDR pc
)
7361 uint64_t newpc
= pc
;
7363 supply_register_by_name (regcache
, "pc", &newpc
);
7366 /* Default implementation of linux_target_ops method "get_pc" for
7367 64-bit pc register which is literally named "pc". */
7370 linux_get_pc_64bit (struct regcache
*regcache
)
7374 collect_register_by_name (regcache
, "pc", &pc
);
7376 debug_printf ("stop pc is 0x%" PRIx64
"\n", pc
);
7380 /* See linux-low.h. */
7383 linux_get_auxv (int wordsize
, CORE_ADDR match
, CORE_ADDR
*valp
)
7385 gdb_byte
*data
= (gdb_byte
*) alloca (2 * wordsize
);
7388 gdb_assert (wordsize
== 4 || wordsize
== 8);
7390 while (the_target
->read_auxv (offset
, data
, 2 * wordsize
) == 2 * wordsize
)
7394 uint32_t *data_p
= (uint32_t *) data
;
7395 if (data_p
[0] == match
)
7403 uint64_t *data_p
= (uint64_t *) data
;
7404 if (data_p
[0] == match
)
7411 offset
+= 2 * wordsize
;
7417 /* See linux-low.h. */
7420 linux_get_hwcap (int wordsize
)
7422 CORE_ADDR hwcap
= 0;
7423 linux_get_auxv (wordsize
, AT_HWCAP
, &hwcap
);
7427 /* See linux-low.h. */
7430 linux_get_hwcap2 (int wordsize
)
7432 CORE_ADDR hwcap2
= 0;
7433 linux_get_auxv (wordsize
, AT_HWCAP2
, &hwcap2
);
7437 #ifdef HAVE_LINUX_REGSETS
7439 initialize_regsets_info (struct regsets_info
*info
)
7441 for (info
->num_regsets
= 0;
7442 info
->regsets
[info
->num_regsets
].size
>= 0;
7443 info
->num_regsets
++)
7449 initialize_low (void)
7451 struct sigaction sigchld_action
;
7453 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
7454 set_target_ops (the_linux_target
);
7456 linux_ptrace_init_warnings ();
7457 linux_proc_init_warnings ();
7459 sigchld_action
.sa_handler
= sigchld_handler
;
7460 sigemptyset (&sigchld_action
.sa_mask
);
7461 sigchld_action
.sa_flags
= SA_RESTART
;
7462 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
7464 initialize_low_arch ();
7466 linux_check_ptrace_features ();