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