cmd.c 23.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-04-30     Bernard      first implementation
 * 2006-05-04     Bernard      add list_thread,
 *                                 list_sem,
 *                                 list_timer
 * 2006-05-20     Bernard      add list_mutex,
 *                                 list_mailbox,
 *                                 list_msgqueue,
 *                                 list_event,
 *                                 list_fevent,
 *                                 list_mempool
 * 2006-06-03     Bernard      display stack information in list_thread
 * 2006-08-10     Bernard      change version to invoke rt_show_version
 * 2008-09-10     Bernard      update the list function for finsh syscall
 *                                 list and sysvar list
 * 2009-05-30     Bernard      add list_device
23 24
 * 2010-04-21     yi.qiu       add list_module
 * 2012-04-29     goprife      improve the command line auto-complete feature.
25
 * 2012-06-02     lgnq         add list_memheap
26
 * 2012-10-22     Bernard      add MS VC++ patch.
27
 * 2016-06-02     armink       beautify the list_thread command
28 29 30
 */

#include <rtthread.h>
31 32 33

#ifdef RT_USING_FINSH

34 35
#include "finsh.h"

36
long hello(void)
37
{
38
    rt_kprintf("Hello RT-Thread!\n");
39

40
    return 0;
41
}
42
FINSH_FUNCTION_EXPORT(hello, say hello world);
43 44

extern void rt_show_version(void);
45
long version(void)
46
{
47
    rt_show_version();
48

49
    return 0;
50
}
51
FINSH_FUNCTION_EXPORT(version, show RT-Thread version information);
B
Bernard Xiong 已提交
52
MSH_CMD_EXPORT(version, show RT-Thread version information);
53

54
static int object_name_maxlen(const char *type_name, struct rt_list_node *list)
55
{
56
    struct rt_list_node *node;
57
    struct rt_object *object = NULL;
58
    int max_length = rt_strlen(type_name), length;
59

60 61 62 63
    rt_enter_critical();
    for (node = list->next; node != list; node = node->next)
    {
        object = rt_list_entry(node, struct rt_object, list);
64

65 66
        length = rt_strlen(object->name);
        if (length > max_length) max_length = length;
67
    }
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    rt_exit_critical();

    if (max_length > RT_NAME_MAX || max_length == 0) max_length = RT_NAME_MAX;

    return max_length;
}

rt_inline void object_split(int len)
{
    while (len--) rt_kprintf("-");
}

static long _list_thread(struct rt_list_node *list)
{
    int maxlen;
    rt_uint8_t *ptr;
    struct rt_thread *thread;
    struct rt_list_node *node;
86
    const char *item_title = "thread";
87

88
    maxlen = object_name_maxlen(item_title, list);
89

90
    rt_kprintf("%-*.s pri  status      sp     stack size max used left tick  error\n", maxlen, item_title); object_split(maxlen);
91
    rt_kprintf(     " ---  ------- ---------- ----------  ------  ---------- ---\n");
92 93
    for (node = list->next; node != list; node = node->next)
    {
94
        rt_uint8_t stat;
95
        thread = rt_list_entry(node, struct rt_thread, list);
96
        rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority);
97

98
        stat = (thread->stat & RT_THREAD_STAT_MASK);
99 100 101 102
        if (stat == RT_THREAD_READY)        rt_kprintf(" ready  ");
        else if (stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
        else if (stat == RT_THREAD_INIT)    rt_kprintf(" init   ");
        else if (stat == RT_THREAD_CLOSE)   rt_kprintf(" close  ");
103

104
        ptr = (rt_uint8_t *)thread->stack_addr;
105 106
        while (*ptr == '#')ptr ++;

107
        rt_kprintf(" 0x%08x 0x%08x    %02d%%   0x%08x %03d\n",
108 109
                   thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
                   thread->stack_size,
110 111
                   (thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t) thread->stack_addr)) * 100
                        / thread->stack_size,
112 113
                   thread->remaining_tick,
                   thread->error);
