]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/linux-low.c
daily update
[thirdparty/binutils-gdb.git] / gdb / gdbserver / linux-low.c
CommitLineData
da6d8c04
DJ
1/* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 2 of the License, or
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
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "server.h"
58caa3dc 23#include "linux-low.h"
da6d8c04 24
58caa3dc 25#include <sys/wait.h>
da6d8c04
DJ
26#include <stdio.h>
27#include <sys/param.h>
28#include <sys/dir.h>
29#include <sys/ptrace.h>
30#include <sys/user.h>
31#include <signal.h>
32#include <sys/ioctl.h>
33#include <fcntl.h>
d07c63e7 34#include <string.h>
0a30fbc4
DJ
35#include <stdlib.h>
36#include <unistd.h>
da6d8c04 37
0d62e5e8
DJ
38/* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
39 however. This requires changing the ID in place when we go from !using_threads
40 to using_threads, immediately.
611cb4a5 41
0d62e5e8
DJ
42 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
43 the same as the LWP ID. */
44
45struct inferior_list all_processes;
46
47/* FIXME this is a bit of a hack, and could be removed. */
48int stopping_threads;
49
50/* FIXME make into a target method? */
51int using_threads;
52
53static void linux_resume_one_process (struct inferior_list_entry *entry,
54 int step, int signal);
611cb4a5 55static void linux_resume (int step, int signal);
0d62e5e8
DJ
56static void stop_all_processes (void);
57static int linux_wait_for_event (struct thread_info *child);
58
59struct pending_signals
60{
61 int signal;
62 struct pending_signals *prev;
63};
611cb4a5 64
d844cde6 65#define PTRACE_ARG3_TYPE long
c6ecbae5 66#define PTRACE_XFER_TYPE long
da6d8c04 67
58caa3dc
DJ
68#ifdef HAVE_LINUX_REGSETS
69static int use_regsets_p = 1;
70#endif
71
da6d8c04 72extern int errno;
c6ecbae5 73
0d62e5e8
DJ
74int debug_threads = 0;
75
76#define pid_of(proc) ((proc)->head.id)
77
78/* FIXME: Delete eventually. */
79#define inferior_pid (pid_of (get_thread_process (current_inferior)))
80
81/* This function should only be called if the process got a SIGTRAP.
82 The SIGTRAP could mean several things.
83
84 On i386, where decr_pc_after_break is non-zero:
85 If we were single-stepping this process using PTRACE_SINGLESTEP,
86 we will get only the one SIGTRAP (even if the instruction we
87 stepped over was a breakpoint). The value of $eip will be the
88 next instruction.
89 If we continue the process using PTRACE_CONT, we will get a
90 SIGTRAP when we hit a breakpoint. The value of $eip will be
91 the instruction after the breakpoint (i.e. needs to be
92 decremented). If we report the SIGTRAP to GDB, we must also
93 report the undecremented PC. If we cancel the SIGTRAP, we
94 must resume at the decremented PC.
95
96 (Presumably, not yet tested) On a non-decr_pc_after_break machine
97 with hardware or kernel single-step:
98 If we single-step over a breakpoint instruction, our PC will
99 point at the following instruction. If we continue and hit a
100 breakpoint instruction, our PC will point at the breakpoint
101 instruction. */
102
103static CORE_ADDR
104get_stop_pc (void)
105{
106 CORE_ADDR stop_pc = (*the_low_target.get_pc) ();
107
108 if (get_thread_process (current_inferior)->stepping)
109 return stop_pc;
110 else
111 return stop_pc - the_low_target.decr_pc_after_break;
112}
ce3a066d 113
0d62e5e8
DJ
114static void *
115add_process (int pid)
611cb4a5 116{
0d62e5e8
DJ
117 struct process_info *process;
118
119 process = (struct process_info *) malloc (sizeof (*process));
120 memset (process, 0, sizeof (*process));
121
122 process->head.id = pid;
123
124 /* Default to tid == lwpid == pid. */
125 process->tid = pid;
126 process->lwpid = pid;
127
128 add_inferior_to_list (&all_processes, &process->head);
129
130 return process;
131}
611cb4a5 132
da6d8c04
DJ
133/* Start an inferior process and returns its pid.
134 ALLARGS is a vector of program-name and args. */
135
ce3a066d
DJ
136static int
137linux_create_inferior (char *program, char **allargs)
da6d8c04 138{
0d62e5e8 139 void *new_process;
da6d8c04
DJ
140 int pid;
141
142 pid = fork ();
143 if (pid < 0)
144 perror_with_name ("fork");
145
146 if (pid == 0)
147 {
148 ptrace (PTRACE_TRACEME, 0, 0, 0);
149
254787d4 150 signal (__SIGRTMIN + 1, SIG_DFL);
0d62e5e8 151
a9fa9f7d
DJ
152 setpgid (0, 0);
153
da6d8c04
DJ
154 execv (program, allargs);
155
156 fprintf (stderr, "Cannot exec %s: %s.\n", program,
d07c63e7 157 strerror (errno));
da6d8c04
DJ
158 fflush (stderr);
159 _exit (0177);
160 }
161
0d62e5e8
DJ
162 new_process = add_process (pid);
163 add_thread (pid, new_process);
611cb4a5 164
a9fa9f7d 165 return pid;
da6d8c04
DJ
166}
167
168/* Attach to an inferior process. */
169
0d62e5e8
DJ
170void
171linux_attach_lwp (int pid, int tid)
da6d8c04 172{
0d62e5e8 173 struct process_info *new_process;
611cb4a5 174
da6d8c04
DJ
175 if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
176 {
177 fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
178 errno < sys_nerr ? sys_errlist[errno] : "unknown error",
179 errno);
180 fflush (stderr);
0d62e5e8
DJ
181
182 /* If we fail to attach to an LWP, just return. */
183 if (!using_threads)
184 _exit (0177);
185 return;
da6d8c04
DJ
186 }
187
0d62e5e8
DJ
188 new_process = (struct process_info *) add_process (pid);
189 add_thread (tid, new_process);
190
191 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
192 brings it to a halt. We should ignore that SIGSTOP and resume the process
193 (unless this is the first process, in which case the flag will be cleared
194 in linux_attach).
195
196 On the other hand, if we are currently trying to stop all threads, we
197 should treat the new thread as if we had sent it a SIGSTOP. This works
198 because we are guaranteed that add_process added us to the end of the
199 list, and so the new thread has not yet reached wait_for_sigstop (but
200 will). */
201 if (! stopping_threads)
202 new_process->stop_expected = 1;
203}
204
205int
206linux_attach (int pid)
207{
208 struct process_info *process;
209
210 linux_attach_lwp (pid, pid);
211
212 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
213 process = (struct process_info *) find_inferior_id (&all_processes, pid);
214 process->stop_expected = 0;
215
da6d8c04
DJ
216 return 0;
217}
218
219/* Kill the inferior process. Make us have no inferior. */
220
ce3a066d 221static void
0d62e5e8 222linux_kill_one_process (struct inferior_list_entry *entry)
da6d8c04 223{
0d62e5e8
DJ
224 struct thread_info *thread = (struct thread_info *) entry;
225 struct process_info *process = get_thread_process (thread);
226 int wstat;
227
228 do
229 {
230 ptrace (PTRACE_KILL, pid_of (process), 0, 0);
231
232 /* Make sure it died. The loop is most likely unnecessary. */
233 wstat = linux_wait_for_event (thread);
234 } while (WIFSTOPPED (wstat));
da6d8c04
DJ
235}
236
0d62e5e8
DJ
237static void
238linux_kill (void)
239{
240 for_each_inferior (&all_threads, linux_kill_one_process);
241}
242
6ad8ae5c
DJ
243static void
244linux_detach_one_process (struct inferior_list_entry *entry)
245{
246 struct thread_info *thread = (struct thread_info *) entry;
247 struct process_info *process = get_thread_process (thread);
248
249 ptrace (PTRACE_DETACH, pid_of (process), 0, 0);
250}
251
252static void
253linux_detach (void)
254{
255 for_each_inferior (&all_threads, linux_detach_one_process);
256}
257
258/* Return nonzero if the given thread is still alive. */
0d62e5e8
DJ
259static int
260linux_thread_alive (int tid)
261{
262 if (find_inferior_id (&all_threads, tid) != NULL)
263 return 1;
264 else
265 return 0;
266}
267
268/* Return nonzero if this process stopped at a breakpoint which
269 no longer appears to be inserted. Also adjust the PC
270 appropriately to resume where the breakpoint used to be. */
ce3a066d 271static int
0d62e5e8 272check_removed_breakpoint (struct process_info *event_child)
da6d8c04 273{
0d62e5e8
DJ
274 CORE_ADDR stop_pc;
275 struct thread_info *saved_inferior;
276
277 if (event_child->pending_is_breakpoint == 0)
278 return 0;
279
280 if (debug_threads)
281 fprintf (stderr, "Checking for breakpoint.\n");
282
283 saved_inferior = current_inferior;
284 current_inferior = get_process_thread (event_child);
285
286 stop_pc = get_stop_pc ();
287
288 /* If the PC has changed since we stopped, then we shouldn't do
289 anything. This happens if, for instance, GDB handled the
290 decr_pc_after_break subtraction itself. */
291 if (stop_pc != event_child->pending_stop_pc)
292 {
293 if (debug_threads)
294 fprintf (stderr, "Ignoring, PC was changed.\n");
295
296 event_child->pending_is_breakpoint = 0;
297 current_inferior = saved_inferior;
298 return 0;
299 }
300
301 /* If the breakpoint is still there, we will report hitting it. */
302 if ((*the_low_target.breakpoint_at) (stop_pc))
303 {
304 if (debug_threads)
305 fprintf (stderr, "Ignoring, breakpoint is still present.\n");
306 current_inferior = saved_inferior;
307 return 0;
308 }
309
310 if (debug_threads)
311 fprintf (stderr, "Removed breakpoint.\n");
312
313 /* For decr_pc_after_break targets, here is where we perform the
314 decrement. We go immediately from this function to resuming,
315 and can not safely call get_stop_pc () again. */
316 if (the_low_target.set_pc != NULL)
317 (*the_low_target.set_pc) (stop_pc);
318
319 /* We consumed the pending SIGTRAP. */
320 event_child->status_pending_p = 0;
321 event_child->status_pending = 0;
322
323 current_inferior = saved_inferior;
da6d8c04
DJ
324 return 1;
325}
326
0d62e5e8
DJ
327/* Return 1 if this process has an interesting status pending. This function
328 may silently resume an inferior process. */
611cb4a5 329static int
0d62e5e8
DJ
330status_pending_p (struct inferior_list_entry *entry, void *dummy)
331{
332 struct process_info *process = (struct process_info *) entry;
333
334 if (process->status_pending_p)
335 if (check_removed_breakpoint (process))
336 {
337 /* This thread was stopped at a breakpoint, and the breakpoint
338 is now gone. We were told to continue (or step...) all threads,
339 so GDB isn't trying to single-step past this breakpoint.
340 So instead of reporting the old SIGTRAP, pretend we got to
341 the breakpoint just after it was removed instead of just
342 before; resume the process. */
343 linux_resume_one_process (&process->head, 0, 0);
344 return 0;
345 }
346
347 return process->status_pending_p;
348}
349
350static void
351linux_wait_for_process (struct process_info **childp, int *wstatp)
611cb4a5 352{
0d62e5e8
DJ
353 int ret;
354 int to_wait_for = -1;
355
356 if (*childp != NULL)
357 to_wait_for = (*childp)->lwpid;
611cb4a5
DJ
358
359 while (1)
360 {
0d62e5e8
DJ
361 ret = waitpid (to_wait_for, wstatp, WNOHANG);
362
363 if (ret == -1)
364 {
365 if (errno != ECHILD)
366 perror_with_name ("waitpid");
367 }
368 else if (ret > 0)
369 break;
370
371 ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE);
372
373 if (ret == -1)
374 {
375 if (errno != ECHILD)
376 perror_with_name ("waitpid (WCLONE)");
377 }
378 else if (ret > 0)
379 break;
380
381 usleep (1000);
382 }
383
384 if (debug_threads
385 && (!WIFSTOPPED (*wstatp)
386 || (WSTOPSIG (*wstatp) != 32
387 && WSTOPSIG (*wstatp) != 33)))
388 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);
389
390 if (to_wait_for == -1)
391 *childp = (struct process_info *) find_inferior_id (&all_processes, ret);
392
393 (*childp)->stopped = 1;
394 (*childp)->pending_is_breakpoint = 0;
395
396 if (debug_threads
397 && WIFSTOPPED (*wstatp))
398 {
399 current_inferior = (struct thread_info *)
400 find_inferior_id (&all_threads, (*childp)->tid);
401 /* For testing only; i386_stop_pc prints out a diagnostic. */
402 if (the_low_target.get_pc != NULL)
403 get_stop_pc ();
404 }
405}
611cb4a5 406
0d62e5e8
DJ
407static int
408linux_wait_for_event (struct thread_info *child)
409{
410 CORE_ADDR stop_pc;
411 struct process_info *event_child;
412 int wstat;
413
414 /* Check for a process with a pending status. */
415 /* It is possible that the user changed the pending task's registers since
416 it stopped. We correctly handle the change of PC if we hit a breakpoint
e5379b03 417 (in check_removed_breakpoint); signals should be reported anyway. */
0d62e5e8
DJ
418 if (child == NULL)
419 {
420 event_child = (struct process_info *)
421 find_inferior (&all_processes, status_pending_p, NULL);
422 if (debug_threads && event_child)
423 fprintf (stderr, "Got a pending child %d\n", event_child->lwpid);
424 }
425 else
426 {
427 event_child = get_thread_process (child);
428 if (event_child->status_pending_p
429 && check_removed_breakpoint (event_child))
430 event_child = NULL;
431 }
611cb4a5 432
0d62e5e8
DJ
433 if (event_child != NULL)
434 {
435 if (event_child->status_pending_p)
611cb4a5 436 {
0d62e5e8
DJ
437 if (debug_threads)
438 fprintf (stderr, "Got an event from pending child %d (%04x)\n",
439 event_child->lwpid, event_child->status_pending);
440 wstat = event_child->status_pending;
441 event_child->status_pending_p = 0;
442 event_child->status_pending = 0;
443 current_inferior = get_process_thread (event_child);
444 return wstat;
445 }
446 }
447
448 /* We only enter this loop if no process has a pending wait status. Thus
449 any action taken in response to a wait status inside this loop is
450 responding as soon as we detect the status, not after any pending
451 events. */
452 while (1)
453 {
454 if (child == NULL)
455 event_child = NULL;
456 else
457 event_child = get_thread_process (child);
458
459 linux_wait_for_process (&event_child, &wstat);
460
461 if (event_child == NULL)
462 error ("event from unknown child");
611cb4a5 463
0d62e5e8
DJ
464 current_inferior = (struct thread_info *)
465 find_inferior_id (&all_threads, event_child->tid);
466
467 if (using_threads)
468 {
469 /* Check for thread exit. */
470 if (! WIFSTOPPED (wstat))
611cb4a5 471 {
0d62e5e8
DJ
472 if (debug_threads)
473 fprintf (stderr, "Thread %d (LWP %d) exiting\n",
474 event_child->tid, event_child->head.id);
475
476 /* If the last thread is exiting, just return. */
477 if (all_threads.head == all_threads.tail)
478 return wstat;
479
480 dead_thread_notify (event_child->tid);
481
482 remove_inferior (&all_processes, &event_child->head);
483 free (event_child);
484 remove_thread (current_inferior);
485 current_inferior = (struct thread_info *) all_threads.head;
486
487 /* If we were waiting for this particular child to do something...
488 well, it did something. */
489 if (child != NULL)
490 return wstat;
491
492 /* Wait for a more interesting event. */
611cb4a5
DJ
493 continue;
494 }
495
0d62e5e8
DJ
496 if (WIFSTOPPED (wstat)
497 && WSTOPSIG (wstat) == SIGSTOP
498 && event_child->stop_expected)
499 {
500 if (debug_threads)
501 fprintf (stderr, "Expected stop.\n");
502 event_child->stop_expected = 0;
503 linux_resume_one_process (&event_child->head,
504 event_child->stepping, 0);
505 continue;
506 }
611cb4a5 507
0d62e5e8
DJ
508 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
509 thread library? */
510 if (WIFSTOPPED (wstat)
254787d4
DJ
511 && (WSTOPSIG (wstat) == __SIGRTMIN
512 || WSTOPSIG (wstat) == __SIGRTMIN + 1))
611cb4a5 513 {
0d62e5e8
DJ
514 if (debug_threads)
515 fprintf (stderr, "Ignored signal %d for %d (LWP %d).\n",
516 WSTOPSIG (wstat), event_child->tid,
517 event_child->head.id);
518 linux_resume_one_process (&event_child->head,
519 event_child->stepping,
520 WSTOPSIG (wstat));
521 continue;
522 }
523 }
611cb4a5 524
0d62e5e8
DJ
525 /* If this event was not handled above, and is not a SIGTRAP, report
526 it. */
527 if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP)
528 return wstat;
611cb4a5 529
0d62e5e8
DJ
530 /* If this target does not support breakpoints, we simply report the
531 SIGTRAP; it's of no concern to us. */
532 if (the_low_target.get_pc == NULL)
533 return wstat;
534
535 stop_pc = get_stop_pc ();
536
537 /* bp_reinsert will only be set if we were single-stepping.
538 Notice that we will resume the process after hitting
539 a gdbserver breakpoint; single-stepping to/over one
540 is not supported (yet). */
541 if (event_child->bp_reinsert != 0)
542 {
543 if (debug_threads)
544 fprintf (stderr, "Reinserted breakpoint.\n");
545 reinsert_breakpoint (event_child->bp_reinsert);
546 event_child->bp_reinsert = 0;
547
548 /* Clear the single-stepping flag and SIGTRAP as we resume. */
549 linux_resume_one_process (&event_child->head, 0, 0);
550 continue;
551 }
552
553 if (debug_threads)
554 fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n");
555
556 if (check_breakpoints (stop_pc) != 0)
557 {
558 /* We hit one of our own breakpoints. We mark it as a pending
e5379b03 559 breakpoint, so that check_removed_breakpoint () will do the PC
0d62e5e8
DJ
560 adjustment for us at the appropriate time. */
561 event_child->pending_is_breakpoint = 1;
562 event_child->pending_stop_pc = stop_pc;
563
564 /* Now we need to put the breakpoint back. We continue in the event
565 loop instead of simply replacing the breakpoint right away,
566 in order to not lose signals sent to the thread that hit the
567 breakpoint. Unfortunately this increases the window where another
568 thread could sneak past the removed breakpoint. For the current
569 use of server-side breakpoints (thread creation) this is
570 acceptable; but it needs to be considered before this breakpoint
571 mechanism can be used in more general ways. For some breakpoints
572 it may be necessary to stop all other threads, but that should
573 be avoided where possible.
574
575 If breakpoint_reinsert_addr is NULL, that means that we can
576 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
577 mark it for reinsertion, and single-step.
578
579 Otherwise, call the target function to figure out where we need
580 our temporary breakpoint, create it, and continue executing this
581 process. */
582 if (the_low_target.breakpoint_reinsert_addr == NULL)
583 {
584 event_child->bp_reinsert = stop_pc;
585 uninsert_breakpoint (stop_pc);
586 linux_resume_one_process (&event_child->head, 1, 0);
587 }
588 else
589 {
590 reinsert_breakpoint_by_bp
591 (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
592 linux_resume_one_process (&event_child->head, 0, 0);
611cb4a5 593 }
0d62e5e8
DJ
594
595 continue;
596 }
597
598 /* If we were single-stepping, we definitely want to report the
599 SIGTRAP. The single-step operation has completed, so also
600 clear the stepping flag; in general this does not matter,
601 because the SIGTRAP will be reported to the client, which
602 will give us a new action for this thread, but clear it for
603 consistency anyway. It's safe to clear the stepping flag
604 because the only consumer of get_stop_pc () after this point
e5379b03 605 is check_removed_breakpoint, and pending_is_breakpoint is not
0d62e5e8
DJ
606 set. It might be wiser to use a step_completed flag instead. */
607 if (event_child->stepping)
608 {
609 event_child->stepping = 0;
610 return wstat;
611 }
612
613 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
614 Check if it is a breakpoint, and if so mark the process information
615 accordingly. This will handle both the necessary fiddling with the
616 PC on decr_pc_after_break targets and suppressing extra threads
617 hitting a breakpoint if two hit it at once and then GDB removes it
618 after the first is reported. Arguably it would be better to report
619 multiple threads hitting breakpoints simultaneously, but the current
620 remote protocol does not allow this. */
621 if ((*the_low_target.breakpoint_at) (stop_pc))
622 {
623 event_child->pending_is_breakpoint = 1;
624 event_child->pending_stop_pc = stop_pc;
611cb4a5
DJ
625 }
626
627 return wstat;
628 }
0d62e5e8 629
611cb4a5
DJ
630 /* NOTREACHED */
631 return 0;
632}
633
0d62e5e8 634/* Wait for process, returns status. */
da6d8c04 635
ce3a066d
DJ
636static unsigned char
637linux_wait (char *status)
da6d8c04 638{
e5f1222d 639 int w;
0d62e5e8
DJ
640 struct thread_info *child = NULL;
641
642retry:
643 /* If we were only supposed to resume one thread, only wait for
644 that thread - if it's still alive. If it died, however - which
645 can happen if we're coming from the thread death case below -
646 then we need to make sure we restart the other threads. We could
647 pick a thread at random or restart all; restarting all is less
648 arbitrary. */
649 if (cont_thread > 0)
650 {
651 child = (struct thread_info *) find_inferior_id (&all_threads,
652 cont_thread);
653
654 /* No stepping, no signal - unless one is pending already, of course. */
655 if (child == NULL)
656 linux_resume (0, 0);
657 }
da6d8c04
DJ
658
659 enable_async_io ();
0d62e5e8
DJ
660 w = linux_wait_for_event (child);
661 stop_all_processes ();
da6d8c04 662 disable_async_io ();
da6d8c04 663
0d62e5e8
DJ
664 /* If we are waiting for a particular child, and it exited,
665 linux_wait_for_event will return its exit status. Similarly if
666 the last child exited. If this is not the last child, however,
667 do not report it as exited until there is a 'thread exited' response
668 available in the remote protocol. Instead, just wait for another event.
669 This should be safe, because if the thread crashed we will already
670 have reported the termination signal to GDB; that should stop any
671 in-progress stepping operations, etc.
672
673 Report the exit status of the last thread to exit. This matches
674 LinuxThreads' behavior. */
675
676 if (all_threads.head == all_threads.tail)
da6d8c04 677 {
0d62e5e8
DJ
678 if (WIFEXITED (w))
679 {
680 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
681 *status = 'W';
682 clear_inferiors ();
683 return ((unsigned char) WEXITSTATUS (w));
684 }
685 else if (!WIFSTOPPED (w))
686 {
687 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
688 clear_inferiors ();
689 *status = 'X';
690 return ((unsigned char) WTERMSIG (w));
691 }
da6d8c04 692 }
0d62e5e8 693 else
da6d8c04 694 {
0d62e5e8
DJ
695 if (!WIFSTOPPED (w))
696 goto retry;
da6d8c04
DJ
697 }
698
da6d8c04
DJ
699 *status = 'T';
700 return ((unsigned char) WSTOPSIG (w));
701}
702
0d62e5e8
DJ
703static void
704send_sigstop (struct inferior_list_entry *entry)
705{
706 struct process_info *process = (struct process_info *) entry;
707
708 if (process->stopped)
709 return;
710
711 /* If we already have a pending stop signal for this process, don't
712 send another. */
713 if (process->stop_expected)
714 {
715 process->stop_expected = 0;
716 return;
717 }
718
719 if (debug_threads)
720 fprintf (stderr, "Sending sigstop to process %d\n", process->head.id);
721
722 kill (process->head.id, SIGSTOP);
723 process->sigstop_sent = 1;
724}
725
726static void
727wait_for_sigstop (struct inferior_list_entry *entry)
728{
729 struct process_info *process = (struct process_info *) entry;
730 struct thread_info *saved_inferior, *thread;
731 int wstat, saved_tid;
732
733 if (process->stopped)
734 return;
735
736 saved_inferior = current_inferior;
737 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
738 thread = (struct thread_info *) find_inferior_id (&all_threads,
739 process->tid);
740 wstat = linux_wait_for_event (thread);
741
742 /* If we stopped with a non-SIGSTOP signal, save it for later
743 and record the pending SIGSTOP. If the process exited, just
744 return. */
745 if (WIFSTOPPED (wstat)
746 && WSTOPSIG (wstat) != SIGSTOP)
747 {
748 if (debug_threads)
749 fprintf (stderr, "Stopped with non-sigstop signal\n");
750 process->status_pending_p = 1;
751 process->status_pending = wstat;
752 process->stop_expected = 1;
753 }
754
755 if (linux_thread_alive (saved_tid))
756 current_inferior = saved_inferior;
757 else
758 {
759 if (debug_threads)
760 fprintf (stderr, "Previously current thread died.\n");
761
762 /* Set a valid thread as current. */
763 set_desired_inferior (0);
764 }
765}
766
767static void
768stop_all_processes (void)
769{
770 stopping_threads = 1;
771 for_each_inferior (&all_processes, send_sigstop);
772 for_each_inferior (&all_processes, wait_for_sigstop);
773 stopping_threads = 0;
774}
775
da6d8c04
DJ
776/* Resume execution of the inferior process.
777 If STEP is nonzero, single-step it.
778 If SIGNAL is nonzero, give it that signal. */
779
ce3a066d 780static void
0d62e5e8
DJ
781linux_resume_one_process (struct inferior_list_entry *entry,
782 int step, int signal)
da6d8c04 783{
0d62e5e8
DJ
784 struct process_info *process = (struct process_info *) entry;
785 struct thread_info *saved_inferior;
786
787 if (process->stopped == 0)
788 return;
789
790 /* If we have pending signals or status, and a new signal, enqueue the
791 signal. Also enqueue the signal if we are waiting to reinsert a
792 breakpoint; it will be picked up again below. */
793 if (signal != 0
794 && (process->status_pending_p || process->pending_signals != NULL
795 || process->bp_reinsert != 0))
796 {
797 struct pending_signals *p_sig;
798 p_sig = malloc (sizeof (*p_sig));
799 p_sig->prev = process->pending_signals;
800 p_sig->signal = signal;
801 process->pending_signals = p_sig;
802 }
803
e5379b03 804 if (process->status_pending_p && !check_removed_breakpoint (process))
0d62e5e8
DJ
805 return;
806
807 saved_inferior = current_inferior;
808 current_inferior = get_process_thread (process);
809
810 if (debug_threads)
811 fprintf (stderr, "Resuming process %d (%s, signal %d, stop %s)\n", inferior_pid,
812 step ? "step" : "continue", signal,
813 process->stop_expected ? "expected" : "not expected");
814
815 /* This bit needs some thinking about. If we get a signal that
816 we must report while a single-step reinsert is still pending,
817 we often end up resuming the thread. It might be better to
818 (ew) allow a stack of pending events; then we could be sure that
819 the reinsert happened right away and not lose any signals.
820
821 Making this stack would also shrink the window in which breakpoints are
822 uninserted (see comment in linux_wait_for_process) but not enough for
823 complete correctness, so it won't solve that problem. It may be
824 worthwhile just to solve this one, however. */
825 if (process->bp_reinsert != 0)
826 {
827 if (debug_threads)
828 fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert);
829 if (step == 0)
830 fprintf (stderr, "BAD - reinserting but not stepping.\n");
831 step = 1;
832
833 /* Postpone any pending signal. It was enqueued above. */
834 signal = 0;
835 }
836
837 check_removed_breakpoint (process);
838
839 if (debug_threads && the_low_target.get_pc != NULL)
840 {
841 fprintf (stderr, " ");
842 (long) (*the_low_target.get_pc) ();
843 }
844
845 /* If we have pending signals, consume one unless we are trying to reinsert
846 a breakpoint. */
847 if (process->pending_signals != NULL && process->bp_reinsert == 0)
848 {
849 struct pending_signals **p_sig;
850
851 p_sig = &process->pending_signals;
852 while ((*p_sig)->prev != NULL)
853 p_sig = &(*p_sig)->prev;
854
855 signal = (*p_sig)->signal;
856 free (*p_sig);
857 *p_sig = NULL;
858 }
859
860 regcache_invalidate_one ((struct inferior_list_entry *)
861 get_process_thread (process));
da6d8c04 862 errno = 0;
0d62e5e8
DJ
863 process->stopped = 0;
864 process->stepping = step;
865 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal);
866
867 current_inferior = saved_inferior;
da6d8c04
DJ
868 if (errno)
869 perror_with_name ("ptrace");
870}
871
0d62e5e8
DJ
872/* This function is called once per process other than the first
873 one. The first process we are told the signal to continue
874 with, and whether to step or continue; for all others, any
875 existing signals will be marked in status_pending_p to be
876 reported momentarily, and we preserve the stepping flag. */
877static void
878linux_continue_one_process (struct inferior_list_entry *entry)
879{
880 struct process_info *process;
c6ecbae5 881
0d62e5e8
DJ
882 process = (struct process_info *) entry;
883 linux_resume_one_process (entry, process->stepping, 0);
884}
885
886static void
887linux_resume (int step, int signal)
888{
889 struct process_info *process;
890
891 process = get_thread_process (current_inferior);
892
893 /* If the current process has a status pending, this signal will
894 be enqueued and sent later. */
895 linux_resume_one_process (&process->head, step, signal);
c6ecbae5 896
0d62e5e8
DJ
897 if (cont_thread == 0 || cont_thread == -1)
898 for_each_inferior (&all_processes, linux_continue_one_process);
899}
900
901#ifdef HAVE_LINUX_USRREGS
da6d8c04
DJ
902
903int
0a30fbc4 904register_addr (int regnum)
da6d8c04
DJ
905{
906 int addr;
907
2ec06d2e 908 if (regnum < 0 || regnum >= the_low_target.num_regs)
da6d8c04
DJ
909 error ("Invalid register number %d.", regnum);
910
2ec06d2e 911 addr = the_low_target.regmap[regnum];
da6d8c04
DJ
912
913 return addr;
914}
915
58caa3dc 916/* Fetch one register. */
da6d8c04
DJ
917static void
918fetch_register (int regno)
919{
920 CORE_ADDR regaddr;
921 register int i;
0d62e5e8 922 char *buf;
da6d8c04 923
2ec06d2e 924 if (regno >= the_low_target.num_regs)
0a30fbc4 925 return;
2ec06d2e 926 if ((*the_low_target.cannot_fetch_register) (regno))
0a30fbc4 927 return;
da6d8c04 928
0a30fbc4
DJ
929 regaddr = register_addr (regno);
930 if (regaddr == -1)
931 return;
0d62e5e8
DJ
932 buf = alloca (register_size (regno));
933 for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE))
da6d8c04
DJ
934 {
935 errno = 0;
0d62e5e8 936 *(PTRACE_XFER_TYPE *) (buf + i) =
da6d8c04
DJ
937 ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
938 regaddr += sizeof (PTRACE_XFER_TYPE);
939 if (errno != 0)
940 {
941 /* Warning, not error, in case we are attached; sometimes the
942 kernel doesn't let us at the registers. */
943 char *err = strerror (errno);
944 char *msg = alloca (strlen (err) + 128);
945 sprintf (msg, "reading register %d: %s", regno, err);
946 error (msg);
947 goto error_exit;
948 }
949 }
0d62e5e8
DJ
950 supply_register (regno, buf);
951
da6d8c04
DJ
952error_exit:;
953}
954
955/* Fetch all registers, or just one, from the child process. */
58caa3dc
DJ
956static void
957usr_fetch_inferior_registers (int regno)
da6d8c04
DJ
958{
959 if (regno == -1 || regno == 0)
2ec06d2e 960 for (regno = 0; regno < the_low_target.num_regs; regno++)
da6d8c04
DJ
961 fetch_register (regno);
962 else
963 fetch_register (regno);
964}
965
966/* Store our register values back into the inferior.
967 If REGNO is -1, do this for all registers.
968 Otherwise, REGNO specifies which register (so we can save time). */
58caa3dc
DJ
969static void
970usr_store_inferior_registers (int regno)
da6d8c04
DJ
971{
972 CORE_ADDR regaddr;
973 int i;
0d62e5e8 974 char *buf;
da6d8c04
DJ
975
976 if (regno >= 0)
977 {
2ec06d2e 978 if (regno >= the_low_target.num_regs)
0a30fbc4
DJ
979 return;
980
bc1e36ca 981 if ((*the_low_target.cannot_store_register) (regno) == 1)
0a30fbc4
DJ
982 return;
983
984 regaddr = register_addr (regno);
985 if (regaddr == -1)
da6d8c04 986 return;
da6d8c04 987 errno = 0;
0d62e5e8
DJ
988 buf = alloca (register_size (regno));
989 collect_register (regno, buf);
990 for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE))
da6d8c04 991 {
0a30fbc4
DJ
992 errno = 0;
993 ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
2ff29de4 994 *(PTRACE_XFER_TYPE *) (buf + i));
da6d8c04
DJ
995 if (errno != 0)
996 {
bc1e36ca
DJ
997 if ((*the_low_target.cannot_store_register) (regno) == 0)
998 {
999 char *err = strerror (errno);
1000 char *msg = alloca (strlen (err) + 128);
1001 sprintf (msg, "writing register %d: %s",
1002 regno, err);
1003 error (msg);
1004 return;
1005 }
da6d8c04 1006 }
2ff29de4 1007 regaddr += sizeof (PTRACE_XFER_TYPE);
da6d8c04 1008 }
da6d8c04
DJ
1009 }
1010 else
2ec06d2e 1011 for (regno = 0; regno < the_low_target.num_regs; regno++)
0d62e5e8 1012 usr_store_inferior_registers (regno);
da6d8c04 1013}
58caa3dc
DJ
1014#endif /* HAVE_LINUX_USRREGS */
1015
1016
1017
1018#ifdef HAVE_LINUX_REGSETS
1019
1020static int
0d62e5e8 1021regsets_fetch_inferior_registers ()
58caa3dc
DJ
1022{
1023 struct regset_info *regset;
1024
1025 regset = target_regsets;
1026
1027 while (regset->size >= 0)
1028 {
1029 void *buf;
1030 int res;
1031
1032 if (regset->size == 0)
1033 {
1034 regset ++;
1035 continue;
1036 }
1037
1038 buf = malloc (regset->size);
d06f167a 1039 res = ptrace (regset->get_request, inferior_pid, 0, buf);
58caa3dc
DJ
1040 if (res < 0)
1041 {
1042 if (errno == EIO)
1043 {
1044 /* If we get EIO on the first regset, do not try regsets again.
1045 If we get EIO on a later regset, disable that regset. */
1046 if (regset == target_regsets)
1047 {
1048 use_regsets_p = 0;
1049 return -1;
1050 }
1051 else
1052 {
1053 regset->size = 0;
1054 continue;
1055 }
1056 }
1057 else
1058 {
0d62e5e8
DJ
1059 char s[256];
1060 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
1061 inferior_pid);
1062 perror (s);
58caa3dc
DJ
1063 }
1064 }
1065 regset->store_function (buf);
1066 regset ++;
1067 }
ce3a066d 1068 return 0;
58caa3dc
DJ
1069}
1070
1071static int
0d62e5e8 1072regsets_store_inferior_registers ()
58caa3dc
DJ
1073{
1074 struct regset_info *regset;
1075
1076 regset = target_regsets;
1077
1078 while (regset->size >= 0)
1079 {
1080 void *buf;
1081 int res;
1082
1083 if (regset->size == 0)
1084 {
1085 regset ++;
1086 continue;
1087 }
1088
1089 buf = malloc (regset->size);
1090 regset->fill_function (buf);
d06f167a 1091 res = ptrace (regset->set_request, inferior_pid, 0, buf);
58caa3dc
DJ
1092 if (res < 0)
1093 {
1094 if (errno == EIO)
1095 {
1096 /* If we get EIO on the first regset, do not try regsets again.
1097 If we get EIO on a later regset, disable that regset. */
1098 if (regset == target_regsets)
1099 {
1100 use_regsets_p = 0;
1101 return -1;
1102 }
1103 else
1104 {
1105 regset->size = 0;
1106 continue;
1107 }
1108 }
1109 else
1110 {
ce3a066d 1111 perror ("Warning: ptrace(regsets_store_inferior_registers)");
58caa3dc
DJ
1112 }
1113 }
1114 regset ++;
09ec9b38 1115 free (buf);
58caa3dc 1116 }
ce3a066d 1117 return 0;
58caa3dc
DJ
1118}
1119
1120#endif /* HAVE_LINUX_REGSETS */
1121
1122
1123void
ce3a066d 1124linux_fetch_registers (int regno)
58caa3dc
DJ
1125{
1126#ifdef HAVE_LINUX_REGSETS
1127 if (use_regsets_p)
1128 {
1129 if (regsets_fetch_inferior_registers () == 0)
1130 return;
1131 }
1132#endif
1133#ifdef HAVE_LINUX_USRREGS
1134 usr_fetch_inferior_registers (regno);
1135#endif
1136}
1137
1138void
ce3a066d 1139linux_store_registers (int regno)
58caa3dc
DJ
1140{
1141#ifdef HAVE_LINUX_REGSETS
1142 if (use_regsets_p)
1143 {
1144 if (regsets_store_inferior_registers () == 0)
1145 return;
1146 }
1147#endif
1148#ifdef HAVE_LINUX_USRREGS
1149 usr_store_inferior_registers (regno);
1150#endif
1151}
1152
da6d8c04 1153
da6d8c04
DJ
1154/* Copy LEN bytes from inferior's memory starting at MEMADDR
1155 to debugger memory starting at MYADDR. */
1156
ce3a066d
DJ
1157static void
1158linux_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
da6d8c04
DJ
1159{
1160 register int i;
1161 /* Round starting address down to longword boundary. */
1162 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1163 /* Round ending address up; get number of longwords that makes. */
1164 register int count
1165 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
1166 / sizeof (PTRACE_XFER_TYPE);
1167 /* Allocate buffer of that many longwords. */
1168 register PTRACE_XFER_TYPE *buffer
1169 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1170
1171 /* Read all the longwords */
1172 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1173 {
d844cde6 1174 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
da6d8c04
DJ
1175 }
1176
1177 /* Copy appropriate bytes out of the buffer. */
1178 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
1179}
1180
1181/* Copy LEN bytes of data from debugger memory at MYADDR
1182 to inferior's memory at MEMADDR.
1183 On failure (cannot write the inferior)
1184 returns the value of errno. */
1185
ce3a066d 1186static int
611cb4a5 1187linux_write_memory (CORE_ADDR memaddr, const char *myaddr, int len)
da6d8c04
DJ
1188{
1189 register int i;
1190 /* Round starting address down to longword boundary. */
1191 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1192 /* Round ending address up; get number of longwords that makes. */
1193 register int count
1194 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
1195 /* Allocate buffer of that many longwords. */
1196 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1197 extern int errno;
1198
0d62e5e8
DJ
1199 if (debug_threads)
1200 {
1201 fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
1202 }
1203
da6d8c04
DJ
1204 /* Fill start and end extra bytes of buffer with existing memory data. */
1205
d844cde6
DJ
1206 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
1207 (PTRACE_ARG3_TYPE) addr, 0);
da6d8c04
DJ
1208
1209 if (count > 1)
1210 {
1211 buffer[count - 1]
1212 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
d844cde6
DJ
1213 (PTRACE_ARG3_TYPE) (addr + (count - 1)
1214 * sizeof (PTRACE_XFER_TYPE)),
1215 0);
da6d8c04
DJ
1216 }
1217
1218 /* Copy data to be written over corresponding part of buffer */
1219
1220 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
1221
1222 /* Write the entire buffer. */
1223
1224 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1225 {
1226 errno = 0;
d844cde6 1227 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
da6d8c04
DJ
1228 if (errno)
1229 return errno;
1230 }
1231
1232 return 0;
1233}
2f2893d9
DJ
1234
1235static void
1236linux_look_up_symbols (void)
1237{
0d62e5e8
DJ
1238#ifdef USE_THREAD_DB
1239 if (using_threads)
1240 return;
1241
1242 using_threads = thread_db_init ();
1243#endif
1244}
1245
e5379b03
DJ
1246static void
1247linux_send_signal (int signum)
1248{
1249 extern int signal_pid;
1250
1251 if (cont_thread > 0)
1252 {
1253 struct process_info *process;
1254
1255 process = get_thread_process (current_inferior);
1256 kill (process->lwpid, signum);
1257 }
1258 else
1259 kill (signal_pid, signum);
1260}
1261
da6d8c04 1262\f
ce3a066d
DJ
1263static struct target_ops linux_target_ops = {
1264 linux_create_inferior,
1265 linux_attach,
1266 linux_kill,
6ad8ae5c 1267 linux_detach,
ce3a066d
DJ
1268 linux_thread_alive,
1269 linux_resume,
1270 linux_wait,
1271 linux_fetch_registers,
1272 linux_store_registers,
1273 linux_read_memory,
1274 linux_write_memory,
2f2893d9 1275 linux_look_up_symbols,
e5379b03 1276 linux_send_signal,
ce3a066d
DJ
1277};
1278
0d62e5e8
DJ
1279static void
1280linux_init_signals ()
1281{
1282 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1283 to find what the cancel signal actually is. */
254787d4 1284 signal (__SIGRTMIN+1, SIG_IGN);
0d62e5e8
DJ
1285}
1286
da6d8c04
DJ
1287void
1288initialize_low (void)
1289{
0d62e5e8 1290 using_threads = 0;
ce3a066d 1291 set_target_ops (&linux_target_ops);
611cb4a5
DJ
1292 set_breakpoint_data (the_low_target.breakpoint,
1293 the_low_target.breakpoint_len);
0a30fbc4 1294 init_registers ();
0d62e5e8 1295 linux_init_signals ();
da6d8c04 1296}