Subversion Repositories HelenOS-historic

Rev

Rev 582 | Rev 589 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 582 Rev 585
1
/*
1
/*
2
 * Copyright (C) 2005 Jakub Jermar
2
 * Copyright (C) 2005 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
#include <console/kconsole.h>
29
#include <console/kconsole.h>
30
#include <console/console.h>
30
#include <console/console.h>
31
#include <console/chardev.h>
31
#include <console/chardev.h>
32
#include <print.h>
32
#include <print.h>
33
#include <panic.h>
33
#include <panic.h>
34
#include <typedefs.h>
34
#include <typedefs.h>
35
#include <arch/types.h>
35
#include <arch/types.h>
36
#include <list.h>
36
#include <list.h>
37
#include <arch.h>
37
#include <arch.h>
38
#include <func.h>
38
#include <func.h>
39
#include <macros.h>
39
#include <macros.h>
40
#include <debug.h>
40
#include <debug.h>
41
#include <symtab.h>
41
#include <symtab.h>
42
 
42
 
43
#define MAX_CMDLINE 256
43
#define MAX_CMDLINE 256
44
 
44
 
45
/** Simple kernel console.
45
/** Simple kernel console.
46
 *
46
 *
47
 * The console is realized by kernel thread kconsole.
47
 * The console is realized by kernel thread kconsole.
48
 * It doesn't understand any useful command on its own,
48
 * It doesn't understand any useful command on its own,
49
 * but makes it possible for other kernel subsystems to
49
 * but makes it possible for other kernel subsystems to
50
 * register their own commands.
50
 * register their own commands.
51
 */
51
 */
52
 
52
 
53
/** Locking.
53
/** Locking.
54
 *
54
 *
55
 * There is a list of cmd_info_t structures. This list
55
 * There is a list of cmd_info_t structures. This list
56
 * is protected by cmd_lock spinlock. Note that specially
56
 * is protected by cmd_lock spinlock. Note that specially
57
 * the link elements of cmd_info_t are protected by
57
 * the link elements of cmd_info_t are protected by
58
 * this lock.
58
 * this lock.
59
 *
59
 *
60
 * Each cmd_info_t also has its own lock, which protects
60
 * Each cmd_info_t also has its own lock, which protects
61
 * all elements thereof except the link element.
61
 * all elements thereof except the link element.
62
 *
62
 *
63
 * cmd_lock must be acquired before any cmd_info lock.
63
 * cmd_lock must be acquired before any cmd_info lock.
64
 * When locking two cmd info structures, structure with
64
 * When locking two cmd info structures, structure with
65
 * lower address must be locked first.
65
 * lower address must be locked first.
66
 */
66
 */
67
 
67
 
68
spinlock_t cmd_lock;    /**< Lock protecting command list. */
68
spinlock_t cmd_lock;    /**< Lock protecting command list. */
69
link_t cmd_head;    /**< Command list. */
69
link_t cmd_head;    /**< Command list. */
70
 
70
 
71
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
71
static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
72
static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end);
72
static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end);
73
 
73
 
74
/** Data and methods for 'help' command. */
74
/** Data and methods for 'help' command. */
75
static int cmd_help(cmd_arg_t *argv);
75
static int cmd_help(cmd_arg_t *argv);
76
static cmd_info_t help_info = {
76
static cmd_info_t help_info = {
77
    .name = "help",
77
    .name = "help",
78
    .description = "List of supported commands.",
78
    .description = "List of supported commands.",
79
    .func = cmd_help,
79
    .func = cmd_help,
80
    .argc = 0
80
    .argc = 0
81
};
81
};
82
 
82
 
83
/** Data and methods for 'description' command. */
83
/** Data and methods for 'description' command. */
84
static int cmd_desc(cmd_arg_t *argv);
84
static int cmd_desc(cmd_arg_t *argv);
85
static void desc_help(void);
85
static void desc_help(void);
86
static char desc_buf[MAX_CMDLINE+1];
86
static char desc_buf[MAX_CMDLINE+1];
87
static cmd_arg_t desc_argv = {
87
static cmd_arg_t desc_argv = {
88
    .type = ARG_TYPE_STRING,
88
    .type = ARG_TYPE_STRING,
89
    .buffer = desc_buf,
89
    .buffer = desc_buf,
90
    .len = sizeof(desc_buf)
90
    .len = sizeof(desc_buf)
91
};
91
};
92
static cmd_info_t desc_info = {
92
static cmd_info_t desc_info = {
93
    .name = "describe",
93
    .name = "describe",
94
    .description = "Describe specified command.",
94
    .description = "Describe specified command.",
95
    .help = desc_help,
95
    .help = desc_help,
96
    .func = cmd_desc,
96
    .func = cmd_desc,
97
    .argc = 1,
97
    .argc = 1,
98
    .argv = &desc_argv
98
    .argv = &desc_argv
99
};
99
};
100
 
100
 
