Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/powerpc/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#include <libproc_compat.h>3839/*ARGSUSED*/40int41dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,42fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)43{44ftp->ftps_type = DTFTP_ENTRY;45ftp->ftps_pc = (uintptr_t)symp->st_value;46ftp->ftps_size = (size_t)symp->st_size;47ftp->ftps_noffs = 1;48ftp->ftps_offs[0] = 0;4950if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {51dt_dprintf("fasttrap probe creation ioctl failed: %s\n",52strerror(errno));53return (dt_set_errno(dtp, errno));54}5556return (1);57}5859int60dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,61fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)62{6364uintptr_t temp;65uint32_t *text;66int i;67int srdepth = 0;6869if ((text = malloc(symp->st_size + 4)) == NULL) {70dt_dprintf("mr sparkle: malloc() failed\n");71return (DT_PROC_ERR);72}7374if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {75dt_dprintf("mr sparkle: Pread() failed\n");76free(text);77return (DT_PROC_ERR);78}7980/*81* Leave a dummy instruction in the last slot to simplify edge82* conditions.83*/84text[symp->st_size / 4] = 0;8586ftp->ftps_type = DTFTP_RETURN;87ftp->ftps_pc = symp->st_value;88ftp->ftps_size = symp->st_size;89ftp->ftps_noffs = 0;9091for (i = 0; i < symp->st_size / 4; i++) {9293if ((text[i] & 0xfc000001) != 0x48000000 &&94text[i] != 0x4e800020)95continue;9697/*98* Check for a jump within this function. If it's outside this99* function then it's a tail-call, so a return point.100*/101if ((text[i] & 0xfc000000) == 0x48000000) {102temp = (text[i] & 0x03fffffc);103/* Bit 30 denotes an absolute address. */104if (!(text[i] & 0x02)) {105temp += symp->st_value + i * 4;106}107else {108/* Sign extend the absolute address. */109if (temp & 0x02000000) {110temp |= (UINTPTR_MAX - 0x03ffffff);111}112}113if (temp >= symp->st_value &&114temp <= (symp->st_value + symp->st_size))115continue;116}117dt_dprintf("return at offset %x\n", i * 4);118ftp->ftps_offs[ftp->ftps_noffs++] = i * 4;119}120121free(text);122if (ftp->ftps_noffs > 0) {123if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {124dt_dprintf("fasttrap probe creation ioctl failed: %s\n",125strerror(errno));126return (dt_set_errno(dtp, errno));127}128}129130131return (ftp->ftps_noffs);132}133134/*ARGSUSED*/135int136dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,137fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)138{139if (off & 0x3)140return (DT_PROC_ALIGN);141142ftp->ftps_type = DTFTP_OFFSETS;143ftp->ftps_pc = (uintptr_t)symp->st_value;144ftp->ftps_size = (size_t)symp->st_size;145ftp->ftps_noffs = 1;146ftp->ftps_offs[0] = off;147148if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {149dt_dprintf("fasttrap probe creation ioctl failed: %s\n",150strerror(errno));151return (dt_set_errno(dtp, errno));152}153154return (1);155}156157/*ARGSUSED*/158int159dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,160fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)161{162ulong_t i;163164ftp->ftps_type = DTFTP_OFFSETS;165ftp->ftps_pc = (uintptr_t)symp->st_value;166ftp->ftps_size = (size_t)symp->st_size;167ftp->ftps_noffs = 0;168169/*170* If we're matching against everything, just iterate through each171* instruction in the function, otherwise look for matching offset172* names by constructing the string and comparing it against the173* pattern.174*/175if (strcmp("*", pattern) == 0) {176for (i = 0; i < symp->st_size; i += 4) {177ftp->ftps_offs[ftp->ftps_noffs++] = i;178}179} else {180char name[sizeof (i) * 2 + 1];181182for (i = 0; i < symp->st_size; i += 4) {183(void) sprintf(name, "%lx", i);184if (gmatch(name, pattern))185ftp->ftps_offs[ftp->ftps_noffs++] = i;186}187}188189if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {190dt_dprintf("fasttrap probe creation ioctl failed: %s\n",191strerror(errno));192return (dt_set_errno(dtp, errno));193}194195return (ftp->ftps_noffs);196}197198199