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