예제 #1
0
파일: ctf_types.c 프로젝트: andreiw/polaris
/*
 * Return the encoding for the specified INTEGER or FLOAT.
 */
int
ctf_type_encoding(ctf_file_t *fp, ctf_id_t type, ctf_encoding_t *ep)
{
	ctf_file_t *ofp = fp;
	const ctf_type_t *tp;
	ssize_t increment;
	uint_t data;

	if ((tp = ctf_lookup_by_id(&fp, type)) == NULL)
		return (CTF_ERR); /* errno is set for us */

	(void) ctf_get_ctt_size(fp, tp, NULL, &increment);

	switch (LCTF_INFO_KIND(fp, tp->ctt_info)) {
	case CTF_K_INTEGER:
		data = *(const uint_t *)((uintptr_t)tp + increment);
		ep->cte_format = CTF_INT_ENCODING(data);
		ep->cte_offset = CTF_INT_OFFSET(data);
		ep->cte_bits = CTF_INT_BITS(data);
		break;
	case CTF_K_FLOAT:
		data = *(const uint_t *)((uintptr_t)tp + increment);
		ep->cte_format = CTF_FP_ENCODING(data);
		ep->cte_offset = CTF_FP_OFFSET(data);
		ep->cte_bits = CTF_FP_BITS(data);
		break;
	default:
		return (ctf_set_errno(ofp, ECTF_NOTINTFP));
	}

	return (0);
}
예제 #2
0
파일: ctf.c 프로젝트: DataIX/src
static void
resurrect_types(ctf_header_t *h, tdata_t *td, tdesc_t **tdarr, int tdsize,
    caddr_t ctfdata, int maxid)
{
	caddr_t buf = ctfdata + h->cth_typeoff;
	size_t bufsz = h->cth_stroff - h->cth_typeoff;
	caddr_t sbuf = ctfdata + h->cth_stroff;
	caddr_t dptr = buf;
	tdesc_t *tdp;
	uint_t data;
	uint_t encoding;
	size_t size, increment;
	int tcnt;
	int iicnt = 0;
	tid_t tid, argid;
	int kind, vlen;
	int i;

	elist_t **epp;
	mlist_t **mpp;
	intr_t *ip;

	ctf_type_t *ctt;
	ctf_array_t *cta;
	ctf_enum_t *cte;

	/*
	 * A maxid of zero indicates a request to resurrect all types, so reset
	 * maxid to the maximum type id.
	 */
	if (maxid == 0)
		maxid = CTF_MAX_TYPE;

	for (dptr = buf, tcnt = 0, tid = 1; dptr < buf + bufsz; tcnt++, tid++) {
		if (tid > maxid)
			break;

		if (tid >= tdsize)
			parseterminate("Reference to invalid type %d", tid);

		void *v = (void *) dptr;
		ctt = v;

		get_ctt_size(ctt, &size, &increment);
		dptr += increment;

		tdp = tdarr[tid];

		if (CTF_NAME_STID(ctt->ctt_name) != CTF_STRTAB_0)
			parseterminate(
			    "Unable to cope with non-zero strtab id");
		if (CTF_NAME_OFFSET(ctt->ctt_name) != 0) {
			tdp->t_name =
			    xstrdup(sbuf + CTF_NAME_OFFSET(ctt->ctt_name));
		} else
			tdp->t_name = NULL;

		kind = CTF_INFO_KIND(ctt->ctt_info);
		vlen = CTF_INFO_VLEN(ctt->ctt_info);

		switch (kind) {
		case CTF_K_INTEGER:
			tdp->t_type = INTRINSIC;
			tdp->t_size = size;

			v = (void *) dptr;
			data = *((uint_t *)v);
			dptr += sizeof (uint_t);
			encoding = CTF_INT_ENCODING(data);

			ip = xmalloc(sizeof (intr_t));
			ip->intr_type = INTR_INT;
			ip->intr_signed = (encoding & CTF_INT_SIGNED) ? 1 : 0;

			if (encoding & CTF_INT_CHAR)
				ip->intr_iformat = 'c';
			else if (encoding & CTF_INT_BOOL)
				ip->intr_iformat = 'b';
			else if (encoding & CTF_INT_VARARGS)
				ip->intr_iformat = 'v';
			else
				ip->intr_iformat = '\0';

			ip->intr_offset = CTF_INT_OFFSET(data);
			ip->intr_nbits = CTF_INT_BITS(data);
			tdp->t_intr = ip;
			break;

		case CTF_K_FLOAT:
			tdp->t_type = INTRINSIC;
			tdp->t_size = size;

			v = (void *) dptr;
			data = *((uint_t *)v);
			dptr += sizeof (uint_t);

			ip = xcalloc(sizeof (intr_t));
			ip->intr_type = INTR_REAL;
			ip->intr_fformat = CTF_FP_ENCODING(data);
			ip->intr_offset = CTF_FP_OFFSET(data);
			ip->intr_nbits = CTF_FP_BITS(data);
			tdp->t_intr = ip;
			break;

		case CTF_K_POINTER:
			tdp->t_type = POINTER;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_ARRAY:
			tdp->t_type = ARRAY;
			tdp->t_size = size;

			v = (void *) dptr;
			cta = v;
			dptr += sizeof (ctf_array_t);

			tdp->t_ardef = xmalloc(sizeof (ardef_t));
			tdp->t_ardef->ad_contents = tdarr[cta->cta_contents];
			tdp->t_ardef->ad_idxtype = tdarr[cta->cta_index];
			tdp->t_ardef->ad_nelems = cta->cta_nelems;
			break;

		case CTF_K_STRUCT:
		case CTF_K_UNION:
			tdp->t_type = (kind == CTF_K_STRUCT ? STRUCT : UNION);
			tdp->t_size = size;

			if (size < CTF_LSTRUCT_THRESH) {
				for (i = 0, mpp = &tdp->t_members; i < vlen;
				    i++, mpp = &((*mpp)->ml_next)) {
					v = (void *) dptr;
					ctf_member_t *ctm = v;
					dptr += sizeof (ctf_member_t);

					*mpp = xmalloc(sizeof (mlist_t));
					(*mpp)->ml_name = xstrdup(sbuf +
					    ctm->ctm_name);
					(*mpp)->ml_type = tdarr[ctm->ctm_type];
					(*mpp)->ml_offset = ctm->ctm_offset;
					(*mpp)->ml_size = 0;
					if (ctm->ctm_type > ntypes) {
						parseterminate("Invalid member type ctm_type=%d",
						    ctm->ctm_type);
					}
				}
			} else {
				for (i = 0, mpp = &tdp->t_members; i < vlen;
				    i++, mpp = &((*mpp)->ml_next)) {
					v = (void *) dptr;
					ctf_lmember_t *ctlm = v;
					dptr += sizeof (ctf_lmember_t);

					*mpp = xmalloc(sizeof (mlist_t));
					(*mpp)->ml_name = xstrdup(sbuf +
					    ctlm->ctlm_name);
					(*mpp)->ml_type =
					    tdarr[ctlm->ctlm_type];
					(*mpp)->ml_offset =
					    (int)CTF_LMEM_OFFSET(ctlm);
					(*mpp)->ml_size = 0;
					if (ctlm->ctlm_type > ntypes) {
						parseterminate("Invalid lmember type ctlm_type=%d",
						    ctlm->ctlm_type);
					}
				}
			}

			*mpp = NULL;
			break;

		case CTF_K_ENUM:
			tdp->t_type = ENUM;
			tdp->t_size = size;

			for (i = 0, epp = &tdp->t_emem; i < vlen;
			    i++, epp = &((*epp)->el_next)) {
				v = (void *) dptr;
				cte = v;
				dptr += sizeof (ctf_enum_t);

				*epp = xmalloc(sizeof (elist_t));
				(*epp)->el_name = xstrdup(sbuf + cte->cte_name);
				(*epp)->el_number = cte->cte_value;
			}
			*epp = NULL;
			break;

		case CTF_K_FORWARD:
			tdp->t_type = FORWARD;
			list_add(&td->td_fwdlist, tdp);
			break;

		case CTF_K_TYPEDEF:
			tdp->t_type = TYPEDEF;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_VOLATILE:
			tdp->t_type = VOLATILE;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_CONST:
			tdp->t_type = CONST;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_FUNCTION:
			tdp->t_type = FUNCTION;
			tdp->t_fndef = xcalloc(sizeof (fndef_t));
			tdp->t_fndef->fn_ret = tdarr[ctt->ctt_type];

			v = (void *) (dptr + (sizeof (ushort_t) * (vlen - 1)));
			if (vlen > 0 && *(ushort_t *)v == 0)
				tdp->t_fndef->fn_vargs = 1;

			tdp->t_fndef->fn_nargs = vlen - tdp->t_fndef->fn_vargs;
			tdp->t_fndef->fn_args = xcalloc(sizeof (tdesc_t) *
			    vlen - tdp->t_fndef->fn_vargs);

			for (i = 0; i < vlen; i++) {
				v = (void *) dptr;
				argid = *(ushort_t *)v;
				dptr += sizeof (ushort_t);

				if (argid != 0)
					tdp->t_fndef->fn_args[i] = tdarr[argid];
			}

			if (vlen & 1)
				dptr += sizeof (ushort_t);
			break;

		case CTF_K_RESTRICT:
			tdp->t_type = RESTRICT;
			tdp->t_tdesc = tdarr[ctt->ctt_type];
			break;

		case CTF_K_UNKNOWN:
			break;

		default:
			warning("Can't parse unknown CTF type %d\n", kind);
		}

		if (CTF_INFO_ISROOT(ctt->ctt_info)) {
			iidesc_t *ii = iidesc_new(tdp->t_name);
			if (tdp->t_type == STRUCT || tdp->t_type == UNION ||
			    tdp->t_type == ENUM)
				ii->ii_type = II_SOU;
			else
				ii->ii_type = II_TYPE;
			ii->ii_dtype = tdp;
			hash_add(td->td_iihash, ii);

			iicnt++;
		}

		debug(3, "Resurrected %d %stype %s (%d)\n", tdp->t_type,
		    (CTF_INFO_ISROOT(ctt->ctt_info) ? "root " : ""),
		    tdesc_name(tdp), tdp->t_id);
	}

	debug(3, "Resurrected %d types (%d were roots)\n", tcnt, iicnt);
}
예제 #3
0
파일: dump.c 프로젝트: andreiw/polaris
static int
read_types(const ctf_header_t *hp, const ctf_data_t *cd)
{
	/* LINTED - pointer alignment */
	const ctf_type_t *tp = (ctf_type_t *)(cd->cd_ctfdata + hp->cth_typeoff);

	/* LINTED - pointer alignment */
	const ctf_type_t *end = (ctf_type_t *)(cd->cd_ctfdata + hp->cth_stroff);

	ulong_t id;

	if (flags != F_STATS)
		print_line("- Types ");

	if (hp->cth_typeoff & 3)
		WARN("cth_typeoff is not aligned properly\n");
	if (hp->cth_typeoff >= cd->cd_ctflen)
		WARN("file is truncated or cth_typeoff is corrupt\n");
	if (hp->cth_stroff >= cd->cd_ctflen)
		WARN("file is truncated or cth_stroff is corrupt\n");
	if (hp->cth_typeoff > hp->cth_stroff)
		WARN("file is corrupt -- cth_typeoff > cth_stroff\n");

	id = 1;
	if (hp->cth_parlabel || hp->cth_parname)
		id += 1 << CTF_PARENT_SHIFT;

	for (/* */; tp < end; id++) {
		ulong_t i, n = CTF_INFO_VLEN(tp->ctt_info);
		size_t size, increment, vlen = 0;
		int kind = CTF_INFO_KIND(tp->ctt_info);

		union {
			const void *ptr;
			const ctf_array_t *ap;
			const ctf_member_t *mp;
			const ctf_lmember_t *lmp;
			const ctf_enum_t *ep;
			const ushort_t *argp;
		} u;

		if (flags != F_STATS) {
			(void) printf("  %c%lu%c ",
			    "[<"[CTF_INFO_ISROOT(tp->ctt_info)], id,
			    "]>"[CTF_INFO_ISROOT(tp->ctt_info)]);
		}

		if (tp->ctt_size == CTF_LSIZE_SENT) {
			increment = sizeof (ctf_type_t);
			size = (size_t)CTF_TYPE_LSIZE(tp);
		} else {
			increment = sizeof (ctf_stype_t);
			size = tp->ctt_size;
		}
		u.ptr = (caddr_t)tp + increment;

		switch (kind) {
		case CTF_K_INTEGER:
			if (flags != F_STATS) {
				uint_t encoding = *((const uint_t *)u.ptr);

				(void) printf("INTEGER %s encoding=%s offset=%u"
				    " bits=%u", ref_to_str(tp->ctt_name, hp,
				    cd), int_encoding_to_str(
				    CTF_INT_ENCODING(encoding)),
				    CTF_INT_OFFSET(encoding),
				    CTF_INT_BITS(encoding));
			}
			vlen = sizeof (uint_t);
			break;

		case CTF_K_FLOAT:
			if (flags != F_STATS) {
				uint_t encoding = *((const uint_t *)u.ptr);

				(void) printf("FLOAT %s encoding=%s offset=%u "
				    "bits=%u", ref_to_str(tp->ctt_name, hp,
				    cd), fp_encoding_to_str(
				    CTF_FP_ENCODING(encoding)),
				    CTF_FP_OFFSET(encoding),
				    CTF_FP_BITS(encoding));
			}
			vlen = sizeof (uint_t);
			break;

		case CTF_K_POINTER:
			if (flags != F_STATS) {
				(void) printf("POINTER %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_ARRAY:
			if (flags != F_STATS) {
				(void) printf("ARRAY %s content: %u index: %u "
				    "nelems: %u\n", ref_to_str(tp->ctt_name,
				    hp, cd), u.ap->cta_contents,
				    u.ap->cta_index, u.ap->cta_nelems);
			}
			vlen = sizeof (ctf_array_t);
			break;

		case CTF_K_FUNCTION:
			if (flags != F_STATS) {
				(void) printf("FUNCTION %s returns: %u args: (",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);

				if (n != 0) {
					(void) printf("%u", *u.argp++);
					for (i = 1; i < n; i++, u.argp++)
						(void) printf(", %u", *u.argp);
				}

				(void) printf(")");
			}

			vlen = sizeof (ushort_t) * (n + (n & 1));
			break;

		case CTF_K_STRUCT:
		case CTF_K_UNION:
			if (kind == CTF_K_STRUCT) {
				stats.s_nsmem += n;
				stats.s_smmax = MAX(stats.s_smmax, n);
				stats.s_nsbytes += size;
				stats.s_sbmax = MAX(stats.s_sbmax, size);

				if (flags != F_STATS)
					(void) printf("STRUCT");
			} else {
				stats.s_numem += n;
				stats.s_ummax = MAX(stats.s_ummax, n);
				stats.s_nubytes += size;
				stats.s_ubmax = MAX(stats.s_ubmax, size);

				if (flags != F_STATS)
					(void) printf("UNION");
			}

			if (flags != F_STATS) {
				(void) printf(" %s (%d bytes)\n",
				    ref_to_str(tp->ctt_name, hp, cd), size);

				if (size >= CTF_LSTRUCT_THRESH) {
					for (i = 0; i < n; i++, u.lmp++) {
						(void) printf(
						    "\t%s type=%u off=%llu\n",
						    ref_to_str(u.lmp->ctlm_name,
						    hp, cd), u.lmp->ctlm_type,
						    CTF_LMEM_OFFSET(u.lmp));
					}
				} else {
					for (i = 0; i < n; i++, u.mp++) {
						(void) printf(
						    "\t%s type=%u off=%u\n",
						    ref_to_str(u.mp->ctm_name,
						    hp, cd), u.mp->ctm_type,
						    u.mp->ctm_offset);
					}
				}
			}

			vlen = n * (size >= CTF_LSTRUCT_THRESH ?
			    sizeof (ctf_lmember_t) : sizeof (ctf_member_t));
			break;

		case CTF_K_ENUM:
			if (flags != F_STATS) {
				(void) printf("ENUM %s\n",
				    ref_to_str(tp->ctt_name, hp, cd));

				for (i = 0; i < n; i++, u.ep++) {
					(void) printf("\t%s = %d\n",
					    ref_to_str(u.ep->cte_name, hp, cd),
					    u.ep->cte_value);
				}
			}

			stats.s_nemem += n;
			stats.s_emmax = MAX(stats.s_emmax, n);

			vlen = sizeof (ctf_enum_t) * n;
			break;

		case CTF_K_FORWARD:
			if (flags != F_STATS) {
				(void) printf("FORWARD %s",
				    ref_to_str(tp->ctt_name, hp, cd));
			}
			break;

		case CTF_K_TYPEDEF:
			if (flags != F_STATS) {
				(void) printf("TYPEDEF %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_VOLATILE:
			if (flags != F_STATS) {
				(void) printf("VOLATILE %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_CONST:
			if (flags != F_STATS) {
				(void) printf("CONST %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_RESTRICT:
			if (flags != F_STATS) {
				(void) printf("RESTRICT %s refers to %u",
				    ref_to_str(tp->ctt_name, hp, cd),
				    tp->ctt_type);
			}
			break;

		case CTF_K_UNKNOWN:
			break; /* hole in type id space */

		default:
			(void) printf("unexpected kind %u\n", kind);
			return (E_ERROR);
		}

		if (flags != F_STATS)
			(void) printf("\n");

		stats.s_ntypes++;
		stats.s_types[kind]++;

		tp = (ctf_type_t *)((uintptr_t)tp + increment + vlen);
	}

	return (E_SUCCESS);
}