xref: /freebsd/lib/libproc/libproc.h (revision 5577b8a709c072fedc73dde9b3ad730699438bf7)
12c633af4SJohn Birrell /*-
25e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
35e53a4f9SPedro F. Giffuni  *
48eb20f36SRui Paulo  * Copyright (c) 2010 The FreeBSD Foundation
52c633af4SJohn Birrell  * Copyright (c) 2008 John Birrell (jb@freebsd.org)
62c633af4SJohn Birrell  * All rights reserved.
72c633af4SJohn Birrell  *
88eb20f36SRui Paulo  * Portions of this software were developed by Rui Paulo under sponsorship
98eb20f36SRui Paulo  * from the FreeBSD Foundation.
108eb20f36SRui Paulo  *
112c633af4SJohn Birrell  * Redistribution and use in source and binary forms, with or without
122c633af4SJohn Birrell  * modification, are permitted provided that the following conditions
132c633af4SJohn Birrell  * are met:
142c633af4SJohn Birrell  * 1. Redistributions of source code must retain the above copyright
152c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer.
162c633af4SJohn Birrell  * 2. Redistributions in binary form must reproduce the above copyright
172c633af4SJohn Birrell  *    notice, this list of conditions and the following disclaimer in the
182c633af4SJohn Birrell  *    documentation and/or other materials provided with the distribution.
192c633af4SJohn Birrell  *
202c633af4SJohn Birrell  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
212c633af4SJohn Birrell  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222c633af4SJohn Birrell  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232c633af4SJohn Birrell  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
242c633af4SJohn Birrell  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
252c633af4SJohn Birrell  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
262c633af4SJohn Birrell  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
272c633af4SJohn Birrell  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
282c633af4SJohn Birrell  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
292c633af4SJohn Birrell  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
302c633af4SJohn Birrell  * SUCH DAMAGE.
312c633af4SJohn Birrell  *
322c633af4SJohn Birrell  * $FreeBSD$
332c633af4SJohn Birrell  */
342c633af4SJohn Birrell 
352c633af4SJohn Birrell #ifndef	_LIBPROC_H_
362c633af4SJohn Birrell #define	_LIBPROC_H_
372c633af4SJohn Birrell 
382c633af4SJohn Birrell #include <gelf.h>
398eb20f36SRui Paulo #include <rtld_db.h>
408eb20f36SRui Paulo #include <limits.h>
412c633af4SJohn Birrell 
4241da933cSMark Johnston struct ctf_file;
432c633af4SJohn Birrell struct proc_handle;
442c633af4SJohn Birrell 
45820e0679SCraig Rodrigues typedef void (*proc_child_func)(void *);
46820e0679SCraig Rodrigues 
472c633af4SJohn Birrell /* Values returned by proc_state(). */
482c633af4SJohn Birrell #define PS_IDLE		1
492c633af4SJohn Birrell #define PS_STOP		2
502c633af4SJohn Birrell #define PS_RUN		3
512c633af4SJohn Birrell #define PS_UNDEAD	4
522c633af4SJohn Birrell #define PS_DEAD		5
532c633af4SJohn Birrell #define PS_LOST		6
542c633af4SJohn Birrell 
55b043b5dcSMark Johnston /* Flags for proc_attach(). */
56b043b5dcSMark Johnston #define	PATTACH_FORCE	0x01
57b043b5dcSMark Johnston #define	PATTACH_RDONLY	0x02
58b043b5dcSMark Johnston #define	PATTACH_NOSTOP	0x04
59b043b5dcSMark Johnston 
608eb20f36SRui Paulo /* Reason values for proc_detach(). */
618eb20f36SRui Paulo #define PRELEASE_HANG	1
628eb20f36SRui Paulo #define PRELEASE_KILL	2
638eb20f36SRui Paulo 
642c633af4SJohn Birrell typedef struct prmap {
652c633af4SJohn Birrell 	uintptr_t	pr_vaddr;	/* Virtual address. */
668eb20f36SRui Paulo 	size_t		pr_size;	/* Mapping size in bytes */
678eb20f36SRui Paulo 	size_t		pr_offset;	/* Mapping offset in object */
688eb20f36SRui Paulo 	char		pr_mapname[PATH_MAX];	/* Mapping filename */
698eb20f36SRui Paulo 	uint8_t		pr_mflags;	/* Protection flags */
708eb20f36SRui Paulo #define	MA_READ		0x01
718eb20f36SRui Paulo #define	MA_WRITE	0x02
728eb20f36SRui Paulo #define	MA_EXEC		0x04
738eb20f36SRui Paulo #define	MA_COW		0x08
748eb20f36SRui Paulo #define MA_NEEDS_COPY	0x10
758eb20f36SRui Paulo #define	MA_NOCOREDUMP	0x20
762c633af4SJohn Birrell } prmap_t;
772c633af4SJohn Birrell 
7841da933cSMark Johnston typedef struct prsyminfo {
7941da933cSMark Johnston 	u_int		prs_lmid;	/* Map id. */
8041da933cSMark Johnston 	u_int		prs_id;		/* Symbol id. */
8141da933cSMark Johnston } prsyminfo_t;
8241da933cSMark Johnston 
838eb20f36SRui Paulo typedef int proc_map_f(void *, const prmap_t *, const char *);
848eb20f36SRui Paulo typedef int proc_sym_f(void *, const GElf_Sym *, const char *);
858eb20f36SRui Paulo 
868eb20f36SRui Paulo /* Values for ELF sections */
878eb20f36SRui Paulo #define	PR_SYMTAB	1
888eb20f36SRui Paulo #define PR_DYNSYM	2
898eb20f36SRui Paulo 
908eb20f36SRui Paulo /* Values for the 'mask' parameter in the iteration functions */
918eb20f36SRui Paulo #define	BIND_LOCAL	0x0001
928eb20f36SRui Paulo #define BIND_GLOBAL	0x0002
938eb20f36SRui Paulo #define BIND_WEAK	0x0004
948eb20f36SRui Paulo #define BIND_ANY	(BIND_LOCAL|BIND_GLOBAL|BIND_WEAK)
958eb20f36SRui Paulo #define TYPE_NOTYPE	0x0100
968eb20f36SRui Paulo #define TYPE_OBJECT	0x0200
978eb20f36SRui Paulo #define TYPE_FUNC	0x0400
988eb20f36SRui Paulo #define TYPE_SECTION	0x0800
998eb20f36SRui Paulo #define TYPE_FILE	0x1000
1008eb20f36SRui Paulo #define TYPE_ANY	(TYPE_NOTYPE|TYPE_OBJECT|TYPE_FUNC|TYPE_SECTION|\
1018eb20f36SRui Paulo     			 TYPE_FILE)
1028eb20f36SRui Paulo 
1038eb20f36SRui Paulo typedef enum {
1048eb20f36SRui Paulo 	REG_PC,
1058eb20f36SRui Paulo 	REG_SP,
1068eb20f36SRui Paulo 	REG_RVAL1,
1078eb20f36SRui Paulo 	REG_RVAL2
1088eb20f36SRui Paulo } proc_reg_t;
1098eb20f36SRui Paulo 
1108eb20f36SRui Paulo #define SIG2STR_MAX	8
1118eb20f36SRui Paulo 
1128eb20f36SRui Paulo typedef struct lwpstatus {
1138eb20f36SRui Paulo 	int pr_why;
1148eb20f36SRui Paulo #define PR_REQUESTED	1
1158eb20f36SRui Paulo #define PR_FAULTED	2
1168eb20f36SRui Paulo #define PR_SYSENTRY	3
1178eb20f36SRui Paulo #define PR_SYSEXIT	4
11892f92525SMark Johnston #define PR_SIGNALLED	5
1198eb20f36SRui Paulo 	int pr_what;
1208eb20f36SRui Paulo #define FLTBPT		-1
1218eb20f36SRui Paulo } lwpstatus_t;
1228eb20f36SRui Paulo 
1234808a678SMark Johnston #define	PR_MODEL_ILP32	1
1244808a678SMark Johnston #define	PR_MODEL_LP64	2
1254808a678SMark Johnston 
126b1bb30e5SMark Johnston struct proc_handle_public {
127b1bb30e5SMark Johnston 	pid_t		pid;
128b1bb30e5SMark Johnston };
129b1bb30e5SMark Johnston 
130b1bb30e5SMark Johnston #define	proc_getpid(phdl)	(((struct proc_handle_public *)(phdl))->pid)
131b1bb30e5SMark Johnston 
1322c633af4SJohn Birrell /* Function prototype definitions. */
1332c633af4SJohn Birrell __BEGIN_DECLS
1342c633af4SJohn Birrell 
1358eb20f36SRui Paulo prmap_t *proc_addr2map(struct proc_handle *, uintptr_t);
1368eb20f36SRui Paulo prmap_t *proc_name2map(struct proc_handle *, const char *);
1372c633af4SJohn Birrell char	*proc_objname(struct proc_handle *, uintptr_t, char *, size_t);
1388eb20f36SRui Paulo int	proc_iter_objs(struct proc_handle *, proc_map_f *, void *);
1398eb20f36SRui Paulo int	proc_iter_symbyaddr(struct proc_handle *, const char *, int,
1408eb20f36SRui Paulo 	    int, proc_sym_f *, void *);
1412c633af4SJohn Birrell int	proc_addr2sym(struct proc_handle *, uintptr_t, char *, size_t, GElf_Sym *);
1422c633af4SJohn Birrell int	proc_attach(pid_t pid, int flags, struct proc_handle **pphdl);
1432c633af4SJohn Birrell int	proc_continue(struct proc_handle *);
1442c633af4SJohn Birrell int	proc_clearflags(struct proc_handle *, int);
145*5577b8a7SMark Johnston int	proc_create(const char *, char * const *, char * const *,
146*5577b8a7SMark Johnston 	    proc_child_func *, void *, struct proc_handle **);
1478eb20f36SRui Paulo int	proc_detach(struct proc_handle *, int);
1482c633af4SJohn Birrell int	proc_getflags(struct proc_handle *);
14941da933cSMark Johnston int	proc_name2sym(struct proc_handle *, const char *, const char *,
15041da933cSMark Johnston 	    GElf_Sym *, prsyminfo_t *);
15141da933cSMark Johnston struct ctf_file *proc_name2ctf(struct proc_handle *, const char *);
1522c633af4SJohn Birrell int	proc_setflags(struct proc_handle *, int);
1532c633af4SJohn Birrell int	proc_state(struct proc_handle *);
1544808a678SMark Johnston int	proc_getmodel(struct proc_handle *);
1558eb20f36SRui Paulo int	proc_wstatus(struct proc_handle *);
1568eb20f36SRui Paulo int	proc_getwstat(struct proc_handle *);
1578eb20f36SRui Paulo char *	proc_signame(int, char *, size_t);
1584c74b245SRui Paulo int	proc_read(struct proc_handle *, void *, size_t, size_t);
15941da933cSMark Johnston const lwpstatus_t *proc_getlwpstatus(struct proc_handle *);
1602c633af4SJohn Birrell void	proc_free(struct proc_handle *);
1618eb20f36SRui Paulo rd_agent_t *proc_rdagent(struct proc_handle *);
1628eb20f36SRui Paulo void	proc_updatesyms(struct proc_handle *);
1638eb20f36SRui Paulo int	proc_bkptset(struct proc_handle *, uintptr_t, unsigned long *);
1648eb20f36SRui Paulo int	proc_bkptdel(struct proc_handle *, uintptr_t, unsigned long);
1658eb20f36SRui Paulo void	proc_bkptregadj(unsigned long *);
1668eb20f36SRui Paulo int	proc_bkptexec(struct proc_handle *, unsigned long);
1678eb20f36SRui Paulo int	proc_regget(struct proc_handle *, proc_reg_t, unsigned long *);
1688eb20f36SRui Paulo int	proc_regset(struct proc_handle *, proc_reg_t, unsigned long);
1692c633af4SJohn Birrell 
1702c633af4SJohn Birrell __END_DECLS
1712c633af4SJohn Birrell 
172fcf9fc10SMark Johnston #endif /* _LIBPROC_H_ */
173