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