xref: /titanic_50/usr/src/lib/libproc/common/Pcontrol.h (revision 14ea4bb737263733ad80a36b4f73f681c30a6b45)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_PCONTROL_H
27 #define	_PCONTROL_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * Implemention-specific include file for libproc process management.
33  * This is not to be seen by the clients of libproc.
34  */
35 
36 #include <stdio.h>
37 #include <gelf.h>
38 #include <synch.h>
39 #include <procfs.h>
40 #include <rtld_db.h>
41 #include <libproc.h>
42 #include <libctf.h>
43 #include <limits.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 sym_tbl {	/* 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 	char	*file_shstrs;	/* section header string table */
96 	size_t	file_shstrsz;	/* section header string table size */
97 	uintptr_t *file_saddrs; /* section header addresses */
98 	uint_t  file_nsaddrs;   /* number of section header addresses */
99 } file_info_t;
100 
101 typedef struct map_info {	/* description of an address space mapping */
102 	prmap_t	map_pmap;	/* /proc description of this mapping */
103 	file_info_t *map_file;	/* pointer into list of mapped files */
104 	off64_t map_offset;	/* offset into core file (if core) */
105 	int map_relocate;	/* associated file_map needs to be relocated */
106 } map_info_t;
107 
108 typedef struct lwp_info {	/* per-lwp information from core file */
109 	plist_t	lwp_list;	/* linked list */
110 	lwpid_t	lwp_id;		/* lwp identifier */
111 	lwpsinfo_t lwp_psinfo;	/* /proc/<pid>/lwp/<lwpid>/lwpsinfo data */
112 	lwpstatus_t lwp_status;	/* /proc/<pid>/lwp/<lwpid>/lwpstatus data */
113 #if defined(sparc) || defined(__sparc)
114 	gwindows_t *lwp_gwins;	/* /proc/<pid>/lwp/<lwpid>/gwindows data */
115 	prxregset_t *lwp_xregs;	/* /proc/<pid>/lwp/<lwpid>/xregs data */
116 	int64_t *lwp_asrs;	/* /proc/<pid>/lwp/<lwpid>/asrs data */
117 #endif
118 } lwp_info_t;
119 
120 typedef struct core_info {	/* information specific to core files */
121 	char core_dmodel;	/* data model for core file */
122 	int core_errno;		/* error during initialization if != 0 */
123 	plist_t core_lwp_head;	/* head of list of lwp info */
124 	lwp_info_t *core_lwp;	/* current lwp information */
125 	uint_t core_nlwp;	/* number of lwp's in list */
126 	off64_t core_size;	/* size of core file in bytes */
127 	char *core_platform;	/* platform string from core file */
128 	struct utsname *core_uts;	/* uname(2) data from core file */
129 	prcred_t *core_cred;	/* process credential from core file */
130 	core_content_t core_content;	/* content dumped to core file */
131 	prpriv_t *core_priv;	/* process privileges from core file */
132 	size_t core_priv_size;	/* size of the privileges */
133 	void *core_privinfo;	/* system privileges info from core file */
134 	priv_impl_info_t *core_ppii;	/* NOTE entry for core_privinfo */
135 	char *core_zonename;	/* zone name from core file */
136 #if defined(__i386) || defined(__amd64)
137 	struct ssd *core_ldt;	/* LDT entries from core file */
138 	uint_t core_nldt;	/* number of LDT entries in core file */
139 #endif
140 } core_info_t;
141 
142 typedef struct elf_file_header { /* extended ELF header */
143 	unsigned char e_ident[EI_NIDENT];
144 	Elf64_Half e_type;
145 	Elf64_Half e_machine;
146 	Elf64_Word e_version;
147 	Elf64_Addr e_entry;
148 	Elf64_Off e_phoff;
149 	Elf64_Off e_shoff;
150 	Elf64_Word e_flags;
151 	Elf64_Half e_ehsize;
152 	Elf64_Half e_phentsize;
153 	Elf64_Half e_shentsize;
154 	Elf64_Word e_phnum;	/* phdr count extended to 32 bits */
155 	Elf64_Word e_shnum;	/* shdr count extended to 32 bits */
156 	Elf64_Word e_shstrndx;	/* shdr string index extended to 32 bits */
157 } elf_file_header_t;
158 
159 typedef struct elf_file {	/* convenience for managing ELF files */
160 	elf_file_header_t e_hdr; /* Extended ELF header */
161 	Elf *e_elf;		/* ELF library handle */
162 	int e_fd;		/* file descriptor */
163 } elf_file_t;
164 
165 typedef struct ps_rwops {	/* ops vector for Pread() and Pwrite() */
166 	ssize_t (*p_pread)(struct ps_prochandle *,
167 	    void *, size_t, uintptr_t);
168 	ssize_t (*p_pwrite)(struct ps_prochandle *,
169 	    const void *, size_t, uintptr_t);
170 } ps_rwops_t;
171 
172 #define	HASHSIZE		1024	/* hash table size, power of 2 */
173 
174 struct ps_prochandle {
175 	struct ps_lwphandle **hashtab;	/* hash table for LWPs (Lgrab()) */
176 	mutex_t	proc_lock;	/* protects hash table; serializes Lgrab() */
177 	pstatus_t orig_status;	/* remembered status on Pgrab() */
178 	pstatus_t status;	/* status when stopped */
179 	psinfo_t psinfo;	/* psinfo_t from last Ppsinfo() request */
180 	uintptr_t sysaddr;	/* address of most recent syscall instruction */
181 	pid_t	pid;		/* process-ID */
182 	int	state;		/* state of the process, see "libproc.h" */
183 	uint_t	flags;		/* see defines below */
184 	uint_t	agentcnt;	/* Pcreate_agent()/Pdestroy_agent() ref count */
185 	int	asfd;		/* /proc/<pid>/as filedescriptor */
186 	int	ctlfd;		/* /proc/<pid>/ctl filedescriptor */
187 	int	statfd;		/* /proc/<pid>/status filedescriptor */
188 	int	agentctlfd;	/* /proc/<pid>/lwp/agent/ctl */
189 	int	agentstatfd;	/* /proc/<pid>/lwp/agent/status */
190 	int	info_valid;	/* if zero, map and file info need updating */
191 	map_info_t *mappings;	/* cached process mappings */
192 	size_t	map_count;	/* number of mappings */
193 	size_t	map_alloc;	/* number of mappings allocated */
194 	uint_t	num_files;	/* number of file elements in file_info */
195 	plist_t	file_head;	/* head of mapped files w/ symbol table info */
196 	char	*execname;	/* name of the executable file */
197 	auxv_t	*auxv;		/* the process's aux vector */
198 	int	nauxv;		/* number of aux vector entries */
199 	rd_agent_t *rap;	/* cookie for rtld_db */
200 	map_info_t *map_exec;	/* the mapping for the executable file */
201 	map_info_t *map_ldso;	/* the mapping for ld.so.1 */
202 	const ps_rwops_t *ops;	/* pointer to ops-vector for read and write */
203 	core_info_t *core;	/* information specific to core (if PS_DEAD) */
204 	uintptr_t *ucaddrs;	/* ucontext-list addresses */
205 	uint_t	ucnelems;	/* number of elements in the ucaddrs list */
206 };
207 
208 /* flags */
209 #define	CREATED		0x01	/* process was created by Pcreate() */
210 #define	SETSIG		0x02	/* set signal trace mask before continuing */
211 #define	SETFAULT	0x04	/* set fault trace mask before continuing */
212 #define	SETENTRY	0x08	/* set sysentry trace mask before continuing */
213 #define	SETEXIT		0x10	/* set sysexit trace mask before continuing */
214 #define	SETHOLD		0x20	/* set signal hold mask before continuing */
215 #define	SETREGS		0x40	/* set registers before continuing */
216 
217 struct ps_lwphandle {
218 	struct ps_prochandle *lwp_proc;	/* process to which this lwp belongs */
219 	struct ps_lwphandle *lwp_hash;	/* hash table linked list */
220 	lwpstatus_t	lwp_status;	/* status when stopped */
221 	lwpsinfo_t	lwp_psinfo;	/* lwpsinfo_t from last Lpsinfo() */
222 	lwpid_t		lwp_id;		/* lwp identifier */
223 	int		lwp_state;	/* state of the lwp, see "libproc.h" */
224 	uint_t		lwp_flags;	/* SETHOLD and/or SETREGS */
225 	int		lwp_ctlfd;	/* /proc/<pid>/lwp/<lwpid>/lwpctl */
226 	int		lwp_statfd;	/* /proc/<pid>/lwp/<lwpid>/lwpstatus */
227 };
228 
229 /*
230  * Implementation functions in the process control library.
231  * These are not exported to clients of the library.
232  */
233 extern	void	prldump(const char *, lwpstatus_t *);
234 extern	int	dupfd(int, int);
235 extern	int	set_minfd(void);
236 extern	int	Pscantext(struct ps_prochandle *);
237 extern	void	Pinitsym(struct ps_prochandle *);
238 extern	void	Preadauxvec(struct ps_prochandle *);
239 extern	void	optimize_symtab(sym_tbl_t *);
240 extern	void	Pbuild_file_symtab(struct ps_prochandle *, file_info_t *);
241 extern	ctf_file_t *Pbuild_file_ctf(struct ps_prochandle *, file_info_t *);
242 extern	map_info_t *Paddr2mptr(struct ps_prochandle *, uintptr_t);
243 extern	char 	*Pfindexec(struct ps_prochandle *, const char *,
244 	int (*)(const char *, void *), void *);
245 extern	int	getlwpstatus(struct ps_prochandle *, lwpid_t, lwpstatus_t *);
246 int	Pstopstatus(struct ps_prochandle *, long, uint32_t);
247 
248 extern	int	Padd_mapping(struct ps_prochandle *, off64_t, file_info_t *,
249     prmap_t *);
250 extern	void	Psort_mappings(struct ps_prochandle *);
251 
252 extern char	procfs_path[PATH_MAX];
253 
254 /*
255  * Architecture-dependent definition of the breakpoint instruction.
256  */
257 #if defined(sparc) || defined(__sparc)
258 #define	BPT	((instr_t)0x91d02001)
259 #elif defined(__i386) || defined(__amd64)
260 #define	BPT	((instr_t)0xcc)
261 #endif
262 
263 /*
264  * Simple convenience.
265  */
266 #define	TRUE	1
267 #define	FALSE	0
268 
269 #ifdef	__cplusplus
270 }
271 #endif
272 
273 #endif	/* _PCONTROL_H */
274