xref: /illumos-gate/usr/src/lib/libproc/common/Pcontrol.h (revision 6918308b4f7a98b1ab7146af715a679a95b03288)
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 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 } 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_header { /* extended ELF header */
139 	unsigned char e_ident[EI_NIDENT];
140 	Elf64_Half e_type;
141 	Elf64_Half e_machine;
142 	Elf64_Word e_version;
143 	Elf64_Addr e_entry;
144 	Elf64_Off e_phoff;
145 	Elf64_Off e_shoff;
146 	Elf64_Word e_flags;
147 	Elf64_Half e_ehsize;
148 	Elf64_Half e_phentsize;
149 	Elf64_Half e_shentsize;
150 	Elf64_Word e_phnum;	/* phdr count extended to 32 bits */
151 	Elf64_Word e_shnum;	/* shdr count extended to 32 bits */
152 	Elf64_Word e_shstrndx;	/* shdr string index extended to 32 bits */
153 } elf_file_header_t;
154 
155 typedef struct elf_file {	/* convenience for managing ELF files */
156 	elf_file_header_t e_hdr; /* Extended ELF header */
157 	Elf *e_elf;		/* ELF library handle */
158 	int e_fd;		/* file descriptor */
159 } elf_file_t;
160 
161 typedef struct ps_rwops {	/* ops vector for Pread() and Pwrite() */
162 	ssize_t (*p_pread)(struct ps_prochandle *,
163 	    void *, size_t, uintptr_t);
164 	ssize_t (*p_pwrite)(struct ps_prochandle *,
165 	    const void *, size_t, uintptr_t);
166 } ps_rwops_t;
167 
168 #define	HASHSIZE		1024	/* hash table size, power of 2 */
169 
170 struct ps_prochandle {
171 	struct ps_lwphandle **hashtab;	/* hash table for LWPs (Lgrab()) */
172 	mutex_t	proc_lock;	/* protects hash table; serializes Lgrab() */
173 	pstatus_t orig_status;	/* remembered status on Pgrab() */
174 	pstatus_t status;	/* status when stopped */
175 	psinfo_t psinfo;	/* psinfo_t from last Ppsinfo() request */
176 	uintptr_t sysaddr;	/* address of most recent syscall instruction */
177 	pid_t	pid;		/* process-ID */
178 	int	state;		/* state of the process, see "libproc.h" */
179 	uint_t	flags;		/* see defines below */
180 	uint_t	agentcnt;	/* Pcreate_agent()/Pdestroy_agent() ref count */
181 	int	asfd;		/* /proc/<pid>/as filedescriptor */
182 	int	ctlfd;		/* /proc/<pid>/ctl filedescriptor */
183 	int	statfd;		/* /proc/<pid>/status filedescriptor */
184 	int	agentctlfd;	/* /proc/<pid>/lwp/agent/ctl */
185 	int	agentstatfd;	/* /proc/<pid>/lwp/agent/status */
186 	int	info_valid;	/* if zero, map and file info need updating */
187 	map_info_t *mappings;	/* cached process mappings */
188 	size_t	map_count;	/* number of mappings */
189 	size_t	map_alloc;	/* number of mappings allocated */
190 	uint_t	num_files;	/* number of file elements in file_info */
191 	plist_t	file_head;	/* head of mapped files w/ symbol table info */
192 	char	*execname;	/* name of the executable file */
193 	auxv_t	*auxv;		/* the process's aux vector */
194 	int	nauxv;		/* number of aux vector entries */
195 	rd_agent_t *rap;	/* cookie for rtld_db */
196 	map_info_t *map_exec;	/* the mapping for the executable file */
197 	map_info_t *map_ldso;	/* the mapping for ld.so.1 */
198 	const ps_rwops_t *ops;	/* pointer to ops-vector for read and write */
199 	core_info_t *core;	/* information specific to core (if PS_DEAD) */
200 	uintptr_t *ucaddrs;	/* ucontext-list addresses */
201 	uint_t	ucnelems;	/* number of elements in the ucaddrs list */
202 };
203 
204 /* flags */
205 #define	CREATED		0x01	/* process was created by Pcreate() */
206 #define	SETSIG		0x02	/* set signal trace mask before continuing */
207 #define	SETFAULT	0x04	/* set fault trace mask before continuing */
208 #define	SETENTRY	0x08	/* set sysentry trace mask before continuing */
209 #define	SETEXIT		0x10	/* set sysexit trace mask before continuing */
210 #define	SETHOLD		0x20	/* set signal hold mask before continuing */
211 #define	SETREGS		0x40	/* set registers before continuing */
212 
213 struct ps_lwphandle {
214 	struct ps_prochandle *lwp_proc;	/* process to which this lwp belongs */
215 	struct ps_lwphandle *lwp_hash;	/* hash table linked list */
216 	lwpstatus_t	lwp_status;	/* status when stopped */
217 	lwpsinfo_t	lwp_psinfo;	/* lwpsinfo_t from last Lpsinfo() */
218 	lwpid_t		lwp_id;		/* lwp identifier */
219 	int		lwp_state;	/* state of the lwp, see "libproc.h" */
220 	uint_t		lwp_flags;	/* SETHOLD and/or SETREGS */
221 	int		lwp_ctlfd;	/* /proc/<pid>/lwp/<lwpid>/lwpctl */
222 	int		lwp_statfd;	/* /proc/<pid>/lwp/<lwpid>/lwpstatus */
223 };
224 
225 /*
226  * Implementation functions in the process control library.
227  * These are not exported to clients of the library.
228  */
229 extern	void	prldump(const char *, lwpstatus_t *);
230 extern	int	dupfd(int, int);
231 extern	int	set_minfd(void);
232 extern	int	Pscantext(struct ps_prochandle *);
233 extern	void	Pinitsym(struct ps_prochandle *);
234 extern	void	Preadauxvec(struct ps_prochandle *);
235 extern	void	optimize_symtab(sym_tbl_t *);
236 extern	void	Pbuild_file_symtab(struct ps_prochandle *, file_info_t *);
237 extern	ctf_file_t *Pbuild_file_ctf(struct ps_prochandle *, file_info_t *);
238 extern	map_info_t *Paddr2mptr(struct ps_prochandle *, uintptr_t);
239 extern	char 	*Pfindexec(struct ps_prochandle *, const char *,
240 	int (*)(const char *, void *), void *);
241 extern	int	getlwpstatus(struct ps_prochandle *, lwpid_t, lwpstatus_t *);
242 int	Pstopstatus(struct ps_prochandle *, long, uint32_t);
243 
244 extern	int	Padd_mapping(struct ps_prochandle *, off64_t, file_info_t *,
245     prmap_t *);
246 extern	void	Psort_mappings(struct ps_prochandle *);
247 
248 
249 /*
250  * Architecture-dependent definition of the breakpoint instruction.
251  */
252 #if defined(sparc) || defined(__sparc)
253 #define	BPT	((instr_t)0x91d02001)
254 #elif defined(__i386) || defined(__amd64)
255 #define	BPT	((instr_t)0xcc)
256 #endif
257 
258 /*
259  * Simple convenience.
260  */
261 #define	TRUE	1
262 #define	FALSE	0
263 
264 #ifdef	__cplusplus
265 }
266 #endif
267 
268 #endif	/* _PCONTROL_H */
269