summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mdb/common/kmdb/kmdb_main.c
blob: 7cc1d3f7b0c5a6e8345108b6b133573fa8058bf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/termios.h>
#include <sys/param.h>
#include <sys/salib.h>

#include <alloca.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <libctf.h>
#include <errno.h>
#include <ctype.h>

#include <kmdb/kmdb_promif.h>
#include <kmdb/kmdb_dpi.h>
#include <kmdb/kmdb_umemglue.h>
#include <kmdb/kmdb_io.h>
#include <kmdb/kmdb_dpi.h>
#include <kmdb/kmdb_wr.h>
#include <kmdb/kmdb_start.h>
#include <kmdb/kmdb_kdi.h>
#include <kmdb/kmdb_kvm.h>
#include <mdb/mdb_lex.h>
#include <mdb/mdb_debug.h>
#include <mdb/mdb_signal.h>
#include <mdb/mdb_string.h>
#include <mdb/mdb_modapi.h>
#include <mdb/mdb_target.h>
#include <mdb/mdb_gelf.h>
#include <mdb/mdb_conf.h>
#include <mdb/mdb_err.h>
#include <mdb/mdb_io_impl.h>
#include <mdb/mdb_frame.h>
#include <mdb/mdb_set.h>
#include <mdb/mdb.h>

#ifdef __sparc
#define	KMDB_STACK_SIZE	(384 * 1024)
#else
#define	KMDB_STACK_SIZE (192 * 1024)
#endif

caddr_t kmdb_main_stack;
size_t kmdb_main_stack_size;

#define	KMDB_DEF_IPATH	"internal"
#if defined(_LP64)
#define	KMDB_DEF_LPATH	\
	"%r/platform/%p/kernel/kmdb/%i:" \
	"%r/platform/%m/kernel/kmdb/%i:" \
	"%r/kernel/kmdb/%i"
#else
#define	KMDB_DEF_LPATH	\
	"%r/platform/%m/kernel/kmdb:" \
	"%r/kernel/kmdb"
#endif

#define	MDB_DEF_PROMPT "[%<_cpuid>]> "

#define	KMDB_DEF_TERM_TYPE	"vt100"

/*
 * Similar to the panic_* variables in the kernel, we keep some relevant
 * information stored in a set of global _mdb_abort_* variables; in the
 * event that the debugger dumps core, these will aid core dump analysis.
 */
const char *volatile _mdb_abort_str;	/* reason for failure */

/*
 * The kernel supplies a space-delimited list of directories
 * (/platform/sun4u/kernel /kernel /usr/kernel ...) which we must transform into
 * a debugger-friendly, colon-delimited module search path.  We add the kmdb
 * module directory to each component and change the delimiter.
 */
static char *
kmdb_modpath2lpath(const char *modpath)
{
#ifdef	_LP64
	static const char suffix[] = "/kmdb/%i:";
#else
	static const char suffix[] = "/kmdb:";
#endif
	const char *c;
	char *lpath, *lpend, *nlpath;
	size_t lpsz, lpres;

	if (strchr(modpath, ':') != NULL) {
		warn("invalid kernel module path\n");
		return (NULL);
	}

	lpres = lpsz = strlen(modpath) + MAXPATHLEN;
	lpend = lpath = mdb_zalloc(lpsz, UM_SLEEP);

	while (isspace(*modpath))
		modpath++;

	for (; *modpath != '\0'; modpath = c) {
		size_t sz;

		for (c = modpath; !isspace(*c) && *c != '\0'; c++)
			continue;

		sz = (c - modpath) + sizeof (suffix) - 1;
		if (sz >= lpres)
			continue;

		(void) strncpy(lpend, modpath, c - modpath);
		(void) strcpy(lpend + (c - modpath), suffix);

		lpend += sz;
		lpres -= sz;

		while (isspace(*c))
			c++;
	}

	if (lpend != lpath)
		lpend[-1] = '\0';	/* eat trailing colon */

	nlpath = strdup(lpath);
	mdb_free(lpath, lpsz);
	return (nlpath);
}

/*
 * called while the kernel is running
 */
