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