xref: /onnv-gate/usr/src/common/ctf/ctf_labels.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 2002-2003 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 <ctf_impl.h>
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate static int
extract_label_info(ctf_file_t * fp,const ctf_lblent_t ** ctl,uint_t * num_labels)32*0Sstevel@tonic-gate extract_label_info(ctf_file_t *fp, const ctf_lblent_t **ctl, uint_t *num_labels)
33*0Sstevel@tonic-gate {
34*0Sstevel@tonic-gate 	const ctf_header_t *h;
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate 	/*
37*0Sstevel@tonic-gate 	 * Labels are only supported in V2 or later
38*0Sstevel@tonic-gate 	 */
39*0Sstevel@tonic-gate 	if (fp->ctf_version < CTF_VERSION_2)
40*0Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOTSUP));
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate 	h = (const ctf_header_t *)fp->ctf_data.cts_data;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	/* LINTED - pointer alignment */
45*0Sstevel@tonic-gate 	*ctl = (const ctf_lblent_t *)(fp->ctf_buf + h->cth_lbloff);
46*0Sstevel@tonic-gate 	*num_labels = (h->cth_objtoff - h->cth_lbloff) / sizeof (ctf_lblent_t);
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 	return (0);
49*0Sstevel@tonic-gate }
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate  * Returns the topmost label, or NULL if any errors are encountered
53*0Sstevel@tonic-gate  */
54*0Sstevel@tonic-gate const char *
ctf_label_topmost(ctf_file_t * fp)55*0Sstevel@tonic-gate ctf_label_topmost(ctf_file_t *fp)
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	const ctf_lblent_t *ctlp;
58*0Sstevel@tonic-gate 	const char *s;
59*0Sstevel@tonic-gate 	uint_t num_labels;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	if (extract_label_info(fp, &ctlp, &num_labels) == CTF_ERR)
62*0Sstevel@tonic-gate 		return (NULL); /* errno is set */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	if (num_labels == 0) {
65*0Sstevel@tonic-gate 		(void) ctf_set_errno(fp, ECTF_NOLABELDATA);
66*0Sstevel@tonic-gate 		return (NULL);
67*0Sstevel@tonic-gate 	}
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	if ((s = ctf_strraw(fp, (ctlp + num_labels - 1)->ctl_label)) == NULL)
70*0Sstevel@tonic-gate 		(void) ctf_set_errno(fp, ECTF_CORRUPT);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	return (s);
73*0Sstevel@tonic-gate }
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * Iterate over all labels.  We pass the label string and the lblinfo_t struct
77*0Sstevel@tonic-gate  * to the specified callback function.
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate int
ctf_label_iter(ctf_file_t * fp,ctf_label_f * func,void * arg)80*0Sstevel@tonic-gate ctf_label_iter(ctf_file_t *fp, ctf_label_f *func, void *arg)
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	const ctf_lblent_t *ctlp;
83*0Sstevel@tonic-gate 	uint_t i, num_labels;
84*0Sstevel@tonic-gate 	ctf_lblinfo_t linfo;
85*0Sstevel@tonic-gate 	const char *lname;
86*0Sstevel@tonic-gate 	int rc;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	if (extract_label_info(fp, &ctlp, &num_labels) == CTF_ERR)
89*0Sstevel@tonic-gate 		return (CTF_ERR); /* errno is set */
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if (num_labels == 0)
92*0Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOLABELDATA));
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	for (i = 0; i < num_labels; i++, ctlp++) {
95*0Sstevel@tonic-gate 		if ((lname = ctf_strraw(fp, ctlp->ctl_label)) == NULL) {
96*0Sstevel@tonic-gate 			ctf_dprintf("failed to decode label %u with "
97*0Sstevel@tonic-gate 			    "typeidx %u\n", ctlp->ctl_label, ctlp->ctl_typeidx);
98*0Sstevel@tonic-gate 			return (ctf_set_errno(fp, ECTF_CORRUPT));
99*0Sstevel@tonic-gate 		}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		linfo.ctb_typeidx = ctlp->ctl_typeidx;
102*0Sstevel@tonic-gate 		if ((rc = func(lname, &linfo, arg)) != 0)
103*0Sstevel@tonic-gate 			return (rc);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 	return (0);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate typedef struct linfo_cb_arg {
110*0Sstevel@tonic-gate 	const char *lca_name;    /* Label we want to retrieve info for */
111*0Sstevel@tonic-gate 	ctf_lblinfo_t *lca_info; /* Where to store the info about the label */
112*0Sstevel@tonic-gate } linfo_cb_arg_t;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate static int
label_info_cb(const char * lname,const ctf_lblinfo_t * linfo,void * arg)115*0Sstevel@tonic-gate label_info_cb(const char *lname, const ctf_lblinfo_t *linfo, void *arg)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	/*
118*0Sstevel@tonic-gate 	 * If lname matches the label we are looking for, copy the
119*0Sstevel@tonic-gate 	 * lblinfo_t struct for the caller.
120*0Sstevel@tonic-gate 	 */
121*0Sstevel@tonic-gate 	if (strcmp(lname, ((linfo_cb_arg_t *)arg)->lca_name) == 0) {
122*0Sstevel@tonic-gate 		/*
123*0Sstevel@tonic-gate 		 * Allow caller not to allocate storage to test if label exists
124*0Sstevel@tonic-gate 		 */
125*0Sstevel@tonic-gate 		if (((linfo_cb_arg_t *)arg)->lca_info != NULL)
126*0Sstevel@tonic-gate 			bcopy(linfo, ((linfo_cb_arg_t *)arg)->lca_info,
127*0Sstevel@tonic-gate 			    sizeof (ctf_lblinfo_t));
128*0Sstevel@tonic-gate 		return (1); /* Indicate we found a match */
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	return (0);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /*
135*0Sstevel@tonic-gate  * Retrieve information about the label with name "lname"
136*0Sstevel@tonic-gate  */
137*0Sstevel@tonic-gate int
ctf_label_info(ctf_file_t * fp,const char * lname,ctf_lblinfo_t * linfo)138*0Sstevel@tonic-gate ctf_label_info(ctf_file_t *fp, const char *lname, ctf_lblinfo_t *linfo)
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate 	linfo_cb_arg_t cb_arg;
141*0Sstevel@tonic-gate 	int rc;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	cb_arg.lca_name = lname;
144*0Sstevel@tonic-gate 	cb_arg.lca_info = linfo;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if ((rc = ctf_label_iter(fp, label_info_cb, &cb_arg)) == CTF_ERR)
147*0Sstevel@tonic-gate 		return (rc);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	if (rc != 1)
150*0Sstevel@tonic-gate 		return (ctf_set_errno(fp, ECTF_NOLABEL));
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	return (0);
153*0Sstevel@tonic-gate }
154