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