blob: 1d2d1df389b95457e6c95c317e5b38e1d496e3e2 [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/*
njnc9539842002-10-02 13:26:35 +00008 This file is part of Valgrind, an extensible x86 protected-mode
9 emulator for monitoring program execution on x86-Unixes.
sewardjde4a1d02002-03-22 01:27:54 +000010
nethercotebb1c9912004-01-04 16:43:23 +000011 Copyright (C) 2000-2004 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
sewardjde4a1d02002-03-22 01:27:54 +000038
39static char vg_mbuf[M_VG_MSGBUF];
40static int vg_n_mbuf;
41
42static void add_to_buf ( Char c )
43{
44 if (vg_n_mbuf >= (M_VG_MSGBUF-1)) return;
45 vg_mbuf[vg_n_mbuf++] = c;
46 vg_mbuf[vg_n_mbuf] = 0;
47}
48
thughes6233a382004-08-21 11:10:44 +000049static void add_timestamp ( Char *buf )
50{
51 struct timeval tv;
52 struct tm tm;
53
54 if ( gettimeofday( &tv, NULL ) == 0 &&
55 localtime_r( &tv.tv_sec, &tm ) == &tm ) {
56 VG_(sprintf)( buf, "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
57 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
58 tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 );
59 }
60 else {
61 VG_(strcpy)( buf, "" );
62 }
63
64 return;
65}
66
sewardjde4a1d02002-03-22 01:27:54 +000067
68/* Publically visible from here onwards. */
69
fitzhardinge39de4b42003-10-31 07:12:21 +000070int
sewardjde4a1d02002-03-22 01:27:54 +000071VG_(add_to_msg) ( Char *format, ... )
72{
fitzhardinge39de4b42003-10-31 07:12:21 +000073 int count;
sewardjde4a1d02002-03-22 01:27:54 +000074 va_list vargs;
75 va_start(vargs,format);
fitzhardinge39de4b42003-10-31 07:12:21 +000076 count = VG_(vprintf) ( add_to_buf, format, vargs );
sewardjde4a1d02002-03-22 01:27:54 +000077 va_end(vargs);
fitzhardinge39de4b42003-10-31 07:12:21 +000078 return count;
79}
80
81int VG_(vmessage) ( VgMsgKind kind, Char* format, va_list vargs )
82{
83 int count;
84 count = VG_(start_msg) ( kind );
85 count += VG_(vprintf) ( add_to_buf, format, vargs );
86 count += VG_(end_msg)();
87 return count;
sewardjde4a1d02002-03-22 01:27:54 +000088}
89
90/* Send a simple single-part message. */
fitzhardinge39de4b42003-10-31 07:12:21 +000091int VG_(message) ( VgMsgKind kind, Char* format, ... )
sewardjde4a1d02002-03-22 01:27:54 +000092{
fitzhardinge39de4b42003-10-31 07:12:21 +000093 int count;
sewardjde4a1d02002-03-22 01:27:54 +000094 va_list vargs;
95 va_start(vargs,format);
fitzhardinge39de4b42003-10-31 07:12:21 +000096 count = VG_(vmessage) ( kind, format, vargs );
sewardjde4a1d02002-03-22 01:27:54 +000097 va_end(vargs);
fitzhardinge39de4b42003-10-31 07:12:21 +000098 return count;
sewardjde4a1d02002-03-22 01:27:54 +000099}
100
fitzhardinge39de4b42003-10-31 07:12:21 +0000101int VG_(start_msg) ( VgMsgKind kind )
sewardjde4a1d02002-03-22 01:27:54 +0000102{
thughes6233a382004-08-21 11:10:44 +0000103 Char ts[32];
sewardjde4a1d02002-03-22 01:27:54 +0000104 Char c;
105 vg_n_mbuf = 0;
106 vg_mbuf[vg_n_mbuf] = 0;
thughes6233a382004-08-21 11:10:44 +0000107 if (VG_(clo_time_stamp))
108 add_timestamp(ts);
109 else
110 VG_(strcpy)(ts, "");
sewardjde4a1d02002-03-22 01:27:54 +0000111 switch (kind) {
112 case Vg_UserMsg: c = '='; break;
113 case Vg_DebugMsg: c = '-'; break;
114 case Vg_DebugExtraMsg: c = '+'; break;
fitzhardinge39de4b42003-10-31 07:12:21 +0000115 case Vg_ClientMsg: c = '*'; break;
sewardjde4a1d02002-03-22 01:27:54 +0000116 default: c = '?'; break;
117 }
thughes6233a382004-08-21 11:10:44 +0000118 return VG_(add_to_msg)( "%c%c%s%d%c%c ",
119 c,c, ts, VG_(getpid)(), c,c );
sewardjde4a1d02002-03-22 01:27:54 +0000120}
121
122
fitzhardinge39de4b42003-10-31 07:12:21 +0000123int VG_(end_msg) ( void )
sewardjde4a1d02002-03-22 01:27:54 +0000124{
fitzhardinge39de4b42003-10-31 07:12:21 +0000125 int count = 0;
nethercotef8548672004-06-21 12:42:35 +0000126 if (VG_(clo_log_fd) >= 0) {
sewardjde4a1d02002-03-22 01:27:54 +0000127 add_to_buf('\n');
sewardj570f8902002-11-03 11:44:36 +0000128 VG_(send_bytes_to_logging_sink) (
129 vg_mbuf, VG_(strlen)(vg_mbuf) );
fitzhardinge39de4b42003-10-31 07:12:21 +0000130 count = 1;
sewardj570f8902002-11-03 11:44:36 +0000131 }
fitzhardinge39de4b42003-10-31 07:12:21 +0000132 return count;
sewardj570f8902002-11-03 11:44:36 +0000133}
134
135
136/* Do the low-level send of a message to the logging sink. */
137void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes )
138{
139 Int rc;
140 if (VG_(logging_to_filedes)) {
nethercotef8548672004-06-21 12:42:35 +0000141 VG_(write)( VG_(clo_log_fd), msg, nbytes );
sewardj570f8902002-11-03 11:44:36 +0000142 } else {
nethercotef8548672004-06-21 12:42:35 +0000143 rc = VG_(write_socket)( VG_(clo_log_fd), msg, nbytes );
sewardj570f8902002-11-03 11:44:36 +0000144 if (rc == -1) {
145 /* for example, the listener process died. Switch back to
146 stderr. */
147 VG_(logging_to_filedes) = True;
148 VG_(clo_log_to) = VgLogTo_Fd;
nethercotef8548672004-06-21 12:42:35 +0000149 VG_(clo_log_fd) = 2;
150 VG_(write)( VG_(clo_log_fd), msg, nbytes );
sewardj570f8902002-11-03 11:44:36 +0000151 }
sewardjde4a1d02002-03-22 01:27:54 +0000152 }
153}
154
sewardjde4a1d02002-03-22 01:27:54 +0000155/*--------------------------------------------------------------------*/
nethercotec91ce8d2004-08-09 11:15:10 +0000156/*--- end ---*/
sewardjde4a1d02002-03-22 01:27:54 +0000157/*--------------------------------------------------------------------*/