blob: f5400c00a5654b2ddeaf1664c704de7b0742c1d6 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*--------------------------------------------------------------------*/
3/*--- For sending error/informative messages. ---*/
nethercote85cdd342004-08-01 22:36:40 +00004/*--- vg_messages.c ---*/
sewardjde4a1d02002-03-22 01:27:54 +00005/*--------------------------------------------------------------------*/
6
7/*
njnb9c427c2004-12-01 14:14:42 +00008 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
sewardjde4a1d02002-03-22 01:27:54 +000010
njn53612422005-03-12 16:22:54 +000011 Copyright (C) 2000-2005 Julian Seward
sewardjde4a1d02002-03-22 01:27:54 +000012 jseward@acm.org
sewardjde4a1d02002-03-22 01:27:54 +000013
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
njn25e49d8e72002-09-23 09:36:25 +000029 The GNU General Public License is contained in the file COPYING.
sewardjde4a1d02002-03-22 01:27:54 +000030*/
31
32
nethercotef1e5e152004-09-01 23:58:16 +000033#include "core.h"
sewardjde4a1d02002-03-22 01:27:54 +000034
thughes6233a382004-08-21 11:10:44 +000035#include <time.h>
36#include <sys/time.h>
37
njn14319cc2005-03-13 06:26:22 +000038/* Size of a buffer used for creating messages. */
39#define M_MSGBUF 10000
sewardjde4a1d02002-03-22 01:27:54 +000040
njn695c16e2005-03-27 03:40:28 +000041static char mbuf[M_MSGBUF];
42static int n_mbuf;
sewardjde4a1d02002-03-22 01:27:54 +000043
sewardjb5f6f512005-03-10 23:59:00 +000044static void add_to_buf ( Char c, void *p )
sewardjde4a1d02002-03-22 01:27:54 +000045{
njn695c16e2005-03-27 03:40:28 +000046 if (n_mbuf >= (M_MSGBUF-1)) return;
47 mbuf[n_mbuf++] = c;
48 mbuf[n_mbuf] = 0;
sewardjde4a1d02002-03-22 01:27:54 +000049}
50
thughes6233a382004-08-21 11:10:44 +000051static void add_timestamp ( Char *buf )
52{
53 struct timeval tv;
54 struct tm tm;
55
56 if ( gettimeofday( &tv, NULL ) == 0 &&
57 localtime_r( &tv.tv_sec, &tm ) == &tm ) {
58 VG_(sprintf)( buf, "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
59 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
60 tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 );
61 }
62 else {
63 VG_(strcpy)( buf, "" );
64 }
65
66 return;
67}
68
sewardj2c5ffbe2005-03-12 13:32:06 +000069static int add_to_msg ( const Char *format, ... )
sewardjde4a1d02002-03-22 01:27:54 +000070{
fitzhardinge39de4b42003-10-31 07:12:21 +000071 int count;
sewardjde4a1d02002-03-22 01:27:54 +000072 va_list vargs;
73 va_start(vargs,format);
sewardjb5f6f512005-03-10 23:59:00 +000074 count = VG_(vprintf) ( add_to_buf, format, vargs, 0 );
sewardjde4a1d02002-03-22 01:27:54 +000075 va_end(vargs);
fitzhardinge39de4b42003-10-31 07:12:21 +000076 return count;
77}
78
sewardj2c5ffbe2005-03-12 13:32:06 +000079static int start_msg ( VgMsgKind kind )
njnc5a79092005-03-12 04:59:51 +000080{
81 Char ts[32];
82 Char c;
83 static const Char pfx[] = ">>>>>>>>>>>>>>>>";
njn695c16e2005-03-27 03:40:28 +000084 n_mbuf = 0;
85 mbuf[n_mbuf] = 0;
njnc5a79092005-03-12 04:59:51 +000086
87 if (VG_(clo_time_stamp))
88 add_timestamp(ts);
89 else
90 VG_(strcpy)(ts, "");
91 switch (kind) {
92 case Vg_UserMsg: c = '='; break;
93 case Vg_DebugMsg: c = '-'; break;
94 case Vg_DebugExtraMsg: c = '+'; break;
95 case Vg_ClientMsg: c = '*'; break;
96 default: c = '?'; break;
97 }
98 // The pfx trick prints one or more '>' characters in front of the
99 // messages when running Valgrind under Valgrind, one per level of
100 // self-hosting.
101 return add_to_msg( "%s%c%c%s%d%c%c ",
102 &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND],
103 c,c, ts, VG_(getpid)(), c,c );
104}
105
sewardj2c5ffbe2005-03-12 13:32:06 +0000106static
njnc5a79092005-03-12 04:59:51 +0000107int end_msg ( void )
108{
109 int count = 0;
110 if (VG_(clo_log_fd) >= 0) {
111 add_to_buf('\n',0);
njn695c16e2005-03-27 03:40:28 +0000112 VG_(send_bytes_to_logging_sink) ( mbuf, VG_(strlen)(mbuf) );
njnc5a79092005-03-12 04:59:51 +0000113 count = 1;
114 }
115 return count;
116}
117
sewardjb5f6f512005-03-10 23:59:00 +0000118int VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs )
fitzhardinge39de4b42003-10-31 07:12:21 +0000119{
120 int count;
njnc5a79092005-03-12 04:59:51 +0000121 count = start_msg ( kind );
sewardjb5f6f512005-03-10 23:59:00 +0000122 count += VG_(vprintf) ( add_to_buf, format, vargs, 0 );
njnc5a79092005-03-12 04:59:51 +0000123 count += end_msg();
fitzhardinge39de4b42003-10-31 07:12:21 +0000124 return count;
sewardjde4a1d02002-03-22 01:27:54 +0000125}
126
127/* Send a simple single-part message. */
sewardjb5f6f512005-03-10 23:59:00 +0000128int VG_(message) ( VgMsgKind kind, const Char* format, ... )
sewardjde4a1d02002-03-22 01:27:54 +0000129{
fitzhardinge39de4b42003-10-31 07:12:21 +0000130 int count;
sewardjde4a1d02002-03-22 01:27:54 +0000131 va_list vargs;
132 va_start(vargs,format);
fitzhardinge39de4b42003-10-31 07:12:21 +0000133 count = VG_(vmessage) ( kind, format, vargs );
sewardjde4a1d02002-03-22 01:27:54 +0000134 va_end(vargs);
fitzhardinge39de4b42003-10-31 07:12:21 +0000135 return count;
sewardjde4a1d02002-03-22 01:27:54 +0000136}
137
sewardj570f8902002-11-03 11:44:36 +0000138/* Do the low-level send of a message to the logging sink. */
139void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes )
140{
141 Int rc;
142 if (VG_(logging_to_filedes)) {
nethercotef8548672004-06-21 12:42:35 +0000143 VG_(write)( VG_(clo_log_fd), msg, nbytes );
sewardj570f8902002-11-03 11:44:36 +0000144 } else {
nethercotef8548672004-06-21 12:42:35 +0000145 rc = VG_(write_socket)( VG_(clo_log_fd), msg, nbytes );
sewardj570f8902002-11-03 11:44:36 +0000146 if (rc == -1) {
147 /* for example, the listener process died. Switch back to
148 stderr. */
149 VG_(logging_to_filedes) = True;
150 VG_(clo_log_to) = VgLogTo_Fd;
nethercotef8548672004-06-21 12:42:35 +0000151 VG_(clo_log_fd) = 2;
152 VG_(write)( VG_(clo_log_fd), msg, nbytes );
sewardj570f8902002-11-03 11:44:36 +0000153 }
sewardjde4a1d02002-03-22 01:27:54 +0000154 }
155}
156
sewardjde4a1d02002-03-22 01:27:54 +0000157/*--------------------------------------------------------------------*/
nethercotec91ce8d2004-08-09 11:15:10 +0000158/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +0000159/*--------------------------------------------------------------------*/