101
/** Data and methods for 'symaddr' command. */
101
/** Data and methods for 'symaddr' command. */
102
static int cmd_symaddr(cmd_arg_t *argv);
102
static int cmd_symaddr(cmd_arg_t *argv);
103
static char symaddr_buf[MAX_CMDLINE+1];
103
static char symaddr_buf[MAX_CMDLINE+1];
104
static cmd_arg_t symaddr_argv = {
104
static cmd_arg_t symaddr_argv = {
105
    .type = ARG_TYPE_STRING,
105
    .type = ARG_TYPE_STRING,
106
    .buffer = symaddr_buf,
106
    .buffer = symaddr_buf,
107
    .len = sizeof(symaddr_buf)
107
    .len = sizeof(symaddr_buf)
108
};
108
};
109
static cmd_info_t symaddr_info = {
109
static cmd_info_t symaddr_info = {
110
    .name = "symaddr",
110
    .name = "symaddr",
111
    .description = "Return symbol address.",
111
    .description = "Return symbol address.",
112
    .func = cmd_symaddr,
112
    .func = cmd_symaddr,
113
    .argc = 1,
113
    .argc = 1,
114
    .argv = &symaddr_argv
114
    .argv = &symaddr_argv
115
};
115
};
116
 
116
 
117
/** Call0 - call function with no parameters */
117
/** Call0 - call function with no parameters */
118
static char call0_buf[MAX_CMDLINE+1];
118
static char call0_buf[MAX_CMDLINE+1];
-
 
119
static char carg1_buf[MAX_CMDLINE+1];
-
 
120
static char carg2_buf[MAX_CMDLINE+1];
119
 
121
 
120
static int cmd_call0(cmd_arg_t *argv);
122
static int cmd_call0(cmd_arg_t *argv);
121
static cmd_arg_t call0_argv = {
123
static cmd_arg_t call0_argv = {
122
    .type = ARG_TYPE_STRING,
124
    .type = ARG_TYPE_STRING,
123
    .buffer = call0_buf,
125
    .buffer = call0_buf,
124
    .len = sizeof(call0_buf)
126
    .len = sizeof(call0_buf)
125
};
127
};
126
static cmd_info_t call0_info = {
128
static cmd_info_t call0_info = {
127
    .name = "call0",
129
    .name = "call0",
128
    .description = "call0 <function> -> call function().",
130
    .description = "call0 <function> -> call function().",
129
    .func = cmd_call0,
131
    .func = cmd_call0,
130
    .argc = 1,
132
    .argc = 1,
131
    .argv = &call0_argv
133
    .argv = &call0_argv
132
};
134
};
133
 
135
 
134
static int cmd_call1(cmd_arg_t *argv);
136
static int cmd_call1(cmd_arg_t *argv);
135
static cmd_arg_t call1_argv[] = {
137
static cmd_arg_t call1_argv[] = {
136
    {
138
    {
137
        .type = ARG_TYPE_STRING,
139
        .type = ARG_TYPE_STRING,
138
        .buffer = call0_buf,
140
        .buffer = call0_buf,
139
        .len = sizeof(call0_buf)
141
        .len = sizeof(call0_buf)
140
    },
142
    },
-
 
143
    {
141
    { .type = ARG_TYPE_INT }
144
        .type = ARG_TYPE_VAR,
-
 
145
        .buffer = carg1_buf,
-
 
146
        .len = sizeof(carg1_buf)
-
 
147
    }
142
};
148
};
143
static cmd_info_t call1_info = {
149
static cmd_info_t call1_info = {
144
    .name = "call1",
150
    .name = "call1",
145
    .description = "call1 <function> <arg1> -> call function(arg1).",
151
    .description = "call1 <function> <arg1> -> call function(arg1).",
146
    .func = cmd_call1,
152
    .func = cmd_call1,
147
    .argc = 2,
153
    .argc = 2,
148
    .argv = call1_argv
154
    .argv = call1_argv
149
};
155
};
150
 
156
 
151
static int cmd_call2(cmd_arg_t *argv);
157
static int cmd_call2(cmd_arg_t *argv);
152
static cmd_arg_t call2_argv[] = {
158
static cmd_arg_t call2_argv[] = {
153
    {
159
    {
154
        .type = ARG_TYPE_STRING,
160
        .type = ARG_TYPE_STRING,
155
        .buffer = call0_buf,
161
        .buffer = call0_buf,
156
        .len = sizeof(call0_buf)
162
        .len = sizeof(call0_buf)
157
    },
163
    },
-
 
164
    {
158
    { .type = ARG_TYPE_INT },
165
        .type = ARG_TYPE_VAR,
-
 
166
        .buffer = carg1_buf,
-
 
167
        .len = sizeof(carg1_buf)
-
 
168
    },
-
 
169
    {
159
    { .type = ARG_TYPE_INT }
170
        .type = ARG_TYPE_VAR,
-
 
171
        .buffer = carg2_buf,
-
 
172
        .len = sizeof(carg2_buf)
-
 
173
    }
160
};
174
};
161
static cmd_info_t call2_info = {
175
static cmd_info_t call2_info = {
162
    .name = "call2",
176
    .name = "call2",
163
    .description = "call2 <function> <arg1> <arg2> -> call function(arg1,arg2).",
177
    .description = "call2 <function> <arg1> <arg2> -> call function(arg1,arg2).",
164
    .func = cmd_call2,
178
    .func = cmd_call2,
165
    .argc = 3,
179
    .argc = 3,
166
    .argv = call2_argv
180
    .argv = call2_argv
167
};
181
};
168
 
