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