dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mdb / common / kmdb / kmdb_main.c
blob48730a482988af498e695dfbb21d5db5b1be3929
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 #define KMDB_STACK_SIZE (192 * 1024)
69 caddr_t kmdb_main_stack;
70 size_t kmdb_main_stack_size;
72 #define KMDB_DEF_IPATH "internal"
73 #if defined(_LP64)
74 #define KMDB_DEF_LPATH \
75 "%r/platform/%p/kernel/kmdb/%i:" \
76 "%r/platform/%m/kernel/kmdb/%i:" \
77 "%r/kernel/kmdb/%i"
78 #else
79 #define KMDB_DEF_LPATH \
80 "%r/platform/%m/kernel/kmdb:" \
81 "%r/kernel/kmdb"
82 #endif
84 #define MDB_DEF_PROMPT "[%<_cpuid>]> "
86 #define KMDB_DEF_TERM_TYPE "vt100"
89 * Similar to the panic_* variables in the kernel, we keep some relevant
90 * information stored in a set of global _mdb_abort_* variables; in the
91 * event that the debugger dumps core, these will aid core dump analysis.
93 const char *volatile _mdb_abort_str; /* reason for failure */
96 * The kernel supplies a space-delimited list of directories
97 * (/platform/sun4u/kernel /kernel /usr/kernel ...) which we must transform into
98 * a debugger-friendly, colon-delimited module search path. We add the kmdb
99 * module directory to each component and change the delimiter.
101 static char *
102 kmdb_modpath2lpath(const char *modpath)
104 static const char suffix[] = "/kmdb:";
105 const char *c;
106 char *lpath, *lpend, *nlpath;
107 size_t lpsz, lpres;
109 if (strchr(modpath, ':') != NULL) {
110 warn("invalid kernel module path\n");
111 return (NULL);
114 lpres = lpsz = strlen(modpath) + MAXPATHLEN;
115 lpend = lpath = mdb_zalloc(lpsz, UM_SLEEP);
117 while (isspace(*modpath))
118 modpath++;
120 for (; *modpath != '\0'; modpath = c) {
121 size_t sz;
123 for (c = modpath; !isspace(*c) && *c != '\0'; c++)
124 continue;
126 sz = (c - modpath) + sizeof (suffix) - 1;
127 if (sz >= lpres)
128 continue;
130 (void) strncpy(lpend, modpath, c - modpath);
131 (void) strcpy(lpend + (c - modpath), suffix);
133 lpend += sz;
134 lpres -= sz;
136 while (isspace(*c))
137 c++;
140 if (lpend != lpath)
141 lpend[-1] = '\0'; /* eat trailing colon */
143 nlpath = strdup(lpath);
144 mdb_free(lpath, lpsz);
145 return (nlpath);
149 * called while the kernel is running
152 kmdb_init(const char *execname, kmdb_auxv_t *kav)
154 mdb_io_t *in_io, *out_io, *err_io, *null_io;
155 mdb_tgt_ctor_f *tgt_ctor = kmdb_kvm_create;
156 mdb_tgt_t *tgt;
157 int i;
160 * The beginnings of debugger initialization are a bit of a dance, due
161 * to interlocking dependencies between kmdb_prom_init,
162 * mdb_umem_startup, and mdb_create. In particular, allocator
163 * initialization can't begin until prom_init() is called,
164 * kmdb_prom_init can't finish until the allocator is ready and
165 * mdb_create has been called. We therefore split kmdb_prom_init into
166 * two pieces, and call thembefore and after umem initialization and
167 * mdb_create.
169 kmdb_prom_init_begin("kmdb", kav);
170 mdb_umem_startup(kav->kav_dseg, kav->kav_dseg_size,
171 kav->kav_pagesize);
172 mdb_create(execname, "kmdb");
173 kmdb_prom_init_finish(kav);
175 mdb.m_dseg = kav->kav_dseg;
176 mdb.m_dsegsz = kav->kav_dseg_size;
178 out_io = kmdb_promio_create("stdout");
179 mdb.m_out = mdb_iob_create(out_io, MDB_IOB_WRONLY);
181 err_io = kmdb_promio_create("stderr");
182 mdb.m_err = mdb_iob_create(err_io, MDB_IOB_WRONLY);
183 mdb_iob_clrflags(mdb.m_err, MDB_IOB_AUTOWRAP);
185 null_io = mdb_nullio_create();
186 mdb.m_null = mdb_iob_create(null_io, MDB_IOB_WRONLY);
188 in_io = kmdb_promio_create("stdin");
189 mdb.m_term = NULL;
191 if (kav->kav_config != NULL)
192 mdb_set_config(kav->kav_config);
194 if (kav->kav_argv != NULL) {
195 for (i = 0; kav->kav_argv[i] != NULL; i++) {
196 if (!mdb_set_options(kav->kav_argv[i], TRUE))
197 return (-1);
201 if (kav->kav_flags & KMDB_AUXV_FL_NOUNLOAD)
202 mdb.m_flags |= MDB_FL_NOUNLOAD;
204 mdb.m_in = mdb_iob_create(in_io, MDB_IOB_RDONLY);
205 mdb_iob_setflags(mdb.m_in, MDB_IOB_TTYLIKE);
207 mdb_lex_reset();
209 kmdb_kdi_init(kav->kav_kdi, kav);
211 if (kmdb_dpi_init(kav) < 0) {
212 warn("Couldn't initialize kernel/PROM interface\n");
213 return (-1);
217 * Path evaluation part 1: Create the initial module path to allow
218 * the target constructor to load a support module. We base kmdb's
219 * module path off the kernel's module path unless the user has
220 * explicitly supplied one.
222 mdb_set_ipath(KMDB_DEF_IPATH);
223 if (strlen(mdb.m_lpathstr) > 0) {
224 mdb_set_lpath(mdb.m_lpathstr);
225 } else {
226 char *lpath;
228 if (kav->kav_modpath != NULL && *kav->kav_modpath != '\0' &&
229 (lpath = kmdb_modpath2lpath(kav->kav_modpath)) != NULL) {
230 mdb_set_lpath(lpath);
231 strfree(lpath);
232 } else {
233 mdb_set_lpath(KMDB_DEF_LPATH);
237 if (mdb_get_prompt() == NULL)
238 (void) mdb_set_prompt(MDB_DEF_PROMPT);
240 tgt = mdb_tgt_create(tgt_ctor, mdb.m_tgtflags, 0, NULL);
242 if (tgt == NULL) {
243 warn("failed to initialize target");
244 return (-1);
247 mdb_tgt_activate(tgt);
249 mdb_create_loadable_disasms();
252 * Path evaluation part 2: Re-evaluate the path now that the target
253 * is ready (and thus we have access to the real platform string).
255 mdb_set_ipath(mdb.m_ipathstr);
256 mdb_set_lpath(mdb.m_lpathstr);
258 if (!(mdb.m_flags & MDB_FL_NOMODS))
259 mdb_module_load_all(MDB_MOD_DEFER);
261 /* Allocate the main debugger stack */
262 kmdb_main_stack = mdb_alloc(KMDB_STACK_SIZE, UM_SLEEP);
263 kmdb_main_stack_size = KMDB_STACK_SIZE;
265 kmdb_kdi_end_init();
267 return (0);
270 #ifdef sun4v
272 void
273 kmdb_init_promif(char *pgmname, kmdb_auxv_t *kav)
275 kmdb_prom_init_promif(pgmname, kav);
278 #else
280 /*ARGSUSED*/
281 void
282 kmdb_init_promif(char *pgmname, kmdb_auxv_t *kav)
285 * Fake function for non sun4v. See comments in kmdb_ctl.h
287 ASSERT(0);
290 #endif
293 * First-time kmdb startup. Run when kmdb has control of the machine for the
294 * first time.
296 static void
297 kmdb_startup(void)
299 mdb_io_t *inio, *outio;
301 if (mdb.m_termtype == NULL) {
303 * The terminal type wasn't specified, so we guess. If we're
304 * on console, we'll get a terminal type from the PROM. If not,
305 * we'll use the default.
307 const char *ttype;
309 if ((ttype = kmdb_prom_term_type()) == NULL) {
310 ttype = KMDB_DEF_TERM_TYPE;
311 warn("unable to determine terminal type: "
312 "assuming `%s'\n", ttype);
315 mdb.m_flags |= MDB_FL_TERMGUESS;
316 mdb.m_termtype = strdup(ttype);
318 } else if (mdb.m_flags & MDB_FL_TERMGUESS) {
320 * The terminal type wasn't specified by the user, but a guess
321 * was made using either $TERM or a property from the SMF. A
322 * terminal type from the PROM code overrides the guess, so
323 * we'll use that if we can.
325 char *promttype;
327 if ((promttype = kmdb_prom_term_type()) != NULL) {
328 strfree(mdb.m_termtype);
329 mdb.m_termtype = strdup(promttype);
333 inio = kmdb_promio_create("stdin");
334 outio = kmdb_promio_create("stdout");
336 if ((mdb.m_term = mdb_termio_create(mdb.m_termtype, inio, outio)) ==
337 NULL && strcmp(mdb.m_termtype, KMDB_DEF_TERM_TYPE) != 0) {
338 warn("failed to set terminal type to `%s', using `"
339 KMDB_DEF_TERM_TYPE "'\n", mdb.m_termtype);
341 strfree(mdb.m_termtype);
342 mdb.m_termtype = strdup(KMDB_DEF_TERM_TYPE);
344 if ((mdb.m_term = mdb_termio_create(mdb.m_termtype, inio,
345 outio)) == NULL) {
346 fail("failed to set terminal type to `"
347 KMDB_DEF_TERM_TYPE "'\n");
351 mdb_iob_destroy(mdb.m_in);
352 mdb.m_in = mdb_iob_create(mdb.m_term, MDB_IOB_RDONLY);
353 mdb_iob_setpager(mdb.m_out, mdb.m_term);
354 mdb_iob_setflags(mdb.m_out, MDB_IOB_PGENABLE);
356 kmdb_kvm_startup();
359 * kmdb_init() and kctl_activate() may have been talking to each other,
360 * and may have left some messages for us. The driver -> debugger
361 * queue is normally processed during the resume path, so we have to
362 * do it manually here if we want it to be run for first startup.
364 kmdb_dpi_process_work_queue();
366 kmdb_kvm_poststartup();
369 void
370 kmdb_main(void)
372 int status;
374 kmdb_dpi_set_state(DPI_STATE_STOPPED, 0);
375 mdb_printf("\nWelcome to kmdb\n");
376 kmdb_startup();
379 * Debugger termination is a bit tricky. For compatibility with kadb,
380 * neither an EOF on stdin nor a normal ::quit will cause the debugger
381 * to unload. In both cases, they get a trip to OBP, after which the
382 * debugger returns.
384 * The only supported way to cause the debugger to unload is to specify
385 * the unload flag to ::quit, or to have the driver request it. The
386 * driver request is the primary exit mechanism - the ::quit flag is
387 * provided for convenience.
389 * Both forms of "exit" (unqualified ::quit that won't cause an unload,
390 * and a driver request that will) are signalled by an MDB_ERR_QUIT. In
391 * the latter case, however, the KDI will have the unload request.
393 for (;;) {
394 status = mdb_run();
396 if (status == MDB_ERR_QUIT && kmdb_kdi_get_unload_request()) {
397 break;
399 } else if (status == MDB_ERR_QUIT || status == 0) {
400 kmdb_dpi_enter_mon();
402 } else if (status == MDB_ERR_OUTPUT) {
404 * If a write failed on stdout, give up. A more
405 * informative error message will already have been
406 * printed by mdb_run().
408 if (mdb_iob_getflags(mdb.m_out) & MDB_IOB_ERR)
409 fail("write to stdout failed, exiting\n");
411 } else if (status != MDB_ERR_ABORT) {
412 fail("debugger exited abnormally (status = %s)\n",
413 mdb_err2str(status));
417 mdb_destroy();
419 kmdb_dpi_resume_unload();
421 /*NOTREACHED*/