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