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