182
 
169
 
183
 
170
/** Data and methods for 'halt' command. */
184
/** Data and methods for 'halt' command. */
171
static int cmd_halt(cmd_arg_t *argv);
185
static int cmd_halt(cmd_arg_t *argv);
172
static cmd_info_t halt_info = {
186
static cmd_info_t halt_info = {
173
    .name = "halt",
187
    .name = "halt",
174
    .description = "Halt the kernel.",
188
    .description = "Halt the kernel.",
175
    .func = cmd_halt,
189
    .func = cmd_halt,
176
    .argc = 0
190
    .argc = 0
177
};
191
};
178
 
192
 
179
/** Initialize kconsole data structures. */
193
/** Initialize kconsole data structures. */
180
void kconsole_init(void)
194
void kconsole_init(void)
181
{
195
{
182
    spinlock_initialize(&cmd_lock, "kconsole_cmd");
196
    spinlock_initialize(&cmd_lock, "kconsole_cmd");
183
    list_initialize(&cmd_head);
197
    list_initialize(&cmd_head);
184
   
198
   
185
    spinlock_initialize(&help_info.lock, "kconsole_help");
199
    spinlock_initialize(&help_info.lock, "kconsole_help");
186
    link_initialize(&help_info.link);
200
    link_initialize(&help_info.link);
187
    if (!cmd_register(&help_info))
201
    if (!cmd_register(&help_info))
188
        panic("could not register command %s\n", help_info.name);
202
        panic("could not register command %s\n", help_info.name);
189
 
203
 
190
 
204
 
191
    spinlock_initialize(&desc_info.lock, "kconsole_desc");
205
    spinlock_initialize(&desc_info.lock, "kconsole_desc");
192
    link_initialize(&desc_info.link);
206
    link_initialize(&desc_info.link);
193
    if (!cmd_register(&desc_info))
207
    if (!cmd_register(&desc_info))
194
        panic("could not register command %s\n", desc_info.name);
208
        panic("could not register command %s\n", desc_info.name);
195
   
209
   
196
    spinlock_initialize(&symaddr_info.lock, "kconsole_symaddr");
210
    spinlock_initialize(&symaddr_info.lock, "kconsole_symaddr");
197
    link_initialize(&symaddr_info.link);
211
    link_initialize(&symaddr_info.link);
198
    if (!cmd_register(&symaddr_info))
212
    if (!cmd_register(&symaddr_info))
199
        panic("could not register command %s\n", symaddr_info.name);
213
        panic("could not register command %s\n", symaddr_info.name);
200
 
214
 
201
    spinlock_initialize(&call0_info.lock, "kconsole_call0");
215
    spinlock_initialize(&call0_info.lock, "kconsole_call0");
202
    link_initialize(&call0_info.link);
216
    link_initialize(&call0_info.link);
203
    if (!cmd_register(&call0_info))
217
    if (!cmd_register(&call0_info))
204
        panic("could not register command %s\n", call0_info.name);
218
        panic("could not register command %s\n", call0_info.name);
205
 
219
 
206
    spinlock_initialize(&call1_info.lock, "kconsole_call1");
220
    spinlock_initialize(&call1_info.lock, "kconsole_call1");
207
    link_initialize(&call1_info.link);
221
    link_initialize(&call1_info.link);
208
    if (!cmd_register(&call1_info))
222
    if (!cmd_register(&call1_info))
209
        panic("could not register command %s\n", call1_info.name);
223
        panic("could not register command %s\n", call1_info.name);
210
 
224
 
211
 
225
 
212
    spinlock_initialize(&call2_info.lock, "kconsole_call2");
226
    spinlock_initialize(&call2_info.lock, "kconsole_call2");
213
    link_initialize(&call2_info.link);
227
    link_initialize(&call2_info.link);
214
    if (!cmd_register(&call2_info))
228
    if (!cmd_register(&call2_info))
215
        panic("could not register command %s\n", call2_info.name);
229
        panic("could not register command %s\n", call2_info.name);
216
   
230
   
217
    spinlock_initialize(&halt_info.lock, "kconsole_halt");
231
    spinlock_initialize(&halt_info.lock, "kconsole_halt");
218
    link_initialize(&halt_info.link);
232
    link_initialize(&halt_info.link);
219
    if (!cmd_register(&halt_info))
233
    if (!cmd_register(&halt_info))
220
        panic("could not register command %s\n", halt_info.name);
234
        panic("could not register command %s\n", halt_info.name);
221
}
235
}
222
 
236
 
223
 
237
 
224
/** Register kconsole command.
238
/** Register kconsole command.
225
 *
239
 *
226
 * @param cmd Structure describing the command.
240
 * @param cmd Structure describing the command.
227
 *
241
 *
228
 * @return 0 on failure, 1 on success.
242
 * @return 0 on failure, 1 on success.
229
 */
243
 */
