1 /* 2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk (at) cs.few.eur.nl> 3 * Copyright (c) 1993 Branko Lankester <branko (at) hacktic.nl> 4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs (at) world.std.com> 5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert (at) cistron.nl> 6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation 7 * Linux for s390 port by D.J. Barrow 8 * <barrow_dj (at) mail.yahoo.com,djbarrow (at) de.ibm.com> 9 * Copyright (c) 1999-2018 The strace developers. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "defs.h" 36 #include <limits.h> 37 #include <fcntl.h> 38 #include <stdarg.h> 39 #ifdef HAVE_SYS_XATTR_H 40 # include <sys/xattr.h> 41 #endif 42 #include <sys/uio.h> 43 #include "xstring.h" 44 45 int 46 tv_nz(const struct timeval *a) 47 { 48 return a->tv_sec || a->tv_usec; 49 } 50 51 int 52 tv_cmp(const struct timeval *a, const struct timeval *b) 53 { 54 if (a->tv_sec < b->tv_sec 55 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) 56 return -1; 57 if (a->tv_sec > b->tv_sec 58 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec)) 59 return 1; 60 return 0; 61 } 62 63 double 64 tv_float(const struct timeval *tv) 65 { 66 return tv->tv_sec + tv->tv_usec/1000000.0; 67 } 68 69 void 70 tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b) 71 { 72 tv->tv_sec = a->tv_sec + b->tv_sec; 73 tv->tv_usec = a->tv_usec + b->tv_usec; 74 if (tv->tv_usec >= 1000000) { 75 tv->tv_sec++; 76 tv->tv_usec -= 1000000; 77 } 78 } 79 80 void 81 tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b) 82 { 83 tv->tv_sec = a->tv_sec - b->tv_sec; 84 tv->tv_usec = a->tv_usec - b->tv_usec; 85 if (((long) tv->tv_usec) < 0) { 86 tv->tv_sec--; 87 tv->tv_usec += 1000000; 88 } 89 } 90 91 void 92 tv_div(struct timeval *tv, const struct timeval *a, int n) 93 { 94 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n; 95 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000; 96 tv->tv_usec %= 1000000; 97 } 98 99 void 100 tv_mul(struct timeval *tv, const struct timeval *a, int n) 101 { 102 tv->tv_usec = a->tv_usec * n; 103 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000; 104 tv->tv_usec %= 1000000; 105 } 106 107 #if !defined HAVE_STPCPY 108 char * 109 stpcpy(char *dst, const char *src) 110 { 111 while ((*dst = *src++) != '\0') 112 dst++; 113 return dst; 114 } 115 #endif 116 117 /* Find a next bit which is set. 118 * Starts testing at cur_bit. 119 * Returns -1 if no more bits are set. 120 * 121 * We never touch bytes we don't need to. 122 * On big-endian, array is assumed to consist of 123 * current_wordsize wide words: for example, is current_wordsize is 4, 124 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence. 125 * On little-endian machines, word size is immaterial. 126 */ 127 int 128 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits) 129 { 130 const unsigned endian = 1; 131 int little_endian = *(char *) (void *) &endian; 132 133 const uint8_t *array = bit_array; 134 unsigned pos = cur_bit / 8; 135 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1; 136 137 for (;;) { 138 uint8_t bitmask; 139 uint8_t cur_byte; 140 141 if (cur_bit >= size_bits) 142 return -1; 143 cur_byte = array[pos ^ pos_xor_mask]; 144 if (cur_byte == 0) { 145 cur_bit = (cur_bit + 8) & (-8); 146 pos++; 147 continue; 148 } 149 bitmask = 1 << (cur_bit & 7); 150 for (;;) { 151 if (cur_byte & bitmask) 152 return cur_bit; 153 cur_bit++; 154 if (cur_bit >= size_bits) 155 return -1; 156 bitmask <<= 1; 157 /* This check *can't be* optimized out: */ 158 if (bitmask == 0) 159 break; 160 } 161 pos++; 162 } 163 } 164 165 /* 166 * Fetch 64bit argument at position arg_no and 167 * return the index of the next argument. 168 */ 169 int 170 getllval(struct tcb *tcp, unsigned long long *val, int arg_no) 171 { 172 #if SIZEOF_KERNEL_LONG_T > 4 173 # ifndef current_klongsize 174 if (current_klongsize < SIZEOF_KERNEL_LONG_T) { 175 # if defined(AARCH64) || defined(POWERPC64) 176 /* Align arg_no to the next even number. */ 177 arg_no = (arg_no + 1) & 0xe; 178 # endif /* AARCH64 || POWERPC64 */ 179 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]); 180 arg_no += 2; 181 } else 182 # endif /* !current_klongsize */ 183 { 184 *val = tcp->u_arg[arg_no]; 185 arg_no++; 186 } 187 #else /* SIZEOF_KERNEL_LONG_T == 4 */ 188 # if defined __ARM_EABI__ \ 189 || defined LINUX_MIPSO32 \ 190 || defined POWERPC \ 191 || defined XTENSA 192 /* Align arg_no to the next even number. */ 193 arg_no = (arg_no + 1) & 0xe; 194 # elif defined SH 195 /* 196 * The SH4 ABI does allow long longs in odd-numbered registers, but 197 * does not allow them to be split between registers and memory - and 198 * there are only four argument registers for normal functions. As a 199 * result, pread, for example, takes an extra padding argument before 200 * the offset. This was changed late in the 2.4 series (around 2.4.20). 201 */ 202 if (arg_no == 3) 203 arg_no++; 204 # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */ 205 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]); 206 arg_no += 2; 207 #endif 208 209 return arg_no; 210 } 211 212 /* 213 * Print 64bit argument at position arg_no and 214 * return the index of the next argument. 215 */ 216 int 217 printllval(struct tcb *tcp, const char *format, int arg_no) 218 { 219 unsigned long long val = 0; 220 221 arg_no = getllval(tcp, &val, arg_no); 222 tprintf(format, val); 223 return arg_no; 224 } 225 226 void 227 printaddr(const kernel_ulong_t addr) 228 { 229 if (!addr) 230 tprints("NULL"); 231 else 232 tprintf("%#" PRI_klx, addr); 233 } 234 235 #define DEF_PRINTNUM(name, type) \ 236 bool \ 237 printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \ 238 const char *const fmt) \ 239 { \ 240 type num; \ 241 if (umove_or_printaddr(tcp, addr, &num)) \ 242 return false; \ 243 tprints("["); \ 244 tprintf(fmt, num); \ 245 tprints("]"); \ 246 return true; \ 247 } 248 249 #define DEF_PRINTNUM_ADDR(name, type) \ 250 bool \ 251 printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \ 252 { \ 253 type num; \ 254 if (umove_or_printaddr(tcp, addr, &num)) \ 255 return false; \ 256 tprints("["); \ 257 printaddr(num); \ 258 tprints("]"); \ 259 return true; \ 260 } 261 262 #define DEF_PRINTPAIR(name, type) \ 263 bool \ 264 printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \ 265 const char *const fmt) \ 266 { \ 267 type pair[2]; \ 268 if (umove_or_printaddr(tcp, addr, &pair)) \ 269 return false; \ 270 tprints("["); \ 271 tprintf(fmt, pair[0]); \ 272 tprints(", "); \ 273 tprintf(fmt, pair[1]); \ 274 tprints("]"); \ 275 return true; \ 276 } 277 278 DEF_PRINTNUM(int, int) 279 DEF_PRINTNUM_ADDR(int, unsigned int) 280 DEF_PRINTPAIR(int, int) 281 DEF_PRINTNUM(short, short) 282 DEF_PRINTNUM(int64, uint64_t) 283 DEF_PRINTNUM_ADDR(int64, uint64_t) 284 DEF_PRINTPAIR(int64, uint64_t) 285 286 #ifndef current_wordsize 287 bool 288 printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr, 289 const char *const fmt_long, const char *const fmt_int) 290 { 291 if (current_wordsize > sizeof(int)) { 292 return printnum_int64(tcp, addr, fmt_long); 293 } else { 294 return printnum_int(tcp, addr, fmt_int); 295 } 296 } 297 298 bool 299 printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr) 300 { 301 if (current_wordsize > sizeof(int)) { 302 return printnum_addr_int64(tcp, addr); 303 } else { 304 return printnum_addr_int(tcp, addr); 305 } 306 } 307 #endif /* !current_wordsize */ 308 309 #ifndef current_klongsize 310 bool 311 printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr) 312 { 313 if (current_klongsize > sizeof(int)) { 314 return printnum_addr_int64(tcp, addr); 315 } else { 316 return printnum_addr_int(tcp, addr); 317 } 318 } 319 #endif /* !current_klongsize */ 320 321 /** 322 * Prints time to a (static internal) buffer and returns pointer to it. 323 * 324 * @param sec Seconds since epoch. 325 * @param part_sec Amount of second parts since the start of a second. 326 * @param max_part_sec Maximum value of a valid part_sec. 327 * @param width 1 + floor(log10(max_part_sec)). 328 */ 329 static const char * 330 sprinttime_ex(const long long sec, const unsigned long long part_sec, 331 const unsigned int max_part_sec, const int width) 332 { 333 static char buf[sizeof(int) * 3 * 6 + sizeof(part_sec) * 3 334 + sizeof("+0000")]; 335 336 if ((sec == 0 && part_sec == 0) || part_sec > max_part_sec) 337 return NULL; 338 339 time_t t = (time_t) sec; 340 struct tm *tmp = (sec == t) ? localtime(&t) : NULL; 341 if (!tmp) 342 return NULL; 343 344 size_t pos = strftime(buf, sizeof(buf), "%FT%T", tmp); 345 if (!pos) 346 return NULL; 347 348 if (part_sec > 0) 349 pos += xsnprintf(buf + pos, sizeof(buf) - pos, ".%0*llu", 350 width, part_sec); 351 352 return strftime(buf + pos, sizeof(buf) - pos, "%z", tmp) ? buf : NULL; 353 } 354 355 const char * 356 sprinttime(long long sec) 357 { 358 return sprinttime_ex(sec, 0, 0, 0); 359 } 360 361 const char * 362 sprinttime_usec(long long sec, unsigned long long usec) 363 { 364 return sprinttime_ex(sec, usec, 999999, 6); 365 } 366 367 const char * 368 sprinttime_nsec(long long sec, unsigned long long nsec) 369 { 370 return sprinttime_ex(sec, nsec, 999999999, 9); 371 } 372 373 enum sock_proto 374 getfdproto(struct tcb *tcp, int fd) 375 { 376 #ifdef HAVE_SYS_XATTR_H 377 size_t bufsize = 256; 378 char buf[bufsize]; 379 ssize_t r; 380 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3]; 381 382 if (fd < 0) 383 return SOCK_PROTO_UNKNOWN; 384 385 xsprintf(path, "/proc/%u/fd/%u", tcp->pid, fd); 386 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1); 387 if (r <= 0) 388 return SOCK_PROTO_UNKNOWN; 389 else { 390 /* 391 * This is a protection for the case when the kernel 392 * side does not append a null byte to the buffer. 393 */ 394 buf[r] = '\0'; 395 396 return get_proto_by_name(buf); 397 } 398 #else 399 return SOCK_PROTO_UNKNOWN; 400 #endif 401 } 402 403 unsigned long 404 getfdinode(struct tcb *tcp, int fd) 405 { 406 char path[PATH_MAX + 1]; 407 408 if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) { 409 const char *str = STR_STRIP_PREFIX(path, "socket:["); 410 411 if (str != path) { 412 const size_t str_len = strlen(str); 413 if (str_len && str[str_len - 1] == ']') 414 return strtoul(str, NULL, 10); 415 } 416 } 417 418 return 0; 419 } 420 421 void 422 printfd(struct tcb *tcp, int fd) 423 { 424 char path[PATH_MAX + 1]; 425 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) { 426 const char *str; 427 size_t len; 428 unsigned long inode; 429 430 tprintf("%d<", fd); 431 if (show_fd_path <= 1 432 || (str = STR_STRIP_PREFIX(path, "socket:[")) == path 433 || !(len = strlen(str)) 434 || str[len - 1] != ']' 435 || !(inode = strtoul(str, NULL, 10)) 436 || !print_sockaddr_by_inode(tcp, fd, inode)) { 437 print_quoted_string(path, strlen(path), 438 QUOTE_OMIT_LEADING_TRAILING_QUOTES); 439 } 440 tprints(">"); 441 } else 442 tprintf("%d", fd); 443 } 444 445 /* 446 * Quote string `instr' of length `size' 447 * Write up to (3 + `size' * 4) bytes to `outstr' buffer. 448 * 449 * If QUOTE_0_TERMINATED `style' flag is set, 450 * treat `instr' as a NUL-terminated string, 451 * checking up to (`size' + 1) bytes of `instr'. 452 * 453 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set, 454 * do not add leading and trailing quoting symbols. 455 * 456 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise. 457 * Note that if QUOTE_0_TERMINATED is not set, always returns 1. 458 */ 459 int 460 string_quote(const char *instr, char *outstr, const unsigned int size, 461 const unsigned int style) 462 { 463 const unsigned char *ustr = (const unsigned char *) instr; 464 char *s = outstr; 465 unsigned int i; 466 int usehex, c, eol; 467 468 if (style & QUOTE_0_TERMINATED) 469 eol = '\0'; 470 else 471 eol = 0x100; /* this can never match a char */ 472 473 usehex = 0; 474 if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) { 475 usehex = 1; 476 } else if (xflag) { 477 /* Check for presence of symbol which require 478 to hex-quote the whole string. */ 479 for (i = 0; i < size; ++i) { 480 c = ustr[i]; 481 /* Check for NUL-terminated string. */ 482 if (c == eol) 483 break; 484 485 /* Force hex unless c is printable or whitespace */ 486 if (c > 0x7e) { 487 usehex = 1; 488 break; 489 } 490 /* In ASCII isspace is only these chars: "\t\n\v\f\r". 491 * They happen to have ASCII codes 9,10,11,12,13. 492 */ 493 if (c < ' ' && (unsigned)(c - 9) >= 5) { 494 usehex = 1; 495 break; 496 } 497 } 498 } 499 500 if (style & QUOTE_EMIT_COMMENT) 501 s = stpcpy(s, " /* "); 502 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES)) 503 *s++ = '\"'; 504 505 if (usehex) { 506 /* Hex-quote the whole string. */ 507 for (i = 0; i < size; ++i) { 508 c = ustr[i]; 509 /* Check for NUL-terminated string. */ 510 if (c == eol) 511 goto asciz_ended; 512 *s++ = '\\'; 513 *s++ = 'x'; 514 *s++ = "0123456789abcdef"[c >> 4]; 515 *s++ = "0123456789abcdef"[c & 0xf]; 516 } 517 } else { 518 for (i = 0; i < size; ++i) { 519 c = ustr[i]; 520 /* Check for NUL-terminated string. */ 521 if (c == eol) 522 goto asciz_ended; 523 if ((i == (size - 1)) && 524 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0')) 525 goto asciz_ended; 526 switch (c) { 527 case '\"': case '\\': 528 *s++ = '\\'; 529 *s++ = c; 530 break; 531 case '\f': 532 *s++ = '\\'; 533 *s++ = 'f'; 534 break; 535 case '\n': 536 *s++ = '\\'; 537 *s++ = 'n'; 538 break; 539 case '\r': 540 *s++ = '\\'; 541 *s++ = 'r'; 542 break; 543 case '\t': 544 *s++ = '\\'; 545 *s++ = 't'; 546 break; 547 case '\v': 548 *s++ = '\\'; 549 *s++ = 'v'; 550 break; 551 default: 552 if (c >= ' ' && c <= 0x7e) 553 *s++ = c; 554 else { 555 /* Print \octal */ 556 *s++ = '\\'; 557 if (i + 1 < size 558 && ustr[i + 1] >= '0' 559 && ustr[i + 1] <= '9' 560 ) { 561 /* Print \ooo */ 562 *s++ = '0' + (c >> 6); 563 *s++ = '0' + ((c >> 3) & 0x7); 564 } else { 565 /* Print \[[o]o]o */ 566 if ((c >> 3) != 0) { 567 if ((c >> 6) != 0) 568 *s++ = '0' + (c >> 6); 569 *s++ = '0' + ((c >> 3) & 0x7); 570 } 571 } 572 *s++ = '0' + (c & 0x7); 573 } 574 break; 575 } 576 } 577 } 578 579 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES)) 580 *s++ = '\"'; 581 if (style & QUOTE_EMIT_COMMENT) 582 s = stpcpy(s, " */"); 583 *s = '\0'; 584 585 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */ 586 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') { 587 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended') 588 * but next char is NUL. 589 */ 590 return 0; 591 } 592 593 return 1; 594 595 asciz_ended: 596 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES)) 597 *s++ = '\"'; 598 if (style & QUOTE_EMIT_COMMENT) 599 s = stpcpy(s, " */"); 600 *s = '\0'; 601 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */ 602 return 0; 603 } 604 605 #ifndef ALLOCA_CUTOFF 606 # define ALLOCA_CUTOFF 4032 607 #endif 608 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF) 609 610 /* 611 * Quote string `str' of length `size' and print the result. 612 * 613 * If QUOTE_0_TERMINATED `style' flag is set, 614 * treat `str' as a NUL-terminated string and 615 * quote at most (`size' - 1) bytes. 616 * 617 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set, 618 * do not add leading and trailing quoting symbols. 619 * 620 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise. 621 * Note that if QUOTE_0_TERMINATED is not set, always returns 1. 622 */ 623 int 624 print_quoted_string(const char *str, unsigned int size, 625 const unsigned int style) 626 { 627 char *buf; 628 char *outstr; 629 unsigned int alloc_size; 630 int rc; 631 632 if (size && style & QUOTE_0_TERMINATED) 633 --size; 634 635 alloc_size = 4 * size; 636 if (alloc_size / 4 != size) { 637 error_msg("Out of memory"); 638 tprints("???"); 639 return -1; 640 } 641 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2) + 642 (style & QUOTE_EMIT_COMMENT ? 7 : 0); 643 644 if (use_alloca(alloc_size)) { 645 outstr = alloca(alloc_size); 646 buf = NULL; 647 } else { 648 outstr = buf = malloc(alloc_size); 649 if (!buf) { 650 error_msg("Out of memory"); 651 tprints("???"); 652 return -1; 653 } 654 } 655 656 rc = string_quote(str, outstr, size, style); 657 tprints(outstr); 658 659 free(buf); 660 return rc; 661 } 662 663 /* 664 * Quote a NUL-terminated string `str' of length up to `size' - 1 665 * and print the result. 666 * 667 * Returns 0 if NUL was seen, 1 otherwise. 668 */ 669 int 670 print_quoted_cstring(const char *str, unsigned int size) 671 { 672 int unterminated = 673 print_quoted_string(str, size, QUOTE_0_TERMINATED); 674 675 if (unterminated) 676 tprints("..."); 677 678 return unterminated; 679 } 680 681 /* 682 * Print path string specified by address `addr' and length `n'. 683 * If path length exceeds `n', append `...' to the output. 684 * 685 * Returns the result of umovenstr. 686 */ 687 int 688 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n) 689 { 690 char path[PATH_MAX]; 691 int nul_seen; 692 693 if (!addr) { 694 tprints("NULL"); 695 return -1; 696 } 697 698 /* Cap path length to the path buffer size */ 699 if (n > sizeof(path) - 1) 700 n = sizeof(path) - 1; 701 702 /* Fetch one byte more to find out whether path length > n. */ 703 nul_seen = umovestr(tcp, addr, n + 1, path); 704 if (nul_seen < 0) 705 printaddr(addr); 706 else { 707 path[n++] = !nul_seen; 708 print_quoted_cstring(path, n); 709 } 710 711 return nul_seen; 712 } 713 714 int 715 printpath(struct tcb *const tcp, const kernel_ulong_t addr) 716 { 717 /* Size must correspond to char path[] size in printpathn */ 718 return printpathn(tcp, addr, PATH_MAX - 1); 719 } 720 721 /* 722 * Print string specified by address `addr' and length `len'. 723 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string 724 * as a NUL-terminated string. 725 * Pass `user_style' on to `string_quote'. 726 * Append `...' to the output if either the string length exceeds `max_strlen', 727 * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'. 728 * 729 * Returns the result of umovenstr if style has QUOTE_0_TERMINATED, 730 * or the result of umoven otherwise. 731 */ 732 int 733 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr, 734 const kernel_ulong_t len, const unsigned int user_style) 735 { 736 static char *str; 737 static char *outstr; 738 739 unsigned int size; 740 unsigned int style = user_style; 741 int rc; 742 int ellipsis; 743 744 if (!addr) { 745 tprints("NULL"); 746 return -1; 747 } 748 /* Allocate static buffers if they are not allocated yet. */ 749 if (!str) { 750 const unsigned int outstr_size = 751 4 * max_strlen + /* for quotes and NUL */ 3; 752 /* 753 * We can assume that outstr_size / 4 == max_strlen 754 * since we have a guarantee that max_strlen <= -1U / 4. 755 */ 756 757 str = xmalloc(max_strlen + 1); 758 outstr = xmalloc(outstr_size); 759 } 760 761 /* Fetch one byte more because string_quote may look one byte ahead. */ 762 size = max_strlen + 1; 763 764 if (size > len) 765 size = len; 766 if (style & QUOTE_0_TERMINATED) 767 rc = umovestr(tcp, addr, size, str); 768 else 769 rc = umoven(tcp, addr, size, str); 770 771 if (rc < 0) { 772 printaddr(addr); 773 return rc; 774 } 775 776 if (size > max_strlen) 777 size = max_strlen; 778 else 779 str[size] = '\xff'; 780 781 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str 782 * or we were requested to print more than -s NUM chars)... 783 */ 784 ellipsis = string_quote(str, outstr, size, style) 785 && len 786 && ((style & QUOTE_0_TERMINATED) 787 || len > max_strlen); 788 789 tprints(outstr); 790 if (ellipsis) 791 tprints("..."); 792 793 return rc; 794 } 795 796 void 797 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr, 798 kernel_ulong_t data_size) 799 { 800 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG 801 union { 802 struct { uint32_t base; uint32_t len; } *iov32; 803 struct { uint64_t base; uint64_t len; } *iov64; 804 } iovu; 805 #define iov iovu.iov64 806 #define sizeof_iov \ 807 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64)) 808 #define iov_iov_base(i) \ 809 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base) 810 #define iov_iov_len(i) \ 811 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len) 812 #else 813 struct iovec *iov; 814 #define sizeof_iov sizeof(*iov) 815 #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base) 816 #define iov_iov_len(i) iov[i].iov_len 817 #endif 818 int i; 819 unsigned size; 820 821 size = sizeof_iov * len; 822 /* Assuming no sane program has millions of iovs */ 823 if ((unsigned)len > 1024*1024 /* insane or negative size? */ 824 || (iov = malloc(size)) == NULL) { 825 error_msg("Out of memory"); 826 return; 827 } 828 if (umoven(tcp, addr, size, iov) >= 0) { 829 for (i = 0; i < len; i++) { 830 kernel_ulong_t iov_len = iov_iov_len(i); 831 if (iov_len > data_size) 832 iov_len = data_size; 833 if (!iov_len) 834 break; 835 data_size -= iov_len; 836 /* include the buffer number to make it easy to 837 * match up the trace with the source */ 838 tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i); 839 dumpstr(tcp, iov_iov_base(i), iov_len); 840 } 841 } 842 free(iov); 843 #undef sizeof_iov 844 #undef iov_iov_base 845 #undef iov_iov_len 846 #undef iov 847 } 848 849 void 850 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len) 851 { 852 static int strsize = -1; 853 static unsigned char *str; 854 855 char outbuf[ 856 ( 857 (sizeof( 858 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx " 859 "1234567890123456") + /*in case I'm off by few:*/ 4) 860 /*align to 8 to make memset easier:*/ + 7) & -8 861 ]; 862 const unsigned char *src; 863 int i; 864 865 memset(outbuf, ' ', sizeof(outbuf)); 866 867 if (strsize < len + 16) { 868 free(str); 869 str = malloc(len + 16); 870 if (!str) { 871 strsize = -1; 872 error_msg("Out of memory"); 873 return; 874 } 875 strsize = len + 16; 876 } 877 878 if (umoven(tcp, addr, len, str) < 0) 879 return; 880 881 /* Space-pad to 16 bytes */ 882 i = len; 883 while (i & 0xf) 884 str[i++] = ' '; 885 886 i = 0; 887 src = str; 888 while (i < len) { 889 char *dst = outbuf; 890 /* Hex dump */ 891 do { 892 if (i < len) { 893 *dst++ = "0123456789abcdef"[*src >> 4]; 894 *dst++ = "0123456789abcdef"[*src & 0xf]; 895 } else { 896 *dst++ = ' '; 897 *dst++ = ' '; 898 } 899 dst++; /* space is there by memset */ 900 i++; 901 if ((i & 7) == 0) 902 dst++; /* space is there by memset */ 903 src++; 904 } while (i & 0xf); 905 /* ASCII dump */ 906 i -= 16; 907 src -= 16; 908 do { 909 if (*src >= ' ' && *src < 0x7f) 910 *dst++ = *src; 911 else 912 *dst++ = '.'; 913 src++; 914 } while (++i & 0xf); 915 *dst = '\0'; 916 tprintf(" | %05x %s |\n", i - 16, outbuf); 917 } 918 } 919 920 int 921 umoven_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr, 922 const unsigned int len, void *const our_addr) 923 { 924 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) || 925 umoven(tcp, addr, len, our_addr) < 0) { 926 printaddr(addr); 927 return -1; 928 } 929 return 0; 930 } 931 932 int 933 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp, 934 const kernel_ulong_t addr, 935 const unsigned int len, 936 void *const our_addr) 937 { 938 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) { 939 printaddr(addr); 940 return -1; 941 } 942 return 0; 943 } 944 945 /* 946 * Iteratively fetch and print up to nmemb elements of elem_size size 947 * from the array that starts at tracee's address start_addr. 948 * 949 * Array elements are being fetched to the address specified by elem_buf. 950 * 951 * The fetcher callback function specified by umoven_func should follow 952 * the same semantics as umoven_or_printaddr function. 953 * 954 * The printer callback function specified by print_func is expected 955 * to print something; if it returns false, no more iterations will be made. 956 * 957 * The pointer specified by opaque_data is passed to each invocation 958 * of print_func callback function. 959 * 960 * This function prints: 961 * - "NULL", if start_addr is NULL; 962 * - "[]", if nmemb is 0; 963 * - start_addr, if nmemb * elem_size overflows or wraps around; 964 * - nothing, if the first element cannot be fetched 965 * (if umoven_func returns non-zero), but it is assumed that 966 * umoven_func has printed the address it failed to fetch data from; 967 * - elements of the array, delimited by ", ", with the array itself 968 * enclosed with [] brackets. 969 * 970 * If abbrev(tcp) is true, then 971 * - the maximum number of elements printed equals to max_strlen; 972 * - "..." is printed instead of max_strlen+1 element 973 * and no more iterations will be made. 974 * 975 * This function returns true only if 976 * - umoven_func has been called at least once AND 977 * - umoven_func has not returned false. 978 */ 979 bool 980 print_array(struct tcb *const tcp, 981 const kernel_ulong_t start_addr, 982 const size_t nmemb, 983 void *const elem_buf, 984 const size_t elem_size, 985 int (*const umoven_func)(struct tcb *, 986 kernel_ulong_t, 987 unsigned int, 988 void *), 989 bool (*const print_func)(struct tcb *, 990 void *elem_buf, 991 size_t elem_size, 992 void *opaque_data), 993 void *const opaque_data) 994 { 995 if (!start_addr) { 996 tprints("NULL"); 997 return false; 998 } 999 1000 if (!nmemb) { 1001 tprints("[]"); 1002 return false; 1003 } 1004 1005 const size_t size = nmemb * elem_size; 1006 const kernel_ulong_t end_addr = start_addr + size; 1007 1008 if (end_addr <= start_addr || size / elem_size != nmemb) { 1009 printaddr(start_addr); 1010 return false; 1011 } 1012 1013 const kernel_ulong_t abbrev_end = 1014 (abbrev(tcp) && max_strlen < nmemb) ? 1015 start_addr + elem_size * max_strlen : end_addr; 1016 kernel_ulong_t cur; 1017 1018 for (cur = start_addr; cur < end_addr; cur += elem_size) { 1019 if (cur != start_addr) 1020 tprints(", "); 1021 1022 if (umoven_func(tcp, cur, elem_size, elem_buf)) 1023 break; 1024 1025 if (cur == start_addr) 1026 tprints("["); 1027 1028 if (cur >= abbrev_end) { 1029 tprints("..."); 1030 cur = end_addr; 1031 break; 1032 } 1033 1034 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) { 1035 cur = end_addr; 1036 break; 1037 } 1038 } 1039 if (cur != start_addr) 1040 tprints("]"); 1041 1042 return cur >= end_addr; 1043 } 1044 1045 int 1046 printargs(struct tcb *tcp) 1047 { 1048 const int n = tcp->s_ent->nargs; 1049 int i; 1050 for (i = 0; i < n; ++i) 1051 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]); 1052 return RVAL_DECODED; 1053 } 1054 1055 int 1056 printargs_u(struct tcb *tcp) 1057 { 1058 const int n = tcp->s_ent->nargs; 1059 int i; 1060 for (i = 0; i < n; ++i) 1061 tprintf("%s%u", i ? ", " : "", 1062 (unsigned int) tcp->u_arg[i]); 1063 return RVAL_DECODED; 1064 } 1065 1066 int 1067 printargs_d(struct tcb *tcp) 1068 { 1069 const int n = tcp->s_ent->nargs; 1070 int i; 1071 for (i = 0; i < n; ++i) 1072 tprintf("%s%d", i ? ", " : "", 1073 (int) tcp->u_arg[i]); 1074 return RVAL_DECODED; 1075 } 1076 1077 /* Print abnormal high bits of a kernel_ulong_t value. */ 1078 void 1079 print_abnormal_hi(const kernel_ulong_t val) 1080 { 1081 if (current_klongsize > 4) { 1082 const unsigned int hi = (unsigned int) ((uint64_t) val >> 32); 1083 if (hi) 1084 tprintf("%#x<<32|", hi); 1085 } 1086 } 1087 1088 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64 1089 # define open_file open64 1090 #else 1091 # define open_file open 1092 #endif 1093 1094 int 1095 read_int_from_file(struct tcb *tcp, const char *const fname, int *const pvalue) 1096 { 1097 const int fd = open_file(fname, O_RDONLY); 1098 if (fd < 0) 1099 return -1; 1100 1101 long lval; 1102 char buf[sizeof(lval) * 3]; 1103 int n = read(fd, buf, sizeof(buf) - 1); 1104 int saved_errno = errno; 1105 close(fd); 1106 1107 if (n < 0) { 1108 errno = saved_errno; 1109 return -1; 1110 } 1111 1112 buf[n] = '\0'; 1113 char *endptr = 0; 1114 errno = 0; 1115 lval = strtol(buf, &endptr, 10); 1116 if (!endptr || (*endptr && '\n' != *endptr) 1117 #if INT_MAX < LONG_MAX 1118 || lval > INT_MAX || lval < INT_MIN 1119 #endif 1120 || ERANGE == errno) { 1121 if (!errno) 1122 errno = EINVAL; 1123 return -1; 1124 } 1125 1126 *pvalue = (int) lval; 1127 return 0; 1128 } 1129