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