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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #ifndef _PCONTROL_H 287c478bd9Sstevel@tonic-gate #define _PCONTROL_H 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* 337c478bd9Sstevel@tonic-gate * Implemention-specific include file for libproc process management. 347c478bd9Sstevel@tonic-gate * This is not to be seen by the clients of libproc. 357c478bd9Sstevel@tonic-gate */ 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #include <stdio.h> 387c478bd9Sstevel@tonic-gate #include <gelf.h> 397c478bd9Sstevel@tonic-gate #include <synch.h> 407c478bd9Sstevel@tonic-gate #include <procfs.h> 417c478bd9Sstevel@tonic-gate #include <rtld_db.h> 427c478bd9Sstevel@tonic-gate #include <libproc.h> 437c478bd9Sstevel@tonic-gate #include <libctf.h> 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #ifdef __cplusplus 467c478bd9Sstevel@tonic-gate extern "C" { 477c478bd9Sstevel@tonic-gate #endif 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #include "Putil.h" 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* 527c478bd9Sstevel@tonic-gate * Definitions of the process control structures, internal to libproc. 537c478bd9Sstevel@tonic-gate * These may change without affecting clients of libproc. 547c478bd9Sstevel@tonic-gate */ 557c478bd9Sstevel@tonic-gate 5630da1432Sahl typedef struct sym_tbl { /* symbol table */ 577c478bd9Sstevel@tonic-gate Elf_Data *sym_data; /* start of table */ 587c478bd9Sstevel@tonic-gate size_t sym_symn; /* number of entries */ 597c478bd9Sstevel@tonic-gate char *sym_strs; /* ptr to strings */ 607c478bd9Sstevel@tonic-gate size_t sym_strsz; /* size of string table */ 617c478bd9Sstevel@tonic-gate GElf_Shdr sym_hdr; /* symbol table section header */ 627c478bd9Sstevel@tonic-gate GElf_Shdr sym_strhdr; /* string table section header */ 6330da1432Sahl Elf *sym_elf; /* faked-up ELF handle from core file */ 6430da1432Sahl void *sym_elfmem; /* data for faked-up ELF handle */ 657c478bd9Sstevel@tonic-gate uint_t *sym_byname; /* symbols sorted by name */ 667c478bd9Sstevel@tonic-gate uint_t *sym_byaddr; /* symbols sorted by addr */ 677c478bd9Sstevel@tonic-gate size_t sym_count; /* number of symbols in each sorted list */ 687c478bd9Sstevel@tonic-gate } sym_tbl_t; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate typedef struct file_info { /* symbol information for a mapped file */ 71fa9e4066Sahrens plist_t file_list; /* linked list */ 727c478bd9Sstevel@tonic-gate char file_pname[PRMAPSZ]; /* name from prmap_t */ 737c478bd9Sstevel@tonic-gate struct map_info *file_map; /* primary (text) mapping */ 747c478bd9Sstevel@tonic-gate int file_ref; /* references from map_info_t structures */ 757c478bd9Sstevel@tonic-gate int file_fd; /* file descriptor for the mapped file */ 767c478bd9Sstevel@tonic-gate int file_init; /* 0: initialization yet to be performed */ 777c478bd9Sstevel@tonic-gate GElf_Half file_etype; /* ELF e_type from ehdr */ 787c478bd9Sstevel@tonic-gate GElf_Half file_class; /* ELF e_ident[EI_CLASS] from ehdr */ 797c478bd9Sstevel@tonic-gate rd_loadobj_t *file_lo; /* load object structure from rtld_db */ 807c478bd9Sstevel@tonic-gate char *file_lname; /* load object name from rtld_db */ 817c478bd9Sstevel@tonic-gate char *file_lbase; /* pointer to basename of file_lname */ 8230da1432Sahl Elf *file_elf; /* ELF handle so we can close */ 8330da1432Sahl void *file_elfmem; /* data for faked-up ELF handle */ 847c478bd9Sstevel@tonic-gate sym_tbl_t file_symtab; /* symbol table */ 857c478bd9Sstevel@tonic-gate sym_tbl_t file_dynsym; /* dynamic symbol table */ 867c478bd9Sstevel@tonic-gate uintptr_t file_dyn_base; /* load address for ET_DYN files */ 877c478bd9Sstevel@tonic-gate uintptr_t file_plt_base; /* base address for PLT */ 887c478bd9Sstevel@tonic-gate size_t file_plt_size; /* size of PLT region */ 897c478bd9Sstevel@tonic-gate uintptr_t file_jmp_rel; /* base address of PLT relocations */ 907c478bd9Sstevel@tonic-gate uintptr_t file_ctf_off; /* offset of CTF data in object file */ 917c478bd9Sstevel@tonic-gate size_t file_ctf_size; /* size of CTF data in object file */ 927c478bd9Sstevel@tonic-gate int file_ctf_dyn; /* does the CTF data reference the dynsym */ 937c478bd9Sstevel@tonic-gate void *file_ctf_buf; /* CTF data for this file */ 947c478bd9Sstevel@tonic-gate ctf_file_t *file_ctfp; /* CTF container for this file */ 95*f957ecc1Svb160487 char *file_shstrs; /* section header string table */ 96*f957ecc1Svb160487 size_t file_shstrsz; /* section header string table size */ 977c478bd9Sstevel@tonic-gate } file_info_t; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate typedef struct map_info { /* description of an address space mapping */ 1007c478bd9Sstevel@tonic-gate prmap_t map_pmap; /* /proc description of this mapping */ 1017c478bd9Sstevel@tonic-gate file_info_t *map_file; /* pointer into list of mapped files */ 1027c478bd9Sstevel@tonic-gate off64_t map_offset; /* offset into core file (if core) */ 1037c478bd9Sstevel@tonic-gate int map_relocate; /* associated file_map needs to be relocated */ 1047c478bd9Sstevel@tonic-gate } map_info_t; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate typedef struct lwp_info { /* per-lwp information from core file */ 107fa9e4066Sahrens plist_t lwp_list; /* linked list */ 1087c478bd9Sstevel@tonic-gate lwpid_t lwp_id; /* lwp identifier */ 1097c478bd9Sstevel@tonic-gate lwpsinfo_t lwp_psinfo; /* /proc/<pid>/lwp/<lwpid>/lwpsinfo data */ 1107c478bd9Sstevel@tonic-gate lwpstatus_t lwp_status; /* /proc/<pid>/lwp/<lwpid>/lwpstatus data */ 1117c478bd9Sstevel@tonic-gate #if defined(sparc) || defined(__sparc) 1127c478bd9Sstevel@tonic-gate gwindows_t *lwp_gwins; /* /proc/<pid>/lwp/<lwpid>/gwindows data */ 1137c478bd9Sstevel@tonic-gate prxregset_t *lwp_xregs; /* /proc/<pid>/lwp/<lwpid>/xregs data */ 1147c478bd9Sstevel@tonic-gate int64_t *lwp_asrs; /* /proc/<pid>/lwp/<lwpid>/asrs data */ 1157c478bd9Sstevel@tonic-gate #endif 1167c478bd9Sstevel@tonic-gate } lwp_info_t; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate typedef struct core_info { /* information specific to core files */ 1197c478bd9Sstevel@tonic-gate char core_dmodel; /* data model for core file */ 1207c478bd9Sstevel@tonic-gate int core_errno; /* error during initialization if != 0 */ 121fa9e4066Sahrens plist_t core_lwp_head; /* head of list of lwp info */ 1227c478bd9Sstevel@tonic-gate lwp_info_t *core_lwp; /* current lwp information */ 1237c478bd9Sstevel@tonic-gate uint_t core_nlwp; /* number of lwp's in list */ 1247c478bd9Sstevel@tonic-gate off64_t core_size; /* size of core file in bytes */ 1257c478bd9Sstevel@tonic-gate char *core_platform; /* platform string from core file */ 1267c478bd9Sstevel@tonic-gate struct utsname *core_uts; /* uname(2) data from core file */ 1277c478bd9Sstevel@tonic-gate prcred_t *core_cred; /* process credential from core file */ 1287c478bd9Sstevel@tonic-gate core_content_t core_content; /* content dumped to core file */ 1297c478bd9Sstevel@tonic-gate prpriv_t *core_priv; /* process privileges from core file */ 1307c478bd9Sstevel@tonic-gate size_t core_priv_size; /* size of the privileges */ 1317c478bd9Sstevel@tonic-gate void *core_privinfo; /* system privileges info from core file */ 1327c478bd9Sstevel@tonic-gate priv_impl_info_t *core_ppii; /* NOTE entry for core_privinfo */ 1337c478bd9Sstevel@tonic-gate char *core_zonename; /* zone name from core file */ 1347c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 1357c478bd9Sstevel@tonic-gate struct ssd *core_ldt; /* LDT entries from core file */ 1367c478bd9Sstevel@tonic-gate uint_t core_nldt; /* number of LDT entries in core file */ 1377c478bd9Sstevel@tonic-gate #endif 1387c478bd9Sstevel@tonic-gate } core_info_t; 1397c478bd9Sstevel@tonic-gate 14030da1432Sahl typedef struct elf_file_header { /* extended ELF header */ 14130da1432Sahl unsigned char e_ident[EI_NIDENT]; 14230da1432Sahl Elf64_Half e_type; 14330da1432Sahl Elf64_Half e_machine; 14430da1432Sahl Elf64_Word e_version; 14530da1432Sahl Elf64_Addr e_entry; 14630da1432Sahl Elf64_Off e_phoff; 14730da1432Sahl Elf64_Off e_shoff; 14830da1432Sahl Elf64_Word e_flags; 14930da1432Sahl Elf64_Half e_ehsize; 15030da1432Sahl Elf64_Half e_phentsize; 15130da1432Sahl Elf64_Half e_shentsize; 15230da1432Sahl Elf64_Word e_phnum; /* phdr count extended to 32 bits */ 15330da1432Sahl Elf64_Word e_shnum; /* shdr count extended to 32 bits */ 15430da1432Sahl Elf64_Word e_shstrndx; /* shdr string index extended to 32 bits */ 15530da1432Sahl } elf_file_header_t; 15630da1432Sahl 1577c478bd9Sstevel@tonic-gate typedef struct elf_file { /* convenience for managing ELF files */ 15830da1432Sahl elf_file_header_t e_hdr; /* Extended ELF header */ 1597c478bd9Sstevel@tonic-gate Elf *e_elf; /* ELF library handle */ 1607c478bd9Sstevel@tonic-gate int e_fd; /* file descriptor */ 1617c478bd9Sstevel@tonic-gate } elf_file_t; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate typedef struct ps_rwops { /* ops vector for Pread() and Pwrite() */ 1647c478bd9Sstevel@tonic-gate ssize_t (*p_pread)(struct ps_prochandle *, 1657c478bd9Sstevel@tonic-gate void *, size_t, uintptr_t); 1667c478bd9Sstevel@tonic-gate ssize_t (*p_pwrite)(struct ps_prochandle *, 1677c478bd9Sstevel@tonic-gate const void *, size_t, uintptr_t); 1687c478bd9Sstevel@tonic-gate } ps_rwops_t; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate #define HASHSIZE 1024 /* hash table size, power of 2 */ 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate struct ps_prochandle { 1737c478bd9Sstevel@tonic-gate struct ps_lwphandle **hashtab; /* hash table for LWPs (Lgrab()) */ 1747c478bd9Sstevel@tonic-gate mutex_t proc_lock; /* protects hash table; serializes Lgrab() */ 1757c478bd9Sstevel@tonic-gate pstatus_t orig_status; /* remembered status on Pgrab() */ 1767c478bd9Sstevel@tonic-gate pstatus_t status; /* status when stopped */ 1777c478bd9Sstevel@tonic-gate psinfo_t psinfo; /* psinfo_t from last Ppsinfo() request */ 1787c478bd9Sstevel@tonic-gate uintptr_t sysaddr; /* address of most recent syscall instruction */ 1797c478bd9Sstevel@tonic-gate pid_t pid; /* process-ID */ 1807c478bd9Sstevel@tonic-gate int state; /* state of the process, see "libproc.h" */ 1817c478bd9Sstevel@tonic-gate uint_t flags; /* see defines below */ 1827c478bd9Sstevel@tonic-gate uint_t agentcnt; /* Pcreate_agent()/Pdestroy_agent() ref count */ 1837c478bd9Sstevel@tonic-gate int asfd; /* /proc/<pid>/as filedescriptor */ 1847c478bd9Sstevel@tonic-gate int ctlfd; /* /proc/<pid>/ctl filedescriptor */ 1857c478bd9Sstevel@tonic-gate int statfd; /* /proc/<pid>/status filedescriptor */ 1867c478bd9Sstevel@tonic-gate int agentctlfd; /* /proc/<pid>/lwp/agent/ctl */ 1877c478bd9Sstevel@tonic-gate int agentstatfd; /* /proc/<pid>/lwp/agent/status */ 1887c478bd9Sstevel@tonic-gate int info_valid; /* if zero, map and file info need updating */ 1897c478bd9Sstevel@tonic-gate map_info_t *mappings; /* cached process mappings */ 1907c478bd9Sstevel@tonic-gate size_t map_count; /* number of mappings */ 1917c478bd9Sstevel@tonic-gate size_t map_alloc; /* number of mappings allocated */ 1927c478bd9Sstevel@tonic-gate uint_t num_files; /* number of file elements in file_info */ 193fa9e4066Sahrens plist_t file_head; /* head of mapped files w/ symbol table info */ 1947c478bd9Sstevel@tonic-gate char *execname; /* name of the executable file */ 1957c478bd9Sstevel@tonic-gate auxv_t *auxv; /* the process's aux vector */ 1967c478bd9Sstevel@tonic-gate int nauxv; /* number of aux vector entries */ 1977c478bd9Sstevel@tonic-gate rd_agent_t *rap; /* cookie for rtld_db */ 1987c478bd9Sstevel@tonic-gate map_info_t *map_exec; /* the mapping for the executable file */ 1997c478bd9Sstevel@tonic-gate map_info_t *map_ldso; /* the mapping for ld.so.1 */ 2007c478bd9Sstevel@tonic-gate const ps_rwops_t *ops; /* pointer to ops-vector for read and write */ 2017c478bd9Sstevel@tonic-gate core_info_t *core; /* information specific to core (if PS_DEAD) */ 2027c478bd9Sstevel@tonic-gate uintptr_t *ucaddrs; /* ucontext-list addresses */ 2037c478bd9Sstevel@tonic-gate uint_t ucnelems; /* number of elements in the ucaddrs list */ 2047c478bd9Sstevel@tonic-gate }; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* flags */ 2077c478bd9Sstevel@tonic-gate #define CREATED 0x01 /* process was created by Pcreate() */ 2087c478bd9Sstevel@tonic-gate #define SETSIG 0x02 /* set signal trace mask before continuing */ 2097c478bd9Sstevel@tonic-gate #define SETFAULT 0x04 /* set fault trace mask before continuing */ 2107c478bd9Sstevel@tonic-gate #define SETENTRY 0x08 /* set sysentry trace mask before continuing */ 2117c478bd9Sstevel@tonic-gate #define SETEXIT 0x10 /* set sysexit trace mask before continuing */ 2127c478bd9Sstevel@tonic-gate #define SETHOLD 0x20 /* set signal hold mask before continuing */ 2137c478bd9Sstevel@tonic-gate #define SETREGS 0x40 /* set registers before continuing */ 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate struct ps_lwphandle { 2167c478bd9Sstevel@tonic-gate struct ps_prochandle *lwp_proc; /* process to which this lwp belongs */ 2177c478bd9Sstevel@tonic-gate struct ps_lwphandle *lwp_hash; /* hash table linked list */ 2187c478bd9Sstevel@tonic-gate lwpstatus_t lwp_status; /* status when stopped */ 2197c478bd9Sstevel@tonic-gate lwpsinfo_t lwp_psinfo; /* lwpsinfo_t from last Lpsinfo() */ 2207c478bd9Sstevel@tonic-gate lwpid_t lwp_id; /* lwp identifier */ 2217c478bd9Sstevel@tonic-gate int lwp_state; /* state of the lwp, see "libproc.h" */ 2227c478bd9Sstevel@tonic-gate uint_t lwp_flags; /* SETHOLD and/or SETREGS */ 2237c478bd9Sstevel@tonic-gate int lwp_ctlfd; /* /proc/<pid>/lwp/<lwpid>/lwpctl */ 2247c478bd9Sstevel@tonic-gate int lwp_statfd; /* /proc/<pid>/lwp/<lwpid>/lwpstatus */ 2257c478bd9Sstevel@tonic-gate }; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate /* 2287c478bd9Sstevel@tonic-gate * Implementation functions in the process control library. 2297c478bd9Sstevel@tonic-gate * These are not exported to clients of the library. 2307c478bd9Sstevel@tonic-gate */ 2317c478bd9Sstevel@tonic-gate extern void prldump(const char *, lwpstatus_t *); 2327c478bd9Sstevel@tonic-gate extern int dupfd(int, int); 2337c478bd9Sstevel@tonic-gate extern int set_minfd(void); 2347c478bd9Sstevel@tonic-gate extern int Pscantext(struct ps_prochandle *); 2357c478bd9Sstevel@tonic-gate extern void Pinitsym(struct ps_prochandle *); 2367c478bd9Sstevel@tonic-gate extern void Preadauxvec(struct ps_prochandle *); 2377c478bd9Sstevel@tonic-gate extern void optimize_symtab(sym_tbl_t *); 2387c478bd9Sstevel@tonic-gate extern void Pbuild_file_symtab(struct ps_prochandle *, file_info_t *); 2397c478bd9Sstevel@tonic-gate extern ctf_file_t *Pbuild_file_ctf(struct ps_prochandle *, file_info_t *); 2407c478bd9Sstevel@tonic-gate extern map_info_t *Paddr2mptr(struct ps_prochandle *, uintptr_t); 2417c478bd9Sstevel@tonic-gate extern char *Pfindexec(struct ps_prochandle *, const char *, 2427c478bd9Sstevel@tonic-gate int (*)(const char *, void *), void *); 2437c478bd9Sstevel@tonic-gate extern int getlwpstatus(struct ps_prochandle *, lwpid_t, lwpstatus_t *); 2447c478bd9Sstevel@tonic-gate int Pstopstatus(struct ps_prochandle *, long, uint32_t); 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate extern int Padd_mapping(struct ps_prochandle *, off64_t, file_info_t *, 2477c478bd9Sstevel@tonic-gate prmap_t *); 2487c478bd9Sstevel@tonic-gate extern void Psort_mappings(struct ps_prochandle *); 2497c478bd9Sstevel@tonic-gate 250fa9e4066Sahrens 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * Architecture-dependent definition of the breakpoint instruction. 2537c478bd9Sstevel@tonic-gate */ 2547c478bd9Sstevel@tonic-gate #if defined(sparc) || defined(__sparc) 2557c478bd9Sstevel@tonic-gate #define BPT ((instr_t)0x91d02001) 2567c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64) 2577c478bd9Sstevel@tonic-gate #define BPT ((instr_t)0xcc) 2587c478bd9Sstevel@tonic-gate #endif 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate /* 2617c478bd9Sstevel@tonic-gate * Simple convenience. 2627c478bd9Sstevel@tonic-gate */ 2637c478bd9Sstevel@tonic-gate #define TRUE 1 2647c478bd9Sstevel@tonic-gate #define FALSE 0 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate #endif 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate #endif /* _PCONTROL_H */ 271