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 void mark_lwp_dead (struct lwp_info
*lwp
, int wstat
);
272 static int lwp_is_marked_dead (struct lwp_info
*lwp
);
273 static int kill_lwp (unsigned long lwpid
, int signo
);
274 static void enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
);
275 static int linux_low_ptrace_options (int attached
);
276 static int check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
);
278 /* When the event-loop is doing a step-over, this points at the thread
280 ptid_t step_over_bkpt
;
283 linux_process_target::low_supports_breakpoints ()
289 linux_process_target::low_get_pc (regcache
*regcache
)
295 linux_process_target::low_set_pc (regcache
*regcache
, CORE_ADDR newpc
)
297 gdb_assert_not_reached ("linux target op low_set_pc is not implemented");
300 std::vector
<CORE_ADDR
>
301 linux_process_target::low_get_next_pcs (regcache
*regcache
)
303 gdb_assert_not_reached ("linux target op low_get_next_pcs is not "
308 linux_process_target::low_decr_pc_after_break ()
313 /* True if LWP is stopped in its stepping range. */
316 lwp_in_step_range (struct lwp_info
*lwp
)
318 CORE_ADDR pc
= lwp
->stop_pc
;
320 return (pc
>= lwp
->step_range_start
&& pc
< lwp
->step_range_end
);
323 struct pending_signals
327 struct pending_signals
*prev
;
330 /* The read/write ends of the pipe registered as waitable file in the
332 static int linux_event_pipe
[2] = { -1, -1 };
334 /* True if we're currently in async mode. */
335 #define target_is_async_p() (linux_event_pipe[0] != -1)
337 static void send_sigstop (struct lwp_info
*lwp
);
339 /* Return non-zero if HEADER is a 64-bit ELF file. */
342 elf_64_header_p (const Elf64_Ehdr
*header
, unsigned int *machine
)
344 if (header
->e_ident
[EI_MAG0
] == ELFMAG0
345 && header
->e_ident
[EI_MAG1
] == ELFMAG1
346 && header
->e_ident
[EI_MAG2
] == ELFMAG2
347 && header
->e_ident
[EI_MAG3
] == ELFMAG3
)
349 *machine
= header
->e_machine
;
350 return header
->e_ident
[EI_CLASS
] == ELFCLASS64
;
357 /* Return non-zero if FILE is a 64-bit ELF file,
358 zero if the file is not a 64-bit ELF file,
359 and -1 if the file is not accessible or doesn't exist. */
362 elf_64_file_p (const char *file
, unsigned int *machine
)
367 fd
= open (file
, O_RDONLY
);
371 if (read (fd
, &header
, sizeof (header
)) != sizeof (header
))
378 return elf_64_header_p (&header
, machine
);
381 /* Accepts an integer PID; Returns true if the executable PID is
382 running is a 64-bit ELF file.. */
385 linux_pid_exe_is_elf_64_file (int pid
, unsigned int *machine
)
389 sprintf (file
, "/proc/%d/exe", pid
);
390 return elf_64_file_p (file
, machine
);
394 linux_process_target::delete_lwp (lwp_info
*lwp
)
396 struct thread_info
*thr
= get_lwp_thread (lwp
);
399 debug_printf ("deleting %ld\n", lwpid_of (thr
));
403 low_delete_thread (lwp
->arch_private
);
409 linux_process_target::low_delete_thread (arch_lwp_info
*info
)
411 /* Default implementation should be overridden if architecture-specific
412 info is being used. */
413 gdb_assert (info
== nullptr);
417 linux_process_target::add_linux_process (int pid
, int attached
)
419 struct process_info
*proc
;
421 proc
= add_process (pid
, attached
);
422 proc
->priv
= XCNEW (struct process_info_private
);
424 proc
->priv
->arch_private
= low_new_process ();
430 linux_process_target::low_new_process ()
436 linux_process_target::low_delete_process (arch_process_info
*info
)
438 /* Default implementation must be overridden if architecture-specific
440 gdb_assert (info
== nullptr);
444 linux_process_target::low_new_fork (process_info
*parent
, process_info
*child
)
450 linux_process_target::arch_setup_thread (thread_info
*thread
)
452 struct thread_info
*saved_thread
;
454 saved_thread
= current_thread
;
455 current_thread
= thread
;
459 current_thread
= saved_thread
;
463 linux_process_target::handle_extended_wait (lwp_info
**orig_event_lwp
,
466 client_state
&cs
= get_client_state ();
467 struct lwp_info
*event_lwp
= *orig_event_lwp
;
468 int event
= linux_ptrace_get_extended_event (wstat
);
469 struct thread_info
*event_thr
= get_lwp_thread (event_lwp
);
470 struct lwp_info
*new_lwp
;
472 gdb_assert (event_lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
474 /* All extended events we currently use are mid-syscall. Only
475 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
476 you have to be using PTRACE_SEIZE to get that. */
477 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
479 if ((event
== PTRACE_EVENT_FORK
) || (event
== PTRACE_EVENT_VFORK
)
480 || (event
== PTRACE_EVENT_CLONE
))
483 unsigned long new_pid
;
486 /* Get the pid of the new lwp. */
487 ptrace (PTRACE_GETEVENTMSG
, lwpid_of (event_thr
), (PTRACE_TYPE_ARG3
) 0,
490 /* If we haven't already seen the new PID stop, wait for it now. */
491 if (!pull_pid_from_list (&stopped_pids
, new_pid
, &status
))
493 /* The new child has a pending SIGSTOP. We can't affect it until it
494 hits the SIGSTOP, but we're already attached. */
496 ret
= my_waitpid (new_pid
, &status
, __WALL
);
499 perror_with_name ("waiting for new child");
500 else if (ret
!= new_pid
)
501 warning ("wait returned unexpected PID %d", ret
);
502 else if (!WIFSTOPPED (status
))
503 warning ("wait returned unexpected status 0x%x", status
);
506 if (event
== PTRACE_EVENT_FORK
|| event
== PTRACE_EVENT_VFORK
)
508 struct process_info
*parent_proc
;
509 struct process_info
*child_proc
;
510 struct lwp_info
*child_lwp
;
511 struct thread_info
*child_thr
;
512 struct target_desc
*tdesc
;
514 ptid
= ptid_t (new_pid
, new_pid
, 0);
518 debug_printf ("HEW: Got fork event from LWP %ld, "
520 ptid_of (event_thr
).lwp (),
524 /* Add the new process to the tables and clone the breakpoint
525 lists of the parent. We need to do this even if the new process
526 will be detached, since we will need the process object and the
527 breakpoints to remove any breakpoints from memory when we
528 detach, and the client side will access registers. */
529 child_proc
= add_linux_process (new_pid
, 0);
530 gdb_assert (child_proc
!= NULL
);
531 child_lwp
= add_lwp (ptid
);
532 gdb_assert (child_lwp
!= NULL
);
533 child_lwp
->stopped
= 1;
534 child_lwp
->must_set_ptrace_flags
= 1;
535 child_lwp
->status_pending_p
= 0;
536 child_thr
= get_lwp_thread (child_lwp
);
537 child_thr
->last_resume_kind
= resume_stop
;
538 child_thr
->last_status
.kind
= TARGET_WAITKIND_STOPPED
;
540 /* If we're suspending all threads, leave this one suspended
541 too. If the fork/clone parent is stepping over a breakpoint,
542 all other threads have been suspended already. Leave the
543 child suspended too. */
544 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
545 || event_lwp
->bp_reinsert
!= 0)
548 debug_printf ("HEW: leaving child suspended\n");
549 child_lwp
->suspended
= 1;
552 parent_proc
= get_thread_process (event_thr
);
553 child_proc
->attached
= parent_proc
->attached
;
555 if (event_lwp
->bp_reinsert
!= 0
556 && supports_software_single_step ()
557 && event
== PTRACE_EVENT_VFORK
)
559 /* If we leave single-step breakpoints there, child will
560 hit it, so uninsert single-step breakpoints from parent
561 (and child). Once vfork child is done, reinsert
562 them back to parent. */
563 uninsert_single_step_breakpoints (event_thr
);
566 clone_all_breakpoints (child_thr
, event_thr
);
568 tdesc
= allocate_target_description ();
569 copy_target_description (tdesc
, parent_proc
->tdesc
);
570 child_proc
->tdesc
= tdesc
;
572 /* Clone arch-specific process data. */
573 low_new_fork (parent_proc
, child_proc
);
575 /* Save fork info in the parent thread. */
576 if (event
== PTRACE_EVENT_FORK
)
577 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_FORKED
;
578 else if (event
== PTRACE_EVENT_VFORK
)
579 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORKED
;
581 event_lwp
->waitstatus
.value
.related_pid
= ptid
;
583 /* The status_pending field contains bits denoting the
584 extended event, so when the pending event is handled,
585 the handler will look at lwp->waitstatus. */
586 event_lwp
->status_pending_p
= 1;
587 event_lwp
->status_pending
= wstat
;
589 /* Link the threads until the parent event is passed on to
591 event_lwp
->fork_relative
= child_lwp
;
592 child_lwp
->fork_relative
= event_lwp
;
594 /* If the parent thread is doing step-over with single-step
595 breakpoints, the list of single-step breakpoints are cloned
596 from the parent's. Remove them from the child process.
597 In case of vfork, we'll reinsert them back once vforked
599 if (event_lwp
->bp_reinsert
!= 0
600 && supports_software_single_step ())
602 /* The child process is forked and stopped, so it is safe
603 to access its memory without stopping all other threads
604 from other processes. */
605 delete_single_step_breakpoints (child_thr
);
607 gdb_assert (has_single_step_breakpoints (event_thr
));
608 gdb_assert (!has_single_step_breakpoints (child_thr
));
611 /* Report the event. */
616 debug_printf ("HEW: Got clone event "
617 "from LWP %ld, new child is LWP %ld\n",
618 lwpid_of (event_thr
), new_pid
);
620 ptid
= ptid_t (pid_of (event_thr
), new_pid
, 0);
621 new_lwp
= add_lwp (ptid
);
623 /* Either we're going to immediately resume the new thread
624 or leave it stopped. resume_one_lwp is a nop if it
625 thinks the thread is currently running, so set this first
626 before calling resume_one_lwp. */
627 new_lwp
->stopped
= 1;
629 /* If we're suspending all threads, leave this one suspended
630 too. If the fork/clone parent is stepping over a breakpoint,
631 all other threads have been suspended already. Leave the
632 child suspended too. */
633 if (stopping_threads
== STOPPING_AND_SUSPENDING_THREADS
634 || event_lwp
->bp_reinsert
!= 0)
635 new_lwp
->suspended
= 1;
637 /* Normally we will get the pending SIGSTOP. But in some cases
638 we might get another signal delivered to the group first.
639 If we do get another signal, be sure not to lose it. */
640 if (WSTOPSIG (status
) != SIGSTOP
)
642 new_lwp
->stop_expected
= 1;
643 new_lwp
->status_pending_p
= 1;
644 new_lwp
->status_pending
= status
;
646 else if (cs
.report_thread_events
)
648 new_lwp
->waitstatus
.kind
= TARGET_WAITKIND_THREAD_CREATED
;
649 new_lwp
->status_pending_p
= 1;
650 new_lwp
->status_pending
= status
;
654 thread_db_notice_clone (event_thr
, ptid
);
657 /* Don't report the event. */
660 else if (event
== PTRACE_EVENT_VFORK_DONE
)
662 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_VFORK_DONE
;
664 if (event_lwp
->bp_reinsert
!= 0 && supports_software_single_step ())
666 reinsert_single_step_breakpoints (event_thr
);
668 gdb_assert (has_single_step_breakpoints (event_thr
));
671 /* Report the event. */
674 else if (event
== PTRACE_EVENT_EXEC
&& cs
.report_exec_events
)
676 struct process_info
*proc
;
677 std::vector
<int> syscalls_to_catch
;
683 debug_printf ("HEW: Got exec event from LWP %ld\n",
684 lwpid_of (event_thr
));
687 /* Get the event ptid. */
688 event_ptid
= ptid_of (event_thr
);
689 event_pid
= event_ptid
.pid ();
691 /* Save the syscall list from the execing process. */
692 proc
= get_thread_process (event_thr
);
693 syscalls_to_catch
= std::move (proc
->syscalls_to_catch
);
695 /* Delete the execing process and all its threads. */
697 current_thread
= NULL
;
699 /* Create a new process/lwp/thread. */
700 proc
= add_linux_process (event_pid
, 0);
701 event_lwp
= add_lwp (event_ptid
);
702 event_thr
= get_lwp_thread (event_lwp
);
703 gdb_assert (current_thread
== event_thr
);
704 arch_setup_thread (event_thr
);
706 /* Set the event status. */
707 event_lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXECD
;
708 event_lwp
->waitstatus
.value
.execd_pathname
709 = xstrdup (linux_proc_pid_to_exec_file (lwpid_of (event_thr
)));
711 /* Mark the exec status as pending. */
712 event_lwp
->stopped
= 1;
713 event_lwp
->status_pending_p
= 1;
714 event_lwp
->status_pending
= wstat
;
715 event_thr
->last_resume_kind
= resume_continue
;
716 event_thr
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
718 /* Update syscall state in the new lwp, effectively mid-syscall too. */
719 event_lwp
->syscall_state
= TARGET_WAITKIND_SYSCALL_ENTRY
;
721 /* Restore the list to catch. Don't rely on the client, which is free
722 to avoid sending a new list when the architecture doesn't change.
723 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
724 proc
->syscalls_to_catch
= std::move (syscalls_to_catch
);
726 /* Report the event. */
727 *orig_event_lwp
= event_lwp
;
731 internal_error (__FILE__
, __LINE__
, _("unknown ptrace event %d"), event
);
735 linux_process_target::get_pc (lwp_info
*lwp
)
737 struct thread_info
*saved_thread
;
738 struct regcache
*regcache
;
741 if (!low_supports_breakpoints ())
744 saved_thread
= current_thread
;
745 current_thread
= get_lwp_thread (lwp
);
747 regcache
= get_thread_regcache (current_thread
, 1);
748 pc
= low_get_pc (regcache
);
751 debug_printf ("pc is 0x%lx\n", (long) pc
);
753 current_thread
= saved_thread
;
758 linux_process_target::get_syscall_trapinfo (lwp_info
*lwp
, int *sysno
)
760 struct thread_info
*saved_thread
;
761 struct regcache
*regcache
;
763 saved_thread
= current_thread
;
764 current_thread
= get_lwp_thread (lwp
);
766 regcache
= get_thread_regcache (current_thread
, 1);
767 low_get_syscall_trapinfo (regcache
, sysno
);
770 debug_printf ("get_syscall_trapinfo sysno %d\n", *sysno
);
772 current_thread
= saved_thread
;
776 linux_process_target::low_get_syscall_trapinfo (regcache
*regcache
, int *sysno
)
778 /* By default, report an unknown system call number. */
779 *sysno
= UNKNOWN_SYSCALL
;
783 linux_process_target::save_stop_reason (lwp_info
*lwp
)
786 CORE_ADDR sw_breakpoint_pc
;
787 struct thread_info
*saved_thread
;
788 #if USE_SIGTRAP_SIGINFO
792 if (!low_supports_breakpoints ())
796 sw_breakpoint_pc
= pc
- low_decr_pc_after_break ();
798 /* breakpoint_at reads from the current thread. */
799 saved_thread
= current_thread
;
800 current_thread
= get_lwp_thread (lwp
);
802 #if USE_SIGTRAP_SIGINFO
803 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
804 (PTRACE_TYPE_ARG3
) 0, &siginfo
) == 0)
806 if (siginfo
.si_signo
== SIGTRAP
)
808 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
)
809 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
811 /* The si_code is ambiguous on this arch -- check debug
813 if (!check_stopped_by_watchpoint (lwp
))
814 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
816 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo
.si_code
))
818 /* If we determine the LWP stopped for a SW breakpoint,
819 trust it. Particularly don't check watchpoint
820 registers, because at least on s390, we'd find
821 stopped-by-watchpoint as long as there's a watchpoint
823 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
825 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo
.si_code
))
827 /* This can indicate either a hardware breakpoint or
828 hardware watchpoint. Check debug registers. */
829 if (!check_stopped_by_watchpoint (lwp
))
830 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
832 else if (siginfo
.si_code
== TRAP_TRACE
)
834 /* We may have single stepped an instruction that
835 triggered a watchpoint. In that case, on some
836 architectures (such as x86), instead of TRAP_HWBKPT,
837 si_code indicates TRAP_TRACE, and we need to check
838 the debug registers separately. */
839 if (!check_stopped_by_watchpoint (lwp
))
840 lwp
->stop_reason
= TARGET_STOPPED_BY_SINGLE_STEP
;
845 /* We may have just stepped a breakpoint instruction. E.g., in
846 non-stop mode, GDB first tells the thread A to step a range, and
847 then the user inserts a breakpoint inside the range. In that
848 case we need to report the breakpoint PC. */
849 if ((!lwp
->stepping
|| lwp
->stop_pc
== sw_breakpoint_pc
)
850 && low_breakpoint_at (sw_breakpoint_pc
))
851 lwp
->stop_reason
= TARGET_STOPPED_BY_SW_BREAKPOINT
;
853 if (hardware_breakpoint_inserted_here (pc
))
854 lwp
->stop_reason
= TARGET_STOPPED_BY_HW_BREAKPOINT
;
856 if (lwp
->stop_reason
== TARGET_STOPPED_BY_NO_REASON
)
857 check_stopped_by_watchpoint (lwp
);
860 if (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
)
864 struct thread_info
*thr
= get_lwp_thread (lwp
);
866 debug_printf ("CSBB: %s stopped by software breakpoint\n",
867 target_pid_to_str (ptid_of (thr
)));
870 /* Back up the PC if necessary. */
871 if (pc
!= sw_breakpoint_pc
)
873 struct regcache
*regcache
874 = get_thread_regcache (current_thread
, 1);
875 low_set_pc (regcache
, sw_breakpoint_pc
);
878 /* Update this so we record the correct stop PC below. */
879 pc
= sw_breakpoint_pc
;
881 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
)
885 struct thread_info
*thr
= get_lwp_thread (lwp
);
887 debug_printf ("CSBB: %s stopped by hardware breakpoint\n",
888 target_pid_to_str (ptid_of (thr
)));
891 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
895 struct thread_info
*thr
= get_lwp_thread (lwp
);
897 debug_printf ("CSBB: %s stopped by hardware watchpoint\n",
898 target_pid_to_str (ptid_of (thr
)));
901 else if (lwp
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
)
905 struct thread_info
*thr
= get_lwp_thread (lwp
);
907 debug_printf ("CSBB: %s stopped by trace\n",
908 target_pid_to_str (ptid_of (thr
)));
913 current_thread
= saved_thread
;
918 linux_process_target::add_lwp (ptid_t ptid
)
920 struct lwp_info
*lwp
;
922 lwp
= XCNEW (struct lwp_info
);
924 lwp
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
926 lwp
->thread
= add_thread (ptid
, lwp
);
928 low_new_thread (lwp
);
934 linux_process_target::low_new_thread (lwp_info
*info
)
939 /* Callback to be used when calling fork_inferior, responsible for
940 actually initiating the tracing of the inferior. */
945 if (ptrace (PTRACE_TRACEME
, 0, (PTRACE_TYPE_ARG3
) 0,
946 (PTRACE_TYPE_ARG4
) 0) < 0)
947 trace_start_error_with_name ("ptrace");
949 if (setpgid (0, 0) < 0)
950 trace_start_error_with_name ("setpgid");
952 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
953 stdout to stderr so that inferior i/o doesn't corrupt the connection.
954 Also, redirect stdin to /dev/null. */
955 if (remote_connection_is_stdio ())
958 trace_start_error_with_name ("close");
959 if (open ("/dev/null", O_RDONLY
) < 0)
960 trace_start_error_with_name ("open");
962 trace_start_error_with_name ("dup2");
963 if (write (2, "stdin/stdout redirected\n",
964 sizeof ("stdin/stdout redirected\n") - 1) < 0)
966 /* Errors ignored. */;
971 /* Start an inferior process and returns its pid.
972 PROGRAM is the name of the program to be started, and PROGRAM_ARGS
973 are its arguments. */
976 linux_process_target::create_inferior (const char *program
,
977 const std::vector
<char *> &program_args
)
979 client_state
&cs
= get_client_state ();
980 struct lwp_info
*new_lwp
;
985 maybe_disable_address_space_randomization restore_personality
986 (cs
.disable_randomization
);
987 std::string str_program_args
= stringify_argv (program_args
);
989 pid
= fork_inferior (program
,
990 str_program_args
.c_str (),
991 get_environ ()->envp (), linux_ptrace_fun
,
992 NULL
, NULL
, NULL
, NULL
);
995 add_linux_process (pid
, 0);
997 ptid
= ptid_t (pid
, pid
, 0);
998 new_lwp
= add_lwp (ptid
);
999 new_lwp
->must_set_ptrace_flags
= 1;
1001 post_fork_inferior (pid
, program
);
1006 /* Implement the post_create_inferior target_ops method. */
1009 linux_process_target::post_create_inferior ()
1011 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
1015 if (lwp
->must_set_ptrace_flags
)
1017 struct process_info
*proc
= current_process ();
1018 int options
= linux_low_ptrace_options (proc
->attached
);
1020 linux_enable_event_reporting (lwpid_of (current_thread
), options
);
1021 lwp
->must_set_ptrace_flags
= 0;
1026 linux_process_target::attach_lwp (ptid_t ptid
)
1028 struct lwp_info
*new_lwp
;
1029 int lwpid
= ptid
.lwp ();
1031 if (ptrace (PTRACE_ATTACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0)
1035 new_lwp
= add_lwp (ptid
);
1037 /* We need to wait for SIGSTOP before being able to make the next
1038 ptrace call on this LWP. */
1039 new_lwp
->must_set_ptrace_flags
= 1;
1041 if (linux_proc_pid_is_stopped (lwpid
))
1044 debug_printf ("Attached to a stopped process\n");
1046 /* The process is definitely stopped. It is in a job control
1047 stop, unless the kernel predates the TASK_STOPPED /
1048 TASK_TRACED distinction, in which case it might be in a
1049 ptrace stop. Make sure it is in a ptrace stop; from there we
1050 can kill it, signal it, et cetera.
1052 First make sure there is a pending SIGSTOP. Since we are
1053 already attached, the process can not transition from stopped
1054 to running without a PTRACE_CONT; so we know this signal will
1055 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1056 probably already in the queue (unless this kernel is old
1057 enough to use TASK_STOPPED for ptrace stops); but since
1058 SIGSTOP is not an RT signal, it can only be queued once. */
1059 kill_lwp (lwpid
, SIGSTOP
);
1061 /* Finally, resume the stopped process. This will deliver the
1062 SIGSTOP (or a higher priority signal, just like normal
1063 PTRACE_ATTACH), which we'll catch later on. */
1064 ptrace (PTRACE_CONT
, lwpid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1067 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1068 brings it to a halt.
1070 There are several cases to consider here:
1072 1) gdbserver has already attached to the process and is being notified
1073 of a new thread that is being created.
1074 In this case we should ignore that SIGSTOP and resume the
1075 process. This is handled below by setting stop_expected = 1,
1076 and the fact that add_thread sets last_resume_kind ==
1079 2) This is the first thread (the process thread), and we're attaching
1080 to it via attach_inferior.
1081 In this case we want the process thread to stop.
1082 This is handled by having linux_attach set last_resume_kind ==
1083 resume_stop after we return.
1085 If the pid we are attaching to is also the tgid, we attach to and
1086 stop all the existing threads. Otherwise, we attach to pid and
1087 ignore any other threads in the same group as this pid.
1089 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1091 In this case we want the thread to stop.
1092 FIXME: This case is currently not properly handled.
1093 We should wait for the SIGSTOP but don't. Things work apparently
1094 because enough time passes between when we ptrace (ATTACH) and when
1095 gdb makes the next ptrace call on the thread.
1097 On the other hand, if we are currently trying to stop all threads, we
1098 should treat the new thread as if we had sent it a SIGSTOP. This works
1099 because we are guaranteed that the add_lwp call above added us to the
1100 end of the list, and so the new thread has not yet reached
1101 wait_for_sigstop (but will). */
1102 new_lwp
->stop_expected
= 1;
1107 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1108 already attached. Returns true if a new LWP is found, false
1112 attach_proc_task_lwp_callback (ptid_t ptid
)
1114 /* Is this a new thread? */
1115 if (find_thread_ptid (ptid
) == NULL
)
1117 int lwpid
= ptid
.lwp ();
1121 debug_printf ("Found new lwp %d\n", lwpid
);
1123 err
= the_linux_target
->attach_lwp (ptid
);
1125 /* Be quiet if we simply raced with the thread exiting. EPERM
1126 is returned if the thread's task still exists, and is marked
1127 as exited or zombie, as well as other conditions, so in that
1128 case, confirm the status in /proc/PID/status. */
1130 || (err
== EPERM
&& linux_proc_pid_is_gone (lwpid
)))
1134 debug_printf ("Cannot attach to lwp %d: "
1135 "thread is gone (%d: %s)\n",
1136 lwpid
, err
, safe_strerror (err
));
1142 = linux_ptrace_attach_fail_reason_string (ptid
, err
);
1144 warning (_("Cannot attach to lwp %d: %s"), lwpid
, reason
.c_str ());
1152 static void async_file_mark (void);
1154 /* Attach to PID. If PID is the tgid, attach to it and all
1158 linux_process_target::attach (unsigned long pid
)
1160 struct process_info
*proc
;
1161 struct thread_info
*initial_thread
;
1162 ptid_t ptid
= ptid_t (pid
, pid
, 0);
1165 proc
= add_linux_process (pid
, 1);
1167 /* Attach to PID. We will check for other threads
1169 err
= attach_lwp (ptid
);
1172 remove_process (proc
);
1174 std::string reason
= linux_ptrace_attach_fail_reason_string (ptid
, err
);
1175 error ("Cannot attach to process %ld: %s", pid
, reason
.c_str ());
1178 /* Don't ignore the initial SIGSTOP if we just attached to this
1179 process. It will be collected by wait shortly. */
1180 initial_thread
= find_thread_ptid (ptid_t (pid
, pid
, 0));
1181 initial_thread
->last_resume_kind
= resume_stop
;
1183 /* We must attach to every LWP. If /proc is mounted, use that to
1184 find them now. On the one hand, the inferior may be using raw
1185 clone instead of using pthreads. On the other hand, even if it
1186 is using pthreads, GDB may not be connected yet (thread_db needs
1187 to do symbol lookups, through qSymbol). Also, thread_db walks
1188 structures in the inferior's address space to find the list of
1189 threads/LWPs, and those structures may well be corrupted. Note
1190 that once thread_db is loaded, we'll still use it to list threads
1191 and associate pthread info with each LWP. */
1192 linux_proc_attach_tgid_threads (pid
, attach_proc_task_lwp_callback
);
1194 /* GDB will shortly read the xml target description for this
1195 process, to figure out the process' architecture. But the target
1196 description is only filled in when the first process/thread in
1197 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1198 that now, otherwise, if GDB is fast enough, it could read the
1199 target description _before_ that initial stop. */
1202 struct lwp_info
*lwp
;
1204 ptid_t pid_ptid
= ptid_t (pid
);
1206 lwpid
= wait_for_event_filtered (pid_ptid
, pid_ptid
, &wstat
, __WALL
);
1207 gdb_assert (lwpid
> 0);
1209 lwp
= find_lwp_pid (ptid_t (lwpid
));
1211 if (!WIFSTOPPED (wstat
) || WSTOPSIG (wstat
) != SIGSTOP
)
1213 lwp
->status_pending_p
= 1;
1214 lwp
->status_pending
= wstat
;
1217 initial_thread
->last_resume_kind
= resume_continue
;
1221 gdb_assert (proc
->tdesc
!= NULL
);
1228 last_thread_of_process_p (int pid
)
1230 bool seen_one
= false;
1232 thread_info
*thread
= find_thread (pid
, [&] (thread_info
*thr_arg
)
1236 /* This is the first thread of this process we see. */
1242 /* This is the second thread of this process we see. */
1247 return thread
== NULL
;
1253 linux_kill_one_lwp (struct lwp_info
*lwp
)
1255 struct thread_info
*thr
= get_lwp_thread (lwp
);
1256 int pid
= lwpid_of (thr
);
1258 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1259 there is no signal context, and ptrace(PTRACE_KILL) (or
1260 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1261 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1262 alternative is to kill with SIGKILL. We only need one SIGKILL
1263 per process, not one for each thread. But since we still support
1264 support debugging programs using raw clone without CLONE_THREAD,
1265 we send one for each thread. For years, we used PTRACE_KILL
1266 only, so we're being a bit paranoid about some old kernels where
1267 PTRACE_KILL might work better (dubious if there are any such, but
1268 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1269 second, and so we're fine everywhere. */
1272 kill_lwp (pid
, SIGKILL
);
1275 int save_errno
= errno
;
1277 debug_printf ("LKL: kill_lwp (SIGKILL) %s, 0, 0 (%s)\n",
1278 target_pid_to_str (ptid_of (thr
)),
1279 save_errno
? safe_strerror (save_errno
) : "OK");
1283 ptrace (PTRACE_KILL
, pid
, (PTRACE_TYPE_ARG3
) 0, (PTRACE_TYPE_ARG4
) 0);
1286 int save_errno
= errno
;
1288 debug_printf ("LKL: PTRACE_KILL %s, 0, 0 (%s)\n",
1289 target_pid_to_str (ptid_of (thr
)),
1290 save_errno
? safe_strerror (save_errno
) : "OK");
1294 /* Kill LWP and wait for it to die. */
1297 kill_wait_lwp (struct lwp_info
*lwp
)
1299 struct thread_info
*thr
= get_lwp_thread (lwp
);
1300 int pid
= ptid_of (thr
).pid ();
1301 int lwpid
= ptid_of (thr
).lwp ();
1306 debug_printf ("kwl: killing lwp %d, for pid: %d\n", lwpid
, pid
);
1310 linux_kill_one_lwp (lwp
);
1312 /* Make sure it died. Notes:
1314 - The loop is most likely unnecessary.
1316 - We don't use wait_for_event as that could delete lwps
1317 while we're iterating over them. We're not interested in
1318 any pending status at this point, only in making sure all
1319 wait status on the kernel side are collected until the
1322 - We don't use __WALL here as the __WALL emulation relies on
1323 SIGCHLD, and killing a stopped process doesn't generate
1324 one, nor an exit status.
1326 res
= my_waitpid (lwpid
, &wstat
, 0);
1327 if (res
== -1 && errno
== ECHILD
)
1328 res
= my_waitpid (lwpid
, &wstat
, __WCLONE
);
1329 } while (res
> 0 && WIFSTOPPED (wstat
));
1331 /* Even if it was stopped, the child may have already disappeared.
1332 E.g., if it was killed by SIGKILL. */
1333 if (res
< 0 && errno
!= ECHILD
)
1334 perror_with_name ("kill_wait_lwp");
1337 /* Callback for `for_each_thread'. Kills an lwp of a given process,
1338 except the leader. */
1341 kill_one_lwp_callback (thread_info
*thread
, int pid
)
1343 struct lwp_info
*lwp
= get_thread_lwp (thread
);
1345 /* We avoid killing the first thread here, because of a Linux kernel (at
1346 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1347 the children get a chance to be reaped, it will remain a zombie
1350 if (lwpid_of (thread
) == pid
)
1353 debug_printf ("lkop: is last of process %s\n",
1354 target_pid_to_str (thread
->id
));
1358 kill_wait_lwp (lwp
);
1362 linux_process_target::kill (process_info
*process
)
1364 int pid
= process
->pid
;
1366 /* If we're killing a running inferior, make sure it is stopped
1367 first, as PTRACE_KILL will not work otherwise. */
1368 stop_all_lwps (0, NULL
);
1370 for_each_thread (pid
, [&] (thread_info
*thread
)
1372 kill_one_lwp_callback (thread
, pid
);
1375 /* See the comment in linux_kill_one_lwp. We did not kill the first
1376 thread in the list, so do so now. */
1377 lwp_info
*lwp
= find_lwp_pid (ptid_t (pid
));
1382 debug_printf ("lk_1: cannot find lwp for pid: %d\n",
1386 kill_wait_lwp (lwp
);
1390 /* Since we presently can only stop all lwps of all processes, we
1391 need to unstop lwps of other processes. */
1392 unstop_all_lwps (0, NULL
);
1396 /* Get pending signal of THREAD, for detaching purposes. This is the
1397 signal the thread last stopped for, which we need to deliver to the
1398 thread when detaching, otherwise, it'd be suppressed/lost. */
1401 get_detach_signal (struct thread_info
*thread
)
1403 client_state
&cs
= get_client_state ();
1404 enum gdb_signal signo
= GDB_SIGNAL_0
;
1406 struct lwp_info
*lp
= get_thread_lwp (thread
);
1408 if (lp
->status_pending_p
)
1409 status
= lp
->status_pending
;
1412 /* If the thread had been suspended by gdbserver, and it stopped
1413 cleanly, then it'll have stopped with SIGSTOP. But we don't
1414 want to deliver that SIGSTOP. */
1415 if (thread
->last_status
.kind
!= TARGET_WAITKIND_STOPPED
1416 || thread
->last_status
.value
.sig
== GDB_SIGNAL_0
)
1419 /* Otherwise, we may need to deliver the signal we
1421 status
= lp
->last_status
;
1424 if (!WIFSTOPPED (status
))
1427 debug_printf ("GPS: lwp %s hasn't stopped: no pending signal\n",
1428 target_pid_to_str (ptid_of (thread
)));
1432 /* Extended wait statuses aren't real SIGTRAPs. */
1433 if (WSTOPSIG (status
) == SIGTRAP
&& linux_is_extended_waitstatus (status
))
1436 debug_printf ("GPS: lwp %s had stopped with extended "
1437 "status: no pending signal\n",
1438 target_pid_to_str (ptid_of (thread
)));
1442 signo
= gdb_signal_from_host (WSTOPSIG (status
));
1444 if (cs
.program_signals_p
&& !cs
.program_signals
[signo
])
1447 debug_printf ("GPS: lwp %s had signal %s, but it is in nopass state\n",
1448 target_pid_to_str (ptid_of (thread
)),
1449 gdb_signal_to_string (signo
));
1452 else if (!cs
.program_signals_p
1453 /* If we have no way to know which signals GDB does not
1454 want to have passed to the program, assume
1455 SIGTRAP/SIGINT, which is GDB's default. */
1456 && (signo
== GDB_SIGNAL_TRAP
|| signo
== GDB_SIGNAL_INT
))
1459 debug_printf ("GPS: lwp %s had signal %s, "
1460 "but we don't know if we should pass it. "
1461 "Default to not.\n",
1462 target_pid_to_str (ptid_of (thread
)),
1463 gdb_signal_to_string (signo
));
1469 debug_printf ("GPS: lwp %s has pending signal %s: delivering it.\n",
1470 target_pid_to_str (ptid_of (thread
)),
1471 gdb_signal_to_string (signo
));
1473 return WSTOPSIG (status
);
1478 linux_process_target::detach_one_lwp (lwp_info
*lwp
)
1480 struct thread_info
*thread
= get_lwp_thread (lwp
);
1484 /* If there is a pending SIGSTOP, get rid of it. */
1485 if (lwp
->stop_expected
)
1488 debug_printf ("Sending SIGCONT to %s\n",
1489 target_pid_to_str (ptid_of (thread
)));
1491 kill_lwp (lwpid_of (thread
), SIGCONT
);
1492 lwp
->stop_expected
= 0;
1495 /* Pass on any pending signal for this thread. */
1496 sig
= get_detach_signal (thread
);
1498 /* Preparing to resume may try to write registers, and fail if the
1499 lwp is zombie. If that happens, ignore the error. We'll handle
1500 it below, when detach fails with ESRCH. */
1503 /* Flush any pending changes to the process's registers. */
1504 regcache_invalidate_thread (thread
);
1506 /* Finally, let it resume. */
1507 low_prepare_to_resume (lwp
);
1509 catch (const gdb_exception_error
&ex
)
1511 if (!check_ptrace_stopped_lwp_gone (lwp
))
1515 lwpid
= lwpid_of (thread
);
1516 if (ptrace (PTRACE_DETACH
, lwpid
, (PTRACE_TYPE_ARG3
) 0,
1517 (PTRACE_TYPE_ARG4
) (long) sig
) < 0)
1519 int save_errno
= errno
;
1521 /* We know the thread exists, so ESRCH must mean the lwp is
1522 zombie. This can happen if one of the already-detached
1523 threads exits the whole thread group. In that case we're
1524 still attached, and must reap the lwp. */
1525 if (save_errno
== ESRCH
)
1529 ret
= my_waitpid (lwpid
, &status
, __WALL
);
1532 warning (_("Couldn't reap LWP %d while detaching: %s"),
1533 lwpid
, safe_strerror (errno
));
1535 else if (!WIFEXITED (status
) && !WIFSIGNALED (status
))
1537 warning (_("Reaping LWP %d while detaching "
1538 "returned unexpected status 0x%x"),
1544 error (_("Can't detach %s: %s"),
1545 target_pid_to_str (ptid_of (thread
)),
1546 safe_strerror (save_errno
));
1549 else if (debug_threads
)
1551 debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)\n",
1552 target_pid_to_str (ptid_of (thread
)),
1560 linux_process_target::detach (process_info
*process
)
1562 struct lwp_info
*main_lwp
;
1564 /* As there's a step over already in progress, let it finish first,
1565 otherwise nesting a stabilize_threads operation on top gets real
1567 complete_ongoing_step_over ();
1569 /* Stop all threads before detaching. First, ptrace requires that
1570 the thread is stopped to successfully detach. Second, thread_db
1571 may need to uninstall thread event breakpoints from memory, which
1572 only works with a stopped process anyway. */
1573 stop_all_lwps (0, NULL
);
1575 #ifdef USE_THREAD_DB
1576 thread_db_detach (process
);
1579 /* Stabilize threads (move out of jump pads). */
1580 target_stabilize_threads ();
1582 /* Detach from the clone lwps first. If the thread group exits just
1583 while we're detaching, we must reap the clone lwps before we're
1584 able to reap the leader. */
1585 for_each_thread (process
->pid
, [this] (thread_info
*thread
)
1587 /* We don't actually detach from the thread group leader just yet.
1588 If the thread group exits, we must reap the zombie clone lwps
1589 before we're able to reap the leader. */
1590 if (thread
->id
.pid () == thread
->id
.lwp ())
1593 lwp_info
*lwp
= get_thread_lwp (thread
);
1594 detach_one_lwp (lwp
);
1597 main_lwp
= find_lwp_pid (ptid_t (process
->pid
));
1598 detach_one_lwp (main_lwp
);
1602 /* Since we presently can only stop all lwps of all processes, we
1603 need to unstop lwps of other processes. */
1604 unstop_all_lwps (0, NULL
);
1608 /* Remove all LWPs that belong to process PROC from the lwp list. */
1611 linux_process_target::mourn (process_info
*process
)
1613 struct process_info_private
*priv
;
1615 #ifdef USE_THREAD_DB
1616 thread_db_mourn (process
);
1619 for_each_thread (process
->pid
, [this] (thread_info
*thread
)
1621 delete_lwp (get_thread_lwp (thread
));
1624 /* Freeing all private data. */
1625 priv
= process
->priv
;
1626 low_delete_process (priv
->arch_private
);
1628 process
->priv
= NULL
;
1630 remove_process (process
);
1634 linux_process_target::join (int pid
)
1639 ret
= my_waitpid (pid
, &status
, 0);
1640 if (WIFEXITED (status
) || WIFSIGNALED (status
))
1642 } while (ret
!= -1 || errno
!= ECHILD
);
1645 /* Return true if the given thread is still alive. */
1648 linux_process_target::thread_alive (ptid_t ptid
)
1650 struct lwp_info
*lwp
= find_lwp_pid (ptid
);
1652 /* We assume we always know if a thread exits. If a whole process
1653 exited but we still haven't been able to report it to GDB, we'll
1654 hold on to the last lwp of the dead process. */
1656 return !lwp_is_marked_dead (lwp
);
1662 linux_process_target::thread_still_has_status_pending (thread_info
*thread
)
1664 struct lwp_info
*lp
= get_thread_lwp (thread
);
1666 if (!lp
->status_pending_p
)
1669 if (thread
->last_resume_kind
!= resume_stop
1670 && (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1671 || lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
))
1673 struct thread_info
*saved_thread
;
1677 gdb_assert (lp
->last_status
!= 0);
1681 saved_thread
= current_thread
;
1682 current_thread
= thread
;
1684 if (pc
!= lp
->stop_pc
)
1687 debug_printf ("PC of %ld changed\n",
1692 #if !USE_SIGTRAP_SIGINFO
1693 else if (lp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
1694 && !low_breakpoint_at (pc
))
1697 debug_printf ("previous SW breakpoint of %ld gone\n",
1701 else if (lp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
1702 && !hardware_breakpoint_inserted_here (pc
))
1705 debug_printf ("previous HW breakpoint of %ld gone\n",
1711 current_thread
= saved_thread
;
1716 debug_printf ("discarding pending breakpoint status\n");
1717 lp
->status_pending_p
= 0;
1725 /* Returns true if LWP is resumed from the client's perspective. */
1728 lwp_resumed (struct lwp_info
*lwp
)
1730 struct thread_info
*thread
= get_lwp_thread (lwp
);
1732 if (thread
->last_resume_kind
!= resume_stop
)
1735 /* Did gdb send us a `vCont;t', but we haven't reported the
1736 corresponding stop to gdb yet? If so, the thread is still
1737 resumed/running from gdb's perspective. */
1738 if (thread
->last_resume_kind
== resume_stop
1739 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
1746 linux_process_target::status_pending_p_callback (thread_info
*thread
,
1749 struct lwp_info
*lp
= get_thread_lwp (thread
);
1751 /* Check if we're only interested in events from a specific process
1752 or a specific LWP. */
1753 if (!thread
->id
.matches (ptid
))
1756 if (!lwp_resumed (lp
))
1759 if (lp
->status_pending_p
1760 && !thread_still_has_status_pending (thread
))
1762 resume_one_lwp (lp
, lp
->stepping
, GDB_SIGNAL_0
, NULL
);
1766 return lp
->status_pending_p
;
1770 find_lwp_pid (ptid_t ptid
)
1772 thread_info
*thread
= find_thread ([&] (thread_info
*thr_arg
)
1774 int lwp
= ptid
.lwp () != 0 ? ptid
.lwp () : ptid
.pid ();
1775 return thr_arg
->id
.lwp () == lwp
;
1781 return get_thread_lwp (thread
);
1784 /* Return the number of known LWPs in the tgid given by PID. */
1791 for_each_thread (pid
, [&] (thread_info
*thread
)
1799 /* See nat/linux-nat.h. */
1802 iterate_over_lwps (ptid_t filter
,
1803 gdb::function_view
<iterate_over_lwps_ftype
> callback
)
1805 thread_info
*thread
= find_thread (filter
, [&] (thread_info
*thr_arg
)
1807 lwp_info
*lwp
= get_thread_lwp (thr_arg
);
1809 return callback (lwp
);
1815 return get_thread_lwp (thread
);
1819 linux_process_target::check_zombie_leaders ()
1821 for_each_process ([this] (process_info
*proc
) {
1822 pid_t leader_pid
= pid_of (proc
);
1823 struct lwp_info
*leader_lp
;
1825 leader_lp
= find_lwp_pid (ptid_t (leader_pid
));
1828 debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1829 "num_lwps=%d, zombie=%d\n",
1830 leader_pid
, leader_lp
!= NULL
, num_lwps (leader_pid
),
1831 linux_proc_pid_is_zombie (leader_pid
));
1833 if (leader_lp
!= NULL
&& !leader_lp
->stopped
1834 /* Check if there are other threads in the group, as we may
1835 have raced with the inferior simply exiting. */
1836 && !last_thread_of_process_p (leader_pid
)
1837 && linux_proc_pid_is_zombie (leader_pid
))
1839 /* A leader zombie can mean one of two things:
1841 - It exited, and there's an exit status pending
1842 available, or only the leader exited (not the whole
1843 program). In the latter case, we can't waitpid the
1844 leader's exit status until all other threads are gone.
1846 - There are 3 or more threads in the group, and a thread
1847 other than the leader exec'd. On an exec, the Linux
1848 kernel destroys all other threads (except the execing
1849 one) in the thread group, and resets the execing thread's
1850 tid to the tgid. No exit notification is sent for the
1851 execing thread -- from the ptracer's perspective, it
1852 appears as though the execing thread just vanishes.
1853 Until we reap all other threads except the leader and the
1854 execing thread, the leader will be zombie, and the
1855 execing thread will be in `D (disc sleep)'. As soon as
1856 all other threads are reaped, the execing thread changes
1857 it's tid to the tgid, and the previous (zombie) leader
1858 vanishes, giving place to the "new" leader. We could try
1859 distinguishing the exit and exec cases, by waiting once
1860 more, and seeing if something comes out, but it doesn't
1861 sound useful. The previous leader _does_ go away, and
1862 we'll re-add the new one once we see the exec event
1863 (which is just the same as what would happen if the
1864 previous leader did exit voluntarily before some other
1868 debug_printf ("CZL: Thread group leader %d zombie "
1869 "(it exited, or another thread execd).\n",
1872 delete_lwp (leader_lp
);
1877 /* Callback for `find_thread'. Returns the first LWP that is not
1881 not_stopped_callback (thread_info
*thread
, ptid_t filter
)
1883 if (!thread
->id
.matches (filter
))
1886 lwp_info
*lwp
= get_thread_lwp (thread
);
1888 return !lwp
->stopped
;
1891 /* Increment LWP's suspend count. */
1894 lwp_suspended_inc (struct lwp_info
*lwp
)
1898 if (debug_threads
&& lwp
->suspended
> 4)
1900 struct thread_info
*thread
= get_lwp_thread (lwp
);
1902 debug_printf ("LWP %ld has a suspiciously high suspend count,"
1903 " suspended=%d\n", lwpid_of (thread
), lwp
->suspended
);
1907 /* Decrement LWP's suspend count. */
1910 lwp_suspended_decr (struct lwp_info
*lwp
)
1914 if (lwp
->suspended
< 0)
1916 struct thread_info
*thread
= get_lwp_thread (lwp
);
1918 internal_error (__FILE__
, __LINE__
,
1919 "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread
),
1924 /* This function should only be called if the LWP got a SIGTRAP.
1926 Handle any tracepoint steps or hits. Return true if a tracepoint
1927 event was handled, 0 otherwise. */
1930 handle_tracepoints (struct lwp_info
*lwp
)
1932 struct thread_info
*tinfo
= get_lwp_thread (lwp
);
1933 int tpoint_related_event
= 0;
1935 gdb_assert (lwp
->suspended
== 0);
1937 /* If this tracepoint hit causes a tracing stop, we'll immediately
1938 uninsert tracepoints. To do this, we temporarily pause all
1939 threads, unpatch away, and then unpause threads. We need to make
1940 sure the unpausing doesn't resume LWP too. */
1941 lwp_suspended_inc (lwp
);
1943 /* And we need to be sure that any all-threads-stopping doesn't try
1944 to move threads out of the jump pads, as it could deadlock the
1945 inferior (LWP could be in the jump pad, maybe even holding the
1948 /* Do any necessary step collect actions. */
1949 tpoint_related_event
|= tracepoint_finished_step (tinfo
, lwp
->stop_pc
);
1951 tpoint_related_event
|= handle_tracepoint_bkpts (tinfo
, lwp
->stop_pc
);
1953 /* See if we just hit a tracepoint and do its main collect
1955 tpoint_related_event
|= tracepoint_was_hit (tinfo
, lwp
->stop_pc
);
1957 lwp_suspended_decr (lwp
);
1959 gdb_assert (lwp
->suspended
== 0);
1960 gdb_assert (!stabilizing_threads
1961 || (lwp
->collecting_fast_tracepoint
1962 != fast_tpoint_collect_result::not_collecting
));
1964 if (tpoint_related_event
)
1967 debug_printf ("got a tracepoint event\n");
1974 fast_tpoint_collect_result
1975 linux_process_target::linux_fast_tracepoint_collecting
1976 (lwp_info
*lwp
, fast_tpoint_collect_status
*status
)
1978 CORE_ADDR thread_area
;
1979 struct thread_info
*thread
= get_lwp_thread (lwp
);
1981 /* Get the thread area address. This is used to recognize which
1982 thread is which when tracing with the in-process agent library.
1983 We don't read anything from the address, and treat it as opaque;
1984 it's the address itself that we assume is unique per-thread. */
1985 if (low_get_thread_area (lwpid_of (thread
), &thread_area
) == -1)
1986 return fast_tpoint_collect_result::not_collecting
;
1988 return fast_tracepoint_collecting (thread_area
, lwp
->stop_pc
, status
);
1992 linux_process_target::low_get_thread_area (int lwpid
, CORE_ADDR
*addrp
)
1998 linux_process_target::maybe_move_out_of_jump_pad (lwp_info
*lwp
, int *wstat
)
2000 struct thread_info
*saved_thread
;
2002 saved_thread
= current_thread
;
2003 current_thread
= get_lwp_thread (lwp
);
2006 || (WIFSTOPPED (*wstat
) && WSTOPSIG (*wstat
) != SIGTRAP
))
2007 && supports_fast_tracepoints ()
2008 && agent_loaded_p ())
2010 struct fast_tpoint_collect_status status
;
2013 debug_printf ("Checking whether LWP %ld needs to move out of the "
2015 lwpid_of (current_thread
));
2017 fast_tpoint_collect_result r
2018 = linux_fast_tracepoint_collecting (lwp
, &status
);
2021 || (WSTOPSIG (*wstat
) != SIGILL
2022 && WSTOPSIG (*wstat
) != SIGFPE
2023 && WSTOPSIG (*wstat
) != SIGSEGV
2024 && WSTOPSIG (*wstat
) != SIGBUS
))
2026 lwp
->collecting_fast_tracepoint
= r
;
2028 if (r
!= fast_tpoint_collect_result::not_collecting
)
2030 if (r
== fast_tpoint_collect_result::before_insn
2031 && lwp
->exit_jump_pad_bkpt
== NULL
)
2033 /* Haven't executed the original instruction yet.
2034 Set breakpoint there, and wait till it's hit,
2035 then single-step until exiting the jump pad. */
2036 lwp
->exit_jump_pad_bkpt
2037 = set_breakpoint_at (status
.adjusted_insn_addr
, NULL
);
2041 debug_printf ("Checking whether LWP %ld needs to move out of "
2042 "the jump pad...it does\n",
2043 lwpid_of (current_thread
));
2044 current_thread
= saved_thread
;
2051 /* If we get a synchronous signal while collecting, *and*
2052 while executing the (relocated) original instruction,
2053 reset the PC to point at the tpoint address, before
2054 reporting to GDB. Otherwise, it's an IPA lib bug: just
2055 report the signal to GDB, and pray for the best. */
2057 lwp
->collecting_fast_tracepoint
2058 = fast_tpoint_collect_result::not_collecting
;
2060 if (r
!= fast_tpoint_collect_result::not_collecting
2061 && (status
.adjusted_insn_addr
<= lwp
->stop_pc
2062 && lwp
->stop_pc
< status
.adjusted_insn_addr_end
))
2065 struct regcache
*regcache
;
2067 /* The si_addr on a few signals references the address
2068 of the faulting instruction. Adjust that as
2070 if ((WSTOPSIG (*wstat
) == SIGILL
2071 || WSTOPSIG (*wstat
) == SIGFPE
2072 || WSTOPSIG (*wstat
) == SIGBUS
2073 || WSTOPSIG (*wstat
) == SIGSEGV
)
2074 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
2075 (PTRACE_TYPE_ARG3
) 0, &info
) == 0
2076 /* Final check just to make sure we don't clobber
2077 the siginfo of non-kernel-sent signals. */
2078 && (uintptr_t) info
.si_addr
== lwp
->stop_pc
)
2080 info
.si_addr
= (void *) (uintptr_t) status
.tpoint_addr
;
2081 ptrace (PTRACE_SETSIGINFO
, lwpid_of (current_thread
),
2082 (PTRACE_TYPE_ARG3
) 0, &info
);
2085 regcache
= get_thread_regcache (current_thread
, 1);
2086 low_set_pc (regcache
, status
.tpoint_addr
);
2087 lwp
->stop_pc
= status
.tpoint_addr
;
2089 /* Cancel any fast tracepoint lock this thread was
2091 force_unlock_trace_buffer ();
2094 if (lwp
->exit_jump_pad_bkpt
!= NULL
)
2097 debug_printf ("Cancelling fast exit-jump-pad: removing bkpt. "
2098 "stopping all threads momentarily.\n");
2100 stop_all_lwps (1, lwp
);
2102 delete_breakpoint (lwp
->exit_jump_pad_bkpt
);
2103 lwp
->exit_jump_pad_bkpt
= NULL
;
2105 unstop_all_lwps (1, lwp
);
2107 gdb_assert (lwp
->suspended
>= 0);
2113 debug_printf ("Checking whether LWP %ld needs to move out of the "
2115 lwpid_of (current_thread
));
2117 current_thread
= saved_thread
;
2121 /* Enqueue one signal in the "signals to report later when out of the
2125 enqueue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2127 struct pending_signals
*p_sig
;
2128 struct thread_info
*thread
= get_lwp_thread (lwp
);
2131 debug_printf ("Deferring signal %d for LWP %ld.\n",
2132 WSTOPSIG (*wstat
), lwpid_of (thread
));
2136 struct pending_signals
*sig
;
2138 for (sig
= lwp
->pending_signals_to_report
;
2141 debug_printf (" Already queued %d\n",
2144 debug_printf (" (no more currently queued signals)\n");
2147 /* Don't enqueue non-RT signals if they are already in the deferred
2148 queue. (SIGSTOP being the easiest signal to see ending up here
2150 if (WSTOPSIG (*wstat
) < __SIGRTMIN
)
2152 struct pending_signals
*sig
;
2154 for (sig
= lwp
->pending_signals_to_report
;
2158 if (sig
->signal
== WSTOPSIG (*wstat
))
2161 debug_printf ("Not requeuing already queued non-RT signal %d"
2170 p_sig
= XCNEW (struct pending_signals
);
2171 p_sig
->prev
= lwp
->pending_signals_to_report
;
2172 p_sig
->signal
= WSTOPSIG (*wstat
);
2174 ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2177 lwp
->pending_signals_to_report
= p_sig
;
2180 /* Dequeue one signal from the "signals to report later when out of
2181 the jump pad" list. */
2184 dequeue_one_deferred_signal (struct lwp_info
*lwp
, int *wstat
)
2186 struct thread_info
*thread
= get_lwp_thread (lwp
);
2188 if (lwp
->pending_signals_to_report
!= NULL
)
2190 struct pending_signals
**p_sig
;
2192 p_sig
= &lwp
->pending_signals_to_report
;
2193 while ((*p_sig
)->prev
!= NULL
)
2194 p_sig
= &(*p_sig
)->prev
;
2196 *wstat
= W_STOPCODE ((*p_sig
)->signal
);
2197 if ((*p_sig
)->info
.si_signo
!= 0)
2198 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
2204 debug_printf ("Reporting deferred signal %d for LWP %ld.\n",
2205 WSTOPSIG (*wstat
), lwpid_of (thread
));
2209 struct pending_signals
*sig
;
2211 for (sig
= lwp
->pending_signals_to_report
;
2214 debug_printf (" Still queued %d\n",
2217 debug_printf (" (no more queued signals)\n");
2227 linux_process_target::check_stopped_by_watchpoint (lwp_info
*child
)
2229 struct thread_info
*saved_thread
= current_thread
;
2230 current_thread
= get_lwp_thread (child
);
2232 if (low_stopped_by_watchpoint ())
2234 child
->stop_reason
= TARGET_STOPPED_BY_WATCHPOINT
;
2235 child
->stopped_data_address
= low_stopped_data_address ();
2238 current_thread
= saved_thread
;
2240 return child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
2244 linux_process_target::low_stopped_by_watchpoint ()
2250 linux_process_target::low_stopped_data_address ()
2255 /* Return the ptrace options that we want to try to enable. */
2258 linux_low_ptrace_options (int attached
)
2260 client_state
&cs
= get_client_state ();
2264 options
|= PTRACE_O_EXITKILL
;
2266 if (cs
.report_fork_events
)
2267 options
|= PTRACE_O_TRACEFORK
;
2269 if (cs
.report_vfork_events
)
2270 options
|= (PTRACE_O_TRACEVFORK
| PTRACE_O_TRACEVFORKDONE
);
2272 if (cs
.report_exec_events
)
2273 options
|= PTRACE_O_TRACEEXEC
;
2275 options
|= PTRACE_O_TRACESYSGOOD
;
2281 linux_process_target::filter_event (int lwpid
, int wstat
)
2283 client_state
&cs
= get_client_state ();
2284 struct lwp_info
*child
;
2285 struct thread_info
*thread
;
2286 int have_stop_pc
= 0;
2288 child
= find_lwp_pid (ptid_t (lwpid
));
2290 /* Check for stop events reported by a process we didn't already
2291 know about - anything not already in our LWP list.
2293 If we're expecting to receive stopped processes after
2294 fork, vfork, and clone events, then we'll just add the
2295 new one to our list and go back to waiting for the event
2296 to be reported - the stopped process might be returned
2297 from waitpid before or after the event is.
2299 But note the case of a non-leader thread exec'ing after the
2300 leader having exited, and gone from our lists (because
2301 check_zombie_leaders deleted it). The non-leader thread
2302 changes its tid to the tgid. */
2304 if (WIFSTOPPED (wstat
) && child
== NULL
&& WSTOPSIG (wstat
) == SIGTRAP
2305 && linux_ptrace_get_extended_event (wstat
) == PTRACE_EVENT_EXEC
)
2309 /* A multi-thread exec after we had seen the leader exiting. */
2312 debug_printf ("LLW: Re-adding thread group leader LWP %d"
2313 "after exec.\n", lwpid
);
2316 child_ptid
= ptid_t (lwpid
, lwpid
, 0);
2317 child
= add_lwp (child_ptid
);
2319 current_thread
= child
->thread
;
2322 /* If we didn't find a process, one of two things presumably happened:
2323 - A process we started and then detached from has exited. Ignore it.
2324 - A process we are controlling has forked and the new child's stop
2325 was reported to us by the kernel. Save its PID. */
2326 if (child
== NULL
&& WIFSTOPPED (wstat
))
2328 add_to_pid_list (&stopped_pids
, lwpid
, wstat
);
2331 else if (child
== NULL
)
2334 thread
= get_lwp_thread (child
);
2338 child
->last_status
= wstat
;
2340 /* Check if the thread has exited. */
2341 if ((WIFEXITED (wstat
) || WIFSIGNALED (wstat
)))
2344 debug_printf ("LLFE: %d exited.\n", lwpid
);
2346 if (finish_step_over (child
))
2348 /* Unsuspend all other LWPs, and set them back running again. */
2349 unsuspend_all_lwps (child
);
2352 /* If there is at least one more LWP, then the exit signal was
2353 not the end of the debugged application and should be
2354 ignored, unless GDB wants to hear about thread exits. */
2355 if (cs
.report_thread_events
2356 || last_thread_of_process_p (pid_of (thread
)))
2358 /* Since events are serialized to GDB core, and we can't
2359 report this one right now. Leave the status pending for
2360 the next time we're able to report it. */
2361 mark_lwp_dead (child
, wstat
);
2371 gdb_assert (WIFSTOPPED (wstat
));
2373 if (WIFSTOPPED (wstat
))
2375 struct process_info
*proc
;
2377 /* Architecture-specific setup after inferior is running. */
2378 proc
= find_process_pid (pid_of (thread
));
2379 if (proc
->tdesc
== NULL
)
2383 /* This needs to happen after we have attached to the
2384 inferior and it is stopped for the first time, but
2385 before we access any inferior registers. */
2386 arch_setup_thread (thread
);
2390 /* The process is started, but GDBserver will do
2391 architecture-specific setup after the program stops at
2392 the first instruction. */
2393 child
->status_pending_p
= 1;
2394 child
->status_pending
= wstat
;
2400 if (WIFSTOPPED (wstat
) && child
->must_set_ptrace_flags
)
2402 struct process_info
*proc
= find_process_pid (pid_of (thread
));
2403 int options
= linux_low_ptrace_options (proc
->attached
);
2405 linux_enable_event_reporting (lwpid
, options
);
2406 child
->must_set_ptrace_flags
= 0;
2409 /* Always update syscall_state, even if it will be filtered later. */
2410 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SYSCALL_SIGTRAP
)
2412 child
->syscall_state
2413 = (child
->syscall_state
== TARGET_WAITKIND_SYSCALL_ENTRY
2414 ? TARGET_WAITKIND_SYSCALL_RETURN
2415 : TARGET_WAITKIND_SYSCALL_ENTRY
);
2419 /* Almost all other ptrace-stops are known to be outside of system
2420 calls, with further exceptions in handle_extended_wait. */
2421 child
->syscall_state
= TARGET_WAITKIND_IGNORE
;
2424 /* Be careful to not overwrite stop_pc until save_stop_reason is
2426 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGTRAP
2427 && linux_is_extended_waitstatus (wstat
))
2429 child
->stop_pc
= get_pc (child
);
2430 if (handle_extended_wait (&child
, wstat
))
2432 /* The event has been handled, so just return without
2438 if (linux_wstatus_maybe_breakpoint (wstat
))
2440 if (save_stop_reason (child
))
2445 child
->stop_pc
= get_pc (child
);
2447 if (WIFSTOPPED (wstat
) && WSTOPSIG (wstat
) == SIGSTOP
2448 && child
->stop_expected
)
2451 debug_printf ("Expected stop.\n");
2452 child
->stop_expected
= 0;
2454 if (thread
->last_resume_kind
== resume_stop
)
2456 /* We want to report the stop to the core. Treat the
2457 SIGSTOP as a normal event. */
2459 debug_printf ("LLW: resume_stop SIGSTOP caught for %s.\n",
2460 target_pid_to_str (ptid_of (thread
)));
2462 else if (stopping_threads
!= NOT_STOPPING_THREADS
)
2464 /* Stopping threads. We don't want this SIGSTOP to end up
2467 debug_printf ("LLW: SIGSTOP caught for %s "
2468 "while stopping threads.\n",
2469 target_pid_to_str (ptid_of (thread
)));
2474 /* This is a delayed SIGSTOP. Filter out the event. */
2476 debug_printf ("LLW: %s %s, 0, 0 (discard delayed SIGSTOP)\n",
2477 child
->stepping
? "step" : "continue",
2478 target_pid_to_str (ptid_of (thread
)));
2480 resume_one_lwp (child
, child
->stepping
, 0, NULL
);
2485 child
->status_pending_p
= 1;
2486 child
->status_pending
= wstat
;
2491 linux_process_target::maybe_hw_step (thread_info
*thread
)
2493 if (supports_hardware_single_step ())
2497 /* GDBserver must insert single-step breakpoint for software
2499 gdb_assert (has_single_step_breakpoints (thread
));
2505 linux_process_target::resume_stopped_resumed_lwps (thread_info
*thread
)
2507 struct lwp_info
*lp
= get_thread_lwp (thread
);
2511 && !lp
->status_pending_p
2512 && thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
)
2516 if (thread
->last_resume_kind
== resume_step
)
2517 step
= maybe_hw_step (thread
);
2520 debug_printf ("RSRL: resuming stopped-resumed LWP %s at %s: step=%d\n",
2521 target_pid_to_str (ptid_of (thread
)),
2522 paddress (lp
->stop_pc
),
2525 resume_one_lwp (lp
, step
, GDB_SIGNAL_0
, NULL
);
2530 linux_process_target::wait_for_event_filtered (ptid_t wait_ptid
,
2532 int *wstatp
, int options
)
2534 struct thread_info
*event_thread
;
2535 struct lwp_info
*event_child
, *requested_child
;
2536 sigset_t block_mask
, prev_mask
;
2539 /* N.B. event_thread points to the thread_info struct that contains
2540 event_child. Keep them in sync. */
2541 event_thread
= NULL
;
2543 requested_child
= NULL
;
2545 /* Check for a lwp with a pending status. */
2547 if (filter_ptid
== minus_one_ptid
|| filter_ptid
.is_pid ())
2549 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2551 return status_pending_p_callback (thread
, filter_ptid
);
2554 if (event_thread
!= NULL
)
2555 event_child
= get_thread_lwp (event_thread
);
2556 if (debug_threads
&& event_thread
)
2557 debug_printf ("Got a pending child %ld\n", lwpid_of (event_thread
));
2559 else if (filter_ptid
!= null_ptid
)
2561 requested_child
= find_lwp_pid (filter_ptid
);
2563 if (stopping_threads
== NOT_STOPPING_THREADS
2564 && requested_child
->status_pending_p
2565 && (requested_child
->collecting_fast_tracepoint
2566 != fast_tpoint_collect_result::not_collecting
))
2568 enqueue_one_deferred_signal (requested_child
,
2569 &requested_child
->status_pending
);
2570 requested_child
->status_pending_p
= 0;
2571 requested_child
->status_pending
= 0;
2572 resume_one_lwp (requested_child
, 0, 0, NULL
);
2575 if (requested_child
->suspended
2576 && requested_child
->status_pending_p
)
2578 internal_error (__FILE__
, __LINE__
,
2579 "requesting an event out of a"
2580 " suspended child?");
2583 if (requested_child
->status_pending_p
)
2585 event_child
= requested_child
;
2586 event_thread
= get_lwp_thread (event_child
);
2590 if (event_child
!= NULL
)
2593 debug_printf ("Got an event from pending child %ld (%04x)\n",
2594 lwpid_of (event_thread
), event_child
->status_pending
);
2595 *wstatp
= event_child
->status_pending
;
2596 event_child
->status_pending_p
= 0;
2597 event_child
->status_pending
= 0;
2598 current_thread
= event_thread
;
2599 return lwpid_of (event_thread
);
2602 /* But if we don't find a pending event, we'll have to wait.
2604 We only enter this loop if no process has a pending wait status.
2605 Thus any action taken in response to a wait status inside this
2606 loop is responding as soon as we detect the status, not after any
2609 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2610 all signals while here. */
2611 sigfillset (&block_mask
);
2612 gdb_sigmask (SIG_BLOCK
, &block_mask
, &prev_mask
);
2614 /* Always pull all events out of the kernel. We'll randomly select
2615 an event LWP out of all that have events, to prevent
2617 while (event_child
== NULL
)
2621 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2624 - If the thread group leader exits while other threads in the
2625 thread group still exist, waitpid(TGID, ...) hangs. That
2626 waitpid won't return an exit status until the other threads
2627 in the group are reaped.
2629 - When a non-leader thread execs, that thread just vanishes
2630 without reporting an exit (so we'd hang if we waited for it
2631 explicitly in that case). The exec event is reported to
2634 ret
= my_waitpid (-1, wstatp
, options
| WNOHANG
);
2637 debug_printf ("LWFE: waitpid(-1, ...) returned %d, %s\n",
2638 ret
, errno
? safe_strerror (errno
) : "ERRNO-OK");
2644 debug_printf ("LLW: waitpid %ld received %s\n",
2645 (long) ret
, status_to_str (*wstatp
));
2648 /* Filter all events. IOW, leave all events pending. We'll
2649 randomly select an event LWP out of all that have events
2651 filter_event (ret
, *wstatp
);
2652 /* Retry until nothing comes out of waitpid. A single
2653 SIGCHLD can indicate more than one child stopped. */
2657 /* Now that we've pulled all events out of the kernel, resume
2658 LWPs that don't have an interesting event to report. */
2659 if (stopping_threads
== NOT_STOPPING_THREADS
)
2660 for_each_thread ([this] (thread_info
*thread
)
2662 resume_stopped_resumed_lwps (thread
);
2665 /* ... and find an LWP with a status to report to the core, if
2667 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2669 return status_pending_p_callback (thread
, filter_ptid
);
2672 if (event_thread
!= NULL
)
2674 event_child
= get_thread_lwp (event_thread
);
2675 *wstatp
= event_child
->status_pending
;
2676 event_child
->status_pending_p
= 0;
2677 event_child
->status_pending
= 0;
2681 /* Check for zombie thread group leaders. Those can't be reaped
2682 until all other threads in the thread group are. */
2683 check_zombie_leaders ();
2685 auto not_stopped
= [&] (thread_info
*thread
)
2687 return not_stopped_callback (thread
, wait_ptid
);
2690 /* If there are no resumed children left in the set of LWPs we
2691 want to wait for, bail. We can't just block in
2692 waitpid/sigsuspend, because lwps might have been left stopped
2693 in trace-stop state, and we'd be stuck forever waiting for
2694 their status to change (which would only happen if we resumed
2695 them). Even if WNOHANG is set, this return code is preferred
2696 over 0 (below), as it is more detailed. */
2697 if (find_thread (not_stopped
) == NULL
)
2700 debug_printf ("LLW: exit (no unwaited-for LWP)\n");
2701 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2705 /* No interesting event to report to the caller. */
2706 if ((options
& WNOHANG
))
2709 debug_printf ("WNOHANG set, no event found\n");
2711 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2715 /* Block until we get an event reported with SIGCHLD. */
2717 debug_printf ("sigsuspend'ing\n");
2719 sigsuspend (&prev_mask
);
2720 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2724 gdb_sigmask (SIG_SETMASK
, &prev_mask
, NULL
);
2726 current_thread
= event_thread
;
2728 return lwpid_of (event_thread
);
2732 linux_process_target::wait_for_event (ptid_t ptid
, int *wstatp
, int options
)
2734 return wait_for_event_filtered (ptid
, ptid
, wstatp
, options
);
2737 /* Select one LWP out of those that have events pending. */
2740 select_event_lwp (struct lwp_info
**orig_lp
)
2742 struct thread_info
*event_thread
= NULL
;
2744 /* In all-stop, give preference to the LWP that is being
2745 single-stepped. There will be at most one, and it's the LWP that
2746 the core is most interested in. If we didn't do this, then we'd
2747 have to handle pending step SIGTRAPs somehow in case the core
2748 later continues the previously-stepped thread, otherwise we'd
2749 report the pending SIGTRAP, and the core, not having stepped the
2750 thread, wouldn't understand what the trap was for, and therefore
2751 would report it to the user as a random signal. */
2754 event_thread
= find_thread ([] (thread_info
*thread
)
2756 lwp_info
*lp
= get_thread_lwp (thread
);
2758 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2759 && thread
->last_resume_kind
== resume_step
2760 && lp
->status_pending_p
);
2763 if (event_thread
!= NULL
)
2766 debug_printf ("SEL: Select single-step %s\n",
2767 target_pid_to_str (ptid_of (event_thread
)));
2770 if (event_thread
== NULL
)
2772 /* No single-stepping LWP. Select one at random, out of those
2773 which have had events. */
2775 event_thread
= find_thread_in_random ([&] (thread_info
*thread
)
2777 lwp_info
*lp
= get_thread_lwp (thread
);
2779 /* Only resumed LWPs that have an event pending. */
2780 return (thread
->last_status
.kind
== TARGET_WAITKIND_IGNORE
2781 && lp
->status_pending_p
);
2785 if (event_thread
!= NULL
)
2787 struct lwp_info
*event_lp
= get_thread_lwp (event_thread
);
2789 /* Switch the event LWP. */
2790 *orig_lp
= event_lp
;
2794 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2798 unsuspend_all_lwps (struct lwp_info
*except
)
2800 for_each_thread ([&] (thread_info
*thread
)
2802 lwp_info
*lwp
= get_thread_lwp (thread
);
2805 lwp_suspended_decr (lwp
);
2809 static bool lwp_running (thread_info
*thread
);
2811 /* Stabilize threads (move out of jump pads).
2813 If a thread is midway collecting a fast tracepoint, we need to
2814 finish the collection and move it out of the jump pad before
2815 reporting the signal.
2817 This avoids recursion while collecting (when a signal arrives
2818 midway, and the signal handler itself collects), which would trash
2819 the trace buffer. In case the user set a breakpoint in a signal
2820 handler, this avoids the backtrace showing the jump pad, etc..
2821 Most importantly, there are certain things we can't do safely if
2822 threads are stopped in a jump pad (or in its callee's). For
2825 - starting a new trace run. A thread still collecting the
2826 previous run, could trash the trace buffer when resumed. The trace
2827 buffer control structures would have been reset but the thread had
2828 no way to tell. The thread could even midway memcpy'ing to the
2829 buffer, which would mean that when resumed, it would clobber the
2830 trace buffer that had been set for a new run.
2832 - we can't rewrite/reuse the jump pads for new tracepoints
2833 safely. Say you do tstart while a thread is stopped midway while
2834 collecting. When the thread is later resumed, it finishes the
2835 collection, and returns to the jump pad, to execute the original
2836 instruction that was under the tracepoint jump at the time the
2837 older run had been started. If the jump pad had been rewritten
2838 since for something else in the new run, the thread would now
2839 execute the wrong / random instructions. */
2842 linux_process_target::stabilize_threads ()
2844 thread_info
*thread_stuck
= find_thread ([this] (thread_info
*thread
)
2846 return stuck_in_jump_pad (thread
);
2849 if (thread_stuck
!= NULL
)
2852 debug_printf ("can't stabilize, LWP %ld is stuck in jump pad\n",
2853 lwpid_of (thread_stuck
));
2857 thread_info
*saved_thread
= current_thread
;
2859 stabilizing_threads
= 1;
2862 for_each_thread ([this] (thread_info
*thread
)
2864 move_out_of_jump_pad (thread
);
2867 /* Loop until all are stopped out of the jump pads. */
2868 while (find_thread (lwp_running
) != NULL
)
2870 struct target_waitstatus ourstatus
;
2871 struct lwp_info
*lwp
;
2874 /* Note that we go through the full wait even loop. While
2875 moving threads out of jump pad, we need to be able to step
2876 over internal breakpoints and such. */
2877 wait_1 (minus_one_ptid
, &ourstatus
, 0);
2879 if (ourstatus
.kind
== TARGET_WAITKIND_STOPPED
)
2881 lwp
= get_thread_lwp (current_thread
);
2884 lwp_suspended_inc (lwp
);
2886 if (ourstatus
.value
.sig
!= GDB_SIGNAL_0
2887 || current_thread
->last_resume_kind
== resume_stop
)
2889 wstat
= W_STOPCODE (gdb_signal_to_host (ourstatus
.value
.sig
));
2890 enqueue_one_deferred_signal (lwp
, &wstat
);
2895 unsuspend_all_lwps (NULL
);
2897 stabilizing_threads
= 0;
2899 current_thread
= saved_thread
;
2903 thread_stuck
= find_thread ([this] (thread_info
*thread
)
2905 return stuck_in_jump_pad (thread
);
2908 if (thread_stuck
!= NULL
)
2909 debug_printf ("couldn't stabilize, LWP %ld got stuck in jump pad\n",
2910 lwpid_of (thread_stuck
));
2914 /* Convenience function that is called when the kernel reports an
2915 event that is not passed out to GDB. */
2918 ignore_event (struct target_waitstatus
*ourstatus
)
2920 /* If we got an event, there may still be others, as a single
2921 SIGCHLD can indicate more than one child stopped. This forces
2922 another target_wait call. */
2925 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2930 linux_process_target::filter_exit_event (lwp_info
*event_child
,
2931 target_waitstatus
*ourstatus
)
2933 client_state
&cs
= get_client_state ();
2934 struct thread_info
*thread
= get_lwp_thread (event_child
);
2935 ptid_t ptid
= ptid_of (thread
);
2937 if (!last_thread_of_process_p (pid_of (thread
)))
2939 if (cs
.report_thread_events
)
2940 ourstatus
->kind
= TARGET_WAITKIND_THREAD_EXITED
;
2942 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
2944 delete_lwp (event_child
);
2949 /* Returns 1 if GDB is interested in any event_child syscalls. */
2952 gdb_catching_syscalls_p (struct lwp_info
*event_child
)
2954 struct thread_info
*thread
= get_lwp_thread (event_child
);
2955 struct process_info
*proc
= get_thread_process (thread
);
2957 return !proc
->syscalls_to_catch
.empty ();
2961 linux_process_target::gdb_catch_this_syscall (lwp_info
*event_child
)
2964 struct thread_info
*thread
= get_lwp_thread (event_child
);
2965 struct process_info
*proc
= get_thread_process (thread
);
2967 if (proc
->syscalls_to_catch
.empty ())
2970 if (proc
->syscalls_to_catch
[0] == ANY_SYSCALL
)
2973 get_syscall_trapinfo (event_child
, &sysno
);
2975 for (int iter
: proc
->syscalls_to_catch
)
2983 linux_process_target::wait_1 (ptid_t ptid
, target_waitstatus
*ourstatus
,
2986 client_state
&cs
= get_client_state ();
2988 struct lwp_info
*event_child
;
2991 int step_over_finished
;
2992 int bp_explains_trap
;
2993 int maybe_internal_trap
;
3002 debug_printf ("wait_1: [%s]\n", target_pid_to_str (ptid
));
3005 /* Translate generic target options into linux options. */
3007 if (target_options
& TARGET_WNOHANG
)
3010 bp_explains_trap
= 0;
3013 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3015 auto status_pending_p_any
= [&] (thread_info
*thread
)
3017 return status_pending_p_callback (thread
, minus_one_ptid
);
3020 auto not_stopped
= [&] (thread_info
*thread
)
3022 return not_stopped_callback (thread
, minus_one_ptid
);
3025 /* Find a resumed LWP, if any. */
3026 if (find_thread (status_pending_p_any
) != NULL
)
3028 else if (find_thread (not_stopped
) != NULL
)
3033 if (step_over_bkpt
== null_ptid
)
3034 pid
= wait_for_event (ptid
, &w
, options
);
3038 debug_printf ("step_over_bkpt set [%s], doing a blocking wait\n",
3039 target_pid_to_str (step_over_bkpt
));
3040 pid
= wait_for_event (step_over_bkpt
, &w
, options
& ~WNOHANG
);
3043 if (pid
== 0 || (pid
== -1 && !any_resumed
))
3045 gdb_assert (target_options
& TARGET_WNOHANG
);
3049 debug_printf ("wait_1 ret = null_ptid, "
3050 "TARGET_WAITKIND_IGNORE\n");
3054 ourstatus
->kind
= TARGET_WAITKIND_IGNORE
;
3061 debug_printf ("wait_1 ret = null_ptid, "
3062 "TARGET_WAITKIND_NO_RESUMED\n");
3066 ourstatus
->kind
= TARGET_WAITKIND_NO_RESUMED
;
3070 event_child
= get_thread_lwp (current_thread
);
3072 /* wait_for_event only returns an exit status for the last
3073 child of a process. Report it. */
3074 if (WIFEXITED (w
) || WIFSIGNALED (w
))
3078 ourstatus
->kind
= TARGET_WAITKIND_EXITED
;
3079 ourstatus
->value
.integer
= WEXITSTATUS (w
);
3083 debug_printf ("wait_1 ret = %s, exited with "
3085 target_pid_to_str (ptid_of (current_thread
)),
3092 ourstatus
->kind
= TARGET_WAITKIND_SIGNALLED
;
3093 ourstatus
->value
.sig
= gdb_signal_from_host (WTERMSIG (w
));
3097 debug_printf ("wait_1 ret = %s, terminated with "
3099 target_pid_to_str (ptid_of (current_thread
)),
3105 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3106 return filter_exit_event (event_child
, ourstatus
);
3108 return ptid_of (current_thread
);
3111 /* If step-over executes a breakpoint instruction, in the case of a
3112 hardware single step it means a gdb/gdbserver breakpoint had been
3113 planted on top of a permanent breakpoint, in the case of a software
3114 single step it may just mean that gdbserver hit the reinsert breakpoint.
3115 The PC has been adjusted by save_stop_reason to point at
3116 the breakpoint address.
3117 So in the case of the hardware single step advance the PC manually
3118 past the breakpoint and in the case of software single step advance only
3119 if it's not the single_step_breakpoint we are hitting.
3120 This avoids that a program would keep trapping a permanent breakpoint
3122 if (step_over_bkpt
!= null_ptid
3123 && event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3124 && (event_child
->stepping
3125 || !single_step_breakpoint_inserted_here (event_child
->stop_pc
)))
3127 int increment_pc
= 0;
3128 int breakpoint_kind
= 0;
3129 CORE_ADDR stop_pc
= event_child
->stop_pc
;
3131 breakpoint_kind
= breakpoint_kind_from_current_state (&stop_pc
);
3132 sw_breakpoint_from_kind (breakpoint_kind
, &increment_pc
);
3136 debug_printf ("step-over for %s executed software breakpoint\n",
3137 target_pid_to_str (ptid_of (current_thread
)));
3140 if (increment_pc
!= 0)
3142 struct regcache
*regcache
3143 = get_thread_regcache (current_thread
, 1);
3145 event_child
->stop_pc
+= increment_pc
;
3146 low_set_pc (regcache
, event_child
->stop_pc
);
3148 if (!low_breakpoint_at (event_child
->stop_pc
))
3149 event_child
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
3153 /* If this event was not handled before, and is not a SIGTRAP, we
3154 report it. SIGILL and SIGSEGV are also treated as traps in case
3155 a breakpoint is inserted at the current PC. If this target does
3156 not support internal breakpoints at all, we also report the
3157 SIGTRAP without further processing; it's of no concern to us. */
3159 = (low_supports_breakpoints ()
3160 && (WSTOPSIG (w
) == SIGTRAP
3161 || ((WSTOPSIG (w
) == SIGILL
3162 || WSTOPSIG (w
) == SIGSEGV
)
3163 && low_breakpoint_at (event_child
->stop_pc
))));
3165 if (maybe_internal_trap
)
3167 /* Handle anything that requires bookkeeping before deciding to
3168 report the event or continue waiting. */
3170 /* First check if we can explain the SIGTRAP with an internal
3171 breakpoint, or if we should possibly report the event to GDB.
3172 Do this before anything that may remove or insert a
3174 bp_explains_trap
= breakpoint_inserted_here (event_child
->stop_pc
);
3176 /* We have a SIGTRAP, possibly a step-over dance has just
3177 finished. If so, tweak the state machine accordingly,
3178 reinsert breakpoints and delete any single-step
3180 step_over_finished
= finish_step_over (event_child
);
3182 /* Now invoke the callbacks of any internal breakpoints there. */
3183 check_breakpoints (event_child
->stop_pc
);
3185 /* Handle tracepoint data collecting. This may overflow the
3186 trace buffer, and cause a tracing stop, removing
3188 trace_event
= handle_tracepoints (event_child
);
3190 if (bp_explains_trap
)
3193 debug_printf ("Hit a gdbserver breakpoint.\n");
3198 /* We have some other signal, possibly a step-over dance was in
3199 progress, and it should be cancelled too. */
3200 step_over_finished
= finish_step_over (event_child
);
3203 /* We have all the data we need. Either report the event to GDB, or
3204 resume threads and keep waiting for more. */
3206 /* If we're collecting a fast tracepoint, finish the collection and
3207 move out of the jump pad before delivering a signal. See
3208 linux_stabilize_threads. */
3211 && WSTOPSIG (w
) != SIGTRAP
3212 && supports_fast_tracepoints ()
3213 && agent_loaded_p ())
3216 debug_printf ("Got signal %d for LWP %ld. Check if we need "
3217 "to defer or adjust it.\n",
3218 WSTOPSIG (w
), lwpid_of (current_thread
));
3220 /* Allow debugging the jump pad itself. */
3221 if (current_thread
->last_resume_kind
!= resume_step
3222 && maybe_move_out_of_jump_pad (event_child
, &w
))
3224 enqueue_one_deferred_signal (event_child
, &w
);
3227 debug_printf ("Signal %d for LWP %ld deferred (in jump pad)\n",
3228 WSTOPSIG (w
), lwpid_of (current_thread
));
3230 resume_one_lwp (event_child
, 0, 0, NULL
);
3234 return ignore_event (ourstatus
);
3238 if (event_child
->collecting_fast_tracepoint
3239 != fast_tpoint_collect_result::not_collecting
)
3242 debug_printf ("LWP %ld was trying to move out of the jump pad (%d). "
3243 "Check if we're already there.\n",
3244 lwpid_of (current_thread
),
3245 (int) event_child
->collecting_fast_tracepoint
);
3249 event_child
->collecting_fast_tracepoint
3250 = linux_fast_tracepoint_collecting (event_child
, NULL
);
3252 if (event_child
->collecting_fast_tracepoint
3253 != fast_tpoint_collect_result::before_insn
)
3255 /* No longer need this breakpoint. */
3256 if (event_child
->exit_jump_pad_bkpt
!= NULL
)
3259 debug_printf ("No longer need exit-jump-pad bkpt; removing it."
3260 "stopping all threads momentarily.\n");
3262 /* Other running threads could hit this breakpoint.
3263 We don't handle moribund locations like GDB does,
3264 instead we always pause all threads when removing
3265 breakpoints, so that any step-over or
3266 decr_pc_after_break adjustment is always taken
3267 care of while the breakpoint is still
3269 stop_all_lwps (1, event_child
);
3271 delete_breakpoint (event_child
->exit_jump_pad_bkpt
);
3272 event_child
->exit_jump_pad_bkpt
= NULL
;
3274 unstop_all_lwps (1, event_child
);
3276 gdb_assert (event_child
->suspended
>= 0);
3280 if (event_child
->collecting_fast_tracepoint
3281 == fast_tpoint_collect_result::not_collecting
)
3284 debug_printf ("fast tracepoint finished "
3285 "collecting successfully.\n");
3287 /* We may have a deferred signal to report. */
3288 if (dequeue_one_deferred_signal (event_child
, &w
))
3291 debug_printf ("dequeued one signal.\n");
3296 debug_printf ("no deferred signals.\n");
3298 if (stabilizing_threads
)
3300 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3301 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3305 debug_printf ("wait_1 ret = %s, stopped "
3306 "while stabilizing threads\n",
3307 target_pid_to_str (ptid_of (current_thread
)));
3311 return ptid_of (current_thread
);
3317 /* Check whether GDB would be interested in this event. */
3319 /* Check if GDB is interested in this syscall. */
3321 && WSTOPSIG (w
) == SYSCALL_SIGTRAP
3322 && !gdb_catch_this_syscall (event_child
))
3326 debug_printf ("Ignored syscall for LWP %ld.\n",
3327 lwpid_of (current_thread
));
3330 resume_one_lwp (event_child
, event_child
->stepping
, 0, NULL
);
3334 return ignore_event (ourstatus
);
3337 /* If GDB is not interested in this signal, don't stop other
3338 threads, and don't report it to GDB. Just resume the inferior
3339 right away. We do this for threading-related signals as well as
3340 any that GDB specifically requested we ignore. But never ignore
3341 SIGSTOP if we sent it ourselves, and do not ignore signals when
3342 stepping - they may require special handling to skip the signal
3343 handler. Also never ignore signals that could be caused by a
3346 && current_thread
->last_resume_kind
!= resume_step
3348 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3349 (current_process ()->priv
->thread_db
!= NULL
3350 && (WSTOPSIG (w
) == __SIGRTMIN
3351 || WSTOPSIG (w
) == __SIGRTMIN
+ 1))
3354 (cs
.pass_signals
[gdb_signal_from_host (WSTOPSIG (w
))]
3355 && !(WSTOPSIG (w
) == SIGSTOP
3356 && current_thread
->last_resume_kind
== resume_stop
)
3357 && !linux_wstatus_maybe_breakpoint (w
))))
3359 siginfo_t info
, *info_p
;
3362 debug_printf ("Ignored signal %d for LWP %ld.\n",
3363 WSTOPSIG (w
), lwpid_of (current_thread
));
3365 if (ptrace (PTRACE_GETSIGINFO
, lwpid_of (current_thread
),
3366 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
3371 if (step_over_finished
)
3373 /* We cancelled this thread's step-over above. We still
3374 need to unsuspend all other LWPs, and set them back
3375 running again while the signal handler runs. */
3376 unsuspend_all_lwps (event_child
);
3378 /* Enqueue the pending signal info so that proceed_all_lwps
3380 enqueue_pending_signal (event_child
, WSTOPSIG (w
), info_p
);
3382 proceed_all_lwps ();
3386 resume_one_lwp (event_child
, event_child
->stepping
,
3387 WSTOPSIG (w
), info_p
);
3393 return ignore_event (ourstatus
);
3396 /* Note that all addresses are always "out of the step range" when
3397 there's no range to begin with. */
3398 in_step_range
= lwp_in_step_range (event_child
);
3400 /* If GDB wanted this thread to single step, and the thread is out
3401 of the step range, we always want to report the SIGTRAP, and let
3402 GDB handle it. Watchpoints should always be reported. So should
3403 signals we can't explain. A SIGTRAP we can't explain could be a
3404 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3405 do, we're be able to handle GDB breakpoints on top of internal
3406 breakpoints, by handling the internal breakpoint and still
3407 reporting the event to GDB. If we don't, we're out of luck, GDB
3408 won't see the breakpoint hit. If we see a single-step event but
3409 the thread should be continuing, don't pass the trap to gdb.
3410 That indicates that we had previously finished a single-step but
3411 left the single-step pending -- see
3412 complete_ongoing_step_over. */
3413 report_to_gdb
= (!maybe_internal_trap
3414 || (current_thread
->last_resume_kind
== resume_step
3416 || event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3418 && !bp_explains_trap
3420 && !step_over_finished
3421 && !(current_thread
->last_resume_kind
== resume_continue
3422 && event_child
->stop_reason
== TARGET_STOPPED_BY_SINGLE_STEP
))
3423 || (gdb_breakpoint_here (event_child
->stop_pc
)
3424 && gdb_condition_true_at_breakpoint (event_child
->stop_pc
)
3425 && gdb_no_commands_at_breakpoint (event_child
->stop_pc
))
3426 || event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
);
3428 run_breakpoint_commands (event_child
->stop_pc
);
3430 /* We found no reason GDB would want us to stop. We either hit one
3431 of our own breakpoints, or finished an internal step GDB
3432 shouldn't know about. */
3437 if (bp_explains_trap
)
3438 debug_printf ("Hit a gdbserver breakpoint.\n");
3439 if (step_over_finished
)
3440 debug_printf ("Step-over finished.\n");
3442 debug_printf ("Tracepoint event.\n");
3443 if (lwp_in_step_range (event_child
))
3444 debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).\n",
3445 paddress (event_child
->stop_pc
),
3446 paddress (event_child
->step_range_start
),
3447 paddress (event_child
->step_range_end
));
3450 /* We're not reporting this breakpoint to GDB, so apply the
3451 decr_pc_after_break adjustment to the inferior's regcache
3454 if (low_supports_breakpoints ())
3456 struct regcache
*regcache
3457 = get_thread_regcache (current_thread
, 1);
3458 low_set_pc (regcache
, event_child
->stop_pc
);
3461 if (step_over_finished
)
3463 /* If we have finished stepping over a breakpoint, we've
3464 stopped and suspended all LWPs momentarily except the
3465 stepping one. This is where we resume them all again.
3466 We're going to keep waiting, so use proceed, which
3467 handles stepping over the next breakpoint. */
3468 unsuspend_all_lwps (event_child
);
3472 /* Remove the single-step breakpoints if any. Note that
3473 there isn't single-step breakpoint if we finished stepping
3475 if (supports_software_single_step ()
3476 && has_single_step_breakpoints (current_thread
))
3478 stop_all_lwps (0, event_child
);
3479 delete_single_step_breakpoints (current_thread
);
3480 unstop_all_lwps (0, event_child
);
3485 debug_printf ("proceeding all threads.\n");
3486 proceed_all_lwps ();
3491 return ignore_event (ourstatus
);
3496 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3499 = target_waitstatus_to_string (&event_child
->waitstatus
);
3501 debug_printf ("LWP %ld: extended event with waitstatus %s\n",
3502 lwpid_of (get_lwp_thread (event_child
)), str
.c_str ());
3504 if (current_thread
->last_resume_kind
== resume_step
)
3506 if (event_child
->step_range_start
== event_child
->step_range_end
)
3507 debug_printf ("GDB wanted to single-step, reporting event.\n");
3508 else if (!lwp_in_step_range (event_child
))
3509 debug_printf ("Out of step range, reporting event.\n");
3511 if (event_child
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
)
3512 debug_printf ("Stopped by watchpoint.\n");
3513 else if (gdb_breakpoint_here (event_child
->stop_pc
))
3514 debug_printf ("Stopped by GDB breakpoint.\n");
3516 debug_printf ("Hit a non-gdbserver trap event.\n");
3519 /* Alright, we're going to report a stop. */
3521 /* Remove single-step breakpoints. */
3522 if (supports_software_single_step ())
3524 /* Remove single-step breakpoints or not. It it is true, stop all
3525 lwps, so that other threads won't hit the breakpoint in the
3527 int remove_single_step_breakpoints_p
= 0;
3531 remove_single_step_breakpoints_p
3532 = has_single_step_breakpoints (current_thread
);
3536 /* In all-stop, a stop reply cancels all previous resume
3537 requests. Delete all single-step breakpoints. */
3539 find_thread ([&] (thread_info
*thread
) {
3540 if (has_single_step_breakpoints (thread
))
3542 remove_single_step_breakpoints_p
= 1;
3550 if (remove_single_step_breakpoints_p
)
3552 /* If we remove single-step breakpoints from memory, stop all lwps,
3553 so that other threads won't hit the breakpoint in the staled
3555 stop_all_lwps (0, event_child
);
3559 gdb_assert (has_single_step_breakpoints (current_thread
));
3560 delete_single_step_breakpoints (current_thread
);
3564 for_each_thread ([] (thread_info
*thread
){
3565 if (has_single_step_breakpoints (thread
))
3566 delete_single_step_breakpoints (thread
);
3570 unstop_all_lwps (0, event_child
);
3574 if (!stabilizing_threads
)
3576 /* In all-stop, stop all threads. */
3578 stop_all_lwps (0, NULL
);
3580 if (step_over_finished
)
3584 /* If we were doing a step-over, all other threads but
3585 the stepping one had been paused in start_step_over,
3586 with their suspend counts incremented. We don't want
3587 to do a full unstop/unpause, because we're in
3588 all-stop mode (so we want threads stopped), but we
3589 still need to unsuspend the other threads, to
3590 decrement their `suspended' count back. */
3591 unsuspend_all_lwps (event_child
);
3595 /* If we just finished a step-over, then all threads had
3596 been momentarily paused. In all-stop, that's fine,
3597 we want threads stopped by now anyway. In non-stop,
3598 we need to re-resume threads that GDB wanted to be
3600 unstop_all_lwps (1, event_child
);
3604 /* If we're not waiting for a specific LWP, choose an event LWP
3605 from among those that have had events. Giving equal priority
3606 to all LWPs that have had events helps prevent
3608 if (ptid
== minus_one_ptid
)
3610 event_child
->status_pending_p
= 1;
3611 event_child
->status_pending
= w
;
3613 select_event_lwp (&event_child
);
3615 /* current_thread and event_child must stay in sync. */
3616 current_thread
= get_lwp_thread (event_child
);
3618 event_child
->status_pending_p
= 0;
3619 w
= event_child
->status_pending
;
3623 /* Stabilize threads (move out of jump pads). */
3625 target_stabilize_threads ();
3629 /* If we just finished a step-over, then all threads had been
3630 momentarily paused. In all-stop, that's fine, we want
3631 threads stopped by now anyway. In non-stop, we need to
3632 re-resume threads that GDB wanted to be running. */
3633 if (step_over_finished
)
3634 unstop_all_lwps (1, event_child
);
3637 if (event_child
->waitstatus
.kind
!= TARGET_WAITKIND_IGNORE
)
3639 /* If the reported event is an exit, fork, vfork or exec, let
3642 /* Break the unreported fork relationship chain. */
3643 if (event_child
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
3644 || event_child
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
)
3646 event_child
->fork_relative
->fork_relative
= NULL
;
3647 event_child
->fork_relative
= NULL
;
3650 *ourstatus
= event_child
->waitstatus
;
3651 /* Clear the event lwp's waitstatus since we handled it already. */
3652 event_child
->waitstatus
.kind
= TARGET_WAITKIND_IGNORE
;
3655 ourstatus
->kind
= TARGET_WAITKIND_STOPPED
;
3657 /* Now that we've selected our final event LWP, un-adjust its PC if
3658 it was a software breakpoint, and the client doesn't know we can
3659 adjust the breakpoint ourselves. */
3660 if (event_child
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
3661 && !cs
.swbreak_feature
)
3663 int decr_pc
= low_decr_pc_after_break ();
3667 struct regcache
*regcache
3668 = get_thread_regcache (current_thread
, 1);
3669 low_set_pc (regcache
, event_child
->stop_pc
+ decr_pc
);
3673 if (WSTOPSIG (w
) == SYSCALL_SIGTRAP
)
3675 get_syscall_trapinfo (event_child
,
3676 &ourstatus
->value
.syscall_number
);
3677 ourstatus
->kind
= event_child
->syscall_state
;
3679 else if (current_thread
->last_resume_kind
== resume_stop
3680 && WSTOPSIG (w
) == SIGSTOP
)
3682 /* A thread that has been requested to stop by GDB with vCont;t,
3683 and it stopped cleanly, so report as SIG0. The use of
3684 SIGSTOP is an implementation detail. */
3685 ourstatus
->value
.sig
= GDB_SIGNAL_0
;
3687 else if (current_thread
->last_resume_kind
== resume_stop
3688 && WSTOPSIG (w
) != SIGSTOP
)
3690 /* A thread that has been requested to stop by GDB with vCont;t,
3691 but, it stopped for other reasons. */
3692 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3694 else if (ourstatus
->kind
== TARGET_WAITKIND_STOPPED
)
3696 ourstatus
->value
.sig
= gdb_signal_from_host (WSTOPSIG (w
));
3699 gdb_assert (step_over_bkpt
== null_ptid
);
3703 debug_printf ("wait_1 ret = %s, %d, %d\n",
3704 target_pid_to_str (ptid_of (current_thread
)),
3705 ourstatus
->kind
, ourstatus
->value
.sig
);
3709 if (ourstatus
->kind
== TARGET_WAITKIND_EXITED
)
3710 return filter_exit_event (event_child
, ourstatus
);
3712 return ptid_of (current_thread
);
3715 /* Get rid of any pending event in the pipe. */
3717 async_file_flush (void)
3723 ret
= read (linux_event_pipe
[0], &buf
, 1);
3724 while (ret
>= 0 || (ret
== -1 && errno
== EINTR
));
3727 /* Put something in the pipe, so the event loop wakes up. */
3729 async_file_mark (void)
3733 async_file_flush ();
3736 ret
= write (linux_event_pipe
[1], "+", 1);
3737 while (ret
== 0 || (ret
== -1 && errno
== EINTR
));
3739 /* Ignore EAGAIN. If the pipe is full, the event loop will already
3740 be awakened anyway. */
3744 linux_process_target::wait (ptid_t ptid
,
3745 target_waitstatus
*ourstatus
,
3750 /* Flush the async file first. */
3751 if (target_is_async_p ())
3752 async_file_flush ();
3756 event_ptid
= wait_1 (ptid
, ourstatus
, target_options
);
3758 while ((target_options
& TARGET_WNOHANG
) == 0
3759 && event_ptid
== null_ptid
3760 && ourstatus
->kind
== TARGET_WAITKIND_IGNORE
);
3762 /* If at least one stop was reported, there may be more. A single
3763 SIGCHLD can signal more than one child stop. */
3764 if (target_is_async_p ()
3765 && (target_options
& TARGET_WNOHANG
) != 0
3766 && event_ptid
!= null_ptid
)
3772 /* Send a signal to an LWP. */
3775 kill_lwp (unsigned long lwpid
, int signo
)
3780 ret
= syscall (__NR_tkill
, lwpid
, signo
);
3781 if (errno
== ENOSYS
)
3783 /* If tkill fails, then we are not using nptl threads, a
3784 configuration we no longer support. */
3785 perror_with_name (("tkill"));
3791 linux_stop_lwp (struct lwp_info
*lwp
)
3797 send_sigstop (struct lwp_info
*lwp
)
3801 pid
= lwpid_of (get_lwp_thread (lwp
));
3803 /* If we already have a pending stop signal for this process, don't
3805 if (lwp
->stop_expected
)
3808 debug_printf ("Have pending sigstop for lwp %d\n", pid
);
3814 debug_printf ("Sending sigstop to lwp %d\n", pid
);
3816 lwp
->stop_expected
= 1;
3817 kill_lwp (pid
, SIGSTOP
);
3821 send_sigstop (thread_info
*thread
, lwp_info
*except
)
3823 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3825 /* Ignore EXCEPT. */
3835 /* Increment the suspend count of an LWP, and stop it, if not stopped
3838 suspend_and_send_sigstop (thread_info
*thread
, lwp_info
*except
)
3840 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3842 /* Ignore EXCEPT. */
3846 lwp_suspended_inc (lwp
);
3848 send_sigstop (thread
, except
);
3852 mark_lwp_dead (struct lwp_info
*lwp
, int wstat
)
3854 /* Store the exit status for later. */
3855 lwp
->status_pending_p
= 1;
3856 lwp
->status_pending
= wstat
;
3858 /* Store in waitstatus as well, as there's nothing else to process
3860 if (WIFEXITED (wstat
))
3862 lwp
->waitstatus
.kind
= TARGET_WAITKIND_EXITED
;
3863 lwp
->waitstatus
.value
.integer
= WEXITSTATUS (wstat
);
3865 else if (WIFSIGNALED (wstat
))
3867 lwp
->waitstatus
.kind
= TARGET_WAITKIND_SIGNALLED
;
3868 lwp
->waitstatus
.value
.sig
= gdb_signal_from_host (WTERMSIG (wstat
));
3871 /* Prevent trying to stop it. */
3874 /* No further stops are expected from a dead lwp. */
3875 lwp
->stop_expected
= 0;
3878 /* Return true if LWP has exited already, and has a pending exit event
3879 to report to GDB. */
3882 lwp_is_marked_dead (struct lwp_info
*lwp
)
3884 return (lwp
->status_pending_p
3885 && (WIFEXITED (lwp
->status_pending
)
3886 || WIFSIGNALED (lwp
->status_pending
)));
3890 linux_process_target::wait_for_sigstop ()
3892 struct thread_info
*saved_thread
;
3897 saved_thread
= current_thread
;
3898 if (saved_thread
!= NULL
)
3899 saved_tid
= saved_thread
->id
;
3901 saved_tid
= null_ptid
; /* avoid bogus unused warning */
3904 debug_printf ("wait_for_sigstop: pulling events\n");
3906 /* Passing NULL_PTID as filter indicates we want all events to be
3907 left pending. Eventually this returns when there are no
3908 unwaited-for children left. */
3909 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
, __WALL
);
3910 gdb_assert (ret
== -1);
3912 if (saved_thread
== NULL
|| mythread_alive (saved_tid
))
3913 current_thread
= saved_thread
;
3917 debug_printf ("Previously current thread died.\n");
3919 /* We can't change the current inferior behind GDB's back,
3920 otherwise, a subsequent command may apply to the wrong
3922 current_thread
= NULL
;
3927 linux_process_target::stuck_in_jump_pad (thread_info
*thread
)
3929 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3931 if (lwp
->suspended
!= 0)
3933 internal_error (__FILE__
, __LINE__
,
3934 "LWP %ld is suspended, suspended=%d\n",
3935 lwpid_of (thread
), lwp
->suspended
);
3937 gdb_assert (lwp
->stopped
);
3939 /* Allow debugging the jump pad, gdb_collect, etc.. */
3940 return (supports_fast_tracepoints ()
3941 && agent_loaded_p ()
3942 && (gdb_breakpoint_here (lwp
->stop_pc
)
3943 || lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
3944 || thread
->last_resume_kind
== resume_step
)
3945 && (linux_fast_tracepoint_collecting (lwp
, NULL
)
3946 != fast_tpoint_collect_result::not_collecting
));
3950 linux_process_target::move_out_of_jump_pad (thread_info
*thread
)
3952 struct thread_info
*saved_thread
;
3953 struct lwp_info
*lwp
= get_thread_lwp (thread
);
3956 if (lwp
->suspended
!= 0)
3958 internal_error (__FILE__
, __LINE__
,
3959 "LWP %ld is suspended, suspended=%d\n",
3960 lwpid_of (thread
), lwp
->suspended
);
3962 gdb_assert (lwp
->stopped
);
3964 /* For gdb_breakpoint_here. */
3965 saved_thread
= current_thread
;
3966 current_thread
= thread
;
3968 wstat
= lwp
->status_pending_p
? &lwp
->status_pending
: NULL
;
3970 /* Allow debugging the jump pad, gdb_collect, etc. */
3971 if (!gdb_breakpoint_here (lwp
->stop_pc
)
3972 && lwp
->stop_reason
!= TARGET_STOPPED_BY_WATCHPOINT
3973 && thread
->last_resume_kind
!= resume_step
3974 && maybe_move_out_of_jump_pad (lwp
, wstat
))
3977 debug_printf ("LWP %ld needs stabilizing (in jump pad)\n",
3982 lwp
->status_pending_p
= 0;
3983 enqueue_one_deferred_signal (lwp
, wstat
);
3986 debug_printf ("Signal %d for LWP %ld deferred "
3988 WSTOPSIG (*wstat
), lwpid_of (thread
));
3991 resume_one_lwp (lwp
, 0, 0, NULL
);
3994 lwp_suspended_inc (lwp
);
3996 current_thread
= saved_thread
;
4000 lwp_running (thread_info
*thread
)
4002 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4004 if (lwp_is_marked_dead (lwp
))
4007 return !lwp
->stopped
;
4011 linux_process_target::stop_all_lwps (int suspend
, lwp_info
*except
)
4013 /* Should not be called recursively. */
4014 gdb_assert (stopping_threads
== NOT_STOPPING_THREADS
);
4019 debug_printf ("stop_all_lwps (%s, except=%s)\n",
4020 suspend
? "stop-and-suspend" : "stop",
4022 ? target_pid_to_str (ptid_of (get_lwp_thread (except
)))
4026 stopping_threads
= (suspend
4027 ? STOPPING_AND_SUSPENDING_THREADS
4028 : STOPPING_THREADS
);
4031 for_each_thread ([&] (thread_info
*thread
)
4033 suspend_and_send_sigstop (thread
, except
);
4036 for_each_thread ([&] (thread_info
*thread
)
4038 send_sigstop (thread
, except
);
4041 wait_for_sigstop ();
4042 stopping_threads
= NOT_STOPPING_THREADS
;
4046 debug_printf ("stop_all_lwps done, setting stopping_threads "
4047 "back to !stopping\n");
4052 /* Enqueue one signal in the chain of signals which need to be
4053 delivered to this process on next resume. */
4056 enqueue_pending_signal (struct lwp_info
*lwp
, int signal
, siginfo_t
*info
)
4058 struct pending_signals
*p_sig
= XNEW (struct pending_signals
);
4060 p_sig
->prev
= lwp
->pending_signals
;
4061 p_sig
->signal
= signal
;
4063 memset (&p_sig
->info
, 0, sizeof (siginfo_t
));
4065 memcpy (&p_sig
->info
, info
, sizeof (siginfo_t
));
4066 lwp
->pending_signals
= p_sig
;
4070 linux_process_target::install_software_single_step_breakpoints (lwp_info
*lwp
)
4072 struct thread_info
*thread
= get_lwp_thread (lwp
);
4073 struct regcache
*regcache
= get_thread_regcache (thread
, 1);
4075 scoped_restore save_current_thread
= make_scoped_restore (¤t_thread
);
4077 current_thread
= thread
;
4078 std::vector
<CORE_ADDR
> next_pcs
= low_get_next_pcs (regcache
);
4080 for (CORE_ADDR pc
: next_pcs
)
4081 set_single_step_breakpoint (pc
, current_ptid
);
4085 linux_process_target::single_step (lwp_info
* lwp
)
4089 if (supports_hardware_single_step ())
4093 else if (supports_software_single_step ())
4095 install_software_single_step_breakpoints (lwp
);
4101 debug_printf ("stepping is not implemented on this target");
4107 /* The signal can be delivered to the inferior if we are not trying to
4108 finish a fast tracepoint collect. Since signal can be delivered in
4109 the step-over, the program may go to signal handler and trap again
4110 after return from the signal handler. We can live with the spurious
4114 lwp_signal_can_be_delivered (struct lwp_info
*lwp
)
4116 return (lwp
->collecting_fast_tracepoint
4117 == fast_tpoint_collect_result::not_collecting
);
4121 linux_process_target::resume_one_lwp_throw (lwp_info
*lwp
, int step
,
4122 int signal
, siginfo_t
*info
)
4124 struct thread_info
*thread
= get_lwp_thread (lwp
);
4125 struct thread_info
*saved_thread
;
4127 struct process_info
*proc
= get_thread_process (thread
);
4129 /* Note that target description may not be initialised
4130 (proc->tdesc == NULL) at this point because the program hasn't
4131 stopped at the first instruction yet. It means GDBserver skips
4132 the extra traps from the wrapper program (see option --wrapper).
4133 Code in this function that requires register access should be
4134 guarded by proc->tdesc == NULL or something else. */
4136 if (lwp
->stopped
== 0)
4139 gdb_assert (lwp
->waitstatus
.kind
== TARGET_WAITKIND_IGNORE
);
4141 fast_tpoint_collect_result fast_tp_collecting
4142 = lwp
->collecting_fast_tracepoint
;
4144 gdb_assert (!stabilizing_threads
4145 || (fast_tp_collecting
4146 != fast_tpoint_collect_result::not_collecting
));
4148 /* Cancel actions that rely on GDB not changing the PC (e.g., the
4149 user used the "jump" command, or "set $pc = foo"). */
4150 if (thread
->while_stepping
!= NULL
&& lwp
->stop_pc
!= get_pc (lwp
))
4152 /* Collecting 'while-stepping' actions doesn't make sense
4154 release_while_stepping_state_list (thread
);
4157 /* If we have pending signals or status, and a new signal, enqueue the
4158 signal. Also enqueue the signal if it can't be delivered to the
4159 inferior right now. */
4161 && (lwp
->status_pending_p
4162 || lwp
->pending_signals
!= NULL
4163 || !lwp_signal_can_be_delivered (lwp
)))
4165 enqueue_pending_signal (lwp
, signal
, info
);
4167 /* Postpone any pending signal. It was enqueued above. */
4171 if (lwp
->status_pending_p
)
4174 debug_printf ("Not resuming lwp %ld (%s, stop %s);"
4175 " has pending status\n",
4176 lwpid_of (thread
), step
? "step" : "continue",
4177 lwp
->stop_expected
? "expected" : "not expected");
4181 saved_thread
= current_thread
;
4182 current_thread
= thread
;
4184 /* This bit needs some thinking about. If we get a signal that
4185 we must report while a single-step reinsert is still pending,
4186 we often end up resuming the thread. It might be better to
4187 (ew) allow a stack of pending events; then we could be sure that
4188 the reinsert happened right away and not lose any signals.
4190 Making this stack would also shrink the window in which breakpoints are
4191 uninserted (see comment in linux_wait_for_lwp) but not enough for
4192 complete correctness, so it won't solve that problem. It may be
4193 worthwhile just to solve this one, however. */
4194 if (lwp
->bp_reinsert
!= 0)
4197 debug_printf (" pending reinsert at 0x%s\n",
4198 paddress (lwp
->bp_reinsert
));
4200 if (supports_hardware_single_step ())
4202 if (fast_tp_collecting
== fast_tpoint_collect_result::not_collecting
)
4205 warning ("BAD - reinserting but not stepping.");
4207 warning ("BAD - reinserting and suspended(%d).",
4212 step
= maybe_hw_step (thread
);
4215 if (fast_tp_collecting
== fast_tpoint_collect_result::before_insn
)
4218 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4219 " (exit-jump-pad-bkpt)\n",
4222 else if (fast_tp_collecting
== fast_tpoint_collect_result::at_insn
)
4225 debug_printf ("lwp %ld wants to get out of fast tracepoint jump pad"
4226 " single-stepping\n",
4229 if (supports_hardware_single_step ())
4233 internal_error (__FILE__
, __LINE__
,
4234 "moving out of jump pad single-stepping"
4235 " not implemented on this target");
4239 /* If we have while-stepping actions in this thread set it stepping.
4240 If we have a signal to deliver, it may or may not be set to
4241 SIG_IGN, we don't know. Assume so, and allow collecting
4242 while-stepping into a signal handler. A possible smart thing to
4243 do would be to set an internal breakpoint at the signal return
4244 address, continue, and carry on catching this while-stepping
4245 action only when that breakpoint is hit. A future
4247 if (thread
->while_stepping
!= NULL
)
4250 debug_printf ("lwp %ld has a while-stepping action -> forcing step.\n",
4253 step
= single_step (lwp
);
4256 if (proc
->tdesc
!= NULL
&& low_supports_breakpoints ())
4258 struct regcache
*regcache
= get_thread_regcache (current_thread
, 1);
4260 lwp
->stop_pc
= low_get_pc (regcache
);
4264 debug_printf (" %s from pc 0x%lx\n", step
? "step" : "continue",
4265 (long) lwp
->stop_pc
);
4269 /* If we have pending signals, consume one if it can be delivered to
4271 if (lwp
->pending_signals
!= NULL
&& lwp_signal_can_be_delivered (lwp
))
4273 struct pending_signals
**p_sig
;
4275 p_sig
= &lwp
->pending_signals
;
4276 while ((*p_sig
)->prev
!= NULL
)
4277 p_sig
= &(*p_sig
)->prev
;
4279 signal
= (*p_sig
)->signal
;
4280 if ((*p_sig
)->info
.si_signo
!= 0)
4281 ptrace (PTRACE_SETSIGINFO
, lwpid_of (thread
), (PTRACE_TYPE_ARG3
) 0,
4289 debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)\n",
4290 lwpid_of (thread
), step
? "step" : "continue", signal
,
4291 lwp
->stop_expected
? "expected" : "not expected");
4293 low_prepare_to_resume (lwp
);
4295 regcache_invalidate_thread (thread
);
4297 lwp
->stepping
= step
;
4299 ptrace_request
= PTRACE_SINGLESTEP
;
4300 else if (gdb_catching_syscalls_p (lwp
))
4301 ptrace_request
= PTRACE_SYSCALL
;
4303 ptrace_request
= PTRACE_CONT
;
4304 ptrace (ptrace_request
,
4306 (PTRACE_TYPE_ARG3
) 0,
4307 /* Coerce to a uintptr_t first to avoid potential gcc warning
4308 of coercing an 8 byte integer to a 4 byte pointer. */
4309 (PTRACE_TYPE_ARG4
) (uintptr_t) signal
);
4311 current_thread
= saved_thread
;
4313 perror_with_name ("resuming thread");
4315 /* Successfully resumed. Clear state that no longer makes sense,
4316 and mark the LWP as running. Must not do this before resuming
4317 otherwise if that fails other code will be confused. E.g., we'd
4318 later try to stop the LWP and hang forever waiting for a stop
4319 status. Note that we must not throw after this is cleared,
4320 otherwise handle_zombie_lwp_error would get confused. */
4322 lwp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4326 linux_process_target::low_prepare_to_resume (lwp_info
*lwp
)
4331 /* Called when we try to resume a stopped LWP and that errors out. If
4332 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4333 or about to become), discard the error, clear any pending status
4334 the LWP may have, and return true (we'll collect the exit status
4335 soon enough). Otherwise, return false. */
4338 check_ptrace_stopped_lwp_gone (struct lwp_info
*lp
)
4340 struct thread_info
*thread
= get_lwp_thread (lp
);
4342 /* If we get an error after resuming the LWP successfully, we'd
4343 confuse !T state for the LWP being gone. */
4344 gdb_assert (lp
->stopped
);
4346 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4347 because even if ptrace failed with ESRCH, the tracee may be "not
4348 yet fully dead", but already refusing ptrace requests. In that
4349 case the tracee has 'R (Running)' state for a little bit
4350 (observed in Linux 3.18). See also the note on ESRCH in the
4351 ptrace(2) man page. Instead, check whether the LWP has any state
4352 other than ptrace-stopped. */
4354 /* Don't assume anything if /proc/PID/status can't be read. */
4355 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread
)) == 0)
4357 lp
->stop_reason
= TARGET_STOPPED_BY_NO_REASON
;
4358 lp
->status_pending_p
= 0;
4365 linux_process_target::resume_one_lwp (lwp_info
*lwp
, int step
, int signal
,
4370 resume_one_lwp_throw (lwp
, step
, signal
, info
);
4372 catch (const gdb_exception_error
&ex
)
4374 if (!check_ptrace_stopped_lwp_gone (lwp
))
4379 /* This function is called once per thread via for_each_thread.
4380 We look up which resume request applies to THREAD and mark it with a
4381 pointer to the appropriate resume request.
4383 This algorithm is O(threads * resume elements), but resume elements
4384 is small (and will remain small at least until GDB supports thread
4388 linux_set_resume_request (thread_info
*thread
, thread_resume
*resume
, size_t n
)
4390 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4392 for (int ndx
= 0; ndx
< n
; ndx
++)
4394 ptid_t ptid
= resume
[ndx
].thread
;
4395 if (ptid
== minus_one_ptid
4396 || ptid
== thread
->id
4397 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4399 || (ptid
.pid () == pid_of (thread
)
4401 || ptid
.lwp () == -1)))
4403 if (resume
[ndx
].kind
== resume_stop
4404 && thread
->last_resume_kind
== resume_stop
)
4407 debug_printf ("already %s LWP %ld at GDB's request\n",
4408 (thread
->last_status
.kind
4409 == TARGET_WAITKIND_STOPPED
)
4417 /* Ignore (wildcard) resume requests for already-resumed
4419 if (resume
[ndx
].kind
!= resume_stop
4420 && thread
->last_resume_kind
!= resume_stop
)
4423 debug_printf ("already %s LWP %ld at GDB's request\n",
4424 (thread
->last_resume_kind
4432 /* Don't let wildcard resumes resume fork children that GDB
4433 does not yet know are new fork children. */
4434 if (lwp
->fork_relative
!= NULL
)
4436 struct lwp_info
*rel
= lwp
->fork_relative
;
4438 if (rel
->status_pending_p
4439 && (rel
->waitstatus
.kind
== TARGET_WAITKIND_FORKED
4440 || rel
->waitstatus
.kind
== TARGET_WAITKIND_VFORKED
))
4443 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4449 /* If the thread has a pending event that has already been
4450 reported to GDBserver core, but GDB has not pulled the
4451 event out of the vStopped queue yet, likewise, ignore the
4452 (wildcard) resume request. */
4453 if (in_queued_stop_replies (thread
->id
))
4456 debug_printf ("not resuming LWP %ld: has queued stop reply\n",
4461 lwp
->resume
= &resume
[ndx
];
4462 thread
->last_resume_kind
= lwp
->resume
->kind
;
4464 lwp
->step_range_start
= lwp
->resume
->step_range_start
;
4465 lwp
->step_range_end
= lwp
->resume
->step_range_end
;
4467 /* If we had a deferred signal to report, dequeue one now.
4468 This can happen if LWP gets more than one signal while
4469 trying to get out of a jump pad. */
4471 && !lwp
->status_pending_p
4472 && dequeue_one_deferred_signal (lwp
, &lwp
->status_pending
))
4474 lwp
->status_pending_p
= 1;
4477 debug_printf ("Dequeueing deferred signal %d for LWP %ld, "
4478 "leaving status pending.\n",
4479 WSTOPSIG (lwp
->status_pending
),
4487 /* No resume action for this thread. */
4492 linux_process_target::resume_status_pending (thread_info
*thread
)
4494 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4496 /* LWPs which will not be resumed are not interesting, because
4497 we might not wait for them next time through linux_wait. */
4498 if (lwp
->resume
== NULL
)
4501 return thread_still_has_status_pending (thread
);
4505 linux_process_target::thread_needs_step_over (thread_info
*thread
)
4507 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4508 struct thread_info
*saved_thread
;
4510 struct process_info
*proc
= get_thread_process (thread
);
4512 /* GDBserver is skipping the extra traps from the wrapper program,
4513 don't have to do step over. */
4514 if (proc
->tdesc
== NULL
)
4517 /* LWPs which will not be resumed are not interesting, because we
4518 might not wait for them next time through linux_wait. */
4523 debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped\n",
4528 if (thread
->last_resume_kind
== resume_stop
)
4531 debug_printf ("Need step over [LWP %ld]? Ignoring, should remain"
4537 gdb_assert (lwp
->suspended
>= 0);
4542 debug_printf ("Need step over [LWP %ld]? Ignoring, suspended\n",
4547 if (lwp
->status_pending_p
)
4550 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4556 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4560 /* If the PC has changed since we stopped, then don't do anything,
4561 and let the breakpoint/tracepoint be hit. This happens if, for
4562 instance, GDB handled the decr_pc_after_break subtraction itself,
4563 GDB is OOL stepping this thread, or the user has issued a "jump"
4564 command, or poked thread's registers herself. */
4565 if (pc
!= lwp
->stop_pc
)
4568 debug_printf ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4569 "Old stop_pc was 0x%s, PC is now 0x%s\n",
4571 paddress (lwp
->stop_pc
), paddress (pc
));
4575 /* On software single step target, resume the inferior with signal
4576 rather than stepping over. */
4577 if (supports_software_single_step ()
4578 && lwp
->pending_signals
!= NULL
4579 && lwp_signal_can_be_delivered (lwp
))
4582 debug_printf ("Need step over [LWP %ld]? Ignoring, has pending"
4589 saved_thread
= current_thread
;
4590 current_thread
= thread
;
4592 /* We can only step over breakpoints we know about. */
4593 if (breakpoint_here (pc
) || fast_tracepoint_jump_here (pc
))
4595 /* Don't step over a breakpoint that GDB expects to hit
4596 though. If the condition is being evaluated on the target's side
4597 and it evaluate to false, step over this breakpoint as well. */
4598 if (gdb_breakpoint_here (pc
)
4599 && gdb_condition_true_at_breakpoint (pc
)
4600 && gdb_no_commands_at_breakpoint (pc
))
4603 debug_printf ("Need step over [LWP %ld]? yes, but found"
4604 " GDB breakpoint at 0x%s; skipping step over\n",
4605 lwpid_of (thread
), paddress (pc
));
4607 current_thread
= saved_thread
;
4613 debug_printf ("Need step over [LWP %ld]? yes, "
4614 "found breakpoint at 0x%s\n",
4615 lwpid_of (thread
), paddress (pc
));
4617 /* We've found an lwp that needs stepping over --- return 1 so
4618 that find_thread stops looking. */
4619 current_thread
= saved_thread
;
4625 current_thread
= saved_thread
;
4628 debug_printf ("Need step over [LWP %ld]? No, no breakpoint found"
4630 lwpid_of (thread
), paddress (pc
));
4636 linux_process_target::start_step_over (lwp_info
*lwp
)
4638 struct thread_info
*thread
= get_lwp_thread (lwp
);
4639 struct thread_info
*saved_thread
;
4644 debug_printf ("Starting step-over on LWP %ld. Stopping all threads\n",
4647 stop_all_lwps (1, lwp
);
4649 if (lwp
->suspended
!= 0)
4651 internal_error (__FILE__
, __LINE__
,
4652 "LWP %ld suspended=%d\n", lwpid_of (thread
),
4657 debug_printf ("Done stopping all threads for step-over.\n");
4659 /* Note, we should always reach here with an already adjusted PC,
4660 either by GDB (if we're resuming due to GDB's request), or by our
4661 caller, if we just finished handling an internal breakpoint GDB
4662 shouldn't care about. */
4665 saved_thread
= current_thread
;
4666 current_thread
= thread
;
4668 lwp
->bp_reinsert
= pc
;
4669 uninsert_breakpoints_at (pc
);
4670 uninsert_fast_tracepoint_jumps_at (pc
);
4672 step
= single_step (lwp
);
4674 current_thread
= saved_thread
;
4676 resume_one_lwp (lwp
, step
, 0, NULL
);
4678 /* Require next event from this LWP. */
4679 step_over_bkpt
= thread
->id
;
4683 linux_process_target::finish_step_over (lwp_info
*lwp
)
4685 if (lwp
->bp_reinsert
!= 0)
4687 struct thread_info
*saved_thread
= current_thread
;
4690 debug_printf ("Finished step over.\n");
4692 current_thread
= get_lwp_thread (lwp
);
4694 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4695 may be no breakpoint to reinsert there by now. */
4696 reinsert_breakpoints_at (lwp
->bp_reinsert
);
4697 reinsert_fast_tracepoint_jumps_at (lwp
->bp_reinsert
);
4699 lwp
->bp_reinsert
= 0;
4701 /* Delete any single-step breakpoints. No longer needed. We
4702 don't have to worry about other threads hitting this trap,
4703 and later not being able to explain it, because we were
4704 stepping over a breakpoint, and we hold all threads but
4705 LWP stopped while doing that. */
4706 if (!supports_hardware_single_step ())
4708 gdb_assert (has_single_step_breakpoints (current_thread
));
4709 delete_single_step_breakpoints (current_thread
);
4712 step_over_bkpt
= null_ptid
;
4713 current_thread
= saved_thread
;
4721 linux_process_target::complete_ongoing_step_over ()
4723 if (step_over_bkpt
!= null_ptid
)
4725 struct lwp_info
*lwp
;
4730 debug_printf ("detach: step over in progress, finish it first\n");
4732 /* Passing NULL_PTID as filter indicates we want all events to
4733 be left pending. Eventually this returns when there are no
4734 unwaited-for children left. */
4735 ret
= wait_for_event_filtered (minus_one_ptid
, null_ptid
, &wstat
,
4737 gdb_assert (ret
== -1);
4739 lwp
= find_lwp_pid (step_over_bkpt
);
4741 finish_step_over (lwp
);
4742 step_over_bkpt
= null_ptid
;
4743 unsuspend_all_lwps (lwp
);
4748 linux_process_target::resume_one_thread (thread_info
*thread
,
4749 bool leave_all_stopped
)
4751 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4754 if (lwp
->resume
== NULL
)
4757 if (lwp
->resume
->kind
== resume_stop
)
4760 debug_printf ("resume_stop request for LWP %ld\n", lwpid_of (thread
));
4765 debug_printf ("stopping LWP %ld\n", lwpid_of (thread
));
4767 /* Stop the thread, and wait for the event asynchronously,
4768 through the event loop. */
4774 debug_printf ("already stopped LWP %ld\n",
4777 /* The LWP may have been stopped in an internal event that
4778 was not meant to be notified back to GDB (e.g., gdbserver
4779 breakpoint), so we should be reporting a stop event in
4782 /* If the thread already has a pending SIGSTOP, this is a
4783 no-op. Otherwise, something later will presumably resume
4784 the thread and this will cause it to cancel any pending
4785 operation, due to last_resume_kind == resume_stop. If
4786 the thread already has a pending status to report, we
4787 will still report it the next time we wait - see
4788 status_pending_p_callback. */
4790 /* If we already have a pending signal to report, then
4791 there's no need to queue a SIGSTOP, as this means we're
4792 midway through moving the LWP out of the jumppad, and we
4793 will report the pending signal as soon as that is
4795 if (lwp
->pending_signals_to_report
== NULL
)
4799 /* For stop requests, we're done. */
4801 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4805 /* If this thread which is about to be resumed has a pending status,
4806 then don't resume it - we can just report the pending status.
4807 Likewise if it is suspended, because e.g., another thread is
4808 stepping past a breakpoint. Make sure to queue any signals that
4809 would otherwise be sent. In all-stop mode, we do this decision
4810 based on if *any* thread has a pending status. If there's a
4811 thread that needs the step-over-breakpoint dance, then don't
4812 resume any other thread but that particular one. */
4813 leave_pending
= (lwp
->suspended
4814 || lwp
->status_pending_p
4815 || leave_all_stopped
);
4817 /* If we have a new signal, enqueue the signal. */
4818 if (lwp
->resume
->sig
!= 0)
4820 siginfo_t info
, *info_p
;
4822 /* If this is the same signal we were previously stopped by,
4823 make sure to queue its siginfo. */
4824 if (WIFSTOPPED (lwp
->last_status
)
4825 && WSTOPSIG (lwp
->last_status
) == lwp
->resume
->sig
4826 && ptrace (PTRACE_GETSIGINFO
, lwpid_of (thread
),
4827 (PTRACE_TYPE_ARG3
) 0, &info
) == 0)
4832 enqueue_pending_signal (lwp
, lwp
->resume
->sig
, info_p
);
4838 debug_printf ("resuming LWP %ld\n", lwpid_of (thread
));
4840 proceed_one_lwp (thread
, NULL
);
4845 debug_printf ("leaving LWP %ld stopped\n", lwpid_of (thread
));
4848 thread
->last_status
.kind
= TARGET_WAITKIND_IGNORE
;
4853 linux_process_target::resume (thread_resume
*resume_info
, size_t n
)
4855 struct thread_info
*need_step_over
= NULL
;
4860 debug_printf ("linux_resume:\n");
4863 for_each_thread ([&] (thread_info
*thread
)
4865 linux_set_resume_request (thread
, resume_info
, n
);
4868 /* If there is a thread which would otherwise be resumed, which has
4869 a pending status, then don't resume any threads - we can just
4870 report the pending status. Make sure to queue any signals that
4871 would otherwise be sent. In non-stop mode, we'll apply this
4872 logic to each thread individually. We consume all pending events
4873 before considering to start a step-over (in all-stop). */
4874 bool any_pending
= false;
4876 any_pending
= find_thread ([this] (thread_info
*thread
)
4878 return resume_status_pending (thread
);
4881 /* If there is a thread which would otherwise be resumed, which is
4882 stopped at a breakpoint that needs stepping over, then don't
4883 resume any threads - have it step over the breakpoint with all
4884 other threads stopped, then resume all threads again. Make sure
4885 to queue any signals that would otherwise be delivered or
4887 if (!any_pending
&& low_supports_breakpoints ())
4888 need_step_over
= find_thread ([this] (thread_info
*thread
)
4890 return thread_needs_step_over (thread
);
4893 bool leave_all_stopped
= (need_step_over
!= NULL
|| any_pending
);
4897 if (need_step_over
!= NULL
)
4898 debug_printf ("Not resuming all, need step over\n");
4899 else if (any_pending
)
4900 debug_printf ("Not resuming, all-stop and found "
4901 "an LWP with pending status\n");
4903 debug_printf ("Resuming, no pending status or step over needed\n");
4906 /* Even if we're leaving threads stopped, queue all signals we'd
4907 otherwise deliver. */
4908 for_each_thread ([&] (thread_info
*thread
)
4910 resume_one_thread (thread
, leave_all_stopped
);
4914 start_step_over (get_thread_lwp (need_step_over
));
4918 debug_printf ("linux_resume done\n");
4922 /* We may have events that were pending that can/should be sent to
4923 the client now. Trigger a linux_wait call. */
4924 if (target_is_async_p ())
4929 linux_process_target::proceed_one_lwp (thread_info
*thread
, lwp_info
*except
)
4931 struct lwp_info
*lwp
= get_thread_lwp (thread
);
4938 debug_printf ("proceed_one_lwp: lwp %ld\n", lwpid_of (thread
));
4943 debug_printf (" LWP %ld already running\n", lwpid_of (thread
));
4947 if (thread
->last_resume_kind
== resume_stop
4948 && thread
->last_status
.kind
!= TARGET_WAITKIND_IGNORE
)
4951 debug_printf (" client wants LWP to remain %ld stopped\n",
4956 if (lwp
->status_pending_p
)
4959 debug_printf (" LWP %ld has pending status, leaving stopped\n",
4964 gdb_assert (lwp
->suspended
>= 0);
4969 debug_printf (" LWP %ld is suspended\n", lwpid_of (thread
));
4973 if (thread
->last_resume_kind
== resume_stop
4974 && lwp
->pending_signals_to_report
== NULL
4975 && (lwp
->collecting_fast_tracepoint
4976 == fast_tpoint_collect_result::not_collecting
))
4978 /* We haven't reported this LWP as stopped yet (otherwise, the
4979 last_status.kind check above would catch it, and we wouldn't
4980 reach here. This LWP may have been momentarily paused by a
4981 stop_all_lwps call while handling for example, another LWP's
4982 step-over. In that case, the pending expected SIGSTOP signal
4983 that was queued at vCont;t handling time will have already
4984 been consumed by wait_for_sigstop, and so we need to requeue
4985 another one here. Note that if the LWP already has a SIGSTOP
4986 pending, this is a no-op. */
4989 debug_printf ("Client wants LWP %ld to stop. "
4990 "Making sure it has a SIGSTOP pending\n",
4996 if (thread
->last_resume_kind
== resume_step
)
4999 debug_printf (" stepping LWP %ld, client wants it stepping\n",
5002 /* If resume_step is requested by GDB, install single-step
5003 breakpoints when the thread is about to be actually resumed if
5004 the single-step breakpoints weren't removed. */
5005 if (supports_software_single_step ()
5006 && !has_single_step_breakpoints (thread
))
5007 install_software_single_step_breakpoints (lwp
);
5009 step
= maybe_hw_step (thread
);
5011 else if (lwp
->bp_reinsert
!= 0)
5014 debug_printf (" stepping LWP %ld, reinsert set\n",
5017 step
= maybe_hw_step (thread
);
5022 resume_one_lwp (lwp
, step
, 0, NULL
);
5026 linux_process_target::unsuspend_and_proceed_one_lwp (thread_info
*thread
,
5029 struct lwp_info
*lwp
= get_thread_lwp (thread
);
5034 lwp_suspended_decr (lwp
);
5036 proceed_one_lwp (thread
, except
);
5040 linux_process_target::proceed_all_lwps ()
5042 struct thread_info
*need_step_over
;
5044 /* If there is a thread which would otherwise be resumed, which is
5045 stopped at a breakpoint that needs stepping over, then don't
5046 resume any threads - have it step over the breakpoint with all
5047 other threads stopped, then resume all threads again. */
5049 if (low_supports_breakpoints ())
5051 need_step_over
= find_thread ([this] (thread_info
*thread
)
5053 return thread_needs_step_over (thread
);
5056 if (need_step_over
!= NULL
)
5059 debug_printf ("proceed_all_lwps: found "
5060 "thread %ld needing a step-over\n",
5061 lwpid_of (need_step_over
));
5063 start_step_over (get_thread_lwp (need_step_over
));
5069 debug_printf ("Proceeding, no step-over needed\n");
5071 for_each_thread ([this] (thread_info
*thread
)
5073 proceed_one_lwp (thread
, NULL
);
5078 linux_process_target::unstop_all_lwps (int unsuspend
, lwp_info
*except
)
5084 debug_printf ("unstopping all lwps, except=(LWP %ld)\n",
5085 lwpid_of (get_lwp_thread (except
)));
5087 debug_printf ("unstopping all lwps\n");
5091 for_each_thread ([&] (thread_info
*thread
)
5093 unsuspend_and_proceed_one_lwp (thread
, except
);
5096 for_each_thread ([&] (thread_info
*thread
)
5098 proceed_one_lwp (thread
, except
);
5103 debug_printf ("unstop_all_lwps done\n");
5109 #ifdef HAVE_LINUX_REGSETS
5111 #define use_linux_regsets 1
5113 /* Returns true if REGSET has been disabled. */
5116 regset_disabled (struct regsets_info
*info
, struct regset_info
*regset
)
5118 return (info
->disabled_regsets
!= NULL
5119 && info
->disabled_regsets
[regset
- info
->regsets
]);
5122 /* Disable REGSET. */
5125 disable_regset (struct regsets_info
*info
, struct regset_info
*regset
)
5129 dr_offset
= regset
- info
->regsets
;
5130 if (info
->disabled_regsets
== NULL
)
5131 info
->disabled_regsets
= (char *) xcalloc (1, info
->num_regsets
);
5132 info
->disabled_regsets
[dr_offset
] = 1;
5136 regsets_fetch_inferior_registers (struct regsets_info
*regsets_info
,
5137 struct regcache
*regcache
)
5139 struct regset_info
*regset
;
5140 int saw_general_regs
= 0;
5144 pid
= lwpid_of (current_thread
);
5145 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5150 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
))
5153 buf
= xmalloc (regset
->size
);
5155 nt_type
= regset
->nt_type
;
5159 iov
.iov_len
= regset
->size
;
5160 data
= (void *) &iov
;
5166 res
= ptrace (regset
->get_request
, pid
,
5167 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5169 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5174 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5176 /* If we get EIO on a regset, or an EINVAL and the regset is
5177 optional, do not try it again for this process mode. */
5178 disable_regset (regsets_info
, regset
);
5180 else if (errno
== ENODATA
)
5182 /* ENODATA may be returned if the regset is currently
5183 not "active". This can happen in normal operation,
5184 so suppress the warning in this case. */
5186 else if (errno
== ESRCH
)
5188 /* At this point, ESRCH should mean the process is
5189 already gone, in which case we simply ignore attempts
5190 to read its registers. */
5195 sprintf (s
, "ptrace(regsets_fetch_inferior_registers) PID=%d",
5202 if (regset
->type
== GENERAL_REGS
)
5203 saw_general_regs
= 1;
5204 regset
->store_function (regcache
, buf
);
5208 if (saw_general_regs
)
5215 regsets_store_inferior_registers (struct regsets_info
*regsets_info
,
5216 struct regcache
*regcache
)
5218 struct regset_info
*regset
;
5219 int saw_general_regs
= 0;
5223 pid
= lwpid_of (current_thread
);
5224 for (regset
= regsets_info
->regsets
; regset
->size
>= 0; regset
++)
5229 if (regset
->size
== 0 || regset_disabled (regsets_info
, regset
)
5230 || regset
->fill_function
== NULL
)
5233 buf
= xmalloc (regset
->size
);
5235 /* First fill the buffer with the current register set contents,
5236 in case there are any items in the kernel's regset that are
5237 not in gdbserver's regcache. */
5239 nt_type
= regset
->nt_type
;
5243 iov
.iov_len
= regset
->size
;
5244 data
= (void *) &iov
;
5250 res
= ptrace (regset
->get_request
, pid
,
5251 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5253 res
= ptrace (regset
->get_request
, pid
, data
, nt_type
);
5258 /* Then overlay our cached registers on that. */
5259 regset
->fill_function (regcache
, buf
);
5261 /* Only now do we write the register set. */
5263 res
= ptrace (regset
->set_request
, pid
,
5264 (PTRACE_TYPE_ARG3
) (long) nt_type
, data
);
5266 res
= ptrace (regset
->set_request
, pid
, data
, nt_type
);
5273 || (errno
== EINVAL
&& regset
->type
== OPTIONAL_REGS
))
5275 /* If we get EIO on a regset, or an EINVAL and the regset is
5276 optional, do not try it again for this process mode. */
5277 disable_regset (regsets_info
, regset
);
5279 else if (errno
== ESRCH
)
5281 /* At this point, ESRCH should mean the process is
5282 already gone, in which case we simply ignore attempts
5283 to change its registers. See also the related
5284 comment in resume_one_lwp. */
5290 perror ("Warning: ptrace(regsets_store_inferior_registers)");
5293 else if (regset
->type
== GENERAL_REGS
)
5294 saw_general_regs
= 1;
5297 if (saw_general_regs
)
5303 #else /* !HAVE_LINUX_REGSETS */
5305 #define use_linux_regsets 0
5306 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5307 #define regsets_store_inferior_registers(regsets_info, regcache) 1
5311 /* Return 1 if register REGNO is supported by one of the regset ptrace
5312 calls or 0 if it has to be transferred individually. */
5315 linux_register_in_regsets (const struct regs_info
*regs_info
, int regno
)
5317 unsigned char mask
= 1 << (regno
% 8);
5318 size_t index
= regno
/ 8;
5320 return (use_linux_regsets
5321 && (regs_info
->regset_bitmap
== NULL
5322 || (regs_info
->regset_bitmap
[index
] & mask
) != 0));
5325 #ifdef HAVE_LINUX_USRREGS
5328 register_addr (const struct usrregs_info
*usrregs
, int regnum
)
5332 if (regnum
< 0 || regnum
>= usrregs
->num_regs
)
5333 error ("Invalid register number %d.", regnum
);
5335 addr
= usrregs
->regmap
[regnum
];
5342 linux_process_target::fetch_register (const usrregs_info
*usrregs
,
5343 regcache
*regcache
, int regno
)
5350 if (regno
>= usrregs
->num_regs
)
5352 if (low_cannot_fetch_register (regno
))
5355 regaddr
= register_addr (usrregs
, regno
);
5359 size
= ((register_size (regcache
->tdesc
, regno
)
5360 + sizeof (PTRACE_XFER_TYPE
) - 1)
5361 & -sizeof (PTRACE_XFER_TYPE
));
5362 buf
= (char *) alloca (size
);
5364 pid
= lwpid_of (current_thread
);
5365 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5368 *(PTRACE_XFER_TYPE
*) (buf
+ i
) =
5369 ptrace (PTRACE_PEEKUSER
, pid
,
5370 /* Coerce to a uintptr_t first to avoid potential gcc warning
5371 of coercing an 8 byte integer to a 4 byte pointer. */
5372 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
, (PTRACE_TYPE_ARG4
) 0);
5373 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5376 /* Mark register REGNO unavailable. */
5377 supply_register (regcache
, regno
, NULL
);
5382 low_supply_ptrace_register (regcache
, regno
, buf
);
5386 linux_process_target::store_register (const usrregs_info
*usrregs
,
5387 regcache
*regcache
, int regno
)
5394 if (regno
>= usrregs
->num_regs
)
5396 if (low_cannot_store_register (regno
))
5399 regaddr
= register_addr (usrregs
, regno
);
5403 size
= ((register_size (regcache
->tdesc
, regno
)
5404 + sizeof (PTRACE_XFER_TYPE
) - 1)
5405 & -sizeof (PTRACE_XFER_TYPE
));
5406 buf
= (char *) alloca (size
);
5407 memset (buf
, 0, size
);
5409 low_collect_ptrace_register (regcache
, regno
, buf
);
5411 pid
= lwpid_of (current_thread
);
5412 for (i
= 0; i
< size
; i
+= sizeof (PTRACE_XFER_TYPE
))
5415 ptrace (PTRACE_POKEUSER
, pid
,
5416 /* Coerce to a uintptr_t first to avoid potential gcc warning
5417 about coercing an 8 byte integer to a 4 byte pointer. */
5418 (PTRACE_TYPE_ARG3
) (uintptr_t) regaddr
,
5419 (PTRACE_TYPE_ARG4
) *(PTRACE_XFER_TYPE
*) (buf
+ i
));
5422 /* At this point, ESRCH should mean the process is
5423 already gone, in which case we simply ignore attempts
5424 to change its registers. See also the related
5425 comment in resume_one_lwp. */
5430 if (!low_cannot_store_register (regno
))
5431 error ("writing register %d: %s", regno
, safe_strerror (errno
));
5433 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
5436 #endif /* HAVE_LINUX_USRREGS */
5439 linux_process_target::low_collect_ptrace_register (regcache
*regcache
,
5440 int regno
, char *buf
)
5442 collect_register (regcache
, regno
, buf
);
5446 linux_process_target::low_supply_ptrace_register (regcache
*regcache
,
5447 int regno
, const char *buf
)
5449 supply_register (regcache
, regno
, buf
);
5453 linux_process_target::usr_fetch_inferior_registers (const regs_info
*regs_info
,
5457 #ifdef HAVE_LINUX_USRREGS
5458 struct usrregs_info
*usr
= regs_info
->usrregs
;
5462 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5463 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5464 fetch_register (usr
, regcache
, regno
);
5467 fetch_register (usr
, regcache
, regno
);
5472 linux_process_target::usr_store_inferior_registers (const regs_info
*regs_info
,
5476 #ifdef HAVE_LINUX_USRREGS
5477 struct usrregs_info
*usr
= regs_info
->usrregs
;
5481 for (regno
= 0; regno
< usr
->num_regs
; regno
++)
5482 if (all
|| !linux_register_in_regsets (regs_info
, regno
))
5483 store_register (usr
, regcache
, regno
);
5486 store_register (usr
, regcache
, regno
);
5491 linux_process_target::fetch_registers (regcache
*regcache
, int regno
)
5495 const regs_info
*regs_info
= get_regs_info ();
5499 if (regs_info
->usrregs
!= NULL
)
5500 for (regno
= 0; regno
< regs_info
->usrregs
->num_regs
; regno
++)
5501 low_fetch_register (regcache
, regno
);
5503 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
, regcache
);
5504 if (regs_info
->usrregs
!= NULL
)
5505 usr_fetch_inferior_registers (regs_info
, regcache
, -1, all
);
5509 if (low_fetch_register (regcache
, regno
))
5512 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5514 all
= regsets_fetch_inferior_registers (regs_info
->regsets_info
,
5516 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5517 usr_fetch_inferior_registers (regs_info
, regcache
, regno
, 1);
5522 linux_process_target::store_registers (regcache
*regcache
, int regno
)
5526 const regs_info
*regs_info
= get_regs_info ();
5530 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5532 if (regs_info
->usrregs
!= NULL
)
5533 usr_store_inferior_registers (regs_info
, regcache
, regno
, all
);
5537 use_regsets
= linux_register_in_regsets (regs_info
, regno
);
5539 all
= regsets_store_inferior_registers (regs_info
->regsets_info
,
5541 if ((!use_regsets
|| all
) && regs_info
->usrregs
!= NULL
)
5542 usr_store_inferior_registers (regs_info
, regcache
, regno
, 1);
5547 linux_process_target::low_fetch_register (regcache
*regcache
, int regno
)
5552 /* A wrapper for the read_memory target op. */
5555 linux_read_memory (CORE_ADDR memaddr
, unsigned char *myaddr
, int len
)
5557 return the_target
->read_memory (memaddr
, myaddr
, len
);
5560 /* Copy LEN bytes from inferior's memory starting at MEMADDR
5561 to debugger memory starting at MYADDR. */
5564 linux_process_target::read_memory (CORE_ADDR memaddr
,
5565 unsigned char *myaddr
, int len
)
5567 int pid
= lwpid_of (current_thread
);
5568 PTRACE_XFER_TYPE
*buffer
;
5576 /* Try using /proc. Don't bother for one word. */
5577 if (len
>= 3 * sizeof (long))
5581 /* We could keep this file open and cache it - possibly one per
5582 thread. That requires some juggling, but is even faster. */
5583 sprintf (filename
, "/proc/%d/mem", pid
);
5584 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
5588 /* If pread64 is available, use it. It's faster if the kernel
5589 supports it (only one syscall), and it's 64-bit safe even on
5590 32-bit platforms (for instance, SPARC debugging a SPARC64
5593 bytes
= pread64 (fd
, myaddr
, len
, memaddr
);
5596 if (lseek (fd
, memaddr
, SEEK_SET
) != -1)
5597 bytes
= read (fd
, myaddr
, len
);
5604 /* Some data was read, we'll try to get the rest with ptrace. */
5614 /* Round starting address down to longword boundary. */
5615 addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5616 /* Round ending address up; get number of longwords that makes. */
5617 count
= ((((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5618 / sizeof (PTRACE_XFER_TYPE
));
5619 /* Allocate buffer of that many longwords. */
5620 buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5622 /* Read all the longwords */
5624 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5626 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5627 about coercing an 8 byte integer to a 4 byte pointer. */
5628 buffer
[i
] = ptrace (PTRACE_PEEKTEXT
, pid
,
5629 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5630 (PTRACE_TYPE_ARG4
) 0);
5636 /* Copy appropriate bytes out of the buffer. */
5639 i
*= sizeof (PTRACE_XFER_TYPE
);
5640 i
-= memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1);
5642 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5649 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5650 memory at MEMADDR. On failure (cannot write to the inferior)
5651 returns the value of errno. Always succeeds if LEN is zero. */
5654 linux_process_target::write_memory (CORE_ADDR memaddr
,
5655 const unsigned char *myaddr
, int len
)
5658 /* Round starting address down to longword boundary. */
5659 CORE_ADDR addr
= memaddr
& -(CORE_ADDR
) sizeof (PTRACE_XFER_TYPE
);
5660 /* Round ending address up; get number of longwords that makes. */
5662 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
5663 / sizeof (PTRACE_XFER_TYPE
);
5665 /* Allocate buffer of that many longwords. */
5666 PTRACE_XFER_TYPE
*buffer
= XALLOCAVEC (PTRACE_XFER_TYPE
, count
);
5668 int pid
= lwpid_of (current_thread
);
5672 /* Zero length write always succeeds. */
5678 /* Dump up to four bytes. */
5679 char str
[4 * 2 + 1];
5681 int dump
= len
< 4 ? len
: 4;
5683 for (i
= 0; i
< dump
; i
++)
5685 sprintf (p
, "%02x", myaddr
[i
]);
5690 debug_printf ("Writing %s to 0x%08lx in process %d\n",
5691 str
, (long) memaddr
, pid
);
5694 /* Fill start and end extra bytes of buffer with existing memory data. */
5697 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5698 about coercing an 8 byte integer to a 4 byte pointer. */
5699 buffer
[0] = ptrace (PTRACE_PEEKTEXT
, pid
,
5700 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5701 (PTRACE_TYPE_ARG4
) 0);
5709 = ptrace (PTRACE_PEEKTEXT
, pid
,
5710 /* Coerce to a uintptr_t first to avoid potential gcc warning
5711 about coercing an 8 byte integer to a 4 byte pointer. */
5712 (PTRACE_TYPE_ARG3
) (uintptr_t) (addr
+ (count
- 1)
5713 * sizeof (PTRACE_XFER_TYPE
)),
5714 (PTRACE_TYPE_ARG4
) 0);
5719 /* Copy data to be written over corresponding part of buffer. */
5721 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
5724 /* Write the entire buffer. */
5726 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
5729 ptrace (PTRACE_POKETEXT
, pid
,
5730 /* Coerce to a uintptr_t first to avoid potential gcc warning
5731 about coercing an 8 byte integer to a 4 byte pointer. */
5732 (PTRACE_TYPE_ARG3
) (uintptr_t) addr
,
5733 (PTRACE_TYPE_ARG4
) buffer
[i
]);
5742 linux_process_target::look_up_symbols ()
5744 #ifdef USE_THREAD_DB
5745 struct process_info
*proc
= current_process ();
5747 if (proc
->priv
->thread_db
!= NULL
)
5755 linux_process_target::request_interrupt ()
5757 /* Send a SIGINT to the process group. This acts just like the user
5758 typed a ^C on the controlling terminal. */
5759 ::kill (-signal_pid
, SIGINT
);
5763 linux_process_target::supports_read_auxv ()
5768 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5769 to debugger memory starting at MYADDR. */
5772 linux_process_target::read_auxv (CORE_ADDR offset
, unsigned char *myaddr
,
5775 char filename
[PATH_MAX
];
5777 int pid
= lwpid_of (current_thread
);
5779 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
5781 fd
= open (filename
, O_RDONLY
);
5785 if (offset
!= (CORE_ADDR
) 0
5786 && lseek (fd
, (off_t
) offset
, SEEK_SET
) != (off_t
) offset
)
5789 n
= read (fd
, myaddr
, len
);
5797 linux_process_target::insert_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5798 int size
, raw_breakpoint
*bp
)
5800 if (type
== raw_bkpt_type_sw
)
5801 return insert_memory_breakpoint (bp
);
5803 return low_insert_point (type
, addr
, size
, bp
);
5807 linux_process_target::low_insert_point (raw_bkpt_type type
, CORE_ADDR addr
,
5808 int size
, raw_breakpoint
*bp
)
5810 /* Unsupported (see target.h). */
5815 linux_process_target::remove_point (enum raw_bkpt_type type
, CORE_ADDR addr
,
5816 int size
, raw_breakpoint
*bp
)
5818 if (type
== raw_bkpt_type_sw
)
5819 return remove_memory_breakpoint (bp
);
5821 return low_remove_point (type
, addr
, size
, bp
);
5825 linux_process_target::low_remove_point (raw_bkpt_type type
, CORE_ADDR addr
,
5826 int size
, raw_breakpoint
*bp
)
5828 /* Unsupported (see target.h). */
5832 /* Implement the stopped_by_sw_breakpoint target_ops
5836 linux_process_target::stopped_by_sw_breakpoint ()
5838 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5840 return (lwp
->stop_reason
== TARGET_STOPPED_BY_SW_BREAKPOINT
);
5843 /* Implement the supports_stopped_by_sw_breakpoint target_ops
5847 linux_process_target::supports_stopped_by_sw_breakpoint ()
5849 return USE_SIGTRAP_SIGINFO
;
5852 /* Implement the stopped_by_hw_breakpoint target_ops
5856 linux_process_target::stopped_by_hw_breakpoint ()
5858 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5860 return (lwp
->stop_reason
== TARGET_STOPPED_BY_HW_BREAKPOINT
);
5863 /* Implement the supports_stopped_by_hw_breakpoint target_ops
5867 linux_process_target::supports_stopped_by_hw_breakpoint ()
5869 return USE_SIGTRAP_SIGINFO
;
5872 /* Implement the supports_hardware_single_step target_ops method. */
5875 linux_process_target::supports_hardware_single_step ()
5881 linux_process_target::stopped_by_watchpoint ()
5883 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5885 return lwp
->stop_reason
== TARGET_STOPPED_BY_WATCHPOINT
;
5889 linux_process_target::stopped_data_address ()
5891 struct lwp_info
*lwp
= get_thread_lwp (current_thread
);
5893 return lwp
->stopped_data_address
;
5896 /* This is only used for targets that define PT_TEXT_ADDR,
5897 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5898 the target has different ways of acquiring this information, like
5902 linux_process_target::supports_read_offsets ()
5904 #ifdef SUPPORTS_READ_OFFSETS
5911 /* Under uClinux, programs are loaded at non-zero offsets, which we need
5912 to tell gdb about. */
5915 linux_process_target::read_offsets (CORE_ADDR
*text_p
, CORE_ADDR
*data_p
)
5917 #ifdef SUPPORTS_READ_OFFSETS
5918 unsigned long text
, text_end
, data
;
5919 int pid
= lwpid_of (current_thread
);
5923 text
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_ADDR
,
5924 (PTRACE_TYPE_ARG4
) 0);
5925 text_end
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_TEXT_END_ADDR
,
5926 (PTRACE_TYPE_ARG4
) 0);
5927 data
= ptrace (PTRACE_PEEKUSER
, pid
, (PTRACE_TYPE_ARG3
) PT_DATA_ADDR
,
5928 (PTRACE_TYPE_ARG4
) 0);
5932 /* Both text and data offsets produced at compile-time (and so
5933 used by gdb) are relative to the beginning of the program,
5934 with the data segment immediately following the text segment.
5935 However, the actual runtime layout in memory may put the data
5936 somewhere else, so when we send gdb a data base-address, we
5937 use the real data base address and subtract the compile-time
5938 data base-address from it (which is just the length of the
5939 text segment). BSS immediately follows data in both
5942 *data_p
= data
- (text_end
- text
);
5948 gdb_assert_not_reached ("target op read_offsets not supported");
5953 linux_process_target::supports_get_tls_address ()
5955 #ifdef USE_THREAD_DB
5963 linux_process_target::get_tls_address (thread_info
*thread
,
5965 CORE_ADDR load_module
,
5968 #ifdef USE_THREAD_DB
5969 return thread_db_get_tls_address (thread
, offset
, load_module
, address
);
5976 linux_process_target::supports_qxfer_osdata ()
5982 linux_process_target::qxfer_osdata (const char *annex
,
5983 unsigned char *readbuf
,
5984 unsigned const char *writebuf
,
5985 CORE_ADDR offset
, int len
)
5987 return linux_common_xfer_osdata (annex
, readbuf
, offset
, len
);
5991 linux_process_target::siginfo_fixup (siginfo_t
*siginfo
,
5992 gdb_byte
*inf_siginfo
, int direction
)
5994 bool done
= low_siginfo_fixup (siginfo
, inf_siginfo
, direction
);
5996 /* If there was no callback, or the callback didn't do anything,
5997 then just do a straight memcpy. */
6001 memcpy (siginfo
, inf_siginfo
, sizeof (siginfo_t
));
6003 memcpy (inf_siginfo
, siginfo
, sizeof (siginfo_t
));
6008 linux_process_target::low_siginfo_fixup (siginfo_t
*native
, gdb_byte
*inf
,
6015 linux_process_target::supports_qxfer_siginfo ()
6021 linux_process_target::qxfer_siginfo (const char *annex
,
6022 unsigned char *readbuf
,
6023 unsigned const char *writebuf
,
6024 CORE_ADDR offset
, int len
)
6028 gdb_byte inf_siginfo
[sizeof (siginfo_t
)];
6030 if (current_thread
== NULL
)
6033 pid
= lwpid_of (current_thread
);
6036 debug_printf ("%s siginfo for lwp %d.\n",
6037 readbuf
!= NULL
? "Reading" : "Writing",
6040 if (offset
>= sizeof (siginfo
))
6043 if (ptrace (PTRACE_GETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6046 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
6047 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
6048 inferior with a 64-bit GDBSERVER should look the same as debugging it
6049 with a 32-bit GDBSERVER, we need to convert it. */
6050 siginfo_fixup (&siginfo
, inf_siginfo
, 0);
6052 if (offset
+ len
> sizeof (siginfo
))
6053 len
= sizeof (siginfo
) - offset
;
6055 if (readbuf
!= NULL
)
6056 memcpy (readbuf
, inf_siginfo
+ offset
, len
);
6059 memcpy (inf_siginfo
+ offset
, writebuf
, len
);
6061 /* Convert back to ptrace layout before flushing it out. */
6062 siginfo_fixup (&siginfo
, inf_siginfo
, 1);
6064 if (ptrace (PTRACE_SETSIGINFO
, pid
, (PTRACE_TYPE_ARG3
) 0, &siginfo
) != 0)
6071 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
6072 so we notice when children change state; as the handler for the
6073 sigsuspend in my_waitpid. */
6076 sigchld_handler (int signo
)
6078 int old_errno
= errno
;
6084 /* Use the async signal safe debug function. */
6085 if (debug_write ("sigchld_handler\n",
6086 sizeof ("sigchld_handler\n") - 1) < 0)
6087 break; /* just ignore */
6091 if (target_is_async_p ())
6092 async_file_mark (); /* trigger a linux_wait */
6098 linux_process_target::supports_non_stop ()
6104 linux_process_target::async (bool enable
)
6106 bool previous
= target_is_async_p ();
6109 debug_printf ("linux_async (%d), previous=%d\n",
6112 if (previous
!= enable
)
6115 sigemptyset (&mask
);
6116 sigaddset (&mask
, SIGCHLD
);
6118 gdb_sigmask (SIG_BLOCK
, &mask
, NULL
);
6122 if (pipe (linux_event_pipe
) == -1)
6124 linux_event_pipe
[0] = -1;
6125 linux_event_pipe
[1] = -1;
6126 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6128 warning ("creating event pipe failed.");
6132 fcntl (linux_event_pipe
[0], F_SETFL
, O_NONBLOCK
);
6133 fcntl (linux_event_pipe
[1], F_SETFL
, O_NONBLOCK
);
6135 /* Register the event loop handler. */
6136 add_file_handler (linux_event_pipe
[0],
6137 handle_target_event
, NULL
);
6139 /* Always trigger a linux_wait. */
6144 delete_file_handler (linux_event_pipe
[0]);
6146 close (linux_event_pipe
[0]);
6147 close (linux_event_pipe
[1]);
6148 linux_event_pipe
[0] = -1;
6149 linux_event_pipe
[1] = -1;
6152 gdb_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
6159 linux_process_target::start_non_stop (bool nonstop
)
6161 /* Register or unregister from event-loop accordingly. */
6162 target_async (nonstop
);
6164 if (target_is_async_p () != (nonstop
!= false))
6171 linux_process_target::supports_multi_process ()
6176 /* Check if fork events are supported. */
6179 linux_process_target::supports_fork_events ()
6181 return linux_supports_tracefork ();
6184 /* Check if vfork events are supported. */
6187 linux_process_target::supports_vfork_events ()
6189 return linux_supports_tracefork ();
6192 /* Check if exec events are supported. */
6195 linux_process_target::supports_exec_events ()
6197 return linux_supports_traceexec ();
6200 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
6201 ptrace flags for all inferiors. This is in case the new GDB connection
6202 doesn't support the same set of events that the previous one did. */
6205 linux_process_target::handle_new_gdb_connection ()
6207 /* Request that all the lwps reset their ptrace options. */
6208 for_each_thread ([] (thread_info
*thread
)
6210 struct lwp_info
*lwp
= get_thread_lwp (thread
);
6214 /* Stop the lwp so we can modify its ptrace options. */
6215 lwp
->must_set_ptrace_flags
= 1;
6216 linux_stop_lwp (lwp
);
6220 /* Already stopped; go ahead and set the ptrace options. */
6221 struct process_info
*proc
= find_process_pid (pid_of (thread
));
6222 int options
= linux_low_ptrace_options (proc
->attached
);
6224 linux_enable_event_reporting (lwpid_of (thread
), options
);
6225 lwp
->must_set_ptrace_flags
= 0;
6231 linux_process_target::handle_monitor_command (char *mon
)
6233 #ifdef USE_THREAD_DB
6234 return thread_db_handle_monitor_command (mon
);
6241 linux_process_target::core_of_thread (ptid_t ptid
)
6243 return linux_common_core_of_thread (ptid
);
6247 linux_process_target::supports_disable_randomization ()
6249 #ifdef HAVE_PERSONALITY
6257 linux_process_target::supports_agent ()
6263 linux_process_target::supports_range_stepping ()
6265 if (supports_software_single_step ())
6268 return low_supports_range_stepping ();
6272 linux_process_target::low_supports_range_stepping ()
6278 linux_process_target::supports_pid_to_exec_file ()
6284 linux_process_target::pid_to_exec_file (int pid
)
6286 return linux_proc_pid_to_exec_file (pid
);
6290 linux_process_target::supports_multifs ()
6296 linux_process_target::multifs_open (int pid
, const char *filename
,
6297 int flags
, mode_t mode
)
6299 return linux_mntns_open_cloexec (pid
, filename
, flags
, mode
);
6303 linux_process_target::multifs_unlink (int pid
, const char *filename
)
6305 return linux_mntns_unlink (pid
, filename
);
6309 linux_process_target::multifs_readlink (int pid
, const char *filename
,
6310 char *buf
, size_t bufsiz
)
6312 return linux_mntns_readlink (pid
, filename
, buf
, bufsiz
);
6315 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
6316 struct target_loadseg
6318 /* Core address to which the segment is mapped. */
6320 /* VMA recorded in the program header. */
6322 /* Size of this segment in memory. */
6326 # if defined PT_GETDSBT
6327 struct target_loadmap
6329 /* Protocol version number, must be zero. */
6331 /* Pointer to the DSBT table, its size, and the DSBT index. */
6332 unsigned *dsbt_table
;
6333 unsigned dsbt_size
, dsbt_index
;
6334 /* Number of segments in this map. */
6336 /* The actual memory map. */
6337 struct target_loadseg segs
[/*nsegs*/];
6339 # define LINUX_LOADMAP PT_GETDSBT
6340 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6341 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6343 struct target_loadmap
6345 /* Protocol version number, must be zero. */
6347 /* Number of segments in this map. */
6349 /* The actual memory map. */
6350 struct target_loadseg segs
[/*nsegs*/];
6352 # define LINUX_LOADMAP PTRACE_GETFDPIC
6353 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6354 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6358 linux_process_target::supports_read_loadmap ()
6364 linux_process_target::read_loadmap (const char *annex
, CORE_ADDR offset
,
6365 unsigned char *myaddr
, unsigned int len
)
6367 int pid
= lwpid_of (current_thread
);
6369 struct target_loadmap
*data
= NULL
;
6370 unsigned int actual_length
, copy_length
;
6372 if (strcmp (annex
, "exec") == 0)
6373 addr
= (int) LINUX_LOADMAP_EXEC
;
6374 else if (strcmp (annex
, "interp") == 0)
6375 addr
= (int) LINUX_LOADMAP_INTERP
;
6379 if (ptrace (LINUX_LOADMAP
, pid
, addr
, &data
) != 0)
6385 actual_length
= sizeof (struct target_loadmap
)
6386 + sizeof (struct target_loadseg
) * data
->nsegs
;
6388 if (offset
< 0 || offset
> actual_length
)
6391 copy_length
= actual_length
- offset
< len
? actual_length
- offset
: len
;
6392 memcpy (myaddr
, (char *) data
+ offset
, copy_length
);
6395 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6398 linux_process_target::supports_catch_syscall ()
6400 return (low_supports_catch_syscall ()
6401 && linux_supports_tracesysgood ());
6405 linux_process_target::low_supports_catch_syscall ()
6411 linux_process_target::read_pc (regcache
*regcache
)
6413 if (!low_supports_breakpoints ())
6416 return low_get_pc (regcache
);
6420 linux_process_target::write_pc (regcache
*regcache
, CORE_ADDR pc
)
6422 gdb_assert (low_supports_breakpoints ());
6424 low_set_pc (regcache
, pc
);
6428 linux_process_target::supports_thread_stopped ()
6434 linux_process_target::thread_stopped (thread_info
*thread
)
6436 return get_thread_lwp (thread
)->stopped
;
6439 /* This exposes stop-all-threads functionality to other modules. */
6442 linux_process_target::pause_all (bool freeze
)
6444 stop_all_lwps (freeze
, NULL
);
6447 /* This exposes unstop-all-threads functionality to other gdbserver
6451 linux_process_target::unpause_all (bool unfreeze
)
6453 unstop_all_lwps (unfreeze
, NULL
);
6457 linux_process_target::prepare_to_access_memory ()
6459 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6462 target_pause_all (true);
6467 linux_process_target::done_accessing_memory ()
6469 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6472 target_unpause_all (true);
6475 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6478 get_phdr_phnum_from_proc_auxv (const int pid
, const int is_elf64
,
6479 CORE_ADDR
*phdr_memaddr
, int *num_phdr
)
6481 char filename
[PATH_MAX
];
6483 const int auxv_size
= is_elf64
6484 ? sizeof (Elf64_auxv_t
) : sizeof (Elf32_auxv_t
);
6485 char buf
[sizeof (Elf64_auxv_t
)]; /* The larger of the two. */
6487 xsnprintf (filename
, sizeof filename
, "/proc/%d/auxv", pid
);
6489 fd
= open (filename
, O_RDONLY
);
6495 while (read (fd
, buf
, auxv_size
) == auxv_size
6496 && (*phdr_memaddr
== 0 || *num_phdr
== 0))
6500 Elf64_auxv_t
*const aux
= (Elf64_auxv_t
*) buf
;
6502 switch (aux
->a_type
)
6505 *phdr_memaddr
= aux
->a_un
.a_val
;
6508 *num_phdr
= aux
->a_un
.a_val
;
6514 Elf32_auxv_t
*const aux
= (Elf32_auxv_t
*) buf
;
6516 switch (aux
->a_type
)
6519 *phdr_memaddr
= aux
->a_un
.a_val
;
6522 *num_phdr
= aux
->a_un
.a_val
;
6530 if (*phdr_memaddr
== 0 || *num_phdr
== 0)
6532 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6533 "phdr_memaddr = %ld, phdr_num = %d",
6534 (long) *phdr_memaddr
, *num_phdr
);
6541 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6544 get_dynamic (const int pid
, const int is_elf64
)
6546 CORE_ADDR phdr_memaddr
, relocation
;
6548 unsigned char *phdr_buf
;
6549 const int phdr_size
= is_elf64
? sizeof (Elf64_Phdr
) : sizeof (Elf32_Phdr
);
6551 if (get_phdr_phnum_from_proc_auxv (pid
, is_elf64
, &phdr_memaddr
, &num_phdr
))
6554 gdb_assert (num_phdr
< 100); /* Basic sanity check. */
6555 phdr_buf
= (unsigned char *) alloca (num_phdr
* phdr_size
);
6557 if (linux_read_memory (phdr_memaddr
, phdr_buf
, num_phdr
* phdr_size
))
6560 /* Compute relocation: it is expected to be 0 for "regular" executables,
6561 non-zero for PIE ones. */
6563 for (i
= 0; relocation
== -1 && i
< num_phdr
; i
++)
6566 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6568 if (p
->p_type
== PT_PHDR
)
6569 relocation
= phdr_memaddr
- p
->p_vaddr
;
6573 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6575 if (p
->p_type
== PT_PHDR
)
6576 relocation
= phdr_memaddr
- p
->p_vaddr
;
6579 if (relocation
== -1)
6581 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6582 any real world executables, including PIE executables, have always
6583 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6584 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6585 or present DT_DEBUG anyway (fpc binaries are statically linked).
6587 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6589 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6594 for (i
= 0; i
< num_phdr
; i
++)
6598 Elf64_Phdr
*const p
= (Elf64_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6600 if (p
->p_type
== PT_DYNAMIC
)
6601 return p
->p_vaddr
+ relocation
;
6605 Elf32_Phdr
*const p
= (Elf32_Phdr
*) (phdr_buf
+ i
* phdr_size
);
6607 if (p
->p_type
== PT_DYNAMIC
)
6608 return p
->p_vaddr
+ relocation
;
6615 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6616 can be 0 if the inferior does not yet have the library list initialized.
6617 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6618 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6621 get_r_debug (const int pid
, const int is_elf64
)
6623 CORE_ADDR dynamic_memaddr
;
6624 const int dyn_size
= is_elf64
? sizeof (Elf64_Dyn
) : sizeof (Elf32_Dyn
);
6625 unsigned char buf
[sizeof (Elf64_Dyn
)]; /* The larger of the two. */
6628 dynamic_memaddr
= get_dynamic (pid
, is_elf64
);
6629 if (dynamic_memaddr
== 0)
6632 while (linux_read_memory (dynamic_memaddr
, buf
, dyn_size
) == 0)
6636 Elf64_Dyn
*const dyn
= (Elf64_Dyn
*) buf
;
6637 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6641 unsigned char buf
[sizeof (Elf64_Xword
)];
6645 #ifdef DT_MIPS_RLD_MAP
6646 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6648 if (linux_read_memory (dyn
->d_un
.d_val
,
6649 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6654 #endif /* DT_MIPS_RLD_MAP */
6655 #ifdef DT_MIPS_RLD_MAP_REL
6656 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6658 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6659 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6664 #endif /* DT_MIPS_RLD_MAP_REL */
6666 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6667 map
= dyn
->d_un
.d_val
;
6669 if (dyn
->d_tag
== DT_NULL
)
6674 Elf32_Dyn
*const dyn
= (Elf32_Dyn
*) buf
;
6675 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6679 unsigned char buf
[sizeof (Elf32_Word
)];
6683 #ifdef DT_MIPS_RLD_MAP
6684 if (dyn
->d_tag
== DT_MIPS_RLD_MAP
)
6686 if (linux_read_memory (dyn
->d_un
.d_val
,
6687 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6692 #endif /* DT_MIPS_RLD_MAP */
6693 #ifdef DT_MIPS_RLD_MAP_REL
6694 if (dyn
->d_tag
== DT_MIPS_RLD_MAP_REL
)
6696 if (linux_read_memory (dyn
->d_un
.d_val
+ dynamic_memaddr
,
6697 rld_map
.buf
, sizeof (rld_map
.buf
)) == 0)
6702 #endif /* DT_MIPS_RLD_MAP_REL */
6704 if (dyn
->d_tag
== DT_DEBUG
&& map
== -1)
6705 map
= dyn
->d_un
.d_val
;
6707 if (dyn
->d_tag
== DT_NULL
)
6711 dynamic_memaddr
+= dyn_size
;
6717 /* Read one pointer from MEMADDR in the inferior. */
6720 read_one_ptr (CORE_ADDR memaddr
, CORE_ADDR
*ptr
, int ptr_size
)
6724 /* Go through a union so this works on either big or little endian
6725 hosts, when the inferior's pointer size is smaller than the size
6726 of CORE_ADDR. It is assumed the inferior's endianness is the
6727 same of the superior's. */
6730 CORE_ADDR core_addr
;
6735 ret
= linux_read_memory (memaddr
, &addr
.uc
, ptr_size
);
6738 if (ptr_size
== sizeof (CORE_ADDR
))
6739 *ptr
= addr
.core_addr
;
6740 else if (ptr_size
== sizeof (unsigned int))
6743 gdb_assert_not_reached ("unhandled pointer size");
6749 linux_process_target::supports_qxfer_libraries_svr4 ()
6754 struct link_map_offsets
6756 /* Offset and size of r_debug.r_version. */
6757 int r_version_offset
;
6759 /* Offset and size of r_debug.r_map. */
6762 /* Offset to l_addr field in struct link_map. */
6765 /* Offset to l_name field in struct link_map. */
6768 /* Offset to l_ld field in struct link_map. */
6771 /* Offset to l_next field in struct link_map. */
6774 /* Offset to l_prev field in struct link_map. */
6778 /* Construct qXfer:libraries-svr4:read reply. */
6781 linux_process_target::qxfer_libraries_svr4 (const char *annex
,
6782 unsigned char *readbuf
,
6783 unsigned const char *writebuf
,
6784 CORE_ADDR offset
, int len
)
6786 struct process_info_private
*const priv
= current_process ()->priv
;
6787 char filename
[PATH_MAX
];
6790 static const struct link_map_offsets lmo_32bit_offsets
=
6792 0, /* r_version offset. */
6793 4, /* r_debug.r_map offset. */
6794 0, /* l_addr offset in link_map. */
6795 4, /* l_name offset in link_map. */
6796 8, /* l_ld offset in link_map. */
6797 12, /* l_next offset in link_map. */
6798 16 /* l_prev offset in link_map. */
6801 static const struct link_map_offsets lmo_64bit_offsets
=
6803 0, /* r_version offset. */
6804 8, /* r_debug.r_map offset. */
6805 0, /* l_addr offset in link_map. */
6806 8, /* l_name offset in link_map. */
6807 16, /* l_ld offset in link_map. */
6808 24, /* l_next offset in link_map. */
6809 32 /* l_prev offset in link_map. */
6811 const struct link_map_offsets
*lmo
;
6812 unsigned int machine
;
6814 CORE_ADDR lm_addr
= 0, lm_prev
= 0;
6815 CORE_ADDR l_name
, l_addr
, l_ld
, l_next
, l_prev
;
6816 int header_done
= 0;
6818 if (writebuf
!= NULL
)
6820 if (readbuf
== NULL
)
6823 pid
= lwpid_of (current_thread
);
6824 xsnprintf (filename
, sizeof filename
, "/proc/%d/exe", pid
);
6825 is_elf64
= elf_64_file_p (filename
, &machine
);
6826 lmo
= is_elf64
? &lmo_64bit_offsets
: &lmo_32bit_offsets
;
6827 ptr_size
= is_elf64
? 8 : 4;
6829 while (annex
[0] != '\0')
6835 sep
= strchr (annex
, '=');
6839 name_len
= sep
- annex
;
6840 if (name_len
== 5 && startswith (annex
, "start"))
6842 else if (name_len
== 4 && startswith (annex
, "prev"))
6846 annex
= strchr (sep
, ';');
6853 annex
= decode_address_to_semicolon (addrp
, sep
+ 1);
6860 if (priv
->r_debug
== 0)
6861 priv
->r_debug
= get_r_debug (pid
, is_elf64
);
6863 /* We failed to find DT_DEBUG. Such situation will not change
6864 for this inferior - do not retry it. Report it to GDB as
6865 E01, see for the reasons at the GDB solib-svr4.c side. */
6866 if (priv
->r_debug
== (CORE_ADDR
) -1)
6869 if (priv
->r_debug
!= 0)
6871 if (linux_read_memory (priv
->r_debug
+ lmo
->r_version_offset
,
6872 (unsigned char *) &r_version
,
6873 sizeof (r_version
)) != 0
6876 warning ("unexpected r_debug version %d", r_version
);
6878 else if (read_one_ptr (priv
->r_debug
+ lmo
->r_map_offset
,
6879 &lm_addr
, ptr_size
) != 0)
6881 warning ("unable to read r_map from 0x%lx",
6882 (long) priv
->r_debug
+ lmo
->r_map_offset
);
6887 std::string document
= "<library-list-svr4 version=\"1.0\"";
6890 && read_one_ptr (lm_addr
+ lmo
->l_name_offset
,
6891 &l_name
, ptr_size
) == 0
6892 && read_one_ptr (lm_addr
+ lmo
->l_addr_offset
,
6893 &l_addr
, ptr_size
) == 0
6894 && read_one_ptr (lm_addr
+ lmo
->l_ld_offset
,
6895 &l_ld
, ptr_size
) == 0
6896 && read_one_ptr (lm_addr
+ lmo
->l_prev_offset
,
6897 &l_prev
, ptr_size
) == 0
6898 && read_one_ptr (lm_addr
+ lmo
->l_next_offset
,
6899 &l_next
, ptr_size
) == 0)
6901 unsigned char libname
[PATH_MAX
];
6903 if (lm_prev
!= l_prev
)
6905 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
6906 (long) lm_prev
, (long) l_prev
);
6910 /* Ignore the first entry even if it has valid name as the first entry
6911 corresponds to the main executable. The first entry should not be
6912 skipped if the dynamic loader was loaded late by a static executable
6913 (see solib-svr4.c parameter ignore_first). But in such case the main
6914 executable does not have PT_DYNAMIC present and this function already
6915 exited above due to failed get_r_debug. */
6917 string_appendf (document
, " main-lm=\"0x%lx\"", (unsigned long) lm_addr
);
6920 /* Not checking for error because reading may stop before
6921 we've got PATH_MAX worth of characters. */
6923 linux_read_memory (l_name
, libname
, sizeof (libname
) - 1);
6924 libname
[sizeof (libname
) - 1] = '\0';
6925 if (libname
[0] != '\0')
6929 /* Terminate `<library-list-svr4'. */
6934 string_appendf (document
, "<library name=\"");
6935 xml_escape_text_append (&document
, (char *) libname
);
6936 string_appendf (document
, "\" lm=\"0x%lx\" "
6937 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
6938 (unsigned long) lm_addr
, (unsigned long) l_addr
,
6939 (unsigned long) l_ld
);
6949 /* Empty list; terminate `<library-list-svr4'. */
6953 document
+= "</library-list-svr4>";
6955 int document_len
= document
.length ();
6956 if (offset
< document_len
)
6957 document_len
-= offset
;
6960 if (len
> document_len
)
6963 memcpy (readbuf
, document
.data () + offset
, len
);
6968 #ifdef HAVE_LINUX_BTRACE
6970 btrace_target_info
*
6971 linux_process_target::enable_btrace (ptid_t ptid
,
6972 const btrace_config
*conf
)
6974 return linux_enable_btrace (ptid
, conf
);
6977 /* See to_disable_btrace target method. */
6980 linux_process_target::disable_btrace (btrace_target_info
*tinfo
)
6982 enum btrace_error err
;
6984 err
= linux_disable_btrace (tinfo
);
6985 return (err
== BTRACE_ERR_NONE
? 0 : -1);
6988 /* Encode an Intel Processor Trace configuration. */
6991 linux_low_encode_pt_config (struct buffer
*buffer
,
6992 const struct btrace_data_pt_config
*config
)
6994 buffer_grow_str (buffer
, "<pt-config>\n");
6996 switch (config
->cpu
.vendor
)
6999 buffer_xml_printf (buffer
, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
7000 "model=\"%u\" stepping=\"%u\"/>\n",
7001 config
->cpu
.family
, config
->cpu
.model
,
7002 config
->cpu
.stepping
);
7009 buffer_grow_str (buffer
, "</pt-config>\n");
7012 /* Encode a raw buffer. */
7015 linux_low_encode_raw (struct buffer
*buffer
, const gdb_byte
*data
,
7021 /* We use hex encoding - see gdbsupport/rsp-low.h. */
7022 buffer_grow_str (buffer
, "<raw>\n");
7028 elem
[0] = tohex ((*data
>> 4) & 0xf);
7029 elem
[1] = tohex (*data
++ & 0xf);
7031 buffer_grow (buffer
, elem
, 2);
7034 buffer_grow_str (buffer
, "</raw>\n");
7037 /* See to_read_btrace target method. */
7040 linux_process_target::read_btrace (btrace_target_info
*tinfo
,
7042 enum btrace_read_type type
)
7044 struct btrace_data btrace
;
7045 enum btrace_error err
;
7047 err
= linux_read_btrace (&btrace
, tinfo
, type
);
7048 if (err
!= BTRACE_ERR_NONE
)
7050 if (err
== BTRACE_ERR_OVERFLOW
)
7051 buffer_grow_str0 (buffer
, "E.Overflow.");
7053 buffer_grow_str0 (buffer
, "E.Generic Error.");
7058 switch (btrace
.format
)
7060 case BTRACE_FORMAT_NONE
:
7061 buffer_grow_str0 (buffer
, "E.No Trace.");
7064 case BTRACE_FORMAT_BTS
:
7065 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7066 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7068 for (const btrace_block
&block
: *btrace
.variant
.bts
.blocks
)
7069 buffer_xml_printf (buffer
, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
7070 paddress (block
.begin
), paddress (block
.end
));
7072 buffer_grow_str0 (buffer
, "</btrace>\n");
7075 case BTRACE_FORMAT_PT
:
7076 buffer_grow_str (buffer
, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
7077 buffer_grow_str (buffer
, "<btrace version=\"1.0\">\n");
7078 buffer_grow_str (buffer
, "<pt>\n");
7080 linux_low_encode_pt_config (buffer
, &btrace
.variant
.pt
.config
);
7082 linux_low_encode_raw (buffer
, btrace
.variant
.pt
.data
,
7083 btrace
.variant
.pt
.size
);
7085 buffer_grow_str (buffer
, "</pt>\n");
7086 buffer_grow_str0 (buffer
, "</btrace>\n");
7090 buffer_grow_str0 (buffer
, "E.Unsupported Trace Format.");
7097 /* See to_btrace_conf target method. */
7100 linux_process_target::read_btrace_conf (const btrace_target_info
*tinfo
,
7103 const struct btrace_config
*conf
;
7105 buffer_grow_str (buffer
, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
7106 buffer_grow_str (buffer
, "<btrace-conf version=\"1.0\">\n");
7108 conf
= linux_btrace_conf (tinfo
);
7111 switch (conf
->format
)
7113 case BTRACE_FORMAT_NONE
:
7116 case BTRACE_FORMAT_BTS
:
7117 buffer_xml_printf (buffer
, "<bts");
7118 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->bts
.size
);
7119 buffer_xml_printf (buffer
, " />\n");
7122 case BTRACE_FORMAT_PT
:
7123 buffer_xml_printf (buffer
, "<pt");
7124 buffer_xml_printf (buffer
, " size=\"0x%x\"", conf
->pt
.size
);
7125 buffer_xml_printf (buffer
, "/>\n");
7130 buffer_grow_str0 (buffer
, "</btrace-conf>\n");
7133 #endif /* HAVE_LINUX_BTRACE */
7135 /* See nat/linux-nat.h. */
7138 current_lwp_ptid (void)
7140 return ptid_of (current_thread
);
7144 linux_process_target::thread_name (ptid_t thread
)
7146 return linux_proc_tid_get_name (thread
);
7151 linux_process_target::thread_handle (ptid_t ptid
, gdb_byte
**handle
,
7154 return thread_db_thread_handle (ptid
, handle
, handle_len
);
7158 /* Default implementation of linux_target_ops method "set_pc" for
7159 32-bit pc register which is literally named "pc". */
7162 linux_set_pc_32bit (struct regcache
*regcache
, CORE_ADDR pc
)
7164 uint32_t newpc
= pc
;
7166 supply_register_by_name (regcache
, "pc", &newpc
);
7169 /* Default implementation of linux_target_ops method "get_pc" for
7170 32-bit pc register which is literally named "pc". */
7173 linux_get_pc_32bit (struct regcache
*regcache
)
7177 collect_register_by_name (regcache
, "pc", &pc
);
7179 debug_printf ("stop pc is 0x%" PRIx32
"\n", pc
);
7183 /* Default implementation of linux_target_ops method "set_pc" for
7184 64-bit pc register which is literally named "pc". */
7187 linux_set_pc_64bit (struct regcache
*regcache
, CORE_ADDR pc
)
7189 uint64_t newpc
= pc
;
7191 supply_register_by_name (regcache
, "pc", &newpc
);
7194 /* Default implementation of linux_target_ops method "get_pc" for
7195 64-bit pc register which is literally named "pc". */
7198 linux_get_pc_64bit (struct regcache
*regcache
)
7202 collect_register_by_name (regcache
, "pc", &pc
);
7204 debug_printf ("stop pc is 0x%" PRIx64
"\n", pc
);
7208 /* See linux-low.h. */
7211 linux_get_auxv (int wordsize
, CORE_ADDR match
, CORE_ADDR
*valp
)
7213 gdb_byte
*data
= (gdb_byte
*) alloca (2 * wordsize
);
7216 gdb_assert (wordsize
== 4 || wordsize
== 8);
7218 while (the_target
->read_auxv (offset
, data
, 2 * wordsize
) == 2 * wordsize
)
7222 uint32_t *data_p
= (uint32_t *) data
;
7223 if (data_p
[0] == match
)
7231 uint64_t *data_p
= (uint64_t *) data
;
7232 if (data_p
[0] == match
)
7239 offset
+= 2 * wordsize
;
7245 /* See linux-low.h. */
7248 linux_get_hwcap (int wordsize
)
7250 CORE_ADDR hwcap
= 0;
7251 linux_get_auxv (wordsize
, AT_HWCAP
, &hwcap
);
7255 /* See linux-low.h. */
7258 linux_get_hwcap2 (int wordsize
)
7260 CORE_ADDR hwcap2
= 0;
7261 linux_get_auxv (wordsize
, AT_HWCAP2
, &hwcap2
);
7265 #ifdef HAVE_LINUX_REGSETS
7267 initialize_regsets_info (struct regsets_info
*info
)
7269 for (info
->num_regsets
= 0;
7270 info
->regsets
[info
->num_regsets
].size
>= 0;
7271 info
->num_regsets
++)
7277 initialize_low (void)
7279 struct sigaction sigchld_action
;
7281 memset (&sigchld_action
, 0, sizeof (sigchld_action
));
7282 set_target_ops (the_linux_target
);
7284 linux_ptrace_init_warnings ();
7285 linux_proc_init_warnings ();
7287 sigchld_action
.sa_handler
= sigchld_handler
;
7288 sigemptyset (&sigchld_action
.sa_mask
);
7289 sigchld_action
.sa_flags
= SA_RESTART
;
7290 sigaction (SIGCHLD
, &sigchld_action
, NULL
);
7292 initialize_low_arch ();
7294 linux_check_ptrace_features ();