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