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