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 | |
| 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 | |
| 31 | #include "core.h" |
| 32 | #include "pub_core_debuglog.h" |
| 33 | #include "pub_core_libcbase.h" |
| 34 | #include "pub_core_libcprint.h" |
| 35 | #include "pub_core_options.h" |
| 36 | #include "valgrind.h" // for RUNNING_ON_VALGRIND |
| 37 | |
| 38 | #include <time.h> |
| 39 | #include <sys/time.h> |
| 40 | |
| 41 | /* --------------------------------------------------------------------- |
| 42 | Writing to file or a socket |
| 43 | ------------------------------------------------------------------ */ |
| 44 | |
| 45 | /* Tell the logging mechanism whether we are logging to a file |
| 46 | descriptor or a socket descriptor. */ |
| 47 | Bool VG_(logging_to_socket) = False; |
| 48 | |
| 49 | /* Do the low-level send of a message to the logging sink. */ |
| 50 | static void send_bytes_to_logging_sink ( Char* msg, Int nbytes ) |
| 51 | { |
| 52 | if (!VG_(logging_to_socket)) { |
| 53 | VG_(write)( VG_(clo_log_fd), msg, nbytes ); |
| 54 | } else { |
| 55 | Int rc = VG_(write_socket)( VG_(clo_log_fd), msg, nbytes ); |
| 56 | if (rc == -1) { |
| 57 | // For example, the listener process died. Switch back to stderr. |
| 58 | VG_(logging_to_socket) = False; |
| 59 | VG_(clo_log_fd) = 2; |
| 60 | VG_(write)( VG_(clo_log_fd), msg, nbytes ); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /* --------------------------------------------------------------------- |
| 66 | printf() and friends |
| 67 | ------------------------------------------------------------------ */ |
| 68 | |
| 69 | typedef struct { |
| 70 | char buf[100]; |
| 71 | int n; |
| 72 | } printf_buf; |
| 73 | |
| 74 | // Adds a single char to the buffer. When the buffer gets sufficiently |
| 75 | // full, we write its contents to the logging sink. |
| 76 | static void add_to_myprintf_buf ( HChar c, void *p ) |
| 77 | { |
| 78 | printf_buf *myprintf_buf = (printf_buf *)p; |
| 79 | |
| 80 | if (myprintf_buf->n >= 100-10 /*paranoia*/ ) { |
| 81 | send_bytes_to_logging_sink( myprintf_buf->buf, myprintf_buf->n ); |
| 82 | myprintf_buf->n = 0; |
| 83 | } |
| 84 | myprintf_buf->buf[myprintf_buf->n++] = c; |
| 85 | myprintf_buf->buf[myprintf_buf->n] = 0; |
| 86 | } |
| 87 | |
| 88 | UInt VG_(vprintf) ( const HChar *format, va_list vargs ) |
| 89 | { |
| 90 | UInt ret = 0; |
| 91 | printf_buf myprintf_buf = {"",0}; |
| 92 | |
| 93 | if (VG_(clo_log_fd) >= 0) { |
| 94 | ret = VG_(debugLog_vprintf) |
| 95 | ( add_to_myprintf_buf, &myprintf_buf, format, vargs ); |
| 96 | |
| 97 | // Write out any chars left in the buffer. |
| 98 | if (myprintf_buf.n > 0) { |
| 99 | send_bytes_to_logging_sink( myprintf_buf.buf, myprintf_buf.n ); |
| 100 | } |
| 101 | } |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | UInt VG_(printf) ( const HChar *format, ... ) |
| 106 | { |
| 107 | UInt ret; |
| 108 | va_list vargs; |
| 109 | |
| 110 | va_start(vargs, format); |
| 111 | ret = VG_(vprintf)(format, vargs); |
| 112 | va_end(vargs); |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | /* A general replacement for sprintf(). */ |
| 118 | static void add_to_vg_sprintf_buf ( HChar c, void *p ) |
| 119 | { |
| 120 | char **vg_sprintf_ptr = p; |
| 121 | *(*vg_sprintf_ptr)++ = c; |
| 122 | } |
| 123 | |
| 124 | UInt VG_(vsprintf) ( Char* buf, const HChar *format, va_list vargs ) |
| 125 | { |
| 126 | Int ret; |
| 127 | Char *vg_sprintf_ptr = buf; |
| 128 | |
| 129 | ret = VG_(debugLog_vprintf) |
| 130 | ( add_to_vg_sprintf_buf, &vg_sprintf_ptr, format, vargs ); |
| 131 | add_to_vg_sprintf_buf('\0', &vg_sprintf_ptr); |
| 132 | |
| 133 | vg_assert(VG_(strlen)(buf) == ret); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | UInt VG_(sprintf) ( Char* buf, const HChar *format, ... ) |
| 139 | { |
| 140 | UInt ret; |
| 141 | va_list vargs; |
| 142 | |
| 143 | va_start(vargs,format); |
| 144 | ret = VG_(vsprintf)(buf, format, vargs); |
| 145 | va_end(vargs); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* --------------------------------------------------------------------- |
| 151 | message() |
| 152 | ------------------------------------------------------------------ */ |
| 153 | |
| 154 | UInt VG_(vmessage) ( VgMsgKind kind, const HChar* format, va_list vargs ) |
| 155 | { |
| 156 | UInt count = 0; |
| 157 | Char c; |
| 158 | const Char* pfx_s; |
| 159 | static const Char pfx[] = ">>>>>>>>>>>>>>>>"; |
| 160 | |
| 161 | switch (kind) { |
| 162 | case Vg_UserMsg: c = '='; break; |
| 163 | case Vg_DebugMsg: c = '-'; break; |
| 164 | case Vg_DebugExtraMsg: c = '+'; break; |
| 165 | case Vg_ClientMsg: c = '*'; break; |
| 166 | default: c = '?'; break; |
| 167 | } |
| 168 | |
| 169 | // The pfx trick prints one or more '>' characters in front of the |
| 170 | // messages when running Valgrind under Valgrind, one per level of |
| 171 | // self-hosting. |
| 172 | pfx_s = &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND], |
| 173 | |
| 174 | // Print the message |
| 175 | count = 0; |
| 176 | |
| 177 | if (!VG_(clo_xml)) |
| 178 | count += VG_(printf) ("%s%c%c", pfx_s, c,c); |
| 179 | |
| 180 | if (VG_(clo_time_stamp)) { |
| 181 | struct timeval tv; |
| 182 | struct tm tm; |
| 183 | |
| 184 | if ( gettimeofday( &tv, NULL ) == 0 && |
| 185 | localtime_r( &tv.tv_sec, &tm ) == &tm ) |
| 186 | { |
| 187 | count += |
| 188 | VG_(printf)( "%04d-%02d-%02d %02d:%02d:%02d.%03d ", |
| 189 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 190 | tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 ); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | if (!VG_(clo_xml)) |
| 195 | count += VG_(printf) ("%d%c%c ", VG_(getpid)(), c,c); |
| 196 | |
| 197 | count += VG_(vprintf)(format, vargs); |
| 198 | count += VG_(printf) ("\n"); |
| 199 | return count; |
| 200 | } |
| 201 | |
| 202 | /* Send a simple single-part message. */ |
| 203 | UInt VG_(message) ( VgMsgKind kind, const HChar* format, ... ) |
| 204 | { |
| 205 | UInt count; |
| 206 | va_list vargs; |
| 207 | va_start(vargs,format); |
| 208 | count = VG_(vmessage) ( kind, format, vargs ); |
| 209 | va_end(vargs); |
| 210 | return count; |
| 211 | } |
| 212 | |
| 213 | /*--------------------------------------------------------------------*/ |
| 214 | /*--- end ---*/ |
| 215 | /*--------------------------------------------------------------------*/ |
| 216 | |