* defs.h, utils.c: xrealloc takes PTR as first arg.
[deliverable/binutils-gdb.git] / gdb / procfs.c
CommitLineData
35f5886e
FF
1/* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright (C) 1991 Free Software Foundation, Inc.
3 Written by Fred Fish at Cygnus Support.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
22/* N O T E S
23
24For information on the details of using /proc consult section proc(4)
25in the UNIX System V Release 4 System Administrator's Reference Manual.
26
27The general register and floating point register sets are manipulated by
28separate ioctl's. This file makes the assumption that if FP0_REGNUM is
29defined, then support for the floating point register set is desired,
30regardless of whether or not the actual target has floating point hardware.
31
32 */
33
34
5129100c 35#include "defs.h"
35f5886e
FF
36
37#ifdef USE_PROC_FS /* Entire file goes away if not using /proc */
38
35f5886e
FF
39#include <sys/procfs.h>
40#include <fcntl.h>
41#include <errno.h>
42
35f5886e
FF
43#include "inferior.h"
44#include "target.h"
45
46#ifndef PROC_NAME_FMT
47#define PROC_NAME_FMT "/proc/%d"
48#endif
49
35f5886e
FF
50#if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
51CORE_ADDR kernel_u_addr;
52#endif
53
54/* All access to the inferior, either one started by gdb or one that has
55 been attached to, is controlled by an instance of a procinfo structure,
56 defined below. Since gdb currently only handles one inferior at a time,
a39ad5ce
FF
57 the procinfo structure for the inferior is statically allocated and
58 only one exists at any given time. There is a separate procinfo
59 structure for use by the "info proc" command, so that we can print
60 useful information about any random process without interfering with
61 the inferior's procinfo information. */
35f5886e
FF
62
63struct procinfo {
64 int valid; /* Nonzero if pid, fd, & pathname are valid */
65 int pid; /* Process ID of inferior */
66 int fd; /* File descriptor for /proc entry */
67 char *pathname; /* Pathname to /proc entry */
68 int was_stopped; /* Nonzero if was stopped prior to attach */
69 prrun_t prrun; /* Control state when it is run */
70 prstatus_t prstatus; /* Current process status info */
71 gregset_t gregset; /* General register set */
72 fpregset_t fpregset; /* Floating point register set */
73 fltset_t fltset; /* Current traced hardware fault set */
74 sigset_t trace; /* Current traced signal set */
75 sysset_t exitset; /* Current traced system call exit set */
76 sysset_t entryset; /* Current traced system call entry set */
a39ad5ce
FF
77};
78
79static struct procinfo pi; /* Inferior's process information */
35f5886e 80
1ab3bf1b
JG
81/* Prototypes for local functions */
82
83static int
84proc_address_to_fd PARAMS ((CORE_ADDR, int));
85
86static int
87open_proc_file PARAMS ((int, struct procinfo *));
88
89static void
90close_proc_file PARAMS ((struct procinfo *));
91
92static void
93unconditionally_kill_inferior PARAMS ((void));
94
95static void
96proc_init_failed PARAMS ((char *));
97
98static void
99proc_info PARAMS ((char *, int));
100
101static void
102proc_info_address_map PARAMS ((struct procinfo *, int));
103
104static char *
105mappingflags PARAMS ((long));
106
107/* External function prototypes that can't be easily included in any
108 header file because the args are typedefs in system include files. */
109
110extern void
111supply_gregset PARAMS ((gregset_t *));
112
113extern void
114fill_gregset PARAMS ((gregset_t *, int));
115
116extern void
117supply_fpregset PARAMS ((fpregset_t *));
118
119extern void
120fill_fpregset PARAMS ((fpregset_t *, int));
35f5886e 121
35f5886e
FF
122
123/*
124
125GLOBAL FUNCTION
126
127 ptrace -- override library version to force errors for /proc version
128
129SYNOPSIS
130
131 int ptrace (int request, int pid, int arg3, int arg4)
132
133DESCRIPTION
134
135 When gdb is configured to use /proc, it should not be calling
136 or otherwise attempting to use ptrace. In order to catch errors
137 where use of /proc is configured, but some routine is still calling
138 ptrace, we provide a local version of a function with that name
139 that does nothing but issue an error message.
140*/
141
142int
1ab3bf1b
JG
143ptrace (request, pid, arg3, arg4)
144 int request;
145 int pid;
146 int arg3;
147 int arg4;
35f5886e
FF
148{
149 error ("internal error - there is a call to ptrace() somewhere");
150 /*NOTREACHED*/
151}
152
153/*
154
155GLOBAL FUNCTION
156
157 kill_inferior_fast -- kill inferior while gdb is exiting
158
159SYNOPSIS
160
161 void kill_inferior_fast (void)
162
163DESCRIPTION
164
165 This is used when GDB is exiting. It gives less chance of error.
166
167NOTES
168
169 Don't attempt to kill attached inferiors since we may be called
170 when gdb is in the process of aborting, and killing the attached
171 inferior may be very anti-social. This is particularly true if we
172 were attached just so we could use the /proc facilities to get
173 detailed information about it's status.
174
175*/
176
177void
1ab3bf1b 178kill_inferior_fast ()
35f5886e
FF
179{
180 if (inferior_pid != 0 && !attach_flag)
181 {
182 unconditionally_kill_inferior ();
183 }
184}
185
186/*
187
188GLOBAL FUNCTION
189
190 kill_inferior - kill any currently inferior
191
192SYNOPSIS
193
194 void kill_inferior (void)
195
196DESCRIPTION
197
198 Kill any current inferior.
199
200NOTES
201
202 Kills even attached inferiors. Presumably the user has already
203 been prompted that the inferior is an attached one rather than
204 one started by gdb. (FIXME?)
205
206*/
207
208void
1ab3bf1b 209kill_inferior ()
35f5886e
FF
210{
211 if (inferior_pid != 0)
212 {
213 unconditionally_kill_inferior ();
214 target_mourn_inferior ();
215 }
216}
217
218/*
219
220LOCAL FUNCTION
221
222 unconditionally_kill_inferior - terminate the inferior
223
224SYNOPSIS
225
226 static void unconditionally_kill_inferior (void)
227
228DESCRIPTION
229
230 Kill the current inferior. Should not be called until it
231 is at least tested that there is an inferior.
232
233NOTE
234
235 A possibly useful enhancement would be to first try sending
236 the inferior a terminate signal, politely asking it to commit
237 suicide, before we murder it.
238
239*/
240
241static void
1ab3bf1b 242unconditionally_kill_inferior ()
35f5886e
FF
243{
244 int signo;
245
246 signo = SIGKILL;
247 (void) ioctl (pi.fd, PIOCKILL, &signo);
a39ad5ce 248 close_proc_file (&pi);
35f5886e
FF
249 wait ((int *) 0);
250}
251
252/*
253
254GLOBAL FUNCTION
255
256 child_xfer_memory -- copy data to or from inferior memory space
257
258SYNOPSIS
259
260 int child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
261 int dowrite, struct target_ops target)
262
263DESCRIPTION
264
265 Copy LEN bytes to/from inferior's memory starting at MEMADDR
266 from/to debugger memory starting at MYADDR. Copy from inferior
267 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
268
269 Returns the length copied, which is either the LEN argument or
270 zero. This xfer function does not do partial moves, since child_ops
271 doesn't allow memory operations to cross below us in the target stack
272 anyway.
273
274NOTES
275
276 The /proc interface makes this an almost trivial task.
277 */
278
279
280int
1ab3bf1b
JG
281child_xfer_memory (memaddr, myaddr, len, dowrite, target)
282 CORE_ADDR memaddr;
283 char *myaddr;
284 int len;
285 int dowrite;
286 struct target_ops *target; /* ignored */
35f5886e
FF
287{
288 int nbytes = 0;
289
290 if (lseek (pi.fd, (off_t) memaddr, 0) == (off_t) memaddr)
291 {
292 if (dowrite)
293 {
294 nbytes = write (pi.fd, myaddr, len);
295 }
296 else
297 {
298 nbytes = read (pi.fd, myaddr, len);
299 }
300 if (nbytes < 0)
301 {
302 nbytes = 0;
303 }
304 }
305 return (nbytes);
306}
307
308/*
309
310GLOBAL FUNCTION
311
312 store_inferior_registers -- copy register values back to inferior
313
314SYNOPSIS
315
316 void store_inferior_registers (int regno)
317
318DESCRIPTION
319
320 Store our current register values back into the inferior. If
321 REGNO is -1 then store all the register, otherwise store just
322 the value specified by REGNO.
323
324NOTES
325
326 If we are storing only a single register, we first have to get all
327 the current values from the process, overwrite the desired register
328 in the gregset with the one we want from gdb's registers, and then
329 send the whole set back to the process. For writing all the
330 registers, all we have to do is generate the gregset and send it to
331 the process.
332
333 Also note that the process has to be stopped on an event of interest
334 for this to work, which basically means that it has to have been
335 run under the control of one of the other /proc ioctl calls and not
336 ptrace. Since we don't use ptrace anyway, we don't worry about this
337 fine point, but it is worth noting for future reference.
338
339 Gdb is confused about what this function is supposed to return.
340 Some versions return a value, others return nothing. Some are
341 declared to return a value and actually return nothing. Gdb ignores
342 anything returned. (FIXME)
343
344 */
345
346void
1ab3bf1b
JG
347store_inferior_registers (regno)
348 int regno;
35f5886e
FF
349{
350 if (regno != -1)
351 {
352 (void) ioctl (pi.fd, PIOCGREG, &pi.gregset);
353 }
354 fill_gregset (&pi.gregset, regno);
355 (void) ioctl (pi.fd, PIOCSREG, &pi.gregset);
356
357#if defined (FP0_REGNUM)
358
359 /* Now repeat everything using the floating point register set, if the
360 target has floating point hardware. Since we ignore the returned value,
361 we'll never know whether it worked or not anyway. */
362
363 if (regno != -1)
364 {
365 (void) ioctl (pi.fd, PIOCGFPREG, &pi.fpregset);
366 }
367 fill_fpregset (&pi.fpregset, regno);
368 (void) ioctl (pi.fd, PIOCSFPREG, &pi.fpregset);
369
370#endif /* FP0_REGNUM */
371
372}
373
374/*
375
376GLOBAL FUNCTION
377
378 inferior_proc_init - initialize access to a /proc entry
379
380SYNOPSIS
381
382 void inferior_proc_init (int pid)
383
384DESCRIPTION
385
386 When gdb starts an inferior, this function is called in the parent
387 process immediately after the fork. It waits for the child to stop
388 on the return from the exec system call (the child itself takes care
389 of ensuring that this is set up), then sets up the set of signals
390 and faults that are to be traced.
391
392NOTES
393
394 If proc_init_failed ever gets called, control returns to the command
395 processing loop via the standard error handling code.
396 */
397
398void
1ab3bf1b
JG
399inferior_proc_init (pid)
400 int pid;
35f5886e 401{
a39ad5ce 402 if (!open_proc_file (pid, &pi))
35f5886e
FF
403 {
404 proc_init_failed ("can't open process file");
405 }
406 else
407 {
408 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
409 prfillset (&pi.prrun.pr_trace);
410 prfillset (&pi.prrun.pr_fault);
411 prdelset (&pi.prrun.pr_fault, FLTPAGE);
412 if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
413 {
414 proc_init_failed ("PIOCWSTOP failed");
415 }
416 else if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace) < 0)
417 {
418 proc_init_failed ("PIOCSTRACE failed");
419 }
420 else if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault) < 0)
421 {
422 proc_init_failed ("PIOCSFAULT failed");
423 }
424 }
425}
426
427/*
428
429GLOBAL FUNCTION
430
431 proc_set_exec_trap -- arrange for exec'd child to halt at startup
432
433SYNOPSIS
434
435 void proc_set_exec_trap (void)
436
437DESCRIPTION
438
439 This function is called in the child process when starting up
440 an inferior, prior to doing the exec of the actual inferior.
441 It sets the child process's exitset to make exit from the exec
442 system call an event of interest to stop on, and then simply
443 returns. The child does the exec, the system call returns, and
444 the child stops at the first instruction, ready for the gdb
445 parent process to take control of it.
446
447NOTE
448
449 We need to use all local variables since the child may be sharing
450 it's data space with the parent, if vfork was used rather than
451 fork.
452 */
453
454void
1ab3bf1b 455proc_set_exec_trap ()
35f5886e
FF
456{
457 sysset_t exitset;
458 auto char procname[32];
459 int fd;
460
461 (void) sprintf (procname, PROC_NAME_FMT, getpid ());
462 if ((fd = open (procname, O_RDWR)) < 0)
463 {
464 perror (procname);
465 fflush (stderr);
466 _exit (127);
467 }
468 premptyset (&exitset);
469 praddset (&exitset, SYS_exec);
470 praddset (&exitset, SYS_execve);
471 if (ioctl (fd, PIOCSEXIT, &exitset) < 0)
472 {
473 perror (procname);
474 fflush (stderr);
475 _exit (127);
476 }
477}
478
f8b76e70
FF
479/*
480
a39ad5ce
FF
481GLOBAL FUNCTION
482
483 proc_iterate_over_mappings -- call function for every mapped space
484
485SYNOPSIS
486
487 int proc_iterate_over_mappings (int (*func)())
488
489DESCRIPTION
490
491 Given a pointer to a function, call that function for every
492 mapped address space, passing it an open file descriptor for
493 the file corresponding to that mapped address space (if any)
494 and the base address of the mapped space. Quit when we hit
495 the end of the mappings or the function returns nonzero.
496 */
497
498int
1ab3bf1b
JG
499proc_iterate_over_mappings (func)
500 int (*func) PARAMS ((int, CORE_ADDR));
a39ad5ce
FF
501{
502 int nmap;
503 int fd;
504 int funcstat = 0;
505 struct prmap *prmaps;
506 struct prmap *prmap;
507 CORE_ADDR baseaddr = 0;
508
509 if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
510 {
1ab3bf1b 511 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
a39ad5ce
FF
512 if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
513 {
514 for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap)
515 {
1ab3bf1b
JG
516 fd = proc_address_to_fd ((CORE_ADDR) prmap -> pr_vaddr, 0);
517 funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr);
a39ad5ce
FF
518 close (fd);
519 }
520 }
521 }
522 return (funcstat);
523}
524
525/*
526
f8b76e70
FF
527GLOBAL FUNCTION
528
529 proc_base_address -- find base address for segment containing address
530
531SYNOPSIS
532
533 CORE_ADDR proc_base_address (CORE_ADDR addr)
534
535DESCRIPTION
536
537 Given an address of a location in the inferior, find and return
538 the base address of the mapped segment containing that address.
539
540 This is used for example, by the shared library support code,
541 where we have the pc value for some location in the shared library
542 where we are stopped, and need to know the base address of the
543 segment containing that address.
544*/
545
546
1ab3bf1b
JG
547#if 0 /* Currently unused */
548
f8b76e70 549CORE_ADDR
1ab3bf1b
JG
550proc_base_address (addr)
551CORE_ADDR addr;
f8b76e70
FF
552{
553 int nmap;
554 struct prmap *prmaps;
555 struct prmap *prmap;
556 CORE_ADDR baseaddr = 0;
557
a39ad5ce 558 if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
f8b76e70 559 {
1ab3bf1b 560 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
f8b76e70
FF
561 if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
562 {
563 for (prmap = prmaps; prmap -> pr_size; ++prmap)
564 {
565 if ((prmap -> pr_vaddr <= (caddr_t) addr) &&
566 (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr))
567 {
568 baseaddr = (CORE_ADDR) prmap -> pr_vaddr;
569 break;
570 }
571 }
572 }
573 }
574 return (baseaddr);
575}
576
1ab3bf1b
JG
577#endif /* 0 */
578
f8b76e70
FF
579/*
580
581GLOBAL_FUNCTION
582
583 proc_address_to_fd -- return open fd for file mapped to address
584
585SYNOPSIS
586
a39ad5ce 587 int proc_address_to_fd (CORE_ADDR addr, complain)
f8b76e70
FF
588
589DESCRIPTION
590
591 Given an address in the current inferior's address space, use the
592 /proc interface to find an open file descriptor for the file that
593 this address was mapped in from. Return -1 if there is no current
594 inferior. Print a warning message if there is an inferior but
595 the address corresponds to no file (IE a bogus address).
596
597*/
598
1ab3bf1b
JG
599static int
600proc_address_to_fd (addr, complain)
601 CORE_ADDR addr;
602 int complain;
f8b76e70
FF
603{
604 int fd = -1;
605
606 if (pi.valid)
607 {
608 if ((fd = ioctl (pi.fd, PIOCOPENM, (caddr_t *) &addr)) < 0)
609 {
a39ad5ce
FF
610 if (complain)
611 {
612 print_sys_errmsg (pi.pathname, errno);
613 warning ("can't find mapped file for address 0x%x", addr);
614 }
f8b76e70
FF
615 }
616 }
617 return (fd);
618}
619
35f5886e
FF
620
621#ifdef ATTACH_DETACH
622
623/*
624
625GLOBAL FUNCTION
626
627 attach -- attach to an already existing process
628
629SYNOPSIS
630
631 int attach (int pid)
632
633DESCRIPTION
634
635 Attach to an already existing process with the specified process
636 id. If the process is not already stopped, query whether to
637 stop it or not.
638
639NOTES
640
641 The option of stopping at attach time is specific to the /proc
642 versions of gdb. Versions using ptrace force the attachee
643 to stop.
644
645*/
646
647int
1ab3bf1b
JG
648attach (pid)
649 int pid;
35f5886e 650{
a39ad5ce 651 if (!open_proc_file (pid, &pi))
35f5886e
FF
652 {
653 perror_with_name (pi.pathname);
654 /* NOTREACHED */
655 }
656
657 /* Get current status of process and if it is not already stopped,
658 then stop it. Remember whether or not it was stopped when we first
659 examined it. */
660
661 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
662 {
663 print_sys_errmsg (pi.pathname, errno);
a39ad5ce 664 close_proc_file (&pi);
35f5886e
FF
665 error ("PIOCSTATUS failed");
666 }
667 if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
668 {
669 pi.was_stopped = 1;
670 }
671 else
672 {
673 pi.was_stopped = 0;
674 if (query ("Process is currently running, stop it? "))
675 {
676 if (ioctl (pi.fd, PIOCSTOP, &pi.prstatus) < 0)
677 {
678 print_sys_errmsg (pi.pathname, errno);
a39ad5ce 679 close_proc_file (&pi);
35f5886e
FF
680 error ("PIOCSTOP failed");
681 }
682 }
683 }
684
685 /* Remember some things about the inferior that we will, or might, change
686 so that we can restore them when we detach. */
687
688 (void) ioctl (pi.fd, PIOCGTRACE, &pi.trace);
689 (void) ioctl (pi.fd, PIOCGFAULT, &pi.fltset);
690 (void) ioctl (pi.fd, PIOCGENTRY, &pi.entryset);
691 (void) ioctl (pi.fd, PIOCGEXIT, &pi.exitset);
692
693 /* Set up trace and fault sets, as gdb expects them. */
694
695 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
696 prfillset (&pi.prrun.pr_trace);
697 prfillset (&pi.prrun.pr_fault);
698 prdelset (&pi.prrun.pr_fault, FLTPAGE);
699 if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault))
700 {
f66f459f 701 print_sys_errmsg ("PIOCSFAULT failed", errno);
35f5886e
FF
702 }
703 if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace))
704 {
f66f459f 705 print_sys_errmsg ("PIOCSTRACE failed", errno);
35f5886e
FF
706 }
707 attach_flag = 1;
708 return (pid);
709}
710
711/*
712
713GLOBAL FUNCTION
714
715 detach -- detach from an attached-to process
716
717SYNOPSIS
718
719 void detach (int signal)
720
721DESCRIPTION
722
723 Detach from the current attachee.
724
725 If signal is non-zero, the attachee is started running again and sent
726 the specified signal.
727
728 If signal is zero and the attachee was not already stopped when we
729 attached to it, then we make it runnable again when we detach.
730
731 Otherwise, we query whether or not to make the attachee runnable
732 again, since we may simply want to leave it in the state it was in
733 when we attached.
734
735 We report any problems, but do not consider them errors, since we
736 MUST detach even if some things don't seem to go right. This may not
737 be the ideal situation. (FIXME).
738 */
739
740void
1ab3bf1b
JG
741detach (signal)
742 int signal;
35f5886e
FF
743{
744 if (signal)
745 {
746 struct siginfo siginfo;
747 siginfo.si_signo = signal;
748 siginfo.si_code = 0;
749 siginfo.si_errno = 0;
750 if (ioctl (pi.fd, PIOCSSIG, &siginfo) < 0)
751 {
752 print_sys_errmsg (pi.pathname, errno);
753 printf ("PIOCSSIG failed.\n");
754 }
755 }
756 if (ioctl (pi.fd, PIOCSEXIT, &pi.exitset) < 0)
757 {
758 print_sys_errmsg (pi.pathname, errno);
759 printf ("PIOCSEXIT failed.\n");
760 }
761 if (ioctl (pi.fd, PIOCSENTRY, &pi.entryset) < 0)
762 {
763 print_sys_errmsg (pi.pathname, errno);
764 printf ("PIOCSENTRY failed.\n");
765 }
766 if (ioctl (pi.fd, PIOCSTRACE, &pi.trace) < 0)
767 {
768 print_sys_errmsg (pi.pathname, errno);
769 printf ("PIOCSTRACE failed.\n");
770 }
771 if (ioctl (pi.fd, PIOCSFAULT, &pi.fltset) < 0)
772 {
773 print_sys_errmsg (pi.pathname, errno);
774 printf ("PIOCSFAULT failed.\n");
775 }
776 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
777 {
778 print_sys_errmsg (pi.pathname, errno);
779 printf ("PIOCSTATUS failed.\n");
780 }
781 else
782 {
783 if (signal || (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
784 {
785 if (signal || !pi.was_stopped ||
786 query ("Was stopped when attached, make it runnable again? "))
787 {
788 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
789 pi.prrun.pr_flags = PRCFAULT;
790 if (ioctl (pi.fd, PIOCRUN, &pi.prrun))
791 {
792 print_sys_errmsg (pi.pathname, errno);
793 printf ("PIOCRUN failed.\n");
794 }
795 }
796 }
797 }
a39ad5ce 798 close_proc_file (&pi);
35f5886e
FF
799 attach_flag = 0;
800}
801
fb182850
FF
802#endif /* ATTACH_DETACH */
803
35f5886e
FF
804/*
805
806GLOBAL FUNCTION
807
808 proc_wait -- emulate wait() as much as possible
809
810SYNOPSIS
811
812 int proc_wait (int *statloc)
813
814DESCRIPTION
815
816 Try to emulate wait() as much as possible. Not sure why we can't
817 just use wait(), but it seems to have problems when applied to a
818 process being controlled with the /proc interface.
819
820NOTES
821
822 We have a race problem here with no obvious solution. We need to let
823 the inferior run until it stops on an event of interest, which means
824 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
825 ioctl if the process is already stopped on something that is not an
826 event of interest, or the call will hang indefinitely. Thus we first
827 use PIOCSTATUS to see if the process is not stopped. If not, then we
828 use PIOCWSTOP. But during the window between the two, if the process
829 stops for any reason that is not an event of interest (such as a job
830 control signal) then gdb will hang. One possible workaround is to set
831 an alarm to wake up every minute of so and check to see if the process
832 is still running, and if so, then reissue the PIOCWSTOP. But this is
833 a real kludge, so has not been implemented. FIXME: investigate
834 alternatives.
835
836 FIXME: Investigate why wait() seems to have problems with programs
837 being control by /proc routines.
838
839 */
840
841int
1ab3bf1b
JG
842proc_wait (statloc)
843 int *statloc;
35f5886e
FF
844{
845 short what;
846 short why;
847 int statval = 0;
848 int checkerr = 0;
849 int rtnval = -1;
850
851 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
852 {
853 checkerr++;
854 }
855 else if (!(pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
856 {
857 if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
858 {
859 checkerr++;
860 }
861 }
862 if (checkerr)
863 {
864 if (errno == ENOENT)
865 {
866 rtnval = wait (&statval);
867 if (rtnval != inferior_pid)
868 {
869 error ("PIOCWSTOP, wait failed, returned %d", rtnval);
870 /* NOTREACHED */
871 }
872 }
873 else
874 {
875 print_sys_errmsg (pi.pathname, errno);
876 error ("PIOCSTATUS or PIOCWSTOP failed.");
877 /* NOTREACHED */
878 }
879 }
880 else if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
881 {
882 rtnval = pi.prstatus.pr_pid;
883 why = pi.prstatus.pr_why;
884 what = pi.prstatus.pr_what;
885 if (why == PR_SIGNALLED)
886 {
887 statval = (what << 8) | 0177;
888 }
889 else if ((why == PR_SYSEXIT) &&
890 (what == SYS_exec || what == SYS_execve))
891 {
892 statval = (SIGTRAP << 8) | 0177;
893 }
894 else if (why == PR_REQUESTED)
895 {
896 statval = (SIGSTOP << 8) | 0177;
897 }
898 else if (why == PR_JOBCONTROL)
899 {
900 statval = (what << 8) | 0177;
901 }
902 else if (why == PR_FAULTED)
903 {
904 switch (what)
905 {
906 case FLTPRIV:
907 case FLTILL:
908 statval = (SIGILL << 8) | 0177;
909 break;
910 case FLTBPT:
911 case FLTTRACE:
912 statval = (SIGTRAP << 8) | 0177;
913 break;
914 case FLTSTACK:
915 case FLTACCESS:
916 case FLTBOUNDS:
917 statval = (SIGSEGV << 8) | 0177;
918 break;
919 case FLTIOVF:
920 case FLTIZDIV:
921 case FLTFPE:
922 statval = (SIGFPE << 8) | 0177;
923 break;
924 case FLTPAGE: /* Recoverable page fault */
925 default:
926 rtnval = -1;
927 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
928 /* NOTREACHED */
929 }
930 }
931 else
932 {
933 rtnval = -1;
934 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
935 /* NOTREACHED */
936 }
937 }
938 else
939 {
940 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
941 pi.prstatus.pr_flags);
942 /* NOTREACHED */
943 }
944 if (statloc)
945 {
946 *statloc = statval;
947 }
948 return (rtnval);
949}
950
951/*
952
953GLOBAL FUNCTION
954
955 child_resume -- resume execution of the inferior process
956
957SYNOPSIS
958
959 void child_resume (int step, int signal)
960
961DESCRIPTION
962
963 Resume execution of the inferior process. If STEP is nozero, then
964 just single step it. If SIGNAL is nonzero, restart it with that
965 signal activated.
966
967NOTE
968
969 It may not be absolutely necessary to specify the PC value for
970 restarting, but to be safe we use the value that gdb considers
971 to be current. One case where this might be necessary is if the
972 user explicitly changes the PC value that gdb considers to be
973 current. FIXME: Investigate if this is necessary or not.
974 */
975
976void
1ab3bf1b
JG
977child_resume (step, signal)
978 int step;
979 int signal;
35f5886e
FF
980{
981 errno = 0;
982 pi.prrun.pr_flags = PRSVADDR | PRSTRACE | PRSFAULT | PRCFAULT;
983 pi.prrun.pr_vaddr = (caddr_t) *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
984 if (signal)
985 {
986 if (signal != pi.prstatus.pr_cursig)
987 {
988 struct siginfo siginfo;
989 siginfo.si_signo = signal;
990 siginfo.si_code = 0;
991 siginfo.si_errno = 0;
992 (void) ioctl (pi.fd, PIOCSSIG, &siginfo);
993 }
994 }
995 else
996 {
997 pi.prrun.pr_flags |= PRCSIG;
998 }
999 if (step)
1000 {
1001 pi.prrun.pr_flags |= PRSTEP;
1002 }
1003 if (ioctl (pi.fd, PIOCRUN, &pi.prrun) != 0)
1004 {
1005 perror_with_name (pi.pathname);
1006 /* NOTREACHED */
1007 }
1008}
1009
1010/*
1011
1012GLOBAL FUNCTION
1013
1014 fetch_inferior_registers -- fetch current registers from inferior
1015
1016SYNOPSIS
1017
1ab3bf1b 1018 void fetch_inferior_registers (int regno)
35f5886e
FF
1019
1020DESCRIPTION
1021
1022 Read the current values of the inferior's registers, both the
1023 general register set and floating point registers (if supported)
1024 and update gdb's idea of their current values.
1025
1026*/
1027
1028void
1ab3bf1b
JG
1029fetch_inferior_registers (regno)
1030 int regno;
35f5886e
FF
1031{
1032 if (ioctl (pi.fd, PIOCGREG, &pi.gregset) != -1)
1033 {
1034 supply_gregset (&pi.gregset);
1035 }
1036#if defined (FP0_REGNUM)
1037 if (ioctl (pi.fd, PIOCGFPREG, &pi.fpregset) != -1)
1038 {
1039 supply_fpregset (&pi.fpregset);
1040 }
1041#endif
1042}
1043
fb182850
FF
1044/*
1045
1046GLOBAL FUNCTION
1047
1048 fetch_core_registers -- fetch current registers from core file data
1049
1050SYNOPSIS
1051
1052 void fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
1ab3bf1b 1053 int which, unsigned in reg_addr)
fb182850
FF
1054
1055DESCRIPTION
1056
1057 Read the values of either the general register set (WHICH equals 0)
1058 or the floating point register set (WHICH equals 2) from the core
1059 file data (pointed to by CORE_REG_SECT), and update gdb's idea of
1060 their current values. The CORE_REG_SIZE parameter is ignored.
1061
1062NOTES
1063
1064 Use the indicated sizes to validate the gregset and fpregset
1065 structures.
1066*/
1067
1068void
1ab3bf1b 1069fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
fb182850
FF
1070 char *core_reg_sect;
1071 unsigned core_reg_size;
1072 int which;
1ab3bf1b 1073 unsigned int reg_addr; /* Unused in this version */
fb182850
FF
1074{
1075
1076 if (which == 0)
1077 {
1078 if (core_reg_size != sizeof (pi.gregset))
1079 {
1080 warning ("wrong size gregset struct in core file");
1081 }
1082 else
1083 {
1084 (void) memcpy ((char *) &pi.gregset, core_reg_sect,
1085 sizeof (pi.gregset));
1086 supply_gregset (&pi.gregset);
1087 }
1088 }
1089 else if (which == 2)
1090 {
1091 if (core_reg_size != sizeof (pi.fpregset))
1092 {
1093 warning ("wrong size fpregset struct in core file");
1094 }
1095 else
1096 {
1097 (void) memcpy ((char *) &pi.fpregset, core_reg_sect,
1098 sizeof (pi.fpregset));
1099#if defined (FP0_REGNUM)
1100 supply_fpregset (&pi.fpregset);
1101#endif
1102 }
1103 }
1104}
35f5886e
FF
1105
1106/*
1107
1108LOCAL FUNCTION
1109
1110 proc_init_failed - called whenever /proc access initialization fails
1111
1112SYNOPSIS
1113
1114 static void proc_init_failed (char *why)
1115
1116DESCRIPTION
1117
1118 This function is called whenever initialization of access to a /proc
1119 entry fails. It prints a suitable error message, does some cleanup,
1120 and then invokes the standard error processing routine which dumps
1121 us back into the command loop.
1122 */
1123
1124static void
1ab3bf1b
JG
1125proc_init_failed (why)
1126 char *why;
35f5886e
FF
1127{
1128 print_sys_errmsg (pi.pathname, errno);
1129 (void) kill (pi.pid, SIGKILL);
a39ad5ce 1130 close_proc_file (&pi);
35f5886e
FF
1131 error (why);
1132 /* NOTREACHED */
1133}
1134
1135/*
1136
1137LOCAL FUNCTION
1138
1139 close_proc_file - close any currently open /proc entry
1140
1141SYNOPSIS
1142
a39ad5ce 1143 static void close_proc_file (struct procinfo *pip)
35f5886e
FF
1144
1145DESCRIPTION
1146
1147 Close any currently open /proc entry and mark the process information
1148 entry as invalid. In order to ensure that we don't try to reuse any
1149 stale information, the pid, fd, and pathnames are explicitly
1150 invalidated, which may be overkill.
1151
1152 */
1153
1154static void
1ab3bf1b
JG
1155close_proc_file (pip)
1156 struct procinfo *pip;
35f5886e 1157{
a39ad5ce
FF
1158 pip -> pid = 0;
1159 if (pip -> valid)
35f5886e 1160 {
a39ad5ce 1161 (void) close (pip -> fd);
35f5886e 1162 }
a39ad5ce
FF
1163 pip -> fd = -1;
1164 if (pip -> pathname)
35f5886e 1165 {
a39ad5ce
FF
1166 free (pip -> pathname);
1167 pip -> pathname = NULL;
35f5886e 1168 }
a39ad5ce 1169 pip -> valid = 0;
35f5886e
FF
1170}
1171
1172/*
1173
1174LOCAL FUNCTION
1175
1176 open_proc_file - open a /proc entry for a given process id
1177
1178SYNOPSIS
1179
a39ad5ce 1180 static int open_proc_file (pid, struct procinfo *pip)
35f5886e
FF
1181
1182DESCRIPTION
1183
1184 Given a process id, close the existing open /proc entry (if any)
1185 and open one for the new process id. Once it is open, then
1186 mark the local process information structure as valid, which
1187 guarantees that the pid, fd, and pathname fields match an open
1188 /proc entry. Returns zero if the open fails, nonzero otherwise.
1189
1190 Note that the pathname is left intact, even when the open fails,
1191 so that callers can use it to construct meaningful error messages
1192 rather than just "file open failed".
1193 */
1194
1195static int
1ab3bf1b
JG
1196open_proc_file (pid, pip)
1197 int pid;
1198 struct procinfo *pip;
35f5886e 1199{
a39ad5ce
FF
1200 pip -> valid = 0;
1201 if (pip -> valid)
1202 {
1203 (void) close (pip -> fd);
1204 }
1205 if (pip -> pathname == NULL)
1206 {
1207 pip -> pathname = xmalloc (32);
1208 }
1209 sprintf (pip -> pathname, PROC_NAME_FMT, pid);
1210 if ((pip -> fd = open (pip -> pathname, O_RDWR)) >= 0)
1211 {
1212 pip -> valid = 1;
1213 pip -> pid = pid;
1214 }
1215 return (pip -> valid);
1216}
1217
f66f459f 1218static char *
1ab3bf1b
JG
1219mappingflags (flags)
1220 long flags;
a39ad5ce
FF
1221{
1222 static char asciiflags[7];
1223
1224 strcpy (asciiflags, "------");
1225 if (flags & MA_STACK) asciiflags[0] = 's';
1226 if (flags & MA_BREAK) asciiflags[1] = 'b';
1227 if (flags & MA_SHARED) asciiflags[2] = 's';
1228 if (flags & MA_READ) asciiflags[3] = 'r';
1229 if (flags & MA_WRITE) asciiflags[4] = 'w';
1230 if (flags & MA_EXEC) asciiflags[5] = 'x';
1231 return (asciiflags);
1232}
1233
1234static void
1ab3bf1b
JG
1235proc_info_address_map (pip, verbose)
1236 struct procinfo *pip;
1237 int verbose;
a39ad5ce
FF
1238{
1239 int nmap;
1240 struct prmap *prmaps;
1241 struct prmap *prmap;
1242
1243 printf_filtered ("Mapped address spaces:\n\n");
1244 printf_filtered ("\t%10s %10s %10s %10s %6s\n",
1245 "Start Addr",
1246 " End Addr",
1247 " Size",
1248 " Offset",
1249 "Flags");
1250 if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0)
1251 {
1ab3bf1b 1252 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
a39ad5ce
FF
1253 if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0)
1254 {
1255 for (prmap = prmaps; prmap -> pr_size; ++prmap)
1256 {
1257 printf_filtered ("\t%#10x %#10x %#10x %#10x %6s\n",
1258 prmap -> pr_vaddr,
1259 prmap -> pr_vaddr + prmap -> pr_size - 1,
1260 prmap -> pr_size,
1261 prmap -> pr_off,
1262 mappingflags (prmap -> pr_mflags));
1263 }
1264 }
1265 }
1266 printf_filtered ("\n\n");
1267}
1268
1269/*
1270
1271LOCAL FUNCTION
1272
1273 proc_info -- implement the "info proc" command
1274
1275SYNOPSIS
1276
1277 void proc_info (char *args, int from_tty)
1278
1279DESCRIPTION
1280
1281 Implement gdb's "info proc" command by using the /proc interface
1282 to print status information about any currently running process.
1283
1284 Examples of the use of "info proc" are:
1285
1286 info proc Print short info about current inferior.
1287 info proc verbose Print verbose info about current inferior.
1288 info proc 123 Print short info about process pid 123.
1289 info proc 123 verbose Print verbose info about process pid 123.
1290
1291 */
1292
1293static void
1ab3bf1b
JG
1294proc_info (args, from_tty)
1295 char *args;
1296 int from_tty;
a39ad5ce
FF
1297{
1298 int verbose = 0;
1299 int pid;
1300 struct procinfo pii;
1301 struct procinfo *pip;
1302 struct cleanup *old_chain;
1303 char *nexttok;
a39ad5ce
FF
1304
1305 old_chain = make_cleanup (null_cleanup, 0);
1306
1307 /* Default to using the current inferior if no pid specified */
1308
1309 pip = &pi;
1310
1311 /* Parse the args string, looking for "verbose" (or any abbrev) and
1312 for a specific pid. If a specific pid is found, the process
1313 file is opened. */
1314
1315 if (args != NULL)
35f5886e 1316 {
a39ad5ce
FF
1317 while ((nexttok = strtok (args, " \t")) != NULL)
1318 {
1319 args = NULL;
1320 if (strncmp (nexttok, "verbose", strlen (nexttok)) == 0)
1321 {
1322 verbose++;
1323 }
1324 else if ((pii.pid = atoi (nexttok)) > 0)
1325 {
1326 pid = pii.pid;
1327 pip = &pii;
1328 (void) memset (&pii, 0, sizeof (pii));
1329 if (!open_proc_file (pid, pip))
1330 {
1331 perror_with_name (pip -> pathname);
1332 /* NOTREACHED */
1333 }
1334 make_cleanup (close_proc_file, pip);
1335 }
1336 }
35f5886e 1337 }
a39ad5ce
FF
1338
1339 /* If we don't have a valid open process at this point, then we have no
1340 inferior or didn't specify a specific pid. */
1341
1342 if (!pip -> valid)
35f5886e 1343 {
a39ad5ce 1344 error ("No process. Run an inferior or specify an explicit pid.");
35f5886e 1345 }
a39ad5ce 1346 if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
35f5886e 1347 {
a39ad5ce
FF
1348 print_sys_errmsg (pip -> pathname, errno);
1349 error ("PIOCSTATUS failed");
35f5886e 1350 }
a39ad5ce
FF
1351
1352 printf_filtered ("\nStatus information for %s:\n\n", pip -> pathname);
1353 proc_info_address_map (pip, verbose);
1354#if 0
1355 proc_info_flags (pip, verbose);
1356 proc_info_why (pip, verbose);
1357 proc_info_what (pip, verbose);
1358 proc_info_info (pip, verbose);
1359 proc_info_cursig (pip, verbose);
1360 proc_info_sigpend (pip, verbose);
1361 proc_info_sighold (pip, verbose);
1362 proc_info_altstack (pip, verbose);
1363 proc_info_action (pip, verbose);
1364 proc_info_id (pip, verbose);
1365 proc_info_times (pip, verbose);
1366 proc_info_clname (pip,verbose);
1367 proc_info_instr (pip, verbose);
1368 proc_info_reg (pip, verbose);
1369#endif
1370
1371 /* All done, deal with closing any temporary process info structure,
1372 freeing temporary memory , etc. */
1373
1374 do_cleanups (old_chain);
1375}
1376
1377/*
1378
1379GLOBAL FUNCTION
1380
1381 _initialize_proc_fs -- initialize the process file system stuff
1382
1383SYNOPSIS
1384
1385 void _initialize_proc_fs (void)
1386
1387DESCRIPTION
1388
1389 Do required initializations during gdb startup for using the
1390 /proc file system interface.
1391
1392*/
1393
1394static char *proc_desc =
1395"Show current process status information using /proc entry.\n\
1396With no arguments, prints short form. With 'verbose' prints long form.";
1397
1398void
1399_initialize_proc_fs ()
1400{
1401 add_info ("proc", proc_info, proc_desc);
35f5886e
FF
1402}
1403
1404#endif /* USE_PROC_FS */
This page took 0.139873 seconds and 4 git commands to generate.