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 5d51e9074Sab196087 * Common Development and Distribution License (the "License"). 6d51e9074Sab196087 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*d9452f23SEdward Pilatowicz * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/stack.h> 277c478bd9Sstevel@tonic-gate #include <sys/regset.h> 287c478bd9Sstevel@tonic-gate #include <sys/frame.h> 297c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 307c478bd9Sstevel@tonic-gate #include <sys/trap.h> 31*d9452f23SEdward Pilatowicz #include <sys/machelf.h> 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <unistd.h> 357c478bd9Sstevel@tonic-gate #include <sys/types.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <string.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #include "Pcontrol.h" 407c478bd9Sstevel@tonic-gate #include "Pstack.h" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate static uchar_t int_syscall_instr[] = { 0xCD, T_SYSCALLINT }; 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate const char * 457c478bd9Sstevel@tonic-gate Ppltdest(struct ps_prochandle *P, uintptr_t pltaddr) 467c478bd9Sstevel@tonic-gate { 477c478bd9Sstevel@tonic-gate map_info_t *mp = Paddr2mptr(P, pltaddr); 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate uintptr_t r_addr; 507c478bd9Sstevel@tonic-gate file_info_t *fp; 517c478bd9Sstevel@tonic-gate Elf32_Rel r; 527c478bd9Sstevel@tonic-gate size_t i; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate if (mp == NULL || (fp = mp->map_file) == NULL || 557c478bd9Sstevel@tonic-gate fp->file_plt_base == 0 || 567c478bd9Sstevel@tonic-gate pltaddr - fp->file_plt_base >= fp->file_plt_size) { 577c478bd9Sstevel@tonic-gate errno = EINVAL; 587c478bd9Sstevel@tonic-gate return (NULL); 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate 61*d9452f23SEdward Pilatowicz i = (pltaddr - fp->file_plt_base) / M_PLT_ENTSIZE - M_PLT_XNumber; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate r_addr = fp->file_jmp_rel + i * sizeof (r); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate if (Pread(P, &r, sizeof (r), r_addr) == sizeof (r) && 667c478bd9Sstevel@tonic-gate (i = ELF32_R_SYM(r.r_info)) < fp->file_dynsym.sym_symn) { 677c478bd9Sstevel@tonic-gate 68d51e9074Sab196087 Elf_Data *data = fp->file_dynsym.sym_data_pri; 697c478bd9Sstevel@tonic-gate Elf32_Sym *symp = &(((Elf32_Sym *)data->d_buf)[i]); 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate return (fp->file_dynsym.sym_strs + symp->st_name); 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate return (NULL); 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate int 787c478bd9Sstevel@tonic-gate Pissyscall(struct ps_prochandle *P, uintptr_t addr) 797c478bd9Sstevel@tonic-gate { 807c478bd9Sstevel@tonic-gate uchar_t instr[16]; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if (Pread(P, instr, sizeof (int_syscall_instr), addr) != 837c478bd9Sstevel@tonic-gate sizeof (int_syscall_instr)) 847c478bd9Sstevel@tonic-gate return (0); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate if (memcmp(instr, int_syscall_instr, sizeof (int_syscall_instr)) == 0) 877c478bd9Sstevel@tonic-gate return (1); 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate return (0); 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate int 937c478bd9Sstevel@tonic-gate Pissyscall_prev(struct ps_prochandle *P, uintptr_t addr, uintptr_t *dst) 947c478bd9Sstevel@tonic-gate { 957c478bd9Sstevel@tonic-gate int ret; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate if (ret = Pissyscall(P, addr - sizeof (int_syscall_instr))) { 987c478bd9Sstevel@tonic-gate if (dst) 997c478bd9Sstevel@tonic-gate *dst = addr - sizeof (int_syscall_instr); 1007c478bd9Sstevel@tonic-gate return (ret); 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate return (0); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1077c478bd9Sstevel@tonic-gate int 1087c478bd9Sstevel@tonic-gate Pissyscall_text(struct ps_prochandle *P, const void *buf, size_t buflen) 1097c478bd9Sstevel@tonic-gate { 1107c478bd9Sstevel@tonic-gate if (buflen < sizeof (int_syscall_instr)) 1117c478bd9Sstevel@tonic-gate return (0); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (memcmp(buf, int_syscall_instr, sizeof (int_syscall_instr)) == 0) 1147c478bd9Sstevel@tonic-gate return (1); 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate return (0); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate #define TR_ARG_MAX 6 /* Max args to print, same as SPARC */ 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* 1227c478bd9Sstevel@tonic-gate * Given a return address, determine the likely number of arguments 1237c478bd9Sstevel@tonic-gate * that were pushed on the stack prior to its execution. We do this by 1247c478bd9Sstevel@tonic-gate * expecting that a typical call sequence consists of pushing arguments on 1257c478bd9Sstevel@tonic-gate * the stack, executing a call instruction, and then performing an add 1267c478bd9Sstevel@tonic-gate * on %esp to restore it to the value prior to pushing the arguments for 1277c478bd9Sstevel@tonic-gate * the call. We attempt to detect such an add, and divide the addend 1287c478bd9Sstevel@tonic-gate * by the size of a word to determine the number of pushed arguments. 1297c478bd9Sstevel@tonic-gate * 1307c478bd9Sstevel@tonic-gate * If we do not find such an add, this does not necessarily imply that the 1317c478bd9Sstevel@tonic-gate * function took no arguments. It is not possible to reliably detect such a 1327c478bd9Sstevel@tonic-gate * void function because hand-coded assembler does not always perform an add 1337c478bd9Sstevel@tonic-gate * to %esp immediately after the "call" instruction (eg. _sys_call()). 1347c478bd9Sstevel@tonic-gate * Because of this, we default to returning MIN(sz, TR_ARG_MAX) instead of 0 1357c478bd9Sstevel@tonic-gate * in the absence of an add to %esp. 1367c478bd9Sstevel@tonic-gate */ 1377c478bd9Sstevel@tonic-gate static ulong_t 1387c478bd9Sstevel@tonic-gate argcount(struct ps_prochandle *P, long pc, ssize_t sz) 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate uchar_t instr[6]; 1417c478bd9Sstevel@tonic-gate ulong_t count, max; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate max = MIN(sz / sizeof (long), TR_ARG_MAX); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* 1467c478bd9Sstevel@tonic-gate * Read the instruction at the return location. 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate if (Pread(P, instr, sizeof (instr), pc) != sizeof (instr) || 1497c478bd9Sstevel@tonic-gate instr[1] != 0xc4) 1507c478bd9Sstevel@tonic-gate return (max); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate switch (instr[0]) { 1537c478bd9Sstevel@tonic-gate case 0x81: /* count is a longword */ 1547c478bd9Sstevel@tonic-gate count = instr[2]+(instr[3]<<8)+(instr[4]<<16)+(instr[5]<<24); 1557c478bd9Sstevel@tonic-gate break; 1567c478bd9Sstevel@tonic-gate case 0x83: /* count is a byte */ 1577c478bd9Sstevel@tonic-gate count = instr[2]; 1587c478bd9Sstevel@tonic-gate break; 1597c478bd9Sstevel@tonic-gate default: 1607c478bd9Sstevel@tonic-gate return (max); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate count /= sizeof (long); 1647c478bd9Sstevel@tonic-gate return (MIN(count, max)); 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate static void 1687c478bd9Sstevel@tonic-gate ucontext_n_to_prgregs(const ucontext_t *src, prgregset_t dst) 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate (void) memcpy(dst, src->uc_mcontext.gregs, sizeof (gregset_t)); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate int 1747c478bd9Sstevel@tonic-gate Pstack_iter(struct ps_prochandle *P, const prgregset_t regs, 1757c478bd9Sstevel@tonic-gate proc_stack_f *func, void *arg) 1767c478bd9Sstevel@tonic-gate { 1777c478bd9Sstevel@tonic-gate prgreg_t *prevfp = NULL; 1787c478bd9Sstevel@tonic-gate uint_t pfpsize = 0; 1797c478bd9Sstevel@tonic-gate int nfp = 0; 1807c478bd9Sstevel@tonic-gate struct { 1817c478bd9Sstevel@tonic-gate long fp; 1827c478bd9Sstevel@tonic-gate long pc; 1837c478bd9Sstevel@tonic-gate long args[32]; 1847c478bd9Sstevel@tonic-gate } frame; 1857c478bd9Sstevel@tonic-gate uint_t argc; 1867c478bd9Sstevel@tonic-gate ssize_t sz; 1877c478bd9Sstevel@tonic-gate prgregset_t gregs; 1887c478bd9Sstevel@tonic-gate prgreg_t fp, pfp; 1897c478bd9Sstevel@tonic-gate prgreg_t pc; 1907c478bd9Sstevel@tonic-gate int rv; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 1937c478bd9Sstevel@tonic-gate * Type definition for a structure corresponding to an IA32 1947c478bd9Sstevel@tonic-gate * signal frame. Refer to the comments in Pstack.c for more info 1957c478bd9Sstevel@tonic-gate */ 1967c478bd9Sstevel@tonic-gate typedef struct { 1977c478bd9Sstevel@tonic-gate long fp; 1987c478bd9Sstevel@tonic-gate long pc; 1997c478bd9Sstevel@tonic-gate int signo; 2007c478bd9Sstevel@tonic-gate ucontext_t *ucp; 2017c478bd9Sstevel@tonic-gate siginfo_t *sip; 2027c478bd9Sstevel@tonic-gate } sf_t; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate uclist_t ucl; 2057c478bd9Sstevel@tonic-gate ucontext_t uc; 2067c478bd9Sstevel@tonic-gate uintptr_t uc_addr; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate init_uclist(&ucl, P); 2097c478bd9Sstevel@tonic-gate (void) memcpy(gregs, regs, sizeof (gregs)); 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate fp = regs[R_FP]; 2127c478bd9Sstevel@tonic-gate pc = regs[R_PC]; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate while (fp != 0 || pc != 0) { 2157c478bd9Sstevel@tonic-gate if (stack_loop(fp, &prevfp, &nfp, &pfpsize)) 2167c478bd9Sstevel@tonic-gate break; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate if (fp != 0 && 2197c478bd9Sstevel@tonic-gate (sz = Pread(P, &frame, sizeof (frame), (uintptr_t)fp) 2207c478bd9Sstevel@tonic-gate >= (ssize_t)(2* sizeof (long)))) { 2217c478bd9Sstevel@tonic-gate /* 2227c478bd9Sstevel@tonic-gate * One more trick for signal frames: the kernel sets 2237c478bd9Sstevel@tonic-gate * the return pc of the signal frame to 0xffffffff on 2247c478bd9Sstevel@tonic-gate * Intel IA32, so argcount won't work. 2257c478bd9Sstevel@tonic-gate */ 2267c478bd9Sstevel@tonic-gate if (frame.pc != -1L) { 2277c478bd9Sstevel@tonic-gate sz -= 2* sizeof (long); 2287c478bd9Sstevel@tonic-gate argc = argcount(P, (long)frame.pc, sz); 2297c478bd9Sstevel@tonic-gate } else 2307c478bd9Sstevel@tonic-gate argc = 3; /* sighandler(signo, sip, ucp) */ 2317c478bd9Sstevel@tonic-gate } else { 2327c478bd9Sstevel@tonic-gate (void) memset(&frame, 0, sizeof (frame)); 2337c478bd9Sstevel@tonic-gate argc = 0; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate gregs[R_FP] = fp; 2377c478bd9Sstevel@tonic-gate gregs[R_PC] = pc; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if ((rv = func(arg, gregs, argc, frame.args)) != 0) 2407c478bd9Sstevel@tonic-gate break; 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate /* 2437c478bd9Sstevel@tonic-gate * In order to allow iteration over java frames (which can have 2447c478bd9Sstevel@tonic-gate * their own frame pointers), we allow the iterator to change 2457c478bd9Sstevel@tonic-gate * the contents of gregs. If we detect a change, then we assume 2467c478bd9Sstevel@tonic-gate * that the new values point to the next frame. 2477c478bd9Sstevel@tonic-gate */ 2487c478bd9Sstevel@tonic-gate if (gregs[R_FP] != fp || gregs[R_PC] != pc) { 2497c478bd9Sstevel@tonic-gate fp = gregs[R_FP]; 2507c478bd9Sstevel@tonic-gate pc = gregs[R_PC]; 2517c478bd9Sstevel@tonic-gate continue; 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate pfp = fp; 2557c478bd9Sstevel@tonic-gate fp = frame.fp; 2567c478bd9Sstevel@tonic-gate pc = frame.pc; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate if (find_uclink(&ucl, pfp + sizeof (sf_t))) 2597c478bd9Sstevel@tonic-gate uc_addr = pfp + sizeof (sf_t); 2607c478bd9Sstevel@tonic-gate else 2617c478bd9Sstevel@tonic-gate uc_addr = NULL; 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate if (uc_addr != NULL && 2647c478bd9Sstevel@tonic-gate Pread(P, &uc, sizeof (uc), uc_addr) == sizeof (uc)) { 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate ucontext_n_to_prgregs(&uc, gregs); 2677c478bd9Sstevel@tonic-gate fp = gregs[R_FP]; 2687c478bd9Sstevel@tonic-gate pc = gregs[R_PC]; 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate if (prevfp) 2737c478bd9Sstevel@tonic-gate free(prevfp); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate free_uclist(&ucl); 2767c478bd9Sstevel@tonic-gate return (rv); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate uintptr_t 2807c478bd9Sstevel@tonic-gate Psyscall_setup(struct ps_prochandle *P, int nargs, int sysindex, uintptr_t sp) 2817c478bd9Sstevel@tonic-gate { 2827c478bd9Sstevel@tonic-gate sp -= sizeof (int) * (nargs+2); /* space for arg list + CALL parms */ 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate P->status.pr_lwp.pr_reg[EAX] = sysindex; 2857c478bd9Sstevel@tonic-gate P->status.pr_lwp.pr_reg[R_SP] = sp; 2867c478bd9Sstevel@tonic-gate P->status.pr_lwp.pr_reg[R_PC] = P->sysaddr; 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate return (sp); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate int 2927c478bd9Sstevel@tonic-gate Psyscall_copyinargs(struct ps_prochandle *P, int nargs, argdes_t *argp, 2937c478bd9Sstevel@tonic-gate uintptr_t ap) 2947c478bd9Sstevel@tonic-gate { 2957c478bd9Sstevel@tonic-gate int32_t arglist[MAXARGS+2]; 2967c478bd9Sstevel@tonic-gate int i; 2977c478bd9Sstevel@tonic-gate argdes_t *adp; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate for (i = 0, adp = argp; i < nargs; i++, adp++) 3007c478bd9Sstevel@tonic-gate arglist[1 + i] = (int32_t)adp->arg_value; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate arglist[0] = P->status.pr_lwp.pr_reg[R_PC]; 3037c478bd9Sstevel@tonic-gate if (Pwrite(P, &arglist[0], sizeof (int) * (nargs+1), 3047c478bd9Sstevel@tonic-gate (uintptr_t)ap) != sizeof (int) * (nargs+1)) 3057c478bd9Sstevel@tonic-gate return (-1); 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate return (0); 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate int 3117c478bd9Sstevel@tonic-gate Psyscall_copyoutargs(struct ps_prochandle *P, int nargs, argdes_t *argp, 3127c478bd9Sstevel@tonic-gate uintptr_t ap) 3137c478bd9Sstevel@tonic-gate { 3147c478bd9Sstevel@tonic-gate uint32_t arglist[MAXARGS + 2]; 3157c478bd9Sstevel@tonic-gate int i; 3167c478bd9Sstevel@tonic-gate argdes_t *adp; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate if (Pread(P, &arglist[0], sizeof (int) * (nargs+1), (uintptr_t)ap) 3197c478bd9Sstevel@tonic-gate != sizeof (int) * (nargs+1)) 3207c478bd9Sstevel@tonic-gate return (-1); 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate for (i = 0, adp = argp; i < nargs; i++, adp++) 3237c478bd9Sstevel@tonic-gate adp->arg_value = arglist[i]; 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate return (0); 3267c478bd9Sstevel@tonic-gate } 327