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