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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <floatingpoint.h>
30 #include <libctf.h>
31 #include <apptrace.h>
32 
33 typedef struct printarg {
34 	ulong_t		pa_addr;
35 	ctf_file_t	*pa_ctfp;
36 	int		pa_depth;
37 	int		pa_nest;
38 } printarg_t;
39 
40 typedef void printarg_f(ctf_id_t, ulong_t, printarg_t *);
41 
42 static int elt_print(const char *, ctf_id_t, ulong_t, int, void *);
43 
44 const char *
type_name(ctf_file_t * ctfp,ctf_id_t type,char * buf,size_t len)45 type_name(ctf_file_t *ctfp, ctf_id_t type, char *buf, size_t len)
46 {
47 	if (ctf_type_name(ctfp, type, buf, len) == NULL)
48 		(void) snprintf(buf, len, "<%ld>", type);
49 
50 	return (buf);
51 }
52 
53 void
print_value(ctf_file_t * ctfp,ctf_id_t type,ulong_t value)54 print_value(ctf_file_t *ctfp, ctf_id_t type, ulong_t value)
55 {
56 	ctf_id_t	rtype = ctf_type_resolve(ctfp, type);
57 	ctf_encoding_t	e;
58 
59 	(void) fprintf(ABISTREAM, "0x%p", (void *)value);
60 
61 	if (ctf_type_kind(ctfp, rtype) == CTF_K_POINTER) {
62 		type = ctf_type_reference(ctfp, rtype);
63 		rtype = ctf_type_resolve(ctfp, type);
64 
65 		if (ctf_type_encoding(ctfp, rtype, &e) == 0 &&
66 		    (e.cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) ==
67 		    (CTF_INT_CHAR | CTF_INT_SIGNED) && e.cte_bits == NBBY) {
68 			if ((char *)value != NULL)
69 				(void) fprintf(ABISTREAM,
70 				    " \"%s\"", (char *)value);
71 			else
72 				(void) fprintf(ABISTREAM, " <NULL>");
73 			(void) fflush(ABISTREAM);
74 			return;
75 		}
76 
77 		if (ctf_type_kind(ctfp, rtype) == CTF_K_STRUCT) {
78 			printarg_t pa;
79 
80 			(void) fprintf(ABISTREAM, " ");
81 
82 			pa.pa_addr = value;
83 			pa.pa_ctfp = ctfp;
84 			pa.pa_nest = 0;
85 			pa.pa_depth = 0;
86 
87 			(void) ctf_type_visit(ctfp, rtype, elt_print, &pa);
88 			(void) fprintf(ABISTREAM, "\t}");
89 			(void) fflush(ABISTREAM);
90 			return;
91 		}
92 	}
93 	(void) fflush(ABISTREAM);
94 }
95 
96 static void
print_bitfield(ulong_t off,ctf_encoding_t * ep)97 print_bitfield(ulong_t off, ctf_encoding_t *ep)
98 {
99 	uint64_t mask = (1ULL << ep->cte_bits) - 1;
100 	uint64_t value = 0;
101 #ifdef _BIG_ENDIAN
102 	size_t size = (ep->cte_bits + (NBBY - 1)) / NBBY;
103 	uint8_t *buf = (uint8_t *)&value;
104 #endif
105 	uint8_t shift;
106 
107 	/*
108 	 * On big-endian machines, we need to adjust the buf pointer to refer
109 	 * to the lowest 'size' bytes in 'value', and we need shift based on
110 	 * the offset from the end of the data, not the offset of the start.
111 	 */
112 #ifdef _BIG_ENDIAN
113 	buf += sizeof (value) - size;
114 	off += ep->cte_bits;
115 #endif
116 	shift = off % NBBY;
117 
118 	/*
119 	 * Offsets are counted from opposite ends on little- and
120 	 * big-endian machines.
121 	 */
122 #ifdef _BIG_ENDIAN
123 	shift = NBBY - shift;
124 #endif
125 
126 	/*
127 	 * If the bits we want do not begin on a byte boundary, shift the data
128 	 * right so that the value is in the lowest 'cte_bits' of 'value'.
129 	 */
130 	if (off % NBBY != 0)
131 		value >>= shift;
132 
133 	(void) fprintf(ABISTREAM, "%llu", (unsigned long long)(value & mask));
134 	(void) fflush(ABISTREAM);
135 }
136 
137 /* ARGSUSED */
138 static void
print_int(ctf_id_t base,ulong_t off,printarg_t * pap)139 print_int(ctf_id_t base, ulong_t off, printarg_t *pap)
140 {
141 	ctf_file_t *ctfp = pap->pa_ctfp;
142 	ctf_encoding_t e;
143 	size_t size;
144 	ulong_t addr = pap->pa_addr + off / NBBY;
145 
146 	if (ctf_type_encoding(ctfp, base, &e) == CTF_ERR) {
147 		(void) fprintf(ABISTREAM, "???");
148 		(void) fflush(ABISTREAM);
149 		return;
150 	}
151 
152 	if (e.cte_format & CTF_INT_VARARGS) {
153 		(void) fprintf(ABISTREAM, "...\n");
154 		(void) fflush(ABISTREAM);
155 		return;
156 	}
157 
158 	size = e.cte_bits / NBBY;
159 	if (size > 8 || (e.cte_bits % NBBY) != 0 || (size & (size - 1)) != 0) {
160 		print_bitfield(off, &e);
161 		return;
162 	}
163 
164 	if (((e).cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) ==
165 	    (CTF_INT_CHAR | CTF_INT_SIGNED) && (e).cte_bits == NBBY) {
166 		(void) fprintf(ABISTREAM, "'%c'", *(char *)addr);
167 		(void) fflush(ABISTREAM);
168 		return;
169 	}
170 
171 	switch (size) {
172 	case sizeof (uint8_t):
173 		(void) fprintf(ABISTREAM, "%#x", *(uint8_t *)addr);
174 		break;
175 	case sizeof (uint16_t):
176 		(void) fprintf(ABISTREAM, "%#x", *(uint16_t *)addr);
177 		break;
178 	case sizeof (uint32_t):
179 		(void) fprintf(ABISTREAM, "%#x", *(uint32_t *)addr);
180 		break;
181 	case sizeof (uint64_t):
182 		(void) fprintf(ABISTREAM, "%#llx",
183 		    (unsigned long long)*(uint64_t *)addr);
184 		break;
185 	}
186 	(void) fflush(ABISTREAM);
187 }
188 
189 /* ARGSUSED */
190 static void
print_float(ctf_id_t base,ulong_t off,printarg_t * pap)191 print_float(ctf_id_t base, ulong_t off, printarg_t *pap)
192 {
193 	ctf_file_t *ctfp = pap->pa_ctfp;
194 	ctf_encoding_t e;
195 
196 	union {
197 		float f;
198 		double d;
199 		long double ld;
200 	} u;
201 
202 	u.f = 0;
203 	if (ctf_type_encoding(ctfp, base, &e) == 0) {
204 		if (e.cte_format == CTF_FP_SINGLE &&
205 		    e.cte_bits == sizeof (float) * NBBY) {
206 			(void) fprintf(ABISTREAM, "%+.7e", u.f);
207 		} else if (e.cte_format == CTF_FP_DOUBLE &&
208 		    e.cte_bits == sizeof (double) * NBBY) {
209 			(void) fprintf(ABISTREAM, "%+.7e", u.d);
210 		} else if (e.cte_format == CTF_FP_LDOUBLE &&
211 		    e.cte_bits == sizeof (long double) * NBBY) {
212 			(void) fprintf(ABISTREAM,
213 			    "%+.16LE", u.ld);
214 		}
215 	}
216 	(void) fflush(ABISTREAM);
217 }
218 
219 /* ARGSUSED */
220 static void
print_ptr(ctf_id_t base,ulong_t off,printarg_t * pap)221 print_ptr(ctf_id_t base, ulong_t off, printarg_t *pap)
222 {
223 	ctf_file_t *ctfp = pap->pa_ctfp;
224 	ulong_t addr = pap->pa_addr + off / NBBY;
225 	ctf_encoding_t e;
226 
227 	if (ctf_type_kind(ctfp, base) != CTF_K_POINTER)
228 		return;
229 
230 	if ((base = ctf_type_reference(ctfp, base)) == CTF_ERR)
231 		return;
232 
233 	if ((base = ctf_type_resolve(ctfp, base)) == CTF_ERR)
234 		return;
235 
236 	if (ctf_type_encoding(ctfp, base, &e) != 0)
237 		return;
238 
239 	if (((e).cte_format & (CTF_INT_CHAR | CTF_INT_SIGNED)) ==
240 	    (CTF_INT_CHAR | CTF_INT_SIGNED) && (e).cte_bits == NBBY)
241 		(void) fprintf(ABISTREAM, "'%c'", *(char *)addr);
242 	(void) fflush(ABISTREAM);
243 }
244 
245 /* ARGSUSED */
246 static void
print_array(ctf_id_t base,ulong_t off,printarg_t * pap)247 print_array(ctf_id_t base, ulong_t off, printarg_t *pap)
248 {
249 	ulong_t addr = pap->pa_addr + off / NBBY;
250 
251 	(void) fprintf(ABISTREAM, "0x%p", (void *)addr);
252 	(void) fflush(ABISTREAM);
253 }
254 
255 /* ARGSUSED */
256 static void
print_sou(ctf_id_t base,ulong_t off,printarg_t * pap)257 print_sou(ctf_id_t base, ulong_t off, printarg_t *pap)
258 {
259 	(void) fprintf(ABISTREAM, "{");
260 }
261 
262 /* ARGSUSED */
263 static void
print_enum(ctf_id_t base,ulong_t off,printarg_t * pap)264 print_enum(ctf_id_t base, ulong_t off, printarg_t *pap)
265 {
266 	ctf_file_t *ctfp = pap->pa_ctfp;
267 	const char *ename;
268 	int value = 0;
269 
270 	if ((ename = ctf_enum_name(ctfp, base, value)) != NULL)
271 		(void) fprintf(ABISTREAM, "%s", ename);
272 	else
273 		(void) fprintf(ABISTREAM, "%d", value);
274 	(void) fflush(ABISTREAM);
275 }
276 
277 /* ARGSUSED */
278 static void
print_tag(ctf_id_t base,ulong_t off,printarg_t * pap)279 print_tag(ctf_id_t base, ulong_t off, printarg_t *pap)
280 {
281 	(void) fprintf(ABISTREAM, "; ");
282 }
283 
284 static printarg_f *const printfuncs[] = {
285 	print_int,	/* CTF_K_INTEGER */
286 	print_float,	/* CTF_K_FLOAT */
287 	print_ptr,	/* CTF_K_POINTER */
288 	print_array,	/* CTF_K_ARRAY */
289 	print_ptr,	/* CTF_K_FUNCTION */
290 	print_sou,	/* CTF_K_STRUCT */
291 	print_sou,	/* CTF_K_UNION */
292 	print_enum,	/* CTF_K_ENUM */
293 	print_tag	/* CTF_K_FORWARD */
294 };
295 
296 static int
elt_print(const char * name,ctf_id_t id,ulong_t off,int depth,void * data)297 elt_print(const char *name, ctf_id_t id, ulong_t off, int depth, void *data)
298 {
299 	char type[256];
300 	int kind, d;
301 	ctf_id_t base;
302 	printarg_t *pap = data;
303 	ctf_file_t *ctfp = pap->pa_ctfp;
304 
305 	for (d = pap->pa_depth - 1; d >= depth; d--) {
306 		(void) fprintf(ABISTREAM, "%*s}\n",
307 		    (depth + pap->pa_nest) * 4, "");
308 	}
309 
310 	if ((base = ctf_type_resolve(ctfp, id)) == CTF_ERR ||
311 	    (kind = ctf_type_kind(ctfp, base)) == CTF_ERR)
312 		return (-1);
313 
314 	if (ctf_type_name(ctfp, id, type, sizeof (type)) == NULL)
315 		(void) snprintf(type, sizeof (type), "<%ld>", id);
316 
317 	(void) fprintf(ABISTREAM, "%*s", (depth + pap->pa_nest) * 4, "");
318 	if (name[0] != '\0')
319 		(void) fprintf(ABISTREAM, "\t%s: ", name);
320 	(void) fprintf(ABISTREAM, "(%s) ", type);
321 
322 	printfuncs[kind - 1](base, off, pap);
323 	(void) fprintf(ABISTREAM, "\n");
324 
325 	(void) fflush(ABISTREAM);
326 	return (0);
327 }
328