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