* infcmd.c (attach_command): Reread symbols if we already have
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-low.c
CommitLineData
da6d8c04 1/* Low level interface to ptrace, for the remote server for GDB.
5544ad89 2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
da6d8c04
DJ
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
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "server.h"
58caa3dc 23#include "linux-low.h"
da6d8c04 24
58caa3dc 25#include <sys/wait.h>
da6d8c04
DJ
26#include <stdio.h>
27#include <sys/param.h>
28#include <sys/dir.h>
29#include <sys/ptrace.h>
30#include <sys/user.h>
31#include <signal.h>
32#include <sys/ioctl.h>
33#include <fcntl.h>
d07c63e7 34#include <string.h>
0a30fbc4
DJ
35#include <stdlib.h>
36#include <unistd.h>
fa6a77dc 37#include <errno.h>
da6d8c04 38
0d62e5e8
DJ
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.
611cb4a5 42
0d62e5e8
DJ
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
54static void linux_resume_one_process (struct inferior_list_entry *entry,
55 int step, int signal);
64386c31 56static void linux_resume (struct thread_resume *resume_info);
0d62e5e8
DJ
57static void stop_all_processes (void);
58static int linux_wait_for_event (struct thread_info *child);
59
60struct pending_signals
61{
62 int signal;
63 struct pending_signals *prev;
64};
611cb4a5 65
d844cde6 66#define PTRACE_ARG3_TYPE long
c6ecbae5 67#define PTRACE_XFER_TYPE long
da6d8c04 68
58caa3dc
DJ
69#ifdef HAVE_LINUX_REGSETS
70static int use_regsets_p = 1;
71#endif
72
0d62e5e8
DJ
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:
84 If we were single-stepping this process using PTRACE_SINGLESTEP,
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
93 must resume at the decremented PC.
94
95 (Presumably, not yet tested) On a non-decr_pc_after_break machine
96 with hardware or kernel single-step:
97 If we single-step over a breakpoint instruction, our PC will
98 point at the following instruction. If we continue and hit a
99 breakpoint instruction, our PC will point at the breakpoint
100 instruction. */
101
102static CORE_ADDR
103get_stop_pc (void)
104{
105 CORE_ADDR stop_pc = (*the_low_target.get_pc) ();
106
107 if (get_thread_process (current_inferior)->stepping)
108 return stop_pc;
109 else
110 return stop_pc - the_low_target.decr_pc_after_break;
111}
ce3a066d 112
0d62e5e8
DJ
113static void *
114add_process (int pid)
611cb4a5 115{
0d62e5e8
DJ
116 struct process_info *process;
117
118 process = (struct process_info *) malloc (sizeof (*process));
119 memset (process, 0, sizeof (*process));
120
121 process->head.id = pid;
122
123 /* Default to tid == lwpid == pid. */
124 process->tid = pid;
125 process->lwpid = pid;
126
127 add_inferior_to_list (&all_processes, &process->head);
128
129 return process;
130}
611cb4a5 131
da6d8c04
DJ
132/* Start an inferior process and returns its pid.
133 ALLARGS is a vector of program-name and args. */
134
ce3a066d
DJ
135static int
136linux_create_inferior (char *program, char **allargs)
da6d8c04 137{
0d62e5e8 138 void *new_process;
da6d8c04
DJ
139 int pid;
140
141 pid = fork ();
142 if (pid < 0)
143 perror_with_name ("fork");
144
145 if (pid == 0)
146 {
147 ptrace (PTRACE_TRACEME, 0, 0, 0);
148
254787d4 149 signal (__SIGRTMIN + 1, SIG_DFL);
0d62e5e8 150
a9fa9f7d
DJ
151 setpgid (0, 0);
152
da6d8c04
DJ
153 execv (program, allargs);
154
155 fprintf (stderr, "Cannot exec %s: %s.\n", program,
d07c63e7 156 strerror (errno));
da6d8c04
DJ
157 fflush (stderr);
158 _exit (0177);
159 }
160
0d62e5e8
DJ
161 new_process = add_process (pid);
162 add_thread (pid, new_process);
611cb4a5 163
a9fa9f7d 164 return pid;
da6d8c04
DJ
165}
166
167/* Attach to an inferior process. */
168
0d62e5e8
DJ
169void
170linux_attach_lwp (int pid, int tid)
da6d8c04 171{
0d62e5e8 172 struct process_info *new_process;
611cb4a5 173
da6d8c04
DJ
174 if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0)
175 {
176 fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid,
43d5792c 177 strerror (errno), errno);
da6d8c04 178 fflush (stderr);
0d62e5e8
DJ
179
180 /* If we fail to attach to an LWP, just return. */
181 if (!using_threads)
182 _exit (0177);
183 return;
da6d8c04
DJ
184 }
185
0d62e5e8
DJ
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
192 in linux_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
204linux_attach (int pid)
205{
206 struct process_info *process;
207
208 linux_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
da6d8c04
DJ
214 return 0;
215}
216
217/* Kill the inferior process. Make us have no inferior. */
218
ce3a066d 219static void
0d62e5e8 220linux_kill_one_process (struct inferior_list_entry *entry)
da6d8c04 221{
0d62e5e8
DJ
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 {
228 ptrace (PTRACE_KILL, pid_of (process), 0, 0);
229
230 /* Make sure it died. The loop is most likely unnecessary. */
231 wstat = linux_wait_for_event (thread);
232 } while (WIFSTOPPED (wstat));
da6d8c04
DJ
233}
234
0d62e5e8
DJ
235static void
236linux_kill (void)
237{
238 for_each_inferior (&all_threads, linux_kill_one_process);
239}
240
6ad8ae5c
DJ
241static void
242linux_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
247 ptrace (PTRACE_DETACH, pid_of (process), 0, 0);
248}
249
250static void
251linux_detach (void)
252{
253 for_each_inferior (&all_threads, linux_detach_one_process);
254}
255
256/* Return nonzero if the given thread is still alive. */
0d62e5e8
DJ
257static int
258linux_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
267 no longer appears to be inserted. Also adjust the PC
268 appropriately to resume where the breakpoint used to be. */
ce3a066d 269static int
0d62e5e8 270check_removed_breakpoint (struct process_info *event_child)
da6d8c04 271{
0d62e5e8
DJ
272 CORE_ADDR stop_pc;
273 struct thread_info *saved_inferior;
274
275 if (event_child->pending_is_breakpoint == 0)
276 return 0;
277
278 if (debug_threads)
279 fprintf (stderr, "Checking for breakpoint.\n");
280
281 saved_inferior = current_inferior;
282 current_inferior = get_process_thread (event_child);
283
284 stop_pc = get_stop_pc ();
285
286 /* If the PC has changed since we stopped, then we shouldn't do
287 anything. This happens if, for instance, GDB handled the
288 decr_pc_after_break subtraction itself. */
289 if (stop_pc != event_child->pending_stop_pc)
290 {
291 if (debug_threads)
292 fprintf (stderr, "Ignoring, PC was changed.\n");
293
294 event_child->pending_is_breakpoint = 0;
295 current_inferior = saved_inferior;
296 return 0;
297 }
298
299 /* If the breakpoint is still there, we will report hitting it. */
300 if ((*the_low_target.breakpoint_at) (stop_pc))
301 {
302 if (debug_threads)
303 fprintf (stderr, "Ignoring, breakpoint is still present.\n");
304 current_inferior = saved_inferior;
305 return 0;
306 }
307
308 if (debug_threads)
309 fprintf (stderr, "Removed breakpoint.\n");
310
311 /* For decr_pc_after_break targets, here is where we perform the
312 decrement. We go immediately from this function to resuming,
313 and can not safely call get_stop_pc () again. */
314 if (the_low_target.set_pc != NULL)
315 (*the_low_target.set_pc) (stop_pc);
316
317 /* We consumed the pending SIGTRAP. */
5544ad89 318 event_child->pending_is_breakpoint = 0;
0d62e5e8
DJ
319 event_child->status_pending_p = 0;
320 event_child->status_pending = 0;
321
322 current_inferior = saved_inferior;
da6d8c04
DJ
323 return 1;
324}
325
0d62e5e8
DJ
326/* Return 1 if this process has an interesting status pending. This function
327 may silently resume an inferior process. */
611cb4a5 328static int
0d62e5e8
DJ
329status_pending_p (struct inferior_list_entry *entry, void *dummy)
330{
331 struct process_info *process = (struct process_info *) entry;
332
333 if (process->status_pending_p)
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. */
342 linux_resume_one_process (&process->head, 0, 0);
343 return 0;
344 }
345
346 return process->status_pending_p;
347}
348
349static void
350linux_wait_for_process (struct process_info **childp, int *wstatp)
611cb4a5 351{
0d62e5e8
DJ
352 int ret;
353 int to_wait_for = -1;
354
355 if (*childp != NULL)
356 to_wait_for = (*childp)->lwpid;
611cb4a5
DJ
357
358 while (1)
359 {
0d62e5e8
DJ
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
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);
388
389 if (to_wait_for == -1)
390 *childp = (struct process_info *) find_inferior_id (&all_processes, ret);
391
392 (*childp)->stopped = 1;
393 (*childp)->pending_is_breakpoint = 0;
394
395 if (debug_threads
396 && WIFSTOPPED (*wstatp))
397 {
398 current_inferior = (struct thread_info *)
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}
611cb4a5 405
0d62e5e8
DJ
406static int
407linux_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
e5379b03 416 (in check_removed_breakpoint); signals should be reported anyway. */
0d62e5e8
DJ
417 if (child == NULL)
418 {
419 event_child = (struct process_info *)
420 find_inferior (&all_processes, status_pending_p, NULL);
421 if (debug_threads && event_child)
422 fprintf (stderr, "Got a pending child %d\n", event_child->lwpid);
423 }
424 else
425 {
426 event_child = get_thread_process (child);
427 if (event_child->status_pending_p
428 && check_removed_breakpoint (event_child))
429 event_child = NULL;
430 }
611cb4a5 431
0d62e5e8
DJ
432 if (event_child != NULL)
433 {
434 if (event_child->status_pending_p)
611cb4a5 435 {
0d62e5e8
DJ
436 if (debug_threads)
437 fprintf (stderr, "Got an event from pending child %d (%04x)\n",
438 event_child->lwpid, event_child->status_pending);
439 wstat = event_child->status_pending;
440 event_child->status_pending_p = 0;
441 event_child->status_pending = 0;
442 current_inferior = get_process_thread (event_child);
443 return wstat;
444 }
445 }
446
447 /* We only enter this loop if no process has a pending wait status. Thus
448 any action taken in response to a wait status inside this loop is
449 responding as soon as we detect the status, not after any pending
450 events. */
451 while (1)
452 {
453 if (child == NULL)
454 event_child = NULL;
455 else
456 event_child = get_thread_process (child);
457
458 linux_wait_for_process (&event_child, &wstat);
459
460 if (event_child == NULL)
461 error ("event from unknown child");
611cb4a5 462
0d62e5e8
DJ
463 current_inferior = (struct thread_info *)
464 find_inferior_id (&all_threads, event_child->tid);
465
466 if (using_threads)
467 {
468 /* Check for thread exit. */
469 if (! WIFSTOPPED (wstat))
611cb4a5 470 {
0d62e5e8
DJ
471 if (debug_threads)
472 fprintf (stderr, "Thread %d (LWP %d) exiting\n",
473 event_child->tid, event_child->head.id);
474
475 /* If the last thread is exiting, just return. */
476 if (all_threads.head == all_threads.tail)
477 return wstat;
478
479 dead_thread_notify (event_child->tid);
480
481 remove_inferior (&all_processes, &event_child->head);
482 free (event_child);
483 remove_thread (current_inferior);
484 current_inferior = (struct thread_info *) all_threads.head;
485
486 /* If we were waiting for this particular child to do something...
487 well, it did something. */
488 if (child != NULL)
489 return wstat;
490
491 /* Wait for a more interesting event. */
611cb4a5
DJ
492 continue;
493 }
494
0d62e5e8
DJ
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;
502 linux_resume_one_process (&event_child->head,
503 event_child->stepping, 0);
504 continue;
505 }
611cb4a5 506
0d62e5e8
DJ
507 /* FIXME drow/2002-06-09: Get signal numbers from the inferior's
508 thread library? */
509 if (WIFSTOPPED (wstat)
254787d4
DJ
510 && (WSTOPSIG (wstat) == __SIGRTMIN
511 || WSTOPSIG (wstat) == __SIGRTMIN + 1))
611cb4a5 512 {
0d62e5e8
DJ
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);
517 linux_resume_one_process (&event_child->head,
518 event_child->stepping,
519 WSTOPSIG (wstat));
520 continue;
521 }
522 }
611cb4a5 523
0d62e5e8
DJ
524 /* If this event was not handled above, and is not a SIGTRAP, report
525 it. */
526 if (!WIFSTOPPED (wstat) || WSTOPSIG (wstat) != SIGTRAP)
527 return wstat;
611cb4a5 528
0d62e5e8
DJ
529 /* If this target does not support breakpoints, we simply report the
530 SIGTRAP; it's of no concern to us. */
531 if (the_low_target.get_pc == NULL)
532 return wstat;
533
534 stop_pc = get_stop_pc ();
535
536 /* bp_reinsert will only be set if we were single-stepping.
537 Notice that we will resume the process after hitting
538 a gdbserver breakpoint; single-stepping to/over one
539 is not supported (yet). */
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. */
548 linux_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 {
557 /* We hit one of our own breakpoints. We mark it as a pending
e5379b03 558 breakpoint, so that check_removed_breakpoint () will do the PC
0d62e5e8
DJ
559 adjustment for us at the appropriate time. */
560 event_child->pending_is_breakpoint = 1;
561 event_child->pending_stop_pc = stop_pc;
562
563 /* Now we need to put the breakpoint back. We continue in the event
564 loop instead of simply replacing the breakpoint right away,
565 in order to not lose signals sent to the thread that hit the
566 breakpoint. Unfortunately this increases the window where another
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
575 use PTRACE_SINGLESTEP 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);
585 linux_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) ());
591 linux_resume_one_process (&event_child->head, 0, 0);
611cb4a5 592 }
0d62e5e8
DJ
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
aa691b87 599 clear the stepping flag; in general this does not matter,
0d62e5e8
DJ
600 because the SIGTRAP will be reported to the client, which
601 will give us a new action for this thread, but clear it for
602 consistency anyway. It's safe to clear the stepping flag
603 because the only consumer of get_stop_pc () after this point
e5379b03 604 is check_removed_breakpoint, and pending_is_breakpoint is not
0d62e5e8
DJ
605 set. It might be wiser to use a step_completed flag instead. */
606 if (event_child->stepping)
607 {
608 event_child->stepping = 0;
609 return wstat;
610 }
611
612 /* A SIGTRAP that we can't explain. It may have been a breakpoint.
613 Check if it is a breakpoint, and if so mark the process information
614 accordingly. This will handle both the necessary fiddling with the
615 PC on decr_pc_after_break targets and suppressing extra threads
616 hitting a breakpoint if two hit it at once and then GDB removes it
617 after the first is reported. Arguably it would be better to report
618 multiple threads hitting breakpoints simultaneously, but the current
619 remote protocol does not allow this. */
620 if ((*the_low_target.breakpoint_at) (stop_pc))
621 {
622 event_child->pending_is_breakpoint = 1;
623 event_child->pending_stop_pc = stop_pc;
611cb4a5
DJ
624 }
625
626 return wstat;
627 }
0d62e5e8 628
611cb4a5
DJ
629 /* NOTREACHED */
630 return 0;
631}
632
0d62e5e8 633/* Wait for process, returns status. */
da6d8c04 634
ce3a066d
DJ
635static unsigned char
636linux_wait (char *status)
da6d8c04 637{
e5f1222d 638 int w;
0d62e5e8
DJ
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 -
645 then we need to make sure we restart the other threads. We could
646 pick a thread at random or restart all; restarting all is less
647 arbitrary. */
648 if (cont_thread > 0)
649 {
650 child = (struct thread_info *) find_inferior_id (&all_threads,
651 cont_thread);
652
653 /* No stepping, no signal - unless one is pending already, of course. */
654 if (child == NULL)
64386c31
DJ
655 {
656 struct thread_resume resume_info;
657 resume_info.thread = -1;
658 resume_info.step = resume_info.sig = resume_info.leave_stopped = 0;
659 linux_resume (&resume_info);
660 }
0d62e5e8 661 }
da6d8c04
DJ
662
663 enable_async_io ();
62ea82f5 664 unblock_async_io ();
0d62e5e8
DJ
665 w = linux_wait_for_event (child);
666 stop_all_processes ();
da6d8c04 667 disable_async_io ();
da6d8c04 668
0d62e5e8
DJ
669 /* If we are waiting for a particular child, and it exited,
670 linux_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
679 LinuxThreads' behavior. */
680
681 if (all_threads.head == all_threads.tail)
da6d8c04 682 {
0d62e5e8
DJ
683 if (WIFEXITED (w))
684 {
685 fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w));
686 *status = 'W';
687 clear_inferiors ();
688 return ((unsigned char) WEXITSTATUS (w));
689 }
690 else if (!WIFSTOPPED (w))
691 {
692 fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w));
693 clear_inferiors ();
694 *status = 'X';
695 return ((unsigned char) WTERMSIG (w));
696 }
da6d8c04 697 }
0d62e5e8 698 else
da6d8c04 699 {
0d62e5e8
DJ
700 if (!WIFSTOPPED (w))
701 goto retry;
da6d8c04
DJ
702 }
703
da6d8c04
DJ
704 *status = 'T';
705 return ((unsigned char) WSTOPSIG (w));
706}
707
0d62e5e8
DJ
708static void
709send_sigstop (struct inferior_list_entry *entry)
710{
711 struct process_info *process = (struct process_info *) entry;
712
713 if (process->stopped)
714 return;
715
716 /* If we already have a pending stop signal for this process, don't
717 send another. */
718 if (process->stop_expected)
719 {
720 process->stop_expected = 0;
721 return;
722 }
723
724 if (debug_threads)
725 fprintf (stderr, "Sending sigstop to process %d\n", process->head.id);
726
727 kill (process->head.id, SIGSTOP);
728 process->sigstop_sent = 1;
729}
730
731static void
732wait_for_sigstop (struct inferior_list_entry *entry)
733{
734 struct process_info *process = (struct process_info *) entry;
735 struct thread_info *saved_inferior, *thread;
736 int wstat, saved_tid;
737
738 if (process->stopped)
739 return;
740
741 saved_inferior = current_inferior;
742 saved_tid = ((struct inferior_list_entry *) saved_inferior)->id;
743 thread = (struct thread_info *) find_inferior_id (&all_threads,
744 process->tid);
745 wstat = linux_wait_for_event (thread);
746
747 /* If we stopped with a non-SIGSTOP signal, save it for later
748 and record the pending SIGSTOP. If the process exited, just
749 return. */
750 if (WIFSTOPPED (wstat)
751 && WSTOPSIG (wstat) != SIGSTOP)
752 {
753 if (debug_threads)
754 fprintf (stderr, "Stopped with non-sigstop signal\n");
755 process->status_pending_p = 1;
756 process->status_pending = wstat;
757 process->stop_expected = 1;
758 }
759
760 if (linux_thread_alive (saved_tid))
761 current_inferior = saved_inferior;
762 else
763 {
764 if (debug_threads)
765 fprintf (stderr, "Previously current thread died.\n");
766
767 /* Set a valid thread as current. */
768 set_desired_inferior (0);
769 }
770}
771
772static void
773stop_all_processes (void)
774{
775 stopping_threads = 1;
776 for_each_inferior (&all_processes, send_sigstop);
777 for_each_inferior (&all_processes, wait_for_sigstop);
778 stopping_threads = 0;
779}
780
da6d8c04
DJ
781/* Resume execution of the inferior process.
782 If STEP is nonzero, single-step it.
783 If SIGNAL is nonzero, give it that signal. */
784
ce3a066d 785static void
0d62e5e8
DJ
786linux_resume_one_process (struct inferior_list_entry *entry,
787 int step, int signal)
da6d8c04 788{
0d62e5e8
DJ
789 struct process_info *process = (struct process_info *) entry;
790 struct thread_info *saved_inferior;
791
792 if (process->stopped == 0)
793 return;
794
795 /* If we have pending signals or status, and a new signal, enqueue the
796 signal. Also enqueue the signal if we are waiting to reinsert a
797 breakpoint; it will be picked up again below. */
798 if (signal != 0
799 && (process->status_pending_p || process->pending_signals != NULL
800 || process->bp_reinsert != 0))
801 {
802 struct pending_signals *p_sig;
803 p_sig = malloc (sizeof (*p_sig));
804 p_sig->prev = process->pending_signals;
805 p_sig->signal = signal;
806 process->pending_signals = p_sig;
807 }
808
e5379b03 809 if (process->status_pending_p && !check_removed_breakpoint (process))
0d62e5e8
DJ
810 return;
811
812 saved_inferior = current_inferior;
813 current_inferior = get_process_thread (process);
814
815 if (debug_threads)
816 fprintf (stderr, "Resuming process %d (%s, signal %d, stop %s)\n", inferior_pid,
817 step ? "step" : "continue", signal,
818 process->stop_expected ? "expected" : "not expected");
819
820 /* This bit needs some thinking about. If we get a signal that
821 we must report while a single-step reinsert is still pending,
822 we often end up resuming the thread. It might be better to
823 (ew) allow a stack of pending events; then we could be sure that
824 the reinsert happened right away and not lose any signals.
825
826 Making this stack would also shrink the window in which breakpoints are
827 uninserted (see comment in linux_wait_for_process) but not enough for
828 complete correctness, so it won't solve that problem. It may be
829 worthwhile just to solve this one, however. */
830 if (process->bp_reinsert != 0)
831 {
832 if (debug_threads)
833 fprintf (stderr, " pending reinsert at %08lx", (long)process->bp_reinsert);
834 if (step == 0)
835 fprintf (stderr, "BAD - reinserting but not stepping.\n");
836 step = 1;
837
838 /* Postpone any pending signal. It was enqueued above. */
839 signal = 0;
840 }
841
842 check_removed_breakpoint (process);
843
aa691b87 844 if (debug_threads && the_low_target.get_pc != NULL)
0d62e5e8
DJ
845 {
846 fprintf (stderr, " ");
847 (long) (*the_low_target.get_pc) ();
848 }
849
850 /* If we have pending signals, consume one unless we are trying to reinsert
851 a breakpoint. */
852 if (process->pending_signals != NULL && process->bp_reinsert == 0)
853 {
854 struct pending_signals **p_sig;
855
856 p_sig = &process->pending_signals;
857 while ((*p_sig)->prev != NULL)
858 p_sig = &(*p_sig)->prev;
859
860 signal = (*p_sig)->signal;
861 free (*p_sig);
862 *p_sig = NULL;
863 }
864
865 regcache_invalidate_one ((struct inferior_list_entry *)
866 get_process_thread (process));
da6d8c04 867 errno = 0;
0d62e5e8
DJ
868 process->stopped = 0;
869 process->stepping = step;
870 ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, process->lwpid, 0, signal);
871
872 current_inferior = saved_inferior;
da6d8c04
DJ
873 if (errno)
874 perror_with_name ("ptrace");
875}
876
64386c31
DJ
877static struct thread_resume *resume_ptr;
878
879/* This function is called once per thread. We look up the thread
5544ad89
DJ
880 in RESUME_PTR, and mark the thread with a pointer to the appropriate
881 resume request.
882
883 This algorithm is O(threads * resume elements), but resume elements
884 is small (and will remain small at least until GDB supports thread
885 suspension). */
0d62e5e8 886static void
5544ad89 887linux_set_resume_request (struct inferior_list_entry *entry)
0d62e5e8
DJ
888{
889 struct process_info *process;
64386c31 890 struct thread_info *thread;
5544ad89 891 int ndx;
64386c31
DJ
892
893 thread = (struct thread_info *) entry;
894 process = get_thread_process (thread);
895
896 ndx = 0;
897 while (resume_ptr[ndx].thread != -1 && resume_ptr[ndx].thread != entry->id)
898 ndx++;
899
5544ad89
DJ
900 process->resume = &resume_ptr[ndx];
901}
902
903/* This function is called once per thread. We check the thread's resume
904 request, which will tell us whether to resume, step, or leave the thread
905 stopped; and what signal, if any, it should be sent. For threads which
906 we aren't explicitly told otherwise, we preserve the stepping flag; this
907 is used for stepping over gdbserver-placed breakpoints. */
908
909static void
910linux_continue_one_thread (struct inferior_list_entry *entry)
911{
912 struct process_info *process;
913 struct thread_info *thread;
914 int step;
915
916 thread = (struct thread_info *) entry;
917 process = get_thread_process (thread);
918
919 if (process->resume->leave_stopped)
64386c31
DJ
920 return;
921
5544ad89
DJ
922 if (process->resume->thread == -1)
923 step = process->stepping || process->resume->step;
64386c31 924 else
5544ad89
DJ
925 step = process->resume->step;
926
927 linux_resume_one_process (&process->head, step, process->resume->sig);
c6ecbae5 928
5544ad89
DJ
929 process->resume = NULL;
930}
931
932/* This function is called once per thread. We check the thread's resume
933 request, which will tell us whether to resume, step, or leave the thread
934 stopped; and what signal, if any, it should be sent. We queue any needed
935 signals, since we won't actually resume. We already have a pending event
936 to report, so we don't need to preserve any step requests; they should
937 be re-issued if necessary. */
938
939static void
940linux_queue_one_thread (struct inferior_list_entry *entry)
941{
942 struct process_info *process;
943 struct thread_info *thread;
944
945 thread = (struct thread_info *) entry;
946 process = get_thread_process (thread);
947
948 if (process->resume->leave_stopped)
949 return;
950
951 /* If we have a new signal, enqueue the signal. */
952 if (process->resume->sig != 0)
953 {
954 struct pending_signals *p_sig;
955 p_sig = malloc (sizeof (*p_sig));
956 p_sig->prev = process->pending_signals;
957 p_sig->signal = process->resume->sig;
958 process->pending_signals = p_sig;
959 }
960
961 process->resume = NULL;
962}
963
964/* Set DUMMY if this process has an interesting status pending. */
965static int
966resume_status_pending_p (struct inferior_list_entry *entry, void *flag_p)
967{
968 struct process_info *process = (struct process_info *) entry;
969
970 /* Processes which will not be resumed are not interesting, because
971 we might not wait for them next time through linux_wait. */
972 if (process->resume->leave_stopped)
973 return 0;
974
975 /* If this thread has a removed breakpoint, we won't have any
976 events to report later, so check now. check_removed_breakpoint
977 may clear status_pending_p. We avoid calling check_removed_breakpoint
978 for any thread that we are not otherwise going to resume - this
979 lets us preserve stopped status when two threads hit a breakpoint.
980 GDB removes the breakpoint to single-step a particular thread
981 past it, then re-inserts it and resumes all threads. We want
982 to report the second thread without resuming it in the interim. */
983 if (process->status_pending_p)
984 check_removed_breakpoint (process);
985
986 if (process->status_pending_p)
987 * (int *) flag_p = 1;
988
989 return 0;
0d62e5e8
DJ
990}
991
992static void
64386c31 993linux_resume (struct thread_resume *resume_info)
0d62e5e8 994{
5544ad89 995 int pending_flag;
c6ecbae5 996
5544ad89 997 /* Yes, the use of a global here is rather ugly. */
64386c31 998 resume_ptr = resume_info;
5544ad89
DJ
999
1000 for_each_inferior (&all_threads, linux_set_resume_request);
1001
1002 /* If there is a thread which would otherwise be resumed, which
1003 has a pending status, then don't resume any threads - we can just
1004 report the pending status. Make sure to queue any signals
1005 that would otherwise be sent. */
1006 pending_flag = 0;
1007 find_inferior (&all_processes, resume_status_pending_p, &pending_flag);
1008
1009 if (debug_threads)
1010 {
1011 if (pending_flag)
1012 fprintf (stderr, "Not resuming, pending status\n");
1013 else
1014 fprintf (stderr, "Resuming, no pending status\n");
1015 }
1016
1017 if (pending_flag)
1018 for_each_inferior (&all_threads, linux_queue_one_thread);
1019 else
62ea82f5
DJ
1020 {
1021 block_async_io ();
1022 enable_async_io ();
1023 for_each_inferior (&all_threads, linux_continue_one_thread);
1024 }
0d62e5e8
DJ
1025}
1026
1027#ifdef HAVE_LINUX_USRREGS
da6d8c04
DJ
1028
1029int
0a30fbc4 1030register_addr (int regnum)
da6d8c04
DJ
1031{
1032 int addr;
1033
2ec06d2e 1034 if (regnum < 0 || regnum >= the_low_target.num_regs)
da6d8c04
DJ
1035 error ("Invalid register number %d.", regnum);
1036
2ec06d2e 1037 addr = the_low_target.regmap[regnum];
da6d8c04
DJ
1038
1039 return addr;
1040}
1041
58caa3dc 1042/* Fetch one register. */
da6d8c04
DJ
1043static void
1044fetch_register (int regno)
1045{
1046 CORE_ADDR regaddr;
1047 register int i;
0d62e5e8 1048 char *buf;
da6d8c04 1049
2ec06d2e 1050 if (regno >= the_low_target.num_regs)
0a30fbc4 1051 return;
2ec06d2e 1052 if ((*the_low_target.cannot_fetch_register) (regno))
0a30fbc4 1053 return;
da6d8c04 1054
0a30fbc4
DJ
1055 regaddr = register_addr (regno);
1056 if (regaddr == -1)
1057 return;
0d62e5e8
DJ
1058 buf = alloca (register_size (regno));
1059 for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE))
da6d8c04
DJ
1060 {
1061 errno = 0;
0d62e5e8 1062 *(PTRACE_XFER_TYPE *) (buf + i) =
da6d8c04
DJ
1063 ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0);
1064 regaddr += sizeof (PTRACE_XFER_TYPE);
1065 if (errno != 0)
1066 {
1067 /* Warning, not error, in case we are attached; sometimes the
1068 kernel doesn't let us at the registers. */
1069 char *err = strerror (errno);
1070 char *msg = alloca (strlen (err) + 128);
1071 sprintf (msg, "reading register %d: %s", regno, err);
1072 error (msg);
1073 goto error_exit;
1074 }
1075 }
0d62e5e8
DJ
1076 supply_register (regno, buf);
1077
da6d8c04
DJ
1078error_exit:;
1079}
1080
1081/* Fetch all registers, or just one, from the child process. */
58caa3dc
DJ
1082static void
1083usr_fetch_inferior_registers (int regno)
da6d8c04
DJ
1084{
1085 if (regno == -1 || regno == 0)
2ec06d2e 1086 for (regno = 0; regno < the_low_target.num_regs; regno++)
da6d8c04
DJ
1087 fetch_register (regno);
1088 else
1089 fetch_register (regno);
1090}
1091
1092/* Store our register values back into the inferior.
1093 If REGNO is -1, do this for all registers.
1094 Otherwise, REGNO specifies which register (so we can save time). */
58caa3dc
DJ
1095static void
1096usr_store_inferior_registers (int regno)
da6d8c04
DJ
1097{
1098 CORE_ADDR regaddr;
1099 int i;
0d62e5e8 1100 char *buf;
da6d8c04
DJ
1101
1102 if (regno >= 0)
1103 {
2ec06d2e 1104 if (regno >= the_low_target.num_regs)
0a30fbc4
DJ
1105 return;
1106
bc1e36ca 1107 if ((*the_low_target.cannot_store_register) (regno) == 1)
0a30fbc4
DJ
1108 return;
1109
1110 regaddr = register_addr (regno);
1111 if (regaddr == -1)
da6d8c04 1112 return;
da6d8c04 1113 errno = 0;
0d62e5e8
DJ
1114 buf = alloca (register_size (regno));
1115 collect_register (regno, buf);
1116 for (i = 0; i < register_size (regno); i += sizeof (PTRACE_XFER_TYPE))
da6d8c04 1117 {
0a30fbc4
DJ
1118 errno = 0;
1119 ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr,
2ff29de4 1120 *(PTRACE_XFER_TYPE *) (buf + i));
da6d8c04
DJ
1121 if (errno != 0)
1122 {
bc1e36ca
DJ
1123 if ((*the_low_target.cannot_store_register) (regno) == 0)
1124 {
1125 char *err = strerror (errno);
1126 char *msg = alloca (strlen (err) + 128);
1127 sprintf (msg, "writing register %d: %s",
1128 regno, err);
1129 error (msg);
1130 return;
1131 }
da6d8c04 1132 }
2ff29de4 1133 regaddr += sizeof (PTRACE_XFER_TYPE);
da6d8c04 1134 }
da6d8c04
DJ
1135 }
1136 else
2ec06d2e 1137 for (regno = 0; regno < the_low_target.num_regs; regno++)
0d62e5e8 1138 usr_store_inferior_registers (regno);
da6d8c04 1139}
58caa3dc
DJ
1140#endif /* HAVE_LINUX_USRREGS */
1141
1142
1143
1144#ifdef HAVE_LINUX_REGSETS
1145
1146static int
0d62e5e8 1147regsets_fetch_inferior_registers ()
58caa3dc
DJ
1148{
1149 struct regset_info *regset;
1150
1151 regset = target_regsets;
1152
1153 while (regset->size >= 0)
1154 {
1155 void *buf;
1156 int res;
1157
1158 if (regset->size == 0)
1159 {
1160 regset ++;
1161 continue;
1162 }
1163
1164 buf = malloc (regset->size);
d06f167a 1165 res = ptrace (regset->get_request, inferior_pid, 0, buf);
58caa3dc
DJ
1166 if (res < 0)
1167 {
1168 if (errno == EIO)
1169 {
1170 /* If we get EIO on the first regset, do not try regsets again.
1171 If we get EIO on a later regset, disable that regset. */
1172 if (regset == target_regsets)
1173 {
1174 use_regsets_p = 0;
1175 return -1;
1176 }
1177 else
1178 {
1179 regset->size = 0;
1180 continue;
1181 }
1182 }
1183 else
1184 {
0d62e5e8
DJ
1185 char s[256];
1186 sprintf (s, "ptrace(regsets_fetch_inferior_registers) PID=%d",
1187 inferior_pid);
1188 perror (s);
58caa3dc
DJ
1189 }
1190 }
1191 regset->store_function (buf);
1192 regset ++;
1193 }
ce3a066d 1194 return 0;
58caa3dc
DJ
1195}
1196
1197static int
0d62e5e8 1198regsets_store_inferior_registers ()
58caa3dc
DJ
1199{
1200 struct regset_info *regset;
1201
1202 regset = target_regsets;
1203
1204 while (regset->size >= 0)
1205 {
1206 void *buf;
1207 int res;
1208
1209 if (regset->size == 0)
1210 {
1211 regset ++;
1212 continue;
1213 }
1214
1215 buf = malloc (regset->size);
1216 regset->fill_function (buf);
d06f167a 1217 res = ptrace (regset->set_request, inferior_pid, 0, buf);
58caa3dc
DJ
1218 if (res < 0)
1219 {
1220 if (errno == EIO)
1221 {
1222 /* If we get EIO on the first regset, do not try regsets again.
1223 If we get EIO on a later regset, disable that regset. */
1224 if (regset == target_regsets)
1225 {
1226 use_regsets_p = 0;
1227 return -1;
1228 }
1229 else
1230 {
1231 regset->size = 0;
1232 continue;
1233 }
1234 }
1235 else
1236 {
ce3a066d 1237 perror ("Warning: ptrace(regsets_store_inferior_registers)");
58caa3dc
DJ
1238 }
1239 }
1240 regset ++;
09ec9b38 1241 free (buf);
58caa3dc 1242 }
ce3a066d 1243 return 0;
58caa3dc
DJ
1244}
1245
1246#endif /* HAVE_LINUX_REGSETS */
1247
1248
1249void
ce3a066d 1250linux_fetch_registers (int regno)
58caa3dc
DJ
1251{
1252#ifdef HAVE_LINUX_REGSETS
1253 if (use_regsets_p)
1254 {
1255 if (regsets_fetch_inferior_registers () == 0)
1256 return;
1257 }
1258#endif
1259#ifdef HAVE_LINUX_USRREGS
1260 usr_fetch_inferior_registers (regno);
1261#endif
1262}
1263
1264void
ce3a066d 1265linux_store_registers (int regno)
58caa3dc
DJ
1266{
1267#ifdef HAVE_LINUX_REGSETS
1268 if (use_regsets_p)
1269 {
1270 if (regsets_store_inferior_registers () == 0)
1271 return;
1272 }
1273#endif
1274#ifdef HAVE_LINUX_USRREGS
1275 usr_store_inferior_registers (regno);
1276#endif
1277}
1278
da6d8c04 1279
da6d8c04
DJ
1280/* Copy LEN bytes from inferior's memory starting at MEMADDR
1281 to debugger memory starting at MYADDR. */
1282
c3e735a6 1283static int
ce3a066d 1284linux_read_memory (CORE_ADDR memaddr, char *myaddr, int len)
da6d8c04
DJ
1285{
1286 register int i;
1287 /* Round starting address down to longword boundary. */
1288 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1289 /* Round ending address up; get number of longwords that makes. */
aa691b87
RM
1290 register int count
1291 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1)
da6d8c04
DJ
1292 / sizeof (PTRACE_XFER_TYPE);
1293 /* Allocate buffer of that many longwords. */
aa691b87 1294 register PTRACE_XFER_TYPE *buffer
da6d8c04
DJ
1295 = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1296
1297 /* Read all the longwords */
1298 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1299 {
c3e735a6 1300 errno = 0;
d844cde6 1301 buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0);
c3e735a6
DJ
1302 if (errno)
1303 return errno;
da6d8c04
DJ
1304 }
1305
1306 /* Copy appropriate bytes out of the buffer. */
1307 memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len);
c3e735a6
DJ
1308
1309 return 0;
da6d8c04
DJ
1310}
1311
1312/* Copy LEN bytes of data from debugger memory at MYADDR
1313 to inferior's memory at MEMADDR.
1314 On failure (cannot write the inferior)
1315 returns the value of errno. */
1316
ce3a066d 1317static int
611cb4a5 1318linux_write_memory (CORE_ADDR memaddr, const char *myaddr, int len)
da6d8c04
DJ
1319{
1320 register int i;
1321 /* Round starting address down to longword boundary. */
1322 register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE);
1323 /* Round ending address up; get number of longwords that makes. */
1324 register int count
1325 = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE);
1326 /* Allocate buffer of that many longwords. */
1327 register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE));
1328 extern int errno;
1329
0d62e5e8
DJ
1330 if (debug_threads)
1331 {
1332 fprintf (stderr, "Writing %02x to %08lx\n", (unsigned)myaddr[0], (long)memaddr);
1333 }
1334
da6d8c04
DJ
1335 /* Fill start and end extra bytes of buffer with existing memory data. */
1336
d844cde6
DJ
1337 buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid,
1338 (PTRACE_ARG3_TYPE) addr, 0);
da6d8c04
DJ
1339
1340 if (count > 1)
1341 {
1342 buffer[count - 1]
1343 = ptrace (PTRACE_PEEKTEXT, inferior_pid,
d844cde6
DJ
1344 (PTRACE_ARG3_TYPE) (addr + (count - 1)
1345 * sizeof (PTRACE_XFER_TYPE)),
1346 0);
da6d8c04
DJ
1347 }
1348
1349 /* Copy data to be written over corresponding part of buffer */
1350
1351 memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len);
1352
1353 /* Write the entire buffer. */
1354
1355 for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE))
1356 {
1357 errno = 0;
d844cde6 1358 ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]);
da6d8c04
DJ
1359 if (errno)
1360 return errno;
1361 }
1362
1363 return 0;
1364}
2f2893d9
DJ
1365
1366static void
1367linux_look_up_symbols (void)
1368{
0d62e5e8
DJ
1369#ifdef USE_THREAD_DB
1370 if (using_threads)
1371 return;
1372
1373 using_threads = thread_db_init ();
1374#endif
1375}
1376
e5379b03
DJ
1377static void
1378linux_send_signal (int signum)
1379{
1380 extern int signal_pid;
1381
1382 if (cont_thread > 0)
1383 {
1384 struct process_info *process;
1385
1386 process = get_thread_process (current_inferior);
1387 kill (process->lwpid, signum);
1388 }
1389 else
1390 kill (signal_pid, signum);
1391}
1392
aa691b87
RM
1393/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
1394 to debugger memory starting at MYADDR. */
1395
1396static int
1397linux_read_auxv (CORE_ADDR offset, char *myaddr, unsigned int len)
1398{
1399 char filename[PATH_MAX];
1400 int fd, n;
1401
1402 snprintf (filename, sizeof filename, "/proc/%d/auxv", inferior_pid);
1403
1404 fd = open (filename, O_RDONLY);
1405 if (fd < 0)
1406 return -1;
1407
1408 if (offset != (CORE_ADDR) 0
1409 && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset)
1410 n = -1;
1411 else
1412 n = read (fd, myaddr, len);
1413
1414 close (fd);
1415
1416 return n;
1417}
1418
da6d8c04 1419\f
ce3a066d
DJ
1420static struct target_ops linux_target_ops = {
1421 linux_create_inferior,
1422 linux_attach,
1423 linux_kill,
6ad8ae5c 1424 linux_detach,
ce3a066d
DJ
1425 linux_thread_alive,
1426 linux_resume,
1427 linux_wait,
1428 linux_fetch_registers,
1429 linux_store_registers,
1430 linux_read_memory,
1431 linux_write_memory,
2f2893d9 1432 linux_look_up_symbols,
e5379b03 1433 linux_send_signal,
aa691b87 1434 linux_read_auxv,
ce3a066d
DJ
1435};
1436
0d62e5e8
DJ
1437static void
1438linux_init_signals ()
1439{
1440 /* FIXME drow/2002-06-09: As above, we should check with LinuxThreads
1441 to find what the cancel signal actually is. */
254787d4 1442 signal (__SIGRTMIN+1, SIG_IGN);
0d62e5e8
DJ
1443}
1444
da6d8c04
DJ
1445void
1446initialize_low (void)
1447{
0d62e5e8 1448 using_threads = 0;
ce3a066d 1449 set_target_ops (&linux_target_ops);
611cb4a5
DJ
1450 set_breakpoint_data (the_low_target.breakpoint,
1451 the_low_target.breakpoint_len);
0a30fbc4 1452 init_registers ();
0d62e5e8 1453 linux_init_signals ();
da6d8c04 1454}
This page took 0.280201 seconds and 4 git commands to generate.