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 | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2008 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 | |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 43 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 44 | /* --------------------------------------------------------------------- |
| 45 | Writing to file or a socket |
| 46 | ------------------------------------------------------------------ */ |
| 47 | |
| 48 | /* Tell the logging mechanism whether we are logging to a file |
| 49 | descriptor or a socket descriptor. */ |
| 50 | Bool VG_(logging_to_socket) = False; |
| 51 | |
| 52 | /* Do the low-level send of a message to the logging sink. */ |
| 53 | static void send_bytes_to_logging_sink ( Char* msg, Int nbytes ) |
| 54 | { |
| 55 | if (!VG_(logging_to_socket)) { |
sewardj | 6e31f80 | 2007-11-17 22:29:25 +0000 | [diff] [blame] | 56 | /* VG_(clo_log_fd) could have been set to -1 in the various |
| 57 | sys-wrappers for sys_fork, if --child-silent-after-fork=yes |
| 58 | is in effect. That is a signal that we should not produce |
| 59 | any more output. */ |
| 60 | if (VG_(clo_log_fd) >= 0) |
| 61 | VG_(write)( VG_(clo_log_fd), msg, nbytes ); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 62 | } else { |
| 63 | Int rc = VG_(write_socket)( VG_(clo_log_fd), msg, nbytes ); |
| 64 | if (rc == -1) { |
| 65 | // For example, the listener process died. Switch back to stderr. |
| 66 | VG_(logging_to_socket) = False; |
| 67 | VG_(clo_log_fd) = 2; |
| 68 | VG_(write)( VG_(clo_log_fd), msg, nbytes ); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* --------------------------------------------------------------------- |
| 74 | printf() and friends |
| 75 | ------------------------------------------------------------------ */ |
| 76 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 77 | typedef |
| 78 | struct { |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 79 | HChar buf[128]; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 80 | Int n; |
| 81 | } |
| 82 | printf_buf; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 83 | |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 84 | static UInt VG_(vprintf_to_buf) ( printf_buf *printf_buf, |
| 85 | const HChar *format, va_list vargs ); |
| 86 | static UInt VG_(printf_to_buf) ( printf_buf* prbuf, const HChar *format, ... ); |
| 87 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 88 | // Adds a single char to the buffer. When the buffer gets sufficiently |
| 89 | // full, we write its contents to the logging sink. |
| 90 | static void add_to_myprintf_buf ( HChar c, void *p ) |
| 91 | { |
| 92 | printf_buf *myprintf_buf = (printf_buf *)p; |
| 93 | |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 94 | if (myprintf_buf->n > sizeof(myprintf_buf->buf) - 2 ) { |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 95 | send_bytes_to_logging_sink( myprintf_buf->buf, myprintf_buf->n ); |
| 96 | myprintf_buf->n = 0; |
| 97 | } |
| 98 | myprintf_buf->buf[myprintf_buf->n++] = c; |
| 99 | myprintf_buf->buf[myprintf_buf->n] = 0; |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 100 | tl_assert(myprintf_buf->n < sizeof(myprintf_buf->buf)); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | UInt VG_(vprintf) ( const HChar *format, va_list vargs ) |
| 104 | { |
| 105 | UInt ret = 0; |
| 106 | printf_buf myprintf_buf = {"",0}; |
| 107 | |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 108 | ret = VG_(vprintf_to_buf)(&myprintf_buf, format, vargs); |
| 109 | // Write out any chars left in the buffer. |
| 110 | if (myprintf_buf.n > 0) { |
| 111 | send_bytes_to_logging_sink( myprintf_buf.buf, myprintf_buf.n ); |
| 112 | } |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static UInt VG_(vprintf_to_buf) ( printf_buf *prbuf, |
| 117 | const HChar *format, va_list vargs ) |
| 118 | { |
| 119 | UInt ret = 0; |
| 120 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 121 | if (VG_(clo_log_fd) >= 0) { |
| 122 | ret = VG_(debugLog_vprintf) |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 123 | ( add_to_myprintf_buf, prbuf, format, vargs ); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 124 | } |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | UInt VG_(printf) ( const HChar *format, ... ) |
| 129 | { |
| 130 | UInt ret; |
| 131 | va_list vargs; |
| 132 | |
| 133 | va_start(vargs, format); |
| 134 | ret = VG_(vprintf)(format, vargs); |
| 135 | va_end(vargs); |
| 136 | |
| 137 | return ret; |
| 138 | } |
| 139 | |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 140 | static UInt VG_(printf_to_buf) ( printf_buf* prbuf, const HChar *format, ... ) |
| 141 | { |
| 142 | UInt ret; |
| 143 | va_list vargs; |
| 144 | |
| 145 | va_start(vargs, format); |
| 146 | ret = VG_(vprintf_to_buf)(prbuf, format, vargs); |
| 147 | va_end(vargs); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 152 | /* A general replacement for sprintf(). */ |
| 153 | static void add_to_vg_sprintf_buf ( HChar c, void *p ) |
| 154 | { |
| 155 | char **vg_sprintf_ptr = p; |
| 156 | *(*vg_sprintf_ptr)++ = c; |
| 157 | } |
| 158 | |
| 159 | UInt VG_(vsprintf) ( Char* buf, const HChar *format, va_list vargs ) |
| 160 | { |
| 161 | Int ret; |
| 162 | Char *vg_sprintf_ptr = buf; |
| 163 | |
| 164 | ret = VG_(debugLog_vprintf) |
| 165 | ( add_to_vg_sprintf_buf, &vg_sprintf_ptr, format, vargs ); |
| 166 | add_to_vg_sprintf_buf('\0', &vg_sprintf_ptr); |
| 167 | |
| 168 | vg_assert(VG_(strlen)(buf) == ret); |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | UInt VG_(sprintf) ( Char* buf, const HChar *format, ... ) |
| 174 | { |
| 175 | UInt ret; |
| 176 | va_list vargs; |
| 177 | |
| 178 | va_start(vargs,format); |
| 179 | ret = VG_(vsprintf)(buf, format, vargs); |
| 180 | va_end(vargs); |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 185 | |
| 186 | /* A replacement for snprintf. */ |
| 187 | typedef |
| 188 | struct { |
| 189 | HChar* buf; |
| 190 | Int buf_size; |
| 191 | Int buf_used; |
| 192 | } |
| 193 | snprintf_buf; |
| 194 | |
| 195 | static void add_to_vg_snprintf_buf ( HChar c, void* p ) |
| 196 | { |
| 197 | snprintf_buf* b = p; |
| 198 | if (b->buf_size > 0 && b->buf_used < b->buf_size) { |
| 199 | b->buf[b->buf_used++] = c; |
| 200 | if (b->buf_used < b->buf_size) |
| 201 | b->buf[b->buf_used] = 0; |
sewardj | 03e2bb8 | 2006-12-24 03:02:18 +0000 | [diff] [blame] | 202 | else |
| 203 | b->buf[b->buf_size-1] = 0; /* pre: b->buf_size > 0 */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | UInt VG_(vsnprintf) ( Char* buf, Int size, const HChar *format, va_list vargs ) |
| 208 | { |
| 209 | Int ret; |
| 210 | snprintf_buf b; |
| 211 | b.buf = buf; |
| 212 | b.buf_size = size < 0 ? 0 : size; |
| 213 | b.buf_used = 0; |
| 214 | |
| 215 | ret = VG_(debugLog_vprintf) |
| 216 | ( add_to_vg_snprintf_buf, &b, format, vargs ); |
| 217 | |
| 218 | return b.buf_used; |
| 219 | } |
| 220 | |
| 221 | UInt VG_(snprintf) ( Char* buf, Int size, const HChar *format, ... ) |
| 222 | { |
| 223 | UInt ret; |
| 224 | va_list vargs; |
| 225 | |
| 226 | va_start(vargs,format); |
| 227 | ret = VG_(vsnprintf)(buf, size, format, vargs); |
| 228 | va_end(vargs); |
| 229 | |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 234 | /* --------------------------------------------------------------------- |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 235 | percentify() |
| 236 | ------------------------------------------------------------------ */ |
| 237 | |
njn | 7a5915e | 2005-07-17 16:12:59 +0000 | [diff] [blame] | 238 | // Percentify n/m with d decimal places. Includes the '%' symbol at the end. |
njn | 5eaff2f | 2005-09-25 19:11:45 +0000 | [diff] [blame] | 239 | // Right justifies in 'buf'. |
| 240 | 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] | 241 | { |
| 242 | Int i, len, space; |
njn | 641e616 | 2005-07-17 16:16:41 +0000 | [diff] [blame] | 243 | ULong p1; |
njn | 5eaff2f | 2005-09-25 19:11:45 +0000 | [diff] [blame] | 244 | Char fmt[32]; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 245 | |
njn | 641e616 | 2005-07-17 16:16:41 +0000 | [diff] [blame] | 246 | if (m == 0) { |
njn | 5eaff2f | 2005-09-25 19:11:45 +0000 | [diff] [blame] | 247 | // Have to generate the format string in order to be flexible about |
| 248 | // the width of the field. |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 249 | VG_(sprintf)(fmt, "%%-%ds", n_buf); |
njn | 5eaff2f | 2005-09-25 19:11:45 +0000 | [diff] [blame] | 250 | // fmt is now "%<n_buf>s" where <d> is 1,2,3... |
| 251 | VG_(sprintf)(buf, fmt, "--%"); |
njn | 641e616 | 2005-07-17 16:16:41 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
| 255 | p1 = (100*n) / m; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 256 | |
| 257 | if (d == 0) { |
| 258 | VG_(sprintf)(buf, "%lld%%", p1); |
| 259 | } else { |
| 260 | ULong p2; |
| 261 | UInt ex; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 262 | switch (d) { |
| 263 | case 1: ex = 10; break; |
| 264 | case 2: ex = 100; break; |
| 265 | case 3: ex = 1000; break; |
| 266 | default: VG_(tool_panic)("Currently can only handle 3 decimal places"); |
| 267 | } |
| 268 | p2 = ((100*n*ex) / m) % ex; |
| 269 | // Have to generate the format string in order to be flexible about |
| 270 | // the width of the post-decimal-point part. |
| 271 | VG_(sprintf)(fmt, "%%lld.%%0%dlld%%%%", d); |
| 272 | // fmt is now "%lld.%0<d>lld%%" where <d> is 1,2,3... |
| 273 | VG_(sprintf)(buf, fmt, p1, p2); |
| 274 | } |
| 275 | |
| 276 | len = VG_(strlen)(buf); |
| 277 | space = n_buf - len; |
| 278 | if (space < 0) space = 0; /* Allow for v. small field_width */ |
| 279 | i = len; |
| 280 | |
| 281 | /* Right justify in field */ |
| 282 | for ( ; i >= 0; i--) buf[i + space] = buf[i]; |
| 283 | for (i = 0; i < space; i++) buf[i] = ' '; |
| 284 | } |
| 285 | |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 286 | |
| 287 | /* --------------------------------------------------------------------- |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 288 | elapsed_wallclock_time() |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 289 | ------------------------------------------------------------------ */ |
| 290 | |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 291 | /* Get the elapsed wallclock time since startup into buf, which must |
| 292 | 16 chars long. This is unchecked. It also relies on the |
| 293 | millisecond timer having been set to zero by an initial read in |
| 294 | m_main during startup. */ |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 295 | |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 296 | void VG_(elapsed_wallclock_time) ( /*OUT*/HChar* buf ) |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 297 | { |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 298 | UInt t, ms, s, mins, hours, days; |
| 299 | |
| 300 | t = VG_(read_millisecond_timer)(); /* milliseconds */ |
| 301 | |
| 302 | ms = t % 1000; |
| 303 | t /= 1000; /* now in seconds */ |
| 304 | |
| 305 | s = t % 60; |
| 306 | t /= 60; /* now in minutes */ |
| 307 | |
| 308 | mins = t % 60; |
| 309 | t /= 60; /* now in hours */ |
| 310 | |
| 311 | hours = t % 24; |
| 312 | t /= 24; /* now in days */ |
| 313 | |
| 314 | days = t; |
| 315 | |
| 316 | VG_(sprintf)(buf, "%02u:%02u:%02u:%02u.%03u", days, hours, mins, s, ms); |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 320 | /* --------------------------------------------------------------------- |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 321 | message() |
| 322 | ------------------------------------------------------------------ */ |
| 323 | |
| 324 | UInt VG_(vmessage) ( VgMsgKind kind, const HChar* format, va_list vargs ) |
| 325 | { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 326 | UInt count = 0; |
| 327 | Char c; |
| 328 | Int i, depth; |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 329 | printf_buf myprintf_buf = {"",0}; |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 330 | |
| 331 | switch (kind) { |
| 332 | case Vg_UserMsg: c = '='; break; |
| 333 | case Vg_DebugMsg: c = '-'; break; |
| 334 | case Vg_DebugExtraMsg: c = '+'; break; |
| 335 | case Vg_ClientMsg: c = '*'; break; |
| 336 | default: c = '?'; break; |
| 337 | } |
| 338 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 339 | // Print one '>' in front of the messages for each level of self-hosting |
| 340 | // being performed. |
| 341 | depth = RUNNING_ON_VALGRIND; |
| 342 | for (i = 0; i < depth; i++) { |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 343 | count += VG_(printf_to_buf) (&myprintf_buf, ">"); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 344 | } |
| 345 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 346 | if (!VG_(clo_xml)) |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 347 | count += VG_(printf_to_buf) (&myprintf_buf, "%c%c", c,c); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 348 | |
| 349 | if (VG_(clo_time_stamp)) { |
sewardj | a11553a | 2005-07-19 12:17:05 +0000 | [diff] [blame] | 350 | HChar buf[50]; |
sewardj | 592ae09 | 2005-11-08 19:01:44 +0000 | [diff] [blame] | 351 | VG_(elapsed_wallclock_time)(buf); |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 352 | count += VG_(printf_to_buf)(&myprintf_buf, "%s ", buf); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | if (!VG_(clo_xml)) |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 356 | count += VG_(printf_to_buf) (&myprintf_buf, "%d%c%c ", VG_(getpid)(), c,c); |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 357 | |
bart | 1a0cb6a | 2008-04-14 16:35:32 +0000 | [diff] [blame^] | 358 | count += VG_(vprintf_to_buf)(&myprintf_buf, format, vargs); |
| 359 | count += VG_(printf_to_buf) (&myprintf_buf, "\n"); |
| 360 | |
| 361 | if (myprintf_buf.n > 0) { |
| 362 | send_bytes_to_logging_sink( myprintf_buf.buf, myprintf_buf.n ); |
| 363 | } |
| 364 | |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 365 | return count; |
| 366 | } |
| 367 | |
| 368 | /* Send a simple single-part message. */ |
| 369 | UInt VG_(message) ( VgMsgKind kind, const HChar* format, ... ) |
| 370 | { |
| 371 | UInt count; |
| 372 | va_list vargs; |
| 373 | va_start(vargs,format); |
| 374 | count = VG_(vmessage) ( kind, format, vargs ); |
| 375 | va_end(vargs); |
| 376 | return count; |
| 377 | } |
| 378 | |
| 379 | /*--------------------------------------------------------------------*/ |
| 380 | /*--- end ---*/ |
| 381 | /*--------------------------------------------------------------------*/ |
| 382 | |