blob: f834d2ba0df2114718549561771d340512a16521 [file] [log] [blame]
Alexey Samsonove9541012012-06-06 13:11:29 +00001//===-- 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
24namespace __sanitizer {
25
26static 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.
36static 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
58static 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
68static 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
78static 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
86int VSNPrintf(char *buff, int buff_length,
87 const char *format, va_list args) {
88 static const char *kPrintfFormatsHelp = "Supported Printf formats: "
Kostya Serebryany81dfbb72012-09-14 04:35:14 +000089 "%%[z]{d,u,x}; %%p; %%s; %%c\n";
Alexey Samsonove9541012012-06-06 13:11:29 +000090 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 Serebryany81dfbb72012-09-14 04:35:14 +0000130 case 'c': {
131 RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);
Kostya Serebryany01167e82012-09-14 06:52:46 +0000132 result += AppendChar(&buff, buff_end, va_arg(args, int));
Kostya Serebryany81dfbb72012-09-14 04:35:14 +0000133 break;
134 }
Alexey Samsonove9541012012-06-06 13:11:29 +0000135 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 Serebryany283c2962012-08-28 11:34:40 +0000150static void (*PrintfAndReportCallback)(const char *);
151void SetPrintfAndReportCallback(void (*callback)(const char *)) {
152 PrintfAndReportCallback = callback;
153}
154
Alexey Samsonove9541012012-06-06 13:11:29 +0000155void Printf(const char *format, ...) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000156 const int kLen = 16 * 1024;
Kostya Serebryany4fa111c2012-08-29 08:40:36 +0000157 InternalScopedBuffer<char> buffer(kLen);
Alexey Samsonove9541012012-06-06 13:11:29 +0000158 va_list args;
159 va_start(args, format);
Kostya Serebryany4fa111c2012-08-29 08:40:36 +0000160 int needed_length = VSNPrintf(buffer.data(), kLen, format, args);
Alexey Samsonove9541012012-06-06 13:11:29 +0000161 va_end(args);
162 RAW_CHECK_MSG(needed_length < kLen, "Buffer in Printf is too short!\n");
Kostya Serebryany4fa111c2012-08-29 08:40:36 +0000163 RawWrite(buffer.data());
Kostya Serebryany283c2962012-08-28 11:34:40 +0000164 if (PrintfAndReportCallback)
Kostya Serebryany4fa111c2012-08-29 08:40:36 +0000165 PrintfAndReportCallback(buffer.data());
Alexey Samsonove9541012012-06-06 13:11:29 +0000166}
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 Samsonovde08c022012-06-19 09:21:57 +0000172int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
Alexey Samsonove9541012012-06-06 13:11:29 +0000173 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.
181void Report(const char *format, ...) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000182 const int kLen = 16 * 1024;
Kostya Serebryany4fa111c2012-08-29 08:40:36 +0000183 InternalScopedBuffer<char> buffer(kLen);
184 int needed_length = internal_snprintf(buffer.data(),
185 kLen, "==%d== ", GetPid());
Alexey Samsonove9541012012-06-06 13:11:29 +0000186 RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n");
187 va_list args;
188 va_start(args, format);
Kostya Serebryany4fa111c2012-08-29 08:40:36 +0000189 needed_length += VSNPrintf(buffer.data() + needed_length,
190 kLen - needed_length, format, args);
Alexey Samsonove9541012012-06-06 13:11:29 +0000191 va_end(args);
192 RAW_CHECK_MSG(needed_length < kLen, "Buffer in Report is too short!\n");
Kostya Serebryany4fa111c2012-08-29 08:40:36 +0000193 RawWrite(buffer.data());
Kostya Serebryany283c2962012-08-28 11:34:40 +0000194 if (PrintfAndReportCallback)
Kostya Serebryany4fa111c2012-08-29 08:40:36 +0000195 PrintfAndReportCallback(buffer.data());
Alexey Samsonove9541012012-06-06 13:11:29 +0000196}
197
198} // namespace __sanitizer