From: Nicholas Nethercote Date: Thu, 12 May 2005 03:51:15 +0000 (+0000) Subject: Cleaned up vg_messages.c and related printf stuff. vg_messages.c is now a X-Git-Tag: svn/VALGRIND_3_0_0~648 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a653c5da3c663fb693208c75a53b6e2b635b083e;p=thirdparty%2Fvalgrind.git Cleaned up vg_messages.c and related printf stuff. vg_messages.c is now a layer above the printf stuff in vg_mylibc.c, which is layered over m_debuglog. This makes the module interfaces neater, more consistent, and cuts 40 lines of code. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3670 --- diff --git a/coregrind/core.h b/coregrind/core.h index 8e035d73e7..82076e1ed0 100644 --- a/coregrind/core.h +++ b/coregrind/core.h @@ -635,14 +635,6 @@ extern void VG_(env_remove_valgrind_env_stuff) ( Char** env ); extern void VG_(nanosleep)(struct vki_timespec *); -/* --------------------------------------------------------------------- - Exports of vg_message.c - ------------------------------------------------------------------ */ - -/* Low-level -- send bytes directly to the message sink. Do not - use. */ -extern void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes ); - /* --------------------------------------------------------------------- Exports of vg_translate.c ------------------------------------------------------------------ */ diff --git a/coregrind/vg_messages.c b/coregrind/vg_messages.c index 1314b3b7a8..59825f5eb9 100644 --- a/coregrind/vg_messages.c +++ b/coregrind/vg_messages.c @@ -29,66 +29,18 @@ The GNU General Public License is contained in the file COPYING. */ - #include "core.h" -#include "pub_core_debuglog.h" /* VG_(debugLog_vprintf) */ #include #include -/* Size of a buffer used for creating messages. */ -#define M_MSGBUF 10000 - -static char mbuf[M_MSGBUF]; -static int n_mbuf; - -static void add_to_buf ( HChar c, void *p ) -{ - if (n_mbuf >= (M_MSGBUF-1)) return; - mbuf[n_mbuf++] = c; - mbuf[n_mbuf] = 0; -} - -static void add_timestamp ( Char *buf ) -{ - struct timeval tv; - struct tm tm; - - if ( gettimeofday( &tv, NULL ) == 0 && - localtime_r( &tv.tv_sec, &tm ) == &tm ) { - VG_(sprintf)( buf, "%04d-%02d-%02d %02d:%02d:%02d.%03d ", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 ); - } - else { - VG_(strcpy)( buf, "" ); - } - - return; -} - -static int add_to_msg ( const Char *format, ... ) +UInt VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs ) { - int count; - va_list vargs; - va_start(vargs,format); - count = VG_(debugLog_vprintf) ( add_to_buf, NULL, format, vargs ); - va_end(vargs); - return count; -} - -static int start_msg ( VgMsgKind kind ) -{ - Char ts[32]; - Char c; + UInt count = 0; + Char c; + const Char* pfx_s; static const Char pfx[] = ">>>>>>>>>>>>>>>>"; - n_mbuf = 0; - mbuf[n_mbuf] = 0; - if (VG_(clo_time_stamp)) - add_timestamp(ts); - else - VG_(strcpy)(ts, ""); switch (kind) { case Vg_UserMsg: c = '='; break; case Vg_DebugMsg: c = '-'; break; @@ -96,39 +48,40 @@ static int start_msg ( VgMsgKind kind ) case Vg_ClientMsg: c = '*'; break; default: c = '?'; break; } + // The pfx trick prints one or more '>' characters in front of the // messages when running Valgrind under Valgrind, one per level of // self-hosting. - return add_to_msg( "%s%c%c%s%d%c%c ", - &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND], - c,c, ts, VG_(getpid)(), c,c ); -} - -static -int end_msg ( void ) -{ - int count = 0; - if (VG_(clo_log_fd) >= 0) { - add_to_buf('\n',0); - VG_(send_bytes_to_logging_sink) ( mbuf, VG_(strlen)(mbuf) ); - count = 1; + pfx_s = &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND], + + // Print the message + count = 0; + count += VG_(printf) ("%s%c%c", pfx_s, c,c); + + if (VG_(clo_time_stamp)) { + struct timeval tv; + struct tm tm; + + if ( gettimeofday( &tv, NULL ) == 0 && + localtime_r( &tv.tv_sec, &tm ) == &tm ) + { + count += + VG_(printf)( "%04d-%02d-%02d %02d:%02d:%02d.%03d ", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 ); + } } - return count; -} -int VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs ) -{ - int count; - count = start_msg ( kind ); - count += VG_(debugLog_vprintf) ( add_to_buf, NULL, format, vargs ); - count += end_msg(); + count += VG_(printf) ("%d%c%c ", VG_(getpid)(), c,c); + count += VG_(vprintf)(format, vargs); + count += VG_(printf) ("\n"); return count; } /* Send a simple single-part message. */ -int VG_(message) ( VgMsgKind kind, const Char* format, ... ) +UInt VG_(message) ( VgMsgKind kind, const Char* format, ... ) { - int count; + UInt count; va_list vargs; va_start(vargs,format); count = VG_(vmessage) ( kind, format, vargs ); @@ -136,25 +89,6 @@ int VG_(message) ( VgMsgKind kind, const Char* format, ... ) return count; } -/* Do the low-level send of a message to the logging sink. */ -void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes ) -{ - Int rc; - if (VG_(logging_to_filedes)) { - VG_(write)( VG_(clo_log_fd), msg, nbytes ); - } else { - rc = VG_(write_socket)( VG_(clo_log_fd), msg, nbytes ); - if (rc == -1) { - /* for example, the listener process died. Switch back to - stderr. */ - VG_(logging_to_filedes) = True; - VG_(clo_log_to) = VgLogTo_Fd; - VG_(clo_log_fd) = 2; - VG_(write)( VG_(clo_log_fd), msg, nbytes ); - } - } -} - /*--------------------------------------------------------------------*/ /*--- end ---*/ /*--------------------------------------------------------------------*/ diff --git a/coregrind/vg_mylibc.c b/coregrind/vg_mylibc.c index 750d59ba05..f9cf01758c 100644 --- a/coregrind/vg_mylibc.c +++ b/coregrind/vg_mylibc.c @@ -390,41 +390,67 @@ Int VG_(poll)( struct vki_pollfd *ufds, UInt nfds, Int timeout) debugging info should be sent via here. The official route is to to use vg_message(). This interface is deprecated. */ + +/* Do the low-level send of a message to the logging sink. */ +static void send_bytes_to_logging_sink ( Char* msg, Int nbytes ) +{ + if (VG_(logging_to_filedes)) { + VG_(write)( VG_(clo_log_fd), msg, nbytes ); + } else { + Int rc = VG_(write_socket)( VG_(clo_log_fd), msg, nbytes ); + if (rc == -1) { + // For example, the listener process died. Switch back to stderr. + VG_(logging_to_filedes) = True; + VG_(clo_log_to) = VgLogTo_Fd; + VG_(clo_log_fd) = 2; + VG_(write)( VG_(clo_log_fd), msg, nbytes ); + } + } +} + typedef struct { char buf[100]; int n; } printf_buf; +// Adds a single char to the buffer. When the buffer gets sufficiently +// full, we write its contents to the logging sink. static void add_to_myprintf_buf ( HChar c, void *p ) { printf_buf *myprintf_buf = (printf_buf *)p; if (myprintf_buf->n >= 100-10 /*paranoia*/ ) { - if (VG_(clo_log_fd) >= 0) { - VG_(send_bytes_to_logging_sink)( - myprintf_buf->buf, VG_(strlen)(myprintf_buf->buf) ); - } + send_bytes_to_logging_sink( myprintf_buf->buf, myprintf_buf->n ); myprintf_buf->n = 0; - myprintf_buf->buf[myprintf_buf->n] = 0; } myprintf_buf->buf[myprintf_buf->n++] = c; - myprintf_buf->buf[myprintf_buf->n] = 0; + myprintf_buf->buf[myprintf_buf->n] = 0; } -UInt VG_(printf) ( const char *format, ... ) +UInt VG_(vprintf) ( const char *format, va_list vargs ) { - UInt ret; - va_list vargs; + UInt ret = 0; printf_buf myprintf_buf = {"",0}; - va_start(vargs,format); - - ret = VG_(debugLog_vprintf) - ( add_to_myprintf_buf, &myprintf_buf, format, vargs ); - if (myprintf_buf.n > 0 && VG_(clo_log_fd) >= 0) { - VG_(send_bytes_to_logging_sink)( myprintf_buf.buf, myprintf_buf.n ); + if (VG_(clo_log_fd) >= 0) { + ret = VG_(debugLog_vprintf) + ( add_to_myprintf_buf, &myprintf_buf, format, vargs ); + + // Write out any chars left in the buffer. + if (myprintf_buf.n > 0) { + send_bytes_to_logging_sink( myprintf_buf.buf, myprintf_buf.n ); + } } + return ret; +} + +UInt VG_(printf) ( const char *format, ... ) +{ + UInt ret; + va_list vargs; + va_start(vargs, format); + ret = VG_(vprintf)(format, vargs); va_end(vargs); return ret; @@ -437,25 +463,32 @@ static void add_to_vg_sprintf_buf ( HChar c, void *p ) *(*vg_sprintf_ptr)++ = c; } -UInt VG_(sprintf) ( Char* buf, Char *format, ... ) +UInt VG_(vsprintf) ( Char* buf, const Char *format, va_list vargs ) { Int ret; - va_list vargs; Char *vg_sprintf_ptr = buf; - va_start(vargs,format); - ret = VG_(debugLog_vprintf) ( add_to_vg_sprintf_buf, &vg_sprintf_ptr, format, vargs ); - add_to_vg_sprintf_buf(0,&vg_sprintf_ptr); - - va_end(vargs); + add_to_vg_sprintf_buf('\0', &vg_sprintf_ptr); vg_assert(VG_(strlen)(buf) == ret); return ret; } +UInt VG_(sprintf) ( Char* buf, const Char *format, ... ) +{ + UInt ret; + va_list vargs; + + va_start(vargs,format); + ret = VG_(vsprintf)(buf, format, vargs); + va_end(vargs); + + return ret; +} + /* --------------------------------------------------------------------- Misc str* functions. @@ -935,7 +968,7 @@ void VG_(assert_fail) ( Bool isCore, const Char* expr, const Char* file, entered = True; va_start(vargs,format); - VG_(debugLog_vprintf) ( add_to_vg_sprintf_buf, &bufptr, format, vargs ); + VG_(vsprintf) ( bufptr, format, vargs ); add_to_vg_sprintf_buf('\0', &bufptr); va_end(vargs); diff --git a/include/tool.h b/include/tool.h index cdf5849ebd..9ba5644aa5 100644 --- a/include/tool.h +++ b/include/tool.h @@ -139,8 +139,8 @@ typedef VgMsgKind; /* Send a single-part message. Appends a newline. */ -extern int VG_(message) ( VgMsgKind kind, const Char* format, ... ); -extern int VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs ); +extern UInt VG_(message) ( VgMsgKind kind, const Char* format, ... ); +extern UInt VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs ); /*====================================================================*/ @@ -262,8 +262,10 @@ extern Addr VG_(get_IP) ( ThreadId tid ); * Hence no need for VG_(fprintf)(). */ extern UInt VG_(printf) ( const char *format, ... ); +extern UInt VG_(vprintf) ( const char *format, va_list vargs ); /* too noisy ... __attribute__ ((format (printf, 1, 2))) ; */ -extern UInt VG_(sprintf) ( Char* buf, Char *format, ... ); +extern UInt VG_(sprintf) ( Char* buf, const Char* format, ... ); +extern UInt VG_(vsprintf)( Char* buf, const Char* format, va_list vargs ); extern Int VG_(rename) ( Char* old_name, Char* new_name );