1 /*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include "defs.h"
35 #include <sys/param.h>
36 #include <fcntl.h>
37 #include <stdarg.h>
38 #ifdef HAVE_SYS_XATTR_H
39 # include <sys/xattr.h>
40 #endif
41 #include <sys/uio.h>
42
43 #include "regs.h"
44 #include "ptrace.h"
45
46 int
string_to_uint(const char * str)47 string_to_uint(const char *str)
48 {
49 char *error;
50 long value;
51
52 if (!*str)
53 return -1;
54 errno = 0;
55 value = strtol(str, &error, 10);
56 if (errno || *error || value < 0 || (long)(int)value != value)
57 return -1;
58 return (int)value;
59 }
60
61 int
tv_nz(const struct timeval * a)62 tv_nz(const struct timeval *a)
63 {
64 return a->tv_sec || a->tv_usec;
65 }
66
67 int
tv_cmp(const struct timeval * a,const struct timeval * b)68 tv_cmp(const struct timeval *a, const struct timeval *b)
69 {
70 if (a->tv_sec < b->tv_sec
71 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
72 return -1;
73 if (a->tv_sec > b->tv_sec
74 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
75 return 1;
76 return 0;
77 }
78
79 double
tv_float(const struct timeval * tv)80 tv_float(const struct timeval *tv)
81 {
82 return tv->tv_sec + tv->tv_usec/1000000.0;
83 }
84
85 void
tv_add(struct timeval * tv,const struct timeval * a,const struct timeval * b)86 tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b)
87 {
88 tv->tv_sec = a->tv_sec + b->tv_sec;
89 tv->tv_usec = a->tv_usec + b->tv_usec;
90 if (tv->tv_usec >= 1000000) {
91 tv->tv_sec++;
92 tv->tv_usec -= 1000000;
93 }
94 }
95
96 void
tv_sub(struct timeval * tv,const struct timeval * a,const struct timeval * b)97 tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b)
98 {
99 tv->tv_sec = a->tv_sec - b->tv_sec;
100 tv->tv_usec = a->tv_usec - b->tv_usec;
101 if (((long) tv->tv_usec) < 0) {
102 tv->tv_sec--;
103 tv->tv_usec += 1000000;
104 }
105 }
106
107 void
tv_div(struct timeval * tv,const struct timeval * a,int n)108 tv_div(struct timeval *tv, const struct timeval *a, int n)
109 {
110 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
111 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
112 tv->tv_usec %= 1000000;
113 }
114
115 void
tv_mul(struct timeval * tv,const struct timeval * a,int n)116 tv_mul(struct timeval *tv, const struct timeval *a, int n)
117 {
118 tv->tv_usec = a->tv_usec * n;
119 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
120 tv->tv_usec %= 1000000;
121 }
122
123 const char *
xlookup(const struct xlat * xlat,const unsigned int val)124 xlookup(const struct xlat *xlat, const unsigned int val)
125 {
126 for (; xlat->str != NULL; xlat++)
127 if (xlat->val == val)
128 return xlat->str;
129 return NULL;
130 }
131
132 static int
xlat_bsearch_compare(const void * a,const void * b)133 xlat_bsearch_compare(const void *a, const void *b)
134 {
135 const unsigned int val1 = (const unsigned long) a;
136 const unsigned int val2 = ((const struct xlat *) b)->val;
137 return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
138 }
139
140 const char *
xlat_search(const struct xlat * xlat,const size_t nmemb,const unsigned int val)141 xlat_search(const struct xlat *xlat, const size_t nmemb, const unsigned int val)
142 {
143 const struct xlat *e =
144 bsearch((const void*) (const unsigned long) val,
145 xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare);
146
147 return e ? e->str : NULL;
148 }
149
150 #if !defined HAVE_STPCPY
151 char *
stpcpy(char * dst,const char * src)152 stpcpy(char *dst, const char *src)
153 {
154 while ((*dst = *src++) != '\0')
155 dst++;
156 return dst;
157 }
158 #endif
159
160 /* Find a next bit which is set.
161 * Starts testing at cur_bit.
162 * Returns -1 if no more bits are set.
163 *
164 * We never touch bytes we don't need to.
165 * On big-endian, array is assumed to consist of
166 * current_wordsize wide words: for example, is current_wordsize is 4,
167 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
168 * On little-endian machines, word size is immaterial.
169 */
170 int
next_set_bit(const void * bit_array,unsigned cur_bit,unsigned size_bits)171 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
172 {
173 const unsigned endian = 1;
174 int little_endian = *(char*)&endian;
175
176 const uint8_t *array = bit_array;
177 unsigned pos = cur_bit / 8;
178 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
179
180 for (;;) {
181 uint8_t bitmask;
182 uint8_t cur_byte;
183
184 if (cur_bit >= size_bits)
185 return -1;
186 cur_byte = array[pos ^ pos_xor_mask];
187 if (cur_byte == 0) {
188 cur_bit = (cur_bit + 8) & (-8);
189 pos++;
190 continue;
191 }
192 bitmask = 1 << (cur_bit & 7);
193 for (;;) {
194 if (cur_byte & bitmask)
195 return cur_bit;
196 cur_bit++;
197 if (cur_bit >= size_bits)
198 return -1;
199 bitmask <<= 1;
200 /* This check *can't be* optimized out: */
201 if (bitmask == 0)
202 break;
203 }
204 pos++;
205 }
206 }
207 /*
208 * Print entry in struct xlat table, if there.
209 */
210 void
printxvals(const unsigned int val,const char * dflt,const struct xlat * xlat,...)211 printxvals(const unsigned int val, const char *dflt, const struct xlat *xlat, ...)
212 {
213 va_list args;
214
215 va_start(args, xlat);
216 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
217 const char *str = xlookup(xlat, val);
218
219 if (str) {
220 tprints(str);
221 va_end(args);
222 return;
223 }
224 }
225 /* No hits -- print raw # instead. */
226 tprintf("%#x /* %s */", val, dflt);
227
228 va_end(args);
229 }
230
231 /*
232 * Fetch 64bit argument at position arg_no and
233 * return the index of the next argument.
234 */
235 int
getllval(struct tcb * tcp,unsigned long long * val,int arg_no)236 getllval(struct tcb *tcp, unsigned long long *val, int arg_no)
237 {
238 #if SIZEOF_LONG > 4 && SIZEOF_LONG == SIZEOF_LONG_LONG
239 # if SUPPORTED_PERSONALITIES > 1
240 # ifdef X86_64
241 if (current_personality != 1) {
242 # else
243 if (current_wordsize > 4) {
244 # endif
245 # endif
246 *val = tcp->u_arg[arg_no];
247 arg_no++;
248 # if SUPPORTED_PERSONALITIES > 1
249 } else {
250 # if defined(AARCH64) || defined(POWERPC64)
251 /* Align arg_no to the next even number. */
252 arg_no = (arg_no + 1) & 0xe;
253 # endif /* AARCH64 || POWERPC64 */
254 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
255 arg_no += 2;
256 }
257 # endif /* SUPPORTED_PERSONALITIES > 1 */
258 #elif SIZEOF_LONG > 4
259 # error Unsupported configuration: SIZEOF_LONG > 4 && SIZEOF_LONG_LONG > SIZEOF_LONG
260 #elif defined LINUX_MIPSN32
261 *val = tcp->ext_arg[arg_no];
262 arg_no++;
263 #elif defined X32
264 if (current_personality == 0) {
265 *val = tcp->ext_arg[arg_no];
266 arg_no++;
267 } else {
268 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
269 arg_no += 2;
270 }
271 #else
272 # if defined __ARM_EABI__ || \
273 defined LINUX_MIPSO32 || \
274 defined POWERPC || \
275 defined XTENSA
276 /* Align arg_no to the next even number. */
277 arg_no = (arg_no + 1) & 0xe;
278 # endif
279 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
280 arg_no += 2;
281 #endif
282
283 return arg_no;
284 }
285
286 /*
287 * Print 64bit argument at position arg_no and
288 * return the index of the next argument.
289 */
290 int
291 printllval(struct tcb *tcp, const char *format, int arg_no)
292 {
293 unsigned long long val = 0;
294
295 arg_no = getllval(tcp, &val, arg_no);
296 tprintf(format, val);
297 return arg_no;
298 }
299
300 /*
301 * Interpret `xlat' as an array of flags
302 * print the entries whose bits are on in `flags'
303 * return # of flags printed.
304 */
305 void
306 addflags(const struct xlat *xlat, int flags)
307 {
308 for (; xlat->str; xlat++) {
309 if (xlat->val && (flags & xlat->val) == xlat->val) {
310 tprintf("|%s", xlat->str);
311 flags &= ~xlat->val;
312 }
313 }
314 if (flags) {
315 tprintf("|%#x", flags);
316 }
317 }
318
319 /*
320 * Interpret `xlat' as an array of flags.
321 * Print to static string the entries whose bits are on in `flags'
322 * Return static string.
323 */
324 const char *
325 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
326 {
327 static char outstr[1024];
328 char *outptr;
329 int found = 0;
330
331 outptr = stpcpy(outstr, prefix);
332
333 if (flags == 0 && xlat->val == 0 && xlat->str) {
334 strcpy(outptr, xlat->str);
335 return outstr;
336 }
337
338 for (; xlat->str; xlat++) {
339 if (xlat->val && (flags & xlat->val) == xlat->val) {
340 if (found)
341 *outptr++ = '|';
342 outptr = stpcpy(outptr, xlat->str);
343 found = 1;
344 flags &= ~xlat->val;
345 if (!flags)
346 break;
347 }
348 }
349 if (flags) {
350 if (found)
351 *outptr++ = '|';
352 outptr += sprintf(outptr, "%#x", flags);
353 }
354
355 return outstr;
356 }
357
358 int
359 printflags(const struct xlat *xlat, int flags, const char *dflt)
360 {
361 int n;
362 const char *sep;
363
364 if (flags == 0 && xlat->val == 0 && xlat->str) {
365 tprints(xlat->str);
366 return 1;
367 }
368
369 sep = "";
370 for (n = 0; xlat->str; xlat++) {
371 if (xlat->val && (flags & xlat->val) == xlat->val) {
372 tprintf("%s%s", sep, xlat->str);
373 flags &= ~xlat->val;
374 sep = "|";
375 n++;
376 }
377 }
378
379 if (n) {
380 if (flags) {
381 tprintf("%s%#x", sep, flags);
382 n++;
383 }
384 } else {
385 if (flags) {
386 tprintf("%#x", flags);
387 if (dflt)
388 tprintf(" /* %s */", dflt);
389 } else {
390 if (dflt)
391 tprints("0");
392 }
393 }
394
395 return n;
396 }
397
398 void
399 printaddr(const long addr)
400 {
401 if (!addr)
402 tprints("NULL");
403 else
404 tprintf("%#lx", addr);
405 }
406
407 #define DEF_PRINTNUM(name, type) \
408 bool \
409 printnum_ ## name(struct tcb *tcp, const long addr, const char *fmt) \
410 { \
411 type num; \
412 if (umove_or_printaddr(tcp, addr, &num)) \
413 return false; \
414 tprints("["); \
415 tprintf(fmt, num); \
416 tprints("]"); \
417 return true; \
418 }
419
420 #define DEF_PRINTPAIR(name, type) \
421 bool \
422 printpair_ ## name(struct tcb *tcp, const long addr, const char *fmt) \
423 { \
424 type pair[2]; \
425 if (umove_or_printaddr(tcp, addr, &pair)) \
426 return false; \
427 tprints("["); \
428 tprintf(fmt, pair[0]); \
429 tprints(", "); \
430 tprintf(fmt, pair[1]); \
431 tprints("]"); \
432 return true; \
433 }
434
435 DEF_PRINTNUM(int, int)
436 DEF_PRINTPAIR(int, int)
437 DEF_PRINTNUM(short, short)
438 DEF_PRINTNUM(int64, uint64_t)
439 DEF_PRINTPAIR(int64, uint64_t)
440
441 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
442 bool
443 printnum_long_int(struct tcb *tcp, const long addr,
444 const char *fmt_long, const char *fmt_int)
445 {
446 if (current_wordsize > sizeof(int)) {
447 return printnum_int64(tcp, addr, fmt_long);
448 } else {
449 return printnum_int(tcp, addr, fmt_int);
450 }
451 }
452 #endif
453
454 const char *
455 sprinttime(time_t t)
456 {
457 struct tm *tmp;
458 static char buf[sizeof(int) * 3 * 6];
459
460 if (t == 0) {
461 strcpy(buf, "0");
462 return buf;
463 }
464 tmp = localtime(&t);
465 if (tmp)
466 snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d",
467 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
468 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
469 else
470 snprintf(buf, sizeof buf, "%lu", (unsigned long) t);
471
472 return buf;
473 }
474
475 static char *
476 getfdproto(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
477 {
478 #ifdef HAVE_SYS_XATTR_H
479 ssize_t r;
480 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
481
482 if (fd < 0)
483 return NULL;
484
485 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
486 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
487 if (r <= 0)
488 return NULL;
489 else {
490 /*
491 * This is a protection for the case when the kernel
492 * side does not append a null byte to the buffer.
493 */
494 buf[r] = '\0';
495 return buf;
496 }
497 #else
498 return NULL;
499 #endif
500 }
501
502 void
503 printfd(struct tcb *tcp, int fd)
504 {
505 char path[PATH_MAX + 1];
506 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
507 static const char socket_prefix[] = "socket:[";
508 const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
509 const size_t path_len = strlen(path);
510
511 tprintf("%d<", fd);
512 if (show_fd_path > 1 &&
513 strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
514 path[path_len - 1] == ']') {
515 unsigned long inodenr;
516 #define PROTO_NAME_LEN 32
517 char proto_buf[PROTO_NAME_LEN];
518 const char *proto =
519 getfdproto(tcp, fd, proto_buf, PROTO_NAME_LEN);
520 inodenr = strtoul(path + socket_prefix_len, NULL, 10);
521 if (!print_sockaddr_by_inode(inodenr, proto)) {
522 if (proto)
523 tprintf("%s:[%lu]", proto, inodenr);
524 else
525 tprints(path);
526 }
527 } else {
528 print_quoted_string(path, path_len,
529 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
530 }
531 tprints(">");
532 } else
533 tprintf("%d", fd);
534 }
535
536 /*
537 * Quote string `instr' of length `size'
538 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
539 *
540 * If QUOTE_0_TERMINATED `style' flag is set,
541 * treat `instr' as a NUL-terminated string,
542 * checking up to (`size' + 1) bytes of `instr'.
543 *
544 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
545 * do not add leading and trailing quoting symbols.
546 *
547 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
548 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
549 */
550 static int
551 string_quote(const char *instr, char *outstr, const unsigned int size,
552 const unsigned int style)
553 {
554 const unsigned char *ustr = (const unsigned char *) instr;
555 char *s = outstr;
556 unsigned int i;
557 int usehex, c, eol;
558
559 if (style & QUOTE_0_TERMINATED)
560 eol = '\0';
561 else
562 eol = 0x100; /* this can never match a char */
563
564 usehex = 0;
565 if (xflag > 1)
566 usehex = 1;
567 else if (xflag) {
568 /* Check for presence of symbol which require
569 to hex-quote the whole string. */
570 for (i = 0; i < size; ++i) {
571 c = ustr[i];
572 /* Check for NUL-terminated string. */
573 if (c == eol)
574 break;
575
576 /* Force hex unless c is printable or whitespace */
577 if (c > 0x7e) {
578 usehex = 1;
579 break;
580 }
581 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
582 * They happen to have ASCII codes 9,10,11,12,13.
583 */
584 if (c < ' ' && (unsigned)(c - 9) >= 5) {
585 usehex = 1;
586 break;
587 }
588 }
589 }
590
591 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
592 *s++ = '\"';
593
594 if (usehex) {
595 /* Hex-quote the whole string. */
596 for (i = 0; i < size; ++i) {
597 c = ustr[i];
598 /* Check for NUL-terminated string. */
599 if (c == eol)
600 goto asciz_ended;
601 *s++ = '\\';
602 *s++ = 'x';
603 *s++ = "0123456789abcdef"[c >> 4];
604 *s++ = "0123456789abcdef"[c & 0xf];
605 }
606 } else {
607 for (i = 0; i < size; ++i) {
608 c = ustr[i];
609 /* Check for NUL-terminated string. */
610 if (c == eol)
611 goto asciz_ended;
612 switch (c) {
613 case '\"': case '\\':
614 *s++ = '\\';
615 *s++ = c;
616 break;
617 case '\f':
618 *s++ = '\\';
619 *s++ = 'f';
620 break;
621 case '\n':
622 *s++ = '\\';
623 *s++ = 'n';
624 break;
625 case '\r':
626 *s++ = '\\';
627 *s++ = 'r';
628 break;
629 case '\t':
630 *s++ = '\\';
631 *s++ = 't';
632 break;
633 case '\v':
634 *s++ = '\\';
635 *s++ = 'v';
636 break;
637 default:
638 if (c >= ' ' && c <= 0x7e)
639 *s++ = c;
640 else {
641 /* Print \octal */
642 *s++ = '\\';
643 if (i + 1 < size
644 && ustr[i + 1] >= '0'
645 && ustr[i + 1] <= '9'
646 ) {
647 /* Print \ooo */
648 *s++ = '0' + (c >> 6);
649 *s++ = '0' + ((c >> 3) & 0x7);
650 } else {
651 /* Print \[[o]o]o */
652 if ((c >> 3) != 0) {
653 if ((c >> 6) != 0)
654 *s++ = '0' + (c >> 6);
655 *s++ = '0' + ((c >> 3) & 0x7);
656 }
657 }
658 *s++ = '0' + (c & 0x7);
659 }
660 break;
661 }
662 }
663 }
664
665 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
666 *s++ = '\"';
667 *s = '\0';
668
669 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
670 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
671 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
672 * but next char is NUL.
673 */
674 return 0;
675 }
676
677 return 1;
678
679 asciz_ended:
680 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
681 *s++ = '\"';
682 *s = '\0';
683 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
684 return 0;
685 }
686
687 #ifndef ALLOCA_CUTOFF
688 # define ALLOCA_CUTOFF 4032
689 #endif
690 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
691
692 /*
693 * Quote string `str' of length `size' and print the result.
694 *
695 * If QUOTE_0_TERMINATED `style' flag is set,
696 * treat `str' as a NUL-terminated string and
697 * quote at most (`size' - 1) bytes.
698 *
699 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
700 * do not add leading and trailing quoting symbols.
701 *
702 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
703 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
704 */
705 int
706 print_quoted_string(const char *str, unsigned int size,
707 const unsigned int style)
708 {
709 char *buf;
710 char *outstr;
711 unsigned int alloc_size;
712 int rc;
713
714 if (size && style & QUOTE_0_TERMINATED)
715 --size;
716
717 alloc_size = 4 * size;
718 if (alloc_size / 4 != size) {
719 error_msg("Out of memory");
720 tprints("???");
721 return -1;
722 }
723 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
724
725 if (use_alloca(alloc_size)) {
726 outstr = alloca(alloc_size);
727 buf = NULL;
728 } else {
729 outstr = buf = malloc(alloc_size);
730 if (!buf) {
731 error_msg("Out of memory");
732 tprints("???");
733 return -1;
734 }
735 }
736
737 rc = string_quote(str, outstr, size, style);
738 tprints(outstr);
739
740 free(buf);
741 return rc;
742 }
743
744 /*
745 * Print path string specified by address `addr' and length `n'.
746 * If path length exceeds `n', append `...' to the output.
747 */
748 void
749 printpathn(struct tcb *tcp, long addr, unsigned int n)
750 {
751 char path[PATH_MAX + 1];
752 int nul_seen;
753
754 if (!addr) {
755 tprints("NULL");
756 return;
757 }
758
759 /* Cap path length to the path buffer size */
760 if (n > sizeof path - 1)
761 n = sizeof path - 1;
762
763 /* Fetch one byte more to find out whether path length > n. */
764 nul_seen = umovestr(tcp, addr, n + 1, path);
765 if (nul_seen < 0)
766 tprintf("%#lx", addr);
767 else {
768 path[n++] = '\0';
769 print_quoted_string(path, n, QUOTE_0_TERMINATED);
770 if (!nul_seen)
771 tprints("...");
772 }
773 }
774
775 void
776 printpath(struct tcb *tcp, long addr)
777 {
778 /* Size must correspond to char path[] size in printpathn */
779 printpathn(tcp, addr, PATH_MAX);
780 }
781
782 /*
783 * Print string specified by address `addr' and length `len'.
784 * If `len' < 0, treat the string as a NUL-terminated string.
785 * If string length exceeds `max_strlen', append `...' to the output.
786 */
787 void
788 printstr(struct tcb *tcp, long addr, long len)
789 {
790 static char *str = NULL;
791 static char *outstr;
792 unsigned int size;
793 unsigned int style;
794 int ellipsis;
795
796 if (!addr) {
797 tprints("NULL");
798 return;
799 }
800 /* Allocate static buffers if they are not allocated yet. */
801 if (!str) {
802 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
803
804 if (outstr_size / 4 != max_strlen)
805 die_out_of_memory();
806 str = xmalloc(max_strlen + 1);
807 outstr = xmalloc(outstr_size);
808 }
809
810 size = max_strlen;
811 if (len == -1) {
812 /*
813 * Treat as a NUL-terminated string: fetch one byte more
814 * because string_quote may look one byte ahead.
815 */
816 if (umovestr(tcp, addr, size + 1, str) < 0) {
817 tprintf("%#lx", addr);
818 return;
819 }
820 style = QUOTE_0_TERMINATED;
821 }
822 else {
823 if (size > (unsigned long)len)
824 size = (unsigned long)len;
825 if (umoven(tcp, addr, size, str) < 0) {
826 tprintf("%#lx", addr);
827 return;
828 }
829 style = 0;
830 }
831
832 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
833 * or we were requested to print more than -s NUM chars)...
834 */
835 ellipsis = (string_quote(str, outstr, size, style) &&
836 (len < 0 || (unsigned long) len > max_strlen));
837
838 tprints(outstr);
839 if (ellipsis)
840 tprints("...");
841 }
842
843 void
844 dumpiov(struct tcb *tcp, int len, long addr)
845 {
846 #if SUPPORTED_PERSONALITIES > 1
847 union {
848 struct { u_int32_t base; u_int32_t len; } *iov32;
849 struct { u_int64_t base; u_int64_t len; } *iov64;
850 } iovu;
851 #define iov iovu.iov64
852 #define sizeof_iov \
853 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
854 #define iov_iov_base(i) \
855 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
856 #define iov_iov_len(i) \
857 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
858 #else
859 struct iovec *iov;
860 #define sizeof_iov sizeof(*iov)
861 #define iov_iov_base(i) iov[i].iov_base
862 #define iov_iov_len(i) iov[i].iov_len
863 #endif
864 int i;
865 unsigned size;
866
867 size = sizeof_iov * len;
868 /* Assuming no sane program has millions of iovs */
869 if ((unsigned)len > 1024*1024 /* insane or negative size? */
870 || (iov = malloc(size)) == NULL) {
871 error_msg("Out of memory");
872 return;
873 }
874 if (umoven(tcp, addr, size, iov) >= 0) {
875 for (i = 0; i < len; i++) {
876 /* include the buffer number to make it easy to
877 * match up the trace with the source */
878 tprintf(" * %lu bytes in buffer %d\n",
879 (unsigned long)iov_iov_len(i), i);
880 dumpstr(tcp, (long) iov_iov_base(i),
881 iov_iov_len(i));
882 }
883 }
884 free(iov);
885 #undef sizeof_iov
886 #undef iov_iov_base
887 #undef iov_iov_len
888 #undef iov
889 }
890
891 void
892 dumpstr(struct tcb *tcp, long addr, int len)
893 {
894 static int strsize = -1;
895 static unsigned char *str;
896
897 char outbuf[
898 (
899 (sizeof(
900 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
901 "1234567890123456") + /*in case I'm off by few:*/ 4)
902 /*align to 8 to make memset easier:*/ + 7) & -8
903 ];
904 const unsigned char *src;
905 int i;
906
907 memset(outbuf, ' ', sizeof(outbuf));
908
909 if (strsize < len + 16) {
910 free(str);
911 str = malloc(len + 16);
912 if (!str) {
913 strsize = -1;
914 error_msg("Out of memory");
915 return;
916 }
917 strsize = len + 16;
918 }
919
920 if (umoven(tcp, addr, len, str) < 0)
921 return;
922
923 /* Space-pad to 16 bytes */
924 i = len;
925 while (i & 0xf)
926 str[i++] = ' ';
927
928 i = 0;
929 src = str;
930 while (i < len) {
931 char *dst = outbuf;
932 /* Hex dump */
933 do {
934 if (i < len) {
935 *dst++ = "0123456789abcdef"[*src >> 4];
936 *dst++ = "0123456789abcdef"[*src & 0xf];
937 }
938 else {
939 *dst++ = ' ';
940 *dst++ = ' ';
941 }
942 dst++; /* space is there by memset */
943 i++;
944 if ((i & 7) == 0)
945 dst++; /* space is there by memset */
946 src++;
947 } while (i & 0xf);
948 /* ASCII dump */
949 i -= 16;
950 src -= 16;
951 do {
952 if (*src >= ' ' && *src < 0x7f)
953 *dst++ = *src;
954 else
955 *dst++ = '.';
956 src++;
957 } while (++i & 0xf);
958 *dst = '\0';
959 tprintf(" | %05x %s |\n", i - 16, outbuf);
960 }
961 }
962
963 #ifdef HAVE_PROCESS_VM_READV
964 /* C library supports this, but the kernel might not. */
965 static bool process_vm_readv_not_supported = 0;
966 #else
967
968 /* Need to do this since process_vm_readv() is not yet available in libc.
969 * When libc is be updated, only "static bool process_vm_readv_not_supported"
970 * line should remain.
971 */
972 #if !defined(__NR_process_vm_readv)
973 # if defined(I386)
974 # define __NR_process_vm_readv 347
975 # elif defined(X86_64)
976 # define __NR_process_vm_readv 310
977 # elif defined(POWERPC)
978 # define __NR_process_vm_readv 351
979 # endif
980 #endif
981
982 #if defined(__NR_process_vm_readv)
983 static bool process_vm_readv_not_supported = 0;
984 /* Have to avoid duplicating with the C library headers. */
985 static ssize_t strace_process_vm_readv(pid_t pid,
986 const struct iovec *lvec,
987 unsigned long liovcnt,
988 const struct iovec *rvec,
989 unsigned long riovcnt,
990 unsigned long flags)
991 {
992 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
993 }
994 #define process_vm_readv strace_process_vm_readv
995 #else
996 static bool process_vm_readv_not_supported = 1;
997 # define process_vm_readv(...) (errno = ENOSYS, -1)
998 #endif
999
1000 #endif /* end of hack */
1001
1002 static ssize_t
1003 vm_read_mem(pid_t pid, void *laddr, long raddr, size_t len)
1004 {
1005 const struct iovec local = {
1006 .iov_base = laddr,
1007 .iov_len = len
1008 };
1009 const struct iovec remote = {
1010 .iov_base = (void *) raddr,
1011 .iov_len = len
1012 };
1013
1014 return process_vm_readv(pid, &local, 1, &remote, 1, 0);
1015 }
1016
1017 /*
1018 * move `len' bytes of data from process `pid'
1019 * at address `addr' to our space at `our_addr'
1020 */
1021 int
1022 umoven(struct tcb *tcp, long addr, unsigned int len, void *our_addr)
1023 {
1024 char *laddr = our_addr;
1025 int pid = tcp->pid;
1026 unsigned int n, m, nread;
1027 union {
1028 long val;
1029 char x[sizeof(long)];
1030 } u;
1031
1032 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1033 if (current_wordsize < sizeof(addr))
1034 addr &= (1ul << 8 * current_wordsize) - 1;
1035 #endif
1036
1037 if (!process_vm_readv_not_supported) {
1038 int r = vm_read_mem(pid, laddr, addr, len);
1039 if ((unsigned int) r == len)
1040 return 0;
1041 if (r >= 0) {
1042 error_msg("umoven: short read (%u < %u) @0x%lx",
1043 (unsigned int) r, len, addr);
1044 return -1;
1045 }
1046 switch (errno) {
1047 case ENOSYS:
1048 process_vm_readv_not_supported = 1;
1049 break;
1050 case EPERM:
1051 /* operation not permitted, try PTRACE_PEEKDATA */
1052 break;
1053 case ESRCH:
1054 /* the process is gone */
1055 return -1;
1056 case EFAULT: case EIO:
1057 /* address space is inaccessible */
1058 return -1;
1059 default:
1060 /* all the rest is strange and should be reported */
1061 perror_msg("process_vm_readv");
1062 return -1;
1063 }
1064 }
1065
1066 nread = 0;
1067 if (addr & (sizeof(long) - 1)) {
1068 /* addr not a multiple of sizeof(long) */
1069 n = addr & (sizeof(long) - 1); /* residue */
1070 addr &= -sizeof(long); /* aligned address */
1071 errno = 0;
1072 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
1073 switch (errno) {
1074 case 0:
1075 break;
1076 case ESRCH: case EINVAL:
1077 /* these could be seen if the process is gone */
1078 return -1;
1079 case EFAULT: case EIO: case EPERM:
1080 /* address space is inaccessible */
1081 return -1;
1082 default:
1083 /* all the rest is strange and should be reported */
1084 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1085 pid, addr);
1086 return -1;
1087 }
1088 m = MIN(sizeof(long) - n, len);
1089 memcpy(laddr, &u.x[n], m);
1090 addr += sizeof(long);
1091 laddr += m;
1092 nread += m;
1093 len -= m;
1094 }
1095 while (len) {
1096 errno = 0;
1097 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
1098 switch (errno) {
1099 case 0:
1100 break;
1101 case ESRCH: case EINVAL:
1102 /* these could be seen if the process is gone */
1103 return -1;
1104 case EFAULT: case EIO: case EPERM:
1105 /* address space is inaccessible */
1106 if (nread) {
1107 perror_msg("umoven: short read (%u < %u) @0x%lx",
1108 nread, nread + len, addr - nread);
1109 }
1110 return -1;
1111 default:
1112 /* all the rest is strange and should be reported */
1113 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1114 pid, addr);
1115 return -1;
1116 }
1117 m = MIN(sizeof(long), len);
1118 memcpy(laddr, u.x, m);
1119 addr += sizeof(long);
1120 laddr += m;
1121 nread += m;
1122 len -= m;
1123 }
1124
1125 return 0;
1126 }
1127
1128 int
1129 umoven_or_printaddr(struct tcb *tcp, const long addr, const unsigned int len,
1130 void *our_addr)
1131 {
1132 if (!addr) {
1133 tprints("NULL");
1134 return -1;
1135 }
1136 if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1137 umoven(tcp, addr, len, our_addr) < 0) {
1138 tprintf("%#lx", addr);
1139 return -1;
1140 }
1141 return 0;
1142 }
1143
1144 int
1145 umove_ulong_or_printaddr(struct tcb *tcp, const long addr, unsigned long *ptr)
1146 {
1147 if (current_wordsize < sizeof(*ptr)) {
1148 uint32_t val32;
1149 int r = umove_or_printaddr(tcp, addr, &val32);
1150 if (!r)
1151 *ptr = (unsigned long) val32;
1152 return r;
1153 }
1154 return umove_or_printaddr(tcp, addr, ptr);
1155 }
1156
1157 int
1158 umove_ulong_array_or_printaddr(struct tcb *tcp, const long addr,
1159 unsigned long *ptr, size_t n)
1160 {
1161 if (current_wordsize < sizeof(*ptr)) {
1162 uint32_t ptr32[n];
1163 int r = umove_or_printaddr(tcp, addr, &ptr32);
1164 if (!r) {
1165 size_t i;
1166
1167 for (i = 0; i < n; ++i)
1168 ptr[i] = (unsigned long) ptr32[i];
1169 }
1170 return r;
1171 }
1172 return umoven_or_printaddr(tcp, addr, n * sizeof(*ptr), ptr);
1173 }
1174
1175 /*
1176 * Like `umove' but make the additional effort of looking
1177 * for a terminating zero byte.
1178 *
1179 * Returns < 0 on error, > 0 if NUL was seen,
1180 * (TODO if useful: return count of bytes including NUL),
1181 * else 0 if len bytes were read but no NUL byte seen.
1182 *
1183 * Note: there is no guarantee we won't overwrite some bytes
1184 * in laddr[] _after_ terminating NUL (but, of course,
1185 * we never write past laddr[len-1]).
1186 */
1187 int
1188 umovestr(struct tcb *tcp, long addr, unsigned int len, char *laddr)
1189 {
1190 #if SIZEOF_LONG == 4
1191 const unsigned long x01010101 = 0x01010101ul;
1192 const unsigned long x80808080 = 0x80808080ul;
1193 #elif SIZEOF_LONG == 8
1194 const unsigned long x01010101 = 0x0101010101010101ul;
1195 const unsigned long x80808080 = 0x8080808080808080ul;
1196 #else
1197 # error SIZEOF_LONG > 8
1198 #endif
1199
1200 int pid = tcp->pid;
1201 unsigned int n, m, nread;
1202 union {
1203 unsigned long val;
1204 char x[sizeof(long)];
1205 } u;
1206
1207 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1208 if (current_wordsize < sizeof(addr))
1209 addr &= (1ul << 8 * current_wordsize) - 1;
1210 #endif
1211
1212 nread = 0;
1213 if (!process_vm_readv_not_supported) {
1214 const size_t page_size = get_pagesize();
1215 const size_t page_mask = page_size - 1;
1216
1217 while (len > 0) {
1218 unsigned int chunk_len;
1219 unsigned int end_in_page;
1220
1221 /*
1222 * Don't cross pages, otherwise we can get EFAULT
1223 * and fail to notice that terminating NUL lies
1224 * in the existing (first) page.
1225 */
1226 chunk_len = len > page_size ? page_size : len;
1227 end_in_page = (addr + chunk_len) & page_mask;
1228 if (chunk_len > end_in_page) /* crosses to the next page */
1229 chunk_len -= end_in_page;
1230
1231 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1232 if (r > 0) {
1233 if (memchr(laddr, '\0', r))
1234 return 1;
1235 addr += r;
1236 laddr += r;
1237 nread += r;
1238 len -= r;
1239 continue;
1240 }
1241 switch (errno) {
1242 case ENOSYS:
1243 process_vm_readv_not_supported = 1;
1244 goto vm_readv_didnt_work;
1245 case ESRCH:
1246 /* the process is gone */
1247 return -1;
1248 case EPERM:
1249 /* operation not permitted, try PTRACE_PEEKDATA */
1250 if (!nread)
1251 goto vm_readv_didnt_work;
1252 /* fall through */
1253 case EFAULT: case EIO:
1254 /* address space is inaccessible */
1255 if (nread) {
1256 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1257 nread, nread + len, addr - nread);
1258 }
1259 return -1;
1260 default:
1261 /* all the rest is strange and should be reported */
1262 perror_msg("process_vm_readv");
1263 return -1;
1264 }
1265 }
1266 return 0;
1267 }
1268 vm_readv_didnt_work:
1269
1270 if (addr & (sizeof(long) - 1)) {
1271 /* addr not a multiple of sizeof(long) */
1272 n = addr & (sizeof(long) - 1); /* residue */
1273 addr &= -sizeof(long); /* aligned address */
1274 errno = 0;
1275 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1276 switch (errno) {
1277 case 0:
1278 break;
1279 case ESRCH: case EINVAL:
1280 /* these could be seen if the process is gone */
1281 return -1;
1282 case EFAULT: case EIO: case EPERM:
1283 /* address space is inaccessible */
1284 return -1;
1285 default:
1286 /* all the rest is strange and should be reported */
1287 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1288 pid, addr);
1289 return -1;
1290 }
1291 m = MIN(sizeof(long) - n, len);
1292 memcpy(laddr, &u.x[n], m);
1293 while (n & (sizeof(long) - 1))
1294 if (u.x[n++] == '\0')
1295 return 1;
1296 addr += sizeof(long);
1297 laddr += m;
1298 nread += m;
1299 len -= m;
1300 }
1301
1302 while (len) {
1303 errno = 0;
1304 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1305 switch (errno) {
1306 case 0:
1307 break;
1308 case ESRCH: case EINVAL:
1309 /* these could be seen if the process is gone */
1310 return -1;
1311 case EFAULT: case EIO: case EPERM:
1312 /* address space is inaccessible */
1313 if (nread) {
1314 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1315 nread, nread + len, addr - nread);
1316 }
1317 return -1;
1318 default:
1319 /* all the rest is strange and should be reported */
1320 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1321 pid, addr);
1322 return -1;
1323 }
1324 m = MIN(sizeof(long), len);
1325 memcpy(laddr, u.x, m);
1326 /* "If a NUL char exists in this word" */
1327 if ((u.val - x01010101) & ~u.val & x80808080)
1328 return 1;
1329 addr += sizeof(long);
1330 laddr += m;
1331 nread += m;
1332 len -= m;
1333 }
1334 return 0;
1335 }
1336
1337 int
1338 upeek(int pid, long off, long *res)
1339 {
1340 long val;
1341
1342 errno = 0;
1343 val = ptrace(PTRACE_PEEKUSER, (pid_t)pid, (char *) off, 0);
1344 if (val == -1 && errno) {
1345 if (errno != ESRCH) {
1346 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", pid, off);
1347 }
1348 return -1;
1349 }
1350 *res = val;
1351 return 0;
1352 }
1353