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