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