114
    }
115

116
    return 0;
117
}
qiuyiuestc's avatar
qiuyiuestc 已提交
118 119 120

long list_thread(void)
{
121 122 123 124
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Thread);
    return _list_thread(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
125
}
126
FINSH_FUNCTION_EXPORT(list_thread, list thread);
B
Bernard Xiong 已提交
127
MSH_CMD_EXPORT(list_thread, list thread);
128

129
static void show_wait_queue(struct rt_list_node *list)
130
{
131 132 133 134 135 136 137
    struct rt_thread *thread;
    struct rt_list_node *node;

    for (node = list->next; node != list; node = node->next)
    {
        thread = rt_list_entry(node, struct rt_thread, tlist);
        rt_kprintf("%s", thread->name);
D
dzzxzz@gmail.com 已提交
138

139
        if (node->next != list)
D
dzzxzz@gmail.com 已提交
140
            rt_kprintf("/");
141
    }
142 143 144
}

#ifdef RT_USING_SEMAPHORE
qiuyiuestc's avatar
qiuyiuestc 已提交
145
static long _list_sem(struct rt_list_node *list)
146
{
147
    int maxlen;
148 149
    struct rt_semaphore *sem;
    struct rt_list_node *node;
150
    const char *item_title = "semaphore";
151

152
    maxlen = object_name_maxlen(item_title, list);
153

154
    rt_kprintf("%-*.s v   suspend thread\n", maxlen, item_title); object_split(maxlen);
155
    rt_kprintf(     " --- --------------\n");
156 157
    for (node = list->next; node != list; node = node->next)
    {
158
        sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list));
D
dzzxzz@gmail.com 已提交
159
        if (!rt_list_isempty(&sem->parent.suspend_thread))
160
        {
161 162
            rt_kprintf("%-*.*s %03d %d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
163 164 165
                       sem->parent.parent.name,
                       sem->value,
                       rt_list_len(&sem->parent.suspend_thread));
166 167 168 169 170
            show_wait_queue(&(sem->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
171 172
            rt_kprintf("%-*.*s %03d %d\n",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
173 174 175
                       sem->parent.parent.name,
                       sem->value,
                       rt_list_len(&sem->parent.suspend_thread));
176 177 178 179
        }
    }

    return 0;
180
}
qiuyiuestc's avatar
qiuyiuestc 已提交
181 182 183

long list_sem(void)
{
184 185 186 187 188
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Semaphore);

    return _list_sem(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
189
}
B
Bernard Xiong 已提交
190 191
FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system);
MSH_CMD_EXPORT(list_sem, list semaphore in system);
192 193 194
#endif

