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