230
int cmd_register(cmd_info_t *cmd)
244
int cmd_register(cmd_info_t *cmd)
231
{
245
{
232
    ipl_t ipl;
246
    ipl_t ipl;
233
    link_t *cur;
247
    link_t *cur;
234
   
248
   
235
    spinlock_lock(&cmd_lock);
249
    spinlock_lock(&cmd_lock);
236
   
250
   
237
    /*
251
    /*
238
     * Make sure the command is not already listed.
252
     * Make sure the command is not already listed.
239
     */
253
     */
240
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
254
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
241
        cmd_info_t *hlp;
255
        cmd_info_t *hlp;
242
       
256
       
243
        hlp = list_get_instance(cur, cmd_info_t, link);
257
        hlp = list_get_instance(cur, cmd_info_t, link);
244
 
258
 
245
        if (hlp == cmd) {
259
        if (hlp == cmd) {
246
            /* The command is already there. */
260
            /* The command is already there. */
247
            spinlock_unlock(&cmd_lock);
261
            spinlock_unlock(&cmd_lock);
248
            return 0;
262
            return 0;
249
        }
263
        }
250
 
264
 
251
        /* Avoid deadlock. */
265
        /* Avoid deadlock. */
252
        if (hlp < cmd) {
266
        if (hlp < cmd) {
253
            spinlock_lock(&hlp->lock);
267
            spinlock_lock(&hlp->lock);
254
            spinlock_lock(&cmd->lock);
268
            spinlock_lock(&cmd->lock);
255
        } else {
269
        } else {
256
            spinlock_lock(&cmd->lock);
270
            spinlock_lock(&cmd->lock);
257
            spinlock_lock(&hlp->lock);
271
            spinlock_lock(&hlp->lock);
258
        }
272
        }
259
       
273
       
260
        if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) {
274
        if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) {
261
            /* The command is already there. */
275
            /* The command is already there. */
262
            spinlock_unlock(&hlp->lock);
276
            spinlock_unlock(&hlp->lock);
263
            spinlock_unlock(&cmd->lock);
277
            spinlock_unlock(&cmd->lock);
264
            spinlock_unlock(&cmd_lock);
278
            spinlock_unlock(&cmd_lock);
265
            return 0;
279
            return 0;
266
        }
280
        }
267
       
281
       
268
        spinlock_unlock(&hlp->lock);
282
        spinlock_unlock(&hlp->lock);
269
        spinlock_unlock(&cmd->lock);
283
        spinlock_unlock(&cmd->lock);
270
    }
284
    }
271
   
285
   
272
    /*
286
    /*
273
     * Now the command can be added.
287
     * Now the command can be added.
274
     */
288
     */
275
    list_append(&cmd->link, &cmd_head);
289
    list_append(&cmd->link, &cmd_head);
276
   
290
   
277
    spinlock_unlock(&cmd_lock);
291
    spinlock_unlock(&cmd_lock);
278
    return 1;
292
    return 1;
279
}
293
}
280
 
294
 
281
/** Kernel console managing thread.
295
/** Kernel console managing thread.
282
 *
296
 *
283
 * @param arg Not used.
297
 * @param arg Not used.
284
 */
298
 */
285
void kconsole(void *arg)
299
void kconsole(void *arg)
286
{
300
{
287
    char cmdline[MAX_CMDLINE+1];
301
    char cmdline[MAX_CMDLINE+1];
288
    cmd_info_t *cmd_info;
302
    cmd_info_t *cmd_info;
289
    count_t len;
303
    count_t len;
290
 
304
 
291
    if (!stdin) {
305
    if (!stdin) {
292
        printf("%s: no stdin\n", __FUNCTION__);
306
        printf("%s: no stdin\n", __FUNCTION__);
293
        return;
307
        return;
294
    }
308
    }
295
   
309
   
296
    while (true) {
310
    while (true) {
297
        printf("%s> ", __FUNCTION__);
311
        printf("%s> ", __FUNCTION__);
298
        if (!(len = gets(stdin, cmdline, sizeof(cmdline))))
312
        if (!(len = gets(stdin, cmdline, sizeof(cmdline))))
299
            continue;
313
            continue;
300
        cmdline[len] = '\0';
314
        cmdline[len] = '\0';
301
        cmd_info = parse_cmdline(cmdline, len);
315
        cmd_info = parse_cmdline(cmdline, len);
302
        if (!cmd_info)
316
        if (!cmd_info)
303
            continue;
317
            continue;
304
        (void) cmd_info->func(cmd_info->argv);
318
        (void) cmd_info->func(cmd_info->argv);
305
    }
319
    }
306
}
320
}
307
 
321
 
-
 
322
static int parse_int_arg(char *text, size_t len, __native *result)
-
 
323
{
-
 
324
    char symname[MAX_SYMBOL_NAME];
-
 
325
    __address symaddr;
-
 
326
    bool isaddr = false;
-
 
327
   
-
 
328
    /* If we get a name, try to find it in symbol table */
-
 
329
    if (text[0] < '0' | text[0] > '9') {
-
 
330
        if (text[0] == '&') {
-
 
331
            isaddr = true;
-
 
332
            text++;len--;
-
 
333
        }
-
 
334
        strncpy(symname, text, min(len+1, MAX_SYMBOL_NAME));
-
 
335
        symaddr = get_symbol_addr(symname);
-
 
336
        if (!symaddr) {
-
 
337
            printf("Symbol %s not found.\n",symname);
-
 
338
            return -1;
-
 
339
        }
-
 
340
        if (symaddr == (__address) -1) {
-
 
341
            printf("Duplicate symbol %s.\n",symname);
-
 
342
            symtab_print_search(symname);
-
 
343
            return -1;
-
 
344
        }
-
 
345
        if (isaddr)
-
 
346
            *result = (__native)symaddr;
-
 
347
        else
-
 
348
            *result = *((__native *)symaddr);
-
 
349
    } else /* It's a number - convert it */
-
 
350
        *result = atoi(text);
-
 
351
    return 0;
-
 
352
}
-
 
