Print this page
10703 smatch unreachable code checking needs reworking
Reviewed by: Toomas Soome <tsoome@me.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/mdb/common/mdb/mdb_main.c
+++ new/usr/src/cmd/mdb/common/mdb/mdb_main.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 * Copyright 2012, Josef 'Jeff' Sipek <jeffpc@31bits.net>. All rights reserved.
25 25 */
26 26
27 27 /*
28 - * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 + * Copyright 2019 Joyent, Inc.
29 29 */
30 30
31 31 #include <sys/types.h>
32 32 #include <sys/mman.h>
33 33 #include <sys/priocntl.h>
34 34 #include <sys/rtpriocntl.h>
35 35 #include <sys/resource.h>
36 36 #include <sys/termios.h>
37 37 #include <sys/param.h>
38 38 #include <sys/regset.h>
39 39 #include <sys/frame.h>
40 40 #include <sys/stack.h>
41 41 #include <sys/reg.h>
42 42
43 43 #include <libproc.h>
44 44 #include <libscf.h>
45 45 #include <alloca.h>
46 46 #include <unistd.h>
47 47 #include <string.h>
48 48 #include <stdlib.h>
49 49 #include <fcntl.h>
50 50 #include <dlfcn.h>
51 51 #include <libctf.h>
52 52 #include <errno.h>
53 53 #include <kvm.h>
54 54
55 55 #include <mdb/mdb_lex.h>
56 56 #include <mdb/mdb_debug.h>
57 57 #include <mdb/mdb_signal.h>
58 58 #include <mdb/mdb_string.h>
59 59 #include <mdb/mdb_modapi.h>
60 60 #include <mdb/mdb_target.h>
61 61 #include <mdb/mdb_gelf.h>
62 62 #include <mdb/mdb_conf.h>
63 63 #include <mdb/mdb_err.h>
64 64 #include <mdb/mdb_io_impl.h>
65 65 #include <mdb/mdb_frame.h>
66 66 #include <mdb/mdb_set.h>
67 67 #include <kmdb/kmdb_kctl.h>
68 68 #include <mdb/mdb.h>
69 69
70 70 #ifndef STACK_BIAS
71 71 #define STACK_BIAS 0
72 72 #endif
73 73
74 74 #if defined(__sparc)
75 75 #define STACK_REGISTER SP
76 76 #else
77 77 #define STACK_REGISTER REG_FP
78 78 #endif
79 79
80 80 #ifdef _LP64
81 81 #define MDB_DEF_IPATH \
82 82 "%r/usr/platform/%p/lib/adb/%i:" \
83 83 "%r/usr/platform/%m/lib/adb/%i:" \
84 84 "%r/usr/lib/adb/%i"
85 85 #define MDB_DEF_LPATH \
86 86 "%r/usr/platform/%p/lib/mdb/%t/%i:" \
87 87 "%r/usr/platform/%m/lib/mdb/%t/%i:" \
88 88 "%r/usr/lib/mdb/%t/%i"
89 89 #else
90 90 #define MDB_DEF_IPATH \
91 91 "%r/usr/platform/%p/lib/adb:" \
92 92 "%r/usr/platform/%m/lib/adb:" \
93 93 "%r/usr/lib/adb"
94 94 #define MDB_DEF_LPATH \
95 95 "%r/usr/platform/%p/lib/mdb/%t:" \
96 96 "%r/usr/platform/%m/lib/mdb/%t:" \
97 97 "%r/usr/lib/mdb/%t"
98 98 #endif
99 99
100 100 #define MDB_DEF_PROMPT "> "
101 101
102 102 /*
103 103 * Similar to the panic_* variables in the kernel, we keep some relevant
104 104 * information stored in a set of global _mdb_abort_* variables; in the
105 105 * event that the debugger dumps core, these will aid core dump analysis.
106 106 */
107 107 const char *volatile _mdb_abort_str; /* reason for failure */
108 108 siginfo_t _mdb_abort_info; /* signal info for fatal signal */
109 109 ucontext_t _mdb_abort_ctx; /* context fatal signal interrupted */
110 110 int _mdb_abort_rcount; /* number of times resume requested */
111 111 int _mdb_self_fd = -1; /* fd for self as for valid_frame */
112 112
113 113 __NORETURN static void
114 114 terminate(int status)
115 115 {
116 116 (void) mdb_signal_blockall();
117 117 mdb_destroy();
118 118 exit(status);
119 119 }
120 120
121 121 static void
122 122 print_frame(uintptr_t pc, int fnum)
123 123 {
124 124 Dl_info dli;
125 125
126 126 if (dladdr((void *)pc, &dli)) {
127 127 mdb_iob_printf(mdb.m_err, " [%d] %s`%s+0x%lx()\n", fnum,
128 128 strbasename(dli.dli_fname), dli.dli_sname,
129 129 pc - (uintptr_t)dli.dli_saddr);
130 130 } else
131 131 mdb_iob_printf(mdb.m_err, " [%d] %p()\n", fnum, pc);
132 132 }
133 133
134 134 static int
135 135 valid_frame(struct frame *fr)
136 136 {
137 137 static struct frame fake;
138 138 uintptr_t addr = (uintptr_t)fr;
139 139
140 140 if (pread(_mdb_self_fd, &fake, sizeof (fake), addr) != sizeof (fake)) {
141 141 mdb_iob_printf(mdb.m_err, " invalid frame (%p)\n", fr);
142 142 return (0);
143 143 }
144 144
145 145 if (addr & (STACK_ALIGN - 1)) {
146 146 mdb_iob_printf(mdb.m_err, " mis-aligned frame (%p)\n", fr);
147 147 return (0);
148 148 }
149 149
150 150 return (1);
151 151 }
152 152
153 153 /*ARGSUSED*/
154 154 static void
155 155 flt_handler(int sig, siginfo_t *sip, ucontext_t *ucp, void *data)
156 156 {
157 157 static const struct rlimit rl = {
158 158 (rlim_t)RLIM_INFINITY, (rlim_t)RLIM_INFINITY
159 159 };
160 160
161 161 const mdb_idcmd_t *idcp = NULL;
162 162
163 163 if (mdb.m_frame != NULL && mdb.m_frame->f_cp != NULL)
164 164 idcp = mdb.m_frame->f_cp->c_dcmd;
165 165
166 166 if (sip != NULL)
167 167 bcopy(sip, &_mdb_abort_info, sizeof (_mdb_abort_info));
168 168 if (ucp != NULL)
169 169 bcopy(ucp, &_mdb_abort_ctx, sizeof (_mdb_abort_ctx));
170 170
171 171 _mdb_abort_info.si_signo = sig;
172 172 (void) mdb_signal_sethandler(sig, SIG_DFL, NULL);
173 173
174 174 /*
175 175 * If there is no current dcmd, or the current dcmd comes from a
176 176 * builtin module, we don't allow resume and always core dump.
177 177 */
178 178 if (idcp == NULL || idcp->idc_modp == NULL ||
179 179 idcp->idc_modp == &mdb.m_rmod || idcp->idc_modp->mod_hdl == NULL)
180 180 goto dump;
181 181
182 182 if (mdb.m_term != NULL) {
183 183 struct frame *fr = (struct frame *)
184 184 (ucp->uc_mcontext.gregs[STACK_REGISTER] + STACK_BIAS);
185 185
186 186 char signame[SIG2STR_MAX];
187 187 int i = 1;
188 188 char c;
189 189
190 190 if (sig2str(sig, signame) == -1) {
191 191 mdb_iob_printf(mdb.m_err,
192 192 "\n*** %s: received signal %d at:\n",
193 193 mdb.m_pname, sig);
194 194 } else {
195 195 mdb_iob_printf(mdb.m_err,
196 196 "\n*** %s: received signal %s at:\n",
197 197 mdb.m_pname, signame);
198 198 }
199 199
200 200 if (ucp->uc_mcontext.gregs[REG_PC] != 0)
201 201 print_frame(ucp->uc_mcontext.gregs[REG_PC], i++);
202 202
203 203 while (fr != NULL && valid_frame(fr) && fr->fr_savpc != 0) {
204 204 print_frame(fr->fr_savpc, i++);
205 205 fr = (struct frame *)
206 206 ((uintptr_t)fr->fr_savfp + STACK_BIAS);
207 207 }
208 208
209 209 query:
210 210 mdb_iob_printf(mdb.m_err, "\n%s: (c)ore dump, (q)uit, "
211 211 "(r)ecover, or (s)top for debugger [cqrs]? ", mdb.m_pname);
212 212
213 213 mdb_iob_flush(mdb.m_err);
214 214
215 215 for (;;) {
216 216 if (IOP_READ(mdb.m_term, &c, sizeof (c)) != sizeof (c))
217 217 goto dump;
218 218
219 219 switch (c) {
220 220 case 'c':
221 221 case 'C':
222 222 (void) setrlimit(RLIMIT_CORE, &rl);
223 223 mdb_iob_printf(mdb.m_err, "\n%s: attempting "
224 224 "to dump core ...\n", mdb.m_pname);
225 225 goto dump;
226 226
227 227 case 'q':
228 228 case 'Q':
229 229 mdb_iob_discard(mdb.m_out);
230 230 mdb_iob_nl(mdb.m_err);
231 231 (void) mdb_signal_unblockall();
232 232 terminate(1);
233 233 /*NOTREACHED*/
234 234
235 235 case 'r':
236 236 case 'R':
237 237 mdb_iob_printf(mdb.m_err, "\n%s: unloading "
238 238 "module '%s' ...\n", mdb.m_pname,
239 239 idcp->idc_modp->mod_name);
240 240
241 241 (void) mdb_module_unload(
242 242 idcp->idc_modp->mod_name, 0);
243 243
244 244 (void) mdb_signal_sethandler(sig,
245 245 flt_handler, NULL);
246 246
247 247 _mdb_abort_rcount++;
248 248 mdb.m_intr = 0;
249 249 mdb.m_pend = 0;
250 250
251 251 (void) mdb_signal_unblockall();
252 252 longjmp(mdb.m_frame->f_pcb, MDB_ERR_ABORT);
253 253 /*NOTREACHED*/
254 254
255 255 case 's':
256 256 case 'S':
257 257 mdb_iob_printf(mdb.m_err, "\n%s: "
258 258 "attempting to stop pid %d ...\n",
259 259 mdb.m_pname, (int)getpid());
260 260
261 261 /*
262 262 * Stop ourself; if this fails or we are
263 263 * subsequently continued, ask again.
264 264 */
265 265 (void) mdb_signal_raise(SIGSTOP);
266 266 (void) mdb_signal_unblockall();
267 267 goto query;
268 268 }
269 269 }
270 270 }
271 271
272 272 dump:
273 273 if (SI_FROMUSER(sip)) {
274 274 (void) mdb_signal_block(sig);
275 275 (void) mdb_signal_raise(sig);
276 276 }
277 277
278 278 (void) sigfillset(&ucp->uc_sigmask);
279 279 (void) sigdelset(&ucp->uc_sigmask, sig);
280 280
281 281 if (_mdb_abort_str == NULL)
282 282 _mdb_abort_str = "fatal signal received";
283 283
284 284 ucp->uc_flags |= UC_SIGMASK;
285 285 (void) setcontext(ucp);
286 286 }
287 287
288 288 /*ARGSUSED*/
289 289 static void
290 290 int_handler(int sig, siginfo_t *sip, ucontext_t *ucp, void *data)
291 291 {
292 292 if (mdb.m_intr == 0)
293 293 longjmp(mdb.m_frame->f_pcb, MDB_ERR_SIGINT);
294 294 else
295 295 mdb.m_pend++;
296 296 }
297 297
298 298 static void
299 299 control_kmdb(int start)
300 300 {
301 301 int fd;
302 302
303 303 if ((fd = open("/dev/kmdb", O_RDONLY)) < 0)
304 304 die("failed to open /dev/kmdb");
305 305
306 306 if (start) {
307 307 char *state = mdb_get_config();
308 308
309 309 if (ioctl(fd, KMDB_IOC_START, state) < 0)
310 310 die("failed to start kmdb");
311 311
312 312 strfree(state);
313 313 } else {
314 314 if (ioctl(fd, KMDB_IOC_STOP) < 0)
315 315 die("failed to stop kmdb");
316 316 }
317 317
318 318 (void) close(fd);
319 319 }
320 320
321 321 static void
322 322 usage(int status)
323 323 {
324 324 mdb_iob_printf(mdb.m_err, "Usage: %s [-fkmuwyAFKMSUW] [+/-o option] "
325 325 "[-p pid] [-s dist] [-I path] [-L path]\n\t[-P prompt] "
326 326 "[-R root] [-V dis-version] [-e expr] "
327 327 "[object [core] | core | suffix]\n\n",
328 328 mdb.m_pname);
329 329
330 330 mdb_iob_puts(mdb.m_err,
331 331 "\t-e evaluate expr and return status\n"
332 332 "\t-f force raw file debugging mode\n"
333 333 "\t-k force kernel debugging mode\n"
334 334 "\t-m disable demand-loading of module symbols\n"
335 335 "\t-o set specified debugger option (+o to unset)\n"
336 336 "\t-p attach to specified process-id\n"
337 337 "\t-s set symbol matching distance\n"
338 338 "\t-u force user program debugging mode\n"
339 339 "\t-w enable write mode\n"
340 340 "\t-y send terminal initialization sequences for tty mode\n"
341 341 "\t-A disable automatic loading of mdb modules\n"
342 342 "\t-F enable forcible takeover mode\n"
343 343 "\t-K stop operating system and enter live kernel debugger\n"
344 344 "\t-M preload all module symbols\n"
345 345 "\t-I set initial path for macro files\n"
346 346 "\t-L set initial path for module libs\n"
347 347 "\t-P set command-line prompt\n"
348 348 "\t-R set root directory for pathname expansion\n"
349 349 "\t-S suppress processing of ~/.mdbrc file\n"
350 350 "\t-U unload live kernel debugger\n"
351 351 "\t-W enable I/O-mapped memory access (kernel only)\n"
352 352 "\t-V set disassembler version\n");
353 353
354 354 terminate(status);
355 355 }
356 356
357 357 static char *
358 358 mdb_scf_console_term(void)
359 359 {
360 360 scf_simple_prop_t *prop;
361 361 char *term = NULL;
362 362
363 363 if ((prop = scf_simple_prop_get(NULL,
364 364 "svc:/system/console-login:default", "ttymon",
365 365 "terminal_type")) == NULL)
366 366 return (NULL);
367 367
368 368 if (scf_simple_prop_type(prop) == SCF_TYPE_ASTRING &&
369 369 (term = scf_simple_prop_next_astring(prop)) != NULL)
370 370 term = strdup(term);
371 371
372 372 scf_simple_prop_free(prop);
373 373 return (term);
374 374 }
375 375
376 376 /*
377 377 * Unpleasant hack: we might be debugging a hypervisor domain dump.
378 378 * Earlier versions use a non-ELF file. Later versions are ELF, but are
379 379 * /always/ ELF64, so our standard ehdr check isn't good enough. Since
380 380 * we don't want to know too much about the file format, we'll ask
381 381 * mdb_kb.
382 382 */
383 383 #ifdef __x86
384 384 static int
385 385 identify_xvm_file(const char *file, int *longmode)
386 386 {
387 387 int (*identify)(const char *, int *);
388 388
389 389 if (mdb_module_load("mdb_kb", MDB_MOD_GLOBAL | MDB_MOD_SILENT) != 0)
390 390 return (0);
391 391
392 392 identify = (int (*)())dlsym(RTLD_NEXT, "xkb_identify");
393 393
394 394 if (identify == NULL)
395 395 return (0);
396 396
397 397 return (identify(file, longmode));
398 398 }
399 399 #else
400 400 /*ARGSUSED*/
401 401 static int
402 402 identify_xvm_file(const char *file, int *longmode)
403 403 {
404 404 return (0);
405 405 }
406 406 #endif /* __x86 */
407 407
408 408 int
409 409 main(int argc, char *argv[], char *envp[])
410 410 {
411 411 extern int mdb_kvm_is_compressed_dump(mdb_io_t *);
412 412 extern int mdb_kvm_is_dump(mdb_io_t *);
413 413 mdb_tgt_ctor_f *tgt_ctor = NULL;
414 414 const char **tgt_argv = alloca((argc + 2) * sizeof (char *));
415 415 int tgt_argc = 0;
416 416 mdb_tgt_t *tgt;
417 417
418 418 char object[MAXPATHLEN], execname[MAXPATHLEN];
419 419 mdb_io_t *in_io, *out_io, *err_io, *null_io;
420 420 struct termios tios;
421 421 int status, c;
422 422 char *p;
423 423
424 424 const char *Iflag = NULL, *Lflag = NULL, *Vflag = NULL, *pidarg = NULL;
425 425 const char *eflag = NULL;
426 426 int fflag = 0, Kflag = 0, Rflag = 0, Sflag = 0, Oflag = 0, Uflag = 0;
427 427
428 428 int ttylike;
429 429 int longmode = 0;
430 430
431 431 stack_t sigstack;
432 432
433 433 if (realpath(getexecname(), execname) == NULL) {
434 434 (void) strncpy(execname, argv[0], MAXPATHLEN);
435 435 execname[MAXPATHLEN - 1] = '\0';
436 436 }
437 437
438 438 mdb_create(execname, argv[0]);
439 439 bzero(tgt_argv, argc * sizeof (char *));
440 440 argv[0] = (char *)mdb.m_pname;
441 441 _mdb_self_fd = open("/proc/self/as", O_RDONLY);
442 442
443 443 mdb.m_env = envp;
444 444
445 445 out_io = mdb_fdio_create(STDOUT_FILENO);
446 446 mdb.m_out = mdb_iob_create(out_io, MDB_IOB_WRONLY);
447 447
448 448 err_io = mdb_fdio_create(STDERR_FILENO);
449 449 mdb.m_err = mdb_iob_create(err_io, MDB_IOB_WRONLY);
450 450 mdb_iob_clrflags(mdb.m_err, MDB_IOB_AUTOWRAP);
451 451
452 452 null_io = mdb_nullio_create();
453 453 mdb.m_null = mdb_iob_create(null_io, MDB_IOB_WRONLY);
454 454
455 455 in_io = mdb_fdio_create(STDIN_FILENO);
456 456 if ((mdb.m_termtype = getenv("TERM")) != NULL) {
457 457 mdb.m_termtype = strdup(mdb.m_termtype);
458 458 mdb.m_flags |= MDB_FL_TERMGUESS;
459 459 }
460 460 mdb.m_term = NULL;
461 461
462 462 mdb_dmode(mdb_dstr2mode(getenv("MDB_DEBUG")));
463 463 mdb.m_pgid = getpgrp();
464 464
465 465 if (getenv("_MDB_EXEC") != NULL)
466 466 mdb.m_flags |= MDB_FL_EXEC;
467 467
468 468 /*
469 469 * Setup an alternate signal stack. When tearing down pipelines in
470 470 * terminate(), we may have to destroy the stack of the context in
471 471 * which we are currently executing the signal handler.
472 472 */
473 473 sigstack.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE,
474 474 MAP_PRIVATE | MAP_ANON, -1, 0);
475 475 if (sigstack.ss_sp == MAP_FAILED)
476 476 die("could not allocate signal stack");
477 477 sigstack.ss_size = SIGSTKSZ;
478 478 sigstack.ss_flags = 0;
479 479 if (sigaltstack(&sigstack, NULL) != 0)
480 480 die("could not set signal stack");
481 481
482 482 (void) mdb_signal_sethandler(SIGPIPE, SIG_IGN, NULL);
483 483 (void) mdb_signal_sethandler(SIGQUIT, SIG_IGN, NULL);
484 484
485 485 (void) mdb_signal_sethandler(SIGILL, flt_handler, NULL);
486 486 (void) mdb_signal_sethandler(SIGTRAP, flt_handler, NULL);
487 487 (void) mdb_signal_sethandler(SIGIOT, flt_handler, NULL);
488 488 (void) mdb_signal_sethandler(SIGEMT, flt_handler, NULL);
489 489 (void) mdb_signal_sethandler(SIGFPE, flt_handler, NULL);
490 490 (void) mdb_signal_sethandler(SIGBUS, flt_handler, NULL);
491 491 (void) mdb_signal_sethandler(SIGSEGV, flt_handler, NULL);
492 492
493 493 (void) mdb_signal_sethandler(SIGHUP, (mdb_signal_f *)terminate, NULL);
494 494 (void) mdb_signal_sethandler(SIGTERM, (mdb_signal_f *)terminate, NULL);
495 495
496 496 for (mdb.m_rdvers = RD_VERSION; mdb.m_rdvers > 0; mdb.m_rdvers--) {
497 497 if (rd_init(mdb.m_rdvers) == RD_OK)
498 498 break;
499 499 }
500 500
501 501 for (mdb.m_ctfvers = CTF_VERSION; mdb.m_ctfvers > 0; mdb.m_ctfvers--) {
502 502 if (ctf_version(mdb.m_ctfvers) != -1)
503 503 break;
504 504 }
505 505
506 506 if ((p = getenv("HISTSIZE")) != NULL && strisnum(p)) {
507 507 mdb.m_histlen = strtoi(p);
508 508 if (mdb.m_histlen < 1)
509 509 mdb.m_histlen = 1;
510 510 }
511 511
512 512 while (optind < argc) {
513 513 while ((c = getopt(argc, argv,
514 514 "e:fkmo:p:s:uwyACD:FI:KL:MOP:R:SUV:W")) != (int)EOF) {
515 515 switch (c) {
516 516 case 'e':
517 517 if (eflag != NULL) {
518 518 warn("-e already specified\n");
519 519 terminate(2);
520 520 }
521 521 eflag = optarg;
522 522 break;
523 523 case 'f':
524 524 fflag++;
525 525 tgt_ctor = mdb_rawfile_tgt_create;
526 526 break;
527 527 case 'k':
528 528 tgt_ctor = mdb_kvm_tgt_create;
529 529 break;
530 530 case 'm':
531 531 mdb.m_tgtflags |= MDB_TGT_F_NOLOAD;
532 532 mdb.m_tgtflags &= ~MDB_TGT_F_PRELOAD;
533 533 break;
534 534 case 'o':
535 535 if (!mdb_set_options(optarg, TRUE))
536 536 terminate(2);
537 537 break;
538 538 case 'p':
539 539 tgt_ctor = mdb_proc_tgt_create;
540 540 pidarg = optarg;
541 541 break;
542 542 case 's':
543 543 if (!strisnum(optarg)) {
544 544 warn("expected integer following -s\n");
545 545 terminate(2);
546 546 }
547 547 mdb.m_symdist = (size_t)(uint_t)strtoi(optarg);
548 548 break;
549 549 case 'u':
550 550 tgt_ctor = mdb_proc_tgt_create;
551 551 break;
552 552 case 'w':
553 553 mdb.m_tgtflags |= MDB_TGT_F_RDWR;
554 554 break;
555 555 case 'y':
556 556 mdb.m_flags |= MDB_FL_USECUP;
557 557 break;
558 558 case 'A':
559 559 (void) mdb_set_options("nomods", TRUE);
560 560 break;
561 561 case 'C':
562 562 (void) mdb_set_options("noctf", TRUE);
563 563 break;
564 564 case 'D':
565 565 mdb_dmode(mdb_dstr2mode(optarg));
566 566 break;
567 567 case 'F':
568 568 mdb.m_tgtflags |= MDB_TGT_F_FORCE;
569 569 break;
570 570 case 'I':
571 571 Iflag = optarg;
572 572 break;
573 573 case 'L':
574 574 Lflag = optarg;
575 575 break;
576 576 case 'K':
577 577 Kflag++;
578 578 break;
579 579 case 'M':
580 580 mdb.m_tgtflags |= MDB_TGT_F_PRELOAD;
581 581 mdb.m_tgtflags &= ~MDB_TGT_F_NOLOAD;
582 582 break;
583 583 case 'O':
584 584 Oflag++;
585 585 break;
586 586 case 'P':
587 587 if (!mdb_set_prompt(optarg))
588 588 terminate(2);
589 589 break;
590 590 case 'R':
591 591 (void) strncpy(mdb.m_root, optarg, MAXPATHLEN);
592 592 mdb.m_root[MAXPATHLEN - 1] = '\0';
593 593 Rflag++;
594 594 break;
595 595 case 'S':
596 596 Sflag++;
597 597 break;
598 598 case 'U':
599 599 Uflag++;
600 600 break;
601 601 case 'V':
602 602 Vflag = optarg;
603 603 break;
604 604 case 'W':
605 605 mdb.m_tgtflags |= MDB_TGT_F_ALLOWIO;
606 606 break;
607 607 case '?':
608 608 if (optopt == '?')
609 609 usage(0);
610 610 /* FALLTHROUGH */
611 611 default:
612 612 usage(2);
613 613 }
614 614 }
615 615
616 616 if (optind < argc) {
617 617 const char *arg = argv[optind++];
618 618
619 619 if (arg[0] == '+' && strlen(arg) == 2) {
620 620 if (arg[1] != 'o') {
621 621 warn("illegal option -- %s\n", arg);
622 622 terminate(2);
623 623 }
624 624 if (optind >= argc) {
625 625 warn("option requires an argument -- "
626 626 "%s\n", arg);
627 627 terminate(2);
628 628 }
629 629 if (!mdb_set_options(argv[optind++], FALSE))
630 630 terminate(2);
631 631 } else
632 632 tgt_argv[tgt_argc++] = arg;
633 633 }
634 634 }
635 635
636 636 if (rd_ctl(RD_CTL_SET_HELPPATH, (void *)mdb.m_root) != RD_OK) {
637 637 warn("cannot set librtld_db helper path to %s\n", mdb.m_root);
638 638 terminate(2);
639 639 }
640 640
641 641 if (mdb.m_debug & MDB_DBG_HELP)
642 642 terminate(0); /* Quit here if we've printed out the tokens */
643 643
644 644
645 645 if (Iflag != NULL && strchr(Iflag, ';') != NULL) {
646 646 warn("macro path cannot contain semicolons\n");
647 647 terminate(2);
648 648 }
649 649
650 650 if (Lflag != NULL && strchr(Lflag, ';') != NULL) {
651 651 warn("module path cannot contain semicolons\n");
652 652 terminate(2);
653 653 }
654 654
655 655 if (Kflag || Uflag) {
656 656 char *nm;
657 657
658 658 if (tgt_ctor != NULL || Iflag != NULL) {
659 659 warn("neither -f, -k, -p, -u, nor -I "
660 660 "may be used with -K\n");
661 661 usage(2);
662 662 }
663 663
664 664 if (Lflag != NULL)
665 665 mdb_set_lpath(Lflag);
666 666
667 667 if ((nm = ttyname(STDIN_FILENO)) == NULL ||
668 668 strcmp(nm, "/dev/console") != 0) {
669 669 /*
670 670 * Due to the consequences of typing mdb -K instead of
671 671 * mdb -k on a tty other than /dev/console, we require
672 672 * -F when starting kmdb from a tty other than
673 673 * /dev/console.
674 674 */
675 675 if (!(mdb.m_tgtflags & MDB_TGT_F_FORCE)) {
676 676 die("-F must also be supplied to start kmdb "
677 677 "from non-console tty\n");
678 678 }
679 679
680 680 if (mdb.m_termtype == NULL || (mdb.m_flags &
681 681 MDB_FL_TERMGUESS)) {
682 682 if (mdb.m_termtype != NULL)
683 683 strfree(mdb.m_termtype);
684 684
685 685 if ((mdb.m_termtype = mdb_scf_console_term()) !=
686 686 NULL)
687 687 mdb.m_flags |= MDB_FL_TERMGUESS;
688 688 }
689 689 } else {
690 690 /*
691 691 * When on console, $TERM (if set) takes precedence over
692 692 * the SMF setting.
693 693 */
694 694 if (mdb.m_termtype == NULL && (mdb.m_termtype =
695 695 mdb_scf_console_term()) != NULL)
696 696 mdb.m_flags |= MDB_FL_TERMGUESS;
697 697 }
698 698
699 699 control_kmdb(Kflag);
700 700 terminate(0);
701 701 /*NOTREACHED*/
702 702 }
703 703
704 704 if (eflag != NULL) {
705 705 IOP_CLOSE(in_io);
706 706 in_io = mdb_strio_create(eflag);
707 707 mdb.m_lastret = 0;
708 708 }
709 709
710 710 /*
711 711 * If standard input appears to have tty attributes, attempt to
712 712 * initialize a terminal i/o backend on top of stdin and stdout.
713 713 */
714 714 ttylike = (IOP_CTL(in_io, TCGETS, &tios) == 0);
715 715 if (ttylike) {
716 716 if ((mdb.m_term = mdb_termio_create(mdb.m_termtype,
717 717 in_io, out_io)) == NULL) {
718 718 if (!(mdb.m_flags & MDB_FL_EXEC)) {
719 719 warn("term init failed: command-line editing "
720 720 "and prompt will not be available\n");
721 721 }
722 722 } else {
723 723 in_io = mdb.m_term;
724 724 }
725 725 }
726 726
727 727 mdb.m_in = mdb_iob_create(in_io, MDB_IOB_RDONLY);
728 728 if (mdb.m_term != NULL) {
729 729 mdb_iob_setpager(mdb.m_out, mdb.m_term);
730 730 if (mdb.m_flags & MDB_FL_PAGER)
731 731 mdb_iob_setflags(mdb.m_out, MDB_IOB_PGENABLE);
732 732 else
733 733 mdb_iob_clrflags(mdb.m_out, MDB_IOB_PGENABLE);
734 734 } else if (ttylike)
735 735 mdb_iob_setflags(mdb.m_in, MDB_IOB_TTYLIKE);
736 736 else
737 737 mdb_iob_setbuf(mdb.m_in, mdb_alloc(1, UM_SLEEP), 1);
738 738
739 739 mdb_pservice_init();
740 740 mdb_lex_reset();
741 741
742 742 if ((mdb.m_shell = getenv("SHELL")) == NULL)
743 743 mdb.m_shell = "/bin/sh";
744 744
745 745 /*
746 746 * If the debugger state is to be inherited from a previous instance,
747 747 * restore it now prior to path evaluation so that %R is updated.
748 748 */
749 749 if ((p = getenv(MDB_CONFIG_ENV_VAR)) != NULL) {
750 750 mdb_set_config(p);
751 751 (void) unsetenv(MDB_CONFIG_ENV_VAR);
752 752 }
753 753
754 754 /*
755 755 * Path evaluation part 1: Create the initial module path to allow
756 756 * the target constructor to load a support module. Then expand
757 757 * any command-line arguments that modify the paths.
758 758 */
759 759 if (Iflag != NULL)
760 760 mdb_set_ipath(Iflag);
761 761 else
762 762 mdb_set_ipath(MDB_DEF_IPATH);
763 763
764 764 if (Lflag != NULL)
765 765 mdb_set_lpath(Lflag);
766 766 else
767 767 mdb_set_lpath(MDB_DEF_LPATH);
768 768
769 769 if (mdb_get_prompt() == NULL && !(mdb.m_flags & MDB_FL_ADB))
770 770 (void) mdb_set_prompt(MDB_DEF_PROMPT);
771 771
772 772 if (tgt_ctor == mdb_kvm_tgt_create) {
773 773 if (pidarg != NULL) {
774 774 warn("-p and -k options are mutually exclusive\n");
775 775 terminate(2);
776 776 }
777 777
778 778 if (tgt_argc == 0)
779 779 tgt_argv[tgt_argc++] = "/dev/ksyms";
780 780 if (tgt_argc == 1 && strisnum(tgt_argv[0]) == 0) {
781 781 if (mdb.m_tgtflags & MDB_TGT_F_ALLOWIO)
782 782 tgt_argv[tgt_argc++] = "/dev/allkmem";
783 783 else
784 784 tgt_argv[tgt_argc++] = "/dev/kmem";
785 785 }
786 786 }
787 787
788 788 if (pidarg != NULL) {
789 789 if (tgt_argc != 0) {
790 790 warn("-p may not be used with other arguments\n");
791 791 terminate(2);
792 792 }
793 793 if (proc_arg_psinfo(pidarg, PR_ARG_PIDS, NULL, &status) == -1) {
794 794 die("cannot attach to %s: %s\n",
795 795 pidarg, Pgrab_error(status));
796 796 }
797 797 if (strchr(pidarg, '/') != NULL)
798 798 (void) mdb_iob_snprintf(object, MAXPATHLEN,
799 799 "%s/object/a.out", pidarg);
800 800 else
801 801 (void) mdb_iob_snprintf(object, MAXPATHLEN,
802 802 "/proc/%s/object/a.out", pidarg);
803 803 tgt_argv[tgt_argc++] = object;
804 804 tgt_argv[tgt_argc++] = pidarg;
805 805 }
806 806
807 807 /*
808 808 * Find the first argument that is not a special "-" token. If one is
809 809 * found, we will examine this file and make some inferences below.
810 810 */
811 811 for (c = 0; c < tgt_argc && strcmp(tgt_argv[c], "-") == 0; c++)
812 812 continue;
813 813
814 814 if (c < tgt_argc) {
815 815 Elf32_Ehdr ehdr;
816 816 mdb_io_t *io;
817 817
818 818 /*
819 819 * If special "-" tokens preceded an argument, shift the entire
820 820 * argument list to the left to remove the leading "-" args.
821 821 */
822 822 if (c > 0) {
823 823 bcopy(&tgt_argv[c], tgt_argv,
824 824 sizeof (const char *) * (tgt_argc - c));
825 825 tgt_argc -= c;
826 826 }
827 827
828 828 if (fflag)
829 829 goto tcreate; /* skip re-exec and just create target */
830 830
831 831 /*
832 832 * If we just have an object file name, and that file doesn't
833 833 * exist, and it's a string of digits, infer it to be a
834 834 * sequence number referring to a pair of crash dump files.
835 835 */
836 836 if (tgt_argc == 1 && access(tgt_argv[0], F_OK) == -1 &&
837 837 strisnum(tgt_argv[0])) {
838 838
839 839 size_t len = strlen(tgt_argv[0]) + 8;
840 840 const char *object = tgt_argv[0];
841 841
842 842 tgt_argv[0] = alloca(len);
843 843 tgt_argv[1] = alloca(len);
844 844
845 845 (void) strcpy((char *)tgt_argv[0], "unix.");
846 846 (void) strcat((char *)tgt_argv[0], object);
847 847 (void) strcpy((char *)tgt_argv[1], "vmcore.");
848 848 (void) strcat((char *)tgt_argv[1], object);
849 849
850 850 if (access(tgt_argv[0], F_OK) == -1 &&
851 851 access(tgt_argv[1], F_OK) != -1) {
852 852 /*
853 853 * If we have a vmcore but not a unix file,
854 854 * set the symbol table to be the vmcore to
855 855 * force libkvm to extract it out of the dump.
856 856 */
857 857 tgt_argv[0] = tgt_argv[1];
858 858 } else if (access(tgt_argv[0], F_OK) == -1 &&
859 859 access(tgt_argv[1], F_OK) == -1) {
860 860 (void) strcpy((char *)tgt_argv[1], "vmdump.");
861 861 (void) strcat((char *)tgt_argv[1], object);
862 862 if (access(tgt_argv[1], F_OK) == 0) {
863 863 mdb_iob_printf(mdb.m_err,
864 864 "cannot open compressed dump; "
865 865 "decompress using savecore -f %s\n",
866 866 tgt_argv[1]);
867 867 terminate(0);
868 868 }
869 869 }
870 870
871 871 tgt_argc = 2;
872 872 }
873 873
874 874 /*
875 875 * We need to open the object file in order to determine its
876 876 * ELF class and potentially re-exec ourself.
877 877 */
878 878 if ((io = mdb_fdio_create_path(NULL, tgt_argv[0],
879 879 O_RDONLY, 0)) == NULL)
880 880 die("failed to open %s", tgt_argv[0]);
881 881
882 882 if (tgt_argc == 1) {
883 883 if (mdb_kvm_is_compressed_dump(io)) {
884 884 /*
885 885 * We have a single vmdump.N compressed dump
886 886 * file; give a helpful message.
887 887 */
888 888 mdb_iob_printf(mdb.m_err,
889 889 "cannot open compressed dump; "
890 890 "decompress using savecore -f %s\n",
891 891 tgt_argv[0]);
892 892 terminate(0);
893 893 } else if (mdb_kvm_is_dump(io)) {
894 894 /*
895 895 * We have an uncompressed dump as our only
896 896 * argument; specify the dump as the symbol
897 897 * table to force libkvm to dig it out of the
898 898 * dump.
899 899 */
900 900 tgt_argv[tgt_argc++] = tgt_argv[0];
901 901 }
902 902 }
903 903
904 904 /*
905 905 * If the target is unknown or is not the rawfile target, do
906 906 * a gelf_check to determine if the file is an ELF file. If
907 907 * it is not and the target is unknown, use the rawfile tgt.
908 908 * Otherwise an ELF-based target is needed, so we must abort.
909 909 */
910 910 if (mdb_gelf_check(io, &ehdr, ET_NONE) == -1) {
911 911 if (tgt_ctor != NULL) {
912 912 (void) mdb_gelf_check(io, &ehdr, ET_EXEC);
913 913 mdb_io_destroy(io);
914 914 terminate(1);
915 915 } else
916 916 tgt_ctor = mdb_rawfile_tgt_create;
917 917 }
918 918
919 919 mdb_io_destroy(io);
920 920
921 921 if (identify_xvm_file(tgt_argv[0], &longmode) == 1) {
922 922 #ifdef _LP64
923 923 if (!longmode)
924 924 goto reexec;
925 925 #else
926 926 if (longmode)
927 927 goto reexec;
928 928 #endif
929 929 tgt_ctor = mdb_kvm_tgt_create;
930 930 goto tcreate;
931 931 }
932 932
933 933 /*
934 934 * The object file turned out to be a user core file (ET_CORE),
935 935 * and no other arguments were specified, swap 0 and 1. The
936 936 * proc target will infer the executable for us.
937 937 */
938 938 if (ehdr.e_type == ET_CORE) {
939 939 tgt_argv[tgt_argc++] = tgt_argv[0];
940 940 tgt_argv[0] = NULL;
941 941 tgt_ctor = mdb_proc_tgt_create;
942 942 }
943 943
944 944 /*
945 945 * If tgt_argv[1] is filled in, open it up and determine if it
946 946 * is a vmcore file. If it is, gelf_check will fail and we
947 947 * set tgt_ctor to 'kvm'; otherwise we use the default.
948 948 */
949 949 if (tgt_argc > 1 && strcmp(tgt_argv[1], "-") != 0 &&
950 950 tgt_argv[0] != NULL && pidarg == NULL) {
951 951 Elf32_Ehdr chdr;
952 952
953 953 if (access(tgt_argv[1], F_OK) == -1)
954 954 die("failed to access %s", tgt_argv[1]);
955 955
956 956 /* *.N case: drop vmdump.N from the list */
957 957 if (tgt_argc == 3) {
958 958 if ((io = mdb_fdio_create_path(NULL,
959 959 tgt_argv[2], O_RDONLY, 0)) == NULL)
960 960 die("failed to open %s", tgt_argv[2]);
961 961 if (mdb_kvm_is_compressed_dump(io))
962 962 tgt_argv[--tgt_argc] = NULL;
963 963 mdb_io_destroy(io);
964 964 }
965 965
966 966 if ((io = mdb_fdio_create_path(NULL, tgt_argv[1],
967 967 O_RDONLY, 0)) == NULL)
968 968 die("failed to open %s", tgt_argv[1]);
969 969
970 970 if (mdb_gelf_check(io, &chdr, ET_NONE) == -1)
971 971 tgt_ctor = mdb_kvm_tgt_create;
972 972
973 973 mdb_io_destroy(io);
974 974 }
975 975
976 976 /*
977 977 * At this point, we've read the ELF header for either an
978 978 * object file or core into ehdr. If the class does not match
979 979 * ours, attempt to exec the mdb of the appropriate class.
980 980 */
981 981 #ifdef _LP64
982 982 if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
983 983 goto reexec;
984 984 #else
985 985 if (ehdr.e_ident[EI_CLASS] == ELFCLASS64)
986 986 goto reexec;
987 987 #endif
988 988 }
989 989
990 990 tcreate:
991 991 if (tgt_ctor == NULL)
992 992 tgt_ctor = mdb_proc_tgt_create;
993 993
994 994 tgt = mdb_tgt_create(tgt_ctor, mdb.m_tgtflags, tgt_argc, tgt_argv);
995 995
996 996 if (tgt == NULL) {
997 997 if (errno == EINVAL)
998 998 usage(2); /* target can return EINVAL to get usage */
999 999 if (errno == EMDB_TGT)
1000 1000 terminate(1); /* target already printed error msg */
1001 1001 die("failed to initialize target");
1002 1002 }
1003 1003
1004 1004 mdb_tgt_activate(tgt);
1005 1005
1006 1006 mdb_create_loadable_disasms();
1007 1007
1008 1008 if (Vflag != NULL && mdb_dis_select(Vflag) == -1)
1009 1009 warn("invalid disassembler mode -- %s\n", Vflag);
1010 1010
1011 1011
1012 1012 if (Rflag && mdb.m_term != NULL)
1013 1013 warn("Using proto area %s\n", mdb.m_root);
1014 1014
1015 1015 /*
1016 1016 * If the target was successfully constructed and -O was specified,
1017 1017 * we now attempt to enter piggy-mode for debugging jurassic problems.
1018 1018 */
1019 1019 if (Oflag) {
1020 1020 pcinfo_t pci;
1021 1021
1022 1022 (void) strcpy(pci.pc_clname, "RT");
1023 1023
1024 1024 if (priocntl(P_LWPID, P_MYID, PC_GETCID, (caddr_t)&pci) != -1) {
1025 1025 pcparms_t pcp;
1026 1026 rtparms_t *rtp = (rtparms_t *)pcp.pc_clparms;
1027 1027
1028 1028 rtp->rt_pri = 35;
1029 1029 rtp->rt_tqsecs = 0;
1030 1030 rtp->rt_tqnsecs = RT_TQDEF;
1031 1031
1032 1032 pcp.pc_cid = pci.pc_cid;
1033 1033
1034 1034 if (priocntl(P_LWPID, P_MYID, PC_SETPARMS,
1035 1035 (caddr_t)&pcp) == -1) {
1036 1036 warn("failed to set RT parameters");
1037 1037 Oflag = 0;
1038 1038 }
1039 1039 } else {
1040 1040 warn("failed to get RT class id");
1041 1041 Oflag = 0;
1042 1042 }
1043 1043
1044 1044 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
1045 1045 warn("failed to lock address space");
1046 1046 Oflag = 0;
1047 1047 }
1048 1048
1049 1049 if (Oflag)
1050 1050 mdb_printf("%s: oink, oink!\n", mdb.m_pname);
1051 1051 }
1052 1052
1053 1053 /*
1054 1054 * Path evaluation part 2: Re-evaluate the path now that the target
1055 1055 * is ready (and thus we have access to the real platform string).
1056 1056 * Do this before reading ~/.mdbrc to allow path modifications prior
1057 1057 * to performing module auto-loading.
1058 1058 */
1059 1059 mdb_set_ipath(mdb.m_ipathstr);
1060 1060 mdb_set_lpath(mdb.m_lpathstr);
1061 1061
1062 1062 if (!Sflag && (p = getenv("HOME")) != NULL) {
1063 1063 char rcpath[MAXPATHLEN];
1064 1064 mdb_io_t *rc_io;
1065 1065 int fd;
1066 1066
1067 1067 (void) mdb_iob_snprintf(rcpath, MAXPATHLEN, "%s/.mdbrc", p);
1068 1068 fd = open64(rcpath, O_RDONLY);
1069 1069
1070 1070 if (fd >= 0 && (rc_io = mdb_fdio_create_named(fd, rcpath))) {
1071 1071 mdb_iob_t *iob = mdb_iob_create(rc_io, MDB_IOB_RDONLY);
1072 1072 mdb_iob_t *old = mdb.m_in;
1073 1073
1074 1074 mdb.m_in = iob;
1075 1075 (void) mdb_run();
1076 1076 mdb.m_in = old;
1077 1077 }
1078 1078 }
1079 1079
1080 1080 if (!(mdb.m_flags & MDB_FL_NOMODS))
1081 1081 mdb_module_load_all(0);
1082 1082
1083 1083 (void) mdb_signal_sethandler(SIGINT, int_handler, NULL);
1084 1084 while ((status = mdb_run()) == MDB_ERR_ABORT ||
1085 1085 status == MDB_ERR_OUTPUT) {
1086 1086 /*
1087 1087 * If a write failed on stdout, give up. A more informative
1088 1088 * error message will already have been printed by mdb_run().
1089 1089 */
1090 1090 if (status == MDB_ERR_OUTPUT &&
↓ open down ↓ |
1052 lines elided |
↑ open up ↑ |
1091 1091 mdb_iob_getflags(mdb.m_out) & MDB_IOB_ERR) {
1092 1092 mdb_warn("write to stdout failed, exiting\n");
1093 1093 break;
1094 1094 }
1095 1095 continue;
1096 1096 }
1097 1097
1098 1098 terminate((status == MDB_ERR_QUIT || status == 0) ?
1099 1099 (eflag != NULL && mdb.m_lastret != 0 ? 1 : 0) : 1);
1100 1100 /*NOTREACHED*/
1101 - return (0);
1102 1101
1103 1102 reexec:
1104 1103 if ((p = strrchr(execname, '/')) == NULL)
1105 1104 die("cannot determine absolute pathname\n");
1106 1105 #ifdef _LP64
1107 1106 #ifdef __sparc
1108 1107 (void) strcpy(p, "/../sparcv7/");
1109 1108 #else
1110 1109 (void) strcpy(p, "/../i86/");
1111 1110 #endif
1112 1111 #else
1113 1112 #ifdef __sparc
1114 1113 (void) strcpy(p, "/../sparcv9/");
1115 1114 #else
1116 1115 (void) strcpy(p, "/../amd64/");
1117 1116 #endif
1118 1117 #endif
1119 1118 (void) strcat(p, mdb.m_pname);
1120 1119
1121 1120 if (mdb.m_term != NULL)
1122 1121 (void) IOP_CTL(in_io, TCSETSW, &tios);
1123 1122
1124 1123 (void) putenv("_MDB_EXEC=1");
1125 1124 (void) execv(execname, argv);
1126 1125
1127 1126 /*
1128 1127 * If execv fails, suppress ENOEXEC. Experience shows the most common
1129 1128 * reason is that the machine is booted under a 32-bit kernel, in which
1130 1129 * case it is clearer to only print the message below.
1131 1130 */
1132 1131 if (errno != ENOEXEC)
1133 1132 warn("failed to exec %s", execname);
1134 1133 #ifdef _LP64
1135 1134 die("64-bit %s cannot debug 32-bit program %s\n",
1136 1135 mdb.m_pname, tgt_argv[0] ?
1137 1136 tgt_argv[0] : tgt_argv[1]);
1138 1137 #else
1139 1138 die("32-bit %s cannot debug 64-bit program %s\n",
1140 1139 mdb.m_pname, tgt_argv[0] ?
1141 1140 tgt_argv[0] : tgt_argv[1]);
1142 1141 #endif
1143 1142
1144 1143 goto tcreate;
1145 1144 }
↓ open down ↓ |
34 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX