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