xref: /illumos-gate/usr/src/cmd/mdb/common/kmdb/kmdb_ctf_open.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * libctf open/close interposition layer
31  *
32  * This interposition layer serves two purposes.  First, it allows the
33  * introduction of a kmdb-specific implementation of ctf_open().  The normal
34  * ctf_open() call open(2)s a file, and reads the CTF data directly from it.
35  * No such facility is available in kmdb, as kmdb does not run in userland.  We
36  * can, however, observe that kmdb uses this interface only to read CTF data
37  * from dmods.  dmods, as viewed by kmdb, do have CTF data, but they have it
38  * in a form that is best accessed by ctf_bufopen().  This interposition layer
39  * allows us to translate ctf_open() calls into ctf_bufopen() calls.
40  *
41  * The second purpose of the interposition layer is to reduce the work needed
42  * to call mdb_ctf_bufopen.
43  */
44 
45 #include <sys/types.h>
46 #include <sys/kobj.h>
47 #include <libctf.h>
48 #include <ctf_impl.h>
49 
50 #include <kmdb/kmdb_module.h>
51 #include <mdb/mdb_ctf.h>
52 #include <mdb/mdb_string.h>
53 #include <mdb/mdb_debug.h>
54 #include <mdb/mdb_err.h>
55 #include <mdb/mdb.h>
56 
57 static kmdb_modctl_t *
58 mdb_ctf_name2ctl(const char *pathname)
59 {
60 	mdb_var_t *v;
61 
62 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, strbasename(pathname))) ==
63 	    NULL)
64 		return (NULL);
65 
66 	return ((kmdb_modctl_t *)MDB_NV_COOKIE(v));
67 }
68 
69 ctf_file_t *
70 mdb_ctf_open(const char *pathname, int *errp)
71 {
72 	struct module *mp;
73 	kmdb_modctl_t *kmc;
74 	ctf_file_t *ctfp;
75 
76 	if ((kmc = mdb_ctf_name2ctl(pathname)) == NULL) {
77 		if (errp != NULL)
78 			*errp = ENOENT;
79 		return (NULL);
80 	}
81 
82 	mp = kmc->kmc_modctl->mod_mp;
83 	if (mp->ctfdata == NULL) {
84 		if (errp != NULL)
85 			*errp = ECTF_NOCTFDATA;
86 		return (NULL);
87 	}
88 
89 	if ((ctfp = mdb_ctf_bufopen(mp->ctfdata, mp->ctfsize, mp->symtbl,
90 	    mp->symhdr, mp->strings, mp->strhdr, errp)) == NULL)
91 		return (NULL);
92 
93 	mdb_dprintf(MDB_DBG_MODULE, "loaded %lu bytes of CTF data for %s\n",
94 	    (ulong_t)mp->ctfsize, kmc->kmc_modname);
95 
96 	return (ctfp);
97 }
98 
99 void
100 mdb_ctf_close(ctf_file_t *fp)
101 {
102 	ctf_close(fp);
103 }
104