Fix: Segmentation fault caused by npd in objdump
[binutils-gdb.git] / gdbserver / linux-low.cc
blob40b6a907ad97bc072996f4215bb053cd943c62f6
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2023 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/>. */
19 #include "server.h"
20 #include "linux-low.h"
21 #include "nat/linux-osdata.h"
22 #include "gdbsupport/agent.h"
23 #include "tdesc.h"
24 #include "gdbsupport/event-loop.h"
25 #include "gdbsupport/event-pipe.h"
26 #include "gdbsupport/rsp-low.h"
27 #include "gdbsupport/signals-state-save-restore.h"
28 #include "nat/linux-nat.h"
29 #include "nat/linux-waitpid.h"
30 #include "gdbsupport/gdb_wait.h"
31 #include "nat/gdb_ptrace.h"
32 #include "nat/linux-ptrace.h"
33 #include "nat/linux-procfs.h"
34 #include "nat/linux-personality.h"
35 #include <signal.h>
36 #include <sys/ioctl.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <sys/syscall.h>
40 #include <sched.h>
41 #include <ctype.h>
42 #include <pwd.h>
43 #include <sys/types.h>
44 #include <dirent.h>
45 #include <sys/stat.h>
46 #include <sys/vfs.h>
47 #include <sys/uio.h>
48 #include "gdbsupport/filestuff.h"
49 #include "tracepoint.h"
50 #include <inttypes.h>
51 #include "gdbsupport/common-inferior.h"
52 #include "nat/fork-inferior.h"
53 #include "gdbsupport/environ.h"
54 #include "gdbsupport/gdb-sigmask.h"
55 #include "gdbsupport/scoped_restore.h"
56 #ifndef ELFMAG0
57 /* Don't include <linux/elf.h> here. If it got included by gdb_proc_service.h
58 then ELFMAG0 will have been defined. If it didn't get included by
59 gdb_proc_service.h then including it will likely introduce a duplicate
60 definition of elf_fpregset_t. */
61 #include <elf.h>
62 #endif
63 #include "nat/linux-namespaces.h"
65 #ifndef O_LARGEFILE
66 #define O_LARGEFILE 0
67 #endif
69 #ifndef AT_HWCAP2
70 #define AT_HWCAP2 26
71 #endif
73 /* Some targets did not define these ptrace constants from the start,
74 so gdbserver defines them locally here. In the future, these may
75 be removed after they are added to asm/ptrace.h. */
76 #if !(defined(PT_TEXT_ADDR) \
77 || defined(PT_DATA_ADDR) \
78 || defined(PT_TEXT_END_ADDR))
79 #if defined(__mcoldfire__)
80 /* These are still undefined in 3.10 kernels. */
81 #define PT_TEXT_ADDR 49*4
82 #define PT_DATA_ADDR 50*4
83 #define PT_TEXT_END_ADDR 51*4
84 /* These are still undefined in 3.10 kernels. */
85 #elif defined(__TMS320C6X__)
86 #define PT_TEXT_ADDR (0x10000*4)
87 #define PT_DATA_ADDR (0x10004*4)
88 #define PT_TEXT_END_ADDR (0x10008*4)
89 #endif
90 #endif
92 #if (defined(__UCLIBC__) \
93 && defined(HAS_NOMMU) \
94 && defined(PT_TEXT_ADDR) \
95 && defined(PT_DATA_ADDR) \
96 && defined(PT_TEXT_END_ADDR))
97 #define SUPPORTS_READ_OFFSETS
98 #endif
100 #ifdef HAVE_LINUX_BTRACE
101 # include "nat/linux-btrace.h"
102 # include "gdbsupport/btrace-common.h"
103 #endif
105 #ifndef HAVE_ELF32_AUXV_T
106 /* Copied from glibc's elf.h. */
107 typedef struct
109 uint32_t a_type; /* Entry type */
110 union
112 uint32_t a_val; /* Integer value */
113 /* We use to have pointer elements added here. We cannot do that,
114 though, since it does not work when using 32-bit definitions
115 on 64-bit platforms and vice versa. */
116 } a_un;
117 } Elf32_auxv_t;
118 #endif
120 #ifndef HAVE_ELF64_AUXV_T
121 /* Copied from glibc's elf.h. */
122 typedef struct
124 uint64_t a_type; /* Entry type */
125 union
127 uint64_t a_val; /* Integer value */
128 /* We use to have pointer elements added here. We cannot do that,
129 though, since it does not work when using 32-bit definitions
130 on 64-bit platforms and vice versa. */
131 } a_un;
132 } Elf64_auxv_t;
133 #endif
135 /* Does the current host support PTRACE_GETREGSET? */
136 int have_ptrace_getregset = -1;
138 /* Return TRUE if THREAD is the leader thread of the process. */
140 static bool
141 is_leader (thread_info *thread)
143 ptid_t ptid = ptid_of (thread);
144 return ptid.pid () == ptid.lwp ();
147 /* LWP accessors. */
149 /* See nat/linux-nat.h. */
151 ptid_t
152 ptid_of_lwp (struct lwp_info *lwp)
154 return ptid_of (get_lwp_thread (lwp));
157 /* See nat/linux-nat.h. */
159 void
160 lwp_set_arch_private_info (struct lwp_info *lwp,
161 struct arch_lwp_info *info)
163 lwp->arch_private = info;
166 /* See nat/linux-nat.h. */
168 struct arch_lwp_info *
169 lwp_arch_private_info (struct lwp_info *lwp)
171 return lwp->arch_private;
174 /* See nat/linux-nat.h. */
177 lwp_is_stopped (struct lwp_info *lwp)
179 return lwp->stopped;
182 /* See nat/linux-nat.h. */
184 enum target_stop_reason
185 lwp_stop_reason (struct lwp_info *lwp)
187 return lwp->stop_reason;
190 /* See nat/linux-nat.h. */
193 lwp_is_stepping (struct lwp_info *lwp)
195 return lwp->stepping;
198 /* A list of all unknown processes which receive stop signals. Some
199 other process will presumably claim each of these as forked
200 children momentarily. */
202 struct simple_pid_list
204 /* The process ID. */
205 int pid;
207 /* The status as reported by waitpid. */
208 int status;
210 /* Next in chain. */
211 struct simple_pid_list *next;
213 static struct simple_pid_list *stopped_pids;
215 /* Trivial list manipulation functions to keep track of a list of new
216 stopped processes. */
218 static void
219 add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
221 struct simple_pid_list *new_pid = XNEW (struct simple_pid_list);
223 new_pid->pid = pid;
224 new_pid->status = status;
225 new_pid->next = *listp;
226 *listp = new_pid;
229 static int
230 pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
232 struct simple_pid_list **p;
234 for (p = listp; *p != NULL; p = &(*p)->next)
235 if ((*p)->pid == pid)
237 struct simple_pid_list *next = (*p)->next;
239 *statusp = (*p)->status;
240 xfree (*p);
241 *p = next;
242 return 1;
244 return 0;
247 enum stopping_threads_kind
249 /* Not stopping threads presently. */
250 NOT_STOPPING_THREADS,
252 /* Stopping threads. */
253 STOPPING_THREADS,
255 /* Stopping and suspending threads. */
256 STOPPING_AND_SUSPENDING_THREADS
259 /* This is set while stop_all_lwps is in effect. */
260 static stopping_threads_kind stopping_threads = NOT_STOPPING_THREADS;
262 /* FIXME make into a target method? */
263 int using_threads = 1;
265 /* True if we're presently stabilizing threads (moving them out of
266 jump pads). */
267 static int stabilizing_threads;
269 static void unsuspend_all_lwps (struct lwp_info *except);
270 static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
271 static int lwp_is_marked_dead (struct lwp_info *lwp);
272 static int kill_lwp (unsigned long lwpid, int signo);
273 static void enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info);
274 static int linux_low_ptrace_options (int attached);
275 static int check_ptrace_stopped_lwp_gone (struct lwp_info *lp);
277 /* When the event-loop is doing a step-over, this points at the thread
278 being stepped. */
279 static ptid_t step_over_bkpt;
281 bool
282 linux_process_target::low_supports_breakpoints ()
284 return false;
287 CORE_ADDR
288 linux_process_target::low_get_pc (regcache *regcache)
290 return 0;
293 void
294 linux_process_target::low_set_pc (regcache *regcache, CORE_ADDR newpc)
296 gdb_assert_not_reached ("linux target op low_set_pc is not implemented");
299 std::vector<CORE_ADDR>
300 linux_process_target::low_get_next_pcs (regcache *regcache)
302 gdb_assert_not_reached ("linux target op low_get_next_pcs is not "
303 "implemented");
307 linux_process_target::low_decr_pc_after_break ()
309 return 0;
312 /* True if LWP is stopped in its stepping range. */
314 static int
315 lwp_in_step_range (struct lwp_info *lwp)
317 CORE_ADDR pc = lwp->stop_pc;
319 return (pc >= lwp->step_range_start && pc < lwp->step_range_end);
322 /* The event pipe registered as a waitable file in the event loop. */
323 static event_pipe linux_event_pipe;
325 /* True if we're currently in async mode. */
326 #define target_is_async_p() (linux_event_pipe.is_open ())
328 static void send_sigstop (struct lwp_info *lwp);
330 /* Return non-zero if HEADER is a 64-bit ELF file. */
332 static int
333 elf_64_header_p (const Elf64_Ehdr *header, unsigned int *machine)
335 if (header->e_ident[EI_MAG0] == ELFMAG0
336 && header->e_ident[EI_MAG1] == ELFMAG1
337 && header->e_ident[EI_MAG2] == ELFMAG2
338 && header->e_ident[EI_MAG3] == ELFMAG3)
340 *machine = header->e_machine;
341 return header->e_ident[EI_CLASS] == ELFCLASS64;
344 *machine = EM_NONE;
345 return -1;
348 /* Return non-zero if FILE is a 64-bit ELF file,
349 zero if the file is not a 64-bit ELF file,
350 and -1 if the file is not accessible or doesn't exist. */
352 static int
353 elf_64_file_p (const char *file, unsigned int *machine)
355 Elf64_Ehdr header;
356 int fd;
358 fd = open (file, O_RDONLY);
359 if (fd < 0)
360 return -1;
362 if (read (fd, &header, sizeof (header)) != sizeof (header))
364 close (fd);
365 return 0;
367 close (fd);
369 return elf_64_header_p (&header, machine);
372 /* Accepts an integer PID; Returns true if the executable PID is
373 running is a 64-bit ELF file.. */
376 linux_pid_exe_is_elf_64_file (int pid, unsigned int *machine)
378 char file[PATH_MAX];
380 sprintf (file, "/proc/%d/exe", pid);
381 return elf_64_file_p (file, machine);
384 void
385 linux_process_target::delete_lwp (lwp_info *lwp)
387 struct thread_info *thr = get_lwp_thread (lwp);
389 threads_debug_printf ("deleting %ld", lwpid_of (thr));
391 remove_thread (thr);
393 low_delete_thread (lwp->arch_private);
395 delete lwp;
398 void
399 linux_process_target::low_delete_thread (arch_lwp_info *info)
401 /* Default implementation should be overridden if architecture-specific
402 info is being used. */
403 gdb_assert (info == nullptr);
406 /* Open the /proc/PID/mem file for PROC. */
408 static void
409 open_proc_mem_file (process_info *proc)
411 gdb_assert (proc->priv->mem_fd == -1);
413 char filename[64];
414 xsnprintf (filename, sizeof filename, "/proc/%d/mem", proc->pid);
416 proc->priv->mem_fd
417 = gdb_open_cloexec (filename, O_RDWR | O_LARGEFILE, 0).release ();
420 process_info *
421 linux_process_target::add_linux_process_no_mem_file (int pid, int attached)
423 struct process_info *proc;
425 proc = add_process (pid, attached);
426 proc->priv = XCNEW (struct process_info_private);
428 proc->priv->arch_private = low_new_process ();
429 proc->priv->mem_fd = -1;
431 return proc;
435 process_info *
436 linux_process_target::add_linux_process (int pid, int attached)
438 process_info *proc = add_linux_process_no_mem_file (pid, attached);
439 open_proc_mem_file (proc);
440 return proc;
443 void
444 linux_process_target::remove_linux_process (process_info *proc)
446 if (proc->priv->mem_fd >= 0)
447 close (proc->priv->mem_fd);
449 this->low_delete_process (proc->priv->arch_private);
451 xfree (proc->priv);
452 proc->priv = nullptr;
454 remove_process (proc);
457 arch_process_info *
458 linux_process_target::low_new_process ()
460 return nullptr;
463 void
464 linux_process_target::low_delete_process (arch_process_info *info)
466 /* Default implementation must be overridden if architecture-specific
467 info exists. */
468 gdb_assert (info == nullptr);
471 void
472 linux_process_target::low_new_fork (process_info *parent, process_info *child)
474 /* Nop. */
477 void
478 linux_process_target::arch_setup_thread (thread_info *thread)
480 scoped_restore_current_thread restore_thread;
481 switch_to_thread (thread);
483 low_arch_setup ();
487 linux_process_target::handle_extended_wait (lwp_info **orig_event_lwp,
488 int wstat)
490 client_state &cs = get_client_state ();
491 struct lwp_info *event_lwp = *orig_event_lwp;
492 int event = linux_ptrace_get_extended_event (wstat);
493 struct thread_info *event_thr = get_lwp_thread (event_lwp);
494 struct lwp_info *new_lwp;
496 gdb_assert (event_lwp->waitstatus.kind () == TARGET_WAITKIND_IGNORE);
498 /* All extended events we currently use are mid-syscall. Only
499 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
500 you have to be using PTRACE_SEIZE to get that. */
501 event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
503 if ((event == PTRACE_EVENT_FORK) || (event == PTRACE_EVENT_VFORK)
504 || (event == PTRACE_EVENT_CLONE))
506 ptid_t ptid;
507 unsigned long new_pid;
508 int ret, status;
510 /* Get the pid of the new lwp. */
511 ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_thr), (PTRACE_TYPE_ARG3) 0,
512 &new_pid);
514 /* If we haven't already seen the new PID stop, wait for it now. */
515 if (!pull_pid_from_list (&stopped_pids, new_pid, &status))
517 /* The new child has a pending SIGSTOP. We can't affect it until it
518 hits the SIGSTOP, but we're already attached. */
520 ret = my_waitpid (new_pid, &status, __WALL);
522 if (ret == -1)
523 perror_with_name ("waiting for new child");
524 else if (ret != new_pid)
525 warning ("wait returned unexpected PID %d", ret);
526 else if (!WIFSTOPPED (status))
527 warning ("wait returned unexpected status 0x%x", status);
530 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
532 struct process_info *parent_proc;
533 struct process_info *child_proc;
534 struct lwp_info *child_lwp;
535 struct thread_info *child_thr;
537 ptid = ptid_t (new_pid, new_pid);
539 threads_debug_printf ("Got fork event from LWP %ld, "
540 "new child is %d",
541 ptid_of (event_thr).lwp (),
542 ptid.pid ());
544 /* Add the new process to the tables and clone the breakpoint
545 lists of the parent. We need to do this even if the new process
546 will be detached, since we will need the process object and the
547 breakpoints to remove any breakpoints from memory when we
548 detach, and the client side will access registers. */
549 child_proc = add_linux_process (new_pid, 0);
550 gdb_assert (child_proc != NULL);
551 child_lwp = add_lwp (ptid);
552 gdb_assert (child_lwp != NULL);
553 child_lwp->stopped = 1;
554 child_lwp->must_set_ptrace_flags = 1;
555 child_lwp->status_pending_p = 0;
556 child_thr = get_lwp_thread (child_lwp);
557 child_thr->last_resume_kind = resume_stop;
558 child_thr->last_status.set_stopped (GDB_SIGNAL_0);
560 /* If we're suspending all threads, leave this one suspended
561 too. If the fork/clone parent is stepping over a breakpoint,
562 all other threads have been suspended already. Leave the
563 child suspended too. */
564 if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
565 || event_lwp->bp_reinsert != 0)
567 threads_debug_printf ("leaving child suspended");
568 child_lwp->suspended = 1;
571 parent_proc = get_thread_process (event_thr);
572 child_proc->attached = parent_proc->attached;
574 if (event_lwp->bp_reinsert != 0
575 && supports_software_single_step ()
576 && event == PTRACE_EVENT_VFORK)
578 /* If we leave single-step breakpoints there, child will
579 hit it, so uninsert single-step breakpoints from parent
580 (and child). Once vfork child is done, reinsert
581 them back to parent. */
582 uninsert_single_step_breakpoints (event_thr);
585 clone_all_breakpoints (child_thr, event_thr);
587 target_desc_up tdesc = allocate_target_description ();
588 copy_target_description (tdesc.get (), parent_proc->tdesc);
589 child_proc->tdesc = tdesc.release ();
591 /* Clone arch-specific process data. */
592 low_new_fork (parent_proc, child_proc);
594 /* Save fork info in the parent thread. */
595 if (event == PTRACE_EVENT_FORK)
596 event_lwp->waitstatus.set_forked (ptid);
597 else if (event == PTRACE_EVENT_VFORK)
598 event_lwp->waitstatus.set_vforked (ptid);
600 /* The status_pending field contains bits denoting the
601 extended event, so when the pending event is handled,
602 the handler will look at lwp->waitstatus. */
603 event_lwp->status_pending_p = 1;
604 event_lwp->status_pending = wstat;
606 /* Link the threads until the parent event is passed on to
607 higher layers. */
608 event_lwp->fork_relative = child_lwp;
609 child_lwp->fork_relative = event_lwp;
611 /* If the parent thread is doing step-over with single-step
612 breakpoints, the list of single-step breakpoints are cloned
613 from the parent's. Remove them from the child process.
614 In case of vfork, we'll reinsert them back once vforked
615 child is done. */
616 if (event_lwp->bp_reinsert != 0
617 && supports_software_single_step ())
619 /* The child process is forked and stopped, so it is safe
620 to access its memory without stopping all other threads
621 from other processes. */
622 delete_single_step_breakpoints (child_thr);
624 gdb_assert (has_single_step_breakpoints (event_thr));
625 gdb_assert (!has_single_step_breakpoints (child_thr));
628 /* Report the event. */
629 return 0;
632 threads_debug_printf
633 ("Got clone event from LWP %ld, new child is LWP %ld",
634 lwpid_of (event_thr), new_pid);
636 ptid = ptid_t (pid_of (event_thr), new_pid);
637 new_lwp = add_lwp (ptid);
639 /* Either we're going to immediately resume the new thread
640 or leave it stopped. resume_one_lwp is a nop if it
641 thinks the thread is currently running, so set this first
642 before calling resume_one_lwp. */
643 new_lwp->stopped = 1;
645 /* If we're suspending all threads, leave this one suspended
646 too. If the fork/clone parent is stepping over a breakpoint,
647 all other threads have been suspended already. Leave the
648 child suspended too. */
649 if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
650 || event_lwp->bp_reinsert != 0)
651 new_lwp->suspended = 1;
653 /* Normally we will get the pending SIGSTOP. But in some cases
654 we might get another signal delivered to the group first.
655 If we do get another signal, be sure not to lose it. */
656 if (WSTOPSIG (status) != SIGSTOP)
658 new_lwp->stop_expected = 1;
659 new_lwp->status_pending_p = 1;
660 new_lwp->status_pending = status;
662 else if (cs.report_thread_events)
664 new_lwp->waitstatus.set_thread_created ();
665 new_lwp->status_pending_p = 1;
666 new_lwp->status_pending = status;
669 #ifdef USE_THREAD_DB
670 thread_db_notice_clone (event_thr, ptid);
671 #endif
673 /* Don't report the event. */
674 return 1;
676 else if (event == PTRACE_EVENT_VFORK_DONE)
678 event_lwp->waitstatus.set_vfork_done ();
680 if (event_lwp->bp_reinsert != 0 && supports_software_single_step ())
682 reinsert_single_step_breakpoints (event_thr);
684 gdb_assert (has_single_step_breakpoints (event_thr));
687 /* Report the event. */
688 return 0;
690 else if (event == PTRACE_EVENT_EXEC && cs.report_exec_events)
692 struct process_info *proc;
693 std::vector<int> syscalls_to_catch;
694 ptid_t event_ptid;
695 pid_t event_pid;
697 threads_debug_printf ("Got exec event from LWP %ld",
698 lwpid_of (event_thr));
700 /* Get the event ptid. */
701 event_ptid = ptid_of (event_thr);
702 event_pid = event_ptid.pid ();
704 /* Save the syscall list from the execing process. */
705 proc = get_thread_process (event_thr);
706 syscalls_to_catch = std::move (proc->syscalls_to_catch);
708 /* Delete the execing process and all its threads. */
709 mourn (proc);
710 switch_to_thread (nullptr);
712 /* Create a new process/lwp/thread. */
713 proc = add_linux_process (event_pid, 0);
714 event_lwp = add_lwp (event_ptid);
715 event_thr = get_lwp_thread (event_lwp);
716 gdb_assert (current_thread == event_thr);
717 arch_setup_thread (event_thr);
719 /* Set the event status. */
720 event_lwp->waitstatus.set_execd
721 (make_unique_xstrdup
722 (linux_proc_pid_to_exec_file (lwpid_of (event_thr))));
724 /* Mark the exec status as pending. */
725 event_lwp->stopped = 1;
726 event_lwp->status_pending_p = 1;
727 event_lwp->status_pending = wstat;
728 event_thr->last_resume_kind = resume_continue;
729 event_thr->last_status.set_ignore ();
731 /* Update syscall state in the new lwp, effectively mid-syscall too. */
732 event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
734 /* Restore the list to catch. Don't rely on the client, which is free
735 to avoid sending a new list when the architecture doesn't change.
736 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
737 proc->syscalls_to_catch = std::move (syscalls_to_catch);
739 /* Report the event. */
740 *orig_event_lwp = event_lwp;
741 return 0;
744 internal_error (_("unknown ptrace event %d"), event);
747 CORE_ADDR
748 linux_process_target::get_pc (lwp_info *lwp)
750 process_info *proc = get_thread_process (get_lwp_thread (lwp));
751 gdb_assert (!proc->starting_up);
753 if (!low_supports_breakpoints ())
754 return 0;
756 scoped_restore_current_thread restore_thread;
757 switch_to_thread (get_lwp_thread (lwp));
759 struct regcache *regcache = get_thread_regcache (current_thread, 1);
760 CORE_ADDR pc = low_get_pc (regcache);
762 threads_debug_printf ("pc is 0x%lx", (long) pc);
764 return pc;
767 void
768 linux_process_target::get_syscall_trapinfo (lwp_info *lwp, int *sysno)
770 struct regcache *regcache;
772 scoped_restore_current_thread restore_thread;
773 switch_to_thread (get_lwp_thread (lwp));
775 regcache = get_thread_regcache (current_thread, 1);
776 low_get_syscall_trapinfo (regcache, sysno);
778 threads_debug_printf ("get_syscall_trapinfo sysno %d", *sysno);
781 void
782 linux_process_target::low_get_syscall_trapinfo (regcache *regcache, int *sysno)
784 /* By default, report an unknown system call number. */
785 *sysno = UNKNOWN_SYSCALL;
788 bool
789 linux_process_target::save_stop_reason (lwp_info *lwp)
791 CORE_ADDR pc;
792 CORE_ADDR sw_breakpoint_pc;
793 #if USE_SIGTRAP_SIGINFO
794 siginfo_t siginfo;
795 #endif
797 if (!low_supports_breakpoints ())
798 return false;
800 process_info *proc = get_thread_process (get_lwp_thread (lwp));
801 if (proc->starting_up)
803 /* Claim we have the stop PC so that the caller doesn't try to
804 fetch it itself. */
805 return true;
808 pc = get_pc (lwp);
809 sw_breakpoint_pc = pc - low_decr_pc_after_break ();
811 /* breakpoint_at reads from the current thread. */
812 scoped_restore_current_thread restore_thread;
813 switch_to_thread (get_lwp_thread (lwp));
815 #if USE_SIGTRAP_SIGINFO
816 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
817 (PTRACE_TYPE_ARG3) 0, &siginfo) == 0)
819 if (siginfo.si_signo == SIGTRAP)
821 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)
822 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
824 /* The si_code is ambiguous on this arch -- check debug
825 registers. */
826 if (!check_stopped_by_watchpoint (lwp))
827 lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
829 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
831 /* If we determine the LWP stopped for a SW breakpoint,
832 trust it. Particularly don't check watchpoint
833 registers, because at least on s390, we'd find
834 stopped-by-watchpoint as long as there's a watchpoint
835 set. */
836 lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
838 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
840 /* This can indicate either a hardware breakpoint or
841 hardware watchpoint. Check debug registers. */
842 if (!check_stopped_by_watchpoint (lwp))
843 lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
845 else if (siginfo.si_code == TRAP_TRACE)
847 /* We may have single stepped an instruction that
848 triggered a watchpoint. In that case, on some
849 architectures (such as x86), instead of TRAP_HWBKPT,
850 si_code indicates TRAP_TRACE, and we need to check
851 the debug registers separately. */
852 if (!check_stopped_by_watchpoint (lwp))
853 lwp->stop_reason = TARGET_STOPPED_BY_SINGLE_STEP;
857 #else
858 /* We may have just stepped a breakpoint instruction. E.g., in
859 non-stop mode, GDB first tells the thread A to step a range, and
860 then the user inserts a breakpoint inside the range. In that
861 case we need to report the breakpoint PC. */
862 if ((!lwp->stepping || lwp->stop_pc == sw_breakpoint_pc)
863 && low_breakpoint_at (sw_breakpoint_pc))
864 lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
866 if (hardware_breakpoint_inserted_here (pc))
867 lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
869 if (lwp->stop_reason == TARGET_STOPPED_BY_NO_REASON)
870 check_stopped_by_watchpoint (lwp);
871 #endif
873 if (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT)
875 threads_debug_printf
876 ("%s stopped by software breakpoint",
877 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
879 /* Back up the PC if necessary. */
880 if (pc != sw_breakpoint_pc)
882 struct regcache *regcache
883 = get_thread_regcache (current_thread, 1);
884 low_set_pc (regcache, sw_breakpoint_pc);
887 /* Update this so we record the correct stop PC below. */
888 pc = sw_breakpoint_pc;
890 else if (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
891 threads_debug_printf
892 ("%s stopped by hardware breakpoint",
893 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
894 else if (lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
895 threads_debug_printf
896 ("%s stopped by hardware watchpoint",
897 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
898 else if (lwp->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP)
899 threads_debug_printf
900 ("%s stopped by trace",
901 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
903 lwp->stop_pc = pc;
904 return true;
907 lwp_info *
908 linux_process_target::add_lwp (ptid_t ptid)
910 lwp_info *lwp = new lwp_info;
912 lwp->thread = add_thread (ptid, lwp);
914 low_new_thread (lwp);
916 return lwp;
919 void
920 linux_process_target::low_new_thread (lwp_info *info)
922 /* Nop. */
925 /* Callback to be used when calling fork_inferior, responsible for
926 actually initiating the tracing of the inferior. */
928 static void
929 linux_ptrace_fun ()
931 if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
932 (PTRACE_TYPE_ARG4) 0) < 0)
933 trace_start_error_with_name ("ptrace");
935 if (setpgid (0, 0) < 0)
936 trace_start_error_with_name ("setpgid");
938 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
939 stdout to stderr so that inferior i/o doesn't corrupt the connection.
940 Also, redirect stdin to /dev/null. */
941 if (remote_connection_is_stdio ())
943 if (close (0) < 0)
944 trace_start_error_with_name ("close");
945 if (open ("/dev/null", O_RDONLY) < 0)
946 trace_start_error_with_name ("open");
947 if (dup2 (2, 1) < 0)
948 trace_start_error_with_name ("dup2");
949 if (write (2, "stdin/stdout redirected\n",
950 sizeof ("stdin/stdout redirected\n") - 1) < 0)
952 /* Errors ignored. */;
957 /* Start an inferior process and returns its pid.
958 PROGRAM is the name of the program to be started, and PROGRAM_ARGS
959 are its arguments. */
962 linux_process_target::create_inferior (const char *program,
963 const std::vector<char *> &program_args)
965 client_state &cs = get_client_state ();
966 struct lwp_info *new_lwp;
967 int pid;
968 ptid_t ptid;
971 maybe_disable_address_space_randomization restore_personality
972 (cs.disable_randomization);
973 std::string str_program_args = construct_inferior_arguments (program_args);
975 pid = fork_inferior (program,
976 str_program_args.c_str (),
977 get_environ ()->envp (), linux_ptrace_fun,
978 NULL, NULL, NULL, NULL);
981 /* When spawning a new process, we can't open the mem file yet. We
982 still have to nurse the process through the shell, and that execs
983 a couple times. The address space a /proc/PID/mem file is
984 accessing is destroyed on exec. */
985 process_info *proc = add_linux_process_no_mem_file (pid, 0);
987 ptid = ptid_t (pid, pid);
988 new_lwp = add_lwp (ptid);
989 new_lwp->must_set_ptrace_flags = 1;
991 post_fork_inferior (pid, program);
993 /* PROC is now past the shell running the program we want, so we can
994 open the /proc/PID/mem file. */
995 open_proc_mem_file (proc);
997 return pid;
1000 /* Implement the post_create_inferior target_ops method. */
1002 void
1003 linux_process_target::post_create_inferior ()
1005 struct lwp_info *lwp = get_thread_lwp (current_thread);
1007 low_arch_setup ();
1009 if (lwp->must_set_ptrace_flags)
1011 struct process_info *proc = current_process ();
1012 int options = linux_low_ptrace_options (proc->attached);
1014 linux_enable_event_reporting (lwpid_of (current_thread), options);
1015 lwp->must_set_ptrace_flags = 0;
1020 linux_process_target::attach_lwp (ptid_t ptid)
1022 struct lwp_info *new_lwp;
1023 int lwpid = ptid.lwp ();
1025 if (ptrace (PTRACE_ATTACH, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0)
1026 != 0)
1027 return errno;
1029 new_lwp = add_lwp (ptid);
1031 /* We need to wait for SIGSTOP before being able to make the next
1032 ptrace call on this LWP. */
1033 new_lwp->must_set_ptrace_flags = 1;
1035 if (linux_proc_pid_is_stopped (lwpid))
1037 threads_debug_printf ("Attached to a stopped process");
1039 /* The process is definitely stopped. It is in a job control
1040 stop, unless the kernel predates the TASK_STOPPED /
1041 TASK_TRACED distinction, in which case it might be in a
1042 ptrace stop. Make sure it is in a ptrace stop; from there we
1043 can kill it, signal it, et cetera.
1045 First make sure there is a pending SIGSTOP. Since we are
1046 already attached, the process can not transition from stopped
1047 to running without a PTRACE_CONT; so we know this signal will
1048 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1049 probably already in the queue (unless this kernel is old
1050 enough to use TASK_STOPPED for ptrace stops); but since
1051 SIGSTOP is not an RT signal, it can only be queued once. */
1052 kill_lwp (lwpid, SIGSTOP);
1054 /* Finally, resume the stopped process. This will deliver the
1055 SIGSTOP (or a higher priority signal, just like normal
1056 PTRACE_ATTACH), which we'll catch later on. */
1057 ptrace (PTRACE_CONT, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
1060 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1061 brings it to a halt.
1063 There are several cases to consider here:
1065 1) gdbserver has already attached to the process and is being notified
1066 of a new thread that is being created.
1067 In this case we should ignore that SIGSTOP and resume the
1068 process. This is handled below by setting stop_expected = 1,
1069 and the fact that add_thread sets last_resume_kind ==
1070 resume_continue.
1072 2) This is the first thread (the process thread), and we're attaching
1073 to it via attach_inferior.
1074 In this case we want the process thread to stop.
1075 This is handled by having linux_attach set last_resume_kind ==
1076 resume_stop after we return.
1078 If the pid we are attaching to is also the tgid, we attach to and
1079 stop all the existing threads. Otherwise, we attach to pid and
1080 ignore any other threads in the same group as this pid.
1082 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1083 existing threads.
1084 In this case we want the thread to stop.
1085 FIXME: This case is currently not properly handled.
1086 We should wait for the SIGSTOP but don't. Things work apparently
1087 because enough time passes between when we ptrace (ATTACH) and when
1088 gdb makes the next ptrace call on the thread.
1090 On the other hand, if we are currently trying to stop all threads, we
1091 should treat the new thread as if we had sent it a SIGSTOP. This works
1092 because we are guaranteed that the add_lwp call above added us to the
1093 end of the list, and so the new thread has not yet reached
1094 wait_for_sigstop (but will). */
1095 new_lwp->stop_expected = 1;
1097 return 0;
1100 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1101 already attached. Returns true if a new LWP is found, false
1102 otherwise. */
1104 static int
1105 attach_proc_task_lwp_callback (ptid_t ptid)
1107 /* Is this a new thread? */
1108 if (find_thread_ptid (ptid) == NULL)
1110 int lwpid = ptid.lwp ();
1111 int err;
1113 threads_debug_printf ("Found new lwp %d", lwpid);
1115 err = the_linux_target->attach_lwp (ptid);
1117 /* Be quiet if we simply raced with the thread exiting. EPERM
1118 is returned if the thread's task still exists, and is marked
1119 as exited or zombie, as well as other conditions, so in that
1120 case, confirm the status in /proc/PID/status. */
1121 if (err == ESRCH
1122 || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
1123 threads_debug_printf
1124 ("Cannot attach to lwp %d: thread is gone (%d: %s)",
1125 lwpid, err, safe_strerror (err));
1126 else if (err != 0)
1128 std::string reason
1129 = linux_ptrace_attach_fail_reason_string (ptid, err);
1131 warning (_("Cannot attach to lwp %d: %s"), lwpid, reason.c_str ());
1134 return 1;
1136 return 0;
1139 static void async_file_mark (void);
1141 /* Attach to PID. If PID is the tgid, attach to it and all
1142 of its threads. */
1145 linux_process_target::attach (unsigned long pid)
1147 struct process_info *proc;
1148 struct thread_info *initial_thread;
1149 ptid_t ptid = ptid_t (pid, pid);
1150 int err;
1152 /* Delay opening the /proc/PID/mem file until we've successfully
1153 attached. */
1154 proc = add_linux_process_no_mem_file (pid, 1);
1156 /* Attach to PID. We will check for other threads
1157 soon. */
1158 err = attach_lwp (ptid);
1159 if (err != 0)
1161 this->remove_linux_process (proc);
1163 std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
1164 error ("Cannot attach to process %ld: %s", pid, reason.c_str ());
1167 open_proc_mem_file (proc);
1169 /* Don't ignore the initial SIGSTOP if we just attached to this
1170 process. It will be collected by wait shortly. */
1171 initial_thread = find_thread_ptid (ptid_t (pid, pid));
1172 gdb_assert (initial_thread != nullptr);
1173 initial_thread->last_resume_kind = resume_stop;
1175 /* We must attach to every LWP. If /proc is mounted, use that to
1176 find them now. On the one hand, the inferior may be using raw
1177 clone instead of using pthreads. On the other hand, even if it
1178 is using pthreads, GDB may not be connected yet (thread_db needs
1179 to do symbol lookups, through qSymbol). Also, thread_db walks
1180 structures in the inferior's address space to find the list of
1181 threads/LWPs, and those structures may well be corrupted. Note
1182 that once thread_db is loaded, we'll still use it to list threads
1183 and associate pthread info with each LWP. */
1184 linux_proc_attach_tgid_threads (pid, attach_proc_task_lwp_callback);
1186 /* GDB will shortly read the xml target description for this
1187 process, to figure out the process' architecture. But the target
1188 description is only filled in when the first process/thread in
1189 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1190 that now, otherwise, if GDB is fast enough, it could read the
1191 target description _before_ that initial stop. */
1192 if (non_stop)
1194 struct lwp_info *lwp;
1195 int wstat, lwpid;
1196 ptid_t pid_ptid = ptid_t (pid);
1198 lwpid = wait_for_event_filtered (pid_ptid, pid_ptid, &wstat, __WALL);
1199 gdb_assert (lwpid > 0);
1201 lwp = find_lwp_pid (ptid_t (lwpid));
1202 gdb_assert (lwp != nullptr);
1204 if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGSTOP)
1206 lwp->status_pending_p = 1;
1207 lwp->status_pending = wstat;
1210 initial_thread->last_resume_kind = resume_continue;
1212 async_file_mark ();
1214 gdb_assert (proc->tdesc != NULL);
1217 return 0;
1220 static int
1221 last_thread_of_process_p (int pid)
1223 bool seen_one = false;
1225 thread_info *thread = find_thread (pid, [&] (thread_info *thr_arg)
1227 if (!seen_one)
1229 /* This is the first thread of this process we see. */
1230 seen_one = true;
1231 return false;
1233 else
1235 /* This is the second thread of this process we see. */
1236 return true;
1240 return thread == NULL;
1243 /* Kill LWP. */
1245 static void
1246 linux_kill_one_lwp (struct lwp_info *lwp)
1248 struct thread_info *thr = get_lwp_thread (lwp);
1249 int pid = lwpid_of (thr);
1251 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1252 there is no signal context, and ptrace(PTRACE_KILL) (or
1253 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1254 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1255 alternative is to kill with SIGKILL. We only need one SIGKILL
1256 per process, not one for each thread. But since we still support
1257 support debugging programs using raw clone without CLONE_THREAD,
1258 we send one for each thread. For years, we used PTRACE_KILL
1259 only, so we're being a bit paranoid about some old kernels where
1260 PTRACE_KILL might work better (dubious if there are any such, but
1261 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1262 second, and so we're fine everywhere. */
1264 errno = 0;
1265 kill_lwp (pid, SIGKILL);
1266 if (debug_threads)
1268 int save_errno = errno;
1270 threads_debug_printf ("kill_lwp (SIGKILL) %s, 0, 0 (%s)",
1271 target_pid_to_str (ptid_of (thr)).c_str (),
1272 save_errno ? safe_strerror (save_errno) : "OK");
1275 errno = 0;
1276 ptrace (PTRACE_KILL, pid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
1277 if (debug_threads)
1279 int save_errno = errno;
1281 threads_debug_printf ("PTRACE_KILL %s, 0, 0 (%s)",
1282 target_pid_to_str (ptid_of (thr)).c_str (),
1283 save_errno ? safe_strerror (save_errno) : "OK");
1287 /* Kill LWP and wait for it to die. */
1289 static void
1290 kill_wait_lwp (struct lwp_info *lwp)
1292 struct thread_info *thr = get_lwp_thread (lwp);
1293 int pid = ptid_of (thr).pid ();
1294 int lwpid = ptid_of (thr).lwp ();
1295 int wstat;
1296 int res;
1298 threads_debug_printf ("killing lwp %d, for pid: %d", lwpid, pid);
1302 linux_kill_one_lwp (lwp);
1304 /* Make sure it died. Notes:
1306 - The loop is most likely unnecessary.
1308 - We don't use wait_for_event as that could delete lwps
1309 while we're iterating over them. We're not interested in
1310 any pending status at this point, only in making sure all
1311 wait status on the kernel side are collected until the
1312 process is reaped.
1314 - We don't use __WALL here as the __WALL emulation relies on
1315 SIGCHLD, and killing a stopped process doesn't generate
1316 one, nor an exit status.
1318 res = my_waitpid (lwpid, &wstat, 0);
1319 if (res == -1 && errno == ECHILD)
1320 res = my_waitpid (lwpid, &wstat, __WCLONE);
1321 } while (res > 0 && WIFSTOPPED (wstat));
1323 /* Even if it was stopped, the child may have already disappeared.
1324 E.g., if it was killed by SIGKILL. */
1325 if (res < 0 && errno != ECHILD)
1326 perror_with_name ("kill_wait_lwp");
1329 /* Callback for `for_each_thread'. Kills an lwp of a given process,
1330 except the leader. */
1332 static void
1333 kill_one_lwp_callback (thread_info *thread, int pid)
1335 struct lwp_info *lwp = get_thread_lwp (thread);
1337 /* We avoid killing the first thread here, because of a Linux kernel (at
1338 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1339 the children get a chance to be reaped, it will remain a zombie
1340 forever. */
1342 if (lwpid_of (thread) == pid)
1344 threads_debug_printf ("is last of process %s",
1345 target_pid_to_str (thread->id).c_str ());
1346 return;
1349 kill_wait_lwp (lwp);
1353 linux_process_target::kill (process_info *process)
1355 int pid = process->pid;
1357 /* If we're killing a running inferior, make sure it is stopped
1358 first, as PTRACE_KILL will not work otherwise. */
1359 stop_all_lwps (0, NULL);
1361 for_each_thread (pid, [&] (thread_info *thread)
1363 kill_one_lwp_callback (thread, pid);
1366 /* See the comment in linux_kill_one_lwp. We did not kill the first
1367 thread in the list, so do so now. */
1368 lwp_info *lwp = find_lwp_pid (ptid_t (pid));
1370 if (lwp == NULL)
1371 threads_debug_printf ("cannot find lwp for pid: %d", pid);
1372 else
1373 kill_wait_lwp (lwp);
1375 mourn (process);
1377 /* Since we presently can only stop all lwps of all processes, we
1378 need to unstop lwps of other processes. */
1379 unstop_all_lwps (0, NULL);
1380 return 0;
1383 /* Get pending signal of THREAD, for detaching purposes. This is the
1384 signal the thread last stopped for, which we need to deliver to the
1385 thread when detaching, otherwise, it'd be suppressed/lost. */
1387 static int
1388 get_detach_signal (struct thread_info *thread)
1390 client_state &cs = get_client_state ();
1391 enum gdb_signal signo = GDB_SIGNAL_0;
1392 int status;
1393 struct lwp_info *lp = get_thread_lwp (thread);
1395 if (lp->status_pending_p)
1396 status = lp->status_pending;
1397 else
1399 /* If the thread had been suspended by gdbserver, and it stopped
1400 cleanly, then it'll have stopped with SIGSTOP. But we don't
1401 want to deliver that SIGSTOP. */
1402 if (thread->last_status.kind () != TARGET_WAITKIND_STOPPED
1403 || thread->last_status.sig () == GDB_SIGNAL_0)
1404 return 0;
1406 /* Otherwise, we may need to deliver the signal we
1407 intercepted. */
1408 status = lp->last_status;
1411 if (!WIFSTOPPED (status))
1413 threads_debug_printf ("lwp %s hasn't stopped: no pending signal",
1414 target_pid_to_str (ptid_of (thread)).c_str ());
1415 return 0;
1418 /* Extended wait statuses aren't real SIGTRAPs. */
1419 if (WSTOPSIG (status) == SIGTRAP && linux_is_extended_waitstatus (status))
1421 threads_debug_printf ("lwp %s had stopped with extended "
1422 "status: no pending signal",
1423 target_pid_to_str (ptid_of (thread)).c_str ());
1424 return 0;
1427 signo = gdb_signal_from_host (WSTOPSIG (status));
1429 if (cs.program_signals_p && !cs.program_signals[signo])
1431 threads_debug_printf ("lwp %s had signal %s, but it is in nopass state",
1432 target_pid_to_str (ptid_of (thread)).c_str (),
1433 gdb_signal_to_string (signo));
1434 return 0;
1436 else if (!cs.program_signals_p
1437 /* If we have no way to know which signals GDB does not
1438 want to have passed to the program, assume
1439 SIGTRAP/SIGINT, which is GDB's default. */
1440 && (signo == GDB_SIGNAL_TRAP || signo == GDB_SIGNAL_INT))
1442 threads_debug_printf ("lwp %s had signal %s, "
1443 "but we don't know if we should pass it. "
1444 "Default to not.",
1445 target_pid_to_str (ptid_of (thread)).c_str (),
1446 gdb_signal_to_string (signo));
1447 return 0;
1449 else
1451 threads_debug_printf ("lwp %s has pending signal %s: delivering it",
1452 target_pid_to_str (ptid_of (thread)).c_str (),
1453 gdb_signal_to_string (signo));
1455 return WSTOPSIG (status);
1459 void
1460 linux_process_target::detach_one_lwp (lwp_info *lwp)
1462 struct thread_info *thread = get_lwp_thread (lwp);
1463 int sig;
1464 int lwpid;
1466 /* If there is a pending SIGSTOP, get rid of it. */
1467 if (lwp->stop_expected)
1469 threads_debug_printf ("Sending SIGCONT to %s",
1470 target_pid_to_str (ptid_of (thread)).c_str ());
1472 kill_lwp (lwpid_of (thread), SIGCONT);
1473 lwp->stop_expected = 0;
1476 /* Pass on any pending signal for this thread. */
1477 sig = get_detach_signal (thread);
1479 /* Preparing to resume may try to write registers, and fail if the
1480 lwp is zombie. If that happens, ignore the error. We'll handle
1481 it below, when detach fails with ESRCH. */
1484 /* Flush any pending changes to the process's registers. */
1485 regcache_invalidate_thread (thread);
1487 /* Finally, let it resume. */
1488 low_prepare_to_resume (lwp);
1490 catch (const gdb_exception_error &ex)
1492 if (!check_ptrace_stopped_lwp_gone (lwp))
1493 throw;
1496 lwpid = lwpid_of (thread);
1497 if (ptrace (PTRACE_DETACH, lwpid, (PTRACE_TYPE_ARG3) 0,
1498 (PTRACE_TYPE_ARG4) (long) sig) < 0)
1500 int save_errno = errno;
1502 /* We know the thread exists, so ESRCH must mean the lwp is
1503 zombie. This can happen if one of the already-detached
1504 threads exits the whole thread group. In that case we're
1505 still attached, and must reap the lwp. */
1506 if (save_errno == ESRCH)
1508 int ret, status;
1510 ret = my_waitpid (lwpid, &status, __WALL);
1511 if (ret == -1)
1513 warning (_("Couldn't reap LWP %d while detaching: %s"),
1514 lwpid, safe_strerror (errno));
1516 else if (!WIFEXITED (status) && !WIFSIGNALED (status))
1518 warning (_("Reaping LWP %d while detaching "
1519 "returned unexpected status 0x%x"),
1520 lwpid, status);
1523 else
1525 error (_("Can't detach %s: %s"),
1526 target_pid_to_str (ptid_of (thread)).c_str (),
1527 safe_strerror (save_errno));
1530 else
1531 threads_debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)",
1532 target_pid_to_str (ptid_of (thread)).c_str (),
1533 strsignal (sig));
1535 delete_lwp (lwp);
1539 linux_process_target::detach (process_info *process)
1541 struct lwp_info *main_lwp;
1543 /* As there's a step over already in progress, let it finish first,
1544 otherwise nesting a stabilize_threads operation on top gets real
1545 messy. */
1546 complete_ongoing_step_over ();
1548 /* Stop all threads before detaching. First, ptrace requires that
1549 the thread is stopped to successfully detach. Second, thread_db
1550 may need to uninstall thread event breakpoints from memory, which
1551 only works with a stopped process anyway. */
1552 stop_all_lwps (0, NULL);
1554 #ifdef USE_THREAD_DB
1555 thread_db_detach (process);
1556 #endif
1558 /* Stabilize threads (move out of jump pads). */
1559 target_stabilize_threads ();
1561 /* Detach from the clone lwps first. If the thread group exits just
1562 while we're detaching, we must reap the clone lwps before we're
1563 able to reap the leader. */
1564 for_each_thread (process->pid, [this] (thread_info *thread)
1566 /* We don't actually detach from the thread group leader just yet.
1567 If the thread group exits, we must reap the zombie clone lwps
1568 before we're able to reap the leader. */
1569 if (thread->id.pid () == thread->id.lwp ())
1570 return;
1572 lwp_info *lwp = get_thread_lwp (thread);
1573 detach_one_lwp (lwp);
1576 main_lwp = find_lwp_pid (ptid_t (process->pid));
1577 gdb_assert (main_lwp != nullptr);
1578 detach_one_lwp (main_lwp);
1580 mourn (process);
1582 /* Since we presently can only stop all lwps of all processes, we
1583 need to unstop lwps of other processes. */
1584 unstop_all_lwps (0, NULL);
1585 return 0;
1588 /* Remove all LWPs that belong to process PROC from the lwp list. */
1590 void
1591 linux_process_target::mourn (process_info *process)
1593 #ifdef USE_THREAD_DB
1594 thread_db_mourn (process);
1595 #endif
1597 for_each_thread (process->pid, [this] (thread_info *thread)
1599 delete_lwp (get_thread_lwp (thread));
1602 this->remove_linux_process (process);
1605 void
1606 linux_process_target::join (int pid)
1608 int status, ret;
1610 do {
1611 ret = my_waitpid (pid, &status, 0);
1612 if (WIFEXITED (status) || WIFSIGNALED (status))
1613 break;
1614 } while (ret != -1 || errno != ECHILD);
1617 /* Return true if the given thread is still alive. */
1619 bool
1620 linux_process_target::thread_alive (ptid_t ptid)
1622 struct lwp_info *lwp = find_lwp_pid (ptid);
1624 /* We assume we always know if a thread exits. If a whole process
1625 exited but we still haven't been able to report it to GDB, we'll
1626 hold on to the last lwp of the dead process. */
1627 if (lwp != NULL)
1628 return !lwp_is_marked_dead (lwp);
1629 else
1630 return 0;
1633 bool
1634 linux_process_target::thread_still_has_status_pending (thread_info *thread)
1636 struct lwp_info *lp = get_thread_lwp (thread);
1638 if (!lp->status_pending_p)
1639 return 0;
1641 if (thread->last_resume_kind != resume_stop
1642 && (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
1643 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT))
1645 CORE_ADDR pc;
1646 int discard = 0;
1648 gdb_assert (lp->last_status != 0);
1650 pc = get_pc (lp);
1652 scoped_restore_current_thread restore_thread;
1653 switch_to_thread (thread);
1655 if (pc != lp->stop_pc)
1657 threads_debug_printf ("PC of %ld changed",
1658 lwpid_of (thread));
1659 discard = 1;
1662 #if !USE_SIGTRAP_SIGINFO
1663 else if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
1664 && !low_breakpoint_at (pc))
1666 threads_debug_printf ("previous SW breakpoint of %ld gone",
1667 lwpid_of (thread));
1668 discard = 1;
1670 else if (lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT
1671 && !hardware_breakpoint_inserted_here (pc))
1673 threads_debug_printf ("previous HW breakpoint of %ld gone",
1674 lwpid_of (thread));
1675 discard = 1;
1677 #endif
1679 if (discard)
1681 threads_debug_printf ("discarding pending breakpoint status");
1682 lp->status_pending_p = 0;
1683 return 0;
1687 return 1;
1690 /* Returns true if LWP is resumed from the client's perspective. */
1692 static int
1693 lwp_resumed (struct lwp_info *lwp)
1695 struct thread_info *thread = get_lwp_thread (lwp);
1697 if (thread->last_resume_kind != resume_stop)
1698 return 1;
1700 /* Did gdb send us a `vCont;t', but we haven't reported the
1701 corresponding stop to gdb yet? If so, the thread is still
1702 resumed/running from gdb's perspective. */
1703 if (thread->last_resume_kind == resume_stop
1704 && thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
1705 return 1;
1707 return 0;
1710 bool
1711 linux_process_target::status_pending_p_callback (thread_info *thread,
1712 ptid_t ptid)
1714 struct lwp_info *lp = get_thread_lwp (thread);
1716 /* Check if we're only interested in events from a specific process
1717 or a specific LWP. */
1718 if (!thread->id.matches (ptid))
1719 return 0;
1721 if (!lwp_resumed (lp))
1722 return 0;
1724 if (lp->status_pending_p
1725 && !thread_still_has_status_pending (thread))
1727 resume_one_lwp (lp, lp->stepping, GDB_SIGNAL_0, NULL);
1728 return 0;
1731 return lp->status_pending_p;
1734 struct lwp_info *
1735 find_lwp_pid (ptid_t ptid)
1737 long lwp = ptid.lwp () != 0 ? ptid.lwp () : ptid.pid ();
1738 thread_info *thread = find_thread ([lwp] (thread_info *thr_arg)
1740 return thr_arg->id.lwp () == lwp;
1743 if (thread == NULL)
1744 return NULL;
1746 return get_thread_lwp (thread);
1749 /* Return the number of known LWPs in the tgid given by PID. */
1751 static int
1752 num_lwps (int pid)
1754 int count = 0;
1756 for_each_thread (pid, [&] (thread_info *thread)
1758 count++;
1761 return count;
1764 /* See nat/linux-nat.h. */
1766 struct lwp_info *
1767 iterate_over_lwps (ptid_t filter,
1768 gdb::function_view<iterate_over_lwps_ftype> callback)
1770 thread_info *thread = find_thread (filter, [&] (thread_info *thr_arg)
1772 lwp_info *lwp = get_thread_lwp (thr_arg);
1774 return callback (lwp);
1777 if (thread == NULL)
1778 return NULL;
1780 return get_thread_lwp (thread);
1783 void
1784 linux_process_target::check_zombie_leaders ()
1786 for_each_process ([this] (process_info *proc)
1788 pid_t leader_pid = pid_of (proc);
1789 lwp_info *leader_lp = find_lwp_pid (ptid_t (leader_pid));
1791 threads_debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1792 "num_lwps=%d, zombie=%d",
1793 leader_pid, leader_lp!= NULL, num_lwps (leader_pid),
1794 linux_proc_pid_is_zombie (leader_pid));
1796 if (leader_lp != NULL && !leader_lp->stopped
1797 /* Check if there are other threads in the group, as we may
1798 have raced with the inferior simply exiting. Note this
1799 isn't a watertight check. If the inferior is
1800 multi-threaded and is exiting, it may be we see the
1801 leader as zombie before we reap all the non-leader
1802 threads. See comments below. */
1803 && !last_thread_of_process_p (leader_pid)
1804 && linux_proc_pid_is_zombie (leader_pid))
1806 /* A zombie leader in a multi-threaded program can mean one
1807 of three things:
1809 #1 - Only the leader exited, not the whole program, e.g.,
1810 with pthread_exit. Since we can't reap the leader's exit
1811 status until all other threads are gone and reaped too,
1812 we want to delete the zombie leader right away, as it
1813 can't be debugged, we can't read its registers, etc.
1814 This is the main reason we check for zombie leaders
1815 disappearing.
1817 #2 - The whole thread-group/process exited (a group exit,
1818 via e.g. exit(3), and there is (or will be shortly) an
1819 exit reported for each thread in the process, and then
1820 finally an exit for the leader once the non-leaders are
1821 reaped.
1823 #3 - There are 3 or more threads in the group, and a
1824 thread other than the leader exec'd. See comments on
1825 exec events at the top of the file.
1827 Ideally we would never delete the leader for case #2.
1828 Instead, we want to collect the exit status of each
1829 non-leader thread, and then finally collect the exit
1830 status of the leader as normal and use its exit code as
1831 whole-process exit code. Unfortunately, there's no
1832 race-free way to distinguish cases #1 and #2. We can't
1833 assume the exit events for the non-leaders threads are
1834 already pending in the kernel, nor can we assume the
1835 non-leader threads are in zombie state already. Between
1836 the leader becoming zombie and the non-leaders exiting
1837 and becoming zombie themselves, there's a small time
1838 window, so such a check would be racy. Temporarily
1839 pausing all threads and checking to see if all threads
1840 exit or not before re-resuming them would work in the
1841 case that all threads are running right now, but it
1842 wouldn't work if some thread is currently already
1843 ptrace-stopped, e.g., due to scheduler-locking.
1845 So what we do is we delete the leader anyhow, and then
1846 later on when we see its exit status, we re-add it back.
1847 We also make sure that we only report a whole-process
1848 exit when we see the leader exiting, as opposed to when
1849 the last LWP in the LWP list exits, which can be a
1850 non-leader if we deleted the leader here. */
1851 threads_debug_printf ("Thread group leader %d zombie "
1852 "(it exited, or another thread execd), "
1853 "deleting it.",
1854 leader_pid);
1855 delete_lwp (leader_lp);
1860 /* Callback for `find_thread'. Returns the first LWP that is not
1861 stopped. */
1863 static bool
1864 not_stopped_callback (thread_info *thread, ptid_t filter)
1866 if (!thread->id.matches (filter))
1867 return false;
1869 lwp_info *lwp = get_thread_lwp (thread);
1871 return !lwp->stopped;
1874 /* Increment LWP's suspend count. */
1876 static void
1877 lwp_suspended_inc (struct lwp_info *lwp)
1879 lwp->suspended++;
1881 if (lwp->suspended > 4)
1882 threads_debug_printf
1883 ("LWP %ld has a suspiciously high suspend count, suspended=%d",
1884 lwpid_of (get_lwp_thread (lwp)), lwp->suspended);
1887 /* Decrement LWP's suspend count. */
1889 static void
1890 lwp_suspended_decr (struct lwp_info *lwp)
1892 lwp->suspended--;
1894 if (lwp->suspended < 0)
1896 struct thread_info *thread = get_lwp_thread (lwp);
1898 internal_error ("unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread),
1899 lwp->suspended);
1903 /* This function should only be called if the LWP got a SIGTRAP.
1905 Handle any tracepoint steps or hits. Return true if a tracepoint
1906 event was handled, 0 otherwise. */
1908 static int
1909 handle_tracepoints (struct lwp_info *lwp)
1911 struct thread_info *tinfo = get_lwp_thread (lwp);
1912 int tpoint_related_event = 0;
1914 gdb_assert (lwp->suspended == 0);
1916 /* If this tracepoint hit causes a tracing stop, we'll immediately
1917 uninsert tracepoints. To do this, we temporarily pause all
1918 threads, unpatch away, and then unpause threads. We need to make
1919 sure the unpausing doesn't resume LWP too. */
1920 lwp_suspended_inc (lwp);
1922 /* And we need to be sure that any all-threads-stopping doesn't try
1923 to move threads out of the jump pads, as it could deadlock the
1924 inferior (LWP could be in the jump pad, maybe even holding the
1925 lock.) */
1927 /* Do any necessary step collect actions. */
1928 tpoint_related_event |= tracepoint_finished_step (tinfo, lwp->stop_pc);
1930 tpoint_related_event |= handle_tracepoint_bkpts (tinfo, lwp->stop_pc);
1932 /* See if we just hit a tracepoint and do its main collect
1933 actions. */
1934 tpoint_related_event |= tracepoint_was_hit (tinfo, lwp->stop_pc);
1936 lwp_suspended_decr (lwp);
1938 gdb_assert (lwp->suspended == 0);
1939 gdb_assert (!stabilizing_threads
1940 || (lwp->collecting_fast_tracepoint
1941 != fast_tpoint_collect_result::not_collecting));
1943 if (tpoint_related_event)
1945 threads_debug_printf ("got a tracepoint event");
1946 return 1;
1949 return 0;
1952 fast_tpoint_collect_result
1953 linux_process_target::linux_fast_tracepoint_collecting
1954 (lwp_info *lwp, fast_tpoint_collect_status *status)
1956 CORE_ADDR thread_area;
1957 struct thread_info *thread = get_lwp_thread (lwp);
1959 /* Get the thread area address. This is used to recognize which
1960 thread is which when tracing with the in-process agent library.
1961 We don't read anything from the address, and treat it as opaque;
1962 it's the address itself that we assume is unique per-thread. */
1963 if (low_get_thread_area (lwpid_of (thread), &thread_area) == -1)
1964 return fast_tpoint_collect_result::not_collecting;
1966 return fast_tracepoint_collecting (thread_area, lwp->stop_pc, status);
1970 linux_process_target::low_get_thread_area (int lwpid, CORE_ADDR *addrp)
1972 return -1;
1975 bool
1976 linux_process_target::maybe_move_out_of_jump_pad (lwp_info *lwp, int *wstat)
1978 scoped_restore_current_thread restore_thread;
1979 switch_to_thread (get_lwp_thread (lwp));
1981 if ((wstat == NULL
1982 || (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) != SIGTRAP))
1983 && supports_fast_tracepoints ()
1984 && agent_loaded_p ())
1986 struct fast_tpoint_collect_status status;
1988 threads_debug_printf
1989 ("Checking whether LWP %ld needs to move out of the jump pad.",
1990 lwpid_of (current_thread));
1992 fast_tpoint_collect_result r
1993 = linux_fast_tracepoint_collecting (lwp, &status);
1995 if (wstat == NULL
1996 || (WSTOPSIG (*wstat) != SIGILL
1997 && WSTOPSIG (*wstat) != SIGFPE
1998 && WSTOPSIG (*wstat) != SIGSEGV
1999 && WSTOPSIG (*wstat) != SIGBUS))
2001 lwp->collecting_fast_tracepoint = r;
2003 if (r != fast_tpoint_collect_result::not_collecting)
2005 if (r == fast_tpoint_collect_result::before_insn
2006 && lwp->exit_jump_pad_bkpt == NULL)
2008 /* Haven't executed the original instruction yet.
2009 Set breakpoint there, and wait till it's hit,
2010 then single-step until exiting the jump pad. */
2011 lwp->exit_jump_pad_bkpt
2012 = set_breakpoint_at (status.adjusted_insn_addr, NULL);
2015 threads_debug_printf
2016 ("Checking whether LWP %ld needs to move out of the jump pad..."
2017 " it does", lwpid_of (current_thread));
2019 return true;
2022 else
2024 /* If we get a synchronous signal while collecting, *and*
2025 while executing the (relocated) original instruction,
2026 reset the PC to point at the tpoint address, before
2027 reporting to GDB. Otherwise, it's an IPA lib bug: just
2028 report the signal to GDB, and pray for the best. */
2030 lwp->collecting_fast_tracepoint
2031 = fast_tpoint_collect_result::not_collecting;
2033 if (r != fast_tpoint_collect_result::not_collecting
2034 && (status.adjusted_insn_addr <= lwp->stop_pc
2035 && lwp->stop_pc < status.adjusted_insn_addr_end))
2037 siginfo_t info;
2038 struct regcache *regcache;
2040 /* The si_addr on a few signals references the address
2041 of the faulting instruction. Adjust that as
2042 well. */
2043 if ((WSTOPSIG (*wstat) == SIGILL
2044 || WSTOPSIG (*wstat) == SIGFPE
2045 || WSTOPSIG (*wstat) == SIGBUS
2046 || WSTOPSIG (*wstat) == SIGSEGV)
2047 && ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
2048 (PTRACE_TYPE_ARG3) 0, &info) == 0
2049 /* Final check just to make sure we don't clobber
2050 the siginfo of non-kernel-sent signals. */
2051 && (uintptr_t) info.si_addr == lwp->stop_pc)
2053 info.si_addr = (void *) (uintptr_t) status.tpoint_addr;
2054 ptrace (PTRACE_SETSIGINFO, lwpid_of (current_thread),
2055 (PTRACE_TYPE_ARG3) 0, &info);
2058 regcache = get_thread_regcache (current_thread, 1);
2059 low_set_pc (regcache, status.tpoint_addr);
2060 lwp->stop_pc = status.tpoint_addr;
2062 /* Cancel any fast tracepoint lock this thread was
2063 holding. */
2064 force_unlock_trace_buffer ();
2067 if (lwp->exit_jump_pad_bkpt != NULL)
2069 threads_debug_printf
2070 ("Cancelling fast exit-jump-pad: removing bkpt."
2071 "stopping all threads momentarily.");
2073 stop_all_lwps (1, lwp);
2075 delete_breakpoint (lwp->exit_jump_pad_bkpt);
2076 lwp->exit_jump_pad_bkpt = NULL;
2078 unstop_all_lwps (1, lwp);
2080 gdb_assert (lwp->suspended >= 0);
2085 threads_debug_printf
2086 ("Checking whether LWP %ld needs to move out of the jump pad... no",
2087 lwpid_of (current_thread));
2089 return false;
2092 /* Enqueue one signal in the "signals to report later when out of the
2093 jump pad" list. */
2095 static void
2096 enqueue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
2098 struct thread_info *thread = get_lwp_thread (lwp);
2100 threads_debug_printf ("Deferring signal %d for LWP %ld.",
2101 WSTOPSIG (*wstat), lwpid_of (thread));
2103 if (debug_threads)
2105 for (const auto &sig : lwp->pending_signals_to_report)
2106 threads_debug_printf (" Already queued %d", sig.signal);
2108 threads_debug_printf (" (no more currently queued signals)");
2111 /* Don't enqueue non-RT signals if they are already in the deferred
2112 queue. (SIGSTOP being the easiest signal to see ending up here
2113 twice) */
2114 if (WSTOPSIG (*wstat) < __SIGRTMIN)
2116 for (const auto &sig : lwp->pending_signals_to_report)
2118 if (sig.signal == WSTOPSIG (*wstat))
2120 threads_debug_printf
2121 ("Not requeuing already queued non-RT signal %d for LWP %ld",
2122 sig.signal, lwpid_of (thread));
2123 return;
2128 lwp->pending_signals_to_report.emplace_back (WSTOPSIG (*wstat));
2130 ptrace (PTRACE_GETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
2131 &lwp->pending_signals_to_report.back ().info);
2134 /* Dequeue one signal from the "signals to report later when out of
2135 the jump pad" list. */
2137 static int
2138 dequeue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
2140 struct thread_info *thread = get_lwp_thread (lwp);
2142 if (!lwp->pending_signals_to_report.empty ())
2144 const pending_signal &p_sig = lwp->pending_signals_to_report.front ();
2146 *wstat = W_STOPCODE (p_sig.signal);
2147 if (p_sig.info.si_signo != 0)
2148 ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
2149 &p_sig.info);
2151 lwp->pending_signals_to_report.pop_front ();
2153 threads_debug_printf ("Reporting deferred signal %d for LWP %ld.",
2154 WSTOPSIG (*wstat), lwpid_of (thread));
2156 if (debug_threads)
2158 for (const auto &sig : lwp->pending_signals_to_report)
2159 threads_debug_printf (" Still queued %d", sig.signal);
2161 threads_debug_printf (" (no more queued signals)");
2164 return 1;
2167 return 0;
2170 bool
2171 linux_process_target::check_stopped_by_watchpoint (lwp_info *child)
2173 scoped_restore_current_thread restore_thread;
2174 switch_to_thread (get_lwp_thread (child));
2176 if (low_stopped_by_watchpoint ())
2178 child->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
2179 child->stopped_data_address = low_stopped_data_address ();
2182 return child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2185 bool
2186 linux_process_target::low_stopped_by_watchpoint ()
2188 return false;
2191 CORE_ADDR
2192 linux_process_target::low_stopped_data_address ()
2194 return 0;
2197 /* Return the ptrace options that we want to try to enable. */
2199 static int
2200 linux_low_ptrace_options (int attached)
2202 client_state &cs = get_client_state ();
2203 int options = 0;
2205 if (!attached)
2206 options |= PTRACE_O_EXITKILL;
2208 if (cs.report_fork_events)
2209 options |= PTRACE_O_TRACEFORK;
2211 if (cs.report_vfork_events)
2212 options |= (PTRACE_O_TRACEVFORK | PTRACE_O_TRACEVFORKDONE);
2214 if (cs.report_exec_events)
2215 options |= PTRACE_O_TRACEEXEC;
2217 options |= PTRACE_O_TRACESYSGOOD;
2219 return options;
2222 void
2223 linux_process_target::filter_event (int lwpid, int wstat)
2225 client_state &cs = get_client_state ();
2226 struct lwp_info *child;
2227 struct thread_info *thread;
2228 int have_stop_pc = 0;
2230 child = find_lwp_pid (ptid_t (lwpid));
2232 /* Check for events reported by anything not in our LWP list. */
2233 if (child == nullptr)
2235 if (WIFSTOPPED (wstat))
2237 if (WSTOPSIG (wstat) == SIGTRAP
2238 && linux_ptrace_get_extended_event (wstat) == PTRACE_EVENT_EXEC)
2240 /* A non-leader thread exec'ed after we've seen the
2241 leader zombie, and removed it from our lists (in
2242 check_zombie_leaders). The non-leader thread changes
2243 its tid to the tgid. */
2244 threads_debug_printf
2245 ("Re-adding thread group leader LWP %d after exec.",
2246 lwpid);
2248 child = add_lwp (ptid_t (lwpid, lwpid));
2249 child->stopped = 1;
2250 switch_to_thread (child->thread);
2252 else
2254 /* A process we are controlling has forked and the new
2255 child's stop was reported to us by the kernel. Save
2256 its PID and go back to waiting for the fork event to
2257 be reported - the stopped process might be returned
2258 from waitpid before or after the fork event is. */
2259 threads_debug_printf
2260 ("Saving LWP %d status %s in stopped_pids list",
2261 lwpid, status_to_str (wstat).c_str ());
2262 add_to_pid_list (&stopped_pids, lwpid, wstat);
2265 else
2267 /* Don't report an event for the exit of an LWP not in our
2268 list, i.e. not part of any inferior we're debugging.
2269 This can happen if we detach from a program we originally
2270 forked and then it exits. However, note that we may have
2271 earlier deleted a leader of an inferior we're debugging,
2272 in check_zombie_leaders. Re-add it back here if so. */
2273 find_process ([&] (process_info *proc)
2275 if (proc->pid == lwpid)
2277 threads_debug_printf
2278 ("Re-adding thread group leader LWP %d after exit.",
2279 lwpid);
2281 child = add_lwp (ptid_t (lwpid, lwpid));
2282 return true;
2284 return false;
2288 if (child == nullptr)
2289 return;
2292 thread = get_lwp_thread (child);
2294 child->stopped = 1;
2296 child->last_status = wstat;
2298 /* Check if the thread has exited. */
2299 if ((WIFEXITED (wstat) || WIFSIGNALED (wstat)))
2301 threads_debug_printf ("%d exited", lwpid);
2303 if (finish_step_over (child))
2305 /* Unsuspend all other LWPs, and set them back running again. */
2306 unsuspend_all_lwps (child);
2309 /* If this is not the leader LWP, then the exit signal was not
2310 the end of the debugged application and should be ignored,
2311 unless GDB wants to hear about thread exits. */
2312 if (cs.report_thread_events || is_leader (thread))
2314 /* Since events are serialized to GDB core, and we can't
2315 report this one right now. Leave the status pending for
2316 the next time we're able to report it. */
2317 mark_lwp_dead (child, wstat);
2318 return;
2320 else
2322 delete_lwp (child);
2323 return;
2327 gdb_assert (WIFSTOPPED (wstat));
2329 if (WIFSTOPPED (wstat))
2331 struct process_info *proc;
2333 /* Architecture-specific setup after inferior is running. */
2334 proc = find_process_pid (pid_of (thread));
2335 if (proc->tdesc == NULL)
2337 if (proc->attached)
2339 /* This needs to happen after we have attached to the
2340 inferior and it is stopped for the first time, but
2341 before we access any inferior registers. */
2342 arch_setup_thread (thread);
2344 else
2346 /* The process is started, but GDBserver will do
2347 architecture-specific setup after the program stops at
2348 the first instruction. */
2349 child->status_pending_p = 1;
2350 child->status_pending = wstat;
2351 return;
2356 if (WIFSTOPPED (wstat) && child->must_set_ptrace_flags)
2358 struct process_info *proc = find_process_pid (pid_of (thread));
2359 int options = linux_low_ptrace_options (proc->attached);
2361 linux_enable_event_reporting (lwpid, options);
2362 child->must_set_ptrace_flags = 0;
2365 /* Always update syscall_state, even if it will be filtered later. */
2366 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SYSCALL_SIGTRAP)
2368 child->syscall_state
2369 = (child->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
2370 ? TARGET_WAITKIND_SYSCALL_RETURN
2371 : TARGET_WAITKIND_SYSCALL_ENTRY);
2373 else
2375 /* Almost all other ptrace-stops are known to be outside of system
2376 calls, with further exceptions in handle_extended_wait. */
2377 child->syscall_state = TARGET_WAITKIND_IGNORE;
2380 /* Be careful to not overwrite stop_pc until save_stop_reason is
2381 called. */
2382 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGTRAP
2383 && linux_is_extended_waitstatus (wstat))
2385 child->stop_pc = get_pc (child);
2386 if (handle_extended_wait (&child, wstat))
2388 /* The event has been handled, so just return without
2389 reporting it. */
2390 return;
2394 if (linux_wstatus_maybe_breakpoint (wstat))
2396 if (save_stop_reason (child))
2397 have_stop_pc = 1;
2400 if (!have_stop_pc)
2401 child->stop_pc = get_pc (child);
2403 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGSTOP
2404 && child->stop_expected)
2406 threads_debug_printf ("Expected stop.");
2408 child->stop_expected = 0;
2410 if (thread->last_resume_kind == resume_stop)
2412 /* We want to report the stop to the core. Treat the
2413 SIGSTOP as a normal event. */
2414 threads_debug_printf ("resume_stop SIGSTOP caught for %s.",
2415 target_pid_to_str (ptid_of (thread)).c_str ());
2417 else if (stopping_threads != NOT_STOPPING_THREADS)
2419 /* Stopping threads. We don't want this SIGSTOP to end up
2420 pending. */
2421 threads_debug_printf ("SIGSTOP caught for %s while stopping threads.",
2422 target_pid_to_str (ptid_of (thread)).c_str ());
2423 return;
2425 else
2427 /* This is a delayed SIGSTOP. Filter out the event. */
2428 threads_debug_printf ("%s %s, 0, 0 (discard delayed SIGSTOP)",
2429 child->stepping ? "step" : "continue",
2430 target_pid_to_str (ptid_of (thread)).c_str ());
2432 resume_one_lwp (child, child->stepping, 0, NULL);
2433 return;
2437 child->status_pending_p = 1;
2438 child->status_pending = wstat;
2439 return;
2442 bool
2443 linux_process_target::maybe_hw_step (thread_info *thread)
2445 if (supports_hardware_single_step ())
2446 return true;
2447 else
2449 /* GDBserver must insert single-step breakpoint for software
2450 single step. */
2451 gdb_assert (has_single_step_breakpoints (thread));
2452 return false;
2456 void
2457 linux_process_target::resume_stopped_resumed_lwps (thread_info *thread)
2459 struct lwp_info *lp = get_thread_lwp (thread);
2461 if (lp->stopped
2462 && !lp->suspended
2463 && !lp->status_pending_p
2464 && thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
2466 int step = 0;
2468 if (thread->last_resume_kind == resume_step)
2470 if (supports_software_single_step ())
2471 install_software_single_step_breakpoints (lp);
2473 step = maybe_hw_step (thread);
2476 threads_debug_printf ("resuming stopped-resumed LWP %s at %s: step=%d",
2477 target_pid_to_str (ptid_of (thread)).c_str (),
2478 paddress (lp->stop_pc), step);
2480 resume_one_lwp (lp, step, GDB_SIGNAL_0, NULL);
2485 linux_process_target::wait_for_event_filtered (ptid_t wait_ptid,
2486 ptid_t filter_ptid,
2487 int *wstatp, int options)
2489 struct thread_info *event_thread;
2490 struct lwp_info *event_child, *requested_child;
2491 sigset_t block_mask, prev_mask;
2493 retry:
2494 /* N.B. event_thread points to the thread_info struct that contains
2495 event_child. Keep them in sync. */
2496 event_thread = NULL;
2497 event_child = NULL;
2498 requested_child = NULL;
2500 /* Check for a lwp with a pending status. */
2502 if (filter_ptid == minus_one_ptid || filter_ptid.is_pid ())
2504 event_thread = find_thread_in_random ([&] (thread_info *thread)
2506 return status_pending_p_callback (thread, filter_ptid);
2509 if (event_thread != NULL)
2511 event_child = get_thread_lwp (event_thread);
2512 threads_debug_printf ("Got a pending child %ld", lwpid_of (event_thread));
2515 else if (filter_ptid != null_ptid)
2517 requested_child = find_lwp_pid (filter_ptid);
2518 gdb_assert (requested_child != nullptr);
2520 if (stopping_threads == NOT_STOPPING_THREADS
2521 && requested_child->status_pending_p
2522 && (requested_child->collecting_fast_tracepoint
2523 != fast_tpoint_collect_result::not_collecting))
2525 enqueue_one_deferred_signal (requested_child,
2526 &requested_child->status_pending);
2527 requested_child->status_pending_p = 0;
2528 requested_child->status_pending = 0;
2529 resume_one_lwp (requested_child, 0, 0, NULL);
2532 if (requested_child->suspended
2533 && requested_child->status_pending_p)
2535 internal_error ("requesting an event out of a"
2536 " suspended child?");
2539 if (requested_child->status_pending_p)
2541 event_child = requested_child;
2542 event_thread = get_lwp_thread (event_child);
2546 if (event_child != NULL)
2548 threads_debug_printf ("Got an event from pending child %ld (%04x)",
2549 lwpid_of (event_thread),
2550 event_child->status_pending);
2552 *wstatp = event_child->status_pending;
2553 event_child->status_pending_p = 0;
2554 event_child->status_pending = 0;
2555 switch_to_thread (event_thread);
2556 return lwpid_of (event_thread);
2559 /* But if we don't find a pending event, we'll have to wait.
2561 We only enter this loop if no process has a pending wait status.
2562 Thus any action taken in response to a wait status inside this
2563 loop is responding as soon as we detect the status, not after any
2564 pending events. */
2566 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2567 all signals while here. */
2568 sigfillset (&block_mask);
2569 gdb_sigmask (SIG_BLOCK, &block_mask, &prev_mask);
2571 /* Always pull all events out of the kernel. We'll randomly select
2572 an event LWP out of all that have events, to prevent
2573 starvation. */
2574 while (event_child == NULL)
2576 pid_t ret = 0;
2578 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2579 quirks:
2581 - If the thread group leader exits while other threads in the
2582 thread group still exist, waitpid(TGID, ...) hangs. That
2583 waitpid won't return an exit status until the other threads
2584 in the group are reaped.
2586 - When a non-leader thread execs, that thread just vanishes
2587 without reporting an exit (so we'd hang if we waited for it
2588 explicitly in that case). The exec event is reported to
2589 the TGID pid. */
2590 errno = 0;
2591 ret = my_waitpid (-1, wstatp, options | WNOHANG);
2593 threads_debug_printf ("waitpid(-1, ...) returned %d, %s",
2594 ret, errno ? safe_strerror (errno) : "ERRNO-OK");
2596 if (ret > 0)
2598 threads_debug_printf ("waitpid %ld received %s",
2599 (long) ret, status_to_str (*wstatp).c_str ());
2601 /* Filter all events. IOW, leave all events pending. We'll
2602 randomly select an event LWP out of all that have events
2603 below. */
2604 filter_event (ret, *wstatp);
2605 /* Retry until nothing comes out of waitpid. A single
2606 SIGCHLD can indicate more than one child stopped. */
2607 continue;
2610 /* Now that we've pulled all events out of the kernel, resume
2611 LWPs that don't have an interesting event to report. */
2612 if (stopping_threads == NOT_STOPPING_THREADS)
2613 for_each_thread ([this] (thread_info *thread)
2615 resume_stopped_resumed_lwps (thread);
2618 /* ... and find an LWP with a status to report to the core, if
2619 any. */
2620 event_thread = find_thread_in_random ([&] (thread_info *thread)
2622 return status_pending_p_callback (thread, filter_ptid);
2625 if (event_thread != NULL)
2627 event_child = get_thread_lwp (event_thread);
2628 *wstatp = event_child->status_pending;
2629 event_child->status_pending_p = 0;
2630 event_child->status_pending = 0;
2631 break;
2634 /* Check for zombie thread group leaders. Those can't be reaped
2635 until all other threads in the thread group are. */
2636 check_zombie_leaders ();
2638 auto not_stopped = [&] (thread_info *thread)
2640 return not_stopped_callback (thread, wait_ptid);
2643 /* If there are no resumed children left in the set of LWPs we
2644 want to wait for, bail. We can't just block in
2645 waitpid/sigsuspend, because lwps might have been left stopped
2646 in trace-stop state, and we'd be stuck forever waiting for
2647 their status to change (which would only happen if we resumed
2648 them). Even if WNOHANG is set, this return code is preferred
2649 over 0 (below), as it is more detailed. */
2650 if (find_thread (not_stopped) == NULL)
2652 threads_debug_printf ("exit (no unwaited-for LWP)");
2654 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2655 return -1;
2658 /* No interesting event to report to the caller. */
2659 if ((options & WNOHANG))
2661 threads_debug_printf ("WNOHANG set, no event found");
2663 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2664 return 0;
2667 /* Block until we get an event reported with SIGCHLD. */
2668 threads_debug_printf ("sigsuspend'ing");
2670 sigsuspend (&prev_mask);
2671 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2672 goto retry;
2675 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2677 switch_to_thread (event_thread);
2679 return lwpid_of (event_thread);
2683 linux_process_target::wait_for_event (ptid_t ptid, int *wstatp, int options)
2685 return wait_for_event_filtered (ptid, ptid, wstatp, options);
2688 /* Select one LWP out of those that have events pending. */
2690 static void
2691 select_event_lwp (struct lwp_info **orig_lp)
2693 struct thread_info *event_thread = NULL;
2695 /* In all-stop, give preference to the LWP that is being
2696 single-stepped. There will be at most one, and it's the LWP that
2697 the core is most interested in. If we didn't do this, then we'd
2698 have to handle pending step SIGTRAPs somehow in case the core
2699 later continues the previously-stepped thread, otherwise we'd
2700 report the pending SIGTRAP, and the core, not having stepped the
2701 thread, wouldn't understand what the trap was for, and therefore
2702 would report it to the user as a random signal. */
2703 if (!non_stop)
2705 event_thread = find_thread ([] (thread_info *thread)
2707 lwp_info *lp = get_thread_lwp (thread);
2709 return (thread->last_status.kind () == TARGET_WAITKIND_IGNORE
2710 && thread->last_resume_kind == resume_step
2711 && lp->status_pending_p);
2714 if (event_thread != NULL)
2715 threads_debug_printf
2716 ("Select single-step %s",
2717 target_pid_to_str (ptid_of (event_thread)).c_str ());
2719 if (event_thread == NULL)
2721 /* No single-stepping LWP. Select one at random, out of those
2722 which have had events. */
2724 event_thread = find_thread_in_random ([&] (thread_info *thread)
2726 lwp_info *lp = get_thread_lwp (thread);
2728 /* Only resumed LWPs that have an event pending. */
2729 return (thread->last_status.kind () == TARGET_WAITKIND_IGNORE
2730 && lp->status_pending_p);
2734 if (event_thread != NULL)
2736 struct lwp_info *event_lp = get_thread_lwp (event_thread);
2738 /* Switch the event LWP. */
2739 *orig_lp = event_lp;
2743 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2744 NULL. */
2746 static void
2747 unsuspend_all_lwps (struct lwp_info *except)
2749 for_each_thread ([&] (thread_info *thread)
2751 lwp_info *lwp = get_thread_lwp (thread);
2753 if (lwp != except)
2754 lwp_suspended_decr (lwp);
2758 static bool lwp_running (thread_info *thread);
2760 /* Stabilize threads (move out of jump pads).
2762 If a thread is midway collecting a fast tracepoint, we need to
2763 finish the collection and move it out of the jump pad before
2764 reporting the signal.
2766 This avoids recursion while collecting (when a signal arrives
2767 midway, and the signal handler itself collects), which would trash
2768 the trace buffer. In case the user set a breakpoint in a signal
2769 handler, this avoids the backtrace showing the jump pad, etc..
2770 Most importantly, there are certain things we can't do safely if
2771 threads are stopped in a jump pad (or in its callee's). For
2772 example:
2774 - starting a new trace run. A thread still collecting the
2775 previous run, could trash the trace buffer when resumed. The trace
2776 buffer control structures would have been reset but the thread had
2777 no way to tell. The thread could even midway memcpy'ing to the
2778 buffer, which would mean that when resumed, it would clobber the
2779 trace buffer that had been set for a new run.
2781 - we can't rewrite/reuse the jump pads for new tracepoints
2782 safely. Say you do tstart while a thread is stopped midway while
2783 collecting. When the thread is later resumed, it finishes the
2784 collection, and returns to the jump pad, to execute the original
2785 instruction that was under the tracepoint jump at the time the
2786 older run had been started. If the jump pad had been rewritten
2787 since for something else in the new run, the thread would now
2788 execute the wrong / random instructions. */
2790 void
2791 linux_process_target::stabilize_threads ()
2793 thread_info *thread_stuck = find_thread ([this] (thread_info *thread)
2795 return stuck_in_jump_pad (thread);
2798 if (thread_stuck != NULL)
2800 threads_debug_printf ("can't stabilize, LWP %ld is stuck in jump pad",
2801 lwpid_of (thread_stuck));
2802 return;
2805 scoped_restore_current_thread restore_thread;
2807 stabilizing_threads = 1;
2809 /* Kick 'em all. */
2810 for_each_thread ([this] (thread_info *thread)
2812 move_out_of_jump_pad (thread);
2815 /* Loop until all are stopped out of the jump pads. */
2816 while (find_thread (lwp_running) != NULL)
2818 struct target_waitstatus ourstatus;
2819 struct lwp_info *lwp;
2820 int wstat;
2822 /* Note that we go through the full wait even loop. While
2823 moving threads out of jump pad, we need to be able to step
2824 over internal breakpoints and such. */
2825 wait_1 (minus_one_ptid, &ourstatus, 0);
2827 if (ourstatus.kind () == TARGET_WAITKIND_STOPPED)
2829 lwp = get_thread_lwp (current_thread);
2831 /* Lock it. */
2832 lwp_suspended_inc (lwp);
2834 if (ourstatus.sig () != GDB_SIGNAL_0
2835 || current_thread->last_resume_kind == resume_stop)
2837 wstat = W_STOPCODE (gdb_signal_to_host (ourstatus.sig ()));
2838 enqueue_one_deferred_signal (lwp, &wstat);
2843 unsuspend_all_lwps (NULL);
2845 stabilizing_threads = 0;
2847 if (debug_threads)
2849 thread_stuck = find_thread ([this] (thread_info *thread)
2851 return stuck_in_jump_pad (thread);
2854 if (thread_stuck != NULL)
2855 threads_debug_printf
2856 ("couldn't stabilize, LWP %ld got stuck in jump pad",
2857 lwpid_of (thread_stuck));
2861 /* Convenience function that is called when the kernel reports an
2862 event that is not passed out to GDB. */
2864 static ptid_t
2865 ignore_event (struct target_waitstatus *ourstatus)
2867 /* If we got an event, there may still be others, as a single
2868 SIGCHLD can indicate more than one child stopped. This forces
2869 another target_wait call. */
2870 async_file_mark ();
2872 ourstatus->set_ignore ();
2873 return null_ptid;
2876 ptid_t
2877 linux_process_target::filter_exit_event (lwp_info *event_child,
2878 target_waitstatus *ourstatus)
2880 client_state &cs = get_client_state ();
2881 struct thread_info *thread = get_lwp_thread (event_child);
2882 ptid_t ptid = ptid_of (thread);
2884 if (!is_leader (thread))
2886 if (cs.report_thread_events)
2887 ourstatus->set_thread_exited (0);
2888 else
2889 ourstatus->set_ignore ();
2891 delete_lwp (event_child);
2893 return ptid;
2896 /* Returns 1 if GDB is interested in any event_child syscalls. */
2898 static int
2899 gdb_catching_syscalls_p (struct lwp_info *event_child)
2901 struct thread_info *thread = get_lwp_thread (event_child);
2902 struct process_info *proc = get_thread_process (thread);
2904 return !proc->syscalls_to_catch.empty ();
2907 bool
2908 linux_process_target::gdb_catch_this_syscall (lwp_info *event_child)
2910 int sysno;
2911 struct thread_info *thread = get_lwp_thread (event_child);
2912 struct process_info *proc = get_thread_process (thread);
2914 if (proc->syscalls_to_catch.empty ())
2915 return false;
2917 if (proc->syscalls_to_catch[0] == ANY_SYSCALL)
2918 return true;
2920 get_syscall_trapinfo (event_child, &sysno);
2922 for (int iter : proc->syscalls_to_catch)
2923 if (iter == sysno)
2924 return true;
2926 return false;
2929 ptid_t
2930 linux_process_target::wait_1 (ptid_t ptid, target_waitstatus *ourstatus,
2931 target_wait_flags target_options)
2933 THREADS_SCOPED_DEBUG_ENTER_EXIT;
2935 client_state &cs = get_client_state ();
2936 int w;
2937 struct lwp_info *event_child;
2938 int options;
2939 int pid;
2940 int step_over_finished;
2941 int bp_explains_trap;
2942 int maybe_internal_trap;
2943 int report_to_gdb;
2944 int trace_event;
2945 int in_step_range;
2946 int any_resumed;
2948 threads_debug_printf ("[%s]", target_pid_to_str (ptid).c_str ());
2950 /* Translate generic target options into linux options. */
2951 options = __WALL;
2952 if (target_options & TARGET_WNOHANG)
2953 options |= WNOHANG;
2955 bp_explains_trap = 0;
2956 trace_event = 0;
2957 in_step_range = 0;
2958 ourstatus->set_ignore ();
2960 auto status_pending_p_any = [&] (thread_info *thread)
2962 return status_pending_p_callback (thread, minus_one_ptid);
2965 auto not_stopped = [&] (thread_info *thread)
2967 return not_stopped_callback (thread, minus_one_ptid);
2970 /* Find a resumed LWP, if any. */
2971 if (find_thread (status_pending_p_any) != NULL)
2972 any_resumed = 1;
2973 else if (find_thread (not_stopped) != NULL)
2974 any_resumed = 1;
2975 else
2976 any_resumed = 0;
2978 if (step_over_bkpt == null_ptid)
2979 pid = wait_for_event (ptid, &w, options);
2980 else
2982 threads_debug_printf ("step_over_bkpt set [%s], doing a blocking wait",
2983 target_pid_to_str (step_over_bkpt).c_str ());
2984 pid = wait_for_event (step_over_bkpt, &w, options & ~WNOHANG);
2987 if (pid == 0 || (pid == -1 && !any_resumed))
2989 gdb_assert (target_options & TARGET_WNOHANG);
2991 threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_IGNORE");
2993 ourstatus->set_ignore ();
2994 return null_ptid;
2996 else if (pid == -1)
2998 threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_NO_RESUMED");
3000 ourstatus->set_no_resumed ();
3001 return null_ptid;
3004 event_child = get_thread_lwp (current_thread);
3006 /* wait_for_event only returns an exit status for the last
3007 child of a process. Report it. */
3008 if (WIFEXITED (w) || WIFSIGNALED (w))
3010 if (WIFEXITED (w))
3012 ourstatus->set_exited (WEXITSTATUS (w));
3014 threads_debug_printf
3015 ("ret = %s, exited with retcode %d",
3016 target_pid_to_str (ptid_of (current_thread)).c_str (),
3017 WEXITSTATUS (w));
3019 else
3021 ourstatus->set_signalled (gdb_signal_from_host (WTERMSIG (w)));
3023 threads_debug_printf
3024 ("ret = %s, terminated with signal %d",
3025 target_pid_to_str (ptid_of (current_thread)).c_str (),
3026 WTERMSIG (w));
3029 if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
3030 return filter_exit_event (event_child, ourstatus);
3032 return ptid_of (current_thread);
3035 /* If step-over executes a breakpoint instruction, in the case of a
3036 hardware single step it means a gdb/gdbserver breakpoint had been
3037 planted on top of a permanent breakpoint, in the case of a software
3038 single step it may just mean that gdbserver hit the reinsert breakpoint.
3039 The PC has been adjusted by save_stop_reason to point at
3040 the breakpoint address.
3041 So in the case of the hardware single step advance the PC manually
3042 past the breakpoint and in the case of software single step advance only
3043 if it's not the single_step_breakpoint we are hitting.
3044 This avoids that a program would keep trapping a permanent breakpoint
3045 forever. */
3046 if (step_over_bkpt != null_ptid
3047 && event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
3048 && (event_child->stepping
3049 || !single_step_breakpoint_inserted_here (event_child->stop_pc)))
3051 int increment_pc = 0;
3052 int breakpoint_kind = 0;
3053 CORE_ADDR stop_pc = event_child->stop_pc;
3055 breakpoint_kind = breakpoint_kind_from_current_state (&stop_pc);
3056 sw_breakpoint_from_kind (breakpoint_kind, &increment_pc);
3058 threads_debug_printf
3059 ("step-over for %s executed software breakpoint",
3060 target_pid_to_str (ptid_of (current_thread)).c_str ());
3062 if (increment_pc != 0)
3064 struct regcache *regcache
3065 = get_thread_regcache (current_thread, 1);
3067 event_child->stop_pc += increment_pc;
3068 low_set_pc (regcache, event_child->stop_pc);
3070 if (!low_breakpoint_at (event_child->stop_pc))
3071 event_child->stop_reason = TARGET_STOPPED_BY_NO_REASON;
3075 /* If this event was not handled before, and is not a SIGTRAP, we
3076 report it. SIGILL and SIGSEGV are also treated as traps in case
3077 a breakpoint is inserted at the current PC. If this target does
3078 not support internal breakpoints at all, we also report the
3079 SIGTRAP without further processing; it's of no concern to us. */
3080 maybe_internal_trap
3081 = (low_supports_breakpoints ()
3082 && (WSTOPSIG (w) == SIGTRAP
3083 || ((WSTOPSIG (w) == SIGILL
3084 || WSTOPSIG (w) == SIGSEGV)
3085 && low_breakpoint_at (event_child->stop_pc))));
3087 if (maybe_internal_trap)
3089 /* Handle anything that requires bookkeeping before deciding to
3090 report the event or continue waiting. */
3092 /* First check if we can explain the SIGTRAP with an internal
3093 breakpoint, or if we should possibly report the event to GDB.
3094 Do this before anything that may remove or insert a
3095 breakpoint. */
3096 bp_explains_trap = breakpoint_inserted_here (event_child->stop_pc);
3098 /* We have a SIGTRAP, possibly a step-over dance has just
3099 finished. If so, tweak the state machine accordingly,
3100 reinsert breakpoints and delete any single-step
3101 breakpoints. */
3102 step_over_finished = finish_step_over (event_child);
3104 /* Now invoke the callbacks of any internal breakpoints there. */
3105 check_breakpoints (event_child->stop_pc);
3107 /* Handle tracepoint data collecting. This may overflow the
3108 trace buffer, and cause a tracing stop, removing
3109 breakpoints. */
3110 trace_event = handle_tracepoints (event_child);
3112 if (bp_explains_trap)
3113 threads_debug_printf ("Hit a gdbserver breakpoint.");
3115 else
3117 /* We have some other signal, possibly a step-over dance was in
3118 progress, and it should be cancelled too. */
3119 step_over_finished = finish_step_over (event_child);
3122 /* We have all the data we need. Either report the event to GDB, or
3123 resume threads and keep waiting for more. */
3125 /* If we're collecting a fast tracepoint, finish the collection and
3126 move out of the jump pad before delivering a signal. See
3127 linux_stabilize_threads. */
3129 if (WIFSTOPPED (w)
3130 && WSTOPSIG (w) != SIGTRAP
3131 && supports_fast_tracepoints ()
3132 && agent_loaded_p ())
3134 threads_debug_printf ("Got signal %d for LWP %ld. Check if we need "
3135 "to defer or adjust it.",
3136 WSTOPSIG (w), lwpid_of (current_thread));
3138 /* Allow debugging the jump pad itself. */
3139 if (current_thread->last_resume_kind != resume_step
3140 && maybe_move_out_of_jump_pad (event_child, &w))
3142 enqueue_one_deferred_signal (event_child, &w);
3144 threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad)",
3145 WSTOPSIG (w), lwpid_of (current_thread));
3147 resume_one_lwp (event_child, 0, 0, NULL);
3149 return ignore_event (ourstatus);
3153 if (event_child->collecting_fast_tracepoint
3154 != fast_tpoint_collect_result::not_collecting)
3156 threads_debug_printf
3157 ("LWP %ld was trying to move out of the jump pad (%d). "
3158 "Check if we're already there.",
3159 lwpid_of (current_thread),
3160 (int) event_child->collecting_fast_tracepoint);
3162 trace_event = 1;
3164 event_child->collecting_fast_tracepoint
3165 = linux_fast_tracepoint_collecting (event_child, NULL);
3167 if (event_child->collecting_fast_tracepoint
3168 != fast_tpoint_collect_result::before_insn)
3170 /* No longer need this breakpoint. */
3171 if (event_child->exit_jump_pad_bkpt != NULL)
3173 threads_debug_printf
3174 ("No longer need exit-jump-pad bkpt; removing it."
3175 "stopping all threads momentarily.");
3177 /* Other running threads could hit this breakpoint.
3178 We don't handle moribund locations like GDB does,
3179 instead we always pause all threads when removing
3180 breakpoints, so that any step-over or
3181 decr_pc_after_break adjustment is always taken
3182 care of while the breakpoint is still
3183 inserted. */
3184 stop_all_lwps (1, event_child);
3186 delete_breakpoint (event_child->exit_jump_pad_bkpt);
3187 event_child->exit_jump_pad_bkpt = NULL;
3189 unstop_all_lwps (1, event_child);
3191 gdb_assert (event_child->suspended >= 0);
3195 if (event_child->collecting_fast_tracepoint
3196 == fast_tpoint_collect_result::not_collecting)
3198 threads_debug_printf
3199 ("fast tracepoint finished collecting successfully.");
3201 /* We may have a deferred signal to report. */
3202 if (dequeue_one_deferred_signal (event_child, &w))
3203 threads_debug_printf ("dequeued one signal.");
3204 else
3206 threads_debug_printf ("no deferred signals.");
3208 if (stabilizing_threads)
3210 ourstatus->set_stopped (GDB_SIGNAL_0);
3212 threads_debug_printf
3213 ("ret = %s, stopped while stabilizing threads",
3214 target_pid_to_str (ptid_of (current_thread)).c_str ());
3216 return ptid_of (current_thread);
3222 /* Check whether GDB would be interested in this event. */
3224 /* Check if GDB is interested in this syscall. */
3225 if (WIFSTOPPED (w)
3226 && WSTOPSIG (w) == SYSCALL_SIGTRAP
3227 && !gdb_catch_this_syscall (event_child))
3229 threads_debug_printf ("Ignored syscall for LWP %ld.",
3230 lwpid_of (current_thread));
3232 resume_one_lwp (event_child, event_child->stepping, 0, NULL);
3234 return ignore_event (ourstatus);
3237 /* If GDB is not interested in this signal, don't stop other
3238 threads, and don't report it to GDB. Just resume the inferior
3239 right away. We do this for threading-related signals as well as
3240 any that GDB specifically requested we ignore. But never ignore
3241 SIGSTOP if we sent it ourselves, and do not ignore signals when
3242 stepping - they may require special handling to skip the signal
3243 handler. Also never ignore signals that could be caused by a
3244 breakpoint. */
3245 if (WIFSTOPPED (w)
3246 && current_thread->last_resume_kind != resume_step
3247 && (
3248 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3249 (current_process ()->priv->thread_db != NULL
3250 && (WSTOPSIG (w) == __SIGRTMIN
3251 || WSTOPSIG (w) == __SIGRTMIN + 1))
3253 #endif
3254 (cs.pass_signals[gdb_signal_from_host (WSTOPSIG (w))]
3255 && !(WSTOPSIG (w) == SIGSTOP
3256 && current_thread->last_resume_kind == resume_stop)
3257 && !linux_wstatus_maybe_breakpoint (w))))
3259 siginfo_t info, *info_p;
3261 threads_debug_printf ("Ignored signal %d for LWP %ld.",
3262 WSTOPSIG (w), lwpid_of (current_thread));
3264 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
3265 (PTRACE_TYPE_ARG3) 0, &info) == 0)
3266 info_p = &info;
3267 else
3268 info_p = NULL;
3270 if (step_over_finished)
3272 /* We cancelled this thread's step-over above. We still
3273 need to unsuspend all other LWPs, and set them back
3274 running again while the signal handler runs. */
3275 unsuspend_all_lwps (event_child);
3277 /* Enqueue the pending signal info so that proceed_all_lwps
3278 doesn't lose it. */
3279 enqueue_pending_signal (event_child, WSTOPSIG (w), info_p);
3281 proceed_all_lwps ();
3283 else
3285 resume_one_lwp (event_child, event_child->stepping,
3286 WSTOPSIG (w), info_p);
3289 return ignore_event (ourstatus);
3292 /* Note that all addresses are always "out of the step range" when
3293 there's no range to begin with. */
3294 in_step_range = lwp_in_step_range (event_child);
3296 /* If GDB wanted this thread to single step, and the thread is out
3297 of the step range, we always want to report the SIGTRAP, and let
3298 GDB handle it. Watchpoints should always be reported. So should
3299 signals we can't explain. A SIGTRAP we can't explain could be a
3300 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3301 do, we're be able to handle GDB breakpoints on top of internal
3302 breakpoints, by handling the internal breakpoint and still
3303 reporting the event to GDB. If we don't, we're out of luck, GDB
3304 won't see the breakpoint hit. If we see a single-step event but
3305 the thread should be continuing, don't pass the trap to gdb.
3306 That indicates that we had previously finished a single-step but
3307 left the single-step pending -- see
3308 complete_ongoing_step_over. */
3309 report_to_gdb = (!maybe_internal_trap
3310 || (current_thread->last_resume_kind == resume_step
3311 && !in_step_range)
3312 || event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
3313 || (!in_step_range
3314 && !bp_explains_trap
3315 && !trace_event
3316 && !step_over_finished
3317 && !(current_thread->last_resume_kind == resume_continue
3318 && event_child->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP))
3319 || (gdb_breakpoint_here (event_child->stop_pc)
3320 && gdb_condition_true_at_breakpoint (event_child->stop_pc)
3321 && gdb_no_commands_at_breakpoint (event_child->stop_pc))
3322 || event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE);
3324 run_breakpoint_commands (event_child->stop_pc);
3326 /* We found no reason GDB would want us to stop. We either hit one
3327 of our own breakpoints, or finished an internal step GDB
3328 shouldn't know about. */
3329 if (!report_to_gdb)
3331 if (bp_explains_trap)
3332 threads_debug_printf ("Hit a gdbserver breakpoint.");
3334 if (step_over_finished)
3335 threads_debug_printf ("Step-over finished.");
3337 if (trace_event)
3338 threads_debug_printf ("Tracepoint event.");
3340 if (lwp_in_step_range (event_child))
3341 threads_debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).",
3342 paddress (event_child->stop_pc),
3343 paddress (event_child->step_range_start),
3344 paddress (event_child->step_range_end));
3346 /* We're not reporting this breakpoint to GDB, so apply the
3347 decr_pc_after_break adjustment to the inferior's regcache
3348 ourselves. */
3350 if (low_supports_breakpoints ())
3352 struct regcache *regcache
3353 = get_thread_regcache (current_thread, 1);
3354 low_set_pc (regcache, event_child->stop_pc);
3357 if (step_over_finished)
3359 /* If we have finished stepping over a breakpoint, we've
3360 stopped and suspended all LWPs momentarily except the
3361 stepping one. This is where we resume them all again.
3362 We're going to keep waiting, so use proceed, which
3363 handles stepping over the next breakpoint. */
3364 unsuspend_all_lwps (event_child);
3366 else
3368 /* Remove the single-step breakpoints if any. Note that
3369 there isn't single-step breakpoint if we finished stepping
3370 over. */
3371 if (supports_software_single_step ()
3372 && has_single_step_breakpoints (current_thread))
3374 stop_all_lwps (0, event_child);
3375 delete_single_step_breakpoints (current_thread);
3376 unstop_all_lwps (0, event_child);
3380 threads_debug_printf ("proceeding all threads.");
3382 proceed_all_lwps ();
3384 return ignore_event (ourstatus);
3387 if (debug_threads)
3389 if (event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
3390 threads_debug_printf ("LWP %ld: extended event with waitstatus %s",
3391 lwpid_of (get_lwp_thread (event_child)),
3392 event_child->waitstatus.to_string ().c_str ());
3394 if (current_thread->last_resume_kind == resume_step)
3396 if (event_child->step_range_start == event_child->step_range_end)
3397 threads_debug_printf
3398 ("GDB wanted to single-step, reporting event.");
3399 else if (!lwp_in_step_range (event_child))
3400 threads_debug_printf ("Out of step range, reporting event.");
3403 if (event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
3404 threads_debug_printf ("Stopped by watchpoint.");
3405 else if (gdb_breakpoint_here (event_child->stop_pc))
3406 threads_debug_printf ("Stopped by GDB breakpoint.");
3409 threads_debug_printf ("Hit a non-gdbserver trap event.");
3411 /* Alright, we're going to report a stop. */
3413 /* Remove single-step breakpoints. */
3414 if (supports_software_single_step ())
3416 /* Remove single-step breakpoints or not. It it is true, stop all
3417 lwps, so that other threads won't hit the breakpoint in the
3418 staled memory. */
3419 int remove_single_step_breakpoints_p = 0;
3421 if (non_stop)
3423 remove_single_step_breakpoints_p
3424 = has_single_step_breakpoints (current_thread);
3426 else
3428 /* In all-stop, a stop reply cancels all previous resume
3429 requests. Delete all single-step breakpoints. */
3431 find_thread ([&] (thread_info *thread) {
3432 if (has_single_step_breakpoints (thread))
3434 remove_single_step_breakpoints_p = 1;
3435 return true;
3438 return false;
3442 if (remove_single_step_breakpoints_p)
3444 /* If we remove single-step breakpoints from memory, stop all lwps,
3445 so that other threads won't hit the breakpoint in the staled
3446 memory. */
3447 stop_all_lwps (0, event_child);
3449 if (non_stop)
3451 gdb_assert (has_single_step_breakpoints (current_thread));
3452 delete_single_step_breakpoints (current_thread);
3454 else
3456 for_each_thread ([] (thread_info *thread){
3457 if (has_single_step_breakpoints (thread))
3458 delete_single_step_breakpoints (thread);
3462 unstop_all_lwps (0, event_child);
3466 if (!stabilizing_threads)
3468 /* In all-stop, stop all threads. */
3469 if (!non_stop)
3470 stop_all_lwps (0, NULL);
3472 if (step_over_finished)
3474 if (!non_stop)
3476 /* If we were doing a step-over, all other threads but
3477 the stepping one had been paused in start_step_over,
3478 with their suspend counts incremented. We don't want
3479 to do a full unstop/unpause, because we're in
3480 all-stop mode (so we want threads stopped), but we
3481 still need to unsuspend the other threads, to
3482 decrement their `suspended' count back. */
3483 unsuspend_all_lwps (event_child);
3485 else
3487 /* If we just finished a step-over, then all threads had
3488 been momentarily paused. In all-stop, that's fine,
3489 we want threads stopped by now anyway. In non-stop,
3490 we need to re-resume threads that GDB wanted to be
3491 running. */
3492 unstop_all_lwps (1, event_child);
3496 /* If we're not waiting for a specific LWP, choose an event LWP
3497 from among those that have had events. Giving equal priority
3498 to all LWPs that have had events helps prevent
3499 starvation. */
3500 if (ptid == minus_one_ptid)
3502 event_child->status_pending_p = 1;
3503 event_child->status_pending = w;
3505 select_event_lwp (&event_child);
3507 /* current_thread and event_child must stay in sync. */
3508 switch_to_thread (get_lwp_thread (event_child));
3510 event_child->status_pending_p = 0;
3511 w = event_child->status_pending;
3515 /* Stabilize threads (move out of jump pads). */
3516 if (!non_stop)
3517 target_stabilize_threads ();
3519 else
3521 /* If we just finished a step-over, then all threads had been
3522 momentarily paused. In all-stop, that's fine, we want
3523 threads stopped by now anyway. In non-stop, we need to
3524 re-resume threads that GDB wanted to be running. */
3525 if (step_over_finished)
3526 unstop_all_lwps (1, event_child);
3529 /* At this point, we haven't set OURSTATUS. This is where we do it. */
3530 gdb_assert (ourstatus->kind () == TARGET_WAITKIND_IGNORE);
3532 if (event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
3534 /* If the reported event is an exit, fork, vfork or exec, let
3535 GDB know. */
3537 /* Break the unreported fork relationship chain. */
3538 if (event_child->waitstatus.kind () == TARGET_WAITKIND_FORKED
3539 || event_child->waitstatus.kind () == TARGET_WAITKIND_VFORKED)
3541 event_child->fork_relative->fork_relative = NULL;
3542 event_child->fork_relative = NULL;
3545 *ourstatus = event_child->waitstatus;
3546 /* Clear the event lwp's waitstatus since we handled it already. */
3547 event_child->waitstatus.set_ignore ();
3549 else
3551 /* The LWP stopped due to a plain signal or a syscall signal. Either way,
3552 event_child->waitstatus wasn't filled in with the details, so look at
3553 the wait status W. */
3554 if (WSTOPSIG (w) == SYSCALL_SIGTRAP)
3556 int syscall_number;
3558 get_syscall_trapinfo (event_child, &syscall_number);
3559 if (event_child->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY)
3560 ourstatus->set_syscall_entry (syscall_number);
3561 else if (event_child->syscall_state == TARGET_WAITKIND_SYSCALL_RETURN)
3562 ourstatus->set_syscall_return (syscall_number);
3563 else
3564 gdb_assert_not_reached ("unexpected syscall state");
3566 else if (current_thread->last_resume_kind == resume_stop
3567 && WSTOPSIG (w) == SIGSTOP)
3569 /* A thread that has been requested to stop by GDB with vCont;t,
3570 and it stopped cleanly, so report as SIG0. The use of
3571 SIGSTOP is an implementation detail. */
3572 ourstatus->set_stopped (GDB_SIGNAL_0);
3574 else
3575 ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w)));
3578 /* Now that we've selected our final event LWP, un-adjust its PC if
3579 it was a software breakpoint, and the client doesn't know we can
3580 adjust the breakpoint ourselves. */
3581 if (event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
3582 && !cs.swbreak_feature)
3584 int decr_pc = low_decr_pc_after_break ();
3586 if (decr_pc != 0)
3588 struct regcache *regcache
3589 = get_thread_regcache (current_thread, 1);
3590 low_set_pc (regcache, event_child->stop_pc + decr_pc);
3594 gdb_assert (step_over_bkpt == null_ptid);
3596 threads_debug_printf ("ret = %s, %s",
3597 target_pid_to_str (ptid_of (current_thread)).c_str (),
3598 ourstatus->to_string ().c_str ());
3600 if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
3601 return filter_exit_event (event_child, ourstatus);
3603 return ptid_of (current_thread);
3606 /* Get rid of any pending event in the pipe. */
3607 static void
3608 async_file_flush (void)
3610 linux_event_pipe.flush ();
3613 /* Put something in the pipe, so the event loop wakes up. */
3614 static void
3615 async_file_mark (void)
3617 linux_event_pipe.mark ();
3620 ptid_t
3621 linux_process_target::wait (ptid_t ptid,
3622 target_waitstatus *ourstatus,
3623 target_wait_flags target_options)
3625 ptid_t event_ptid;
3627 /* Flush the async file first. */
3628 if (target_is_async_p ())
3629 async_file_flush ();
3633 event_ptid = wait_1 (ptid, ourstatus, target_options);
3635 while ((target_options & TARGET_WNOHANG) == 0
3636 && event_ptid == null_ptid
3637 && ourstatus->kind () == TARGET_WAITKIND_IGNORE);
3639 /* If at least one stop was reported, there may be more. A single
3640 SIGCHLD can signal more than one child stop. */
3641 if (target_is_async_p ()
3642 && (target_options & TARGET_WNOHANG) != 0
3643 && event_ptid != null_ptid)
3644 async_file_mark ();
3646 return event_ptid;
3649 /* Send a signal to an LWP. */
3651 static int
3652 kill_lwp (unsigned long lwpid, int signo)
3654 int ret;
3656 errno = 0;
3657 ret = syscall (__NR_tkill, lwpid, signo);
3658 if (errno == ENOSYS)
3660 /* If tkill fails, then we are not using nptl threads, a
3661 configuration we no longer support. */
3662 perror_with_name (("tkill"));
3664 return ret;
3667 void
3668 linux_stop_lwp (struct lwp_info *lwp)
3670 send_sigstop (lwp);
3673 static void
3674 send_sigstop (struct lwp_info *lwp)
3676 int pid;
3678 pid = lwpid_of (get_lwp_thread (lwp));
3680 /* If we already have a pending stop signal for this process, don't
3681 send another. */
3682 if (lwp->stop_expected)
3684 threads_debug_printf ("Have pending sigstop for lwp %d", pid);
3686 return;
3689 threads_debug_printf ("Sending sigstop to lwp %d", pid);
3691 lwp->stop_expected = 1;
3692 kill_lwp (pid, SIGSTOP);
3695 static void
3696 send_sigstop (thread_info *thread, lwp_info *except)
3698 struct lwp_info *lwp = get_thread_lwp (thread);
3700 /* Ignore EXCEPT. */
3701 if (lwp == except)
3702 return;
3704 if (lwp->stopped)
3705 return;
3707 send_sigstop (lwp);
3710 /* Increment the suspend count of an LWP, and stop it, if not stopped
3711 yet. */
3712 static void
3713 suspend_and_send_sigstop (thread_info *thread, lwp_info *except)
3715 struct lwp_info *lwp = get_thread_lwp (thread);
3717 /* Ignore EXCEPT. */
3718 if (lwp == except)
3719 return;
3721 lwp_suspended_inc (lwp);
3723 send_sigstop (thread, except);
3726 static void
3727 mark_lwp_dead (struct lwp_info *lwp, int wstat)
3729 /* Store the exit status for later. */
3730 lwp->status_pending_p = 1;
3731 lwp->status_pending = wstat;
3733 /* Store in waitstatus as well, as there's nothing else to process
3734 for this event. */
3735 if (WIFEXITED (wstat))
3736 lwp->waitstatus.set_exited (WEXITSTATUS (wstat));
3737 else if (WIFSIGNALED (wstat))
3738 lwp->waitstatus.set_signalled (gdb_signal_from_host (WTERMSIG (wstat)));
3740 /* Prevent trying to stop it. */
3741 lwp->stopped = 1;
3743 /* No further stops are expected from a dead lwp. */
3744 lwp->stop_expected = 0;
3747 /* Return true if LWP has exited already, and has a pending exit event
3748 to report to GDB. */
3750 static int
3751 lwp_is_marked_dead (struct lwp_info *lwp)
3753 return (lwp->status_pending_p
3754 && (WIFEXITED (lwp->status_pending)
3755 || WIFSIGNALED (lwp->status_pending)));
3758 void
3759 linux_process_target::wait_for_sigstop ()
3761 struct thread_info *saved_thread;
3762 ptid_t saved_tid;
3763 int wstat;
3764 int ret;
3766 saved_thread = current_thread;
3767 if (saved_thread != NULL)
3768 saved_tid = saved_thread->id;
3769 else
3770 saved_tid = null_ptid; /* avoid bogus unused warning */
3772 scoped_restore_current_thread restore_thread;
3774 threads_debug_printf ("pulling events");
3776 /* Passing NULL_PTID as filter indicates we want all events to be
3777 left pending. Eventually this returns when there are no
3778 unwaited-for children left. */
3779 ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat, __WALL);
3780 gdb_assert (ret == -1);
3782 if (saved_thread == NULL || mythread_alive (saved_tid))
3783 return;
3784 else
3786 threads_debug_printf ("Previously current thread died.");
3788 /* We can't change the current inferior behind GDB's back,
3789 otherwise, a subsequent command may apply to the wrong
3790 process. */
3791 restore_thread.dont_restore ();
3792 switch_to_thread (nullptr);
3796 bool
3797 linux_process_target::stuck_in_jump_pad (thread_info *thread)
3799 struct lwp_info *lwp = get_thread_lwp (thread);
3801 if (lwp->suspended != 0)
3803 internal_error ("LWP %ld is suspended, suspended=%d\n",
3804 lwpid_of (thread), lwp->suspended);
3806 gdb_assert (lwp->stopped);
3808 /* Allow debugging the jump pad, gdb_collect, etc.. */
3809 return (supports_fast_tracepoints ()
3810 && agent_loaded_p ()
3811 && (gdb_breakpoint_here (lwp->stop_pc)
3812 || lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
3813 || thread->last_resume_kind == resume_step)
3814 && (linux_fast_tracepoint_collecting (lwp, NULL)
3815 != fast_tpoint_collect_result::not_collecting));
3818 void
3819 linux_process_target::move_out_of_jump_pad (thread_info *thread)
3821 struct lwp_info *lwp = get_thread_lwp (thread);
3822 int *wstat;
3824 if (lwp->suspended != 0)
3826 internal_error ("LWP %ld is suspended, suspended=%d\n",
3827 lwpid_of (thread), lwp->suspended);
3829 gdb_assert (lwp->stopped);
3831 /* For gdb_breakpoint_here. */
3832 scoped_restore_current_thread restore_thread;
3833 switch_to_thread (thread);
3835 wstat = lwp->status_pending_p ? &lwp->status_pending : NULL;
3837 /* Allow debugging the jump pad, gdb_collect, etc. */
3838 if (!gdb_breakpoint_here (lwp->stop_pc)
3839 && lwp->stop_reason != TARGET_STOPPED_BY_WATCHPOINT
3840 && thread->last_resume_kind != resume_step
3841 && maybe_move_out_of_jump_pad (lwp, wstat))
3843 threads_debug_printf ("LWP %ld needs stabilizing (in jump pad)",
3844 lwpid_of (thread));
3846 if (wstat)
3848 lwp->status_pending_p = 0;
3849 enqueue_one_deferred_signal (lwp, wstat);
3851 threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad",
3852 WSTOPSIG (*wstat), lwpid_of (thread));
3855 resume_one_lwp (lwp, 0, 0, NULL);
3857 else
3858 lwp_suspended_inc (lwp);
3861 static bool
3862 lwp_running (thread_info *thread)
3864 struct lwp_info *lwp = get_thread_lwp (thread);
3866 if (lwp_is_marked_dead (lwp))
3867 return false;
3869 return !lwp->stopped;
3872 void
3873 linux_process_target::stop_all_lwps (int suspend, lwp_info *except)
3875 /* Should not be called recursively. */
3876 gdb_assert (stopping_threads == NOT_STOPPING_THREADS);
3878 THREADS_SCOPED_DEBUG_ENTER_EXIT;
3880 threads_debug_printf
3881 ("%s, except=%s", suspend ? "stop-and-suspend" : "stop",
3882 (except != NULL
3883 ? target_pid_to_str (ptid_of (get_lwp_thread (except))).c_str ()
3884 : "none"));
3886 stopping_threads = (suspend
3887 ? STOPPING_AND_SUSPENDING_THREADS
3888 : STOPPING_THREADS);
3890 if (suspend)
3891 for_each_thread ([&] (thread_info *thread)
3893 suspend_and_send_sigstop (thread, except);
3895 else
3896 for_each_thread ([&] (thread_info *thread)
3898 send_sigstop (thread, except);
3901 wait_for_sigstop ();
3902 stopping_threads = NOT_STOPPING_THREADS;
3904 threads_debug_printf ("setting stopping_threads back to !stopping");
3907 /* Enqueue one signal in the chain of signals which need to be
3908 delivered to this process on next resume. */
3910 static void
3911 enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info)
3913 lwp->pending_signals.emplace_back (signal);
3914 if (info == nullptr)
3915 memset (&lwp->pending_signals.back ().info, 0, sizeof (siginfo_t));
3916 else
3917 lwp->pending_signals.back ().info = *info;
3920 void
3921 linux_process_target::install_software_single_step_breakpoints (lwp_info *lwp)
3923 struct thread_info *thread = get_lwp_thread (lwp);
3924 struct regcache *regcache = get_thread_regcache (thread, 1);
3926 scoped_restore_current_thread restore_thread;
3928 switch_to_thread (thread);
3929 std::vector<CORE_ADDR> next_pcs = low_get_next_pcs (regcache);
3931 for (CORE_ADDR pc : next_pcs)
3932 set_single_step_breakpoint (pc, current_ptid);
3936 linux_process_target::single_step (lwp_info* lwp)
3938 int step = 0;
3940 if (supports_hardware_single_step ())
3942 step = 1;
3944 else if (supports_software_single_step ())
3946 install_software_single_step_breakpoints (lwp);
3947 step = 0;
3949 else
3950 threads_debug_printf ("stepping is not implemented on this target");
3952 return step;
3955 /* The signal can be delivered to the inferior if we are not trying to
3956 finish a fast tracepoint collect. Since signal can be delivered in
3957 the step-over, the program may go to signal handler and trap again
3958 after return from the signal handler. We can live with the spurious
3959 double traps. */
3961 static int
3962 lwp_signal_can_be_delivered (struct lwp_info *lwp)
3964 return (lwp->collecting_fast_tracepoint
3965 == fast_tpoint_collect_result::not_collecting);
3968 void
3969 linux_process_target::resume_one_lwp_throw (lwp_info *lwp, int step,
3970 int signal, siginfo_t *info)
3972 struct thread_info *thread = get_lwp_thread (lwp);
3973 int ptrace_request;
3974 struct process_info *proc = get_thread_process (thread);
3976 /* Note that target description may not be initialised
3977 (proc->tdesc == NULL) at this point because the program hasn't
3978 stopped at the first instruction yet. It means GDBserver skips
3979 the extra traps from the wrapper program (see option --wrapper).
3980 Code in this function that requires register access should be
3981 guarded by proc->tdesc == NULL or something else. */
3983 if (lwp->stopped == 0)
3984 return;
3986 gdb_assert (lwp->waitstatus.kind () == TARGET_WAITKIND_IGNORE);
3988 fast_tpoint_collect_result fast_tp_collecting
3989 = lwp->collecting_fast_tracepoint;
3991 gdb_assert (!stabilizing_threads
3992 || (fast_tp_collecting
3993 != fast_tpoint_collect_result::not_collecting));
3995 /* Cancel actions that rely on GDB not changing the PC (e.g., the
3996 user used the "jump" command, or "set $pc = foo"). */
3997 if (thread->while_stepping != NULL && lwp->stop_pc != get_pc (lwp))
3999 /* Collecting 'while-stepping' actions doesn't make sense
4000 anymore. */
4001 release_while_stepping_state_list (thread);
4004 /* If we have pending signals or status, and a new signal, enqueue the
4005 signal. Also enqueue the signal if it can't be delivered to the
4006 inferior right now. */
4007 if (signal != 0
4008 && (lwp->status_pending_p
4009 || !lwp->pending_signals.empty ()
4010 || !lwp_signal_can_be_delivered (lwp)))
4012 enqueue_pending_signal (lwp, signal, info);
4014 /* Postpone any pending signal. It was enqueued above. */
4015 signal = 0;
4018 if (lwp->status_pending_p)
4020 threads_debug_printf
4021 ("Not resuming lwp %ld (%s, stop %s); has pending status",
4022 lwpid_of (thread), step ? "step" : "continue",
4023 lwp->stop_expected ? "expected" : "not expected");
4024 return;
4027 scoped_restore_current_thread restore_thread;
4028 switch_to_thread (thread);
4030 /* This bit needs some thinking about. If we get a signal that
4031 we must report while a single-step reinsert is still pending,
4032 we often end up resuming the thread. It might be better to
4033 (ew) allow a stack of pending events; then we could be sure that
4034 the reinsert happened right away and not lose any signals.
4036 Making this stack would also shrink the window in which breakpoints are
4037 uninserted (see comment in linux_wait_for_lwp) but not enough for
4038 complete correctness, so it won't solve that problem. It may be
4039 worthwhile just to solve this one, however. */
4040 if (lwp->bp_reinsert != 0)
4042 threads_debug_printf (" pending reinsert at 0x%s",
4043 paddress (lwp->bp_reinsert));
4045 if (supports_hardware_single_step ())
4047 if (fast_tp_collecting == fast_tpoint_collect_result::not_collecting)
4049 if (step == 0)
4050 warning ("BAD - reinserting but not stepping.");
4051 if (lwp->suspended)
4052 warning ("BAD - reinserting and suspended(%d).",
4053 lwp->suspended);
4057 step = maybe_hw_step (thread);
4060 if (fast_tp_collecting == fast_tpoint_collect_result::before_insn)
4061 threads_debug_printf
4062 ("lwp %ld wants to get out of fast tracepoint jump pad "
4063 "(exit-jump-pad-bkpt)", lwpid_of (thread));
4065 else if (fast_tp_collecting == fast_tpoint_collect_result::at_insn)
4067 threads_debug_printf
4068 ("lwp %ld wants to get out of fast tracepoint jump pad single-stepping",
4069 lwpid_of (thread));
4071 if (supports_hardware_single_step ())
4072 step = 1;
4073 else
4075 internal_error ("moving out of jump pad single-stepping"
4076 " not implemented on this target");
4080 /* If we have while-stepping actions in this thread set it stepping.
4081 If we have a signal to deliver, it may or may not be set to
4082 SIG_IGN, we don't know. Assume so, and allow collecting
4083 while-stepping into a signal handler. A possible smart thing to
4084 do would be to set an internal breakpoint at the signal return
4085 address, continue, and carry on catching this while-stepping
4086 action only when that breakpoint is hit. A future
4087 enhancement. */
4088 if (thread->while_stepping != NULL)
4090 threads_debug_printf
4091 ("lwp %ld has a while-stepping action -> forcing step.",
4092 lwpid_of (thread));
4094 step = single_step (lwp);
4097 if (proc->tdesc != NULL && low_supports_breakpoints ())
4099 struct regcache *regcache = get_thread_regcache (current_thread, 1);
4101 lwp->stop_pc = low_get_pc (regcache);
4103 threads_debug_printf (" %s from pc 0x%lx", step ? "step" : "continue",
4104 (long) lwp->stop_pc);
4107 /* If we have pending signals, consume one if it can be delivered to
4108 the inferior. */
4109 if (!lwp->pending_signals.empty () && lwp_signal_can_be_delivered (lwp))
4111 const pending_signal &p_sig = lwp->pending_signals.front ();
4113 signal = p_sig.signal;
4114 if (p_sig.info.si_signo != 0)
4115 ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
4116 &p_sig.info);
4118 lwp->pending_signals.pop_front ();
4121 threads_debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)",
4122 lwpid_of (thread), step ? "step" : "continue", signal,
4123 lwp->stop_expected ? "expected" : "not expected");
4125 low_prepare_to_resume (lwp);
4127 regcache_invalidate_thread (thread);
4128 errno = 0;
4129 lwp->stepping = step;
4130 if (step)
4131 ptrace_request = PTRACE_SINGLESTEP;
4132 else if (gdb_catching_syscalls_p (lwp))
4133 ptrace_request = PTRACE_SYSCALL;
4134 else
4135 ptrace_request = PTRACE_CONT;
4136 ptrace (ptrace_request,
4137 lwpid_of (thread),
4138 (PTRACE_TYPE_ARG3) 0,
4139 /* Coerce to a uintptr_t first to avoid potential gcc warning
4140 of coercing an 8 byte integer to a 4 byte pointer. */
4141 (PTRACE_TYPE_ARG4) (uintptr_t) signal);
4143 if (errno)
4145 int saved_errno = errno;
4147 threads_debug_printf ("ptrace errno = %d (%s)",
4148 saved_errno, strerror (saved_errno));
4150 errno = saved_errno;
4151 perror_with_name ("resuming thread");
4154 /* Successfully resumed. Clear state that no longer makes sense,
4155 and mark the LWP as running. Must not do this before resuming
4156 otherwise if that fails other code will be confused. E.g., we'd
4157 later try to stop the LWP and hang forever waiting for a stop
4158 status. Note that we must not throw after this is cleared,
4159 otherwise handle_zombie_lwp_error would get confused. */
4160 lwp->stopped = 0;
4161 lwp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4164 void
4165 linux_process_target::low_prepare_to_resume (lwp_info *lwp)
4167 /* Nop. */
4170 /* Called when we try to resume a stopped LWP and that errors out. If
4171 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4172 or about to become), discard the error, clear any pending status
4173 the LWP may have, and return true (we'll collect the exit status
4174 soon enough). Otherwise, return false. */
4176 static int
4177 check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
4179 struct thread_info *thread = get_lwp_thread (lp);
4181 /* If we get an error after resuming the LWP successfully, we'd
4182 confuse !T state for the LWP being gone. */
4183 gdb_assert (lp->stopped);
4185 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4186 because even if ptrace failed with ESRCH, the tracee may be "not
4187 yet fully dead", but already refusing ptrace requests. In that
4188 case the tracee has 'R (Running)' state for a little bit
4189 (observed in Linux 3.18). See also the note on ESRCH in the
4190 ptrace(2) man page. Instead, check whether the LWP has any state
4191 other than ptrace-stopped. */
4193 /* Don't assume anything if /proc/PID/status can't be read. */
4194 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread)) == 0)
4196 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4197 lp->status_pending_p = 0;
4198 return 1;
4200 return 0;
4203 void
4204 linux_process_target::resume_one_lwp (lwp_info *lwp, int step, int signal,
4205 siginfo_t *info)
4209 resume_one_lwp_throw (lwp, step, signal, info);
4211 catch (const gdb_exception_error &ex)
4213 if (check_ptrace_stopped_lwp_gone (lwp))
4215 /* This could because we tried to resume an LWP after its leader
4216 exited. Mark it as resumed, so we can collect an exit event
4217 from it. */
4218 lwp->stopped = 0;
4219 lwp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4221 else
4222 throw;
4226 /* This function is called once per thread via for_each_thread.
4227 We look up which resume request applies to THREAD and mark it with a
4228 pointer to the appropriate resume request.
4230 This algorithm is O(threads * resume elements), but resume elements
4231 is small (and will remain small at least until GDB supports thread
4232 suspension). */
4234 static void
4235 linux_set_resume_request (thread_info *thread, thread_resume *resume, size_t n)
4237 struct lwp_info *lwp = get_thread_lwp (thread);
4239 for (int ndx = 0; ndx < n; ndx++)
4241 ptid_t ptid = resume[ndx].thread;
4242 if (ptid == minus_one_ptid
4243 || ptid == thread->id
4244 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4245 of PID'. */
4246 || (ptid.pid () == pid_of (thread)
4247 && (ptid.is_pid ()
4248 || ptid.lwp () == -1)))
4250 if (resume[ndx].kind == resume_stop
4251 && thread->last_resume_kind == resume_stop)
4253 threads_debug_printf
4254 ("already %s LWP %ld at GDB's request",
4255 (thread->last_status.kind () == TARGET_WAITKIND_STOPPED
4256 ? "stopped" : "stopping"),
4257 lwpid_of (thread));
4259 continue;
4262 /* Ignore (wildcard) resume requests for already-resumed
4263 threads. */
4264 if (resume[ndx].kind != resume_stop
4265 && thread->last_resume_kind != resume_stop)
4267 threads_debug_printf
4268 ("already %s LWP %ld at GDB's request",
4269 (thread->last_resume_kind == resume_step
4270 ? "stepping" : "continuing"),
4271 lwpid_of (thread));
4272 continue;
4275 /* Don't let wildcard resumes resume fork children that GDB
4276 does not yet know are new fork children. */
4277 if (lwp->fork_relative != NULL)
4279 struct lwp_info *rel = lwp->fork_relative;
4281 if (rel->status_pending_p
4282 && (rel->waitstatus.kind () == TARGET_WAITKIND_FORKED
4283 || rel->waitstatus.kind () == TARGET_WAITKIND_VFORKED))
4285 threads_debug_printf
4286 ("not resuming LWP %ld: has queued stop reply",
4287 lwpid_of (thread));
4288 continue;
4292 /* If the thread has a pending event that has already been
4293 reported to GDBserver core, but GDB has not pulled the
4294 event out of the vStopped queue yet, likewise, ignore the
4295 (wildcard) resume request. */
4296 if (in_queued_stop_replies (thread->id))
4298 threads_debug_printf
4299 ("not resuming LWP %ld: has queued stop reply",
4300 lwpid_of (thread));
4301 continue;
4304 lwp->resume = &resume[ndx];
4305 thread->last_resume_kind = lwp->resume->kind;
4307 lwp->step_range_start = lwp->resume->step_range_start;
4308 lwp->step_range_end = lwp->resume->step_range_end;
4310 /* If we had a deferred signal to report, dequeue one now.
4311 This can happen if LWP gets more than one signal while
4312 trying to get out of a jump pad. */
4313 if (lwp->stopped
4314 && !lwp->status_pending_p
4315 && dequeue_one_deferred_signal (lwp, &lwp->status_pending))
4317 lwp->status_pending_p = 1;
4319 threads_debug_printf
4320 ("Dequeueing deferred signal %d for LWP %ld, "
4321 "leaving status pending.",
4322 WSTOPSIG (lwp->status_pending),
4323 lwpid_of (thread));
4326 return;
4330 /* No resume action for this thread. */
4331 lwp->resume = NULL;
4334 bool
4335 linux_process_target::resume_status_pending (thread_info *thread)
4337 struct lwp_info *lwp = get_thread_lwp (thread);
4339 /* LWPs which will not be resumed are not interesting, because
4340 we might not wait for them next time through linux_wait. */
4341 if (lwp->resume == NULL)
4342 return false;
4344 return thread_still_has_status_pending (thread);
4347 bool
4348 linux_process_target::thread_needs_step_over (thread_info *thread)
4350 struct lwp_info *lwp = get_thread_lwp (thread);
4351 CORE_ADDR pc;
4352 struct process_info *proc = get_thread_process (thread);
4354 /* GDBserver is skipping the extra traps from the wrapper program,
4355 don't have to do step over. */
4356 if (proc->tdesc == NULL)
4357 return false;
4359 /* LWPs which will not be resumed are not interesting, because we
4360 might not wait for them next time through linux_wait. */
4362 if (!lwp->stopped)
4364 threads_debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped",
4365 lwpid_of (thread));
4366 return false;
4369 if (thread->last_resume_kind == resume_stop)
4371 threads_debug_printf
4372 ("Need step over [LWP %ld]? Ignoring, should remain stopped",
4373 lwpid_of (thread));
4374 return false;
4377 gdb_assert (lwp->suspended >= 0);
4379 if (lwp->suspended)
4381 threads_debug_printf ("Need step over [LWP %ld]? Ignoring, suspended",
4382 lwpid_of (thread));
4383 return false;
4386 if (lwp->status_pending_p)
4388 threads_debug_printf
4389 ("Need step over [LWP %ld]? Ignoring, has pending status.",
4390 lwpid_of (thread));
4391 return false;
4394 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4395 or we have. */
4396 pc = get_pc (lwp);
4398 /* If the PC has changed since we stopped, then don't do anything,
4399 and let the breakpoint/tracepoint be hit. This happens if, for
4400 instance, GDB handled the decr_pc_after_break subtraction itself,
4401 GDB is OOL stepping this thread, or the user has issued a "jump"
4402 command, or poked thread's registers herself. */
4403 if (pc != lwp->stop_pc)
4405 threads_debug_printf
4406 ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4407 "Old stop_pc was 0x%s, PC is now 0x%s", lwpid_of (thread),
4408 paddress (lwp->stop_pc), paddress (pc));
4409 return false;
4412 /* On software single step target, resume the inferior with signal
4413 rather than stepping over. */
4414 if (supports_software_single_step ()
4415 && !lwp->pending_signals.empty ()
4416 && lwp_signal_can_be_delivered (lwp))
4418 threads_debug_printf
4419 ("Need step over [LWP %ld]? Ignoring, has pending signals.",
4420 lwpid_of (thread));
4422 return false;
4425 scoped_restore_current_thread restore_thread;
4426 switch_to_thread (thread);
4428 /* We can only step over breakpoints we know about. */
4429 if (breakpoint_here (pc) || fast_tracepoint_jump_here (pc))
4431 /* Don't step over a breakpoint that GDB expects to hit
4432 though. If the condition is being evaluated on the target's side
4433 and it evaluate to false, step over this breakpoint as well. */
4434 if (gdb_breakpoint_here (pc)
4435 && gdb_condition_true_at_breakpoint (pc)
4436 && gdb_no_commands_at_breakpoint (pc))
4438 threads_debug_printf ("Need step over [LWP %ld]? yes, but found"
4439 " GDB breakpoint at 0x%s; skipping step over",
4440 lwpid_of (thread), paddress (pc));
4442 return false;
4444 else
4446 threads_debug_printf ("Need step over [LWP %ld]? yes, "
4447 "found breakpoint at 0x%s",
4448 lwpid_of (thread), paddress (pc));
4450 /* We've found an lwp that needs stepping over --- return 1 so
4451 that find_thread stops looking. */
4452 return true;
4456 threads_debug_printf
4457 ("Need step over [LWP %ld]? No, no breakpoint found at 0x%s",
4458 lwpid_of (thread), paddress (pc));
4460 return false;
4463 void
4464 linux_process_target::start_step_over (lwp_info *lwp)
4466 struct thread_info *thread = get_lwp_thread (lwp);
4467 CORE_ADDR pc;
4469 threads_debug_printf ("Starting step-over on LWP %ld. Stopping all threads",
4470 lwpid_of (thread));
4472 stop_all_lwps (1, lwp);
4474 if (lwp->suspended != 0)
4476 internal_error ("LWP %ld suspended=%d\n", lwpid_of (thread),
4477 lwp->suspended);
4480 threads_debug_printf ("Done stopping all threads for step-over.");
4482 /* Note, we should always reach here with an already adjusted PC,
4483 either by GDB (if we're resuming due to GDB's request), or by our
4484 caller, if we just finished handling an internal breakpoint GDB
4485 shouldn't care about. */
4486 pc = get_pc (lwp);
4488 bool step = false;
4490 scoped_restore_current_thread restore_thread;
4491 switch_to_thread (thread);
4493 lwp->bp_reinsert = pc;
4494 uninsert_breakpoints_at (pc);
4495 uninsert_fast_tracepoint_jumps_at (pc);
4497 step = single_step (lwp);
4500 resume_one_lwp (lwp, step, 0, NULL);
4502 /* Require next event from this LWP. */
4503 step_over_bkpt = thread->id;
4506 bool
4507 linux_process_target::finish_step_over (lwp_info *lwp)
4509 if (lwp->bp_reinsert != 0)
4511 scoped_restore_current_thread restore_thread;
4513 threads_debug_printf ("Finished step over.");
4515 switch_to_thread (get_lwp_thread (lwp));
4517 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4518 may be no breakpoint to reinsert there by now. */
4519 reinsert_breakpoints_at (lwp->bp_reinsert);
4520 reinsert_fast_tracepoint_jumps_at (lwp->bp_reinsert);
4522 lwp->bp_reinsert = 0;
4524 /* Delete any single-step breakpoints. No longer needed. We
4525 don't have to worry about other threads hitting this trap,
4526 and later not being able to explain it, because we were
4527 stepping over a breakpoint, and we hold all threads but
4528 LWP stopped while doing that. */
4529 if (!supports_hardware_single_step ())
4531 gdb_assert (has_single_step_breakpoints (current_thread));
4532 delete_single_step_breakpoints (current_thread);
4535 step_over_bkpt = null_ptid;
4536 return true;
4538 else
4539 return false;
4542 void
4543 linux_process_target::complete_ongoing_step_over ()
4545 if (step_over_bkpt != null_ptid)
4547 struct lwp_info *lwp;
4548 int wstat;
4549 int ret;
4551 threads_debug_printf ("detach: step over in progress, finish it first");
4553 /* Passing NULL_PTID as filter indicates we want all events to
4554 be left pending. Eventually this returns when there are no
4555 unwaited-for children left. */
4556 ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat,
4557 __WALL);
4558 gdb_assert (ret == -1);
4560 lwp = find_lwp_pid (step_over_bkpt);
4561 if (lwp != NULL)
4563 finish_step_over (lwp);
4565 /* If we got our step SIGTRAP, don't leave it pending,
4566 otherwise we would report it to GDB as a spurious
4567 SIGTRAP. */
4568 gdb_assert (lwp->status_pending_p);
4569 if (WIFSTOPPED (lwp->status_pending)
4570 && WSTOPSIG (lwp->status_pending) == SIGTRAP)
4572 thread_info *thread = get_lwp_thread (lwp);
4573 if (thread->last_resume_kind != resume_step)
4575 threads_debug_printf ("detach: discard step-over SIGTRAP");
4577 lwp->status_pending_p = 0;
4578 lwp->status_pending = 0;
4579 resume_one_lwp (lwp, lwp->stepping, 0, NULL);
4581 else
4582 threads_debug_printf
4583 ("detach: resume_step, not discarding step-over SIGTRAP");
4586 step_over_bkpt = null_ptid;
4587 unsuspend_all_lwps (lwp);
4591 void
4592 linux_process_target::resume_one_thread (thread_info *thread,
4593 bool leave_all_stopped)
4595 struct lwp_info *lwp = get_thread_lwp (thread);
4596 int leave_pending;
4598 if (lwp->resume == NULL)
4599 return;
4601 if (lwp->resume->kind == resume_stop)
4603 threads_debug_printf ("resume_stop request for LWP %ld",
4604 lwpid_of (thread));
4606 if (!lwp->stopped)
4608 threads_debug_printf ("stopping LWP %ld", lwpid_of (thread));
4610 /* Stop the thread, and wait for the event asynchronously,
4611 through the event loop. */
4612 send_sigstop (lwp);
4614 else
4616 threads_debug_printf ("already stopped LWP %ld", lwpid_of (thread));
4618 /* The LWP may have been stopped in an internal event that
4619 was not meant to be notified back to GDB (e.g., gdbserver
4620 breakpoint), so we should be reporting a stop event in
4621 this case too. */
4623 /* If the thread already has a pending SIGSTOP, this is a
4624 no-op. Otherwise, something later will presumably resume
4625 the thread and this will cause it to cancel any pending
4626 operation, due to last_resume_kind == resume_stop. If
4627 the thread already has a pending status to report, we
4628 will still report it the next time we wait - see
4629 status_pending_p_callback. */
4631 /* If we already have a pending signal to report, then
4632 there's no need to queue a SIGSTOP, as this means we're
4633 midway through moving the LWP out of the jumppad, and we
4634 will report the pending signal as soon as that is
4635 finished. */
4636 if (lwp->pending_signals_to_report.empty ())
4637 send_sigstop (lwp);
4640 /* For stop requests, we're done. */
4641 lwp->resume = NULL;
4642 thread->last_status.set_ignore ();
4643 return;
4646 /* If this thread which is about to be resumed has a pending status,
4647 then don't resume it - we can just report the pending status.
4648 Likewise if it is suspended, because e.g., another thread is
4649 stepping past a breakpoint. Make sure to queue any signals that
4650 would otherwise be sent. In all-stop mode, we do this decision
4651 based on if *any* thread has a pending status. If there's a
4652 thread that needs the step-over-breakpoint dance, then don't
4653 resume any other thread but that particular one. */
4654 leave_pending = (lwp->suspended
4655 || lwp->status_pending_p
4656 || leave_all_stopped);
4658 /* If we have a new signal, enqueue the signal. */
4659 if (lwp->resume->sig != 0)
4661 siginfo_t info, *info_p;
4663 /* If this is the same signal we were previously stopped by,
4664 make sure to queue its siginfo. */
4665 if (WIFSTOPPED (lwp->last_status)
4666 && WSTOPSIG (lwp->last_status) == lwp->resume->sig
4667 && ptrace (PTRACE_GETSIGINFO, lwpid_of (thread),
4668 (PTRACE_TYPE_ARG3) 0, &info) == 0)
4669 info_p = &info;
4670 else
4671 info_p = NULL;
4673 enqueue_pending_signal (lwp, lwp->resume->sig, info_p);
4676 if (!leave_pending)
4678 threads_debug_printf ("resuming LWP %ld", lwpid_of (thread));
4680 proceed_one_lwp (thread, NULL);
4682 else
4683 threads_debug_printf ("leaving LWP %ld stopped", lwpid_of (thread));
4685 thread->last_status.set_ignore ();
4686 lwp->resume = NULL;
4689 void
4690 linux_process_target::resume (thread_resume *resume_info, size_t n)
4692 struct thread_info *need_step_over = NULL;
4694 THREADS_SCOPED_DEBUG_ENTER_EXIT;
4696 for_each_thread ([&] (thread_info *thread)
4698 linux_set_resume_request (thread, resume_info, n);
4701 /* If there is a thread which would otherwise be resumed, which has
4702 a pending status, then don't resume any threads - we can just
4703 report the pending status. Make sure to queue any signals that
4704 would otherwise be sent. In non-stop mode, we'll apply this
4705 logic to each thread individually. We consume all pending events
4706 before considering to start a step-over (in all-stop). */
4707 bool any_pending = false;
4708 if (!non_stop)
4709 any_pending = find_thread ([this] (thread_info *thread)
4711 return resume_status_pending (thread);
4712 }) != nullptr;
4714 /* If there is a thread which would otherwise be resumed, which is
4715 stopped at a breakpoint that needs stepping over, then don't
4716 resume any threads - have it step over the breakpoint with all
4717 other threads stopped, then resume all threads again. Make sure
4718 to queue any signals that would otherwise be delivered or
4719 queued. */
4720 if (!any_pending && low_supports_breakpoints ())
4721 need_step_over = find_thread ([this] (thread_info *thread)
4723 return thread_needs_step_over (thread);
4726 bool leave_all_stopped = (need_step_over != NULL || any_pending);
4728 if (need_step_over != NULL)
4729 threads_debug_printf ("Not resuming all, need step over");
4730 else if (any_pending)
4731 threads_debug_printf ("Not resuming, all-stop and found "
4732 "an LWP with pending status");
4733 else
4734 threads_debug_printf ("Resuming, no pending status or step over needed");
4736 /* Even if we're leaving threads stopped, queue all signals we'd
4737 otherwise deliver. */
4738 for_each_thread ([&] (thread_info *thread)
4740 resume_one_thread (thread, leave_all_stopped);
4743 if (need_step_over)
4744 start_step_over (get_thread_lwp (need_step_over));
4746 /* We may have events that were pending that can/should be sent to
4747 the client now. Trigger a linux_wait call. */
4748 if (target_is_async_p ())
4749 async_file_mark ();
4752 void
4753 linux_process_target::proceed_one_lwp (thread_info *thread, lwp_info *except)
4755 struct lwp_info *lwp = get_thread_lwp (thread);
4756 int step;
4758 if (lwp == except)
4759 return;
4761 threads_debug_printf ("lwp %ld", lwpid_of (thread));
4763 if (!lwp->stopped)
4765 threads_debug_printf (" LWP %ld already running", lwpid_of (thread));
4766 return;
4769 if (thread->last_resume_kind == resume_stop
4770 && thread->last_status.kind () != TARGET_WAITKIND_IGNORE)
4772 threads_debug_printf (" client wants LWP to remain %ld stopped",
4773 lwpid_of (thread));
4774 return;
4777 if (lwp->status_pending_p)
4779 threads_debug_printf (" LWP %ld has pending status, leaving stopped",
4780 lwpid_of (thread));
4781 return;
4784 gdb_assert (lwp->suspended >= 0);
4786 if (lwp->suspended)
4788 threads_debug_printf (" LWP %ld is suspended", lwpid_of (thread));
4789 return;
4792 if (thread->last_resume_kind == resume_stop
4793 && lwp->pending_signals_to_report.empty ()
4794 && (lwp->collecting_fast_tracepoint
4795 == fast_tpoint_collect_result::not_collecting))
4797 /* We haven't reported this LWP as stopped yet (otherwise, the
4798 last_status.kind check above would catch it, and we wouldn't
4799 reach here. This LWP may have been momentarily paused by a
4800 stop_all_lwps call while handling for example, another LWP's
4801 step-over. In that case, the pending expected SIGSTOP signal
4802 that was queued at vCont;t handling time will have already
4803 been consumed by wait_for_sigstop, and so we need to requeue
4804 another one here. Note that if the LWP already has a SIGSTOP
4805 pending, this is a no-op. */
4807 threads_debug_printf
4808 ("Client wants LWP %ld to stop. Making sure it has a SIGSTOP pending",
4809 lwpid_of (thread));
4811 send_sigstop (lwp);
4814 if (thread->last_resume_kind == resume_step)
4816 threads_debug_printf (" stepping LWP %ld, client wants it stepping",
4817 lwpid_of (thread));
4819 /* If resume_step is requested by GDB, install single-step
4820 breakpoints when the thread is about to be actually resumed if
4821 the single-step breakpoints weren't removed. */
4822 if (supports_software_single_step ()
4823 && !has_single_step_breakpoints (thread))
4824 install_software_single_step_breakpoints (lwp);
4826 step = maybe_hw_step (thread);
4828 else if (lwp->bp_reinsert != 0)
4830 threads_debug_printf (" stepping LWP %ld, reinsert set",
4831 lwpid_of (thread));
4833 step = maybe_hw_step (thread);
4835 else
4836 step = 0;
4838 resume_one_lwp (lwp, step, 0, NULL);
4841 void
4842 linux_process_target::unsuspend_and_proceed_one_lwp (thread_info *thread,
4843 lwp_info *except)
4845 struct lwp_info *lwp = get_thread_lwp (thread);
4847 if (lwp == except)
4848 return;
4850 lwp_suspended_decr (lwp);
4852 proceed_one_lwp (thread, except);
4855 void
4856 linux_process_target::proceed_all_lwps ()
4858 struct thread_info *need_step_over;
4860 /* If there is a thread which would otherwise be resumed, which is
4861 stopped at a breakpoint that needs stepping over, then don't
4862 resume any threads - have it step over the breakpoint with all
4863 other threads stopped, then resume all threads again. */
4865 if (low_supports_breakpoints ())
4867 need_step_over = find_thread ([this] (thread_info *thread)
4869 return thread_needs_step_over (thread);
4872 if (need_step_over != NULL)
4874 threads_debug_printf ("found thread %ld needing a step-over",
4875 lwpid_of (need_step_over));
4877 start_step_over (get_thread_lwp (need_step_over));
4878 return;
4882 threads_debug_printf ("Proceeding, no step-over needed");
4884 for_each_thread ([this] (thread_info *thread)
4886 proceed_one_lwp (thread, NULL);
4890 void
4891 linux_process_target::unstop_all_lwps (int unsuspend, lwp_info *except)
4893 THREADS_SCOPED_DEBUG_ENTER_EXIT;
4895 if (except)
4896 threads_debug_printf ("except=(LWP %ld)",
4897 lwpid_of (get_lwp_thread (except)));
4898 else
4899 threads_debug_printf ("except=nullptr");
4901 if (unsuspend)
4902 for_each_thread ([&] (thread_info *thread)
4904 unsuspend_and_proceed_one_lwp (thread, except);
4906 else
4907 for_each_thread ([&] (thread_info *thread)
4909 proceed_one_lwp (thread, except);
4914 #ifdef HAVE_LINUX_REGSETS
4916 #define use_linux_regsets 1
4918 /* Returns true if REGSET has been disabled. */
4920 static int
4921 regset_disabled (struct regsets_info *info, struct regset_info *regset)
4923 return (info->disabled_regsets != NULL
4924 && info->disabled_regsets[regset - info->regsets]);
4927 /* Disable REGSET. */
4929 static void
4930 disable_regset (struct regsets_info *info, struct regset_info *regset)
4932 int dr_offset;
4934 dr_offset = regset - info->regsets;
4935 if (info->disabled_regsets == NULL)
4936 info->disabled_regsets = (char *) xcalloc (1, info->num_regsets);
4937 info->disabled_regsets[dr_offset] = 1;
4940 static int
4941 regsets_fetch_inferior_registers (struct regsets_info *regsets_info,
4942 struct regcache *regcache)
4944 struct regset_info *regset;
4945 int saw_general_regs = 0;
4946 int pid;
4947 struct iovec iov;
4949 pid = lwpid_of (current_thread);
4950 for (regset = regsets_info->regsets; regset->size >= 0; regset++)
4952 void *buf, *data;
4953 int nt_type, res;
4955 if (regset->size == 0 || regset_disabled (regsets_info, regset))
4956 continue;
4958 buf = xmalloc (regset->size);
4960 nt_type = regset->nt_type;
4961 if (nt_type)
4963 iov.iov_base = buf;
4964 iov.iov_len = regset->size;
4965 data = (void *) &iov;
4967 else
4968 data = buf;
4970 #ifndef __sparc__
4971 res = ptrace (regset->get_request, pid,
4972 (PTRACE_TYPE_ARG3) (long) nt_type, data);
4973 #else
4974 res = ptrace (regset->get_request, pid, data, nt_type);
4975 #endif
4976 if (res < 0)
4978 if (errno == EIO
4979 || (errno == EINVAL && regset->type == OPTIONAL_REGS))
4981 /* If we get EIO on a regset, or an EINVAL and the regset is
4982 optional, do not try it again for this process mode. */
4983 disable_regset (regsets_info, regset);
4985 else if (errno == ENODATA)
4987 /* ENODATA may be returned if the regset is currently
4988 not "active". This can happen in normal operation,
4989 so suppress the warning in this case. */
4991 else if (errno == ESRCH)
4993 /* At this point, ESRCH should mean the process is
4994 already gone, in which case we simply ignore attempts
4995 to read its registers. */
4997 else
4999 char s[256];
5000 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
5001 pid);
5002 perror (s);
5005 else
5007 if (regset->type == GENERAL_REGS)
5008 saw_general_regs = 1;
5009 regset->store_function (regcache, buf);
5011 free (buf);
5013 if (saw_general_regs)
5014 return 0;
5015 else
5016 return 1;
5019 static int
5020 regsets_store_inferior_registers (struct regsets_info *regsets_info,
5021 struct regcache *regcache)
5023 struct regset_info *regset;
5024 int saw_general_regs = 0;
5025 int pid;
5026 struct iovec iov;
5028 pid = lwpid_of (current_thread);
5029 for (regset = regsets_info->regsets; regset->size >= 0; regset++)
5031 void *buf, *data;
5032 int nt_type, res;
5034 if (regset->size == 0 || regset_disabled (regsets_info, regset)
5035 || regset->fill_function == NULL)
5036 continue;
5038 buf = xmalloc (regset->size);
5040 /* First fill the buffer with the current register set contents,
5041 in case there are any items in the kernel's regset that are
5042 not in gdbserver's regcache. */
5044 nt_type = regset->nt_type;
5045 if (nt_type)
5047 iov.iov_base = buf;
5048 iov.iov_len = regset->size;
5049 data = (void *) &iov;
5051 else
5052 data = buf;
5054 #ifndef __sparc__
5055 res = ptrace (regset->get_request, pid,
5056 (PTRACE_TYPE_ARG3) (long) nt_type, data);
5057 #else
5058 res = ptrace (regset->get_request, pid, data, nt_type);
5059 #endif
5061 if (res == 0)
5063 /* Then overlay our cached registers on that. */
5064 regset->fill_function (regcache, buf);
5066 /* Only now do we write the register set. */
5067 #ifndef __sparc__
5068 res = ptrace (regset->set_request, pid,
5069 (PTRACE_TYPE_ARG3) (long) nt_type, data);
5070 #else
5071 res = ptrace (regset->set_request, pid, data, nt_type);
5072 #endif
5075 if (res < 0)
5077 if (errno == EIO
5078 || (errno == EINVAL && regset->type == OPTIONAL_REGS))
5080 /* If we get EIO on a regset, or an EINVAL and the regset is
5081 optional, do not try it again for this process mode. */
5082 disable_regset (regsets_info, regset);
5084 else if (errno == ESRCH)
5086 /* At this point, ESRCH should mean the process is
5087 already gone, in which case we simply ignore attempts
5088 to change its registers. See also the related
5089 comment in resume_one_lwp. */
5090 free (buf);
5091 return 0;
5093 else
5095 perror ("Warning: ptrace(regsets_store_inferior_registers)");
5098 else if (regset->type == GENERAL_REGS)
5099 saw_general_regs = 1;
5100 free (buf);
5102 if (saw_general_regs)
5103 return 0;
5104 else
5105 return 1;
5108 #else /* !HAVE_LINUX_REGSETS */
5110 #define use_linux_regsets 0
5111 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5112 #define regsets_store_inferior_registers(regsets_info, regcache) 1
5114 #endif
5116 /* Return 1 if register REGNO is supported by one of the regset ptrace
5117 calls or 0 if it has to be transferred individually. */
5119 static int
5120 linux_register_in_regsets (const struct regs_info *regs_info, int regno)
5122 unsigned char mask = 1 << (regno % 8);
5123 size_t index = regno / 8;
5125 return (use_linux_regsets
5126 && (regs_info->regset_bitmap == NULL
5127 || (regs_info->regset_bitmap[index] & mask) != 0));
5130 #ifdef HAVE_LINUX_USRREGS
5132 static int
5133 register_addr (const struct usrregs_info *usrregs, int regnum)
5135 int addr;
5137 if (regnum < 0 || regnum >= usrregs->num_regs)
5138 error ("Invalid register number %d.", regnum);
5140 addr = usrregs->regmap[regnum];
5142 return addr;
5146 void
5147 linux_process_target::fetch_register (const usrregs_info *usrregs,
5148 regcache *regcache, int regno)
5150 CORE_ADDR regaddr;
5151 int i, size;
5152 char *buf;
5153 int pid;
5155 if (regno >= usrregs->num_regs)
5156 return;
5157 if (low_cannot_fetch_register (regno))
5158 return;
5160 regaddr = register_addr (usrregs, regno);
5161 if (regaddr == -1)
5162 return;
5164 size = ((register_size (regcache->tdesc, regno)
5165 + sizeof (PTRACE_XFER_TYPE) - 1)
5166 & -sizeof (PTRACE_XFER_TYPE));
5167 buf = (char *) alloca (size);
5169 pid = lwpid_of (current_thread);
5170 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
5172 errno = 0;
5173 *(PTRACE_XFER_TYPE *) (buf + i) =
5174 ptrace (PTRACE_PEEKUSER, pid,
5175 /* Coerce to a uintptr_t first to avoid potential gcc warning
5176 of coercing an 8 byte integer to a 4 byte pointer. */
5177 (PTRACE_TYPE_ARG3) (uintptr_t) regaddr, (PTRACE_TYPE_ARG4) 0);
5178 regaddr += sizeof (PTRACE_XFER_TYPE);
5179 if (errno != 0)
5181 /* Mark register REGNO unavailable. */
5182 supply_register (regcache, regno, NULL);
5183 return;
5187 low_supply_ptrace_register (regcache, regno, buf);
5190 void
5191 linux_process_target::store_register (const usrregs_info *usrregs,
5192 regcache *regcache, int regno)
5194 CORE_ADDR regaddr;
5195 int i, size;
5196 char *buf;
5197 int pid;
5199 if (regno >= usrregs->num_regs)
5200 return;
5201 if (low_cannot_store_register (regno))
5202 return;
5204 regaddr = register_addr (usrregs, regno);
5205 if (regaddr == -1)
5206 return;
5208 size = ((register_size (regcache->tdesc, regno)
5209 + sizeof (PTRACE_XFER_TYPE) - 1)
5210 & -sizeof (PTRACE_XFER_TYPE));
5211 buf = (char *) alloca (size);
5212 memset (buf, 0, size);
5214 low_collect_ptrace_register (regcache, regno, buf);
5216 pid = lwpid_of (current_thread);
5217 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
5219 errno = 0;
5220 ptrace (PTRACE_POKEUSER, pid,
5221 /* Coerce to a uintptr_t first to avoid potential gcc warning
5222 about coercing an 8 byte integer to a 4 byte pointer. */
5223 (PTRACE_TYPE_ARG3) (uintptr_t) regaddr,
5224 (PTRACE_TYPE_ARG4) *(PTRACE_XFER_TYPE *) (buf + i));
5225 if (errno != 0)
5227 /* At this point, ESRCH should mean the process is
5228 already gone, in which case we simply ignore attempts
5229 to change its registers. See also the related
5230 comment in resume_one_lwp. */
5231 if (errno == ESRCH)
5232 return;
5235 if (!low_cannot_store_register (regno))
5236 error ("writing register %d: %s", regno, safe_strerror (errno));
5238 regaddr += sizeof (PTRACE_XFER_TYPE);
5241 #endif /* HAVE_LINUX_USRREGS */
5243 void
5244 linux_process_target::low_collect_ptrace_register (regcache *regcache,
5245 int regno, char *buf)
5247 collect_register (regcache, regno, buf);
5250 void
5251 linux_process_target::low_supply_ptrace_register (regcache *regcache,
5252 int regno, const char *buf)
5254 supply_register (regcache, regno, buf);
5257 void
5258 linux_process_target::usr_fetch_inferior_registers (const regs_info *regs_info,
5259 regcache *regcache,
5260 int regno, int all)
5262 #ifdef HAVE_LINUX_USRREGS
5263 struct usrregs_info *usr = regs_info->usrregs;
5265 if (regno == -1)
5267 for (regno = 0; regno < usr->num_regs; regno++)
5268 if (all || !linux_register_in_regsets (regs_info, regno))
5269 fetch_register (usr, regcache, regno);
5271 else
5272 fetch_register (usr, regcache, regno);
5273 #endif
5276 void
5277 linux_process_target::usr_store_inferior_registers (const regs_info *regs_info,
5278 regcache *regcache,
5279 int regno, int all)
5281 #ifdef HAVE_LINUX_USRREGS
5282 struct usrregs_info *usr = regs_info->usrregs;
5284 if (regno == -1)
5286 for (regno = 0; regno < usr->num_regs; regno++)
5287 if (all || !linux_register_in_regsets (regs_info, regno))
5288 store_register (usr, regcache, regno);
5290 else
5291 store_register (usr, regcache, regno);
5292 #endif
5295 void
5296 linux_process_target::fetch_registers (regcache *regcache, int regno)
5298 int use_regsets;
5299 int all = 0;
5300 const regs_info *regs_info = get_regs_info ();
5302 if (regno == -1)
5304 if (regs_info->usrregs != NULL)
5305 for (regno = 0; regno < regs_info->usrregs->num_regs; regno++)
5306 low_fetch_register (regcache, regno);
5308 all = regsets_fetch_inferior_registers (regs_info->regsets_info, regcache);
5309 if (regs_info->usrregs != NULL)
5310 usr_fetch_inferior_registers (regs_info, regcache, -1, all);
5312 else
5314 if (low_fetch_register (regcache, regno))
5315 return;
5317 use_regsets = linux_register_in_regsets (regs_info, regno);
5318 if (use_regsets)
5319 all = regsets_fetch_inferior_registers (regs_info->regsets_info,
5320 regcache);
5321 if ((!use_regsets || all) && regs_info->usrregs != NULL)
5322 usr_fetch_inferior_registers (regs_info, regcache, regno, 1);
5326 void
5327 linux_process_target::store_registers (regcache *regcache, int regno)
5329 int use_regsets;
5330 int all = 0;
5331 const regs_info *regs_info = get_regs_info ();
5333 if (regno == -1)
5335 all = regsets_store_inferior_registers (regs_info->regsets_info,
5336 regcache);
5337 if (regs_info->usrregs != NULL)
5338 usr_store_inferior_registers (regs_info, regcache, regno, all);
5340 else
5342 use_regsets = linux_register_in_regsets (regs_info, regno);
5343 if (use_regsets)
5344 all = regsets_store_inferior_registers (regs_info->regsets_info,
5345 regcache);
5346 if ((!use_regsets || all) && regs_info->usrregs != NULL)
5347 usr_store_inferior_registers (regs_info, regcache, regno, 1);
5351 bool
5352 linux_process_target::low_fetch_register (regcache *regcache, int regno)
5354 return false;
5357 /* A wrapper for the read_memory target op. */
5359 static int
5360 linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
5362 return the_target->read_memory (memaddr, myaddr, len);
5366 /* Helper for read_memory/write_memory using /proc/PID/mem. Because
5367 we can use a single read/write call, this can be much more
5368 efficient than banging away at PTRACE_PEEKTEXT. Also, unlike
5369 PTRACE_PEEKTEXT/PTRACE_POKETEXT, this works with running threads.
5370 One an only one of READBUF and WRITEBUF is non-null. If READBUF is
5371 not null, then we're reading, otherwise we're writing. */
5373 static int
5374 proc_xfer_memory (CORE_ADDR memaddr, unsigned char *readbuf,
5375 const gdb_byte *writebuf, int len)
5377 gdb_assert ((readbuf == nullptr) != (writebuf == nullptr));
5379 process_info *proc = current_process ();
5381 int fd = proc->priv->mem_fd;
5382 if (fd == -1)
5383 return EIO;
5385 while (len > 0)
5387 int bytes;
5389 /* Use pread64/pwrite64 if available, since they save a syscall
5390 and can handle 64-bit offsets even on 32-bit platforms (for
5391 instance, SPARC debugging a SPARC64 application). But only
5392 use them if the offset isn't so high that when cast to off_t
5393 it'd be negative, as seen on SPARC64. pread64/pwrite64
5394 outright reject such offsets. lseek does not. */
5395 #ifdef HAVE_PREAD64
5396 if ((off_t) memaddr >= 0)
5397 bytes = (readbuf != nullptr
5398 ? pread64 (fd, readbuf, len, memaddr)
5399 : pwrite64 (fd, writebuf, len, memaddr));
5400 else
5401 #endif
5403 bytes = -1;
5404 if (lseek (fd, memaddr, SEEK_SET) != -1)
5405 bytes = (readbuf != nullptr
5406 ? read (fd, readbuf, len)
5407 : write (fd, writebuf, len));
5410 if (bytes < 0)
5411 return errno;
5412 else if (bytes == 0)
5414 /* EOF means the address space is gone, the whole process
5415 exited or execed. */
5416 return EIO;
5419 memaddr += bytes;
5420 if (readbuf != nullptr)
5421 readbuf += bytes;
5422 else
5423 writebuf += bytes;
5424 len -= bytes;
5427 return 0;
5431 linux_process_target::read_memory (CORE_ADDR memaddr,
5432 unsigned char *myaddr, int len)
5434 return proc_xfer_memory (memaddr, myaddr, nullptr, len);
5437 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5438 memory at MEMADDR. On failure (cannot write to the inferior)
5439 returns the value of errno. Always succeeds if LEN is zero. */
5442 linux_process_target::write_memory (CORE_ADDR memaddr,
5443 const unsigned char *myaddr, int len)
5445 if (debug_threads)
5447 /* Dump up to four bytes. */
5448 char str[4 * 2 + 1];
5449 char *p = str;
5450 int dump = len < 4 ? len : 4;
5452 for (int i = 0; i < dump; i++)
5454 sprintf (p, "%02x", myaddr[i]);
5455 p += 2;
5457 *p = '\0';
5459 threads_debug_printf ("Writing %s to 0x%08lx in process %d",
5460 str, (long) memaddr, current_process ()->pid);
5463 return proc_xfer_memory (memaddr, nullptr, myaddr, len);
5466 void
5467 linux_process_target::look_up_symbols ()
5469 #ifdef USE_THREAD_DB
5470 struct process_info *proc = current_process ();
5472 if (proc->priv->thread_db != NULL)
5473 return;
5475 thread_db_init ();
5476 #endif
5479 void
5480 linux_process_target::request_interrupt ()
5482 /* Send a SIGINT to the process group. This acts just like the user
5483 typed a ^C on the controlling terminal. */
5484 int res = ::kill (-signal_pid, SIGINT);
5485 if (res == -1)
5486 warning (_("Sending SIGINT to process group of pid %ld failed: %s"),
5487 signal_pid, safe_strerror (errno));
5490 bool
5491 linux_process_target::supports_read_auxv ()
5493 return true;
5496 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5497 to debugger memory starting at MYADDR. */
5500 linux_process_target::read_auxv (int pid, CORE_ADDR offset,
5501 unsigned char *myaddr, unsigned int len)
5503 char filename[PATH_MAX];
5504 int fd, n;
5506 xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
5508 fd = open (filename, O_RDONLY);
5509 if (fd < 0)
5510 return -1;
5512 if (offset != (CORE_ADDR) 0
5513 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
5514 n = -1;
5515 else
5516 n = read (fd, myaddr, len);
5518 close (fd);
5520 return n;
5524 linux_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
5525 int size, raw_breakpoint *bp)
5527 if (type == raw_bkpt_type_sw)
5528 return insert_memory_breakpoint (bp);
5529 else
5530 return low_insert_point (type, addr, size, bp);
5534 linux_process_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
5535 int size, raw_breakpoint *bp)
5537 /* Unsupported (see target.h). */
5538 return 1;
5542 linux_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
5543 int size, raw_breakpoint *bp)
5545 if (type == raw_bkpt_type_sw)
5546 return remove_memory_breakpoint (bp);
5547 else
5548 return low_remove_point (type, addr, size, bp);
5552 linux_process_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
5553 int size, raw_breakpoint *bp)
5555 /* Unsupported (see target.h). */
5556 return 1;
5559 /* Implement the stopped_by_sw_breakpoint target_ops
5560 method. */
5562 bool
5563 linux_process_target::stopped_by_sw_breakpoint ()
5565 struct lwp_info *lwp = get_thread_lwp (current_thread);
5567 return (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT);
5570 /* Implement the supports_stopped_by_sw_breakpoint target_ops
5571 method. */
5573 bool
5574 linux_process_target::supports_stopped_by_sw_breakpoint ()
5576 return USE_SIGTRAP_SIGINFO;
5579 /* Implement the stopped_by_hw_breakpoint target_ops
5580 method. */
5582 bool
5583 linux_process_target::stopped_by_hw_breakpoint ()
5585 struct lwp_info *lwp = get_thread_lwp (current_thread);
5587 return (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT);
5590 /* Implement the supports_stopped_by_hw_breakpoint target_ops
5591 method. */
5593 bool
5594 linux_process_target::supports_stopped_by_hw_breakpoint ()
5596 return USE_SIGTRAP_SIGINFO;
5599 /* Implement the supports_hardware_single_step target_ops method. */
5601 bool
5602 linux_process_target::supports_hardware_single_step ()
5604 return true;
5607 bool
5608 linux_process_target::stopped_by_watchpoint ()
5610 struct lwp_info *lwp = get_thread_lwp (current_thread);
5612 return lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
5615 CORE_ADDR
5616 linux_process_target::stopped_data_address ()
5618 struct lwp_info *lwp = get_thread_lwp (current_thread);
5620 return lwp->stopped_data_address;
5623 /* This is only used for targets that define PT_TEXT_ADDR,
5624 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5625 the target has different ways of acquiring this information, like
5626 loadmaps. */
5628 bool
5629 linux_process_target::supports_read_offsets ()
5631 #ifdef SUPPORTS_READ_OFFSETS
5632 return true;
5633 #else
5634 return false;
5635 #endif
5638 /* Under uClinux, programs are loaded at non-zero offsets, which we need
5639 to tell gdb about. */
5642 linux_process_target::read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
5644 #ifdef SUPPORTS_READ_OFFSETS
5645 unsigned long text, text_end, data;
5646 int pid = lwpid_of (current_thread);
5648 errno = 0;
5650 text = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_ADDR,
5651 (PTRACE_TYPE_ARG4) 0);
5652 text_end = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_END_ADDR,
5653 (PTRACE_TYPE_ARG4) 0);
5654 data = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_DATA_ADDR,
5655 (PTRACE_TYPE_ARG4) 0);
5657 if (errno == 0)
5659 /* Both text and data offsets produced at compile-time (and so
5660 used by gdb) are relative to the beginning of the program,
5661 with the data segment immediately following the text segment.
5662 However, the actual runtime layout in memory may put the data
5663 somewhere else, so when we send gdb a data base-address, we
5664 use the real data base address and subtract the compile-time
5665 data base-address from it (which is just the length of the
5666 text segment). BSS immediately follows data in both
5667 cases. */
5668 *text_p = text;
5669 *data_p = data - (text_end - text);
5671 return 1;
5673 return 0;
5674 #else
5675 gdb_assert_not_reached ("target op read_offsets not supported");
5676 #endif
5679 bool
5680 linux_process_target::supports_get_tls_address ()
5682 #ifdef USE_THREAD_DB
5683 return true;
5684 #else
5685 return false;
5686 #endif
5690 linux_process_target::get_tls_address (thread_info *thread,
5691 CORE_ADDR offset,
5692 CORE_ADDR load_module,
5693 CORE_ADDR *address)
5695 #ifdef USE_THREAD_DB
5696 return thread_db_get_tls_address (thread, offset, load_module, address);
5697 #else
5698 return -1;
5699 #endif
5702 bool
5703 linux_process_target::supports_qxfer_osdata ()
5705 return true;
5709 linux_process_target::qxfer_osdata (const char *annex,
5710 unsigned char *readbuf,
5711 unsigned const char *writebuf,
5712 CORE_ADDR offset, int len)
5714 return linux_common_xfer_osdata (annex, readbuf, offset, len);
5717 void
5718 linux_process_target::siginfo_fixup (siginfo_t *siginfo,
5719 gdb_byte *inf_siginfo, int direction)
5721 bool done = low_siginfo_fixup (siginfo, inf_siginfo, direction);
5723 /* If there was no callback, or the callback didn't do anything,
5724 then just do a straight memcpy. */
5725 if (!done)
5727 if (direction == 1)
5728 memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
5729 else
5730 memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
5734 bool
5735 linux_process_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
5736 int direction)
5738 return false;
5741 bool
5742 linux_process_target::supports_qxfer_siginfo ()
5744 return true;
5748 linux_process_target::qxfer_siginfo (const char *annex,
5749 unsigned char *readbuf,
5750 unsigned const char *writebuf,
5751 CORE_ADDR offset, int len)
5753 int pid;
5754 siginfo_t siginfo;
5755 gdb_byte inf_siginfo[sizeof (siginfo_t)];
5757 if (current_thread == NULL)
5758 return -1;
5760 pid = lwpid_of (current_thread);
5762 threads_debug_printf ("%s siginfo for lwp %d.",
5763 readbuf != NULL ? "Reading" : "Writing",
5764 pid);
5766 if (offset >= sizeof (siginfo))
5767 return -1;
5769 if (ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
5770 return -1;
5772 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
5773 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
5774 inferior with a 64-bit GDBSERVER should look the same as debugging it
5775 with a 32-bit GDBSERVER, we need to convert it. */
5776 siginfo_fixup (&siginfo, inf_siginfo, 0);
5778 if (offset + len > sizeof (siginfo))
5779 len = sizeof (siginfo) - offset;
5781 if (readbuf != NULL)
5782 memcpy (readbuf, inf_siginfo + offset, len);
5783 else
5785 memcpy (inf_siginfo + offset, writebuf, len);
5787 /* Convert back to ptrace layout before flushing it out. */
5788 siginfo_fixup (&siginfo, inf_siginfo, 1);
5790 if (ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
5791 return -1;
5794 return len;
5797 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
5798 so we notice when children change state; as the handler for the
5799 sigsuspend in my_waitpid. */
5801 static void
5802 sigchld_handler (int signo)
5804 int old_errno = errno;
5806 if (debug_threads)
5810 /* Use the async signal safe debug function. */
5811 if (debug_write ("sigchld_handler\n",
5812 sizeof ("sigchld_handler\n") - 1) < 0)
5813 break; /* just ignore */
5814 } while (0);
5817 if (target_is_async_p ())
5818 async_file_mark (); /* trigger a linux_wait */
5820 errno = old_errno;
5823 bool
5824 linux_process_target::supports_non_stop ()
5826 return true;
5829 bool
5830 linux_process_target::async (bool enable)
5832 bool previous = target_is_async_p ();
5834 threads_debug_printf ("async (%d), previous=%d",
5835 enable, previous);
5837 if (previous != enable)
5839 sigset_t mask;
5840 sigemptyset (&mask);
5841 sigaddset (&mask, SIGCHLD);
5843 gdb_sigmask (SIG_BLOCK, &mask, NULL);
5845 if (enable)
5847 if (!linux_event_pipe.open_pipe ())
5849 gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
5851 warning ("creating event pipe failed.");
5852 return previous;
5855 /* Register the event loop handler. */
5856 add_file_handler (linux_event_pipe.event_fd (),
5857 handle_target_event, NULL,
5858 "linux-low");
5860 /* Always trigger a linux_wait. */
5861 async_file_mark ();
5863 else
5865 delete_file_handler (linux_event_pipe.event_fd ());
5867 linux_event_pipe.close_pipe ();
5870 gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
5873 return previous;
5877 linux_process_target::start_non_stop (bool nonstop)
5879 /* Register or unregister from event-loop accordingly. */
5880 target_async (nonstop);
5882 if (target_is_async_p () != (nonstop != false))
5883 return -1;
5885 return 0;
5888 bool
5889 linux_process_target::supports_multi_process ()
5891 return true;
5894 /* Check if fork events are supported. */
5896 bool
5897 linux_process_target::supports_fork_events ()
5899 return true;
5902 /* Check if vfork events are supported. */
5904 bool
5905 linux_process_target::supports_vfork_events ()
5907 return true;
5910 /* Check if exec events are supported. */
5912 bool
5913 linux_process_target::supports_exec_events ()
5915 return true;
5918 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
5919 ptrace flags for all inferiors. This is in case the new GDB connection
5920 doesn't support the same set of events that the previous one did. */
5922 void
5923 linux_process_target::handle_new_gdb_connection ()
5925 /* Request that all the lwps reset their ptrace options. */
5926 for_each_thread ([] (thread_info *thread)
5928 struct lwp_info *lwp = get_thread_lwp (thread);
5930 if (!lwp->stopped)
5932 /* Stop the lwp so we can modify its ptrace options. */
5933 lwp->must_set_ptrace_flags = 1;
5934 linux_stop_lwp (lwp);
5936 else
5938 /* Already stopped; go ahead and set the ptrace options. */
5939 struct process_info *proc = find_process_pid (pid_of (thread));
5940 int options = linux_low_ptrace_options (proc->attached);
5942 linux_enable_event_reporting (lwpid_of (thread), options);
5943 lwp->must_set_ptrace_flags = 0;
5949 linux_process_target::handle_monitor_command (char *mon)
5951 #ifdef USE_THREAD_DB
5952 return thread_db_handle_monitor_command (mon);
5953 #else
5954 return 0;
5955 #endif
5959 linux_process_target::core_of_thread (ptid_t ptid)
5961 return linux_common_core_of_thread (ptid);
5964 bool
5965 linux_process_target::supports_disable_randomization ()
5967 return true;
5970 bool
5971 linux_process_target::supports_agent ()
5973 return true;
5976 bool
5977 linux_process_target::supports_range_stepping ()
5979 if (supports_software_single_step ())
5980 return true;
5982 return low_supports_range_stepping ();
5985 bool
5986 linux_process_target::low_supports_range_stepping ()
5988 return false;
5991 bool
5992 linux_process_target::supports_pid_to_exec_file ()
5994 return true;
5997 const char *
5998 linux_process_target::pid_to_exec_file (int pid)
6000 return linux_proc_pid_to_exec_file (pid);
6003 bool
6004 linux_process_target::supports_multifs ()
6006 return true;
6010 linux_process_target::multifs_open (int pid, const char *filename,
6011 int flags, mode_t mode)
6013 return linux_mntns_open_cloexec (pid, filename, flags, mode);
6017 linux_process_target::multifs_unlink (int pid, const char *filename)
6019 return linux_mntns_unlink (pid, filename);
6022 ssize_t
6023 linux_process_target::multifs_readlink (int pid, const char *filename,
6024 char *buf, size_t bufsiz)
6026 return linux_mntns_readlink (pid, filename, buf, bufsiz);
6029 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
6030 struct target_loadseg
6032 /* Core address to which the segment is mapped. */
6033 Elf32_Addr addr;
6034 /* VMA recorded in the program header. */
6035 Elf32_Addr p_vaddr;
6036 /* Size of this segment in memory. */
6037 Elf32_Word p_memsz;
6040 # if defined PT_GETDSBT
6041 struct target_loadmap
6043 /* Protocol version number, must be zero. */
6044 Elf32_Word version;
6045 /* Pointer to the DSBT table, its size, and the DSBT index. */
6046 unsigned *dsbt_table;
6047 unsigned dsbt_size, dsbt_index;
6048 /* Number of segments in this map. */
6049 Elf32_Word nsegs;
6050 /* The actual memory map. */
6051 struct target_loadseg segs[/*nsegs*/];
6053 # define LINUX_LOADMAP PT_GETDSBT
6054 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6055 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6056 # else
6057 struct target_loadmap
6059 /* Protocol version number, must be zero. */
6060 Elf32_Half version;
6061 /* Number of segments in this map. */
6062 Elf32_Half nsegs;
6063 /* The actual memory map. */
6064 struct target_loadseg segs[/*nsegs*/];
6066 # define LINUX_LOADMAP PTRACE_GETFDPIC
6067 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6068 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6069 # endif
6071 bool
6072 linux_process_target::supports_read_loadmap ()
6074 return true;
6078 linux_process_target::read_loadmap (const char *annex, CORE_ADDR offset,
6079 unsigned char *myaddr, unsigned int len)
6081 int pid = lwpid_of (current_thread);
6082 int addr = -1;
6083 struct target_loadmap *data = NULL;
6084 unsigned int actual_length, copy_length;
6086 if (strcmp (annex, "exec") == 0)
6087 addr = (int) LINUX_LOADMAP_EXEC;
6088 else if (strcmp (annex, "interp") == 0)
6089 addr = (int) LINUX_LOADMAP_INTERP;
6090 else
6091 return -1;
6093 if (ptrace (LINUX_LOADMAP, pid, addr, &data) != 0)
6094 return -1;
6096 if (data == NULL)
6097 return -1;
6099 actual_length = sizeof (struct target_loadmap)
6100 + sizeof (struct target_loadseg) * data->nsegs;
6102 if (offset < 0 || offset > actual_length)
6103 return -1;
6105 copy_length = actual_length - offset < len ? actual_length - offset : len;
6106 memcpy (myaddr, (char *) data + offset, copy_length);
6107 return copy_length;
6109 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6111 bool
6112 linux_process_target::supports_catch_syscall ()
6114 return low_supports_catch_syscall ();
6117 bool
6118 linux_process_target::low_supports_catch_syscall ()
6120 return false;
6123 CORE_ADDR
6124 linux_process_target::read_pc (regcache *regcache)
6126 if (!low_supports_breakpoints ())
6127 return 0;
6129 return low_get_pc (regcache);
6132 void
6133 linux_process_target::write_pc (regcache *regcache, CORE_ADDR pc)
6135 gdb_assert (low_supports_breakpoints ());
6137 low_set_pc (regcache, pc);
6140 bool
6141 linux_process_target::supports_thread_stopped ()
6143 return true;
6146 bool
6147 linux_process_target::thread_stopped (thread_info *thread)
6149 return get_thread_lwp (thread)->stopped;
6152 /* This exposes stop-all-threads functionality to other modules. */
6154 void
6155 linux_process_target::pause_all (bool freeze)
6157 stop_all_lwps (freeze, NULL);
6160 /* This exposes unstop-all-threads functionality to other gdbserver
6161 modules. */
6163 void
6164 linux_process_target::unpause_all (bool unfreeze)
6166 unstop_all_lwps (unfreeze, NULL);
6169 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6171 static int
6172 get_phdr_phnum_from_proc_auxv (const int pid, const int is_elf64,
6173 CORE_ADDR *phdr_memaddr, int *num_phdr)
6175 char filename[PATH_MAX];
6176 int fd;
6177 const int auxv_size = is_elf64
6178 ? sizeof (Elf64_auxv_t) : sizeof (Elf32_auxv_t);
6179 char buf[sizeof (Elf64_auxv_t)]; /* The larger of the two. */
6181 xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
6183 fd = open (filename, O_RDONLY);
6184 if (fd < 0)
6185 return 1;
6187 *phdr_memaddr = 0;
6188 *num_phdr = 0;
6189 while (read (fd, buf, auxv_size) == auxv_size
6190 && (*phdr_memaddr == 0 || *num_phdr == 0))
6192 if (is_elf64)
6194 Elf64_auxv_t *const aux = (Elf64_auxv_t *) buf;
6196 switch (aux->a_type)
6198 case AT_PHDR:
6199 *phdr_memaddr = aux->a_un.a_val;
6200 break;
6201 case AT_PHNUM:
6202 *num_phdr = aux->a_un.a_val;
6203 break;
6206 else
6208 Elf32_auxv_t *const aux = (Elf32_auxv_t *) buf;
6210 switch (aux->a_type)
6212 case AT_PHDR:
6213 *phdr_memaddr = aux->a_un.a_val;
6214 break;
6215 case AT_PHNUM:
6216 *num_phdr = aux->a_un.a_val;
6217 break;
6222 close (fd);
6224 if (*phdr_memaddr == 0 || *num_phdr == 0)
6226 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6227 "phdr_memaddr = %ld, phdr_num = %d",
6228 (long) *phdr_memaddr, *num_phdr);
6229 return 2;
6232 return 0;
6235 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6237 static CORE_ADDR
6238 get_dynamic (const int pid, const int is_elf64)
6240 CORE_ADDR phdr_memaddr, relocation;
6241 int num_phdr, i;
6242 unsigned char *phdr_buf;
6243 const int phdr_size = is_elf64 ? sizeof (Elf64_Phdr) : sizeof (Elf32_Phdr);
6245 if (get_phdr_phnum_from_proc_auxv (pid, is_elf64, &phdr_memaddr, &num_phdr))
6246 return 0;
6248 gdb_assert (num_phdr < 100); /* Basic sanity check. */
6249 phdr_buf = (unsigned char *) alloca (num_phdr * phdr_size);
6251 if (linux_read_memory (phdr_memaddr, phdr_buf, num_phdr * phdr_size))
6252 return 0;
6254 /* Compute relocation: it is expected to be 0 for "regular" executables,
6255 non-zero for PIE ones. */
6256 relocation = -1;
6257 for (i = 0; relocation == -1 && i < num_phdr; i++)
6258 if (is_elf64)
6260 Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
6262 if (p->p_type == PT_PHDR)
6263 relocation = phdr_memaddr - p->p_vaddr;
6265 else
6267 Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
6269 if (p->p_type == PT_PHDR)
6270 relocation = phdr_memaddr - p->p_vaddr;
6273 if (relocation == -1)
6275 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6276 any real world executables, including PIE executables, have always
6277 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6278 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6279 or present DT_DEBUG anyway (fpc binaries are statically linked).
6281 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6283 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6285 return 0;
6288 for (i = 0; i < num_phdr; i++)
6290 if (is_elf64)
6292 Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
6294 if (p->p_type == PT_DYNAMIC)
6295 return p->p_vaddr + relocation;
6297 else
6299 Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
6301 if (p->p_type == PT_DYNAMIC)
6302 return p->p_vaddr + relocation;
6306 return 0;
6309 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6310 can be 0 if the inferior does not yet have the library list initialized.
6311 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6312 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6314 static CORE_ADDR
6315 get_r_debug (const int pid, const int is_elf64)
6317 CORE_ADDR dynamic_memaddr;
6318 const int dyn_size = is_elf64 ? sizeof (Elf64_Dyn) : sizeof (Elf32_Dyn);
6319 unsigned char buf[sizeof (Elf64_Dyn)]; /* The larger of the two. */
6320 CORE_ADDR map = -1;
6322 dynamic_memaddr = get_dynamic (pid, is_elf64);
6323 if (dynamic_memaddr == 0)
6324 return map;
6326 while (linux_read_memory (dynamic_memaddr, buf, dyn_size) == 0)
6328 if (is_elf64)
6330 Elf64_Dyn *const dyn = (Elf64_Dyn *) buf;
6331 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6332 union
6334 Elf64_Xword map;
6335 unsigned char buf[sizeof (Elf64_Xword)];
6337 rld_map;
6338 #endif
6339 #ifdef DT_MIPS_RLD_MAP
6340 if (dyn->d_tag == DT_MIPS_RLD_MAP)
6342 if (linux_read_memory (dyn->d_un.d_val,
6343 rld_map.buf, sizeof (rld_map.buf)) == 0)
6344 return rld_map.map;
6345 else
6346 break;
6348 #endif /* DT_MIPS_RLD_MAP */
6349 #ifdef DT_MIPS_RLD_MAP_REL
6350 if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
6352 if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
6353 rld_map.buf, sizeof (rld_map.buf)) == 0)
6354 return rld_map.map;
6355 else
6356 break;
6358 #endif /* DT_MIPS_RLD_MAP_REL */
6360 if (dyn->d_tag == DT_DEBUG && map == -1)
6361 map = dyn->d_un.d_val;
6363 if (dyn->d_tag == DT_NULL)
6364 break;
6366 else
6368 Elf32_Dyn *const dyn = (Elf32_Dyn *) buf;
6369 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6370 union
6372 Elf32_Word map;
6373 unsigned char buf[sizeof (Elf32_Word)];
6375 rld_map;
6376 #endif
6377 #ifdef DT_MIPS_RLD_MAP
6378 if (dyn->d_tag == DT_MIPS_RLD_MAP)
6380 if (linux_read_memory (dyn->d_un.d_val,
6381 rld_map.buf, sizeof (rld_map.buf)) == 0)
6382 return rld_map.map;
6383 else
6384 break;
6386 #endif /* DT_MIPS_RLD_MAP */
6387 #ifdef DT_MIPS_RLD_MAP_REL
6388 if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
6390 if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
6391 rld_map.buf, sizeof (rld_map.buf)) == 0)
6392 return rld_map.map;
6393 else
6394 break;
6396 #endif /* DT_MIPS_RLD_MAP_REL */
6398 if (dyn->d_tag == DT_DEBUG && map == -1)
6399 map = dyn->d_un.d_val;
6401 if (dyn->d_tag == DT_NULL)
6402 break;
6405 dynamic_memaddr += dyn_size;
6408 return map;
6411 /* Read one pointer from MEMADDR in the inferior. */
6413 static int
6414 read_one_ptr (CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size)
6416 int ret;
6418 /* Go through a union so this works on either big or little endian
6419 hosts, when the inferior's pointer size is smaller than the size
6420 of CORE_ADDR. It is assumed the inferior's endianness is the
6421 same of the superior's. */
6422 union
6424 CORE_ADDR core_addr;
6425 unsigned int ui;
6426 unsigned char uc;
6427 } addr;
6429 ret = linux_read_memory (memaddr, &addr.uc, ptr_size);
6430 if (ret == 0)
6432 if (ptr_size == sizeof (CORE_ADDR))
6433 *ptr = addr.core_addr;
6434 else if (ptr_size == sizeof (unsigned int))
6435 *ptr = addr.ui;
6436 else
6437 gdb_assert_not_reached ("unhandled pointer size");
6439 return ret;
6442 bool
6443 linux_process_target::supports_qxfer_libraries_svr4 ()
6445 return true;
6448 struct link_map_offsets
6450 /* Offset and size of r_debug.r_version. */
6451 int r_version_offset;
6453 /* Offset and size of r_debug.r_map. */
6454 int r_map_offset;
6456 /* Offset of r_debug_extended.r_next. */
6457 int r_next_offset;
6459 /* Offset to l_addr field in struct link_map. */
6460 int l_addr_offset;
6462 /* Offset to l_name field in struct link_map. */
6463 int l_name_offset;
6465 /* Offset to l_ld field in struct link_map. */
6466 int l_ld_offset;
6468 /* Offset to l_next field in struct link_map. */
6469 int l_next_offset;
6471 /* Offset to l_prev field in struct link_map. */
6472 int l_prev_offset;
6475 static const link_map_offsets lmo_32bit_offsets =
6477 0, /* r_version offset. */
6478 4, /* r_debug.r_map offset. */
6479 20, /* r_debug_extended.r_next. */
6480 0, /* l_addr offset in link_map. */
6481 4, /* l_name offset in link_map. */
6482 8, /* l_ld offset in link_map. */
6483 12, /* l_next offset in link_map. */
6484 16 /* l_prev offset in link_map. */
6487 static const link_map_offsets lmo_64bit_offsets =
6489 0, /* r_version offset. */
6490 8, /* r_debug.r_map offset. */
6491 40, /* r_debug_extended.r_next. */
6492 0, /* l_addr offset in link_map. */
6493 8, /* l_name offset in link_map. */
6494 16, /* l_ld offset in link_map. */
6495 24, /* l_next offset in link_map. */
6496 32 /* l_prev offset in link_map. */
6499 /* Get the loaded shared libraries from one namespace. */
6501 static void
6502 read_link_map (std::string &document, CORE_ADDR lmid, CORE_ADDR lm_addr,
6503 CORE_ADDR lm_prev, int ptr_size, const link_map_offsets *lmo)
6505 CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
6507 while (lm_addr
6508 && read_one_ptr (lm_addr + lmo->l_name_offset,
6509 &l_name, ptr_size) == 0
6510 && read_one_ptr (lm_addr + lmo->l_addr_offset,
6511 &l_addr, ptr_size) == 0
6512 && read_one_ptr (lm_addr + lmo->l_ld_offset,
6513 &l_ld, ptr_size) == 0
6514 && read_one_ptr (lm_addr + lmo->l_prev_offset,
6515 &l_prev, ptr_size) == 0
6516 && read_one_ptr (lm_addr + lmo->l_next_offset,
6517 &l_next, ptr_size) == 0)
6519 unsigned char libname[PATH_MAX];
6521 if (lm_prev != l_prev)
6523 warning ("Corrupted shared library list: 0x%s != 0x%s",
6524 paddress (lm_prev), paddress (l_prev));
6525 break;
6528 /* Not checking for error because reading may stop before we've got
6529 PATH_MAX worth of characters. */
6530 libname[0] = '\0';
6531 linux_read_memory (l_name, libname, sizeof (libname) - 1);
6532 libname[sizeof (libname) - 1] = '\0';
6533 if (libname[0] != '\0')
6535 string_appendf (document, "<library name=\"");
6536 xml_escape_text_append (document, (char *) libname);
6537 string_appendf (document, "\" lm=\"0x%s\" l_addr=\"0x%s\" "
6538 "l_ld=\"0x%s\" lmid=\"0x%s\"/>",
6539 paddress (lm_addr), paddress (l_addr),
6540 paddress (l_ld), paddress (lmid));
6543 lm_prev = lm_addr;
6544 lm_addr = l_next;
6548 /* Construct qXfer:libraries-svr4:read reply. */
6551 linux_process_target::qxfer_libraries_svr4 (const char *annex,
6552 unsigned char *readbuf,
6553 unsigned const char *writebuf,
6554 CORE_ADDR offset, int len)
6556 struct process_info_private *const priv = current_process ()->priv;
6557 char filename[PATH_MAX];
6558 int pid, is_elf64;
6559 unsigned int machine;
6560 CORE_ADDR lmid = 0, lm_addr = 0, lm_prev = 0;
6562 if (writebuf != NULL)
6563 return -2;
6564 if (readbuf == NULL)
6565 return -1;
6567 pid = lwpid_of (current_thread);
6568 xsnprintf (filename, sizeof filename, "/proc/%d/exe", pid);
6569 is_elf64 = elf_64_file_p (filename, &machine);
6570 const link_map_offsets *lmo;
6571 int ptr_size;
6572 if (is_elf64)
6574 lmo = &lmo_64bit_offsets;
6575 ptr_size = 8;
6577 else
6579 lmo = &lmo_32bit_offsets;
6580 ptr_size = 4;
6583 while (annex[0] != '\0')
6585 const char *sep;
6586 CORE_ADDR *addrp;
6587 int name_len;
6589 sep = strchr (annex, '=');
6590 if (sep == NULL)
6591 break;
6593 name_len = sep - annex;
6594 if (name_len == 4 && startswith (annex, "lmid"))
6595 addrp = &lmid;
6596 else if (name_len == 5 && startswith (annex, "start"))
6597 addrp = &lm_addr;
6598 else if (name_len == 4 && startswith (annex, "prev"))
6599 addrp = &lm_prev;
6600 else
6602 annex = strchr (sep, ';');
6603 if (annex == NULL)
6604 break;
6605 annex++;
6606 continue;
6609 annex = decode_address_to_semicolon (addrp, sep + 1);
6612 std::string document = "<library-list-svr4 version=\"1.0\"";
6614 /* When the starting LM_ADDR is passed in the annex, only traverse that
6615 namespace, which is assumed to be identified by LMID.
6617 Otherwise, start with R_DEBUG and traverse all namespaces we find. */
6618 if (lm_addr != 0)
6620 document += ">";
6621 read_link_map (document, lmid, lm_addr, lm_prev, ptr_size, lmo);
6623 else
6625 if (lm_prev != 0)
6626 warning ("ignoring prev=0x%s without start", paddress (lm_prev));
6628 /* We could interpret LMID as 'provide only the libraries for this
6629 namespace' but GDB is currently only providing lmid, start, and
6630 prev, or nothing. */
6631 if (lmid != 0)
6632 warning ("ignoring lmid=0x%s without start", paddress (lmid));
6634 CORE_ADDR r_debug = priv->r_debug;
6635 if (r_debug == 0)
6636 r_debug = priv->r_debug = get_r_debug (pid, is_elf64);
6638 /* We failed to find DT_DEBUG. Such situation will not change
6639 for this inferior - do not retry it. Report it to GDB as
6640 E01, see for the reasons at the GDB solib-svr4.c side. */
6641 if (r_debug == (CORE_ADDR) -1)
6642 return -1;
6644 /* Terminate the header if we end up with an empty list. */
6645 if (r_debug == 0)
6646 document += ">";
6648 while (r_debug != 0)
6650 int r_version = 0;
6651 if (linux_read_memory (r_debug + lmo->r_version_offset,
6652 (unsigned char *) &r_version,
6653 sizeof (r_version)) != 0)
6655 warning ("unable to read r_version from 0x%s",
6656 paddress (r_debug + lmo->r_version_offset));
6657 break;
6660 if (r_version < 1)
6662 warning ("unexpected r_debug version %d", r_version);
6663 break;
6666 if (read_one_ptr (r_debug + lmo->r_map_offset, &lm_addr,
6667 ptr_size) != 0)
6669 warning ("unable to read r_map from 0x%s",
6670 paddress (r_debug + lmo->r_map_offset));
6671 break;
6674 /* We read the entire namespace. */
6675 lm_prev = 0;
6677 /* The first entry corresponds to the main executable unless the
6678 dynamic loader was loaded late by a static executable. But
6679 in such case the main executable does not have PT_DYNAMIC
6680 present and we would not have gotten here. */
6681 if (r_debug == priv->r_debug)
6683 if (lm_addr != 0)
6684 string_appendf (document, " main-lm=\"0x%s\">",
6685 paddress (lm_addr));
6686 else
6687 document += ">";
6689 lm_prev = lm_addr;
6690 if (read_one_ptr (lm_addr + lmo->l_next_offset,
6691 &lm_addr, ptr_size) != 0)
6693 warning ("unable to read l_next from 0x%s",
6694 paddress (lm_addr + lmo->l_next_offset));
6695 break;
6699 read_link_map (document, r_debug, lm_addr, lm_prev, ptr_size, lmo);
6701 if (r_version < 2)
6702 break;
6704 if (read_one_ptr (r_debug + lmo->r_next_offset, &r_debug,
6705 ptr_size) != 0)
6707 warning ("unable to read r_next from 0x%s",
6708 paddress (r_debug + lmo->r_next_offset));
6709 break;
6714 document += "</library-list-svr4>";
6716 int document_len = document.length ();
6717 if (offset < document_len)
6718 document_len -= offset;
6719 else
6720 document_len = 0;
6721 if (len > document_len)
6722 len = document_len;
6724 memcpy (readbuf, document.data () + offset, len);
6726 return len;
6729 #ifdef HAVE_LINUX_BTRACE
6731 bool
6732 linux_process_target::supports_btrace ()
6734 return true;
6737 btrace_target_info *
6738 linux_process_target::enable_btrace (thread_info *tp,
6739 const btrace_config *conf)
6741 return linux_enable_btrace (tp->id, conf);
6744 /* See to_disable_btrace target method. */
6747 linux_process_target::disable_btrace (btrace_target_info *tinfo)
6749 enum btrace_error err;
6751 err = linux_disable_btrace (tinfo);
6752 return (err == BTRACE_ERR_NONE ? 0 : -1);
6755 /* Encode an Intel Processor Trace configuration. */
6757 static void
6758 linux_low_encode_pt_config (std::string *buffer,
6759 const struct btrace_data_pt_config *config)
6761 *buffer += "<pt-config>\n";
6763 switch (config->cpu.vendor)
6765 case CV_INTEL:
6766 string_xml_appendf (*buffer, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
6767 "model=\"%u\" stepping=\"%u\"/>\n",
6768 config->cpu.family, config->cpu.model,
6769 config->cpu.stepping);
6770 break;
6772 default:
6773 break;
6776 *buffer += "</pt-config>\n";
6779 /* Encode a raw buffer. */
6781 static void
6782 linux_low_encode_raw (std::string *buffer, const gdb_byte *data,
6783 unsigned int size)
6785 if (size == 0)
6786 return;
6788 /* We use hex encoding - see gdbsupport/rsp-low.h. */
6789 *buffer += "<raw>\n";
6791 while (size-- > 0)
6793 char elem[2];
6795 elem[0] = tohex ((*data >> 4) & 0xf);
6796 elem[1] = tohex (*data++ & 0xf);
6798 buffer->append (elem, 2);
6801 *buffer += "</raw>\n";
6804 /* See to_read_btrace target method. */
6807 linux_process_target::read_btrace (btrace_target_info *tinfo,
6808 std::string *buffer,
6809 enum btrace_read_type type)
6811 struct btrace_data btrace;
6812 enum btrace_error err;
6814 err = linux_read_btrace (&btrace, tinfo, type);
6815 if (err != BTRACE_ERR_NONE)
6817 if (err == BTRACE_ERR_OVERFLOW)
6818 *buffer += "E.Overflow.";
6819 else
6820 *buffer += "E.Generic Error.";
6822 return -1;
6825 switch (btrace.format)
6827 case BTRACE_FORMAT_NONE:
6828 *buffer += "E.No Trace.";
6829 return -1;
6831 case BTRACE_FORMAT_BTS:
6832 *buffer += "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n";
6833 *buffer += "<btrace version=\"1.0\">\n";
6835 for (const btrace_block &block : *btrace.variant.bts.blocks)
6836 string_xml_appendf (*buffer, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
6837 paddress (block.begin), paddress (block.end));
6839 *buffer += "</btrace>\n";
6840 break;
6842 case BTRACE_FORMAT_PT:
6843 *buffer += "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n";
6844 *buffer += "<btrace version=\"1.0\">\n";
6845 *buffer += "<pt>\n";
6847 linux_low_encode_pt_config (buffer, &btrace.variant.pt.config);
6849 linux_low_encode_raw (buffer, btrace.variant.pt.data,
6850 btrace.variant.pt.size);
6852 *buffer += "</pt>\n";
6853 *buffer += "</btrace>\n";
6854 break;
6856 default:
6857 *buffer += "E.Unsupported Trace Format.";
6858 return -1;
6861 return 0;
6864 /* See to_btrace_conf target method. */
6867 linux_process_target::read_btrace_conf (const btrace_target_info *tinfo,
6868 std::string *buffer)
6870 const struct btrace_config *conf;
6872 *buffer += "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n";
6873 *buffer += "<btrace-conf version=\"1.0\">\n";
6875 conf = linux_btrace_conf (tinfo);
6876 if (conf != NULL)
6878 switch (conf->format)
6880 case BTRACE_FORMAT_NONE:
6881 break;
6883 case BTRACE_FORMAT_BTS:
6884 string_xml_appendf (*buffer, "<bts");
6885 string_xml_appendf (*buffer, " size=\"0x%x\"", conf->bts.size);
6886 string_xml_appendf (*buffer, " />\n");
6887 break;
6889 case BTRACE_FORMAT_PT:
6890 string_xml_appendf (*buffer, "<pt");
6891 string_xml_appendf (*buffer, " size=\"0x%x\"", conf->pt.size);
6892 string_xml_appendf (*buffer, "/>\n");
6893 break;
6897 *buffer += "</btrace-conf>\n";
6898 return 0;
6900 #endif /* HAVE_LINUX_BTRACE */
6902 /* See nat/linux-nat.h. */
6904 ptid_t
6905 current_lwp_ptid (void)
6907 return ptid_of (current_thread);
6910 const char *
6911 linux_process_target::thread_name (ptid_t thread)
6913 return linux_proc_tid_get_name (thread);
6916 #if USE_THREAD_DB
6917 bool
6918 linux_process_target::thread_handle (ptid_t ptid, gdb_byte **handle,
6919 int *handle_len)
6921 return thread_db_thread_handle (ptid, handle, handle_len);
6923 #endif
6925 thread_info *
6926 linux_process_target::thread_pending_parent (thread_info *thread)
6928 lwp_info *parent = get_thread_lwp (thread)->pending_parent ();
6930 if (parent == nullptr)
6931 return nullptr;
6933 return get_lwp_thread (parent);
6936 thread_info *
6937 linux_process_target::thread_pending_child (thread_info *thread)
6939 lwp_info *child = get_thread_lwp (thread)->pending_child ();
6941 if (child == nullptr)
6942 return nullptr;
6944 return get_lwp_thread (child);
6947 /* Default implementation of linux_target_ops method "set_pc" for
6948 32-bit pc register which is literally named "pc". */
6950 void
6951 linux_set_pc_32bit (struct regcache *regcache, CORE_ADDR pc)
6953 uint32_t newpc = pc;
6955 supply_register_by_name (regcache, "pc", &newpc);
6958 /* Default implementation of linux_target_ops method "get_pc" for
6959 32-bit pc register which is literally named "pc". */
6961 CORE_ADDR
6962 linux_get_pc_32bit (struct regcache *regcache)
6964 uint32_t pc;
6966 collect_register_by_name (regcache, "pc", &pc);
6967 threads_debug_printf ("stop pc is 0x%" PRIx32, pc);
6968 return pc;
6971 /* Default implementation of linux_target_ops method "set_pc" for
6972 64-bit pc register which is literally named "pc". */
6974 void
6975 linux_set_pc_64bit (struct regcache *regcache, CORE_ADDR pc)
6977 uint64_t newpc = pc;
6979 supply_register_by_name (regcache, "pc", &newpc);
6982 /* Default implementation of linux_target_ops method "get_pc" for
6983 64-bit pc register which is literally named "pc". */
6985 CORE_ADDR
6986 linux_get_pc_64bit (struct regcache *regcache)
6988 uint64_t pc;
6990 collect_register_by_name (regcache, "pc", &pc);
6991 threads_debug_printf ("stop pc is 0x%" PRIx64, pc);
6992 return pc;
6995 /* See linux-low.h. */
6998 linux_get_auxv (int pid, int wordsize, CORE_ADDR match, CORE_ADDR *valp)
7000 gdb_byte *data = (gdb_byte *) alloca (2 * wordsize);
7001 int offset = 0;
7003 gdb_assert (wordsize == 4 || wordsize == 8);
7005 while (the_target->read_auxv (pid, offset, data, 2 * wordsize)
7006 == 2 * wordsize)
7008 if (wordsize == 4)
7010 uint32_t *data_p = (uint32_t *) data;
7011 if (data_p[0] == match)
7013 *valp = data_p[1];
7014 return 1;
7017 else
7019 uint64_t *data_p = (uint64_t *) data;
7020 if (data_p[0] == match)
7022 *valp = data_p[1];
7023 return 1;
7027 offset += 2 * wordsize;
7030 return 0;
7033 /* See linux-low.h. */
7035 CORE_ADDR
7036 linux_get_hwcap (int pid, int wordsize)
7038 CORE_ADDR hwcap = 0;
7039 linux_get_auxv (pid, wordsize, AT_HWCAP, &hwcap);
7040 return hwcap;
7043 /* See linux-low.h. */
7045 CORE_ADDR
7046 linux_get_hwcap2 (int pid, int wordsize)
7048 CORE_ADDR hwcap2 = 0;
7049 linux_get_auxv (pid, wordsize, AT_HWCAP2, &hwcap2);
7050 return hwcap2;
7053 #ifdef HAVE_LINUX_REGSETS
7054 void
7055 initialize_regsets_info (struct regsets_info *info)
7057 for (info->num_regsets = 0;
7058 info->regsets[info->num_regsets].size >= 0;
7059 info->num_regsets++)
7062 #endif
7064 void
7065 initialize_low (void)
7067 struct sigaction sigchld_action;
7069 memset (&sigchld_action, 0, sizeof (sigchld_action));
7070 set_target_ops (the_linux_target);
7072 linux_ptrace_init_warnings ();
7073 linux_proc_init_warnings ();
7075 sigchld_action.sa_handler = sigchld_handler;
7076 sigemptyset (&sigchld_action.sa_mask);
7077 sigchld_action.sa_flags = SA_RESTART;
7078 sigaction (SIGCHLD, &sigchld_action, NULL);
7080 initialize_low_arch ();
7082 linux_check_ptrace_features ();