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