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