8 #include "jdb_module.h"
10 #include "kernel_console.h"
12 #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;
63 if (Koptions::o()->opt(Koptions::F_out_buf))
64 len = Koptions::o()->out_buf;
70 * Allocates a buffer of the given size.
71 * @param size the buffer size in bytes.
75 Console_buffer::alloc( size_t size)
79 out_buf_size = (size + Config::PAGE_SIZE - 1) & Config::PAGE_MASK;
81 out_buf = (char*)Kmem_alloc::allocator()->
82 unaligned_alloc(out_buf_size);
91 Console_buffer::~Console_buffer()
94 Kmem_alloc::allocator()->
95 unaligned_free(out_buf_size, out_buf);
101 Console_buffer::write( char const *str, size_t len )
103 if (_enabled && out_buf)
108 s = out_buf_size - (out_buf_w - out_buf);
111 memcpy( out_buf_w, str, s );
112 if (out_buf_w + s >= out_buf + out_buf_size)
118 if (out_buf_len > out_buf_size)
119 out_buf_len = out_buf_size;
125 PRIVATE static inline
127 Console_buffer::inc_ptr(char **c)
129 if (++*c >= out_buf + out_buf_size)
133 PRIVATE static inline
135 Console_buffer::dec_out_ptr(char **c)
143 Console_buffer::getchar(bool)
149 * Prints the buffer to the standard I/O.
150 * @param lines the number of lines to skip.
151 * This method prints the buffer contents to the normal I/O.
152 * Before doing this the buffer is disabled, that no recursion
153 * appers even if the buffer is part of the muxed I/O.
157 Console_buffer::print_buffer(unsigned lines)
161 bool page = (lines == 0);
163 size_t len = out_buf_len;
165 if (out_buf_len == 0)
171 // go back <lines> lines
174 size_t l = out_buf_len;
176 // skip terminating 0x00, 0x0a ...
177 while ((*c == '\0' || *c == '\r') && (l > 0))
183 while (lines-- && (l > 0))
188 while ((*c != '\n') && (l > 0))
201 len = out_buf_len - l;
216 if (page && !Jdb_core::new_line(lines))
227 printf("use -out_buf=<size> to enable output buffer\n");
234 Console_buffer::strncmp(char *start, const char *search, size_t len)
236 while (len && *search && *start == *search)
243 return *search == '\0';
247 * Prints the buffer to the standard I/O.
248 * @param str the string the output should be filtered for
249 * This method prints the buffer contents to the normal I/O.
250 * Before doing this the buffer is disabled, that no recursion
251 * appers even if the buffer is part of the muxed I/O.
255 Console_buffer::print_buffer(const char *str)
259 char *bol = out_buf_w;
260 size_t len = out_buf_len;
262 if (out_buf_len == 0)
288 if (strncmp(bos, str, lens))
290 // found => print whole line
291 for (; len && bol != bos; len--)
297 putstr(Jdb::esc_emph);
298 for (s=str; *s && len; len--, s++)
304 for (; len && *bol != '\n'; len--)
306 if (*str && strncmp(bol, str, len))
328 printf("use -out_buf=<size> to enable output buffer\n");
333 Mword Console_buffer::get_attributes() const
339 PUBLIC static FIASCO_INIT
341 Console_buffer::init()
343 static Console_buffer cb;
344 Kconsole::console()->register_console(&cb);
347 STATIC_INITIALIZE(Console_buffer);
353 class Jdb_cb : public Jdb_module
356 Jdb_cb() FIASCO_INIT;
358 static char first_char;
359 static char search_str[30];
360 static Mword output_lines;
363 char Jdb_cb::first_char;
364 char Jdb_cb::search_str[30];
365 Mword Jdb_cb::output_lines;
368 Jdb_module::Action_code
369 Jdb_cb::action(int cmd, void *&args, char const *&fmt, int &next_char)
374 if (args == &first_char)
376 if (first_char == '/')
384 if (first_char != ' ' && first_char != KEY_RETURN)
386 next_char = first_char;
387 args = &output_lines;
389 return EXTRA_INPUT_WITH_NEXTCHAR;
392 else if (args == search_str)
395 Console_buffer::print_buffer(search_str);
400 Console_buffer::print_buffer(output_lines);
406 Jdb_module::Cmd const *
409 static const Cmd cs[] =
411 { 0, "B", "consolebuffer", "%C",
412 "B[<lines>|/<str>]\tshow (last n lines of) console buffer/search",
420 Jdb_cb::num_cmds() const
427 : Jdb_module("GENERAL")
431 static Jdb_cb jdb_cb INIT_PRIORITY(JDB_MODULE_INIT_PRIO);