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