353
 
308
/** Parse command line.
354
/** Parse command line.
309
 *
355
 *
310
 * @param cmdline Command line as read from input device.
356
 * @param cmdline Command line as read from input device.
311
 * @param len Command line length.
357
 * @param len Command line length.
312
 *
358
 *
313
 * @return Structure describing the command.
359
 * @return Structure describing the command.
314
 */
360
 */
315
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
361
cmd_info_t *parse_cmdline(char *cmdline, size_t len)
316
{
362
{
317
    index_t start = 0, end = 0;
363
    index_t start = 0, end = 0;
318
    cmd_info_t *cmd = NULL;
364
    cmd_info_t *cmd = NULL;
319
    link_t *cur;
365
    link_t *cur;
320
    ipl_t ipl;
366
    ipl_t ipl;
321
    int i;
367
    int i;
322
   
368
   
323
    if (!parse_argument(cmdline, len, &start, &end)) {
369
    if (!parse_argument(cmdline, len, &start, &end)) {
324
        /* Command line did not contain alphanumeric word. */
370
        /* Command line did not contain alphanumeric word. */
325
        return NULL;
371
        return NULL;
326
    }
372
    }
327
 
373
 
328
    spinlock_lock(&cmd_lock);
374
    spinlock_lock(&cmd_lock);
329
   
375
   
330
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
376
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
331
        cmd_info_t *hlp;
377
        cmd_info_t *hlp;
332
       
378
       
333
        hlp = list_get_instance(cur, cmd_info_t, link);
379
        hlp = list_get_instance(cur, cmd_info_t, link);
334
        spinlock_lock(&hlp->lock);
380
        spinlock_lock(&hlp->lock);
335
       
381
       
336
        if (strncmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) {
382
        if (strncmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) {
337
            cmd = hlp;
383
            cmd = hlp;
338
            break;
384
            break;
339
        }
385
        }
340
       
386
       
341
        spinlock_unlock(&hlp->lock);
387
        spinlock_unlock(&hlp->lock);
342
    }
388
    }
343
   
389
   
344
    spinlock_unlock(&cmd_lock);
390
    spinlock_unlock(&cmd_lock);
345
   
391
   
346
    if (!cmd) {
392
    if (!cmd) {
347
        /* Unknown command. */
393
        /* Unknown command. */
348
        printf("Unknown command.\n");
394
        printf("Unknown command.\n");
349
        return NULL;
395
        return NULL;
350
    }
396
    }
351
 
397
 
352
    /* cmd == hlp is locked */
398
    /* cmd == hlp is locked */
353
   
399
   
354
    /*
400
    /*
355
     * The command line must be further analyzed and
401
     * The command line must be further analyzed and
356
     * the parameters therefrom must be matched and
402
     * the parameters therefrom must be matched and
357
     * converted to those specified in the cmd info
403
     * converted to those specified in the cmd info
358
     * structure.
404
     * structure.
359
     */
405
     */
360
 
406
 
