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