#ifdef RT_USING_EVENT
qiuyiuestc's avatar
qiuyiuestc 已提交
195
static long _list_event(struct rt_list_node *list)
196
{
197
    int maxlen;
198 199
    struct rt_event *e;
    struct rt_list_node *node;
200
    const char *item_title = "event";
201

202
    maxlen = object_name_maxlen(item_title, list);
203

204
    rt_kprintf("%-*.s      set    suspend thread\n", maxlen, item_title); object_split(maxlen);
205
    rt_kprintf(     "  ---------- --------------\n");
206 207
    for (node = list->next; node != list; node = node->next)
    {
208 209
        e = (struct rt_event *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&e->parent.suspend_thread))
210
        {
211 212
            rt_kprintf("%-*.*s  0x%08x %03d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
213 214 215
                       e->parent.parent.name,
                       e->set,
                       rt_list_len(&e->parent.suspend_thread));
216 217 218 219 220
            show_wait_queue(&(e->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
221 222
            rt_kprintf("%-*.*s  0x%08x 0\n",
                       maxlen, RT_NAME_MAX, e->parent.parent.name, e->set);
223 224 225 226
        }
    }

    return 0;
227
}
qiuyiuestc's avatar
qiuyiuestc 已提交
228 229 230

long list_event(void)
{
231 232 233 234
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Event);
    return _list_event(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
235
}
B
Bernard Xiong 已提交
236 237
FINSH_FUNCTION_EXPORT(list_event, list event in system);
MSH_CMD_EXPORT(list_event, list event in system);
238 239 240
#endif

#ifdef RT_USING_MUTEX
qiuyiuestc's avatar
qiuyiuestc 已提交
241
static long _list_mutex(struct rt_list_node *list)
242
{
243
    int maxlen;
244 245
    struct rt_mutex *m;
    struct rt_list_node *node;
246
    const char *item_title = "mutex";
247

248 249 250
    maxlen = object_name_maxlen(item_title, list);

    rt_kprintf("%-*.s   owner  hold suspend thread\n", maxlen, item_title); object_split(maxlen);
251
    rt_kprintf(     " -------- ---- --------------\n");
252 253
    for (node = list->next; node != list; node = node->next)
    {
254
        m = (struct rt_mutex *)(rt_list_entry(node, struct rt_object, list));
255 256
        rt_kprintf("%-*.*s %-8.*s %04d %d\n",
                   maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
257 258 259 260 261
                   m->parent.parent.name,
                   RT_NAME_MAX,
                   m->owner->name,
                   m->hold,
                   rt_list_len(&m->parent.suspend_thread));
262 263 264
    }

    return 0;
265
}
qiuyiuestc's avatar
qiuyiuestc 已提交
266 267 268

long list_mutex(void)
{
269 270 271 272 273
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Mutex);

    return _list_mutex(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
274
}
B
Bernard Xiong 已提交
275 276
FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system);
MSH_CMD_EXPORT(list_mutex, list mutex in system);
277 278 279
#endif

#ifdef RT_USING_MAILBOX
qiuyiuestc's avatar
qiuyiuestc 已提交
280
static long _list_mailbox(struct rt_list_node *list)
281
{
282
    int maxlen;
283 284
    struct rt_mailbox *m;
    struct rt_list_node *node;
G
gbcwbz 已提交
285
    const char *item_title = "mailbox";
286

287
    maxlen = object_name_maxlen(item_title, list);
288

289 290
    rt_kprintf("%-*.s entry size suspend thread\n", maxlen, item_title); object_split(maxlen);
    rt_kprintf(     " ----  ---- --------------\n");
291 292
    for (node = list->next; node != list; node = node->next)
    {
293 294
        m = (struct rt_mailbox *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&m->parent.suspend_thread))
295
        {
296 297
            rt_kprintf("%-*.*s %04d  %04d %d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
298 299 300 301
                       m->parent.parent.name,
                       m->entry,
                       m->size,
                       rt_list_len(&m->parent.suspend_thread));
302 303 304 305 306
            show_wait_queue(&(m->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
307 308
            rt_kprintf("%-*.*s %04d  %04d %d\n",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
309 310 311 312
                       m->parent.parent.name,
                       m->entry,
                       m->size,
                       rt_list_len(&m->parent.suspend_thread));
313 314 315 316
        }
    }

    return 0;
317
}
qiuyiuestc's avatar
qiuyiuestc 已提交
318 319 320

long list_mailbox(void)
{
321 322 323 324
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_MailBox);
    return _list_mailbox(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
325
}
B
Bernard Xiong 已提交
326 327
FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system);
MSH_CMD_EXPORT(list_mailbox, list mail box in system);
328 329 330
#endif

#ifdef RT_USING_MESSAGEQUEUE
qiuyiuestc's avatar
qiuyiuestc 已提交
331
static long _list_msgqueue(struct rt_list_node *list)
332
{
333
    int maxlen;
334 335
    struct rt_messagequeue *m;
    struct rt_list_node *node;
G
gbcwbz 已提交
336
    const char *item_title = "msgqueue";
337

338
    maxlen = object_name_maxlen(item_title, list);
339

340
    rt_kprintf("%-*.s entry suspend thread\n", maxlen, item_title); object_split(maxlen);
341
    rt_kprintf(     " ----  --------------\n");
342 343
    for (node = list->next; node != list; node = node->next)
    {
344 345
        m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&m->parent.suspend_thread))
