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