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