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