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