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