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