Deleted Added
full compact
fbsd-low.c (157572) fbsd-low.c (157574)
1/* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
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

--- 5 unchanged lines hidden (view full) ---

14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
1/* Low level interface to ptrace, for the remote server for GDB.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
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

--- 5 unchanged lines hidden (view full) ---

14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include <sys/cdefs.h>
23__FBSDID("$FreeBSD: head/gnu/usr.bin/gdb/gdbserver/fbsd-low.c 157574 2006-04-06 22:24:01Z jmg $");
24
22#include "server.h"
25#include "server.h"
23#include "linux-low.h"
26#include "fbsd-low.h"
24
25#include <sys/wait.h>
27
28#include <sys/wait.h>
26#include <stdio.h>
27#include <sys/param.h>
29#include <sys/param.h>
28#include <sys/dir.h>
29#include <sys/ptrace.h>
30#include <sys/user.h>
30#include <sys/ptrace.h>
31#include <sys/user.h>
31#include <signal.h>
32#include <sys/ioctl.h>
32#include <sys/ioctl.h>
33#include <dirent.h>
34#include <errno.h>
33#include <fcntl.h>
35#include <fcntl.h>
34#include <string.h>
36#include <signal.h>
37#include <stdio.h>
35#include <stdlib.h>
38#include <stdlib.h>
39#include <string.h>
36#include <unistd.h>
40#include <unistd.h>
37#include <errno.h>
38
39/* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
40 however. This requires changing the ID in place when we go from !using_threads
41 to using_threads, immediately.
42
43 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
44 the same as the LWP ID. */
45
46struct inferior_list all_processes;
47
48/* FIXME this is a bit of a hack, and could be removed. */
49int stopping_threads;
50
51/* FIXME make into a target method? */
52int using_threads;
53
41
42/* ``all_threads'' is keyed by the LWP ID - it should be the thread ID instead,
43 however. This requires changing the ID in place when we go from !using_threads
44 to using_threads, immediately.
45
46 ``all_processes'' is keyed by the process ID - which on Linux is (presently)
47 the same as the LWP ID. */
48
49struct inferior_list all_processes;
50
51/* FIXME this is a bit of a hack, and could be removed. */
52int stopping_threads;
53
54/* FIXME make into a target method? */
55int using_threads;
56
54static void linux_resume_one_process (struct inferior_list_entry *entry,
57static void fbsd_resume_one_process (struct inferior_list_entry *entry,
55 int step, int signal);
58 int step, int signal);
56static void linux_resume (struct thread_resume *resume_info);
59static void fbsd_resume (struct thread_resume *resume_info);
57static void stop_all_processes (void);
60static void stop_all_processes (void);
58static int linux_wait_for_event (struct thread_info *child);
61static int fbsd_wait_for_event (struct thread_info *child);
59
60struct pending_signals
61{
62 int signal;
63 struct pending_signals *prev;
64};
65
62
63struct pending_signals
64{
65 int signal;
66 struct pending_signals *prev;
67};
68
66#define PTRACE_ARG3_TYPE long
67#define PTRACE_XFER_TYPE long
69#define PTRACE_ARG3_TYPE caddr_t
70#define PTRACE_XFER_TYPE int
68
71
69#ifdef HAVE_LINUX_REGSETS
70static int use_regsets_p = 1;
71#endif
72
73int debug_threads = 0;
74
75#define pid_of(proc) ((proc)->head.id)
76
77/* FIXME: Delete eventually. */
78#define inferior_pid (pid_of (get_thread_process (current_inferior)))
79
80/* This function should only be called if the process got a SIGTRAP.
81 The SIGTRAP could mean several things.
82
83 On i386, where decr_pc_after_break is non-zero:
72int debug_threads = 0;
73
74#define pid_of(proc) ((proc)->head.id)
75
76/* FIXME: Delete eventually. */
77#define inferior_pid (pid_of (get_thread_process (current_inferior)))
78
79/* This function should only be called if the process got a SIGTRAP.
80 The SIGTRAP could mean several things.
81
82 On i386, where decr_pc_after_break is non-zero:
84 If we were single-stepping this process using PTRACE_SINGLESTEP,
83 If we were single-stepping this process using PT_STEP,
85 we will get only the one SIGTRAP (even if the instruction we
86 stepped over was a breakpoint). The value of $eip will be the
87 next instruction.
88 If we continue the process using PTRACE_CONT, we will get a
89 SIGTRAP when we hit a breakpoint. The value of $eip will be
90 the instruction after the breakpoint (i.e. needs to be
91 decremented). If we report the SIGTRAP to GDB, we must also
92 report the undecremented PC. If we cancel the SIGTRAP, we

--- 35 unchanged lines hidden (view full) ---

128
129 return process;
130}
131
132/* Start an inferior process and returns its pid.
133 ALLARGS is a vector of program-name and args. */
134
135static int
84 we will get only the one SIGTRAP (even if the instruction we
85 stepped over was a breakpoint). The value of $eip will be the
86 next instruction.
87 If we continue the process using PTRACE_CONT, we will get a
88 SIGTRAP when we hit a breakpoint. The value of $eip will be
89 the instruction after the breakpoint (i.e. needs to be
90 decremented). If we report the SIGTRAP to GDB, we must also
91 report the undecremented PC. If we cancel the SIGTRAP, we

--- 35 unchanged lines hidden (view full) ---