361
    for (i = 0; i < cmd->argc; i++) {
407
    for (i = 0; i < cmd->argc; i++) {
362
        char *buf;
408
        char *buf;
363
        start = end + 1;
409
        start = end + 1;
364
        if (!parse_argument(cmdline, len, &start, &end)) {
410
        if (!parse_argument(cmdline, len, &start, &end)) {
365
            printf("Too few arguments.\n");
411
            printf("Too few arguments.\n");
366
            spinlock_unlock(&cmd->lock);
412
            spinlock_unlock(&cmd->lock);
367
            return NULL;
413
            return NULL;
368
        }
414
        }
369
       
415
       
370
        switch (cmd->argv[i].type) {
416
        switch (cmd->argv[i].type) {
371
        case ARG_TYPE_STRING:
417
        case ARG_TYPE_STRING:
372
                buf = cmd->argv[i].buffer;
418
                buf = cmd->argv[i].buffer;
373
                strncpy(buf, (const char *) &cmdline[start], min((end - start) + 1, cmd->argv[i].len - 1));
419
                strncpy(buf, (const char *) &cmdline[start], min((end - start) + 2, cmd->argv[i].len));
374
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0';
420
            buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0';
375
            break;
421
            break;
376
        case ARG_TYPE_INT: {
422
        case ARG_TYPE_INT:
377
            char symname[MAX_SYMBOL_NAME];
-
 
378
            __address symaddr;
-
 
379
 
-
 
380
            /* If we get a name, try to find it in symbol table */
-
 
381
            if (cmdline[start] < '0' | cmdline[start] > '9') {
423
            if (parse_int_arg(cmdline+start, end-start+1,
382
                strncpy(symname, cmdline+start, min((end-start) + 1, MAX_SYMBOL_NAME -1 ));
-
 
383
                symaddr = get_symbol_addr(symname);
-
 
384
                if (!symaddr) {
-
 
385
                    printf("Symbol %s not found.\n",symname);
-
 
386
                    return NULL;
-
 
387
                }
-
 
388
                if (symaddr == (__address) -1) {
424
                      &cmd->argv[i].intval))
389
                    printf("Duplicate symbol %s.\n",symname);
-
 
390
                    symtab_print_search(symname);
-
 
391
                    return NULL;
425
                return NULL;
392
                }
-
 
393
                cmd->argv[i].intval = *((__native *)symaddr);
-
 
394
            } else /* It's a number - convert it */
-
 
395
                cmd->argv[i].intval = atoi(cmdline+start);
-
 
396
            break;
426
            break;
-
 
427
        case ARG_TYPE_VAR:
-
 
428
            if (start != end && cmdline[start] == '"' && cmdline[end] == '"') {
-
 
429
                buf = cmd->argv[i].buffer;
-
 
430
                strncpy(buf, (const char *) &cmdline[start+1],
-
 
431
                    min((end-start), cmd->argv[i].len));
-
 
432
                buf[min((end - start), cmd->argv[i].len - 1)] = '\0';
-
 
433
                cmd->argv[i].intval = (__native) buf;
-
 
434
                cmd->argv[i].vartype = ARG_TYPE_STRING;
-
 
435
            } else if (!parse_int_arg(cmdline+start, end-start+1,
-
 
436
                         &cmd->argv[i].intval))
-
 
437
                cmd->argv[i].vartype = ARG_TYPE_INT;
-
 
438
            else {
-
 
439
                printf("Unrecognized variable argument.\n");
-
 
440
                return NULL;
397
            }
441
            }
-
 
442
            break;
398
        case ARG_TYPE_INVALID:
443
        case ARG_TYPE_INVALID:
399
        default:
444
        default:
400
            printf("invalid argument type\n");
445
            printf("invalid argument type\n");
401
            return NULL;
446
            return NULL;
402
            break;
447
            break;
403
        }
448
        }
404
    }
449
    }
405
   
450
   
406
    start = end + 1;
451
    start = end + 1;
407
    if (parse_argument(cmdline, len, &start, &end)) {
452
    if (parse_argument(cmdline, len, &start, &end)) {
408
        printf("Too many arguments.\n");
453
        printf("Too many arguments.\n");
409
        spinlock_unlock(&cmd->lock);
454
        spinlock_unlock(&cmd->lock);
410
        return NULL;
455
        return NULL;
411
    }
456
    }
412
   
457
   
413
    spinlock_unlock(&cmd->lock);
458
    spinlock_unlock(&cmd->lock);
414
    return cmd;
459
    return cmd;
415
}
460
}
416
 
461
 
417
/** Parse argument.
462
/** Parse argument.
418
 *
463
 *
419
 * Find start and end positions of command line argument.
464
 * Find start and end positions of command line argument.
420
 *
465
 *
421
 * @param cmdline Command line as read from the input device.
466
 * @param cmdline Command line as read from the input device.
422
 * @param len Number of characters in cmdline.
467
 * @param len Number of characters in cmdline.
423
 * @param start On entry, 'start' contains pointer to the index
468
 * @param start On entry, 'start' contains pointer to the index
424
 *        of first unprocessed character of cmdline.
469
 *        of first unprocessed character of cmdline.
425
 *        On successful exit, it marks beginning of the next argument.
470
 *        On successful exit, it marks beginning of the next argument.
426
 * @param end Undefined on entry. On exit, 'end' points to the last character
471
 * @param end Undefined on entry. On exit, 'end' points to the last character
427
 *        of the next argument.
472
 *        of the next argument.
428
 *
473
 *
429
 * @return false on failure, true on success.
474
 * @return false on failure, true on success.
430
 */
475
 */
431
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
476
bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
432
{
477
{
433
    int i;
478
    int i;
434
    bool found_start = false;
479
    bool found_start = false;
435
   
480
   
436
    ASSERT(start != NULL);
481
    ASSERT(start != NULL);
437
    ASSERT(end != NULL);
482
    ASSERT(end != NULL);
438
   
483
   
439
    for (i = *start; i < len; i++) {
484
    for (i = *start; i < len; i++) {
440
        if (!found_start) {
485
        if (!found_start) {
441
            if (is_white(cmdline[i]))
486
            if (is_white(cmdline[i]))
442
                (*start)++;
487
                (*start)++;
443
            else
488
            else
444
                found_start = true;
489
                found_start = true;
445
        } else {
490
        } else {
446
            if (is_white(cmdline[i]))
491
            if (is_white(cmdline[i]))
447
                break;
492
                break;
448
        }
493
        }
449
    }
494
    }
450
    *end = i - 1;
495
    *end = i - 1;
451
 
496
 
452
    return found_start;
497
    return found_start;
453
}
498
}
454
 
499
 
455
 
500
 
