xref: /netbsd-src/external/cddl/osnet/dist/lib/libdtrace/arm/dt_isadep.c (revision 924795e69c8bb3f17afd8fcbb799710cc1719dc4)
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  * Copyright 2014 Howard Su
26  * Copyright 2015 George V. Neville-Neil
27  *
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <libgen.h>
37 #include <sys/ioctl.h>
38 
39 #include <dt_impl.h>
40 #include <dt_pid.h>
41 
42 #ifndef illumos
43 #include <libproc_compat.h>
44 #endif
45 
46 #define	OP(x)		((x) >> 30)
47 #define	OP2(x)		(((x) >> 22) & 0x07)
48 #define	COND(x)		(((x) >> 25) & 0x0f)
49 #define	A(x)		(((x) >> 29) & 0x01)
50 
51 #define	OP_BRANCH	0
52 
53 #define	OP2_BPcc	0x1
54 #define	OP2_Bicc	0x2
55 #define	OP2_BPr		0x3
56 #define	OP2_FBPfcc	0x5
57 #define	OP2_FBfcc	0x6
58 
59 /*ARGSUSED*/
60 int
61 dt_pid_create_entry_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
62     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp)
63 {
64 	ftp->ftps_type = DTFTP_ENTRY;
65 	ftp->ftps_pc = (uintptr_t)symp->st_value;
66 	ftp->ftps_size = (size_t)symp->st_size;
67 	ftp->ftps_noffs = 1;
68 	ftp->ftps_offs[0] = 0;
69 
70 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
71 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
72 		    strerror(errno));
73 		return (dt_set_errno(dtp, errno));
74 	}
75 
76 	return (1);
77 }
78 
79 int
80 dt_pid_create_return_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
81     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, uint64_t *stret)
82 {
83 
84 	uint32_t *text;
85 	int i;
86 	int srdepth = 0;
87 
88 	dt_dprintf("%s: unimplemented\n", __func__);
89 	return (DT_PROC_ERR);
90 
91 	if ((text = malloc(symp->st_size + 4)) == NULL) {
92 		dt_dprintf("mr sparkle: malloc() failed\n");
93 		return (DT_PROC_ERR);
94 	}
95 
96 	if (Pread(P, text, symp->st_size, symp->st_value) != symp->st_size) {
97 		dt_dprintf("mr sparkle: Pread() failed\n");
98 		free(text);
99 		return (DT_PROC_ERR);
100 	}
101 
102 	/*
103 	 * Leave a dummy instruction in the last slot to simplify edge
104 	 * conditions.
105 	 */
106 	text[symp->st_size / 4] = 0;
107 
108 	ftp->ftps_type = DTFTP_RETURN;
109 	ftp->ftps_pc = symp->st_value;
110 	ftp->ftps_size = symp->st_size;
111 	ftp->ftps_noffs = 0;
112 
113 
114 	free(text);
115 	if (ftp->ftps_noffs > 0) {
116 		if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
117 			dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
118 			    strerror(errno));
119 			return (dt_set_errno(dtp, errno));
120 		}
121 	}
122 
123 
124 	return (ftp->ftps_noffs);
125 }
126 
127 /*ARGSUSED*/
128 int
129 dt_pid_create_offset_probe(struct ps_prochandle *P, dtrace_hdl_t *dtp,
130     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, ulong_t off)
131 {
132 	if (off & 0x3)
133 		return (DT_PROC_ALIGN);
134 
135 	ftp->ftps_type = DTFTP_OFFSETS;
136 	ftp->ftps_pc = (uintptr_t)symp->st_value;
137 	ftp->ftps_size = (size_t)symp->st_size;
138 	ftp->ftps_noffs = 1;
139 	ftp->ftps_offs[0] = off;
140 
141 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
142 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
143 		    strerror(errno));
144 		return (dt_set_errno(dtp, errno));
145 	}
146 
147 	return (1);
148 }
149 
150 /*ARGSUSED*/
151 int
152 dt_pid_create_glob_offset_probes(struct ps_prochandle *P, dtrace_hdl_t *dtp,
153     fasttrap_probe_spec_t *ftp, const GElf_Sym *symp, const char *pattern)
154 {
155 	ulong_t i;
156 
157 	ftp->ftps_type = DTFTP_OFFSETS;
158 	ftp->ftps_pc = (uintptr_t)symp->st_value;
159 	ftp->ftps_size = (size_t)symp->st_size;
160 	ftp->ftps_noffs = 0;
161 
162 	/*
163 	 * If we're matching against everything, just iterate through each
164 	 * instruction in the function, otherwise look for matching offset
165 	 * names by constructing the string and comparing it against the
166 	 * pattern.
167 	 */
168 	if (strcmp("*", pattern) == 0) {
169 		for (i = 0; i < symp->st_size; i += 4) {
170 			ftp->ftps_offs[ftp->ftps_noffs++] = i;
171 		}
172 	} else {
173 		char name[sizeof (i) * 2 + 1];
174 
175 		for (i = 0; i < symp->st_size; i += 4) {
176 			(void) snprintf(name, sizeof(name), "%lx", i);
177 			if (gmatch(name, pattern))
178 				ftp->ftps_offs[ftp->ftps_noffs++] = i;
179 		}
180 	}
181 
182 	if (ioctl(dtp->dt_ftfd, FASTTRAPIOC_MAKEPROBE, ftp) != 0) {
183 		dt_dprintf("fasttrap probe creation ioctl failed: %s\n",
184 		    strerror(errno));
185 		return (dt_set_errno(dtp, errno));
186 	}
187 
188 	return (ftp->ftps_noffs);
189 }
190