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 ();
296 linux_process_target::low_supports_breakpoints ()
302 linux_process_target::low_get_pc (regcache
*regcache
)
308 linux_process_target::low_set_pc (regcache
*regcache
, CORE_ADDR newpc
)
310 gdb_assert_not_reached ("linux target op low_set_pc is not implemented");
313 std::vector
<CORE_ADDR
>
314 linux_process_target::low_get_next_pcs (regcache
*regcache
)
316 gdb_assert_not_reached ("linux target op low_get_next_pcs is not "
320 /* Returns true if this target can support fast tracepoints. This
321 does not mean that the in-process agent has been loaded in the
325 supports_fast_tracepoints (void)
327 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
330 /* True if LWP is stopped in its stepping range. */
333 lwp_in_step_range (struct lwp_info
*lwp
)
335 CORE_ADDR pc
= lwp
->stop_pc
;
337 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
340 struct pending_signals
344 struct pending_signals
*prev
;
347 /* The read/write ends of the pipe registered as waitable file in the
349 static int linux_event_pipe
[2] = { -1, -1 };
351 /* True if we're currently in async mode. */
352 #define target_is_async_p() (linux_event_pipe[0] != -1)
354 static void send_sigstop (struct lwp_info
*lwp
);
356 /* Return non-zero if HEADER is a 64-bit ELF file. */
359 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
361 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
362 && header
->e_ident
[EI_MAG1
] == ELFMAG1
363 && header
->e_ident
[EI_MAG2
] == ELFMAG2
364 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
366 *machine
= header
->e_machine
;
367 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
374 /* Return non-zero if FILE is a 64-bit ELF file,
375 zero if the file is not a 64-bit ELF file,
376 and -1 if the file is not accessible or doesn't exist. */
379 elf_64_file_p (const char *file
, unsigned int *machine
)
384 fd
= open (file
, O_RDONLY
);
388 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
395 return elf_64_header_p (&header
, machine
);
398 /* Accepts an integer PID; Returns true if the executable PID is
399 running is a 64-bit ELF file.. */
402 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
406 sprintf (file
, "/proc/%d/exe", pid
);
407 return elf_64_file_p (file
, machine
);
411 delete_lwp (struct lwp_info
*lwp
)
413 struct thread_info
*thr
= get_lwp_thread (lwp
);
416 debug_printf ("deleting %ld\n", lwpid_of (thr
));
420 if (the_low_target
.delete_thread
!= NULL
)
421 the_low_target
.delete_thread (lwp
->arch_private
);
423 gdb_assert (lwp
->arch_private
== NULL
);
428 /* Add a process to the common process list, and set its private
431 static struct process_info
*
432 linux_add_process (int pid
, int attached
)
434 struct process_info
*proc
;
436 proc
= add_process (pid
, attached
);
437 proc
->priv
= XCNEW (struct process_info_private
);
439 if (the_low_target
.new_process
!= NULL
)
440 proc
->priv
->arch_private
= the_low_target
.new_process ();
446 linux_process_target::arch_setup_thread (thread_info
*thread
)
448 struct thread_info
*saved_thread
;
450 saved_thread
= current_thread
;
451 current_thread
= thread
;
455 current_thread
= saved_thread
;
459 linux_process_target::handle_extended_wait (lwp_info
**orig_event_lwp
,
462 client_state
&cs
= get_client_state ();
463 struct lwp_info
*event_lwp
= *orig_event_lwp
;
464 int event
= linux_ptrace_get_extended_event (wstat
);
465 struct thread_info
*event_thr
= get_lwp_thread (event_lwp
);
466 struct lwp_info
*new_lwp
;
468 gdb_assert (event_lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
470 /* All extended events we currently use are mid-syscall. Only
471 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
472 you have to be using PTRACE_SEIZE to get that. */
473 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
475 if ((event
== PTRACE_EVENT_FORK
) || (event
== PTRACE_EVENT_VFORK
)
476 || (event
== PTRACE_EVENT_CLONE
))
479 unsigned long new_pid
;
482 /* Get the pid of the new lwp. */
483 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_thr
), (PTRACE_TYPE_ARG3
) 0,
486 /* If we haven't already seen the new PID stop, wait for it now. */
487 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
489 /* The new child has a pending SIGSTOP. We can't affect it until it
490 hits the SIGSTOP, but we're already attached. */
492 ret
= my_waitpid (new_pid
, &status
, __WALL
);
495 perror_with_name ("waiting for new child");
496 else if (ret
!= new_pid
)
497 warning ("wait returned unexpected PID %d", ret
);
498 else if (!WIFSTOPPED (status
))
499 warning ("wait returned unexpected status 0x%x", status
);
502 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
504 struct process_info
*parent_proc
;
505 struct process_info
*child_proc
;
506 struct lwp_info
*child_lwp
;
507 struct thread_info
*child_thr
;
508 struct target_desc
*tdesc
;
510 ptid
= ptid_t (new_pid
, new_pid
, 0);
514 debug_printf ("HEW: Got fork event from LWP %ld, "
516 ptid_of (event_thr
).lwp (),
520 /* Add the new process to the tables and clone the breakpoint
521 lists of the parent. We need to do this even if the new process
522 will be detached, since we will need the process object and the
523 breakpoints to remove any breakpoints from memory when we
524 detach, and the client side will access registers. */
525 child_proc
= linux_add_process (new_pid
, 0);
526 gdb_assert (child_proc
!= NULL
);
527 child_lwp
= add_lwp (ptid
);
528 gdb_assert (child_lwp
!= NULL
);
529 child_lwp
->stopped
= 1;
530 child_lwp
->must_set_ptrace_flags
= 1;
531 child_lwp
->status_pending_p
= 0;
532 child_thr
= get_lwp_thread (child_lwp
);
533 child_thr
->last_resume_kind
= resume_stop
;
534 child_thr
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
536 /* If we're suspending all threads, leave this one suspended
537 too. If the fork/clone parent is stepping over a breakpoint,
538 all other threads have been suspended already. Leave the
539 child suspended too. */
540 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
541 || event_lwp
->bp_reinsert
!= 0)
544 debug_printf ("HEW: leaving child suspended\n");
545 child_lwp
->suspended
= 1;
548 parent_proc
= get_thread_process (event_thr
);
549 child_proc
->attached
= parent_proc
->attached
;
551 if (event_lwp
->bp_reinsert
!= 0
552 && supports_software_single_step ()
553 && event
== PTRACE_EVENT_VFORK
)
555 /* If we leave single-step breakpoints there, child will
556 hit it, so uninsert single-step breakpoints from parent
557 (and child). Once vfork child is done, reinsert
558 them back to parent. */
559 uninsert_single_step_breakpoints (event_thr
);
562 clone_all_breakpoints (child_thr
, event_thr
);
564 tdesc
= allocate_target_description ();
565 copy_target_description (tdesc
, parent_proc
->tdesc
);
566 child_proc
->tdesc
= tdesc
;
568 /* Clone arch-specific process data. */
569 if (the_low_target
.new_fork
!= NULL
)
570 the_low_target
.new_fork (parent_proc
, child_proc
);
572 /* Save fork info in the parent thread. */
573 if (event
== PTRACE_EVENT_FORK
)
574 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_FORKED
;
575 else if (event
== PTRACE_EVENT_VFORK
)
576 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORKED
;
578 event_lwp
->waitstatus
.value
.related_pid
= ptid
;
580 /* The status_pending field contains bits denoting the
581 extended event, so when the pending event is handled,
582 the handler will look at lwp->waitstatus. */
583 event_lwp
->status_pending_p
= 1;
584 event_lwp
->status_pending
= wstat
;
586 /* Link the threads until the parent event is passed on to
588 event_lwp
->fork_relative
= child_lwp
;
589 child_lwp
->fork_relative
= event_lwp
;
591 /* If the parent thread is doing step-over with single-step
592 breakpoints, the list of single-step breakpoints are cloned
593 from the parent's. Remove them from the child process.
594 In case of vfork, we'll reinsert them back once vforked
596 if (event_lwp
->bp_reinsert
!= 0
597 && supports_software_single_step ())
599 /* The child process is forked and stopped, so it is safe
600 to access its memory without stopping all other threads
601 from other processes. */
602 delete_single_step_breakpoints (child_thr
);
604 gdb_assert (has_single_step_breakpoints (event_thr
));
605 gdb_assert (!has_single_step_breakpoints (child_thr
));
608 /* Report the event. */
613 debug_printf ("HEW: Got clone event "
614 "from LWP %ld, new child is LWP %ld\n",
615 lwpid_of (event_thr
), new_pid
);
617 ptid
= ptid_t (pid_of (event_thr
), new_pid
, 0);
618 new_lwp
= add_lwp (ptid
);
620 /* Either we're going to immediately resume the new thread
621 or leave it stopped. resume_one_lwp is a nop if it
622 thinks the thread is currently running, so set this first
623 before calling resume_one_lwp. */
624 new_lwp
->stopped
= 1;
626 /* If we're suspending all threads, leave this one suspended
627 too. If the fork/clone parent is stepping over a breakpoint,
628 all other threads have been suspended already. Leave the
629 child suspended too. */
630 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
631 || event_lwp
->bp_reinsert
!= 0)
632 new_lwp
->suspended
= 1;
634 /* Normally we will get the pending SIGSTOP. But in some cases
635 we might get another signal delivered to the group first.
636 If we do get another signal, be sure not to lose it. */
637 if (WSTOPSIG (status
) != SIGSTOP
)
639 new_lwp
->stop_expected
= 1;
640 new_lwp
->status_pending_p
= 1;
641 new_lwp
->status_pending
= status
;
643 else if (cs
.report_thread_events
)
645 new_lwp
->waitstatus
.kind
= TARGET_WAITKIND_THREAD_CREATED
;
646 new_lwp
->status_pending_p
= 1;
647 new_lwp
->status_pending
= status
;
651 thread_db_notice_clone (event_thr
, ptid
);
654 /* Don't report the event. */
657 else if (event
== PTRACE_EVENT_VFORK_DONE
)
659 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
661 if (event_lwp
->bp_reinsert
!= 0 && supports_software_single_step ())
663 reinsert_single_step_breakpoints (event_thr
);
665 gdb_assert (has_single_step_breakpoints (event_thr
));
668 /* Report the event. */
671 else if (event
== PTRACE_EVENT_EXEC
&& cs
.report_exec_events
)
673 struct process_info
*proc
;
674 std::vector
<int> syscalls_to_catch
;
680 debug_printf ("HEW: Got exec event from LWP %ld\n",
681 lwpid_of (event_thr
));
684 /* Get the event ptid. */
685 event_ptid
= ptid_of (event_thr
);
686 event_pid
= event_ptid
.pid ();
688 /* Save the syscall list from the execing process. */
689 proc
= get_thread_process (event_thr
);
690 syscalls_to_catch
= std::move (proc
->syscalls_to_catch
);
692 /* Delete the execing process and all its threads. */
694 current_thread
= NULL
;
696 /* Create a new process/lwp/thread. */
697 proc
= linux_add_process (event_pid
, 0);
698 event_lwp
= add_lwp (event_ptid
);
699 event_thr
= get_lwp_thread (event_lwp
);
700 gdb_assert (current_thread
== event_thr
);
701 arch_setup_thread (event_thr
);
703 /* Set the event status. */
704 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXECD
;
705 event_lwp
->waitstatus
.value
.execd_pathname
706 = xstrdup (linux_proc_pid_to_exec_file (lwpid_of (event_thr
)));
708 /* Mark the exec status as pending. */
709 event_lwp
->stopped
= 1;
710 event_lwp
->status_pending_p
= 1;
711 event_lwp
->status_pending
= wstat
;
712 event_thr
->last_resume_kind
= resume_continue
;
713 event_thr
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
715 /* Update syscall state in the new lwp, effectively mid-syscall too. */
716 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
718 /* Restore the list to catch. Don't rely on the client, which is free
719 to avoid sending a new list when the architecture doesn't change.
720 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
721 proc
->syscalls_to_catch
= std::move (syscalls_to_catch
);
723 /* Report the event. */
724 *orig_event_lwp
= event_lwp
;
728 internal_error (__FILE__
, __LINE__
, _("unknown ptrace event %d"), event
);
732 linux_process_target::get_pc (lwp_info
*lwp
)
734 struct thread_info
*saved_thread
;
735 struct regcache
*regcache
;
738 if (!low_supports_breakpoints ())
741 saved_thread
= current_thread
;
742 current_thread
= get_lwp_thread (lwp
);
744 regcache
= get_thread_regcache (current_thread
, 1);
745 pc
= low_get_pc (regcache
);
748 debug_printf ("pc is 0x%lx\n", (long) pc
);
750 current_thread
= saved_thread
;
754 /* This function should only be called if LWP got a SYSCALL_SIGTRAP.
755 Fill *SYSNO with the syscall nr trapped. */
758 get_syscall_trapinfo (struct lwp_info
*lwp
, int *sysno
)
760 struct thread_info
*saved_thread
;
761 struct regcache
*regcache
;
763 if (the_low_target
.get_syscall_trapinfo
== NULL
)
765 /* If we cannot get the syscall trapinfo, report an unknown
766 system call number. */
767 *sysno
= UNKNOWN_SYSCALL
;
771 saved_thread
= current_thread
;
772 current_thread
= get_lwp_thread (lwp
);
774 regcache
= get_thread_regcache (current_thread
, 1);
775 (*the_low_target
.get_syscall_trapinfo
) (regcache
, sysno
);
778 debug_printf ("get_syscall_trapinfo sysno %d\n", *sysno
);
780 current_thread
= saved_thread
;
783 static int check_stopped_by_watchpoint (struct lwp_info
*child
);
786 linux_process_target::save_stop_reason (lwp_info
*lwp
)
789 CORE_ADDR sw_breakpoint_pc
;
790 struct thread_info
*saved_thread
;
791 #if USE_SIGTRAP_SIGINFO
795 if (!low_supports_breakpoints ())
799 sw_breakpoint_pc
= pc
- the_low_target
.decr_pc_after_break
;
801 /* breakpoint_at reads from the current thread. */
802 saved_thread
= current_thread
;
803 current_thread
= get_lwp_thread (lwp
);
805 #if USE_SIGTRAP_SIGINFO
806 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
807 (PTRACE_TYPE_ARG3
) 0, &siginfo
) == 0)
809 if (siginfo
.si_signo
== SIGTRAP
)
811 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
)
812 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
814 /* The si_code is ambiguous on this arch -- check debug
816 if (!check_stopped_by_watchpoint (lwp
))
817 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
819 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
))
821 /* If we determine the LWP stopped for a SW breakpoint,
822 trust it. Particularly don't check watchpoint
823 registers, because at least on s390, we'd find
824 stopped-by-watchpoint as long as there's a watchpoint
826 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
828 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
830 /* This can indicate either a hardware breakpoint or
831 hardware watchpoint. Check debug registers. */
832 if (!check_stopped_by_watchpoint (lwp
))
833 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
835 else if (siginfo
.si_code
== TRAP_TRACE
)
837 /* We may have single stepped an instruction that
838 triggered a watchpoint. In that case, on some
839 architectures (such as x86), instead of TRAP_HWBKPT,
840 si_code indicates TRAP_TRACE, and we need to check
841 the debug registers separately. */
842 if (!check_stopped_by_watchpoint (lwp
))
843 lwp
->stop_reason
= TARGET_STOPPED_BY_SINGLE_STEP
;
848 /* We may have just stepped a breakpoint instruction. E.g., in
849 non-stop mode, GDB first tells the thread A to step a range, and
850 then the user inserts a breakpoint inside the range. In that
851 case we need to report the breakpoint PC. */
852 if ((!lwp
->stepping
|| lwp
->stop_pc
== sw_breakpoint_pc
)
853 && (*the_low_target
.breakpoint_at
) (sw_breakpoint_pc
))
854 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
856 if (hardware_breakpoint_inserted_here (pc
))
857 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
859 if (lwp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
)
860 check_stopped_by_watchpoint (lwp
);
863 if (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
)
867 struct thread_info
*thr
= get_lwp_thread (lwp
);
869 debug_printf ("CSBB: %s stopped by software breakpoint\n",
870 target_pid_to_str (ptid_of (thr
)));
873 /* Back up the PC if necessary. */
874 if (pc
!= sw_breakpoint_pc
)
876 struct regcache
*regcache
877 = get_thread_regcache (current_thread
, 1);
878 low_set_pc (regcache
, sw_breakpoint_pc
);
881 /* Update this so we record the correct stop PC below. */
882 pc
= sw_breakpoint_pc
;
884 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
888 struct thread_info
*thr
= get_lwp_thread (lwp
);
890 debug_printf ("CSBB: %s stopped by hardware breakpoint\n",
891 target_pid_to_str (ptid_of (thr
)));
894 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
898 struct thread_info
*thr
= get_lwp_thread (lwp
);
900 debug_printf ("CSBB: %s stopped by hardware watchpoint\n",
901 target_pid_to_str (ptid_of (thr
)));
904 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
)
908 struct thread_info
*thr
= get_lwp_thread (lwp
);
910 debug_printf ("CSBB: %s stopped by trace\n",
911 target_pid_to_str (ptid_of (thr
)));
916 current_thread
= saved_thread
;
920 static struct lwp_info
*
921 add_lwp (ptid_t ptid
)
923 struct lwp_info
*lwp
;
925 lwp
= XCNEW (struct lwp_info
);
927 lwp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
929 lwp
->thread
= add_thread (ptid
, lwp
);
931 if (the_low_target
.new_thread
!= NULL
)
932 the_low_target
.new_thread (lwp
);
937 /* Callback to be used when calling fork_inferior, responsible for
938 actually initiating the tracing of the inferior. */
943 if (ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0,
944 (PTRACE_TYPE_ARG4
) 0) < 0)
945 trace_start_error_with_name ("ptrace");
947 if (setpgid (0, 0) < 0)
948 trace_start_error_with_name ("setpgid");
950 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
951 stdout to stderr so that inferior i/o doesn't corrupt the connection.
952 Also, redirect stdin to /dev/null. */
953 if (remote_connection_is_stdio ())
956 trace_start_error_with_name ("close");
957 if (open ("/dev/null", O_RDONLY
) < 0)
958 trace_start_error_with_name ("open");
960 trace_start_error_with_name ("dup2");
961 if (write (2, "stdin/stdout redirected\n",
962 sizeof ("stdin/stdout redirected\n") - 1) < 0)
964 /* Errors ignored. */;
969 /* Start an inferior process and returns its pid.
970 PROGRAM is the name of the program to be started, and PROGRAM_ARGS
971 are its arguments. */
974 linux_process_target::create_inferior (const char *program
,
975 const std::vector
<char *> &program_args
)
977 client_state
&cs
= get_client_state ();
978 struct lwp_info
*new_lwp
;
983 maybe_disable_address_space_randomization restore_personality
984 (cs
.disable_randomization
);
985 std::string str_program_args
= stringify_argv (program_args
);
987 pid
= fork_inferior (program
,
988 str_program_args
.c_str (),
989 get_environ ()->envp (), linux_ptrace_fun
,
990 NULL
, NULL
, NULL
, NULL
);
993 linux_add_process (pid
, 0);
995 ptid
= ptid_t (pid
, pid
, 0);
996 new_lwp
= add_lwp (ptid
);
997 new_lwp
->must_set_ptrace_flags
= 1;
999 post_fork_inferior (pid
, program
);
1004 /* Implement the post_create_inferior target_ops method. */
1007 linux_process_target::post_create_inferior ()
1009 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
1013 if (lwp
->must_set_ptrace_flags
)
1015 struct process_info
*proc
= current_process ();
1016 int options
= linux_low_ptrace_options (proc
->attached
);
1018 linux_enable_event_reporting (lwpid_of (current_thread
), options
);
1019 lwp
->must_set_ptrace_flags
= 0;
1023 /* Attach to an inferior process. Returns 0 on success, ERRNO on
1027 linux_attach_lwp (ptid_t ptid
)
1029 struct lwp_info
*new_lwp
;
1030 int lwpid
= ptid
.lwp ();
1032 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0)
1036 new_lwp
= add_lwp (ptid
);
1038 /* We need to wait for SIGSTOP before being able to make the next
1039 ptrace call on this LWP. */
1040 new_lwp
->must_set_ptrace_flags
= 1;
1042 if (linux_proc_pid_is_stopped (lwpid
))
1045 debug_printf ("Attached to a stopped process\n");
1047 /* The process is definitely stopped. It is in a job control
1048 stop, unless the kernel predates the TASK_STOPPED /
1049 TASK_TRACED distinction, in which case it might be in a
1050 ptrace stop. Make sure it is in a ptrace stop; from there we
1051 can kill it, signal it, et cetera.
1053 First make sure there is a pending SIGSTOP. Since we are
1054 already attached, the process can not transition from stopped
1055 to running without a PTRACE_CONT; so we know this signal will
1056 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1057 probably already in the queue (unless this kernel is old
1058 enough to use TASK_STOPPED for ptrace stops); but since
1059 SIGSTOP is not an RT signal, it can only be queued once. */
1060 kill_lwp (lwpid
, SIGSTOP
);
1062 /* Finally, resume the stopped process. This will deliver the
1063 SIGSTOP (or a higher priority signal, just like normal
1064 PTRACE_ATTACH), which we'll catch later on. */
1065 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1068 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1069 brings it to a halt.
1071 There are several cases to consider here:
1073 1) gdbserver has already attached to the process and is being notified
1074 of a new thread that is being created.
1075 In this case we should ignore that SIGSTOP and resume the
1076 process. This is handled below by setting stop_expected = 1,
1077 and the fact that add_thread sets last_resume_kind ==
1080 2) This is the first thread (the process thread), and we're attaching
1081 to it via attach_inferior.
1082 In this case we want the process thread to stop.
1083 This is handled by having linux_attach set last_resume_kind ==
1084 resume_stop after we return.
1086 If the pid we are attaching to is also the tgid, we attach to and
1087 stop all the existing threads. Otherwise, we attach to pid and
1088 ignore any other threads in the same group as this pid.
1090 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1092 In this case we want the thread to stop.
1093 FIXME: This case is currently not properly handled.
1094 We should wait for the SIGSTOP but don't. Things work apparently
1095 because enough time passes between when we ptrace (ATTACH) and when
1096 gdb makes the next ptrace call on the thread.
1098 On the other hand, if we are currently trying to stop all threads, we
1099 should treat the new thread as if we had sent it a SIGSTOP. This works
1100 because we are guaranteed that the add_lwp call above added us to the
1101 end of the list, and so the new thread has not yet reached
1102 wait_for_sigstop (but will). */
1103 new_lwp
->stop_expected
= 1;
1108 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1109 already attached. Returns true if a new LWP is found, false
1113 attach_proc_task_lwp_callback (ptid_t ptid
)
1115 /* Is this a new thread? */
1116 if (find_thread_ptid (ptid
) == NULL
)
1118 int lwpid
= ptid
.lwp ();
1122 debug_printf ("Found new lwp %d\n", lwpid
);
1124 err
= linux_attach_lwp (ptid
);
1126 /* Be quiet if we simply raced with the thread exiting. EPERM
1127 is returned if the thread's task still exists, and is marked
1128 as exited or zombie, as well as other conditions, so in that
1129 case, confirm the status in /proc/PID/status. */
1131 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1135 debug_printf ("Cannot attach to lwp %d: "
1136 "thread is gone (%d: %s)\n",
1137 lwpid
, err
, safe_strerror (err
));
1143 = linux_ptrace_attach_fail_reason_string (ptid
, err
);
1145 warning (_("Cannot attach to lwp %d: %s"), lwpid
, reason
.c_str ());
1153 static void async_file_mark (void);
1155 /* Attach to PID. If PID is the tgid, attach to it and all
1159 linux_process_target::attach (unsigned long pid
)
1161 struct process_info
*proc
;
1162 struct thread_info
*initial_thread
;
1163 ptid_t ptid
= ptid_t (pid
, pid
, 0);
1166 proc
= linux_add_process (pid
, 1);
1168 /* Attach to PID. We will check for other threads
1170 err
= linux_attach_lwp (ptid
);
1173 remove_process (proc
);
1175 std::string reason
= linux_ptrace_attach_fail_reason_string (ptid
, err
);
1176 error ("Cannot attach to process %ld: %s", pid
, reason
.c_str ());
1179 /* Don't ignore the initial SIGSTOP if we just attached to this
1180 process. It will be collected by wait shortly. */
1181 initial_thread
= find_thread_ptid (ptid_t (pid
, pid
, 0));
1182 initial_thread
->last_resume_kind
= resume_stop
;
1184 /* We must attach to every LWP. If /proc is mounted, use that to
1185 find them now. On the one hand, the inferior may be using raw
1186 clone instead of using pthreads. On the other hand, even if it
1187 is using pthreads, GDB may not be connected yet (thread_db needs
1188 to do symbol lookups, through qSymbol). Also, thread_db walks
1189 structures in the inferior's address space to find the list of
1190 threads/LWPs, and those structures may well be corrupted. Note
1191 that once thread_db is loaded, we'll still use it to list threads
1192 and associate pthread info with each LWP. */
1193 linux_proc_attach_tgid_threads (pid
, attach_proc_task_lwp_callback
);
1195 /* GDB will shortly read the xml target description for this
1196 process, to figure out the process' architecture. But the target
1197 description is only filled in when the first process/thread in
1198 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1199 that now, otherwise, if GDB is fast enough, it could read the
1200 target description _before_ that initial stop. */
1203 struct lwp_info
*lwp
;
1205 ptid_t pid_ptid
= ptid_t (pid
);
1207 lwpid
= wait_for_event_filtered (pid_ptid
, pid_ptid
, &wstat
, __WALL
);
1208 gdb_assert (lwpid
> 0);
1210 lwp
= find_lwp_pid (ptid_t (lwpid
));
1212 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGSTOP
)
1214 lwp
->status_pending_p
= 1;
1215 lwp
->status_pending
= wstat
;
1218 initial_thread
->last_resume_kind
= resume_continue
;
1222 gdb_assert (proc
->tdesc
!= NULL
);
1229 last_thread_of_process_p (int pid
)
1231 bool seen_one
= false;
1233 thread_info
*thread
= find_thread (pid
, [&] (thread_info
*thr_arg
)
1237 /* This is the first thread of this process we see. */
1243 /* This is the second thread of this process we see. */
1248 return thread
== NULL
;
1254 linux_kill_one_lwp (struct lwp_info
*lwp
)
1256 struct thread_info
*thr
= get_lwp_thread (lwp
);
1257 int pid
= lwpid_of (thr
);
1259 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1260 there is no signal context, and ptrace(PTRACE_KILL) (or
1261 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1262 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1263 alternative is to kill with SIGKILL. We only need one SIGKILL
1264 per process, not one for each thread. But since we still support
1265 support debugging programs using raw clone without CLONE_THREAD,
1266 we send one for each thread. For years, we used PTRACE_KILL
1267 only, so we're being a bit paranoid about some old kernels where
1268 PTRACE_KILL might work better (dubious if there are any such, but
1269 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1270 second, and so we're fine everywhere. */
1273 kill_lwp (pid
, SIGKILL
);
1276 int save_errno
= errno
;
1278 debug_printf ("LKL: kill_lwp (SIGKILL) %s, 0, 0 (%s)\n",
1279 target_pid_to_str (ptid_of (thr
)),
1280 save_errno
? safe_strerror (save_errno
) : "OK");
1284 ptrace (PTRACE_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1287 int save_errno
= errno
;
1289 debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
1290 target_pid_to_str (ptid_of (thr
)),
1291 save_errno
? safe_strerror (save_errno
) : "OK");
1295 /* Kill LWP and wait for it to die. */
1298 kill_wait_lwp (struct lwp_info
*lwp
)
1300 struct thread_info
*thr
= get_lwp_thread (lwp
);
1301 int pid
= ptid_of (thr
).pid ();
1302 int lwpid
= ptid_of (thr
).lwp ();
1307 debug_printf ("kwl: killing lwp %d, for pid: %d\n", lwpid
, pid
);
1311 linux_kill_one_lwp (lwp
);
1313 /* Make sure it died. Notes:
1315 - The loop is most likely unnecessary.
1317 - We don't use wait_for_event as that could delete lwps
1318 while we're iterating over them. We're not interested in
1319 any pending status at this point, only in making sure all
1320 wait status on the kernel side are collected until the
1323 - We don't use __WALL here as the __WALL emulation relies on
1324 SIGCHLD, and killing a stopped process doesn't generate
1325 one, nor an exit status.
1327 res
= my_waitpid (lwpid
, &wstat
, 0);
1328 if (res
== -1 && errno
== ECHILD
)
1329 res
= my_waitpid (lwpid
, &wstat
, __WCLONE
);
1330 } while (res
> 0 && WIFSTOPPED (wstat
));
1332 /* Even if it was stopped, the child may have already disappeared.
1333 E.g., if it was killed by SIGKILL. */
1334 if (res
< 0 && errno
!= ECHILD
)
1335 perror_with_name ("kill_wait_lwp");
1338 /* Callback for `for_each_thread'. Kills an lwp of a given process,
1339 except the leader. */
1342 kill_one_lwp_callback (thread_info
*thread
, int pid
)
1344 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1346 /* We avoid killing the first thread here, because of a Linux kernel (at
1347 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1348 the children get a chance to be reaped, it will remain a zombie
1351 if (lwpid_of (thread
) == pid
)
1354 debug_printf ("lkop: is last of process %s\n",
1355 target_pid_to_str (thread
->id
));
1359 kill_wait_lwp (lwp
);
1363 linux_process_target::kill (process_info
*process
)
1365 int pid
= process
->pid
;
1367 /* If we're killing a running inferior, make sure it is stopped
1368 first, as PTRACE_KILL will not work otherwise. */
1369 stop_all_lwps (0, NULL
);
1371 for_each_thread (pid
, [&] (thread_info
*thread
)
1373 kill_one_lwp_callback (thread
, pid
);
1376 /* See the comment in linux_kill_one_lwp. We did not kill the first
1377 thread in the list, so do so now. */
1378 lwp_info
*lwp
= find_lwp_pid (ptid_t (pid
));
1383 debug_printf ("lk_1: cannot find lwp for pid: %d\n",
1387 kill_wait_lwp (lwp
);
1391 /* Since we presently can only stop all lwps of all processes, we
1392 need to unstop lwps of other processes. */
1393 unstop_all_lwps (0, NULL
);
1397 /* Get pending signal of THREAD, for detaching purposes. This is the
1398 signal the thread last stopped for, which we need to deliver to the
1399 thread when detaching, otherwise, it'd be suppressed/lost. */
1402 get_detach_signal (struct thread_info
*thread
)
1404 client_state
&cs
= get_client_state ();
1405 enum gdb_signal signo
= GDB_SIGNAL_0
;
1407 struct lwp_info
*lp
= get_thread_lwp (thread
);
1409 if (lp
->status_pending_p
)
1410 status
= lp
->status_pending
;
1413 /* If the thread had been suspended by gdbserver, and it stopped
1414 cleanly, then it'll have stopped with SIGSTOP. But we don't
1415 want to deliver that SIGSTOP. */
1416 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1417 || thread
->last_status
.value
.sig
== GDB_SIGNAL_0
)
1420 /* Otherwise, we may need to deliver the signal we
1422 status
= lp
->last_status
;
1425 if (!WIFSTOPPED (status
))
1428 debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1429 target_pid_to_str (ptid_of (thread
)));
1433 /* Extended wait statuses aren't real SIGTRAPs. */
1434 if (WSTOPSIG (status
) == SIGTRAP
&& linux_is_extended_waitstatus (status
))
1437 debug_printf ("GPS: lwp %s had stopped with extended "
1438 "status: no pending signal\n",
1439 target_pid_to_str (ptid_of (thread
)));
1443 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1445 if (cs
.program_signals_p
&& !cs
.program_signals
[signo
])
1448 debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1449 target_pid_to_str (ptid_of (thread
)),
1450 gdb_signal_to_string (signo
));
1453 else if (!cs
.program_signals_p
1454 /* If we have no way to know which signals GDB does not
1455 want to have passed to the program, assume
1456 SIGTRAP/SIGINT, which is GDB's default. */
1457 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1460 debug_printf ("GPS: lwp %s had signal %s, "
1461 "but we don't know if we should pass it. "
1462 "Default to not.\n",
1463 target_pid_to_str (ptid_of (thread
)),
1464 gdb_signal_to_string (signo
));
1470 debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1471 target_pid_to_str (ptid_of (thread
)),
1472 gdb_signal_to_string (signo
));
1474 return WSTOPSIG (status
);
1478 /* Detach from LWP. */
1481 linux_detach_one_lwp (struct lwp_info
*lwp
)
1483 struct thread_info
*thread
= get_lwp_thread (lwp
);
1487 /* If there is a pending SIGSTOP, get rid of it. */
1488 if (lwp
->stop_expected
)
1491 debug_printf ("Sending SIGCONT to %s\n",
1492 target_pid_to_str (ptid_of (thread
)));
1494 kill_lwp (lwpid_of (thread
), SIGCONT
);
1495 lwp
->stop_expected
= 0;
1498 /* Pass on any pending signal for this thread. */
1499 sig
= get_detach_signal (thread
);
1501 /* Preparing to resume may try to write registers, and fail if the
1502 lwp is zombie. If that happens, ignore the error. We'll handle
1503 it below, when detach fails with ESRCH. */
1506 /* Flush any pending changes to the process's registers. */
1507 regcache_invalidate_thread (thread
);
1509 /* Finally, let it resume. */
1510 if (the_low_target
.prepare_to_resume
!= NULL
)
1511 the_low_target
.prepare_to_resume (lwp
);
1513 catch (const gdb_exception_error
&ex
)
1515 if (!check_ptrace_stopped_lwp_gone (lwp
))
1519 lwpid
= lwpid_of (thread
);
1520 if (ptrace (PTRACE_DETACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0,
1521 (PTRACE_TYPE_ARG4
) (long) sig
) < 0)
1523 int save_errno
= errno
;
1525 /* We know the thread exists, so ESRCH must mean the lwp is
1526 zombie. This can happen if one of the already-detached
1527 threads exits the whole thread group. In that case we're
1528 still attached, and must reap the lwp. */
1529 if (save_errno
== ESRCH
)
1533 ret
= my_waitpid (lwpid
, &status
, __WALL
);
1536 warning (_("Couldn't reap LWP %d while detaching: %s"),
1537 lwpid
, safe_strerror (errno
));
1539 else if (!WIFEXITED (status
) && !WIFSIGNALED (status
))
1541 warning (_("Reaping LWP %d while detaching "
1542 "returned unexpected status 0x%x"),
1548 error (_("Can't detach %s: %s"),
1549 target_pid_to_str (ptid_of (thread
)),
1550 safe_strerror (save_errno
));
1553 else if (debug_threads
)
1555 debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)\n",
1556 target_pid_to_str (ptid_of (thread
)),
1563 /* Callback for for_each_thread. Detaches from non-leader threads of a
1567 linux_detach_lwp_callback (thread_info
*thread
)
1569 /* We don't actually detach from the thread group leader just yet.
1570 If the thread group exits, we must reap the zombie clone lwps
1571 before we're able to reap the leader. */
1572 if (thread
->id
.pid () == thread
->id
.lwp ())
1575 lwp_info
*lwp
= get_thread_lwp (thread
);
1576 linux_detach_one_lwp (lwp
);
1580 linux_process_target::detach (process_info
*process
)
1582 struct lwp_info
*main_lwp
;
1584 /* As there's a step over already in progress, let it finish first,
1585 otherwise nesting a stabilize_threads operation on top gets real
1587 complete_ongoing_step_over ();
1589 /* Stop all threads before detaching. First, ptrace requires that
1590 the thread is stopped to successfully detach. Second, thread_db
1591 may need to uninstall thread event breakpoints from memory, which
1592 only works with a stopped process anyway. */
1593 stop_all_lwps (0, NULL
);
1595 #ifdef USE_THREAD_DB
1596 thread_db_detach (process
);
1599 /* Stabilize threads (move out of jump pads). */
1600 target_stabilize_threads ();
1602 /* Detach from the clone lwps first. If the thread group exits just
1603 while we're detaching, we must reap the clone lwps before we're
1604 able to reap the leader. */
1605 for_each_thread (process
->pid
, linux_detach_lwp_callback
);
1607 main_lwp
= find_lwp_pid (ptid_t (process
->pid
));
1608 linux_detach_one_lwp (main_lwp
);
1612 /* Since we presently can only stop all lwps of all processes, we
1613 need to unstop lwps of other processes. */
1614 unstop_all_lwps (0, NULL
);
1618 /* Remove all LWPs that belong to process PROC from the lwp list. */
1621 linux_process_target::mourn (process_info
*process
)
1623 struct process_info_private
*priv
;
1625 #ifdef USE_THREAD_DB
1626 thread_db_mourn (process
);
1629 for_each_thread (process
->pid
, [] (thread_info
*thread
)
1631 delete_lwp (get_thread_lwp (thread
));
1634 /* Freeing all private data. */
1635 priv
= process
->priv
;
1636 if (the_low_target
.delete_process
!= NULL
)
1637 the_low_target
.delete_process (priv
->arch_private
);
1639 gdb_assert (priv
->arch_private
== NULL
);
1641 process
->priv
= NULL
;
1643 remove_process (process
);
1647 linux_process_target::join (int pid
)
1652 ret
= my_waitpid (pid
, &status
, 0);
1653 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1655 } while (ret
!= -1 || errno
!= ECHILD
);
1658 /* Return true if the given thread is still alive. */
1661 linux_process_target::thread_alive (ptid_t ptid
)
1663 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1665 /* We assume we always know if a thread exits. If a whole process
1666 exited but we still haven't been able to report it to GDB, we'll
1667 hold on to the last lwp of the dead process. */
1669 return !lwp_is_marked_dead (lwp
);
1675 linux_process_target::thread_still_has_status_pending (thread_info
*thread
)
1677 struct lwp_info
*lp
= get_thread_lwp (thread
);
1679 if (!lp
->status_pending_p
)
1682 if (thread
->last_resume_kind
!= resume_stop
1683 && (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1684 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
))
1686 struct thread_info
*saved_thread
;
1690 gdb_assert (lp
->last_status
!= 0);
1694 saved_thread
= current_thread
;
1695 current_thread
= thread
;
1697 if (pc
!= lp
->stop_pc
)
1700 debug_printf ("PC of %ld changed\n",
1705 #if !USE_SIGTRAP_SIGINFO
1706 else if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1707 && !(*the_low_target
.breakpoint_at
) (pc
))
1710 debug_printf ("previous SW breakpoint of %ld gone\n",
1714 else if (lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
1715 && !hardware_breakpoint_inserted_here (pc
))
1718 debug_printf ("previous HW breakpoint of %ld gone\n",
1724 current_thread
= saved_thread
;
1729 debug_printf ("discarding pending breakpoint status\n");
1730 lp
->status_pending_p
= 0;
1738 /* Returns true if LWP is resumed from the client's perspective. */
1741 lwp_resumed (struct lwp_info
*lwp
)
1743 struct thread_info
*thread
= get_lwp_thread (lwp
);
1745 if (thread
->last_resume_kind
!= resume_stop
)
1748 /* Did gdb send us a `vCont;t', but we haven't reported the
1749 corresponding stop to gdb yet? If so, the thread is still
1750 resumed/running from gdb's perspective. */
1751 if (thread
->last_resume_kind
== resume_stop
1752 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
1759 linux_process_target::status_pending_p_callback (thread_info
*thread
,
1762 struct lwp_info
*lp
= get_thread_lwp (thread
);
1764 /* Check if we're only interested in events from a specific process
1765 or a specific LWP. */
1766 if (!thread
->id
.matches (ptid
))
1769 if (!lwp_resumed (lp
))
1772 if (lp
->status_pending_p
1773 && !thread_still_has_status_pending (thread
))
1775 resume_one_lwp (lp
, lp
->stepping
, GDB_SIGNAL_0
, NULL
);
1779 return lp
->status_pending_p
;
1783 find_lwp_pid (ptid_t ptid
)
1785 thread_info
*thread
= find_thread ([&] (thread_info
*thr_arg
)
1787 int lwp
= ptid
.lwp () != 0 ? ptid
.lwp () : ptid
.pid ();
1788 return thr_arg
->id
.lwp () == lwp
;
1794 return get_thread_lwp (thread
);
1797 /* Return the number of known LWPs in the tgid given by PID. */
1804 for_each_thread (pid
, [&] (thread_info
*thread
)
1812 /* See nat/linux-nat.h. */
1815 iterate_over_lwps (ptid_t filter
,
1816 gdb::function_view
<iterate_over_lwps_ftype
> callback
)
1818 thread_info
*thread
= find_thread (filter
, [&] (thread_info
*thr_arg
)
1820 lwp_info
*lwp
= get_thread_lwp (thr_arg
);
1822 return callback (lwp
);
1828 return get_thread_lwp (thread
);
1831 /* Detect zombie thread group leaders, and "exit" them. We can't reap
1832 their exits until all other threads in the group have exited. */
1835 check_zombie_leaders (void)
1837 for_each_process ([] (process_info
*proc
) {
1838 pid_t leader_pid
= pid_of (proc
);
1839 struct lwp_info
*leader_lp
;
1841 leader_lp
= find_lwp_pid (ptid_t (leader_pid
));
1844 debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1845 "num_lwps=%d, zombie=%d\n",
1846 leader_pid
, leader_lp
!= NULL
, num_lwps (leader_pid
),
1847 linux_proc_pid_is_zombie (leader_pid
));
1849 if (leader_lp
!= NULL
&& !leader_lp
->stopped
1850 /* Check if there are other threads in the group, as we may
1851 have raced with the inferior simply exiting. */
1852 && !last_thread_of_process_p (leader_pid
)
1853 && linux_proc_pid_is_zombie (leader_pid
))
1855 /* A leader zombie can mean one of two things:
1857 - It exited, and there's an exit status pending
1858 available, or only the leader exited (not the whole
1859 program). In the latter case, we can't waitpid the
1860 leader's exit status until all other threads are gone.
1862 - There are 3 or more threads in the group, and a thread
1863 other than the leader exec'd. On an exec, the Linux
1864 kernel destroys all other threads (except the execing
1865 one) in the thread group, and resets the execing thread's
1866 tid to the tgid. No exit notification is sent for the
1867 execing thread -- from the ptracer's perspective, it
1868 appears as though the execing thread just vanishes.
1869 Until we reap all other threads except the leader and the
1870 execing thread, the leader will be zombie, and the
1871 execing thread will be in `D (disc sleep)'. As soon as
1872 all other threads are reaped, the execing thread changes
1873 it's tid to the tgid, and the previous (zombie) leader
1874 vanishes, giving place to the "new" leader. We could try
1875 distinguishing the exit and exec cases, by waiting once
1876 more, and seeing if something comes out, but it doesn't
1877 sound useful. The previous leader _does_ go away, and
1878 we'll re-add the new one once we see the exec event
1879 (which is just the same as what would happen if the
1880 previous leader did exit voluntarily before some other
1884 debug_printf ("CZL: Thread group leader %d zombie "
1885 "(it exited, or another thread execd).\n",
1888 delete_lwp (leader_lp
);
1893 /* Callback for `find_thread'. Returns the first LWP that is not
1897 not_stopped_callback (thread_info
*thread
, ptid_t filter
)
1899 if (!thread
->id
.matches (filter
))
1902 lwp_info
*lwp
= get_thread_lwp (thread
);
1904 return !lwp
->stopped
;
1907 /* Increment LWP's suspend count. */
1910 lwp_suspended_inc (struct lwp_info
*lwp
)
1914 if (debug_threads
&& lwp
->suspended
> 4)
1916 struct thread_info
*thread
= get_lwp_thread (lwp
);
1918 debug_printf ("LWP %ld has a suspiciously high suspend count,"
1919 " suspended=%d\n", lwpid_of (thread
), lwp
->suspended
);
1923 /* Decrement LWP's suspend count. */
1926 lwp_suspended_decr (struct lwp_info
*lwp
)
1930 if (lwp
->suspended
< 0)
1932 struct thread_info
*thread
= get_lwp_thread (lwp
);
1934 internal_error (__FILE__
, __LINE__
,
1935 "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread
),
1940 /* This function should only be called if the LWP got a SIGTRAP.
1942 Handle any tracepoint steps or hits. Return true if a tracepoint
1943 event was handled, 0 otherwise. */
1946 handle_tracepoints (struct lwp_info
*lwp
)
1948 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1949 int tpoint_related_event
= 0;
1951 gdb_assert (lwp
->suspended
== 0);
1953 /* If this tracepoint hit causes a tracing stop, we'll immediately
1954 uninsert tracepoints. To do this, we temporarily pause all
1955 threads, unpatch away, and then unpause threads. We need to make
1956 sure the unpausing doesn't resume LWP too. */
1957 lwp_suspended_inc (lwp
);
1959 /* And we need to be sure that any all-threads-stopping doesn't try
1960 to move threads out of the jump pads, as it could deadlock the
1961 inferior (LWP could be in the jump pad, maybe even holding the
1964 /* Do any necessary step collect actions. */
1965 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1967 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1969 /* See if we just hit a tracepoint and do its main collect
1971 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1973 lwp_suspended_decr (lwp
);
1975 gdb_assert (lwp
->suspended
== 0);
1976 gdb_assert (!stabilizing_threads
1977 || (lwp
->collecting_fast_tracepoint
1978 != fast_tpoint_collect_result::not_collecting
));
1980 if (tpoint_related_event
)
1983 debug_printf ("got a tracepoint event\n");
1990 /* Convenience wrapper. Returns information about LWP's fast tracepoint
1991 collection status. */
1993 static fast_tpoint_collect_result
1994 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
1995 struct fast_tpoint_collect_status
*status
)
1997 CORE_ADDR thread_area
;
1998 struct thread_info
*thread
= get_lwp_thread (lwp
);
2000 if (the_low_target
.get_thread_area
== NULL
)
2001 return fast_tpoint_collect_result::not_collecting
;
2003 /* Get the thread area address. This is used to recognize which
2004 thread is which when tracing with the in-process agent library.
2005 We don't read anything from the address, and treat it as opaque;
2006 it's the address itself that we assume is unique per-thread. */
2007 if ((*the_low_target
.get_thread_area
) (lwpid_of (thread
), &thread_area
) == -1)
2008 return fast_tpoint_collect_result::not_collecting
;
2010 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
2014 linux_process_target::maybe_move_out_of_jump_pad (lwp_info
*lwp
, int *wstat
)
2016 struct thread_info
*saved_thread
;
2018 saved_thread
= current_thread
;
2019 current_thread
= get_lwp_thread (lwp
);
2022 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
2023 && supports_fast_tracepoints ()
2024 && agent_loaded_p ())
2026 struct fast_tpoint_collect_status status
;
2029 debug_printf ("Checking whether LWP %ld needs to move out of the "
2031 lwpid_of (current_thread
));
2033 fast_tpoint_collect_result r
2034 = linux_fast_tracepoint_collecting (lwp
, &status
);
2037 || (WSTOPSIG (*wstat
) != SIGILL
2038 && WSTOPSIG (*wstat
) != SIGFPE
2039 && WSTOPSIG (*wstat
) != SIGSEGV
2040 && WSTOPSIG (*wstat
) != SIGBUS
))
2042 lwp
->collecting_fast_tracepoint
= r
;
2044 if (r
!= fast_tpoint_collect_result::not_collecting
)
2046 if (r
== fast_tpoint_collect_result::before_insn
2047 && lwp
->exit_jump_pad_bkpt
== NULL
)
2049 /* Haven't executed the original instruction yet.
2050 Set breakpoint there, and wait till it's hit,
2051 then single-step until exiting the jump pad. */
2052 lwp
->exit_jump_pad_bkpt
2053 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
2057 debug_printf ("Checking whether LWP %ld needs to move out of "
2058 "the jump pad...it does\n",
2059 lwpid_of (current_thread
));
2060 current_thread
= saved_thread
;
2067 /* If we get a synchronous signal while collecting, *and*
2068 while executing the (relocated) original instruction,
2069 reset the PC to point at the tpoint address, before
2070 reporting to GDB. Otherwise, it's an IPA lib bug: just
2071 report the signal to GDB, and pray for the best. */
2073 lwp
->collecting_fast_tracepoint
2074 = fast_tpoint_collect_result::not_collecting
;
2076 if (r
!= fast_tpoint_collect_result::not_collecting
2077 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
2078 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
2081 struct regcache
*regcache
;
2083 /* The si_addr on a few signals references the address
2084 of the faulting instruction. Adjust that as
2086 if ((WSTOPSIG (*wstat
) == SIGILL
2087 || WSTOPSIG (*wstat
) == SIGFPE
2088 || WSTOPSIG (*wstat
) == SIGBUS
2089 || WSTOPSIG (*wstat
) == SIGSEGV
)
2090 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
2091 (PTRACE_TYPE_ARG3
) 0, &info
) == 0
2092 /* Final check just to make sure we don't clobber
2093 the siginfo of non-kernel-sent signals. */
2094 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
2096 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
2097 ptrace (PTRACE_SETSIGINFO
, lwpid_of (current_thread
),
2098 (PTRACE_TYPE_ARG3
) 0, &info
);
2101 regcache
= get_thread_regcache (current_thread
, 1);
2102 low_set_pc (regcache
, status
.tpoint_addr
);
2103 lwp
->stop_pc
= status
.tpoint_addr
;
2105 /* Cancel any fast tracepoint lock this thread was
2107 force_unlock_trace_buffer ();
2110 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
2113 debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
2114 "stopping all threads momentarily.\n");
2116 stop_all_lwps (1, lwp
);
2118 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
2119 lwp
->exit_jump_pad_bkpt
= NULL
;
2121 unstop_all_lwps (1, lwp
);
2123 gdb_assert (lwp
->suspended
>= 0);
2129 debug_printf ("Checking whether LWP %ld needs to move out of the "
2131 lwpid_of (current_thread
));
2133 current_thread
= saved_thread
;
2137 /* Enqueue one signal in the "signals to report later when out of the
2141 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2143 struct pending_signals
*p_sig
;
2144 struct thread_info
*thread
= get_lwp_thread (lwp
);
2147 debug_printf ("Deferring signal %d for LWP %ld.\n",
2148 WSTOPSIG (*wstat
), lwpid_of (thread
));
2152 struct pending_signals
*sig
;
2154 for (sig
= lwp
->pending_signals_to_report
;
2157 debug_printf (" Already queued %d\n",
2160 debug_printf (" (no more currently queued signals)\n");
2163 /* Don't enqueue non-RT signals if they are already in the deferred
2164 queue. (SIGSTOP being the easiest signal to see ending up here
2166 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
2168 struct pending_signals
*sig
;
2170 for (sig
= lwp
->pending_signals_to_report
;
2174 if (sig
->signal
== WSTOPSIG (*wstat
))
2177 debug_printf ("Not requeuing already queued non-RT signal %d"
2186 p_sig
= XCNEW (struct pending_signals
);
2187 p_sig
->prev
= lwp
->pending_signals_to_report
;
2188 p_sig
->signal
= WSTOPSIG (*wstat
);
2190 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2193 lwp
->pending_signals_to_report
= p_sig
;
2196 /* Dequeue one signal from the "signals to report later when out of
2197 the jump pad" list. */
2200 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2202 struct thread_info
*thread
= get_lwp_thread (lwp
);
2204 if (lwp
->pending_signals_to_report
!= NULL
)
2206 struct pending_signals
**p_sig
;
2208 p_sig
= &lwp
->pending_signals_to_report
;
2209 while ((*p_sig
)->prev
!= NULL
)
2210 p_sig
= &(*p_sig
)->prev
;
2212 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
2213 if ((*p_sig
)->info
.si_signo
!= 0)
2214 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2220 debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
2221 WSTOPSIG (*wstat
), lwpid_of (thread
));
2225 struct pending_signals
*sig
;
2227 for (sig
= lwp
->pending_signals_to_report
;
2230 debug_printf (" Still queued %d\n",
2233 debug_printf (" (no more queued signals)\n");
2242 /* Fetch the possibly triggered data watchpoint info and store it in
2245 On some archs, like x86, that use debug registers to set
2246 watchpoints, it's possible that the way to know which watched
2247 address trapped, is to check the register that is used to select
2248 which address to watch. Problem is, between setting the watchpoint
2249 and reading back which data address trapped, the user may change
2250 the set of watchpoints, and, as a consequence, GDB changes the
2251 debug registers in the inferior. To avoid reading back a stale
2252 stopped-data-address when that happens, we cache in LP the fact
2253 that a watchpoint trapped, and the corresponding data address, as
2254 soon as we see CHILD stop with a SIGTRAP. If GDB changes the debug
2255 registers meanwhile, we have the cached data we can rely on. */
2258 check_stopped_by_watchpoint (struct lwp_info
*child
)
2260 if (the_low_target
.stopped_by_watchpoint
!= NULL
)
2262 struct thread_info
*saved_thread
;
2264 saved_thread
= current_thread
;
2265 current_thread
= get_lwp_thread (child
);
2267 if (the_low_target
.stopped_by_watchpoint ())
2269 child
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2271 if (the_low_target
.stopped_data_address
!= NULL
)
2272 child
->stopped_data_address
2273 = the_low_target
.stopped_data_address ();
2275 child
->stopped_data_address
= 0;
2278 current_thread
= saved_thread
;
2281 return child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2284 /* Return the ptrace options that we want to try to enable. */
2287 linux_low_ptrace_options (int attached
)
2289 client_state
&cs
= get_client_state ();
2293 options
|= PTRACE_O_EXITKILL
;
2295 if (cs
.report_fork_events
)
2296 options
|= PTRACE_O_TRACEFORK
;
2298 if (cs
.report_vfork_events
)
2299 options
|= (PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEVFORKDONE
);
2301 if (cs
.report_exec_events
)
2302 options
|= PTRACE_O_TRACEEXEC
;
2304 options
|= PTRACE_O_TRACESYSGOOD
;
2310 linux_process_target::filter_event (int lwpid
, int wstat
)
2312 client_state
&cs
= get_client_state ();
2313 struct lwp_info
*child
;
2314 struct thread_info
*thread
;
2315 int have_stop_pc
= 0;
2317 child
= find_lwp_pid (ptid_t (lwpid
));
2319 /* Check for stop events reported by a process we didn't already
2320 know about - anything not already in our LWP list.
2322 If we're expecting to receive stopped processes after
2323 fork, vfork, and clone events, then we'll just add the
2324 new one to our list and go back to waiting for the event
2325 to be reported - the stopped process might be returned
2326 from waitpid before or after the event is.
2328 But note the case of a non-leader thread exec'ing after the
2329 leader having exited, and gone from our lists (because
2330 check_zombie_leaders deleted it). The non-leader thread
2331 changes its tid to the tgid. */
2333 if (WIFSTOPPED (wstat
) && child
== NULL
&& WSTOPSIG (wstat
) == SIGTRAP
2334 && linux_ptrace_get_extended_event (wstat
) == PTRACE_EVENT_EXEC
)
2338 /* A multi-thread exec after we had seen the leader exiting. */
2341 debug_printf ("LLW: Re-adding thread group leader LWP %d"
2342 "after exec.\n", lwpid
);
2345 child_ptid
= ptid_t (lwpid
, lwpid
, 0);
2346 child
= add_lwp (child_ptid
);
2348 current_thread
= child
->thread
;
2351 /* If we didn't find a process, one of two things presumably happened:
2352 - A process we started and then detached from has exited. Ignore it.
2353 - A process we are controlling has forked and the new child's stop
2354 was reported to us by the kernel. Save its PID. */
2355 if (child
== NULL
&& WIFSTOPPED (wstat
))
2357 add_to_pid_list (&stopped_pids
, lwpid
, wstat
);
2360 else if (child
== NULL
)
2363 thread
= get_lwp_thread (child
);
2367 child
->last_status
= wstat
;
2369 /* Check if the thread has exited. */
2370 if ((WIFEXITED (wstat
) || WIFSIGNALED (wstat
)))
2373 debug_printf ("LLFE: %d exited.\n", lwpid
);
2375 if (finish_step_over (child
))
2377 /* Unsuspend all other LWPs, and set them back running again. */
2378 unsuspend_all_lwps (child
);
2381 /* If there is at least one more LWP, then the exit signal was
2382 not the end of the debugged application and should be
2383 ignored, unless GDB wants to hear about thread exits. */
2384 if (cs
.report_thread_events
2385 || last_thread_of_process_p (pid_of (thread
)))
2387 /* Since events are serialized to GDB core, and we can't
2388 report this one right now. Leave the status pending for
2389 the next time we're able to report it. */
2390 mark_lwp_dead (child
, wstat
);
2400 gdb_assert (WIFSTOPPED (wstat
));
2402 if (WIFSTOPPED (wstat
))
2404 struct process_info
*proc
;
2406 /* Architecture-specific setup after inferior is running. */
2407 proc
= find_process_pid (pid_of (thread
));
2408 if (proc
->tdesc
== NULL
)
2412 /* This needs to happen after we have attached to the
2413 inferior and it is stopped for the first time, but
2414 before we access any inferior registers. */
2415 arch_setup_thread (thread
);
2419 /* The process is started, but GDBserver will do
2420 architecture-specific setup after the program stops at
2421 the first instruction. */
2422 child
->status_pending_p
= 1;
2423 child
->status_pending
= wstat
;
2429 if (WIFSTOPPED (wstat
) && child
->must_set_ptrace_flags
)
2431 struct process_info
*proc
= find_process_pid (pid_of (thread
));
2432 int options
= linux_low_ptrace_options (proc
->attached
);
2434 linux_enable_event_reporting (lwpid
, options
);
2435 child
->must_set_ptrace_flags
= 0;
2438 /* Always update syscall_state, even if it will be filtered later. */
2439 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SYSCALL_SIGTRAP
)
2441 child
->syscall_state
2442 = (child
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2443 ? TARGET_WAITKIND_SYSCALL_RETURN
2444 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2448 /* Almost all other ptrace-stops are known to be outside of system
2449 calls, with further exceptions in handle_extended_wait. */
2450 child
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2453 /* Be careful to not overwrite stop_pc until save_stop_reason is
2455 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
2456 && linux_is_extended_waitstatus (wstat
))
2458 child
->stop_pc
= get_pc (child
);
2459 if (handle_extended_wait (&child
, wstat
))
2461 /* The event has been handled, so just return without
2467 if (linux_wstatus_maybe_breakpoint (wstat
))
2469 if (save_stop_reason (child
))
2474 child
->stop_pc
= get_pc (child
);
2476 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGSTOP
2477 && child
->stop_expected
)
2480 debug_printf ("Expected stop.\n");
2481 child
->stop_expected
= 0;
2483 if (thread
->last_resume_kind
== resume_stop
)
2485 /* We want to report the stop to the core. Treat the
2486 SIGSTOP as a normal event. */
2488 debug_printf ("LLW: resume_stop SIGSTOP caught for %s.\n",
2489 target_pid_to_str (ptid_of (thread
)));
2491 else if (stopping_threads
!= NOT_STOPPING_THREADS
)
2493 /* Stopping threads. We don't want this SIGSTOP to end up
2496 debug_printf ("LLW: SIGSTOP caught for %s "
2497 "while stopping threads.\n",
2498 target_pid_to_str (ptid_of (thread
)));
2503 /* This is a delayed SIGSTOP. Filter out the event. */
2505 debug_printf ("LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2506 child
->stepping
? "step" : "continue",
2507 target_pid_to_str (ptid_of (thread
)));
2509 resume_one_lwp (child
, child
->stepping
, 0, NULL
);
2514 child
->status_pending_p
= 1;
2515 child
->status_pending
= wstat
;
2519 /* Return true if THREAD is doing hardware single step. */
2522 maybe_hw_step (struct thread_info
*thread
)
2524 if (can_hardware_single_step ())
2528 /* GDBserver must insert single-step breakpoint for software
2530 gdb_assert (has_single_step_breakpoints (thread
));
2536 linux_process_target::resume_stopped_resumed_lwps (thread_info
*thread
)
2538 struct lwp_info
*lp
= get_thread_lwp (thread
);
2542 && !lp
->status_pending_p
2543 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2547 if (thread
->last_resume_kind
== resume_step
)
2548 step
= maybe_hw_step (thread
);
2551 debug_printf ("RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
2552 target_pid_to_str (ptid_of (thread
)),
2553 paddress (lp
->stop_pc
),
2556 resume_one_lwp (lp
, step
, GDB_SIGNAL_0
, NULL
);
2561 linux_process_target::wait_for_event_filtered (ptid_t wait_ptid
,
2563 int *wstatp
, int options
)
2565 struct thread_info
*event_thread
;
2566 struct lwp_info
*event_child
, *requested_child
;
2567 sigset_t block_mask
, prev_mask
;
2570 /* N.B. event_thread points to the thread_info struct that contains
2571 event_child. Keep them in sync. */
2572 event_thread
= NULL
;
2574 requested_child
= NULL
;
2576 /* Check for a lwp with a pending status. */
2578 if (filter_ptid
== minus_one_ptid
|| filter_ptid
.is_pid ())
2580 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2582 return status_pending_p_callback (thread
, filter_ptid
);
2585 if (event_thread
!= NULL
)
2586 event_child
= get_thread_lwp (event_thread
);
2587 if (debug_threads
&& event_thread
)
2588 debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread
));
2590 else if (filter_ptid
!= null_ptid
)
2592 requested_child
= find_lwp_pid (filter_ptid
);
2594 if (stopping_threads
== NOT_STOPPING_THREADS
2595 && requested_child
->status_pending_p
2596 && (requested_child
->collecting_fast_tracepoint
2597 != fast_tpoint_collect_result::not_collecting
))
2599 enqueue_one_deferred_signal (requested_child
,
2600 &requested_child
->status_pending
);
2601 requested_child
->status_pending_p
= 0;
2602 requested_child
->status_pending
= 0;
2603 resume_one_lwp (requested_child
, 0, 0, NULL
);
2606 if (requested_child
->suspended
2607 && requested_child
->status_pending_p
)
2609 internal_error (__FILE__
, __LINE__
,
2610 "requesting an event out of a"
2611 " suspended child?");
2614 if (requested_child
->status_pending_p
)
2616 event_child
= requested_child
;
2617 event_thread
= get_lwp_thread (event_child
);
2621 if (event_child
!= NULL
)
2624 debug_printf ("Got an event from pending child %ld (%04x)\n",
2625 lwpid_of (event_thread
), event_child
->status_pending
);
2626 *wstatp
= event_child
->status_pending
;
2627 event_child
->status_pending_p
= 0;
2628 event_child
->status_pending
= 0;
2629 current_thread
= event_thread
;
2630 return lwpid_of (event_thread
);
2633 /* But if we don't find a pending event, we'll have to wait.
2635 We only enter this loop if no process has a pending wait status.
2636 Thus any action taken in response to a wait status inside this
2637 loop is responding as soon as we detect the status, not after any
2640 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2641 all signals while here. */
2642 sigfillset (&block_mask
);
2643 gdb_sigmask (SIG_BLOCK
, &block_mask
, &prev_mask
);
2645 /* Always pull all events out of the kernel. We'll randomly select
2646 an event LWP out of all that have events, to prevent
2648 while (event_child
== NULL
)
2652 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2655 - If the thread group leader exits while other threads in the
2656 thread group still exist, waitpid(TGID, ...) hangs. That
2657 waitpid won't return an exit status until the other threads
2658 in the group are reaped.
2660 - When a non-leader thread execs, that thread just vanishes
2661 without reporting an exit (so we'd hang if we waited for it
2662 explicitly in that case). The exec event is reported to
2665 ret
= my_waitpid (-1, wstatp
, options
| WNOHANG
);
2668 debug_printf ("LWFE: waitpid(-1, ...) returned %d, %s\n",
2669 ret
, errno
? safe_strerror (errno
) : "ERRNO-OK");
2675 debug_printf ("LLW: waitpid %ld received %s\n",
2676 (long) ret
, status_to_str (*wstatp
));
2679 /* Filter all events. IOW, leave all events pending. We'll
2680 randomly select an event LWP out of all that have events
2682 filter_event (ret
, *wstatp
);
2683 /* Retry until nothing comes out of waitpid. A single
2684 SIGCHLD can indicate more than one child stopped. */
2688 /* Now that we've pulled all events out of the kernel, resume
2689 LWPs that don't have an interesting event to report. */
2690 if (stopping_threads
== NOT_STOPPING_THREADS
)
2691 for_each_thread ([this] (thread_info
*thread
)
2693 resume_stopped_resumed_lwps (thread
);
2696 /* ... and find an LWP with a status to report to the core, if
2698 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2700 return status_pending_p_callback (thread
, filter_ptid
);
2703 if (event_thread
!= NULL
)
2705 event_child
= get_thread_lwp (event_thread
);
2706 *wstatp
= event_child
->status_pending
;
2707 event_child
->status_pending_p
= 0;
2708 event_child
->status_pending
= 0;
2712 /* Check for zombie thread group leaders. Those can't be reaped
2713 until all other threads in the thread group are. */
2714 check_zombie_leaders ();
2716 auto not_stopped
= [&] (thread_info
*thread
)
2718 return not_stopped_callback (thread
, wait_ptid
);
2721 /* If there are no resumed children left in the set of LWPs we
2722 want to wait for, bail. We can't just block in
2723 waitpid/sigsuspend, because lwps might have been left stopped
2724 in trace-stop state, and we'd be stuck forever waiting for
2725 their status to change (which would only happen if we resumed
2726 them). Even if WNOHANG is set, this return code is preferred
2727 over 0 (below), as it is more detailed. */
2728 if (find_thread (not_stopped
) == NULL
)
2731 debug_printf ("LLW: exit (no unwaited-for LWP)\n");
2732 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2736 /* No interesting event to report to the caller. */
2737 if ((options
& WNOHANG
))
2740 debug_printf ("WNOHANG set, no event found\n");
2742 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2746 /* Block until we get an event reported with SIGCHLD. */
2748 debug_printf ("sigsuspend'ing\n");
2750 sigsuspend (&prev_mask
);
2751 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2755 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2757 current_thread
= event_thread
;
2759 return lwpid_of (event_thread
);
2763 linux_process_target::wait_for_event (ptid_t ptid
, int *wstatp
, int options
)
2765 return wait_for_event_filtered (ptid
, ptid
, wstatp
, options
);
2768 /* Select one LWP out of those that have events pending. */
2771 select_event_lwp (struct lwp_info
**orig_lp
)
2773 struct thread_info
*event_thread
= NULL
;
2775 /* In all-stop, give preference to the LWP that is being
2776 single-stepped. There will be at most one, and it's the LWP that
2777 the core is most interested in. If we didn't do this, then we'd
2778 have to handle pending step SIGTRAPs somehow in case the core
2779 later continues the previously-stepped thread, otherwise we'd
2780 report the pending SIGTRAP, and the core, not having stepped the
2781 thread, wouldn't understand what the trap was for, and therefore
2782 would report it to the user as a random signal. */
2785 event_thread
= find_thread ([] (thread_info
*thread
)
2787 lwp_info
*lp
= get_thread_lwp (thread
);
2789 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2790 && thread
->last_resume_kind
== resume_step
2791 && lp
->status_pending_p
);
2794 if (event_thread
!= NULL
)
2797 debug_printf ("SEL: Select single-step %s\n",
2798 target_pid_to_str (ptid_of (event_thread
)));
2801 if (event_thread
== NULL
)
2803 /* No single-stepping LWP. Select one at random, out of those
2804 which have had events. */
2806 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2808 lwp_info
*lp
= get_thread_lwp (thread
);
2810 /* Only resumed LWPs that have an event pending. */
2811 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2812 && lp
->status_pending_p
);
2816 if (event_thread
!= NULL
)
2818 struct lwp_info
*event_lp
= get_thread_lwp (event_thread
);
2820 /* Switch the event LWP. */
2821 *orig_lp
= event_lp
;
2825 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2829 unsuspend_all_lwps (struct lwp_info
*except
)
2831 for_each_thread ([&] (thread_info
*thread
)
2833 lwp_info
*lwp
= get_thread_lwp (thread
);
2836 lwp_suspended_decr (lwp
);
2840 static bool stuck_in_jump_pad_callback (thread_info
*thread
);
2841 static bool lwp_running (thread_info
*thread
);
2843 /* Stabilize threads (move out of jump pads).
2845 If a thread is midway collecting a fast tracepoint, we need to
2846 finish the collection and move it out of the jump pad before
2847 reporting the signal.
2849 This avoids recursion while collecting (when a signal arrives
2850 midway, and the signal handler itself collects), which would trash
2851 the trace buffer. In case the user set a breakpoint in a signal
2852 handler, this avoids the backtrace showing the jump pad, etc..
2853 Most importantly, there are certain things we can't do safely if
2854 threads are stopped in a jump pad (or in its callee's). For
2857 - starting a new trace run. A thread still collecting the
2858 previous run, could trash the trace buffer when resumed. The trace
2859 buffer control structures would have been reset but the thread had
2860 no way to tell. The thread could even midway memcpy'ing to the
2861 buffer, which would mean that when resumed, it would clobber the
2862 trace buffer that had been set for a new run.
2864 - we can't rewrite/reuse the jump pads for new tracepoints
2865 safely. Say you do tstart while a thread is stopped midway while
2866 collecting. When the thread is later resumed, it finishes the
2867 collection, and returns to the jump pad, to execute the original
2868 instruction that was under the tracepoint jump at the time the
2869 older run had been started. If the jump pad had been rewritten
2870 since for something else in the new run, the thread would now
2871 execute the wrong / random instructions. */
2874 linux_process_target::stabilize_threads ()
2876 thread_info
*thread_stuck
= find_thread (stuck_in_jump_pad_callback
);
2878 if (thread_stuck
!= NULL
)
2881 debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
2882 lwpid_of (thread_stuck
));
2886 thread_info
*saved_thread
= current_thread
;
2888 stabilizing_threads
= 1;
2891 for_each_thread ([this] (thread_info
*thread
)
2893 move_out_of_jump_pad (thread
);
2896 /* Loop until all are stopped out of the jump pads. */
2897 while (find_thread (lwp_running
) != NULL
)
2899 struct target_waitstatus ourstatus
;
2900 struct lwp_info
*lwp
;
2903 /* Note that we go through the full wait even loop. While
2904 moving threads out of jump pad, we need to be able to step
2905 over internal breakpoints and such. */
2906 wait_1 (minus_one_ptid
, &ourstatus
, 0);
2908 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
2910 lwp
= get_thread_lwp (current_thread
);
2913 lwp_suspended_inc (lwp
);
2915 if (ourstatus
.value
.sig
!= GDB_SIGNAL_0
2916 || current_thread
->last_resume_kind
== resume_stop
)
2918 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.value
.sig
));
2919 enqueue_one_deferred_signal (lwp
, &wstat
);
2924 unsuspend_all_lwps (NULL
);
2926 stabilizing_threads
= 0;
2928 current_thread
= saved_thread
;
2932 thread_stuck
= find_thread (stuck_in_jump_pad_callback
);
2934 if (thread_stuck
!= NULL
)
2935 debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
2936 lwpid_of (thread_stuck
));
2940 /* Convenience function that is called when the kernel reports an
2941 event that is not passed out to GDB. */
2944 ignore_event (struct target_waitstatus
*ourstatus
)
2946 /* If we got an event, there may still be others, as a single
2947 SIGCHLD can indicate more than one child stopped. This forces
2948 another target_wait call. */
2951 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2955 /* Convenience function that is called when the kernel reports an exit
2956 event. This decides whether to report the event to GDB as a
2957 process exit event, a thread exit event, or to suppress the
2961 filter_exit_event (struct lwp_info
*event_child
,
2962 struct target_waitstatus
*ourstatus
)
2964 client_state
&cs
= get_client_state ();
2965 struct thread_info
*thread
= get_lwp_thread (event_child
);
2966 ptid_t ptid
= ptid_of (thread
);
2968 if (!last_thread_of_process_p (pid_of (thread
)))
2970 if (cs
.report_thread_events
)
2971 ourstatus
->kind
= TARGET_WAITKIND_THREAD_EXITED
;
2973 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2975 delete_lwp (event_child
);
2980 /* Returns 1 if GDB is interested in any event_child syscalls. */
2983 gdb_catching_syscalls_p (struct lwp_info
*event_child
)
2985 struct thread_info
*thread
= get_lwp_thread (event_child
);
2986 struct process_info
*proc
= get_thread_process (thread
);
2988 return !proc
->syscalls_to_catch
.empty ();
2991 /* Returns 1 if GDB is interested in the event_child syscall.
2992 Only to be called when stopped reason is SYSCALL_SIGTRAP. */
2995 gdb_catch_this_syscall_p (struct lwp_info
*event_child
)
2998 struct thread_info
*thread
= get_lwp_thread (event_child
);
2999 struct process_info
*proc
= get_thread_process (thread
);
3001 if (proc
->syscalls_to_catch
.empty ())
3004 if (proc
->syscalls_to_catch
[0] == ANY_SYSCALL
)
3007 get_syscall_trapinfo (event_child
, &sysno
);
3009 for (int iter
: proc
->syscalls_to_catch
)
3017 linux_process_target::wait_1 (ptid_t ptid
, target_waitstatus
*ourstatus
,
3020 client_state
&cs
= get_client_state ();
3022 struct lwp_info
*event_child
;
3025 int step_over_finished
;
3026 int bp_explains_trap
;
3027 int maybe_internal_trap
;
3036 debug_printf ("wait_1: [%s]\n", target_pid_to_str (ptid
));
3039 /* Translate generic target options into linux options. */
3041 if (target_options
& TARGET_WNOHANG
)
3044 bp_explains_trap
= 0;
3047 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3049 auto status_pending_p_any
= [&] (thread_info
*thread
)
3051 return status_pending_p_callback (thread
, minus_one_ptid
);
3054 auto not_stopped
= [&] (thread_info
*thread
)
3056 return not_stopped_callback (thread
, minus_one_ptid
);
3059 /* Find a resumed LWP, if any. */
3060 if (find_thread (status_pending_p_any
) != NULL
)
3062 else if (find_thread (not_stopped
) != NULL
)
3067 if (step_over_bkpt
== null_ptid
)
3068 pid
= wait_for_event (ptid
, &w
, options
);
3072 debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
3073 target_pid_to_str (step_over_bkpt
));
3074 pid
= wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
3077 if (pid
== 0 || (pid
== -1 && !any_resumed
))
3079 gdb_assert (target_options
& TARGET_WNOHANG
);
3083 debug_printf ("wait_1 ret = null_ptid, "
3084 "TARGET_WAITKIND_IGNORE\n");
3088 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3095 debug_printf ("wait_1 ret = null_ptid, "
3096 "TARGET_WAITKIND_NO_RESUMED\n");
3100 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3104 event_child
= get_thread_lwp (current_thread
);
3106 /* wait_for_event only returns an exit status for the last
3107 child of a process. Report it. */
3108 if (WIFEXITED (w
) || WIFSIGNALED (w
))
3112 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
3113 ourstatus
->value
.integer
= WEXITSTATUS (w
);
3117 debug_printf ("wait_1 ret = %s, exited with "
3119 target_pid_to_str (ptid_of (current_thread
)),
3126 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
3127 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (w
));
3131 debug_printf ("wait_1 ret = %s, terminated with "
3133 target_pid_to_str (ptid_of (current_thread
)),
3139 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3140 return filter_exit_event (event_child
, ourstatus
);
3142 return ptid_of (current_thread
);
3145 /* If step-over executes a breakpoint instruction, in the case of a
3146 hardware single step it means a gdb/gdbserver breakpoint had been
3147 planted on top of a permanent breakpoint, in the case of a software
3148 single step it may just mean that gdbserver hit the reinsert breakpoint.
3149 The PC has been adjusted by save_stop_reason to point at
3150 the breakpoint address.
3151 So in the case of the hardware single step advance the PC manually
3152 past the breakpoint and in the case of software single step advance only
3153 if it's not the single_step_breakpoint we are hitting.
3154 This avoids that a program would keep trapping a permanent breakpoint
3156 if (step_over_bkpt
!= null_ptid
3157 && event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3158 && (event_child
->stepping
3159 || !single_step_breakpoint_inserted_here (event_child
->stop_pc
)))
3161 int increment_pc
= 0;
3162 int breakpoint_kind
= 0;
3163 CORE_ADDR stop_pc
= event_child
->stop_pc
;
3165 breakpoint_kind
= breakpoint_kind_from_current_state (&stop_pc
);
3166 sw_breakpoint_from_kind (breakpoint_kind
, &increment_pc
);
3170 debug_printf ("step-over for %s executed software breakpoint\n",
3171 target_pid_to_str (ptid_of (current_thread
)));
3174 if (increment_pc
!= 0)
3176 struct regcache
*regcache
3177 = get_thread_regcache (current_thread
, 1);
3179 event_child
->stop_pc
+= increment_pc
;
3180 low_set_pc (regcache
, event_child
->stop_pc
);
3182 if (!(*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))
3183 event_child
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
3187 /* If this event was not handled before, and is not a SIGTRAP, we
3188 report it. SIGILL and SIGSEGV are also treated as traps in case
3189 a breakpoint is inserted at the current PC. If this target does
3190 not support internal breakpoints at all, we also report the
3191 SIGTRAP without further processing; it's of no concern to us. */
3193 = (low_supports_breakpoints ()
3194 && (WSTOPSIG (w
) == SIGTRAP
3195 || ((WSTOPSIG (w
) == SIGILL
3196 || WSTOPSIG (w
) == SIGSEGV
)
3197 && (*the_low_target
.breakpoint_at
) (event_child
->stop_pc
))));
3199 if (maybe_internal_trap
)
3201 /* Handle anything that requires bookkeeping before deciding to
3202 report the event or continue waiting. */
3204 /* First check if we can explain the SIGTRAP with an internal
3205 breakpoint, or if we should possibly report the event to GDB.
3206 Do this before anything that may remove or insert a
3208 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
3210 /* We have a SIGTRAP, possibly a step-over dance has just
3211 finished. If so, tweak the state machine accordingly,
3212 reinsert breakpoints and delete any single-step
3214 step_over_finished
= finish_step_over (event_child
);
3216 /* Now invoke the callbacks of any internal breakpoints there. */
3217 check_breakpoints (event_child
->stop_pc
);
3219 /* Handle tracepoint data collecting. This may overflow the
3220 trace buffer, and cause a tracing stop, removing
3222 trace_event
= handle_tracepoints (event_child
);
3224 if (bp_explains_trap
)
3227 debug_printf ("Hit a gdbserver breakpoint.\n");
3232 /* We have some other signal, possibly a step-over dance was in
3233 progress, and it should be cancelled too. */
3234 step_over_finished
= finish_step_over (event_child
);
3237 /* We have all the data we need. Either report the event to GDB, or
3238 resume threads and keep waiting for more. */
3240 /* If we're collecting a fast tracepoint, finish the collection and
3241 move out of the jump pad before delivering a signal. See
3242 linux_stabilize_threads. */
3245 && WSTOPSIG (w
) != SIGTRAP
3246 && supports_fast_tracepoints ()
3247 && agent_loaded_p ())
3250 debug_printf ("Got signal %d for LWP %ld. Check if we need "
3251 "to defer or adjust it.\n",
3252 WSTOPSIG (w
), lwpid_of (current_thread
));
3254 /* Allow debugging the jump pad itself. */
3255 if (current_thread
->last_resume_kind
!= resume_step
3256 && maybe_move_out_of_jump_pad (event_child
, &w
))
3258 enqueue_one_deferred_signal (event_child
, &w
);
3261 debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
3262 WSTOPSIG (w
), lwpid_of (current_thread
));
3264 resume_one_lwp (event_child
, 0, 0, NULL
);
3268 return ignore_event (ourstatus
);
3272 if (event_child
->collecting_fast_tracepoint
3273 != fast_tpoint_collect_result::not_collecting
)
3276 debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
3277 "Check if we're already there.\n",
3278 lwpid_of (current_thread
),
3279 (int) event_child
->collecting_fast_tracepoint
);
3283 event_child
->collecting_fast_tracepoint
3284 = linux_fast_tracepoint_collecting (event_child
, NULL
);
3286 if (event_child
->collecting_fast_tracepoint
3287 != fast_tpoint_collect_result::before_insn
)
3289 /* No longer need this breakpoint. */
3290 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
3293 debug_printf ("No longer need exit-jump-pad bkpt; removing it."
3294 "stopping all threads momentarily.\n");
3296 /* Other running threads could hit this breakpoint.
3297 We don't handle moribund locations like GDB does,
3298 instead we always pause all threads when removing
3299 breakpoints, so that any step-over or
3300 decr_pc_after_break adjustment is always taken
3301 care of while the breakpoint is still
3303 stop_all_lwps (1, event_child
);
3305 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
3306 event_child
->exit_jump_pad_bkpt
= NULL
;
3308 unstop_all_lwps (1, event_child
);
3310 gdb_assert (event_child
->suspended
>= 0);
3314 if (event_child
->collecting_fast_tracepoint
3315 == fast_tpoint_collect_result::not_collecting
)
3318 debug_printf ("fast tracepoint finished "
3319 "collecting successfully.\n");
3321 /* We may have a deferred signal to report. */
3322 if (dequeue_one_deferred_signal (event_child
, &w
))
3325 debug_printf ("dequeued one signal.\n");
3330 debug_printf ("no deferred signals.\n");
3332 if (stabilizing_threads
)
3334 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3335 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3339 debug_printf ("wait_1 ret = %s, stopped "
3340 "while stabilizing threads\n",
3341 target_pid_to_str (ptid_of (current_thread
)));
3345 return ptid_of (current_thread
);
3351 /* Check whether GDB would be interested in this event. */
3353 /* Check if GDB is interested in this syscall. */
3355 && WSTOPSIG (w
) == SYSCALL_SIGTRAP
3356 && !gdb_catch_this_syscall_p (event_child
))
3360 debug_printf ("Ignored syscall for LWP %ld.\n",
3361 lwpid_of (current_thread
));
3364 resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
3368 return ignore_event (ourstatus
);
3371 /* If GDB is not interested in this signal, don't stop other
3372 threads, and don't report it to GDB. Just resume the inferior
3373 right away. We do this for threading-related signals as well as
3374 any that GDB specifically requested we ignore. But never ignore
3375 SIGSTOP if we sent it ourselves, and do not ignore signals when
3376 stepping - they may require special handling to skip the signal
3377 handler. Also never ignore signals that could be caused by a
3380 && current_thread
->last_resume_kind
!= resume_step
3382 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3383 (current_process ()->priv
->thread_db
!= NULL
3384 && (WSTOPSIG (w
) == __SIGRTMIN
3385 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
3388 (cs
.pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
3389 && !(WSTOPSIG (w
) == SIGSTOP
3390 && current_thread
->last_resume_kind
== resume_stop
)
3391 && !linux_wstatus_maybe_breakpoint (w
))))
3393 siginfo_t info
, *info_p
;
3396 debug_printf ("Ignored signal %d for LWP %ld.\n",
3397 WSTOPSIG (w
), lwpid_of (current_thread
));
3399 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
3400 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
3405 if (step_over_finished
)
3407 /* We cancelled this thread's step-over above. We still
3408 need to unsuspend all other LWPs, and set them back
3409 running again while the signal handler runs. */
3410 unsuspend_all_lwps (event_child
);
3412 /* Enqueue the pending signal info so that proceed_all_lwps
3414 enqueue_pending_signal (event_child
, WSTOPSIG (w
), info_p
);
3416 proceed_all_lwps ();
3420 resume_one_lwp (event_child
, event_child
->stepping
,
3421 WSTOPSIG (w
), info_p
);
3427 return ignore_event (ourstatus
);
3430 /* Note that all addresses are always "out of the step range" when
3431 there's no range to begin with. */
3432 in_step_range
= lwp_in_step_range (event_child
);
3434 /* If GDB wanted this thread to single step, and the thread is out
3435 of the step range, we always want to report the SIGTRAP, and let
3436 GDB handle it. Watchpoints should always be reported. So should
3437 signals we can't explain. A SIGTRAP we can't explain could be a
3438 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3439 do, we're be able to handle GDB breakpoints on top of internal
3440 breakpoints, by handling the internal breakpoint and still
3441 reporting the event to GDB. If we don't, we're out of luck, GDB
3442 won't see the breakpoint hit. If we see a single-step event but
3443 the thread should be continuing, don't pass the trap to gdb.
3444 That indicates that we had previously finished a single-step but
3445 left the single-step pending -- see
3446 complete_ongoing_step_over. */
3447 report_to_gdb
= (!maybe_internal_trap
3448 || (current_thread
->last_resume_kind
== resume_step
3450 || event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3452 && !bp_explains_trap
3454 && !step_over_finished
3455 && !(current_thread
->last_resume_kind
== resume_continue
3456 && event_child
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
))
3457 || (gdb_breakpoint_here (event_child
->stop_pc
)
3458 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
3459 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
))
3460 || event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
);
3462 run_breakpoint_commands (event_child
->stop_pc
);
3464 /* We found no reason GDB would want us to stop. We either hit one
3465 of our own breakpoints, or finished an internal step GDB
3466 shouldn't know about. */
3471 if (bp_explains_trap
)
3472 debug_printf ("Hit a gdbserver breakpoint.\n");
3473 if (step_over_finished
)
3474 debug_printf ("Step-over finished.\n");
3476 debug_printf ("Tracepoint event.\n");
3477 if (lwp_in_step_range (event_child
))
3478 debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
3479 paddress (event_child
->stop_pc
),
3480 paddress (event_child
->step_range_start
),
3481 paddress (event_child
->step_range_end
));
3484 /* We're not reporting this breakpoint to GDB, so apply the
3485 decr_pc_after_break adjustment to the inferior's regcache
3488 if (low_supports_breakpoints ())
3490 struct regcache
*regcache
3491 = get_thread_regcache (current_thread
, 1);
3492 low_set_pc (regcache
, event_child
->stop_pc
);
3495 if (step_over_finished
)
3497 /* If we have finished stepping over a breakpoint, we've
3498 stopped and suspended all LWPs momentarily except the
3499 stepping one. This is where we resume them all again.
3500 We're going to keep waiting, so use proceed, which
3501 handles stepping over the next breakpoint. */
3502 unsuspend_all_lwps (event_child
);
3506 /* Remove the single-step breakpoints if any. Note that
3507 there isn't single-step breakpoint if we finished stepping
3509 if (supports_software_single_step ()
3510 && has_single_step_breakpoints (current_thread
))
3512 stop_all_lwps (0, event_child
);
3513 delete_single_step_breakpoints (current_thread
);
3514 unstop_all_lwps (0, event_child
);
3519 debug_printf ("proceeding all threads.\n");
3520 proceed_all_lwps ();
3525 return ignore_event (ourstatus
);
3530 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3533 = target_waitstatus_to_string (&event_child
->waitstatus
);
3535 debug_printf ("LWP %ld: extended event with waitstatus %s\n",
3536 lwpid_of (get_lwp_thread (event_child
)), str
.c_str ());
3538 if (current_thread
->last_resume_kind
== resume_step
)
3540 if (event_child
->step_range_start
== event_child
->step_range_end
)
3541 debug_printf ("GDB wanted to single-step, reporting event.\n");
3542 else if (!lwp_in_step_range (event_child
))
3543 debug_printf ("Out of step range, reporting event.\n");
3545 if (event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
3546 debug_printf ("Stopped by watchpoint.\n");
3547 else if (gdb_breakpoint_here (event_child
->stop_pc
))
3548 debug_printf ("Stopped by GDB breakpoint.\n");
3550 debug_printf ("Hit a non-gdbserver trap event.\n");
3553 /* Alright, we're going to report a stop. */
3555 /* Remove single-step breakpoints. */
3556 if (supports_software_single_step ())
3558 /* Remove single-step breakpoints or not. It it is true, stop all
3559 lwps, so that other threads won't hit the breakpoint in the
3561 int remove_single_step_breakpoints_p
= 0;
3565 remove_single_step_breakpoints_p
3566 = has_single_step_breakpoints (current_thread
);
3570 /* In all-stop, a stop reply cancels all previous resume
3571 requests. Delete all single-step breakpoints. */
3573 find_thread ([&] (thread_info
*thread
) {
3574 if (has_single_step_breakpoints (thread
))
3576 remove_single_step_breakpoints_p
= 1;
3584 if (remove_single_step_breakpoints_p
)
3586 /* If we remove single-step breakpoints from memory, stop all lwps,
3587 so that other threads won't hit the breakpoint in the staled
3589 stop_all_lwps (0, event_child
);
3593 gdb_assert (has_single_step_breakpoints (current_thread
));
3594 delete_single_step_breakpoints (current_thread
);
3598 for_each_thread ([] (thread_info
*thread
){
3599 if (has_single_step_breakpoints (thread
))
3600 delete_single_step_breakpoints (thread
);
3604 unstop_all_lwps (0, event_child
);
3608 if (!stabilizing_threads
)
3610 /* In all-stop, stop all threads. */
3612 stop_all_lwps (0, NULL
);
3614 if (step_over_finished
)
3618 /* If we were doing a step-over, all other threads but
3619 the stepping one had been paused in start_step_over,
3620 with their suspend counts incremented. We don't want
3621 to do a full unstop/unpause, because we're in
3622 all-stop mode (so we want threads stopped), but we
3623 still need to unsuspend the other threads, to
3624 decrement their `suspended' count back. */
3625 unsuspend_all_lwps (event_child
);
3629 /* If we just finished a step-over, then all threads had
3630 been momentarily paused. In all-stop, that's fine,
3631 we want threads stopped by now anyway. In non-stop,
3632 we need to re-resume threads that GDB wanted to be
3634 unstop_all_lwps (1, event_child
);
3638 /* If we're not waiting for a specific LWP, choose an event LWP
3639 from among those that have had events. Giving equal priority
3640 to all LWPs that have had events helps prevent
3642 if (ptid
== minus_one_ptid
)
3644 event_child
->status_pending_p
= 1;
3645 event_child
->status_pending
= w
;
3647 select_event_lwp (&event_child
);
3649 /* current_thread and event_child must stay in sync. */
3650 current_thread
= get_lwp_thread (event_child
);
3652 event_child
->status_pending_p
= 0;
3653 w
= event_child
->status_pending
;
3657 /* Stabilize threads (move out of jump pads). */
3659 target_stabilize_threads ();
3663 /* If we just finished a step-over, then all threads had been
3664 momentarily paused. In all-stop, that's fine, we want
3665 threads stopped by now anyway. In non-stop, we need to
3666 re-resume threads that GDB wanted to be running. */
3667 if (step_over_finished
)
3668 unstop_all_lwps (1, event_child
);
3671 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3673 /* If the reported event is an exit, fork, vfork or exec, let
3676 /* Break the unreported fork relationship chain. */
3677 if (event_child
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
3678 || event_child
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
)
3680 event_child
->fork_relative
->fork_relative
= NULL
;
3681 event_child
->fork_relative
= NULL
;
3684 *ourstatus
= event_child
->waitstatus
;
3685 /* Clear the event lwp's waitstatus since we handled it already. */
3686 event_child
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3689 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3691 /* Now that we've selected our final event LWP, un-adjust its PC if
3692 it was a software breakpoint, and the client doesn't know we can
3693 adjust the breakpoint ourselves. */
3694 if (event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3695 && !cs
.swbreak_feature
)
3697 int decr_pc
= the_low_target
.decr_pc_after_break
;
3701 struct regcache
*regcache
3702 = get_thread_regcache (current_thread
, 1);
3703 low_set_pc (regcache
, event_child
->stop_pc
+ decr_pc
);
3707 if (WSTOPSIG (w
) == SYSCALL_SIGTRAP
)
3709 get_syscall_trapinfo (event_child
,
3710 &ourstatus
->value
.syscall_number
);
3711 ourstatus
->kind
= event_child
->syscall_state
;
3713 else if (current_thread
->last_resume_kind
== resume_stop
3714 && WSTOPSIG (w
) == SIGSTOP
)
3716 /* A thread that has been requested to stop by GDB with vCont;t,
3717 and it stopped cleanly, so report as SIG0. The use of
3718 SIGSTOP is an implementation detail. */
3719 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3721 else if (current_thread
->last_resume_kind
== resume_stop
3722 && WSTOPSIG (w
) != SIGSTOP
)
3724 /* A thread that has been requested to stop by GDB with vCont;t,
3725 but, it stopped for other reasons. */
3726 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3728 else if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
)
3730 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3733 gdb_assert (step_over_bkpt
== null_ptid
);
3737 debug_printf ("wait_1 ret = %s, %d, %d\n",
3738 target_pid_to_str (ptid_of (current_thread
)),
3739 ourstatus
->kind
, ourstatus
->value
.sig
);
3743 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3744 return filter_exit_event (event_child
, ourstatus
);
3746 return ptid_of (current_thread
);
3749 /* Get rid of any pending event in the pipe. */
3751 async_file_flush (void)
3757 ret
= read (linux_event_pipe
[0], &buf
, 1);
3758 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
3761 /* Put something in the pipe, so the event loop wakes up. */
3763 async_file_mark (void)
3767 async_file_flush ();
3770 ret
= write (linux_event_pipe
[1], "+", 1);
3771 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
3773 /* Ignore EAGAIN. If the pipe is full, the event loop will already
3774 be awakened anyway. */
3778 linux_process_target::wait (ptid_t ptid
,
3779 target_waitstatus
*ourstatus
,
3784 /* Flush the async file first. */
3785 if (target_is_async_p ())
3786 async_file_flush ();
3790 event_ptid
= wait_1 (ptid
, ourstatus
, target_options
);
3792 while ((target_options
& TARGET_WNOHANG
) == 0
3793 && event_ptid
== null_ptid
3794 && ourstatus
->kind
== TARGET_WAITKIND_IGNORE
);
3796 /* If at least one stop was reported, there may be more. A single
3797 SIGCHLD can signal more than one child stop. */
3798 if (target_is_async_p ()
3799 && (target_options
& TARGET_WNOHANG
) != 0
3800 && event_ptid
!= null_ptid
)
3806 /* Send a signal to an LWP. */
3809 kill_lwp (unsigned long lwpid
, int signo
)
3814 ret
= syscall (__NR_tkill
, lwpid
, signo
);
3815 if (errno
== ENOSYS
)
3817 /* If tkill fails, then we are not using nptl threads, a
3818 configuration we no longer support. */
3819 perror_with_name (("tkill"));
3825 linux_stop_lwp (struct lwp_info
*lwp
)
3831 send_sigstop (struct lwp_info
*lwp
)
3835 pid
= lwpid_of (get_lwp_thread (lwp
));
3837 /* If we already have a pending stop signal for this process, don't
3839 if (lwp
->stop_expected
)
3842 debug_printf ("Have pending sigstop for lwp %d\n", pid
);
3848 debug_printf ("Sending sigstop to lwp %d\n", pid
);
3850 lwp
->stop_expected
= 1;
3851 kill_lwp (pid
, SIGSTOP
);
3855 send_sigstop (thread_info
*thread
, lwp_info
*except
)
3857 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3859 /* Ignore EXCEPT. */
3869 /* Increment the suspend count of an LWP, and stop it, if not stopped
3872 suspend_and_send_sigstop (thread_info
*thread
, lwp_info
*except
)
3874 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3876 /* Ignore EXCEPT. */
3880 lwp_suspended_inc (lwp
);
3882 send_sigstop (thread
, except
);
3886 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
3888 /* Store the exit status for later. */
3889 lwp
->status_pending_p
= 1;
3890 lwp
->status_pending
= wstat
;
3892 /* Store in waitstatus as well, as there's nothing else to process
3894 if (WIFEXITED (wstat
))
3896 lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXITED
;
3897 lwp
->waitstatus
.value
.integer
= WEXITSTATUS (wstat
);
3899 else if (WIFSIGNALED (wstat
))
3901 lwp
->waitstatus
.kind
= TARGET_WAITKIND_SIGNALLED
;
3902 lwp
->waitstatus
.value
.sig
= gdb_signal_from_host (WTERMSIG (wstat
));
3905 /* Prevent trying to stop it. */
3908 /* No further stops are expected from a dead lwp. */
3909 lwp
->stop_expected
= 0;
3912 /* Return true if LWP has exited already, and has a pending exit event
3913 to report to GDB. */
3916 lwp_is_marked_dead (struct lwp_info
*lwp
)
3918 return (lwp
->status_pending_p
3919 && (WIFEXITED (lwp
->status_pending
)
3920 || WIFSIGNALED (lwp
->status_pending
)));
3924 linux_process_target::wait_for_sigstop ()
3926 struct thread_info
*saved_thread
;
3931 saved_thread
= current_thread
;
3932 if (saved_thread
!= NULL
)
3933 saved_tid
= saved_thread
->id
;
3935 saved_tid
= null_ptid
; /* avoid bogus unused warning */
3938 debug_printf ("wait_for_sigstop: pulling events\n");
3940 /* Passing NULL_PTID as filter indicates we want all events to be
3941 left pending. Eventually this returns when there are no
3942 unwaited-for children left. */
3943 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
, __WALL
);
3944 gdb_assert (ret
== -1);
3946 if (saved_thread
== NULL
|| mythread_alive (saved_tid
))
3947 current_thread
= saved_thread
;
3951 debug_printf ("Previously current thread died.\n");
3953 /* We can't change the current inferior behind GDB's back,
3954 otherwise, a subsequent command may apply to the wrong
3956 current_thread
= NULL
;
3960 /* Returns true if THREAD is stopped in a jump pad, and we can't
3961 move it out, because we need to report the stop event to GDB. For
3962 example, if the user puts a breakpoint in the jump pad, it's
3963 because she wants to debug it. */
3966 stuck_in_jump_pad_callback (thread_info
*thread
)
3968 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3970 if (lwp
->suspended
!= 0)
3972 internal_error (__FILE__
, __LINE__
,
3973 "LWP %ld is suspended, suspended=%d\n",
3974 lwpid_of (thread
), lwp
->suspended
);
3976 gdb_assert (lwp
->stopped
);
3978 /* Allow debugging the jump pad, gdb_collect, etc.. */
3979 return (supports_fast_tracepoints ()
3980 && agent_loaded_p ()
3981 && (gdb_breakpoint_here (lwp
->stop_pc
)
3982 || lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3983 || thread
->last_resume_kind
== resume_step
)
3984 && (linux_fast_tracepoint_collecting (lwp
, NULL
)
3985 != fast_tpoint_collect_result::not_collecting
));
3989 linux_process_target::move_out_of_jump_pad (thread_info
*thread
)
3991 struct thread_info
*saved_thread
;
3992 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3995 if (lwp
->suspended
!= 0)
3997 internal_error (__FILE__
, __LINE__
,
3998 "LWP %ld is suspended, suspended=%d\n",
3999 lwpid_of (thread
), lwp
->suspended
);
4001 gdb_assert (lwp
->stopped
);
4003 /* For gdb_breakpoint_here. */
4004 saved_thread
= current_thread
;
4005 current_thread
= thread
;
4007 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
4009 /* Allow debugging the jump pad, gdb_collect, etc. */
4010 if (!gdb_breakpoint_here (lwp
->stop_pc
)
4011 && lwp
->stop_reason
!= TARGET_STOPPED_BY_WATCHPOINT
4012 && thread
->last_resume_kind
!= resume_step
4013 && maybe_move_out_of_jump_pad (lwp
, wstat
))
4016 debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
4021 lwp
->status_pending_p
= 0;
4022 enqueue_one_deferred_signal (lwp
, wstat
);
4025 debug_printf ("Signal %d for LWP %ld deferred "
4027 WSTOPSIG (*wstat
), lwpid_of (thread
));
4030 resume_one_lwp (lwp
, 0, 0, NULL
);
4033 lwp_suspended_inc (lwp
);
4035 current_thread
= saved_thread
;
4039 lwp_running (thread_info
*thread
)
4041 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4043 if (lwp_is_marked_dead (lwp
))
4046 return !lwp
->stopped
;
4050 linux_process_target::stop_all_lwps (int suspend
, lwp_info
*except
)
4052 /* Should not be called recursively. */
4053 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
4058 debug_printf ("stop_all_lwps (%s, except=%s)\n",
4059 suspend
? "stop-and-suspend" : "stop",
4061 ? target_pid_to_str (ptid_of (get_lwp_thread (except
)))
4065 stopping_threads
= (suspend
4066 ? STOPPING_AND_SUSPENDING_THREADS
4067 : STOPPING_THREADS
);
4070 for_each_thread ([&] (thread_info
*thread
)
4072 suspend_and_send_sigstop (thread
, except
);
4075 for_each_thread ([&] (thread_info
*thread
)
4077 send_sigstop (thread
, except
);
4080 wait_for_sigstop ();
4081 stopping_threads
= NOT_STOPPING_THREADS
;
4085 debug_printf ("stop_all_lwps done, setting stopping_threads "
4086 "back to !stopping\n");
4091 /* Enqueue one signal in the chain of signals which need to be
4092 delivered to this process on next resume. */
4095 enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
)
4097 struct pending_signals
*p_sig
= XNEW (struct pending_signals
);
4099 p_sig
->prev
= lwp
->pending_signals
;
4100 p_sig
->signal
= signal
;
4102 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
4104 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
4105 lwp
->pending_signals
= p_sig
;
4109 linux_process_target::install_software_single_step_breakpoints (lwp_info
*lwp
)
4111 struct thread_info
*thread
= get_lwp_thread (lwp
);
4112 struct regcache
*regcache
= get_thread_regcache (thread
, 1);
4114 scoped_restore save_current_thread
= make_scoped_restore (¤t_thread
);
4116 current_thread
= thread
;
4117 std::vector
<CORE_ADDR
> next_pcs
= low_get_next_pcs (regcache
);
4119 for (CORE_ADDR pc
: next_pcs
)
4120 set_single_step_breakpoint (pc
, current_ptid
);
4124 linux_process_target::single_step (lwp_info
* lwp
)
4128 if (can_hardware_single_step ())
4132 else if (supports_software_single_step ())
4134 install_software_single_step_breakpoints (lwp
);
4140 debug_printf ("stepping is not implemented on this target");
4146 /* The signal can be delivered to the inferior if we are not trying to
4147 finish a fast tracepoint collect. Since signal can be delivered in
4148 the step-over, the program may go to signal handler and trap again
4149 after return from the signal handler. We can live with the spurious
4153 lwp_signal_can_be_delivered (struct lwp_info
*lwp
)
4155 return (lwp
->collecting_fast_tracepoint
4156 == fast_tpoint_collect_result::not_collecting
);
4160 linux_process_target::resume_one_lwp_throw (lwp_info
*lwp
, int step
,
4161 int signal
, siginfo_t
*info
)
4163 struct thread_info
*thread
= get_lwp_thread (lwp
);
4164 struct thread_info
*saved_thread
;
4166 struct process_info
*proc
= get_thread_process (thread
);
4168 /* Note that target description may not be initialised
4169 (proc->tdesc == NULL) at this point because the program hasn't
4170 stopped at the first instruction yet. It means GDBserver skips
4171 the extra traps from the wrapper program (see option --wrapper).
4172 Code in this function that requires register access should be
4173 guarded by proc->tdesc == NULL or something else. */
4175 if (lwp
->stopped
== 0)
4178 gdb_assert (lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
4180 fast_tpoint_collect_result fast_tp_collecting
4181 = lwp
->collecting_fast_tracepoint
;
4183 gdb_assert (!stabilizing_threads
4184 || (fast_tp_collecting
4185 != fast_tpoint_collect_result::not_collecting
));
4187 /* Cancel actions that rely on GDB not changing the PC (e.g., the
4188 user used the "jump" command, or "set $pc = foo"). */
4189 if (thread
->while_stepping
!= NULL
&& lwp
->stop_pc
!= get_pc (lwp
))
4191 /* Collecting 'while-stepping' actions doesn't make sense
4193 release_while_stepping_state_list (thread
);
4196 /* If we have pending signals or status, and a new signal, enqueue the
4197 signal. Also enqueue the signal if it can't be delivered to the
4198 inferior right now. */
4200 && (lwp
->status_pending_p
4201 || lwp
->pending_signals
!= NULL
4202 || !lwp_signal_can_be_delivered (lwp
)))
4204 enqueue_pending_signal (lwp
, signal
, info
);
4206 /* Postpone any pending signal. It was enqueued above. */
4210 if (lwp
->status_pending_p
)
4213 debug_printf ("Not resuming lwp %ld (%s, stop %s);"
4214 " has pending status\n",
4215 lwpid_of (thread
), step
? "step" : "continue",
4216 lwp
->stop_expected
? "expected" : "not expected");
4220 saved_thread
= current_thread
;
4221 current_thread
= thread
;
4223 /* This bit needs some thinking about. If we get a signal that
4224 we must report while a single-step reinsert is still pending,
4225 we often end up resuming the thread. It might be better to
4226 (ew) allow a stack of pending events; then we could be sure that
4227 the reinsert happened right away and not lose any signals.
4229 Making this stack would also shrink the window in which breakpoints are
4230 uninserted (see comment in linux_wait_for_lwp) but not enough for
4231 complete correctness, so it won't solve that problem. It may be
4232 worthwhile just to solve this one, however. */
4233 if (lwp
->bp_reinsert
!= 0)
4236 debug_printf (" pending reinsert at 0x%s\n",
4237 paddress (lwp
->bp_reinsert
));
4239 if (can_hardware_single_step ())
4241 if (fast_tp_collecting
== fast_tpoint_collect_result::not_collecting
)
4244 warning ("BAD - reinserting but not stepping.");
4246 warning ("BAD - reinserting and suspended(%d).",
4251 step
= maybe_hw_step (thread
);
4254 if (fast_tp_collecting
== fast_tpoint_collect_result::before_insn
)
4257 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4258 " (exit-jump-pad-bkpt)\n",
4261 else if (fast_tp_collecting
== fast_tpoint_collect_result::at_insn
)
4264 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4265 " single-stepping\n",
4268 if (can_hardware_single_step ())
4272 internal_error (__FILE__
, __LINE__
,
4273 "moving out of jump pad single-stepping"
4274 " not implemented on this target");
4278 /* If we have while-stepping actions in this thread set it stepping.
4279 If we have a signal to deliver, it may or may not be set to
4280 SIG_IGN, we don't know. Assume so, and allow collecting
4281 while-stepping into a signal handler. A possible smart thing to
4282 do would be to set an internal breakpoint at the signal return
4283 address, continue, and carry on catching this while-stepping
4284 action only when that breakpoint is hit. A future
4286 if (thread
->while_stepping
!= NULL
)
4289 debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
4292 step
= single_step (lwp
);
4295 if (proc
->tdesc
!= NULL
&& low_supports_breakpoints ())
4297 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
4299 lwp
->stop_pc
= low_get_pc (regcache
);
4303 debug_printf (" %s from pc 0x%lx\n", step
? "step" : "continue",
4304 (long) lwp
->stop_pc
);
4308 /* If we have pending signals, consume one if it can be delivered to
4310 if (lwp
->pending_signals
!= NULL
&& lwp_signal_can_be_delivered (lwp
))
4312 struct pending_signals
**p_sig
;
4314 p_sig
= &lwp
->pending_signals
;
4315 while ((*p_sig
)->prev
!= NULL
)
4316 p_sig
= &(*p_sig
)->prev
;
4318 signal
= (*p_sig
)->signal
;
4319 if ((*p_sig
)->info
.si_signo
!= 0)
4320 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
4328 debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
4329 lwpid_of (thread
), step
? "step" : "continue", signal
,
4330 lwp
->stop_expected
? "expected" : "not expected");
4332 if (the_low_target
.prepare_to_resume
!= NULL
)
4333 the_low_target
.prepare_to_resume (lwp
);
4335 regcache_invalidate_thread (thread
);
4337 lwp
->stepping
= step
;
4339 ptrace_request
= PTRACE_SINGLESTEP
;
4340 else if (gdb_catching_syscalls_p (lwp
))
4341 ptrace_request
= PTRACE_SYSCALL
;
4343 ptrace_request
= PTRACE_CONT
;
4344 ptrace (ptrace_request
,
4346 (PTRACE_TYPE_ARG3
) 0,
4347 /* Coerce to a uintptr_t first to avoid potential gcc warning
4348 of coercing an 8 byte integer to a 4 byte pointer. */
4349 (PTRACE_TYPE_ARG4
) (uintptr_t) signal
);
4351 current_thread
= saved_thread
;
4353 perror_with_name ("resuming thread");
4355 /* Successfully resumed. Clear state that no longer makes sense,
4356 and mark the LWP as running. Must not do this before resuming
4357 otherwise if that fails other code will be confused. E.g., we'd
4358 later try to stop the LWP and hang forever waiting for a stop
4359 status. Note that we must not throw after this is cleared,
4360 otherwise handle_zombie_lwp_error would get confused. */
4362 lwp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4365 /* Called when we try to resume a stopped LWP and that errors out. If
4366 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4367 or about to become), discard the error, clear any pending status
4368 the LWP may have, and return true (we'll collect the exit status
4369 soon enough). Otherwise, return false. */
4372 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
4374 struct thread_info
*thread
= get_lwp_thread (lp
);
4376 /* If we get an error after resuming the LWP successfully, we'd
4377 confuse !T state for the LWP being gone. */
4378 gdb_assert (lp
->stopped
);
4380 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4381 because even if ptrace failed with ESRCH, the tracee may be "not
4382 yet fully dead", but already refusing ptrace requests. In that
4383 case the tracee has 'R (Running)' state for a little bit
4384 (observed in Linux 3.18). See also the note on ESRCH in the
4385 ptrace(2) man page. Instead, check whether the LWP has any state
4386 other than ptrace-stopped. */
4388 /* Don't assume anything if /proc/PID/status can't be read. */
4389 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread
)) == 0)
4391 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4392 lp
->status_pending_p
= 0;
4399 linux_process_target::resume_one_lwp (lwp_info
*lwp
, int step
, int signal
,
4404 resume_one_lwp_throw (lwp
, step
, signal
, info
);
4406 catch (const gdb_exception_error
&ex
)
4408 if (!check_ptrace_stopped_lwp_gone (lwp
))
4413 /* This function is called once per thread via for_each_thread.
4414 We look up which resume request applies to THREAD and mark it with a
4415 pointer to the appropriate resume request.
4417 This algorithm is O(threads * resume elements), but resume elements
4418 is small (and will remain small at least until GDB supports thread
4422 linux_set_resume_request (thread_info
*thread
, thread_resume
*resume
, size_t n
)
4424 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4426 for (int ndx
= 0; ndx
< n
; ndx
++)
4428 ptid_t ptid
= resume
[ndx
].thread
;
4429 if (ptid
== minus_one_ptid
4430 || ptid
== thread
->id
4431 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4433 || (ptid
.pid () == pid_of (thread
)
4435 || ptid
.lwp () == -1)))
4437 if (resume
[ndx
].kind
== resume_stop
4438 && thread
->last_resume_kind
== resume_stop
)
4441 debug_printf ("already %s LWP %ld at GDB's request\n",
4442 (thread
->last_status
.kind
4443 == TARGET_WAITKIND_STOPPED
)
4451 /* Ignore (wildcard) resume requests for already-resumed
4453 if (resume
[ndx
].kind
!= resume_stop
4454 && thread
->last_resume_kind
!= resume_stop
)
4457 debug_printf ("already %s LWP %ld at GDB's request\n",
4458 (thread
->last_resume_kind
4466 /* Don't let wildcard resumes resume fork children that GDB
4467 does not yet know are new fork children. */
4468 if (lwp
->fork_relative
!= NULL
)
4470 struct lwp_info
*rel
= lwp
->fork_relative
;
4472 if (rel
->status_pending_p
4473 && (rel
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
4474 || rel
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
))
4477 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4483 /* If the thread has a pending event that has already been
4484 reported to GDBserver core, but GDB has not pulled the
4485 event out of the vStopped queue yet, likewise, ignore the
4486 (wildcard) resume request. */
4487 if (in_queued_stop_replies (thread
->id
))
4490 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4495 lwp
->resume
= &resume
[ndx
];
4496 thread
->last_resume_kind
= lwp
->resume
->kind
;
4498 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
4499 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
4501 /* If we had a deferred signal to report, dequeue one now.
4502 This can happen if LWP gets more than one signal while
4503 trying to get out of a jump pad. */
4505 && !lwp
->status_pending_p
4506 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
4508 lwp
->status_pending_p
= 1;
4511 debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
4512 "leaving status pending.\n",
4513 WSTOPSIG (lwp
->status_pending
),
4521 /* No resume action for this thread. */
4526 linux_process_target::resume_status_pending (thread_info
*thread
)
4528 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4530 /* LWPs which will not be resumed are not interesting, because
4531 we might not wait for them next time through linux_wait. */
4532 if (lwp
->resume
== NULL
)
4535 return thread_still_has_status_pending (thread
);
4539 linux_process_target::thread_needs_step_over (thread_info
*thread
)
4541 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4542 struct thread_info
*saved_thread
;
4544 struct process_info
*proc
= get_thread_process (thread
);
4546 /* GDBserver is skipping the extra traps from the wrapper program,
4547 don't have to do step over. */
4548 if (proc
->tdesc
== NULL
)
4551 /* LWPs which will not be resumed are not interesting, because we
4552 might not wait for them next time through linux_wait. */
4557 debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
4562 if (thread
->last_resume_kind
== resume_stop
)
4565 debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
4571 gdb_assert (lwp
->suspended
>= 0);
4576 debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
4581 if (lwp
->status_pending_p
)
4584 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4590 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4594 /* If the PC has changed since we stopped, then don't do anything,
4595 and let the breakpoint/tracepoint be hit. This happens if, for
4596 instance, GDB handled the decr_pc_after_break subtraction itself,
4597 GDB is OOL stepping this thread, or the user has issued a "jump"
4598 command, or poked thread's registers herself. */
4599 if (pc
!= lwp
->stop_pc
)
4602 debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4603 "Old stop_pc was 0x%s, PC is now 0x%s\n",
4605 paddress (lwp
->stop_pc
), paddress (pc
));
4609 /* On software single step target, resume the inferior with signal
4610 rather than stepping over. */
4611 if (supports_software_single_step ()
4612 && lwp
->pending_signals
!= NULL
4613 && lwp_signal_can_be_delivered (lwp
))
4616 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4623 saved_thread
= current_thread
;
4624 current_thread
= thread
;
4626 /* We can only step over breakpoints we know about. */
4627 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
4629 /* Don't step over a breakpoint that GDB expects to hit
4630 though. If the condition is being evaluated on the target's side
4631 and it evaluate to false, step over this breakpoint as well. */
4632 if (gdb_breakpoint_here (pc
)
4633 && gdb_condition_true_at_breakpoint (pc
)
4634 && gdb_no_commands_at_breakpoint (pc
))
4637 debug_printf ("Need step over [LWP %ld]? yes, but found"
4638 " GDB breakpoint at 0x%s; skipping step over\n",
4639 lwpid_of (thread
), paddress (pc
));
4641 current_thread
= saved_thread
;
4647 debug_printf ("Need step over [LWP %ld]? yes, "
4648 "found breakpoint at 0x%s\n",
4649 lwpid_of (thread
), paddress (pc
));
4651 /* We've found an lwp that needs stepping over --- return 1 so
4652 that find_thread stops looking. */
4653 current_thread
= saved_thread
;
4659 current_thread
= saved_thread
;
4662 debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
4664 lwpid_of (thread
), paddress (pc
));
4670 linux_process_target::start_step_over (lwp_info
*lwp
)
4672 struct thread_info
*thread
= get_lwp_thread (lwp
);
4673 struct thread_info
*saved_thread
;
4678 debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
4681 stop_all_lwps (1, lwp
);
4683 if (lwp
->suspended
!= 0)
4685 internal_error (__FILE__
, __LINE__
,
4686 "LWP %ld suspended=%d\n", lwpid_of (thread
),
4691 debug_printf ("Done stopping all threads for step-over.\n");
4693 /* Note, we should always reach here with an already adjusted PC,
4694 either by GDB (if we're resuming due to GDB's request), or by our
4695 caller, if we just finished handling an internal breakpoint GDB
4696 shouldn't care about. */
4699 saved_thread
= current_thread
;
4700 current_thread
= thread
;
4702 lwp
->bp_reinsert
= pc
;
4703 uninsert_breakpoints_at (pc
);
4704 uninsert_fast_tracepoint_jumps_at (pc
);
4706 step
= single_step (lwp
);
4708 current_thread
= saved_thread
;
4710 resume_one_lwp (lwp
, step
, 0, NULL
);
4712 /* Require next event from this LWP. */
4713 step_over_bkpt
= thread
->id
;
4716 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
4717 start_step_over, if still there, and delete any single-step
4718 breakpoints we've set, on non hardware single-step targets. */
4721 finish_step_over (struct lwp_info
*lwp
)
4723 if (lwp
->bp_reinsert
!= 0)
4725 struct thread_info
*saved_thread
= current_thread
;
4728 debug_printf ("Finished step over.\n");
4730 current_thread
= get_lwp_thread (lwp
);
4732 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4733 may be no breakpoint to reinsert there by now. */
4734 reinsert_breakpoints_at (lwp
->bp_reinsert
);
4735 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
4737 lwp
->bp_reinsert
= 0;
4739 /* Delete any single-step breakpoints. No longer needed. We
4740 don't have to worry about other threads hitting this trap,
4741 and later not being able to explain it, because we were
4742 stepping over a breakpoint, and we hold all threads but
4743 LWP stopped while doing that. */
4744 if (!can_hardware_single_step ())
4746 gdb_assert (has_single_step_breakpoints (current_thread
));
4747 delete_single_step_breakpoints (current_thread
);
4750 step_over_bkpt
= null_ptid
;
4751 current_thread
= saved_thread
;
4759 linux_process_target::complete_ongoing_step_over ()
4761 if (step_over_bkpt
!= null_ptid
)
4763 struct lwp_info
*lwp
;
4768 debug_printf ("detach: step over in progress, finish it first\n");
4770 /* Passing NULL_PTID as filter indicates we want all events to
4771 be left pending. Eventually this returns when there are no
4772 unwaited-for children left. */
4773 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
,
4775 gdb_assert (ret
== -1);
4777 lwp
= find_lwp_pid (step_over_bkpt
);
4779 finish_step_over (lwp
);
4780 step_over_bkpt
= null_ptid
;
4781 unsuspend_all_lwps (lwp
);
4786 linux_process_target::resume_one_thread (thread_info
*thread
,
4787 bool leave_all_stopped
)
4789 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4792 if (lwp
->resume
== NULL
)
4795 if (lwp
->resume
->kind
== resume_stop
)
4798 debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread
));
4803 debug_printf ("stopping LWP %ld\n", lwpid_of (thread
));
4805 /* Stop the thread, and wait for the event asynchronously,
4806 through the event loop. */
4812 debug_printf ("already stopped LWP %ld\n",
4815 /* The LWP may have been stopped in an internal event that
4816 was not meant to be notified back to GDB (e.g., gdbserver
4817 breakpoint), so we should be reporting a stop event in
4820 /* If the thread already has a pending SIGSTOP, this is a
4821 no-op. Otherwise, something later will presumably resume
4822 the thread and this will cause it to cancel any pending
4823 operation, due to last_resume_kind == resume_stop. If
4824 the thread already has a pending status to report, we
4825 will still report it the next time we wait - see
4826 status_pending_p_callback. */
4828 /* If we already have a pending signal to report, then
4829 there's no need to queue a SIGSTOP, as this means we're
4830 midway through moving the LWP out of the jumppad, and we
4831 will report the pending signal as soon as that is
4833 if (lwp
->pending_signals_to_report
== NULL
)
4837 /* For stop requests, we're done. */
4839 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4843 /* If this thread which is about to be resumed has a pending status,
4844 then don't resume it - we can just report the pending status.
4845 Likewise if it is suspended, because e.g., another thread is
4846 stepping past a breakpoint. Make sure to queue any signals that
4847 would otherwise be sent. In all-stop mode, we do this decision
4848 based on if *any* thread has a pending status. If there's a
4849 thread that needs the step-over-breakpoint dance, then don't
4850 resume any other thread but that particular one. */
4851 leave_pending
= (lwp
->suspended
4852 || lwp
->status_pending_p
4853 || leave_all_stopped
);
4855 /* If we have a new signal, enqueue the signal. */
4856 if (lwp
->resume
->sig
!= 0)
4858 siginfo_t info
, *info_p
;
4860 /* If this is the same signal we were previously stopped by,
4861 make sure to queue its siginfo. */
4862 if (WIFSTOPPED (lwp
->last_status
)
4863 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
4864 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
),
4865 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
4870 enqueue_pending_signal (lwp
, lwp
->resume
->sig
, info_p
);
4876 debug_printf ("resuming LWP %ld\n", lwpid_of (thread
));
4878 proceed_one_lwp (thread
, NULL
);
4883 debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread
));
4886 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4891 linux_process_target::resume (thread_resume
*resume_info
, size_t n
)
4893 struct thread_info
*need_step_over
= NULL
;
4898 debug_printf ("linux_resume:\n");
4901 for_each_thread ([&] (thread_info
*thread
)
4903 linux_set_resume_request (thread
, resume_info
, n
);
4906 /* If there is a thread which would otherwise be resumed, which has
4907 a pending status, then don't resume any threads - we can just
4908 report the pending status. Make sure to queue any signals that
4909 would otherwise be sent. In non-stop mode, we'll apply this
4910 logic to each thread individually. We consume all pending events
4911 before considering to start a step-over (in all-stop). */
4912 bool any_pending
= false;
4914 any_pending
= find_thread ([this] (thread_info
*thread
)
4916 return resume_status_pending (thread
);
4919 /* If there is a thread which would otherwise be resumed, which is
4920 stopped at a breakpoint that needs stepping over, then don't
4921 resume any threads - have it step over the breakpoint with all
4922 other threads stopped, then resume all threads again. Make sure
4923 to queue any signals that would otherwise be delivered or
4925 if (!any_pending
&& low_supports_breakpoints ())
4926 need_step_over
= find_thread ([this] (thread_info
*thread
)
4928 return thread_needs_step_over (thread
);
4931 bool leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
4935 if (need_step_over
!= NULL
)
4936 debug_printf ("Not resuming all, need step over\n");
4937 else if (any_pending
)
4938 debug_printf ("Not resuming, all-stop and found "
4939 "an LWP with pending status\n");
4941 debug_printf ("Resuming, no pending status or step over needed\n");
4944 /* Even if we're leaving threads stopped, queue all signals we'd
4945 otherwise deliver. */
4946 for_each_thread ([&] (thread_info
*thread
)
4948 resume_one_thread (thread
, leave_all_stopped
);
4952 start_step_over (get_thread_lwp (need_step_over
));
4956 debug_printf ("linux_resume done\n");
4960 /* We may have events that were pending that can/should be sent to
4961 the client now. Trigger a linux_wait call. */
4962 if (target_is_async_p ())
4967 linux_process_target::proceed_one_lwp (thread_info
*thread
, lwp_info
*except
)
4969 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4976 debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread
));
4981 debug_printf (" LWP %ld already running\n", lwpid_of (thread
));
4985 if (thread
->last_resume_kind
== resume_stop
4986 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4989 debug_printf (" client wants LWP to remain %ld stopped\n",
4994 if (lwp
->status_pending_p
)
4997 debug_printf (" LWP %ld has pending status, leaving stopped\n",
5002 gdb_assert (lwp
->suspended
>= 0);
5007 debug_printf (" LWP %ld is suspended\n", lwpid_of (thread
));
5011 if (thread
->last_resume_kind
== resume_stop
5012 && lwp
->pending_signals_to_report
== NULL
5013 && (lwp
->collecting_fast_tracepoint
5014 == fast_tpoint_collect_result::not_collecting
))
5016 /* We haven't reported this LWP as stopped yet (otherwise, the
5017 last_status.kind check above would catch it, and we wouldn't
5018 reach here. This LWP may have been momentarily paused by a
5019 stop_all_lwps call while handling for example, another LWP's
5020 step-over. In that case, the pending expected SIGSTOP signal
5021 that was queued at vCont;t handling time will have already
5022 been consumed by wait_for_sigstop, and so we need to requeue
5023 another one here. Note that if the LWP already has a SIGSTOP
5024 pending, this is a no-op. */
5027 debug_printf ("Client wants LWP %ld to stop. "
5028 "Making sure it has a SIGSTOP pending\n",
5034 if (thread
->last_resume_kind
== resume_step
)
5037 debug_printf (" stepping LWP %ld, client wants it stepping\n",
5040 /* If resume_step is requested by GDB, install single-step
5041 breakpoints when the thread is about to be actually resumed if
5042 the single-step breakpoints weren't removed. */
5043 if (supports_software_single_step ()
5044 && !has_single_step_breakpoints (thread
))
5045 install_software_single_step_breakpoints (lwp
);
5047 step
= maybe_hw_step (thread
);
5049 else if (lwp
->bp_reinsert
!= 0)
5052 debug_printf (" stepping LWP %ld, reinsert set\n",
5055 step
= maybe_hw_step (thread
);
5060 resume_one_lwp (lwp
, step
, 0, NULL
);
5064 linux_process_target::unsuspend_and_proceed_one_lwp (thread_info
*thread
,
5067 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5072 lwp_suspended_decr (lwp
);
5074 proceed_one_lwp (thread
, except
);
5078 linux_process_target::proceed_all_lwps ()
5080 struct thread_info
*need_step_over
;
5082 /* If there is a thread which would otherwise be resumed, which is
5083 stopped at a breakpoint that needs stepping over, then don't
5084 resume any threads - have it step over the breakpoint with all
5085 other threads stopped, then resume all threads again. */
5087 if (low_supports_breakpoints ())
5089 need_step_over
= find_thread ([this] (thread_info
*thread
)
5091 return thread_needs_step_over (thread
);
5094 if (need_step_over
!= NULL
)
5097 debug_printf ("proceed_all_lwps: found "
5098 "thread %ld needing a step-over\n",
5099 lwpid_of (need_step_over
));
5101 start_step_over (get_thread_lwp (need_step_over
));
5107 debug_printf ("Proceeding, no step-over needed\n");
5109 for_each_thread ([this] (thread_info
*thread
)
5111 proceed_one_lwp (thread
, NULL
);
5116 linux_process_target::unstop_all_lwps (int unsuspend
, lwp_info
*except
)
5122 debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
5123 lwpid_of (get_lwp_thread (except
)));
5125 debug_printf ("unstopping all lwps\n");
5129 for_each_thread ([&] (thread_info
*thread
)
5131 unsuspend_and_proceed_one_lwp (thread
, except
);
5134 for_each_thread ([&] (thread_info
*thread
)
5136 proceed_one_lwp (thread
, except
);
5141 debug_printf ("unstop_all_lwps done\n");
5147 #ifdef HAVE_LINUX_REGSETS
5149 #define use_linux_regsets 1
5151 /* Returns true if REGSET has been disabled. */
5154 regset_disabled (struct regsets_info
*info
, struct regset_info
*regset
)
5156 return (info
->disabled_regsets
!= NULL
5157 && info
->disabled_regsets
[regset
- info
->regsets
]);
5160 /* Disable REGSET. */
5163 disable_regset (struct regsets_info
*info
, struct regset_info
*regset
)
5167 dr_offset
= regset
- info
->regsets
;
5168 if (info
->disabled_regsets
== NULL
)
5169 info
->disabled_regsets
= (char *) xcalloc (1, info
->num_regsets
);
5170 info
->disabled_regsets
[dr_offset
] = 1;
5174 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
5175 struct regcache
*regcache
)
5177 struct regset_info
*regset
;
5178 int saw_general_regs
= 0;
5182 pid
= lwpid_of (current_thread
);
5183 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5188 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
5191 buf
= xmalloc (regset
->size
);
5193 nt_type
= regset
->nt_type
;
5197 iov
.iov_len
= regset
->size
;
5198 data
= (void *) &iov
;
5204 res
= ptrace (regset
->get_request
, pid
,
5205 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5207 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5212 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5214 /* If we get EIO on a regset, or an EINVAL and the regset is
5215 optional, do not try it again for this process mode. */
5216 disable_regset (regsets_info
, regset
);
5218 else if (errno
== ENODATA
)
5220 /* ENODATA may be returned if the regset is currently
5221 not "active". This can happen in normal operation,
5222 so suppress the warning in this case. */
5224 else if (errno
== ESRCH
)
5226 /* At this point, ESRCH should mean the process is
5227 already gone, in which case we simply ignore attempts
5228 to read its registers. */
5233 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
5240 if (regset
->type
== GENERAL_REGS
)
5241 saw_general_regs
= 1;
5242 regset
->store_function (regcache
, buf
);
5246 if (saw_general_regs
)
5253 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
5254 struct regcache
*regcache
)
5256 struct regset_info
*regset
;
5257 int saw_general_regs
= 0;
5261 pid
= lwpid_of (current_thread
);
5262 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5267 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
)
5268 || regset
->fill_function
== NULL
)
5271 buf
= xmalloc (regset
->size
);
5273 /* First fill the buffer with the current register set contents,
5274 in case there are any items in the kernel's regset that are
5275 not in gdbserver's regcache. */
5277 nt_type
= regset
->nt_type
;
5281 iov
.iov_len
= regset
->size
;
5282 data
= (void *) &iov
;
5288 res
= ptrace (regset
->get_request
, pid
,
5289 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5291 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5296 /* Then overlay our cached registers on that. */
5297 regset
->fill_function (regcache
, buf
);
5299 /* Only now do we write the register set. */
5301 res
= ptrace (regset
->set_request
, pid
,
5302 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5304 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
5311 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5313 /* If we get EIO on a regset, or an EINVAL and the regset is
5314 optional, do not try it again for this process mode. */
5315 disable_regset (regsets_info
, regset
);
5317 else if (errno
== ESRCH
)
5319 /* At this point, ESRCH should mean the process is
5320 already gone, in which case we simply ignore attempts
5321 to change its registers. See also the related
5322 comment in resume_one_lwp. */
5328 perror ("Warning: ptrace(regsets_store_inferior_registers)");
5331 else if (regset
->type
== GENERAL_REGS
)
5332 saw_general_regs
= 1;
5335 if (saw_general_regs
)
5341 #else /* !HAVE_LINUX_REGSETS */
5343 #define use_linux_regsets 0
5344 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5345 #define regsets_store_inferior_registers(regsets_info, regcache) 1
5349 /* Return 1 if register REGNO is supported by one of the regset ptrace
5350 calls or 0 if it has to be transferred individually. */
5353 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
5355 unsigned char mask
= 1 << (regno
% 8);
5356 size_t index
= regno
/ 8;
5358 return (use_linux_regsets
5359 && (regs_info
->regset_bitmap
== NULL
5360 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
5363 #ifdef HAVE_LINUX_USRREGS
5366 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
5370 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
5371 error ("Invalid register number %d.", regnum
);
5373 addr
= usrregs
->regmap
[regnum
];
5380 linux_process_target::fetch_register (const usrregs_info
*usrregs
,
5381 regcache
*regcache
, int regno
)
5388 if (regno
>= usrregs
->num_regs
)
5390 if (low_cannot_fetch_register (regno
))
5393 regaddr
= register_addr (usrregs
, regno
);
5397 size
= ((register_size (regcache
->tdesc
, regno
)
5398 + sizeof (PTRACE_XFER_TYPE
) - 1)
5399 & -sizeof (PTRACE_XFER_TYPE
));
5400 buf
= (char *) alloca (size
);
5402 pid
= lwpid_of (current_thread
);
5403 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5406 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
5407 ptrace (PTRACE_PEEKUSER
, pid
,
5408 /* Coerce to a uintptr_t first to avoid potential gcc warning
5409 of coercing an 8 byte integer to a 4 byte pointer. */
5410 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
, (PTRACE_TYPE_ARG4
) 0);
5411 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5414 /* Mark register REGNO unavailable. */
5415 supply_register (regcache
, regno
, NULL
);
5420 if (the_low_target
.supply_ptrace_register
)
5421 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
5423 supply_register (regcache
, regno
, buf
);
5427 linux_process_target::store_register (const usrregs_info
*usrregs
,
5428 regcache
*regcache
, int regno
)
5435 if (regno
>= usrregs
->num_regs
)
5437 if (low_cannot_store_register (regno
))
5440 regaddr
= register_addr (usrregs
, regno
);
5444 size
= ((register_size (regcache
->tdesc
, regno
)
5445 + sizeof (PTRACE_XFER_TYPE
) - 1)
5446 & -sizeof (PTRACE_XFER_TYPE
));
5447 buf
= (char *) alloca (size
);
5448 memset (buf
, 0, size
);
5450 if (the_low_target
.collect_ptrace_register
)
5451 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
5453 collect_register (regcache
, regno
, buf
);
5455 pid
= lwpid_of (current_thread
);
5456 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5459 ptrace (PTRACE_POKEUSER
, pid
,
5460 /* Coerce to a uintptr_t first to avoid potential gcc warning
5461 about coercing an 8 byte integer to a 4 byte pointer. */
5462 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
,
5463 (PTRACE_TYPE_ARG4
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
5466 /* At this point, ESRCH should mean the process is
5467 already gone, in which case we simply ignore attempts
5468 to change its registers. See also the related
5469 comment in resume_one_lwp. */
5474 if (!low_cannot_store_register (regno
))
5475 error ("writing register %d: %s", regno
, safe_strerror (errno
));
5477 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5480 #endif /* HAVE_LINUX_USRREGS */
5483 linux_process_target::usr_fetch_inferior_registers (const regs_info
*regs_info
,
5487 #ifdef HAVE_LINUX_USRREGS
5488 struct usrregs_info
*usr
= regs_info
->usrregs
;
5492 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5493 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5494 fetch_register (usr
, regcache
, regno
);
5497 fetch_register (usr
, regcache
, regno
);
5502 linux_process_target::usr_store_inferior_registers (const regs_info
*regs_info
,
5506 #ifdef HAVE_LINUX_USRREGS
5507 struct usrregs_info
*usr
= regs_info
->usrregs
;
5511 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5512 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5513 store_register (usr
, regcache
, regno
);
5516 store_register (usr
, regcache
, regno
);
5521 linux_process_target::fetch_registers (regcache
*regcache
, int regno
)
5525 const regs_info
*regs_info
= get_regs_info ();
5529 if (regs_info
->usrregs
!= NULL
)
5530 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
5531 low_fetch_register (regcache
, regno
);
5533 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
5534 if (regs_info
->usrregs
!= NULL
)
5535 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
5539 if (low_fetch_register (regcache
, regno
))
5542 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5544 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
5546 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5547 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
5552 linux_process_target::store_registers (regcache
*regcache
, int regno
)
5556 const regs_info
*regs_info
= get_regs_info ();
5560 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5562 if (regs_info
->usrregs
!= NULL
)
5563 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
5567 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5569 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5571 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5572 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
5577 linux_process_target::low_fetch_register (regcache
*regcache
, int regno
)
5582 /* A wrapper for the read_memory target op. */
5585 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
5587 return the_target
->read_memory (memaddr
, myaddr
, len
);
5590 /* Copy LEN bytes from inferior's memory starting at MEMADDR
5591 to debugger memory starting at MYADDR. */
5594 linux_process_target::read_memory (CORE_ADDR memaddr
,
5595 unsigned char *myaddr
, int len
)
5597 int pid
= lwpid_of (current_thread
);
5598 PTRACE_XFER_TYPE
*buffer
;
5606 /* Try using /proc. Don't bother for one word. */
5607 if (len
>= 3 * sizeof (long))
5611 /* We could keep this file open and cache it - possibly one per
5612 thread. That requires some juggling, but is even faster. */
5613 sprintf (filename
, "/proc/%d/mem", pid
);
5614 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
5618 /* If pread64 is available, use it. It's faster if the kernel
5619 supports it (only one syscall), and it's 64-bit safe even on
5620 32-bit platforms (for instance, SPARC debugging a SPARC64
5623 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
5626 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
5627 bytes
= read (fd
, myaddr
, len
);
5634 /* Some data was read, we'll try to get the rest with ptrace. */
5644 /* Round starting address down to longword boundary. */
5645 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5646 /* Round ending address up; get number of longwords that makes. */
5647 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5648 / sizeof (PTRACE_XFER_TYPE
));
5649 /* Allocate buffer of that many longwords. */
5650 buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5652 /* Read all the longwords */
5654 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5656 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5657 about coercing an 8 byte integer to a 4 byte pointer. */
5658 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
5659 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5660 (PTRACE_TYPE_ARG4
) 0);
5666 /* Copy appropriate bytes out of the buffer. */
5669 i
*= sizeof (PTRACE_XFER_TYPE
);
5670 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
5672 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5679 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5680 memory at MEMADDR. On failure (cannot write to the inferior)
5681 returns the value of errno. Always succeeds if LEN is zero. */
5684 linux_process_target::write_memory (CORE_ADDR memaddr
,
5685 const unsigned char *myaddr
, int len
)
5688 /* Round starting address down to longword boundary. */
5689 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5690 /* Round ending address up; get number of longwords that makes. */
5692 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5693 / sizeof (PTRACE_XFER_TYPE
);
5695 /* Allocate buffer of that many longwords. */
5696 PTRACE_XFER_TYPE
*buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5698 int pid
= lwpid_of (current_thread
);
5702 /* Zero length write always succeeds. */
5708 /* Dump up to four bytes. */
5709 char str
[4 * 2 + 1];
5711 int dump
= len
< 4 ? len
: 4;
5713 for (i
= 0; i
< dump
; i
++)
5715 sprintf (p
, "%02x", myaddr
[i
]);
5720 debug_printf ("Writing %s to 0x%08lx in process %d\n",
5721 str
, (long) memaddr
, pid
);
5724 /* Fill start and end extra bytes of buffer with existing memory data. */
5727 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5728 about coercing an 8 byte integer to a 4 byte pointer. */
5729 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
5730 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5731 (PTRACE_TYPE_ARG4
) 0);
5739 = ptrace (PTRACE_PEEKTEXT
, pid
,
5740 /* Coerce to a uintptr_t first to avoid potential gcc warning
5741 about coercing an 8 byte integer to a 4 byte pointer. */
5742 (PTRACE_TYPE_ARG3
) (uintptr_t) (addr
+ (count
- 1)
5743 * sizeof (PTRACE_XFER_TYPE
)),
5744 (PTRACE_TYPE_ARG4
) 0);
5749 /* Copy data to be written over corresponding part of buffer. */
5751 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5754 /* Write the entire buffer. */
5756 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5759 ptrace (PTRACE_POKETEXT
, pid
,
5760 /* Coerce to a uintptr_t first to avoid potential gcc warning
5761 about coercing an 8 byte integer to a 4 byte pointer. */
5762 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5763 (PTRACE_TYPE_ARG4
) buffer
[i
]);
5772 linux_process_target::look_up_symbols ()
5774 #ifdef USE_THREAD_DB
5775 struct process_info
*proc
= current_process ();
5777 if (proc
->priv
->thread_db
!= NULL
)
5785 linux_process_target::request_interrupt ()
5787 /* Send a SIGINT to the process group. This acts just like the user
5788 typed a ^C on the controlling terminal. */
5789 ::kill (-signal_pid
, SIGINT
);
5793 linux_process_target::supports_read_auxv ()
5798 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5799 to debugger memory starting at MYADDR. */
5802 linux_process_target::read_auxv (CORE_ADDR offset
, unsigned char *myaddr
,
5805 char filename
[PATH_MAX
];
5807 int pid
= lwpid_of (current_thread
);
5809 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5811 fd
= open (filename
, O_RDONLY
);
5815 if (offset
!= (CORE_ADDR
) 0
5816 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5819 n
= read (fd
, myaddr
, len
);
5826 /* These breakpoint and watchpoint related wrapper functions simply
5827 pass on the function call if the target has registered a
5828 corresponding function. */
5831 linux_process_target::supports_z_point_type (char z_type
)
5833 return (the_low_target
.supports_z_point_type
!= NULL
5834 && the_low_target
.supports_z_point_type (z_type
));
5838 linux_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5839 int size
, raw_breakpoint
*bp
)
5841 if (type
== raw_bkpt_type_sw
)
5842 return insert_memory_breakpoint (bp
);
5843 else if (the_low_target
.insert_point
!= NULL
)
5844 return the_low_target
.insert_point (type
, addr
, size
, bp
);
5846 /* Unsupported (see target.h). */
5851 linux_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5852 int size
, raw_breakpoint
*bp
)
5854 if (type
== raw_bkpt_type_sw
)
5855 return remove_memory_breakpoint (bp
);
5856 else if (the_low_target
.remove_point
!= NULL
)
5857 return the_low_target
.remove_point (type
, addr
, size
, bp
);
5859 /* Unsupported (see target.h). */
5863 /* Implement the stopped_by_sw_breakpoint target_ops
5867 linux_process_target::stopped_by_sw_breakpoint ()
5869 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5871 return (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
);
5874 /* Implement the supports_stopped_by_sw_breakpoint target_ops
5878 linux_process_target::supports_stopped_by_sw_breakpoint ()
5880 return USE_SIGTRAP_SIGINFO
;
5883 /* Implement the stopped_by_hw_breakpoint target_ops
5887 linux_process_target::stopped_by_hw_breakpoint ()
5889 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5891 return (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
);
5894 /* Implement the supports_stopped_by_hw_breakpoint target_ops
5898 linux_process_target::supports_stopped_by_hw_breakpoint ()
5900 return USE_SIGTRAP_SIGINFO
;
5903 /* Implement the supports_hardware_single_step target_ops method. */
5906 linux_process_target::supports_hardware_single_step ()
5908 return can_hardware_single_step ();
5912 linux_process_target::stopped_by_watchpoint ()
5914 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5916 return lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
5920 linux_process_target::stopped_data_address ()
5922 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5924 return lwp
->stopped_data_address
;
5927 /* This is only used for targets that define PT_TEXT_ADDR,
5928 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5929 the target has different ways of acquiring this information, like
5933 linux_process_target::supports_read_offsets ()
5935 #ifdef SUPPORTS_READ_OFFSETS
5942 /* Under uClinux, programs are loaded at non-zero offsets, which we need
5943 to tell gdb about. */
5946 linux_process_target::read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
5948 #ifdef SUPPORTS_READ_OFFSETS
5949 unsigned long text
, text_end
, data
;
5950 int pid
= lwpid_of (current_thread
);
5954 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_ADDR
,
5955 (PTRACE_TYPE_ARG4
) 0);
5956 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_END_ADDR
,
5957 (PTRACE_TYPE_ARG4
) 0);
5958 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_DATA_ADDR
,
5959 (PTRACE_TYPE_ARG4
) 0);
5963 /* Both text and data offsets produced at compile-time (and so
5964 used by gdb) are relative to the beginning of the program,
5965 with the data segment immediately following the text segment.
5966 However, the actual runtime layout in memory may put the data
5967 somewhere else, so when we send gdb a data base-address, we
5968 use the real data base address and subtract the compile-time
5969 data base-address from it (which is just the length of the
5970 text segment). BSS immediately follows data in both
5973 *data_p
= data
- (text_end
- text
);
5979 gdb_assert_not_reached ("target op read_offsets not supported");
5984 linux_process_target::supports_get_tls_address ()
5986 #ifdef USE_THREAD_DB
5994 linux_process_target::get_tls_address (thread_info
*thread
,
5996 CORE_ADDR load_module
,
5999 #ifdef USE_THREAD_DB
6000 return thread_db_get_tls_address (thread
, offset
, load_module
, address
);
6007 linux_process_target::supports_qxfer_osdata ()
6013 linux_process_target::qxfer_osdata (const char *annex
,
6014 unsigned char *readbuf
,
6015 unsigned const char *writebuf
,
6016 CORE_ADDR offset
, int len
)
6018 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
6021 /* Convert a native/host siginfo object, into/from the siginfo in the
6022 layout of the inferiors' architecture. */
6025 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
6029 if (the_low_target
.siginfo_fixup
!= NULL
)
6030 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
6032 /* If there was no callback, or the callback didn't do anything,
6033 then just do a straight memcpy. */
6037 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
6039 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
6044 linux_process_target::supports_qxfer_siginfo ()
6050 linux_process_target::qxfer_siginfo (const char *annex
,
6051 unsigned char *readbuf
,
6052 unsigned const char *writebuf
,
6053 CORE_ADDR offset
, int len
)
6057 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
6059 if (current_thread
== NULL
)
6062 pid
= lwpid_of (current_thread
);
6065 debug_printf ("%s siginfo for lwp %d.\n",
6066 readbuf
!= NULL
? "Reading" : "Writing",
6069 if (offset
>= sizeof (siginfo
))
6072 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6075 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
6076 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
6077 inferior with a 64-bit GDBSERVER should look the same as debugging it
6078 with a 32-bit GDBSERVER, we need to convert it. */
6079 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
6081 if (offset
+ len
> sizeof (siginfo
))
6082 len
= sizeof (siginfo
) - offset
;
6084 if (readbuf
!= NULL
)
6085 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
6088 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
6090 /* Convert back to ptrace layout before flushing it out. */
6091 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
6093 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6100 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
6101 so we notice when children change state; as the handler for the
6102 sigsuspend in my_waitpid. */
6105 sigchld_handler (int signo
)
6107 int old_errno
= errno
;
6113 /* Use the async signal safe debug function. */
6114 if (debug_write ("sigchld_handler\n",
6115 sizeof ("sigchld_handler\n") - 1) < 0)
6116 break; /* just ignore */
6120 if (target_is_async_p ())
6121 async_file_mark (); /* trigger a linux_wait */
6127 linux_process_target::supports_non_stop ()
6133 linux_process_target::async (bool enable
)
6135 bool previous
= target_is_async_p ();
6138 debug_printf ("linux_async (%d), previous=%d\n",
6141 if (previous
!= enable
)
6144 sigemptyset (&mask
);
6145 sigaddset (&mask
, SIGCHLD
);
6147 gdb_sigmask (SIG_BLOCK
, &mask
, NULL
);
6151 if (pipe (linux_event_pipe
) == -1)
6153 linux_event_pipe
[0] = -1;
6154 linux_event_pipe
[1] = -1;
6155 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6157 warning ("creating event pipe failed.");
6161 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
6162 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
6164 /* Register the event loop handler. */
6165 add_file_handler (linux_event_pipe
[0],
6166 handle_target_event
, NULL
);
6168 /* Always trigger a linux_wait. */
6173 delete_file_handler (linux_event_pipe
[0]);
6175 close (linux_event_pipe
[0]);
6176 close (linux_event_pipe
[1]);
6177 linux_event_pipe
[0] = -1;
6178 linux_event_pipe
[1] = -1;
6181 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6188 linux_process_target::start_non_stop (bool nonstop
)
6190 /* Register or unregister from event-loop accordingly. */
6191 target_async (nonstop
);
6193 if (target_is_async_p () != (nonstop
!= false))
6200 linux_process_target::supports_multi_process ()
6205 /* Check if fork events are supported. */
6208 linux_process_target::supports_fork_events ()
6210 return linux_supports_tracefork ();
6213 /* Check if vfork events are supported. */
6216 linux_process_target::supports_vfork_events ()
6218 return linux_supports_tracefork ();
6221 /* Check if exec events are supported. */
6224 linux_process_target::supports_exec_events ()
6226 return linux_supports_traceexec ();
6229 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
6230 ptrace flags for all inferiors. This is in case the new GDB connection
6231 doesn't support the same set of events that the previous one did. */
6234 linux_process_target::handle_new_gdb_connection ()
6236 /* Request that all the lwps reset their ptrace options. */
6237 for_each_thread ([] (thread_info
*thread
)
6239 struct lwp_info
*lwp
= get_thread_lwp (thread
);
6243 /* Stop the lwp so we can modify its ptrace options. */
6244 lwp
->must_set_ptrace_flags
= 1;
6245 linux_stop_lwp (lwp
);
6249 /* Already stopped; go ahead and set the ptrace options. */
6250 struct process_info
*proc
= find_process_pid (pid_of (thread
));
6251 int options
= linux_low_ptrace_options (proc
->attached
);
6253 linux_enable_event_reporting (lwpid_of (thread
), options
);
6254 lwp
->must_set_ptrace_flags
= 0;
6260 linux_process_target::handle_monitor_command (char *mon
)
6262 #ifdef USE_THREAD_DB
6263 return thread_db_handle_monitor_command (mon
);
6270 linux_process_target::core_of_thread (ptid_t ptid
)
6272 return linux_common_core_of_thread (ptid
);
6276 linux_process_target::supports_disable_randomization ()
6278 #ifdef HAVE_PERSONALITY
6286 linux_process_target::supports_agent ()
6292 linux_process_target::supports_range_stepping ()
6294 if (supports_software_single_step ())
6296 if (*the_low_target
.supports_range_stepping
== NULL
)
6299 return (*the_low_target
.supports_range_stepping
) ();
6303 linux_process_target::supports_pid_to_exec_file ()
6309 linux_process_target::pid_to_exec_file (int pid
)
6311 return linux_proc_pid_to_exec_file (pid
);
6315 linux_process_target::supports_multifs ()
6321 linux_process_target::multifs_open (int pid
, const char *filename
,
6322 int flags
, mode_t mode
)
6324 return linux_mntns_open_cloexec (pid
, filename
, flags
, mode
);
6328 linux_process_target::multifs_unlink (int pid
, const char *filename
)
6330 return linux_mntns_unlink (pid
, filename
);
6334 linux_process_target::multifs_readlink (int pid
, const char *filename
,
6335 char *buf
, size_t bufsiz
)
6337 return linux_mntns_readlink (pid
, filename
, buf
, bufsiz
);
6340 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
6341 struct target_loadseg
6343 /* Core address to which the segment is mapped. */
6345 /* VMA recorded in the program header. */
6347 /* Size of this segment in memory. */
6351 # if defined PT_GETDSBT
6352 struct target_loadmap
6354 /* Protocol version number, must be zero. */
6356 /* Pointer to the DSBT table, its size, and the DSBT index. */
6357 unsigned *dsbt_table
;
6358 unsigned dsbt_size
, dsbt_index
;
6359 /* Number of segments in this map. */
6361 /* The actual memory map. */
6362 struct target_loadseg segs
[/*nsegs*/];
6364 # define LINUX_LOADMAP PT_GETDSBT
6365 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6366 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6368 struct target_loadmap
6370 /* Protocol version number, must be zero. */
6372 /* Number of segments in this map. */
6374 /* The actual memory map. */
6375 struct target_loadseg segs
[/*nsegs*/];
6377 # define LINUX_LOADMAP PTRACE_GETFDPIC
6378 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6379 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6383 linux_process_target::supports_read_loadmap ()
6389 linux_process_target::read_loadmap (const char *annex
, CORE_ADDR offset
,
6390 unsigned char *myaddr
, unsigned int len
)
6392 int pid
= lwpid_of (current_thread
);
6394 struct target_loadmap
*data
= NULL
;
6395 unsigned int actual_length
, copy_length
;
6397 if (strcmp (annex
, "exec") == 0)
6398 addr
= (int) LINUX_LOADMAP_EXEC
;
6399 else if (strcmp (annex
, "interp") == 0)
6400 addr
= (int) LINUX_LOADMAP_INTERP
;
6404 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
6410 actual_length
= sizeof (struct target_loadmap
)
6411 + sizeof (struct target_loadseg
) * data
->nsegs
;
6413 if (offset
< 0 || offset
> actual_length
)
6416 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
6417 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
6420 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6423 linux_process_target::process_qsupported (char **features
, int count
)
6425 if (the_low_target
.process_qsupported
!= NULL
)
6426 the_low_target
.process_qsupported (features
, count
);
6430 linux_process_target::supports_catch_syscall ()
6432 return (the_low_target
.get_syscall_trapinfo
!= NULL
6433 && linux_supports_tracesysgood ());
6437 linux_process_target::get_ipa_tdesc_idx ()
6439 if (the_low_target
.get_ipa_tdesc_idx
== NULL
)
6442 return (*the_low_target
.get_ipa_tdesc_idx
) ();
6446 linux_process_target::supports_tracepoints ()
6448 if (*the_low_target
.supports_tracepoints
== NULL
)
6451 return (*the_low_target
.supports_tracepoints
) ();
6455 linux_process_target::read_pc (regcache
*regcache
)
6457 if (!low_supports_breakpoints ())
6460 return low_get_pc (regcache
);
6464 linux_process_target::write_pc (regcache
*regcache
, CORE_ADDR pc
)
6466 gdb_assert (low_supports_breakpoints ());
6468 low_set_pc (regcache
, pc
);
6472 linux_process_target::supports_thread_stopped ()
6478 linux_process_target::thread_stopped (thread_info
*thread
)
6480 return get_thread_lwp (thread
)->stopped
;
6483 /* This exposes stop-all-threads functionality to other modules. */
6486 linux_process_target::pause_all (bool freeze
)
6488 stop_all_lwps (freeze
, NULL
);
6491 /* This exposes unstop-all-threads functionality to other gdbserver
6495 linux_process_target::unpause_all (bool unfreeze
)
6497 unstop_all_lwps (unfreeze
, NULL
);
6501 linux_process_target::prepare_to_access_memory ()
6503 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6506 target_pause_all (true);
6511 linux_process_target::done_accessing_memory ()
6513 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6516 target_unpause_all (true);
6520 linux_process_target::supports_fast_tracepoints ()
6522 return the_low_target
.install_fast_tracepoint_jump_pad
!= nullptr;
6526 linux_process_target::install_fast_tracepoint_jump_pad
6527 (CORE_ADDR tpoint
, CORE_ADDR tpaddr
, CORE_ADDR collector
,
6528 CORE_ADDR lockaddr
, ULONGEST orig_size
, CORE_ADDR
*jump_entry
,
6529 CORE_ADDR
*trampoline
, ULONGEST
*trampoline_size
,
6530 unsigned char *jjump_pad_insn
, ULONGEST
*jjump_pad_insn_size
,
6531 CORE_ADDR
*adjusted_insn_addr
, CORE_ADDR
*adjusted_insn_addr_end
,
6534 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
6535 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
6536 jump_entry
, trampoline
, trampoline_size
,
6537 jjump_pad_insn
, jjump_pad_insn_size
,
6538 adjusted_insn_addr
, adjusted_insn_addr_end
,
6543 linux_process_target::emit_ops ()
6545 if (the_low_target
.emit_ops
!= NULL
)
6546 return (*the_low_target
.emit_ops
) ();
6552 linux_process_target::get_min_fast_tracepoint_insn_len ()
6554 return (*the_low_target
.get_min_fast_tracepoint_insn_len
) ();
6557 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6560 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
6561 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
6563 char filename
[PATH_MAX
];
6565 const int auxv_size
= is_elf64
6566 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
6567 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
6569 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
6571 fd
= open (filename
, O_RDONLY
);
6577 while (read (fd
, buf
, auxv_size
) == auxv_size
6578 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
6582 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
6584 switch (aux
->a_type
)
6587 *phdr_memaddr
= aux
->a_un
.a_val
;
6590 *num_phdr
= aux
->a_un
.a_val
;
6596 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
6598 switch (aux
->a_type
)
6601 *phdr_memaddr
= aux
->a_un
.a_val
;
6604 *num_phdr
= aux
->a_un
.a_val
;
6612 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
6614 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6615 "phdr_memaddr = %ld, phdr_num = %d",
6616 (long) *phdr_memaddr
, *num_phdr
);
6623 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6626 get_dynamic (const int pid
, const int is_elf64
)
6628 CORE_ADDR phdr_memaddr
, relocation
;
6630 unsigned char *phdr_buf
;
6631 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
6633 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
6636 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
6637 phdr_buf
= (unsigned char *) alloca (num_phdr
* phdr_size
);
6639 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
6642 /* Compute relocation: it is expected to be 0 for "regular" executables,
6643 non-zero for PIE ones. */
6645 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
6648 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6650 if (p
->p_type
== PT_PHDR
)
6651 relocation
= phdr_memaddr
- p
->p_vaddr
;
6655 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6657 if (p
->p_type
== PT_PHDR
)
6658 relocation
= phdr_memaddr
- p
->p_vaddr
;
6661 if (relocation
== -1)
6663 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6664 any real world executables, including PIE executables, have always
6665 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6666 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6667 or present DT_DEBUG anyway (fpc binaries are statically linked).
6669 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6671 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6676 for (i
= 0; i
< num_phdr
; i
++)
6680 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6682 if (p
->p_type
== PT_DYNAMIC
)
6683 return p
->p_vaddr
+ relocation
;
6687 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6689 if (p
->p_type
== PT_DYNAMIC
)
6690 return p
->p_vaddr
+ relocation
;
6697 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6698 can be 0 if the inferior does not yet have the library list initialized.
6699 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6700 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6703 get_r_debug (const int pid
, const int is_elf64
)
6705 CORE_ADDR dynamic_memaddr
;
6706 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
6707 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
6710 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
6711 if (dynamic_memaddr
== 0)
6714 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
6718 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
6719 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6723 unsigned char buf
[sizeof (Elf64_Xword
)];
6727 #ifdef DT_MIPS_RLD_MAP
6728 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6730 if (linux_read_memory (dyn
->d_un
.d_val
,
6731 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6736 #endif /* DT_MIPS_RLD_MAP */
6737 #ifdef DT_MIPS_RLD_MAP_REL
6738 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6740 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6741 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6746 #endif /* DT_MIPS_RLD_MAP_REL */
6748 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6749 map
= dyn
->d_un
.d_val
;
6751 if (dyn
->d_tag
== DT_NULL
)
6756 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
6757 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6761 unsigned char buf
[sizeof (Elf32_Word
)];
6765 #ifdef DT_MIPS_RLD_MAP
6766 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6768 if (linux_read_memory (dyn
->d_un
.d_val
,
6769 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6774 #endif /* DT_MIPS_RLD_MAP */
6775 #ifdef DT_MIPS_RLD_MAP_REL
6776 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6778 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6779 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6784 #endif /* DT_MIPS_RLD_MAP_REL */
6786 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6787 map
= dyn
->d_un
.d_val
;
6789 if (dyn
->d_tag
== DT_NULL
)
6793 dynamic_memaddr
+= dyn_size
;
6799 /* Read one pointer from MEMADDR in the inferior. */
6802 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
6806 /* Go through a union so this works on either big or little endian
6807 hosts, when the inferior's pointer size is smaller than the size
6808 of CORE_ADDR. It is assumed the inferior's endianness is the
6809 same of the superior's. */
6812 CORE_ADDR core_addr
;
6817 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
6820 if (ptr_size
== sizeof (CORE_ADDR
))
6821 *ptr
= addr
.core_addr
;
6822 else if (ptr_size
== sizeof (unsigned int))
6825 gdb_assert_not_reached ("unhandled pointer size");
6831 linux_process_target::supports_qxfer_libraries_svr4 ()
6836 struct link_map_offsets
6838 /* Offset and size of r_debug.r_version. */
6839 int r_version_offset
;
6841 /* Offset and size of r_debug.r_map. */
6844 /* Offset to l_addr field in struct link_map. */
6847 /* Offset to l_name field in struct link_map. */
6850 /* Offset to l_ld field in struct link_map. */
6853 /* Offset to l_next field in struct link_map. */
6856 /* Offset to l_prev field in struct link_map. */
6860 /* Construct qXfer:libraries-svr4:read reply. */
6863 linux_process_target::qxfer_libraries_svr4 (const char *annex
,
6864 unsigned char *readbuf
,
6865 unsigned const char *writebuf
,
6866 CORE_ADDR offset
, int len
)
6868 struct process_info_private
*const priv
= current_process ()->priv
;
6869 char filename
[PATH_MAX
];
6872 static const struct link_map_offsets lmo_32bit_offsets
=
6874 0, /* r_version offset. */
6875 4, /* r_debug.r_map offset. */
6876 0, /* l_addr offset in link_map. */
6877 4, /* l_name offset in link_map. */
6878 8, /* l_ld offset in link_map. */
6879 12, /* l_next offset in link_map. */
6880 16 /* l_prev offset in link_map. */
6883 static const struct link_map_offsets lmo_64bit_offsets
=
6885 0, /* r_version offset. */
6886 8, /* r_debug.r_map offset. */
6887 0, /* l_addr offset in link_map. */
6888 8, /* l_name offset in link_map. */
6889 16, /* l_ld offset in link_map. */
6890 24, /* l_next offset in link_map. */
6891 32 /* l_prev offset in link_map. */
6893 const struct link_map_offsets
*lmo
;
6894 unsigned int machine
;
6896 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
6897 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
6898 int header_done
= 0;
6900 if (writebuf
!= NULL
)
6902 if (readbuf
== NULL
)
6905 pid
= lwpid_of (current_thread
);
6906 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
6907 is_elf64
= elf_64_file_p (filename
, &machine
);
6908 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
6909 ptr_size
= is_elf64
? 8 : 4;
6911 while (annex
[0] != '\0')
6917 sep
= strchr (annex
, '=');
6921 name_len
= sep
- annex
;
6922 if (name_len
== 5 && startswith (annex
, "start"))
6924 else if (name_len
== 4 && startswith (annex
, "prev"))
6928 annex
= strchr (sep
, ';');
6935 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
6942 if (priv
->r_debug
== 0)
6943 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
6945 /* We failed to find DT_DEBUG. Such situation will not change
6946 for this inferior - do not retry it. Report it to GDB as
6947 E01, see for the reasons at the GDB solib-svr4.c side. */
6948 if (priv
->r_debug
== (CORE_ADDR
) -1)
6951 if (priv
->r_debug
!= 0)
6953 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
6954 (unsigned char *) &r_version
,
6955 sizeof (r_version
)) != 0
6958 warning ("unexpected r_debug version %d", r_version
);
6960 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
6961 &lm_addr
, ptr_size
) != 0)
6963 warning ("unable to read r_map from 0x%lx",
6964 (long) priv
->r_debug
+ lmo
->r_map_offset
);
6969 std::string document
= "<library-list-svr4 version=\"1.0\"";
6972 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
6973 &l_name
, ptr_size
) == 0
6974 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
6975 &l_addr
, ptr_size
) == 0
6976 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
6977 &l_ld
, ptr_size
) == 0
6978 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
6979 &l_prev
, ptr_size
) == 0
6980 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
6981 &l_next
, ptr_size
) == 0)
6983 unsigned char libname
[PATH_MAX
];
6985 if (lm_prev
!= l_prev
)
6987 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
6988 (long) lm_prev
, (long) l_prev
);
6992 /* Ignore the first entry even if it has valid name as the first entry
6993 corresponds to the main executable. The first entry should not be
6994 skipped if the dynamic loader was loaded late by a static executable
6995 (see solib-svr4.c parameter ignore_first). But in such case the main
6996 executable does not have PT_DYNAMIC present and this function already
6997 exited above due to failed get_r_debug. */
6999 string_appendf (document
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
7002 /* Not checking for error because reading may stop before
7003 we've got PATH_MAX worth of characters. */
7005 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
7006 libname
[sizeof (libname
) - 1] = '\0';
7007 if (libname
[0] != '\0')
7011 /* Terminate `<library-list-svr4'. */
7016 string_appendf (document
, "<library name=\"");
7017 xml_escape_text_append (&document
, (char *) libname
);
7018 string_appendf (document
, "\" lm=\"0x%lx\" "
7019 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
7020 (unsigned long) lm_addr
, (unsigned long) l_addr
,
7021 (unsigned long) l_ld
);
7031 /* Empty list; terminate `<library-list-svr4'. */
7035 document
+= "</library-list-svr4>";
7037 int document_len
= document
.length ();
7038 if (offset
< document_len
)
7039 document_len
-= offset
;
7042 if (len
> document_len
)
7045 memcpy (readbuf
, document
.data () + offset
, len
);
7050 #ifdef HAVE_LINUX_BTRACE
7052 btrace_target_info
*
7053 linux_process_target::enable_btrace (ptid_t ptid
,
7054 const btrace_config
*conf
)
7056 return linux_enable_btrace (ptid
, conf
);
7059 /* See to_disable_btrace target method. */
7062 linux_process_target::disable_btrace (btrace_target_info
*tinfo
)
7064 enum btrace_error err
;
7066 err
= linux_disable_btrace (tinfo
);
7067 return (err
== BTRACE_ERR_NONE
? 0 : -1);
7070 /* Encode an Intel Processor Trace configuration. */
7073 linux_low_encode_pt_config (struct buffer
*buffer
,
7074 const struct btrace_data_pt_config
*config
)
7076 buffer_grow_str (buffer
, "<pt-config>\n");
7078 switch (config
->cpu
.vendor
)
7081 buffer_xml_printf (buffer
, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
7082 "model=\"%u\" stepping=\"%u\"/>\n",
7083 config
->cpu
.family
, config
->cpu
.model
,
7084 config
->cpu
.stepping
);
7091 buffer_grow_str (buffer
, "</pt-config>\n");
7094 /* Encode a raw buffer. */
7097 linux_low_encode_raw (struct buffer
*buffer
, const gdb_byte
*data
,
7103 /* We use hex encoding - see gdbsupport/rsp-low.h. */
7104 buffer_grow_str (buffer
, "<raw>\n");
7110 elem
[0] = tohex ((*data
>> 4) & 0xf);
7111 elem
[1] = tohex (*data
++ & 0xf);
7113 buffer_grow (buffer
, elem
, 2);
7116 buffer_grow_str (buffer
, "</raw>\n");
7119 /* See to_read_btrace target method. */
7122 linux_process_target::read_btrace (btrace_target_info
*tinfo
,
7124 enum btrace_read_type type
)
7126 struct btrace_data btrace
;
7127 enum btrace_error err
;
7129 err
= linux_read_btrace (&btrace
, tinfo
, type
);
7130 if (err
!= BTRACE_ERR_NONE
)
7132 if (err
== BTRACE_ERR_OVERFLOW
)
7133 buffer_grow_str0 (buffer
, "E.Overflow.");
7135 buffer_grow_str0 (buffer
, "E.Generic Error.");
7140 switch (btrace
.format
)
7142 case BTRACE_FORMAT_NONE
:
7143 buffer_grow_str0 (buffer
, "E.No Trace.");
7146 case BTRACE_FORMAT_BTS
:
7147 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7148 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7150 for (const btrace_block
&block
: *btrace
.variant
.bts
.blocks
)
7151 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
7152 paddress (block
.begin
), paddress (block
.end
));
7154 buffer_grow_str0 (buffer
, "</btrace>\n");
7157 case BTRACE_FORMAT_PT
:
7158 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7159 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7160 buffer_grow_str (buffer
, "<pt>\n");
7162 linux_low_encode_pt_config (buffer
, &btrace
.variant
.pt
.config
);
7164 linux_low_encode_raw (buffer
, btrace
.variant
.pt
.data
,
7165 btrace
.variant
.pt
.size
);
7167 buffer_grow_str (buffer
, "</pt>\n");
7168 buffer_grow_str0 (buffer
, "</btrace>\n");
7172 buffer_grow_str0 (buffer
, "E.Unsupported Trace Format.");
7179 /* See to_btrace_conf target method. */
7182 linux_process_target::read_btrace_conf (const btrace_target_info
*tinfo
,
7185 const struct btrace_config
*conf
;
7187 buffer_grow_str (buffer
, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
7188 buffer_grow_str (buffer
, "<btrace-conf version=\"1.0\">\n");
7190 conf
= linux_btrace_conf (tinfo
);
7193 switch (conf
->format
)
7195 case BTRACE_FORMAT_NONE
:
7198 case BTRACE_FORMAT_BTS
:
7199 buffer_xml_printf (buffer
, "<bts");
7200 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->bts
.size
);
7201 buffer_xml_printf (buffer
, " />\n");
7204 case BTRACE_FORMAT_PT
:
7205 buffer_xml_printf (buffer
, "<pt");
7206 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->pt
.size
);
7207 buffer_xml_printf (buffer
, "/>\n");
7212 buffer_grow_str0 (buffer
, "</btrace-conf>\n");
7215 #endif /* HAVE_LINUX_BTRACE */
7217 /* See nat/linux-nat.h. */
7220 current_lwp_ptid (void)
7222 return ptid_of (current_thread
);
7226 linux_process_target::thread_name (ptid_t thread
)
7228 return linux_proc_tid_get_name (thread
);
7233 linux_process_target::thread_handle (ptid_t ptid
, gdb_byte
**handle
,
7236 return thread_db_thread_handle (ptid
, handle
, handle_len
);
7240 /* Default implementation of linux_target_ops method "set_pc" for
7241 32-bit pc register which is literally named "pc". */
7244 linux_set_pc_32bit (struct regcache
*regcache
, CORE_ADDR pc
)
7246 uint32_t newpc
= pc
;
7248 supply_register_by_name (regcache
, "pc", &newpc
);
7251 /* Default implementation of linux_target_ops method "get_pc" for
7252 32-bit pc register which is literally named "pc". */
7255 linux_get_pc_32bit (struct regcache
*regcache
)
7259 collect_register_by_name (regcache
, "pc", &pc
);
7261 debug_printf ("stop pc is 0x%" PRIx32
"\n", pc
);
7265 /* Default implementation of linux_target_ops method "set_pc" for
7266 64-bit pc register which is literally named "pc". */
7269 linux_set_pc_64bit (struct regcache
*regcache
, CORE_ADDR pc
)
7271 uint64_t newpc
= pc
;
7273 supply_register_by_name (regcache
, "pc", &newpc
);
7276 /* Default implementation of linux_target_ops method "get_pc" for
7277 64-bit pc register which is literally named "pc". */
7280 linux_get_pc_64bit (struct regcache
*regcache
)
7284 collect_register_by_name (regcache
, "pc", &pc
);
7286 debug_printf ("stop pc is 0x%" PRIx64
"\n", pc
);
7290 /* See linux-low.h. */
7293 linux_get_auxv (int wordsize
, CORE_ADDR match
, CORE_ADDR
*valp
)
7295 gdb_byte
*data
= (gdb_byte
*) alloca (2 * wordsize
);
7298 gdb_assert (wordsize
== 4 || wordsize
== 8);
7300 while (the_target
->read_auxv (offset
, data
, 2 * wordsize
) == 2 * wordsize
)
7304 uint32_t *data_p
= (uint32_t *) data
;
7305 if (data_p
[0] == match
)
7313 uint64_t *data_p
= (uint64_t *) data
;
7314 if (data_p
[0] == match
)
7321 offset
+= 2 * wordsize
;
7327 /* See linux-low.h. */
7330 linux_get_hwcap (int wordsize
)
7332 CORE_ADDR hwcap
= 0;
7333 linux_get_auxv (wordsize
, AT_HWCAP
, &hwcap
);
7337 /* See linux-low.h. */
7340 linux_get_hwcap2 (int wordsize
)
7342 CORE_ADDR hwcap2
= 0;
7343 linux_get_auxv (wordsize
, AT_HWCAP2
, &hwcap2
);
7347 #ifdef HAVE_LINUX_REGSETS
7349 initialize_regsets_info (struct regsets_info
*info
)
7351 for (info
->num_regsets
= 0;
7352 info
->regsets
[info
->num_regsets
].size
>= 0;
7353 info
->num_regsets
++)
7359 initialize_low (void)
7361 struct sigaction sigchld_action
;
7363 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
7364 set_target_ops (the_linux_target
);
7366 linux_ptrace_init_warnings ();
7367 linux_proc_init_warnings ();
7369 sigchld_action
.sa_handler
= sigchld_handler
;
7370 sigemptyset (&sigchld_action
.sa_mask
);
7371 sigchld_action
.sa_flags
= SA_RESTART
;
7372 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
7374 initialize_low_arch ();
7376 linux_check_ptrace_features ();