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 <assert.h>
|
---|
42 | #include <console/kconsole.h>
|
---|
43 | #include <console/console.h>
|
---|
44 | #include <console/chardev.h>
|
---|
45 | #include <console/cmd.h>
|
---|
46 | #include <console/prompt.h>
|
---|
47 | #include <print.h>
|
---|
48 | #include <panic.h>
|
---|
49 | #include <typedefs.h>
|
---|
50 | #include <adt/list.h>
|
---|
51 | #include <arch.h>
|
---|
52 | #include <macros.h>
|
---|
53 | #include <debug.h>
|
---|
54 | #include <halt.h>
|
---|
55 | #include <str.h>
|
---|
56 | #include <sysinfo/sysinfo.h>
|
---|
57 | #include <symtab.h>
|
---|
58 | #include <errno.h>
|
---|
59 | #include <putchar.h>
|
---|
60 | #include <mm/slab.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 |
|
---|
85 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */
|
---|
86 | LIST_INITIALIZE(cmd_list); /**< Command list. */
|
---|
87 |
|
---|
88 | static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = { };
|
---|
89 | static 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 | */
|
---|
97 | void 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 | */
|
---|
113 | bool cmd_register(cmd_info_t *cmd)
|
---|
114 | {
|
---|
115 | spinlock_lock(&cmd_lock);
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Make sure the command is not already listed.
|
---|
119 | */
|
---|
120 | list_foreach(cmd_list, link, cmd_info_t, hlp) {
|
---|
121 | if (hlp == cmd) {
|
---|
122 | /* The command is already there. */
|
---|
123 | spinlock_unlock(&cmd_lock);
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /* Avoid deadlock. */
|
---|
128 | if (hlp < cmd) {
|
---|
129 | spinlock_lock(&hlp->lock);
|
---|
130 | spinlock_lock(&cmd->lock);
|
---|
131 | } else {
|
---|
132 | spinlock_lock(&cmd->lock);
|
---|
133 | spinlock_lock(&hlp->lock);
|
---|
134 | }
|
---|
135 |
|
---|
136 | if (str_cmp(hlp->name, cmd->name) == 0) {
|
---|
137 | /* The command is already there. */
|
---|
138 | spinlock_unlock(&hlp->lock);
|
---|
139 | spinlock_unlock(&cmd->lock);
|
---|
140 | spinlock_unlock(&cmd_lock);
|
---|
141 | return false;
|
---|
142 | }
|
---|
143 |
|
---|
144 | spinlock_unlock(&hlp->lock);
|
---|
145 | spinlock_unlock(&cmd->lock);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Now the command can be added.
|
---|
150 | */
|
---|
151 | list_append(&cmd->link, &cmd_list);
|
---|
152 |
|
---|
153 | spinlock_unlock(&cmd_lock);
|
---|
154 | return true;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** Print count times a character */
|
---|
158 | NO_TRACE static void print_cc(wchar_t ch, size_t count)
|
---|
159 | {
|
---|
160 | size_t i;
|
---|
161 | for (i = 0; i < count; i++)
|
---|
162 | putchar(ch);
|
---|
163 | }
|
---|
164 |
|
---|
165 | /** Try to find a command beginning with prefix */
|
---|
166 | const char *cmdtab_enum(const char *name, const char **h, void **ctx)
|
---|
167 | {
|
---|
168 | link_t **startpos = (link_t **) ctx;
|
---|
169 | size_t namelen = str_length(name);
|
---|
170 |
|
---|
171 | spinlock_lock(&cmd_lock);
|
---|
172 |
|
---|
173 | if (*startpos == NULL)
|
---|
174 | *startpos = cmd_list.head.next;
|
---|
175 |
|
---|
176 | for (; *startpos != &cmd_list.head; *startpos = (*startpos)->next) {
|
---|
177 | cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link);
|
---|
178 |
|
---|
179 | const char *curname = hlp->name;
|
---|
180 | if (str_length(curname) < namelen)
|
---|
181 | continue;
|
---|
182 |
|
---|
183 | if (str_lcmp(curname, name, namelen) == 0) {
|
---|
184 | *startpos = (*startpos)->next;
|
---|
185 | if (h)
|
---|
186 | *h = hlp->description;
|
---|
187 |
|
---|
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 | */
|
---|
205 | NO_TRACE static int cmdtab_compl(char *input, size_t size, indev_t *indev,
|
---|
206 | hints_enum_func_t hints_enum)
|
---|
207 | {
|
---|
208 | const char *name = input;
|
---|
209 |
|
---|
210 | size_t found = 0;
|
---|
211 |
|
---|
212 | /*
|
---|
213 | * Maximum Match Length: Length of longest matching common
|
---|
214 | * substring in case more than one match is found.
|
---|
215 | */
|
---|
216 | size_t max_match_len = size;
|
---|
217 | size_t max_match_len_tmp = size;
|
---|
218 | void *pos = NULL;
|
---|
219 | const char *hint;
|
---|
220 | const char *help;
|
---|
221 | char *output = malloc(MAX_CMDLINE, 0);
|
---|
222 | size_t hints_to_show = MAX_TAB_HINTS - 1;
|
---|
223 | size_t total_hints_shown = 0;
|
---|
224 | bool continue_showing_hints = true;
|
---|
225 |
|
---|
226 | output[0] = 0;
|
---|
227 |
|
---|
228 | while ((hint = hints_enum(name, NULL, &pos))) {
|
---|
229 | if ((found == 0) || (str_length(hint) > str_length(output)))
|
---|
230 | str_cpy(output, MAX_CMDLINE, hint);
|
---|
231 |
|
---|
232 | found++;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * If the number of possible completions is more than MAX_TAB_HINTS,
|
---|
237 | * ask the user whether to display them or not.
|
---|
238 | */
|
---|
239 | if (found > MAX_TAB_HINTS) {
|
---|
240 | printf("\n");
|
---|
241 | continue_showing_hints =
|
---|
242 | console_prompt_display_all_hints(indev, found);
|
---|
243 | }
|
---|
244 |
|
---|
245 | if ((found > 1) && (str_length(output) != 0)) {
|
---|
246 | printf("\n");
|
---|
247 | pos = NULL;
|
---|
248 | while ((hint = hints_enum(name, &help, &pos))) {
|
---|
249 |
|
---|
250 | if (continue_showing_hints) {
|
---|
251 | if (help)
|
---|
252 | printf("%s%s (%s)\n", name, hint, help);
|
---|
253 | else
|
---|
254 | printf("%s%s\n", name, hint);
|
---|
255 |
|
---|
256 | --hints_to_show;
|
---|
257 | ++total_hints_shown;
|
---|
258 |
|
---|
259 | if ((hints_to_show == 0) && (total_hints_shown != found)) {
|
---|
260 | /* Ask user to continue */
|
---|
261 | continue_showing_hints =
|
---|
262 | console_prompt_more_hints(indev, &hints_to_show);
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | max_match_len_tmp = 0;
|
---|
267 | while ((output[max_match_len_tmp] ==
|
---|
268 | hint[max_match_len_tmp]) &&
|
---|
269 | (max_match_len_tmp < max_match_len))
|
---|
270 | ++max_match_len_tmp;
|
---|
271 |
|
---|
272 | max_match_len = max_match_len_tmp;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /* Keep only the characters common in all completions */
|
---|
276 | output[max_match_len] = 0;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (found > 0)
|
---|
280 | str_cpy(input, size, output);
|
---|
281 |
|
---|
282 | free(output);
|
---|
283 | return found;
|
---|
284 | }
|
---|
285 |
|
---|
286 | NO_TRACE static cmd_info_t *parse_cmd(const wchar_t *cmdline)
|
---|
287 | {
|
---|
288 | size_t start = 0;
|
---|
289 | size_t end;
|
---|
290 | char *tmp;
|
---|
291 |
|
---|
292 | while (isspace(cmdline[start]))
|
---|
293 | start++;
|
---|
294 |
|
---|
295 | end = start + 1;
|
---|
296 |
|
---|
297 | while (!isspace(cmdline[end]))
|
---|
298 | end++;
|
---|
299 |
|
---|
300 | tmp = malloc(STR_BOUNDS(end - start + 1), 0);
|
---|
301 |
|
---|
302 | wstr_to_str(tmp, end - start + 1, &cmdline[start]);
|
---|
303 |
|
---|
304 | spinlock_lock(&cmd_lock);
|
---|
305 |
|
---|
306 | list_foreach(cmd_list, link, cmd_info_t, hlp) {
|
---|
307 | spinlock_lock(&hlp->lock);
|
---|
308 |
|
---|
309 | if (str_cmp(hlp->name, tmp) == 0) {
|
---|
310 | spinlock_unlock(&hlp->lock);
|
---|
311 | spinlock_unlock(&cmd_lock);
|
---|
312 | free(tmp);
|
---|
313 | return hlp;
|
---|
314 | }
|
---|
315 |
|
---|
316 | spinlock_unlock(&hlp->lock);
|
---|
317 | }
|
---|
318 |
|
---|
319 | free(tmp);
|
---|
320 | spinlock_unlock(&cmd_lock);
|
---|
321 |
|
---|
322 | return NULL;
|
---|
323 | }
|
---|
324 |
|
---|
325 | NO_TRACE static wchar_t *clever_readline(const char *prompt, indev_t *indev)
|
---|
326 | {
|
---|
327 | printf("%s> ", prompt);
|
---|
328 |
|
---|
329 | size_t position = 0;
|
---|
330 | wchar_t *current = history[history_pos];
|
---|
331 | current[0] = 0;
|
---|
332 | char *tmp = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
|
---|
333 |
|
---|
334 | while (true) {
|
---|
335 | wchar_t ch = indev_pop_character(indev);
|
---|
336 |
|
---|
337 | if (ch == '\n') {
|
---|
338 | /* Enter */
|
---|
339 | putchar(ch);
|
---|
340 | break;
|
---|
341 | }
|
---|
342 |
|
---|
343 | if (ch == '\b') {
|
---|
344 | /* Backspace */
|
---|
345 | if (position == 0)
|
---|
346 | continue;
|
---|
347 |
|
---|
348 | if (wstr_remove(current, position - 1)) {
|
---|
349 | position--;
|
---|
350 | putchar('\b');
|
---|
351 | printf("%ls ", current + position);
|
---|
352 | print_cc('\b', wstr_length(current) - position + 1);
|
---|
353 | continue;
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | if (ch == '\t') {
|
---|
358 | /* Tab completion */
|
---|
359 |
|
---|
360 | /* Move to the end of the word */
|
---|
361 | for (; (current[position] != 0) && (!isspace(current[position]));
|
---|
362 | position++)
|
---|
363 | putchar(current[position]);
|
---|
364 |
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * Find the beginning of the word
|
---|
368 | * and copy it to tmp
|
---|
369 | */
|
---|
370 | size_t beg;
|
---|
371 | unsigned narg = 0;
|
---|
372 | if (position == 0) {
|
---|
373 | tmp[0] = '\0';
|
---|
374 | beg = 0;
|
---|
375 | } else {
|
---|
376 | beg = position - 1;
|
---|
377 | while ((beg > 0) && (!isspace(current[beg])))
|
---|
378 | beg--;
|
---|
379 |
|
---|
380 | if (isspace(current[beg]))
|
---|
381 | beg++;
|
---|
382 |
|
---|
383 | wstr_to_str(tmp, position - beg + 1, current + beg);
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* Count which argument number are we tabbing (narg=0 is cmd) */
|
---|
387 | bool sp = false;
|
---|
388 | for (; beg > 0; beg--) {
|
---|
389 | if (isspace(current[beg])) {
|
---|
390 | if (!sp) {
|
---|
391 | narg++;
|
---|
392 | sp = true;
|
---|
393 | }
|
---|
394 | } else
|
---|
395 | sp = false;
|
---|
396 | }
|
---|
397 |
|
---|
398 | if (narg && isspace(current[0]))
|
---|
399 | narg--;
|
---|
400 |
|
---|
401 | int found;
|
---|
402 | if (narg == 0) {
|
---|
403 | /* Command completion */
|
---|
404 | found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev,
|
---|
405 | cmdtab_enum);
|
---|
406 | } else {
|
---|
407 | /* Arguments completion */
|
---|
408 | cmd_info_t *cmd = parse_cmd(current);
|
---|
409 | if (!cmd || !cmd->hints_enum || cmd->argc < narg)
|
---|
410 | continue;
|
---|
411 | found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev,
|
---|
412 | cmd->hints_enum);
|
---|
413 | }
|
---|
414 |
|
---|
415 | if (found == 0)
|
---|
416 | continue;
|
---|
417 |
|
---|
418 | /*
|
---|
419 | * We have hints, possibly many. In case of more than one hint,
|
---|
420 | * tmp will contain the common prefix.
|
---|
421 | */
|
---|
422 | size_t off = 0;
|
---|
423 | size_t i = 0;
|
---|
424 | while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) {
|
---|
425 | if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
|
---|
426 | break;
|
---|
427 |
|
---|
428 | i++;
|
---|
429 | }
|
---|
430 |
|
---|
431 | if (found > 1) {
|
---|
432 | /* No unique hint, list was printed */
|
---|
433 | printf("%s> ", prompt);
|
---|
434 | printf("%ls", current);
|
---|
435 | position += str_length(tmp);
|
---|
436 | print_cc('\b', wstr_length(current) - position);
|
---|
437 | continue;
|
---|
438 | }
|
---|
439 |
|
---|
440 | /* We have a hint */
|
---|
441 |
|
---|
442 | printf("%ls", current + position);
|
---|
443 | position += str_length(tmp);
|
---|
444 | print_cc('\b', wstr_length(current) - position);
|
---|
445 |
|
---|
446 | if (position == wstr_length(current)) {
|
---|
447 | /* Insert a space after the last completed argument */
|
---|
448 | if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) {
|
---|
449 | printf("%ls", current + position);
|
---|
450 | position++;
|
---|
451 | }
|
---|
452 | }
|
---|
453 | continue;
|
---|
454 | }
|
---|
455 |
|
---|
456 | if (ch == U_LEFT_ARROW) {
|
---|
457 | /* Left */
|
---|
458 | if (position > 0) {
|
---|
459 | putchar('\b');
|
---|
460 | position--;
|
---|
461 | }
|
---|
462 | continue;
|
---|
463 | }
|
---|
464 |
|
---|
465 | if (ch == U_RIGHT_ARROW) {
|
---|
466 | /* Right */
|
---|
467 | if (position < wstr_length(current)) {
|
---|
468 | putchar(current[position]);
|
---|
469 | position++;
|
---|
470 | }
|
---|
471 | continue;
|
---|
472 | }
|
---|
473 |
|
---|
474 | if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) {
|
---|
475 | /* Up, down */
|
---|
476 | print_cc('\b', position);
|
---|
477 | print_cc(' ', wstr_length(current));
|
---|
478 | print_cc('\b', wstr_length(current));
|
---|
479 |
|
---|
480 | if (ch == U_UP_ARROW) {
|
---|
481 | /* Up */
|
---|
482 | if (history_pos == 0)
|
---|
483 | history_pos = KCONSOLE_HISTORY - 1;
|
---|
484 | else
|
---|
485 | history_pos--;
|
---|
486 | } else {
|
---|
487 | /* Down */
|
---|
488 | history_pos++;
|
---|
489 | history_pos = history_pos % KCONSOLE_HISTORY;
|
---|
490 | }
|
---|
491 | current = history[history_pos];
|
---|
492 | printf("%ls", current);
|
---|
493 | position = wstr_length(current);
|
---|
494 | continue;
|
---|
495 | }
|
---|
496 |
|
---|
497 | if (ch == U_HOME_ARROW) {
|
---|
498 | /* Home */
|
---|
499 | print_cc('\b', position);
|
---|
500 | position = 0;
|
---|
501 | continue;
|
---|
502 | }
|
---|
503 |
|
---|
504 | if (ch == U_END_ARROW) {
|
---|
505 | /* End */
|
---|
506 | printf("%ls", current + position);
|
---|
507 | position = wstr_length(current);
|
---|
508 | continue;
|
---|
509 | }
|
---|
510 |
|
---|
511 | if (ch == U_DELETE) {
|
---|
512 | /* Delete */
|
---|
513 | if (position == wstr_length(current))
|
---|
514 | continue;
|
---|
515 |
|
---|
516 | if (wstr_remove(current, position)) {
|
---|
517 | printf("%ls ", current + position);
|
---|
518 | print_cc('\b', wstr_length(current) - position + 1);
|
---|
519 | }
|
---|
520 | continue;
|
---|
521 | }
|
---|
522 |
|
---|
523 | if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
|
---|
524 | printf("%ls", current + position);
|
---|
525 | position++;
|
---|
526 | print_cc('\b', wstr_length(current) - position);
|
---|
527 | }
|
---|
528 | }
|
---|
529 |
|
---|
530 | if (wstr_length(current) > 0) {
|
---|
531 | history_pos++;
|
---|
532 | history_pos = history_pos % KCONSOLE_HISTORY;
|
---|
533 | }
|
---|
534 |
|
---|
535 | free(tmp);
|
---|
536 | return current;
|
---|
537 | }
|
---|
538 |
|
---|
539 | bool kconsole_check_poll(void)
|
---|
540 | {
|
---|
541 | return check_poll(stdin);
|
---|
542 | }
|
---|
543 |
|
---|
544 | NO_TRACE static bool parse_int_arg(const char *text, size_t len,
|
---|
545 | sysarg_t *result)
|
---|
546 | {
|
---|
547 | bool isaddr = false;
|
---|
548 | bool isptr = false;
|
---|
549 |
|
---|
550 | /* If we get a name, try to find it in symbol table */
|
---|
551 | if (text[0] == '&') {
|
---|
552 | isaddr = true;
|
---|
553 | text++;
|
---|
554 | len--;
|
---|
555 | } else if (text[0] == '*') {
|
---|
556 | isptr = true;
|
---|
557 | text++;
|
---|
558 | len--;
|
---|
559 | }
|
---|
560 |
|
---|
561 | if ((text[0] < '0') || (text[0] > '9')) {
|
---|
562 | char symname[MAX_SYMBOL_NAME];
|
---|
563 | str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1);
|
---|
564 |
|
---|
565 | uintptr_t symaddr;
|
---|
566 | errno_t rc = symtab_addr_lookup(symname, &symaddr);
|
---|
567 | switch (rc) {
|
---|
568 | case ENOENT:
|
---|
569 | printf("Symbol %s not found.\n", symname);
|
---|
570 | return false;
|
---|
571 | case EOVERFLOW:
|
---|
572 | printf("Duplicate symbol %s.\n", symname);
|
---|
573 | symtab_print_search(symname);
|
---|
574 | return false;
|
---|
575 | case ENOTSUP:
|
---|
576 | printf("No symbol information available.\n");
|
---|
577 | return false;
|
---|
578 | case EOK:
|
---|
579 | if (isaddr)
|
---|
580 | *result = (sysarg_t) symaddr;
|
---|
581 | else if (isptr)
|
---|
582 | *result = **((sysarg_t **) symaddr);
|
---|
583 | else
|
---|
584 | *result = *((sysarg_t *) symaddr);
|
---|
585 | break;
|
---|
586 | default:
|
---|
587 | printf("Unknown error.\n");
|
---|
588 | return false;
|
---|
589 | }
|
---|
590 | } else {
|
---|
591 | /* It's a number - convert it */
|
---|
592 | uint64_t value;
|
---|
593 | char *end;
|
---|
594 | errno_t rc = str_uint64_t(text, &end, 0, false, &value);
|
---|
595 | if (end != text + len)
|
---|
596 | rc = EINVAL;
|
---|
597 | switch (rc) {
|
---|
598 | case EINVAL:
|
---|
599 | printf("Invalid number '%s'.\n", text);
|
---|
600 | return false;
|
---|
601 | case EOVERFLOW:
|
---|
602 | printf("Integer overflow in '%s'.\n", text);
|
---|
603 | return false;
|
---|
604 | case EOK:
|
---|
605 | *result = (sysarg_t) value;
|
---|
606 | if (isptr)
|
---|
607 | *result = *((sysarg_t *) *result);
|
---|
608 | break;
|
---|
609 | default:
|
---|
610 | printf("Unknown error parsing '%s'.\n", text);
|
---|
611 | return false;
|
---|
612 | }
|
---|
613 | }
|
---|
614 |
|
---|
615 | return true;
|
---|
616 | }
|
---|
617 |
|
---|
618 | /** Parse argument.
|
---|
619 | *
|
---|
620 | * Find start and end positions of command line argument.
|
---|
621 | *
|
---|
622 | * @param cmdline Command line as read from the input device.
|
---|
623 | * @param size Size (in bytes) of the string.
|
---|
624 | * @param start On entry, 'start' contains pointer to the offset
|
---|
625 | * of the first unprocessed character of cmdline.
|
---|
626 | * On successful exit, it marks beginning of the next argument.
|
---|
627 | * @param end Undefined on entry. On exit, 'end' is the offset of the first
|
---|
628 | * character behind the next argument.
|
---|
629 | *
|
---|
630 | * @return False on failure, true on success.
|
---|
631 | *
|
---|
632 | */
|
---|
633 | NO_TRACE static bool parse_argument(const char *cmdline, size_t size,
|
---|
634 | size_t *start, size_t *end)
|
---|
635 | {
|
---|
636 | assert(start != NULL);
|
---|
637 | assert(end != NULL);
|
---|
638 |
|
---|
639 | bool found_start = false;
|
---|
640 | size_t offset = *start;
|
---|
641 | size_t prev = *start;
|
---|
642 | wchar_t ch;
|
---|
643 |
|
---|
644 | while ((ch = str_decode(cmdline, &offset, size)) != 0) {
|
---|
645 | if (!found_start) {
|
---|
646 | if (!isspace(ch)) {
|
---|
647 | *start = prev;
|
---|
648 | found_start = true;
|
---|
649 | }
|
---|
650 | } else {
|
---|
651 | if (isspace(ch))
|
---|
652 | break;
|
---|
653 | }
|
---|
654 |
|
---|
655 | prev = offset;
|
---|
656 | }
|
---|
657 | *end = prev;
|
---|
658 |
|
---|
659 | return found_start;
|
---|
660 | }
|
---|
661 |
|
---|
662 | /** Parse command line.
|
---|
663 | *
|
---|
664 | * @param cmdline Command line as read from input device.
|
---|
665 | * @param size Size (in bytes) of the string.
|
---|
666 | *
|
---|
667 | * @return Structure describing the command.
|
---|
668 | *
|
---|
669 | */
|
---|
670 | NO_TRACE static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
|
---|
671 | {
|
---|
672 | size_t start = 0;
|
---|
673 | size_t end = 0;
|
---|
674 | if (!parse_argument(cmdline, size, &start, &end)) {
|
---|
675 | /* Command line did not contain alphanumeric word. */
|
---|
676 | return NULL;
|
---|
677 | }
|
---|
678 | spinlock_lock(&cmd_lock);
|
---|
679 |
|
---|
680 | cmd_info_t *cmd = NULL;
|
---|
681 |
|
---|
682 | list_foreach(cmd_list, link, cmd_info_t, hlp) {
|
---|
683 | spinlock_lock(&hlp->lock);
|
---|
684 |
|
---|
685 | if (str_lcmp(hlp->name, cmdline + start,
|
---|
686 | max(str_length(hlp->name),
|
---|
687 | str_nlength(cmdline + start, (size_t) (end - start)))) == 0) {
|
---|
688 | cmd = hlp;
|
---|
689 | break;
|
---|
690 | }
|
---|
691 |
|
---|
692 | spinlock_unlock(&hlp->lock);
|
---|
693 | }
|
---|
694 |
|
---|
695 | spinlock_unlock(&cmd_lock);
|
---|
696 |
|
---|
697 | if (!cmd) {
|
---|
698 | /* Unknown command. */
|
---|
699 | printf("Unknown command.\n");
|
---|
700 | return NULL;
|
---|
701 | }
|
---|
702 |
|
---|
703 | /* cmd == hlp is locked */
|
---|
704 |
|
---|
705 | /*
|
---|
706 | * The command line must be further analyzed and
|
---|
707 | * the parameters therefrom must be matched and
|
---|
708 | * converted to those specified in the cmd info
|
---|
709 | * structure.
|
---|
710 | */
|
---|
711 |
|
---|
712 | bool error = false;
|
---|
713 | size_t i;
|
---|
714 | for (i = 0; i < cmd->argc; i++) {
|
---|
715 | char *buf;
|
---|
716 |
|
---|
717 | start = end;
|
---|
718 | if (!parse_argument(cmdline, size, &start, &end)) {
|
---|
719 | if (cmd->argv[i].type == ARG_TYPE_STRING_OPTIONAL) {
|
---|
720 | buf = (char *) cmd->argv[i].buffer;
|
---|
721 | str_cpy(buf, cmd->argv[i].len, "");
|
---|
722 | continue;
|
---|
723 | }
|
---|
724 |
|
---|
725 | printf("Too few arguments.\n");
|
---|
726 | spinlock_unlock(&cmd->lock);
|
---|
727 | return NULL;
|
---|
728 | }
|
---|
729 |
|
---|
730 | switch (cmd->argv[i].type) {
|
---|
731 | case ARG_TYPE_STRING:
|
---|
732 | case ARG_TYPE_STRING_OPTIONAL:
|
---|
733 | buf = (char *) cmd->argv[i].buffer;
|
---|
734 | str_ncpy(buf, cmd->argv[i].len, cmdline + start,
|
---|
735 | end - start);
|
---|
736 | break;
|
---|
737 | case ARG_TYPE_INT:
|
---|
738 | if (!parse_int_arg(cmdline + start, end - start,
|
---|
739 | &cmd->argv[i].intval))
|
---|
740 | error = true;
|
---|
741 | break;
|
---|
742 | case ARG_TYPE_VAR:
|
---|
743 | if ((start < end - 1) && (cmdline[start] == '"')) {
|
---|
744 | if (cmdline[end - 1] == '"') {
|
---|
745 | buf = (char *) cmd->argv[i].buffer;
|
---|
746 | str_ncpy(buf, cmd->argv[i].len,
|
---|
747 | cmdline + start + 1,
|
---|
748 | (end - start) - 1);
|
---|
749 | cmd->argv[i].intval = (sysarg_t) buf;
|
---|
750 | cmd->argv[i].vartype = ARG_TYPE_STRING;
|
---|
751 | } else {
|
---|
752 | printf("Wrong syntax.\n");
|
---|
753 | error = true;
|
---|
754 | }
|
---|
755 | } else if (parse_int_arg(cmdline + start,
|
---|
756 | end - start, &cmd->argv[i].intval)) {
|
---|
757 | cmd->argv[i].vartype = ARG_TYPE_INT;
|
---|
758 | } else {
|
---|
759 | printf("Unrecognized variable argument.\n");
|
---|
760 | error = true;
|
---|
761 | }
|
---|
762 | break;
|
---|
763 | case ARG_TYPE_INVALID:
|
---|
764 | default:
|
---|
765 | printf("Invalid argument type\n");
|
---|
766 | error = true;
|
---|
767 | break;
|
---|
768 | }
|
---|
769 | }
|
---|
770 |
|
---|
771 | if (error) {
|
---|
772 | spinlock_unlock(&cmd->lock);
|
---|
773 | return NULL;
|
---|
774 | }
|
---|
775 |
|
---|
776 | start = end;
|
---|
777 | if (parse_argument(cmdline, size, &start, &end)) {
|
---|
778 | printf("Too many arguments.\n");
|
---|
779 | spinlock_unlock(&cmd->lock);
|
---|
780 | return NULL;
|
---|
781 | }
|
---|
782 |
|
---|
783 | spinlock_unlock(&cmd->lock);
|
---|
784 | return cmd;
|
---|
785 | }
|
---|
786 |
|
---|
787 | /** Kernel console prompt.
|
---|
788 | *
|
---|
789 | * @param prompt Kernel console prompt (e.g kconsole/panic).
|
---|
790 | * @param msg Message to display in the beginning.
|
---|
791 | * @param kcon Wait for keypress to show the prompt
|
---|
792 | * and never exit.
|
---|
793 | *
|
---|
794 | */
|
---|
795 | void kconsole(const char *prompt, const char *msg, bool kcon)
|
---|
796 | {
|
---|
797 | if (!stdin) {
|
---|
798 | LOG("No stdin for kernel console");
|
---|
799 | return;
|
---|
800 | }
|
---|
801 |
|
---|
802 | if (msg)
|
---|
803 | printf("%s", msg);
|
---|
804 |
|
---|
805 | if (kcon)
|
---|
806 | indev_pop_character(stdin);
|
---|
807 | else
|
---|
808 | printf("Type \"exit\" to leave the console.\n");
|
---|
809 |
|
---|
810 | char *cmdline = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
|
---|
811 | while (true) {
|
---|
812 | wchar_t *tmp = clever_readline((char *) prompt, stdin);
|
---|
813 | size_t len = wstr_length(tmp);
|
---|
814 | if (!len)
|
---|
815 | continue;
|
---|
816 |
|
---|
817 | wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
|
---|
818 |
|
---|
819 | if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
|
---|
820 | break;
|
---|
821 |
|
---|
822 | cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
|
---|
823 | if (!cmd_info)
|
---|
824 | continue;
|
---|
825 |
|
---|
826 | (void) cmd_info->func(cmd_info->argv);
|
---|
827 | }
|
---|
828 | free(cmdline);
|
---|
829 | }
|
---|
830 |
|
---|
831 | /** Kernel console managing thread.
|
---|
832 | *
|
---|
833 | */
|
---|
834 | void kconsole_thread(void *data)
|
---|
835 | {
|
---|
836 | kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
|
---|
837 | }
|
---|
838 |
|
---|
839 | /** @}
|
---|
840 | */
|
---|