Add support to store less than the full packet in an etherdump
[gem5.git] / dev / simconsole.cc
1 /*
2 * Copyright (c) 2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* @file
30 * Implements the user interface to a serial console
31 */
32
33 #include <sys/ioctl.h>
34 #include <sys/termios.h>
35 #include <sys/types.h>
36 #include <errno.h>
37 #include <poll.h>
38 #include <unistd.h>
39
40 #include <iostream>
41 #include <fstream>
42 #include <sstream>
43 #include <string>
44
45 #include "base/misc.hh"
46 #include "base/socket.hh"
47 #include "base/trace.hh"
48 #include "dev/simconsole.hh"
49 #include "mem/functional_mem/memory_control.hh"
50 #include "sim/builder.hh"
51 #include "targetarch/ev5.hh"
52 #include "dev/uart.hh"
53 #include "dev/platform.hh"
54
55 using namespace std;
56
57 ////////////////////////////////////////////////////////////////////////
58 //
59 //
60
61 SimConsole::Event::Event(SimConsole *c, int fd, int e)
62 : PollEvent(fd, e), cons(c)
63 {
64 }
65
66 void
67 SimConsole::Event::process(int revent)
68 {
69 if (revent & POLLIN)
70 cons->data();
71 else if (revent & POLLNVAL)
72 cons->detach();
73 }
74
75 SimConsole::SimConsole(const string &name, const string &file, int num)
76 : SimObject(name), event(NULL), number(num), in_fd(-1), out_fd(-1),
77 listener(NULL), txbuf(16384), rxbuf(16384), outfile(NULL)
78 #if TRACING_ON == 1
79 , linebuf(16384)
80 #endif
81 {
82 if (!file.empty())
83 outfile = new ofstream(file.c_str());
84
85 if (outfile)
86 outfile->setf(ios::unitbuf);
87
88 }
89
90 SimConsole::~SimConsole()
91 {
92 close();
93
94 if (outfile)
95 delete outfile;
96 }
97
98 void
99 SimConsole::close()
100 {
101 if (in_fd != -1)
102 ::close(in_fd);
103
104 if (out_fd != in_fd && out_fd != -1)
105 ::close(out_fd);
106 }
107
108 void
109 SimConsole::attach(int in, int out, ConsoleListener *l)
110 {
111 in_fd = in;
112 out_fd = out;
113 listener = l;
114
115 event = new Event(this, in, POLLIN);
116 pollQueue.schedule(event);
117
118 stringstream stream;
119 ccprintf(stream, "==== m5 slave console: Console %d ====", number);
120
121 // we need an actual carriage return followed by a newline for the
122 // terminal
123 stream << "\r\n";
124
125 write((const uint8_t *)stream.str().c_str(), stream.str().size());
126
127
128 DPRINTFN("attach console %d\n", number);
129
130 txbuf.readall(out);
131 }
132
133 void
134 SimConsole::detach()
135 {
136 close();
137 in_fd = -1;
138 out_fd = -1;
139
140 pollQueue.remove(event);
141
142 if (listener) {
143 listener->add(this);
144 listener = NULL;
145 }
146
147 DPRINTFN("detach console %d\n", number);
148 }
149
150 void
151 SimConsole::data()
152 {
153 uint8_t buf[1024];
154 int len;
155
156 len = read(buf, sizeof(buf));
157 if (len) {
158 rxbuf.write((char *)buf, len);
159 // Inform the UART there is data available
160 uart->dataAvailable();
161 }
162 }
163
164 size_t
165 SimConsole::read(uint8_t *buf, size_t len)
166 {
167 if (in_fd < 0)
168 panic("Console not properly attached.\n");
169
170 size_t ret;
171 do {
172 ret = ::read(in_fd, buf, len);
173 } while (ret == -1 && errno == EINTR);
174
175
176 if (ret < 0)
177 DPRINTFN("Read failed.\n");
178
179 if (ret <= 0) {
180 detach();
181 return 0;
182 }
183
184 return ret;
185 }
186
187 // Console output.
188 size_t
189 SimConsole::write(const uint8_t *buf, size_t len)
190 {
191 if (out_fd < 0)
192 panic("Console not properly attached.\n");
193
194 size_t ret;
195 for (;;) {
196 ret = ::write(out_fd, buf, len);
197
198 if (ret >= 0)
199 break;
200
201 if (errno != EINTR)
202 detach();
203 }
204
205 return ret;
206 }
207
208 #define MORE_PENDING (ULL(1) << 61)
209 #define RECEIVE_SUCCESS (ULL(0) << 62)
210 #define RECEIVE_NONE (ULL(2) << 62)
211 #define RECEIVE_ERROR (ULL(3) << 62)
212
213 bool
214 SimConsole::in(uint8_t &c)
215 {
216 bool empty, ret;
217
218 empty = rxbuf.empty();
219 ret = !empty;
220 if (!empty) {
221 rxbuf.read((char *)&c, 1);
222 empty = rxbuf.empty();
223 }
224
225 DPRINTF(ConsoleVerbose, "in: \'%c\' %#02x more: %d, return: %d\n",
226 isprint(c) ? c : ' ', c, !empty, ret);
227
228 return ret;
229 }
230
231 uint64_t
232 SimConsole::console_in()
233 {
234 uint8_t c;
235 uint64_t value;
236
237 if (in(c)) {
238 value = RECEIVE_SUCCESS | c;
239 if (!rxbuf.empty())
240 value |= MORE_PENDING;
241 } else {
242 value = RECEIVE_NONE;
243 }
244
245 DPRINTF(ConsoleVerbose, "console_in: return: %#x\n", value);
246
247 return value;
248 }
249
250 void
251 SimConsole::out(char c)
252 {
253 #if TRACING_ON == 1
254 if (DTRACE(Console)) {
255 static char last = '\0';
256
257 if (c != '\n' && c != '\r' ||
258 last != '\n' && last != '\r') {
259 if (c == '\n' || c == '\r') {
260 int size = linebuf.size();
261 char *buffer = new char[size + 1];
262 linebuf.read(buffer, size);
263 buffer[size] = '\0';
264 DPRINTF(Console, "%s\n", buffer);
265 delete [] buffer;
266 } else {
267 linebuf.write(c);
268 }
269 }
270
271 last = c;
272 }
273 #endif
274
275 txbuf.write(c);
276
277 if (out_fd >= 0)
278 write(c);
279
280 if (outfile)
281 outfile->write(&c, 1);
282
283 DPRINTF(ConsoleVerbose, "out: \'%c\' %#02x\n",
284 isprint(c) ? c : ' ', (int)c);
285
286 }
287
288
289 void
290 SimConsole::serialize(ostream &os)
291 {
292 }
293
294 void
295 SimConsole::unserialize(Checkpoint *cp, const std::string &section)
296 {
297 }
298
299
300 BEGIN_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
301
302 SimObjectParam<ConsoleListener *> listener;
303 SimObjectParam<IntrControl *> intr_control;
304 Param<string> output;
305 Param<bool> append_name;
306 Param<int> number;
307
308 END_DECLARE_SIM_OBJECT_PARAMS(SimConsole)
309
310 BEGIN_INIT_SIM_OBJECT_PARAMS(SimConsole)
311
312 INIT_PARAM(listener, "console listener"),
313 INIT_PARAM(intr_control, "interrupt controller"),
314 INIT_PARAM_DFLT(output, "file to dump output to", ""),
315 INIT_PARAM_DFLT(append_name, "append name() to filename", true),
316 INIT_PARAM_DFLT(number, "console number", 0)
317
318 END_INIT_SIM_OBJECT_PARAMS(SimConsole)
319
320 CREATE_SIM_OBJECT(SimConsole)
321 {
322 string filename = output;
323 if (filename.empty()) {
324 if (!outputDirectory.empty())
325 filename = outputDirectory + getInstanceName();
326 } else {
327 if (append_name)
328 filename += "." + getInstanceName();
329 if (!outputDirectory.empty())
330 filename = outputDirectory + filename;
331 }
332
333 SimConsole *console = new SimConsole(getInstanceName(), filename, number);
334 ((ConsoleListener *)listener)->add(console);
335
336 return console;
337 }
338
339 REGISTER_SIM_OBJECT("SimConsole", SimConsole)
340
341 ////////////////////////////////////////////////////////////////////////
342 //
343 //
344
345 ConsoleListener::ConsoleListener(const string &name)
346 : SimObject(name), event(NULL)
347 {}
348
349 ConsoleListener::~ConsoleListener()
350 {
351 if (event)
352 delete event;
353 }
354
355 void
356 ConsoleListener::Event::process(int revent)
357 {
358 listener->accept();
359 }
360
361 ///////////////////////////////////////////////////////////////////////
362 // socket creation and console attach
363 //
364
365 void
366 ConsoleListener::listen(int port)
367 {
368 while (!listener.listen(port, true)) {
369 DPRINTF(Console,
370 ": can't bind address console port %d inuse PID %d\n",
371 port, getpid());
372 port++;
373 }
374
375 ccprintf(cerr, "Listening for console connection on port %d\n", port);
376
377 event = new Event(this, listener.getfd(), POLLIN);
378 pollQueue.schedule(event);
379 }
380
381 void
382 ConsoleListener::add(SimConsole *cons)
383 { ConsoleList.push_back(cons);}
384
385 void
386 ConsoleListener::accept()
387 {
388 if (!listener.islistening())
389 panic("%s: cannot accept a connection if not listening!", name());
390
391 int sfd = listener.accept(true);
392 if (sfd != -1) {
393 iter_t i = ConsoleList.begin();
394 iter_t end = ConsoleList.end();
395 if (i == end) {
396 close(sfd);
397 } else {
398 (*i)->attach(sfd, this);
399 i = ConsoleList.erase(i);
400 }
401 }
402 }
403
404 BEGIN_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
405
406 Param<int> port;
407
408 END_DECLARE_SIM_OBJECT_PARAMS(ConsoleListener)
409
410 BEGIN_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
411
412 INIT_PARAM_DFLT(port, "listen port", 3456)
413
414 END_INIT_SIM_OBJECT_PARAMS(ConsoleListener)
415
416 CREATE_SIM_OBJECT(ConsoleListener)
417 {
418 ConsoleListener *listener = new ConsoleListener(getInstanceName());
419 listener->listen(port);
420
421 return listener;
422 }
423
424 REGISTER_SIM_OBJECT("ConsoleListener", ConsoleListener)