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 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_core_basics.h" |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 32 | #include "pub_core_debuglog.h" |
| 33 | #include "pub_core_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 34 | #include "pub_core_libcassert.h" |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 35 | #include "pub_core_libcfile.h" // For VG_(write)(), VG_(write_socket)() |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 36 | #include "pub_core_libcprint.h" |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 37 | #include "pub_core_libcproc.h" // For VG_(getpid)() |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 38 | #include "pub_core_options.h" |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 39 | #include "valgrind.h" // For RUNNING_ON_VALGRIND |
njn | c44a6c2 | 2005-06-03 13:21:18 +0000 | [diff] [blame] | 40 | |
| 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. */ |
| 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)) { |
| 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 | |
| 72 | typedef 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. |
| 79 | static 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 | |
| 91 | UInt 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 | |
| 108 | UInt 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(). */ |
| 121 | static void add_to_vg_sprintf_buf ( HChar c, void *p ) |
| 122 | { |
| 123 | char **vg_sprintf_ptr = p; |
| 124 | *(*vg_sprintf_ptr)++ = c; |
| 125 | } |
| 126 | |
| 127 | UInt 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 | |
| 141 | UInt 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 | /* --------------------------------------------------------------------- |
| 154 | message() |
| 155 | ------------------------------------------------------------------ */ |
| 156 | |
| 157 | UInt VG_(vmessage) ( VgMsgKind kind, const HChar* format, va_list vargs ) |
| 158 | { |
| 159 | UInt count = 0; |
| 160 | Char c; |
| 161 | const Char* pfx_s; |
| 162 | static const Char pfx[] = ">>>>>>>>>>>>>>>>"; |
| 163 | |
| 164 | switch (kind) { |
| 165 | case Vg_UserMsg: c = '='; break; |
| 166 | case Vg_DebugMsg: c = '-'; break; |
| 167 | case Vg_DebugExtraMsg: c = '+'; break; |
| 168 | case Vg_ClientMsg: c = '*'; break; |
| 169 | default: c = '?'; break; |
| 170 | } |
| 171 | |
| 172 | // The pfx trick prints one or more '>' characters in front of the |
| 173 | // messages when running Valgrind under Valgrind, one per level of |
| 174 | // self-hosting. |
| 175 | pfx_s = &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND], |
| 176 | |
| 177 | // Print the message |
| 178 | count = 0; |
| 179 | |
| 180 | if (!VG_(clo_xml)) |
| 181 | count += VG_(printf) ("%s%c%c", pfx_s, c,c); |
| 182 | |
| 183 | if (VG_(clo_time_stamp)) { |
| 184 | struct timeval tv; |
| 185 | struct tm tm; |
| 186 | |
| 187 | if ( gettimeofday( &tv, NULL ) == 0 && |
| 188 | localtime_r( &tv.tv_sec, &tm ) == &tm ) |
| 189 | { |
| 190 | count += |
| 191 | VG_(printf)( "%04d-%02d-%02d %02d:%02d:%02d.%03d ", |
| 192 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 193 | tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (!VG_(clo_xml)) |
| 198 | count += VG_(printf) ("%d%c%c ", VG_(getpid)(), c,c); |
| 199 | |
| 200 | count += VG_(vprintf)(format, vargs); |
| 201 | count += VG_(printf) ("\n"); |
| 202 | return count; |
| 203 | } |
| 204 | |
| 205 | /* Send a simple single-part message. */ |
| 206 | UInt VG_(message) ( VgMsgKind kind, const HChar* format, ... ) |
| 207 | { |
| 208 | UInt count; |
| 209 | va_list vargs; |
| 210 | va_start(vargs,format); |
| 211 | count = VG_(vmessage) ( kind, format, vargs ); |
| 212 | va_end(vargs); |
| 213 | return count; |
| 214 | } |
| 215 | |
| 216 | /*--------------------------------------------------------------------*/ |
| 217 | /*--- end ---*/ |
| 218 | /*--------------------------------------------------------------------*/ |
| 219 | |