1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2022 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/event-loop.h"
25 #include "gdbsupport/event-pipe.h"
26 #include "gdbsupport/rsp-low.h"
27 #include "gdbsupport/signals-state-save-restore.h"
28 #include "nat/linux-nat.h"
29 #include "nat/linux-waitpid.h"
30 #include "gdbsupport/gdb_wait.h"
31 #include "nat/gdb_ptrace.h"
32 #include "nat/linux-ptrace.h"
33 #include "nat/linux-procfs.h"
34 #include "nat/linux-personality.h"
36 #include <sys/ioctl.h>
39 #include <sys/syscall.h>
43 #include <sys/types.h>
48 #include "gdbsupport/filestuff.h"
49 #include "tracepoint.h"
51 #include "gdbsupport/common-inferior.h"
52 #include "nat/fork-inferior.h"
53 #include "gdbsupport/environ.h"
54 #include "gdbsupport/gdb-sigmask.h"
55 #include "gdbsupport/scoped_restore.h"
57 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
58 then ELFMAG0 will have been defined. If it didn't get included by
59 gdb_proc_service.h then including it will likely introduce a duplicate
60 definition of elf_fpregset_t. */
63 #include "nat/linux-namespaces.h"
73 /* Some targets did not define these ptrace constants from the start,
74 so gdbserver defines them locally here. In the future, these may
75 be removed after they are added to asm/ptrace.h. */
76 #if !(defined(PT_TEXT_ADDR) \
77 || defined(PT_DATA_ADDR) \
78 || defined(PT_TEXT_END_ADDR))
79 #if defined(__mcoldfire__)
80 /* These are still undefined in 3.10 kernels. */
81 #define PT_TEXT_ADDR 49*4
82 #define PT_DATA_ADDR 50*4
83 #define PT_TEXT_END_ADDR 51*4
84 /* These are still undefined in 3.10 kernels. */
85 #elif defined(__TMS320C6X__)
86 #define PT_TEXT_ADDR (0x10000*4)
87 #define PT_DATA_ADDR (0x10004*4)
88 #define PT_TEXT_END_ADDR (0x10008*4)
92 #if (defined(__UCLIBC__) \
93 && defined(HAS_NOMMU) \
94 && defined(PT_TEXT_ADDR) \
95 && defined(PT_DATA_ADDR) \
96 && defined(PT_TEXT_END_ADDR))
97 #define SUPPORTS_READ_OFFSETS
100 #ifdef HAVE_LINUX_BTRACE
101 # include "nat/linux-btrace.h"
102 # include "gdbsupport/btrace-common.h"
105 #ifndef HAVE_ELF32_AUXV_T
106 /* Copied from glibc's elf.h. */
109 uint32_t a_type
; /* Entry type */
112 uint32_t a_val
; /* Integer value */
113 /* We use to have pointer elements added here. We cannot do that,
114 though, since it does not work when using 32-bit definitions
115 on 64-bit platforms and vice versa. */
120 #ifndef HAVE_ELF64_AUXV_T
121 /* Copied from glibc's elf.h. */
124 uint64_t a_type
; /* Entry type */
127 uint64_t a_val
; /* Integer value */
128 /* We use to have pointer elements added here. We cannot do that,
129 though, since it does not work when using 32-bit definitions
130 on 64-bit platforms and vice versa. */
135 /* Does the current host support PTRACE_GETREGSET? */
136 int have_ptrace_getregset
= -1;
140 /* See nat/linux-nat.h. */
143 ptid_of_lwp (struct lwp_info
*lwp
)
145 return ptid_of (get_lwp_thread (lwp
));
148 /* See nat/linux-nat.h. */
151 lwp_set_arch_private_info (struct lwp_info
*lwp
,
152 struct arch_lwp_info
*info
)
154 lwp
->arch_private
= info
;
157 /* See nat/linux-nat.h. */
159 struct arch_lwp_info
*
160 lwp_arch_private_info (struct lwp_info
*lwp
)
162 return lwp
->arch_private
;
165 /* See nat/linux-nat.h. */
168 lwp_is_stopped (struct lwp_info
*lwp
)
173 /* See nat/linux-nat.h. */
175 enum target_stop_reason
176 lwp_stop_reason (struct lwp_info
*lwp
)
178 return lwp
->stop_reason
;
181 /* See nat/linux-nat.h. */
184 lwp_is_stepping (struct lwp_info
*lwp
)
186 return lwp
->stepping
;
189 /* A list of all unknown processes which receive stop signals. Some
190 other process will presumably claim each of these as forked
191 children momentarily. */
193 struct simple_pid_list
195 /* The process ID. */
198 /* The status as reported by waitpid. */
202 struct simple_pid_list
*next
;
204 static struct simple_pid_list
*stopped_pids
;
206 /* Trivial list manipulation functions to keep track of a list of new
207 stopped processes. */
210 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
212 struct simple_pid_list
*new_pid
= XNEW (struct simple_pid_list
);
215 new_pid
->status
= status
;
216 new_pid
->next
= *listp
;
221 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
223 struct simple_pid_list
**p
;
225 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
226 if ((*p
)->pid
== pid
)
228 struct simple_pid_list
*next
= (*p
)->next
;
230 *statusp
= (*p
)->status
;
238 enum stopping_threads_kind
240 /* Not stopping threads presently. */
241 NOT_STOPPING_THREADS
,
243 /* Stopping threads. */
246 /* Stopping and suspending threads. */
247 STOPPING_AND_SUSPENDING_THREADS
250 /* This is set while stop_all_lwps is in effect. */
251 static stopping_threads_kind stopping_threads
= NOT_STOPPING_THREADS
;
253 /* FIXME make into a target method? */
254 int using_threads
= 1;
256 /* True if we're presently stabilizing threads (moving them out of
258 static int stabilizing_threads
;
260 static void unsuspend_all_lwps (struct lwp_info
*except
);
261 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
262 static int lwp_is_marked_dead (struct lwp_info
*lwp
);
263 static int kill_lwp (unsigned long lwpid
, int signo
);
264 static void enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
);
265 static int linux_low_ptrace_options (int attached
);
266 static int check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
);
268 /* When the event-loop is doing a step-over, this points at the thread
270 static ptid_t step_over_bkpt
;
273 linux_process_target::low_supports_breakpoints ()
279 linux_process_target::low_get_pc (regcache
*regcache
)
285 linux_process_target::low_set_pc (regcache
*regcache
, CORE_ADDR newpc
)
287 gdb_assert_not_reached ("linux target op low_set_pc is not implemented");
290 std::vector
<CORE_ADDR
>
291 linux_process_target::low_get_next_pcs (regcache
*regcache
)
293 gdb_assert_not_reached ("linux target op low_get_next_pcs is not "
298 linux_process_target::low_decr_pc_after_break ()
303 /* True if LWP is stopped in its stepping range. */
306 lwp_in_step_range (struct lwp_info
*lwp
)
308 CORE_ADDR pc
= lwp
->stop_pc
;
310 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
313 /* The event pipe registered as a waitable file in the event loop. */
314 static event_pipe linux_event_pipe
;
316 /* True if we're currently in async mode. */
317 #define target_is_async_p() (linux_event_pipe.is_open ())
319 static void send_sigstop (struct lwp_info
*lwp
);
321 /* Return non-zero if HEADER is a 64-bit ELF file. */
324 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
326 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
327 && header
->e_ident
[EI_MAG1
] == ELFMAG1
328 && header
->e_ident
[EI_MAG2
] == ELFMAG2
329 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
331 *machine
= header
->e_machine
;
332 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
339 /* Return non-zero if FILE is a 64-bit ELF file,
340 zero if the file is not a 64-bit ELF file,
341 and -1 if the file is not accessible or doesn't exist. */
344 elf_64_file_p (const char *file
, unsigned int *machine
)
349 fd
= open (file
, O_RDONLY
);
353 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
360 return elf_64_header_p (&header
, machine
);
363 /* Accepts an integer PID; Returns true if the executable PID is
364 running is a 64-bit ELF file.. */
367 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
371 sprintf (file
, "/proc/%d/exe", pid
);
372 return elf_64_file_p (file
, machine
);
376 linux_process_target::delete_lwp (lwp_info
*lwp
)
378 struct thread_info
*thr
= get_lwp_thread (lwp
);
380 threads_debug_printf ("deleting %ld", lwpid_of (thr
));
384 low_delete_thread (lwp
->arch_private
);
390 linux_process_target::low_delete_thread (arch_lwp_info
*info
)
392 /* Default implementation should be overridden if architecture-specific
393 info is being used. */
394 gdb_assert (info
== nullptr);
398 linux_process_target::add_linux_process (int pid
, int attached
)
400 struct process_info
*proc
;
402 proc
= add_process (pid
, attached
);
403 proc
->priv
= XCNEW (struct process_info_private
);
405 proc
->priv
->arch_private
= low_new_process ();
411 linux_process_target::low_new_process ()
417 linux_process_target::low_delete_process (arch_process_info
*info
)
419 /* Default implementation must be overridden if architecture-specific
421 gdb_assert (info
== nullptr);
425 linux_process_target::low_new_fork (process_info
*parent
, process_info
*child
)
431 linux_process_target::arch_setup_thread (thread_info
*thread
)
433 scoped_restore_current_thread restore_thread
;
434 switch_to_thread (thread
);
440 linux_process_target::handle_extended_wait (lwp_info
**orig_event_lwp
,
443 client_state
&cs
= get_client_state ();
444 struct lwp_info
*event_lwp
= *orig_event_lwp
;
445 int event
= linux_ptrace_get_extended_event (wstat
);
446 struct thread_info
*event_thr
= get_lwp_thread (event_lwp
);
447 struct lwp_info
*new_lwp
;
449 gdb_assert (event_lwp
->waitstatus
.kind () == TARGET_WAITKIND_IGNORE
);
451 /* All extended events we currently use are mid-syscall. Only
452 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
453 you have to be using PTRACE_SEIZE to get that. */
454 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
456 if ((event
== PTRACE_EVENT_FORK
) || (event
== PTRACE_EVENT_VFORK
)
457 || (event
== PTRACE_EVENT_CLONE
))
460 unsigned long new_pid
;
463 /* Get the pid of the new lwp. */
464 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_thr
), (PTRACE_TYPE_ARG3
) 0,
467 /* If we haven't already seen the new PID stop, wait for it now. */
468 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
470 /* The new child has a pending SIGSTOP. We can't affect it until it
471 hits the SIGSTOP, but we're already attached. */
473 ret
= my_waitpid (new_pid
, &status
, __WALL
);
476 perror_with_name ("waiting for new child");
477 else if (ret
!= new_pid
)
478 warning ("wait returned unexpected PID %d", ret
);
479 else if (!WIFSTOPPED (status
))
480 warning ("wait returned unexpected status 0x%x", status
);
483 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
485 struct process_info
*parent_proc
;
486 struct process_info
*child_proc
;
487 struct lwp_info
*child_lwp
;
488 struct thread_info
*child_thr
;
490 ptid
= ptid_t (new_pid
, new_pid
);
492 threads_debug_printf ("Got fork event from LWP %ld, "
494 ptid_of (event_thr
).lwp (),
497 /* Add the new process to the tables and clone the breakpoint
498 lists of the parent. We need to do this even if the new process
499 will be detached, since we will need the process object and the
500 breakpoints to remove any breakpoints from memory when we
501 detach, and the client side will access registers. */
502 child_proc
= add_linux_process (new_pid
, 0);
503 gdb_assert (child_proc
!= NULL
);
504 child_lwp
= add_lwp (ptid
);
505 gdb_assert (child_lwp
!= NULL
);
506 child_lwp
->stopped
= 1;
507 child_lwp
->must_set_ptrace_flags
= 1;
508 child_lwp
->status_pending_p
= 0;
509 child_thr
= get_lwp_thread (child_lwp
);
510 child_thr
->last_resume_kind
= resume_stop
;
511 child_thr
->last_status
.set_stopped (GDB_SIGNAL_0
);
513 /* If we're suspending all threads, leave this one suspended
514 too. If the fork/clone parent is stepping over a breakpoint,
515 all other threads have been suspended already. Leave the
516 child suspended too. */
517 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
518 || event_lwp
->bp_reinsert
!= 0)
520 threads_debug_printf ("leaving child suspended");
521 child_lwp
->suspended
= 1;
524 parent_proc
= get_thread_process (event_thr
);
525 child_proc
->attached
= parent_proc
->attached
;
527 if (event_lwp
->bp_reinsert
!= 0
528 && supports_software_single_step ()
529 && event
== PTRACE_EVENT_VFORK
)
531 /* If we leave single-step breakpoints there, child will
532 hit it, so uninsert single-step breakpoints from parent
533 (and child). Once vfork child is done, reinsert
534 them back to parent. */
535 uninsert_single_step_breakpoints (event_thr
);
538 clone_all_breakpoints (child_thr
, event_thr
);
540 target_desc_up tdesc
= allocate_target_description ();
541 copy_target_description (tdesc
.get (), parent_proc
->tdesc
);
542 child_proc
->tdesc
= tdesc
.release ();
544 /* Clone arch-specific process data. */
545 low_new_fork (parent_proc
, child_proc
);
547 /* Save fork info in the parent thread. */
548 if (event
== PTRACE_EVENT_FORK
)
549 event_lwp
->waitstatus
.set_forked (ptid
);
550 else if (event
== PTRACE_EVENT_VFORK
)
551 event_lwp
->waitstatus
.set_vforked (ptid
);
553 /* The status_pending field contains bits denoting the
554 extended event, so when the pending event is handled,
555 the handler will look at lwp->waitstatus. */
556 event_lwp
->status_pending_p
= 1;
557 event_lwp
->status_pending
= wstat
;
559 /* Link the threads until the parent event is passed on to
561 event_lwp
->fork_relative
= child_lwp
;
562 child_lwp
->fork_relative
= event_lwp
;
564 /* If the parent thread is doing step-over with single-step
565 breakpoints, the list of single-step breakpoints are cloned
566 from the parent's. Remove them from the child process.
567 In case of vfork, we'll reinsert them back once vforked
569 if (event_lwp
->bp_reinsert
!= 0
570 && supports_software_single_step ())
572 /* The child process is forked and stopped, so it is safe
573 to access its memory without stopping all other threads
574 from other processes. */
575 delete_single_step_breakpoints (child_thr
);
577 gdb_assert (has_single_step_breakpoints (event_thr
));
578 gdb_assert (!has_single_step_breakpoints (child_thr
));
581 /* Report the event. */
586 ("Got clone event from LWP %ld, new child is LWP %ld",
587 lwpid_of (event_thr
), new_pid
);
589 ptid
= ptid_t (pid_of (event_thr
), new_pid
);
590 new_lwp
= add_lwp (ptid
);
592 /* Either we're going to immediately resume the new thread
593 or leave it stopped. resume_one_lwp is a nop if it
594 thinks the thread is currently running, so set this first
595 before calling resume_one_lwp. */
596 new_lwp
->stopped
= 1;
598 /* If we're suspending all threads, leave this one suspended
599 too. If the fork/clone parent is stepping over a breakpoint,
600 all other threads have been suspended already. Leave the
601 child suspended too. */
602 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
603 || event_lwp
->bp_reinsert
!= 0)
604 new_lwp
->suspended
= 1;
606 /* Normally we will get the pending SIGSTOP. But in some cases
607 we might get another signal delivered to the group first.
608 If we do get another signal, be sure not to lose it. */
609 if (WSTOPSIG (status
) != SIGSTOP
)
611 new_lwp
->stop_expected
= 1;
612 new_lwp
->status_pending_p
= 1;
613 new_lwp
->status_pending
= status
;
615 else if (cs
.report_thread_events
)
617 new_lwp
->waitstatus
.set_thread_created ();
618 new_lwp
->status_pending_p
= 1;
619 new_lwp
->status_pending
= status
;
623 thread_db_notice_clone (event_thr
, ptid
);
626 /* Don't report the event. */
629 else if (event
== PTRACE_EVENT_VFORK_DONE
)
631 event_lwp
->waitstatus
.set_vfork_done ();
633 if (event_lwp
->bp_reinsert
!= 0 && supports_software_single_step ())
635 reinsert_single_step_breakpoints (event_thr
);
637 gdb_assert (has_single_step_breakpoints (event_thr
));
640 /* Report the event. */
643 else if (event
== PTRACE_EVENT_EXEC
&& cs
.report_exec_events
)
645 struct process_info
*proc
;
646 std::vector
<int> syscalls_to_catch
;
650 threads_debug_printf ("Got exec event from LWP %ld",
651 lwpid_of (event_thr
));
653 /* Get the event ptid. */
654 event_ptid
= ptid_of (event_thr
);
655 event_pid
= event_ptid
.pid ();
657 /* Save the syscall list from the execing process. */
658 proc
= get_thread_process (event_thr
);
659 syscalls_to_catch
= std::move (proc
->syscalls_to_catch
);
661 /* Delete the execing process and all its threads. */
663 switch_to_thread (nullptr);
665 /* Create a new process/lwp/thread. */
666 proc
= add_linux_process (event_pid
, 0);
667 event_lwp
= add_lwp (event_ptid
);
668 event_thr
= get_lwp_thread (event_lwp
);
669 gdb_assert (current_thread
== event_thr
);
670 arch_setup_thread (event_thr
);
672 /* Set the event status. */
673 event_lwp
->waitstatus
.set_execd
675 (linux_proc_pid_to_exec_file (lwpid_of (event_thr
))));
677 /* Mark the exec status as pending. */
678 event_lwp
->stopped
= 1;
679 event_lwp
->status_pending_p
= 1;
680 event_lwp
->status_pending
= wstat
;
681 event_thr
->last_resume_kind
= resume_continue
;
682 event_thr
->last_status
.set_ignore ();
684 /* Update syscall state in the new lwp, effectively mid-syscall too. */
685 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
687 /* Restore the list to catch. Don't rely on the client, which is free
688 to avoid sending a new list when the architecture doesn't change.
689 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
690 proc
->syscalls_to_catch
= std::move (syscalls_to_catch
);
692 /* Report the event. */
693 *orig_event_lwp
= event_lwp
;
697 internal_error (__FILE__
, __LINE__
, _("unknown ptrace event %d"), event
);
701 linux_process_target::get_pc (lwp_info
*lwp
)
703 struct regcache
*regcache
;
706 if (!low_supports_breakpoints ())
709 scoped_restore_current_thread restore_thread
;
710 switch_to_thread (get_lwp_thread (lwp
));
712 regcache
= get_thread_regcache (current_thread
, 1);
713 pc
= low_get_pc (regcache
);
715 threads_debug_printf ("pc is 0x%lx", (long) pc
);
721 linux_process_target::get_syscall_trapinfo (lwp_info
*lwp
, int *sysno
)
723 struct regcache
*regcache
;
725 scoped_restore_current_thread restore_thread
;
726 switch_to_thread (get_lwp_thread (lwp
));
728 regcache
= get_thread_regcache (current_thread
, 1);
729 low_get_syscall_trapinfo (regcache
, sysno
);
731 threads_debug_printf ("get_syscall_trapinfo sysno %d", *sysno
);
735 linux_process_target::low_get_syscall_trapinfo (regcache
*regcache
, int *sysno
)
737 /* By default, report an unknown system call number. */
738 *sysno
= UNKNOWN_SYSCALL
;
742 linux_process_target::save_stop_reason (lwp_info
*lwp
)
745 CORE_ADDR sw_breakpoint_pc
;
746 #if USE_SIGTRAP_SIGINFO
750 if (!low_supports_breakpoints ())
754 sw_breakpoint_pc
= pc
- low_decr_pc_after_break ();
756 /* breakpoint_at reads from the current thread. */
757 scoped_restore_current_thread restore_thread
;
758 switch_to_thread (get_lwp_thread (lwp
));
760 #if USE_SIGTRAP_SIGINFO
761 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
762 (PTRACE_TYPE_ARG3
) 0, &siginfo
) == 0)
764 if (siginfo
.si_signo
== SIGTRAP
)
766 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
)
767 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
769 /* The si_code is ambiguous on this arch -- check debug
771 if (!check_stopped_by_watchpoint (lwp
))
772 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
774 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
))
776 /* If we determine the LWP stopped for a SW breakpoint,
777 trust it. Particularly don't check watchpoint
778 registers, because at least on s390, we'd find
779 stopped-by-watchpoint as long as there's a watchpoint
781 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
783 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
785 /* This can indicate either a hardware breakpoint or
786 hardware watchpoint. Check debug registers. */
787 if (!check_stopped_by_watchpoint (lwp
))
788 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
790 else if (siginfo
.si_code
== TRAP_TRACE
)
792 /* We may have single stepped an instruction that
793 triggered a watchpoint. In that case, on some
794 architectures (such as x86), instead of TRAP_HWBKPT,
795 si_code indicates TRAP_TRACE, and we need to check
796 the debug registers separately. */
797 if (!check_stopped_by_watchpoint (lwp
))
798 lwp
->stop_reason
= TARGET_STOPPED_BY_SINGLE_STEP
;
803 /* We may have just stepped a breakpoint instruction. E.g., in
804 non-stop mode, GDB first tells the thread A to step a range, and
805 then the user inserts a breakpoint inside the range. In that
806 case we need to report the breakpoint PC. */
807 if ((!lwp
->stepping
|| lwp
->stop_pc
== sw_breakpoint_pc
)
808 && low_breakpoint_at (sw_breakpoint_pc
))
809 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
811 if (hardware_breakpoint_inserted_here (pc
))
812 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
814 if (lwp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
)
815 check_stopped_by_watchpoint (lwp
);
818 if (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
)
821 ("%s stopped by software breakpoint",
822 target_pid_to_str (ptid_of (get_lwp_thread (lwp
))).c_str ());
824 /* Back up the PC if necessary. */
825 if (pc
!= sw_breakpoint_pc
)
827 struct regcache
*regcache
828 = get_thread_regcache (current_thread
, 1);
829 low_set_pc (regcache
, sw_breakpoint_pc
);
832 /* Update this so we record the correct stop PC below. */
833 pc
= sw_breakpoint_pc
;
835 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
837 ("%s stopped by hardware breakpoint",
838 target_pid_to_str (ptid_of (get_lwp_thread (lwp
))).c_str ());
839 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
841 ("%s stopped by hardware watchpoint",
842 target_pid_to_str (ptid_of (get_lwp_thread (lwp
))).c_str ());
843 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
)
845 ("%s stopped by trace",
846 target_pid_to_str (ptid_of (get_lwp_thread (lwp
))).c_str ());
853 linux_process_target::add_lwp (ptid_t ptid
)
855 lwp_info
*lwp
= new lwp_info
;
857 lwp
->thread
= add_thread (ptid
, lwp
);
859 low_new_thread (lwp
);
865 linux_process_target::low_new_thread (lwp_info
*info
)
870 /* Callback to be used when calling fork_inferior, responsible for
871 actually initiating the tracing of the inferior. */
876 if (ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0,
877 (PTRACE_TYPE_ARG4
) 0) < 0)
878 trace_start_error_with_name ("ptrace");
880 if (setpgid (0, 0) < 0)
881 trace_start_error_with_name ("setpgid");
883 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
884 stdout to stderr so that inferior i/o doesn't corrupt the connection.
885 Also, redirect stdin to /dev/null. */
886 if (remote_connection_is_stdio ())
889 trace_start_error_with_name ("close");
890 if (open ("/dev/null", O_RDONLY
) < 0)
891 trace_start_error_with_name ("open");
893 trace_start_error_with_name ("dup2");
894 if (write (2, "stdin/stdout redirected\n",
895 sizeof ("stdin/stdout redirected\n") - 1) < 0)
897 /* Errors ignored. */;
902 /* Start an inferior process and returns its pid.
903 PROGRAM is the name of the program to be started, and PROGRAM_ARGS
904 are its arguments. */
907 linux_process_target::create_inferior (const char *program
,
908 const std::vector
<char *> &program_args
)
910 client_state
&cs
= get_client_state ();
911 struct lwp_info
*new_lwp
;
916 maybe_disable_address_space_randomization restore_personality
917 (cs
.disable_randomization
);
918 std::string str_program_args
= construct_inferior_arguments (program_args
);
920 pid
= fork_inferior (program
,
921 str_program_args
.c_str (),
922 get_environ ()->envp (), linux_ptrace_fun
,
923 NULL
, NULL
, NULL
, NULL
);
926 add_linux_process (pid
, 0);
928 ptid
= ptid_t (pid
, pid
);
929 new_lwp
= add_lwp (ptid
);
930 new_lwp
->must_set_ptrace_flags
= 1;
932 post_fork_inferior (pid
, program
);
937 /* Implement the post_create_inferior target_ops method. */
940 linux_process_target::post_create_inferior ()
942 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
946 if (lwp
->must_set_ptrace_flags
)
948 struct process_info
*proc
= current_process ();
949 int options
= linux_low_ptrace_options (proc
->attached
);
951 linux_enable_event_reporting (lwpid_of (current_thread
), options
);
952 lwp
->must_set_ptrace_flags
= 0;
957 linux_process_target::attach_lwp (ptid_t ptid
)
959 struct lwp_info
*new_lwp
;
960 int lwpid
= ptid
.lwp ();
962 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0)
966 new_lwp
= add_lwp (ptid
);
968 /* We need to wait for SIGSTOP before being able to make the next
969 ptrace call on this LWP. */
970 new_lwp
->must_set_ptrace_flags
= 1;
972 if (linux_proc_pid_is_stopped (lwpid
))
974 threads_debug_printf ("Attached to a stopped process");
976 /* The process is definitely stopped. It is in a job control
977 stop, unless the kernel predates the TASK_STOPPED /
978 TASK_TRACED distinction, in which case it might be in a
979 ptrace stop. Make sure it is in a ptrace stop; from there we
980 can kill it, signal it, et cetera.
982 First make sure there is a pending SIGSTOP. Since we are
983 already attached, the process can not transition from stopped
984 to running without a PTRACE_CONT; so we know this signal will
985 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
986 probably already in the queue (unless this kernel is old
987 enough to use TASK_STOPPED for ptrace stops); but since
988 SIGSTOP is not an RT signal, it can only be queued once. */
989 kill_lwp (lwpid
, SIGSTOP
);
991 /* Finally, resume the stopped process. This will deliver the
992 SIGSTOP (or a higher priority signal, just like normal
993 PTRACE_ATTACH), which we'll catch later on. */
994 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
997 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1000 There are several cases to consider here:
1002 1) gdbserver has already attached to the process and is being notified
1003 of a new thread that is being created.
1004 In this case we should ignore that SIGSTOP and resume the
1005 process. This is handled below by setting stop_expected = 1,
1006 and the fact that add_thread sets last_resume_kind ==
1009 2) This is the first thread (the process thread), and we're attaching
1010 to it via attach_inferior.
1011 In this case we want the process thread to stop.
1012 This is handled by having linux_attach set last_resume_kind ==
1013 resume_stop after we return.
1015 If the pid we are attaching to is also the tgid, we attach to and
1016 stop all the existing threads. Otherwise, we attach to pid and
1017 ignore any other threads in the same group as this pid.
1019 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1021 In this case we want the thread to stop.
1022 FIXME: This case is currently not properly handled.
1023 We should wait for the SIGSTOP but don't. Things work apparently
1024 because enough time passes between when we ptrace (ATTACH) and when
1025 gdb makes the next ptrace call on the thread.
1027 On the other hand, if we are currently trying to stop all threads, we
1028 should treat the new thread as if we had sent it a SIGSTOP. This works
1029 because we are guaranteed that the add_lwp call above added us to the
1030 end of the list, and so the new thread has not yet reached
1031 wait_for_sigstop (but will). */
1032 new_lwp
->stop_expected
= 1;
1037 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1038 already attached. Returns true if a new LWP is found, false
1042 attach_proc_task_lwp_callback (ptid_t ptid
)
1044 /* Is this a new thread? */
1045 if (find_thread_ptid (ptid
) == NULL
)
1047 int lwpid
= ptid
.lwp ();
1050 threads_debug_printf ("Found new lwp %d", lwpid
);
1052 err
= the_linux_target
->attach_lwp (ptid
);
1054 /* Be quiet if we simply raced with the thread exiting. EPERM
1055 is returned if the thread's task still exists, and is marked
1056 as exited or zombie, as well as other conditions, so in that
1057 case, confirm the status in /proc/PID/status. */
1059 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1060 threads_debug_printf
1061 ("Cannot attach to lwp %d: thread is gone (%d: %s)",
1062 lwpid
, err
, safe_strerror (err
));
1066 = linux_ptrace_attach_fail_reason_string (ptid
, err
);
1068 warning (_("Cannot attach to lwp %d: %s"), lwpid
, reason
.c_str ());
1076 static void async_file_mark (void);
1078 /* Attach to PID. If PID is the tgid, attach to it and all
1082 linux_process_target::attach (unsigned long pid
)
1084 struct process_info
*proc
;
1085 struct thread_info
*initial_thread
;
1086 ptid_t ptid
= ptid_t (pid
, pid
);
1089 proc
= add_linux_process (pid
, 1);
1091 /* Attach to PID. We will check for other threads
1093 err
= attach_lwp (ptid
);
1096 remove_process (proc
);
1098 std::string reason
= linux_ptrace_attach_fail_reason_string (ptid
, err
);
1099 error ("Cannot attach to process %ld: %s", pid
, reason
.c_str ());
1102 /* Don't ignore the initial SIGSTOP if we just attached to this
1103 process. It will be collected by wait shortly. */
1104 initial_thread
= find_thread_ptid (ptid_t (pid
, pid
));
1105 initial_thread
->last_resume_kind
= resume_stop
;
1107 /* We must attach to every LWP. If /proc is mounted, use that to
1108 find them now. On the one hand, the inferior may be using raw
1109 clone instead of using pthreads. On the other hand, even if it
1110 is using pthreads, GDB may not be connected yet (thread_db needs
1111 to do symbol lookups, through qSymbol). Also, thread_db walks
1112 structures in the inferior's address space to find the list of
1113 threads/LWPs, and those structures may well be corrupted. Note
1114 that once thread_db is loaded, we'll still use it to list threads
1115 and associate pthread info with each LWP. */
1116 linux_proc_attach_tgid_threads (pid
, attach_proc_task_lwp_callback
);
1118 /* GDB will shortly read the xml target description for this
1119 process, to figure out the process' architecture. But the target
1120 description is only filled in when the first process/thread in
1121 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1122 that now, otherwise, if GDB is fast enough, it could read the
1123 target description _before_ that initial stop. */
1126 struct lwp_info
*lwp
;
1128 ptid_t pid_ptid
= ptid_t (pid
);
1130 lwpid
= wait_for_event_filtered (pid_ptid
, pid_ptid
, &wstat
, __WALL
);
1131 gdb_assert (lwpid
> 0);
1133 lwp
= find_lwp_pid (ptid_t (lwpid
));
1135 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGSTOP
)
1137 lwp
->status_pending_p
= 1;
1138 lwp
->status_pending
= wstat
;
1141 initial_thread
->last_resume_kind
= resume_continue
;
1145 gdb_assert (proc
->tdesc
!= NULL
);
1152 last_thread_of_process_p (int pid
)
1154 bool seen_one
= false;
1156 thread_info
*thread
= find_thread (pid
, [&] (thread_info
*thr_arg
)
1160 /* This is the first thread of this process we see. */
1166 /* This is the second thread of this process we see. */
1171 return thread
== NULL
;
1177 linux_kill_one_lwp (struct lwp_info
*lwp
)
1179 struct thread_info
*thr
= get_lwp_thread (lwp
);
1180 int pid
= lwpid_of (thr
);
1182 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1183 there is no signal context, and ptrace(PTRACE_KILL) (or
1184 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1185 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1186 alternative is to kill with SIGKILL. We only need one SIGKILL
1187 per process, not one for each thread. But since we still support
1188 support debugging programs using raw clone without CLONE_THREAD,
1189 we send one for each thread. For years, we used PTRACE_KILL
1190 only, so we're being a bit paranoid about some old kernels where
1191 PTRACE_KILL might work better (dubious if there are any such, but
1192 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1193 second, and so we're fine everywhere. */
1196 kill_lwp (pid
, SIGKILL
);
1199 int save_errno
= errno
;
1201 threads_debug_printf ("kill_lwp (SIGKILL) %s, 0, 0 (%s)",
1202 target_pid_to_str (ptid_of (thr
)).c_str (),
1203 save_errno
? safe_strerror (save_errno
) : "OK");
1207 ptrace (PTRACE_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1210 int save_errno
= errno
;
1212 threads_debug_printf ("PTRACE_KILL %s, 0, 0 (%s)",
1213 target_pid_to_str (ptid_of (thr
)).c_str (),
1214 save_errno
? safe_strerror (save_errno
) : "OK");
1218 /* Kill LWP and wait for it to die. */
1221 kill_wait_lwp (struct lwp_info
*lwp
)
1223 struct thread_info
*thr
= get_lwp_thread (lwp
);
1224 int pid
= ptid_of (thr
).pid ();
1225 int lwpid
= ptid_of (thr
).lwp ();
1229 threads_debug_printf ("killing lwp %d, for pid: %d", lwpid
, pid
);
1233 linux_kill_one_lwp (lwp
);
1235 /* Make sure it died. Notes:
1237 - The loop is most likely unnecessary.
1239 - We don't use wait_for_event as that could delete lwps
1240 while we're iterating over them. We're not interested in
1241 any pending status at this point, only in making sure all
1242 wait status on the kernel side are collected until the
1245 - We don't use __WALL here as the __WALL emulation relies on
1246 SIGCHLD, and killing a stopped process doesn't generate
1247 one, nor an exit status.
1249 res
= my_waitpid (lwpid
, &wstat
, 0);
1250 if (res
== -1 && errno
== ECHILD
)
1251 res
= my_waitpid (lwpid
, &wstat
, __WCLONE
);
1252 } while (res
> 0 && WIFSTOPPED (wstat
));
1254 /* Even if it was stopped, the child may have already disappeared.
1255 E.g., if it was killed by SIGKILL. */
1256 if (res
< 0 && errno
!= ECHILD
)
1257 perror_with_name ("kill_wait_lwp");
1260 /* Callback for `for_each_thread'. Kills an lwp of a given process,
1261 except the leader. */
1264 kill_one_lwp_callback (thread_info
*thread
, int pid
)
1266 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1268 /* We avoid killing the first thread here, because of a Linux kernel (at
1269 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1270 the children get a chance to be reaped, it will remain a zombie
1273 if (lwpid_of (thread
) == pid
)
1275 threads_debug_printf ("is last of process %s",
1276 target_pid_to_str (thread
->id
).c_str ());
1280 kill_wait_lwp (lwp
);
1284 linux_process_target::kill (process_info
*process
)
1286 int pid
= process
->pid
;
1288 /* If we're killing a running inferior, make sure it is stopped
1289 first, as PTRACE_KILL will not work otherwise. */
1290 stop_all_lwps (0, NULL
);
1292 for_each_thread (pid
, [&] (thread_info
*thread
)
1294 kill_one_lwp_callback (thread
, pid
);
1297 /* See the comment in linux_kill_one_lwp. We did not kill the first
1298 thread in the list, so do so now. */
1299 lwp_info
*lwp
= find_lwp_pid (ptid_t (pid
));
1302 threads_debug_printf ("cannot find lwp for pid: %d", pid
);
1304 kill_wait_lwp (lwp
);
1308 /* Since we presently can only stop all lwps of all processes, we
1309 need to unstop lwps of other processes. */
1310 unstop_all_lwps (0, NULL
);
1314 /* Get pending signal of THREAD, for detaching purposes. This is the
1315 signal the thread last stopped for, which we need to deliver to the
1316 thread when detaching, otherwise, it'd be suppressed/lost. */
1319 get_detach_signal (struct thread_info
*thread
)
1321 client_state
&cs
= get_client_state ();
1322 enum gdb_signal signo
= GDB_SIGNAL_0
;
1324 struct lwp_info
*lp
= get_thread_lwp (thread
);
1326 if (lp
->status_pending_p
)
1327 status
= lp
->status_pending
;
1330 /* If the thread had been suspended by gdbserver, and it stopped
1331 cleanly, then it'll have stopped with SIGSTOP. But we don't
1332 want to deliver that SIGSTOP. */
1333 if (thread
->last_status
.kind () != TARGET_WAITKIND_STOPPED
1334 || thread
->last_status
.sig () == GDB_SIGNAL_0
)
1337 /* Otherwise, we may need to deliver the signal we
1339 status
= lp
->last_status
;
1342 if (!WIFSTOPPED (status
))
1344 threads_debug_printf ("lwp %s hasn't stopped: no pending signal",
1345 target_pid_to_str (ptid_of (thread
)).c_str ());
1349 /* Extended wait statuses aren't real SIGTRAPs. */
1350 if (WSTOPSIG (status
) == SIGTRAP
&& linux_is_extended_waitstatus (status
))
1352 threads_debug_printf ("lwp %s had stopped with extended "
1353 "status: no pending signal",
1354 target_pid_to_str (ptid_of (thread
)).c_str ());
1358 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1360 if (cs
.program_signals_p
&& !cs
.program_signals
[signo
])
1362 threads_debug_printf ("lwp %s had signal %s, but it is in nopass state",
1363 target_pid_to_str (ptid_of (thread
)).c_str (),
1364 gdb_signal_to_string (signo
));
1367 else if (!cs
.program_signals_p
1368 /* If we have no way to know which signals GDB does not
1369 want to have passed to the program, assume
1370 SIGTRAP/SIGINT, which is GDB's default. */
1371 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1373 threads_debug_printf ("lwp %s had signal %s, "
1374 "but we don't know if we should pass it. "
1376 target_pid_to_str (ptid_of (thread
)).c_str (),
1377 gdb_signal_to_string (signo
));
1382 threads_debug_printf ("lwp %s has pending signal %s: delivering it",
1383 target_pid_to_str (ptid_of (thread
)).c_str (),
1384 gdb_signal_to_string (signo
));
1386 return WSTOPSIG (status
);
1391 linux_process_target::detach_one_lwp (lwp_info
*lwp
)
1393 struct thread_info
*thread
= get_lwp_thread (lwp
);
1397 /* If there is a pending SIGSTOP, get rid of it. */
1398 if (lwp
->stop_expected
)
1400 threads_debug_printf ("Sending SIGCONT to %s",
1401 target_pid_to_str (ptid_of (thread
)).c_str ());
1403 kill_lwp (lwpid_of (thread
), SIGCONT
);
1404 lwp
->stop_expected
= 0;
1407 /* Pass on any pending signal for this thread. */
1408 sig
= get_detach_signal (thread
);
1410 /* Preparing to resume may try to write registers, and fail if the
1411 lwp is zombie. If that happens, ignore the error. We'll handle
1412 it below, when detach fails with ESRCH. */
1415 /* Flush any pending changes to the process's registers. */
1416 regcache_invalidate_thread (thread
);
1418 /* Finally, let it resume. */
1419 low_prepare_to_resume (lwp
);
1421 catch (const gdb_exception_error
&ex
)
1423 if (!check_ptrace_stopped_lwp_gone (lwp
))
1427 lwpid
= lwpid_of (thread
);
1428 if (ptrace (PTRACE_DETACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0,
1429 (PTRACE_TYPE_ARG4
) (long) sig
) < 0)
1431 int save_errno
= errno
;
1433 /* We know the thread exists, so ESRCH must mean the lwp is
1434 zombie. This can happen if one of the already-detached
1435 threads exits the whole thread group. In that case we're
1436 still attached, and must reap the lwp. */
1437 if (save_errno
== ESRCH
)
1441 ret
= my_waitpid (lwpid
, &status
, __WALL
);
1444 warning (_("Couldn't reap LWP %d while detaching: %s"),
1445 lwpid
, safe_strerror (errno
));
1447 else if (!WIFEXITED (status
) && !WIFSIGNALED (status
))
1449 warning (_("Reaping LWP %d while detaching "
1450 "returned unexpected status 0x%x"),
1456 error (_("Can't detach %s: %s"),
1457 target_pid_to_str (ptid_of (thread
)).c_str (),
1458 safe_strerror (save_errno
));
1462 threads_debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)",
1463 target_pid_to_str (ptid_of (thread
)).c_str (),
1470 linux_process_target::detach (process_info
*process
)
1472 struct lwp_info
*main_lwp
;
1474 /* As there's a step over already in progress, let it finish first,
1475 otherwise nesting a stabilize_threads operation on top gets real
1477 complete_ongoing_step_over ();
1479 /* Stop all threads before detaching. First, ptrace requires that
1480 the thread is stopped to successfully detach. Second, thread_db
1481 may need to uninstall thread event breakpoints from memory, which
1482 only works with a stopped process anyway. */
1483 stop_all_lwps (0, NULL
);
1485 #ifdef USE_THREAD_DB
1486 thread_db_detach (process
);
1489 /* Stabilize threads (move out of jump pads). */
1490 target_stabilize_threads ();
1492 /* Detach from the clone lwps first. If the thread group exits just
1493 while we're detaching, we must reap the clone lwps before we're
1494 able to reap the leader. */
1495 for_each_thread (process
->pid
, [this] (thread_info
*thread
)
1497 /* We don't actually detach from the thread group leader just yet.
1498 If the thread group exits, we must reap the zombie clone lwps
1499 before we're able to reap the leader. */
1500 if (thread
->id
.pid () == thread
->id
.lwp ())
1503 lwp_info
*lwp
= get_thread_lwp (thread
);
1504 detach_one_lwp (lwp
);
1507 main_lwp
= find_lwp_pid (ptid_t (process
->pid
));
1508 detach_one_lwp (main_lwp
);
1512 /* Since we presently can only stop all lwps of all processes, we
1513 need to unstop lwps of other processes. */
1514 unstop_all_lwps (0, NULL
);
1518 /* Remove all LWPs that belong to process PROC from the lwp list. */
1521 linux_process_target::mourn (process_info
*process
)
1523 struct process_info_private
*priv
;
1525 #ifdef USE_THREAD_DB
1526 thread_db_mourn (process
);
1529 for_each_thread (process
->pid
, [this] (thread_info
*thread
)
1531 delete_lwp (get_thread_lwp (thread
));
1534 /* Freeing all private data. */
1535 priv
= process
->priv
;
1536 low_delete_process (priv
->arch_private
);
1538 process
->priv
= NULL
;
1540 remove_process (process
);
1544 linux_process_target::join (int pid
)
1549 ret
= my_waitpid (pid
, &status
, 0);
1550 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1552 } while (ret
!= -1 || errno
!= ECHILD
);
1555 /* Return true if the given thread is still alive. */
1558 linux_process_target::thread_alive (ptid_t ptid
)
1560 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1562 /* We assume we always know if a thread exits. If a whole process
1563 exited but we still haven't been able to report it to GDB, we'll
1564 hold on to the last lwp of the dead process. */
1566 return !lwp_is_marked_dead (lwp
);
1572 linux_process_target::thread_still_has_status_pending (thread_info
*thread
)
1574 struct lwp_info
*lp
= get_thread_lwp (thread
);
1576 if (!lp
->status_pending_p
)
1579 if (thread
->last_resume_kind
!= resume_stop
1580 && (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1581 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
))
1586 gdb_assert (lp
->last_status
!= 0);
1590 scoped_restore_current_thread restore_thread
;
1591 switch_to_thread (thread
);
1593 if (pc
!= lp
->stop_pc
)
1595 threads_debug_printf ("PC of %ld changed",
1600 #if !USE_SIGTRAP_SIGINFO
1601 else if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1602 && !low_breakpoint_at (pc
))
1604 threads_debug_printf ("previous SW breakpoint of %ld gone",
1608 else if (lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
1609 && !hardware_breakpoint_inserted_here (pc
))
1611 threads_debug_printf ("previous HW breakpoint of %ld gone",
1619 threads_debug_printf ("discarding pending breakpoint status");
1620 lp
->status_pending_p
= 0;
1628 /* Returns true if LWP is resumed from the client's perspective. */
1631 lwp_resumed (struct lwp_info
*lwp
)
1633 struct thread_info
*thread
= get_lwp_thread (lwp
);
1635 if (thread
->last_resume_kind
!= resume_stop
)
1638 /* Did gdb send us a `vCont;t', but we haven't reported the
1639 corresponding stop to gdb yet? If so, the thread is still
1640 resumed/running from gdb's perspective. */
1641 if (thread
->last_resume_kind
== resume_stop
1642 && thread
->last_status
.kind () == TARGET_WAITKIND_IGNORE
)
1649 linux_process_target::status_pending_p_callback (thread_info
*thread
,
1652 struct lwp_info
*lp
= get_thread_lwp (thread
);
1654 /* Check if we're only interested in events from a specific process
1655 or a specific LWP. */
1656 if (!thread
->id
.matches (ptid
))
1659 if (!lwp_resumed (lp
))
1662 if (lp
->status_pending_p
1663 && !thread_still_has_status_pending (thread
))
1665 resume_one_lwp (lp
, lp
->stepping
, GDB_SIGNAL_0
, NULL
);
1669 return lp
->status_pending_p
;
1673 find_lwp_pid (ptid_t ptid
)
1675 thread_info
*thread
= find_thread ([&] (thread_info
*thr_arg
)
1677 int lwp
= ptid
.lwp () != 0 ? ptid
.lwp () : ptid
.pid ();
1678 return thr_arg
->id
.lwp () == lwp
;
1684 return get_thread_lwp (thread
);
1687 /* Return the number of known LWPs in the tgid given by PID. */
1694 for_each_thread (pid
, [&] (thread_info
*thread
)
1702 /* See nat/linux-nat.h. */
1705 iterate_over_lwps (ptid_t filter
,
1706 gdb::function_view
<iterate_over_lwps_ftype
> callback
)
1708 thread_info
*thread
= find_thread (filter
, [&] (thread_info
*thr_arg
)
1710 lwp_info
*lwp
= get_thread_lwp (thr_arg
);
1712 return callback (lwp
);
1718 return get_thread_lwp (thread
);
1722 linux_process_target::check_zombie_leaders ()
1724 for_each_process ([this] (process_info
*proc
) {
1725 pid_t leader_pid
= pid_of (proc
);
1726 struct lwp_info
*leader_lp
;
1728 leader_lp
= find_lwp_pid (ptid_t (leader_pid
));
1730 threads_debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1731 "num_lwps=%d, zombie=%d",
1732 leader_pid
, leader_lp
!= NULL
, num_lwps (leader_pid
),
1733 linux_proc_pid_is_zombie (leader_pid
));
1735 if (leader_lp
!= NULL
&& !leader_lp
->stopped
1736 /* Check if there are other threads in the group, as we may
1737 have raced with the inferior simply exiting. */
1738 && !last_thread_of_process_p (leader_pid
)
1739 && linux_proc_pid_is_zombie (leader_pid
))
1741 /* A leader zombie can mean one of two things:
1743 - It exited, and there's an exit status pending
1744 available, or only the leader exited (not the whole
1745 program). In the latter case, we can't waitpid the
1746 leader's exit status until all other threads are gone.
1748 - There are 3 or more threads in the group, and a thread
1749 other than the leader exec'd. On an exec, the Linux
1750 kernel destroys all other threads (except the execing
1751 one) in the thread group, and resets the execing thread's
1752 tid to the tgid. No exit notification is sent for the
1753 execing thread -- from the ptracer's perspective, it
1754 appears as though the execing thread just vanishes.
1755 Until we reap all other threads except the leader and the
1756 execing thread, the leader will be zombie, and the
1757 execing thread will be in `D (disc sleep)'. As soon as
1758 all other threads are reaped, the execing thread changes
1759 it's tid to the tgid, and the previous (zombie) leader
1760 vanishes, giving place to the "new" leader. We could try
1761 distinguishing the exit and exec cases, by waiting once
1762 more, and seeing if something comes out, but it doesn't
1763 sound useful. The previous leader _does_ go away, and
1764 we'll re-add the new one once we see the exec event
1765 (which is just the same as what would happen if the
1766 previous leader did exit voluntarily before some other
1769 threads_debug_printf ("Thread group leader %d zombie "
1770 "(it exited, or another thread execd).",
1773 delete_lwp (leader_lp
);
1778 /* Callback for `find_thread'. Returns the first LWP that is not
1782 not_stopped_callback (thread_info
*thread
, ptid_t filter
)
1784 if (!thread
->id
.matches (filter
))
1787 lwp_info
*lwp
= get_thread_lwp (thread
);
1789 return !lwp
->stopped
;
1792 /* Increment LWP's suspend count. */
1795 lwp_suspended_inc (struct lwp_info
*lwp
)
1799 if (lwp
->suspended
> 4)
1800 threads_debug_printf
1801 ("LWP %ld has a suspiciously high suspend count, suspended=%d",
1802 lwpid_of (get_lwp_thread (lwp
)), lwp
->suspended
);
1805 /* Decrement LWP's suspend count. */
1808 lwp_suspended_decr (struct lwp_info
*lwp
)
1812 if (lwp
->suspended
< 0)
1814 struct thread_info
*thread
= get_lwp_thread (lwp
);
1816 internal_error (__FILE__
, __LINE__
,
1817 "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread
),
1822 /* This function should only be called if the LWP got a SIGTRAP.
1824 Handle any tracepoint steps or hits. Return true if a tracepoint
1825 event was handled, 0 otherwise. */
1828 handle_tracepoints (struct lwp_info
*lwp
)
1830 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1831 int tpoint_related_event
= 0;
1833 gdb_assert (lwp
->suspended
== 0);
1835 /* If this tracepoint hit causes a tracing stop, we'll immediately
1836 uninsert tracepoints. To do this, we temporarily pause all
1837 threads, unpatch away, and then unpause threads. We need to make
1838 sure the unpausing doesn't resume LWP too. */
1839 lwp_suspended_inc (lwp
);
1841 /* And we need to be sure that any all-threads-stopping doesn't try
1842 to move threads out of the jump pads, as it could deadlock the
1843 inferior (LWP could be in the jump pad, maybe even holding the
1846 /* Do any necessary step collect actions. */
1847 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1849 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1851 /* See if we just hit a tracepoint and do its main collect
1853 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1855 lwp_suspended_decr (lwp
);
1857 gdb_assert (lwp
->suspended
== 0);
1858 gdb_assert (!stabilizing_threads
1859 || (lwp
->collecting_fast_tracepoint
1860 != fast_tpoint_collect_result::not_collecting
));
1862 if (tpoint_related_event
)
1864 threads_debug_printf ("got a tracepoint event");
1871 fast_tpoint_collect_result
1872 linux_process_target::linux_fast_tracepoint_collecting
1873 (lwp_info
*lwp
, fast_tpoint_collect_status
*status
)
1875 CORE_ADDR thread_area
;
1876 struct thread_info
*thread
= get_lwp_thread (lwp
);
1878 /* Get the thread area address. This is used to recognize which
1879 thread is which when tracing with the in-process agent library.
1880 We don't read anything from the address, and treat it as opaque;
1881 it's the address itself that we assume is unique per-thread. */
1882 if (low_get_thread_area (lwpid_of (thread
), &thread_area
) == -1)
1883 return fast_tpoint_collect_result::not_collecting
;
1885 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
1889 linux_process_target::low_get_thread_area (int lwpid
, CORE_ADDR
*addrp
)
1895 linux_process_target::maybe_move_out_of_jump_pad (lwp_info
*lwp
, int *wstat
)
1897 scoped_restore_current_thread restore_thread
;
1898 switch_to_thread (get_lwp_thread (lwp
));
1901 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
1902 && supports_fast_tracepoints ()
1903 && agent_loaded_p ())
1905 struct fast_tpoint_collect_status status
;
1907 threads_debug_printf
1908 ("Checking whether LWP %ld needs to move out of the jump pad.",
1909 lwpid_of (current_thread
));
1911 fast_tpoint_collect_result r
1912 = linux_fast_tracepoint_collecting (lwp
, &status
);
1915 || (WSTOPSIG (*wstat
) != SIGILL
1916 && WSTOPSIG (*wstat
) != SIGFPE
1917 && WSTOPSIG (*wstat
) != SIGSEGV
1918 && WSTOPSIG (*wstat
) != SIGBUS
))
1920 lwp
->collecting_fast_tracepoint
= r
;
1922 if (r
!= fast_tpoint_collect_result::not_collecting
)
1924 if (r
== fast_tpoint_collect_result::before_insn
1925 && lwp
->exit_jump_pad_bkpt
== NULL
)
1927 /* Haven't executed the original instruction yet.
1928 Set breakpoint there, and wait till it's hit,
1929 then single-step until exiting the jump pad. */
1930 lwp
->exit_jump_pad_bkpt
1931 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
1934 threads_debug_printf
1935 ("Checking whether LWP %ld needs to move out of the jump pad..."
1936 " it does", lwpid_of (current_thread
));
1943 /* If we get a synchronous signal while collecting, *and*
1944 while executing the (relocated) original instruction,
1945 reset the PC to point at the tpoint address, before
1946 reporting to GDB. Otherwise, it's an IPA lib bug: just
1947 report the signal to GDB, and pray for the best. */
1949 lwp
->collecting_fast_tracepoint
1950 = fast_tpoint_collect_result::not_collecting
;
1952 if (r
!= fast_tpoint_collect_result::not_collecting
1953 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
1954 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
1957 struct regcache
*regcache
;
1959 /* The si_addr on a few signals references the address
1960 of the faulting instruction. Adjust that as
1962 if ((WSTOPSIG (*wstat
) == SIGILL
1963 || WSTOPSIG (*wstat
) == SIGFPE
1964 || WSTOPSIG (*wstat
) == SIGBUS
1965 || WSTOPSIG (*wstat
) == SIGSEGV
)
1966 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
1967 (PTRACE_TYPE_ARG3
) 0, &info
) == 0
1968 /* Final check just to make sure we don't clobber
1969 the siginfo of non-kernel-sent signals. */
1970 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
1972 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
1973 ptrace (PTRACE_SETSIGINFO
, lwpid_of (current_thread
),
1974 (PTRACE_TYPE_ARG3
) 0, &info
);
1977 regcache
= get_thread_regcache (current_thread
, 1);
1978 low_set_pc (regcache
, status
.tpoint_addr
);
1979 lwp
->stop_pc
= status
.tpoint_addr
;
1981 /* Cancel any fast tracepoint lock this thread was
1983 force_unlock_trace_buffer ();
1986 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
1988 threads_debug_printf
1989 ("Cancelling fast exit-jump-pad: removing bkpt."
1990 "stopping all threads momentarily.");
1992 stop_all_lwps (1, lwp
);
1994 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
1995 lwp
->exit_jump_pad_bkpt
= NULL
;
1997 unstop_all_lwps (1, lwp
);
1999 gdb_assert (lwp
->suspended
>= 0);
2004 threads_debug_printf
2005 ("Checking whether LWP %ld needs to move out of the jump pad... no",
2006 lwpid_of (current_thread
));
2011 /* Enqueue one signal in the "signals to report later when out of the
2015 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2017 struct thread_info
*thread
= get_lwp_thread (lwp
);
2019 threads_debug_printf ("Deferring signal %d for LWP %ld.",
2020 WSTOPSIG (*wstat
), lwpid_of (thread
));
2024 for (const auto &sig
: lwp
->pending_signals_to_report
)
2025 threads_debug_printf (" Already queued %d", sig
.signal
);
2027 threads_debug_printf (" (no more currently queued signals)");
2030 /* Don't enqueue non-RT signals if they are already in the deferred
2031 queue. (SIGSTOP being the easiest signal to see ending up here
2033 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
2035 for (const auto &sig
: lwp
->pending_signals_to_report
)
2037 if (sig
.signal
== WSTOPSIG (*wstat
))
2039 threads_debug_printf
2040 ("Not requeuing already queued non-RT signal %d for LWP %ld",
2041 sig
.signal
, lwpid_of (thread
));
2047 lwp
->pending_signals_to_report
.emplace_back (WSTOPSIG (*wstat
));
2049 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2050 &lwp
->pending_signals_to_report
.back ().info
);
2053 /* Dequeue one signal from the "signals to report later when out of
2054 the jump pad" list. */
2057 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2059 struct thread_info
*thread
= get_lwp_thread (lwp
);
2061 if (!lwp
->pending_signals_to_report
.empty ())
2063 const pending_signal
&p_sig
= lwp
->pending_signals_to_report
.front ();
2065 *wstat
= W_STOPCODE (p_sig
.signal
);
2066 if (p_sig
.info
.si_signo
!= 0)
2067 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2070 lwp
->pending_signals_to_report
.pop_front ();
2072 threads_debug_printf ("Reporting deferred signal %d for LWP %ld.",
2073 WSTOPSIG (*wstat
), lwpid_of (thread
));
2077 for (const auto &sig
: lwp
->pending_signals_to_report
)
2078 threads_debug_printf (" Still queued %d", sig
.signal
);
2080 threads_debug_printf (" (no more queued signals)");
2090 linux_process_target::check_stopped_by_watchpoint (lwp_info
*child
)
2092 scoped_restore_current_thread restore_thread
;
2093 switch_to_thread (get_lwp_thread (child
));
2095 if (low_stopped_by_watchpoint ())
2097 child
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2098 child
->stopped_data_address
= low_stopped_data_address ();
2101 return child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2105 linux_process_target::low_stopped_by_watchpoint ()
2111 linux_process_target::low_stopped_data_address ()
2116 /* Return the ptrace options that we want to try to enable. */
2119 linux_low_ptrace_options (int attached
)
2121 client_state
&cs
= get_client_state ();
2125 options
|= PTRACE_O_EXITKILL
;
2127 if (cs
.report_fork_events
)
2128 options
|= PTRACE_O_TRACEFORK
;
2130 if (cs
.report_vfork_events
)
2131 options
|= (PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEVFORKDONE
);
2133 if (cs
.report_exec_events
)
2134 options
|= PTRACE_O_TRACEEXEC
;
2136 options
|= PTRACE_O_TRACESYSGOOD
;
2142 linux_process_target::filter_event (int lwpid
, int wstat
)
2144 client_state
&cs
= get_client_state ();
2145 struct lwp_info
*child
;
2146 struct thread_info
*thread
;
2147 int have_stop_pc
= 0;
2149 child
= find_lwp_pid (ptid_t (lwpid
));
2151 /* Check for stop events reported by a process we didn't already
2152 know about - anything not already in our LWP list.
2154 If we're expecting to receive stopped processes after
2155 fork, vfork, and clone events, then we'll just add the
2156 new one to our list and go back to waiting for the event
2157 to be reported - the stopped process might be returned
2158 from waitpid before or after the event is.
2160 But note the case of a non-leader thread exec'ing after the
2161 leader having exited, and gone from our lists (because
2162 check_zombie_leaders deleted it). The non-leader thread
2163 changes its tid to the tgid. */
2165 if (WIFSTOPPED (wstat
) && child
== NULL
&& WSTOPSIG (wstat
) == SIGTRAP
2166 && linux_ptrace_get_extended_event (wstat
) == PTRACE_EVENT_EXEC
)
2170 /* A multi-thread exec after we had seen the leader exiting. */
2171 threads_debug_printf ("Re-adding thread group leader LWP %d after exec.",
2174 child_ptid
= ptid_t (lwpid
, lwpid
);
2175 child
= add_lwp (child_ptid
);
2177 switch_to_thread (child
->thread
);
2180 /* If we didn't find a process, one of two things presumably happened:
2181 - A process we started and then detached from has exited. Ignore it.
2182 - A process we are controlling has forked and the new child's stop
2183 was reported to us by the kernel. Save its PID. */
2184 if (child
== NULL
&& WIFSTOPPED (wstat
))
2186 add_to_pid_list (&stopped_pids
, lwpid
, wstat
);
2189 else if (child
== NULL
)
2192 thread
= get_lwp_thread (child
);
2196 child
->last_status
= wstat
;
2198 /* Check if the thread has exited. */
2199 if ((WIFEXITED (wstat
) || WIFSIGNALED (wstat
)))
2201 threads_debug_printf ("%d exited", lwpid
);
2203 if (finish_step_over (child
))
2205 /* Unsuspend all other LWPs, and set them back running again. */
2206 unsuspend_all_lwps (child
);
2209 /* If there is at least one more LWP, then the exit signal was
2210 not the end of the debugged application and should be
2211 ignored, unless GDB wants to hear about thread exits. */
2212 if (cs
.report_thread_events
2213 || last_thread_of_process_p (pid_of (thread
)))
2215 /* Since events are serialized to GDB core, and we can't
2216 report this one right now. Leave the status pending for
2217 the next time we're able to report it. */
2218 mark_lwp_dead (child
, wstat
);
2228 gdb_assert (WIFSTOPPED (wstat
));
2230 if (WIFSTOPPED (wstat
))
2232 struct process_info
*proc
;
2234 /* Architecture-specific setup after inferior is running. */
2235 proc
= find_process_pid (pid_of (thread
));
2236 if (proc
->tdesc
== NULL
)
2240 /* This needs to happen after we have attached to the
2241 inferior and it is stopped for the first time, but
2242 before we access any inferior registers. */
2243 arch_setup_thread (thread
);
2247 /* The process is started, but GDBserver will do
2248 architecture-specific setup after the program stops at
2249 the first instruction. */
2250 child
->status_pending_p
= 1;
2251 child
->status_pending
= wstat
;
2257 if (WIFSTOPPED (wstat
) && child
->must_set_ptrace_flags
)
2259 struct process_info
*proc
= find_process_pid (pid_of (thread
));
2260 int options
= linux_low_ptrace_options (proc
->attached
);
2262 linux_enable_event_reporting (lwpid
, options
);
2263 child
->must_set_ptrace_flags
= 0;
2266 /* Always update syscall_state, even if it will be filtered later. */
2267 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SYSCALL_SIGTRAP
)
2269 child
->syscall_state
2270 = (child
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2271 ? TARGET_WAITKIND_SYSCALL_RETURN
2272 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2276 /* Almost all other ptrace-stops are known to be outside of system
2277 calls, with further exceptions in handle_extended_wait. */
2278 child
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2281 /* Be careful to not overwrite stop_pc until save_stop_reason is
2283 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
2284 && linux_is_extended_waitstatus (wstat
))
2286 child
->stop_pc
= get_pc (child
);
2287 if (handle_extended_wait (&child
, wstat
))
2289 /* The event has been handled, so just return without
2295 if (linux_wstatus_maybe_breakpoint (wstat
))
2297 if (save_stop_reason (child
))
2302 child
->stop_pc
= get_pc (child
);
2304 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGSTOP
2305 && child
->stop_expected
)
2307 threads_debug_printf ("Expected stop.");
2309 child
->stop_expected
= 0;
2311 if (thread
->last_resume_kind
== resume_stop
)
2313 /* We want to report the stop to the core. Treat the
2314 SIGSTOP as a normal event. */
2315 threads_debug_printf ("resume_stop SIGSTOP caught for %s.",
2316 target_pid_to_str (ptid_of (thread
)).c_str ());
2318 else if (stopping_threads
!= NOT_STOPPING_THREADS
)
2320 /* Stopping threads. We don't want this SIGSTOP to end up
2322 threads_debug_printf ("SIGSTOP caught for %s while stopping threads.",
2323 target_pid_to_str (ptid_of (thread
)).c_str ());
2328 /* This is a delayed SIGSTOP. Filter out the event. */
2329 threads_debug_printf ("%s %s, 0, 0 (discard delayed SIGSTOP)",
2330 child
->stepping
? "step" : "continue",
2331 target_pid_to_str (ptid_of (thread
)).c_str ());
2333 resume_one_lwp (child
, child
->stepping
, 0, NULL
);
2338 child
->status_pending_p
= 1;
2339 child
->status_pending
= wstat
;
2344 linux_process_target::maybe_hw_step (thread_info
*thread
)
2346 if (supports_hardware_single_step ())
2350 /* GDBserver must insert single-step breakpoint for software
2352 gdb_assert (has_single_step_breakpoints (thread
));
2358 linux_process_target::resume_stopped_resumed_lwps (thread_info
*thread
)
2360 struct lwp_info
*lp
= get_thread_lwp (thread
);
2364 && !lp
->status_pending_p
2365 && thread
->last_status
.kind () == TARGET_WAITKIND_IGNORE
)
2369 if (thread
->last_resume_kind
== resume_step
)
2370 step
= maybe_hw_step (thread
);
2372 threads_debug_printf ("resuming stopped-resumed LWP %s at %s: step=%d",
2373 target_pid_to_str (ptid_of (thread
)).c_str (),
2374 paddress (lp
->stop_pc
), step
);
2376 resume_one_lwp (lp
, step
, GDB_SIGNAL_0
, NULL
);
2381 linux_process_target::wait_for_event_filtered (ptid_t wait_ptid
,
2383 int *wstatp
, int options
)
2385 struct thread_info
*event_thread
;
2386 struct lwp_info
*event_child
, *requested_child
;
2387 sigset_t block_mask
, prev_mask
;
2390 /* N.B. event_thread points to the thread_info struct that contains
2391 event_child. Keep them in sync. */
2392 event_thread
= NULL
;
2394 requested_child
= NULL
;
2396 /* Check for a lwp with a pending status. */
2398 if (filter_ptid
== minus_one_ptid
|| filter_ptid
.is_pid ())
2400 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2402 return status_pending_p_callback (thread
, filter_ptid
);
2405 if (event_thread
!= NULL
)
2407 event_child
= get_thread_lwp (event_thread
);
2408 threads_debug_printf ("Got a pending child %ld", lwpid_of (event_thread
));
2411 else if (filter_ptid
!= null_ptid
)
2413 requested_child
= find_lwp_pid (filter_ptid
);
2415 if (stopping_threads
== NOT_STOPPING_THREADS
2416 && requested_child
->status_pending_p
2417 && (requested_child
->collecting_fast_tracepoint
2418 != fast_tpoint_collect_result::not_collecting
))
2420 enqueue_one_deferred_signal (requested_child
,
2421 &requested_child
->status_pending
);
2422 requested_child
->status_pending_p
= 0;
2423 requested_child
->status_pending
= 0;
2424 resume_one_lwp (requested_child
, 0, 0, NULL
);
2427 if (requested_child
->suspended
2428 && requested_child
->status_pending_p
)
2430 internal_error (__FILE__
, __LINE__
,
2431 "requesting an event out of a"
2432 " suspended child?");
2435 if (requested_child
->status_pending_p
)
2437 event_child
= requested_child
;
2438 event_thread
= get_lwp_thread (event_child
);
2442 if (event_child
!= NULL
)
2444 threads_debug_printf ("Got an event from pending child %ld (%04x)",
2445 lwpid_of (event_thread
),
2446 event_child
->status_pending
);
2448 *wstatp
= event_child
->status_pending
;
2449 event_child
->status_pending_p
= 0;
2450 event_child
->status_pending
= 0;
2451 switch_to_thread (event_thread
);
2452 return lwpid_of (event_thread
);
2455 /* But if we don't find a pending event, we'll have to wait.
2457 We only enter this loop if no process has a pending wait status.
2458 Thus any action taken in response to a wait status inside this
2459 loop is responding as soon as we detect the status, not after any
2462 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2463 all signals while here. */
2464 sigfillset (&block_mask
);
2465 gdb_sigmask (SIG_BLOCK
, &block_mask
, &prev_mask
);
2467 /* Always pull all events out of the kernel. We'll randomly select
2468 an event LWP out of all that have events, to prevent
2470 while (event_child
== NULL
)
2474 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2477 - If the thread group leader exits while other threads in the
2478 thread group still exist, waitpid(TGID, ...) hangs. That
2479 waitpid won't return an exit status until the other threads
2480 in the group are reaped.
2482 - When a non-leader thread execs, that thread just vanishes
2483 without reporting an exit (so we'd hang if we waited for it
2484 explicitly in that case). The exec event is reported to
2487 ret
= my_waitpid (-1, wstatp
, options
| WNOHANG
);
2489 threads_debug_printf ("waitpid(-1, ...) returned %d, %s",
2490 ret
, errno
? safe_strerror (errno
) : "ERRNO-OK");
2494 threads_debug_printf ("waitpid %ld received %s",
2495 (long) ret
, status_to_str (*wstatp
).c_str ());
2497 /* Filter all events. IOW, leave all events pending. We'll
2498 randomly select an event LWP out of all that have events
2500 filter_event (ret
, *wstatp
);
2501 /* Retry until nothing comes out of waitpid. A single
2502 SIGCHLD can indicate more than one child stopped. */
2506 /* Now that we've pulled all events out of the kernel, resume
2507 LWPs that don't have an interesting event to report. */
2508 if (stopping_threads
== NOT_STOPPING_THREADS
)
2509 for_each_thread ([this] (thread_info
*thread
)
2511 resume_stopped_resumed_lwps (thread
);
2514 /* ... and find an LWP with a status to report to the core, if
2516 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2518 return status_pending_p_callback (thread
, filter_ptid
);
2521 if (event_thread
!= NULL
)
2523 event_child
= get_thread_lwp (event_thread
);
2524 *wstatp
= event_child
->status_pending
;
2525 event_child
->status_pending_p
= 0;
2526 event_child
->status_pending
= 0;
2530 /* Check for zombie thread group leaders. Those can't be reaped
2531 until all other threads in the thread group are. */
2532 check_zombie_leaders ();
2534 auto not_stopped
= [&] (thread_info
*thread
)
2536 return not_stopped_callback (thread
, wait_ptid
);
2539 /* If there are no resumed children left in the set of LWPs we
2540 want to wait for, bail. We can't just block in
2541 waitpid/sigsuspend, because lwps might have been left stopped
2542 in trace-stop state, and we'd be stuck forever waiting for
2543 their status to change (which would only happen if we resumed
2544 them). Even if WNOHANG is set, this return code is preferred
2545 over 0 (below), as it is more detailed. */
2546 if (find_thread (not_stopped
) == NULL
)
2548 threads_debug_printf ("exit (no unwaited-for LWP)");
2550 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2554 /* No interesting event to report to the caller. */
2555 if ((options
& WNOHANG
))
2557 threads_debug_printf ("WNOHANG set, no event found");
2559 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2563 /* Block until we get an event reported with SIGCHLD. */
2564 threads_debug_printf ("sigsuspend'ing");
2566 sigsuspend (&prev_mask
);
2567 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2571 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2573 switch_to_thread (event_thread
);
2575 return lwpid_of (event_thread
);
2579 linux_process_target::wait_for_event (ptid_t ptid
, int *wstatp
, int options
)
2581 return wait_for_event_filtered (ptid
, ptid
, wstatp
, options
);
2584 /* Select one LWP out of those that have events pending. */
2587 select_event_lwp (struct lwp_info
**orig_lp
)
2589 struct thread_info
*event_thread
= NULL
;
2591 /* In all-stop, give preference to the LWP that is being
2592 single-stepped. There will be at most one, and it's the LWP that
2593 the core is most interested in. If we didn't do this, then we'd
2594 have to handle pending step SIGTRAPs somehow in case the core
2595 later continues the previously-stepped thread, otherwise we'd
2596 report the pending SIGTRAP, and the core, not having stepped the
2597 thread, wouldn't understand what the trap was for, and therefore
2598 would report it to the user as a random signal. */
2601 event_thread
= find_thread ([] (thread_info
*thread
)
2603 lwp_info
*lp
= get_thread_lwp (thread
);
2605 return (thread
->last_status
.kind () == TARGET_WAITKIND_IGNORE
2606 && thread
->last_resume_kind
== resume_step
2607 && lp
->status_pending_p
);
2610 if (event_thread
!= NULL
)
2611 threads_debug_printf
2612 ("Select single-step %s",
2613 target_pid_to_str (ptid_of (event_thread
)).c_str ());
2615 if (event_thread
== NULL
)
2617 /* No single-stepping LWP. Select one at random, out of those
2618 which have had events. */
2620 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2622 lwp_info
*lp
= get_thread_lwp (thread
);
2624 /* Only resumed LWPs that have an event pending. */
2625 return (thread
->last_status
.kind () == TARGET_WAITKIND_IGNORE
2626 && lp
->status_pending_p
);
2630 if (event_thread
!= NULL
)
2632 struct lwp_info
*event_lp
= get_thread_lwp (event_thread
);
2634 /* Switch the event LWP. */
2635 *orig_lp
= event_lp
;
2639 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2643 unsuspend_all_lwps (struct lwp_info
*except
)
2645 for_each_thread ([&] (thread_info
*thread
)
2647 lwp_info
*lwp
= get_thread_lwp (thread
);
2650 lwp_suspended_decr (lwp
);
2654 static bool lwp_running (thread_info
*thread
);
2656 /* Stabilize threads (move out of jump pads).
2658 If a thread is midway collecting a fast tracepoint, we need to
2659 finish the collection and move it out of the jump pad before
2660 reporting the signal.
2662 This avoids recursion while collecting (when a signal arrives
2663 midway, and the signal handler itself collects), which would trash
2664 the trace buffer. In case the user set a breakpoint in a signal
2665 handler, this avoids the backtrace showing the jump pad, etc..
2666 Most importantly, there are certain things we can't do safely if
2667 threads are stopped in a jump pad (or in its callee's). For
2670 - starting a new trace run. A thread still collecting the
2671 previous run, could trash the trace buffer when resumed. The trace
2672 buffer control structures would have been reset but the thread had
2673 no way to tell. The thread could even midway memcpy'ing to the
2674 buffer, which would mean that when resumed, it would clobber the
2675 trace buffer that had been set for a new run.
2677 - we can't rewrite/reuse the jump pads for new tracepoints
2678 safely. Say you do tstart while a thread is stopped midway while
2679 collecting. When the thread is later resumed, it finishes the
2680 collection, and returns to the jump pad, to execute the original
2681 instruction that was under the tracepoint jump at the time the
2682 older run had been started. If the jump pad had been rewritten
2683 since for something else in the new run, the thread would now
2684 execute the wrong / random instructions. */
2687 linux_process_target::stabilize_threads ()
2689 thread_info
*thread_stuck
= find_thread ([this] (thread_info
*thread
)
2691 return stuck_in_jump_pad (thread
);
2694 if (thread_stuck
!= NULL
)
2696 threads_debug_printf ("can't stabilize, LWP %ld is stuck in jump pad",
2697 lwpid_of (thread_stuck
));
2701 scoped_restore_current_thread restore_thread
;
2703 stabilizing_threads
= 1;
2706 for_each_thread ([this] (thread_info
*thread
)
2708 move_out_of_jump_pad (thread
);
2711 /* Loop until all are stopped out of the jump pads. */
2712 while (find_thread (lwp_running
) != NULL
)
2714 struct target_waitstatus ourstatus
;
2715 struct lwp_info
*lwp
;
2718 /* Note that we go through the full wait even loop. While
2719 moving threads out of jump pad, we need to be able to step
2720 over internal breakpoints and such. */
2721 wait_1 (minus_one_ptid
, &ourstatus
, 0);
2723 if (ourstatus
.kind () == TARGET_WAITKIND_STOPPED
)
2725 lwp
= get_thread_lwp (current_thread
);
2728 lwp_suspended_inc (lwp
);
2730 if (ourstatus
.sig () != GDB_SIGNAL_0
2731 || current_thread
->last_resume_kind
== resume_stop
)
2733 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.sig ()));
2734 enqueue_one_deferred_signal (lwp
, &wstat
);
2739 unsuspend_all_lwps (NULL
);
2741 stabilizing_threads
= 0;
2745 thread_stuck
= find_thread ([this] (thread_info
*thread
)
2747 return stuck_in_jump_pad (thread
);
2750 if (thread_stuck
!= NULL
)
2751 threads_debug_printf
2752 ("couldn't stabilize, LWP %ld got stuck in jump pad",
2753 lwpid_of (thread_stuck
));
2757 /* Convenience function that is called when the kernel reports an
2758 event that is not passed out to GDB. */
2761 ignore_event (struct target_waitstatus
*ourstatus
)
2763 /* If we got an event, there may still be others, as a single
2764 SIGCHLD can indicate more than one child stopped. This forces
2765 another target_wait call. */
2768 ourstatus
->set_ignore ();
2773 linux_process_target::filter_exit_event (lwp_info
*event_child
,
2774 target_waitstatus
*ourstatus
)
2776 client_state
&cs
= get_client_state ();
2777 struct thread_info
*thread
= get_lwp_thread (event_child
);
2778 ptid_t ptid
= ptid_of (thread
);
2780 if (!last_thread_of_process_p (pid_of (thread
)))
2782 if (cs
.report_thread_events
)
2783 ourstatus
->set_thread_exited (0);
2785 ourstatus
->set_ignore ();
2787 delete_lwp (event_child
);
2792 /* Returns 1 if GDB is interested in any event_child syscalls. */
2795 gdb_catching_syscalls_p (struct lwp_info
*event_child
)
2797 struct thread_info
*thread
= get_lwp_thread (event_child
);
2798 struct process_info
*proc
= get_thread_process (thread
);
2800 return !proc
->syscalls_to_catch
.empty ();
2804 linux_process_target::gdb_catch_this_syscall (lwp_info
*event_child
)
2807 struct thread_info
*thread
= get_lwp_thread (event_child
);
2808 struct process_info
*proc
= get_thread_process (thread
);
2810 if (proc
->syscalls_to_catch
.empty ())
2813 if (proc
->syscalls_to_catch
[0] == ANY_SYSCALL
)
2816 get_syscall_trapinfo (event_child
, &sysno
);
2818 for (int iter
: proc
->syscalls_to_catch
)
2826 linux_process_target::wait_1 (ptid_t ptid
, target_waitstatus
*ourstatus
,
2827 target_wait_flags target_options
)
2829 THREADS_SCOPED_DEBUG_ENTER_EXIT
;
2831 client_state
&cs
= get_client_state ();
2833 struct lwp_info
*event_child
;
2836 int step_over_finished
;
2837 int bp_explains_trap
;
2838 int maybe_internal_trap
;
2844 threads_debug_printf ("[%s]", target_pid_to_str (ptid
).c_str ());
2846 /* Translate generic target options into linux options. */
2848 if (target_options
& TARGET_WNOHANG
)
2851 bp_explains_trap
= 0;
2854 ourstatus
->set_ignore ();
2856 auto status_pending_p_any
= [&] (thread_info
*thread
)
2858 return status_pending_p_callback (thread
, minus_one_ptid
);
2861 auto not_stopped
= [&] (thread_info
*thread
)
2863 return not_stopped_callback (thread
, minus_one_ptid
);
2866 /* Find a resumed LWP, if any. */
2867 if (find_thread (status_pending_p_any
) != NULL
)
2869 else if (find_thread (not_stopped
) != NULL
)
2874 if (step_over_bkpt
== null_ptid
)
2875 pid
= wait_for_event (ptid
, &w
, options
);
2878 threads_debug_printf ("step_over_bkpt set [%s], doing a blocking wait",
2879 target_pid_to_str (step_over_bkpt
).c_str ());
2880 pid
= wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
2883 if (pid
== 0 || (pid
== -1 && !any_resumed
))
2885 gdb_assert (target_options
& TARGET_WNOHANG
);
2887 threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_IGNORE");
2889 ourstatus
->set_ignore ();
2894 threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_NO_RESUMED");
2896 ourstatus
->set_no_resumed ();
2900 event_child
= get_thread_lwp (current_thread
);
2902 /* wait_for_event only returns an exit status for the last
2903 child of a process. Report it. */
2904 if (WIFEXITED (w
) || WIFSIGNALED (w
))
2908 ourstatus
->set_exited (WEXITSTATUS (w
));
2910 threads_debug_printf
2911 ("ret = %s, exited with retcode %d",
2912 target_pid_to_str (ptid_of (current_thread
)).c_str (),
2917 ourstatus
->set_signalled (gdb_signal_from_host (WTERMSIG (w
)));
2919 threads_debug_printf
2920 ("ret = %s, terminated with signal %d",
2921 target_pid_to_str (ptid_of (current_thread
)).c_str (),
2925 if (ourstatus
->kind () == TARGET_WAITKIND_EXITED
)
2926 return filter_exit_event (event_child
, ourstatus
);
2928 return ptid_of (current_thread
);
2931 /* If step-over executes a breakpoint instruction, in the case of a
2932 hardware single step it means a gdb/gdbserver breakpoint had been
2933 planted on top of a permanent breakpoint, in the case of a software
2934 single step it may just mean that gdbserver hit the reinsert breakpoint.
2935 The PC has been adjusted by save_stop_reason to point at
2936 the breakpoint address.
2937 So in the case of the hardware single step advance the PC manually
2938 past the breakpoint and in the case of software single step advance only
2939 if it's not the single_step_breakpoint we are hitting.
2940 This avoids that a program would keep trapping a permanent breakpoint
2942 if (step_over_bkpt
!= null_ptid
2943 && event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
2944 && (event_child
->stepping
2945 || !single_step_breakpoint_inserted_here (event_child
->stop_pc
)))
2947 int increment_pc
= 0;
2948 int breakpoint_kind
= 0;
2949 CORE_ADDR stop_pc
= event_child
->stop_pc
;
2951 breakpoint_kind
= breakpoint_kind_from_current_state (&stop_pc
);
2952 sw_breakpoint_from_kind (breakpoint_kind
, &increment_pc
);
2954 threads_debug_printf
2955 ("step-over for %s executed software breakpoint",
2956 target_pid_to_str (ptid_of (current_thread
)).c_str ());
2958 if (increment_pc
!= 0)
2960 struct regcache
*regcache
2961 = get_thread_regcache (current_thread
, 1);
2963 event_child
->stop_pc
+= increment_pc
;
2964 low_set_pc (regcache
, event_child
->stop_pc
);
2966 if (!low_breakpoint_at (event_child
->stop_pc
))
2967 event_child
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
2971 /* If this event was not handled before, and is not a SIGTRAP, we
2972 report it. SIGILL and SIGSEGV are also treated as traps in case
2973 a breakpoint is inserted at the current PC. If this target does
2974 not support internal breakpoints at all, we also report the
2975 SIGTRAP without further processing; it's of no concern to us. */
2977 = (low_supports_breakpoints ()
2978 && (WSTOPSIG (w
) == SIGTRAP
2979 || ((WSTOPSIG (w
) == SIGILL
2980 || WSTOPSIG (w
) == SIGSEGV
)
2981 && low_breakpoint_at (event_child
->stop_pc
))));
2983 if (maybe_internal_trap
)
2985 /* Handle anything that requires bookkeeping before deciding to
2986 report the event or continue waiting. */
2988 /* First check if we can explain the SIGTRAP with an internal
2989 breakpoint, or if we should possibly report the event to GDB.
2990 Do this before anything that may remove or insert a
2992 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
2994 /* We have a SIGTRAP, possibly a step-over dance has just
2995 finished. If so, tweak the state machine accordingly,
2996 reinsert breakpoints and delete any single-step
2998 step_over_finished
= finish_step_over (event_child
);
3000 /* Now invoke the callbacks of any internal breakpoints there. */
3001 check_breakpoints (event_child
->stop_pc
);
3003 /* Handle tracepoint data collecting. This may overflow the
3004 trace buffer, and cause a tracing stop, removing
3006 trace_event
= handle_tracepoints (event_child
);
3008 if (bp_explains_trap
)
3009 threads_debug_printf ("Hit a gdbserver breakpoint.");
3013 /* We have some other signal, possibly a step-over dance was in
3014 progress, and it should be cancelled too. */
3015 step_over_finished
= finish_step_over (event_child
);
3018 /* We have all the data we need. Either report the event to GDB, or
3019 resume threads and keep waiting for more. */
3021 /* If we're collecting a fast tracepoint, finish the collection and
3022 move out of the jump pad before delivering a signal. See
3023 linux_stabilize_threads. */
3026 && WSTOPSIG (w
) != SIGTRAP
3027 && supports_fast_tracepoints ()
3028 && agent_loaded_p ())
3030 threads_debug_printf ("Got signal %d for LWP %ld. Check if we need "
3031 "to defer or adjust it.",
3032 WSTOPSIG (w
), lwpid_of (current_thread
));
3034 /* Allow debugging the jump pad itself. */
3035 if (current_thread
->last_resume_kind
!= resume_step
3036 && maybe_move_out_of_jump_pad (event_child
, &w
))
3038 enqueue_one_deferred_signal (event_child
, &w
);
3040 threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad)",
3041 WSTOPSIG (w
), lwpid_of (current_thread
));
3043 resume_one_lwp (event_child
, 0, 0, NULL
);
3045 return ignore_event (ourstatus
);
3049 if (event_child
->collecting_fast_tracepoint
3050 != fast_tpoint_collect_result::not_collecting
)
3052 threads_debug_printf
3053 ("LWP %ld was trying to move out of the jump pad (%d). "
3054 "Check if we're already there.",
3055 lwpid_of (current_thread
),
3056 (int) event_child
->collecting_fast_tracepoint
);
3060 event_child
->collecting_fast_tracepoint
3061 = linux_fast_tracepoint_collecting (event_child
, NULL
);
3063 if (event_child
->collecting_fast_tracepoint
3064 != fast_tpoint_collect_result::before_insn
)
3066 /* No longer need this breakpoint. */
3067 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
3069 threads_debug_printf
3070 ("No longer need exit-jump-pad bkpt; removing it."
3071 "stopping all threads momentarily.");
3073 /* Other running threads could hit this breakpoint.
3074 We don't handle moribund locations like GDB does,
3075 instead we always pause all threads when removing
3076 breakpoints, so that any step-over or
3077 decr_pc_after_break adjustment is always taken
3078 care of while the breakpoint is still
3080 stop_all_lwps (1, event_child
);
3082 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
3083 event_child
->exit_jump_pad_bkpt
= NULL
;
3085 unstop_all_lwps (1, event_child
);
3087 gdb_assert (event_child
->suspended
>= 0);
3091 if (event_child
->collecting_fast_tracepoint
3092 == fast_tpoint_collect_result::not_collecting
)
3094 threads_debug_printf
3095 ("fast tracepoint finished collecting successfully.");
3097 /* We may have a deferred signal to report. */
3098 if (dequeue_one_deferred_signal (event_child
, &w
))
3099 threads_debug_printf ("dequeued one signal.");
3102 threads_debug_printf ("no deferred signals.");
3104 if (stabilizing_threads
)
3106 ourstatus
->set_stopped (GDB_SIGNAL_0
);
3108 threads_debug_printf
3109 ("ret = %s, stopped while stabilizing threads",
3110 target_pid_to_str (ptid_of (current_thread
)).c_str ());
3112 return ptid_of (current_thread
);
3118 /* Check whether GDB would be interested in this event. */
3120 /* Check if GDB is interested in this syscall. */
3122 && WSTOPSIG (w
) == SYSCALL_SIGTRAP
3123 && !gdb_catch_this_syscall (event_child
))
3125 threads_debug_printf ("Ignored syscall for LWP %ld.",
3126 lwpid_of (current_thread
));
3128 resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
3130 return ignore_event (ourstatus
);
3133 /* If GDB is not interested in this signal, don't stop other
3134 threads, and don't report it to GDB. Just resume the inferior
3135 right away. We do this for threading-related signals as well as
3136 any that GDB specifically requested we ignore. But never ignore
3137 SIGSTOP if we sent it ourselves, and do not ignore signals when
3138 stepping - they may require special handling to skip the signal
3139 handler. Also never ignore signals that could be caused by a
3142 && current_thread
->last_resume_kind
!= resume_step
3144 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3145 (current_process ()->priv
->thread_db
!= NULL
3146 && (WSTOPSIG (w
) == __SIGRTMIN
3147 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
3150 (cs
.pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
3151 && !(WSTOPSIG (w
) == SIGSTOP
3152 && current_thread
->last_resume_kind
== resume_stop
)
3153 && !linux_wstatus_maybe_breakpoint (w
))))
3155 siginfo_t info
, *info_p
;
3157 threads_debug_printf ("Ignored signal %d for LWP %ld.",
3158 WSTOPSIG (w
), lwpid_of (current_thread
));
3160 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
3161 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
3166 if (step_over_finished
)
3168 /* We cancelled this thread's step-over above. We still
3169 need to unsuspend all other LWPs, and set them back
3170 running again while the signal handler runs. */
3171 unsuspend_all_lwps (event_child
);
3173 /* Enqueue the pending signal info so that proceed_all_lwps
3175 enqueue_pending_signal (event_child
, WSTOPSIG (w
), info_p
);
3177 proceed_all_lwps ();
3181 resume_one_lwp (event_child
, event_child
->stepping
,
3182 WSTOPSIG (w
), info_p
);
3185 return ignore_event (ourstatus
);
3188 /* Note that all addresses are always "out of the step range" when
3189 there's no range to begin with. */
3190 in_step_range
= lwp_in_step_range (event_child
);
3192 /* If GDB wanted this thread to single step, and the thread is out
3193 of the step range, we always want to report the SIGTRAP, and let
3194 GDB handle it. Watchpoints should always be reported. So should
3195 signals we can't explain. A SIGTRAP we can't explain could be a
3196 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3197 do, we're be able to handle GDB breakpoints on top of internal
3198 breakpoints, by handling the internal breakpoint and still
3199 reporting the event to GDB. If we don't, we're out of luck, GDB
3200 won't see the breakpoint hit. If we see a single-step event but
3201 the thread should be continuing, don't pass the trap to gdb.
3202 That indicates that we had previously finished a single-step but
3203 left the single-step pending -- see
3204 complete_ongoing_step_over. */
3205 report_to_gdb
= (!maybe_internal_trap
3206 || (current_thread
->last_resume_kind
== resume_step
3208 || event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3210 && !bp_explains_trap
3212 && !step_over_finished
3213 && !(current_thread
->last_resume_kind
== resume_continue
3214 && event_child
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
))
3215 || (gdb_breakpoint_here (event_child
->stop_pc
)
3216 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
3217 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
))
3218 || event_child
->waitstatus
.kind () != TARGET_WAITKIND_IGNORE
);
3220 run_breakpoint_commands (event_child
->stop_pc
);
3222 /* We found no reason GDB would want us to stop. We either hit one
3223 of our own breakpoints, or finished an internal step GDB
3224 shouldn't know about. */
3227 if (bp_explains_trap
)
3228 threads_debug_printf ("Hit a gdbserver breakpoint.");
3230 if (step_over_finished
)
3231 threads_debug_printf ("Step-over finished.");
3234 threads_debug_printf ("Tracepoint event.");
3236 if (lwp_in_step_range (event_child
))
3237 threads_debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).",
3238 paddress (event_child
->stop_pc
),
3239 paddress (event_child
->step_range_start
),
3240 paddress (event_child
->step_range_end
));
3242 /* We're not reporting this breakpoint to GDB, so apply the
3243 decr_pc_after_break adjustment to the inferior's regcache
3246 if (low_supports_breakpoints ())
3248 struct regcache
*regcache
3249 = get_thread_regcache (current_thread
, 1);
3250 low_set_pc (regcache
, event_child
->stop_pc
);
3253 if (step_over_finished
)
3255 /* If we have finished stepping over a breakpoint, we've
3256 stopped and suspended all LWPs momentarily except the
3257 stepping one. This is where we resume them all again.
3258 We're going to keep waiting, so use proceed, which
3259 handles stepping over the next breakpoint. */
3260 unsuspend_all_lwps (event_child
);
3264 /* Remove the single-step breakpoints if any. Note that
3265 there isn't single-step breakpoint if we finished stepping
3267 if (supports_software_single_step ()
3268 && has_single_step_breakpoints (current_thread
))
3270 stop_all_lwps (0, event_child
);
3271 delete_single_step_breakpoints (current_thread
);
3272 unstop_all_lwps (0, event_child
);
3276 threads_debug_printf ("proceeding all threads.");
3278 proceed_all_lwps ();
3280 return ignore_event (ourstatus
);
3285 if (event_child
->waitstatus
.kind () != TARGET_WAITKIND_IGNORE
)
3286 threads_debug_printf ("LWP %ld: extended event with waitstatus %s",
3287 lwpid_of (get_lwp_thread (event_child
)),
3288 event_child
->waitstatus
.to_string ().c_str ());
3290 if (current_thread
->last_resume_kind
== resume_step
)
3292 if (event_child
->step_range_start
== event_child
->step_range_end
)
3293 threads_debug_printf
3294 ("GDB wanted to single-step, reporting event.");
3295 else if (!lwp_in_step_range (event_child
))
3296 threads_debug_printf ("Out of step range, reporting event.");
3299 if (event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
3300 threads_debug_printf ("Stopped by watchpoint.");
3301 else if (gdb_breakpoint_here (event_child
->stop_pc
))
3302 threads_debug_printf ("Stopped by GDB breakpoint.");
3305 threads_debug_printf ("Hit a non-gdbserver trap event.");
3307 /* Alright, we're going to report a stop. */
3309 /* Remove single-step breakpoints. */
3310 if (supports_software_single_step ())
3312 /* Remove single-step breakpoints or not. It it is true, stop all
3313 lwps, so that other threads won't hit the breakpoint in the
3315 int remove_single_step_breakpoints_p
= 0;
3319 remove_single_step_breakpoints_p
3320 = has_single_step_breakpoints (current_thread
);
3324 /* In all-stop, a stop reply cancels all previous resume
3325 requests. Delete all single-step breakpoints. */
3327 find_thread ([&] (thread_info
*thread
) {
3328 if (has_single_step_breakpoints (thread
))
3330 remove_single_step_breakpoints_p
= 1;
3338 if (remove_single_step_breakpoints_p
)
3340 /* If we remove single-step breakpoints from memory, stop all lwps,
3341 so that other threads won't hit the breakpoint in the staled
3343 stop_all_lwps (0, event_child
);
3347 gdb_assert (has_single_step_breakpoints (current_thread
));
3348 delete_single_step_breakpoints (current_thread
);
3352 for_each_thread ([] (thread_info
*thread
){
3353 if (has_single_step_breakpoints (thread
))
3354 delete_single_step_breakpoints (thread
);
3358 unstop_all_lwps (0, event_child
);
3362 if (!stabilizing_threads
)
3364 /* In all-stop, stop all threads. */
3366 stop_all_lwps (0, NULL
);
3368 if (step_over_finished
)
3372 /* If we were doing a step-over, all other threads but
3373 the stepping one had been paused in start_step_over,
3374 with their suspend counts incremented. We don't want
3375 to do a full unstop/unpause, because we're in
3376 all-stop mode (so we want threads stopped), but we
3377 still need to unsuspend the other threads, to
3378 decrement their `suspended' count back. */
3379 unsuspend_all_lwps (event_child
);
3383 /* If we just finished a step-over, then all threads had
3384 been momentarily paused. In all-stop, that's fine,
3385 we want threads stopped by now anyway. In non-stop,
3386 we need to re-resume threads that GDB wanted to be
3388 unstop_all_lwps (1, event_child
);
3392 /* If we're not waiting for a specific LWP, choose an event LWP
3393 from among those that have had events. Giving equal priority
3394 to all LWPs that have had events helps prevent
3396 if (ptid
== minus_one_ptid
)
3398 event_child
->status_pending_p
= 1;
3399 event_child
->status_pending
= w
;
3401 select_event_lwp (&event_child
);
3403 /* current_thread and event_child must stay in sync. */
3404 switch_to_thread (get_lwp_thread (event_child
));
3406 event_child
->status_pending_p
= 0;
3407 w
= event_child
->status_pending
;
3411 /* Stabilize threads (move out of jump pads). */
3413 target_stabilize_threads ();
3417 /* If we just finished a step-over, then all threads had been
3418 momentarily paused. In all-stop, that's fine, we want
3419 threads stopped by now anyway. In non-stop, we need to
3420 re-resume threads that GDB wanted to be running. */
3421 if (step_over_finished
)
3422 unstop_all_lwps (1, event_child
);
3425 if (event_child
->waitstatus
.kind () != TARGET_WAITKIND_IGNORE
)
3427 /* If the reported event is an exit, fork, vfork or exec, let
3430 /* Break the unreported fork relationship chain. */
3431 if (event_child
->waitstatus
.kind () == TARGET_WAITKIND_FORKED
3432 || event_child
->waitstatus
.kind () == TARGET_WAITKIND_VFORKED
)
3434 event_child
->fork_relative
->fork_relative
= NULL
;
3435 event_child
->fork_relative
= NULL
;
3438 *ourstatus
= event_child
->waitstatus
;
3439 /* Clear the event lwp's waitstatus since we handled it already. */
3440 event_child
->waitstatus
.set_ignore ();
3444 /* The actual stop signal is overwritten below. */
3445 ourstatus
->set_stopped (GDB_SIGNAL_0
);
3448 /* Now that we've selected our final event LWP, un-adjust its PC if
3449 it was a software breakpoint, and the client doesn't know we can
3450 adjust the breakpoint ourselves. */
3451 if (event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3452 && !cs
.swbreak_feature
)
3454 int decr_pc
= low_decr_pc_after_break ();
3458 struct regcache
*regcache
3459 = get_thread_regcache (current_thread
, 1);
3460 low_set_pc (regcache
, event_child
->stop_pc
+ decr_pc
);
3464 if (WSTOPSIG (w
) == SYSCALL_SIGTRAP
)
3468 get_syscall_trapinfo (event_child
, &syscall_number
);
3469 if (event_child
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
)
3470 ourstatus
->set_syscall_entry (syscall_number
);
3471 else if (event_child
->syscall_state
== TARGET_WAITKIND_SYSCALL_RETURN
)
3472 ourstatus
->set_syscall_return (syscall_number
);
3474 gdb_assert_not_reached ("unexpected syscall state");
3476 else if (current_thread
->last_resume_kind
== resume_stop
3477 && WSTOPSIG (w
) == SIGSTOP
)
3479 /* A thread that has been requested to stop by GDB with vCont;t,
3480 and it stopped cleanly, so report as SIG0. The use of
3481 SIGSTOP is an implementation detail. */
3482 ourstatus
->set_stopped (GDB_SIGNAL_0
);
3484 else if (current_thread
->last_resume_kind
== resume_stop
3485 && WSTOPSIG (w
) != SIGSTOP
)
3487 /* A thread that has been requested to stop by GDB with vCont;t,
3488 but, it stopped for other reasons. */
3489 ourstatus
->set_stopped (gdb_signal_from_host (WSTOPSIG (w
)));
3491 else if (ourstatus
->kind () == TARGET_WAITKIND_STOPPED
)
3492 ourstatus
->set_stopped (gdb_signal_from_host (WSTOPSIG (w
)));
3494 gdb_assert (step_over_bkpt
== null_ptid
);
3496 threads_debug_printf ("ret = %s, %d, %d",
3497 target_pid_to_str (ptid_of (current_thread
)).c_str (),
3498 ourstatus
->kind (), ourstatus
->sig ());
3500 if (ourstatus
->kind () == TARGET_WAITKIND_EXITED
)
3501 return filter_exit_event (event_child
, ourstatus
);
3503 return ptid_of (current_thread
);
3506 /* Get rid of any pending event in the pipe. */
3508 async_file_flush (void)
3510 linux_event_pipe
.flush ();
3513 /* Put something in the pipe, so the event loop wakes up. */
3515 async_file_mark (void)
3517 linux_event_pipe
.mark ();
3521 linux_process_target::wait (ptid_t ptid
,
3522 target_waitstatus
*ourstatus
,
3523 target_wait_flags target_options
)
3527 /* Flush the async file first. */
3528 if (target_is_async_p ())
3529 async_file_flush ();
3533 event_ptid
= wait_1 (ptid
, ourstatus
, target_options
);
3535 while ((target_options
& TARGET_WNOHANG
) == 0
3536 && event_ptid
== null_ptid
3537 && ourstatus
->kind () == TARGET_WAITKIND_IGNORE
);
3539 /* If at least one stop was reported, there may be more. A single
3540 SIGCHLD can signal more than one child stop. */
3541 if (target_is_async_p ()
3542 && (target_options
& TARGET_WNOHANG
) != 0
3543 && event_ptid
!= null_ptid
)
3549 /* Send a signal to an LWP. */
3552 kill_lwp (unsigned long lwpid
, int signo
)
3557 ret
= syscall (__NR_tkill
, lwpid
, signo
);
3558 if (errno
== ENOSYS
)
3560 /* If tkill fails, then we are not using nptl threads, a
3561 configuration we no longer support. */
3562 perror_with_name (("tkill"));
3568 linux_stop_lwp (struct lwp_info
*lwp
)
3574 send_sigstop (struct lwp_info
*lwp
)
3578 pid
= lwpid_of (get_lwp_thread (lwp
));
3580 /* If we already have a pending stop signal for this process, don't
3582 if (lwp
->stop_expected
)
3584 threads_debug_printf ("Have pending sigstop for lwp %d", pid
);
3589 threads_debug_printf ("Sending sigstop to lwp %d", pid
);
3591 lwp
->stop_expected
= 1;
3592 kill_lwp (pid
, SIGSTOP
);
3596 send_sigstop (thread_info
*thread
, lwp_info
*except
)
3598 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3600 /* Ignore EXCEPT. */
3610 /* Increment the suspend count of an LWP, and stop it, if not stopped
3613 suspend_and_send_sigstop (thread_info
*thread
, lwp_info
*except
)
3615 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3617 /* Ignore EXCEPT. */
3621 lwp_suspended_inc (lwp
);
3623 send_sigstop (thread
, except
);
3627 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
3629 /* Store the exit status for later. */
3630 lwp
->status_pending_p
= 1;
3631 lwp
->status_pending
= wstat
;
3633 /* Store in waitstatus as well, as there's nothing else to process
3635 if (WIFEXITED (wstat
))
3636 lwp
->waitstatus
.set_exited (WEXITSTATUS (wstat
));
3637 else if (WIFSIGNALED (wstat
))
3638 lwp
->waitstatus
.set_signalled (gdb_signal_from_host (WTERMSIG (wstat
)));
3640 /* Prevent trying to stop it. */
3643 /* No further stops are expected from a dead lwp. */
3644 lwp
->stop_expected
= 0;
3647 /* Return true if LWP has exited already, and has a pending exit event
3648 to report to GDB. */
3651 lwp_is_marked_dead (struct lwp_info
*lwp
)
3653 return (lwp
->status_pending_p
3654 && (WIFEXITED (lwp
->status_pending
)
3655 || WIFSIGNALED (lwp
->status_pending
)));
3659 linux_process_target::wait_for_sigstop ()
3661 struct thread_info
*saved_thread
;
3666 saved_thread
= current_thread
;
3667 if (saved_thread
!= NULL
)
3668 saved_tid
= saved_thread
->id
;
3670 saved_tid
= null_ptid
; /* avoid bogus unused warning */
3672 scoped_restore_current_thread restore_thread
;
3674 threads_debug_printf ("pulling events");
3676 /* Passing NULL_PTID as filter indicates we want all events to be
3677 left pending. Eventually this returns when there are no
3678 unwaited-for children left. */
3679 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
, __WALL
);
3680 gdb_assert (ret
== -1);
3682 if (saved_thread
== NULL
|| mythread_alive (saved_tid
))
3686 threads_debug_printf ("Previously current thread died.");
3688 /* We can't change the current inferior behind GDB's back,
3689 otherwise, a subsequent command may apply to the wrong
3691 restore_thread
.dont_restore ();
3692 switch_to_thread (nullptr);
3697 linux_process_target::stuck_in_jump_pad (thread_info
*thread
)
3699 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3701 if (lwp
->suspended
!= 0)
3703 internal_error (__FILE__
, __LINE__
,
3704 "LWP %ld is suspended, suspended=%d\n",
3705 lwpid_of (thread
), lwp
->suspended
);
3707 gdb_assert (lwp
->stopped
);
3709 /* Allow debugging the jump pad, gdb_collect, etc.. */
3710 return (supports_fast_tracepoints ()
3711 && agent_loaded_p ()
3712 && (gdb_breakpoint_here (lwp
->stop_pc
)
3713 || lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3714 || thread
->last_resume_kind
== resume_step
)
3715 && (linux_fast_tracepoint_collecting (lwp
, NULL
)
3716 != fast_tpoint_collect_result::not_collecting
));
3720 linux_process_target::move_out_of_jump_pad (thread_info
*thread
)
3722 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3725 if (lwp
->suspended
!= 0)
3727 internal_error (__FILE__
, __LINE__
,
3728 "LWP %ld is suspended, suspended=%d\n",
3729 lwpid_of (thread
), lwp
->suspended
);
3731 gdb_assert (lwp
->stopped
);
3733 /* For gdb_breakpoint_here. */
3734 scoped_restore_current_thread restore_thread
;
3735 switch_to_thread (thread
);
3737 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
3739 /* Allow debugging the jump pad, gdb_collect, etc. */
3740 if (!gdb_breakpoint_here (lwp
->stop_pc
)
3741 && lwp
->stop_reason
!= TARGET_STOPPED_BY_WATCHPOINT
3742 && thread
->last_resume_kind
!= resume_step
3743 && maybe_move_out_of_jump_pad (lwp
, wstat
))
3745 threads_debug_printf ("LWP %ld needs stabilizing (in jump pad)",
3750 lwp
->status_pending_p
= 0;
3751 enqueue_one_deferred_signal (lwp
, wstat
);
3753 threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad",
3754 WSTOPSIG (*wstat
), lwpid_of (thread
));
3757 resume_one_lwp (lwp
, 0, 0, NULL
);
3760 lwp_suspended_inc (lwp
);
3764 lwp_running (thread_info
*thread
)
3766 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3768 if (lwp_is_marked_dead (lwp
))
3771 return !lwp
->stopped
;
3775 linux_process_target::stop_all_lwps (int suspend
, lwp_info
*except
)
3777 /* Should not be called recursively. */
3778 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
3780 THREADS_SCOPED_DEBUG_ENTER_EXIT
;
3782 threads_debug_printf
3783 ("%s, except=%s", suspend
? "stop-and-suspend" : "stop",
3785 ? target_pid_to_str (ptid_of (get_lwp_thread (except
))).c_str ()
3788 stopping_threads
= (suspend
3789 ? STOPPING_AND_SUSPENDING_THREADS
3790 : STOPPING_THREADS
);
3793 for_each_thread ([&] (thread_info
*thread
)
3795 suspend_and_send_sigstop (thread
, except
);
3798 for_each_thread ([&] (thread_info
*thread
)
3800 send_sigstop (thread
, except
);
3803 wait_for_sigstop ();
3804 stopping_threads
= NOT_STOPPING_THREADS
;
3806 threads_debug_printf ("setting stopping_threads back to !stopping");
3809 /* Enqueue one signal in the chain of signals which need to be
3810 delivered to this process on next resume. */
3813 enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
)
3815 lwp
->pending_signals
.emplace_back (signal
);
3816 if (info
== nullptr)
3817 memset (&lwp
->pending_signals
.back ().info
, 0, sizeof (siginfo_t
));
3819 lwp
->pending_signals
.back ().info
= *info
;
3823 linux_process_target::install_software_single_step_breakpoints (lwp_info
*lwp
)
3825 struct thread_info
*thread
= get_lwp_thread (lwp
);
3826 struct regcache
*regcache
= get_thread_regcache (thread
, 1);
3828 scoped_restore_current_thread restore_thread
;
3830 switch_to_thread (thread
);
3831 std::vector
<CORE_ADDR
> next_pcs
= low_get_next_pcs (regcache
);
3833 for (CORE_ADDR pc
: next_pcs
)
3834 set_single_step_breakpoint (pc
, current_ptid
);
3838 linux_process_target::single_step (lwp_info
* lwp
)
3842 if (supports_hardware_single_step ())
3846 else if (supports_software_single_step ())
3848 install_software_single_step_breakpoints (lwp
);
3852 threads_debug_printf ("stepping is not implemented on this target");
3857 /* The signal can be delivered to the inferior if we are not trying to
3858 finish a fast tracepoint collect. Since signal can be delivered in
3859 the step-over, the program may go to signal handler and trap again
3860 after return from the signal handler. We can live with the spurious
3864 lwp_signal_can_be_delivered (struct lwp_info
*lwp
)
3866 return (lwp
->collecting_fast_tracepoint
3867 == fast_tpoint_collect_result::not_collecting
);
3871 linux_process_target::resume_one_lwp_throw (lwp_info
*lwp
, int step
,
3872 int signal
, siginfo_t
*info
)
3874 struct thread_info
*thread
= get_lwp_thread (lwp
);
3876 struct process_info
*proc
= get_thread_process (thread
);
3878 /* Note that target description may not be initialised
3879 (proc->tdesc == NULL) at this point because the program hasn't
3880 stopped at the first instruction yet. It means GDBserver skips
3881 the extra traps from the wrapper program (see option --wrapper).
3882 Code in this function that requires register access should be
3883 guarded by proc->tdesc == NULL or something else. */
3885 if (lwp
->stopped
== 0)
3888 gdb_assert (lwp
->waitstatus
.kind () == TARGET_WAITKIND_IGNORE
);
3890 fast_tpoint_collect_result fast_tp_collecting
3891 = lwp
->collecting_fast_tracepoint
;
3893 gdb_assert (!stabilizing_threads
3894 || (fast_tp_collecting
3895 != fast_tpoint_collect_result::not_collecting
));
3897 /* Cancel actions that rely on GDB not changing the PC (e.g., the
3898 user used the "jump" command, or "set $pc = foo"). */
3899 if (thread
->while_stepping
!= NULL
&& lwp
->stop_pc
!= get_pc (lwp
))
3901 /* Collecting 'while-stepping' actions doesn't make sense
3903 release_while_stepping_state_list (thread
);
3906 /* If we have pending signals or status, and a new signal, enqueue the
3907 signal. Also enqueue the signal if it can't be delivered to the
3908 inferior right now. */
3910 && (lwp
->status_pending_p
3911 || !lwp
->pending_signals
.empty ()
3912 || !lwp_signal_can_be_delivered (lwp
)))
3914 enqueue_pending_signal (lwp
, signal
, info
);
3916 /* Postpone any pending signal. It was enqueued above. */
3920 if (lwp
->status_pending_p
)
3922 threads_debug_printf
3923 ("Not resuming lwp %ld (%s, stop %s); has pending status",
3924 lwpid_of (thread
), step
? "step" : "continue",
3925 lwp
->stop_expected
? "expected" : "not expected");
3929 scoped_restore_current_thread restore_thread
;
3930 switch_to_thread (thread
);
3932 /* This bit needs some thinking about. If we get a signal that
3933 we must report while a single-step reinsert is still pending,
3934 we often end up resuming the thread. It might be better to
3935 (ew) allow a stack of pending events; then we could be sure that
3936 the reinsert happened right away and not lose any signals.
3938 Making this stack would also shrink the window in which breakpoints are
3939 uninserted (see comment in linux_wait_for_lwp) but not enough for
3940 complete correctness, so it won't solve that problem. It may be
3941 worthwhile just to solve this one, however. */
3942 if (lwp
->bp_reinsert
!= 0)
3944 threads_debug_printf (" pending reinsert at 0x%s",
3945 paddress (lwp
->bp_reinsert
));
3947 if (supports_hardware_single_step ())
3949 if (fast_tp_collecting
== fast_tpoint_collect_result::not_collecting
)
3952 warning ("BAD - reinserting but not stepping.");
3954 warning ("BAD - reinserting and suspended(%d).",
3959 step
= maybe_hw_step (thread
);
3962 if (fast_tp_collecting
== fast_tpoint_collect_result::before_insn
)
3963 threads_debug_printf
3964 ("lwp %ld wants to get out of fast tracepoint jump pad "
3965 "(exit-jump-pad-bkpt)", lwpid_of (thread
));
3967 else if (fast_tp_collecting
== fast_tpoint_collect_result::at_insn
)
3969 threads_debug_printf
3970 ("lwp %ld wants to get out of fast tracepoint jump pad single-stepping",
3973 if (supports_hardware_single_step ())
3977 internal_error (__FILE__
, __LINE__
,
3978 "moving out of jump pad single-stepping"
3979 " not implemented on this target");
3983 /* If we have while-stepping actions in this thread set it stepping.
3984 If we have a signal to deliver, it may or may not be set to
3985 SIG_IGN, we don't know. Assume so, and allow collecting
3986 while-stepping into a signal handler. A possible smart thing to
3987 do would be to set an internal breakpoint at the signal return
3988 address, continue, and carry on catching this while-stepping
3989 action only when that breakpoint is hit. A future
3991 if (thread
->while_stepping
!= NULL
)
3993 threads_debug_printf
3994 ("lwp %ld has a while-stepping action -> forcing step.",
3997 step
= single_step (lwp
);
4000 if (proc
->tdesc
!= NULL
&& low_supports_breakpoints ())
4002 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
4004 lwp
->stop_pc
= low_get_pc (regcache
);
4006 threads_debug_printf (" %s from pc 0x%lx", step
? "step" : "continue",
4007 (long) lwp
->stop_pc
);
4010 /* If we have pending signals, consume one if it can be delivered to
4012 if (!lwp
->pending_signals
.empty () && lwp_signal_can_be_delivered (lwp
))
4014 const pending_signal
&p_sig
= lwp
->pending_signals
.front ();
4016 signal
= p_sig
.signal
;
4017 if (p_sig
.info
.si_signo
!= 0)
4018 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
4021 lwp
->pending_signals
.pop_front ();
4024 threads_debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)",
4025 lwpid_of (thread
), step
? "step" : "continue", signal
,
4026 lwp
->stop_expected
? "expected" : "not expected");
4028 low_prepare_to_resume (lwp
);
4030 regcache_invalidate_thread (thread
);
4032 lwp
->stepping
= step
;
4034 ptrace_request
= PTRACE_SINGLESTEP
;
4035 else if (gdb_catching_syscalls_p (lwp
))
4036 ptrace_request
= PTRACE_SYSCALL
;
4038 ptrace_request
= PTRACE_CONT
;
4039 ptrace (ptrace_request
,
4041 (PTRACE_TYPE_ARG3
) 0,
4042 /* Coerce to a uintptr_t first to avoid potential gcc warning
4043 of coercing an 8 byte integer to a 4 byte pointer. */
4044 (PTRACE_TYPE_ARG4
) (uintptr_t) signal
);
4047 perror_with_name ("resuming thread");
4049 /* Successfully resumed. Clear state that no longer makes sense,
4050 and mark the LWP as running. Must not do this before resuming
4051 otherwise if that fails other code will be confused. E.g., we'd
4052 later try to stop the LWP and hang forever waiting for a stop
4053 status. Note that we must not throw after this is cleared,
4054 otherwise handle_zombie_lwp_error would get confused. */
4056 lwp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4060 linux_process_target::low_prepare_to_resume (lwp_info
*lwp
)
4065 /* Called when we try to resume a stopped LWP and that errors out. If
4066 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4067 or about to become), discard the error, clear any pending status
4068 the LWP may have, and return true (we'll collect the exit status
4069 soon enough). Otherwise, return false. */
4072 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
4074 struct thread_info
*thread
= get_lwp_thread (lp
);
4076 /* If we get an error after resuming the LWP successfully, we'd
4077 confuse !T state for the LWP being gone. */
4078 gdb_assert (lp
->stopped
);
4080 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4081 because even if ptrace failed with ESRCH, the tracee may be "not
4082 yet fully dead", but already refusing ptrace requests. In that
4083 case the tracee has 'R (Running)' state for a little bit
4084 (observed in Linux 3.18). See also the note on ESRCH in the
4085 ptrace(2) man page. Instead, check whether the LWP has any state
4086 other than ptrace-stopped. */
4088 /* Don't assume anything if /proc/PID/status can't be read. */
4089 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread
)) == 0)
4091 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4092 lp
->status_pending_p
= 0;
4099 linux_process_target::resume_one_lwp (lwp_info
*lwp
, int step
, int signal
,
4104 resume_one_lwp_throw (lwp
, step
, signal
, info
);
4106 catch (const gdb_exception_error
&ex
)
4108 if (!check_ptrace_stopped_lwp_gone (lwp
))
4113 /* This function is called once per thread via for_each_thread.
4114 We look up which resume request applies to THREAD and mark it with a
4115 pointer to the appropriate resume request.
4117 This algorithm is O(threads * resume elements), but resume elements
4118 is small (and will remain small at least until GDB supports thread
4122 linux_set_resume_request (thread_info
*thread
, thread_resume
*resume
, size_t n
)
4124 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4126 for (int ndx
= 0; ndx
< n
; ndx
++)
4128 ptid_t ptid
= resume
[ndx
].thread
;
4129 if (ptid
== minus_one_ptid
4130 || ptid
== thread
->id
4131 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4133 || (ptid
.pid () == pid_of (thread
)
4135 || ptid
.lwp () == -1)))
4137 if (resume
[ndx
].kind
== resume_stop
4138 && thread
->last_resume_kind
== resume_stop
)
4140 threads_debug_printf
4141 ("already %s LWP %ld at GDB's request",
4142 (thread
->last_status
.kind () == TARGET_WAITKIND_STOPPED
4143 ? "stopped" : "stopping"),
4149 /* Ignore (wildcard) resume requests for already-resumed
4151 if (resume
[ndx
].kind
!= resume_stop
4152 && thread
->last_resume_kind
!= resume_stop
)
4154 threads_debug_printf
4155 ("already %s LWP %ld at GDB's request",
4156 (thread
->last_resume_kind
== resume_step
4157 ? "stepping" : "continuing"),
4162 /* Don't let wildcard resumes resume fork children that GDB
4163 does not yet know are new fork children. */
4164 if (lwp
->fork_relative
!= NULL
)
4166 struct lwp_info
*rel
= lwp
->fork_relative
;
4168 if (rel
->status_pending_p
4169 && (rel
->waitstatus
.kind () == TARGET_WAITKIND_FORKED
4170 || rel
->waitstatus
.kind () == TARGET_WAITKIND_VFORKED
))
4172 threads_debug_printf
4173 ("not resuming LWP %ld: has queued stop reply",
4179 /* If the thread has a pending event that has already been
4180 reported to GDBserver core, but GDB has not pulled the
4181 event out of the vStopped queue yet, likewise, ignore the
4182 (wildcard) resume request. */
4183 if (in_queued_stop_replies (thread
->id
))
4185 threads_debug_printf
4186 ("not resuming LWP %ld: has queued stop reply",
4191 lwp
->resume
= &resume
[ndx
];
4192 thread
->last_resume_kind
= lwp
->resume
->kind
;
4194 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
4195 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
4197 /* If we had a deferred signal to report, dequeue one now.
4198 This can happen if LWP gets more than one signal while
4199 trying to get out of a jump pad. */
4201 && !lwp
->status_pending_p
4202 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
4204 lwp
->status_pending_p
= 1;
4206 threads_debug_printf
4207 ("Dequeueing deferred signal %d for LWP %ld, "
4208 "leaving status pending.",
4209 WSTOPSIG (lwp
->status_pending
),
4217 /* No resume action for this thread. */
4222 linux_process_target::resume_status_pending (thread_info
*thread
)
4224 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4226 /* LWPs which will not be resumed are not interesting, because
4227 we might not wait for them next time through linux_wait. */
4228 if (lwp
->resume
== NULL
)
4231 return thread_still_has_status_pending (thread
);
4235 linux_process_target::thread_needs_step_over (thread_info
*thread
)
4237 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4239 struct process_info
*proc
= get_thread_process (thread
);
4241 /* GDBserver is skipping the extra traps from the wrapper program,
4242 don't have to do step over. */
4243 if (proc
->tdesc
== NULL
)
4246 /* LWPs which will not be resumed are not interesting, because we
4247 might not wait for them next time through linux_wait. */
4251 threads_debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped",
4256 if (thread
->last_resume_kind
== resume_stop
)
4258 threads_debug_printf
4259 ("Need step over [LWP %ld]? Ignoring, should remain stopped",
4264 gdb_assert (lwp
->suspended
>= 0);
4268 threads_debug_printf ("Need step over [LWP %ld]? Ignoring, suspended",
4273 if (lwp
->status_pending_p
)
4275 threads_debug_printf
4276 ("Need step over [LWP %ld]? Ignoring, has pending status.",
4281 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4285 /* If the PC has changed since we stopped, then don't do anything,
4286 and let the breakpoint/tracepoint be hit. This happens if, for
4287 instance, GDB handled the decr_pc_after_break subtraction itself,
4288 GDB is OOL stepping this thread, or the user has issued a "jump"
4289 command, or poked thread's registers herself. */
4290 if (pc
!= lwp
->stop_pc
)
4292 threads_debug_printf
4293 ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4294 "Old stop_pc was 0x%s, PC is now 0x%s", lwpid_of (thread
),
4295 paddress (lwp
->stop_pc
), paddress (pc
));
4299 /* On software single step target, resume the inferior with signal
4300 rather than stepping over. */
4301 if (supports_software_single_step ()
4302 && !lwp
->pending_signals
.empty ()
4303 && lwp_signal_can_be_delivered (lwp
))
4305 threads_debug_printf
4306 ("Need step over [LWP %ld]? Ignoring, has pending signals.",
4312 scoped_restore_current_thread restore_thread
;
4313 switch_to_thread (thread
);
4315 /* We can only step over breakpoints we know about. */
4316 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
4318 /* Don't step over a breakpoint that GDB expects to hit
4319 though. If the condition is being evaluated on the target's side
4320 and it evaluate to false, step over this breakpoint as well. */
4321 if (gdb_breakpoint_here (pc
)
4322 && gdb_condition_true_at_breakpoint (pc
)
4323 && gdb_no_commands_at_breakpoint (pc
))
4325 threads_debug_printf ("Need step over [LWP %ld]? yes, but found"
4326 " GDB breakpoint at 0x%s; skipping step over",
4327 lwpid_of (thread
), paddress (pc
));
4333 threads_debug_printf ("Need step over [LWP %ld]? yes, "
4334 "found breakpoint at 0x%s",
4335 lwpid_of (thread
), paddress (pc
));
4337 /* We've found an lwp that needs stepping over --- return 1 so
4338 that find_thread stops looking. */
4343 threads_debug_printf
4344 ("Need step over [LWP %ld]? No, no breakpoint found at 0x%s",
4345 lwpid_of (thread
), paddress (pc
));
4351 linux_process_target::start_step_over (lwp_info
*lwp
)
4353 struct thread_info
*thread
= get_lwp_thread (lwp
);
4356 threads_debug_printf ("Starting step-over on LWP %ld. Stopping all threads",
4359 stop_all_lwps (1, lwp
);
4361 if (lwp
->suspended
!= 0)
4363 internal_error (__FILE__
, __LINE__
,
4364 "LWP %ld suspended=%d\n", lwpid_of (thread
),
4368 threads_debug_printf ("Done stopping all threads for step-over.");
4370 /* Note, we should always reach here with an already adjusted PC,
4371 either by GDB (if we're resuming due to GDB's request), or by our
4372 caller, if we just finished handling an internal breakpoint GDB
4373 shouldn't care about. */
4378 scoped_restore_current_thread restore_thread
;
4379 switch_to_thread (thread
);
4381 lwp
->bp_reinsert
= pc
;
4382 uninsert_breakpoints_at (pc
);
4383 uninsert_fast_tracepoint_jumps_at (pc
);
4385 step
= single_step (lwp
);
4388 resume_one_lwp (lwp
, step
, 0, NULL
);
4390 /* Require next event from this LWP. */
4391 step_over_bkpt
= thread
->id
;
4395 linux_process_target::finish_step_over (lwp_info
*lwp
)
4397 if (lwp
->bp_reinsert
!= 0)
4399 scoped_restore_current_thread restore_thread
;
4401 threads_debug_printf ("Finished step over.");
4403 switch_to_thread (get_lwp_thread (lwp
));
4405 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4406 may be no breakpoint to reinsert there by now. */
4407 reinsert_breakpoints_at (lwp
->bp_reinsert
);
4408 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
4410 lwp
->bp_reinsert
= 0;
4412 /* Delete any single-step breakpoints. No longer needed. We
4413 don't have to worry about other threads hitting this trap,
4414 and later not being able to explain it, because we were
4415 stepping over a breakpoint, and we hold all threads but
4416 LWP stopped while doing that. */
4417 if (!supports_hardware_single_step ())
4419 gdb_assert (has_single_step_breakpoints (current_thread
));
4420 delete_single_step_breakpoints (current_thread
);
4423 step_over_bkpt
= null_ptid
;
4431 linux_process_target::complete_ongoing_step_over ()
4433 if (step_over_bkpt
!= null_ptid
)
4435 struct lwp_info
*lwp
;
4439 threads_debug_printf ("detach: step over in progress, finish it first");
4441 /* Passing NULL_PTID as filter indicates we want all events to
4442 be left pending. Eventually this returns when there are no
4443 unwaited-for children left. */
4444 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
,
4446 gdb_assert (ret
== -1);
4448 lwp
= find_lwp_pid (step_over_bkpt
);
4451 finish_step_over (lwp
);
4453 /* If we got our step SIGTRAP, don't leave it pending,
4454 otherwise we would report it to GDB as a spurious
4456 gdb_assert (lwp
->status_pending_p
);
4457 if (WIFSTOPPED (lwp
->status_pending
)
4458 && WSTOPSIG (lwp
->status_pending
) == SIGTRAP
)
4460 thread_info
*thread
= get_lwp_thread (lwp
);
4461 if (thread
->last_resume_kind
!= resume_step
)
4463 threads_debug_printf ("detach: discard step-over SIGTRAP");
4465 lwp
->status_pending_p
= 0;
4466 lwp
->status_pending
= 0;
4467 resume_one_lwp (lwp
, lwp
->stepping
, 0, NULL
);
4470 threads_debug_printf
4471 ("detach: resume_step, not discarding step-over SIGTRAP");
4474 step_over_bkpt
= null_ptid
;
4475 unsuspend_all_lwps (lwp
);
4480 linux_process_target::resume_one_thread (thread_info
*thread
,
4481 bool leave_all_stopped
)
4483 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4486 if (lwp
->resume
== NULL
)
4489 if (lwp
->resume
->kind
== resume_stop
)
4491 threads_debug_printf ("resume_stop request for LWP %ld",
4496 threads_debug_printf ("stopping LWP %ld", lwpid_of (thread
));
4498 /* Stop the thread, and wait for the event asynchronously,
4499 through the event loop. */
4504 threads_debug_printf ("already stopped LWP %ld", lwpid_of (thread
));
4506 /* The LWP may have been stopped in an internal event that
4507 was not meant to be notified back to GDB (e.g., gdbserver
4508 breakpoint), so we should be reporting a stop event in
4511 /* If the thread already has a pending SIGSTOP, this is a
4512 no-op. Otherwise, something later will presumably resume
4513 the thread and this will cause it to cancel any pending
4514 operation, due to last_resume_kind == resume_stop. If
4515 the thread already has a pending status to report, we
4516 will still report it the next time we wait - see
4517 status_pending_p_callback. */
4519 /* If we already have a pending signal to report, then
4520 there's no need to queue a SIGSTOP, as this means we're
4521 midway through moving the LWP out of the jumppad, and we
4522 will report the pending signal as soon as that is
4524 if (lwp
->pending_signals_to_report
.empty ())
4528 /* For stop requests, we're done. */
4530 thread
->last_status
.set_ignore ();
4534 /* If this thread which is about to be resumed has a pending status,
4535 then don't resume it - we can just report the pending status.
4536 Likewise if it is suspended, because e.g., another thread is
4537 stepping past a breakpoint. Make sure to queue any signals that
4538 would otherwise be sent. In all-stop mode, we do this decision
4539 based on if *any* thread has a pending status. If there's a
4540 thread that needs the step-over-breakpoint dance, then don't
4541 resume any other thread but that particular one. */
4542 leave_pending
= (lwp
->suspended
4543 || lwp
->status_pending_p
4544 || leave_all_stopped
);
4546 /* If we have a new signal, enqueue the signal. */
4547 if (lwp
->resume
->sig
!= 0)
4549 siginfo_t info
, *info_p
;
4551 /* If this is the same signal we were previously stopped by,
4552 make sure to queue its siginfo. */
4553 if (WIFSTOPPED (lwp
->last_status
)
4554 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
4555 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
),
4556 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
4561 enqueue_pending_signal (lwp
, lwp
->resume
->sig
, info_p
);
4566 threads_debug_printf ("resuming LWP %ld", lwpid_of (thread
));
4568 proceed_one_lwp (thread
, NULL
);
4571 threads_debug_printf ("leaving LWP %ld stopped", lwpid_of (thread
));
4573 thread
->last_status
.set_ignore ();
4578 linux_process_target::resume (thread_resume
*resume_info
, size_t n
)
4580 struct thread_info
*need_step_over
= NULL
;
4582 THREADS_SCOPED_DEBUG_ENTER_EXIT
;
4584 for_each_thread ([&] (thread_info
*thread
)
4586 linux_set_resume_request (thread
, resume_info
, n
);
4589 /* If there is a thread which would otherwise be resumed, which has
4590 a pending status, then don't resume any threads - we can just
4591 report the pending status. Make sure to queue any signals that
4592 would otherwise be sent. In non-stop mode, we'll apply this
4593 logic to each thread individually. We consume all pending events
4594 before considering to start a step-over (in all-stop). */
4595 bool any_pending
= false;
4597 any_pending
= find_thread ([this] (thread_info
*thread
)
4599 return resume_status_pending (thread
);
4602 /* If there is a thread which would otherwise be resumed, which is
4603 stopped at a breakpoint that needs stepping over, then don't
4604 resume any threads - have it step over the breakpoint with all
4605 other threads stopped, then resume all threads again. Make sure
4606 to queue any signals that would otherwise be delivered or
4608 if (!any_pending
&& low_supports_breakpoints ())
4609 need_step_over
= find_thread ([this] (thread_info
*thread
)
4611 return thread_needs_step_over (thread
);
4614 bool leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
4616 if (need_step_over
!= NULL
)
4617 threads_debug_printf ("Not resuming all, need step over");
4618 else if (any_pending
)
4619 threads_debug_printf ("Not resuming, all-stop and found "
4620 "an LWP with pending status");
4622 threads_debug_printf ("Resuming, no pending status or step over needed");
4624 /* Even if we're leaving threads stopped, queue all signals we'd
4625 otherwise deliver. */
4626 for_each_thread ([&] (thread_info
*thread
)
4628 resume_one_thread (thread
, leave_all_stopped
);
4632 start_step_over (get_thread_lwp (need_step_over
));
4634 /* We may have events that were pending that can/should be sent to
4635 the client now. Trigger a linux_wait call. */
4636 if (target_is_async_p ())
4641 linux_process_target::proceed_one_lwp (thread_info
*thread
, lwp_info
*except
)
4643 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4649 threads_debug_printf ("lwp %ld", lwpid_of (thread
));
4653 threads_debug_printf (" LWP %ld already running", lwpid_of (thread
));
4657 if (thread
->last_resume_kind
== resume_stop
4658 && thread
->last_status
.kind () != TARGET_WAITKIND_IGNORE
)
4660 threads_debug_printf (" client wants LWP to remain %ld stopped",
4665 if (lwp
->status_pending_p
)
4667 threads_debug_printf (" LWP %ld has pending status, leaving stopped",
4672 gdb_assert (lwp
->suspended
>= 0);
4676 threads_debug_printf (" LWP %ld is suspended", lwpid_of (thread
));
4680 if (thread
->last_resume_kind
== resume_stop
4681 && lwp
->pending_signals_to_report
.empty ()
4682 && (lwp
->collecting_fast_tracepoint
4683 == fast_tpoint_collect_result::not_collecting
))
4685 /* We haven't reported this LWP as stopped yet (otherwise, the
4686 last_status.kind check above would catch it, and we wouldn't
4687 reach here. This LWP may have been momentarily paused by a
4688 stop_all_lwps call while handling for example, another LWP's
4689 step-over. In that case, the pending expected SIGSTOP signal
4690 that was queued at vCont;t handling time will have already
4691 been consumed by wait_for_sigstop, and so we need to requeue
4692 another one here. Note that if the LWP already has a SIGSTOP
4693 pending, this is a no-op. */
4695 threads_debug_printf
4696 ("Client wants LWP %ld to stop. Making sure it has a SIGSTOP pending",
4702 if (thread
->last_resume_kind
== resume_step
)
4704 threads_debug_printf (" stepping LWP %ld, client wants it stepping",
4707 /* If resume_step is requested by GDB, install single-step
4708 breakpoints when the thread is about to be actually resumed if
4709 the single-step breakpoints weren't removed. */
4710 if (supports_software_single_step ()
4711 && !has_single_step_breakpoints (thread
))
4712 install_software_single_step_breakpoints (lwp
);
4714 step
= maybe_hw_step (thread
);
4716 else if (lwp
->bp_reinsert
!= 0)
4718 threads_debug_printf (" stepping LWP %ld, reinsert set",
4721 step
= maybe_hw_step (thread
);
4726 resume_one_lwp (lwp
, step
, 0, NULL
);
4730 linux_process_target::unsuspend_and_proceed_one_lwp (thread_info
*thread
,
4733 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4738 lwp_suspended_decr (lwp
);
4740 proceed_one_lwp (thread
, except
);
4744 linux_process_target::proceed_all_lwps ()
4746 struct thread_info
*need_step_over
;
4748 /* If there is a thread which would otherwise be resumed, which is
4749 stopped at a breakpoint that needs stepping over, then don't
4750 resume any threads - have it step over the breakpoint with all
4751 other threads stopped, then resume all threads again. */
4753 if (low_supports_breakpoints ())
4755 need_step_over
= find_thread ([this] (thread_info
*thread
)
4757 return thread_needs_step_over (thread
);
4760 if (need_step_over
!= NULL
)
4762 threads_debug_printf ("found thread %ld needing a step-over",
4763 lwpid_of (need_step_over
));
4765 start_step_over (get_thread_lwp (need_step_over
));
4770 threads_debug_printf ("Proceeding, no step-over needed");
4772 for_each_thread ([this] (thread_info
*thread
)
4774 proceed_one_lwp (thread
, NULL
);
4779 linux_process_target::unstop_all_lwps (int unsuspend
, lwp_info
*except
)
4781 THREADS_SCOPED_DEBUG_ENTER_EXIT
;
4784 threads_debug_printf ("except=(LWP %ld)",
4785 lwpid_of (get_lwp_thread (except
)));
4787 threads_debug_printf ("except=nullptr");
4790 for_each_thread ([&] (thread_info
*thread
)
4792 unsuspend_and_proceed_one_lwp (thread
, except
);
4795 for_each_thread ([&] (thread_info
*thread
)
4797 proceed_one_lwp (thread
, except
);
4802 #ifdef HAVE_LINUX_REGSETS
4804 #define use_linux_regsets 1
4806 /* Returns true if REGSET has been disabled. */
4809 regset_disabled (struct regsets_info
*info
, struct regset_info
*regset
)
4811 return (info
->disabled_regsets
!= NULL
4812 && info
->disabled_regsets
[regset
- info
->regsets
]);
4815 /* Disable REGSET. */
4818 disable_regset (struct regsets_info
*info
, struct regset_info
*regset
)
4822 dr_offset
= regset
- info
->regsets
;
4823 if (info
->disabled_regsets
== NULL
)
4824 info
->disabled_regsets
= (char *) xcalloc (1, info
->num_regsets
);
4825 info
->disabled_regsets
[dr_offset
] = 1;
4829 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
4830 struct regcache
*regcache
)
4832 struct regset_info
*regset
;
4833 int saw_general_regs
= 0;
4837 pid
= lwpid_of (current_thread
);
4838 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
4843 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
4846 buf
= xmalloc (regset
->size
);
4848 nt_type
= regset
->nt_type
;
4852 iov
.iov_len
= regset
->size
;
4853 data
= (void *) &iov
;
4859 res
= ptrace (regset
->get_request
, pid
,
4860 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
4862 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4867 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
4869 /* If we get EIO on a regset, or an EINVAL and the regset is
4870 optional, do not try it again for this process mode. */
4871 disable_regset (regsets_info
, regset
);
4873 else if (errno
== ENODATA
)
4875 /* ENODATA may be returned if the regset is currently
4876 not "active". This can happen in normal operation,
4877 so suppress the warning in this case. */
4879 else if (errno
== ESRCH
)
4881 /* At this point, ESRCH should mean the process is
4882 already gone, in which case we simply ignore attempts
4883 to read its registers. */
4888 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
4895 if (regset
->type
== GENERAL_REGS
)
4896 saw_general_regs
= 1;
4897 regset
->store_function (regcache
, buf
);
4901 if (saw_general_regs
)
4908 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
4909 struct regcache
*regcache
)
4911 struct regset_info
*regset
;
4912 int saw_general_regs
= 0;
4916 pid
= lwpid_of (current_thread
);
4917 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
4922 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
)
4923 || regset
->fill_function
== NULL
)
4926 buf
= xmalloc (regset
->size
);
4928 /* First fill the buffer with the current register set contents,
4929 in case there are any items in the kernel's regset that are
4930 not in gdbserver's regcache. */
4932 nt_type
= regset
->nt_type
;
4936 iov
.iov_len
= regset
->size
;
4937 data
= (void *) &iov
;
4943 res
= ptrace (regset
->get_request
, pid
,
4944 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
4946 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
4951 /* Then overlay our cached registers on that. */
4952 regset
->fill_function (regcache
, buf
);
4954 /* Only now do we write the register set. */
4956 res
= ptrace (regset
->set_request
, pid
,
4957 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
4959 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
4966 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
4968 /* If we get EIO on a regset, or an EINVAL and the regset is
4969 optional, do not try it again for this process mode. */
4970 disable_regset (regsets_info
, regset
);
4972 else if (errno
== ESRCH
)
4974 /* At this point, ESRCH should mean the process is
4975 already gone, in which case we simply ignore attempts
4976 to change its registers. See also the related
4977 comment in resume_one_lwp. */
4983 perror ("Warning: ptrace(regsets_store_inferior_registers)");
4986 else if (regset
->type
== GENERAL_REGS
)
4987 saw_general_regs
= 1;
4990 if (saw_general_regs
)
4996 #else /* !HAVE_LINUX_REGSETS */
4998 #define use_linux_regsets 0
4999 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5000 #define regsets_store_inferior_registers(regsets_info, regcache) 1
5004 /* Return 1 if register REGNO is supported by one of the regset ptrace
5005 calls or 0 if it has to be transferred individually. */
5008 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
5010 unsigned char mask
= 1 << (regno
% 8);
5011 size_t index
= regno
/ 8;
5013 return (use_linux_regsets
5014 && (regs_info
->regset_bitmap
== NULL
5015 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
5018 #ifdef HAVE_LINUX_USRREGS
5021 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
5025 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
5026 error ("Invalid register number %d.", regnum
);
5028 addr
= usrregs
->regmap
[regnum
];
5035 linux_process_target::fetch_register (const usrregs_info
*usrregs
,
5036 regcache
*regcache
, int regno
)
5043 if (regno
>= usrregs
->num_regs
)
5045 if (low_cannot_fetch_register (regno
))
5048 regaddr
= register_addr (usrregs
, regno
);
5052 size
= ((register_size (regcache
->tdesc
, regno
)
5053 + sizeof (PTRACE_XFER_TYPE
) - 1)
5054 & -sizeof (PTRACE_XFER_TYPE
));
5055 buf
= (char *) alloca (size
);
5057 pid
= lwpid_of (current_thread
);
5058 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5061 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
5062 ptrace (PTRACE_PEEKUSER
, pid
,
5063 /* Coerce to a uintptr_t first to avoid potential gcc warning
5064 of coercing an 8 byte integer to a 4 byte pointer. */
5065 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
, (PTRACE_TYPE_ARG4
) 0);
5066 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5069 /* Mark register REGNO unavailable. */
5070 supply_register (regcache
, regno
, NULL
);
5075 low_supply_ptrace_register (regcache
, regno
, buf
);
5079 linux_process_target::store_register (const usrregs_info
*usrregs
,
5080 regcache
*regcache
, int regno
)
5087 if (regno
>= usrregs
->num_regs
)
5089 if (low_cannot_store_register (regno
))
5092 regaddr
= register_addr (usrregs
, regno
);
5096 size
= ((register_size (regcache
->tdesc
, regno
)
5097 + sizeof (PTRACE_XFER_TYPE
) - 1)
5098 & -sizeof (PTRACE_XFER_TYPE
));
5099 buf
= (char *) alloca (size
);
5100 memset (buf
, 0, size
);
5102 low_collect_ptrace_register (regcache
, regno
, buf
);
5104 pid
= lwpid_of (current_thread
);
5105 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5108 ptrace (PTRACE_POKEUSER
, pid
,
5109 /* Coerce to a uintptr_t first to avoid potential gcc warning
5110 about coercing an 8 byte integer to a 4 byte pointer. */
5111 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
,
5112 (PTRACE_TYPE_ARG4
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
5115 /* At this point, ESRCH should mean the process is
5116 already gone, in which case we simply ignore attempts
5117 to change its registers. See also the related
5118 comment in resume_one_lwp. */
5123 if (!low_cannot_store_register (regno
))
5124 error ("writing register %d: %s", regno
, safe_strerror (errno
));
5126 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5129 #endif /* HAVE_LINUX_USRREGS */
5132 linux_process_target::low_collect_ptrace_register (regcache
*regcache
,
5133 int regno
, char *buf
)
5135 collect_register (regcache
, regno
, buf
);
5139 linux_process_target::low_supply_ptrace_register (regcache
*regcache
,
5140 int regno
, const char *buf
)
5142 supply_register (regcache
, regno
, buf
);
5146 linux_process_target::usr_fetch_inferior_registers (const regs_info
*regs_info
,
5150 #ifdef HAVE_LINUX_USRREGS
5151 struct usrregs_info
*usr
= regs_info
->usrregs
;
5155 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5156 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5157 fetch_register (usr
, regcache
, regno
);
5160 fetch_register (usr
, regcache
, regno
);
5165 linux_process_target::usr_store_inferior_registers (const regs_info
*regs_info
,
5169 #ifdef HAVE_LINUX_USRREGS
5170 struct usrregs_info
*usr
= regs_info
->usrregs
;
5174 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5175 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5176 store_register (usr
, regcache
, regno
);
5179 store_register (usr
, regcache
, regno
);
5184 linux_process_target::fetch_registers (regcache
*regcache
, int regno
)
5188 const regs_info
*regs_info
= get_regs_info ();
5192 if (regs_info
->usrregs
!= NULL
)
5193 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
5194 low_fetch_register (regcache
, regno
);
5196 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
5197 if (regs_info
->usrregs
!= NULL
)
5198 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
5202 if (low_fetch_register (regcache
, regno
))
5205 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5207 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
5209 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5210 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
5215 linux_process_target::store_registers (regcache
*regcache
, int regno
)
5219 const regs_info
*regs_info
= get_regs_info ();
5223 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5225 if (regs_info
->usrregs
!= NULL
)
5226 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
5230 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5232 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5234 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5235 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
5240 linux_process_target::low_fetch_register (regcache
*regcache
, int regno
)
5245 /* A wrapper for the read_memory target op. */
5248 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
5250 return the_target
->read_memory (memaddr
, myaddr
, len
);
5253 /* Copy LEN bytes from inferior's memory starting at MEMADDR
5254 to debugger memory starting at MYADDR. */
5257 linux_process_target::read_memory (CORE_ADDR memaddr
,
5258 unsigned char *myaddr
, int len
)
5260 int pid
= lwpid_of (current_thread
);
5261 PTRACE_XFER_TYPE
*buffer
;
5269 /* Try using /proc. Don't bother for one word. */
5270 if (len
>= 3 * sizeof (long))
5274 /* We could keep this file open and cache it - possibly one per
5275 thread. That requires some juggling, but is even faster. */
5276 sprintf (filename
, "/proc/%d/mem", pid
);
5277 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
5281 /* If pread64 is available, use it. It's faster if the kernel
5282 supports it (only one syscall), and it's 64-bit safe even on
5283 32-bit platforms (for instance, SPARC debugging a SPARC64
5286 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
5289 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
5290 bytes
= read (fd
, myaddr
, len
);
5297 /* Some data was read, we'll try to get the rest with ptrace. */
5307 /* Round starting address down to longword boundary. */
5308 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5309 /* Round ending address up; get number of longwords that makes. */
5310 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5311 / sizeof (PTRACE_XFER_TYPE
));
5312 /* Allocate buffer of that many longwords. */
5313 buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5315 /* Read all the longwords */
5317 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5319 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5320 about coercing an 8 byte integer to a 4 byte pointer. */
5321 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
5322 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5323 (PTRACE_TYPE_ARG4
) 0);
5329 /* Copy appropriate bytes out of the buffer. */
5332 i
*= sizeof (PTRACE_XFER_TYPE
);
5333 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
5335 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5342 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5343 memory at MEMADDR. On failure (cannot write to the inferior)
5344 returns the value of errno. Always succeeds if LEN is zero. */
5347 linux_process_target::write_memory (CORE_ADDR memaddr
,
5348 const unsigned char *myaddr
, int len
)
5351 /* Round starting address down to longword boundary. */
5352 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5353 /* Round ending address up; get number of longwords that makes. */
5355 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5356 / sizeof (PTRACE_XFER_TYPE
);
5358 /* Allocate buffer of that many longwords. */
5359 PTRACE_XFER_TYPE
*buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5361 int pid
= lwpid_of (current_thread
);
5365 /* Zero length write always succeeds. */
5371 /* Dump up to four bytes. */
5372 char str
[4 * 2 + 1];
5374 int dump
= len
< 4 ? len
: 4;
5376 for (i
= 0; i
< dump
; i
++)
5378 sprintf (p
, "%02x", myaddr
[i
]);
5383 threads_debug_printf ("Writing %s to 0x%08lx in process %d",
5384 str
, (long) memaddr
, pid
);
5387 /* Fill start and end extra bytes of buffer with existing memory data. */
5390 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5391 about coercing an 8 byte integer to a 4 byte pointer. */
5392 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
5393 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5394 (PTRACE_TYPE_ARG4
) 0);
5402 = ptrace (PTRACE_PEEKTEXT
, pid
,
5403 /* Coerce to a uintptr_t first to avoid potential gcc warning
5404 about coercing an 8 byte integer to a 4 byte pointer. */
5405 (PTRACE_TYPE_ARG3
) (uintptr_t) (addr
+ (count
- 1)
5406 * sizeof (PTRACE_XFER_TYPE
)),
5407 (PTRACE_TYPE_ARG4
) 0);
5412 /* Copy data to be written over corresponding part of buffer. */
5414 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5417 /* Write the entire buffer. */
5419 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5422 ptrace (PTRACE_POKETEXT
, pid
,
5423 /* Coerce to a uintptr_t first to avoid potential gcc warning
5424 about coercing an 8 byte integer to a 4 byte pointer. */
5425 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5426 (PTRACE_TYPE_ARG4
) buffer
[i
]);
5435 linux_process_target::look_up_symbols ()
5437 #ifdef USE_THREAD_DB
5438 struct process_info
*proc
= current_process ();
5440 if (proc
->priv
->thread_db
!= NULL
)
5448 linux_process_target::request_interrupt ()
5450 /* Send a SIGINT to the process group. This acts just like the user
5451 typed a ^C on the controlling terminal. */
5452 ::kill (-signal_pid
, SIGINT
);
5456 linux_process_target::supports_read_auxv ()
5461 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5462 to debugger memory starting at MYADDR. */
5465 linux_process_target::read_auxv (CORE_ADDR offset
, unsigned char *myaddr
,
5468 char filename
[PATH_MAX
];
5470 int pid
= lwpid_of (current_thread
);
5472 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5474 fd
= open (filename
, O_RDONLY
);
5478 if (offset
!= (CORE_ADDR
) 0
5479 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5482 n
= read (fd
, myaddr
, len
);
5490 linux_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5491 int size
, raw_breakpoint
*bp
)
5493 if (type
== raw_bkpt_type_sw
)
5494 return insert_memory_breakpoint (bp
);
5496 return low_insert_point (type
, addr
, size
, bp
);
5500 linux_process_target::low_insert_point (raw_bkpt_type type
, CORE_ADDR addr
,
5501 int size
, raw_breakpoint
*bp
)
5503 /* Unsupported (see target.h). */
5508 linux_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5509 int size
, raw_breakpoint
*bp
)
5511 if (type
== raw_bkpt_type_sw
)
5512 return remove_memory_breakpoint (bp
);
5514 return low_remove_point (type
, addr
, size
, bp
);
5518 linux_process_target::low_remove_point (raw_bkpt_type type
, CORE_ADDR addr
,
5519 int size
, raw_breakpoint
*bp
)
5521 /* Unsupported (see target.h). */
5525 /* Implement the stopped_by_sw_breakpoint target_ops
5529 linux_process_target::stopped_by_sw_breakpoint ()
5531 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5533 return (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
);
5536 /* Implement the supports_stopped_by_sw_breakpoint target_ops
5540 linux_process_target::supports_stopped_by_sw_breakpoint ()
5542 return USE_SIGTRAP_SIGINFO
;
5545 /* Implement the stopped_by_hw_breakpoint target_ops
5549 linux_process_target::stopped_by_hw_breakpoint ()
5551 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5553 return (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
);
5556 /* Implement the supports_stopped_by_hw_breakpoint target_ops
5560 linux_process_target::supports_stopped_by_hw_breakpoint ()
5562 return USE_SIGTRAP_SIGINFO
;
5565 /* Implement the supports_hardware_single_step target_ops method. */
5568 linux_process_target::supports_hardware_single_step ()
5574 linux_process_target::stopped_by_watchpoint ()
5576 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5578 return lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
5582 linux_process_target::stopped_data_address ()
5584 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5586 return lwp
->stopped_data_address
;
5589 /* This is only used for targets that define PT_TEXT_ADDR,
5590 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5591 the target has different ways of acquiring this information, like
5595 linux_process_target::supports_read_offsets ()
5597 #ifdef SUPPORTS_READ_OFFSETS
5604 /* Under uClinux, programs are loaded at non-zero offsets, which we need
5605 to tell gdb about. */
5608 linux_process_target::read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
5610 #ifdef SUPPORTS_READ_OFFSETS
5611 unsigned long text
, text_end
, data
;
5612 int pid
= lwpid_of (current_thread
);
5616 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_ADDR
,
5617 (PTRACE_TYPE_ARG4
) 0);
5618 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_END_ADDR
,
5619 (PTRACE_TYPE_ARG4
) 0);
5620 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_DATA_ADDR
,
5621 (PTRACE_TYPE_ARG4
) 0);
5625 /* Both text and data offsets produced at compile-time (and so
5626 used by gdb) are relative to the beginning of the program,
5627 with the data segment immediately following the text segment.
5628 However, the actual runtime layout in memory may put the data
5629 somewhere else, so when we send gdb a data base-address, we
5630 use the real data base address and subtract the compile-time
5631 data base-address from it (which is just the length of the
5632 text segment). BSS immediately follows data in both
5635 *data_p
= data
- (text_end
- text
);
5641 gdb_assert_not_reached ("target op read_offsets not supported");
5646 linux_process_target::supports_get_tls_address ()
5648 #ifdef USE_THREAD_DB
5656 linux_process_target::get_tls_address (thread_info
*thread
,
5658 CORE_ADDR load_module
,
5661 #ifdef USE_THREAD_DB
5662 return thread_db_get_tls_address (thread
, offset
, load_module
, address
);
5669 linux_process_target::supports_qxfer_osdata ()
5675 linux_process_target::qxfer_osdata (const char *annex
,
5676 unsigned char *readbuf
,
5677 unsigned const char *writebuf
,
5678 CORE_ADDR offset
, int len
)
5680 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
5684 linux_process_target::siginfo_fixup (siginfo_t
*siginfo
,
5685 gdb_byte
*inf_siginfo
, int direction
)
5687 bool done
= low_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
5689 /* If there was no callback, or the callback didn't do anything,
5690 then just do a straight memcpy. */
5694 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
5696 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
5701 linux_process_target::low_siginfo_fixup (siginfo_t
*native
, gdb_byte
*inf
,
5708 linux_process_target::supports_qxfer_siginfo ()
5714 linux_process_target::qxfer_siginfo (const char *annex
,
5715 unsigned char *readbuf
,
5716 unsigned const char *writebuf
,
5717 CORE_ADDR offset
, int len
)
5721 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
5723 if (current_thread
== NULL
)
5726 pid
= lwpid_of (current_thread
);
5728 threads_debug_printf ("%s siginfo for lwp %d.",
5729 readbuf
!= NULL
? "Reading" : "Writing",
5732 if (offset
>= sizeof (siginfo
))
5735 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
5738 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
5739 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
5740 inferior with a 64-bit GDBSERVER should look the same as debugging it
5741 with a 32-bit GDBSERVER, we need to convert it. */
5742 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
5744 if (offset
+ len
> sizeof (siginfo
))
5745 len
= sizeof (siginfo
) - offset
;
5747 if (readbuf
!= NULL
)
5748 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
5751 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
5753 /* Convert back to ptrace layout before flushing it out. */
5754 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
5756 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
5763 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
5764 so we notice when children change state; as the handler for the
5765 sigsuspend in my_waitpid. */
5768 sigchld_handler (int signo
)
5770 int old_errno
= errno
;
5776 /* Use the async signal safe debug function. */
5777 if (debug_write ("sigchld_handler\n",
5778 sizeof ("sigchld_handler\n") - 1) < 0)
5779 break; /* just ignore */
5783 if (target_is_async_p ())
5784 async_file_mark (); /* trigger a linux_wait */
5790 linux_process_target::supports_non_stop ()
5796 linux_process_target::async (bool enable
)
5798 bool previous
= target_is_async_p ();
5800 threads_debug_printf ("async (%d), previous=%d",
5803 if (previous
!= enable
)
5806 sigemptyset (&mask
);
5807 sigaddset (&mask
, SIGCHLD
);
5809 gdb_sigmask (SIG_BLOCK
, &mask
, NULL
);
5813 if (!linux_event_pipe
.open ())
5815 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
5817 warning ("creating event pipe failed.");
5821 /* Register the event loop handler. */
5822 add_file_handler (linux_event_pipe
.event_fd (),
5823 handle_target_event
, NULL
,
5826 /* Always trigger a linux_wait. */
5831 delete_file_handler (linux_event_pipe
.event_fd ());
5833 linux_event_pipe
.close ();
5836 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
5843 linux_process_target::start_non_stop (bool nonstop
)
5845 /* Register or unregister from event-loop accordingly. */
5846 target_async (nonstop
);
5848 if (target_is_async_p () != (nonstop
!= false))
5855 linux_process_target::supports_multi_process ()
5860 /* Check if fork events are supported. */
5863 linux_process_target::supports_fork_events ()
5868 /* Check if vfork events are supported. */
5871 linux_process_target::supports_vfork_events ()
5876 /* Check if exec events are supported. */
5879 linux_process_target::supports_exec_events ()
5884 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
5885 ptrace flags for all inferiors. This is in case the new GDB connection
5886 doesn't support the same set of events that the previous one did. */
5889 linux_process_target::handle_new_gdb_connection ()
5891 /* Request that all the lwps reset their ptrace options. */
5892 for_each_thread ([] (thread_info
*thread
)
5894 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5898 /* Stop the lwp so we can modify its ptrace options. */
5899 lwp
->must_set_ptrace_flags
= 1;
5900 linux_stop_lwp (lwp
);
5904 /* Already stopped; go ahead and set the ptrace options. */
5905 struct process_info
*proc
= find_process_pid (pid_of (thread
));
5906 int options
= linux_low_ptrace_options (proc
->attached
);
5908 linux_enable_event_reporting (lwpid_of (thread
), options
);
5909 lwp
->must_set_ptrace_flags
= 0;
5915 linux_process_target::handle_monitor_command (char *mon
)
5917 #ifdef USE_THREAD_DB
5918 return thread_db_handle_monitor_command (mon
);
5925 linux_process_target::core_of_thread (ptid_t ptid
)
5927 return linux_common_core_of_thread (ptid
);
5931 linux_process_target::supports_disable_randomization ()
5937 linux_process_target::supports_agent ()
5943 linux_process_target::supports_range_stepping ()
5945 if (supports_software_single_step ())
5948 return low_supports_range_stepping ();
5952 linux_process_target::low_supports_range_stepping ()
5958 linux_process_target::supports_pid_to_exec_file ()
5964 linux_process_target::pid_to_exec_file (int pid
)
5966 return linux_proc_pid_to_exec_file (pid
);
5970 linux_process_target::supports_multifs ()
5976 linux_process_target::multifs_open (int pid
, const char *filename
,
5977 int flags
, mode_t mode
)
5979 return linux_mntns_open_cloexec (pid
, filename
, flags
, mode
);
5983 linux_process_target::multifs_unlink (int pid
, const char *filename
)
5985 return linux_mntns_unlink (pid
, filename
);
5989 linux_process_target::multifs_readlink (int pid
, const char *filename
,
5990 char *buf
, size_t bufsiz
)
5992 return linux_mntns_readlink (pid
, filename
, buf
, bufsiz
);
5995 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
5996 struct target_loadseg
5998 /* Core address to which the segment is mapped. */
6000 /* VMA recorded in the program header. */
6002 /* Size of this segment in memory. */
6006 # if defined PT_GETDSBT
6007 struct target_loadmap
6009 /* Protocol version number, must be zero. */
6011 /* Pointer to the DSBT table, its size, and the DSBT index. */
6012 unsigned *dsbt_table
;
6013 unsigned dsbt_size
, dsbt_index
;
6014 /* Number of segments in this map. */
6016 /* The actual memory map. */
6017 struct target_loadseg segs
[/*nsegs*/];
6019 # define LINUX_LOADMAP PT_GETDSBT
6020 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6021 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6023 struct target_loadmap
6025 /* Protocol version number, must be zero. */
6027 /* Number of segments in this map. */
6029 /* The actual memory map. */
6030 struct target_loadseg segs
[/*nsegs*/];
6032 # define LINUX_LOADMAP PTRACE_GETFDPIC
6033 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6034 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6038 linux_process_target::supports_read_loadmap ()
6044 linux_process_target::read_loadmap (const char *annex
, CORE_ADDR offset
,
6045 unsigned char *myaddr
, unsigned int len
)
6047 int pid
= lwpid_of (current_thread
);
6049 struct target_loadmap
*data
= NULL
;
6050 unsigned int actual_length
, copy_length
;
6052 if (strcmp (annex
, "exec") == 0)
6053 addr
= (int) LINUX_LOADMAP_EXEC
;
6054 else if (strcmp (annex
, "interp") == 0)
6055 addr
= (int) LINUX_LOADMAP_INTERP
;
6059 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
6065 actual_length
= sizeof (struct target_loadmap
)
6066 + sizeof (struct target_loadseg
) * data
->nsegs
;
6068 if (offset
< 0 || offset
> actual_length
)
6071 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
6072 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
6075 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6078 linux_process_target::supports_catch_syscall ()
6080 return low_supports_catch_syscall ();
6084 linux_process_target::low_supports_catch_syscall ()
6090 linux_process_target::read_pc (regcache
*regcache
)
6092 if (!low_supports_breakpoints ())
6095 return low_get_pc (regcache
);
6099 linux_process_target::write_pc (regcache
*regcache
, CORE_ADDR pc
)
6101 gdb_assert (low_supports_breakpoints ());
6103 low_set_pc (regcache
, pc
);
6107 linux_process_target::supports_thread_stopped ()
6113 linux_process_target::thread_stopped (thread_info
*thread
)
6115 return get_thread_lwp (thread
)->stopped
;
6118 /* This exposes stop-all-threads functionality to other modules. */
6121 linux_process_target::pause_all (bool freeze
)
6123 stop_all_lwps (freeze
, NULL
);
6126 /* This exposes unstop-all-threads functionality to other gdbserver
6130 linux_process_target::unpause_all (bool unfreeze
)
6132 unstop_all_lwps (unfreeze
, NULL
);
6136 linux_process_target::prepare_to_access_memory ()
6138 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6141 target_pause_all (true);
6146 linux_process_target::done_accessing_memory ()
6148 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6151 target_unpause_all (true);
6154 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6157 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
6158 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
6160 char filename
[PATH_MAX
];
6162 const int auxv_size
= is_elf64
6163 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
6164 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
6166 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
6168 fd
= open (filename
, O_RDONLY
);
6174 while (read (fd
, buf
, auxv_size
) == auxv_size
6175 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
6179 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
6181 switch (aux
->a_type
)
6184 *phdr_memaddr
= aux
->a_un
.a_val
;
6187 *num_phdr
= aux
->a_un
.a_val
;
6193 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
6195 switch (aux
->a_type
)
6198 *phdr_memaddr
= aux
->a_un
.a_val
;
6201 *num_phdr
= aux
->a_un
.a_val
;
6209 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
6211 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6212 "phdr_memaddr = %ld, phdr_num = %d",
6213 (long) *phdr_memaddr
, *num_phdr
);
6220 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6223 get_dynamic (const int pid
, const int is_elf64
)
6225 CORE_ADDR phdr_memaddr
, relocation
;
6227 unsigned char *phdr_buf
;
6228 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
6230 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
6233 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
6234 phdr_buf
= (unsigned char *) alloca (num_phdr
* phdr_size
);
6236 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
6239 /* Compute relocation: it is expected to be 0 for "regular" executables,
6240 non-zero for PIE ones. */
6242 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
6245 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6247 if (p
->p_type
== PT_PHDR
)
6248 relocation
= phdr_memaddr
- p
->p_vaddr
;
6252 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6254 if (p
->p_type
== PT_PHDR
)
6255 relocation
= phdr_memaddr
- p
->p_vaddr
;
6258 if (relocation
== -1)
6260 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6261 any real world executables, including PIE executables, have always
6262 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6263 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6264 or present DT_DEBUG anyway (fpc binaries are statically linked).
6266 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6268 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6273 for (i
= 0; i
< num_phdr
; i
++)
6277 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6279 if (p
->p_type
== PT_DYNAMIC
)
6280 return p
->p_vaddr
+ relocation
;
6284 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6286 if (p
->p_type
== PT_DYNAMIC
)
6287 return p
->p_vaddr
+ relocation
;
6294 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6295 can be 0 if the inferior does not yet have the library list initialized.
6296 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6297 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6300 get_r_debug (const int pid
, const int is_elf64
)
6302 CORE_ADDR dynamic_memaddr
;
6303 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
6304 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
6307 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
6308 if (dynamic_memaddr
== 0)
6311 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
6315 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
6316 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6320 unsigned char buf
[sizeof (Elf64_Xword
)];
6324 #ifdef DT_MIPS_RLD_MAP
6325 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6327 if (linux_read_memory (dyn
->d_un
.d_val
,
6328 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6333 #endif /* DT_MIPS_RLD_MAP */
6334 #ifdef DT_MIPS_RLD_MAP_REL
6335 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6337 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6338 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6343 #endif /* DT_MIPS_RLD_MAP_REL */
6345 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6346 map
= dyn
->d_un
.d_val
;
6348 if (dyn
->d_tag
== DT_NULL
)
6353 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
6354 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6358 unsigned char buf
[sizeof (Elf32_Word
)];
6362 #ifdef DT_MIPS_RLD_MAP
6363 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6365 if (linux_read_memory (dyn
->d_un
.d_val
,
6366 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6371 #endif /* DT_MIPS_RLD_MAP */
6372 #ifdef DT_MIPS_RLD_MAP_REL
6373 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6375 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6376 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6381 #endif /* DT_MIPS_RLD_MAP_REL */
6383 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6384 map
= dyn
->d_un
.d_val
;
6386 if (dyn
->d_tag
== DT_NULL
)
6390 dynamic_memaddr
+= dyn_size
;
6396 /* Read one pointer from MEMADDR in the inferior. */
6399 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
6403 /* Go through a union so this works on either big or little endian
6404 hosts, when the inferior's pointer size is smaller than the size
6405 of CORE_ADDR. It is assumed the inferior's endianness is the
6406 same of the superior's. */
6409 CORE_ADDR core_addr
;
6414 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
6417 if (ptr_size
== sizeof (CORE_ADDR
))
6418 *ptr
= addr
.core_addr
;
6419 else if (ptr_size
== sizeof (unsigned int))
6422 gdb_assert_not_reached ("unhandled pointer size");
6428 linux_process_target::supports_qxfer_libraries_svr4 ()
6433 struct link_map_offsets
6435 /* Offset and size of r_debug.r_version. */
6436 int r_version_offset
;
6438 /* Offset and size of r_debug.r_map. */
6441 /* Offset to l_addr field in struct link_map. */
6444 /* Offset to l_name field in struct link_map. */
6447 /* Offset to l_ld field in struct link_map. */
6450 /* Offset to l_next field in struct link_map. */
6453 /* Offset to l_prev field in struct link_map. */
6457 /* Construct qXfer:libraries-svr4:read reply. */
6460 linux_process_target::qxfer_libraries_svr4 (const char *annex
,
6461 unsigned char *readbuf
,
6462 unsigned const char *writebuf
,
6463 CORE_ADDR offset
, int len
)
6465 struct process_info_private
*const priv
= current_process ()->priv
;
6466 char filename
[PATH_MAX
];
6469 static const struct link_map_offsets lmo_32bit_offsets
=
6471 0, /* r_version offset. */
6472 4, /* r_debug.r_map offset. */
6473 0, /* l_addr offset in link_map. */
6474 4, /* l_name offset in link_map. */
6475 8, /* l_ld offset in link_map. */
6476 12, /* l_next offset in link_map. */
6477 16 /* l_prev offset in link_map. */
6480 static const struct link_map_offsets lmo_64bit_offsets
=
6482 0, /* r_version offset. */
6483 8, /* r_debug.r_map offset. */
6484 0, /* l_addr offset in link_map. */
6485 8, /* l_name offset in link_map. */
6486 16, /* l_ld offset in link_map. */
6487 24, /* l_next offset in link_map. */
6488 32 /* l_prev offset in link_map. */
6490 const struct link_map_offsets
*lmo
;
6491 unsigned int machine
;
6493 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
6494 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
6495 int header_done
= 0;
6497 if (writebuf
!= NULL
)
6499 if (readbuf
== NULL
)
6502 pid
= lwpid_of (current_thread
);
6503 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
6504 is_elf64
= elf_64_file_p (filename
, &machine
);
6505 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
6506 ptr_size
= is_elf64
? 8 : 4;
6508 while (annex
[0] != '\0')
6514 sep
= strchr (annex
, '=');
6518 name_len
= sep
- annex
;
6519 if (name_len
== 5 && startswith (annex
, "start"))
6521 else if (name_len
== 4 && startswith (annex
, "prev"))
6525 annex
= strchr (sep
, ';');
6532 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
6539 if (priv
->r_debug
== 0)
6540 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
6542 /* We failed to find DT_DEBUG. Such situation will not change
6543 for this inferior - do not retry it. Report it to GDB as
6544 E01, see for the reasons at the GDB solib-svr4.c side. */
6545 if (priv
->r_debug
== (CORE_ADDR
) -1)
6548 if (priv
->r_debug
!= 0)
6550 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
6551 (unsigned char *) &r_version
,
6552 sizeof (r_version
)) != 0
6555 warning ("unexpected r_debug version %d", r_version
);
6557 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
6558 &lm_addr
, ptr_size
) != 0)
6560 warning ("unable to read r_map from 0x%lx",
6561 (long) priv
->r_debug
+ lmo
->r_map_offset
);
6566 std::string document
= "<library-list-svr4 version=\"1.0\"";
6569 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
6570 &l_name
, ptr_size
) == 0
6571 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
6572 &l_addr
, ptr_size
) == 0
6573 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
6574 &l_ld
, ptr_size
) == 0
6575 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
6576 &l_prev
, ptr_size
) == 0
6577 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
6578 &l_next
, ptr_size
) == 0)
6580 unsigned char libname
[PATH_MAX
];
6582 if (lm_prev
!= l_prev
)
6584 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
6585 (long) lm_prev
, (long) l_prev
);
6589 /* Ignore the first entry even if it has valid name as the first entry
6590 corresponds to the main executable. The first entry should not be
6591 skipped if the dynamic loader was loaded late by a static executable
6592 (see solib-svr4.c parameter ignore_first). But in such case the main
6593 executable does not have PT_DYNAMIC present and this function already
6594 exited above due to failed get_r_debug. */
6596 string_appendf (document
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
6599 /* Not checking for error because reading may stop before
6600 we've got PATH_MAX worth of characters. */
6602 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
6603 libname
[sizeof (libname
) - 1] = '\0';
6604 if (libname
[0] != '\0')
6608 /* Terminate `<library-list-svr4'. */
6613 string_appendf (document
, "<library name=\"");
6614 xml_escape_text_append (&document
, (char *) libname
);
6615 string_appendf (document
, "\" lm=\"0x%lx\" "
6616 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
6617 (unsigned long) lm_addr
, (unsigned long) l_addr
,
6618 (unsigned long) l_ld
);
6628 /* Empty list; terminate `<library-list-svr4'. */
6632 document
+= "</library-list-svr4>";
6634 int document_len
= document
.length ();
6635 if (offset
< document_len
)
6636 document_len
-= offset
;
6639 if (len
> document_len
)
6642 memcpy (readbuf
, document
.data () + offset
, len
);
6647 #ifdef HAVE_LINUX_BTRACE
6649 btrace_target_info
*
6650 linux_process_target::enable_btrace (thread_info
*tp
,
6651 const btrace_config
*conf
)
6653 return linux_enable_btrace (tp
->id
, conf
);
6656 /* See to_disable_btrace target method. */
6659 linux_process_target::disable_btrace (btrace_target_info
*tinfo
)
6661 enum btrace_error err
;
6663 err
= linux_disable_btrace (tinfo
);
6664 return (err
== BTRACE_ERR_NONE
? 0 : -1);
6667 /* Encode an Intel Processor Trace configuration. */
6670 linux_low_encode_pt_config (struct buffer
*buffer
,
6671 const struct btrace_data_pt_config
*config
)
6673 buffer_grow_str (buffer
, "<pt-config>\n");
6675 switch (config
->cpu
.vendor
)
6678 buffer_xml_printf (buffer
, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
6679 "model=\"%u\" stepping=\"%u\"/>\n",
6680 config
->cpu
.family
, config
->cpu
.model
,
6681 config
->cpu
.stepping
);
6688 buffer_grow_str (buffer
, "</pt-config>\n");
6691 /* Encode a raw buffer. */
6694 linux_low_encode_raw (struct buffer
*buffer
, const gdb_byte
*data
,
6700 /* We use hex encoding - see gdbsupport/rsp-low.h. */
6701 buffer_grow_str (buffer
, "<raw>\n");
6707 elem
[0] = tohex ((*data
>> 4) & 0xf);
6708 elem
[1] = tohex (*data
++ & 0xf);
6710 buffer_grow (buffer
, elem
, 2);
6713 buffer_grow_str (buffer
, "</raw>\n");
6716 /* See to_read_btrace target method. */
6719 linux_process_target::read_btrace (btrace_target_info
*tinfo
,
6721 enum btrace_read_type type
)
6723 struct btrace_data btrace
;
6724 enum btrace_error err
;
6726 err
= linux_read_btrace (&btrace
, tinfo
, type
);
6727 if (err
!= BTRACE_ERR_NONE
)
6729 if (err
== BTRACE_ERR_OVERFLOW
)
6730 buffer_grow_str0 (buffer
, "E.Overflow.");
6732 buffer_grow_str0 (buffer
, "E.Generic Error.");
6737 switch (btrace
.format
)
6739 case BTRACE_FORMAT_NONE
:
6740 buffer_grow_str0 (buffer
, "E.No Trace.");
6743 case BTRACE_FORMAT_BTS
:
6744 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
6745 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
6747 for (const btrace_block
&block
: *btrace
.variant
.bts
.blocks
)
6748 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
6749 paddress (block
.begin
), paddress (block
.end
));
6751 buffer_grow_str0 (buffer
, "</btrace>\n");
6754 case BTRACE_FORMAT_PT
:
6755 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
6756 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
6757 buffer_grow_str (buffer
, "<pt>\n");
6759 linux_low_encode_pt_config (buffer
, &btrace
.variant
.pt
.config
);
6761 linux_low_encode_raw (buffer
, btrace
.variant
.pt
.data
,
6762 btrace
.variant
.pt
.size
);
6764 buffer_grow_str (buffer
, "</pt>\n");
6765 buffer_grow_str0 (buffer
, "</btrace>\n");
6769 buffer_grow_str0 (buffer
, "E.Unsupported Trace Format.");
6776 /* See to_btrace_conf target method. */
6779 linux_process_target::read_btrace_conf (const btrace_target_info
*tinfo
,
6782 const struct btrace_config
*conf
;
6784 buffer_grow_str (buffer
, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
6785 buffer_grow_str (buffer
, "<btrace-conf version=\"1.0\">\n");
6787 conf
= linux_btrace_conf (tinfo
);
6790 switch (conf
->format
)
6792 case BTRACE_FORMAT_NONE
:
6795 case BTRACE_FORMAT_BTS
:
6796 buffer_xml_printf (buffer
, "<bts");
6797 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->bts
.size
);
6798 buffer_xml_printf (buffer
, " />\n");
6801 case BTRACE_FORMAT_PT
:
6802 buffer_xml_printf (buffer
, "<pt");
6803 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->pt
.size
);
6804 buffer_xml_printf (buffer
, "/>\n");
6809 buffer_grow_str0 (buffer
, "</btrace-conf>\n");
6812 #endif /* HAVE_LINUX_BTRACE */
6814 /* See nat/linux-nat.h. */
6817 current_lwp_ptid (void)
6819 return ptid_of (current_thread
);
6823 linux_process_target::thread_name (ptid_t thread
)
6825 return linux_proc_tid_get_name (thread
);
6830 linux_process_target::thread_handle (ptid_t ptid
, gdb_byte
**handle
,
6833 return thread_db_thread_handle (ptid
, handle
, handle_len
);
6838 linux_process_target::thread_pending_parent (thread_info
*thread
)
6840 lwp_info
*parent
= get_thread_lwp (thread
)->pending_parent ();
6842 if (parent
== nullptr)
6845 return get_lwp_thread (parent
);
6849 linux_process_target::thread_pending_child (thread_info
*thread
)
6851 lwp_info
*child
= get_thread_lwp (thread
)->pending_child ();
6853 if (child
== nullptr)
6856 return get_lwp_thread (child
);
6859 /* Default implementation of linux_target_ops method "set_pc" for
6860 32-bit pc register which is literally named "pc". */
6863 linux_set_pc_32bit (struct regcache
*regcache
, CORE_ADDR pc
)
6865 uint32_t newpc
= pc
;
6867 supply_register_by_name (regcache
, "pc", &newpc
);
6870 /* Default implementation of linux_target_ops method "get_pc" for
6871 32-bit pc register which is literally named "pc". */
6874 linux_get_pc_32bit (struct regcache
*regcache
)
6878 collect_register_by_name (regcache
, "pc", &pc
);
6879 threads_debug_printf ("stop pc is 0x%" PRIx32
, pc
);
6883 /* Default implementation of linux_target_ops method "set_pc" for
6884 64-bit pc register which is literally named "pc". */
6887 linux_set_pc_64bit (struct regcache
*regcache
, CORE_ADDR pc
)
6889 uint64_t newpc
= pc
;
6891 supply_register_by_name (regcache
, "pc", &newpc
);
6894 /* Default implementation of linux_target_ops method "get_pc" for
6895 64-bit pc register which is literally named "pc". */
6898 linux_get_pc_64bit (struct regcache
*regcache
)
6902 collect_register_by_name (regcache
, "pc", &pc
);
6903 threads_debug_printf ("stop pc is 0x%" PRIx64
, pc
);
6907 /* See linux-low.h. */
6910 linux_get_auxv (int wordsize
, CORE_ADDR match
, CORE_ADDR
*valp
)
6912 gdb_byte
*data
= (gdb_byte
*) alloca (2 * wordsize
);
6915 gdb_assert (wordsize
== 4 || wordsize
== 8);
6917 while (the_target
->read_auxv (offset
, data
, 2 * wordsize
) == 2 * wordsize
)
6921 uint32_t *data_p
= (uint32_t *) data
;
6922 if (data_p
[0] == match
)
6930 uint64_t *data_p
= (uint64_t *) data
;
6931 if (data_p
[0] == match
)
6938 offset
+= 2 * wordsize
;
6944 /* See linux-low.h. */
6947 linux_get_hwcap (int wordsize
)
6949 CORE_ADDR hwcap
= 0;
6950 linux_get_auxv (wordsize
, AT_HWCAP
, &hwcap
);
6954 /* See linux-low.h. */
6957 linux_get_hwcap2 (int wordsize
)
6959 CORE_ADDR hwcap2
= 0;
6960 linux_get_auxv (wordsize
, AT_HWCAP2
, &hwcap2
);
6964 #ifdef HAVE_LINUX_REGSETS
6966 initialize_regsets_info (struct regsets_info
*info
)
6968 for (info
->num_regsets
= 0;
6969 info
->regsets
[info
->num_regsets
].size
>= 0;
6970 info
->num_regsets
++)
6976 initialize_low (void)
6978 struct sigaction sigchld_action
;
6980 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
6981 set_target_ops (the_linux_target
);
6983 linux_ptrace_init_warnings ();
6984 linux_proc_init_warnings ();
6986 sigchld_action
.sa_handler
= sigchld_handler
;
6987 sigemptyset (&sigchld_action
.sa_mask
);
6988 sigchld_action
.sa_flags
= SA_RESTART
;
6989 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
6991 initialize_low_arch ();
6993 linux_check_ptrace_features ();