456
/** List supported commands.
501
/** List supported commands.
457
 *
502
 *
458
 * @param argv Argument vector.
503
 * @param argv Argument vector.
459
 *
504
 *
460
 * @return 0 on failure, 1 on success.
505
 * @return 0 on failure, 1 on success.
461
 */
506
 */
462
int cmd_help(cmd_arg_t *argv)
507
int cmd_help(cmd_arg_t *argv)
463
{
508
{
464
    link_t *cur;
509
    link_t *cur;
465
    ipl_t ipl;
510
    ipl_t ipl;
466
 
511
 
467
    spinlock_lock(&cmd_lock);
512
    spinlock_lock(&cmd_lock);
468
   
513
   
469
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
514
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
470
        cmd_info_t *hlp;
515
        cmd_info_t *hlp;
471
       
516
       
472
        hlp = list_get_instance(cur, cmd_info_t, link);
517
        hlp = list_get_instance(cur, cmd_info_t, link);
473
        spinlock_lock(&hlp->lock);
518
        spinlock_lock(&hlp->lock);
474
       
519
       
475
        printf("%s - %s\n", hlp->name, hlp->description);
520
        printf("%s - %s\n", hlp->name, hlp->description);
476
 
521
 
477
        spinlock_unlock(&hlp->lock);
522
        spinlock_unlock(&hlp->lock);
478
    }
523
    }
479
   
524
   
480
    spinlock_unlock(&cmd_lock);
525
    spinlock_unlock(&cmd_lock);
481
 
526
 
482
    return 1;
527
    return 1;
483
}
528
}
484
 
529
 
485
/** Describe specified command.
530
/** Describe specified command.
486
 *
531
 *
487
 * @param argv Argument vector.
532
 * @param argv Argument vector.
488
 *
533
 *
489
 * @return 0 on failure, 1 on success.
534
 * @return 0 on failure, 1 on success.
490
 */
535
 */
491
int cmd_desc(cmd_arg_t *argv)
536
int cmd_desc(cmd_arg_t *argv)
492
{
537
{
493
    link_t *cur;
538
    link_t *cur;
494
    ipl_t ipl;
539
    ipl_t ipl;
495
 
540
 
496
    spinlock_lock(&cmd_lock);
541
    spinlock_lock(&cmd_lock);
497
   
542
   
498
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
543
    for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
499
        cmd_info_t *hlp;
544
        cmd_info_t *hlp;
500
       
545
       
501
        hlp = list_get_instance(cur, cmd_info_t, link);
546
        hlp = list_get_instance(cur, cmd_info_t, link);
502
        spinlock_lock(&hlp->lock);
547
        spinlock_lock(&hlp->lock);
503
 
548
 
504
        if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) {
549
        if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) {
505
            printf("%s - %s\n", hlp->name, hlp->description);
550
            printf("%s - %s\n", hlp->name, hlp->description);
506
            if (hlp->help)
551
            if (hlp->help)
507
                hlp->help();
552
                hlp->help();
508
            spinlock_unlock(&hlp->lock);
553
            spinlock_unlock(&hlp->lock);
509
            break;
554
            break;
510
        }
555
        }
511
 
556
 
512
        spinlock_unlock(&hlp->lock);
557
        spinlock_unlock(&hlp->lock);
513
    }
558
    }
514
   
559
   
515
    spinlock_unlock(&cmd_lock);
560
    spinlock_unlock(&cmd_lock);
516
 
561
 
517
    return 1;
562
    return 1;
518
}
563
}
519
 
564
 
520
/** Search symbol table */
565
/** Search symbol table */
521
int cmd_symaddr(cmd_arg_t *argv)
566
int cmd_symaddr(cmd_arg_t *argv)
522
{
567
{
523
    __address symaddr;
568
    __address symaddr;
524
    char *symbol;
569
    char *symbol;
525
 
570
 
526
    symtab_print_search(argv->buffer);
571
    symtab_print_search(argv->buffer);
527
   
572
   
528
    return 1;
573
    return 1;
529
}
574
}
530
 
575
 
531
/** Call function with zero parameters */
576
/** Call function with zero parameters */
532
int cmd_call0(cmd_arg_t *argv)
577
int cmd_call0(cmd_arg_t *argv)
533
{
578
{
534
    __address symaddr;
579
    __address symaddr;
535
    char *symbol;
580
    char *symbol;
536
    __native (*f)(void);
581
    __native (*f)(void);
537
 
582
 
538
    symaddr = get_symbol_addr(argv->buffer);
583
    symaddr = get_symbol_addr(argv->buffer);
539
    if (!symaddr)
584
    if (!symaddr)
540
        printf("Symbol not found.\n");
585
        printf("Symbol %s not found.\n", argv->buffer);
541
    else if (symaddr == (__address) -1) {
586
    else if (symaddr == (__address) -1) {
542
        symtab_print_search(argv->buffer);
587
        symtab_print_search(argv->buffer);
543
        printf("Duplicate symbol, be more specific.\n");
588
        printf("Duplicate symbol, be more specific.\n");
544
    } else {
589
    } else {
545
        symbol = get_symtab_entry(symaddr);
590
        symbol = get_symtab_entry(symaddr);
546
        printf("Calling f(): 0x%p: %s\n", symaddr, symbol);
591
        printf("Calling f(): 0x%p: %s\n", symaddr, symbol);
547
        f =  (__native (*)(void)) symaddr;
592
        f =  (__native (*)(void)) symaddr;
548
        printf("Result: 0x%X\n", f());
593
        printf("Result: 0x%X\n", f());
549
    }
594
    }
550
   
595
   
551
    return 1;
596
    return 1;
552
}
597
}
553
 
