]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/linux-low.c
* inferior.c (add_inferior): Move observer_notify_new_inferior
[thirdparty/binutils-gdb.git] / gdb / gdbserver / linux-low.c
CommitLineData
da6d8c04 1/* Low level interface to ptrace, for the remote server for GDB.
545587ee 2 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
0fb0cc75 3 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
da6d8c04
DJ
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
da6d8c04
DJ
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
da6d8c04
DJ
19
20#include "server.h"
58caa3dc 21#include "linux-low.h"
da6d8c04 22
58caa3dc 23#include <sys/wait.h>
da6d8c04
DJ
24#include <stdio.h>
25#include <sys/param.h>
da6d8c04 26#include <sys/ptrace.h>
da6d8c04
DJ
27#include <signal.h>
28#include <sys/ioctl.h>
29#include <fcntl.h>
d07c63e7 30#include <string.h>
0a30fbc4
DJ
31#include <stdlib.h>
32#include <unistd.h>
fa6a77dc 33#include <errno.h>
fd500816 34#include <sys/syscall.h>
f9387fc3 35#include <sched.h>
07e059b5
VP
36#include <ctype.h>
37#include <pwd.h>
38#include <sys/types.h>
39#include <dirent.h>
da6d8c04 40
32ca6d61
DJ
41#ifndef PTRACE_GETSIGINFO
42# define PTRACE_GETSIGINFO 0x4202
43# define PTRACE_SETSIGINFO 0x4203
44#endif
45
fd462a61
DJ
46#ifndef O_LARGEFILE
47#define O_LARGEFILE 0
48#endif
49
24a09b5f
DJ
50/* If the system headers did not provide the constants, hard-code the normal
51 values. */
52#ifndef PTRACE_EVENT_FORK
53
54#define PTRACE_SETOPTIONS 0x4200
55#define PTRACE_GETEVENTMSG 0x4201
56
57/* options set using PTRACE_SETOPTIONS */
58#define PTRACE_O_TRACESYSGOOD 0x00000001
59#define PTRACE_O_TRACEFORK 0x00000002
60#define PTRACE_O_TRACEVFORK 0x00000004
61#define PTRACE_O_TRACECLONE 0x00000008
62#define PTRACE_O_TRACEEXEC 0x00000010
63#define PTRACE_O_TRACEVFORKDONE 0x00000020
64#define PTRACE_O_TRACEEXIT 0x00000040
65
66/* Wait extended result codes for the above trace options. */
67#define PTRACE_EVENT_FORK 1
68#define PTRACE_EVENT_VFORK 2
69#define PTRACE_EVENT_CLONE 3
70#define PTRACE_EVENT_EXEC 4
71#define PTRACE_EVENT_VFORK_DONE 5
72#define PTRACE_EVENT_EXIT 6
73
74#endif /* PTRACE_EVENT_FORK */
75
76/* We can't always assume that this flag is available, but all systems
77 with the ptrace event handlers also have __WALL, so it's safe to use
78 in some contexts. */
79#ifndef __WALL
80#define __WALL 0x40000000 /* Wait for any child. */
81#endif
82
42c81e2a
DJ
83#ifdef __UCLIBC__
84#if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
85#define HAS_NOMMU
86#endif
87#endif
88
24a09b5f
DJ
89/* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
90 representation of the thread ID.
611cb4a5 91
54a0b537 92 ``all_lwps'' is keyed by the process ID - which on Linux is (presently)
95954743
PA
93 the same as the LWP ID.
94
95 ``all_processes'' is keyed by the "overall process ID", which
96 GNU/Linux calls tgid, "thread group ID". */
0d62e5e8 97
54a0b537 98struct inferior_list all_lwps;
0d62e5e8 99
24a09b5f
DJ
100/* A list of all unknown processes which receive stop signals. Some other
101 process will presumably claim each of these as forked children
102 momentarily. */
103
104struct inferior_list stopped_pids;
105
0d62e5e8
DJ
106/* FIXME this is a bit of a hack, and could be removed. */
107int stopping_threads;
108
109/* FIXME make into a target method? */
24a09b5f 110int using_threads = 1;
24a09b5f
DJ
111
112static int must_set_ptrace_flags;
0d62e5e8 113
95954743
PA
114/* This flag is true iff we've just created or attached to our first
115 inferior but it has not stopped yet. As soon as it does, we need
116 to call the low target's arch_setup callback. Doing this only on
117 the first inferior avoids reinializing the architecture on every
118 inferior, and avoids messing with the register caches of the
119 already running inferiors. NOTE: this assumes all inferiors under
120 control of gdbserver have the same architecture. */
d61ddec4
UW
121static int new_inferior;
122
54a0b537
PA
123static void linux_resume_one_lwp (struct inferior_list_entry *entry,
124 int step, int signal, siginfo_t *info);
2bd7c093 125static void linux_resume (struct thread_resume *resume_info, size_t n);
54a0b537 126static void stop_all_lwps (void);
95954743 127static int linux_wait_for_event (ptid_t ptid, int *wstat, int options);
54a0b537 128static int check_removed_breakpoint (struct lwp_info *event_child);
95954743 129static void *add_lwp (ptid_t ptid);
97438e3f 130static int my_waitpid (int pid, int *status, int flags);
c35fafde 131static int linux_stopped_by_watchpoint (void);
95954743 132static void mark_lwp_dead (struct lwp_info *lwp, int wstat);
0d62e5e8
DJ
133
134struct pending_signals
135{
136 int signal;
32ca6d61 137 siginfo_t info;
0d62e5e8
DJ
138 struct pending_signals *prev;
139};
611cb4a5 140
d844cde6 141#define PTRACE_ARG3_TYPE long
c6ecbae5 142#define PTRACE_XFER_TYPE long
da6d8c04 143
58caa3dc 144#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
145static char *disabled_regsets;
146static int num_regsets;
58caa3dc
DJ
147#endif
148
bd99dc85
PA
149/* The read/write ends of the pipe registered as waitable file in the
150 event loop. */
151static int linux_event_pipe[2] = { -1, -1 };
152
153/* True if we're currently in async mode. */
154#define target_is_async_p() (linux_event_pipe[0] != -1)
155
156static void send_sigstop (struct inferior_list_entry *entry);
157static void wait_for_sigstop (struct inferior_list_entry *entry);
158
159static void
160delete_lwp (struct lwp_info *lwp)
161{
162 remove_thread (get_lwp_thread (lwp));
163 remove_inferior (&all_lwps, &lwp->head);
164 free (lwp);
165}
166
95954743
PA
167/* Add a process to the common process list, and set its private
168 data. */
169
170static struct process_info *
171linux_add_process (int pid, int attached)
172{
173 struct process_info *proc;
174
175 /* Is this the first process? If so, then set the arch. */
176 if (all_processes.head == NULL)
177 new_inferior = 1;
178
179 proc = add_process (pid, attached);
180 proc->private = xcalloc (1, sizeof (*proc->private));
181
182 return proc;
183}
184
5091eb23
DE
185/* Remove a process from the common process list,
186 also freeing all private data. */
187
188static void
189linux_remove_process (struct process_info *process)
190{
191 free (process->private);
192 remove_process (process);
193}
194
bd99dc85
PA
195/* Handle a GNU/Linux extended wait response. If we see a clone
196 event, we need to add the new LWP to our list (and not report the
197 trap to higher layers). */
0d62e5e8 198
24a09b5f 199static void
54a0b537 200handle_extended_wait (struct lwp_info *event_child, int wstat)
24a09b5f
DJ
201{
202 int event = wstat >> 16;
54a0b537 203 struct lwp_info *new_lwp;
24a09b5f
DJ
204
205 if (event == PTRACE_EVENT_CLONE)
206 {
95954743 207 ptid_t ptid;
24a09b5f 208 unsigned long new_pid;
836acd6d 209 int ret, status = W_STOPCODE (SIGSTOP);
24a09b5f 210
bd99dc85 211 ptrace (PTRACE_GETEVENTMSG, lwpid_of (event_child), 0, &new_pid);
24a09b5f
DJ
212
213 /* If we haven't already seen the new PID stop, wait for it now. */
214 if (! pull_pid_from_list (&stopped_pids, new_pid))
215 {
216 /* The new child has a pending SIGSTOP. We can't affect it until it
217 hits the SIGSTOP, but we're already attached. */
218
97438e3f 219 ret = my_waitpid (new_pid, &status, __WALL);
24a09b5f
DJ
220
221 if (ret == -1)
222 perror_with_name ("waiting for new child");
223 else if (ret != new_pid)
224 warning ("wait returned unexpected PID %d", ret);
da5898ce 225 else if (!WIFSTOPPED (status))
24a09b5f
DJ
226 warning ("wait returned unexpected status 0x%x", status);
227 }
228
229 ptrace (PTRACE_SETOPTIONS, new_pid, 0, PTRACE_O_TRACECLONE);
230
95954743
PA
231 ptid = ptid_build (pid_of (event_child), new_pid, 0);
232 new_lwp = (struct lwp_info *) add_lwp (ptid);
233 add_thread (ptid, new_lwp);
24a09b5f 234
da5898ce
DJ
235 /* Normally we will get the pending SIGSTOP. But in some cases
236 we might get another signal delivered to the group first.
f21cc1a2 237 If we do get another signal, be sure not to lose it. */
da5898ce
DJ
238 if (WSTOPSIG (status) == SIGSTOP)
239 {
240 if (stopping_threads)
54a0b537 241 new_lwp->stopped = 1;
da5898ce
DJ
242 else
243 ptrace (PTRACE_CONT, new_pid, 0, 0);
244 }
24a09b5f 245 else
da5898ce 246 {
54a0b537 247 new_lwp->stop_expected = 1;
da5898ce
DJ
248 if (stopping_threads)
249 {
54a0b537
PA
250 new_lwp->stopped = 1;
251 new_lwp->status_pending_p = 1;
252 new_lwp->status_pending = status;
da5898ce
DJ
253 }
254 else
255 /* Pass the signal on. This is what GDB does - except
256 shouldn't we really report it instead? */
257 ptrace (PTRACE_CONT, new_pid, 0, WSTOPSIG (status));
258 }
24a09b5f
DJ
259
260 /* Always resume the current thread. If we are stopping
261 threads, it will have a pending SIGSTOP; we may as well
262 collect it now. */
54a0b537
PA
263 linux_resume_one_lwp (&event_child->head,
264 event_child->stepping, 0, NULL);
24a09b5f
DJ
265 }
266}
267
0d62e5e8
DJ
268/* This function should only be called if the process got a SIGTRAP.
269 The SIGTRAP could mean several things.
270
271 On i386, where decr_pc_after_break is non-zero:
272 If we were single-stepping this process using PTRACE_SINGLESTEP,
273 we will get only the one SIGTRAP (even if the instruction we
274 stepped over was a breakpoint). The value of $eip will be the
275 next instruction.
276 If we continue the process using PTRACE_CONT, we will get a
277 SIGTRAP when we hit a breakpoint. The value of $eip will be
278 the instruction after the breakpoint (i.e. needs to be
279 decremented). If we report the SIGTRAP to GDB, we must also
280 report the undecremented PC. If we cancel the SIGTRAP, we
281 must resume at the decremented PC.
282
283 (Presumably, not yet tested) On a non-decr_pc_after_break machine
284 with hardware or kernel single-step:
285 If we single-step over a breakpoint instruction, our PC will
286 point at the following instruction. If we continue and hit a
287 breakpoint instruction, our PC will point at the breakpoint
288 instruction. */
289
290static CORE_ADDR
291get_stop_pc (void)
292{
293 CORE_ADDR stop_pc = (*the_low_target.get_pc) ();
294
54a0b537 295 if (get_thread_lwp (current_inferior)->stepping)
0d62e5e8
DJ
296 return stop_pc;
297 else
298 return stop_pc - the_low_target.decr_pc_after_break;
299}
ce3a066d 300
0d62e5e8 301static void *
95954743 302add_lwp (ptid_t ptid)
611cb4a5 303{
54a0b537 304 struct lwp_info *lwp;
0d62e5e8 305
54a0b537
PA
306 lwp = (struct lwp_info *) xmalloc (sizeof (*lwp));
307 memset (lwp, 0, sizeof (*lwp));
0d62e5e8 308
95954743 309 lwp->head.id = ptid;
0d62e5e8 310
54a0b537 311 add_inferior_to_list (&all_lwps, &lwp->head);
0d62e5e8 312
54a0b537 313 return lwp;
0d62e5e8 314}
611cb4a5 315
da6d8c04
DJ
316/* Start an inferior process and returns its pid.
317 ALLARGS is a vector of program-name and args. */
318
ce3a066d
DJ
319static int
320linux_create_inferior (char *program, char **allargs)
da6d8c04 321{
54a0b537 322 void *new_lwp;
da6d8c04 323 int pid;
95954743 324 ptid_t ptid;
da6d8c04 325
42c81e2a 326#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
327 pid = vfork ();
328#else
da6d8c04 329 pid = fork ();
52fb6437 330#endif
da6d8c04
DJ
331 if (pid < 0)
332 perror_with_name ("fork");
333
334 if (pid == 0)
335 {
336 ptrace (PTRACE_TRACEME, 0, 0, 0);
337
254787d4 338 signal (__SIGRTMIN + 1, SIG_DFL);
0d62e5e8 339
a9fa9f7d
DJ
340 setpgid (0, 0);
341
2b876972
DJ
342 execv (program, allargs);
343 if (errno == ENOENT)
344 execvp (program, allargs);
da6d8c04
DJ
345
346 fprintf (stderr, "Cannot exec %s: %s.\n", program,
d07c63e7 347 strerror (errno));
da6d8c04
DJ
348 fflush (stderr);
349 _exit (0177);
350 }
351
95954743
PA
352 linux_add_process (pid, 0);
353
354 ptid = ptid_build (pid, pid, 0);
355 new_lwp = add_lwp (ptid);
356 add_thread (ptid, new_lwp);
24a09b5f 357 must_set_ptrace_flags = 1;
611cb4a5 358
a9fa9f7d 359 return pid;
da6d8c04
DJ
360}
361
362/* Attach to an inferior process. */
363
95954743
PA
364static void
365linux_attach_lwp_1 (unsigned long lwpid, int initial)
da6d8c04 366{
95954743 367 ptid_t ptid;
54a0b537 368 struct lwp_info *new_lwp;
611cb4a5 369
95954743 370 if (ptrace (PTRACE_ATTACH, lwpid, 0, 0) != 0)
da6d8c04 371 {
95954743 372 if (!initial)
2d717e4f
DJ
373 {
374 /* If we fail to attach to an LWP, just warn. */
95954743 375 fprintf (stderr, "Cannot attach to lwp %ld: %s (%d)\n", lwpid,
2d717e4f
DJ
376 strerror (errno), errno);
377 fflush (stderr);
378 return;
379 }
380 else
381 /* If we fail to attach to a process, report an error. */
95954743 382 error ("Cannot attach to lwp %ld: %s (%d)\n", lwpid,
43d5792c 383 strerror (errno), errno);
da6d8c04
DJ
384 }
385
0e21c1ec
DE
386 /* FIXME: This intermittently fails.
387 We need to wait for SIGSTOP first. */
95954743
PA
388 ptrace (PTRACE_SETOPTIONS, lwpid, 0, PTRACE_O_TRACECLONE);
389
390 if (initial)
391 /* NOTE/FIXME: This lwp might have not been the tgid. */
392 ptid = ptid_build (lwpid, lwpid, 0);
393 else
394 {
395 /* Note that extracting the pid from the current inferior is
396 safe, since we're always called in the context of the same
397 process as this new thread. */
398 int pid = pid_of (get_thread_lwp (current_inferior));
399 ptid = ptid_build (pid, lwpid, 0);
400 }
24a09b5f 401
95954743
PA
402 new_lwp = (struct lwp_info *) add_lwp (ptid);
403 add_thread (ptid, new_lwp);
0d62e5e8
DJ
404
405 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
0e21c1ec
DE
406 brings it to a halt.
407
408 There are several cases to consider here:
409
410 1) gdbserver has already attached to the process and is being notified
1b3f6016
PA
411 of a new thread that is being created.
412 In this case we should ignore that SIGSTOP and resume the process.
413 This is handled below by setting stop_expected = 1.
0e21c1ec
DE
414
415 2) This is the first thread (the process thread), and we're attaching
1b3f6016
PA
416 to it via attach_inferior.
417 In this case we want the process thread to stop.
418 This is handled by having linux_attach clear stop_expected after
419 we return.
420 ??? If the process already has several threads we leave the other
421 threads running.
0e21c1ec
DE
422
423 3) GDB is connecting to gdbserver and is requesting an enumeration of all
1b3f6016
PA
424 existing threads.
425 In this case we want the thread to stop.
426 FIXME: This case is currently not properly handled.
427 We should wait for the SIGSTOP but don't. Things work apparently
428 because enough time passes between when we ptrace (ATTACH) and when
429 gdb makes the next ptrace call on the thread.
0d62e5e8
DJ
430
431 On the other hand, if we are currently trying to stop all threads, we
432 should treat the new thread as if we had sent it a SIGSTOP. This works
54a0b537 433 because we are guaranteed that the add_lwp call above added us to the
0e21c1ec
DE
434 end of the list, and so the new thread has not yet reached
435 wait_for_sigstop (but will). */
0d62e5e8 436 if (! stopping_threads)
54a0b537 437 new_lwp->stop_expected = 1;
0d62e5e8
DJ
438}
439
95954743
PA
440void
441linux_attach_lwp (unsigned long lwpid)
442{
443 linux_attach_lwp_1 (lwpid, 0);
444}
445
0d62e5e8 446int
a1928bad 447linux_attach (unsigned long pid)
0d62e5e8 448{
54a0b537 449 struct lwp_info *lwp;
0d62e5e8 450
95954743
PA
451 linux_attach_lwp_1 (pid, 1);
452
453 linux_add_process (pid, 1);
0d62e5e8 454
bd99dc85
PA
455 if (!non_stop)
456 {
457 /* Don't ignore the initial SIGSTOP if we just attached to this
458 process. It will be collected by wait shortly. */
95954743
PA
459 lwp = (struct lwp_info *) find_inferior_id (&all_lwps,
460 ptid_build (pid, pid, 0));
bd99dc85
PA
461 lwp->stop_expected = 0;
462 }
0d62e5e8 463
95954743
PA
464 return 0;
465}
466
467struct counter
468{
469 int pid;
470 int count;
471};
472
473static int
474second_thread_of_pid_p (struct inferior_list_entry *entry, void *args)
475{
476 struct counter *counter = args;
477
478 if (ptid_get_pid (entry->id) == counter->pid)
479 {
480 if (++counter->count > 1)
481 return 1;
482 }
d61ddec4 483
da6d8c04
DJ
484 return 0;
485}
486
95954743
PA
487static int
488last_thread_of_process_p (struct thread_info *thread)
489{
490 ptid_t ptid = ((struct inferior_list_entry *)thread)->id;
491 int pid = ptid_get_pid (ptid);
492 struct counter counter = { pid , 0 };
da6d8c04 493
95954743
PA
494 return (find_inferior (&all_threads,
495 second_thread_of_pid_p, &counter) == NULL);
496}
497
498/* Kill the inferior lwp. */
499
500static int
501linux_kill_one_lwp (struct inferior_list_entry *entry, void *args)
da6d8c04 502{
0d62e5e8 503 struct thread_info *thread = (struct thread_info *) entry;
54a0b537 504 struct lwp_info *lwp = get_thread_lwp (thread);
0d62e5e8 505 int wstat;
95954743
PA
506 int pid = * (int *) args;
507
508 if (ptid_get_pid (entry->id) != pid)
509 return 0;
0d62e5e8 510
fd500816
DJ
511 /* We avoid killing the first thread here, because of a Linux kernel (at
512 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
513 the children get a chance to be reaped, it will remain a zombie
514 forever. */
95954743
PA
515
516 if (last_thread_of_process_p (thread))
517 {
518 if (debug_threads)
519 fprintf (stderr, "lkop: is last of process %s\n",
520 target_pid_to_str (entry->id));
521 return 0;
522 }
fd500816 523
bd99dc85
PA
524 /* If we're killing a running inferior, make sure it is stopped
525 first, as PTRACE_KILL will not work otherwise. */
526 if (!lwp->stopped)
527 send_sigstop (&lwp->head);
528
0d62e5e8
DJ
529 do
530 {
bd99dc85 531 ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0);
0d62e5e8
DJ
532
533 /* Make sure it died. The loop is most likely unnecessary. */
95954743 534 pid = linux_wait_for_event (lwp->head.id, &wstat, __WALL);
bd99dc85 535 } while (pid > 0 && WIFSTOPPED (wstat));
95954743
PA
536
537 return 0;
da6d8c04
DJ
538}
539
95954743
PA
540static int
541linux_kill (int pid)
0d62e5e8 542{
95954743 543 struct process_info *process;
54a0b537 544 struct lwp_info *lwp;
95954743 545 struct thread_info *thread;
fd500816 546 int wstat;
95954743 547 int lwpid;
fd500816 548
95954743
PA
549 process = find_process_pid (pid);
550 if (process == NULL)
551 return -1;
9d606399 552
95954743 553 find_inferior (&all_threads, linux_kill_one_lwp, &pid);
fd500816 554
54a0b537 555 /* See the comment in linux_kill_one_lwp. We did not kill the first
fd500816 556 thread in the list, so do so now. */
95954743
PA
557 lwp = find_lwp_pid (pid_to_ptid (pid));
558 thread = get_lwp_thread (lwp);
bd99dc85
PA
559
560 if (debug_threads)
95954743
PA
561 fprintf (stderr, "lk_1: killing lwp %ld, for pid: %d\n",
562 lwpid_of (lwp), pid);
bd99dc85
PA
563
564 /* If we're killing a running inferior, make sure it is stopped
565 first, as PTRACE_KILL will not work otherwise. */
566 if (!lwp->stopped)
567 send_sigstop (&lwp->head);
568
fd500816
DJ
569 do
570 {
bd99dc85 571 ptrace (PTRACE_KILL, lwpid_of (lwp), 0, 0);
fd500816
DJ
572
573 /* Make sure it died. The loop is most likely unnecessary. */
95954743
PA
574 lwpid = linux_wait_for_event (lwp->head.id, &wstat, __WALL);
575 } while (lwpid > 0 && WIFSTOPPED (wstat));
2d717e4f 576
bd99dc85 577 delete_lwp (lwp);
5091eb23 578 linux_remove_process (process);
95954743 579 return 0;
0d62e5e8
DJ
580}
581
95954743
PA
582static int
583linux_detach_one_lwp (struct inferior_list_entry *entry, void *args)
6ad8ae5c
DJ
584{
585 struct thread_info *thread = (struct thread_info *) entry;
54a0b537 586 struct lwp_info *lwp = get_thread_lwp (thread);
95954743
PA
587 int pid = * (int *) args;
588
589 if (ptid_get_pid (entry->id) != pid)
590 return 0;
6ad8ae5c 591
bd99dc85
PA
592 /* If we're detaching from a running inferior, make sure it is
593 stopped first, as PTRACE_DETACH will not work otherwise. */
594 if (!lwp->stopped)
595 {
95954743 596 int lwpid = lwpid_of (lwp);
bd99dc85
PA
597
598 stopping_threads = 1;
599 send_sigstop (&lwp->head);
600
601 /* If this detects a new thread through a clone event, the new
602 thread is appended to the end of the lwp list, so we'll
603 eventually detach from it. */
604 wait_for_sigstop (&lwp->head);
605 stopping_threads = 0;
606
607 /* If LWP exits while we're trying to stop it, there's nothing
608 left to do. */
95954743 609 lwp = find_lwp_pid (pid_to_ptid (lwpid));
bd99dc85 610 if (lwp == NULL)
95954743 611 return 0;
bd99dc85
PA
612 }
613
ae13219e
DJ
614 /* Make sure the process isn't stopped at a breakpoint that's
615 no longer there. */
54a0b537 616 check_removed_breakpoint (lwp);
ae13219e
DJ
617
618 /* If this process is stopped but is expecting a SIGSTOP, then make
619 sure we take care of that now. This isn't absolutely guaranteed
620 to collect the SIGSTOP, but is fairly likely to. */
54a0b537 621 if (lwp->stop_expected)
ae13219e 622 {
bd99dc85 623 int wstat;
ae13219e 624 /* Clear stop_expected, so that the SIGSTOP will be reported. */
54a0b537
PA
625 lwp->stop_expected = 0;
626 if (lwp->stopped)
627 linux_resume_one_lwp (&lwp->head, 0, 0, NULL);
95954743 628 linux_wait_for_event (lwp->head.id, &wstat, __WALL);
ae13219e
DJ
629 }
630
631 /* Flush any pending changes to the process's registers. */
632 regcache_invalidate_one ((struct inferior_list_entry *)
54a0b537 633 get_lwp_thread (lwp));
ae13219e
DJ
634
635 /* Finally, let it resume. */
bd99dc85
PA
636 ptrace (PTRACE_DETACH, lwpid_of (lwp), 0, 0);
637
638 delete_lwp (lwp);
95954743 639 return 0;
6ad8ae5c
DJ
640}
641
dd6953e1 642static int
95954743 643any_thread_of (struct inferior_list_entry *entry, void *args)
6ad8ae5c 644{
95954743
PA
645 int *pid_p = args;
646
647 if (ptid_get_pid (entry->id) == *pid_p)
648 return 1;
649
650 return 0;
651}
652
653static int
654linux_detach (int pid)
655{
656 struct process_info *process;
657
658 process = find_process_pid (pid);
659 if (process == NULL)
660 return -1;
661
662 current_inferior =
663 (struct thread_info *) find_inferior (&all_threads, any_thread_of, &pid);
664
ae13219e 665 delete_all_breakpoints ();
95954743 666 find_inferior (&all_threads, linux_detach_one_lwp, &pid);
5091eb23 667 linux_remove_process (process);
dd6953e1 668 return 0;
6ad8ae5c
DJ
669}
670
444d6139 671static void
95954743 672linux_join (int pid)
444d6139 673{
444d6139 674 int status, ret;
95954743 675 struct process_info *process;
bd99dc85 676
95954743
PA
677 process = find_process_pid (pid);
678 if (process == NULL)
679 return;
444d6139
PA
680
681 do {
95954743 682 ret = my_waitpid (pid, &status, 0);
444d6139
PA
683 if (WIFEXITED (status) || WIFSIGNALED (status))
684 break;
685 } while (ret != -1 || errno != ECHILD);
686}
687
6ad8ae5c 688/* Return nonzero if the given thread is still alive. */
0d62e5e8 689static int
95954743 690linux_thread_alive (ptid_t ptid)
0d62e5e8 691{
95954743
PA
692 struct lwp_info *lwp = find_lwp_pid (ptid);
693
694 /* We assume we always know if a thread exits. If a whole process
695 exited but we still haven't been able to report it to GDB, we'll
696 hold on to the last lwp of the dead process. */
697 if (lwp != NULL)
698 return !lwp->dead;
0d62e5e8
DJ
699 else
700 return 0;
701}
702
703/* Return nonzero if this process stopped at a breakpoint which
704 no longer appears to be inserted. Also adjust the PC
705 appropriately to resume where the breakpoint used to be. */
ce3a066d 706static int
54a0b537 707check_removed_breakpoint (struct lwp_info *event_child)
da6d8c04 708{
0d62e5e8
DJ
709 CORE_ADDR stop_pc;
710 struct thread_info *saved_inferior;
711
712 if (event_child->pending_is_breakpoint == 0)
713 return 0;
714
715 if (debug_threads)
54a0b537 716 fprintf (stderr, "Checking for breakpoint in lwp %ld.\n",
bd99dc85 717 lwpid_of (event_child));
0d62e5e8
DJ
718
719 saved_inferior = current_inferior;
54a0b537 720 current_inferior = get_lwp_thread (event_child);
0d62e5e8
DJ
721
722 stop_pc = get_stop_pc ();
723
724 /* If the PC has changed since we stopped, then we shouldn't do
725 anything. This happens if, for instance, GDB handled the
726 decr_pc_after_break subtraction itself. */
727 if (stop_pc != event_child->pending_stop_pc)
728 {
729 if (debug_threads)
ae13219e
DJ
730 fprintf (stderr, "Ignoring, PC was changed. Old PC was 0x%08llx\n",
731 event_child->pending_stop_pc);
0d62e5e8
DJ
732
733 event_child->pending_is_breakpoint = 0;
734 current_inferior = saved_inferior;
735 return 0;
736 }
737
738 /* If the breakpoint is still there, we will report hitting it. */
739 if ((*the_low_target.breakpoint_at) (stop_pc))
740 {
741 if (debug_threads)
742 fprintf (stderr, "Ignoring, breakpoint is still present.\n");
743 current_inferior = saved_inferior;
744 return 0;
745 }
746
747 if (debug_threads)
748 fprintf (stderr, "Removed breakpoint.\n");
749
750 /* For decr_pc_after_break targets, here is where we perform the
751 decrement. We go immediately from this function to resuming,
752 and can not safely call get_stop_pc () again. */
753 if (the_low_target.set_pc != NULL)
754 (*the_low_target.set_pc) (stop_pc);
755
756 /* We consumed the pending SIGTRAP. */
5544ad89 757 event_child->pending_is_breakpoint = 0;
0d62e5e8
DJ
758 event_child->status_pending_p = 0;
759 event_child->status_pending = 0;
760
761 current_inferior = saved_inferior;
da6d8c04
DJ
762 return 1;
763}
764
54a0b537
PA
765/* Return 1 if this lwp has an interesting status pending. This
766 function may silently resume an inferior lwp. */
611cb4a5 767static int
95954743 768status_pending_p (struct inferior_list_entry *entry, void *arg)
0d62e5e8 769{
54a0b537 770 struct lwp_info *lwp = (struct lwp_info *) entry;
95954743
PA
771 ptid_t ptid = * (ptid_t *) arg;
772
773 /* Check if we're only interested in events from a specific process
774 or its lwps. */
775 if (!ptid_equal (minus_one_ptid, ptid)
776 && ptid_get_pid (ptid) != ptid_get_pid (lwp->head.id))
777 return 0;
0d62e5e8 778
bd99dc85 779 if (lwp->status_pending_p && !lwp->suspended)
54a0b537 780 if (check_removed_breakpoint (lwp))
0d62e5e8
DJ
781 {
782 /* This thread was stopped at a breakpoint, and the breakpoint
783 is now gone. We were told to continue (or step...) all threads,
784 so GDB isn't trying to single-step past this breakpoint.
785 So instead of reporting the old SIGTRAP, pretend we got to
786 the breakpoint just after it was removed instead of just
787 before; resume the process. */
54a0b537 788 linux_resume_one_lwp (&lwp->head, 0, 0, NULL);
0d62e5e8
DJ
789 return 0;
790 }
791
bd99dc85 792 return (lwp->status_pending_p && !lwp->suspended);
0d62e5e8
DJ
793}
794
95954743
PA
795static int
796same_lwp (struct inferior_list_entry *entry, void *data)
797{
798 ptid_t ptid = *(ptid_t *) data;
799 int lwp;
800
801 if (ptid_get_lwp (ptid) != 0)
802 lwp = ptid_get_lwp (ptid);
803 else
804 lwp = ptid_get_pid (ptid);
805
806 if (ptid_get_lwp (entry->id) == lwp)
807 return 1;
808
809 return 0;
810}
811
812struct lwp_info *
813find_lwp_pid (ptid_t ptid)
814{
815 return (struct lwp_info*) find_inferior (&all_lwps, same_lwp, &ptid);
816}
817
bd99dc85 818static struct lwp_info *
95954743 819linux_wait_for_lwp (ptid_t ptid, int *wstatp, int options)
611cb4a5 820{
0d62e5e8 821 int ret;
95954743 822 int to_wait_for = -1;
bd99dc85 823 struct lwp_info *child = NULL;
0d62e5e8 824
bd99dc85 825 if (debug_threads)
95954743
PA
826 fprintf (stderr, "linux_wait_for_lwp: %s\n", target_pid_to_str (ptid));
827
828 if (ptid_equal (ptid, minus_one_ptid))
829 to_wait_for = -1; /* any child */
830 else
831 to_wait_for = ptid_get_lwp (ptid); /* this lwp only */
0d62e5e8 832
bd99dc85 833 options |= __WALL;
0d62e5e8 834
bd99dc85 835retry:
0d62e5e8 836
bd99dc85
PA
837 ret = my_waitpid (to_wait_for, wstatp, options);
838 if (ret == 0 || (ret == -1 && errno == ECHILD && (options & WNOHANG)))
839 return NULL;
840 else if (ret == -1)
841 perror_with_name ("waitpid");
0d62e5e8
DJ
842
843 if (debug_threads
844 && (!WIFSTOPPED (*wstatp)
845 || (WSTOPSIG (*wstatp) != 32
846 && WSTOPSIG (*wstatp) != 33)))
847 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
848
95954743 849 child = find_lwp_pid (pid_to_ptid (ret));
0d62e5e8 850
24a09b5f
DJ
851 /* If we didn't find a process, one of two things presumably happened:
852 - A process we started and then detached from has exited. Ignore it.
853 - A process we are controlling has forked and the new child's stop
854 was reported to us by the kernel. Save its PID. */
bd99dc85 855 if (child == NULL && WIFSTOPPED (*wstatp))
24a09b5f
DJ
856 {
857 add_pid_to_list (&stopped_pids, ret);
858 goto retry;
859 }
bd99dc85 860 else if (child == NULL)
24a09b5f
DJ
861 goto retry;
862
bd99dc85
PA
863 child->stopped = 1;
864 child->pending_is_breakpoint = 0;
0d62e5e8 865
bd99dc85 866 child->last_status = *wstatp;
32ca6d61 867
d61ddec4
UW
868 /* Architecture-specific setup after inferior is running.
869 This needs to happen after we have attached to the inferior
870 and it is stopped for the first time, but before we access
871 any inferior registers. */
872 if (new_inferior)
873 {
874 the_low_target.arch_setup ();
52fa2412
UW
875#ifdef HAVE_LINUX_REGSETS
876 memset (disabled_regsets, 0, num_regsets);
877#endif
d61ddec4
UW
878 new_inferior = 0;
879 }
880
0d62e5e8
DJ
881 if (debug_threads
882 && WIFSTOPPED (*wstatp))
883 {
896c7fbb 884 struct thread_info *saved_inferior = current_inferior;
0d62e5e8 885 current_inferior = (struct thread_info *)
95954743 886 find_inferior_id (&all_threads, child->head.id);
0d62e5e8
DJ
887 /* For testing only; i386_stop_pc prints out a diagnostic. */
888 if (the_low_target.get_pc != NULL)
889 get_stop_pc ();
896c7fbb 890 current_inferior = saved_inferior;
0d62e5e8 891 }
bd99dc85
PA
892
893 return child;
0d62e5e8 894}
611cb4a5 895
bd99dc85
PA
896/* Wait for an event from child PID. If PID is -1, wait for any
897 child. Store the stop status through the status pointer WSTAT.
898 OPTIONS is passed to the waitpid call. Return 0 if no child stop
899 event was found and OPTIONS contains WNOHANG. Return the PID of
900 the stopped child otherwise. */
901
0d62e5e8 902static int
95954743 903linux_wait_for_event_1 (ptid_t ptid, int *wstat, int options)
0d62e5e8
DJ
904{
905 CORE_ADDR stop_pc;
bd99dc85 906 struct lwp_info *event_child = NULL;
b65d95c5 907 int bp_status;
bd99dc85 908 struct lwp_info *requested_child = NULL;
0d62e5e8 909
95954743 910 /* Check for a lwp with a pending status. */
0d62e5e8
DJ
911 /* It is possible that the user changed the pending task's registers since
912 it stopped. We correctly handle the change of PC if we hit a breakpoint
e5379b03 913 (in check_removed_breakpoint); signals should be reported anyway. */
bd99dc85 914
95954743
PA
915 if (ptid_equal (ptid, minus_one_ptid)
916 || ptid_equal (pid_to_ptid (ptid_get_pid (ptid)), ptid))
0d62e5e8 917 {
54a0b537 918 event_child = (struct lwp_info *)
95954743 919 find_inferior (&all_lwps, status_pending_p, &ptid);
0d62e5e8 920 if (debug_threads && event_child)
bd99dc85 921 fprintf (stderr, "Got a pending child %ld\n", lwpid_of (event_child));
0d62e5e8
DJ
922 }
923 else
924 {
95954743 925 requested_child = find_lwp_pid (ptid);
bd99dc85
PA
926 if (requested_child->status_pending_p
927 && !check_removed_breakpoint (requested_child))
928 event_child = requested_child;
0d62e5e8 929 }
611cb4a5 930
0d62e5e8
DJ
931 if (event_child != NULL)
932 {
bd99dc85
PA
933 if (debug_threads)
934 fprintf (stderr, "Got an event from pending child %ld (%04x)\n",
935 lwpid_of (event_child), event_child->status_pending);
936 *wstat = event_child->status_pending;
937 event_child->status_pending_p = 0;
938 event_child->status_pending = 0;
939 current_inferior = get_lwp_thread (event_child);
940 return lwpid_of (event_child);
0d62e5e8
DJ
941 }
942
943 /* We only enter this loop if no process has a pending wait status. Thus
944 any action taken in response to a wait status inside this loop is
945 responding as soon as we detect the status, not after any pending
946 events. */
947 while (1)
948 {
95954743 949 event_child = linux_wait_for_lwp (ptid, wstat, options);
0d62e5e8 950
bd99dc85
PA
951 if ((options & WNOHANG) && event_child == NULL)
952 return 0;
0d62e5e8
DJ
953
954 if (event_child == NULL)
955 error ("event from unknown child");
611cb4a5 956
bd99dc85 957 current_inferior = get_lwp_thread (event_child);
0d62e5e8 958
89be2091 959 /* Check for thread exit. */
bd99dc85 960 if (! WIFSTOPPED (*wstat))
0d62e5e8 961 {
89be2091 962 if (debug_threads)
95954743 963 fprintf (stderr, "LWP %ld exiting\n", lwpid_of (event_child));
89be2091
DJ
964
965 /* If the last thread is exiting, just return. */
95954743 966 if (last_thread_of_process_p (current_inferior))
bd99dc85
PA
967 {
968 if (debug_threads)
95954743
PA
969 fprintf (stderr, "LWP %ld is last lwp of process\n",
970 lwpid_of (event_child));
bd99dc85
PA
971 return lwpid_of (event_child);
972 }
89be2091 973
bd99dc85 974 delete_lwp (event_child);
89be2091 975
bd99dc85
PA
976 if (!non_stop)
977 {
978 current_inferior = (struct thread_info *) all_threads.head;
979 if (debug_threads)
980 fprintf (stderr, "Current inferior is now %ld\n",
981 lwpid_of (get_thread_lwp (current_inferior)));
982 }
983 else
984 {
985 current_inferior = NULL;
986 if (debug_threads)
987 fprintf (stderr, "Current inferior is now <NULL>\n");
988 }
89be2091
DJ
989
990 /* If we were waiting for this particular child to do something...
991 well, it did something. */
bd99dc85 992 if (requested_child != NULL)
95954743 993 return lwpid_of (event_child);
89be2091
DJ
994
995 /* Wait for a more interesting event. */
996 continue;
997 }
998
bd99dc85
PA
999 if (WIFSTOPPED (*wstat)
1000 && WSTOPSIG (*wstat) == SIGSTOP
89be2091
DJ
1001 && event_child->stop_expected)
1002 {
1003 if (debug_threads)
1004 fprintf (stderr, "Expected stop.\n");
1005 event_child->stop_expected = 0;
54a0b537
PA
1006 linux_resume_one_lwp (&event_child->head,
1007 event_child->stepping, 0, NULL);
89be2091
DJ
1008 continue;
1009 }
1010
bd99dc85
PA
1011 if (WIFSTOPPED (*wstat) && WSTOPSIG (*wstat) == SIGTRAP
1012 && *wstat >> 16 != 0)
24a09b5f 1013 {
bd99dc85 1014 handle_extended_wait (event_child, *wstat);
24a09b5f
DJ
1015 continue;
1016 }
1017
89be2091
DJ
1018 /* If GDB is not interested in this signal, don't stop other
1019 threads, and don't report it to GDB. Just resume the
1020 inferior right away. We do this for threading-related
69f223ed
DJ
1021 signals as well as any that GDB specifically requested we
1022 ignore. But never ignore SIGSTOP if we sent it ourselves,
1023 and do not ignore signals when stepping - they may require
1024 special handling to skip the signal handler. */
89be2091
DJ
1025 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
1026 thread library? */
bd99dc85 1027 if (WIFSTOPPED (*wstat)
69f223ed 1028 && !event_child->stepping
24a09b5f
DJ
1029 && (
1030#ifdef USE_THREAD_DB
95954743 1031 (current_process ()->private->thread_db_active
bd99dc85
PA
1032 && (WSTOPSIG (*wstat) == __SIGRTMIN
1033 || WSTOPSIG (*wstat) == __SIGRTMIN + 1))
24a09b5f
DJ
1034 ||
1035#endif
bd99dc85
PA
1036 (pass_signals[target_signal_from_host (WSTOPSIG (*wstat))]
1037 && (WSTOPSIG (*wstat) != SIGSTOP || !stopping_threads))))
89be2091
DJ
1038 {
1039 siginfo_t info, *info_p;
1040
1041 if (debug_threads)
24a09b5f 1042 fprintf (stderr, "Ignored signal %d for LWP %ld.\n",
bd99dc85 1043 WSTOPSIG (*wstat), lwpid_of (event_child));
89be2091 1044
bd99dc85 1045 if (ptrace (PTRACE_GETSIGINFO, lwpid_of (event_child), 0, &info) == 0)
89be2091
DJ
1046 info_p = &info;
1047 else
1048 info_p = NULL;
54a0b537
PA
1049 linux_resume_one_lwp (&event_child->head,
1050 event_child->stepping,
bd99dc85 1051 WSTOPSIG (*wstat), info_p);
89be2091 1052 continue;
0d62e5e8 1053 }
611cb4a5 1054
0d62e5e8
DJ
1055 /* If this event was not handled above, and is not a SIGTRAP, report
1056 it. */
bd99dc85
PA
1057 if (!WIFSTOPPED (*wstat) || WSTOPSIG (*wstat) != SIGTRAP)
1058 return lwpid_of (event_child);
611cb4a5 1059
0d62e5e8
DJ
1060 /* If this target does not support breakpoints, we simply report the
1061 SIGTRAP; it's of no concern to us. */
1062 if (the_low_target.get_pc == NULL)
bd99dc85 1063 return lwpid_of (event_child);
0d62e5e8
DJ
1064
1065 stop_pc = get_stop_pc ();
1066
1067 /* bp_reinsert will only be set if we were single-stepping.
1068 Notice that we will resume the process after hitting
1069 a gdbserver breakpoint; single-stepping to/over one
1070 is not supported (yet). */
1071 if (event_child->bp_reinsert != 0)
1072 {
1073 if (debug_threads)
1074 fprintf (stderr, "Reinserted breakpoint.\n");
1075 reinsert_breakpoint (event_child->bp_reinsert);
1076 event_child->bp_reinsert = 0;
1077
1078 /* Clear the single-stepping flag and SIGTRAP as we resume. */
54a0b537 1079 linux_resume_one_lwp (&event_child->head, 0, 0, NULL);
0d62e5e8
DJ
1080 continue;
1081 }
1082
b65d95c5 1083 bp_status = check_breakpoints (stop_pc);
0d62e5e8 1084
b65d95c5 1085 if (bp_status != 0)
0d62e5e8 1086 {
b65d95c5
DJ
1087 if (debug_threads)
1088 fprintf (stderr, "Hit a gdbserver breakpoint.\n");
1089
0d62e5e8 1090 /* We hit one of our own breakpoints. We mark it as a pending
e5379b03 1091 breakpoint, so that check_removed_breakpoint () will do the PC
0d62e5e8
DJ
1092 adjustment for us at the appropriate time. */
1093 event_child->pending_is_breakpoint = 1;
1094 event_child->pending_stop_pc = stop_pc;
1095
b65d95c5 1096 /* We may need to put the breakpoint back. We continue in the event
0d62e5e8
DJ
1097 loop instead of simply replacing the breakpoint right away,
1098 in order to not lose signals sent to the thread that hit the
1099 breakpoint. Unfortunately this increases the window where another
1100 thread could sneak past the removed breakpoint. For the current
1101 use of server-side breakpoints (thread creation) this is
1102 acceptable; but it needs to be considered before this breakpoint
1103 mechanism can be used in more general ways. For some breakpoints
1104 it may be necessary to stop all other threads, but that should
1105 be avoided where possible.
1106
1107 If breakpoint_reinsert_addr is NULL, that means that we can
1108 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
1109 mark it for reinsertion, and single-step.
1110
1111 Otherwise, call the target function to figure out where we need
1112 our temporary breakpoint, create it, and continue executing this
1113 process. */
bd99dc85
PA
1114
1115 /* NOTE: we're lifting breakpoints in non-stop mode. This
1116 is currently only used for thread event breakpoints, so
1117 it isn't that bad as long as we have PTRACE_EVENT_CLONE
1118 events. */
b65d95c5
DJ
1119 if (bp_status == 2)
1120 /* No need to reinsert. */
54a0b537 1121 linux_resume_one_lwp (&event_child->head, 0, 0, NULL);
b65d95c5 1122 else if (the_low_target.breakpoint_reinsert_addr == NULL)
0d62e5e8
DJ
1123 {
1124 event_child->bp_reinsert = stop_pc;
1125 uninsert_breakpoint (stop_pc);
54a0b537 1126 linux_resume_one_lwp (&event_child->head, 1, 0, NULL);
0d62e5e8
DJ
1127 }
1128 else
1129 {
1130 reinsert_breakpoint_by_bp
1131 (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
54a0b537 1132 linux_resume_one_lwp (&event_child->head, 0, 0, NULL);
611cb4a5 1133 }
0d62e5e8
DJ
1134
1135 continue;
1136 }
1137
b65d95c5
DJ
1138 if (debug_threads)
1139 fprintf (stderr, "Hit a non-gdbserver breakpoint.\n");
1140
0d62e5e8 1141 /* If we were single-stepping, we definitely want to report the
c35fafde
PA
1142 SIGTRAP. Although the single-step operation has completed,
1143 do not clear clear the stepping flag yet; we need to check it
1144 in wait_for_sigstop. */
0d62e5e8 1145 if (event_child->stepping)
bd99dc85 1146 return lwpid_of (event_child);
0d62e5e8
DJ
1147
1148 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
1149 Check if it is a breakpoint, and if so mark the process information
1150 accordingly. This will handle both the necessary fiddling with the
1151 PC on decr_pc_after_break targets and suppressing extra threads
1152 hitting a breakpoint if two hit it at once and then GDB removes it
1153 after the first is reported. Arguably it would be better to report
1154 multiple threads hitting breakpoints simultaneously, but the current
1155 remote protocol does not allow this. */
1156 if ((*the_low_target.breakpoint_at) (stop_pc))
1157 {
1158 event_child->pending_is_breakpoint = 1;
1159 event_child->pending_stop_pc = stop_pc;
611cb4a5
DJ
1160 }
1161
bd99dc85 1162 return lwpid_of (event_child);
611cb4a5 1163 }
0d62e5e8 1164
611cb4a5
DJ
1165 /* NOTREACHED */
1166 return 0;
1167}
1168
95954743
PA
1169static int
1170linux_wait_for_event (ptid_t ptid, int *wstat, int options)
1171{
1172 ptid_t wait_ptid;
1173
1174 if (ptid_is_pid (ptid))
1175 {
1176 /* A request to wait for a specific tgid. This is not possible
1177 with waitpid, so instead, we wait for any child, and leave
1178 children we're not interested in right now with a pending
1179 status to report later. */
1180 wait_ptid = minus_one_ptid;
1181 }
1182 else
1183 wait_ptid = ptid;
1184
1185 while (1)
1186 {
1187 int event_pid;
1188
1189 event_pid = linux_wait_for_event_1 (wait_ptid, wstat, options);
1190
1191 if (event_pid > 0
1192 && ptid_is_pid (ptid) && ptid_get_pid (ptid) != event_pid)
1193 {
1194 struct lwp_info *event_child = find_lwp_pid (pid_to_ptid (event_pid));
1195
1196 if (! WIFSTOPPED (*wstat))
1197 mark_lwp_dead (event_child, *wstat);
1198 else
1199 {
1200 event_child->status_pending_p = 1;
1201 event_child->status_pending = *wstat;
1202 }
1203 }
1204 else
1205 return event_pid;
1206 }
1207}
1208
0d62e5e8 1209/* Wait for process, returns status. */
da6d8c04 1210
95954743
PA
1211static ptid_t
1212linux_wait_1 (ptid_t ptid,
1213 struct target_waitstatus *ourstatus, int target_options)
da6d8c04 1214{
e5f1222d 1215 int w;
bd99dc85
PA
1216 struct thread_info *thread = NULL;
1217 struct lwp_info *lwp = NULL;
1218 int options;
bd99dc85
PA
1219 int pid;
1220
1221 /* Translate generic target options into linux options. */
1222 options = __WALL;
1223 if (target_options & TARGET_WNOHANG)
1224 options |= WNOHANG;
0d62e5e8
DJ
1225
1226retry:
bd99dc85
PA
1227 ourstatus->kind = TARGET_WAITKIND_IGNORE;
1228
0d62e5e8
DJ
1229 /* If we were only supposed to resume one thread, only wait for
1230 that thread - if it's still alive. If it died, however - which
1231 can happen if we're coming from the thread death case below -
1232 then we need to make sure we restart the other threads. We could
1233 pick a thread at random or restart all; restarting all is less
1234 arbitrary. */
95954743
PA
1235 if (!non_stop
1236 && !ptid_equal (cont_thread, null_ptid)
1237 && !ptid_equal (cont_thread, minus_one_ptid))
0d62e5e8 1238 {
bd99dc85
PA
1239 thread = (struct thread_info *) find_inferior_id (&all_threads,
1240 cont_thread);
0d62e5e8
DJ
1241
1242 /* No stepping, no signal - unless one is pending already, of course. */
bd99dc85 1243 if (thread == NULL)
64386c31
DJ
1244 {
1245 struct thread_resume resume_info;
95954743 1246 resume_info.thread = minus_one_ptid;
bd99dc85
PA
1247 resume_info.kind = resume_continue;
1248 resume_info.sig = 0;
2bd7c093 1249 linux_resume (&resume_info, 1);
64386c31 1250 }
bd99dc85 1251 else
95954743 1252 ptid = cont_thread;
0d62e5e8 1253 }
da6d8c04 1254
95954743 1255 pid = linux_wait_for_event (ptid, &w, options);
bd99dc85 1256 if (pid == 0) /* only if TARGET_WNOHANG */
95954743 1257 return null_ptid;
bd99dc85
PA
1258
1259 lwp = get_thread_lwp (current_inferior);
da6d8c04 1260
24a09b5f
DJ
1261 if (must_set_ptrace_flags)
1262 {
bd99dc85 1263 ptrace (PTRACE_SETOPTIONS, lwpid_of (lwp), 0, PTRACE_O_TRACECLONE);
24a09b5f
DJ
1264 must_set_ptrace_flags = 0;
1265 }
0d62e5e8
DJ
1266 /* If we are waiting for a particular child, and it exited,
1267 linux_wait_for_event will return its exit status. Similarly if
1268 the last child exited. If this is not the last child, however,
1269 do not report it as exited until there is a 'thread exited' response
1270 available in the remote protocol. Instead, just wait for another event.
1271 This should be safe, because if the thread crashed we will already
1272 have reported the termination signal to GDB; that should stop any
1273 in-progress stepping operations, etc.
1274
1275 Report the exit status of the last thread to exit. This matches
1276 LinuxThreads' behavior. */
1277
95954743 1278 if (last_thread_of_process_p (current_inferior))
da6d8c04 1279 {
bd99dc85 1280 if (WIFEXITED (w) || WIFSIGNALED (w))
0d62e5e8 1281 {
95954743
PA
1282 int pid = pid_of (lwp);
1283 struct process_info *process = find_process_pid (pid);
5b1c542e 1284
bd99dc85 1285 delete_lwp (lwp);
5091eb23 1286 linux_remove_process (process);
5b1c542e 1287
bd99dc85 1288 current_inferior = NULL;
5b1c542e 1289
bd99dc85
PA
1290 if (WIFEXITED (w))
1291 {
1292 ourstatus->kind = TARGET_WAITKIND_EXITED;
1293 ourstatus->value.integer = WEXITSTATUS (w);
1294
1295 if (debug_threads)
1296 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
1297 }
1298 else
1299 {
1300 ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
1301 ourstatus->value.sig = target_signal_from_host (WTERMSIG (w));
1302
1303 if (debug_threads)
1304 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
1305
1306 }
5b1c542e 1307
95954743 1308 return pid_to_ptid (pid);
0d62e5e8 1309 }
da6d8c04 1310 }
0d62e5e8 1311 else
da6d8c04 1312 {
0d62e5e8
DJ
1313 if (!WIFSTOPPED (w))
1314 goto retry;
da6d8c04
DJ
1315 }
1316
bd99dc85
PA
1317 /* In all-stop, stop all threads. Be careful to only do this if
1318 we're about to report an event to GDB. */
1319 if (!non_stop)
1320 stop_all_lwps ();
1321
5b1c542e 1322 ourstatus->kind = TARGET_WAITKIND_STOPPED;
5b1c542e 1323
bd99dc85
PA
1324 if (lwp->suspended && WSTOPSIG (w) == SIGSTOP)
1325 {
1326 /* A thread that has been requested to stop by GDB with vCont;t,
1327 and it stopped cleanly, so report as SIG0. The use of
1328 SIGSTOP is an implementation detail. */
1329 ourstatus->value.sig = TARGET_SIGNAL_0;
1330 }
1331 else if (lwp->suspended && WSTOPSIG (w) != SIGSTOP)
1332 {
1333 /* A thread that has been requested to stop by GDB with vCont;t,
1334 but, it stopped for other reasons. Set stop_expected so the
1335 pending SIGSTOP is ignored and the LWP is resumed. */
1336 lwp->stop_expected = 1;
1337 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w));
1338 }
1339 else
1340 {
1341 ourstatus->value.sig = target_signal_from_host (WSTOPSIG (w));
1342 }
1343
1344 if (debug_threads)
95954743
PA
1345 fprintf (stderr, "linux_wait ret = %s, %d, %d\n",
1346 target_pid_to_str (lwp->head.id),
bd99dc85
PA
1347 ourstatus->kind,
1348 ourstatus->value.sig);
1349
95954743 1350 return lwp->head.id;
bd99dc85
PA
1351}
1352
1353/* Get rid of any pending event in the pipe. */
1354static void
1355async_file_flush (void)
1356{
1357 int ret;
1358 char buf;
1359
1360 do
1361 ret = read (linux_event_pipe[0], &buf, 1);
1362 while (ret >= 0 || (ret == -1 && errno == EINTR));
1363}
1364
1365/* Put something in the pipe, so the event loop wakes up. */
1366static void
1367async_file_mark (void)
1368{
1369 int ret;
1370
1371 async_file_flush ();
1372
1373 do
1374 ret = write (linux_event_pipe[1], "+", 1);
1375 while (ret == 0 || (ret == -1 && errno == EINTR));
1376
1377 /* Ignore EAGAIN. If the pipe is full, the event loop will already
1378 be awakened anyway. */
1379}
1380
95954743
PA
1381static ptid_t
1382linux_wait (ptid_t ptid,
1383 struct target_waitstatus *ourstatus, int target_options)
bd99dc85 1384{
95954743 1385 ptid_t event_ptid;
bd99dc85
PA
1386
1387 if (debug_threads)
95954743 1388 fprintf (stderr, "linux_wait: [%s]\n", target_pid_to_str (ptid));
bd99dc85
PA
1389
1390 /* Flush the async file first. */
1391 if (target_is_async_p ())
1392 async_file_flush ();
1393
95954743 1394 event_ptid = linux_wait_1 (ptid, ourstatus, target_options);
bd99dc85
PA
1395
1396 /* If at least one stop was reported, there may be more. A single
1397 SIGCHLD can signal more than one child stop. */
1398 if (target_is_async_p ()
1399 && (target_options & TARGET_WNOHANG) != 0
95954743 1400 && !ptid_equal (event_ptid, null_ptid))
bd99dc85
PA
1401 async_file_mark ();
1402
1403 return event_ptid;
da6d8c04
DJ
1404}
1405
fd500816
DJ
1406/* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
1407 thread groups are in use, we need to use tkill. */
1408
1409static int
a1928bad 1410kill_lwp (unsigned long lwpid, int signo)
fd500816
DJ
1411{
1412 static int tkill_failed;
1413
1414 errno = 0;
1415
1416#ifdef SYS_tkill
1417 if (!tkill_failed)
1418 {
1419 int ret = syscall (SYS_tkill, lwpid, signo);
1420 if (errno != ENOSYS)
1b3f6016 1421 return ret;
fd500816
DJ
1422 errno = 0;
1423 tkill_failed = 1;
1424 }
1425#endif
1426
1427 return kill (lwpid, signo);
1428}
1429
0d62e5e8
DJ
1430static void
1431send_sigstop (struct inferior_list_entry *entry)
1432{
54a0b537 1433 struct lwp_info *lwp = (struct lwp_info *) entry;
bd99dc85 1434 int pid;
0d62e5e8 1435
54a0b537 1436 if (lwp->stopped)
0d62e5e8
DJ
1437 return;
1438
bd99dc85
PA
1439 pid = lwpid_of (lwp);
1440
0d62e5e8
DJ
1441 /* If we already have a pending stop signal for this process, don't
1442 send another. */
54a0b537 1443 if (lwp->stop_expected)
0d62e5e8 1444 {
ae13219e 1445 if (debug_threads)
bd99dc85 1446 fprintf (stderr, "Have pending sigstop for lwp %d\n", pid);
ae13219e
DJ
1447
1448 /* We clear the stop_expected flag so that wait_for_sigstop
1449 will receive the SIGSTOP event (instead of silently resuming and
1450 waiting again). It'll be reset below. */
54a0b537 1451 lwp->stop_expected = 0;
0d62e5e8
DJ
1452 return;
1453 }
1454
1455 if (debug_threads)
bd99dc85 1456 fprintf (stderr, "Sending sigstop to lwp %d\n", pid);
0d62e5e8 1457
bd99dc85 1458 kill_lwp (pid, SIGSTOP);
0d62e5e8
DJ
1459}
1460
95954743
PA
1461static void
1462mark_lwp_dead (struct lwp_info *lwp, int wstat)
1463{
1464 /* It's dead, really. */
1465 lwp->dead = 1;
1466
1467 /* Store the exit status for later. */
1468 lwp->status_pending_p = 1;
1469 lwp->status_pending = wstat;
1470
1471 /* So that check_removed_breakpoint doesn't try to figure out if
1472 this is stopped at a breakpoint. */
1473 lwp->pending_is_breakpoint = 0;
1474
1475 /* Prevent trying to stop it. */
1476 lwp->stopped = 1;
1477
1478 /* No further stops are expected from a dead lwp. */
1479 lwp->stop_expected = 0;
1480}
1481
0d62e5e8
DJ
1482static void
1483wait_for_sigstop (struct inferior_list_entry *entry)
1484{
54a0b537 1485 struct lwp_info *lwp = (struct lwp_info *) entry;
bd99dc85 1486 struct thread_info *saved_inferior;
a1928bad 1487 int wstat;
95954743
PA
1488 ptid_t saved_tid;
1489 ptid_t ptid;
0d62e5e8 1490
54a0b537 1491 if (lwp->stopped)
0d62e5e8
DJ
1492 return;
1493
1494 saved_inferior = current_inferior;
bd99dc85
PA
1495 if (saved_inferior != NULL)
1496 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
1497 else
95954743 1498 saved_tid = null_ptid; /* avoid bogus unused warning */
bd99dc85 1499
95954743 1500 ptid = lwp->head.id;
bd99dc85
PA
1501
1502 linux_wait_for_event (ptid, &wstat, __WALL);
0d62e5e8
DJ
1503
1504 /* If we stopped with a non-SIGSTOP signal, save it for later
1505 and record the pending SIGSTOP. If the process exited, just
1506 return. */
1507 if (WIFSTOPPED (wstat)
1508 && WSTOPSIG (wstat) != SIGSTOP)
1509 {
1510 if (debug_threads)
24a09b5f 1511 fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n",
bd99dc85 1512 lwpid_of (lwp), wstat);
c35fafde
PA
1513
1514 /* Do not leave a pending single-step finish to be reported to
1515 the client. The client will give us a new action for this
1516 thread, possibly a continue request --- otherwise, the client
1517 would consider this pending SIGTRAP reported later a spurious
1518 signal. */
1519 if (WSTOPSIG (wstat) == SIGTRAP
1520 && lwp->stepping
1521 && !linux_stopped_by_watchpoint ())
1522 {
1523 if (debug_threads)
1524 fprintf (stderr, " single-step SIGTRAP ignored\n");
1525 }
1526 else
1527 {
1528 lwp->status_pending_p = 1;
1529 lwp->status_pending = wstat;
1530 }
54a0b537 1531 lwp->stop_expected = 1;
0d62e5e8 1532 }
95954743
PA
1533 else if (!WIFSTOPPED (wstat))
1534 {
1535 if (debug_threads)
1536 fprintf (stderr, "Process %ld exited while stopping LWPs\n",
1537 lwpid_of (lwp));
1538
1539 /* Leave this status pending for the next time we're able to
1540 report it. In the mean time, we'll report this lwp as dead
1541 to GDB, so GDB doesn't try to read registers and memory from
1542 it. */
1543 mark_lwp_dead (lwp, wstat);
1544 }
0d62e5e8 1545
bd99dc85 1546 if (saved_inferior == NULL || linux_thread_alive (saved_tid))
0d62e5e8
DJ
1547 current_inferior = saved_inferior;
1548 else
1549 {
1550 if (debug_threads)
1551 fprintf (stderr, "Previously current thread died.\n");
1552
bd99dc85
PA
1553 if (non_stop)
1554 {
1555 /* We can't change the current inferior behind GDB's back,
1556 otherwise, a subsequent command may apply to the wrong
1557 process. */
1558 current_inferior = NULL;
1559 }
1560 else
1561 {
1562 /* Set a valid thread as current. */
1563 set_desired_inferior (0);
1564 }
0d62e5e8
DJ
1565 }
1566}
1567
1568static void
54a0b537 1569stop_all_lwps (void)
0d62e5e8
DJ
1570{
1571 stopping_threads = 1;
54a0b537
PA
1572 for_each_inferior (&all_lwps, send_sigstop);
1573 for_each_inferior (&all_lwps, wait_for_sigstop);
0d62e5e8
DJ
1574 stopping_threads = 0;
1575}
1576
da6d8c04
DJ
1577/* Resume execution of the inferior process.
1578 If STEP is nonzero, single-step it.
1579 If SIGNAL is nonzero, give it that signal. */
1580
ce3a066d 1581static void
54a0b537
PA
1582linux_resume_one_lwp (struct inferior_list_entry *entry,
1583 int step, int signal, siginfo_t *info)
da6d8c04 1584{
54a0b537 1585 struct lwp_info *lwp = (struct lwp_info *) entry;
0d62e5e8
DJ
1586 struct thread_info *saved_inferior;
1587
54a0b537 1588 if (lwp->stopped == 0)
0d62e5e8
DJ
1589 return;
1590
1591 /* If we have pending signals or status, and a new signal, enqueue the
1592 signal. Also enqueue the signal if we are waiting to reinsert a
1593 breakpoint; it will be picked up again below. */
1594 if (signal != 0
54a0b537
PA
1595 && (lwp->status_pending_p || lwp->pending_signals != NULL
1596 || lwp->bp_reinsert != 0))
0d62e5e8
DJ
1597 {
1598 struct pending_signals *p_sig;
bca929d3 1599 p_sig = xmalloc (sizeof (*p_sig));
54a0b537 1600 p_sig->prev = lwp->pending_signals;
0d62e5e8 1601 p_sig->signal = signal;
32ca6d61
DJ
1602 if (info == NULL)
1603 memset (&p_sig->info, 0, sizeof (siginfo_t));
1604 else
1605 memcpy (&p_sig->info, info, sizeof (siginfo_t));
54a0b537 1606 lwp->pending_signals = p_sig;
0d62e5e8
DJ
1607 }
1608
54a0b537 1609 if (lwp->status_pending_p && !check_removed_breakpoint (lwp))
0d62e5e8
DJ
1610 return;
1611
1612 saved_inferior = current_inferior;
54a0b537 1613 current_inferior = get_lwp_thread (lwp);
0d62e5e8
DJ
1614
1615 if (debug_threads)
1b3f6016 1616 fprintf (stderr, "Resuming lwp %ld (%s, signal %d, stop %s)\n",
bd99dc85 1617 lwpid_of (lwp), step ? "step" : "continue", signal,
54a0b537 1618 lwp->stop_expected ? "expected" : "not expected");
0d62e5e8
DJ
1619
1620 /* This bit needs some thinking about. If we get a signal that
1621 we must report while a single-step reinsert is still pending,
1622 we often end up resuming the thread. It might be better to
1623 (ew) allow a stack of pending events; then we could be sure that
1624 the reinsert happened right away and not lose any signals.
1625
1626 Making this stack would also shrink the window in which breakpoints are
54a0b537 1627 uninserted (see comment in linux_wait_for_lwp) but not enough for
0d62e5e8
DJ
1628 complete correctness, so it won't solve that problem. It may be
1629 worthwhile just to solve this one, however. */
54a0b537 1630 if (lwp->bp_reinsert != 0)
0d62e5e8
DJ
1631 {
1632 if (debug_threads)
54a0b537 1633 fprintf (stderr, " pending reinsert at %08lx", (long)lwp->bp_reinsert);
0d62e5e8
DJ
1634 if (step == 0)
1635 fprintf (stderr, "BAD - reinserting but not stepping.\n");
1636 step = 1;
1637
1638 /* Postpone any pending signal. It was enqueued above. */
1639 signal = 0;
1640 }
1641
54a0b537 1642 check_removed_breakpoint (lwp);
0d62e5e8 1643
aa691b87 1644 if (debug_threads && the_low_target.get_pc != NULL)
0d62e5e8
DJ
1645 {
1646 fprintf (stderr, " ");
52fb6437 1647 (*the_low_target.get_pc) ();
0d62e5e8
DJ
1648 }
1649
1650 /* If we have pending signals, consume one unless we are trying to reinsert
1651 a breakpoint. */
54a0b537 1652 if (lwp->pending_signals != NULL && lwp->bp_reinsert == 0)
0d62e5e8
DJ
1653 {
1654 struct pending_signals **p_sig;
1655
54a0b537 1656 p_sig = &lwp->pending_signals;
0d62e5e8
DJ
1657 while ((*p_sig)->prev != NULL)
1658 p_sig = &(*p_sig)->prev;
1659
1660 signal = (*p_sig)->signal;
32ca6d61 1661 if ((*p_sig)->info.si_signo != 0)
bd99dc85 1662 ptrace (PTRACE_SETSIGINFO, lwpid_of (lwp), 0, &(*p_sig)->info);
32ca6d61 1663
0d62e5e8
DJ
1664 free (*p_sig);
1665 *p_sig = NULL;
1666 }
1667
1668 regcache_invalidate_one ((struct inferior_list_entry *)
54a0b537 1669 get_lwp_thread (lwp));
da6d8c04 1670 errno = 0;
54a0b537
PA
1671 lwp->stopped = 0;
1672 lwp->stepping = step;
bd99dc85 1673 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, lwpid_of (lwp), 0, signal);
0d62e5e8
DJ
1674
1675 current_inferior = saved_inferior;
da6d8c04 1676 if (errno)
3221518c
UW
1677 {
1678 /* ESRCH from ptrace either means that the thread was already
1679 running (an error) or that it is gone (a race condition). If
1680 it's gone, we will get a notification the next time we wait,
1681 so we can ignore the error. We could differentiate these
1682 two, but it's tricky without waiting; the thread still exists
1683 as a zombie, so sending it signal 0 would succeed. So just
1684 ignore ESRCH. */
1685 if (errno == ESRCH)
1686 return;
1687
1688 perror_with_name ("ptrace");
1689 }
da6d8c04
DJ
1690}
1691
2bd7c093
PA
1692struct thread_resume_array
1693{
1694 struct thread_resume *resume;
1695 size_t n;
1696};
64386c31
DJ
1697
1698/* This function is called once per thread. We look up the thread
5544ad89
DJ
1699 in RESUME_PTR, and mark the thread with a pointer to the appropriate
1700 resume request.
1701
1702 This algorithm is O(threads * resume elements), but resume elements
1703 is small (and will remain small at least until GDB supports thread
1704 suspension). */
2bd7c093
PA
1705static int
1706linux_set_resume_request (struct inferior_list_entry *entry, void *arg)
0d62e5e8 1707{
54a0b537 1708 struct lwp_info *lwp;
64386c31 1709 struct thread_info *thread;
5544ad89 1710 int ndx;
2bd7c093 1711 struct thread_resume_array *r;
64386c31
DJ
1712
1713 thread = (struct thread_info *) entry;
54a0b537 1714 lwp = get_thread_lwp (thread);
2bd7c093 1715 r = arg;
64386c31 1716
2bd7c093 1717 for (ndx = 0; ndx < r->n; ndx++)
95954743
PA
1718 {
1719 ptid_t ptid = r->resume[ndx].thread;
1720 if (ptid_equal (ptid, minus_one_ptid)
1721 || ptid_equal (ptid, entry->id)
1722 || (ptid_is_pid (ptid)
1723 && (ptid_get_pid (ptid) == pid_of (lwp)))
1724 || (ptid_get_lwp (ptid) == -1
1725 && (ptid_get_pid (ptid) == pid_of (lwp))))
1726 {
1727 lwp->resume = &r->resume[ndx];
1728 return 0;
1729 }
1730 }
2bd7c093
PA
1731
1732 /* No resume action for this thread. */
1733 lwp->resume = NULL;
64386c31 1734
2bd7c093 1735 return 0;
5544ad89
DJ
1736}
1737
5544ad89 1738
bd99dc85
PA
1739/* Set *FLAG_P if this lwp has an interesting status pending. */
1740static int
1741resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
5544ad89 1742{
bd99dc85 1743 struct lwp_info *lwp = (struct lwp_info *) entry;
5544ad89 1744
bd99dc85
PA
1745 /* LWPs which will not be resumed are not interesting, because
1746 we might not wait for them next time through linux_wait. */
2bd7c093 1747 if (lwp->resume == NULL)
bd99dc85 1748 return 0;
64386c31 1749
bd99dc85
PA
1750 /* If this thread has a removed breakpoint, we won't have any
1751 events to report later, so check now. check_removed_breakpoint
1752 may clear status_pending_p. We avoid calling check_removed_breakpoint
1753 for any thread that we are not otherwise going to resume - this
1754 lets us preserve stopped status when two threads hit a breakpoint.
1755 GDB removes the breakpoint to single-step a particular thread
1756 past it, then re-inserts it and resumes all threads. We want
1757 to report the second thread without resuming it in the interim. */
1758 if (lwp->status_pending_p)
1759 check_removed_breakpoint (lwp);
5544ad89 1760
bd99dc85
PA
1761 if (lwp->status_pending_p)
1762 * (int *) flag_p = 1;
c6ecbae5 1763
bd99dc85 1764 return 0;
5544ad89
DJ
1765}
1766
1767/* This function is called once per thread. We check the thread's resume
1768 request, which will tell us whether to resume, step, or leave the thread
bd99dc85 1769 stopped; and what signal, if any, it should be sent.
5544ad89 1770
bd99dc85
PA
1771 For threads which we aren't explicitly told otherwise, we preserve
1772 the stepping flag; this is used for stepping over gdbserver-placed
1773 breakpoints.
1774
1775 If pending_flags was set in any thread, we queue any needed
1776 signals, since we won't actually resume. We already have a pending
1777 event to report, so we don't need to preserve any step requests;
1778 they should be re-issued if necessary. */
1779
1780static int
1781linux_resume_one_thread (struct inferior_list_entry *entry, void *arg)
5544ad89 1782{
54a0b537 1783 struct lwp_info *lwp;
5544ad89 1784 struct thread_info *thread;
bd99dc85
PA
1785 int step;
1786 int pending_flag = * (int *) arg;
5544ad89
DJ
1787
1788 thread = (struct thread_info *) entry;
54a0b537 1789 lwp = get_thread_lwp (thread);
5544ad89 1790
2bd7c093 1791 if (lwp->resume == NULL)
bd99dc85 1792 return 0;
5544ad89 1793
bd99dc85 1794 if (lwp->resume->kind == resume_stop)
5544ad89 1795 {
bd99dc85
PA
1796 if (debug_threads)
1797 fprintf (stderr, "suspending LWP %ld\n", lwpid_of (lwp));
1798
1799 if (!lwp->stopped)
1800 {
1801 if (debug_threads)
95954743 1802 fprintf (stderr, "running -> suspending LWP %ld\n", lwpid_of (lwp));
bd99dc85
PA
1803
1804 lwp->suspended = 1;
1805 send_sigstop (&lwp->head);
1806 }
1807 else
1808 {
1809 if (debug_threads)
1810 {
1811 if (lwp->suspended)
1812 fprintf (stderr, "already stopped/suspended LWP %ld\n",
1813 lwpid_of (lwp));
1814 else
1815 fprintf (stderr, "already stopped/not suspended LWP %ld\n",
1816 lwpid_of (lwp));
1817 }
32ca6d61 1818
bd99dc85
PA
1819 /* Make sure we leave the LWP suspended, so we don't try to
1820 resume it without GDB telling us to. FIXME: The LWP may
1821 have been stopped in an internal event that was not meant
1822 to be notified back to GDB (e.g., gdbserver breakpoint),
1823 so we should be reporting a stop event in that case
1824 too. */
1825 lwp->suspended = 1;
1826 }
32ca6d61 1827
bd99dc85
PA
1828 /* For stop requests, we're done. */
1829 lwp->resume = NULL;
1830 return 0;
5544ad89 1831 }
bd99dc85
PA
1832 else
1833 lwp->suspended = 0;
5544ad89 1834
bd99dc85
PA
1835 /* If this thread which is about to be resumed has a pending status,
1836 then don't resume any threads - we can just report the pending
1837 status. Make sure to queue any signals that would otherwise be
1838 sent. In all-stop mode, we do this decision based on if *any*
1839 thread has a pending status. */
1840 if (non_stop)
1841 resume_status_pending_p (&lwp->head, &pending_flag);
5544ad89 1842
bd99dc85
PA
1843 if (!pending_flag)
1844 {
1845 if (debug_threads)
1846 fprintf (stderr, "resuming LWP %ld\n", lwpid_of (lwp));
5544ad89 1847
95954743 1848 if (ptid_equal (lwp->resume->thread, minus_one_ptid)
bd99dc85
PA
1849 && lwp->stepping
1850 && lwp->pending_is_breakpoint)
1851 step = 1;
1852 else
1853 step = (lwp->resume->kind == resume_step);
5544ad89 1854
bd99dc85
PA
1855 linux_resume_one_lwp (&lwp->head, step, lwp->resume->sig, NULL);
1856 }
1857 else
1858 {
1859 if (debug_threads)
1860 fprintf (stderr, "leaving LWP %ld stopped\n", lwpid_of (lwp));
5544ad89 1861
bd99dc85
PA
1862 /* If we have a new signal, enqueue the signal. */
1863 if (lwp->resume->sig != 0)
1864 {
1865 struct pending_signals *p_sig;
1866 p_sig = xmalloc (sizeof (*p_sig));
1867 p_sig->prev = lwp->pending_signals;
1868 p_sig->signal = lwp->resume->sig;
1869 memset (&p_sig->info, 0, sizeof (siginfo_t));
1870
1871 /* If this is the same signal we were previously stopped by,
1872 make sure to queue its siginfo. We can ignore the return
1873 value of ptrace; if it fails, we'll skip
1874 PTRACE_SETSIGINFO. */
1875 if (WIFSTOPPED (lwp->last_status)
1876 && WSTOPSIG (lwp->last_status) == lwp->resume->sig)
1877 ptrace (PTRACE_GETSIGINFO, lwpid_of (lwp), 0, &p_sig->info);
1878
1879 lwp->pending_signals = p_sig;
1880 }
1881 }
5544ad89 1882
bd99dc85 1883 lwp->resume = NULL;
5544ad89 1884 return 0;
0d62e5e8
DJ
1885}
1886
1887static void
2bd7c093 1888linux_resume (struct thread_resume *resume_info, size_t n)
0d62e5e8 1889{
5544ad89 1890 int pending_flag;
2bd7c093 1891 struct thread_resume_array array = { resume_info, n };
c6ecbae5 1892
2bd7c093 1893 find_inferior (&all_threads, linux_set_resume_request, &array);
5544ad89
DJ
1894
1895 /* If there is a thread which would otherwise be resumed, which
1896 has a pending status, then don't resume any threads - we can just
1897 report the pending status. Make sure to queue any signals
bd99dc85
PA
1898 that would otherwise be sent. In non-stop mode, we'll apply this
1899 logic to each thread individually. */
5544ad89 1900 pending_flag = 0;
bd99dc85
PA
1901 if (!non_stop)
1902 find_inferior (&all_lwps, resume_status_pending_p, &pending_flag);
5544ad89
DJ
1903
1904 if (debug_threads)
1905 {
1906 if (pending_flag)
1907 fprintf (stderr, "Not resuming, pending status\n");
1908 else
1909 fprintf (stderr, "Resuming, no pending status\n");
1910 }
1911
bd99dc85 1912 find_inferior (&all_threads, linux_resume_one_thread, &pending_flag);
0d62e5e8
DJ
1913}
1914
1915#ifdef HAVE_LINUX_USRREGS
da6d8c04
DJ
1916
1917int
0a30fbc4 1918register_addr (int regnum)
da6d8c04
DJ
1919{
1920 int addr;
1921
2ec06d2e 1922 if (regnum < 0 || regnum >= the_low_target.num_regs)
da6d8c04
DJ
1923 error ("Invalid register number %d.", regnum);
1924
2ec06d2e 1925 addr = the_low_target.regmap[regnum];
da6d8c04
DJ
1926
1927 return addr;
1928}
1929
58caa3dc 1930/* Fetch one register. */
da6d8c04
DJ
1931static void
1932fetch_register (int regno)
1933{
1934 CORE_ADDR regaddr;
48d93c75 1935 int i, size;
0d62e5e8 1936 char *buf;
95954743 1937 int pid;
da6d8c04 1938
2ec06d2e 1939 if (regno >= the_low_target.num_regs)
0a30fbc4 1940 return;
2ec06d2e 1941 if ((*the_low_target.cannot_fetch_register) (regno))
0a30fbc4 1942 return;
da6d8c04 1943
0a30fbc4
DJ
1944 regaddr = register_addr (regno);
1945 if (regaddr == -1)
1946 return;
95954743
PA
1947
1948 pid = lwpid_of (get_thread_lwp (current_inferior));
1b3f6016
PA
1949 size = ((register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
1950 & - sizeof (PTRACE_XFER_TYPE));
48d93c75
UW
1951 buf = alloca (size);
1952 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04
DJ
1953 {
1954 errno = 0;
0d62e5e8 1955 *(PTRACE_XFER_TYPE *) (buf + i) =
95954743 1956 ptrace (PTRACE_PEEKUSER, pid, (PTRACE_ARG3_TYPE) regaddr, 0);
da6d8c04
DJ
1957 regaddr += sizeof (PTRACE_XFER_TYPE);
1958 if (errno != 0)
1959 {
1960 /* Warning, not error, in case we are attached; sometimes the
1961 kernel doesn't let us at the registers. */
1962 char *err = strerror (errno);
1963 char *msg = alloca (strlen (err) + 128);
1964 sprintf (msg, "reading register %d: %s", regno, err);
1965 error (msg);
1966 goto error_exit;
1967 }
1968 }
ee1a7ae4
UW
1969
1970 if (the_low_target.supply_ptrace_register)
1971 the_low_target.supply_ptrace_register (regno, buf);
5a1f5858
DJ
1972 else
1973 supply_register (regno, buf);
0d62e5e8 1974
da6d8c04
DJ
1975error_exit:;
1976}
1977
1978/* Fetch all registers, or just one, from the child process. */
58caa3dc
DJ
1979static void
1980usr_fetch_inferior_registers (int regno)
da6d8c04
DJ
1981{
1982 if (regno == -1 || regno == 0)
2ec06d2e 1983 for (regno = 0; regno < the_low_target.num_regs; regno++)
da6d8c04
DJ
1984 fetch_register (regno);
1985 else
1986 fetch_register (regno);
1987}
1988
1989/* Store our register values back into the inferior.
1990 If REGNO is -1, do this for all registers.
1991 Otherwise, REGNO specifies which register (so we can save time). */
58caa3dc
DJ
1992static void
1993usr_store_inferior_registers (int regno)
da6d8c04
DJ
1994{
1995 CORE_ADDR regaddr;
48d93c75 1996 int i, size;
0d62e5e8 1997 char *buf;
55ac2b99 1998 int pid;
da6d8c04
DJ
1999
2000 if (regno >= 0)
2001 {
2ec06d2e 2002 if (regno >= the_low_target.num_regs)
0a30fbc4
DJ
2003 return;
2004
bc1e36ca 2005 if ((*the_low_target.cannot_store_register) (regno) == 1)
0a30fbc4
DJ
2006 return;
2007
2008 regaddr = register_addr (regno);
2009 if (regaddr == -1)
da6d8c04 2010 return;
da6d8c04 2011 errno = 0;
48d93c75
UW
2012 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
2013 & - sizeof (PTRACE_XFER_TYPE);
2014 buf = alloca (size);
2015 memset (buf, 0, size);
ee1a7ae4
UW
2016
2017 if (the_low_target.collect_ptrace_register)
2018 the_low_target.collect_ptrace_register (regno, buf);
5a1f5858
DJ
2019 else
2020 collect_register (regno, buf);
ee1a7ae4 2021
95954743 2022 pid = lwpid_of (get_thread_lwp (current_inferior));
48d93c75 2023 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04 2024 {
0a30fbc4 2025 errno = 0;
95954743 2026 ptrace (PTRACE_POKEUSER, pid, (PTRACE_ARG3_TYPE) regaddr,
2ff29de4 2027 *(PTRACE_XFER_TYPE *) (buf + i));
da6d8c04
DJ
2028 if (errno != 0)
2029 {
1b3f6016
PA
2030 /* At this point, ESRCH should mean the process is
2031 already gone, in which case we simply ignore attempts
2032 to change its registers. See also the related
2033 comment in linux_resume_one_lwp. */
3221518c
UW
2034 if (errno == ESRCH)
2035 return;
2036
bc1e36ca
DJ
2037 if ((*the_low_target.cannot_store_register) (regno) == 0)
2038 {
2039 char *err = strerror (errno);
2040 char *msg = alloca (strlen (err) + 128);
2041 sprintf (msg, "writing register %d: %s",
2042 regno, err);
2043 error (msg);
2044 return;
2045 }
da6d8c04 2046 }
2ff29de4 2047 regaddr += sizeof (PTRACE_XFER_TYPE);
da6d8c04 2048 }
da6d8c04
DJ
2049 }
2050 else
2ec06d2e 2051 for (regno = 0; regno < the_low_target.num_regs; regno++)
0d62e5e8 2052 usr_store_inferior_registers (regno);
da6d8c04 2053}
58caa3dc
DJ
2054#endif /* HAVE_LINUX_USRREGS */
2055
2056
2057
2058#ifdef HAVE_LINUX_REGSETS
2059
2060static int
0d62e5e8 2061regsets_fetch_inferior_registers ()
58caa3dc
DJ
2062{
2063 struct regset_info *regset;
e9d25b98 2064 int saw_general_regs = 0;
95954743 2065 int pid;
58caa3dc
DJ
2066
2067 regset = target_regsets;
2068
95954743 2069 pid = lwpid_of (get_thread_lwp (current_inferior));
58caa3dc
DJ
2070 while (regset->size >= 0)
2071 {
2072 void *buf;
2073 int res;
2074
52fa2412 2075 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
58caa3dc
DJ
2076 {
2077 regset ++;
2078 continue;
2079 }
2080
bca929d3 2081 buf = xmalloc (regset->size);
dfb64f85 2082#ifndef __sparc__
95954743 2083 res = ptrace (regset->get_request, pid, 0, buf);
dfb64f85 2084#else
95954743 2085 res = ptrace (regset->get_request, pid, buf, 0);
dfb64f85 2086#endif
58caa3dc
DJ
2087 if (res < 0)
2088 {
2089 if (errno == EIO)
2090 {
52fa2412
UW
2091 /* If we get EIO on a regset, do not try it again for
2092 this process. */
2093 disabled_regsets[regset - target_regsets] = 1;
2094 continue;
58caa3dc
DJ
2095 }
2096 else
2097 {
0d62e5e8 2098 char s[256];
95954743
PA
2099 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
2100 pid);
0d62e5e8 2101 perror (s);
58caa3dc
DJ
2102 }
2103 }
e9d25b98
DJ
2104 else if (regset->type == GENERAL_REGS)
2105 saw_general_regs = 1;
58caa3dc
DJ
2106 regset->store_function (buf);
2107 regset ++;
2108 }
e9d25b98
DJ
2109 if (saw_general_regs)
2110 return 0;
2111 else
2112 return 1;
58caa3dc
DJ
2113}
2114
2115static int
0d62e5e8 2116regsets_store_inferior_registers ()
58caa3dc
DJ
2117{
2118 struct regset_info *regset;
e9d25b98 2119 int saw_general_regs = 0;
95954743 2120 int pid;
58caa3dc
DJ
2121
2122 regset = target_regsets;
2123
95954743 2124 pid = lwpid_of (get_thread_lwp (current_inferior));
58caa3dc
DJ
2125 while (regset->size >= 0)
2126 {
2127 void *buf;
2128 int res;
2129
52fa2412 2130 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
58caa3dc
DJ
2131 {
2132 regset ++;
2133 continue;
2134 }
2135
bca929d3 2136 buf = xmalloc (regset->size);
545587ee
DJ
2137
2138 /* First fill the buffer with the current register set contents,
2139 in case there are any items in the kernel's regset that are
2140 not in gdbserver's regcache. */
dfb64f85 2141#ifndef __sparc__
95954743 2142 res = ptrace (regset->get_request, pid, 0, buf);
dfb64f85 2143#else
95954743 2144 res = ptrace (regset->get_request, pid, buf, 0);
dfb64f85 2145#endif
545587ee
DJ
2146
2147 if (res == 0)
2148 {
2149 /* Then overlay our cached registers on that. */
2150 regset->fill_function (buf);
2151
2152 /* Only now do we write the register set. */
dfb64f85 2153#ifndef __sparc__
95954743 2154 res = ptrace (regset->set_request, pid, 0, buf);
dfb64f85 2155#else
95954743 2156 res = ptrace (regset->set_request, pid, buf, 0);
dfb64f85 2157#endif
545587ee
DJ
2158 }
2159
58caa3dc
DJ
2160 if (res < 0)
2161 {
2162 if (errno == EIO)
2163 {
52fa2412
UW
2164 /* If we get EIO on a regset, do not try it again for
2165 this process. */
2166 disabled_regsets[regset - target_regsets] = 1;
2167 continue;
58caa3dc 2168 }
3221518c
UW
2169 else if (errno == ESRCH)
2170 {
1b3f6016
PA
2171 /* At this point, ESRCH should mean the process is
2172 already gone, in which case we simply ignore attempts
2173 to change its registers. See also the related
2174 comment in linux_resume_one_lwp. */
3221518c
UW
2175 return 0;
2176 }
58caa3dc
DJ
2177 else
2178 {
ce3a066d 2179 perror ("Warning: ptrace(regsets_store_inferior_registers)");
58caa3dc
DJ
2180 }
2181 }
e9d25b98
DJ
2182 else if (regset->type == GENERAL_REGS)
2183 saw_general_regs = 1;
58caa3dc 2184 regset ++;
09ec9b38 2185 free (buf);
58caa3dc 2186 }
e9d25b98
DJ
2187 if (saw_general_regs)
2188 return 0;
2189 else
2190 return 1;
ce3a066d 2191 return 0;
58caa3dc
DJ
2192}
2193
2194#endif /* HAVE_LINUX_REGSETS */
2195
2196
2197void
ce3a066d 2198linux_fetch_registers (int regno)
58caa3dc
DJ
2199{
2200#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
2201 if (regsets_fetch_inferior_registers () == 0)
2202 return;
58caa3dc
DJ
2203#endif
2204#ifdef HAVE_LINUX_USRREGS
2205 usr_fetch_inferior_registers (regno);
2206#endif
2207}
2208
2209void
ce3a066d 2210linux_store_registers (int regno)
58caa3dc
DJ
2211{
2212#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
2213 if (regsets_store_inferior_registers () == 0)
2214 return;
58caa3dc
DJ
2215#endif
2216#ifdef HAVE_LINUX_USRREGS
2217 usr_store_inferior_registers (regno);
2218#endif
2219}
2220
da6d8c04 2221
da6d8c04
DJ
2222/* Copy LEN bytes from inferior's memory starting at MEMADDR
2223 to debugger memory starting at MYADDR. */
2224
c3e735a6 2225static int
f450004a 2226linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
da6d8c04
DJ
2227{
2228 register int i;
2229 /* Round starting address down to longword boundary. */
2230 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
2231 /* Round ending address up; get number of longwords that makes. */
aa691b87
RM
2232 register int count
2233 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
da6d8c04
DJ
2234 / sizeof (PTRACE_XFER_TYPE);
2235 /* Allocate buffer of that many longwords. */
aa691b87 2236 register PTRACE_XFER_TYPE *buffer
da6d8c04 2237 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
fd462a61
DJ
2238 int fd;
2239 char filename[64];
95954743 2240 int pid = lwpid_of (get_thread_lwp (current_inferior));
fd462a61
DJ
2241
2242 /* Try using /proc. Don't bother for one word. */
2243 if (len >= 3 * sizeof (long))
2244 {
2245 /* We could keep this file open and cache it - possibly one per
2246 thread. That requires some juggling, but is even faster. */
95954743 2247 sprintf (filename, "/proc/%d/mem", pid);
fd462a61
DJ
2248 fd = open (filename, O_RDONLY | O_LARGEFILE);
2249 if (fd == -1)
2250 goto no_proc;
2251
2252 /* If pread64 is available, use it. It's faster if the kernel
2253 supports it (only one syscall), and it's 64-bit safe even on
2254 32-bit platforms (for instance, SPARC debugging a SPARC64
2255 application). */
2256#ifdef HAVE_PREAD64
2257 if (pread64 (fd, myaddr, len, memaddr) != len)
2258#else
2259 if (lseek (fd, memaddr, SEEK_SET) == -1 || read (fd, memaddr, len) != len)
2260#endif
2261 {
2262 close (fd);
2263 goto no_proc;
2264 }
2265
2266 close (fd);
2267 return 0;
2268 }
da6d8c04 2269
fd462a61 2270 no_proc:
da6d8c04
DJ
2271 /* Read all the longwords */
2272 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
2273 {
c3e735a6 2274 errno = 0;
95954743 2275 buffer[i] = ptrace (PTRACE_PEEKTEXT, pid, (PTRACE_ARG3_TYPE) addr, 0);
c3e735a6
DJ
2276 if (errno)
2277 return errno;
da6d8c04
DJ
2278 }
2279
2280 /* Copy appropriate bytes out of the buffer. */
1b3f6016
PA
2281 memcpy (myaddr,
2282 (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)),
2283 len);
c3e735a6
DJ
2284
2285 return 0;
da6d8c04
DJ
2286}
2287
2288/* Copy LEN bytes of data from debugger memory at MYADDR
2289 to inferior's memory at MEMADDR.
2290 On failure (cannot write the inferior)
2291 returns the value of errno. */
2292
ce3a066d 2293static int
f450004a 2294linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
da6d8c04
DJ
2295{
2296 register int i;
2297 /* Round starting address down to longword boundary. */
2298 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
2299 /* Round ending address up; get number of longwords that makes. */
2300 register int count
2301 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
2302 /* Allocate buffer of that many longwords. */
2303 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
95954743 2304 int pid = lwpid_of (get_thread_lwp (current_inferior));
da6d8c04 2305
0d62e5e8
DJ
2306 if (debug_threads)
2307 {
2308 fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
2309 }
2310
da6d8c04
DJ
2311 /* Fill start and end extra bytes of buffer with existing memory data. */
2312
95954743 2313 buffer[0] = ptrace (PTRACE_PEEKTEXT, pid, (PTRACE_ARG3_TYPE) addr, 0);
da6d8c04
DJ
2314
2315 if (count > 1)
2316 {
2317 buffer[count - 1]
95954743 2318 = ptrace (PTRACE_PEEKTEXT, pid,
d844cde6
DJ
2319 (PTRACE_ARG3_TYPE) (addr + (count - 1)
2320 * sizeof (PTRACE_XFER_TYPE)),
2321 0);
da6d8c04
DJ
2322 }
2323
2324 /* Copy data to be written over corresponding part of buffer */
2325
2326 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
2327
2328 /* Write the entire buffer. */
2329
2330 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
2331 {
2332 errno = 0;
95954743 2333 ptrace (PTRACE_POKETEXT, pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
da6d8c04
DJ
2334 if (errno)
2335 return errno;
2336 }
2337
2338 return 0;
2339}
2f2893d9 2340
24a09b5f
DJ
2341static int linux_supports_tracefork_flag;
2342
51c2684e 2343/* Helper functions for linux_test_for_tracefork, called via clone (). */
24a09b5f 2344
51c2684e
DJ
2345static int
2346linux_tracefork_grandchild (void *arg)
2347{
2348 _exit (0);
2349}
2350
7407e2de
AS
2351#define STACK_SIZE 4096
2352
51c2684e
DJ
2353static int
2354linux_tracefork_child (void *arg)
24a09b5f
DJ
2355{
2356 ptrace (PTRACE_TRACEME, 0, 0, 0);
2357 kill (getpid (), SIGSTOP);
7407e2de
AS
2358#ifdef __ia64__
2359 __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE,
2360 CLONE_VM | SIGCHLD, NULL);
2361#else
2362 clone (linux_tracefork_grandchild, arg + STACK_SIZE,
2363 CLONE_VM | SIGCHLD, NULL);
2364#endif
24a09b5f
DJ
2365 _exit (0);
2366}
2367
bd99dc85
PA
2368/* Wrapper function for waitpid which handles EINTR, and emulates
2369 __WALL for systems where that is not available. */
24a09b5f
DJ
2370
2371static int
2372my_waitpid (int pid, int *status, int flags)
2373{
bd99dc85
PA
2374 int ret, out_errno;
2375
2376 if (debug_threads)
2377 fprintf (stderr, "my_waitpid (%d, 0x%x)\n", pid, flags);
2378
2379 if (flags & __WALL)
24a09b5f 2380 {
bd99dc85
PA
2381 sigset_t block_mask, org_mask, wake_mask;
2382 int wnohang;
2383
2384 wnohang = (flags & WNOHANG) != 0;
2385 flags &= ~(__WALL | __WCLONE);
2386 flags |= WNOHANG;
2387
2388 /* Block all signals while here. This avoids knowing about
2389 LinuxThread's signals. */
2390 sigfillset (&block_mask);
2391 sigprocmask (SIG_BLOCK, &block_mask, &org_mask);
2392
2393 /* ... except during the sigsuspend below. */
2394 sigemptyset (&wake_mask);
2395
2396 while (1)
2397 {
2398 /* Since all signals are blocked, there's no need to check
2399 for EINTR here. */
2400 ret = waitpid (pid, status, flags);
2401 out_errno = errno;
2402
2403 if (ret == -1 && out_errno != ECHILD)
2404 break;
2405 else if (ret > 0)
2406 break;
2407
2408 if (flags & __WCLONE)
2409 {
2410 /* We've tried both flavors now. If WNOHANG is set,
2411 there's nothing else to do, just bail out. */
2412 if (wnohang)
2413 break;
2414
2415 if (debug_threads)
2416 fprintf (stderr, "blocking\n");
2417
2418 /* Block waiting for signals. */
2419 sigsuspend (&wake_mask);
2420 }
2421
2422 flags ^= __WCLONE;
2423 }
2424
2425 sigprocmask (SIG_SETMASK, &org_mask, NULL);
24a09b5f 2426 }
bd99dc85
PA
2427 else
2428 {
2429 do
2430 ret = waitpid (pid, status, flags);
2431 while (ret == -1 && errno == EINTR);
2432 out_errno = errno;
2433 }
2434
2435 if (debug_threads)
2436 fprintf (stderr, "my_waitpid (%d, 0x%x): status(%x), %d\n",
2437 pid, flags, status ? *status : -1, ret);
24a09b5f 2438
bd99dc85 2439 errno = out_errno;
24a09b5f
DJ
2440 return ret;
2441}
2442
2443/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
2444 sure that we can enable the option, and that it had the desired
2445 effect. */
2446
2447static void
2448linux_test_for_tracefork (void)
2449{
2450 int child_pid, ret, status;
2451 long second_pid;
bca929d3 2452 char *stack = xmalloc (STACK_SIZE * 4);
24a09b5f
DJ
2453
2454 linux_supports_tracefork_flag = 0;
2455
51c2684e 2456 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
7407e2de
AS
2457#ifdef __ia64__
2458 child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE,
2459 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
2460#else
2461 child_pid = clone (linux_tracefork_child, stack + STACK_SIZE,
2462 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
2463#endif
24a09b5f 2464 if (child_pid == -1)
51c2684e 2465 perror_with_name ("clone");
24a09b5f
DJ
2466
2467 ret = my_waitpid (child_pid, &status, 0);
2468 if (ret == -1)
2469 perror_with_name ("waitpid");
2470 else if (ret != child_pid)
2471 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
2472 if (! WIFSTOPPED (status))
2473 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
2474
2475 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
2476 if (ret != 0)
2477 {
2478 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
2479 if (ret != 0)
2480 {
2481 warning ("linux_test_for_tracefork: failed to kill child");
2482 return;
2483 }
2484
2485 ret = my_waitpid (child_pid, &status, 0);
2486 if (ret != child_pid)
2487 warning ("linux_test_for_tracefork: failed to wait for killed child");
2488 else if (!WIFSIGNALED (status))
2489 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
2490 "killed child", status);
2491
2492 return;
2493 }
2494
2495 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
2496 if (ret != 0)
2497 warning ("linux_test_for_tracefork: failed to resume child");
2498
2499 ret = my_waitpid (child_pid, &status, 0);
2500
2501 if (ret == child_pid && WIFSTOPPED (status)
2502 && status >> 16 == PTRACE_EVENT_FORK)
2503 {
2504 second_pid = 0;
2505 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
2506 if (ret == 0 && second_pid != 0)
2507 {
2508 int second_status;
2509
2510 linux_supports_tracefork_flag = 1;
2511 my_waitpid (second_pid, &second_status, 0);
2512 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
2513 if (ret != 0)
2514 warning ("linux_test_for_tracefork: failed to kill second child");
2515 my_waitpid (second_pid, &status, 0);
2516 }
2517 }
2518 else
2519 warning ("linux_test_for_tracefork: unexpected result from waitpid "
2520 "(%d, status 0x%x)", ret, status);
2521
2522 do
2523 {
2524 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
2525 if (ret != 0)
2526 warning ("linux_test_for_tracefork: failed to kill child");
2527 my_waitpid (child_pid, &status, 0);
2528 }
2529 while (WIFSTOPPED (status));
51c2684e
DJ
2530
2531 free (stack);
24a09b5f
DJ
2532}
2533
2534
2f2893d9
DJ
2535static void
2536linux_look_up_symbols (void)
2537{
0d62e5e8 2538#ifdef USE_THREAD_DB
95954743
PA
2539 struct process_info *proc = current_process ();
2540
2541 if (proc->private->thread_db_active)
0d62e5e8
DJ
2542 return;
2543
95954743
PA
2544 proc->private->thread_db_active
2545 = thread_db_init (!linux_supports_tracefork_flag);
0d62e5e8
DJ
2546#endif
2547}
2548
e5379b03 2549static void
ef57601b 2550linux_request_interrupt (void)
e5379b03 2551{
a1928bad 2552 extern unsigned long signal_pid;
e5379b03 2553
95954743
PA
2554 if (!ptid_equal (cont_thread, null_ptid)
2555 && !ptid_equal (cont_thread, minus_one_ptid))
e5379b03 2556 {
54a0b537 2557 struct lwp_info *lwp;
bd99dc85 2558 int lwpid;
e5379b03 2559
54a0b537 2560 lwp = get_thread_lwp (current_inferior);
bd99dc85
PA
2561 lwpid = lwpid_of (lwp);
2562 kill_lwp (lwpid, SIGINT);
e5379b03
DJ
2563 }
2564 else
ef57601b 2565 kill_lwp (signal_pid, SIGINT);
e5379b03
DJ
2566}
2567
aa691b87
RM
2568/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
2569 to debugger memory starting at MYADDR. */
2570
2571static int
f450004a 2572linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
aa691b87
RM
2573{
2574 char filename[PATH_MAX];
2575 int fd, n;
95954743 2576 int pid = lwpid_of (get_thread_lwp (current_inferior));
aa691b87 2577
95954743 2578 snprintf (filename, sizeof filename, "/proc/%d/auxv", pid);
aa691b87
RM
2579
2580 fd = open (filename, O_RDONLY);
2581 if (fd < 0)
2582 return -1;
2583
2584 if (offset != (CORE_ADDR) 0
2585 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
2586 n = -1;
2587 else
2588 n = read (fd, myaddr, len);
2589
2590 close (fd);
2591
2592 return n;
2593}
2594
e013ee27
OF
2595/* These watchpoint related wrapper functions simply pass on the function call
2596 if the target has registered a corresponding function. */
2597
2598static int
2599linux_insert_watchpoint (char type, CORE_ADDR addr, int len)
2600{
2601 if (the_low_target.insert_watchpoint != NULL)
2602 return the_low_target.insert_watchpoint (type, addr, len);
2603 else
2604 /* Unsupported (see target.h). */
2605 return 1;
2606}
2607
2608static int
2609linux_remove_watchpoint (char type, CORE_ADDR addr, int len)
2610{
2611 if (the_low_target.remove_watchpoint != NULL)
2612 return the_low_target.remove_watchpoint (type, addr, len);
2613 else
2614 /* Unsupported (see target.h). */
2615 return 1;
2616}
2617
2618static int
2619linux_stopped_by_watchpoint (void)
2620{
2621 if (the_low_target.stopped_by_watchpoint != NULL)
2622 return the_low_target.stopped_by_watchpoint ();
2623 else
2624 return 0;
2625}
2626
2627static CORE_ADDR
2628linux_stopped_data_address (void)
2629{
2630 if (the_low_target.stopped_data_address != NULL)
2631 return the_low_target.stopped_data_address ();
2632 else
2633 return 0;
2634}
2635
42c81e2a 2636#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
2637#if defined(__mcoldfire__)
2638/* These should really be defined in the kernel's ptrace.h header. */
2639#define PT_TEXT_ADDR 49*4
2640#define PT_DATA_ADDR 50*4
2641#define PT_TEXT_END_ADDR 51*4
2642#endif
2643
2644/* Under uClinux, programs are loaded at non-zero offsets, which we need
2645 to tell gdb about. */
2646
2647static int
2648linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
2649{
2650#if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
2651 unsigned long text, text_end, data;
bd99dc85 2652 int pid = lwpid_of (get_thread_lwp (current_inferior));
52fb6437
NS
2653
2654 errno = 0;
2655
2656 text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0);
2657 text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0);
2658 data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0);
2659
2660 if (errno == 0)
2661 {
2662 /* Both text and data offsets produced at compile-time (and so
1b3f6016
PA
2663 used by gdb) are relative to the beginning of the program,
2664 with the data segment immediately following the text segment.
2665 However, the actual runtime layout in memory may put the data
2666 somewhere else, so when we send gdb a data base-address, we
2667 use the real data base address and subtract the compile-time
2668 data base-address from it (which is just the length of the
2669 text segment). BSS immediately follows data in both
2670 cases. */
52fb6437
NS
2671 *text_p = text;
2672 *data_p = data - (text_end - text);
1b3f6016 2673
52fb6437
NS
2674 return 1;
2675 }
2676#endif
2677 return 0;
2678}
2679#endif
2680
07e059b5
VP
2681static int
2682linux_qxfer_osdata (const char *annex,
1b3f6016
PA
2683 unsigned char *readbuf, unsigned const char *writebuf,
2684 CORE_ADDR offset, int len)
07e059b5
VP
2685{
2686 /* We make the process list snapshot when the object starts to be
2687 read. */
2688 static const char *buf;
2689 static long len_avail = -1;
2690 static struct buffer buffer;
2691
2692 DIR *dirp;
2693
2694 if (strcmp (annex, "processes") != 0)
2695 return 0;
2696
2697 if (!readbuf || writebuf)
2698 return 0;
2699
2700 if (offset == 0)
2701 {
2702 if (len_avail != -1 && len_avail != 0)
2703 buffer_free (&buffer);
2704 len_avail = 0;
2705 buf = NULL;
2706 buffer_init (&buffer);
2707 buffer_grow_str (&buffer, "<osdata type=\"processes\">");
2708
2709 dirp = opendir ("/proc");
2710 if (dirp)
2711 {
1b3f6016
PA
2712 struct dirent *dp;
2713 while ((dp = readdir (dirp)) != NULL)
2714 {
2715 struct stat statbuf;
2716 char procentry[sizeof ("/proc/4294967295")];
2717
2718 if (!isdigit (dp->d_name[0])
2719 || strlen (dp->d_name) > sizeof ("4294967295") - 1)
2720 continue;
2721
2722 sprintf (procentry, "/proc/%s", dp->d_name);
2723 if (stat (procentry, &statbuf) == 0
2724 && S_ISDIR (statbuf.st_mode))
2725 {
2726 char pathname[128];
2727 FILE *f;
2728 char cmd[MAXPATHLEN + 1];
2729 struct passwd *entry;
2730
2731 sprintf (pathname, "/proc/%s/cmdline", dp->d_name);
2732 entry = getpwuid (statbuf.st_uid);
2733
2734 if ((f = fopen (pathname, "r")) != NULL)
2735 {
2736 size_t len = fread (cmd, 1, sizeof (cmd) - 1, f);
2737 if (len > 0)
2738 {
2739 int i;
2740 for (i = 0; i < len; i++)
2741 if (cmd[i] == '\0')
2742 cmd[i] = ' ';
2743 cmd[len] = '\0';
2744
2745 buffer_xml_printf (
07e059b5
VP
2746 &buffer,
2747 "<item>"
2748 "<column name=\"pid\">%s</column>"
2749 "<column name=\"user\">%s</column>"
2750 "<column name=\"command\">%s</column>"
2751 "</item>",
2752 dp->d_name,
2753 entry ? entry->pw_name : "?",
2754 cmd);
1b3f6016
PA
2755 }
2756 fclose (f);
2757 }
2758 }
2759 }
07e059b5 2760
1b3f6016 2761 closedir (dirp);
07e059b5
VP
2762 }
2763 buffer_grow_str0 (&buffer, "</osdata>\n");
2764 buf = buffer_finish (&buffer);
2765 len_avail = strlen (buf);
2766 }
2767
2768 if (offset >= len_avail)
2769 {
2770 /* Done. Get rid of the data. */
2771 buffer_free (&buffer);
2772 buf = NULL;
2773 len_avail = 0;
2774 return 0;
2775 }
2776
2777 if (len > len_avail - offset)
2778 len = len_avail - offset;
2779 memcpy (readbuf, buf + offset, len);
2780
2781 return len;
2782}
2783
4aa995e1
PA
2784static int
2785linux_xfer_siginfo (const char *annex, unsigned char *readbuf,
2786 unsigned const char *writebuf, CORE_ADDR offset, int len)
2787{
2788 struct siginfo siginfo;
2789 long pid = -1;
2790
2791 if (current_inferior == NULL)
2792 return -1;
2793
bd99dc85 2794 pid = lwpid_of (get_thread_lwp (current_inferior));
4aa995e1
PA
2795
2796 if (debug_threads)
2797 fprintf (stderr, "%s siginfo for lwp %ld.\n",
2798 readbuf != NULL ? "Reading" : "Writing",
2799 pid);
2800
2801 if (offset > sizeof (siginfo))
2802 return -1;
2803
2804 if (ptrace (PTRACE_GETSIGINFO, pid, 0, &siginfo) != 0)
2805 return -1;
2806
2807 if (offset + len > sizeof (siginfo))
2808 len = sizeof (siginfo) - offset;
2809
2810 if (readbuf != NULL)
2811 memcpy (readbuf, (char *) &siginfo + offset, len);
2812 else
2813 {
2814 memcpy ((char *) &siginfo + offset, writebuf, len);
2815 if (ptrace (PTRACE_SETSIGINFO, pid, 0, &siginfo) != 0)
2816 return -1;
2817 }
2818
2819 return len;
2820}
2821
bd99dc85
PA
2822/* SIGCHLD handler that serves two purposes: In non-stop/async mode,
2823 so we notice when children change state; as the handler for the
2824 sigsuspend in my_waitpid. */
2825
2826static void
2827sigchld_handler (int signo)
2828{
2829 int old_errno = errno;
2830
2831 if (debug_threads)
2832 /* fprintf is not async-signal-safe, so call write directly. */
2833 write (2, "sigchld_handler\n", sizeof ("sigchld_handler\n") - 1);
2834
2835 if (target_is_async_p ())
2836 async_file_mark (); /* trigger a linux_wait */
2837
2838 errno = old_errno;
2839}
2840
2841static int
2842linux_supports_non_stop (void)
2843{
2844 return 1;
2845}
2846
2847static int
2848linux_async (int enable)
2849{
2850 int previous = (linux_event_pipe[0] != -1);
2851
2852 if (previous != enable)
2853 {
2854 sigset_t mask;
2855 sigemptyset (&mask);
2856 sigaddset (&mask, SIGCHLD);
2857
2858 sigprocmask (SIG_BLOCK, &mask, NULL);
2859
2860 if (enable)
2861 {
2862 if (pipe (linux_event_pipe) == -1)
2863 fatal ("creating event pipe failed.");
2864
2865 fcntl (linux_event_pipe[0], F_SETFL, O_NONBLOCK);
2866 fcntl (linux_event_pipe[1], F_SETFL, O_NONBLOCK);
2867
2868 /* Register the event loop handler. */
2869 add_file_handler (linux_event_pipe[0],
2870 handle_target_event, NULL);
2871
2872 /* Always trigger a linux_wait. */
2873 async_file_mark ();
2874 }
2875 else
2876 {
2877 delete_file_handler (linux_event_pipe[0]);
2878
2879 close (linux_event_pipe[0]);
2880 close (linux_event_pipe[1]);
2881 linux_event_pipe[0] = -1;
2882 linux_event_pipe[1] = -1;
2883 }
2884
2885 sigprocmask (SIG_UNBLOCK, &mask, NULL);
2886 }
2887
2888 return previous;
2889}
2890
2891static int
2892linux_start_non_stop (int nonstop)
2893{
2894 /* Register or unregister from event-loop accordingly. */
2895 linux_async (nonstop);
2896 return 0;
2897}
2898
ce3a066d
DJ
2899static struct target_ops linux_target_ops = {
2900 linux_create_inferior,
2901 linux_attach,
2902 linux_kill,
6ad8ae5c 2903 linux_detach,
444d6139 2904 linux_join,
ce3a066d
DJ
2905 linux_thread_alive,
2906 linux_resume,
2907 linux_wait,
2908 linux_fetch_registers,
2909 linux_store_registers,
2910 linux_read_memory,
2911 linux_write_memory,
2f2893d9 2912 linux_look_up_symbols,
ef57601b 2913 linux_request_interrupt,
aa691b87 2914 linux_read_auxv,
e013ee27
OF
2915 linux_insert_watchpoint,
2916 linux_remove_watchpoint,
2917 linux_stopped_by_watchpoint,
2918 linux_stopped_data_address,
42c81e2a 2919#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437 2920 linux_read_offsets,
dae5f5cf
DJ
2921#else
2922 NULL,
2923#endif
2924#ifdef USE_THREAD_DB
2925 thread_db_get_tls_address,
2926#else
2927 NULL,
52fb6437 2928#endif
59a016f0
PA
2929 NULL,
2930 hostio_last_error_from_errno,
07e059b5 2931 linux_qxfer_osdata,
4aa995e1 2932 linux_xfer_siginfo,
bd99dc85
PA
2933 linux_supports_non_stop,
2934 linux_async,
2935 linux_start_non_stop,
ce3a066d
DJ
2936};
2937
0d62e5e8
DJ
2938static void
2939linux_init_signals ()
2940{
2941 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
2942 to find what the cancel signal actually is. */
254787d4 2943 signal (__SIGRTMIN+1, SIG_IGN);
0d62e5e8
DJ
2944}
2945
da6d8c04
DJ
2946void
2947initialize_low (void)
2948{
bd99dc85
PA
2949 struct sigaction sigchld_action;
2950 memset (&sigchld_action, 0, sizeof (sigchld_action));
ce3a066d 2951 set_target_ops (&linux_target_ops);
611cb4a5
DJ
2952 set_breakpoint_data (the_low_target.breakpoint,
2953 the_low_target.breakpoint_len);
0d62e5e8 2954 linux_init_signals ();
24a09b5f 2955 linux_test_for_tracefork ();
52fa2412
UW
2956#ifdef HAVE_LINUX_REGSETS
2957 for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++)
2958 ;
bca929d3 2959 disabled_regsets = xmalloc (num_regsets);
52fa2412 2960#endif
bd99dc85
PA
2961
2962 sigchld_action.sa_handler = sigchld_handler;
2963 sigemptyset (&sigchld_action.sa_mask);
2964 sigchld_action.sa_flags = SA_RESTART;
2965 sigaction (SIGCHLD, &sigchld_action, NULL);
da6d8c04 2966}