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