1 //===-- sanitizer_printf.cc -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer.
12 // Internal printf function, used inside run-time libraries.
13 // We can't use libc printf because we intercept some of the functions used
15 //===----------------------------------------------------------------------===//
18 #include "sanitizer_common.h"
19 #include "sanitizer_libc.h"
24 namespace __sanitizer
{
26 static int AppendChar(char **buff
, const char *buff_end
, char c
) {
27 if (*buff
< buff_end
) {
34 // Appends number in a given base to buffer. If its length is less than
35 // "minimal_num_length", it is padded with leading zeroes.
36 static int AppendUnsigned(char **buff
, const char *buff_end
, u64 num
,
37 u8 base
, u8 minimal_num_length
) {
38 uptr
const kMaxLen
= 30;
39 RAW_CHECK(base
== 10 || base
== 16);
40 RAW_CHECK(minimal_num_length
< kMaxLen
);
41 uptr num_buffer
[kMaxLen
];
44 RAW_CHECK_MSG(pos
< kMaxLen
, "appendNumber buffer overflow");
45 num_buffer
[pos
++] = num
% base
;
48 if (pos
< minimal_num_length
) {
49 // Make sure compiler doesn't insert call to memset here.
50 internal_memset(&num_buffer
[pos
], 0,
51 sizeof(num_buffer
[0]) * (minimal_num_length
- pos
));
52 pos
= minimal_num_length
;
56 uptr digit
= num_buffer
[pos
];
57 result
+= AppendChar(buff
, buff_end
, (digit
< 10) ? '0' + digit
63 static int AppendSignedDecimal(char **buff
, const char *buff_end
, s64 num
,
64 u8 minimal_num_length
) {
67 result
+= AppendChar(buff
, buff_end
, '-');
69 if (minimal_num_length
)
72 result
+= AppendUnsigned(buff
, buff_end
, (u64
)num
, 10, minimal_num_length
);
76 static int AppendString(char **buff
, const char *buff_end
, const char *s
) {
81 result
+= AppendChar(buff
, buff_end
, *s
);
86 static int AppendPointer(char **buff
, const char *buff_end
, u64 ptr_value
) {
88 result
+= AppendString(buff
, buff_end
, "0x");
89 result
+= AppendUnsigned(buff
, buff_end
, ptr_value
, 16,
90 (SANITIZER_WORDSIZE
== 64) ? 12 : 8);
94 int VSNPrintf(char *buff
, int buff_length
,
95 const char *format
, va_list args
) {
96 static const char *kPrintfFormatsHelp
=
97 "Supported Printf formats: %(0[0-9]*)?(z|ll)?{d,u,x}; %p; %s; %c\n";
99 RAW_CHECK(buff_length
> 0);
100 const char *buff_end
= &buff
[buff_length
- 1];
101 const char *cur
= format
;
103 for (; *cur
; cur
++) {
105 result
+= AppendChar(&buff
, buff_end
, *cur
);
109 bool have_width
= (*cur
== '0');
112 while (*cur
>= '0' && *cur
<= '9') {
114 width
= width
* 10 + *cur
++ - '0';
117 bool have_z
= (*cur
== 'z');
119 bool have_ll
= !have_z
&& (cur
[0] == 'l' && cur
[1] == 'l');
123 bool have_flags
= have_width
| have_z
| have_ll
;
126 dval
= have_ll
? va_arg(args
, s64
)
127 : have_z
? va_arg(args
, sptr
)
129 result
+= AppendSignedDecimal(&buff
, buff_end
, dval
, width
);
134 uval
= have_ll
? va_arg(args
, u64
)
135 : have_z
? va_arg(args
, uptr
)
136 : va_arg(args
, unsigned);
137 result
+= AppendUnsigned(&buff
, buff_end
, uval
,
138 (*cur
== 'u') ? 10 : 16, width
);
142 RAW_CHECK_MSG(!have_flags
, kPrintfFormatsHelp
);
143 result
+= AppendPointer(&buff
, buff_end
, va_arg(args
, uptr
));
147 RAW_CHECK_MSG(!have_flags
, kPrintfFormatsHelp
);
148 result
+= AppendString(&buff
, buff_end
, va_arg(args
, char*));
152 RAW_CHECK_MSG(!have_flags
, kPrintfFormatsHelp
);
153 result
+= AppendChar(&buff
, buff_end
, va_arg(args
, int));
157 RAW_CHECK_MSG(!have_flags
, kPrintfFormatsHelp
);
158 result
+= AppendChar(&buff
, buff_end
, '%');
162 RAW_CHECK_MSG(false, kPrintfFormatsHelp
);
166 RAW_CHECK(buff
<= buff_end
);
167 AppendChar(&buff
, buff_end
+ 1, '\0');
171 static void (*PrintfAndReportCallback
)(const char *);
172 void SetPrintfAndReportCallback(void (*callback
)(const char *)) {
173 PrintfAndReportCallback
= callback
;
176 void Printf(const char *format
, ...) {
177 const int kLen
= 16 * 1024;
178 InternalScopedBuffer
<char> buffer(kLen
);
180 va_start(args
, format
);
181 int needed_length
= VSNPrintf(buffer
.data(), kLen
, format
, args
);
183 RAW_CHECK_MSG(needed_length
< kLen
, "Buffer in Printf is too short!\n");
184 RawWrite(buffer
.data());
185 if (PrintfAndReportCallback
)
186 PrintfAndReportCallback(buffer
.data());
189 // Writes at most "length" symbols to "buffer" (including trailing '\0').
190 // Returns the number of symbols that should have been written to buffer
191 // (not including trailing '\0'). Thus, the string is truncated
192 // iff return value is not less than "length".
193 int internal_snprintf(char *buffer
, uptr length
, const char *format
, ...) {
195 va_start(args
, format
);
196 int needed_length
= VSNPrintf(buffer
, length
, format
, args
);
198 return needed_length
;
201 // Like Printf, but prints the current PID before the output string.
202 void Report(const char *format
, ...) {
203 const int kLen
= 16 * 1024;
204 InternalScopedBuffer
<char> buffer(kLen
);
205 int needed_length
= internal_snprintf(buffer
.data(),
206 kLen
, "==%d== ", GetPid());
207 RAW_CHECK_MSG(needed_length
< kLen
, "Buffer in Report is too short!\n");
209 va_start(args
, format
);
210 needed_length
+= VSNPrintf(buffer
.data() + needed_length
,
211 kLen
- needed_length
, format
, args
);
213 RAW_CHECK_MSG(needed_length
< kLen
, "Buffer in Report is too short!\n");
214 RawWrite(buffer
.data());
215 if (PrintfAndReportCallback
)
216 PrintfAndReportCallback(buffer
.data());
219 } // namespace __sanitizer