346
        {
347 348
            rt_kprintf("%-*.*s %04d  %d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
349 350 351
                       m->parent.parent.name,
                       m->entry,
                       rt_list_len(&m->parent.suspend_thread));
352 353 354 355 356
            show_wait_queue(&(m->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
357 358
            rt_kprintf("%-*.*s %04d  %d\n",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
359 360 361
                       m->parent.parent.name,
                       m->entry,
                       rt_list_len(&m->parent.suspend_thread));
362 363 364 365
        }
    }

    return 0;
366
}
qiuyiuestc's avatar
qiuyiuestc 已提交
367 368 369

long list_msgqueue(void)
{
370 371 372 373
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_MessageQueue);
    return _list_msgqueue(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
374
}
B
Bernard Xiong 已提交
375 376
FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system);
MSH_CMD_EXPORT(list_msgqueue, list message queue in system);
377 378
#endif

379 380 381
#ifdef RT_USING_MEMHEAP
static long _list_memheap(struct rt_list_node *list)
{
382
    int maxlen;
383 384
    struct rt_memheap *mh;
    struct rt_list_node *node;
385
    const char *item_title = "memheap";
386

387
    maxlen = object_name_maxlen(item_title, list);
388

389
    rt_kprintf("%-*.s  pool size  max used size available size\n", maxlen, item_title); object_split(maxlen);
390
    rt_kprintf(      " ---------- ------------- --------------\n");
391 392 393 394
    for (node = list->next; node != list; node = node->next)
    {
        mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);

395 396
        rt_kprintf("%-*.*s %-010d %-013d %-05d\n",
                   maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
397 398 399 400
                   mh->parent.name,
                   mh->pool_size,
                   mh->max_used_size,
                   mh->available_size);
401 402 403 404 405 406 407
    }

    return 0;
}

long list_memheap(void)
{
408 409 410 411
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_MemHeap);
    return _list_memheap(&info->object_list);
412
}
B
Bernard Xiong 已提交
413 414
FINSH_FUNCTION_EXPORT(list_memheap, list memory heap in system);
MSH_CMD_EXPORT(list_memheap, list memory heap in system);
415 416
#endif

417
#ifdef RT_USING_MEMPOOL
qiuyiuestc's avatar
qiuyiuestc 已提交
418
static long _list_mempool(struct rt_list_node *list)
419
{
420
    int maxlen;
421 422
    struct rt_mempool *mp;
    struct rt_list_node *node;
423
    const char *item_title = "mempool";
424

425
    maxlen = object_name_maxlen(item_title, list);
426

427
    rt_kprintf("%-*.s block total free suspend thread\n", maxlen, item_title); object_split(maxlen);
428
    rt_kprintf(     " ----  ----  ---- --------------\n");
429 430
    for (node = list->next; node != list; node = node->next)
    {
431
        mp = (struct rt_mempool *)rt_list_entry(node, struct rt_object, list);
432 433
        if (mp->suspend_thread_count > 0)
        {
434 435
            rt_kprintf("%-*.*s %04d  %04d  %04d %d:",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
436 437 438 439 440
                       mp->parent.name,
                       mp->block_size,
                       mp->block_total_count,
                       mp->block_free_count,
                       mp->suspend_thread_count);
441
            show_wait_queue(&(mp->suspend_thread));
D
dzzxzz@gmail.com 已提交
442
            rt_kprintf("\n");
443 444 445
        }
        else
        {
446 447
            rt_kprintf("%-*.*s %04d  %04d  %04d %d\n",
                       maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
448 449 450 451 452
                       mp->parent.name,
                       mp->block_size,
                       mp->block_total_count,
                       mp->block_free_count,
                       mp->suspend_thread_count);
453 454 455 456
        }
    }

    return 0;
457
}
qiuyiuestc's avatar
qiuyiuestc 已提交
458 459 460

