xref: /freebsd/lib/libproc/libproc.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
12c633af4SJohn Birrell /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 
332c633af4SJohn Birrell #ifndef	_LIBPROC_H_
342c633af4SJohn Birrell #define	_LIBPROC_H_
352c633af4SJohn Birrell 
362c633af4SJohn Birrell #include <gelf.h>
378eb20f36SRui Paulo #include <rtld_db.h>
388eb20f36SRui Paulo #include <limits.h>
392c633af4SJohn Birrell 
4041da933cSMark Johnston struct ctf_file;
412c633af4SJohn Birrell struct proc_handle;
422c633af4SJohn Birrell 
43820e0679SCraig Rodrigues typedef void (*proc_child_func)(void *);
44820e0679SCraig Rodrigues 
452c633af4SJohn Birrell /* Values returned by proc_state(). */
462c633af4SJohn Birrell #define PS_IDLE		1
472c633af4SJohn Birrell #define PS_STOP		2
482c633af4SJohn Birrell #define PS_RUN		3
492c633af4SJohn Birrell #define PS_UNDEAD	4
502c633af4SJohn Birrell #define PS_DEAD		5
512c633af4SJohn Birrell #define PS_LOST		6
522c633af4SJohn Birrell 
53b043b5dcSMark Johnston /* Flags for proc_attach(). */
54b043b5dcSMark Johnston #define	PATTACH_FORCE	0x01
55b043b5dcSMark Johnston #define	PATTACH_RDONLY	0x02
56b043b5dcSMark Johnston #define	PATTACH_NOSTOP	0x04
57b043b5dcSMark Johnston 
588eb20f36SRui Paulo /* Reason values for proc_detach(). */
598eb20f36SRui Paulo #define PRELEASE_HANG	1
608eb20f36SRui Paulo #define PRELEASE_KILL	2
618eb20f36SRui Paulo 
622c633af4SJohn Birrell typedef struct prmap {
632c633af4SJohn Birrell 	uintptr_t	pr_vaddr;	/* Virtual address. */
648eb20f36SRui Paulo 	size_t		pr_size;	/* Mapping size in bytes */
658eb20f36SRui Paulo 	size_t		pr_offset;	/* Mapping offset in object */
668eb20f36SRui Paulo 	char		pr_mapname[PATH_MAX];	/* Mapping filename */
678eb20f36SRui Paulo 	uint8_t		pr_mflags;	/* Protection flags */
688eb20f36SRui Paulo #define	MA_READ		0x01
698eb20f36SRui Paulo #define	MA_WRITE	0x02
708eb20f36SRui Paulo #define	MA_EXEC		0x04
718eb20f36SRui Paulo #define	MA_COW		0x08
728eb20f36SRui Paulo #define MA_NEEDS_COPY	0x10
738eb20f36SRui Paulo #define	MA_NOCOREDUMP	0x20
742c633af4SJohn Birrell } prmap_t;
752c633af4SJohn Birrell 
7641da933cSMark Johnston typedef struct prsyminfo {
7741da933cSMark Johnston 	u_int		prs_lmid;	/* Map id. */
7841da933cSMark Johnston 	u_int		prs_id;		/* Symbol id. */
7941da933cSMark Johnston } prsyminfo_t;
8041da933cSMark Johnston 
818eb20f36SRui Paulo typedef int proc_map_f(void *, const prmap_t *, const char *);
828eb20f36SRui Paulo typedef int proc_sym_f(void *, const GElf_Sym *, const char *);
838eb20f36SRui Paulo 
848eb20f36SRui Paulo /* Values for ELF sections */
858eb20f36SRui Paulo #define	PR_SYMTAB	1
868eb20f36SRui Paulo #define PR_DYNSYM	2
878eb20f36SRui Paulo 
888eb20f36SRui Paulo /* Values for the 'mask' parameter in the iteration functions */
898eb20f36SRui Paulo #define	BIND_LOCAL	0x0001
908eb20f36SRui Paulo #define BIND_GLOBAL	0x0002
918eb20f36SRui Paulo #define BIND_WEAK	0x0004
928eb20f36SRui Paulo #define BIND_ANY	(BIND_LOCAL|BIND_GLOBAL|BIND_WEAK)
938eb20f36SRui Paulo #define TYPE_NOTYPE	0x0100
948eb20f36SRui Paulo #define TYPE_OBJECT	0x0200
958eb20f36SRui Paulo #define TYPE_FUNC	0x0400
968eb20f36SRui Paulo #define TYPE_SECTION	0x0800
978eb20f36SRui Paulo #define TYPE_FILE	0x1000
988eb20f36SRui Paulo #define TYPE_ANY	(TYPE_NOTYPE|TYPE_OBJECT|TYPE_FUNC|TYPE_SECTION|\
998eb20f36SRui Paulo     			 TYPE_FILE)
1008eb20f36SRui Paulo 
1018eb20f36SRui Paulo typedef enum {
1028eb20f36SRui Paulo 	REG_PC,
1038eb20f36SRui Paulo 	REG_SP,
1048eb20f36SRui Paulo 	REG_RVAL1,
1058eb20f36SRui Paulo 	REG_RVAL2
1068eb20f36SRui Paulo } proc_reg_t;
1078eb20f36SRui Paulo 
1088eb20f36SRui Paulo #define SIG2STR_MAX	8
1098eb20f36SRui Paulo 
1108eb20f36SRui Paulo typedef struct lwpstatus {
1118eb20f36SRui Paulo 	int pr_why;
112*1112883eSEric van Gyzen #define PR_REQUESTED	1	/* not implemented */
1138eb20f36SRui Paulo #define PR_FAULTED	2
1148eb20f36SRui Paulo #define PR_SYSENTRY	3
1158eb20f36SRui Paulo #define PR_SYSEXIT	4
11692f92525SMark Johnston #define PR_SIGNALLED	5
1178eb20f36SRui Paulo 	int pr_what;
1188eb20f36SRui Paulo #define FLTBPT		-1
1198eb20f36SRui Paulo } lwpstatus_t;
1208eb20f36SRui Paulo 
1214808a678SMark Johnston #define	PR_MODEL_ILP32	1
1224808a678SMark Johnston #define	PR_MODEL_LP64	2
1234808a678SMark Johnston 
124b1bb30e5SMark Johnston struct proc_handle_public {
125b1bb30e5SMark Johnston 	pid_t		pid;
126b1bb30e5SMark Johnston };
127b1bb30e5SMark Johnston 
128b1bb30e5SMark Johnston #define	proc_getpid(phdl)	(((struct proc_handle_public *)(phdl))->pid)
129b1bb30e5SMark Johnston 
1302c633af4SJohn Birrell /* Function prototype definitions. */
1312c633af4SJohn Birrell __BEGIN_DECLS
1322c633af4SJohn Birrell 
1338eb20f36SRui Paulo prmap_t *proc_addr2map(struct proc_handle *, uintptr_t);
1348eb20f36SRui Paulo prmap_t *proc_name2map(struct proc_handle *, const char *);
1352c633af4SJohn Birrell char	*proc_objname(struct proc_handle *, uintptr_t, char *, size_t);
1368eb20f36SRui Paulo int	proc_iter_objs(struct proc_handle *, proc_map_f *, void *);
1378eb20f36SRui Paulo int	proc_iter_symbyaddr(struct proc_handle *, const char *, int,
1388eb20f36SRui Paulo 	    int, proc_sym_f *, void *);
1392c633af4SJohn Birrell int	proc_addr2sym(struct proc_handle *, uintptr_t, char *, size_t, GElf_Sym *);
1402c633af4SJohn Birrell int	proc_attach(pid_t pid, int flags, struct proc_handle **pphdl);
1412c633af4SJohn Birrell int	proc_continue(struct proc_handle *);
1422c633af4SJohn Birrell int	proc_clearflags(struct proc_handle *, int);
1435577b8a7SMark Johnston int	proc_create(const char *, char * const *, char * const *,
1445577b8a7SMark Johnston 	    proc_child_func *, void *, struct proc_handle **);
1458eb20f36SRui Paulo int	proc_detach(struct proc_handle *, int);
1462c633af4SJohn Birrell int	proc_getflags(struct proc_handle *);
14741da933cSMark Johnston int	proc_name2sym(struct proc_handle *, const char *, const char *,
14841da933cSMark Johnston 	    GElf_Sym *, prsyminfo_t *);
14941da933cSMark Johnston struct ctf_file *proc_name2ctf(struct proc_handle *, const char *);
1502c633af4SJohn Birrell int	proc_setflags(struct proc_handle *, int);
1512c633af4SJohn Birrell int	proc_state(struct proc_handle *);
1524808a678SMark Johnston int	proc_getmodel(struct proc_handle *);
1538eb20f36SRui Paulo int	proc_wstatus(struct proc_handle *);
1548eb20f36SRui Paulo int	proc_getwstat(struct proc_handle *);
1558eb20f36SRui Paulo char *	proc_signame(int, char *, size_t);
1564c74b245SRui Paulo int	proc_read(struct proc_handle *, void *, size_t, size_t);
15741da933cSMark Johnston const lwpstatus_t *proc_getlwpstatus(struct proc_handle *);
1582c633af4SJohn Birrell void	proc_free(struct proc_handle *);
1598eb20f36SRui Paulo rd_agent_t *proc_rdagent(struct proc_handle *);
1608eb20f36SRui Paulo void	proc_updatesyms(struct proc_handle *);
1618eb20f36SRui Paulo int	proc_bkptset(struct proc_handle *, uintptr_t, unsigned long *);
1628eb20f36SRui Paulo int	proc_bkptdel(struct proc_handle *, uintptr_t, unsigned long);
1638eb20f36SRui Paulo void	proc_bkptregadj(unsigned long *);
1648eb20f36SRui Paulo int	proc_bkptexec(struct proc_handle *, unsigned long);
1658eb20f36SRui Paulo int	proc_regget(struct proc_handle *, proc_reg_t, unsigned long *);
1668eb20f36SRui Paulo int	proc_regset(struct proc_handle *, proc_reg_t, unsigned long);
1672c633af4SJohn Birrell 
1682c633af4SJohn Birrell __END_DECLS
1692c633af4SJohn Birrell 
170fcf9fc10SMark Johnston #endif /* _LIBPROC_H_ */
171