1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #ifndef _MDB_PROC_H
28 #define _MDB_PROC_H
29
30 #include <mdb/mdb_target_impl.h>
31 #include <mdb/mdb_io_impl.h>
32 #include <mdb/mdb_addrvec.h>
33 #include <mdb/mdb_modapi.h>
34 #include <mdb/mdb_gelf.h>
35 #include <mdb/mdb_tdb.h>
36
37 #include <sys/param.h>
38 #include <libproc.h>
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #ifdef _MDB
45
46 /*
47 * The proc target must provide support for examining multi-threaded processes
48 * that use the raw LWP interface, as well as those that use either of the
49 * existing libthread.so implementations. We must also support multiple active
50 * instances of the proc target, as well as the notion that a clean process
51 * can dlopen() libthread after startup, at which point we need to switch to
52 * using libthread_db interfaces to properly debug it. To satisfy these
53 * constraints, we declare an ops vector of functions for obtaining the
54 * register sets of each thread. The proc target will define two versions
55 * of this vector, one for the LWP mode and one for the libthread_db mode,
56 * and then switch the ops vector pointer as appropriate during debugging.
57 * The macros defined below expand to calls to the appropriate entry point.
58 */
59 typedef struct pt_ptl_ops {
60 int (*ptl_ctor)(mdb_tgt_t *);
61 void (*ptl_dtor)(mdb_tgt_t *, void *);
62 mdb_tgt_tid_t (*ptl_tid)(mdb_tgt_t *, void *);
63 int (*ptl_iter)(mdb_tgt_t *, void *, mdb_addrvec_t *);
64 int (*ptl_getregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t, prgregset_t);
65 int (*ptl_setregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t, prgregset_t);
66 #ifdef __sparc
67 int (*ptl_getxregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t,
68 prxregset_t *);
69 int (*ptl_setxregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t,
70 const prxregset_t *);
71 #endif
72 int (*ptl_getfpregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t,
73 prfpregset_t *);
74 int (*ptl_setfpregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t,
75 const prfpregset_t *);
76 } pt_ptl_ops_t;
77
78 #define PTL_CTOR(t) \
79 (((pt_data_t *)(t)->t_data)->p_ptl_ops->ptl_ctor(t))
80
81 #define PTL_DTOR(t) \
82 (((pt_data_t *)(t)->t_data)->p_ptl_ops->ptl_dtor((t), \
83 ((pt_data_t *)((t)->t_data))->p_ptl_hdl))
84
85 #define PTL_TID(t) \
86 (((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_tid((t), \
87 ((pt_data_t *)(t)->t_data)->p_ptl_hdl))
88
89 #define PTL_ITER(t, ap) \
90 (((pt_data_t *)(t)->t_data)->p_ptl_ops->ptl_iter((t), \
91 ((pt_data_t *)((t)->t_data))->p_ptl_hdl, (ap)))
92
93 #define PTL_GETREGS(t, tid, gregs) \
94 (((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_getregs((t), \
95 ((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (gregs)))
96
97 #define PTL_SETREGS(t, tid, gregs) \
98 (((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_setregs((t), \
99 ((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (gregs)))
100
101 #ifdef __sparc
102
103 #define PTL_GETXREGS(t, tid, xregs) \
104 (((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_getxregs((t), \
105 ((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (xregs)))
106
107 #define PTL_SETXREGS(t, tid, xregs) \
108 (((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_setxregs((t), \
109 ((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (xregs)))
110
111 #endif /* __sparc */
112
113 #define PTL_GETFPREGS(t, tid, fpregs) \
114 (((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_getfpregs((t), \
115 ((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (fpregs)))
116
117 #define PTL_SETFPREGS(t, tid, fpregs) \
118 (((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_setfpregs((t), \
119 ((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (fpregs)))
120
121 /*
122 * When we are following children and a vfork(2) occurs, we append the libproc
123 * handle for the parent to a list of vfork parents. We need to keep track of
124 * this handle so that when the child subsequently execs or dies, we clear out
125 * our breakpoints before releasing the parent.
126 */
127 typedef struct pt_vforkp {
128 mdb_list_t p_list; /* List forward/back pointers */
129 struct ps_prochandle *p_pshandle; /* libproc handle */
130 } pt_vforkp_t;
131
132 /*
133 * Private data structure for the proc target. Among other things, we keep
134 * pointers to the various symbol tables and the ELF file for the executable
135 * here, along with handles for our ops vector defined above.
136 */
137 typedef struct pt_data {
138 struct ps_prochandle *p_idlehandle; /* idle libproc handle */
139 mdb_gelf_symtab_t *p_symtab; /* Standard symbol table */
140 mdb_gelf_symtab_t *p_dynsym; /* Dynamic symbol table */
141 mdb_gelf_file_t *p_file; /* ELF file object */
142 mdb_io_t *p_fio; /* Current file i/o backend */
143 mdb_io_t *p_aout_fio; /* Original file i/o backend */
144 char p_platform[MAXNAMELEN]; /* Platform string */
145 char p_symname[MDB_TGT_SYM_NAMLEN]; /* Temporary buffer for syms */
146 char p_objname[MDB_TGT_MAPSZ]; /* Temporary buffer for objs */
147 mdb_map_t p_map; /* Persistent map for callers */
148 mdb_list_t p_vforkp; /* List of vfork parents */
149 mdb_nv_t p_regs; /* Register descriptions */
150 const mdb_tdb_ops_t *p_tdb_ops; /* libthread_db ops */
151 const pt_ptl_ops_t *p_ptl_ops; /* Proc thread layer ops */
152 void *p_ptl_hdl; /* Proc thread layer handle */
153 rd_agent_t *p_rtld; /* librtld_db agent handle */
154 const char *p_stdin; /* File for stdin redirect */
155 const char *p_stdout; /* File for stdout redirect */
156 int p_oflags; /* Flags for open(2) */
157 int p_gflags; /* Flags for Pgrab() */
158 int p_rflags; /* Flags for Prelease() */
159 int p_signal; /* Signal to post at next run */
160 int p_rtld_finished; /* Has rtld init completed? */
161 int p_rdstate; /* Dlopen state (see below) */
162 int p_maxsig; /* Maximum valid signal */
163 mdb_nv_t p_env; /* Current environment */
164 } pt_data_t;
165
166 #define PT_RD_NONE 0 /* No update pending */
167 #define PT_RD_ADD 1 /* Dlopen detected */
168 #define PT_RD_CONSIST 2 /* Link maps consistent */
169
170 /*
171 * The mdb_tgt_gregset type is opaque to callers of the target interface.
172 * Inside the target we define it explicitly to be a prgregset_t.
173 */
174 struct mdb_tgt_gregset {
175 prgregset_t gregs;
176 };
177
178 typedef struct pt_symarg {
179 mdb_tgt_t *psym_targ; /* Target pointer */
180 uint_t psym_which; /* Type of symbol table */
181 uint_t psym_type; /* Type of symbols to match */
182 mdb_tgt_sym_f *psym_func; /* Callback function */
183 void *psym_private; /* Callback data */
184 mdb_syminfo_t psym_info; /* Symbol id and table id */
185 const char *psym_obj; /* Containing object */
186 } pt_symarg_t;
187
188 typedef struct pt_maparg {
189 mdb_tgt_t *pmap_targ; /* Target pointer */
190 mdb_tgt_map_f *pmap_func; /* Callback function */
191 void *pmap_private; /* Callback data */
192 } pt_maparg_t;
193
194 typedef struct pt_stkarg {
195 mdb_tgt_stack_f *pstk_func; /* Callback function */
196 void *pstk_private; /* Callback data */
197 uint_t pstk_gotpc; /* Non-zero pc found */
198 } pt_stkarg_t;
199
200 typedef struct pt_addarg_t {
201 pt_data_t *pa_pt; /* Proc target data */
202 mdb_addrvec_t *pa_ap; /* Addrvec pointer */
203 } pt_addarg_t;
204
205 typedef struct pt_brkpt {
206 uintptr_t ptb_addr; /* Breakpoint address */
207 ulong_t ptb_instr; /* Saved instruction */
208 } pt_brkpt_t;
209
210 typedef struct pt_bparg {
211 char *pta_symbol; /* Symbolic name */
212 uintptr_t pta_addr; /* Explicit address */
213 } pt_bparg_t;
214
215 /*
216 * The proc_isadep.c file is expected to define the following
217 * ISA-dependent pieces of the proc target:
218 */
219 extern int pt_regs(uintptr_t, uint_t, int, const mdb_arg_t *);
220 extern int pt_fpregs(uintptr_t, uint_t, int, const mdb_arg_t *);
221 extern int pt_step_out(mdb_tgt_t *, uintptr_t *);
222 extern int pt_next(mdb_tgt_t *, uintptr_t *);
223 extern int pt_getfpreg(mdb_tgt_t *, mdb_tgt_tid_t, ushort_t, ushort_t,
224 mdb_tgt_reg_t *);
225 extern int pt_putfpreg(mdb_tgt_t *, mdb_tgt_tid_t, ushort_t, ushort_t,
226 mdb_tgt_reg_t);
227 extern void pt_addfpregs(mdb_tgt_t *);
228 extern const char *pt_disasm(const GElf_Ehdr *);
229 extern int pt_frameregs(void *, uintptr_t, uint_t, const long *,
230 const mdb_tgt_gregset_t *, boolean_t);
231 extern const mdb_tgt_regdesc_t pt_regdesc[];
232
233 #endif /* _MDB */
234
235 #ifdef __cplusplus
236 }
237 #endif
238
239 #endif /* _MDB_PROC_H */