annotate projects/compiler-rt/lib/sanitizer_common/sanitizer_printf.cc @ 214:0cf2d4ade63d

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 13 Jul 2021 09:53:52 +0900
parents f476a9ba4795
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
131
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
1 //===-- sanitizer_printf.cc -----------------------------------------------===//
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
2 //
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
4 //
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
7 //
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
9 //
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
10 // This file is shared between AddressSanitizer and ThreadSanitizer.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
11 //
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
12 // Internal printf function, used inside run-time libraries.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
13 // We can't use libc printf because we intercept some of the functions used
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
14 // inside it.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
15 //===----------------------------------------------------------------------===//
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
16
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
17 #include "sanitizer_common.h"
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
18 #include "sanitizer_flags.h"
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
19 #include "sanitizer_libc.h"
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
20
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
21 #include <stdio.h>
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
22 #include <stdarg.h>
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
23
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
24 #if SANITIZER_WINDOWS && defined(_MSC_VER) && _MSC_VER < 1800 && \
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
25 !defined(va_copy)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
26 # define va_copy(dst, src) ((dst) = (src))
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
27 #endif
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
28
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
29 namespace __sanitizer {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
30
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
31 static int AppendChar(char **buff, const char *buff_end, char c) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
32 if (*buff < buff_end) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
33 **buff = c;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
34 (*buff)++;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
35 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
36 return 1;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
37 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
38
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
39 // Appends number in a given base to buffer. If its length is less than
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
40 // |minimal_num_length|, it is padded with leading zeroes or spaces, depending
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
41 // on the value of |pad_with_zero|.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
42 static int AppendNumber(char **buff, const char *buff_end, u64 absolute_value,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
43 u8 base, u8 minimal_num_length, bool pad_with_zero,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
44 bool negative, bool uppercase) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
45 uptr const kMaxLen = 30;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
46 RAW_CHECK(base == 10 || base == 16);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
47 RAW_CHECK(base == 10 || !negative);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
48 RAW_CHECK(absolute_value || !negative);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
49 RAW_CHECK(minimal_num_length < kMaxLen);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
50 int result = 0;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
51 if (negative && minimal_num_length)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
52 --minimal_num_length;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
53 if (negative && pad_with_zero)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
54 result += AppendChar(buff, buff_end, '-');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
55 uptr num_buffer[kMaxLen];
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
56 int pos = 0;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
57 do {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
58 RAW_CHECK_MSG((uptr)pos < kMaxLen, "AppendNumber buffer overflow");
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
59 num_buffer[pos++] = absolute_value % base;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
60 absolute_value /= base;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
61 } while (absolute_value > 0);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
62 if (pos < minimal_num_length) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
63 // Make sure compiler doesn't insert call to memset here.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
64 internal_memset(&num_buffer[pos], 0,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
65 sizeof(num_buffer[0]) * (minimal_num_length - pos));
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
66 pos = minimal_num_length;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
67 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
68 RAW_CHECK(pos > 0);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
69 pos--;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
70 for (; pos >= 0 && num_buffer[pos] == 0; pos--) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
71 char c = (pad_with_zero || pos == 0) ? '0' : ' ';
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
72 result += AppendChar(buff, buff_end, c);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
73 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
74 if (negative && !pad_with_zero) result += AppendChar(buff, buff_end, '-');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
75 for (; pos >= 0; pos--) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
76 char digit = static_cast<char>(num_buffer[pos]);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
77 digit = (digit < 10) ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
78 result += AppendChar(buff, buff_end, digit);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
79 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
80 return result;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
81 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
82
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
83 static int AppendUnsigned(char **buff, const char *buff_end, u64 num, u8 base,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
84 u8 minimal_num_length, bool pad_with_zero,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
85 bool uppercase) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
86 return AppendNumber(buff, buff_end, num, base, minimal_num_length,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
87 pad_with_zero, false /* negative */, uppercase);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
88 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
89
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
90 static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
91 u8 minimal_num_length, bool pad_with_zero) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
92 bool negative = (num < 0);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
93 return AppendNumber(buff, buff_end, (u64)(negative ? -num : num), 10,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
94 minimal_num_length, pad_with_zero, negative,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
95 false /* uppercase */);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
96 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
97
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
98 static int AppendString(char **buff, const char *buff_end, int precision,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
99 const char *s) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
100 if (!s)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
101 s = "<null>";
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
102 int result = 0;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
103 for (; *s; s++) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
104 if (precision >= 0 && result >= precision)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
105 break;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
106 result += AppendChar(buff, buff_end, *s);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
107 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
108 return result;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
109 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
110
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
111 static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
112 int result = 0;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
113 result += AppendString(buff, buff_end, -1, "0x");
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
114 result += AppendUnsigned(buff, buff_end, ptr_value, 16,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
115 SANITIZER_POINTER_FORMAT_LENGTH,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
116 true /* pad_with_zero */, false /* uppercase */);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
117 return result;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
118 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
119
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
120 int VSNPrintf(char *buff, int buff_length,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
121 const char *format, va_list args) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
122 static const char *kPrintfFormatsHelp =
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
123 "Supported Printf formats: %([0-9]*)?(z|ll)?{d,u,x,X}; %p; %(\\.\\*)?s; "
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
124 "%c\n";
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
125 RAW_CHECK(format);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
126 RAW_CHECK(buff_length > 0);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
127 const char *buff_end = &buff[buff_length - 1];
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
128 const char *cur = format;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
129 int result = 0;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
130 for (; *cur; cur++) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
131 if (*cur != '%') {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
132 result += AppendChar(&buff, buff_end, *cur);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
133 continue;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
134 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
135 cur++;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
136 bool have_width = (*cur >= '0' && *cur <= '9');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
137 bool pad_with_zero = (*cur == '0');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
138 int width = 0;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
139 if (have_width) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
140 while (*cur >= '0' && *cur <= '9') {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
141 width = width * 10 + *cur++ - '0';
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
142 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
143 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
144 bool have_precision = (cur[0] == '.' && cur[1] == '*');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
145 int precision = -1;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
146 if (have_precision) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
147 cur += 2;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
148 precision = va_arg(args, int);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
149 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
150 bool have_z = (*cur == 'z');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
151 cur += have_z;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
152 bool have_ll = !have_z && (cur[0] == 'l' && cur[1] == 'l');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
153 cur += have_ll * 2;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
154 s64 dval;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
155 u64 uval;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
156 bool have_flags = have_width | have_z | have_ll;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
157 // Only %s supports precision for now
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
158 CHECK(!(precision >= 0 && *cur != 's'));
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
159 switch (*cur) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
160 case 'd': {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
161 dval = have_ll ? va_arg(args, s64)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
162 : have_z ? va_arg(args, sptr)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
163 : va_arg(args, int);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
164 result += AppendSignedDecimal(&buff, buff_end, dval, width,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
165 pad_with_zero);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
166 break;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
167 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
168 case 'u':
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
169 case 'x':
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
170 case 'X': {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
171 uval = have_ll ? va_arg(args, u64)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
172 : have_z ? va_arg(args, uptr)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
173 : va_arg(args, unsigned);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
174 bool uppercase = (*cur == 'X');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
175 result += AppendUnsigned(&buff, buff_end, uval, (*cur == 'u') ? 10 : 16,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
176 width, pad_with_zero, uppercase);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
177 break;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
178 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
179 case 'p': {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
180 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
181 result += AppendPointer(&buff, buff_end, va_arg(args, uptr));
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
182 break;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
183 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
184 case 's': {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
185 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
186 result += AppendString(&buff, buff_end, precision, va_arg(args, char*));
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
187 break;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
188 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
189 case 'c': {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
190 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
191 result += AppendChar(&buff, buff_end, va_arg(args, int));
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
192 break;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
193 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
194 case '%' : {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
195 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
196 result += AppendChar(&buff, buff_end, '%');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
197 break;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
198 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
199 default: {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
200 RAW_CHECK_MSG(false, kPrintfFormatsHelp);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
201 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
202 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
203 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
204 RAW_CHECK(buff <= buff_end);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
205 AppendChar(&buff, buff_end + 1, '\0');
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
206 return result;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
207 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
208
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
209 static void (*PrintfAndReportCallback)(const char *);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
210 void SetPrintfAndReportCallback(void (*callback)(const char *)) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
211 PrintfAndReportCallback = callback;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
212 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
213
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
214 // Can be overriden in frontend.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
215 #if SANITIZER_GO && defined(TSAN_EXTERNAL_HOOKS)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
216 // Implementation must be defined in frontend.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
217 extern "C" void OnPrint(const char *str);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
218 #else
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
219 SANITIZER_INTERFACE_WEAK_DEF(void, OnPrint, const char *str) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
220 (void)str;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
221 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
222 #endif
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
223
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
224 static void CallPrintfAndReportCallback(const char *str) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
225 OnPrint(str);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
226 if (PrintfAndReportCallback)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
227 PrintfAndReportCallback(str);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
228 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
229
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
230 static void NOINLINE SharedPrintfCodeNoBuffer(bool append_pid,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
231 char *local_buffer,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
232 int buffer_size,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
233 const char *format,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
234 va_list args) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
235 va_list args2;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
236 va_copy(args2, args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
237 const int kLen = 16 * 1024;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
238 int needed_length;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
239 char *buffer = local_buffer;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
240 // First try to print a message using a local buffer, and then fall back to
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
241 // mmaped buffer.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
242 for (int use_mmap = 0; use_mmap < 2; use_mmap++) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
243 if (use_mmap) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
244 va_end(args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
245 va_copy(args, args2);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
246 buffer = (char*)MmapOrDie(kLen, "Report");
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
247 buffer_size = kLen;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
248 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
249 needed_length = 0;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
250 // Check that data fits into the current buffer.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
251 # define CHECK_NEEDED_LENGTH \
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
252 if (needed_length >= buffer_size) { \
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
253 if (!use_mmap) continue; \
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
254 RAW_CHECK_MSG(needed_length < kLen, \
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
255 "Buffer in Report is too short!\n"); \
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
256 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
257 // Fuchsia's logging infrastructure always keeps track of the logging
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
258 // process, thread, and timestamp, so never prepend such information.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
259 if (!SANITIZER_FUCHSIA && append_pid) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
260 int pid = internal_getpid();
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
261 const char *exe_name = GetProcessName();
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
262 if (common_flags()->log_exe_name && exe_name) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
263 needed_length += internal_snprintf(buffer, buffer_size,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
264 "==%s", exe_name);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
265 CHECK_NEEDED_LENGTH
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
266 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
267 needed_length += internal_snprintf(
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
268 buffer + needed_length, buffer_size - needed_length, "==%d==", pid);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
269 CHECK_NEEDED_LENGTH
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
270 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
271 needed_length += VSNPrintf(buffer + needed_length,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
272 buffer_size - needed_length, format, args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
273 CHECK_NEEDED_LENGTH
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
274 // If the message fit into the buffer, print it and exit.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
275 break;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
276 # undef CHECK_NEEDED_LENGTH
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
277 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
278 RawWrite(buffer);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
279
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
280 // Remove color sequences from the message.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
281 RemoveANSIEscapeSequencesFromString(buffer);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
282 CallPrintfAndReportCallback(buffer);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
283 LogMessageOnPrintf(buffer);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
284
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
285 // If we had mapped any memory, clean up.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
286 if (buffer != local_buffer)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
287 UnmapOrDie((void *)buffer, buffer_size);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
288 va_end(args2);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
289 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
290
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
291 static void NOINLINE SharedPrintfCode(bool append_pid, const char *format,
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
292 va_list args) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
293 // |local_buffer| is small enough not to overflow the stack and/or violate
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
294 // the stack limit enforced by TSan (-Wframe-larger-than=512). On the other
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
295 // hand, the bigger the buffer is, the more the chance the error report will
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
296 // fit into it.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
297 char local_buffer[400];
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
298 SharedPrintfCodeNoBuffer(append_pid, local_buffer, ARRAY_SIZE(local_buffer),
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
299 format, args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
300 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
301
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
302 FORMAT(1, 2)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
303 void Printf(const char *format, ...) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
304 va_list args;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
305 va_start(args, format);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
306 SharedPrintfCode(false, format, args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
307 va_end(args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
308 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
309
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
310 // Like Printf, but prints the current PID before the output string.
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
311 FORMAT(1, 2)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
312 void Report(const char *format, ...) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
313 va_list args;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
314 va_start(args, format);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
315 SharedPrintfCode(true, format, args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
316 va_end(args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
317 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
318
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
319 // Writes at most "length" symbols to "buffer" (including trailing '\0').
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
320 // Returns the number of symbols that should have been written to buffer
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
321 // (not including trailing '\0'). Thus, the string is truncated
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
322 // iff return value is not less than "length".
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
323 FORMAT(3, 4)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
324 int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
325 va_list args;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
326 va_start(args, format);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
327 int needed_length = VSNPrintf(buffer, length, format, args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
328 va_end(args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
329 return needed_length;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
330 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
331
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
332 FORMAT(2, 3)
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
333 void InternalScopedString::append(const char *format, ...) {
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
334 CHECK_LT(length_, size());
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
335 va_list args;
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
336 va_start(args, format);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
337 VSNPrintf(data() + length_, size() - length_, format, args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
338 va_end(args);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
339 length_ += internal_strlen(data() + length_);
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
340 CHECK_LT(length_, size());
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
341 }
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
342
f476a9ba4795 http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
mir3636
parents:
diff changeset
343 } // namespace __sanitizer