cmd.c 24.7 KB
Newer Older
1 2 3 4 5 6 7
/*
 * File      : cmd.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
8
 * http://www.rt-thread.org/license/LICENSE
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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
27 28
 * 2010-04-21     yi.qiu       add list_module
 * 2012-04-29     goprife      improve the command line auto-complete feature.
29
 * 2012-06-02     lgnq         add list_memheap
30
 * 2012-10-22     Bernard      add MS VC++ patch.
31 32 33
 */

#include <rtthread.h>
34 35 36 37
#include "finsh.h"

rt_inline unsigned int rt_list_len(const rt_list_t *l)
{
38 39
    unsigned int len = 0;
    const rt_list_t *p = l;
40
    while (p->next != l)
41 42
    {
        p = p->next;
43
        len ++;
44
    }
D
dzzxzz@gmail.com 已提交
45

46
    return len;
M
mbbill 已提交
47
}
48

49
long hello(void)
50
{
51
    rt_kprintf("Hello RT-Thread!\n");
52

53
    return 0;
54
}
55
FINSH_FUNCTION_EXPORT(hello, say hello world);
56 57

extern void rt_show_version(void);
58
long version(void)
59
{
60
    rt_show_version();
61

62
    return 0;
63
}
64
FINSH_FUNCTION_EXPORT(version, show RT-Thread version information);
65 66 67

extern struct rt_object_information rt_object_container[];

68
static long _list_thread(struct rt_list_node *list)
69
{
70 71
    struct rt_thread *thread;
    struct rt_list_node *node;
72
    rt_uint8_t *ptr;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

    rt_kprintf(" thread  pri  status      sp     stack size max used   left tick  error\n");
    rt_kprintf("-------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
    for (node = list->next; node != list; node = node->next)
    {
        thread = rt_list_entry(node, struct rt_thread, list);
        rt_kprintf("%-8.*s 0x%02x", RT_NAME_MAX, thread->name, thread->current_priority);

        if (thread->stat == RT_THREAD_READY)        rt_kprintf(" ready  ");
        else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
        else if (thread->stat == RT_THREAD_INIT)    rt_kprintf(" init   ");
        else if (thread->stat == RT_THREAD_CLOSE)   rt_kprintf(" close  ");

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

        rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
            thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
            thread->stack_size,
            thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
            thread->remaining_tick,
            thread->error);
    }
96
	
97
    return 0;
98
}
qiuyiuestc's avatar
qiuyiuestc 已提交
99 100 101

long list_thread(void)
{
102
    return _list_thread(&rt_object_container[RT_Object_Class_Thread].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
103
}
104
FINSH_FUNCTION_EXPORT(list_thread, list thread);
105

106
static void show_wait_queue(struct rt_list_node *list)
107
{
108 109 110 111 112 113 114
    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 已提交
115

116
        if (node->next != list)
D
dzzxzz@gmail.com 已提交
117
            rt_kprintf("/");
118
    }
119 120 121
}

#ifdef RT_USING_SEMAPHORE
qiuyiuestc's avatar
qiuyiuestc 已提交
122
static long _list_sem(struct rt_list_node *list)
123
{
124 125 126 127 128 129 130
    struct rt_semaphore *sem;
    struct rt_list_node *node;

    rt_kprintf("semaphore v   suspend thread\n");
    rt_kprintf("--------  --- --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
131
        sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list));
D
dzzxzz@gmail.com 已提交
132
        if (!rt_list_isempty(&sem->parent.suspend_thread))
133
        {
D
dzzxzz@gmail.com 已提交
134 135 136 137 138
            rt_kprintf("%-8.*s  %03d %d:", 
                       RT_NAME_MAX,
                       sem->parent.parent.name,
                       sem->value,
                       rt_list_len(&sem->parent.suspend_thread));
139 140 141 142 143
            show_wait_queue(&(sem->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
D
dzzxzz@gmail.com 已提交
144 145 146 147 148
            rt_kprintf("%-8.*s  %03d %d\n",
                       RT_NAME_MAX,
                       sem->parent.parent.name,
                       sem->value,
                       rt_list_len(&sem->parent.suspend_thread));
149 150 151 152
        }
    }

    return 0;
153
}
qiuyiuestc's avatar
qiuyiuestc 已提交
154 155 156

