1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "linux-low.h"
21 #include "nat/linux-osdata.h"
22 #include "gdbsupport/agent.h"
24 #include "gdbsupport/rsp-low.h"
25 #include "gdbsupport/signals-state-save-restore.h"
26 #include "nat/linux-nat.h"
27 #include "nat/linux-waitpid.h"
28 #include "gdbsupport/gdb_wait.h"
29 #include "nat/gdb_ptrace.h"
30 #include "nat/linux-ptrace.h"
31 #include "nat/linux-procfs.h"
32 #include "nat/linux-personality.h"
34 #include <sys/ioctl.h>
37 #include <sys/syscall.h>
41 #include <sys/types.h>
46 #include "gdbsupport/filestuff.h"
47 #include "tracepoint.h"
49 #include "gdbsupport/common-inferior.h"
50 #include "nat/fork-inferior.h"
51 #include "gdbsupport/environ.h"
52 #include "gdbsupport/gdb-sigmask.h"
53 #include "gdbsupport/scoped_restore.h"
55 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
56 then ELFMAG0 will have been defined. If it didn't get included by
57 gdb_proc_service.h then including it will likely introduce a duplicate
58 definition of elf_fpregset_t. */
61 #include "nat/linux-namespaces.h"
63 #ifdef HAVE_PERSONALITY
64 # include <sys/personality.h>
65 # if !HAVE_DECL_ADDR_NO_RANDOMIZE
66 # define ADDR_NO_RANDOMIZE 0x0040000
78 /* Some targets did not define these ptrace constants from the start,
79 so gdbserver defines them locally here. In the future, these may
80 be removed after they are added to asm/ptrace.h. */
81 #if !(defined(PT_TEXT_ADDR) \
82 || defined(PT_DATA_ADDR) \
83 || defined(PT_TEXT_END_ADDR))
84 #if defined(__mcoldfire__)
85 /* These are still undefined in 3.10 kernels. */
86 #define PT_TEXT_ADDR 49*4
87 #define PT_DATA_ADDR 50*4
88 #define PT_TEXT_END_ADDR 51*4
89 /* BFIN already defines these since at least 2.6.32 kernels. */
91 #define PT_TEXT_ADDR 220
92 #define PT_TEXT_END_ADDR 224
93 #define PT_DATA_ADDR 228
94 /* These are still undefined in 3.10 kernels. */
95 #elif defined(__TMS320C6X__)
96 #define PT_TEXT_ADDR (0x10000*4)
97 #define PT_DATA_ADDR (0x10004*4)
98 #define PT_TEXT_END_ADDR (0x10008*4)
102 #if (defined(__UCLIBC__) \
103 && defined(HAS_NOMMU) \
104 && defined(PT_TEXT_ADDR) \
105 && defined(PT_DATA_ADDR) \
106 && defined(PT_TEXT_END_ADDR))
107 #define SUPPORTS_READ_OFFSETS
110 #ifdef HAVE_LINUX_BTRACE
111 # include "nat/linux-btrace.h"
112 # include "gdbsupport/btrace-common.h"
115 #ifndef HAVE_ELF32_AUXV_T
116 /* Copied from glibc's elf.h. */
119 uint32_t a_type
; /* Entry type */
122 uint32_t a_val
; /* Integer value */
123 /* We use to have pointer elements added here. We cannot do that,
124 though, since it does not work when using 32-bit definitions
125 on 64-bit platforms and vice versa. */
130 #ifndef HAVE_ELF64_AUXV_T
131 /* Copied from glibc's elf.h. */
134 uint64_t a_type
; /* Entry type */
137 uint64_t a_val
; /* Integer value */
138 /* We use to have pointer elements added here. We cannot do that,
139 though, since it does not work when using 32-bit definitions
140 on 64-bit platforms and vice versa. */
145 /* Does the current host support PTRACE_GETREGSET? */
146 int have_ptrace_getregset
= -1;
150 /* See nat/linux-nat.h. */
153 ptid_of_lwp (struct lwp_info
*lwp
)
155 return ptid_of (get_lwp_thread (lwp
));
158 /* See nat/linux-nat.h. */
161 lwp_set_arch_private_info (struct lwp_info
*lwp
,
162 struct arch_lwp_info
*info
)
164 lwp
->arch_private
= info
;
167 /* See nat/linux-nat.h. */
169 struct arch_lwp_info
*
170 lwp_arch_private_info (struct lwp_info
*lwp
)
172 return lwp
->arch_private
;
175 /* See nat/linux-nat.h. */
178 lwp_is_stopped (struct lwp_info
*lwp
)
183 /* See nat/linux-nat.h. */
185 enum target_stop_reason
186 lwp_stop_reason (struct lwp_info
*lwp
)
188 return lwp
->stop_reason
;
191 /* See nat/linux-nat.h. */
194 lwp_is_stepping (struct lwp_info
*lwp
)
196 return lwp
->stepping
;
199 /* A list of all unknown processes which receive stop signals. Some
200 other process will presumably claim each of these as forked
201 children momentarily. */
203 struct simple_pid_list
205 /* The process ID. */
208 /* The status as reported by waitpid. */
212 struct simple_pid_list
*next
;
214 struct simple_pid_list
*stopped_pids
;
216 /* Trivial list manipulation functions to keep track of a list of new
217 stopped processes. */
220 add_to_pid_list (struct simple_pid_list
**listp
, int pid
, int status
)
222 struct simple_pid_list
*new_pid
= XNEW (struct simple_pid_list
);
225 new_pid
->status
= status
;
226 new_pid
->next
= *listp
;
231 pull_pid_from_list (struct simple_pid_list
**listp
, int pid
, int *statusp
)
233 struct simple_pid_list
**p
;
235 for (p
= listp
; *p
!= NULL
; p
= &(*p
)->next
)
236 if ((*p
)->pid
== pid
)
238 struct simple_pid_list
*next
= (*p
)->next
;
240 *statusp
= (*p
)->status
;
248 enum stopping_threads_kind
250 /* Not stopping threads presently. */
251 NOT_STOPPING_THREADS
,
253 /* Stopping threads. */
256 /* Stopping and suspending threads. */
257 STOPPING_AND_SUSPENDING_THREADS
260 /* This is set while stop_all_lwps is in effect. */
261 enum stopping_threads_kind stopping_threads
= NOT_STOPPING_THREADS
;
263 /* FIXME make into a target method? */
264 int using_threads
= 1;
266 /* True if we're presently stabilizing threads (moving them out of
268 static int stabilizing_threads
;
270 static void unsuspend_all_lwps (struct lwp_info
*except
);
271 static struct lwp_info
*add_lwp (ptid_t ptid
);
272 static void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
273 static int lwp_is_marked_dead (struct lwp_info
*lwp
);
274 static int finish_step_over (struct lwp_info
*lwp
);
275 static int kill_lwp (unsigned long lwpid
, int signo
);
276 static void enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
);
277 static int linux_low_ptrace_options (int attached
);
278 static int check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
);
280 /* When the event-loop is doing a step-over, this points at the thread
282 ptid_t step_over_bkpt
;
284 /* True if the low target can hardware single-step. */
287 can_hardware_single_step (void)
289 if (the_low_target
.supports_hardware_single_step
!= NULL
)
290 return the_low_target
.supports_hardware_single_step ();
296 linux_process_target::low_supports_breakpoints ()
302 linux_process_target::low_get_pc (regcache
*regcache
)
308 linux_process_target::low_set_pc (regcache
*regcache
, CORE_ADDR newpc
)
310 gdb_assert_not_reached ("linux target op low_set_pc is not implemented");
313 std::vector
<CORE_ADDR
>
314 linux_process_target::low_get_next_pcs (regcache
*regcache
)
316 gdb_assert_not_reached ("linux target op low_get_next_pcs is not "
321 linux_process_target::low_decr_pc_after_break ()
326 /* Returns true if this target can support fast tracepoints. This
327 does not mean that the in-process agent has been loaded in the
331 supports_fast_tracepoints (void)
333 return the_low_target
.install_fast_tracepoint_jump_pad
!= NULL
;
336 /* True if LWP is stopped in its stepping range. */
339 lwp_in_step_range (struct lwp_info
*lwp
)
341 CORE_ADDR pc
= lwp
->stop_pc
;
343 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
346 struct pending_signals
350 struct pending_signals
*prev
;
353 /* The read/write ends of the pipe registered as waitable file in the
355 static int linux_event_pipe
[2] = { -1, -1 };
357 /* True if we're currently in async mode. */
358 #define target_is_async_p() (linux_event_pipe[0] != -1)
360 static void send_sigstop (struct lwp_info
*lwp
);
362 /* Return non-zero if HEADER is a 64-bit ELF file. */
365 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
367 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
368 && header
->e_ident
[EI_MAG1
] == ELFMAG1
369 && header
->e_ident
[EI_MAG2
] == ELFMAG2
370 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
372 *machine
= header
->e_machine
;
373 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
380 /* Return non-zero if FILE is a 64-bit ELF file,
381 zero if the file is not a 64-bit ELF file,
382 and -1 if the file is not accessible or doesn't exist. */
385 elf_64_file_p (const char *file
, unsigned int *machine
)
390 fd
= open (file
, O_RDONLY
);
394 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
401 return elf_64_header_p (&header
, machine
);
404 /* Accepts an integer PID; Returns true if the executable PID is
405 running is a 64-bit ELF file.. */
408 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
412 sprintf (file
, "/proc/%d/exe", pid
);
413 return elf_64_file_p (file
, machine
);
417 delete_lwp (struct lwp_info
*lwp
)
419 struct thread_info
*thr
= get_lwp_thread (lwp
);
422 debug_printf ("deleting %ld\n", lwpid_of (thr
));
426 if (the_low_target
.delete_thread
!= NULL
)
427 the_low_target
.delete_thread (lwp
->arch_private
);
429 gdb_assert (lwp
->arch_private
== NULL
);
434 /* Add a process to the common process list, and set its private
437 static struct process_info
*
438 linux_add_process (int pid
, int attached
)
440 struct process_info
*proc
;
442 proc
= add_process (pid
, attached
);
443 proc
->priv
= XCNEW (struct process_info_private
);
445 if (the_low_target
.new_process
!= NULL
)
446 proc
->priv
->arch_private
= the_low_target
.new_process ();
452 linux_process_target::arch_setup_thread (thread_info
*thread
)
454 struct thread_info
*saved_thread
;
456 saved_thread
= current_thread
;
457 current_thread
= thread
;
461 current_thread
= saved_thread
;
465 linux_process_target::handle_extended_wait (lwp_info
**orig_event_lwp
,
468 client_state
&cs
= get_client_state ();
469 struct lwp_info
*event_lwp
= *orig_event_lwp
;
470 int event
= linux_ptrace_get_extended_event (wstat
);
471 struct thread_info
*event_thr
= get_lwp_thread (event_lwp
);
472 struct lwp_info
*new_lwp
;
474 gdb_assert (event_lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
476 /* All extended events we currently use are mid-syscall. Only
477 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
478 you have to be using PTRACE_SEIZE to get that. */
479 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
481 if ((event
== PTRACE_EVENT_FORK
) || (event
== PTRACE_EVENT_VFORK
)
482 || (event
== PTRACE_EVENT_CLONE
))
485 unsigned long new_pid
;
488 /* Get the pid of the new lwp. */
489 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_thr
), (PTRACE_TYPE_ARG3
) 0,
492 /* If we haven't already seen the new PID stop, wait for it now. */
493 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
495 /* The new child has a pending SIGSTOP. We can't affect it until it
496 hits the SIGSTOP, but we're already attached. */
498 ret
= my_waitpid (new_pid
, &status
, __WALL
);
501 perror_with_name ("waiting for new child");
502 else if (ret
!= new_pid
)
503 warning ("wait returned unexpected PID %d", ret
);
504 else if (!WIFSTOPPED (status
))
505 warning ("wait returned unexpected status 0x%x", status
);
508 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
510 struct process_info
*parent_proc
;
511 struct process_info
*child_proc
;
512 struct lwp_info
*child_lwp
;
513 struct thread_info
*child_thr
;
514 struct target_desc
*tdesc
;
516 ptid
= ptid_t (new_pid
, new_pid
, 0);
520 debug_printf ("HEW: Got fork event from LWP %ld, "
522 ptid_of (event_thr
).lwp (),
526 /* Add the new process to the tables and clone the breakpoint
527 lists of the parent. We need to do this even if the new process
528 will be detached, since we will need the process object and the
529 breakpoints to remove any breakpoints from memory when we
530 detach, and the client side will access registers. */
531 child_proc
= linux_add_process (new_pid
, 0);
532 gdb_assert (child_proc
!= NULL
);
533 child_lwp
= add_lwp (ptid
);
534 gdb_assert (child_lwp
!= NULL
);
535 child_lwp
->stopped
= 1;
536 child_lwp
->must_set_ptrace_flags
= 1;
537 child_lwp
->status_pending_p
= 0;
538 child_thr
= get_lwp_thread (child_lwp
);
539 child_thr
->last_resume_kind
= resume_stop
;
540 child_thr
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
542 /* If we're suspending all threads, leave this one suspended
543 too. If the fork/clone parent is stepping over a breakpoint,
544 all other threads have been suspended already. Leave the
545 child suspended too. */
546 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
547 || event_lwp
->bp_reinsert
!= 0)
550 debug_printf ("HEW: leaving child suspended\n");
551 child_lwp
->suspended
= 1;
554 parent_proc
= get_thread_process (event_thr
);
555 child_proc
->attached
= parent_proc
->attached
;
557 if (event_lwp
->bp_reinsert
!= 0
558 && supports_software_single_step ()
559 && event
== PTRACE_EVENT_VFORK
)
561 /* If we leave single-step breakpoints there, child will
562 hit it, so uninsert single-step breakpoints from parent
563 (and child). Once vfork child is done, reinsert
564 them back to parent. */
565 uninsert_single_step_breakpoints (event_thr
);
568 clone_all_breakpoints (child_thr
, event_thr
);
570 tdesc
= allocate_target_description ();
571 copy_target_description (tdesc
, parent_proc
->tdesc
);
572 child_proc
->tdesc
= tdesc
;
574 /* Clone arch-specific process data. */
575 if (the_low_target
.new_fork
!= NULL
)
576 the_low_target
.new_fork (parent_proc
, child_proc
);
578 /* Save fork info in the parent thread. */
579 if (event
== PTRACE_EVENT_FORK
)
580 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_FORKED
;
581 else if (event
== PTRACE_EVENT_VFORK
)
582 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORKED
;
584 event_lwp
->waitstatus
.value
.related_pid
= ptid
;
586 /* The status_pending field contains bits denoting the
587 extended event, so when the pending event is handled,
588 the handler will look at lwp->waitstatus. */
589 event_lwp
->status_pending_p
= 1;
590 event_lwp
->status_pending
= wstat
;
592 /* Link the threads until the parent event is passed on to
594 event_lwp
->fork_relative
= child_lwp
;
595 child_lwp
->fork_relative
= event_lwp
;
597 /* If the parent thread is doing step-over with single-step
598 breakpoints, the list of single-step breakpoints are cloned
599 from the parent's. Remove them from the child process.
600 In case of vfork, we'll reinsert them back once vforked
602 if (event_lwp
->bp_reinsert
!= 0
603 && supports_software_single_step ())
605 /* The child process is forked and stopped, so it is safe
606 to access its memory without stopping all other threads
607 from other processes. */
608 delete_single_step_breakpoints (child_thr
);
610 gdb_assert (has_single_step_breakpoints (event_thr
));
611 gdb_assert (!has_single_step_breakpoints (child_thr
));
614 /* Report the event. */
619 debug_printf ("HEW: Got clone event "
620 "from LWP %ld, new child is LWP %ld\n",
621 lwpid_of (event_thr
), new_pid
);
623 ptid
= ptid_t (pid_of (event_thr
), new_pid
, 0);
624 new_lwp
= add_lwp (ptid
);
626 /* Either we're going to immediately resume the new thread
627 or leave it stopped. resume_one_lwp is a nop if it
628 thinks the thread is currently running, so set this first
629 before calling resume_one_lwp. */
630 new_lwp
->stopped
= 1;
632 /* If we're suspending all threads, leave this one suspended
633 too. If the fork/clone parent is stepping over a breakpoint,
634 all other threads have been suspended already. Leave the
635 child suspended too. */
636 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
637 || event_lwp
->bp_reinsert
!= 0)
638 new_lwp
->suspended
= 1;
640 /* Normally we will get the pending SIGSTOP. But in some cases
641 we might get another signal delivered to the group first.
642 If we do get another signal, be sure not to lose it. */
643 if (WSTOPSIG (status
) != SIGSTOP
)
645 new_lwp
->stop_expected
= 1;
646 new_lwp
->status_pending_p
= 1;
647 new_lwp
->status_pending
= status
;
649 else if (cs
.report_thread_events
)
651 new_lwp
->waitstatus
.kind
= TARGET_WAITKIND_THREAD_CREATED
;
652 new_lwp
->status_pending_p
= 1;
653 new_lwp
->status_pending
= status
;
657 thread_db_notice_clone (event_thr
, ptid
);
660 /* Don't report the event. */
663 else if (event
== PTRACE_EVENT_VFORK_DONE
)
665 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
667 if (event_lwp
->bp_reinsert
!= 0 && supports_software_single_step ())
669 reinsert_single_step_breakpoints (event_thr
);
671 gdb_assert (has_single_step_breakpoints (event_thr
));
674 /* Report the event. */
677 else if (event
== PTRACE_EVENT_EXEC
&& cs
.report_exec_events
)
679 struct process_info
*proc
;
680 std::vector
<int> syscalls_to_catch
;
686 debug_printf ("HEW: Got exec event from LWP %ld\n",
687 lwpid_of (event_thr
));
690 /* Get the event ptid. */
691 event_ptid
= ptid_of (event_thr
);
692 event_pid
= event_ptid
.pid ();
694 /* Save the syscall list from the execing process. */
695 proc
= get_thread_process (event_thr
);
696 syscalls_to_catch
= std::move (proc
->syscalls_to_catch
);
698 /* Delete the execing process and all its threads. */
700 current_thread
= NULL
;
702 /* Create a new process/lwp/thread. */
703 proc
= linux_add_process (event_pid
, 0);
704 event_lwp
= add_lwp (event_ptid
);
705 event_thr
= get_lwp_thread (event_lwp
);
706 gdb_assert (current_thread
== event_thr
);
707 arch_setup_thread (event_thr
);
709 /* Set the event status. */
710 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXECD
;
711 event_lwp
->waitstatus
.value
.execd_pathname
712 = xstrdup (linux_proc_pid_to_exec_file (lwpid_of (event_thr
)));
714 /* Mark the exec status as pending. */
715 event_lwp
->stopped
= 1;
716 event_lwp
->status_pending_p
= 1;
717 event_lwp
->status_pending
= wstat
;
718 event_thr
->last_resume_kind
= resume_continue
;
719 event_thr
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
721 /* Update syscall state in the new lwp, effectively mid-syscall too. */
722 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
724 /* Restore the list to catch. Don't rely on the client, which is free
725 to avoid sending a new list when the architecture doesn't change.
726 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
727 proc
->syscalls_to_catch
= std::move (syscalls_to_catch
);
729 /* Report the event. */
730 *orig_event_lwp
= event_lwp
;
734 internal_error (__FILE__
, __LINE__
, _("unknown ptrace event %d"), event
);
738 linux_process_target::get_pc (lwp_info
*lwp
)
740 struct thread_info
*saved_thread
;
741 struct regcache
*regcache
;
744 if (!low_supports_breakpoints ())
747 saved_thread
= current_thread
;
748 current_thread
= get_lwp_thread (lwp
);
750 regcache
= get_thread_regcache (current_thread
, 1);
751 pc
= low_get_pc (regcache
);
754 debug_printf ("pc is 0x%lx\n", (long) pc
);
756 current_thread
= saved_thread
;
760 /* This function should only be called if LWP got a SYSCALL_SIGTRAP.
761 Fill *SYSNO with the syscall nr trapped. */
764 get_syscall_trapinfo (struct lwp_info
*lwp
, int *sysno
)
766 struct thread_info
*saved_thread
;
767 struct regcache
*regcache
;
769 if (the_low_target
.get_syscall_trapinfo
== NULL
)
771 /* If we cannot get the syscall trapinfo, report an unknown
772 system call number. */
773 *sysno
= UNKNOWN_SYSCALL
;
777 saved_thread
= current_thread
;
778 current_thread
= get_lwp_thread (lwp
);
780 regcache
= get_thread_regcache (current_thread
, 1);
781 (*the_low_target
.get_syscall_trapinfo
) (regcache
, sysno
);
784 debug_printf ("get_syscall_trapinfo sysno %d\n", *sysno
);
786 current_thread
= saved_thread
;
790 linux_process_target::save_stop_reason (lwp_info
*lwp
)
793 CORE_ADDR sw_breakpoint_pc
;
794 struct thread_info
*saved_thread
;
795 #if USE_SIGTRAP_SIGINFO
799 if (!low_supports_breakpoints ())
803 sw_breakpoint_pc
= pc
- low_decr_pc_after_break ();
805 /* breakpoint_at reads from the current thread. */
806 saved_thread
= current_thread
;
807 current_thread
= get_lwp_thread (lwp
);
809 #if USE_SIGTRAP_SIGINFO
810 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
811 (PTRACE_TYPE_ARG3
) 0, &siginfo
) == 0)
813 if (siginfo
.si_signo
== SIGTRAP
)
815 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
)
816 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
818 /* The si_code is ambiguous on this arch -- check debug
820 if (!check_stopped_by_watchpoint (lwp
))
821 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
823 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
))
825 /* If we determine the LWP stopped for a SW breakpoint,
826 trust it. Particularly don't check watchpoint
827 registers, because at least on s390, we'd find
828 stopped-by-watchpoint as long as there's a watchpoint
830 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
832 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
834 /* This can indicate either a hardware breakpoint or
835 hardware watchpoint. Check debug registers. */
836 if (!check_stopped_by_watchpoint (lwp
))
837 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
839 else if (siginfo
.si_code
== TRAP_TRACE
)
841 /* We may have single stepped an instruction that
842 triggered a watchpoint. In that case, on some
843 architectures (such as x86), instead of TRAP_HWBKPT,
844 si_code indicates TRAP_TRACE, and we need to check
845 the debug registers separately. */
846 if (!check_stopped_by_watchpoint (lwp
))
847 lwp
->stop_reason
= TARGET_STOPPED_BY_SINGLE_STEP
;
852 /* We may have just stepped a breakpoint instruction. E.g., in
853 non-stop mode, GDB first tells the thread A to step a range, and
854 then the user inserts a breakpoint inside the range. In that
855 case we need to report the breakpoint PC. */
856 if ((!lwp
->stepping
|| lwp
->stop_pc
== sw_breakpoint_pc
)
857 && low_breakpoint_at (sw_breakpoint_pc
))
858 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
860 if (hardware_breakpoint_inserted_here (pc
))
861 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
863 if (lwp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
)
864 check_stopped_by_watchpoint (lwp
);
867 if (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
)
871 struct thread_info
*thr
= get_lwp_thread (lwp
);
873 debug_printf ("CSBB: %s stopped by software breakpoint\n",
874 target_pid_to_str (ptid_of (thr
)));
877 /* Back up the PC if necessary. */
878 if (pc
!= sw_breakpoint_pc
)
880 struct regcache
*regcache
881 = get_thread_regcache (current_thread
, 1);
882 low_set_pc (regcache
, sw_breakpoint_pc
);
885 /* Update this so we record the correct stop PC below. */
886 pc
= sw_breakpoint_pc
;
888 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
892 struct thread_info
*thr
= get_lwp_thread (lwp
);
894 debug_printf ("CSBB: %s stopped by hardware breakpoint\n",
895 target_pid_to_str (ptid_of (thr
)));
898 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
902 struct thread_info
*thr
= get_lwp_thread (lwp
);
904 debug_printf ("CSBB: %s stopped by hardware watchpoint\n",
905 target_pid_to_str (ptid_of (thr
)));
908 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
)
912 struct thread_info
*thr
= get_lwp_thread (lwp
);
914 debug_printf ("CSBB: %s stopped by trace\n",
915 target_pid_to_str (ptid_of (thr
)));
920 current_thread
= saved_thread
;
924 static struct lwp_info
*
925 add_lwp (ptid_t ptid
)
927 struct lwp_info
*lwp
;
929 lwp
= XCNEW (struct lwp_info
);
931 lwp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
933 lwp
->thread
= add_thread (ptid
, lwp
);
935 if (the_low_target
.new_thread
!= NULL
)
936 the_low_target
.new_thread (lwp
);
941 /* Callback to be used when calling fork_inferior, responsible for
942 actually initiating the tracing of the inferior. */
947 if (ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0,
948 (PTRACE_TYPE_ARG4
) 0) < 0)
949 trace_start_error_with_name ("ptrace");
951 if (setpgid (0, 0) < 0)
952 trace_start_error_with_name ("setpgid");
954 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
955 stdout to stderr so that inferior i/o doesn't corrupt the connection.
956 Also, redirect stdin to /dev/null. */
957 if (remote_connection_is_stdio ())
960 trace_start_error_with_name ("close");
961 if (open ("/dev/null", O_RDONLY
) < 0)
962 trace_start_error_with_name ("open");
964 trace_start_error_with_name ("dup2");
965 if (write (2, "stdin/stdout redirected\n",
966 sizeof ("stdin/stdout redirected\n") - 1) < 0)
968 /* Errors ignored. */;
973 /* Start an inferior process and returns its pid.
974 PROGRAM is the name of the program to be started, and PROGRAM_ARGS
975 are its arguments. */
978 linux_process_target::create_inferior (const char *program
,
979 const std::vector
<char *> &program_args
)
981 client_state
&cs
= get_client_state ();
982 struct lwp_info
*new_lwp
;
987 maybe_disable_address_space_randomization restore_personality
988 (cs
.disable_randomization
);
989 std::string str_program_args
= stringify_argv (program_args
);
991 pid
= fork_inferior (program
,
992 str_program_args
.c_str (),
993 get_environ ()->envp (), linux_ptrace_fun
,
994 NULL
, NULL
, NULL
, NULL
);
997 linux_add_process (pid
, 0);
999 ptid
= ptid_t (pid
, pid
, 0);
1000 new_lwp
= add_lwp (ptid
);
1001 new_lwp
->must_set_ptrace_flags
= 1;
1003 post_fork_inferior (pid
, program
);
1008 /* Implement the post_create_inferior target_ops method. */
1011 linux_process_target::post_create_inferior ()
1013 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
1017 if (lwp
->must_set_ptrace_flags
)
1019 struct process_info
*proc
= current_process ();
1020 int options
= linux_low_ptrace_options (proc
->attached
);
1022 linux_enable_event_reporting (lwpid_of (current_thread
), options
);
1023 lwp
->must_set_ptrace_flags
= 0;
1027 /* Attach to an inferior process. Returns 0 on success, ERRNO on
1031 linux_attach_lwp (ptid_t ptid
)
1033 struct lwp_info
*new_lwp
;
1034 int lwpid
= ptid
.lwp ();
1036 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0)
1040 new_lwp
= add_lwp (ptid
);
1042 /* We need to wait for SIGSTOP before being able to make the next
1043 ptrace call on this LWP. */
1044 new_lwp
->must_set_ptrace_flags
= 1;
1046 if (linux_proc_pid_is_stopped (lwpid
))
1049 debug_printf ("Attached to a stopped process\n");
1051 /* The process is definitely stopped. It is in a job control
1052 stop, unless the kernel predates the TASK_STOPPED /
1053 TASK_TRACED distinction, in which case it might be in a
1054 ptrace stop. Make sure it is in a ptrace stop; from there we
1055 can kill it, signal it, et cetera.
1057 First make sure there is a pending SIGSTOP. Since we are
1058 already attached, the process can not transition from stopped
1059 to running without a PTRACE_CONT; so we know this signal will
1060 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1061 probably already in the queue (unless this kernel is old
1062 enough to use TASK_STOPPED for ptrace stops); but since
1063 SIGSTOP is not an RT signal, it can only be queued once. */
1064 kill_lwp (lwpid
, SIGSTOP
);
1066 /* Finally, resume the stopped process. This will deliver the
1067 SIGSTOP (or a higher priority signal, just like normal
1068 PTRACE_ATTACH), which we'll catch later on. */
1069 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1072 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1073 brings it to a halt.
1075 There are several cases to consider here:
1077 1) gdbserver has already attached to the process and is being notified
1078 of a new thread that is being created.
1079 In this case we should ignore that SIGSTOP and resume the
1080 process. This is handled below by setting stop_expected = 1,
1081 and the fact that add_thread sets last_resume_kind ==
1084 2) This is the first thread (the process thread), and we're attaching
1085 to it via attach_inferior.
1086 In this case we want the process thread to stop.
1087 This is handled by having linux_attach set last_resume_kind ==
1088 resume_stop after we return.
1090 If the pid we are attaching to is also the tgid, we attach to and
1091 stop all the existing threads. Otherwise, we attach to pid and
1092 ignore any other threads in the same group as this pid.
1094 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1096 In this case we want the thread to stop.
1097 FIXME: This case is currently not properly handled.
1098 We should wait for the SIGSTOP but don't. Things work apparently
1099 because enough time passes between when we ptrace (ATTACH) and when
1100 gdb makes the next ptrace call on the thread.
1102 On the other hand, if we are currently trying to stop all threads, we
1103 should treat the new thread as if we had sent it a SIGSTOP. This works
1104 because we are guaranteed that the add_lwp call above added us to the
1105 end of the list, and so the new thread has not yet reached
1106 wait_for_sigstop (but will). */
1107 new_lwp
->stop_expected
= 1;
1112 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1113 already attached. Returns true if a new LWP is found, false
1117 attach_proc_task_lwp_callback (ptid_t ptid
)
1119 /* Is this a new thread? */
1120 if (find_thread_ptid (ptid
) == NULL
)
1122 int lwpid
= ptid
.lwp ();
1126 debug_printf ("Found new lwp %d\n", lwpid
);
1128 err
= linux_attach_lwp (ptid
);
1130 /* Be quiet if we simply raced with the thread exiting. EPERM
1131 is returned if the thread's task still exists, and is marked
1132 as exited or zombie, as well as other conditions, so in that
1133 case, confirm the status in /proc/PID/status. */
1135 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1139 debug_printf ("Cannot attach to lwp %d: "
1140 "thread is gone (%d: %s)\n",
1141 lwpid
, err
, safe_strerror (err
));
1147 = linux_ptrace_attach_fail_reason_string (ptid
, err
);
1149 warning (_("Cannot attach to lwp %d: %s"), lwpid
, reason
.c_str ());
1157 static void async_file_mark (void);
1159 /* Attach to PID. If PID is the tgid, attach to it and all
1163 linux_process_target::attach (unsigned long pid
)
1165 struct process_info
*proc
;
1166 struct thread_info
*initial_thread
;
1167 ptid_t ptid
= ptid_t (pid
, pid
, 0);
1170 proc
= linux_add_process (pid
, 1);
1172 /* Attach to PID. We will check for other threads
1174 err
= linux_attach_lwp (ptid
);
1177 remove_process (proc
);
1179 std::string reason
= linux_ptrace_attach_fail_reason_string (ptid
, err
);
1180 error ("Cannot attach to process %ld: %s", pid
, reason
.c_str ());
1183 /* Don't ignore the initial SIGSTOP if we just attached to this
1184 process. It will be collected by wait shortly. */
1185 initial_thread
= find_thread_ptid (ptid_t (pid
, pid
, 0));
1186 initial_thread
->last_resume_kind
= resume_stop
;
1188 /* We must attach to every LWP. If /proc is mounted, use that to
1189 find them now. On the one hand, the inferior may be using raw
1190 clone instead of using pthreads. On the other hand, even if it
1191 is using pthreads, GDB may not be connected yet (thread_db needs
1192 to do symbol lookups, through qSymbol). Also, thread_db walks
1193 structures in the inferior's address space to find the list of
1194 threads/LWPs, and those structures may well be corrupted. Note
1195 that once thread_db is loaded, we'll still use it to list threads
1196 and associate pthread info with each LWP. */
1197 linux_proc_attach_tgid_threads (pid
, attach_proc_task_lwp_callback
);
1199 /* GDB will shortly read the xml target description for this
1200 process, to figure out the process' architecture. But the target
1201 description is only filled in when the first process/thread in
1202 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1203 that now, otherwise, if GDB is fast enough, it could read the
1204 target description _before_ that initial stop. */
1207 struct lwp_info
*lwp
;
1209 ptid_t pid_ptid
= ptid_t (pid
);
1211 lwpid
= wait_for_event_filtered (pid_ptid
, pid_ptid
, &wstat
, __WALL
);
1212 gdb_assert (lwpid
> 0);
1214 lwp
= find_lwp_pid (ptid_t (lwpid
));
1216 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGSTOP
)
1218 lwp
->status_pending_p
= 1;
1219 lwp
->status_pending
= wstat
;
1222 initial_thread
->last_resume_kind
= resume_continue
;
1226 gdb_assert (proc
->tdesc
!= NULL
);
1233 last_thread_of_process_p (int pid
)
1235 bool seen_one
= false;
1237 thread_info
*thread
= find_thread (pid
, [&] (thread_info
*thr_arg
)
1241 /* This is the first thread of this process we see. */
1247 /* This is the second thread of this process we see. */
1252 return thread
== NULL
;
1258 linux_kill_one_lwp (struct lwp_info
*lwp
)
1260 struct thread_info
*thr
= get_lwp_thread (lwp
);
1261 int pid
= lwpid_of (thr
);
1263 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1264 there is no signal context, and ptrace(PTRACE_KILL) (or
1265 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1266 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1267 alternative is to kill with SIGKILL. We only need one SIGKILL
1268 per process, not one for each thread. But since we still support
1269 support debugging programs using raw clone without CLONE_THREAD,
1270 we send one for each thread. For years, we used PTRACE_KILL
1271 only, so we're being a bit paranoid about some old kernels where
1272 PTRACE_KILL might work better (dubious if there are any such, but
1273 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1274 second, and so we're fine everywhere. */
1277 kill_lwp (pid
, SIGKILL
);
1280 int save_errno
= errno
;
1282 debug_printf ("LKL: kill_lwp (SIGKILL) %s, 0, 0 (%s)\n",
1283 target_pid_to_str (ptid_of (thr
)),
1284 save_errno
? safe_strerror (save_errno
) : "OK");
1288 ptrace (PTRACE_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1291 int save_errno
= errno
;
1293 debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
1294 target_pid_to_str (ptid_of (thr
)),
1295 save_errno
? safe_strerror (save_errno
) : "OK");
1299 /* Kill LWP and wait for it to die. */
1302 kill_wait_lwp (struct lwp_info
*lwp
)
1304 struct thread_info
*thr
= get_lwp_thread (lwp
);
1305 int pid
= ptid_of (thr
).pid ();
1306 int lwpid
= ptid_of (thr
).lwp ();
1311 debug_printf ("kwl: killing lwp %d, for pid: %d\n", lwpid
, pid
);
1315 linux_kill_one_lwp (lwp
);
1317 /* Make sure it died. Notes:
1319 - The loop is most likely unnecessary.
1321 - We don't use wait_for_event as that could delete lwps
1322 while we're iterating over them. We're not interested in
1323 any pending status at this point, only in making sure all
1324 wait status on the kernel side are collected until the
1327 - We don't use __WALL here as the __WALL emulation relies on
1328 SIGCHLD, and killing a stopped process doesn't generate
1329 one, nor an exit status.
1331 res
= my_waitpid (lwpid
, &wstat
, 0);
1332 if (res
== -1 && errno
== ECHILD
)
1333 res
= my_waitpid (lwpid
, &wstat
, __WCLONE
);
1334 } while (res
> 0 && WIFSTOPPED (wstat
));
1336 /* Even if it was stopped, the child may have already disappeared.
1337 E.g., if it was killed by SIGKILL. */
1338 if (res
< 0 && errno
!= ECHILD
)
1339 perror_with_name ("kill_wait_lwp");
1342 /* Callback for `for_each_thread'. Kills an lwp of a given process,
1343 except the leader. */
1346 kill_one_lwp_callback (thread_info
*thread
, int pid
)
1348 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1350 /* We avoid killing the first thread here, because of a Linux kernel (at
1351 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1352 the children get a chance to be reaped, it will remain a zombie
1355 if (lwpid_of (thread
) == pid
)
1358 debug_printf ("lkop: is last of process %s\n",
1359 target_pid_to_str (thread
->id
));
1363 kill_wait_lwp (lwp
);
1367 linux_process_target::kill (process_info
*process
)
1369 int pid
= process
->pid
;
1371 /* If we're killing a running inferior, make sure it is stopped
1372 first, as PTRACE_KILL will not work otherwise. */
1373 stop_all_lwps (0, NULL
);
1375 for_each_thread (pid
, [&] (thread_info
*thread
)
1377 kill_one_lwp_callback (thread
, pid
);
1380 /* See the comment in linux_kill_one_lwp. We did not kill the first
1381 thread in the list, so do so now. */
1382 lwp_info
*lwp
= find_lwp_pid (ptid_t (pid
));
1387 debug_printf ("lk_1: cannot find lwp for pid: %d\n",
1391 kill_wait_lwp (lwp
);
1395 /* Since we presently can only stop all lwps of all processes, we
1396 need to unstop lwps of other processes. */
1397 unstop_all_lwps (0, NULL
);
1401 /* Get pending signal of THREAD, for detaching purposes. This is the
1402 signal the thread last stopped for, which we need to deliver to the
1403 thread when detaching, otherwise, it'd be suppressed/lost. */
1406 get_detach_signal (struct thread_info
*thread
)
1408 client_state
&cs
= get_client_state ();
1409 enum gdb_signal signo
= GDB_SIGNAL_0
;
1411 struct lwp_info
*lp
= get_thread_lwp (thread
);
1413 if (lp
->status_pending_p
)
1414 status
= lp
->status_pending
;
1417 /* If the thread had been suspended by gdbserver, and it stopped
1418 cleanly, then it'll have stopped with SIGSTOP. But we don't
1419 want to deliver that SIGSTOP. */
1420 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1421 || thread
->last_status
.value
.sig
== GDB_SIGNAL_0
)
1424 /* Otherwise, we may need to deliver the signal we
1426 status
= lp
->last_status
;
1429 if (!WIFSTOPPED (status
))
1432 debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1433 target_pid_to_str (ptid_of (thread
)));
1437 /* Extended wait statuses aren't real SIGTRAPs. */
1438 if (WSTOPSIG (status
) == SIGTRAP
&& linux_is_extended_waitstatus (status
))
1441 debug_printf ("GPS: lwp %s had stopped with extended "
1442 "status: no pending signal\n",
1443 target_pid_to_str (ptid_of (thread
)));
1447 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1449 if (cs
.program_signals_p
&& !cs
.program_signals
[signo
])
1452 debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1453 target_pid_to_str (ptid_of (thread
)),
1454 gdb_signal_to_string (signo
));
1457 else if (!cs
.program_signals_p
1458 /* If we have no way to know which signals GDB does not
1459 want to have passed to the program, assume
1460 SIGTRAP/SIGINT, which is GDB's default. */
1461 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1464 debug_printf ("GPS: lwp %s had signal %s, "
1465 "but we don't know if we should pass it. "
1466 "Default to not.\n",
1467 target_pid_to_str (ptid_of (thread
)),
1468 gdb_signal_to_string (signo
));
1474 debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1475 target_pid_to_str (ptid_of (thread
)),
1476 gdb_signal_to_string (signo
));
1478 return WSTOPSIG (status
);
1482 /* Detach from LWP. */
1485 linux_detach_one_lwp (struct lwp_info
*lwp
)
1487 struct thread_info
*thread
= get_lwp_thread (lwp
);
1491 /* If there is a pending SIGSTOP, get rid of it. */
1492 if (lwp
->stop_expected
)
1495 debug_printf ("Sending SIGCONT to %s\n",
1496 target_pid_to_str (ptid_of (thread
)));
1498 kill_lwp (lwpid_of (thread
), SIGCONT
);
1499 lwp
->stop_expected
= 0;
1502 /* Pass on any pending signal for this thread. */
1503 sig
= get_detach_signal (thread
);
1505 /* Preparing to resume may try to write registers, and fail if the
1506 lwp is zombie. If that happens, ignore the error. We'll handle
1507 it below, when detach fails with ESRCH. */
1510 /* Flush any pending changes to the process's registers. */
1511 regcache_invalidate_thread (thread
);
1513 /* Finally, let it resume. */
1514 if (the_low_target
.prepare_to_resume
!= NULL
)
1515 the_low_target
.prepare_to_resume (lwp
);
1517 catch (const gdb_exception_error
&ex
)
1519 if (!check_ptrace_stopped_lwp_gone (lwp
))
1523 lwpid
= lwpid_of (thread
);
1524 if (ptrace (PTRACE_DETACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0,
1525 (PTRACE_TYPE_ARG4
) (long) sig
) < 0)
1527 int save_errno
= errno
;
1529 /* We know the thread exists, so ESRCH must mean the lwp is
1530 zombie. This can happen if one of the already-detached
1531 threads exits the whole thread group. In that case we're
1532 still attached, and must reap the lwp. */
1533 if (save_errno
== ESRCH
)
1537 ret
= my_waitpid (lwpid
, &status
, __WALL
);
1540 warning (_("Couldn't reap LWP %d while detaching: %s"),
1541 lwpid
, safe_strerror (errno
));
1543 else if (!WIFEXITED (status
) && !WIFSIGNALED (status
))
1545 warning (_("Reaping LWP %d while detaching "
1546 "returned unexpected status 0x%x"),
1552 error (_("Can't detach %s: %s"),
1553 target_pid_to_str (ptid_of (thread
)),
1554 safe_strerror (save_errno
));
1557 else if (debug_threads
)
1559 debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)\n",
1560 target_pid_to_str (ptid_of (thread
)),
1567 /* Callback for for_each_thread. Detaches from non-leader threads of a
1571 linux_detach_lwp_callback (thread_info
*thread
)
1573 /* We don't actually detach from the thread group leader just yet.
1574 If the thread group exits, we must reap the zombie clone lwps
1575 before we're able to reap the leader. */
1576 if (thread
->id
.pid () == thread
->id
.lwp ())
1579 lwp_info
*lwp
= get_thread_lwp (thread
);
1580 linux_detach_one_lwp (lwp
);
1584 linux_process_target::detach (process_info
*process
)
1586 struct lwp_info
*main_lwp
;
1588 /* As there's a step over already in progress, let it finish first,
1589 otherwise nesting a stabilize_threads operation on top gets real
1591 complete_ongoing_step_over ();
1593 /* Stop all threads before detaching. First, ptrace requires that
1594 the thread is stopped to successfully detach. Second, thread_db
1595 may need to uninstall thread event breakpoints from memory, which
1596 only works with a stopped process anyway. */
1597 stop_all_lwps (0, NULL
);
1599 #ifdef USE_THREAD_DB
1600 thread_db_detach (process
);
1603 /* Stabilize threads (move out of jump pads). */
1604 target_stabilize_threads ();
1606 /* Detach from the clone lwps first. If the thread group exits just
1607 while we're detaching, we must reap the clone lwps before we're
1608 able to reap the leader. */
1609 for_each_thread (process
->pid
, linux_detach_lwp_callback
);
1611 main_lwp
= find_lwp_pid (ptid_t (process
->pid
));
1612 linux_detach_one_lwp (main_lwp
);
1616 /* Since we presently can only stop all lwps of all processes, we
1617 need to unstop lwps of other processes. */
1618 unstop_all_lwps (0, NULL
);
1622 /* Remove all LWPs that belong to process PROC from the lwp list. */
1625 linux_process_target::mourn (process_info
*process
)
1627 struct process_info_private
*priv
;
1629 #ifdef USE_THREAD_DB
1630 thread_db_mourn (process
);
1633 for_each_thread (process
->pid
, [] (thread_info
*thread
)
1635 delete_lwp (get_thread_lwp (thread
));
1638 /* Freeing all private data. */
1639 priv
= process
->priv
;
1640 if (the_low_target
.delete_process
!= NULL
)
1641 the_low_target
.delete_process (priv
->arch_private
);
1643 gdb_assert (priv
->arch_private
== NULL
);
1645 process
->priv
= NULL
;
1647 remove_process (process
);
1651 linux_process_target::join (int pid
)
1656 ret
= my_waitpid (pid
, &status
, 0);
1657 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1659 } while (ret
!= -1 || errno
!= ECHILD
);
1662 /* Return true if the given thread is still alive. */
1665 linux_process_target::thread_alive (ptid_t ptid
)
1667 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1669 /* We assume we always know if a thread exits. If a whole process
1670 exited but we still haven't been able to report it to GDB, we'll
1671 hold on to the last lwp of the dead process. */
1673 return !lwp_is_marked_dead (lwp
);
1679 linux_process_target::thread_still_has_status_pending (thread_info
*thread
)
1681 struct lwp_info
*lp
= get_thread_lwp (thread
);
1683 if (!lp
->status_pending_p
)
1686 if (thread
->last_resume_kind
!= resume_stop
1687 && (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1688 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
))
1690 struct thread_info
*saved_thread
;
1694 gdb_assert (lp
->last_status
!= 0);
1698 saved_thread
= current_thread
;
1699 current_thread
= thread
;
1701 if (pc
!= lp
->stop_pc
)
1704 debug_printf ("PC of %ld changed\n",
1709 #if !USE_SIGTRAP_SIGINFO
1710 else if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1711 && !low_breakpoint_at (pc
))
1714 debug_printf ("previous SW breakpoint of %ld gone\n",
1718 else if (lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
1719 && !hardware_breakpoint_inserted_here (pc
))
1722 debug_printf ("previous HW breakpoint of %ld gone\n",
1728 current_thread
= saved_thread
;
1733 debug_printf ("discarding pending breakpoint status\n");
1734 lp
->status_pending_p
= 0;
1742 /* Returns true if LWP is resumed from the client's perspective. */
1745 lwp_resumed (struct lwp_info
*lwp
)
1747 struct thread_info
*thread
= get_lwp_thread (lwp
);
1749 if (thread
->last_resume_kind
!= resume_stop
)
1752 /* Did gdb send us a `vCont;t', but we haven't reported the
1753 corresponding stop to gdb yet? If so, the thread is still
1754 resumed/running from gdb's perspective. */
1755 if (thread
->last_resume_kind
== resume_stop
1756 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
1763 linux_process_target::status_pending_p_callback (thread_info
*thread
,
1766 struct lwp_info
*lp
= get_thread_lwp (thread
);
1768 /* Check if we're only interested in events from a specific process
1769 or a specific LWP. */
1770 if (!thread
->id
.matches (ptid
))
1773 if (!lwp_resumed (lp
))
1776 if (lp
->status_pending_p
1777 && !thread_still_has_status_pending (thread
))
1779 resume_one_lwp (lp
, lp
->stepping
, GDB_SIGNAL_0
, NULL
);
1783 return lp
->status_pending_p
;
1787 find_lwp_pid (ptid_t ptid
)
1789 thread_info
*thread
= find_thread ([&] (thread_info
*thr_arg
)
1791 int lwp
= ptid
.lwp () != 0 ? ptid
.lwp () : ptid
.pid ();
1792 return thr_arg
->id
.lwp () == lwp
;
1798 return get_thread_lwp (thread
);
1801 /* Return the number of known LWPs in the tgid given by PID. */
1808 for_each_thread (pid
, [&] (thread_info
*thread
)
1816 /* See nat/linux-nat.h. */
1819 iterate_over_lwps (ptid_t filter
,
1820 gdb::function_view
<iterate_over_lwps_ftype
> callback
)
1822 thread_info
*thread
= find_thread (filter
, [&] (thread_info
*thr_arg
)
1824 lwp_info
*lwp
= get_thread_lwp (thr_arg
);
1826 return callback (lwp
);
1832 return get_thread_lwp (thread
);
1835 /* Detect zombie thread group leaders, and "exit" them. We can't reap
1836 their exits until all other threads in the group have exited. */
1839 check_zombie_leaders (void)
1841 for_each_process ([] (process_info
*proc
) {
1842 pid_t leader_pid
= pid_of (proc
);
1843 struct lwp_info
*leader_lp
;
1845 leader_lp
= find_lwp_pid (ptid_t (leader_pid
));
1848 debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1849 "num_lwps=%d, zombie=%d\n",
1850 leader_pid
, leader_lp
!= NULL
, num_lwps (leader_pid
),
1851 linux_proc_pid_is_zombie (leader_pid
));
1853 if (leader_lp
!= NULL
&& !leader_lp
->stopped
1854 /* Check if there are other threads in the group, as we may
1855 have raced with the inferior simply exiting. */
1856 && !last_thread_of_process_p (leader_pid
)
1857 && linux_proc_pid_is_zombie (leader_pid
))
1859 /* A leader zombie can mean one of two things:
1861 - It exited, and there's an exit status pending
1862 available, or only the leader exited (not the whole
1863 program). In the latter case, we can't waitpid the
1864 leader's exit status until all other threads are gone.
1866 - There are 3 or more threads in the group, and a thread
1867 other than the leader exec'd. On an exec, the Linux
1868 kernel destroys all other threads (except the execing
1869 one) in the thread group, and resets the execing thread's
1870 tid to the tgid. No exit notification is sent for the
1871 execing thread -- from the ptracer's perspective, it
1872 appears as though the execing thread just vanishes.
1873 Until we reap all other threads except the leader and the
1874 execing thread, the leader will be zombie, and the
1875 execing thread will be in `D (disc sleep)'. As soon as
1876 all other threads are reaped, the execing thread changes
1877 it's tid to the tgid, and the previous (zombie) leader
1878 vanishes, giving place to the "new" leader. We could try
1879 distinguishing the exit and exec cases, by waiting once
1880 more, and seeing if something comes out, but it doesn't
1881 sound useful. The previous leader _does_ go away, and
1882 we'll re-add the new one once we see the exec event
1883 (which is just the same as what would happen if the
1884 previous leader did exit voluntarily before some other
1888 debug_printf ("CZL: Thread group leader %d zombie "
1889 "(it exited, or another thread execd).\n",
1892 delete_lwp (leader_lp
);
1897 /* Callback for `find_thread'. Returns the first LWP that is not
1901 not_stopped_callback (thread_info
*thread
, ptid_t filter
)
1903 if (!thread
->id
.matches (filter
))
1906 lwp_info
*lwp
= get_thread_lwp (thread
);
1908 return !lwp
->stopped
;
1911 /* Increment LWP's suspend count. */
1914 lwp_suspended_inc (struct lwp_info
*lwp
)
1918 if (debug_threads
&& lwp
->suspended
> 4)
1920 struct thread_info
*thread
= get_lwp_thread (lwp
);
1922 debug_printf ("LWP %ld has a suspiciously high suspend count,"
1923 " suspended=%d\n", lwpid_of (thread
), lwp
->suspended
);
1927 /* Decrement LWP's suspend count. */
1930 lwp_suspended_decr (struct lwp_info
*lwp
)
1934 if (lwp
->suspended
< 0)
1936 struct thread_info
*thread
= get_lwp_thread (lwp
);
1938 internal_error (__FILE__
, __LINE__
,
1939 "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread
),
1944 /* This function should only be called if the LWP got a SIGTRAP.
1946 Handle any tracepoint steps or hits. Return true if a tracepoint
1947 event was handled, 0 otherwise. */
1950 handle_tracepoints (struct lwp_info
*lwp
)
1952 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1953 int tpoint_related_event
= 0;
1955 gdb_assert (lwp
->suspended
== 0);
1957 /* If this tracepoint hit causes a tracing stop, we'll immediately
1958 uninsert tracepoints. To do this, we temporarily pause all
1959 threads, unpatch away, and then unpause threads. We need to make
1960 sure the unpausing doesn't resume LWP too. */
1961 lwp_suspended_inc (lwp
);
1963 /* And we need to be sure that any all-threads-stopping doesn't try
1964 to move threads out of the jump pads, as it could deadlock the
1965 inferior (LWP could be in the jump pad, maybe even holding the
1968 /* Do any necessary step collect actions. */
1969 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1971 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1973 /* See if we just hit a tracepoint and do its main collect
1975 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1977 lwp_suspended_decr (lwp
);
1979 gdb_assert (lwp
->suspended
== 0);
1980 gdb_assert (!stabilizing_threads
1981 || (lwp
->collecting_fast_tracepoint
1982 != fast_tpoint_collect_result::not_collecting
));
1984 if (tpoint_related_event
)
1987 debug_printf ("got a tracepoint event\n");
1994 /* Convenience wrapper. Returns information about LWP's fast tracepoint
1995 collection status. */
1997 static fast_tpoint_collect_result
1998 linux_fast_tracepoint_collecting (struct lwp_info
*lwp
,
1999 struct fast_tpoint_collect_status
*status
)
2001 CORE_ADDR thread_area
;
2002 struct thread_info
*thread
= get_lwp_thread (lwp
);
2004 if (the_low_target
.get_thread_area
== NULL
)
2005 return fast_tpoint_collect_result::not_collecting
;
2007 /* Get the thread area address. This is used to recognize which
2008 thread is which when tracing with the in-process agent library.
2009 We don't read anything from the address, and treat it as opaque;
2010 it's the address itself that we assume is unique per-thread. */
2011 if ((*the_low_target
.get_thread_area
) (lwpid_of (thread
), &thread_area
) == -1)
2012 return fast_tpoint_collect_result::not_collecting
;
2014 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
2018 linux_process_target::maybe_move_out_of_jump_pad (lwp_info
*lwp
, int *wstat
)
2020 struct thread_info
*saved_thread
;
2022 saved_thread
= current_thread
;
2023 current_thread
= get_lwp_thread (lwp
);
2026 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
2027 && supports_fast_tracepoints ()
2028 && agent_loaded_p ())
2030 struct fast_tpoint_collect_status status
;
2033 debug_printf ("Checking whether LWP %ld needs to move out of the "
2035 lwpid_of (current_thread
));
2037 fast_tpoint_collect_result r
2038 = linux_fast_tracepoint_collecting (lwp
, &status
);
2041 || (WSTOPSIG (*wstat
) != SIGILL
2042 && WSTOPSIG (*wstat
) != SIGFPE
2043 && WSTOPSIG (*wstat
) != SIGSEGV
2044 && WSTOPSIG (*wstat
) != SIGBUS
))
2046 lwp
->collecting_fast_tracepoint
= r
;
2048 if (r
!= fast_tpoint_collect_result::not_collecting
)
2050 if (r
== fast_tpoint_collect_result::before_insn
2051 && lwp
->exit_jump_pad_bkpt
== NULL
)
2053 /* Haven't executed the original instruction yet.
2054 Set breakpoint there, and wait till it's hit,
2055 then single-step until exiting the jump pad. */
2056 lwp
->exit_jump_pad_bkpt
2057 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
2061 debug_printf ("Checking whether LWP %ld needs to move out of "
2062 "the jump pad...it does\n",
2063 lwpid_of (current_thread
));
2064 current_thread
= saved_thread
;
2071 /* If we get a synchronous signal while collecting, *and*
2072 while executing the (relocated) original instruction,
2073 reset the PC to point at the tpoint address, before
2074 reporting to GDB. Otherwise, it's an IPA lib bug: just
2075 report the signal to GDB, and pray for the best. */
2077 lwp
->collecting_fast_tracepoint
2078 = fast_tpoint_collect_result::not_collecting
;
2080 if (r
!= fast_tpoint_collect_result::not_collecting
2081 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
2082 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
2085 struct regcache
*regcache
;
2087 /* The si_addr on a few signals references the address
2088 of the faulting instruction. Adjust that as
2090 if ((WSTOPSIG (*wstat
) == SIGILL
2091 || WSTOPSIG (*wstat
) == SIGFPE
2092 || WSTOPSIG (*wstat
) == SIGBUS
2093 || WSTOPSIG (*wstat
) == SIGSEGV
)
2094 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
2095 (PTRACE_TYPE_ARG3
) 0, &info
) == 0
2096 /* Final check just to make sure we don't clobber
2097 the siginfo of non-kernel-sent signals. */
2098 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
2100 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
2101 ptrace (PTRACE_SETSIGINFO
, lwpid_of (current_thread
),
2102 (PTRACE_TYPE_ARG3
) 0, &info
);
2105 regcache
= get_thread_regcache (current_thread
, 1);
2106 low_set_pc (regcache
, status
.tpoint_addr
);
2107 lwp
->stop_pc
= status
.tpoint_addr
;
2109 /* Cancel any fast tracepoint lock this thread was
2111 force_unlock_trace_buffer ();
2114 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
2117 debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
2118 "stopping all threads momentarily.\n");
2120 stop_all_lwps (1, lwp
);
2122 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
2123 lwp
->exit_jump_pad_bkpt
= NULL
;
2125 unstop_all_lwps (1, lwp
);
2127 gdb_assert (lwp
->suspended
>= 0);
2133 debug_printf ("Checking whether LWP %ld needs to move out of the "
2135 lwpid_of (current_thread
));
2137 current_thread
= saved_thread
;
2141 /* Enqueue one signal in the "signals to report later when out of the
2145 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2147 struct pending_signals
*p_sig
;
2148 struct thread_info
*thread
= get_lwp_thread (lwp
);
2151 debug_printf ("Deferring signal %d for LWP %ld.\n",
2152 WSTOPSIG (*wstat
), lwpid_of (thread
));
2156 struct pending_signals
*sig
;
2158 for (sig
= lwp
->pending_signals_to_report
;
2161 debug_printf (" Already queued %d\n",
2164 debug_printf (" (no more currently queued signals)\n");
2167 /* Don't enqueue non-RT signals if they are already in the deferred
2168 queue. (SIGSTOP being the easiest signal to see ending up here
2170 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
2172 struct pending_signals
*sig
;
2174 for (sig
= lwp
->pending_signals_to_report
;
2178 if (sig
->signal
== WSTOPSIG (*wstat
))
2181 debug_printf ("Not requeuing already queued non-RT signal %d"
2190 p_sig
= XCNEW (struct pending_signals
);
2191 p_sig
->prev
= lwp
->pending_signals_to_report
;
2192 p_sig
->signal
= WSTOPSIG (*wstat
);
2194 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2197 lwp
->pending_signals_to_report
= p_sig
;
2200 /* Dequeue one signal from the "signals to report later when out of
2201 the jump pad" list. */
2204 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2206 struct thread_info
*thread
= get_lwp_thread (lwp
);
2208 if (lwp
->pending_signals_to_report
!= NULL
)
2210 struct pending_signals
**p_sig
;
2212 p_sig
= &lwp
->pending_signals_to_report
;
2213 while ((*p_sig
)->prev
!= NULL
)
2214 p_sig
= &(*p_sig
)->prev
;
2216 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
2217 if ((*p_sig
)->info
.si_signo
!= 0)
2218 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2224 debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
2225 WSTOPSIG (*wstat
), lwpid_of (thread
));
2229 struct pending_signals
*sig
;
2231 for (sig
= lwp
->pending_signals_to_report
;
2234 debug_printf (" Still queued %d\n",
2237 debug_printf (" (no more queued signals)\n");
2247 linux_process_target::check_stopped_by_watchpoint (lwp_info
*child
)
2249 struct thread_info
*saved_thread
= current_thread
;
2250 current_thread
= get_lwp_thread (child
);
2252 if (low_stopped_by_watchpoint ())
2254 child
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2255 child
->stopped_data_address
= low_stopped_data_address ();
2258 current_thread
= saved_thread
;
2260 return child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2264 linux_process_target::low_stopped_by_watchpoint ()
2270 linux_process_target::low_stopped_data_address ()
2275 /* Return the ptrace options that we want to try to enable. */
2278 linux_low_ptrace_options (int attached
)
2280 client_state
&cs
= get_client_state ();
2284 options
|= PTRACE_O_EXITKILL
;
2286 if (cs
.report_fork_events
)
2287 options
|= PTRACE_O_TRACEFORK
;
2289 if (cs
.report_vfork_events
)
2290 options
|= (PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEVFORKDONE
);
2292 if (cs
.report_exec_events
)
2293 options
|= PTRACE_O_TRACEEXEC
;
2295 options
|= PTRACE_O_TRACESYSGOOD
;
2301 linux_process_target::filter_event (int lwpid
, int wstat
)
2303 client_state
&cs
= get_client_state ();
2304 struct lwp_info
*child
;
2305 struct thread_info
*thread
;
2306 int have_stop_pc
= 0;
2308 child
= find_lwp_pid (ptid_t (lwpid
));
2310 /* Check for stop events reported by a process we didn't already
2311 know about - anything not already in our LWP list.
2313 If we're expecting to receive stopped processes after
2314 fork, vfork, and clone events, then we'll just add the
2315 new one to our list and go back to waiting for the event
2316 to be reported - the stopped process might be returned
2317 from waitpid before or after the event is.
2319 But note the case of a non-leader thread exec'ing after the
2320 leader having exited, and gone from our lists (because
2321 check_zombie_leaders deleted it). The non-leader thread
2322 changes its tid to the tgid. */
2324 if (WIFSTOPPED (wstat
) && child
== NULL
&& WSTOPSIG (wstat
) == SIGTRAP
2325 && linux_ptrace_get_extended_event (wstat
) == PTRACE_EVENT_EXEC
)
2329 /* A multi-thread exec after we had seen the leader exiting. */
2332 debug_printf ("LLW: Re-adding thread group leader LWP %d"
2333 "after exec.\n", lwpid
);
2336 child_ptid
= ptid_t (lwpid
, lwpid
, 0);
2337 child
= add_lwp (child_ptid
);
2339 current_thread
= child
->thread
;
2342 /* If we didn't find a process, one of two things presumably happened:
2343 - A process we started and then detached from has exited. Ignore it.
2344 - A process we are controlling has forked and the new child's stop
2345 was reported to us by the kernel. Save its PID. */
2346 if (child
== NULL
&& WIFSTOPPED (wstat
))
2348 add_to_pid_list (&stopped_pids
, lwpid
, wstat
);
2351 else if (child
== NULL
)
2354 thread
= get_lwp_thread (child
);
2358 child
->last_status
= wstat
;
2360 /* Check if the thread has exited. */
2361 if ((WIFEXITED (wstat
) || WIFSIGNALED (wstat
)))
2364 debug_printf ("LLFE: %d exited.\n", lwpid
);
2366 if (finish_step_over (child
))
2368 /* Unsuspend all other LWPs, and set them back running again. */
2369 unsuspend_all_lwps (child
);
2372 /* If there is at least one more LWP, then the exit signal was
2373 not the end of the debugged application and should be
2374 ignored, unless GDB wants to hear about thread exits. */
2375 if (cs
.report_thread_events
2376 || last_thread_of_process_p (pid_of (thread
)))
2378 /* Since events are serialized to GDB core, and we can't
2379 report this one right now. Leave the status pending for
2380 the next time we're able to report it. */
2381 mark_lwp_dead (child
, wstat
);
2391 gdb_assert (WIFSTOPPED (wstat
));
2393 if (WIFSTOPPED (wstat
))
2395 struct process_info
*proc
;
2397 /* Architecture-specific setup after inferior is running. */
2398 proc
= find_process_pid (pid_of (thread
));
2399 if (proc
->tdesc
== NULL
)
2403 /* This needs to happen after we have attached to the
2404 inferior and it is stopped for the first time, but
2405 before we access any inferior registers. */
2406 arch_setup_thread (thread
);
2410 /* The process is started, but GDBserver will do
2411 architecture-specific setup after the program stops at
2412 the first instruction. */
2413 child
->status_pending_p
= 1;
2414 child
->status_pending
= wstat
;
2420 if (WIFSTOPPED (wstat
) && child
->must_set_ptrace_flags
)
2422 struct process_info
*proc
= find_process_pid (pid_of (thread
));
2423 int options
= linux_low_ptrace_options (proc
->attached
);
2425 linux_enable_event_reporting (lwpid
, options
);
2426 child
->must_set_ptrace_flags
= 0;
2429 /* Always update syscall_state, even if it will be filtered later. */
2430 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SYSCALL_SIGTRAP
)
2432 child
->syscall_state
2433 = (child
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2434 ? TARGET_WAITKIND_SYSCALL_RETURN
2435 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2439 /* Almost all other ptrace-stops are known to be outside of system
2440 calls, with further exceptions in handle_extended_wait. */
2441 child
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2444 /* Be careful to not overwrite stop_pc until save_stop_reason is
2446 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
2447 && linux_is_extended_waitstatus (wstat
))
2449 child
->stop_pc
= get_pc (child
);
2450 if (handle_extended_wait (&child
, wstat
))
2452 /* The event has been handled, so just return without
2458 if (linux_wstatus_maybe_breakpoint (wstat
))
2460 if (save_stop_reason (child
))
2465 child
->stop_pc
= get_pc (child
);
2467 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGSTOP
2468 && child
->stop_expected
)
2471 debug_printf ("Expected stop.\n");
2472 child
->stop_expected
= 0;
2474 if (thread
->last_resume_kind
== resume_stop
)
2476 /* We want to report the stop to the core. Treat the
2477 SIGSTOP as a normal event. */
2479 debug_printf ("LLW: resume_stop SIGSTOP caught for %s.\n",
2480 target_pid_to_str (ptid_of (thread
)));
2482 else if (stopping_threads
!= NOT_STOPPING_THREADS
)
2484 /* Stopping threads. We don't want this SIGSTOP to end up
2487 debug_printf ("LLW: SIGSTOP caught for %s "
2488 "while stopping threads.\n",
2489 target_pid_to_str (ptid_of (thread
)));
2494 /* This is a delayed SIGSTOP. Filter out the event. */
2496 debug_printf ("LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2497 child
->stepping
? "step" : "continue",
2498 target_pid_to_str (ptid_of (thread
)));
2500 resume_one_lwp (child
, child
->stepping
, 0, NULL
);
2505 child
->status_pending_p
= 1;
2506 child
->status_pending
= wstat
;
2510 /* Return true if THREAD is doing hardware single step. */
2513 maybe_hw_step (struct thread_info
*thread
)
2515 if (can_hardware_single_step ())
2519 /* GDBserver must insert single-step breakpoint for software
2521 gdb_assert (has_single_step_breakpoints (thread
));
2527 linux_process_target::resume_stopped_resumed_lwps (thread_info
*thread
)
2529 struct lwp_info
*lp
= get_thread_lwp (thread
);
2533 && !lp
->status_pending_p
2534 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2538 if (thread
->last_resume_kind
== resume_step
)
2539 step
= maybe_hw_step (thread
);
2542 debug_printf ("RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
2543 target_pid_to_str (ptid_of (thread
)),
2544 paddress (lp
->stop_pc
),
2547 resume_one_lwp (lp
, step
, GDB_SIGNAL_0
, NULL
);
2552 linux_process_target::wait_for_event_filtered (ptid_t wait_ptid
,
2554 int *wstatp
, int options
)
2556 struct thread_info
*event_thread
;
2557 struct lwp_info
*event_child
, *requested_child
;
2558 sigset_t block_mask
, prev_mask
;
2561 /* N.B. event_thread points to the thread_info struct that contains
2562 event_child. Keep them in sync. */
2563 event_thread
= NULL
;
2565 requested_child
= NULL
;
2567 /* Check for a lwp with a pending status. */
2569 if (filter_ptid
== minus_one_ptid
|| filter_ptid
.is_pid ())
2571 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2573 return status_pending_p_callback (thread
, filter_ptid
);
2576 if (event_thread
!= NULL
)
2577 event_child
= get_thread_lwp (event_thread
);
2578 if (debug_threads
&& event_thread
)
2579 debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread
));
2581 else if (filter_ptid
!= null_ptid
)
2583 requested_child
= find_lwp_pid (filter_ptid
);
2585 if (stopping_threads
== NOT_STOPPING_THREADS
2586 && requested_child
->status_pending_p
2587 && (requested_child
->collecting_fast_tracepoint
2588 != fast_tpoint_collect_result::not_collecting
))
2590 enqueue_one_deferred_signal (requested_child
,
2591 &requested_child
->status_pending
);
2592 requested_child
->status_pending_p
= 0;
2593 requested_child
->status_pending
= 0;
2594 resume_one_lwp (requested_child
, 0, 0, NULL
);
2597 if (requested_child
->suspended
2598 && requested_child
->status_pending_p
)
2600 internal_error (__FILE__
, __LINE__
,
2601 "requesting an event out of a"
2602 " suspended child?");
2605 if (requested_child
->status_pending_p
)
2607 event_child
= requested_child
;
2608 event_thread
= get_lwp_thread (event_child
);
2612 if (event_child
!= NULL
)
2615 debug_printf ("Got an event from pending child %ld (%04x)\n",
2616 lwpid_of (event_thread
), event_child
->status_pending
);
2617 *wstatp
= event_child
->status_pending
;
2618 event_child
->status_pending_p
= 0;
2619 event_child
->status_pending
= 0;
2620 current_thread
= event_thread
;
2621 return lwpid_of (event_thread
);
2624 /* But if we don't find a pending event, we'll have to wait.
2626 We only enter this loop if no process has a pending wait status.
2627 Thus any action taken in response to a wait status inside this
2628 loop is responding as soon as we detect the status, not after any
2631 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2632 all signals while here. */
2633 sigfillset (&block_mask
);
2634 gdb_sigmask (SIG_BLOCK
, &block_mask
, &prev_mask
);
2636 /* Always pull all events out of the kernel. We'll randomly select
2637 an event LWP out of all that have events, to prevent
2639 while (event_child
== NULL
)
2643 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2646 - If the thread group leader exits while other threads in the
2647 thread group still exist, waitpid(TGID, ...) hangs. That
2648 waitpid won't return an exit status until the other threads
2649 in the group are reaped.
2651 - When a non-leader thread execs, that thread just vanishes
2652 without reporting an exit (so we'd hang if we waited for it
2653 explicitly in that case). The exec event is reported to
2656 ret
= my_waitpid (-1, wstatp
, options
| WNOHANG
);
2659 debug_printf ("LWFE: waitpid(-1, ...) returned %d, %s\n",
2660 ret
, errno
? safe_strerror (errno
) : "ERRNO-OK");
2666 debug_printf ("LLW: waitpid %ld received %s\n",
2667 (long) ret
, status_to_str (*wstatp
));
2670 /* Filter all events. IOW, leave all events pending. We'll
2671 randomly select an event LWP out of all that have events
2673 filter_event (ret
, *wstatp
);
2674 /* Retry until nothing comes out of waitpid. A single
2675 SIGCHLD can indicate more than one child stopped. */
2679 /* Now that we've pulled all events out of the kernel, resume
2680 LWPs that don't have an interesting event to report. */
2681 if (stopping_threads
== NOT_STOPPING_THREADS
)
2682 for_each_thread ([this] (thread_info
*thread
)
2684 resume_stopped_resumed_lwps (thread
);
2687 /* ... and find an LWP with a status to report to the core, if
2689 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2691 return status_pending_p_callback (thread
, filter_ptid
);
2694 if (event_thread
!= NULL
)
2696 event_child
= get_thread_lwp (event_thread
);
2697 *wstatp
= event_child
->status_pending
;
2698 event_child
->status_pending_p
= 0;
2699 event_child
->status_pending
= 0;
2703 /* Check for zombie thread group leaders. Those can't be reaped
2704 until all other threads in the thread group are. */
2705 check_zombie_leaders ();
2707 auto not_stopped
= [&] (thread_info
*thread
)
2709 return not_stopped_callback (thread
, wait_ptid
);
2712 /* If there are no resumed children left in the set of LWPs we
2713 want to wait for, bail. We can't just block in
2714 waitpid/sigsuspend, because lwps might have been left stopped
2715 in trace-stop state, and we'd be stuck forever waiting for
2716 their status to change (which would only happen if we resumed
2717 them). Even if WNOHANG is set, this return code is preferred
2718 over 0 (below), as it is more detailed. */
2719 if (find_thread (not_stopped
) == NULL
)
2722 debug_printf ("LLW: exit (no unwaited-for LWP)\n");
2723 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2727 /* No interesting event to report to the caller. */
2728 if ((options
& WNOHANG
))
2731 debug_printf ("WNOHANG set, no event found\n");
2733 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2737 /* Block until we get an event reported with SIGCHLD. */
2739 debug_printf ("sigsuspend'ing\n");
2741 sigsuspend (&prev_mask
);
2742 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2746 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2748 current_thread
= event_thread
;
2750 return lwpid_of (event_thread
);
2754 linux_process_target::wait_for_event (ptid_t ptid
, int *wstatp
, int options
)
2756 return wait_for_event_filtered (ptid
, ptid
, wstatp
, options
);
2759 /* Select one LWP out of those that have events pending. */
2762 select_event_lwp (struct lwp_info
**orig_lp
)
2764 struct thread_info
*event_thread
= NULL
;
2766 /* In all-stop, give preference to the LWP that is being
2767 single-stepped. There will be at most one, and it's the LWP that
2768 the core is most interested in. If we didn't do this, then we'd
2769 have to handle pending step SIGTRAPs somehow in case the core
2770 later continues the previously-stepped thread, otherwise we'd
2771 report the pending SIGTRAP, and the core, not having stepped the
2772 thread, wouldn't understand what the trap was for, and therefore
2773 would report it to the user as a random signal. */
2776 event_thread
= find_thread ([] (thread_info
*thread
)
2778 lwp_info
*lp
= get_thread_lwp (thread
);
2780 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2781 && thread
->last_resume_kind
== resume_step
2782 && lp
->status_pending_p
);
2785 if (event_thread
!= NULL
)
2788 debug_printf ("SEL: Select single-step %s\n",
2789 target_pid_to_str (ptid_of (event_thread
)));
2792 if (event_thread
== NULL
)
2794 /* No single-stepping LWP. Select one at random, out of those
2795 which have had events. */
2797 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2799 lwp_info
*lp
= get_thread_lwp (thread
);
2801 /* Only resumed LWPs that have an event pending. */
2802 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2803 && lp
->status_pending_p
);
2807 if (event_thread
!= NULL
)
2809 struct lwp_info
*event_lp
= get_thread_lwp (event_thread
);
2811 /* Switch the event LWP. */
2812 *orig_lp
= event_lp
;
2816 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2820 unsuspend_all_lwps (struct lwp_info
*except
)
2822 for_each_thread ([&] (thread_info
*thread
)
2824 lwp_info
*lwp
= get_thread_lwp (thread
);
2827 lwp_suspended_decr (lwp
);
2831 static bool stuck_in_jump_pad_callback (thread_info
*thread
);
2832 static bool lwp_running (thread_info
*thread
);
2834 /* Stabilize threads (move out of jump pads).
2836 If a thread is midway collecting a fast tracepoint, we need to
2837 finish the collection and move it out of the jump pad before
2838 reporting the signal.
2840 This avoids recursion while collecting (when a signal arrives
2841 midway, and the signal handler itself collects), which would trash
2842 the trace buffer. In case the user set a breakpoint in a signal
2843 handler, this avoids the backtrace showing the jump pad, etc..
2844 Most importantly, there are certain things we can't do safely if
2845 threads are stopped in a jump pad (or in its callee's). For
2848 - starting a new trace run. A thread still collecting the
2849 previous run, could trash the trace buffer when resumed. The trace
2850 buffer control structures would have been reset but the thread had
2851 no way to tell. The thread could even midway memcpy'ing to the
2852 buffer, which would mean that when resumed, it would clobber the
2853 trace buffer that had been set for a new run.
2855 - we can't rewrite/reuse the jump pads for new tracepoints
2856 safely. Say you do tstart while a thread is stopped midway while
2857 collecting. When the thread is later resumed, it finishes the
2858 collection, and returns to the jump pad, to execute the original
2859 instruction that was under the tracepoint jump at the time the
2860 older run had been started. If the jump pad had been rewritten
2861 since for something else in the new run, the thread would now
2862 execute the wrong / random instructions. */
2865 linux_process_target::stabilize_threads ()
2867 thread_info
*thread_stuck
= find_thread (stuck_in_jump_pad_callback
);
2869 if (thread_stuck
!= NULL
)
2872 debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
2873 lwpid_of (thread_stuck
));
2877 thread_info
*saved_thread
= current_thread
;
2879 stabilizing_threads
= 1;
2882 for_each_thread ([this] (thread_info
*thread
)
2884 move_out_of_jump_pad (thread
);
2887 /* Loop until all are stopped out of the jump pads. */
2888 while (find_thread (lwp_running
) != NULL
)
2890 struct target_waitstatus ourstatus
;
2891 struct lwp_info
*lwp
;
2894 /* Note that we go through the full wait even loop. While
2895 moving threads out of jump pad, we need to be able to step
2896 over internal breakpoints and such. */
2897 wait_1 (minus_one_ptid
, &ourstatus
, 0);
2899 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
2901 lwp
= get_thread_lwp (current_thread
);
2904 lwp_suspended_inc (lwp
);
2906 if (ourstatus
.value
.sig
!= GDB_SIGNAL_0
2907 || current_thread
->last_resume_kind
== resume_stop
)
2909 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.value
.sig
));
2910 enqueue_one_deferred_signal (lwp
, &wstat
);
2915 unsuspend_all_lwps (NULL
);
2917 stabilizing_threads
= 0;
2919 current_thread
= saved_thread
;
2923 thread_stuck
= find_thread (stuck_in_jump_pad_callback
);
2925 if (thread_stuck
!= NULL
)
2926 debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
2927 lwpid_of (thread_stuck
));
2931 /* Convenience function that is called when the kernel reports an
2932 event that is not passed out to GDB. */
2935 ignore_event (struct target_waitstatus
*ourstatus
)
2937 /* If we got an event, there may still be others, as a single
2938 SIGCHLD can indicate more than one child stopped. This forces
2939 another target_wait call. */
2942 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2946 /* Convenience function that is called when the kernel reports an exit
2947 event. This decides whether to report the event to GDB as a
2948 process exit event, a thread exit event, or to suppress the
2952 filter_exit_event (struct lwp_info
*event_child
,
2953 struct target_waitstatus
*ourstatus
)
2955 client_state
&cs
= get_client_state ();
2956 struct thread_info
*thread
= get_lwp_thread (event_child
);
2957 ptid_t ptid
= ptid_of (thread
);
2959 if (!last_thread_of_process_p (pid_of (thread
)))
2961 if (cs
.report_thread_events
)
2962 ourstatus
->kind
= TARGET_WAITKIND_THREAD_EXITED
;
2964 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2966 delete_lwp (event_child
);
2971 /* Returns 1 if GDB is interested in any event_child syscalls. */
2974 gdb_catching_syscalls_p (struct lwp_info
*event_child
)
2976 struct thread_info
*thread
= get_lwp_thread (event_child
);
2977 struct process_info
*proc
= get_thread_process (thread
);
2979 return !proc
->syscalls_to_catch
.empty ();
2982 /* Returns 1 if GDB is interested in the event_child syscall.
2983 Only to be called when stopped reason is SYSCALL_SIGTRAP. */
2986 gdb_catch_this_syscall_p (struct lwp_info
*event_child
)
2989 struct thread_info
*thread
= get_lwp_thread (event_child
);
2990 struct process_info
*proc
= get_thread_process (thread
);
2992 if (proc
->syscalls_to_catch
.empty ())
2995 if (proc
->syscalls_to_catch
[0] == ANY_SYSCALL
)
2998 get_syscall_trapinfo (event_child
, &sysno
);
3000 for (int iter
: proc
->syscalls_to_catch
)
3008 linux_process_target::wait_1 (ptid_t ptid
, target_waitstatus
*ourstatus
,
3011 client_state
&cs
= get_client_state ();
3013 struct lwp_info
*event_child
;
3016 int step_over_finished
;
3017 int bp_explains_trap
;
3018 int maybe_internal_trap
;
3027 debug_printf ("wait_1: [%s]\n", target_pid_to_str (ptid
));
3030 /* Translate generic target options into linux options. */
3032 if (target_options
& TARGET_WNOHANG
)
3035 bp_explains_trap
= 0;
3038 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3040 auto status_pending_p_any
= [&] (thread_info
*thread
)
3042 return status_pending_p_callback (thread
, minus_one_ptid
);
3045 auto not_stopped
= [&] (thread_info
*thread
)
3047 return not_stopped_callback (thread
, minus_one_ptid
);
3050 /* Find a resumed LWP, if any. */
3051 if (find_thread (status_pending_p_any
) != NULL
)
3053 else if (find_thread (not_stopped
) != NULL
)
3058 if (step_over_bkpt
== null_ptid
)
3059 pid
= wait_for_event (ptid
, &w
, options
);
3063 debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
3064 target_pid_to_str (step_over_bkpt
));
3065 pid
= wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
3068 if (pid
== 0 || (pid
== -1 && !any_resumed
))
3070 gdb_assert (target_options
& TARGET_WNOHANG
);
3074 debug_printf ("wait_1 ret = null_ptid, "
3075 "TARGET_WAITKIND_IGNORE\n");
3079 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3086 debug_printf ("wait_1 ret = null_ptid, "
3087 "TARGET_WAITKIND_NO_RESUMED\n");
3091 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3095 event_child
= get_thread_lwp (current_thread
);
3097 /* wait_for_event only returns an exit status for the last
3098 child of a process. Report it. */
3099 if (WIFEXITED (w
) || WIFSIGNALED (w
))
3103 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
3104 ourstatus
->value
.integer
= WEXITSTATUS (w
);
3108 debug_printf ("wait_1 ret = %s, exited with "
3110 target_pid_to_str (ptid_of (current_thread
)),
3117 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
3118 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (w
));
3122 debug_printf ("wait_1 ret = %s, terminated with "
3124 target_pid_to_str (ptid_of (current_thread
)),
3130 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3131 return filter_exit_event (event_child
, ourstatus
);
3133 return ptid_of (current_thread
);
3136 /* If step-over executes a breakpoint instruction, in the case of a
3137 hardware single step it means a gdb/gdbserver breakpoint had been
3138 planted on top of a permanent breakpoint, in the case of a software
3139 single step it may just mean that gdbserver hit the reinsert breakpoint.
3140 The PC has been adjusted by save_stop_reason to point at
3141 the breakpoint address.
3142 So in the case of the hardware single step advance the PC manually
3143 past the breakpoint and in the case of software single step advance only
3144 if it's not the single_step_breakpoint we are hitting.
3145 This avoids that a program would keep trapping a permanent breakpoint
3147 if (step_over_bkpt
!= null_ptid
3148 && event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3149 && (event_child
->stepping
3150 || !single_step_breakpoint_inserted_here (event_child
->stop_pc
)))
3152 int increment_pc
= 0;
3153 int breakpoint_kind
= 0;
3154 CORE_ADDR stop_pc
= event_child
->stop_pc
;
3156 breakpoint_kind
= breakpoint_kind_from_current_state (&stop_pc
);
3157 sw_breakpoint_from_kind (breakpoint_kind
, &increment_pc
);
3161 debug_printf ("step-over for %s executed software breakpoint\n",
3162 target_pid_to_str (ptid_of (current_thread
)));
3165 if (increment_pc
!= 0)
3167 struct regcache
*regcache
3168 = get_thread_regcache (current_thread
, 1);
3170 event_child
->stop_pc
+= increment_pc
;
3171 low_set_pc (regcache
, event_child
->stop_pc
);
3173 if (!low_breakpoint_at (event_child
->stop_pc
))
3174 event_child
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
3178 /* If this event was not handled before, and is not a SIGTRAP, we
3179 report it. SIGILL and SIGSEGV are also treated as traps in case
3180 a breakpoint is inserted at the current PC. If this target does
3181 not support internal breakpoints at all, we also report the
3182 SIGTRAP without further processing; it's of no concern to us. */
3184 = (low_supports_breakpoints ()
3185 && (WSTOPSIG (w
) == SIGTRAP
3186 || ((WSTOPSIG (w
) == SIGILL
3187 || WSTOPSIG (w
) == SIGSEGV
)
3188 && low_breakpoint_at (event_child
->stop_pc
))));
3190 if (maybe_internal_trap
)
3192 /* Handle anything that requires bookkeeping before deciding to
3193 report the event or continue waiting. */
3195 /* First check if we can explain the SIGTRAP with an internal
3196 breakpoint, or if we should possibly report the event to GDB.
3197 Do this before anything that may remove or insert a
3199 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
3201 /* We have a SIGTRAP, possibly a step-over dance has just
3202 finished. If so, tweak the state machine accordingly,
3203 reinsert breakpoints and delete any single-step
3205 step_over_finished
= finish_step_over (event_child
);
3207 /* Now invoke the callbacks of any internal breakpoints there. */
3208 check_breakpoints (event_child
->stop_pc
);
3210 /* Handle tracepoint data collecting. This may overflow the
3211 trace buffer, and cause a tracing stop, removing
3213 trace_event
= handle_tracepoints (event_child
);
3215 if (bp_explains_trap
)
3218 debug_printf ("Hit a gdbserver breakpoint.\n");
3223 /* We have some other signal, possibly a step-over dance was in
3224 progress, and it should be cancelled too. */
3225 step_over_finished
= finish_step_over (event_child
);
3228 /* We have all the data we need. Either report the event to GDB, or
3229 resume threads and keep waiting for more. */
3231 /* If we're collecting a fast tracepoint, finish the collection and
3232 move out of the jump pad before delivering a signal. See
3233 linux_stabilize_threads. */
3236 && WSTOPSIG (w
) != SIGTRAP
3237 && supports_fast_tracepoints ()
3238 && agent_loaded_p ())
3241 debug_printf ("Got signal %d for LWP %ld. Check if we need "
3242 "to defer or adjust it.\n",
3243 WSTOPSIG (w
), lwpid_of (current_thread
));
3245 /* Allow debugging the jump pad itself. */
3246 if (current_thread
->last_resume_kind
!= resume_step
3247 && maybe_move_out_of_jump_pad (event_child
, &w
))
3249 enqueue_one_deferred_signal (event_child
, &w
);
3252 debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
3253 WSTOPSIG (w
), lwpid_of (current_thread
));
3255 resume_one_lwp (event_child
, 0, 0, NULL
);
3259 return ignore_event (ourstatus
);
3263 if (event_child
->collecting_fast_tracepoint
3264 != fast_tpoint_collect_result::not_collecting
)
3267 debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
3268 "Check if we're already there.\n",
3269 lwpid_of (current_thread
),
3270 (int) event_child
->collecting_fast_tracepoint
);
3274 event_child
->collecting_fast_tracepoint
3275 = linux_fast_tracepoint_collecting (event_child
, NULL
);
3277 if (event_child
->collecting_fast_tracepoint
3278 != fast_tpoint_collect_result::before_insn
)
3280 /* No longer need this breakpoint. */
3281 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
3284 debug_printf ("No longer need exit-jump-pad bkpt; removing it."
3285 "stopping all threads momentarily.\n");
3287 /* Other running threads could hit this breakpoint.
3288 We don't handle moribund locations like GDB does,
3289 instead we always pause all threads when removing
3290 breakpoints, so that any step-over or
3291 decr_pc_after_break adjustment is always taken
3292 care of while the breakpoint is still
3294 stop_all_lwps (1, event_child
);
3296 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
3297 event_child
->exit_jump_pad_bkpt
= NULL
;
3299 unstop_all_lwps (1, event_child
);
3301 gdb_assert (event_child
->suspended
>= 0);
3305 if (event_child
->collecting_fast_tracepoint
3306 == fast_tpoint_collect_result::not_collecting
)
3309 debug_printf ("fast tracepoint finished "
3310 "collecting successfully.\n");
3312 /* We may have a deferred signal to report. */
3313 if (dequeue_one_deferred_signal (event_child
, &w
))
3316 debug_printf ("dequeued one signal.\n");
3321 debug_printf ("no deferred signals.\n");
3323 if (stabilizing_threads
)
3325 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3326 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3330 debug_printf ("wait_1 ret = %s, stopped "
3331 "while stabilizing threads\n",
3332 target_pid_to_str (ptid_of (current_thread
)));
3336 return ptid_of (current_thread
);
3342 /* Check whether GDB would be interested in this event. */
3344 /* Check if GDB is interested in this syscall. */
3346 && WSTOPSIG (w
) == SYSCALL_SIGTRAP
3347 && !gdb_catch_this_syscall_p (event_child
))
3351 debug_printf ("Ignored syscall for LWP %ld.\n",
3352 lwpid_of (current_thread
));
3355 resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
3359 return ignore_event (ourstatus
);
3362 /* If GDB is not interested in this signal, don't stop other
3363 threads, and don't report it to GDB. Just resume the inferior
3364 right away. We do this for threading-related signals as well as
3365 any that GDB specifically requested we ignore. But never ignore
3366 SIGSTOP if we sent it ourselves, and do not ignore signals when
3367 stepping - they may require special handling to skip the signal
3368 handler. Also never ignore signals that could be caused by a
3371 && current_thread
->last_resume_kind
!= resume_step
3373 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3374 (current_process ()->priv
->thread_db
!= NULL
3375 && (WSTOPSIG (w
) == __SIGRTMIN
3376 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
3379 (cs
.pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
3380 && !(WSTOPSIG (w
) == SIGSTOP
3381 && current_thread
->last_resume_kind
== resume_stop
)
3382 && !linux_wstatus_maybe_breakpoint (w
))))
3384 siginfo_t info
, *info_p
;
3387 debug_printf ("Ignored signal %d for LWP %ld.\n",
3388 WSTOPSIG (w
), lwpid_of (current_thread
));
3390 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
3391 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
3396 if (step_over_finished
)
3398 /* We cancelled this thread's step-over above. We still
3399 need to unsuspend all other LWPs, and set them back
3400 running again while the signal handler runs. */
3401 unsuspend_all_lwps (event_child
);
3403 /* Enqueue the pending signal info so that proceed_all_lwps
3405 enqueue_pending_signal (event_child
, WSTOPSIG (w
), info_p
);
3407 proceed_all_lwps ();
3411 resume_one_lwp (event_child
, event_child
->stepping
,
3412 WSTOPSIG (w
), info_p
);
3418 return ignore_event (ourstatus
);
3421 /* Note that all addresses are always "out of the step range" when
3422 there's no range to begin with. */
3423 in_step_range
= lwp_in_step_range (event_child
);
3425 /* If GDB wanted this thread to single step, and the thread is out
3426 of the step range, we always want to report the SIGTRAP, and let
3427 GDB handle it. Watchpoints should always be reported. So should
3428 signals we can't explain. A SIGTRAP we can't explain could be a
3429 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3430 do, we're be able to handle GDB breakpoints on top of internal
3431 breakpoints, by handling the internal breakpoint and still
3432 reporting the event to GDB. If we don't, we're out of luck, GDB
3433 won't see the breakpoint hit. If we see a single-step event but
3434 the thread should be continuing, don't pass the trap to gdb.
3435 That indicates that we had previously finished a single-step but
3436 left the single-step pending -- see
3437 complete_ongoing_step_over. */
3438 report_to_gdb
= (!maybe_internal_trap
3439 || (current_thread
->last_resume_kind
== resume_step
3441 || event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3443 && !bp_explains_trap
3445 && !step_over_finished
3446 && !(current_thread
->last_resume_kind
== resume_continue
3447 && event_child
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
))
3448 || (gdb_breakpoint_here (event_child
->stop_pc
)
3449 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
3450 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
))
3451 || event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
);
3453 run_breakpoint_commands (event_child
->stop_pc
);
3455 /* We found no reason GDB would want us to stop. We either hit one
3456 of our own breakpoints, or finished an internal step GDB
3457 shouldn't know about. */
3462 if (bp_explains_trap
)
3463 debug_printf ("Hit a gdbserver breakpoint.\n");
3464 if (step_over_finished
)
3465 debug_printf ("Step-over finished.\n");
3467 debug_printf ("Tracepoint event.\n");
3468 if (lwp_in_step_range (event_child
))
3469 debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
3470 paddress (event_child
->stop_pc
),
3471 paddress (event_child
->step_range_start
),
3472 paddress (event_child
->step_range_end
));
3475 /* We're not reporting this breakpoint to GDB, so apply the
3476 decr_pc_after_break adjustment to the inferior's regcache
3479 if (low_supports_breakpoints ())
3481 struct regcache
*regcache
3482 = get_thread_regcache (current_thread
, 1);
3483 low_set_pc (regcache
, event_child
->stop_pc
);
3486 if (step_over_finished
)
3488 /* If we have finished stepping over a breakpoint, we've
3489 stopped and suspended all LWPs momentarily except the
3490 stepping one. This is where we resume them all again.
3491 We're going to keep waiting, so use proceed, which
3492 handles stepping over the next breakpoint. */
3493 unsuspend_all_lwps (event_child
);
3497 /* Remove the single-step breakpoints if any. Note that
3498 there isn't single-step breakpoint if we finished stepping
3500 if (supports_software_single_step ()
3501 && has_single_step_breakpoints (current_thread
))
3503 stop_all_lwps (0, event_child
);
3504 delete_single_step_breakpoints (current_thread
);
3505 unstop_all_lwps (0, event_child
);
3510 debug_printf ("proceeding all threads.\n");
3511 proceed_all_lwps ();
3516 return ignore_event (ourstatus
);
3521 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3524 = target_waitstatus_to_string (&event_child
->waitstatus
);
3526 debug_printf ("LWP %ld: extended event with waitstatus %s\n",
3527 lwpid_of (get_lwp_thread (event_child
)), str
.c_str ());
3529 if (current_thread
->last_resume_kind
== resume_step
)
3531 if (event_child
->step_range_start
== event_child
->step_range_end
)
3532 debug_printf ("GDB wanted to single-step, reporting event.\n");
3533 else if (!lwp_in_step_range (event_child
))
3534 debug_printf ("Out of step range, reporting event.\n");
3536 if (event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
3537 debug_printf ("Stopped by watchpoint.\n");
3538 else if (gdb_breakpoint_here (event_child
->stop_pc
))
3539 debug_printf ("Stopped by GDB breakpoint.\n");
3541 debug_printf ("Hit a non-gdbserver trap event.\n");
3544 /* Alright, we're going to report a stop. */
3546 /* Remove single-step breakpoints. */
3547 if (supports_software_single_step ())
3549 /* Remove single-step breakpoints or not. It it is true, stop all
3550 lwps, so that other threads won't hit the breakpoint in the
3552 int remove_single_step_breakpoints_p
= 0;
3556 remove_single_step_breakpoints_p
3557 = has_single_step_breakpoints (current_thread
);
3561 /* In all-stop, a stop reply cancels all previous resume
3562 requests. Delete all single-step breakpoints. */
3564 find_thread ([&] (thread_info
*thread
) {
3565 if (has_single_step_breakpoints (thread
))
3567 remove_single_step_breakpoints_p
= 1;
3575 if (remove_single_step_breakpoints_p
)
3577 /* If we remove single-step breakpoints from memory, stop all lwps,
3578 so that other threads won't hit the breakpoint in the staled
3580 stop_all_lwps (0, event_child
);
3584 gdb_assert (has_single_step_breakpoints (current_thread
));
3585 delete_single_step_breakpoints (current_thread
);
3589 for_each_thread ([] (thread_info
*thread
){
3590 if (has_single_step_breakpoints (thread
))
3591 delete_single_step_breakpoints (thread
);
3595 unstop_all_lwps (0, event_child
);
3599 if (!stabilizing_threads
)
3601 /* In all-stop, stop all threads. */
3603 stop_all_lwps (0, NULL
);
3605 if (step_over_finished
)
3609 /* If we were doing a step-over, all other threads but
3610 the stepping one had been paused in start_step_over,
3611 with their suspend counts incremented. We don't want
3612 to do a full unstop/unpause, because we're in
3613 all-stop mode (so we want threads stopped), but we
3614 still need to unsuspend the other threads, to
3615 decrement their `suspended' count back. */
3616 unsuspend_all_lwps (event_child
);
3620 /* If we just finished a step-over, then all threads had
3621 been momentarily paused. In all-stop, that's fine,
3622 we want threads stopped by now anyway. In non-stop,
3623 we need to re-resume threads that GDB wanted to be
3625 unstop_all_lwps (1, event_child
);
3629 /* If we're not waiting for a specific LWP, choose an event LWP
3630 from among those that have had events. Giving equal priority
3631 to all LWPs that have had events helps prevent
3633 if (ptid
== minus_one_ptid
)
3635 event_child
->status_pending_p
= 1;
3636 event_child
->status_pending
= w
;
3638 select_event_lwp (&event_child
);
3640 /* current_thread and event_child must stay in sync. */
3641 current_thread
= get_lwp_thread (event_child
);
3643 event_child
->status_pending_p
= 0;
3644 w
= event_child
->status_pending
;
3648 /* Stabilize threads (move out of jump pads). */
3650 target_stabilize_threads ();
3654 /* If we just finished a step-over, then all threads had been
3655 momentarily paused. In all-stop, that's fine, we want
3656 threads stopped by now anyway. In non-stop, we need to
3657 re-resume threads that GDB wanted to be running. */
3658 if (step_over_finished
)
3659 unstop_all_lwps (1, event_child
);
3662 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3664 /* If the reported event is an exit, fork, vfork or exec, let
3667 /* Break the unreported fork relationship chain. */
3668 if (event_child
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
3669 || event_child
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
)
3671 event_child
->fork_relative
->fork_relative
= NULL
;
3672 event_child
->fork_relative
= NULL
;
3675 *ourstatus
= event_child
->waitstatus
;
3676 /* Clear the event lwp's waitstatus since we handled it already. */
3677 event_child
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3680 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3682 /* Now that we've selected our final event LWP, un-adjust its PC if
3683 it was a software breakpoint, and the client doesn't know we can
3684 adjust the breakpoint ourselves. */
3685 if (event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3686 && !cs
.swbreak_feature
)
3688 int decr_pc
= low_decr_pc_after_break ();
3692 struct regcache
*regcache
3693 = get_thread_regcache (current_thread
, 1);
3694 low_set_pc (regcache
, event_child
->stop_pc
+ decr_pc
);
3698 if (WSTOPSIG (w
) == SYSCALL_SIGTRAP
)
3700 get_syscall_trapinfo (event_child
,
3701 &ourstatus
->value
.syscall_number
);
3702 ourstatus
->kind
= event_child
->syscall_state
;
3704 else if (current_thread
->last_resume_kind
== resume_stop
3705 && WSTOPSIG (w
) == SIGSTOP
)
3707 /* A thread that has been requested to stop by GDB with vCont;t,
3708 and it stopped cleanly, so report as SIG0. The use of
3709 SIGSTOP is an implementation detail. */
3710 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3712 else if (current_thread
->last_resume_kind
== resume_stop
3713 && WSTOPSIG (w
) != SIGSTOP
)
3715 /* A thread that has been requested to stop by GDB with vCont;t,
3716 but, it stopped for other reasons. */
3717 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3719 else if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
)
3721 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3724 gdb_assert (step_over_bkpt
== null_ptid
);
3728 debug_printf ("wait_1 ret = %s, %d, %d\n",
3729 target_pid_to_str (ptid_of (current_thread
)),
3730 ourstatus
->kind
, ourstatus
->value
.sig
);
3734 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3735 return filter_exit_event (event_child
, ourstatus
);
3737 return ptid_of (current_thread
);
3740 /* Get rid of any pending event in the pipe. */
3742 async_file_flush (void)
3748 ret
= read (linux_event_pipe
[0], &buf
, 1);
3749 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
3752 /* Put something in the pipe, so the event loop wakes up. */
3754 async_file_mark (void)
3758 async_file_flush ();
3761 ret
= write (linux_event_pipe
[1], "+", 1);
3762 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
3764 /* Ignore EAGAIN. If the pipe is full, the event loop will already
3765 be awakened anyway. */
3769 linux_process_target::wait (ptid_t ptid
,
3770 target_waitstatus
*ourstatus
,
3775 /* Flush the async file first. */
3776 if (target_is_async_p ())
3777 async_file_flush ();
3781 event_ptid
= wait_1 (ptid
, ourstatus
, target_options
);
3783 while ((target_options
& TARGET_WNOHANG
) == 0
3784 && event_ptid
== null_ptid
3785 && ourstatus
->kind
== TARGET_WAITKIND_IGNORE
);
3787 /* If at least one stop was reported, there may be more. A single
3788 SIGCHLD can signal more than one child stop. */
3789 if (target_is_async_p ()
3790 && (target_options
& TARGET_WNOHANG
) != 0
3791 && event_ptid
!= null_ptid
)
3797 /* Send a signal to an LWP. */
3800 kill_lwp (unsigned long lwpid
, int signo
)
3805 ret
= syscall (__NR_tkill
, lwpid
, signo
);
3806 if (errno
== ENOSYS
)
3808 /* If tkill fails, then we are not using nptl threads, a
3809 configuration we no longer support. */
3810 perror_with_name (("tkill"));
3816 linux_stop_lwp (struct lwp_info
*lwp
)
3822 send_sigstop (struct lwp_info
*lwp
)
3826 pid
= lwpid_of (get_lwp_thread (lwp
));
3828 /* If we already have a pending stop signal for this process, don't
3830 if (lwp
->stop_expected
)
3833 debug_printf ("Have pending sigstop for lwp %d\n", pid
);
3839 debug_printf ("Sending sigstop to lwp %d\n", pid
);
3841 lwp
->stop_expected
= 1;
3842 kill_lwp (pid
, SIGSTOP
);
3846 send_sigstop (thread_info
*thread
, lwp_info
*except
)
3848 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3850 /* Ignore EXCEPT. */
3860 /* Increment the suspend count of an LWP, and stop it, if not stopped
3863 suspend_and_send_sigstop (thread_info
*thread
, lwp_info
*except
)
3865 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3867 /* Ignore EXCEPT. */
3871 lwp_suspended_inc (lwp
);
3873 send_sigstop (thread
, except
);
3877 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
3879 /* Store the exit status for later. */
3880 lwp
->status_pending_p
= 1;
3881 lwp
->status_pending
= wstat
;
3883 /* Store in waitstatus as well, as there's nothing else to process
3885 if (WIFEXITED (wstat
))
3887 lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXITED
;
3888 lwp
->waitstatus
.value
.integer
= WEXITSTATUS (wstat
);
3890 else if (WIFSIGNALED (wstat
))
3892 lwp
->waitstatus
.kind
= TARGET_WAITKIND_SIGNALLED
;
3893 lwp
->waitstatus
.value
.sig
= gdb_signal_from_host (WTERMSIG (wstat
));
3896 /* Prevent trying to stop it. */
3899 /* No further stops are expected from a dead lwp. */
3900 lwp
->stop_expected
= 0;
3903 /* Return true if LWP has exited already, and has a pending exit event
3904 to report to GDB. */
3907 lwp_is_marked_dead (struct lwp_info
*lwp
)
3909 return (lwp
->status_pending_p
3910 && (WIFEXITED (lwp
->status_pending
)
3911 || WIFSIGNALED (lwp
->status_pending
)));
3915 linux_process_target::wait_for_sigstop ()
3917 struct thread_info
*saved_thread
;
3922 saved_thread
= current_thread
;
3923 if (saved_thread
!= NULL
)
3924 saved_tid
= saved_thread
->id
;
3926 saved_tid
= null_ptid
; /* avoid bogus unused warning */
3929 debug_printf ("wait_for_sigstop: pulling events\n");
3931 /* Passing NULL_PTID as filter indicates we want all events to be
3932 left pending. Eventually this returns when there are no
3933 unwaited-for children left. */
3934 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
, __WALL
);
3935 gdb_assert (ret
== -1);
3937 if (saved_thread
== NULL
|| mythread_alive (saved_tid
))
3938 current_thread
= saved_thread
;
3942 debug_printf ("Previously current thread died.\n");
3944 /* We can't change the current inferior behind GDB's back,
3945 otherwise, a subsequent command may apply to the wrong
3947 current_thread
= NULL
;
3951 /* Returns true if THREAD is stopped in a jump pad, and we can't
3952 move it out, because we need to report the stop event to GDB. For
3953 example, if the user puts a breakpoint in the jump pad, it's
3954 because she wants to debug it. */
3957 stuck_in_jump_pad_callback (thread_info
*thread
)
3959 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3961 if (lwp
->suspended
!= 0)
3963 internal_error (__FILE__
, __LINE__
,
3964 "LWP %ld is suspended, suspended=%d\n",
3965 lwpid_of (thread
), lwp
->suspended
);
3967 gdb_assert (lwp
->stopped
);
3969 /* Allow debugging the jump pad, gdb_collect, etc.. */
3970 return (supports_fast_tracepoints ()
3971 && agent_loaded_p ()
3972 && (gdb_breakpoint_here (lwp
->stop_pc
)
3973 || lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3974 || thread
->last_resume_kind
== resume_step
)
3975 && (linux_fast_tracepoint_collecting (lwp
, NULL
)
3976 != fast_tpoint_collect_result::not_collecting
));
3980 linux_process_target::move_out_of_jump_pad (thread_info
*thread
)
3982 struct thread_info
*saved_thread
;
3983 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3986 if (lwp
->suspended
!= 0)
3988 internal_error (__FILE__
, __LINE__
,
3989 "LWP %ld is suspended, suspended=%d\n",
3990 lwpid_of (thread
), lwp
->suspended
);
3992 gdb_assert (lwp
->stopped
);
3994 /* For gdb_breakpoint_here. */
3995 saved_thread
= current_thread
;
3996 current_thread
= thread
;
3998 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
4000 /* Allow debugging the jump pad, gdb_collect, etc. */
4001 if (!gdb_breakpoint_here (lwp
->stop_pc
)
4002 && lwp
->stop_reason
!= TARGET_STOPPED_BY_WATCHPOINT
4003 && thread
->last_resume_kind
!= resume_step
4004 && maybe_move_out_of_jump_pad (lwp
, wstat
))
4007 debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
4012 lwp
->status_pending_p
= 0;
4013 enqueue_one_deferred_signal (lwp
, wstat
);
4016 debug_printf ("Signal %d for LWP %ld deferred "
4018 WSTOPSIG (*wstat
), lwpid_of (thread
));
4021 resume_one_lwp (lwp
, 0, 0, NULL
);
4024 lwp_suspended_inc (lwp
);
4026 current_thread
= saved_thread
;
4030 lwp_running (thread_info
*thread
)
4032 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4034 if (lwp_is_marked_dead (lwp
))
4037 return !lwp
->stopped
;
4041 linux_process_target::stop_all_lwps (int suspend
, lwp_info
*except
)
4043 /* Should not be called recursively. */
4044 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
4049 debug_printf ("stop_all_lwps (%s, except=%s)\n",
4050 suspend
? "stop-and-suspend" : "stop",
4052 ? target_pid_to_str (ptid_of (get_lwp_thread (except
)))
4056 stopping_threads
= (suspend
4057 ? STOPPING_AND_SUSPENDING_THREADS
4058 : STOPPING_THREADS
);
4061 for_each_thread ([&] (thread_info
*thread
)
4063 suspend_and_send_sigstop (thread
, except
);
4066 for_each_thread ([&] (thread_info
*thread
)
4068 send_sigstop (thread
, except
);
4071 wait_for_sigstop ();
4072 stopping_threads
= NOT_STOPPING_THREADS
;
4076 debug_printf ("stop_all_lwps done, setting stopping_threads "
4077 "back to !stopping\n");
4082 /* Enqueue one signal in the chain of signals which need to be
4083 delivered to this process on next resume. */
4086 enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
)
4088 struct pending_signals
*p_sig
= XNEW (struct pending_signals
);
4090 p_sig
->prev
= lwp
->pending_signals
;
4091 p_sig
->signal
= signal
;
4093 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
4095 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
4096 lwp
->pending_signals
= p_sig
;
4100 linux_process_target::install_software_single_step_breakpoints (lwp_info
*lwp
)
4102 struct thread_info
*thread
= get_lwp_thread (lwp
);
4103 struct regcache
*regcache
= get_thread_regcache (thread
, 1);
4105 scoped_restore save_current_thread
= make_scoped_restore (¤t_thread
);
4107 current_thread
= thread
;
4108 std::vector
<CORE_ADDR
> next_pcs
= low_get_next_pcs (regcache
);
4110 for (CORE_ADDR pc
: next_pcs
)
4111 set_single_step_breakpoint (pc
, current_ptid
);
4115 linux_process_target::single_step (lwp_info
* lwp
)
4119 if (can_hardware_single_step ())
4123 else if (supports_software_single_step ())
4125 install_software_single_step_breakpoints (lwp
);
4131 debug_printf ("stepping is not implemented on this target");
4137 /* The signal can be delivered to the inferior if we are not trying to
4138 finish a fast tracepoint collect. Since signal can be delivered in
4139 the step-over, the program may go to signal handler and trap again
4140 after return from the signal handler. We can live with the spurious
4144 lwp_signal_can_be_delivered (struct lwp_info
*lwp
)
4146 return (lwp
->collecting_fast_tracepoint
4147 == fast_tpoint_collect_result::not_collecting
);
4151 linux_process_target::resume_one_lwp_throw (lwp_info
*lwp
, int step
,
4152 int signal
, siginfo_t
*info
)
4154 struct thread_info
*thread
= get_lwp_thread (lwp
);
4155 struct thread_info
*saved_thread
;
4157 struct process_info
*proc
= get_thread_process (thread
);
4159 /* Note that target description may not be initialised
4160 (proc->tdesc == NULL) at this point because the program hasn't
4161 stopped at the first instruction yet. It means GDBserver skips
4162 the extra traps from the wrapper program (see option --wrapper).
4163 Code in this function that requires register access should be
4164 guarded by proc->tdesc == NULL or something else. */
4166 if (lwp
->stopped
== 0)
4169 gdb_assert (lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
4171 fast_tpoint_collect_result fast_tp_collecting
4172 = lwp
->collecting_fast_tracepoint
;
4174 gdb_assert (!stabilizing_threads
4175 || (fast_tp_collecting
4176 != fast_tpoint_collect_result::not_collecting
));
4178 /* Cancel actions that rely on GDB not changing the PC (e.g., the
4179 user used the "jump" command, or "set $pc = foo"). */
4180 if (thread
->while_stepping
!= NULL
&& lwp
->stop_pc
!= get_pc (lwp
))
4182 /* Collecting 'while-stepping' actions doesn't make sense
4184 release_while_stepping_state_list (thread
);
4187 /* If we have pending signals or status, and a new signal, enqueue the
4188 signal. Also enqueue the signal if it can't be delivered to the
4189 inferior right now. */
4191 && (lwp
->status_pending_p
4192 || lwp
->pending_signals
!= NULL
4193 || !lwp_signal_can_be_delivered (lwp
)))
4195 enqueue_pending_signal (lwp
, signal
, info
);
4197 /* Postpone any pending signal. It was enqueued above. */
4201 if (lwp
->status_pending_p
)
4204 debug_printf ("Not resuming lwp %ld (%s, stop %s);"
4205 " has pending status\n",
4206 lwpid_of (thread
), step
? "step" : "continue",
4207 lwp
->stop_expected
? "expected" : "not expected");
4211 saved_thread
= current_thread
;
4212 current_thread
= thread
;
4214 /* This bit needs some thinking about. If we get a signal that
4215 we must report while a single-step reinsert is still pending,
4216 we often end up resuming the thread. It might be better to
4217 (ew) allow a stack of pending events; then we could be sure that
4218 the reinsert happened right away and not lose any signals.
4220 Making this stack would also shrink the window in which breakpoints are
4221 uninserted (see comment in linux_wait_for_lwp) but not enough for
4222 complete correctness, so it won't solve that problem. It may be
4223 worthwhile just to solve this one, however. */
4224 if (lwp
->bp_reinsert
!= 0)
4227 debug_printf (" pending reinsert at 0x%s\n",
4228 paddress (lwp
->bp_reinsert
));
4230 if (can_hardware_single_step ())
4232 if (fast_tp_collecting
== fast_tpoint_collect_result::not_collecting
)
4235 warning ("BAD - reinserting but not stepping.");
4237 warning ("BAD - reinserting and suspended(%d).",
4242 step
= maybe_hw_step (thread
);
4245 if (fast_tp_collecting
== fast_tpoint_collect_result::before_insn
)
4248 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4249 " (exit-jump-pad-bkpt)\n",
4252 else if (fast_tp_collecting
== fast_tpoint_collect_result::at_insn
)
4255 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4256 " single-stepping\n",
4259 if (can_hardware_single_step ())
4263 internal_error (__FILE__
, __LINE__
,
4264 "moving out of jump pad single-stepping"
4265 " not implemented on this target");
4269 /* If we have while-stepping actions in this thread set it stepping.
4270 If we have a signal to deliver, it may or may not be set to
4271 SIG_IGN, we don't know. Assume so, and allow collecting
4272 while-stepping into a signal handler. A possible smart thing to
4273 do would be to set an internal breakpoint at the signal return
4274 address, continue, and carry on catching this while-stepping
4275 action only when that breakpoint is hit. A future
4277 if (thread
->while_stepping
!= NULL
)
4280 debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
4283 step
= single_step (lwp
);
4286 if (proc
->tdesc
!= NULL
&& low_supports_breakpoints ())
4288 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
4290 lwp
->stop_pc
= low_get_pc (regcache
);
4294 debug_printf (" %s from pc 0x%lx\n", step
? "step" : "continue",
4295 (long) lwp
->stop_pc
);
4299 /* If we have pending signals, consume one if it can be delivered to
4301 if (lwp
->pending_signals
!= NULL
&& lwp_signal_can_be_delivered (lwp
))
4303 struct pending_signals
**p_sig
;
4305 p_sig
= &lwp
->pending_signals
;
4306 while ((*p_sig
)->prev
!= NULL
)
4307 p_sig
= &(*p_sig
)->prev
;
4309 signal
= (*p_sig
)->signal
;
4310 if ((*p_sig
)->info
.si_signo
!= 0)
4311 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
4319 debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
4320 lwpid_of (thread
), step
? "step" : "continue", signal
,
4321 lwp
->stop_expected
? "expected" : "not expected");
4323 if (the_low_target
.prepare_to_resume
!= NULL
)
4324 the_low_target
.prepare_to_resume (lwp
);
4326 regcache_invalidate_thread (thread
);
4328 lwp
->stepping
= step
;
4330 ptrace_request
= PTRACE_SINGLESTEP
;
4331 else if (gdb_catching_syscalls_p (lwp
))
4332 ptrace_request
= PTRACE_SYSCALL
;
4334 ptrace_request
= PTRACE_CONT
;
4335 ptrace (ptrace_request
,
4337 (PTRACE_TYPE_ARG3
) 0,
4338 /* Coerce to a uintptr_t first to avoid potential gcc warning
4339 of coercing an 8 byte integer to a 4 byte pointer. */
4340 (PTRACE_TYPE_ARG4
) (uintptr_t) signal
);
4342 current_thread
= saved_thread
;
4344 perror_with_name ("resuming thread");
4346 /* Successfully resumed. Clear state that no longer makes sense,
4347 and mark the LWP as running. Must not do this before resuming
4348 otherwise if that fails other code will be confused. E.g., we'd
4349 later try to stop the LWP and hang forever waiting for a stop
4350 status. Note that we must not throw after this is cleared,
4351 otherwise handle_zombie_lwp_error would get confused. */
4353 lwp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4356 /* Called when we try to resume a stopped LWP and that errors out. If
4357 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4358 or about to become), discard the error, clear any pending status
4359 the LWP may have, and return true (we'll collect the exit status
4360 soon enough). Otherwise, return false. */
4363 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
4365 struct thread_info
*thread
= get_lwp_thread (lp
);
4367 /* If we get an error after resuming the LWP successfully, we'd
4368 confuse !T state for the LWP being gone. */
4369 gdb_assert (lp
->stopped
);
4371 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4372 because even if ptrace failed with ESRCH, the tracee may be "not
4373 yet fully dead", but already refusing ptrace requests. In that
4374 case the tracee has 'R (Running)' state for a little bit
4375 (observed in Linux 3.18). See also the note on ESRCH in the
4376 ptrace(2) man page. Instead, check whether the LWP has any state
4377 other than ptrace-stopped. */
4379 /* Don't assume anything if /proc/PID/status can't be read. */
4380 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread
)) == 0)
4382 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4383 lp
->status_pending_p
= 0;
4390 linux_process_target::resume_one_lwp (lwp_info
*lwp
, int step
, int signal
,
4395 resume_one_lwp_throw (lwp
, step
, signal
, info
);
4397 catch (const gdb_exception_error
&ex
)
4399 if (!check_ptrace_stopped_lwp_gone (lwp
))
4404 /* This function is called once per thread via for_each_thread.
4405 We look up which resume request applies to THREAD and mark it with a
4406 pointer to the appropriate resume request.
4408 This algorithm is O(threads * resume elements), but resume elements
4409 is small (and will remain small at least until GDB supports thread
4413 linux_set_resume_request (thread_info
*thread
, thread_resume
*resume
, size_t n
)
4415 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4417 for (int ndx
= 0; ndx
< n
; ndx
++)
4419 ptid_t ptid
= resume
[ndx
].thread
;
4420 if (ptid
== minus_one_ptid
4421 || ptid
== thread
->id
4422 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4424 || (ptid
.pid () == pid_of (thread
)
4426 || ptid
.lwp () == -1)))
4428 if (resume
[ndx
].kind
== resume_stop
4429 && thread
->last_resume_kind
== resume_stop
)
4432 debug_printf ("already %s LWP %ld at GDB's request\n",
4433 (thread
->last_status
.kind
4434 == TARGET_WAITKIND_STOPPED
)
4442 /* Ignore (wildcard) resume requests for already-resumed
4444 if (resume
[ndx
].kind
!= resume_stop
4445 && thread
->last_resume_kind
!= resume_stop
)
4448 debug_printf ("already %s LWP %ld at GDB's request\n",
4449 (thread
->last_resume_kind
4457 /* Don't let wildcard resumes resume fork children that GDB
4458 does not yet know are new fork children. */
4459 if (lwp
->fork_relative
!= NULL
)
4461 struct lwp_info
*rel
= lwp
->fork_relative
;
4463 if (rel
->status_pending_p
4464 && (rel
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
4465 || rel
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
))
4468 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4474 /* If the thread has a pending event that has already been
4475 reported to GDBserver core, but GDB has not pulled the
4476 event out of the vStopped queue yet, likewise, ignore the
4477 (wildcard) resume request. */
4478 if (in_queued_stop_replies (thread
->id
))
4481 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4486 lwp
->resume
= &resume
[ndx
];
4487 thread
->last_resume_kind
= lwp
->resume
->kind
;
4489 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
4490 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
4492 /* If we had a deferred signal to report, dequeue one now.
4493 This can happen if LWP gets more than one signal while
4494 trying to get out of a jump pad. */
4496 && !lwp
->status_pending_p
4497 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
4499 lwp
->status_pending_p
= 1;
4502 debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
4503 "leaving status pending.\n",
4504 WSTOPSIG (lwp
->status_pending
),
4512 /* No resume action for this thread. */
4517 linux_process_target::resume_status_pending (thread_info
*thread
)
4519 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4521 /* LWPs which will not be resumed are not interesting, because
4522 we might not wait for them next time through linux_wait. */
4523 if (lwp
->resume
== NULL
)
4526 return thread_still_has_status_pending (thread
);
4530 linux_process_target::thread_needs_step_over (thread_info
*thread
)
4532 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4533 struct thread_info
*saved_thread
;
4535 struct process_info
*proc
= get_thread_process (thread
);
4537 /* GDBserver is skipping the extra traps from the wrapper program,
4538 don't have to do step over. */
4539 if (proc
->tdesc
== NULL
)
4542 /* LWPs which will not be resumed are not interesting, because we
4543 might not wait for them next time through linux_wait. */
4548 debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
4553 if (thread
->last_resume_kind
== resume_stop
)
4556 debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
4562 gdb_assert (lwp
->suspended
>= 0);
4567 debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
4572 if (lwp
->status_pending_p
)
4575 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4581 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4585 /* If the PC has changed since we stopped, then don't do anything,
4586 and let the breakpoint/tracepoint be hit. This happens if, for
4587 instance, GDB handled the decr_pc_after_break subtraction itself,
4588 GDB is OOL stepping this thread, or the user has issued a "jump"
4589 command, or poked thread's registers herself. */
4590 if (pc
!= lwp
->stop_pc
)
4593 debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4594 "Old stop_pc was 0x%s, PC is now 0x%s\n",
4596 paddress (lwp
->stop_pc
), paddress (pc
));
4600 /* On software single step target, resume the inferior with signal
4601 rather than stepping over. */
4602 if (supports_software_single_step ()
4603 && lwp
->pending_signals
!= NULL
4604 && lwp_signal_can_be_delivered (lwp
))
4607 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4614 saved_thread
= current_thread
;
4615 current_thread
= thread
;
4617 /* We can only step over breakpoints we know about. */
4618 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
4620 /* Don't step over a breakpoint that GDB expects to hit
4621 though. If the condition is being evaluated on the target's side
4622 and it evaluate to false, step over this breakpoint as well. */
4623 if (gdb_breakpoint_here (pc
)
4624 && gdb_condition_true_at_breakpoint (pc
)
4625 && gdb_no_commands_at_breakpoint (pc
))
4628 debug_printf ("Need step over [LWP %ld]? yes, but found"
4629 " GDB breakpoint at 0x%s; skipping step over\n",
4630 lwpid_of (thread
), paddress (pc
));
4632 current_thread
= saved_thread
;
4638 debug_printf ("Need step over [LWP %ld]? yes, "
4639 "found breakpoint at 0x%s\n",
4640 lwpid_of (thread
), paddress (pc
));
4642 /* We've found an lwp that needs stepping over --- return 1 so
4643 that find_thread stops looking. */
4644 current_thread
= saved_thread
;
4650 current_thread
= saved_thread
;
4653 debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
4655 lwpid_of (thread
), paddress (pc
));
4661 linux_process_target::start_step_over (lwp_info
*lwp
)
4663 struct thread_info
*thread
= get_lwp_thread (lwp
);
4664 struct thread_info
*saved_thread
;
4669 debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
4672 stop_all_lwps (1, lwp
);
4674 if (lwp
->suspended
!= 0)
4676 internal_error (__FILE__
, __LINE__
,
4677 "LWP %ld suspended=%d\n", lwpid_of (thread
),
4682 debug_printf ("Done stopping all threads for step-over.\n");
4684 /* Note, we should always reach here with an already adjusted PC,
4685 either by GDB (if we're resuming due to GDB's request), or by our
4686 caller, if we just finished handling an internal breakpoint GDB
4687 shouldn't care about. */
4690 saved_thread
= current_thread
;
4691 current_thread
= thread
;
4693 lwp
->bp_reinsert
= pc
;
4694 uninsert_breakpoints_at (pc
);
4695 uninsert_fast_tracepoint_jumps_at (pc
);
4697 step
= single_step (lwp
);
4699 current_thread
= saved_thread
;
4701 resume_one_lwp (lwp
, step
, 0, NULL
);
4703 /* Require next event from this LWP. */
4704 step_over_bkpt
= thread
->id
;
4707 /* Finish a step-over. Reinsert the breakpoint we had uninserted in
4708 start_step_over, if still there, and delete any single-step
4709 breakpoints we've set, on non hardware single-step targets. */
4712 finish_step_over (struct lwp_info
*lwp
)
4714 if (lwp
->bp_reinsert
!= 0)
4716 struct thread_info
*saved_thread
= current_thread
;
4719 debug_printf ("Finished step over.\n");
4721 current_thread
= get_lwp_thread (lwp
);
4723 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4724 may be no breakpoint to reinsert there by now. */
4725 reinsert_breakpoints_at (lwp
->bp_reinsert
);
4726 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
4728 lwp
->bp_reinsert
= 0;
4730 /* Delete any single-step breakpoints. No longer needed. We
4731 don't have to worry about other threads hitting this trap,
4732 and later not being able to explain it, because we were
4733 stepping over a breakpoint, and we hold all threads but
4734 LWP stopped while doing that. */
4735 if (!can_hardware_single_step ())
4737 gdb_assert (has_single_step_breakpoints (current_thread
));
4738 delete_single_step_breakpoints (current_thread
);
4741 step_over_bkpt
= null_ptid
;
4742 current_thread
= saved_thread
;
4750 linux_process_target::complete_ongoing_step_over ()
4752 if (step_over_bkpt
!= null_ptid
)
4754 struct lwp_info
*lwp
;
4759 debug_printf ("detach: step over in progress, finish it first\n");
4761 /* Passing NULL_PTID as filter indicates we want all events to
4762 be left pending. Eventually this returns when there are no
4763 unwaited-for children left. */
4764 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
,
4766 gdb_assert (ret
== -1);
4768 lwp
= find_lwp_pid (step_over_bkpt
);
4770 finish_step_over (lwp
);
4771 step_over_bkpt
= null_ptid
;
4772 unsuspend_all_lwps (lwp
);
4777 linux_process_target::resume_one_thread (thread_info
*thread
,
4778 bool leave_all_stopped
)
4780 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4783 if (lwp
->resume
== NULL
)
4786 if (lwp
->resume
->kind
== resume_stop
)
4789 debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread
));
4794 debug_printf ("stopping LWP %ld\n", lwpid_of (thread
));
4796 /* Stop the thread, and wait for the event asynchronously,
4797 through the event loop. */
4803 debug_printf ("already stopped LWP %ld\n",
4806 /* The LWP may have been stopped in an internal event that
4807 was not meant to be notified back to GDB (e.g., gdbserver
4808 breakpoint), so we should be reporting a stop event in
4811 /* If the thread already has a pending SIGSTOP, this is a
4812 no-op. Otherwise, something later will presumably resume
4813 the thread and this will cause it to cancel any pending
4814 operation, due to last_resume_kind == resume_stop. If
4815 the thread already has a pending status to report, we
4816 will still report it the next time we wait - see
4817 status_pending_p_callback. */
4819 /* If we already have a pending signal to report, then
4820 there's no need to queue a SIGSTOP, as this means we're
4821 midway through moving the LWP out of the jumppad, and we
4822 will report the pending signal as soon as that is
4824 if (lwp
->pending_signals_to_report
== NULL
)
4828 /* For stop requests, we're done. */
4830 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4834 /* If this thread which is about to be resumed has a pending status,
4835 then don't resume it - we can just report the pending status.
4836 Likewise if it is suspended, because e.g., another thread is
4837 stepping past a breakpoint. Make sure to queue any signals that
4838 would otherwise be sent. In all-stop mode, we do this decision
4839 based on if *any* thread has a pending status. If there's a
4840 thread that needs the step-over-breakpoint dance, then don't
4841 resume any other thread but that particular one. */
4842 leave_pending
= (lwp
->suspended
4843 || lwp
->status_pending_p
4844 || leave_all_stopped
);
4846 /* If we have a new signal, enqueue the signal. */
4847 if (lwp
->resume
->sig
!= 0)
4849 siginfo_t info
, *info_p
;
4851 /* If this is the same signal we were previously stopped by,
4852 make sure to queue its siginfo. */
4853 if (WIFSTOPPED (lwp
->last_status
)
4854 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
4855 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
),
4856 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
4861 enqueue_pending_signal (lwp
, lwp
->resume
->sig
, info_p
);
4867 debug_printf ("resuming LWP %ld\n", lwpid_of (thread
));
4869 proceed_one_lwp (thread
, NULL
);
4874 debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread
));
4877 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4882 linux_process_target::resume (thread_resume
*resume_info
, size_t n
)
4884 struct thread_info
*need_step_over
= NULL
;
4889 debug_printf ("linux_resume:\n");
4892 for_each_thread ([&] (thread_info
*thread
)
4894 linux_set_resume_request (thread
, resume_info
, n
);
4897 /* If there is a thread which would otherwise be resumed, which has
4898 a pending status, then don't resume any threads - we can just
4899 report the pending status. Make sure to queue any signals that
4900 would otherwise be sent. In non-stop mode, we'll apply this
4901 logic to each thread individually. We consume all pending events
4902 before considering to start a step-over (in all-stop). */
4903 bool any_pending
= false;
4905 any_pending
= find_thread ([this] (thread_info
*thread
)
4907 return resume_status_pending (thread
);
4910 /* If there is a thread which would otherwise be resumed, which is
4911 stopped at a breakpoint that needs stepping over, then don't
4912 resume any threads - have it step over the breakpoint with all
4913 other threads stopped, then resume all threads again. Make sure
4914 to queue any signals that would otherwise be delivered or
4916 if (!any_pending
&& low_supports_breakpoints ())
4917 need_step_over
= find_thread ([this] (thread_info
*thread
)
4919 return thread_needs_step_over (thread
);
4922 bool leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
4926 if (need_step_over
!= NULL
)
4927 debug_printf ("Not resuming all, need step over\n");
4928 else if (any_pending
)
4929 debug_printf ("Not resuming, all-stop and found "
4930 "an LWP with pending status\n");
4932 debug_printf ("Resuming, no pending status or step over needed\n");
4935 /* Even if we're leaving threads stopped, queue all signals we'd
4936 otherwise deliver. */
4937 for_each_thread ([&] (thread_info
*thread
)
4939 resume_one_thread (thread
, leave_all_stopped
);
4943 start_step_over (get_thread_lwp (need_step_over
));
4947 debug_printf ("linux_resume done\n");
4951 /* We may have events that were pending that can/should be sent to
4952 the client now. Trigger a linux_wait call. */
4953 if (target_is_async_p ())
4958 linux_process_target::proceed_one_lwp (thread_info
*thread
, lwp_info
*except
)
4960 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4967 debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread
));
4972 debug_printf (" LWP %ld already running\n", lwpid_of (thread
));
4976 if (thread
->last_resume_kind
== resume_stop
4977 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4980 debug_printf (" client wants LWP to remain %ld stopped\n",
4985 if (lwp
->status_pending_p
)
4988 debug_printf (" LWP %ld has pending status, leaving stopped\n",
4993 gdb_assert (lwp
->suspended
>= 0);
4998 debug_printf (" LWP %ld is suspended\n", lwpid_of (thread
));
5002 if (thread
->last_resume_kind
== resume_stop
5003 && lwp
->pending_signals_to_report
== NULL
5004 && (lwp
->collecting_fast_tracepoint
5005 == fast_tpoint_collect_result::not_collecting
))
5007 /* We haven't reported this LWP as stopped yet (otherwise, the
5008 last_status.kind check above would catch it, and we wouldn't
5009 reach here. This LWP may have been momentarily paused by a
5010 stop_all_lwps call while handling for example, another LWP's
5011 step-over. In that case, the pending expected SIGSTOP signal
5012 that was queued at vCont;t handling time will have already
5013 been consumed by wait_for_sigstop, and so we need to requeue
5014 another one here. Note that if the LWP already has a SIGSTOP
5015 pending, this is a no-op. */
5018 debug_printf ("Client wants LWP %ld to stop. "
5019 "Making sure it has a SIGSTOP pending\n",
5025 if (thread
->last_resume_kind
== resume_step
)
5028 debug_printf (" stepping LWP %ld, client wants it stepping\n",
5031 /* If resume_step is requested by GDB, install single-step
5032 breakpoints when the thread is about to be actually resumed if
5033 the single-step breakpoints weren't removed. */
5034 if (supports_software_single_step ()
5035 && !has_single_step_breakpoints (thread
))
5036 install_software_single_step_breakpoints (lwp
);
5038 step
= maybe_hw_step (thread
);
5040 else if (lwp
->bp_reinsert
!= 0)
5043 debug_printf (" stepping LWP %ld, reinsert set\n",
5046 step
= maybe_hw_step (thread
);
5051 resume_one_lwp (lwp
, step
, 0, NULL
);
5055 linux_process_target::unsuspend_and_proceed_one_lwp (thread_info
*thread
,
5058 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5063 lwp_suspended_decr (lwp
);
5065 proceed_one_lwp (thread
, except
);
5069 linux_process_target::proceed_all_lwps ()
5071 struct thread_info
*need_step_over
;
5073 /* If there is a thread which would otherwise be resumed, which is
5074 stopped at a breakpoint that needs stepping over, then don't
5075 resume any threads - have it step over the breakpoint with all
5076 other threads stopped, then resume all threads again. */
5078 if (low_supports_breakpoints ())
5080 need_step_over
= find_thread ([this] (thread_info
*thread
)
5082 return thread_needs_step_over (thread
);
5085 if (need_step_over
!= NULL
)
5088 debug_printf ("proceed_all_lwps: found "
5089 "thread %ld needing a step-over\n",
5090 lwpid_of (need_step_over
));
5092 start_step_over (get_thread_lwp (need_step_over
));
5098 debug_printf ("Proceeding, no step-over needed\n");
5100 for_each_thread ([this] (thread_info
*thread
)
5102 proceed_one_lwp (thread
, NULL
);
5107 linux_process_target::unstop_all_lwps (int unsuspend
, lwp_info
*except
)
5113 debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
5114 lwpid_of (get_lwp_thread (except
)));
5116 debug_printf ("unstopping all lwps\n");
5120 for_each_thread ([&] (thread_info
*thread
)
5122 unsuspend_and_proceed_one_lwp (thread
, except
);
5125 for_each_thread ([&] (thread_info
*thread
)
5127 proceed_one_lwp (thread
, except
);
5132 debug_printf ("unstop_all_lwps done\n");
5138 #ifdef HAVE_LINUX_REGSETS
5140 #define use_linux_regsets 1
5142 /* Returns true if REGSET has been disabled. */
5145 regset_disabled (struct regsets_info
*info
, struct regset_info
*regset
)
5147 return (info
->disabled_regsets
!= NULL
5148 && info
->disabled_regsets
[regset
- info
->regsets
]);
5151 /* Disable REGSET. */
5154 disable_regset (struct regsets_info
*info
, struct regset_info
*regset
)
5158 dr_offset
= regset
- info
->regsets
;
5159 if (info
->disabled_regsets
== NULL
)
5160 info
->disabled_regsets
= (char *) xcalloc (1, info
->num_regsets
);
5161 info
->disabled_regsets
[dr_offset
] = 1;
5165 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
5166 struct regcache
*regcache
)
5168 struct regset_info
*regset
;
5169 int saw_general_regs
= 0;
5173 pid
= lwpid_of (current_thread
);
5174 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5179 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
5182 buf
= xmalloc (regset
->size
);
5184 nt_type
= regset
->nt_type
;
5188 iov
.iov_len
= regset
->size
;
5189 data
= (void *) &iov
;
5195 res
= ptrace (regset
->get_request
, pid
,
5196 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5198 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5203 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5205 /* If we get EIO on a regset, or an EINVAL and the regset is
5206 optional, do not try it again for this process mode. */
5207 disable_regset (regsets_info
, regset
);
5209 else if (errno
== ENODATA
)
5211 /* ENODATA may be returned if the regset is currently
5212 not "active". This can happen in normal operation,
5213 so suppress the warning in this case. */
5215 else if (errno
== ESRCH
)
5217 /* At this point, ESRCH should mean the process is
5218 already gone, in which case we simply ignore attempts
5219 to read its registers. */
5224 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
5231 if (regset
->type
== GENERAL_REGS
)
5232 saw_general_regs
= 1;
5233 regset
->store_function (regcache
, buf
);
5237 if (saw_general_regs
)
5244 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
5245 struct regcache
*regcache
)
5247 struct regset_info
*regset
;
5248 int saw_general_regs
= 0;
5252 pid
= lwpid_of (current_thread
);
5253 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5258 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
)
5259 || regset
->fill_function
== NULL
)
5262 buf
= xmalloc (regset
->size
);
5264 /* First fill the buffer with the current register set contents,
5265 in case there are any items in the kernel's regset that are
5266 not in gdbserver's regcache. */
5268 nt_type
= regset
->nt_type
;
5272 iov
.iov_len
= regset
->size
;
5273 data
= (void *) &iov
;
5279 res
= ptrace (regset
->get_request
, pid
,
5280 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5282 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5287 /* Then overlay our cached registers on that. */
5288 regset
->fill_function (regcache
, buf
);
5290 /* Only now do we write the register set. */
5292 res
= ptrace (regset
->set_request
, pid
,
5293 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5295 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
5302 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5304 /* If we get EIO on a regset, or an EINVAL and the regset is
5305 optional, do not try it again for this process mode. */
5306 disable_regset (regsets_info
, regset
);
5308 else if (errno
== ESRCH
)
5310 /* At this point, ESRCH should mean the process is
5311 already gone, in which case we simply ignore attempts
5312 to change its registers. See also the related
5313 comment in resume_one_lwp. */
5319 perror ("Warning: ptrace(regsets_store_inferior_registers)");
5322 else if (regset
->type
== GENERAL_REGS
)
5323 saw_general_regs
= 1;
5326 if (saw_general_regs
)
5332 #else /* !HAVE_LINUX_REGSETS */
5334 #define use_linux_regsets 0
5335 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5336 #define regsets_store_inferior_registers(regsets_info, regcache) 1
5340 /* Return 1 if register REGNO is supported by one of the regset ptrace
5341 calls or 0 if it has to be transferred individually. */
5344 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
5346 unsigned char mask
= 1 << (regno
% 8);
5347 size_t index
= regno
/ 8;
5349 return (use_linux_regsets
5350 && (regs_info
->regset_bitmap
== NULL
5351 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
5354 #ifdef HAVE_LINUX_USRREGS
5357 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
5361 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
5362 error ("Invalid register number %d.", regnum
);
5364 addr
= usrregs
->regmap
[regnum
];
5371 linux_process_target::fetch_register (const usrregs_info
*usrregs
,
5372 regcache
*regcache
, int regno
)
5379 if (regno
>= usrregs
->num_regs
)
5381 if (low_cannot_fetch_register (regno
))
5384 regaddr
= register_addr (usrregs
, regno
);
5388 size
= ((register_size (regcache
->tdesc
, regno
)
5389 + sizeof (PTRACE_XFER_TYPE
) - 1)
5390 & -sizeof (PTRACE_XFER_TYPE
));
5391 buf
= (char *) alloca (size
);
5393 pid
= lwpid_of (current_thread
);
5394 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5397 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
5398 ptrace (PTRACE_PEEKUSER
, pid
,
5399 /* Coerce to a uintptr_t first to avoid potential gcc warning
5400 of coercing an 8 byte integer to a 4 byte pointer. */
5401 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
, (PTRACE_TYPE_ARG4
) 0);
5402 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5405 /* Mark register REGNO unavailable. */
5406 supply_register (regcache
, regno
, NULL
);
5411 if (the_low_target
.supply_ptrace_register
)
5412 the_low_target
.supply_ptrace_register (regcache
, regno
, buf
);
5414 supply_register (regcache
, regno
, buf
);
5418 linux_process_target::store_register (const usrregs_info
*usrregs
,
5419 regcache
*regcache
, int regno
)
5426 if (regno
>= usrregs
->num_regs
)
5428 if (low_cannot_store_register (regno
))
5431 regaddr
= register_addr (usrregs
, regno
);
5435 size
= ((register_size (regcache
->tdesc
, regno
)
5436 + sizeof (PTRACE_XFER_TYPE
) - 1)
5437 & -sizeof (PTRACE_XFER_TYPE
));
5438 buf
= (char *) alloca (size
);
5439 memset (buf
, 0, size
);
5441 if (the_low_target
.collect_ptrace_register
)
5442 the_low_target
.collect_ptrace_register (regcache
, regno
, buf
);
5444 collect_register (regcache
, regno
, buf
);
5446 pid
= lwpid_of (current_thread
);
5447 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5450 ptrace (PTRACE_POKEUSER
, pid
,
5451 /* Coerce to a uintptr_t first to avoid potential gcc warning
5452 about coercing an 8 byte integer to a 4 byte pointer. */
5453 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
,
5454 (PTRACE_TYPE_ARG4
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
5457 /* At this point, ESRCH should mean the process is
5458 already gone, in which case we simply ignore attempts
5459 to change its registers. See also the related
5460 comment in resume_one_lwp. */
5465 if (!low_cannot_store_register (regno
))
5466 error ("writing register %d: %s", regno
, safe_strerror (errno
));
5468 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5471 #endif /* HAVE_LINUX_USRREGS */
5474 linux_process_target::usr_fetch_inferior_registers (const regs_info
*regs_info
,
5478 #ifdef HAVE_LINUX_USRREGS
5479 struct usrregs_info
*usr
= regs_info
->usrregs
;
5483 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5484 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5485 fetch_register (usr
, regcache
, regno
);
5488 fetch_register (usr
, regcache
, regno
);
5493 linux_process_target::usr_store_inferior_registers (const regs_info
*regs_info
,
5497 #ifdef HAVE_LINUX_USRREGS
5498 struct usrregs_info
*usr
= regs_info
->usrregs
;
5502 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5503 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5504 store_register (usr
, regcache
, regno
);
5507 store_register (usr
, regcache
, regno
);
5512 linux_process_target::fetch_registers (regcache
*regcache
, int regno
)
5516 const regs_info
*regs_info
= get_regs_info ();
5520 if (regs_info
->usrregs
!= NULL
)
5521 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
5522 low_fetch_register (regcache
, regno
);
5524 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
5525 if (regs_info
->usrregs
!= NULL
)
5526 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
5530 if (low_fetch_register (regcache
, regno
))
5533 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5535 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
5537 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5538 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
5543 linux_process_target::store_registers (regcache
*regcache
, int regno
)
5547 const regs_info
*regs_info
= get_regs_info ();
5551 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5553 if (regs_info
->usrregs
!= NULL
)
5554 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
5558 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5560 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5562 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5563 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
5568 linux_process_target::low_fetch_register (regcache
*regcache
, int regno
)
5573 /* A wrapper for the read_memory target op. */
5576 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
5578 return the_target
->read_memory (memaddr
, myaddr
, len
);
5581 /* Copy LEN bytes from inferior's memory starting at MEMADDR
5582 to debugger memory starting at MYADDR. */
5585 linux_process_target::read_memory (CORE_ADDR memaddr
,
5586 unsigned char *myaddr
, int len
)
5588 int pid
= lwpid_of (current_thread
);
5589 PTRACE_XFER_TYPE
*buffer
;
5597 /* Try using /proc. Don't bother for one word. */
5598 if (len
>= 3 * sizeof (long))
5602 /* We could keep this file open and cache it - possibly one per
5603 thread. That requires some juggling, but is even faster. */
5604 sprintf (filename
, "/proc/%d/mem", pid
);
5605 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
5609 /* If pread64 is available, use it. It's faster if the kernel
5610 supports it (only one syscall), and it's 64-bit safe even on
5611 32-bit platforms (for instance, SPARC debugging a SPARC64
5614 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
5617 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
5618 bytes
= read (fd
, myaddr
, len
);
5625 /* Some data was read, we'll try to get the rest with ptrace. */
5635 /* Round starting address down to longword boundary. */
5636 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5637 /* Round ending address up; get number of longwords that makes. */
5638 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5639 / sizeof (PTRACE_XFER_TYPE
));
5640 /* Allocate buffer of that many longwords. */
5641 buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5643 /* Read all the longwords */
5645 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5647 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5648 about coercing an 8 byte integer to a 4 byte pointer. */
5649 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
5650 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5651 (PTRACE_TYPE_ARG4
) 0);
5657 /* Copy appropriate bytes out of the buffer. */
5660 i
*= sizeof (PTRACE_XFER_TYPE
);
5661 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
5663 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5670 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5671 memory at MEMADDR. On failure (cannot write to the inferior)
5672 returns the value of errno. Always succeeds if LEN is zero. */
5675 linux_process_target::write_memory (CORE_ADDR memaddr
,
5676 const unsigned char *myaddr
, int len
)
5679 /* Round starting address down to longword boundary. */
5680 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5681 /* Round ending address up; get number of longwords that makes. */
5683 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5684 / sizeof (PTRACE_XFER_TYPE
);
5686 /* Allocate buffer of that many longwords. */
5687 PTRACE_XFER_TYPE
*buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5689 int pid
= lwpid_of (current_thread
);
5693 /* Zero length write always succeeds. */
5699 /* Dump up to four bytes. */
5700 char str
[4 * 2 + 1];
5702 int dump
= len
< 4 ? len
: 4;
5704 for (i
= 0; i
< dump
; i
++)
5706 sprintf (p
, "%02x", myaddr
[i
]);
5711 debug_printf ("Writing %s to 0x%08lx in process %d\n",
5712 str
, (long) memaddr
, pid
);
5715 /* Fill start and end extra bytes of buffer with existing memory data. */
5718 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5719 about coercing an 8 byte integer to a 4 byte pointer. */
5720 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
5721 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5722 (PTRACE_TYPE_ARG4
) 0);
5730 = ptrace (PTRACE_PEEKTEXT
, pid
,
5731 /* Coerce to a uintptr_t first to avoid potential gcc warning
5732 about coercing an 8 byte integer to a 4 byte pointer. */
5733 (PTRACE_TYPE_ARG3
) (uintptr_t) (addr
+ (count
- 1)
5734 * sizeof (PTRACE_XFER_TYPE
)),
5735 (PTRACE_TYPE_ARG4
) 0);
5740 /* Copy data to be written over corresponding part of buffer. */
5742 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5745 /* Write the entire buffer. */
5747 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5750 ptrace (PTRACE_POKETEXT
, pid
,
5751 /* Coerce to a uintptr_t first to avoid potential gcc warning
5752 about coercing an 8 byte integer to a 4 byte pointer. */
5753 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5754 (PTRACE_TYPE_ARG4
) buffer
[i
]);
5763 linux_process_target::look_up_symbols ()
5765 #ifdef USE_THREAD_DB
5766 struct process_info
*proc
= current_process ();
5768 if (proc
->priv
->thread_db
!= NULL
)
5776 linux_process_target::request_interrupt ()
5778 /* Send a SIGINT to the process group. This acts just like the user
5779 typed a ^C on the controlling terminal. */
5780 ::kill (-signal_pid
, SIGINT
);
5784 linux_process_target::supports_read_auxv ()
5789 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5790 to debugger memory starting at MYADDR. */
5793 linux_process_target::read_auxv (CORE_ADDR offset
, unsigned char *myaddr
,
5796 char filename
[PATH_MAX
];
5798 int pid
= lwpid_of (current_thread
);
5800 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5802 fd
= open (filename
, O_RDONLY
);
5806 if (offset
!= (CORE_ADDR
) 0
5807 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5810 n
= read (fd
, myaddr
, len
);
5818 linux_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5819 int size
, raw_breakpoint
*bp
)
5821 if (type
== raw_bkpt_type_sw
)
5822 return insert_memory_breakpoint (bp
);
5824 return low_insert_point (type
, addr
, size
, bp
);
5828 linux_process_target::low_insert_point (raw_bkpt_type type
, CORE_ADDR addr
,
5829 int size
, raw_breakpoint
*bp
)
5831 /* Unsupported (see target.h). */
5836 linux_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5837 int size
, raw_breakpoint
*bp
)
5839 if (type
== raw_bkpt_type_sw
)
5840 return remove_memory_breakpoint (bp
);
5842 return low_remove_point (type
, addr
, size
, bp
);
5846 linux_process_target::low_remove_point (raw_bkpt_type type
, CORE_ADDR addr
,
5847 int size
, raw_breakpoint
*bp
)
5849 /* Unsupported (see target.h). */
5853 /* Implement the stopped_by_sw_breakpoint target_ops
5857 linux_process_target::stopped_by_sw_breakpoint ()
5859 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5861 return (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
);
5864 /* Implement the supports_stopped_by_sw_breakpoint target_ops
5868 linux_process_target::supports_stopped_by_sw_breakpoint ()
5870 return USE_SIGTRAP_SIGINFO
;
5873 /* Implement the stopped_by_hw_breakpoint target_ops
5877 linux_process_target::stopped_by_hw_breakpoint ()
5879 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5881 return (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
);
5884 /* Implement the supports_stopped_by_hw_breakpoint target_ops
5888 linux_process_target::supports_stopped_by_hw_breakpoint ()
5890 return USE_SIGTRAP_SIGINFO
;
5893 /* Implement the supports_hardware_single_step target_ops method. */
5896 linux_process_target::supports_hardware_single_step ()
5898 return can_hardware_single_step ();
5902 linux_process_target::stopped_by_watchpoint ()
5904 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5906 return lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
5910 linux_process_target::stopped_data_address ()
5912 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5914 return lwp
->stopped_data_address
;
5917 /* This is only used for targets that define PT_TEXT_ADDR,
5918 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5919 the target has different ways of acquiring this information, like
5923 linux_process_target::supports_read_offsets ()
5925 #ifdef SUPPORTS_READ_OFFSETS
5932 /* Under uClinux, programs are loaded at non-zero offsets, which we need
5933 to tell gdb about. */
5936 linux_process_target::read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
5938 #ifdef SUPPORTS_READ_OFFSETS
5939 unsigned long text
, text_end
, data
;
5940 int pid
= lwpid_of (current_thread
);
5944 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_ADDR
,
5945 (PTRACE_TYPE_ARG4
) 0);
5946 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_END_ADDR
,
5947 (PTRACE_TYPE_ARG4
) 0);
5948 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_DATA_ADDR
,
5949 (PTRACE_TYPE_ARG4
) 0);
5953 /* Both text and data offsets produced at compile-time (and so
5954 used by gdb) are relative to the beginning of the program,
5955 with the data segment immediately following the text segment.
5956 However, the actual runtime layout in memory may put the data
5957 somewhere else, so when we send gdb a data base-address, we
5958 use the real data base address and subtract the compile-time
5959 data base-address from it (which is just the length of the
5960 text segment). BSS immediately follows data in both
5963 *data_p
= data
- (text_end
- text
);
5969 gdb_assert_not_reached ("target op read_offsets not supported");
5974 linux_process_target::supports_get_tls_address ()
5976 #ifdef USE_THREAD_DB
5984 linux_process_target::get_tls_address (thread_info
*thread
,
5986 CORE_ADDR load_module
,
5989 #ifdef USE_THREAD_DB
5990 return thread_db_get_tls_address (thread
, offset
, load_module
, address
);
5997 linux_process_target::supports_qxfer_osdata ()
6003 linux_process_target::qxfer_osdata (const char *annex
,
6004 unsigned char *readbuf
,
6005 unsigned const char *writebuf
,
6006 CORE_ADDR offset
, int len
)
6008 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
6011 /* Convert a native/host siginfo object, into/from the siginfo in the
6012 layout of the inferiors' architecture. */
6015 siginfo_fixup (siginfo_t
*siginfo
, gdb_byte
*inf_siginfo
, int direction
)
6019 if (the_low_target
.siginfo_fixup
!= NULL
)
6020 done
= the_low_target
.siginfo_fixup (siginfo
, inf_siginfo
, direction
);
6022 /* If there was no callback, or the callback didn't do anything,
6023 then just do a straight memcpy. */
6027 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
6029 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
6034 linux_process_target::supports_qxfer_siginfo ()
6040 linux_process_target::qxfer_siginfo (const char *annex
,
6041 unsigned char *readbuf
,
6042 unsigned const char *writebuf
,
6043 CORE_ADDR offset
, int len
)
6047 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
6049 if (current_thread
== NULL
)
6052 pid
= lwpid_of (current_thread
);
6055 debug_printf ("%s siginfo for lwp %d.\n",
6056 readbuf
!= NULL
? "Reading" : "Writing",
6059 if (offset
>= sizeof (siginfo
))
6062 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6065 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
6066 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
6067 inferior with a 64-bit GDBSERVER should look the same as debugging it
6068 with a 32-bit GDBSERVER, we need to convert it. */
6069 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
6071 if (offset
+ len
> sizeof (siginfo
))
6072 len
= sizeof (siginfo
) - offset
;
6074 if (readbuf
!= NULL
)
6075 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
6078 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
6080 /* Convert back to ptrace layout before flushing it out. */
6081 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
6083 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6090 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
6091 so we notice when children change state; as the handler for the
6092 sigsuspend in my_waitpid. */
6095 sigchld_handler (int signo
)
6097 int old_errno
= errno
;
6103 /* Use the async signal safe debug function. */
6104 if (debug_write ("sigchld_handler\n",
6105 sizeof ("sigchld_handler\n") - 1) < 0)
6106 break; /* just ignore */
6110 if (target_is_async_p ())
6111 async_file_mark (); /* trigger a linux_wait */
6117 linux_process_target::supports_non_stop ()
6123 linux_process_target::async (bool enable
)
6125 bool previous
= target_is_async_p ();
6128 debug_printf ("linux_async (%d), previous=%d\n",
6131 if (previous
!= enable
)
6134 sigemptyset (&mask
);
6135 sigaddset (&mask
, SIGCHLD
);
6137 gdb_sigmask (SIG_BLOCK
, &mask
, NULL
);
6141 if (pipe (linux_event_pipe
) == -1)
6143 linux_event_pipe
[0] = -1;
6144 linux_event_pipe
[1] = -1;
6145 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6147 warning ("creating event pipe failed.");
6151 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
6152 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
6154 /* Register the event loop handler. */
6155 add_file_handler (linux_event_pipe
[0],
6156 handle_target_event
, NULL
);
6158 /* Always trigger a linux_wait. */
6163 delete_file_handler (linux_event_pipe
[0]);
6165 close (linux_event_pipe
[0]);
6166 close (linux_event_pipe
[1]);
6167 linux_event_pipe
[0] = -1;
6168 linux_event_pipe
[1] = -1;
6171 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6178 linux_process_target::start_non_stop (bool nonstop
)
6180 /* Register or unregister from event-loop accordingly. */
6181 target_async (nonstop
);
6183 if (target_is_async_p () != (nonstop
!= false))
6190 linux_process_target::supports_multi_process ()
6195 /* Check if fork events are supported. */
6198 linux_process_target::supports_fork_events ()
6200 return linux_supports_tracefork ();
6203 /* Check if vfork events are supported. */
6206 linux_process_target::supports_vfork_events ()
6208 return linux_supports_tracefork ();
6211 /* Check if exec events are supported. */
6214 linux_process_target::supports_exec_events ()
6216 return linux_supports_traceexec ();
6219 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
6220 ptrace flags for all inferiors. This is in case the new GDB connection
6221 doesn't support the same set of events that the previous one did. */
6224 linux_process_target::handle_new_gdb_connection ()
6226 /* Request that all the lwps reset their ptrace options. */
6227 for_each_thread ([] (thread_info
*thread
)
6229 struct lwp_info
*lwp
= get_thread_lwp (thread
);
6233 /* Stop the lwp so we can modify its ptrace options. */
6234 lwp
->must_set_ptrace_flags
= 1;
6235 linux_stop_lwp (lwp
);
6239 /* Already stopped; go ahead and set the ptrace options. */
6240 struct process_info
*proc
= find_process_pid (pid_of (thread
));
6241 int options
= linux_low_ptrace_options (proc
->attached
);
6243 linux_enable_event_reporting (lwpid_of (thread
), options
);
6244 lwp
->must_set_ptrace_flags
= 0;
6250 linux_process_target::handle_monitor_command (char *mon
)
6252 #ifdef USE_THREAD_DB
6253 return thread_db_handle_monitor_command (mon
);
6260 linux_process_target::core_of_thread (ptid_t ptid
)
6262 return linux_common_core_of_thread (ptid
);
6266 linux_process_target::supports_disable_randomization ()
6268 #ifdef HAVE_PERSONALITY
6276 linux_process_target::supports_agent ()
6282 linux_process_target::supports_range_stepping ()
6284 if (supports_software_single_step ())
6286 if (*the_low_target
.supports_range_stepping
== NULL
)
6289 return (*the_low_target
.supports_range_stepping
) ();
6293 linux_process_target::supports_pid_to_exec_file ()
6299 linux_process_target::pid_to_exec_file (int pid
)
6301 return linux_proc_pid_to_exec_file (pid
);
6305 linux_process_target::supports_multifs ()
6311 linux_process_target::multifs_open (int pid
, const char *filename
,
6312 int flags
, mode_t mode
)
6314 return linux_mntns_open_cloexec (pid
, filename
, flags
, mode
);
6318 linux_process_target::multifs_unlink (int pid
, const char *filename
)
6320 return linux_mntns_unlink (pid
, filename
);
6324 linux_process_target::multifs_readlink (int pid
, const char *filename
,
6325 char *buf
, size_t bufsiz
)
6327 return linux_mntns_readlink (pid
, filename
, buf
, bufsiz
);
6330 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
6331 struct target_loadseg
6333 /* Core address to which the segment is mapped. */
6335 /* VMA recorded in the program header. */
6337 /* Size of this segment in memory. */
6341 # if defined PT_GETDSBT
6342 struct target_loadmap
6344 /* Protocol version number, must be zero. */
6346 /* Pointer to the DSBT table, its size, and the DSBT index. */
6347 unsigned *dsbt_table
;
6348 unsigned dsbt_size
, dsbt_index
;
6349 /* Number of segments in this map. */
6351 /* The actual memory map. */
6352 struct target_loadseg segs
[/*nsegs*/];
6354 # define LINUX_LOADMAP PT_GETDSBT
6355 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6356 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6358 struct target_loadmap
6360 /* Protocol version number, must be zero. */
6362 /* Number of segments in this map. */
6364 /* The actual memory map. */
6365 struct target_loadseg segs
[/*nsegs*/];
6367 # define LINUX_LOADMAP PTRACE_GETFDPIC
6368 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6369 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6373 linux_process_target::supports_read_loadmap ()
6379 linux_process_target::read_loadmap (const char *annex
, CORE_ADDR offset
,
6380 unsigned char *myaddr
, unsigned int len
)
6382 int pid
= lwpid_of (current_thread
);
6384 struct target_loadmap
*data
= NULL
;
6385 unsigned int actual_length
, copy_length
;
6387 if (strcmp (annex
, "exec") == 0)
6388 addr
= (int) LINUX_LOADMAP_EXEC
;
6389 else if (strcmp (annex
, "interp") == 0)
6390 addr
= (int) LINUX_LOADMAP_INTERP
;
6394 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
6400 actual_length
= sizeof (struct target_loadmap
)
6401 + sizeof (struct target_loadseg
) * data
->nsegs
;
6403 if (offset
< 0 || offset
> actual_length
)
6406 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
6407 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
6410 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6413 linux_process_target::process_qsupported (char **features
, int count
)
6415 if (the_low_target
.process_qsupported
!= NULL
)
6416 the_low_target
.process_qsupported (features
, count
);
6420 linux_process_target::supports_catch_syscall ()
6422 return (the_low_target
.get_syscall_trapinfo
!= NULL
6423 && linux_supports_tracesysgood ());
6427 linux_process_target::get_ipa_tdesc_idx ()
6429 if (the_low_target
.get_ipa_tdesc_idx
== NULL
)
6432 return (*the_low_target
.get_ipa_tdesc_idx
) ();
6436 linux_process_target::supports_tracepoints ()
6438 if (*the_low_target
.supports_tracepoints
== NULL
)
6441 return (*the_low_target
.supports_tracepoints
) ();
6445 linux_process_target::read_pc (regcache
*regcache
)
6447 if (!low_supports_breakpoints ())
6450 return low_get_pc (regcache
);
6454 linux_process_target::write_pc (regcache
*regcache
, CORE_ADDR pc
)
6456 gdb_assert (low_supports_breakpoints ());
6458 low_set_pc (regcache
, pc
);
6462 linux_process_target::supports_thread_stopped ()
6468 linux_process_target::thread_stopped (thread_info
*thread
)
6470 return get_thread_lwp (thread
)->stopped
;
6473 /* This exposes stop-all-threads functionality to other modules. */
6476 linux_process_target::pause_all (bool freeze
)
6478 stop_all_lwps (freeze
, NULL
);
6481 /* This exposes unstop-all-threads functionality to other gdbserver
6485 linux_process_target::unpause_all (bool unfreeze
)
6487 unstop_all_lwps (unfreeze
, NULL
);
6491 linux_process_target::prepare_to_access_memory ()
6493 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6496 target_pause_all (true);
6501 linux_process_target::done_accessing_memory ()
6503 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6506 target_unpause_all (true);
6510 linux_process_target::supports_fast_tracepoints ()
6512 return the_low_target
.install_fast_tracepoint_jump_pad
!= nullptr;
6516 linux_process_target::install_fast_tracepoint_jump_pad
6517 (CORE_ADDR tpoint
, CORE_ADDR tpaddr
, CORE_ADDR collector
,
6518 CORE_ADDR lockaddr
, ULONGEST orig_size
, CORE_ADDR
*jump_entry
,
6519 CORE_ADDR
*trampoline
, ULONGEST
*trampoline_size
,
6520 unsigned char *jjump_pad_insn
, ULONGEST
*jjump_pad_insn_size
,
6521 CORE_ADDR
*adjusted_insn_addr
, CORE_ADDR
*adjusted_insn_addr_end
,
6524 return (*the_low_target
.install_fast_tracepoint_jump_pad
)
6525 (tpoint
, tpaddr
, collector
, lockaddr
, orig_size
,
6526 jump_entry
, trampoline
, trampoline_size
,
6527 jjump_pad_insn
, jjump_pad_insn_size
,
6528 adjusted_insn_addr
, adjusted_insn_addr_end
,
6533 linux_process_target::emit_ops ()
6535 if (the_low_target
.emit_ops
!= NULL
)
6536 return (*the_low_target
.emit_ops
) ();
6542 linux_process_target::get_min_fast_tracepoint_insn_len ()
6544 return (*the_low_target
.get_min_fast_tracepoint_insn_len
) ();
6547 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6550 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
6551 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
6553 char filename
[PATH_MAX
];
6555 const int auxv_size
= is_elf64
6556 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
6557 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
6559 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
6561 fd
= open (filename
, O_RDONLY
);
6567 while (read (fd
, buf
, auxv_size
) == auxv_size
6568 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
6572 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
6574 switch (aux
->a_type
)
6577 *phdr_memaddr
= aux
->a_un
.a_val
;
6580 *num_phdr
= aux
->a_un
.a_val
;
6586 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
6588 switch (aux
->a_type
)
6591 *phdr_memaddr
= aux
->a_un
.a_val
;
6594 *num_phdr
= aux
->a_un
.a_val
;
6602 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
6604 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6605 "phdr_memaddr = %ld, phdr_num = %d",
6606 (long) *phdr_memaddr
, *num_phdr
);
6613 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6616 get_dynamic (const int pid
, const int is_elf64
)
6618 CORE_ADDR phdr_memaddr
, relocation
;
6620 unsigned char *phdr_buf
;
6621 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
6623 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
6626 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
6627 phdr_buf
= (unsigned char *) alloca (num_phdr
* phdr_size
);
6629 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
6632 /* Compute relocation: it is expected to be 0 for "regular" executables,
6633 non-zero for PIE ones. */
6635 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
6638 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6640 if (p
->p_type
== PT_PHDR
)
6641 relocation
= phdr_memaddr
- p
->p_vaddr
;
6645 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6647 if (p
->p_type
== PT_PHDR
)
6648 relocation
= phdr_memaddr
- p
->p_vaddr
;
6651 if (relocation
== -1)
6653 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6654 any real world executables, including PIE executables, have always
6655 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6656 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6657 or present DT_DEBUG anyway (fpc binaries are statically linked).
6659 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6661 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6666 for (i
= 0; i
< num_phdr
; i
++)
6670 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6672 if (p
->p_type
== PT_DYNAMIC
)
6673 return p
->p_vaddr
+ relocation
;
6677 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6679 if (p
->p_type
== PT_DYNAMIC
)
6680 return p
->p_vaddr
+ relocation
;
6687 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6688 can be 0 if the inferior does not yet have the library list initialized.
6689 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6690 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6693 get_r_debug (const int pid
, const int is_elf64
)
6695 CORE_ADDR dynamic_memaddr
;
6696 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
6697 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
6700 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
6701 if (dynamic_memaddr
== 0)
6704 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
6708 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
6709 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6713 unsigned char buf
[sizeof (Elf64_Xword
)];
6717 #ifdef DT_MIPS_RLD_MAP
6718 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6720 if (linux_read_memory (dyn
->d_un
.d_val
,
6721 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6726 #endif /* DT_MIPS_RLD_MAP */
6727 #ifdef DT_MIPS_RLD_MAP_REL
6728 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6730 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6731 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6736 #endif /* DT_MIPS_RLD_MAP_REL */
6738 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6739 map
= dyn
->d_un
.d_val
;
6741 if (dyn
->d_tag
== DT_NULL
)
6746 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
6747 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6751 unsigned char buf
[sizeof (Elf32_Word
)];
6755 #ifdef DT_MIPS_RLD_MAP
6756 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6758 if (linux_read_memory (dyn
->d_un
.d_val
,
6759 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6764 #endif /* DT_MIPS_RLD_MAP */
6765 #ifdef DT_MIPS_RLD_MAP_REL
6766 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6768 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6769 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6774 #endif /* DT_MIPS_RLD_MAP_REL */
6776 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6777 map
= dyn
->d_un
.d_val
;
6779 if (dyn
->d_tag
== DT_NULL
)
6783 dynamic_memaddr
+= dyn_size
;
6789 /* Read one pointer from MEMADDR in the inferior. */
6792 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
6796 /* Go through a union so this works on either big or little endian
6797 hosts, when the inferior's pointer size is smaller than the size
6798 of CORE_ADDR. It is assumed the inferior's endianness is the
6799 same of the superior's. */
6802 CORE_ADDR core_addr
;
6807 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
6810 if (ptr_size
== sizeof (CORE_ADDR
))
6811 *ptr
= addr
.core_addr
;
6812 else if (ptr_size
== sizeof (unsigned int))
6815 gdb_assert_not_reached ("unhandled pointer size");
6821 linux_process_target::supports_qxfer_libraries_svr4 ()
6826 struct link_map_offsets
6828 /* Offset and size of r_debug.r_version. */
6829 int r_version_offset
;
6831 /* Offset and size of r_debug.r_map. */
6834 /* Offset to l_addr field in struct link_map. */
6837 /* Offset to l_name field in struct link_map. */
6840 /* Offset to l_ld field in struct link_map. */
6843 /* Offset to l_next field in struct link_map. */
6846 /* Offset to l_prev field in struct link_map. */
6850 /* Construct qXfer:libraries-svr4:read reply. */
6853 linux_process_target::qxfer_libraries_svr4 (const char *annex
,
6854 unsigned char *readbuf
,
6855 unsigned const char *writebuf
,
6856 CORE_ADDR offset
, int len
)
6858 struct process_info_private
*const priv
= current_process ()->priv
;
6859 char filename
[PATH_MAX
];
6862 static const struct link_map_offsets lmo_32bit_offsets
=
6864 0, /* r_version offset. */
6865 4, /* r_debug.r_map offset. */
6866 0, /* l_addr offset in link_map. */
6867 4, /* l_name offset in link_map. */
6868 8, /* l_ld offset in link_map. */
6869 12, /* l_next offset in link_map. */
6870 16 /* l_prev offset in link_map. */
6873 static const struct link_map_offsets lmo_64bit_offsets
=
6875 0, /* r_version offset. */
6876 8, /* r_debug.r_map offset. */
6877 0, /* l_addr offset in link_map. */
6878 8, /* l_name offset in link_map. */
6879 16, /* l_ld offset in link_map. */
6880 24, /* l_next offset in link_map. */
6881 32 /* l_prev offset in link_map. */
6883 const struct link_map_offsets
*lmo
;
6884 unsigned int machine
;
6886 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
6887 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
6888 int header_done
= 0;
6890 if (writebuf
!= NULL
)
6892 if (readbuf
== NULL
)
6895 pid
= lwpid_of (current_thread
);
6896 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
6897 is_elf64
= elf_64_file_p (filename
, &machine
);
6898 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
6899 ptr_size
= is_elf64
? 8 : 4;
6901 while (annex
[0] != '\0')
6907 sep
= strchr (annex
, '=');
6911 name_len
= sep
- annex
;
6912 if (name_len
== 5 && startswith (annex
, "start"))
6914 else if (name_len
== 4 && startswith (annex
, "prev"))
6918 annex
= strchr (sep
, ';');
6925 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
6932 if (priv
->r_debug
== 0)
6933 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
6935 /* We failed to find DT_DEBUG. Such situation will not change
6936 for this inferior - do not retry it. Report it to GDB as
6937 E01, see for the reasons at the GDB solib-svr4.c side. */
6938 if (priv
->r_debug
== (CORE_ADDR
) -1)
6941 if (priv
->r_debug
!= 0)
6943 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
6944 (unsigned char *) &r_version
,
6945 sizeof (r_version
)) != 0
6948 warning ("unexpected r_debug version %d", r_version
);
6950 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
6951 &lm_addr
, ptr_size
) != 0)
6953 warning ("unable to read r_map from 0x%lx",
6954 (long) priv
->r_debug
+ lmo
->r_map_offset
);
6959 std::string document
= "<library-list-svr4 version=\"1.0\"";
6962 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
6963 &l_name
, ptr_size
) == 0
6964 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
6965 &l_addr
, ptr_size
) == 0
6966 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
6967 &l_ld
, ptr_size
) == 0
6968 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
6969 &l_prev
, ptr_size
) == 0
6970 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
6971 &l_next
, ptr_size
) == 0)
6973 unsigned char libname
[PATH_MAX
];
6975 if (lm_prev
!= l_prev
)
6977 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
6978 (long) lm_prev
, (long) l_prev
);
6982 /* Ignore the first entry even if it has valid name as the first entry
6983 corresponds to the main executable. The first entry should not be
6984 skipped if the dynamic loader was loaded late by a static executable
6985 (see solib-svr4.c parameter ignore_first). But in such case the main
6986 executable does not have PT_DYNAMIC present and this function already
6987 exited above due to failed get_r_debug. */
6989 string_appendf (document
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
6992 /* Not checking for error because reading may stop before
6993 we've got PATH_MAX worth of characters. */
6995 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
6996 libname
[sizeof (libname
) - 1] = '\0';
6997 if (libname
[0] != '\0')
7001 /* Terminate `<library-list-svr4'. */
7006 string_appendf (document
, "<library name=\"");
7007 xml_escape_text_append (&document
, (char *) libname
);
7008 string_appendf (document
, "\" lm=\"0x%lx\" "
7009 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
7010 (unsigned long) lm_addr
, (unsigned long) l_addr
,
7011 (unsigned long) l_ld
);
7021 /* Empty list; terminate `<library-list-svr4'. */
7025 document
+= "</library-list-svr4>";
7027 int document_len
= document
.length ();
7028 if (offset
< document_len
)
7029 document_len
-= offset
;
7032 if (len
> document_len
)
7035 memcpy (readbuf
, document
.data () + offset
, len
);
7040 #ifdef HAVE_LINUX_BTRACE
7042 btrace_target_info
*
7043 linux_process_target::enable_btrace (ptid_t ptid
,
7044 const btrace_config
*conf
)
7046 return linux_enable_btrace (ptid
, conf
);
7049 /* See to_disable_btrace target method. */
7052 linux_process_target::disable_btrace (btrace_target_info
*tinfo
)
7054 enum btrace_error err
;
7056 err
= linux_disable_btrace (tinfo
);
7057 return (err
== BTRACE_ERR_NONE
? 0 : -1);
7060 /* Encode an Intel Processor Trace configuration. */
7063 linux_low_encode_pt_config (struct buffer
*buffer
,
7064 const struct btrace_data_pt_config
*config
)
7066 buffer_grow_str (buffer
, "<pt-config>\n");
7068 switch (config
->cpu
.vendor
)
7071 buffer_xml_printf (buffer
, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
7072 "model=\"%u\" stepping=\"%u\"/>\n",
7073 config
->cpu
.family
, config
->cpu
.model
,
7074 config
->cpu
.stepping
);
7081 buffer_grow_str (buffer
, "</pt-config>\n");
7084 /* Encode a raw buffer. */
7087 linux_low_encode_raw (struct buffer
*buffer
, const gdb_byte
*data
,
7093 /* We use hex encoding - see gdbsupport/rsp-low.h. */
7094 buffer_grow_str (buffer
, "<raw>\n");
7100 elem
[0] = tohex ((*data
>> 4) & 0xf);
7101 elem
[1] = tohex (*data
++ & 0xf);
7103 buffer_grow (buffer
, elem
, 2);
7106 buffer_grow_str (buffer
, "</raw>\n");
7109 /* See to_read_btrace target method. */
7112 linux_process_target::read_btrace (btrace_target_info
*tinfo
,
7114 enum btrace_read_type type
)
7116 struct btrace_data btrace
;
7117 enum btrace_error err
;
7119 err
= linux_read_btrace (&btrace
, tinfo
, type
);
7120 if (err
!= BTRACE_ERR_NONE
)
7122 if (err
== BTRACE_ERR_OVERFLOW
)
7123 buffer_grow_str0 (buffer
, "E.Overflow.");
7125 buffer_grow_str0 (buffer
, "E.Generic Error.");
7130 switch (btrace
.format
)
7132 case BTRACE_FORMAT_NONE
:
7133 buffer_grow_str0 (buffer
, "E.No Trace.");
7136 case BTRACE_FORMAT_BTS
:
7137 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7138 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7140 for (const btrace_block
&block
: *btrace
.variant
.bts
.blocks
)
7141 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
7142 paddress (block
.begin
), paddress (block
.end
));
7144 buffer_grow_str0 (buffer
, "</btrace>\n");
7147 case BTRACE_FORMAT_PT
:
7148 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7149 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7150 buffer_grow_str (buffer
, "<pt>\n");
7152 linux_low_encode_pt_config (buffer
, &btrace
.variant
.pt
.config
);
7154 linux_low_encode_raw (buffer
, btrace
.variant
.pt
.data
,
7155 btrace
.variant
.pt
.size
);
7157 buffer_grow_str (buffer
, "</pt>\n");
7158 buffer_grow_str0 (buffer
, "</btrace>\n");
7162 buffer_grow_str0 (buffer
, "E.Unsupported Trace Format.");
7169 /* See to_btrace_conf target method. */
7172 linux_process_target::read_btrace_conf (const btrace_target_info
*tinfo
,
7175 const struct btrace_config
*conf
;
7177 buffer_grow_str (buffer
, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
7178 buffer_grow_str (buffer
, "<btrace-conf version=\"1.0\">\n");
7180 conf
= linux_btrace_conf (tinfo
);
7183 switch (conf
->format
)
7185 case BTRACE_FORMAT_NONE
:
7188 case BTRACE_FORMAT_BTS
:
7189 buffer_xml_printf (buffer
, "<bts");
7190 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->bts
.size
);
7191 buffer_xml_printf (buffer
, " />\n");
7194 case BTRACE_FORMAT_PT
:
7195 buffer_xml_printf (buffer
, "<pt");
7196 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->pt
.size
);
7197 buffer_xml_printf (buffer
, "/>\n");
7202 buffer_grow_str0 (buffer
, "</btrace-conf>\n");
7205 #endif /* HAVE_LINUX_BTRACE */
7207 /* See nat/linux-nat.h. */
7210 current_lwp_ptid (void)
7212 return ptid_of (current_thread
);
7216 linux_process_target::thread_name (ptid_t thread
)
7218 return linux_proc_tid_get_name (thread
);
7223 linux_process_target::thread_handle (ptid_t ptid
, gdb_byte
**handle
,
7226 return thread_db_thread_handle (ptid
, handle
, handle_len
);
7230 /* Default implementation of linux_target_ops method "set_pc" for
7231 32-bit pc register which is literally named "pc". */
7234 linux_set_pc_32bit (struct regcache
*regcache
, CORE_ADDR pc
)
7236 uint32_t newpc
= pc
;
7238 supply_register_by_name (regcache
, "pc", &newpc
);
7241 /* Default implementation of linux_target_ops method "get_pc" for
7242 32-bit pc register which is literally named "pc". */
7245 linux_get_pc_32bit (struct regcache
*regcache
)
7249 collect_register_by_name (regcache
, "pc", &pc
);
7251 debug_printf ("stop pc is 0x%" PRIx32
"\n", pc
);
7255 /* Default implementation of linux_target_ops method "set_pc" for
7256 64-bit pc register which is literally named "pc". */
7259 linux_set_pc_64bit (struct regcache
*regcache
, CORE_ADDR pc
)
7261 uint64_t newpc
= pc
;
7263 supply_register_by_name (regcache
, "pc", &newpc
);
7266 /* Default implementation of linux_target_ops method "get_pc" for
7267 64-bit pc register which is literally named "pc". */
7270 linux_get_pc_64bit (struct regcache
*regcache
)
7274 collect_register_by_name (regcache
, "pc", &pc
);
7276 debug_printf ("stop pc is 0x%" PRIx64
"\n", pc
);
7280 /* See linux-low.h. */
7283 linux_get_auxv (int wordsize
, CORE_ADDR match
, CORE_ADDR
*valp
)
7285 gdb_byte
*data
= (gdb_byte
*) alloca (2 * wordsize
);
7288 gdb_assert (wordsize
== 4 || wordsize
== 8);
7290 while (the_target
->read_auxv (offset
, data
, 2 * wordsize
) == 2 * wordsize
)
7294 uint32_t *data_p
= (uint32_t *) data
;
7295 if (data_p
[0] == match
)
7303 uint64_t *data_p
= (uint64_t *) data
;
7304 if (data_p
[0] == match
)
7311 offset
+= 2 * wordsize
;
7317 /* See linux-low.h. */
7320 linux_get_hwcap (int wordsize
)
7322 CORE_ADDR hwcap
= 0;
7323 linux_get_auxv (wordsize
, AT_HWCAP
, &hwcap
);
7327 /* See linux-low.h. */
7330 linux_get_hwcap2 (int wordsize
)
7332 CORE_ADDR hwcap2
= 0;
7333 linux_get_auxv (wordsize
, AT_HWCAP2
, &hwcap2
);
7337 #ifdef HAVE_LINUX_REGSETS
7339 initialize_regsets_info (struct regsets_info
*info
)
7341 for (info
->num_regsets
= 0;
7342 info
->regsets
[info
->num_regsets
].size
>= 0;
7343 info
->num_regsets
++)
7349 initialize_low (void)
7351 struct sigaction sigchld_action
;
7353 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
7354 set_target_ops (the_linux_target
);
7356 linux_ptrace_init_warnings ();
7357 linux_proc_init_warnings ();
7359 sigchld_action
.sa_handler
= sigchld_handler
;
7360 sigemptyset (&sigchld_action
.sa_mask
);
7361 sigchld_action
.sa_flags
= SA_RESTART
;
7362 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
7364 initialize_low_arch ();
7366 linux_check_ptrace_features ();