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