source: mainline/kernel/generic/src/console/kconsole.c@ bbfdf62

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bbfdf62 was 7a0359b, checked in by Martin Decky <martin@…>, 15 years ago

improve kernel function tracing

  • add support for more generic kernel sources
  • replace attribute((no_instrument_function)) with NO_TRACE macro (shorter and for future compatibility with different compilers)
  • to be on the safe side, do not instrument most of the inline and static functions (plus some specific non-static functions)

collateral code cleanup (no change in functionality)

  • Property mode set to 100644
File size: 16.3 KB
Line 
1/*
2 * Copyright (c) 2005 Jakub Jermar
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
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT 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 OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericconsole
30 * @{
31 */
32
33/**
34 * @file kconsole.c
35 * @brief Kernel console.
36 *
37 * This file contains kernel thread managing the kernel console.
38 *
39 */
40
41#include <console/kconsole.h>
42#include <console/console.h>
43#include <console/chardev.h>
44#include <console/cmd.h>
45#include <print.h>
46#include <panic.h>
47#include <typedefs.h>
48#include <adt/list.h>
49#include <arch.h>
50#include <macros.h>
51#include <debug.h>
52#include <func.h>
53#include <str.h>
54#include <macros.h>
55#include <sysinfo/sysinfo.h>
56#include <ddi/device.h>
57#include <symtab.h>
58#include <errno.h>
59#include <putchar.h>
60#include <str.h>
61
62/** Simple kernel console.
63 *
64 * The console is realized by kernel thread kconsole.
65 * It doesn't understand any useful command on its own,
66 * but makes it possible for other kernel subsystems to
67 * register their own commands.
68 */
69
70/** Locking.
71 *
72 * There is a list of cmd_info_t structures. This list
73 * is protected by cmd_lock spinlock. Note that specially
74 * the link elements of cmd_info_t are protected by
75 * this lock.
76 *
77 * Each cmd_info_t also has its own lock, which protects
78 * all elements thereof except the link element.
79 *
80 * cmd_lock must be acquired before any cmd_info lock.
81 * When locking two cmd info structures, structure with
82 * lower address must be locked first.
83 */
84
85SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */
86LIST_INITIALIZE(cmd_head); /**< Command list. */
87
88static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
89static size_t history_pos = 0;
90
91/** Initialize kconsole data structures
92 *
93 * This is the most basic initialization, almost no
94 * other kernel subsystem is ready yet.
95 *
96 */
97void kconsole_init(void)
98{
99 unsigned int i;
100
101 cmd_init();
102 for (i = 0; i < KCONSOLE_HISTORY; i++)
103 history[i][0] = 0;
104}
105
106/** Register kconsole command.
107 *
108 * @param cmd Structure describing the command.
109 *
110 * @return False on failure, true on success.
111 *
112 */
113bool cmd_register(cmd_info_t *cmd)
114{
115 link_t *cur;
116
117 spinlock_lock(&cmd_lock);
118
119 /*
120 * Make sure the command is not already listed.
121 */
122 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
123 cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
124
125 if (hlp == cmd) {
126 /* The command is already there. */
127 spinlock_unlock(&cmd_lock);
128 return false;
129 }
130
131 /* Avoid deadlock. */
132 if (hlp < cmd) {
133 spinlock_lock(&hlp->lock);
134 spinlock_lock(&cmd->lock);
135 } else {
136 spinlock_lock(&cmd->lock);
137 spinlock_lock(&hlp->lock);
138 }
139
140 if (str_cmp(hlp->name, cmd->name) == 0) {
141 /* The command is already there. */
142 spinlock_unlock(&hlp->lock);
143 spinlock_unlock(&cmd->lock);
144 spinlock_unlock(&cmd_lock);
145 return false;
146 }
147
148 spinlock_unlock(&hlp->lock);
149 spinlock_unlock(&cmd->lock);
150 }
151
152 /*
153 * Now the command can be added.
154 */
155 list_append(&cmd->link, &cmd_head);
156
157 spinlock_unlock(&cmd_lock);
158 return true;
159}
160
161/** Print count times a character */
162NO_TRACE static void print_cc(wchar_t ch, size_t count)
163{
164 size_t i;
165 for (i = 0; i < count; i++)
166 putchar(ch);
167}
168
169/** Try to find a command beginning with prefix */
170NO_TRACE static const char *cmdtab_search_one(const char *name,
171 link_t **startpos)
172{
173 size_t namelen = str_length(name);
174
175 spinlock_lock(&cmd_lock);
176
177 if (*startpos == NULL)
178 *startpos = cmd_head.next;
179
180 for (; *startpos != &cmd_head; *startpos = (*startpos)->next) {
181 cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link);
182
183 const char *curname = hlp->name;
184 if (str_length(curname) < namelen)
185 continue;
186
187 if (str_lcmp(curname, name, namelen) == 0) {
188 spinlock_unlock(&cmd_lock);
189 return (curname + str_lsize(curname, namelen));
190 }
191 }
192
193 spinlock_unlock(&cmd_lock);
194 return NULL;
195}
196
197/** Command completion of the commands
198 *
199 * @param name String to match, changed to hint on exit
200 * @param size Input buffer size
201 *
202 * @return Number of found matches
203 *
204 */
205NO_TRACE static int cmdtab_compl(char *input, size_t size)
206{
207 const char *name = input;
208
209 size_t found = 0;
210 link_t *pos = NULL;
211 const char *hint;
212 char output[MAX_CMDLINE];
213
214 output[0] = 0;
215
216 while ((hint = cmdtab_search_one(name, &pos))) {
217 if ((found == 0) || (str_length(output) > str_length(hint)))
218 str_cpy(output, MAX_CMDLINE, hint);
219
220 pos = pos->next;
221 found++;
222 }
223
224 if ((found > 1) && (str_length(output) != 0)) {
225 printf("\n");
226 pos = NULL;
227 while (cmdtab_search_one(name, &pos)) {
228 cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link);
229 printf("%s (%s)\n", hlp->name, hlp->description);
230 pos = pos->next;
231 }
232 }
233
234 if (found > 0)
235 str_cpy(input, size, output);
236
237 return found;
238}
239
240NO_TRACE static wchar_t *clever_readline(const char *prompt, indev_t *indev)
241{
242 printf("%s> ", prompt);
243
244 size_t position = 0;
245 wchar_t *current = history[history_pos];
246 current[0] = 0;
247
248 while (true) {
249 wchar_t ch = indev_pop_character(indev);
250
251 if (ch == '\n') {
252 /* Enter */
253 putchar(ch);
254 break;
255 }
256
257 if (ch == '\b') {
258 /* Backspace */
259 if (position == 0)
260 continue;
261
262 if (wstr_remove(current, position - 1)) {
263 position--;
264 putchar('\b');
265 printf("%ls ", current + position);
266 print_cc('\b', wstr_length(current) - position + 1);
267 continue;
268 }
269 }
270
271 if (ch == '\t') {
272 /* Tab completion */
273
274 /* Move to the end of the word */
275 for (; (current[position] != 0) && (!isspace(current[position]));
276 position++)
277 putchar(current[position]);
278
279 if (position == 0)
280 continue;
281
282 /* Find the beginning of the word
283 and copy it to tmp */
284 size_t beg;
285 for (beg = position - 1; (beg > 0) && (!isspace(current[beg]));
286 beg--);
287
288 if (isspace(current[beg]))
289 beg++;
290
291 char tmp[STR_BOUNDS(MAX_CMDLINE)];
292 wstr_to_str(tmp, position - beg + 1, current + beg);
293
294 int found;
295 if (beg == 0) {
296 /* Command completion */
297 found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
298 } else {
299 /* Symbol completion */
300 found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
301 }
302
303 if (found == 0)
304 continue;
305
306 if (found > 1) {
307 /* No unique hint, list was printed */
308 printf("%s> ", prompt);
309 printf("%ls", current);
310 print_cc('\b', wstr_length(current) - position);
311 continue;
312 }
313
314 /* We have a hint */
315
316 size_t off = 0;
317 size_t i = 0;
318 while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) {
319 if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
320 break;
321 i++;
322 }
323
324 printf("%ls", current + position);
325 position += str_length(tmp);
326 print_cc('\b', wstr_length(current) - position);
327
328 if (position == wstr_length(current)) {
329 /* Insert a space after the last completed argument */
330 if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) {
331 printf("%ls", current + position);
332 position++;
333 }
334 }
335 continue;
336 }
337
338 if (ch == U_LEFT_ARROW) {
339 /* Left */
340 if (position > 0) {
341 putchar('\b');
342 position--;
343 }
344 continue;
345 }
346
347 if (ch == U_RIGHT_ARROW) {
348 /* Right */
349 if (position < wstr_length(current)) {
350 putchar(current[position]);
351 position++;
352 }
353 continue;
354 }
355
356 if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) {
357 /* Up, down */
358 print_cc('\b', position);
359 print_cc(' ', wstr_length(current));
360 print_cc('\b', wstr_length(current));
361
362 if (ch == U_UP_ARROW) {
363 /* Up */
364 if (history_pos == 0)
365 history_pos = KCONSOLE_HISTORY - 1;
366 else
367 history_pos--;
368 } else {
369 /* Down */
370 history_pos++;
371 history_pos = history_pos % KCONSOLE_HISTORY;
372 }
373 current = history[history_pos];
374 printf("%ls", current);
375 position = wstr_length(current);
376 continue;
377 }
378
379 if (ch == U_HOME_ARROW) {
380 /* Home */
381 print_cc('\b', position);
382 position = 0;
383 continue;
384 }
385
386 if (ch == U_END_ARROW) {
387 /* End */
388 printf("%ls", current + position);
389 position = wstr_length(current);
390 continue;
391 }
392
393 if (ch == U_DELETE) {
394 /* Delete */
395 if (position == wstr_length(current))
396 continue;
397
398 if (wstr_remove(current, position)) {
399 printf("%ls ", current + position);
400 print_cc('\b', wstr_length(current) - position + 1);
401 }
402 continue;
403 }
404
405 if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
406 printf("%ls", current + position);
407 position++;
408 print_cc('\b', wstr_length(current) - position);
409 }
410 }
411
412 if (wstr_length(current) > 0) {
413 history_pos++;
414 history_pos = history_pos % KCONSOLE_HISTORY;
415 }
416
417 return current;
418}
419
420bool kconsole_check_poll(void)
421{
422 return check_poll(stdin);
423}
424
425NO_TRACE static bool parse_int_arg(const char *text, size_t len,
426 unative_t *result)
427{
428 bool isaddr = false;
429 bool isptr = false;
430
431 /* If we get a name, try to find it in symbol table */
432 if (text[0] == '&') {
433 isaddr = true;
434 text++;
435 len--;
436 } else if (text[0] == '*') {
437 isptr = true;
438 text++;
439 len--;
440 }
441
442 if ((text[0] < '0') || (text[0] > '9')) {
443 char symname[MAX_SYMBOL_NAME];
444 str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1);
445
446 uintptr_t symaddr;
447 int rc = symtab_addr_lookup(symname, &symaddr);
448 switch (rc) {
449 case ENOENT:
450 printf("Symbol %s not found.\n", symname);
451 return false;
452 case EOVERFLOW:
453 printf("Duplicate symbol %s.\n", symname);
454 symtab_print_search(symname);
455 return false;
456 case ENOTSUP:
457 printf("No symbol information available.\n");
458 return false;
459 case EOK:
460 if (isaddr)
461 *result = (unative_t) symaddr;
462 else if (isptr)
463 *result = **((unative_t **) symaddr);
464 else
465 *result = *((unative_t *) symaddr);
466 break;
467 default:
468 printf("Unknown error.\n");
469 return false;
470 }
471 } else {
472 /* It's a number - convert it */
473 uint64_t value;
474 int rc = str_uint64(text, NULL, 0, true, &value);
475 switch (rc) {
476 case EINVAL:
477 printf("Invalid number.\n");
478 return false;
479 case EOVERFLOW:
480 printf("Integer overflow.\n");
481 return false;
482 case EOK:
483 *result = (unative_t) value;
484 if (isptr)
485 *result = *((unative_t *) *result);
486 break;
487 default:
488 printf("Unknown error.\n");
489 return false;
490 }
491 }
492
493 return true;
494}
495
496/** Parse argument.
497 *
498 * Find start and end positions of command line argument.
499 *
500 * @param cmdline Command line as read from the input device.
501 * @param size Size (in bytes) of the string.
502 * @param start On entry, 'start' contains pointer to the offset
503 * of the first unprocessed character of cmdline.
504 * On successful exit, it marks beginning of the next argument.
505 * @param end Undefined on entry. On exit, 'end' is the offset of the first
506 * character behind the next argument.
507 *
508 * @return False on failure, true on success.
509 *
510 */
511NO_TRACE static bool parse_argument(const char *cmdline, size_t size,
512 size_t *start, size_t *end)
513{
514 ASSERT(start != NULL);
515 ASSERT(end != NULL);
516
517 bool found_start = false;
518 size_t offset = *start;
519 size_t prev = *start;
520 wchar_t ch;
521
522 while ((ch = str_decode(cmdline, &offset, size)) != 0) {
523 if (!found_start) {
524 if (!isspace(ch)) {
525 *start = prev;
526 found_start = true;
527 }
528 } else {
529 if (isspace(ch))
530 break;
531 }
532
533 prev = offset;
534 }
535 *end = prev;
536
537 return found_start;
538}
539
540/** Parse command line.
541 *
542 * @param cmdline Command line as read from input device.
543 * @param size Size (in bytes) of the string.
544 *
545 * @return Structure describing the command.
546 *
547 */
548NO_TRACE static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
549{
550 size_t start = 0;
551 size_t end = 0;
552 if (!parse_argument(cmdline, size, &start, &end)) {
553 /* Command line did not contain alphanumeric word. */
554 return NULL;
555 }
556 spinlock_lock(&cmd_lock);
557
558 cmd_info_t *cmd = NULL;
559 link_t *cur;
560
561 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
562 cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
563 spinlock_lock(&hlp->lock);
564
565 if (str_lcmp(hlp->name, cmdline + start,
566 max(str_length(hlp->name),
567 str_nlength(cmdline + start, (size_t) (end - start)))) == 0) {
568 cmd = hlp;
569 break;
570 }
571
572 spinlock_unlock(&hlp->lock);
573 }
574
575 spinlock_unlock(&cmd_lock);
576
577 if (!cmd) {
578 /* Unknown command. */
579 printf("Unknown command.\n");
580 return NULL;
581 }
582
583 /* cmd == hlp is locked */
584
585 /*
586 * The command line must be further analyzed and
587 * the parameters therefrom must be matched and
588 * converted to those specified in the cmd info
589 * structure.
590 */
591
592 bool error = false;
593 size_t i;
594 for (i = 0; i < cmd->argc; i++) {
595 char *buf;
596
597 start = end;
598 if (!parse_argument(cmdline, size, &start, &end)) {
599 if (cmd->argv[i].type == ARG_TYPE_STRING_OPTIONAL) {
600 buf = (char *) cmd->argv[i].buffer;
601 str_cpy(buf, cmd->argv[i].len, "");
602 continue;
603 }
604
605 printf("Too few arguments.\n");
606 spinlock_unlock(&cmd->lock);
607 return NULL;
608 }
609
610 switch (cmd->argv[i].type) {
611 case ARG_TYPE_STRING:
612 case ARG_TYPE_STRING_OPTIONAL:
613 buf = (char *) cmd->argv[i].buffer;
614 str_ncpy(buf, cmd->argv[i].len, cmdline + start,
615 end - start);
616 break;
617 case ARG_TYPE_INT:
618 if (!parse_int_arg(cmdline + start, end - start,
619 &cmd->argv[i].intval))
620 error = true;
621 break;
622 case ARG_TYPE_VAR:
623 if ((start < end - 1) && (cmdline[start] == '"')) {
624 if (cmdline[end - 1] == '"') {
625 buf = (char *) cmd->argv[i].buffer;
626 str_ncpy(buf, cmd->argv[i].len,
627 cmdline + start + 1,
628 (end - start) - 1);
629 cmd->argv[i].intval = (unative_t) buf;
630 cmd->argv[i].vartype = ARG_TYPE_STRING;
631 } else {
632 printf("Wrong synxtax.\n");
633 error = true;
634 }
635 } else if (parse_int_arg(cmdline + start,
636 end - start, &cmd->argv[i].intval)) {
637 cmd->argv[i].vartype = ARG_TYPE_INT;
638 } else {
639 printf("Unrecognized variable argument.\n");
640 error = true;
641 }
642 break;
643 case ARG_TYPE_INVALID:
644 default:
645 printf("Invalid argument type\n");
646 error = true;
647 break;
648 }
649 }
650
651 if (error) {
652 spinlock_unlock(&cmd->lock);
653 return NULL;
654 }
655
656 start = end;
657 if (parse_argument(cmdline, size, &start, &end)) {
658 printf("Too many arguments.\n");
659 spinlock_unlock(&cmd->lock);
660 return NULL;
661 }
662
663 spinlock_unlock(&cmd->lock);
664 return cmd;
665}
666
667/** Kernel console prompt.
668 *
669 * @param prompt Kernel console prompt (e.g kconsole/panic).
670 * @param msg Message to display in the beginning.
671 * @param kcon Wait for keypress to show the prompt
672 * and never exit.
673 *
674 */
675void kconsole(const char *prompt, const char *msg, bool kcon)
676{
677 if (!stdin) {
678 LOG("No stdin for kernel console");
679 return;
680 }
681
682 if (msg)
683 printf("%s", msg);
684
685 if (kcon)
686 indev_pop_character(stdin);
687 else
688 printf("Type \"exit\" to leave the console.\n");
689
690 while (true) {
691 wchar_t *tmp = clever_readline((char *) prompt, stdin);
692 size_t len = wstr_length(tmp);
693 if (!len)
694 continue;
695
696 char cmdline[STR_BOUNDS(MAX_CMDLINE)];
697 wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
698
699 if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
700 break;
701
702 cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
703 if (!cmd_info)
704 continue;
705
706 (void) cmd_info->func(cmd_info->argv);
707 }
708}
709
710/** Kernel console managing thread.
711 *
712 */
713void kconsole_thread(void *data)
714{
715 kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
716}
717
718/** @}
719 */
Note: See TracBrowser for help on using the repository browser.