1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 /*
  26  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
  27  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
  28  * Copyright (c) 2013 by Delphix. All rights reserved.
  29  */
  30 
  31 #ifndef _PCONTROL_H
  32 #define _PCONTROL_H
  33 
  34 /*
  35  * Implemention-specific include file for libproc process management.
  36  * This is not to be seen by the clients of libproc.
  37  */
  38 
  39 #include <stdio.h>
  40 #include <gelf.h>
  41 #include <synch.h>
  42 #include <procfs.h>
  43 #include <rtld_db.h>
  44 #include <libproc.h>
  45 #include <libctf.h>
  46 #include <limits.h>
  47 #include <libproc.h>
  48 
  49 #ifdef  __cplusplus
  50 extern "C" {
  51 #endif
  52 
  53 #include "Putil.h"
  54 
  55 /*
  56  * Definitions of the process control structures, internal to libproc.
  57  * These may change without affecting clients of libproc.
  58  */
  59 
  60 /*
  61  * sym_tbl_t contains a primary and an (optional) auxiliary symbol table, which
  62  * we wish to treat as a single logical symbol table. In this logical table,
  63  * the data from the auxiliary table preceeds that from the primary. Symbol
  64  * indices start at [0], which is the first item in the auxiliary table
  65  * if there is one. The sole purpose for this is so that we can treat the
  66  * combination of .SUNW_ldynsym and .dynsym sections as a logically single
  67  * entity without having to violate the public interface to libelf.
  68  *
  69  * Both tables must share the same string table section.
  70  *
  71  * The symtab_getsym() function serves as a gelf_getsym() replacement
  72  * that is aware of the two tables and makes them look like a single table
  73  * to the caller.
  74  *
  75  */
  76 typedef struct sym_tbl {        /* symbol table */
  77         Elf_Data *sym_data_pri; /* primary table */
  78         Elf_Data *sym_data_aux; /* auxiliary table */
  79         size_t  sym_symn_aux;   /* number of entries in auxiliary table */
  80         size_t  sym_symn;       /* total number of entries in both tables */
  81         char    *sym_strs;      /* ptr to strings */
  82         size_t  sym_strsz;      /* size of string table */
  83         GElf_Shdr sym_hdr_pri;  /* primary symbol table section header */
  84         GElf_Shdr sym_hdr_aux;  /* auxiliary symbol table section header */
  85         GElf_Shdr sym_strhdr;   /* string table section header */
  86         Elf     *sym_elf;       /* faked-up ELF handle from core file */
  87         void    *sym_elfmem;    /* data for faked-up ELF handle */
  88         uint_t  *sym_byname;    /* symbols sorted by name */
  89         uint_t  *sym_byaddr;    /* symbols sorted by addr */
  90         size_t  sym_count;      /* number of symbols in each sorted list */
  91 } sym_tbl_t;
  92 
  93 typedef struct file_info {      /* symbol information for a mapped file */
  94         plist_t file_list;      /* linked list */
  95         char    file_pname[PRMAPSZ];    /* name from prmap_t */
  96         struct map_info *file_map;      /* primary (text) mapping */
  97         int     file_ref;       /* references from map_info_t structures */
  98         int     file_fd;        /* file descriptor for the mapped file */
  99         int     file_init;      /* 0: initialization yet to be performed */
 100         GElf_Half file_etype;   /* ELF e_type from ehdr */
 101         GElf_Half file_class;   /* ELF e_ident[EI_CLASS] from ehdr */
 102         rd_loadobj_t *file_lo;  /* load object structure from rtld_db */
 103         char    *file_lname;    /* load object name from rtld_db */
 104         char    *file_lbase;    /* pointer to basename of file_lname */
 105         char    *file_rname;    /* resolved on-disk object pathname */
 106         char    *file_rbase;    /* pointer to basename of file_rname */
 107         Elf     *file_elf;      /* ELF handle so we can close */
 108         void    *file_elfmem;   /* data for faked-up ELF handle */
 109         sym_tbl_t file_symtab;  /* symbol table */
 110         sym_tbl_t file_dynsym;  /* dynamic symbol table */
 111         uintptr_t file_dyn_base;        /* load address for ET_DYN files */
 112         uintptr_t file_plt_base;        /* base address for PLT */
 113         size_t  file_plt_size;  /* size of PLT region */
 114         uintptr_t file_jmp_rel; /* base address of PLT relocations */
 115         uintptr_t file_ctf_off; /* offset of CTF data in object file */
 116         size_t  file_ctf_size;  /* size of CTF data in object file */
 117         int     file_ctf_dyn;   /* does the CTF data reference the dynsym */
 118         void    *file_ctf_buf;  /* CTF data for this file */
 119         ctf_file_t *file_ctfp;  /* CTF container for this file */
 120         char    *file_shstrs;   /* section header string table */
 121         size_t  file_shstrsz;   /* section header string table size */
 122         uintptr_t *file_saddrs; /* section header addresses */
 123         uint_t  file_nsaddrs;   /* number of section header addresses */
 124 } file_info_t;
 125 
 126 typedef struct map_info {       /* description of an address space mapping */
 127         prmap_t map_pmap;       /* /proc description of this mapping */
 128         file_info_t *map_file;  /* pointer into list of mapped files */
 129         off64_t map_offset;     /* offset into core file (if core) */
 130         int map_relocate;       /* associated file_map needs to be relocated */
 131 } map_info_t;
 132 
 133 typedef struct lwp_info {       /* per-lwp information from core file */
 134         plist_t lwp_list;       /* linked list */
 135         lwpid_t lwp_id;         /* lwp identifier */
 136         lwpsinfo_t lwp_psinfo;  /* /proc/<pid>/lwp/<lwpid>/lwpsinfo data */
 137         lwpstatus_t lwp_status; /* /proc/<pid>/lwp/<lwpid>/lwpstatus data */
 138 #if defined(sparc) || defined(__sparc)
 139         gwindows_t *lwp_gwins;  /* /proc/<pid>/lwp/<lwpid>/gwindows data */
 140         prxregset_t *lwp_xregs; /* /proc/<pid>/lwp/<lwpid>/xregs data */
 141         int64_t *lwp_asrs;      /* /proc/<pid>/lwp/<lwpid>/asrs data */
 142 #endif
 143 } lwp_info_t;
 144 
 145 typedef struct fd_info {
 146         plist_t fd_list;        /* linked list */
 147         prfdinfo_t fd_info;     /* fd info */
 148 } fd_info_t;
 149 
 150 typedef struct core_info {      /* information specific to core files */
 151         char core_dmodel;       /* data model for core file */
 152         char core_osabi;        /* ELF OS ABI */
 153         int core_errno;         /* error during initialization if != 0 */
 154         plist_t core_lwp_head;  /* head of list of lwp info */
 155         lwp_info_t *core_lwp;   /* current lwp information */
 156         uint_t core_nlwp;       /* number of lwp's in list */
 157         off64_t core_size;      /* size of core file in bytes */
 158         char *core_platform;    /* platform string from core file */
 159         struct utsname *core_uts;       /* uname(2) data from core file */
 160         prcred_t *core_cred;    /* process credential from core file */
 161         core_content_t core_content;    /* content dumped to core file */
 162         prpriv_t *core_priv;    /* process privileges from core file */
 163         size_t core_priv_size;  /* size of the privileges */
 164         void *core_privinfo;    /* system privileges info from core file */
 165         priv_impl_info_t *core_ppii;    /* NOTE entry for core_privinfo */
 166         char *core_zonename;    /* zone name from core file */
 167 #if defined(__i386) || defined(__amd64)
 168         struct ssd *core_ldt;   /* LDT entries from core file */
 169         uint_t core_nldt;       /* number of LDT entries in core file */
 170 #endif
 171 } core_info_t;
 172 
 173 typedef struct elf_file_header { /* extended ELF header */
 174         unsigned char e_ident[EI_NIDENT];
 175         Elf64_Half e_type;
 176         Elf64_Half e_machine;
 177         Elf64_Word e_version;
 178         Elf64_Addr e_entry;
 179         Elf64_Off e_phoff;
 180         Elf64_Off e_shoff;
 181         Elf64_Word e_flags;
 182         Elf64_Half e_ehsize;
 183         Elf64_Half e_phentsize;
 184         Elf64_Half e_shentsize;
 185         Elf64_Word e_phnum;     /* phdr count extended to 32 bits */
 186         Elf64_Word e_shnum;     /* shdr count extended to 32 bits */
 187         Elf64_Word e_shstrndx;  /* shdr string index extended to 32 bits */
 188 } elf_file_header_t;
 189 
 190 typedef struct elf_file {       /* convenience for managing ELF files */
 191         elf_file_header_t e_hdr; /* Extended ELF header */
 192         Elf *e_elf;             /* ELF library handle */
 193         int e_fd;               /* file descriptor */
 194 } elf_file_t;
 195 
 196 #define HASHSIZE                1024    /* hash table size, power of 2 */
 197 
 198 struct ps_prochandle {
 199         struct ps_lwphandle **hashtab;  /* hash table for LWPs (Lgrab()) */
 200         mutex_t proc_lock;      /* protects hash table; serializes Lgrab() */
 201         pstatus_t orig_status;  /* remembered status on Pgrab() */
 202         pstatus_t status;       /* status when stopped */
 203         psinfo_t psinfo;        /* psinfo_t from last Ppsinfo() request */
 204         uintptr_t sysaddr;      /* address of most recent syscall instruction */
 205         pid_t   pid;            /* process-ID */
 206         int     state;          /* state of the process, see "libproc.h" */
 207         uint_t  flags;          /* see defines below */
 208         uint_t  agentcnt;       /* Pcreate_agent()/Pdestroy_agent() ref count */
 209         int     asfd;           /* /proc/<pid>/as filedescriptor */
 210         int     ctlfd;          /* /proc/<pid>/ctl filedescriptor */
 211         int     statfd;         /* /proc/<pid>/status filedescriptor */
 212         int     agentctlfd;     /* /proc/<pid>/lwp/agent/ctl */
 213         int     agentstatfd;    /* /proc/<pid>/lwp/agent/status */
 214         int     info_valid;     /* if zero, map and file info need updating */
 215         map_info_t *mappings;   /* cached process mappings */
 216         size_t  map_count;      /* number of mappings */
 217         size_t  map_alloc;      /* number of mappings allocated */
 218         uint_t  num_files;      /* number of file elements in file_info */
 219         plist_t file_head;      /* head of mapped files w/ symbol table info */
 220         char    *execname;      /* name of the executable file */
 221         auxv_t  *auxv;          /* the process's aux vector */
 222         int     nauxv;          /* number of aux vector entries */
 223         rd_agent_t *rap;        /* cookie for rtld_db */
 224         map_info_t *map_exec;   /* the mapping for the executable file */
 225         map_info_t *map_ldso;   /* the mapping for ld.so.1 */
 226         ps_ops_t ops;           /* ops-vector */
 227         uintptr_t *ucaddrs;     /* ucontext-list addresses */
 228         uint_t  ucnelems;       /* number of elements in the ucaddrs list */
 229         char    *zoneroot;      /* cached path to zone root */
 230         plist_t fd_head;        /* head of file desc info list */
 231         int     num_fd;         /* number of file descs in list */
 232         uintptr_t map_missing;  /* first missing mapping in core due to sig */
 233         siginfo_t killinfo;     /* signal that interrupted core dump */
 234         psinfo_t spymaster;     /* agent LWP's spymaster, if any */
 235         void *data;             /* private data */
 236 };
 237 
 238 /* flags */
 239 #define CREATED         0x01    /* process was created by Pcreate() */
 240 #define SETSIG          0x02    /* set signal trace mask before continuing */
 241 #define SETFAULT        0x04    /* set fault trace mask before continuing */
 242 #define SETENTRY        0x08    /* set sysentry trace mask before continuing */
 243 #define SETEXIT         0x10    /* set sysexit trace mask before continuing */
 244 #define SETHOLD         0x20    /* set signal hold mask before continuing */
 245 #define SETREGS         0x40    /* set registers before continuing */
 246 #define INCORE          0x80    /* use in-core data to build symbol tables */
 247 
 248 struct ps_lwphandle {
 249         struct ps_prochandle *lwp_proc; /* process to which this lwp belongs */
 250         struct ps_lwphandle *lwp_hash;  /* hash table linked list */
 251         lwpstatus_t     lwp_status;     /* status when stopped */
 252         lwpsinfo_t      lwp_psinfo;     /* lwpsinfo_t from last Lpsinfo() */
 253         lwpid_t         lwp_id;         /* lwp identifier */
 254         int             lwp_state;      /* state of the lwp, see "libproc.h" */
 255         uint_t          lwp_flags;      /* SETHOLD and/or SETREGS */
 256         int             lwp_ctlfd;      /* /proc/<pid>/lwp/<lwpid>/lwpctl */
 257         int             lwp_statfd;     /* /proc/<pid>/lwp/<lwpid>/lwpstatus */
 258 };
 259 
 260 /*
 261  * Implementation functions in the process control library.
 262  * These are not exported to clients of the library.
 263  */
 264 extern  void    prldump(const char *, lwpstatus_t *);
 265 extern  int     dupfd(int, int);
 266 extern  int     set_minfd(void);
 267 extern  int     Pscantext(struct ps_prochandle *);
 268 extern  void    Pinitsym(struct ps_prochandle *);
 269 extern  void    Preadauxvec(struct ps_prochandle *);
 270 extern  void    optimize_symtab(sym_tbl_t *);
 271 extern  void    Pbuild_file_symtab(struct ps_prochandle *, file_info_t *);
 272 extern  ctf_file_t *Pbuild_file_ctf(struct ps_prochandle *, file_info_t *);
 273 extern  map_info_t *Paddr2mptr(struct ps_prochandle *, uintptr_t);
 274 extern  char    *Pfindexec(struct ps_prochandle *, const char *,
 275         int (*)(const char *, void *), void *);
 276 extern  int     getlwpstatus(struct ps_prochandle *, lwpid_t, lwpstatus_t *);
 277 int     Pstopstatus(struct ps_prochandle *, long, uint32_t);
 278 extern  file_info_t *file_info_new(struct ps_prochandle *, map_info_t *);
 279 extern  char    *Plofspath(const char *, char *, size_t);
 280 extern  char    *Pzoneroot(struct ps_prochandle *, char *, size_t);
 281 extern  char    *Pzonepath(struct ps_prochandle *, const char *, char *,
 282         size_t);
 283 extern  fd_info_t *Pfd2info(struct ps_prochandle *, int);
 284 
 285 extern  char    *Pfindmap(struct ps_prochandle *, map_info_t *, char *,
 286         size_t);
 287 
 288 extern  int     Padd_mapping(struct ps_prochandle *, off64_t, file_info_t *,
 289     prmap_t *);
 290 extern  void    Psort_mappings(struct ps_prochandle *);
 291 
 292 extern char     procfs_path[PATH_MAX];
 293 
 294 /*
 295  * Architecture-dependent definition of the breakpoint instruction.
 296  */
 297 #if defined(sparc) || defined(__sparc)
 298 #define BPT     ((instr_t)0x91d02001)
 299 #elif defined(__i386) || defined(__amd64)
 300 #define BPT     ((instr_t)0xcc)
 301 #endif
 302 
 303 /*
 304  * Simple convenience.
 305  */
 306 #define TRUE    1
 307 #define FALSE   0
 308 
 309 #ifdef  __cplusplus
 310 }
 311 #endif
 312 
 313 #endif  /* _PCONTROL_H */