127
128 return process;
129}
130
131/* Start an inferior process and returns its pid.
132 ALLARGS is a vector of program-name and args. */
133
134static int
136linux_create_inferior (char *program, char **allargs)
135fbsd_create_inferior (char *program, char **allargs)
137{
138 void *new_process;
139 int pid;
140
136{
137 void *new_process;
138 int pid;
139
141 pid = fork ();
140 pid = vfork ();
142 if (pid < 0)
141 if (pid < 0)
143 perror_with_name ("fork");
142 perror_with_name ("vfork");
144
145 if (pid == 0)
146 {
143
144 if (pid == 0)
145 {
147 ptrace (PTRACE_TRACEME, 0, 0, 0);
146 ptrace (PT_TRACE_ME, 0, 0, 0);
148
147
149 signal (__SIGRTMIN + 1, SIG_DFL);
150
151 setpgid (0, 0);
152
153 execv (program, allargs);
154
155 fprintf (stderr, "Cannot exec %s: %s.\n", program,
156 strerror (errno));
157 fflush (stderr);
158 _exit (0177);
159 }
160
161 new_process = add_process (pid);
162 add_thread (pid, new_process);
163
164 return pid;
165}
166
167/* Attach to an inferior process. */
168
169void
148 setpgid (0, 0);
149
150 execv (program, allargs);
151
152 fprintf (stderr, "Cannot exec %s: %s.\n", program,
153 strerror (errno));
154 fflush (stderr);
155 _exit (0177);
156 }
157
158 new_process = add_process (pid);
159 add_thread (pid, new_process);
160
161 return pid;
162}
163
164/* Attach to an inferior process. */
165
166void
170linux_attach_lwp (int pid, int tid)
167fbsd_attach_lwp (int pid, int tid)
171{
172 struct process_info *new_process;
173
168{
169 struct process_info *new_process;
170
174 if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
171 if (ptrace (PT_ATTACH, pid, 0, 0) != 0)
175 {
176 fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
177 strerror (errno), errno);
178 fflush (stderr);
179
180 /* If we fail to attach to an LWP, just return. */
181 if (!using_threads)
182 _exit (0177);
183 return;
184 }
185
186 new_process = (struct process_info *) add_process (pid);
187 add_thread (tid, new_process);
188
189 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
190 brings it to a halt. We should ignore that SIGSTOP and resume the process
191 (unless this is the first process, in which case the flag will be cleared
172 {
173 fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
174 strerror (errno), errno);
175 fflush (stderr);
176
177 /* If we fail to attach to an LWP, just return. */
178 if (!using_threads)
179 _exit (0177);
180 return;
181 }
182
183 new_process = (struct process_info *) add_process (pid);
184 add_thread (tid, new_process);
185
186 /* The next time we wait for this LWP we'll see a SIGSTOP as PTRACE_ATTACH
187 brings it to a halt. We should ignore that SIGSTOP and resume the process
188 (unless this is the first process, in which case the flag will be cleared
192 in linux_attach).
189 in fbsd_attach).
193
194 On the other hand, if we are currently trying to stop all threads, we
195 should treat the new thread as if we had sent it a SIGSTOP. This works
196 because we are guaranteed that add_process added us to the end of the
197 list, and so the new thread has not yet reached wait_for_sigstop (but
198 will). */
199 if (! stopping_threads)
200 new_process->stop_expected = 1;
201}
202
203int
190
191 On the other hand, if we are currently trying to stop all threads, we
192 should treat the new thread as if we had sent it a SIGSTOP. This works
193 because we are guaranteed that add_process added us to the end of the
194 list, and so the new thread has not yet reached wait_for_sigstop (but
195 will). */
196 if (! stopping_threads)
197 new_process->stop_expected = 1;
198}
199
200int
204linux_attach (int pid)
201fbsd_attach (int pid)
205{
206 struct process_info *process;
207
202{
203 struct process_info *process;
204
208 linux_attach_lwp (pid, pid);
205 fbsd_attach_lwp (pid, pid);
209
210 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
211 process = (struct process_info *) find_inferior_id (&all_processes, pid);
212 process->stop_expected = 0;
213
214 return 0;
215}
216
217/* Kill the inferior process. Make us have no inferior. */
218
219static void
206
207 /* Don't ignore the initial SIGSTOP if we just attached to this process. */
208 process = (struct process_info *) find_inferior_id (&all_processes, pid);
209 process->stop_expected = 0;
210
211 return 0;
212}
213
214/* Kill the inferior process. Make us have no inferior. */
215
216static void
220linux_kill_one_process (struct inferior_list_entry *entry)
217fbsd_kill_one_process (struct inferior_list_entry *entry)
221{
222 struct thread_info *thread = (struct thread_info *) entry;
223 struct process_info *process = get_thread_process (thread);
224 int wstat;
225
226 do
227 {
218{
219 struct thread_info *thread = (struct thread_info *) entry;
220 struct process_info *process = get_thread_process (thread);
221 int wstat;
222
223 do
224 {
228 ptrace (PTRACE_KILL, pid_of (process), 0, 0);
225 ptrace (PT_KILL, pid_of (process), 0, 0);
229
230 /* Make sure it died. The loop is most likely unnecessary. */
226
227 /* Make sure it died. The loop is most likely unnecessary. */
231 wstat = linux_wait_for_event (thread);
228 wstat = fbsd_wait_for_event (thread);
232 } while (WIFSTOPPED (wstat));
233}
234
235static void
229 } while (WIFSTOPPED (wstat));
230}
231
232static void
236linux_kill (void)
233fbsd_kill (void)
237{
234{
238 for_each_inferior (&all_threads, linux_kill_one_process);
235 for_each_inferior (&all_threads, fbsd_kill_one_process);
239}
240
241static void
236}
237
238static void
242linux_detach_one_process (struct inferior_list_entry *entry)
239fbsd_detach_one_process (struct inferior_list_entry *entry)
243{
244 struct thread_info *thread = (struct thread_info *) entry;
245 struct process_info *process = get_thread_process (thread);
246
240{
241 struct thread_info *thread = (struct thread_info *) entry;
242 struct process_info *process = get_thread_process (thread);
243
247 ptrace (PTRACE_DETACH, pid_of (process), 0, 0);
244 ptrace (PT_DETACH, pid_of (process), 0, 0);
248}
249
250static void
245}
246
247static void
251linux_detach (void)
248fbsd_detach (void)
252{
249{
253 for_each_inferior (&all_threads, linux_detach_one_process);
250 for_each_inferior (&all_threads, fbsd_detach_one_process);
254}
255
256/* Return nonzero if the given thread is still alive. */
257static int
251}
252
253/* Return nonzero if the given thread is still alive. */
254static int
258linux_thread_alive (int tid)
255fbsd_thread_alive (int tid)
259{
260 if (find_inferior_id (&all_threads, tid) != NULL)
261 return 1;
262 else
263 return 0;
264}
265
266/* Return nonzero if this process stopped at a breakpoint which

--- 67 unchanged lines hidden (view full) ---

334 if (check_removed_breakpoint (process))
335 {
336 /* This thread was stopped at a breakpoint, and the breakpoint
337 is now gone. We were told to continue (or step...) all threads,
338 so GDB isn't trying to single-step past this breakpoint.
339 So instead of reporting the old SIGTRAP, pretend we got to
340 the breakpoint just after it was removed instead of just
341 before; resume the process. */
256{
257 if (find_inferior_id (&all_threads, tid) != NULL)
258 return 1;
259 else
260 return 0;
261}
262
263/* Return nonzero if this process stopped at a breakpoint which

--- 67 unchanged lines hidden (view full) ---

331 if (check_removed_breakpoint (process))
332 {
333 /* This thread was stopped at a breakpoint, and the breakpoint
334 is now gone. We were told to continue (or step...) all threads,
335 so GDB isn't trying to single-step past this breakpoint.
336 So instead of reporting the old SIGTRAP, pretend we got to
337 the breakpoint just after it was removed instead of just
338 before; resume the process. */
342 linux_resume_one_process (&process->head, 0, 0);
339 fbsd_resume_one_process (&process->head, 0, 0);
343 return 0;
344 }
345
346 return process->status_pending_p;
347}
348
349static void
340 return 0;
341 }
342
343 return process->status_pending_p;
344}
345
346static void
350linux_wait_for_process (struct process_info **childp, int *wstatp)
347fbsd_wait_for_process (struct process_info **childp, int *wstatp)
351{
352 int ret;
353 int to_wait_for = -1;
354
355 if (*childp != NULL)
356 to_wait_for = (*childp)->lwpid;
357
358 while (1)
359 {
360 ret = waitpid (to_wait_for, wstatp, WNOHANG);
361
362 if (ret == -1)
363 {
364 if (errno != ECHILD)
365 perror_with_name ("waitpid");
366 }
367 else if (ret > 0)
368 break;
369
348{
349 int ret;
350 int to_wait_for = -1;
351
352 if (*childp != NULL)
353 to_wait_for = (*childp)->lwpid;
354
355 while (1)
356 {
357 ret = waitpid (to_wait_for, wstatp, WNOHANG);
358
359 if (ret == -1)
360 {
361 if (errno != ECHILD)
362 perror_with_name ("waitpid");
363 }
364 else if (ret > 0)
365 break;
366
370 ret = waitpid (to_wait_for, wstatp, WNOHANG | __WCLONE);
371
372 if (ret == -1)
373 {
374 if (errno != ECHILD)
375 perror_with_name ("waitpid (WCLONE)");
376 }
377 else if (ret > 0)
378 break;
379
380 usleep (1000);
381 }
382
383 if (debug_threads
384 && (!WIFSTOPPED (*wstatp)
385 || (WSTOPSIG (*wstatp) != 32
386 && WSTOPSIG (*wstatp) != 33)))
387 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);

--- 11 unchanged lines hidden (view full) ---

399 find_inferior_id (&all_threads, (*childp)->tid);
400 /* For testing only; i386_stop_pc prints out a diagnostic. */
401 if (the_low_target.get_pc != NULL)
402 get_stop_pc ();
403 }
404}
405
406static int
367 usleep (1000);
368 }
369
370 if (debug_threads
371 && (!WIFSTOPPED (*wstatp)
372 || (WSTOPSIG (*wstatp) != 32
373 && WSTOPSIG (*wstatp) != 33)))
374 fprintf (stderr, "Got an event from %d (%x)\n", ret, *wstatp);

