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 5*20c1c355SRod Evans * Common Development and Distribution License (the "License"). 6*20c1c355SRod Evans * 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 */ 21*20c1c355SRod Evans 227c478bd9Sstevel@tonic-gate /* 23*20c1c355SRod Evans * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate #include <stdio.h> 267c478bd9Sstevel@tonic-gate #include <fcntl.h> 277c478bd9Sstevel@tonic-gate #include <link.h> 287c478bd9Sstevel@tonic-gate #include <stdlib.h> 297c478bd9Sstevel@tonic-gate #include <unistd.h> 307c478bd9Sstevel@tonic-gate #include <strings.h> 317c478bd9Sstevel@tonic-gate #include <sys/regset.h> 327c478bd9Sstevel@tonic-gate #include <sys/frame.h> 337c478bd9Sstevel@tonic-gate #include <sys/stack.h> 347c478bd9Sstevel@tonic-gate #include <signal.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include "env.h" 377c478bd9Sstevel@tonic-gate #include "mach.h" 387c478bd9Sstevel@tonic-gate #include "who.h" 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate static int detail_syms = 0; /* display detail symbol information */ 42*20c1c355SRod Evans static Objinfo *objhead = NULL; /* head of object list */ 43*20c1c355SRod Evans static Elist *funclist = NULL; 447c478bd9Sstevel@tonic-gate static sigset_t iset; 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate static void 477c478bd9Sstevel@tonic-gate add_object(Objinfo **objlist, Link_map *lmp) 487c478bd9Sstevel@tonic-gate { 497c478bd9Sstevel@tonic-gate Objinfo *op, *cur, *prev; 507c478bd9Sstevel@tonic-gate Elf_Ehdr *ehdr; 517c478bd9Sstevel@tonic-gate Elf_Phdr *phdr; 527c478bd9Sstevel@tonic-gate caddr_t lpc, hpc; 537c478bd9Sstevel@tonic-gate int i; 547c478bd9Sstevel@tonic-gate 55*20c1c355SRod Evans if ((op = calloc(1, sizeof (Objinfo))) == NULL) { 567c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "who.so.1: calloc failed\n"); 577c478bd9Sstevel@tonic-gate exit(1); 587c478bd9Sstevel@tonic-gate } 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate lpc = hpc = (caddr_t)lmp->l_addr; 617c478bd9Sstevel@tonic-gate /* LINTED */ 627c478bd9Sstevel@tonic-gate ehdr = (Elf_Ehdr *)lpc; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* LINTED */ 657c478bd9Sstevel@tonic-gate for (i = 0, phdr = (Elf_Phdr *)(ehdr->e_phoff + lpc); 667c478bd9Sstevel@tonic-gate i < ehdr->e_phnum; i++, phdr++) { 677c478bd9Sstevel@tonic-gate caddr_t _hpc; 687c478bd9Sstevel@tonic-gate if ((phdr->p_type == PT_LOAD) && 697c478bd9Sstevel@tonic-gate ((_hpc = phdr->p_vaddr + phdr->p_memsz + lpc) > hpc)) 707c478bd9Sstevel@tonic-gate hpc = _hpc; 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate op->o_lpc = lpc; 737c478bd9Sstevel@tonic-gate op->o_hpc = hpc; 747c478bd9Sstevel@tonic-gate op->o_lmp = lmp; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate if (ehdr->e_type == ET_EXEC) 777c478bd9Sstevel@tonic-gate op->o_flags |= FLG_OB_FIXED; 787c478bd9Sstevel@tonic-gate 79*20c1c355SRod Evans if (*objlist == NULL) { 807c478bd9Sstevel@tonic-gate *objlist = op; 817c478bd9Sstevel@tonic-gate return; 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * Do an insertion sort to maintain the list 857c478bd9Sstevel@tonic-gate * in order. 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate if ((*objlist)->o_lmp->l_addr > lmp->l_addr) { 887c478bd9Sstevel@tonic-gate op->o_next = *objlist; 897c478bd9Sstevel@tonic-gate *objlist = op; 907c478bd9Sstevel@tonic-gate return; 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 93*20c1c355SRod Evans for (prev = NULL, cur = *objlist; cur; prev = cur, cur = cur->o_next) { 947c478bd9Sstevel@tonic-gate if (lpc < cur->o_lpc) 957c478bd9Sstevel@tonic-gate break; 967c478bd9Sstevel@tonic-gate } 97*20c1c355SRod Evans if (prev == NULL) { 987c478bd9Sstevel@tonic-gate op->o_next = *objlist; 997c478bd9Sstevel@tonic-gate *objlist = op; 1007c478bd9Sstevel@tonic-gate return; 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate prev->o_next = op; 1037c478bd9Sstevel@tonic-gate op->o_next = cur; 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate static void 1077c478bd9Sstevel@tonic-gate remove_object(Objinfo **objlist, Link_map *lmp) 1087c478bd9Sstevel@tonic-gate { 1097c478bd9Sstevel@tonic-gate Objinfo *cur, *prev; 1107c478bd9Sstevel@tonic-gate 111*20c1c355SRod Evans for (prev = NULL, cur = *objlist; cur; prev = cur, cur = cur->o_next) { 1127c478bd9Sstevel@tonic-gate if (cur->o_lmp == lmp) 1137c478bd9Sstevel@tonic-gate break; 1147c478bd9Sstevel@tonic-gate } 115*20c1c355SRod Evans 1167c478bd9Sstevel@tonic-gate /* 1177c478bd9Sstevel@tonic-gate * Did we find it? 1187c478bd9Sstevel@tonic-gate */ 1197c478bd9Sstevel@tonic-gate if (!cur) 1207c478bd9Sstevel@tonic-gate return; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate if (!prev) 1237c478bd9Sstevel@tonic-gate *objlist = cur->o_next; 1247c478bd9Sstevel@tonic-gate else 1257c478bd9Sstevel@tonic-gate prev->o_next = cur->o_next; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate if (cur->o_elf) { 1287c478bd9Sstevel@tonic-gate (void) elf_end(cur->o_elf); 1297c478bd9Sstevel@tonic-gate (void) close(cur->o_fd); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate free(cur); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate static void 1357c478bd9Sstevel@tonic-gate print_simple_address(void *pc) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate Dl_info info; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate if (dladdr(pc, &info) == 0) { 1407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1417c478bd9Sstevel@tonic-gate "\t<unknown>: 0x%lx\n", (unsigned long)pc); 1427c478bd9Sstevel@tonic-gate return; 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 145*20c1c355SRod Evans (void) fprintf(stderr, "\t%s:%s+0x%lx\n", info.dli_fname, 146*20c1c355SRod Evans info.dli_sname, 1477c478bd9Sstevel@tonic-gate (ulong_t)((uintptr_t)pc - (uintptr_t)info.dli_saddr)); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate static void 1517c478bd9Sstevel@tonic-gate load_syms(Objinfo *op) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate int fd; 1547c478bd9Sstevel@tonic-gate Elf *elf; 1557c478bd9Sstevel@tonic-gate Elf_Scn *scn; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 1587c478bd9Sstevel@tonic-gate op->o_flags |= FLG_OB_NOSYMS; 1597c478bd9Sstevel@tonic-gate return; 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate if ((fd = open(op->o_lmp->l_name, O_RDONLY)) == -1) { 1637c478bd9Sstevel@tonic-gate op->o_flags |= FLG_OB_NOSYMS; 1647c478bd9Sstevel@tonic-gate return; 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 167*20c1c355SRod Evans if ((elf = elf_begin(fd, ELF_C_READ, 0)) == NULL) { 1687c478bd9Sstevel@tonic-gate op->o_flags |= FLG_OB_NOSYMS; 1697c478bd9Sstevel@tonic-gate (void) close(fd); 1707c478bd9Sstevel@tonic-gate return; 1717c478bd9Sstevel@tonic-gate } 172*20c1c355SRod Evans scn = NULL; 173*20c1c355SRod Evans while ((scn = elf_nextscn(elf, scn)) != NULL) { 1747c478bd9Sstevel@tonic-gate Elf_Shdr *shdr; 1757c478bd9Sstevel@tonic-gate Elf_Data *data; 176*20c1c355SRod Evans 1777c478bd9Sstevel@tonic-gate shdr = elf_getshdr(scn); 1787c478bd9Sstevel@tonic-gate if (shdr->sh_type != SHT_SYMTAB) 1797c478bd9Sstevel@tonic-gate continue; 1807c478bd9Sstevel@tonic-gate data = elf_getdata(scn, 0); 1817c478bd9Sstevel@tonic-gate op->o_syms = (Elf_Sym *)data->d_buf; 1827c478bd9Sstevel@tonic-gate /* LINTED */ 1837c478bd9Sstevel@tonic-gate op->o_symcnt = (uint_t)(shdr->sh_size / shdr->sh_entsize); 1847c478bd9Sstevel@tonic-gate scn = elf_getscn(elf, shdr->sh_link); 1857c478bd9Sstevel@tonic-gate data = elf_getdata(scn, 0); 1867c478bd9Sstevel@tonic-gate op->o_strs = (const char *)data->d_buf; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate if (!op->o_syms) { 1897c478bd9Sstevel@tonic-gate (void) elf_end(elf); 1907c478bd9Sstevel@tonic-gate (void) close(fd); 1917c478bd9Sstevel@tonic-gate op->o_flags |= FLG_OB_NOSYMS; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate } 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate static void 1977c478bd9Sstevel@tonic-gate print_address(caddr_t pc) 1987c478bd9Sstevel@tonic-gate { 1997c478bd9Sstevel@tonic-gate Elf_Sym *sym, *_sym; 2007c478bd9Sstevel@tonic-gate Objinfo *op; 2017c478bd9Sstevel@tonic-gate int i; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate if (!detail_syms) { 2047c478bd9Sstevel@tonic-gate print_simple_address(pc); 2057c478bd9Sstevel@tonic-gate return; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate for (op = objhead; op; op = op->o_next) { 2087c478bd9Sstevel@tonic-gate if ((pc >= op->o_lpc) && (pc <= op->o_hpc)) 2097c478bd9Sstevel@tonic-gate break; 2107c478bd9Sstevel@tonic-gate } 211*20c1c355SRod Evans if (op && (op->o_syms == NULL)) 2127c478bd9Sstevel@tonic-gate load_syms(op); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate if (!op || (op->o_flags & FLG_OB_NOSYMS)) { 2157c478bd9Sstevel@tonic-gate print_simple_address(pc); 2167c478bd9Sstevel@tonic-gate return; 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate sym = op->o_syms; 2207c478bd9Sstevel@tonic-gate if ((op->o_flags & FLG_OB_FIXED) == 0) 2217c478bd9Sstevel@tonic-gate pc = (caddr_t)((uintptr_t)pc - (uintptr_t)op->o_lpc); 2227c478bd9Sstevel@tonic-gate for (i = 0, _sym = op->o_syms; i < op->o_symcnt; i++, _sym++) { 2237c478bd9Sstevel@tonic-gate if (((uintptr_t)_sym->st_value < (uintptr_t)pc) && 2247c478bd9Sstevel@tonic-gate (_sym->st_value > sym->st_value)) 2257c478bd9Sstevel@tonic-gate sym = _sym; 2267c478bd9Sstevel@tonic-gate } 227*20c1c355SRod Evans (void) fprintf(stderr, "\t%s:%s+0x%lx\n", op->o_lmp->l_name, 228*20c1c355SRod Evans sym->st_name + op->o_strs, 2297c478bd9Sstevel@tonic-gate (ulong_t)((uintptr_t)pc - (uintptr_t)sym->st_value)); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate static void 2337c478bd9Sstevel@tonic-gate print_stack(struct frame *sp) 2347c478bd9Sstevel@tonic-gate { 2357c478bd9Sstevel@tonic-gate FLUSHWIN(); 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate while (sp && sp->fr_savpc) { 2387c478bd9Sstevel@tonic-gate print_address((caddr_t)sp->fr_savpc); 2397c478bd9Sstevel@tonic-gate sp = (struct frame *)((ulong_t)sp->fr_savfp + STACK_BIAS); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate uint_t 2447c478bd9Sstevel@tonic-gate la_version(uint_t version) 2457c478bd9Sstevel@tonic-gate { 2467c478bd9Sstevel@tonic-gate if (version > LAV_CURRENT) 2477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "who.so: unexpected version: %d\n", 2487c478bd9Sstevel@tonic-gate version); 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate if (checkenv((const char *)"WHO_DETAIL")) 2517c478bd9Sstevel@tonic-gate detail_syms++; 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate build_env_list(&funclist, (const char *)"WHOCALLS"); 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate /* 2567c478bd9Sstevel@tonic-gate * Initalize iset to the full set of signals to be masked durring 2577c478bd9Sstevel@tonic-gate * pltenter/pltexit 2587c478bd9Sstevel@tonic-gate */ 2597c478bd9Sstevel@tonic-gate (void) sigfillset(&iset); 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate return (LAV_CURRENT); 2627c478bd9Sstevel@tonic-gate } 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate /* ARGSUSED1 */ 2657c478bd9Sstevel@tonic-gate uint_t 2667c478bd9Sstevel@tonic-gate la_objopen(Link_map *lmp, Lmid_t lmid, uintptr_t *cookie) 2677c478bd9Sstevel@tonic-gate { 2687c478bd9Sstevel@tonic-gate add_object(&objhead, lmp); 2697c478bd9Sstevel@tonic-gate return (LA_FLG_BINDTO | LA_FLG_BINDFROM); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate uint_t 2737c478bd9Sstevel@tonic-gate la_objclose(uintptr_t *cookie) 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate remove_object(&objhead, (Link_map *)(*cookie)); 2767c478bd9Sstevel@tonic-gate return (1); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate /* ARGSUSED1 */ 2807c478bd9Sstevel@tonic-gate #if defined(__sparcv9) 2817c478bd9Sstevel@tonic-gate uintptr_t 2827c478bd9Sstevel@tonic-gate la_sparcv9_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie, 2837c478bd9Sstevel@tonic-gate uintptr_t *defcookie, La_sparcv9_regs *regset, uint_t *sb_flags, 2847c478bd9Sstevel@tonic-gate const char *sym_name) 2857c478bd9Sstevel@tonic-gate #elif defined(__sparc) 2867c478bd9Sstevel@tonic-gate uintptr_t 2877c478bd9Sstevel@tonic-gate la_sparcv8_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcookie, 2887c478bd9Sstevel@tonic-gate uintptr_t *defcookie, La_sparcv8_regs *regset, uint_t *sb_flags) 2897c478bd9Sstevel@tonic-gate #elif defined(__amd64) 2907c478bd9Sstevel@tonic-gate uintptr_t 2917c478bd9Sstevel@tonic-gate la_amd64_pltenter(Elf64_Sym *symp, uint_t symndx, uintptr_t *refcookie, 2927c478bd9Sstevel@tonic-gate uintptr_t *defcookie, La_amd64_regs *regset, uint_t *sb_flags, 2937c478bd9Sstevel@tonic-gate const char *sym_name) 2947c478bd9Sstevel@tonic-gate #elif defined(__i386) 2957c478bd9Sstevel@tonic-gate uintptr_t 2967c478bd9Sstevel@tonic-gate la_i86_pltenter(Elf32_Sym *symp, uint_t symndx, uintptr_t *refcooke, 2977c478bd9Sstevel@tonic-gate uintptr_t *defcook, La_i86_regs *regset, uint_t *sb_flags) 2987c478bd9Sstevel@tonic-gate #endif 2997c478bd9Sstevel@tonic-gate { 3007c478bd9Sstevel@tonic-gate sigset_t oset; 3017c478bd9Sstevel@tonic-gate #if !defined(_LP64) 3027c478bd9Sstevel@tonic-gate const char *sym_name = (const char *)symp->st_name; 3037c478bd9Sstevel@tonic-gate #endif 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &iset, &oset); 3067c478bd9Sstevel@tonic-gate if (check_list(funclist, sym_name)) { 3077c478bd9Sstevel@tonic-gate struct frame *frame_p; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s(0x%lx, 0x%lx, 0x%lx)\n", sym_name, 3107c478bd9Sstevel@tonic-gate (long)GETARG0(regset), (long)GETARG1(regset), 3117c478bd9Sstevel@tonic-gate (long)GETARG2(regset)); 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate print_address((caddr_t)GETPREVPC(regset)); 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate frame_p = (struct frame *)((ulong_t)GETFRAME(regset) 3167c478bd9Sstevel@tonic-gate + STACK_BIAS); 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate print_stack(frame_p); 3197c478bd9Sstevel@tonic-gate (void) fflush(stdout); 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &oset, NULL); 3227c478bd9Sstevel@tonic-gate return (symp->st_value); 3237c478bd9Sstevel@tonic-gate } 324