long list_mempool(void)
{
461 462 463 464
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_MemPool);
    return _list_mempool(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
465
}
466
FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system)
B
Bernard Xiong 已提交
467
MSH_CMD_EXPORT(list_mempool, list memory pool in system);
468 469
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
470
static long _list_timer(struct rt_list_node *list)
471
{
472
    int maxlen;
473 474
    struct rt_timer *timer;
    struct rt_list_node *node;
475
    const char *item_title = "timer";
476

477
    maxlen = object_name_maxlen(item_title, list);
478

479
    rt_kprintf("%-*.s  periodic   timeout       flag\n", maxlen, item_title); object_split(maxlen);
480
    rt_kprintf(     " ---------- ---------- -----------\n");
481 482
    for (node = list->next; node != list; node = node->next)
    {
483
        timer = (struct rt_timer *)(rt_list_entry(node, struct rt_object, list));
484 485
        rt_kprintf("%-*.*s 0x%08x 0x%08x ",
                   maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
486 487 488
                   timer->parent.name,
                   timer->init_tick,
                   timer->timeout_tick);
489
        if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
D
dzzxzz@gmail.com 已提交
490
            rt_kprintf("activated\n");
491
        else
D
dzzxzz@gmail.com 已提交
492
            rt_kprintf("deactivated\n");
493 494 495 496 497
    }

    rt_kprintf("current tick:0x%08x\n", rt_tick_get());

    return 0;
498
}
qiuyiuestc's avatar
qiuyiuestc 已提交
499 500 501

long list_timer(void)
{
502 503 504 505
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Timer);
    return _list_timer(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
506
}
B
Bernard Xiong 已提交
507 508
FINSH_FUNCTION_EXPORT(list_timer, list timer in system);
MSH_CMD_EXPORT(list_timer, list timer in system);
509 510

#ifdef RT_USING_DEVICE
qiuyiuestc's avatar
qiuyiuestc 已提交
511
static long _list_device(struct rt_list_node *list)
512
{
513
    int maxlen;
514 515
    struct rt_device *device;
    struct rt_list_node *node;
516
    char *const device_type_str[] =
517 518 519 520 521 522 523 524
    {
        "Character Device",
        "Block Device",
        "Network Interface",
        "MTD Device",
        "CAN Device",
        "RTC",
        "Sound Device",
525
        "Graphic Device",
526
        "I2C Bus",
527 528 529 530 531
        "USB Slave Device",
        "USB Host Bus",
        "SPI Bus",
        "SPI Device",
        "SDIO Bus",
532
        "PM Pseudo Device",
533 534
        "Pipe",
        "Portal Device",
535 536
        "Timer Device",
        "Miscellaneous Device",
537 538
        "Unknown"
    };
G
gbcwbz 已提交
539 540
    const char *item_title = "device";

541
    maxlen = object_name_maxlen(item_title, list);
542

543 544
    rt_kprintf("%-*.s         type         ref count\n", maxlen, item_title); object_split(maxlen);
    rt_kprintf(     " -------------------- ----------\n");
545 546
    for (node = list->next; node != list; node = node->next)
    {
547
        device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
548 549
        rt_kprintf("%-*.*s %-20s %-8d\n",
                   maxlen, RT_NAME_MAX,
D
dzzxzz@gmail.com 已提交
550 551 552
                   device->parent.name,
                   (device->type <= RT_Device_Class_Unknown) ?
                   device_type_str[device->type] :
553 554
                   device_type_str[RT_Device_Class_Unknown],
                   device->ref_count);
555 556 557
    }

    return 0;
558
}
qiuyiuestc's avatar
qiuyiuestc 已提交
559 560 561

