xref: /titanic_51/usr/src/cmd/mdb/common/mdb/mdb_modapi.h (revision 14ea4bb737263733ad80a36b4f73f681c30a6b45)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_MDB_MODAPI_H
27 #define	_MDB_MODAPI_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * MDB Module API
33  *
34  * The debugger provides a set of interfaces for use in writing loadable
35  * debugger modules.  Modules that call functions not listed in this header
36  * file may not be compatible with future versions of the debugger.
37  */
38 
39 #include <sys/types.h>
40 #include <gelf.h>
41 
42 #ifdef	__cplusplus
43 extern "C" {
44 #endif
45 
46 /*
47  * Make sure that NULL, TRUE, FALSE, MIN, and MAX have the usual definitions
48  * so module writers can depend on these macros and defines.
49  */
50 #ifndef NULL
51 #if defined(_LP64) && !defined(__cplusplus)
52 #define	NULL	0L
53 #else
54 #define	NULL	0
55 #endif
56 #endif
57 
58 #ifndef TRUE
59 #define	TRUE	1
60 #endif
61 
62 #ifndef FALSE
63 #define	FALSE	0
64 #endif
65 
66 #ifndef MIN
67 #define	MIN(x, y) ((x) < (y) ? (x) : (y))
68 #endif
69 
70 #ifndef MAX
71 #define	MAX(x, y) ((x) > (y) ? (x) : (y))
72 #endif
73 
74 #define	MDB_API_VERSION	3	/* Current API version number */
75 
76 /*
77  * Debugger command function flags:
78  */
79 #define	DCMD_ADDRSPEC	0x01	/* Dcmd invoked with explicit address */
80 #define	DCMD_LOOP	0x02	/* Dcmd invoked in loop with ,cnt syntax */
81 #define	DCMD_LOOPFIRST	0x04	/* Dcmd invoked as first iteration of LOOP */
82 #define	DCMD_PIPE	0x08	/* Dcmd invoked with input from pipe */
83 #define	DCMD_PIPE_OUT	0x10	/* Dcmd invoked with output set to pipe */
84 
85 #define	DCMD_HDRSPEC(fl)	(((fl) & DCMD_LOOPFIRST) || !((fl) & DCMD_LOOP))
86 
87 /*
88  * Debugger command function return values:
89  */
90 #define	DCMD_OK		0	/* Dcmd completed successfully */
91 #define	DCMD_ERR	1	/* Dcmd failed due to an error */
92 #define	DCMD_USAGE	2	/* Dcmd usage error; abort and print usage */
93 #define	DCMD_NEXT	3	/* Invoke next dcmd in precedence list */
94 #define	DCMD_ABORT	4	/* Dcmd failed; abort current loop or pipe */
95 
96 #define	OFFSETOF(s, m)		(size_t)(&(((s *)0)->m))
97 
98 extern int mdb_prop_postmortem;	/* Are we looking at a static dump? */
99 extern int mdb_prop_kernel;	/* Are we looking at a kernel? */
100 
101 typedef enum {
102 	MDB_TYPE_STRING,	/* a_un.a_str is valid */
103 	MDB_TYPE_IMMEDIATE,	/* a_un.a_val is valid */
104 	MDB_TYPE_CHAR		/* a_un.a_char is valid */
105 } mdb_type_t;
106 
107 typedef struct mdb_arg {
108 	mdb_type_t a_type;
109 	union {
110 		const char *a_str;
111 		uintmax_t a_val;
112 		char a_char;
113 	} a_un;
114 } mdb_arg_t;
115 
116 typedef int mdb_dcmd_f(uintptr_t, uint_t, int, const mdb_arg_t *);
117 
118 typedef struct mdb_dcmd {
119 	const char *dc_name;		/* Command name */
120 	const char *dc_usage;		/* Usage message (optional) */
121 	const char *dc_descr;		/* Description */
122 	mdb_dcmd_f *dc_funcp;		/* Command function */
123 	void (*dc_help)(void);		/* Command help function (or NULL) */
124 } mdb_dcmd_t;
125 
126 #define	WALK_ERR	-1		/* Walk fatal error (terminate walk) */
127 #define	WALK_NEXT	0		/* Walk should continue to next step */
128 #define	WALK_DONE	1		/* Walk is complete (no errors) */
129 
130 typedef int (*mdb_walk_cb_t)(uintptr_t, const void *, void *);
131 
132 typedef struct mdb_walk_state {
133 	mdb_walk_cb_t walk_callback;	/* Callback to issue */
134 	void *walk_cbdata;		/* Callback private data */
135 	uintptr_t walk_addr;		/* Current address */
136 	void *walk_data;		/* Walk private data */
137 	void *walk_arg;			/* Walk private argument */
138 	const void *walk_layer;		/* Data from underlying layer */
139 } mdb_walk_state_t;
140 
141 typedef struct mdb_walker {
142 	const char *walk_name;		/* Walk type name */
143 	const char *walk_descr;		/* Walk description */
144 	int (*walk_init)(mdb_walk_state_t *);	/* Walk constructor */
145 	int (*walk_step)(mdb_walk_state_t *);	/* Walk iterator */
146 	void (*walk_fini)(mdb_walk_state_t *);	/* Walk destructor */
147 	void *walk_init_arg;		/* Walk constructor argument */
148 } mdb_walker_t;
149 
150 typedef struct mdb_modinfo {
151 	ushort_t mi_dvers;		/* Debugger version number */
152 	const mdb_dcmd_t *mi_dcmds;	/* NULL-terminated list of dcmds */
153 	const mdb_walker_t *mi_walkers;	/* NULL-terminated list of walks */
154 } mdb_modinfo_t;
155 
156 typedef struct mdb_bitmask {
157 	const char *bm_name;		/* String name to print */
158 	u_longlong_t bm_mask;		/* Mask for bits */
159 	u_longlong_t bm_bits;		/* Result required for value & mask */
160 } mdb_bitmask_t;
161 
162 typedef struct mdb_pipe {
163 	uintptr_t *pipe_data;		/* Array of pipe values */
164 	size_t pipe_len;		/* Array length */
165 } mdb_pipe_t;
166 
167 extern int mdb_pwalk(const char *, mdb_walk_cb_t, void *, uintptr_t);
168 extern int mdb_walk(const char *, mdb_walk_cb_t, void *);
169 
170 extern int mdb_pwalk_dcmd(const char *, const char *,
171 	int, const mdb_arg_t *, uintptr_t);
172 
173 extern int mdb_walk_dcmd(const char *, const char *, int, const mdb_arg_t *);
174 
175 extern int mdb_layered_walk(const char *, mdb_walk_state_t *);
176 
177 extern int mdb_call_dcmd(const char *, uintptr_t,
178 	uint_t, int, const mdb_arg_t *);
179 
180 extern int mdb_add_walker(const mdb_walker_t *);
181 extern int mdb_remove_walker(const char *);
182 
183 extern ssize_t mdb_vread(void *, size_t, uintptr_t);
184 extern ssize_t mdb_vwrite(const void *, size_t, uintptr_t);
185 
186 extern ssize_t mdb_fread(void *, size_t, uintptr_t);
187 extern ssize_t mdb_fwrite(const void *, size_t, uintptr_t);
188 
189 extern ssize_t mdb_pread(void *, size_t, uint64_t);
190 extern ssize_t mdb_pwrite(const void *, size_t, uint64_t);
191 
192 extern ssize_t mdb_readstr(char *, size_t, uintptr_t);
193 extern ssize_t mdb_writestr(const char *, uintptr_t);
194 
195 extern ssize_t mdb_readsym(void *, size_t, const char *);
196 extern ssize_t mdb_writesym(const void *, size_t, const char *);
197 
198 extern ssize_t mdb_readvar(void *, const char *);
199 extern ssize_t mdb_writevar(const void *, const char *);
200 
201 #define	MDB_SYM_NAMLEN	1024			/* Recommended max name len */
202 
203 #define	MDB_SYM_FUZZY	0			/* Match closest address */
204 #define	MDB_SYM_EXACT	1			/* Match exact address only */
205 
206 #define	MDB_OBJ_EXEC	((const char *)0L)	/* Primary executable file */
207 #define	MDB_OBJ_RTLD	((const char *)1L)	/* Run-time link-editor */
208 #define	MDB_OBJ_EVERY	((const char *)-1L)	/* All known symbols */
209 
210 extern int mdb_lookup_by_name(const char *, GElf_Sym *);
211 extern int mdb_lookup_by_obj(const char *, const char *, GElf_Sym *);
212 extern int mdb_lookup_by_addr(uintptr_t, uint_t, char *, size_t, GElf_Sym *);
213 
214 #define	MDB_OPT_SETBITS	1			/* Set specified flag bits */
215 #define	MDB_OPT_CLRBITS	2			/* Clear specified flag bits */
216 #define	MDB_OPT_STR	3			/* const char * argument */
217 #define	MDB_OPT_UINTPTR	4			/* uintptr_t argument */
218 #define	MDB_OPT_UINT64	5			/* uint64_t argument */
219 #define	MDB_OPT_UINTPTR_SET	6		/* boolean_t+uintptr_t args */
220 
221 extern int mdb_getopts(int, const mdb_arg_t *, ...);
222 
223 extern u_longlong_t mdb_strtoull(const char *);
224 
225 #define	UM_NOSLEEP	0x0	/* Do not call failure handler; may fail */
226 #define	UM_SLEEP	0x1	/* Can block for memory; will always succeed */
227 #define	UM_GC		0x2	/* Garbage-collect this block automatically */
228 
229 extern void *mdb_alloc(size_t, uint_t);
230 extern void *mdb_zalloc(size_t, uint_t);
231 extern void mdb_free(void *, size_t);
232 
233 extern size_t mdb_snprintf(char *, size_t, const char *, ...);
234 extern void mdb_printf(const char *, ...);
235 extern void mdb_warn(const char *, ...);
236 extern void mdb_flush(void);
237 
238 extern int mdb_ffs(uintmax_t);
239 
240 extern void mdb_nhconvert(void *, const void *, size_t);
241 
242 #define	MDB_DUMP_RELATIVE	0x0001	/* Start numbering at 0 */
243 #define	MDB_DUMP_ALIGN		0x0002	/* Enforce paragraph alignment */
244 #define	MDB_DUMP_PEDANT		0x0004	/* Full-width addresses */
245 #define	MDB_DUMP_ASCII		0x0008	/* Display ASCII values */
246 #define	MDB_DUMP_HEADER		0x0010	/* Display a header */
247 #define	MDB_DUMP_TRIM		0x0020	/* Trim at boundaries */
248 #define	MDB_DUMP_SQUISH		0x0040	/* Eliminate redundant lines */
249 #define	MDB_DUMP_NEWDOT		0x0080	/* Update dot when done */
250 #define	MDB_DUMP_ENDIAN		0x0100	/* Adjust for endianness */
251 #define	MDB_DUMP_WIDTH(x)	((((x) - 1) & 0xf) << 16) /* paragraphs/line */
252 #define	MDB_DUMP_GROUP(x)	((((x) - 1) & 0xff) << 20) /* bytes/group */
253 
254 typedef ssize_t (*mdb_dumpptr_cb_t)(void *, size_t, uintptr_t, void *);
255 typedef ssize_t (*mdb_dump64_cb_t)(void *, size_t, uint64_t, void *);
256 
257 extern int mdb_dumpptr(uintptr_t, size_t, uint_t, mdb_dumpptr_cb_t, void *);
258 extern int mdb_dump64(uint64_t, uint64_t, uint_t, mdb_dump64_cb_t, void *);
259 
260 extern const char *mdb_one_bit(int, int, int);
261 extern const char *mdb_inval_bits(int, int, int);
262 
263 extern ulong_t mdb_inc_indent(ulong_t);
264 extern ulong_t mdb_dec_indent(ulong_t);
265 
266 extern int mdb_eval(const char *);
267 extern void mdb_set_dot(uintmax_t);
268 extern uintmax_t mdb_get_dot(void);
269 
270 extern void mdb_get_pipe(mdb_pipe_t *);
271 extern void mdb_set_pipe(const mdb_pipe_t *);
272 
273 extern ssize_t mdb_get_xdata(const char *, void *, size_t);
274 
275 #define	MDB_STATE_IDLE		0	/* Target is idle (not running yet) */
276 #define	MDB_STATE_RUNNING	1	/* Target is currently executing */
277 #define	MDB_STATE_STOPPED	2	/* Target is stopped */
278 #define	MDB_STATE_UNDEAD	3	/* Target is undead (zombie) */
279 #define	MDB_STATE_DEAD		4	/* Target is dead (core dump) */
280 #define	MDB_STATE_LOST		5	/* Target lost by debugger */
281 
282 extern int mdb_get_state(void);
283 
284 #define	MDB_CALLBACK_STCHG	1
285 #define	MDB_CALLBACK_PROMPT	2
286 
287 typedef void (*mdb_callback_f)(void *);
288 
289 extern void *mdb_callback_add(int, mdb_callback_f, void *);
290 extern void mdb_callback_remove(void *);
291 
292 extern size_t strlcat(char *, const char *, size_t);
293 extern char *strcat(char *, const char *);
294 extern char *strcpy(char *, const char *);
295 extern char *strncpy(char *, const char *, size_t);
296 
297 /* Need to be consistent with <string.h> C++ definitions */
298 #if __cplusplus >= 199711L
299 extern const char *strchr(const char *, int);
300 #ifndef	_STRCHR_INLINE
301 #define	_STRCHR_INLINE
302 extern "C++" {
303 	inline char *strchr(char *__s, int __c) {
304 		return (char *)strchr((const char *)__s, __c);
305 	}
306 }
307 #endif	/* _STRCHR_INLINE */
308 extern const char *strrchr(const char *, int);
309 #ifndef	_STRRCHR_INLINE
310 #define	_STRRCHR_INLINE
311 extern	"C++" {
312 	inline char *strrchr(char *__s, int __c) {
313 		return (char *)strrchr((const char *)__s, __c);
314 	}
315 }
316 #endif	/* _STRRCHR_INLINE */
317 extern const char *strstr(const char *, const char *);
318 #ifndef	_STRSTR_INLINE
319 #define	_STRSTR_INLINE
320 extern "C++" {
321 	inline char *strstr(char *__s1, const char *__s2) {
322 		return (char *)strstr((const char *)__s1, __s2);
323 	}
324 }
325 #endif	/* _STRSTR_INLINE */
326 #else
327 extern char *strchr(const char *, int);
328 extern char *strrchr(const char *, int);
329 extern char *strstr(const char *, const char *);
330 #endif	/* __cplusplus >= 199711L */
331 
332 extern int strcmp(const char *, const char *);
333 extern int strncmp(const char *, const char *, size_t);
334 extern int strcasecmp(const char *, const char *);
335 extern int strncasecmp(const char *, const char *, size_t);
336 
337 extern size_t strlen(const char *);
338 
339 extern int bcmp(const void *, const void *, size_t);
340 extern void bcopy(const void *, void *, size_t);
341 extern void bzero(void *, size_t);
342 
343 extern void *memcpy(void *, const void *, size_t);
344 extern void *memmove(void *, const void *, size_t);
345 extern int memcmp(const void *, const void *, size_t);
346 /* Need to be consistent with <string.h> C++ definitions */
347 #if __cplusplus >= 199711L
348 extern const void *memchr(const void *, int, size_t);
349 #ifndef _MEMCHR_INLINE
350 #define	_MEMCHR_INLINE
351 extern "C++" {
352 	inline void *memchr(void * __s, int __c, size_t __n) {
353 		return (void *)memchr((const void *)__s, __c, __n);
354 	}
355 }
356 #endif  /* _MEMCHR_INLINE */
357 #else
358 extern void *memchr(const void *, int, size_t);
359 #endif /* __cplusplus >= 199711L */
360 extern void *memset(void *, int, size_t);
361 extern void *memccpy(void *, const void *, int, size_t);
362 
363 extern void *bsearch(const void *, const void *, size_t, size_t,
364     int (*)(const void *, const void *));
365 
366 extern void qsort(void *, size_t, size_t,
367     int (*)(const void *, const void *));
368 
369 #ifdef	__cplusplus
370 }
371 #endif
372 
373 #endif	/* _MDB_MODAPI_H */
374