]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbserver/linux-low.cc
Fix deletion of FinishBreakpoints
[thirdparty/binutils-gdb.git] / gdbserver / linux-low.cc
CommitLineData
da6d8c04 1/* Low level interface to ptrace, for the remote server for GDB.
4a94e368 2 Copyright (C) 1995-2022 Free Software Foundation, Inc.
da6d8c04
DJ
3
4 This file is part of GDB.
5
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
da6d8c04
DJ
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
da6d8c04
DJ
18
19#include "server.h"
58caa3dc 20#include "linux-low.h"
125f8a3d 21#include "nat/linux-osdata.h"
268a13a5 22#include "gdbsupport/agent.h"
de0d863e 23#include "tdesc.h"
cdc8e9b2
JB
24#include "gdbsupport/event-loop.h"
25#include "gdbsupport/event-pipe.h"
268a13a5
TT
26#include "gdbsupport/rsp-low.h"
27#include "gdbsupport/signals-state-save-restore.h"
96d7229d
LM
28#include "nat/linux-nat.h"
29#include "nat/linux-waitpid.h"
268a13a5 30#include "gdbsupport/gdb_wait.h"
5826e159 31#include "nat/gdb_ptrace.h"
125f8a3d
GB
32#include "nat/linux-ptrace.h"
33#include "nat/linux-procfs.h"
8cc73a39 34#include "nat/linux-personality.h"
da6d8c04
DJ
35#include <signal.h>
36#include <sys/ioctl.h>
37#include <fcntl.h>
0a30fbc4 38#include <unistd.h>
fd500816 39#include <sys/syscall.h>
f9387fc3 40#include <sched.h>
07e059b5
VP
41#include <ctype.h>
42#include <pwd.h>
43#include <sys/types.h>
44#include <dirent.h>
53ce3c39 45#include <sys/stat.h>
efcbbd14 46#include <sys/vfs.h>
1570b33e 47#include <sys/uio.h>
268a13a5 48#include "gdbsupport/filestuff.h"
c144c7a0 49#include "tracepoint.h"
276d4552 50#include <inttypes.h>
268a13a5 51#include "gdbsupport/common-inferior.h"
2090129c 52#include "nat/fork-inferior.h"
268a13a5 53#include "gdbsupport/environ.h"
21987b9c 54#include "gdbsupport/gdb-sigmask.h"
268a13a5 55#include "gdbsupport/scoped_restore.h"
957f3f49
DE
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
14d2069a 63#include "nat/linux-namespaces.h"
efcbbd14 64
fd462a61
DJ
65#ifndef O_LARGEFILE
66#define O_LARGEFILE 0
67#endif
1a981360 68
69f4c9cc
AH
69#ifndef AT_HWCAP2
70#define AT_HWCAP2 26
71#endif
72
db0dfaa0
LM
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
db0dfaa0
LM
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
91
5203ae1e
TBA
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
99
9accd112 100#ifdef HAVE_LINUX_BTRACE
125f8a3d 101# include "nat/linux-btrace.h"
268a13a5 102# include "gdbsupport/btrace-common.h"
9accd112
MM
103#endif
104
8365dcf5
TJB
105#ifndef HAVE_ELF32_AUXV_T
106/* Copied from glibc's elf.h. */
107typedef struct
108{
109 uint32_t a_type; /* Entry type */
110 union
111 {
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
119
120#ifndef HAVE_ELF64_AUXV_T
121/* Copied from glibc's elf.h. */
122typedef struct
123{
124 uint64_t a_type; /* Entry type */
125 union
126 {
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
134
ded48a5e
YQ
135/* Does the current host support PTRACE_GETREGSET? */
136int have_ptrace_getregset = -1;
137
8a841a35
PA
138/* Return TRUE if THREAD is the leader thread of the process. */
139
140static bool
141is_leader (thread_info *thread)
142{
143 ptid_t ptid = ptid_of (thread);
144 return ptid.pid () == ptid.lwp ();
145}
146
cff068da
GB
147/* LWP accessors. */
148
149/* See nat/linux-nat.h. */
150
151ptid_t
152ptid_of_lwp (struct lwp_info *lwp)
153{
154 return ptid_of (get_lwp_thread (lwp));
155}
156
157/* See nat/linux-nat.h. */
158
4b134ca1
GB
159void
160lwp_set_arch_private_info (struct lwp_info *lwp,
161 struct arch_lwp_info *info)
162{
163 lwp->arch_private = info;
164}
165
166/* See nat/linux-nat.h. */
167
168struct arch_lwp_info *
169lwp_arch_private_info (struct lwp_info *lwp)
170{
171 return lwp->arch_private;
172}
173
174/* See nat/linux-nat.h. */
175
cff068da
GB
176int
177lwp_is_stopped (struct lwp_info *lwp)
178{
179 return lwp->stopped;
180}
181
182/* See nat/linux-nat.h. */
183
184enum target_stop_reason
185lwp_stop_reason (struct lwp_info *lwp)
186{
187 return lwp->stop_reason;
188}
189
0e00e962
AA
190/* See nat/linux-nat.h. */
191
192int
193lwp_is_stepping (struct lwp_info *lwp)
194{
195 return lwp->stepping;
196}
197
05044653
PA
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. */
24a09b5f 201
05044653
PA
202struct simple_pid_list
203{
204 /* The process ID. */
205 int pid;
206
207 /* The status as reported by waitpid. */
208 int status;
209
210 /* Next in chain. */
211 struct simple_pid_list *next;
212};
05c309a8 213static struct simple_pid_list *stopped_pids;
05044653
PA
214
215/* Trivial list manipulation functions to keep track of a list of new
216 stopped processes. */
217
218static void
219add_to_pid_list (struct simple_pid_list **listp, int pid, int status)
220{
8d749320 221 struct simple_pid_list *new_pid = XNEW (struct simple_pid_list);
05044653
PA
222
223 new_pid->pid = pid;
224 new_pid->status = status;
225 new_pid->next = *listp;
226 *listp = new_pid;
227}
228
229static int
230pull_pid_from_list (struct simple_pid_list **listp, int pid, int *statusp)
231{
232 struct simple_pid_list **p;
233
234 for (p = listp; *p != NULL; p = &(*p)->next)
235 if ((*p)->pid == pid)
236 {
237 struct simple_pid_list *next = (*p)->next;
238
239 *statusp = (*p)->status;
240 xfree (*p);
241 *p = next;
242 return 1;
243 }
244 return 0;
245}
24a09b5f 246
bde24c0a
PA
247enum stopping_threads_kind
248 {
249 /* Not stopping threads presently. */
250 NOT_STOPPING_THREADS,
251
252 /* Stopping threads. */
253 STOPPING_THREADS,
254
255 /* Stopping and suspending threads. */
256 STOPPING_AND_SUSPENDING_THREADS
257 };
258
259/* This is set while stop_all_lwps is in effect. */
6bd434d6 260static stopping_threads_kind stopping_threads = NOT_STOPPING_THREADS;
0d62e5e8
DJ
261
262/* FIXME make into a target method? */
24a09b5f 263int using_threads = 1;
24a09b5f 264
fa593d66
PA
265/* True if we're presently stabilizing threads (moving them out of
266 jump pads). */
267static int stabilizing_threads;
268
f50bf8e5 269static void unsuspend_all_lwps (struct lwp_info *except);
95954743 270static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
00db26fa 271static int lwp_is_marked_dead (struct lwp_info *lwp);
d50171e4 272static int kill_lwp (unsigned long lwpid, int signo);
863d01bd 273static void enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info);
ece66d65 274static int linux_low_ptrace_options (int attached);
ced2dffb 275static int check_ptrace_stopped_lwp_gone (struct lwp_info *lp);
d50171e4 276
582511be
PA
277/* When the event-loop is doing a step-over, this points at the thread
278 being stepped. */
6bd434d6 279static ptid_t step_over_bkpt;
582511be 280
bf9ae9d8
TBA
281bool
282linux_process_target::low_supports_breakpoints ()
283{
284 return false;
285}
d50171e4 286
bf9ae9d8
TBA
287CORE_ADDR
288linux_process_target::low_get_pc (regcache *regcache)
289{
290 return 0;
291}
292
293void
294linux_process_target::low_set_pc (regcache *regcache, CORE_ADDR newpc)
d50171e4 295{
bf9ae9d8 296 gdb_assert_not_reached ("linux target op low_set_pc is not implemented");
d50171e4 297}
0d62e5e8 298
7582c77c
TBA
299std::vector<CORE_ADDR>
300linux_process_target::low_get_next_pcs (regcache *regcache)
301{
302 gdb_assert_not_reached ("linux target op low_get_next_pcs is not "
303 "implemented");
304}
305
d4807ea2
TBA
306int
307linux_process_target::low_decr_pc_after_break ()
308{
309 return 0;
310}
311
c2d6af84
PA
312/* True if LWP is stopped in its stepping range. */
313
314static int
315lwp_in_step_range (struct lwp_info *lwp)
316{
317 CORE_ADDR pc = lwp->stop_pc;
318
319 return (pc >= lwp->step_range_start && pc < lwp->step_range_end);
320}
321
cdc8e9b2
JB
322/* The event pipe registered as a waitable file in the event loop. */
323static event_pipe linux_event_pipe;
bd99dc85
PA
324
325/* True if we're currently in async mode. */
cdc8e9b2 326#define target_is_async_p() (linux_event_pipe.is_open ())
bd99dc85 327
02fc4de7 328static void send_sigstop (struct lwp_info *lwp);
bd99dc85 329
d0722149
DE
330/* Return non-zero if HEADER is a 64-bit ELF file. */
331
332static int
214d508e 333elf_64_header_p (const Elf64_Ehdr *header, unsigned int *machine)
d0722149 334{
214d508e
L
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)
339 {
340 *machine = header->e_machine;
341 return header->e_ident[EI_CLASS] == ELFCLASS64;
342
343 }
344 *machine = EM_NONE;
345 return -1;
d0722149
DE
346}
347
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. */
351
be07f1a2 352static int
214d508e 353elf_64_file_p (const char *file, unsigned int *machine)
d0722149 354{
957f3f49 355 Elf64_Ehdr header;
d0722149
DE
356 int fd;
357
358 fd = open (file, O_RDONLY);
359 if (fd < 0)
360 return -1;
361
362 if (read (fd, &header, sizeof (header)) != sizeof (header))
363 {
364 close (fd);
365 return 0;
366 }
367 close (fd);
368
214d508e 369 return elf_64_header_p (&header, machine);
d0722149
DE
370}
371
be07f1a2
PA
372/* Accepts an integer PID; Returns true if the executable PID is
373 running is a 64-bit ELF file.. */
374
375int
214d508e 376linux_pid_exe_is_elf_64_file (int pid, unsigned int *machine)
be07f1a2 377{
d8d2a3ee 378 char file[PATH_MAX];
be07f1a2
PA
379
380 sprintf (file, "/proc/%d/exe", pid);
214d508e 381 return elf_64_file_p (file, machine);
be07f1a2
PA
382}
383
fd000fb3
TBA
384void
385linux_process_target::delete_lwp (lwp_info *lwp)
bd99dc85 386{
fa96cb38
PA
387 struct thread_info *thr = get_lwp_thread (lwp);
388
c058728c 389 threads_debug_printf ("deleting %ld", lwpid_of (thr));
fa96cb38
PA
390
391 remove_thread (thr);
466eecee 392
fd000fb3 393 low_delete_thread (lwp->arch_private);
466eecee 394
013e3554 395 delete lwp;
bd99dc85
PA
396}
397
fd000fb3
TBA
398void
399linux_process_target::low_delete_thread (arch_lwp_info *info)
400{
401 /* Default implementation should be overridden if architecture-specific
402 info is being used. */
403 gdb_assert (info == nullptr);
404}
95954743 405
421490af
PA
406/* Open the /proc/PID/mem file for PROC. */
407
408static void
409open_proc_mem_file (process_info *proc)
410{
411 gdb_assert (proc->priv->mem_fd == -1);
412
413 char filename[64];
414 xsnprintf (filename, sizeof filename, "/proc/%d/mem", proc->pid);
415
416 proc->priv->mem_fd
417 = gdb_open_cloexec (filename, O_RDWR | O_LARGEFILE, 0).release ();
418}
419
fd000fb3 420process_info *
421490af 421linux_process_target::add_linux_process_no_mem_file (int pid, int attached)
95954743
PA
422{
423 struct process_info *proc;
424
95954743 425 proc = add_process (pid, attached);
8d749320 426 proc->priv = XCNEW (struct process_info_private);
95954743 427
fd000fb3 428 proc->priv->arch_private = low_new_process ();
421490af
PA
429 proc->priv->mem_fd = -1;
430
431 return proc;
432}
433
aa5ca48f 434
421490af
PA
435process_info *
436linux_process_target::add_linux_process (int pid, int attached)
437{
438 process_info *proc = add_linux_process_no_mem_file (pid, attached);
439 open_proc_mem_file (proc);
95954743
PA
440 return proc;
441}
442
f551c8ef
SM
443void
444linux_process_target::remove_linux_process (process_info *proc)
445{
446 if (proc->priv->mem_fd >= 0)
447 close (proc->priv->mem_fd);
448
449 this->low_delete_process (proc->priv->arch_private);
450
451 xfree (proc->priv);
452 proc->priv = nullptr;
453
454 remove_process (proc);
455}
456
fd000fb3
TBA
457arch_process_info *
458linux_process_target::low_new_process ()
459{
460 return nullptr;
461}
462
463void
464linux_process_target::low_delete_process (arch_process_info *info)
465{
466 /* Default implementation must be overridden if architecture-specific
467 info exists. */
468 gdb_assert (info == nullptr);
469}
470
471void
472linux_process_target::low_new_fork (process_info *parent, process_info *child)
473{
474 /* Nop. */
475}
476
797bcff5
TBA
477void
478linux_process_target::arch_setup_thread (thread_info *thread)
94585166 479{
24583e45
TBA
480 scoped_restore_current_thread restore_thread;
481 switch_to_thread (thread);
94585166 482
797bcff5 483 low_arch_setup ();
94585166
DB
484}
485
d16f3f6c
TBA
486int
487linux_process_target::handle_extended_wait (lwp_info **orig_event_lwp,
488 int wstat)
24a09b5f 489{
c12a5089 490 client_state &cs = get_client_state ();
94585166 491 struct lwp_info *event_lwp = *orig_event_lwp;
89a5711c 492 int event = linux_ptrace_get_extended_event (wstat);
de0d863e 493 struct thread_info *event_thr = get_lwp_thread (event_lwp);
54a0b537 494 struct lwp_info *new_lwp;
24a09b5f 495
183be222 496 gdb_assert (event_lwp->waitstatus.kind () == TARGET_WAITKIND_IGNORE);
65706a29 497
82075af2
JS
498 /* All extended events we currently use are mid-syscall. Only
499 PTRACE_EVENT_STOP is delivered more like a signal-stop, but
500 you have to be using PTRACE_SEIZE to get that. */
501 event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
502
c269dbdb
DB
503 if ((event == PTRACE_EVENT_FORK) || (event == PTRACE_EVENT_VFORK)
504 || (event == PTRACE_EVENT_CLONE))
24a09b5f 505 {
95954743 506 ptid_t ptid;
24a09b5f 507 unsigned long new_pid;
05044653 508 int ret, status;
24a09b5f 509
de0d863e 510 /* Get the pid of the new lwp. */
d86d4aaf 511 ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_thr), (PTRACE_TYPE_ARG3) 0,
56f7af9c 512 &new_pid);
24a09b5f
DJ
513
514 /* If we haven't already seen the new PID stop, wait for it now. */
05044653 515 if (!pull_pid_from_list (&stopped_pids, new_pid, &status))
24a09b5f
DJ
516 {
517 /* The new child has a pending SIGSTOP. We can't affect it until it
518 hits the SIGSTOP, but we're already attached. */
519
97438e3f 520 ret = my_waitpid (new_pid, &status, __WALL);
24a09b5f
DJ
521
522 if (ret == -1)
523 perror_with_name ("waiting for new child");
524 else if (ret != new_pid)
525 warning ("wait returned unexpected PID %d", ret);
da5898ce 526 else if (!WIFSTOPPED (status))
24a09b5f
DJ
527 warning ("wait returned unexpected status 0x%x", status);
528 }
529
c269dbdb 530 if (event == PTRACE_EVENT_FORK || event == PTRACE_EVENT_VFORK)
de0d863e
DB
531 {
532 struct process_info *parent_proc;
533 struct process_info *child_proc;
534 struct lwp_info *child_lwp;
bfacd19d 535 struct thread_info *child_thr;
de0d863e 536
184ea2f7 537 ptid = ptid_t (new_pid, new_pid);
de0d863e 538
c058728c
SM
539 threads_debug_printf ("Got fork event from LWP %ld, "
540 "new child is %d",
541 ptid_of (event_thr).lwp (),
542 ptid.pid ());
de0d863e
DB
543
544 /* Add the new process to the tables and clone the breakpoint
545 lists of the parent. We need to do this even if the new process
546 will be detached, since we will need the process object and the
547 breakpoints to remove any breakpoints from memory when we
548 detach, and the client side will access registers. */
fd000fb3 549 child_proc = add_linux_process (new_pid, 0);
de0d863e
DB
550 gdb_assert (child_proc != NULL);
551 child_lwp = add_lwp (ptid);
552 gdb_assert (child_lwp != NULL);
553 child_lwp->stopped = 1;
bfacd19d
DB
554 child_lwp->must_set_ptrace_flags = 1;
555 child_lwp->status_pending_p = 0;
556 child_thr = get_lwp_thread (child_lwp);
557 child_thr->last_resume_kind = resume_stop;
183be222 558 child_thr->last_status.set_stopped (GDB_SIGNAL_0);
998d452a 559
863d01bd 560 /* If we're suspending all threads, leave this one suspended
0f8288ae
YQ
561 too. If the fork/clone parent is stepping over a breakpoint,
562 all other threads have been suspended already. Leave the
563 child suspended too. */
564 if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
565 || event_lwp->bp_reinsert != 0)
863d01bd 566 {
c058728c 567 threads_debug_printf ("leaving child suspended");
863d01bd
PA
568 child_lwp->suspended = 1;
569 }
570
de0d863e
DB
571 parent_proc = get_thread_process (event_thr);
572 child_proc->attached = parent_proc->attached;
2e7b624b
YQ
573
574 if (event_lwp->bp_reinsert != 0
7582c77c 575 && supports_software_single_step ()
2e7b624b
YQ
576 && event == PTRACE_EVENT_VFORK)
577 {
3b9a79ef
YQ
578 /* If we leave single-step breakpoints there, child will
579 hit it, so uninsert single-step breakpoints from parent
2e7b624b
YQ
580 (and child). Once vfork child is done, reinsert
581 them back to parent. */
3b9a79ef 582 uninsert_single_step_breakpoints (event_thr);
2e7b624b
YQ
583 }
584
63c40ec7 585 clone_all_breakpoints (child_thr, event_thr);
de0d863e 586
51a948fd
AB
587 target_desc_up tdesc = allocate_target_description ();
588 copy_target_description (tdesc.get (), parent_proc->tdesc);
589 child_proc->tdesc = tdesc.release ();
de0d863e 590
3a8a0396 591 /* Clone arch-specific process data. */
fd000fb3 592 low_new_fork (parent_proc, child_proc);
3a8a0396 593
de0d863e 594 /* Save fork info in the parent thread. */
c269dbdb 595 if (event == PTRACE_EVENT_FORK)
183be222 596 event_lwp->waitstatus.set_forked (ptid);
c269dbdb 597 else if (event == PTRACE_EVENT_VFORK)
183be222 598 event_lwp->waitstatus.set_vforked (ptid);
c269dbdb 599
de0d863e
DB
600 /* The status_pending field contains bits denoting the
601 extended event, so when the pending event is handled,
602 the handler will look at lwp->waitstatus. */
603 event_lwp->status_pending_p = 1;
604 event_lwp->status_pending = wstat;
605
5a04c4cf
PA
606 /* Link the threads until the parent event is passed on to
607 higher layers. */
608 event_lwp->fork_relative = child_lwp;
609 child_lwp->fork_relative = event_lwp;
610
3b9a79ef
YQ
611 /* If the parent thread is doing step-over with single-step
612 breakpoints, the list of single-step breakpoints are cloned
2e7b624b
YQ
613 from the parent's. Remove them from the child process.
614 In case of vfork, we'll reinsert them back once vforked
615 child is done. */
8a81c5d7 616 if (event_lwp->bp_reinsert != 0
7582c77c 617 && supports_software_single_step ())
8a81c5d7 618 {
8a81c5d7
YQ
619 /* The child process is forked and stopped, so it is safe
620 to access its memory without stopping all other threads
621 from other processes. */
3b9a79ef 622 delete_single_step_breakpoints (child_thr);
8a81c5d7 623
3b9a79ef
YQ
624 gdb_assert (has_single_step_breakpoints (event_thr));
625 gdb_assert (!has_single_step_breakpoints (child_thr));
8a81c5d7
YQ
626 }
627
de0d863e
DB
628 /* Report the event. */
629 return 0;
630 }
631
c058728c
SM
632 threads_debug_printf
633 ("Got clone event from LWP %ld, new child is LWP %ld",
634 lwpid_of (event_thr), new_pid);
fa96cb38 635
184ea2f7 636 ptid = ptid_t (pid_of (event_thr), new_pid);
b3312d80 637 new_lwp = add_lwp (ptid);
24a09b5f 638
e27d73f6 639 /* Either we're going to immediately resume the new thread
df95181f 640 or leave it stopped. resume_one_lwp is a nop if it
e27d73f6 641 thinks the thread is currently running, so set this first
df95181f 642 before calling resume_one_lwp. */
e27d73f6
DE
643 new_lwp->stopped = 1;
644
0f8288ae
YQ
645 /* If we're suspending all threads, leave this one suspended
646 too. If the fork/clone parent is stepping over a breakpoint,
647 all other threads have been suspended already. Leave the
648 child suspended too. */
649 if (stopping_threads == STOPPING_AND_SUSPENDING_THREADS
650 || event_lwp->bp_reinsert != 0)
bde24c0a
PA
651 new_lwp->suspended = 1;
652
da5898ce
DJ
653 /* Normally we will get the pending SIGSTOP. But in some cases
654 we might get another signal delivered to the group first.
f21cc1a2 655 If we do get another signal, be sure not to lose it. */
20ba1ce6 656 if (WSTOPSIG (status) != SIGSTOP)
da5898ce 657 {
54a0b537 658 new_lwp->stop_expected = 1;
20ba1ce6
PA
659 new_lwp->status_pending_p = 1;
660 new_lwp->status_pending = status;
da5898ce 661 }
c12a5089 662 else if (cs.report_thread_events)
65706a29 663 {
183be222 664 new_lwp->waitstatus.set_thread_created ();
65706a29
PA
665 new_lwp->status_pending_p = 1;
666 new_lwp->status_pending = status;
667 }
de0d863e 668
a0aad537 669#ifdef USE_THREAD_DB
94c207e0 670 thread_db_notice_clone (event_thr, ptid);
a0aad537 671#endif
86299109 672
de0d863e
DB
673 /* Don't report the event. */
674 return 1;
24a09b5f 675 }
c269dbdb
DB
676 else if (event == PTRACE_EVENT_VFORK_DONE)
677 {
183be222 678 event_lwp->waitstatus.set_vfork_done ();
c269dbdb 679
7582c77c 680 if (event_lwp->bp_reinsert != 0 && supports_software_single_step ())
2e7b624b 681 {
3b9a79ef 682 reinsert_single_step_breakpoints (event_thr);
2e7b624b 683
3b9a79ef 684 gdb_assert (has_single_step_breakpoints (event_thr));
2e7b624b
YQ
685 }
686
c269dbdb
DB
687 /* Report the event. */
688 return 0;
689 }
c12a5089 690 else if (event == PTRACE_EVENT_EXEC && cs.report_exec_events)
94585166
DB
691 {
692 struct process_info *proc;
f27866ba 693 std::vector<int> syscalls_to_catch;
94585166
DB
694 ptid_t event_ptid;
695 pid_t event_pid;
696
c058728c
SM
697 threads_debug_printf ("Got exec event from LWP %ld",
698 lwpid_of (event_thr));
94585166
DB
699
700 /* Get the event ptid. */
701 event_ptid = ptid_of (event_thr);
e99b03dc 702 event_pid = event_ptid.pid ();
94585166 703
82075af2 704 /* Save the syscall list from the execing process. */
94585166 705 proc = get_thread_process (event_thr);
f27866ba 706 syscalls_to_catch = std::move (proc->syscalls_to_catch);
82075af2
JS
707
708 /* Delete the execing process and all its threads. */
d16f3f6c 709 mourn (proc);
24583e45 710 switch_to_thread (nullptr);
94585166
DB
711
712 /* Create a new process/lwp/thread. */
fd000fb3 713 proc = add_linux_process (event_pid, 0);
94585166
DB
714 event_lwp = add_lwp (event_ptid);
715 event_thr = get_lwp_thread (event_lwp);
716 gdb_assert (current_thread == event_thr);
797bcff5 717 arch_setup_thread (event_thr);
94585166
DB
718
719 /* Set the event status. */
183be222
SM
720 event_lwp->waitstatus.set_execd
721 (make_unique_xstrdup
722 (linux_proc_pid_to_exec_file (lwpid_of (event_thr))));
94585166
DB
723
724 /* Mark the exec status as pending. */
725 event_lwp->stopped = 1;
726 event_lwp->status_pending_p = 1;
727 event_lwp->status_pending = wstat;
728 event_thr->last_resume_kind = resume_continue;
183be222 729 event_thr->last_status.set_ignore ();
94585166 730
82075af2
JS
731 /* Update syscall state in the new lwp, effectively mid-syscall too. */
732 event_lwp->syscall_state = TARGET_WAITKIND_SYSCALL_ENTRY;
733
734 /* Restore the list to catch. Don't rely on the client, which is free
735 to avoid sending a new list when the architecture doesn't change.
736 Also, for ANY_SYSCALL, the architecture doesn't really matter. */
f27866ba 737 proc->syscalls_to_catch = std::move (syscalls_to_catch);
82075af2 738
94585166
DB
739 /* Report the event. */
740 *orig_event_lwp = event_lwp;
741 return 0;
742 }
de0d863e 743
f34652de 744 internal_error (_("unknown ptrace event %d"), event);
24a09b5f
DJ
745}
746
df95181f
TBA
747CORE_ADDR
748linux_process_target::get_pc (lwp_info *lwp)
d50171e4 749{
a9deee17
PA
750 process_info *proc = get_thread_process (get_lwp_thread (lwp));
751 gdb_assert (!proc->starting_up);
d50171e4 752
bf9ae9d8 753 if (!low_supports_breakpoints ())
d50171e4
PA
754 return 0;
755
24583e45
TBA
756 scoped_restore_current_thread restore_thread;
757 switch_to_thread (get_lwp_thread (lwp));
d50171e4 758
a9deee17
PA
759 struct regcache *regcache = get_thread_regcache (current_thread, 1);
760 CORE_ADDR pc = low_get_pc (regcache);
d50171e4 761
c058728c 762 threads_debug_printf ("pc is 0x%lx", (long) pc);
d50171e4 763
d50171e4
PA
764 return pc;
765}
766
9eedd27d
TBA
767void
768linux_process_target::get_syscall_trapinfo (lwp_info *lwp, int *sysno)
82075af2 769{
82075af2
JS
770 struct regcache *regcache;
771
24583e45
TBA
772 scoped_restore_current_thread restore_thread;
773 switch_to_thread (get_lwp_thread (lwp));
82075af2
JS
774
775 regcache = get_thread_regcache (current_thread, 1);
9eedd27d 776 low_get_syscall_trapinfo (regcache, sysno);
82075af2 777
c058728c 778 threads_debug_printf ("get_syscall_trapinfo sysno %d", *sysno);
82075af2
JS
779}
780
9eedd27d
TBA
781void
782linux_process_target::low_get_syscall_trapinfo (regcache *regcache, int *sysno)
783{
784 /* By default, report an unknown system call number. */
785 *sysno = UNKNOWN_SYSCALL;
786}
787
df95181f
TBA
788bool
789linux_process_target::save_stop_reason (lwp_info *lwp)
0d62e5e8 790{
582511be
PA
791 CORE_ADDR pc;
792 CORE_ADDR sw_breakpoint_pc;
3e572f71
PA
793#if USE_SIGTRAP_SIGINFO
794 siginfo_t siginfo;
795#endif
d50171e4 796
bf9ae9d8 797 if (!low_supports_breakpoints ())
df95181f 798 return false;
0d62e5e8 799
a9deee17
PA
800 process_info *proc = get_thread_process (get_lwp_thread (lwp));
801 if (proc->starting_up)
802 {
803 /* Claim we have the stop PC so that the caller doesn't try to
804 fetch it itself. */
805 return true;
806 }
807
582511be 808 pc = get_pc (lwp);
d4807ea2 809 sw_breakpoint_pc = pc - low_decr_pc_after_break ();
d50171e4 810
582511be 811 /* breakpoint_at reads from the current thread. */
24583e45
TBA
812 scoped_restore_current_thread restore_thread;
813 switch_to_thread (get_lwp_thread (lwp));
47c0c975 814
3e572f71
PA
815#if USE_SIGTRAP_SIGINFO
816 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
817 (PTRACE_TYPE_ARG3) 0, &siginfo) == 0)
818 {
819 if (siginfo.si_signo == SIGTRAP)
820 {
e7ad2f14
PA
821 if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code)
822 && GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
3e572f71 823 {
e7ad2f14
PA
824 /* The si_code is ambiguous on this arch -- check debug
825 registers. */
826 if (!check_stopped_by_watchpoint (lwp))
827 lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
828 }
829 else if (GDB_ARCH_IS_TRAP_BRKPT (siginfo.si_code))
830 {
831 /* If we determine the LWP stopped for a SW breakpoint,
832 trust it. Particularly don't check watchpoint
833 registers, because at least on s390, we'd find
834 stopped-by-watchpoint as long as there's a watchpoint
835 set. */
3e572f71 836 lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
3e572f71 837 }
e7ad2f14 838 else if (GDB_ARCH_IS_TRAP_HWBKPT (siginfo.si_code))
3e572f71 839 {
e7ad2f14
PA
840 /* This can indicate either a hardware breakpoint or
841 hardware watchpoint. Check debug registers. */
842 if (!check_stopped_by_watchpoint (lwp))
843 lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
3e572f71 844 }
2bf6fb9d
PA
845 else if (siginfo.si_code == TRAP_TRACE)
846 {
e7ad2f14
PA
847 /* We may have single stepped an instruction that
848 triggered a watchpoint. In that case, on some
849 architectures (such as x86), instead of TRAP_HWBKPT,
850 si_code indicates TRAP_TRACE, and we need to check
851 the debug registers separately. */
852 if (!check_stopped_by_watchpoint (lwp))
853 lwp->stop_reason = TARGET_STOPPED_BY_SINGLE_STEP;
2bf6fb9d 854 }
3e572f71
PA
855 }
856 }
857#else
582511be
PA
858 /* We may have just stepped a breakpoint instruction. E.g., in
859 non-stop mode, GDB first tells the thread A to step a range, and
860 then the user inserts a breakpoint inside the range. In that
8090aef2
PA
861 case we need to report the breakpoint PC. */
862 if ((!lwp->stepping || lwp->stop_pc == sw_breakpoint_pc)
d7146cda 863 && low_breakpoint_at (sw_breakpoint_pc))
e7ad2f14
PA
864 lwp->stop_reason = TARGET_STOPPED_BY_SW_BREAKPOINT;
865
866 if (hardware_breakpoint_inserted_here (pc))
867 lwp->stop_reason = TARGET_STOPPED_BY_HW_BREAKPOINT;
868
869 if (lwp->stop_reason == TARGET_STOPPED_BY_NO_REASON)
870 check_stopped_by_watchpoint (lwp);
871#endif
872
873 if (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT)
582511be 874 {
c058728c
SM
875 threads_debug_printf
876 ("%s stopped by software breakpoint",
877 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
582511be
PA
878
879 /* Back up the PC if necessary. */
880 if (pc != sw_breakpoint_pc)
e7ad2f14 881 {
582511be
PA
882 struct regcache *regcache
883 = get_thread_regcache (current_thread, 1);
bf9ae9d8 884 low_set_pc (regcache, sw_breakpoint_pc);
582511be
PA
885 }
886
e7ad2f14
PA
887 /* Update this so we record the correct stop PC below. */
888 pc = sw_breakpoint_pc;
582511be 889 }
e7ad2f14 890 else if (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT)
c058728c
SM
891 threads_debug_printf
892 ("%s stopped by hardware breakpoint",
893 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
e7ad2f14 894 else if (lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
c058728c
SM
895 threads_debug_printf
896 ("%s stopped by hardware watchpoint",
897 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
e7ad2f14 898 else if (lwp->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP)
c058728c
SM
899 threads_debug_printf
900 ("%s stopped by trace",
901 target_pid_to_str (ptid_of (get_lwp_thread (lwp))).c_str ());
e7ad2f14
PA
902
903 lwp->stop_pc = pc;
df95181f 904 return true;
0d62e5e8 905}
ce3a066d 906
fd000fb3
TBA
907lwp_info *
908linux_process_target::add_lwp (ptid_t ptid)
611cb4a5 909{
c360a473 910 lwp_info *lwp = new lwp_info;
0d62e5e8 911
754e3168
AH
912 lwp->thread = add_thread (ptid, lwp);
913
fd000fb3 914 low_new_thread (lwp);
aa5ca48f 915
54a0b537 916 return lwp;
0d62e5e8 917}
611cb4a5 918
fd000fb3
TBA
919void
920linux_process_target::low_new_thread (lwp_info *info)
921{
922 /* Nop. */
923}
924
2090129c
SDJ
925/* Callback to be used when calling fork_inferior, responsible for
926 actually initiating the tracing of the inferior. */
927
928static void
929linux_ptrace_fun ()
930{
931 if (ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
932 (PTRACE_TYPE_ARG4) 0) < 0)
50fa3001 933 trace_start_error_with_name ("ptrace");
2090129c
SDJ
934
935 if (setpgid (0, 0) < 0)
936 trace_start_error_with_name ("setpgid");
937
938 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
939 stdout to stderr so that inferior i/o doesn't corrupt the connection.
940 Also, redirect stdin to /dev/null. */
941 if (remote_connection_is_stdio ())
942 {
943 if (close (0) < 0)
944 trace_start_error_with_name ("close");
945 if (open ("/dev/null", O_RDONLY) < 0)
946 trace_start_error_with_name ("open");
947 if (dup2 (2, 1) < 0)
948 trace_start_error_with_name ("dup2");
949 if (write (2, "stdin/stdout redirected\n",
950 sizeof ("stdin/stdout redirected\n") - 1) < 0)
951 {
952 /* Errors ignored. */;
953 }
954 }
955}
956
da6d8c04 957/* Start an inferior process and returns its pid.
2090129c
SDJ
958 PROGRAM is the name of the program to be started, and PROGRAM_ARGS
959 are its arguments. */
da6d8c04 960
15295543
TBA
961int
962linux_process_target::create_inferior (const char *program,
963 const std::vector<char *> &program_args)
da6d8c04 964{
c12a5089 965 client_state &cs = get_client_state ();
a6dbe5df 966 struct lwp_info *new_lwp;
da6d8c04 967 int pid;
95954743 968 ptid_t ptid;
03583c20 969
41272101
TT
970 {
971 maybe_disable_address_space_randomization restore_personality
c12a5089 972 (cs.disable_randomization);
bea571eb 973 std::string str_program_args = construct_inferior_arguments (program_args);
41272101
TT
974
975 pid = fork_inferior (program,
976 str_program_args.c_str (),
977 get_environ ()->envp (), linux_ptrace_fun,
978 NULL, NULL, NULL, NULL);
979 }
03583c20 980
421490af
PA
981 /* When spawning a new process, we can't open the mem file yet. We
982 still have to nurse the process through the shell, and that execs
983 a couple times. The address space a /proc/PID/mem file is
984 accessing is destroyed on exec. */
985 process_info *proc = add_linux_process_no_mem_file (pid, 0);
95954743 986
184ea2f7 987 ptid = ptid_t (pid, pid);
95954743 988 new_lwp = add_lwp (ptid);
a6dbe5df 989 new_lwp->must_set_ptrace_flags = 1;
611cb4a5 990
2090129c
SDJ
991 post_fork_inferior (pid, program);
992
421490af
PA
993 /* PROC is now past the shell running the program we want, so we can
994 open the /proc/PID/mem file. */
995 open_proc_mem_file (proc);
996
a9fa9f7d 997 return pid;
da6d8c04
DJ
998}
999
ece66d65
JS
1000/* Implement the post_create_inferior target_ops method. */
1001
6dee9afb
TBA
1002void
1003linux_process_target::post_create_inferior ()
ece66d65
JS
1004{
1005 struct lwp_info *lwp = get_thread_lwp (current_thread);
1006
797bcff5 1007 low_arch_setup ();
ece66d65
JS
1008
1009 if (lwp->must_set_ptrace_flags)
1010 {
1011 struct process_info *proc = current_process ();
1012 int options = linux_low_ptrace_options (proc->attached);
1013
1014 linux_enable_event_reporting (lwpid_of (current_thread), options);
1015 lwp->must_set_ptrace_flags = 0;
1016 }
1017}
1018
7ae1a6a6 1019int
fd000fb3 1020linux_process_target::attach_lwp (ptid_t ptid)
da6d8c04 1021{
54a0b537 1022 struct lwp_info *new_lwp;
e38504b3 1023 int lwpid = ptid.lwp ();
611cb4a5 1024
b8e1b30e 1025 if (ptrace (PTRACE_ATTACH, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0)
56f7af9c 1026 != 0)
7ae1a6a6 1027 return errno;
24a09b5f 1028
b3312d80 1029 new_lwp = add_lwp (ptid);
0d62e5e8 1030
a6dbe5df
PA
1031 /* We need to wait for SIGSTOP before being able to make the next
1032 ptrace call on this LWP. */
1033 new_lwp->must_set_ptrace_flags = 1;
1034
644cebc9 1035 if (linux_proc_pid_is_stopped (lwpid))
c14d7ab2 1036 {
c058728c 1037 threads_debug_printf ("Attached to a stopped process");
c14d7ab2
PA
1038
1039 /* The process is definitely stopped. It is in a job control
1040 stop, unless the kernel predates the TASK_STOPPED /
1041 TASK_TRACED distinction, in which case it might be in a
1042 ptrace stop. Make sure it is in a ptrace stop; from there we
1043 can kill it, signal it, et cetera.
1044
1045 First make sure there is a pending SIGSTOP. Since we are
1046 already attached, the process can not transition from stopped
1047 to running without a PTRACE_CONT; so we know this signal will
1048 go into the queue. The SIGSTOP generated by PTRACE_ATTACH is
1049 probably already in the queue (unless this kernel is old
1050 enough to use TASK_STOPPED for ptrace stops); but since
1051 SIGSTOP is not an RT signal, it can only be queued once. */
1052 kill_lwp (lwpid, SIGSTOP);
1053
1054 /* Finally, resume the stopped process. This will deliver the
1055 SIGSTOP (or a higher priority signal, just like normal
1056 PTRACE_ATTACH), which we'll catch later on. */
b8e1b30e 1057 ptrace (PTRACE_CONT, lwpid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
c14d7ab2
PA
1058 }
1059
0d62e5e8 1060 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
0e21c1ec
DE
1061 brings it to a halt.
1062
1063 There are several cases to consider here:
1064
1065 1) gdbserver has already attached to the process and is being notified
1b3f6016 1066 of a new thread that is being created.
d50171e4
PA
1067 In this case we should ignore that SIGSTOP and resume the
1068 process. This is handled below by setting stop_expected = 1,
8336d594 1069 and the fact that add_thread sets last_resume_kind ==
d50171e4 1070 resume_continue.
0e21c1ec
DE
1071
1072 2) This is the first thread (the process thread), and we're attaching
1b3f6016
PA
1073 to it via attach_inferior.
1074 In this case we want the process thread to stop.
d50171e4
PA
1075 This is handled by having linux_attach set last_resume_kind ==
1076 resume_stop after we return.
e3deef73
LM
1077
1078 If the pid we are attaching to is also the tgid, we attach to and
1079 stop all the existing threads. Otherwise, we attach to pid and
1080 ignore any other threads in the same group as this pid.
0e21c1ec
DE
1081
1082 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1b3f6016
PA
1083 existing threads.
1084 In this case we want the thread to stop.
1085 FIXME: This case is currently not properly handled.
1086 We should wait for the SIGSTOP but don't. Things work apparently
1087 because enough time passes between when we ptrace (ATTACH) and when
1088 gdb makes the next ptrace call on the thread.
0d62e5e8
DJ
1089
1090 On the other hand, if we are currently trying to stop all threads, we
1091 should treat the new thread as if we had sent it a SIGSTOP. This works
54a0b537 1092 because we are guaranteed that the add_lwp call above added us to the
0e21c1ec
DE
1093 end of the list, and so the new thread has not yet reached
1094 wait_for_sigstop (but will). */
d50171e4 1095 new_lwp->stop_expected = 1;
0d62e5e8 1096
7ae1a6a6 1097 return 0;
95954743
PA
1098}
1099
8784d563
PA
1100/* Callback for linux_proc_attach_tgid_threads. Attach to PTID if not
1101 already attached. Returns true if a new LWP is found, false
1102 otherwise. */
1103
1104static int
1105attach_proc_task_lwp_callback (ptid_t ptid)
1106{
1107 /* Is this a new thread? */
1108 if (find_thread_ptid (ptid) == NULL)
1109 {
e38504b3 1110 int lwpid = ptid.lwp ();
8784d563
PA
1111 int err;
1112
c058728c 1113 threads_debug_printf ("Found new lwp %d", lwpid);
8784d563 1114
fd000fb3 1115 err = the_linux_target->attach_lwp (ptid);
8784d563
PA
1116
1117 /* Be quiet if we simply raced with the thread exiting. EPERM
1118 is returned if the thread's task still exists, and is marked
1119 as exited or zombie, as well as other conditions, so in that
1120 case, confirm the status in /proc/PID/status. */
1121 if (err == ESRCH
1122 || (err == EPERM && linux_proc_pid_is_gone (lwpid)))
c058728c
SM
1123 threads_debug_printf
1124 ("Cannot attach to lwp %d: thread is gone (%d: %s)",
1125 lwpid, err, safe_strerror (err));
8784d563
PA
1126 else if (err != 0)
1127 {
4d9b86e1 1128 std::string reason
50fa3001 1129 = linux_ptrace_attach_fail_reason_string (ptid, err);
4d9b86e1
SM
1130
1131 warning (_("Cannot attach to lwp %d: %s"), lwpid, reason.c_str ());
8784d563
PA
1132 }
1133
1134 return 1;
1135 }
1136 return 0;
1137}
1138
500c1d85
PA
1139static void async_file_mark (void);
1140
e3deef73
LM
1141/* Attach to PID. If PID is the tgid, attach to it and all
1142 of its threads. */
1143
ef03dad8
TBA
1144int
1145linux_process_target::attach (unsigned long pid)
0d62e5e8 1146{
500c1d85
PA
1147 struct process_info *proc;
1148 struct thread_info *initial_thread;
184ea2f7 1149 ptid_t ptid = ptid_t (pid, pid);
7ae1a6a6
PA
1150 int err;
1151
421490af
PA
1152 /* Delay opening the /proc/PID/mem file until we've successfully
1153 attached. */
1154 proc = add_linux_process_no_mem_file (pid, 1);
df0da8a2 1155
e3deef73
LM
1156 /* Attach to PID. We will check for other threads
1157 soon. */
fd000fb3 1158 err = attach_lwp (ptid);
7ae1a6a6 1159 if (err != 0)
4d9b86e1 1160 {
f551c8ef 1161 this->remove_linux_process (proc);
4d9b86e1 1162
50fa3001
SDJ
1163 std::string reason = linux_ptrace_attach_fail_reason_string (ptid, err);
1164 error ("Cannot attach to process %ld: %s", pid, reason.c_str ());
4d9b86e1 1165 }
7ae1a6a6 1166
421490af
PA
1167 open_proc_mem_file (proc);
1168
500c1d85
PA
1169 /* Don't ignore the initial SIGSTOP if we just attached to this
1170 process. It will be collected by wait shortly. */
184ea2f7 1171 initial_thread = find_thread_ptid (ptid_t (pid, pid));
500c1d85 1172 initial_thread->last_resume_kind = resume_stop;
0d62e5e8 1173
8784d563
PA
1174 /* We must attach to every LWP. If /proc is mounted, use that to
1175 find them now. On the one hand, the inferior may be using raw
1176 clone instead of using pthreads. On the other hand, even if it
1177 is using pthreads, GDB may not be connected yet (thread_db needs
1178 to do symbol lookups, through qSymbol). Also, thread_db walks
1179 structures in the inferior's address space to find the list of
1180 threads/LWPs, and those structures may well be corrupted. Note
1181 that once thread_db is loaded, we'll still use it to list threads
1182 and associate pthread info with each LWP. */
1183 linux_proc_attach_tgid_threads (pid, attach_proc_task_lwp_callback);
500c1d85
PA
1184
1185 /* GDB will shortly read the xml target description for this
1186 process, to figure out the process' architecture. But the target
1187 description is only filled in when the first process/thread in
1188 the thread group reports its initial PTRACE_ATTACH SIGSTOP. Do
1189 that now, otherwise, if GDB is fast enough, it could read the
1190 target description _before_ that initial stop. */
1191 if (non_stop)
1192 {
1193 struct lwp_info *lwp;
1194 int wstat, lwpid;
f2907e49 1195 ptid_t pid_ptid = ptid_t (pid);
500c1d85 1196
d16f3f6c 1197 lwpid = wait_for_event_filtered (pid_ptid, pid_ptid, &wstat, __WALL);
500c1d85
PA
1198 gdb_assert (lwpid > 0);
1199
f2907e49 1200 lwp = find_lwp_pid (ptid_t (lwpid));
500c1d85
PA
1201
1202 if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGSTOP)
1203 {
1204 lwp->status_pending_p = 1;
1205 lwp->status_pending = wstat;
1206 }
1207
1208 initial_thread->last_resume_kind = resume_continue;
1209
1210 async_file_mark ();
1211
1212 gdb_assert (proc->tdesc != NULL);
1213 }
1214
95954743
PA
1215 return 0;
1216}
1217
95954743 1218static int
e4eb0dec 1219last_thread_of_process_p (int pid)
95954743 1220{
e4eb0dec 1221 bool seen_one = false;
95954743 1222
da4ae14a 1223 thread_info *thread = find_thread (pid, [&] (thread_info *thr_arg)
95954743 1224 {
e4eb0dec
SM
1225 if (!seen_one)
1226 {
1227 /* This is the first thread of this process we see. */
1228 seen_one = true;
1229 return false;
1230 }
1231 else
1232 {
1233 /* This is the second thread of this process we see. */
1234 return true;
1235 }
1236 });
da6d8c04 1237
e4eb0dec 1238 return thread == NULL;
95954743
PA
1239}
1240
da84f473
PA
1241/* Kill LWP. */
1242
1243static void
1244linux_kill_one_lwp (struct lwp_info *lwp)
1245{
d86d4aaf
DE
1246 struct thread_info *thr = get_lwp_thread (lwp);
1247 int pid = lwpid_of (thr);
da84f473
PA
1248
1249 /* PTRACE_KILL is unreliable. After stepping into a signal handler,
1250 there is no signal context, and ptrace(PTRACE_KILL) (or
1251 ptrace(PTRACE_CONT, SIGKILL), pretty much the same) acts like
1252 ptrace(CONT, pid, 0,0) and just resumes the tracee. A better
1253 alternative is to kill with SIGKILL. We only need one SIGKILL
1254 per process, not one for each thread. But since we still support
4a6ed09b
PA
1255 support debugging programs using raw clone without CLONE_THREAD,
1256 we send one for each thread. For years, we used PTRACE_KILL
1257 only, so we're being a bit paranoid about some old kernels where
1258 PTRACE_KILL might work better (dubious if there are any such, but
1259 that's why it's paranoia), so we try SIGKILL first, PTRACE_KILL
1260 second, and so we're fine everywhere. */
da84f473
PA
1261
1262 errno = 0;
69ff6be5 1263 kill_lwp (pid, SIGKILL);
da84f473 1264 if (debug_threads)
ce9e3fe7
PA
1265 {
1266 int save_errno = errno;
1267
c058728c
SM
1268 threads_debug_printf ("kill_lwp (SIGKILL) %s, 0, 0 (%s)",
1269 target_pid_to_str (ptid_of (thr)).c_str (),
1270 save_errno ? safe_strerror (save_errno) : "OK");
ce9e3fe7 1271 }
da84f473
PA
1272
1273 errno = 0;
b8e1b30e 1274 ptrace (PTRACE_KILL, pid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
da84f473 1275 if (debug_threads)
ce9e3fe7
PA
1276 {
1277 int save_errno = errno;
1278
c058728c
SM
1279 threads_debug_printf ("PTRACE_KILL %s, 0, 0 (%s)",
1280 target_pid_to_str (ptid_of (thr)).c_str (),
1281 save_errno ? safe_strerror (save_errno) : "OK");
ce9e3fe7 1282 }
da84f473
PA
1283}
1284
e76126e8
PA
1285/* Kill LWP and wait for it to die. */
1286
1287static void
1288kill_wait_lwp (struct lwp_info *lwp)
1289{
1290 struct thread_info *thr = get_lwp_thread (lwp);
e99b03dc 1291 int pid = ptid_of (thr).pid ();
e38504b3 1292 int lwpid = ptid_of (thr).lwp ();
e76126e8
PA
1293 int wstat;
1294 int res;
1295
c058728c 1296 threads_debug_printf ("killing lwp %d, for pid: %d", lwpid, pid);
e76126e8
PA
1297
1298 do
1299 {
1300 linux_kill_one_lwp (lwp);
1301
1302 /* Make sure it died. Notes:
1303
1304 - The loop is most likely unnecessary.
1305
d16f3f6c 1306 - We don't use wait_for_event as that could delete lwps
e76126e8
PA
1307 while we're iterating over them. We're not interested in
1308 any pending status at this point, only in making sure all
1309 wait status on the kernel side are collected until the
1310 process is reaped.
1311
1312 - We don't use __WALL here as the __WALL emulation relies on
1313 SIGCHLD, and killing a stopped process doesn't generate
1314 one, nor an exit status.
1315 */
1316 res = my_waitpid (lwpid, &wstat, 0);
1317 if (res == -1 && errno == ECHILD)
1318 res = my_waitpid (lwpid, &wstat, __WCLONE);
1319 } while (res > 0 && WIFSTOPPED (wstat));
1320
586b02a9
PA
1321 /* Even if it was stopped, the child may have already disappeared.
1322 E.g., if it was killed by SIGKILL. */
1323 if (res < 0 && errno != ECHILD)
1324 perror_with_name ("kill_wait_lwp");
e76126e8
PA
1325}
1326
578290ec 1327/* Callback for `for_each_thread'. Kills an lwp of a given process,
da84f473 1328 except the leader. */
95954743 1329
578290ec
SM
1330static void
1331kill_one_lwp_callback (thread_info *thread, int pid)
da6d8c04 1332{
54a0b537 1333 struct lwp_info *lwp = get_thread_lwp (thread);
0d62e5e8 1334
fd500816
DJ
1335 /* We avoid killing the first thread here, because of a Linux kernel (at
1336 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
1337 the children get a chance to be reaped, it will remain a zombie
1338 forever. */
95954743 1339
d86d4aaf 1340 if (lwpid_of (thread) == pid)
95954743 1341 {
c058728c
SM
1342 threads_debug_printf ("is last of process %s",
1343 target_pid_to_str (thread->id).c_str ());
578290ec 1344 return;
95954743 1345 }
fd500816 1346
e76126e8 1347 kill_wait_lwp (lwp);
da6d8c04
DJ
1348}
1349
c6885a57
TBA
1350int
1351linux_process_target::kill (process_info *process)
0d62e5e8 1352{
a780ef4f 1353 int pid = process->pid;
9d606399 1354
f9e39928
PA
1355 /* If we're killing a running inferior, make sure it is stopped
1356 first, as PTRACE_KILL will not work otherwise. */
7984d532 1357 stop_all_lwps (0, NULL);
f9e39928 1358
578290ec
SM
1359 for_each_thread (pid, [&] (thread_info *thread)
1360 {
1361 kill_one_lwp_callback (thread, pid);
1362 });
fd500816 1363
54a0b537 1364 /* See the comment in linux_kill_one_lwp. We did not kill the first
fd500816 1365 thread in the list, so do so now. */
a780ef4f 1366 lwp_info *lwp = find_lwp_pid (ptid_t (pid));
bd99dc85 1367
784867a5 1368 if (lwp == NULL)
c058728c 1369 threads_debug_printf ("cannot find lwp for pid: %d", pid);
784867a5 1370 else
e76126e8 1371 kill_wait_lwp (lwp);
2d717e4f 1372
8adb37b9 1373 mourn (process);
f9e39928
PA
1374
1375 /* Since we presently can only stop all lwps of all processes, we
1376 need to unstop lwps of other processes. */
7984d532 1377 unstop_all_lwps (0, NULL);
95954743 1378 return 0;
0d62e5e8
DJ
1379}
1380
9b224c5e
PA
1381/* Get pending signal of THREAD, for detaching purposes. This is the
1382 signal the thread last stopped for, which we need to deliver to the
1383 thread when detaching, otherwise, it'd be suppressed/lost. */
1384
1385static int
1386get_detach_signal (struct thread_info *thread)
1387{
c12a5089 1388 client_state &cs = get_client_state ();
a493e3e2 1389 enum gdb_signal signo = GDB_SIGNAL_0;
9b224c5e
PA
1390 int status;
1391 struct lwp_info *lp = get_thread_lwp (thread);
1392
1393 if (lp->status_pending_p)
1394 status = lp->status_pending;
1395 else
1396 {
1397 /* If the thread had been suspended by gdbserver, and it stopped
1398 cleanly, then it'll have stopped with SIGSTOP. But we don't
1399 want to deliver that SIGSTOP. */
183be222
SM
1400 if (thread->last_status.kind () != TARGET_WAITKIND_STOPPED
1401 || thread->last_status.sig () == GDB_SIGNAL_0)
9b224c5e
PA
1402 return 0;
1403
1404 /* Otherwise, we may need to deliver the signal we
1405 intercepted. */
1406 status = lp->last_status;
1407 }
1408
1409 if (!WIFSTOPPED (status))
1410 {
c058728c
SM
1411 threads_debug_printf ("lwp %s hasn't stopped: no pending signal",
1412 target_pid_to_str (ptid_of (thread)).c_str ());
9b224c5e
PA
1413 return 0;
1414 }
1415
1416 /* Extended wait statuses aren't real SIGTRAPs. */
89a5711c 1417 if (WSTOPSIG (status) == SIGTRAP && linux_is_extended_waitstatus (status))
9b224c5e 1418 {
c058728c
SM
1419 threads_debug_printf ("lwp %s had stopped with extended "
1420 "status: no pending signal",
1421 target_pid_to_str (ptid_of (thread)).c_str ());
9b224c5e
PA
1422 return 0;
1423 }
1424
2ea28649 1425 signo = gdb_signal_from_host (WSTOPSIG (status));
9b224c5e 1426
c12a5089 1427 if (cs.program_signals_p && !cs.program_signals[signo])
9b224c5e 1428 {
c058728c
SM
1429 threads_debug_printf ("lwp %s had signal %s, but it is in nopass state",
1430 target_pid_to_str (ptid_of (thread)).c_str (),
1431 gdb_signal_to_string (signo));
9b224c5e
PA
1432 return 0;
1433 }
c12a5089 1434 else if (!cs.program_signals_p
9b224c5e
PA
1435 /* If we have no way to know which signals GDB does not
1436 want to have passed to the program, assume
1437 SIGTRAP/SIGINT, which is GDB's default. */
a493e3e2 1438 && (signo == GDB_SIGNAL_TRAP || signo == GDB_SIGNAL_INT))
9b224c5e 1439 {
c058728c
SM
1440 threads_debug_printf ("lwp %s had signal %s, "
1441 "but we don't know if we should pass it. "
1442 "Default to not.",
1443 target_pid_to_str (ptid_of (thread)).c_str (),
1444 gdb_signal_to_string (signo));
9b224c5e
PA
1445 return 0;
1446 }
1447 else
1448 {
c058728c
SM
1449 threads_debug_printf ("lwp %s has pending signal %s: delivering it",
1450 target_pid_to_str (ptid_of (thread)).c_str (),
1451 gdb_signal_to_string (signo));
9b224c5e
PA
1452
1453 return WSTOPSIG (status);
1454 }
1455}
1456
fd000fb3
TBA
1457void
1458linux_process_target::detach_one_lwp (lwp_info *lwp)
6ad8ae5c 1459{
ced2dffb 1460 struct thread_info *thread = get_lwp_thread (lwp);
9b224c5e 1461 int sig;
ced2dffb 1462 int lwpid;
6ad8ae5c 1463
9b224c5e 1464 /* If there is a pending SIGSTOP, get rid of it. */
54a0b537 1465 if (lwp->stop_expected)
ae13219e 1466 {
c058728c
SM
1467 threads_debug_printf ("Sending SIGCONT to %s",
1468 target_pid_to_str (ptid_of (thread)).c_str ());
9b224c5e 1469
d86d4aaf 1470 kill_lwp (lwpid_of (thread), SIGCONT);
54a0b537 1471 lwp->stop_expected = 0;
ae13219e
DJ
1472 }
1473
9b224c5e
PA
1474 /* Pass on any pending signal for this thread. */
1475 sig = get_detach_signal (thread);
1476
ced2dffb
PA
1477 /* Preparing to resume may try to write registers, and fail if the
1478 lwp is zombie. If that happens, ignore the error. We'll handle
1479 it below, when detach fails with ESRCH. */
a70b8144 1480 try
ced2dffb
PA
1481 {
1482 /* Flush any pending changes to the process's registers. */
1483 regcache_invalidate_thread (thread);
1484
1485 /* Finally, let it resume. */
d7599cc0 1486 low_prepare_to_resume (lwp);
ced2dffb 1487 }
230d2906 1488 catch (const gdb_exception_error &ex)
ced2dffb
PA
1489 {
1490 if (!check_ptrace_stopped_lwp_gone (lwp))
eedc3f4f 1491 throw;
ced2dffb 1492 }
ced2dffb
PA
1493
1494 lwpid = lwpid_of (thread);
1495 if (ptrace (PTRACE_DETACH, lwpid, (PTRACE_TYPE_ARG3) 0,
b8e1b30e 1496 (PTRACE_TYPE_ARG4) (long) sig) < 0)
ced2dffb
PA
1497 {
1498 int save_errno = errno;
1499
1500 /* We know the thread exists, so ESRCH must mean the lwp is
1501 zombie. This can happen if one of the already-detached
1502 threads exits the whole thread group. In that case we're
1503 still attached, and must reap the lwp. */
1504 if (save_errno == ESRCH)
1505 {
1506 int ret, status;
1507
1508 ret = my_waitpid (lwpid, &status, __WALL);
1509 if (ret == -1)
1510 {
1511 warning (_("Couldn't reap LWP %d while detaching: %s"),
6d91ce9a 1512 lwpid, safe_strerror (errno));
ced2dffb
PA
1513 }
1514 else if (!WIFEXITED (status) && !WIFSIGNALED (status))
1515 {
1516 warning (_("Reaping LWP %d while detaching "
1517 "returned unexpected status 0x%x"),
1518 lwpid, status);
1519 }
1520 }
1521 else
1522 {
1523 error (_("Can't detach %s: %s"),
61d7f128 1524 target_pid_to_str (ptid_of (thread)).c_str (),
6d91ce9a 1525 safe_strerror (save_errno));
ced2dffb
PA
1526 }
1527 }
c058728c
SM
1528 else
1529 threads_debug_printf ("PTRACE_DETACH (%s, %s, 0) (OK)",
1530 target_pid_to_str (ptid_of (thread)).c_str (),
1531 strsignal (sig));
bd99dc85
PA
1532
1533 delete_lwp (lwp);
ced2dffb
PA
1534}
1535
9061c9cf
TBA
1536int
1537linux_process_target::detach (process_info *process)
95954743 1538{
ced2dffb 1539 struct lwp_info *main_lwp;
95954743 1540
863d01bd
PA
1541 /* As there's a step over already in progress, let it finish first,
1542 otherwise nesting a stabilize_threads operation on top gets real
1543 messy. */
1544 complete_ongoing_step_over ();
1545
f9e39928 1546 /* Stop all threads before detaching. First, ptrace requires that
30baf67b 1547 the thread is stopped to successfully detach. Second, thread_db
f9e39928
PA
1548 may need to uninstall thread event breakpoints from memory, which
1549 only works with a stopped process anyway. */
7984d532 1550 stop_all_lwps (0, NULL);
f9e39928 1551
ca5c370d 1552#ifdef USE_THREAD_DB
8336d594 1553 thread_db_detach (process);
ca5c370d
PA
1554#endif
1555
fa593d66 1556 /* Stabilize threads (move out of jump pads). */
5c9eb2f2 1557 target_stabilize_threads ();
fa593d66 1558
ced2dffb
PA
1559 /* Detach from the clone lwps first. If the thread group exits just
1560 while we're detaching, we must reap the clone lwps before we're
1561 able to reap the leader. */
fd000fb3
TBA
1562 for_each_thread (process->pid, [this] (thread_info *thread)
1563 {
1564 /* We don't actually detach from the thread group leader just yet.
1565 If the thread group exits, we must reap the zombie clone lwps
1566 before we're able to reap the leader. */
1567 if (thread->id.pid () == thread->id.lwp ())
1568 return;
1569
1570 lwp_info *lwp = get_thread_lwp (thread);
1571 detach_one_lwp (lwp);
1572 });
ced2dffb 1573
ef2ddb33 1574 main_lwp = find_lwp_pid (ptid_t (process->pid));
fd000fb3 1575 detach_one_lwp (main_lwp);
8336d594 1576
8adb37b9 1577 mourn (process);
f9e39928
PA
1578
1579 /* Since we presently can only stop all lwps of all processes, we
1580 need to unstop lwps of other processes. */
7984d532 1581 unstop_all_lwps (0, NULL);
f9e39928
PA
1582 return 0;
1583}
1584
1585/* Remove all LWPs that belong to process PROC from the lwp list. */
1586
8adb37b9
TBA
1587void
1588linux_process_target::mourn (process_info *process)
8336d594 1589{
8336d594
PA
1590#ifdef USE_THREAD_DB
1591 thread_db_mourn (process);
1592#endif
1593
fd000fb3 1594 for_each_thread (process->pid, [this] (thread_info *thread)
6b2a85da
SM
1595 {
1596 delete_lwp (get_thread_lwp (thread));
1597 });
f9e39928 1598
f551c8ef 1599 this->remove_linux_process (process);
8336d594
PA
1600}
1601
95a49a39
TBA
1602void
1603linux_process_target::join (int pid)
444d6139 1604{
444d6139
PA
1605 int status, ret;
1606
1607 do {
d105de22 1608 ret = my_waitpid (pid, &status, 0);
444d6139
PA
1609 if (WIFEXITED (status) || WIFSIGNALED (status))
1610 break;
1611 } while (ret != -1 || errno != ECHILD);
1612}
1613
13d3d99b
TBA
1614/* Return true if the given thread is still alive. */
1615
1616bool
1617linux_process_target::thread_alive (ptid_t ptid)
0d62e5e8 1618{
95954743
PA
1619 struct lwp_info *lwp = find_lwp_pid (ptid);
1620
1621 /* We assume we always know if a thread exits. If a whole process
1622 exited but we still haven't been able to report it to GDB, we'll
1623 hold on to the last lwp of the dead process. */
1624 if (lwp != NULL)
00db26fa 1625 return !lwp_is_marked_dead (lwp);
0d62e5e8
DJ
1626 else
1627 return 0;
1628}
1629
df95181f
TBA
1630bool
1631linux_process_target::thread_still_has_status_pending (thread_info *thread)
582511be
PA
1632{
1633 struct lwp_info *lp = get_thread_lwp (thread);
1634
1635 if (!lp->status_pending_p)
1636 return 0;
1637
582511be 1638 if (thread->last_resume_kind != resume_stop
15c66dd6
PA
1639 && (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
1640 || lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT))
582511be 1641 {
582511be
PA
1642 CORE_ADDR pc;
1643 int discard = 0;
1644
1645 gdb_assert (lp->last_status != 0);
1646
1647 pc = get_pc (lp);
1648
24583e45
TBA
1649 scoped_restore_current_thread restore_thread;
1650 switch_to_thread (thread);
582511be
PA
1651
1652 if (pc != lp->stop_pc)
1653 {
c058728c
SM
1654 threads_debug_printf ("PC of %ld changed",
1655 lwpid_of (thread));
582511be
PA
1656 discard = 1;
1657 }
3e572f71
PA
1658
1659#if !USE_SIGTRAP_SIGINFO
15c66dd6 1660 else if (lp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
d7146cda 1661 && !low_breakpoint_at (pc))
582511be 1662 {
c058728c
SM
1663 threads_debug_printf ("previous SW breakpoint of %ld gone",
1664 lwpid_of (thread));
582511be
PA
1665 discard = 1;
1666 }
15c66dd6 1667 else if (lp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT
582511be
PA
1668 && !hardware_breakpoint_inserted_here (pc))
1669 {
c058728c
SM
1670 threads_debug_printf ("previous HW breakpoint of %ld gone",
1671 lwpid_of (thread));
582511be
PA
1672 discard = 1;
1673 }
3e572f71 1674#endif
582511be 1675
582511be
PA
1676 if (discard)
1677 {
c058728c 1678 threads_debug_printf ("discarding pending breakpoint status");
582511be
PA
1679 lp->status_pending_p = 0;
1680 return 0;
1681 }
1682 }
1683
1684 return 1;
1685}
1686
a681f9c9
PA
1687/* Returns true if LWP is resumed from the client's perspective. */
1688
1689static int
1690lwp_resumed (struct lwp_info *lwp)
1691{
1692 struct thread_info *thread = get_lwp_thread (lwp);
1693
1694 if (thread->last_resume_kind != resume_stop)
1695 return 1;
1696
1697 /* Did gdb send us a `vCont;t', but we haven't reported the
1698 corresponding stop to gdb yet? If so, the thread is still
1699 resumed/running from gdb's perspective. */
1700 if (thread->last_resume_kind == resume_stop
183be222 1701 && thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
a681f9c9
PA
1702 return 1;
1703
1704 return 0;
1705}
1706
df95181f
TBA
1707bool
1708linux_process_target::status_pending_p_callback (thread_info *thread,
1709 ptid_t ptid)
0d62e5e8 1710{
582511be 1711 struct lwp_info *lp = get_thread_lwp (thread);
95954743
PA
1712
1713 /* Check if we're only interested in events from a specific process
afa8d396 1714 or a specific LWP. */
83e1b6c1 1715 if (!thread->id.matches (ptid))
95954743 1716 return 0;
0d62e5e8 1717
a681f9c9
PA
1718 if (!lwp_resumed (lp))
1719 return 0;
1720
582511be 1721 if (lp->status_pending_p
df95181f 1722 && !thread_still_has_status_pending (thread))
582511be 1723 {
df95181f 1724 resume_one_lwp (lp, lp->stepping, GDB_SIGNAL_0, NULL);
582511be
PA
1725 return 0;
1726 }
0d62e5e8 1727
582511be 1728 return lp->status_pending_p;
0d62e5e8
DJ
1729}
1730
95954743
PA
1731struct lwp_info *
1732find_lwp_pid (ptid_t ptid)
1733{
da4ae14a 1734 thread_info *thread = find_thread ([&] (thread_info *thr_arg)
454296a2
SM
1735 {
1736 int lwp = ptid.lwp () != 0 ? ptid.lwp () : ptid.pid ();
da4ae14a 1737 return thr_arg->id.lwp () == lwp;
454296a2 1738 });
d86d4aaf
DE
1739
1740 if (thread == NULL)
1741 return NULL;
1742
9c80ecd6 1743 return get_thread_lwp (thread);
95954743
PA
1744}
1745
fa96cb38 1746/* Return the number of known LWPs in the tgid given by PID. */
0d62e5e8 1747
fa96cb38
PA
1748static int
1749num_lwps (int pid)
1750{
fa96cb38 1751 int count = 0;
0d62e5e8 1752
4d3bb80e
SM
1753 for_each_thread (pid, [&] (thread_info *thread)
1754 {
9c80ecd6 1755 count++;
4d3bb80e 1756 });
3aee8918 1757
fa96cb38
PA
1758 return count;
1759}
d61ddec4 1760
6d4ee8c6
GB
1761/* See nat/linux-nat.h. */
1762
1763struct lwp_info *
1764iterate_over_lwps (ptid_t filter,
d3a70e03 1765 gdb::function_view<iterate_over_lwps_ftype> callback)
6d4ee8c6 1766{
da4ae14a 1767 thread_info *thread = find_thread (filter, [&] (thread_info *thr_arg)
6d1e5673 1768 {
da4ae14a 1769 lwp_info *lwp = get_thread_lwp (thr_arg);
6d1e5673 1770
d3a70e03 1771 return callback (lwp);
6d1e5673 1772 });
6d4ee8c6 1773
9c80ecd6 1774 if (thread == NULL)
6d4ee8c6
GB
1775 return NULL;
1776
9c80ecd6 1777 return get_thread_lwp (thread);
6d4ee8c6
GB
1778}
1779
fd000fb3
TBA
1780void
1781linux_process_target::check_zombie_leaders ()
fa96cb38 1782{
aa40a989
PA
1783 for_each_process ([this] (process_info *proc)
1784 {
1785 pid_t leader_pid = pid_of (proc);
1786 lwp_info *leader_lp = find_lwp_pid (ptid_t (leader_pid));
1787
1788 threads_debug_printf ("leader_pid=%d, leader_lp!=NULL=%d, "
1789 "num_lwps=%d, zombie=%d",
1790 leader_pid, leader_lp!= NULL, num_lwps (leader_pid),
1791 linux_proc_pid_is_zombie (leader_pid));
1792
1793 if (leader_lp != NULL && !leader_lp->stopped
1794 /* Check if there are other threads in the group, as we may
8a841a35
PA
1795 have raced with the inferior simply exiting. Note this
1796 isn't a watertight check. If the inferior is
1797 multi-threaded and is exiting, it may be we see the
1798 leader as zombie before we reap all the non-leader
1799 threads. See comments below. */
aa40a989
PA
1800 && !last_thread_of_process_p (leader_pid)
1801 && linux_proc_pid_is_zombie (leader_pid))
1802 {
8a841a35
PA
1803 /* A zombie leader in a multi-threaded program can mean one
1804 of three things:
1805
1806 #1 - Only the leader exited, not the whole program, e.g.,
1807 with pthread_exit. Since we can't reap the leader's exit
1808 status until all other threads are gone and reaped too,
1809 we want to delete the zombie leader right away, as it
1810 can't be debugged, we can't read its registers, etc.
1811 This is the main reason we check for zombie leaders
1812 disappearing.
1813
1814 #2 - The whole thread-group/process exited (a group exit,
1815 via e.g. exit(3), and there is (or will be shortly) an
1816 exit reported for each thread in the process, and then
1817 finally an exit for the leader once the non-leaders are
1818 reaped.
1819
1820 #3 - There are 3 or more threads in the group, and a
1821 thread other than the leader exec'd. See comments on
1822 exec events at the top of the file.
1823
1824 Ideally we would never delete the leader for case #2.
1825 Instead, we want to collect the exit status of each
1826 non-leader thread, and then finally collect the exit
1827 status of the leader as normal and use its exit code as
1828 whole-process exit code. Unfortunately, there's no
1829 race-free way to distinguish cases #1 and #2. We can't
1830 assume the exit events for the non-leaders threads are
1831 already pending in the kernel, nor can we assume the
1832 non-leader threads are in zombie state already. Between
1833 the leader becoming zombie and the non-leaders exiting
1834 and becoming zombie themselves, there's a small time
1835 window, so such a check would be racy. Temporarily
1836 pausing all threads and checking to see if all threads
1837 exit or not before re-resuming them would work in the
1838 case that all threads are running right now, but it
1839 wouldn't work if some thread is currently already
1840 ptrace-stopped, e.g., due to scheduler-locking.
1841
1842 So what we do is we delete the leader anyhow, and then
1843 later on when we see its exit status, we re-add it back.
1844 We also make sure that we only report a whole-process
1845 exit when we see the leader exiting, as opposed to when
1846 the last LWP in the LWP list exits, which can be a
1847 non-leader if we deleted the leader here. */
aa40a989 1848 threads_debug_printf ("Thread group leader %d zombie "
8a841a35
PA
1849 "(it exited, or another thread execd), "
1850 "deleting it.",
aa40a989 1851 leader_pid);
aa40a989
PA
1852 delete_lwp (leader_lp);
1853 }
9179355e 1854 });
fa96cb38 1855}
c3adc08c 1856
a1385b7b
SM
1857/* Callback for `find_thread'. Returns the first LWP that is not
1858 stopped. */
d50171e4 1859
a1385b7b
SM
1860static bool
1861not_stopped_callback (thread_info *thread, ptid_t filter)
fa96cb38 1862{
a1385b7b
SM
1863 if (!thread->id.matches (filter))
1864 return false;
47c0c975 1865
a1385b7b 1866 lwp_info *lwp = get_thread_lwp (thread);
fa96cb38 1867
a1385b7b 1868 return !lwp->stopped;
0d62e5e8 1869}
611cb4a5 1870
863d01bd
PA
1871/* Increment LWP's suspend count. */
1872
1873static void
1874lwp_suspended_inc (struct lwp_info *lwp)
1875{
1876 lwp->suspended++;
1877
c058728c
SM
1878 if (lwp->suspended > 4)
1879 threads_debug_printf
1880 ("LWP %ld has a suspiciously high suspend count, suspended=%d",
1881 lwpid_of (get_lwp_thread (lwp)), lwp->suspended);
863d01bd
PA
1882}
1883
1884/* Decrement LWP's suspend count. */
1885
1886static void
1887lwp_suspended_decr (struct lwp_info *lwp)
1888{
1889 lwp->suspended--;
1890
1891 if (lwp->suspended < 0)
1892 {
1893 struct thread_info *thread = get_lwp_thread (lwp);
1894
f34652de 1895 internal_error ("unsuspend LWP %ld, suspended=%d\n", lwpid_of (thread),
863d01bd
PA
1896 lwp->suspended);
1897 }
1898}
1899
219f2f23
PA
1900/* This function should only be called if the LWP got a SIGTRAP.
1901
1902 Handle any tracepoint steps or hits. Return true if a tracepoint
1903 event was handled, 0 otherwise. */
1904
1905static int
1906handle_tracepoints (struct lwp_info *lwp)
1907{
1908 struct thread_info *tinfo = get_lwp_thread (lwp);
1909 int tpoint_related_event = 0;
1910
582511be
PA
1911 gdb_assert (lwp->suspended == 0);
1912
7984d532
PA
1913 /* If this tracepoint hit causes a tracing stop, we'll immediately
1914 uninsert tracepoints. To do this, we temporarily pause all
1915 threads, unpatch away, and then unpause threads. We need to make
1916 sure the unpausing doesn't resume LWP too. */
863d01bd 1917 lwp_suspended_inc (lwp);
7984d532 1918
219f2f23
PA
1919 /* And we need to be sure that any all-threads-stopping doesn't try
1920 to move threads out of the jump pads, as it could deadlock the
1921 inferior (LWP could be in the jump pad, maybe even holding the
1922 lock.) */
1923
1924 /* Do any necessary step collect actions. */
1925 tpoint_related_event |= tracepoint_finished_step (tinfo, lwp->stop_pc);
1926
fa593d66
PA
1927 tpoint_related_event |= handle_tracepoint_bkpts (tinfo, lwp->stop_pc);
1928
219f2f23
PA
1929 /* See if we just hit a tracepoint and do its main collect
1930 actions. */
1931 tpoint_related_event |= tracepoint_was_hit (tinfo, lwp->stop_pc);
1932
863d01bd 1933 lwp_suspended_decr (lwp);
7984d532
PA
1934
1935 gdb_assert (lwp->suspended == 0);
229d26fc
SM
1936 gdb_assert (!stabilizing_threads
1937 || (lwp->collecting_fast_tracepoint
1938 != fast_tpoint_collect_result::not_collecting));
7984d532 1939
219f2f23
PA
1940 if (tpoint_related_event)
1941 {
c058728c 1942 threads_debug_printf ("got a tracepoint event");
219f2f23
PA
1943 return 1;
1944 }
1945
1946 return 0;
1947}
1948
13e567af
TBA
1949fast_tpoint_collect_result
1950linux_process_target::linux_fast_tracepoint_collecting
1951 (lwp_info *lwp, fast_tpoint_collect_status *status)
fa593d66
PA
1952{
1953 CORE_ADDR thread_area;
d86d4aaf 1954 struct thread_info *thread = get_lwp_thread (lwp);
fa593d66 1955
fa593d66
PA
1956 /* Get the thread area address. This is used to recognize which
1957 thread is which when tracing with the in-process agent library.
1958 We don't read anything from the address, and treat it as opaque;
1959 it's the address itself that we assume is unique per-thread. */
13e567af 1960 if (low_get_thread_area (lwpid_of (thread), &thread_area) == -1)
229d26fc 1961 return fast_tpoint_collect_result::not_collecting;
fa593d66
PA
1962
1963 return fast_tracepoint_collecting (thread_area, lwp->stop_pc, status);
1964}
1965
13e567af
TBA
1966int
1967linux_process_target::low_get_thread_area (int lwpid, CORE_ADDR *addrp)
1968{
1969 return -1;
1970}
1971
d16f3f6c
TBA
1972bool
1973linux_process_target::maybe_move_out_of_jump_pad (lwp_info *lwp, int *wstat)
fa593d66 1974{
24583e45
TBA
1975 scoped_restore_current_thread restore_thread;
1976 switch_to_thread (get_lwp_thread (lwp));
fa593d66
PA
1977
1978 if ((wstat == NULL
1979 || (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) != SIGTRAP))
1980 && supports_fast_tracepoints ()
58b4daa5 1981 && agent_loaded_p ())
fa593d66
PA
1982 {
1983 struct fast_tpoint_collect_status status;
fa593d66 1984
c058728c
SM
1985 threads_debug_printf
1986 ("Checking whether LWP %ld needs to move out of the jump pad.",
1987 lwpid_of (current_thread));
fa593d66 1988
229d26fc
SM
1989 fast_tpoint_collect_result r
1990 = linux_fast_tracepoint_collecting (lwp, &status);
fa593d66
PA
1991
1992 if (wstat == NULL
1993 || (WSTOPSIG (*wstat) != SIGILL
1994 && WSTOPSIG (*wstat) != SIGFPE
1995 && WSTOPSIG (*wstat) != SIGSEGV
1996 && WSTOPSIG (*wstat) != SIGBUS))
1997 {
1998 lwp->collecting_fast_tracepoint = r;
1999
229d26fc 2000 if (r != fast_tpoint_collect_result::not_collecting)
fa593d66 2001 {
229d26fc
SM
2002 if (r == fast_tpoint_collect_result::before_insn
2003 && lwp->exit_jump_pad_bkpt == NULL)
fa593d66
PA
2004 {
2005 /* Haven't executed the original instruction yet.
2006 Set breakpoint there, and wait till it's hit,
2007 then single-step until exiting the jump pad. */
2008 lwp->exit_jump_pad_bkpt
2009 = set_breakpoint_at (status.adjusted_insn_addr, NULL);
2010 }
2011
c058728c
SM
2012 threads_debug_printf
2013 ("Checking whether LWP %ld needs to move out of the jump pad..."
2014 " it does", lwpid_of (current_thread));
fa593d66 2015
d16f3f6c 2016 return true;
fa593d66
PA
2017 }
2018 }
2019 else
2020 {
2021 /* If we get a synchronous signal while collecting, *and*
2022 while executing the (relocated) original instruction,
2023 reset the PC to point at the tpoint address, before
2024 reporting to GDB. Otherwise, it's an IPA lib bug: just
2025 report the signal to GDB, and pray for the best. */
2026
229d26fc
SM
2027 lwp->collecting_fast_tracepoint
2028 = fast_tpoint_collect_result::not_collecting;
fa593d66 2029
229d26fc 2030 if (r != fast_tpoint_collect_result::not_collecting
fa593d66
PA
2031 && (status.adjusted_insn_addr <= lwp->stop_pc
2032 && lwp->stop_pc < status.adjusted_insn_addr_end))
2033 {
2034 siginfo_t info;
2035 struct regcache *regcache;
2036
2037 /* The si_addr on a few signals references the address
2038 of the faulting instruction. Adjust that as
2039 well. */
2040 if ((WSTOPSIG (*wstat) == SIGILL
2041 || WSTOPSIG (*wstat) == SIGFPE
2042 || WSTOPSIG (*wstat) == SIGBUS
2043 || WSTOPSIG (*wstat) == SIGSEGV)
0bfdf32f 2044 && ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
b8e1b30e 2045 (PTRACE_TYPE_ARG3) 0, &info) == 0
fa593d66
PA
2046 /* Final check just to make sure we don't clobber
2047 the siginfo of non-kernel-sent signals. */
2048 && (uintptr_t) info.si_addr == lwp->stop_pc)
2049 {
2050 info.si_addr = (void *) (uintptr_t) status.tpoint_addr;
0bfdf32f 2051 ptrace (PTRACE_SETSIGINFO, lwpid_of (current_thread),
b8e1b30e 2052 (PTRACE_TYPE_ARG3) 0, &info);
fa593d66
PA
2053 }
2054
0bfdf32f 2055 regcache = get_thread_regcache (current_thread, 1);
bf9ae9d8 2056 low_set_pc (regcache, status.tpoint_addr);
fa593d66
PA
2057 lwp->stop_pc = status.tpoint_addr;
2058
2059 /* Cancel any fast tracepoint lock this thread was
2060 holding. */
2061 force_unlock_trace_buffer ();
2062 }
2063
2064 if (lwp->exit_jump_pad_bkpt != NULL)
2065 {
c058728c
SM
2066 threads_debug_printf
2067 ("Cancelling fast exit-jump-pad: removing bkpt."
2068 "stopping all threads momentarily.");
fa593d66
PA
2069
2070 stop_all_lwps (1, lwp);
fa593d66
PA
2071
2072 delete_breakpoint (lwp->exit_jump_pad_bkpt);
2073 lwp->exit_jump_pad_bkpt = NULL;
2074
2075 unstop_all_lwps (1, lwp);
2076
2077 gdb_assert (lwp->suspended >= 0);
2078 }
2079 }
2080 }
2081
c058728c
SM
2082 threads_debug_printf
2083 ("Checking whether LWP %ld needs to move out of the jump pad... no",
2084 lwpid_of (current_thread));
0cccb683 2085
d16f3f6c 2086 return false;
fa593d66
PA
2087}
2088
2089/* Enqueue one signal in the "signals to report later when out of the
2090 jump pad" list. */
2091
2092static void
2093enqueue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
2094{
d86d4aaf 2095 struct thread_info *thread = get_lwp_thread (lwp);
fa593d66 2096
c058728c
SM
2097 threads_debug_printf ("Deferring signal %d for LWP %ld.",
2098 WSTOPSIG (*wstat), lwpid_of (thread));
fa593d66
PA
2099
2100 if (debug_threads)
2101 {
013e3554 2102 for (const auto &sig : lwp->pending_signals_to_report)
c058728c 2103 threads_debug_printf (" Already queued %d", sig.signal);
fa593d66 2104
c058728c 2105 threads_debug_printf (" (no more currently queued signals)");
fa593d66
PA
2106 }
2107
1a981360
PA
2108 /* Don't enqueue non-RT signals if they are already in the deferred
2109 queue. (SIGSTOP being the easiest signal to see ending up here
2110 twice) */
2111 if (WSTOPSIG (*wstat) < __SIGRTMIN)
2112 {
013e3554 2113 for (const auto &sig : lwp->pending_signals_to_report)
1a981360 2114 {
013e3554 2115 if (sig.signal == WSTOPSIG (*wstat))
1a981360 2116 {
c058728c
SM
2117 threads_debug_printf
2118 ("Not requeuing already queued non-RT signal %d for LWP %ld",
2119 sig.signal, lwpid_of (thread));
1a981360
PA
2120 return;
2121 }
2122 }
2123 }
2124
013e3554 2125 lwp->pending_signals_to_report.emplace_back (WSTOPSIG (*wstat));
8d749320 2126
d86d4aaf 2127 ptrace (PTRACE_GETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
013e3554 2128 &lwp->pending_signals_to_report.back ().info);
fa593d66
PA
2129}
2130
2131/* Dequeue one signal from the "signals to report later when out of
2132 the jump pad" list. */
2133
2134static int
2135dequeue_one_deferred_signal (struct lwp_info *lwp, int *wstat)
2136{
d86d4aaf
DE
2137 struct thread_info *thread = get_lwp_thread (lwp);
2138
013e3554 2139 if (!lwp->pending_signals_to_report.empty ())
fa593d66 2140 {
013e3554 2141 const pending_signal &p_sig = lwp->pending_signals_to_report.front ();
fa593d66 2142
013e3554
TBA
2143 *wstat = W_STOPCODE (p_sig.signal);
2144 if (p_sig.info.si_signo != 0)
d86d4aaf 2145 ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
013e3554
TBA
2146 &p_sig.info);
2147
2148 lwp->pending_signals_to_report.pop_front ();
fa593d66 2149
c058728c
SM
2150 threads_debug_printf ("Reporting deferred signal %d for LWP %ld.",
2151 WSTOPSIG (*wstat), lwpid_of (thread));
fa593d66
PA
2152
2153 if (debug_threads)
2154 {
013e3554 2155 for (const auto &sig : lwp->pending_signals_to_report)
c058728c 2156 threads_debug_printf (" Still queued %d", sig.signal);
fa593d66 2157
c058728c 2158 threads_debug_printf (" (no more queued signals)");
fa593d66
PA
2159 }
2160
2161 return 1;
2162 }
2163
2164 return 0;
2165}
2166
ac1bbaca
TBA
2167bool
2168linux_process_target::check_stopped_by_watchpoint (lwp_info *child)
582511be 2169{
24583e45
TBA
2170 scoped_restore_current_thread restore_thread;
2171 switch_to_thread (get_lwp_thread (child));
d50171e4 2172
ac1bbaca
TBA
2173 if (low_stopped_by_watchpoint ())
2174 {
2175 child->stop_reason = TARGET_STOPPED_BY_WATCHPOINT;
2176 child->stopped_data_address = low_stopped_data_address ();
2177 }
582511be 2178
ac1bbaca
TBA
2179 return child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
2180}
d50171e4 2181
ac1bbaca
TBA
2182bool
2183linux_process_target::low_stopped_by_watchpoint ()
2184{
2185 return false;
2186}
d50171e4 2187
ac1bbaca
TBA
2188CORE_ADDR
2189linux_process_target::low_stopped_data_address ()
2190{
2191 return 0;
c4d9ceb6
YQ
2192}
2193
de0d863e
DB
2194/* Return the ptrace options that we want to try to enable. */
2195
2196static int
2197linux_low_ptrace_options (int attached)
2198{
c12a5089 2199 client_state &cs = get_client_state ();
de0d863e
DB
2200 int options = 0;
2201
2202 if (!attached)
2203 options |= PTRACE_O_EXITKILL;
2204
c12a5089 2205 if (cs.report_fork_events)
de0d863e
DB
2206 options |= PTRACE_O_TRACEFORK;
2207
c12a5089 2208 if (cs.report_vfork_events)
c269dbdb
DB
2209 options |= (PTRACE_O_TRACEVFORK | PTRACE_O_TRACEVFORKDONE);
2210
c12a5089 2211 if (cs.report_exec_events)
94585166
DB
2212 options |= PTRACE_O_TRACEEXEC;
2213
82075af2
JS
2214 options |= PTRACE_O_TRACESYSGOOD;
2215
de0d863e
DB
2216 return options;
2217}
2218
1a48f002 2219void
d16f3f6c 2220linux_process_target::filter_event (int lwpid, int wstat)
fa96cb38 2221{
c12a5089 2222 client_state &cs = get_client_state ();
fa96cb38
PA
2223 struct lwp_info *child;
2224 struct thread_info *thread;
582511be 2225 int have_stop_pc = 0;
fa96cb38 2226
f2907e49 2227 child = find_lwp_pid (ptid_t (lwpid));
fa96cb38 2228
5406bc3f
PA
2229 /* Check for events reported by anything not in our LWP list. */
2230 if (child == nullptr)
94585166 2231 {
5406bc3f
PA
2232 if (WIFSTOPPED (wstat))
2233 {
2234 if (WSTOPSIG (wstat) == SIGTRAP
2235 && linux_ptrace_get_extended_event (wstat) == PTRACE_EVENT_EXEC)
2236 {
2237 /* A non-leader thread exec'ed after we've seen the
2238 leader zombie, and removed it from our lists (in
2239 check_zombie_leaders). The non-leader thread changes
2240 its tid to the tgid. */
2241 threads_debug_printf
2242 ("Re-adding thread group leader LWP %d after exec.",
2243 lwpid);
94585166 2244
5406bc3f
PA
2245 child = add_lwp (ptid_t (lwpid, lwpid));
2246 child->stopped = 1;
2247 switch_to_thread (child->thread);
2248 }
2249 else
2250 {
2251 /* A process we are controlling has forked and the new
2252 child's stop was reported to us by the kernel. Save
2253 its PID and go back to waiting for the fork event to
2254 be reported - the stopped process might be returned
2255 from waitpid before or after the fork event is. */
2256 threads_debug_printf
2257 ("Saving LWP %d status %s in stopped_pids list",
2258 lwpid, status_to_str (wstat).c_str ());
2259 add_to_pid_list (&stopped_pids, lwpid, wstat);
2260 }
2261 }
2262 else
2263 {
2264 /* Don't report an event for the exit of an LWP not in our
2265 list, i.e. not part of any inferior we're debugging.
2266 This can happen if we detach from a program we originally
8a841a35
PA
2267 forked and then it exits. However, note that we may have
2268 earlier deleted a leader of an inferior we're debugging,
2269 in check_zombie_leaders. Re-add it back here if so. */
2270 find_process ([&] (process_info *proc)
2271 {
2272 if (proc->pid == lwpid)
2273 {
2274 threads_debug_printf
2275 ("Re-adding thread group leader LWP %d after exit.",
2276 lwpid);
2277
2278 child = add_lwp (ptid_t (lwpid, lwpid));
2279 return true;
2280 }
2281 return false;
2282 });
5406bc3f 2283 }
94585166 2284
5406bc3f
PA
2285 if (child == nullptr)
2286 return;
fa96cb38 2287 }
fa96cb38
PA
2288
2289 thread = get_lwp_thread (child);
2290
2291 child->stopped = 1;
2292
2293 child->last_status = wstat;
2294
582511be
PA
2295 /* Check if the thread has exited. */
2296 if ((WIFEXITED (wstat) || WIFSIGNALED (wstat)))
2297 {
c058728c 2298 threads_debug_printf ("%d exited", lwpid);
f50bf8e5
YQ
2299
2300 if (finish_step_over (child))
2301 {
2302 /* Unsuspend all other LWPs, and set them back running again. */
2303 unsuspend_all_lwps (child);
2304 }
2305
8a841a35
PA
2306 /* If this is not the leader LWP, then the exit signal was not
2307 the end of the debugged application and should be ignored,
2308 unless GDB wants to hear about thread exits. */
2309 if (cs.report_thread_events || is_leader (thread))
582511be 2310 {
65706a29
PA
2311 /* Since events are serialized to GDB core, and we can't
2312 report this one right now. Leave the status pending for
2313 the next time we're able to report it. */
2314 mark_lwp_dead (child, wstat);
1a48f002 2315 return;
582511be
PA
2316 }
2317 else
2318 {
65706a29 2319 delete_lwp (child);
1a48f002 2320 return;
582511be
PA
2321 }
2322 }
2323
2324 gdb_assert (WIFSTOPPED (wstat));
2325
fa96cb38
PA
2326 if (WIFSTOPPED (wstat))
2327 {
2328 struct process_info *proc;
2329
c06cbd92 2330 /* Architecture-specific setup after inferior is running. */
fa96cb38 2331 proc = find_process_pid (pid_of (thread));
c06cbd92 2332 if (proc->tdesc == NULL)
fa96cb38 2333 {
c06cbd92
YQ
2334 if (proc->attached)
2335 {
c06cbd92
YQ
2336 /* This needs to happen after we have attached to the
2337 inferior and it is stopped for the first time, but
2338 before we access any inferior registers. */
797bcff5 2339 arch_setup_thread (thread);
c06cbd92
YQ
2340 }
2341 else
2342 {
2343 /* The process is started, but GDBserver will do
2344 architecture-specific setup after the program stops at
2345 the first instruction. */
2346 child->status_pending_p = 1;
2347 child->status_pending = wstat;
1a48f002 2348 return;
c06cbd92 2349 }
fa96cb38
PA
2350 }
2351 }
2352
fa96cb38
PA
2353 if (WIFSTOPPED (wstat) && child->must_set_ptrace_flags)
2354 {
beed38b8 2355 struct process_info *proc = find_process_pid (pid_of (thread));
de0d863e 2356 int options = linux_low_ptrace_options (proc->attached);
beed38b8 2357
de0d863e 2358 linux_enable_event_reporting (lwpid, options);
fa96cb38
PA
2359 child->must_set_ptrace_flags = 0;
2360 }
2361
82075af2
JS
2362 /* Always update syscall_state, even if it will be filtered later. */
2363 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SYSCALL_SIGTRAP)
2364 {
2365 child->syscall_state
2366 = (child->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
2367 ? TARGET_WAITKIND_SYSCALL_RETURN
2368 : TARGET_WAITKIND_SYSCALL_ENTRY);
2369 }
2370 else
2371 {
2372 /* Almost all other ptrace-stops are known to be outside of system
2373 calls, with further exceptions in handle_extended_wait. */
2374 child->syscall_state = TARGET_WAITKIND_IGNORE;
2375 }
2376
e7ad2f14
PA
2377 /* Be careful to not overwrite stop_pc until save_stop_reason is
2378 called. */
fa96cb38 2379 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGTRAP
89a5711c 2380 && linux_is_extended_waitstatus (wstat))
fa96cb38 2381 {
582511be 2382 child->stop_pc = get_pc (child);
94585166 2383 if (handle_extended_wait (&child, wstat))
de0d863e
DB
2384 {
2385 /* The event has been handled, so just return without
2386 reporting it. */
1a48f002 2387 return;
de0d863e 2388 }
fa96cb38
PA
2389 }
2390
80aea927 2391 if (linux_wstatus_maybe_breakpoint (wstat))
582511be 2392 {
e7ad2f14 2393 if (save_stop_reason (child))
582511be
PA
2394 have_stop_pc = 1;
2395 }
2396
2397 if (!have_stop_pc)
2398 child->stop_pc = get_pc (child);
2399
fa96cb38
PA
2400 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGSTOP
2401 && child->stop_expected)
2402 {
c058728c
SM
2403 threads_debug_printf ("Expected stop.");
2404
fa96cb38
PA
2405 child->stop_expected = 0;
2406
2407 if (thread->last_resume_kind == resume_stop)
2408 {
2409 /* We want to report the stop to the core. Treat the
2410 SIGSTOP as a normal event. */
c058728c
SM
2411 threads_debug_printf ("resume_stop SIGSTOP caught for %s.",
2412 target_pid_to_str (ptid_of (thread)).c_str ());
fa96cb38
PA
2413 }
2414 else if (stopping_threads != NOT_STOPPING_THREADS)
2415 {
2416 /* Stopping threads. We don't want this SIGSTOP to end up
582511be 2417 pending. */
c058728c
SM
2418 threads_debug_printf ("SIGSTOP caught for %s while stopping threads.",
2419 target_pid_to_str (ptid_of (thread)).c_str ());
1a48f002 2420 return;
fa96cb38
PA
2421 }
2422 else
2423 {
2bf6fb9d 2424 /* This is a delayed SIGSTOP. Filter out the event. */
c058728c 2425 threads_debug_printf ("%s %s, 0, 0 (discard delayed SIGSTOP)",
2bf6fb9d 2426 child->stepping ? "step" : "continue",
61d7f128 2427 target_pid_to_str (ptid_of (thread)).c_str ());
2bf6fb9d 2428
df95181f 2429 resume_one_lwp (child, child->stepping, 0, NULL);
1a48f002 2430 return;
fa96cb38
PA
2431 }
2432 }
2433
582511be
PA
2434 child->status_pending_p = 1;
2435 child->status_pending = wstat;
1a48f002 2436 return;
fa96cb38
PA
2437}
2438
b31cdfa6
TBA
2439bool
2440linux_process_target::maybe_hw_step (thread_info *thread)
f79b145d 2441{
b31cdfa6
TBA
2442 if (supports_hardware_single_step ())
2443 return true;
f79b145d
YQ
2444 else
2445 {
3b9a79ef 2446 /* GDBserver must insert single-step breakpoint for software
f79b145d 2447 single step. */
3b9a79ef 2448 gdb_assert (has_single_step_breakpoints (thread));
b31cdfa6 2449 return false;
f79b145d
YQ
2450 }
2451}
2452
df95181f
TBA
2453void
2454linux_process_target::resume_stopped_resumed_lwps (thread_info *thread)
20ba1ce6 2455{
20ba1ce6
PA
2456 struct lwp_info *lp = get_thread_lwp (thread);
2457
2458 if (lp->stopped
863d01bd 2459 && !lp->suspended
20ba1ce6 2460 && !lp->status_pending_p
183be222 2461 && thread->last_status.kind () == TARGET_WAITKIND_IGNORE)
20ba1ce6 2462 {
8901d193
YQ
2463 int step = 0;
2464
2465 if (thread->last_resume_kind == resume_step)
2466 step = maybe_hw_step (thread);
20ba1ce6 2467
c058728c
SM
2468 threads_debug_printf ("resuming stopped-resumed LWP %s at %s: step=%d",
2469 target_pid_to_str (ptid_of (thread)).c_str (),
2470 paddress (lp->stop_pc), step);
20ba1ce6 2471
df95181f 2472 resume_one_lwp (lp, step, GDB_SIGNAL_0, NULL);
20ba1ce6
PA
2473 }
2474}
2475
d16f3f6c
TBA
2476int
2477linux_process_target::wait_for_event_filtered (ptid_t wait_ptid,
2478 ptid_t filter_ptid,
2479 int *wstatp, int options)
0d62e5e8 2480{
d86d4aaf 2481 struct thread_info *event_thread;
d50171e4 2482 struct lwp_info *event_child, *requested_child;
fa96cb38 2483 sigset_t block_mask, prev_mask;
d50171e4 2484
fa96cb38 2485 retry:
d86d4aaf
DE
2486 /* N.B. event_thread points to the thread_info struct that contains
2487 event_child. Keep them in sync. */
2488 event_thread = NULL;
d50171e4
PA
2489 event_child = NULL;
2490 requested_child = NULL;
0d62e5e8 2491
95954743 2492 /* Check for a lwp with a pending status. */
bd99dc85 2493
d7e15655 2494 if (filter_ptid == minus_one_ptid || filter_ptid.is_pid ())
0d62e5e8 2495 {
83e1b6c1
SM
2496 event_thread = find_thread_in_random ([&] (thread_info *thread)
2497 {
2498 return status_pending_p_callback (thread, filter_ptid);
2499 });
2500
d86d4aaf 2501 if (event_thread != NULL)
c058728c
SM
2502 {
2503 event_child = get_thread_lwp (event_thread);
2504 threads_debug_printf ("Got a pending child %ld", lwpid_of (event_thread));
2505 }
0d62e5e8 2506 }
d7e15655 2507 else if (filter_ptid != null_ptid)
0d62e5e8 2508 {
fa96cb38 2509 requested_child = find_lwp_pid (filter_ptid);
d50171e4 2510
bde24c0a 2511 if (stopping_threads == NOT_STOPPING_THREADS
fa593d66 2512 && requested_child->status_pending_p
229d26fc
SM
2513 && (requested_child->collecting_fast_tracepoint
2514 != fast_tpoint_collect_result::not_collecting))
fa593d66
PA
2515 {
2516 enqueue_one_deferred_signal (requested_child,
2517 &requested_child->status_pending);
2518 requested_child->status_pending_p = 0;
2519 requested_child->status_pending = 0;
df95181f 2520 resume_one_lwp (requested_child, 0, 0, NULL);
fa593d66
PA
2521 }
2522
2523 if (requested_child->suspended
2524 && requested_child->status_pending_p)
38e08fca 2525 {
f34652de 2526 internal_error ("requesting an event out of a"
38e08fca
GB
2527 " suspended child?");
2528 }
fa593d66 2529
d50171e4 2530 if (requested_child->status_pending_p)
d86d4aaf
DE
2531 {
2532 event_child = requested_child;
2533 event_thread = get_lwp_thread (event_child);
2534 }
0d62e5e8 2535 }
611cb4a5 2536
0d62e5e8
DJ
2537 if (event_child != NULL)
2538 {
c058728c
SM
2539 threads_debug_printf ("Got an event from pending child %ld (%04x)",
2540 lwpid_of (event_thread),
2541 event_child->status_pending);
2542
fa96cb38 2543 *wstatp = event_child->status_pending;
bd99dc85
PA
2544 event_child->status_pending_p = 0;
2545 event_child->status_pending = 0;
24583e45 2546 switch_to_thread (event_thread);
d86d4aaf 2547 return lwpid_of (event_thread);
0d62e5e8
DJ
2548 }
2549
fa96cb38
PA
2550 /* But if we don't find a pending event, we'll have to wait.
2551
2552 We only enter this loop if no process has a pending wait status.
2553 Thus any action taken in response to a wait status inside this
2554 loop is responding as soon as we detect the status, not after any
2555 pending events. */
d8301ad1 2556
fa96cb38
PA
2557 /* Make sure SIGCHLD is blocked until the sigsuspend below. Block
2558 all signals while here. */
2559 sigfillset (&block_mask);
21987b9c 2560 gdb_sigmask (SIG_BLOCK, &block_mask, &prev_mask);
fa96cb38 2561
582511be
PA
2562 /* Always pull all events out of the kernel. We'll randomly select
2563 an event LWP out of all that have events, to prevent
2564 starvation. */
fa96cb38 2565 while (event_child == NULL)
0d62e5e8 2566 {
fa96cb38 2567 pid_t ret = 0;
0d62e5e8 2568
fa96cb38
PA
2569 /* Always use -1 and WNOHANG, due to couple of a kernel/ptrace
2570 quirks:
0d62e5e8 2571
fa96cb38
PA
2572 - If the thread group leader exits while other threads in the
2573 thread group still exist, waitpid(TGID, ...) hangs. That
2574 waitpid won't return an exit status until the other threads
2575 in the group are reaped.
611cb4a5 2576
fa96cb38
PA
2577 - When a non-leader thread execs, that thread just vanishes
2578 without reporting an exit (so we'd hang if we waited for it
2579 explicitly in that case). The exec event is reported to
94585166 2580 the TGID pid. */
fa96cb38
PA
2581 errno = 0;
2582 ret = my_waitpid (-1, wstatp, options | WNOHANG);
d8301ad1 2583
c058728c
SM
2584 threads_debug_printf ("waitpid(-1, ...) returned %d, %s",
2585 ret, errno ? safe_strerror (errno) : "ERRNO-OK");
0d62e5e8 2586
fa96cb38 2587 if (ret > 0)
0d62e5e8 2588 {
c058728c
SM
2589 threads_debug_printf ("waitpid %ld received %s",
2590 (long) ret, status_to_str (*wstatp).c_str ());
89be2091 2591
582511be
PA
2592 /* Filter all events. IOW, leave all events pending. We'll
2593 randomly select an event LWP out of all that have events
2594 below. */
d16f3f6c 2595 filter_event (ret, *wstatp);
fa96cb38
PA
2596 /* Retry until nothing comes out of waitpid. A single
2597 SIGCHLD can indicate more than one child stopped. */
89be2091
DJ
2598 continue;
2599 }
2600
20ba1ce6
PA
2601 /* Now that we've pulled all events out of the kernel, resume
2602 LWPs that don't have an interesting event to report. */
2603 if (stopping_threads == NOT_STOPPING_THREADS)
df95181f
TBA
2604 for_each_thread ([this] (thread_info *thread)
2605 {
2606 resume_stopped_resumed_lwps (thread);
2607 });
20ba1ce6
PA
2608
2609 /* ... and find an LWP with a status to report to the core, if
2610 any. */
83e1b6c1
SM
2611 event_thread = find_thread_in_random ([&] (thread_info *thread)
2612 {
2613 return status_pending_p_callback (thread, filter_ptid);
2614 });
2615
582511be
PA
2616 if (event_thread != NULL)
2617 {
2618 event_child = get_thread_lwp (event_thread);
2619 *wstatp = event_child->status_pending;
2620 event_child->status_pending_p = 0;
2621 event_child->status_pending = 0;
2622 break;
2623 }
2624
fa96cb38
PA
2625 /* Check for zombie thread group leaders. Those can't be reaped
2626 until all other threads in the thread group are. */
2627 check_zombie_leaders ();
2628
a1385b7b
SM
2629 auto not_stopped = [&] (thread_info *thread)
2630 {
2631 return not_stopped_callback (thread, wait_ptid);
2632 };
2633
fa96cb38
PA
2634 /* If there are no resumed children left in the set of LWPs we
2635 want to wait for, bail. We can't just block in
2636 waitpid/sigsuspend, because lwps might have been left stopped
2637 in trace-stop state, and we'd be stuck forever waiting for
2638 their status to change (which would only happen if we resumed
2639 them). Even if WNOHANG is set, this return code is preferred
2640 over 0 (below), as it is more detailed. */
a1385b7b 2641 if (find_thread (not_stopped) == NULL)
a6dbe5df 2642 {
c058728c
SM
2643 threads_debug_printf ("exit (no unwaited-for LWP)");
2644
21987b9c 2645 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
fa96cb38 2646 return -1;
a6dbe5df
PA
2647 }
2648
fa96cb38
PA
2649 /* No interesting event to report to the caller. */
2650 if ((options & WNOHANG))
24a09b5f 2651 {
c058728c 2652 threads_debug_printf ("WNOHANG set, no event found");
fa96cb38 2653
21987b9c 2654 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
fa96cb38 2655 return 0;
24a09b5f
DJ
2656 }
2657
fa96cb38 2658 /* Block until we get an event reported with SIGCHLD. */
c058728c 2659 threads_debug_printf ("sigsuspend'ing");
d50171e4 2660
fa96cb38 2661 sigsuspend (&prev_mask);
21987b9c 2662 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
fa96cb38
PA
2663 goto retry;
2664 }
d50171e4 2665
21987b9c 2666 gdb_sigmask (SIG_SETMASK, &prev_mask, NULL);
d50171e4 2667
24583e45 2668 switch_to_thread (event_thread);
d50171e4 2669
fa96cb38
PA
2670 return lwpid_of (event_thread);
2671}
2672
d16f3f6c
TBA
2673int
2674linux_process_target::wait_for_event (ptid_t ptid, int *wstatp, int options)
fa96cb38 2675{
d16f3f6c 2676 return wait_for_event_filtered (ptid, ptid, wstatp, options);
611cb4a5
DJ
2677}
2678
6bf5e0ba
PA
2679/* Select one LWP out of those that have events pending. */
2680
2681static void
2682select_event_lwp (struct lwp_info **orig_lp)
2683{
582511be
PA
2684 struct thread_info *event_thread = NULL;
2685
2686 /* In all-stop, give preference to the LWP that is being
2687 single-stepped. There will be at most one, and it's the LWP that
2688 the core is most interested in. If we didn't do this, then we'd
2689 have to handle pending step SIGTRAPs somehow in case the core
2690 later continues the previously-stepped thread, otherwise we'd
2691 report the pending SIGTRAP, and the core, not having stepped the
2692 thread, wouldn't understand what the trap was for, and therefore
2693 would report it to the user as a random signal. */
2694 if (!non_stop)
6bf5e0ba 2695 {
39a64da5
SM
2696 event_thread = find_thread ([] (thread_info *thread)
2697 {
2698 lwp_info *lp = get_thread_lwp (thread);
2699
183be222 2700 return (thread->last_status.kind () == TARGET_WAITKIND_IGNORE
39a64da5
SM
2701 && thread->last_resume_kind == resume_step
2702 && lp->status_pending_p);
2703 });
2704
582511be 2705 if (event_thread != NULL)
c058728c
SM
2706 threads_debug_printf
2707 ("Select single-step %s",
2708 target_pid_to_str (ptid_of (event_thread)).c_str ());
6bf5e0ba 2709 }
582511be 2710 if (event_thread == NULL)
6bf5e0ba
PA
2711 {
2712 /* No single-stepping LWP. Select one at random, out of those
dda83cd7 2713 which have had events. */
6bf5e0ba 2714
b0319eaa 2715 event_thread = find_thread_in_random ([&] (thread_info *thread)
39a64da5
SM
2716 {
2717 lwp_info *lp = get_thread_lwp (thread);
2718
b0319eaa 2719 /* Only resumed LWPs that have an event pending. */
183be222 2720 return (thread->last_status.kind () == TARGET_WAITKIND_IGNORE
b0319eaa 2721 && lp->status_pending_p);
39a64da5 2722 });
6bf5e0ba
PA
2723 }
2724
d86d4aaf 2725 if (event_thread != NULL)
6bf5e0ba 2726 {
d86d4aaf
DE
2727 struct lwp_info *event_lp = get_thread_lwp (event_thread);
2728
6bf5e0ba
PA
2729 /* Switch the event LWP. */
2730 *orig_lp = event_lp;
2731 }
2732}
2733
7984d532
PA
2734/* Decrement the suspend count of all LWPs, except EXCEPT, if non
2735 NULL. */
2736
2737static void
2738unsuspend_all_lwps (struct lwp_info *except)
2739{
139720c5
SM
2740 for_each_thread ([&] (thread_info *thread)
2741 {
2742 lwp_info *lwp = get_thread_lwp (thread);
2743
2744 if (lwp != except)
2745 lwp_suspended_decr (lwp);
2746 });
7984d532
PA
2747}
2748
5a6b0a41 2749static bool lwp_running (thread_info *thread);
fa593d66
PA
2750
2751/* Stabilize threads (move out of jump pads).
2752
2753 If a thread is midway collecting a fast tracepoint, we need to
2754 finish the collection and move it out of the jump pad before
2755 reporting the signal.
2756
2757 This avoids recursion while collecting (when a signal arrives
2758 midway, and the signal handler itself collects), which would trash
2759 the trace buffer. In case the user set a breakpoint in a signal
2760 handler, this avoids the backtrace showing the jump pad, etc..
2761 Most importantly, there are certain things we can't do safely if
2762 threads are stopped in a jump pad (or in its callee's). For
2763 example:
2764
2765 - starting a new trace run. A thread still collecting the
2766 previous run, could trash the trace buffer when resumed. The trace
2767 buffer control structures would have been reset but the thread had
2768 no way to tell. The thread could even midway memcpy'ing to the
2769 buffer, which would mean that when resumed, it would clobber the
2770 trace buffer that had been set for a new run.
2771
2772 - we can't rewrite/reuse the jump pads for new tracepoints
2773 safely. Say you do tstart while a thread is stopped midway while
2774 collecting. When the thread is later resumed, it finishes the
2775 collection, and returns to the jump pad, to execute the original
2776 instruction that was under the tracepoint jump at the time the
2777 older run had been started. If the jump pad had been rewritten
2778 since for something else in the new run, the thread would now
2779 execute the wrong / random instructions. */
2780
5c9eb2f2
TBA
2781void
2782linux_process_target::stabilize_threads ()
fa593d66 2783{
13e567af
TBA
2784 thread_info *thread_stuck = find_thread ([this] (thread_info *thread)
2785 {
2786 return stuck_in_jump_pad (thread);
2787 });
fa593d66 2788
d86d4aaf 2789 if (thread_stuck != NULL)
fa593d66 2790 {
c058728c
SM
2791 threads_debug_printf ("can't stabilize, LWP %ld is stuck in jump pad",
2792 lwpid_of (thread_stuck));
fa593d66
PA
2793 return;
2794 }
2795
24583e45 2796 scoped_restore_current_thread restore_thread;
fa593d66
PA
2797
2798 stabilizing_threads = 1;
2799
2800 /* Kick 'em all. */
d16f3f6c
TBA
2801 for_each_thread ([this] (thread_info *thread)
2802 {
2803 move_out_of_jump_pad (thread);
2804 });
fa593d66
PA
2805
2806 /* Loop until all are stopped out of the jump pads. */
5a6b0a41 2807 while (find_thread (lwp_running) != NULL)
fa593d66
PA
2808 {
2809 struct target_waitstatus ourstatus;
2810 struct lwp_info *lwp;
fa593d66
PA
2811 int wstat;
2812
2813 /* Note that we go through the full wait even loop. While
2814 moving threads out of jump pad, we need to be able to step
2815 over internal breakpoints and such. */
d16f3f6c 2816 wait_1 (minus_one_ptid, &ourstatus, 0);
fa593d66 2817
183be222 2818 if (ourstatus.kind () == TARGET_WAITKIND_STOPPED)
fa593d66 2819 {
0bfdf32f 2820 lwp = get_thread_lwp (current_thread);
fa593d66
PA
2821
2822 /* Lock it. */
863d01bd 2823 lwp_suspended_inc (lwp);
fa593d66 2824
183be222 2825 if (ourstatus.sig () != GDB_SIGNAL_0
0bfdf32f 2826 || current_thread->last_resume_kind == resume_stop)
fa593d66 2827 {
183be222 2828 wstat = W_STOPCODE (gdb_signal_to_host (ourstatus.sig ()));
fa593d66
PA
2829 enqueue_one_deferred_signal (lwp, &wstat);
2830 }
2831 }
2832 }
2833
fcdad592 2834 unsuspend_all_lwps (NULL);
fa593d66
PA
2835
2836 stabilizing_threads = 0;
2837
b4d51a55 2838 if (debug_threads)
fa593d66 2839 {
13e567af
TBA
2840 thread_stuck = find_thread ([this] (thread_info *thread)
2841 {
2842 return stuck_in_jump_pad (thread);
2843 });
fcb056a5 2844
d86d4aaf 2845 if (thread_stuck != NULL)
c058728c
SM
2846 threads_debug_printf
2847 ("couldn't stabilize, LWP %ld got stuck in jump pad",
2848 lwpid_of (thread_stuck));
fa593d66
PA
2849 }
2850}
2851
582511be
PA
2852/* Convenience function that is called when the kernel reports an
2853 event that is not passed out to GDB. */
2854
2855static ptid_t
2856ignore_event (struct target_waitstatus *ourstatus)
2857{
2858 /* If we got an event, there may still be others, as a single
2859 SIGCHLD can indicate more than one child stopped. This forces
2860 another target_wait call. */
2861 async_file_mark ();
2862
183be222 2863 ourstatus->set_ignore ();
582511be
PA
2864 return null_ptid;
2865}
2866
fd000fb3
TBA
2867ptid_t
2868linux_process_target::filter_exit_event (lwp_info *event_child,
2869 target_waitstatus *ourstatus)
65706a29 2870{
c12a5089 2871 client_state &cs = get_client_state ();
65706a29
PA
2872 struct thread_info *thread = get_lwp_thread (event_child);
2873 ptid_t ptid = ptid_of (thread);
2874
8a841a35 2875 if (!is_leader (thread))
65706a29 2876 {
c12a5089 2877 if (cs.report_thread_events)
183be222 2878 ourstatus->set_thread_exited (0);
65706a29 2879 else
183be222 2880 ourstatus->set_ignore ();
65706a29
PA
2881
2882 delete_lwp (event_child);
2883 }
2884 return ptid;
2885}
2886
82075af2
JS
2887/* Returns 1 if GDB is interested in any event_child syscalls. */
2888
2889static int
2890gdb_catching_syscalls_p (struct lwp_info *event_child)
2891{
2892 struct thread_info *thread = get_lwp_thread (event_child);
2893 struct process_info *proc = get_thread_process (thread);
2894
f27866ba 2895 return !proc->syscalls_to_catch.empty ();
82075af2
JS
2896}
2897
9eedd27d
TBA
2898bool
2899linux_process_target::gdb_catch_this_syscall (lwp_info *event_child)
82075af2 2900{
4cc32bec 2901 int sysno;
82075af2
JS
2902 struct thread_info *thread = get_lwp_thread (event_child);
2903 struct process_info *proc = get_thread_process (thread);
2904
f27866ba 2905 if (proc->syscalls_to_catch.empty ())
9eedd27d 2906 return false;
82075af2 2907
f27866ba 2908 if (proc->syscalls_to_catch[0] == ANY_SYSCALL)
9eedd27d 2909 return true;
82075af2 2910
4cc32bec 2911 get_syscall_trapinfo (event_child, &sysno);
f27866ba
SM
2912
2913 for (int iter : proc->syscalls_to_catch)
82075af2 2914 if (iter == sysno)
9eedd27d 2915 return true;
82075af2 2916
9eedd27d 2917 return false;
82075af2
JS
2918}
2919
d16f3f6c
TBA
2920ptid_t
2921linux_process_target::wait_1 (ptid_t ptid, target_waitstatus *ourstatus,
b60cea74 2922 target_wait_flags target_options)
da6d8c04 2923{
c058728c
SM
2924 THREADS_SCOPED_DEBUG_ENTER_EXIT;
2925
c12a5089 2926 client_state &cs = get_client_state ();
e5f1222d 2927 int w;
fc7238bb 2928 struct lwp_info *event_child;
bd99dc85 2929 int options;
bd99dc85 2930 int pid;
6bf5e0ba
PA
2931 int step_over_finished;
2932 int bp_explains_trap;
2933 int maybe_internal_trap;
2934 int report_to_gdb;
219f2f23 2935 int trace_event;
c2d6af84 2936 int in_step_range;
f2faf941 2937 int any_resumed;
bd99dc85 2938
c058728c 2939 threads_debug_printf ("[%s]", target_pid_to_str (ptid).c_str ());
87ce2a04 2940
bd99dc85
PA
2941 /* Translate generic target options into linux options. */
2942 options = __WALL;
2943 if (target_options & TARGET_WNOHANG)
2944 options |= WNOHANG;
0d62e5e8 2945
fa593d66
PA
2946 bp_explains_trap = 0;
2947 trace_event = 0;
c2d6af84 2948 in_step_range = 0;
183be222 2949 ourstatus->set_ignore ();
bd99dc85 2950
83e1b6c1
SM
2951 auto status_pending_p_any = [&] (thread_info *thread)
2952 {
2953 return status_pending_p_callback (thread, minus_one_ptid);
2954 };
2955
a1385b7b
SM
2956 auto not_stopped = [&] (thread_info *thread)
2957 {
2958 return not_stopped_callback (thread, minus_one_ptid);
2959 };
2960
f2faf941 2961 /* Find a resumed LWP, if any. */
83e1b6c1 2962 if (find_thread (status_pending_p_any) != NULL)
f2faf941 2963 any_resumed = 1;
a1385b7b 2964 else if (find_thread (not_stopped) != NULL)
f2faf941
PA
2965 any_resumed = 1;
2966 else
2967 any_resumed = 0;
2968
d7e15655 2969 if (step_over_bkpt == null_ptid)
d16f3f6c 2970 pid = wait_for_event (ptid, &w, options);
6bf5e0ba
PA
2971 else
2972 {
c058728c
SM
2973 threads_debug_printf ("step_over_bkpt set [%s], doing a blocking wait",
2974 target_pid_to_str (step_over_bkpt).c_str ());
d16f3f6c 2975 pid = wait_for_event (step_over_bkpt, &w, options & ~WNOHANG);
6bf5e0ba
PA
2976 }
2977
f2faf941 2978 if (pid == 0 || (pid == -1 && !any_resumed))
87ce2a04 2979 {
fa96cb38
PA
2980 gdb_assert (target_options & TARGET_WNOHANG);
2981
c058728c 2982 threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_IGNORE");
fa96cb38 2983
183be222 2984 ourstatus->set_ignore ();
87ce2a04
DE
2985 return null_ptid;
2986 }
fa96cb38
PA
2987 else if (pid == -1)
2988 {
c058728c 2989 threads_debug_printf ("ret = null_ptid, TARGET_WAITKIND_NO_RESUMED");
bd99dc85 2990
183be222 2991 ourstatus->set_no_resumed ();
fa96cb38
PA
2992 return null_ptid;
2993 }
0d62e5e8 2994
0bfdf32f 2995 event_child = get_thread_lwp (current_thread);
0d62e5e8 2996
d16f3f6c 2997 /* wait_for_event only returns an exit status for the last
fa96cb38
PA
2998 child of a process. Report it. */
2999 if (WIFEXITED (w) || WIFSIGNALED (w))
da6d8c04 3000 {
fa96cb38 3001 if (WIFEXITED (w))
0d62e5e8 3002 {
183be222 3003 ourstatus->set_exited (WEXITSTATUS (w));
bd99dc85 3004
c058728c
SM
3005 threads_debug_printf
3006 ("ret = %s, exited with retcode %d",
3007 target_pid_to_str (ptid_of (current_thread)).c_str (),
3008 WEXITSTATUS (w));
fa96cb38
PA
3009 }
3010 else
3011 {
183be222 3012 ourstatus->set_signalled (gdb_signal_from_host (WTERMSIG (w)));
5b1c542e 3013
c058728c
SM
3014 threads_debug_printf
3015 ("ret = %s, terminated with signal %d",
3016 target_pid_to_str (ptid_of (current_thread)).c_str (),
3017 WTERMSIG (w));
0d62e5e8 3018 }
fa96cb38 3019
183be222 3020 if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
65706a29
PA
3021 return filter_exit_event (event_child, ourstatus);
3022
0bfdf32f 3023 return ptid_of (current_thread);
da6d8c04
DJ
3024 }
3025
2d97cd35
AT
3026 /* If step-over executes a breakpoint instruction, in the case of a
3027 hardware single step it means a gdb/gdbserver breakpoint had been
3028 planted on top of a permanent breakpoint, in the case of a software
3029 single step it may just mean that gdbserver hit the reinsert breakpoint.
e7ad2f14 3030 The PC has been adjusted by save_stop_reason to point at
2d97cd35
AT
3031 the breakpoint address.
3032 So in the case of the hardware single step advance the PC manually
3033 past the breakpoint and in the case of software single step advance only
3b9a79ef 3034 if it's not the single_step_breakpoint we are hitting.
2d97cd35
AT
3035 This avoids that a program would keep trapping a permanent breakpoint
3036 forever. */
d7e15655 3037 if (step_over_bkpt != null_ptid
2d97cd35
AT
3038 && event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
3039 && (event_child->stepping
3b9a79ef 3040 || !single_step_breakpoint_inserted_here (event_child->stop_pc)))
8090aef2 3041 {
dd373349
AT
3042 int increment_pc = 0;
3043 int breakpoint_kind = 0;
3044 CORE_ADDR stop_pc = event_child->stop_pc;
3045
d16f3f6c
TBA
3046 breakpoint_kind = breakpoint_kind_from_current_state (&stop_pc);
3047 sw_breakpoint_from_kind (breakpoint_kind, &increment_pc);
8090aef2 3048
c058728c
SM
3049 threads_debug_printf
3050 ("step-over for %s executed software breakpoint",
3051 target_pid_to_str (ptid_of (current_thread)).c_str ());
8090aef2
PA
3052
3053 if (increment_pc != 0)
3054 {
3055 struct regcache *regcache
3056 = get_thread_regcache (current_thread, 1);
3057
3058 event_child->stop_pc += increment_pc;
bf9ae9d8 3059 low_set_pc (regcache, event_child->stop_pc);
8090aef2 3060
d7146cda 3061 if (!low_breakpoint_at (event_child->stop_pc))
15c66dd6 3062 event_child->stop_reason = TARGET_STOPPED_BY_NO_REASON;
8090aef2
PA
3063 }
3064 }
3065
6bf5e0ba
PA
3066 /* If this event was not handled before, and is not a SIGTRAP, we
3067 report it. SIGILL and SIGSEGV are also treated as traps in case
3068 a breakpoint is inserted at the current PC. If this target does
3069 not support internal breakpoints at all, we also report the
3070 SIGTRAP without further processing; it's of no concern to us. */
3071 maybe_internal_trap
bf9ae9d8 3072 = (low_supports_breakpoints ()
6bf5e0ba
PA
3073 && (WSTOPSIG (w) == SIGTRAP
3074 || ((WSTOPSIG (w) == SIGILL
3075 || WSTOPSIG (w) == SIGSEGV)
d7146cda 3076 && low_breakpoint_at (event_child->stop_pc))));
6bf5e0ba
PA
3077
3078 if (maybe_internal_trap)
3079 {
3080 /* Handle anything that requires bookkeeping before deciding to
3081 report the event or continue waiting. */
3082
3083 /* First check if we can explain the SIGTRAP with an internal
3084 breakpoint, or if we should possibly report the event to GDB.
3085 Do this before anything that may remove or insert a
3086 breakpoint. */
3087 bp_explains_trap = breakpoint_inserted_here (event_child->stop_pc);
3088
3089 /* We have a SIGTRAP, possibly a step-over dance has just
3090 finished. If so, tweak the state machine accordingly,
3b9a79ef
YQ
3091 reinsert breakpoints and delete any single-step
3092 breakpoints. */
6bf5e0ba
PA
3093 step_over_finished = finish_step_over (event_child);
3094
3095 /* Now invoke the callbacks of any internal breakpoints there. */
3096 check_breakpoints (event_child->stop_pc);
3097
219f2f23
PA
3098 /* Handle tracepoint data collecting. This may overflow the
3099 trace buffer, and cause a tracing stop, removing
3100 breakpoints. */
3101 trace_event = handle_tracepoints (event_child);
3102
6bf5e0ba 3103 if (bp_explains_trap)
c058728c 3104 threads_debug_printf ("Hit a gdbserver breakpoint.");
6bf5e0ba
PA
3105 }
3106 else
3107 {
3108 /* We have some other signal, possibly a step-over dance was in
3109 progress, and it should be cancelled too. */
3110 step_over_finished = finish_step_over (event_child);
fa593d66
PA
3111 }
3112
3113 /* We have all the data we need. Either report the event to GDB, or
3114 resume threads and keep waiting for more. */
3115
3116 /* If we're collecting a fast tracepoint, finish the collection and
3117 move out of the jump pad before delivering a signal. See
3118 linux_stabilize_threads. */
3119
3120 if (WIFSTOPPED (w)
3121 && WSTOPSIG (w) != SIGTRAP
3122 && supports_fast_tracepoints ()
58b4daa5 3123 && agent_loaded_p ())
fa593d66 3124 {
c058728c
SM
3125 threads_debug_printf ("Got signal %d for LWP %ld. Check if we need "
3126 "to defer or adjust it.",
3127 WSTOPSIG (w), lwpid_of (current_thread));
fa593d66
PA
3128
3129 /* Allow debugging the jump pad itself. */
0bfdf32f 3130 if (current_thread->last_resume_kind != resume_step
fa593d66
PA
3131 && maybe_move_out_of_jump_pad (event_child, &w))
3132 {
3133 enqueue_one_deferred_signal (event_child, &w);
3134
c058728c
SM
3135 threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad)",
3136 WSTOPSIG (w), lwpid_of (current_thread));
fa593d66 3137
df95181f 3138 resume_one_lwp (event_child, 0, 0, NULL);
582511be
PA
3139
3140 return ignore_event (ourstatus);
fa593d66
PA
3141 }
3142 }
219f2f23 3143
229d26fc
SM
3144 if (event_child->collecting_fast_tracepoint
3145 != fast_tpoint_collect_result::not_collecting)
fa593d66 3146 {
c058728c
SM
3147 threads_debug_printf
3148 ("LWP %ld was trying to move out of the jump pad (%d). "
3149 "Check if we're already there.",
3150 lwpid_of (current_thread),
3151 (int) event_child->collecting_fast_tracepoint);
fa593d66
PA
3152
3153 trace_event = 1;
3154
3155 event_child->collecting_fast_tracepoint
3156 = linux_fast_tracepoint_collecting (event_child, NULL);
3157
229d26fc
SM
3158 if (event_child->collecting_fast_tracepoint
3159 != fast_tpoint_collect_result::before_insn)
fa593d66
PA
3160 {
3161 /* No longer need this breakpoint. */
3162 if (event_child->exit_jump_pad_bkpt != NULL)
3163 {
c058728c
SM
3164 threads_debug_printf
3165 ("No longer need exit-jump-pad bkpt; removing it."
3166 "stopping all threads momentarily.");
fa593d66
PA
3167
3168 /* Other running threads could hit this breakpoint.
3169 We don't handle moribund locations like GDB does,
3170 instead we always pause all threads when removing
3171 breakpoints, so that any step-over or
3172 decr_pc_after_break adjustment is always taken
3173 care of while the breakpoint is still
3174 inserted. */
3175 stop_all_lwps (1, event_child);
fa593d66
PA
3176
3177 delete_breakpoint (event_child->exit_jump_pad_bkpt);
3178 event_child->exit_jump_pad_bkpt = NULL;
3179
3180 unstop_all_lwps (1, event_child);
3181
3182 gdb_assert (event_child->suspended >= 0);
3183 }
3184 }
3185
229d26fc
SM
3186 if (event_child->collecting_fast_tracepoint
3187 == fast_tpoint_collect_result::not_collecting)
fa593d66 3188 {
c058728c
SM
3189 threads_debug_printf
3190 ("fast tracepoint finished collecting successfully.");
fa593d66
PA
3191
3192 /* We may have a deferred signal to report. */
3193 if (dequeue_one_deferred_signal (event_child, &w))
c058728c 3194 threads_debug_printf ("dequeued one signal.");
3c11dd79 3195 else
fa593d66 3196 {
c058728c 3197 threads_debug_printf ("no deferred signals.");
fa593d66
PA
3198
3199 if (stabilizing_threads)
3200 {
183be222 3201 ourstatus->set_stopped (GDB_SIGNAL_0);
87ce2a04 3202
c058728c
SM
3203 threads_debug_printf
3204 ("ret = %s, stopped while stabilizing threads",
3205 target_pid_to_str (ptid_of (current_thread)).c_str ());
87ce2a04 3206
0bfdf32f 3207 return ptid_of (current_thread);
fa593d66
PA
3208 }
3209 }
3210 }
6bf5e0ba
PA
3211 }
3212
e471f25b
PA
3213 /* Check whether GDB would be interested in this event. */
3214
82075af2
JS
3215 /* Check if GDB is interested in this syscall. */
3216 if (WIFSTOPPED (w)
3217 && WSTOPSIG (w) == SYSCALL_SIGTRAP
9eedd27d 3218 && !gdb_catch_this_syscall (event_child))
82075af2 3219 {
c058728c
SM
3220 threads_debug_printf ("Ignored syscall for LWP %ld.",
3221 lwpid_of (current_thread));
82075af2 3222
df95181f 3223 resume_one_lwp (event_child, event_child->stepping, 0, NULL);
edeeb602 3224
82075af2
JS
3225 return ignore_event (ourstatus);
3226 }
3227
e471f25b
PA
3228 /* If GDB is not interested in this signal, don't stop other
3229 threads, and don't report it to GDB. Just resume the inferior
3230 right away. We do this for threading-related signals as well as
3231 any that GDB specifically requested we ignore. But never ignore
3232 SIGSTOP if we sent it ourselves, and do not ignore signals when
3233 stepping - they may require special handling to skip the signal
c9587f88
AT
3234 handler. Also never ignore signals that could be caused by a
3235 breakpoint. */
e471f25b 3236 if (WIFSTOPPED (w)
0bfdf32f 3237 && current_thread->last_resume_kind != resume_step
e471f25b 3238 && (
1a981360 3239#if defined (USE_THREAD_DB) && !defined (__ANDROID__)
fe978cb0 3240 (current_process ()->priv->thread_db != NULL
e471f25b
PA
3241 && (WSTOPSIG (w) == __SIGRTMIN
3242 || WSTOPSIG (w) == __SIGRTMIN + 1))
3243 ||
3244#endif
c12a5089 3245 (cs.pass_signals[gdb_signal_from_host (WSTOPSIG (w))]
e471f25b 3246 && !(WSTOPSIG (w) == SIGSTOP
c9587f88
AT
3247 && current_thread->last_resume_kind == resume_stop)
3248 && !linux_wstatus_maybe_breakpoint (w))))
e471f25b
PA
3249 {
3250 siginfo_t info, *info_p;
3251
c058728c
SM
3252 threads_debug_printf ("Ignored signal %d for LWP %ld.",
3253 WSTOPSIG (w), lwpid_of (current_thread));
e471f25b 3254
0bfdf32f 3255 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread),
b8e1b30e 3256 (PTRACE_TYPE_ARG3) 0, &info) == 0)
e471f25b
PA
3257 info_p = &info;
3258 else
3259 info_p = NULL;
863d01bd
PA
3260
3261 if (step_over_finished)
3262 {
3263 /* We cancelled this thread's step-over above. We still
3264 need to unsuspend all other LWPs, and set them back
3265 running again while the signal handler runs. */
3266 unsuspend_all_lwps (event_child);
3267
3268 /* Enqueue the pending signal info so that proceed_all_lwps
3269 doesn't lose it. */
3270 enqueue_pending_signal (event_child, WSTOPSIG (w), info_p);
3271
3272 proceed_all_lwps ();
3273 }
3274 else
3275 {
df95181f
TBA
3276 resume_one_lwp (event_child, event_child->stepping,
3277 WSTOPSIG (w), info_p);
863d01bd 3278 }
edeeb602 3279
582511be 3280 return ignore_event (ourstatus);
e471f25b
PA
3281 }
3282
c2d6af84
PA
3283 /* Note that all addresses are always "out of the step range" when
3284 there's no range to begin with. */
3285 in_step_range = lwp_in_step_range (event_child);
3286
3287 /* If GDB wanted this thread to single step, and the thread is out
3288 of the step range, we always want to report the SIGTRAP, and let
3289 GDB handle it. Watchpoints should always be reported. So should
3290 signals we can't explain. A SIGTRAP we can't explain could be a
3291 GDB breakpoint --- we may or not support Z0 breakpoints. If we
3292 do, we're be able to handle GDB breakpoints on top of internal
3293 breakpoints, by handling the internal breakpoint and still
3294 reporting the event to GDB. If we don't, we're out of luck, GDB
863d01bd
PA
3295 won't see the breakpoint hit. If we see a single-step event but
3296 the thread should be continuing, don't pass the trap to gdb.
3297 That indicates that we had previously finished a single-step but
3298 left the single-step pending -- see
3299 complete_ongoing_step_over. */
6bf5e0ba 3300 report_to_gdb = (!maybe_internal_trap
0bfdf32f 3301 || (current_thread->last_resume_kind == resume_step
c2d6af84 3302 && !in_step_range)
15c66dd6 3303 || event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
863d01bd
PA
3304 || (!in_step_range
3305 && !bp_explains_trap
3306 && !trace_event
3307 && !step_over_finished
3308 && !(current_thread->last_resume_kind == resume_continue
3309 && event_child->stop_reason == TARGET_STOPPED_BY_SINGLE_STEP))
9f3a5c85 3310 || (gdb_breakpoint_here (event_child->stop_pc)
d3ce09f5 3311 && gdb_condition_true_at_breakpoint (event_child->stop_pc)
de0d863e 3312 && gdb_no_commands_at_breakpoint (event_child->stop_pc))
183be222 3313 || event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE);
d3ce09f5
SS
3314
3315 run_breakpoint_commands (event_child->stop_pc);
6bf5e0ba
PA
3316
3317 /* We found no reason GDB would want us to stop. We either hit one
3318 of our own breakpoints, or finished an internal step GDB
3319 shouldn't know about. */
3320 if (!report_to_gdb)
3321 {
c058728c
SM
3322 if (bp_explains_trap)
3323 threads_debug_printf ("Hit a gdbserver breakpoint.");
3324
3325 if (step_over_finished)
3326 threads_debug_printf ("Step-over finished.");
3327
3328 if (trace_event)
3329 threads_debug_printf ("Tracepoint event.");
3330
3331 if (lwp_in_step_range (event_child))
3332 threads_debug_printf ("Range stepping pc 0x%s [0x%s, 0x%s).",
3333 paddress (event_child->stop_pc),
3334 paddress (event_child->step_range_start),
3335 paddress (event_child->step_range_end));
6bf5e0ba
PA
3336
3337 /* We're not reporting this breakpoint to GDB, so apply the
3338 decr_pc_after_break adjustment to the inferior's regcache
3339 ourselves. */
3340
bf9ae9d8 3341 if (low_supports_breakpoints ())
6bf5e0ba
PA
3342 {
3343 struct regcache *regcache
0bfdf32f 3344 = get_thread_regcache (current_thread, 1);
bf9ae9d8 3345 low_set_pc (regcache, event_child->stop_pc);
6bf5e0ba
PA
3346 }
3347
7984d532 3348 if (step_over_finished)
e3652c84
YQ
3349 {
3350 /* If we have finished stepping over a breakpoint, we've
3351 stopped and suspended all LWPs momentarily except the
3352 stepping one. This is where we resume them all again.
3353 We're going to keep waiting, so use proceed, which
3354 handles stepping over the next breakpoint. */
3355 unsuspend_all_lwps (event_child);
3356 }
3357 else
3358 {
3359 /* Remove the single-step breakpoints if any. Note that
3360 there isn't single-step breakpoint if we finished stepping
3361 over. */
7582c77c 3362 if (supports_software_single_step ()
e3652c84
YQ
3363 && has_single_step_breakpoints (current_thread))
3364 {
3365 stop_all_lwps (0, event_child);
3366 delete_single_step_breakpoints (current_thread);
3367 unstop_all_lwps (0, event_child);
3368 }
3369 }
7984d532 3370
c058728c 3371 threads_debug_printf ("proceeding all threads.");
edeeb602 3372
c058728c 3373 proceed_all_lwps ();
edeeb602 3374
582511be 3375 return ignore_event (ourstatus);
6bf5e0ba
PA
3376 }
3377
c058728c
SM
3378 if (debug_threads)
3379 {
3380 if (event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
3381 threads_debug_printf ("LWP %ld: extended event with waitstatus %s",
3382 lwpid_of (get_lwp_thread (event_child)),
3383 event_child->waitstatus.to_string ().c_str ());
3384
3385 if (current_thread->last_resume_kind == resume_step)
3386 {
3387 if (event_child->step_range_start == event_child->step_range_end)
3388 threads_debug_printf
3389 ("GDB wanted to single-step, reporting event.");
3390 else if (!lwp_in_step_range (event_child))
3391 threads_debug_printf ("Out of step range, reporting event.");
3392 }
3393
3394 if (event_child->stop_reason == TARGET_STOPPED_BY_WATCHPOINT)
3395 threads_debug_printf ("Stopped by watchpoint.");
3396 else if (gdb_breakpoint_here (event_child->stop_pc))
3397 threads_debug_printf ("Stopped by GDB breakpoint.");
3398 }
3399
3400 threads_debug_printf ("Hit a non-gdbserver trap event.");
6bf5e0ba
PA
3401
3402 /* Alright, we're going to report a stop. */
3403
3b9a79ef 3404 /* Remove single-step breakpoints. */
7582c77c 3405 if (supports_software_single_step ())
8901d193 3406 {
3b9a79ef 3407 /* Remove single-step breakpoints or not. It it is true, stop all
8901d193
YQ
3408 lwps, so that other threads won't hit the breakpoint in the
3409 staled memory. */
3b9a79ef 3410 int remove_single_step_breakpoints_p = 0;
8901d193
YQ
3411
3412 if (non_stop)
3413 {
3b9a79ef
YQ
3414 remove_single_step_breakpoints_p
3415 = has_single_step_breakpoints (current_thread);
8901d193
YQ
3416 }
3417 else
3418 {
3419 /* In all-stop, a stop reply cancels all previous resume
3b9a79ef 3420 requests. Delete all single-step breakpoints. */
8901d193 3421
9c80ecd6
SM
3422 find_thread ([&] (thread_info *thread) {
3423 if (has_single_step_breakpoints (thread))
3424 {
3425 remove_single_step_breakpoints_p = 1;
3426 return true;
3427 }
8901d193 3428
9c80ecd6
SM
3429 return false;
3430 });
8901d193
YQ
3431 }
3432
3b9a79ef 3433 if (remove_single_step_breakpoints_p)
8901d193 3434 {
3b9a79ef 3435 /* If we remove single-step breakpoints from memory, stop all lwps,
8901d193
YQ
3436 so that other threads won't hit the breakpoint in the staled
3437 memory. */
3438 stop_all_lwps (0, event_child);
3439
3440 if (non_stop)
3441 {
3b9a79ef
YQ
3442 gdb_assert (has_single_step_breakpoints (current_thread));
3443 delete_single_step_breakpoints (current_thread);
8901d193
YQ
3444 }
3445 else
3446 {
9c80ecd6
SM
3447 for_each_thread ([] (thread_info *thread){
3448 if (has_single_step_breakpoints (thread))
3449 delete_single_step_breakpoints (thread);
3450 });
8901d193
YQ
3451 }
3452
3453 unstop_all_lwps (0, event_child);
3454 }
3455 }
3456
582511be 3457 if (!stabilizing_threads)
6bf5e0ba
PA
3458 {
3459 /* In all-stop, stop all threads. */
582511be
PA
3460 if (!non_stop)
3461 stop_all_lwps (0, NULL);
6bf5e0ba 3462
c03e6ccc 3463 if (step_over_finished)
582511be
PA
3464 {
3465 if (!non_stop)
3466 {
3467 /* If we were doing a step-over, all other threads but
3468 the stepping one had been paused in start_step_over,
3469 with their suspend counts incremented. We don't want
3470 to do a full unstop/unpause, because we're in
3471 all-stop mode (so we want threads stopped), but we
3472 still need to unsuspend the other threads, to
3473 decrement their `suspended' count back. */
3474 unsuspend_all_lwps (event_child);
3475 }
3476 else
3477 {
3478 /* If we just finished a step-over, then all threads had
3479 been momentarily paused. In all-stop, that's fine,
3480 we want threads stopped by now anyway. In non-stop,
3481 we need to re-resume threads that GDB wanted to be
3482 running. */
3483 unstop_all_lwps (1, event_child);
3484 }
3485 }
c03e6ccc 3486
3aa5cfa0
AT
3487 /* If we're not waiting for a specific LWP, choose an event LWP
3488 from among those that have had events. Giving equal priority
3489 to all LWPs that have had events helps prevent
3490 starvation. */
d7e15655 3491 if (ptid == minus_one_ptid)
3aa5cfa0
AT
3492 {
3493 event_child->status_pending_p = 1;
3494 event_child->status_pending = w;
3495
3496 select_event_lwp (&event_child);
3497
3498 /* current_thread and event_child must stay in sync. */
24583e45 3499 switch_to_thread (get_lwp_thread (event_child));
3aa5cfa0
AT
3500
3501 event_child->status_pending_p = 0;
3502 w = event_child->status_pending;
3503 }
3504
3505
fa593d66 3506 /* Stabilize threads (move out of jump pads). */
582511be 3507 if (!non_stop)
5c9eb2f2 3508 target_stabilize_threads ();
6bf5e0ba
PA
3509 }
3510 else
3511 {
3512 /* If we just finished a step-over, then all threads had been
3513 momentarily paused. In all-stop, that's fine, we want
3514 threads stopped by now anyway. In non-stop, we need to
3515 re-resume threads that GDB wanted to be running. */
3516 if (step_over_finished)
7984d532 3517 unstop_all_lwps (1, event_child);
6bf5e0ba
PA
3518 }
3519
e88cf517
SM
3520 /* At this point, we haven't set OURSTATUS. This is where we do it. */
3521 gdb_assert (ourstatus->kind () == TARGET_WAITKIND_IGNORE);
3522
183be222 3523 if (event_child->waitstatus.kind () != TARGET_WAITKIND_IGNORE)
de0d863e 3524 {
00db26fa
PA
3525 /* If the reported event is an exit, fork, vfork or exec, let
3526 GDB know. */
5a04c4cf
PA
3527
3528 /* Break the unreported fork relationship chain. */
183be222
SM
3529 if (event_child->waitstatus.kind () == TARGET_WAITKIND_FORKED
3530 || event_child->waitstatus.kind () == TARGET_WAITKIND_VFORKED)
5a04c4cf
PA
3531 {
3532 event_child->fork_relative->fork_relative = NULL;
3533 event_child->fork_relative = NULL;
3534 }
3535
00db26fa 3536 *ourstatus = event_child->waitstatus;
de0d863e 3537 /* Clear the event lwp's waitstatus since we handled it already. */
183be222 3538 event_child->waitstatus.set_ignore ();
de0d863e
DB
3539 }
3540 else
183be222 3541 {
e88cf517
SM
3542 /* The LWP stopped due to a plain signal or a syscall signal. Either way,
3543 event_chid->waitstatus wasn't filled in with the details, so look at
3544 the wait status W. */
3545 if (WSTOPSIG (w) == SYSCALL_SIGTRAP)
3546 {
3547 int syscall_number;
3548
3549 get_syscall_trapinfo (event_child, &syscall_number);
3550 if (event_child->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY)
3551 ourstatus->set_syscall_entry (syscall_number);
3552 else if (event_child->syscall_state == TARGET_WAITKIND_SYSCALL_RETURN)
3553 ourstatus->set_syscall_return (syscall_number);
3554 else
3555 gdb_assert_not_reached ("unexpected syscall state");
3556 }
3557 else if (current_thread->last_resume_kind == resume_stop
3558 && WSTOPSIG (w) == SIGSTOP)
3559 {
3560 /* A thread that has been requested to stop by GDB with vCont;t,
3561 and it stopped cleanly, so report as SIG0. The use of
3562 SIGSTOP is an implementation detail. */
3563 ourstatus->set_stopped (GDB_SIGNAL_0);
3564 }
3565 else
3566 ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (w)));
183be222 3567 }
5b1c542e 3568
582511be 3569 /* Now that we've selected our final event LWP, un-adjust its PC if
3e572f71
PA
3570 it was a software breakpoint, and the client doesn't know we can
3571 adjust the breakpoint ourselves. */
3572 if (event_child->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT
c12a5089 3573 && !cs.swbreak_feature)
582511be 3574 {
d4807ea2 3575 int decr_pc = low_decr_pc_after_break ();
582511be
PA
3576
3577 if (decr_pc != 0)
3578 {
3579 struct regcache *regcache
3580 = get_thread_regcache (current_thread, 1);
bf9ae9d8 3581 low_set_pc (regcache, event_child->stop_pc + decr_pc);
582511be
PA
3582 }
3583 }
3584
d7e15655 3585 gdb_assert (step_over_bkpt == null_ptid);
d50171e4 3586
e48359ea 3587 threads_debug_printf ("ret = %s, %s",
c058728c 3588 target_pid_to_str (ptid_of (current_thread)).c_str (),
e48359ea 3589 ourstatus->to_string ().c_str ());
bd99dc85 3590
183be222 3591 if (ourstatus->kind () == TARGET_WAITKIND_EXITED)
65706a29
PA
3592 return filter_exit_event (event_child, ourstatus);
3593
0bfdf32f 3594 return ptid_of (current_thread);
bd99dc85
PA
3595}
3596
3597/* Get rid of any pending event in the pipe. */
3598static void
3599async_file_flush (void)
3600{
cdc8e9b2 3601 linux_event_pipe.flush ();
bd99dc85
PA
3602}
3603
3604/* Put something in the pipe, so the event loop wakes up. */
3605static void
3606async_file_mark (void)
3607{
cdc8e9b2 3608 linux_event_pipe.mark ();
bd99dc85
PA
3609}
3610
6532e7e3
TBA
3611ptid_t
3612linux_process_target::wait (ptid_t ptid,
3613 target_waitstatus *ourstatus,
b60cea74 3614 target_wait_flags target_options)
bd99dc85 3615{
95954743 3616 ptid_t event_ptid;
bd99dc85 3617
bd99dc85
PA
3618 /* Flush the async file first. */
3619 if (target_is_async_p ())
3620 async_file_flush ();
3621
582511be
PA
3622 do
3623 {
d16f3f6c 3624 event_ptid = wait_1 (ptid, ourstatus, target_options);
582511be
PA
3625 }
3626 while ((target_options & TARGET_WNOHANG) == 0
d7e15655 3627 && event_ptid == null_ptid
183be222 3628 && ourstatus->kind () == TARGET_WAITKIND_IGNORE);
bd99dc85
PA
3629
3630 /* If at least one stop was reported, there may be more. A single
3631 SIGCHLD can signal more than one child stop. */
3632 if (target_is_async_p ()
3633 && (target_options & TARGET_WNOHANG) != 0
d7e15655 3634 && event_ptid != null_ptid)
bd99dc85
PA
3635 async_file_mark ();
3636
3637 return event_ptid;
da6d8c04
DJ
3638}
3639
c5f62d5f 3640/* Send a signal to an LWP. */
fd500816
DJ
3641
3642static int
a1928bad 3643kill_lwp (unsigned long lwpid, int signo)
fd500816 3644{
4a6ed09b 3645 int ret;
fd500816 3646
4a6ed09b
PA
3647 errno = 0;
3648 ret = syscall (__NR_tkill, lwpid, signo);
3649 if (errno == ENOSYS)
3650 {
3651 /* If tkill fails, then we are not using nptl threads, a
3652 configuration we no longer support. */
3653 perror_with_name (("tkill"));
3654 }
3655 return ret;
fd500816
DJ
3656}
3657
964e4306
PA
3658void
3659linux_stop_lwp (struct lwp_info *lwp)
3660{
3661 send_sigstop (lwp);
3662}
3663
0d62e5e8 3664static void
02fc4de7 3665send_sigstop (struct lwp_info *lwp)
0d62e5e8 3666{
bd99dc85 3667 int pid;
0d62e5e8 3668
d86d4aaf 3669 pid = lwpid_of (get_lwp_thread (lwp));
bd99dc85 3670
0d62e5e8
DJ
3671 /* If we already have a pending stop signal for this process, don't
3672 send another. */
54a0b537 3673 if (lwp->stop_expected)
0d62e5e8 3674 {
c058728c 3675 threads_debug_printf ("Have pending sigstop for lwp %d", pid);
ae13219e 3676
0d62e5e8
DJ
3677 return;
3678 }
3679
c058728c 3680 threads_debug_printf ("Sending sigstop to lwp %d", pid);
0d62e5e8 3681
d50171e4 3682 lwp->stop_expected = 1;
bd99dc85 3683 kill_lwp (pid, SIGSTOP);
0d62e5e8
DJ
3684}
3685
df3e4dbe
SM
3686static void
3687send_sigstop (thread_info *thread, lwp_info *except)
02fc4de7 3688{
d86d4aaf 3689 struct lwp_info *lwp = get_thread_lwp (thread);
02fc4de7 3690
7984d532
PA
3691 /* Ignore EXCEPT. */
3692 if (lwp == except)
df3e4dbe 3693 return;
7984d532 3694
02fc4de7 3695 if (lwp->stopped)
df3e4dbe 3696 return;
02fc4de7
PA
3697
3698 send_sigstop (lwp);
7984d532
PA
3699}
3700
3701/* Increment the suspend count of an LWP, and stop it, if not stopped
3702 yet. */
df3e4dbe
SM
3703static void
3704suspend_and_send_sigstop (thread_info *thread, lwp_info *except)
7984d532 3705{
d86d4aaf 3706 struct lwp_info *lwp = get_thread_lwp (thread);
7984d532
PA
3707
3708 /* Ignore EXCEPT. */
3709 if (lwp == except)
df3e4dbe 3710 return;
7984d532 3711
863d01bd 3712 lwp_suspended_inc (lwp);
7984d532 3713
df3e4dbe 3714 send_sigstop (thread, except);
02fc4de7
PA
3715}
3716
95954743
PA
3717static void
3718mark_lwp_dead (struct lwp_info *lwp, int wstat)
3719{
95954743
PA
3720 /* Store the exit status for later. */
3721 lwp->status_pending_p = 1;
3722 lwp->status_pending = wstat;
3723
00db26fa
PA
3724 /* Store in waitstatus as well, as there's nothing else to process
3725 for this event. */
3726 if (WIFEXITED (wstat))
183be222 3727 lwp->waitstatus.set_exited (WEXITSTATUS (wstat));
00db26fa 3728 else if (WIFSIGNALED (wstat))
183be222 3729 lwp->waitstatus.set_signalled (gdb_signal_from_host (WTERMSIG (wstat)));
00db26fa 3730
95954743
PA
3731 /* Prevent trying to stop it. */
3732 lwp->stopped = 1;
3733
3734 /* No further stops are expected from a dead lwp. */
3735 lwp->stop_expected = 0;
3736}
3737
00db26fa
PA
3738/* Return true if LWP has exited already, and has a pending exit event
3739 to report to GDB. */
3740
3741static int
3742lwp_is_marked_dead (struct lwp_info *lwp)
3743{
3744 return (lwp->status_pending_p
3745 && (WIFEXITED (lwp->status_pending)
3746 || WIFSIGNALED (lwp->status_pending)));
3747}
3748
d16f3f6c
TBA
3749void
3750linux_process_target::wait_for_sigstop ()
0d62e5e8 3751{
0bfdf32f 3752 struct thread_info *saved_thread;
95954743 3753 ptid_t saved_tid;
fa96cb38
PA
3754 int wstat;
3755 int ret;
0d62e5e8 3756
0bfdf32f
GB
3757 saved_thread = current_thread;
3758 if (saved_thread != NULL)
9c80ecd6 3759 saved_tid = saved_thread->id;
bd99dc85 3760 else
95954743 3761 saved_tid = null_ptid; /* avoid bogus unused warning */
bd99dc85 3762
20ac1cdb
TBA
3763 scoped_restore_current_thread restore_thread;
3764
c058728c 3765 threads_debug_printf ("pulling events");
d50171e4 3766
fa96cb38
PA
3767 /* Passing NULL_PTID as filter indicates we want all events to be
3768 left pending. Eventually this returns when there are no
3769 unwaited-for children left. */
d16f3f6c 3770 ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat, __WALL);
fa96cb38 3771 gdb_assert (ret == -1);
0d62e5e8 3772
13d3d99b 3773 if (saved_thread == NULL || mythread_alive (saved_tid))
20ac1cdb 3774 return;
0d62e5e8
DJ
3775 else
3776 {
c058728c 3777 threads_debug_printf ("Previously current thread died.");
0d62e5e8 3778
f0db101d
PA
3779 /* We can't change the current inferior behind GDB's back,
3780 otherwise, a subsequent command may apply to the wrong
3781 process. */
20ac1cdb
TBA
3782 restore_thread.dont_restore ();
3783 switch_to_thread (nullptr);
0d62e5e8
DJ
3784 }
3785}
3786
13e567af
TBA
3787bool
3788linux_process_target::stuck_in_jump_pad (thread_info *thread)
fa593d66 3789{
d86d4aaf 3790 struct lwp_info *lwp = get_thread_lwp (thread);
fa593d66 3791
863d01bd
PA
3792 if (lwp->suspended != 0)
3793 {
f34652de 3794 internal_error ("LWP %ld is suspended, suspended=%d\n",
863d01bd
PA
3795 lwpid_of (thread), lwp->suspended);
3796 }
fa593d66
PA
3797 gdb_assert (lwp->stopped);
3798
3799 /* Allow debugging the jump pad, gdb_collect, etc.. */
3800 return (supports_fast_tracepoints ()
58b4daa5 3801 && agent_loaded_p ()
fa593d66 3802 && (gdb_breakpoint_here (lwp->stop_pc)
15c66dd6 3803 || lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT
fa593d66 3804 || thread->last_resume_kind == resume_step)
229d26fc
SM
3805 && (linux_fast_tracepoint_collecting (lwp, NULL)
3806 != fast_tpoint_collect_result::not_collecting));
fa593d66
PA
3807}
3808
d16f3f6c
TBA
3809void
3810linux_process_target::move_out_of_jump_pad (thread_info *thread)
fa593d66 3811{
d86d4aaf 3812 struct lwp_info *lwp = get_thread_lwp (thread);
fa593d66
PA
3813 int *wstat;
3814
863d01bd
PA
3815 if (lwp->suspended != 0)
3816 {
f34652de 3817 internal_error ("LWP %ld is suspended, suspended=%d\n",
863d01bd
PA
3818 lwpid_of (thread), lwp->suspended);
3819 }
fa593d66
PA
3820 gdb_assert (lwp->stopped);
3821
f0ce0d3a 3822 /* For gdb_breakpoint_here. */
24583e45
TBA
3823 scoped_restore_current_thread restore_thread;
3824 switch_to_thread (thread);
f0ce0d3a 3825
fa593d66
PA
3826 wstat = lwp->status_pending_p ? &lwp->status_pending : NULL;
3827
3828 /* Allow debugging the jump pad, gdb_collect, etc. */
3829 if (!gdb_breakpoint_here (lwp->stop_pc)
15c66dd6 3830 && lwp->stop_reason != TARGET_STOPPED_BY_WATCHPOINT
fa593d66
PA
3831 && thread->last_resume_kind != resume_step
3832 && maybe_move_out_of_jump_pad (lwp, wstat))
3833 {
c058728c
SM
3834 threads_debug_printf ("LWP %ld needs stabilizing (in jump pad)",
3835 lwpid_of (thread));
fa593d66
PA
3836
3837 if (wstat)
3838 {
3839 lwp->status_pending_p = 0;
3840 enqueue_one_deferred_signal (lwp, wstat);
3841
c058728c
SM
3842 threads_debug_printf ("Signal %d for LWP %ld deferred (in jump pad",
3843 WSTOPSIG (*wstat), lwpid_of (thread));
fa593d66
PA
3844 }
3845
df95181f 3846 resume_one_lwp (lwp, 0, 0, NULL);
fa593d66
PA
3847 }
3848 else
863d01bd 3849 lwp_suspended_inc (lwp);
fa593d66
PA
3850}
3851
5a6b0a41
SM
3852static bool
3853lwp_running (thread_info *thread)
fa593d66 3854{
d86d4aaf 3855 struct lwp_info *lwp = get_thread_lwp (thread);
fa593d66 3856
00db26fa 3857 if (lwp_is_marked_dead (lwp))
5a6b0a41
SM
3858 return false;
3859
3860 return !lwp->stopped;
fa593d66
PA
3861}
3862
d16f3f6c
TBA
3863void
3864linux_process_target::stop_all_lwps (int suspend, lwp_info *except)
0d62e5e8 3865{
bde24c0a
PA
3866 /* Should not be called recursively. */
3867 gdb_assert (stopping_threads == NOT_STOPPING_THREADS);
3868
c058728c
SM
3869 THREADS_SCOPED_DEBUG_ENTER_EXIT;
3870
3871 threads_debug_printf
3872 ("%s, except=%s", suspend ? "stop-and-suspend" : "stop",
3873 (except != NULL
3874 ? target_pid_to_str (ptid_of (get_lwp_thread (except))).c_str ()
3875 : "none"));
87ce2a04 3876
bde24c0a
PA
3877 stopping_threads = (suspend
3878 ? STOPPING_AND_SUSPENDING_THREADS
3879 : STOPPING_THREADS);
7984d532
PA
3880
3881 if (suspend)
df3e4dbe
SM
3882 for_each_thread ([&] (thread_info *thread)
3883 {
3884 suspend_and_send_sigstop (thread, except);
3885 });
7984d532 3886 else
df3e4dbe
SM
3887 for_each_thread ([&] (thread_info *thread)
3888 {
3889 send_sigstop (thread, except);
3890 });
3891
fa96cb38 3892 wait_for_sigstop ();
bde24c0a 3893 stopping_threads = NOT_STOPPING_THREADS;
87ce2a04 3894
c058728c 3895 threads_debug_printf ("setting stopping_threads back to !stopping");
0d62e5e8
DJ
3896}
3897
863d01bd
PA
3898/* Enqueue one signal in the chain of signals which need to be
3899 delivered to this process on next resume. */
3900
3901static void
3902enqueue_pending_signal (struct lwp_info *lwp, int signal, siginfo_t *info)
3903{
013e3554
TBA
3904 lwp->pending_signals.emplace_back (signal);
3905 if (info == nullptr)
3906 memset (&lwp->pending_signals.back ().info, 0, sizeof (siginfo_t));
863d01bd 3907 else
013e3554 3908 lwp->pending_signals.back ().info = *info;
863d01bd
PA
3909}
3910
df95181f
TBA
3911void
3912linux_process_target::install_software_single_step_breakpoints (lwp_info *lwp)
fa5308bd 3913{
984a2c04
YQ
3914 struct thread_info *thread = get_lwp_thread (lwp);
3915 struct regcache *regcache = get_thread_regcache (thread, 1);
8ce47547 3916
24583e45 3917 scoped_restore_current_thread restore_thread;
984a2c04 3918
24583e45 3919 switch_to_thread (thread);
7582c77c 3920 std::vector<CORE_ADDR> next_pcs = low_get_next_pcs (regcache);
fa5308bd 3921
a0ff9e1a 3922 for (CORE_ADDR pc : next_pcs)
3b9a79ef 3923 set_single_step_breakpoint (pc, current_ptid);
fa5308bd
AT
3924}
3925
df95181f
TBA
3926int
3927linux_process_target::single_step (lwp_info* lwp)
7fe5e27e
AT
3928{
3929 int step = 0;
3930
b31cdfa6 3931 if (supports_hardware_single_step ())
7fe5e27e
AT
3932 {
3933 step = 1;
3934 }
7582c77c 3935 else if (supports_software_single_step ())
7fe5e27e
AT
3936 {
3937 install_software_single_step_breakpoints (lwp);
3938 step = 0;
3939 }
3940 else
c058728c 3941 threads_debug_printf ("stepping is not implemented on this target");
7fe5e27e
AT
3942
3943 return step;
3944}
3945
35ac8b3e 3946/* The signal can be delivered to the inferior if we are not trying to
5b061e98
YQ
3947 finish a fast tracepoint collect. Since signal can be delivered in
3948 the step-over, the program may go to signal handler and trap again
3949 after return from the signal handler. We can live with the spurious
3950 double traps. */
35ac8b3e
YQ
3951
3952static int
3953lwp_signal_can_be_delivered (struct lwp_info *lwp)
3954{
229d26fc
SM
3955 return (lwp->collecting_fast_tracepoint
3956 == fast_tpoint_collect_result::not_collecting);
35ac8b3e
YQ
3957}
3958
df95181f
TBA
3959void
3960linux_process_target::resume_one_lwp_throw (lwp_info *lwp, int step,
3961 int signal, siginfo_t *info)
da6d8c04 3962{
d86d4aaf 3963 struct thread_info *thread = get_lwp_thread (lwp);
82075af2 3964 int ptrace_request;
c06cbd92
YQ
3965 struct process_info *proc = get_thread_process (thread);
3966
3967 /* Note that target description may not be initialised
3968 (proc->tdesc == NULL) at this point because the program hasn't
3969 stopped at the first instruction yet. It means GDBserver skips
3970 the extra traps from the wrapper program (see option --wrapper).
3971 Code in this function that requires register access should be
3972 guarded by proc->tdesc == NULL or something else. */
0d62e5e8 3973
54a0b537 3974 if (lwp->stopped == 0)
0d62e5e8
DJ
3975 return;
3976
183be222 3977 gdb_assert (lwp->waitstatus.kind () == TARGET_WAITKIND_IGNORE);
65706a29 3978
229d26fc
SM
3979 fast_tpoint_collect_result fast_tp_collecting
3980 = lwp->collecting_fast_tracepoint;
fa593d66 3981
229d26fc
SM
3982 gdb_assert (!stabilizing_threads
3983 || (fast_tp_collecting
3984 != fast_tpoint_collect_result::not_collecting));
fa593d66 3985
219f2f23
PA
3986 /* Cancel actions that rely on GDB not changing the PC (e.g., the
3987 user used the "jump" command, or "set $pc = foo"). */
c06cbd92 3988 if (thread->while_stepping != NULL && lwp->stop_pc != get_pc (lwp))
219f2f23
PA
3989 {
3990 /* Collecting 'while-stepping' actions doesn't make sense
3991 anymore. */
d86d4aaf 3992 release_while_stepping_state_list (thread);
219f2f23
PA
3993 }
3994
0d62e5e8 3995 /* If we have pending signals or status, and a new signal, enqueue the
35ac8b3e
YQ
3996 signal. Also enqueue the signal if it can't be delivered to the
3997 inferior right now. */
0d62e5e8 3998 if (signal != 0
fa593d66 3999 && (lwp->status_pending_p
013e3554 4000 || !lwp->pending_signals.empty ()
35ac8b3e 4001 || !lwp_signal_can_be_delivered (lwp)))
94610ec4
YQ
4002 {
4003 enqueue_pending_signal (lwp, signal, info);
4004
4005 /* Postpone any pending signal. It was enqueued above. */
4006 signal = 0;
4007 }
0d62e5e8 4008
d50171e4
PA
4009 if (lwp->status_pending_p)
4010 {
c058728c
SM
4011 threads_debug_printf
4012 ("Not resuming lwp %ld (%s, stop %s); has pending status",
4013 lwpid_of (thread), step ? "step" : "continue",
4014 lwp->stop_expected ? "expected" : "not expected");
d50171e4
PA
4015 return;
4016 }
0d62e5e8 4017
24583e45
TBA
4018 scoped_restore_current_thread restore_thread;
4019 switch_to_thread (thread);
0d62e5e8 4020
0d62e5e8
DJ
4021 /* This bit needs some thinking about. If we get a signal that
4022 we must report while a single-step reinsert is still pending,
4023 we often end up resuming the thread. It might be better to
4024 (ew) allow a stack of pending events; then we could be sure that
4025 the reinsert happened right away and not lose any signals.
4026
4027 Making this stack would also shrink the window in which breakpoints are
54a0b537 4028 uninserted (see comment in linux_wait_for_lwp) but not enough for
0d62e5e8
DJ
4029 complete correctness, so it won't solve that problem. It may be
4030 worthwhile just to solve this one, however. */
54a0b537 4031 if (lwp->bp_reinsert != 0)
0d62e5e8 4032 {
c058728c
SM
4033 threads_debug_printf (" pending reinsert at 0x%s",
4034 paddress (lwp->bp_reinsert));
d50171e4 4035
b31cdfa6 4036 if (supports_hardware_single_step ())
d50171e4 4037 {
229d26fc 4038 if (fast_tp_collecting == fast_tpoint_collect_result::not_collecting)
fa593d66
PA
4039 {
4040 if (step == 0)
9986ba08 4041 warning ("BAD - reinserting but not stepping.");
fa593d66 4042 if (lwp->suspended)
9986ba08
PA
4043 warning ("BAD - reinserting and suspended(%d).",
4044 lwp->suspended);
fa593d66 4045 }
d50171e4 4046 }
f79b145d
YQ
4047
4048 step = maybe_hw_step (thread);
0d62e5e8
DJ
4049 }
4050
229d26fc 4051 if (fast_tp_collecting == fast_tpoint_collect_result::before_insn)
c058728c
SM
4052 threads_debug_printf
4053 ("lwp %ld wants to get out of fast tracepoint jump pad "
4054 "(exit-jump-pad-bkpt)", lwpid_of (thread));
4055
229d26fc 4056 else if (fast_tp_collecting == fast_tpoint_collect_result::at_insn)
fa593d66 4057 {
c058728c
SM
4058 threads_debug_printf
4059 ("lwp %ld wants to get out of fast tracepoint jump pad single-stepping",
4060 lwpid_of (thread));
fa593d66 4061
b31cdfa6 4062 if (supports_hardware_single_step ())
fa593d66
PA
4063 step = 1;
4064 else
38e08fca 4065 {
f34652de 4066 internal_error ("moving out of jump pad single-stepping"
38e08fca
GB
4067 " not implemented on this target");
4068 }
fa593d66
PA
4069 }
4070
219f2f23
PA
4071 /* If we have while-stepping actions in this thread set it stepping.
4072 If we have a signal to deliver, it may or may not be set to
4073 SIG_IGN, we don't know. Assume so, and allow collecting
4074 while-stepping into a signal handler. A possible smart thing to
4075 do would be to set an internal breakpoint at the signal return
4076 address, continue, and carry on catching this while-stepping
4077 action only when that breakpoint is hit. A future
4078 enhancement. */
7fe5e27e 4079 if (thread->while_stepping != NULL)
219f2f23 4080 {
c058728c
SM
4081 threads_debug_printf
4082 ("lwp %ld has a while-stepping action -> forcing step.",
4083 lwpid_of (thread));
7fe5e27e
AT
4084
4085 step = single_step (lwp);
219f2f23
PA
4086 }
4087
bf9ae9d8 4088 if (proc->tdesc != NULL && low_supports_breakpoints ())
0d62e5e8 4089 {
0bfdf32f 4090 struct regcache *regcache = get_thread_regcache (current_thread, 1);
582511be 4091
bf9ae9d8 4092 lwp->stop_pc = low_get_pc (regcache);
582511be 4093
c058728c
SM
4094 threads_debug_printf (" %s from pc 0x%lx", step ? "step" : "continue",
4095 (long) lwp->stop_pc);
0d62e5e8
DJ
4096 }
4097
35ac8b3e
YQ
4098 /* If we have pending signals, consume one if it can be delivered to
4099 the inferior. */
013e3554 4100 if (!lwp->pending_signals.empty () && lwp_signal_can_be_delivered (lwp))
0d62e5e8 4101 {
013e3554 4102 const pending_signal &p_sig = lwp->pending_signals.front ();
0d62e5e8 4103
013e3554
TBA
4104 signal = p_sig.signal;
4105 if (p_sig.info.si_signo != 0)
d86d4aaf 4106 ptrace (PTRACE_SETSIGINFO, lwpid_of (thread), (PTRACE_TYPE_ARG3) 0,
013e3554 4107 &p_sig.info);
32ca6d61 4108
013e3554 4109 lwp->pending_signals.pop_front ();
0d62e5e8
DJ
4110 }
4111
c058728c
SM
4112 threads_debug_printf ("Resuming lwp %ld (%s, signal %d, stop %s)",
4113 lwpid_of (thread), step ? "step" : "continue", signal,
4114 lwp->stop_expected ? "expected" : "not expected");
94610ec4 4115
d7599cc0 4116 low_prepare_to_resume (lwp);
aa5ca48f 4117
d86d4aaf 4118 regcache_invalidate_thread (thread);
da6d8c04 4119 errno = 0;
54a0b537 4120 lwp->stepping = step;
82075af2
JS
4121 if (step)
4122 ptrace_request = PTRACE_SINGLESTEP;
4123 else if (gdb_catching_syscalls_p (lwp))
4124 ptrace_request = PTRACE_SYSCALL;
4125 else
4126 ptrace_request = PTRACE_CONT;
4127 ptrace (ptrace_request,
4128 lwpid_of (thread),
b8e1b30e 4129 (PTRACE_TYPE_ARG3) 0,
14ce3065
DE
4130 /* Coerce to a uintptr_t first to avoid potential gcc warning
4131 of coercing an 8 byte integer to a 4 byte pointer. */
b8e1b30e 4132 (PTRACE_TYPE_ARG4) (uintptr_t) signal);
0d62e5e8 4133
da6d8c04 4134 if (errno)
20471e00
SM
4135 {
4136 int saved_errno = errno;
4137
4138 threads_debug_printf ("ptrace errno = %d (%s)",
4139 saved_errno, strerror (saved_errno));
4140
4141 errno = saved_errno;
4142 perror_with_name ("resuming thread");
4143 }
23f238d3
PA
4144
4145 /* Successfully resumed. Clear state that no longer makes sense,
4146 and mark the LWP as running. Must not do this before resuming
4147 otherwise if that fails other code will be confused. E.g., we'd
4148 later try to stop the LWP and hang forever waiting for a stop
4149 status. Note that we must not throw after this is cleared,
4150 otherwise handle_zombie_lwp_error would get confused. */
4151 lwp->stopped = 0;
4152 lwp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4153}
4154
d7599cc0
TBA
4155void
4156linux_process_target::low_prepare_to_resume (lwp_info *lwp)
4157{
4158 /* Nop. */
4159}
4160
23f238d3
PA
4161/* Called when we try to resume a stopped LWP and that errors out. If
4162 the LWP is no longer in ptrace-stopped state (meaning it's zombie,
4163 or about to become), discard the error, clear any pending status
4164 the LWP may have, and return true (we'll collect the exit status
4165 soon enough). Otherwise, return false. */
4166
4167static int
4168check_ptrace_stopped_lwp_gone (struct lwp_info *lp)
4169{
4170 struct thread_info *thread = get_lwp_thread (lp);
4171
4172 /* If we get an error after resuming the LWP successfully, we'd
4173 confuse !T state for the LWP being gone. */
4174 gdb_assert (lp->stopped);
4175
4176 /* We can't just check whether the LWP is in 'Z (Zombie)' state,
4177 because even if ptrace failed with ESRCH, the tracee may be "not
4178 yet fully dead", but already refusing ptrace requests. In that
4179 case the tracee has 'R (Running)' state for a little bit
4180 (observed in Linux 3.18). See also the note on ESRCH in the
4181 ptrace(2) man page. Instead, check whether the LWP has any state
4182 other than ptrace-stopped. */
4183
4184 /* Don't assume anything if /proc/PID/status can't be read. */
4185 if (linux_proc_pid_is_trace_stopped_nowarn (lwpid_of (thread)) == 0)
3221518c 4186 {
23f238d3
PA
4187 lp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4188 lp->status_pending_p = 0;
4189 return 1;
4190 }
4191 return 0;
4192}
4193
df95181f
TBA
4194void
4195linux_process_target::resume_one_lwp (lwp_info *lwp, int step, int signal,
4196 siginfo_t *info)
23f238d3 4197{
a70b8144 4198 try
23f238d3 4199 {
df95181f 4200 resume_one_lwp_throw (lwp, step, signal, info);
23f238d3 4201 }
230d2906 4202 catch (const gdb_exception_error &ex)
23f238d3 4203 {
20471e00
SM
4204 if (check_ptrace_stopped_lwp_gone (lwp))
4205 {
4206 /* This could because we tried to resume an LWP after its leader
4207 exited. Mark it as resumed, so we can collect an exit event
4208 from it. */
4209 lwp->stopped = 0;
4210 lwp->stop_reason = TARGET_STOPPED_BY_NO_REASON;
4211 }
4212 else
eedc3f4f 4213 throw;
3221518c 4214 }
da6d8c04
DJ
4215}
4216
5fdda392
SM
4217/* This function is called once per thread via for_each_thread.
4218 We look up which resume request applies to THREAD and mark it with a
4219 pointer to the appropriate resume request.
5544ad89
DJ
4220
4221 This algorithm is O(threads * resume elements), but resume elements
4222 is small (and will remain small at least until GDB supports thread
4223 suspension). */
ebcf782c 4224
5fdda392
SM
4225static void
4226linux_set_resume_request (thread_info *thread, thread_resume *resume, size_t n)
0d62e5e8 4227{
d86d4aaf 4228 struct lwp_info *lwp = get_thread_lwp (thread);
64386c31 4229
5fdda392 4230 for (int ndx = 0; ndx < n; ndx++)
95954743 4231 {
5fdda392 4232 ptid_t ptid = resume[ndx].thread;
d7e15655 4233 if (ptid == minus_one_ptid
9c80ecd6 4234 || ptid == thread->id
0c9070b3
YQ
4235 /* Handle both 'pPID' and 'pPID.-1' as meaning 'all threads
4236 of PID'. */
e99b03dc 4237 || (ptid.pid () == pid_of (thread)
0e998d96 4238 && (ptid.is_pid ()
e38504b3 4239 || ptid.lwp () == -1)))
95954743 4240 {
5fdda392 4241 if (resume[ndx].kind == resume_stop
8336d594 4242 && thread->last_resume_kind == resume_stop)
d50171e4 4243 {
c058728c
SM
4244 threads_debug_printf
4245 ("already %s LWP %ld at GDB's request",
4246 (thread->last_status.kind () == TARGET_WAITKIND_STOPPED
4247 ? "stopped" : "stopping"),
4248 lwpid_of (thread));
d50171e4
PA
4249
4250 continue;
4251 }
4252
5a04c4cf
PA
4253 /* Ignore (wildcard) resume requests for already-resumed
4254 threads. */
5fdda392 4255 if (resume[ndx].kind != resume_stop
5a04c4cf
PA
4256 && thread->last_resume_kind != resume_stop)
4257 {
c058728c
SM
4258 threads_debug_printf
4259 ("already %s LWP %ld at GDB's request",
4260 (thread->last_resume_kind == resume_step
4261 ? "stepping" : "continuing"),
4262 lwpid_of (thread));
5a04c4cf
PA
4263 continue;
4264 }
4265
4266 /* Don't let wildcard resumes resume fork children that GDB
4267 does not yet know are new fork children. */
4268 if (lwp->fork_relative != NULL)
4269 {
5a04c4cf
PA
4270 struct lwp_info *rel = lwp->fork_relative;
4271
4272 if (rel->status_pending_p
183be222
SM
4273 && (rel->waitstatus.kind () == TARGET_WAITKIND_FORKED
4274 || rel->waitstatus.kind () == TARGET_WAITKIND_VFORKED))
5a04c4cf 4275 {
c058728c
SM
4276 threads_debug_printf
4277 ("not resuming LWP %ld: has queued stop reply",
4278 lwpid_of (thread));
5a04c4cf
PA
4279 continue;
4280 }
4281 }
4282
4283 /* If the thread has a pending event that has already been
4284 reported to GDBserver core, but GDB has not pulled the
4285 event out of the vStopped queue yet, likewise, ignore the
4286 (wildcard) resume request. */
9c80ecd6 4287 if (in_queued_stop_replies (thread->id))
5a04c4cf 4288 {
c058728c
SM
4289 threads_debug_printf
4290 ("not resuming LWP %ld: has queued stop reply",
4291 lwpid_of (thread));
5a04c4cf
PA
4292 continue;
4293 }
4294
5fdda392 4295 lwp->resume = &resume[ndx];
8336d594 4296 thread->last_resume_kind = lwp->resume->kind;
fa593d66 4297
c2d6af84
PA
4298 lwp->step_range_start = lwp->resume->step_range_start;
4299 lwp->step_range_end = lwp->resume->step_range_end;
4300
fa593d66
PA
4301 /* If we had a deferred signal to report, dequeue one now.
4302 This can happen if LWP gets more than one signal while
4303 trying to get out of a jump pad. */
4304 if (lwp->stopped
4305 && !lwp->status_pending_p
4306 && dequeue_one_deferred_signal (lwp, &lwp->status_pending))
4307 {
4308 lwp->status_pending_p = 1;
4309
c058728c
SM
4310 threads_debug_printf
4311 ("Dequeueing deferred signal %d for LWP %ld, "
4312 "leaving status pending.",
4313 WSTOPSIG (lwp->status_pending),
4314 lwpid_of (thread));
fa593d66
PA
4315 }
4316
5fdda392 4317 return;
95954743
PA
4318 }
4319 }
2bd7c093
PA
4320
4321 /* No resume action for this thread. */
4322 lwp->resume = NULL;
5544ad89
DJ
4323}
4324
df95181f
TBA
4325bool
4326linux_process_target::resume_status_pending (thread_info *thread)
5544ad89 4327{
d86d4aaf 4328 struct lwp_info *lwp = get_thread_lwp (thread);
5544ad89 4329
bd99dc85
PA
4330 /* LWPs which will not be resumed are not interesting, because
4331 we might not wait for them next time through linux_wait. */
2bd7c093 4332 if (lwp->resume == NULL)
25c28b4d 4333 return false;
64386c31 4334
df95181f 4335 return thread_still_has_status_pending (thread);
d50171e4
PA
4336}
4337
df95181f
TBA
4338bool
4339linux_process_target::thread_needs_step_over (thread_info *thread)
d50171e4 4340{
d86d4aaf 4341 struct lwp_info *lwp = get_thread_lwp (thread);
d50171e4 4342 CORE_ADDR pc;
c06cbd92
YQ
4343 struct process_info *proc = get_thread_process (thread);
4344
4345 /* GDBserver is skipping the extra traps from the wrapper program,
4346 don't have to do step over. */
4347 if (proc->tdesc == NULL)
eca55aec 4348 return false;
d50171e4
PA
4349
4350 /* LWPs which will not be resumed are not interesting, because we
4351 might not wait for them next time through linux_wait. */
4352
4353 if (!lwp->stopped)
4354 {
c058728c
SM
4355 threads_debug_printf ("Need step over [LWP %ld]? Ignoring, not stopped",
4356 lwpid_of (thread));
eca55aec 4357 return false;
d50171e4
PA
4358 }
4359
8336d594 4360 if (thread->last_resume_kind == resume_stop)
d50171e4 4361 {
c058728c
SM
4362 threads_debug_printf
4363 ("Need step over [LWP %ld]? Ignoring, should remain stopped",
4364 lwpid_of (thread));
eca55aec 4365 return false;
d50171e4
PA
4366 }
4367
7984d532
PA
4368 gdb_assert (lwp->suspended >= 0);
4369
4370 if (lwp->suspended)
4371 {
c058728c
SM
4372 threads_debug_printf ("Need step over [LWP %ld]? Ignoring, suspended",
4373 lwpid_of (thread));
eca55aec 4374 return false;
7984d532
PA
4375 }
4376
bd99dc85 4377 if (lwp->status_pending_p)
d50171e4 4378 {
c058728c
SM
4379 threads_debug_printf
4380 ("Need step over [LWP %ld]? Ignoring, has pending status.",
4381 lwpid_of (thread));
eca55aec 4382 return false;
d50171e4
PA
4383 }
4384
4385 /* Note: PC, not STOP_PC. Either GDB has adjusted the PC already,
4386 or we have. */
4387 pc = get_pc (lwp);
4388
4389 /* If the PC has changed since we stopped, then don't do anything,
4390 and let the breakpoint/tracepoint be hit. This happens if, for
4391 instance, GDB handled the decr_pc_after_break subtraction itself,
4392 GDB is OOL stepping this thread, or the user has issued a "jump"
4393 command, or poked thread's registers herself. */
4394 if (pc != lwp->stop_pc)
4395 {
c058728c
SM
4396 threads_debug_printf
4397 ("Need step over [LWP %ld]? Cancelling, PC was changed. "
4398 "Old stop_pc was 0x%s, PC is now 0x%s", lwpid_of (thread),
4399 paddress (lwp->stop_pc), paddress (pc));
eca55aec 4400 return false;
d50171e4
PA
4401 }
4402
484b3c32
YQ
4403 /* On software single step target, resume the inferior with signal
4404 rather than stepping over. */
7582c77c 4405 if (supports_software_single_step ()
013e3554 4406 && !lwp->pending_signals.empty ()
484b3c32
YQ
4407 && lwp_signal_can_be_delivered (lwp))
4408 {
c058728c
SM
4409 threads_debug_printf
4410 ("Need step over [LWP %ld]? Ignoring, has pending signals.",
4411 lwpid_of (thread));
484b3c32 4412
eca55aec 4413 return false;
484b3c32
YQ
4414 }
4415
24583e45
TBA
4416 scoped_restore_current_thread restore_thread;
4417 switch_to_thread (thread);
d50171e4 4418
8b07ae33 4419 /* We can only step over breakpoints we know about. */
fa593d66 4420 if (breakpoint_here (pc) || fast_tracepoint_jump_here (pc))
d50171e4 4421 {
8b07ae33 4422 /* Don't step over a breakpoint that GDB expects to hit
9f3a5c85
LM
4423 though. If the condition is being evaluated on the target's side
4424 and it evaluate to false, step over this breakpoint as well. */
4425 if (gdb_breakpoint_here (pc)
d3ce09f5
SS
4426 && gdb_condition_true_at_breakpoint (pc)
4427 && gdb_no_commands_at_breakpoint (pc))
8b07ae33 4428 {
c058728c
SM
4429 threads_debug_printf ("Need step over [LWP %ld]? yes, but found"
4430 " GDB breakpoint at 0x%s; skipping step over",
4431 lwpid_of (thread), paddress (pc));
d50171e4 4432
eca55aec 4433 return false;
8b07ae33
PA
4434 }
4435 else
4436 {
c058728c
SM
4437 threads_debug_printf ("Need step over [LWP %ld]? yes, "
4438 "found breakpoint at 0x%s",
4439 lwpid_of (thread), paddress (pc));
d50171e4 4440
8b07ae33 4441 /* We've found an lwp that needs stepping over --- return 1 so
8f86d7aa 4442 that find_thread stops looking. */
eca55aec 4443 return true;
8b07ae33 4444 }
d50171e4
PA
4445 }
4446
c058728c
SM
4447 threads_debug_printf
4448 ("Need step over [LWP %ld]? No, no breakpoint found at 0x%s",
4449 lwpid_of (thread), paddress (pc));
c6ecbae5 4450
eca55aec 4451 return false;
5544ad89
DJ
4452}
4453
d16f3f6c
TBA
4454void
4455linux_process_target::start_step_over (lwp_info *lwp)
d50171e4 4456{
d86d4aaf 4457 struct thread_info *thread = get_lwp_thread (lwp);
d50171e4 4458 CORE_ADDR pc;
d50171e4 4459
c058728c
SM
4460 threads_debug_printf ("Starting step-over on LWP %ld. Stopping all threads",
4461 lwpid_of (thread));
d50171e4 4462
7984d532 4463 stop_all_lwps (1, lwp);
863d01bd
PA
4464
4465 if (lwp->suspended != 0)
4466 {
f34652de 4467 internal_error ("LWP %ld suspended=%d\n", lwpid_of (thread),
863d01bd
PA
4468 lwp->suspended);
4469 }
d50171e4 4470
c058728c 4471 threads_debug_printf ("Done stopping all threads for step-over.");
d50171e4
PA
4472
4473 /* Note, we should always reach here with an already adjusted PC,
4474 either by GDB (if we're resuming due to GDB's request), or by our
4475 caller, if we just finished handling an internal breakpoint GDB
4476 shouldn't care about. */
4477 pc = get_pc (lwp);
4478
24583e45
TBA
4479 bool step = false;
4480 {
4481 scoped_restore_current_thread restore_thread;
4482 switch_to_thread (thread);
d50171e4 4483
24583e45
TBA
4484 lwp->bp_reinsert = pc;
4485 uninsert_breakpoints_at (pc);
4486 uninsert_fast_tracepoint_jumps_at (pc);
d50171e4 4487
24583e45
TBA
4488 step = single_step (lwp);
4489 }
d50171e4 4490
df95181f 4491 resume_one_lwp (lwp, step, 0, NULL);
d50171e4
PA
4492
4493 /* Require next event from this LWP. */
9c80ecd6 4494 step_over_bkpt = thread->id;
d50171e4
PA
4495}
4496
b31cdfa6
TBA
4497bool
4498linux_process_target::finish_step_over (lwp_info *lwp)
d50171e4
PA
4499{
4500 if (lwp->bp_reinsert != 0)
4501 {
24583e45 4502 scoped_restore_current_thread restore_thread;
f79b145d 4503
c058728c 4504 threads_debug_printf ("Finished step over.");
d50171e4 4505
24583e45 4506 switch_to_thread (get_lwp_thread (lwp));
f79b145d 4507
d50171e4
PA
4508 /* Reinsert any breakpoint at LWP->BP_REINSERT. Note that there
4509 may be no breakpoint to reinsert there by now. */
4510 reinsert_breakpoints_at (lwp->bp_reinsert);
fa593d66 4511 reinsert_fast_tracepoint_jumps_at (lwp->bp_reinsert);
d50171e4
PA
4512
4513 lwp->bp_reinsert = 0;
4514
3b9a79ef
YQ
4515 /* Delete any single-step breakpoints. No longer needed. We
4516 don't have to worry about other threads hitting this trap,
4517 and later not being able to explain it, because we were
4518 stepping over a breakpoint, and we hold all threads but
4519 LWP stopped while doing that. */
b31cdfa6 4520 if (!supports_hardware_single_step ())
f79b145d 4521 {
3b9a79ef
YQ
4522 gdb_assert (has_single_step_breakpoints (current_thread));
4523 delete_single_step_breakpoints (current_thread);
f79b145d 4524 }
d50171e4
PA
4525
4526 step_over_bkpt = null_ptid;
b31cdfa6 4527 return true;
d50171e4
PA
4528 }
4529 else
b31cdfa6 4530 return false;
d50171e4
PA
4531}
4532
d16f3f6c
TBA
4533void
4534linux_process_target::complete_ongoing_step_over ()
863d01bd 4535{
d7e15655 4536 if (step_over_bkpt != null_ptid)
863d01bd
PA
4537 {
4538 struct lwp_info *lwp;
4539 int wstat;
4540 int ret;
4541
c058728c 4542 threads_debug_printf ("detach: step over in progress, finish it first");
863d01bd
PA
4543
4544 /* Passing NULL_PTID as filter indicates we want all events to
4545 be left pending. Eventually this returns when there are no
4546 unwaited-for children left. */
d16f3f6c
TBA
4547 ret = wait_for_event_filtered (minus_one_ptid, null_ptid, &wstat,
4548 __WALL);
863d01bd
PA
4549 gdb_assert (ret == -1);
4550
4551 lwp = find_lwp_pid (step_over_bkpt);
4552 if (lwp != NULL)
7e9cf1fe
PA
4553 {
4554 finish_step_over (lwp);
4555
4556 /* If we got our step SIGTRAP, don't leave it pending,
4557 otherwise we would report it to GDB as a spurious
4558 SIGTRAP. */
4559 gdb_assert (lwp->status_pending_p);
4560 if (WIFSTOPPED (lwp->status_pending)
4561 && WSTOPSIG (lwp->status_pending) == SIGTRAP)
4562 {
4563 thread_info *thread = get_lwp_thread (lwp);
4564 if (thread->last_resume_kind != resume_step)
4565 {
c058728c 4566 threads_debug_printf ("detach: discard step-over SIGTRAP");
7e9cf1fe
PA
4567
4568 lwp->status_pending_p = 0;
4569 lwp->status_pending = 0;
4570 resume_one_lwp (lwp, lwp->stepping, 0, NULL);
4571 }
4572 else
c058728c
SM
4573 threads_debug_printf
4574 ("detach: resume_step, not discarding step-over SIGTRAP");
7e9cf1fe
PA
4575 }
4576 }
863d01bd
PA
4577 step_over_bkpt = null_ptid;
4578 unsuspend_all_lwps (lwp);
4579 }
4580}
4581
df95181f
TBA
4582void
4583linux_process_target::resume_one_thread (thread_info *thread,
4584 bool leave_all_stopped)
5544ad89 4585{
d86d4aaf 4586 struct lwp_info *lwp = get_thread_lwp (thread);
d50171e4 4587 int leave_pending;
5544ad89 4588
2bd7c093 4589 if (lwp->resume == NULL)
c80825ff 4590 return;
5544ad89 4591
bd99dc85 4592 if (lwp->resume->kind == resume_stop)
5544ad89 4593 {
c058728c
SM
4594 threads_debug_printf ("resume_stop request for LWP %ld",
4595 lwpid_of (thread));
bd99dc85
PA
4596
4597 if (!lwp->stopped)
4598 {
c058728c 4599 threads_debug_printf ("stopping LWP %ld", lwpid_of (thread));
bd99dc85 4600
d50171e4
PA
4601 /* Stop the thread, and wait for the event asynchronously,
4602 through the event loop. */
02fc4de7 4603 send_sigstop (lwp);
bd99dc85
PA
4604 }
4605 else
4606 {
c058728c 4607 threads_debug_printf ("already stopped LWP %ld", lwpid_of (thread));
d50171e4
PA
4608
4609 /* The LWP may have been stopped in an internal event that
4610 was not meant to be notified back to GDB (e.g., gdbserver
4611 breakpoint), so we should be reporting a stop event in
4612 this case too. */
4613
4614 /* If the thread already has a pending SIGSTOP, this is a
4615 no-op. Otherwise, something later will presumably resume
4616 the thread and this will cause it to cancel any pending
4617 operation, due to last_resume_kind == resume_stop. If
4618 the thread already has a pending status to report, we
4619 will still report it the next time we wait - see
4620 status_pending_p_callback. */
1a981360
PA
4621
4622 /* If we already have a pending signal to report, then
4623 there's no need to queue a SIGSTOP, as this means we're
4624 midway through moving the LWP out of the jumppad, and we
4625 will report the pending signal as soon as that is
4626 finished. */
013e3554 4627 if (lwp->pending_signals_to_report.empty ())
1a981360 4628 send_sigstop (lwp);
bd99dc85 4629 }
32ca6d61 4630
bd99dc85
PA
4631 /* For stop requests, we're done. */
4632 lwp->resume = NULL;
183be222 4633 thread->last_status.set_ignore ();
c80825ff 4634 return;
5544ad89
DJ
4635 }
4636
bd99dc85 4637 /* If this thread which is about to be resumed has a pending status,
863d01bd
PA
4638 then don't resume it - we can just report the pending status.
4639 Likewise if it is suspended, because e.g., another thread is
4640 stepping past a breakpoint. Make sure to queue any signals that
4641 would otherwise be sent. In all-stop mode, we do this decision
4642 based on if *any* thread has a pending status. If there's a
4643 thread that needs the step-over-breakpoint dance, then don't
4644 resume any other thread but that particular one. */
4645 leave_pending = (lwp->suspended
4646 || lwp->status_pending_p
4647 || leave_all_stopped);
5544ad89 4648
0e9a339e
YQ
4649 /* If we have a new signal, enqueue the signal. */
4650 if (lwp->resume->sig != 0)
4651 {
4652 siginfo_t info, *info_p;
4653
4654 /* If this is the same signal we were previously stopped by,
4655 make sure to queue its siginfo. */
4656 if (WIFSTOPPED (lwp->last_status)
4657 && WSTOPSIG (lwp->last_status) == lwp->resume->sig
4658 && ptrace (PTRACE_GETSIGINFO, lwpid_of (thread),
4659 (PTRACE_TYPE_ARG3) 0, &info) == 0)
4660 info_p = &info;
4661 else
4662 info_p = NULL;
4663
4664 enqueue_pending_signal (lwp, lwp->resume->sig, info_p);
4665 }
4666
d50171e4 4667 if (!leave_pending)
bd99dc85 4668 {
c058728c 4669 threads_debug_printf ("resuming LWP %ld", lwpid_of (thread));
5544ad89 4670
9c80ecd6 4671 proceed_one_lwp (thread, NULL);
bd99dc85
PA
4672 }
4673 else
c058728c 4674 threads_debug_printf ("leaving LWP %ld stopped", lwpid_of (thread));
5544ad89 4675
183be222 4676 thread->last_status.set_ignore ();
bd99dc85 4677 lwp->resume = NULL;
0d62e5e8
DJ
4678}
4679
0e4d7e35
TBA
4680void
4681linux_process_target::resume (thread_resume *resume_info, size_t n)
0d62e5e8 4682{
d86d4aaf 4683 struct thread_info *need_step_over = NULL;
c6ecbae5 4684
c058728c 4685 THREADS_SCOPED_DEBUG_ENTER_EXIT;
87ce2a04 4686
5fdda392
SM
4687 for_each_thread ([&] (thread_info *thread)
4688 {
4689 linux_set_resume_request (thread, resume_info, n);
4690 });
5544ad89 4691
d50171e4
PA
4692 /* If there is a thread which would otherwise be resumed, which has
4693 a pending status, then don't resume any threads - we can just
4694 report the pending status. Make sure to queue any signals that
4695 would otherwise be sent. In non-stop mode, we'll apply this
4696 logic to each thread individually. We consume all pending events
4697 before considering to start a step-over (in all-stop). */
25c28b4d 4698 bool any_pending = false;
bd99dc85 4699 if (!non_stop)
df95181f
TBA
4700 any_pending = find_thread ([this] (thread_info *thread)
4701 {
4702 return resume_status_pending (thread);
4703 }) != nullptr;
d50171e4
PA
4704
4705 /* If there is a thread which would otherwise be resumed, which is
4706 stopped at a breakpoint that needs stepping over, then don't
4707 resume any threads - have it step over the breakpoint with all
4708 other threads stopped, then resume all threads again. Make sure
4709 to queue any signals that would otherwise be delivered or
4710 queued. */
bf9ae9d8 4711 if (!any_pending && low_supports_breakpoints ())
df95181f
TBA
4712 need_step_over = find_thread ([this] (thread_info *thread)
4713 {
4714 return thread_needs_step_over (thread);
4715 });
d50171e4 4716
c80825ff 4717 bool leave_all_stopped = (need_step_over != NULL || any_pending);
d50171e4 4718
c058728c
SM
4719 if (need_step_over != NULL)
4720 threads_debug_printf ("Not resuming all, need step over");
4721 else if (any_pending)
4722 threads_debug_printf ("Not resuming, all-stop and found "
4723 "an LWP with pending status");
4724 else
4725 threads_debug_printf ("Resuming, no pending status or step over needed");
d50171e4
PA
4726
4727 /* Even if we're leaving threads stopped, queue all signals we'd
4728 otherwise deliver. */
c80825ff
SM
4729 for_each_thread ([&] (thread_info *thread)
4730 {
df95181f 4731 resume_one_thread (thread, leave_all_stopped);
c80825ff 4732 });
d50171e4
PA
4733
4734 if (need_step_over)
d86d4aaf 4735 start_step_over (get_thread_lwp (need_step_over));
87ce2a04 4736
1bebeeca
PA
4737 /* We may have events that were pending that can/should be sent to
4738 the client now. Trigger a linux_wait call. */
4739 if (target_is_async_p ())
4740 async_file_mark ();
d50171e4
PA
4741}
4742
df95181f
TBA
4743void
4744linux_process_target::proceed_one_lwp (thread_info *thread, lwp_info *except)
d50171e4 4745{
d86d4aaf 4746 struct lwp_info *lwp = get_thread_lwp (thread);
d50171e4
PA
4747 int step;
4748
7984d532 4749 if (lwp == except)
e2b44075 4750 return;
d50171e4 4751
c058728c 4752 threads_debug_printf ("lwp %ld", lwpid_of (thread));
d50171e4
PA
4753
4754 if (!lwp->stopped)
4755 {
c058728c 4756 threads_debug_printf (" LWP %ld already running", lwpid_of (thread));
e2b44075 4757 return;
d50171e4
PA
4758 }
4759
02fc4de7 4760 if (thread->last_resume_kind == resume_stop
183be222 4761 && thread->last_status.kind () != TARGET_WAITKIND_IGNORE)
d50171e4 4762 {
c058728c
SM
4763 threads_debug_printf (" client wants LWP to remain %ld stopped",
4764 lwpid_of (thread));
e2b44075 4765 return;
d50171e4
PA
4766 }
4767
4768 if (lwp->status_pending_p)
4769 {
c058728c
SM
4770 threads_debug_printf (" LWP %ld has pending status, leaving stopped",
4771 lwpid_of (thread));
e2b44075 4772 return;
d50171e4
PA
4773 }
4774
7984d532
PA
4775 gdb_assert (lwp->suspended >= 0);
4776
d50171e4
PA
4777 if (lwp->suspended)
4778 {
c058728c 4779 threads_debug_printf (" LWP %ld is suspended", lwpid_of (thread));
e2b44075 4780 return;
d50171e4
PA
4781 }
4782
1a981360 4783 if (thread->last_resume_kind == resume_stop
013e3554 4784 && lwp->pending_signals_to_report.empty ()
229d26fc
SM
4785 && (lwp->collecting_fast_tracepoint
4786 == fast_tpoint_collect_result::not_collecting))
02fc4de7
PA
4787 {
4788 /* We haven't reported this LWP as stopped yet (otherwise, the
4789 last_status.kind check above would catch it, and we wouldn't
4790 reach here. This LWP may have been momentarily paused by a
4791 stop_all_lwps call while handling for example, another LWP's
4792 step-over. In that case, the pending expected SIGSTOP signal
4793 that was queued at vCont;t handling time will have already
4794 been consumed by wait_for_sigstop, and so we need to requeue
4795 another one here. Note that if the LWP already has a SIGSTOP
4796 pending, this is a no-op. */
4797
c058728c
SM
4798 threads_debug_printf
4799 ("Client wants LWP %ld to stop. Making sure it has a SIGSTOP pending",
4800 lwpid_of (thread));
02fc4de7
PA
4801
4802 send_sigstop (lwp);
4803 }
4804
863d01bd
PA
4805 if (thread->last_resume_kind == resume_step)
4806 {
c058728c
SM
4807 threads_debug_printf (" stepping LWP %ld, client wants it stepping",
4808 lwpid_of (thread));
8901d193 4809
3b9a79ef 4810 /* If resume_step is requested by GDB, install single-step
8901d193 4811 breakpoints when the thread is about to be actually resumed if
3b9a79ef 4812 the single-step breakpoints weren't removed. */
7582c77c 4813 if (supports_software_single_step ()
3b9a79ef 4814 && !has_single_step_breakpoints (thread))
8901d193
YQ
4815 install_software_single_step_breakpoints (lwp);
4816
4817 step = maybe_hw_step (thread);
863d01bd
PA
4818 }
4819 else if (lwp->bp_reinsert != 0)
4820 {
c058728c
SM
4821 threads_debug_printf (" stepping LWP %ld, reinsert set",
4822 lwpid_of (thread));
f79b145d
YQ
4823
4824 step = maybe_hw_step (thread);
863d01bd
PA
4825 }
4826 else
4827 step = 0;
4828
df95181f 4829 resume_one_lwp (lwp, step, 0, NULL);
7984d532
PA
4830}
4831
df95181f
TBA
4832void
4833linux_process_target::unsuspend_and_proceed_one_lwp (thread_info *thread,
4834 lwp_info *except)
7984d532 4835{
d86d4aaf 4836 struct lwp_info *lwp = get_thread_lwp (thread);
7984d532
PA
4837
4838 if (lwp == except)
e2b44075 4839 return;
7984d532 4840
863d01bd 4841 lwp_suspended_decr (lwp);
7984d532 4842
e2b44075 4843 proceed_one_lwp (thread, except);
d50171e4
PA
4844}
4845
d16f3f6c
TBA
4846void
4847linux_process_target::proceed_all_lwps ()
d50171e4 4848{
d86d4aaf 4849 struct thread_info *need_step_over;
d50171e4
PA
4850
4851 /* If there is a thread which would otherwise be resumed, which is
4852 stopped at a breakpoint that needs stepping over, then don't
4853 resume any threads - have it step over the breakpoint with all
4854 other threads stopped, then resume all threads again. */
4855
bf9ae9d8 4856 if (low_supports_breakpoints ())
d50171e4 4857 {
df95181f
TBA
4858 need_step_over = find_thread ([this] (thread_info *thread)
4859 {
4860 return thread_needs_step_over (thread);
4861 });
d50171e4
PA
4862
4863 if (need_step_over != NULL)
4864 {
c058728c
SM
4865 threads_debug_printf ("found thread %ld needing a step-over",
4866 lwpid_of (need_step_over));
d50171e4 4867
d86d4aaf 4868 start_step_over (get_thread_lwp (need_step_over));
d50171e4
PA
4869 return;
4870 }
4871 }
5544ad89 4872
c058728c 4873 threads_debug_printf ("Proceeding, no step-over needed");
d50171e4 4874
df95181f 4875 for_each_thread ([this] (thread_info *thread)
e2b44075
SM
4876 {
4877 proceed_one_lwp (thread, NULL);
4878 });
d50171e4
PA
4879}
4880
d16f3f6c
TBA
4881void
4882linux_process_target::unstop_all_lwps (int unsuspend, lwp_info *except)
d50171e4 4883{
c058728c
SM
4884 THREADS_SCOPED_DEBUG_ENTER_EXIT;
4885
4886 if (except)
4887 threads_debug_printf ("except=(LWP %ld)",
4888 lwpid_of (get_lwp_thread (except)));
4889 else
4890 threads_debug_printf ("except=nullptr");
5544ad89 4891
7984d532 4892 if (unsuspend)
e2b44075
SM
4893 for_each_thread ([&] (thread_info *thread)
4894 {
4895 unsuspend_and_proceed_one_lwp (thread, except);
4896 });
7984d532 4897 else
e2b44075
SM
4898 for_each_thread ([&] (thread_info *thread)
4899 {
4900 proceed_one_lwp (thread, except);
4901 });
0d62e5e8
DJ
4902}
4903
58caa3dc
DJ
4904
4905#ifdef HAVE_LINUX_REGSETS
4906
1faeff08
MR
4907#define use_linux_regsets 1
4908
030031ee
PA
4909/* Returns true if REGSET has been disabled. */
4910
4911static int
4912regset_disabled (struct regsets_info *info, struct regset_info *regset)
4913{
4914 return (info->disabled_regsets != NULL
4915 && info->disabled_regsets[regset - info->regsets]);
4916}
4917
4918/* Disable REGSET. */
4919
4920static void
4921disable_regset (struct regsets_info *info, struct regset_info *regset)
4922{
4923 int dr_offset;
4924
4925 dr_offset = regset - info->regsets;
4926 if (info->disabled_regsets == NULL)
224c3ddb 4927 info->disabled_regsets = (char *) xcalloc (1, info->num_regsets);
030031ee
PA
4928 info->disabled_regsets[dr_offset] = 1;
4929}
4930
58caa3dc 4931static int
3aee8918
PA
4932regsets_fetch_inferior_registers (struct regsets_info *regsets_info,
4933 struct regcache *regcache)
58caa3dc
DJ
4934{
4935 struct regset_info *regset;
e9d25b98 4936 int saw_general_regs = 0;
95954743 4937 int pid;
1570b33e 4938 struct iovec iov;
58caa3dc 4939
0bfdf32f 4940 pid = lwpid_of (current_thread);
28eef672 4941 for (regset = regsets_info->regsets; regset->size >= 0; regset++)
58caa3dc 4942 {
1570b33e
L
4943 void *buf, *data;
4944 int nt_type, res;
58caa3dc 4945
030031ee 4946 if (regset->size == 0 || regset_disabled (regsets_info, regset))
28eef672 4947 continue;
58caa3dc 4948
bca929d3 4949 buf = xmalloc (regset->size);
1570b33e
L
4950
4951 nt_type = regset->nt_type;
4952 if (nt_type)
4953 {
4954 iov.iov_base = buf;
4955 iov.iov_len = regset->size;
4956 data = (void *) &iov;
4957 }
4958 else
4959 data = buf;
4960
dfb64f85 4961#ifndef __sparc__
f15f9948 4962 res = ptrace (regset->get_request, pid,
b8e1b30e 4963 (PTRACE_TYPE_ARG3) (long) nt_type, data);
dfb64f85 4964#else
1570b33e 4965 res = ptrace (regset->get_request, pid, data, nt_type);
dfb64f85 4966#endif
58caa3dc
DJ
4967 if (res < 0)
4968 {
1ef53e6b
AH
4969 if (errno == EIO
4970 || (errno == EINVAL && regset->type == OPTIONAL_REGS))
58caa3dc 4971 {
1ef53e6b
AH
4972 /* If we get EIO on a regset, or an EINVAL and the regset is
4973 optional, do not try it again for this process mode. */
030031ee 4974 disable_regset (regsets_info, regset);
58caa3dc 4975 }
e5a9158d
AA
4976 else if (errno == ENODATA)
4977 {
4978 /* ENODATA may be returned if the regset is currently
4979 not "active". This can happen in normal operation,
4980 so suppress the warning in this case. */
4981 }
fcd4a73d
YQ
4982 else if (errno == ESRCH)
4983 {
4984 /* At this point, ESRCH should mean the process is
4985 already gone, in which case we simply ignore attempts
4986 to read its registers. */
4987 }
58caa3dc
DJ
4988 else
4989 {
0d62e5e8 4990 char s[256];
95954743
PA
4991 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
4992 pid);
0d62e5e8 4993 perror (s);
58caa3dc
DJ
4994 }
4995 }
098dbe61
AA
4996 else
4997 {
4998 if (regset->type == GENERAL_REGS)
4999 saw_general_regs = 1;
5000 regset->store_function (regcache, buf);
5001 }
fdeb2a12 5002 free (buf);
58caa3dc 5003 }
e9d25b98
DJ
5004 if (saw_general_regs)
5005 return 0;
5006 else
5007 return 1;
58caa3dc
DJ
5008}
5009
5010static int
3aee8918
PA
5011regsets_store_inferior_registers (struct regsets_info *regsets_info,
5012 struct regcache *regcache)
58caa3dc
DJ
5013{
5014 struct regset_info *regset;
e9d25b98 5015 int saw_general_regs = 0;
95954743 5016 int pid;
1570b33e 5017 struct iovec iov;
58caa3dc 5018
0bfdf32f 5019 pid = lwpid_of (current_thread);
28eef672 5020 for (regset = regsets_info->regsets; regset->size >= 0; regset++)
58caa3dc 5021 {
1570b33e
L
5022 void *buf, *data;
5023 int nt_type, res;
58caa3dc 5024
feea5f36
AA
5025 if (regset->size == 0 || regset_disabled (regsets_info, regset)
5026 || regset->fill_function == NULL)
28eef672 5027 continue;
58caa3dc 5028
bca929d3 5029 buf = xmalloc (regset->size);
545587ee
DJ
5030
5031 /* First fill the buffer with the current register set contents,
5032 in case there are any items in the kernel's regset that are
5033 not in gdbserver's regcache. */
1570b33e
L
5034
5035 nt_type = regset->nt_type;
5036 if (nt_type)
5037 {
5038 iov.iov_base = buf;
5039 iov.iov_len = regset->size;
5040 data = (void *) &iov;
5041 }
5042 else
5043 data = buf;
5044
dfb64f85 5045#ifndef __sparc__
f15f9948 5046 res = ptrace (regset->get_request, pid,
b8e1b30e 5047 (PTRACE_TYPE_ARG3) (long) nt_type, data);
dfb64f85 5048#else
689cc2ae 5049 res = ptrace (regset->get_request, pid, data, nt_type);
dfb64f85 5050#endif
545587ee
DJ
5051
5052 if (res == 0)
5053 {
5054 /* Then overlay our cached registers on that. */
442ea881 5055 regset->fill_function (regcache, buf);
545587ee
DJ
5056
5057 /* Only now do we write the register set. */
dfb64f85 5058#ifndef __sparc__
f15f9948 5059 res = ptrace (regset->set_request, pid,
b8e1b30e 5060 (PTRACE_TYPE_ARG3) (long) nt_type, data);
dfb64f85 5061#else
1570b33e 5062 res = ptrace (regset->set_request, pid, data, nt_type);
dfb64f85 5063#endif
545587ee
DJ
5064 }
5065
58caa3dc
DJ
5066 if (res < 0)
5067 {
1ef53e6b
AH
5068 if (errno == EIO
5069 || (errno == EINVAL && regset->type == OPTIONAL_REGS))
58caa3dc 5070 {
1ef53e6b
AH
5071 /* If we get EIO on a regset, or an EINVAL and the regset is
5072 optional, do not try it again for this process mode. */
030031ee 5073 disable_regset (regsets_info, regset);
58caa3dc 5074 }
3221518c
UW
5075 else if (errno == ESRCH)
5076 {
1b3f6016
PA
5077 /* At this point, ESRCH should mean the process is
5078 already gone, in which case we simply ignore attempts
5079 to change its registers. See also the related
df95181f 5080 comment in resume_one_lwp. */
fdeb2a12 5081 free (buf);
3221518c
UW
5082 return 0;
5083 }
58caa3dc
DJ
5084 else
5085 {
ce3a066d 5086 perror ("Warning: ptrace(regsets_store_inferior_registers)");
58caa3dc
DJ
5087 }
5088 }
e9d25b98
DJ
5089 else if (regset->type == GENERAL_REGS)
5090 saw_general_regs = 1;
09ec9b38 5091 free (buf);
58caa3dc 5092 }
e9d25b98
DJ
5093 if (saw_general_regs)
5094 return 0;
5095 else
5096 return 1;
58caa3dc
DJ
5097}
5098
1faeff08 5099#else /* !HAVE_LINUX_REGSETS */
58caa3dc 5100
1faeff08 5101#define use_linux_regsets 0
3aee8918
PA
5102#define regsets_fetch_inferior_registers(regsets_info, regcache) 1
5103#define regsets_store_inferior_registers(regsets_info, regcache) 1
58caa3dc 5104
58caa3dc 5105#endif
1faeff08
MR
5106
5107/* Return 1 if register REGNO is supported by one of the regset ptrace
5108 calls or 0 if it has to be transferred individually. */
5109
5110static int
3aee8918 5111linux_register_in_regsets (const struct regs_info *regs_info, int regno)
1faeff08
MR
5112{
5113 unsigned char mask = 1 << (regno % 8);
5114 size_t index = regno / 8;
5115
5116 return (use_linux_regsets
3aee8918
PA
5117 && (regs_info->regset_bitmap == NULL
5118 || (regs_info->regset_bitmap[index] & mask) != 0));
1faeff08
MR
5119}
5120
58caa3dc 5121#ifdef HAVE_LINUX_USRREGS
1faeff08 5122
5b3da067 5123static int
3aee8918 5124register_addr (const struct usrregs_info *usrregs, int regnum)
1faeff08
MR
5125{
5126 int addr;
5127
3aee8918 5128 if (regnum < 0 || regnum >= usrregs->num_regs)
1faeff08
MR
5129 error ("Invalid register number %d.", regnum);
5130
3aee8918 5131 addr = usrregs->regmap[regnum];
1faeff08
MR
5132
5133 return addr;
5134}
5135
daca57a7
TBA
5136
5137void
5138linux_process_target::fetch_register (const usrregs_info *usrregs,
5139 regcache *regcache, int regno)
1faeff08
MR
5140{
5141 CORE_ADDR regaddr;
5142 int i, size;
5143 char *buf;
5144 int pid;
5145
3aee8918 5146 if (regno >= usrregs->num_regs)
1faeff08 5147 return;
daca57a7 5148 if (low_cannot_fetch_register (regno))
1faeff08
MR
5149 return;
5150
3aee8918 5151 regaddr = register_addr (usrregs, regno);
1faeff08
MR
5152 if (regaddr == -1)
5153 return;
5154
3aee8918
PA
5155 size = ((register_size (regcache->tdesc, regno)
5156 + sizeof (PTRACE_XFER_TYPE) - 1)
1faeff08 5157 & -sizeof (PTRACE_XFER_TYPE));
224c3ddb 5158 buf = (char *) alloca (size);
1faeff08 5159
0bfdf32f 5160 pid = lwpid_of (current_thread);
1faeff08
MR
5161 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
5162 {
5163 errno = 0;
5164 *(PTRACE_XFER_TYPE *) (buf + i) =
5165 ptrace (PTRACE_PEEKUSER, pid,
5166 /* Coerce to a uintptr_t first to avoid potential gcc warning
5167 of coercing an 8 byte integer to a 4 byte pointer. */
b8e1b30e 5168 (PTRACE_TYPE_ARG3) (uintptr_t) regaddr, (PTRACE_TYPE_ARG4) 0);
1faeff08
MR
5169 regaddr += sizeof (PTRACE_XFER_TYPE);
5170 if (errno != 0)
9a70f35c
YQ
5171 {
5172 /* Mark register REGNO unavailable. */
5173 supply_register (regcache, regno, NULL);
5174 return;
5175 }
1faeff08
MR
5176 }
5177
b35db733 5178 low_supply_ptrace_register (regcache, regno, buf);
1faeff08
MR
5179}
5180
daca57a7
TBA
5181void
5182linux_process_target::store_register (const usrregs_info *usrregs,
5183 regcache *regcache, int regno)
1faeff08
MR
5184{
5185 CORE_ADDR regaddr;
5186 int i, size;
5187 char *buf;
5188 int pid;
5189
3aee8918 5190 if (regno >= usrregs->num_regs)
1faeff08 5191 return;
daca57a7 5192 if (low_cannot_store_register (regno))
1faeff08
MR
5193 return;
5194
3aee8918 5195 regaddr = register_addr (usrregs, regno);
1faeff08
MR
5196 if (regaddr == -1)
5197 return;
5198
3aee8918
PA
5199 size = ((register_size (regcache->tdesc, regno)
5200 + sizeof (PTRACE_XFER_TYPE) - 1)
1faeff08 5201 & -sizeof (PTRACE_XFER_TYPE));
224c3ddb 5202 buf = (char *) alloca (size);
1faeff08
MR
5203 memset (buf, 0, size);
5204
b35db733 5205 low_collect_ptrace_register (regcache, regno, buf);
1faeff08 5206
0bfdf32f 5207 pid = lwpid_of (current_thread);
1faeff08
MR
5208 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
5209 {
5210 errno = 0;
5211 ptrace (PTRACE_POKEUSER, pid,
5212 /* Coerce to a uintptr_t first to avoid potential gcc warning
5213 about coercing an 8 byte integer to a 4 byte pointer. */
b8e1b30e
LM
5214 (PTRACE_TYPE_ARG3) (uintptr_t) regaddr,
5215 (PTRACE_TYPE_ARG4) *(PTRACE_XFER_TYPE *) (buf + i));
1faeff08
MR
5216 if (errno != 0)
5217 {
5218 /* At this point, ESRCH should mean the process is
5219 already gone, in which case we simply ignore attempts
5220 to change its registers. See also the related
df95181f 5221 comment in resume_one_lwp. */
1faeff08
MR
5222 if (errno == ESRCH)
5223 return;
5224
daca57a7
TBA
5225
5226 if (!low_cannot_store_register (regno))
6d91ce9a 5227 error ("writing register %d: %s", regno, safe_strerror (errno));
1faeff08
MR
5228 }
5229 regaddr += sizeof (PTRACE_XFER_TYPE);
5230 }
5231}
daca57a7 5232#endif /* HAVE_LINUX_USRREGS */
1faeff08 5233
b35db733
TBA
5234void
5235linux_process_target::low_collect_ptrace_register (regcache *regcache,
5236 int regno, char *buf)
5237{
5238 collect_register (regcache, regno, buf);
5239}
5240
5241void
5242linux_process_target::low_supply_ptrace_register (regcache *regcache,
5243 int regno, const char *buf)
5244{
5245 supply_register (regcache, regno, buf);
5246}
5247
daca57a7
TBA
5248void
5249linux_process_target::usr_fetch_inferior_registers (const regs_info *regs_info,
5250 regcache *regcache,
5251 int regno, int all)
1faeff08 5252{
daca57a7 5253#ifdef HAVE_LINUX_USRREGS
3aee8918
PA
5254 struct usrregs_info *usr = regs_info->usrregs;
5255
1faeff08
MR
5256 if (regno == -1)
5257 {
3aee8918
PA
5258 for (regno = 0; regno < usr->num_regs; regno++)
5259 if (all || !linux_register_in_regsets (regs_info, regno))
5260 fetch_register (usr, regcache, regno);
1faeff08
MR
5261 }
5262 else
3aee8918 5263 fetch_register (usr, regcache, regno);
daca57a7 5264#endif
1faeff08
MR
5265}
5266
daca57a7
TBA
5267void
5268linux_process_target::usr_store_inferior_registers (const regs_info *regs_info,
5269 regcache *regcache,
5270 int regno, int all)
1faeff08 5271{
daca57a7 5272#ifdef HAVE_LINUX_USRREGS
3aee8918
PA
5273 struct usrregs_info *usr = regs_info->usrregs;
5274
1faeff08
MR
5275 if (regno == -1)
5276 {
3aee8918
PA
5277 for (regno = 0; regno < usr->num_regs; regno++)
5278 if (all || !linux_register_in_regsets (regs_info, regno))
5279 store_register (usr, regcache, regno);
1faeff08
MR
5280 }
5281 else
3aee8918 5282 store_register (usr, regcache, regno);
58caa3dc 5283#endif
daca57a7 5284}
1faeff08 5285
a5a4d4cd
TBA
5286void
5287linux_process_target::fetch_registers (regcache *regcache, int regno)
1faeff08
MR
5288{
5289 int use_regsets;
5290 int all = 0;
aa8d21c9 5291 const regs_info *regs_info = get_regs_info ();
1faeff08
MR
5292
5293 if (regno == -1)
5294 {
bd70b1f2 5295 if (regs_info->usrregs != NULL)
3aee8918 5296 for (regno = 0; regno < regs_info->usrregs->num_regs; regno++)
bd70b1f2 5297 low_fetch_register (regcache, regno);
c14dfd32 5298
3aee8918
PA
5299 all = regsets_fetch_inferior_registers (regs_info->regsets_info, regcache);
5300 if (regs_info->usrregs != NULL)
5301 usr_fetch_inferior_registers (regs_info, regcache, -1, all);
1faeff08
MR
5302 }
5303 else
5304 {
bd70b1f2 5305 if (low_fetch_register (regcache, regno))
c14dfd32
PA
5306 return;
5307
3aee8918 5308 use_regsets = linux_register_in_regsets (regs_info, regno);
1faeff08 5309 if (use_regsets)
3aee8918
PA
5310 all = regsets_fetch_inferior_registers (regs_info->regsets_info,
5311 regcache);
5312 if ((!use_regsets || all) && regs_info->usrregs != NULL)
5313 usr_fetch_inferior_registers (regs_info, regcache, regno, 1);
1faeff08 5314 }
58caa3dc
DJ
5315}
5316
a5a4d4cd
TBA
5317void
5318linux_process_target::store_registers (regcache *regcache, int regno)
58caa3dc 5319{
1faeff08
MR
5320 int use_regsets;
5321 int all = 0;
aa8d21c9 5322 const regs_info *regs_info = get_regs_info ();
1faeff08
MR
5323
5324 if (regno == -1)
5325 {
3aee8918
PA
5326 all = regsets_store_inferior_registers (regs_info->regsets_info,
5327 regcache);
5328 if (regs_info->usrregs != NULL)
5329 usr_store_inferior_registers (regs_info, regcache, regno, all);
1faeff08
MR
5330 }
5331 else
5332 {
3aee8918 5333 use_regsets = linux_register_in_regsets (regs_info, regno);
1faeff08 5334 if (use_regsets)
3aee8918
PA
5335 all = regsets_store_inferior_registers (regs_info->regsets_info,
5336 regcache);
5337 if ((!use_regsets || all) && regs_info->usrregs != NULL)
5338 usr_store_inferior_registers (regs_info, regcache, regno, 1);
1faeff08 5339 }
58caa3dc
DJ
5340}
5341
bd70b1f2
TBA
5342bool
5343linux_process_target::low_fetch_register (regcache *regcache, int regno)
5344{
5345 return false;
5346}
da6d8c04 5347
e2558df3 5348/* A wrapper for the read_memory target op. */
da6d8c04 5349
c3e735a6 5350static int
f450004a 5351linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
e2558df3 5352{
52405d85 5353 return the_target->read_memory (memaddr, myaddr, len);
e2558df3
TBA
5354}
5355
e2558df3 5356
421490af
PA
5357/* Helper for read_memory/write_memory using /proc/PID/mem. Because
5358 we can use a single read/write call, this can be much more
5359 efficient than banging away at PTRACE_PEEKTEXT. Also, unlike
5360 PTRACE_PEEKTEXT/PTRACE_POKETEXT, this works with running threads.
5361 One an only one of READBUF and WRITEBUF is non-null. If READBUF is
5362 not null, then we're reading, otherwise we're writing. */
5363
5364static int
5365proc_xfer_memory (CORE_ADDR memaddr, unsigned char *readbuf,
5366 const gdb_byte *writebuf, int len)
da6d8c04 5367{
421490af 5368 gdb_assert ((readbuf == nullptr) != (writebuf == nullptr));
fd462a61 5369
421490af
PA
5370 process_info *proc = current_process ();
5371
5372 int fd = proc->priv->mem_fd;
5373 if (fd == -1)
5374 return EIO;
5375
5376 while (len > 0)
fd462a61 5377 {
4934b29e
MR
5378 int bytes;
5379
fd462a61
DJ
5380 /* If pread64 is available, use it. It's faster if the kernel
5381 supports it (only one syscall), and it's 64-bit safe even on
5382 32-bit platforms (for instance, SPARC debugging a SPARC64
5383 application). */
5384#ifdef HAVE_PREAD64
421490af
PA
5385 bytes = (readbuf != nullptr
5386 ? pread64 (fd, readbuf, len, memaddr)
5387 : pwrite64 (fd, writebuf, len, memaddr));
fd462a61 5388#else
4934b29e
MR
5389 bytes = -1;
5390 if (lseek (fd, memaddr, SEEK_SET) != -1)
421490af
PA
5391 bytes = (readbuf != nullptr
5392 ? read (fd, readbuf, len)
5393 ? write (fd, writebuf, len));
fd462a61 5394#endif
fd462a61 5395
421490af
PA
5396 if (bytes < 0)
5397 return errno;
5398 else if (bytes == 0)
4934b29e 5399 {
421490af
PA
5400 /* EOF means the address space is gone, the whole process
5401 exited or execed. */
5402 return EIO;
4934b29e 5403 }
da6d8c04 5404
421490af
PA
5405 memaddr += bytes;
5406 if (readbuf != nullptr)
5407 readbuf += bytes;
5408 else
5409 writebuf += bytes;
5410 len -= bytes;
da6d8c04
DJ
5411 }
5412
421490af
PA
5413 return 0;
5414}
c3e735a6 5415
421490af
PA
5416int
5417linux_process_target::read_memory (CORE_ADDR memaddr,
5418 unsigned char *myaddr, int len)
5419{
5420 return proc_xfer_memory (memaddr, myaddr, nullptr, len);
da6d8c04
DJ
5421}
5422
93ae6fdc
PA
5423/* Copy LEN bytes of data from debugger memory at MYADDR to inferior's
5424 memory at MEMADDR. On failure (cannot write to the inferior)
f0ae6fc3 5425 returns the value of errno. Always succeeds if LEN is zero. */
da6d8c04 5426
e2558df3
TBA
5427int
5428linux_process_target::write_memory (CORE_ADDR memaddr,
5429 const unsigned char *myaddr, int len)
da6d8c04 5430{
0d62e5e8
DJ
5431 if (debug_threads)
5432 {
58d6951d 5433 /* Dump up to four bytes. */
bf47e248
PA
5434 char str[4 * 2 + 1];
5435 char *p = str;
5436 int dump = len < 4 ? len : 4;
5437
421490af 5438 for (int i = 0; i < dump; i++)
bf47e248
PA
5439 {
5440 sprintf (p, "%02x", myaddr[i]);
5441 p += 2;
5442 }
5443 *p = '\0';
5444
c058728c 5445 threads_debug_printf ("Writing %s to 0x%08lx in process %d",
421490af 5446 str, (long) memaddr, current_process ()->pid);
0d62e5e8
DJ
5447 }
5448
421490af 5449 return proc_xfer_memory (memaddr, nullptr, myaddr, len);
da6d8c04 5450}
2f2893d9 5451
2a31c7aa
TBA
5452void
5453linux_process_target::look_up_symbols ()
2f2893d9 5454{
0d62e5e8 5455#ifdef USE_THREAD_DB
95954743
PA
5456 struct process_info *proc = current_process ();
5457
fe978cb0 5458 if (proc->priv->thread_db != NULL)
0d62e5e8
DJ
5459 return;
5460
9b4c5f87 5461 thread_db_init ();
0d62e5e8
DJ
5462#endif
5463}
5464
eb497a2a
TBA
5465void
5466linux_process_target::request_interrupt ()
e5379b03 5467{
78708b7c
PA
5468 /* Send a SIGINT to the process group. This acts just like the user
5469 typed a ^C on the controlling terminal. */
eb497a2a 5470 ::kill (-signal_pid, SIGINT);
e5379b03
DJ
5471}
5472
eac215cc
TBA
5473bool
5474linux_process_target::supports_read_auxv ()
5475{
5476 return true;
5477}
5478
aa691b87
RM
5479/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
5480 to debugger memory starting at MYADDR. */
5481
eac215cc
TBA
5482int
5483linux_process_target::read_auxv (CORE_ADDR offset, unsigned char *myaddr,
5484 unsigned int len)
aa691b87
RM
5485{
5486 char filename[PATH_MAX];
5487 int fd, n;
0bfdf32f 5488 int pid = lwpid_of (current_thread);
aa691b87 5489
6cebaf6e 5490 xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
aa691b87
RM
5491
5492 fd = open (filename, O_RDONLY);
5493 if (fd < 0)
5494 return -1;
5495
5496 if (offset != (CORE_ADDR) 0
5497 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
5498 n = -1;
5499 else
5500 n = read (fd, myaddr, len);
5501
5502 close (fd);
5503
5504 return n;
5505}
5506
7e0bde70
TBA
5507int
5508linux_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
5509 int size, raw_breakpoint *bp)
e013ee27 5510{
c8f4bfdd
YQ
5511 if (type == raw_bkpt_type_sw)
5512 return insert_memory_breakpoint (bp);
e013ee27 5513 else
9db9aa23
TBA
5514 return low_insert_point (type, addr, size, bp);
5515}
5516
5517int
5518linux_process_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
5519 int size, raw_breakpoint *bp)
5520{
5521 /* Unsupported (see target.h). */
5522 return 1;
e013ee27
OF
5523}
5524
7e0bde70
TBA
5525int
5526linux_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
5527 int size, raw_breakpoint *bp)
e013ee27 5528{
c8f4bfdd
YQ
5529 if (type == raw_bkpt_type_sw)
5530 return remove_memory_breakpoint (bp);
e013ee27 5531 else
9db9aa23
TBA
5532 return low_remove_point (type, addr, size, bp);
5533}
5534
5535int
5536linux_process_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
5537 int size, raw_breakpoint *bp)
5538{
5539 /* Unsupported (see target.h). */
5540 return 1;
e013ee27
OF
5541}
5542
84320c4e 5543/* Implement the stopped_by_sw_breakpoint target_ops
3e572f71
PA
5544 method. */
5545
84320c4e
TBA
5546bool
5547linux_process_target::stopped_by_sw_breakpoint ()
3e572f71
PA
5548{
5549 struct lwp_info *lwp = get_thread_lwp (current_thread);
5550
5551 return (lwp->stop_reason == TARGET_STOPPED_BY_SW_BREAKPOINT);
5552}
5553
84320c4e 5554/* Implement the supports_stopped_by_sw_breakpoint target_ops
3e572f71
PA
5555 method. */
5556
84320c4e
TBA
5557bool
5558linux_process_target::supports_stopped_by_sw_breakpoint ()
3e572f71
PA
5559{
5560 return USE_SIGTRAP_SIGINFO;
5561}
5562
93fe88b2 5563/* Implement the stopped_by_hw_breakpoint target_ops
3e572f71
PA
5564 method. */
5565
93fe88b2
TBA
5566bool
5567linux_process_target::stopped_by_hw_breakpoint ()
3e572f71
PA
5568{
5569 struct lwp_info *lwp = get_thread_lwp (current_thread);
5570
5571 return (lwp->stop_reason == TARGET_STOPPED_BY_HW_BREAKPOINT);
5572}
5573
93fe88b2 5574/* Implement the supports_stopped_by_hw_breakpoint target_ops
3e572f71
PA
5575 method. */
5576
93fe88b2
TBA
5577bool
5578linux_process_target::supports_stopped_by_hw_breakpoint ()
3e572f71
PA
5579{
5580 return USE_SIGTRAP_SIGINFO;
5581}
5582
70b90b91 5583/* Implement the supports_hardware_single_step target_ops method. */
45614f15 5584
22aa6223
TBA
5585bool
5586linux_process_target::supports_hardware_single_step ()
45614f15 5587{
b31cdfa6 5588 return true;
45614f15
YQ
5589}
5590
6eeb5c55
TBA
5591bool
5592linux_process_target::stopped_by_watchpoint ()
e013ee27 5593{
0bfdf32f 5594 struct lwp_info *lwp = get_thread_lwp (current_thread);
c3adc08c 5595
15c66dd6 5596 return lwp->stop_reason == TARGET_STOPPED_BY_WATCHPOINT;
e013ee27
OF
5597}
5598
6eeb5c55
TBA
5599CORE_ADDR
5600linux_process_target::stopped_data_address ()
e013ee27 5601{
0bfdf32f 5602 struct lwp_info *lwp = get_thread_lwp (current_thread);
c3adc08c
PA
5603
5604 return lwp->stopped_data_address;
e013ee27
OF
5605}
5606
db0dfaa0
LM
5607/* This is only used for targets that define PT_TEXT_ADDR,
5608 PT_DATA_ADDR and PT_TEXT_END_ADDR. If those are not defined, supposedly
5609 the target has different ways of acquiring this information, like
5610 loadmaps. */
52fb6437 5611
5203ae1e
TBA
5612bool
5613linux_process_target::supports_read_offsets ()
5614{
5615#ifdef SUPPORTS_READ_OFFSETS
5616 return true;
5617#else
5618 return false;
5619#endif
5620}
5621
52fb6437
NS
5622/* Under uClinux, programs are loaded at non-zero offsets, which we need
5623 to tell gdb about. */
5624
5203ae1e
TBA
5625int
5626linux_process_target::read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
52fb6437 5627{
5203ae1e 5628#ifdef SUPPORTS_READ_OFFSETS
52fb6437 5629 unsigned long text, text_end, data;
62828379 5630 int pid = lwpid_of (current_thread);
52fb6437
NS
5631
5632 errno = 0;
5633
b8e1b30e
LM
5634 text = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_ADDR,
5635 (PTRACE_TYPE_ARG4) 0);
5636 text_end = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_TEXT_END_ADDR,
5637 (PTRACE_TYPE_ARG4) 0);
5638 data = ptrace (PTRACE_PEEKUSER, pid, (PTRACE_TYPE_ARG3) PT_DATA_ADDR,
5639 (PTRACE_TYPE_ARG4) 0);
52fb6437
NS
5640
5641 if (errno == 0)
5642 {
5643 /* Both text and data offsets produced at compile-time (and so
1b3f6016
PA
5644 used by gdb) are relative to the beginning of the program,
5645 with the data segment immediately following the text segment.
5646 However, the actual runtime layout in memory may put the data
5647 somewhere else, so when we send gdb a data base-address, we
5648 use the real data base address and subtract the compile-time
5649 data base-address from it (which is just the length of the
5650 text segment). BSS immediately follows data in both
5651 cases. */
52fb6437
NS
5652 *text_p = text;
5653 *data_p = data - (text_end - text);
1b3f6016 5654
52fb6437
NS
5655 return 1;
5656 }
5203ae1e
TBA
5657 return 0;
5658#else
5659 gdb_assert_not_reached ("target op read_offsets not supported");
52fb6437 5660#endif
5203ae1e 5661}
52fb6437 5662
6e3fd7e9
TBA
5663bool
5664linux_process_target::supports_get_tls_address ()
5665{
5666#ifdef USE_THREAD_DB
5667 return true;
5668#else
5669 return false;
5670#endif
5671}
5672
5673int
5674linux_process_target::get_tls_address (thread_info *thread,
5675 CORE_ADDR offset,
5676 CORE_ADDR load_module,
5677 CORE_ADDR *address)
5678{
5679#ifdef USE_THREAD_DB
5680 return thread_db_get_tls_address (thread, offset, load_module, address);
5681#else
5682 return -1;
5683#endif
5684}
5685
2d0795ee
TBA
5686bool
5687linux_process_target::supports_qxfer_osdata ()
5688{
5689 return true;
5690}
5691
5692int
5693linux_process_target::qxfer_osdata (const char *annex,
5694 unsigned char *readbuf,
5695 unsigned const char *writebuf,
5696 CORE_ADDR offset, int len)
07e059b5 5697{
d26e3629 5698 return linux_common_xfer_osdata (annex, readbuf, offset, len);
07e059b5
VP
5699}
5700
cb63de7c
TBA
5701void
5702linux_process_target::siginfo_fixup (siginfo_t *siginfo,
5703 gdb_byte *inf_siginfo, int direction)
d0722149 5704{
cb63de7c 5705 bool done = low_siginfo_fixup (siginfo, inf_siginfo, direction);
d0722149
DE
5706
5707 /* If there was no callback, or the callback didn't do anything,
5708 then just do a straight memcpy. */
5709 if (!done)
5710 {
5711 if (direction == 1)
a5362b9a 5712 memcpy (siginfo, inf_siginfo, sizeof (siginfo_t));
d0722149 5713 else
a5362b9a 5714 memcpy (inf_siginfo, siginfo, sizeof (siginfo_t));
d0722149
DE
5715 }
5716}
5717
cb63de7c
TBA
5718bool
5719linux_process_target::low_siginfo_fixup (siginfo_t *native, gdb_byte *inf,
5720 int direction)
5721{
5722 return false;
5723}
5724
d7abedf7
TBA
5725bool
5726linux_process_target::supports_qxfer_siginfo ()
5727{
5728 return true;
5729}
5730
5731int
5732linux_process_target::qxfer_siginfo (const char *annex,
5733 unsigned char *readbuf,
5734 unsigned const char *writebuf,
5735 CORE_ADDR offset, int len)
4aa995e1 5736{
d0722149 5737 int pid;
a5362b9a 5738 siginfo_t siginfo;
8adce034 5739 gdb_byte inf_siginfo[sizeof (siginfo_t)];
4aa995e1 5740
0bfdf32f 5741 if (current_thread == NULL)
4aa995e1
PA
5742 return -1;
5743
0bfdf32f 5744 pid = lwpid_of (current_thread);
4aa995e1 5745
c058728c
SM
5746 threads_debug_printf ("%s siginfo for lwp %d.",
5747 readbuf != NULL ? "Reading" : "Writing",
5748 pid);
4aa995e1 5749
0adea5f7 5750 if (offset >= sizeof (siginfo))
4aa995e1
PA
5751 return -1;
5752
b8e1b30e 5753 if (ptrace (PTRACE_GETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
4aa995e1
PA
5754 return -1;
5755
d0722149
DE
5756 /* When GDBSERVER is built as a 64-bit application, ptrace writes into
5757 SIGINFO an object with 64-bit layout. Since debugging a 32-bit
5758 inferior with a 64-bit GDBSERVER should look the same as debugging it
5759 with a 32-bit GDBSERVER, we need to convert it. */
5760 siginfo_fixup (&siginfo, inf_siginfo, 0);
5761
4aa995e1
PA
5762 if (offset + len > sizeof (siginfo))
5763 len = sizeof (siginfo) - offset;
5764
5765 if (readbuf != NULL)
d0722149 5766 memcpy (readbuf, inf_siginfo + offset, len);
4aa995e1
PA
5767 else
5768 {
d0722149
DE
5769 memcpy (inf_siginfo + offset, writebuf, len);
5770
5771 /* Convert back to ptrace layout before flushing it out. */
5772 siginfo_fixup (&siginfo, inf_siginfo, 1);
5773
b8e1b30e 5774 if (ptrace (PTRACE_SETSIGINFO, pid, (PTRACE_TYPE_ARG3) 0, &siginfo) != 0)
4aa995e1
PA
5775 return -1;
5776 }
5777
5778 return len;
5779}
5780
bd99dc85
PA
5781/* SIGCHLD handler that serves two purposes: In non-stop/async mode,
5782 so we notice when children change state; as the handler for the
5783 sigsuspend in my_waitpid. */
5784
5785static void
5786sigchld_handler (int signo)
5787{
5788 int old_errno = errno;
5789
5790 if (debug_threads)
e581f2b4
PA
5791 {
5792 do
5793 {
a7e559cc
AH
5794 /* Use the async signal safe debug function. */
5795 if (debug_write ("sigchld_handler\n",
5796 sizeof ("sigchld_handler\n") - 1) < 0)
e581f2b4
PA
5797 break; /* just ignore */
5798 } while (0);
5799 }
bd99dc85
PA
5800
5801 if (target_is_async_p ())
5802 async_file_mark (); /* trigger a linux_wait */
5803
5804 errno = old_errno;
5805}
5806
0dc587d4
TBA
5807bool
5808linux_process_target::supports_non_stop ()
bd99dc85 5809{
0dc587d4 5810 return true;
bd99dc85
PA
5811}
5812
0dc587d4
TBA
5813bool
5814linux_process_target::async (bool enable)
bd99dc85 5815{
0dc587d4 5816 bool previous = target_is_async_p ();
bd99dc85 5817
c058728c
SM
5818 threads_debug_printf ("async (%d), previous=%d",
5819 enable, previous);
8336d594 5820
bd99dc85
PA
5821 if (previous != enable)
5822 {
5823 sigset_t mask;
5824 sigemptyset (&mask);
5825 sigaddset (&mask, SIGCHLD);
5826
21987b9c 5827 gdb_sigmask (SIG_BLOCK, &mask, NULL);
bd99dc85
PA
5828
5829 if (enable)
5830 {
8674f082 5831 if (!linux_event_pipe.open_pipe ())
aa96c426 5832 {
21987b9c 5833 gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
aa96c426
GB
5834
5835 warning ("creating event pipe failed.");
5836 return previous;
5837 }
bd99dc85 5838
bd99dc85 5839 /* Register the event loop handler. */
cdc8e9b2 5840 add_file_handler (linux_event_pipe.event_fd (),
2554f6f5
SM
5841 handle_target_event, NULL,
5842 "linux-low");
bd99dc85
PA
5843
5844 /* Always trigger a linux_wait. */
5845 async_file_mark ();
5846 }
5847 else
5848 {
cdc8e9b2 5849 delete_file_handler (linux_event_pipe.event_fd ());
bd99dc85 5850
8674f082 5851 linux_event_pipe.close_pipe ();
bd99dc85
PA
5852 }
5853
21987b9c 5854 gdb_sigmask (SIG_UNBLOCK, &mask, NULL);
bd99dc85
PA
5855 }
5856
5857 return previous;
5858}
5859
0dc587d4
TBA
5860int
5861linux_process_target::start_non_stop (bool nonstop)
bd99dc85
PA
5862{
5863 /* Register or unregister from event-loop accordingly. */
0dc587d4 5864 target_async (nonstop);
aa96c426 5865
0dc587d4 5866 if (target_is_async_p () != (nonstop != false))
aa96c426
GB
5867 return -1;
5868
bd99dc85
PA
5869 return 0;
5870}
5871
652aef77
TBA
5872bool
5873linux_process_target::supports_multi_process ()
cf8fd78b 5874{
652aef77 5875 return true;
cf8fd78b
PA
5876}
5877
89245bc0
DB
5878/* Check if fork events are supported. */
5879
9690a72a
TBA
5880bool
5881linux_process_target::supports_fork_events ()
89245bc0 5882{
a2885186 5883 return true;
89245bc0
DB
5884}
5885
5886/* Check if vfork events are supported. */
5887
9690a72a
TBA
5888bool
5889linux_process_target::supports_vfork_events ()
89245bc0 5890{
a2885186 5891 return true;
89245bc0
DB
5892}
5893
94585166
DB
5894/* Check if exec events are supported. */
5895
9690a72a
TBA
5896bool
5897linux_process_target::supports_exec_events ()
94585166 5898{
a2885186 5899 return true;
94585166
DB
5900}
5901
de0d863e
DB
5902/* Target hook for 'handle_new_gdb_connection'. Causes a reset of the
5903 ptrace flags for all inferiors. This is in case the new GDB connection
5904 doesn't support the same set of events that the previous one did. */
5905
fb00dfce
TBA
5906void
5907linux_process_target::handle_new_gdb_connection ()
de0d863e 5908{
de0d863e 5909 /* Request that all the lwps reset their ptrace options. */
bbf550d5
SM
5910 for_each_thread ([] (thread_info *thread)
5911 {
5912 struct lwp_info *lwp = get_thread_lwp (thread);
5913
5914 if (!lwp->stopped)
5915 {
5916 /* Stop the lwp so we can modify its ptrace options. */
5917 lwp->must_set_ptrace_flags = 1;
5918 linux_stop_lwp (lwp);
5919 }
5920 else
5921 {
5922 /* Already stopped; go ahead and set the ptrace options. */
5923 struct process_info *proc = find_process_pid (pid_of (thread));
5924 int options = linux_low_ptrace_options (proc->attached);
5925
5926 linux_enable_event_reporting (lwpid_of (thread), options);
5927 lwp->must_set_ptrace_flags = 0;
5928 }
5929 });
de0d863e
DB
5930}
5931
55cf3021
TBA
5932int
5933linux_process_target::handle_monitor_command (char *mon)
5934{
5935#ifdef USE_THREAD_DB
5936 return thread_db_handle_monitor_command (mon);
5937#else
5938 return 0;
5939#endif
5940}
5941
95a45fc1
TBA
5942int
5943linux_process_target::core_of_thread (ptid_t ptid)
5944{
5945 return linux_common_core_of_thread (ptid);
5946}
5947
c756403b
TBA
5948bool
5949linux_process_target::supports_disable_randomization ()
03583c20 5950{
c756403b 5951 return true;
03583c20 5952}
efcbbd14 5953
c0245cb9
TBA
5954bool
5955linux_process_target::supports_agent ()
d1feda86 5956{
c0245cb9 5957 return true;
d1feda86
YQ
5958}
5959
2526e0cd
TBA
5960bool
5961linux_process_target::supports_range_stepping ()
c2d6af84 5962{
7582c77c 5963 if (supports_software_single_step ())
2526e0cd 5964 return true;
c2d6af84 5965
9cfd8715
TBA
5966 return low_supports_range_stepping ();
5967}
5968
5969bool
5970linux_process_target::low_supports_range_stepping ()
5971{
5972 return false;
c2d6af84
PA
5973}
5974
8247b823
TBA
5975bool
5976linux_process_target::supports_pid_to_exec_file ()
5977{
5978 return true;
5979}
5980
04977957 5981const char *
8247b823
TBA
5982linux_process_target::pid_to_exec_file (int pid)
5983{
5984 return linux_proc_pid_to_exec_file (pid);
5985}
5986
c9b7b804
TBA
5987bool
5988linux_process_target::supports_multifs ()
5989{
5990 return true;
5991}
5992
5993int
5994linux_process_target::multifs_open (int pid, const char *filename,
5995 int flags, mode_t mode)
5996{
5997 return linux_mntns_open_cloexec (pid, filename, flags, mode);
5998}
5999
6000int
6001linux_process_target::multifs_unlink (int pid, const char *filename)
6002{
6003 return linux_mntns_unlink (pid, filename);
6004}
6005
6006ssize_t
6007linux_process_target::multifs_readlink (int pid, const char *filename,
6008 char *buf, size_t bufsiz)
6009{
6010 return linux_mntns_readlink (pid, filename, buf, bufsiz);
6011}
6012
723b724b 6013#if defined PT_GETDSBT || defined PTRACE_GETFDPIC
78d85199
YQ
6014struct target_loadseg
6015{
6016 /* Core address to which the segment is mapped. */
6017 Elf32_Addr addr;
6018 /* VMA recorded in the program header. */
6019 Elf32_Addr p_vaddr;
6020 /* Size of this segment in memory. */
6021 Elf32_Word p_memsz;
6022};
6023
723b724b 6024# if defined PT_GETDSBT
78d85199
YQ
6025struct target_loadmap
6026{
6027 /* Protocol version number, must be zero. */
6028 Elf32_Word version;
6029 /* Pointer to the DSBT table, its size, and the DSBT index. */
6030 unsigned *dsbt_table;
6031 unsigned dsbt_size, dsbt_index;
6032 /* Number of segments in this map. */
6033 Elf32_Word nsegs;
6034 /* The actual memory map. */
6035 struct target_loadseg segs[/*nsegs*/];
6036};
723b724b
MF
6037# define LINUX_LOADMAP PT_GETDSBT
6038# define LINUX_LOADMAP_EXEC PTRACE_GETDSBT_EXEC
6039# define LINUX_LOADMAP_INTERP PTRACE_GETDSBT_INTERP
6040# else
6041struct target_loadmap
6042{
6043 /* Protocol version number, must be zero. */
6044 Elf32_Half version;
6045 /* Number of segments in this map. */
6046 Elf32_Half nsegs;
6047 /* The actual memory map. */
6048 struct target_loadseg segs[/*nsegs*/];
6049};
6050# define LINUX_LOADMAP PTRACE_GETFDPIC
6051# define LINUX_LOADMAP_EXEC PTRACE_GETFDPIC_EXEC
6052# define LINUX_LOADMAP_INTERP PTRACE_GETFDPIC_INTERP
6053# endif
78d85199 6054
9da41fda
TBA
6055bool
6056linux_process_target::supports_read_loadmap ()
6057{
6058 return true;
6059}
6060
6061int
6062linux_process_target::read_loadmap (const char *annex, CORE_ADDR offset,
6063 unsigned char *myaddr, unsigned int len)
78d85199 6064{
0bfdf32f 6065 int pid = lwpid_of (current_thread);
78d85199
YQ
6066 int addr = -1;
6067 struct target_loadmap *data = NULL;
6068 unsigned int actual_length, copy_length;
6069
6070 if (strcmp (annex, "exec") == 0)
723b724b 6071 addr = (int) LINUX_LOADMAP_EXEC;
78d85199 6072 else if (strcmp (annex, "interp") == 0)
723b724b 6073 addr = (int) LINUX_LOADMAP_INTERP;
78d85199
YQ
6074 else
6075 return -1;
6076
723b724b 6077 if (ptrace (LINUX_LOADMAP, pid, addr, &data) != 0)
78d85199
YQ
6078 return -1;
6079
6080 if (data == NULL)
6081 return -1;
6082
6083 actual_length = sizeof (struct target_loadmap)
6084 + sizeof (struct target_loadseg) * data->nsegs;
6085
6086 if (offset < 0 || offset > actual_length)
6087 return -1;
6088
6089 copy_length = actual_length - offset < len ? actual_length - offset : len;
6090 memcpy (myaddr, (char *) data + offset, copy_length);
6091 return copy_length;
6092}
723b724b 6093#endif /* defined PT_GETDSBT || defined PTRACE_GETFDPIC */
78d85199 6094
bc8d3ae4
TBA
6095bool
6096linux_process_target::supports_catch_syscall ()
82075af2 6097{
a2885186 6098 return low_supports_catch_syscall ();
82075af2
JS
6099}
6100
9eedd27d
TBA
6101bool
6102linux_process_target::low_supports_catch_syscall ()
6103{
6104 return false;
6105}
6106
770d8f6a
TBA
6107CORE_ADDR
6108linux_process_target::read_pc (regcache *regcache)
219f2f23 6109{
bf9ae9d8 6110 if (!low_supports_breakpoints ())
219f2f23
PA
6111 return 0;
6112
bf9ae9d8 6113 return low_get_pc (regcache);
219f2f23
PA
6114}
6115
770d8f6a
TBA
6116void
6117linux_process_target::write_pc (regcache *regcache, CORE_ADDR pc)
219f2f23 6118{
bf9ae9d8 6119 gdb_assert (low_supports_breakpoints ());
219f2f23 6120
bf9ae9d8 6121 low_set_pc (regcache, pc);
219f2f23
PA
6122}
6123
68119632
TBA
6124bool
6125linux_process_target::supports_thread_stopped ()
6126{
6127 return true;
6128}
6129
6130bool
6131linux_process_target::thread_stopped (thread_info *thread)
8336d594
PA
6132{
6133 return get_thread_lwp (thread)->stopped;
6134}
6135
6136/* This exposes stop-all-threads functionality to other modules. */
6137
29e8dc09
TBA
6138void
6139linux_process_target::pause_all (bool freeze)
8336d594 6140{
7984d532
PA
6141 stop_all_lwps (freeze, NULL);
6142}
6143
6144/* This exposes unstop-all-threads functionality to other gdbserver
6145 modules. */
6146
29e8dc09
TBA
6147void
6148linux_process_target::unpause_all (bool unfreeze)
7984d532
PA
6149{
6150 unstop_all_lwps (unfreeze, NULL);
8336d594
PA
6151}
6152
2268b414
JK
6153/* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
6154
6155static int
6156get_phdr_phnum_from_proc_auxv (const int pid, const int is_elf64,
6157 CORE_ADDR *phdr_memaddr, int *num_phdr)
6158{
6159 char filename[PATH_MAX];
6160 int fd;
6161 const int auxv_size = is_elf64
6162 ? sizeof (Elf64_auxv_t) : sizeof (Elf32_auxv_t);
6163 char buf[sizeof (Elf64_auxv_t)]; /* The larger of the two. */
6164
6165 xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
6166
6167 fd = open (filename, O_RDONLY);
6168 if (fd < 0)
6169 return 1;
6170
6171 *phdr_memaddr = 0;
6172 *num_phdr = 0;
6173 while (read (fd, buf, auxv_size) == auxv_size
6174 && (*phdr_memaddr == 0 || *num_phdr == 0))
6175 {
6176 if (is_elf64)
6177 {
6178 Elf64_auxv_t *const aux = (Elf64_auxv_t *) buf;
6179
6180 switch (aux->a_type)
6181 {
6182 case AT_PHDR:
6183 *phdr_memaddr = aux->a_un.a_val;
6184 break;
6185 case AT_PHNUM:
6186 *num_phdr = aux->a_un.a_val;
6187 break;
6188 }
6189 }
6190 else
6191 {
6192 Elf32_auxv_t *const aux = (Elf32_auxv_t *) buf;
6193
6194 switch (aux->a_type)
6195 {
6196 case AT_PHDR:
6197 *phdr_memaddr = aux->a_un.a_val;
6198 break;
6199 case AT_PHNUM:
6200 *num_phdr = aux->a_un.a_val;
6201 break;
6202 }
6203 }
6204 }
6205
6206 close (fd);
6207
6208 if (*phdr_memaddr == 0 || *num_phdr == 0)
6209 {
6210 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
6211 "phdr_memaddr = %ld, phdr_num = %d",
6212 (long) *phdr_memaddr, *num_phdr);
6213 return 2;
6214 }
6215
6216 return 0;
6217}
6218
6219/* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
6220
6221static CORE_ADDR
6222get_dynamic (const int pid, const int is_elf64)
6223{
6224 CORE_ADDR phdr_memaddr, relocation;
db1ff28b 6225 int num_phdr, i;
2268b414 6226 unsigned char *phdr_buf;
db1ff28b 6227 const int phdr_size = is_elf64 ? sizeof (Elf64_Phdr) : sizeof (Elf32_Phdr);
2268b414
JK
6228
6229 if (get_phdr_phnum_from_proc_auxv (pid, is_elf64, &phdr_memaddr, &num_phdr))
6230 return 0;
6231
6232 gdb_assert (num_phdr < 100); /* Basic sanity check. */
224c3ddb 6233 phdr_buf = (unsigned char *) alloca (num_phdr * phdr_size);
2268b414
JK
6234
6235 if (linux_read_memory (phdr_memaddr, phdr_buf, num_phdr * phdr_size))
6236 return 0;
6237
6238 /* Compute relocation: it is expected to be 0 for "regular" executables,
6239 non-zero for PIE ones. */
6240 relocation = -1;
db1ff28b
JK
6241 for (i = 0; relocation == -1 && i < num_phdr; i++)
6242 if (is_elf64)
6243 {
6244 Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
6245
6246 if (p->p_type == PT_PHDR)
6247 relocation = phdr_memaddr - p->p_vaddr;
6248 }
6249 else
6250 {
6251 Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
6252
6253 if (p->p_type == PT_PHDR)
6254 relocation = phdr_memaddr - p->p_vaddr;
6255 }
6256
2268b414
JK
6257 if (relocation == -1)
6258 {
e237a7e2
JK
6259 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
6260 any real world executables, including PIE executables, have always
6261 PT_PHDR present. PT_PHDR is not present in some shared libraries or
6262 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
6263 or present DT_DEBUG anyway (fpc binaries are statically linked).
6264
6265 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
6266
6267 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
6268
2268b414
JK
6269 return 0;
6270 }
6271
db1ff28b
JK
6272 for (i = 0; i < num_phdr; i++)
6273 {
6274 if (is_elf64)
6275 {
6276 Elf64_Phdr *const p = (Elf64_Phdr *) (phdr_buf + i * phdr_size);
6277
6278 if (p->p_type == PT_DYNAMIC)
6279 return p->p_vaddr + relocation;
6280 }
6281 else
6282 {
6283 Elf32_Phdr *const p = (Elf32_Phdr *) (phdr_buf + i * phdr_size);
2268b414 6284
db1ff28b
JK
6285 if (p->p_type == PT_DYNAMIC)
6286 return p->p_vaddr + relocation;
6287 }
6288 }
2268b414
JK
6289
6290 return 0;
6291}
6292
6293/* Return &_r_debug in the inferior, or -1 if not present. Return value
367ba2c2
MR
6294 can be 0 if the inferior does not yet have the library list initialized.
6295 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
6296 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
2268b414
JK
6297
6298static CORE_ADDR
6299get_r_debug (const int pid, const int is_elf64)
6300{
6301 CORE_ADDR dynamic_memaddr;
6302 const int dyn_size = is_elf64 ? sizeof (Elf64_Dyn) : sizeof (Elf32_Dyn);
6303 unsigned char buf[sizeof (Elf64_Dyn)]; /* The larger of the two. */
367ba2c2 6304 CORE_ADDR map = -1;
2268b414
JK
6305
6306 dynamic_memaddr = get_dynamic (pid, is_elf64);
6307 if (dynamic_memaddr == 0)
367ba2c2 6308 return map;
2268b414
JK
6309
6310 while (linux_read_memory (dynamic_memaddr, buf, dyn_size) == 0)
6311 {
6312 if (is_elf64)
6313 {
6314 Elf64_Dyn *const dyn = (Elf64_Dyn *) buf;
a738da3a 6315#if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
367ba2c2
MR
6316 union
6317 {
6318 Elf64_Xword map;
6319 unsigned char buf[sizeof (Elf64_Xword)];
6320 }
6321 rld_map;
a738da3a
MF
6322#endif
6323#ifdef DT_MIPS_RLD_MAP
367ba2c2
MR
6324 if (dyn->d_tag == DT_MIPS_RLD_MAP)
6325 {
6326 if (linux_read_memory (dyn->d_un.d_val,
6327 rld_map.buf, sizeof (rld_map.buf)) == 0)
6328 return rld_map.map;
6329 else
6330 break;
6331 }
75f62ce7 6332#endif /* DT_MIPS_RLD_MAP */
a738da3a
MF
6333#ifdef DT_MIPS_RLD_MAP_REL
6334 if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
6335 {
6336 if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
6337 rld_map.buf, sizeof (rld_map.buf)) == 0)
6338 return rld_map.map;
6339 else
6340 break;
6341 }
6342#endif /* DT_MIPS_RLD_MAP_REL */
2268b414 6343
367ba2c2
MR
6344 if (dyn->d_tag == DT_DEBUG && map == -1)
6345 map = dyn->d_un.d_val;
2268b414
JK
6346
6347 if (dyn->d_tag == DT_NULL)
6348 break;
6349 }
6350 else
6351 {
6352 Elf32_Dyn *const dyn = (Elf32_Dyn *) buf;
a738da3a 6353#if defined DT_MIPS_RLD_MAP || defined DT_MIPS_RLD_MAP_REL
367ba2c2
MR
6354 union
6355 {
6356 Elf32_Word map;
6357 unsigned char buf[sizeof (Elf32_Word)];
6358 }
6359 rld_map;
a738da3a
MF
6360#endif
6361#ifdef DT_MIPS_RLD_MAP
367ba2c2
MR
6362 if (dyn->d_tag == DT_MIPS_RLD_MAP)
6363 {
6364 if (linux_read_memory (dyn->d_un.d_val,
6365 rld_map.buf, sizeof (rld_map.buf)) == 0)
6366 return rld_map.map;
6367 else
6368 break;
6369 }
75f62ce7 6370#endif /* DT_MIPS_RLD_MAP */
a738da3a
MF
6371#ifdef DT_MIPS_RLD_MAP_REL
6372 if (dyn->d_tag == DT_MIPS_RLD_MAP_REL)
6373 {
6374 if (linux_read_memory (dyn->d_un.d_val + dynamic_memaddr,
6375 rld_map.buf, sizeof (rld_map.buf)) == 0)
6376 return rld_map.map;
6377 else
6378 break;
6379 }
6380#endif /* DT_MIPS_RLD_MAP_REL */
2268b414 6381
367ba2c2
MR
6382 if (dyn->d_tag == DT_DEBUG && map == -1)
6383 map = dyn->d_un.d_val;
2268b414
JK
6384
6385 if (dyn->d_tag == DT_NULL)
6386 break;
6387 }
6388
6389 dynamic_memaddr += dyn_size;
6390 }
6391
367ba2c2 6392 return map;
2268b414
JK
6393}
6394
6395/* Read one pointer from MEMADDR in the inferior. */
6396
6397static int
6398read_one_ptr (CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size)
6399{
485f1ee4
PA
6400 int ret;
6401
6402 /* Go through a union so this works on either big or little endian
6403 hosts, when the inferior's pointer size is smaller than the size
6404 of CORE_ADDR. It is assumed the inferior's endianness is the
6405 same of the superior's. */
6406 union
6407 {
6408 CORE_ADDR core_addr;
6409 unsigned int ui;
6410 unsigned char uc;
6411 } addr;
6412
6413 ret = linux_read_memory (memaddr, &addr.uc, ptr_size);
6414 if (ret == 0)
6415 {
6416 if (ptr_size == sizeof (CORE_ADDR))
6417 *ptr = addr.core_addr;
6418 else if (ptr_size == sizeof (unsigned int))
6419 *ptr = addr.ui;
6420 else
6421 gdb_assert_not_reached ("unhandled pointer size");
6422 }
6423 return ret;
2268b414
JK
6424}
6425
974387bb
TBA
6426bool
6427linux_process_target::supports_qxfer_libraries_svr4 ()
6428{
6429 return true;
6430}
6431
2268b414
JK
6432struct link_map_offsets
6433 {
6434 /* Offset and size of r_debug.r_version. */
6435 int r_version_offset;
6436
6437 /* Offset and size of r_debug.r_map. */
6438 int r_map_offset;
6439
8d56636a
MM
6440 /* Offset of r_debug_extended.r_next. */
6441 int r_next_offset;
6442
2268b414
JK
6443 /* Offset to l_addr field in struct link_map. */
6444 int l_addr_offset;
6445
6446 /* Offset to l_name field in struct link_map. */
6447 int l_name_offset;
6448
6449 /* Offset to l_ld field in struct link_map. */
6450 int l_ld_offset;
6451
6452 /* Offset to l_next field in struct link_map. */
6453 int l_next_offset;
6454
6455 /* Offset to l_prev field in struct link_map. */
6456 int l_prev_offset;
6457 };
6458
8d56636a
MM
6459static const link_map_offsets lmo_32bit_offsets =
6460 {
6461 0, /* r_version offset. */
6462 4, /* r_debug.r_map offset. */
6463 20, /* r_debug_extended.r_next. */
6464 0, /* l_addr offset in link_map. */
6465 4, /* l_name offset in link_map. */
6466 8, /* l_ld offset in link_map. */
6467 12, /* l_next offset in link_map. */
6468 16 /* l_prev offset in link_map. */
6469 };
6470
6471static const link_map_offsets lmo_64bit_offsets =
6472 {
6473 0, /* r_version offset. */
6474 8, /* r_debug.r_map offset. */
6475 40, /* r_debug_extended.r_next. */
6476 0, /* l_addr offset in link_map. */
6477 8, /* l_name offset in link_map. */
6478 16, /* l_ld offset in link_map. */
6479 24, /* l_next offset in link_map. */
6480 32 /* l_prev offset in link_map. */
6481 };
6482
6483/* Get the loaded shared libraries from one namespace. */
6484
6485static void
2733d9d5
MM
6486read_link_map (std::string &document, CORE_ADDR lmid, CORE_ADDR lm_addr,
6487 CORE_ADDR lm_prev, int ptr_size, const link_map_offsets *lmo)
8d56636a
MM
6488{
6489 CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
6490
6491 while (lm_addr
6492 && read_one_ptr (lm_addr + lmo->l_name_offset,
6493 &l_name, ptr_size) == 0
6494 && read_one_ptr (lm_addr + lmo->l_addr_offset,
6495 &l_addr, ptr_size) == 0
6496 && read_one_ptr (lm_addr + lmo->l_ld_offset,
6497 &l_ld, ptr_size) == 0
6498 && read_one_ptr (lm_addr + lmo->l_prev_offset,
6499 &l_prev, ptr_size) == 0
6500 && read_one_ptr (lm_addr + lmo->l_next_offset,
6501 &l_next, ptr_size) == 0)
6502 {
6503 unsigned char libname[PATH_MAX];
6504
6505 if (lm_prev != l_prev)
6506 {
6507 warning ("Corrupted shared library list: 0x%s != 0x%s",
6508 paddress (lm_prev), paddress (l_prev));
6509 break;
6510 }
6511
ad10f44e
MM
6512 /* Not checking for error because reading may stop before we've got
6513 PATH_MAX worth of characters. */
6514 libname[0] = '\0';
6515 linux_read_memory (l_name, libname, sizeof (libname) - 1);
6516 libname[sizeof (libname) - 1] = '\0';
6517 if (libname[0] != '\0')
8d56636a 6518 {
ad10f44e
MM
6519 string_appendf (document, "<library name=\"");
6520 xml_escape_text_append (&document, (char *) libname);
6521 string_appendf (document, "\" lm=\"0x%s\" l_addr=\"0x%s\" "
2733d9d5 6522 "l_ld=\"0x%s\" lmid=\"0x%s\"/>",
ad10f44e 6523 paddress (lm_addr), paddress (l_addr),
2733d9d5 6524 paddress (l_ld), paddress (lmid));
8d56636a
MM
6525 }
6526
6527 lm_prev = lm_addr;
6528 lm_addr = l_next;
6529 }
6530}
6531
fb723180 6532/* Construct qXfer:libraries-svr4:read reply. */
2268b414 6533
974387bb
TBA
6534int
6535linux_process_target::qxfer_libraries_svr4 (const char *annex,
6536 unsigned char *readbuf,
6537 unsigned const char *writebuf,
6538 CORE_ADDR offset, int len)
2268b414 6539{
fe978cb0 6540 struct process_info_private *const priv = current_process ()->priv;
2268b414
JK
6541 char filename[PATH_MAX];
6542 int pid, is_elf64;
214d508e 6543 unsigned int machine;
2733d9d5 6544 CORE_ADDR lmid = 0, lm_addr = 0, lm_prev = 0;
2268b414
JK
6545
6546 if (writebuf != NULL)
6547 return -2;
6548 if (readbuf == NULL)
6549 return -1;
6550
0bfdf32f 6551 pid = lwpid_of (current_thread);
2268b414 6552 xsnprintf (filename, sizeof filename, "/proc/%d/exe", pid);
214d508e 6553 is_elf64 = elf_64_file_p (filename, &machine);
8d56636a
MM
6554 const link_map_offsets *lmo;
6555 int ptr_size;
6556 if (is_elf64)
6557 {
6558 lmo = &lmo_64bit_offsets;
6559 ptr_size = 8;
6560 }
6561 else
6562 {
6563 lmo = &lmo_32bit_offsets;
6564 ptr_size = 4;
6565 }
2268b414 6566
b1fbec62
GB
6567 while (annex[0] != '\0')
6568 {
6569 const char *sep;
6570 CORE_ADDR *addrp;
da4ae14a 6571 int name_len;
2268b414 6572
b1fbec62
GB
6573 sep = strchr (annex, '=');
6574 if (sep == NULL)
6575 break;
0c5bf5a9 6576
da4ae14a 6577 name_len = sep - annex;
2733d9d5
MM
6578 if (name_len == 4 && startswith (annex, "lmid"))
6579 addrp = &lmid;
6580 else if (name_len == 5 && startswith (annex, "start"))
b1fbec62 6581 addrp = &lm_addr;
da4ae14a 6582 else if (name_len == 4 && startswith (annex, "prev"))
b1fbec62
GB
6583 addrp = &lm_prev;
6584 else
6585 {
6586 annex = strchr (sep, ';');
6587 if (annex == NULL)
6588 break;
6589 annex++;
6590 continue;
6591 }
6592
6593 annex = decode_address_to_semicolon (addrp, sep + 1);
2268b414 6594 }
b1fbec62 6595
8d56636a
MM
6596 std::string document = "<library-list-svr4 version=\"1.0\"";
6597
6598 /* When the starting LM_ADDR is passed in the annex, only traverse that
2733d9d5 6599 namespace, which is assumed to be identified by LMID.
8d56636a
MM
6600
6601 Otherwise, start with R_DEBUG and traverse all namespaces we find. */
6602 if (lm_addr != 0)
ad10f44e
MM
6603 {
6604 document += ">";
2733d9d5 6605 read_link_map (document, lmid, lm_addr, lm_prev, ptr_size, lmo);
ad10f44e 6606 }
8d56636a 6607 else
2268b414 6608 {
8d56636a
MM
6609 if (lm_prev != 0)
6610 warning ("ignoring prev=0x%s without start", paddress (lm_prev));
b1fbec62 6611
2733d9d5
MM
6612 /* We could interpret LMID as 'provide only the libraries for this
6613 namespace' but GDB is currently only providing lmid, start, and
6614 prev, or nothing. */
6615 if (lmid != 0)
6616 warning ("ignoring lmid=0x%s without start", paddress (lmid));
6617
8d56636a
MM
6618 CORE_ADDR r_debug = priv->r_debug;
6619 if (r_debug == 0)
6620 r_debug = priv->r_debug = get_r_debug (pid, is_elf64);
b1fbec62
GB
6621
6622 /* We failed to find DT_DEBUG. Such situation will not change
6623 for this inferior - do not retry it. Report it to GDB as
6624 E01, see for the reasons at the GDB solib-svr4.c side. */
8d56636a 6625 if (r_debug == (CORE_ADDR) -1)
b1fbec62
GB
6626 return -1;
6627
ad10f44e
MM
6628 /* Terminate the header if we end up with an empty list. */
6629 if (r_debug == 0)
6630 document += ">";
6631
8d56636a 6632 while (r_debug != 0)
2268b414 6633 {
8d56636a
MM
6634 int r_version = 0;
6635 if (linux_read_memory (r_debug + lmo->r_version_offset,
b1fbec62 6636 (unsigned char *) &r_version,
8d56636a
MM
6637 sizeof (r_version)) != 0)
6638 {
6639 warning ("unable to read r_version from 0x%s",
6640 paddress (r_debug + lmo->r_version_offset));
6641 break;
6642 }
6643
6644 if (r_version < 1)
b1fbec62
GB
6645 {
6646 warning ("unexpected r_debug version %d", r_version);
8d56636a 6647 break;
b1fbec62 6648 }
8d56636a
MM
6649
6650 if (read_one_ptr (r_debug + lmo->r_map_offset, &lm_addr,
6651 ptr_size) != 0)
b1fbec62 6652 {
8d56636a
MM
6653 warning ("unable to read r_map from 0x%s",
6654 paddress (r_debug + lmo->r_map_offset));
6655 break;
b1fbec62 6656 }
2268b414 6657
ad10f44e
MM
6658 /* We read the entire namespace. */
6659 lm_prev = 0;
6660
6661 /* The first entry corresponds to the main executable unless the
6662 dynamic loader was loaded late by a static executable. But
6663 in such case the main executable does not have PT_DYNAMIC
6664 present and we would not have gotten here. */
6665 if (r_debug == priv->r_debug)
6666 {
6667 if (lm_addr != 0)
6668 string_appendf (document, " main-lm=\"0x%s\">",
6669 paddress (lm_addr));
6670 else
6671 document += ">";
6672
6673 lm_prev = lm_addr;
6674 if (read_one_ptr (lm_addr + lmo->l_next_offset,
6675 &lm_addr, ptr_size) != 0)
6676 {
6677 warning ("unable to read l_next from 0x%s",
6678 paddress (lm_addr + lmo->l_next_offset));
6679 break;
6680 }
6681 }
6682
2733d9d5 6683 read_link_map (document, r_debug, lm_addr, lm_prev, ptr_size, lmo);
b1fbec62 6684
8d56636a
MM
6685 if (r_version < 2)
6686 break;
b1fbec62 6687
8d56636a
MM
6688 if (read_one_ptr (r_debug + lmo->r_next_offset, &r_debug,
6689 ptr_size) != 0)
2268b414 6690 {
8d56636a
MM
6691 warning ("unable to read r_next from 0x%s",
6692 paddress (r_debug + lmo->r_next_offset));
6693 break;
d878444c 6694 }
0afae3cf 6695 }
2268b414
JK
6696 }
6697
ad10f44e 6698 document += "</library-list-svr4>";
b1fbec62 6699
f6e8a41e 6700 int document_len = document.length ();
2268b414
JK
6701 if (offset < document_len)
6702 document_len -= offset;
6703 else
6704 document_len = 0;
6705 if (len > document_len)
6706 len = document_len;
6707
f6e8a41e 6708 memcpy (readbuf, document.data () + offset, len);
2268b414
JK
6709
6710 return len;
6711}
6712
9accd112
MM
6713#ifdef HAVE_LINUX_BTRACE
6714
8263b346
TBA
6715bool
6716linux_process_target::supports_btrace ()
6717{
6718 return true;
6719}
6720
79597bdd 6721btrace_target_info *
696c0d5e 6722linux_process_target::enable_btrace (thread_info *tp,
79597bdd
TBA
6723 const btrace_config *conf)
6724{
696c0d5e 6725 return linux_enable_btrace (tp->id, conf);
79597bdd
TBA
6726}
6727
969c39fb 6728/* See to_disable_btrace target method. */
9accd112 6729
79597bdd
TBA
6730int
6731linux_process_target::disable_btrace (btrace_target_info *tinfo)
969c39fb
MM
6732{
6733 enum btrace_error err;
6734
6735 err = linux_disable_btrace (tinfo);
6736 return (err == BTRACE_ERR_NONE ? 0 : -1);
6737}
6738
bc504a31 6739/* Encode an Intel Processor Trace configuration. */
b20a6524
MM
6740
6741static void
6742linux_low_encode_pt_config (struct buffer *buffer,
6743 const struct btrace_data_pt_config *config)
6744{
6745 buffer_grow_str (buffer, "<pt-config>\n");
6746
6747 switch (config->cpu.vendor)
6748 {
6749 case CV_INTEL:
6750 buffer_xml_printf (buffer, "<cpu vendor=\"GenuineIntel\" family=\"%u\" "
6751 "model=\"%u\" stepping=\"%u\"/>\n",
6752 config->cpu.family, config->cpu.model,
6753 config->cpu.stepping);
6754 break;
6755
6756 default:
6757 break;
6758 }
6759
6760 buffer_grow_str (buffer, "</pt-config>\n");
6761}
6762
6763/* Encode a raw buffer. */
6764
6765static void
6766linux_low_encode_raw (struct buffer *buffer, const gdb_byte *data,
6767 unsigned int size)
6768{
6769 if (size == 0)
6770 return;
6771
268a13a5 6772 /* We use hex encoding - see gdbsupport/rsp-low.h. */
b20a6524
MM
6773 buffer_grow_str (buffer, "<raw>\n");
6774
6775 while (size-- > 0)
6776 {
6777 char elem[2];
6778
6779 elem[0] = tohex ((*data >> 4) & 0xf);
6780 elem[1] = tohex (*data++ & 0xf);
6781
6782 buffer_grow (buffer, elem, 2);
6783 }
6784
6785 buffer_grow_str (buffer, "</raw>\n");
6786}
6787
969c39fb
MM
6788/* See to_read_btrace target method. */
6789
79597bdd
TBA
6790int
6791linux_process_target::read_btrace (btrace_target_info *tinfo,
6792 buffer *buffer,
6793 enum btrace_read_type type)
9accd112 6794{
734b0e4b 6795 struct btrace_data btrace;
969c39fb 6796 enum btrace_error err;
9accd112 6797
969c39fb
MM
6798 err = linux_read_btrace (&btrace, tinfo, type);
6799 if (err != BTRACE_ERR_NONE)
6800 {
6801 if (err == BTRACE_ERR_OVERFLOW)
6802 buffer_grow_str0 (buffer, "E.Overflow.");
6803 else
6804 buffer_grow_str0 (buffer, "E.Generic Error.");
6805
8dcc53b3 6806 return -1;
969c39fb 6807 }
9accd112 6808
734b0e4b
MM
6809 switch (btrace.format)
6810 {
6811 case BTRACE_FORMAT_NONE:
6812 buffer_grow_str0 (buffer, "E.No Trace.");
8dcc53b3 6813 return -1;
734b0e4b
MM
6814
6815 case BTRACE_FORMAT_BTS:
6816 buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
6817 buffer_grow_str (buffer, "<btrace version=\"1.0\">\n");
9accd112 6818
46f29a9a 6819 for (const btrace_block &block : *btrace.variant.bts.blocks)
734b0e4b 6820 buffer_xml_printf (buffer, "<block begin=\"0x%s\" end=\"0x%s\"/>\n",
46f29a9a 6821 paddress (block.begin), paddress (block.end));
9accd112 6822
734b0e4b
MM
6823 buffer_grow_str0 (buffer, "</btrace>\n");
6824 break;
6825
b20a6524
MM
6826 case BTRACE_FORMAT_PT:
6827 buffer_grow_str (buffer, "<!DOCTYPE btrace SYSTEM \"btrace.dtd\">\n");
6828 buffer_grow_str (buffer, "<btrace version=\"1.0\">\n");
6829 buffer_grow_str (buffer, "<pt>\n");
6830
6831 linux_low_encode_pt_config (buffer, &btrace.variant.pt.config);
9accd112 6832
b20a6524
MM
6833 linux_low_encode_raw (buffer, btrace.variant.pt.data,
6834 btrace.variant.pt.size);
6835
6836 buffer_grow_str (buffer, "</pt>\n");
6837 buffer_grow_str0 (buffer, "</btrace>\n");
6838 break;
6839
6840 default:
6841 buffer_grow_str0 (buffer, "E.Unsupported Trace Format.");
8dcc53b3 6842 return -1;
734b0e4b 6843 }
969c39fb
MM
6844
6845 return 0;
9accd112 6846}
f4abbc16
MM
6847
6848/* See to_btrace_conf target method. */
6849
79597bdd
TBA
6850int
6851linux_process_target::read_btrace_conf (const btrace_target_info *tinfo,
6852 buffer *buffer)
f4abbc16
MM
6853{
6854 const struct btrace_config *conf;
6855
6856 buffer_grow_str (buffer, "<!DOCTYPE btrace-conf SYSTEM \"btrace-conf.dtd\">\n");
6857 buffer_grow_str (buffer, "<btrace-conf version=\"1.0\">\n");
6858
6859 conf = linux_btrace_conf (tinfo);
6860 if (conf != NULL)
6861 {
6862 switch (conf->format)
6863 {
6864 case BTRACE_FORMAT_NONE:
6865 break;
6866
6867 case BTRACE_FORMAT_BTS:
d33501a5
MM
6868 buffer_xml_printf (buffer, "<bts");
6869 buffer_xml_printf (buffer, " size=\"0x%x\"", conf->bts.size);
6870 buffer_xml_printf (buffer, " />\n");
f4abbc16 6871 break;
b20a6524
MM
6872
6873 case BTRACE_FORMAT_PT:
6874 buffer_xml_printf (buffer, "<pt");
6875 buffer_xml_printf (buffer, " size=\"0x%x\"", conf->pt.size);
6876 buffer_xml_printf (buffer, "/>\n");
6877 break;
f4abbc16
MM
6878 }
6879 }
6880
6881 buffer_grow_str0 (buffer, "</btrace-conf>\n");
6882 return 0;
6883}
9accd112
MM
6884#endif /* HAVE_LINUX_BTRACE */
6885
7b669087
GB
6886/* See nat/linux-nat.h. */
6887
6888ptid_t
6889current_lwp_ptid (void)
6890{
6891 return ptid_of (current_thread);
6892}
6893
7f63b89b
TBA
6894const char *
6895linux_process_target::thread_name (ptid_t thread)
6896{
6897 return linux_proc_tid_get_name (thread);
6898}
6899
6900#if USE_THREAD_DB
6901bool
6902linux_process_target::thread_handle (ptid_t ptid, gdb_byte **handle,
6903 int *handle_len)
6904{
6905 return thread_db_thread_handle (ptid, handle, handle_len);
6906}
6907#endif
6908
7b961964
SM
6909thread_info *
6910linux_process_target::thread_pending_parent (thread_info *thread)
6911{
6912 lwp_info *parent = get_thread_lwp (thread)->pending_parent ();
6913
6914 if (parent == nullptr)
6915 return nullptr;
6916
6917 return get_lwp_thread (parent);
6918}
6919
df5ad102
SM
6920thread_info *
6921linux_process_target::thread_pending_child (thread_info *thread)
6922{
6923 lwp_info *child = get_thread_lwp (thread)->pending_child ();
6924
6925 if (child == nullptr)
6926 return nullptr;
6927
6928 return get_lwp_thread (child);
6929}
6930
276d4552
YQ
6931/* Default implementation of linux_target_ops method "set_pc" for
6932 32-bit pc register which is literally named "pc". */
6933
6934void
6935linux_set_pc_32bit (struct regcache *regcache, CORE_ADDR pc)
6936{
6937 uint32_t newpc = pc;
6938
6939 supply_register_by_name (regcache, "pc", &newpc);
6940}
6941
6942/* Default implementation of linux_target_ops method "get_pc" for
6943 32-bit pc register which is literally named "pc". */
6944
6945CORE_ADDR
6946linux_get_pc_32bit (struct regcache *regcache)
6947{
6948 uint32_t pc;
6949
6950 collect_register_by_name (regcache, "pc", &pc);
c058728c 6951 threads_debug_printf ("stop pc is 0x%" PRIx32, pc);
276d4552
YQ
6952 return pc;
6953}
6954
6f69e520
YQ
6955/* Default implementation of linux_target_ops method "set_pc" for
6956 64-bit pc register which is literally named "pc". */
6957
6958void
6959linux_set_pc_64bit (struct regcache *regcache, CORE_ADDR pc)
6960{
6961 uint64_t newpc = pc;
6962
6963 supply_register_by_name (regcache, "pc", &newpc);
6964}
6965
6966/* Default implementation of linux_target_ops method "get_pc" for
6967 64-bit pc register which is literally named "pc". */
6968
6969CORE_ADDR
6970linux_get_pc_64bit (struct regcache *regcache)
6971{
6972 uint64_t pc;
6973
6974 collect_register_by_name (regcache, "pc", &pc);
c058728c 6975 threads_debug_printf ("stop pc is 0x%" PRIx64, pc);
6f69e520
YQ
6976 return pc;
6977}
6978
0570503d 6979/* See linux-low.h. */
974c89e0 6980
0570503d
PFC
6981int
6982linux_get_auxv (int wordsize, CORE_ADDR match, CORE_ADDR *valp)
974c89e0
AH
6983{
6984 gdb_byte *data = (gdb_byte *) alloca (2 * wordsize);
6985 int offset = 0;
6986
6987 gdb_assert (wordsize == 4 || wordsize == 8);
6988
52405d85 6989 while (the_target->read_auxv (offset, data, 2 * wordsize) == 2 * wordsize)
974c89e0
AH
6990 {
6991 if (wordsize == 4)
6992 {
0570503d 6993 uint32_t *data_p = (uint32_t *) data;
974c89e0 6994 if (data_p[0] == match)
0570503d
PFC
6995 {
6996 *valp = data_p[1];
6997 return 1;
6998 }
974c89e0
AH
6999 }
7000 else
7001 {
0570503d 7002 uint64_t *data_p = (uint64_t *) data;
974c89e0 7003 if (data_p[0] == match)
0570503d
PFC
7004 {
7005 *valp = data_p[1];
7006 return 1;
7007 }
974c89e0
AH
7008 }
7009
7010 offset += 2 * wordsize;
7011 }
7012
7013 return 0;
7014}
7015
7016/* See linux-low.h. */
7017
7018CORE_ADDR
7019linux_get_hwcap (int wordsize)
7020{
0570503d
PFC
7021 CORE_ADDR hwcap = 0;
7022 linux_get_auxv (wordsize, AT_HWCAP, &hwcap);
7023 return hwcap;
974c89e0
AH
7024}
7025
7026/* See linux-low.h. */
7027
7028CORE_ADDR
7029linux_get_hwcap2 (int wordsize)
7030{
0570503d
PFC
7031 CORE_ADDR hwcap2 = 0;
7032 linux_get_auxv (wordsize, AT_HWCAP2, &hwcap2);
7033 return hwcap2;
974c89e0 7034}
6f69e520 7035
3aee8918
PA
7036#ifdef HAVE_LINUX_REGSETS
7037void
7038initialize_regsets_info (struct regsets_info *info)
7039{
7040 for (info->num_regsets = 0;
7041 info->regsets[info->num_regsets].size >= 0;
7042 info->num_regsets++)
7043 ;
3aee8918
PA
7044}
7045#endif
7046
da6d8c04
DJ
7047void
7048initialize_low (void)
7049{
bd99dc85 7050 struct sigaction sigchld_action;
dd373349 7051
bd99dc85 7052 memset (&sigchld_action, 0, sizeof (sigchld_action));
ef0478f6 7053 set_target_ops (the_linux_target);
dd373349 7054
aa7c7447 7055 linux_ptrace_init_warnings ();
1b919490 7056 linux_proc_init_warnings ();
bd99dc85
PA
7057
7058 sigchld_action.sa_handler = sigchld_handler;
7059 sigemptyset (&sigchld_action.sa_mask);
7060 sigchld_action.sa_flags = SA_RESTART;
7061 sigaction (SIGCHLD, &sigchld_action, NULL);
3aee8918
PA
7062
7063 initialize_low_arch ();
89245bc0
DB
7064
7065 linux_check_ptrace_features ();
da6d8c04 7066}