xref: /titanic_41/usr/src/cmd/mdb/common/mdb/mdb_module_load.c (revision 3b6e0a598869dfc84461624e8699bf51738f68b3)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2012 by Delphix. All rights reserved.
25  * Copyright (c) 2012 Joyent, Inc. All rights reserved.
26  */
27 
28 #include <sys/param.h>
29 #include <unistd.h>
30 #include <strings.h>
31 #include <dlfcn.h>
32 #include <ctype.h>
33 #include <link.h>
34 
35 #include <mdb/mdb_module.h>
36 #include <mdb/mdb_modapi.h>
37 #include <mdb/mdb_debug.h>
38 #include <mdb/mdb_string.h>
39 #include <mdb/mdb_err.h>
40 #include <mdb/mdb_io.h>
41 #include <mdb/mdb_frame.h>
42 #include <mdb/mdb.h>
43 
44 int
mdb_module_load(const char * name,int mode)45 mdb_module_load(const char *name, int mode)
46 {
47 	const char *wformat = "no module '%s' could be found\n";
48 	const char *fullname = NULL;
49 	char buf[MAXPATHLEN], *p, *q;
50 	int i;
51 
52 	ASSERT(!(mode & MDB_MOD_DEFER));
53 
54 	if (strchr(name, '/') != NULL) {
55 		ASSERT(!(mode & MDB_MOD_BUILTIN));
56 
57 		(void) mdb_iob_snprintf(buf, sizeof (buf), "%s",
58 		    strbasename(name));
59 
60 		/*
61 		 * Remove any .so(.[0-9]+)? suffix
62 		 */
63 		while ((p = strrchr(buf, '.')) != NULL) {
64 			for (q = p + 1; isdigit(*q); q++)
65 				;
66 
67 			if (*q == '\0') {
68 				/* found digits to remove */
69 				*p = '\0';
70 				continue;
71 			}
72 
73 			if (strcmp(p, ".so") == 0) {
74 				*p = '\0';
75 				break;
76 			}
77 
78 		}
79 		fullname = name;
80 		name = buf;
81 	}
82 
83 	if (!mdb_module_validate_name(name, &wformat))
84 		goto err;
85 
86 	if (fullname != NULL) {
87 		if (access(fullname, F_OK) != 0) {
88 			name = fullname; /* for warn() below */
89 			goto err;
90 		}
91 		return (mdb_module_create(name, fullname, mode, NULL));
92 	}
93 
94 	/*
95 	 * If a simple name is specified, search for it in the module path.
96 	 * The module path is searched in order, and for each element we
97 	 * look for the following files:
98 	 *
99 	 * 1. If the module name ends in ".so(.[0-9]+)?", search for the literal
100 	 *    name and then search for the name without the [0-9]+ suffix.
101 	 * 2. If the module name ends in ".so", search for the literal name.
102 	 * 3. Search for the module name with ".so" appended.
103 	 *
104 	 * Once a matching file is detected, we attempt to load that module
105 	 * and do not resume our search in the case of an error.
106 	 */
107 	for (i = 0; mdb.m_lpath[i] != NULL; i++) {
108 		if ((p = strrchr(name, '.')) != NULL && *++p != '\0') {
109 			if (strisnum(p) || strcmp(p, "so") == 0) {
110 				(void) mdb_iob_snprintf(buf, sizeof (buf),
111 				    "%s/%s", mdb.m_lpath[i], name);
112 				mdb_dprintf(MDB_DBG_MODULE,
113 				    "checking for %s\n", buf);
114 				if (access(buf, F_OK) == 0) {
115 					return (mdb_module_create(name, buf,
116 					    mode, NULL));
117 				}
118 			}
119 
120 			while (strisnum(p) && (p = strrchr(buf, '.')) != NULL) {
121 				*p = '\0'; /* strip trailing digits */
122 				mdb_dprintf(MDB_DBG_MODULE,
123 				    "checking for %s\n", buf);
124 				if (access(buf, F_OK) == 0) {
125 					return (mdb_module_create(name, buf,
126 					    mode, NULL));
127 				}
128 			}
129 		}
130 
131 		(void) mdb_iob_snprintf(buf, sizeof (buf), "%s/%s.so",
132 		    mdb.m_lpath[i], name);
133 
134 		mdb_dprintf(MDB_DBG_MODULE, "checking for %s\n", buf);
135 
136 		if (access(buf, F_OK) == 0)
137 			return (mdb_module_create(name, buf, mode, NULL));
138 	}
139 err:
140 	if (!(mode & MDB_MOD_SILENT))
141 		warn(wformat, name);
142 
143 	return (-1);
144 }
145 
146 typedef struct mdb_modload_data {
147 	int mld_first;
148 	int mld_mode;
149 } mdb_modload_data_t;
150 
151 /*ARGSUSED*/
152 static int
module_load(void * fp,const mdb_map_t * map,const char * fullname)153 module_load(void *fp, const mdb_map_t *map, const char *fullname)
154 {
155 	mdb_modload_data_t *mld = fp;
156 	const char *name = strbasename(fullname);
157 
158 	if (mdb_module_load(name, mld->mld_mode) == 0 && mdb.m_term != NULL) {
159 		if (mld->mld_first == TRUE) {
160 			mdb_iob_puts(mdb.m_out, "Loading modules: [");
161 			mld->mld_first = FALSE;
162 		}
163 		mdb_iob_printf(mdb.m_out, " %s", name);
164 		mdb_iob_flush(mdb.m_out);
165 	}
166 
167 	if (strstr(fullname, "/libc/") != NULL) {
168 		/*
169 		 * A bit of a kludge:  because we manage alternately capable
170 		 * libc instances by mounting the appropriately capable
171 		 * instance over /lib/libc.so.1, we may find that our object
172 		 * list does not contain libc.so.1, but rather one of its
173 		 * hwcap variants.  Unfortunately, there is not a way of
174 		 * getting from this shared object to the object that it is
175 		 * effectively interposing on -- which means that without
176 		 * special processing, we will not load any libc.so dmod.  So
177 		 * if we see that we have a shared object coming out of the
178 		 * "libc" directory, we assume that we have a "libc-like"
179 		 * object, and explicitly load the "libc.so" dmod.
180 		 */
181 		return (module_load(fp, map, "libc.so.1"));
182 	}
183 
184 	return (0);
185 }
186 
187 void
mdb_module_load_all(int mode)188 mdb_module_load_all(int mode)
189 {
190 	uint_t oflag = mdb_iob_getflags(mdb.m_out) & MDB_IOB_PGENABLE;
191 	mdb_modload_data_t mld;
192 
193 	mld.mld_first = TRUE;
194 	mld.mld_mode = mode | MDB_MOD_LOCAL | MDB_MOD_SILENT;
195 
196 	mdb_iob_clrflags(mdb.m_out, oflag);
197 
198 	(void) mdb_tgt_object_iter(mdb.m_target, module_load, &mld);
199 
200 	if (mdb.m_term != NULL && mld.mld_first == FALSE)
201 		mdb_iob_puts(mdb.m_out, " ]\n");
202 
203 	mdb_iob_setflags(mdb.m_out, oflag);
204 }
205 
206 /*ARGSUSED*/
207 int
mdb_module_unload(const char * name,int mode)208 mdb_module_unload(const char *name, int mode)
209 {
210 	ASSERT((mode & ~MDB_MOD_SILENT) == 0);
211 
212 	return (mdb_module_unload_common(name));
213 }
214