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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1c8dc0 was e2b762ec, checked in by Jiri Svoboda <jirik.svoboda@…>, 17 years ago

Make kernel symbol information optional.

  • Property mode set to 100644
File size: 16.8 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#include <console/kconsole.h>
41#include <console/console.h>
42#include <console/chardev.h>
43#include <console/cmd.h>
44#include <print.h>
45#include <panic.h>
46#include <arch/types.h>
47#include <adt/list.h>
48#include <arch.h>
49#include <macros.h>
50#include <debug.h>
51#include <func.h>
52#include <string.h>
53#include <macros.h>
54#include <sysinfo/sysinfo.h>
55#include <ddi/device.h>
56
57#ifdef CONFIG_SYMTAB
58#include <symtab.h>
59#endif
60
61/** Simple kernel console.
62 *
63 * The console is realized by kernel thread kconsole.
64 * It doesn't understand any useful command on its own,
65 * but makes it possible for other kernel subsystems to
66 * register their own commands.
67 */
68
69/** Locking.
70 *
71 * There is a list of cmd_info_t structures. This list
72 * is protected by cmd_lock spinlock. Note that specially
73 * the link elements of cmd_info_t are protected by
74 * this lock.
75 *
76 * Each cmd_info_t also has its own lock, which protects
77 * all elements thereof except the link element.
78 *
79 * cmd_lock must be acquired before any cmd_info lock.
80 * When locking two cmd info structures, structure with
81 * lower address must be locked first.
82 */
83
84SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */
85LIST_INITIALIZE(cmd_head); /**< Command list. */
86
87static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
88static bool parse_argument(char *cmdline, size_t len, index_t *start,
89 index_t *end);
90static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
91
92/*
93 * For now, we use 0 as INR.
94 * However, it is therefore desirable to have architecture specific
95 * definition of KCONSOLE_VIRT_INR in the future.
96 */
97#define KCONSOLE_VIRT_INR 0
98
99bool kconsole_notify = false;
100irq_t kconsole_irq;
101
102
103/** Allways refuse IRQ ownership.
104 *
105 * This is not a real IRQ, so we always decline.
106 *
107 * @return Always returns IRQ_DECLINE.
108 *
109 */
110static irq_ownership_t kconsole_claim(irq_t *irq)
111{
112 return IRQ_DECLINE;
113}
114
115
116/** Initialize kconsole data structures
117 *
118 * This is the most basic initialization, almost no
119 * other kernel subsystem is ready yet.
120 *
121 */
122void kconsole_init(void)
123{
124 unsigned int i;
125
126 cmd_init();
127 for (i = 0; i < KCONSOLE_HISTORY; i++)
128 history[i][0] = '\0';
129}
130
131
132/** Initialize kconsole notification mechanism
133 *
134 * Initialize the virtual IRQ notification mechanism.
135 *
136 */
137void kconsole_notify_init(void)
138{
139 devno_t devno = device_assign_devno();
140
141 sysinfo_set_item_val("kconsole.present", NULL, true);
142 sysinfo_set_item_val("kconsole.devno", NULL, devno);
143 sysinfo_set_item_val("kconsole.inr", NULL, KCONSOLE_VIRT_INR);
144
145 irq_initialize(&kconsole_irq);
146 kconsole_irq.devno = devno;
147 kconsole_irq.inr = KCONSOLE_VIRT_INR;
148 kconsole_irq.claim = kconsole_claim;
149 irq_register(&kconsole_irq);
150
151 kconsole_notify = true;
152}
153
154
155/** Register kconsole command.
156 *
157 * @param cmd Structure describing the command.
158 *
159 * @return 0 on failure, 1 on success.
160 */
161int cmd_register(cmd_info_t *cmd)
162{
163 link_t *cur;
164
165 spinlock_lock(&cmd_lock);
166
167 /*
168 * Make sure the command is not already listed.
169 */
170 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
171 cmd_info_t *hlp;
172
173 hlp = list_get_instance(cur, cmd_info_t, link);
174
175 if (hlp == cmd) {
176 /* The command is already there. */
177 spinlock_unlock(&cmd_lock);
178 return 0;
179 }
180
181 /* Avoid deadlock. */
182 if (hlp < cmd) {
183 spinlock_lock(&hlp->lock);
184 spinlock_lock(&cmd->lock);
185 } else {
186 spinlock_lock(&cmd->lock);
187 spinlock_lock(&hlp->lock);
188 }
189 if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name),
190 strlen(hlp->name))) == 0)) {
191 /* The command is already there. */
192 spinlock_unlock(&hlp->lock);
193 spinlock_unlock(&cmd->lock);
194 spinlock_unlock(&cmd_lock);
195 return 0;
196 }
197
198 spinlock_unlock(&hlp->lock);
199 spinlock_unlock(&cmd->lock);
200 }
201
202 /*
203 * Now the command can be added.
204 */
205 list_append(&cmd->link, &cmd_head);
206
207 spinlock_unlock(&cmd_lock);
208 return 1;
209}
210
211/** Print count times a character */
212static void rdln_print_c(char ch, int count)
213{
214 int i;
215 for (i = 0; i < count; i++)
216 putchar(ch);
217}
218
219/** Insert character to string */
220static void insert_char(char *str, char ch, int pos)
221{
222 int i;
223
224 for (i = strlen(str); i > pos; i--)
225 str[i] = str[i - 1];
226 str[pos] = ch;
227}
228
229/** Try to find a command beginning with prefix */
230static const char *cmdtab_search_one(const char *name,link_t **startpos)
231{
232 size_t namelen = strlen(name);
233 const char *curname;
234
235 spinlock_lock(&cmd_lock);
236
237 if (!*startpos)
238 *startpos = cmd_head.next;
239
240 for (; *startpos != &cmd_head; *startpos = (*startpos)->next) {
241 cmd_info_t *hlp;
242 hlp = list_get_instance(*startpos, cmd_info_t, link);
243
244 curname = hlp->name;
245 if (strlen(curname) < namelen)
246 continue;
247 if (strncmp(curname, name, namelen) == 0) {
248 spinlock_unlock(&cmd_lock);
249 return curname+namelen;
250 }
251 }
252 spinlock_unlock(&cmd_lock);
253 return NULL;
254}
255
256
257/** Command completion of the commands
258 *
259 * @param name - string to match, changed to hint on exit
260 * @return number of found matches
261 */
262static int cmdtab_compl(char *name)
263{
264 static char output[/*MAX_SYMBOL_NAME*/128 + 1];
265 link_t *startpos = NULL;
266 const char *foundtxt;
267 int found = 0;
268 int i;
269
270 output[0] = '\0';
271 while ((foundtxt = cmdtab_search_one(name, &startpos))) {
272 startpos = startpos->next;
273 if (!found)
274 strncpy(output, foundtxt, strlen(foundtxt) + 1);
275 else {
276 for (i = 0; output[i] && foundtxt[i] &&
277 output[i] == foundtxt[i]; i++)
278 ;
279 output[i] = '\0';
280 }
281 found++;
282 }
283 if (!found)
284 return 0;
285
286 if (found > 1 && !strlen(output)) {
287 printf("\n");
288 startpos = NULL;
289 while ((foundtxt = cmdtab_search_one(name, &startpos))) {
290 cmd_info_t *hlp;
291 hlp = list_get_instance(startpos, cmd_info_t, link);
292 printf("%s - %s\n", hlp->name, hlp->description);
293 startpos = startpos->next;
294 }
295 }
296 strncpy(name, output, 128/*MAX_SYMBOL_NAME*/);
297 return found;
298}
299
300static char *clever_readline(const char *prompt, indev_t *input)
301{
302 static int histposition = 0;
303
304 static char tmp[MAX_CMDLINE + 1];
305 int curlen = 0, position = 0;
306 char *current = history[histposition];
307 int i;
308 char mod; /* Command Modifier */
309 char c;
310
311 printf("%s> ", prompt);
312 while (1) {
313 c = _getc(input);
314 if (c == '\n') {
315 putchar(c);
316 break;
317 }
318 if (c == '\b') { /* Backspace */
319 if (position == 0)
320 continue;
321 for (i = position; i < curlen; i++)
322 current[i - 1] = current[i];
323 curlen--;
324 position--;
325 putchar('\b');
326 for (i = position; i < curlen; i++)
327 putchar(current[i]);
328 putchar(' ');
329 rdln_print_c('\b', curlen - position + 1);
330 continue;
331 }
332 if (c == '\t') { /* Tabulator */
333 int found;
334
335 /* Move to the end of the word */
336 for (; position < curlen && current[position] != ' ';
337 position++)
338 putchar(current[position]);
339 /* Copy to tmp last word */
340 for (i = position - 1; i >= 0 && current[i] != ' '; i--)
341 ;
342 /* If word begins with * or &, skip it */
343 if (tmp[0] == '*' || tmp[0] == '&')
344 for (i = 1; tmp[i]; i++)
345 tmp[i - 1] = tmp[i];
346 i++; /* I is at the start of the word */
347 strncpy(tmp, current + i, position - i + 1);
348
349 if (i == 0) { /* Command completion */
350 found = cmdtab_compl(tmp);
351 } else { /* Symtab completion */
352#ifdef CONFIG_SYMTAB
353 found = symtab_compl(tmp);
354#else
355 found = 0;
356#endif
357 }
358
359 if (found == 0)
360 continue;
361 for (i = 0; tmp[i] && curlen < MAX_CMDLINE;
362 i++, curlen++)
363 insert_char(current, tmp[i], i + position);
364
365 if (strlen(tmp) || found == 1) { /* If we have a hint */
366 for (i = position; i < curlen; i++)
367 putchar(current[i]);
368 position += strlen(tmp);
369 /* Add space to end */
370 if (found == 1 && position == curlen &&
371 curlen < MAX_CMDLINE) {
372 current[position] = ' ';
373 curlen++;
374 position++;
375 putchar(' ');
376 }
377 } else { /* No hint, table was printed */
378 printf("%s> ", prompt);
379 for (i = 0; i < curlen; i++)
380 putchar(current[i]);
381 position += strlen(tmp);
382 }
383 rdln_print_c('\b', curlen - position);
384 continue;
385 }
386 if (c == 0x1b) { /* Special command */
387 mod = _getc(input);
388 c = _getc(input);
389
390 if (mod != 0x5b && mod != 0x4f)
391 continue;
392
393 if (c == 0x33 && _getc(input) == 0x7e) {
394 /* Delete */
395 if (position == curlen)
396 continue;
397 for (i = position + 1; i < curlen; i++) {
398 putchar(current[i]);
399 current[i - 1] = current[i];
400 }
401 putchar(' ');
402 rdln_print_c('\b', curlen - position);
403 curlen--;
404 } else if (c == 0x48) { /* Home */
405 rdln_print_c('\b', position);
406 position = 0;
407 } else if (c == 0x46) { /* End */
408 for (i = position; i < curlen; i++)
409 putchar(current[i]);
410 position = curlen;
411 } else if (c == 0x44) { /* Left */
412 if (position > 0) {
413 putchar('\b');
414 position--;
415 }
416 continue;
417 } else if (c == 0x43) { /* Right */
418 if (position < curlen) {
419 putchar(current[position]);
420 position++;
421 }
422 continue;
423 } else if (c == 0x41 || c == 0x42) {
424 /* Up, down */
425 rdln_print_c('\b', position);
426 rdln_print_c(' ', curlen);
427 rdln_print_c('\b', curlen);
428 if (c == 0x41) /* Up */
429 histposition--;
430 else
431 histposition++;
432 if (histposition < 0) {
433 histposition = KCONSOLE_HISTORY - 1;
434 } else {
435 histposition =
436 histposition % KCONSOLE_HISTORY;
437 }
438 current = history[histposition];
439 printf("%s", current);
440 curlen = strlen(current);
441 position = curlen;
442 continue;
443 }
444 continue;
445 }
446 if (curlen >= MAX_CMDLINE)
447 continue;
448
449 insert_char(current, c, position);
450
451 curlen++;
452 for (i = position; i < curlen; i++)
453 putchar(current[i]);
454 position++;
455 rdln_print_c('\b',curlen - position);
456 }
457 if (curlen) {
458 histposition++;
459 histposition = histposition % KCONSOLE_HISTORY;
460 }
461 current[curlen] = '\0';
462 return current;
463}
464
465bool kconsole_check_poll(void)
466{
467 return check_poll(stdin);
468}
469
470/** Kernel console prompt.
471 *
472 * @param prompt Kernel console prompt (e.g kconsole/panic).
473 * @param msg Message to display in the beginning.
474 * @param kcon Wait for keypress to show the prompt
475 * and never exit.
476 *
477 */
478void kconsole(char *prompt, char *msg, bool kcon)
479{
480 cmd_info_t *cmd_info;
481 count_t len;
482 char *cmdline;
483
484 if (!stdin) {
485 LOG("No stdin for kernel console");
486 return;
487 }
488
489 if (msg)
490 printf("%s", msg);
491
492 if (kcon)
493 _getc(stdin);
494 else
495 printf("Type \"exit\" to leave the console.\n");
496
497 while (true) {
498 cmdline = clever_readline((char *) prompt, stdin);
499 len = strlen(cmdline);
500 if (!len)
501 continue;
502
503 if ((!kcon) && (len == 4) && (strncmp(cmdline, "exit", 4) == 0))
504 break;
505
506 cmd_info = parse_cmdline(cmdline, len);
507 if (!cmd_info)
508 continue;
509
510 (void) cmd_info->func(cmd_info->argv);
511 }
512}
513
514/** Kernel console managing thread.
515 *
516 */
517void kconsole_thread(void *data)
518{
519 kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
520}
521
522static int parse_int_arg(char *text, size_t len, unative_t *result)
523{
524 uintptr_t symaddr;
525 bool isaddr = false;
526 bool isptr = false;
527
528#ifdef CONFIG_SYMTAB
529 static char symname[MAX_SYMBOL_NAME];
530#endif
531
532 /* If we get a name, try to find it in symbol table */
533 if (text[0] == '&') {
534 isaddr = true;
535 text++;
536 len--;
537 } else if (text[0] == '*') {
538 isptr = true;
539 text++;
540 len--;
541 }
542 if (text[0] < '0' || text[0] > '9') {
543#ifdef CONFIG_SYMTAB
544 strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
545 symaddr = get_symbol_addr(symname);
546 if (!symaddr) {
547 printf("Symbol %s not found.\n", symname);
548 return -1;
549 }
550 if (symaddr == (uintptr_t) -1) {
551 printf("Duplicate symbol %s.\n", symname);
552 symtab_print_search(symname);
553 return -1;
554 }
555#else
556 symaddr = 0;
557#endif
558 if (isaddr)
559 *result = (unative_t)symaddr;
560 else if (isptr)
561 *result = **((unative_t **)symaddr);
562 else
563 *result = *((unative_t *)symaddr);
564 } else { /* It's a number - convert it */
565 *result = atoi(text);
566 if (isptr)
567 *result = *((unative_t *)*result);
568 }
569
570 return 0;
571}
572
573/** Parse command line.
574 *
575 * @param cmdline Command line as read from input device.
576 * @param len Command line length.
577 *
578 * @return Structure describing the command.
579 */
580cmd_info_t *parse_cmdline(char *cmdline, size_t len)
581{
582 index_t start = 0, end = 0;
583 cmd_info_t *cmd = NULL;
584 link_t *cur;
585 count_t i;
586 int error = 0;
587
588 if (!parse_argument(cmdline, len, &start, &end)) {
589 /* Command line did not contain alphanumeric word. */
590 return NULL;
591 }
592
593 spinlock_lock(&cmd_lock);
594
595 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
596 cmd_info_t *hlp;
597
598 hlp = list_get_instance(cur, cmd_info_t, link);
599 spinlock_lock(&hlp->lock);
600
601 if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
602 end - start + 1)) == 0) {
603 cmd = hlp;
604 break;
605 }
606
607 spinlock_unlock(&hlp->lock);
608 }
609
610 spinlock_unlock(&cmd_lock);
611
612 if (!cmd) {
613 /* Unknown command. */
614 printf("Unknown command.\n");
615 return NULL;
616 }
617
618 /* cmd == hlp is locked */
619
620 /*
621 * The command line must be further analyzed and
622 * the parameters therefrom must be matched and
623 * converted to those specified in the cmd info
624 * structure.
625 */
626
627 for (i = 0; i < cmd->argc; i++) {
628 char *buf;
629 start = end + 1;
630 if (!parse_argument(cmdline, len, &start, &end)) {
631 printf("Too few arguments.\n");
632 spinlock_unlock(&cmd->lock);
633 return NULL;
634 }
635
636 error = 0;
637 switch (cmd->argv[i].type) {
638 case ARG_TYPE_STRING:
639 buf = (char *) cmd->argv[i].buffer;
640 strncpy(buf, (const char *) &cmdline[start],
641 min((end - start) + 2, cmd->argv[i].len));
642 buf[min((end - start) + 1, cmd->argv[i].len - 1)] =
643 '\0';
644 break;
645 case ARG_TYPE_INT:
646 if (parse_int_arg(cmdline + start, end - start + 1,
647 &cmd->argv[i].intval))
648 error = 1;
649 break;
650 case ARG_TYPE_VAR:
651 if (start != end && cmdline[start] == '"' &&
652 cmdline[end] == '"') {
653 buf = (char *) cmd->argv[i].buffer;
654 strncpy(buf, (const char *) &cmdline[start + 1],
655 min((end-start), cmd->argv[i].len));
656 buf[min((end - start), cmd->argv[i].len - 1)] =
657 '\0';
658 cmd->argv[i].intval = (unative_t) buf;
659 cmd->argv[i].vartype = ARG_TYPE_STRING;
660 } else if (!parse_int_arg(cmdline + start,
661 end - start + 1, &cmd->argv[i].intval)) {
662 cmd->argv[i].vartype = ARG_TYPE_INT;
663 } else {
664 printf("Unrecognized variable argument.\n");
665 error = 1;
666 }
667 break;
668 case ARG_TYPE_INVALID:
669 default:
670 printf("invalid argument type\n");
671 error = 1;
672 break;
673 }
674 }
675
676 if (error) {
677 spinlock_unlock(&cmd->lock);
678 return NULL;
679 }
680
681 start = end + 1;
682 if (parse_argument(cmdline, len, &start, &end)) {
683 printf("Too many arguments.\n");
684 spinlock_unlock(&cmd->lock);
685 return NULL;
686 }
687
688 spinlock_unlock(&cmd->lock);
689 return cmd;
690}
691
692/** Parse argument.
693 *
694 * Find start and end positions of command line argument.
695 *
696 * @param cmdline Command line as read from the input device.
697 * @param len Number of characters in cmdline.
698 * @param start On entry, 'start' contains pointer to the index
699 * of first unprocessed character of cmdline.
700 * On successful exit, it marks beginning of the next argument.
701 * @param end Undefined on entry. On exit, 'end' points to the last character
702 * of the next argument.
703 *
704 * @return false on failure, true on success.
705 */
706bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
707{
708 index_t i;
709 bool found_start = false;
710
711 ASSERT(start != NULL);
712 ASSERT(end != NULL);
713
714 for (i = *start; i < len; i++) {
715 if (!found_start) {
716 if (isspace(cmdline[i]))
717 (*start)++;
718 else
719 found_start = true;
720 } else {
721 if (isspace(cmdline[i]))
722 break;
723 }
724 }
725 *end = i - 1;
726
727 return found_start;
728}
729
730/** @}
731 */
Note: See TracBrowser for help on using the repository browser.