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