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