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