Automatic date update in version.in
[binutils-gdb.git] / gdbserver / linux-low.cc
blobd68d4199f26b3f10744cfd1377bb098e8c80eba2
1 /* Low level interface to ptrace, for the remote server for GDB.
2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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 process_info *
407 linux_process_target::add_linux_process (int pid, int attached)
409 struct process_info *proc;
411 proc = add_process (pid, attached);
412 proc->priv = XCNEW (struct process_info_private);
414 proc->priv->arch_private = low_new_process ();
416 return proc;
419 arch_process_info *
420 linux_process_target::low_new_process ()
422 return nullptr;
425 void
426 linux_process_target::low_delete_process (arch_process_info *info)
428 /* Default implementation must be overridden if architecture-specific
429 info exists. */
430 gdb_assert (info == nullptr);
433 void
434 linux_process_target::low_new_fork (process_info *parent, process_info *child)
436 /* Nop. */
439 void
440 linux_process_target::arch_setup_thread (thread_info *thread)
442 scoped_restore_current_thread restore_thread;
443 switch_to_thread (thread);
445 low_arch_setup ();
449 linux_process_target::handle_extended_wait (lwp_info **orig_event_lwp,
450 int wstat)
452 client_state &cs = get_client_state ();
453 struct lwp_info *event_lwp = *orig_event_lwp;
454 int event = linux_ptrace_get_extended_event (wstat);
455 struct thread_info *event_thr = get_lwp_thread (event_lwp);
456 struct lwp_info *new_lwp;
458 gdb_assert (event_lwp->waitstatus.kind () == TARGET_WAITKIND_IGNORE);
460 /* All extended events we currently use are mid-syscall. Only
461 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
462 you have to be using PTRACE_SEIZE to get that. */
463 event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
465 if ((event == PTRACE_EVENT_FORK) || (event == PTRACE_EVENT_VFORK)
466 || (event == PTRACE_EVENT_CLONE))
468 ptid_t ptid;
469 unsigned long new_pid;
470 int ret, status;
472 /* Get the pid of the new lwp. */
473 ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_thr), (PTRACE_TYPE_ARG3) 0,
474 &new_pid);
476 /* If we haven't already seen the new PID stop, wait for it now. */
477 if (!pull_pid_from_list (&stopped_pids, new_pid, &status))
479 /* The new child has a pending SIGSTOP. We can't affect it until it
480 hits the SIGSTOP, but we're already attached. */
482 ret = my_waitpid (new_pid, &status, __WALL);
484 if (ret == -1)
485 perror_with_name ("waiting for new child");
486 else if (ret != new_pid)
487 warning ("wait returned unexpected PID %d", ret);
488 else if (!WIFSTOPPED (status))
489 warning ("wait returned unexpected status 0x%x", status);
492 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
494 struct process_info *parent_proc;
495 struct process_info *child_proc;
496 struct lwp_info *child_lwp;
497 struct thread_info *child_thr;
499 ptid = ptid_t (new_pid, new_pid);
501 threads_debug_printf ("Got fork event from LWP %ld, "
502 "new child is %d",
503 ptid_of (event_thr).lwp (),
504 ptid.pid ());
506 /* Add the new process to the tables and clone the breakpoint
507 lists of the parent. We need to do this even if the new process
508 will be detached, since we will need the process object and the
509 breakpoints to remove any breakpoints from memory when we
510 detach, and the client side will access registers. */
511 child_proc = add_linux_process (new_pid, 0);
512 gdb_assert (child_proc != NULL);
513 child_lwp = add_lwp (ptid);
514 gdb_assert (child_lwp != NULL);
515 child_lwp->stopped = 1;
516 child_lwp->must_set_ptrace_flags = 1;
517 child_lwp->status_pending_p = 0;
518 child_thr = get_lwp_thread (child_lwp);
519 child_thr->last_resume_kind = resume_stop;
520 child_thr->last_status.set_stopped (GDB_SIGNAL_0);
522 /* If we're suspending all threads, leave this one suspended
523 too. If the fork/clone parent is stepping over a breakpoint,
524 all other threads have been suspended already. Leave the
525 child suspended too. */
526 if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
527 || event_lwp->bp_reinsert != 0)
529 threads_debug_printf ("leaving child suspended");
530 child_lwp->suspended = 1;
533 parent_proc = get_thread_process (event_thr);
534 child_proc->attached = parent_proc->attached;
536 if (event_lwp->bp_reinsert != 0
537 && supports_software_single_step ()
538 && event == PTRACE_EVENT_VFORK)
540 /* If we leave single-step breakpoints there, child will
541 hit it, so uninsert single-step breakpoints from parent
542 (and child). Once vfork child is done, reinsert
543 them back to parent. */
544 uninsert_single_step_breakpoints (event_thr);
547 clone_all_breakpoints (child_thr, event_thr);
549 target_desc_up tdesc = allocate_target_description ();
550 copy_target_description (tdesc.get (), parent_proc->tdesc);
551 child_proc->tdesc = tdesc.release ();
553 /* Clone arch-specific process data. */
554 low_new_fork (parent_proc, child_proc);
556 /* Save fork info in the parent thread. */
557 if (event == PTRACE_EVENT_FORK)
558 event_lwp->waitstatus.set_forked (ptid);
559 else if (event == PTRACE_EVENT_VFORK)
560 event_lwp->waitstatus.set_vforked (ptid);
562 /* The status_pending field contains bits denoting the
563 extended event, so when the pending event is handled,
564 the handler will look at lwp->waitstatus. */
565 event_lwp->status_pending_p = 1;
566 event_lwp->status_pending = wstat;
568 /* Link the threads until the parent event is passed on to
569 higher layers. */
570 event_lwp->fork_relative = child_lwp;
571 child_lwp->fork_relative = event_lwp;
573 /* If the parent thread is doing step-over with single-step
574 breakpoints, the list of single-step breakpoints are cloned
575 from the parent's. Remove them from the child process.
576 In case of vfork, we'll reinsert them back once vforked
577 child is done. */
578 if (event_lwp->bp_reinsert != 0
579 && supports_software_single_step ())
581 /* The child process is forked and stopped, so it is safe
582 to access its memory without stopping all other threads
583 from other processes. */
584 delete_single_step_breakpoints (child_thr);
586 gdb_assert (has_single_step_breakpoints (event_thr));
587 gdb_assert (!has_single_step_breakpoints (child_thr));
590 /* Report the event. */
591 return 0;
594 threads_debug_printf
595 ("Got clone event from LWP %ld, new child is LWP %ld",
596 lwpid_of (event_thr), new_pid);
598 ptid = ptid_t (pid_of (event_thr), new_pid);
599 new_lwp = add_lwp (ptid);
601 /* Either we're going to immediately resume the new thread
602 or leave it stopped. resume_one_lwp is a nop if it
603 thinks the thread is currently running, so set this first
604 before calling resume_one_lwp. */
605 new_lwp->stopped = 1;
607 /* If we're suspending all threads, leave this one suspended
608 too. If the fork/clone parent is stepping over a breakpoint,
609 all other threads have been suspended already. Leave the
610 child suspended too. */
611 if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
612 || event_lwp->bp_reinsert != 0)
613 new_lwp->suspended = 1;
615 /* Normally we will get the pending SIGSTOP. But in some cases
616 we might get another signal delivered to the group first.
617 If we do get another signal, be sure not to lose it. */
618 if (WSTOPSIG (status) != SIGSTOP)
620 new_lwp->stop_expected = 1;
621 new_lwp->status_pending_p = 1;
622 new_lwp->status_pending = status;
624 else if (cs.report_thread_events)
626 new_lwp->waitstatus.set_thread_created ();
627 new_lwp->status_pending_p = 1;
628 new_lwp->status_pending = status;
631 #ifdef USE_THREAD_DB
632 thread_db_notice_clone (event_thr, ptid);
633 #endif
635 /* Don't report the event. */
636 return 1;
638 else if (event == PTRACE_EVENT_VFORK_DONE)
640 event_lwp->waitstatus.set_vfork_done ();
642 if (event_lwp->bp_reinsert != 0 && supports_software_single_step ())
644 reinsert_single_step_breakpoints (event_thr);
646 gdb_assert (has_single_step_breakpoints (event_thr));
649 /* Report the event. */
650 return 0;
652 else if (event == PTRACE_EVENT_EXEC && cs.report_exec_events)
654 struct process_info *proc;
655 std::vector<int> syscalls_to_catch;
656 ptid_t event_ptid;
657 pid_t event_pid;
659 threads_debug_printf ("Got exec event from LWP %ld",
660 lwpid_of (event_thr));
662 /* Get the event ptid. */
663 event_ptid = ptid_of (event_thr);
664 event_pid = event_ptid.pid ();
666 /* Save the syscall list from the execing process. */
667 proc = get_thread_process (event_thr);
668 syscalls_to_catch = std::move (proc->syscalls_to_catch);
670 /* Delete the execing process and all its threads. */
671 mourn (proc);
672 switch_to_thread (nullptr);
674 /* Create a new process/lwp/thread. */
675 proc = add_linux_process (event_pid, 0);
676 event_lwp = add_lwp (event_ptid);
677 event_thr = get_lwp_thread (event_lwp);
678 gdb_assert (current_thread == event_thr);
679 arch_setup_thread (event_thr);
681 /* Set the event status. */
682 event_lwp->waitstatus.set_execd
683 (make_unique_xstrdup
684 (linux_proc_pid_to_exec_file (lwpid_of (event_thr))));
686 /* Mark the exec status as pending. */
687 event_lwp->stopped = 1;
688 event_lwp->status_pending_p = 1;
689 event_lwp->status_pending = wstat;
690 event_thr->last_resume_kind = resume_continue;
691 event_thr->last_status.set_ignore ();
693 /* Update syscall state in the new lwp, effectively mid-syscall too. */
694 event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
696 /* Restore the list to catch. Don't rely on the client, which is free
697 to avoid sending a new list when the architecture doesn't change.
698 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
699 proc->syscalls_to_catch = std::move (syscalls_to_catch);
701 /* Report the event. */
702 *orig_event_lwp = event_lwp;
703 return 0;
706 internal_error (__FILE__, __LINE__, _("unknown ptrace event %d"), event);
709 CORE_ADDR
710 linux_process_target::get_pc (lwp_info *lwp)
712 struct regcache *regcache;
713 CORE_ADDR pc;
715 if (!low_supports_breakpoints ())
716 return 0;
718 scoped_restore_current_thread restore_thread;
719 switch_to_thread (get_lwp_thread (lwp));
721 regcache = get_thread_regcache (current_thread, 1);
722 pc = low_get_pc (regcache);
724 threads_debug_printf ("pc is 0x%lx", (long) pc);
726 return pc;
729 void
730 linux_process_target::get_syscall_trapinfo (lwp_info *lwp, int *sysno)
732 struct regcache *regcache;
734 scoped_restore_current_thread restore_thread;
735 switch_to_thread (get_lwp_thread (lwp));
737 regcache = get_thread_regcache (current_thread, 1);
738 low_get_syscall_trapinfo (regcache, sysno);
740 threads_debug_printf ("get_syscall_trapinfo sysno %d", *sysno);
743 void
744 linux_process_target::low_get_syscall_trapinfo (regcache *regcache, int *sysno)
746 /* By default, report an unknown system call number. */
747 *sysno = UNKNOWN_SYSCALL;
750 bool
751 linux_process_target::save_stop_reason (lwp_info *lwp)
753 CORE_ADDR pc;
754 CORE_ADDR sw_breakpoint_pc;
755 #if USE_SIGTRAP_SIGINFO
756 siginfo_t siginfo;
757 #endif
759 if (!low_supports_breakpoints ())
760 return false;
762 pc = get_pc (lwp);
763 sw_breakpoint_pc = pc - low_decr_pc_after_break ();
765 /* breakpoint_at reads from the current thread. */
766 scoped_restore_current_thread restore_thread;
767 switch_to_thread (get_lwp_thread (lwp));
769 #if USE_SIGTRAP_SIGINFO
770 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
771 (PTRACE_TYPE_ARG3) 0, &siginfo) == 0)
773 if (siginfo.si_signo == SIGTRAP)
775 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)
776 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
778 /* The si_code is ambiguous on this arch -- check debug
779 registers. */
780 if (!check_stopped_by_watchpoint (lwp))
781 lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
783 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
785 /* If we determine the LWP stopped for a SW breakpoint,
786 trust it. Particularly don't check watchpoint
787 registers, because at least on s390, we'd find
788 stopped-by-watchpoint as long as there's a watchpoint
789 set. */
790 lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
792 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
794 /* This can indicate either a hardware breakpoint or
795 hardware watchpoint. Check debug registers. */
796 if (!check_stopped_by_watchpoint (lwp))
797 lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
799 else if (siginfo.si_code == TRAP_TRACE)
801 /* We may have single stepped an instruction that
802 triggered a watchpoint. In that case, on some
803 architectures (such as x86), instead of TRAP_HWBKPT,
804 si_code indicates TRAP_TRACE, and we need to check
805 the debug registers separately. */
806 if (!check_stopped_by_watchpoint (lwp))
807 lwp->stop_reason = TARGET_STOPPED_BY_SINGLE_STEP;
811 #else
812 /* We may have just stepped a breakpoint instruction. E.g., in
813 non-stop mode, GDB first tells the thread A to step a range, and
814 then the user inserts a breakpoint inside the range. In that
815 case we need to report the breakpoint PC. */
816 if ((!lwp->stepping || lwp->stop_pc == sw_breakpoint_pc)
817 && low_breakpoint_at (sw_breakpoint_pc))
818 lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
820 if (hardware_breakpoint_inserted_here (pc))
821 lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
823 if (lwp->stop_reason == TARGET_STOPPED_BY_NO_REASON)
824 check_stopped_by_watchpoint (lwp);
825 #endif
827 if (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT)
829 threads_debug_printf
830 ("%s stopped by software breakpoint",
831 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
833 /* Back up the PC if necessary. */
834 if (pc != sw_breakpoint_pc)
836 struct regcache *regcache
837 = get_thread_regcache (current_thread, 1);
838 low_set_pc (regcache, sw_breakpoint_pc);
841 /* Update this so we record the correct stop PC below. */
842 pc = sw_breakpoint_pc;
844 else if (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
845 threads_debug_printf
846 ("%s stopped by hardware breakpoint",
847 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
848 else if (lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
849 threads_debug_printf
850 ("%s stopped by hardware watchpoint",
851 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
852 else if (lwp->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP)
853 threads_debug_printf
854 ("%s stopped by trace",
855 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
857 lwp->stop_pc = pc;
858 return true;
861 lwp_info *
862 linux_process_target::add_lwp (ptid_t ptid)
864 lwp_info *lwp = new lwp_info;
866 lwp->thread = add_thread (ptid, lwp);
868 low_new_thread (lwp);
870 return lwp;
873 void
874 linux_process_target::low_new_thread (lwp_info *info)
876 /* Nop. */
879 /* Callback to be used when calling fork_inferior, responsible for
880 actually initiating the tracing of the inferior. */
882 static void
883 linux_ptrace_fun ()
885 if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
886 (PTRACE_TYPE_ARG4) 0) < 0)
887 trace_start_error_with_name ("ptrace");
889 if (setpgid (0, 0) < 0)
890 trace_start_error_with_name ("setpgid");
892 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
893 stdout to stderr so that inferior i/o doesn't corrupt the connection.
894 Also, redirect stdin to /dev/null. */
895 if (remote_connection_is_stdio ())
897 if (close (0) < 0)
898 trace_start_error_with_name ("close");
899 if (open ("/dev/null", O_RDONLY) < 0)
900 trace_start_error_with_name ("open");
901 if (dup2 (2, 1) < 0)
902 trace_start_error_with_name ("dup2");
903 if (write (2, "stdin/stdout redirected\n",
904 sizeof ("stdin/stdout redirected\n") - 1) < 0)
906 /* Errors ignored. */;
911 /* Start an inferior process and returns its pid.
912 PROGRAM is the name of the program to be started, and PROGRAM_ARGS
913 are its arguments. */
916 linux_process_target::create_inferior (const char *program,
917 const std::vector<char *> &program_args)
919 client_state &cs = get_client_state ();
920 struct lwp_info *new_lwp;
921 int pid;
922 ptid_t ptid;
925 maybe_disable_address_space_randomization restore_personality
926 (cs.disable_randomization);
927 std::string str_program_args = construct_inferior_arguments (program_args);
929 pid = fork_inferior (program,
930 str_program_args.c_str (),
931 get_environ ()->envp (), linux_ptrace_fun,
932 NULL, NULL, NULL, NULL);
935 add_linux_process (pid, 0);
937 ptid = ptid_t (pid, pid);
938 new_lwp = add_lwp (ptid);
939 new_lwp->must_set_ptrace_flags = 1;
941 post_fork_inferior (pid, program);
943 return pid;
946 /* Implement the post_create_inferior target_ops method. */
948 void
949 linux_process_target::post_create_inferior ()
951 struct lwp_info *lwp = get_thread_lwp (current_thread);
953 low_arch_setup ();
955 if (lwp->must_set_ptrace_flags)
957 struct process_info *proc = current_process ();
958 int options = linux_low_ptrace_options (proc->attached);
960 linux_enable_event_reporting (lwpid_of (current_thread), options);
961 lwp->must_set_ptrace_flags = 0;
966 linux_process_target::attach_lwp (ptid_t ptid)
968 struct lwp_info *new_lwp;
969 int lwpid = ptid.lwp ();
971 if (ptrace (PTRACE_ATTACH, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0)
972 != 0)
973 return errno;
975 new_lwp = add_lwp (ptid);
977 /* We need to wait for SIGSTOP before being able to make the next
978 ptrace call on this LWP. */
979 new_lwp->must_set_ptrace_flags = 1;
981 if (linux_proc_pid_is_stopped (lwpid))
983 threads_debug_printf ("Attached to a stopped process");
985 /* The process is definitely stopped. It is in a job control
986 stop, unless the kernel predates the TASK_STOPPED /
987 TASK_TRACED distinction, in which case it might be in a
988 ptrace stop. Make sure it is in a ptrace stop; from there we
989 can kill it, signal it, et cetera.
991 First make sure there is a pending SIGSTOP. Since we are
992 already attached, the process can not transition from stopped
993 to running without a PTRACE_CONT; so we know this signal will
994 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
995 probably already in the queue (unless this kernel is old
996 enough to use TASK_STOPPED for ptrace stops); but since
997 SIGSTOP is not an RT signal, it can only be queued once. */
998 kill_lwp (lwpid, SIGSTOP);
1000 /* Finally, resume the stopped process. This will deliver the
1001 SIGSTOP (or a higher priority signal, just like normal
1002 PTRACE_ATTACH), which we'll catch later on. */
1003 ptrace (PTRACE_CONT, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
1006 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
1007 brings it to a halt.
1009 There are several cases to consider here:
1011 1) gdbserver has already attached to the process and is being notified
1012 of a new thread that is being created.
1013 In this case we should ignore that SIGSTOP and resume the
1014 process. This is handled below by setting stop_expected = 1,
1015 and the fact that add_thread sets last_resume_kind ==
1016 resume_continue.
1018 2) This is the first thread (the process thread), and we're attaching
1019 to it via attach_inferior.
1020 In this case we want the process thread to stop.
1021 This is handled by having linux_attach set last_resume_kind ==
1022 resume_stop after we return.
1024 If the pid we are attaching to is also the tgid, we attach to and
1025 stop all the existing threads. Otherwise, we attach to pid and
1026 ignore any other threads in the same group as this pid.
1028 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1029 existing threads.
1030 In this case we want the thread to stop.
1031 FIXME: This case is currently not properly handled.
1032 We should wait for the SIGSTOP but don't. Things work apparently
1033 because enough time passes between when we ptrace (ATTACH) and when
1034 gdb makes the next ptrace call on the thread.
1036 On the other hand, if we are currently trying to stop all threads, we
1037 should treat the new thread as if we had sent it a SIGSTOP. This works
1038 because we are guaranteed that the add_lwp call above added us to the
1039 end of the list, and so the new thread has not yet reached
1040 wait_for_sigstop (but will). */
1041 new_lwp->stop_expected = 1;
1043 return 0;
1046 /* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1047 already attached. Returns true if a new LWP is found, false
1048 otherwise. */
1050 static int
1051 attach_proc_task_lwp_callback (ptid_t ptid)
1053 /* Is this a new thread? */
1054 if (find_thread_ptid (ptid) == NULL)
1056 int lwpid = ptid.lwp ();
1057 int err;
1059 threads_debug_printf ("Found new lwp %d", lwpid);
1061 err = the_linux_target->attach_lwp (ptid);
1063 /* Be quiet if we simply raced with the thread exiting. EPERM
1064 is returned if the thread's task still exists, and is marked
1065 as exited or zombie, as well as other conditions, so in that
1066 case, confirm the status in /proc/PID/status. */
1067 if (err == ESRCH
1068 || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
1069 threads_debug_printf
1070 ("Cannot attach to lwp %d: thread is gone (%d: %s)",
1071 lwpid, err, safe_strerror (err));
1072 else if (err != 0)
1074 std::string reason
1075 = linux_ptrace_attach_fail_reason_string (ptid, err);
1077 warning (_("Cannot attach to lwp %d: %s"), lwpid, reason.c_str ());
1080 return 1;
1082 return 0;
1085 static void async_file_mark (void);
1087 /* Attach to PID. If PID is the tgid, attach to it and all
1088 of its threads. */
1091 linux_process_target::attach (unsigned long pid)
1093 struct process_info *proc;
1094 struct thread_info *initial_thread;
1095 ptid_t ptid = ptid_t (pid, pid);
1096 int err;
1098 proc = add_linux_process (pid, 1);
1100 /* Attach to PID. We will check for other threads
1101 soon. */
1102 err = attach_lwp (ptid);
1103 if (err != 0)
1105 remove_process (proc);
1107 std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
1108 error ("Cannot attach to process %ld: %s", pid, reason.c_str ());
1111 /* Don't ignore the initial SIGSTOP if we just attached to this
1112 process. It will be collected by wait shortly. */
1113 initial_thread = find_thread_ptid (ptid_t (pid, pid));
1114 initial_thread->last_resume_kind = resume_stop;
1116 /* We must attach to every LWP. If /proc is mounted, use that to
1117 find them now. On the one hand, the inferior may be using raw
1118 clone instead of using pthreads. On the other hand, even if it
1119 is using pthreads, GDB may not be connected yet (thread_db needs
1120 to do symbol lookups, through qSymbol). Also, thread_db walks
1121 structures in the inferior's address space to find the list of
1122 threads/LWPs, and those structures may well be corrupted. Note
1123 that once thread_db is loaded, we'll still use it to list threads
1124 and associate pthread info with each LWP. */
1125 linux_proc_attach_tgid_threads (pid, attach_proc_task_lwp_callback);
1127 /* GDB will shortly read the xml target description for this
1128 process, to figure out the process' architecture. But the target
1129 description is only filled in when the first process/thread in
1130 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1131 that now, otherwise, if GDB is fast enough, it could read the
1132 target description _before_ that initial stop. */
1133 if (non_stop)
1135 struct lwp_info *lwp;
1136 int wstat, lwpid;
1137 ptid_t pid_ptid = ptid_t (pid);
1139 lwpid = wait_for_event_filtered (pid_ptid, pid_ptid, &wstat, __WALL);
1140 gdb_assert (lwpid > 0);
1142 lwp = find_lwp_pid (ptid_t (lwpid));
1144 if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGSTOP)
1146 lwp->status_pending_p = 1;
1147 lwp->status_pending = wstat;
1150 initial_thread->last_resume_kind = resume_continue;
1152 async_file_mark ();
1154 gdb_assert (proc->tdesc != NULL);
1157 return 0;
1160 static int
1161 last_thread_of_process_p (int pid)
1163 bool seen_one = false;
1165 thread_info *thread = find_thread (pid, [&] (thread_info *thr_arg)
1167 if (!seen_one)
1169 /* This is the first thread of this process we see. */
1170 seen_one = true;
1171 return false;
1173 else
1175 /* This is the second thread of this process we see. */
1176 return true;
1180 return thread == NULL;
1183 /* Kill LWP. */
1185 static void
1186 linux_kill_one_lwp (struct lwp_info *lwp)
1188 struct thread_info *thr = get_lwp_thread (lwp);
1189 int pid = lwpid_of (thr);
1191 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1192 there is no signal context, and ptrace(PTRACE_KILL) (or
1193 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1194 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1195 alternative is to kill with SIGKILL. We only need one SIGKILL
1196 per process, not one for each thread. But since we still support
1197 support debugging programs using raw clone without CLONE_THREAD,
1198 we send one for each thread. For years, we used PTRACE_KILL
1199 only, so we're being a bit paranoid about some old kernels where
1200 PTRACE_KILL might work better (dubious if there are any such, but
1201 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1202 second, and so we're fine everywhere. */
1204 errno = 0;
1205 kill_lwp (pid, SIGKILL);
1206 if (debug_threads)
1208 int save_errno = errno;
1210 threads_debug_printf ("kill_lwp (SIGKILL) %s, 0, 0 (%s)",
1211 target_pid_to_str (ptid_of (thr)).c_str (),
1212 save_errno ? safe_strerror (save_errno) : "OK");
1215 errno = 0;
1216 ptrace (PTRACE_KILL, pid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
1217 if (debug_threads)
1219 int save_errno = errno;
1221 threads_debug_printf ("PTRACE_KILL %s, 0, 0 (%s)",
1222 target_pid_to_str (ptid_of (thr)).c_str (),
1223 save_errno ? safe_strerror (save_errno) : "OK");
1227 /* Kill LWP and wait for it to die. */
1229 static void
1230 kill_wait_lwp (struct lwp_info *lwp)
1232 struct thread_info *thr = get_lwp_thread (lwp);
1233 int pid = ptid_of (thr).pid ();
1234 int lwpid = ptid_of (thr).lwp ();
1235 int wstat;
1236 int res;
1238 threads_debug_printf ("killing lwp %d, for pid: %d", lwpid, pid);
1242 linux_kill_one_lwp (lwp);
1244 /* Make sure it died. Notes:
1246 - The loop is most likely unnecessary.
1248 - We don't use wait_for_event as that could delete lwps
1249 while we're iterating over them. We're not interested in
1250 any pending status at this point, only in making sure all
1251 wait status on the kernel side are collected until the
1252 process is reaped.
1254 - We don't use __WALL here as the __WALL emulation relies on
1255 SIGCHLD, and killing a stopped process doesn't generate
1256 one, nor an exit status.
1258 res = my_waitpid (lwpid, &wstat, 0);
1259 if (res == -1 && errno == ECHILD)
1260 res = my_waitpid (lwpid, &wstat, __WCLONE);
1261 } while (res > 0 && WIFSTOPPED (wstat));
1263 /* Even if it was stopped, the child may have already disappeared.
1264 E.g., if it was killed by SIGKILL. */
1265 if (res < 0 && errno != ECHILD)
1266 perror_with_name ("kill_wait_lwp");
1269 /* Callback for `for_each_thread'. Kills an lwp of a given process,
1270 except the leader. */
1272 static void
1273 kill_one_lwp_callback (thread_info *thread, int pid)
1275 struct lwp_info *lwp = get_thread_lwp (thread);
1277 /* We avoid killing the first thread here, because of a Linux kernel (at
1278 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1279 the children get a chance to be reaped, it will remain a zombie
1280 forever. */
1282 if (lwpid_of (thread) == pid)
1284 threads_debug_printf ("is last of process %s",
1285 target_pid_to_str (thread->id).c_str ());
1286 return;
1289 kill_wait_lwp (lwp);
1293 linux_process_target::kill (process_info *process)
1295 int pid = process->pid;
1297 /* If we're killing a running inferior, make sure it is stopped
1298 first, as PTRACE_KILL will not work otherwise. */
1299 stop_all_lwps (0, NULL);
1301 for_each_thread (pid, [&] (thread_info *thread)
1303 kill_one_lwp_callback (thread, pid);
1306 /* See the comment in linux_kill_one_lwp. We did not kill the first
1307 thread in the list, so do so now. */
1308 lwp_info *lwp = find_lwp_pid (ptid_t (pid));
1310 if (lwp == NULL)
1311 threads_debug_printf ("cannot find lwp for pid: %d", pid);
1312 else
1313 kill_wait_lwp (lwp);
1315 mourn (process);
1317 /* Since we presently can only stop all lwps of all processes, we
1318 need to unstop lwps of other processes. */
1319 unstop_all_lwps (0, NULL);
1320 return 0;
1323 /* Get pending signal of THREAD, for detaching purposes. This is the
1324 signal the thread last stopped for, which we need to deliver to the
1325 thread when detaching, otherwise, it'd be suppressed/lost. */
1327 static int
1328 get_detach_signal (struct thread_info *thread)
1330 client_state &cs = get_client_state ();
1331 enum gdb_signal signo = GDB_SIGNAL_0;
1332 int status;
1333 struct lwp_info *lp = get_thread_lwp (thread);
1335 if (lp->status_pending_p)
1336 status = lp->status_pending;
1337 else
1339 /* If the thread had been suspended by gdbserver, and it stopped
1340 cleanly, then it'll have stopped with SIGSTOP. But we don't
1341 want to deliver that SIGSTOP. */
1342 if (thread->last_status.kind () != TARGET_WAITKIND_STOPPED
1343 || thread->last_status.sig () == GDB_SIGNAL_0)
1344 return 0;
1346 /* Otherwise, we may need to deliver the signal we
1347 intercepted. */
1348 status = lp->last_status;
1351 if (!WIFSTOPPED (status))
1353 threads_debug_printf ("lwp %s hasn't stopped: no pending signal",
1354 target_pid_to_str (ptid_of (thread)).c_str ());
1355 return 0;
1358 /* Extended wait statuses aren't real SIGTRAPs. */
1359 if (WSTOPSIG (status) == SIGTRAP && linux_is_extended_waitstatus (status))
1361 threads_debug_printf ("lwp %s had stopped with extended "
1362 "status: no pending signal",
1363 target_pid_to_str (ptid_of (thread)).c_str ());
1364 return 0;
1367 signo = gdb_signal_from_host (WSTOPSIG (status));
1369 if (cs.program_signals_p && !cs.program_signals[signo])
1371 threads_debug_printf ("lwp %s had signal %s, but it is in nopass state",
1372 target_pid_to_str (ptid_of (thread)).c_str (),
1373 gdb_signal_to_string (signo));
1374 return 0;
1376 else if (!cs.program_signals_p
1377 /* If we have no way to know which signals GDB does not
1378 want to have passed to the program, assume
1379 SIGTRAP/SIGINT, which is GDB's default. */
1380 && (signo == GDB_SIGNAL_TRAP || signo == GDB_SIGNAL_INT))
1382 threads_debug_printf ("lwp %s had signal %s, "
1383 "but we don't know if we should pass it. "
1384 "Default to not.",
1385 target_pid_to_str (ptid_of (thread)).c_str (),
1386 gdb_signal_to_string (signo));
1387 return 0;
1389 else
1391 threads_debug_printf ("lwp %s has pending signal %s: delivering it",
1392 target_pid_to_str (ptid_of (thread)).c_str (),
1393 gdb_signal_to_string (signo));
1395 return WSTOPSIG (status);
1399 void
1400 linux_process_target::detach_one_lwp (lwp_info *lwp)
1402 struct thread_info *thread = get_lwp_thread (lwp);
1403 int sig;
1404 int lwpid;
1406 /* If there is a pending SIGSTOP, get rid of it. */
1407 if (lwp->stop_expected)
1409 threads_debug_printf ("Sending SIGCONT to %s",
1410 target_pid_to_str (ptid_of (thread)).c_str ());
1412 kill_lwp (lwpid_of (thread), SIGCONT);
1413 lwp->stop_expected = 0;
1416 /* Pass on any pending signal for this thread. */
1417 sig = get_detach_signal (thread);
1419 /* Preparing to resume may try to write registers, and fail if the
1420 lwp is zombie. If that happens, ignore the error. We'll handle
1421 it below, when detach fails with ESRCH. */
1424 /* Flush any pending changes to the process's registers. */
1425 regcache_invalidate_thread (thread);
1427 /* Finally, let it resume. */
1428 low_prepare_to_resume (lwp);
1430 catch (const gdb_exception_error &ex)
1432 if (!check_ptrace_stopped_lwp_gone (lwp))
1433 throw;
1436 lwpid = lwpid_of (thread);
1437 if (ptrace (PTRACE_DETACH, lwpid, (PTRACE_TYPE_ARG3) 0,
1438 (PTRACE_TYPE_ARG4) (long) sig) < 0)
1440 int save_errno = errno;
1442 /* We know the thread exists, so ESRCH must mean the lwp is
1443 zombie. This can happen if one of the already-detached
1444 threads exits the whole thread group. In that case we're
1445 still attached, and must reap the lwp. */
1446 if (save_errno == ESRCH)
1448 int ret, status;
1450 ret = my_waitpid (lwpid, &status, __WALL);
1451 if (ret == -1)
1453 warning (_("Couldn't reap LWP %d while detaching: %s"),
1454 lwpid, safe_strerror (errno));
1456 else if (!WIFEXITED (status) && !WIFSIGNALED (status))
1458 warning (_("Reaping LWP %d while detaching "
1459 "returned unexpected status 0x%x"),
1460 lwpid, status);
1463 else
1465 error (_("Can't detach %s: %s"),
1466 target_pid_to_str (ptid_of (thread)).c_str (),
1467 safe_strerror (save_errno));
1470 else
1471 threads_debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)",
1472 target_pid_to_str (ptid_of (thread)).c_str (),
1473 strsignal (sig));
1475 delete_lwp (lwp);
1479 linux_process_target::detach (process_info *process)
1481 struct lwp_info *main_lwp;
1483 /* As there's a step over already in progress, let it finish first,
1484 otherwise nesting a stabilize_threads operation on top gets real
1485 messy. */
1486 complete_ongoing_step_over ();
1488 /* Stop all threads before detaching. First, ptrace requires that
1489 the thread is stopped to successfully detach. Second, thread_db
1490 may need to uninstall thread event breakpoints from memory, which
1491 only works with a stopped process anyway. */
1492 stop_all_lwps (0, NULL);
1494 #ifdef USE_THREAD_DB
1495 thread_db_detach (process);
1496 #endif
1498 /* Stabilize threads (move out of jump pads). */
1499 target_stabilize_threads ();
1501 /* Detach from the clone lwps first. If the thread group exits just
1502 while we're detaching, we must reap the clone lwps before we're
1503 able to reap the leader. */
1504 for_each_thread (process->pid, [this] (thread_info *thread)
1506 /* We don't actually detach from the thread group leader just yet.
1507 If the thread group exits, we must reap the zombie clone lwps
1508 before we're able to reap the leader. */
1509 if (thread->id.pid () == thread->id.lwp ())
1510 return;
1512 lwp_info *lwp = get_thread_lwp (thread);
1513 detach_one_lwp (lwp);
1516 main_lwp = find_lwp_pid (ptid_t (process->pid));
1517 detach_one_lwp (main_lwp);
1519 mourn (process);
1521 /* Since we presently can only stop all lwps of all processes, we
1522 need to unstop lwps of other processes. */
1523 unstop_all_lwps (0, NULL);
1524 return 0;
1527 /* Remove all LWPs that belong to process PROC from the lwp list. */
1529 void
1530 linux_process_target::mourn (process_info *process)
1532 struct process_info_private *priv;
1534 #ifdef USE_THREAD_DB
1535 thread_db_mourn (process);
1536 #endif
1538 for_each_thread (process->pid, [this] (thread_info *thread)
1540 delete_lwp (get_thread_lwp (thread));
1543 /* Freeing all private data. */
1544 priv = process->priv;
1545 low_delete_process (priv->arch_private);
1546 free (priv);
1547 process->priv = NULL;
1549 remove_process (process);
1552 void
1553 linux_process_target::join (int pid)
1555 int status, ret;
1557 do {
1558 ret = my_waitpid (pid, &status, 0);
1559 if (WIFEXITED (status) || WIFSIGNALED (status))
1560 break;
1561 } while (ret != -1 || errno != ECHILD);
1564 /* Return true if the given thread is still alive. */
1566 bool
1567 linux_process_target::thread_alive (ptid_t ptid)
1569 struct lwp_info *lwp = find_lwp_pid (ptid);
1571 /* We assume we always know if a thread exits. If a whole process
1572 exited but we still haven't been able to report it to GDB, we'll
1573 hold on to the last lwp of the dead process. */
1574 if (lwp != NULL)
1575 return !lwp_is_marked_dead (lwp);
1576 else
1577 return 0;
1580 bool
1581 linux_process_target::thread_still_has_status_pending (thread_info *thread)
1583 struct lwp_info *lp = get_thread_lwp (thread);
1585 if (!lp->status_pending_p)
1586 return 0;
1588 if (thread->last_resume_kind != resume_stop
1589 && (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
1590 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT))
1592 CORE_ADDR pc;
1593 int discard = 0;
1595 gdb_assert (lp->last_status != 0);
1597 pc = get_pc (lp);
1599 scoped_restore_current_thread restore_thread;
1600 switch_to_thread (thread);
1602 if (pc != lp->stop_pc)
1604 threads_debug_printf ("PC of %ld changed",
1605 lwpid_of (thread));
1606 discard = 1;
1609 #if !USE_SIGTRAP_SIGINFO
1610 else if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
1611 && !low_breakpoint_at (pc))
1613 threads_debug_printf ("previous SW breakpoint of %ld gone",
1614 lwpid_of (thread));
1615 discard = 1;
1617 else if (lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT
1618 && !hardware_breakpoint_inserted_here (pc))
1620 threads_debug_printf ("previous HW breakpoint of %ld gone",
1621 lwpid_of (thread));
1622 discard = 1;
1624 #endif
1626 if (discard)
1628 threads_debug_printf ("discarding pending breakpoint status");
1629 lp->status_pending_p = 0;
1630 return 0;
1634 return 1;
1637 /* Returns true if LWP is resumed from the client's perspective. */
1639 static int
1640 lwp_resumed (struct lwp_info *lwp)
1642 struct thread_info *thread = get_lwp_thread (lwp);
1644 if (thread->last_resume_kind != resume_stop)
1645 return 1;
1647 /* Did gdb send us a `vCont;t', but we haven't reported the
1648 corresponding stop to gdb yet? If so, the thread is still
1649 resumed/running from gdb's perspective. */
1650 if (thread->last_resume_kind == resume_stop
1651 && thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
1652 return 1;
1654 return 0;
1657 bool
1658 linux_process_target::status_pending_p_callback (thread_info *thread,
1659 ptid_t ptid)
1661 struct lwp_info *lp = get_thread_lwp (thread);
1663 /* Check if we're only interested in events from a specific process
1664 or a specific LWP. */
1665 if (!thread->id.matches (ptid))
1666 return 0;
1668 if (!lwp_resumed (lp))
1669 return 0;
1671 if (lp->status_pending_p
1672 && !thread_still_has_status_pending (thread))
1674 resume_one_lwp (lp, lp->stepping, GDB_SIGNAL_0, NULL);
1675 return 0;
1678 return lp->status_pending_p;
1681 struct lwp_info *
1682 find_lwp_pid (ptid_t ptid)
1684 thread_info *thread = find_thread ([&] (thread_info *thr_arg)
1686 int lwp = ptid.lwp () != 0 ? ptid.lwp () : ptid.pid ();
1687 return thr_arg->id.lwp () == lwp;
1690 if (thread == NULL)
1691 return NULL;
1693 return get_thread_lwp (thread);
1696 /* Return the number of known LWPs in the tgid given by PID. */
1698 static int
1699 num_lwps (int pid)
1701 int count = 0;
1703 for_each_thread (pid, [&] (thread_info *thread)
1705 count++;
1708 return count;
1711 /* See nat/linux-nat.h. */
1713 struct lwp_info *
1714 iterate_over_lwps (ptid_t filter,
1715 gdb::function_view<iterate_over_lwps_ftype> callback)
1717 thread_info *thread = find_thread (filter, [&] (thread_info *thr_arg)
1719 lwp_info *lwp = get_thread_lwp (thr_arg);
1721 return callback (lwp);
1724 if (thread == NULL)
1725 return NULL;
1727 return get_thread_lwp (thread);
1730 void
1731 linux_process_target::check_zombie_leaders ()
1733 for_each_process ([this] (process_info *proc)
1735 pid_t leader_pid = pid_of (proc);
1736 lwp_info *leader_lp = find_lwp_pid (ptid_t (leader_pid));
1738 threads_debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1739 "num_lwps=%d, zombie=%d",
1740 leader_pid, leader_lp!= NULL, num_lwps (leader_pid),
1741 linux_proc_pid_is_zombie (leader_pid));
1743 if (leader_lp != NULL && !leader_lp->stopped
1744 /* Check if there are other threads in the group, as we may
1745 have raced with the inferior simply exiting. Note this
1746 isn't a watertight check. If the inferior is
1747 multi-threaded and is exiting, it may be we see the
1748 leader as zombie before we reap all the non-leader
1749 threads. See comments below. */
1750 && !last_thread_of_process_p (leader_pid)
1751 && linux_proc_pid_is_zombie (leader_pid))
1753 /* A zombie leader in a multi-threaded program can mean one
1754 of three things:
1756 #1 - Only the leader exited, not the whole program, e.g.,
1757 with pthread_exit. Since we can't reap the leader's exit
1758 status until all other threads are gone and reaped too,
1759 we want to delete the zombie leader right away, as it
1760 can't be debugged, we can't read its registers, etc.
1761 This is the main reason we check for zombie leaders
1762 disappearing.
1764 #2 - The whole thread-group/process exited (a group exit,
1765 via e.g. exit(3), and there is (or will be shortly) an
1766 exit reported for each thread in the process, and then
1767 finally an exit for the leader once the non-leaders are
1768 reaped.
1770 #3 - There are 3 or more threads in the group, and a
1771 thread other than the leader exec'd. See comments on
1772 exec events at the top of the file.
1774 Ideally we would never delete the leader for case #2.
1775 Instead, we want to collect the exit status of each
1776 non-leader thread, and then finally collect the exit
1777 status of the leader as normal and use its exit code as
1778 whole-process exit code. Unfortunately, there's no
1779 race-free way to distinguish cases #1 and #2. We can't
1780 assume the exit events for the non-leaders threads are
1781 already pending in the kernel, nor can we assume the
1782 non-leader threads are in zombie state already. Between
1783 the leader becoming zombie and the non-leaders exiting
1784 and becoming zombie themselves, there's a small time
1785 window, so such a check would be racy. Temporarily
1786 pausing all threads and checking to see if all threads
1787 exit or not before re-resuming them would work in the
1788 case that all threads are running right now, but it
1789 wouldn't work if some thread is currently already
1790 ptrace-stopped, e.g., due to scheduler-locking.
1792 So what we do is we delete the leader anyhow, and then
1793 later on when we see its exit status, we re-add it back.
1794 We also make sure that we only report a whole-process
1795 exit when we see the leader exiting, as opposed to when
1796 the last LWP in the LWP list exits, which can be a
1797 non-leader if we deleted the leader here. */
1798 threads_debug_printf ("Thread group leader %d zombie "
1799 "(it exited, or another thread execd), "
1800 "deleting it.",
1801 leader_pid);
1802 delete_lwp (leader_lp);
1807 /* Callback for `find_thread'. Returns the first LWP that is not
1808 stopped. */
1810 static bool
1811 not_stopped_callback (thread_info *thread, ptid_t filter)
1813 if (!thread->id.matches (filter))
1814 return false;
1816 lwp_info *lwp = get_thread_lwp (thread);
1818 return !lwp->stopped;
1821 /* Increment LWP's suspend count. */
1823 static void
1824 lwp_suspended_inc (struct lwp_info *lwp)
1826 lwp->suspended++;
1828 if (lwp->suspended > 4)
1829 threads_debug_printf
1830 ("LWP %ld has a suspiciously high suspend count, suspended=%d",
1831 lwpid_of (get_lwp_thread (lwp)), lwp->suspended);
1834 /* Decrement LWP's suspend count. */
1836 static void
1837 lwp_suspended_decr (struct lwp_info *lwp)
1839 lwp->suspended--;
1841 if (lwp->suspended < 0)
1843 struct thread_info *thread = get_lwp_thread (lwp);
1845 internal_error (__FILE__, __LINE__,
1846 "unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread),
1847 lwp->suspended);
1851 /* This function should only be called if the LWP got a SIGTRAP.
1853 Handle any tracepoint steps or hits. Return true if a tracepoint
1854 event was handled, 0 otherwise. */
1856 static int
1857 handle_tracepoints (struct lwp_info *lwp)
1859 struct thread_info *tinfo = get_lwp_thread (lwp);
1860 int tpoint_related_event = 0;
1862 gdb_assert (lwp->suspended == 0);
1864 /* If this tracepoint hit causes a tracing stop, we'll immediately
1865 uninsert tracepoints. To do this, we temporarily pause all
1866 threads, unpatch away, and then unpause threads. We need to make
1867 sure the unpausing doesn't resume LWP too. */
1868 lwp_suspended_inc (lwp);
1870 /* And we need to be sure that any all-threads-stopping doesn't try
1871 to move threads out of the jump pads, as it could deadlock the
1872 inferior (LWP could be in the jump pad, maybe even holding the
1873 lock.) */
1875 /* Do any necessary step collect actions. */
1876 tpoint_related_event |= tracepoint_finished_step (tinfo, lwp->stop_pc);
1878 tpoint_related_event |= handle_tracepoint_bkpts (tinfo, lwp->stop_pc);
1880 /* See if we just hit a tracepoint and do its main collect
1881 actions. */
1882 tpoint_related_event |= tracepoint_was_hit (tinfo, lwp->stop_pc);
1884 lwp_suspended_decr (lwp);
1886 gdb_assert (lwp->suspended == 0);
1887 gdb_assert (!stabilizing_threads
1888 || (lwp->collecting_fast_tracepoint
1889 != fast_tpoint_collect_result::not_collecting));
1891 if (tpoint_related_event)
1893 threads_debug_printf ("got a tracepoint event");
1894 return 1;
1897 return 0;
1900 fast_tpoint_collect_result
1901 linux_process_target::linux_fast_tracepoint_collecting
1902 (lwp_info *lwp, fast_tpoint_collect_status *status)
1904 CORE_ADDR thread_area;
1905 struct thread_info *thread = get_lwp_thread (lwp);
1907 /* Get the thread area address. This is used to recognize which
1908 thread is which when tracing with the in-process agent library.
1909 We don't read anything from the address, and treat it as opaque;
1910 it's the address itself that we assume is unique per-thread. */
1911 if (low_get_thread_area (lwpid_of (thread), &thread_area) == -1)
1912 return fast_tpoint_collect_result::not_collecting;
1914 return fast_tracepoint_collecting (thread_area, lwp->stop_pc, status);
1918 linux_process_target::low_get_thread_area (int lwpid, CORE_ADDR *addrp)
1920 return -1;
1923 bool
1924 linux_process_target::maybe_move_out_of_jump_pad (lwp_info *lwp, int *wstat)
1926 scoped_restore_current_thread restore_thread;
1927 switch_to_thread (get_lwp_thread (lwp));
1929 if ((wstat == NULL
1930 || (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) != SIGTRAP))
1931 && supports_fast_tracepoints ()
1932 && agent_loaded_p ())
1934 struct fast_tpoint_collect_status status;
1936 threads_debug_printf
1937 ("Checking whether LWP %ld needs to move out of the jump pad.",
1938 lwpid_of (current_thread));
1940 fast_tpoint_collect_result r
1941 = linux_fast_tracepoint_collecting (lwp, &status);
1943 if (wstat == NULL
1944 || (WSTOPSIG (*wstat) != SIGILL
1945 && WSTOPSIG (*wstat) != SIGFPE
1946 && WSTOPSIG (*wstat) != SIGSEGV
1947 && WSTOPSIG (*wstat) != SIGBUS))
1949 lwp->collecting_fast_tracepoint = r;
1951 if (r != fast_tpoint_collect_result::not_collecting)
1953 if (r == fast_tpoint_collect_result::before_insn
1954 && lwp->exit_jump_pad_bkpt == NULL)
1956 /* Haven't executed the original instruction yet.
1957 Set breakpoint there, and wait till it's hit,
1958 then single-step until exiting the jump pad. */
1959 lwp->exit_jump_pad_bkpt
1960 = set_breakpoint_at (status.adjusted_insn_addr, NULL);
1963 threads_debug_printf
1964 ("Checking whether LWP %ld needs to move out of the jump pad..."
1965 " it does", lwpid_of (current_thread));
1967 return true;
1970 else
1972 /* If we get a synchronous signal while collecting, *and*
1973 while executing the (relocated) original instruction,
1974 reset the PC to point at the tpoint address, before
1975 reporting to GDB. Otherwise, it's an IPA lib bug: just
1976 report the signal to GDB, and pray for the best. */
1978 lwp->collecting_fast_tracepoint
1979 = fast_tpoint_collect_result::not_collecting;
1981 if (r != fast_tpoint_collect_result::not_collecting
1982 && (status.adjusted_insn_addr <= lwp->stop_pc
1983 && lwp->stop_pc < status.adjusted_insn_addr_end))
1985 siginfo_t info;
1986 struct regcache *regcache;
1988 /* The si_addr on a few signals references the address
1989 of the faulting instruction. Adjust that as
1990 well. */
1991 if ((WSTOPSIG (*wstat) == SIGILL
1992 || WSTOPSIG (*wstat) == SIGFPE
1993 || WSTOPSIG (*wstat) == SIGBUS
1994 || WSTOPSIG (*wstat) == SIGSEGV)
1995 && ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
1996 (PTRACE_TYPE_ARG3) 0, &info) == 0
1997 /* Final check just to make sure we don't clobber
1998 the siginfo of non-kernel-sent signals. */
1999 && (uintptr_t) info.si_addr == lwp->stop_pc)
2001 info.si_addr = (void *) (uintptr_t) status.tpoint_addr;
2002 ptrace (PTRACE_SETSIGINFO, lwpid_of (current_thread),
2003 (PTRACE_TYPE_ARG3) 0, &info);
2006 regcache = get_thread_regcache (current_thread, 1);
2007 low_set_pc (regcache, status.tpoint_addr);
2008 lwp->stop_pc = status.tpoint_addr;
2010 /* Cancel any fast tracepoint lock this thread was
2011 holding. */
2012 force_unlock_trace_buffer ();
2015 if (lwp->exit_jump_pad_bkpt != NULL)
2017 threads_debug_printf
2018 ("Cancelling fast exit-jump-pad: removing bkpt."
2019 "stopping all threads momentarily.");
2021 stop_all_lwps (1, lwp);
2023 delete_breakpoint (lwp->exit_jump_pad_bkpt);
2024 lwp->exit_jump_pad_bkpt = NULL;
2026 unstop_all_lwps (1, lwp);
2028 gdb_assert (lwp->suspended >= 0);
2033 threads_debug_printf
2034 ("Checking whether LWP %ld needs to move out of the jump pad... no",
2035 lwpid_of (current_thread));
2037 return false;
2040 /* Enqueue one signal in the "signals to report later when out of the
2041 jump pad" list. */
2043 static void
2044 enqueue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
2046 struct thread_info *thread = get_lwp_thread (lwp);
2048 threads_debug_printf ("Deferring signal %d for LWP %ld.",
2049 WSTOPSIG (*wstat), lwpid_of (thread));
2051 if (debug_threads)
2053 for (const auto &sig : lwp->pending_signals_to_report)
2054 threads_debug_printf (" Already queued %d", sig.signal);
2056 threads_debug_printf (" (no more currently queued signals)");
2059 /* Don't enqueue non-RT signals if they are already in the deferred
2060 queue. (SIGSTOP being the easiest signal to see ending up here
2061 twice) */
2062 if (WSTOPSIG (*wstat) < __SIGRTMIN)
2064 for (const auto &sig : lwp->pending_signals_to_report)
2066 if (sig.signal == WSTOPSIG (*wstat))
2068 threads_debug_printf
2069 ("Not requeuing already queued non-RT signal %d for LWP %ld",
2070 sig.signal, lwpid_of (thread));
2071 return;
2076 lwp->pending_signals_to_report.emplace_back (WSTOPSIG (*wstat));
2078 ptrace (PTRACE_GETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
2079 &lwp->pending_signals_to_report.back ().info);
2082 /* Dequeue one signal from the "signals to report later when out of
2083 the jump pad" list. */
2085 static int
2086 dequeue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
2088 struct thread_info *thread = get_lwp_thread (lwp);
2090 if (!lwp->pending_signals_to_report.empty ())
2092 const pending_signal &p_sig = lwp->pending_signals_to_report.front ();
2094 *wstat = W_STOPCODE (p_sig.signal);
2095 if (p_sig.info.si_signo != 0)
2096 ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
2097 &p_sig.info);
2099 lwp->pending_signals_to_report.pop_front ();
2101 threads_debug_printf ("Reporting deferred signal %d for LWP %ld.",
2102 WSTOPSIG (*wstat), lwpid_of (thread));
2104 if (debug_threads)
2106 for (const auto &sig : lwp->pending_signals_to_report)
2107 threads_debug_printf (" Still queued %d", sig.signal);
2109 threads_debug_printf (" (no more queued signals)");
2112 return 1;
2115 return 0;
2118 bool
2119 linux_process_target::check_stopped_by_watchpoint (lwp_info *child)
2121 scoped_restore_current_thread restore_thread;
2122 switch_to_thread (get_lwp_thread (child));
2124 if (low_stopped_by_watchpoint ())
2126 child->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
2127 child->stopped_data_address = low_stopped_data_address ();
2130 return child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2133 bool
2134 linux_process_target::low_stopped_by_watchpoint ()
2136 return false;
2139 CORE_ADDR
2140 linux_process_target::low_stopped_data_address ()
2142 return 0;
2145 /* Return the ptrace options that we want to try to enable. */
2147 static int
2148 linux_low_ptrace_options (int attached)
2150 client_state &cs = get_client_state ();
2151 int options = 0;
2153 if (!attached)
2154 options |= PTRACE_O_EXITKILL;
2156 if (cs.report_fork_events)
2157 options |= PTRACE_O_TRACEFORK;
2159 if (cs.report_vfork_events)
2160 options |= (PTRACE_O_TRACEVFORK | PTRACE_O_TRACEVFORKDONE);
2162 if (cs.report_exec_events)
2163 options |= PTRACE_O_TRACEEXEC;
2165 options |= PTRACE_O_TRACESYSGOOD;
2167 return options;
2170 void
2171 linux_process_target::filter_event (int lwpid, int wstat)
2173 client_state &cs = get_client_state ();
2174 struct lwp_info *child;
2175 struct thread_info *thread;
2176 int have_stop_pc = 0;
2178 child = find_lwp_pid (ptid_t (lwpid));
2180 /* Check for events reported by anything not in our LWP list. */
2181 if (child == nullptr)
2183 if (WIFSTOPPED (wstat))
2185 if (WSTOPSIG (wstat) == SIGTRAP
2186 && linux_ptrace_get_extended_event (wstat) == PTRACE_EVENT_EXEC)
2188 /* A non-leader thread exec'ed after we've seen the
2189 leader zombie, and removed it from our lists (in
2190 check_zombie_leaders). The non-leader thread changes
2191 its tid to the tgid. */
2192 threads_debug_printf
2193 ("Re-adding thread group leader LWP %d after exec.",
2194 lwpid);
2196 child = add_lwp (ptid_t (lwpid, lwpid));
2197 child->stopped = 1;
2198 switch_to_thread (child->thread);
2200 else
2202 /* A process we are controlling has forked and the new
2203 child's stop was reported to us by the kernel. Save
2204 its PID and go back to waiting for the fork event to
2205 be reported - the stopped process might be returned
2206 from waitpid before or after the fork event is. */
2207 threads_debug_printf
2208 ("Saving LWP %d status %s in stopped_pids list",
2209 lwpid, status_to_str (wstat).c_str ());
2210 add_to_pid_list (&stopped_pids, lwpid, wstat);
2213 else
2215 /* Don't report an event for the exit of an LWP not in our
2216 list, i.e. not part of any inferior we're debugging.
2217 This can happen if we detach from a program we originally
2218 forked and then it exits. However, note that we may have
2219 earlier deleted a leader of an inferior we're debugging,
2220 in check_zombie_leaders. Re-add it back here if so. */
2221 find_process ([&] (process_info *proc)
2223 if (proc->pid == lwpid)
2225 threads_debug_printf
2226 ("Re-adding thread group leader LWP %d after exit.",
2227 lwpid);
2229 child = add_lwp (ptid_t (lwpid, lwpid));
2230 return true;
2232 return false;
2236 if (child == nullptr)
2237 return;
2240 thread = get_lwp_thread (child);
2242 child->stopped = 1;
2244 child->last_status = wstat;
2246 /* Check if the thread has exited. */
2247 if ((WIFEXITED (wstat) || WIFSIGNALED (wstat)))
2249 threads_debug_printf ("%d exited", lwpid);
2251 if (finish_step_over (child))
2253 /* Unsuspend all other LWPs, and set them back running again. */
2254 unsuspend_all_lwps (child);
2257 /* If this is not the leader LWP, then the exit signal was not
2258 the end of the debugged application and should be ignored,
2259 unless GDB wants to hear about thread exits. */
2260 if (cs.report_thread_events || is_leader (thread))
2262 /* Since events are serialized to GDB core, and we can't
2263 report this one right now. Leave the status pending for
2264 the next time we're able to report it. */
2265 mark_lwp_dead (child, wstat);
2266 return;
2268 else
2270 delete_lwp (child);
2271 return;
2275 gdb_assert (WIFSTOPPED (wstat));
2277 if (WIFSTOPPED (wstat))
2279 struct process_info *proc;
2281 /* Architecture-specific setup after inferior is running. */
2282 proc = find_process_pid (pid_of (thread));
2283 if (proc->tdesc == NULL)
2285 if (proc->attached)
2287 /* This needs to happen after we have attached to the
2288 inferior and it is stopped for the first time, but
2289 before we access any inferior registers. */
2290 arch_setup_thread (thread);
2292 else
2294 /* The process is started, but GDBserver will do
2295 architecture-specific setup after the program stops at
2296 the first instruction. */
2297 child->status_pending_p = 1;
2298 child->status_pending = wstat;
2299 return;
2304 if (WIFSTOPPED (wstat) && child->must_set_ptrace_flags)
2306 struct process_info *proc = find_process_pid (pid_of (thread));
2307 int options = linux_low_ptrace_options (proc->attached);
2309 linux_enable_event_reporting (lwpid, options);
2310 child->must_set_ptrace_flags = 0;
2313 /* Always update syscall_state, even if it will be filtered later. */
2314 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SYSCALL_SIGTRAP)
2316 child->syscall_state
2317 = (child->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
2318 ? TARGET_WAITKIND_SYSCALL_RETURN
2319 : TARGET_WAITKIND_SYSCALL_ENTRY);
2321 else
2323 /* Almost all other ptrace-stops are known to be outside of system
2324 calls, with further exceptions in handle_extended_wait. */
2325 child->syscall_state = TARGET_WAITKIND_IGNORE;
2328 /* Be careful to not overwrite stop_pc until save_stop_reason is
2329 called. */
2330 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGTRAP
2331 && linux_is_extended_waitstatus (wstat))
2333 child->stop_pc = get_pc (child);
2334 if (handle_extended_wait (&child, wstat))
2336 /* The event has been handled, so just return without
2337 reporting it. */
2338 return;
2342 if (linux_wstatus_maybe_breakpoint (wstat))
2344 if (save_stop_reason (child))
2345 have_stop_pc = 1;
2348 if (!have_stop_pc)
2349 child->stop_pc = get_pc (child);
2351 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGSTOP
2352 && child->stop_expected)
2354 threads_debug_printf ("Expected stop.");
2356 child->stop_expected = 0;
2358 if (thread->last_resume_kind == resume_stop)
2360 /* We want to report the stop to the core. Treat the
2361 SIGSTOP as a normal event. */
2362 threads_debug_printf ("resume_stop SIGSTOP caught for %s.",
2363 target_pid_to_str (ptid_of (thread)).c_str ());
2365 else if (stopping_threads != NOT_STOPPING_THREADS)
2367 /* Stopping threads. We don't want this SIGSTOP to end up
2368 pending. */
2369 threads_debug_printf ("SIGSTOP caught for %s while stopping threads.",
2370 target_pid_to_str (ptid_of (thread)).c_str ());
2371 return;
2373 else
2375 /* This is a delayed SIGSTOP. Filter out the event. */
2376 threads_debug_printf ("%s %s, 0, 0 (discard delayed SIGSTOP)",
2377 child->stepping ? "step" : "continue",
2378 target_pid_to_str (ptid_of (thread)).c_str ());
2380 resume_one_lwp (child, child->stepping, 0, NULL);
2381 return;
2385 child->status_pending_p = 1;
2386 child->status_pending = wstat;
2387 return;
2390 bool
2391 linux_process_target::maybe_hw_step (thread_info *thread)
2393 if (supports_hardware_single_step ())
2394 return true;
2395 else
2397 /* GDBserver must insert single-step breakpoint for software
2398 single step. */
2399 gdb_assert (has_single_step_breakpoints (thread));
2400 return false;
2404 void
2405 linux_process_target::resume_stopped_resumed_lwps (thread_info *thread)
2407 struct lwp_info *lp = get_thread_lwp (thread);
2409 if (lp->stopped
2410 && !lp->suspended
2411 && !lp->status_pending_p
2412 && thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
2414 int step = 0;
2416 if (thread->last_resume_kind == resume_step)
2417 step = maybe_hw_step (thread);
2419 threads_debug_printf ("resuming stopped-resumed LWP %s at %s: step=%d",
2420 target_pid_to_str (ptid_of (thread)).c_str (),
2421 paddress (lp->stop_pc), step);
2423 resume_one_lwp (lp, step, GDB_SIGNAL_0, NULL);
2428 linux_process_target::wait_for_event_filtered (ptid_t wait_ptid,
2429 ptid_t filter_ptid,
2430 int *wstatp, int options)
2432 struct thread_info *event_thread;
2433 struct lwp_info *event_child, *requested_child;
2434 sigset_t block_mask, prev_mask;
2436 retry:
2437 /* N.B. event_thread points to the thread_info struct that contains
2438 event_child. Keep them in sync. */
2439 event_thread = NULL;
2440 event_child = NULL;
2441 requested_child = NULL;
2443 /* Check for a lwp with a pending status. */
2445 if (filter_ptid == minus_one_ptid || filter_ptid.is_pid ())
2447 event_thread = find_thread_in_random ([&] (thread_info *thread)
2449 return status_pending_p_callback (thread, filter_ptid);
2452 if (event_thread != NULL)
2454 event_child = get_thread_lwp (event_thread);
2455 threads_debug_printf ("Got a pending child %ld", lwpid_of (event_thread));
2458 else if (filter_ptid != null_ptid)
2460 requested_child = find_lwp_pid (filter_ptid);
2462 if (stopping_threads == NOT_STOPPING_THREADS
2463 && requested_child->status_pending_p
2464 && (requested_child->collecting_fast_tracepoint
2465 != fast_tpoint_collect_result::not_collecting))
2467 enqueue_one_deferred_signal (requested_child,
2468 &requested_child->status_pending);
2469 requested_child->status_pending_p = 0;
2470 requested_child->status_pending = 0;
2471 resume_one_lwp (requested_child, 0, 0, NULL);
2474 if (requested_child->suspended
2475 && requested_child->status_pending_p)
2477 internal_error (__FILE__, __LINE__,
2478 "requesting an event out of a"
2479 " suspended child?");
2482 if (requested_child->status_pending_p)
2484 event_child = requested_child;
2485 event_thread = get_lwp_thread (event_child);
2489 if (event_child != NULL)
2491 threads_debug_printf ("Got an event from pending child %ld (%04x)",
2492 lwpid_of (event_thread),
2493 event_child->status_pending);
2495 *wstatp = event_child->status_pending;
2496 event_child->status_pending_p = 0;
2497 event_child->status_pending = 0;
2498 switch_to_thread (event_thread);
2499 return lwpid_of (event_thread);
2502 /* But if we don't find a pending event, we'll have to wait.
2504 We only enter this loop if no process has a pending wait status.
2505 Thus any action taken in response to a wait status inside this
2506 loop is responding as soon as we detect the status, not after any
2507 pending events. */
2509 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2510 all signals while here. */
2511 sigfillset (&block_mask);
2512 gdb_sigmask (SIG_BLOCK, &block_mask, &prev_mask);
2514 /* Always pull all events out of the kernel. We'll randomly select
2515 an event LWP out of all that have events, to prevent
2516 starvation. */
2517 while (event_child == NULL)
2519 pid_t ret = 0;
2521 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2522 quirks:
2524 - If the thread group leader exits while other threads in the
2525 thread group still exist, waitpid(TGID, ...) hangs. That
2526 waitpid won't return an exit status until the other threads
2527 in the group are reaped.
2529 - When a non-leader thread execs, that thread just vanishes
2530 without reporting an exit (so we'd hang if we waited for it
2531 explicitly in that case). The exec event is reported to
2532 the TGID pid. */
2533 errno = 0;
2534 ret = my_waitpid (-1, wstatp, options | WNOHANG);
2536 threads_debug_printf ("waitpid(-1, ...) returned %d, %s",
2537 ret, errno ? safe_strerror (errno) : "ERRNO-OK");
2539 if (ret > 0)
2541 threads_debug_printf ("waitpid %ld received %s",
2542 (long) ret, status_to_str (*wstatp).c_str ());
2544 /* Filter all events. IOW, leave all events pending. We'll
2545 randomly select an event LWP out of all that have events
2546 below. */
2547 filter_event (ret, *wstatp);
2548 /* Retry until nothing comes out of waitpid. A single
2549 SIGCHLD can indicate more than one child stopped. */
2550 continue;
2553 /* Now that we've pulled all events out of the kernel, resume
2554 LWPs that don't have an interesting event to report. */
2555 if (stopping_threads == NOT_STOPPING_THREADS)
2556 for_each_thread ([this] (thread_info *thread)
2558 resume_stopped_resumed_lwps (thread);
2561 /* ... and find an LWP with a status to report to the core, if
2562 any. */
2563 event_thread = find_thread_in_random ([&] (thread_info *thread)
2565 return status_pending_p_callback (thread, filter_ptid);
2568 if (event_thread != NULL)
2570 event_child = get_thread_lwp (event_thread);
2571 *wstatp = event_child->status_pending;
2572 event_child->status_pending_p = 0;
2573 event_child->status_pending = 0;
2574 break;
2577 /* Check for zombie thread group leaders. Those can't be reaped
2578 until all other threads in the thread group are. */
2579 check_zombie_leaders ();
2581 auto not_stopped = [&] (thread_info *thread)
2583 return not_stopped_callback (thread, wait_ptid);
2586 /* If there are no resumed children left in the set of LWPs we
2587 want to wait for, bail. We can't just block in
2588 waitpid/sigsuspend, because lwps might have been left stopped
2589 in trace-stop state, and we'd be stuck forever waiting for
2590 their status to change (which would only happen if we resumed
2591 them). Even if WNOHANG is set, this return code is preferred
2592 over 0 (below), as it is more detailed. */
2593 if (find_thread (not_stopped) == NULL)
2595 threads_debug_printf ("exit (no unwaited-for LWP)");
2597 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2598 return -1;
2601 /* No interesting event to report to the caller. */
2602 if ((options & WNOHANG))
2604 threads_debug_printf ("WNOHANG set, no event found");
2606 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2607 return 0;
2610 /* Block until we get an event reported with SIGCHLD. */
2611 threads_debug_printf ("sigsuspend'ing");
2613 sigsuspend (&prev_mask);
2614 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2615 goto retry;
2618 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
2620 switch_to_thread (event_thread);
2622 return lwpid_of (event_thread);
2626 linux_process_target::wait_for_event (ptid_t ptid, int *wstatp, int options)
2628 return wait_for_event_filtered (ptid, ptid, wstatp, options);
2631 /* Select one LWP out of those that have events pending. */
2633 static void
2634 select_event_lwp (struct lwp_info **orig_lp)
2636 struct thread_info *event_thread = NULL;
2638 /* In all-stop, give preference to the LWP that is being
2639 single-stepped. There will be at most one, and it's the LWP that
2640 the core is most interested in. If we didn't do this, then we'd
2641 have to handle pending step SIGTRAPs somehow in case the core
2642 later continues the previously-stepped thread, otherwise we'd
2643 report the pending SIGTRAP, and the core, not having stepped the
2644 thread, wouldn't understand what the trap was for, and therefore
2645 would report it to the user as a random signal. */
2646 if (!non_stop)
2648 event_thread = find_thread ([] (thread_info *thread)
2650 lwp_info *lp = get_thread_lwp (thread);
2652 return (thread->last_status.kind () == TARGET_WAITKIND_IGNORE
2653 && thread->last_resume_kind == resume_step
2654 && lp->status_pending_p);
2657 if (event_thread != NULL)
2658 threads_debug_printf
2659 ("Select single-step %s",
2660 target_pid_to_str (ptid_of (event_thread)).c_str ());
2662 if (event_thread == NULL)
2664 /* No single-stepping LWP. Select one at random, out of those
2665 which have had events. */
2667 event_thread = find_thread_in_random ([&] (thread_info *thread)
2669 lwp_info *lp = get_thread_lwp (thread);
2671 /* Only resumed LWPs that have an event pending. */
2672 return (thread->last_status.kind () == TARGET_WAITKIND_IGNORE
2673 && lp->status_pending_p);
2677 if (event_thread != NULL)
2679 struct lwp_info *event_lp = get_thread_lwp (event_thread);
2681 /* Switch the event LWP. */
2682 *orig_lp = event_lp;
2686 /* Decrement the suspend count of all LWPs, except EXCEPT, if non
2687 NULL. */
2689 static void
2690 unsuspend_all_lwps (struct lwp_info *except)
2692 for_each_thread ([&] (thread_info *thread)
2694 lwp_info *lwp = get_thread_lwp (thread);
2696 if (lwp != except)
2697 lwp_suspended_decr (lwp);
2701 static bool lwp_running (thread_info *thread);
2703 /* Stabilize threads (move out of jump pads).
2705 If a thread is midway collecting a fast tracepoint, we need to
2706 finish the collection and move it out of the jump pad before
2707 reporting the signal.
2709 This avoids recursion while collecting (when a signal arrives
2710 midway, and the signal handler itself collects), which would trash
2711 the trace buffer. In case the user set a breakpoint in a signal
2712 handler, this avoids the backtrace showing the jump pad, etc..
2713 Most importantly, there are certain things we can't do safely if
2714 threads are stopped in a jump pad (or in its callee's). For
2715 example:
2717 - starting a new trace run. A thread still collecting the
2718 previous run, could trash the trace buffer when resumed. The trace
2719 buffer control structures would have been reset but the thread had
2720 no way to tell. The thread could even midway memcpy'ing to the
2721 buffer, which would mean that when resumed, it would clobber the
2722 trace buffer that had been set for a new run.
2724 - we can't rewrite/reuse the jump pads for new tracepoints
2725 safely. Say you do tstart while a thread is stopped midway while
2726 collecting. When the thread is later resumed, it finishes the
2727 collection, and returns to the jump pad, to execute the original
2728 instruction that was under the tracepoint jump at the time the
2729 older run had been started. If the jump pad had been rewritten
2730 since for something else in the new run, the thread would now
2731 execute the wrong / random instructions. */
2733 void
2734 linux_process_target::stabilize_threads ()
2736 thread_info *thread_stuck = find_thread ([this] (thread_info *thread)
2738 return stuck_in_jump_pad (thread);
2741 if (thread_stuck != NULL)
2743 threads_debug_printf ("can't stabilize, LWP %ld is stuck in jump pad",
2744 lwpid_of (thread_stuck));
2745 return;
2748 scoped_restore_current_thread restore_thread;
2750 stabilizing_threads = 1;
2752 /* Kick 'em all. */
2753 for_each_thread ([this] (thread_info *thread)
2755 move_out_of_jump_pad (thread);
2758 /* Loop until all are stopped out of the jump pads. */
2759 while (find_thread (lwp_running) != NULL)
2761 struct target_waitstatus ourstatus;
2762 struct lwp_info *lwp;
2763 int wstat;
2765 /* Note that we go through the full wait even loop. While
2766 moving threads out of jump pad, we need to be able to step
2767 over internal breakpoints and such. */
2768 wait_1 (minus_one_ptid, &ourstatus, 0);
2770 if (ourstatus.kind () == TARGET_WAITKIND_STOPPED)
2772 lwp = get_thread_lwp (current_thread);
2774 /* Lock it. */
2775 lwp_suspended_inc (lwp);
2777 if (ourstatus.sig () != GDB_SIGNAL_0
2778 || current_thread->last_resume_kind == resume_stop)
2780 wstat = W_STOPCODE (gdb_signal_to_host (ourstatus.sig ()));
2781 enqueue_one_deferred_signal (lwp, &wstat);
2786 unsuspend_all_lwps (NULL);
2788 stabilizing_threads = 0;
2790 if (debug_threads)
2792 thread_stuck = find_thread ([this] (thread_info *thread)
2794 return stuck_in_jump_pad (thread);
2797 if (thread_stuck != NULL)
2798 threads_debug_printf
2799 ("couldn't stabilize, LWP %ld got stuck in jump pad",
2800 lwpid_of (thread_stuck));
2804 /* Convenience function that is called when the kernel reports an
2805 event that is not passed out to GDB. */
2807 static ptid_t
2808 ignore_event (struct target_waitstatus *ourstatus)
2810 /* If we got an event, there may still be others, as a single
2811 SIGCHLD can indicate more than one child stopped. This forces
2812 another target_wait call. */
2813 async_file_mark ();
2815 ourstatus->set_ignore ();
2816 return null_ptid;
2819 ptid_t
2820 linux_process_target::filter_exit_event (lwp_info *event_child,
2821 target_waitstatus *ourstatus)
2823 client_state &cs = get_client_state ();
2824 struct thread_info *thread = get_lwp_thread (event_child);
2825 ptid_t ptid = ptid_of (thread);
2827 if (!is_leader (thread))
2829 if (cs.report_thread_events)
2830 ourstatus->set_thread_exited (0);
2831 else
2832 ourstatus->set_ignore ();
2834 delete_lwp (event_child);
2836 return ptid;
2839 /* Returns 1 if GDB is interested in any event_child syscalls. */
2841 static int
2842 gdb_catching_syscalls_p (struct lwp_info *event_child)
2844 struct thread_info *thread = get_lwp_thread (event_child);
2845 struct process_info *proc = get_thread_process (thread);
2847 return !proc->syscalls_to_catch.empty ();
2850 bool
2851 linux_process_target::gdb_catch_this_syscall (lwp_info *event_child)
2853 int sysno;
2854 struct thread_info *thread = get_lwp_thread (event_child);
2855 struct process_info *proc = get_thread_process (thread);
2857 if (proc->syscalls_to_catch.empty ())
2858 return false;
2860 if (proc->syscalls_to_catch[0] == ANY_SYSCALL)
2861 return true;
2863 get_syscall_trapinfo (event_child, &sysno);
2865 for (int iter : proc->syscalls_to_catch)
2866 if (iter == sysno)
2867 return true;
2869 return false;
2872 ptid_t
2873 linux_process_target::wait_1 (ptid_t ptid, target_waitstatus *ourstatus,
2874 target_wait_flags target_options)
2876 THREADS_SCOPED_DEBUG_ENTER_EXIT;
2878 client_state &cs = get_client_state ();
2879 int w;
2880 struct lwp_info *event_child;
2881 int options;
2882 int pid;
2883 int step_over_finished;
2884 int bp_explains_trap;
2885 int maybe_internal_trap;
2886 int report_to_gdb;
2887 int trace_event;
2888 int in_step_range;
2889 int any_resumed;
2891 threads_debug_printf ("[%s]", target_pid_to_str (ptid).c_str ());
2893 /* Translate generic target options into linux options. */
2894 options = __WALL;
2895 if (target_options & TARGET_WNOHANG)
2896 options |= WNOHANG;
2898 bp_explains_trap = 0;
2899 trace_event = 0;
2900 in_step_range = 0;
2901 ourstatus->set_ignore ();
2903 auto status_pending_p_any = [&] (thread_info *thread)
2905 return status_pending_p_callback (thread, minus_one_ptid);
2908 auto not_stopped = [&] (thread_info *thread)
2910 return not_stopped_callback (thread, minus_one_ptid);
2913 /* Find a resumed LWP, if any. */
2914 if (find_thread (status_pending_p_any) != NULL)
2915 any_resumed = 1;
2916 else if (find_thread (not_stopped) != NULL)
2917 any_resumed = 1;
2918 else
2919 any_resumed = 0;
2921 if (step_over_bkpt == null_ptid)
2922 pid = wait_for_event (ptid, &w, options);
2923 else
2925 threads_debug_printf ("step_over_bkpt set [%s], doing a blocking wait",
2926 target_pid_to_str (step_over_bkpt).c_str ());
2927 pid = wait_for_event (step_over_bkpt, &w, options & ~WNOHANG);
2930 if (pid == 0 || (pid == -1 && !any_resumed))
2932 gdb_assert (target_options & TARGET_WNOHANG);
2934 threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_IGNORE");
2936 ourstatus->set_ignore ();
2937 return null_ptid;
2939 else if (pid == -1)
2941 threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_NO_RESUMED");
2943 ourstatus->set_no_resumed ();
2944 return null_ptid;
2947 event_child = get_thread_lwp (current_thread);
2949 /* wait_for_event only returns an exit status for the last
2950 child of a process. Report it. */
2951 if (WIFEXITED (w) || WIFSIGNALED (w))
2953 if (WIFEXITED (w))
2955 ourstatus->set_exited (WEXITSTATUS (w));
2957 threads_debug_printf
2958 ("ret = %s, exited with retcode %d",
2959 target_pid_to_str (ptid_of (current_thread)).c_str (),
2960 WEXITSTATUS (w));
2962 else
2964 ourstatus->set_signalled (gdb_signal_from_host (WTERMSIG (w)));
2966 threads_debug_printf
2967 ("ret = %s, terminated with signal %d",
2968 target_pid_to_str (ptid_of (current_thread)).c_str (),
2969 WTERMSIG (w));
2972 if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
2973 return filter_exit_event (event_child, ourstatus);
2975 return ptid_of (current_thread);
2978 /* If step-over executes a breakpoint instruction, in the case of a
2979 hardware single step it means a gdb/gdbserver breakpoint had been
2980 planted on top of a permanent breakpoint, in the case of a software
2981 single step it may just mean that gdbserver hit the reinsert breakpoint.
2982 The PC has been adjusted by save_stop_reason to point at
2983 the breakpoint address.
2984 So in the case of the hardware single step advance the PC manually
2985 past the breakpoint and in the case of software single step advance only
2986 if it's not the single_step_breakpoint we are hitting.
2987 This avoids that a program would keep trapping a permanent breakpoint
2988 forever. */
2989 if (step_over_bkpt != null_ptid
2990 && event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
2991 && (event_child->stepping
2992 || !single_step_breakpoint_inserted_here (event_child->stop_pc)))
2994 int increment_pc = 0;
2995 int breakpoint_kind = 0;
2996 CORE_ADDR stop_pc = event_child->stop_pc;
2998 breakpoint_kind = breakpoint_kind_from_current_state (&stop_pc);
2999 sw_breakpoint_from_kind (breakpoint_kind, &increment_pc);
3001 threads_debug_printf
3002 ("step-over for %s executed software breakpoint",
3003 target_pid_to_str (ptid_of (current_thread)).c_str ());
3005 if (increment_pc != 0)
3007 struct regcache *regcache
3008 = get_thread_regcache (current_thread, 1);
3010 event_child->stop_pc += increment_pc;
3011 low_set_pc (regcache, event_child->stop_pc);
3013 if (!low_breakpoint_at (event_child->stop_pc))
3014 event_child->stop_reason = TARGET_STOPPED_BY_NO_REASON;
3018 /* If this event was not handled before, and is not a SIGTRAP, we
3019 report it. SIGILL and SIGSEGV are also treated as traps in case
3020 a breakpoint is inserted at the current PC. If this target does
3021 not support internal breakpoints at all, we also report the
3022 SIGTRAP without further processing; it's of no concern to us. */
3023 maybe_internal_trap
3024 = (low_supports_breakpoints ()
3025 && (WSTOPSIG (w) == SIGTRAP
3026 || ((WSTOPSIG (w) == SIGILL
3027 || WSTOPSIG (w) == SIGSEGV)
3028 && low_breakpoint_at (event_child->stop_pc))));
3030 if (maybe_internal_trap)
3032 /* Handle anything that requires bookkeeping before deciding to
3033 report the event or continue waiting. */
3035 /* First check if we can explain the SIGTRAP with an internal
3036 breakpoint, or if we should possibly report the event to GDB.
3037 Do this before anything that may remove or insert a
3038 breakpoint. */
3039 bp_explains_trap = breakpoint_inserted_here (event_child->stop_pc);
3041 /* We have a SIGTRAP, possibly a step-over dance has just
3042 finished. If so, tweak the state machine accordingly,
3043 reinsert breakpoints and delete any single-step
3044 breakpoints. */
3045 step_over_finished = finish_step_over (event_child);
3047 /* Now invoke the callbacks of any internal breakpoints there. */
3048 check_breakpoints (event_child->stop_pc);
3050 /* Handle tracepoint data collecting. This may overflow the
3051 trace buffer, and cause a tracing stop, removing
3052 breakpoints. */
3053 trace_event = handle_tracepoints (event_child);
3055 if (bp_explains_trap)
3056 threads_debug_printf ("Hit a gdbserver breakpoint.");
3058 else
3060 /* We have some other signal, possibly a step-over dance was in
3061 progress, and it should be cancelled too. */
3062 step_over_finished = finish_step_over (event_child);
3065 /* We have all the data we need. Either report the event to GDB, or
3066 resume threads and keep waiting for more. */
3068 /* If we're collecting a fast tracepoint, finish the collection and
3069 move out of the jump pad before delivering a signal. See
3070 linux_stabilize_threads. */
3072 if (WIFSTOPPED (w)
3073 && WSTOPSIG (w) != SIGTRAP
3074 && supports_fast_tracepoints ()
3075 && agent_loaded_p ())
3077 threads_debug_printf ("Got signal %d for LWP %ld. Check if we need "
3078 "to defer or adjust it.",
3079 WSTOPSIG (w), lwpid_of (current_thread));
3081 /* Allow debugging the jump pad itself. */
3082 if (current_thread->last_resume_kind != resume_step
3083 && maybe_move_out_of_jump_pad (event_child, &w))
3085 enqueue_one_deferred_signal (event_child, &w);
3087 threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad)",
3088 WSTOPSIG (w), lwpid_of (current_thread));
3090 resume_one_lwp (event_child, 0, 0, NULL);
3092 return ignore_event (ourstatus);
3096 if (event_child->collecting_fast_tracepoint
3097 != fast_tpoint_collect_result::not_collecting)
3099 threads_debug_printf
3100 ("LWP %ld was trying to move out of the jump pad (%d). "
3101 "Check if we're already there.",
3102 lwpid_of (current_thread),
3103 (int) event_child->collecting_fast_tracepoint);
3105 trace_event = 1;
3107 event_child->collecting_fast_tracepoint
3108 = linux_fast_tracepoint_collecting (event_child, NULL);
3110 if (event_child->collecting_fast_tracepoint
3111 != fast_tpoint_collect_result::before_insn)
3113 /* No longer need this breakpoint. */
3114 if (event_child->exit_jump_pad_bkpt != NULL)
3116 threads_debug_printf
3117 ("No longer need exit-jump-pad bkpt; removing it."
3118 "stopping all threads momentarily.");
3120 /* Other running threads could hit this breakpoint.
3121 We don't handle moribund locations like GDB does,
3122 instead we always pause all threads when removing
3123 breakpoints, so that any step-over or
3124 decr_pc_after_break adjustment is always taken
3125 care of while the breakpoint is still
3126 inserted. */
3127 stop_all_lwps (1, event_child);
3129 delete_breakpoint (event_child->exit_jump_pad_bkpt);
3130 event_child->exit_jump_pad_bkpt = NULL;
3132 unstop_all_lwps (1, event_child);
3134 gdb_assert (event_child->suspended >= 0);
3138 if (event_child->collecting_fast_tracepoint
3139 == fast_tpoint_collect_result::not_collecting)
3141 threads_debug_printf
3142 ("fast tracepoint finished collecting successfully.");
3144 /* We may have a deferred signal to report. */
3145 if (dequeue_one_deferred_signal (event_child, &w))
3146 threads_debug_printf ("dequeued one signal.");
3147 else
3149 threads_debug_printf ("no deferred signals.");
3151 if (stabilizing_threads)
3153 ourstatus->set_stopped (GDB_SIGNAL_0);
3155 threads_debug_printf
3156 ("ret = %s, stopped while stabilizing threads",
3157 target_pid_to_str (ptid_of (current_thread)).c_str ());
3159 return ptid_of (current_thread);
3165 /* Check whether GDB would be interested in this event. */
3167 /* Check if GDB is interested in this syscall. */
3168 if (WIFSTOPPED (w)
3169 && WSTOPSIG (w) == SYSCALL_SIGTRAP
3170 && !gdb_catch_this_syscall (event_child))
3172 threads_debug_printf ("Ignored syscall for LWP %ld.",
3173 lwpid_of (current_thread));
3175 resume_one_lwp (event_child, event_child->stepping, 0, NULL);
3177 return ignore_event (ourstatus);
3180 /* If GDB is not interested in this signal, don't stop other
3181 threads, and don't report it to GDB. Just resume the inferior
3182 right away. We do this for threading-related signals as well as
3183 any that GDB specifically requested we ignore. But never ignore
3184 SIGSTOP if we sent it ourselves, and do not ignore signals when
3185 stepping - they may require special handling to skip the signal
3186 handler. Also never ignore signals that could be caused by a
3187 breakpoint. */
3188 if (WIFSTOPPED (w)
3189 && current_thread->last_resume_kind != resume_step
3190 && (
3191 #if defined (USE_THREAD_DB) && !defined (__ANDROID__)
3192 (current_process ()->priv->thread_db != NULL
3193 && (WSTOPSIG (w) == __SIGRTMIN
3194 || WSTOPSIG (w) == __SIGRTMIN + 1))
3196 #endif
3197 (cs.pass_signals[gdb_signal_from_host (WSTOPSIG (w))]
3198 && !(WSTOPSIG (w) == SIGSTOP
3199 && current_thread->last_resume_kind == resume_stop)
3200 && !linux_wstatus_maybe_breakpoint (w))))
3202 siginfo_t info, *info_p;
3204 threads_debug_printf ("Ignored signal %d for LWP %ld.",
3205 WSTOPSIG (w), lwpid_of (current_thread));
3207 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
3208 (PTRACE_TYPE_ARG3) 0, &info) == 0)
3209 info_p = &info;
3210 else
3211 info_p = NULL;
3213 if (step_over_finished)
3215 /* We cancelled this thread's step-over above. We still
3216 need to unsuspend all other LWPs, and set them back
3217 running again while the signal handler runs. */
3218 unsuspend_all_lwps (event_child);
3220 /* Enqueue the pending signal info so that proceed_all_lwps
3221 doesn't lose it. */
3222 enqueue_pending_signal (event_child, WSTOPSIG (w), info_p);
3224 proceed_all_lwps ();
3226 else
3228 resume_one_lwp (event_child, event_child->stepping,
3229 WSTOPSIG (w), info_p);
3232 return ignore_event (ourstatus);
3235 /* Note that all addresses are always "out of the step range" when
3236 there's no range to begin with. */
3237 in_step_range = lwp_in_step_range (event_child);
3239 /* If GDB wanted this thread to single step, and the thread is out
3240 of the step range, we always want to report the SIGTRAP, and let
3241 GDB handle it. Watchpoints should always be reported. So should
3242 signals we can't explain. A SIGTRAP we can't explain could be a
3243 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3244 do, we're be able to handle GDB breakpoints on top of internal
3245 breakpoints, by handling the internal breakpoint and still
3246 reporting the event to GDB. If we don't, we're out of luck, GDB
3247 won't see the breakpoint hit. If we see a single-step event but
3248 the thread should be continuing, don't pass the trap to gdb.
3249 That indicates that we had previously finished a single-step but
3250 left the single-step pending -- see
3251 complete_ongoing_step_over. */
3252 report_to_gdb = (!maybe_internal_trap
3253 || (current_thread->last_resume_kind == resume_step
3254 && !in_step_range)
3255 || event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
3256 || (!in_step_range
3257 && !bp_explains_trap
3258 && !trace_event
3259 && !step_over_finished
3260 && !(current_thread->last_resume_kind == resume_continue
3261 && event_child->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP))
3262 || (gdb_breakpoint_here (event_child->stop_pc)
3263 && gdb_condition_true_at_breakpoint (event_child->stop_pc)
3264 && gdb_no_commands_at_breakpoint (event_child->stop_pc))
3265 || event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE);
3267 run_breakpoint_commands (event_child->stop_pc);
3269 /* We found no reason GDB would want us to stop. We either hit one
3270 of our own breakpoints, or finished an internal step GDB
3271 shouldn't know about. */
3272 if (!report_to_gdb)
3274 if (bp_explains_trap)
3275 threads_debug_printf ("Hit a gdbserver breakpoint.");
3277 if (step_over_finished)
3278 threads_debug_printf ("Step-over finished.");
3280 if (trace_event)
3281 threads_debug_printf ("Tracepoint event.");
3283 if (lwp_in_step_range (event_child))
3284 threads_debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).",
3285 paddress (event_child->stop_pc),
3286 paddress (event_child->step_range_start),
3287 paddress (event_child->step_range_end));
3289 /* We're not reporting this breakpoint to GDB, so apply the
3290 decr_pc_after_break adjustment to the inferior's regcache
3291 ourselves. */
3293 if (low_supports_breakpoints ())
3295 struct regcache *regcache
3296 = get_thread_regcache (current_thread, 1);
3297 low_set_pc (regcache, event_child->stop_pc);
3300 if (step_over_finished)
3302 /* If we have finished stepping over a breakpoint, we've
3303 stopped and suspended all LWPs momentarily except the
3304 stepping one. This is where we resume them all again.
3305 We're going to keep waiting, so use proceed, which
3306 handles stepping over the next breakpoint. */
3307 unsuspend_all_lwps (event_child);
3309 else
3311 /* Remove the single-step breakpoints if any. Note that
3312 there isn't single-step breakpoint if we finished stepping
3313 over. */
3314 if (supports_software_single_step ()
3315 && has_single_step_breakpoints (current_thread))
3317 stop_all_lwps (0, event_child);
3318 delete_single_step_breakpoints (current_thread);
3319 unstop_all_lwps (0, event_child);
3323 threads_debug_printf ("proceeding all threads.");
3325 proceed_all_lwps ();
3327 return ignore_event (ourstatus);
3330 if (debug_threads)
3332 if (event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
3333 threads_debug_printf ("LWP %ld: extended event with waitstatus %s",
3334 lwpid_of (get_lwp_thread (event_child)),
3335 event_child->waitstatus.to_string ().c_str ());
3337 if (current_thread->last_resume_kind == resume_step)
3339 if (event_child->step_range_start == event_child->step_range_end)
3340 threads_debug_printf
3341 ("GDB wanted to single-step, reporting event.");
3342 else if (!lwp_in_step_range (event_child))
3343 threads_debug_printf ("Out of step range, reporting event.");
3346 if (event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
3347 threads_debug_printf ("Stopped by watchpoint.");
3348 else if (gdb_breakpoint_here (event_child->stop_pc))
3349 threads_debug_printf ("Stopped by GDB breakpoint.");
3352 threads_debug_printf ("Hit a non-gdbserver trap event.");
3354 /* Alright, we're going to report a stop. */
3356 /* Remove single-step breakpoints. */
3357 if (supports_software_single_step ())
3359 /* Remove single-step breakpoints or not. It it is true, stop all
3360 lwps, so that other threads won't hit the breakpoint in the
3361 staled memory. */
3362 int remove_single_step_breakpoints_p = 0;
3364 if (non_stop)
3366 remove_single_step_breakpoints_p
3367 = has_single_step_breakpoints (current_thread);
3369 else
3371 /* In all-stop, a stop reply cancels all previous resume
3372 requests. Delete all single-step breakpoints. */
3374 find_thread ([&] (thread_info *thread) {
3375 if (has_single_step_breakpoints (thread))
3377 remove_single_step_breakpoints_p = 1;
3378 return true;
3381 return false;
3385 if (remove_single_step_breakpoints_p)
3387 /* If we remove single-step breakpoints from memory, stop all lwps,
3388 so that other threads won't hit the breakpoint in the staled
3389 memory. */
3390 stop_all_lwps (0, event_child);
3392 if (non_stop)
3394 gdb_assert (has_single_step_breakpoints (current_thread));
3395 delete_single_step_breakpoints (current_thread);
3397 else
3399 for_each_thread ([] (thread_info *thread){
3400 if (has_single_step_breakpoints (thread))
3401 delete_single_step_breakpoints (thread);
3405 unstop_all_lwps (0, event_child);
3409 if (!stabilizing_threads)
3411 /* In all-stop, stop all threads. */
3412 if (!non_stop)
3413 stop_all_lwps (0, NULL);
3415 if (step_over_finished)
3417 if (!non_stop)
3419 /* If we were doing a step-over, all other threads but
3420 the stepping one had been paused in start_step_over,
3421 with their suspend counts incremented. We don't want
3422 to do a full unstop/unpause, because we're in
3423 all-stop mode (so we want threads stopped), but we
3424 still need to unsuspend the other threads, to
3425 decrement their `suspended' count back. */
3426 unsuspend_all_lwps (event_child);
3428 else
3430 /* If we just finished a step-over, then all threads had
3431 been momentarily paused. In all-stop, that's fine,
3432 we want threads stopped by now anyway. In non-stop,
3433 we need to re-resume threads that GDB wanted to be
3434 running. */
3435 unstop_all_lwps (1, event_child);
3439 /* If we're not waiting for a specific LWP, choose an event LWP
3440 from among those that have had events. Giving equal priority
3441 to all LWPs that have had events helps prevent
3442 starvation. */
3443 if (ptid == minus_one_ptid)
3445 event_child->status_pending_p = 1;
3446 event_child->status_pending = w;
3448 select_event_lwp (&event_child);
3450 /* current_thread and event_child must stay in sync. */
3451 switch_to_thread (get_lwp_thread (event_child));
3453 event_child->status_pending_p = 0;
3454 w = event_child->status_pending;
3458 /* Stabilize threads (move out of jump pads). */
3459 if (!non_stop)
3460 target_stabilize_threads ();
3462 else
3464 /* If we just finished a step-over, then all threads had been
3465 momentarily paused. In all-stop, that's fine, we want
3466 threads stopped by now anyway. In non-stop, we need to
3467 re-resume threads that GDB wanted to be running. */
3468 if (step_over_finished)
3469 unstop_all_lwps (1, event_child);
3472 if (event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
3474 /* If the reported event is an exit, fork, vfork or exec, let
3475 GDB know. */
3477 /* Break the unreported fork relationship chain. */
3478 if (event_child->waitstatus.kind () == TARGET_WAITKIND_FORKED
3479 || event_child->waitstatus.kind () == TARGET_WAITKIND_VFORKED)
3481 event_child->fork_relative->fork_relative = NULL;
3482 event_child->fork_relative = NULL;
3485 *ourstatus = event_child->waitstatus;
3486 /* Clear the event lwp's waitstatus since we handled it already. */
3487 event_child->waitstatus.set_ignore ();
3489 else
3491 /* The actual stop signal is overwritten below. */
3492 ourstatus->set_stopped (GDB_SIGNAL_0);
3495 /* Now that we've selected our final event LWP, un-adjust its PC if
3496 it was a software breakpoint, and the client doesn't know we can
3497 adjust the breakpoint ourselves. */
3498 if (event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
3499 && !cs.swbreak_feature)
3501 int decr_pc = low_decr_pc_after_break ();
3503 if (decr_pc != 0)
3505 struct regcache *regcache
3506 = get_thread_regcache (current_thread, 1);
3507 low_set_pc (regcache, event_child->stop_pc + decr_pc);
3511 if (WSTOPSIG (w) == SYSCALL_SIGTRAP)
3513 int syscall_number;
3515 get_syscall_trapinfo (event_child, &syscall_number);
3516 if (event_child->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY)
3517 ourstatus->set_syscall_entry (syscall_number);
3518 else if (event_child->syscall_state == TARGET_WAITKIND_SYSCALL_RETURN)
3519 ourstatus->set_syscall_return (syscall_number);
3520 else
3521 gdb_assert_not_reached ("unexpected syscall state");
3523 else if (current_thread->last_resume_kind == resume_stop
3524 && WSTOPSIG (w) == SIGSTOP)
3526 /* A thread that has been requested to stop by GDB with vCont;t,
3527 and it stopped cleanly, so report as SIG0. The use of
3528 SIGSTOP is an implementation detail. */
3529 ourstatus->set_stopped (GDB_SIGNAL_0);
3531 else if (current_thread->last_resume_kind == resume_stop
3532 && WSTOPSIG (w) != SIGSTOP)
3534 /* A thread that has been requested to stop by GDB with vCont;t,
3535 but, it stopped for other reasons. */
3536 ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w)));
3538 else if (ourstatus->kind () == TARGET_WAITKIND_STOPPED)
3539 ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w)));
3541 gdb_assert (step_over_bkpt == null_ptid);
3543 threads_debug_printf ("ret = %s, %s",
3544 target_pid_to_str (ptid_of (current_thread)).c_str (),
3545 ourstatus->to_string ().c_str ());
3547 if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
3548 return filter_exit_event (event_child, ourstatus);
3550 return ptid_of (current_thread);
3553 /* Get rid of any pending event in the pipe. */
3554 static void
3555 async_file_flush (void)
3557 linux_event_pipe.flush ();
3560 /* Put something in the pipe, so the event loop wakes up. */
3561 static void
3562 async_file_mark (void)
3564 linux_event_pipe.mark ();
3567 ptid_t
3568 linux_process_target::wait (ptid_t ptid,
3569 target_waitstatus *ourstatus,
3570 target_wait_flags target_options)
3572 ptid_t event_ptid;
3574 /* Flush the async file first. */
3575 if (target_is_async_p ())
3576 async_file_flush ();
3580 event_ptid = wait_1 (ptid, ourstatus, target_options);
3582 while ((target_options & TARGET_WNOHANG) == 0
3583 && event_ptid == null_ptid
3584 && ourstatus->kind () == TARGET_WAITKIND_IGNORE);
3586 /* If at least one stop was reported, there may be more. A single
3587 SIGCHLD can signal more than one child stop. */
3588 if (target_is_async_p ()
3589 && (target_options & TARGET_WNOHANG) != 0
3590 && event_ptid != null_ptid)
3591 async_file_mark ();
3593 return event_ptid;
3596 /* Send a signal to an LWP. */
3598 static int
3599 kill_lwp (unsigned long lwpid, int signo)
3601 int ret;
3603 errno = 0;
3604 ret = syscall (__NR_tkill, lwpid, signo);
3605 if (errno == ENOSYS)
3607 /* If tkill fails, then we are not using nptl threads, a
3608 configuration we no longer support. */
3609 perror_with_name (("tkill"));
3611 return ret;
3614 void
3615 linux_stop_lwp (struct lwp_info *lwp)
3617 send_sigstop (lwp);
3620 static void
3621 send_sigstop (struct lwp_info *lwp)
3623 int pid;
3625 pid = lwpid_of (get_lwp_thread (lwp));
3627 /* If we already have a pending stop signal for this process, don't
3628 send another. */
3629 if (lwp->stop_expected)
3631 threads_debug_printf ("Have pending sigstop for lwp %d", pid);
3633 return;
3636 threads_debug_printf ("Sending sigstop to lwp %d", pid);
3638 lwp->stop_expected = 1;
3639 kill_lwp (pid, SIGSTOP);
3642 static void
3643 send_sigstop (thread_info *thread, lwp_info *except)
3645 struct lwp_info *lwp = get_thread_lwp (thread);
3647 /* Ignore EXCEPT. */
3648 if (lwp == except)
3649 return;
3651 if (lwp->stopped)
3652 return;
3654 send_sigstop (lwp);
3657 /* Increment the suspend count of an LWP, and stop it, if not stopped
3658 yet. */
3659 static void
3660 suspend_and_send_sigstop (thread_info *thread, lwp_info *except)
3662 struct lwp_info *lwp = get_thread_lwp (thread);
3664 /* Ignore EXCEPT. */
3665 if (lwp == except)
3666 return;
3668 lwp_suspended_inc (lwp);
3670 send_sigstop (thread, except);
3673 static void
3674 mark_lwp_dead (struct lwp_info *lwp, int wstat)
3676 /* Store the exit status for later. */
3677 lwp->status_pending_p = 1;
3678 lwp->status_pending = wstat;
3680 /* Store in waitstatus as well, as there's nothing else to process
3681 for this event. */
3682 if (WIFEXITED (wstat))
3683 lwp->waitstatus.set_exited (WEXITSTATUS (wstat));
3684 else if (WIFSIGNALED (wstat))
3685 lwp->waitstatus.set_signalled (gdb_signal_from_host (WTERMSIG (wstat)));
3687 /* Prevent trying to stop it. */
3688 lwp->stopped = 1;
3690 /* No further stops are expected from a dead lwp. */
3691 lwp->stop_expected = 0;
3694 /* Return true if LWP has exited already, and has a pending exit event
3695 to report to GDB. */
3697 static int
3698 lwp_is_marked_dead (struct lwp_info *lwp)
3700 return (lwp->status_pending_p
3701 && (WIFEXITED (lwp->status_pending)
3702 || WIFSIGNALED (lwp->status_pending)));
3705 void
3706 linux_process_target::wait_for_sigstop ()
3708 struct thread_info *saved_thread;
3709 ptid_t saved_tid;
3710 int wstat;
3711 int ret;
3713 saved_thread = current_thread;
3714 if (saved_thread != NULL)
3715 saved_tid = saved_thread->id;
3716 else
3717 saved_tid = null_ptid; /* avoid bogus unused warning */
3719 scoped_restore_current_thread restore_thread;
3721 threads_debug_printf ("pulling events");
3723 /* Passing NULL_PTID as filter indicates we want all events to be
3724 left pending. Eventually this returns when there are no
3725 unwaited-for children left. */
3726 ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat, __WALL);
3727 gdb_assert (ret == -1);
3729 if (saved_thread == NULL || mythread_alive (saved_tid))
3730 return;
3731 else
3733 threads_debug_printf ("Previously current thread died.");
3735 /* We can't change the current inferior behind GDB's back,
3736 otherwise, a subsequent command may apply to the wrong
3737 process. */
3738 restore_thread.dont_restore ();
3739 switch_to_thread (nullptr);
3743 bool
3744 linux_process_target::stuck_in_jump_pad (thread_info *thread)
3746 struct lwp_info *lwp = get_thread_lwp (thread);
3748 if (lwp->suspended != 0)
3750 internal_error (__FILE__, __LINE__,
3751 "LWP %ld is suspended, suspended=%d\n",
3752 lwpid_of (thread), lwp->suspended);
3754 gdb_assert (lwp->stopped);
3756 /* Allow debugging the jump pad, gdb_collect, etc.. */
3757 return (supports_fast_tracepoints ()
3758 && agent_loaded_p ()
3759 && (gdb_breakpoint_here (lwp->stop_pc)
3760 || lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
3761 || thread->last_resume_kind == resume_step)
3762 && (linux_fast_tracepoint_collecting (lwp, NULL)
3763 != fast_tpoint_collect_result::not_collecting));
3766 void
3767 linux_process_target::move_out_of_jump_pad (thread_info *thread)
3769 struct lwp_info *lwp = get_thread_lwp (thread);
3770 int *wstat;
3772 if (lwp->suspended != 0)
3774 internal_error (__FILE__, __LINE__,
3775 "LWP %ld is suspended, suspended=%d\n",
3776 lwpid_of (thread), lwp->suspended);
3778 gdb_assert (lwp->stopped);
3780 /* For gdb_breakpoint_here. */
3781 scoped_restore_current_thread restore_thread;
3782 switch_to_thread (thread);
3784 wstat = lwp->status_pending_p ? &lwp->status_pending : NULL;
3786 /* Allow debugging the jump pad, gdb_collect, etc. */
3787 if (!gdb_breakpoint_here (lwp->stop_pc)
3788 && lwp->stop_reason != TARGET_STOPPED_BY_WATCHPOINT
3789 && thread->last_resume_kind != resume_step
3790 && maybe_move_out_of_jump_pad (lwp, wstat))
3792 threads_debug_printf ("LWP %ld needs stabilizing (in jump pad)",
3793 lwpid_of (thread));
3795 if (wstat)
3797 lwp->status_pending_p = 0;
3798 enqueue_one_deferred_signal (lwp, wstat);
3800 threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad",
3801 WSTOPSIG (*wstat), lwpid_of (thread));
3804 resume_one_lwp (lwp, 0, 0, NULL);
3806 else
3807 lwp_suspended_inc (lwp);
3810 static bool
3811 lwp_running (thread_info *thread)
3813 struct lwp_info *lwp = get_thread_lwp (thread);
3815 if (lwp_is_marked_dead (lwp))
3816 return false;
3818 return !lwp->stopped;
3821 void
3822 linux_process_target::stop_all_lwps (int suspend, lwp_info *except)
3824 /* Should not be called recursively. */
3825 gdb_assert (stopping_threads == NOT_STOPPING_THREADS);
3827 THREADS_SCOPED_DEBUG_ENTER_EXIT;
3829 threads_debug_printf
3830 ("%s, except=%s", suspend ? "stop-and-suspend" : "stop",
3831 (except != NULL
3832 ? target_pid_to_str (ptid_of (get_lwp_thread (except))).c_str ()
3833 : "none"));
3835 stopping_threads = (suspend
3836 ? STOPPING_AND_SUSPENDING_THREADS
3837 : STOPPING_THREADS);
3839 if (suspend)
3840 for_each_thread ([&] (thread_info *thread)
3842 suspend_and_send_sigstop (thread, except);
3844 else
3845 for_each_thread ([&] (thread_info *thread)
3847 send_sigstop (thread, except);
3850 wait_for_sigstop ();
3851 stopping_threads = NOT_STOPPING_THREADS;
3853 threads_debug_printf ("setting stopping_threads back to !stopping");
3856 /* Enqueue one signal in the chain of signals which need to be
3857 delivered to this process on next resume. */
3859 static void
3860 enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info)
3862 lwp->pending_signals.emplace_back (signal);
3863 if (info == nullptr)
3864 memset (&lwp->pending_signals.back ().info, 0, sizeof (siginfo_t));
3865 else
3866 lwp->pending_signals.back ().info = *info;
3869 void
3870 linux_process_target::install_software_single_step_breakpoints (lwp_info *lwp)
3872 struct thread_info *thread = get_lwp_thread (lwp);
3873 struct regcache *regcache = get_thread_regcache (thread, 1);
3875 scoped_restore_current_thread restore_thread;
3877 switch_to_thread (thread);
3878 std::vector<CORE_ADDR> next_pcs = low_get_next_pcs (regcache);
3880 for (CORE_ADDR pc : next_pcs)
3881 set_single_step_breakpoint (pc, current_ptid);
3885 linux_process_target::single_step (lwp_info* lwp)
3887 int step = 0;
3889 if (supports_hardware_single_step ())
3891 step = 1;
3893 else if (supports_software_single_step ())
3895 install_software_single_step_breakpoints (lwp);
3896 step = 0;
3898 else
3899 threads_debug_printf ("stepping is not implemented on this target");
3901 return step;
3904 /* The signal can be delivered to the inferior if we are not trying to
3905 finish a fast tracepoint collect. Since signal can be delivered in
3906 the step-over, the program may go to signal handler and trap again
3907 after return from the signal handler. We can live with the spurious
3908 double traps. */
3910 static int
3911 lwp_signal_can_be_delivered (struct lwp_info *lwp)
3913 return (lwp->collecting_fast_tracepoint
3914 == fast_tpoint_collect_result::not_collecting);
3917 void
3918 linux_process_target::resume_one_lwp_throw (lwp_info *lwp, int step,
3919 int signal, siginfo_t *info)
3921 struct thread_info *thread = get_lwp_thread (lwp);
3922 int ptrace_request;
3923 struct process_info *proc = get_thread_process (thread);
3925 /* Note that target description may not be initialised
3926 (proc->tdesc == NULL) at this point because the program hasn't
3927 stopped at the first instruction yet. It means GDBserver skips
3928 the extra traps from the wrapper program (see option --wrapper).
3929 Code in this function that requires register access should be
3930 guarded by proc->tdesc == NULL or something else. */
3932 if (lwp->stopped == 0)
3933 return;
3935 gdb_assert (lwp->waitstatus.kind () == TARGET_WAITKIND_IGNORE);
3937 fast_tpoint_collect_result fast_tp_collecting
3938 = lwp->collecting_fast_tracepoint;
3940 gdb_assert (!stabilizing_threads
3941 || (fast_tp_collecting
3942 != fast_tpoint_collect_result::not_collecting));
3944 /* Cancel actions that rely on GDB not changing the PC (e.g., the
3945 user used the "jump" command, or "set $pc = foo"). */
3946 if (thread->while_stepping != NULL && lwp->stop_pc != get_pc (lwp))
3948 /* Collecting 'while-stepping' actions doesn't make sense
3949 anymore. */
3950 release_while_stepping_state_list (thread);
3953 /* If we have pending signals or status, and a new signal, enqueue the
3954 signal. Also enqueue the signal if it can't be delivered to the
3955 inferior right now. */
3956 if (signal != 0
3957 && (lwp->status_pending_p
3958 || !lwp->pending_signals.empty ()
3959 || !lwp_signal_can_be_delivered (lwp)))
3961 enqueue_pending_signal (lwp, signal, info);
3963 /* Postpone any pending signal. It was enqueued above. */
3964 signal = 0;
3967 if (lwp->status_pending_p)
3969 threads_debug_printf
3970 ("Not resuming lwp %ld (%s, stop %s); has pending status",
3971 lwpid_of (thread), step ? "step" : "continue",
3972 lwp->stop_expected ? "expected" : "not expected");
3973 return;
3976 scoped_restore_current_thread restore_thread;
3977 switch_to_thread (thread);
3979 /* This bit needs some thinking about. If we get a signal that
3980 we must report while a single-step reinsert is still pending,
3981 we often end up resuming the thread. It might be better to
3982 (ew) allow a stack of pending events; then we could be sure that
3983 the reinsert happened right away and not lose any signals.
3985 Making this stack would also shrink the window in which breakpoints are
3986 uninserted (see comment in linux_wait_for_lwp) but not enough for
3987 complete correctness, so it won't solve that problem. It may be
3988 worthwhile just to solve this one, however. */
3989 if (lwp->bp_reinsert != 0)
3991 threads_debug_printf (" pending reinsert at 0x%s",
3992 paddress (lwp->bp_reinsert));
3994 if (supports_hardware_single_step ())
3996 if (fast_tp_collecting == fast_tpoint_collect_result::not_collecting)
3998 if (step == 0)
3999 warning ("BAD - reinserting but not stepping.");
4000 if (lwp->suspended)
4001 warning ("BAD - reinserting and suspended(%d).",
4002 lwp->suspended);
4006 step = maybe_hw_step (thread);
4009 if (fast_tp_collecting == fast_tpoint_collect_result::before_insn)
4010 threads_debug_printf
4011 ("lwp %ld wants to get out of fast tracepoint jump pad "
4012 "(exit-jump-pad-bkpt)", lwpid_of (thread));
4014 else if (fast_tp_collecting == fast_tpoint_collect_result::at_insn)
4016 threads_debug_printf
4017 ("lwp %ld wants to get out of fast tracepoint jump pad single-stepping",
4018 lwpid_of (thread));
4020 if (supports_hardware_single_step ())
4021 step = 1;
4022 else
4024 internal_error (__FILE__, __LINE__,
4025 "moving out of jump pad single-stepping"
4026 " not implemented on this target");
4030 /* If we have while-stepping actions in this thread set it stepping.
4031 If we have a signal to deliver, it may or may not be set to
4032 SIG_IGN, we don't know. Assume so, and allow collecting
4033 while-stepping into a signal handler. A possible smart thing to
4034 do would be to set an internal breakpoint at the signal return
4035 address, continue, and carry on catching this while-stepping
4036 action only when that breakpoint is hit. A future
4037 enhancement. */
4038 if (thread->while_stepping != NULL)
4040 threads_debug_printf
4041 ("lwp %ld has a while-stepping action -> forcing step.",
4042 lwpid_of (thread));
4044 step = single_step (lwp);
4047 if (proc->tdesc != NULL && low_supports_breakpoints ())
4049 struct regcache *regcache = get_thread_regcache (current_thread, 1);
4051 lwp->stop_pc = low_get_pc (regcache);
4053 threads_debug_printf (" %s from pc 0x%lx", step ? "step" : "continue",
4054 (long) lwp->stop_pc);
4057 /* If we have pending signals, consume one if it can be delivered to
4058 the inferior. */
4059 if (!lwp->pending_signals.empty () && lwp_signal_can_be_delivered (lwp))
4061 const pending_signal &p_sig = lwp->pending_signals.front ();
4063 signal = p_sig.signal;
4064 if (p_sig.info.si_signo != 0)
4065 ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
4066 &p_sig.info);
4068 lwp->pending_signals.pop_front ();
4071 threads_debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)",
4072 lwpid_of (thread), step ? "step" : "continue", signal,
4073 lwp->stop_expected ? "expected" : "not expected");
4075 low_prepare_to_resume (lwp);
4077 regcache_invalidate_thread (thread);
4078 errno = 0;
4079 lwp->stepping = step;
4080 if (step)
4081 ptrace_request = PTRACE_SINGLESTEP;
4082 else if (gdb_catching_syscalls_p (lwp))
4083 ptrace_request = PTRACE_SYSCALL;
4084 else
4085 ptrace_request = PTRACE_CONT;
4086 ptrace (ptrace_request,
4087 lwpid_of (thread),
4088 (PTRACE_TYPE_ARG3) 0,
4089 /* Coerce to a uintptr_t first to avoid potential gcc warning
4090 of coercing an 8 byte integer to a 4 byte pointer. */
4091 (PTRACE_TYPE_ARG4) (uintptr_t) signal);
4093 if (errno)
4095 int saved_errno = errno;
4097 threads_debug_printf ("ptrace errno = %d (%s)",
4098 saved_errno, strerror (saved_errno));
4100 errno = saved_errno;
4101 perror_with_name ("resuming thread");
4104 /* Successfully resumed. Clear state that no longer makes sense,
4105 and mark the LWP as running. Must not do this before resuming
4106 otherwise if that fails other code will be confused. E.g., we'd
4107 later try to stop the LWP and hang forever waiting for a stop
4108 status. Note that we must not throw after this is cleared,
4109 otherwise handle_zombie_lwp_error would get confused. */
4110 lwp->stopped = 0;
4111 lwp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4114 void
4115 linux_process_target::low_prepare_to_resume (lwp_info *lwp)
4117 /* Nop. */
4120 /* Called when we try to resume a stopped LWP and that errors out. If
4121 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4122 or about to become), discard the error, clear any pending status
4123 the LWP may have, and return true (we'll collect the exit status
4124 soon enough). Otherwise, return false. */
4126 static int
4127 check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
4129 struct thread_info *thread = get_lwp_thread (lp);
4131 /* If we get an error after resuming the LWP successfully, we'd
4132 confuse !T state for the LWP being gone. */
4133 gdb_assert (lp->stopped);
4135 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4136 because even if ptrace failed with ESRCH, the tracee may be "not
4137 yet fully dead", but already refusing ptrace requests. In that
4138 case the tracee has 'R (Running)' state for a little bit
4139 (observed in Linux 3.18). See also the note on ESRCH in the
4140 ptrace(2) man page. Instead, check whether the LWP has any state
4141 other than ptrace-stopped. */
4143 /* Don't assume anything if /proc/PID/status can't be read. */
4144 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread)) == 0)
4146 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4147 lp->status_pending_p = 0;
4148 return 1;
4150 return 0;
4153 void
4154 linux_process_target::resume_one_lwp (lwp_info *lwp, int step, int signal,
4155 siginfo_t *info)
4159 resume_one_lwp_throw (lwp, step, signal, info);
4161 catch (const gdb_exception_error &ex)
4163 if (check_ptrace_stopped_lwp_gone (lwp))
4165 /* This could because we tried to resume an LWP after its leader
4166 exited. Mark it as resumed, so we can collect an exit event
4167 from it. */
4168 lwp->stopped = 0;
4169 lwp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4171 else
4172 throw;
4176 /* This function is called once per thread via for_each_thread.
4177 We look up which resume request applies to THREAD and mark it with a
4178 pointer to the appropriate resume request.
4180 This algorithm is O(threads * resume elements), but resume elements
4181 is small (and will remain small at least until GDB supports thread
4182 suspension). */
4184 static void
4185 linux_set_resume_request (thread_info *thread, thread_resume *resume, size_t n)
4187 struct lwp_info *lwp = get_thread_lwp (thread);
4189 for (int ndx = 0; ndx < n; ndx++)
4191 ptid_t ptid = resume[ndx].thread;
4192 if (ptid == minus_one_ptid
4193 || ptid == thread->id
4194 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4195 of PID'. */
4196 || (ptid.pid () == pid_of (thread)
4197 && (ptid.is_pid ()
4198 || ptid.lwp () == -1)))
4200 if (resume[ndx].kind == resume_stop
4201 && thread->last_resume_kind == resume_stop)
4203 threads_debug_printf
4204 ("already %s LWP %ld at GDB's request",
4205 (thread->last_status.kind () == TARGET_WAITKIND_STOPPED
4206 ? "stopped" : "stopping"),
4207 lwpid_of (thread));
4209 continue;
4212 /* Ignore (wildcard) resume requests for already-resumed
4213 threads. */
4214 if (resume[ndx].kind != resume_stop
4215 && thread->last_resume_kind != resume_stop)
4217 threads_debug_printf
4218 ("already %s LWP %ld at GDB's request",
4219 (thread->last_resume_kind == resume_step
4220 ? "stepping" : "continuing"),
4221 lwpid_of (thread));
4222 continue;
4225 /* Don't let wildcard resumes resume fork children that GDB
4226 does not yet know are new fork children. */
4227 if (lwp->fork_relative != NULL)
4229 struct lwp_info *rel = lwp->fork_relative;
4231 if (rel->status_pending_p
4232 && (rel->waitstatus.kind () == TARGET_WAITKIND_FORKED
4233 || rel->waitstatus.kind () == TARGET_WAITKIND_VFORKED))
4235 threads_debug_printf
4236 ("not resuming LWP %ld: has queued stop reply",
4237 lwpid_of (thread));
4238 continue;
4242 /* If the thread has a pending event that has already been
4243 reported to GDBserver core, but GDB has not pulled the
4244 event out of the vStopped queue yet, likewise, ignore the
4245 (wildcard) resume request. */
4246 if (in_queued_stop_replies (thread->id))
4248 threads_debug_printf
4249 ("not resuming LWP %ld: has queued stop reply",
4250 lwpid_of (thread));
4251 continue;
4254 lwp->resume = &resume[ndx];
4255 thread->last_resume_kind = lwp->resume->kind;
4257 lwp->step_range_start = lwp->resume->step_range_start;
4258 lwp->step_range_end = lwp->resume->step_range_end;
4260 /* If we had a deferred signal to report, dequeue one now.
4261 This can happen if LWP gets more than one signal while
4262 trying to get out of a jump pad. */
4263 if (lwp->stopped
4264 && !lwp->status_pending_p
4265 && dequeue_one_deferred_signal (lwp, &lwp->status_pending))
4267 lwp->status_pending_p = 1;
4269 threads_debug_printf
4270 ("Dequeueing deferred signal %d for LWP %ld, "
4271 "leaving status pending.",
4272 WSTOPSIG (lwp->status_pending),
4273 lwpid_of (thread));
4276 return;
4280 /* No resume action for this thread. */
4281 lwp->resume = NULL;
4284 bool
4285 linux_process_target::resume_status_pending (thread_info *thread)
4287 struct lwp_info *lwp = get_thread_lwp (thread);
4289 /* LWPs which will not be resumed are not interesting, because
4290 we might not wait for them next time through linux_wait. */
4291 if (lwp->resume == NULL)
4292 return false;
4294 return thread_still_has_status_pending (thread);
4297 bool
4298 linux_process_target::thread_needs_step_over (thread_info *thread)
4300 struct lwp_info *lwp = get_thread_lwp (thread);
4301 CORE_ADDR pc;
4302 struct process_info *proc = get_thread_process (thread);
4304 /* GDBserver is skipping the extra traps from the wrapper program,
4305 don't have to do step over. */
4306 if (proc->tdesc == NULL)
4307 return false;
4309 /* LWPs which will not be resumed are not interesting, because we
4310 might not wait for them next time through linux_wait. */
4312 if (!lwp->stopped)
4314 threads_debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped",
4315 lwpid_of (thread));
4316 return false;
4319 if (thread->last_resume_kind == resume_stop)
4321 threads_debug_printf
4322 ("Need step over [LWP %ld]? Ignoring, should remain stopped",
4323 lwpid_of (thread));
4324 return false;
4327 gdb_assert (lwp->suspended >= 0);
4329 if (lwp->suspended)
4331 threads_debug_printf ("Need step over [LWP %ld]? Ignoring, suspended",
4332 lwpid_of (thread));
4333 return false;
4336 if (lwp->status_pending_p)
4338 threads_debug_printf
4339 ("Need step over [LWP %ld]? Ignoring, has pending status.",
4340 lwpid_of (thread));
4341 return false;
4344 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4345 or we have. */
4346 pc = get_pc (lwp);
4348 /* If the PC has changed since we stopped, then don't do anything,
4349 and let the breakpoint/tracepoint be hit. This happens if, for
4350 instance, GDB handled the decr_pc_after_break subtraction itself,
4351 GDB is OOL stepping this thread, or the user has issued a "jump"
4352 command, or poked thread's registers herself. */
4353 if (pc != lwp->stop_pc)
4355 threads_debug_printf
4356 ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4357 "Old stop_pc was 0x%s, PC is now 0x%s", lwpid_of (thread),
4358 paddress (lwp->stop_pc), paddress (pc));
4359 return false;
4362 /* On software single step target, resume the inferior with signal
4363 rather than stepping over. */
4364 if (supports_software_single_step ()
4365 && !lwp->pending_signals.empty ()
4366 && lwp_signal_can_be_delivered (lwp))
4368 threads_debug_printf
4369 ("Need step over [LWP %ld]? Ignoring, has pending signals.",
4370 lwpid_of (thread));
4372 return false;
4375 scoped_restore_current_thread restore_thread;
4376 switch_to_thread (thread);
4378 /* We can only step over breakpoints we know about. */
4379 if (breakpoint_here (pc) || fast_tracepoint_jump_here (pc))
4381 /* Don't step over a breakpoint that GDB expects to hit
4382 though. If the condition is being evaluated on the target's side
4383 and it evaluate to false, step over this breakpoint as well. */
4384 if (gdb_breakpoint_here (pc)
4385 && gdb_condition_true_at_breakpoint (pc)
4386 && gdb_no_commands_at_breakpoint (pc))
4388 threads_debug_printf ("Need step over [LWP %ld]? yes, but found"
4389 " GDB breakpoint at 0x%s; skipping step over",
4390 lwpid_of (thread), paddress (pc));
4392 return false;
4394 else
4396 threads_debug_printf ("Need step over [LWP %ld]? yes, "
4397 "found breakpoint at 0x%s",
4398 lwpid_of (thread), paddress (pc));
4400 /* We've found an lwp that needs stepping over --- return 1 so
4401 that find_thread stops looking. */
4402 return true;
4406 threads_debug_printf
4407 ("Need step over [LWP %ld]? No, no breakpoint found at 0x%s",
4408 lwpid_of (thread), paddress (pc));
4410 return false;
4413 void
4414 linux_process_target::start_step_over (lwp_info *lwp)
4416 struct thread_info *thread = get_lwp_thread (lwp);
4417 CORE_ADDR pc;
4419 threads_debug_printf ("Starting step-over on LWP %ld. Stopping all threads",
4420 lwpid_of (thread));
4422 stop_all_lwps (1, lwp);
4424 if (lwp->suspended != 0)
4426 internal_error (__FILE__, __LINE__,
4427 "LWP %ld suspended=%d\n", lwpid_of (thread),
4428 lwp->suspended);
4431 threads_debug_printf ("Done stopping all threads for step-over.");
4433 /* Note, we should always reach here with an already adjusted PC,
4434 either by GDB (if we're resuming due to GDB's request), or by our
4435 caller, if we just finished handling an internal breakpoint GDB
4436 shouldn't care about. */
4437 pc = get_pc (lwp);
4439 bool step = false;
4441 scoped_restore_current_thread restore_thread;
4442 switch_to_thread (thread);
4444 lwp->bp_reinsert = pc;
4445 uninsert_breakpoints_at (pc);
4446 uninsert_fast_tracepoint_jumps_at (pc);
4448 step = single_step (lwp);
4451 resume_one_lwp (lwp, step, 0, NULL);
4453 /* Require next event from this LWP. */
4454 step_over_bkpt = thread->id;
4457 bool
4458 linux_process_target::finish_step_over (lwp_info *lwp)
4460 if (lwp->bp_reinsert != 0)
4462 scoped_restore_current_thread restore_thread;
4464 threads_debug_printf ("Finished step over.");
4466 switch_to_thread (get_lwp_thread (lwp));
4468 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4469 may be no breakpoint to reinsert there by now. */
4470 reinsert_breakpoints_at (lwp->bp_reinsert);
4471 reinsert_fast_tracepoint_jumps_at (lwp->bp_reinsert);
4473 lwp->bp_reinsert = 0;
4475 /* Delete any single-step breakpoints. No longer needed. We
4476 don't have to worry about other threads hitting this trap,
4477 and later not being able to explain it, because we were
4478 stepping over a breakpoint, and we hold all threads but
4479 LWP stopped while doing that. */
4480 if (!supports_hardware_single_step ())
4482 gdb_assert (has_single_step_breakpoints (current_thread));
4483 delete_single_step_breakpoints (current_thread);
4486 step_over_bkpt = null_ptid;
4487 return true;
4489 else
4490 return false;
4493 void
4494 linux_process_target::complete_ongoing_step_over ()
4496 if (step_over_bkpt != null_ptid)
4498 struct lwp_info *lwp;
4499 int wstat;
4500 int ret;
4502 threads_debug_printf ("detach: step over in progress, finish it first");
4504 /* Passing NULL_PTID as filter indicates we want all events to
4505 be left pending. Eventually this returns when there are no
4506 unwaited-for children left. */
4507 ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat,
4508 __WALL);
4509 gdb_assert (ret == -1);
4511 lwp = find_lwp_pid (step_over_bkpt);
4512 if (lwp != NULL)
4514 finish_step_over (lwp);
4516 /* If we got our step SIGTRAP, don't leave it pending,
4517 otherwise we would report it to GDB as a spurious
4518 SIGTRAP. */
4519 gdb_assert (lwp->status_pending_p);
4520 if (WIFSTOPPED (lwp->status_pending)
4521 && WSTOPSIG (lwp->status_pending) == SIGTRAP)
4523 thread_info *thread = get_lwp_thread (lwp);
4524 if (thread->last_resume_kind != resume_step)
4526 threads_debug_printf ("detach: discard step-over SIGTRAP");
4528 lwp->status_pending_p = 0;
4529 lwp->status_pending = 0;
4530 resume_one_lwp (lwp, lwp->stepping, 0, NULL);
4532 else
4533 threads_debug_printf
4534 ("detach: resume_step, not discarding step-over SIGTRAP");
4537 step_over_bkpt = null_ptid;
4538 unsuspend_all_lwps (lwp);
4542 void
4543 linux_process_target::resume_one_thread (thread_info *thread,
4544 bool leave_all_stopped)
4546 struct lwp_info *lwp = get_thread_lwp (thread);
4547 int leave_pending;
4549 if (lwp->resume == NULL)
4550 return;
4552 if (lwp->resume->kind == resume_stop)
4554 threads_debug_printf ("resume_stop request for LWP %ld",
4555 lwpid_of (thread));
4557 if (!lwp->stopped)
4559 threads_debug_printf ("stopping LWP %ld", lwpid_of (thread));
4561 /* Stop the thread, and wait for the event asynchronously,
4562 through the event loop. */
4563 send_sigstop (lwp);
4565 else
4567 threads_debug_printf ("already stopped LWP %ld", lwpid_of (thread));
4569 /* The LWP may have been stopped in an internal event that
4570 was not meant to be notified back to GDB (e.g., gdbserver
4571 breakpoint), so we should be reporting a stop event in
4572 this case too. */
4574 /* If the thread already has a pending SIGSTOP, this is a
4575 no-op. Otherwise, something later will presumably resume
4576 the thread and this will cause it to cancel any pending
4577 operation, due to last_resume_kind == resume_stop. If
4578 the thread already has a pending status to report, we
4579 will still report it the next time we wait - see
4580 status_pending_p_callback. */
4582 /* If we already have a pending signal to report, then
4583 there's no need to queue a SIGSTOP, as this means we're
4584 midway through moving the LWP out of the jumppad, and we
4585 will report the pending signal as soon as that is
4586 finished. */
4587 if (lwp->pending_signals_to_report.empty ())
4588 send_sigstop (lwp);
4591 /* For stop requests, we're done. */
4592 lwp->resume = NULL;
4593 thread->last_status.set_ignore ();
4594 return;
4597 /* If this thread which is about to be resumed has a pending status,
4598 then don't resume it - we can just report the pending status.
4599 Likewise if it is suspended, because e.g., another thread is
4600 stepping past a breakpoint. Make sure to queue any signals that
4601 would otherwise be sent. In all-stop mode, we do this decision
4602 based on if *any* thread has a pending status. If there's a
4603 thread that needs the step-over-breakpoint dance, then don't
4604 resume any other thread but that particular one. */
4605 leave_pending = (lwp->suspended
4606 || lwp->status_pending_p
4607 || leave_all_stopped);
4609 /* If we have a new signal, enqueue the signal. */
4610 if (lwp->resume->sig != 0)
4612 siginfo_t info, *info_p;
4614 /* If this is the same signal we were previously stopped by,
4615 make sure to queue its siginfo. */
4616 if (WIFSTOPPED (lwp->last_status)
4617 && WSTOPSIG (lwp->last_status) == lwp->resume->sig
4618 && ptrace (PTRACE_GETSIGINFO, lwpid_of (thread),
4619 (PTRACE_TYPE_ARG3) 0, &info) == 0)
4620 info_p = &info;
4621 else
4622 info_p = NULL;
4624 enqueue_pending_signal (lwp, lwp->resume->sig, info_p);
4627 if (!leave_pending)
4629 threads_debug_printf ("resuming LWP %ld", lwpid_of (thread));
4631 proceed_one_lwp (thread, NULL);
4633 else
4634 threads_debug_printf ("leaving LWP %ld stopped", lwpid_of (thread));
4636 thread->last_status.set_ignore ();
4637 lwp->resume = NULL;
4640 void
4641 linux_process_target::resume (thread_resume *resume_info, size_t n)
4643 struct thread_info *need_step_over = NULL;
4645 THREADS_SCOPED_DEBUG_ENTER_EXIT;
4647 for_each_thread ([&] (thread_info *thread)
4649 linux_set_resume_request (thread, resume_info, n);
4652 /* If there is a thread which would otherwise be resumed, which has
4653 a pending status, then don't resume any threads - we can just
4654 report the pending status. Make sure to queue any signals that
4655 would otherwise be sent. In non-stop mode, we'll apply this
4656 logic to each thread individually. We consume all pending events
4657 before considering to start a step-over (in all-stop). */
4658 bool any_pending = false;
4659 if (!non_stop)
4660 any_pending = find_thread ([this] (thread_info *thread)
4662 return resume_status_pending (thread);
4663 }) != nullptr;
4665 /* If there is a thread which would otherwise be resumed, which is
4666 stopped at a breakpoint that needs stepping over, then don't
4667 resume any threads - have it step over the breakpoint with all
4668 other threads stopped, then resume all threads again. Make sure
4669 to queue any signals that would otherwise be delivered or
4670 queued. */
4671 if (!any_pending && low_supports_breakpoints ())
4672 need_step_over = find_thread ([this] (thread_info *thread)
4674 return thread_needs_step_over (thread);
4677 bool leave_all_stopped = (need_step_over != NULL || any_pending);
4679 if (need_step_over != NULL)
4680 threads_debug_printf ("Not resuming all, need step over");
4681 else if (any_pending)
4682 threads_debug_printf ("Not resuming, all-stop and found "
4683 "an LWP with pending status");
4684 else
4685 threads_debug_printf ("Resuming, no pending status or step over needed");
4687 /* Even if we're leaving threads stopped, queue all signals we'd
4688 otherwise deliver. */
4689 for_each_thread ([&] (thread_info *thread)
4691 resume_one_thread (thread, leave_all_stopped);
4694 if (need_step_over)
4695 start_step_over (get_thread_lwp (need_step_over));
4697 /* We may have events that were pending that can/should be sent to
4698 the client now. Trigger a linux_wait call. */
4699 if (target_is_async_p ())
4700 async_file_mark ();
4703 void
4704 linux_process_target::proceed_one_lwp (thread_info *thread, lwp_info *except)
4706 struct lwp_info *lwp = get_thread_lwp (thread);
4707 int step;
4709 if (lwp == except)
4710 return;
4712 threads_debug_printf ("lwp %ld", lwpid_of (thread));
4714 if (!lwp->stopped)
4716 threads_debug_printf (" LWP %ld already running", lwpid_of (thread));
4717 return;
4720 if (thread->last_resume_kind == resume_stop
4721 && thread->last_status.kind () != TARGET_WAITKIND_IGNORE)
4723 threads_debug_printf (" client wants LWP to remain %ld stopped",
4724 lwpid_of (thread));
4725 return;
4728 if (lwp->status_pending_p)
4730 threads_debug_printf (" LWP %ld has pending status, leaving stopped",
4731 lwpid_of (thread));
4732 return;
4735 gdb_assert (lwp->suspended >= 0);
4737 if (lwp->suspended)
4739 threads_debug_printf (" LWP %ld is suspended", lwpid_of (thread));
4740 return;
4743 if (thread->last_resume_kind == resume_stop
4744 && lwp->pending_signals_to_report.empty ()
4745 && (lwp->collecting_fast_tracepoint
4746 == fast_tpoint_collect_result::not_collecting))
4748 /* We haven't reported this LWP as stopped yet (otherwise, the
4749 last_status.kind check above would catch it, and we wouldn't
4750 reach here. This LWP may have been momentarily paused by a
4751 stop_all_lwps call while handling for example, another LWP's
4752 step-over. In that case, the pending expected SIGSTOP signal
4753 that was queued at vCont;t handling time will have already
4754 been consumed by wait_for_sigstop, and so we need to requeue
4755 another one here. Note that if the LWP already has a SIGSTOP
4756 pending, this is a no-op. */
4758 threads_debug_printf
4759 ("Client wants LWP %ld to stop. Making sure it has a SIGSTOP pending",
4760 lwpid_of (thread));
4762 send_sigstop (lwp);
4765 if (thread->last_resume_kind == resume_step)
4767 threads_debug_printf (" stepping LWP %ld, client wants it stepping",
4768 lwpid_of (thread));
4770 /* If resume_step is requested by GDB, install single-step
4771 breakpoints when the thread is about to be actually resumed if
4772 the single-step breakpoints weren't removed. */
4773 if (supports_software_single_step ()
4774 && !has_single_step_breakpoints (thread))
4775 install_software_single_step_breakpoints (lwp);
4777 step = maybe_hw_step (thread);
4779 else if (lwp->bp_reinsert != 0)
4781 threads_debug_printf (" stepping LWP %ld, reinsert set",
4782 lwpid_of (thread));
4784 step = maybe_hw_step (thread);
4786 else
4787 step = 0;
4789 resume_one_lwp (lwp, step, 0, NULL);
4792 void
4793 linux_process_target::unsuspend_and_proceed_one_lwp (thread_info *thread,
4794 lwp_info *except)
4796 struct lwp_info *lwp = get_thread_lwp (thread);
4798 if (lwp == except)
4799 return;
4801 lwp_suspended_decr (lwp);
4803 proceed_one_lwp (thread, except);
4806 void
4807 linux_process_target::proceed_all_lwps ()
4809 struct thread_info *need_step_over;
4811 /* If there is a thread which would otherwise be resumed, which is
4812 stopped at a breakpoint that needs stepping over, then don't
4813 resume any threads - have it step over the breakpoint with all
4814 other threads stopped, then resume all threads again. */
4816 if (low_supports_breakpoints ())
4818 need_step_over = find_thread ([this] (thread_info *thread)
4820 return thread_needs_step_over (thread);
4823 if (need_step_over != NULL)
4825 threads_debug_printf ("found thread %ld needing a step-over",
4826 lwpid_of (need_step_over));
4828 start_step_over (get_thread_lwp (need_step_over));
4829 return;
4833 threads_debug_printf ("Proceeding, no step-over needed");
4835 for_each_thread ([this] (thread_info *thread)
4837 proceed_one_lwp (thread, NULL);
4841 void
4842 linux_process_target::unstop_all_lwps (int unsuspend, lwp_info *except)
4844 THREADS_SCOPED_DEBUG_ENTER_EXIT;
4846 if (except)
4847 threads_debug_printf ("except=(LWP %ld)",
4848 lwpid_of (get_lwp_thread (except)));
4849 else
4850 threads_debug_printf ("except=nullptr");
4852 if (unsuspend)
4853 for_each_thread ([&] (thread_info *thread)
4855 unsuspend_and_proceed_one_lwp (thread, except);
4857 else
4858 for_each_thread ([&] (thread_info *thread)
4860 proceed_one_lwp (thread, except);
4865 #ifdef HAVE_LINUX_REGSETS
4867 #define use_linux_regsets 1
4869 /* Returns true if REGSET has been disabled. */
4871 static int
4872 regset_disabled (struct regsets_info *info, struct regset_info *regset)
4874 return (info->disabled_regsets != NULL
4875 && info->disabled_regsets[regset - info->regsets]);
4878 /* Disable REGSET. */
4880 static void
4881 disable_regset (struct regsets_info *info, struct regset_info *regset)
4883 int dr_offset;
4885 dr_offset = regset - info->regsets;
4886 if (info->disabled_regsets == NULL)
4887 info->disabled_regsets = (char *) xcalloc (1, info->num_regsets);
4888 info->disabled_regsets[dr_offset] = 1;
4891 static int
4892 regsets_fetch_inferior_registers (struct regsets_info *regsets_info,
4893 struct regcache *regcache)
4895 struct regset_info *regset;
4896 int saw_general_regs = 0;
4897 int pid;
4898 struct iovec iov;
4900 pid = lwpid_of (current_thread);
4901 for (regset = regsets_info->regsets; regset->size >= 0; regset++)
4903 void *buf, *data;
4904 int nt_type, res;
4906 if (regset->size == 0 || regset_disabled (regsets_info, regset))
4907 continue;
4909 buf = xmalloc (regset->size);
4911 nt_type = regset->nt_type;
4912 if (nt_type)
4914 iov.iov_base = buf;
4915 iov.iov_len = regset->size;
4916 data = (void *) &iov;
4918 else
4919 data = buf;
4921 #ifndef __sparc__
4922 res = ptrace (regset->get_request, pid,
4923 (PTRACE_TYPE_ARG3) (long) nt_type, data);
4924 #else
4925 res = ptrace (regset->get_request, pid, data, nt_type);
4926 #endif
4927 if (res < 0)
4929 if (errno == EIO
4930 || (errno == EINVAL && regset->type == OPTIONAL_REGS))
4932 /* If we get EIO on a regset, or an EINVAL and the regset is
4933 optional, do not try it again for this process mode. */
4934 disable_regset (regsets_info, regset);
4936 else if (errno == ENODATA)
4938 /* ENODATA may be returned if the regset is currently
4939 not "active". This can happen in normal operation,
4940 so suppress the warning in this case. */
4942 else if (errno == ESRCH)
4944 /* At this point, ESRCH should mean the process is
4945 already gone, in which case we simply ignore attempts
4946 to read its registers. */
4948 else
4950 char s[256];
4951 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
4952 pid);
4953 perror (s);
4956 else
4958 if (regset->type == GENERAL_REGS)
4959 saw_general_regs = 1;
4960 regset->store_function (regcache, buf);
4962 free (buf);
4964 if (saw_general_regs)
4965 return 0;
4966 else
4967 return 1;
4970 static int
4971 regsets_store_inferior_registers (struct regsets_info *regsets_info,
4972 struct regcache *regcache)
4974 struct regset_info *regset;
4975 int saw_general_regs = 0;
4976 int pid;
4977 struct iovec iov;
4979 pid = lwpid_of (current_thread);
4980 for (regset = regsets_info->regsets; regset->size >= 0; regset++)
4982 void *buf, *data;
4983 int nt_type, res;
4985 if (regset->size == 0 || regset_disabled (regsets_info, regset)
4986 || regset->fill_function == NULL)
4987 continue;
4989 buf = xmalloc (regset->size);
4991 /* First fill the buffer with the current register set contents,
4992 in case there are any items in the kernel's regset that are
4993 not in gdbserver's regcache. */
4995 nt_type = regset->nt_type;
4996 if (nt_type)
4998 iov.iov_base = buf;
4999 iov.iov_len = regset->size;
5000 data = (void *) &iov;
5002 else
5003 data = buf;
5005 #ifndef __sparc__
5006 res = ptrace (regset->get_request, pid,
5007 (PTRACE_TYPE_ARG3) (long) nt_type, data);
5008 #else
5009 res = ptrace (regset->get_request, pid, data, nt_type);
5010 #endif
5012 if (res == 0)
5014 /* Then overlay our cached registers on that. */
5015 regset->fill_function (regcache, buf);
5017 /* Only now do we write the register set. */
5018 #ifndef __sparc__
5019 res = ptrace (regset->set_request, pid,
5020 (PTRACE_TYPE_ARG3) (long) nt_type, data);
5021 #else
5022 res = ptrace (regset->set_request, pid, data, nt_type);
5023 #endif
5026 if (res < 0)
5028 if (errno == EIO
5029 || (errno == EINVAL && regset->type == OPTIONAL_REGS))
5031 /* If we get EIO on a regset, or an EINVAL and the regset is
5032 optional, do not try it again for this process mode. */
5033 disable_regset (regsets_info, regset);
5035 else if (errno == ESRCH)
5037 /* At this point, ESRCH should mean the process is
5038 already gone, in which case we simply ignore attempts
5039 to change its registers. See also the related
5040 comment in resume_one_lwp. */
5041 free (buf);
5042 return 0;
5044 else
5046 perror ("Warning: ptrace(regsets_store_inferior_registers)");
5049 else if (regset->type == GENERAL_REGS)
5050 saw_general_regs = 1;
5051 free (buf);
5053 if (saw_general_regs)
5054 return 0;
5055 else
5056 return 1;
5059 #else /* !HAVE_LINUX_REGSETS */
5061 #define use_linux_regsets 0
5062 #define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5063 #define regsets_store_inferior_registers(regsets_info, regcache) 1
5065 #endif
5067 /* Return 1 if register REGNO is supported by one of the regset ptrace
5068 calls or 0 if it has to be transferred individually. */
5070 static int
5071 linux_register_in_regsets (const struct regs_info *regs_info, int regno)
5073 unsigned char mask = 1 << (regno % 8);
5074 size_t index = regno / 8;
5076 return (use_linux_regsets
5077 && (regs_info->regset_bitmap == NULL
5078 || (regs_info->regset_bitmap[index] & mask) != 0));
5081 #ifdef HAVE_LINUX_USRREGS
5083 static int
5084 register_addr (const struct usrregs_info *usrregs, int regnum)
5086 int addr;
5088 if (regnum < 0 || regnum >= usrregs->num_regs)
5089 error ("Invalid register number %d.", regnum);
5091 addr = usrregs->regmap[regnum];
5093 return addr;
5097 void
5098 linux_process_target::fetch_register (const usrregs_info *usrregs,
5099 regcache *regcache, int regno)
5101 CORE_ADDR regaddr;
5102 int i, size;
5103 char *buf;
5104 int pid;
5106 if (regno >= usrregs->num_regs)
5107 return;
5108 if (low_cannot_fetch_register (regno))
5109 return;
5111 regaddr = register_addr (usrregs, regno);
5112 if (regaddr == -1)
5113 return;
5115 size = ((register_size (regcache->tdesc, regno)
5116 + sizeof (PTRACE_XFER_TYPE) - 1)
5117 & -sizeof (PTRACE_XFER_TYPE));
5118 buf = (char *) alloca (size);
5120 pid = lwpid_of (current_thread);
5121 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
5123 errno = 0;
5124 *(PTRACE_XFER_TYPE *) (buf + i) =
5125 ptrace (PTRACE_PEEKUSER, pid,
5126 /* Coerce to a uintptr_t first to avoid potential gcc warning
5127 of coercing an 8 byte integer to a 4 byte pointer. */
5128 (PTRACE_TYPE_ARG3) (uintptr_t) regaddr, (PTRACE_TYPE_ARG4) 0);
5129 regaddr += sizeof (PTRACE_XFER_TYPE);
5130 if (errno != 0)
5132 /* Mark register REGNO unavailable. */
5133 supply_register (regcache, regno, NULL);
5134 return;
5138 low_supply_ptrace_register (regcache, regno, buf);
5141 void
5142 linux_process_target::store_register (const usrregs_info *usrregs,
5143 regcache *regcache, int regno)
5145 CORE_ADDR regaddr;
5146 int i, size;
5147 char *buf;
5148 int pid;
5150 if (regno >= usrregs->num_regs)
5151 return;
5152 if (low_cannot_store_register (regno))
5153 return;
5155 regaddr = register_addr (usrregs, regno);
5156 if (regaddr == -1)
5157 return;
5159 size = ((register_size (regcache->tdesc, regno)
5160 + sizeof (PTRACE_XFER_TYPE) - 1)
5161 & -sizeof (PTRACE_XFER_TYPE));
5162 buf = (char *) alloca (size);
5163 memset (buf, 0, size);
5165 low_collect_ptrace_register (regcache, regno, buf);
5167 pid = lwpid_of (current_thread);
5168 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
5170 errno = 0;
5171 ptrace (PTRACE_POKEUSER, pid,
5172 /* Coerce to a uintptr_t first to avoid potential gcc warning
5173 about coercing an 8 byte integer to a 4 byte pointer. */
5174 (PTRACE_TYPE_ARG3) (uintptr_t) regaddr,
5175 (PTRACE_TYPE_ARG4) *(PTRACE_XFER_TYPE *) (buf + i));
5176 if (errno != 0)
5178 /* At this point, ESRCH should mean the process is
5179 already gone, in which case we simply ignore attempts
5180 to change its registers. See also the related
5181 comment in resume_one_lwp. */
5182 if (errno == ESRCH)
5183 return;
5186 if (!low_cannot_store_register (regno))
5187 error ("writing register %d: %s", regno, safe_strerror (errno));
5189 regaddr += sizeof (PTRACE_XFER_TYPE);
5192 #endif /* HAVE_LINUX_USRREGS */
5194 void
5195 linux_process_target::low_collect_ptrace_register (regcache *regcache,
5196 int regno, char *buf)
5198 collect_register (regcache, regno, buf);
5201 void
5202 linux_process_target::low_supply_ptrace_register (regcache *regcache,
5203 int regno, const char *buf)
5205 supply_register (regcache, regno, buf);
5208 void
5209 linux_process_target::usr_fetch_inferior_registers (const regs_info *regs_info,
5210 regcache *regcache,
5211 int regno, int all)
5213 #ifdef HAVE_LINUX_USRREGS
5214 struct usrregs_info *usr = regs_info->usrregs;
5216 if (regno == -1)
5218 for (regno = 0; regno < usr->num_regs; regno++)
5219 if (all || !linux_register_in_regsets (regs_info, regno))
5220 fetch_register (usr, regcache, regno);
5222 else
5223 fetch_register (usr, regcache, regno);
5224 #endif
5227 void
5228 linux_process_target::usr_store_inferior_registers (const regs_info *regs_info,
5229 regcache *regcache,
5230 int regno, int all)
5232 #ifdef HAVE_LINUX_USRREGS
5233 struct usrregs_info *usr = regs_info->usrregs;
5235 if (regno == -1)
5237 for (regno = 0; regno < usr->num_regs; regno++)
5238 if (all || !linux_register_in_regsets (regs_info, regno))
5239 store_register (usr, regcache, regno);
5241 else
5242 store_register (usr, regcache, regno);
5243 #endif
5246 void
5247 linux_process_target::fetch_registers (regcache *regcache, int regno)
5249 int use_regsets;
5250 int all = 0;
5251 const regs_info *regs_info = get_regs_info ();
5253 if (regno == -1)
5255 if (regs_info->usrregs != NULL)
5256 for (regno = 0; regno < regs_info->usrregs->num_regs; regno++)
5257 low_fetch_register (regcache, regno);
5259 all = regsets_fetch_inferior_registers (regs_info->regsets_info, regcache);
5260 if (regs_info->usrregs != NULL)
5261 usr_fetch_inferior_registers (regs_info, regcache, -1, all);
5263 else
5265 if (low_fetch_register (regcache, regno))
5266 return;
5268 use_regsets = linux_register_in_regsets (regs_info, regno);
5269 if (use_regsets)
5270 all = regsets_fetch_inferior_registers (regs_info->regsets_info,
5271 regcache);
5272 if ((!use_regsets || all) && regs_info->usrregs != NULL)
5273 usr_fetch_inferior_registers (regs_info, regcache, regno, 1);
5277 void
5278 linux_process_target::store_registers (regcache *regcache, int regno)
5280 int use_regsets;
5281 int all = 0;
5282 const regs_info *regs_info = get_regs_info ();
5284 if (regno == -1)
5286 all = regsets_store_inferior_registers (regs_info->regsets_info,
5287 regcache);
5288 if (regs_info->usrregs != NULL)
5289 usr_store_inferior_registers (regs_info, regcache, regno, all);
5291 else
5293 use_regsets = linux_register_in_regsets (regs_info, regno);
5294 if (use_regsets)
5295 all = regsets_store_inferior_registers (regs_info->regsets_info,
5296 regcache);
5297 if ((!use_regsets || all) && regs_info->usrregs != NULL)
5298 usr_store_inferior_registers (regs_info, regcache, regno, 1);
5302 bool
5303 linux_process_target::low_fetch_register (regcache *regcache, int regno)
5305 return false;
5308 /* A wrapper for the read_memory target op. */
5310 static int
5311 linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
5313 return the_target->read_memory (memaddr, myaddr, len);
5316 /* Copy LEN bytes from inferior's memory starting at MEMADDR
5317 to debugger memory starting at MYADDR. */
5320 linux_process_target::read_memory (CORE_ADDR memaddr,
5321 unsigned char *myaddr, int len)
5323 int pid = lwpid_of (current_thread);
5324 PTRACE_XFER_TYPE *buffer;
5325 CORE_ADDR addr;
5326 int count;
5327 char filename[64];
5328 int i;
5329 int ret;
5330 int fd;
5332 /* Try using /proc. Don't bother for one word. */
5333 if (len >= 3 * sizeof (long))
5335 int bytes;
5337 /* We could keep this file open and cache it - possibly one per
5338 thread. That requires some juggling, but is even faster. */
5339 sprintf (filename, "/proc/%d/mem", pid);
5340 fd = open (filename, O_RDONLY | O_LARGEFILE);
5341 if (fd == -1)
5342 goto no_proc;
5344 /* If pread64 is available, use it. It's faster if the kernel
5345 supports it (only one syscall), and it's 64-bit safe even on
5346 32-bit platforms (for instance, SPARC debugging a SPARC64
5347 application). */
5348 #ifdef HAVE_PREAD64
5349 bytes = pread64 (fd, myaddr, len, memaddr);
5350 #else
5351 bytes = -1;
5352 if (lseek (fd, memaddr, SEEK_SET) != -1)
5353 bytes = read (fd, myaddr, len);
5354 #endif
5356 close (fd);
5357 if (bytes == len)
5358 return 0;
5360 /* Some data was read, we'll try to get the rest with ptrace. */
5361 if (bytes > 0)
5363 memaddr += bytes;
5364 myaddr += bytes;
5365 len -= bytes;
5369 no_proc:
5370 /* Round starting address down to longword boundary. */
5371 addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
5372 /* Round ending address up; get number of longwords that makes. */
5373 count = ((((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
5374 / sizeof (PTRACE_XFER_TYPE));
5375 /* Allocate buffer of that many longwords. */
5376 buffer = XALLOCAVEC (PTRACE_XFER_TYPE, count);
5378 /* Read all the longwords */
5379 errno = 0;
5380 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
5382 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5383 about coercing an 8 byte integer to a 4 byte pointer. */
5384 buffer[i] = ptrace (PTRACE_PEEKTEXT, pid,
5385 (PTRACE_TYPE_ARG3) (uintptr_t) addr,
5386 (PTRACE_TYPE_ARG4) 0);
5387 if (errno)
5388 break;
5390 ret = errno;
5392 /* Copy appropriate bytes out of the buffer. */
5393 if (i > 0)
5395 i *= sizeof (PTRACE_XFER_TYPE);
5396 i -= memaddr & (sizeof (PTRACE_XFER_TYPE) - 1);
5397 memcpy (myaddr,
5398 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
5399 i < len ? i : len);
5402 return ret;
5405 /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5406 memory at MEMADDR. On failure (cannot write to the inferior)
5407 returns the value of errno. Always succeeds if LEN is zero. */
5410 linux_process_target::write_memory (CORE_ADDR memaddr,
5411 const unsigned char *myaddr, int len)
5413 int i;
5414 /* Round starting address down to longword boundary. */
5415 CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
5416 /* Round ending address up; get number of longwords that makes. */
5417 int count
5418 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
5419 / sizeof (PTRACE_XFER_TYPE);
5421 /* Allocate buffer of that many longwords. */
5422 PTRACE_XFER_TYPE *buffer = XALLOCAVEC (PTRACE_XFER_TYPE, count);
5424 int pid = lwpid_of (current_thread);
5426 if (len == 0)
5428 /* Zero length write always succeeds. */
5429 return 0;
5432 if (debug_threads)
5434 /* Dump up to four bytes. */
5435 char str[4 * 2 + 1];
5436 char *p = str;
5437 int dump = len < 4 ? len : 4;
5439 for (i = 0; i < dump; i++)
5441 sprintf (p, "%02x", myaddr[i]);
5442 p += 2;
5444 *p = '\0';
5446 threads_debug_printf ("Writing %s to 0x%08lx in process %d",
5447 str, (long) memaddr, pid);
5450 /* Fill start and end extra bytes of buffer with existing memory data. */
5452 errno = 0;
5453 /* Coerce the 3rd arg to a uintptr_t first to avoid potential gcc warning
5454 about coercing an 8 byte integer to a 4 byte pointer. */
5455 buffer[0] = ptrace (PTRACE_PEEKTEXT, pid,
5456 (PTRACE_TYPE_ARG3) (uintptr_t) addr,
5457 (PTRACE_TYPE_ARG4) 0);
5458 if (errno)
5459 return errno;
5461 if (count > 1)
5463 errno = 0;
5464 buffer[count - 1]
5465 = ptrace (PTRACE_PEEKTEXT, pid,
5466 /* Coerce to a uintptr_t first to avoid potential gcc warning
5467 about coercing an 8 byte integer to a 4 byte pointer. */
5468 (PTRACE_TYPE_ARG3) (uintptr_t) (addr + (count - 1)
5469 * sizeof (PTRACE_XFER_TYPE)),
5470 (PTRACE_TYPE_ARG4) 0);
5471 if (errno)
5472 return errno;
5475 /* Copy data to be written over corresponding part of buffer. */
5477 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
5478 myaddr, len);
5480 /* Write the entire buffer. */
5482 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
5484 errno = 0;
5485 ptrace (PTRACE_POKETEXT, pid,
5486 /* Coerce to a uintptr_t first to avoid potential gcc warning
5487 about coercing an 8 byte integer to a 4 byte pointer. */
5488 (PTRACE_TYPE_ARG3) (uintptr_t) addr,
5489 (PTRACE_TYPE_ARG4) buffer[i]);
5490 if (errno)
5491 return errno;
5494 return 0;
5497 void
5498 linux_process_target::look_up_symbols ()
5500 #ifdef USE_THREAD_DB
5501 struct process_info *proc = current_process ();
5503 if (proc->priv->thread_db != NULL)
5504 return;
5506 thread_db_init ();
5507 #endif
5510 void
5511 linux_process_target::request_interrupt ()
5513 /* Send a SIGINT to the process group. This acts just like the user
5514 typed a ^C on the controlling terminal. */
5515 ::kill (-signal_pid, SIGINT);
5518 bool
5519 linux_process_target::supports_read_auxv ()
5521 return true;
5524 /* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5525 to debugger memory starting at MYADDR. */
5528 linux_process_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
5529 unsigned int len)
5531 char filename[PATH_MAX];
5532 int fd, n;
5533 int pid = lwpid_of (current_thread);
5535 xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
5537 fd = open (filename, O_RDONLY);
5538 if (fd < 0)
5539 return -1;
5541 if (offset != (CORE_ADDR) 0
5542 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
5543 n = -1;
5544 else
5545 n = read (fd, myaddr, len);
5547 close (fd);
5549 return n;
5553 linux_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
5554 int size, raw_breakpoint *bp)
5556 if (type == raw_bkpt_type_sw)
5557 return insert_memory_breakpoint (bp);
5558 else
5559 return low_insert_point (type, addr, size, bp);
5563 linux_process_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
5564 int size, raw_breakpoint *bp)
5566 /* Unsupported (see target.h). */
5567 return 1;
5571 linux_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
5572 int size, raw_breakpoint *bp)
5574 if (type == raw_bkpt_type_sw)
5575 return remove_memory_breakpoint (bp);
5576 else
5577 return low_remove_point (type, addr, size, bp);
5581 linux_process_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
5582 int size, raw_breakpoint *bp)
5584 /* Unsupported (see target.h). */
5585 return 1;
5588 /* Implement the stopped_by_sw_breakpoint target_ops
5589 method. */
5591 bool
5592 linux_process_target::stopped_by_sw_breakpoint ()
5594 struct lwp_info *lwp = get_thread_lwp (current_thread);
5596 return (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT);
5599 /* Implement the supports_stopped_by_sw_breakpoint target_ops
5600 method. */
5602 bool
5603 linux_process_target::supports_stopped_by_sw_breakpoint ()
5605 return USE_SIGTRAP_SIGINFO;
5608 /* Implement the stopped_by_hw_breakpoint target_ops
5609 method. */
5611 bool
5612 linux_process_target::stopped_by_hw_breakpoint ()
5614 struct lwp_info *lwp = get_thread_lwp (current_thread);
5616 return (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT);
5619 /* Implement the supports_stopped_by_hw_breakpoint target_ops
5620 method. */
5622 bool
5623 linux_process_target::supports_stopped_by_hw_breakpoint ()
5625 return USE_SIGTRAP_SIGINFO;
5628 /* Implement the supports_hardware_single_step target_ops method. */
5630 bool
5631 linux_process_target::supports_hardware_single_step ()
5633 return true;
5636 bool
5637 linux_process_target::stopped_by_watchpoint ()
5639 struct lwp_info *lwp = get_thread_lwp (current_thread);
5641 return lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
5644 CORE_ADDR
5645 linux_process_target::stopped_data_address ()
5647 struct lwp_info *lwp = get_thread_lwp (current_thread);
5649 return lwp->stopped_data_address;
5652 /* This is only used for targets that define PT_TEXT_ADDR,
5653 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5654 the target has different ways of acquiring this information, like
5655 loadmaps. */
5657 bool
5658 linux_process_target::supports_read_offsets ()
5660 #ifdef SUPPORTS_READ_OFFSETS
5661 return true;
5662 #else
5663 return false;
5664 #endif
5667 /* Under uClinux, programs are loaded at non-zero offsets, which we need
5668 to tell gdb about. */
5671 linux_process_target::read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
5673 #ifdef SUPPORTS_READ_OFFSETS
5674 unsigned long text, text_end, data;
5675 int pid = lwpid_of (current_thread);
5677 errno = 0;
5679 text = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_ADDR,
5680 (PTRACE_TYPE_ARG4) 0);
5681 text_end = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_END_ADDR,
5682 (PTRACE_TYPE_ARG4) 0);
5683 data = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_DATA_ADDR,
5684 (PTRACE_TYPE_ARG4) 0);
5686 if (errno == 0)
5688 /* Both text and data offsets produced at compile-time (and so
5689 used by gdb) are relative to the beginning of the program,
5690 with the data segment immediately following the text segment.
5691 However, the actual runtime layout in memory may put the data
5692 somewhere else, so when we send gdb a data base-address, we
5693 use the real data base address and subtract the compile-time
5694 data base-address from it (which is just the length of the
5695 text segment). BSS immediately follows data in both
5696 cases. */
5697 *text_p = text;
5698 *data_p = data - (text_end - text);
5700 return 1;
5702 return 0;
5703 #else
5704 gdb_assert_not_reached ("target op read_offsets not supported");
5705 #endif
5708 bool
5709 linux_process_target::supports_get_tls_address ()
5711 #ifdef USE_THREAD_DB
5712 return true;
5713 #else
5714 return false;
5715 #endif
5719 linux_process_target::get_tls_address (thread_info *thread,
5720 CORE_ADDR offset,
5721 CORE_ADDR load_module,
5722 CORE_ADDR *address)
5724 #ifdef USE_THREAD_DB
5725 return thread_db_get_tls_address (thread, offset, load_module, address);
5726 #else
5727 return -1;
5728 #endif
5731 bool
5732 linux_process_target::supports_qxfer_osdata ()
5734 return true;
5738 linux_process_target::qxfer_osdata (const char *annex,
5739 unsigned char *readbuf,
5740 unsigned const char *writebuf,
5741 CORE_ADDR offset, int len)
5743 return linux_common_xfer_osdata (annex, readbuf, offset, len);
5746 void
5747 linux_process_target::siginfo_fixup (siginfo_t *siginfo,
5748 gdb_byte *inf_siginfo, int direction)
5750 bool done = low_siginfo_fixup (siginfo, inf_siginfo, direction);
5752 /* If there was no callback, or the callback didn't do anything,
5753 then just do a straight memcpy. */
5754 if (!done)
5756 if (direction == 1)
5757 memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
5758 else
5759 memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
5763 bool
5764 linux_process_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
5765 int direction)
5767 return false;
5770 bool
5771 linux_process_target::supports_qxfer_siginfo ()
5773 return true;
5777 linux_process_target::qxfer_siginfo (const char *annex,
5778 unsigned char *readbuf,
5779 unsigned const char *writebuf,
5780 CORE_ADDR offset, int len)
5782 int pid;
5783 siginfo_t siginfo;
5784 gdb_byte inf_siginfo[sizeof (siginfo_t)];
5786 if (current_thread == NULL)
5787 return -1;
5789 pid = lwpid_of (current_thread);
5791 threads_debug_printf ("%s siginfo for lwp %d.",
5792 readbuf != NULL ? "Reading" : "Writing",
5793 pid);
5795 if (offset >= sizeof (siginfo))
5796 return -1;
5798 if (ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
5799 return -1;
5801 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
5802 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
5803 inferior with a 64-bit GDBSERVER should look the same as debugging it
5804 with a 32-bit GDBSERVER, we need to convert it. */
5805 siginfo_fixup (&siginfo, inf_siginfo, 0);
5807 if (offset + len > sizeof (siginfo))
5808 len = sizeof (siginfo) - offset;
5810 if (readbuf != NULL)
5811 memcpy (readbuf, inf_siginfo + offset, len);
5812 else
5814 memcpy (inf_siginfo + offset, writebuf, len);
5816 /* Convert back to ptrace layout before flushing it out. */
5817 siginfo_fixup (&siginfo, inf_siginfo, 1);
5819 if (ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
5820 return -1;
5823 return len;
5826 /* SIGCHLD handler that serves two purposes: In non-stop/async mode,
5827 so we notice when children change state; as the handler for the
5828 sigsuspend in my_waitpid. */
5830 static void
5831 sigchld_handler (int signo)
5833 int old_errno = errno;
5835 if (debug_threads)
5839 /* Use the async signal safe debug function. */
5840 if (debug_write ("sigchld_handler\n",
5841 sizeof ("sigchld_handler\n") - 1) < 0)
5842 break; /* just ignore */
5843 } while (0);
5846 if (target_is_async_p ())
5847 async_file_mark (); /* trigger a linux_wait */
5849 errno = old_errno;
5852 bool
5853 linux_process_target::supports_non_stop ()
5855 return true;
5858 bool
5859 linux_process_target::async (bool enable)
5861 bool previous = target_is_async_p ();
5863 threads_debug_printf ("async (%d), previous=%d",
5864 enable, previous);
5866 if (previous != enable)
5868 sigset_t mask;
5869 sigemptyset (&mask);
5870 sigaddset (&mask, SIGCHLD);
5872 gdb_sigmask (SIG_BLOCK, &mask, NULL);
5874 if (enable)
5876 if (!linux_event_pipe.open_pipe ())
5878 gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
5880 warning ("creating event pipe failed.");
5881 return previous;
5884 /* Register the event loop handler. */
5885 add_file_handler (linux_event_pipe.event_fd (),
5886 handle_target_event, NULL,
5887 "linux-low");
5889 /* Always trigger a linux_wait. */
5890 async_file_mark ();
5892 else
5894 delete_file_handler (linux_event_pipe.event_fd ());
5896 linux_event_pipe.close_pipe ();
5899 gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
5902 return previous;
5906 linux_process_target::start_non_stop (bool nonstop)
5908 /* Register or unregister from event-loop accordingly. */
5909 target_async (nonstop);
5911 if (target_is_async_p () != (nonstop != false))
5912 return -1;
5914 return 0;
5917 bool
5918 linux_process_target::supports_multi_process ()
5920 return true;
5923 /* Check if fork events are supported. */
5925 bool
5926 linux_process_target::supports_fork_events ()
5928 return true;
5931 /* Check if vfork events are supported. */
5933 bool
5934 linux_process_target::supports_vfork_events ()
5936 return true;
5939 /* Check if exec events are supported. */
5941 bool
5942 linux_process_target::supports_exec_events ()
5944 return true;
5947 /* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
5948 ptrace flags for all inferiors. This is in case the new GDB connection
5949 doesn't support the same set of events that the previous one did. */
5951 void
5952 linux_process_target::handle_new_gdb_connection ()
5954 /* Request that all the lwps reset their ptrace options. */
5955 for_each_thread ([] (thread_info *thread)
5957 struct lwp_info *lwp = get_thread_lwp (thread);
5959 if (!lwp->stopped)
5961 /* Stop the lwp so we can modify its ptrace options. */
5962 lwp->must_set_ptrace_flags = 1;
5963 linux_stop_lwp (lwp);
5965 else
5967 /* Already stopped; go ahead and set the ptrace options. */
5968 struct process_info *proc = find_process_pid (pid_of (thread));
5969 int options = linux_low_ptrace_options (proc->attached);
5971 linux_enable_event_reporting (lwpid_of (thread), options);
5972 lwp->must_set_ptrace_flags = 0;
5978 linux_process_target::handle_monitor_command (char *mon)
5980 #ifdef USE_THREAD_DB
5981 return thread_db_handle_monitor_command (mon);
5982 #else
5983 return 0;
5984 #endif
5988 linux_process_target::core_of_thread (ptid_t ptid)
5990 return linux_common_core_of_thread (ptid);
5993 bool
5994 linux_process_target::supports_disable_randomization ()
5996 return true;
5999 bool
6000 linux_process_target::supports_agent ()
6002 return true;
6005 bool
6006 linux_process_target::supports_range_stepping ()
6008 if (supports_software_single_step ())
6009 return true;
6011 return low_supports_range_stepping ();
6014 bool
6015 linux_process_target::low_supports_range_stepping ()
6017 return false;
6020 bool
6021 linux_process_target::supports_pid_to_exec_file ()
6023 return true;
6026 const char *
6027 linux_process_target::pid_to_exec_file (int pid)
6029 return linux_proc_pid_to_exec_file (pid);
6032 bool
6033 linux_process_target::supports_multifs ()
6035 return true;
6039 linux_process_target::multifs_open (int pid, const char *filename,
6040 int flags, mode_t mode)
6042 return linux_mntns_open_cloexec (pid, filename, flags, mode);
6046 linux_process_target::multifs_unlink (int pid, const char *filename)
6048 return linux_mntns_unlink (pid, filename);
6051 ssize_t
6052 linux_process_target::multifs_readlink (int pid, const char *filename,
6053 char *buf, size_t bufsiz)
6055 return linux_mntns_readlink (pid, filename, buf, bufsiz);
6058 #if defined PT_GETDSBT || defined PTRACE_GETFDPIC
6059 struct target_loadseg
6061 /* Core address to which the segment is mapped. */
6062 Elf32_Addr addr;
6063 /* VMA recorded in the program header. */
6064 Elf32_Addr p_vaddr;
6065 /* Size of this segment in memory. */
6066 Elf32_Word p_memsz;
6069 # if defined PT_GETDSBT
6070 struct target_loadmap
6072 /* Protocol version number, must be zero. */
6073 Elf32_Word version;
6074 /* Pointer to the DSBT table, its size, and the DSBT index. */
6075 unsigned *dsbt_table;
6076 unsigned dsbt_size, dsbt_index;
6077 /* Number of segments in this map. */
6078 Elf32_Word nsegs;
6079 /* The actual memory map. */
6080 struct target_loadseg segs[/*nsegs*/];
6082 # define LINUX_LOADMAP PT_GETDSBT
6083 # define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6084 # define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6085 # else
6086 struct target_loadmap
6088 /* Protocol version number, must be zero. */
6089 Elf32_Half version;
6090 /* Number of segments in this map. */
6091 Elf32_Half nsegs;
6092 /* The actual memory map. */
6093 struct target_loadseg segs[/*nsegs*/];
6095 # define LINUX_LOADMAP PTRACE_GETFDPIC
6096 # define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6097 # define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6098 # endif
6100 bool
6101 linux_process_target::supports_read_loadmap ()
6103 return true;
6107 linux_process_target::read_loadmap (const char *annex, CORE_ADDR offset,
6108 unsigned char *myaddr, unsigned int len)
6110 int pid = lwpid_of (current_thread);
6111 int addr = -1;
6112 struct target_loadmap *data = NULL;
6113 unsigned int actual_length, copy_length;
6115 if (strcmp (annex, "exec") == 0)
6116 addr = (int) LINUX_LOADMAP_EXEC;
6117 else if (strcmp (annex, "interp") == 0)
6118 addr = (int) LINUX_LOADMAP_INTERP;
6119 else
6120 return -1;
6122 if (ptrace (LINUX_LOADMAP, pid, addr, &data) != 0)
6123 return -1;
6125 if (data == NULL)
6126 return -1;
6128 actual_length = sizeof (struct target_loadmap)
6129 + sizeof (struct target_loadseg) * data->nsegs;
6131 if (offset < 0 || offset > actual_length)
6132 return -1;
6134 copy_length = actual_length - offset < len ? actual_length - offset : len;
6135 memcpy (myaddr, (char *) data + offset, copy_length);
6136 return copy_length;
6138 #endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
6140 bool
6141 linux_process_target::supports_catch_syscall ()
6143 return low_supports_catch_syscall ();
6146 bool
6147 linux_process_target::low_supports_catch_syscall ()
6149 return false;
6152 CORE_ADDR
6153 linux_process_target::read_pc (regcache *regcache)
6155 if (!low_supports_breakpoints ())
6156 return 0;
6158 return low_get_pc (regcache);
6161 void
6162 linux_process_target::write_pc (regcache *regcache, CORE_ADDR pc)
6164 gdb_assert (low_supports_breakpoints ());
6166 low_set_pc (regcache, pc);
6169 bool
6170 linux_process_target::supports_thread_stopped ()
6172 return true;
6175 bool
6176 linux_process_target::thread_stopped (thread_info *thread)
6178 return get_thread_lwp (thread)->stopped;
6181 /* This exposes stop-all-threads functionality to other modules. */
6183 void
6184 linux_process_target::pause_all (bool freeze)
6186 stop_all_lwps (freeze, NULL);
6189 /* This exposes unstop-all-threads functionality to other gdbserver
6190 modules. */
6192 void
6193 linux_process_target::unpause_all (bool unfreeze)
6195 unstop_all_lwps (unfreeze, NULL);
6199 linux_process_target::prepare_to_access_memory ()
6201 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6202 running LWP. */
6203 if (non_stop)
6204 target_pause_all (true);
6205 return 0;
6208 void
6209 linux_process_target::done_accessing_memory ()
6211 /* Neither ptrace nor /proc/PID/mem allow accessing memory through a
6212 running LWP. */
6213 if (non_stop)
6214 target_unpause_all (true);
6217 /* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6219 static int
6220 get_phdr_phnum_from_proc_auxv (const int pid, const int is_elf64,
6221 CORE_ADDR *phdr_memaddr, int *num_phdr)
6223 char filename[PATH_MAX];
6224 int fd;
6225 const int auxv_size = is_elf64
6226 ? sizeof (Elf64_auxv_t) : sizeof (Elf32_auxv_t);
6227 char buf[sizeof (Elf64_auxv_t)]; /* The larger of the two. */
6229 xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
6231 fd = open (filename, O_RDONLY);
6232 if (fd < 0)
6233 return 1;
6235 *phdr_memaddr = 0;
6236 *num_phdr = 0;
6237 while (read (fd, buf, auxv_size) == auxv_size
6238 && (*phdr_memaddr == 0 || *num_phdr == 0))
6240 if (is_elf64)
6242 Elf64_auxv_t *const aux = (Elf64_auxv_t *) buf;
6244 switch (aux->a_type)
6246 case AT_PHDR:
6247 *phdr_memaddr = aux->a_un.a_val;
6248 break;
6249 case AT_PHNUM:
6250 *num_phdr = aux->a_un.a_val;
6251 break;
6254 else
6256 Elf32_auxv_t *const aux = (Elf32_auxv_t *) buf;
6258 switch (aux->a_type)
6260 case AT_PHDR:
6261 *phdr_memaddr = aux->a_un.a_val;
6262 break;
6263 case AT_PHNUM:
6264 *num_phdr = aux->a_un.a_val;
6265 break;
6270 close (fd);
6272 if (*phdr_memaddr == 0 || *num_phdr == 0)
6274 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6275 "phdr_memaddr = %ld, phdr_num = %d",
6276 (long) *phdr_memaddr, *num_phdr);
6277 return 2;
6280 return 0;
6283 /* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6285 static CORE_ADDR
6286 get_dynamic (const int pid, const int is_elf64)
6288 CORE_ADDR phdr_memaddr, relocation;
6289 int num_phdr, i;
6290 unsigned char *phdr_buf;
6291 const int phdr_size = is_elf64 ? sizeof (Elf64_Phdr) : sizeof (Elf32_Phdr);
6293 if (get_phdr_phnum_from_proc_auxv (pid, is_elf64, &phdr_memaddr, &num_phdr))
6294 return 0;
6296 gdb_assert (num_phdr < 100); /* Basic sanity check. */
6297 phdr_buf = (unsigned char *) alloca (num_phdr * phdr_size);
6299 if (linux_read_memory (phdr_memaddr, phdr_buf, num_phdr * phdr_size))
6300 return 0;
6302 /* Compute relocation: it is expected to be 0 for "regular" executables,
6303 non-zero for PIE ones. */
6304 relocation = -1;
6305 for (i = 0; relocation == -1 && i < num_phdr; i++)
6306 if (is_elf64)
6308 Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
6310 if (p->p_type == PT_PHDR)
6311 relocation = phdr_memaddr - p->p_vaddr;
6313 else
6315 Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
6317 if (p->p_type == PT_PHDR)
6318 relocation = phdr_memaddr - p->p_vaddr;
6321 if (relocation == -1)
6323 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6324 any real world executables, including PIE executables, have always
6325 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6326 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6327 or present DT_DEBUG anyway (fpc binaries are statically linked).
6329 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6331 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6333 return 0;
6336 for (i = 0; i < num_phdr; i++)
6338 if (is_elf64)
6340 Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
6342 if (p->p_type == PT_DYNAMIC)
6343 return p->p_vaddr + relocation;
6345 else
6347 Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
6349 if (p->p_type == PT_DYNAMIC)
6350 return p->p_vaddr + relocation;
6354 return 0;
6357 /* Return &_r_debug in the inferior, or -1 if not present. Return value
6358 can be 0 if the inferior does not yet have the library list initialized.
6359 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6360 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
6362 static CORE_ADDR
6363 get_r_debug (const int pid, const int is_elf64)
6365 CORE_ADDR dynamic_memaddr;
6366 const int dyn_size = is_elf64 ? sizeof (Elf64_Dyn) : sizeof (Elf32_Dyn);
6367 unsigned char buf[sizeof (Elf64_Dyn)]; /* The larger of the two. */
6368 CORE_ADDR map = -1;
6370 dynamic_memaddr = get_dynamic (pid, is_elf64);
6371 if (dynamic_memaddr == 0)
6372 return map;
6374 while (linux_read_memory (dynamic_memaddr, buf, dyn_size) == 0)
6376 if (is_elf64)
6378 Elf64_Dyn *const dyn = (Elf64_Dyn *) buf;
6379 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6380 union
6382 Elf64_Xword map;
6383 unsigned char buf[sizeof (Elf64_Xword)];
6385 rld_map;
6386 #endif
6387 #ifdef DT_MIPS_RLD_MAP
6388 if (dyn->d_tag == DT_MIPS_RLD_MAP)
6390 if (linux_read_memory (dyn->d_un.d_val,
6391 rld_map.buf, sizeof (rld_map.buf)) == 0)
6392 return rld_map.map;
6393 else
6394 break;
6396 #endif /* DT_MIPS_RLD_MAP */
6397 #ifdef DT_MIPS_RLD_MAP_REL
6398 if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
6400 if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
6401 rld_map.buf, sizeof (rld_map.buf)) == 0)
6402 return rld_map.map;
6403 else
6404 break;
6406 #endif /* DT_MIPS_RLD_MAP_REL */
6408 if (dyn->d_tag == DT_DEBUG && map == -1)
6409 map = dyn->d_un.d_val;
6411 if (dyn->d_tag == DT_NULL)
6412 break;
6414 else
6416 Elf32_Dyn *const dyn = (Elf32_Dyn *) buf;
6417 #if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
6418 union
6420 Elf32_Word map;
6421 unsigned char buf[sizeof (Elf32_Word)];
6423 rld_map;
6424 #endif
6425 #ifdef DT_MIPS_RLD_MAP
6426 if (dyn->d_tag == DT_MIPS_RLD_MAP)
6428 if (linux_read_memory (dyn->d_un.d_val,
6429 rld_map.buf, sizeof (rld_map.buf)) == 0)
6430 return rld_map.map;
6431 else
6432 break;
6434 #endif /* DT_MIPS_RLD_MAP */
6435 #ifdef DT_MIPS_RLD_MAP_REL
6436 if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
6438 if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
6439 rld_map.buf, sizeof (rld_map.buf)) == 0)
6440 return rld_map.map;
6441 else
6442 break;
6444 #endif /* DT_MIPS_RLD_MAP_REL */
6446 if (dyn->d_tag == DT_DEBUG && map == -1)
6447 map = dyn->d_un.d_val;
6449 if (dyn->d_tag == DT_NULL)
6450 break;
6453 dynamic_memaddr += dyn_size;
6456 return map;
6459 /* Read one pointer from MEMADDR in the inferior. */
6461 static int
6462 read_one_ptr (CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size)
6464 int ret;
6466 /* Go through a union so this works on either big or little endian
6467 hosts, when the inferior's pointer size is smaller than the size
6468 of CORE_ADDR. It is assumed the inferior's endianness is the
6469 same of the superior's. */
6470 union
6472 CORE_ADDR core_addr;
6473 unsigned int ui;
6474 unsigned char uc;
6475 } addr;
6477 ret = linux_read_memory (memaddr, &addr.uc, ptr_size);
6478 if (ret == 0)
6480 if (ptr_size == sizeof (CORE_ADDR))
6481 *ptr = addr.core_addr;
6482 else if (ptr_size == sizeof (unsigned int))
6483 *ptr = addr.ui;
6484 else
6485 gdb_assert_not_reached ("unhandled pointer size");
6487 return ret;
6490 bool
6491 linux_process_target::supports_qxfer_libraries_svr4 ()
6493 return true;
6496 struct link_map_offsets
6498 /* Offset and size of r_debug.r_version. */
6499 int r_version_offset;
6501 /* Offset and size of r_debug.r_map. */
6502 int r_map_offset;
6504 /* Offset to l_addr field in struct link_map. */
6505 int l_addr_offset;
6507 /* Offset to l_name field in struct link_map. */
6508 int l_name_offset;
6510 /* Offset to l_ld field in struct link_map. */
6511 int l_ld_offset;
6513 /* Offset to l_next field in struct link_map. */
6514 int l_next_offset;
6516 /* Offset to l_prev field in struct link_map. */
6517 int l_prev_offset;
6520 /* Construct qXfer:libraries-svr4:read reply. */
6523 linux_process_target::qxfer_libraries_svr4 (const char *annex,
6524 unsigned char *readbuf,
6525 unsigned const char *writebuf,
6526 CORE_ADDR offset, int len)
6528 struct process_info_private *const priv = current_process ()->priv;
6529 char filename[PATH_MAX];
6530 int pid, is_elf64;
6532 static const struct link_map_offsets lmo_32bit_offsets =
6534 0, /* r_version offset. */
6535 4, /* r_debug.r_map offset. */
6536 0, /* l_addr offset in link_map. */
6537 4, /* l_name offset in link_map. */
6538 8, /* l_ld offset in link_map. */
6539 12, /* l_next offset in link_map. */
6540 16 /* l_prev offset in link_map. */
6543 static const struct link_map_offsets lmo_64bit_offsets =
6545 0, /* r_version offset. */
6546 8, /* r_debug.r_map offset. */
6547 0, /* l_addr offset in link_map. */
6548 8, /* l_name offset in link_map. */
6549 16, /* l_ld offset in link_map. */
6550 24, /* l_next offset in link_map. */
6551 32 /* l_prev offset in link_map. */
6553 const struct link_map_offsets *lmo;
6554 unsigned int machine;
6555 int ptr_size;
6556 CORE_ADDR lm_addr = 0, lm_prev = 0;
6557 CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
6558 int header_done = 0;
6560 if (writebuf != NULL)
6561 return -2;
6562 if (readbuf == NULL)
6563 return -1;
6565 pid = lwpid_of (current_thread);
6566 xsnprintf (filename, sizeof filename, "/proc/%d/exe", pid);
6567 is_elf64 = elf_64_file_p (filename, &machine);
6568 lmo = is_elf64 ? &lmo_64bit_offsets : &lmo_32bit_offsets;
6569 ptr_size = is_elf64 ? 8 : 4;
6571 while (annex[0] != '\0')
6573 const char *sep;
6574 CORE_ADDR *addrp;
6575 int name_len;
6577 sep = strchr (annex, '=');
6578 if (sep == NULL)
6579 break;
6581 name_len = sep - annex;
6582 if (name_len == 5 && startswith (annex, "start"))
6583 addrp = &lm_addr;
6584 else if (name_len == 4 && startswith (annex, "prev"))
6585 addrp = &lm_prev;
6586 else
6588 annex = strchr (sep, ';');
6589 if (annex == NULL)
6590 break;
6591 annex++;
6592 continue;
6595 annex = decode_address_to_semicolon (addrp, sep + 1);
6598 if (lm_addr == 0)
6600 int r_version = 0;
6602 if (priv->r_debug == 0)
6603 priv->r_debug = get_r_debug (pid, is_elf64);
6605 /* We failed to find DT_DEBUG. Such situation will not change
6606 for this inferior - do not retry it. Report it to GDB as
6607 E01, see for the reasons at the GDB solib-svr4.c side. */
6608 if (priv->r_debug == (CORE_ADDR) -1)
6609 return -1;
6611 if (priv->r_debug != 0)
6613 if (linux_read_memory (priv->r_debug + lmo->r_version_offset,
6614 (unsigned char *) &r_version,
6615 sizeof (r_version)) != 0
6616 || r_version < 1)
6618 warning ("unexpected r_debug version %d", r_version);
6620 else if (read_one_ptr (priv->r_debug + lmo->r_map_offset,
6621 &lm_addr, ptr_size) != 0)
6623 warning ("unable to read r_map from 0x%lx",
6624 (long) priv->r_debug + lmo->r_map_offset);
6629 std::string document = "<library-list-svr4 version=\"1.0\"";
6631 while (lm_addr
6632 && read_one_ptr (lm_addr + lmo->l_name_offset,
6633 &l_name, ptr_size) == 0
6634 && read_one_ptr (lm_addr + lmo->l_addr_offset,
6635 &l_addr, ptr_size) == 0
6636 && read_one_ptr (lm_addr + lmo->l_ld_offset,
6637 &l_ld, ptr_size) == 0
6638 && read_one_ptr (lm_addr + lmo->l_prev_offset,
6639 &l_prev, ptr_size) == 0
6640 && read_one_ptr (lm_addr + lmo->l_next_offset,
6641 &l_next, ptr_size) == 0)
6643 unsigned char libname[PATH_MAX];
6645 if (lm_prev != l_prev)
6647 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
6648 (long) lm_prev, (long) l_prev);
6649 break;
6652 /* Ignore the first entry even if it has valid name as the first entry
6653 corresponds to the main executable. The first entry should not be
6654 skipped if the dynamic loader was loaded late by a static executable
6655 (see solib-svr4.c parameter ignore_first). But in such case the main
6656 executable does not have PT_DYNAMIC present and this function already
6657 exited above due to failed get_r_debug. */
6658 if (lm_prev == 0)
6659 string_appendf (document, " main-lm=\"0x%lx\"", (unsigned long) lm_addr);
6660 else
6662 /* Not checking for error because reading may stop before
6663 we've got PATH_MAX worth of characters. */
6664 libname[0] = '\0';
6665 linux_read_memory (l_name, libname, sizeof (libname) - 1);
6666 libname[sizeof (libname) - 1] = '\0';
6667 if (libname[0] != '\0')
6669 if (!header_done)
6671 /* Terminate `<library-list-svr4'. */
6672 document += '>';
6673 header_done = 1;
6676 string_appendf (document, "<library name=\"");
6677 xml_escape_text_append (&document, (char *) libname);
6678 string_appendf (document, "\" lm=\"0x%lx\" "
6679 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
6680 (unsigned long) lm_addr, (unsigned long) l_addr,
6681 (unsigned long) l_ld);
6685 lm_prev = lm_addr;
6686 lm_addr = l_next;
6689 if (!header_done)
6691 /* Empty list; terminate `<library-list-svr4'. */
6692 document += "/>";
6694 else
6695 document += "</library-list-svr4>";
6697 int document_len = document.length ();
6698 if (offset < document_len)
6699 document_len -= offset;
6700 else
6701 document_len = 0;
6702 if (len > document_len)
6703 len = document_len;
6705 memcpy (readbuf, document.data () + offset, len);
6707 return len;
6710 #ifdef HAVE_LINUX_BTRACE
6712 btrace_target_info *
6713 linux_process_target::enable_btrace (thread_info *tp,
6714 const btrace_config *conf)
6716 return linux_enable_btrace (tp->id, conf);
6719 /* See to_disable_btrace target method. */
6722 linux_process_target::disable_btrace (btrace_target_info *tinfo)
6724 enum btrace_error err;
6726 err = linux_disable_btrace (tinfo);
6727 return (err == BTRACE_ERR_NONE ? 0 : -1);
6730 /* Encode an Intel Processor Trace configuration. */
6732 static void
6733 linux_low_encode_pt_config (struct buffer *buffer,
6734 const struct btrace_data_pt_config *config)
6736 buffer_grow_str (buffer, "<pt-config>\n");
6738 switch (config->cpu.vendor)
6740 case CV_INTEL:
6741 buffer_xml_printf (buffer, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
6742 "model=\"%u\" stepping=\"%u\"/>\n",
6743 config->cpu.family, config->cpu.model,
6744 config->cpu.stepping);
6745 break;
6747 default:
6748 break;
6751 buffer_grow_str (buffer, "</pt-config>\n");
6754 /* Encode a raw buffer. */
6756 static void
6757 linux_low_encode_raw (struct buffer *buffer, const gdb_byte *data,
6758 unsigned int size)
6760 if (size == 0)
6761 return;
6763 /* We use hex encoding - see gdbsupport/rsp-low.h. */
6764 buffer_grow_str (buffer, "<raw>\n");
6766 while (size-- > 0)
6768 char elem[2];
6770 elem[0] = tohex ((*data >> 4) & 0xf);
6771 elem[1] = tohex (*data++ & 0xf);
6773 buffer_grow (buffer, elem, 2);
6776 buffer_grow_str (buffer, "</raw>\n");
6779 /* See to_read_btrace target method. */
6782 linux_process_target::read_btrace (btrace_target_info *tinfo,
6783 buffer *buffer,
6784 enum btrace_read_type type)
6786 struct btrace_data btrace;
6787 enum btrace_error err;
6789 err = linux_read_btrace (&btrace, tinfo, type);
6790 if (err != BTRACE_ERR_NONE)
6792 if (err == BTRACE_ERR_OVERFLOW)
6793 buffer_grow_str0 (buffer, "E.Overflow.");
6794 else
6795 buffer_grow_str0 (buffer, "E.Generic Error.");
6797 return -1;
6800 switch (btrace.format)
6802 case BTRACE_FORMAT_NONE:
6803 buffer_grow_str0 (buffer, "E.No Trace.");
6804 return -1;
6806 case BTRACE_FORMAT_BTS:
6807 buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
6808 buffer_grow_str (buffer, "<btrace version=\"1.0\">\n");
6810 for (const btrace_block &block : *btrace.variant.bts.blocks)
6811 buffer_xml_printf (buffer, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
6812 paddress (block.begin), paddress (block.end));
6814 buffer_grow_str0 (buffer, "</btrace>\n");
6815 break;
6817 case BTRACE_FORMAT_PT:
6818 buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
6819 buffer_grow_str (buffer, "<btrace version=\"1.0\">\n");
6820 buffer_grow_str (buffer, "<pt>\n");
6822 linux_low_encode_pt_config (buffer, &btrace.variant.pt.config);
6824 linux_low_encode_raw (buffer, btrace.variant.pt.data,
6825 btrace.variant.pt.size);
6827 buffer_grow_str (buffer, "</pt>\n");
6828 buffer_grow_str0 (buffer, "</btrace>\n");
6829 break;
6831 default:
6832 buffer_grow_str0 (buffer, "E.Unsupported Trace Format.");
6833 return -1;
6836 return 0;
6839 /* See to_btrace_conf target method. */
6842 linux_process_target::read_btrace_conf (const btrace_target_info *tinfo,
6843 buffer *buffer)
6845 const struct btrace_config *conf;
6847 buffer_grow_str (buffer, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
6848 buffer_grow_str (buffer, "<btrace-conf version=\"1.0\">\n");
6850 conf = linux_btrace_conf (tinfo);
6851 if (conf != NULL)
6853 switch (conf->format)
6855 case BTRACE_FORMAT_NONE:
6856 break;
6858 case BTRACE_FORMAT_BTS:
6859 buffer_xml_printf (buffer, "<bts");
6860 buffer_xml_printf (buffer, " size=\"0x%x\"", conf->bts.size);
6861 buffer_xml_printf (buffer, " />\n");
6862 break;
6864 case BTRACE_FORMAT_PT:
6865 buffer_xml_printf (buffer, "<pt");
6866 buffer_xml_printf (buffer, " size=\"0x%x\"", conf->pt.size);
6867 buffer_xml_printf (buffer, "/>\n");
6868 break;
6872 buffer_grow_str0 (buffer, "</btrace-conf>\n");
6873 return 0;
6875 #endif /* HAVE_LINUX_BTRACE */
6877 /* See nat/linux-nat.h. */
6879 ptid_t
6880 current_lwp_ptid (void)
6882 return ptid_of (current_thread);
6885 const char *
6886 linux_process_target::thread_name (ptid_t thread)
6888 return linux_proc_tid_get_name (thread);
6891 #if USE_THREAD_DB
6892 bool
6893 linux_process_target::thread_handle (ptid_t ptid, gdb_byte **handle,
6894 int *handle_len)
6896 return thread_db_thread_handle (ptid, handle, handle_len);
6898 #endif
6900 thread_info *
6901 linux_process_target::thread_pending_parent (thread_info *thread)
6903 lwp_info *parent = get_thread_lwp (thread)->pending_parent ();
6905 if (parent == nullptr)
6906 return nullptr;
6908 return get_lwp_thread (parent);
6911 thread_info *
6912 linux_process_target::thread_pending_child (thread_info *thread)
6914 lwp_info *child = get_thread_lwp (thread)->pending_child ();
6916 if (child == nullptr)
6917 return nullptr;
6919 return get_lwp_thread (child);
6922 /* Default implementation of linux_target_ops method "set_pc" for
6923 32-bit pc register which is literally named "pc". */
6925 void
6926 linux_set_pc_32bit (struct regcache *regcache, CORE_ADDR pc)
6928 uint32_t newpc = pc;
6930 supply_register_by_name (regcache, "pc", &newpc);
6933 /* Default implementation of linux_target_ops method "get_pc" for
6934 32-bit pc register which is literally named "pc". */
6936 CORE_ADDR
6937 linux_get_pc_32bit (struct regcache *regcache)
6939 uint32_t pc;
6941 collect_register_by_name (regcache, "pc", &pc);
6942 threads_debug_printf ("stop pc is 0x%" PRIx32, pc);
6943 return pc;
6946 /* Default implementation of linux_target_ops method "set_pc" for
6947 64-bit pc register which is literally named "pc". */
6949 void
6950 linux_set_pc_64bit (struct regcache *regcache, CORE_ADDR pc)
6952 uint64_t newpc = pc;
6954 supply_register_by_name (regcache, "pc", &newpc);
6957 /* Default implementation of linux_target_ops method "get_pc" for
6958 64-bit pc register which is literally named "pc". */
6960 CORE_ADDR
6961 linux_get_pc_64bit (struct regcache *regcache)
6963 uint64_t pc;
6965 collect_register_by_name (regcache, "pc", &pc);
6966 threads_debug_printf ("stop pc is 0x%" PRIx64, pc);
6967 return pc;
6970 /* See linux-low.h. */
6973 linux_get_auxv (int wordsize, CORE_ADDR match, CORE_ADDR *valp)
6975 gdb_byte *data = (gdb_byte *) alloca (2 * wordsize);
6976 int offset = 0;
6978 gdb_assert (wordsize == 4 || wordsize == 8);
6980 while (the_target->read_auxv (offset, data, 2 * wordsize) == 2 * wordsize)
6982 if (wordsize == 4)
6984 uint32_t *data_p = (uint32_t *) data;
6985 if (data_p[0] == match)
6987 *valp = data_p[1];
6988 return 1;
6991 else
6993 uint64_t *data_p = (uint64_t *) data;
6994 if (data_p[0] == match)
6996 *valp = data_p[1];
6997 return 1;
7001 offset += 2 * wordsize;
7004 return 0;
7007 /* See linux-low.h. */
7009 CORE_ADDR
7010 linux_get_hwcap (int wordsize)
7012 CORE_ADDR hwcap = 0;
7013 linux_get_auxv (wordsize, AT_HWCAP, &hwcap);
7014 return hwcap;
7017 /* See linux-low.h. */
7019 CORE_ADDR
7020 linux_get_hwcap2 (int wordsize)
7022 CORE_ADDR hwcap2 = 0;
7023 linux_get_auxv (wordsize, AT_HWCAP2, &hwcap2);
7024 return hwcap2;
7027 #ifdef HAVE_LINUX_REGSETS
7028 void
7029 initialize_regsets_info (struct regsets_info *info)
7031 for (info->num_regsets = 0;
7032 info->regsets[info->num_regsets].size >= 0;
7033 info->num_regsets++)
7036 #endif
7038 void
7039 initialize_low (void)
7041 struct sigaction sigchld_action;
7043 memset (&sigchld_action, 0, sizeof (sigchld_action));
7044 set_target_ops (the_linux_target);
7046 linux_ptrace_init_warnings ();
7047 linux_proc_init_warnings ();
7049 sigchld_action.sa_handler = sigchld_handler;
7050 sigemptyset (&sigchld_action.sa_mask);
7051 sigchld_action.sa_flags = SA_RESTART;
7052 sigaction (SIGCHLD, &sigchld_action, NULL);
7054 initialize_low_arch ();
7056 linux_check_ptrace_features ();