Ejemplo n.º 1
0
int
ctf_add_enumerator(ctf_file_t *fp, ctf_id_t enid, const char *name, int value)
{
	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, enid);
	ctf_dmdef_t *dmd;

	uint_t kind, vlen, root;
	char *s;

	if (name == NULL)
		return (ctf_set_errno(fp, EINVAL));

	if (!(fp->ctf_flags & LCTF_RDWR))
		return (ctf_set_errno(fp, ECTF_RDONLY));

	if (dtd == NULL)
		return (ctf_set_errno(fp, ECTF_BADID));

	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);

	if (kind != CTF_K_ENUM)
		return (ctf_set_errno(fp, ECTF_NOTENUM));

	if (vlen == CTF_MAX_VLEN)
		return (ctf_set_errno(fp, ECTF_DTFULL));

	for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
	    dmd != NULL; dmd = ctf_list_next(dmd)) {
		if (strcmp(dmd->dmd_name, name) == 0)
			return (ctf_set_errno(fp, ECTF_DUPMEMBER));
	}

	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
		return (ctf_set_errno(fp, EAGAIN));

	if ((s = ctf_strdup(name)) == NULL) {
		ctf_free(dmd, sizeof (ctf_dmdef_t));
		return (ctf_set_errno(fp, EAGAIN));
	}

	dmd->dmd_name = s;
	dmd->dmd_type = CTF_ERR;
	dmd->dmd_offset = 0;
	dmd->dmd_value = value;

	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);

	fp->ctf_dtstrlen += strlen(s) + 1;
	fp->ctf_flags |= LCTF_DIRTY;

	return (0);
}
Ejemplo n.º 2
0
/*
 * Iterate over every root (user-visible) type in the given CTF container.
 * We pass the type ID of each type to the specified callback function.
 */
int
ctf_type_iter(ctf_file_t *fp, ctf_type_f *func, void *arg)
{
	ctf_id_t id, max = fp->ctf_typemax;
	int rc, child = (fp->ctf_flags & LCTF_CHILD);

	for (id = 1; id <= max; id++) {
		const ctf_type_t *tp = LCTF_INDEX_TO_TYPEPTR(fp, id);
		if (CTF_INFO_ISROOT(tp->ctt_info) &&
		    (rc = func(CTF_INDEX_TO_TYPE(id, child), arg)) != 0)
			return (rc);
	}

	return (0);
}
Ejemplo n.º 3
0
static ushort_t
get_root_v2(ushort_t info)
{
	return (CTF_INFO_ISROOT(info));
}
Ejemplo n.º 4
0
int
ctf_add_member(ctf_file_t *fp, ctf_id_t souid, const char *name, ctf_id_t type)
{
	ctf_dtdef_t *dtd = ctf_dtd_lookup(fp, souid);
	ctf_dmdef_t *dmd;

	ssize_t msize, malign, ssize;
	uint_t kind, vlen, root;
	char *s = NULL;

	if (!(fp->ctf_flags & LCTF_RDWR))
		return (ctf_set_errno(fp, ECTF_RDONLY));

	if (dtd == NULL)
		return (ctf_set_errno(fp, ECTF_BADID));

	kind = CTF_INFO_KIND(dtd->dtd_data.ctt_info);
	root = CTF_INFO_ISROOT(dtd->dtd_data.ctt_info);
	vlen = CTF_INFO_VLEN(dtd->dtd_data.ctt_info);

	if (kind != CTF_K_STRUCT && kind != CTF_K_UNION)
		return (ctf_set_errno(fp, ECTF_NOTSOU));

	if (vlen == CTF_MAX_VLEN)
		return (ctf_set_errno(fp, ECTF_DTFULL));

	if (name != NULL) {
		for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members);
		    dmd != NULL; dmd = ctf_list_next(dmd)) {
			if (dmd->dmd_name != NULL &&
			    strcmp(dmd->dmd_name, name) == 0)
				return (ctf_set_errno(fp, ECTF_DUPMEMBER));
		}
	}

	if ((msize = ctf_type_size(fp, type)) == CTF_ERR ||
	    (malign = ctf_type_align(fp, type)) == CTF_ERR)
		return (CTF_ERR); /* errno is set for us */

	if ((dmd = ctf_alloc(sizeof (ctf_dmdef_t))) == NULL)
		return (ctf_set_errno(fp, EAGAIN));

	if (name != NULL && (s = ctf_strdup(name)) == NULL) {
		ctf_free(dmd, sizeof (ctf_dmdef_t));
		return (ctf_set_errno(fp, EAGAIN));
	}

	dmd->dmd_name = s;
	dmd->dmd_type = type;
	dmd->dmd_value = -1;

	if (kind == CTF_K_STRUCT && vlen != 0) {
		ctf_dmdef_t *lmd = ctf_list_prev(&dtd->dtd_u.dtu_members);
		ctf_id_t ltype = ctf_type_resolve(fp, lmd->dmd_type);
		size_t off = lmd->dmd_offset;

		ctf_encoding_t linfo;
		ssize_t lsize;

		if (ctf_type_encoding(fp, ltype, &linfo) != CTF_ERR)
			off += linfo.cte_bits;
		else if ((lsize = ctf_type_size(fp, ltype)) != CTF_ERR)
			off += lsize * NBBY;

		/*
		 * Round up the offset of the end of the last member to the
		 * next byte boundary, convert 'off' to bytes, and then round
		 * it up again to the next multiple of the alignment required
		 * by the new member.  Finally, convert back to bits and store
		 * the result in dmd_offset.  Technically we could do more
		 * efficient packing if the new member is a bit-field, but
		 * we're the "compiler" and ANSI says we can do as we choose.
		 */
		off = roundup(off, NBBY) / NBBY;
		off = roundup(off, MAX(malign, 1));
		dmd->dmd_offset = off * NBBY;
		ssize = off + msize;
	} else {
		dmd->dmd_offset = 0;
		ssize = ctf_get_ctt_size(fp, &dtd->dtd_data, NULL, NULL);
		ssize = MAX(ssize, msize);
	}

	if (ssize > CTF_MAX_SIZE) {
		dtd->dtd_data.ctt_size = CTF_LSIZE_SENT;
		dtd->dtd_data.ctt_lsizehi = CTF_SIZE_TO_LSIZE_HI(ssize);
		dtd->dtd_data.ctt_lsizelo = CTF_SIZE_TO_LSIZE_LO(ssize);
	} else
		dtd->dtd_data.ctt_size = (ushort_t)ssize;

	dtd->dtd_data.ctt_info = CTF_TYPE_INFO(kind, root, vlen + 1);
	ctf_list_append(&dtd->dtd_u.dtu_members, dmd);

	if (s != NULL)
		fp->ctf_dtstrlen += strlen(s) + 1;

	ctf_ref_inc(fp, type);
	fp->ctf_flags |= LCTF_DIRTY;
	return (0);
}
Ejemplo n.º 5
0
Archivo: ctf.c Proyecto: 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);
}
Ejemplo n.º 6
0
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);
}