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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #ifndef _PCONTROL_H 28 #define _PCONTROL_H 29 30 #pragma ident "%Z%%M% %I% %E% SMI" 31 32 /* 33 * Implemention-specific include file for libproc process management. 34 * This is not to be seen by the clients of libproc. 35 */ 36 37 #include <stdio.h> 38 #include <gelf.h> 39 #include <synch.h> 40 #include <procfs.h> 41 #include <rtld_db.h> 42 #include <libproc.h> 43 #include <libctf.h> 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 #include "Putil.h" 50 51 /* 52 * Definitions of the process control structures, internal to libproc. 53 * These may change without affecting clients of libproc. 54 */ 55 56 typedef struct { /* symbol table */ 57 Elf_Data *sym_data; /* start of table */ 58 size_t sym_symn; /* number of entries */ 59 char *sym_strs; /* ptr to strings */ 60 size_t sym_strsz; /* size of string table */ 61 GElf_Shdr sym_hdr; /* symbol table section header */ 62 GElf_Shdr sym_strhdr; /* string table section header */ 63 Elf *sym_elf; /* faked-up elf handle from core file */ 64 void *sym_elfmem; /* data for faked-up elf handle */ 65 uint_t *sym_byname; /* symbols sorted by name */ 66 uint_t *sym_byaddr; /* symbols sorted by addr */ 67 size_t sym_count; /* number of symbols in each sorted list */ 68 } sym_tbl_t; 69 70 typedef struct file_info { /* symbol information for a mapped file */ 71 plist_t file_list; /* linked list */ 72 char file_pname[PRMAPSZ]; /* name from prmap_t */ 73 struct map_info *file_map; /* primary (text) mapping */ 74 int file_ref; /* references from map_info_t structures */ 75 int file_fd; /* file descriptor for the mapped file */ 76 int file_init; /* 0: initialization yet to be performed */ 77 GElf_Half file_etype; /* ELF e_type from ehdr */ 78 GElf_Half file_class; /* ELF e_ident[EI_CLASS] from ehdr */ 79 rd_loadobj_t *file_lo; /* load object structure from rtld_db */ 80 char *file_lname; /* load object name from rtld_db */ 81 char *file_lbase; /* pointer to basename of file_lname */ 82 Elf *file_elf; /* elf handle so we can close */ 83 void *file_elfmem; /* data for faked-up elf handle */ 84 sym_tbl_t file_symtab; /* symbol table */ 85 sym_tbl_t file_dynsym; /* dynamic symbol table */ 86 uintptr_t file_dyn_base; /* load address for ET_DYN files */ 87 uintptr_t file_plt_base; /* base address for PLT */ 88 size_t file_plt_size; /* size of PLT region */ 89 uintptr_t file_jmp_rel; /* base address of PLT relocations */ 90 uintptr_t file_ctf_off; /* offset of CTF data in object file */ 91 size_t file_ctf_size; /* size of CTF data in object file */ 92 int file_ctf_dyn; /* does the CTF data reference the dynsym */ 93 void *file_ctf_buf; /* CTF data for this file */ 94 ctf_file_t *file_ctfp; /* CTF container for this file */ 95 } file_info_t; 96 97 typedef struct map_info { /* description of an address space mapping */ 98 prmap_t map_pmap; /* /proc description of this mapping */ 99 file_info_t *map_file; /* pointer into list of mapped files */ 100 off64_t map_offset; /* offset into core file (if core) */ 101 int map_relocate; /* associated file_map needs to be relocated */ 102 } map_info_t; 103 104 typedef struct lwp_info { /* per-lwp information from core file */ 105 plist_t lwp_list; /* linked list */ 106 lwpid_t lwp_id; /* lwp identifier */ 107 lwpsinfo_t lwp_psinfo; /* /proc/<pid>/lwp/<lwpid>/lwpsinfo data */ 108 lwpstatus_t lwp_status; /* /proc/<pid>/lwp/<lwpid>/lwpstatus data */ 109 #if defined(sparc) || defined(__sparc) 110 gwindows_t *lwp_gwins; /* /proc/<pid>/lwp/<lwpid>/gwindows data */ 111 prxregset_t *lwp_xregs; /* /proc/<pid>/lwp/<lwpid>/xregs data */ 112 int64_t *lwp_asrs; /* /proc/<pid>/lwp/<lwpid>/asrs data */ 113 #endif 114 } lwp_info_t; 115 116 typedef struct core_info { /* information specific to core files */ 117 char core_dmodel; /* data model for core file */ 118 int core_errno; /* error during initialization if != 0 */ 119 plist_t core_lwp_head; /* head of list of lwp info */ 120 lwp_info_t *core_lwp; /* current lwp information */ 121 uint_t core_nlwp; /* number of lwp's in list */ 122 off64_t core_size; /* size of core file in bytes */ 123 char *core_platform; /* platform string from core file */ 124 struct utsname *core_uts; /* uname(2) data from core file */ 125 prcred_t *core_cred; /* process credential from core file */ 126 core_content_t core_content; /* content dumped to core file */ 127 prpriv_t *core_priv; /* process privileges from core file */ 128 size_t core_priv_size; /* size of the privileges */ 129 void *core_privinfo; /* system privileges info from core file */ 130 priv_impl_info_t *core_ppii; /* NOTE entry for core_privinfo */ 131 char *core_zonename; /* zone name from core file */ 132 #if defined(__i386) || defined(__amd64) 133 struct ssd *core_ldt; /* LDT entries from core file */ 134 uint_t core_nldt; /* number of LDT entries in core file */ 135 #endif 136 } core_info_t; 137 138 typedef struct elf_file { /* convenience for managing ELF files */ 139 GElf_Ehdr e_hdr; /* ELF file header information */ 140 Elf *e_elf; /* ELF library handle */ 141 int e_fd; /* file descriptor */ 142 } elf_file_t; 143 144 typedef struct ps_rwops { /* ops vector for Pread() and Pwrite() */ 145 ssize_t (*p_pread)(struct ps_prochandle *, 146 void *, size_t, uintptr_t); 147 ssize_t (*p_pwrite)(struct ps_prochandle *, 148 const void *, size_t, uintptr_t); 149 } ps_rwops_t; 150 151 #define HASHSIZE 1024 /* hash table size, power of 2 */ 152 153 struct ps_prochandle { 154 struct ps_lwphandle **hashtab; /* hash table for LWPs (Lgrab()) */ 155 mutex_t proc_lock; /* protects hash table; serializes Lgrab() */ 156 pstatus_t orig_status; /* remembered status on Pgrab() */ 157 pstatus_t status; /* status when stopped */ 158 psinfo_t psinfo; /* psinfo_t from last Ppsinfo() request */ 159 uintptr_t sysaddr; /* address of most recent syscall instruction */ 160 pid_t pid; /* process-ID */ 161 int state; /* state of the process, see "libproc.h" */ 162 uint_t flags; /* see defines below */ 163 uint_t agentcnt; /* Pcreate_agent()/Pdestroy_agent() ref count */ 164 int asfd; /* /proc/<pid>/as filedescriptor */ 165 int ctlfd; /* /proc/<pid>/ctl filedescriptor */ 166 int statfd; /* /proc/<pid>/status filedescriptor */ 167 int agentctlfd; /* /proc/<pid>/lwp/agent/ctl */ 168 int agentstatfd; /* /proc/<pid>/lwp/agent/status */ 169 int info_valid; /* if zero, map and file info need updating */ 170 map_info_t *mappings; /* cached process mappings */ 171 size_t map_count; /* number of mappings */ 172 size_t map_alloc; /* number of mappings allocated */ 173 uint_t num_files; /* number of file elements in file_info */ 174 plist_t file_head; /* head of mapped files w/ symbol table info */ 175 char *execname; /* name of the executable file */ 176 auxv_t *auxv; /* the process's aux vector */ 177 int nauxv; /* number of aux vector entries */ 178 rd_agent_t *rap; /* cookie for rtld_db */ 179 map_info_t *map_exec; /* the mapping for the executable file */ 180 map_info_t *map_ldso; /* the mapping for ld.so.1 */ 181 const ps_rwops_t *ops; /* pointer to ops-vector for read and write */ 182 core_info_t *core; /* information specific to core (if PS_DEAD) */ 183 uintptr_t *ucaddrs; /* ucontext-list addresses */ 184 uint_t ucnelems; /* number of elements in the ucaddrs list */ 185 }; 186 187 /* flags */ 188 #define CREATED 0x01 /* process was created by Pcreate() */ 189 #define SETSIG 0x02 /* set signal trace mask before continuing */ 190 #define SETFAULT 0x04 /* set fault trace mask before continuing */ 191 #define SETENTRY 0x08 /* set sysentry trace mask before continuing */ 192 #define SETEXIT 0x10 /* set sysexit trace mask before continuing */ 193 #define SETHOLD 0x20 /* set signal hold mask before continuing */ 194 #define SETREGS 0x40 /* set registers before continuing */ 195 196 struct ps_lwphandle { 197 struct ps_prochandle *lwp_proc; /* process to which this lwp belongs */ 198 struct ps_lwphandle *lwp_hash; /* hash table linked list */ 199 lwpstatus_t lwp_status; /* status when stopped */ 200 lwpsinfo_t lwp_psinfo; /* lwpsinfo_t from last Lpsinfo() */ 201 lwpid_t lwp_id; /* lwp identifier */ 202 int lwp_state; /* state of the lwp, see "libproc.h" */ 203 uint_t lwp_flags; /* SETHOLD and/or SETREGS */ 204 int lwp_ctlfd; /* /proc/<pid>/lwp/<lwpid>/lwpctl */ 205 int lwp_statfd; /* /proc/<pid>/lwp/<lwpid>/lwpstatus */ 206 }; 207 208 /* 209 * Implementation functions in the process control library. 210 * These are not exported to clients of the library. 211 */ 212 extern void prldump(const char *, lwpstatus_t *); 213 extern int dupfd(int, int); 214 extern int set_minfd(void); 215 extern int Pscantext(struct ps_prochandle *); 216 extern void Pinitsym(struct ps_prochandle *); 217 extern void Preadauxvec(struct ps_prochandle *); 218 extern void optimize_symtab(sym_tbl_t *); 219 extern void Pbuild_file_symtab(struct ps_prochandle *, file_info_t *); 220 extern ctf_file_t *Pbuild_file_ctf(struct ps_prochandle *, file_info_t *); 221 extern map_info_t *Paddr2mptr(struct ps_prochandle *, uintptr_t); 222 extern char *Pfindexec(struct ps_prochandle *, const char *, 223 int (*)(const char *, void *), void *); 224 extern int getlwpstatus(struct ps_prochandle *, lwpid_t, lwpstatus_t *); 225 int Pstopstatus(struct ps_prochandle *, long, uint32_t); 226 227 extern int Padd_mapping(struct ps_prochandle *, off64_t, file_info_t *, 228 prmap_t *); 229 extern void Psort_mappings(struct ps_prochandle *); 230 231 232 /* 233 * Architecture-dependent definition of the breakpoint instruction. 234 */ 235 #if defined(sparc) || defined(__sparc) 236 #define BPT ((instr_t)0x91d02001) 237 #elif defined(__i386) || defined(__amd64) 238 #define BPT ((instr_t)0xcc) 239 #endif 240 241 /* 242 * Simple convenience. 243 */ 244 #define TRUE 1 245 #define FALSE 0 246 247 #ifdef __cplusplus 248 } 249 #endif 250 251 #endif /* _PCONTROL_H */ 252