1285SN/A/*
2462SN/A * CDDL HEADER START
3285SN/A *
4285SN/A * The contents of this file are subject to the terms of the
5285SN/A * Common Development and Distribution License, Version 1.0 only
6285SN/A * (the "License").  You may not use this file except in compliance
7285SN/A * with the License.
8285SN/A *
9285SN/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10285SN/A * or http://www.opensolaris.org/os/licensing.
11285SN/A * See the License for the specific language governing permissions
12285SN/A * and limitations under the License.
13285SN/A *
14285SN/A * When distributing Covered Code, include this CDDL HEADER in each
15285SN/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16285SN/A * If applicable, add the following below this CDDL HEADER, with the
17285SN/A * fields enclosed by brackets "[]" replaced with your own identifying
18285SN/A * information: Portions Copyright [yyyy] [name of copyright owner]
19285SN/A *
20285SN/A * CDDL HEADER END
21285SN/A */
22285SN/A/*
23285SN/A * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24285SN/A * Use is subject to license terms.
25285SN/A */
26285SN/A
27285SN/A#pragma ident	"%Z%%M%	%I%	%E% SMI"
28285SN/A
29285SN/A#include <ctf_impl.h>
30285SN/A
31285SN/Astatic int
32285SN/Aextract_label_info(ctf_file_t *fp, const ctf_lblent_t **ctl, uint_t *num_labels)
33285SN/A{
34285SN/A	const ctf_header_t *h;
35285SN/A
36285SN/A	/*
37285SN/A	 * Labels are only supported in V2 or later
38285SN/A	 */
39285SN/A	if (fp->ctf_version < CTF_VERSION_2)
40285SN/A		return (ctf_set_errno(fp, ECTF_NOTSUP));
41285SN/A
42285SN/A	h = (const ctf_header_t *)fp->ctf_data.cts_data;
43285SN/A
44285SN/A	/* LINTED - pointer alignment */
45285SN/A	*ctl = (const ctf_lblent_t *)(fp->ctf_buf + h->cth_lbloff);
46285SN/A	*num_labels = (h->cth_objtoff - h->cth_lbloff) / sizeof (ctf_lblent_t);
47285SN/A
48	return (0);
49}
50
51/*
52 * Returns the topmost label, or NULL if any errors are encountered
53 */
54const char *
55ctf_label_topmost(ctf_file_t *fp)
56{
57	const ctf_lblent_t *ctlp;
58	const char *s;
59	uint_t num_labels;
60
61	if (extract_label_info(fp, &ctlp, &num_labels) == CTF_ERR)
62		return (NULL); /* errno is set */
63
64	if (num_labels == 0) {
65		(void) ctf_set_errno(fp, ECTF_NOLABELDATA);
66		return (NULL);
67	}
68
69	if ((s = ctf_strraw(fp, (ctlp + num_labels - 1)->ctl_label)) == NULL)
70		(void) ctf_set_errno(fp, ECTF_CORRUPT);
71
72	return (s);
73}
74
75/*
76 * Iterate over all labels.  We pass the label string and the lblinfo_t struct
77 * to the specified callback function.
78 */
79int
80ctf_label_iter(ctf_file_t *fp, ctf_label_f *func, void *arg)
81{
82	const ctf_lblent_t *ctlp;
83	uint_t i, num_labels;
84	ctf_lblinfo_t linfo;
85	const char *lname;
86	int rc;
87
88	if (extract_label_info(fp, &ctlp, &num_labels) == CTF_ERR)
89		return (CTF_ERR); /* errno is set */
90
91	if (num_labels == 0)
92		return (ctf_set_errno(fp, ECTF_NOLABELDATA));
93
94	for (i = 0; i < num_labels; i++, ctlp++) {
95		if ((lname = ctf_strraw(fp, ctlp->ctl_label)) == NULL) {
96			ctf_dprintf("failed to decode label %u with "
97			    "typeidx %u\n", ctlp->ctl_label, ctlp->ctl_typeidx);
98			return (ctf_set_errno(fp, ECTF_CORRUPT));
99		}
100
101		linfo.ctb_typeidx = ctlp->ctl_typeidx;
102		if ((rc = func(lname, &linfo, arg)) != 0)
103			return (rc);
104	}
105
106	return (0);
107}
108
109typedef struct linfo_cb_arg {
110	const char *lca_name;    /* Label we want to retrieve info for */
111	ctf_lblinfo_t *lca_info; /* Where to store the info about the label */
112} linfo_cb_arg_t;
113
114static int
115label_info_cb(const char *lname, const ctf_lblinfo_t *linfo, void *arg)
116{
117	/*
118	 * If lname matches the label we are looking for, copy the
119	 * lblinfo_t struct for the caller.
120	 */
121	if (strcmp(lname, ((linfo_cb_arg_t *)arg)->lca_name) == 0) {
122		/*
123		 * Allow caller not to allocate storage to test if label exists
124		 */
125		if (((linfo_cb_arg_t *)arg)->lca_info != NULL)
126			bcopy(linfo, ((linfo_cb_arg_t *)arg)->lca_info,
127			    sizeof (ctf_lblinfo_t));
128		return (1); /* Indicate we found a match */
129	}
130
131	return (0);
132}
133
134/*
135 * Retrieve information about the label with name "lname"
136 */
137int
138ctf_label_info(ctf_file_t *fp, const char *lname, ctf_lblinfo_t *linfo)
139{
140	linfo_cb_arg_t cb_arg;
141	int rc;
142
143	cb_arg.lca_name = lname;
144	cb_arg.lca_info = linfo;
145
146	if ((rc = ctf_label_iter(fp, label_info_cb, &cb_arg)) == CTF_ERR)
147		return (rc);
148
149	if (rc != 1)
150		return (ctf_set_errno(fp, ECTF_NOLABEL));
151
152	return (0);
153}
154