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