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