xref: /netbsd-src/external/cddl/osnet/dist/lib/libdtrace/arm/dt_isadep.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <libgen.h>
34 #include <sys/ioctl.h>
35 
36 #include <dt_impl.h>
37 #include <dt_pid.h>
38 
39 #define	OP(x)		((x) >> 30)
40 #define	OP2(x)		(((x) >> 22) & 0x07)
41 #define	COND(x)		(((x) >> 25) & 0x0f)
42 #define	A(x)		(((x) >> 29) & 0x01)
43 
44 #define	OP_BRANCH	0
45 
46 #define	OP2_BPcc	0x1
47 #define	OP2_Bicc	0x2
48 #define	OP2_BPr		0x3
49 #define	OP2_FBPfcc	0x5
50 #define	OP2_FBfcc	0x6
51 
52 /*ARGSUSED*/
53 int
54 dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
55     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
56 {
57 	ftp->ftps_type = DTFTP_ENTRY;
58 	ftp->ftps_pc = (uintptr_t)symp->st_value;
59 	ftp->ftps_size = (size_t)symp->st_size;
60 	ftp->ftps_noffs = 1;
61 	ftp->ftps_offs[0] = 0;
62 
63 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
64 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
65 		    strerror(errno));
66 		return (dt_set_errno(dtp, errno));
67 	}
68 
69 	return (1);
70 }
71 
72 int
73 dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
74     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)
75 {
76 
77 	uint32_t *text;
78 
79 	dt_dprintf("%s: unimplemented\n", __func__);
80 	return (DT_PROC_ERR);
81 
82 	if ((text = malloc(symp->st_size + 4)) == NULL) {
83 		dt_dprintf("mr sparkle: malloc() failed\n");
84 		return (DT_PROC_ERR);
85 	}
86 #ifdef DOODAD
87 	if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
88 		dt_dprintf("mr sparkle: Pread() failed\n");
89 		free(text);
90 		return (DT_PROC_ERR);
91 	}
92 #endif
93 
94 	/*
95 	 * Leave a dummy instruction in the last slot to simplify edge
96 	 * conditions.
97 	 */
98 	text[symp->st_size / 4] = 0;
99 
100 	ftp->ftps_type = DTFTP_RETURN;
101 	ftp->ftps_pc = symp->st_value;
102 	ftp->ftps_size = symp->st_size;
103 	ftp->ftps_noffs = 0;
104 
105 
106 	free(text);
107 	if (ftp->ftps_noffs > 0) {
108 		if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
109 			dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
110 			    strerror(errno));
111 			return (dt_set_errno(dtp, errno));
112 		}
113 	}
114 
115 
116 	return (ftp->ftps_noffs);
117 }
118 
119 /*ARGSUSED*/
120 int
121 dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
122     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)
123 {
124 	if (off & 0x3)
125 		return (DT_PROC_ALIGN);
126 
127 	ftp->ftps_type = DTFTP_OFFSETS;
128 	ftp->ftps_pc = (uintptr_t)symp->st_value;
129 	ftp->ftps_size = (size_t)symp->st_size;
130 	ftp->ftps_noffs = 1;
131 	ftp->ftps_offs[0] = off;
132 
133 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
134 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
135 		    strerror(errno));
136 		return (dt_set_errno(dtp, errno));
137 	}
138 
139 	return (1);
140 }
141 
142 /*ARGSUSED*/
143 int
144 dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,
145     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)
146 {
147 	ulong_t i;
148 
149 	ftp->ftps_type = DTFTP_OFFSETS;
150 	ftp->ftps_pc = (uintptr_t)symp->st_value;
151 	ftp->ftps_size = (size_t)symp->st_size;
152 	ftp->ftps_noffs = 0;
153 
154 	/*
155 	 * If we're matching against everything, just iterate through each
156 	 * instruction in the function, otherwise look for matching offset
157 	 * names by constructing the string and comparing it against the
158 	 * pattern.
159 	 */
160 	if (strcmp("*", pattern) == 0) {
161 		for (i = 0; i < symp->st_size; i += 4) {
162 			ftp->ftps_offs[ftp->ftps_noffs++] = i;
163 		}
164 	} else {
165 		char name[sizeof (i) * 2 + 1];
166 
167 		for (i = 0; i < symp->st_size; i += 4) {
168 			(void) snprintf(name, sizeof(name), "%lx", i);
169 			if (gmatch(name, pattern))
170 				ftp->ftps_offs[ftp->ftps_noffs++] = i;
171 		}
172 	}
173 
174 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
175 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
176 		    strerror(errno));
177 		return (dt_set_errno(dtp, errno));
178 	}
179 
180 	return (ftp->ftps_noffs);
181 }
182 
183