njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Libc printing. m_libcprint.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
njn | 9f20746 | 2009-03-10 22:02:09 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2009 Julian Seward |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 32 | #include "pub_core_vki.h" |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 33 | #include "pub_core_debuglog.h" |
| 34 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 35 | #include "pub_core_libcassert.h" |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 36 | #include "pub_core_libcfile.h" // VG_(write)(), VG_(write_socket)() |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 37 | #include "pub_core_libcprint.h" |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 38 | #include "pub_core_libcproc.h" // VG_(getpid)(), VG_(read_millisecond_timer() |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 39 | #include "pub_core_options.h" |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 40 | #include "valgrind.h" // For RUNNING_ON_VALGRIND |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 41 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 42 | |
| 43 | /* --------------------------------------------------------------------- |
| 44 | Writing to file or a socket |
| 45 | ------------------------------------------------------------------ */ |
| 46 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 47 | /* The destination sinks for normal and XML output. These have their |
| 48 | initial values here; they are set to final values by |
| 49 | m_main.main_process_cmd_line_options(). See comment at the top of |
| 50 | that function for the associated logic. */ |
| 51 | OutputSink VG_(log_output_sink) = { 2, False }; /* 2 = stderr */ |
| 52 | OutputSink VG_(xml_output_sink) = { -1, False }; /* disabled */ |
| 53 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 54 | /* Do the low-level send of a message to the logging sink. */ |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 55 | static |
| 56 | void send_bytes_to_logging_sink ( OutputSink* sink, Char* msg, Int nbytes ) |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 57 | { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 58 | if (sink->is_socket) { |
| 59 | Int rc = VG_(write_socket)( sink->fd, msg, nbytes ); |
| 60 | if (rc == -1) { |
| 61 | // For example, the listener process died. Switch back to stderr. |
| 62 | sink->is_socket = False; |
| 63 | sink->fd = 2; |
| 64 | VG_(write)( sink->fd, msg, nbytes ); |
| 65 | } |
| 66 | } else { |
| 67 | /* sink->fd could have been set to -1 in the various |
sewardj | 6e31f80 | 2007-11-17 22:29:25 +0000 | [diff] [blame] | 68 | sys-wrappers for sys_fork, if --child-silent-after-fork=yes |
| 69 | is in effect. That is a signal that we should not produce |
| 70 | any more output. */ |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 71 | if (sink->fd >= 0) |
| 72 | VG_(write)( sink->fd, msg, nbytes ); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 76 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 77 | /* --------------------------------------------------------------------- |
| 78 | printf() and friends |
| 79 | ------------------------------------------------------------------ */ |
| 80 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 81 | /* --------- printf --------- */ |
| 82 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 83 | typedef |
| 84 | struct { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 85 | HChar buf[512]; |
| 86 | Int buf_used; |
| 87 | OutputSink* sink; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 88 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 89 | printf_buf_t; |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame] | 90 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 91 | // Adds a single char to the buffer. When the buffer gets sufficiently |
| 92 | // full, we write its contents to the logging sink. |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 93 | static void add_to__printf_buf ( HChar c, void *p ) |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 94 | { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 95 | printf_buf_t *b = (printf_buf_t *)p; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 96 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 97 | if (b->buf_used > sizeof(b->buf) - 2 ) { |
| 98 | send_bytes_to_logging_sink( b->sink, b->buf, b->buf_used ); |
| 99 | b->buf_used = 0; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 100 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 101 | b->buf[b->buf_used++] = c; |
| 102 | b->buf[b->buf_used] = 0; |
| 103 | tl_assert(b->buf_used < sizeof(b->buf)); |
| 104 | } |
| 105 | |
| 106 | static UInt vprintf_to_buf ( printf_buf_t* b, |
| 107 | const HChar *format, va_list vargs ) |
| 108 | { |
| 109 | UInt ret = 0; |
| 110 | if (b->sink->fd >= 0) { |
| 111 | ret = VG_(debugLog_vprintf) |
| 112 | ( add_to__printf_buf, b, format, vargs ); |
| 113 | } |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static UInt vprintf_WRK ( OutputSink* sink, |
| 118 | const HChar *format, va_list vargs ) |
| 119 | { |
| 120 | printf_buf_t myprintf_buf |
| 121 | = { "", 0, sink }; |
| 122 | UInt ret |
| 123 | = vprintf_to_buf(&myprintf_buf, format, vargs); |
| 124 | // Write out any chars left in the buffer. |
| 125 | if (myprintf_buf.buf_used > 0) { |
| 126 | send_bytes_to_logging_sink( myprintf_buf.sink, |
| 127 | myprintf_buf.buf, |
| 128 | myprintf_buf.buf_used ); |
| 129 | } |
| 130 | return ret; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | UInt VG_(vprintf) ( const HChar *format, va_list vargs ) |
| 134 | { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 135 | return vprintf_WRK( &VG_(log_output_sink), format, vargs ); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | UInt VG_(printf) ( const HChar *format, ... ) |
| 139 | { |
| 140 | UInt ret; |
| 141 | va_list vargs; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 142 | va_start(vargs, format); |
| 143 | ret = VG_(vprintf)(format, vargs); |
| 144 | va_end(vargs); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 145 | return ret; |
| 146 | } |
| 147 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 148 | UInt VG_(vprintf_xml) ( const HChar *format, va_list vargs ) |
| 149 | { |
| 150 | return vprintf_WRK( &VG_(xml_output_sink), format, vargs ); |
| 151 | } |
| 152 | |
| 153 | UInt VG_(printf_xml) ( const HChar *format, ... ) |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame] | 154 | { |
| 155 | UInt ret; |
| 156 | va_list vargs; |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame] | 157 | va_start(vargs, format); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 158 | ret = VG_(vprintf_xml)(format, vargs); |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame] | 159 | va_end(vargs); |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame] | 160 | return ret; |
| 161 | } |
| 162 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 163 | /* An exact clone of VG_(printf_xml), unfortunately. */ |
| 164 | UInt VG_(printf_xml_no_f_c) ( const HChar *format, ... ) |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 165 | { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 166 | UInt ret; |
| 167 | va_list vargs; |
| 168 | va_start(vargs, format); |
| 169 | ret = VG_(vprintf_xml)(format, vargs); |
| 170 | va_end(vargs); |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /* --------- sprintf --------- */ |
| 176 | |
| 177 | /* If we had an explicit buf structure here, it would contain only one |
| 178 | field, indicating where the next char is to go. So use p directly |
| 179 | for that, rather than having it be a pointer to a structure. */ |
| 180 | |
| 181 | static void add_to__sprintf_buf ( HChar c, void *p ) |
| 182 | { |
| 183 | HChar** b = p; |
| 184 | *(*b)++ = c; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | UInt VG_(vsprintf) ( Char* buf, const HChar *format, va_list vargs ) |
| 188 | { |
| 189 | Int ret; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 190 | HChar* sprintf_ptr = buf; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 191 | |
| 192 | ret = VG_(debugLog_vprintf) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 193 | ( add_to__sprintf_buf, &sprintf_ptr, format, vargs ); |
| 194 | add_to__sprintf_buf('\0', &sprintf_ptr); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 195 | |
| 196 | vg_assert(VG_(strlen)(buf) == ret); |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | UInt VG_(sprintf) ( Char* buf, const HChar *format, ... ) |
| 202 | { |
| 203 | UInt ret; |
| 204 | va_list vargs; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 205 | va_start(vargs,format); |
| 206 | ret = VG_(vsprintf)(buf, format, vargs); |
| 207 | va_end(vargs); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 208 | return ret; |
| 209 | } |
| 210 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 211 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 212 | /* --------- snprintf --------- */ |
| 213 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 214 | typedef |
| 215 | struct { |
| 216 | HChar* buf; |
| 217 | Int buf_size; |
| 218 | Int buf_used; |
| 219 | } |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 220 | snprintf_buf_t; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 221 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 222 | static void add_to__snprintf_buf ( HChar c, void* p ) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 223 | { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 224 | snprintf_buf_t* b = p; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 225 | if (b->buf_size > 0 && b->buf_used < b->buf_size) { |
| 226 | b->buf[b->buf_used++] = c; |
| 227 | if (b->buf_used < b->buf_size) |
| 228 | b->buf[b->buf_used] = 0; |
sewardj | 03e2bb8 | 2006-12-24 03:02:18 +0000 | [diff] [blame] | 229 | else |
| 230 | b->buf[b->buf_size-1] = 0; /* pre: b->buf_size > 0 */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | UInt VG_(vsnprintf) ( Char* buf, Int size, const HChar *format, va_list vargs ) |
| 235 | { |
| 236 | Int ret; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 237 | snprintf_buf_t b; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 238 | b.buf = buf; |
| 239 | b.buf_size = size < 0 ? 0 : size; |
| 240 | b.buf_used = 0; |
| 241 | |
| 242 | ret = VG_(debugLog_vprintf) |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 243 | ( add_to__snprintf_buf, &b, format, vargs ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 244 | |
| 245 | return b.buf_used; |
| 246 | } |
| 247 | |
| 248 | UInt VG_(snprintf) ( Char* buf, Int size, const HChar *format, ... ) |
| 249 | { |
| 250 | UInt ret; |
| 251 | va_list vargs; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 252 | va_start(vargs,format); |
| 253 | ret = VG_(vsnprintf)(buf, size, format, vargs); |
| 254 | va_end(vargs); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 259 | /* --------- vcbprintf --------- */ |
| 260 | |
| 261 | void VG_(vcbprintf)( void(*char_sink)(HChar, void* opaque), |
| 262 | void* opaque, |
| 263 | const HChar* format, va_list vargs ) |
| 264 | { |
| 265 | (void) VG_(debugLog_vprintf) |
| 266 | ( char_sink, opaque, format, vargs ); |
| 267 | } |
| 268 | |
| 269 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 270 | /* --------------------------------------------------------------------- |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 271 | percentify() |
| 272 | ------------------------------------------------------------------ */ |
| 273 | |
njn | 7a5915e | 2005-07-17 16:12:59 +0000 | [diff] [blame] | 274 | // Percentify n/m with d decimal places. Includes the '%' symbol at the end. |
njn | 5eaff2f | 2005-09-25 19:11:45 +0000 | [diff] [blame] | 275 | // Right justifies in 'buf'. |
| 276 | void VG_(percentify)(ULong n, ULong m, UInt d, Int n_buf, char buf[]) |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 277 | { |
| 278 | Int i, len, space; |
njn | 641e616 | 2005-07-17 16:16:41 +0000 | [diff] [blame] | 279 | ULong p1; |
njn | 5eaff2f | 2005-09-25 19:11:45 +0000 | [diff] [blame] | 280 | Char fmt[32]; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 281 | |
njn | 641e616 | 2005-07-17 16:16:41 +0000 | [diff] [blame] | 282 | if (m == 0) { |
njn | 5eaff2f | 2005-09-25 19:11:45 +0000 | [diff] [blame] | 283 | // Have to generate the format string in order to be flexible about |
| 284 | // the width of the field. |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 285 | VG_(sprintf)(fmt, "%%-%ds", n_buf); |
njn | 5eaff2f | 2005-09-25 19:11:45 +0000 | [diff] [blame] | 286 | // fmt is now "%<n_buf>s" where <d> is 1,2,3... |
| 287 | VG_(sprintf)(buf, fmt, "--%"); |
njn | 641e616 | 2005-07-17 16:16:41 +0000 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | p1 = (100*n) / m; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 292 | |
| 293 | if (d == 0) { |
| 294 | VG_(sprintf)(buf, "%lld%%", p1); |
| 295 | } else { |
| 296 | ULong p2; |
| 297 | UInt ex; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 298 | switch (d) { |
| 299 | case 1: ex = 10; break; |
| 300 | case 2: ex = 100; break; |
| 301 | case 3: ex = 1000; break; |
| 302 | default: VG_(tool_panic)("Currently can only handle 3 decimal places"); |
| 303 | } |
| 304 | p2 = ((100*n*ex) / m) % ex; |
| 305 | // Have to generate the format string in order to be flexible about |
| 306 | // the width of the post-decimal-point part. |
| 307 | VG_(sprintf)(fmt, "%%lld.%%0%dlld%%%%", d); |
| 308 | // fmt is now "%lld.%0<d>lld%%" where <d> is 1,2,3... |
| 309 | VG_(sprintf)(buf, fmt, p1, p2); |
| 310 | } |
| 311 | |
| 312 | len = VG_(strlen)(buf); |
| 313 | space = n_buf - len; |
| 314 | if (space < 0) space = 0; /* Allow for v. small field_width */ |
| 315 | i = len; |
| 316 | |
| 317 | /* Right justify in field */ |
| 318 | for ( ; i >= 0; i--) buf[i + space] = buf[i]; |
| 319 | for (i = 0; i < space; i++) buf[i] = ' '; |
| 320 | } |
| 321 | |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 322 | |
| 323 | /* --------------------------------------------------------------------- |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 324 | elapsed_wallclock_time() |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 325 | ------------------------------------------------------------------ */ |
| 326 | |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 327 | /* Get the elapsed wallclock time since startup into buf, which must |
| 328 | 16 chars long. This is unchecked. It also relies on the |
| 329 | millisecond timer having been set to zero by an initial read in |
| 330 | m_main during startup. */ |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 331 | |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 332 | void VG_(elapsed_wallclock_time) ( /*OUT*/HChar* buf ) |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 333 | { |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 334 | UInt t, ms, s, mins, hours, days; |
| 335 | |
| 336 | t = VG_(read_millisecond_timer)(); /* milliseconds */ |
| 337 | |
| 338 | ms = t % 1000; |
| 339 | t /= 1000; /* now in seconds */ |
| 340 | |
| 341 | s = t % 60; |
| 342 | t /= 60; /* now in minutes */ |
| 343 | |
| 344 | mins = t % 60; |
| 345 | t /= 60; /* now in hours */ |
| 346 | |
| 347 | hours = t % 24; |
| 348 | t /= 24; /* now in days */ |
| 349 | |
| 350 | days = t; |
| 351 | |
njn | 82baca7 | 2009-07-24 05:35:49 +0000 | [diff] [blame] | 352 | VG_(sprintf)(buf, "%02u:%02u:%02u:%02u.%03u ", days, hours, mins, s, ms); |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 356 | /* --------------------------------------------------------------------- |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 357 | message() |
| 358 | ------------------------------------------------------------------ */ |
| 359 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 360 | /* A buffer for accumulating VG_(message) style output. This is |
| 361 | pretty much the same as VG_(printf)'s scheme, with two differences: |
| 362 | |
| 363 | * The message buffer persists between calls, so that multiple |
| 364 | calls to VG_(message) can build up output. |
| 365 | |
| 366 | * Whenever the first character on a line is emitted, the |
| 367 | ==PID== style preamble is stuffed in before it. |
| 368 | */ |
| 369 | typedef |
| 370 | struct { |
| 371 | HChar buf[512+128]; |
| 372 | Int buf_used; |
| 373 | Bool atLeft; /* notionally, is the next char position at the |
| 374 | leftmost column? */ |
| 375 | /* Current message kind - changes from call to call */ |
| 376 | VgMsgKind kind; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 377 | /* destination */ |
| 378 | OutputSink* sink; |
| 379 | } |
| 380 | vmessage_buf_t; |
| 381 | |
| 382 | static vmessage_buf_t vmessage_buf |
sewardj | 8ddb93a | 2010-02-15 10:07:05 +0000 | [diff] [blame^] | 383 | = { "", 0, True, Vg_UserMsg, &VG_(log_output_sink) }; |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 384 | |
| 385 | |
| 386 | // Adds a single char to the buffer. We aim to have at least 128 |
| 387 | // bytes free in the buffer, so that it's always possible to emit |
| 388 | // the preamble into the buffer if c happens to be the character |
| 389 | // following a \n. When the buffer gets too full, we write its |
| 390 | // contents to the logging sink. |
| 391 | static void add_to__vmessage_buf ( HChar c, void *p ) |
| 392 | { |
| 393 | HChar tmp[64]; |
| 394 | vmessage_buf_t* b = (vmessage_buf_t*)p; |
| 395 | |
| 396 | vg_assert(b->buf_used >= 0 && b->buf_used < sizeof(b->buf)-128); |
| 397 | |
| 398 | if (UNLIKELY(b->atLeft)) { |
| 399 | // insert preamble |
| 400 | HChar ch; |
| 401 | Int i, depth; |
| 402 | |
| 403 | switch (b->kind) { |
| 404 | case Vg_UserMsg: ch = '='; break; |
| 405 | case Vg_DebugMsg: ch = '-'; break; |
| 406 | case Vg_DebugExtraMsg: ch = '+'; break; |
| 407 | case Vg_ClientMsg: ch = '*'; break; |
| 408 | default: ch = '?'; break; |
| 409 | } |
| 410 | |
| 411 | // Print one '>' in front of the messages for each level of |
| 412 | // self-hosting being performed. |
| 413 | depth = RUNNING_ON_VALGRIND; |
| 414 | if (depth > 10) |
| 415 | depth = 10; // ?!?! |
| 416 | for (i = 0; i < depth; i++) { |
| 417 | b->buf[b->buf_used++] = '>'; |
| 418 | } |
| 419 | |
| 420 | b->buf[b->buf_used++] = ch; |
| 421 | b->buf[b->buf_used++] = ch; |
| 422 | |
| 423 | if (VG_(clo_time_stamp)) { |
| 424 | VG_(memset)(tmp, 0, sizeof(tmp)); |
| 425 | VG_(elapsed_wallclock_time)(tmp); |
| 426 | tmp[sizeof(tmp)-1] = 0; |
| 427 | for (i = 0; tmp[i]; i++) |
| 428 | b->buf[b->buf_used++] = tmp[i]; |
| 429 | } |
| 430 | |
sewardj | 8ddb93a | 2010-02-15 10:07:05 +0000 | [diff] [blame^] | 431 | VG_(sprintf)(tmp, "%d", VG_(getpid)()); |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 432 | tmp[sizeof(tmp)-1] = 0; |
| 433 | for (i = 0; tmp[i]; i++) |
| 434 | b->buf[b->buf_used++] = tmp[i]; |
| 435 | |
| 436 | b->buf[b->buf_used++] = ch; |
| 437 | b->buf[b->buf_used++] = ch; |
| 438 | b->buf[b->buf_used++] = ' '; |
| 439 | |
| 440 | /* We can't possibly have stuffed 96 chars in merely as a result |
| 441 | of making the preamble (can we?) */ |
| 442 | vg_assert(b->buf_used < sizeof(b->buf)-32); |
| 443 | } |
| 444 | |
| 445 | b->buf[b->buf_used++] = c; |
| 446 | b->buf[b->buf_used] = 0; |
| 447 | |
| 448 | if (b->buf_used >= sizeof(b->buf) - 128) { |
| 449 | send_bytes_to_logging_sink( b->sink, b->buf, b->buf_used ); |
| 450 | b->buf_used = 0; |
| 451 | } |
| 452 | |
| 453 | b->atLeft = c == '\n'; |
| 454 | } |
| 455 | |
| 456 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 457 | UInt VG_(vmessage) ( VgMsgKind kind, const HChar* format, va_list vargs ) |
| 458 | { |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 459 | UInt ret; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 460 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 461 | /* Note (carefully) that the buf persists from call to call, unlike |
| 462 | with the other printf variants in earlier parts of this file. */ |
| 463 | vmessage_buf_t* b = &vmessage_buf; /* shorthand for convenience */ |
| 464 | |
| 465 | /* We have to set this each call, so that the correct flavour |
| 466 | of preamble is emitted at each \n. */ |
| 467 | b->kind = kind; |
| 468 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 469 | ret = VG_(debugLog_vprintf) ( add_to__vmessage_buf, |
| 470 | b, format, vargs ); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 471 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 472 | /* If the message finished exactly with a \n, then flush it at this |
| 473 | point. If not, assume more bits of the same line will turn up |
| 474 | in later messages, so don't bother to flush it right now. */ |
| 475 | |
| 476 | if (b->atLeft && b->buf_used > 0) { |
| 477 | send_bytes_to_logging_sink( b->sink, b->buf, b->buf_used ); |
| 478 | b->buf_used = 0; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 479 | } |
| 480 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 481 | return ret; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 482 | } |
| 483 | |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 484 | /* Send a simple single-part XML message. */ |
| 485 | UInt VG_(message_no_f_c) ( VgMsgKind kind, const HChar* format, ... ) |
| 486 | { |
| 487 | UInt count; |
| 488 | va_list vargs; |
| 489 | va_start(vargs,format); |
| 490 | count = VG_(vmessage) ( kind, format, vargs ); |
| 491 | va_end(vargs); |
| 492 | return count; |
| 493 | } |
| 494 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 495 | /* Send a simple single-part message. */ |
| 496 | UInt VG_(message) ( VgMsgKind kind, const HChar* format, ... ) |
| 497 | { |
| 498 | UInt count; |
| 499 | va_list vargs; |
| 500 | va_start(vargs,format); |
| 501 | count = VG_(vmessage) ( kind, format, vargs ); |
| 502 | va_end(vargs); |
| 503 | return count; |
| 504 | } |
| 505 | |
sewardj | 738856f | 2009-07-15 14:48:32 +0000 | [diff] [blame] | 506 | /* VG_(message) variants with hardwired first argument. */ |
| 507 | UInt VG_(umsg) ( const HChar* format, ... ) |
| 508 | { |
| 509 | UInt count; |
| 510 | va_list vargs; |
| 511 | va_start(vargs,format); |
| 512 | count = VG_(vmessage) ( Vg_UserMsg, format, vargs ); |
| 513 | va_end(vargs); |
| 514 | return count; |
| 515 | } |
| 516 | |
| 517 | UInt VG_(dmsg) ( const HChar* format, ... ) |
| 518 | { |
| 519 | UInt count; |
| 520 | va_list vargs; |
| 521 | va_start(vargs,format); |
| 522 | count = VG_(vmessage) ( Vg_DebugMsg, format, vargs ); |
| 523 | va_end(vargs); |
| 524 | return count; |
| 525 | } |
| 526 | |
| 527 | UInt VG_(emsg) ( const HChar* format, ... ) |
| 528 | { |
| 529 | UInt count; |
| 530 | va_list vargs; |
| 531 | va_start(vargs,format); |
| 532 | count = VG_(vmessage) ( Vg_DebugExtraMsg, format, vargs ); |
| 533 | va_end(vargs); |
| 534 | return count; |
| 535 | } |
| 536 | |
| 537 | /* Flush any output that has accumulated in vmessage_buf as a |
| 538 | result of previous calls to VG_(message) et al. */ |
| 539 | void VG_(message_flush) ( void ) |
| 540 | { |
| 541 | vmessage_buf_t* b = &vmessage_buf; |
| 542 | send_bytes_to_logging_sink( b->sink, b->buf, b->buf_used ); |
| 543 | b->buf_used = 0; |
| 544 | } |
| 545 | |
| 546 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 547 | /*--------------------------------------------------------------------*/ |
| 548 | /*--- end ---*/ |
| 549 | /*--------------------------------------------------------------------*/ |
| 550 | |