int
kmdb_init(const char *execname, kmdb_auxv_t *kav)
{
	mdb_io_t *in_io, *out_io, *err_io, *null_io;
	mdb_tgt_ctor_f *tgt_ctor = kmdb_kvm_create;
	mdb_tgt_t *tgt;
	int i;

	/*
	 * The beginnings of debugger initialization are a bit of a dance, due
	 * to interlocking dependencies between kmdb_prom_init,
	 * mdb_umem_startup, and mdb_create.  In particular, allocator
	 * initialization can't begin until prom_init() is called,
	 * kmdb_prom_init can't finish until the allocator is ready and
	 * mdb_create has been called.  We therefore split kmdb_prom_init into
	 * two pieces, and call thembefore and after umem initialization and
	 * mdb_create.
	 */
	kmdb_prom_init_begin("kmdb", kav);
	mdb_umem_startup(kav->kav_dseg, kav->kav_dseg_size,
	    kav->kav_pagesize);
	mdb_create(execname, "kmdb");
	kmdb_prom_init_finish(kav);

	mdb.m_dseg = kav->kav_dseg;
	mdb.m_dsegsz = kav->kav_dseg_size;

	out_io = kmdb_promio_create("stdout");
	mdb.m_out = mdb_iob_create(out_io, MDB_IOB_WRONLY);

	err_io = kmdb_promio_create("stderr");
	mdb.m_err = mdb_iob_create(err_io, MDB_IOB_WRONLY);
	mdb_iob_clrflags(mdb.m_err, MDB_IOB_AUTOWRAP);

	null_io = mdb_nullio_create();
	mdb.m_null = mdb_iob_create(null_io, MDB_IOB_WRONLY);

	in_io = kmdb_promio_create("stdin");
	mdb.m_term = NULL;

	if (kav->kav_config != NULL)
		mdb_set_config(kav->kav_config);

	if (kav->kav_argv != NULL) {
		for (i = 0; kav->kav_argv[i] != NULL; i++) {
			if (!mdb_set_options(kav->kav_argv[i], TRUE))
				return (-1);
		}
	}

	if (kav->kav_flags & KMDB_AUXV_FL_NOUNLOAD)
		mdb.m_flags |= MDB_FL_NOUNLOAD;

	mdb.m_in = mdb_iob_create(in_io, MDB_IOB_RDONLY);
	mdb_iob_setflags(mdb.m_in, MDB_IOB_TTYLIKE);

	mdb_lex_reset();

	kmdb_kdi_init(kav->kav_kdi, kav);

	if (kmdb_dpi_init(kav) < 0) {
		warn("Couldn't initialize kernel/PROM interface\n");
		return (-1);
	}

	/*
	 * Path evaluation part 1: Create the initial module path to allow
	 * the target constructor to load a support module.  We base kmdb's
	 * module path off the kernel's module path unless the user has
	 * explicitly supplied one.
	 */
	mdb_set_ipath(KMDB_DEF_IPATH);
	if (strlen(mdb.m_lpathstr) > 0) {
		mdb_set_lpath(mdb.m_lpathstr);
	} else {
		char *lpath;

		if (kav->kav_modpath != NULL && *kav->kav_modpath != '\0' &&
		    (lpath = kmdb_modpath2lpath(kav->kav_modpath)) != NULL) {
			mdb_set_lpath(lpath);
			strfree(lpath);
		} else {
			mdb_set_lpath(KMDB_DEF_LPATH);
		}
	}

	if (mdb_get_prompt() == NULL)
		(void) mdb_set_prompt(MDB_DEF_PROMPT);

	tgt = mdb_tgt_create(tgt_ctor, mdb.m_tgtflags, 0, NULL);

	if (tgt == NULL) {
		warn("failed to initialize target");
		return (-1);
	}

	mdb_tgt_activate(tgt);

	mdb_create_loadable_disasms();

	/*
	 * Path evaluation part 2: Re-evaluate the path now that the target
	 * is ready (and thus we have access to the real platform string).
	 */
	mdb_set_ipath(mdb.m_ipathstr);
	mdb_set_lpath(mdb.m_lpathstr);

	if (!(mdb.m_flags & MDB_FL_NOMODS))
		mdb_module_load_all(MDB_MOD_DEFER);

	/* Allocate the main debugger stack */
	kmdb_main_stack = mdb_alloc(KMDB_STACK_SIZE, UM_SLEEP);
	kmdb_main_stack_size = KMDB_STACK_SIZE;

	kmdb_kdi_end_init();

	return (0);
}

#ifdef sun4v

void
kmdb_init_promif(char *pgmname, kmdb_auxv_t *kav)
{
	kmdb_prom_init_promif(pgmname, kav);
}

#else

/*ARGSUSED*/
void
kmdb_init_promif(char *pgmname, kmdb_auxv_t *kav)
{
	/*
	 * Fake function for non sun4v. See comments in kmdb_ctl.h
	 */
	ASSERT(0);
}

#endif

