xref: /titanic_41/usr/src/lib/libproc/common/libproc.h (revision cde2885fdf538266ee2a3b08dee2d5075ce8fa2b)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Portions Copyright 2007 Chad Mynhier
27  */
28 
29 /*
30  * Interfaces available from the process control library, libproc.
31  *
32  * libproc provides process control functions for the /proc tools
33  * (commands in /usr/proc/bin), /usr/bin/truss, and /usr/bin/gcore.
34  * libproc is a private support library for these commands only.
35  * It is _not_ a public interface, although it might become one
36  * in the fullness of time, when the interfaces settle down.
37  *
38  * In the meantime, be aware that any program linked with libproc in this
39  * release of Solaris is almost guaranteed to break in the next release.
40  *
41  * In short, do not use this header file or libproc for any purpose.
42  */
43 
44 #ifndef	_LIBPROC_H
45 #define	_LIBPROC_H
46 
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <nlist.h>
51 #include <door.h>
52 #include <gelf.h>
53 #include <proc_service.h>
54 #include <rtld_db.h>
55 #include <procfs.h>
56 #include <rctl.h>
57 #include <libctf.h>
58 #include <sys/stat.h>
59 #include <sys/statvfs.h>
60 #include <sys/auxv.h>
61 #include <sys/resource.h>
62 #include <sys/socket.h>
63 #include <sys/utsname.h>
64 #include <sys/corectl.h>
65 #if defined(__i386) || defined(__amd64)
66 #include <sys/sysi86.h>
67 #endif
68 
69 #ifdef	__cplusplus
70 extern "C" {
71 #endif
72 
73 /*
74  * Opaque structure tag reference to a process control structure.
75  * Clients of libproc cannot look inside the process control structure.
76  * The implementation of struct ps_prochandle can change w/o affecting clients.
77  */
78 struct ps_prochandle;
79 
80 /*
81  * Opaque structure tag reference to an lwp control structure.
82  */
83 struct ps_lwphandle;
84 
85 extern	int	_libproc_debug;	/* set non-zero to enable debugging fprintfs */
86 extern	int	_libproc_no_qsort;	/* set non-zero to inhibit sorting */
87 					/* of symbol tables */
88 
89 #if defined(__sparc)
90 #define	R_RVAL1	R_O0		/* register holding a function return value */
91 #define	R_RVAL2	R_O1		/* 32 more bits for a 64-bit return value */
92 #endif	/* __sparc */
93 
94 #if defined(__amd64)
95 #define	R_PC	REG_RIP
96 #define	R_SP	REG_RSP
97 #define	R_RVAL1	REG_RAX		/* register holding a function return value */
98 #define	R_RVAL2	REG_RDX		/* 32 more bits for a 64-bit return value */
99 #elif defined(__i386)
100 #define	R_PC	EIP
101 #define	R_SP	UESP
102 #define	R_RVAL1	EAX		/* register holding a function return value */
103 #define	R_RVAL2	EDX		/* 32 more bits for a 64-bit return value */
104 #endif	/* __amd64 || __i386 */
105 
106 #define	R_RVAL	R_RVAL1		/* simple function return value register */
107 
108 /* maximum sizes of things */
109 #define	PRMAXSIG	(32 * sizeof (sigset_t) / sizeof (uint32_t))
110 #define	PRMAXFAULT	(32 * sizeof (fltset_t) / sizeof (uint32_t))
111 #define	PRMAXSYS	(32 * sizeof (sysset_t) / sizeof (uint32_t))
112 
113 /* State values returned by Pstate() */
114 #define	PS_RUN		1	/* process is running */
115 #define	PS_STOP		2	/* process is stopped */
116 #define	PS_LOST		3	/* process is lost to control (EAGAIN) */
117 #define	PS_UNDEAD	4	/* process is terminated (zombie) */
118 #define	PS_DEAD		5	/* process is terminated (core file) */
119 #define	PS_IDLE		6	/* process has not been run */
120 
121 /* Flags accepted by Pgrab() */
122 #define	PGRAB_RETAIN	0x01	/* Retain tracing flags, else clear flags */
123 #define	PGRAB_FORCE	0x02	/* Open the process w/o O_EXCL */
124 #define	PGRAB_RDONLY	0x04	/* Open the process or core w/ O_RDONLY */
125 #define	PGRAB_NOSTOP	0x08	/* Open the process but do not stop it */
126 
127 /* Error codes from Pcreate() */
128 #define	C_STRANGE	-1	/* Unanticipated error, errno is meaningful */
129 #define	C_FORK		1	/* Unable to fork */
130 #define	C_PERM		2	/* No permission (file set-id or unreadable) */
131 #define	C_NOEXEC	3	/* Cannot execute file */
132 #define	C_INTR		4	/* Interrupt received while creating */
133 #define	C_LP64		5	/* Program is _LP64, self is _ILP32 */
134 #define	C_NOENT		6	/* Cannot find executable file */
135 
136 /* Error codes from Pgrab(), Pfgrab_core(), and Pgrab_core() */
137 #define	G_STRANGE	-1	/* Unanticipated error, errno is meaningful */
138 #define	G_NOPROC	1	/* No such process */
139 #define	G_NOCORE	2	/* No such core file */
140 #define	G_NOPROCORCORE	3	/* No such proc or core (for proc_arg_grab) */
141 #define	G_NOEXEC	4	/* Cannot locate executable file */
142 #define	G_ZOMB		5	/* Zombie process */
143 #define	G_PERM		6	/* No permission */
144 #define	G_BUSY		7	/* Another process has control */
145 #define	G_SYS		8	/* System process */
146 #define	G_SELF		9	/* Process is self */
147 #define	G_INTR		10	/* Interrupt received while grabbing */
148 #define	G_LP64		11	/* Process is _LP64, self is ILP32 */
149 #define	G_FORMAT	12	/* File is not an ELF format core file */
150 #define	G_ELF		13	/* Libelf error, elf_errno() is meaningful */
151 #define	G_NOTE		14	/* Required PT_NOTE Phdr not present in core */
152 #define	G_ISAINVAL	15	/* Wrong ELF machine type */
153 #define	G_BADLWPS	16	/* Bad '/lwps' specification */
154 #define	G_NOFD		17	/* No more file descriptors */
155 
156 
157 /* Flags accepted by Prelease */
158 #define	PRELEASE_CLEAR	0x10	/* Clear all tracing flags */
159 #define	PRELEASE_RETAIN	0x20	/* Retain final tracing flags */
160 #define	PRELEASE_HANG	0x40	/* Leave the process stopped */
161 #define	PRELEASE_KILL	0x80	/* Terminate the process */
162 
163 typedef	struct {	/* argument descriptor for system call (Psyscall) */
164 	long	arg_value;	/* value of argument given to system call */
165 	void	*arg_object;	/* pointer to object in controlling process */
166 	char	arg_type;	/* AT_BYVAL, AT_BYREF */
167 	char	arg_inout;	/* AI_INPUT, AI_OUTPUT, AI_INOUT */
168 	ushort_t arg_size;	/* if AT_BYREF, size of object in bytes */
169 } argdes_t;
170 
171 /* values for type */
172 #define	AT_BYVAL	1
173 #define	AT_BYREF	2
174 
175 /* values for inout */
176 #define	AI_INPUT	1
177 #define	AI_OUTPUT	2
178 #define	AI_INOUT	3
179 
180 /* maximum number of syscall arguments */
181 #define	MAXARGS		8
182 
183 /* maximum size in bytes of a BYREF argument */
184 #define	MAXARGL		(4*1024)
185 
186 /*
187  * Function prototypes for routines in the process control package.
188  */
189 extern struct ps_prochandle *Pcreate(const char *, char *const *,
190     int *, char *, size_t);
191 extern struct ps_prochandle *Pxcreate(const char *, char *const *,
192     char *const *, int *, char *, size_t);
193 
194 extern const char *Pcreate_error(int);
195 
196 extern struct ps_prochandle *Pgrab(pid_t, int, int *);
197 extern struct ps_prochandle *Pgrab_core(const char *, const char *, int, int *);
198 extern struct ps_prochandle *Pfgrab_core(int, const char *, int *);
199 extern struct ps_prochandle *Pgrab_file(const char *, int *);
200 extern const char *Pgrab_error(int);
201 
202 extern	int	Preopen(struct ps_prochandle *);
203 extern	void	Prelease(struct ps_prochandle *, int);
204 extern	void	Pfree(struct ps_prochandle *);
205 
206 extern	int	Pasfd(struct ps_prochandle *);
207 extern	char   *Pbrandname(struct ps_prochandle *, char *, size_t);
208 extern	int	Pctlfd(struct ps_prochandle *);
209 extern	int	Pcreate_agent(struct ps_prochandle *);
210 extern	void	Pdestroy_agent(struct ps_prochandle *);
211 extern	int	Pstopstatus(struct ps_prochandle *, long, uint_t);
212 extern	int	Pwait(struct ps_prochandle *, uint_t);
213 extern	int	Pstop(struct ps_prochandle *, uint_t);
214 extern	int	Pdstop(struct ps_prochandle *);
215 extern	int	Pstate(struct ps_prochandle *);
216 extern	const psinfo_t *Ppsinfo(struct ps_prochandle *);
217 extern	const pstatus_t *Pstatus(struct ps_prochandle *);
218 extern	int	Pcred(struct ps_prochandle *, prcred_t *, int);
219 extern	int	Psetcred(struct ps_prochandle *, const prcred_t *);
220 extern	ssize_t	Ppriv(struct ps_prochandle *, prpriv_t *, size_t);
221 extern	int	Psetpriv(struct ps_prochandle *, prpriv_t *);
222 extern	void   *Pprivinfo(struct ps_prochandle *);
223 extern	int	Psetzoneid(struct ps_prochandle *, zoneid_t);
224 extern	int	Pgetareg(struct ps_prochandle *, int, prgreg_t *);
225 extern	int	Pputareg(struct ps_prochandle *, int, prgreg_t);
226 extern	int	Psetrun(struct ps_prochandle *, int, int);
227 extern	ssize_t	Pread(struct ps_prochandle *, void *, size_t, uintptr_t);
228 extern	ssize_t Pread_string(struct ps_prochandle *, char *, size_t, uintptr_t);
229 extern	ssize_t	Pwrite(struct ps_prochandle *, const void *, size_t, uintptr_t);
230 extern	int	Pclearsig(struct ps_prochandle *);
231 extern	int	Pclearfault(struct ps_prochandle *);
232 extern	int	Psetbkpt(struct ps_prochandle *, uintptr_t, ulong_t *);
233 extern	int	Pdelbkpt(struct ps_prochandle *, uintptr_t, ulong_t);
234 extern	int	Pxecbkpt(struct ps_prochandle *, ulong_t);
235 extern	int	Psetwapt(struct ps_prochandle *, const prwatch_t *);
236 extern	int	Pdelwapt(struct ps_prochandle *, const prwatch_t *);
237 extern	int	Pxecwapt(struct ps_prochandle *, const prwatch_t *);
238 extern	int	Psetflags(struct ps_prochandle *, long);
239 extern	int	Punsetflags(struct ps_prochandle *, long);
240 extern	int	Psignal(struct ps_prochandle *, int, int);
241 extern	int	Pfault(struct ps_prochandle *, int, int);
242 extern	int	Psysentry(struct ps_prochandle *, int, int);
243 extern	int	Psysexit(struct ps_prochandle *, int, int);
244 extern	void	Psetsignal(struct ps_prochandle *, const sigset_t *);
245 extern	void	Psetfault(struct ps_prochandle *, const fltset_t *);
246 extern	void	Psetsysentry(struct ps_prochandle *, const sysset_t *);
247 extern	void	Psetsysexit(struct ps_prochandle *, const sysset_t *);
248 
249 extern	void	Psync(struct ps_prochandle *);
250 extern	int	Psyscall(struct ps_prochandle *, sysret_t *,
251 			int, uint_t, argdes_t *);
252 extern	int	Pisprocdir(struct ps_prochandle *, const char *);
253 
254 /*
255  * Function prototypes for lwp-specific operations.
256  */
257 extern	struct ps_lwphandle *Lgrab(struct ps_prochandle *, lwpid_t, int *);
258 extern	const char *Lgrab_error(int);
259 
260 extern	struct ps_prochandle *Lprochandle(struct ps_lwphandle *);
261 extern	void	Lfree(struct ps_lwphandle *);
262 
263 extern	int	Lctlfd(struct ps_lwphandle *);
264 extern	int	Lwait(struct ps_lwphandle *, uint_t);
265 extern	int	Lstop(struct ps_lwphandle *, uint_t);
266 extern	int	Ldstop(struct ps_lwphandle *);
267 extern	int	Lstate(struct ps_lwphandle *);
268 extern	const lwpsinfo_t *Lpsinfo(struct ps_lwphandle *);
269 extern	const lwpstatus_t *Lstatus(struct ps_lwphandle *);
270 extern	int	Lgetareg(struct ps_lwphandle *, int, prgreg_t *);
271 extern	int	Lputareg(struct ps_lwphandle *, int, prgreg_t);
272 extern	int	Lsetrun(struct ps_lwphandle *, int, int);
273 extern	int	Lclearsig(struct ps_lwphandle *);
274 extern	int	Lclearfault(struct ps_lwphandle *);
275 extern	int	Lxecbkpt(struct ps_lwphandle *, ulong_t);
276 extern	int	Lxecwapt(struct ps_lwphandle *, const prwatch_t *);
277 extern	void	Lsync(struct ps_lwphandle *);
278 
279 extern	int	Lstack(struct ps_lwphandle *, stack_t *);
280 extern	int	Lmain_stack(struct ps_lwphandle *, stack_t *);
281 extern	int	Lalt_stack(struct ps_lwphandle *, stack_t *);
282 
283 /*
284  * Function prototypes for system calls forced on the victim process.
285  */
286 extern	int	pr_open(struct ps_prochandle *, const char *, int, mode_t);
287 extern	int	pr_creat(struct ps_prochandle *, const char *, mode_t);
288 extern	int	pr_close(struct ps_prochandle *, int);
289 extern	int	pr_access(struct ps_prochandle *, const char *, int);
290 extern	int	pr_door_info(struct ps_prochandle *, int, struct door_info *);
291 extern	void	*pr_mmap(struct ps_prochandle *,
292 			void *, size_t, int, int, int, off_t);
293 extern	void	*pr_zmap(struct ps_prochandle *,
294 			void *, size_t, int, int);
295 extern	int	pr_munmap(struct ps_prochandle *, void *, size_t);
296 extern	int	pr_memcntl(struct ps_prochandle *,
297 			caddr_t, size_t, int, caddr_t, int, int);
298 extern	int	pr_meminfo(struct ps_prochandle *, const uint64_t *addrs,
299 			int addr_count, const uint_t *info, int info_count,
300 			uint64_t *outdata, uint_t *validity);
301 extern	int	pr_sigaction(struct ps_prochandle *,
302 			int, const struct sigaction *, struct sigaction *);
303 extern	int	pr_getitimer(struct ps_prochandle *,
304 			int, struct itimerval *);
305 extern	int	pr_setitimer(struct ps_prochandle *,
306 			int, const struct itimerval *, struct itimerval *);
307 extern	int	pr_ioctl(struct ps_prochandle *, int, int, void *, size_t);
308 extern	int	pr_fcntl(struct ps_prochandle *, int, int, void *);
309 extern	int	pr_stat(struct ps_prochandle *, const char *, struct stat *);
310 extern	int	pr_lstat(struct ps_prochandle *, const char *, struct stat *);
311 extern	int	pr_fstat(struct ps_prochandle *, int, struct stat *);
312 extern	int	pr_stat64(struct ps_prochandle *, const char *,
313 			struct stat64 *);
314 extern	int	pr_lstat64(struct ps_prochandle *, const char *,
315 			struct stat64 *);
316 extern	int	pr_fstat64(struct ps_prochandle *, int, struct stat64 *);
317 extern	int	pr_statvfs(struct ps_prochandle *, const char *, statvfs_t *);
318 extern	int	pr_fstatvfs(struct ps_prochandle *, int, statvfs_t *);
319 extern	projid_t pr_getprojid(struct ps_prochandle *Pr);
320 extern	taskid_t pr_gettaskid(struct ps_prochandle *Pr);
321 extern	taskid_t pr_settaskid(struct ps_prochandle *Pr, projid_t project,
322 			int flags);
323 extern	zoneid_t pr_getzoneid(struct ps_prochandle *Pr);
324 extern	int	pr_getrctl(struct ps_prochandle *,
325 			const char *, rctlblk_t *, rctlblk_t *, int);
326 extern	int	pr_setrctl(struct ps_prochandle *,
327 			const char *, rctlblk_t *, rctlblk_t *, int);
328 extern	int	pr_getrlimit(struct ps_prochandle *,
329 			int, struct rlimit *);
330 extern	int	pr_setrlimit(struct ps_prochandle *,
331 			int, const struct rlimit *);
332 extern	int	pr_setprojrctl(struct ps_prochandle *, const char *,
333 			rctlblk_t *, size_t, int);
334 #if defined(_LARGEFILE64_SOURCE)
335 extern	int	pr_getrlimit64(struct ps_prochandle *,
336 			int, struct rlimit64 *);
337 extern	int	pr_setrlimit64(struct ps_prochandle *,
338 			int, const struct rlimit64 *);
339 #endif	/* _LARGEFILE64_SOURCE */
340 extern	int	pr_lwp_exit(struct ps_prochandle *);
341 extern	int	pr_exit(struct ps_prochandle *, int);
342 extern	int	pr_waitid(struct ps_prochandle *,
343 			idtype_t, id_t, siginfo_t *, int);
344 extern	off_t	pr_lseek(struct ps_prochandle *, int, off_t, int);
345 extern	offset_t pr_llseek(struct ps_prochandle *, int, offset_t, int);
346 extern	int	pr_rename(struct ps_prochandle *, const char *, const char *);
347 extern	int	pr_link(struct ps_prochandle *, const char *, const char *);
348 extern	int	pr_unlink(struct ps_prochandle *, const char *);
349 extern	int	pr_getpeername(struct ps_prochandle *,
350 			int, struct sockaddr *, socklen_t *);
351 extern	int	pr_getsockname(struct ps_prochandle *,
352 			int, struct sockaddr *, socklen_t *);
353 extern	int	pr_getsockopt(struct ps_prochandle *,
354 			int, int, int, void *, int *);
355 extern	int	pr_processor_bind(struct ps_prochandle *,
356 			idtype_t, id_t, int, int *);
357 extern	int	pr_pset_bind(struct ps_prochandle *,
358 			int, idtype_t, id_t, int *);
359 
360 /*
361  * Function prototypes for accessing per-LWP register information.
362  */
363 extern int Plwp_getregs(struct ps_prochandle *, lwpid_t, prgregset_t);
364 extern int Plwp_setregs(struct ps_prochandle *, lwpid_t, const prgregset_t);
365 
366 extern int Plwp_getfpregs(struct ps_prochandle *, lwpid_t, prfpregset_t *);
367 extern int Plwp_setfpregs(struct ps_prochandle *, lwpid_t,
368     const prfpregset_t *);
369 
370 #if defined(__sparc)
371 
372 extern int Plwp_getxregs(struct ps_prochandle *, lwpid_t, prxregset_t *);
373 extern int Plwp_setxregs(struct ps_prochandle *, lwpid_t, const prxregset_t *);
374 
375 extern int Plwp_getgwindows(struct ps_prochandle *, lwpid_t, gwindows_t *);
376 
377 #if defined(__sparcv9)
378 extern int Plwp_getasrs(struct ps_prochandle *, lwpid_t, asrset_t);
379 extern int Plwp_setasrs(struct ps_prochandle *, lwpid_t, const asrset_t);
380 #endif	/* __sparcv9 */
381 
382 #endif	/* __sparc */
383 
384 #if defined(__i386) || defined(__amd64)
385 extern	int	Pldt(struct ps_prochandle *, struct ssd *, int);
386 extern	int	proc_get_ldt(pid_t, struct ssd *, int);
387 #endif	/* __i386 || __amd64 */
388 
389 extern int Plwp_getpsinfo(struct ps_prochandle *, lwpid_t, lwpsinfo_t *);
390 
391 extern int Plwp_stack(struct ps_prochandle *, lwpid_t, stack_t *);
392 extern int Plwp_main_stack(struct ps_prochandle *, lwpid_t, stack_t *);
393 extern int Plwp_alt_stack(struct ps_prochandle *, lwpid_t, stack_t *);
394 
395 /*
396  * LWP iteration interface; iterate over all active LWPs.
397  */
398 typedef int proc_lwp_f(void *, const lwpstatus_t *);
399 extern int Plwp_iter(struct ps_prochandle *, proc_lwp_f *, void *);
400 
401 /*
402  * LWP iteration interface; iterate over all LWPs, active and zombie.
403  */
404 typedef int proc_lwp_all_f(void *, const lwpstatus_t *, const lwpsinfo_t *);
405 extern int Plwp_iter_all(struct ps_prochandle *, proc_lwp_all_f *, void *);
406 
407 /*
408  * Process iteration interface; iterate over all non-system processes.
409  */
410 typedef int proc_walk_f(psinfo_t *, lwpsinfo_t *, void *);
411 extern int proc_walk(proc_walk_f *, void *, int);
412 
413 #define	PR_WALK_PROC	0		/* walk processes only */
414 #define	PR_WALK_LWP	1		/* walk all lwps */
415 
416 /*
417  * Determine if an lwp is in a set as returned from proc_arg_xgrab().
418  */
419 extern int proc_lwp_in_set(const char *, lwpid_t);
420 extern int proc_lwp_range_valid(const char *);
421 
422 /*
423  * Symbol table interfaces.
424  */
425 
426 /*
427  * Pseudo-names passed to Plookup_by_name() for well-known load objects.
428  * NOTE: It is required that PR_OBJ_EXEC and PR_OBJ_LDSO exactly match
429  * the definitions of PS_OBJ_EXEC and PS_OBJ_LDSO from <proc_service.h>.
430  */
431 #define	PR_OBJ_EXEC	((const char *)0)	/* search the executable file */
432 #define	PR_OBJ_LDSO	((const char *)1)	/* search ld.so.1 */
433 #define	PR_OBJ_EVERY	((const char *)-1)	/* search every load object */
434 
435 /*
436  * Special Lmid_t passed to Plookup_by_lmid() to search all link maps.  The
437  * special values LM_ID_BASE and LM_ID_LDSO from <link.h> may also be used.
438  * If PR_OBJ_EXEC is used as the object name, the lmid must be PR_LMID_EVERY
439  * or LM_ID_BASE in order to return a match.  If PR_OBJ_LDSO is used as the
440  * object name, the lmid must be PR_LMID_EVERY or LM_ID_LDSO to return a match.
441  */
442 #define	PR_LMID_EVERY	((Lmid_t)-1UL)		/* search every link map */
443 
444 /*
445  * 'object_name' is the name of a load object obtained from an
446  * iteration over the process's address space mappings (Pmapping_iter),
447  * or an iteration over the process's mapped objects (Pobject_iter),
448  * or else it is one of the special PR_OBJ_* values above.
449  */
450 extern int Plookup_by_name(struct ps_prochandle *,
451     const char *, const char *, GElf_Sym *);
452 
453 extern int Plookup_by_addr(struct ps_prochandle *,
454     uintptr_t, char *, size_t, GElf_Sym *);
455 
456 typedef struct prsyminfo {
457 	const char	*prs_object;		/* object name */
458 	const char	*prs_name;		/* symbol name */
459 	Lmid_t		prs_lmid;		/* link map id */
460 	uint_t		prs_id;			/* symbol id */
461 	uint_t		prs_table;		/* symbol table id */
462 } prsyminfo_t;
463 
464 extern int Pxlookup_by_name(struct ps_prochandle *,
465     Lmid_t, const char *, const char *, GElf_Sym *, prsyminfo_t *);
466 
467 extern int Pxlookup_by_addr(struct ps_prochandle *,
468     uintptr_t, char *, size_t, GElf_Sym *, prsyminfo_t *);
469 extern int Pxlookup_by_addr_resolved(struct ps_prochandle *,
470     uintptr_t, char *, size_t, GElf_Sym *, prsyminfo_t *);
471 
472 typedef int proc_map_f(void *, const prmap_t *, const char *);
473 
474 extern int Pmapping_iter(struct ps_prochandle *, proc_map_f *, void *);
475 extern int Pmapping_iter_resolved(struct ps_prochandle *, proc_map_f *, void *);
476 extern int Pobject_iter(struct ps_prochandle *, proc_map_f *, void *);
477 extern int Pobject_iter_resolved(struct ps_prochandle *, proc_map_f *, void *);
478 
479 extern const prmap_t *Paddr_to_map(struct ps_prochandle *, uintptr_t);
480 extern const prmap_t *Paddr_to_text_map(struct ps_prochandle *, uintptr_t);
481 extern const prmap_t *Pname_to_map(struct ps_prochandle *, const char *);
482 extern const prmap_t *Plmid_to_map(struct ps_prochandle *,
483     Lmid_t, const char *);
484 
485 extern const rd_loadobj_t *Paddr_to_loadobj(struct ps_prochandle *, uintptr_t);
486 extern const rd_loadobj_t *Pname_to_loadobj(struct ps_prochandle *,
487     const char *);
488 extern const rd_loadobj_t *Plmid_to_loadobj(struct ps_prochandle *,
489     Lmid_t, const char *);
490 
491 extern ctf_file_t *Paddr_to_ctf(struct ps_prochandle *, uintptr_t);
492 extern ctf_file_t *Pname_to_ctf(struct ps_prochandle *, const char *);
493 
494 extern char *Pplatform(struct ps_prochandle *, char *, size_t);
495 extern int Puname(struct ps_prochandle *, struct utsname *);
496 extern char *Pzonename(struct ps_prochandle *, char *, size_t);
497 extern char *Pfindobj(struct ps_prochandle *, const char *, char *, size_t);
498 
499 extern char *Pexecname(struct ps_prochandle *, char *, size_t);
500 extern char *Pobjname(struct ps_prochandle *, uintptr_t, char *, size_t);
501 extern char *Pobjname_resolved(struct ps_prochandle *, uintptr_t, char *,
502     size_t);
503 extern int Plmid(struct ps_prochandle *, uintptr_t, Lmid_t *);
504 
505 typedef int proc_env_f(void *, struct ps_prochandle *, uintptr_t, const char *);
506 extern	int Penv_iter(struct ps_prochandle *, proc_env_f *, void *);
507 extern char *Pgetenv(struct ps_prochandle *, const char *, char *, size_t);
508 extern long Pgetauxval(struct ps_prochandle *, int);
509 extern const auxv_t *Pgetauxvec(struct ps_prochandle *);
510 
511 extern void Pset_procfs_path(const char *);
512 
513 /*
514  * Symbol table iteration interface.  The special lmid constants LM_ID_BASE,
515  * LM_ID_LDSO, and PR_LMID_EVERY may be used with Psymbol_iter_by_lmid.
516  */
517 typedef int proc_sym_f(void *, const GElf_Sym *, const char *);
518 typedef int proc_xsym_f(void *, const GElf_Sym *, const char *,
519     const prsyminfo_t *);
520 
521 extern int Psymbol_iter(struct ps_prochandle *,
522     const char *, int, int, proc_sym_f *, void *);
523 extern int Psymbol_iter_by_addr(struct ps_prochandle *,
524     const char *, int, int, proc_sym_f *, void *);
525 extern int Psymbol_iter_by_name(struct ps_prochandle *,
526     const char *, int, int, proc_sym_f *, void *);
527 
528 extern int Psymbol_iter_by_lmid(struct ps_prochandle *,
529     Lmid_t, const char *, int, int, proc_sym_f *, void *);
530 
531 extern int Pxsymbol_iter(struct ps_prochandle *,
532     Lmid_t, const char *, int, int, proc_xsym_f *, void *);
533 
534 /*
535  * 'which' selects which symbol table and can be one of the following.
536  */
537 #define	PR_SYMTAB	1
538 #define	PR_DYNSYM	2
539 /*
540  * 'type' selects the symbols of interest by binding and type.  It is a bit-
541  * mask of one or more of the following flags, whose order MUST match the
542  * order of STB and STT constants in <sys/elf.h>.
543  */
544 #define	BIND_LOCAL	0x0001
545 #define	BIND_GLOBAL	0x0002
546 #define	BIND_WEAK	0x0004
547 #define	BIND_ANY (BIND_LOCAL|BIND_GLOBAL|BIND_WEAK)
548 #define	TYPE_NOTYPE	0x0100
549 #define	TYPE_OBJECT	0x0200
550 #define	TYPE_FUNC	0x0400
551 #define	TYPE_SECTION	0x0800
552 #define	TYPE_FILE	0x1000
553 #define	TYPE_ANY (TYPE_NOTYPE|TYPE_OBJECT|TYPE_FUNC|TYPE_SECTION|TYPE_FILE)
554 
555 /*
556  * This returns the rtld_db agent handle for the process.
557  * The handle will become invalid at the next successful exec() and
558  * must not be used beyond that point (see Preset_maps(), below).
559  */
560 extern rd_agent_t *Prd_agent(struct ps_prochandle *);
561 
562 /*
563  * This should be called when an RD_DLACTIVITY event with the
564  * RD_CONSISTENT state occurs via librtld_db's event mechanism.
565  * This makes libproc's address space mappings and symbol tables current.
566  * The variant Pupdate_syms() can be used to preload all symbol tables as well.
567  */
568 extern void Pupdate_maps(struct ps_prochandle *);
569 extern void Pupdate_syms(struct ps_prochandle *);
570 
571 /*
572  * This must be called after the victim process performs a successful
573  * exec() if any of the symbol table interface functions have been called
574  * prior to that point.  This is essential because an exec() invalidates
575  * all previous symbol table and address space mapping information.
576  * It is always safe to call, but if it is called other than after an
577  * exec() by the victim process it just causes unnecessary overhead.
578  *
579  * The rtld_db agent handle obtained from a previous call to Prd_agent() is
580  * made invalid by Preset_maps() and Prd_agent() must be called again to get
581  * the new handle.
582  */
583 extern void Preset_maps(struct ps_prochandle *);
584 
585 /*
586  * Given an address, Ppltdest() determines if this is part of a PLT, and if
587  * so returns a pointer to the symbol name that will be used for resolution.
588  * If the specified address is not part of a PLT, the function returns NULL.
589  */
590 extern const char *Ppltdest(struct ps_prochandle *, uintptr_t);
591 
592 /*
593  * See comments for Pissyscall(), in Pisadep.h
594  */
595 extern int Pissyscall_prev(struct ps_prochandle *, uintptr_t, uintptr_t *);
596 
597 /*
598  * Stack frame iteration interface.
599  */
600 typedef int proc_stack_f(void *, prgregset_t, uint_t, const long *);
601 
602 extern int Pstack_iter(struct ps_prochandle *,
603     const prgregset_t, proc_stack_f *, void *);
604 
605 /*
606  * The following functions define a set of passive interfaces: libproc provides
607  * default, empty definitions that are called internally.  If a client wishes
608  * to override these definitions, it can simply provide its own version with
609  * the same signature that interposes on the libproc definition.
610  *
611  * If the client program wishes to report additional error information, it
612  * can provide its own version of Perror_printf.
613  *
614  * If the client program wishes to receive a callback after Pcreate forks
615  * but before it execs, it can provide its own version of Pcreate_callback.
616  */
617 extern void Perror_printf(struct ps_prochandle *P, const char *format, ...);
618 extern void Pcreate_callback(struct ps_prochandle *);
619 
620 /*
621  * Remove unprintable characters from psinfo.pr_psargs and replace with
622  * whitespace characters so it is safe for printing.
623  */
624 extern void proc_unctrl_psinfo(psinfo_t *);
625 
626 /*
627  * Utility functions for processing arguments which should be /proc files,
628  * pids, and/or core files.  The returned error code can be passed to
629  * Pgrab_error() in order to convert it to an error string.
630  */
631 #define	PR_ARG_PIDS	0x1	/* Allow pid and /proc file arguments */
632 #define	PR_ARG_CORES	0x2	/* Allow core file arguments */
633 
634 #define	PR_ARG_ANY	(PR_ARG_PIDS | PR_ARG_CORES)
635 
636 extern struct ps_prochandle *proc_arg_grab(const char *, int, int, int *);
637 extern struct ps_prochandle *proc_arg_xgrab(const char *, const char *, int,
638     int, int *, const char **);
639 extern pid_t proc_arg_psinfo(const char *, int, psinfo_t *, int *);
640 extern pid_t proc_arg_xpsinfo(const char *, int, psinfo_t *, int *,
641     const char **);
642 
643 /*
644  * Utility functions for obtaining information via /proc without actually
645  * performing a Pcreate() or Pgrab():
646  */
647 extern int proc_get_auxv(pid_t, auxv_t *, int);
648 extern int proc_get_cred(pid_t, prcred_t *, int);
649 extern prpriv_t *proc_get_priv(pid_t);
650 extern int proc_get_psinfo(pid_t, psinfo_t *);
651 extern int proc_get_status(pid_t, pstatus_t *);
652 
653 /*
654  * Utility functions for debugging tools to convert numeric fault,
655  * signal, and system call numbers to symbolic names:
656  */
657 #define	FLT2STR_MAX 32	/* max. string length of faults (like SIG2STR_MAX) */
658 #define	SYS2STR_MAX 32	/* max. string length of syscalls (like SIG2STR_MAX) */
659 
660 extern char *proc_fltname(int, char *, size_t);
661 extern char *proc_signame(int, char *, size_t);
662 extern char *proc_sysname(int, char *, size_t);
663 
664 /*
665  * Utility functions for debugging tools to convert fault, signal, and system
666  * call names back to the numeric constants:
667  */
668 extern int proc_str2flt(const char *, int *);
669 extern int proc_str2sig(const char *, int *);
670 extern int proc_str2sys(const char *, int *);
671 
672 /*
673  * Utility functions for debugging tools to convert a fault, signal or system
674  * call set to a string representation (e.g. "BUS,SEGV" or "open,close,read").
675  */
676 #define	PRSIGBUFSZ	1024	/* buffer size for proc_sigset2str() */
677 
678 extern char *proc_fltset2str(const fltset_t *, const char *, int,
679     char *, size_t);
680 extern char *proc_sigset2str(const sigset_t *, const char *, int,
681     char *, size_t);
682 extern char *proc_sysset2str(const sysset_t *, const char *, int,
683     char *, size_t);
684 
685 extern int Pgcore(struct ps_prochandle *, const char *, core_content_t);
686 extern int Pfgcore(struct ps_prochandle *, int, core_content_t);
687 extern core_content_t Pcontent(struct ps_prochandle *);
688 
689 /*
690  * Utility functions for debugging tools to convert a string representation of
691  * a fault, signal or system call set back to the numeric value of the
692  * corresponding set type.
693  */
694 extern char *proc_str2fltset(const char *, const char *, int, fltset_t *);
695 extern char *proc_str2sigset(const char *, const char *, int, sigset_t *);
696 extern char *proc_str2sysset(const char *, const char *, int, sysset_t *);
697 
698 /*
699  * Utility functions for converting between strings and core_content_t.
700  */
701 #define	PRCONTENTBUFSZ	80	/* buffer size for proc_content2str() */
702 
703 extern int proc_str2content(const char *, core_content_t *);
704 extern int proc_content2str(core_content_t, char *, size_t);
705 
706 /*
707  * Utility functions for buffering output to stdout, stderr while
708  * process is grabbed.  Prevents deadlocks due to pfiles `pgrep xterm`
709  * and other varients.
710  */
711 extern int proc_initstdio(void);
712 extern int proc_flushstdio(void);
713 extern int proc_finistdio(void);
714 
715 #ifdef	__cplusplus
716 }
717 #endif
718 
719 #endif	/* _LIBPROC_H */
720