Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/sparc/dt_isadep.c
39563 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2005 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728#include <stdlib.h>29#include <assert.h>30#include <errno.h>31#include <string.h>32#include <libgen.h>3334#include <dt_impl.h>35#include <dt_pid.h>3637#define OP(x) ((x) >> 30)38#define OP2(x) (((x) >> 22) & 0x07)39#define COND(x) (((x) >> 25) & 0x0f)40#define A(x) (((x) >> 29) & 0x01)4142#define OP_BRANCH 04344#define OP2_BPcc 0x145#define OP2_Bicc 0x246#define OP2_BPr 0x347#define OP2_FBPfcc 0x548#define OP2_FBfcc 0x64950/*ARGSUSED*/51int52dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,53fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)54{55ftp->ftps_type = DTFTP_ENTRY;56ftp->ftps_pc = (uintptr_t)symp->st_value;57ftp->ftps_size = (size_t)symp->st_size;58ftp->ftps_noffs = 1;59ftp->ftps_offs[0] = 0;6061if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {62dt_dprintf("fasttrap probe creation ioctl failed: %s\n",63strerror(errno));64return (dt_set_errno(dtp, errno));65}6667return (1);68}6970int71dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,72fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)73{7475uint32_t *text;76int i;77int srdepth = 0;7879if ((text = malloc(symp->st_size + 4)) == NULL) {80dt_dprintf("mr sparkle: malloc() failed\n");81return (DT_PROC_ERR);82}8384if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {85dt_dprintf("mr sparkle: Pread() failed\n");86free(text);87return (DT_PROC_ERR);88}8990/*91* Leave a dummy instruction in the last slot to simplify edge92* conditions.93*/94text[symp->st_size / 4] = 0;9596ftp->ftps_type = DTFTP_RETURN;97ftp->ftps_pc = symp->st_value;98ftp->ftps_size = symp->st_size;99ftp->ftps_noffs = 0;100101for (i = 0; i < symp->st_size / 4; i++) {102/*103* If we encounter an existing tracepoint, query the104* kernel to find out the instruction that was105* replaced at this spot.106*/107while (text[i] == FASTTRAP_INSTR) {108fasttrap_instr_query_t instr;109110instr.ftiq_pid = Pstatus(P)->pr_pid;111instr.ftiq_pc = symp->st_value + i * 4;112113if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_GETINSTR,114&instr) != 0) {115116if (errno == ESRCH || errno == ENOENT) {117if (Pread(P, &text[i], 4,118instr.ftiq_pc) != 4) {119dt_dprintf("mr sparkle: "120"Pread() failed\n");121free(text);122return (DT_PROC_ERR);123}124continue;125}126127free(text);128dt_dprintf("mr sparkle: getinstr query "129"failed: %s\n", strerror(errno));130return (DT_PROC_ERR);131}132133text[i] = instr.ftiq_instr;134break;135}136137/* save */138if ((text[i] & 0xc1f80000) == 0x81e00000) {139srdepth++;140continue;141}142143/* restore */144if ((text[i] & 0xc1f80000) == 0x81e80000) {145srdepth--;146continue;147}148149if (srdepth > 0) {150/* ret */151if (text[i] == 0x81c7e008)152goto is_ret;153154/* return */155if (text[i] == 0x81cfe008)156goto is_ret;157158/* call or jmpl w/ restore in the slot */159if (((text[i] & 0xc0000000) == 0x40000000 ||160(text[i] & 0xc1f80000) == 0x81c00000) &&161(text[i + 1] & 0xc1f80000) == 0x81e80000)162goto is_ret;163164/* call to one of the stret routines */165if ((text[i] & 0xc0000000) == 0x40000000) {166int32_t disp = text[i] << 2;167uint64_t dest = ftp->ftps_pc + i * 4 + disp;168169dt_dprintf("dest = %llx\n", (u_longlong_t)dest);170171if (dest == stret[0] || dest == stret[1] ||172dest == stret[2] || dest == stret[3])173goto is_ret;174}175} else {176/* external call */177if ((text[i] & 0xc0000000) == 0x40000000) {178int32_t dst = text[i] << 2;179180dst += i * 4;181182if ((uintptr_t)dst >= (uintptr_t)symp->st_size)183goto is_ret;184}185186/* jmpl into %g0 -- this includes the retl pseudo op */187if ((text[i] & 0xfff80000) == 0x81c00000)188goto is_ret;189190/* external branch -- possible return site */191if (OP(text[i]) == OP_BRANCH) {192int32_t dst;193int baa;194195switch (OP2(text[i])) {196case OP2_BPcc:197dst = text[i] & 0x7ffff;198dst <<= 13;199dst >>= 11;200201baa = COND(text[i]) == 8 && A(text[i]);202break;203case OP2_Bicc:204dst = text[i] & 0x3fffff;205dst <<= 10;206dst >>= 8;207208baa = COND(text[i]) == 8 && A(text[i]);209break;210case OP2_BPr:211dst = (((text[i]) >> 6) & 0xc000) |212((text[i]) & 0x3fff);213dst <<= 16;214dst >>= 14;215216baa = 0;217break;218case OP2_FBPfcc:219dst = text[i] & 0x7ffff;220dst <<= 13;221dst >>= 11;222223baa = COND(text[i]) == 8 && A(text[i]);224break;225case OP2_FBfcc:226dst = text[i] & 0x3fffff;227dst <<= 10;228dst >>= 8;229230baa = COND(text[i]) == 8 && A(text[i]);231break;232default:233continue;234}235236dst += i * 4;237238/*239* Interpret branches outside of the function's240* bounds as potential return sites. If the241* branch is a ba,a don't skip the instruction242* in the delay slot.243*/244if ((uintptr_t)dst >=245(uintptr_t)symp->st_size) {246if (baa)247goto is_ret_baa;248else249goto is_ret;250}251}252}253254continue;255is_ret:256i++;257is_ret_baa:258dt_dprintf("return at offset %x\n", i * 4);259ftp->ftps_offs[ftp->ftps_noffs++] = i * 4;260}261262free(text);263if (ftp->ftps_noffs > 0) {264if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {265dt_dprintf("fasttrap probe creation ioctl failed: %s\n",266strerror(errno));267return (dt_set_errno(dtp, errno));268}269}270271272return (ftp->ftps_noffs);273}274275/*ARGSUSED*/276int277dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,278fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)279{280if (off & 0x3)281return (DT_PROC_ALIGN);282283ftp->ftps_type = DTFTP_OFFSETS;284ftp->ftps_pc = (uintptr_t)symp->st_value;285ftp->ftps_size = (size_t)symp->st_size;286ftp->ftps_noffs = 1;287ftp->ftps_offs[0] = off;288289if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {290dt_dprintf("fasttrap probe creation ioctl failed: %s\n",291strerror(errno));292return (dt_set_errno(dtp, errno));293}294295return (1);296}297298/*ARGSUSED*/299int300dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,301fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)302{303ulong_t i;304305ftp->ftps_type = DTFTP_OFFSETS;306ftp->ftps_pc = (uintptr_t)symp->st_value;307ftp->ftps_size = (size_t)symp->st_size;308ftp->ftps_noffs = 0;309310/*311* If we're matching against everything, just iterate through each312* instruction in the function, otherwise look for matching offset313* names by constructing the string and comparing it against the314* pattern.315*/316if (strcmp("*", pattern) == 0) {317for (i = 0; i < symp->st_size; i += 4) {318ftp->ftps_offs[ftp->ftps_noffs++] = i;319}320} else {321char name[sizeof (i) * 2 + 1];322323for (i = 0; i < symp->st_size; i += 4) {324(void) sprintf(name, "%lx", i);325if (gmatch(name, pattern))326ftp->ftps_offs[ftp->ftps_noffs++] = i;327}328}329330if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {331dt_dprintf("fasttrap probe creation ioctl failed: %s\n",332strerror(errno));333return (dt_set_errno(dtp, errno));334}335336return (ftp->ftps_noffs);337}338339340