17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*2becb8cdSRobert Mustacchi  * Copyright (c) 2015, Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * libctf open/close interposition layer
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * This interposition layer serves two purposes.  First, it allows the
327c478bd9Sstevel@tonic-gate  * introduction of a kmdb-specific implementation of ctf_open().  The normal
337c478bd9Sstevel@tonic-gate  * ctf_open() call open(2)s a file, and reads the CTF data directly from it.
347c478bd9Sstevel@tonic-gate  * No such facility is available in kmdb, as kmdb does not run in userland.  We
357c478bd9Sstevel@tonic-gate  * can, however, observe that kmdb uses this interface only to read CTF data
367c478bd9Sstevel@tonic-gate  * from dmods.  dmods, as viewed by kmdb, do have CTF data, but they have it
377c478bd9Sstevel@tonic-gate  * in a form that is best accessed by ctf_bufopen().  This interposition layer
387c478bd9Sstevel@tonic-gate  * allows us to translate ctf_open() calls into ctf_bufopen() calls.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * The second purpose of the interposition layer is to reduce the work needed
417c478bd9Sstevel@tonic-gate  * to call mdb_ctf_bufopen.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
467c478bd9Sstevel@tonic-gate #include <libctf.h>
477c478bd9Sstevel@tonic-gate #include <ctf_impl.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_module.h>
507c478bd9Sstevel@tonic-gate #include <mdb/mdb_ctf.h>
517c478bd9Sstevel@tonic-gate #include <mdb/mdb_string.h>
527c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
537c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
547c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static kmdb_modctl_t *
mdb_ctf_name2ctl(const char * pathname)577c478bd9Sstevel@tonic-gate mdb_ctf_name2ctl(const char *pathname)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	mdb_var_t *v;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, strbasename(pathname))) ==
627c478bd9Sstevel@tonic-gate 	    NULL)
637c478bd9Sstevel@tonic-gate 		return (NULL);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	return ((kmdb_modctl_t *)MDB_NV_COOKIE(v));
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate ctf_file_t *
mdb_ctf_open(const char * pathname,int * errp)697c478bd9Sstevel@tonic-gate mdb_ctf_open(const char *pathname, int *errp)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	struct module *mp;
727c478bd9Sstevel@tonic-gate 	kmdb_modctl_t *kmc;
737c478bd9Sstevel@tonic-gate 	ctf_file_t *ctfp;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if ((kmc = mdb_ctf_name2ctl(pathname)) == NULL) {
767c478bd9Sstevel@tonic-gate 		if (errp != NULL)
777c478bd9Sstevel@tonic-gate 			*errp = ENOENT;
787c478bd9Sstevel@tonic-gate 		return (NULL);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	mp = kmc->kmc_modctl->mod_mp;
827c478bd9Sstevel@tonic-gate 	if (mp->ctfdata == NULL) {
837c478bd9Sstevel@tonic-gate 		if (errp != NULL)
847c478bd9Sstevel@tonic-gate 			*errp = ECTF_NOCTFDATA;
857c478bd9Sstevel@tonic-gate 		return (NULL);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if ((ctfp = mdb_ctf_bufopen(mp->ctfdata, mp->ctfsize, mp->symtbl,
897c478bd9Sstevel@tonic-gate 	    mp->symhdr, mp->strings, mp->strhdr, errp)) == NULL)
907c478bd9Sstevel@tonic-gate 		return (NULL);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_MODULE, "loaded %lu bytes of CTF data for %s\n",
937c478bd9Sstevel@tonic-gate 	    (ulong_t)mp->ctfsize, kmc->kmc_modname);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	return (ctfp);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate void
mdb_ctf_close(ctf_file_t * fp)997c478bd9Sstevel@tonic-gate mdb_ctf_close(ctf_file_t *fp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	ctf_close(fp);
1027c478bd9Sstevel@tonic-gate }
103*2becb8cdSRobert Mustacchi 
104*2becb8cdSRobert Mustacchi /*ARGSUSED*/
105*2becb8cdSRobert Mustacchi int
mdb_ctf_write(const char * file,ctf_file_t * fp)106*2becb8cdSRobert Mustacchi mdb_ctf_write(const char *file, ctf_file_t *fp)
107*2becb8cdSRobert Mustacchi {
108*2becb8cdSRobert Mustacchi 	return (ENOTSUP);
109*2becb8cdSRobert Mustacchi }
110