blob: c51bd1d6be14cb323434f3a18404d6a44a963e69 [file] [log] [blame]
njnc44a6c22005-06-03 13:21:18 +00001
2/*--------------------------------------------------------------------*/
3/*--- Libc printing. m_libcprint.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
10 Copyright (C) 2000-2005 Julian Seward
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
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
njnc44a6c22005-06-03 13:21:18 +000032#include "pub_core_debuglog.h"
33#include "pub_core_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000034#include "pub_core_libcassert.h"
njn24a6efb2005-06-20 03:36:51 +000035#include "pub_core_libcfile.h" // For VG_(write)(), VG_(write_socket)()
njnc44a6c22005-06-03 13:21:18 +000036#include "pub_core_libcprint.h"
njn24a6efb2005-06-20 03:36:51 +000037#include "pub_core_libcproc.h" // For VG_(getpid)()
njnc44a6c22005-06-03 13:21:18 +000038#include "pub_core_options.h"
njn24a6efb2005-06-20 03:36:51 +000039#include "valgrind.h" // For RUNNING_ON_VALGRIND
njnc44a6c22005-06-03 13:21:18 +000040
41#include <time.h>
42#include <sys/time.h>
43
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. */
50Bool VG_(logging_to_socket) = False;
51
52/* Do the low-level send of a message to the logging sink. */
53static void send_bytes_to_logging_sink ( Char* msg, Int nbytes )
54{
55 if (!VG_(logging_to_socket)) {
56 VG_(write)( VG_(clo_log_fd), msg, nbytes );
57 } else {
58 Int rc = VG_(write_socket)( VG_(clo_log_fd), msg, nbytes );
59 if (rc == -1) {
60 // For example, the listener process died. Switch back to stderr.
61 VG_(logging_to_socket) = False;
62 VG_(clo_log_fd) = 2;
63 VG_(write)( VG_(clo_log_fd), msg, nbytes );
64 }
65 }
66}
67
68/* ---------------------------------------------------------------------
69 printf() and friends
70 ------------------------------------------------------------------ */
71
72typedef struct {
73 char buf[100];
74 int n;
75} printf_buf;
76
77// Adds a single char to the buffer. When the buffer gets sufficiently
78// full, we write its contents to the logging sink.
79static void add_to_myprintf_buf ( HChar c, void *p )
80{
81 printf_buf *myprintf_buf = (printf_buf *)p;
82
83 if (myprintf_buf->n >= 100-10 /*paranoia*/ ) {
84 send_bytes_to_logging_sink( myprintf_buf->buf, myprintf_buf->n );
85 myprintf_buf->n = 0;
86 }
87 myprintf_buf->buf[myprintf_buf->n++] = c;
88 myprintf_buf->buf[myprintf_buf->n] = 0;
89}
90
91UInt VG_(vprintf) ( const HChar *format, va_list vargs )
92{
93 UInt ret = 0;
94 printf_buf myprintf_buf = {"",0};
95
96 if (VG_(clo_log_fd) >= 0) {
97 ret = VG_(debugLog_vprintf)
98 ( add_to_myprintf_buf, &myprintf_buf, format, vargs );
99
100 // Write out any chars left in the buffer.
101 if (myprintf_buf.n > 0) {
102 send_bytes_to_logging_sink( myprintf_buf.buf, myprintf_buf.n );
103 }
104 }
105 return ret;
106}
107
108UInt VG_(printf) ( const HChar *format, ... )
109{
110 UInt ret;
111 va_list vargs;
112
113 va_start(vargs, format);
114 ret = VG_(vprintf)(format, vargs);
115 va_end(vargs);
116
117 return ret;
118}
119
120/* A general replacement for sprintf(). */
121static void add_to_vg_sprintf_buf ( HChar c, void *p )
122{
123 char **vg_sprintf_ptr = p;
124 *(*vg_sprintf_ptr)++ = c;
125}
126
127UInt VG_(vsprintf) ( Char* buf, const HChar *format, va_list vargs )
128{
129 Int ret;
130 Char *vg_sprintf_ptr = buf;
131
132 ret = VG_(debugLog_vprintf)
133 ( add_to_vg_sprintf_buf, &vg_sprintf_ptr, format, vargs );
134 add_to_vg_sprintf_buf('\0', &vg_sprintf_ptr);
135
136 vg_assert(VG_(strlen)(buf) == ret);
137
138 return ret;
139}
140
141UInt VG_(sprintf) ( Char* buf, const HChar *format, ... )
142{
143 UInt ret;
144 va_list vargs;
145
146 va_start(vargs,format);
147 ret = VG_(vsprintf)(buf, format, vargs);
148 va_end(vargs);
149
150 return ret;
151}
152
153/* ---------------------------------------------------------------------
njn856c54e2005-06-26 18:43:40 +0000154 percentify()
155 ------------------------------------------------------------------ */
156
157// Percentify n/m with p decimal places. Includes the '%' symbol at the end.
158void VG_(percentify)(UInt n, UInt m, UInt d, Int n_buf, char buf[])
159{
160 Int i, len, space;
161
162 ULong p1 = (100*n) / m;
163
164 if (d == 0) {
165 VG_(sprintf)(buf, "%lld%%", p1);
166 } else {
167 ULong p2;
168 UInt ex;
169 Char fmt[32];
170 switch (d) {
171 case 1: ex = 10; break;
172 case 2: ex = 100; break;
173 case 3: ex = 1000; break;
174 default: VG_(tool_panic)("Currently can only handle 3 decimal places");
175 }
176 p2 = ((100*n*ex) / m) % ex;
177 // Have to generate the format string in order to be flexible about
178 // the width of the post-decimal-point part.
179 VG_(sprintf)(fmt, "%%lld.%%0%dlld%%%%", d);
180 // fmt is now "%lld.%0<d>lld%%" where <d> is 1,2,3...
181 VG_(sprintf)(buf, fmt, p1, p2);
182 }
183
184 len = VG_(strlen)(buf);
185 space = n_buf - len;
186 if (space < 0) space = 0; /* Allow for v. small field_width */
187 i = len;
188
189 /* Right justify in field */
190 for ( ; i >= 0; i--) buf[i + space] = buf[i];
191 for (i = 0; i < space; i++) buf[i] = ' ';
192}
193
194/* ---------------------------------------------------------------------
njnc44a6c22005-06-03 13:21:18 +0000195 message()
196 ------------------------------------------------------------------ */
197
198UInt VG_(vmessage) ( VgMsgKind kind, const HChar* format, va_list vargs )
199{
200 UInt count = 0;
201 Char c;
202 const Char* pfx_s;
203 static const Char pfx[] = ">>>>>>>>>>>>>>>>";
204
205 switch (kind) {
206 case Vg_UserMsg: c = '='; break;
207 case Vg_DebugMsg: c = '-'; break;
208 case Vg_DebugExtraMsg: c = '+'; break;
209 case Vg_ClientMsg: c = '*'; break;
210 default: c = '?'; break;
211 }
212
213 // The pfx trick prints one or more '>' characters in front of the
214 // messages when running Valgrind under Valgrind, one per level of
215 // self-hosting.
216 pfx_s = &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND],
217
218 // Print the message
219 count = 0;
220
221 if (!VG_(clo_xml))
222 count += VG_(printf) ("%s%c%c", pfx_s, c,c);
223
224 if (VG_(clo_time_stamp)) {
225 struct timeval tv;
226 struct tm tm;
227
228 if ( gettimeofday( &tv, NULL ) == 0 &&
229 localtime_r( &tv.tv_sec, &tm ) == &tm )
230 {
231 count +=
232 VG_(printf)( "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
233 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
234 tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 );
235 }
236 }
237
238 if (!VG_(clo_xml))
239 count += VG_(printf) ("%d%c%c ", VG_(getpid)(), c,c);
240
241 count += VG_(vprintf)(format, vargs);
242 count += VG_(printf) ("\n");
243 return count;
244}
245
246/* Send a simple single-part message. */
247UInt VG_(message) ( VgMsgKind kind, const HChar* format, ... )
248{
249 UInt count;
250 va_list vargs;
251 va_start(vargs,format);
252 count = VG_(vmessage) ( kind, format, vargs );
253 va_end(vargs);
254 return count;
255}
256
257/*--------------------------------------------------------------------*/
258/*--- end ---*/
259/*--------------------------------------------------------------------*/
260