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