Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 1 | //===-- sanitizer_printf.cc -----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is shared between AddressSanitizer and ThreadSanitizer. |
| 11 | // |
| 12 | // Internal printf function, used inside run-time libraries. |
| 13 | // We can't use libc printf because we intercept some of the functions used |
| 14 | // inside it. |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | |
| 18 | #include "sanitizer_common.h" |
| 19 | #include "sanitizer_libc.h" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdarg.h> |
| 23 | |
| 24 | namespace __sanitizer { |
| 25 | |
| 26 | static int AppendChar(char **buff, const char *buff_end, char c) { |
| 27 | if (*buff < buff_end) { |
| 28 | **buff = c; |
| 29 | (*buff)++; |
| 30 | } |
| 31 | return 1; |
| 32 | } |
| 33 | |
| 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]; |
| 42 | uptr pos = 0; |
| 43 | do { |
| 44 | RAW_CHECK_MSG(pos < kMaxLen, "appendNumber buffer overflow"); |
| 45 | num_buffer[pos++] = num % base; |
| 46 | num /= base; |
| 47 | } while (num > 0); |
| 48 | while (pos < minimal_num_length) num_buffer[pos++] = 0; |
| 49 | int result = 0; |
| 50 | while (pos-- > 0) { |
| 51 | uptr digit = num_buffer[pos]; |
| 52 | result += AppendChar(buff, buff_end, (digit < 10) ? '0' + digit |
| 53 | : 'a' + digit - 10); |
| 54 | } |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num) { |
| 59 | int result = 0; |
| 60 | if (num < 0) { |
| 61 | result += AppendChar(buff, buff_end, '-'); |
| 62 | num = -num; |
| 63 | } |
| 64 | result += AppendUnsigned(buff, buff_end, (u64)num, 10, 0); |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | static int AppendString(char **buff, const char *buff_end, const char *s) { |
| 69 | if (s == 0) |
| 70 | s = "<null>"; |
| 71 | int result = 0; |
| 72 | for (; *s; s++) { |
| 73 | result += AppendChar(buff, buff_end, *s); |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) { |
| 79 | int result = 0; |
| 80 | result += AppendString(buff, buff_end, "0x"); |
| 81 | result += AppendUnsigned(buff, buff_end, ptr_value, 16, |
| 82 | (__WORDSIZE == 64) ? 12 : 8); |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | int VSNPrintf(char *buff, int buff_length, |
| 87 | const char *format, va_list args) { |
| 88 | static const char *kPrintfFormatsHelp = "Supported Printf formats: " |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 89 | "%%[z]{d,u,x}; %%p; %%s; %%c\n"; |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 90 | RAW_CHECK(format); |
| 91 | RAW_CHECK(buff_length > 0); |
| 92 | const char *buff_end = &buff[buff_length - 1]; |
| 93 | const char *cur = format; |
| 94 | int result = 0; |
| 95 | for (; *cur; cur++) { |
| 96 | if (*cur != '%') { |
| 97 | result += AppendChar(&buff, buff_end, *cur); |
| 98 | continue; |
| 99 | } |
| 100 | cur++; |
| 101 | bool have_z = (*cur == 'z'); |
| 102 | cur += have_z; |
| 103 | s64 dval; |
| 104 | u64 uval; |
| 105 | switch (*cur) { |
| 106 | case 'd': { |
| 107 | dval = have_z ? va_arg(args, sptr) |
| 108 | : va_arg(args, int); |
| 109 | result += AppendSignedDecimal(&buff, buff_end, dval); |
| 110 | break; |
| 111 | } |
| 112 | case 'u': |
| 113 | case 'x': { |
| 114 | uval = have_z ? va_arg(args, uptr) |
| 115 | : va_arg(args, unsigned); |
| 116 | result += AppendUnsigned(&buff, buff_end, uval, |
| 117 | (*cur == 'u') ? 10 : 16, 0); |
| 118 | break; |
| 119 | } |
| 120 | case 'p': { |
| 121 | RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp); |
| 122 | result += AppendPointer(&buff, buff_end, va_arg(args, uptr)); |
| 123 | break; |
| 124 | } |
| 125 | case 's': { |
| 126 | RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp); |
| 127 | result += AppendString(&buff, buff_end, va_arg(args, char*)); |
| 128 | break; |
| 129 | } |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 130 | case 'c': { |
| 131 | RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp); |
Kostya Serebryany | 01167e8 | 2012-09-14 06:52:46 +0000 | [diff] [blame] | 132 | result += AppendChar(&buff, buff_end, va_arg(args, int)); |
Kostya Serebryany | 81dfbb7 | 2012-09-14 04:35:14 +0000 | [diff] [blame] | 133 | break; |
| 134 | } |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 135 | case '%' : { |
| 136 | RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp); |
| 137 | result += AppendChar(&buff, buff_end, '%'); |
| 138 | break; |
| 139 | } |
| 140 | default: { |
| 141 | RAW_CHECK_MSG(false, kPrintfFormatsHelp); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | RAW_CHECK(buff <= buff_end); |
| 146 | AppendChar(&buff, buff_end + 1, '\0'); |
| 147 | return result; |
| 148 | } |
| 149 | |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 150 | static void (*PrintfAndReportCallback)(const char *); |
| 151 | void SetPrintfAndReportCallback(void (*callback)(const char *)) { |
| 152 | PrintfAndReportCallback = callback; |
| 153 | } |
| 154 | |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 155 | void Printf(const char *format, ...) { |
| 156 | const int kLen = 1024 * 4; |
Kostya Serebryany | 4fa111c | 2012-08-29 08:40:36 +0000 | [diff] [blame] | 157 | InternalScopedBuffer<char> buffer(kLen); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 158 | va_list args; |
| 159 | va_start(args, format); |
Kostya Serebryany | 4fa111c | 2012-08-29 08:40:36 +0000 | [diff] [blame] | 160 | int needed_length = VSNPrintf(buffer.data(), kLen, format, args); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 161 | va_end(args); |
| 162 | RAW_CHECK_MSG(needed_length < kLen, "Buffer in Printf is too short!\n"); |
Kostya Serebryany | 4fa111c | 2012-08-29 08:40:36 +0000 | [diff] [blame] | 163 | RawWrite(buffer.data()); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 164 | if (PrintfAndReportCallback) |
Kostya Serebryany | 4fa111c | 2012-08-29 08:40:36 +0000 | [diff] [blame] | 165 | PrintfAndReportCallback(buffer.data()); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Writes at most "length" symbols to "buffer" (including trailing '\0'). |
| 169 | // Returns the number of symbols that should have been written to buffer |
| 170 | // (not including trailing '\0'). Thus, the string is truncated |
| 171 | // iff return value is not less than "length". |
Alexey Samsonov | de08c02 | 2012-06-19 09:21:57 +0000 | [diff] [blame] | 172 | int internal_snprintf(char *buffer, uptr length, const char *format, ...) { |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 173 | va_list args; |
| 174 | va_start(args, format); |
| 175 | int needed_length = VSNPrintf(buffer, length, format, args); |
| 176 | va_end(args); |
| 177 | return needed_length; |
| 178 | } |
| 179 | |
| 180 | // Like Printf, but prints the current PID before the output string. |
| 181 | void Report(const char *format, ...) { |
| 182 | const int kLen = 1024 * 4; |
Kostya Serebryany | 4fa111c | 2012-08-29 08:40:36 +0000 | [diff] [blame] | 183 | InternalScopedBuffer<char> buffer(kLen); |
| 184 | int needed_length = internal_snprintf(buffer.data(), |
| 185 | kLen, "==%d== ", GetPid()); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 186 | RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n"); |
| 187 | va_list args; |
| 188 | va_start(args, format); |
Kostya Serebryany | 4fa111c | 2012-08-29 08:40:36 +0000 | [diff] [blame] | 189 | needed_length += VSNPrintf(buffer.data() + needed_length, |
| 190 | kLen - needed_length, format, args); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 191 | va_end(args); |
| 192 | RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n"); |
Kostya Serebryany | 4fa111c | 2012-08-29 08:40:36 +0000 | [diff] [blame] | 193 | RawWrite(buffer.data()); |
Kostya Serebryany | 283c296 | 2012-08-28 11:34:40 +0000 | [diff] [blame] | 194 | if (PrintfAndReportCallback) |
Kostya Serebryany | 4fa111c | 2012-08-29 08:40:36 +0000 | [diff] [blame] | 195 | PrintfAndReportCallback(buffer.data()); |
Alexey Samsonov | e954101 | 2012-06-06 13:11:29 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | } // namespace __sanitizer |