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