home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / procfs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  74.3 KB  |  3,116 lines

  1. /* Machine independent support for SVR4 /proc (process file system) for GDB.
  2.    Copyright 1991, 1992 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. #include <time.h>
  38. #include <sys/procfs.h>
  39. #include <fcntl.h>
  40. #include <errno.h>
  41. #include <string.h>
  42.  
  43. #include "inferior.h"
  44. #include "target.h"
  45. #include "command.h"
  46. #include "gdbcore.h"
  47.  
  48. #define MAX_SYSCALLS    256    /* Maximum number of syscalls for table */
  49.  
  50. #ifndef PROC_NAME_FMT
  51. #define PROC_NAME_FMT "/proc/%05d"
  52. #endif
  53.  
  54. extern struct target_ops procfs_ops;        /* Forward declaration */
  55.  
  56. #if 1    /* FIXME: Gross and ugly hack to resolve coredep.c global */
  57. CORE_ADDR kernel_u_addr;
  58. #endif
  59.  
  60. #ifdef BROKEN_SIGINFO_H        /* Workaround broken SGS <sys/siginfo.h> */
  61. #undef si_pid
  62. #define si_pid _data._proc.pid
  63. #undef si_uid
  64. #define si_uid _data._proc._pdata._kill.uid
  65. #endif /* BROKEN_SIGINFO_H */
  66.  
  67. /*  All access to the inferior, either one started by gdb or one that has
  68.     been attached to, is controlled by an instance of a procinfo structure,
  69.     defined below.  Since gdb currently only handles one inferior at a time,
  70.     the procinfo structure for the inferior is statically allocated and
  71.     only one exists at any given time.  There is a separate procinfo
  72.     structure for use by the "info proc" command, so that we can print
  73.     useful information about any random process without interfering with
  74.     the inferior's procinfo information. */
  75.  
  76. struct procinfo {
  77.   int valid;            /* Nonzero if pid, fd, & pathname are valid */
  78.   int pid;            /* Process ID of inferior */
  79.   int fd;            /* File descriptor for /proc entry */
  80.   char *pathname;        /* Pathname to /proc entry */
  81.   int was_stopped;        /* Nonzero if was stopped prior to attach */
  82.   int nopass_next_sigstop;    /* Don't pass a sigstop on next resume */
  83.   prrun_t prrun;        /* Control state when it is run */
  84.   prstatus_t prstatus;        /* Current process status info */
  85.   gregset_t gregset;        /* General register set */
  86.   fpregset_t fpregset;        /* Floating point register set */
  87.   fltset_t fltset;        /* Current traced hardware fault set */
  88.   sigset_t trace;        /* Current traced signal set */
  89.   sysset_t exitset;        /* Current traced system call exit set */
  90.   sysset_t entryset;        /* Current traced system call entry set */
  91.   fltset_t saved_fltset;    /* Saved traced hardware fault set */
  92.   sigset_t saved_trace;        /* Saved traced signal set */
  93.   sigset_t saved_sighold;    /* Saved held signal set */
  94.   sysset_t saved_exitset;    /* Saved traced system call exit set */
  95.   sysset_t saved_entryset;    /* Saved traced system call entry set */
  96. };
  97.  
  98. static struct procinfo pi;    /* Inferior's process information */
  99.  
  100. /*  Much of the information used in the /proc interface, particularly for
  101.     printing status information, is kept as tables of structures of the
  102.     following form.  These tables can be used to map numeric values to
  103.     their symbolic names and to a string that describes their specific use. */
  104.  
  105. struct trans {
  106.   int value;            /* The numeric value */
  107.   char *name;            /* The equivalent symbolic value */
  108.   char *desc;            /* Short description of value */
  109. };
  110.  
  111. /*  Translate bits in the pr_flags member of the prstatus structure, into the
  112.     names and desc information. */
  113.  
  114. static struct trans pr_flag_table[] =
  115. {
  116. #if defined (PR_STOPPED)
  117.   PR_STOPPED, "PR_STOPPED", "Process is stopped",
  118. #endif
  119. #if defined (PR_ISTOP)
  120.   PR_ISTOP, "PR_ISTOP", "Stopped on an event of interest",
  121. #endif
  122. #if defined (PR_DSTOP)
  123.   PR_DSTOP, "PR_DSTOP", "A stop directive is in effect",
  124. #endif
  125. #if defined (PR_ASLEEP)
  126.   PR_ASLEEP, "PR_ASLEEP", "Sleeping in an interruptible system call",
  127. #endif
  128. #if defined (PR_FORK)
  129.   PR_FORK, "PR_FORK", "Inherit-on-fork is in effect",
  130. #endif
  131. #if defined (PR_RLC)
  132.   PR_RLC, "PR_RLC", "Run-on-last-close is in effect",
  133. #endif
  134. #if defined (PR_PTRACE)
  135.   PR_PTRACE, "PR_PTRACE", "Process is being controlled by ptrace",
  136. #endif
  137. #if defined (PR_PCINVAL)
  138.   PR_PCINVAL, "PR_PCINVAL", "PC refers to an invalid virtual address",
  139. #endif
  140. #if defined (PR_ISSYS)
  141.   PR_ISSYS, "PR_ISSYS", "Is a system process",
  142. #endif
  143. #if defined (PR_STEP)
  144.   PR_STEP, "PR_STEP", "Process has single step pending",
  145. #endif
  146. #if defined (PR_KLC)
  147.   PR_KLC, "PR_KLC", "Kill-on-last-close is in effect",
  148. #endif
  149. #if defined (PR_ASYNC)
  150.   PR_ASYNC, "PR_ASYNC", "Asynchronous stop is in effect",
  151. #endif
  152. #if defined (PR_PCOMPAT)
  153.   PR_PCOMPAT, "PR_PCOMPAT", "Ptrace compatibility mode in effect",
  154. #endif
  155.  0, NULL, NULL
  156. };
  157.  
  158. /*  Translate values in the pr_why field of the prstatus struct. */
  159.  
  160. static struct trans pr_why_table[] =
  161. {
  162. #if defined (PR_REQUESTED)
  163.  PR_REQUESTED, "PR_REQUESTED", "Directed to stop via PIOCSTOP/PIOCWSTOP",
  164. #endif
  165. #if defined (PR_SIGNALLED)
  166.  PR_SIGNALLED, "PR_SIGNALLED", "Receipt of a traced signal",
  167. #endif
  168. #if defined (PR_FAULTED)
  169.  PR_FAULTED, "PR_FAULTED", "Incurred a traced hardware fault",
  170. #endif
  171. #if defined (PR_SYSENTRY)
  172.  PR_SYSENTRY, "PR_SYSENTRY", "Entry to a traced system call",
  173. #endif
  174. #if defined (PR_SYSEXIT)
  175.  PR_SYSEXIT, "PR_SYSEXIT", "Exit from a traced system call",
  176. #endif
  177. #if defined (PR_JOBCONTROL)
  178.  PR_JOBCONTROL, "PR_JOBCONTROL", "Default job control stop signal action",
  179. #endif
  180. #if defined (PR_SUSPENDED)
  181.  PR_SUSPENDED, "PR_SUSPENDED", "Process suspended",
  182. #endif
  183.  0, NULL, NULL
  184. };
  185.  
  186. /*  Hardware fault translation table. */
  187.  
  188. static struct trans faults_table[] =
  189. {
  190. #if defined (FLTILL)
  191.  FLTILL, "FLTILL", "Illegal instruction",
  192. #endif
  193. #if defined (FLTPRIV)
  194.  FLTPRIV, "FLTPRIV", "Privileged instruction",
  195. #endif
  196. #if defined (FLTBPT)
  197.  FLTBPT, "FLTBPT", "Breakpoint trap",
  198. #endif
  199. #if defined (FLTTRACE)
  200.  FLTTRACE, "FLTTRACE", "Trace trap",
  201. #endif
  202. #if defined (FLTACCESS)
  203.  FLTACCESS, "FLTACCESS", "Memory access fault",
  204. #endif
  205. #if defined (FLTBOUNDS)
  206.  FLTBOUNDS, "FLTBOUNDS", "Memory bounds violation",
  207. #endif
  208. #if defined (FLTIOVF)
  209.  FLTIOVF, "FLTIOVF", "Integer overflow",
  210. #endif
  211. #if defined (FLTIZDIV)
  212.  FLTIZDIV, "FLTIZDIV", "Integer zero divide",
  213. #endif
  214. #if defined (FLTFPE)
  215.  FLTFPE, "FLTFPE", "Floating-point exception",
  216. #endif
  217. #if defined (FLTSTACK)
  218.  FLTSTACK, "FLTSTACK", "Unrecoverable stack fault",
  219. #endif
  220. #if defined (FLTPAGE)
  221.  FLTPAGE, "FLTPAGE", "Recoverable page fault",
  222. #endif
  223.  0, NULL, NULL
  224. };
  225.  
  226. /* Translation table for signal generation information.  See UNIX System
  227.    V Release 4 Programmer's Reference Manual, siginfo(5).  */
  228.  
  229. static struct sigcode {
  230.   int signo;
  231.   int code;
  232.   char *codename;
  233.   char *desc;
  234. } siginfo_table[] = {
  235. #if defined (SIGILL) && defined (ILL_ILLOPC)
  236.   SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode",
  237. #endif
  238. #if defined (SIGILL) && defined (ILL_ILLOPN)
  239.   SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand",
  240. #endif
  241. #if defined (SIGILL) && defined (ILL_ILLADR)
  242.   SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode",
  243. #endif
  244. #if defined (SIGILL) && defined (ILL_ILLTRP)
  245.   SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap",
  246. #endif
  247. #if defined (SIGILL) && defined (ILL_PRVOPC)
  248.   SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode",
  249. #endif
  250. #if defined (SIGILL) && defined (ILL_PRVREG)
  251.   SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register",
  252. #endif
  253. #if defined (SIGILL) && defined (ILL_COPROC)
  254.   SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error",
  255. #endif
  256. #if defined (SIGILL) && defined (ILL_BADSTK)
  257.   SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error",
  258. #endif
  259. #if defined (SIGFPE) && defined (FPE_INTDIV)
  260.   SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero",
  261. #endif
  262. #if defined (SIGFPE) && defined (FPE_INTOVF)
  263.   SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow",
  264. #endif
  265. #if defined (SIGFPE) && defined (FPE_FLTDIV)
  266.   SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating point divide by zero",
  267. #endif
  268. #if defined (SIGFPE) && defined (FPE_FLTOVF)
  269.   SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating point overflow",
  270. #endif
  271. #if defined (SIGFPE) && defined (FPE_FLTUND)
  272.   SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating point underflow",
  273. #endif
  274. #if defined (SIGFPE) && defined (FPE_FLTRES)
  275.   SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating point inexact result",
  276. #endif
  277. #if defined (SIGFPE) && defined (FPE_FLTINV)
  278.   SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating point operation",
  279. #endif
  280. #if defined (SIGFPE) && defined (FPE_FLTSUB)
  281.   SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range",
  282. #endif
  283. #if defined (SIGSEGV) && defined (SEGV_MAPERR)
  284.   SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object",
  285. #endif
  286. #if defined (SIGSEGV) && defined (SEGV_ACCERR)
  287.   SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for object",
  288. #endif
  289. #if defined (SIGBUS) && defined (BUS_ADRALN)
  290.   SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment",
  291. #endif
  292. #if defined (SIGBUS) && defined (BUS_ADRERR)
  293.   SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Non-existent physical address",
  294. #endif
  295. #if defined (SIGBUS) && defined (BUS_OBJERR)
  296.   SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object specific hardware error",
  297. #endif
  298. #if defined (SIGTRAP) && defined (TRAP_BRKPT)
  299.   SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint",
  300. #endif
  301. #if defined (SIGTRAP) && defined (TRAP_TRACE)
  302.   SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap",
  303. #endif
  304. #if defined (SIGCLD) && defined (CLD_EXITED)
  305.   SIGCLD, CLD_EXITED, "CLD_EXITED", "Child has exited",
  306. #endif
  307. #if defined (SIGCLD) && defined (CLD_KILLED)
  308.   SIGCLD, CLD_KILLED, "CLD_KILLED", "Child was killed",
  309. #endif
  310. #if defined (SIGCLD) && defined (CLD_DUMPED)
  311.   SIGCLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally",
  312. #endif
  313. #if defined (SIGCLD) && defined (CLD_TRAPPED)
  314.   SIGCLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped",
  315. #endif
  316. #if defined (SIGCLD) && defined (CLD_STOPPED)
  317.   SIGCLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped",
  318. #endif
  319. #if defined (SIGCLD) && defined (CLD_CONTINUED)
  320.   SIGCLD, CLD_CONTINUED, "CLD_CONTINUED", "Stopped child had continued",
  321. #endif
  322. #if defined (SIGPOLL) && defined (POLL_IN)
  323.   SIGPOLL, POLL_IN, "POLL_IN", "Input input available",
  324. #endif
  325. #if defined (SIGPOLL) && defined (POLL_OUT)
  326.   SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available",
  327. #endif
  328. #if defined (SIGPOLL) && defined (POLL_MSG)
  329.   SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available",
  330. #endif
  331. #if defined (SIGPOLL) && defined (POLL_ERR)
  332.   SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error",
  333. #endif
  334. #if defined (SIGPOLL) && defined (POLL_PRI)
  335.   SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available",
  336. #endif
  337. #if defined (SIGPOLL) && defined (POLL_HUP)
  338.   SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected",
  339. #endif
  340.   0, 0, NULL, NULL
  341. };
  342.  
  343. static char *syscall_table[MAX_SYSCALLS];
  344.  
  345. /* Prototypes for local functions */
  346.  
  347. static void
  348. set_proc_siginfo PARAMS ((struct procinfo *, int));
  349.  
  350. static void
  351. init_syscall_table PARAMS ((void));
  352.  
  353. static char *
  354. syscallname PARAMS ((int));
  355.  
  356. static char *
  357. signalname PARAMS ((int));
  358.  
  359. static char *
  360. errnoname PARAMS ((int));
  361.  
  362. static int
  363. proc_address_to_fd PARAMS ((CORE_ADDR, int));
  364.  
  365. static int
  366. open_proc_file PARAMS ((int, struct procinfo *, int));
  367.  
  368. static void
  369. close_proc_file PARAMS ((struct procinfo *));
  370.  
  371. static void
  372. unconditionally_kill_inferior PARAMS ((void));
  373.  
  374. static void
  375. proc_init_failed PARAMS ((char *));
  376.  
  377. static void
  378. info_proc PARAMS ((char *, int));
  379.  
  380. static void
  381. info_proc_flags PARAMS ((struct procinfo *, int));
  382.  
  383. static void
  384. info_proc_stop PARAMS ((struct procinfo *, int));
  385.  
  386. static void
  387. info_proc_siginfo PARAMS ((struct procinfo *, int));
  388.  
  389. static void
  390. info_proc_syscalls PARAMS ((struct procinfo *, int));
  391.  
  392. static void
  393. info_proc_mappings PARAMS ((struct procinfo *, int));
  394.  
  395. static void
  396. info_proc_signals PARAMS ((struct procinfo *, int));
  397.  
  398. static void
  399. info_proc_faults PARAMS ((struct procinfo *, int));
  400.  
  401. static char *
  402. mappingflags PARAMS ((long));
  403.  
  404. static char *
  405. lookupname PARAMS ((struct trans *, unsigned int, char *));
  406.  
  407. static char *
  408. lookupdesc PARAMS ((struct trans *, unsigned int));
  409.  
  410. static int
  411. do_attach PARAMS ((int pid));
  412.  
  413. static void
  414. do_detach PARAMS ((int siggnal));
  415.  
  416. static void
  417. procfs_create_inferior PARAMS ((char *, char *, char **));
  418.  
  419. static void
  420. procfs_notice_signals PARAMS ((void));
  421.  
  422. /* External function prototypes that can't be easily included in any
  423.    header file because the args are typedefs in system include files. */
  424.  
  425. extern void
  426. supply_gregset PARAMS ((gregset_t *));
  427.  
  428. extern void
  429. fill_gregset PARAMS ((gregset_t *, int));
  430.  
  431. extern void
  432. supply_fpregset PARAMS ((fpregset_t *));
  433.  
  434. extern void
  435. fill_fpregset PARAMS ((fpregset_t *, int));
  436.  
  437. /*
  438.  
  439. LOCAL FUNCTION
  440.  
  441.     lookupdesc -- translate a value to a summary desc string
  442.  
  443. SYNOPSIS
  444.  
  445.     static char *lookupdesc (struct trans *transp, unsigned int val);
  446.  
  447. DESCRIPTION
  448.     
  449.     Given a pointer to a translation table and a value to be translated,
  450.     lookup the desc string and return it.
  451.  */
  452.  
  453. static char *
  454. lookupdesc (transp, val)
  455.      struct trans *transp;
  456.      unsigned int val;
  457. {
  458.   char *desc;
  459.   
  460.   for (desc = NULL; transp -> name != NULL; transp++)
  461.     {
  462.       if (transp -> value == val)
  463.     {
  464.       desc = transp -> desc;
  465.       break;
  466.     }
  467.     }
  468.  
  469.   /* Didn't find a translation for the specified value, set a default one. */
  470.  
  471.   if (desc == NULL)
  472.     {
  473.       desc = "Unknown";
  474.     }
  475.   return (desc);
  476. }
  477.  
  478. /*
  479.  
  480. LOCAL FUNCTION
  481.  
  482.     lookupname -- translate a value to symbolic name
  483.  
  484. SYNOPSIS
  485.  
  486.     static char *lookupname (struct trans *transp, unsigned int val,
  487.                  char *prefix);
  488.  
  489. DESCRIPTION
  490.     
  491.     Given a pointer to a translation table, a value to be translated,
  492.     and a default prefix to return if the value can't be translated,
  493.     match the value with one of the translation table entries and
  494.     return a pointer to the symbolic name.
  495.  
  496.     If no match is found it just returns the value as a printable string,
  497.     with the given prefix.  The previous such value, if any, is freed
  498.     at this time.
  499.  */
  500.  
  501. static char *
  502. lookupname (transp, val, prefix)
  503.      struct trans *transp;
  504.      unsigned int val;
  505.      char *prefix;
  506. {
  507.   static char *locbuf;
  508.   char *name;
  509.   
  510.   for (name = NULL; transp -> name != NULL; transp++)
  511.     {
  512.       if (transp -> value == val)
  513.     {
  514.       name = transp -> name;
  515.       break;
  516.     }
  517.     }
  518.  
  519.   /* Didn't find a translation for the specified value, build a default
  520.      one using the specified prefix and return it.  The lifetime of
  521.      the value is only until the next one is needed. */
  522.  
  523.   if (name == NULL)
  524.     {
  525.       if (locbuf != NULL)
  526.     {
  527.       free (locbuf);
  528.     }
  529.       locbuf = xmalloc (strlen (prefix) + 16);
  530.       sprintf (locbuf, "%s %u", prefix, val);
  531.       name = locbuf;
  532.     }
  533.   return (name);
  534. }
  535.  
  536. static char *
  537. sigcodename (sip)
  538.      siginfo_t *sip;
  539. {
  540.   struct sigcode *scp;
  541.   char *name = NULL;
  542.   static char locbuf[32];
  543.   
  544.   for (scp = siginfo_table; scp -> codename != NULL; scp++)
  545.     {
  546.       if ((scp -> signo == sip -> si_signo) &&
  547.       (scp -> code == sip -> si_code))
  548.     {
  549.       name = scp -> codename;
  550.       break;
  551.     }
  552.     }
  553.   if (name == NULL)
  554.     {
  555.       sprintf (locbuf, "sigcode %u", sip -> si_signo);
  556.       name = locbuf;
  557.     }
  558.   return (name);
  559. }
  560.  
  561. static char *
  562. sigcodedesc (sip)
  563.      siginfo_t *sip;
  564. {
  565.   struct sigcode *scp;
  566.   char *desc = NULL;
  567.   
  568.   for (scp = siginfo_table; scp -> codename != NULL; scp++)
  569.     {
  570.       if ((scp -> signo == sip -> si_signo) &&
  571.       (scp -> code == sip -> si_code))
  572.     {
  573.       desc = scp -> desc;
  574.       break;
  575.     }
  576.     }
  577.   if (desc == NULL)
  578.     {
  579.       desc = "Unrecognized signal or trap use";
  580.     }
  581.   return (desc);
  582. }
  583.  
  584. /*
  585.  
  586. LOCAL FUNCTION
  587.  
  588.     syscallname - translate a system call number into a system call name
  589.  
  590. SYNOPSIS
  591.  
  592.     char *syscallname (int syscallnum)
  593.  
  594. DESCRIPTION
  595.  
  596.     Given a system call number, translate it into the printable name
  597.     of a system call, or into "syscall <num>" if it is an unknown
  598.     number.
  599.  */
  600.  
  601. static char *
  602. syscallname (syscallnum)
  603.      int syscallnum;
  604. {
  605.   static char locbuf[32];
  606.   char *rtnval;
  607.   
  608.   if (syscallnum >= 0 && syscallnum < MAX_SYSCALLS)
  609.     {
  610.       rtnval = syscall_table[syscallnum];
  611.     }
  612.   else
  613.     {
  614.       sprintf (locbuf, "syscall %u", syscallnum);
  615.       rtnval = locbuf;
  616.     }
  617.   return (rtnval);
  618. }
  619.  
  620. /*
  621.  
  622. LOCAL FUNCTION
  623.  
  624.     init_syscall_table - initialize syscall translation table
  625.  
  626. SYNOPSIS
  627.  
  628.     void init_syscall_table (void)
  629.  
  630. DESCRIPTION
  631.  
  632.     Dynamically initialize the translation table to convert system
  633.     call numbers into printable system call names.  Done once per
  634.     gdb run, on initialization.
  635.  
  636. NOTES
  637.  
  638.     This is awfully ugly, but preprocessor tricks to make it prettier
  639.     tend to be nonportable.
  640.  */
  641.  
  642. static void
  643. init_syscall_table ()
  644. {
  645. #if defined (SYS_exit)
  646.   syscall_table[SYS_exit] = "exit";
  647. #endif
  648. #if defined (SYS_fork)
  649.   syscall_table[SYS_fork] = "fork";
  650. #endif
  651. #if defined (SYS_read)
  652.   syscall_table[SYS_read] = "read";
  653. #endif
  654. #if defined (SYS_write)
  655.   syscall_table[SYS_write] = "write";
  656. #endif
  657. #if defined (SYS_open)
  658.   syscall_table[SYS_open] = "open";
  659. #endif
  660. #if defined (SYS_close)
  661.   syscall_table[SYS_close] = "close";
  662. #endif
  663. #if defined (SYS_wait)
  664.   syscall_table[SYS_wait] = "wait";
  665. #endif
  666. #if defined (SYS_creat)
  667.   syscall_table[SYS_creat] = "creat";
  668. #endif
  669. #if defined (SYS_link)
  670.   syscall_table[SYS_link] = "link";
  671. #endif
  672. #if defined (SYS_unlink)
  673.   syscall_table[SYS_unlink] = "unlink";
  674. #endif
  675. #if defined (SYS_exec)
  676.   syscall_table[SYS_exec] = "exec";
  677. #endif
  678. #if defined (SYS_execv)
  679.   syscall_table[SYS_execv] = "execv";
  680. #endif
  681. #if defined (SYS_execve)
  682.   syscall_table[SYS_execve] = "execve";
  683. #endif
  684. #if defined (SYS_chdir)
  685.   syscall_table[SYS_chdir] = "chdir";
  686. #endif
  687. #if defined (SYS_time)
  688.   syscall_table[SYS_time] = "time";
  689. #endif
  690. #if defined (SYS_mknod)
  691.   syscall_table[SYS_mknod] = "mknod";
  692. #endif
  693. #if defined (SYS_chmod)
  694.   syscall_table[SYS_chmod] = "chmod";
  695. #endif
  696. #if defined (SYS_chown)
  697.   syscall_table[SYS_chown] = "chown";
  698. #endif
  699. #if defined (SYS_brk)
  700.   syscall_table[SYS_brk] = "brk";
  701. #endif
  702. #if defined (SYS_stat)
  703.   syscall_table[SYS_stat] = "stat";
  704. #endif
  705. #if defined (SYS_lseek)
  706.   syscall_table[SYS_lseek] = "lseek";
  707. #endif
  708. #if defined (SYS_getpid)
  709.   syscall_table[SYS_getpid] = "getpid";
  710. #endif
  711. #if defined (SYS_mount)
  712.   syscall_table[SYS_mount] = "mount";
  713. #endif
  714. #if defined (SYS_umount)
  715.   syscall_table[SYS_umount] = "umount";
  716. #endif
  717. #if defined (SYS_setuid)
  718.   syscall_table[SYS_setuid] = "setuid";
  719. #endif
  720. #if defined (SYS_getuid)
  721.   syscall_table[SYS_getuid] = "getuid";
  722. #endif
  723. #if defined (SYS_stime)
  724.   syscall_table[SYS_stime] = "stime";
  725. #endif
  726. #if defined (SYS_ptrace)
  727.   syscall_table[SYS_ptrace] = "ptrace";
  728. #endif
  729. #if defined (SYS_alarm)
  730.   syscall_table[SYS_alarm] = "alarm";
  731. #endif
  732. #if defined (SYS_fstat)
  733.   syscall_table[SYS_fstat] = "fstat";
  734. #endif
  735. #if defined (SYS_pause)
  736.   syscall_table[SYS_pause] = "pause";
  737. #endif
  738. #if defined (SYS_utime)
  739.   syscall_table[SYS_utime] = "utime";
  740. #endif
  741. #if defined (SYS_stty)
  742.   syscall_table[SYS_stty] = "stty";
  743. #endif
  744. #if defined (SYS_gtty)
  745.   syscall_table[SYS_gtty] = "gtty";
  746. #endif
  747. #if defined (SYS_access)
  748.   syscall_table[SYS_access] = "access";
  749. #endif
  750. #if defined (SYS_nice)
  751.   syscall_table[SYS_nice] = "nice";
  752. #endif
  753. #if defined (SYS_statfs)
  754.   syscall_table[SYS_statfs] = "statfs";
  755. #endif
  756. #if defined (SYS_sync)
  757.   syscall_table[SYS_sync] = "sync";
  758. #endif
  759. #if defined (SYS_kill)
  760.   syscall_table[SYS_kill] = "kill";
  761. #endif
  762. #if defined (SYS_fstatfs)
  763.   syscall_table[SYS_fstatfs] = "fstatfs";
  764. #endif
  765. #if defined (SYS_pgrpsys)
  766.   syscall_table[SYS_pgrpsys] = "pgrpsys";
  767. #endif
  768. #if defined (SYS_xenix)
  769.   syscall_table[SYS_xenix] = "xenix";
  770. #endif
  771. #if defined (SYS_dup)
  772.   syscall_table[SYS_dup] = "dup";
  773. #endif
  774. #if defined (SYS_pipe)
  775.   syscall_table[SYS_pipe] = "pipe";
  776. #endif
  777. #if defined (SYS_times)
  778.   syscall_table[SYS_times] = "times";
  779. #endif
  780. #if defined (SYS_profil)
  781.   syscall_table[SYS_profil] = "profil";
  782. #endif
  783. #if defined (SYS_plock)
  784.   syscall_table[SYS_plock] = "plock";
  785. #endif
  786. #if defined (SYS_setgid)
  787.   syscall_table[SYS_setgid] = "setgid";
  788. #endif
  789. #if defined (SYS_getgid)
  790.   syscall_table[SYS_getgid] = "getgid";
  791. #endif
  792. #if defined (SYS_signal)
  793.   syscall_table[SYS_signal] = "signal";
  794. #endif
  795. #if defined (SYS_msgsys)
  796.   syscall_table[SYS_msgsys] = "msgsys";
  797. #endif
  798. #if defined (SYS_sys3b)
  799.   syscall_table[SYS_sys3b] = "sys3b";
  800. #endif
  801. #if defined (SYS_acct)
  802.   syscall_table[SYS_acct] = "acct";
  803. #endif
  804. #if defined (SYS_shmsys)
  805.   syscall_table[SYS_shmsys] = "shmsys";
  806. #endif
  807. #if defined (SYS_semsys)
  808.   syscall_table[SYS_semsys] = "semsys";
  809. #endif
  810. #if defined (SYS_ioctl)
  811.   syscall_table[SYS_ioctl] = "ioctl";
  812. #endif
  813. #if defined (SYS_uadmin)
  814.   syscall_table[SYS_uadmin] = "uadmin";
  815. #endif
  816. #if defined (SYS_utssys)
  817.   syscall_table[SYS_utssys] = "utssys";
  818. #endif
  819. #if defined (SYS_fsync)
  820.   syscall_table[SYS_fsync] = "fsync";
  821. #endif
  822. #if defined (SYS_umask)
  823.   syscall_table[SYS_umask] = "umask";
  824. #endif
  825. #if defined (SYS_chroot)
  826.   syscall_table[SYS_chroot] = "chroot";
  827. #endif
  828. #if defined (SYS_fcntl)
  829.   syscall_table[SYS_fcntl] = "fcntl";
  830. #endif
  831. #if defined (SYS_ulimit)
  832.   syscall_table[SYS_ulimit] = "ulimit";
  833. #endif
  834. #if defined (SYS_rfsys)
  835.   syscall_table[SYS_rfsys] = "rfsys";
  836. #endif
  837. #if defined (SYS_rmdir)
  838.   syscall_table[SYS_rmdir] = "rmdir";
  839. #endif
  840. #if defined (SYS_mkdir)
  841.   syscall_table[SYS_mkdir] = "mkdir";
  842. #endif
  843. #if defined (SYS_getdents)
  844.   syscall_table[SYS_getdents] = "getdents";
  845. #endif
  846. #if defined (SYS_sysfs)
  847.   syscall_table[SYS_sysfs] = "sysfs";
  848. #endif
  849. #if defined (SYS_getmsg)
  850.   syscall_table[SYS_getmsg] = "getmsg";
  851. #endif
  852. #if defined (SYS_putmsg)
  853.   syscall_table[SYS_putmsg] = "putmsg";
  854. #endif
  855. #if defined (SYS_poll)
  856.   syscall_table[SYS_poll] = "poll";
  857. #endif
  858. #if defined (SYS_lstat)
  859.   syscall_table[SYS_lstat] = "lstat";
  860. #endif
  861. #if defined (SYS_symlink)
  862.   syscall_table[SYS_symlink] = "symlink";
  863. #endif
  864. #if defined (SYS_readlink)
  865.   syscall_table[SYS_readlink] = "readlink";
  866. #endif
  867. #if defined (SYS_setgroups)
  868.   syscall_table[SYS_setgroups] = "setgroups";
  869. #endif
  870. #if defined (SYS_getgroups)
  871.   syscall_table[SYS_getgroups] = "getgroups";
  872. #endif
  873. #if defined (SYS_fchmod)
  874.   syscall_table[SYS_fchmod] = "fchmod";
  875. #endif
  876. #if defined (SYS_fchown)
  877.   syscall_table[SYS_fchown] = "fchown";
  878. #endif
  879. #if defined (SYS_sigprocmask)
  880.   syscall_table[SYS_sigprocmask] = "sigprocmask";
  881. #endif
  882. #if defined (SYS_sigsuspend)
  883.   syscall_table[SYS_sigsuspend] = "sigsuspend";
  884. #endif
  885. #if defined (SYS_sigaltstack)
  886.   syscall_table[SYS_sigaltstack] = "sigaltstack";
  887. #endif
  888. #if defined (SYS_sigaction)
  889.   syscall_table[SYS_sigaction] = "sigaction";
  890. #endif
  891. #if defined (SYS_sigpending)
  892.   syscall_table[SYS_sigpending] = "sigpending";
  893. #endif
  894. #if defined (SYS_context)
  895.   syscall_table[SYS_context] = "context";
  896. #endif
  897. #if defined (SYS_evsys)
  898.   syscall_table[SYS_evsys] = "evsys";
  899. #endif
  900. #if defined (SYS_evtrapret)
  901.   syscall_table[SYS_evtrapret] = "evtrapret";
  902. #endif
  903. #if defined (SYS_statvfs)
  904.   syscall_table[SYS_statvfs] = "statvfs";
  905. #endif
  906. #if defined (SYS_fstatvfs)
  907.   syscall_table[SYS_fstatvfs] = "fstatvfs";
  908. #endif
  909. #if defined (SYS_nfssys)
  910.   syscall_table[SYS_nfssys] = "nfssys";
  911. #endif
  912. #if defined (SYS_waitsys)
  913.   syscall_table[SYS_waitsys] = "waitsys";
  914. #endif
  915. #if defined (SYS_sigsendsys)
  916.   syscall_table[SYS_sigsendsys] = "sigsendsys";
  917. #endif
  918. #if defined (SYS_hrtsys)
  919.   syscall_table[SYS_hrtsys] = "hrtsys";
  920. #endif
  921. #if defined (SYS_acancel)
  922.   syscall_table[SYS_acancel] = "acancel";
  923. #endif
  924. #if defined (SYS_async)
  925.   syscall_table[SYS_async] = "async";
  926. #endif
  927. #if defined (SYS_priocntlsys)
  928.   syscall_table[SYS_priocntlsys] = "priocntlsys";
  929. #endif
  930. #if defined (SYS_pathconf)
  931.   syscall_table[SYS_pathconf] = "pathconf";
  932. #endif
  933. #if defined (SYS_mincore)
  934.   syscall_table[SYS_mincore] = "mincore";
  935. #endif
  936. #if defined (SYS_mmap)
  937.   syscall_table[SYS_mmap] = "mmap";
  938. #endif
  939. #if defined (SYS_mprotect)
  940.   syscall_table[SYS_mprotect] = "mprotect";
  941. #endif
  942. #if defined (SYS_munmap)
  943.   syscall_table[SYS_munmap] = "munmap";
  944. #endif
  945. #if defined (SYS_fpathconf)
  946.   syscall_table[SYS_fpathconf] = "fpathconf";
  947. #endif
  948. #if defined (SYS_vfork)
  949.   syscall_table[SYS_vfork] = "vfork";
  950. #endif
  951. #if defined (SYS_fchdir)
  952.   syscall_table[SYS_fchdir] = "fchdir";
  953. #endif
  954. #if defined (SYS_readv)
  955.   syscall_table[SYS_readv] = "readv";
  956. #endif
  957. #if defined (SYS_writev)
  958.   syscall_table[SYS_writev] = "writev";
  959. #endif
  960. #if defined (SYS_xstat)
  961.   syscall_table[SYS_xstat] = "xstat";
  962. #endif
  963. #if defined (SYS_lxstat)
  964.   syscall_table[SYS_lxstat] = "lxstat";
  965. #endif
  966. #if defined (SYS_fxstat)
  967.   syscall_table[SYS_fxstat] = "fxstat";
  968. #endif
  969. #if defined (SYS_xmknod)
  970.   syscall_table[SYS_xmknod] = "xmknod";
  971. #endif
  972. #if defined (SYS_clocal)
  973.   syscall_table[SYS_clocal] = "clocal";
  974. #endif
  975. #if defined (SYS_setrlimit)
  976.   syscall_table[SYS_setrlimit] = "setrlimit";
  977. #endif
  978. #if defined (SYS_getrlimit)
  979.   syscall_table[SYS_getrlimit] = "getrlimit";
  980. #endif
  981. #if defined (SYS_lchown)
  982.   syscall_table[SYS_lchown] = "lchown";
  983. #endif
  984. #if defined (SYS_memcntl)
  985.   syscall_table[SYS_memcntl] = "memcntl";
  986. #endif
  987. #if defined (SYS_getpmsg)
  988.   syscall_table[SYS_getpmsg] = "getpmsg";
  989. #endif
  990. #if defined (SYS_putpmsg)
  991.   syscall_table[SYS_putpmsg] = "putpmsg";
  992. #endif
  993. #if defined (SYS_rename)
  994.   syscall_table[SYS_rename] = "rename";
  995. #endif
  996. #if defined (SYS_uname)
  997.   syscall_table[SYS_uname] = "uname";
  998. #endif
  999. #if defined (SYS_setegid)
  1000.   syscall_table[SYS_setegid] = "setegid";
  1001. #endif
  1002. #if defined (SYS_sysconfig)
  1003.   syscall_table[SYS_sysconfig] = "sysconfig";
  1004. #endif
  1005. #if defined (SYS_adjtime)
  1006.   syscall_table[SYS_adjtime] = "adjtime";
  1007. #endif
  1008. #if defined (SYS_systeminfo)
  1009.   syscall_table[SYS_systeminfo] = "systeminfo";
  1010. #endif
  1011. #if defined (SYS_seteuid)
  1012.   syscall_table[SYS_seteuid] = "seteuid";
  1013. #endif
  1014. }
  1015.  
  1016. /*
  1017.  
  1018. GLOBAL FUNCTION
  1019.  
  1020.     ptrace -- override library version to force errors for /proc version
  1021.  
  1022. SYNOPSIS
  1023.  
  1024.     int ptrace (int request, int pid, PTRACE_ARG3_TYPE arg3, int arg4)
  1025.  
  1026. DESCRIPTION
  1027.  
  1028.     When gdb is configured to use /proc, it should not be calling
  1029.     or otherwise attempting to use ptrace.  In order to catch errors
  1030.     where use of /proc is configured, but some routine is still calling
  1031.     ptrace, we provide a local version of a function with that name
  1032.     that does nothing but issue an error message.
  1033. */
  1034.  
  1035. int
  1036. ptrace (request, pid, arg3, arg4)
  1037.      int request;
  1038.      int pid;
  1039.      PTRACE_ARG3_TYPE arg3;
  1040.      int arg4;
  1041. {
  1042.   error ("internal error - there is a call to ptrace() somewhere");
  1043.   /*NOTREACHED*/
  1044. }
  1045.  
  1046. /*
  1047.  
  1048. LOCAL FUNCTION
  1049.  
  1050.     procfs_kill_inferior - kill any currently inferior
  1051.  
  1052. SYNOPSIS
  1053.  
  1054.     void procfs_kill_inferior (void)
  1055.  
  1056. DESCRIPTION
  1057.  
  1058.     Kill any current inferior.
  1059.  
  1060. NOTES
  1061.  
  1062.     Kills even attached inferiors.  Presumably the user has already
  1063.     been prompted that the inferior is an attached one rather than
  1064.     one started by gdb.  (FIXME?)
  1065.  
  1066. */
  1067.  
  1068. static void
  1069. procfs_kill_inferior ()
  1070. {
  1071.   if (inferior_pid != 0)
  1072.     {
  1073.       unconditionally_kill_inferior ();
  1074.       target_mourn_inferior ();
  1075.     }
  1076. }
  1077.  
  1078. /*
  1079.  
  1080. LOCAL FUNCTION
  1081.  
  1082.     unconditionally_kill_inferior - terminate the inferior
  1083.  
  1084. SYNOPSIS
  1085.  
  1086.     static void unconditionally_kill_inferior (void)
  1087.  
  1088. DESCRIPTION
  1089.  
  1090.     Kill the current inferior.  Should not be called until it
  1091.     is at least tested that there is an inferior.
  1092.  
  1093. NOTE
  1094.  
  1095.     A possibly useful enhancement would be to first try sending
  1096.     the inferior a terminate signal, politely asking it to commit
  1097.     suicide, before we murder it.
  1098.  
  1099. */
  1100.  
  1101. static void
  1102. unconditionally_kill_inferior ()
  1103. {
  1104.   int signo;
  1105.   
  1106.   signo = SIGKILL;
  1107.   ioctl (pi.fd, PIOCKILL, &signo);
  1108.   close_proc_file (&pi);
  1109.   wait ((int *) 0);
  1110. }
  1111.  
  1112. /*
  1113.  
  1114. LOCAL FUNCTION
  1115.  
  1116.     procfs_xfer_memory -- copy data to or from inferior memory space
  1117.  
  1118. SYNOPSIS
  1119.  
  1120.     int procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
  1121.         int dowrite, struct target_ops target)
  1122.  
  1123. DESCRIPTION
  1124.  
  1125.     Copy LEN bytes to/from inferior's memory starting at MEMADDR
  1126.     from/to debugger memory starting at MYADDR.  Copy from inferior
  1127.     if DOWRITE is zero or to inferior if DOWRITE is nonzero.
  1128.   
  1129.     Returns the length copied, which is either the LEN argument or
  1130.     zero.  This xfer function does not do partial moves, since procfs_ops
  1131.     doesn't allow memory operations to cross below us in the target stack
  1132.     anyway.
  1133.  
  1134. NOTES
  1135.  
  1136.     The /proc interface makes this an almost trivial task.
  1137.  */
  1138.  
  1139. static int
  1140. procfs_xfer_memory (memaddr, myaddr, len, dowrite, target)
  1141.      CORE_ADDR memaddr;
  1142.      char *myaddr;
  1143.      int len;
  1144.      int dowrite;
  1145.      struct target_ops *target; /* ignored */
  1146. {
  1147.   int nbytes = 0;
  1148.  
  1149.   if (lseek (pi.fd, (off_t) memaddr, 0) == (off_t) memaddr)
  1150.     {
  1151.       if (dowrite)
  1152.     {
  1153.       nbytes = write (pi.fd, myaddr, len);
  1154.     }
  1155.       else
  1156.     {
  1157.       nbytes = read (pi.fd, myaddr, len);
  1158.     }
  1159.       if (nbytes < 0)
  1160.     {
  1161.       nbytes = 0;
  1162.     }
  1163.     }
  1164.   return (nbytes);
  1165. }
  1166.  
  1167. /*
  1168.  
  1169. LOCAL FUNCTION
  1170.  
  1171.     procfs_store_registers -- copy register values back to inferior
  1172.  
  1173. SYNOPSIS
  1174.  
  1175.     void procfs_store_registers (int regno)
  1176.  
  1177. DESCRIPTION
  1178.  
  1179.     Store our current register values back into the inferior.  If
  1180.     REGNO is -1 then store all the register, otherwise store just
  1181.     the value specified by REGNO.
  1182.  
  1183. NOTES
  1184.  
  1185.     If we are storing only a single register, we first have to get all
  1186.     the current values from the process, overwrite the desired register
  1187.     in the gregset with the one we want from gdb's registers, and then
  1188.     send the whole set back to the process.  For writing all the
  1189.     registers, all we have to do is generate the gregset and send it to
  1190.     the process.
  1191.  
  1192.     Also note that the process has to be stopped on an event of interest
  1193.     for this to work, which basically means that it has to have been
  1194.     run under the control of one of the other /proc ioctl calls and not
  1195.     ptrace.  Since we don't use ptrace anyway, we don't worry about this
  1196.     fine point, but it is worth noting for future reference.
  1197.  
  1198.     Gdb is confused about what this function is supposed to return.
  1199.     Some versions return a value, others return nothing.  Some are
  1200.     declared to return a value and actually return nothing.  Gdb ignores
  1201.     anything returned.  (FIXME)
  1202.  
  1203.  */
  1204.  
  1205. static void
  1206. procfs_store_registers (regno)
  1207.      int regno;
  1208. {
  1209.   if (regno != -1)
  1210.     {
  1211.       ioctl (pi.fd, PIOCGREG, &pi.gregset);
  1212.     }
  1213.   fill_gregset (&pi.gregset, regno);
  1214.   ioctl (pi.fd, PIOCSREG, &pi.gregset);
  1215.  
  1216. #if defined (FP0_REGNUM)
  1217.  
  1218.   /* Now repeat everything using the floating point register set, if the
  1219.      target has floating point hardware. Since we ignore the returned value,
  1220.      we'll never know whether it worked or not anyway. */
  1221.  
  1222.   if (regno != -1)
  1223.     {
  1224.       ioctl (pi.fd, PIOCGFPREG, &pi.fpregset);
  1225.     }
  1226.   fill_fpregset (&pi.fpregset, regno);
  1227.   ioctl (pi.fd, PIOCSFPREG, &pi.fpregset);
  1228.  
  1229. #endif    /* FP0_REGNUM */
  1230.  
  1231. }
  1232.  
  1233. /*
  1234.  
  1235. LOCAL FUNCTION
  1236.  
  1237.     procfs_init_inferior - initialize access to a /proc entry
  1238.  
  1239. SYNOPSIS
  1240.  
  1241.     void procfs_init_inferior (int pid)
  1242.  
  1243. DESCRIPTION
  1244.  
  1245.     When gdb starts an inferior, this function is called in the parent
  1246.     process immediately after the fork.  It waits for the child to stop
  1247.     on the return from the exec system call (the child itself takes care
  1248.     of ensuring that this is set up), then sets up the set of signals
  1249.     and faults that are to be traced.
  1250.  
  1251. NOTES
  1252.  
  1253.     If proc_init_failed ever gets called, control returns to the command
  1254.     processing loop via the standard error handling code.
  1255.  
  1256.  */
  1257.  
  1258. static void
  1259. procfs_init_inferior (pid)
  1260.      int pid;
  1261. {
  1262.  
  1263.   push_target (&procfs_ops);
  1264.  
  1265.   if (!open_proc_file (pid, &pi, O_RDWR))
  1266.     {
  1267.       proc_init_failed ("can't open process file");
  1268.     }
  1269.   else
  1270.     {
  1271.       memset ((char *) &pi.prrun, 0, sizeof (pi.prrun));
  1272.       prfillset (&pi.prrun.pr_trace);
  1273.       procfs_notice_signals ();
  1274.       prfillset (&pi.prrun.pr_fault);
  1275.       prdelset (&pi.prrun.pr_fault, FLTPAGE);
  1276.       if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
  1277.     {
  1278.       proc_init_failed ("PIOCWSTOP failed");
  1279.     }
  1280.       else if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault) < 0)
  1281.     {
  1282.       proc_init_failed ("PIOCSFAULT failed");
  1283.     }
  1284.     }
  1285. }
  1286.  
  1287. /*
  1288.  
  1289. GLOBAL FUNCTION
  1290.  
  1291.     procfs_notice_signals
  1292.  
  1293. SYNOPSIS
  1294.  
  1295.     static void procfs_notice_signals (void);
  1296.  
  1297. DESCRIPTION
  1298.  
  1299.     When the user changes the state of gdb's signal handling via the
  1300.     "handle" command, this function gets called to see if any change
  1301.     in the /proc interface is required.  It is also called internally
  1302.     by other /proc interface functions to initialize the state of
  1303.     the traced signal set.
  1304.  
  1305.     One thing it does is that signals for which the state is "nostop",
  1306.     "noprint", and "pass", have their trace bits reset in the pr_trace
  1307.     field, so that they are no longer traced.  This allows them to be
  1308.     delivered directly to the inferior without the debugger ever being
  1309.     involved.
  1310.  */
  1311.  
  1312. static void
  1313. procfs_notice_signals ()
  1314. {
  1315.   int signo;
  1316.  
  1317.   if (pi.valid)
  1318.     {
  1319.       for (signo = 0; signo < NSIG; signo++)
  1320.     {
  1321.       if (signal_stop_state (signo) == 0 &&
  1322.           signal_print_state (signo) == 0 &&
  1323.           signal_pass_state (signo) == 1)
  1324.         {
  1325.           prdelset (&pi.prrun.pr_trace, signo);
  1326.         }
  1327.       else
  1328.         {
  1329.           praddset (&pi.prrun.pr_trace, signo);
  1330.         }
  1331.     }
  1332.       if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace))
  1333.     {
  1334.       print_sys_errmsg ("PIOCSTRACE failed", errno);
  1335.     }
  1336.     }
  1337. }
  1338.  
  1339. /*
  1340.  
  1341. LOCAL FUNCTION
  1342.  
  1343.     proc_set_exec_trap -- arrange for exec'd child to halt at startup
  1344.  
  1345. SYNOPSIS
  1346.  
  1347.     void proc_set_exec_trap (void)
  1348.  
  1349. DESCRIPTION
  1350.  
  1351.     This function is called in the child process when starting up
  1352.     an inferior, prior to doing the exec of the actual inferior.
  1353.     It sets the child process's exitset to make exit from the exec
  1354.     system call an event of interest to stop on, and then simply
  1355.     returns.  The child does the exec, the system call returns, and
  1356.     the child stops at the first instruction, ready for the gdb
  1357.     parent process to take control of it.
  1358.  
  1359. NOTE
  1360.  
  1361.     We need to use all local variables since the child may be sharing
  1362.     it's data space with the parent, if vfork was used rather than
  1363.     fork.
  1364.  
  1365.     Also note that we want to turn off the inherit-on-fork flag in
  1366.     the child process so that any grand-children start with all
  1367.     tracing flags cleared.
  1368.  */
  1369.  
  1370. static void
  1371. proc_set_exec_trap ()
  1372. {
  1373.   sysset_t exitset;
  1374.   auto char procname[32];
  1375.   int fd;
  1376.   
  1377.   sprintf (procname, PROC_NAME_FMT, getpid ());
  1378.   if ((fd = open (procname, O_RDWR)) < 0)
  1379.     {
  1380.       perror (procname);
  1381.       fflush (stderr);
  1382.       _exit (127);
  1383.     }
  1384.   premptyset (&exitset);
  1385.  
  1386.   /* GW: Rationale...
  1387.      Not all systems with /proc have all the exec* syscalls with the same
  1388.      names.  On the SGI, for example, there is no SYS_exec, but there
  1389.      *is* a SYS_execv.  So, we try to account for that. */
  1390.  
  1391. #ifdef SYS_exec
  1392.   praddset (&exitset, SYS_exec);
  1393. #endif
  1394. #ifdef SYS_execve
  1395.   praddset (&exitset, SYS_execve);
  1396. #endif
  1397. #ifdef SYS_execv
  1398.   praddset(&exitset, SYS_execv);
  1399. #endif
  1400.  
  1401.   if (ioctl (fd, PIOCSEXIT, &exitset) < 0)
  1402.     {
  1403.       perror (procname);
  1404.       fflush (stderr);
  1405.       _exit (127);
  1406.     }
  1407.  
  1408.   /* Turn off inherit-on-fork flag so that all grand-children of gdb
  1409.      start with tracing flags cleared. */
  1410.  
  1411. #if defined (PIOCRESET)    /* New method */
  1412.   {
  1413.       long pr_flags;
  1414.       pr_flags = PR_FORK;
  1415.       ioctl (fd, PIOCRESET, &pr_flags);
  1416.   }
  1417. #else
  1418. #if defined (PIOCRFORK)    /* Original method */
  1419.   ioctl (fd, PIOCRFORK, NULL);
  1420. #endif
  1421. #endif
  1422.  
  1423.   /* Turn on run-on-last-close flag so that this process will not hang
  1424.      if GDB goes away for some reason.  */
  1425.  
  1426. #if defined (PIOCSET)    /* New method */
  1427.   {
  1428.       long pr_flags;
  1429.       pr_flags = PR_RLC;
  1430.       (void) ioctl (fd, PIOCSET, &pr_flags);
  1431.   }
  1432. #else
  1433. #if defined (PIOCSRLC)    /* Original method */
  1434.   (void) ioctl (fd, PIOCSRLC, 0);
  1435. #endif
  1436. #endif
  1437. }
  1438.  
  1439. /*
  1440.  
  1441. GLOBAL FUNCTION
  1442.  
  1443.     proc_iterate_over_mappings -- call function for every mapped space
  1444.  
  1445. SYNOPSIS
  1446.  
  1447.     int proc_iterate_over_mappings (int (*func)())
  1448.  
  1449. DESCRIPTION
  1450.  
  1451.     Given a pointer to a function, call that function for every
  1452.     mapped address space, passing it an open file descriptor for
  1453.     the file corresponding to that mapped address space (if any)
  1454.     and the base address of the mapped space.  Quit when we hit
  1455.     the end of the mappings or the function returns nonzero.
  1456.  */
  1457.  
  1458. int
  1459. proc_iterate_over_mappings (func)
  1460.      int (*func) PARAMS ((int, CORE_ADDR));
  1461. {
  1462.   int nmap;
  1463.   int fd;
  1464.   int funcstat = 0;
  1465.   struct prmap *prmaps;
  1466.   struct prmap *prmap;
  1467.  
  1468.   if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
  1469.     {
  1470.       prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
  1471.       if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
  1472.     {
  1473.       for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap)
  1474.         {
  1475.           fd = proc_address_to_fd ((CORE_ADDR) prmap -> pr_vaddr, 0);
  1476.           funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr);
  1477.           close (fd);
  1478.         }
  1479.     }
  1480.     }
  1481.   return (funcstat);
  1482. }
  1483.  
  1484. #if 0    /* Currently unused */
  1485. /*
  1486.  
  1487. GLOBAL FUNCTION
  1488.  
  1489.     proc_base_address -- find base address for segment containing address
  1490.  
  1491. SYNOPSIS
  1492.  
  1493.     CORE_ADDR proc_base_address (CORE_ADDR addr)
  1494.  
  1495. DESCRIPTION
  1496.  
  1497.     Given an address of a location in the inferior, find and return
  1498.     the base address of the mapped segment containing that address.
  1499.  
  1500.     This is used for example, by the shared library support code,
  1501.     where we have the pc value for some location in the shared library
  1502.     where we are stopped, and need to know the base address of the
  1503.     segment containing that address.
  1504. */
  1505.  
  1506. CORE_ADDR
  1507. proc_base_address (addr)
  1508.      CORE_ADDR addr;
  1509. {
  1510.   int nmap;
  1511.   struct prmap *prmaps;
  1512.   struct prmap *prmap;
  1513.   CORE_ADDR baseaddr = 0;
  1514.  
  1515.   if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
  1516.     {
  1517.       prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
  1518.       if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
  1519.     {
  1520.       for (prmap = prmaps; prmap -> pr_size; ++prmap)
  1521.         {
  1522.           if ((prmap -> pr_vaddr <= (caddr_t) addr) &&
  1523.           (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr))
  1524.         {
  1525.           baseaddr = (CORE_ADDR) prmap -> pr_vaddr;
  1526.           break;
  1527.         }
  1528.         }
  1529.     }
  1530.     }
  1531.   return (baseaddr);
  1532. }
  1533.  
  1534. #endif    /* 0 */
  1535.  
  1536. /*
  1537.  
  1538. LOCAL FUNCTION
  1539.  
  1540.     proc_address_to_fd -- return open fd for file mapped to address
  1541.  
  1542. SYNOPSIS
  1543.  
  1544.     int proc_address_to_fd (CORE_ADDR addr, complain)
  1545.  
  1546. DESCRIPTION
  1547.  
  1548.     Given an address in the current inferior's address space, use the
  1549.     /proc interface to find an open file descriptor for the file that
  1550.     this address was mapped in from.  Return -1 if there is no current
  1551.     inferior.  Print a warning message if there is an inferior but
  1552.     the address corresponds to no file (IE a bogus address).
  1553.  
  1554. */
  1555.  
  1556. static int
  1557. proc_address_to_fd (addr, complain)
  1558.      CORE_ADDR addr;
  1559.      int complain;
  1560. {
  1561.   int fd = -1;
  1562.  
  1563.   if (pi.valid)
  1564.     {
  1565.       if ((fd = ioctl (pi.fd, PIOCOPENM, (caddr_t *) &addr)) < 0)
  1566.     {
  1567.       if (complain)
  1568.         {
  1569.           print_sys_errmsg (pi.pathname, errno);
  1570.           warning ("can't find mapped file for address 0x%x", addr);
  1571.         }
  1572.     }
  1573.     }
  1574.   return (fd);
  1575. }
  1576.  
  1577.  
  1578. /* Attach to process PID, then initialize for debugging it
  1579.    and wait for the trace-trap that results from attaching.  */
  1580.  
  1581. static void
  1582. procfs_attach (args, from_tty)
  1583.      char *args;
  1584.      int from_tty;
  1585. {
  1586.   char *exec_file;
  1587.   int pid;
  1588.  
  1589.   if (!args)
  1590.     error_no_arg ("process-id to attach");
  1591.  
  1592.   pid = atoi (args);
  1593.  
  1594.   if (pid == getpid())        /* Trying to masturbate? */
  1595.     error ("I refuse to debug myself!");
  1596.  
  1597.   if (from_tty)
  1598.     {
  1599.       exec_file = (char *) get_exec_file (0);
  1600.  
  1601.       if (exec_file)
  1602.     printf ("Attaching program `%s', pid %d\n", exec_file, pid);
  1603.       else
  1604.     printf ("Attaching pid %d\n", pid);
  1605.  
  1606.       fflush (stdout);
  1607.     }
  1608.  
  1609.   do_attach (pid);
  1610.   inferior_pid = pid;
  1611.   push_target (&procfs_ops);
  1612. }
  1613.  
  1614.  
  1615. /* Take a program previously attached to and detaches it.
  1616.    The program resumes execution and will no longer stop
  1617.    on signals, etc.  We'd better not have left any breakpoints
  1618.    in the program or it'll die when it hits one.  For this
  1619.    to work, it may be necessary for the process to have been
  1620.    previously attached.  It *might* work if the program was
  1621.    started via the normal ptrace (PTRACE_TRACEME).  */
  1622.  
  1623. static void
  1624. procfs_detach (args, from_tty)
  1625.      char *args;
  1626.      int from_tty;
  1627. {
  1628.   int siggnal = 0;
  1629.  
  1630.   if (from_tty)
  1631.     {
  1632.       char *exec_file = get_exec_file (0);
  1633.       if (exec_file == 0)
  1634.     exec_file = "";
  1635.       printf ("Detaching program: %s pid %d\n",
  1636.           exec_file, inferior_pid);
  1637.       fflush (stdout);
  1638.     }
  1639.   if (args)
  1640.     siggnal = atoi (args);
  1641.   
  1642.   do_detach (siggnal);
  1643.   inferior_pid = 0;
  1644.   unpush_target (&procfs_ops);        /* Pop out of handling an inferior */
  1645. }
  1646.  
  1647. /* Get ready to modify the registers array.  On machines which store
  1648.    individual registers, this doesn't need to do anything.  On machines
  1649.    which store all the registers in one fell swoop, this makes sure
  1650.    that registers contains all the registers from the program being
  1651.    debugged.  */
  1652.  
  1653. static void
  1654. procfs_prepare_to_store ()
  1655. {
  1656. #ifdef CHILD_PREPARE_TO_STORE
  1657.   CHILD_PREPARE_TO_STORE ();
  1658. #endif
  1659. }
  1660.  
  1661. /* Print status information about what we're accessing.  */
  1662.  
  1663. static void
  1664. procfs_files_info (ignore)
  1665.      struct target_ops *ignore;
  1666. {
  1667.   printf ("\tUsing the running image of %s process %d via /proc.\n",
  1668.       attach_flag? "attached": "child", inferior_pid);
  1669. }
  1670.  
  1671. /* ARGSUSED */
  1672. static void
  1673. procfs_open (arg, from_tty)
  1674.      char *arg;
  1675.      int from_tty;
  1676. {
  1677.   error ("Use the \"run\" command to start a Unix child process.");
  1678. }
  1679.  
  1680. /*
  1681.  
  1682. LOCAL FUNCTION
  1683.  
  1684.     do_attach -- attach to an already existing process
  1685.  
  1686. SYNOPSIS
  1687.  
  1688.     int do_attach (int pid)
  1689.  
  1690. DESCRIPTION
  1691.  
  1692.     Attach to an already existing process with the specified process
  1693.     id.  If the process is not already stopped, query whether to
  1694.     stop it or not.
  1695.  
  1696. NOTES
  1697.  
  1698.     The option of stopping at attach time is specific to the /proc
  1699.     versions of gdb.  Versions using ptrace force the attachee
  1700.     to stop.  (I have changed this version to do so, too.  All you
  1701.     have to do is "continue" to make it go on. -- gnu@cygnus.com)
  1702.  
  1703. */
  1704.  
  1705. static int
  1706. do_attach (pid)
  1707.      int pid;
  1708. {
  1709.   int result;
  1710.  
  1711.   if (!open_proc_file (pid, &pi, O_RDWR))
  1712.     {
  1713.       perror_with_name (pi.pathname);
  1714.       /* NOTREACHED */
  1715.     }
  1716.   
  1717.   /*  Get current status of process and if it is not already stopped,
  1718.       then stop it.  Remember whether or not it was stopped when we first
  1719.       examined it. */
  1720.   
  1721.   if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
  1722.     {
  1723.       print_sys_errmsg (pi.pathname, errno);
  1724.       close_proc_file (&pi);
  1725.       error ("PIOCSTATUS failed");
  1726.     }
  1727.   if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
  1728.     {
  1729.       pi.was_stopped = 1;
  1730.     }
  1731.   else
  1732.     {
  1733.       pi.was_stopped = 0;
  1734.       if (1 || query ("Process is currently running, stop it? "))
  1735.     {
  1736.       /* Make it run again when we close it.  */
  1737. #if defined (PIOCSET)    /* New method */
  1738.       {
  1739.           long pr_flags;
  1740.           pr_flags = PR_RLC;
  1741.           result = ioctl (pi.fd, PIOCSET, &pr_flags);
  1742.       }
  1743. #else
  1744. #if defined (PIOCSRLC)    /* Original method */
  1745.       result = ioctl (pi.fd, PIOCSRLC, 0);
  1746. #endif
  1747. #endif
  1748.       if (result < 0)
  1749.         {
  1750.           print_sys_errmsg (pi.pathname, errno);
  1751.           close_proc_file (&pi);
  1752.           error ("PIOCSRLC or PIOCSET failed");
  1753.         }
  1754.       if (ioctl (pi.fd, PIOCSTOP, &pi.prstatus) < 0)
  1755.         {
  1756.           print_sys_errmsg (pi.pathname, errno);
  1757.           close_proc_file (&pi);
  1758.           error ("PIOCSTOP failed");
  1759.         }
  1760.       pi.nopass_next_sigstop = 1;
  1761.     }
  1762.       else
  1763.     {
  1764.       printf ("Ok, gdb will wait for process %u to stop.\n", pid);
  1765.     }
  1766.     }
  1767.  
  1768.   /*  Remember some things about the inferior that we will, or might, change
  1769.       so that we can restore them when we detach. */
  1770.   
  1771.   ioctl (pi.fd, PIOCGTRACE, &pi.saved_trace);
  1772.   ioctl (pi.fd, PIOCGHOLD, &pi.saved_sighold);
  1773.   ioctl (pi.fd, PIOCGFAULT, &pi.saved_fltset);
  1774.   ioctl (pi.fd, PIOCGENTRY, &pi.saved_entryset);
  1775.   ioctl (pi.fd, PIOCGEXIT, &pi.saved_exitset);
  1776.   
  1777.   /* Set up trace and fault sets, as gdb expects them. */
  1778.   
  1779.   memset (&pi.prrun, 0, sizeof (pi.prrun));
  1780.   prfillset (&pi.prrun.pr_trace);
  1781.   procfs_notice_signals ();
  1782.   prfillset (&pi.prrun.pr_fault);
  1783.   prdelset (&pi.prrun.pr_fault, FLTPAGE);
  1784.   if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault))
  1785.     {
  1786.       print_sys_errmsg ("PIOCSFAULT failed", errno);
  1787.     }
  1788.   if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace))
  1789.     {
  1790.       print_sys_errmsg ("PIOCSTRACE failed", errno);
  1791.     }
  1792.   attach_flag = 1;
  1793.   return (pid);
  1794. }
  1795.  
  1796. /*
  1797.  
  1798. LOCAL FUNCTION
  1799.  
  1800.     do_detach -- detach from an attached-to process
  1801.  
  1802. SYNOPSIS
  1803.  
  1804.     void do_detach (int signal)
  1805.  
  1806. DESCRIPTION
  1807.  
  1808.     Detach from the current attachee.
  1809.  
  1810.     If signal is non-zero, the attachee is started running again and sent
  1811.     the specified signal.
  1812.  
  1813.     If signal is zero and the attachee was not already stopped when we
  1814.     attached to it, then we make it runnable again when we detach.
  1815.  
  1816.     Otherwise, we query whether or not to make the attachee runnable
  1817.     again, since we may simply want to leave it in the state it was in
  1818.     when we attached.
  1819.  
  1820.     We report any problems, but do not consider them errors, since we
  1821.     MUST detach even if some things don't seem to go right.  This may not
  1822.     be the ideal situation.  (FIXME).
  1823.  */
  1824.  
  1825. static void
  1826. do_detach (signal)
  1827.      int signal;
  1828. {
  1829.   int result;
  1830.  
  1831.   if (signal)
  1832.     {
  1833.       set_proc_siginfo (&pi, signal);
  1834.     }
  1835.   if (ioctl (pi.fd, PIOCSEXIT, &pi.saved_exitset) < 0)
  1836.     {
  1837.       print_sys_errmsg (pi.pathname, errno);
  1838.       printf ("PIOCSEXIT failed.\n");
  1839.     }
  1840.   if (ioctl (pi.fd, PIOCSENTRY, &pi.saved_entryset) < 0)
  1841.     {
  1842.       print_sys_errmsg (pi.pathname, errno);
  1843.       printf ("PIOCSENTRY failed.\n");
  1844.     }
  1845.   if (ioctl (pi.fd, PIOCSTRACE, &pi.saved_trace) < 0)
  1846.     {
  1847.       print_sys_errmsg (pi.pathname, errno);
  1848.       printf ("PIOCSTRACE failed.\n");
  1849.     }
  1850.   if (ioctl (pi.fd, PIOCSHOLD, &pi.saved_sighold) < 0)
  1851.     {
  1852.       print_sys_errmsg (pi.pathname, errno);
  1853.       printf ("PIOSCHOLD failed.\n");
  1854.     }
  1855.   if (ioctl (pi.fd, PIOCSFAULT, &pi.saved_fltset) < 0)
  1856.     {
  1857.       print_sys_errmsg (pi.pathname, errno);
  1858.       printf ("PIOCSFAULT failed.\n");
  1859.     }
  1860.   if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
  1861.     {
  1862.       print_sys_errmsg (pi.pathname, errno);
  1863.       printf ("PIOCSTATUS failed.\n");
  1864.     }
  1865.   else
  1866.     {
  1867.       if (signal || (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
  1868.     {
  1869.       if (signal || !pi.was_stopped ||
  1870.           query ("Was stopped when attached, make it runnable again? "))
  1871.         {
  1872.           /* Clear any fault that might have stopped it.  */
  1873.           if (ioctl (pi.fd, PIOCCFAULT, 0))
  1874.           {
  1875.             print_sys_errmsg (pi.pathname, errno);
  1876.           printf ("PIOCCFAULT failed.\n");
  1877.           }
  1878.  
  1879.           /* Make it run again when we close it.  */
  1880. #if defined (PIOCSET)    /* New method */
  1881.           {
  1882.           long pr_flags;
  1883.           pr_flags = PR_RLC;
  1884.           result = ioctl (pi.fd, PIOCSET, &pr_flags);
  1885.           }
  1886. #else
  1887. #if defined (PIOCSRLC)    /* Original method */
  1888.           result = ioctl (pi.fd, PIOCSRLC, 0);
  1889. #endif
  1890. #endif
  1891.           if (result)
  1892.         {
  1893.           print_sys_errmsg (pi.pathname, errno);
  1894.           printf ("PIOCSRLC or PIOCSET failed.\n");
  1895.         }
  1896.         }
  1897.     }
  1898.     }
  1899.   close_proc_file (&pi);
  1900.   attach_flag = 0;
  1901. }
  1902.  
  1903. /*
  1904.  
  1905. LOCAL FUNCTION
  1906.  
  1907.     procfs_wait -- emulate wait() as much as possible
  1908.     Wait for child to do something.  Return pid of child, or -1 in case
  1909.     of error; store status through argument pointer STATUS.
  1910.  
  1911.  
  1912. SYNOPSIS
  1913.  
  1914.     int procfs_wait (int *statloc)
  1915.  
  1916. DESCRIPTION
  1917.  
  1918.     Try to emulate wait() as much as possible.  Not sure why we can't
  1919.     just use wait(), but it seems to have problems when applied to a
  1920.     process being controlled with the /proc interface.
  1921.  
  1922. NOTES
  1923.  
  1924.     We have a race problem here with no obvious solution.  We need to let
  1925.     the inferior run until it stops on an event of interest, which means
  1926.     that we need to use the PIOCWSTOP ioctl.  However, we cannot use this
  1927.     ioctl if the process is already stopped on something that is not an
  1928.     event of interest, or the call will hang indefinitely.  Thus we first
  1929.     use PIOCSTATUS to see if the process is not stopped.  If not, then we
  1930.     use PIOCWSTOP.  But during the window between the two, if the process
  1931.     stops for any reason that is not an event of interest (such as a job
  1932.     control signal) then gdb will hang.  One possible workaround is to set
  1933.     an alarm to wake up every minute of so and check to see if the process
  1934.     is still running, and if so, then reissue the PIOCWSTOP.  But this is
  1935.     a real kludge, so has not been implemented.  FIXME: investigate
  1936.     alternatives.
  1937.  
  1938.     FIXME:  Investigate why wait() seems to have problems with programs
  1939.     being control by /proc routines.
  1940.  
  1941.  */
  1942.  
  1943. static int
  1944. procfs_wait (statloc)
  1945.      int *statloc;
  1946. {
  1947.   short what;
  1948.   short why;
  1949.   int statval = 0;
  1950.   int checkerr = 0;
  1951.   int rtnval = -1;
  1952.   
  1953.   if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
  1954.     {
  1955.       checkerr++;
  1956.     }
  1957.   else if (!(pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
  1958.     {
  1959.       if (attach_flag)
  1960.     set_sigint_trap();    /* Causes SIGINT to be passed on to the
  1961.                    attached process. */
  1962.  
  1963.       if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
  1964.     {
  1965.       checkerr++;
  1966.     }
  1967.  
  1968.       if (attach_flag)
  1969.     clear_sigint_trap();
  1970.     }    
  1971.   if (checkerr)
  1972.     {
  1973.       if (errno == ENOENT)
  1974.     {
  1975.       rtnval = wait (&statval);
  1976.       if (rtnval != inferior_pid)
  1977.         {
  1978.           print_sys_errmsg (pi.pathname, errno);
  1979.           error ("PIOCWSTOP, wait failed, returned %d", rtnval);
  1980.           /* NOTREACHED */
  1981.         }
  1982.     }
  1983.       else
  1984.     {
  1985.       print_sys_errmsg (pi.pathname, errno);
  1986.       error ("PIOCSTATUS or PIOCWSTOP failed.");
  1987.       /* NOTREACHED */
  1988.     }
  1989.     }
  1990.   else if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
  1991.     {
  1992.       rtnval = pi.prstatus.pr_pid;
  1993.       why = pi.prstatus.pr_why;
  1994.       what = pi.prstatus.pr_what;
  1995.       if (why == PR_SIGNALLED)
  1996.     {
  1997.       statval = (what << 8) | 0177;
  1998.     }
  1999.       else if ((why == PR_SYSEXIT)
  2000.            &&
  2001.            (
  2002. #ifdef SYS_exec
  2003.         what == SYS_exec
  2004. #else
  2005.         0 == 0
  2006. #endif
  2007. #ifdef SYS_execve
  2008.         || what == SYS_execve
  2009. #endif
  2010. #ifdef SYS_execv
  2011.         || what == SYS_execv
  2012. #endif
  2013.         ))
  2014.     {
  2015.       statval = (SIGTRAP << 8) | 0177;
  2016.     }
  2017.       else if (why == PR_REQUESTED)
  2018.     {
  2019.       statval = (SIGSTOP << 8) | 0177;
  2020.     }
  2021.       else if (why == PR_JOBCONTROL)
  2022.     {
  2023.       statval = (what << 8) | 0177;
  2024.     }
  2025.       else if (why == PR_FAULTED)
  2026.     {
  2027.       switch (what)
  2028.         {
  2029.         case FLTPRIV:
  2030.         case FLTILL:
  2031.           statval = (SIGILL << 8) | 0177;
  2032.           break;
  2033.         case FLTBPT:
  2034.         case FLTTRACE:
  2035.           statval = (SIGTRAP << 8) | 0177;
  2036.           break;
  2037.         case FLTSTACK:
  2038.         case FLTACCESS:
  2039.         case FLTBOUNDS:
  2040.           statval = (SIGSEGV << 8) | 0177;
  2041.           break;
  2042.         case FLTIOVF:
  2043.         case FLTIZDIV:
  2044.         case FLTFPE:
  2045.           statval = (SIGFPE << 8) | 0177;
  2046.           break;
  2047.         case FLTPAGE:        /* Recoverable page fault */
  2048.         default:
  2049.           rtnval = -1;
  2050.           error ("PIOCWSTOP, unknown why %d, what %d", why, what);
  2051.           /* NOTREACHED */
  2052.         }
  2053.     }
  2054.       else
  2055.     {
  2056.       rtnval = -1;
  2057.       error ("PIOCWSTOP, unknown why %d, what %d", why, what);
  2058.       /* NOTREACHED */
  2059.     }
  2060.     }
  2061.   else
  2062.     {
  2063.       error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x", 
  2064.          pi.prstatus.pr_flags);
  2065.       /* NOTREACHED */
  2066.     }
  2067.  
  2068.   if (statloc)
  2069.     {
  2070.       *statloc = statval;
  2071.     }
  2072.  
  2073.   if (rtnval == -1)        /* No more children to wait for */
  2074.     {
  2075.       fprintf (stderr, "Child process unexpectedly missing.\n");
  2076.       *statloc = 42;    /* Claim it exited with signal 42 */
  2077.       return rtnval;
  2078.     }
  2079.  
  2080.   return (rtnval);
  2081. }
  2082.  
  2083. /*
  2084.  
  2085. LOCAL FUNCTION
  2086.  
  2087.     set_proc_siginfo - set a process's current signal info
  2088.  
  2089. SYNOPSIS
  2090.  
  2091.     void set_proc_siginfo (struct procinfo *pip, int signo);
  2092.  
  2093. DESCRIPTION
  2094.  
  2095.     Given a pointer to a process info struct in PIP and a signal number
  2096.     in SIGNO, set the process's current signal and its associated signal
  2097.     information.  The signal will be delivered to the process immediately
  2098.     after execution is resumed, even if it is being held.  In addition,
  2099.     this particular delivery will not cause another PR_SIGNALLED stop
  2100.     even if the signal is being traced.
  2101.  
  2102.     If we are not delivering the same signal that the prstatus siginfo
  2103.     struct contains information about, then synthesize a siginfo struct
  2104.     to match the signal we are doing to deliver, make it of the type
  2105.     "generated by a user process", and send this synthesized copy.  When
  2106.     used to set the inferior's signal state, this will be required if we
  2107.     are not currently stopped because of a traced signal, or if we decide
  2108.     to continue with a different signal.
  2109.  
  2110.     Note that when continuing the inferior from a stop due to receipt
  2111.     of a traced signal, we either have set PRCSIG to clear the existing
  2112.     signal, or we have to call this function to do a PIOCSSIG with either
  2113.     the existing siginfo struct from pr_info, or one we have synthesized
  2114.     appropriately for the signal we want to deliver.  Otherwise if the
  2115.     signal is still being traced, the inferior will immediately stop
  2116.     again.
  2117.  
  2118.     See siginfo(5) for more details.
  2119. */
  2120.  
  2121. static void
  2122. set_proc_siginfo (pip, signo)
  2123.      struct procinfo *pip;
  2124.      int signo;
  2125. {
  2126.   struct siginfo newsiginfo;
  2127.   struct siginfo *sip;
  2128.  
  2129.   if (pip -> valid)
  2130.     {
  2131.       if (signo == pip -> prstatus.pr_info.si_signo)
  2132.     {
  2133.       sip = &pip -> prstatus.pr_info;
  2134.     }
  2135.       else
  2136.     {
  2137.       memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
  2138.       sip = &newsiginfo;
  2139.       sip -> si_signo = signo;
  2140.       sip -> si_code = 0;
  2141.       sip -> si_errno = 0;
  2142.       sip -> si_pid = getpid ();
  2143.       sip -> si_uid = getuid ();
  2144.     }
  2145.       if (ioctl (pip -> fd, PIOCSSIG, sip) < 0)
  2146.     {
  2147.       print_sys_errmsg (pip -> pathname, errno);
  2148.       warning ("PIOCSSIG failed");
  2149.     }
  2150.     }
  2151. }
  2152.  
  2153. /*
  2154.  
  2155. LOCAL FUNCTION
  2156.  
  2157.     procfs_resume -- resume execution of the inferior process
  2158.  
  2159. SYNOPSIS
  2160.  
  2161.     void procfs_resume (int step, int signo)
  2162.  
  2163. DESCRIPTION
  2164.  
  2165.     Resume execution of the inferior process.  If STEP is nozero, then
  2166.     just single step it.  If SIGNAL is nonzero, restart it with that
  2167.     signal activated.
  2168.  
  2169. NOTE
  2170.  
  2171.     It may not be absolutely necessary to specify the PC value for
  2172.     restarting, but to be safe we use the value that gdb considers
  2173.     to be current.  One case where this might be necessary is if the
  2174.     user explicitly changes the PC value that gdb considers to be
  2175.     current.  FIXME:  Investigate if this is necessary or not.
  2176.  
  2177.     When attaching to a child process, if we forced it to stop with
  2178.     a PIOCSTOP, then we will have set the nopass_next_sigstop flag.
  2179.     Upon resuming the first time after such a stop, we explicitly
  2180.     inhibit sending it another SIGSTOP, which would be the normal
  2181.     result of default signal handling.  One potential drawback to
  2182.     this is that we will also ignore any attempt to by the user
  2183.     to explicitly continue after the attach with a SIGSTOP.  Ultimately
  2184.     this problem should be dealt with by making the routines that
  2185.     deal with the inferior a little smarter, and possibly even allow
  2186.     an inferior to continue running at the same time as gdb.  (FIXME?)
  2187.  */
  2188.  
  2189. static void
  2190. procfs_resume (step, signo)
  2191.      int step;
  2192.      int signo;
  2193. {
  2194.   errno = 0;
  2195.   pi.prrun.pr_flags = PRSTRACE | PRSFAULT | PRCFAULT;
  2196.  
  2197. #ifdef PRSVADDR_BROKEN
  2198. /* Can't do this under Solaris running on a Sparc, as there seems to be no
  2199.    place to put nPC.  In fact, if you use this, nPC seems to be set to some
  2200.    random garbage.  We have to rely on the fact that PC and nPC have been
  2201.    written previously via PIOCSREG during a register flush. */
  2202.  
  2203.   pi.prrun.pr_vaddr = (caddr_t) *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)];
  2204.   pi.prrun.pr_flags != PRSVADDR;
  2205. #endif
  2206.  
  2207.   if (signo && !(signo == SIGSTOP && pi.nopass_next_sigstop))
  2208.     {
  2209.       set_proc_siginfo (&pi, signo);
  2210.     }
  2211.   else
  2212.     {
  2213.       pi.prrun.pr_flags |= PRCSIG;
  2214.     }
  2215.   pi.nopass_next_sigstop = 0;
  2216.   if (step)
  2217.     {
  2218.       pi.prrun.pr_flags |= PRSTEP;
  2219.     }
  2220.   if (ioctl (pi.fd, PIOCRUN, &pi.prrun) != 0)
  2221.     {
  2222.       perror_with_name (pi.pathname);
  2223.       /* NOTREACHED */
  2224.     }
  2225. }
  2226.  
  2227. /*
  2228.  
  2229. LOCAL FUNCTION
  2230.  
  2231.     procfs_fetch_registers -- fetch current registers from inferior
  2232.  
  2233. SYNOPSIS
  2234.  
  2235.     void procfs_fetch_registers (int regno)
  2236.  
  2237. DESCRIPTION
  2238.  
  2239.     Read the current values of the inferior's registers, both the
  2240.     general register set and floating point registers (if supported)
  2241.     and update gdb's idea of their current values.
  2242.  
  2243. */
  2244.  
  2245. static void
  2246. procfs_fetch_registers (regno)
  2247.      int regno;
  2248. {
  2249.   if (ioctl (pi.fd, PIOCGREG, &pi.gregset) != -1)
  2250.     {
  2251.       supply_gregset (&pi.gregset);
  2252.     }
  2253. #if defined (FP0_REGNUM)
  2254.   if (ioctl (pi.fd, PIOCGFPREG, &pi.fpregset) != -1)
  2255.     {
  2256.       supply_fpregset (&pi.fpregset);
  2257.     }
  2258. #endif
  2259. }
  2260.  
  2261. /*
  2262.  
  2263. LOCAL FUNCTION
  2264.  
  2265.     proc_init_failed - called whenever /proc access initialization fails
  2266.  
  2267. SYNOPSIS
  2268.  
  2269.     static void proc_init_failed (char *why)
  2270.  
  2271. DESCRIPTION
  2272.  
  2273.     This function is called whenever initialization of access to a /proc
  2274.     entry fails.  It prints a suitable error message, does some cleanup,
  2275.     and then invokes the standard error processing routine which dumps
  2276.     us back into the command loop.
  2277.  */
  2278.  
  2279. static void
  2280. proc_init_failed (why)
  2281.      char *why;
  2282. {
  2283.   print_sys_errmsg (pi.pathname, errno);
  2284.   kill (pi.pid, SIGKILL);
  2285.   close_proc_file (&pi);
  2286.   error (why);
  2287.   /* NOTREACHED */
  2288. }
  2289.  
  2290. /*
  2291.  
  2292. LOCAL FUNCTION
  2293.  
  2294.     close_proc_file - close any currently open /proc entry
  2295.  
  2296. SYNOPSIS
  2297.  
  2298.     static void close_proc_file (struct procinfo *pip)
  2299.  
  2300. DESCRIPTION
  2301.  
  2302.     Close any currently open /proc entry and mark the process information
  2303.     entry as invalid.  In order to ensure that we don't try to reuse any
  2304.     stale information, the pid, fd, and pathnames are explicitly
  2305.     invalidated, which may be overkill.
  2306.  
  2307.  */
  2308.  
  2309. static void
  2310. close_proc_file (pip)
  2311.      struct procinfo *pip;
  2312. {
  2313.   pip -> pid = 0;
  2314.   if (pip -> valid)
  2315.     {
  2316.       close (pip -> fd);
  2317.     }
  2318.   pip -> fd = -1;
  2319.   if (pip -> pathname)
  2320.     {
  2321.       free (pip -> pathname);
  2322.       pip -> pathname = NULL;
  2323.     }
  2324.   pip -> valid = 0;
  2325. }
  2326.  
  2327. /*
  2328.  
  2329. LOCAL FUNCTION
  2330.  
  2331.     open_proc_file - open a /proc entry for a given process id
  2332.  
  2333. SYNOPSIS
  2334.  
  2335.     static int open_proc_file (int pid, struct procinfo *pip, int mode)
  2336.  
  2337. DESCRIPTION
  2338.  
  2339.     Given a process id and a mode, close the existing open /proc
  2340.     entry (if any) and open one for the new process id, in the
  2341.     specified mode.  Once it is open, then mark the local process
  2342.     information structure as valid, which guarantees that the pid,
  2343.     fd, and pathname fields match an open /proc entry.  Returns
  2344.     zero if the open fails, nonzero otherwise.
  2345.  
  2346.     Note that the pathname is left intact, even when the open fails,
  2347.     so that callers can use it to construct meaningful error messages
  2348.     rather than just "file open failed".
  2349.  */
  2350.  
  2351. static int
  2352. open_proc_file (pid, pip, mode)
  2353.      int pid;
  2354.      struct procinfo *pip;
  2355.      int mode;
  2356. {
  2357.   pip -> valid = 0;        /* FIXME, what is this? ?!  */
  2358.   if (pip -> valid)
  2359.     {
  2360.       close (pip -> fd);
  2361.     }
  2362.   if (pip -> pathname == NULL)
  2363.     {
  2364.       pip -> pathname = xmalloc (32);
  2365.     }
  2366.   sprintf (pip -> pathname, PROC_NAME_FMT, pid);
  2367.   if ((pip -> fd = open (pip -> pathname, mode)) >= 0)
  2368.     {
  2369.       pip -> valid = 1;
  2370.       pip -> pid = pid;
  2371.     }
  2372.   return (pip -> valid);
  2373. }
  2374.  
  2375. static char *
  2376. mappingflags (flags)
  2377.      long flags;
  2378. {
  2379.   static char asciiflags[8];
  2380.   
  2381.   strcpy (asciiflags, "-------");
  2382. #if defined (MA_PHYS)
  2383.   if (flags & MA_PHYS)   asciiflags[0] = 'd';
  2384. #endif
  2385.   if (flags & MA_STACK)  asciiflags[1] = 's';
  2386.   if (flags & MA_BREAK)  asciiflags[2] = 'b';
  2387.   if (flags & MA_SHARED) asciiflags[3] = 's';
  2388.   if (flags & MA_READ)   asciiflags[4] = 'r';
  2389.   if (flags & MA_WRITE)  asciiflags[5] = 'w';
  2390.   if (flags & MA_EXEC)   asciiflags[6] = 'x';
  2391.   return (asciiflags);
  2392. }
  2393.  
  2394. static void
  2395. info_proc_flags (pip, summary)
  2396.      struct procinfo *pip;
  2397.      int summary;
  2398. {
  2399.   struct trans *transp;
  2400.  
  2401.   printf_filtered ("%-32s", "Process status flags:");
  2402.   if (!summary)
  2403.     {
  2404.       printf_filtered ("\n\n");
  2405.     }
  2406.   for (transp = pr_flag_table; transp -> name != NULL; transp++)
  2407.     {
  2408.       if (pip -> prstatus.pr_flags & transp -> value)
  2409.     {
  2410.       if (summary)
  2411.         {
  2412.           printf_filtered ("%s ", transp -> name);
  2413.         }
  2414.       else
  2415.         {
  2416.           printf_filtered ("\t%-16s %s.\n", transp -> name, transp -> desc);
  2417.         }
  2418.     }
  2419.     }
  2420.   printf_filtered ("\n");
  2421. }
  2422.  
  2423. static void
  2424. info_proc_stop (pip, summary)
  2425.      struct procinfo *pip;
  2426.      int summary;
  2427. {
  2428.   struct trans *transp;
  2429.   int why;
  2430.   int what;
  2431.  
  2432.   why = pip -> prstatus.pr_why;
  2433.   what = pip -> prstatus.pr_what;
  2434.  
  2435.   if (pip -> prstatus.pr_flags & PR_STOPPED)
  2436.     {
  2437.       printf_filtered ("%-32s", "Reason for stopping:");
  2438.       if (!summary)
  2439.     {
  2440.       printf_filtered ("\n\n");
  2441.     }
  2442.       for (transp = pr_why_table; transp -> name != NULL; transp++)
  2443.     {
  2444.       if (why == transp -> value)
  2445.         {
  2446.           if (summary)
  2447.         {
  2448.           printf_filtered ("%s ", transp -> name);
  2449.         }
  2450.           else
  2451.         {
  2452.           printf_filtered ("\t%-16s %s.\n",
  2453.                    transp -> name, transp -> desc);
  2454.         }
  2455.           break;
  2456.         }
  2457.     }
  2458.       
  2459.       /* Use the pr_why field to determine what the pr_what field means, and
  2460.      print more information. */
  2461.       
  2462.       switch (why)
  2463.     {
  2464.       case PR_REQUESTED:
  2465.         /* pr_what is unused for this case */
  2466.         break;
  2467.       case PR_JOBCONTROL:
  2468.       case PR_SIGNALLED:
  2469.         if (summary)
  2470.           {
  2471.         printf_filtered ("%s ", signalname (what));
  2472.           }
  2473.         else
  2474.           {
  2475.         printf_filtered ("\t%-16s %s.\n", signalname (what),
  2476.                  safe_strsignal (what));
  2477.           }
  2478.         break;
  2479.       case PR_SYSENTRY:
  2480.         if (summary)
  2481.           {
  2482.         printf_filtered ("%s ", syscallname (what));
  2483.           }
  2484.         else
  2485.           {
  2486.         printf_filtered ("\t%-16s %s.\n", syscallname (what),
  2487.                  "Entered this system call");
  2488.           }
  2489.         break;
  2490.       case PR_SYSEXIT:
  2491.         if (summary)
  2492.           {
  2493.         printf_filtered ("%s ", syscallname (what));
  2494.           }
  2495.         else
  2496.           {
  2497.         printf_filtered ("\t%-16s %s.\n", syscallname (what),
  2498.                  "Returned from this system call");
  2499.           }
  2500.         break;
  2501.       case PR_FAULTED:
  2502.         if (summary)
  2503.           {
  2504.         printf_filtered ("%s ",
  2505.                  lookupname (faults_table, what, "fault"));
  2506.           }
  2507.         else
  2508.           {
  2509.         printf_filtered ("\t%-16s %s.\n",
  2510.                  lookupname (faults_table, what, "fault"),
  2511.                  lookupdesc (faults_table, what));
  2512.           }
  2513.         break;
  2514.       }
  2515.       printf_filtered ("\n");
  2516.     }
  2517. }
  2518.  
  2519. static void
  2520. info_proc_siginfo (pip, summary)
  2521.      struct procinfo *pip;
  2522.      int summary;
  2523. {
  2524.   struct siginfo *sip;
  2525.  
  2526.   if ((pip -> prstatus.pr_flags & PR_STOPPED) &&
  2527.       (pip -> prstatus.pr_why == PR_SIGNALLED ||
  2528.        pip -> prstatus.pr_why == PR_FAULTED))
  2529.     {
  2530.       printf_filtered ("%-32s", "Additional signal/fault info:");
  2531.       sip = &pip -> prstatus.pr_info;
  2532.       if (summary)
  2533.     {
  2534.       printf_filtered ("%s ", signalname (sip -> si_signo));
  2535.       if (sip -> si_errno > 0)
  2536.         {
  2537.           printf_filtered ("%s ", errnoname (sip -> si_errno));
  2538.         }
  2539.       if (sip -> si_code <= 0)
  2540.         {
  2541.           printf_filtered ("sent by pid %d, uid %d ", sip -> si_pid,
  2542.                    sip -> si_uid);
  2543.         }
  2544.       else
  2545.         {
  2546.           printf_filtered ("%s ", sigcodename (sip));
  2547.           if ((sip -> si_signo == SIGILL) ||
  2548.           (sip -> si_signo == SIGFPE) ||
  2549.           (sip -> si_signo == SIGSEGV) ||
  2550.           (sip -> si_signo == SIGBUS))
  2551.         {
  2552.           printf_filtered ("addr=%#x ", sip -> si_addr);
  2553.         }
  2554.           else if ((sip -> si_signo == SIGCHLD))
  2555.         {
  2556.           printf_filtered ("child pid %u, status %u ",
  2557.                    sip -> si_pid,
  2558.                    sip -> si_status);
  2559.         }
  2560.           else if ((sip -> si_signo == SIGPOLL))
  2561.         {
  2562.           printf_filtered ("band %u ", sip -> si_band);
  2563.         }
  2564.         }
  2565.     }
  2566.       else
  2567.     {
  2568.       printf_filtered ("\n\n");
  2569.       printf_filtered ("\t%-16s %s.\n", signalname (sip -> si_signo),
  2570.                safe_strsignal (sip -> si_signo));
  2571.       if (sip -> si_errno > 0)
  2572.         {
  2573.           printf_filtered ("\t%-16s %s.\n",
  2574.                    errnoname (sip -> si_errno),
  2575.                    safe_strerror (sip -> si_errno));
  2576.         }
  2577.       if (sip -> si_code <= 0)
  2578.         {
  2579.           printf_filtered ("\t%-16u %s\n", sip -> si_pid,
  2580.                    "PID of process sending signal");
  2581.           printf_filtered ("\t%-16u %s\n", sip -> si_uid,
  2582.                    "UID of process sending signal");
  2583.         }
  2584.       else
  2585.         {
  2586.           printf_filtered ("\t%-16s %s.\n", sigcodename (sip),
  2587.                    sigcodedesc (sip));
  2588.           if ((sip -> si_signo == SIGILL) ||
  2589.           (sip -> si_signo == SIGFPE))
  2590.         {
  2591.           printf_filtered ("\t%-16#x %s.\n", sip -> si_addr,
  2592.                    "Address of faulting instruction");
  2593.         }
  2594.           else if ((sip -> si_signo == SIGSEGV) ||
  2595.                (sip -> si_signo == SIGBUS))
  2596.         {
  2597.           printf_filtered ("\t%-16#x %s.\n", sip -> si_addr,
  2598.                    "Address of faulting memory reference");
  2599.         }
  2600.           else if ((sip -> si_signo == SIGCHLD))
  2601.         {
  2602.           printf_filtered ("\t%-16u %s.\n", sip -> si_pid,
  2603.                    "Child process ID");
  2604.           printf_filtered ("\t%-16u %s.\n", sip -> si_status,
  2605.                    "Child process exit value or signal");
  2606.         }
  2607.           else if ((sip -> si_signo == SIGPOLL))
  2608.         {
  2609.           printf_filtered ("\t%-16u %s.\n", sip -> si_band,
  2610.                    "Band event for POLL_{IN,OUT,MSG}");
  2611.         }
  2612.         }
  2613.     }
  2614.       printf_filtered ("\n");
  2615.     }
  2616. }
  2617.  
  2618. static void
  2619. info_proc_syscalls (pip, summary)
  2620.      struct procinfo *pip;
  2621.      int summary;
  2622. {
  2623.   int syscallnum;
  2624.  
  2625.   if (!summary)
  2626.     {
  2627.  
  2628. #if 0    /* FIXME:  Needs to use gdb-wide configured info about system calls. */
  2629.       if (pip -> prstatus.pr_flags & PR_ASLEEP)
  2630.     {
  2631.       int syscallnum = pip -> prstatus.pr_reg[R_D0];
  2632.       if (summary)
  2633.         {
  2634.           printf_filtered ("%-32s", "Sleeping in system call:");
  2635.           printf_filtered ("%s", syscallname (syscallnum));
  2636.         }
  2637.       else
  2638.         {
  2639.           printf_filtered ("Sleeping in system call '%s'.\n",
  2640.                    syscallname (syscallnum));
  2641.         }
  2642.     }
  2643. #endif
  2644.  
  2645.       if (ioctl (pip -> fd, PIOCGENTRY, &pip -> entryset) < 0)
  2646.     {
  2647.       print_sys_errmsg (pip -> pathname, errno);
  2648.       error ("PIOCGENTRY failed");
  2649.     }
  2650.       
  2651.       if (ioctl (pip -> fd, PIOCGEXIT, &pip -> exitset) < 0)
  2652.     {
  2653.       print_sys_errmsg (pip -> pathname, errno);
  2654.       error ("PIOCGEXIT failed");
  2655.     }
  2656.       
  2657.       printf_filtered ("System call tracing information:\n\n");
  2658.       
  2659.       printf_filtered ("\t%-12s %-8s %-8s\n",
  2660.                "System call",
  2661.                "Entry",
  2662.                "Exit");
  2663.       for (syscallnum = 0; syscallnum < MAX_SYSCALLS; syscallnum++)
  2664.     {
  2665.       QUIT;
  2666.       if (syscall_table[syscallnum] != NULL)
  2667.         {
  2668.           printf_filtered ("\t%-12s ", syscall_table[syscallnum]);
  2669.           printf_filtered ("%-8s ",
  2670.                    prismember (&pip -> entryset, syscallnum)
  2671.                    ? "on" : "off");
  2672.           printf_filtered ("%-8s ",
  2673.                    prismember (&pip -> exitset, syscallnum)
  2674.                    ? "on" : "off");
  2675.           printf_filtered ("\n");
  2676.         }
  2677.       }
  2678.       printf_filtered ("\n");
  2679.     }
  2680. }
  2681.  
  2682. static char *
  2683. signalname (signo)
  2684.      int signo;
  2685. {
  2686.   char *name;
  2687.   static char locbuf[32];
  2688.  
  2689.   name = strsigno (signo);
  2690.   if (name == NULL)
  2691.     {
  2692.       sprintf (locbuf, "Signal %d", signo);
  2693.     }
  2694.   else
  2695.     {
  2696.       sprintf (locbuf, "%s (%d)", name, signo);
  2697.     }
  2698.   return (locbuf);
  2699. }
  2700.  
  2701. static char *
  2702. errnoname (errnum)
  2703.      int errnum;
  2704. {
  2705.   char *name;
  2706.   static char locbuf[32];
  2707.  
  2708.   name = strerrno (errnum);
  2709.   if (name == NULL)
  2710.     {
  2711.       sprintf (locbuf, "Errno %d", errnum);
  2712.     }
  2713.   else
  2714.     {
  2715.       sprintf (locbuf, "%s (%d)", name, errnum);
  2716.     }
  2717.   return (locbuf);
  2718. }
  2719.  
  2720. static void
  2721. info_proc_signals (pip, summary)
  2722.      struct procinfo *pip;
  2723.      int summary;
  2724. {
  2725.   int signo;
  2726.  
  2727.   if (!summary)
  2728.     {
  2729.       if (ioctl (pip -> fd, PIOCGTRACE, &pip -> trace) < 0)
  2730.     {
  2731.       print_sys_errmsg (pip -> pathname, errno);
  2732.       error ("PIOCGTRACE failed");
  2733.     }
  2734.       
  2735.       printf_filtered ("Disposition of signals:\n\n");
  2736.       printf_filtered ("\t%-15s %-8s %-8s %-8s  %s\n\n",
  2737.                "Signal", "Trace", "Hold", "Pending", "Description");
  2738.       for (signo = 0; signo < NSIG; signo++)
  2739.     {
  2740.       QUIT;
  2741.       printf_filtered ("\t%-15s ", signalname (signo));
  2742.       printf_filtered ("%-8s ",
  2743.                prismember (&pip -> trace, signo)
  2744.                ? "on" : "off");
  2745.       printf_filtered ("%-8s ",
  2746.                prismember (&pip -> prstatus.pr_sighold, signo)
  2747.                ? "on" : "off");
  2748.       printf_filtered ("%-8s ",
  2749.                prismember (&pip -> prstatus.pr_sigpend, signo)
  2750.                ? "yes" : "no");
  2751.       printf_filtered (" %s\n", safe_strsignal (signo));
  2752.     }
  2753.       printf_filtered ("\n");
  2754.     }
  2755. }
  2756.  
  2757. static void
  2758. info_proc_faults (pip, summary)
  2759.      struct procinfo *pip;
  2760.      int summary;
  2761. {
  2762.   struct trans *transp;
  2763.  
  2764.   if (!summary)
  2765.     {
  2766.       if (ioctl (pip -> fd, PIOCGFAULT, &pip -> fltset) < 0)
  2767.     {
  2768.       print_sys_errmsg (pip -> pathname, errno);
  2769.       error ("PIOCGFAULT failed");
  2770.     }
  2771.       
  2772.       printf_filtered ("Current traced hardware fault set:\n\n");
  2773.       printf_filtered ("\t%-12s %-8s\n", "Fault", "Trace");
  2774.  
  2775.       for (transp = faults_table; transp -> name != NULL; transp++)
  2776.     {
  2777.       QUIT;
  2778.       printf_filtered ("\t%-12s ", transp -> name);
  2779.       printf_filtered ("%-8s", prismember (&pip -> fltset, transp -> value)
  2780.                ? "on" : "off");
  2781.       printf_filtered ("\n");
  2782.     }
  2783.       printf_filtered ("\n");
  2784.     }
  2785. }
  2786.  
  2787. static void
  2788. info_proc_mappings (pip, summary)
  2789.      struct procinfo *pip;
  2790.      int summary;
  2791. {
  2792.   int nmap;
  2793.   struct prmap *prmaps;
  2794.   struct prmap *prmap;
  2795.  
  2796.   if (!summary)
  2797.     {
  2798.       printf_filtered ("Mapped address spaces:\n\n");
  2799.       printf_filtered ("\t%10s %10s %10s %10s %7s\n",
  2800.                "Start Addr",
  2801.                "  End Addr",
  2802.                "      Size",
  2803.                "    Offset",
  2804.                "Flags");
  2805.       if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0)
  2806.     {
  2807.       prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
  2808.       if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0)
  2809.         {
  2810.           for (prmap = prmaps; prmap -> pr_size; ++prmap)
  2811.         {
  2812.           printf_filtered ("\t%#10x %#10x %#10x %#10x %7s\n",
  2813.                    prmap -> pr_vaddr,
  2814.                    prmap -> pr_vaddr + prmap -> pr_size - 1,
  2815.                    prmap -> pr_size,
  2816.                    prmap -> pr_off,
  2817.                    mappingflags (prmap -> pr_mflags));
  2818.         }
  2819.         }
  2820.     }
  2821.       printf_filtered ("\n");
  2822.     }
  2823. }
  2824.  
  2825. /*
  2826.  
  2827. LOCAL FUNCTION
  2828.  
  2829.     info_proc -- implement the "info proc" command
  2830.  
  2831. SYNOPSIS
  2832.  
  2833.     void info_proc (char *args, int from_tty)
  2834.  
  2835. DESCRIPTION
  2836.  
  2837.     Implement gdb's "info proc" command by using the /proc interface
  2838.     to print status information about any currently running process.
  2839.  
  2840.     Examples of the use of "info proc" are:
  2841.  
  2842.     info proc        (prints summary info for current inferior)
  2843.     info proc 123        (prints summary info for process with pid 123)
  2844.     info proc mappings    (prints address mappings)
  2845.     info proc times        (prints process/children times)
  2846.     info proc id        (prints pid, ppid, gid, sid, etc)
  2847.         FIXME:  i proc id not implemented.
  2848.     info proc status    (prints general process state info)
  2849.         FIXME:  i proc status not implemented.
  2850.     info proc signals    (prints info about signal handling)
  2851.     info proc all        (prints all info)
  2852.  
  2853.  */
  2854.  
  2855. static void
  2856. info_proc (args, from_tty)
  2857.      char *args;
  2858.      int from_tty;
  2859. {
  2860.   int pid;
  2861.   struct procinfo pii;
  2862.   struct procinfo *pip;
  2863.   struct cleanup *old_chain;
  2864.   char **argv;
  2865.   int argsize;
  2866.   int summary = 1;
  2867.   int flags = 0;
  2868.   int syscalls = 0;
  2869.   int signals = 0;
  2870.   int faults = 0;
  2871.   int mappings = 0;
  2872.   int times = 0;
  2873.   int id = 0;
  2874.   int status = 0;
  2875.   int all = 0;
  2876.  
  2877.   old_chain = make_cleanup (null_cleanup, 0);
  2878.  
  2879.   /* Default to using the current inferior if no pid specified */
  2880.  
  2881.   pip = π
  2882.  
  2883.   if (args != NULL)
  2884.     {
  2885.       if ((argv = buildargv (args)) == NULL)
  2886.     {
  2887.       nomem (0);
  2888.     }
  2889.       make_cleanup (freeargv, (char *) argv);
  2890.  
  2891.       while (*argv != NULL)
  2892.     {
  2893.       argsize = strlen (*argv);
  2894.       if (argsize >= 1 && strncmp (*argv, "all", argsize) == 0)
  2895.         {
  2896.           summary = 0;
  2897.           all = 1;
  2898.         }
  2899.       else if (argsize >= 2 && strncmp (*argv, "faults", argsize) == 0)
  2900.         {
  2901.           summary = 0;
  2902.           faults = 1;
  2903.         }
  2904.       else if (argsize >= 2 && strncmp (*argv, "flags", argsize) == 0)
  2905.         {
  2906.           summary = 0;
  2907.           flags = 1;
  2908.         }
  2909.       else if (argsize >= 1 && strncmp (*argv, "id", argsize) == 0)
  2910.         {
  2911.           summary = 0;
  2912.           id = 1;
  2913.         }
  2914.       else if (argsize >= 1 && strncmp (*argv, "mappings", argsize) == 0)
  2915.         {
  2916.           summary = 0;
  2917.           mappings = 1;
  2918.         }
  2919.       else if (argsize >= 2 && strncmp (*argv, "signals", argsize) == 0)
  2920.         {
  2921.           summary = 0;
  2922.           signals = 1;
  2923.         }
  2924.       else if (argsize >= 2 && strncmp (*argv, "status", argsize) == 0)
  2925.         {
  2926.           summary = 0;
  2927.           status = 1;
  2928.         }
  2929.       else if (argsize >= 2 && strncmp (*argv, "syscalls", argsize) == 0)
  2930.         {
  2931.           summary = 0;
  2932.           syscalls = 1;
  2933.         }
  2934.       else if (argsize >= 1 && strncmp (*argv, "times", argsize) == 0)
  2935.         {
  2936.           summary = 0;
  2937.           times = 1;
  2938.         }
  2939.       else if ((pii.pid = atoi (*argv)) > 0)
  2940.         {
  2941.           pid = pii.pid;
  2942.           pip = &pii;
  2943.           memset (&pii, 0, sizeof (pii));
  2944.           if (!open_proc_file (pid, pip, O_RDONLY))
  2945.         {
  2946.           perror_with_name (pip -> pathname);
  2947.           /* NOTREACHED */
  2948.         }
  2949.           make_cleanup (close_proc_file, pip);
  2950.         }
  2951.       else if (**argv != '\000')
  2952.         {
  2953.           error ("Unrecognized or ambiguous keyword `%s'.", *argv);
  2954.         }
  2955.       argv++;
  2956.     }
  2957.     }
  2958.  
  2959.   /* If we don't have a valid open process at this point, then we have no
  2960.      inferior or didn't specify a specific pid. */
  2961.  
  2962.   if (!pip -> valid)
  2963.     {
  2964.       error ("No process.  Run an inferior or specify an explicit pid.");
  2965.     }
  2966.   if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
  2967.     {
  2968.       print_sys_errmsg (pip -> pathname, errno);
  2969.       error ("PIOCSTATUS failed");
  2970.     }
  2971.  
  2972.   /* Print verbose information of the requested type(s), or just a summary
  2973.      of the information for all types. */
  2974.  
  2975.   printf_filtered ("\nInformation for %s:\n\n", pip -> pathname);
  2976.   if (summary || all || flags)
  2977.     {
  2978.       info_proc_flags (pip, summary);
  2979.     }
  2980.   if (summary || all)
  2981.     {
  2982.       info_proc_stop (pip, summary);
  2983.     }
  2984.   if (summary || all || signals || faults)
  2985.     {
  2986.       info_proc_siginfo (pip, summary);
  2987.     }
  2988.   if (summary || all || syscalls)
  2989.     {
  2990.       info_proc_syscalls (pip, summary);
  2991.     }
  2992.   if (summary || all || mappings)
  2993.     {
  2994.       info_proc_mappings (pip, summary);
  2995.     }
  2996.   if (summary || all || signals)
  2997.     {
  2998.       info_proc_signals (pip, summary);
  2999.     }
  3000.   if (summary || all || faults)
  3001.     {
  3002.       info_proc_faults (pip, summary);
  3003.     }
  3004.   printf_filtered ("\n");
  3005.  
  3006.   /* All done, deal with closing any temporary process info structure,
  3007.      freeing temporary memory , etc. */
  3008.  
  3009.   do_cleanups (old_chain);
  3010. }
  3011.  
  3012. /* Fork an inferior process, and start debugging it with /proc.  */
  3013.  
  3014. static void
  3015. procfs_create_inferior (exec_file, allargs, env)
  3016.      char *exec_file;
  3017.      char *allargs;
  3018.      char **env;
  3019. {
  3020.   fork_inferior (exec_file, allargs, env,
  3021.          proc_set_exec_trap, procfs_init_inferior);
  3022.   /* We are at the first instruction we care about.  */
  3023.   /* Pedal to the metal... */
  3024.   proceed ((CORE_ADDR) -1, 0, 0);
  3025. }
  3026.  
  3027. /* Clean up after the inferior dies.  */
  3028.  
  3029. static void
  3030. procfs_mourn_inferior ()
  3031. {
  3032.   unpush_target (&procfs_ops);
  3033.   generic_mourn_inferior ();
  3034. }
  3035.  
  3036. /* Mark our target-struct as eligible for stray "run" and "attach" commands.  */
  3037. static int
  3038. procfs_can_run ()
  3039. {
  3040.   return(1);
  3041. }
  3042.  
  3043. struct target_ops procfs_ops = {
  3044.   "procfs",            /* to_shortname */
  3045.   "Unix /proc child process",    /* to_longname */
  3046.   "Unix /proc child process (started by the \"run\" command).",    /* to_doc */
  3047.   procfs_open,            /* to_open */
  3048.   0,                /* to_close */
  3049.   procfs_attach,            /* to_attach */
  3050.   procfs_detach,         /* to_detach */
  3051.   procfs_resume,            /* to_resume */
  3052.   procfs_wait,            /* to_wait */
  3053.   procfs_fetch_registers,    /* to_fetch_registers */
  3054.   procfs_store_registers,    /* to_store_registers */
  3055.   procfs_prepare_to_store,    /* to_prepare_to_store */
  3056.   procfs_xfer_memory,        /* to_xfer_memory */
  3057.   procfs_files_info,        /* to_files_info */
  3058.   memory_insert_breakpoint,    /* to_insert_breakpoint */
  3059.   memory_remove_breakpoint,    /* to_remove_breakpoint */
  3060.   terminal_init_inferior,    /* to_terminal_init */
  3061.   terminal_inferior,         /* to_terminal_inferior */
  3062.   terminal_ours_for_output,    /* to_terminal_ours_for_output */
  3063.   terminal_ours,        /* to_terminal_ours */
  3064.   child_terminal_info,        /* to_terminal_info */
  3065.   procfs_kill_inferior,        /* to_kill */
  3066.   0,                /* to_load */
  3067.   0,                /* to_lookup_symbol */
  3068.   procfs_create_inferior,    /* to_create_inferior */
  3069.   procfs_mourn_inferior,    /* to_mourn_inferior */
  3070.   procfs_can_run,        /* to_can_run */
  3071.   procfs_notice_signals,    /* to_notice_signals */
  3072.   process_stratum,        /* to_stratum */
  3073.   0,                /* to_next */
  3074.   1,                /* to_has_all_memory */
  3075.   1,                /* to_has_memory */
  3076.   1,                /* to_has_stack */
  3077.   1,                /* to_has_registers */
  3078.   1,                /* to_has_execution */
  3079.   0,                /* sections */
  3080.   0,                /* sections_end */
  3081.   OPS_MAGIC            /* to_magic */
  3082. };
  3083.  
  3084. /*
  3085.  
  3086. GLOBAL FUNCTION
  3087.  
  3088.     _initialize_procfs -- initialize the process file system stuff
  3089.  
  3090. SYNOPSIS
  3091.  
  3092.     void _initialize_procfs (void)
  3093.  
  3094. DESCRIPTION
  3095.  
  3096.     Do required initializations during gdb startup for using the
  3097.     /proc file system interface.
  3098.  
  3099. */
  3100.  
  3101. void
  3102. _initialize_procfs ()
  3103. {
  3104.   add_target (&procfs_ops);
  3105.  
  3106.   add_info ("proc", info_proc, 
  3107. "Show process status information using /proc entry.\n\
  3108. Specify process id or use current inferior by default.\n\
  3109. Specify keywords for detailed information; default is summary.\n\
  3110. Keywords are: `all', `faults', `flags', `id', `mappings', `signals',\n\
  3111. `status', `syscalls', and `times'.\n\
  3112. Unambiguous abbreviations may be used.");
  3113.  
  3114.   init_syscall_table ();
  3115. }
  3116.