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