xref: /illumos-gate/usr/src/cmd/sgs/demo_rdb/common/rdb.h (revision d9328cd4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57a5d89c4Sab  * Common Development and Distribution License (the "License").
67a5d89c4Sab  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2120c1c355SRod Evans 
227c478bd9Sstevel@tonic-gate /*
2320c1c355SRod Evans  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate #ifndef _RDB_H
267c478bd9Sstevel@tonic-gate #define	_RDB_H
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <rtld_db.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <procfs.h>
317c478bd9Sstevel@tonic-gate #include <proc_service.h>
327c478bd9Sstevel@tonic-gate #include <libelf.h>
337c478bd9Sstevel@tonic-gate #include <gelf.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <rdb_mach.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
387c478bd9Sstevel@tonic-gate extern "C" {
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Definitions from 2.7 sys/procfs_isa.h.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate #ifndef	PR_MODEL_LP64
457c478bd9Sstevel@tonic-gate #define	PR_MODEL_UNKNOWN 0
467c478bd9Sstevel@tonic-gate #define	PR_MODEL_ILP32	1	/* process data model is ILP32 */
477c478bd9Sstevel@tonic-gate #define	PR_MODEL_LP64	2	/* process data model is LP64 */
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	INTERPSECT	".interp"
517c478bd9Sstevel@tonic-gate #define	PLTSECT		".plt"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Flags for step_n routine
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate typedef enum {
577c478bd9Sstevel@tonic-gate 	FLG_SN_NONE = 0,
5820c1c355SRod Evans 	FLG_SN_VERBOSE = (1 << 0),	/* disassemble instructions */
597c478bd9Sstevel@tonic-gate 	FLG_SN_PLTSKIP = (1 << 1)	/* step *over* PLTS */
607c478bd9Sstevel@tonic-gate } sn_flags_e;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate typedef	enum {
647c478bd9Sstevel@tonic-gate 	RET_FAILED = -1,
657c478bd9Sstevel@tonic-gate 	RET_OK = 0
667c478bd9Sstevel@tonic-gate } retc_t;
677c478bd9Sstevel@tonic-gate 
687a5d89c4Sab /*
697a5d89c4Sab  * sym_tbl_t contains a primary and an (optional) auxiliary symbol table, which
707a5d89c4Sab  * we wish to treat as a single logical symbol table. In this logical table,
7120c1c355SRod Evans  * the data from the auxiliary table precedes that from the primary. Symbol
727a5d89c4Sab  * indices start at [0], which is the first item in the auxiliary table
737a5d89c4Sab  * if there is one. The sole purpose for this is so that we can treat the
747a5d89c4Sab  * combination of .SUNW_ldynsym and .dynsym sections as a logically single
757a5d89c4Sab  * entity without having to violate the public interface to libelf.
767a5d89c4Sab  *
777a5d89c4Sab  * Both tables must share the same string table section.
787a5d89c4Sab  *
797a5d89c4Sab  * The symtab_getsym() function serves as a gelf_getsym() replacement
807a5d89c4Sab  * that is aware of the two tables and makes them look like a single table
817a5d89c4Sab  * to the caller.
827a5d89c4Sab  *
837a5d89c4Sab  */
847c478bd9Sstevel@tonic-gate typedef struct sym_tbl {
8520c1c355SRod Evans 	Elf_Data	*st_syms_pri;	/* start of primary table */
8620c1c355SRod Evans 	Elf_Data	*st_syms_aux;	/* start of auxiliary table */
877a5d89c4Sab 	char		*st_strs;	/* ptr to strings */
887a5d89c4Sab 	size_t		st_symn;	/* Total # of entries in both tables */
897a5d89c4Sab 	size_t		st_symn_aux;	/* # of entries in auxiliary table */
907c478bd9Sstevel@tonic-gate } sym_tbl_t;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate typedef struct	map_info {
937a5d89c4Sab 	char			*mi_name;	/* file info */
947a5d89c4Sab 	char			*mi_refname;	/* filter reference name */
957c478bd9Sstevel@tonic-gate 	ulong_t			mi_addr;	/* start address */
967c478bd9Sstevel@tonic-gate 	ulong_t			mi_end;		/* end address */
977c478bd9Sstevel@tonic-gate 	int			mi_mapfd;	/* file desc. for mapping */
9820c1c355SRod Evans 	size_t			mi_pltentsz;	/* size of PLT entries */
997a5d89c4Sab 	Elf			*mi_elf;	/* elf handle so we can close */
1007c478bd9Sstevel@tonic-gate 	GElf_Ehdr		mi_ehdr;
1017c478bd9Sstevel@tonic-gate 	sym_tbl_t		mi_symtab;	/* symbol table */
1027c478bd9Sstevel@tonic-gate 	sym_tbl_t		mi_dynsym;	/* dynamic symbol table */
1037c478bd9Sstevel@tonic-gate 	Lmid_t			mi_lmident;	/* Link Map Ident */
1047c478bd9Sstevel@tonic-gate 	ulong_t			mi_pltbase;	/* PLT base address */
1057c478bd9Sstevel@tonic-gate 	ulong_t			mi_pltsize;	/* size of PLT table */
1067a5d89c4Sab 	struct map_info		*mi_next;
1077c478bd9Sstevel@tonic-gate 	ulong_t			mi_flags;	/* misc flags */
1087c478bd9Sstevel@tonic-gate 	rd_loadobj_t		mi_loadobj;	/* keep the old loadobj for */
1097c478bd9Sstevel@tonic-gate 						/* 	good luck */
1107c478bd9Sstevel@tonic-gate } map_info_t;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #define	FLG_MI_EXEC		0x0001		/* is object an EXEC */
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #define	FLG_PAP_SONAME		0x0001		/* embed SONAME in sym name */
1157c478bd9Sstevel@tonic-gate #define	FLG_PAP_NOHEXNAME	0x0002		/* if no symbol return */
1167c478bd9Sstevel@tonic-gate 						/* null string */
1177c478bd9Sstevel@tonic-gate #define	FLG_PAP_PLTDECOM	0x0004		/* decompe PLT name if */
1187c478bd9Sstevel@tonic-gate 						/* possible */
1197c478bd9Sstevel@tonic-gate typedef struct map_list {
1207a5d89c4Sab 	map_info_t		*ml_head;
1217a5d89c4Sab 	map_info_t		*ml_tail;
1227c478bd9Sstevel@tonic-gate } map_list_t;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * Break point information
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate typedef struct bpt_struct {
1287c478bd9Sstevel@tonic-gate 	ulong_t			bl_addr;	/* address of breakpoint */
1297c478bd9Sstevel@tonic-gate 	bptinstr_t 		bl_instr;	/* original instruction */
1307c478bd9Sstevel@tonic-gate 	unsigned		bl_flags;	/* break point flags */
1317a5d89c4Sab 	struct bpt_struct	*bl_next;
1327c478bd9Sstevel@tonic-gate } bptlist_t;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #define	FLG_BP_USERDEF		0x0001		/* user defined BP */
1357c478bd9Sstevel@tonic-gate #define	FLG_BP_RDPREINIT	0x0002		/* PREINIT BreakPoint */
1367c478bd9Sstevel@tonic-gate #define	FLG_BP_RDPOSTINIT	0x0004		/* POSTINIT BreakPoint */
1377c478bd9Sstevel@tonic-gate #define	FLG_BP_RDDLACT		0x0008		/* DLACT BreakPoint */
1387c478bd9Sstevel@tonic-gate #define	FLG_BP_PLTRES		0x0010		/* PLT Resolve BP */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #define	MASK_BP_SPECIAL \
1417c478bd9Sstevel@tonic-gate 		(FLG_BP_RDPREINIT | FLG_BP_RDPOSTINIT | FLG_BP_RDDLACT)
1427c478bd9Sstevel@tonic-gate #define	MASK_BP_STOP \
1437c478bd9Sstevel@tonic-gate 		(FLG_BP_USERDEF | FLG_BP_PLTRES)
1447c478bd9Sstevel@tonic-gate #define	MASK_BP_ALL \
1457c478bd9Sstevel@tonic-gate 		(MASK_BP_SPECIAL | FLG_BP_USERDEF)
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Proc Services Structure
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate struct ps_prochandle {
1517c478bd9Sstevel@tonic-gate 	pid_t		pp_pid;		/* debug process pid */
1527a5d89c4Sab 	rd_agent_t	*pp_rap;	/* rtld_db handle */
1537c478bd9Sstevel@tonic-gate 	int		pp_ctlfd;	/* open ctl proc fd */
1547c478bd9Sstevel@tonic-gate 	int		pp_statusfd;	/* open status proc fd */
1557c478bd9Sstevel@tonic-gate 	int		pp_asfd;	/* open as proc fd */
1567c478bd9Sstevel@tonic-gate 	int		pp_mapfd;	/* open map proc fd */
1577c478bd9Sstevel@tonic-gate 	uintptr_t	pp_ldsobase;	/* ld.so.1 base address */
1587c478bd9Sstevel@tonic-gate 	uintptr_t	pp_execphdr;	/* a.out phdr address */
1597c478bd9Sstevel@tonic-gate 	map_info_t	pp_ldsomap;	/* ld.so.1 map info */
1607c478bd9Sstevel@tonic-gate 	map_info_t	pp_execmap;	/* exec map info */
1617c478bd9Sstevel@tonic-gate 	map_list_t	pp_lmaplist;	/* list of link map infos */
1627a5d89c4Sab 	bptlist_t	*pp_breakpoints; /* break point list */
1637a5d89c4Sab 	void		*pp_auxvp;	/* pointer to AUX vectors */
1647c478bd9Sstevel@tonic-gate 	int		pp_flags;	/* misc flags */
1657c478bd9Sstevel@tonic-gate 	int		pp_dmodel;	/* data model */
1667c478bd9Sstevel@tonic-gate };
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #define	FLG_PP_PROMPT	0x0001		/* display debugger prompt */
1697c478bd9Sstevel@tonic-gate #define	FLG_PP_LMAPS	0x0002		/* link maps available */
1707c478bd9Sstevel@tonic-gate #define	FLG_PP_PACT	0x0004		/* active process being traced */
1717c478bd9Sstevel@tonic-gate #define	FLG_PP_PLTSKIP	0x0008		/* PLT skipping is active */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * Debugging Structure
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate typedef struct rtld_debug {
1777c478bd9Sstevel@tonic-gate 	int		rd_vers;
1787c478bd9Sstevel@tonic-gate 	caddr_t		rd_preinit;
1797c478bd9Sstevel@tonic-gate 	caddr_t		rd_postinit;
1807c478bd9Sstevel@tonic-gate } rtld_debug_t;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #define	TRAPBREAK	0x91d02001	/* ta	ST_BREAKPOINT */
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * values for rdb_flags
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate #define	RDB_FL_EVENTS	0x0001		/* enable printing event information */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * Globals
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate extern struct ps_prochandle	proch;
1947c478bd9Sstevel@tonic-gate extern unsigned long		rdb_flags;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Functions
1987c478bd9Sstevel@tonic-gate  */
1997a5d89c4Sab extern map_info_t	*addr_to_map(struct ps_prochandle *, ulong_t);
2007c478bd9Sstevel@tonic-gate extern retc_t		addr_to_sym(struct ps_prochandle *, ulong_t,
2017c478bd9Sstevel@tonic-gate 				GElf_Sym *, char **);
2027a5d89c4Sab extern void		CallStack(struct ps_prochandle *ph);
2037c478bd9Sstevel@tonic-gate extern unsigned		continue_to_break(struct ps_prochandle *);
2047c478bd9Sstevel@tonic-gate extern retc_t		delete_all_breakpoints(struct ps_prochandle *);
2057c478bd9Sstevel@tonic-gate extern retc_t		delete_breakpoint(struct ps_prochandle *, ulong_t,
2067c478bd9Sstevel@tonic-gate 				unsigned);
2077c478bd9Sstevel@tonic-gate extern void		disasm(struct ps_prochandle *, int);
2087c478bd9Sstevel@tonic-gate extern retc_t		disasm_addr(struct ps_prochandle *, ulong_t, int);
2097c478bd9Sstevel@tonic-gate extern retc_t		display_all_regs(struct ps_prochandle *);
2107c478bd9Sstevel@tonic-gate extern retc_t		display_maps(struct ps_prochandle *);
2117c478bd9Sstevel@tonic-gate extern retc_t		display_linkmaps(struct ps_prochandle *);
2127c478bd9Sstevel@tonic-gate extern void		free_linkmaps(struct ps_prochandle *);
2137c478bd9Sstevel@tonic-gate extern retc_t		get_linkmaps(struct ps_prochandle *);
2147c478bd9Sstevel@tonic-gate extern ulong_t		hexstr_to_num(const char *);
2157c478bd9Sstevel@tonic-gate extern ulong_t		is_plt(struct ps_prochandle *, ulong_t);
2167c478bd9Sstevel@tonic-gate extern void		list_breakpoints(struct ps_prochandle *);
2177c478bd9Sstevel@tonic-gate extern retc_t		load_map(struct ps_prochandle *, caddr_t,
2187a5d89c4Sab 				map_info_t *mp);
2197a5d89c4Sab extern char		*print_address(unsigned long);
2207a5d89c4Sab extern char		*print_address_ps(struct ps_prochandle *,
2217c478bd9Sstevel@tonic-gate 				unsigned long, unsigned);
2227c478bd9Sstevel@tonic-gate extern void		print_mem(struct ps_prochandle *, ulong_t, int,
2237c478bd9Sstevel@tonic-gate 				char *);
2247c478bd9Sstevel@tonic-gate extern void		print_varstring(struct ps_prochandle *, const char *);
2257c478bd9Sstevel@tonic-gate extern void		print_mach_varstring(struct ps_prochandle *,
2267c478bd9Sstevel@tonic-gate 				const char *);
2277c478bd9Sstevel@tonic-gate extern void		rdb_help(const char *);
2287c478bd9Sstevel@tonic-gate extern void		rdb_prompt();
2297c478bd9Sstevel@tonic-gate extern void		perr(char *);
2307c478bd9Sstevel@tonic-gate extern retc_t		proc_string_read(struct ps_prochandle *,
2317c478bd9Sstevel@tonic-gate 				ulong_t, char *, int);
2327c478bd9Sstevel@tonic-gate extern retc_t		ps_close(struct ps_prochandle *);
2337c478bd9Sstevel@tonic-gate extern retc_t		ps_init(int, int, pid_t, struct ps_prochandle *);
2347c478bd9Sstevel@tonic-gate extern retc_t		set_breakpoint(struct ps_prochandle *, ulong_t,
2357c478bd9Sstevel@tonic-gate 				unsigned);
2367c478bd9Sstevel@tonic-gate extern retc_t		set_objpad(struct ps_prochandle *, size_t);
2377c478bd9Sstevel@tonic-gate extern retc_t		step_n(struct ps_prochandle *, size_t, sn_flags_e);
2387c478bd9Sstevel@tonic-gate extern void		step_to_addr(struct ps_prochandle *, ulong_t);
2397c478bd9Sstevel@tonic-gate extern retc_t		str_map_sym(const char *, map_info_t *, GElf_Sym *,
2407c478bd9Sstevel@tonic-gate 				char **);
2417a5d89c4Sab extern map_info_t	*str_to_map(struct ps_prochandle *, const char *);
2427c478bd9Sstevel@tonic-gate extern retc_t		str_to_sym(struct ps_prochandle *, const char *,
2437c478bd9Sstevel@tonic-gate 				GElf_Sym *);
2447c478bd9Sstevel@tonic-gate extern int		yyparse(void);
24520c1c355SRod Evans extern int		yyerror(char *);
2467c478bd9Sstevel@tonic-gate extern int		yylex(void);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate #endif
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate #endif /* _RDB_H */
253