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