9 #include "jdb_module.h"
11 #include "kernel_console.h"
13 #include "kmem_alloc.h"
14 #include "static_init.h"
17 * A output console that stores the output in a buffer.
19 * This buffer can be usefull for accessing older the debugging
20 * output without a serial console.
22 class Console_buffer : public Console
26 static size_t out_buf_size;
27 static size_t out_buf_len;
29 static char *out_buf_w;
32 bool Console_buffer::_enabled;
33 size_t Console_buffer::out_buf_size;
34 size_t Console_buffer::out_buf_len;
35 char* Console_buffer::out_buf;
36 char* Console_buffer::out_buf_w;
40 Console_buffer::at_jdb_enter()
47 Console_buffer::at_jdb_leave()
53 Console_buffer::Console_buffer()
55 static Jdb_handler enter(at_jdb_enter);
56 static Jdb_handler leave(at_jdb_leave);
58 Jdb::jdb_enter.add(&enter);
59 Jdb::jdb_leave.add(&leave);
61 size_t len = 2*Config::PAGE_SIZE;
64 if ( (s = strstr(Cmdline::cmdline(), " -out_buf="))
65 ||(s = strstr(Cmdline::cmdline(), " -out_buf ")))
66 len = strtoul(s+10, 0, 0);
72 * Allocates a buffer of the given size.
73 * @param size the buffer size in bytes.
77 Console_buffer::alloc( size_t size)
81 out_buf_size = (size + Config::PAGE_SIZE - 1) & Config::PAGE_MASK;
83 out_buf = (char*)Kmem_alloc::allocator()->
84 unaligned_alloc(out_buf_size);
93 Console_buffer::~Console_buffer()
96 Kmem_alloc::allocator()->
97 unaligned_free(out_buf_size, out_buf);
103 Console_buffer::write( char const *str, size_t len )
105 if (_enabled && out_buf)
110 s = out_buf_size - (out_buf_w - out_buf);
113 memcpy( out_buf_w, str, s );
114 if (out_buf_w + s >= out_buf + out_buf_size)
120 if (out_buf_len > out_buf_size)
121 out_buf_len = out_buf_size;
127 PRIVATE static inline
129 Console_buffer::inc_ptr(char **c)
131 if (++*c >= out_buf + out_buf_size)
135 PRIVATE static inline
137 Console_buffer::dec_out_ptr(char **c)
145 Console_buffer::getchar(bool)
151 * Prints the buffer to the standard I/O.
152 * @param lines the number of lines to skip.
153 * This method prints the buffer contents to the normal I/O.
154 * Before doing this the buffer is disabled, that no recursion
155 * appers even if the buffer is part of the muxed I/O.
159 Console_buffer::print_buffer(unsigned lines)
163 bool page = (lines == 0);
165 size_t len = out_buf_len;
167 if (out_buf_len == 0)
173 // go back <lines> lines
176 size_t l = out_buf_len;
178 // skip terminating 0x00, 0x0a ...
179 while ((*c == '\0' || *c == '\r') && (l > 0))
185 while (lines-- && (l > 0))
190 while ((*c != '\n') && (l > 0))
203 len = out_buf_len - l;
218 if (page && !Jdb_core::new_line(lines))
229 printf("use -out_buf=<size> to enable output buffer\n");
236 Console_buffer::strncmp(char *start, const char *search, size_t len)
238 while (len && *search && *start == *search)
245 return *search == '\0';
249 * Prints the buffer to the standard I/O.
250 * @param str the string the output should be filtered for
251 * This method prints the buffer contents to the normal I/O.
252 * Before doing this the buffer is disabled, that no recursion
253 * appers even if the buffer is part of the muxed I/O.
257 Console_buffer::print_buffer(const char *str)
261 char *bol = out_buf_w;
262 size_t len = out_buf_len;
264 if (out_buf_len == 0)
290 if (strncmp(bos, str, lens))
292 // found => print whole line
293 for (; len && bol != bos; len--)
299 putstr(Jdb::esc_emph);
300 for (s=str; *s && len; len--, s++)
306 for (; len && *bol != '\n'; len--)
308 if (*str && strncmp(bol, str, len))
330 printf("use -out_buf=<size> to enable output buffer\n");
335 Mword Console_buffer::get_attributes() const
341 PUBLIC static FIASCO_INIT
343 Console_buffer::init()
345 static Console_buffer cb;
346 Kconsole::console()->register_console(&cb);
349 STATIC_INITIALIZE(Console_buffer);
355 class Jdb_cb : public Jdb_module
358 Jdb_cb() FIASCO_INIT;
360 static char first_char;
361 static char search_str[30];
362 static Mword output_lines;
365 char Jdb_cb::first_char;
366 char Jdb_cb::search_str[30];
367 Mword Jdb_cb::output_lines;
370 Jdb_module::Action_code
371 Jdb_cb::action(int cmd, void *&args, char const *&fmt, int &next_char)
376 if (args == &first_char)
378 if (first_char == '/')
386 if (first_char != ' ' && first_char != KEY_RETURN)
388 next_char = first_char;
389 args = &output_lines;
391 return EXTRA_INPUT_WITH_NEXTCHAR;
394 else if (args == search_str)
397 Console_buffer::print_buffer(search_str);
402 Console_buffer::print_buffer(output_lines);
408 Jdb_module::Cmd const *
411 static const Cmd cs[] =
413 { 0, "B", "consolebuffer", "%C",
414 "B[<lines>|/<str>]\tshow (last n lines of) console buffer/search",
422 Jdb_cb::num_cmds() const
429 : Jdb_module("GENERAL")
433 static Jdb_cb jdb_cb INIT_PRIORITY(JDB_MODULE_INIT_PRIO);