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