home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / GDB / GDB-4.13 / GDB-4 / gdb-4.13 / gdb / procfs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  90.9 KB  |  3,713 lines

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