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