xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_module_load.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 2006 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 #include <sys/param.h>
30 #include <unistd.h>
31 #include <strings.h>
32 #include <dlfcn.h>
33 #include <ctype.h>
34 #include <link.h>
35 
36 #include <mdb/mdb_module.h>
37 #include <mdb/mdb_modapi.h>
38 #include <mdb/mdb_debug.h>
39 #include <mdb/mdb_string.h>
40 #include <mdb/mdb_err.h>
41 #include <mdb/mdb_io.h>
42 #include <mdb/mdb_frame.h>
43 #include <mdb/mdb.h>
44 
45 int
46 mdb_module_load(const char *name, int mode)
47 {
48 	const char *wformat = "no module '%s' could be found\n";
49 	const char *fullname = NULL;
50 	char buf[MAXPATHLEN], *p, *q;
51 	int i;
52 
53 	ASSERT(!(mode & MDB_MOD_DEFER));
54 
55 	if (strchr(name, '/') != NULL) {
56 		ASSERT(!(mode & MDB_MOD_BUILTIN));
57 
58 		(void) mdb_iob_snprintf(buf, sizeof (buf), "%s",
59 		    strbasename(name));
60 
61 		/*
62 		 * Remove any .so(.[0-9]+)? suffix
63 		 */
64 		if ((p = strrchr(buf, '.')) != NULL) {
65 			for (q = p + 1; isdigit(*q); q++)
66 				;
67 
68 			if (*q == '\0') {
69 				/* found digits to remove */
70 				*p = '\0';
71 				p = strrchr(buf, '.'); /* search for ".so" */
72 			}
73 		}
74 
75 		if (p != NULL) {
76 			if (strcmp(p, ".so") == 0)
77 				*p = '\0';
78 		}
79 
80 		fullname = name;
81 		name = buf;
82 	}
83 
84 	if (!mdb_module_validate_name(name, &wformat))
85 		goto err;
86 
87 	if (fullname != NULL) {
88 		if (access(fullname, F_OK) != 0) {
89 			name = fullname; /* for warn() below */
90 			goto err;
91 		}
92 		return (mdb_module_create(name, fullname, mode, NULL));
93 	}
94 
95 	/*
96 	 * If a simple name is specified, search for it in the module path.
97 	 * The module path is searched in order, and for each element we
98 	 * look for the following files:
99 	 *
100 	 * 1. If the module name ends in ".so.[0-9]+", search for the literal
101 	 *    name and then search for the name without the [0-9]+ suffix.
102 	 * 2. If the module name ends in ".so", search for the literal name.
103 	 * 3. Search for the module name with ".so" appended.
104 	 *
105 	 * Once a matching file is detected, we attempt to load that module
106 	 * and do not resume our search in the case of an error.
107 	 */
108 	for (i = 0; mdb.m_lpath[i] != NULL; i++) {
109 		if ((p = strrchr(name, '.')) != NULL && *++p != '\0') {
110 			if (strisnum(p) || strcmp(p, "so") == 0) {
111 				(void) mdb_iob_snprintf(buf, sizeof (buf),
112 				    "%s/%s", mdb.m_lpath[i], name);
113 				mdb_dprintf(MDB_DBG_MODULE,
114 				    "checking for %s\n", buf);
115 				if (access(buf, F_OK) == 0) {
116 					return (mdb_module_create(name, buf,
117 					    mode, NULL));
118 				}
119 			}
120 
121 			if (strisnum(p) && (p = strrchr(buf, '.')) != NULL) {
122 				*p = '\0'; /* strip trailing digits */
123 				mdb_dprintf(MDB_DBG_MODULE,
124 				    "checking for %s\n", buf);
125 				if (access(buf, F_OK) == 0) {
126 					return (mdb_module_create(name, buf,
127 					    mode, NULL));
128 				}
129 			}
130 		}
131 
132 		(void) mdb_iob_snprintf(buf, sizeof (buf), "%s/%s.so",
133 		    mdb.m_lpath[i], name);
134 
135 		mdb_dprintf(MDB_DBG_MODULE, "checking for %s\n", buf);
136 
137 		if (access(buf, F_OK) == 0)
138 			return (mdb_module_create(name, buf, mode, NULL));
139 	}
140 err:
141 	if (!(mode & MDB_MOD_SILENT))
142 		warn(wformat, name);
143 
144 	return (-1);
145 }
146 
147 typedef struct mdb_modload_data {
148 	int mld_first;
149 	int mld_mode;
150 } mdb_modload_data_t;
151 
152 /*ARGSUSED*/
153 static int
154 module_load(void *fp, const mdb_map_t *map, const char *name)
155 {
156 	mdb_modload_data_t *mld = fp;
157 	name = strbasename(name);
158 
159 	if (mdb_module_load(name, mld->mld_mode) == 0 && mdb.m_term != NULL) {
160 		if (mld->mld_first == TRUE) {
161 			mdb_iob_puts(mdb.m_out, "Loading modules: [");
162 			mld->mld_first = FALSE;
163 		}
164 		mdb_iob_printf(mdb.m_out, " %s", name);
165 		mdb_iob_flush(mdb.m_out);
166 	}
167 
168 	return (0);
169 }
170 
171 void
172 mdb_module_load_all(int mode)
173 {
174 	uint_t oflag = mdb_iob_getflags(mdb.m_out) & MDB_IOB_PGENABLE;
175 	mdb_modload_data_t mld;
176 
177 	mld.mld_first = TRUE;
178 	mld.mld_mode = mode | MDB_MOD_LOCAL | MDB_MOD_SILENT;
179 
180 	mdb_iob_clrflags(mdb.m_out, oflag);
181 
182 	(void) mdb_tgt_object_iter(mdb.m_target, module_load, &mld);
183 
184 	if (mdb.m_term != NULL && mld.mld_first == FALSE)
185 		mdb_iob_puts(mdb.m_out, " ]\n");
186 
187 	mdb_iob_setflags(mdb.m_out, oflag);
188 }
189 
190 int
191 mdb_module_unload(const char *name, int mode)
192 {
193 	ASSERT((mode & ~MDB_MOD_SILENT) == 0);
194 
195 	return (mdb_module_unload_common(name));
196 }
197