598
 
554
/** Call function with one parameter */
599
/** Call function with one parameter */
555
int cmd_call1(cmd_arg_t *argv)
600
int cmd_call1(cmd_arg_t *argv)
556
{
601
{
557
    __address symaddr;
602
    __address symaddr;
558
    char *symbol;
603
    char *symbol;
559
    __native (*f)(__native);
604
    __native (*f)(__native);
560
    __native arg1 = argv[1].intval;
605
    __native arg1 = argv[1].intval;
561
 
606
 
562
    symaddr = get_symbol_addr(argv->buffer);
607
    symaddr = get_symbol_addr(argv->buffer);
563
    if (!symaddr)
608
    if (!symaddr)
564
        printf("Symbol not found.\n");
609
        printf("Symbol %s not found.\n", argv->buffer);
565
    else if (symaddr == (__address) -1) {
610
    else if (symaddr == (__address) -1) {
566
        symtab_print_search(argv->buffer);
611
        symtab_print_search(argv->buffer);
567
        printf("Duplicate symbol, be more specific.\n");
612
        printf("Duplicate symbol, be more specific.\n");
568
    } else {
613
    } else {
569
        symbol = get_symtab_entry(symaddr);
614
        symbol = get_symtab_entry(symaddr);
570
        printf("Calling f(0x%x): 0x%p: %s\n", arg1, symaddr, symbol);
615
        printf("Calling f(0x%x): 0x%p: %s\n", arg1, symaddr, symbol);
571
        f =  (__native (*)(__native)) symaddr;
616
        f =  (__native (*)(__native)) symaddr;
572
        printf("Result: 0x%x\n", f(arg1));
617
        printf("Result: 0x%x\n", f(arg1));
573
    }
618
    }
574
   
619
   
575
    return 1;
620
    return 1;
576
}
621
}
577
 
622
 
578
/** Call function with two parameters */
623
/** Call function with two parameters */
579
int cmd_call2(cmd_arg_t *argv)
624
int cmd_call2(cmd_arg_t *argv)
580
{
625
{
581
    __address symaddr;
626
    __address symaddr;
582
    char *symbol;
627
    char *symbol;
583
    __native (*f)(__native);
628
    __native (*f)(__native,__native);
584
    __native arg1 = argv[1].intval;
629
    __native arg1 = argv[1].intval;
585
    __native arg2 = argv[2].intval;
630
    __native arg2 = argv[2].intval;
586
 
631
 
587
    symaddr = get_symbol_addr(argv->buffer);
632
    symaddr = get_symbol_addr(argv->buffer);
588
    if (!symaddr)
633
    if (!symaddr)
589
        printf("Symbol not found.\n");
634
        printf("Symbol %s not found.\n", argv->buffer);
590
    else if (symaddr == (__address) -1) {
635
    else if (symaddr == (__address) -1) {
591
        symtab_print_search(argv->buffer);
636
        symtab_print_search(argv->buffer);
592
        printf("Duplicate symbol, be more specific.\n");
637
        printf("Duplicate symbol, be more specific.\n");
593
    } else {
638
    } else {
594
        symbol = get_symtab_entry(symaddr);
639
        symbol = get_symtab_entry(symaddr);
595
        printf("Calling f(0x%x,0x%x): 0x%p: %s\n",
640
        printf("Calling f(0x%x,0x%x): 0x%p: %s\n",
596
               arg1, arg2, symaddr, symbol);
641
               arg1, arg2, symaddr, symbol);
597
        f =  (__native (*)(__native)) symaddr;
642
        f =  (__native (*)(__native,__native)) symaddr;
598
        printf("Result: 0x%x\n", f(arg1));
643
        printf("Result: 0x%x\n", f(arg1, arg2));
599
    }
644
    }
600
   
645
   
601
    return 1;
646
    return 1;
602
}
647
}
603
 
648
 
604
 
649
 
605
/** Print detailed description of 'describe' command. */
650
/** Print detailed description of 'describe' command. */
606
void desc_help(void)
651
void desc_help(void)
607
{
652
{
608
    printf("Syntax: describe command_name\n");
653
    printf("Syntax: describe command_name\n");
609
}
654
}
610
 
655
 
611
/** Halt the kernel.
656
/** Halt the kernel.
612
 *
657
 *
613
 * @param argv Argument vector (ignored).
658
 * @param argv Argument vector (ignored).
614
 *
659
 *
615
 * @return 0 on failure, 1 on success (never returns).
660
 * @return 0 on failure, 1 on success (never returns).
616
 */
661
 */
617
int cmd_halt(cmd_arg_t *argv)
662
int cmd_halt(cmd_arg_t *argv)
618
{
663
{
619
    halt();
664
    halt();
620
    return 1;
665
    return 1;
621
}
666
}
622
 
667