|
From: <sv...@va...> - 2005-05-12 03:51:18
|
Author: njn
Date: 2005-05-12 04:51:15 +0100 (Thu, 12 May 2005)
New Revision: 3670
Modified:
trunk/coregrind/core.h
trunk/coregrind/vg_messages.c
trunk/coregrind/vg_mylibc.c
trunk/include/tool.h
Log:
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, an=
d
cuts 40 lines of code.
Modified: trunk/coregrind/core.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/coregrind/core.h 2005-05-12 03:47:31 UTC (rev 3669)
+++ trunk/coregrind/core.h 2005-05-12 03:51:15 UTC (rev 3670)
@@ -636,14 +636,6 @@
extern void VG_(nanosleep)(struct vki_timespec *);
=20
/* ---------------------------------------------------------------------
- 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
------------------------------------------------------------------ */
=20
Modified: trunk/coregrind/vg_messages.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/coregrind/vg_messages.c 2005-05-12 03:47:31 UTC (rev 3669)
+++ trunk/coregrind/vg_messages.c 2005-05-12 03:51:15 UTC (rev 3670)
@@ -29,66 +29,18 @@
The GNU General Public License is contained in the file COPYING.
*/
=20
-
#include "core.h"
-#include "pub_core_debuglog.h" /* VG_(debugLog_vprintf) */
=20
#include <time.h>
#include <sys/time.h>
=20
-/* 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 )
+UInt VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs )
{
- if (n_mbuf >=3D (M_MSGBUF-1)) return;
- mbuf[n_mbuf++] =3D c;
- mbuf[n_mbuf] =3D 0;
-}
-
-static void add_timestamp ( Char *buf )
-{
- struct timeval tv;
- struct tm tm;
- =20
- if ( gettimeofday( &tv, NULL ) =3D=3D 0 &&
- localtime_r( &tv.tv_sec, &tm ) =3D=3D &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, "" );
- }
- =20
- return;
-}
-
-static int add_to_msg ( const Char *format, ... )
-{
- int count;
- va_list vargs;
- va_start(vargs,format);
- count =3D 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 =3D 0;
+ Char c;
+ const Char* pfx_s;
static const Char pfx[] =3D ">>>>>>>>>>>>>>>>";
- n_mbuf =3D 0;
- mbuf[n_mbuf] =3D 0;
=20
- if (VG_(clo_time_stamp))
- add_timestamp(ts);
- else
- VG_(strcpy)(ts, "");
switch (kind) {
case Vg_UserMsg: c =3D '=3D'; break;
case Vg_DebugMsg: c =3D '-'; break;
@@ -96,39 +48,40 @@
case Vg_ClientMsg: c =3D '*'; break;
default: c =3D '?'; 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 ",=20
- &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND],
- c,c, ts, VG_(getpid)(), c,c );
-}
+ pfx_s =3D &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND],
=20
-static=20
-int end_msg ( void )
-{
- int count =3D 0;
- if (VG_(clo_log_fd) >=3D 0) {
- add_to_buf('\n',0);
- VG_(send_bytes_to_logging_sink) ( mbuf, VG_(strlen)(mbuf) );
- count =3D 1;
+ // Print the message
+ count =3D 0;
+ count +=3D VG_(printf) ("%s%c%c", pfx_s, c,c);
+
+ if (VG_(clo_time_stamp)) {
+ struct timeval tv;
+ struct tm tm;
+ =20
+ if ( gettimeofday( &tv, NULL ) =3D=3D 0 &&
+ localtime_r( &tv.tv_sec, &tm ) =3D=3D &tm )
+ {
+ count +=3D
+ 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;
-}
=20
-int VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs )
-{
- int count;
- count =3D start_msg ( kind );
- count +=3D VG_(debugLog_vprintf) ( add_to_buf, NULL, format, vargs );
- count +=3D end_msg();
+ count +=3D VG_(printf) ("%d%c%c ", VG_(getpid)(), c,c);
+ count +=3D VG_(vprintf)(format, vargs);
+ count +=3D VG_(printf) ("\n");
return count;
}
=20
/* 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 =3D VG_(vmessage) ( kind, format, vargs );
@@ -136,25 +89,6 @@
return count;
}
=20
-/* 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 =3D VG_(write_socket)( VG_(clo_log_fd), msg, nbytes );
- if (rc =3D=3D -1) {
- /* for example, the listener process died. Switch back to
- stderr. */
- VG_(logging_to_filedes) =3D True;
- VG_(clo_log_to) =3D VgLogTo_Fd;
- VG_(clo_log_fd) =3D 2;
- VG_(write)( VG_(clo_log_fd), msg, nbytes );
- }
- }
-}
-
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/
Modified: trunk/coregrind/vg_mylibc.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/coregrind/vg_mylibc.c 2005-05-12 03:47:31 UTC (rev 3669)
+++ trunk/coregrind/vg_mylibc.c 2005-05-12 03:51:15 UTC (rev 3670)
@@ -390,41 +390,67 @@
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 =3D VG_(write_socket)( VG_(clo_log_fd), msg, nbytes );
+ if (rc =3D=3D -1) {
+ // For example, the listener process died. Switch back to stde=
rr.
+ VG_(logging_to_filedes) =3D True;
+ VG_(clo_log_to) =3D VgLogTo_Fd;
+ VG_(clo_log_fd) =3D 2;
+ VG_(write)( VG_(clo_log_fd), msg, nbytes );
+ }
+ }
+}
+
typedef struct {
char buf[100];
int n;
} printf_buf;
=20
+// 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 =3D (printf_buf *)p;
=20
if (myprintf_buf->n >=3D 100-10 /*paranoia*/ ) {
- if (VG_(clo_log_fd) >=3D 0) {
- VG_(send_bytes_to_logging_sink)(=20
- myprintf_buf->buf, VG_(strlen)(myprintf_buf->buf) );
- }
+ send_bytes_to_logging_sink( myprintf_buf->buf, myprintf_buf->n );
myprintf_buf->n =3D 0;
- myprintf_buf->buf[myprintf_buf->n] =3D 0; =20
}
myprintf_buf->buf[myprintf_buf->n++] =3D c;
- myprintf_buf->buf[myprintf_buf->n] =3D 0;
+ myprintf_buf->buf[myprintf_buf->n] =3D 0;
}
=20
+UInt VG_(vprintf) ( const char *format, va_list vargs )
+{
+ UInt ret =3D 0;
+ printf_buf myprintf_buf =3D {"",0};
+
+ if (VG_(clo_log_fd) >=3D 0) {
+ ret =3D VG_(debugLog_vprintf)=20
+ ( 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;
- printf_buf myprintf_buf =3D {"",0};
- va_start(vargs,format);
- =20
- ret =3D VG_(debugLog_vprintf)=20
- ( add_to_myprintf_buf, &myprintf_buf, format, vargs );
=20
- if (myprintf_buf.n > 0 && VG_(clo_log_fd) >=3D 0) {
- VG_(send_bytes_to_logging_sink)( myprintf_buf.buf, myprintf_buf.n =
);
- }
-
+ va_start(vargs, format);
+ ret =3D VG_(vprintf)(format, vargs);
va_end(vargs);
=20
return ret;
@@ -437,26 +463,33 @@
*(*vg_sprintf_ptr)++ =3D c;
}
=20
-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 =3D buf;
=20
- va_start(vargs,format);
-
ret =3D VG_(debugLog_vprintf)=20
( add_to_vg_sprintf_buf, &vg_sprintf_ptr, format, vargs );
- add_to_vg_sprintf_buf(0,&vg_sprintf_ptr);
+ add_to_vg_sprintf_buf('\0', &vg_sprintf_ptr);
=20
- va_end(vargs);
-
vg_assert(VG_(strlen)(buf) =3D=3D ret);
=20
return ret;
}
=20
+UInt VG_(sprintf) ( Char* buf, const Char *format, ... )
+{
+ UInt ret;
+ va_list vargs;
=20
+ va_start(vargs,format);
+ ret =3D VG_(vsprintf)(buf, format, vargs);
+ va_end(vargs);
+
+ return ret;
+}
+
+
/* ---------------------------------------------------------------------
Misc str* functions.
------------------------------------------------------------------ */
@@ -935,7 +968,7 @@
entered =3D True;
=20
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);
=20
Modified: trunk/include/tool.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- trunk/include/tool.h 2005-05-12 03:47:31 UTC (rev 3669)
+++ trunk/include/tool.h 2005-05-12 03:51:15 UTC (rev 3670)
@@ -139,8 +139,8 @@
VgMsgKind;
=20
/* 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_lis=
t vargs );
=20
=20
/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*/
@@ -262,8 +262,10 @@
* 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 =
);
=20
extern Int VG_(rename) ( Char* old_name, Char* new_name );
=20
|