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