source: mainline/generic/src/console/kconsole.c@ 3550c393

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3550c393 was 3550c393, checked in by Ondrej Palkovsky <ondrap@…>, 20 years ago

Symtab returns correct addresses even when symtab is befor bss.
Some tab completion optimization.

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