xref: /freebsd-src/cddl/contrib/opensolaris/common/ctf/ctf_labels.c (revision d876124d6ae9d56da5b4ff4c6015efd1d0c9222a)
1*d876124dSJohn Birrell /*
2*d876124dSJohn Birrell  * CDDL HEADER START
3*d876124dSJohn Birrell  *
4*d876124dSJohn Birrell  * The contents of this file are subject to the terms of the
5*d876124dSJohn Birrell  * Common Development and Distribution License, Version 1.0 only
6*d876124dSJohn Birrell  * (the "License").  You may not use this file except in compliance
7*d876124dSJohn Birrell  * with the License.
8*d876124dSJohn Birrell  *
9*d876124dSJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*d876124dSJohn Birrell  * or http://www.opensolaris.org/os/licensing.
11*d876124dSJohn Birrell  * See the License for the specific language governing permissions
12*d876124dSJohn Birrell  * and limitations under the License.
13*d876124dSJohn Birrell  *
14*d876124dSJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
15*d876124dSJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*d876124dSJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
17*d876124dSJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
18*d876124dSJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
19*d876124dSJohn Birrell  *
20*d876124dSJohn Birrell  * CDDL HEADER END
21*d876124dSJohn Birrell  */
22*d876124dSJohn Birrell /*
23*d876124dSJohn Birrell  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24*d876124dSJohn Birrell  * Use is subject to license terms.
25*d876124dSJohn Birrell  */
26*d876124dSJohn Birrell 
27*d876124dSJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*d876124dSJohn Birrell 
29*d876124dSJohn Birrell #include <ctf_impl.h>
30*d876124dSJohn Birrell 
31*d876124dSJohn Birrell static int
extract_label_info(ctf_file_t * fp,const ctf_lblent_t ** ctl,uint_t * num_labels)32*d876124dSJohn Birrell extract_label_info(ctf_file_t *fp, const ctf_lblent_t **ctl, uint_t *num_labels)
33*d876124dSJohn Birrell {
34*d876124dSJohn Birrell 	const ctf_header_t *h;
35*d876124dSJohn Birrell 
36*d876124dSJohn Birrell 	/*
37*d876124dSJohn Birrell 	 * Labels are only supported in V2 or later
38*d876124dSJohn Birrell 	 */
39*d876124dSJohn Birrell 	if (fp->ctf_version < CTF_VERSION_2)
40*d876124dSJohn Birrell 		return (ctf_set_errno(fp, ECTF_NOTSUP));
41*d876124dSJohn Birrell 
42*d876124dSJohn Birrell 	h = (const ctf_header_t *)fp->ctf_data.cts_data;
43*d876124dSJohn Birrell 
44*d876124dSJohn Birrell 	/* LINTED - pointer alignment */
45*d876124dSJohn Birrell 	*ctl = (const ctf_lblent_t *)(fp->ctf_buf + h->cth_lbloff);
46*d876124dSJohn Birrell 	*num_labels = (h->cth_objtoff - h->cth_lbloff) / sizeof (ctf_lblent_t);
47*d876124dSJohn Birrell 
48*d876124dSJohn Birrell 	return (0);
49*d876124dSJohn Birrell }
50*d876124dSJohn Birrell 
51*d876124dSJohn Birrell /*
52*d876124dSJohn Birrell  * Returns the topmost label, or NULL if any errors are encountered
53*d876124dSJohn Birrell  */
54*d876124dSJohn Birrell const char *
ctf_label_topmost(ctf_file_t * fp)55*d876124dSJohn Birrell ctf_label_topmost(ctf_file_t *fp)
56*d876124dSJohn Birrell {
57*d876124dSJohn Birrell 	const ctf_lblent_t *ctlp;
58*d876124dSJohn Birrell 	const char *s;
59*d876124dSJohn Birrell 	uint_t num_labels;
60*d876124dSJohn Birrell 
61*d876124dSJohn Birrell 	if (extract_label_info(fp, &ctlp, &num_labels) == CTF_ERR)
62*d876124dSJohn Birrell 		return (NULL); /* errno is set */
63*d876124dSJohn Birrell 
64*d876124dSJohn Birrell 	if (num_labels == 0) {
65*d876124dSJohn Birrell 		(void) ctf_set_errno(fp, ECTF_NOLABELDATA);
66*d876124dSJohn Birrell 		return (NULL);
67*d876124dSJohn Birrell 	}
68*d876124dSJohn Birrell 
69*d876124dSJohn Birrell 	if ((s = ctf_strraw(fp, (ctlp + num_labels - 1)->ctl_label)) == NULL)
70*d876124dSJohn Birrell 		(void) ctf_set_errno(fp, ECTF_CORRUPT);
71*d876124dSJohn Birrell 
72*d876124dSJohn Birrell 	return (s);
73*d876124dSJohn Birrell }
74*d876124dSJohn Birrell 
75*d876124dSJohn Birrell /*
76*d876124dSJohn Birrell  * Iterate over all labels.  We pass the label string and the lblinfo_t struct
77*d876124dSJohn Birrell  * to the specified callback function.
78*d876124dSJohn Birrell  */
79*d876124dSJohn Birrell int
ctf_label_iter(ctf_file_t * fp,ctf_label_f * func,void * arg)80*d876124dSJohn Birrell ctf_label_iter(ctf_file_t *fp, ctf_label_f *func, void *arg)
81*d876124dSJohn Birrell {
82*d876124dSJohn Birrell 	const ctf_lblent_t *ctlp;
83*d876124dSJohn Birrell 	uint_t i, num_labels;
84*d876124dSJohn Birrell 	ctf_lblinfo_t linfo;
85*d876124dSJohn Birrell 	const char *lname;
86*d876124dSJohn Birrell 	int rc;
87*d876124dSJohn Birrell 
88*d876124dSJohn Birrell 	if (extract_label_info(fp, &ctlp, &num_labels) == CTF_ERR)
89*d876124dSJohn Birrell 		return (CTF_ERR); /* errno is set */
90*d876124dSJohn Birrell 
91*d876124dSJohn Birrell 	if (num_labels == 0)
92*d876124dSJohn Birrell 		return (ctf_set_errno(fp, ECTF_NOLABELDATA));
93*d876124dSJohn Birrell 
94*d876124dSJohn Birrell 	for (i = 0; i < num_labels; i++, ctlp++) {
95*d876124dSJohn Birrell 		if ((lname = ctf_strraw(fp, ctlp->ctl_label)) == NULL) {
96*d876124dSJohn Birrell 			ctf_dprintf("failed to decode label %u with "
97*d876124dSJohn Birrell 			    "typeidx %u\n", ctlp->ctl_label, ctlp->ctl_typeidx);
98*d876124dSJohn Birrell 			return (ctf_set_errno(fp, ECTF_CORRUPT));
99*d876124dSJohn Birrell 		}
100*d876124dSJohn Birrell 
101*d876124dSJohn Birrell 		linfo.ctb_typeidx = ctlp->ctl_typeidx;
102*d876124dSJohn Birrell 		if ((rc = func(lname, &linfo, arg)) != 0)
103*d876124dSJohn Birrell 			return (rc);
104*d876124dSJohn Birrell 	}
105*d876124dSJohn Birrell 
106*d876124dSJohn Birrell 	return (0);
107*d876124dSJohn Birrell }
108*d876124dSJohn Birrell 
109*d876124dSJohn Birrell typedef struct linfo_cb_arg {
110*d876124dSJohn Birrell 	const char *lca_name;    /* Label we want to retrieve info for */
111*d876124dSJohn Birrell 	ctf_lblinfo_t *lca_info; /* Where to store the info about the label */
112*d876124dSJohn Birrell } linfo_cb_arg_t;
113*d876124dSJohn Birrell 
114*d876124dSJohn Birrell static int
label_info_cb(const char * lname,const ctf_lblinfo_t * linfo,void * arg)115*d876124dSJohn Birrell label_info_cb(const char *lname, const ctf_lblinfo_t *linfo, void *arg)
116*d876124dSJohn Birrell {
117*d876124dSJohn Birrell 	/*
118*d876124dSJohn Birrell 	 * If lname matches the label we are looking for, copy the
119*d876124dSJohn Birrell 	 * lblinfo_t struct for the caller.
120*d876124dSJohn Birrell 	 */
121*d876124dSJohn Birrell 	if (strcmp(lname, ((linfo_cb_arg_t *)arg)->lca_name) == 0) {
122*d876124dSJohn Birrell 		/*
123*d876124dSJohn Birrell 		 * Allow caller not to allocate storage to test if label exists
124*d876124dSJohn Birrell 		 */
125*d876124dSJohn Birrell 		if (((linfo_cb_arg_t *)arg)->lca_info != NULL)
126*d876124dSJohn Birrell 			bcopy(linfo, ((linfo_cb_arg_t *)arg)->lca_info,
127*d876124dSJohn Birrell 			    sizeof (ctf_lblinfo_t));
128*d876124dSJohn Birrell 		return (1); /* Indicate we found a match */
129*d876124dSJohn Birrell 	}
130*d876124dSJohn Birrell 
131*d876124dSJohn Birrell 	return (0);
132*d876124dSJohn Birrell }
133*d876124dSJohn Birrell 
134*d876124dSJohn Birrell /*
135*d876124dSJohn Birrell  * Retrieve information about the label with name "lname"
136*d876124dSJohn Birrell  */
137*d876124dSJohn Birrell int
ctf_label_info(ctf_file_t * fp,const char * lname,ctf_lblinfo_t * linfo)138*d876124dSJohn Birrell ctf_label_info(ctf_file_t *fp, const char *lname, ctf_lblinfo_t *linfo)
139*d876124dSJohn Birrell {
140*d876124dSJohn Birrell 	linfo_cb_arg_t cb_arg;
141*d876124dSJohn Birrell 	int rc;
142*d876124dSJohn Birrell 
143*d876124dSJohn Birrell 	cb_arg.lca_name = lname;
144*d876124dSJohn Birrell 	cb_arg.lca_info = linfo;
145*d876124dSJohn Birrell 
146*d876124dSJohn Birrell 	if ((rc = ctf_label_iter(fp, label_info_cb, &cb_arg)) == CTF_ERR)
147*d876124dSJohn Birrell 		return (rc);
148*d876124dSJohn Birrell 
149*d876124dSJohn Birrell 	if (rc != 1)
150*d876124dSJohn Birrell 		return (ctf_set_errno(fp, ECTF_NOLABEL));
151*d876124dSJohn Birrell 
152*d876124dSJohn Birrell 	return (0);
153*d876124dSJohn Birrell }
154