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 unsuspend_all_lwps (struct lwp_info
*except
);
271 static struct lwp_info
*add_lwp (ptid_t ptid
);
272 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
273 static int lwp_is_marked_dead (struct lwp_info
*lwp
);
274 static int finish_step_over (struct lwp_info
*lwp
);
275 static int kill_lwp (unsigned long lwpid
, int signo
);
276 static void enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
);
277 static int linux_low_ptrace_options (int attached
);
278 static int check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
);
280 /* When the event-loop is doing a step-over, this points at the thread
282 ptid_t step_over_bkpt
;
284 /* True if the low target can hardware single-step. */
287 can_hardware_single_step (void)
289 if (the_low_target
.supports_hardware_single_step
!= NULL
)
290 return the_low_target
.supports_hardware_single_step ();
295 /* True if the low target can software single-step. Such targets
296 implement the GET_NEXT_PCS callback. */
299 can_software_single_step (void)
301 return (the_low_target
.get_next_pcs
!= NULL
);
305 linux_process_target::low_supports_breakpoints ()
311 linux_process_target::low_get_pc (regcache
*regcache
)
317 linux_process_target::low_set_pc (regcache
*regcache
, CORE_ADDR newpc
)
319 gdb_assert_not_reached ("linux target op low_set_pc is not implemented");
322 /* Returns true if this target can support fast tracepoints. This
323 does not mean that the in-process agent has been loaded in the
327 supports_fast_tracepoints (void)
329 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
332 /* True if LWP is stopped in its stepping range. */
335 lwp_in_step_range (struct lwp_info
*lwp
)
337 CORE_ADDR pc
= lwp
->stop_pc
;
339 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
342 struct pending_signals
346 struct pending_signals
*prev
;
349 /* The read/write ends of the pipe registered as waitable file in the
351 static int linux_event_pipe
[2] = { -1, -1 };
353 /* True if we're currently in async mode. */
354 #define target_is_async_p() (linux_event_pipe[0] != -1)
356 static void send_sigstop (struct lwp_info
*lwp
);
358 /* Return non-zero if HEADER is a 64-bit ELF file. */
361 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
363 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
364 && header
->e_ident
[EI_MAG1
] == ELFMAG1
365 && header
->e_ident
[EI_MAG2
] == ELFMAG2
366 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
368 *machine
= header
->e_machine
;
369 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
376 /* Return non-zero if FILE is a 64-bit ELF file,
377 zero if the file is not a 64-bit ELF file,
378 and -1 if the file is not accessible or doesn't exist. */
381 elf_64_file_p (const char *file
, unsigned int *machine
)
386 fd
= open (file
, O_RDONLY
);
390 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
397 return elf_64_header_p (&header
, machine
);
400 /* Accepts an integer PID; Returns true if the executable PID is
401 running is a 64-bit ELF file.. */
404 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
408 sprintf (file
, "/proc/%d/exe", pid
);
409 return elf_64_file_p (file
, machine
);
413 delete_lwp (struct lwp_info
*lwp
)
415 struct thread_info
*thr
= get_lwp_thread (lwp
);
418 debug_printf ("deleting %ld\n", lwpid_of (thr
));
422 if (the_low_target
.delete_thread
!= NULL
)
423 the_low_target
.delete_thread (lwp
->arch_private
);
425 gdb_assert (lwp
->arch_private
== NULL
);
430 /* Add a process to the common process list, and set its private
433 static struct process_info
*
434 linux_add_process (int pid
, int attached
)
436 struct process_info
*proc
;
438 proc
= add_process (pid
, attached
);
439 proc
->priv
= XCNEW (struct process_info_private
);
441 if (the_low_target
.new_process
!= NULL
)
442 proc
->priv
->arch_private
= the_low_target
.new_process ();
448 linux_process_target::arch_setup_thread (thread_info
*thread
)
450 struct thread_info
*saved_thread
;
452 saved_thread
= current_thread
;
453 current_thread
= thread
;
457 current_thread
= saved_thread
;
461 linux_process_target::handle_extended_wait (lwp_info
**orig_event_lwp
,
464 client_state
&cs
= get_client_state ();
465 struct lwp_info
*event_lwp
= *orig_event_lwp
;
466 int event
= linux_ptrace_get_extended_event (wstat
);
467 struct thread_info
*event_thr
= get_lwp_thread (event_lwp
);
468 struct lwp_info
*new_lwp
;
470 gdb_assert (event_lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
472 /* All extended events we currently use are mid-syscall. Only
473 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
474 you have to be using PTRACE_SEIZE to get that. */
475 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
477 if ((event
== PTRACE_EVENT_FORK
) || (event
== PTRACE_EVENT_VFORK
)
478 || (event
== PTRACE_EVENT_CLONE
))
481 unsigned long new_pid
;
484 /* Get the pid of the new lwp. */
485 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_thr
), (PTRACE_TYPE_ARG3
) 0,
488 /* If we haven't already seen the new PID stop, wait for it now. */
489 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
491 /* The new child has a pending SIGSTOP. We can't affect it until it
492 hits the SIGSTOP, but we're already attached. */
494 ret
= my_waitpid (new_pid
, &status
, __WALL
);
497 perror_with_name ("waiting for new child");
498 else if (ret
!= new_pid
)
499 warning ("wait returned unexpected PID %d", ret
);
500 else if (!WIFSTOPPED (status
))
501 warning ("wait returned unexpected status 0x%x", status
);
504 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
506 struct process_info
*parent_proc
;
507 struct process_info
*child_proc
;
508 struct lwp_info
*child_lwp
;
509 struct thread_info
*child_thr
;
510 struct target_desc
*tdesc
;
512 ptid
= ptid_t (new_pid
, new_pid
, 0);
516 debug_printf ("HEW: Got fork event from LWP %ld, "
518 ptid_of (event_thr
).lwp (),
522 /* Add the new process to the tables and clone the breakpoint
523 lists of the parent. We need to do this even if the new process
524 will be detached, since we will need the process object and the
525 breakpoints to remove any breakpoints from memory when we
526 detach, and the client side will access registers. */
527 child_proc
= linux_add_process (new_pid
, 0);
528 gdb_assert (child_proc
!= NULL
);
529 child_lwp
= add_lwp (ptid
);
530 gdb_assert (child_lwp
!= NULL
);
531 child_lwp
->stopped
= 1;
532 child_lwp
->must_set_ptrace_flags
= 1;
533 child_lwp
->status_pending_p
= 0;
534 child_thr
= get_lwp_thread (child_lwp
);
535 child_thr
->last_resume_kind
= resume_stop
;
536 child_thr
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
538 /* If we're suspending all threads, leave this one suspended
539 too. If the fork/clone parent is stepping over a breakpoint,
540 all other threads have been suspended already. Leave the
541 child suspended too. */
542 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
543 || event_lwp
->bp_reinsert
!= 0)
546 debug_printf ("HEW: leaving child suspended\n");
547 child_lwp
->suspended
= 1;
550 parent_proc
= get_thread_process (event_thr
);
551 child_proc
->attached
= parent_proc
->attached
;
553 if (event_lwp
->bp_reinsert
!= 0
554 && can_software_single_step ()
555 && event
== PTRACE_EVENT_VFORK
)
557 /* If we leave single-step breakpoints there, child will
558 hit it, so uninsert single-step breakpoints from parent
559 (and child). Once vfork child is done, reinsert
560 them back to parent. */
561 uninsert_single_step_breakpoints (event_thr
);
564 clone_all_breakpoints (child_thr
, event_thr
);
566 tdesc
= allocate_target_description ();
567 copy_target_description (tdesc
, parent_proc
->tdesc
);
568 child_proc
->tdesc
= tdesc
;
570 /* Clone arch-specific process data. */
571 if (the_low_target
.new_fork
!= NULL
)
572 the_low_target
.new_fork (parent_proc
, child_proc
);
574 /* Save fork info in the parent thread. */
575 if (event
== PTRACE_EVENT_FORK
)
576 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_FORKED
;
577 else if (event
== PTRACE_EVENT_VFORK
)
578 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORKED
;
580 event_lwp
->waitstatus
.value
.related_pid
= ptid
;
582 /* The status_pending field contains bits denoting the
583 extended event, so when the pending event is handled,
584 the handler will look at lwp->waitstatus. */
585 event_lwp
->status_pending_p
= 1;
586 event_lwp
->status_pending
= wstat
;
588 /* Link the threads until the parent event is passed on to
590 event_lwp
->fork_relative
= child_lwp
;
591 child_lwp
->fork_relative
= event_lwp
;
593 /* If the parent thread is doing step-over with single-step
594 breakpoints, the list of single-step breakpoints are cloned
595 from the parent's. Remove them from the child process.
596 In case of vfork, we'll reinsert them back once vforked
598 if (event_lwp
->bp_reinsert
!= 0
599 && can_software_single_step ())
601 /* The child process is forked and stopped, so it is safe
602 to access its memory without stopping all other threads
603 from other processes. */
604 delete_single_step_breakpoints (child_thr
);
606 gdb_assert (has_single_step_breakpoints (event_thr
));
607 gdb_assert (!has_single_step_breakpoints (child_thr
));
610 /* Report the event. */
615 debug_printf ("HEW: Got clone event "
616 "from LWP %ld, new child is LWP %ld\n",
617 lwpid_of (event_thr
), new_pid
);
619 ptid
= ptid_t (pid_of (event_thr
), new_pid
, 0);
620 new_lwp
= add_lwp (ptid
);
622 /* Either we're going to immediately resume the new thread
623 or leave it stopped. resume_one_lwp is a nop if it
624 thinks the thread is currently running, so set this first
625 before calling resume_one_lwp. */
626 new_lwp
->stopped
= 1;
628 /* If we're suspending all threads, leave this one suspended
629 too. If the fork/clone parent is stepping over a breakpoint,
630 all other threads have been suspended already. Leave the
631 child suspended too. */
632 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
633 || event_lwp
->bp_reinsert
!= 0)
634 new_lwp
->suspended
= 1;
636 /* Normally we will get the pending SIGSTOP. But in some cases
637 we might get another signal delivered to the group first.
638 If we do get another signal, be sure not to lose it. */
639 if (WSTOPSIG (status
) != SIGSTOP
)
641 new_lwp
->stop_expected
= 1;
642 new_lwp
->status_pending_p
= 1;
643 new_lwp
->status_pending
= status
;
645 else if (cs
.report_thread_events
)
647 new_lwp
->waitstatus
.kind
= TARGET_WAITKIND_THREAD_CREATED
;
648 new_lwp
->status_pending_p
= 1;
649 new_lwp
->status_pending
= status
;
653 thread_db_notice_clone (event_thr
, ptid
);
656 /* Don't report the event. */
659 else if (event
== PTRACE_EVENT_VFORK_DONE
)
661 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
663 if (event_lwp
->bp_reinsert
!= 0 && can_software_single_step ())
665 reinsert_single_step_breakpoints (event_thr
);
667 gdb_assert (has_single_step_breakpoints (event_thr
));
670 /* Report the event. */
673 else if (event
== PTRACE_EVENT_EXEC
&& cs
.report_exec_events
)
675 struct process_info
*proc
;
676 std::vector
<int> syscalls_to_catch
;
682 debug_printf ("HEW: Got exec event from LWP %ld\n",
683 lwpid_of (event_thr
));
686 /* Get the event ptid. */
687 event_ptid
= ptid_of (event_thr
);
688 event_pid
= event_ptid
.pid ();
690 /* Save the syscall list from the execing process. */
691 proc
= get_thread_process (event_thr
);
692 syscalls_to_catch
= std::move (proc
->syscalls_to_catch
);
694 /* Delete the execing process and all its threads. */
696 current_thread
= NULL
;
698 /* Create a new process/lwp/thread. */
699 proc
= linux_add_process (event_pid
, 0);
700 event_lwp
= add_lwp (event_ptid
);
701 event_thr
= get_lwp_thread (event_lwp
);
702 gdb_assert (current_thread
== event_thr
);
703 arch_setup_thread (event_thr
);
705 /* Set the event status. */
706 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXECD
;
707 event_lwp
->waitstatus
.value
.execd_pathname
708 = xstrdup (linux_proc_pid_to_exec_file (lwpid_of (event_thr
)));
710 /* Mark the exec status as pending. */
711 event_lwp
->stopped
= 1;
712 event_lwp
->status_pending_p
= 1;
713 event_lwp
->status_pending
= wstat
;
714 event_thr
->last_resume_kind
= resume_continue
;
715 event_thr
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
717 /* Update syscall state in the new lwp, effectively mid-syscall too. */
718 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
720 /* Restore the list to catch. Don't rely on the client, which is free
721 to avoid sending a new list when the architecture doesn't change.
722 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
723 proc
->syscalls_to_catch
= std::move (syscalls_to_catch
);
725 /* Report the event. */
726 *orig_event_lwp
= event_lwp
;
730 internal_error (__FILE__
, __LINE__
, _("unknown ptrace event %d"), event
);
734 linux_process_target::get_pc (lwp_info
*lwp
)
736 struct thread_info
*saved_thread
;
737 struct regcache
*regcache
;
740 if (!low_supports_breakpoints ())
743 saved_thread
= current_thread
;
744 current_thread
= get_lwp_thread (lwp
);
746 regcache
= get_thread_regcache (current_thread
, 1);
747 pc
= low_get_pc (regcache
);
750 debug_printf ("pc is 0x%lx\n", (long) pc
);
752 current_thread
= saved_thread
;
756 /* This function should only be called if LWP got a SYSCALL_SIGTRAP.
757 Fill *SYSNO with the syscall nr trapped. */
760 get_syscall_trapinfo (struct lwp_info
*lwp
, int *sysno
)
762 struct thread_info
*saved_thread
;
763 struct regcache
*regcache
;
765 if (the_low_target
.get_syscall_trapinfo
== NULL
)
767 /* If we cannot get the syscall trapinfo, report an unknown
768 system call number. */
769 *sysno
= UNKNOWN_SYSCALL
;
773 saved_thread
= current_thread
;
774 current_thread
= get_lwp_thread (lwp
);
776 regcache
= get_thread_regcache (current_thread
, 1);
777 (*the_low_target
.get_syscall_trapinfo
) (regcache
, sysno
);
780 debug_printf ("get_syscall_trapinfo sysno %d\n", *sysno
);
782 current_thread
= saved_thread
;
785 static int check_stopped_by_watchpoint (struct lwp_info
*child
);
788 linux_process_target::save_stop_reason (lwp_info
*lwp
)
791 CORE_ADDR sw_breakpoint_pc
;
792 struct thread_info
*saved_thread
;
793 #if USE_SIGTRAP_SIGINFO
797 if (!low_supports_breakpoints ())
801 sw_breakpoint_pc
= pc
- the_low_target
.decr_pc_after_break
;
803 /* breakpoint_at reads from the current thread. */
804 saved_thread
= current_thread
;
805 current_thread
= get_lwp_thread (lwp
);
807 #if USE_SIGTRAP_SIGINFO
808 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
809 (PTRACE_TYPE_ARG3
) 0, &siginfo
) == 0)
811 if (siginfo
.si_signo
== SIGTRAP
)
813 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
)
814 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
816 /* The si_code is ambiguous on this arch -- check debug
818 if (!check_stopped_by_watchpoint (lwp
))
819 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
821 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
))
823 /* If we determine the LWP stopped for a SW breakpoint,
824 trust it. Particularly don't check watchpoint
825 registers, because at least on s390, we'd find
826 stopped-by-watchpoint as long as there's a watchpoint
828 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
830 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
832 /* This can indicate either a hardware breakpoint or
833 hardware watchpoint. Check debug registers. */
834 if (!check_stopped_by_watchpoint (lwp
))
835 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
837 else if (siginfo
.si_code
== TRAP_TRACE
)
839 /* We may have single stepped an instruction that
840 triggered a watchpoint. In that case, on some
841 architectures (such as x86), instead of TRAP_HWBKPT,
842 si_code indicates TRAP_TRACE, and we need to check
843 the debug registers separately. */
844 if (!check_stopped_by_watchpoint (lwp
))
845 lwp
->stop_reason
= TARGET_STOPPED_BY_SINGLE_STEP
;
850 /* We may have just stepped a breakpoint instruction. E.g., in
851 non-stop mode, GDB first tells the thread A to step a range, and
852 then the user inserts a breakpoint inside the range. In that
853 case we need to report the breakpoint PC. */
854 if ((!lwp
->stepping
|| lwp
->stop_pc
== sw_breakpoint_pc
)
855 && (*the_low_target
.breakpoint_at
) (sw_breakpoint_pc
))
856 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
858 if (hardware_breakpoint_inserted_here (pc
))
859 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
861 if (lwp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
)
862 check_stopped_by_watchpoint (lwp
);
865 if (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
)
869 struct thread_info
*thr
= get_lwp_thread (lwp
);
871 debug_printf ("CSBB: %s stopped by software breakpoint\n",
872 target_pid_to_str (ptid_of (thr
)));
875 /* Back up the PC if necessary. */
876 if (pc
!= sw_breakpoint_pc
)
878 struct regcache
*regcache
879 = get_thread_regcache (current_thread
, 1);
880 low_set_pc (regcache
, sw_breakpoint_pc
);
883 /* Update this so we record the correct stop PC below. */
884 pc
= sw_breakpoint_pc
;
886 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
890 struct thread_info
*thr
= get_lwp_thread (lwp
);
892 debug_printf ("CSBB: %s stopped by hardware breakpoint\n",
893 target_pid_to_str (ptid_of (thr
)));
896 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
900 struct thread_info
*thr
= get_lwp_thread (lwp
);
902 debug_printf ("CSBB: %s stopped by hardware watchpoint\n",
903 target_pid_to_str (ptid_of (thr
)));
906 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
)
910 struct thread_info
*thr
= get_lwp_thread (lwp
);
912 debug_printf ("CSBB: %s stopped by trace\n",
913 target_pid_to_str (ptid_of (thr
)));
918 current_thread
= saved_thread
;
922 static struct lwp_info
*
923 add_lwp (ptid_t ptid
)
925 struct lwp_info
*lwp
;
927 lwp
= XCNEW (struct lwp_info
);
929 lwp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
931 lwp
->thread
= add_thread (ptid
, lwp
);
933 if (the_low_target
.new_thread
!= NULL
)
934 the_low_target
.new_thread (lwp
);
939 /* Callback to be used when calling fork_inferior, responsible for
940 actually initiating the tracing of the inferior. */
945 if (ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0,
946 (PTRACE_TYPE_ARG4
) 0) < 0)
947 trace_start_error_with_name ("ptrace");
949 if (setpgid (0, 0) < 0)
950 trace_start_error_with_name ("setpgid");
952 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
953 stdout to stderr so that inferior i/o doesn't corrupt the connection.
954 Also, redirect stdin to /dev/null. */
955 if (remote_connection_is_stdio ())
958 trace_start_error_with_name ("close");
959 if (open ("/dev/null", O_RDONLY
) < 0)
960 trace_start_error_with_name ("open");
962 trace_start_error_with_name ("dup2");
963 if (write (2, "stdin/stdout redirected\n",
964 sizeof ("stdin/stdout redirected\n") - 1) < 0)
966 /* Errors ignored. */;
971 /* Start an inferior process and returns its pid.
972 PROGRAM is the name of the program to be started, and PROGRAM_ARGS
973 are its arguments. */
976 linux_process_target::create_inferior (const char *program
,
977 const std::vector
<char *> &program_args
)
979 client_state
&cs
= get_client_state ();
980 struct lwp_info
*new_lwp
;
985 maybe_disable_address_space_randomization restore_personality
986 (cs
.disable_randomization
);
987 std::string str_program_args
= stringify_argv (program_args
);
989 pid
= fork_inferior (program
,
990 str_program_args
.c_str (),
991 get_environ ()->envp (), linux_ptrace_fun
,
992 NULL
, NULL
, NULL
, NULL
);
995 linux_add_process (pid
, 0);
997 ptid
= ptid_t (pid
, pid
, 0);
998 new_lwp
= add_lwp (ptid
);
999 new_lwp
->must_set_ptrace_flags
= 1;
1001 post_fork_inferior (pid
, program
);
1006 /* Implement the post_create_inferior target_ops method. */
1009 linux_process_target::post_create_inferior ()
1011 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
1015 if (lwp
->must_set_ptrace_flags
)
1017 struct process_info
*proc
= current_process ();
1018 int options
= linux_low_ptrace_options (proc
->attached
);
1020 linux_enable_event_reporting (lwpid_of (current_thread
), options
);
1021 lwp
->must_set_ptrace_flags
= 0;
1025 /* Attach to an inferior process. Returns 0 on success, ERRNO on
1029 linux_attach_lwp (ptid_t ptid
)
1031 struct lwp_info
*new_lwp
;
1032 int lwpid
= ptid
.lwp ();
1034 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0)
1038 new_lwp
= add_lwp (ptid
);
1040 /* We need to wait for SIGSTOP before being able to make the next
1041 ptrace call on this LWP. */
1042 new_lwp
->must_set_ptrace_flags
= 1;
1044 if (linux_proc_pid_is_stopped (lwpid
))
1047 debug_printf ("Attached to a stopped process\n");
1049 /* The process is definitely stopped. It is in a job control
1050 stop, unless the kernel predates the TASK_STOPPED /
1051 TASK_TRACED distinction, in which case it might be in a
1052 ptrace stop. Make sure it is in a ptrace stop; from there we
1053 can kill it, signal it, et cetera.
1055 First make sure there is a pending SIGSTOP. Since we are
1056 already attached, the process can not transition from stopped
1057 to running without a PTRACE_CONT; so we know this signal will
1058 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1059 probably already in the queue (unless this kernel is old
1060 enough to use TASK_STOPPED for ptrace stops); but since
1061 SIGSTOP is not an RT signal, it can only be queued once. */
1062 kill_lwp (lwpid
, SIGSTOP
);
1064 /* Finally, resume the stopped process. This will deliver the
1065 SIGSTOP (or a higher priority signal, just like normal
1066 PTRACE_ATTACH), which we'll catch later on. */
1067 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1070 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1071 brings it to a halt.
1073 There are several cases to consider here:
1075 1) gdbserver has already attached to the process and is being notified
1076 of a new thread that is being created.
1077 In this case we should ignore that SIGSTOP and resume the
1078 process. This is handled below by setting stop_expected = 1,
1079 and the fact that add_thread sets last_resume_kind ==
1082 2) This is the first thread (the process thread), and we're attaching
1083 to it via attach_inferior.
1084 In this case we want the process thread to stop.
1085 This is handled by having linux_attach set last_resume_kind ==
1086 resume_stop after we return.
1088 If the pid we are attaching to is also the tgid, we attach to and
1089 stop all the existing threads. Otherwise, we attach to pid and
1090 ignore any other threads in the same group as this pid.
1092 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1094 In this case we want the thread to stop.
1095 FIXME: This case is currently not properly handled.
1096 We should wait for the SIGSTOP but don't. Things work apparently
1097 because enough time passes between when we ptrace (ATTACH) and when
1098 gdb makes the next ptrace call on the thread.
1100 On the other hand, if we are currently trying to stop all threads, we
1101 should treat the new thread as if we had sent it a SIGSTOP. This works
1102 because we are guaranteed that the add_lwp call above added us to the
1103 end of the list, and so the new thread has not yet reached
1104 wait_for_sigstop (but will). */
1105 new_lwp
->stop_expected
= 1;
1110 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1111 already attached. Returns true if a new LWP is found, false
1115 attach_proc_task_lwp_callback (ptid_t ptid
)
1117 /* Is this a new thread? */
1118 if (find_thread_ptid (ptid
) == NULL
)
1120 int lwpid
= ptid
.lwp ();
1124 debug_printf ("Found new lwp %d\n", lwpid
);
1126 err
= linux_attach_lwp (ptid
);
1128 /* Be quiet if we simply raced with the thread exiting. EPERM
1129 is returned if the thread's task still exists, and is marked
1130 as exited or zombie, as well as other conditions, so in that
1131 case, confirm the status in /proc/PID/status. */
1133 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1137 debug_printf ("Cannot attach to lwp %d: "
1138 "thread is gone (%d: %s)\n",
1139 lwpid
, err
, safe_strerror (err
));
1145 = linux_ptrace_attach_fail_reason_string (ptid
, err
);
1147 warning (_("Cannot attach to lwp %d: %s"), lwpid
, reason
.c_str ());
1155 static void async_file_mark (void);
1157 /* Attach to PID. If PID is the tgid, attach to it and all
1161 linux_process_target::attach (unsigned long pid
)
1163 struct process_info
*proc
;
1164 struct thread_info
*initial_thread
;
1165 ptid_t ptid
= ptid_t (pid
, pid
, 0);
1168 proc
= linux_add_process (pid
, 1);
1170 /* Attach to PID. We will check for other threads
1172 err
= linux_attach_lwp (ptid
);
1175 remove_process (proc
);
1177 std::string reason
= linux_ptrace_attach_fail_reason_string (ptid
, err
);
1178 error ("Cannot attach to process %ld: %s", pid
, reason
.c_str ());
1181 /* Don't ignore the initial SIGSTOP if we just attached to this
1182 process. It will be collected by wait shortly. */
1183 initial_thread
= find_thread_ptid (ptid_t (pid
, pid
, 0));
1184 initial_thread
->last_resume_kind
= resume_stop
;
1186 /* We must attach to every LWP. If /proc is mounted, use that to
1187 find them now. On the one hand, the inferior may be using raw
1188 clone instead of using pthreads. On the other hand, even if it
1189 is using pthreads, GDB may not be connected yet (thread_db needs
1190 to do symbol lookups, through qSymbol). Also, thread_db walks
1191 structures in the inferior's address space to find the list of
1192 threads/LWPs, and those structures may well be corrupted. Note
1193 that once thread_db is loaded, we'll still use it to list threads
1194 and associate pthread info with each LWP. */
1195 linux_proc_attach_tgid_threads (pid
, attach_proc_task_lwp_callback
);
1197 /* GDB will shortly read the xml target description for this
1198 process, to figure out the process' architecture. But the target
1199 description is only filled in when the first process/thread in
1200 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1201 that now, otherwise, if GDB is fast enough, it could read the
1202 target description _before_ that initial stop. */
1205 struct lwp_info
*lwp
;
1207 ptid_t pid_ptid
= ptid_t (pid
);
1209 lwpid
= wait_for_event_filtered (pid_ptid
, pid_ptid
, &wstat
, __WALL
);
1210 gdb_assert (lwpid
> 0);
1212 lwp
= find_lwp_pid (ptid_t (lwpid
));
1214 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGSTOP
)
1216 lwp
->status_pending_p
= 1;
1217 lwp
->status_pending
= wstat
;
1220 initial_thread
->last_resume_kind
= resume_continue
;
1224 gdb_assert (proc
->tdesc
!= NULL
);
1231 last_thread_of_process_p (int pid
)
1233 bool seen_one
= false;
1235 thread_info
*thread
= find_thread (pid
, [&] (thread_info
*thr_arg
)
1239 /* This is the first thread of this process we see. */
1245 /* This is the second thread of this process we see. */
1250 return thread
== NULL
;
1256 linux_kill_one_lwp (struct lwp_info
*lwp
)
1258 struct thread_info
*thr
= get_lwp_thread (lwp
);
1259 int pid
= lwpid_of (thr
);
1261 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1262 there is no signal context, and ptrace(PTRACE_KILL) (or
1263 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1264 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1265 alternative is to kill with SIGKILL. We only need one SIGKILL
1266 per process, not one for each thread. But since we still support
1267 support debugging programs using raw clone without CLONE_THREAD,
1268 we send one for each thread. For years, we used PTRACE_KILL
1269 only, so we're being a bit paranoid about some old kernels where
1270 PTRACE_KILL might work better (dubious if there are any such, but
1271 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1272 second, and so we're fine everywhere. */
1275 kill_lwp (pid
, SIGKILL
);
1278 int save_errno
= errno
;
1280 debug_printf ("LKL: kill_lwp (SIGKILL) %s, 0, 0 (%s)\n",
1281 target_pid_to_str (ptid_of (thr
)),
1282 save_errno
? safe_strerror (save_errno
) : "OK");
1286 ptrace (PTRACE_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1289 int save_errno
= errno
;
1291 debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
1292 target_pid_to_str (ptid_of (thr
)),
1293 save_errno
? safe_strerror (save_errno
) : "OK");
1297 /* Kill LWP and wait for it to die. */
1300 kill_wait_lwp (struct lwp_info
*lwp
)
1302 struct thread_info
*thr
= get_lwp_thread (lwp
);
1303 int pid
= ptid_of (thr
).pid ();
1304 int lwpid
= ptid_of (thr
).lwp ();
1309 debug_printf ("kwl: killing lwp %d, for pid: %d\n", lwpid
, pid
);
1313 linux_kill_one_lwp (lwp
);
1315 /* Make sure it died. Notes:
1317 - The loop is most likely unnecessary.
1319 - We don't use wait_for_event as that could delete lwps
1320 while we're iterating over them. We're not interested in
1321 any pending status at this point, only in making sure all
1322 wait status on the kernel side are collected until the
1325 - We don't use __WALL here as the __WALL emulation relies on
1326 SIGCHLD, and killing a stopped process doesn't generate
1327 one, nor an exit status.
1329 res
= my_waitpid (lwpid
, &wstat
, 0);
1330 if (res
== -1 && errno
== ECHILD
)
1331 res
= my_waitpid (lwpid
, &wstat
, __WCLONE
);
1332 } while (res
> 0 && WIFSTOPPED (wstat
));
1334 /* Even if it was stopped, the child may have already disappeared.
1335 E.g., if it was killed by SIGKILL. */
1336 if (res
< 0 && errno
!= ECHILD
)
1337 perror_with_name ("kill_wait_lwp");
1340 /* Callback for `for_each_thread'. Kills an lwp of a given process,
1341 except the leader. */
1344 kill_one_lwp_callback (thread_info
*thread
, int pid
)
1346 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1348 /* We avoid killing the first thread here, because of a Linux kernel (at
1349 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1350 the children get a chance to be reaped, it will remain a zombie
1353 if (lwpid_of (thread
) == pid
)
1356 debug_printf ("lkop: is last of process %s\n",
1357 target_pid_to_str (thread
->id
));
1361 kill_wait_lwp (lwp
);
1365 linux_process_target::kill (process_info
*process
)
1367 int pid
= process
->pid
;
1369 /* If we're killing a running inferior, make sure it is stopped
1370 first, as PTRACE_KILL will not work otherwise. */
1371 stop_all_lwps (0, NULL
);
1373 for_each_thread (pid
, [&] (thread_info
*thread
)
1375 kill_one_lwp_callback (thread
, pid
);
1378 /* See the comment in linux_kill_one_lwp. We did not kill the first
1379 thread in the list, so do so now. */
1380 lwp_info
*lwp
= find_lwp_pid (ptid_t (pid
));
1385 debug_printf ("lk_1: cannot find lwp for pid: %d\n",
1389 kill_wait_lwp (lwp
);
1393 /* Since we presently can only stop all lwps of all processes, we
1394 need to unstop lwps of other processes. */
1395 unstop_all_lwps (0, NULL
);
1399 /* Get pending signal of THREAD, for detaching purposes. This is the
1400 signal the thread last stopped for, which we need to deliver to the
1401 thread when detaching, otherwise, it'd be suppressed/lost. */
1404 get_detach_signal (struct thread_info
*thread
)
1406 client_state
&cs
= get_client_state ();
1407 enum gdb_signal signo
= GDB_SIGNAL_0
;
1409 struct lwp_info
*lp
= get_thread_lwp (thread
);
1411 if (lp
->status_pending_p
)
1412 status
= lp
->status_pending
;
1415 /* If the thread had been suspended by gdbserver, and it stopped
1416 cleanly, then it'll have stopped with SIGSTOP. But we don't
1417 want to deliver that SIGSTOP. */
1418 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1419 || thread
->last_status
.value
.sig
== GDB_SIGNAL_0
)
1422 /* Otherwise, we may need to deliver the signal we
1424 status
= lp
->last_status
;
1427 if (!WIFSTOPPED (status
))
1430 debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1431 target_pid_to_str (ptid_of (thread
)));
1435 /* Extended wait statuses aren't real SIGTRAPs. */
1436 if (WSTOPSIG (status
) == SIGTRAP
&& linux_is_extended_waitstatus (status
))
1439 debug_printf ("GPS: lwp %s had stopped with extended "
1440 "status: no pending signal\n",
1441 target_pid_to_str (ptid_of (thread
)));
1445 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1447 if (cs
.program_signals_p
&& !cs
.program_signals
[signo
])
1450 debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1451 target_pid_to_str (ptid_of (thread
)),
1452 gdb_signal_to_string (signo
));
1455 else if (!cs
.program_signals_p
1456 /* If we have no way to know which signals GDB does not
1457 want to have passed to the program, assume
1458 SIGTRAP/SIGINT, which is GDB's default. */
1459 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1462 debug_printf ("GPS: lwp %s had signal %s, "
1463 "but we don't know if we should pass it. "
1464 "Default to not.\n",
1465 target_pid_to_str (ptid_of (thread
)),
1466 gdb_signal_to_string (signo
));
1472 debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1473 target_pid_to_str (ptid_of (thread
)),
1474 gdb_signal_to_string (signo
));
1476 return WSTOPSIG (status
);
1480 /* Detach from LWP. */
1483 linux_detach_one_lwp (struct lwp_info
*lwp
)
1485 struct thread_info
*thread
= get_lwp_thread (lwp
);
1489 /* If there is a pending SIGSTOP, get rid of it. */
1490 if (lwp
->stop_expected
)
1493 debug_printf ("Sending SIGCONT to %s\n",
1494 target_pid_to_str (ptid_of (thread
)));
1496 kill_lwp (lwpid_of (thread
), SIGCONT
);
1497 lwp
->stop_expected
= 0;
1500 /* Pass on any pending signal for this thread. */
1501 sig
= get_detach_signal (thread
);
1503 /* Preparing to resume may try to write registers, and fail if the
1504 lwp is zombie. If that happens, ignore the error. We'll handle
1505 it below, when detach fails with ESRCH. */
1508 /* Flush any pending changes to the process's registers. */
1509 regcache_invalidate_thread (thread
);
1511 /* Finally, let it resume. */
1512 if (the_low_target
.prepare_to_resume
!= NULL
)
1513 the_low_target
.prepare_to_resume (lwp
);
1515 catch (const gdb_exception_error
&ex
)
1517 if (!check_ptrace_stopped_lwp_gone (lwp
))
1521 lwpid
= lwpid_of (thread
);
1522 if (ptrace (PTRACE_DETACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0,
1523 (PTRACE_TYPE_ARG4
) (long) sig
) < 0)
1525 int save_errno
= errno
;
1527 /* We know the thread exists, so ESRCH must mean the lwp is
1528 zombie. This can happen if one of the already-detached
1529 threads exits the whole thread group. In that case we're
1530 still attached, and must reap the lwp. */
1531 if (save_errno
== ESRCH
)
1535 ret
= my_waitpid (lwpid
, &status
, __WALL
);
1538 warning (_("Couldn't reap LWP %d while detaching: %s"),
1539 lwpid
, safe_strerror (errno
));
1541 else if (!WIFEXITED (status
) && !WIFSIGNALED (status
))
1543 warning (_("Reaping LWP %d while detaching "
1544 "returned unexpected status 0x%x"),
1550 error (_("Can't detach %s: %s"),
1551 target_pid_to_str (ptid_of (thread
)),
1552 safe_strerror (save_errno
));
1555 else if (debug_threads
)
1557 debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)\n",
1558 target_pid_to_str (ptid_of (thread
)),
1565 /* Callback for for_each_thread. Detaches from non-leader threads of a
1569 linux_detach_lwp_callback (thread_info
*thread
)
1571 /* We don't actually detach from the thread group leader just yet.
1572 If the thread group exits, we must reap the zombie clone lwps
1573 before we're able to reap the leader. */
1574 if (thread
->id
.pid () == thread
->id
.lwp ())
1577 lwp_info
*lwp
= get_thread_lwp (thread
);
1578 linux_detach_one_lwp (lwp
);
1582 linux_process_target::detach (process_info
*process
)
1584 struct lwp_info
*main_lwp
;
1586 /* As there's a step over already in progress, let it finish first,
1587 otherwise nesting a stabilize_threads operation on top gets real
1589 complete_ongoing_step_over ();
1591 /* Stop all threads before detaching. First, ptrace requires that
1592 the thread is stopped to successfully detach. Second, thread_db
1593 may need to uninstall thread event breakpoints from memory, which
1594 only works with a stopped process anyway. */
1595 stop_all_lwps (0, NULL
);
1597 #ifdef USE_THREAD_DB
1598 thread_db_detach (process
);
1601 /* Stabilize threads (move out of jump pads). */
1602 target_stabilize_threads ();
1604 /* Detach from the clone lwps first. If the thread group exits just
1605 while we're detaching, we must reap the clone lwps before we're
1606 able to reap the leader. */
1607 for_each_thread (process
->pid
, linux_detach_lwp_callback
);
1609 main_lwp
= find_lwp_pid (ptid_t (process
->pid
));
1610 linux_detach_one_lwp (main_lwp
);
1614 /* Since we presently can only stop all lwps of all processes, we
1615 need to unstop lwps of other processes. */
1616 unstop_all_lwps (0, NULL
);
1620 /* Remove all LWPs that belong to process PROC from the lwp list. */
1623 linux_process_target::mourn (process_info
*process
)
1625 struct process_info_private
*priv
;
1627 #ifdef USE_THREAD_DB
1628 thread_db_mourn (process
);
1631 for_each_thread (process
->pid
, [] (thread_info
*thread
)
1633 delete_lwp (get_thread_lwp (thread
));
1636 /* Freeing all private data. */
1637 priv
= process
->priv
;
1638 if (the_low_target
.delete_process
!= NULL
)
1639 the_low_target
.delete_process (priv
->arch_private
);
1641 gdb_assert (priv
->arch_private
== NULL
);
1643 process
->priv
= NULL
;
1645 remove_process (process
);
1649 linux_process_target::join (int pid
)
1654 ret
= my_waitpid (pid
, &status
, 0);
1655 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1657 } while (ret
!= -1 || errno
!= ECHILD
);
1660 /* Return true if the given thread is still alive. */
1663 linux_process_target::thread_alive (ptid_t ptid
)
1665 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1667 /* We assume we always know if a thread exits. If a whole process
1668 exited but we still haven't been able to report it to GDB, we'll
1669 hold on to the last lwp of the dead process. */
1671 return !lwp_is_marked_dead (lwp
);
1677 linux_process_target::thread_still_has_status_pending (thread_info
*thread
)
1679 struct lwp_info
*lp
= get_thread_lwp (thread
);
1681 if (!lp
->status_pending_p
)
1684 if (thread
->last_resume_kind
!= resume_stop
1685 && (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1686 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
))
1688 struct thread_info
*saved_thread
;
1692 gdb_assert (lp
->last_status
!= 0);
1696 saved_thread
= current_thread
;
1697 current_thread
= thread
;
1699 if (pc
!= lp
->stop_pc
)
1702 debug_printf ("PC of %ld changed\n",
1707 #if !USE_SIGTRAP_SIGINFO
1708 else if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1709 && !(*the_low_target
.breakpoint_at
) (pc
))
1712 debug_printf ("previous SW breakpoint of %ld gone\n",
1716 else if (lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
1717 && !hardware_breakpoint_inserted_here (pc
))
1720 debug_printf ("previous HW breakpoint of %ld gone\n",
1726 current_thread
= saved_thread
;
1731 debug_printf ("discarding pending breakpoint status\n");
1732 lp
->status_pending_p
= 0;
1740 /* Returns true if LWP is resumed from the client's perspective. */
1743 lwp_resumed (struct lwp_info
*lwp
)
1745 struct thread_info
*thread
= get_lwp_thread (lwp
);
1747 if (thread
->last_resume_kind
!= resume_stop
)
1750 /* Did gdb send us a `vCont;t', but we haven't reported the
1751 corresponding stop to gdb yet? If so, the thread is still
1752 resumed/running from gdb's perspective. */
1753 if (thread
->last_resume_kind
== resume_stop
1754 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
1761 linux_process_target::status_pending_p_callback (thread_info
*thread
,
1764 struct lwp_info
*lp
= get_thread_lwp (thread
);
1766 /* Check if we're only interested in events from a specific process
1767 or a specific LWP. */
1768 if (!thread
->id
.matches (ptid
))
1771 if (!lwp_resumed (lp
))
1774 if (lp
->status_pending_p
1775 && !thread_still_has_status_pending (thread
))
1777 resume_one_lwp (lp
, lp
->stepping
, GDB_SIGNAL_0
, NULL
);
1781 return lp
->status_pending_p
;
1785 find_lwp_pid (ptid_t ptid
)
1787 thread_info
*thread
= find_thread ([&] (thread_info
*thr_arg
)
1789 int lwp
= ptid
.lwp () != 0 ? ptid
.lwp () : ptid
.pid ();
1790 return thr_arg
->id
.lwp () == lwp
;
1796 return get_thread_lwp (thread
);
1799 /* Return the number of known LWPs in the tgid given by PID. */
1806 for_each_thread (pid
, [&] (thread_info
*thread
)
1814 /* See nat/linux-nat.h. */
1817 iterate_over_lwps (ptid_t filter
,
1818 gdb::function_view
<iterate_over_lwps_ftype
> callback
)
1820 thread_info
*thread
= find_thread (filter
, [&] (thread_info
*thr_arg
)
1822 lwp_info
*lwp
= get_thread_lwp (thr_arg
);
1824 return callback (lwp
);
1830 return get_thread_lwp (thread
);
1833 /* Detect zombie thread group leaders, and "exit" them. We can't reap
1834 their exits until all other threads in the group have exited. */
1837 check_zombie_leaders (void)
1839 for_each_process ([] (process_info
*proc
) {
1840 pid_t leader_pid
= pid_of (proc
);
1841 struct lwp_info
*leader_lp
;
1843 leader_lp
= find_lwp_pid (ptid_t (leader_pid
));
1846 debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1847 "num_lwps=%d, zombie=%d\n",
1848 leader_pid
, leader_lp
!= NULL
, num_lwps (leader_pid
),
1849 linux_proc_pid_is_zombie (leader_pid
));
1851 if (leader_lp
!= NULL
&& !leader_lp
->stopped
1852 /* Check if there are other threads in the group, as we may
1853 have raced with the inferior simply exiting. */
1854 && !last_thread_of_process_p (leader_pid
)
1855 && linux_proc_pid_is_zombie (leader_pid
))
1857 /* A leader zombie can mean one of two things:
1859 - It exited, and there's an exit status pending
1860 available, or only the leader exited (not the whole
1861 program). In the latter case, we can't waitpid the
1862 leader's exit status until all other threads are gone.
1864 - There are 3 or more threads in the group, and a thread
1865 other than the leader exec'd. On an exec, the Linux
1866 kernel destroys all other threads (except the execing
1867 one) in the thread group, and resets the execing thread's
1868 tid to the tgid. No exit notification is sent for the
1869 execing thread -- from the ptracer's perspective, it
1870 appears as though the execing thread just vanishes.
1871 Until we reap all other threads except the leader and the
1872 execing thread, the leader will be zombie, and the
1873 execing thread will be in `D (disc sleep)'. As soon as
1874 all other threads are reaped, the execing thread changes
1875 it's tid to the tgid, and the previous (zombie) leader
1876 vanishes, giving place to the "new" leader. We could try
1877 distinguishing the exit and exec cases, by waiting once
1878 more, and seeing if something comes out, but it doesn't
1879 sound useful. The previous leader _does_ go away, and
1880 we'll re-add the new one once we see the exec event
1881 (which is just the same as what would happen if the
1882 previous leader did exit voluntarily before some other
1886 debug_printf ("CZL: Thread group leader %d zombie "
1887 "(it exited, or another thread execd).\n",
1890 delete_lwp (leader_lp
);
1895 /* Callback for `find_thread'. Returns the first LWP that is not
1899 not_stopped_callback (thread_info
*thread
, ptid_t filter
)
1901 if (!thread
->id
.matches (filter
))
1904 lwp_info
*lwp
= get_thread_lwp (thread
);
1906 return !lwp
->stopped
;
1909 /* Increment LWP's suspend count. */
1912 lwp_suspended_inc (struct lwp_info
*lwp
)
1916 if (debug_threads
&& lwp
->suspended
> 4)
1918 struct thread_info
*thread
= get_lwp_thread (lwp
);
1920 debug_printf ("LWP %ld has a suspiciously high suspend count,"
1921 " suspended=%d\n", lwpid_of (thread
), lwp
->suspended
);
1925 /* Decrement LWP's suspend count. */
1928 lwp_suspended_decr (struct lwp_info
*lwp
)
1932 if (lwp
->suspended
< 0)
1934 struct thread_info
*thread
= get_lwp_thread (lwp
);
1936 internal_error (__FILE__
, __LINE__
,
1937 "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread
),
1942 /* This function should only be called if the LWP got a SIGTRAP.
1944 Handle any tracepoint steps or hits. Return true if a tracepoint
1945 event was handled, 0 otherwise. */
1948 handle_tracepoints (struct lwp_info
*lwp
)
1950 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1951 int tpoint_related_event
= 0;
1953 gdb_assert (lwp
->suspended
== 0);
1955 /* If this tracepoint hit causes a tracing stop, we'll immediately
1956 uninsert tracepoints. To do this, we temporarily pause all
1957 threads, unpatch away, and then unpause threads. We need to make
1958 sure the unpausing doesn't resume LWP too. */
1959 lwp_suspended_inc (lwp
);
1961 /* And we need to be sure that any all-threads-stopping doesn't try
1962 to move threads out of the jump pads, as it could deadlock the
1963 inferior (LWP could be in the jump pad, maybe even holding the
1966 /* Do any necessary step collect actions. */
1967 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1969 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1971 /* See if we just hit a tracepoint and do its main collect
1973 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1975 lwp_suspended_decr (lwp
);
1977 gdb_assert (lwp
->suspended
== 0);
1978 gdb_assert (!stabilizing_threads
1979 || (lwp
->collecting_fast_tracepoint
1980 != fast_tpoint_collect_result::not_collecting
));
1982 if (tpoint_related_event
)
1985 debug_printf ("got a tracepoint event\n");
1992 /* Convenience wrapper. Returns information about LWP's fast tracepoint
1993 collection status. */
1995 static fast_tpoint_collect_result
1996 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
1997 struct fast_tpoint_collect_status
*status
)
1999 CORE_ADDR thread_area
;
2000 struct thread_info
*thread
= get_lwp_thread (lwp
);
2002 if (the_low_target
.get_thread_area
== NULL
)
2003 return fast_tpoint_collect_result::not_collecting
;
2005 /* Get the thread area address. This is used to recognize which
2006 thread is which when tracing with the in-process agent library.
2007 We don't read anything from the address, and treat it as opaque;
2008 it's the address itself that we assume is unique per-thread. */
2009 if ((*the_low_target
.get_thread_area
) (lwpid_of (thread
), &thread_area
) == -1)
2010 return fast_tpoint_collect_result::not_collecting
;
2012 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
2016 linux_process_target::maybe_move_out_of_jump_pad (lwp_info
*lwp
, int *wstat
)
2018 struct thread_info
*saved_thread
;
2020 saved_thread
= current_thread
;
2021 current_thread
= get_lwp_thread (lwp
);
2024 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
2025 && supports_fast_tracepoints ()
2026 && agent_loaded_p ())
2028 struct fast_tpoint_collect_status status
;
2031 debug_printf ("Checking whether LWP %ld needs to move out of the "
2033 lwpid_of (current_thread
));
2035 fast_tpoint_collect_result r
2036 = linux_fast_tracepoint_collecting (lwp
, &status
);
2039 || (WSTOPSIG (*wstat
) != SIGILL
2040 && WSTOPSIG (*wstat
) != SIGFPE
2041 && WSTOPSIG (*wstat
) != SIGSEGV
2042 && WSTOPSIG (*wstat
) != SIGBUS
))
2044 lwp
->collecting_fast_tracepoint
= r
;
2046 if (r
!= fast_tpoint_collect_result::not_collecting
)
2048 if (r
== fast_tpoint_collect_result::before_insn
2049 && lwp
->exit_jump_pad_bkpt
== NULL
)
2051 /* Haven't executed the original instruction yet.
2052 Set breakpoint there, and wait till it's hit,
2053 then single-step until exiting the jump pad. */
2054 lwp
->exit_jump_pad_bkpt
2055 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
2059 debug_printf ("Checking whether LWP %ld needs to move out of "
2060 "the jump pad...it does\n",
2061 lwpid_of (current_thread
));
2062 current_thread
= saved_thread
;
2069 /* If we get a synchronous signal while collecting, *and*
2070 while executing the (relocated) original instruction,
2071 reset the PC to point at the tpoint address, before
2072 reporting to GDB. Otherwise, it's an IPA lib bug: just
2073 report the signal to GDB, and pray for the best. */
2075 lwp
->collecting_fast_tracepoint
2076 = fast_tpoint_collect_result::not_collecting
;
2078 if (r
!= fast_tpoint_collect_result::not_collecting
2079 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
2080 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
2083 struct regcache
*regcache
;
2085 /* The si_addr on a few signals references the address
2086 of the faulting instruction. Adjust that as
2088 if ((WSTOPSIG (*wstat
) == SIGILL
2089 || WSTOPSIG (*wstat
) == SIGFPE
2090 || WSTOPSIG (*wstat
) == SIGBUS
2091 || WSTOPSIG (*wstat
) == SIGSEGV
)
2092 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
2093 (PTRACE_TYPE_ARG3
) 0, &info
) == 0
2094 /* Final check just to make sure we don't clobber
2095 the siginfo of non-kernel-sent signals. */
2096 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
2098 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
2099 ptrace (PTRACE_SETSIGINFO
, lwpid_of (current_thread
),
2100 (PTRACE_TYPE_ARG3
) 0, &info
);
2103 regcache
= get_thread_regcache (current_thread
, 1);
2104 low_set_pc (regcache
, status
.tpoint_addr
);
2105 lwp
->stop_pc
= status
.tpoint_addr
;
2107 /* Cancel any fast tracepoint lock this thread was
2109 force_unlock_trace_buffer ();
2112 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
2115 debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
2116 "stopping all threads momentarily.\n");
2118 stop_all_lwps (1, lwp
);
2120 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
2121 lwp
->exit_jump_pad_bkpt
= NULL
;
2123 unstop_all_lwps (1, lwp
);
2125 gdb_assert (lwp
->suspended
>= 0);
2131 debug_printf ("Checking whether LWP %ld needs to move out of the "
2133 lwpid_of (current_thread
));
2135 current_thread
= saved_thread
;
2139 /* Enqueue one signal in the "signals to report later when out of the
2143 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2145 struct pending_signals
*p_sig
;
2146 struct thread_info
*thread
= get_lwp_thread (lwp
);
2149 debug_printf ("Deferring signal %d for LWP %ld.\n",
2150 WSTOPSIG (*wstat
), lwpid_of (thread
));
2154 struct pending_signals
*sig
;
2156 for (sig
= lwp
->pending_signals_to_report
;
2159 debug_printf (" Already queued %d\n",
2162 debug_printf (" (no more currently queued signals)\n");
2165 /* Don't enqueue non-RT signals if they are already in the deferred
2166 queue. (SIGSTOP being the easiest signal to see ending up here
2168 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
2170 struct pending_signals
*sig
;
2172 for (sig
= lwp
->pending_signals_to_report
;
2176 if (sig
->signal
== WSTOPSIG (*wstat
))
2179 debug_printf ("Not requeuing already queued non-RT signal %d"
2188 p_sig
= XCNEW (struct pending_signals
);
2189 p_sig
->prev
= lwp
->pending_signals_to_report
;
2190 p_sig
->signal
= WSTOPSIG (*wstat
);
2192 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2195 lwp
->pending_signals_to_report
= p_sig
;
2198 /* Dequeue one signal from the "signals to report later when out of
2199 the jump pad" list. */
2202 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2204 struct thread_info
*thread
= get_lwp_thread (lwp
);
2206 if (lwp
->pending_signals_to_report
!= NULL
)
2208 struct pending_signals
**p_sig
;
2210 p_sig
= &lwp
->pending_signals_to_report
;
2211 while ((*p_sig
)->prev
!= NULL
)
2212 p_sig
= &(*p_sig
)->prev
;
2214 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
2215 if ((*p_sig
)->info
.si_signo
!= 0)
2216 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2222 debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
2223 WSTOPSIG (*wstat
), lwpid_of (thread
));
2227 struct pending_signals
*sig
;
2229 for (sig
= lwp
->pending_signals_to_report
;
2232 debug_printf (" Still queued %d\n",
2235 debug_printf (" (no more queued signals)\n");
2244 /* Fetch the possibly triggered data watchpoint info and store it in
2247 On some archs, like x86, that use debug registers to set
2248 watchpoints, it's possible that the way to know which watched
2249 address trapped, is to check the register that is used to select
2250 which address to watch. Problem is, between setting the watchpoint
2251 and reading back which data address trapped, the user may change
2252 the set of watchpoints, and, as a consequence, GDB changes the
2253 debug registers in the inferior. To avoid reading back a stale
2254 stopped-data-address when that happens, we cache in LP the fact
2255 that a watchpoint trapped, and the corresponding data address, as
2256 soon as we see CHILD stop with a SIGTRAP. If GDB changes the debug
2257 registers meanwhile, we have the cached data we can rely on. */
2260 check_stopped_by_watchpoint (struct lwp_info
*child
)
2262 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
2264 struct thread_info
*saved_thread
;
2266 saved_thread
= current_thread
;
2267 current_thread
= get_lwp_thread (child
);
2269 if (the_low_target
.stopped_by_watchpoint ())
2271 child
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2273 if (the_low_target
.stopped_data_address
!= NULL
)
2274 child
->stopped_data_address
2275 = the_low_target
.stopped_data_address ();
2277 child
->stopped_data_address
= 0;
2280 current_thread
= saved_thread
;
2283 return child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2286 /* Return the ptrace options that we want to try to enable. */
2289 linux_low_ptrace_options (int attached
)
2291 client_state
&cs
= get_client_state ();
2295 options
|= PTRACE_O_EXITKILL
;
2297 if (cs
.report_fork_events
)
2298 options
|= PTRACE_O_TRACEFORK
;
2300 if (cs
.report_vfork_events
)
2301 options
|= (PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEVFORKDONE
);
2303 if (cs
.report_exec_events
)
2304 options
|= PTRACE_O_TRACEEXEC
;
2306 options
|= PTRACE_O_TRACESYSGOOD
;
2312 linux_process_target::filter_event (int lwpid
, int wstat
)
2314 client_state
&cs
= get_client_state ();
2315 struct lwp_info
*child
;
2316 struct thread_info
*thread
;
2317 int have_stop_pc
= 0;
2319 child
= find_lwp_pid (ptid_t (lwpid
));
2321 /* Check for stop events reported by a process we didn't already
2322 know about - anything not already in our LWP list.
2324 If we're expecting to receive stopped processes after
2325 fork, vfork, and clone events, then we'll just add the
2326 new one to our list and go back to waiting for the event
2327 to be reported - the stopped process might be returned
2328 from waitpid before or after the event is.
2330 But note the case of a non-leader thread exec'ing after the
2331 leader having exited, and gone from our lists (because
2332 check_zombie_leaders deleted it). The non-leader thread
2333 changes its tid to the tgid. */
2335 if (WIFSTOPPED (wstat
) && child
== NULL
&& WSTOPSIG (wstat
) == SIGTRAP
2336 && linux_ptrace_get_extended_event (wstat
) == PTRACE_EVENT_EXEC
)
2340 /* A multi-thread exec after we had seen the leader exiting. */
2343 debug_printf ("LLW: Re-adding thread group leader LWP %d"
2344 "after exec.\n", lwpid
);
2347 child_ptid
= ptid_t (lwpid
, lwpid
, 0);
2348 child
= add_lwp (child_ptid
);
2350 current_thread
= child
->thread
;
2353 /* If we didn't find a process, one of two things presumably happened:
2354 - A process we started and then detached from has exited. Ignore it.
2355 - A process we are controlling has forked and the new child's stop
2356 was reported to us by the kernel. Save its PID. */
2357 if (child
== NULL
&& WIFSTOPPED (wstat
))
2359 add_to_pid_list (&stopped_pids
, lwpid
, wstat
);
2362 else if (child
== NULL
)
2365 thread
= get_lwp_thread (child
);
2369 child
->last_status
= wstat
;
2371 /* Check if the thread has exited. */
2372 if ((WIFEXITED (wstat
) || WIFSIGNALED (wstat
)))
2375 debug_printf ("LLFE: %d exited.\n", lwpid
);
2377 if (finish_step_over (child
))
2379 /* Unsuspend all other LWPs, and set them back running again. */
2380 unsuspend_all_lwps (child
);
2383 /* If there is at least one more LWP, then the exit signal was
2384 not the end of the debugged application and should be
2385 ignored, unless GDB wants to hear about thread exits. */
2386 if (cs
.report_thread_events
2387 || last_thread_of_process_p (pid_of (thread
)))
2389 /* Since events are serialized to GDB core, and we can't
2390 report this one right now. Leave the status pending for
2391 the next time we're able to report it. */
2392 mark_lwp_dead (child
, wstat
);
2402 gdb_assert (WIFSTOPPED (wstat
));
2404 if (WIFSTOPPED (wstat
))
2406 struct process_info
*proc
;
2408 /* Architecture-specific setup after inferior is running. */
2409 proc
= find_process_pid (pid_of (thread
));
2410 if (proc
->tdesc
== NULL
)
2414 /* This needs to happen after we have attached to the
2415 inferior and it is stopped for the first time, but
2416 before we access any inferior registers. */
2417 arch_setup_thread (thread
);
2421 /* The process is started, but GDBserver will do
2422 architecture-specific setup after the program stops at
2423 the first instruction. */
2424 child
->status_pending_p
= 1;
2425 child
->status_pending
= wstat
;
2431 if (WIFSTOPPED (wstat
) && child
->must_set_ptrace_flags
)
2433 struct process_info
*proc
= find_process_pid (pid_of (thread
));
2434 int options
= linux_low_ptrace_options (proc
->attached
);
2436 linux_enable_event_reporting (lwpid
, options
);
2437 child
->must_set_ptrace_flags
= 0;
2440 /* Always update syscall_state, even if it will be filtered later. */
2441 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SYSCALL_SIGTRAP
)
2443 child
->syscall_state
2444 = (child
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2445 ? TARGET_WAITKIND_SYSCALL_RETURN
2446 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2450 /* Almost all other ptrace-stops are known to be outside of system
2451 calls, with further exceptions in handle_extended_wait. */
2452 child
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2455 /* Be careful to not overwrite stop_pc until save_stop_reason is
2457 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
2458 && linux_is_extended_waitstatus (wstat
))
2460 child
->stop_pc
= get_pc (child
);
2461 if (handle_extended_wait (&child
, wstat
))
2463 /* The event has been handled, so just return without
2469 if (linux_wstatus_maybe_breakpoint (wstat
))
2471 if (save_stop_reason (child
))
2476 child
->stop_pc
= get_pc (child
);
2478 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGSTOP
2479 && child
->stop_expected
)
2482 debug_printf ("Expected stop.\n");
2483 child
->stop_expected
= 0;
2485 if (thread
->last_resume_kind
== resume_stop
)
2487 /* We want to report the stop to the core. Treat the
2488 SIGSTOP as a normal event. */
2490 debug_printf ("LLW: resume_stop SIGSTOP caught for %s.\n",
2491 target_pid_to_str (ptid_of (thread
)));
2493 else if (stopping_threads
!= NOT_STOPPING_THREADS
)
2495 /* Stopping threads. We don't want this SIGSTOP to end up
2498 debug_printf ("LLW: SIGSTOP caught for %s "
2499 "while stopping threads.\n",
2500 target_pid_to_str (ptid_of (thread
)));
2505 /* This is a delayed SIGSTOP. Filter out the event. */
2507 debug_printf ("LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2508 child
->stepping
? "step" : "continue",
2509 target_pid_to_str (ptid_of (thread
)));
2511 resume_one_lwp (child
, child
->stepping
, 0, NULL
);
2516 child
->status_pending_p
= 1;
2517 child
->status_pending
= wstat
;
2521 /* Return true if THREAD is doing hardware single step. */
2524 maybe_hw_step (struct thread_info
*thread
)
2526 if (can_hardware_single_step ())
2530 /* GDBserver must insert single-step breakpoint for software
2532 gdb_assert (has_single_step_breakpoints (thread
));
2538 linux_process_target::resume_stopped_resumed_lwps (thread_info
*thread
)
2540 struct lwp_info
*lp
= get_thread_lwp (thread
);
2544 && !lp
->status_pending_p
2545 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2549 if (thread
->last_resume_kind
== resume_step
)
2550 step
= maybe_hw_step (thread
);
2553 debug_printf ("RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
2554 target_pid_to_str (ptid_of (thread
)),
2555 paddress (lp
->stop_pc
),
2558 resume_one_lwp (lp
, step
, GDB_SIGNAL_0
, NULL
);
2563 linux_process_target::wait_for_event_filtered (ptid_t wait_ptid
,
2565 int *wstatp
, int options
)
2567 struct thread_info
*event_thread
;
2568 struct lwp_info
*event_child
, *requested_child
;
2569 sigset_t block_mask
, prev_mask
;
2572 /* N.B. event_thread points to the thread_info struct that contains
2573 event_child. Keep them in sync. */
2574 event_thread
= NULL
;
2576 requested_child
= NULL
;
2578 /* Check for a lwp with a pending status. */
2580 if (filter_ptid
== minus_one_ptid
|| filter_ptid
.is_pid ())
2582 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2584 return status_pending_p_callback (thread
, filter_ptid
);
2587 if (event_thread
!= NULL
)
2588 event_child
= get_thread_lwp (event_thread
);
2589 if (debug_threads
&& event_thread
)
2590 debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread
));
2592 else if (filter_ptid
!= null_ptid
)
2594 requested_child
= find_lwp_pid (filter_ptid
);
2596 if (stopping_threads
== NOT_STOPPING_THREADS
2597 && requested_child
->status_pending_p
2598 && (requested_child
->collecting_fast_tracepoint
2599 != fast_tpoint_collect_result::not_collecting
))
2601 enqueue_one_deferred_signal (requested_child
,
2602 &requested_child
->status_pending
);
2603 requested_child
->status_pending_p
= 0;
2604 requested_child
->status_pending
= 0;
2605 resume_one_lwp (requested_child
, 0, 0, NULL
);
2608 if (requested_child
->suspended
2609 && requested_child
->status_pending_p
)
2611 internal_error (__FILE__
, __LINE__
,
2612 "requesting an event out of a"
2613 " suspended child?");
2616 if (requested_child
->status_pending_p
)
2618 event_child
= requested_child
;
2619 event_thread
= get_lwp_thread (event_child
);
2623 if (event_child
!= NULL
)
2626 debug_printf ("Got an event from pending child %ld (%04x)\n",
2627 lwpid_of (event_thread
), event_child
->status_pending
);
2628 *wstatp
= event_child
->status_pending
;
2629 event_child
->status_pending_p
= 0;
2630 event_child
->status_pending
= 0;
2631 current_thread
= event_thread
;
2632 return lwpid_of (event_thread
);
2635 /* But if we don't find a pending event, we'll have to wait.
2637 We only enter this loop if no process has a pending wait status.
2638 Thus any action taken in response to a wait status inside this
2639 loop is responding as soon as we detect the status, not after any
2642 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2643 all signals while here. */
2644 sigfillset (&block_mask
);
2645 gdb_sigmask (SIG_BLOCK
, &block_mask
, &prev_mask
);
2647 /* Always pull all events out of the kernel. We'll randomly select
2648 an event LWP out of all that have events, to prevent
2650 while (event_child
== NULL
)
2654 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2657 - If the thread group leader exits while other threads in the
2658 thread group still exist, waitpid(TGID, ...) hangs. That
2659 waitpid won't return an exit status until the other threads
2660 in the group are reaped.
2662 - When a non-leader thread execs, that thread just vanishes
2663 without reporting an exit (so we'd hang if we waited for it
2664 explicitly in that case). The exec event is reported to
2667 ret
= my_waitpid (-1, wstatp
, options
| WNOHANG
);
2670 debug_printf ("LWFE: waitpid(-1, ...) returned %d, %s\n",
2671 ret
, errno
? safe_strerror (errno
) : "ERRNO-OK");
2677 debug_printf ("LLW: waitpid %ld received %s\n",
2678 (long) ret
, status_to_str (*wstatp
));
2681 /* Filter all events. IOW, leave all events pending. We'll
2682 randomly select an event LWP out of all that have events
2684 filter_event (ret
, *wstatp
);
2685 /* Retry until nothing comes out of waitpid. A single
2686 SIGCHLD can indicate more than one child stopped. */
2690 /* Now that we've pulled all events out of the kernel, resume
2691 LWPs that don't have an interesting event to report. */
2692 if (stopping_threads
== NOT_STOPPING_THREADS
)
2693 for_each_thread ([this] (thread_info
*thread
)
2695 resume_stopped_resumed_lwps (thread
);
2698 /* ... and find an LWP with a status to report to the core, if
2700 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2702 return status_pending_p_callback (thread
, filter_ptid
);
2705 if (event_thread
!= NULL
)
2707 event_child
= get_thread_lwp (event_thread
);
2708 *wstatp
= event_child
->status_pending
;
2709 event_child
->status_pending_p
= 0;
2710 event_child
->status_pending
= 0;
2714 /* Check for zombie thread group leaders. Those can't be reaped
2715 until all other threads in the thread group are. */
2716 check_zombie_leaders ();
2718 auto not_stopped
= [&] (thread_info
*thread
)
2720 return not_stopped_callback (thread
, wait_ptid
);
2723 /* If there are no resumed children left in the set of LWPs we
2724 want to wait for, bail. We can't just block in
2725 waitpid/sigsuspend, because lwps might have been left stopped
2726 in trace-stop state, and we'd be stuck forever waiting for
2727 their status to change (which would only happen if we resumed
2728 them). Even if WNOHANG is set, this return code is preferred
2729 over 0 (below), as it is more detailed. */
2730 if (find_thread (not_stopped
) == NULL
)
2733 debug_printf ("LLW: exit (no unwaited-for LWP)\n");
2734 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2738 /* No interesting event to report to the caller. */
2739 if ((options
& WNOHANG
))
2742 debug_printf ("WNOHANG set, no event found\n");
2744 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2748 /* Block until we get an event reported with SIGCHLD. */
2750 debug_printf ("sigsuspend'ing\n");
2752 sigsuspend (&prev_mask
);
2753 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2757 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2759 current_thread
= event_thread
;
2761 return lwpid_of (event_thread
);
2765 linux_process_target::wait_for_event (ptid_t ptid
, int *wstatp
, int options
)
2767 return wait_for_event_filtered (ptid
, ptid
, wstatp
, options
);
2770 /* Select one LWP out of those that have events pending. */
2773 select_event_lwp (struct lwp_info
**orig_lp
)
2775 struct thread_info
*event_thread
= NULL
;
2777 /* In all-stop, give preference to the LWP that is being
2778 single-stepped. There will be at most one, and it's the LWP that
2779 the core is most interested in. If we didn't do this, then we'd
2780 have to handle pending step SIGTRAPs somehow in case the core
2781 later continues the previously-stepped thread, otherwise we'd
2782 report the pending SIGTRAP, and the core, not having stepped the
2783 thread, wouldn't understand what the trap was for, and therefore
2784 would report it to the user as a random signal. */
2787 event_thread
= find_thread ([] (thread_info
*thread
)
2789 lwp_info
*lp
= get_thread_lwp (thread
);
2791 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2792 && thread
->last_resume_kind
== resume_step
2793 && lp
->status_pending_p
);
2796 if (event_thread
!= NULL
)
2799 debug_printf ("SEL: Select single-step %s\n",
2800 target_pid_to_str (ptid_of (event_thread
)));
2803 if (event_thread
== NULL
)
2805 /* No single-stepping LWP. Select one at random, out of those
2806 which have had events. */
2808 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2810 lwp_info
*lp
= get_thread_lwp (thread
);
2812 /* Only resumed LWPs that have an event pending. */
2813 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2814 && lp
->status_pending_p
);
2818 if (event_thread
!= NULL
)
2820 struct lwp_info
*event_lp
= get_thread_lwp (event_thread
);
2822 /* Switch the event LWP. */
2823 *orig_lp
= event_lp
;
2827 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2831 unsuspend_all_lwps (struct lwp_info
*except
)
2833 for_each_thread ([&] (thread_info
*thread
)
2835 lwp_info
*lwp
= get_thread_lwp (thread
);
2838 lwp_suspended_decr (lwp
);
2842 static bool stuck_in_jump_pad_callback (thread_info
*thread
);
2843 static bool lwp_running (thread_info
*thread
);
2845 /* Stabilize threads (move out of jump pads).
2847 If a thread is midway collecting a fast tracepoint, we need to
2848 finish the collection and move it out of the jump pad before
2849 reporting the signal.
2851 This avoids recursion while collecting (when a signal arrives
2852 midway, and the signal handler itself collects), which would trash
2853 the trace buffer. In case the user set a breakpoint in a signal
2854 handler, this avoids the backtrace showing the jump pad, etc..
2855 Most importantly, there are certain things we can't do safely if
2856 threads are stopped in a jump pad (or in its callee's). For
2859 - starting a new trace run. A thread still collecting the
2860 previous run, could trash the trace buffer when resumed. The trace
2861 buffer control structures would have been reset but the thread had
2862 no way to tell. The thread could even midway memcpy'ing to the
2863 buffer, which would mean that when resumed, it would clobber the
2864 trace buffer that had been set for a new run.
2866 - we can't rewrite/reuse the jump pads for new tracepoints
2867 safely. Say you do tstart while a thread is stopped midway while
2868 collecting. When the thread is later resumed, it finishes the
2869 collection, and returns to the jump pad, to execute the original
2870 instruction that was under the tracepoint jump at the time the
2871 older run had been started. If the jump pad had been rewritten
2872 since for something else in the new run, the thread would now
2873 execute the wrong / random instructions. */
2876 linux_process_target::stabilize_threads ()
2878 thread_info
*thread_stuck
= find_thread (stuck_in_jump_pad_callback
);
2880 if (thread_stuck
!= NULL
)
2883 debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
2884 lwpid_of (thread_stuck
));
2888 thread_info
*saved_thread
= current_thread
;
2890 stabilizing_threads
= 1;
2893 for_each_thread ([this] (thread_info
*thread
)
2895 move_out_of_jump_pad (thread
);
2898 /* Loop until all are stopped out of the jump pads. */
2899 while (find_thread (lwp_running
) != NULL
)
2901 struct target_waitstatus ourstatus
;
2902 struct lwp_info
*lwp
;
2905 /* Note that we go through the full wait even loop. While
2906 moving threads out of jump pad, we need to be able to step
2907 over internal breakpoints and such. */
2908 wait_1 (minus_one_ptid
, &ourstatus
, 0);
2910 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
2912 lwp
= get_thread_lwp (current_thread
);
2915 lwp_suspended_inc (lwp
);
2917 if (ourstatus
.value
.sig
!= GDB_SIGNAL_0
2918 || current_thread
->last_resume_kind
== resume_stop
)
2920 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.value
.sig
));
2921 enqueue_one_deferred_signal (lwp
, &wstat
);
2926 unsuspend_all_lwps (NULL
);
2928 stabilizing_threads
= 0;
2930 current_thread
= saved_thread
;
2934 thread_stuck
= find_thread (stuck_in_jump_pad_callback
);
2936 if (thread_stuck
!= NULL
)
2937 debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
2938 lwpid_of (thread_stuck
));
2942 /* Convenience function that is called when the kernel reports an
2943 event that is not passed out to GDB. */
2946 ignore_event (struct target_waitstatus
*ourstatus
)
2948 /* If we got an event, there may still be others, as a single
2949 SIGCHLD can indicate more than one child stopped. This forces
2950 another target_wait call. */
2953 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2957 /* Convenience function that is called when the kernel reports an exit
2958 event. This decides whether to report the event to GDB as a
2959 process exit event, a thread exit event, or to suppress the
2963 filter_exit_event (struct lwp_info
*event_child
,
2964 struct target_waitstatus
*ourstatus
)
2966 client_state
&cs
= get_client_state ();
2967 struct thread_info
*thread
= get_lwp_thread (event_child
);
2968 ptid_t ptid
= ptid_of (thread
);
2970 if (!last_thread_of_process_p (pid_of (thread
)))
2972 if (cs
.report_thread_events
)
2973 ourstatus
->kind
= TARGET_WAITKIND_THREAD_EXITED
;
2975 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2977 delete_lwp (event_child
);
2982 /* Returns 1 if GDB is interested in any event_child syscalls. */
2985 gdb_catching_syscalls_p (struct lwp_info
*event_child
)
2987 struct thread_info
*thread
= get_lwp_thread (event_child
);
2988 struct process_info
*proc
= get_thread_process (thread
);
2990 return !proc
->syscalls_to_catch
.empty ();
2993 /* Returns 1 if GDB is interested in the event_child syscall.
2994 Only to be called when stopped reason is SYSCALL_SIGTRAP. */
2997 gdb_catch_this_syscall_p (struct lwp_info
*event_child
)
3000 struct thread_info
*thread
= get_lwp_thread (event_child
);
3001 struct process_info
*proc
= get_thread_process (thread
);
3003 if (proc
->syscalls_to_catch
.empty ())
3006 if (proc
->syscalls_to_catch
[0] == ANY_SYSCALL
)
3009 get_syscall_trapinfo (event_child
, &sysno
);
3011 for (int iter
: proc
->syscalls_to_catch
)
3019 linux_process_target::wait_1 (ptid_t ptid
, target_waitstatus
*ourstatus
,
3022 client_state
&cs
= get_client_state ();
3024 struct lwp_info
*event_child
;
3027 int step_over_finished
;
3028 int bp_explains_trap
;
3029 int maybe_internal_trap
;
3038 debug_printf ("wait_1: [%s]\n", target_pid_to_str (ptid
));
3041 /* Translate generic target options into linux options. */
3043 if (target_options
& TARGET_WNOHANG
)
3046 bp_explains_trap
= 0;
3049 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3051 auto status_pending_p_any
= [&] (thread_info
*thread
)
3053 return status_pending_p_callback (thread
, minus_one_ptid
);
3056 auto not_stopped
= [&] (thread_info
*thread
)
3058 return not_stopped_callback (thread
, minus_one_ptid
);
3061 /* Find a resumed LWP, if any. */
3062 if (find_thread (status_pending_p_any
) != NULL
)
3064 else if (find_thread (not_stopped
) != NULL
)
3069 if (step_over_bkpt
== null_ptid
)
3070 pid
= wait_for_event (ptid
, &w
, options
);
3074 debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
3075 target_pid_to_str (step_over_bkpt
));
3076 pid
= wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
3079 if (pid
== 0 || (pid
== -1 && !any_resumed
))
3081 gdb_assert (target_options
& TARGET_WNOHANG
);
3085 debug_printf ("wait_1 ret = null_ptid, "
3086 "TARGET_WAITKIND_IGNORE\n");
3090 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3097 debug_printf ("wait_1 ret = null_ptid, "
3098 "TARGET_WAITKIND_NO_RESUMED\n");
3102 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3106 event_child
= get_thread_lwp (current_thread
);
3108 /* wait_for_event only returns an exit status for the last
3109 child of a process. Report it. */
3110 if (WIFEXITED (w
) || WIFSIGNALED (w
))
3114 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
3115 ourstatus
->value
.integer
= WEXITSTATUS (w
);
3119 debug_printf ("wait_1 ret = %s, exited with "
3121 target_pid_to_str (ptid_of (current_thread
)),
3128 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
3129 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (w
));
3133 debug_printf ("wait_1 ret = %s, terminated with "
3135 target_pid_to_str (ptid_of (current_thread
)),
3141 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3142 return filter_exit_event (event_child
, ourstatus
);
3144 return ptid_of (current_thread
);
3147 /* If step-over executes a breakpoint instruction, in the case of a
3148 hardware single step it means a gdb/gdbserver breakpoint had been
3149 planted on top of a permanent breakpoint, in the case of a software
3150 single step it may just mean that gdbserver hit the reinsert breakpoint.
3151 The PC has been adjusted by save_stop_reason to point at
3152 the breakpoint address.
3153 So in the case of the hardware single step advance the PC manually
3154 past the breakpoint and in the case of software single step advance only
3155 if it's not the single_step_breakpoint we are hitting.
3156 This avoids that a program would keep trapping a permanent breakpoint
3158 if (step_over_bkpt
!= null_ptid
3159 && event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3160 && (event_child
->stepping
3161 || !single_step_breakpoint_inserted_here (event_child
->stop_pc
)))
3163 int increment_pc
= 0;
3164 int breakpoint_kind
= 0;
3165 CORE_ADDR stop_pc
= event_child
->stop_pc
;
3167 breakpoint_kind
= breakpoint_kind_from_current_state (&stop_pc
);
3168 sw_breakpoint_from_kind (breakpoint_kind
, &increment_pc
);
3172 debug_printf ("step-over for %s executed software breakpoint\n",
3173 target_pid_to_str (ptid_of (current_thread
)));
3176 if (increment_pc
!= 0)
3178 struct regcache
*regcache
3179 = get_thread_regcache (current_thread
, 1);
3181 event_child
->stop_pc
+= increment_pc
;
3182 low_set_pc (regcache
, event_child
->stop_pc
);
3184 if (!(*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))
3185 event_child
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
3189 /* If this event was not handled before, and is not a SIGTRAP, we
3190 report it. SIGILL and SIGSEGV are also treated as traps in case
3191 a breakpoint is inserted at the current PC. If this target does
3192 not support internal breakpoints at all, we also report the
3193 SIGTRAP without further processing; it's of no concern to us. */
3195 = (low_supports_breakpoints ()
3196 && (WSTOPSIG (w
) == SIGTRAP
3197 || ((WSTOPSIG (w
) == SIGILL
3198 || WSTOPSIG (w
) == SIGSEGV
)
3199 && (*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))));
3201 if (maybe_internal_trap
)
3203 /* Handle anything that requires bookkeeping before deciding to
3204 report the event or continue waiting. */
3206 /* First check if we can explain the SIGTRAP with an internal
3207 breakpoint, or if we should possibly report the event to GDB.
3208 Do this before anything that may remove or insert a
3210 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
3212 /* We have a SIGTRAP, possibly a step-over dance has just
3213 finished. If so, tweak the state machine accordingly,
3214 reinsert breakpoints and delete any single-step
3216 step_over_finished
= finish_step_over (event_child
);
3218 /* Now invoke the callbacks of any internal breakpoints there. */
3219 check_breakpoints (event_child
->stop_pc
);
3221 /* Handle tracepoint data collecting. This may overflow the
3222 trace buffer, and cause a tracing stop, removing
3224 trace_event
= handle_tracepoints (event_child
);
3226 if (bp_explains_trap
)
3229 debug_printf ("Hit a gdbserver breakpoint.\n");
3234 /* We have some other signal, possibly a step-over dance was in
3235 progress, and it should be cancelled too. */
3236 step_over_finished
= finish_step_over (event_child
);
3239 /* We have all the data we need. Either report the event to GDB, or
3240 resume threads and keep waiting for more. */
3242 /* If we're collecting a fast tracepoint, finish the collection and
3243 move out of the jump pad before delivering a signal. See
3244 linux_stabilize_threads. */
3247 && WSTOPSIG (w
) != SIGTRAP
3248 && supports_fast_tracepoints ()
3249 && agent_loaded_p ())
3252 debug_printf ("Got signal %d for LWP %ld. Check if we need "
3253 "to defer or adjust it.\n",
3254 WSTOPSIG (w
), lwpid_of (current_thread
));
3256 /* Allow debugging the jump pad itself. */
3257 if (current_thread
->last_resume_kind
!= resume_step
3258 && maybe_move_out_of_jump_pad (event_child
, &w
))
3260 enqueue_one_deferred_signal (event_child
, &w
);
3263 debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
3264 WSTOPSIG (w
), lwpid_of (current_thread
));
3266 resume_one_lwp (event_child
, 0, 0, NULL
);
3270 return ignore_event (ourstatus
);
3274 if (event_child
->collecting_fast_tracepoint
3275 != fast_tpoint_collect_result::not_collecting
)
3278 debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
3279 "Check if we're already there.\n",
3280 lwpid_of (current_thread
),
3281 (int) event_child
->collecting_fast_tracepoint
);
3285 event_child
->collecting_fast_tracepoint
3286 = linux_fast_tracepoint_collecting (event_child
, NULL
);
3288 if (event_child
->collecting_fast_tracepoint
3289 != fast_tpoint_collect_result::before_insn
)
3291 /* No longer need this breakpoint. */
3292 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
3295 debug_printf ("No longer need exit-jump-pad bkpt; removing it."
3296 "stopping all threads momentarily.\n");
3298 /* Other running threads could hit this breakpoint.
3299 We don't handle moribund locations like GDB does,
3300 instead we always pause all threads when removing
3301 breakpoints, so that any step-over or
3302 decr_pc_after_break adjustment is always taken
3303 care of while the breakpoint is still
3305 stop_all_lwps (1, event_child
);
3307 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
3308 event_child
->exit_jump_pad_bkpt
= NULL
;
3310 unstop_all_lwps (1, event_child
);
3312 gdb_assert (event_child
->suspended
>= 0);
3316 if (event_child
->collecting_fast_tracepoint
3317 == fast_tpoint_collect_result::not_collecting
)
3320 debug_printf ("fast tracepoint finished "
3321 "collecting successfully.\n");
3323 /* We may have a deferred signal to report. */
3324 if (dequeue_one_deferred_signal (event_child
, &w
))
3327 debug_printf ("dequeued one signal.\n");
3332 debug_printf ("no deferred signals.\n");
3334 if (stabilizing_threads
)
3336 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3337 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3341 debug_printf ("wait_1 ret = %s, stopped "
3342 "while stabilizing threads\n",
3343 target_pid_to_str (ptid_of (current_thread
)));
3347 return ptid_of (current_thread
);
3353 /* Check whether GDB would be interested in this event. */
3355 /* Check if GDB is interested in this syscall. */
3357 && WSTOPSIG (w
) == SYSCALL_SIGTRAP
3358 && !gdb_catch_this_syscall_p (event_child
))
3362 debug_printf ("Ignored syscall for LWP %ld.\n",
3363 lwpid_of (current_thread
));
3366 resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
3370 return ignore_event (ourstatus
);
3373 /* If GDB is not interested in this signal, don't stop other
3374 threads, and don't report it to GDB. Just resume the inferior
3375 right away. We do this for threading-related signals as well as
3376 any that GDB specifically requested we ignore. But never ignore
3377 SIGSTOP if we sent it ourselves, and do not ignore signals when
3378 stepping - they may require special handling to skip the signal
3379 handler. Also never ignore signals that could be caused by a
3382 && current_thread
->last_resume_kind
!= resume_step
3384 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3385 (current_process ()->priv
->thread_db
!= NULL
3386 && (WSTOPSIG (w
) == __SIGRTMIN
3387 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
3390 (cs
.pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
3391 && !(WSTOPSIG (w
) == SIGSTOP
3392 && current_thread
->last_resume_kind
== resume_stop
)
3393 && !linux_wstatus_maybe_breakpoint (w
))))
3395 siginfo_t info
, *info_p
;
3398 debug_printf ("Ignored signal %d for LWP %ld.\n",
3399 WSTOPSIG (w
), lwpid_of (current_thread
));
3401 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
3402 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
3407 if (step_over_finished
)
3409 /* We cancelled this thread's step-over above. We still
3410 need to unsuspend all other LWPs, and set them back
3411 running again while the signal handler runs. */
3412 unsuspend_all_lwps (event_child
);
3414 /* Enqueue the pending signal info so that proceed_all_lwps
3416 enqueue_pending_signal (event_child
, WSTOPSIG (w
), info_p
);
3418 proceed_all_lwps ();
3422 resume_one_lwp (event_child
, event_child
->stepping
,
3423 WSTOPSIG (w
), info_p
);
3429 return ignore_event (ourstatus
);
3432 /* Note that all addresses are always "out of the step range" when
3433 there's no range to begin with. */
3434 in_step_range
= lwp_in_step_range (event_child
);
3436 /* If GDB wanted this thread to single step, and the thread is out
3437 of the step range, we always want to report the SIGTRAP, and let
3438 GDB handle it. Watchpoints should always be reported. So should
3439 signals we can't explain. A SIGTRAP we can't explain could be a
3440 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3441 do, we're be able to handle GDB breakpoints on top of internal
3442 breakpoints, by handling the internal breakpoint and still
3443 reporting the event to GDB. If we don't, we're out of luck, GDB
3444 won't see the breakpoint hit. If we see a single-step event but
3445 the thread should be continuing, don't pass the trap to gdb.
3446 That indicates that we had previously finished a single-step but
3447 left the single-step pending -- see
3448 complete_ongoing_step_over. */
3449 report_to_gdb
= (!maybe_internal_trap
3450 || (current_thread
->last_resume_kind
== resume_step
3452 || event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3454 && !bp_explains_trap
3456 && !step_over_finished
3457 && !(current_thread
->last_resume_kind
== resume_continue
3458 && event_child
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
))
3459 || (gdb_breakpoint_here (event_child
->stop_pc
)
3460 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
3461 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
))
3462 || event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
);
3464 run_breakpoint_commands (event_child
->stop_pc
);
3466 /* We found no reason GDB would want us to stop. We either hit one
3467 of our own breakpoints, or finished an internal step GDB
3468 shouldn't know about. */
3473 if (bp_explains_trap
)
3474 debug_printf ("Hit a gdbserver breakpoint.\n");
3475 if (step_over_finished
)
3476 debug_printf ("Step-over finished.\n");
3478 debug_printf ("Tracepoint event.\n");
3479 if (lwp_in_step_range (event_child
))
3480 debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
3481 paddress (event_child
->stop_pc
),
3482 paddress (event_child
->step_range_start
),
3483 paddress (event_child
->step_range_end
));
3486 /* We're not reporting this breakpoint to GDB, so apply the
3487 decr_pc_after_break adjustment to the inferior's regcache
3490 if (low_supports_breakpoints ())
3492 struct regcache
*regcache
3493 = get_thread_regcache (current_thread
, 1);
3494 low_set_pc (regcache
, event_child
->stop_pc
);
3497 if (step_over_finished
)
3499 /* If we have finished stepping over a breakpoint, we've
3500 stopped and suspended all LWPs momentarily except the
3501 stepping one. This is where we resume them all again.
3502 We're going to keep waiting, so use proceed, which
3503 handles stepping over the next breakpoint. */
3504 unsuspend_all_lwps (event_child
);
3508 /* Remove the single-step breakpoints if any. Note that
3509 there isn't single-step breakpoint if we finished stepping
3511 if (can_software_single_step ()
3512 && has_single_step_breakpoints (current_thread
))
3514 stop_all_lwps (0, event_child
);
3515 delete_single_step_breakpoints (current_thread
);
3516 unstop_all_lwps (0, event_child
);
3521 debug_printf ("proceeding all threads.\n");
3522 proceed_all_lwps ();
3527 return ignore_event (ourstatus
);
3532 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3535 = target_waitstatus_to_string (&event_child
->waitstatus
);
3537 debug_printf ("LWP %ld: extended event with waitstatus %s\n",
3538 lwpid_of (get_lwp_thread (event_child
)), str
.c_str ());
3540 if (current_thread
->last_resume_kind
== resume_step
)
3542 if (event_child
->step_range_start
== event_child
->step_range_end
)
3543 debug_printf ("GDB wanted to single-step, reporting event.\n");
3544 else if (!lwp_in_step_range (event_child
))
3545 debug_printf ("Out of step range, reporting event.\n");
3547 if (event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
3548 debug_printf ("Stopped by watchpoint.\n");
3549 else if (gdb_breakpoint_here (event_child
->stop_pc
))
3550 debug_printf ("Stopped by GDB breakpoint.\n");
3552 debug_printf ("Hit a non-gdbserver trap event.\n");
3555 /* Alright, we're going to report a stop. */
3557 /* Remove single-step breakpoints. */
3558 if (can_software_single_step ())
3560 /* Remove single-step breakpoints or not. It it is true, stop all
3561 lwps, so that other threads won't hit the breakpoint in the
3563 int remove_single_step_breakpoints_p
= 0;
3567 remove_single_step_breakpoints_p
3568 = has_single_step_breakpoints (current_thread
);
3572 /* In all-stop, a stop reply cancels all previous resume
3573 requests. Delete all single-step breakpoints. */
3575 find_thread ([&] (thread_info
*thread
) {
3576 if (has_single_step_breakpoints (thread
))
3578 remove_single_step_breakpoints_p
= 1;
3586 if (remove_single_step_breakpoints_p
)
3588 /* If we remove single-step breakpoints from memory, stop all lwps,
3589 so that other threads won't hit the breakpoint in the staled
3591 stop_all_lwps (0, event_child
);
3595 gdb_assert (has_single_step_breakpoints (current_thread
));
3596 delete_single_step_breakpoints (current_thread
);
3600 for_each_thread ([] (thread_info
*thread
){
3601 if (has_single_step_breakpoints (thread
))
3602 delete_single_step_breakpoints (thread
);
3606 unstop_all_lwps (0, event_child
);
3610 if (!stabilizing_threads
)
3612 /* In all-stop, stop all threads. */
3614 stop_all_lwps (0, NULL
);
3616 if (step_over_finished
)
3620 /* If we were doing a step-over, all other threads but
3621 the stepping one had been paused in start_step_over,
3622 with their suspend counts incremented. We don't want
3623 to do a full unstop/unpause, because we're in
3624 all-stop mode (so we want threads stopped), but we
3625 still need to unsuspend the other threads, to
3626 decrement their `suspended' count back. */
3627 unsuspend_all_lwps (event_child
);
3631 /* If we just finished a step-over, then all threads had
3632 been momentarily paused. In all-stop, that's fine,
3633 we want threads stopped by now anyway. In non-stop,
3634 we need to re-resume threads that GDB wanted to be
3636 unstop_all_lwps (1, event_child
);
3640 /* If we're not waiting for a specific LWP, choose an event LWP
3641 from among those that have had events. Giving equal priority
3642 to all LWPs that have had events helps prevent
3644 if (ptid
== minus_one_ptid
)
3646 event_child
->status_pending_p
= 1;
3647 event_child
->status_pending
= w
;
3649 select_event_lwp (&event_child
);
3651 /* current_thread and event_child must stay in sync. */
3652 current_thread
= get_lwp_thread (event_child
);
3654 event_child
->status_pending_p
= 0;
3655 w
= event_child
->status_pending
;
3659 /* Stabilize threads (move out of jump pads). */
3661 target_stabilize_threads ();
3665 /* If we just finished a step-over, then all threads had been
3666 momentarily paused. In all-stop, that's fine, we want
3667 threads stopped by now anyway. In non-stop, we need to
3668 re-resume threads that GDB wanted to be running. */
3669 if (step_over_finished
)
3670 unstop_all_lwps (1, event_child
);
3673 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3675 /* If the reported event is an exit, fork, vfork or exec, let
3678 /* Break the unreported fork relationship chain. */
3679 if (event_child
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
3680 || event_child
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
)
3682 event_child
->fork_relative
->fork_relative
= NULL
;
3683 event_child
->fork_relative
= NULL
;
3686 *ourstatus
= event_child
->waitstatus
;
3687 /* Clear the event lwp's waitstatus since we handled it already. */
3688 event_child
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3691 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3693 /* Now that we've selected our final event LWP, un-adjust its PC if
3694 it was a software breakpoint, and the client doesn't know we can
3695 adjust the breakpoint ourselves. */
3696 if (event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3697 && !cs
.swbreak_feature
)
3699 int decr_pc
= the_low_target
.decr_pc_after_break
;
3703 struct regcache
*regcache
3704 = get_thread_regcache (current_thread
, 1);
3705 low_set_pc (regcache
, event_child
->stop_pc
+ decr_pc
);
3709 if (WSTOPSIG (w
) == SYSCALL_SIGTRAP
)
3711 get_syscall_trapinfo (event_child
,
3712 &ourstatus
->value
.syscall_number
);
3713 ourstatus
->kind
= event_child
->syscall_state
;
3715 else if (current_thread
->last_resume_kind
== resume_stop
3716 && WSTOPSIG (w
) == SIGSTOP
)
3718 /* A thread that has been requested to stop by GDB with vCont;t,
3719 and it stopped cleanly, so report as SIG0. The use of
3720 SIGSTOP is an implementation detail. */
3721 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3723 else if (current_thread
->last_resume_kind
== resume_stop
3724 && WSTOPSIG (w
) != SIGSTOP
)
3726 /* A thread that has been requested to stop by GDB with vCont;t,
3727 but, it stopped for other reasons. */
3728 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3730 else if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
)
3732 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3735 gdb_assert (step_over_bkpt
== null_ptid
);
3739 debug_printf ("wait_1 ret = %s, %d, %d\n",
3740 target_pid_to_str (ptid_of (current_thread
)),
3741 ourstatus
->kind
, ourstatus
->value
.sig
);
3745 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3746 return filter_exit_event (event_child
, ourstatus
);
3748 return ptid_of (current_thread
);
3751 /* Get rid of any pending event in the pipe. */
3753 async_file_flush (void)
3759 ret
= read (linux_event_pipe
[0], &buf
, 1);
3760 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
3763 /* Put something in the pipe, so the event loop wakes up. */
3765 async_file_mark (void)
3769 async_file_flush ();
3772 ret
= write (linux_event_pipe
[1], "+", 1);
3773 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
3775 /* Ignore EAGAIN. If the pipe is full, the event loop will already
3776 be awakened anyway. */
3780 linux_process_target::wait (ptid_t ptid
,
3781 target_waitstatus
*ourstatus
,
3786 /* Flush the async file first. */
3787 if (target_is_async_p ())
3788 async_file_flush ();
3792 event_ptid
= wait_1 (ptid
, ourstatus
, target_options
);
3794 while ((target_options
& TARGET_WNOHANG
) == 0
3795 && event_ptid
== null_ptid
3796 && ourstatus
->kind
== TARGET_WAITKIND_IGNORE
);
3798 /* If at least one stop was reported, there may be more. A single
3799 SIGCHLD can signal more than one child stop. */
3800 if (target_is_async_p ()
3801 && (target_options
& TARGET_WNOHANG
) != 0
3802 && event_ptid
!= null_ptid
)
3808 /* Send a signal to an LWP. */
3811 kill_lwp (unsigned long lwpid
, int signo
)
3816 ret
= syscall (__NR_tkill
, lwpid
, signo
);
3817 if (errno
== ENOSYS
)
3819 /* If tkill fails, then we are not using nptl threads, a
3820 configuration we no longer support. */
3821 perror_with_name (("tkill"));
3827 linux_stop_lwp (struct lwp_info
*lwp
)
3833 send_sigstop (struct lwp_info
*lwp
)
3837 pid
= lwpid_of (get_lwp_thread (lwp
));
3839 /* If we already have a pending stop signal for this process, don't
3841 if (lwp
->stop_expected
)
3844 debug_printf ("Have pending sigstop for lwp %d\n", pid
);
3850 debug_printf ("Sending sigstop to lwp %d\n", pid
);
3852 lwp
->stop_expected
= 1;
3853 kill_lwp (pid
, SIGSTOP
);
3857 send_sigstop (thread_info
*thread
, lwp_info
*except
)
3859 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3861 /* Ignore EXCEPT. */
3871 /* Increment the suspend count of an LWP, and stop it, if not stopped
3874 suspend_and_send_sigstop (thread_info
*thread
, lwp_info
*except
)
3876 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3878 /* Ignore EXCEPT. */
3882 lwp_suspended_inc (lwp
);
3884 send_sigstop (thread
, except
);
3888 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
3890 /* Store the exit status for later. */
3891 lwp
->status_pending_p
= 1;
3892 lwp
->status_pending
= wstat
;
3894 /* Store in waitstatus as well, as there's nothing else to process
3896 if (WIFEXITED (wstat
))
3898 lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXITED
;
3899 lwp
->waitstatus
.value
.integer
= WEXITSTATUS (wstat
);
3901 else if (WIFSIGNALED (wstat
))
3903 lwp
->waitstatus
.kind
= TARGET_WAITKIND_SIGNALLED
;
3904 lwp
->waitstatus
.value
.sig
= gdb_signal_from_host (WTERMSIG (wstat
));
3907 /* Prevent trying to stop it. */
3910 /* No further stops are expected from a dead lwp. */
3911 lwp
->stop_expected
= 0;
3914 /* Return true if LWP has exited already, and has a pending exit event
3915 to report to GDB. */
3918 lwp_is_marked_dead (struct lwp_info
*lwp
)
3920 return (lwp
->status_pending_p
3921 && (WIFEXITED (lwp
->status_pending
)
3922 || WIFSIGNALED (lwp
->status_pending
)));
3926 linux_process_target::wait_for_sigstop ()
3928 struct thread_info
*saved_thread
;
3933 saved_thread
= current_thread
;
3934 if (saved_thread
!= NULL
)
3935 saved_tid
= saved_thread
->id
;
3937 saved_tid
= null_ptid
; /* avoid bogus unused warning */
3940 debug_printf ("wait_for_sigstop: pulling events\n");
3942 /* Passing NULL_PTID as filter indicates we want all events to be
3943 left pending. Eventually this returns when there are no
3944 unwaited-for children left. */
3945 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
, __WALL
);
3946 gdb_assert (ret
== -1);
3948 if (saved_thread
== NULL
|| mythread_alive (saved_tid
))
3949 current_thread
= saved_thread
;
3953 debug_printf ("Previously current thread died.\n");
3955 /* We can't change the current inferior behind GDB's back,
3956 otherwise, a subsequent command may apply to the wrong
3958 current_thread
= NULL
;
3962 /* Returns true if THREAD is stopped in a jump pad, and we can't
3963 move it out, because we need to report the stop event to GDB. For
3964 example, if the user puts a breakpoint in the jump pad, it's
3965 because she wants to debug it. */
3968 stuck_in_jump_pad_callback (thread_info
*thread
)
3970 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3972 if (lwp
->suspended
!= 0)
3974 internal_error (__FILE__
, __LINE__
,
3975 "LWP %ld is suspended, suspended=%d\n",
3976 lwpid_of (thread
), lwp
->suspended
);
3978 gdb_assert (lwp
->stopped
);
3980 /* Allow debugging the jump pad, gdb_collect, etc.. */
3981 return (supports_fast_tracepoints ()
3982 && agent_loaded_p ()
3983 && (gdb_breakpoint_here (lwp
->stop_pc
)
3984 || lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3985 || thread
->last_resume_kind
== resume_step
)
3986 && (linux_fast_tracepoint_collecting (lwp
, NULL
)
3987 != fast_tpoint_collect_result::not_collecting
));
3991 linux_process_target::move_out_of_jump_pad (thread_info
*thread
)
3993 struct thread_info
*saved_thread
;
3994 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3997 if (lwp
->suspended
!= 0)
3999 internal_error (__FILE__
, __LINE__
,
4000 "LWP %ld is suspended, suspended=%d\n",
4001 lwpid_of (thread
), lwp
->suspended
);
4003 gdb_assert (lwp
->stopped
);
4005 /* For gdb_breakpoint_here. */
4006 saved_thread
= current_thread
;
4007 current_thread
= thread
;
4009 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
4011 /* Allow debugging the jump pad, gdb_collect, etc. */
4012 if (!gdb_breakpoint_here (lwp
->stop_pc
)
4013 && lwp
->stop_reason
!= TARGET_STOPPED_BY_WATCHPOINT
4014 && thread
->last_resume_kind
!= resume_step
4015 && maybe_move_out_of_jump_pad (lwp
, wstat
))
4018 debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
4023 lwp
->status_pending_p
= 0;
4024 enqueue_one_deferred_signal (lwp
, wstat
);
4027 debug_printf ("Signal %d for LWP %ld deferred "
4029 WSTOPSIG (*wstat
), lwpid_of (thread
));
4032 resume_one_lwp (lwp
, 0, 0, NULL
);
4035 lwp_suspended_inc (lwp
);
4037 current_thread
= saved_thread
;
4041 lwp_running (thread_info
*thread
)
4043 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4045 if (lwp_is_marked_dead (lwp
))
4048 return !lwp
->stopped
;
4052 linux_process_target::stop_all_lwps (int suspend
, lwp_info
*except
)
4054 /* Should not be called recursively. */
4055 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
4060 debug_printf ("stop_all_lwps (%s, except=%s)\n",
4061 suspend
? "stop-and-suspend" : "stop",
4063 ? target_pid_to_str (ptid_of (get_lwp_thread (except
)))
4067 stopping_threads
= (suspend
4068 ? STOPPING_AND_SUSPENDING_THREADS
4069 : STOPPING_THREADS
);
4072 for_each_thread ([&] (thread_info
*thread
)
4074 suspend_and_send_sigstop (thread
, except
);
4077 for_each_thread ([&] (thread_info
*thread
)
4079 send_sigstop (thread
, except
);
4082 wait_for_sigstop ();
4083 stopping_threads
= NOT_STOPPING_THREADS
;
4087 debug_printf ("stop_all_lwps done, setting stopping_threads "
4088 "back to !stopping\n");
4093 /* Enqueue one signal in the chain of signals which need to be
4094 delivered to this process on next resume. */
4097 enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
)
4099 struct pending_signals
*p_sig
= XNEW (struct pending_signals
);
4101 p_sig
->prev
= lwp
->pending_signals
;
4102 p_sig
->signal
= signal
;
4104 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
4106 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
4107 lwp
->pending_signals
= p_sig
;
4111 linux_process_target::install_software_single_step_breakpoints (lwp_info
*lwp
)
4113 struct thread_info
*thread
= get_lwp_thread (lwp
);
4114 struct regcache
*regcache
= get_thread_regcache (thread
, 1);
4116 scoped_restore save_current_thread
= make_scoped_restore (¤t_thread
);
4118 current_thread
= thread
;
4119 std::vector
<CORE_ADDR
> next_pcs
= the_low_target
.get_next_pcs (regcache
);
4121 for (CORE_ADDR pc
: next_pcs
)
4122 set_single_step_breakpoint (pc
, current_ptid
);
4126 linux_process_target::single_step (lwp_info
* lwp
)
4130 if (can_hardware_single_step ())
4134 else if (can_software_single_step ())
4136 install_software_single_step_breakpoints (lwp
);
4142 debug_printf ("stepping is not implemented on this target");
4148 /* The signal can be delivered to the inferior if we are not trying to
4149 finish a fast tracepoint collect. Since signal can be delivered in
4150 the step-over, the program may go to signal handler and trap again
4151 after return from the signal handler. We can live with the spurious
4155 lwp_signal_can_be_delivered (struct lwp_info
*lwp
)
4157 return (lwp
->collecting_fast_tracepoint
4158 == fast_tpoint_collect_result::not_collecting
);
4162 linux_process_target::resume_one_lwp_throw (lwp_info
*lwp
, int step
,
4163 int signal
, siginfo_t
*info
)
4165 struct thread_info
*thread
= get_lwp_thread (lwp
);
4166 struct thread_info
*saved_thread
;
4168 struct process_info
*proc
= get_thread_process (thread
);
4170 /* Note that target description may not be initialised
4171 (proc->tdesc == NULL) at this point because the program hasn't
4172 stopped at the first instruction yet. It means GDBserver skips
4173 the extra traps from the wrapper program (see option --wrapper).
4174 Code in this function that requires register access should be
4175 guarded by proc->tdesc == NULL or something else. */
4177 if (lwp
->stopped
== 0)
4180 gdb_assert (lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
4182 fast_tpoint_collect_result fast_tp_collecting
4183 = lwp
->collecting_fast_tracepoint
;
4185 gdb_assert (!stabilizing_threads
4186 || (fast_tp_collecting
4187 != fast_tpoint_collect_result::not_collecting
));
4189 /* Cancel actions that rely on GDB not changing the PC (e.g., the
4190 user used the "jump" command, or "set $pc = foo"). */
4191 if (thread
->while_stepping
!= NULL
&& lwp
->stop_pc
!= get_pc (lwp
))
4193 /* Collecting 'while-stepping' actions doesn't make sense
4195 release_while_stepping_state_list (thread
);
4198 /* If we have pending signals or status, and a new signal, enqueue the
4199 signal. Also enqueue the signal if it can't be delivered to the
4200 inferior right now. */
4202 && (lwp
->status_pending_p
4203 || lwp
->pending_signals
!= NULL
4204 || !lwp_signal_can_be_delivered (lwp
)))
4206 enqueue_pending_signal (lwp
, signal
, info
);
4208 /* Postpone any pending signal. It was enqueued above. */
4212 if (lwp
->status_pending_p
)
4215 debug_printf ("Not resuming lwp %ld (%s, stop %s);"
4216 " has pending status\n",
4217 lwpid_of (thread
), step
? "step" : "continue",
4218 lwp
->stop_expected
? "expected" : "not expected");
4222 saved_thread
= current_thread
;
4223 current_thread
= thread
;
4225 /* This bit needs some thinking about. If we get a signal that
4226 we must report while a single-step reinsert is still pending,
4227 we often end up resuming the thread. It might be better to
4228 (ew) allow a stack of pending events; then we could be sure that
4229 the reinsert happened right away and not lose any signals.
4231 Making this stack would also shrink the window in which breakpoints are
4232 uninserted (see comment in linux_wait_for_lwp) but not enough for
4233 complete correctness, so it won't solve that problem. It may be
4234 worthwhile just to solve this one, however. */
4235 if (lwp
->bp_reinsert
!= 0)
4238 debug_printf (" pending reinsert at 0x%s\n",
4239 paddress (lwp
->bp_reinsert
));
4241 if (can_hardware_single_step ())
4243 if (fast_tp_collecting
== fast_tpoint_collect_result::not_collecting
)
4246 warning ("BAD - reinserting but not stepping.");
4248 warning ("BAD - reinserting and suspended(%d).",
4253 step
= maybe_hw_step (thread
);
4256 if (fast_tp_collecting
== fast_tpoint_collect_result::before_insn
)
4259 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4260 " (exit-jump-pad-bkpt)\n",
4263 else if (fast_tp_collecting
== fast_tpoint_collect_result::at_insn
)
4266 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4267 " single-stepping\n",
4270 if (can_hardware_single_step ())
4274 internal_error (__FILE__
, __LINE__
,
4275 "moving out of jump pad single-stepping"
4276 " not implemented on this target");
4280 /* If we have while-stepping actions in this thread set it stepping.
4281 If we have a signal to deliver, it may or may not be set to
4282 SIG_IGN, we don't know. Assume so, and allow collecting
4283 while-stepping into a signal handler. A possible smart thing to
4284 do would be to set an internal breakpoint at the signal return
4285 address, continue, and carry on catching this while-stepping
4286 action only when that breakpoint is hit. A future
4288 if (thread
->while_stepping
!= NULL
)
4291 debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
4294 step
= single_step (lwp
);
4297 if (proc
->tdesc
!= NULL
&& low_supports_breakpoints ())
4299 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
4301 lwp
->stop_pc
= low_get_pc (regcache
);
4305 debug_printf (" %s from pc 0x%lx\n", step
? "step" : "continue",
4306 (long) lwp
->stop_pc
);
4310 /* If we have pending signals, consume one if it can be delivered to
4312 if (lwp
->pending_signals
!= NULL
&& lwp_signal_can_be_delivered (lwp
))
4314 struct pending_signals
**p_sig
;
4316 p_sig
= &lwp
->pending_signals
;
4317 while ((*p_sig
)->prev
!= NULL
)
4318 p_sig
= &(*p_sig
)->prev
;
4320 signal
= (*p_sig
)->signal
;
4321 if ((*p_sig
)->info
.si_signo
!= 0)
4322 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
4330 debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
4331 lwpid_of (thread
), step
? "step" : "continue", signal
,
4332 lwp
->stop_expected
? "expected" : "not expected");
4334 if (the_low_target
.prepare_to_resume
!= NULL
)
4335 the_low_target
.prepare_to_resume (lwp
);
4337 regcache_invalidate_thread (thread
);
4339 lwp
->stepping
= step
;
4341 ptrace_request
= PTRACE_SINGLESTEP
;
4342 else if (gdb_catching_syscalls_p (lwp
))
4343 ptrace_request
= PTRACE_SYSCALL
;
4345 ptrace_request
= PTRACE_CONT
;
4346 ptrace (ptrace_request
,
4348 (PTRACE_TYPE_ARG3
) 0,
4349 /* Coerce to a uintptr_t first to avoid potential gcc warning
4350 of coercing an 8 byte integer to a 4 byte pointer. */
4351 (PTRACE_TYPE_ARG4
) (uintptr_t) signal
);
4353 current_thread
= saved_thread
;
4355 perror_with_name ("resuming thread");
4357 /* Successfully resumed. Clear state that no longer makes sense,
4358 and mark the LWP as running. Must not do this before resuming
4359 otherwise if that fails other code will be confused. E.g., we'd
4360 later try to stop the LWP and hang forever waiting for a stop
4361 status. Note that we must not throw after this is cleared,
4362 otherwise handle_zombie_lwp_error would get confused. */
4364 lwp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4367 /* Called when we try to resume a stopped LWP and that errors out. If
4368 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4369 or about to become), discard the error, clear any pending status
4370 the LWP may have, and return true (we'll collect the exit status
4371 soon enough). Otherwise, return false. */
4374 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
4376 struct thread_info
*thread
= get_lwp_thread (lp
);
4378 /* If we get an error after resuming the LWP successfully, we'd
4379 confuse !T state for the LWP being gone. */
4380 gdb_assert (lp
->stopped
);
4382 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4383 because even if ptrace failed with ESRCH, the tracee may be "not
4384 yet fully dead", but already refusing ptrace requests. In that
4385 case the tracee has 'R (Running)' state for a little bit
4386 (observed in Linux 3.18). See also the note on ESRCH in the
4387 ptrace(2) man page. Instead, check whether the LWP has any state
4388 other than ptrace-stopped. */
4390 /* Don't assume anything if /proc/PID/status can't be read. */
4391 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread
)) == 0)
4393 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4394 lp
->status_pending_p
= 0;
4401 linux_process_target::resume_one_lwp (lwp_info
*lwp
, int step
, int signal
,
4406 resume_one_lwp_throw (lwp
, step
, signal
, info
);
4408 catch (const gdb_exception_error
&ex
)
4410 if (!check_ptrace_stopped_lwp_gone (lwp
))
4415 /* This function is called once per thread via for_each_thread.
4416 We look up which resume request applies to THREAD and mark it with a
4417 pointer to the appropriate resume request.
4419 This algorithm is O(threads * resume elements), but resume elements
4420 is small (and will remain small at least until GDB supports thread
4424 linux_set_resume_request (thread_info
*thread
, thread_resume
*resume
, size_t n
)
4426 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4428 for (int ndx
= 0; ndx
< n
; ndx
++)
4430 ptid_t ptid
= resume
[ndx
].thread
;
4431 if (ptid
== minus_one_ptid
4432 || ptid
== thread
->id
4433 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4435 || (ptid
.pid () == pid_of (thread
)
4437 || ptid
.lwp () == -1)))
4439 if (resume
[ndx
].kind
== resume_stop
4440 && thread
->last_resume_kind
== resume_stop
)
4443 debug_printf ("already %s LWP %ld at GDB's request\n",
4444 (thread
->last_status
.kind
4445 == TARGET_WAITKIND_STOPPED
)
4453 /* Ignore (wildcard) resume requests for already-resumed
4455 if (resume
[ndx
].kind
!= resume_stop
4456 && thread
->last_resume_kind
!= resume_stop
)
4459 debug_printf ("already %s LWP %ld at GDB's request\n",
4460 (thread
->last_resume_kind
4468 /* Don't let wildcard resumes resume fork children that GDB
4469 does not yet know are new fork children. */
4470 if (lwp
->fork_relative
!= NULL
)
4472 struct lwp_info
*rel
= lwp
->fork_relative
;
4474 if (rel
->status_pending_p
4475 && (rel
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
4476 || rel
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
))
4479 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4485 /* If the thread has a pending event that has already been
4486 reported to GDBserver core, but GDB has not pulled the
4487 event out of the vStopped queue yet, likewise, ignore the
4488 (wildcard) resume request. */
4489 if (in_queued_stop_replies (thread
->id
))
4492 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4497 lwp
->resume
= &resume
[ndx
];
4498 thread
->last_resume_kind
= lwp
->resume
->kind
;
4500 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
4501 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
4503 /* If we had a deferred signal to report, dequeue one now.
4504 This can happen if LWP gets more than one signal while
4505 trying to get out of a jump pad. */
4507 && !lwp
->status_pending_p
4508 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
4510 lwp
->status_pending_p
= 1;
4513 debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
4514 "leaving status pending.\n",
4515 WSTOPSIG (lwp
->status_pending
),
4523 /* No resume action for this thread. */
4528 linux_process_target::resume_status_pending (thread_info
*thread
)
4530 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4532 /* LWPs which will not be resumed are not interesting, because
4533 we might not wait for them next time through linux_wait. */
4534 if (lwp
->resume
== NULL
)
4537 return thread_still_has_status_pending (thread
);
4541 linux_process_target::thread_needs_step_over (thread_info
*thread
)
4543 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4544 struct thread_info
*saved_thread
;
4546 struct process_info
*proc
= get_thread_process (thread
);
4548 /* GDBserver is skipping the extra traps from the wrapper program,
4549 don't have to do step over. */
4550 if (proc
->tdesc
== NULL
)
4553 /* LWPs which will not be resumed are not interesting, because we
4554 might not wait for them next time through linux_wait. */
4559 debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
4564 if (thread
->last_resume_kind
== resume_stop
)
4567 debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
4573 gdb_assert (lwp
->suspended
>= 0);
4578 debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
4583 if (lwp
->status_pending_p
)
4586 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4592 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4596 /* If the PC has changed since we stopped, then don't do anything,
4597 and let the breakpoint/tracepoint be hit. This happens if, for
4598 instance, GDB handled the decr_pc_after_break subtraction itself,
4599 GDB is OOL stepping this thread, or the user has issued a "jump"
4600 command, or poked thread's registers herself. */
4601 if (pc
!= lwp
->stop_pc
)
4604 debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4605 "Old stop_pc was 0x%s, PC is now 0x%s\n",
4607 paddress (lwp
->stop_pc
), paddress (pc
));
4611 /* On software single step target, resume the inferior with signal
4612 rather than stepping over. */
4613 if (can_software_single_step ()
4614 && lwp
->pending_signals
!= NULL
4615 && lwp_signal_can_be_delivered (lwp
))
4618 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4625 saved_thread
= current_thread
;
4626 current_thread
= thread
;
4628 /* We can only step over breakpoints we know about. */
4629 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
4631 /* Don't step over a breakpoint that GDB expects to hit
4632 though. If the condition is being evaluated on the target's side
4633 and it evaluate to false, step over this breakpoint as well. */
4634 if (gdb_breakpoint_here (pc
)
4635 && gdb_condition_true_at_breakpoint (pc
)
4636 && gdb_no_commands_at_breakpoint (pc
))
4639 debug_printf ("Need step over [LWP %ld]? yes, but found"
4640 " GDB breakpoint at 0x%s; skipping step over\n",
4641 lwpid_of (thread
), paddress (pc
));
4643 current_thread
= saved_thread
;
4649 debug_printf ("Need step over [LWP %ld]? yes, "
4650 "found breakpoint at 0x%s\n",
4651 lwpid_of (thread
), paddress (pc
));
4653 /* We've found an lwp that needs stepping over --- return 1 so
4654 that find_thread stops looking. */
4655 current_thread
= saved_thread
;
4661 current_thread
= saved_thread
;
4664 debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
4666 lwpid_of (thread
), paddress (pc
));
4672 linux_process_target::start_step_over (lwp_info
*lwp
)
4674 struct thread_info
*thread
= get_lwp_thread (lwp
);
4675 struct thread_info
*saved_thread
;
4680 debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
4683 stop_all_lwps (1, lwp
);
4685 if (lwp
->suspended
!= 0)
4687 internal_error (__FILE__
, __LINE__
,
4688 "LWP %ld suspended=%d\n", lwpid_of (thread
),
4693 debug_printf ("Done stopping all threads for step-over.\n");
4695 /* Note, we should always reach here with an already adjusted PC,
4696 either by GDB (if we're resuming due to GDB's request), or by our
4697 caller, if we just finished handling an internal breakpoint GDB
4698 shouldn't care about. */
4701 saved_thread
= current_thread
;
4702 current_thread
= thread
;
4704 lwp
->bp_reinsert
= pc
;
4705 uninsert_breakpoints_at (pc
);
4706 uninsert_fast_tracepoint_jumps_at (pc
);
4708 step
= single_step (lwp
);
4710 current_thread
= saved_thread
;
4712 resume_one_lwp (lwp
, step
, 0, NULL
);
4714 /* Require next event from this LWP. */
4715 step_over_bkpt
= thread
->id
;
4718 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
4719 start_step_over, if still there, and delete any single-step
4720 breakpoints we've set, on non hardware single-step targets. */
4723 finish_step_over (struct lwp_info
*lwp
)
4725 if (lwp
->bp_reinsert
!= 0)
4727 struct thread_info
*saved_thread
= current_thread
;
4730 debug_printf ("Finished step over.\n");
4732 current_thread
= get_lwp_thread (lwp
);
4734 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4735 may be no breakpoint to reinsert there by now. */
4736 reinsert_breakpoints_at (lwp
->bp_reinsert
);
4737 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
4739 lwp
->bp_reinsert
= 0;
4741 /* Delete any single-step breakpoints. No longer needed. We
4742 don't have to worry about other threads hitting this trap,
4743 and later not being able to explain it, because we were
4744 stepping over a breakpoint, and we hold all threads but
4745 LWP stopped while doing that. */
4746 if (!can_hardware_single_step ())
4748 gdb_assert (has_single_step_breakpoints (current_thread
));
4749 delete_single_step_breakpoints (current_thread
);
4752 step_over_bkpt
= null_ptid
;
4753 current_thread
= saved_thread
;
4761 linux_process_target::complete_ongoing_step_over ()
4763 if (step_over_bkpt
!= null_ptid
)
4765 struct lwp_info
*lwp
;
4770 debug_printf ("detach: step over in progress, finish it first\n");
4772 /* Passing NULL_PTID as filter indicates we want all events to
4773 be left pending. Eventually this returns when there are no
4774 unwaited-for children left. */
4775 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
,
4777 gdb_assert (ret
== -1);
4779 lwp
= find_lwp_pid (step_over_bkpt
);
4781 finish_step_over (lwp
);
4782 step_over_bkpt
= null_ptid
;
4783 unsuspend_all_lwps (lwp
);
4788 linux_process_target::resume_one_thread (thread_info
*thread
,
4789 bool leave_all_stopped
)
4791 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4794 if (lwp
->resume
== NULL
)
4797 if (lwp
->resume
->kind
== resume_stop
)
4800 debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread
));
4805 debug_printf ("stopping LWP %ld\n", lwpid_of (thread
));
4807 /* Stop the thread, and wait for the event asynchronously,
4808 through the event loop. */
4814 debug_printf ("already stopped LWP %ld\n",
4817 /* The LWP may have been stopped in an internal event that
4818 was not meant to be notified back to GDB (e.g., gdbserver
4819 breakpoint), so we should be reporting a stop event in
4822 /* If the thread already has a pending SIGSTOP, this is a
4823 no-op. Otherwise, something later will presumably resume
4824 the thread and this will cause it to cancel any pending
4825 operation, due to last_resume_kind == resume_stop. If
4826 the thread already has a pending status to report, we
4827 will still report it the next time we wait - see
4828 status_pending_p_callback. */
4830 /* If we already have a pending signal to report, then
4831 there's no need to queue a SIGSTOP, as this means we're
4832 midway through moving the LWP out of the jumppad, and we
4833 will report the pending signal as soon as that is
4835 if (lwp
->pending_signals_to_report
== NULL
)
4839 /* For stop requests, we're done. */
4841 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4845 /* If this thread which is about to be resumed has a pending status,
4846 then don't resume it - we can just report the pending status.
4847 Likewise if it is suspended, because e.g., another thread is
4848 stepping past a breakpoint. Make sure to queue any signals that
4849 would otherwise be sent. In all-stop mode, we do this decision
4850 based on if *any* thread has a pending status. If there's a
4851 thread that needs the step-over-breakpoint dance, then don't
4852 resume any other thread but that particular one. */
4853 leave_pending
= (lwp
->suspended
4854 || lwp
->status_pending_p
4855 || leave_all_stopped
);
4857 /* If we have a new signal, enqueue the signal. */
4858 if (lwp
->resume
->sig
!= 0)
4860 siginfo_t info
, *info_p
;
4862 /* If this is the same signal we were previously stopped by,
4863 make sure to queue its siginfo. */
4864 if (WIFSTOPPED (lwp
->last_status
)
4865 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
4866 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
),
4867 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
4872 enqueue_pending_signal (lwp
, lwp
->resume
->sig
, info_p
);
4878 debug_printf ("resuming LWP %ld\n", lwpid_of (thread
));
4880 proceed_one_lwp (thread
, NULL
);
4885 debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread
));
4888 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4893 linux_process_target::resume (thread_resume
*resume_info
, size_t n
)
4895 struct thread_info
*need_step_over
= NULL
;
4900 debug_printf ("linux_resume:\n");
4903 for_each_thread ([&] (thread_info
*thread
)
4905 linux_set_resume_request (thread
, resume_info
, n
);
4908 /* If there is a thread which would otherwise be resumed, which has
4909 a pending status, then don't resume any threads - we can just
4910 report the pending status. Make sure to queue any signals that
4911 would otherwise be sent. In non-stop mode, we'll apply this
4912 logic to each thread individually. We consume all pending events
4913 before considering to start a step-over (in all-stop). */
4914 bool any_pending
= false;
4916 any_pending
= find_thread ([this] (thread_info
*thread
)
4918 return resume_status_pending (thread
);
4921 /* If there is a thread which would otherwise be resumed, which is
4922 stopped at a breakpoint that needs stepping over, then don't
4923 resume any threads - have it step over the breakpoint with all
4924 other threads stopped, then resume all threads again. Make sure
4925 to queue any signals that would otherwise be delivered or
4927 if (!any_pending
&& low_supports_breakpoints ())
4928 need_step_over
= find_thread ([this] (thread_info
*thread
)
4930 return thread_needs_step_over (thread
);
4933 bool leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
4937 if (need_step_over
!= NULL
)
4938 debug_printf ("Not resuming all, need step over\n");
4939 else if (any_pending
)
4940 debug_printf ("Not resuming, all-stop and found "
4941 "an LWP with pending status\n");
4943 debug_printf ("Resuming, no pending status or step over needed\n");
4946 /* Even if we're leaving threads stopped, queue all signals we'd
4947 otherwise deliver. */
4948 for_each_thread ([&] (thread_info
*thread
)
4950 resume_one_thread (thread
, leave_all_stopped
);
4954 start_step_over (get_thread_lwp (need_step_over
));
4958 debug_printf ("linux_resume done\n");
4962 /* We may have events that were pending that can/should be sent to
4963 the client now. Trigger a linux_wait call. */
4964 if (target_is_async_p ())
4969 linux_process_target::proceed_one_lwp (thread_info
*thread
, lwp_info
*except
)
4971 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4978 debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread
));
4983 debug_printf (" LWP %ld already running\n", lwpid_of (thread
));
4987 if (thread
->last_resume_kind
== resume_stop
4988 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4991 debug_printf (" client wants LWP to remain %ld stopped\n",
4996 if (lwp
->status_pending_p
)
4999 debug_printf (" LWP %ld has pending status, leaving stopped\n",
5004 gdb_assert (lwp
->suspended
>= 0);
5009 debug_printf (" LWP %ld is suspended\n", lwpid_of (thread
));
5013 if (thread
->last_resume_kind
== resume_stop
5014 && lwp
->pending_signals_to_report
== NULL
5015 && (lwp
->collecting_fast_tracepoint
5016 == fast_tpoint_collect_result::not_collecting
))
5018 /* We haven't reported this LWP as stopped yet (otherwise, the
5019 last_status.kind check above would catch it, and we wouldn't
5020 reach here. This LWP may have been momentarily paused by a
5021 stop_all_lwps call while handling for example, another LWP's
5022 step-over. In that case, the pending expected SIGSTOP signal
5023 that was queued at vCont;t handling time will have already
5024 been consumed by wait_for_sigstop, and so we need to requeue
5025 another one here. Note that if the LWP already has a SIGSTOP
5026 pending, this is a no-op. */
5029 debug_printf ("Client wants LWP %ld to stop. "
5030 "Making sure it has a SIGSTOP pending\n",
5036 if (thread
->last_resume_kind
== resume_step
)
5039 debug_printf (" stepping LWP %ld, client wants it stepping\n",
5042 /* If resume_step is requested by GDB, install single-step
5043 breakpoints when the thread is about to be actually resumed if
5044 the single-step breakpoints weren't removed. */
5045 if (can_software_single_step ()
5046 && !has_single_step_breakpoints (thread
))
5047 install_software_single_step_breakpoints (lwp
);
5049 step
= maybe_hw_step (thread
);
5051 else if (lwp
->bp_reinsert
!= 0)
5054 debug_printf (" stepping LWP %ld, reinsert set\n",
5057 step
= maybe_hw_step (thread
);
5062 resume_one_lwp (lwp
, step
, 0, NULL
);
5066 linux_process_target::unsuspend_and_proceed_one_lwp (thread_info
*thread
,
5069 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5074 lwp_suspended_decr (lwp
);
5076 proceed_one_lwp (thread
, except
);
5080 linux_process_target::proceed_all_lwps ()
5082 struct thread_info
*need_step_over
;
5084 /* If there is a thread which would otherwise be resumed, which is
5085 stopped at a breakpoint that needs stepping over, then don't
5086 resume any threads - have it step over the breakpoint with all
5087 other threads stopped, then resume all threads again. */
5089 if (low_supports_breakpoints ())
5091 need_step_over
= find_thread ([this] (thread_info
*thread
)
5093 return thread_needs_step_over (thread
);
5096 if (need_step_over
!= NULL
)
5099 debug_printf ("proceed_all_lwps: found "
5100 "thread %ld needing a step-over\n",
5101 lwpid_of (need_step_over
));
5103 start_step_over (get_thread_lwp (need_step_over
));
5109 debug_printf ("Proceeding, no step-over needed\n");
5111 for_each_thread ([this] (thread_info
*thread
)
5113 proceed_one_lwp (thread
, NULL
);
5118 linux_process_target::unstop_all_lwps (int unsuspend
, lwp_info
*except
)
5124 debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
5125 lwpid_of (get_lwp_thread (except
)));
5127 debug_printf ("unstopping all lwps\n");
5131 for_each_thread ([&] (thread_info
*thread
)
5133 unsuspend_and_proceed_one_lwp (thread
, except
);
5136 for_each_thread ([&] (thread_info
*thread
)
5138 proceed_one_lwp (thread
, except
);
5143 debug_printf ("unstop_all_lwps done\n");
5149 #ifdef HAVE_LINUX_REGSETS
5151 #define use_linux_regsets 1
5153 /* Returns true if REGSET has been disabled. */
5156 regset_disabled (struct regsets_info
*info
, struct regset_info
*regset
)
5158 return (info
->disabled_regsets
!= NULL
5159 && info
->disabled_regsets
[regset
- info
->regsets
]);
5162 /* Disable REGSET. */
5165 disable_regset (struct regsets_info
*info
, struct regset_info
*regset
)
5169 dr_offset
= regset
- info
->regsets
;
5170 if (info
->disabled_regsets
== NULL
)
5171 info
->disabled_regsets
= (char *) xcalloc (1, info
->num_regsets
);
5172 info
->disabled_regsets
[dr_offset
] = 1;
5176 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
5177 struct regcache
*regcache
)
5179 struct regset_info
*regset
;
5180 int saw_general_regs
= 0;
5184 pid
= lwpid_of (current_thread
);
5185 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5190 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
5193 buf
= xmalloc (regset
->size
);
5195 nt_type
= regset
->nt_type
;
5199 iov
.iov_len
= regset
->size
;
5200 data
= (void *) &iov
;
5206 res
= ptrace (regset
->get_request
, pid
,
5207 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5209 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5214 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5216 /* If we get EIO on a regset, or an EINVAL and the regset is
5217 optional, do not try it again for this process mode. */
5218 disable_regset (regsets_info
, regset
);
5220 else if (errno
== ENODATA
)
5222 /* ENODATA may be returned if the regset is currently
5223 not "active". This can happen in normal operation,
5224 so suppress the warning in this case. */
5226 else if (errno
== ESRCH
)
5228 /* At this point, ESRCH should mean the process is
5229 already gone, in which case we simply ignore attempts
5230 to read its registers. */
5235 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
5242 if (regset
->type
== GENERAL_REGS
)
5243 saw_general_regs
= 1;
5244 regset
->store_function (regcache
, buf
);
5248 if (saw_general_regs
)
5255 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
5256 struct regcache
*regcache
)
5258 struct regset_info
*regset
;
5259 int saw_general_regs
= 0;
5263 pid
= lwpid_of (current_thread
);
5264 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5269 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
)
5270 || regset
->fill_function
== NULL
)
5273 buf
= xmalloc (regset
->size
);
5275 /* First fill the buffer with the current register set contents,
5276 in case there are any items in the kernel's regset that are
5277 not in gdbserver's regcache. */
5279 nt_type
= regset
->nt_type
;
5283 iov
.iov_len
= regset
->size
;
5284 data
= (void *) &iov
;
5290 res
= ptrace (regset
->get_request
, pid
,
5291 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5293 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5298 /* Then overlay our cached registers on that. */
5299 regset
->fill_function (regcache
, buf
);
5301 /* Only now do we write the register set. */
5303 res
= ptrace (regset
->set_request
, pid
,
5304 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5306 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
5313 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5315 /* If we get EIO on a regset, or an EINVAL and the regset is
5316 optional, do not try it again for this process mode. */
5317 disable_regset (regsets_info
, regset
);
5319 else if (errno
== ESRCH
)
5321 /* At this point, ESRCH should mean the process is
5322 already gone, in which case we simply ignore attempts
5323 to change its registers. See also the related
5324 comment in resume_one_lwp. */
5330 perror ("Warning: ptrace(regsets_store_inferior_registers)");
5333 else if (regset
->type
== GENERAL_REGS
)
5334 saw_general_regs
= 1;
5337 if (saw_general_regs
)
5343 #else /* !HAVE_LINUX_REGSETS */
5345 #define use_linux_regsets 0
5346 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5347 #define regsets_store_inferior_registers(regsets_info, regcache) 1
5351 /* Return 1 if register REGNO is supported by one of the regset ptrace
5352 calls or 0 if it has to be transferred individually. */
5355 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
5357 unsigned char mask
= 1 << (regno
% 8);
5358 size_t index
= regno
/ 8;
5360 return (use_linux_regsets
5361 && (regs_info
->regset_bitmap
== NULL
5362 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
5365 #ifdef HAVE_LINUX_USRREGS
5368 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
5372 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
5373 error ("Invalid register number %d.", regnum
);
5375 addr
= usrregs
->regmap
[regnum
];
5382 linux_process_target::fetch_register (const usrregs_info
*usrregs
,
5383 regcache
*regcache
, int regno
)
5390 if (regno
>= usrregs
->num_regs
)
5392 if (low_cannot_fetch_register (regno
))
5395 regaddr
= register_addr (usrregs
, regno
);
5399 size
= ((register_size (regcache
->tdesc
, regno
)
5400 + sizeof (PTRACE_XFER_TYPE
) - 1)
5401 & -sizeof (PTRACE_XFER_TYPE
));
5402 buf
= (char *) alloca (size
);
5404 pid
= lwpid_of (current_thread
);
5405 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5408 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
5409 ptrace (PTRACE_PEEKUSER
, pid
,
5410 /* Coerce to a uintptr_t first to avoid potential gcc warning
5411 of coercing an 8 byte integer to a 4 byte pointer. */
5412 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
, (PTRACE_TYPE_ARG4
) 0);
5413 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5416 /* Mark register REGNO unavailable. */
5417 supply_register (regcache
, regno
, NULL
);
5422 if (the_low_target
.supply_ptrace_register
)
5423 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
5425 supply_register (regcache
, regno
, buf
);
5429 linux_process_target::store_register (const usrregs_info
*usrregs
,
5430 regcache
*regcache
, int regno
)
5437 if (regno
>= usrregs
->num_regs
)
5439 if (low_cannot_store_register (regno
))
5442 regaddr
= register_addr (usrregs
, regno
);
5446 size
= ((register_size (regcache
->tdesc
, regno
)
5447 + sizeof (PTRACE_XFER_TYPE
) - 1)
5448 & -sizeof (PTRACE_XFER_TYPE
));
5449 buf
= (char *) alloca (size
);
5450 memset (buf
, 0, size
);
5452 if (the_low_target
.collect_ptrace_register
)
5453 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
5455 collect_register (regcache
, regno
, buf
);
5457 pid
= lwpid_of (current_thread
);
5458 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5461 ptrace (PTRACE_POKEUSER
, pid
,
5462 /* Coerce to a uintptr_t first to avoid potential gcc warning
5463 about coercing an 8 byte integer to a 4 byte pointer. */
5464 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
,
5465 (PTRACE_TYPE_ARG4
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
5468 /* At this point, ESRCH should mean the process is
5469 already gone, in which case we simply ignore attempts
5470 to change its registers. See also the related
5471 comment in resume_one_lwp. */
5476 if (!low_cannot_store_register (regno
))
5477 error ("writing register %d: %s", regno
, safe_strerror (errno
));
5479 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5482 #endif /* HAVE_LINUX_USRREGS */
5485 linux_process_target::usr_fetch_inferior_registers (const regs_info
*regs_info
,
5489 #ifdef HAVE_LINUX_USRREGS
5490 struct usrregs_info
*usr
= regs_info
->usrregs
;
5494 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5495 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5496 fetch_register (usr
, regcache
, regno
);
5499 fetch_register (usr
, regcache
, regno
);
5504 linux_process_target::usr_store_inferior_registers (const regs_info
*regs_info
,
5508 #ifdef HAVE_LINUX_USRREGS
5509 struct usrregs_info
*usr
= regs_info
->usrregs
;
5513 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5514 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5515 store_register (usr
, regcache
, regno
);
5518 store_register (usr
, regcache
, regno
);
5523 linux_process_target::fetch_registers (regcache
*regcache
, int regno
)
5527 const regs_info
*regs_info
= get_regs_info ();
5531 if (regs_info
->usrregs
!= NULL
)
5532 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
5533 low_fetch_register (regcache
, regno
);
5535 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
5536 if (regs_info
->usrregs
!= NULL
)
5537 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
5541 if (low_fetch_register (regcache
, regno
))
5544 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5546 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
5548 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5549 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
5554 linux_process_target::store_registers (regcache
*regcache
, int regno
)
5558 const regs_info
*regs_info
= get_regs_info ();
5562 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5564 if (regs_info
->usrregs
!= NULL
)
5565 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
5569 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5571 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5573 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5574 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
5579 linux_process_target::low_fetch_register (regcache
*regcache
, int regno
)
5584 /* A wrapper for the read_memory target op. */
5587 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
5589 return the_target
->read_memory (memaddr
, myaddr
, len
);
5592 /* Copy LEN bytes from inferior's memory starting at MEMADDR
5593 to debugger memory starting at MYADDR. */
5596 linux_process_target::read_memory (CORE_ADDR memaddr
,
5597 unsigned char *myaddr
, int len
)
5599 int pid
= lwpid_of (current_thread
);
5600 PTRACE_XFER_TYPE
*buffer
;
5608 /* Try using /proc. Don't bother for one word. */
5609 if (len
>= 3 * sizeof (long))
5613 /* We could keep this file open and cache it - possibly one per
5614 thread. That requires some juggling, but is even faster. */
5615 sprintf (filename
, "/proc/%d/mem", pid
);
5616 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
5620 /* If pread64 is available, use it. It's faster if the kernel
5621 supports it (only one syscall), and it's 64-bit safe even on
5622 32-bit platforms (for instance, SPARC debugging a SPARC64
5625 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
5628 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
5629 bytes
= read (fd
, myaddr
, len
);
5636 /* Some data was read, we'll try to get the rest with ptrace. */
5646 /* Round starting address down to longword boundary. */
5647 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5648 /* Round ending address up; get number of longwords that makes. */
5649 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5650 / sizeof (PTRACE_XFER_TYPE
));
5651 /* Allocate buffer of that many longwords. */
5652 buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5654 /* Read all the longwords */
5656 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5658 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5659 about coercing an 8 byte integer to a 4 byte pointer. */
5660 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
5661 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5662 (PTRACE_TYPE_ARG4
) 0);
5668 /* Copy appropriate bytes out of the buffer. */
5671 i
*= sizeof (PTRACE_XFER_TYPE
);
5672 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
5674 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5681 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5682 memory at MEMADDR. On failure (cannot write to the inferior)
5683 returns the value of errno. Always succeeds if LEN is zero. */
5686 linux_process_target::write_memory (CORE_ADDR memaddr
,
5687 const unsigned char *myaddr
, int len
)
5690 /* Round starting address down to longword boundary. */
5691 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5692 /* Round ending address up; get number of longwords that makes. */
5694 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5695 / sizeof (PTRACE_XFER_TYPE
);
5697 /* Allocate buffer of that many longwords. */
5698 PTRACE_XFER_TYPE
*buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5700 int pid
= lwpid_of (current_thread
);
5704 /* Zero length write always succeeds. */
5710 /* Dump up to four bytes. */
5711 char str
[4 * 2 + 1];
5713 int dump
= len
< 4 ? len
: 4;
5715 for (i
= 0; i
< dump
; i
++)
5717 sprintf (p
, "%02x", myaddr
[i
]);
5722 debug_printf ("Writing %s to 0x%08lx in process %d\n",
5723 str
, (long) memaddr
, pid
);
5726 /* Fill start and end extra bytes of buffer with existing memory data. */
5729 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5730 about coercing an 8 byte integer to a 4 byte pointer. */
5731 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
5732 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5733 (PTRACE_TYPE_ARG4
) 0);
5741 = ptrace (PTRACE_PEEKTEXT
, pid
,
5742 /* Coerce to a uintptr_t first to avoid potential gcc warning
5743 about coercing an 8 byte integer to a 4 byte pointer. */
5744 (PTRACE_TYPE_ARG3
) (uintptr_t) (addr
+ (count
- 1)
5745 * sizeof (PTRACE_XFER_TYPE
)),
5746 (PTRACE_TYPE_ARG4
) 0);
5751 /* Copy data to be written over corresponding part of buffer. */
5753 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5756 /* Write the entire buffer. */
5758 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5761 ptrace (PTRACE_POKETEXT
, pid
,
5762 /* Coerce to a uintptr_t first to avoid potential gcc warning
5763 about coercing an 8 byte integer to a 4 byte pointer. */
5764 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5765 (PTRACE_TYPE_ARG4
) buffer
[i
]);
5774 linux_process_target::look_up_symbols ()
5776 #ifdef USE_THREAD_DB
5777 struct process_info
*proc
= current_process ();
5779 if (proc
->priv
->thread_db
!= NULL
)
5787 linux_process_target::request_interrupt ()
5789 /* Send a SIGINT to the process group. This acts just like the user
5790 typed a ^C on the controlling terminal. */
5791 ::kill (-signal_pid
, SIGINT
);
5795 linux_process_target::supports_read_auxv ()
5800 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5801 to debugger memory starting at MYADDR. */
5804 linux_process_target::read_auxv (CORE_ADDR offset
, unsigned char *myaddr
,
5807 char filename
[PATH_MAX
];
5809 int pid
= lwpid_of (current_thread
);
5811 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5813 fd
= open (filename
, O_RDONLY
);
5817 if (offset
!= (CORE_ADDR
) 0
5818 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5821 n
= read (fd
, myaddr
, len
);
5828 /* These breakpoint and watchpoint related wrapper functions simply
5829 pass on the function call if the target has registered a
5830 corresponding function. */
5833 linux_process_target::supports_z_point_type (char z_type
)
5835 return (the_low_target
.supports_z_point_type
!= NULL
5836 && the_low_target
.supports_z_point_type (z_type
));
5840 linux_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5841 int size
, raw_breakpoint
*bp
)
5843 if (type
== raw_bkpt_type_sw
)
5844 return insert_memory_breakpoint (bp
);
5845 else if (the_low_target
.insert_point
!= NULL
)
5846 return the_low_target
.insert_point (type
, addr
, size
, bp
);
5848 /* Unsupported (see target.h). */
5853 linux_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5854 int size
, raw_breakpoint
*bp
)
5856 if (type
== raw_bkpt_type_sw
)
5857 return remove_memory_breakpoint (bp
);
5858 else if (the_low_target
.remove_point
!= NULL
)
5859 return the_low_target
.remove_point (type
, addr
, size
, bp
);
5861 /* Unsupported (see target.h). */
5865 /* Implement the stopped_by_sw_breakpoint target_ops
5869 linux_process_target::stopped_by_sw_breakpoint ()
5871 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5873 return (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
);
5876 /* Implement the supports_stopped_by_sw_breakpoint target_ops
5880 linux_process_target::supports_stopped_by_sw_breakpoint ()
5882 return USE_SIGTRAP_SIGINFO
;
5885 /* Implement the stopped_by_hw_breakpoint target_ops
5889 linux_process_target::stopped_by_hw_breakpoint ()
5891 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5893 return (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
);
5896 /* Implement the supports_stopped_by_hw_breakpoint target_ops
5900 linux_process_target::supports_stopped_by_hw_breakpoint ()
5902 return USE_SIGTRAP_SIGINFO
;
5905 /* Implement the supports_hardware_single_step target_ops method. */
5908 linux_process_target::supports_hardware_single_step ()
5910 return can_hardware_single_step ();
5914 linux_process_target::supports_software_single_step ()
5916 return can_software_single_step ();
5920 linux_process_target::stopped_by_watchpoint ()
5922 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5924 return lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
5928 linux_process_target::stopped_data_address ()
5930 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5932 return lwp
->stopped_data_address
;
5935 /* This is only used for targets that define PT_TEXT_ADDR,
5936 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5937 the target has different ways of acquiring this information, like
5941 linux_process_target::supports_read_offsets ()
5943 #ifdef SUPPORTS_READ_OFFSETS
5950 /* Under uClinux, programs are loaded at non-zero offsets, which we need
5951 to tell gdb about. */
5954 linux_process_target::read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
5956 #ifdef SUPPORTS_READ_OFFSETS
5957 unsigned long text
, text_end
, data
;
5958 int pid
= lwpid_of (current_thread
);
5962 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_ADDR
,
5963 (PTRACE_TYPE_ARG4
) 0);
5964 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_END_ADDR
,
5965 (PTRACE_TYPE_ARG4
) 0);
5966 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_DATA_ADDR
,
5967 (PTRACE_TYPE_ARG4
) 0);
5971 /* Both text and data offsets produced at compile-time (and so
5972 used by gdb) are relative to the beginning of the program,
5973 with the data segment immediately following the text segment.
5974 However, the actual runtime layout in memory may put the data
5975 somewhere else, so when we send gdb a data base-address, we
5976 use the real data base address and subtract the compile-time
5977 data base-address from it (which is just the length of the
5978 text segment). BSS immediately follows data in both
5981 *data_p
= data
- (text_end
- text
);
5987 gdb_assert_not_reached ("target op read_offsets not supported");
5992 linux_process_target::supports_get_tls_address ()
5994 #ifdef USE_THREAD_DB
6002 linux_process_target::get_tls_address (thread_info
*thread
,
6004 CORE_ADDR load_module
,
6007 #ifdef USE_THREAD_DB
6008 return thread_db_get_tls_address (thread
, offset
, load_module
, address
);
6015 linux_process_target::supports_qxfer_osdata ()
6021 linux_process_target::qxfer_osdata (const char *annex
,
6022 unsigned char *readbuf
,
6023 unsigned const char *writebuf
,
6024 CORE_ADDR offset
, int len
)
6026 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
6029 /* Convert a native/host siginfo object, into/from the siginfo in the
6030 layout of the inferiors' architecture. */
6033 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
6037 if (the_low_target
.siginfo_fixup
!= NULL
)
6038 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
6040 /* If there was no callback, or the callback didn't do anything,
6041 then just do a straight memcpy. */
6045 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
6047 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
6052 linux_process_target::supports_qxfer_siginfo ()
6058 linux_process_target::qxfer_siginfo (const char *annex
,
6059 unsigned char *readbuf
,
6060 unsigned const char *writebuf
,
6061 CORE_ADDR offset
, int len
)
6065 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
6067 if (current_thread
== NULL
)
6070 pid
= lwpid_of (current_thread
);
6073 debug_printf ("%s siginfo for lwp %d.\n",
6074 readbuf
!= NULL
? "Reading" : "Writing",
6077 if (offset
>= sizeof (siginfo
))
6080 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6083 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
6084 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
6085 inferior with a 64-bit GDBSERVER should look the same as debugging it
6086 with a 32-bit GDBSERVER, we need to convert it. */
6087 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
6089 if (offset
+ len
> sizeof (siginfo
))
6090 len
= sizeof (siginfo
) - offset
;
6092 if (readbuf
!= NULL
)
6093 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
6096 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
6098 /* Convert back to ptrace layout before flushing it out. */
6099 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
6101 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6108 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
6109 so we notice when children change state; as the handler for the
6110 sigsuspend in my_waitpid. */
6113 sigchld_handler (int signo
)
6115 int old_errno
= errno
;
6121 /* Use the async signal safe debug function. */
6122 if (debug_write ("sigchld_handler\n",
6123 sizeof ("sigchld_handler\n") - 1) < 0)
6124 break; /* just ignore */
6128 if (target_is_async_p ())
6129 async_file_mark (); /* trigger a linux_wait */
6135 linux_process_target::supports_non_stop ()
6141 linux_process_target::async (bool enable
)
6143 bool previous
= target_is_async_p ();
6146 debug_printf ("linux_async (%d), previous=%d\n",
6149 if (previous
!= enable
)
6152 sigemptyset (&mask
);
6153 sigaddset (&mask
, SIGCHLD
);
6155 gdb_sigmask (SIG_BLOCK
, &mask
, NULL
);
6159 if (pipe (linux_event_pipe
) == -1)
6161 linux_event_pipe
[0] = -1;
6162 linux_event_pipe
[1] = -1;
6163 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6165 warning ("creating event pipe failed.");
6169 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
6170 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
6172 /* Register the event loop handler. */
6173 add_file_handler (linux_event_pipe
[0],
6174 handle_target_event
, NULL
);
6176 /* Always trigger a linux_wait. */
6181 delete_file_handler (linux_event_pipe
[0]);
6183 close (linux_event_pipe
[0]);
6184 close (linux_event_pipe
[1]);
6185 linux_event_pipe
[0] = -1;
6186 linux_event_pipe
[1] = -1;
6189 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6196 linux_process_target::start_non_stop (bool nonstop
)
6198 /* Register or unregister from event-loop accordingly. */
6199 target_async (nonstop
);
6201 if (target_is_async_p () != (nonstop
!= false))
6208 linux_process_target::supports_multi_process ()
6213 /* Check if fork events are supported. */
6216 linux_process_target::supports_fork_events ()
6218 return linux_supports_tracefork ();
6221 /* Check if vfork events are supported. */
6224 linux_process_target::supports_vfork_events ()
6226 return linux_supports_tracefork ();
6229 /* Check if exec events are supported. */
6232 linux_process_target::supports_exec_events ()
6234 return linux_supports_traceexec ();
6237 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
6238 ptrace flags for all inferiors. This is in case the new GDB connection
6239 doesn't support the same set of events that the previous one did. */
6242 linux_process_target::handle_new_gdb_connection ()
6244 /* Request that all the lwps reset their ptrace options. */
6245 for_each_thread ([] (thread_info
*thread
)
6247 struct lwp_info
*lwp
= get_thread_lwp (thread
);
6251 /* Stop the lwp so we can modify its ptrace options. */
6252 lwp
->must_set_ptrace_flags
= 1;
6253 linux_stop_lwp (lwp
);
6257 /* Already stopped; go ahead and set the ptrace options. */
6258 struct process_info
*proc
= find_process_pid (pid_of (thread
));
6259 int options
= linux_low_ptrace_options (proc
->attached
);
6261 linux_enable_event_reporting (lwpid_of (thread
), options
);
6262 lwp
->must_set_ptrace_flags
= 0;
6268 linux_process_target::handle_monitor_command (char *mon
)
6270 #ifdef USE_THREAD_DB
6271 return thread_db_handle_monitor_command (mon
);
6278 linux_process_target::core_of_thread (ptid_t ptid
)
6280 return linux_common_core_of_thread (ptid
);
6284 linux_process_target::supports_disable_randomization ()
6286 #ifdef HAVE_PERSONALITY
6294 linux_process_target::supports_agent ()
6300 linux_process_target::supports_range_stepping ()
6302 if (can_software_single_step ())
6304 if (*the_low_target
.supports_range_stepping
== NULL
)
6307 return (*the_low_target
.supports_range_stepping
) ();
6311 linux_process_target::supports_pid_to_exec_file ()
6317 linux_process_target::pid_to_exec_file (int pid
)
6319 return linux_proc_pid_to_exec_file (pid
);
6323 linux_process_target::supports_multifs ()
6329 linux_process_target::multifs_open (int pid
, const char *filename
,
6330 int flags
, mode_t mode
)
6332 return linux_mntns_open_cloexec (pid
, filename
, flags
, mode
);
6336 linux_process_target::multifs_unlink (int pid
, const char *filename
)
6338 return linux_mntns_unlink (pid
, filename
);
6342 linux_process_target::multifs_readlink (int pid
, const char *filename
,
6343 char *buf
, size_t bufsiz
)
6345 return linux_mntns_readlink (pid
, filename
, buf
, bufsiz
);
6348 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
6349 struct target_loadseg
6351 /* Core address to which the segment is mapped. */
6353 /* VMA recorded in the program header. */
6355 /* Size of this segment in memory. */
6359 # if defined PT_GETDSBT
6360 struct target_loadmap
6362 /* Protocol version number, must be zero. */
6364 /* Pointer to the DSBT table, its size, and the DSBT index. */
6365 unsigned *dsbt_table
;
6366 unsigned dsbt_size
, dsbt_index
;
6367 /* Number of segments in this map. */
6369 /* The actual memory map. */
6370 struct target_loadseg segs
[/*nsegs*/];
6372 # define LINUX_LOADMAP PT_GETDSBT
6373 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6374 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6376 struct target_loadmap
6378 /* Protocol version number, must be zero. */
6380 /* Number of segments in this map. */
6382 /* The actual memory map. */
6383 struct target_loadseg segs
[/*nsegs*/];
6385 # define LINUX_LOADMAP PTRACE_GETFDPIC
6386 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6387 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6391 linux_process_target::supports_read_loadmap ()
6397 linux_process_target::read_loadmap (const char *annex
, CORE_ADDR offset
,
6398 unsigned char *myaddr
, unsigned int len
)
6400 int pid
= lwpid_of (current_thread
);
6402 struct target_loadmap
*data
= NULL
;
6403 unsigned int actual_length
, copy_length
;
6405 if (strcmp (annex
, "exec") == 0)
6406 addr
= (int) LINUX_LOADMAP_EXEC
;
6407 else if (strcmp (annex
, "interp") == 0)
6408 addr
= (int) LINUX_LOADMAP_INTERP
;
6412 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
6418 actual_length
= sizeof (struct target_loadmap
)
6419 + sizeof (struct target_loadseg
) * data
->nsegs
;
6421 if (offset
< 0 || offset
> actual_length
)
6424 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
6425 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
6428 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6431 linux_process_target::process_qsupported (char **features
, int count
)
6433 if (the_low_target
.process_qsupported
!= NULL
)
6434 the_low_target
.process_qsupported (features
, count
);
6438 linux_process_target::supports_catch_syscall ()
6440 return (the_low_target
.get_syscall_trapinfo
!= NULL
6441 && linux_supports_tracesysgood ());
6445 linux_process_target::get_ipa_tdesc_idx ()
6447 if (the_low_target
.get_ipa_tdesc_idx
== NULL
)
6450 return (*the_low_target
.get_ipa_tdesc_idx
) ();
6454 linux_process_target::supports_tracepoints ()
6456 if (*the_low_target
.supports_tracepoints
== NULL
)
6459 return (*the_low_target
.supports_tracepoints
) ();
6463 linux_process_target::read_pc (regcache
*regcache
)
6465 if (!low_supports_breakpoints ())
6468 return low_get_pc (regcache
);
6472 linux_process_target::write_pc (regcache
*regcache
, CORE_ADDR pc
)
6474 gdb_assert (low_supports_breakpoints ());
6476 low_set_pc (regcache
, pc
);
6480 linux_process_target::supports_thread_stopped ()
6486 linux_process_target::thread_stopped (thread_info
*thread
)
6488 return get_thread_lwp (thread
)->stopped
;
6491 /* This exposes stop-all-threads functionality to other modules. */
6494 linux_process_target::pause_all (bool freeze
)
6496 stop_all_lwps (freeze
, NULL
);
6499 /* This exposes unstop-all-threads functionality to other gdbserver
6503 linux_process_target::unpause_all (bool unfreeze
)
6505 unstop_all_lwps (unfreeze
, NULL
);
6509 linux_process_target::prepare_to_access_memory ()
6511 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6514 target_pause_all (true);
6519 linux_process_target::done_accessing_memory ()
6521 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6524 target_unpause_all (true);
6528 linux_process_target::supports_fast_tracepoints ()
6530 return the_low_target
.install_fast_tracepoint_jump_pad
!= nullptr;
6534 linux_process_target::install_fast_tracepoint_jump_pad
6535 (CORE_ADDR tpoint
, CORE_ADDR tpaddr
, CORE_ADDR collector
,
6536 CORE_ADDR lockaddr
, ULONGEST orig_size
, CORE_ADDR
*jump_entry
,
6537 CORE_ADDR
*trampoline
, ULONGEST
*trampoline_size
,
6538 unsigned char *jjump_pad_insn
, ULONGEST
*jjump_pad_insn_size
,
6539 CORE_ADDR
*adjusted_insn_addr
, CORE_ADDR
*adjusted_insn_addr_end
,
6542 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
6543 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
6544 jump_entry
, trampoline
, trampoline_size
,
6545 jjump_pad_insn
, jjump_pad_insn_size
,
6546 adjusted_insn_addr
, adjusted_insn_addr_end
,
6551 linux_process_target::emit_ops ()
6553 if (the_low_target
.emit_ops
!= NULL
)
6554 return (*the_low_target
.emit_ops
) ();
6560 linux_process_target::get_min_fast_tracepoint_insn_len ()
6562 return (*the_low_target
.get_min_fast_tracepoint_insn_len
) ();
6565 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6568 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
6569 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
6571 char filename
[PATH_MAX
];
6573 const int auxv_size
= is_elf64
6574 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
6575 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
6577 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
6579 fd
= open (filename
, O_RDONLY
);
6585 while (read (fd
, buf
, auxv_size
) == auxv_size
6586 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
6590 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
6592 switch (aux
->a_type
)
6595 *phdr_memaddr
= aux
->a_un
.a_val
;
6598 *num_phdr
= aux
->a_un
.a_val
;
6604 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
6606 switch (aux
->a_type
)
6609 *phdr_memaddr
= aux
->a_un
.a_val
;
6612 *num_phdr
= aux
->a_un
.a_val
;
6620 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
6622 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6623 "phdr_memaddr = %ld, phdr_num = %d",
6624 (long) *phdr_memaddr
, *num_phdr
);
6631 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6634 get_dynamic (const int pid
, const int is_elf64
)
6636 CORE_ADDR phdr_memaddr
, relocation
;
6638 unsigned char *phdr_buf
;
6639 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
6641 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
6644 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
6645 phdr_buf
= (unsigned char *) alloca (num_phdr
* phdr_size
);
6647 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
6650 /* Compute relocation: it is expected to be 0 for "regular" executables,
6651 non-zero for PIE ones. */
6653 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
6656 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6658 if (p
->p_type
== PT_PHDR
)
6659 relocation
= phdr_memaddr
- p
->p_vaddr
;
6663 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6665 if (p
->p_type
== PT_PHDR
)
6666 relocation
= phdr_memaddr
- p
->p_vaddr
;
6669 if (relocation
== -1)
6671 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6672 any real world executables, including PIE executables, have always
6673 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6674 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6675 or present DT_DEBUG anyway (fpc binaries are statically linked).
6677 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6679 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6684 for (i
= 0; i
< num_phdr
; i
++)
6688 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6690 if (p
->p_type
== PT_DYNAMIC
)
6691 return p
->p_vaddr
+ relocation
;
6695 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6697 if (p
->p_type
== PT_DYNAMIC
)
6698 return p
->p_vaddr
+ relocation
;
6705 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6706 can be 0 if the inferior does not yet have the library list initialized.
6707 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6708 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6711 get_r_debug (const int pid
, const int is_elf64
)
6713 CORE_ADDR dynamic_memaddr
;
6714 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
6715 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
6718 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
6719 if (dynamic_memaddr
== 0)
6722 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
6726 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
6727 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6731 unsigned char buf
[sizeof (Elf64_Xword
)];
6735 #ifdef DT_MIPS_RLD_MAP
6736 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6738 if (linux_read_memory (dyn
->d_un
.d_val
,
6739 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6744 #endif /* DT_MIPS_RLD_MAP */
6745 #ifdef DT_MIPS_RLD_MAP_REL
6746 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6748 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6749 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6754 #endif /* DT_MIPS_RLD_MAP_REL */
6756 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6757 map
= dyn
->d_un
.d_val
;
6759 if (dyn
->d_tag
== DT_NULL
)
6764 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
6765 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6769 unsigned char buf
[sizeof (Elf32_Word
)];
6773 #ifdef DT_MIPS_RLD_MAP
6774 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6776 if (linux_read_memory (dyn
->d_un
.d_val
,
6777 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6782 #endif /* DT_MIPS_RLD_MAP */
6783 #ifdef DT_MIPS_RLD_MAP_REL
6784 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6786 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6787 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6792 #endif /* DT_MIPS_RLD_MAP_REL */
6794 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6795 map
= dyn
->d_un
.d_val
;
6797 if (dyn
->d_tag
== DT_NULL
)
6801 dynamic_memaddr
+= dyn_size
;
6807 /* Read one pointer from MEMADDR in the inferior. */
6810 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
6814 /* Go through a union so this works on either big or little endian
6815 hosts, when the inferior's pointer size is smaller than the size
6816 of CORE_ADDR. It is assumed the inferior's endianness is the
6817 same of the superior's. */
6820 CORE_ADDR core_addr
;
6825 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
6828 if (ptr_size
== sizeof (CORE_ADDR
))
6829 *ptr
= addr
.core_addr
;
6830 else if (ptr_size
== sizeof (unsigned int))
6833 gdb_assert_not_reached ("unhandled pointer size");
6839 linux_process_target::supports_qxfer_libraries_svr4 ()
6844 struct link_map_offsets
6846 /* Offset and size of r_debug.r_version. */
6847 int r_version_offset
;
6849 /* Offset and size of r_debug.r_map. */
6852 /* Offset to l_addr field in struct link_map. */
6855 /* Offset to l_name field in struct link_map. */
6858 /* Offset to l_ld field in struct link_map. */
6861 /* Offset to l_next field in struct link_map. */
6864 /* Offset to l_prev field in struct link_map. */
6868 /* Construct qXfer:libraries-svr4:read reply. */
6871 linux_process_target::qxfer_libraries_svr4 (const char *annex
,
6872 unsigned char *readbuf
,
6873 unsigned const char *writebuf
,
6874 CORE_ADDR offset
, int len
)
6876 struct process_info_private
*const priv
= current_process ()->priv
;
6877 char filename
[PATH_MAX
];
6880 static const struct link_map_offsets lmo_32bit_offsets
=
6882 0, /* r_version offset. */
6883 4, /* r_debug.r_map offset. */
6884 0, /* l_addr offset in link_map. */
6885 4, /* l_name offset in link_map. */
6886 8, /* l_ld offset in link_map. */
6887 12, /* l_next offset in link_map. */
6888 16 /* l_prev offset in link_map. */
6891 static const struct link_map_offsets lmo_64bit_offsets
=
6893 0, /* r_version offset. */
6894 8, /* r_debug.r_map offset. */
6895 0, /* l_addr offset in link_map. */
6896 8, /* l_name offset in link_map. */
6897 16, /* l_ld offset in link_map. */
6898 24, /* l_next offset in link_map. */
6899 32 /* l_prev offset in link_map. */
6901 const struct link_map_offsets
*lmo
;
6902 unsigned int machine
;
6904 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
6905 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
6906 int header_done
= 0;
6908 if (writebuf
!= NULL
)
6910 if (readbuf
== NULL
)
6913 pid
= lwpid_of (current_thread
);
6914 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
6915 is_elf64
= elf_64_file_p (filename
, &machine
);
6916 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
6917 ptr_size
= is_elf64
? 8 : 4;
6919 while (annex
[0] != '\0')
6925 sep
= strchr (annex
, '=');
6929 name_len
= sep
- annex
;
6930 if (name_len
== 5 && startswith (annex
, "start"))
6932 else if (name_len
== 4 && startswith (annex
, "prev"))
6936 annex
= strchr (sep
, ';');
6943 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
6950 if (priv
->r_debug
== 0)
6951 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
6953 /* We failed to find DT_DEBUG. Such situation will not change
6954 for this inferior - do not retry it. Report it to GDB as
6955 E01, see for the reasons at the GDB solib-svr4.c side. */
6956 if (priv
->r_debug
== (CORE_ADDR
) -1)
6959 if (priv
->r_debug
!= 0)
6961 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
6962 (unsigned char *) &r_version
,
6963 sizeof (r_version
)) != 0
6966 warning ("unexpected r_debug version %d", r_version
);
6968 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
6969 &lm_addr
, ptr_size
) != 0)
6971 warning ("unable to read r_map from 0x%lx",
6972 (long) priv
->r_debug
+ lmo
->r_map_offset
);
6977 std::string document
= "<library-list-svr4 version=\"1.0\"";
6980 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
6981 &l_name
, ptr_size
) == 0
6982 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
6983 &l_addr
, ptr_size
) == 0
6984 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
6985 &l_ld
, ptr_size
) == 0
6986 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
6987 &l_prev
, ptr_size
) == 0
6988 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
6989 &l_next
, ptr_size
) == 0)
6991 unsigned char libname
[PATH_MAX
];
6993 if (lm_prev
!= l_prev
)
6995 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
6996 (long) lm_prev
, (long) l_prev
);
7000 /* Ignore the first entry even if it has valid name as the first entry
7001 corresponds to the main executable. The first entry should not be
7002 skipped if the dynamic loader was loaded late by a static executable
7003 (see solib-svr4.c parameter ignore_first). But in such case the main
7004 executable does not have PT_DYNAMIC present and this function already
7005 exited above due to failed get_r_debug. */
7007 string_appendf (document
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
7010 /* Not checking for error because reading may stop before
7011 we've got PATH_MAX worth of characters. */
7013 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
7014 libname
[sizeof (libname
) - 1] = '\0';
7015 if (libname
[0] != '\0')
7019 /* Terminate `<library-list-svr4'. */
7024 string_appendf (document
, "<library name=\"");
7025 xml_escape_text_append (&document
, (char *) libname
);
7026 string_appendf (document
, "\" lm=\"0x%lx\" "
7027 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
7028 (unsigned long) lm_addr
, (unsigned long) l_addr
,
7029 (unsigned long) l_ld
);
7039 /* Empty list; terminate `<library-list-svr4'. */
7043 document
+= "</library-list-svr4>";
7045 int document_len
= document
.length ();
7046 if (offset
< document_len
)
7047 document_len
-= offset
;
7050 if (len
> document_len
)
7053 memcpy (readbuf
, document
.data () + offset
, len
);
7058 #ifdef HAVE_LINUX_BTRACE
7060 btrace_target_info
*
7061 linux_process_target::enable_btrace (ptid_t ptid
,
7062 const btrace_config
*conf
)
7064 return linux_enable_btrace (ptid
, conf
);
7067 /* See to_disable_btrace target method. */
7070 linux_process_target::disable_btrace (btrace_target_info
*tinfo
)
7072 enum btrace_error err
;
7074 err
= linux_disable_btrace (tinfo
);
7075 return (err
== BTRACE_ERR_NONE
? 0 : -1);
7078 /* Encode an Intel Processor Trace configuration. */
7081 linux_low_encode_pt_config (struct buffer
*buffer
,
7082 const struct btrace_data_pt_config
*config
)
7084 buffer_grow_str (buffer
, "<pt-config>\n");
7086 switch (config
->cpu
.vendor
)
7089 buffer_xml_printf (buffer
, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
7090 "model=\"%u\" stepping=\"%u\"/>\n",
7091 config
->cpu
.family
, config
->cpu
.model
,
7092 config
->cpu
.stepping
);
7099 buffer_grow_str (buffer
, "</pt-config>\n");
7102 /* Encode a raw buffer. */
7105 linux_low_encode_raw (struct buffer
*buffer
, const gdb_byte
*data
,
7111 /* We use hex encoding - see gdbsupport/rsp-low.h. */
7112 buffer_grow_str (buffer
, "<raw>\n");
7118 elem
[0] = tohex ((*data
>> 4) & 0xf);
7119 elem
[1] = tohex (*data
++ & 0xf);
7121 buffer_grow (buffer
, elem
, 2);
7124 buffer_grow_str (buffer
, "</raw>\n");
7127 /* See to_read_btrace target method. */
7130 linux_process_target::read_btrace (btrace_target_info
*tinfo
,
7132 enum btrace_read_type type
)
7134 struct btrace_data btrace
;
7135 enum btrace_error err
;
7137 err
= linux_read_btrace (&btrace
, tinfo
, type
);
7138 if (err
!= BTRACE_ERR_NONE
)
7140 if (err
== BTRACE_ERR_OVERFLOW
)
7141 buffer_grow_str0 (buffer
, "E.Overflow.");
7143 buffer_grow_str0 (buffer
, "E.Generic Error.");
7148 switch (btrace
.format
)
7150 case BTRACE_FORMAT_NONE
:
7151 buffer_grow_str0 (buffer
, "E.No Trace.");
7154 case BTRACE_FORMAT_BTS
:
7155 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7156 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7158 for (const btrace_block
&block
: *btrace
.variant
.bts
.blocks
)
7159 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
7160 paddress (block
.begin
), paddress (block
.end
));
7162 buffer_grow_str0 (buffer
, "</btrace>\n");
7165 case BTRACE_FORMAT_PT
:
7166 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7167 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7168 buffer_grow_str (buffer
, "<pt>\n");
7170 linux_low_encode_pt_config (buffer
, &btrace
.variant
.pt
.config
);
7172 linux_low_encode_raw (buffer
, btrace
.variant
.pt
.data
,
7173 btrace
.variant
.pt
.size
);
7175 buffer_grow_str (buffer
, "</pt>\n");
7176 buffer_grow_str0 (buffer
, "</btrace>\n");
7180 buffer_grow_str0 (buffer
, "E.Unsupported Trace Format.");
7187 /* See to_btrace_conf target method. */
7190 linux_process_target::read_btrace_conf (const btrace_target_info
*tinfo
,
7193 const struct btrace_config
*conf
;
7195 buffer_grow_str (buffer
, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
7196 buffer_grow_str (buffer
, "<btrace-conf version=\"1.0\">\n");
7198 conf
= linux_btrace_conf (tinfo
);
7201 switch (conf
->format
)
7203 case BTRACE_FORMAT_NONE
:
7206 case BTRACE_FORMAT_BTS
:
7207 buffer_xml_printf (buffer
, "<bts");
7208 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->bts
.size
);
7209 buffer_xml_printf (buffer
, " />\n");
7212 case BTRACE_FORMAT_PT
:
7213 buffer_xml_printf (buffer
, "<pt");
7214 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->pt
.size
);
7215 buffer_xml_printf (buffer
, "/>\n");
7220 buffer_grow_str0 (buffer
, "</btrace-conf>\n");
7223 #endif /* HAVE_LINUX_BTRACE */
7225 /* See nat/linux-nat.h. */
7228 current_lwp_ptid (void)
7230 return ptid_of (current_thread
);
7233 /* Implementation of the target_ops method "sw_breakpoint_from_kind". */
7236 linux_process_target::sw_breakpoint_from_kind (int kind
, int *size
)
7238 gdb_assert (the_low_target
.sw_breakpoint_from_kind
!= NULL
);
7240 return (*the_low_target
.sw_breakpoint_from_kind
) (kind
, size
);
7244 linux_process_target::thread_name (ptid_t thread
)
7246 return linux_proc_tid_get_name (thread
);
7251 linux_process_target::thread_handle (ptid_t ptid
, gdb_byte
**handle
,
7254 return thread_db_thread_handle (ptid
, handle
, handle_len
);
7258 /* Default implementation of linux_target_ops method "set_pc" for
7259 32-bit pc register which is literally named "pc". */
7262 linux_set_pc_32bit (struct regcache
*regcache
, CORE_ADDR pc
)
7264 uint32_t newpc
= pc
;
7266 supply_register_by_name (regcache
, "pc", &newpc
);
7269 /* Default implementation of linux_target_ops method "get_pc" for
7270 32-bit pc register which is literally named "pc". */
7273 linux_get_pc_32bit (struct regcache
*regcache
)
7277 collect_register_by_name (regcache
, "pc", &pc
);
7279 debug_printf ("stop pc is 0x%" PRIx32
"\n", pc
);
7283 /* Default implementation of linux_target_ops method "set_pc" for
7284 64-bit pc register which is literally named "pc". */
7287 linux_set_pc_64bit (struct regcache
*regcache
, CORE_ADDR pc
)
7289 uint64_t newpc
= pc
;
7291 supply_register_by_name (regcache
, "pc", &newpc
);
7294 /* Default implementation of linux_target_ops method "get_pc" for
7295 64-bit pc register which is literally named "pc". */
7298 linux_get_pc_64bit (struct regcache
*regcache
)
7302 collect_register_by_name (regcache
, "pc", &pc
);
7304 debug_printf ("stop pc is 0x%" PRIx64
"\n", pc
);
7308 /* See linux-low.h. */
7311 linux_get_auxv (int wordsize
, CORE_ADDR match
, CORE_ADDR
*valp
)
7313 gdb_byte
*data
= (gdb_byte
*) alloca (2 * wordsize
);
7316 gdb_assert (wordsize
== 4 || wordsize
== 8);
7318 while (the_target
->read_auxv (offset
, data
, 2 * wordsize
) == 2 * wordsize
)
7322 uint32_t *data_p
= (uint32_t *) data
;
7323 if (data_p
[0] == match
)
7331 uint64_t *data_p
= (uint64_t *) data
;
7332 if (data_p
[0] == match
)
7339 offset
+= 2 * wordsize
;
7345 /* See linux-low.h. */
7348 linux_get_hwcap (int wordsize
)
7350 CORE_ADDR hwcap
= 0;
7351 linux_get_auxv (wordsize
, AT_HWCAP
, &hwcap
);
7355 /* See linux-low.h. */
7358 linux_get_hwcap2 (int wordsize
)
7360 CORE_ADDR hwcap2
= 0;
7361 linux_get_auxv (wordsize
, AT_HWCAP2
, &hwcap2
);
7365 #ifdef HAVE_LINUX_REGSETS
7367 initialize_regsets_info (struct regsets_info
*info
)
7369 for (info
->num_regsets
= 0;
7370 info
->regsets
[info
->num_regsets
].size
>= 0;
7371 info
->num_regsets
++)
7377 initialize_low (void)
7379 struct sigaction sigchld_action
;
7381 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
7382 set_target_ops (the_linux_target
);
7384 linux_ptrace_init_warnings ();
7385 linux_proc_init_warnings ();
7387 sigchld_action
.sa_handler
= sigchld_handler
;
7388 sigemptyset (&sigchld_action
.sa_mask
);
7389 sigchld_action
.sa_flags
= SA_RESTART
;
7390 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
7392 initialize_low_arch ();
7394 linux_check_ptrace_features ();