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