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