xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_proc.h (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
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 	int (*ptl_getxregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t,
67 	    prxregset_t **, size_t *);
68 	void (*ptl_freexregs)(mdb_tgt_t *, void *, prxregset_t *, size_t);
69 	int (*ptl_setxregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t,
70 	    const prxregset_t *, size_t);
71 	int (*ptl_getfpregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t,
72 	    prfpregset_t *);
73 	int (*ptl_setfpregs)(mdb_tgt_t *, void *, mdb_tgt_tid_t,
74 	    const prfpregset_t *);
75 } pt_ptl_ops_t;
76 
77 #define	PTL_CTOR(t) \
78 	(((pt_data_t *)(t)->t_data)->p_ptl_ops->ptl_ctor(t))
79 
80 #define	PTL_DTOR(t) \
81 	(((pt_data_t *)(t)->t_data)->p_ptl_ops->ptl_dtor((t), \
82 	((pt_data_t *)((t)->t_data))->p_ptl_hdl))
83 
84 #define	PTL_TID(t) \
85 	(((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_tid((t), \
86 	((pt_data_t *)(t)->t_data)->p_ptl_hdl))
87 
88 #define	PTL_ITER(t, ap) \
89 	(((pt_data_t *)(t)->t_data)->p_ptl_ops->ptl_iter((t), \
90 	((pt_data_t *)((t)->t_data))->p_ptl_hdl, (ap)))
91 
92 #define	PTL_GETREGS(t, tid, gregs) \
93 	(((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_getregs((t), \
94 	((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (gregs)))
95 
96 #define	PTL_SETREGS(t, tid, gregs) \
97 	(((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_setregs((t), \
98 	((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (gregs)))
99 
100 #define	PTL_GETXREGS(t, tid, xregs, size) \
101 	(((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_getxregs((t), \
102 	((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (xregs), (size)))
103 
104 #define	PTL_FREEXREGS(t, xregs, size) \
105 	(((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_freexregs((t), \
106 	((pt_data_t *)((t)->t_data))->p_ptl_hdl, (xregs), (size)))
107 
108 #define	PTL_SETXREGS(t, tid, xregs, size) \
109 	(((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_setxregs((t), \
110 	((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (xregs), (size)))
111 
112 #define	PTL_GETFPREGS(t, tid, fpregs) \
113 	(((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_getfpregs((t), \
114 	((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (fpregs)))
115 
116 #define	PTL_SETFPREGS(t, tid, fpregs) \
117 	(((pt_data_t *)((t)->t_data))->p_ptl_ops->ptl_setfpregs((t), \
118 	((pt_data_t *)((t)->t_data))->p_ptl_hdl, (tid), (fpregs)))
119 
120 /*
121  * When we are following children and a vfork(2) occurs, we append the libproc
122  * handle for the parent to a list of vfork parents.  We need to keep track of
123  * this handle so that when the child subsequently execs or dies, we clear out
124  * our breakpoints before releasing the parent.
125  */
126 typedef struct pt_vforkp {
127 	mdb_list_t p_list;			/* List forward/back pointers */
128 	struct ps_prochandle *p_pshandle;	/* libproc handle */
129 } pt_vforkp_t;
130 
131 /*
132  * Private data structure for the proc target.  Among other things, we keep
133  * pointers to the various symbol tables and the ELF file for the executable
134  * here, along with handles for our ops vector defined above.
135  */
136 typedef struct pt_data {
137 	struct ps_prochandle *p_idlehandle;	/* idle libproc handle */
138 	mdb_gelf_symtab_t *p_symtab;		/* Standard symbol table */
139 	mdb_gelf_symtab_t *p_dynsym;		/* Dynamic symbol table */
140 	mdb_gelf_file_t *p_file;		/* ELF file object */
141 	mdb_io_t *p_fio;			/* Current file i/o backend */
142 	mdb_io_t *p_aout_fio;			/* Original file i/o backend */
143 	char p_platform[MAXNAMELEN];		/* Platform string */
144 	char p_symname[MDB_TGT_SYM_NAMLEN];	/* Temporary buffer for syms */
145 	char p_objname[MDB_TGT_MAPSZ];		/* Temporary buffer for objs */
146 	mdb_map_t p_map;			/* Persistent map for callers */
147 	mdb_list_t p_vforkp;			/* List of vfork parents */
148 	mdb_nv_t p_regs;			/* Register descriptions */
149 	const mdb_tdb_ops_t *p_tdb_ops;		/* libthread_db ops */
150 	const pt_ptl_ops_t *p_ptl_ops;		/* Proc thread layer ops */
151 	void *p_ptl_hdl;			/* Proc thread layer handle */
152 	rd_agent_t *p_rtld;			/* librtld_db agent handle */
153 	const char *p_stdin;			/* File for stdin redirect */
154 	const char *p_stdout;			/* File for stdout redirect */
155 	int p_oflags;				/* Flags for open(2) */
156 	int p_gflags;				/* Flags for Pgrab() */
157 	int p_rflags;				/* Flags for Prelease() */
158 	int p_signal;				/* Signal to post at next run */
159 	int p_rtld_finished;			/* Has rtld init completed? */
160 	int p_rdstate;				/* Dlopen state (see below) */
161 	int p_maxsig;				/* Maximum valid signal */
162 	mdb_nv_t p_env;				/* Current environment */
163 } pt_data_t;
164 
165 #define	PT_RD_NONE	0			/* No update pending */
166 #define	PT_RD_ADD	1			/* Dlopen detected */
167 #define	PT_RD_CONSIST	2			/* Link maps consistent */
168 
169 /*
170  * The mdb_tgt_gregset type is opaque to callers of the target interface.
171  * Inside the target we define it explicitly to be a prgregset_t.
172  */
173 struct mdb_tgt_gregset {
174 	prgregset_t gregs;
175 };
176 
177 typedef struct pt_symarg {
178 	mdb_tgt_t *psym_targ;			/* Target pointer */
179 	uint_t psym_which;			/* Type of symbol table */
180 	uint_t psym_type;			/* Type of symbols to match */
181 	mdb_tgt_sym_f *psym_func;		/* Callback function */
182 	void *psym_private;			/* Callback data */
183 	mdb_syminfo_t psym_info;		/* Symbol id and table id */
184 	const char *psym_obj;			/* Containing object */
185 } pt_symarg_t;
186 
187 typedef struct pt_maparg {
188 	mdb_tgt_t *pmap_targ;			/* Target pointer */
189 	mdb_tgt_map_f *pmap_func;		/* Callback function */
190 	void *pmap_private;			/* Callback data */
191 } pt_maparg_t;
192 
193 typedef struct pt_stkarg {
194 	mdb_tgt_stack_f *pstk_func;		/* Callback function */
195 	void *pstk_private;			/* Callback data */
196 	uint_t pstk_gotpc;			/* Non-zero pc found */
197 } pt_stkarg_t;
198 
199 typedef struct pt_addarg_t {
200 	pt_data_t *pa_pt;			/* Proc target data */
201 	mdb_addrvec_t *pa_ap;			/* Addrvec pointer */
202 } pt_addarg_t;
203 
204 typedef struct pt_brkpt {
205 	uintptr_t ptb_addr;			/* Breakpoint address */
206 	ulong_t ptb_instr;			/* Saved instruction */
207 } pt_brkpt_t;
208 
209 typedef struct pt_bparg {
210 	char *pta_symbol;			/* Symbolic name */
211 	uintptr_t pta_addr;			/* Explicit address */
212 } pt_bparg_t;
213 
214 /*
215  * The proc_isadep.c file is expected to define the following
216  * ISA-dependent pieces of the proc target:
217  */
218 extern int pt_regs(uintptr_t, uint_t, int, const mdb_arg_t *);
219 extern int pt_fpregs(uintptr_t, uint_t, int, const mdb_arg_t *);
220 extern int pt_step_out(mdb_tgt_t *, uintptr_t *);
221 extern int pt_next(mdb_tgt_t *, uintptr_t *);
222 extern int pt_getfpreg(mdb_tgt_t *, mdb_tgt_tid_t, ushort_t, ushort_t,
223     mdb_tgt_reg_t *);
224 extern int pt_putfpreg(mdb_tgt_t *, mdb_tgt_tid_t, ushort_t, ushort_t,
225     mdb_tgt_reg_t);
226 extern void pt_addfpregs(mdb_tgt_t *);
227 extern const char *pt_disasm(const GElf_Ehdr *);
228 extern int pt_frameregs(void *, uintptr_t, uint_t, const long *,
229     const mdb_tgt_gregset_t *, boolean_t);
230 extern const mdb_tgt_regdesc_t pt_regdesc[];
231 
232 #endif /* _MDB */
233 
234 #ifdef	__cplusplus
235 }
236 #endif
237 
238 #endif	/* _MDB_PROC_H */
239