1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 /*--------------------------------------------------------------------*/
4 /*--- Libc printing. m_libcprint.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2013 Julian Seward
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 The GNU General Public License is contained in the file COPYING.
32 #include "pub_core_basics.h"
33 #include "pub_core_vki.h"
34 #include "pub_core_debuglog.h"
35 #include "pub_core_gdbserver.h" // VG_(gdb_printf)
36 #include "pub_core_libcbase.h"
37 #include "pub_core_libcassert.h"
38 #include "pub_core_libcfile.h" // VG_(write)(), VG_(write_socket)()
39 #include "pub_core_libcprint.h"
40 #include "pub_core_libcproc.h" // VG_(getpid)(), VG_(read_millisecond_timer()
41 #include "pub_core_mallocfree.h" // VG_(malloc)
42 #include "pub_core_options.h"
43 #include "pub_core_clreq.h" // For RUNNING_ON_VALGRIND
46 /* ---------------------------------------------------------------------
47 Writing to file or a socket
48 ------------------------------------------------------------------ */
50 /* The destination sinks for normal and XML output. These have their
51 initial values here; they are set to final values by
52 m_main.main_process_cmd_line_options(). See comment at the top of
53 that function for the associated logic.
54 After startup, the gdbserver monitor command might temporarily
55 set the fd of log_output_sink to -2 to indicate that output is
56 to be given to gdb rather than output to the startup fd */
57 OutputSink
VG_(log_output_sink
) = { 2, False
}; /* 2 = stderr */
58 OutputSink
VG_(xml_output_sink
) = { -1, False
}; /* disabled */
60 /* Do the low-level send of a message to the logging sink. */
62 void send_bytes_to_logging_sink ( OutputSink
* sink
, const HChar
* msg
, Int nbytes
)
64 if (sink
->is_socket
) {
65 Int rc
= VG_(write_socket
)( sink
->fd
, msg
, nbytes
);
67 // For example, the listener process died. Switch back to stderr.
68 sink
->is_socket
= False
;
70 VG_(write
)( sink
->fd
, msg
, nbytes
);
73 /* sink->fd could have been set to -1 in the various
74 sys-wrappers for sys_fork, if --child-silent-after-fork=yes
75 is in effect. That is a signal that we should not produce
78 VG_(write
)( sink
->fd
, msg
, nbytes
);
79 else if (sink
->fd
== -2 && nbytes
> 0)
80 /* send to gdb the provided data, which must be
81 a null terminated string with len >= 1 */
82 VG_(gdb_printf
)("%s", msg
);
87 /* ---------------------------------------------------------------------
89 ------------------------------------------------------------------ */
91 /* --------- printf --------- */
101 // Adds a single char to the buffer. When the buffer gets sufficiently
102 // full, we write its contents to the logging sink.
103 static void add_to__printf_buf ( HChar c
, void *p
)
105 printf_buf_t
*b
= (printf_buf_t
*)p
;
107 if (b
->buf_used
> sizeof(b
->buf
) - 2 ) {
108 send_bytes_to_logging_sink( b
->sink
, b
->buf
, b
->buf_used
);
111 b
->buf
[b
->buf_used
++] = c
;
112 b
->buf
[b
->buf_used
] = 0;
113 vg_assert(b
->buf_used
< sizeof(b
->buf
));
116 static UInt
vprintf_to_buf ( printf_buf_t
* b
,
117 const HChar
*format
, va_list vargs
)
120 if (b
->sink
->fd
>= 0 || b
->sink
->fd
== -2) {
121 ret
= VG_(debugLog_vprintf
)
122 ( add_to__printf_buf
, b
, format
, vargs
);
127 static UInt
vprintf_WRK ( OutputSink
* sink
,
128 const HChar
*format
, va_list vargs
)
130 printf_buf_t myprintf_buf
133 = vprintf_to_buf(&myprintf_buf
, format
, vargs
);
134 // Write out any chars left in the buffer.
135 if (myprintf_buf
.buf_used
> 0) {
136 send_bytes_to_logging_sink( myprintf_buf
.sink
,
138 myprintf_buf
.buf_used
);
143 UInt
VG_(vprintf
) ( const HChar
*format
, va_list vargs
)
145 return vprintf_WRK( &VG_(log_output_sink
), format
, vargs
);
148 UInt
VG_(printf
) ( const HChar
*format
, ... )
152 va_start(vargs
, format
);
153 ret
= VG_(vprintf
)(format
, vargs
);
158 UInt
VG_(vprintf_xml
) ( const HChar
*format
, va_list vargs
)
160 return vprintf_WRK( &VG_(xml_output_sink
), format
, vargs
);
163 UInt
VG_(printf_xml
) ( const HChar
*format
, ... )
167 va_start(vargs
, format
);
168 ret
= VG_(vprintf_xml
)(format
, vargs
);
173 static UInt
emit_WRK ( const HChar
* format
, va_list vargs
)
176 return VG_(vprintf_xml
)(format
, vargs
);
177 } else if (VG_(log_output_sink
).fd
== -2) {
178 return VG_(vprintf
) (format
, vargs
);
180 return VG_(vmessage
)(Vg_UserMsg
, format
, vargs
);
183 UInt
VG_(emit
) ( const HChar
* format
, ... )
187 va_start(vargs
, format
);
188 ret
= emit_WRK(format
, vargs
);
193 /* --------- sprintf --------- */
195 /* If we had an explicit buf structure here, it would contain only one
196 field, indicating where the next char is to go. So use p directly
197 for that, rather than having it be a pointer to a structure. */
199 static void add_to__sprintf_buf ( HChar c
, void *p
)
205 UInt
VG_(vsprintf
) ( HChar
* buf
, const HChar
*format
, va_list vargs
)
208 HChar
* sprintf_ptr
= buf
;
210 ret
= VG_(debugLog_vprintf
)
211 ( add_to__sprintf_buf
, &sprintf_ptr
, format
, vargs
);
212 add_to__sprintf_buf('\0', &sprintf_ptr
);
214 vg_assert(VG_(strlen
)(buf
) == ret
);
219 UInt
VG_(sprintf
) ( HChar
* buf
, const HChar
*format
, ... )
223 va_start(vargs
,format
);
224 ret
= VG_(vsprintf
)(buf
, format
, vargs
);
230 /* --------- snprintf --------- */
232 /* The return value of VG_(snprintf) and VG_(vsnprintf) differs from
233 what is defined in C99. Let S be the size of the buffer as given in
236 R < S: The output string was successfully written to the buffer.
237 It is null-terminated and R == strlen( output string )
238 R == S: The supplied buffer was too small to hold the output string.
239 The first S-1 characters of the output string were written
240 to the buffer followed by the terminating null character.
251 static void add_to__snprintf_buf ( HChar c
, void* p
)
253 snprintf_buf_t
* b
= p
;
254 if (b
->buf_size
> 0 && b
->buf_used
< b
->buf_size
) {
255 b
->buf
[b
->buf_used
++] = c
;
256 if (b
->buf_used
< b
->buf_size
)
257 b
->buf
[b
->buf_used
] = 0;
259 b
->buf
[b
->buf_size
-1] = 0; /* pre: b->buf_size > 0 */
263 UInt
VG_(vsnprintf
) ( HChar
* buf
, Int size
, const HChar
*format
, va_list vargs
)
267 b
.buf_size
= size
< 0 ? 0 : size
;
270 b
.buf
[0] = 0; // ensure to null terminate buf if empty format
271 (void) VG_(debugLog_vprintf
)
272 ( add_to__snprintf_buf
, &b
, format
, vargs
);
277 UInt
VG_(snprintf
) ( HChar
* buf
, Int size
, const HChar
*format
, ... )
281 va_start(vargs
,format
);
282 ret
= VG_(vsnprintf
)(buf
, size
, format
, vargs
);
288 /* --------- vcbprintf --------- */
290 void VG_(vcbprintf
)( void(*char_sink
)(HChar
, void* opaque
),
292 const HChar
* format
, va_list vargs
)
294 (void) VG_(debugLog_vprintf
)
295 ( char_sink
, opaque
, format
, vargs
);
299 /* --------- fprintf ---------- */
301 /* This is like [v]fprintf, except it writes to a file handle using
304 #define VGFILE_BUFSIZE 8192
307 HChar buf
[VGFILE_BUFSIZE
];
308 UInt num_chars
; // number of characters in buf
309 Int fd
; // file descriptor to write to
313 static void add_to__vgfile ( HChar c
, void *p
)
317 fp
->buf
[fp
->num_chars
++] = c
;
319 if (fp
->num_chars
== VGFILE_BUFSIZE
) {
320 VG_(write
)(fp
->fd
, fp
->buf
, fp
->num_chars
);
325 VgFile
*VG_(fopen
)(const HChar
*name
, Int flags
, Int mode
)
327 SysRes res
= VG_(open
)(name
, flags
, mode
);
332 VgFile
*fp
= VG_(malloc
)("fopen", sizeof(VgFile
));
334 fp
->fd
= sr_Res(res
);
341 UInt
VG_(vfprintf
) ( VgFile
*fp
, const HChar
*format
, va_list vargs
)
343 return VG_(debugLog_vprintf
)(add_to__vgfile
, fp
, format
, vargs
);
346 UInt
VG_(fprintf
) ( VgFile
*fp
, const HChar
*format
, ... )
350 va_start(vargs
,format
);
351 ret
= VG_(vfprintf
)(fp
, format
, vargs
);
356 void VG_(fclose
)( VgFile
*fp
)
360 VG_(write
)(fp
->fd
, fp
->buf
, fp
->num_chars
);
365 /* ---------------------------------------------------------------------
367 ------------------------------------------------------------------ */
369 // Percentify n/m with d decimal places. Includes the '%' symbol at the end.
370 // Right justifies in 'buf'.
371 void VG_(percentify
)(ULong n
, ULong m
, UInt d
, Int n_buf
, HChar buf
[])
375 HChar fmt
[32]; // large enough
378 // Have to generate the format string in order to be flexible about
379 // the width of the field.
380 VG_(sprintf
)(fmt
, "%%%ds", n_buf
);
381 // fmt is now "%<n_buf>s" where <d> is 1,2,3...
382 VG_(sprintf
)(buf
, fmt
, "--%");
389 VG_(sprintf
)(buf
, "%llu%%", p1
); // FIXME: unsafe
394 case 1: ex
= 10; break;
395 case 2: ex
= 100; break;
396 case 3: ex
= 1000; break;
397 default: VG_(core_panic
)("Currently can only handle 3 decimal places");
399 p2
= ((100*n
*ex
) / m
) % ex
;
400 // Have to generate the format string in order to be flexible about
401 // the width of the post-decimal-point part.
402 VG_(sprintf
)(fmt
, "%%llu.%%0%ullu%%%%", d
);
403 // fmt is now "%llu.%0<d>llu%%" where <d> is 1,2,3...
404 VG_(sprintf
)(buf
, fmt
, p1
, p2
); // FIXME: unsafe
407 len
= VG_(strlen
)(buf
);
409 if (space
< 0) space
= 0; /* Allow for v. small field_width */
412 /* Right justify in field */
413 for ( ; i
>= 0; i
--) buf
[i
+ space
] = buf
[i
];
414 for (i
= 0; i
< space
; i
++) buf
[i
] = ' ';
418 /* ---------------------------------------------------------------------
419 elapsed_wallclock_time()
420 ------------------------------------------------------------------ */
422 /* Get the elapsed wallclock time since startup into buf, which must
423 16 chars long. This is unchecked. It also relies on the
424 millisecond timer having been set to zero by an initial read in
425 m_main during startup. */
427 void VG_(elapsed_wallclock_time
) ( /*OUT*/HChar
* buf
, SizeT bufsize
)
429 UInt t
, ms
, s
, mins
, hours
, days
;
431 vg_assert(bufsize
> 20);
433 t
= VG_(read_millisecond_timer
)(); /* milliseconds */
436 t
/= 1000; /* now in seconds */
439 t
/= 60; /* now in minutes */
442 t
/= 60; /* now in hours */
445 t
/= 24; /* now in days */
449 VG_(sprintf
)(buf
, "%02u:%02u:%02u:%02u.%03u ", days
, hours
, mins
, s
, ms
);
453 /* ---------------------------------------------------------------------
455 ------------------------------------------------------------------ */
457 /* A buffer for accumulating VG_(message) style output. This is
458 pretty much the same as VG_(printf)'s scheme, with two differences:
460 * The message buffer persists between calls, so that multiple
461 calls to VG_(message) can build up output.
463 * Whenever the first character on a line is emitted, the
464 ==PID== style preamble is stuffed in before it.
470 Bool atLeft
; /* notionally, is the next char position at the
472 /* Current message kind - changes from call to call */
479 static vmessage_buf_t vmessage_buf
480 = { "", 0, True
, Vg_UserMsg
, &VG_(log_output_sink
) };
483 // Adds a single char to the buffer. We aim to have at least 128
484 // bytes free in the buffer, so that it's always possible to emit
485 // the preamble into the buffer if c happens to be the character
486 // following a \n. When the buffer gets too full, we write its
487 // contents to the logging sink.
488 static void add_to__vmessage_buf ( HChar c
, void *p
)
491 vmessage_buf_t
* b
= (vmessage_buf_t
*)p
;
493 vg_assert(b
->buf_used
>= 0 && b
->buf_used
< sizeof(b
->buf
)-128);
495 if (UNLIKELY(b
->atLeft
)) {
500 // Print one '>' in front of the messages for each level of
501 // self-hosting being performed.
502 // Do not print such '>' if sim hint "no-inner-prefix" given
503 // (useful to run regression tests in an outer/inner setup
504 // and avoid the diff failing due to these unexpected '>').
505 depth
= RUNNING_ON_VALGRIND
;
507 && !SimHintiS(SimHint_no_inner_prefix
, VG_(clo_sim_hints
))) {
510 for (i
= 0; i
< depth
; i
++) {
511 b
->buf
[b
->buf_used
++] = '>';
515 if (Vg_FailMsg
== b
->kind
) {
516 // "valgrind: " prefix.
517 b
->buf
[b
->buf_used
++] = 'v';
518 b
->buf
[b
->buf_used
++] = 'a';
519 b
->buf
[b
->buf_used
++] = 'l';
520 b
->buf
[b
->buf_used
++] = 'g';
521 b
->buf
[b
->buf_used
++] = 'r';
522 b
->buf
[b
->buf_used
++] = 'i';
523 b
->buf
[b
->buf_used
++] = 'n';
524 b
->buf
[b
->buf_used
++] = 'd';
525 b
->buf
[b
->buf_used
++] = ':';
526 b
->buf
[b
->buf_used
++] = ' ';
529 case Vg_UserMsg
: ch
= '='; break;
530 case Vg_DebugMsg
: ch
= '-'; break;
531 case Vg_ClientMsg
: ch
= '*'; break;
532 default: ch
= '?'; break;
535 b
->buf
[b
->buf_used
++] = ch
;
536 b
->buf
[b
->buf_used
++] = ch
;
538 if (VG_(clo_time_stamp
)) {
539 VG_(elapsed_wallclock_time
)(tmp
, sizeof tmp
);
540 for (i
= 0; tmp
[i
]; i
++)
541 b
->buf
[b
->buf_used
++] = tmp
[i
];
544 VG_(sprintf
)(tmp
, "%d", VG_(getpid
)());
545 tmp
[sizeof(tmp
)-1] = 0;
546 for (i
= 0; tmp
[i
]; i
++)
547 b
->buf
[b
->buf_used
++] = tmp
[i
];
549 b
->buf
[b
->buf_used
++] = ch
;
550 b
->buf
[b
->buf_used
++] = ch
;
551 b
->buf
[b
->buf_used
++] = ' ';
554 /* We can't possibly have stuffed 96 chars in merely as a result
555 of making the preamble (can we?) */
556 vg_assert(b
->buf_used
< sizeof(b
->buf
)-32);
559 b
->buf
[b
->buf_used
++] = c
;
560 b
->buf
[b
->buf_used
] = 0;
562 if (b
->buf_used
>= sizeof(b
->buf
) - 128) {
563 send_bytes_to_logging_sink( b
->sink
, b
->buf
, b
->buf_used
);
567 b
->atLeft
= c
== '\n';
571 UInt
VG_(vmessage
) ( VgMsgKind kind
, const HChar
* format
, va_list vargs
)
575 /* Note (carefully) that the buf persists from call to call, unlike
576 with the other printf variants in earlier parts of this file. */
577 vmessage_buf_t
* b
= &vmessage_buf
; /* shorthand for convenience */
579 /* We have to set this each call, so that the correct flavour
580 of preamble is emitted at each \n. */
583 ret
= VG_(debugLog_vprintf
) ( add_to__vmessage_buf
,
586 /* If the message finished exactly with a \n, then flush it at this
587 point. If not, assume more bits of the same line will turn up
588 in later messages, so don't bother to flush it right now. */
590 if (b
->atLeft
&& b
->buf_used
> 0) {
591 send_bytes_to_logging_sink( b
->sink
, b
->buf
, b
->buf_used
);
598 /* Send a simple single-part message. */
599 UInt
VG_(message
) ( VgMsgKind kind
, const HChar
* format
, ... )
603 va_start(vargs
,format
);
604 count
= VG_(vmessage
) ( kind
, format
, vargs
);
609 static void revert_to_stderr ( void )
611 VG_(log_output_sink
).fd
= 2; /* stderr */
612 VG_(log_output_sink
).is_socket
= False
;
615 /* VG_(message) variants with hardwired first argument. */
617 UInt
VG_(fmsg
) ( const HChar
* format
, ... )
621 va_start(vargs
,format
);
622 count
= VG_(vmessage
) ( Vg_FailMsg
, format
, vargs
);
627 void VG_(fmsg_bad_option
) ( const HChar
* opt
, const HChar
* format
, ... )
630 va_start(vargs
,format
);
632 VG_(message
) (Vg_FailMsg
, "Bad option: %s\n", opt
);
633 VG_(vmessage
)(Vg_FailMsg
, format
, vargs
);
634 VG_(message
) (Vg_FailMsg
, "Use --help for more information or consult the user manual.\n");
639 UInt
VG_(umsg
) ( const HChar
* format
, ... )
643 va_start(vargs
,format
);
644 count
= VG_(vmessage
) ( Vg_UserMsg
, format
, vargs
);
649 UInt
VG_(dmsg
) ( const HChar
* format
, ... )
653 va_start(vargs
,format
);
654 count
= VG_(vmessage
) ( Vg_DebugMsg
, format
, vargs
);
659 /* Flush any output that has accumulated in vmessage_buf as a
660 result of previous calls to VG_(message) et al. */
661 void VG_(message_flush
) ( void )
663 vmessage_buf_t
* b
= &vmessage_buf
;
664 send_bytes_to_logging_sink( b
->sink
, b
->buf
, b
->buf_used
);
668 __attribute__((noreturn
))
669 void VG_(err_missing_prog
) ( void )
672 VG_(fmsg
)("no program specified\n");
673 VG_(fmsg
)("Use --help for more information.\n");
677 __attribute__((noreturn
))
678 void VG_(err_config_error
) ( const HChar
* format
, ... )
681 va_start(vargs
,format
);
683 VG_(message
) (Vg_FailMsg
, "Startup or configuration error:\n ");
684 VG_(vmessage
)(Vg_FailMsg
, format
, vargs
);
685 VG_(message
) (Vg_FailMsg
, "Unable to start up properly. Giving up.\n");
691 /*--------------------------------------------------------------------*/
693 /*--------------------------------------------------------------------*/