/*
 * First-time kmdb startup.  Run when kmdb has control of the machine for the
 * first time.
 */
static void
kmdb_startup(void)
{
	mdb_io_t *inio, *outio;

	if (mdb.m_termtype == NULL) {
		/*
		 * The terminal type wasn't specified, so we guess.  If we're
		 * on console, we'll get a terminal type from the PROM.  If not,
		 * we'll use the default.
		 */
		const char *ttype;

		if ((ttype = kmdb_prom_term_type()) == NULL) {
			ttype = KMDB_DEF_TERM_TYPE;
			warn("unable to determine terminal type: "
			    "assuming `%s'\n", ttype);
		}

		mdb.m_flags |= MDB_FL_TERMGUESS;
		mdb.m_termtype = strdup(ttype);

	} else if (mdb.m_flags & MDB_FL_TERMGUESS) {
		/*
		 * The terminal type wasn't specified by the user, but a guess
		 * was made using either $TERM or a property from the SMF.  A
		 * terminal type from the PROM code overrides the guess, so
		 * we'll use that if we can.
		 */
		char *promttype;

		if ((promttype = kmdb_prom_term_type()) != NULL) {
			strfree(mdb.m_termtype);
			mdb.m_termtype = strdup(promttype);
		}
	}

	inio = kmdb_promio_create("stdin");
	outio = kmdb_promio_create("stdout");

	if ((mdb.m_term = mdb_termio_create(mdb.m_termtype, inio, outio)) ==
	    NULL && strcmp(mdb.m_termtype, KMDB_DEF_TERM_TYPE) != 0) {
		warn("failed to set terminal type to `%s', using `"
		    KMDB_DEF_TERM_TYPE "'\n", mdb.m_termtype);

		strfree(mdb.m_termtype);
		mdb.m_termtype = strdup(KMDB_DEF_TERM_TYPE);

		if ((mdb.m_term = mdb_termio_create(mdb.m_termtype, inio,
		    outio)) == NULL) {
			fail("failed to set terminal type to `"
			    KMDB_DEF_TERM_TYPE "'\n");
		}
	}

	mdb_iob_destroy(mdb.m_in);
	mdb.m_in = mdb_iob_create(mdb.m_term, MDB_IOB_RDONLY);
	mdb_iob_setpager(mdb.m_out, mdb.m_term);
	mdb_iob_setflags(mdb.m_out, MDB_IOB_PGENABLE);

	kmdb_kvm_startup();

	/*
	 * kmdb_init() and kctl_activate() may have been talking to each other,
	 * and may have left some messages for us.  The driver -> debugger
	 * queue is normally processed during the resume path, so we have to
	 * do it manually here if we want it to be run for first startup.
	 */
	kmdb_dpi_process_work_queue();

	kmdb_kvm_poststartup();
}

void
kmdb_main(void)
{
	int status;

	kmdb_dpi_set_state(DPI_STATE_STOPPED, 0);
	mdb_printf("\nWelcome to kmdb\n");
	kmdb_startup();

	/*
	 * Debugger termination is a bit tricky.  For compatibility with kadb,
	 * neither an EOF on stdin nor a normal ::quit will cause the debugger
	 * to unload.  In both cases, they get a trip to OBP, after which the
	 * debugger returns.
	 *
	 * The only supported way to cause the debugger to unload is to specify
	 * the unload flag to ::quit, or to have the driver request it.  The
	 * driver request is the primary exit mechanism - the ::quit flag is
	 * provided for convenience.
	 *
	 * Both forms of "exit" (unqualified ::quit that won't cause an unload,
	 * and a driver request that will) are signalled by an MDB_ERR_QUIT.  In
	 * the latter case, however, the KDI will have the unload request.
	 */
	for (;;) {
		status = mdb_run();

		if (status == MDB_ERR_QUIT && kmdb_kdi_get_unload_request()) {
			break;

		} else if (status == MDB_ERR_QUIT || status == 0) {
			kmdb_dpi_enter_mon();

		} else if (status == MDB_ERR_OUTPUT) {
			/*
			 * If a write failed on stdout, give up.  A more
			 * informative error message will already have been
			 * printed by mdb_run().
			 */
			if (mdb_iob_getflags(mdb.m_out) & MDB_IOB_ERR)
				fail("write to stdout failed, exiting\n");

		} else if (status != MDB_ERR_ABORT) {
			fail("debugger exited abnormally (status = %s)\n",
			    mdb_err2str(status));
		}
	}

	mdb_destroy();

	kmdb_dpi_resume_unload();

	/*NOTREACHED*/
}