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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright (c) 2012, Joyent, Inc. All rights reserved. 28 */ 29 30 /* 31 * MDB uses its own enhanced standard i/o mechanism for all input and output. 32 * This file provides the underpinnings of this mechanism, including the 33 * printf-style formatting code, the output pager, and APIs for raw input 34 * and output. This mechanism is used throughout the debugger for everything 35 * from simple sprintf and printf-style formatting, to input to the lexer 36 * and parser, to raw file i/o for reading ELF files. In general, we divide 37 * our i/o implementation into two parts: 38 * 39 * (1) An i/o buffer (mdb_iob_t) provides buffered read or write capabilities, 40 * as well as access to formatting and the ability to invoke a pager. The 41 * buffer is constructed explicitly for use in either reading or writing; it 42 * may not be used for both simultaneously. 43 * 44 * (2) Each i/o buffer is associated with an underlying i/o backend (mdb_io_t). 45 * The backend provides, through an ops-vector, equivalents for the standard 46 * read, write, lseek, ioctl, and close operations. In addition, the backend 47 * can provide an IOP_NAME entry point for returning a name for the backend, 48 * IOP_LINK and IOP_UNLINK entry points that are called when the backend is 49 * connected or disconnected from an mdb_iob_t, and an IOP_SETATTR entry point 50 * for manipulating terminal attributes. 51 * 52 * The i/o objects themselves are reference counted so that more than one i/o 53 * buffer may make use of the same i/o backend. In addition, each buffer 54 * provides the ability to push or pop backends to interpose on input or output 55 * behavior. We make use of this, for example, to implement interactive 56 * session logging. Normally, the stdout iob has a backend that is either 57 * file descriptor 1, or a terminal i/o backend associated with the tty. 58 * However, we can push a log i/o backend on top that multiplexes stdout to 59 * the original back-end and another backend that writes to a log file. The 60 * use of i/o backends is also used for simplifying tasks such as making 61 * lex and yacc read from strings for mdb_eval(), and making our ELF file 62 * processing code read executable "files" from a crash dump via kvm_uread. 63 * 64 * Additionally, the formatting code provides auto-wrap and indent facilities 65 * that are necessary for compatibility with adb macro formatting. In auto- 66 * wrap mode, the formatting code examines each new chunk of output to determine 67 * if it will fit on the current line. If not, instead of having the chunk 68 * divided between the current line of output and the next, the auto-wrap 69 * code will automatically output a newline, auto-indent the next line, 70 * and then continue. Auto-indent is implemented by simply prepending a number 71 * of blanks equal to iob_margin to the start of each line. The margin is 72 * inserted when the iob is created, and following each flush of the buffer. 73 */ 74 75 #include <sys/types.h> 76 #include <sys/termios.h> 77 #include <stdarg.h> 78 #include <arpa/inet.h> 79 #include <sys/socket.h> 80 81 #include <mdb/mdb_types.h> 82 #include <mdb/mdb_argvec.h> 83 #include <mdb/mdb_stdlib.h> 84 #include <mdb/mdb_string.h> 85 #include <mdb/mdb_target.h> 86 #include <mdb/mdb_signal.h> 87 #include <mdb/mdb_debug.h> 88 #include <mdb/mdb_io_impl.h> 89 #include <mdb/mdb_modapi.h> 90 #include <mdb/mdb_demangle.h> 91 #include <mdb/mdb_err.h> 92 #include <mdb/mdb_nv.h> 93 #include <mdb/mdb_frame.h> 94 #include <mdb/mdb_lex.h> 95 #include <mdb/mdb.h> 96 97 /* 98 * Define list of possible integer sizes for conversion routines: 99 */ 100 typedef enum { 101 SZ_SHORT, /* format %h? */ 102 SZ_INT, /* format %? */ 103 SZ_LONG, /* format %l? */ 104 SZ_LONGLONG /* format %ll? */ 105 } intsize_t; 106 107 /* 108 * The iob snprintf family of functions makes use of a special "sprintf 109 * buffer" i/o backend in order to provide the appropriate snprintf semantics. 110 * This structure is maintained as the backend-specific private storage, 111 * and its use is described in more detail below (see spbuf_write()). 112 */ 113 typedef struct { 114 char *spb_buf; /* pointer to underlying buffer */ 115 size_t spb_bufsiz; /* length of underlying buffer */ 116 size_t spb_total; /* total of all bytes passed via IOP_WRITE */ 117 } spbuf_t; 118 119 /* 120 * Define VA_ARG macro for grabbing the next datum to format for the printf 121 * family of functions. We use VA_ARG so that we can support two kinds of 122 * argument lists: the va_list type supplied by <stdarg.h> used for printf and 123 * vprintf, and an array of mdb_arg_t structures, which we expect will be 124 * either type STRING or IMMEDIATE. The vec_arg function takes care of 125 * handling the mdb_arg_t case. 126 */ 127 128 typedef enum { 129 VAT_VARARGS, /* va_list is a va_list */ 130 VAT_ARGVEC /* va_list is a const mdb_arg_t[] in disguise */ 131 } vatype_t; 132 133 typedef struct { 134 vatype_t val_type; 135 union { 136 va_list _val_valist; 137 const mdb_arg_t *_val_argv; 138 } _val_u; 139 } varglist_t; 140 141 #define val_valist _val_u._val_valist 142 #define val_argv _val_u._val_argv 143 144 #define VA_ARG(ap, type) ((ap->val_type == VAT_VARARGS) ? \ 145 va_arg(ap->val_valist, type) : (type)vec_arg(&ap->val_argv)) 146 #define VA_PTRARG(ap) ((ap->val_type == VAT_VARARGS) ? \ 147 (void *)va_arg(ap->val_valist, uintptr_t) : \ 148 (void *)(uintptr_t)vec_arg(&ap->val_argv)) 149 150 /* 151 * Define macro for converting char constant to Ctrl-char equivalent: 152 */ 153 #ifndef CTRL 154 #define CTRL(c) ((c) & 0x01f) 155 #endif 156 157 /* 158 * Define macro for determining if we should automatically wrap to the next 159 * line of output, based on the amount of consumed buffer space and the 160 * specified size of the next thing to be inserted (n). 161 */ 162 #define IOB_WRAPNOW(iob, n) \ 163 (((iob)->iob_flags & MDB_IOB_AUTOWRAP) && ((iob)->iob_nbytes != 0) && \ 164 ((n) + (iob)->iob_nbytes > (iob)->iob_cols)) 165 166 /* 167 * Define prompt string and string to erase prompt string for iob_pager 168 * function, which is invoked if the pager is enabled on an i/o buffer 169 * and we're about to print a line which would be the last on the screen. 170 */ 171 172 static const char io_prompt[] = ">> More [<space>, <cr>, q, n, c, a] ? "; 173 static const char io_perase[] = " "; 174 175 static const char io_pbcksp[] = 176 /*CSTYLED*/ 177 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"; 178 179 static const size_t io_promptlen = sizeof (io_prompt) - 1; 180 static const size_t io_peraselen = sizeof (io_perase) - 1; 181 static const size_t io_pbcksplen = sizeof (io_pbcksp) - 1; 182 183 static ssize_t 184 iob_write(mdb_iob_t *iob, mdb_io_t *io, const void *buf, size_t n) 185 { 186 ssize_t resid = n; 187 ssize_t len; 188 189 while (resid != 0) { 190 if ((len = IOP_WRITE(io, buf, resid)) <= 0) 191 break; 192 193 buf = (char *)buf + len; 194 resid -= len; 195 } 196 197 /* 198 * Note that if we had a partial write before an error, we still want 199 * to return the fact something was written. The caller will get an 200 * error next time it tries to write anything. 201 */ 202 if (resid == n && n != 0) { 203 iob->iob_flags |= MDB_IOB_ERR; 204 return (-1); 205 } 206 207 return (n - resid); 208 } 209 210 static ssize_t 211 iob_read(mdb_iob_t *iob, mdb_io_t *io) 212 { 213 ssize_t len; 214 215 ASSERT(iob->iob_nbytes == 0); 216 len = IOP_READ(io, iob->iob_buf, iob->iob_bufsiz); 217 iob->iob_bufp = &iob->iob_buf[0]; 218 219 switch (len) { 220 case -1: 221 iob->iob_flags |= MDB_IOB_ERR; 222 break; 223 case 0: 224 iob->iob_flags |= MDB_IOB_EOF; 225 break; 226 default: 227 iob->iob_nbytes = len; 228 } 229 230 return (len); 231 } 232 233 /*ARGSUSED*/ 234 static void 235 iob_winch(int sig, siginfo_t *sip, ucontext_t *ucp, void *data) 236 { 237 siglongjmp(*((sigjmp_buf *)data), sig); 238 } 239 240 static int 241 iob_pager(mdb_iob_t *iob) 242 { 243 int status = 0; 244 sigjmp_buf env; 245 uchar_t c; 246 247 mdb_signal_f *termio_winch; 248 void *termio_data; 249 size_t old_rows; 250 251 if (iob->iob_pgp == NULL || (iob->iob_flags & MDB_IOB_PGCONT)) 252 return (0); 253 254 termio_winch = mdb_signal_gethandler(SIGWINCH, &termio_data); 255 (void) mdb_signal_sethandler(SIGWINCH, iob_winch, &env); 256 257 if (sigsetjmp(env, 1) != 0) { 258 /* 259 * Reset the cursor back to column zero before printing a new 260 * prompt, since its position is unreliable after a SIGWINCH. 261 */ 262 (void) iob_write(iob, iob->iob_pgp, "\r", sizeof (char)); 263 old_rows = iob->iob_rows; 264 265 /* 266 * If an existing SIGWINCH handler was present, call it. We 267 * expect that this will be termio: the handler will read the 268 * new window size, and then resize this iob appropriately. 269 */ 270 if (termio_winch != (mdb_signal_f *)NULL) 271 termio_winch(SIGWINCH, NULL, NULL, termio_data); 272 273 /* 274 * If the window has increased in size, we treat this like a 275 * request to fill out the new remainder of the page. 276 */ 277 if (iob->iob_rows > old_rows) { 278 iob->iob_flags &= ~MDB_IOB_PGSINGLE; 279 iob->iob_nlines = old_rows; 280 status = 0; 281 goto winch; 282 } 283 } 284 285 (void) iob_write(iob, iob->iob_pgp, io_prompt, io_promptlen); 286 287 for (;;) { 288 if (IOP_READ(iob->iob_pgp, &c, sizeof (c)) != sizeof (c)) { 289 status = MDB_ERR_PAGER; 290 break; 291 } 292 293 switch (c) { 294 case 'N': 295 case 'n': 296 case '\n': 297 case '\r': 298 iob->iob_flags |= MDB_IOB_PGSINGLE; 299 goto done; 300 301 case CTRL('c'): 302 case CTRL('\\'): 303 case 'Q': 304 case 'q': 305 mdb_iob_discard(iob); 306 status = MDB_ERR_PAGER; 307 goto done; 308 309 case 'A': 310 case 'a': 311 mdb_iob_discard(iob); 312 status = MDB_ERR_ABORT; 313 goto done; 314 315 case 'C': 316 case 'c': 317 iob->iob_flags |= MDB_IOB_PGCONT; 318 /*FALLTHRU*/ 319 320 case ' ': 321 iob->iob_flags &= ~MDB_IOB_PGSINGLE; 322 goto done; 323 } 324 } 325 326 done: 327 (void) iob_write(iob, iob->iob_pgp, io_pbcksp, io_pbcksplen); 328 winch: 329 (void) iob_write(iob, iob->iob_pgp, io_perase, io_peraselen); 330 (void) iob_write(iob, iob->iob_pgp, io_pbcksp, io_pbcksplen); 331 (void) mdb_signal_sethandler(SIGWINCH, termio_winch, termio_data); 332 333 if ((iob->iob_flags & MDB_IOB_ERR) && status == 0) 334 status = MDB_ERR_OUTPUT; 335 336 return (status); 337 } 338 339 static void 340 iob_indent(mdb_iob_t *iob) 341 { 342 if (iob->iob_nbytes == 0 && iob->iob_margin != 0 && 343 (iob->iob_flags & MDB_IOB_INDENT)) { 344 size_t i; 345 346 ASSERT(iob->iob_margin < iob->iob_cols); 347 ASSERT(iob->iob_bufp == iob->iob_buf); 348 349 for (i = 0; i < iob->iob_margin; i++) 350 *iob->iob_bufp++ = ' '; 351 352 iob->iob_nbytes = iob->iob_margin; 353 } 354 } 355 356 static void 357 iob_unindent(mdb_iob_t *iob) 358 { 359 if (iob->iob_nbytes != 0 && iob->iob_nbytes == iob->iob_margin) { 360 const char *p = iob->iob_buf; 361 362 while (p < &iob->iob_buf[iob->iob_margin]) { 363 if (*p++ != ' ') 364 return; 365 } 366 367 iob->iob_bufp = &iob->iob_buf[0]; 368 iob->iob_nbytes = 0; 369 } 370 } 371 372 mdb_iob_t * 373 mdb_iob_create(mdb_io_t *io, uint_t flags) 374 { 375 mdb_iob_t *iob = mdb_alloc(sizeof (mdb_iob_t), UM_SLEEP); 376 377 iob->iob_buf = mdb_alloc(BUFSIZ, UM_SLEEP); 378 iob->iob_bufsiz = BUFSIZ; 379 iob->iob_bufp = &iob->iob_buf[0]; 380 iob->iob_nbytes = 0; 381 iob->iob_nlines = 0; 382 iob->iob_lineno = 1; 383 iob->iob_rows = MDB_IOB_DEFROWS; 384 iob->iob_cols = MDB_IOB_DEFCOLS; 385 iob->iob_tabstop = MDB_IOB_DEFTAB; 386 iob->iob_margin = MDB_IOB_DEFMARGIN; 387 iob->iob_flags = flags & ~(MDB_IOB_EOF|MDB_IOB_ERR) | MDB_IOB_AUTOWRAP; 388 iob->iob_iop = mdb_io_hold(io); 389 iob->iob_pgp = NULL; 390 iob->iob_next = NULL; 391 392 IOP_LINK(io, iob); 393 iob_indent(iob); 394 return (iob); 395 } 396 397 void 398 mdb_iob_pipe(mdb_iob_t **iobs, mdb_iobsvc_f *rdsvc, mdb_iobsvc_f *wrsvc) 399 { 400 mdb_io_t *pio = mdb_pipeio_create(rdsvc, wrsvc); 401 int i; 402 403 iobs[0] = mdb_iob_create(pio, MDB_IOB_RDONLY); 404 iobs[1] = mdb_iob_create(pio, MDB_IOB_WRONLY); 405 406 for (i = 0; i < 2; i++) { 407 iobs[i]->iob_flags &= ~MDB_IOB_AUTOWRAP; 408 iobs[i]->iob_cols = iobs[i]->iob_bufsiz; 409 } 410 } 411 412 void 413 mdb_iob_destroy(mdb_iob_t *iob) 414 { 415 /* 416 * Don't flush a pipe, since it may cause a context swith when the 417 * other side has already been destroyed. 418 */ 419 if (!mdb_iob_isapipe(iob)) 420 mdb_iob_flush(iob); 421 422 if (iob->iob_pgp != NULL) 423 mdb_io_rele(iob->iob_pgp); 424 425 while (iob->iob_iop != NULL) { 426 IOP_UNLINK(iob->iob_iop, iob); 427 (void) mdb_iob_pop_io(iob); 428 } 429 430 mdb_free(iob->iob_buf, iob->iob_bufsiz); 431 mdb_free(iob, sizeof (mdb_iob_t)); 432 } 433 434 void 435 mdb_iob_discard(mdb_iob_t *iob) 436 { 437 iob->iob_bufp = &iob->iob_buf[0]; 438 iob->iob_nbytes = 0; 439 } 440 441 void 442 mdb_iob_flush(mdb_iob_t *iob) 443 { 444 int pgerr = 0; 445 446 if (iob->iob_nbytes == 0) 447 return; /* Nothing to do if buffer is empty */ 448 449 if (iob->iob_flags & MDB_IOB_WRONLY) { 450 if (iob->iob_flags & MDB_IOB_PGSINGLE) { 451 iob->iob_flags &= ~MDB_IOB_PGSINGLE; 452 iob->iob_nlines = 0; 453 pgerr = iob_pager(iob); 454 455 } else if (iob->iob_nlines >= iob->iob_rows - 1) { 456 iob->iob_nlines = 0; 457 if (iob->iob_flags & MDB_IOB_PGENABLE) 458 pgerr = iob_pager(iob); 459 } 460 461 if (pgerr == 0) { 462 /* 463 * We only jump out of the dcmd on error if the iob is 464 * m_out. Presumably, if a dcmd has opened a special 465 * file and is writing to it, it will handle errors 466 * properly. 467 */ 468 if (iob_write(iob, iob->iob_iop, iob->iob_buf, 469 iob->iob_nbytes) < 0 && iob == mdb.m_out) 470 pgerr = MDB_ERR_OUTPUT; 471 iob->iob_nlines++; 472 } 473 } 474 475 iob->iob_bufp = &iob->iob_buf[0]; 476 iob->iob_nbytes = 0; 477 iob_indent(iob); 478 479 if (pgerr) 480 longjmp(mdb.m_frame->f_pcb, pgerr); 481 } 482 483 void 484 mdb_iob_nlflush(mdb_iob_t *iob) 485 { 486 iob_unindent(iob); 487 488 if (iob->iob_nbytes != 0) 489 mdb_iob_nl(iob); 490 else 491 iob_indent(iob); 492 } 493 494 void 495 mdb_iob_push_io(mdb_iob_t *iob, mdb_io_t *io) 496 { 497 ASSERT(io->io_next == NULL); 498 499 io->io_next = iob->iob_iop; 500 iob->iob_iop = mdb_io_hold(io); 501 } 502 503 mdb_io_t * 504 mdb_iob_pop_io(mdb_iob_t *iob) 505 { 506 mdb_io_t *io = iob->iob_iop; 507 508 if (io != NULL) { 509 iob->iob_iop = io->io_next; 510 io->io_next = NULL; 511 mdb_io_rele(io); 512 } 513 514 return (io); 515 } 516 517 void 518 mdb_iob_resize(mdb_iob_t *iob, size_t rows, size_t cols) 519 { 520 if (cols > iob->iob_bufsiz) 521 iob->iob_cols = iob->iob_bufsiz; 522 else 523 iob->iob_cols = cols != 0 ? cols : MDB_IOB_DEFCOLS; 524 525 iob->iob_rows = rows != 0 ? rows : MDB_IOB_DEFROWS; 526 } 527 528 void 529 mdb_iob_setpager(mdb_iob_t *iob, mdb_io_t *pgio) 530 { 531 struct winsize winsz; 532 533 if (iob->iob_pgp != NULL) { 534 IOP_UNLINK(iob->iob_pgp, iob); 535 mdb_io_rele(iob->iob_pgp); 536 } 537 538 iob->iob_flags |= MDB_IOB_PGENABLE; 539 iob->iob_flags &= ~(MDB_IOB_PGSINGLE | MDB_IOB_PGCONT); 540 iob->iob_pgp = mdb_io_hold(pgio); 541 542 IOP_LINK(iob->iob_pgp, iob); 543 544 if (IOP_CTL(pgio, TIOCGWINSZ, &winsz) == 0) 545 mdb_iob_resize(iob, (size_t)winsz.ws_row, (size_t)winsz.ws_col); 546 } 547 548 void 549 mdb_iob_tabstop(mdb_iob_t *iob, size_t tabstop) 550 { 551 iob->iob_tabstop = MIN(tabstop, iob->iob_cols - 1); 552 } 553 554 void 555 mdb_iob_margin(mdb_iob_t *iob, size_t margin) 556 { 557 iob_unindent(iob); 558 iob->iob_margin = MIN(margin, iob->iob_cols - 1); 559 iob_indent(iob); 560 } 561 562 void 563 mdb_iob_setbuf(mdb_iob_t *iob, void *buf, size_t bufsiz) 564 { 565 ASSERT(buf != NULL && bufsiz != 0); 566 567 mdb_free(iob->iob_buf, iob->iob_bufsiz); 568 iob->iob_buf = buf; 569 iob->iob_bufsiz = bufsiz; 570 571 if (iob->iob_flags & MDB_IOB_WRONLY) 572 iob->iob_cols = MIN(iob->iob_cols, iob->iob_bufsiz); 573 } 574 575 void 576 mdb_iob_clearlines(mdb_iob_t *iob) 577 { 578 iob->iob_flags &= ~(MDB_IOB_PGSINGLE | MDB_IOB_PGCONT); 579 iob->iob_nlines = 0; 580 } 581 582 void 583 mdb_iob_setflags(mdb_iob_t *iob, uint_t flags) 584 { 585 iob->iob_flags |= flags; 586 if (flags & MDB_IOB_INDENT) 587 iob_indent(iob); 588 } 589 590 void 591 mdb_iob_clrflags(mdb_iob_t *iob, uint_t flags) 592 { 593 iob->iob_flags &= ~flags; 594 if (flags & MDB_IOB_INDENT) 595 iob_unindent(iob); 596 } 597 598 uint_t 599 mdb_iob_getflags(mdb_iob_t *iob) 600 { 601 return (iob->iob_flags); 602 } 603 604 static uintmax_t 605 vec_arg(const mdb_arg_t **app) 606 { 607 uintmax_t value; 608 609 if ((*app)->a_type == MDB_TYPE_STRING) 610 value = (uintmax_t)(uintptr_t)(*app)->a_un.a_str; 611 else 612 value = (*app)->a_un.a_val; 613 614 (*app)++; 615 return (value); 616 } 617 618 static const char * 619 iob_size2str(intsize_t size) 620 { 621 switch (size) { 622 case SZ_SHORT: 623 return ("short"); 624 case SZ_INT: 625 return ("int"); 626 case SZ_LONG: 627 return ("long"); 628 case SZ_LONGLONG: 629 return ("long long"); 630 } 631 return (""); 632 } 633 634 /* 635 * In order to simplify maintenance of the ::formats display, we provide an 636 * unparser for mdb_printf format strings that converts a simple format 637 * string with one specifier into a descriptive representation, e.g. 638 * mdb_iob_format2str("%llx") returns "hexadecimal long long". 639 */ 640 const char * 641 mdb_iob_format2str(const char *format) 642 { 643 intsize_t size = SZ_INT; 644 const char *p; 645 646 static char buf[64]; 647 648 buf[0] = '\0'; 649 650 if ((p = strchr(format, '%')) == NULL) 651 goto done; 652 653 fmt_switch: 654 switch (*++p) { 655 case '0': case '1': case '2': case '3': case '4': 656 case '5': case '6': case '7': case '8': case '9': 657 while (*p >= '0' && *p <= '9') 658 p++; 659 p--; 660 goto fmt_switch; 661 662 case 'a': 663 case 'A': 664 return ("symbol"); 665 666 case 'b': 667 (void) strcpy(buf, "unsigned "); 668 (void) strcat(buf, iob_size2str(size)); 669 (void) strcat(buf, " bitfield"); 670 break; 671 672 case 'c': 673 return ("character"); 674 675 case 'd': 676 case 'i': 677 (void) strcpy(buf, "decimal signed "); 678 (void) strcat(buf, iob_size2str(size)); 679 break; 680 681 case 'e': 682 case 'E': 683 case 'g': 684 case 'G': 685 return ("double"); 686 687 case 'h': 688 size = SZ_SHORT; 689 goto fmt_switch; 690 691 case 'H': 692 return ("human-readable size"); 693 694 case 'I': 695 return ("IPv4 address"); 696 697 case 'l': 698 if (size >= SZ_LONG) 699 size = SZ_LONGLONG; 700 else 701 size = SZ_LONG; 702 goto fmt_switch; 703 704 case 'm': 705 return ("margin"); 706 707 case 'N': 708 return ("IPv6 address"); 709 710 case 'o': 711 (void) strcpy(buf, "octal unsigned "); 712 (void) strcat(buf, iob_size2str(size)); 713 break; 714 715 case 'p': 716 return ("pointer"); 717 718 case 'q': 719 (void) strcpy(buf, "octal signed "); 720 (void) strcat(buf, iob_size2str(size)); 721 break; 722 723 case 'r': 724 (void) strcpy(buf, "default radix unsigned "); 725 (void) strcat(buf, iob_size2str(size)); 726 break; 727 728 case 'R': 729 (void) strcpy(buf, "default radix signed "); 730 (void) strcat(buf, iob_size2str(size)); 731 break; 732 733 case 's': 734 return ("string"); 735 736 case 't': 737 case 'T': 738 return ("tab"); 739 740 case 'u': 741 (void) strcpy(buf, "decimal unsigned "); 742 (void) strcat(buf, iob_size2str(size)); 743 break; 744 745 case 'x': 746 case 'X': 747 (void) strcat(buf, "hexadecimal "); 748 (void) strcat(buf, iob_size2str(size)); 749 break; 750 751 case 'Y': 752 return ("time_t"); 753 754 case '<': 755 return ("terminal attribute"); 756 757 case '?': 758 case '#': 759 case '+': 760 case '-': 761 goto fmt_switch; 762 } 763 764 done: 765 if (buf[0] == '\0') 766 (void) strcpy(buf, "text"); 767 768 return ((const char *)buf); 769 } 770 771 static const char * 772 iob_int2str(varglist_t *ap, intsize_t size, int base, uint_t flags, int *zero, 773 u_longlong_t *value) 774 { 775 uintmax_t i; 776 777 switch (size) { 778 case SZ_LONGLONG: 779 if (flags & NTOS_UNSIGNED) 780 i = (u_longlong_t)VA_ARG(ap, u_longlong_t); 781 else 782 i = (longlong_t)VA_ARG(ap, longlong_t); 783 break; 784 785 case SZ_LONG: 786 if (flags & NTOS_UNSIGNED) 787 i = (ulong_t)VA_ARG(ap, ulong_t); 788 else 789 i = (long)VA_ARG(ap, long); 790 break; 791 792 case SZ_SHORT: 793 if (flags & NTOS_UNSIGNED) 794 i = (ushort_t)VA_ARG(ap, uint_t); 795 else 796 i = (short)VA_ARG(ap, int); 797 break; 798 799 default: 800 if (flags & NTOS_UNSIGNED) 801 i = (uint_t)VA_ARG(ap, uint_t); 802 else 803 i = (int)VA_ARG(ap, int); 804 } 805 806 *zero = i == 0; /* Return flag indicating if result was zero */ 807 *value = i; /* Return value retrieved from va_list */ 808 809 return (numtostr(i, base, flags)); 810 } 811 812 static const char * 813 iob_time2str(time_t *tmp) 814 { 815 /* 816 * ctime(3c) returns a string of the form 817 * "Fri Sep 13 00:00:00 1986\n\0". We turn this into the canonical 818 * adb /y format "1986 Sep 13 00:00:00" below. 819 */ 820 const char *src = ctime(tmp); 821 static char buf[32]; 822 char *dst = buf; 823 int i; 824 825 if (src == NULL) 826 return (numtostr((uintmax_t)*tmp, mdb.m_radix, 0)); 827 828 for (i = 20; i < 24; i++) 829 *dst++ = src[i]; /* Copy the 4-digit year */ 830 831 for (i = 3; i < 19; i++) 832 *dst++ = src[i]; /* Copy month, day, and h:m:s */ 833 834 *dst = '\0'; 835 return (buf); 836 } 837 838 static const char * 839 iob_addr2str(uintptr_t addr) 840 { 841 static char buf[MDB_TGT_SYM_NAMLEN]; 842 char *name = buf; 843 longlong_t offset; 844 GElf_Sym sym; 845 846 if (mdb_tgt_lookup_by_addr(mdb.m_target, addr, 847 MDB_TGT_SYM_FUZZY, buf, sizeof (buf), &sym, NULL) == -1) 848 return (NULL); 849 850 if (mdb.m_demangler != NULL && (mdb.m_flags & MDB_FL_DEMANGLE)) 851 name = (char *)mdb_dem_convert(mdb.m_demangler, buf); 852 853 /* 854 * Here we provide a little cooperation between the %a formatting code 855 * and the proc target: if the initial address passed to %a is in fact 856 * a PLT address, the proc target's lookup_by_addr code will convert 857 * this to the PLT destination (a different address). We do not want 858 * to append a "+/-offset" suffix based on comparison with the query 859 * symbol in this case because the proc target has really done a hidden 860 * query for us with a different address. We detect this case by 861 * comparing the initial characters of buf to the special PLT= string. 862 */ 863 if (sym.st_value != addr && strncmp(name, "PLT=", 4) != 0) { 864 if (sym.st_value > addr) 865 offset = -(longlong_t)(sym.st_value - addr); 866 else 867 offset = (longlong_t)(addr - sym.st_value); 868 869 (void) strcat(name, numtostr(offset, mdb.m_radix, 870 NTOS_SIGNPOS | NTOS_SHOWBASE)); 871 } 872 873 return (name); 874 } 875 876 /* 877 * Produce human-readable size, similar in spirit (and identical in output) 878 * to libzfs's zfs_nicenum() -- but made significantly more complicated by 879 * the constraint that we cannot use snprintf() as an implementation detail. 880 */ 881 static const char * 882 iob_bytes2str(varglist_t *ap, intsize_t size) 883 { 884 const int sigfig = 3; 885 uint64_t n, orig; 886 static char buf[68], *c; 887 int index = 0; 888 char u; 889 890 switch (size) { 891 case SZ_LONGLONG: 892 n = (u_longlong_t)VA_ARG(ap, u_longlong_t); 893 break; 894 895 case SZ_LONG: 896 n = (ulong_t)VA_ARG(ap, ulong_t); 897 break; 898 899 case SZ_SHORT: 900 n = (ushort_t)VA_ARG(ap, uint_t); 901 902 default: 903 n = (uint_t)VA_ARG(ap, uint_t); 904 } 905 906 orig = n; 907 908 while (n >= 1024) { 909 n /= 1024; 910 index++; 911 } 912 913 u = " KMGTPE"[index]; 914 buf[0] = '\0'; 915 916 if (index == 0) { 917 return (numtostr(n, 10, 0)); 918 } else if ((orig & ((1ULL << 10 * index) - 1)) == 0) { 919 /* 920 * If this is an even multiple of the base, always display 921 * without any decimal precision. 922 */ 923 (void) strcat(buf, numtostr(n, 10, 0)); 924 } else { 925 /* 926 * We want to choose a precision that results in the specified 927 * number of significant figures (by default, 3). This is 928 * similar to the output that one would get specifying the %.*g 929 * format specifier (where the asterisk denotes the number of 930 * significant digits), but (1) we include trailing zeros if 931 * the there are non-zero digits beyond the number of 932 * significant digits (that is, 10241 is '10.0K', not the 933 * '10K' that it would be with %.3g) and (2) we never resort 934 * to %e notation when the number of digits exceeds the 935 * number of significant figures (that is, 1043968 is '1020K', 936 * not '1.02e+03K'). This is also made somewhat complicated 937 * by the fact that we need to deal with rounding (10239 is 938 * '10.0K', not '9.99K'), for which we perform nearest-even 939 * rounding. 940 */ 941 double val = (double)orig / (1ULL << 10 * index); 942 int i, mag = 1, thresh; 943 944 for (i = 0; i < sigfig - 1; i++) 945 mag *= 10; 946 947 for (thresh = mag * 10; mag >= 1; mag /= 10, i--) { 948 double mult = val * (double)mag; 949 uint32_t v; 950 951 /* 952 * Note that we cast mult to a 32-bit value. We know 953 * that val is less than 1024 due to the logic above, 954 * and that mag is at most 10^(sigfig - 1). This means 955 * that as long as sigfig is 9 or lower, this will not 956 * overflow. (We perform this cast because it assures 957 * that we are never converting a double to a uint64_t, 958 * which for some compilers requires a call to a 959 * function not guaranteed to be in libstand.) 960 */ 961 if (mult - (double)(uint32_t)mult != 0.5) { 962 v = (uint32_t)(mult + 0.5); 963 } else { 964 /* 965 * We are exactly between integer multiples 966 * of units; perform nearest-even rounding 967 * to be consistent with the behavior of 968 * printf(). 969 */ 970 if ((v = (uint32_t)mult) & 1) 971 v++; 972 } 973 974 if (mag == 1) { 975 (void) strcat(buf, numtostr(v, 10, 0)); 976 break; 977 } 978 979 if (v < thresh) { 980 (void) strcat(buf, numtostr(v / mag, 10, 0)); 981 (void) strcat(buf, "."); 982 983 c = (char *)numtostr(v % mag, 10, 0); 984 i -= strlen(c); 985 986 /* 987 * We need to zero-fill from the right of the 988 * decimal point to the first significant digit 989 * of the fractional component. 990 */ 991 while (i--) 992 (void) strcat(buf, "0"); 993 994 (void) strcat(buf, c); 995 break; 996 } 997 } 998 } 999 1000 c = &buf[strlen(buf)]; 1001 *c++ = u; 1002 *c++ = '\0'; 1003 1004 return (buf); 1005 } 1006 1007 static int 1008 iob_setattr(mdb_iob_t *iob, const char *s, size_t nbytes) 1009 { 1010 uint_t attr; 1011 int req; 1012 1013 if (iob->iob_pgp == NULL) 1014 return (set_errno(ENOTTY)); 1015 1016 if (nbytes != 0 && *s == '/') { 1017 req = ATT_OFF; 1018 nbytes--; 1019 s++; 1020 } else 1021 req = ATT_ON; 1022 1023 if (nbytes != 1) 1024 return (set_errno(EINVAL)); 1025 1026 switch (*s) { 1027 case 's': 1028 attr = ATT_STANDOUT; 1029 break; 1030 case 'u': 1031 attr = ATT_UNDERLINE; 1032 break; 1033 case 'r': 1034 attr = ATT_REVERSE; 1035 break; 1036 case 'b': 1037 attr = ATT_BOLD; 1038 break; 1039 case 'd': 1040 attr = ATT_DIM; 1041 break; 1042 case 'a': 1043 attr = ATT_ALTCHARSET; 1044 break; 1045 default: 1046 return (set_errno(EINVAL)); 1047 } 1048 1049 /* 1050 * We need to flush the current buffer contents before calling 1051 * IOP_SETATTR because IOP_SETATTR may need to synchronously output 1052 * terminal escape sequences directly to the underlying device. 1053 */ 1054 (void) iob_write(iob, iob->iob_iop, iob->iob_buf, iob->iob_nbytes); 1055 iob->iob_bufp = &iob->iob_buf[0]; 1056 iob->iob_nbytes = 0; 1057 1058 return (IOP_SETATTR(iob->iob_pgp, req, attr)); 1059 } 1060 1061 static void 1062 iob_bits2str(mdb_iob_t *iob, u_longlong_t value, const mdb_bitmask_t *bmp, 1063 mdb_bool_t altflag) 1064 { 1065 mdb_bool_t delim = FALSE; 1066 const char *str; 1067 size_t width; 1068 1069 if (bmp == NULL) 1070 goto out; 1071 1072 for (; bmp->bm_name != NULL; bmp++) { 1073 if ((value & bmp->bm_mask) == bmp->bm_bits) { 1074 width = strlen(bmp->bm_name) + delim; 1075 1076 if (IOB_WRAPNOW(iob, width)) 1077 mdb_iob_nl(iob); 1078 1079 if (delim) 1080 mdb_iob_putc(iob, ','); 1081 else 1082 delim = TRUE; 1083 1084 mdb_iob_puts(iob, bmp->bm_name); 1085 value &= ~bmp->bm_bits; 1086 } 1087 } 1088 1089 out: 1090 if (altflag == TRUE && (delim == FALSE || value != 0)) { 1091 str = numtostr(value, 16, NTOS_UNSIGNED | NTOS_SHOWBASE); 1092 width = strlen(str) + delim; 1093 1094 if (IOB_WRAPNOW(iob, width)) 1095 mdb_iob_nl(iob); 1096 if (delim) 1097 mdb_iob_putc(iob, ','); 1098 mdb_iob_puts(iob, str); 1099 } 1100 } 1101 1102 static const char * 1103 iob_inaddr2str(uint32_t addr) 1104 { 1105 static char buf[INET_ADDRSTRLEN]; 1106 1107 (void) mdb_inet_ntop(AF_INET, &addr, buf, sizeof (buf)); 1108 1109 return (buf); 1110 } 1111 1112 static const char * 1113 iob_ipv6addr2str(void *addr) 1114 { 1115 static char buf[INET6_ADDRSTRLEN]; 1116 1117 (void) mdb_inet_ntop(AF_INET6, addr, buf, sizeof (buf)); 1118 1119 return (buf); 1120 } 1121 1122 static const char * 1123 iob_getvar(const char *s, size_t len) 1124 { 1125 mdb_var_t *val; 1126 char *var; 1127 1128 if (len == 0) { 1129 (void) set_errno(EINVAL); 1130 return (NULL); 1131 } 1132 1133 var = strndup(s, len); 1134 val = mdb_nv_lookup(&mdb.m_nv, var); 1135 strfree(var); 1136 1137 if (val == NULL) { 1138 (void) set_errno(EINVAL); 1139 return (NULL); 1140 } 1141 1142 return (numtostr(mdb_nv_get_value(val), 10, 0)); 1143 } 1144 1145 /* 1146 * The iob_doprnt function forms the main engine of the debugger's output 1147 * formatting capabilities. Note that this is NOT exactly compatible with 1148 * the printf(3S) family, nor is it intended to be so. We support some 1149 * extensions and format characters not supported by printf(3S), and we 1150 * explicitly do NOT provide support for %C, %S, %ws (wide-character strings), 1151 * do NOT provide for the complete functionality of %f, %e, %E, %g, %G 1152 * (alternate double formats), and do NOT support %.x (precision specification). 1153 * Note that iob_doprnt consumes varargs off the original va_list. 1154 */ 1155 static void 1156 iob_doprnt(mdb_iob_t *iob, const char *format, varglist_t *ap) 1157 { 1158 char c[2] = { 0, 0 }; /* Buffer for single character output */ 1159 const char *p; /* Current position in format string */ 1160 size_t len; /* Length of format string to copy verbatim */ 1161 size_t altlen; /* Length of alternate print format prefix */ 1162 const char *altstr; /* Alternate print format prefix */ 1163 const char *symstr; /* Symbol + offset string */ 1164 1165 u_longlong_t val; /* Current integer value */ 1166 intsize_t size; /* Current integer value size */ 1167 uint_t flags; /* Current flags to pass to iob_int2str */ 1168 size_t width; /* Current field width */ 1169 int zero; /* If != 0, then integer value == 0 */ 1170 1171 mdb_bool_t f_alt; /* Use alternate print format (%#) */ 1172 mdb_bool_t f_altsuff; /* Alternate print format is a suffix */ 1173 mdb_bool_t f_zfill; /* Zero-fill field (%0) */ 1174 mdb_bool_t f_left; /* Left-adjust field (%-) */ 1175 mdb_bool_t f_digits; /* Explicit digits used to set field width */ 1176 1177 union { 1178 const char *str; 1179 uint32_t ui32; 1180 void *ptr; 1181 time_t tm; 1182 char c; 1183 double d; 1184 long double ld; 1185 } u; 1186 1187 ASSERT(iob->iob_flags & MDB_IOB_WRONLY); 1188 1189 while ((p = strchr(format, '%')) != NULL) { 1190 /* 1191 * Output the format string verbatim up to the next '%' char 1192 */ 1193 if (p != format) { 1194 len = p - format; 1195 if (IOB_WRAPNOW(iob, len) && *format != '\n') 1196 mdb_iob_nl(iob); 1197 mdb_iob_nputs(iob, format, len); 1198 } 1199 1200 /* 1201 * Now we need to parse the sequence of format characters 1202 * following the % marker and do the appropriate thing. 1203 */ 1204 size = SZ_INT; /* Use normal-sized int by default */ 1205 flags = 0; /* Clear numtostr() format flags */ 1206 width = 0; /* No field width limit by default */ 1207 altlen = 0; /* No alternate format string yet */ 1208 altstr = NULL; /* No alternate format string yet */ 1209 1210 f_alt = FALSE; /* Alternate format off by default */ 1211 f_altsuff = FALSE; /* Alternate format is a prefix */ 1212 f_zfill = FALSE; /* Zero-fill off by default */ 1213 f_left = FALSE; /* Left-adjust off by default */ 1214 f_digits = FALSE; /* No digits for width specified yet */ 1215 1216 fmt_switch: 1217 switch (*++p) { 1218 case '0': case '1': case '2': case '3': case '4': 1219 case '5': case '6': case '7': case '8': case '9': 1220 if (f_digits == FALSE && *p == '0') { 1221 f_zfill = TRUE; 1222 goto fmt_switch; 1223 } 1224 1225 if (f_digits == FALSE) 1226 width = 0; /* clear any other width specifier */ 1227 1228 for (u.c = *p; u.c >= '0' && u.c <= '9'; u.c = *++p) 1229 width = width * 10 + u.c - '0'; 1230 1231 p--; 1232 f_digits = TRUE; 1233 goto fmt_switch; 1234 1235 case 'a': 1236 if (size < SZ_LONG) 1237 size = SZ_LONG; /* Bump to size of uintptr_t */ 1238 1239 u.str = iob_int2str(ap, size, 16, 1240 NTOS_UNSIGNED | NTOS_SHOWBASE, &zero, &val); 1241 1242 if ((symstr = iob_addr2str(val)) != NULL) 1243 u.str = symstr; 1244 1245 if (f_alt == TRUE) { 1246 f_altsuff = TRUE; 1247 altstr = ":"; 1248 altlen = 1; 1249 } 1250 break; 1251 1252 case 'A': 1253 if (size < SZ_LONG) 1254 size = SZ_LONG; /* Bump to size of uintptr_t */ 1255 1256 (void) iob_int2str(ap, size, 16, 1257 NTOS_UNSIGNED, &zero, &val); 1258 1259 u.str = iob_addr2str(val); 1260 1261 if (f_alt == TRUE && u.str == NULL) 1262 u.str = "?"; 1263 break; 1264 1265 case 'b': 1266 u.str = iob_int2str(ap, size, 16, 1267 NTOS_UNSIGNED | NTOS_SHOWBASE, &zero, &val); 1268 1269 iob_bits2str(iob, val, VA_PTRARG(ap), f_alt); 1270 1271 format = ++p; 1272 continue; 1273 1274 case 'c': 1275 c[0] = (char)VA_ARG(ap, int); 1276 u.str = c; 1277 break; 1278 1279 case 'd': 1280 case 'i': 1281 if (f_alt) 1282 flags |= NTOS_SHOWBASE; 1283 u.str = iob_int2str(ap, size, 10, flags, &zero, &val); 1284 break; 1285 1286 /* No floating point in kmdb */ 1287 #ifndef _KMDB 1288 case 'e': 1289 case 'E': 1290 u.d = VA_ARG(ap, double); 1291 u.str = doubletos(u.d, 7, *p); 1292 break; 1293 1294 case 'g': 1295 case 'G': 1296 if (size >= SZ_LONG) { 1297 u.ld = VA_ARG(ap, long double); 1298 u.str = longdoubletos(&u.ld, 16, 1299 (*p == 'g') ? 'e' : 'E'); 1300 } else { 1301 u.d = VA_ARG(ap, double); 1302 u.str = doubletos(u.d, 16, 1303 (*p == 'g') ? 'e' : 'E'); 1304 } 1305 break; 1306 #endif 1307 1308 case 'h': 1309 size = SZ_SHORT; 1310 goto fmt_switch; 1311 1312 case 'H': 1313 u.str = iob_bytes2str(ap, size); 1314 break; 1315 1316 case 'I': 1317 u.ui32 = VA_ARG(ap, uint32_t); 1318 u.str = iob_inaddr2str(u.ui32); 1319 break; 1320 1321 case 'l': 1322 if (size >= SZ_LONG) 1323 size = SZ_LONGLONG; 1324 else 1325 size = SZ_LONG; 1326 goto fmt_switch; 1327 1328 case 'm': 1329 if (iob->iob_nbytes == 0) { 1330 mdb_iob_ws(iob, (width != 0) ? width : 1331 iob->iob_margin); 1332 } 1333 format = ++p; 1334 continue; 1335 1336 case 'N': 1337 u.ptr = VA_PTRARG(ap); 1338 u.str = iob_ipv6addr2str(u.ptr); 1339 break; 1340 1341 case 'o': 1342 u.str = iob_int2str(ap, size, 8, NTOS_UNSIGNED, 1343 &zero, &val); 1344 1345 if (f_alt && !zero) { 1346 altstr = "0"; 1347 altlen = 1; 1348 } 1349 break; 1350 1351 case 'p': 1352 u.ptr = VA_PTRARG(ap); 1353 u.str = numtostr((uintptr_t)u.ptr, 16, NTOS_UNSIGNED); 1354 break; 1355 1356 case 'q': 1357 u.str = iob_int2str(ap, size, 8, flags, &zero, &val); 1358 1359 if (f_alt && !zero) { 1360 altstr = "0"; 1361 altlen = 1; 1362 } 1363 break; 1364 1365 case 'r': 1366 if (f_alt) 1367 flags |= NTOS_SHOWBASE; 1368 u.str = iob_int2str(ap, size, mdb.m_radix, 1369 NTOS_UNSIGNED | flags, &zero, &val); 1370 break; 1371 1372 case 'R': 1373 if (f_alt) 1374 flags |= NTOS_SHOWBASE; 1375 u.str = iob_int2str(ap, size, mdb.m_radix, flags, 1376 &zero, &val); 1377 break; 1378 1379 case 's': 1380 u.str = VA_PTRARG(ap); 1381 if (u.str == NULL) 1382 u.str = "<NULL>"; /* Be forgiving of NULL */ 1383 break; 1384 1385 case 't': 1386 if (width != 0) { 1387 while (width-- > 0) 1388 mdb_iob_tab(iob); 1389 } else 1390 mdb_iob_tab(iob); 1391 1392 format = ++p; 1393 continue; 1394 1395 case 'T': 1396 if (width != 0 && (iob->iob_nbytes % width) != 0) { 1397 size_t ots = iob->iob_tabstop; 1398 iob->iob_tabstop = width; 1399 mdb_iob_tab(iob); 1400 iob->iob_tabstop = ots; 1401 } 1402 format = ++p; 1403 continue; 1404 1405 case 'u': 1406 if (f_alt) 1407 flags |= NTOS_SHOWBASE; 1408 u.str = iob_int2str(ap, size, 10, 1409 flags | NTOS_UNSIGNED, &zero, &val); 1410 break; 1411 1412 case 'x': 1413 u.str = iob_int2str(ap, size, 16, NTOS_UNSIGNED, 1414 &zero, &val); 1415 1416 if (f_alt && !zero) { 1417 altstr = "0x"; 1418 altlen = 2; 1419 } 1420 break; 1421 1422 case 'X': 1423 u.str = iob_int2str(ap, size, 16, 1424 NTOS_UNSIGNED | NTOS_UPCASE, &zero, &val); 1425 1426 if (f_alt && !zero) { 1427 altstr = "0X"; 1428 altlen = 2; 1429 } 1430 break; 1431 1432 case 'Y': 1433 u.tm = VA_ARG(ap, time_t); 1434 u.str = iob_time2str(&u.tm); 1435 break; 1436 1437 case '<': 1438 /* 1439 * Used to turn attributes on (<b>), to turn them 1440 * off (</b>), or to print variables (<_var>). 1441 */ 1442 for (u.str = ++p; *p != '\0' && *p != '>'; p++) 1443 continue; 1444 1445 if (*p == '>') { 1446 size_t paramlen = p - u.str; 1447 1448 if (paramlen > 0) { 1449 if (*u.str == '_') { 1450 u.str = iob_getvar(u.str + 1, 1451 paramlen - 1); 1452 break; 1453 } else { 1454 (void) iob_setattr(iob, u.str, 1455 paramlen); 1456 } 1457 } 1458 1459 p++; 1460 } 1461 1462 format = p; 1463 continue; 1464 1465 case '*': 1466 width = (size_t)(uint_t)VA_ARG(ap, int); 1467 goto fmt_switch; 1468 1469 case '%': 1470 u.str = "%"; 1471 break; 1472 1473 case '?': 1474 width = sizeof (uintptr_t) * 2; 1475 goto fmt_switch; 1476 1477 case '#': 1478 f_alt = TRUE; 1479 goto fmt_switch; 1480 1481 case '+': 1482 flags |= NTOS_SIGNPOS; 1483 goto fmt_switch; 1484 1485 case '-': 1486 f_left = TRUE; 1487 goto fmt_switch; 1488 1489 default: 1490 c[0] = p[0]; 1491 u.str = c; 1492 } 1493 1494 len = u.str != NULL ? strlen(u.str) : 0; 1495 1496 if (len + altlen > width) 1497 width = len + altlen; 1498 1499 /* 1500 * If the string and the option altstr won't fit on this line 1501 * and auto-wrap is set (default), skip to the next line. 1502 */ 1503 if (IOB_WRAPNOW(iob, width)) 1504 mdb_iob_nl(iob); 1505 1506 /* 1507 * Optionally add whitespace or zeroes prefixing the value if 1508 * we haven't filled the minimum width and we're right-aligned. 1509 */ 1510 if (len < (width - altlen) && f_left == FALSE) { 1511 mdb_iob_fill(iob, f_zfill ? '0' : ' ', 1512 width - altlen - len); 1513 } 1514 1515 /* 1516 * Print the alternate string if it's a prefix, and then 1517 * print the value string itself. 1518 */ 1519 if (altstr != NULL && f_altsuff == FALSE) 1520 mdb_iob_nputs(iob, altstr, altlen); 1521 if (len != 0) 1522 mdb_iob_nputs(iob, u.str, len); 1523 1524 /* 1525 * If we have an alternate string and it's a suffix, print it. 1526 */ 1527 if (altstr != NULL && f_altsuff == TRUE) 1528 mdb_iob_nputs(iob, altstr, altlen); 1529 1530 /* 1531 * Finally, if we haven't filled the field width and we're 1532 * left-aligned, pad out the rest with whitespace. 1533 */ 1534 if ((len + altlen) < width && f_left == TRUE) 1535 mdb_iob_ws(iob, width - altlen - len); 1536 1537 format = (*p != '\0') ? ++p : p; 1538 } 1539 1540 /* 1541 * If there's anything left in the format string, output it now 1542 */ 1543 if (*format != '\0') { 1544 len = strlen(format); 1545 if (IOB_WRAPNOW(iob, len) && *format != '\n') 1546 mdb_iob_nl(iob); 1547 mdb_iob_nputs(iob, format, len); 1548 } 1549 } 1550 1551 void 1552 mdb_iob_vprintf(mdb_iob_t *iob, const char *format, va_list alist) 1553 { 1554 varglist_t ap = { VAT_VARARGS }; 1555 va_copy(ap.val_valist, alist); 1556 iob_doprnt(iob, format, &ap); 1557 } 1558 1559 void 1560 mdb_iob_aprintf(mdb_iob_t *iob, const char *format, const mdb_arg_t *argv) 1561 { 1562 varglist_t ap = { VAT_ARGVEC }; 1563 ap.val_argv = argv; 1564 iob_doprnt(iob, format, &ap); 1565 } 1566 1567 void 1568 mdb_iob_printf(mdb_iob_t *iob, const char *format, ...) 1569 { 1570 va_list alist; 1571 1572 va_start(alist, format); 1573 mdb_iob_vprintf(iob, format, alist); 1574 va_end(alist); 1575 } 1576 1577 /* 1578 * In order to handle the sprintf family of functions, we define a special 1579 * i/o backend known as a "sprintf buf" (or spbuf for short). This back end 1580 * provides an IOP_WRITE entry point that concatenates each buffer sent from 1581 * mdb_iob_flush() onto the caller's buffer until the caller's buffer is 1582 * exhausted. We also keep an absolute count of how many bytes were sent to 1583 * this function during the lifetime of the snprintf call. This allows us 1584 * to provide the ability to (1) return the total size required for the given 1585 * format string and argument list, and (2) support a call to snprintf with a 1586 * NULL buffer argument with no special case code elsewhere. 1587 */ 1588 static ssize_t 1589 spbuf_write(mdb_io_t *io, const void *buf, size_t buflen) 1590 { 1591 spbuf_t *spb = io->io_data; 1592 1593 if (spb->spb_bufsiz != 0) { 1594 size_t n = MIN(spb->spb_bufsiz, buflen); 1595 bcopy(buf, spb->spb_buf, n); 1596 spb->spb_buf += n; 1597 spb->spb_bufsiz -= n; 1598 } 1599 1600 spb->spb_total += buflen; 1601 return (buflen); 1602 } 1603 1604 static const mdb_io_ops_t spbuf_ops = { 1605 no_io_read, 1606 spbuf_write, 1607 no_io_seek, 1608 no_io_ctl, 1609 no_io_close, 1610 no_io_name, 1611 no_io_link, 1612 no_io_unlink, 1613 no_io_setattr, 1614 no_io_suspend, 1615 no_io_resume 1616 }; 1617 1618 /* 1619 * The iob_spb_create function initializes an iob suitable for snprintf calls, 1620 * a spbuf i/o backend, and the spbuf private data, and then glues these 1621 * objects together. The caller (either vsnprintf or asnprintf below) is 1622 * expected to have allocated the various structures on their stack. 1623 */ 1624 static void 1625 iob_spb_create(mdb_iob_t *iob, char *iob_buf, size_t iob_len, 1626 mdb_io_t *io, spbuf_t *spb, char *spb_buf, size_t spb_len) 1627 { 1628 spb->spb_buf = spb_buf; 1629 spb->spb_bufsiz = spb_len; 1630 spb->spb_total = 0; 1631 1632 io->io_ops = &spbuf_ops; 1633 io->io_data = spb; 1634 io->io_next = NULL; 1635 io->io_refcnt = 1; 1636 1637 iob->iob_buf = iob_buf; 1638 iob->iob_bufsiz = iob_len; 1639 iob->iob_bufp = iob_buf; 1640 iob->iob_nbytes = 0; 1641 iob->iob_nlines = 0; 1642 iob->iob_lineno = 1; 1643 iob->iob_rows = MDB_IOB_DEFROWS; 1644 iob->iob_cols = iob_len; 1645 iob->iob_tabstop = MDB_IOB_DEFTAB; 1646 iob->iob_margin = MDB_IOB_DEFMARGIN; 1647 iob->iob_flags = MDB_IOB_WRONLY; 1648 iob->iob_iop = io; 1649 iob->iob_pgp = NULL; 1650 iob->iob_next = NULL; 1651 } 1652 1653 /*ARGSUSED*/ 1654 ssize_t 1655 null_io_write(mdb_io_t *io, const void *buf, size_t nbytes) 1656 { 1657 return (nbytes); 1658 } 1659 1660 static const mdb_io_ops_t null_ops = { 1661 no_io_read, 1662 null_io_write, 1663 no_io_seek, 1664 no_io_ctl, 1665 no_io_close, 1666 no_io_name, 1667 no_io_link, 1668 no_io_unlink, 1669 no_io_setattr, 1670 no_io_suspend, 1671 no_io_resume 1672 }; 1673 1674 mdb_io_t * 1675 mdb_nullio_create(void) 1676 { 1677 static mdb_io_t null_io = { 1678 &null_ops, 1679 NULL, 1680 NULL, 1681 1 1682 }; 1683 1684 return (&null_io); 1685 } 1686 1687 size_t 1688 mdb_iob_vsnprintf(char *buf, size_t nbytes, const char *format, va_list alist) 1689 { 1690 varglist_t ap = { VAT_VARARGS }; 1691 char iob_buf[64]; 1692 mdb_iob_t iob; 1693 mdb_io_t io; 1694 spbuf_t spb; 1695 1696 ASSERT(buf != NULL || nbytes == 0); 1697 iob_spb_create(&iob, iob_buf, sizeof (iob_buf), &io, &spb, buf, nbytes); 1698 va_copy(ap.val_valist, alist); 1699 iob_doprnt(&iob, format, &ap); 1700 mdb_iob_flush(&iob); 1701 1702 if (spb.spb_bufsiz != 0) 1703 *spb.spb_buf = '\0'; 1704 else if (buf != NULL && nbytes > 0) 1705 *--spb.spb_buf = '\0'; 1706 1707 return (spb.spb_total); 1708 } 1709 1710 size_t 1711 mdb_iob_asnprintf(char *buf, size_t nbytes, const char *format, 1712 const mdb_arg_t *argv) 1713 { 1714 varglist_t ap = { VAT_ARGVEC }; 1715 char iob_buf[64]; 1716 mdb_iob_t iob; 1717 mdb_io_t io; 1718 spbuf_t spb; 1719 1720 ASSERT(buf != NULL || nbytes == 0); 1721 iob_spb_create(&iob, iob_buf, sizeof (iob_buf), &io, &spb, buf, nbytes); 1722 ap.val_argv = argv; 1723 iob_doprnt(&iob, format, &ap); 1724 mdb_iob_flush(&iob); 1725 1726 if (spb.spb_bufsiz != 0) 1727 *spb.spb_buf = '\0'; 1728 else if (buf != NULL && nbytes > 0) 1729 *--spb.spb_buf = '\0'; 1730 1731 return (spb.spb_total); 1732 } 1733 1734 /*PRINTFLIKE3*/ 1735 size_t 1736 mdb_iob_snprintf(char *buf, size_t nbytes, const char *format, ...) 1737 { 1738 va_list alist; 1739 1740 va_start(alist, format); 1741 nbytes = mdb_iob_vsnprintf(buf, nbytes, format, alist); 1742 va_end(alist); 1743 1744 return (nbytes); 1745 } 1746 1747 void 1748 mdb_iob_nputs(mdb_iob_t *iob, const char *s, size_t nbytes) 1749 { 1750 size_t m, n, nleft = nbytes; 1751 const char *p, *q = s; 1752 1753 ASSERT(iob->iob_flags & MDB_IOB_WRONLY); 1754 1755 if (nbytes == 0) 1756 return; /* Return immediately if there is no work to do */ 1757 1758 /* 1759 * If the string contains embedded newlines or tabs, invoke ourself 1760 * recursively for each string component, followed by a call to the 1761 * newline or tab routine. This insures that strings with these 1762 * characters obey our wrapping and indenting rules, and that strings 1763 * with embedded newlines are flushed after each newline, allowing 1764 * the output pager to take over if it is enabled. 1765 */ 1766 while ((p = strnpbrk(q, "\t\n", nleft)) != NULL) { 1767 if (p > q) 1768 mdb_iob_nputs(iob, q, (size_t)(p - q)); 1769 1770 if (*p == '\t') 1771 mdb_iob_tab(iob); 1772 else 1773 mdb_iob_nl(iob); 1774 1775 nleft -= (size_t)(p - q) + 1; /* Update byte count */ 1776 q = p + 1; /* Advance past delimiter */ 1777 } 1778 1779 /* 1780 * For a given string component, we determine how many bytes (n) we can 1781 * copy into our buffer (limited by either cols or bufsiz depending 1782 * on whether AUTOWRAP is on), copy a chunk into the buffer, and 1783 * flush the buffer if we reach the end of a line. 1784 */ 1785 while (nleft != 0) { 1786 if (iob->iob_flags & MDB_IOB_AUTOWRAP) { 1787 ASSERT(iob->iob_cols >= iob->iob_nbytes); 1788 n = iob->iob_cols - iob->iob_nbytes; 1789 } else { 1790 ASSERT(iob->iob_bufsiz >= iob->iob_nbytes); 1791 n = iob->iob_bufsiz - iob->iob_nbytes; 1792 } 1793 1794 m = MIN(nleft, n); /* copy at most n bytes in this pass */ 1795 1796 bcopy(q, iob->iob_bufp, m); 1797 nleft -= m; 1798 q += m; 1799 1800 iob->iob_bufp += m; 1801 iob->iob_nbytes += m; 1802 1803 if (m == n && nleft != 0) { 1804 if (iob->iob_flags & MDB_IOB_AUTOWRAP) 1805 mdb_iob_nl(iob); 1806 else 1807 mdb_iob_flush(iob); 1808 } 1809 } 1810 } 1811 1812 void 1813 mdb_iob_puts(mdb_iob_t *iob, const char *s) 1814 { 1815 mdb_iob_nputs(iob, s, strlen(s)); 1816 } 1817 1818 void 1819 mdb_iob_putc(mdb_iob_t *iob, int c) 1820 { 1821 mdb_iob_fill(iob, c, 1); 1822 } 1823 1824 void 1825 mdb_iob_tab(mdb_iob_t *iob) 1826 { 1827 ASSERT(iob->iob_flags & MDB_IOB_WRONLY); 1828 1829 if (iob->iob_tabstop != 0) { 1830 /* 1831 * Round up to the next multiple of the tabstop. If this puts 1832 * us off the end of the line, just insert a newline; otherwise 1833 * insert sufficient whitespace to reach position n. 1834 */ 1835 size_t n = (iob->iob_nbytes + iob->iob_tabstop) / 1836 iob->iob_tabstop * iob->iob_tabstop; 1837 1838 if (n < iob->iob_cols) 1839 mdb_iob_fill(iob, ' ', n - iob->iob_nbytes); 1840 else 1841 mdb_iob_nl(iob); 1842 } 1843 } 1844 1845 void 1846 mdb_iob_fill(mdb_iob_t *iob, int c, size_t nfill) 1847 { 1848 size_t i, m, n; 1849 1850 ASSERT(iob->iob_flags & MDB_IOB_WRONLY); 1851 1852 while (nfill != 0) { 1853 if (iob->iob_flags & MDB_IOB_AUTOWRAP) { 1854 ASSERT(iob->iob_cols >= iob->iob_nbytes); 1855 n = iob->iob_cols - iob->iob_nbytes; 1856 } else { 1857 ASSERT(iob->iob_bufsiz >= iob->iob_nbytes); 1858 n = iob->iob_bufsiz - iob->iob_nbytes; 1859 } 1860 1861 m = MIN(nfill, n); /* fill at most n bytes in this pass */ 1862 1863 for (i = 0; i < m; i++) 1864 *iob->iob_bufp++ = (char)c; 1865 1866 iob->iob_nbytes += m; 1867 nfill -= m; 1868 1869 if (m == n && nfill != 0) { 1870 if (iob->iob_flags & MDB_IOB_AUTOWRAP) 1871 mdb_iob_nl(iob); 1872 else 1873 mdb_iob_flush(iob); 1874 } 1875 } 1876 } 1877 1878 void 1879 mdb_iob_ws(mdb_iob_t *iob, size_t n) 1880 { 1881 if (iob->iob_nbytes + n < iob->iob_cols) 1882 mdb_iob_fill(iob, ' ', n); 1883 else 1884 mdb_iob_nl(iob); 1885 } 1886 1887 void 1888 mdb_iob_nl(mdb_iob_t *iob) 1889 { 1890 ASSERT(iob->iob_flags & MDB_IOB_WRONLY); 1891 1892 if (iob->iob_nbytes == iob->iob_bufsiz) 1893 mdb_iob_flush(iob); 1894 1895 *iob->iob_bufp++ = '\n'; 1896 iob->iob_nbytes++; 1897 1898 mdb_iob_flush(iob); 1899 } 1900 1901 ssize_t 1902 mdb_iob_ngets(mdb_iob_t *iob, char *buf, size_t n) 1903 { 1904 ssize_t resid = n - 1; 1905 ssize_t len; 1906 int c; 1907 1908 if (iob->iob_flags & (MDB_IOB_WRONLY | MDB_IOB_EOF)) 1909 return (EOF); /* can't gets a write buf or a read buf at EOF */ 1910 1911 if (n == 0) 1912 return (0); /* we need room for a terminating \0 */ 1913 1914 while (resid != 0) { 1915 if (iob->iob_nbytes == 0 && iob_read(iob, iob->iob_iop) <= 0) 1916 goto done; /* failed to refill buffer */ 1917 1918 for (len = MIN(iob->iob_nbytes, resid); len != 0; len--) { 1919 c = *iob->iob_bufp++; 1920 iob->iob_nbytes--; 1921 1922 if (c == EOF || c == '\n') 1923 goto done; 1924 1925 *buf++ = (char)c; 1926 resid--; 1927 } 1928 } 1929 done: 1930 *buf = '\0'; 1931 return (n - resid - 1); 1932 } 1933 1934 int 1935 mdb_iob_getc(mdb_iob_t *iob) 1936 { 1937 int c; 1938 1939 if (iob->iob_flags & (MDB_IOB_WRONLY | MDB_IOB_EOF | MDB_IOB_ERR)) 1940 return (EOF); /* can't getc if write-only, EOF, or error bit */ 1941 1942 if (iob->iob_nbytes == 0 && iob_read(iob, iob->iob_iop) <= 0) 1943 return (EOF); /* failed to refill buffer */ 1944 1945 c = (uchar_t)*iob->iob_bufp++; 1946 iob->iob_nbytes--; 1947 1948 return (c); 1949 } 1950 1951 int 1952 mdb_iob_ungetc(mdb_iob_t *iob, int c) 1953 { 1954 if (iob->iob_flags & (MDB_IOB_WRONLY | MDB_IOB_ERR)) 1955 return (EOF); /* can't ungetc if write-only or error bit set */ 1956 1957 if (c == EOF || iob->iob_nbytes == iob->iob_bufsiz) 1958 return (EOF); /* can't ungetc EOF, or ungetc if buffer full */ 1959 1960 *--iob->iob_bufp = (char)c; 1961 iob->iob_nbytes++; 1962 iob->iob_flags &= ~MDB_IOB_EOF; 1963 1964 return (c); 1965 } 1966 1967 int 1968 mdb_iob_eof(mdb_iob_t *iob) 1969 { 1970 return ((iob->iob_flags & (MDB_IOB_RDONLY | MDB_IOB_EOF)) == 1971 (MDB_IOB_RDONLY | MDB_IOB_EOF)); 1972 } 1973 1974 int 1975 mdb_iob_err(mdb_iob_t *iob) 1976 { 1977 return ((iob->iob_flags & MDB_IOB_ERR) == MDB_IOB_ERR); 1978 } 1979 1980 ssize_t 1981 mdb_iob_read(mdb_iob_t *iob, void *buf, size_t n) 1982 { 1983 ssize_t resid = n; 1984 ssize_t len; 1985 1986 if (iob->iob_flags & (MDB_IOB_WRONLY | MDB_IOB_EOF | MDB_IOB_ERR)) 1987 return (0); /* can't read if write-only, eof, or error */ 1988 1989 while (resid != 0) { 1990 if (iob->iob_nbytes == 0 && iob_read(iob, iob->iob_iop) <= 0) 1991 break; /* failed to refill buffer */ 1992 1993 len = MIN(resid, iob->iob_nbytes); 1994 bcopy(iob->iob_bufp, buf, len); 1995 1996 iob->iob_bufp += len; 1997 iob->iob_nbytes -= len; 1998 1999 buf = (char *)buf + len; 2000 resid -= len; 2001 } 2002 2003 return (n - resid); 2004 } 2005 2006 /* 2007 * For now, all binary writes are performed unbuffered. This has the 2008 * side effect that the pager will not be triggered by mdb_iob_write. 2009 */ 2010 ssize_t 2011 mdb_iob_write(mdb_iob_t *iob, const void *buf, size_t n) 2012 { 2013 ssize_t ret; 2014 2015 if (iob->iob_flags & MDB_IOB_ERR) 2016 return (set_errno(EIO)); 2017 if (iob->iob_flags & MDB_IOB_RDONLY) 2018 return (set_errno(EMDB_IORO)); 2019 2020 mdb_iob_flush(iob); 2021 ret = iob_write(iob, iob->iob_iop, buf, n); 2022 2023 if (ret < 0 && iob == mdb.m_out) 2024 longjmp(mdb.m_frame->f_pcb, MDB_ERR_OUTPUT); 2025 2026 return (ret); 2027 } 2028 2029 int 2030 mdb_iob_ctl(mdb_iob_t *iob, int req, void *arg) 2031 { 2032 return (IOP_CTL(iob->iob_iop, req, arg)); 2033 } 2034 2035 const char * 2036 mdb_iob_name(mdb_iob_t *iob) 2037 { 2038 if (iob == NULL) 2039 return ("<NULL>"); 2040 2041 return (IOP_NAME(iob->iob_iop)); 2042 } 2043 2044 size_t 2045 mdb_iob_lineno(mdb_iob_t *iob) 2046 { 2047 return (iob->iob_lineno); 2048 } 2049 2050 size_t 2051 mdb_iob_gettabstop(mdb_iob_t *iob) 2052 { 2053 return (iob->iob_tabstop); 2054 } 2055 2056 size_t 2057 mdb_iob_getmargin(mdb_iob_t *iob) 2058 { 2059 return (iob->iob_margin); 2060 } 2061 2062 mdb_io_t * 2063 mdb_io_hold(mdb_io_t *io) 2064 { 2065 io->io_refcnt++; 2066 return (io); 2067 } 2068 2069 void 2070 mdb_io_rele(mdb_io_t *io) 2071 { 2072 ASSERT(io->io_refcnt != 0); 2073 2074 if (--io->io_refcnt == 0) { 2075 IOP_CLOSE(io); 2076 mdb_free(io, sizeof (mdb_io_t)); 2077 } 2078 } 2079 2080 void 2081 mdb_io_destroy(mdb_io_t *io) 2082 { 2083 ASSERT(io->io_refcnt == 0); 2084 IOP_CLOSE(io); 2085 mdb_free(io, sizeof (mdb_io_t)); 2086 } 2087 2088 void 2089 mdb_iob_stack_create(mdb_iob_stack_t *stk) 2090 { 2091 stk->stk_top = NULL; 2092 stk->stk_size = 0; 2093 } 2094 2095 void 2096 mdb_iob_stack_destroy(mdb_iob_stack_t *stk) 2097 { 2098 mdb_iob_t *top, *ntop; 2099 2100 for (top = stk->stk_top; top != NULL; top = ntop) { 2101 ntop = top->iob_next; 2102 mdb_iob_destroy(top); 2103 } 2104 } 2105 2106 void 2107 mdb_iob_stack_push(mdb_iob_stack_t *stk, mdb_iob_t *iob, size_t lineno) 2108 { 2109 iob->iob_lineno = lineno; 2110 iob->iob_next = stk->stk_top; 2111 stk->stk_top = iob; 2112 stk->stk_size++; 2113 yylineno = 1; 2114 } 2115 2116 mdb_iob_t * 2117 mdb_iob_stack_pop(mdb_iob_stack_t *stk) 2118 { 2119 mdb_iob_t *top = stk->stk_top; 2120 2121 ASSERT(top != NULL); 2122 2123 stk->stk_top = top->iob_next; 2124 top->iob_next = NULL; 2125 stk->stk_size--; 2126 2127 return (top); 2128 } 2129 2130 size_t 2131 mdb_iob_stack_size(mdb_iob_stack_t *stk) 2132 { 2133 return (stk->stk_size); 2134 } 2135 2136 /* 2137 * Stub functions for i/o backend implementors: these stubs either act as 2138 * pass-through no-ops or return ENOTSUP as appropriate. 2139 */ 2140 ssize_t 2141 no_io_read(mdb_io_t *io, void *buf, size_t nbytes) 2142 { 2143 if (io->io_next != NULL) 2144 return (IOP_READ(io->io_next, buf, nbytes)); 2145 2146 return (set_errno(EMDB_IOWO)); 2147 } 2148 2149 ssize_t 2150 no_io_write(mdb_io_t *io, const void *buf, size_t nbytes) 2151 { 2152 if (io->io_next != NULL) 2153 return (IOP_WRITE(io->io_next, buf, nbytes)); 2154 2155 return (set_errno(EMDB_IORO)); 2156 } 2157 2158 off64_t 2159 no_io_seek(mdb_io_t *io, off64_t offset, int whence) 2160 { 2161 if (io->io_next != NULL) 2162 return (IOP_SEEK(io->io_next, offset, whence)); 2163 2164 return (set_errno(ENOTSUP)); 2165 } 2166 2167 int 2168 no_io_ctl(mdb_io_t *io, int req, void *arg) 2169 { 2170 if (io->io_next != NULL) 2171 return (IOP_CTL(io->io_next, req, arg)); 2172 2173 return (set_errno(ENOTSUP)); 2174 } 2175 2176 /*ARGSUSED*/ 2177 void 2178 no_io_close(mdb_io_t *io) 2179 { 2180 /* 2181 * Note that we do not propagate IOP_CLOSE down the io stack. IOP_CLOSE should 2182 * only be called by mdb_io_rele when an io's reference count has gone to zero. 2183 */ 2184 } 2185 2186 const char * 2187 no_io_name(mdb_io_t *io) 2188 { 2189 if (io->io_next != NULL) 2190 return (IOP_NAME(io->io_next)); 2191 2192 return ("(anonymous)"); 2193 } 2194 2195 void 2196 no_io_link(mdb_io_t *io, mdb_iob_t *iob) 2197 { 2198 if (io->io_next != NULL) 2199 IOP_LINK(io->io_next, iob); 2200 } 2201 2202 void 2203 no_io_unlink(mdb_io_t *io, mdb_iob_t *iob) 2204 { 2205 if (io->io_next != NULL) 2206 IOP_UNLINK(io->io_next, iob); 2207 } 2208 2209 int 2210 no_io_setattr(mdb_io_t *io, int req, uint_t attrs) 2211 { 2212 if (io->io_next != NULL) 2213 return (IOP_SETATTR(io->io_next, req, attrs)); 2214 2215 return (set_errno(ENOTSUP)); 2216 } 2217 2218 void 2219 no_io_suspend(mdb_io_t *io) 2220 { 2221 if (io->io_next != NULL) 2222 IOP_SUSPEND(io->io_next); 2223 } 2224 2225 void 2226 no_io_resume(mdb_io_t *io) 2227 { 2228 if (io->io_next != NULL) 2229 IOP_RESUME(io->io_next); 2230 } 2231 2232 /* 2233 * Iterate over the varargs. The first item indicates the mode: 2234 * MDB_TBL_PRNT 2235 * pull out the next vararg as a const char * and pass it and the 2236 * remaining varargs to iob_doprnt; if we want to print the column, 2237 * direct the output to mdb.m_out otherwise direct it to mdb.m_null 2238 * 2239 * MDB_TBL_FUNC 2240 * pull out the next vararg as type mdb_table_print_f and the 2241 * following one as a void * argument to the function; call the 2242 * function with the given argument if we want to print the column 2243 * 2244 * The second item indicates the flag; if the flag is set in the flags 2245 * argument, then the column is printed. A flag value of 0 indicates 2246 * that the column should always be printed. 2247 */ 2248 void 2249 mdb_table_print(uint_t flags, const char *delimeter, ...) 2250 { 2251 va_list alist; 2252 uint_t flg; 2253 uint_t type; 2254 const char *fmt; 2255 mdb_table_print_f *func; 2256 void *arg; 2257 mdb_iob_t *out; 2258 mdb_bool_t first = TRUE; 2259 mdb_bool_t print; 2260 2261 va_start(alist, delimeter); 2262 2263 while ((type = va_arg(alist, uint_t)) != MDB_TBL_DONE) { 2264 flg = va_arg(alist, uint_t); 2265 2266 print = flg == 0 || (flg & flags) != 0; 2267 2268 if (print) { 2269 if (first) 2270 first = FALSE; 2271 else 2272 mdb_printf("%s", delimeter); 2273 } 2274 2275 switch (type) { 2276 case MDB_TBL_PRNT: { 2277 varglist_t ap = { VAT_VARARGS }; 2278 fmt = va_arg(alist, const char *); 2279 out = print ? mdb.m_out : mdb.m_null; 2280 va_copy(ap.val_valist, alist); 2281 iob_doprnt(out, fmt, &ap); 2282 va_end(alist); 2283 va_copy(alist, ap.val_valist); 2284 break; 2285 } 2286 2287 case MDB_TBL_FUNC: 2288 func = va_arg(alist, mdb_table_print_f *); 2289 arg = va_arg(alist, void *); 2290 2291 if (print) 2292 func(arg); 2293 2294 break; 2295 2296 default: 2297 warn("bad format type %x\n", type); 2298 break; 2299 } 2300 } 2301 2302 va_end(alist); 2303 } 2304