|
From: <sv...@va...> - 2005-06-03 13:21:54
|
Author: njn
Date: 2005-06-03 14:21:18 +0100 (Fri, 03 Jun 2005)
New Revision: 3841
Added:
trunk/coregrind/m_libcprint.c
Log:
whoops, add this
Added: trunk/coregrind/m_libcprint.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/m_libcprint.c 2005-06-03 03:08:39 UTC (rev 3840)
+++ trunk/coregrind/m_libcprint.c 2005-06-03 13:21:18 UTC (rev 3841)
@@ -0,0 +1,216 @@
+
+/*--------------------------------------------------------------------*/
+/*--- Libc printing. m_libcprint.c ---*/
+/*--------------------------------------------------------------------*/
+=20
+/*
+ This file is part of Valgrind, a dynamic binary instrumentation
+ framework.
+
+ Copyright (C) 2000-2005 Julian Seward=20
+ js...@ac...
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307, USA.
+
+ The GNU General Public License is contained in the file COPYING.
+*/
+
+#include "core.h"
+#include "pub_core_debuglog.h"
+#include "pub_core_libcbase.h"
+#include "pub_core_libcprint.h"
+#include "pub_core_options.h"
+#include "valgrind.h" // for RUNNING_ON_VALGRIND
+
+#include <time.h>
+#include <sys/time.h>
+
+/* ---------------------------------------------------------------------
+ Writing to file or a socket
+ ------------------------------------------------------------------ */
+
+/* Tell the logging mechanism whether we are logging to a file
+ descriptor or a socket descriptor. */
+Bool VG_(logging_to_socket) =3D False;
+
+/* 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_socket)) {
+ 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_socket) =3D False;
+ VG_(clo_log_fd) =3D 2;
+ VG_(write)( VG_(clo_log_fd), msg, nbytes );
+ }
+ }
+}
+
+/* ---------------------------------------------------------------------
+ printf() and friends
+ ------------------------------------------------------------------ */
+
+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 =3D (printf_buf *)p;
+ =20
+ if (myprintf_buf->n >=3D 100-10 /*paranoia*/ ) {
+ send_bytes_to_logging_sink( myprintf_buf->buf, myprintf_buf->n );
+ myprintf_buf->n =3D 0;
+ }
+ myprintf_buf->buf[myprintf_buf->n++] =3D c;
+ myprintf_buf->buf[myprintf_buf->n] =3D 0;
+}
+
+UInt VG_(vprintf) ( const HChar *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 HChar *format, ... )
+{
+ UInt ret;
+ va_list vargs;
+
+ va_start(vargs, format);
+ ret =3D VG_(vprintf)(format, vargs);
+ va_end(vargs);
+
+ return ret;
+}
+
+/* A general replacement for sprintf(). */
+static void add_to_vg_sprintf_buf ( HChar c, void *p )
+{
+ char **vg_sprintf_ptr =3D p;
+ *(*vg_sprintf_ptr)++ =3D c;
+}
+
+UInt VG_(vsprintf) ( Char* buf, const HChar *format, va_list vargs )
+{
+ Int ret;
+ Char *vg_sprintf_ptr =3D buf;
+
+ 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);
+
+ vg_assert(VG_(strlen)(buf) =3D=3D ret);
+
+ return ret;
+}
+
+UInt VG_(sprintf) ( Char* buf, const HChar *format, ... )
+{
+ UInt ret;
+ va_list vargs;
+
+ va_start(vargs,format);
+ ret =3D VG_(vsprintf)(buf, format, vargs);
+ va_end(vargs);
+
+ return ret;
+}
+
+/* ---------------------------------------------------------------------
+ message()
+ ------------------------------------------------------------------ */
+
+UInt VG_(vmessage) ( VgMsgKind kind, const HChar* format, va_list vargs =
)
+{
+ UInt count =3D 0;
+ Char c;
+ const Char* pfx_s;
+ static const Char pfx[] =3D ">>>>>>>>>>>>>>>>";
+
+ switch (kind) {
+ case Vg_UserMsg: c =3D '=3D'; break;
+ case Vg_DebugMsg: c =3D '-'; break;
+ case Vg_DebugExtraMsg: c =3D '+'; break;
+ 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.
+ pfx_s =3D &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND],
+
+ // Print the message
+ count =3D 0;
+
+ if (!VG_(clo_xml))
+ 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 );
+ }
+ }
+
+ if (!VG_(clo_xml))
+ count +=3D VG_(printf) ("%d%c%c ", VG_(getpid)(), c,c);
+
+ count +=3D VG_(vprintf)(format, vargs);
+ count +=3D VG_(printf) ("\n");
+ return count;
+}
+
+/* Send a simple single-part message. */
+UInt VG_(message) ( VgMsgKind kind, const HChar* format, ... )
+{
+ UInt count;
+ va_list vargs;
+ va_start(vargs,format);
+ count =3D VG_(vmessage) ( kind, format, vargs );
+ va_end(vargs);
+ return count;
+}
+
+/*--------------------------------------------------------------------*/
+/*--- end ---*/
+/*--------------------------------------------------------------------*/
+
|