long list_device(void)
{
562 563 564 565
    struct rt_object_information *info;

    info = rt_object_get_information(RT_Object_Class_Device);
    return _list_device(&info->object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
566
}
B
Bernard Xiong 已提交
567 568
FINSH_FUNCTION_EXPORT(list_device, list device in system);
MSH_CMD_EXPORT(list_device, list device in system);
569 570
#endif

D
dzzxzz 已提交
571
long list(void)
572
{
B
bernard 已提交
573
#ifndef FINSH_USING_MSH_ONLY
574 575
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
B
bernard 已提交
576
#endif
577

578 579
    rt_kprintf("--Function List:\n");
    {
580
        struct finsh_syscall *index;
D
dzzxzz@gmail.com 已提交
581
        for (index = _syscall_table_begin;
582 583
                index < _syscall_table_end;
                FINSH_NEXT_SYSCALL(index))
584
        {
585 586
            /* skip the internal command */
            if (strncmp((char *)index->name, "__", 2) == 0) continue;
B
Bernard Xiong 已提交
587

588
#ifdef FINSH_USING_DESCRIPTION
589
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
590
#else
591
            rt_kprintf("%s\n", index->name);
592
#endif
593 594 595
        }
    }

B
bernard 已提交
596
#ifndef FINSH_USING_MSH_ONLY
597 598 599 600 601 602 603 604 605 606
    /* list syscall list */
    syscall_item = global_syscall_list;
    while (syscall_item != NULL)
    {
        rt_kprintf("[l] %s\n", syscall_item->syscall.name);
        syscall_item = syscall_item->next;
    }

    rt_kprintf("--Variable List:\n");
    {
607
        struct finsh_sysvar *index;
608
        for (index = _sysvar_table_begin;
609 610
                index < _sysvar_table_end;
                FINSH_NEXT_SYSVAR(index))
611
        {
612
#ifdef FINSH_USING_DESCRIPTION
613
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
614
#else
615
            rt_kprintf("%s\n", index->name);
616
#endif
617 618
        }
    }
619

620 621 622 623 624 625
    sysvar_item = global_sysvar_list;
    while (sysvar_item != NULL)
    {
        rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
        sysvar_item = sysvar_item->next;
    }
B
bernard 已提交
626
#endif
627

628
    return 0;
629 630 631
}
FINSH_FUNCTION_EXPORT(list, list all symbol in system)

B
bernard 已提交
632
#ifndef FINSH_USING_MSH_ONLY
633
static int str_is_prefix(const char *prefix, const char *str)
634
{
635 636 637 638 639 640
    while ((*prefix) && (*prefix == *str))
    {
        prefix ++;
        str ++;
    }

641
    if (*prefix == 0)
D
dzzxzz@gmail.com 已提交
642 643
        return 0;

644 645 646
    return -1;
}

647
static int str_common(const char *str1, const char *str2)
648
{
649
    const char *str = str1;
650

651
    while ((*str != 0) && (*str2 != 0) && (*str == *str2))
652 653 654 655 656 657
    {
        str ++;
        str2 ++;
    }

    return (str - str1);
658 659
}

660
void list_prefix(char *prefix)
661
{
662 663
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
664 665
    rt_uint16_t func_cnt, var_cnt;
    int length, min_length;
666
    const char *name_ptr;
667 668 669

    func_cnt = 0;
    var_cnt  = 0;
D
dzzxzz@gmail.com 已提交
670
    min_length = 0;
671 672 673 674
    name_ptr = RT_NULL;

    /* checks in system function call */
    {
D
dzzxzz@gmail.com 已提交
675 676
        struct finsh_syscall *index;
        for (index = _syscall_table_begin;
677 678
                index < _syscall_table_end;
                FINSH_NEXT_SYSCALL(index))
679
        {
680 681 682
            /* skip internal command */
            if (str_is_prefix("__", index->name) == 0) continue;

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
            if (str_is_prefix(prefix, index->name) == 0)
            {
                if (func_cnt == 0)
                {
                    rt_kprintf("--function:\n");

                    if (*prefix != 0)
                    {
                        /* set name_ptr */
                        name_ptr = index->name;

                        /* set initial length */
                        min_length = strlen(name_ptr);
                    }
                }

                func_cnt ++;

                if (*prefix != 0)
                {
                    length = str_common(name_ptr, index->name);
                    if (length < min_length)
                        min_length = length;
                }
707 708

#ifdef FINSH_USING_DESCRIPTION
709
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
710
#else
711
                rt_kprintf("%s\n", index->name);
712
#endif
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
            }
        }
    }

    /* checks in dynamic system function call */
    syscall_item = global_syscall_list;
    while (syscall_item != NULL)
    {
        if (str_is_prefix(prefix, syscall_item->syscall.name) == 0)
        {
            if (func_cnt == 0)
            {
                rt_kprintf("--function:\n");
                if (*prefix != 0 && name_ptr == NULL)
                {
                    /* set name_ptr */
                    name_ptr = syscall_item->syscall.name;

                    /* set initial length */
                    min_length = strlen(name_ptr);
                }
            }

            func_cnt ++;

            if (*prefix != 0)
            {
                length = str_common(name_ptr, syscall_item->syscall.name);
                if (length < min_length)
                    min_length = length;
            }

            rt_kprintf("[l] %s\n", syscall_item->syscall.name);
        }
        syscall_item = syscall_item->next;
    }

    /* checks in system variable */
    {
752
        struct finsh_sysvar *index;
753
        for (index = _sysvar_table_begin;
754 755
                index < _sysvar_table_end;
                FINSH_NEXT_SYSVAR(index))
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
        {
            if (str_is_prefix(prefix, index->name) == 0)
            {
                if (var_cnt == 0)
                {
                    rt_kprintf("--variable:\n");

                    if (*prefix != 0 && name_ptr == NULL)
                    {
                        /* set name_ptr */
                        name_ptr = index->name;

                        /* set initial length */
                        min_length = strlen(name_ptr);

                    }
                }

                var_cnt ++;

                if (*prefix != 0)
                {
                    length = str_common(name_ptr, index->name);
                    if (length < min_length)
                        min_length = length;
                }
B
bernard.xiong 已提交
782

783
#ifdef FINSH_USING_DESCRIPTION
784
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
785
#else
786
                rt_kprintf("%s\n", index->name);
787
#endif
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
            }
        }
    }

    /* checks in dynamic system variable */
    sysvar_item = global_sysvar_list;
    while (sysvar_item != NULL)
    {
        if (str_is_prefix(prefix, sysvar_item->sysvar.name) == 0)
        {
            if (var_cnt == 0)
            {
                rt_kprintf("--variable:\n");
                if (*prefix != 0 && name_ptr == NULL)
                {
                    /* set name_ptr */
                    name_ptr = sysvar_item->sysvar.name;

                    /* set initial length */
                    min_length = strlen(name_ptr);
                }
            }

            var_cnt ++;

            if (*prefix != 0)
            {
                length = str_common(name_ptr, sysvar_item->sysvar.name);
                if (length < min_length)
                    min_length = length;
            }

            rt_kprintf("[v] %s\n", sysvar_item->sysvar.name);
        }
        sysvar_item = sysvar_item->next;
    }

    /* only one matched */
    if (name_ptr != NULL)
    {
        rt_strncpy(prefix, name_ptr, min_length);
    }
830
}
B
bernard 已提交
831
#endif
832

B
bernard 已提交
833
#if defined(FINSH_USING_SYMTAB) && !defined(FINSH_USING_MSH_ONLY)
834 835 836
static int dummy = 0;
FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
#endif
837 838 839

#endif /* RT_USING_FINSH */