⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 procfs.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 5 页
字号:
/* Machine independent support for SVR4 /proc (process file system) for GDB.   Copyright 1991, 1992 Free Software Foundation, Inc.   Written by Fred Fish at Cygnus Support.This file is part of GDB.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  *//*			N  O  T  E  SFor information on the details of using /proc consult section proc(4)in the UNIX System V Release 4 System Administrator's Reference Manual.The general register and floating point register sets are manipulated byseparate ioctl's.  This file makes the assumption that if FP0_REGNUM isdefined, then support for the floating point register set is desired,regardless of whether or not the actual target has floating point hardware. */#include "defs.h"#include <time.h>#include <sys/procfs.h>#include <fcntl.h>#include <errno.h>#include <string.h>#include "inferior.h"#include "target.h"#include "command.h"#include "gdbcore.h"#include "nm.h"#define MAX_SYSCALLS	256	/* Maximum number of syscalls for table */#ifndef PROC_NAME_FMT#define PROC_NAME_FMT "/proc/%05d"#endifextern struct target_ops procfs_ops;		/* Forward declaration */#if 1	/* FIXME: Gross and ugly hack to resolve coredep.c global */CORE_ADDR kernel_u_addr;#endif#ifdef BROKEN_SIGINFO_H		/* Workaround broken SGS <sys/siginfo.h> */#undef si_pid#define si_pid _data._proc.pid#undef si_uid#define si_uid _data._proc._pdata._kill.uid#endif /* BROKEN_SIGINFO_H *//*  All access to the inferior, either one started by gdb or one that has    been attached to, is controlled by an instance of a procinfo structure,    defined below.  Since gdb currently only handles one inferior at a time,    the procinfo structure for the inferior is statically allocated and    only one exists at any given time.  There is a separate procinfo    structure for use by the "info proc" command, so that we can print    useful information about any random process without interfering with    the inferior's procinfo information. */struct procinfo {  int valid;			/* Nonzero if pid, fd, & pathname are valid */  int pid;			/* Process ID of inferior */  int fd;			/* File descriptor for /proc entry */  char *pathname;		/* Pathname to /proc entry */  int was_stopped;		/* Nonzero if was stopped prior to attach */  int nopass_next_sigstop;	/* Don't pass a sigstop on next resume */  prrun_t prrun;		/* Control state when it is run */  prstatus_t prstatus;		/* Current process status info */  gregset_t gregset;		/* General register set */  fpregset_t fpregset;		/* Floating point register set */  fltset_t fltset;		/* Current traced hardware fault set */  sigset_t trace;		/* Current traced signal set */  sysset_t exitset;		/* Current traced system call exit set */  sysset_t entryset;		/* Current traced system call entry set */  fltset_t saved_fltset;	/* Saved traced hardware fault set */  sigset_t saved_trace;		/* Saved traced signal set */  sigset_t saved_sighold;	/* Saved held signal set */  sysset_t saved_exitset;	/* Saved traced system call exit set */  sysset_t saved_entryset;	/* Saved traced system call entry set */};static struct procinfo pi;	/* Inferior's process information *//*  Much of the information used in the /proc interface, particularly for    printing status information, is kept as tables of structures of the    following form.  These tables can be used to map numeric values to    their symbolic names and to a string that describes their specific use. */struct trans {  int value;			/* The numeric value */  char *name;			/* The equivalent symbolic value */  char *desc;			/* Short description of value */};/*  Translate bits in the pr_flags member of the prstatus structure, into the    names and desc information. */static struct trans pr_flag_table[] ={#if defined (PR_STOPPED)  PR_STOPPED, "PR_STOPPED", "Process is stopped",#endif#if defined (PR_ISTOP)  PR_ISTOP, "PR_ISTOP", "Stopped on an event of interest",#endif#if defined (PR_DSTOP)  PR_DSTOP, "PR_DSTOP", "A stop directive is in effect",#endif#if defined (PR_ASLEEP)  PR_ASLEEP, "PR_ASLEEP", "Sleeping in an interruptible system call",#endif#if defined (PR_FORK)  PR_FORK, "PR_FORK", "Inherit-on-fork is in effect",#endif#if defined (PR_RLC)  PR_RLC, "PR_RLC", "Run-on-last-close is in effect",#endif#if defined (PR_PTRACE)  PR_PTRACE, "PR_PTRACE", "Process is being controlled by ptrace",#endif#if defined (PR_PCINVAL)  PR_PCINVAL, "PR_PCINVAL", "PC refers to an invalid virtual address",#endif#if defined (PR_ISSYS)  PR_ISSYS, "PR_ISSYS", "Is a system process",#endif#if defined (PR_STEP)  PR_STEP, "PR_STEP", "Process has single step pending",#endif#if defined (PR_KLC)  PR_KLC, "PR_KLC", "Kill-on-last-close is in effect",#endif#if defined (PR_ASYNC)  PR_ASYNC, "PR_ASYNC", "Asynchronous stop is in effect",#endif#if defined (PR_PCOMPAT)  PR_PCOMPAT, "PR_PCOMPAT", "Ptrace compatibility mode in effect",#endif 0, NULL, NULL};/*  Translate values in the pr_why field of the prstatus struct. */static struct trans pr_why_table[] ={#if defined (PR_REQUESTED) PR_REQUESTED, "PR_REQUESTED", "Directed to stop via PIOCSTOP/PIOCWSTOP",#endif#if defined (PR_SIGNALLED) PR_SIGNALLED, "PR_SIGNALLED", "Receipt of a traced signal",#endif#if defined (PR_FAULTED) PR_FAULTED, "PR_FAULTED", "Incurred a traced hardware fault",#endif#if defined (PR_SYSENTRY) PR_SYSENTRY, "PR_SYSENTRY", "Entry to a traced system call",#endif#if defined (PR_SYSEXIT) PR_SYSEXIT, "PR_SYSEXIT", "Exit from a traced system call",#endif#if defined (PR_JOBCONTROL) PR_JOBCONTROL, "PR_JOBCONTROL", "Default job control stop signal action",#endif#if defined (PR_SUSPENDED) PR_SUSPENDED, "PR_SUSPENDED", "Process suspended",#endif 0, NULL, NULL};/*  Hardware fault translation table. */static struct trans faults_table[] ={#if defined (FLTILL) FLTILL, "FLTILL", "Illegal instruction",#endif#if defined (FLTPRIV) FLTPRIV, "FLTPRIV", "Privileged instruction",#endif#if defined (FLTBPT) FLTBPT, "FLTBPT", "Breakpoint trap",#endif#if defined (FLTTRACE) FLTTRACE, "FLTTRACE", "Trace trap",#endif#if defined (FLTACCESS) FLTACCESS, "FLTACCESS", "Memory access fault",#endif#if defined (FLTBOUNDS) FLTBOUNDS, "FLTBOUNDS", "Memory bounds violation",#endif#if defined (FLTIOVF) FLTIOVF, "FLTIOVF", "Integer overflow",#endif#if defined (FLTIZDIV) FLTIZDIV, "FLTIZDIV", "Integer zero divide",#endif#if defined (FLTFPE) FLTFPE, "FLTFPE", "Floating-point exception",#endif#if defined (FLTSTACK) FLTSTACK, "FLTSTACK", "Unrecoverable stack fault",#endif#if defined (FLTPAGE) FLTPAGE, "FLTPAGE", "Recoverable page fault",#endif 0, NULL, NULL};/* Translation table for signal generation information.  See UNIX System   V Release 4 Programmer's Reference Manual, siginfo(5).  */static struct sigcode {  int signo;  int code;  char *codename;  char *desc;} siginfo_table[] = {#if defined (SIGILL) && defined (ILL_ILLOPC)  SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode",#endif#if defined (SIGILL) && defined (ILL_ILLOPN)  SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand",#endif#if defined (SIGILL) && defined (ILL_ILLADR)  SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode",#endif#if defined (SIGILL) && defined (ILL_ILLTRP)  SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap",#endif#if defined (SIGILL) && defined (ILL_PRVOPC)  SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode",#endif#if defined (SIGILL) && defined (ILL_PRVREG)  SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register",#endif#if defined (SIGILL) && defined (ILL_COPROC)  SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error",#endif#if defined (SIGILL) && defined (ILL_BADSTK)  SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error",#endif#if defined (SIGFPE) && defined (FPE_INTDIV)  SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero",#endif#if defined (SIGFPE) && defined (FPE_INTOVF)  SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow",#endif#if defined (SIGFPE) && defined (FPE_FLTDIV)  SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating point divide by zero",#endif#if defined (SIGFPE) && defined (FPE_FLTOVF)  SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating point overflow",#endif#if defined (SIGFPE) && defined (FPE_FLTUND)  SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating point underflow",#endif#if defined (SIGFPE) && defined (FPE_FLTRES)  SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating point inexact result",#endif#if defined (SIGFPE) && defined (FPE_FLTINV)  SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating point operation",#endif#if defined (SIGFPE) && defined (FPE_FLTSUB)  SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range",#endif#if defined (SIGSEGV) && defined (SEGV_MAPERR)  SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object",#endif#if defined (SIGSEGV) && defined (SEGV_ACCERR)  SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for object",#endif#if defined (SIGBUS) && defined (BUS_ADRALN)  SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment",#endif#if defined (SIGBUS) && defined (BUS_ADRERR)  SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Non-existent physical address",#endif#if defined (SIGBUS) && defined (BUS_OBJERR)  SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object specific hardware error",#endif#if defined (SIGTRAP) && defined (TRAP_BRKPT)  SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint",#endif#if defined (SIGTRAP) && defined (TRAP_TRACE)  SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap",#endif#if defined (SIGCLD) && defined (CLD_EXITED)  SIGCLD, CLD_EXITED, "CLD_EXITED", "Child has exited",#endif#if defined (SIGCLD) && defined (CLD_KILLED)  SIGCLD, CLD_KILLED, "CLD_KILLED", "Child was killed",#endif#if defined (SIGCLD) && defined (CLD_DUMPED)  SIGCLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally",#endif#if defined (SIGCLD) && defined (CLD_TRAPPED)  SIGCLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped",#endif#if defined (SIGCLD) && defined (CLD_STOPPED)  SIGCLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped",#endif#if defined (SIGCLD) && defined (CLD_CONTINUED)  SIGCLD, CLD_CONTINUED, "CLD_CONTINUED", "Stopped child had continued",#endif#if defined (SIGPOLL) && defined (POLL_IN)  SIGPOLL, POLL_IN, "POLL_IN", "Input input available",#endif#if defined (SIGPOLL) && defined (POLL_OUT)  SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available",#endif#if defined (SIGPOLL) && defined (POLL_MSG)  SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available",#endif#if defined (SIGPOLL) && defined (POLL_ERR)  SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error",#endif#if defined (SIGPOLL) && defined (POLL_PRI)  SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available",#endif#if defined (SIGPOLL) && defined (POLL_HUP)  SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected",#endif  0, 0, NULL, NULL};static char *syscall_table[MAX_SYSCALLS];/* Prototypes for local functions */static voidset_proc_siginfo PARAMS ((struct procinfo *, int));static voidinit_syscall_table PARAMS ((void));static char *syscallname PARAMS ((int));static char *signalname PARAMS ((int));static char *errnoname PARAMS ((int));static intproc_address_to_fd PARAMS ((CORE_ADDR, int));static intopen_proc_file PARAMS ((int, struct procinfo *, int));static voidclose_proc_file PARAMS ((struct procinfo *));static voidunconditionally_kill_inferior PARAMS ((void));static voidproc_init_failed PARAMS ((char *));static voidinfo_proc PARAMS ((char *, int));static voidinfo_proc_flags PARAMS ((struct procinfo *, int));static voidinfo_proc_stop PARAMS ((struct procinfo *, int));static voidinfo_proc_siginfo PARAMS ((struct procinfo *, int));static voidinfo_proc_syscalls PARAMS ((struct procinfo *, int));static voidinfo_proc_mappings PARAMS ((struct procinfo *, int));static voidinfo_proc_signals PARAMS ((struct procinfo *, int));static voidinfo_proc_faults PARAMS ((struct procinfo *, int));static char *mappingflags PARAMS ((long));static char *lookupname PARAMS ((struct trans *, unsigned int, char *));static char *lookupdesc PARAMS ((struct trans *, unsigned int));static intdo_attach PARAMS ((int pid));static voiddo_detach PARAMS ((int siggnal));static voidprocfs_create_inferior PARAMS ((char *, char *, char **));static voidprocfs_notice_signals PARAMS ((void));/* External function prototypes that can't be easily included in any   header file because the args are typedefs in system include files. */extern voidsupply_gregset PARAMS ((gregset_t *));extern voidfill_gregset PARAMS ((gregset_t *, int));extern voidsupply_fpregset PARAMS ((fpregset_t *));extern voidfill_fpregset PARAMS ((fpregset_t *, int));/*LOCAL FUNCTION	lookupdesc -- translate a value to a summary desc stringSYNOPSIS	static char *lookupdesc (struct trans *transp, unsigned int val);DESCRIPTION		Given a pointer to a translation table and a value to be translated,	lookup the desc string and return it. */static char *lookupdesc (transp, val)     struct trans *transp;     unsigned int val;{  char *desc;    for (desc = NULL; transp -> name != NULL; transp++)    {      if (transp -> value == val)	{	  desc = transp -> desc;	  break;	}    }  /* Didn't find a translation for the specified value, set a default one. */  if (desc == NULL)    {      desc = "Unknown";    }  return (desc);}/*LOCAL FUNCTION	lookupname -- translate a value to symbolic nameSYNOPSIS	static char *lookupname (struct trans *transp, unsigned int val,				 char *prefix);DESCRIPTION		Given a pointer to a translation table, a value to be translated,	and a default prefix to return if the value can't be translated,	match the value with one of the translation table entries and	return a pointer to the symbolic name.	If no match is found it just returns the value as a printable string,	with the given prefix.  The previous such value, if any, is freed	at this time. */static char *lookupname (transp, val, prefix)     struct trans *transp;     unsigned int val;     char *prefix;{  static char *locbuf;  char *name;    for (name = NULL; transp -> name != NULL; transp++)    {      if (transp -> value == val)	{	  name = transp -> name;	  break;	}    }  /* Didn't find a translation for the specified value, build a default     one using the specified prefix and return it.  The lifetime of     the value is only until the next one is needed. */  if (name == NULL)    {      if (locbuf != NULL)	{	  free (locbuf);	}      locbuf = xmalloc (strlen (prefix) + 16);      sprintf (locbuf, "%s %u", prefix, val);      name = locbuf;    }  return (name);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -