]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/ecostest/common/eCosTestMonitorFilter.cpp
Initial revision
[karo-tx-redboot.git] / tools / src / tools / ecostest / common / eCosTestMonitorFilter.cpp
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
8 // This program is free software; you can redistribute it and/or modify it 
9 // under the terms of the GNU General Public License as published by the Free 
10 // Software Foundation; either version 2 of the License, or (at your option) 
11 // any later version.
12 // 
13 // This program is distributed in the hope that it will be useful, but WITHOUT 
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16 // more details.
17 // 
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25 //=================================================================
26 //
27 //        eCosTestMonitorFilter.cpp
28 //
29 //        Simple filter for monitoring data flowing through the client
30 //
31 //=================================================================
32 //=================================================================
33 //#####DESCRIPTIONBEGIN####
34 //
35 // Author(s):     jskov
36 // Contributors:  jskov
37 // Date:          2000-03-16
38 // Description:   This filter sits between GDB and the test running on
39 //                the target, allowing all transmitted data to be output.
40 //
41 // *Take* the time to move some of the functions here (which are shared
42 // with eCosTestSerialFilter, and probably DownloadFilter) and move
43 // them to some shared file.
44 //####DESCRIPTIONEND####
45
46 #include "eCosStd.h"
47
48 #include "eCosTestMonitorFilter.h"
49
50 CeCosTestMonitorFilter::CeCosTestMonitorFilter():
51   m_bOptVerbose(false)
52 {
53 }
54
55 CeCosTestMonitorFilter::~CeCosTestMonitorFilter()
56 {
57 }
58
59 //------------------------
60 // Output helpers.
61
62 void
63 CeCosTestMonitorFilter::ConsoleWrite(const char* pszStr)
64 {
65     fputs(pszStr, stderr);
66     fflush(stderr);
67 }
68
69 void
70 CeCosTestMonitorFilter::Trace(const char* pszFormat, ...)
71 {
72   
73   va_list marker;
74   va_start (marker, pszFormat);
75   
76   for(int nLength=100;nLength;) {
77     char *buf=new char[1+nLength];
78     int n=vsnprintf(buf, nLength, pszFormat, marker ); 
79     if(-1==n){
80       nLength*=2;  // NT behavior
81     } else if (n<nLength){
82       ConsoleWrite(buf);
83       nLength=0;   // trigger exit from loop
84     } else {
85       nLength=n+1; // UNIX behavior generally, or NT behavior when buffer size exactly matches required length
86     }
87     delete [] buf;
88   }
89   
90   va_end (marker);
91 }
92
93 void
94 CeCosTestMonitorFilter::PrintHex(const unsigned char* d1, int len, data_origin_t origin/*=SF_TARGET*/)
95 {
96     int offset = 0;
97     int i;
98     char buf[128];
99     int width = 8;
100
101     while (len) {
102         int count = MIN(width, len);
103         char* p = buf;
104         switch (origin) {
105         case MF_TARGET:
106             p += sprintf(p, "T:");
107             break;
108         case MF_HOST:
109             p += sprintf(p, "H:");
110             break;
111         }
112         p += sprintf(p, "%04x > ", offset);
113         // Print hex values.
114         for (i = 0; i < count; i++)
115             p += sprintf(p, "%02x ", d1[i]);
116         for (     ; i < width   ; i++)
117             p += sprintf(p, ".. ");
118
119         // Print ASCII string
120         p += sprintf(p, "'");
121         for (i = 0; i < count; i++) {
122             int c = d1[i];
123             if (' ' >= c || 'z' <= c)
124                 c = '.';
125             p += sprintf(p, "%c", c);
126         }
127         sprintf(p, "'\n");
128
129         Trace("%s", buf);
130
131         len -= count;
132         offset += count;
133         d1 += count;
134     }
135 }
136
137 bool CALLBACK
138 SerialMonitorFunction(void*& pBuf,
139                       unsigned int& nRead,
140                       CeCosSerial& serial,
141                       CeCosSocket& socket,
142                       void* pParem)
143 {
144     CeCosTestMonitorFilter* p = (CeCosTestMonitorFilter*) pParem;
145     return p->FilterFunctionProper(pBuf, nRead);
146 }
147
148 bool
149 CeCosTestMonitorFilter::FilterFunctionProper(void*& pBuf, unsigned int& nRead)
150 {
151     char* buffer = (char*) pBuf;
152
153     if (m_bOptVerbose)
154         PrintHex((unsigned char*) buffer, nRead, m_eOrigin);
155
156     return true;
157 }