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