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