xref: /onnv-gate/usr/src/lib/libproc/common/Pcontrol.h (revision 7675:df8814607da7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52712Snn35248  * Common Development and Distribution License (the "License").
62712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7675SEdward.Pilatowicz@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #ifndef	_PCONTROL_H
270Sstevel@tonic-gate #define	_PCONTROL_H
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Implemention-specific include file for libproc process management.
310Sstevel@tonic-gate  * This is not to be seen by the clients of libproc.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <gelf.h>
360Sstevel@tonic-gate #include <synch.h>
370Sstevel@tonic-gate #include <procfs.h>
380Sstevel@tonic-gate #include <rtld_db.h>
390Sstevel@tonic-gate #include <libproc.h>
400Sstevel@tonic-gate #include <libctf.h>
412712Snn35248 #include <limits.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #ifdef	__cplusplus
440Sstevel@tonic-gate extern "C" {
450Sstevel@tonic-gate #endif
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include "Putil.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Definitions of the process control structures, internal to libproc.
510Sstevel@tonic-gate  * These may change without affecting clients of libproc.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
543347Sab196087 /*
553347Sab196087  * sym_tbl_t contains a primary and an (optional) auxiliary symbol table, which
563347Sab196087  * we wish to treat as a single logical symbol table. In this logical table,
573347Sab196087  * the data from the auxiliary table preceeds that from the primary. Symbol
583347Sab196087  * indices start at [0], which is the first item in the auxiliary table
593347Sab196087  * if there is one. The sole purpose for this is so that we can treat the
603347Sab196087  * combination of .SUNW_ldynsym and .dynsym sections as a logically single
613347Sab196087  * entity without having to violate the public interface to libelf.
623347Sab196087  *
633347Sab196087  * Both tables must share the same string table section.
643347Sab196087  *
653347Sab196087  * The symtab_getsym() function serves as a gelf_getsym() replacement
663347Sab196087  * that is aware of the two tables and makes them look like a single table
673347Sab196087  * to the caller.
683347Sab196087  *
693347Sab196087  */
70942Sahl typedef struct sym_tbl {	/* symbol table */
713347Sab196087 	Elf_Data *sym_data_pri;	/* primary table */
723347Sab196087 	Elf_Data *sym_data_aux;	/* auxiliary table */
733347Sab196087 	size_t	sym_symn_aux;	/* number of entries in auxiliary table */
743347Sab196087 	size_t	sym_symn;	/* total number of entries in both tables */
750Sstevel@tonic-gate 	char	*sym_strs;	/* ptr to strings */
760Sstevel@tonic-gate 	size_t	sym_strsz;	/* size of string table */
773347Sab196087 	GElf_Shdr sym_hdr_pri;	/* primary symbol table section header */
783347Sab196087 	GElf_Shdr sym_hdr_aux;	/* auxiliary symbol table section header */
790Sstevel@tonic-gate 	GElf_Shdr sym_strhdr;	/* string table section header */
80942Sahl 	Elf	*sym_elf;	/* faked-up ELF handle from core file */
81942Sahl 	void	*sym_elfmem;	/* data for faked-up ELF handle */
820Sstevel@tonic-gate 	uint_t	*sym_byname;	/* symbols sorted by name */
830Sstevel@tonic-gate 	uint_t	*sym_byaddr;	/* symbols sorted by addr */
840Sstevel@tonic-gate 	size_t	sym_count;	/* number of symbols in each sorted list */
850Sstevel@tonic-gate } sym_tbl_t;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate typedef struct file_info {	/* symbol information for a mapped file */
88789Sahrens 	plist_t	file_list;	/* linked list */
890Sstevel@tonic-gate 	char	file_pname[PRMAPSZ];	/* name from prmap_t */
900Sstevel@tonic-gate 	struct map_info *file_map;	/* primary (text) mapping */
910Sstevel@tonic-gate 	int	file_ref;	/* references from map_info_t structures */
920Sstevel@tonic-gate 	int	file_fd;	/* file descriptor for the mapped file */
930Sstevel@tonic-gate 	int	file_init;	/* 0: initialization yet to be performed */
940Sstevel@tonic-gate 	GElf_Half file_etype;	/* ELF e_type from ehdr */
950Sstevel@tonic-gate 	GElf_Half file_class;	/* ELF e_ident[EI_CLASS] from ehdr */
960Sstevel@tonic-gate 	rd_loadobj_t *file_lo;	/* load object structure from rtld_db */
970Sstevel@tonic-gate 	char	*file_lname;	/* load object name from rtld_db */
980Sstevel@tonic-gate 	char	*file_lbase;	/* pointer to basename of file_lname */
99*7675SEdward.Pilatowicz@Sun.COM 	char	*file_rname;	/* resolved on-disk object pathname */
100*7675SEdward.Pilatowicz@Sun.COM 	char	*file_rbase;	/* pointer to basename of file_rname */
101942Sahl 	Elf	*file_elf;	/* ELF handle so we can close */
102942Sahl 	void	*file_elfmem;	/* data for faked-up ELF handle */
1030Sstevel@tonic-gate 	sym_tbl_t file_symtab;	/* symbol table */
1040Sstevel@tonic-gate 	sym_tbl_t file_dynsym;	/* dynamic symbol table */
1050Sstevel@tonic-gate 	uintptr_t file_dyn_base;	/* load address for ET_DYN files */
1060Sstevel@tonic-gate 	uintptr_t file_plt_base;	/* base address for PLT */
1070Sstevel@tonic-gate 	size_t	file_plt_size;	/* size of PLT region */
1080Sstevel@tonic-gate 	uintptr_t file_jmp_rel;	/* base address of PLT relocations */
1090Sstevel@tonic-gate 	uintptr_t file_ctf_off;	/* offset of CTF data in object file */
1100Sstevel@tonic-gate 	size_t	file_ctf_size;	/* size of CTF data in object file */
1110Sstevel@tonic-gate 	int	file_ctf_dyn;	/* does the CTF data reference the dynsym */
1120Sstevel@tonic-gate 	void	*file_ctf_buf;	/* CTF data for this file */
1130Sstevel@tonic-gate 	ctf_file_t *file_ctfp;	/* CTF container for this file */
1141160Svb160487 	char	*file_shstrs;	/* section header string table */
1151160Svb160487 	size_t	file_shstrsz;	/* section header string table size */
1162915Srh87107 	uintptr_t *file_saddrs; /* section header addresses */
1172915Srh87107 	uint_t  file_nsaddrs;   /* number of section header addresses */
1180Sstevel@tonic-gate } file_info_t;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate typedef struct map_info {	/* description of an address space mapping */
1210Sstevel@tonic-gate 	prmap_t	map_pmap;	/* /proc description of this mapping */
1220Sstevel@tonic-gate 	file_info_t *map_file;	/* pointer into list of mapped files */
1230Sstevel@tonic-gate 	off64_t map_offset;	/* offset into core file (if core) */
1240Sstevel@tonic-gate 	int map_relocate;	/* associated file_map needs to be relocated */
1250Sstevel@tonic-gate } map_info_t;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate typedef struct lwp_info {	/* per-lwp information from core file */
128789Sahrens 	plist_t	lwp_list;	/* linked list */
1290Sstevel@tonic-gate 	lwpid_t	lwp_id;		/* lwp identifier */
1300Sstevel@tonic-gate 	lwpsinfo_t lwp_psinfo;	/* /proc/<pid>/lwp/<lwpid>/lwpsinfo data */
1310Sstevel@tonic-gate 	lwpstatus_t lwp_status;	/* /proc/<pid>/lwp/<lwpid>/lwpstatus data */
1320Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
1330Sstevel@tonic-gate 	gwindows_t *lwp_gwins;	/* /proc/<pid>/lwp/<lwpid>/gwindows data */
1340Sstevel@tonic-gate 	prxregset_t *lwp_xregs;	/* /proc/<pid>/lwp/<lwpid>/xregs data */
1350Sstevel@tonic-gate 	int64_t *lwp_asrs;	/* /proc/<pid>/lwp/<lwpid>/asrs data */
1360Sstevel@tonic-gate #endif
1370Sstevel@tonic-gate } lwp_info_t;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate typedef struct core_info {	/* information specific to core files */
1400Sstevel@tonic-gate 	char core_dmodel;	/* data model for core file */
1410Sstevel@tonic-gate 	int core_errno;		/* error during initialization if != 0 */
142789Sahrens 	plist_t core_lwp_head;	/* head of list of lwp info */
1430Sstevel@tonic-gate 	lwp_info_t *core_lwp;	/* current lwp information */
1440Sstevel@tonic-gate 	uint_t core_nlwp;	/* number of lwp's in list */
1450Sstevel@tonic-gate 	off64_t core_size;	/* size of core file in bytes */
1460Sstevel@tonic-gate 	char *core_platform;	/* platform string from core file */
1470Sstevel@tonic-gate 	struct utsname *core_uts;	/* uname(2) data from core file */
1480Sstevel@tonic-gate 	prcred_t *core_cred;	/* process credential from core file */
1490Sstevel@tonic-gate 	core_content_t core_content;	/* content dumped to core file */
1500Sstevel@tonic-gate 	prpriv_t *core_priv;	/* process privileges from core file */
1510Sstevel@tonic-gate 	size_t core_priv_size;	/* size of the privileges */
1520Sstevel@tonic-gate 	void *core_privinfo;	/* system privileges info from core file */
1530Sstevel@tonic-gate 	priv_impl_info_t *core_ppii;	/* NOTE entry for core_privinfo */
1540Sstevel@tonic-gate 	char *core_zonename;	/* zone name from core file */
1550Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1560Sstevel@tonic-gate 	struct ssd *core_ldt;	/* LDT entries from core file */
1570Sstevel@tonic-gate 	uint_t core_nldt;	/* number of LDT entries in core file */
1580Sstevel@tonic-gate #endif
1590Sstevel@tonic-gate } core_info_t;
1600Sstevel@tonic-gate 
161942Sahl typedef struct elf_file_header { /* extended ELF header */
162942Sahl 	unsigned char e_ident[EI_NIDENT];
163942Sahl 	Elf64_Half e_type;
164942Sahl 	Elf64_Half e_machine;
165942Sahl 	Elf64_Word e_version;
166942Sahl 	Elf64_Addr e_entry;
167942Sahl 	Elf64_Off e_phoff;
168942Sahl 	Elf64_Off e_shoff;
169942Sahl 	Elf64_Word e_flags;
170942Sahl 	Elf64_Half e_ehsize;
171942Sahl 	Elf64_Half e_phentsize;
172942Sahl 	Elf64_Half e_shentsize;
173942Sahl 	Elf64_Word e_phnum;	/* phdr count extended to 32 bits */
174942Sahl 	Elf64_Word e_shnum;	/* shdr count extended to 32 bits */
175942Sahl 	Elf64_Word e_shstrndx;	/* shdr string index extended to 32 bits */
176942Sahl } elf_file_header_t;
177942Sahl 
1780Sstevel@tonic-gate typedef struct elf_file {	/* convenience for managing ELF files */
179942Sahl 	elf_file_header_t e_hdr; /* Extended ELF header */
1800Sstevel@tonic-gate 	Elf *e_elf;		/* ELF library handle */
1810Sstevel@tonic-gate 	int e_fd;		/* file descriptor */
1820Sstevel@tonic-gate } elf_file_t;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate typedef struct ps_rwops {	/* ops vector for Pread() and Pwrite() */
1850Sstevel@tonic-gate 	ssize_t (*p_pread)(struct ps_prochandle *,
1860Sstevel@tonic-gate 	    void *, size_t, uintptr_t);
1870Sstevel@tonic-gate 	ssize_t (*p_pwrite)(struct ps_prochandle *,
1880Sstevel@tonic-gate 	    const void *, size_t, uintptr_t);
1890Sstevel@tonic-gate } ps_rwops_t;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate #define	HASHSIZE		1024	/* hash table size, power of 2 */
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate struct ps_prochandle {
1940Sstevel@tonic-gate 	struct ps_lwphandle **hashtab;	/* hash table for LWPs (Lgrab()) */
1950Sstevel@tonic-gate 	mutex_t	proc_lock;	/* protects hash table; serializes Lgrab() */
1960Sstevel@tonic-gate 	pstatus_t orig_status;	/* remembered status on Pgrab() */
1970Sstevel@tonic-gate 	pstatus_t status;	/* status when stopped */
1980Sstevel@tonic-gate 	psinfo_t psinfo;	/* psinfo_t from last Ppsinfo() request */
1990Sstevel@tonic-gate 	uintptr_t sysaddr;	/* address of most recent syscall instruction */
2000Sstevel@tonic-gate 	pid_t	pid;		/* process-ID */
2010Sstevel@tonic-gate 	int	state;		/* state of the process, see "libproc.h" */
2020Sstevel@tonic-gate 	uint_t	flags;		/* see defines below */
2030Sstevel@tonic-gate 	uint_t	agentcnt;	/* Pcreate_agent()/Pdestroy_agent() ref count */
2040Sstevel@tonic-gate 	int	asfd;		/* /proc/<pid>/as filedescriptor */
2050Sstevel@tonic-gate 	int	ctlfd;		/* /proc/<pid>/ctl filedescriptor */
2060Sstevel@tonic-gate 	int	statfd;		/* /proc/<pid>/status filedescriptor */
2070Sstevel@tonic-gate 	int	agentctlfd;	/* /proc/<pid>/lwp/agent/ctl */
2080Sstevel@tonic-gate 	int	agentstatfd;	/* /proc/<pid>/lwp/agent/status */
2090Sstevel@tonic-gate 	int	info_valid;	/* if zero, map and file info need updating */
2100Sstevel@tonic-gate 	map_info_t *mappings;	/* cached process mappings */
2110Sstevel@tonic-gate 	size_t	map_count;	/* number of mappings */
2120Sstevel@tonic-gate 	size_t	map_alloc;	/* number of mappings allocated */
2130Sstevel@tonic-gate 	uint_t	num_files;	/* number of file elements in file_info */
214789Sahrens 	plist_t	file_head;	/* head of mapped files w/ symbol table info */
2150Sstevel@tonic-gate 	char	*execname;	/* name of the executable file */
2160Sstevel@tonic-gate 	auxv_t	*auxv;		/* the process's aux vector */
2170Sstevel@tonic-gate 	int	nauxv;		/* number of aux vector entries */
2180Sstevel@tonic-gate 	rd_agent_t *rap;	/* cookie for rtld_db */
2190Sstevel@tonic-gate 	map_info_t *map_exec;	/* the mapping for the executable file */
2200Sstevel@tonic-gate 	map_info_t *map_ldso;	/* the mapping for ld.so.1 */
2210Sstevel@tonic-gate 	const ps_rwops_t *ops;	/* pointer to ops-vector for read and write */
2220Sstevel@tonic-gate 	core_info_t *core;	/* information specific to core (if PS_DEAD) */
2230Sstevel@tonic-gate 	uintptr_t *ucaddrs;	/* ucontext-list addresses */
2240Sstevel@tonic-gate 	uint_t	ucnelems;	/* number of elements in the ucaddrs list */
225*7675SEdward.Pilatowicz@Sun.COM 	char	*zoneroot;	/* cached path to zone root */
2260Sstevel@tonic-gate };
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /* flags */
2290Sstevel@tonic-gate #define	CREATED		0x01	/* process was created by Pcreate() */
2300Sstevel@tonic-gate #define	SETSIG		0x02	/* set signal trace mask before continuing */
2310Sstevel@tonic-gate #define	SETFAULT	0x04	/* set fault trace mask before continuing */
2320Sstevel@tonic-gate #define	SETENTRY	0x08	/* set sysentry trace mask before continuing */
2330Sstevel@tonic-gate #define	SETEXIT		0x10	/* set sysexit trace mask before continuing */
2340Sstevel@tonic-gate #define	SETHOLD		0x20	/* set signal hold mask before continuing */
2350Sstevel@tonic-gate #define	SETREGS		0x40	/* set registers before continuing */
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate struct ps_lwphandle {
2380Sstevel@tonic-gate 	struct ps_prochandle *lwp_proc;	/* process to which this lwp belongs */
2390Sstevel@tonic-gate 	struct ps_lwphandle *lwp_hash;	/* hash table linked list */
2400Sstevel@tonic-gate 	lwpstatus_t	lwp_status;	/* status when stopped */
2410Sstevel@tonic-gate 	lwpsinfo_t	lwp_psinfo;	/* lwpsinfo_t from last Lpsinfo() */
2420Sstevel@tonic-gate 	lwpid_t		lwp_id;		/* lwp identifier */
2430Sstevel@tonic-gate 	int		lwp_state;	/* state of the lwp, see "libproc.h" */
2440Sstevel@tonic-gate 	uint_t		lwp_flags;	/* SETHOLD and/or SETREGS */
2450Sstevel@tonic-gate 	int		lwp_ctlfd;	/* /proc/<pid>/lwp/<lwpid>/lwpctl */
2460Sstevel@tonic-gate 	int		lwp_statfd;	/* /proc/<pid>/lwp/<lwpid>/lwpstatus */
2470Sstevel@tonic-gate };
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate  * Implementation functions in the process control library.
2510Sstevel@tonic-gate  * These are not exported to clients of the library.
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate extern	void	prldump(const char *, lwpstatus_t *);
2540Sstevel@tonic-gate extern	int	dupfd(int, int);
2550Sstevel@tonic-gate extern	int	set_minfd(void);
2560Sstevel@tonic-gate extern	int	Pscantext(struct ps_prochandle *);
2570Sstevel@tonic-gate extern	void	Pinitsym(struct ps_prochandle *);
2580Sstevel@tonic-gate extern	void	Preadauxvec(struct ps_prochandle *);
2590Sstevel@tonic-gate extern	void	optimize_symtab(sym_tbl_t *);
2600Sstevel@tonic-gate extern	void	Pbuild_file_symtab(struct ps_prochandle *, file_info_t *);
2610Sstevel@tonic-gate extern	ctf_file_t *Pbuild_file_ctf(struct ps_prochandle *, file_info_t *);
2620Sstevel@tonic-gate extern	map_info_t *Paddr2mptr(struct ps_prochandle *, uintptr_t);
2630Sstevel@tonic-gate extern	char 	*Pfindexec(struct ps_prochandle *, const char *,
2640Sstevel@tonic-gate 	int (*)(const char *, void *), void *);
2650Sstevel@tonic-gate extern	int	getlwpstatus(struct ps_prochandle *, lwpid_t, lwpstatus_t *);
2660Sstevel@tonic-gate int	Pstopstatus(struct ps_prochandle *, long, uint32_t);
2674753Srh87107 extern	file_info_t *file_info_new(struct ps_prochandle *, map_info_t *);
268*7675SEdward.Pilatowicz@Sun.COM extern	char	*Plofspath(const char *, char *, size_t);
269*7675SEdward.Pilatowicz@Sun.COM extern	char	*Pzoneroot(struct ps_prochandle *, char *, size_t);
270*7675SEdward.Pilatowicz@Sun.COM extern	char	*Pzonepath(struct ps_prochandle *, const char *, char *,
271*7675SEdward.Pilatowicz@Sun.COM 	size_t);
272*7675SEdward.Pilatowicz@Sun.COM extern	char	*Pfindmap(struct ps_prochandle *, map_info_t *, char *,
273*7675SEdward.Pilatowicz@Sun.COM 	size_t);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate extern	int	Padd_mapping(struct ps_prochandle *, off64_t, file_info_t *,
2760Sstevel@tonic-gate     prmap_t *);
2770Sstevel@tonic-gate extern	void	Psort_mappings(struct ps_prochandle *);
2780Sstevel@tonic-gate 
2792712Snn35248 extern char	procfs_path[PATH_MAX];
280789Sahrens 
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate  * Architecture-dependent definition of the breakpoint instruction.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate #if defined(sparc) || defined(__sparc)
2850Sstevel@tonic-gate #define	BPT	((instr_t)0x91d02001)
2860Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)
2870Sstevel@tonic-gate #define	BPT	((instr_t)0xcc)
2880Sstevel@tonic-gate #endif
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate  * Simple convenience.
2920Sstevel@tonic-gate  */
2930Sstevel@tonic-gate #define	TRUE	1
2940Sstevel@tonic-gate #define	FALSE	0
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate #ifdef	__cplusplus
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate #endif
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate #endif	/* _PCONTROL_H */
301