--- 11 unchanged lines hidden (view full) ---

386 find_inferior_id (&all_threads, (*childp)->tid);
387 /* For testing only; i386_stop_pc prints out a diagnostic. */
388 if (the_low_target.get_pc != NULL)
389 get_stop_pc ();
390 }
391}
392
393static int
407linux_wait_for_event (struct thread_info *child)
394fbsd_wait_for_event (struct thread_info *child)
408{
409 CORE_ADDR stop_pc;
410 struct process_info *event_child;
411 int wstat;
412
413 /* Check for a process with a pending status. */
414 /* It is possible that the user changed the pending task's registers since
415 it stopped. We correctly handle the change of PC if we hit a breakpoint

--- 34 unchanged lines hidden (view full) ---

450 events. */
451 while (1)
452 {
453 if (child == NULL)
454 event_child = NULL;
455 else
456 event_child = get_thread_process (child);
457
395{
396 CORE_ADDR stop_pc;
397 struct process_info *event_child;
398 int wstat;
399
400 /* Check for a process with a pending status. */
401 /* It is possible that the user changed the pending task's registers since
402 it stopped. We correctly handle the change of PC if we hit a breakpoint

--- 34 unchanged lines hidden (view full) ---

437 events. */
438 while (1)
439 {
440 if (child == NULL)
441 event_child = NULL;
442 else
443 event_child = get_thread_process (child);
444
458 linux_wait_for_process (&event_child, &wstat);
445 fbsd_wait_for_process (&event_child, &wstat);
459
460 if (event_child == NULL)
461 error ("event from unknown child");
462
463 current_inferior = (struct thread_info *)
464 find_inferior_id (&all_threads, event_child->tid);
465
466 if (using_threads)

--- 27 unchanged lines hidden (view full) ---

494
495 if (WIFSTOPPED (wstat)
496 && WSTOPSIG (wstat) == SIGSTOP
497 && event_child->stop_expected)
498 {
499 if (debug_threads)
500 fprintf (stderr, "Expected stop.\n");
501 event_child->stop_expected = 0;
446
447 if (event_child == NULL)
448 error ("event from unknown child");
449
450 current_inferior = (struct thread_info *)
451 find_inferior_id (&all_threads, event_child->tid);
452
453 if (using_threads)

--- 27 unchanged lines hidden (view full) ---

481
482 if (WIFSTOPPED (wstat)
483 && WSTOPSIG (wstat) == SIGSTOP
484 && event_child->stop_expected)
485 {
486 if (debug_threads)
487 fprintf (stderr, "Expected stop.\n");
488 event_child->stop_expected = 0;
502 linux_resume_one_process (&event_child->head,
489 fbsd_resume_one_process (&event_child->head,
503 event_child->stepping, 0);
504 continue;
505 }
506
507 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
508 thread library? */
490 event_child->stepping, 0);
491 continue;
492 }
493
494 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
495 thread library? */
509 if (WIFSTOPPED (wstat)
510 && (WSTOPSIG (wstat) == __SIGRTMIN
511 || WSTOPSIG (wstat) == __SIGRTMIN + 1))
496 if (WIFSTOPPED (wstat))
512 {
513 if (debug_threads)
514 fprintf (stderr, "Ignored signal %d for %d (LWP %d).\n",
515 WSTOPSIG (wstat), event_child->tid,
516 event_child->head.id);
497 {
498 if (debug_threads)
499 fprintf (stderr, "Ignored signal %d for %d (LWP %d).\n",
500 WSTOPSIG (wstat), event_child->tid,
501 event_child->head.id);
517 linux_resume_one_process (&event_child->head,
502 fbsd_resume_one_process (&event_child->head,
518 event_child->stepping,
519 WSTOPSIG (wstat));
520 continue;
521 }
522 }
523
524 /* If this event was not handled above, and is not a SIGTRAP, report
525 it. */

--- 14 unchanged lines hidden (view full) ---

540 if (event_child->bp_reinsert != 0)
541 {
542 if (debug_threads)
543 fprintf (stderr, "Reinserted breakpoint.\n");
544 reinsert_breakpoint (event_child->bp_reinsert);
545 event_child->bp_reinsert = 0;
546
547 /* Clear the single-stepping flag and SIGTRAP as we resume. */
503 event_child->stepping,
504 WSTOPSIG (wstat));
505 continue;
506 }
507 }
508
509 /* If this event was not handled above, and is not a SIGTRAP, report
510 it. */

--- 14 unchanged lines hidden (view full) ---

525 if (event_child->bp_reinsert != 0)
526 {
527 if (debug_threads)
528 fprintf (stderr, "Reinserted breakpoint.\n");
529 reinsert_breakpoint (event_child->bp_reinsert);
530 event_child->bp_reinsert = 0;
531
532 /* Clear the single-stepping flag and SIGTRAP as we resume. */
548 linux_resume_one_process (&event_child->head, 0, 0);
533 fbsd_resume_one_process (&event_child->head, 0, 0);
549 continue;
550 }
551
552 if (debug_threads)
553 fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n");
554
555 if (check_breakpoints (stop_pc) != 0)
556 {

--- 10 unchanged lines hidden (view full) ---

567 thread could sneak past the removed breakpoint. For the current
568 use of server-side breakpoints (thread creation) this is
569 acceptable; but it needs to be considered before this breakpoint
570 mechanism can be used in more general ways. For some breakpoints
571 it may be necessary to stop all other threads, but that should
572 be avoided where possible.
573
574 If breakpoint_reinsert_addr is NULL, that means that we can
534 continue;
535 }
536
537 if (debug_threads)
538 fprintf (stderr, "Hit a (non-reinsert) breakpoint.\n");
539
540 if (check_breakpoints (stop_pc) != 0)
541 {

--- 10 unchanged lines hidden (view full) ---

552 thread could sneak past the removed breakpoint. For the current
553 use of server-side breakpoints (thread creation) this is
554 acceptable; but it needs to be considered before this breakpoint
555 mechanism can be used in more general ways. For some breakpoints
556 it may be necessary to stop all other threads, but that should
557 be avoided where possible.
558
559 If breakpoint_reinsert_addr is NULL, that means that we can
575 use PTRACE_SINGLESTEP on this platform. Uninsert the breakpoint,
560 use PT_STEP on this platform. Uninsert the breakpoint,
576 mark it for reinsertion, and single-step.
577
578 Otherwise, call the target function to figure out where we need
579 our temporary breakpoint, create it, and continue executing this
580 process. */
581 if (the_low_target.breakpoint_reinsert_addr == NULL)
582 {
583 event_child->bp_reinsert = stop_pc;
584 uninsert_breakpoint (stop_pc);
561 mark it for reinsertion, and single-step.
562
563 Otherwise, call the target function to figure out where we need
564 our temporary breakpoint, create it, and continue executing this
565 process. */
566 if (the_low_target.breakpoint_reinsert_addr == NULL)
567 {
568 event_child->bp_reinsert = stop_pc;
569 uninsert_breakpoint (stop_pc);
585 linux_resume_one_process (&event_child->head, 1, 0);
570 fbsd_resume_one_process (&event_child->head, 1, 0);
586 }
587 else
588 {
589 reinsert_breakpoint_by_bp
590 (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
571 }
572 else
573 {
574 reinsert_breakpoint_by_bp
575 (stop_pc, (*the_low_target.breakpoint_reinsert_addr) ());
591 linux_resume_one_process (&event_child->head, 0, 0);
576 fbsd_resume_one_process (&event_child->head, 0, 0);
592 }
593
594 continue;
595 }
596
597 /* If we were single-stepping, we definitely want to report the
598 SIGTRAP. The single-step operation has completed, so also
599 clear the stepping flag; in general this does not matter,

--- 28 unchanged lines hidden (view full) ---

628
629 /* NOTREACHED */
630 return 0;
631}
632
633/* Wait for process, returns status. */
634
635static unsigned char
577 }
578
579 continue;
580 }
581
582 /* If we were single-stepping, we definitely want to report the
583 SIGTRAP. The single-step operation has completed, so also
584 clear the stepping flag; in general this does not matter,

--- 28 unchanged lines hidden (view full) ---

613
614 /* NOTREACHED */
615 return 0;
616}
617
618/* Wait for process, returns status. */
619
620static unsigned char
636linux_wait (char *status)
621fbsd_wait (char *status)
637{
638 int w;
639 struct thread_info *child = NULL;
640
641retry:
642 /* If we were only supposed to resume one thread, only wait for
643 that thread - if it's still alive. If it died, however - which
644 can happen if we're coming from the thread death case below -

--- 6 unchanged lines hidden (view full) ---

651 cont_thread);
652
653 /* No stepping, no signal - unless one is pending already, of course. */
654 if (child == NULL)
655 {
656 struct thread_resume resume_info;
657 resume_info.thread = -1;
658 resume_info.step = resume_info.sig = resume_info.leave_stopped = 0;
622{
623 int w;
624 struct thread_info *child = NULL;
625
626retry:
627 /* If we were only supposed to resume one thread, only wait for
628 that thread - if it's still alive. If it died, however - which
629 can happen if we're coming from the thread death case below -

--- 6 unchanged lines hidden (view full) ---

636 cont_thread);
637
638 /* No stepping, no signal - unless one is pending already, of course. */
639 if (child == NULL)
640 {
641 struct thread_resume resume_info;
642 resume_info.thread = -1;
643 resume_info.step = resume_info.sig = resume_info.leave_stopped = 0;
659 linux_resume (&resume_info);
644 fbsd_resume (&resume_info);
660 }
661 }
662
663 enable_async_io ();
664 unblock_async_io ();
645 }
646 }
647
648 enable_async_io ();
649 unblock_async_io ();
665 w = linux_wait_for_event (child);
650 w = fbsd_wait_for_event (child);
666 stop_all_processes ();
667 disable_async_io ();
668
669 /* If we are waiting for a particular child, and it exited,
651 stop_all_processes ();
652 disable_async_io ();
653
654 /* If we are waiting for a particular child, and it exited,
670 linux_wait_for_event will return its exit status. Similarly if
655 fbsd_wait_for_event will return its exit status. Similarly if
671 the last child exited. If this is not the last child, however,
672 do not report it as exited until there is a 'thread exited' response
673 available in the remote protocol. Instead, just wait for another event.
674 This should be safe, because if the thread crashed we will already
675 have reported the termination signal to GDB; that should stop any
676 in-progress stepping operations, etc.
677
678 Report the exit status of the last thread to exit. This matches

--- 62 unchanged lines hidden (view full) ---

741
742 if (process->stopped)
743 return;
744
745 saved_inferior = current_inferior;
746 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
747 thread = (struct thread_info *) find_inferior_id (&all_threads,
748 process->tid);
656 the last child exited. If this is not the last child, however,
657 do not report it as exited until there is a 'thread exited' response
658 available in the remote protocol. Instead, just wait for another event.
659 This should be safe, because if the thread crashed we will already
660 have reported the termination signal to GDB; that should stop any
661 in-progress stepping operations, etc.
662
663 Report the exit status of the last thread to exit. This matches

--- 62 unchanged lines hidden (view full) ---

726
727 if (process->stopped)
728 return;
729
730 saved_inferior = current_inferior;
731 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
732 thread = (struct thread_info *) find_inferior_id (&all_threads,
733 process->tid);
749 wstat = linux_wait_for_event (thread);
734 wstat = fbsd_wait_for_event (thread);
750
751 /* If we stopped with a non-SIGSTOP signal, save it for later
752 and record the pending SIGSTOP. If the process exited, just
753 return. */
754 if (WIFSTOPPED (wstat)
755 && WSTOPSIG (wstat) != SIGSTOP)
756 {
757 if (debug_threads)
758 fprintf (stderr, "Stopped with non-sigstop signal\n");
759 process->status_pending_p = 1;
760 process->status_pending = wstat;
761 process->stop_expected = 1;
762 }
763
735
736 /* If we stopped with a non-SIGSTOP signal, save it for later
737 and record the pending SIGSTOP. If the process exited, just
738 return. */
739 if (WIFSTOPPED (wstat)
740 && WSTOPSIG (wstat) != SIGSTOP)
741 {
742 if (debug_threads)
743 fprintf (stderr, "Stopped with non-sigstop signal\n");
744 process->status_pending_p = 1;
745 process->status_pending = wstat;
746 process->stop_expected = 1;
747 }
748
764 if (linux_thread_alive (saved_tid))
749 if (fbsd_thread_alive (saved_tid))
765 current_inferior = saved_inferior;
766 else
767 {
768 if (debug_threads)
769 fprintf (stderr, "Previously current thread died.\n");
770
771 /* Set a valid thread as current. */
772 set_desired_inferior (0);

--- 9 unchanged lines hidden (view full) ---

782 stopping_threads = 0;
783}
784
785/* Resume execution of the inferior process.
786 If STEP is nonzero, single-step it.
787 If SIGNAL is nonzero, give it that signal. */
788
789static void
750 current_inferior = saved_inferior;
751 else
752 {
753 if (debug_threads)
754 fprintf (stderr, "Previously current thread died.\n");
755
756 /* Set a valid thread as current. */
757 set_desired_inferior (0);

--- 9 unchanged lines hidden (view full) ---

767 stopping_threads = 0;
768}
769
770/* Resume execution of the inferior process.
771 If STEP is nonzero, single-step it.
772 If SIGNAL is nonzero, give it that signal. */
773
774static void
790linux_resume_one_process (struct inferior_list_entry *entry,
775fbsd_resume_one_process (struct inferior_list_entry *entry,
791 int step, int signal)
792{
793 struct process_info *process = (struct process_info *) entry;
794 struct thread_info *saved_inferior;
795
796 if (process->stopped == 0)
797 return;
798

--- 24 unchanged lines hidden (view full) ---

823
824 /* This bit needs some thinking about. If we get a signal that
825 we must report while a single-step reinsert is still pending,
826 we often end up resuming the thread. It might be better to
827 (ew) allow a stack of pending events; then we could be sure that
828 the reinsert happened right away and not lose any signals.
829
830 Making this stack would also shrink the window in which breakpoints are
776 int step, int signal)
777{
778 struct process_info *process = (struct process_info *) entry;
779 struct thread_info *saved_inferior;
780
781 if (process->stopped == 0)
782 return;
783

--- 24 unchanged lines hidden (view full) ---

808
809 /* This bit needs some thinking about. If we get a signal that
810 we must report while a single-step reinsert is still pending,
811 we often end up resuming the thread. It might be better to
812 (ew) allow a stack of pending events; then we could be sure that
813 the reinsert happened right away and not lose any signals.
814
815 Making this stack would also shrink the window in which breakpoints are
831 uninserted (see comment in linux_wait_for_process) but not enough for
816 uninserted (see comment in fbsd_wait_for_process) but not enough for
832 complete correctness, so it won't solve that problem. It may be
833 worthwhile just to solve this one, however. */
834 if (process->bp_reinsert != 0)
835 {
836 if (debug_threads)
837 fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert);
838 if (step == 0)
839 fprintf (stderr, "BAD - reinserting but not stepping.\n");

--- 26 unchanged lines hidden (view full) ---

866 *p_sig = NULL;
867 }
868
869 regcache_invalidate_one ((struct inferior_list_entry *)
870 get_process_thread (process));
871 errno = 0;
872 process->stopped = 0;
873 process->stepping = step;
817 complete correctness, so it won't solve that problem. It may be
818 worthwhile just to solve this one, however. */
819 if (process->bp_reinsert != 0)
820 {
821 if (debug_threads)
822 fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert);
823 if (step == 0)
824 fprintf (stderr, "BAD - reinserting but not stepping.\n");

--- 26 unchanged lines hidden (view full) ---

851 *p_sig = NULL;
852 }
853
854 regcache_invalidate_one ((struct inferior_list_entry *)
855 get_process_thread (process));
856 errno = 0;
857 process->stopped = 0;
858 process->stepping = step;
874 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal);
859 ptrace (step ? PT_STEP : PT_CONTINUE, process->lwpid, (PTRACE_ARG3_TYPE) 1, signal);
875
876 current_inferior = saved_inferior;
877 if (errno)
878 perror_with_name ("ptrace");
879}
880
881static struct thread_resume *resume_ptr;
882
883/* This function is called once per thread. We look up the thread
884 in RESUME_PTR, and mark the thread with a pointer to the appropriate
885 resume request.
886
887 This algorithm is O(threads * resume elements), but resume elements
888 is small (and will remain small at least until GDB supports thread
889 suspension). */
890static void
860
861 current_inferior = saved_inferior;
862 if (errno)
863 perror_with_name ("ptrace");
864}
865
866static struct thread_resume *resume_ptr;
867
868/* This function is called once per thread. We look up the thread
869 in RESUME_PTR, and mark the thread with a pointer to the appropriate
870 resume request.
871
872 This algorithm is O(threads * resume elements), but resume elements
873 is small (and will remain small at least until GDB supports thread
874 suspension). */
875static void
891linux_set_resume_request (struct inferior_list_entry *entry)
876fbsd_set_resume_request (struct inferior_list_entry *entry)
892{
893 struct process_info *process;
894 struct thread_info *thread;
895 int ndx;
896
897 thread = (struct thread_info *) entry;
898 process = get_thread_process (thread);
899

--- 6 unchanged lines hidden (view full) ---

906
907/* This function is called once per thread. We check the thread's resume
908 request, which will tell us whether to resume, step, or leave the thread
909 stopped; and what signal, if any, it should be sent. For threads which
910 we aren't explicitly told otherwise, we preserve the stepping flag; this
911 is used for stepping over gdbserver-placed breakpoints. */
912
913static void
877{
878 struct process_info *process;
879 struct thread_info *thread;
880 int ndx;
881
882 thread = (struct thread_info *) entry;
883 process = get_thread_process (thread);
884

--- 6 unchanged lines hidden (view full) ---

891
892/* This function is called once per thread. We check the thread's resume
893 request, which will tell us whether to resume, step, or leave the thread
894 stopped; and what signal, if any, it should be sent. For threads which
895 we aren't explicitly told otherwise, we preserve the stepping flag; this
896 is used for stepping over gdbserver-placed breakpoints. */
897
898static void
914linux_continue_one_thread (struct inferior_list_entry *entry)
899fbsd_continue_one_thread (struct inferior_list_entry *entry)
915{
916 struct process_info *process;
917 struct thread_info *thread;
918 int step;
919
920 thread = (struct thread_info *) entry;
921 process = get_thread_process (thread);
922
923 if (process->resume->leave_stopped)
924 return;
925
926 if (process->resume->thread == -1)
927 step = process->stepping || process->resume->step;
928 else
929 step = process->resume->step;
930
900{
901 struct process_info *process;
902 struct thread_info *thread;
903 int step;
904
905 thread = (struct thread_info *) entry;
906 process = get_thread_process (thread);
907
908 if (process->resume->leave_stopped)
909 return;
910
911 if (process->resume->thread == -1)
912 step = process->stepping || process->resume->step;
913 else
914 step = process->resume->step;
915
931 linux_resume_one_process (&process->head, step, process->resume->sig);
916 fbsd_resume_one_process (&process->head, step, process->resume->sig);
932
933 process->resume = NULL;
934}
935
936/* This function is called once per thread. We check the thread's resume
937 request, which will tell us whether to resume, step, or leave the thread
938 stopped; and what signal, if any, it should be sent. We queue any needed
939 signals, since we won't actually resume. We already have a pending event
940 to report, so we don't need to preserve any step requests; they should
941 be re-issued if necessary. */
942
943static void
917
918 process->resume = NULL;
919}
920
921/* This function is called once per thread. We check the thread's resume
922 request, which will tell us whether to resume, step, or leave the thread
923 stopped; and what signal, if any, it should be sent. We queue any needed
924 signals, since we won't actually resume. We already have a pending event
925 to report, so we don't need to preserve any step requests; they should
926 be re-issued if necessary. */
927
928static void
944linux_queue_one_thread (struct inferior_list_entry *entry)
929fbsd_queue_one_thread (struct inferior_list_entry *entry)
945{
946 struct process_info *process;
947 struct thread_info *thread;
948
949 thread = (struct thread_info *) entry;
950 process = get_thread_process (thread);
951
952 if (process->resume->leave_stopped)

--- 14 unchanged lines hidden (view full) ---

967
968/* Set DUMMY if this process has an interesting status pending. */
969static int
970resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
971{
972 struct process_info *process = (struct process_info *) entry;
973
974 /* Processes which will not be resumed are not interesting, because
930{
931 struct process_info *process;
932 struct thread_info *thread;
933
934 thread = (struct thread_info *) entry;
935 process = get_thread_process (thread);
936
937 if (process->resume->leave_stopped)

--- 14 unchanged lines hidden (view full) ---

952
953/* Set DUMMY if this process has an interesting status pending. */
954static int
955resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
956{
957 struct process_info *process = (struct process_info *) entry;
958
959 /* Processes which will not be resumed are not interesting, because
975 we might not wait for them next time through linux_wait. */
960 we might not wait for them next time through fbsd_wait. */
976 if (process->resume->leave_stopped)
977 return 0;
978
979 /* If this thread has a removed breakpoint, we won't have any
980 events to report later, so check now. check_removed_breakpoint
981 may clear status_pending_p. We avoid calling check_removed_breakpoint
982 for any thread that we are not otherwise going to resume - this
983 lets us preserve stopped status when two threads hit a breakpoint.

--- 5 unchanged lines hidden (view full) ---

989
990 if (process->status_pending_p)
991 * (int *) flag_p = 1;
992
993 return 0;
994}
995
996static void
961 if (process->resume->leave_stopped)
962 return 0;
963
964 /* If this thread has a removed breakpoint, we won't have any
965 events to report later, so check now. check_removed_breakpoint
966 may clear status_pending_p. We avoid calling check_removed_breakpoint
967 for any thread that we are not otherwise going to resume - this
968 lets us preserve stopped status when two threads hit a breakpoint.

--- 5 unchanged lines hidden (view full) ---

974
975 if (process->status_pending_p)
976 * (int *) flag_p = 1;
977
978 return 0;
979}
980
981static void
997linux_resume (struct thread_resume *resume_info)
982fbsd_resume (struct thread_resume *resume_info)
998{
999 int pending_flag;
1000
1001 /* Yes, the use of a global here is rather ugly. */
1002 resume_ptr = resume_info;
1003
983{
984 int pending_flag;
985
986 /* Yes, the use of a global here is rather ugly. */
987 resume_ptr = resume_info;
988
1004 for_each_inferior (&all_threads, linux_set_resume_request);
989 for_each_inferior (&all_threads, fbsd_set_resume_request);
1005
1006 /* If there is a thread which would otherwise be resumed, which
1007 has a pending status, then don't resume any threads - we can just
1008 report the pending status. Make sure to queue any signals
1009 that would otherwise be sent. */
1010 pending_flag = 0;
1011 find_inferior (&all_processes, resume_status_pending_p, &pending_flag);
1012
1013 if (debug_threads)
1014 {
1015 if (pending_flag)
1016 fprintf (stderr, "Not resuming, pending status\n");
1017 else
1018 fprintf (stderr, "Resuming, no pending status\n");
1019 }
1020
1021 if (pending_flag)
990
991 /* If there is a thread which would otherwise be resumed, which
992 has a pending status, then don't resume any threads - we can just
993 report the pending status. Make sure to queue any signals
994 that would otherwise be sent. */
995 pending_flag = 0;
996 find_inferior (&all_processes, resume_status_pending_p, &pending_flag);
997
998 if (debug_threads)
999 {
1000 if (pending_flag)
1001 fprintf (stderr, "Not resuming, pending status\n");
1002 else
1003 fprintf (stderr, "Resuming, no pending status\n");
1004 }
1005
1006 if (pending_flag)
1022 for_each_inferior (&all_threads, linux_queue_one_thread);
1007 for_each_inferior (&all_threads, fbsd_queue_one_thread);
1023 else
1024 {
1025 block_async_io ();
1026 enable_async_io ();
1008 else
1009 {
1010 block_async_io ();
1011 enable_async_io ();
1027 for_each_inferior (&all_threads, linux_continue_one_thread);
1012 for_each_inferior (&all_threads, fbsd_continue_one_thread);
1028 }
1029}
1030
1013 }
1014}
1015
1031#ifdef HAVE_LINUX_USRREGS
1032
1016
1033int
1034register_addr (int regnum)
1035{
1036 int addr;
1037
1038 if (regnum < 0 || regnum >= the_low_target.num_regs)
1039 error ("Invalid register number %d.", regnum);
1040
1041 addr = the_low_target.regmap[regnum];
1042
1043 return addr;
1044}
1045
1046/* Fetch one register. */
1047static void
1048fetch_register (int regno)
1049{
1050 CORE_ADDR regaddr;
1051 register int i;
1052 char *buf;
1053
1054 if (regno >= the_low_target.num_regs)
1055 return;
1056 if ((*the_low_target.cannot_fetch_register) (regno))
1057 return;
1058
1059 regaddr = register_addr (regno);
1060 if (regaddr == -1)
1061 return;
1062 buf = alloca (register_size (regno));
1063 for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE))
1064 {
1065 errno = 0;
1066 *(PTRACE_XFER_TYPE *) (buf + i) =
1067 ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
1068 regaddr += sizeof (PTRACE_XFER_TYPE);
1069 if (errno != 0)
1070 {
1071 /* Warning, not error, in case we are attached; sometimes the
1072 kernel doesn't let us at the registers. */
1073 char *err = strerror (errno);
1074 char *msg = alloca (strlen (err) + 128);
1075 sprintf (msg, "reading register %d: %s", regno, err);
1076 error (msg);
1077 goto error_exit;
1078 }
1079 }
1080 supply_register (regno, buf);
1081
1082error_exit:;
1083}
1084
1085/* Fetch all registers, or just one, from the child process. */
1086static void
1087usr_fetch_inferior_registers (int regno)
1088{
1089 if (regno == -1 || regno == 0)
1090 for (regno = 0; regno < the_low_target.num_regs; regno++)
1091 fetch_register (regno);
1092 else
1093 fetch_register (regno);
1094}
1095
1096/* Store our register values back into the inferior.
1097 If REGNO is -1, do this for all registers.
1098 Otherwise, REGNO specifies which register (so we can save time). */
1099static void
1100usr_store_inferior_registers (int regno)
1101{
1102 CORE_ADDR regaddr;
1103 int i;
1104 char *buf;
1105
1106 if (regno >= 0)
1107 {
1108 if (regno >= the_low_target.num_regs)
1109 return;
1110
1111 if ((*the_low_target.cannot_store_register) (regno) == 1)
1112 return;
1113
1114 regaddr = register_addr (regno);
1115 if (regaddr == -1)
1116 return;
1117 errno = 0;
1118 buf = alloca (register_size (regno));
1119 collect_register (regno, buf);
1120 for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE))
1121 {
1122 errno = 0;
1123 ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
1124 *(PTRACE_XFER_TYPE *) (buf + i));
1125 if (errno != 0)
1126 {
1127 if ((*the_low_target.cannot_store_register) (regno) == 0)
1128 {
1129 char *err = strerror (errno);
1130 char *msg = alloca (strlen (err) + 128);
1131 sprintf (msg, "writing register %d: %s",
1132 regno, err);
1133 error (msg);
1134 return;
1135 }
1136 }
1137 regaddr += sizeof (PTRACE_XFER_TYPE);
1138 }
1139 }
1140 else
1141 for (regno = 0; regno < the_low_target.num_regs; regno++)
1142 usr_store_inferior_registers (regno);
1143}
1144#endif /* HAVE_LINUX_USRREGS */
1145
1146
1147
1148#ifdef HAVE_LINUX_REGSETS
1149
1150static int
1151regsets_fetch_inferior_registers ()
1152{
1153 struct regset_info *regset;
1154
1155 regset = target_regsets;
1156
1157 while (regset->size >= 0)
1158 {
1159 void *buf;
1160 int res;
1161
1162 if (regset->size == 0)
1163 {
1164 regset ++;
1165 continue;
1166 }
1167
1168 buf = malloc (regset->size);
1017static int
1018regsets_fetch_inferior_registers ()
1019{
1020 struct regset_info *regset;
1021
1022 regset = target_regsets;
1023
1024 while (regset->size >= 0)
1025 {
1026 void *buf;
1027 int res;
1028
1029 if (regset->size == 0)
1030 {
1031 regset ++;
1032 continue;
1033 }
1034
1035 buf = malloc (regset->size);
1169 res = ptrace (regset->get_request, inferior_pid, 0, buf);
1036 res = ptrace (regset->get_request, inferior_pid, (PTRACE_ARG3_TYPE) buf, 0);
1170 if (res < 0)
1171 {
1037 if (res < 0)
1038 {
1172 if (errno == EIO)
1173 {
1174 /* If we get EIO on the first regset, do not try regsets again.
1175 If we get EIO on a later regset, disable that regset. */
1176 if (regset == target_regsets)
1177 {
1178 use_regsets_p = 0;
1179 return -1;
1180 }
1181 else
1182 {
1183 regset->size = 0;
1184 continue;
1185 }
1186 }
1187 else
1188 {
1189 char s[256];
1190 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
1191 inferior_pid);
1192 perror (s);
1193 }
1039 char s[256];
1040 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
1041 inferior_pid);
1042 perror (s);
1194 }
1195 regset->store_function (buf);
1196 regset ++;
1197 }
1198 return 0;
1199}
1200
1201static int

--- 11 unchanged lines hidden (view full) ---

1213 if (regset->size == 0)
1214 {
1215 regset ++;
1216 continue;
1217 }
1218
1219 buf = malloc (regset->size);
1220 regset->fill_function (buf);
1043 }
1044 regset->store_function (buf);
1045 regset ++;
1046 }
1047 return 0;
1048}
1049
1050static int

