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