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 567c478bd9Sstevel@tonic-gate typedef struct { /* 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 */ 637c478bd9Sstevel@tonic-gate Elf *sym_elf; /* faked-up elf handle from core file */ 647c478bd9Sstevel@tonic-gate 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 */ 71*fa9e4066Sahrens 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 */ 827c478bd9Sstevel@tonic-gate Elf *file_elf; /* elf handle so we can close */ 837c478bd9Sstevel@tonic-gate 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 */ 957c478bd9Sstevel@tonic-gate } file_info_t; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate typedef struct map_info { /* description of an address space mapping */ 987c478bd9Sstevel@tonic-gate prmap_t map_pmap; /* /proc description of this mapping */ 997c478bd9Sstevel@tonic-gate file_info_t *map_file; /* pointer into list of mapped files */ 1007c478bd9Sstevel@tonic-gate off64_t map_offset; /* offset into core file (if core) */ 1017c478bd9Sstevel@tonic-gate int map_relocate; /* associated file_map needs to be relocated */ 1027c478bd9Sstevel@tonic-gate } map_info_t; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate typedef struct lwp_info { /* per-lwp information from core file */ 105*fa9e4066Sahrens plist_t lwp_list; /* linked list */ 1067c478bd9Sstevel@tonic-gate lwpid_t lwp_id; /* lwp identifier */ 1077c478bd9Sstevel@tonic-gate lwpsinfo_t lwp_psinfo; /* /proc/<pid>/lwp/<lwpid>/lwpsinfo data */ 1087c478bd9Sstevel@tonic-gate lwpstatus_t lwp_status; /* /proc/<pid>/lwp/<lwpid>/lwpstatus data */ 1097c478bd9Sstevel@tonic-gate #if defined(sparc) || defined(__sparc) 1107c478bd9Sstevel@tonic-gate gwindows_t *lwp_gwins; /* /proc/<pid>/lwp/<lwpid>/gwindows data */ 1117c478bd9Sstevel@tonic-gate prxregset_t *lwp_xregs; /* /proc/<pid>/lwp/<lwpid>/xregs data */ 1127c478bd9Sstevel@tonic-gate int64_t *lwp_asrs; /* /proc/<pid>/lwp/<lwpid>/asrs data */ 1137c478bd9Sstevel@tonic-gate #endif 1147c478bd9Sstevel@tonic-gate } lwp_info_t; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate typedef struct core_info { /* information specific to core files */ 1177c478bd9Sstevel@tonic-gate char core_dmodel; /* data model for core file */ 1187c478bd9Sstevel@tonic-gate int core_errno; /* error during initialization if != 0 */ 119*fa9e4066Sahrens plist_t core_lwp_head; /* head of list of lwp info */ 1207c478bd9Sstevel@tonic-gate lwp_info_t *core_lwp; /* current lwp information */ 1217c478bd9Sstevel@tonic-gate uint_t core_nlwp; /* number of lwp's in list */ 1227c478bd9Sstevel@tonic-gate off64_t core_size; /* size of core file in bytes */ 1237c478bd9Sstevel@tonic-gate char *core_platform; /* platform string from core file */ 1247c478bd9Sstevel@tonic-gate struct utsname *core_uts; /* uname(2) data from core file */ 1257c478bd9Sstevel@tonic-gate prcred_t *core_cred; /* process credential from core file */ 1267c478bd9Sstevel@tonic-gate core_content_t core_content; /* content dumped to core file */ 1277c478bd9Sstevel@tonic-gate prpriv_t *core_priv; /* process privileges from core file */ 1287c478bd9Sstevel@tonic-gate size_t core_priv_size; /* size of the privileges */ 1297c478bd9Sstevel@tonic-gate void *core_privinfo; /* system privileges info from core file */ 1307c478bd9Sstevel@tonic-gate priv_impl_info_t *core_ppii; /* NOTE entry for core_privinfo */ 1317c478bd9Sstevel@tonic-gate char *core_zonename; /* zone name from core file */ 1327c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 1337c478bd9Sstevel@tonic-gate struct ssd *core_ldt; /* LDT entries from core file */ 1347c478bd9Sstevel@tonic-gate uint_t core_nldt; /* number of LDT entries in core file */ 1357c478bd9Sstevel@tonic-gate #endif 1367c478bd9Sstevel@tonic-gate } core_info_t; 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate typedef struct elf_file { /* convenience for managing ELF files */ 1397c478bd9Sstevel@tonic-gate GElf_Ehdr e_hdr; /* ELF file header information */ 1407c478bd9Sstevel@tonic-gate Elf *e_elf; /* ELF library handle */ 1417c478bd9Sstevel@tonic-gate int e_fd; /* file descriptor */ 1427c478bd9Sstevel@tonic-gate } elf_file_t; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate typedef struct ps_rwops { /* ops vector for Pread() and Pwrite() */ 1457c478bd9Sstevel@tonic-gate ssize_t (*p_pread)(struct ps_prochandle *, 1467c478bd9Sstevel@tonic-gate void *, size_t, uintptr_t); 1477c478bd9Sstevel@tonic-gate ssize_t (*p_pwrite)(struct ps_prochandle *, 1487c478bd9Sstevel@tonic-gate const void *, size_t, uintptr_t); 1497c478bd9Sstevel@tonic-gate } ps_rwops_t; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate #define HASHSIZE 1024 /* hash table size, power of 2 */ 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate struct ps_prochandle { 1547c478bd9Sstevel@tonic-gate struct ps_lwphandle **hashtab; /* hash table for LWPs (Lgrab()) */ 1557c478bd9Sstevel@tonic-gate mutex_t proc_lock; /* protects hash table; serializes Lgrab() */ 1567c478bd9Sstevel@tonic-gate pstatus_t orig_status; /* remembered status on Pgrab() */ 1577c478bd9Sstevel@tonic-gate pstatus_t status; /* status when stopped */ 1587c478bd9Sstevel@tonic-gate psinfo_t psinfo; /* psinfo_t from last Ppsinfo() request */ 1597c478bd9Sstevel@tonic-gate uintptr_t sysaddr; /* address of most recent syscall instruction */ 1607c478bd9Sstevel@tonic-gate pid_t pid; /* process-ID */ 1617c478bd9Sstevel@tonic-gate int state; /* state of the process, see "libproc.h" */ 1627c478bd9Sstevel@tonic-gate uint_t flags; /* see defines below */ 1637c478bd9Sstevel@tonic-gate uint_t agentcnt; /* Pcreate_agent()/Pdestroy_agent() ref count */ 1647c478bd9Sstevel@tonic-gate int asfd; /* /proc/<pid>/as filedescriptor */ 1657c478bd9Sstevel@tonic-gate int ctlfd; /* /proc/<pid>/ctl filedescriptor */ 1667c478bd9Sstevel@tonic-gate int statfd; /* /proc/<pid>/status filedescriptor */ 1677c478bd9Sstevel@tonic-gate int agentctlfd; /* /proc/<pid>/lwp/agent/ctl */ 1687c478bd9Sstevel@tonic-gate int agentstatfd; /* /proc/<pid>/lwp/agent/status */ 1697c478bd9Sstevel@tonic-gate int info_valid; /* if zero, map and file info need updating */ 1707c478bd9Sstevel@tonic-gate map_info_t *mappings; /* cached process mappings */ 1717c478bd9Sstevel@tonic-gate size_t map_count; /* number of mappings */ 1727c478bd9Sstevel@tonic-gate size_t map_alloc; /* number of mappings allocated */ 1737c478bd9Sstevel@tonic-gate uint_t num_files; /* number of file elements in file_info */ 174*fa9e4066Sahrens plist_t file_head; /* head of mapped files w/ symbol table info */ 1757c478bd9Sstevel@tonic-gate char *execname; /* name of the executable file */ 1767c478bd9Sstevel@tonic-gate auxv_t *auxv; /* the process's aux vector */ 1777c478bd9Sstevel@tonic-gate int nauxv; /* number of aux vector entries */ 1787c478bd9Sstevel@tonic-gate rd_agent_t *rap; /* cookie for rtld_db */ 1797c478bd9Sstevel@tonic-gate map_info_t *map_exec; /* the mapping for the executable file */ 1807c478bd9Sstevel@tonic-gate map_info_t *map_ldso; /* the mapping for ld.so.1 */ 1817c478bd9Sstevel@tonic-gate const ps_rwops_t *ops; /* pointer to ops-vector for read and write */ 1827c478bd9Sstevel@tonic-gate core_info_t *core; /* information specific to core (if PS_DEAD) */ 1837c478bd9Sstevel@tonic-gate uintptr_t *ucaddrs; /* ucontext-list addresses */ 1847c478bd9Sstevel@tonic-gate uint_t ucnelems; /* number of elements in the ucaddrs list */ 1857c478bd9Sstevel@tonic-gate }; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate /* flags */ 1887c478bd9Sstevel@tonic-gate #define CREATED 0x01 /* process was created by Pcreate() */ 1897c478bd9Sstevel@tonic-gate #define SETSIG 0x02 /* set signal trace mask before continuing */ 1907c478bd9Sstevel@tonic-gate #define SETFAULT 0x04 /* set fault trace mask before continuing */ 1917c478bd9Sstevel@tonic-gate #define SETENTRY 0x08 /* set sysentry trace mask before continuing */ 1927c478bd9Sstevel@tonic-gate #define SETEXIT 0x10 /* set sysexit trace mask before continuing */ 1937c478bd9Sstevel@tonic-gate #define SETHOLD 0x20 /* set signal hold mask before continuing */ 1947c478bd9Sstevel@tonic-gate #define SETREGS 0x40 /* set registers before continuing */ 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate struct ps_lwphandle { 1977c478bd9Sstevel@tonic-gate struct ps_prochandle *lwp_proc; /* process to which this lwp belongs */ 1987c478bd9Sstevel@tonic-gate struct ps_lwphandle *lwp_hash; /* hash table linked list */ 1997c478bd9Sstevel@tonic-gate lwpstatus_t lwp_status; /* status when stopped */ 2007c478bd9Sstevel@tonic-gate lwpsinfo_t lwp_psinfo; /* lwpsinfo_t from last Lpsinfo() */ 2017c478bd9Sstevel@tonic-gate lwpid_t lwp_id; /* lwp identifier */ 2027c478bd9Sstevel@tonic-gate int lwp_state; /* state of the lwp, see "libproc.h" */ 2037c478bd9Sstevel@tonic-gate uint_t lwp_flags; /* SETHOLD and/or SETREGS */ 2047c478bd9Sstevel@tonic-gate int lwp_ctlfd; /* /proc/<pid>/lwp/<lwpid>/lwpctl */ 2057c478bd9Sstevel@tonic-gate int lwp_statfd; /* /proc/<pid>/lwp/<lwpid>/lwpstatus */ 2067c478bd9Sstevel@tonic-gate }; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate /* 2097c478bd9Sstevel@tonic-gate * Implementation functions in the process control library. 2107c478bd9Sstevel@tonic-gate * These are not exported to clients of the library. 2117c478bd9Sstevel@tonic-gate */ 2127c478bd9Sstevel@tonic-gate extern void prldump(const char *, lwpstatus_t *); 2137c478bd9Sstevel@tonic-gate extern int dupfd(int, int); 2147c478bd9Sstevel@tonic-gate extern int set_minfd(void); 2157c478bd9Sstevel@tonic-gate extern int Pscantext(struct ps_prochandle *); 2167c478bd9Sstevel@tonic-gate extern void Pinitsym(struct ps_prochandle *); 2177c478bd9Sstevel@tonic-gate extern void Preadauxvec(struct ps_prochandle *); 2187c478bd9Sstevel@tonic-gate extern void optimize_symtab(sym_tbl_t *); 2197c478bd9Sstevel@tonic-gate extern void Pbuild_file_symtab(struct ps_prochandle *, file_info_t *); 2207c478bd9Sstevel@tonic-gate extern ctf_file_t *Pbuild_file_ctf(struct ps_prochandle *, file_info_t *); 2217c478bd9Sstevel@tonic-gate extern map_info_t *Paddr2mptr(struct ps_prochandle *, uintptr_t); 2227c478bd9Sstevel@tonic-gate extern char *Pfindexec(struct ps_prochandle *, const char *, 2237c478bd9Sstevel@tonic-gate int (*)(const char *, void *), void *); 2247c478bd9Sstevel@tonic-gate extern int getlwpstatus(struct ps_prochandle *, lwpid_t, lwpstatus_t *); 2257c478bd9Sstevel@tonic-gate int Pstopstatus(struct ps_prochandle *, long, uint32_t); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate extern int Padd_mapping(struct ps_prochandle *, off64_t, file_info_t *, 2287c478bd9Sstevel@tonic-gate prmap_t *); 2297c478bd9Sstevel@tonic-gate extern void Psort_mappings(struct ps_prochandle *); 2307c478bd9Sstevel@tonic-gate 231*fa9e4066Sahrens 2327c478bd9Sstevel@tonic-gate /* 2337c478bd9Sstevel@tonic-gate * Architecture-dependent definition of the breakpoint instruction. 2347c478bd9Sstevel@tonic-gate */ 2357c478bd9Sstevel@tonic-gate #if defined(sparc) || defined(__sparc) 2367c478bd9Sstevel@tonic-gate #define BPT ((instr_t)0x91d02001) 2377c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64) 2387c478bd9Sstevel@tonic-gate #define BPT ((instr_t)0xcc) 2397c478bd9Sstevel@tonic-gate #endif 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * Simple convenience. 2437c478bd9Sstevel@tonic-gate */ 2447c478bd9Sstevel@tonic-gate #define TRUE 1 2457c478bd9Sstevel@tonic-gate #define FALSE 0 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate #endif 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate #endif /* _PCONTROL_H */ 252