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