xref: /netbsd-src/external/bsd/libproc/dist/libproc.h (revision eec15c920c8c2d214c93cdb40df9ed1aa292c738)
1fbcd1dd1Schristos /*-
2fbcd1dd1Schristos  * Copyright (c) 2010 The FreeBSD Foundation
3fbcd1dd1Schristos  * Copyright (c) 2008 John Birrell (jb@freebsd.org)
4fbcd1dd1Schristos  * All rights reserved.
5fbcd1dd1Schristos  *
6fbcd1dd1Schristos  * Portions of this software were developed by Rui Paulo under sponsorship
7fbcd1dd1Schristos  * from the FreeBSD Foundation.
8fbcd1dd1Schristos  *
9fbcd1dd1Schristos  * Redistribution and use in source and binary forms, with or without
10fbcd1dd1Schristos  * modification, are permitted provided that the following conditions
11fbcd1dd1Schristos  * are met:
12fbcd1dd1Schristos  * 1. Redistributions of source code must retain the above copyright
13fbcd1dd1Schristos  *    notice, this list of conditions and the following disclaimer.
14fbcd1dd1Schristos  * 2. Redistributions in binary form must reproduce the above copyright
15fbcd1dd1Schristos  *    notice, this list of conditions and the following disclaimer in the
16fbcd1dd1Schristos  *    documentation and/or other materials provided with the distribution.
17fbcd1dd1Schristos  *
18fbcd1dd1Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19fbcd1dd1Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20fbcd1dd1Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21fbcd1dd1Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22fbcd1dd1Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23fbcd1dd1Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24fbcd1dd1Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25fbcd1dd1Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26fbcd1dd1Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27fbcd1dd1Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28fbcd1dd1Schristos  * SUCH DAMAGE.
29fbcd1dd1Schristos  *
30fbcd1dd1Schristos  * $FreeBSD: head/lib/libproc/libproc.h 272488 2014-10-03 23:20:37Z markj $
31fbcd1dd1Schristos  */
32fbcd1dd1Schristos 
33fbcd1dd1Schristos #ifndef	_LIBPROC_H_
34fbcd1dd1Schristos #define	_LIBPROC_H_
35fbcd1dd1Schristos 
36fbcd1dd1Schristos #include <gelf.h>
37fbcd1dd1Schristos #include <rtld_db.h>
38fbcd1dd1Schristos #include <limits.h>
399a58287bSchristos #include <sys/ptrace.h>
40fbcd1dd1Schristos 
41fbcd1dd1Schristos struct ctf_file;
42fbcd1dd1Schristos struct proc_handle;
43fbcd1dd1Schristos 
44fbcd1dd1Schristos typedef void (*proc_child_func)(void *);
45fbcd1dd1Schristos 
46fbcd1dd1Schristos /* Values returned by proc_state(). */
47fbcd1dd1Schristos #define PS_IDLE		1
48fbcd1dd1Schristos #define PS_STOP		2
49fbcd1dd1Schristos #define PS_RUN		3
50fbcd1dd1Schristos #define PS_UNDEAD	4
51fbcd1dd1Schristos #define PS_DEAD		5
52fbcd1dd1Schristos #define PS_LOST		6
53fbcd1dd1Schristos 
54*eec15c92Schs /* Flags for proc_attach(). */
55*eec15c92Schs #define	PATTACH_FORCE	0x01
56*eec15c92Schs #define	PATTACH_RDONLY	0x02
57*eec15c92Schs #define	PATTACH_NOSTOP	0x04
58*eec15c92Schs 
59fbcd1dd1Schristos /* Reason values for proc_detach(). */
60fbcd1dd1Schristos #define PRELEASE_HANG	1
61fbcd1dd1Schristos #define PRELEASE_KILL	2
62fbcd1dd1Schristos 
63fbcd1dd1Schristos typedef struct prmap {
64fbcd1dd1Schristos 	uintptr_t	pr_vaddr;	/* Virtual address. */
65fbcd1dd1Schristos 	size_t		pr_size;	/* Mapping size in bytes */
66fbcd1dd1Schristos 	size_t		pr_offset;	/* Mapping offset in object */
67fbcd1dd1Schristos 	char		pr_mapname[PATH_MAX];	/* Mapping filename */
68fbcd1dd1Schristos 	uint8_t		pr_mflags;	/* Protection flags */
69fbcd1dd1Schristos #define	MA_READ		0x01
70fbcd1dd1Schristos #define	MA_WRITE	0x02
71fbcd1dd1Schristos #define	MA_EXEC		0x04
72fbcd1dd1Schristos #define	MA_COW		0x08
73fbcd1dd1Schristos #define MA_NEEDS_COPY	0x10
74fbcd1dd1Schristos #define	MA_NOCOREDUMP	0x20
75fbcd1dd1Schristos } prmap_t;
76fbcd1dd1Schristos 
77fbcd1dd1Schristos typedef struct prsyminfo {
78fbcd1dd1Schristos 	u_int		prs_lmid;	/* Map id. */
79fbcd1dd1Schristos 	u_int		prs_id;		/* Symbol id. */
80fbcd1dd1Schristos } prsyminfo_t;
81fbcd1dd1Schristos 
82fbcd1dd1Schristos typedef int proc_map_f(void *, const prmap_t *, const char *);
83fbcd1dd1Schristos typedef int proc_sym_f(void *, const GElf_Sym *, const char *);
84fbcd1dd1Schristos 
85fbcd1dd1Schristos /* Values for ELF sections */
86fbcd1dd1Schristos #define	PR_SYMTAB	1
87fbcd1dd1Schristos #define PR_DYNSYM	2
88fbcd1dd1Schristos 
89fbcd1dd1Schristos /* Values for the 'mask' parameter in the iteration functions */
90fbcd1dd1Schristos #define	BIND_LOCAL	0x0001
91fbcd1dd1Schristos #define BIND_GLOBAL	0x0002
92fbcd1dd1Schristos #define BIND_WEAK	0x0004
93fbcd1dd1Schristos #define BIND_ANY	(BIND_LOCAL|BIND_GLOBAL|BIND_WEAK)
94fbcd1dd1Schristos #define TYPE_NOTYPE	0x0100
95fbcd1dd1Schristos #define TYPE_OBJECT	0x0200
96fbcd1dd1Schristos #define TYPE_FUNC	0x0400
97fbcd1dd1Schristos #define TYPE_SECTION	0x0800
98fbcd1dd1Schristos #define TYPE_FILE	0x1000
99fbcd1dd1Schristos #define TYPE_ANY	(TYPE_NOTYPE|TYPE_OBJECT|TYPE_FUNC|TYPE_SECTION|\
100fbcd1dd1Schristos     			 TYPE_FILE)
101fbcd1dd1Schristos 
102fbcd1dd1Schristos typedef enum {
103fbcd1dd1Schristos 	REG_PC,
104fbcd1dd1Schristos 	REG_SP,
105fbcd1dd1Schristos 	REG_RVAL1,
106fbcd1dd1Schristos 	REG_RVAL2
107fbcd1dd1Schristos } proc_reg_t;
108fbcd1dd1Schristos 
109fbcd1dd1Schristos #define SIG2STR_MAX	8
110fbcd1dd1Schristos 
111fbcd1dd1Schristos typedef struct lwpstatus {
112fbcd1dd1Schristos 	int pr_why;
113fbcd1dd1Schristos #define PR_REQUESTED	1
114fbcd1dd1Schristos #define PR_FAULTED	2
115fbcd1dd1Schristos #define PR_SYSENTRY	3
116fbcd1dd1Schristos #define PR_SYSEXIT	4
117fbcd1dd1Schristos #define PR_SIGNALLED	5
118fbcd1dd1Schristos 	int pr_what;
119fbcd1dd1Schristos #define FLTBPT		-1
120fbcd1dd1Schristos } lwpstatus_t;
121fbcd1dd1Schristos 
12249f0a76dSchs #define	PR_MODEL_ILP32	1
12349f0a76dSchs #define	PR_MODEL_LP64	2
12449f0a76dSchs 
1259a58287bSchristos typedef struct {
1269a58287bSchristos 	uint8_t data[PTRACE_BREAKPOINT_SIZE];
1279a58287bSchristos } proc_breakpoint_t;
1289a58287bSchristos 
1299a58287bSchristos typedef unsigned long proc_regvalue_t;
1309a58287bSchristos 
131fbcd1dd1Schristos /* Function prototype definitions. */
132fbcd1dd1Schristos __BEGIN_DECLS
133fbcd1dd1Schristos 
134fbcd1dd1Schristos prmap_t *proc_addr2map(struct proc_handle *, uintptr_t);
135fbcd1dd1Schristos prmap_t *proc_name2map(struct proc_handle *, const char *);
136fbcd1dd1Schristos char	*proc_objname(struct proc_handle *, uintptr_t, char *, size_t);
137fbcd1dd1Schristos prmap_t *proc_obj2map(struct proc_handle *, const char *);
138fbcd1dd1Schristos int	proc_iter_objs(struct proc_handle *, proc_map_f *, void *);
139fbcd1dd1Schristos int	proc_iter_symbyaddr(struct proc_handle *, const char *, int,
140fbcd1dd1Schristos 	     int, proc_sym_f *, void *);
141fbcd1dd1Schristos int	proc_addr2sym(struct proc_handle *, uintptr_t, char *, size_t, GElf_Sym *);
142fbcd1dd1Schristos int	proc_attach(pid_t pid, int flags, struct proc_handle **pphdl);
143fbcd1dd1Schristos int	proc_continue(struct proc_handle *);
144fbcd1dd1Schristos int	proc_clearflags(struct proc_handle *, int);
145fbcd1dd1Schristos int	proc_create(const char *, char * const *, proc_child_func *, void *,
146fbcd1dd1Schristos 	    struct proc_handle **);
147fbcd1dd1Schristos int	proc_detach(struct proc_handle *, int);
148fbcd1dd1Schristos int	proc_getflags(struct proc_handle *);
149fbcd1dd1Schristos int	proc_name2sym(struct proc_handle *, const char *, const char *,
150fbcd1dd1Schristos 	    GElf_Sym *, prsyminfo_t *);
151fbcd1dd1Schristos struct ctf_file *proc_name2ctf(struct proc_handle *, const char *);
152fbcd1dd1Schristos int	proc_setflags(struct proc_handle *, int);
153fbcd1dd1Schristos int	proc_state(struct proc_handle *);
15449f0a76dSchs int	proc_getmodel(struct proc_handle *);
155fbcd1dd1Schristos pid_t	proc_getpid(struct proc_handle *);
156fbcd1dd1Schristos int	proc_wstatus(struct proc_handle *);
157fbcd1dd1Schristos int	proc_getwstat(struct proc_handle *);
158fbcd1dd1Schristos char *	proc_signame(int, char *, size_t);
159fbcd1dd1Schristos int	proc_read(struct proc_handle *, void *, size_t, size_t);
160fbcd1dd1Schristos const lwpstatus_t *proc_getlwpstatus(struct proc_handle *);
161fbcd1dd1Schristos void	proc_free(struct proc_handle *);
162fbcd1dd1Schristos rd_agent_t *proc_rdagent(struct proc_handle *);
163fbcd1dd1Schristos void	proc_updatesyms(struct proc_handle *);
1649a58287bSchristos int	proc_bkptset(struct proc_handle *, uintptr_t, proc_breakpoint_t *);
1659a58287bSchristos int	proc_bkptdel(struct proc_handle *, uintptr_t, proc_breakpoint_t *);
166fbcd1dd1Schristos void	proc_bkptregadj(unsigned long *);
1679a58287bSchristos int	proc_bkptexec(struct proc_handle *, proc_breakpoint_t *);
1689a58287bSchristos int	proc_regget(struct proc_handle *, proc_reg_t, proc_regvalue_t *);
1699a58287bSchristos int	proc_regset(struct proc_handle *, proc_reg_t, proc_regvalue_t);
170fbcd1dd1Schristos 
171fbcd1dd1Schristos __END_DECLS
172fbcd1dd1Schristos 
173fbcd1dd1Schristos #endif /* !_LIBPROC_H_ */
174