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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3ff2b54 was 939e9c1, checked in by Jakub Jermar <jakub@…>, 17 years ago

Remove left-over debugging stuff.

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