--- 11 unchanged lines hidden (view full) ---

1062 if (regset->size == 0)
1063 {
1064 regset ++;
1065 continue;
1066 }
1067
1068 buf = malloc (regset->size);
1069 regset->fill_function (buf);
1221 res = ptrace (regset->set_request, inferior_pid, 0, buf);
1070 res = ptrace (regset->set_request, inferior_pid, (PTRACE_ARG3_TYPE) buf, 0);
1222 if (res < 0)
1223 {
1071 if (res < 0)
1072 {
1224 if (errno == EIO)
1225 {
1226 /* If we get EIO on the first regset, do not try regsets again.
1227 If we get EIO on a later regset, disable that regset. */
1228 if (regset == target_regsets)
1229 {
1230 use_regsets_p = 0;
1231 return -1;
1232 }
1233 else
1234 {
1235 regset->size = 0;
1236 continue;
1237 }
1238 }
1239 else
1240 {
1241 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1242 }
1073 perror ("Warning: ptrace(regsets_store_inferior_registers)");
1243 }
1244 regset ++;
1245 free (buf);
1246 }
1247 return 0;
1248}
1249
1074 }
1075 regset ++;
1076 free (buf);
1077 }
1078 return 0;
1079}
1080
1250#endif /* HAVE_LINUX_REGSETS */
1251
1252
1253void
1081void
1254linux_fetch_registers (int regno)
1082fbsd_fetch_registers (int regno)
1255{
1083{
1256#ifdef HAVE_LINUX_REGSETS
1257 if (use_regsets_p)
1258 {
1259 if (regsets_fetch_inferior_registers () == 0)
1260 return;
1261 }
1262#endif
1263#ifdef HAVE_LINUX_USRREGS
1264 usr_fetch_inferior_registers (regno);
1265#endif
1084 regsets_fetch_inferior_registers ();
1266}
1267
1268void
1085}
1086
1087void
1269linux_store_registers (int regno)
1088fbsd_store_registers (int regno)
1270{
1089{
1271#ifdef HAVE_LINUX_REGSETS
1272 if (use_regsets_p)
1273 {
1274 if (regsets_store_inferior_registers () == 0)
1275 return;
1276 }
1277#endif
1278#ifdef HAVE_LINUX_USRREGS
1279 usr_store_inferior_registers (regno);
1280#endif
1090 regsets_store_inferior_registers ();
1281}
1282
1283
1284/* Copy LEN bytes from inferior's memory starting at MEMADDR
1285 to debugger memory starting at MYADDR. */
1286
1287static int
1091}
1092
1093
1094/* Copy LEN bytes from inferior's memory starting at MEMADDR
1095 to debugger memory starting at MYADDR. */
1096
1097static int
1288linux_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
1098fbsd_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
1289{
1290 register int i;
1291 /* Round starting address down to longword boundary. */
1292 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1293 /* Round ending address up; get number of longwords that makes. */
1294 register int count
1295 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
1296 / sizeof (PTRACE_XFER_TYPE);
1297 /* Allocate buffer of that many longwords. */
1298 register PTRACE_XFER_TYPE *buffer
1299 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1300
1301 /* Read all the longwords */
1302 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1303 {
1304 errno = 0;
1099{
1100 register int i;
1101 /* Round starting address down to longword boundary. */
1102 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1103 /* Round ending address up; get number of longwords that makes. */
1104 register int count
1105 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
1106 / sizeof (PTRACE_XFER_TYPE);
1107 /* Allocate buffer of that many longwords. */
1108 register PTRACE_XFER_TYPE *buffer
1109 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1110
1111 /* Read all the longwords */
1112 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1113 {
1114 errno = 0;
1305 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
1115 buffer[i] = ptrace (PT_READ_D, inferior_pid, (PTRACE_ARG3_TYPE) (intptr_t)addr, 0);
1306 if (errno)
1307 return errno;
1308 }
1309
1310 /* Copy appropriate bytes out of the buffer. */
1311 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
1312
1313 return 0;
1314}
1315
1316/* Copy LEN bytes of data from debugger memory at MYADDR
1317 to inferior's memory at MEMADDR.
1318 On failure (cannot write the inferior)
1319 returns the value of errno. */
1320
1321static int
1116 if (errno)
1117 return errno;
1118 }
1119
1120 /* Copy appropriate bytes out of the buffer. */
1121 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
1122
1123 return 0;
1124}
1125
1126/* Copy LEN bytes of data from debugger memory at MYADDR
1127 to inferior's memory at MEMADDR.
1128 On failure (cannot write the inferior)
1129 returns the value of errno. */
1130
1131static int
1322linux_write_memory (CORE_ADDR memaddr, const char *myaddr, int len)
1132fbsd_write_memory (CORE_ADDR memaddr, const char *myaddr, int len)
1323{
1324 register int i;
1325 /* Round starting address down to longword boundary. */
1326 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1327 /* Round ending address up; get number of longwords that makes. */
1328 register int count
1329 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
1330 /* Allocate buffer of that many longwords. */
1331 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1332 extern int errno;
1333
1334 if (debug_threads)
1335 {
1336 fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
1337 }
1338
1339 /* Fill start and end extra bytes of buffer with existing memory data. */
1340
1133{
1134 register int i;
1135 /* Round starting address down to longword boundary. */
1136 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1137 /* Round ending address up; get number of longwords that makes. */
1138 register int count
1139 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
1140 /* Allocate buffer of that many longwords. */
1141 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1142 extern int errno;
1143
1144 if (debug_threads)
1145 {
1146 fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
1147 }
1148
1149 /* Fill start and end extra bytes of buffer with existing memory data. */
1150
1341 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
1342 (PTRACE_ARG3_TYPE) addr, 0);
1151 buffer[0] = ptrace (PT_READ_D, inferior_pid,
1152 (PTRACE_ARG3_TYPE) (intptr_t)addr, 0);
1343
1344 if (count > 1)
1345 {
1346 buffer[count - 1]
1153
1154 if (count > 1)
1155 {
1156 buffer[count - 1]
1347 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
1348 (PTRACE_ARG3_TYPE) (addr + (count - 1)
1157 = ptrace (PT_READ_D, inferior_pid,
1158 (PTRACE_ARG3_TYPE) (intptr_t) (addr + (count - 1)
1349 * sizeof (PTRACE_XFER_TYPE)),
1350 0);
1351 }
1352
1353 /* Copy data to be written over corresponding part of buffer */
1354
1355 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
1356
1357 /* Write the entire buffer. */
1358
1359 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1360 {
1361 errno = 0;
1159 * sizeof (PTRACE_XFER_TYPE)),
1160 0);
1161 }
1162
1163 /* Copy data to be written over corresponding part of buffer */
1164
1165 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
1166
1167 /* Write the entire buffer. */
1168
1169 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1170 {
1171 errno = 0;
1362 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
1172 ptrace (PT_WRITE_D, inferior_pid, (PTRACE_ARG3_TYPE) (intptr_t)addr, buffer[i]);
1363 if (errno)
1364 return errno;
1365 }
1366
1367 return 0;
1368}
1369
1370static void
1173 if (errno)
1174 return errno;
1175 }
1176
1177 return 0;
1178}
1179
1180static void
1371linux_look_up_symbols (void)
1181fbsd_look_up_symbols (void)
1372{
1373#ifdef USE_THREAD_DB
1374 if (using_threads)
1375 return;
1376
1377 using_threads = thread_db_init ();
1378#endif
1379}
1380
1381static void
1182{
1183#ifdef USE_THREAD_DB
1184 if (using_threads)
1185 return;
1186
1187 using_threads = thread_db_init ();
1188#endif
1189}
1190
1191static void
1382linux_send_signal (int signum)
1192fbsd_send_signal (int signum)
1383{
1384 extern int signal_pid;
1385
1386 if (cont_thread > 0)
1387 {
1388 struct process_info *process;
1389
1390 process = get_thread_process (current_inferior);
1391 kill (process->lwpid, signum);
1392 }
1393 else
1394 kill (signal_pid, signum);
1395}
1396
1397/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1398 to debugger memory starting at MYADDR. */
1399
1400static int
1193{
1194 extern int signal_pid;
1195
1196 if (cont_thread > 0)
1197 {
1198 struct process_info *process;
1199
1200 process = get_thread_process (current_inferior);
1201 kill (process->lwpid, signum);
1202 }
1203 else
1204 kill (signal_pid, signum);
1205}
1206
1207/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1208 to debugger memory starting at MYADDR. */
1209
1210static int
1401linux_read_auxv (CORE_ADDR offset, char *myaddr, unsigned int len)
1211fbsd_read_auxv (CORE_ADDR offset, char *myaddr, unsigned int len)
1402{
1403 char filename[PATH_MAX];
1404 int fd, n;
1405
1406 snprintf (filename, sizeof filename, "/proc/%d/auxv", inferior_pid);
1407
1408 fd = open (filename, O_RDONLY);
1409 if (fd < 0)

--- 6 unchanged lines hidden (view full) ---

1416 n = read (fd, myaddr, len);
1417
1418 close (fd);
1419
1420 return n;
1421}
1422
1423
1212{
1213 char filename[PATH_MAX];
1214 int fd, n;
1215
1216 snprintf (filename, sizeof filename, "/proc/%d/auxv", inferior_pid);
1217
1218 fd = open (filename, O_RDONLY);
1219 if (fd < 0)

--- 6 unchanged lines hidden (view full) ---

1226 n = read (fd, myaddr, len);
1227
1228 close (fd);
1229
1230 return n;
1231}
1232
1233
1424static struct target_ops linux_target_ops = {
1425 linux_create_inferior,
1426 linux_attach,
1427 linux_kill,
1428 linux_detach,
1429 linux_thread_alive,
1430 linux_resume,
1431 linux_wait,
1432 linux_fetch_registers,
1433 linux_store_registers,
1434 linux_read_memory,
1435 linux_write_memory,
1436 linux_look_up_symbols,
1437 linux_send_signal,
1438 linux_read_auxv,
1234static struct target_ops fbsd_target_ops = {
1235 fbsd_create_inferior,
1236 fbsd_attach,
1237 fbsd_kill,
1238 fbsd_detach,
1239 fbsd_thread_alive,
1240 fbsd_resume,
1241 fbsd_wait,
1242 fbsd_fetch_registers,
1243 fbsd_store_registers,
1244 fbsd_read_memory,
1245 fbsd_write_memory,
1246 fbsd_look_up_symbols,
1247 fbsd_send_signal,
1248 fbsd_read_auxv,
1439};
1440
1441static void
1249};
1250
1251static void
1442linux_init_signals ()
1252fbsd_init_signals ()
1443{
1253{
1444 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1445 to find what the cancel signal actually is. */
1446 signal (__SIGRTMIN+1, SIG_IGN);
1447}
1448
1449void
1450initialize_low (void)
1451{
1452 using_threads = 0;
1254}
1255
1256void
1257initialize_low (void)
1258{
1259 using_threads = 0;
1453 set_target_ops (&linux_target_ops);
1260 set_target_ops (&fbsd_target_ops);
1454 set_breakpoint_data (the_low_target.breakpoint,
1455 the_low_target.breakpoint_len);
1456 init_registers ();
1261 set_breakpoint_data (the_low_target.breakpoint,
1262 the_low_target.breakpoint_len);
1263 init_registers ();
1457 linux_init_signals ();
1264 fbsd_init_signals ();
1458}
1265}