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 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/mman.h> 28 #include <sys/resource.h> 29 #include <sys/termios.h> 30 #include <sys/param.h> 31 #include <sys/salib.h> 32 33 #include <alloca.h> 34 #include <unistd.h> 35 #include <string.h> 36 #include <stdlib.h> 37 #include <fcntl.h> 38 #include <libctf.h> 39 #include <errno.h> 40 #include <ctype.h> 41 42 #include <kmdb/kmdb_promif.h> 43 #include <kmdb/kmdb_dpi.h> 44 #include <kmdb/kmdb_umemglue.h> 45 #include <kmdb/kmdb_io.h> 46 #include <kmdb/kmdb_dpi.h> 47 #include <kmdb/kmdb_wr.h> 48 #include <kmdb/kmdb_start.h> 49 #include <kmdb/kmdb_kdi.h> 50 #include <kmdb/kmdb_kvm.h> 51 #include <mdb/mdb_lex.h> 52 #include <mdb/mdb_debug.h> 53 #include <mdb/mdb_signal.h> 54 #include <mdb/mdb_string.h> 55 #include <mdb/mdb_modapi.h> 56 #include <mdb/mdb_target.h> 57 #include <mdb/mdb_gelf.h> 58 #include <mdb/mdb_conf.h> 59 #include <mdb/mdb_err.h> 60 #include <mdb/mdb_io_impl.h> 61 #include <mdb/mdb_frame.h> 62 #include <mdb/mdb_set.h> 63 #include <mdb/mdb.h> 64 65 #ifdef __sparc 66 #define KMDB_STACK_SIZE (384 * 1024) 67 #else 68 #define KMDB_STACK_SIZE (192 * 1024) 69 #endif 70 71 caddr_t kmdb_main_stack; 72 size_t kmdb_main_stack_size; 73 74 #define KMDB_DEF_IPATH "internal" 75 #if defined(_LP64) 76 #define KMDB_DEF_LPATH \ 77 "%r/platform/%p/kernel/kmdb/%i:" \ 78 "%r/platform/%m/kernel/kmdb/%i:" \ 79 "%r/kernel/kmdb/%i" 80 #else 81 #define KMDB_DEF_LPATH \ 82 "%r/platform/%m/kernel/kmdb:" \ 83 "%r/kernel/kmdb" 84 #endif 85 86 #define MDB_DEF_PROMPT "[%<_cpuid>]> " 87 88 #define KMDB_DEF_TERM_TYPE "vt100" 89 90 /* 91 * Similar to the panic_* variables in the kernel, we keep some relevant 92 * information stored in a set of global _mdb_abort_* variables; in the 93 * event that the debugger dumps core, these will aid core dump analysis. 94 */ 95 const char *volatile _mdb_abort_str; /* reason for failure */ 96 97 /* 98 * The kernel supplies a space-delimited list of directories 99 * (/platform/sun4u/kernel /kernel /usr/kernel ...) which we must transform into 100 * a debugger-friendly, colon-delimited module search path. We add the kmdb 101 * module directory to each component and change the delimiter. 102 */ 103 static char * 104 kmdb_modpath2lpath(const char *modpath) 105 { 106 #ifdef _LP64 107 static const char suffix[] = "/kmdb/%i:"; 108 #else 109 static const char suffix[] = "/kmdb:"; 110 #endif 111 const char *c; 112 char *lpath, *lpend, *nlpath; 113 size_t lpsz, lpres; 114 115 if (strchr(modpath, ':') != NULL) { 116 warn("invalid kernel module path\n"); 117 return (NULL); 118 } 119 120 lpres = lpsz = strlen(modpath) + MAXPATHLEN; 121 lpend = lpath = mdb_zalloc(lpsz, UM_SLEEP); 122 123 while (isspace(*modpath)) 124 modpath++; 125 126 for (; *modpath != '\0'; modpath = c) { 127 size_t sz; 128 129 for (c = modpath; !isspace(*c) && *c != '\0'; c++) 130 continue; 131 132 sz = (c - modpath) + sizeof (suffix) - 1; 133 if (sz >= lpres) 134 continue; 135 136 (void) strncpy(lpend, modpath, c - modpath); 137 (void) strcpy(lpend + (c - modpath), suffix); 138 139 lpend += sz; 140 lpres -= sz; 141 142 while (isspace(*c)) 143 c++; 144 } 145 146 if (lpend != lpath) 147 lpend[-1] = '\0'; /* eat trailing colon */ 148 149 nlpath = strdup(lpath); 150 mdb_free(lpath, lpsz); 151 return (nlpath); 152 } 153 154 /* 155 * called while the kernel is running 156 */ 157 int 158 kmdb_init(const char *execname, kmdb_auxv_t *kav) 159 { 160 mdb_io_t *in_io, *out_io, *err_io, *null_io; 161 mdb_tgt_ctor_f *tgt_ctor = kmdb_kvm_create; 162 mdb_tgt_t *tgt; 163 int i; 164 165 /* 166 * The beginnings of debugger initialization are a bit of a dance, due 167 * to interlocking dependencies between kmdb_prom_init, 168 * mdb_umem_startup, and mdb_create. In particular, allocator 169 * initialization can't begin until prom_init() is called, 170 * kmdb_prom_init can't finish until the allocator is ready and 171 * mdb_create has been called. We therefore split kmdb_prom_init into 172 * two pieces, and call thembefore and after umem initialization and 173 * mdb_create. 174 */ 175 kmdb_prom_init_begin("kmdb", kav); 176 mdb_umem_startup(kav->kav_dseg, kav->kav_dseg_size, 177 kav->kav_pagesize); 178 mdb_create(execname, "kmdb"); 179 kmdb_prom_init_finish(kav); 180 181 mdb.m_dseg = kav->kav_dseg; 182 mdb.m_dsegsz = kav->kav_dseg_size; 183 184 out_io = kmdb_promio_create("stdout"); 185 mdb.m_out = mdb_iob_create(out_io, MDB_IOB_WRONLY); 186 187 err_io = kmdb_promio_create("stderr"); 188 mdb.m_err = mdb_iob_create(err_io, MDB_IOB_WRONLY); 189 mdb_iob_clrflags(mdb.m_err, MDB_IOB_AUTOWRAP); 190 191 null_io = mdb_nullio_create(); 192 mdb.m_null = mdb_iob_create(null_io, MDB_IOB_WRONLY); 193 194 in_io = kmdb_promio_create("stdin"); 195 mdb.m_term = NULL; 196 197 if (kav->kav_config != NULL) 198 mdb_set_config(kav->kav_config); 199 200 if (kav->kav_argv != NULL) { 201 for (i = 0; kav->kav_argv[i] != NULL; i++) { 202 if (!mdb_set_options(kav->kav_argv[i], TRUE)) 203 return (-1); 204 } 205 } 206 207 if (kav->kav_flags & KMDB_AUXV_FL_NOUNLOAD) 208 mdb.m_flags |= MDB_FL_NOUNLOAD; 209 210 mdb.m_in = mdb_iob_create(in_io, MDB_IOB_RDONLY); 211 mdb_iob_setflags(mdb.m_in, MDB_IOB_TTYLIKE); 212 213 mdb_lex_reset(); 214 215 kmdb_kdi_init(kav->kav_kdi, kav); 216 217 if (kmdb_dpi_init(kav) < 0) { 218 warn("Couldn't initialize kernel/PROM interface\n"); 219 return (-1); 220 } 221 222 /* 223 * Path evaluation part 1: Create the initial module path to allow 224 * the target constructor to load a support module. We base kmdb's 225 * module path off the kernel's module path unless the user has 226 * explicitly supplied one. 227 */ 228 mdb_set_ipath(KMDB_DEF_IPATH); 229 if (strlen(mdb.m_lpathstr) > 0) { 230 mdb_set_lpath(mdb.m_lpathstr); 231 } else { 232 char *lpath; 233 234 if (kav->kav_modpath != NULL && *kav->kav_modpath != '\0' && 235 (lpath = kmdb_modpath2lpath(kav->kav_modpath)) != NULL) { 236 mdb_set_lpath(lpath); 237 strfree(lpath); 238 } else { 239 mdb_set_lpath(KMDB_DEF_LPATH); 240 } 241 } 242 243 if (mdb_get_prompt() == NULL) 244 (void) mdb_set_prompt(MDB_DEF_PROMPT); 245 246 tgt = mdb_tgt_create(tgt_ctor, mdb.m_tgtflags, 0, NULL); 247 248 if (tgt == NULL) { 249 warn("failed to initialize target"); 250 return (-1); 251 } 252 253 mdb_tgt_activate(tgt); 254 255 mdb_create_loadable_disasms(); 256 257 /* 258 * Path evaluation part 2: Re-evaluate the path now that the target 259 * is ready (and thus we have access to the real platform string). 260 */ 261 mdb_set_ipath(mdb.m_ipathstr); 262 mdb_set_lpath(mdb.m_lpathstr); 263 264 if (!(mdb.m_flags & MDB_FL_NOMODS)) 265 mdb_module_load_all(MDB_MOD_DEFER); 266 267 /* Allocate the main debugger stack */ 268 kmdb_main_stack = mdb_alloc(KMDB_STACK_SIZE, UM_SLEEP); 269 kmdb_main_stack_size = KMDB_STACK_SIZE; 270 271 kmdb_kdi_end_init(); 272 273 return (0); 274 } 275 276 #ifdef sun4v 277 278 void 279 kmdb_init_promif(char *pgmname, kmdb_auxv_t *kav) 280 { 281 kmdb_prom_init_promif(pgmname, kav); 282 } 283 284 #else 285 286 /*ARGSUSED*/ 287 void 288 kmdb_init_promif(char *pgmname, kmdb_auxv_t *kav) 289 { 290 /* 291 * Fake function for non sun4v. See comments in kmdb_ctl.h 292 */ 293 ASSERT(0); 294 } 295 296 #endif 297 298 /* 299 * First-time kmdb startup. Run when kmdb has control of the machine for the 300 * first time. 301 */ 302 static void 303 kmdb_startup(void) 304 { 305 mdb_io_t *inio, *outio; 306 307 if (mdb.m_termtype == NULL) { 308 /* 309 * The terminal type wasn't specified, so we guess. If we're 310 * on console, we'll get a terminal type from the PROM. If not, 311 * we'll use the default. 312 */ 313 const char *ttype; 314 315 if ((ttype = kmdb_prom_term_type()) == NULL) { 316 ttype = KMDB_DEF_TERM_TYPE; 317 warn("unable to determine terminal type: " 318 "assuming `%s'\n", ttype); 319 } 320 321 mdb.m_flags |= MDB_FL_TERMGUESS; 322 mdb.m_termtype = strdup(ttype); 323 324 } else if (mdb.m_flags & MDB_FL_TERMGUESS) { 325 /* 326 * The terminal type wasn't specified by the user, but a guess 327 * was made using either $TERM or a property from the SMF. A 328 * terminal type from the PROM code overrides the guess, so 329 * we'll use that if we can. 330 */ 331 char *promttype; 332 333 if ((promttype = kmdb_prom_term_type()) != NULL) { 334 strfree(mdb.m_termtype); 335 mdb.m_termtype = strdup(promttype); 336 } 337 } 338 339 inio = kmdb_promio_create("stdin"); 340 outio = kmdb_promio_create("stdout"); 341 342 if ((mdb.m_term = mdb_termio_create(mdb.m_termtype, inio, outio)) == 343 NULL && strcmp(mdb.m_termtype, KMDB_DEF_TERM_TYPE) != 0) { 344 warn("failed to set terminal type to `%s', using `" 345 KMDB_DEF_TERM_TYPE "'\n", mdb.m_termtype); 346 347 strfree(mdb.m_termtype); 348 mdb.m_termtype = strdup(KMDB_DEF_TERM_TYPE); 349 350 if ((mdb.m_term = mdb_termio_create(mdb.m_termtype, inio, 351 outio)) == NULL) { 352 fail("failed to set terminal type to `" 353 KMDB_DEF_TERM_TYPE "'\n"); 354 } 355 } 356 357 mdb_iob_destroy(mdb.m_in); 358 mdb.m_in = mdb_iob_create(mdb.m_term, MDB_IOB_RDONLY); 359 mdb_iob_setpager(mdb.m_out, mdb.m_term); 360 mdb_iob_setflags(mdb.m_out, MDB_IOB_PGENABLE); 361 362 kmdb_kvm_startup(); 363 364 /* 365 * kmdb_init() and kctl_activate() may have been talking to each other, 366 * and may have left some messages for us. The driver -> debugger 367 * queue is normally processed during the resume path, so we have to 368 * do it manually here if we want it to be run for first startup. 369 */ 370 kmdb_dpi_process_work_queue(); 371 372 kmdb_kvm_poststartup(); 373 } 374 375 void 376 kmdb_main(void) 377 { 378 int status; 379 380 kmdb_dpi_set_state(DPI_STATE_STOPPED, 0); 381 mdb_printf("\nWelcome to kmdb\n"); 382 kmdb_startup(); 383 384 /* 385 * Debugger termination is a bit tricky. For compatibility with kadb, 386 * neither an EOF on stdin nor a normal ::quit will cause the debugger 387 * to unload. In both cases, they get a trip to OBP, after which the 388 * debugger returns. 389 * 390 * The only supported way to cause the debugger to unload is to specify 391 * the unload flag to ::quit, or to have the driver request it. The 392 * driver request is the primary exit mechanism - the ::quit flag is 393 * provided for convenience. 394 * 395 * Both forms of "exit" (unqualified ::quit that won't cause an unload, 396 * and a driver request that will) are signalled by an MDB_ERR_QUIT. In 397 * the latter case, however, the KDI will have the unload request. 398 */ 399 for (;;) { 400 status = mdb_run(); 401 402 if (status == MDB_ERR_QUIT && kmdb_kdi_get_unload_request()) { 403 break; 404 405 } else if (status == MDB_ERR_QUIT || status == 0) { 406 kmdb_dpi_enter_mon(); 407 408 } else if (status == MDB_ERR_OUTPUT) { 409 /* 410 * If a write failed on stdout, give up. A more 411 * informative error message will already have been 412 * printed by mdb_run(). 413 */ 414 if (mdb_iob_getflags(mdb.m_out) & MDB_IOB_ERR) 415 fail("write to stdout failed, exiting\n"); 416 417 } else if (status != MDB_ERR_ABORT) { 418 fail("debugger exited abnormally (status = %s)\n", 419 mdb_err2str(status)); 420 } 421 } 422 423 mdb_destroy(); 424 425 kmdb_dpi_resume_unload(); 426 427 /*NOTREACHED*/ 428 } 429