[BACK]Return to cmd.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / test / kmon

Annotation of prex-old/usr/test/kmon/cmd.c, Revision 1.1.1.1

1.1       nbrk        1: /*
                      2:  * Copyright (c) 2005-2006, Kohsuke Ohtani
                      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:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. Neither the name of the author nor the names of any co-contributors
                     14:  *    may be used to endorse or promote products derived from this software
                     15:  *    without specific prior written permission.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
                     30: /*
                     31:  * cmd.c - command processor
                     32:  */
                     33:
                     34: #include <prex/prex.h>
                     35: #include <prex/power.h>
                     36: #include <sys/ioctl.h>
                     37: #include <sys/types.h>
                     38:
                     39: #include <unistd.h>
                     40: #include <stdlib.h>
                     41: #include <string.h>
                     42: #include <errno.h>
                     43: #include <stdio.h>
                     44:
                     45: int cmd_help(int argc, char **argv);
                     46: int cmd_ver(int argc, char **argv);
                     47: int cmd_mem(int argc, char **argv);
                     48: int cmd_clear(int argc, char **argv);
                     49: int cmd_kill(int argc, char **argv);
                     50: #ifdef DEBUG
                     51: int cmd_thread(int argc, char **argv);
                     52: int cmd_task(int argc, char **argv);
                     53: int cmd_object(int argc, char **argv);
                     54: int cmd_timer(int argc, char **argv);
                     55: int cmd_irq(int argc, char **argv);
                     56: int cmd_device(int argc, char **argv);
                     57: int cmd_vm(int argc, char **argv);
                     58: int cmd_dmesg(int argc, char **argv);
                     59: #endif
                     60: int cmd_reboot(int argc, char **argv);
                     61: int cmd_shutdown(int argc, char **argv);
                     62:
                     63: static const char *err_msg[] = {
                     64:        "Syntax error",
                     65: };
                     66:
                     67: struct cmd_entry {
                     68:        char *cmd;
                     69:        int (*func) (int, char **);
                     70:        char *usage;
                     71: };
                     72:
                     73: static struct cmd_entry cmd_table[] = {
                     74:        { "help"        ,cmd_help       ,"help     - This help" },
                     75:        { "ver"         ,cmd_ver        ,"ver      - Kernel version information" },
                     76:        { "mem"         ,cmd_mem        ,"mem      - Show memory usage" },
                     77:        { "clear"       ,cmd_clear      ,"clear    - Clear screen" },
                     78:        { "kill"        ,cmd_kill       ,"kill     - Terminate thread" },
                     79: #ifdef DEBUG
                     80:        { "thread"      ,cmd_thread     ,"thread   - Dump threads" },
                     81:        { "task"        ,cmd_task       ,"task     - Dump tasks" },
                     82:        { "object"      ,cmd_object     ,"object   - Dump objects" },
                     83:        { "timer"       ,cmd_timer      ,"timer    - Dump system timers" },
                     84:        { "irq"         ,cmd_irq        ,"irq      - Dump irq information" },
                     85:        { "device"      ,cmd_device     ,"device   - Dump devices" },
                     86:        { "vm"          ,cmd_vm         ,"vm       - Dump virtual memory information" },
                     87:        { "dmesg"       ,cmd_dmesg      ,"dmesg    - Dump kernel message log" },
                     88: #endif
                     89:        { "reboot"      ,cmd_reboot     ,"reboot   - Reboot system" },
                     90:        { "shutdown"    ,cmd_shutdown   ,"shutdown - Shutdown system" },
                     91:        { NULL          ,NULL           ,NULL },
                     92: };
                     93:
                     94: int
                     95: cmd_help(int argc, char **argv)
                     96: {
                     97:        int i = 0;
                     98:
                     99:        while (cmd_table[i].cmd != NULL) {
                    100:                puts(cmd_table[i].usage);
                    101:                i++;
                    102:        }
                    103:        return 0;
                    104: }
                    105:
                    106: int
                    107: cmd_ver(int argc, char **argv)
                    108: {
                    109:        struct info_kernel info;
                    110:
                    111:        sys_info(INFO_KERNEL, &info);
                    112:
                    113:        printf("Kernel version:\n");
                    114:        printf("%s version %s for %s\n",
                    115:               info.sysname, info.version, info.machine);
                    116:        return 0;
                    117: }
                    118:
                    119: int
                    120: cmd_mem(int argc, char **argv)
                    121: {
                    122:        struct info_memory info;
                    123:
                    124:        sys_info(INFO_MEMORY, &info);
                    125:
                    126:        printf("Memory usage:\n");
                    127:        printf("    total     used     free   kernel\n");
                    128:        printf(" %8d %8d %8d %8d\n", (u_int)info.total,
                    129:               (u_int)(info.total - info.free), (u_int)info.free, (u_int)info.kernel);
                    130:        return 0;
                    131: }
                    132:
                    133: int
                    134: cmd_clear(int argc, char **argv)
                    135: {
                    136:        printf("\33[2J");
                    137:        return 0;
                    138: }
                    139:
                    140: int
                    141: cmd_kill(int argc, char **argv)
                    142: {
                    143:        thread_t th;
                    144:        char *ep;
                    145:
                    146:        if (argc < 2)
                    147:                return 1;
                    148:        th = (thread_t)strtoul(argv[1], &ep, 16);
                    149:        printf("Kill thread id:%x\n", (u_int)th);
                    150:        if (thread_terminate(th)) {
                    151:                printf("Thread %x does not exist\n", (u_int)th);
                    152:                return 1;
                    153:        }
                    154:        return 0;
                    155: }
                    156:
                    157: #ifdef DEBUG
                    158: int
                    159: cmd_thread(int argc, char **argv)
                    160: {
                    161:        sys_debug(DCMD_DUMP, DUMP_THREAD);
                    162:        return 0;
                    163: }
                    164:
                    165: int
                    166: cmd_task(int argc, char **argv)
                    167: {
                    168:        sys_debug(DCMD_DUMP, DUMP_TASK);
                    169:        return 0;
                    170: }
                    171:
                    172: int
                    173: cmd_object(int argc, char **argv)
                    174: {
                    175:        sys_debug(DCMD_DUMP, DUMP_OBJECT);
                    176:        return 0;
                    177: }
                    178:
                    179: int
                    180: cmd_timer(int argc, char **argv)
                    181: {
                    182:        sys_debug(DCMD_DUMP, DUMP_TIMER);
                    183:        return 0;
                    184: }
                    185:
                    186: int
                    187: cmd_irq(int argc, char **argv)
                    188: {
                    189:        sys_debug(DCMD_DUMP, DUMP_IRQ);
                    190:        return 0;
                    191: }
                    192:
                    193: int
                    194: cmd_device(int argc, char **argv)
                    195: {
                    196:        sys_debug(DCMD_DUMP, DUMP_DEVICE);
                    197:        return 0;
                    198: }
                    199:
                    200: int
                    201: cmd_vm(int argc, char **argv)
                    202: {
                    203:        sys_debug(DCMD_DUMP, DUMP_VM);
                    204:        return 0;
                    205: }
                    206:
                    207: int
                    208: cmd_dmesg(int argc, char **argv)
                    209: {
                    210:        sys_debug(DCMD_DUMP, DUMP_MSGLOG);
                    211:        return 0;
                    212: }
                    213:
                    214: #endif /* DEBUG */
                    215:
                    216: int
                    217: cmd_reboot(int argc, char **argv)
                    218: {
                    219:        reboot(0);
                    220:        return 0;
                    221: }
                    222:
                    223: int
                    224: cmd_shutdown(int argc, char **argv)
                    225: {
                    226:        return shutdown(0);
                    227: }
                    228:
                    229: int
                    230: dispatch_cmd(int argc, char **argv)
                    231: {
                    232:        int i = 0;
                    233:        int err = 0;
                    234:
                    235:        while (cmd_table[i].cmd != NULL) {
                    236:                if (!strcmp(argv[0], cmd_table[i].cmd)) {
                    237:                        err = (cmd_table[i].func)(argc, argv);
                    238:                        break;
                    239:                }
                    240:                i++;
                    241:        }
                    242:        if (cmd_table[i].cmd == NULL)
                    243:                printf("%s: command not found\n", argv[0]);
                    244:        if (err)
                    245:                printf("Error %d:%s\n", err, err_msg[err - 1]);
                    246:        return 0;
                    247: }

CVSweb