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 5*3347Sab196087 * Common Development and Distribution License (the "License"). 6*3347Sab196087 * 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*3347Sab196087 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/stack.h> 290Sstevel@tonic-gate #include <sys/regset.h> 300Sstevel@tonic-gate #include <sys/frame.h> 310Sstevel@tonic-gate #include <sys/sysmacros.h> 320Sstevel@tonic-gate #include <sys/trap.h> 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include <stdlib.h> 350Sstevel@tonic-gate #include <unistd.h> 360Sstevel@tonic-gate #include <sys/types.h> 370Sstevel@tonic-gate #include <errno.h> 380Sstevel@tonic-gate #include <string.h> 390Sstevel@tonic-gate 400Sstevel@tonic-gate #include "Pcontrol.h" 410Sstevel@tonic-gate #include "Pstack.h" 420Sstevel@tonic-gate 430Sstevel@tonic-gate #define M_PLT_NRSV 1 /* reserved PLT entries */ 440Sstevel@tonic-gate #define M_PLT_ENTSIZE 16 /* size of each PLT entry */ 450Sstevel@tonic-gate 460Sstevel@tonic-gate static uchar_t int_syscall_instr[] = { 0xCD, T_SYSCALLINT }; 470Sstevel@tonic-gate 480Sstevel@tonic-gate const char * 490Sstevel@tonic-gate Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr) 500Sstevel@tonic-gate { 510Sstevel@tonic-gate map_info_t *mp = Paddr2mptr(P, pltaddr); 520Sstevel@tonic-gate 530Sstevel@tonic-gate uintptr_t r_addr; 540Sstevel@tonic-gate file_info_t *fp; 550Sstevel@tonic-gate Elf32_Rel r; 560Sstevel@tonic-gate size_t i; 570Sstevel@tonic-gate 580Sstevel@tonic-gate if (mp == NULL || (fp = mp->map_file) == NULL || 590Sstevel@tonic-gate fp->file_plt_base == 0 || 600Sstevel@tonic-gate pltaddr - fp->file_plt_base >= fp->file_plt_size) { 610Sstevel@tonic-gate errno = EINVAL; 620Sstevel@tonic-gate return (NULL); 630Sstevel@tonic-gate } 640Sstevel@tonic-gate 650Sstevel@tonic-gate i = (pltaddr - fp->file_plt_base) / M_PLT_ENTSIZE - M_PLT_NRSV; 660Sstevel@tonic-gate 670Sstevel@tonic-gate r_addr = fp->file_jmp_rel + i * sizeof (r); 680Sstevel@tonic-gate 690Sstevel@tonic-gate if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) && 700Sstevel@tonic-gate (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) { 710Sstevel@tonic-gate 72*3347Sab196087 Elf_Data *data = fp->file_dynsym.sym_data_pri; 730Sstevel@tonic-gate Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]); 740Sstevel@tonic-gate 750Sstevel@tonic-gate return (fp->file_dynsym.sym_strs + symp->st_name); 760Sstevel@tonic-gate } 770Sstevel@tonic-gate 780Sstevel@tonic-gate return (NULL); 790Sstevel@tonic-gate } 800Sstevel@tonic-gate 810Sstevel@tonic-gate int 820Sstevel@tonic-gate Pissyscall(struct ps_prochandle *P, uintptr_t addr) 830Sstevel@tonic-gate { 840Sstevel@tonic-gate uchar_t instr[16]; 850Sstevel@tonic-gate 860Sstevel@tonic-gate if (Pread(P, instr, sizeof (int_syscall_instr), addr) != 870Sstevel@tonic-gate sizeof (int_syscall_instr)) 880Sstevel@tonic-gate return (0); 890Sstevel@tonic-gate 900Sstevel@tonic-gate if (memcmp(instr, int_syscall_instr, sizeof (int_syscall_instr)) == 0) 910Sstevel@tonic-gate return (1); 920Sstevel@tonic-gate 930Sstevel@tonic-gate return (0); 940Sstevel@tonic-gate } 950Sstevel@tonic-gate 960Sstevel@tonic-gate int 970Sstevel@tonic-gate Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst) 980Sstevel@tonic-gate { 990Sstevel@tonic-gate int ret; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate if (ret = Pissyscall(P, addr - sizeof (int_syscall_instr))) { 1020Sstevel@tonic-gate if (dst) 1030Sstevel@tonic-gate *dst = addr - sizeof (int_syscall_instr); 1040Sstevel@tonic-gate return (ret); 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate return (0); 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* ARGSUSED */ 1110Sstevel@tonic-gate int 1120Sstevel@tonic-gate Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen) 1130Sstevel@tonic-gate { 1140Sstevel@tonic-gate if (buflen < sizeof (int_syscall_instr)) 1150Sstevel@tonic-gate return (0); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate if (memcmp(buf, int_syscall_instr, sizeof (int_syscall_instr)) == 0) 1180Sstevel@tonic-gate return (1); 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate return (0); 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate #define TR_ARG_MAX 6 /* Max args to print, same as SPARC */ 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate /* 1260Sstevel@tonic-gate * Given a return address, determine the likely number of arguments 1270Sstevel@tonic-gate * that were pushed on the stack prior to its execution. We do this by 1280Sstevel@tonic-gate * expecting that a typical call sequence consists of pushing arguments on 1290Sstevel@tonic-gate * the stack, executing a call instruction, and then performing an add 1300Sstevel@tonic-gate * on %esp to restore it to the value prior to pushing the arguments for 1310Sstevel@tonic-gate * the call. We attempt to detect such an add, and divide the addend 1320Sstevel@tonic-gate * by the size of a word to determine the number of pushed arguments. 1330Sstevel@tonic-gate * 1340Sstevel@tonic-gate * If we do not find such an add, this does not necessarily imply that the 1350Sstevel@tonic-gate * function took no arguments. It is not possible to reliably detect such a 1360Sstevel@tonic-gate * void function because hand-coded assembler does not always perform an add 1370Sstevel@tonic-gate * to %esp immediately after the "call" instruction (eg. _sys_call()). 1380Sstevel@tonic-gate * Because of this, we default to returning MIN(sz, TR_ARG_MAX) instead of 0 1390Sstevel@tonic-gate * in the absence of an add to %esp. 1400Sstevel@tonic-gate */ 1410Sstevel@tonic-gate static ulong_t 1420Sstevel@tonic-gate argcount(struct ps_prochandle *P, long pc, ssize_t sz) 1430Sstevel@tonic-gate { 1440Sstevel@tonic-gate uchar_t instr[6]; 1450Sstevel@tonic-gate ulong_t count, max; 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate max = MIN(sz / sizeof (long), TR_ARG_MAX); 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate /* 1500Sstevel@tonic-gate * Read the instruction at the return location. 1510Sstevel@tonic-gate */ 1520Sstevel@tonic-gate if (Pread(P, instr, sizeof (instr), pc) != sizeof (instr) || 1530Sstevel@tonic-gate instr[1] != 0xc4) 1540Sstevel@tonic-gate return (max); 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate switch (instr[0]) { 1570Sstevel@tonic-gate case 0x81: /* count is a longword */ 1580Sstevel@tonic-gate count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24); 1590Sstevel@tonic-gate break; 1600Sstevel@tonic-gate case 0x83: /* count is a byte */ 1610Sstevel@tonic-gate count = instr[2]; 1620Sstevel@tonic-gate break; 1630Sstevel@tonic-gate default: 1640Sstevel@tonic-gate return (max); 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate count /= sizeof (long); 1680Sstevel@tonic-gate return (MIN(count, max)); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate static void 1720Sstevel@tonic-gate ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst) 1730Sstevel@tonic-gate { 1740Sstevel@tonic-gate (void) memcpy(dst, src->uc_mcontext.gregs, sizeof (gregset_t)); 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate int 1780Sstevel@tonic-gate Pstack_iter(struct ps_prochandle *P, const prgregset_t regs, 1790Sstevel@tonic-gate proc_stack_f *func, void *arg) 1800Sstevel@tonic-gate { 1810Sstevel@tonic-gate prgreg_t *prevfp = NULL; 1820Sstevel@tonic-gate uint_t pfpsize = 0; 1830Sstevel@tonic-gate int nfp = 0; 1840Sstevel@tonic-gate struct { 1850Sstevel@tonic-gate long fp; 1860Sstevel@tonic-gate long pc; 1870Sstevel@tonic-gate long args[32]; 1880Sstevel@tonic-gate } frame; 1890Sstevel@tonic-gate uint_t argc; 1900Sstevel@tonic-gate ssize_t sz; 1910Sstevel@tonic-gate prgregset_t gregs; 1920Sstevel@tonic-gate prgreg_t fp, pfp; 1930Sstevel@tonic-gate prgreg_t pc; 1940Sstevel@tonic-gate int rv; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* 1970Sstevel@tonic-gate * Type definition for a structure corresponding to an IA32 1980Sstevel@tonic-gate * signal frame. Refer to the comments in Pstack.c for more info 1990Sstevel@tonic-gate */ 2000Sstevel@tonic-gate typedef struct { 2010Sstevel@tonic-gate long fp; 2020Sstevel@tonic-gate long pc; 2030Sstevel@tonic-gate int signo; 2040Sstevel@tonic-gate ucontext_t *ucp; 2050Sstevel@tonic-gate siginfo_t *sip; 2060Sstevel@tonic-gate } sf_t; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate uclist_t ucl; 2090Sstevel@tonic-gate ucontext_t uc; 2100Sstevel@tonic-gate uintptr_t uc_addr; 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate init_uclist(&ucl, P); 2130Sstevel@tonic-gate (void) memcpy(gregs, regs, sizeof (gregs)); 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate fp = regs[R_FP]; 2160Sstevel@tonic-gate pc = regs[R_PC]; 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate while (fp != 0 || pc != 0) { 2190Sstevel@tonic-gate if (stack_loop(fp, &prevfp, &nfp, &pfpsize)) 2200Sstevel@tonic-gate break; 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate if (fp != 0 && 2230Sstevel@tonic-gate (sz = Pread(P, &frame, sizeof (frame), (uintptr_t)fp) 2240Sstevel@tonic-gate >= (ssize_t)(2* sizeof (long)))) { 2250Sstevel@tonic-gate /* 2260Sstevel@tonic-gate * One more trick for signal frames: the kernel sets 2270Sstevel@tonic-gate * the return pc of the signal frame to 0xffffffff on 2280Sstevel@tonic-gate * Intel IA32, so argcount won't work. 2290Sstevel@tonic-gate */ 2300Sstevel@tonic-gate if (frame.pc != -1L) { 2310Sstevel@tonic-gate sz -= 2* sizeof (long); 2320Sstevel@tonic-gate argc = argcount(P, (long)frame.pc, sz); 2330Sstevel@tonic-gate } else 2340Sstevel@tonic-gate argc = 3; /* sighandler(signo, sip, ucp) */ 2350Sstevel@tonic-gate } else { 2360Sstevel@tonic-gate (void) memset(&frame, 0, sizeof (frame)); 2370Sstevel@tonic-gate argc = 0; 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate gregs[R_FP] = fp; 2410Sstevel@tonic-gate gregs[R_PC] = pc; 2420Sstevel@tonic-gate 2430Sstevel@tonic-gate if ((rv = func(arg, gregs, argc, frame.args)) != 0) 2440Sstevel@tonic-gate break; 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate /* 2470Sstevel@tonic-gate * In order to allow iteration over java frames (which can have 2480Sstevel@tonic-gate * their own frame pointers), we allow the iterator to change 2490Sstevel@tonic-gate * the contents of gregs. If we detect a change, then we assume 2500Sstevel@tonic-gate * that the new values point to the next frame. 2510Sstevel@tonic-gate */ 2520Sstevel@tonic-gate if (gregs[R_FP] != fp || gregs[R_PC] != pc) { 2530Sstevel@tonic-gate fp = gregs[R_FP]; 2540Sstevel@tonic-gate pc = gregs[R_PC]; 2550Sstevel@tonic-gate continue; 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate pfp = fp; 2590Sstevel@tonic-gate fp = frame.fp; 2600Sstevel@tonic-gate pc = frame.pc; 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate if (find_uclink(&ucl, pfp + sizeof (sf_t))) 2630Sstevel@tonic-gate uc_addr = pfp + sizeof (sf_t); 2640Sstevel@tonic-gate else 2650Sstevel@tonic-gate uc_addr = NULL; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate if (uc_addr != NULL && 2680Sstevel@tonic-gate Pread(P, &uc, sizeof (uc), uc_addr) == sizeof (uc)) { 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate ucontext_n_to_prgregs(&uc, gregs); 2710Sstevel@tonic-gate fp = gregs[R_FP]; 2720Sstevel@tonic-gate pc = gregs[R_PC]; 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate if (prevfp) 2770Sstevel@tonic-gate free(prevfp); 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate free_uclist(&ucl); 2800Sstevel@tonic-gate return (rv); 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate uintptr_t 2840Sstevel@tonic-gate Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp) 2850Sstevel@tonic-gate { 2860Sstevel@tonic-gate sp -= sizeof (int) * (nargs+2); /* space for arg list + CALL parms */ 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate P->status.pr_lwp.pr_reg[EAX] = sysindex; 2890Sstevel@tonic-gate P->status.pr_lwp.pr_reg[R_SP] = sp; 2900Sstevel@tonic-gate P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr; 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate return (sp); 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate int 2960Sstevel@tonic-gate Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp, 2970Sstevel@tonic-gate uintptr_t ap) 2980Sstevel@tonic-gate { 2990Sstevel@tonic-gate int32_t arglist[MAXARGS+2]; 3000Sstevel@tonic-gate int i; 3010Sstevel@tonic-gate argdes_t *adp; 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate for (i = 0, adp = argp; i < nargs; i++, adp++) 3040Sstevel@tonic-gate arglist[1 + i] = (int32_t)adp->arg_value; 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate arglist[0] = P->status.pr_lwp.pr_reg[R_PC]; 3070Sstevel@tonic-gate if (Pwrite(P, &arglist[0], sizeof (int) * (nargs+1), 3080Sstevel@tonic-gate (uintptr_t)ap) != sizeof (int) * (nargs+1)) 3090Sstevel@tonic-gate return (-1); 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate return (0); 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate int 3150Sstevel@tonic-gate Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp, 3160Sstevel@tonic-gate uintptr_t ap) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate uint32_t arglist[MAXARGS + 2]; 3190Sstevel@tonic-gate int i; 3200Sstevel@tonic-gate argdes_t *adp; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate if (Pread(P, &arglist[0], sizeof (int) * (nargs+1), (uintptr_t)ap) 3230Sstevel@tonic-gate != sizeof (int) * (nargs+1)) 3240Sstevel@tonic-gate return (-1); 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate for (i = 0, adp = argp; i < nargs; i++, adp++) 3270Sstevel@tonic-gate adp->arg_value = arglist[i]; 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate return (0); 3300Sstevel@tonic-gate } 331