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