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