1179185Sjb/*-
2210688Srpaulo * Copyright (c) 2010 The FreeBSD Foundation
3179185Sjb * Copyright (c) 2008 John Birrell (jb@freebsd.org)
4179185Sjb * All rights reserved.
5179185Sjb *
6210688Srpaulo * Portions of this software were developed by Rui Paulo under sponsorship
7210688Srpaulo * from the FreeBSD Foundation.
8210688Srpaulo *
9179185Sjb * Redistribution and use in source and binary forms, with or without
10179185Sjb * modification, are permitted provided that the following conditions
11179185Sjb * are met:
12179185Sjb * 1. Redistributions of source code must retain the above copyright
13179185Sjb *    notice, this list of conditions and the following disclaimer.
14179185Sjb * 2. Redistributions in binary form must reproduce the above copyright
15179185Sjb *    notice, this list of conditions and the following disclaimer in the
16179185Sjb *    documentation and/or other materials provided with the distribution.
17179185Sjb *
18179185Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19179185Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20179185Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21179185Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22179185Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23179185Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24179185Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25179185Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26179185Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27179185Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28179185Sjb * SUCH DAMAGE.
29179185Sjb *
30179185Sjb * $FreeBSD$
31179185Sjb */
32179185Sjb
33179185Sjb#ifndef	_LIBPROC_H_
34179185Sjb#define	_LIBPROC_H_
35179185Sjb
36179185Sjb#include <gelf.h>
37210688Srpaulo#include <rtld_db.h>
38210688Srpaulo#include <limits.h>
39179185Sjb
40179185Sjbstruct proc_handle;
41179185Sjb
42184697Srodrigctypedef void (*proc_child_func)(void *);
43184697Srodrigc
44179185Sjb/* Values returned by proc_state(). */
45179185Sjb#define PS_IDLE		1
46179185Sjb#define PS_STOP		2
47179185Sjb#define PS_RUN		3
48179185Sjb#define PS_UNDEAD	4
49179185Sjb#define PS_DEAD		5
50179185Sjb#define PS_LOST		6
51179185Sjb
52210688Srpaulo/* Reason values for proc_detach(). */
53210688Srpaulo#define PRELEASE_HANG	1
54210688Srpaulo#define PRELEASE_KILL	2
55210688Srpaulo
56179185Sjbtypedef struct prmap {
57179185Sjb	uintptr_t	pr_vaddr;	/* Virtual address. */
58210688Srpaulo	size_t		pr_size;	/* Mapping size in bytes */
59210688Srpaulo	size_t		pr_offset;	/* Mapping offset in object */
60210688Srpaulo	char		pr_mapname[PATH_MAX];	/* Mapping filename */
61210688Srpaulo	uint8_t		pr_mflags;	/* Protection flags */
62210688Srpaulo#define	MA_READ		0x01
63210688Srpaulo#define	MA_WRITE	0x02
64210688Srpaulo#define	MA_EXEC		0x04
65210688Srpaulo#define	MA_COW		0x08
66210688Srpaulo#define MA_NEEDS_COPY	0x10
67210688Srpaulo#define	MA_NOCOREDUMP	0x20
68179185Sjb} prmap_t;
69179185Sjb
70210688Srpaulotypedef int proc_map_f(void *, const prmap_t *, const char *);
71210688Srpaulotypedef int proc_sym_f(void *, const GElf_Sym *, const char *);
72210688Srpaulo
73210688Srpaulo/* Values for ELF sections */
74210688Srpaulo#define	PR_SYMTAB	1
75210688Srpaulo#define PR_DYNSYM	2
76210688Srpaulo
77210688Srpaulo/* Values for the 'mask' parameter in the iteration functions */
78210688Srpaulo#define	BIND_LOCAL	0x0001
79210688Srpaulo#define BIND_GLOBAL	0x0002
80210688Srpaulo#define BIND_WEAK	0x0004
81210688Srpaulo#define BIND_ANY	(BIND_LOCAL|BIND_GLOBAL|BIND_WEAK)
82210688Srpaulo#define TYPE_NOTYPE	0x0100
83210688Srpaulo#define TYPE_OBJECT	0x0200
84210688Srpaulo#define TYPE_FUNC	0x0400
85210688Srpaulo#define TYPE_SECTION	0x0800
86210688Srpaulo#define TYPE_FILE	0x1000
87210688Srpaulo#define TYPE_ANY	(TYPE_NOTYPE|TYPE_OBJECT|TYPE_FUNC|TYPE_SECTION|\
88210688Srpaulo    			 TYPE_FILE)
89210688Srpaulo
90210688Srpaulotypedef enum {
91210688Srpaulo	REG_PC,
92210688Srpaulo	REG_SP,
93210688Srpaulo	REG_RVAL1,
94210688Srpaulo	REG_RVAL2
95210688Srpaulo} proc_reg_t;
96210688Srpaulo
97210688Srpaulo#define SIG2STR_MAX	8
98210688Srpaulo
99210688Srpaulotypedef struct lwpstatus {
100210688Srpaulo	int pr_why;
101210688Srpaulo#define PR_REQUESTED	1
102210688Srpaulo#define PR_FAULTED	2
103210688Srpaulo#define PR_SYSENTRY	3
104210688Srpaulo#define PR_SYSEXIT	4
105210688Srpaulo	int pr_what;
106210688Srpaulo#define FLTBPT		-1
107210688Srpaulo} lwpstatus_t;
108210688Srpaulo
109179185Sjb/* Function prototype definitions. */
110179185Sjb__BEGIN_DECLS
111179185Sjb
112210688Srpauloprmap_t *proc_addr2map(struct proc_handle *, uintptr_t);
113210688Srpauloprmap_t *proc_name2map(struct proc_handle *, const char *);
114179185Sjbchar	*proc_objname(struct proc_handle *, uintptr_t, char *, size_t);
115210688Srpauloprmap_t *proc_obj2map(struct proc_handle *, const char *);
116210688Srpauloint	proc_iter_objs(struct proc_handle *, proc_map_f *, void *);
117210688Srpauloint	proc_iter_symbyaddr(struct proc_handle *, const char *, int,
118210688Srpaulo	     int, proc_sym_f *, void *);
119179185Sjbint	proc_addr2sym(struct proc_handle *, uintptr_t, char *, size_t, GElf_Sym *);
120179185Sjbint	proc_attach(pid_t pid, int flags, struct proc_handle **pphdl);
121179185Sjbint	proc_continue(struct proc_handle *);
122179185Sjbint	proc_clearflags(struct proc_handle *, int);
123184697Srodrigcint	proc_create(const char *, char * const *, proc_child_func *, void *,
124184697Srodrigc	    struct proc_handle **);
125210688Srpauloint	proc_detach(struct proc_handle *, int);
126179185Sjbint	proc_getflags(struct proc_handle *);
127179185Sjbint	proc_name2sym(struct proc_handle *, const char *, const char *, GElf_Sym *);
128179185Sjbint	proc_setflags(struct proc_handle *, int);
129179185Sjbint	proc_state(struct proc_handle *);
130179185Sjbpid_t	proc_getpid(struct proc_handle *);
131210688Srpauloint	proc_wstatus(struct proc_handle *);
132210688Srpauloint	proc_getwstat(struct proc_handle *);
133210688Srpaulochar *	proc_signame(int, char *, size_t);
134211184Srpauloint	proc_read(struct proc_handle *, void *, size_t, size_t);
135210688Srpauloconst lwpstatus_t *
136210688Srpaulo	proc_getlwpstatus(struct proc_handle *);
137179185Sjbvoid	proc_free(struct proc_handle *);
138210688Srpaulord_agent_t *proc_rdagent(struct proc_handle *);
139210688Srpaulovoid	proc_updatesyms(struct proc_handle *);
140210688Srpauloint	proc_bkptset(struct proc_handle *, uintptr_t, unsigned long *);
141210688Srpauloint	proc_bkptdel(struct proc_handle *, uintptr_t, unsigned long);
142210688Srpaulovoid	proc_bkptregadj(unsigned long *);
143210688Srpauloint	proc_bkptexec(struct proc_handle *, unsigned long);
144210688Srpauloint	proc_regget(struct proc_handle *, proc_reg_t, unsigned long *);
145210688Srpauloint	proc_regset(struct proc_handle *, proc_reg_t, unsigned long);
146179185Sjb
147179185Sjb__END_DECLS
148179185Sjb
149179185Sjb#endif /* !_LIBPROC_H_ */
150