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