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