1 //===-- sanitizer_printf.cc -----------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer.
10 // Internal printf function, used inside run-time libraries.
11 // We can't use libc printf because we intercept some of the functions used
13 //===----------------------------------------------------------------------===//
16 #include "sanitizer_common.h"
17 #include "sanitizer_libc.h"
22 namespace __sanitizer {
24 static int AppendChar(char **buff, const char *buff_end, char c) {
25 if (*buff < buff_end) {
32 // Appends number in a given base to buffer. If its length is less than
33 // "minimal_num_length", it is padded with leading zeroes.
34 static int AppendUnsigned(char **buff, const char *buff_end, u64 num,
35 u8 base, u8 minimal_num_length) {
36 uptr const kMaxLen = 30;
37 RAW_CHECK(base == 10 || base == 16);
38 RAW_CHECK(minimal_num_length < kMaxLen);
39 uptr num_buffer[kMaxLen];
42 RAW_CHECK_MSG(pos < kMaxLen, "appendNumber buffer overflow");
43 num_buffer[pos++] = num % base;
46 if (pos < minimal_num_length) {
47 // Make sure compiler doesn't insert call to memset here.
48 internal_memset(&num_buffer[pos], 0,
49 sizeof(num_buffer[0]) * (minimal_num_length - pos));
50 pos = minimal_num_length;
54 uptr digit = num_buffer[pos];
55 result += AppendChar(buff, buff_end, (digit < 10) ? '0' + digit
61 static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num,
62 u8 minimal_num_length) {
65 result += AppendChar(buff, buff_end, '-');
67 if (minimal_num_length)
70 result += AppendUnsigned(buff, buff_end, (u64)num, 10, minimal_num_length);
74 static int AppendString(char **buff, const char *buff_end, const char *s) {
79 result += AppendChar(buff, buff_end, *s);
84 static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) {
86 result += AppendString(buff, buff_end, "0x");
87 result += AppendUnsigned(buff, buff_end, ptr_value, 16,
88 (SANITIZER_WORDSIZE == 64) ? 12 : 8);
92 int VSNPrintf(char *buff, int buff_length,
93 const char *format, va_list args) {
94 static const char *kPrintfFormatsHelp =
95 "Supported Printf formats: %(0[0-9]*)?(z|ll)?{d,u,x}; %p; %s; %c\n";
97 RAW_CHECK(buff_length > 0);
98 const char *buff_end = &buff[buff_length - 1];
99 const char *cur = format;
101 for (; *cur; cur++) {
103 result += AppendChar(&buff, buff_end, *cur);
107 bool have_width = (*cur == '0');
110 while (*cur >= '0' && *cur <= '9') {
112 width = width * 10 + *cur++ - '0';
115 bool have_z = (*cur == 'z');
117 bool have_ll = !have_z && (cur[0] == 'l' && cur[1] == 'l');
121 bool have_flags = have_width | have_z | have_ll;
124 dval = have_ll ? va_arg(args, s64)
125 : have_z ? va_arg(args, sptr)
127 result += AppendSignedDecimal(&buff, buff_end, dval, width);
132 uval = have_ll ? va_arg(args, u64)
133 : have_z ? va_arg(args, uptr)
134 : va_arg(args, unsigned);
135 result += AppendUnsigned(&buff, buff_end, uval,
136 (*cur == 'u') ? 10 : 16, width);
140 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
141 result += AppendPointer(&buff, buff_end, va_arg(args, uptr));
145 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
146 result += AppendString(&buff, buff_end, va_arg(args, char*));
150 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
151 result += AppendChar(&buff, buff_end, va_arg(args, int));
155 RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
156 result += AppendChar(&buff, buff_end, '%');
160 RAW_CHECK_MSG(false, kPrintfFormatsHelp);
164 RAW_CHECK(buff <= buff_end);
165 AppendChar(&buff, buff_end + 1, '\0');
169 static void (*PrintfAndReportCallback)(const char *);
170 void SetPrintfAndReportCallback(void (*callback)(const char *)) {
171 PrintfAndReportCallback = callback;
174 void Printf(const char *format, ...) {
175 const int kLen = 16 * 1024;
176 InternalScopedBuffer<char> buffer(kLen);
178 va_start(args, format);
179 int needed_length = VSNPrintf(buffer.data(), kLen, format, args);
181 RAW_CHECK_MSG(needed_length < kLen, "Buffer in Printf is too short!\n");
182 RawWrite(buffer.data());
183 if (PrintfAndReportCallback)
184 PrintfAndReportCallback(buffer.data());
187 // Writes at most "length" symbols to "buffer" (including trailing '\0').
188 // Returns the number of symbols that should have been written to buffer
189 // (not including trailing '\0'). Thus, the string is truncated
190 // iff return value is not less than "length".
191 int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
193 va_start(args, format);
194 int needed_length = VSNPrintf(buffer, length, format, args);
196 return needed_length;
199 // Like Printf, but prints the current PID before the output string.
200 void Report(const char *format, ...) {
201 const int kLen = 16 * 1024;
202 InternalScopedBuffer<char> buffer(kLen);
203 int needed_length = internal_snprintf(buffer.data(),
204 kLen, "==%d== ", GetPid());
205 RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n");
207 va_start(args, format);
208 needed_length += VSNPrintf(buffer.data() + needed_length,
209 kLen - needed_length, format, args);
211 RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n");
212 RawWrite(buffer.data());
213 if (PrintfAndReportCallback)
214 PrintfAndReportCallback(buffer.data());
217 } // namespace __sanitizer