]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/linux-low.c
2008-08-04 John David Anglin <dave.anglin@nrc-cnrc.gc.ca>
[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,
9b254dd1 3 2006, 2007, 2008 Free Software Foundation, Inc.
da6d8c04
DJ
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
da6d8c04
DJ
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
da6d8c04
DJ
19
20#include "server.h"
58caa3dc 21#include "linux-low.h"
da6d8c04 22
58caa3dc 23#include <sys/wait.h>
da6d8c04
DJ
24#include <stdio.h>
25#include <sys/param.h>
da6d8c04 26#include <sys/ptrace.h>
da6d8c04
DJ
27#include <signal.h>
28#include <sys/ioctl.h>
29#include <fcntl.h>
d07c63e7 30#include <string.h>
0a30fbc4
DJ
31#include <stdlib.h>
32#include <unistd.h>
fa6a77dc 33#include <errno.h>
fd500816 34#include <sys/syscall.h>
f9387fc3 35#include <sched.h>
da6d8c04 36
32ca6d61
DJ
37#ifndef PTRACE_GETSIGINFO
38# define PTRACE_GETSIGINFO 0x4202
39# define PTRACE_SETSIGINFO 0x4203
40#endif
41
fd462a61
DJ
42#ifndef O_LARGEFILE
43#define O_LARGEFILE 0
44#endif
45
24a09b5f
DJ
46/* If the system headers did not provide the constants, hard-code the normal
47 values. */
48#ifndef PTRACE_EVENT_FORK
49
50#define PTRACE_SETOPTIONS 0x4200
51#define PTRACE_GETEVENTMSG 0x4201
52
53/* options set using PTRACE_SETOPTIONS */
54#define PTRACE_O_TRACESYSGOOD 0x00000001
55#define PTRACE_O_TRACEFORK 0x00000002
56#define PTRACE_O_TRACEVFORK 0x00000004
57#define PTRACE_O_TRACECLONE 0x00000008
58#define PTRACE_O_TRACEEXEC 0x00000010
59#define PTRACE_O_TRACEVFORKDONE 0x00000020
60#define PTRACE_O_TRACEEXIT 0x00000040
61
62/* Wait extended result codes for the above trace options. */
63#define PTRACE_EVENT_FORK 1
64#define PTRACE_EVENT_VFORK 2
65#define PTRACE_EVENT_CLONE 3
66#define PTRACE_EVENT_EXEC 4
67#define PTRACE_EVENT_VFORK_DONE 5
68#define PTRACE_EVENT_EXIT 6
69
70#endif /* PTRACE_EVENT_FORK */
71
72/* We can't always assume that this flag is available, but all systems
73 with the ptrace event handlers also have __WALL, so it's safe to use
74 in some contexts. */
75#ifndef __WALL
76#define __WALL 0x40000000 /* Wait for any child. */
77#endif
78
42c81e2a
DJ
79#ifdef __UCLIBC__
80#if !(defined(__UCLIBC_HAS_MMU__) || defined(__ARCH_HAS_MMU__))
81#define HAS_NOMMU
82#endif
83#endif
84
24a09b5f
DJ
85/* ``all_threads'' is keyed by the LWP ID, which we use as the GDB protocol
86 representation of the thread ID.
611cb4a5 87
0d62e5e8
DJ
88 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
89 the same as the LWP ID. */
90
91struct inferior_list all_processes;
92
24a09b5f
DJ
93/* A list of all unknown processes which receive stop signals. Some other
94 process will presumably claim each of these as forked children
95 momentarily. */
96
97struct inferior_list stopped_pids;
98
0d62e5e8
DJ
99/* FIXME this is a bit of a hack, and could be removed. */
100int stopping_threads;
101
102/* FIXME make into a target method? */
24a09b5f
DJ
103int using_threads = 1;
104static int thread_db_active;
105
106static int must_set_ptrace_flags;
0d62e5e8 107
d61ddec4
UW
108/* This flag is true iff we've just created or attached to a new inferior
109 but it has not stopped yet. As soon as it does, we need to call the
110 low target's arch_setup callback. */
111static int new_inferior;
112
0d62e5e8 113static void linux_resume_one_process (struct inferior_list_entry *entry,
32ca6d61 114 int step, int signal, siginfo_t *info);
64386c31 115static void linux_resume (struct thread_resume *resume_info);
0d62e5e8
DJ
116static void stop_all_processes (void);
117static int linux_wait_for_event (struct thread_info *child);
ae13219e 118static int check_removed_breakpoint (struct process_info *event_child);
24a09b5f 119static void *add_process (unsigned long pid);
0d62e5e8
DJ
120
121struct pending_signals
122{
123 int signal;
32ca6d61 124 siginfo_t info;
0d62e5e8
DJ
125 struct pending_signals *prev;
126};
611cb4a5 127
d844cde6 128#define PTRACE_ARG3_TYPE long
c6ecbae5 129#define PTRACE_XFER_TYPE long
da6d8c04 130
58caa3dc 131#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
132static char *disabled_regsets;
133static int num_regsets;
58caa3dc
DJ
134#endif
135
0d62e5e8
DJ
136#define pid_of(proc) ((proc)->head.id)
137
138/* FIXME: Delete eventually. */
139#define inferior_pid (pid_of (get_thread_process (current_inferior)))
140
24a09b5f
DJ
141static void
142handle_extended_wait (struct process_info *event_child, int wstat)
143{
144 int event = wstat >> 16;
145 struct process_info *new_process;
146
147 if (event == PTRACE_EVENT_CLONE)
148 {
149 unsigned long new_pid;
836acd6d 150 int ret, status = W_STOPCODE (SIGSTOP);
24a09b5f
DJ
151
152 ptrace (PTRACE_GETEVENTMSG, inferior_pid, 0, &new_pid);
153
154 /* If we haven't already seen the new PID stop, wait for it now. */
155 if (! pull_pid_from_list (&stopped_pids, new_pid))
156 {
157 /* The new child has a pending SIGSTOP. We can't affect it until it
158 hits the SIGSTOP, but we're already attached. */
159
160 do {
161 ret = waitpid (new_pid, &status, __WALL);
162 } while (ret == -1 && errno == EINTR);
163
164 if (ret == -1)
165 perror_with_name ("waiting for new child");
166 else if (ret != new_pid)
167 warning ("wait returned unexpected PID %d", ret);
da5898ce 168 else if (!WIFSTOPPED (status))
24a09b5f
DJ
169 warning ("wait returned unexpected status 0x%x", status);
170 }
171
172 ptrace (PTRACE_SETOPTIONS, new_pid, 0, PTRACE_O_TRACECLONE);
173
174 new_process = (struct process_info *) add_process (new_pid);
175 add_thread (new_pid, new_process, new_pid);
176 new_thread_notify (thread_id_to_gdb_id (new_process->lwpid));
177
da5898ce
DJ
178 /* Normally we will get the pending SIGSTOP. But in some cases
179 we might get another signal delivered to the group first.
180 If we do, be sure not to lose it. */
181 if (WSTOPSIG (status) == SIGSTOP)
182 {
183 if (stopping_threads)
184 new_process->stopped = 1;
185 else
186 ptrace (PTRACE_CONT, new_pid, 0, 0);
187 }
24a09b5f 188 else
da5898ce
DJ
189 {
190 new_process->stop_expected = 1;
191 if (stopping_threads)
192 {
193 new_process->stopped = 1;
194 new_process->status_pending_p = 1;
195 new_process->status_pending = status;
196 }
197 else
198 /* Pass the signal on. This is what GDB does - except
199 shouldn't we really report it instead? */
200 ptrace (PTRACE_CONT, new_pid, 0, WSTOPSIG (status));
201 }
24a09b5f
DJ
202
203 /* Always resume the current thread. If we are stopping
204 threads, it will have a pending SIGSTOP; we may as well
205 collect it now. */
206 linux_resume_one_process (&event_child->head,
207 event_child->stepping, 0, NULL);
208 }
209}
210
0d62e5e8
DJ
211/* This function should only be called if the process got a SIGTRAP.
212 The SIGTRAP could mean several things.
213
214 On i386, where decr_pc_after_break is non-zero:
215 If we were single-stepping this process using PTRACE_SINGLESTEP,
216 we will get only the one SIGTRAP (even if the instruction we
217 stepped over was a breakpoint). The value of $eip will be the
218 next instruction.
219 If we continue the process using PTRACE_CONT, we will get a
220 SIGTRAP when we hit a breakpoint. The value of $eip will be
221 the instruction after the breakpoint (i.e. needs to be
222 decremented). If we report the SIGTRAP to GDB, we must also
223 report the undecremented PC. If we cancel the SIGTRAP, we
224 must resume at the decremented PC.
225
226 (Presumably, not yet tested) On a non-decr_pc_after_break machine
227 with hardware or kernel single-step:
228 If we single-step over a breakpoint instruction, our PC will
229 point at the following instruction. If we continue and hit a
230 breakpoint instruction, our PC will point at the breakpoint
231 instruction. */
232
233static CORE_ADDR
234get_stop_pc (void)
235{
236 CORE_ADDR stop_pc = (*the_low_target.get_pc) ();
237
238 if (get_thread_process (current_inferior)->stepping)
239 return stop_pc;
240 else
241 return stop_pc - the_low_target.decr_pc_after_break;
242}
ce3a066d 243
0d62e5e8 244static void *
a1928bad 245add_process (unsigned long pid)
611cb4a5 246{
0d62e5e8
DJ
247 struct process_info *process;
248
249 process = (struct process_info *) malloc (sizeof (*process));
250 memset (process, 0, sizeof (*process));
251
252 process->head.id = pid;
0d62e5e8
DJ
253 process->lwpid = pid;
254
255 add_inferior_to_list (&all_processes, &process->head);
256
257 return process;
258}
611cb4a5 259
da6d8c04
DJ
260/* Start an inferior process and returns its pid.
261 ALLARGS is a vector of program-name and args. */
262
ce3a066d
DJ
263static int
264linux_create_inferior (char *program, char **allargs)
da6d8c04 265{
0d62e5e8 266 void *new_process;
da6d8c04
DJ
267 int pid;
268
42c81e2a 269#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
270 pid = vfork ();
271#else
da6d8c04 272 pid = fork ();
52fb6437 273#endif
da6d8c04
DJ
274 if (pid < 0)
275 perror_with_name ("fork");
276
277 if (pid == 0)
278 {
279 ptrace (PTRACE_TRACEME, 0, 0, 0);
280
254787d4 281 signal (__SIGRTMIN + 1, SIG_DFL);
0d62e5e8 282
a9fa9f7d
DJ
283 setpgid (0, 0);
284
2b876972
DJ
285 execv (program, allargs);
286 if (errno == ENOENT)
287 execvp (program, allargs);
da6d8c04
DJ
288
289 fprintf (stderr, "Cannot exec %s: %s.\n", program,
d07c63e7 290 strerror (errno));
da6d8c04
DJ
291 fflush (stderr);
292 _exit (0177);
293 }
294
0d62e5e8 295 new_process = add_process (pid);
a06660f7 296 add_thread (pid, new_process, pid);
24a09b5f 297 must_set_ptrace_flags = 1;
d61ddec4 298 new_inferior = 1;
611cb4a5 299
a9fa9f7d 300 return pid;
da6d8c04
DJ
301}
302
303/* Attach to an inferior process. */
304
0d62e5e8 305void
24a09b5f 306linux_attach_lwp (unsigned long pid)
da6d8c04 307{
0d62e5e8 308 struct process_info *new_process;
611cb4a5 309
da6d8c04
DJ
310 if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
311 {
2d717e4f
DJ
312 if (all_threads.head != NULL)
313 {
314 /* If we fail to attach to an LWP, just warn. */
315 fprintf (stderr, "Cannot attach to process %ld: %s (%d)\n", pid,
316 strerror (errno), errno);
317 fflush (stderr);
318 return;
319 }
320 else
321 /* If we fail to attach to a process, report an error. */
322 error ("Cannot attach to process %ld: %s (%d)\n", pid,
43d5792c 323 strerror (errno), errno);
da6d8c04
DJ
324 }
325
24a09b5f
DJ
326 ptrace (PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACECLONE);
327
0d62e5e8 328 new_process = (struct process_info *) add_process (pid);
24a09b5f
DJ
329 add_thread (pid, new_process, pid);
330 new_thread_notify (thread_id_to_gdb_id (new_process->lwpid));
0d62e5e8
DJ
331
332 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
333 brings it to a halt. We should ignore that SIGSTOP and resume the process
334 (unless this is the first process, in which case the flag will be cleared
335 in linux_attach).
336
337 On the other hand, if we are currently trying to stop all threads, we
338 should treat the new thread as if we had sent it a SIGSTOP. This works
339 because we are guaranteed that add_process added us to the end of the
340 list, and so the new thread has not yet reached wait_for_sigstop (but
341 will). */
342 if (! stopping_threads)
343 new_process->stop_expected = 1;
344}
345
346int
a1928bad 347linux_attach (unsigned long pid)
0d62e5e8
DJ
348{
349 struct process_info *process;
350
24a09b5f 351 linux_attach_lwp (pid);
0d62e5e8 352
ae13219e
DJ
353 /* Don't ignore the initial SIGSTOP if we just attached to this process.
354 It will be collected by wait shortly. */
0d62e5e8
DJ
355 process = (struct process_info *) find_inferior_id (&all_processes, pid);
356 process->stop_expected = 0;
357
d61ddec4
UW
358 new_inferior = 1;
359
da6d8c04
DJ
360 return 0;
361}
362
363/* Kill the inferior process. Make us have no inferior. */
364
ce3a066d 365static void
0d62e5e8 366linux_kill_one_process (struct inferior_list_entry *entry)
da6d8c04 367{
0d62e5e8
DJ
368 struct thread_info *thread = (struct thread_info *) entry;
369 struct process_info *process = get_thread_process (thread);
370 int wstat;
371
fd500816
DJ
372 /* We avoid killing the first thread here, because of a Linux kernel (at
373 least 2.6.0-test7 through 2.6.8-rc4) bug; if we kill the parent before
374 the children get a chance to be reaped, it will remain a zombie
375 forever. */
376 if (entry == all_threads.head)
377 return;
378
0d62e5e8
DJ
379 do
380 {
381 ptrace (PTRACE_KILL, pid_of (process), 0, 0);
382
383 /* Make sure it died. The loop is most likely unnecessary. */
384 wstat = linux_wait_for_event (thread);
385 } while (WIFSTOPPED (wstat));
da6d8c04
DJ
386}
387
0d62e5e8
DJ
388static void
389linux_kill (void)
390{
fd500816 391 struct thread_info *thread = (struct thread_info *) all_threads.head;
9d606399 392 struct process_info *process;
fd500816
DJ
393 int wstat;
394
9d606399
DJ
395 if (thread == NULL)
396 return;
397
0d62e5e8 398 for_each_inferior (&all_threads, linux_kill_one_process);
fd500816
DJ
399
400 /* See the comment in linux_kill_one_process. We did not kill the first
401 thread in the list, so do so now. */
9d606399 402 process = get_thread_process (thread);
fd500816
DJ
403 do
404 {
405 ptrace (PTRACE_KILL, pid_of (process), 0, 0);
406
407 /* Make sure it died. The loop is most likely unnecessary. */
408 wstat = linux_wait_for_event (thread);
409 } while (WIFSTOPPED (wstat));
2d717e4f
DJ
410
411 clear_inferiors ();
412 free (all_processes.head);
413 all_processes.head = all_processes.tail = NULL;
0d62e5e8
DJ
414}
415
6ad8ae5c
DJ
416static void
417linux_detach_one_process (struct inferior_list_entry *entry)
418{
419 struct thread_info *thread = (struct thread_info *) entry;
420 struct process_info *process = get_thread_process (thread);
421
ae13219e
DJ
422 /* Make sure the process isn't stopped at a breakpoint that's
423 no longer there. */
424 check_removed_breakpoint (process);
425
426 /* If this process is stopped but is expecting a SIGSTOP, then make
427 sure we take care of that now. This isn't absolutely guaranteed
428 to collect the SIGSTOP, but is fairly likely to. */
429 if (process->stop_expected)
430 {
431 /* Clear stop_expected, so that the SIGSTOP will be reported. */
432 process->stop_expected = 0;
433 if (process->stopped)
434 linux_resume_one_process (&process->head, 0, 0, NULL);
435 linux_wait_for_event (thread);
436 }
437
438 /* Flush any pending changes to the process's registers. */
439 regcache_invalidate_one ((struct inferior_list_entry *)
440 get_process_thread (process));
441
442 /* Finally, let it resume. */
6ad8ae5c
DJ
443 ptrace (PTRACE_DETACH, pid_of (process), 0, 0);
444}
445
dd6953e1 446static int
6ad8ae5c
DJ
447linux_detach (void)
448{
ae13219e 449 delete_all_breakpoints ();
6ad8ae5c 450 for_each_inferior (&all_threads, linux_detach_one_process);
ae13219e 451 clear_inferiors ();
2d717e4f
DJ
452 free (all_processes.head);
453 all_processes.head = all_processes.tail = NULL;
dd6953e1 454 return 0;
6ad8ae5c
DJ
455}
456
444d6139
PA
457static void
458linux_join (void)
459{
460 extern unsigned long signal_pid;
461 int status, ret;
462
463 do {
464 ret = waitpid (signal_pid, &status, 0);
465 if (WIFEXITED (status) || WIFSIGNALED (status))
466 break;
467 } while (ret != -1 || errno != ECHILD);
468}
469
6ad8ae5c 470/* Return nonzero if the given thread is still alive. */
0d62e5e8 471static int
24a09b5f 472linux_thread_alive (unsigned long lwpid)
0d62e5e8 473{
24a09b5f 474 if (find_inferior_id (&all_threads, lwpid) != NULL)
0d62e5e8
DJ
475 return 1;
476 else
477 return 0;
478}
479
480/* Return nonzero if this process stopped at a breakpoint which
481 no longer appears to be inserted. Also adjust the PC
482 appropriately to resume where the breakpoint used to be. */
ce3a066d 483static int
0d62e5e8 484check_removed_breakpoint (struct process_info *event_child)
da6d8c04 485{
0d62e5e8
DJ
486 CORE_ADDR stop_pc;
487 struct thread_info *saved_inferior;
488
489 if (event_child->pending_is_breakpoint == 0)
490 return 0;
491
492 if (debug_threads)
ae13219e
DJ
493 fprintf (stderr, "Checking for breakpoint in process %ld.\n",
494 event_child->lwpid);
0d62e5e8
DJ
495
496 saved_inferior = current_inferior;
497 current_inferior = get_process_thread (event_child);
498
499 stop_pc = get_stop_pc ();
500
501 /* If the PC has changed since we stopped, then we shouldn't do
502 anything. This happens if, for instance, GDB handled the
503 decr_pc_after_break subtraction itself. */
504 if (stop_pc != event_child->pending_stop_pc)
505 {
506 if (debug_threads)
ae13219e
DJ
507 fprintf (stderr, "Ignoring, PC was changed. Old PC was 0x%08llx\n",
508 event_child->pending_stop_pc);
0d62e5e8
DJ
509
510 event_child->pending_is_breakpoint = 0;
511 current_inferior = saved_inferior;
512 return 0;
513 }
514
515 /* If the breakpoint is still there, we will report hitting it. */
516 if ((*the_low_target.breakpoint_at) (stop_pc))
517 {
518 if (debug_threads)
519 fprintf (stderr, "Ignoring, breakpoint is still present.\n");
520 current_inferior = saved_inferior;
521 return 0;
522 }
523
524 if (debug_threads)
525 fprintf (stderr, "Removed breakpoint.\n");
526
527 /* For decr_pc_after_break targets, here is where we perform the
528 decrement. We go immediately from this function to resuming,
529 and can not safely call get_stop_pc () again. */
530 if (the_low_target.set_pc != NULL)
531 (*the_low_target.set_pc) (stop_pc);
532
533 /* We consumed the pending SIGTRAP. */
5544ad89 534 event_child->pending_is_breakpoint = 0;
0d62e5e8
DJ
535 event_child->status_pending_p = 0;
536 event_child->status_pending = 0;
537
538 current_inferior = saved_inferior;
da6d8c04
DJ
539 return 1;
540}
541
0d62e5e8
DJ
542/* Return 1 if this process has an interesting status pending. This function
543 may silently resume an inferior process. */
611cb4a5 544static int
0d62e5e8
DJ
545status_pending_p (struct inferior_list_entry *entry, void *dummy)
546{
547 struct process_info *process = (struct process_info *) entry;
548
549 if (process->status_pending_p)
550 if (check_removed_breakpoint (process))
551 {
552 /* This thread was stopped at a breakpoint, and the breakpoint
553 is now gone. We were told to continue (or step...) all threads,
554 so GDB isn't trying to single-step past this breakpoint.
555 So instead of reporting the old SIGTRAP, pretend we got to
556 the breakpoint just after it was removed instead of just
557 before; resume the process. */
32ca6d61 558 linux_resume_one_process (&process->head, 0, 0, NULL);
0d62e5e8
DJ
559 return 0;
560 }
561
562 return process->status_pending_p;
563}
564
565static void
566linux_wait_for_process (struct process_info **childp, int *wstatp)
611cb4a5 567{
0d62e5e8
DJ
568 int ret;
569 int to_wait_for = -1;
570
571 if (*childp != NULL)
572 to_wait_for = (*childp)->lwpid;
611cb4a5 573
24a09b5f 574retry:
611cb4a5
DJ
575 while (1)
576 {
0d62e5e8
DJ
577 ret = waitpid (to_wait_for, wstatp, WNOHANG);
578
579 if (ret == -1)
580 {
581 if (errno != ECHILD)
582 perror_with_name ("waitpid");
583 }
584 else if (ret > 0)
585 break;
586
587 ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE);
588
589 if (ret == -1)
590 {
591 if (errno != ECHILD)
592 perror_with_name ("waitpid (WCLONE)");
593 }
594 else if (ret > 0)
595 break;
596
597 usleep (1000);
598 }
599
600 if (debug_threads
601 && (!WIFSTOPPED (*wstatp)
602 || (WSTOPSIG (*wstatp) != 32
603 && WSTOPSIG (*wstatp) != 33)))
604 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
605
606 if (to_wait_for == -1)
607 *childp = (struct process_info *) find_inferior_id (&all_processes, ret);
608
24a09b5f
DJ
609 /* If we didn't find a process, one of two things presumably happened:
610 - A process we started and then detached from has exited. Ignore it.
611 - A process we are controlling has forked and the new child's stop
612 was reported to us by the kernel. Save its PID. */
613 if (*childp == NULL && WIFSTOPPED (*wstatp))
614 {
615 add_pid_to_list (&stopped_pids, ret);
616 goto retry;
617 }
618 else if (*childp == NULL)
619 goto retry;
620
0d62e5e8
DJ
621 (*childp)->stopped = 1;
622 (*childp)->pending_is_breakpoint = 0;
623
32ca6d61
DJ
624 (*childp)->last_status = *wstatp;
625
d61ddec4
UW
626 /* Architecture-specific setup after inferior is running.
627 This needs to happen after we have attached to the inferior
628 and it is stopped for the first time, but before we access
629 any inferior registers. */
630 if (new_inferior)
631 {
632 the_low_target.arch_setup ();
52fa2412
UW
633#ifdef HAVE_LINUX_REGSETS
634 memset (disabled_regsets, 0, num_regsets);
635#endif
d61ddec4
UW
636 new_inferior = 0;
637 }
638
0d62e5e8
DJ
639 if (debug_threads
640 && WIFSTOPPED (*wstatp))
641 {
642 current_inferior = (struct thread_info *)
24a09b5f 643 find_inferior_id (&all_threads, (*childp)->lwpid);
0d62e5e8
DJ
644 /* For testing only; i386_stop_pc prints out a diagnostic. */
645 if (the_low_target.get_pc != NULL)
646 get_stop_pc ();
647 }
648}
611cb4a5 649
0d62e5e8
DJ
650static int
651linux_wait_for_event (struct thread_info *child)
652{
653 CORE_ADDR stop_pc;
654 struct process_info *event_child;
655 int wstat;
b65d95c5 656 int bp_status;
0d62e5e8
DJ
657
658 /* Check for a process with a pending status. */
659 /* It is possible that the user changed the pending task's registers since
660 it stopped. We correctly handle the change of PC if we hit a breakpoint
e5379b03 661 (in check_removed_breakpoint); signals should be reported anyway. */
0d62e5e8
DJ
662 if (child == NULL)
663 {
664 event_child = (struct process_info *)
665 find_inferior (&all_processes, status_pending_p, NULL);
666 if (debug_threads && event_child)
a1928bad 667 fprintf (stderr, "Got a pending child %ld\n", event_child->lwpid);
0d62e5e8
DJ
668 }
669 else
670 {
671 event_child = get_thread_process (child);
672 if (event_child->status_pending_p
673 && check_removed_breakpoint (event_child))
674 event_child = NULL;
675 }
611cb4a5 676
0d62e5e8
DJ
677 if (event_child != NULL)
678 {
679 if (event_child->status_pending_p)
611cb4a5 680 {
0d62e5e8 681 if (debug_threads)
a1928bad 682 fprintf (stderr, "Got an event from pending child %ld (%04x)\n",
0d62e5e8
DJ
683 event_child->lwpid, event_child->status_pending);
684 wstat = event_child->status_pending;
685 event_child->status_pending_p = 0;
686 event_child->status_pending = 0;
687 current_inferior = get_process_thread (event_child);
688 return wstat;
689 }
690 }
691
692 /* We only enter this loop if no process has a pending wait status. Thus
693 any action taken in response to a wait status inside this loop is
694 responding as soon as we detect the status, not after any pending
695 events. */
696 while (1)
697 {
698 if (child == NULL)
699 event_child = NULL;
700 else
701 event_child = get_thread_process (child);
702
703 linux_wait_for_process (&event_child, &wstat);
704
705 if (event_child == NULL)
706 error ("event from unknown child");
611cb4a5 707
0d62e5e8 708 current_inferior = (struct thread_info *)
24a09b5f 709 find_inferior_id (&all_threads, event_child->lwpid);
0d62e5e8 710
89be2091 711 /* Check for thread exit. */
24a09b5f 712 if (! WIFSTOPPED (wstat))
0d62e5e8 713 {
89be2091 714 if (debug_threads)
24a09b5f 715 fprintf (stderr, "LWP %ld exiting\n", event_child->head.id);
89be2091
DJ
716
717 /* If the last thread is exiting, just return. */
718 if (all_threads.head == all_threads.tail)
719 return wstat;
720
24a09b5f 721 dead_thread_notify (thread_id_to_gdb_id (event_child->lwpid));
89be2091
DJ
722
723 remove_inferior (&all_processes, &event_child->head);
724 free (event_child);
725 remove_thread (current_inferior);
726 current_inferior = (struct thread_info *) all_threads.head;
727
728 /* If we were waiting for this particular child to do something...
729 well, it did something. */
730 if (child != NULL)
731 return wstat;
732
733 /* Wait for a more interesting event. */
734 continue;
735 }
736
24a09b5f 737 if (WIFSTOPPED (wstat)
89be2091
DJ
738 && WSTOPSIG (wstat) == SIGSTOP
739 && event_child->stop_expected)
740 {
741 if (debug_threads)
742 fprintf (stderr, "Expected stop.\n");
743 event_child->stop_expected = 0;
744 linux_resume_one_process (&event_child->head,
745 event_child->stepping, 0, NULL);
746 continue;
747 }
748
24a09b5f
DJ
749 if (WIFSTOPPED (wstat) && WSTOPSIG (wstat) == SIGTRAP
750 && wstat >> 16 != 0)
751 {
752 handle_extended_wait (event_child, wstat);
753 continue;
754 }
755
89be2091
DJ
756 /* If GDB is not interested in this signal, don't stop other
757 threads, and don't report it to GDB. Just resume the
758 inferior right away. We do this for threading-related
69f223ed
DJ
759 signals as well as any that GDB specifically requested we
760 ignore. But never ignore SIGSTOP if we sent it ourselves,
761 and do not ignore signals when stepping - they may require
762 special handling to skip the signal handler. */
89be2091
DJ
763 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
764 thread library? */
765 if (WIFSTOPPED (wstat)
69f223ed 766 && !event_child->stepping
24a09b5f
DJ
767 && (
768#ifdef USE_THREAD_DB
769 (thread_db_active && (WSTOPSIG (wstat) == __SIGRTMIN
770 || WSTOPSIG (wstat) == __SIGRTMIN + 1))
771 ||
772#endif
773 (pass_signals[target_signal_from_host (WSTOPSIG (wstat))]
774 && (WSTOPSIG (wstat) != SIGSTOP || !stopping_threads))))
89be2091
DJ
775 {
776 siginfo_t info, *info_p;
777
778 if (debug_threads)
24a09b5f
DJ
779 fprintf (stderr, "Ignored signal %d for LWP %ld.\n",
780 WSTOPSIG (wstat), event_child->head.id);
89be2091
DJ
781
782 if (ptrace (PTRACE_GETSIGINFO, event_child->lwpid, 0, &info) == 0)
783 info_p = &info;
784 else
785 info_p = NULL;
786 linux_resume_one_process (&event_child->head,
787 event_child->stepping,
788 WSTOPSIG (wstat), info_p);
789 continue;
0d62e5e8 790 }
611cb4a5 791
0d62e5e8
DJ
792 /* If this event was not handled above, and is not a SIGTRAP, report
793 it. */
794 if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP)
795 return wstat;
611cb4a5 796
0d62e5e8
DJ
797 /* If this target does not support breakpoints, we simply report the
798 SIGTRAP; it's of no concern to us. */
799 if (the_low_target.get_pc == NULL)
800 return wstat;
801
802 stop_pc = get_stop_pc ();
803
804 /* bp_reinsert will only be set if we were single-stepping.
805 Notice that we will resume the process after hitting
806 a gdbserver breakpoint; single-stepping to/over one
807 is not supported (yet). */
808 if (event_child->bp_reinsert != 0)
809 {
810 if (debug_threads)
811 fprintf (stderr, "Reinserted breakpoint.\n");
812 reinsert_breakpoint (event_child->bp_reinsert);
813 event_child->bp_reinsert = 0;
814
815 /* Clear the single-stepping flag and SIGTRAP as we resume. */
32ca6d61 816 linux_resume_one_process (&event_child->head, 0, 0, NULL);
0d62e5e8
DJ
817 continue;
818 }
819
b65d95c5 820 bp_status = check_breakpoints (stop_pc);
0d62e5e8 821
b65d95c5 822 if (bp_status != 0)
0d62e5e8 823 {
b65d95c5
DJ
824 if (debug_threads)
825 fprintf (stderr, "Hit a gdbserver breakpoint.\n");
826
0d62e5e8 827 /* We hit one of our own breakpoints. We mark it as a pending
e5379b03 828 breakpoint, so that check_removed_breakpoint () will do the PC
0d62e5e8
DJ
829 adjustment for us at the appropriate time. */
830 event_child->pending_is_breakpoint = 1;
831 event_child->pending_stop_pc = stop_pc;
832
b65d95c5 833 /* We may need to put the breakpoint back. We continue in the event
0d62e5e8
DJ
834 loop instead of simply replacing the breakpoint right away,
835 in order to not lose signals sent to the thread that hit the
836 breakpoint. Unfortunately this increases the window where another
837 thread could sneak past the removed breakpoint. For the current
838 use of server-side breakpoints (thread creation) this is
839 acceptable; but it needs to be considered before this breakpoint
840 mechanism can be used in more general ways. For some breakpoints
841 it may be necessary to stop all other threads, but that should
842 be avoided where possible.
843
844 If breakpoint_reinsert_addr is NULL, that means that we can
845 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
846 mark it for reinsertion, and single-step.
847
848 Otherwise, call the target function to figure out where we need
849 our temporary breakpoint, create it, and continue executing this
850 process. */
b65d95c5
DJ
851 if (bp_status == 2)
852 /* No need to reinsert. */
853 linux_resume_one_process (&event_child->head, 0, 0, NULL);
854 else if (the_low_target.breakpoint_reinsert_addr == NULL)
0d62e5e8
DJ
855 {
856 event_child->bp_reinsert = stop_pc;
857 uninsert_breakpoint (stop_pc);
32ca6d61 858 linux_resume_one_process (&event_child->head, 1, 0, NULL);
0d62e5e8
DJ
859 }
860 else
861 {
862 reinsert_breakpoint_by_bp
863 (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
32ca6d61 864 linux_resume_one_process (&event_child->head, 0, 0, NULL);
611cb4a5 865 }
0d62e5e8
DJ
866
867 continue;
868 }
869
b65d95c5
DJ
870 if (debug_threads)
871 fprintf (stderr, "Hit a non-gdbserver breakpoint.\n");
872
0d62e5e8
DJ
873 /* If we were single-stepping, we definitely want to report the
874 SIGTRAP. The single-step operation has completed, so also
aa691b87 875 clear the stepping flag; in general this does not matter,
0d62e5e8
DJ
876 because the SIGTRAP will be reported to the client, which
877 will give us a new action for this thread, but clear it for
878 consistency anyway. It's safe to clear the stepping flag
879 because the only consumer of get_stop_pc () after this point
e5379b03 880 is check_removed_breakpoint, and pending_is_breakpoint is not
0d62e5e8
DJ
881 set. It might be wiser to use a step_completed flag instead. */
882 if (event_child->stepping)
883 {
884 event_child->stepping = 0;
885 return wstat;
886 }
887
888 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
889 Check if it is a breakpoint, and if so mark the process information
890 accordingly. This will handle both the necessary fiddling with the
891 PC on decr_pc_after_break targets and suppressing extra threads
892 hitting a breakpoint if two hit it at once and then GDB removes it
893 after the first is reported. Arguably it would be better to report
894 multiple threads hitting breakpoints simultaneously, but the current
895 remote protocol does not allow this. */
896 if ((*the_low_target.breakpoint_at) (stop_pc))
897 {
898 event_child->pending_is_breakpoint = 1;
899 event_child->pending_stop_pc = stop_pc;
611cb4a5
DJ
900 }
901
902 return wstat;
903 }
0d62e5e8 904
611cb4a5
DJ
905 /* NOTREACHED */
906 return 0;
907}
908
0d62e5e8 909/* Wait for process, returns status. */
da6d8c04 910
ce3a066d
DJ
911static unsigned char
912linux_wait (char *status)
da6d8c04 913{
e5f1222d 914 int w;
0d62e5e8
DJ
915 struct thread_info *child = NULL;
916
917retry:
918 /* If we were only supposed to resume one thread, only wait for
919 that thread - if it's still alive. If it died, however - which
920 can happen if we're coming from the thread death case below -
921 then we need to make sure we restart the other threads. We could
922 pick a thread at random or restart all; restarting all is less
923 arbitrary. */
d592fa2f 924 if (cont_thread != 0 && cont_thread != -1)
0d62e5e8
DJ
925 {
926 child = (struct thread_info *) find_inferior_id (&all_threads,
927 cont_thread);
928
929 /* No stepping, no signal - unless one is pending already, of course. */
930 if (child == NULL)
64386c31
DJ
931 {
932 struct thread_resume resume_info;
933 resume_info.thread = -1;
934 resume_info.step = resume_info.sig = resume_info.leave_stopped = 0;
935 linux_resume (&resume_info);
936 }
0d62e5e8 937 }
da6d8c04 938
0d62e5e8
DJ
939 w = linux_wait_for_event (child);
940 stop_all_processes ();
da6d8c04 941
24a09b5f
DJ
942 if (must_set_ptrace_flags)
943 {
944 ptrace (PTRACE_SETOPTIONS, inferior_pid, 0, PTRACE_O_TRACECLONE);
945 must_set_ptrace_flags = 0;
946 }
947
0d62e5e8
DJ
948 /* If we are waiting for a particular child, and it exited,
949 linux_wait_for_event will return its exit status. Similarly if
950 the last child exited. If this is not the last child, however,
951 do not report it as exited until there is a 'thread exited' response
952 available in the remote protocol. Instead, just wait for another event.
953 This should be safe, because if the thread crashed we will already
954 have reported the termination signal to GDB; that should stop any
955 in-progress stepping operations, etc.
956
957 Report the exit status of the last thread to exit. This matches
958 LinuxThreads' behavior. */
959
960 if (all_threads.head == all_threads.tail)
da6d8c04 961 {
0d62e5e8
DJ
962 if (WIFEXITED (w))
963 {
964 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
965 *status = 'W';
966 clear_inferiors ();
075b3282
DJ
967 free (all_processes.head);
968 all_processes.head = all_processes.tail = NULL;
b80864fb 969 return WEXITSTATUS (w);
0d62e5e8
DJ
970 }
971 else if (!WIFSTOPPED (w))
972 {
973 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
0d62e5e8 974 *status = 'X';
075b3282
DJ
975 clear_inferiors ();
976 free (all_processes.head);
977 all_processes.head = all_processes.tail = NULL;
b80864fb 978 return target_signal_from_host (WTERMSIG (w));
0d62e5e8 979 }
da6d8c04 980 }
0d62e5e8 981 else
da6d8c04 982 {
0d62e5e8
DJ
983 if (!WIFSTOPPED (w))
984 goto retry;
da6d8c04
DJ
985 }
986
da6d8c04 987 *status = 'T';
b80864fb 988 return target_signal_from_host (WSTOPSIG (w));
da6d8c04
DJ
989}
990
fd500816
DJ
991/* Send a signal to an LWP. For LinuxThreads, kill is enough; however, if
992 thread groups are in use, we need to use tkill. */
993
994static int
a1928bad 995kill_lwp (unsigned long lwpid, int signo)
fd500816
DJ
996{
997 static int tkill_failed;
998
999 errno = 0;
1000
1001#ifdef SYS_tkill
1002 if (!tkill_failed)
1003 {
1004 int ret = syscall (SYS_tkill, lwpid, signo);
1005 if (errno != ENOSYS)
1006 return ret;
1007 errno = 0;
1008 tkill_failed = 1;
1009 }
1010#endif
1011
1012 return kill (lwpid, signo);
1013}
1014
0d62e5e8
DJ
1015static void
1016send_sigstop (struct inferior_list_entry *entry)
1017{
1018 struct process_info *process = (struct process_info *) entry;
1019
1020 if (process->stopped)
1021 return;
1022
1023 /* If we already have a pending stop signal for this process, don't
1024 send another. */
1025 if (process->stop_expected)
1026 {
ae13219e
DJ
1027 if (debug_threads)
1028 fprintf (stderr, "Have pending sigstop for process %ld\n",
1029 process->lwpid);
1030
1031 /* We clear the stop_expected flag so that wait_for_sigstop
1032 will receive the SIGSTOP event (instead of silently resuming and
1033 waiting again). It'll be reset below. */
0d62e5e8
DJ
1034 process->stop_expected = 0;
1035 return;
1036 }
1037
1038 if (debug_threads)
a1928bad 1039 fprintf (stderr, "Sending sigstop to process %ld\n", process->head.id);
0d62e5e8 1040
fd500816 1041 kill_lwp (process->head.id, SIGSTOP);
0d62e5e8
DJ
1042}
1043
1044static void
1045wait_for_sigstop (struct inferior_list_entry *entry)
1046{
1047 struct process_info *process = (struct process_info *) entry;
1048 struct thread_info *saved_inferior, *thread;
a1928bad
DJ
1049 int wstat;
1050 unsigned long saved_tid;
0d62e5e8
DJ
1051
1052 if (process->stopped)
1053 return;
1054
1055 saved_inferior = current_inferior;
1056 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
1057 thread = (struct thread_info *) find_inferior_id (&all_threads,
24a09b5f 1058 process->lwpid);
0d62e5e8
DJ
1059 wstat = linux_wait_for_event (thread);
1060
1061 /* If we stopped with a non-SIGSTOP signal, save it for later
1062 and record the pending SIGSTOP. If the process exited, just
1063 return. */
1064 if (WIFSTOPPED (wstat)
1065 && WSTOPSIG (wstat) != SIGSTOP)
1066 {
1067 if (debug_threads)
24a09b5f
DJ
1068 fprintf (stderr, "LWP %ld stopped with non-sigstop status %06x\n",
1069 process->lwpid, wstat);
0d62e5e8
DJ
1070 process->status_pending_p = 1;
1071 process->status_pending = wstat;
1072 process->stop_expected = 1;
1073 }
1074
1075 if (linux_thread_alive (saved_tid))
1076 current_inferior = saved_inferior;
1077 else
1078 {
1079 if (debug_threads)
1080 fprintf (stderr, "Previously current thread died.\n");
1081
1082 /* Set a valid thread as current. */
1083 set_desired_inferior (0);
1084 }
1085}
1086
1087static void
1088stop_all_processes (void)
1089{
1090 stopping_threads = 1;
1091 for_each_inferior (&all_processes, send_sigstop);
1092 for_each_inferior (&all_processes, wait_for_sigstop);
1093 stopping_threads = 0;
1094}
1095
da6d8c04
DJ
1096/* Resume execution of the inferior process.
1097 If STEP is nonzero, single-step it.
1098 If SIGNAL is nonzero, give it that signal. */
1099
ce3a066d 1100static void
0d62e5e8 1101linux_resume_one_process (struct inferior_list_entry *entry,
32ca6d61 1102 int step, int signal, siginfo_t *info)
da6d8c04 1103{
0d62e5e8
DJ
1104 struct process_info *process = (struct process_info *) entry;
1105 struct thread_info *saved_inferior;
1106
1107 if (process->stopped == 0)
1108 return;
1109
1110 /* If we have pending signals or status, and a new signal, enqueue the
1111 signal. Also enqueue the signal if we are waiting to reinsert a
1112 breakpoint; it will be picked up again below. */
1113 if (signal != 0
1114 && (process->status_pending_p || process->pending_signals != NULL
1115 || process->bp_reinsert != 0))
1116 {
1117 struct pending_signals *p_sig;
1118 p_sig = malloc (sizeof (*p_sig));
1119 p_sig->prev = process->pending_signals;
1120 p_sig->signal = signal;
32ca6d61
DJ
1121 if (info == NULL)
1122 memset (&p_sig->info, 0, sizeof (siginfo_t));
1123 else
1124 memcpy (&p_sig->info, info, sizeof (siginfo_t));
0d62e5e8
DJ
1125 process->pending_signals = p_sig;
1126 }
1127
e5379b03 1128 if (process->status_pending_p && !check_removed_breakpoint (process))
0d62e5e8
DJ
1129 return;
1130
1131 saved_inferior = current_inferior;
1132 current_inferior = get_process_thread (process);
1133
1134 if (debug_threads)
a1928bad 1135 fprintf (stderr, "Resuming process %ld (%s, signal %d, stop %s)\n", inferior_pid,
0d62e5e8
DJ
1136 step ? "step" : "continue", signal,
1137 process->stop_expected ? "expected" : "not expected");
1138
1139 /* This bit needs some thinking about. If we get a signal that
1140 we must report while a single-step reinsert is still pending,
1141 we often end up resuming the thread. It might be better to
1142 (ew) allow a stack of pending events; then we could be sure that
1143 the reinsert happened right away and not lose any signals.
1144
1145 Making this stack would also shrink the window in which breakpoints are
1146 uninserted (see comment in linux_wait_for_process) but not enough for
1147 complete correctness, so it won't solve that problem. It may be
1148 worthwhile just to solve this one, however. */
1149 if (process->bp_reinsert != 0)
1150 {
1151 if (debug_threads)
1152 fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert);
1153 if (step == 0)
1154 fprintf (stderr, "BAD - reinserting but not stepping.\n");
1155 step = 1;
1156
1157 /* Postpone any pending signal. It was enqueued above. */
1158 signal = 0;
1159 }
1160
1161 check_removed_breakpoint (process);
1162
aa691b87 1163 if (debug_threads && the_low_target.get_pc != NULL)
0d62e5e8
DJ
1164 {
1165 fprintf (stderr, " ");
52fb6437 1166 (*the_low_target.get_pc) ();
0d62e5e8
DJ
1167 }
1168
1169 /* If we have pending signals, consume one unless we are trying to reinsert
1170 a breakpoint. */
1171 if (process->pending_signals != NULL && process->bp_reinsert == 0)
1172 {
1173 struct pending_signals **p_sig;
1174
1175 p_sig = &process->pending_signals;
1176 while ((*p_sig)->prev != NULL)
1177 p_sig = &(*p_sig)->prev;
1178
1179 signal = (*p_sig)->signal;
32ca6d61
DJ
1180 if ((*p_sig)->info.si_signo != 0)
1181 ptrace (PTRACE_SETSIGINFO, process->lwpid, 0, &(*p_sig)->info);
1182
0d62e5e8
DJ
1183 free (*p_sig);
1184 *p_sig = NULL;
1185 }
1186
1187 regcache_invalidate_one ((struct inferior_list_entry *)
1188 get_process_thread (process));
da6d8c04 1189 errno = 0;
0d62e5e8
DJ
1190 process->stopped = 0;
1191 process->stepping = step;
1192 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal);
1193
1194 current_inferior = saved_inferior;
da6d8c04
DJ
1195 if (errno)
1196 perror_with_name ("ptrace");
1197}
1198
64386c31
DJ
1199static struct thread_resume *resume_ptr;
1200
1201/* This function is called once per thread. We look up the thread
5544ad89
DJ
1202 in RESUME_PTR, and mark the thread with a pointer to the appropriate
1203 resume request.
1204
1205 This algorithm is O(threads * resume elements), but resume elements
1206 is small (and will remain small at least until GDB supports thread
1207 suspension). */
0d62e5e8 1208static void
5544ad89 1209linux_set_resume_request (struct inferior_list_entry *entry)
0d62e5e8
DJ
1210{
1211 struct process_info *process;
64386c31 1212 struct thread_info *thread;
5544ad89 1213 int ndx;
64386c31
DJ
1214
1215 thread = (struct thread_info *) entry;
1216 process = get_thread_process (thread);
1217
1218 ndx = 0;
1219 while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id)
1220 ndx++;
1221
5544ad89
DJ
1222 process->resume = &resume_ptr[ndx];
1223}
1224
1225/* This function is called once per thread. We check the thread's resume
1226 request, which will tell us whether to resume, step, or leave the thread
1227 stopped; and what signal, if any, it should be sent. For threads which
1228 we aren't explicitly told otherwise, we preserve the stepping flag; this
1229 is used for stepping over gdbserver-placed breakpoints. */
1230
1231static void
1232linux_continue_one_thread (struct inferior_list_entry *entry)
1233{
1234 struct process_info *process;
1235 struct thread_info *thread;
1236 int step;
1237
1238 thread = (struct thread_info *) entry;
1239 process = get_thread_process (thread);
1240
1241 if (process->resume->leave_stopped)
64386c31
DJ
1242 return;
1243
5544ad89
DJ
1244 if (process->resume->thread == -1)
1245 step = process->stepping || process->resume->step;
64386c31 1246 else
5544ad89
DJ
1247 step = process->resume->step;
1248
32ca6d61 1249 linux_resume_one_process (&process->head, step, process->resume->sig, NULL);
c6ecbae5 1250
5544ad89
DJ
1251 process->resume = NULL;
1252}
1253
1254/* This function is called once per thread. We check the thread's resume
1255 request, which will tell us whether to resume, step, or leave the thread
1256 stopped; and what signal, if any, it should be sent. We queue any needed
1257 signals, since we won't actually resume. We already have a pending event
1258 to report, so we don't need to preserve any step requests; they should
1259 be re-issued if necessary. */
1260
1261static void
1262linux_queue_one_thread (struct inferior_list_entry *entry)
1263{
1264 struct process_info *process;
1265 struct thread_info *thread;
1266
1267 thread = (struct thread_info *) entry;
1268 process = get_thread_process (thread);
1269
1270 if (process->resume->leave_stopped)
1271 return;
1272
1273 /* If we have a new signal, enqueue the signal. */
1274 if (process->resume->sig != 0)
1275 {
1276 struct pending_signals *p_sig;
1277 p_sig = malloc (sizeof (*p_sig));
1278 p_sig->prev = process->pending_signals;
1279 p_sig->signal = process->resume->sig;
32ca6d61
DJ
1280 memset (&p_sig->info, 0, sizeof (siginfo_t));
1281
1282 /* If this is the same signal we were previously stopped by,
1283 make sure to queue its siginfo. We can ignore the return
1284 value of ptrace; if it fails, we'll skip
1285 PTRACE_SETSIGINFO. */
1286 if (WIFSTOPPED (process->last_status)
1287 && WSTOPSIG (process->last_status) == process->resume->sig)
1288 ptrace (PTRACE_GETSIGINFO, process->lwpid, 0, &p_sig->info);
1289
5544ad89
DJ
1290 process->pending_signals = p_sig;
1291 }
1292
1293 process->resume = NULL;
1294}
1295
1296/* Set DUMMY if this process has an interesting status pending. */
1297static int
1298resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
1299{
1300 struct process_info *process = (struct process_info *) entry;
1301
1302 /* Processes which will not be resumed are not interesting, because
1303 we might not wait for them next time through linux_wait. */
1304 if (process->resume->leave_stopped)
1305 return 0;
1306
1307 /* If this thread has a removed breakpoint, we won't have any
1308 events to report later, so check now. check_removed_breakpoint
1309 may clear status_pending_p. We avoid calling check_removed_breakpoint
1310 for any thread that we are not otherwise going to resume - this
1311 lets us preserve stopped status when two threads hit a breakpoint.
1312 GDB removes the breakpoint to single-step a particular thread
1313 past it, then re-inserts it and resumes all threads. We want
1314 to report the second thread without resuming it in the interim. */
1315 if (process->status_pending_p)
1316 check_removed_breakpoint (process);
1317
1318 if (process->status_pending_p)
1319 * (int *) flag_p = 1;
1320
1321 return 0;
0d62e5e8
DJ
1322}
1323
1324static void
64386c31 1325linux_resume (struct thread_resume *resume_info)
0d62e5e8 1326{
5544ad89 1327 int pending_flag;
c6ecbae5 1328
5544ad89 1329 /* Yes, the use of a global here is rather ugly. */
64386c31 1330 resume_ptr = resume_info;
5544ad89
DJ
1331
1332 for_each_inferior (&all_threads, linux_set_resume_request);
1333
1334 /* If there is a thread which would otherwise be resumed, which
1335 has a pending status, then don't resume any threads - we can just
1336 report the pending status. Make sure to queue any signals
1337 that would otherwise be sent. */
1338 pending_flag = 0;
1339 find_inferior (&all_processes, resume_status_pending_p, &pending_flag);
1340
1341 if (debug_threads)
1342 {
1343 if (pending_flag)
1344 fprintf (stderr, "Not resuming, pending status\n");
1345 else
1346 fprintf (stderr, "Resuming, no pending status\n");
1347 }
1348
1349 if (pending_flag)
1350 for_each_inferior (&all_threads, linux_queue_one_thread);
1351 else
a20d5e98 1352 for_each_inferior (&all_threads, linux_continue_one_thread);
0d62e5e8
DJ
1353}
1354
1355#ifdef HAVE_LINUX_USRREGS
da6d8c04
DJ
1356
1357int
0a30fbc4 1358register_addr (int regnum)
da6d8c04
DJ
1359{
1360 int addr;
1361
2ec06d2e 1362 if (regnum < 0 || regnum >= the_low_target.num_regs)
da6d8c04
DJ
1363 error ("Invalid register number %d.", regnum);
1364
2ec06d2e 1365 addr = the_low_target.regmap[regnum];
da6d8c04
DJ
1366
1367 return addr;
1368}
1369
58caa3dc 1370/* Fetch one register. */
da6d8c04
DJ
1371static void
1372fetch_register (int regno)
1373{
1374 CORE_ADDR regaddr;
48d93c75 1375 int i, size;
0d62e5e8 1376 char *buf;
da6d8c04 1377
2ec06d2e 1378 if (regno >= the_low_target.num_regs)
0a30fbc4 1379 return;
2ec06d2e 1380 if ((*the_low_target.cannot_fetch_register) (regno))
0a30fbc4 1381 return;
da6d8c04 1382
0a30fbc4
DJ
1383 regaddr = register_addr (regno);
1384 if (regaddr == -1)
1385 return;
48d93c75
UW
1386 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
1387 & - sizeof (PTRACE_XFER_TYPE);
1388 buf = alloca (size);
1389 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04
DJ
1390 {
1391 errno = 0;
0d62e5e8 1392 *(PTRACE_XFER_TYPE *) (buf + i) =
da6d8c04
DJ
1393 ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
1394 regaddr += sizeof (PTRACE_XFER_TYPE);
1395 if (errno != 0)
1396 {
1397 /* Warning, not error, in case we are attached; sometimes the
1398 kernel doesn't let us at the registers. */
1399 char *err = strerror (errno);
1400 char *msg = alloca (strlen (err) + 128);
1401 sprintf (msg, "reading register %d: %s", regno, err);
1402 error (msg);
1403 goto error_exit;
1404 }
1405 }
ee1a7ae4
UW
1406
1407 if (the_low_target.supply_ptrace_register)
1408 the_low_target.supply_ptrace_register (regno, buf);
5a1f5858
DJ
1409 else
1410 supply_register (regno, buf);
0d62e5e8 1411
da6d8c04
DJ
1412error_exit:;
1413}
1414
1415/* Fetch all registers, or just one, from the child process. */
58caa3dc
DJ
1416static void
1417usr_fetch_inferior_registers (int regno)
da6d8c04
DJ
1418{
1419 if (regno == -1 || regno == 0)
2ec06d2e 1420 for (regno = 0; regno < the_low_target.num_regs; regno++)
da6d8c04
DJ
1421 fetch_register (regno);
1422 else
1423 fetch_register (regno);
1424}
1425
1426/* Store our register values back into the inferior.
1427 If REGNO is -1, do this for all registers.
1428 Otherwise, REGNO specifies which register (so we can save time). */
58caa3dc
DJ
1429static void
1430usr_store_inferior_registers (int regno)
da6d8c04
DJ
1431{
1432 CORE_ADDR regaddr;
48d93c75 1433 int i, size;
0d62e5e8 1434 char *buf;
da6d8c04
DJ
1435
1436 if (regno >= 0)
1437 {
2ec06d2e 1438 if (regno >= the_low_target.num_regs)
0a30fbc4
DJ
1439 return;
1440
bc1e36ca 1441 if ((*the_low_target.cannot_store_register) (regno) == 1)
0a30fbc4
DJ
1442 return;
1443
1444 regaddr = register_addr (regno);
1445 if (regaddr == -1)
da6d8c04 1446 return;
da6d8c04 1447 errno = 0;
48d93c75
UW
1448 size = (register_size (regno) + sizeof (PTRACE_XFER_TYPE) - 1)
1449 & - sizeof (PTRACE_XFER_TYPE);
1450 buf = alloca (size);
1451 memset (buf, 0, size);
ee1a7ae4
UW
1452
1453 if (the_low_target.collect_ptrace_register)
1454 the_low_target.collect_ptrace_register (regno, buf);
5a1f5858
DJ
1455 else
1456 collect_register (regno, buf);
ee1a7ae4 1457
48d93c75 1458 for (i = 0; i < size; i += sizeof (PTRACE_XFER_TYPE))
da6d8c04 1459 {
0a30fbc4
DJ
1460 errno = 0;
1461 ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
2ff29de4 1462 *(PTRACE_XFER_TYPE *) (buf + i));
da6d8c04
DJ
1463 if (errno != 0)
1464 {
bc1e36ca
DJ
1465 if ((*the_low_target.cannot_store_register) (regno) == 0)
1466 {
1467 char *err = strerror (errno);
1468 char *msg = alloca (strlen (err) + 128);
1469 sprintf (msg, "writing register %d: %s",
1470 regno, err);
1471 error (msg);
1472 return;
1473 }
da6d8c04 1474 }
2ff29de4 1475 regaddr += sizeof (PTRACE_XFER_TYPE);
da6d8c04 1476 }
da6d8c04
DJ
1477 }
1478 else
2ec06d2e 1479 for (regno = 0; regno < the_low_target.num_regs; regno++)
0d62e5e8 1480 usr_store_inferior_registers (regno);
da6d8c04 1481}
58caa3dc
DJ
1482#endif /* HAVE_LINUX_USRREGS */
1483
1484
1485
1486#ifdef HAVE_LINUX_REGSETS
1487
1488static int
0d62e5e8 1489regsets_fetch_inferior_registers ()
58caa3dc
DJ
1490{
1491 struct regset_info *regset;
e9d25b98 1492 int saw_general_regs = 0;
58caa3dc
DJ
1493
1494 regset = target_regsets;
1495
1496 while (regset->size >= 0)
1497 {
1498 void *buf;
1499 int res;
1500
52fa2412 1501 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
58caa3dc
DJ
1502 {
1503 regset ++;
1504 continue;
1505 }
1506
1507 buf = malloc (regset->size);
d06f167a 1508 res = ptrace (regset->get_request, inferior_pid, 0, buf);
58caa3dc
DJ
1509 if (res < 0)
1510 {
1511 if (errno == EIO)
1512 {
52fa2412
UW
1513 /* If we get EIO on a regset, do not try it again for
1514 this process. */
1515 disabled_regsets[regset - target_regsets] = 1;
1516 continue;
58caa3dc
DJ
1517 }
1518 else
1519 {
0d62e5e8 1520 char s[256];
a1928bad 1521 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%ld",
0d62e5e8
DJ
1522 inferior_pid);
1523 perror (s);
58caa3dc
DJ
1524 }
1525 }
e9d25b98
DJ
1526 else if (regset->type == GENERAL_REGS)
1527 saw_general_regs = 1;
58caa3dc
DJ
1528 regset->store_function (buf);
1529 regset ++;
1530 }
e9d25b98
DJ
1531 if (saw_general_regs)
1532 return 0;
1533 else
1534 return 1;
58caa3dc
DJ
1535}
1536
1537static int
0d62e5e8 1538regsets_store_inferior_registers ()
58caa3dc
DJ
1539{
1540 struct regset_info *regset;
e9d25b98 1541 int saw_general_regs = 0;
58caa3dc
DJ
1542
1543 regset = target_regsets;
1544
1545 while (regset->size >= 0)
1546 {
1547 void *buf;
1548 int res;
1549
52fa2412 1550 if (regset->size == 0 || disabled_regsets[regset - target_regsets])
58caa3dc
DJ
1551 {
1552 regset ++;
1553 continue;
1554 }
1555
1556 buf = malloc (regset->size);
545587ee
DJ
1557
1558 /* First fill the buffer with the current register set contents,
1559 in case there are any items in the kernel's regset that are
1560 not in gdbserver's regcache. */
1561 res = ptrace (regset->get_request, inferior_pid, 0, buf);
1562
1563 if (res == 0)
1564 {
1565 /* Then overlay our cached registers on that. */
1566 regset->fill_function (buf);
1567
1568 /* Only now do we write the register set. */
1569 res = ptrace (regset->set_request, inferior_pid, 0, buf);
1570 }
1571
58caa3dc
DJ
1572 if (res < 0)
1573 {
1574 if (errno == EIO)
1575 {
52fa2412
UW
1576 /* If we get EIO on a regset, do not try it again for
1577 this process. */
1578 disabled_regsets[regset - target_regsets] = 1;
1579 continue;
58caa3dc
DJ
1580 }
1581 else
1582 {
ce3a066d 1583 perror ("Warning: ptrace(regsets_store_inferior_registers)");
58caa3dc
DJ
1584 }
1585 }
e9d25b98
DJ
1586 else if (regset->type == GENERAL_REGS)
1587 saw_general_regs = 1;
58caa3dc 1588 regset ++;
09ec9b38 1589 free (buf);
58caa3dc 1590 }
e9d25b98
DJ
1591 if (saw_general_regs)
1592 return 0;
1593 else
1594 return 1;
ce3a066d 1595 return 0;
58caa3dc
DJ
1596}
1597
1598#endif /* HAVE_LINUX_REGSETS */
1599
1600
1601void
ce3a066d 1602linux_fetch_registers (int regno)
58caa3dc
DJ
1603{
1604#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
1605 if (regsets_fetch_inferior_registers () == 0)
1606 return;
58caa3dc
DJ
1607#endif
1608#ifdef HAVE_LINUX_USRREGS
1609 usr_fetch_inferior_registers (regno);
1610#endif
1611}
1612
1613void
ce3a066d 1614linux_store_registers (int regno)
58caa3dc
DJ
1615{
1616#ifdef HAVE_LINUX_REGSETS
52fa2412
UW
1617 if (regsets_store_inferior_registers () == 0)
1618 return;
58caa3dc
DJ
1619#endif
1620#ifdef HAVE_LINUX_USRREGS
1621 usr_store_inferior_registers (regno);
1622#endif
1623}
1624
da6d8c04 1625
da6d8c04
DJ
1626/* Copy LEN bytes from inferior's memory starting at MEMADDR
1627 to debugger memory starting at MYADDR. */
1628
c3e735a6 1629static int
f450004a 1630linux_read_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len)
da6d8c04
DJ
1631{
1632 register int i;
1633 /* Round starting address down to longword boundary. */
1634 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1635 /* Round ending address up; get number of longwords that makes. */
aa691b87
RM
1636 register int count
1637 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
da6d8c04
DJ
1638 / sizeof (PTRACE_XFER_TYPE);
1639 /* Allocate buffer of that many longwords. */
aa691b87 1640 register PTRACE_XFER_TYPE *buffer
da6d8c04 1641 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
fd462a61
DJ
1642 int fd;
1643 char filename[64];
1644
1645 /* Try using /proc. Don't bother for one word. */
1646 if (len >= 3 * sizeof (long))
1647 {
1648 /* We could keep this file open and cache it - possibly one per
1649 thread. That requires some juggling, but is even faster. */
1650 sprintf (filename, "/proc/%ld/mem", inferior_pid);
1651 fd = open (filename, O_RDONLY | O_LARGEFILE);
1652 if (fd == -1)
1653 goto no_proc;
1654
1655 /* If pread64 is available, use it. It's faster if the kernel
1656 supports it (only one syscall), and it's 64-bit safe even on
1657 32-bit platforms (for instance, SPARC debugging a SPARC64
1658 application). */
1659#ifdef HAVE_PREAD64
1660 if (pread64 (fd, myaddr, len, memaddr) != len)
1661#else
1662 if (lseek (fd, memaddr, SEEK_SET) == -1 || read (fd, memaddr, len) != len)
1663#endif
1664 {
1665 close (fd);
1666 goto no_proc;
1667 }
1668
1669 close (fd);
1670 return 0;
1671 }
da6d8c04 1672
fd462a61 1673 no_proc:
da6d8c04
DJ
1674 /* Read all the longwords */
1675 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1676 {
c3e735a6 1677 errno = 0;
d844cde6 1678 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
c3e735a6
DJ
1679 if (errno)
1680 return errno;
da6d8c04
DJ
1681 }
1682
1683 /* Copy appropriate bytes out of the buffer. */
1684 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
c3e735a6
DJ
1685
1686 return 0;
da6d8c04
DJ
1687}
1688
1689/* Copy LEN bytes of data from debugger memory at MYADDR
1690 to inferior's memory at MEMADDR.
1691 On failure (cannot write the inferior)
1692 returns the value of errno. */
1693
ce3a066d 1694static int
f450004a 1695linux_write_memory (CORE_ADDR memaddr, const unsigned char *myaddr, int len)
da6d8c04
DJ
1696{
1697 register int i;
1698 /* Round starting address down to longword boundary. */
1699 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1700 /* Round ending address up; get number of longwords that makes. */
1701 register int count
1702 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
1703 /* Allocate buffer of that many longwords. */
1704 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
da6d8c04 1705
0d62e5e8
DJ
1706 if (debug_threads)
1707 {
1708 fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
1709 }
1710
da6d8c04
DJ
1711 /* Fill start and end extra bytes of buffer with existing memory data. */
1712
d844cde6
DJ
1713 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
1714 (PTRACE_ARG3_TYPE) addr, 0);
da6d8c04
DJ
1715
1716 if (count > 1)
1717 {
1718 buffer[count - 1]
1719 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
d844cde6
DJ
1720 (PTRACE_ARG3_TYPE) (addr + (count - 1)
1721 * sizeof (PTRACE_XFER_TYPE)),
1722 0);
da6d8c04
DJ
1723 }
1724
1725 /* Copy data to be written over corresponding part of buffer */
1726
1727 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
1728
1729 /* Write the entire buffer. */
1730
1731 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1732 {
1733 errno = 0;
d844cde6 1734 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
da6d8c04
DJ
1735 if (errno)
1736 return errno;
1737 }
1738
1739 return 0;
1740}
2f2893d9 1741
24a09b5f
DJ
1742static int linux_supports_tracefork_flag;
1743
51c2684e 1744/* Helper functions for linux_test_for_tracefork, called via clone (). */
24a09b5f 1745
51c2684e
DJ
1746static int
1747linux_tracefork_grandchild (void *arg)
1748{
1749 _exit (0);
1750}
1751
7407e2de
AS
1752#define STACK_SIZE 4096
1753
51c2684e
DJ
1754static int
1755linux_tracefork_child (void *arg)
24a09b5f
DJ
1756{
1757 ptrace (PTRACE_TRACEME, 0, 0, 0);
1758 kill (getpid (), SIGSTOP);
7407e2de
AS
1759#ifdef __ia64__
1760 __clone2 (linux_tracefork_grandchild, arg, STACK_SIZE,
1761 CLONE_VM | SIGCHLD, NULL);
1762#else
1763 clone (linux_tracefork_grandchild, arg + STACK_SIZE,
1764 CLONE_VM | SIGCHLD, NULL);
1765#endif
24a09b5f
DJ
1766 _exit (0);
1767}
1768
1769/* Wrapper function for waitpid which handles EINTR. */
1770
1771static int
1772my_waitpid (int pid, int *status, int flags)
1773{
1774 int ret;
1775 do
1776 {
1777 ret = waitpid (pid, status, flags);
1778 }
1779 while (ret == -1 && errno == EINTR);
1780
1781 return ret;
1782}
1783
1784/* Determine if PTRACE_O_TRACEFORK can be used to follow fork events. Make
1785 sure that we can enable the option, and that it had the desired
1786 effect. */
1787
1788static void
1789linux_test_for_tracefork (void)
1790{
1791 int child_pid, ret, status;
1792 long second_pid;
7407e2de 1793 char *stack = malloc (STACK_SIZE * 4);
24a09b5f
DJ
1794
1795 linux_supports_tracefork_flag = 0;
1796
51c2684e 1797 /* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
7407e2de
AS
1798#ifdef __ia64__
1799 child_pid = __clone2 (linux_tracefork_child, stack, STACK_SIZE,
1800 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
1801#else
1802 child_pid = clone (linux_tracefork_child, stack + STACK_SIZE,
1803 CLONE_VM | SIGCHLD, stack + STACK_SIZE * 2);
1804#endif
24a09b5f 1805 if (child_pid == -1)
51c2684e 1806 perror_with_name ("clone");
24a09b5f
DJ
1807
1808 ret = my_waitpid (child_pid, &status, 0);
1809 if (ret == -1)
1810 perror_with_name ("waitpid");
1811 else if (ret != child_pid)
1812 error ("linux_test_for_tracefork: waitpid: unexpected result %d.", ret);
1813 if (! WIFSTOPPED (status))
1814 error ("linux_test_for_tracefork: waitpid: unexpected status %d.", status);
1815
1816 ret = ptrace (PTRACE_SETOPTIONS, child_pid, 0, PTRACE_O_TRACEFORK);
1817 if (ret != 0)
1818 {
1819 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
1820 if (ret != 0)
1821 {
1822 warning ("linux_test_for_tracefork: failed to kill child");
1823 return;
1824 }
1825
1826 ret = my_waitpid (child_pid, &status, 0);
1827 if (ret != child_pid)
1828 warning ("linux_test_for_tracefork: failed to wait for killed child");
1829 else if (!WIFSIGNALED (status))
1830 warning ("linux_test_for_tracefork: unexpected wait status 0x%x from "
1831 "killed child", status);
1832
1833 return;
1834 }
1835
1836 ret = ptrace (PTRACE_CONT, child_pid, 0, 0);
1837 if (ret != 0)
1838 warning ("linux_test_for_tracefork: failed to resume child");
1839
1840 ret = my_waitpid (child_pid, &status, 0);
1841
1842 if (ret == child_pid && WIFSTOPPED (status)
1843 && status >> 16 == PTRACE_EVENT_FORK)
1844 {
1845 second_pid = 0;
1846 ret = ptrace (PTRACE_GETEVENTMSG, child_pid, 0, &second_pid);
1847 if (ret == 0 && second_pid != 0)
1848 {
1849 int second_status;
1850
1851 linux_supports_tracefork_flag = 1;
1852 my_waitpid (second_pid, &second_status, 0);
1853 ret = ptrace (PTRACE_KILL, second_pid, 0, 0);
1854 if (ret != 0)
1855 warning ("linux_test_for_tracefork: failed to kill second child");
1856 my_waitpid (second_pid, &status, 0);
1857 }
1858 }
1859 else
1860 warning ("linux_test_for_tracefork: unexpected result from waitpid "
1861 "(%d, status 0x%x)", ret, status);
1862
1863 do
1864 {
1865 ret = ptrace (PTRACE_KILL, child_pid, 0, 0);
1866 if (ret != 0)
1867 warning ("linux_test_for_tracefork: failed to kill child");
1868 my_waitpid (child_pid, &status, 0);
1869 }
1870 while (WIFSTOPPED (status));
51c2684e
DJ
1871
1872 free (stack);
24a09b5f
DJ
1873}
1874
1875
2f2893d9
DJ
1876static void
1877linux_look_up_symbols (void)
1878{
0d62e5e8 1879#ifdef USE_THREAD_DB
24a09b5f 1880 if (thread_db_active)
0d62e5e8
DJ
1881 return;
1882
24a09b5f 1883 thread_db_active = thread_db_init (!linux_supports_tracefork_flag);
0d62e5e8
DJ
1884#endif
1885}
1886
e5379b03 1887static void
ef57601b 1888linux_request_interrupt (void)
e5379b03 1889{
a1928bad 1890 extern unsigned long signal_pid;
e5379b03 1891
d592fa2f 1892 if (cont_thread != 0 && cont_thread != -1)
e5379b03
DJ
1893 {
1894 struct process_info *process;
1895
1896 process = get_thread_process (current_inferior);
ef57601b 1897 kill_lwp (process->lwpid, SIGINT);
e5379b03
DJ
1898 }
1899 else
ef57601b 1900 kill_lwp (signal_pid, SIGINT);
e5379b03
DJ
1901}
1902
aa691b87
RM
1903/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1904 to debugger memory starting at MYADDR. */
1905
1906static int
f450004a 1907linux_read_auxv (CORE_ADDR offset, unsigned char *myaddr, unsigned int len)
aa691b87
RM
1908{
1909 char filename[PATH_MAX];
1910 int fd, n;
1911
a1928bad 1912 snprintf (filename, sizeof filename, "/proc/%ld/auxv", inferior_pid);
aa691b87
RM
1913
1914 fd = open (filename, O_RDONLY);
1915 if (fd < 0)
1916 return -1;
1917
1918 if (offset != (CORE_ADDR) 0
1919 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
1920 n = -1;
1921 else
1922 n = read (fd, myaddr, len);
1923
1924 close (fd);
1925
1926 return n;
1927}
1928
e013ee27
OF
1929/* These watchpoint related wrapper functions simply pass on the function call
1930 if the target has registered a corresponding function. */
1931
1932static int
1933linux_insert_watchpoint (char type, CORE_ADDR addr, int len)
1934{
1935 if (the_low_target.insert_watchpoint != NULL)
1936 return the_low_target.insert_watchpoint (type, addr, len);
1937 else
1938 /* Unsupported (see target.h). */
1939 return 1;
1940}
1941
1942static int
1943linux_remove_watchpoint (char type, CORE_ADDR addr, int len)
1944{
1945 if (the_low_target.remove_watchpoint != NULL)
1946 return the_low_target.remove_watchpoint (type, addr, len);
1947 else
1948 /* Unsupported (see target.h). */
1949 return 1;
1950}
1951
1952static int
1953linux_stopped_by_watchpoint (void)
1954{
1955 if (the_low_target.stopped_by_watchpoint != NULL)
1956 return the_low_target.stopped_by_watchpoint ();
1957 else
1958 return 0;
1959}
1960
1961static CORE_ADDR
1962linux_stopped_data_address (void)
1963{
1964 if (the_low_target.stopped_data_address != NULL)
1965 return the_low_target.stopped_data_address ();
1966 else
1967 return 0;
1968}
1969
42c81e2a 1970#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437
NS
1971#if defined(__mcoldfire__)
1972/* These should really be defined in the kernel's ptrace.h header. */
1973#define PT_TEXT_ADDR 49*4
1974#define PT_DATA_ADDR 50*4
1975#define PT_TEXT_END_ADDR 51*4
1976#endif
1977
1978/* Under uClinux, programs are loaded at non-zero offsets, which we need
1979 to tell gdb about. */
1980
1981static int
1982linux_read_offsets (CORE_ADDR *text_p, CORE_ADDR *data_p)
1983{
1984#if defined(PT_TEXT_ADDR) && defined(PT_DATA_ADDR) && defined(PT_TEXT_END_ADDR)
1985 unsigned long text, text_end, data;
1986 int pid = get_thread_process (current_inferior)->head.id;
1987
1988 errno = 0;
1989
1990 text = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_ADDR, 0);
1991 text_end = ptrace (PTRACE_PEEKUSER, pid, (long)PT_TEXT_END_ADDR, 0);
1992 data = ptrace (PTRACE_PEEKUSER, pid, (long)PT_DATA_ADDR, 0);
1993
1994 if (errno == 0)
1995 {
1996 /* Both text and data offsets produced at compile-time (and so
1997 used by gdb) are relative to the beginning of the program,
1998 with the data segment immediately following the text segment.
1999 However, the actual runtime layout in memory may put the data
2000 somewhere else, so when we send gdb a data base-address, we
2001 use the real data base address and subtract the compile-time
2002 data base-address from it (which is just the length of the
2003 text segment). BSS immediately follows data in both
2004 cases. */
2005 *text_p = text;
2006 *data_p = data - (text_end - text);
2007
2008 return 1;
2009 }
2010#endif
2011 return 0;
2012}
2013#endif
2014
ce3a066d
DJ
2015static struct target_ops linux_target_ops = {
2016 linux_create_inferior,
2017 linux_attach,
2018 linux_kill,
6ad8ae5c 2019 linux_detach,
444d6139 2020 linux_join,
ce3a066d
DJ
2021 linux_thread_alive,
2022 linux_resume,
2023 linux_wait,
2024 linux_fetch_registers,
2025 linux_store_registers,
2026 linux_read_memory,
2027 linux_write_memory,
2f2893d9 2028 linux_look_up_symbols,
ef57601b 2029 linux_request_interrupt,
aa691b87 2030 linux_read_auxv,
e013ee27
OF
2031 linux_insert_watchpoint,
2032 linux_remove_watchpoint,
2033 linux_stopped_by_watchpoint,
2034 linux_stopped_data_address,
42c81e2a 2035#if defined(__UCLIBC__) && defined(HAS_NOMMU)
52fb6437 2036 linux_read_offsets,
dae5f5cf
DJ
2037#else
2038 NULL,
2039#endif
2040#ifdef USE_THREAD_DB
2041 thread_db_get_tls_address,
2042#else
2043 NULL,
52fb6437 2044#endif
59a016f0
PA
2045 NULL,
2046 hostio_last_error_from_errno,
ce3a066d
DJ
2047};
2048
0d62e5e8
DJ
2049static void
2050linux_init_signals ()
2051{
2052 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
2053 to find what the cancel signal actually is. */
254787d4 2054 signal (__SIGRTMIN+1, SIG_IGN);
0d62e5e8
DJ
2055}
2056
da6d8c04
DJ
2057void
2058initialize_low (void)
2059{
24a09b5f 2060 thread_db_active = 0;
ce3a066d 2061 set_target_ops (&linux_target_ops);
611cb4a5
DJ
2062 set_breakpoint_data (the_low_target.breakpoint,
2063 the_low_target.breakpoint_len);
0d62e5e8 2064 linux_init_signals ();
24a09b5f 2065 linux_test_for_tracefork ();
52fa2412
UW
2066#ifdef HAVE_LINUX_REGSETS
2067 for (num_regsets = 0; target_regsets[num_regsets].size >= 0; num_regsets++)
2068 ;
2069 disabled_regsets = malloc (num_regsets);
2070#endif
da6d8c04 2071}