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