long list_sem(void)
{
157
    return _list_sem(&rt_object_container[RT_Object_Class_Semaphore].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
158
}
159 160 161 162
FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system)
#endif

#ifdef RT_USING_EVENT
qiuyiuestc's avatar
qiuyiuestc 已提交
163
static long _list_event(struct rt_list_node *list)
164
{
165 166 167 168 169 170 171
    struct rt_event *e;
    struct rt_list_node *node;

    rt_kprintf("event    set        suspend thread\n");
    rt_kprintf("-------- ---------- --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
172 173
        e = (struct rt_event *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&e->parent.suspend_thread))
174
        {
D
dzzxzz@gmail.com 已提交
175 176 177 178 179
            rt_kprintf("%-8.*s  0x%08x %03d:",
                       RT_NAME_MAX,
                       e->parent.parent.name,
                       e->set,
                       rt_list_len(&e->parent.suspend_thread));
180 181 182 183 184
            show_wait_queue(&(e->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
D
dzzxzz@gmail.com 已提交
185 186
            rt_kprintf("%-8.*s  0x%08x 0\n",
                       RT_NAME_MAX, e->parent.parent.name, e->set);
187 188 189 190
        }
    }

    return 0;
191
}
qiuyiuestc's avatar
qiuyiuestc 已提交
192 193 194

long list_event(void)
{
195
    return _list_event(&rt_object_container[RT_Object_Class_Event].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
196
}
197 198 199 200
FINSH_FUNCTION_EXPORT(list_event, list event in system)
#endif

#ifdef RT_USING_MUTEX
qiuyiuestc's avatar
qiuyiuestc 已提交
201
static long _list_mutex(struct rt_list_node *list)
202
{
203 204 205 206 207 208 209
    struct rt_mutex *m;
    struct rt_list_node *node;

    rt_kprintf("mutex    owner    hold suspend thread\n");
    rt_kprintf("-------- -------- ---- --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
210
        m = (struct rt_mutex *)(rt_list_entry(node, struct rt_object, list));
D
dzzxzz@gmail.com 已提交
211 212 213 214 215 216 217
        rt_kprintf("%-8.*s %-8.*s %04d %d\n",
                   RT_NAME_MAX,
                   m->parent.parent.name,
                   RT_NAME_MAX,
                   m->owner->name,
                   m->hold,
                   rt_list_len(&m->parent.suspend_thread));
218 219 220
    }

    return 0;
221
}
qiuyiuestc's avatar
qiuyiuestc 已提交
222 223 224

long list_mutex(void)
{
225
    return _list_mutex(&rt_object_container[RT_Object_Class_Mutex].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
226
}
227 228 229 230
FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system)
#endif

#ifdef RT_USING_MAILBOX
qiuyiuestc's avatar
qiuyiuestc 已提交
231
static long _list_mailbox(struct rt_list_node *list)
232
{
233 234 235 236 237 238 239
    struct rt_mailbox *m;
    struct rt_list_node *node;

    rt_kprintf("mailbox  entry size suspend thread\n");
    rt_kprintf("-------- ----  ---- --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
240 241
        m = (struct rt_mailbox *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&m->parent.suspend_thread))
242
        {
D
dzzxzz@gmail.com 已提交
243 244 245 246 247 248
            rt_kprintf("%-8.*s %04d  %04d %d:",
                       RT_NAME_MAX,
                       m->parent.parent.name,
                       m->entry,
                       m->size,
                       rt_list_len(&m->parent.suspend_thread));
249 250 251 252 253
            show_wait_queue(&(m->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
D
dzzxzz@gmail.com 已提交
254 255 256 257 258 259
            rt_kprintf("%-8.*s %04d  %04d %d\n",
                       RT_NAME_MAX,
                       m->parent.parent.name,
                       m->entry,
                       m->size,
                       rt_list_len(&m->parent.suspend_thread));
260 261 262 263
        }
    }

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

long list_mailbox(void)
{
268
    return _list_mailbox(&rt_object_container[RT_Object_Class_MailBox].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
269
}
270 271 272 273
FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system)
#endif

#ifdef RT_USING_MESSAGEQUEUE
qiuyiuestc's avatar
qiuyiuestc 已提交
274
static long _list_msgqueue(struct rt_list_node *list)
275
{
276 277 278 279 280 281 282
    struct rt_messagequeue *m;
    struct rt_list_node *node;

    rt_kprintf("msgqueue entry suspend thread\n");
    rt_kprintf("-------- ----  --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
283 284
        m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&m->parent.suspend_thread))
285
        {
D
dzzxzz@gmail.com 已提交
286 287 288 289 290
            rt_kprintf("%-8.*s %04d  %d:",
                       RT_NAME_MAX,
                       m->parent.parent.name,
                       m->entry,
                       rt_list_len(&m->parent.suspend_thread));
291 292 293 294 295
            show_wait_queue(&(m->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
D
dzzxzz@gmail.com 已提交
296 297 298 299 300
            rt_kprintf("%-8.*s %04d  %d\n",
                       RT_NAME_MAX,
                       m->parent.parent.name,
                       m->entry,
                       rt_list_len(&m->parent.suspend_thread));
301 302 303 304
        }
    }

    return 0;
305
}
qiuyiuestc's avatar
qiuyiuestc 已提交
306 307 308

long list_msgqueue(void)
{
309
    return _list_msgqueue(&rt_object_container[RT_Object_Class_MessageQueue].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
310
}
311 312 313
FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system)
#endif

314 315 316 317 318 319
#ifdef RT_USING_MEMHEAP
static long _list_memheap(struct rt_list_node *list)
{
    struct rt_memheap *mh;
    struct rt_list_node *node;

320 321
    rt_kprintf("memheap  pool size  max used size available size\n");
    rt_kprintf("-------- ---------- ------------- --------------\n");
322 323 324 325
    for (node = list->next; node != list; node = node->next)
    {
        mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);

D
dzzxzz@gmail.com 已提交
326 327 328 329 330 331
        rt_kprintf("%-8.*s %-010d %-013d %-05d\n",
                   RT_NAME_MAX,
                   mh->parent.name,
                   mh->pool_size,
                   mh->max_used_size,
                   mh->available_size);
332 333 334 335 336 337 338 339 340 341 342 343
    }

    return 0;
}

long list_memheap(void)
{
    return _list_memheap(&rt_object_container[RT_Object_Class_MemHeap].object_list);
}
FINSH_FUNCTION_EXPORT(list_memheap, list memory heap in system)
#endif

344
#ifdef RT_USING_MEMPOOL
qiuyiuestc's avatar
qiuyiuestc 已提交
345
static long _list_mempool(struct rt_list_node *list)
346
{
347 348 349 350 351 352 353
    struct rt_mempool *mp;
    struct rt_list_node *node;

    rt_kprintf("mempool  block total free suspend thread\n");
    rt_kprintf("-------- ----  ----  ---- --------------\n");
    for (node = list->next; node != list; node = node->next)
    {
354
        mp = (struct rt_mempool *)rt_list_entry(node, struct rt_object, list);
355 356
        if (mp->suspend_thread_count > 0)
        {
D
dzzxzz@gmail.com 已提交
357 358 359 360 361 362 363
            rt_kprintf("%-8.*s %04d  %04d  %04d %d:",
                       RT_NAME_MAX,
                       mp->parent.name,
                       mp->block_size,
                       mp->block_total_count,
                       mp->block_free_count,
                       mp->suspend_thread_count);
364
            show_wait_queue(&(mp->suspend_thread));
D
dzzxzz@gmail.com 已提交
365
            rt_kprintf("\n");
366 367 368
        }
        else
        {
D
dzzxzz@gmail.com 已提交
369 370 371 372 373 374 375
            rt_kprintf("%-8.*s %04d  %04d  %04d %d\n",
                       RT_NAME_MAX,
                       mp->parent.name,
                       mp->block_size,
                       mp->block_total_count,
                       mp->block_free_count,
                       mp->suspend_thread_count);
376 377 378 379
        }
    }

    return 0;
380
}
qiuyiuestc's avatar
qiuyiuestc 已提交
381 382 383

long list_mempool(void)
{
384
    return _list_mempool(&rt_object_container[RT_Object_Class_MemPool].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
385
}
386 387 388
FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system)
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
389
static long _list_timer(struct rt_list_node *list)
390
{
391 392 393 394 395 396 397
    struct rt_timer *timer;
    struct rt_list_node *node;

    rt_kprintf("timer    periodic   timeout    flag\n");
    rt_kprintf("-------- ---------- ---------- -----------\n");
    for (node = list->next; node != list; node = node->next)
    {
398
        timer = (struct rt_timer *)(rt_list_entry(node, struct rt_object, list));
D
dzzxzz@gmail.com 已提交
399 400 401 402 403
        rt_kprintf("%-8.*s 0x%08x 0x%08x ",
                   RT_NAME_MAX,
                   timer->parent.name,
                   timer->init_tick,
                   timer->timeout_tick);
404
        if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
D
dzzxzz@gmail.com 已提交
405
            rt_kprintf("activated\n");
406
        else
D
dzzxzz@gmail.com 已提交
407
            rt_kprintf("deactivated\n");
408 409 410 411 412
    }

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

    return 0;
413
}
qiuyiuestc's avatar
qiuyiuestc 已提交
414 415 416

long list_timer(void)
{
417
    return _list_timer(&rt_object_container[RT_Object_Class_Timer].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
418
}
419 420 421
FINSH_FUNCTION_EXPORT(list_timer, list timer in system)

#ifdef RT_USING_DEVICE
qiuyiuestc's avatar
qiuyiuestc 已提交
422
static long _list_device(struct rt_list_node *list)
423
{
424 425
    struct rt_device *device;
    struct rt_list_node *node;
426
    char * const device_type_str[] =
427 428 429 430 431 432 433 434
    {
        "Character Device",
        "Block Device",
        "Network Interface",
        "MTD Device",
        "CAN Device",
        "RTC",
        "Sound Device",
435
        "Graphic Device",
436
        "I2C Bus",
437 438 439 440 441
        "USB Slave Device",
        "USB Host Bus",
        "SPI Bus",
        "SPI Device",
        "SDIO Bus",
D
dzzxzz@gmail.com 已提交
442
        "PM Pseudo Device",
443 444 445 446 447 448 449
        "Unknown"
    };

    rt_kprintf("device    type      \n");
    rt_kprintf("-------- ---------- \n");
    for (node = list->next; node != list; node = node->next)
    {
450
        device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
D
dzzxzz@gmail.com 已提交
451 452 453 454 455 456
        rt_kprintf("%-8.*s %-8s \n",
                   RT_NAME_MAX,
                   device->parent.name,
                   (device->type <= RT_Device_Class_Unknown) ?
                   device_type_str[device->type] :
                   device_type_str[RT_Device_Class_Unknown]);
457 458 459
    }

    return 0;
460
}
qiuyiuestc's avatar
qiuyiuestc 已提交
461 462 463

long list_device(void)
{
464
    return _list_device(&rt_object_container[RT_Object_Class_Device].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
465
}
466 467 468
FINSH_FUNCTION_EXPORT(list_device, list device in system)
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
469
#ifdef RT_USING_MODULE
qiuyiuestc's avatar
qiuyiuestc 已提交
470 471
#include <rtm.h>

472
int list_module(void)
qiuyiuestc's avatar
qiuyiuestc 已提交
473
{
474 475
    struct rt_module *module;
    struct rt_list_node *list, *node;
qiuyiuestc's avatar
qiuyiuestc 已提交
476

477
    list = &rt_object_container[RT_Object_Class_Module].object_list;
qiuyiuestc's avatar
qiuyiuestc 已提交
478

479 480 481 482
    rt_kprintf("module name     ref\n");
    rt_kprintf("------------ --------\n");
    for (node = list->next; node != list; node = node->next)
    {
483
        module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list));
D
dzzxzz@gmail.com 已提交
484 485
        rt_kprintf("%-16.*s %-04d\n",
                   RT_NAME_MAX, module->parent.name, module->nref);
486
    }
M
mbbill@gmail.com 已提交
487

488
    return 0;
qiuyiuestc's avatar
qiuyiuestc 已提交
489
}
qiuyiuestc's avatar
qiuyiuestc 已提交
490

qiuyiuestc's avatar
qiuyiuestc 已提交
491
FINSH_FUNCTION_EXPORT(list_module, list module in system)
qiuyiuestc's avatar
qiuyiuestc 已提交
492

493
int list_mod_detail(const char *name)
qiuyiuestc's avatar
qiuyiuestc 已提交
494
{
495 496 497 498
    int i;
    struct rt_module *module;
    
    /* find module */
499
    if ((module = rt_module_find(name)) != RT_NULL)
500 501
    {
        /* module has entry point */
502
        if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
503 504 505
        {   
            struct rt_thread *thread;
            struct rt_list_node *tlist;
506
            rt_uint8_t *ptr;
507 508

            /* list main thread in module */
509
            if (module->module_thread != RT_NULL)
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
            {   
                rt_kprintf("main thread  pri  status      sp     stack size max used   left tick  error\n");
                rt_kprintf("------------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
                thread = module->module_thread;
                rt_kprintf("%-8.*s 0x%02x", RT_NAME_MAX, thread->name, thread->current_priority);

                if (thread->stat == RT_THREAD_READY)        rt_kprintf(" ready  ");
                else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
                else if (thread->stat == RT_THREAD_INIT)    rt_kprintf(" init   ");

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

                rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
                    thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
                    thread->stack_size,
                    thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
                    thread->remaining_tick,
                    thread->error);
            }   

            /* list sub thread in module */
            tlist = &module->module_object[RT_Object_Class_Thread].object_list;
533
            if (!rt_list_isempty(tlist)) _list_thread(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
534
#ifdef RT_USING_SEMAPHORE
535 536
            /* list semaphored in module */
            tlist = &module->module_object[RT_Object_Class_Semaphore].object_list;
537
            if (!rt_list_isempty(tlist)) _list_sem(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
538 539
#endif
#ifdef RT_USING_MUTEX
540 541
            /* list mutex in module */
            tlist = &module->module_object[RT_Object_Class_Mutex].object_list;
542
            if (!rt_list_isempty(tlist)) _list_mutex(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
543 544
#endif
#ifdef RT_USING_EVENT
545 546
            /* list event in module */
            tlist = &module->module_object[RT_Object_Class_Event].object_list;
547
            if (!rt_list_isempty(tlist)) _list_event(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
548 549
#endif
#ifdef RT_USING_MAILBOX
550 551
            /* list mailbox in module */
            tlist = &module->module_object[RT_Object_Class_MailBox].object_list;
552
            if (!rt_list_isempty(tlist)) _list_mailbox(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
553 554
#endif
#ifdef RT_USING_MESSAGEQUEUE
555 556
            /* list message queue in module */
            tlist = &module->module_object[RT_Object_Class_MessageQueue].object_list;
557 558 559 560 561 562
            if (!rt_list_isempty(tlist)) _list_msgqueue(tlist);
#endif
#ifdef RT_USING_MEMHEAP
            /* list memory heap in module */
            tlist = &module->module_object[RT_Object_Class_MemHeap].object_list;
            if (!rt_list_isempty(tlist)) _list_memheap(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
563 564
#endif
#ifdef RT_USING_MEMPOOL
565 566
            /* list memory pool in module */
            tlist = &module->module_object[RT_Object_Class_MemPool].object_list;
567
            if (!rt_list_isempty(tlist)) _list_mempool(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
568 569
#endif
#ifdef RT_USING_DEVICE
570 571
            /* list device in module */
            tlist = &module->module_object[RT_Object_Class_Device].object_list;
572
            if (!rt_list_isempty(tlist)) _list_device(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
573
#endif
574 575
            /* list timer in module */
            tlist = &module->module_object[RT_Object_Class_Timer].object_list;
576
            if (!rt_list_isempty(tlist)) _list_timer(tlist);
577 578 579 580 581 582
        }

        rt_kprintf("symbol    address   \n");
        rt_kprintf("-------- ----------\n");
    
        /* list module export symbols */
583
        for (i=0; i<module->nsym; i++)
584
        {
D
dzzxzz@gmail.com 已提交
585 586 587
            rt_kprintf("%s 0x%x\n",
                       module->symtab[i].name, module->symtab[i].addr);
        }
588 589 590
    }

    return 0;
qiuyiuestc's avatar
qiuyiuestc 已提交
591
}
qiuyiuestc's avatar
qiuyiuestc 已提交
592
FINSH_FUNCTION_EXPORT(list_mod_detail, list module objects in system)
qiuyiuestc's avatar
qiuyiuestc 已提交
593 594
#endif

D
dzzxzz 已提交
595
long list(void)
596
{
597 598
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
599 600 601

    rt_kprintf("--Function List:\n");
    {
602
        struct finsh_syscall *index;
D
dzzxzz@gmail.com 已提交
603 604 605
        for (index = _syscall_table_begin;
             index < _syscall_table_end;
             FINSH_NEXT_SYSCALL(index))
606
        {
607
#ifdef FINSH_USING_DESCRIPTION
608
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
609
#else
610
            rt_kprintf("%s\n", index->name);
611
#endif
612 613 614 615 616 617 618 619 620 621 622 623 624
        }
    }

    /* 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");
    {
625
        struct finsh_sysvar *index;
626 627
        for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
        {
628
#ifdef FINSH_USING_DESCRIPTION
629
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
630
#else
631
            rt_kprintf("%s\n", index->name);
632
#endif
633 634
        }
    }
635

636 637 638 639 640 641
    sysvar_item = global_sysvar_list;
    while (sysvar_item != NULL)
    {
        rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
        sysvar_item = sysvar_item->next;
    }
642

643
    return 0;
644 645 646
}
FINSH_FUNCTION_EXPORT(list, list all symbol in system)

647
static int str_is_prefix(const char *prefix, const char *str)
648
{
649 650 651 652 653 654
    while ((*prefix) && (*prefix == *str))
    {
        prefix ++;
        str ++;
    }

655
    if (*prefix == 0)
D
dzzxzz@gmail.com 已提交
656 657
        return 0;

658 659 660
    return -1;
}

661
static int str_common(const char *str1, const char *str2)
662
{
663
    const char *str = str1;
664

665
    while ((*str != 0) && (*str2 != 0) && (*str == *str2))
666 667 668 669 670 671
    {
        str ++;
        str2 ++;
    }

    return (str - str1);
672 673
}

674
void list_prefix(char *prefix)
675
{
676 677
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
678 679
    rt_uint16_t func_cnt, var_cnt;
    int length, min_length;
680
    const char *name_ptr;
681 682 683

    func_cnt = 0;
    var_cnt  = 0;
D
dzzxzz@gmail.com 已提交
684
    min_length = 0;
685 686 687 688
    name_ptr = RT_NULL;

    /* checks in system function call */
    {
D
dzzxzz@gmail.com 已提交
689 690 691 692
        struct finsh_syscall *index;
        for (index = _syscall_table_begin;
             index < _syscall_table_end;
             FINSH_NEXT_SYSCALL(index))
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
        {
            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;
                }
718 719

#ifdef FINSH_USING_DESCRIPTION
720
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
721
#else
722
                rt_kprintf("%s\n", index->name);
723
#endif
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 752 753 754 755 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 782 783 784 785 786 787 788 789 790
            }
        }
    }

    /* 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 */
    {
        struct finsh_sysvar* index;
        for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
        {
            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 已提交
791

792
#ifdef FINSH_USING_DESCRIPTION
793
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
794
#else
795
                rt_kprintf("%s\n", index->name);
796
#endif
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 830 831 832 833 834 835 836 837 838
            }
        }
    }

    /* 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);
    }
839 840
}

841 842 843 844
#ifdef FINSH_USING_SYMTAB
static int dummy = 0;
FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
#endif