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