cmd.c 31.5 KB
Newer Older
1
/*
B
Bernard Xiong 已提交
2
 *  RT-Thread finsh shell commands
3
 *
B
Bernard Xiong 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
 *
 *  This file is part of RT-Thread (http://www.rt-thread.org)
 *  Maintainer: bernard.xiong <bernard.xiong at gmail.com>
 *
 *  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * 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
42 43
 * 2010-04-21     yi.qiu       add list_module
 * 2012-04-29     goprife      improve the command line auto-complete feature.
44
 * 2012-06-02     lgnq         add list_memheap
45
 * 2012-10-22     Bernard      add MS VC++ patch.
46
 * 2016-06-02     armink       beautify all list object information command
47 48 49
 */

#include <rtthread.h>
50 51 52 53
#include "finsh.h"

rt_inline unsigned int rt_list_len(const rt_list_t *l)
{
54 55
    unsigned int len = 0;
    const rt_list_t *p = l;
56
    while (p->next != l)
57 58
    {
        p = p->next;
59
        len ++;
60
    }
D
dzzxzz@gmail.com 已提交
61

62
    return len;
M
mbbill 已提交
63
}
64

65
long hello(void)
66
{
67
    rt_kprintf("Hello RT-Thread!\n");
68

69
    return 0;
70
}
71
FINSH_FUNCTION_EXPORT(hello, say hello world);
72 73

extern void rt_show_version(void);
74
long version(void)
75
{
76
    rt_show_version();
77

78
    return 0;
79
}
80
FINSH_FUNCTION_EXPORT(version, show RT-Thread version information);
B
Bernard Xiong 已提交
81
MSH_CMD_EXPORT(version, show RT-Thread version information);
82 83 84

extern struct rt_object_information rt_object_container[];

85 86 87 88 89 90 91 92 93 94 95
/**
 * Preprocessing the object name show information
 *
 * @param list object list
 * @param type object type e.g., thread, semaphore
 * @param header object name header information
 * @param split_sign the split sign is using '-' between the object name header and object name table
 * @param max_len the maximum object name length in list
 */
static void pre_obj_name_show_info(struct rt_list_node *list, const char *type, char *header,
        char *split_sign, rt_size_t *max_len) {
96
    struct rt_list_node *node;
97 98
    struct rt_object *node_obj = NULL;
    rt_size_t i, len, type_len = rt_strlen(type), type_index;
99

100 101
    *max_len = 0;
    /* calculate the maximum object name length */
102
    for (node = list->next; node != list; node = node->next) {
103 104 105 106
        node_obj = rt_list_entry(node, struct rt_object, list);
        len = rt_strlen(node_obj->name);
        if (*max_len < len) {
            *max_len = len;
107 108 109
        }
    }

110 111
    if (*max_len < type_len) {
        *max_len = type_len;
112
    }
113 114 115 116 117 118 119 120 121 122 123
    /* calculate the object type string in header index */
    type_index = (*max_len - type_len) / 2;

    for (i = 0; i < *max_len; i ++) {
        split_sign[i] = '-';
        if (type_len <= *max_len) {
            if (i < type_index) {
                header[i] = ' ';
                header[*max_len - i - 1] = ' ';
            } else if (i < type_index + type_len) {
                header[i] = type[i - type_index];
124
            } else {
125
                header[i] = ' ';
126 127 128
            }
        }
    }
129 130 131
    header[*max_len] = '\0';
    split_sign[*max_len] = '\0';
}
132

133 134 135 136 137 138 139 140 141 142 143 144
static long _list_thread(struct rt_list_node *list)
{
    struct rt_thread *thread;
    struct rt_list_node *node;
    rt_uint8_t *ptr;
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "thread", name_header, name_split_sign, &max_name_len);

    rt_kprintf("%s pri status      sp     stack size max used left tick  error\n", name_header);
    rt_kprintf("%s --  ------- ---------- ----------    ---   ---------- ---\n", name_split_sign);
145 146 147
    for (node = list->next; node != list; node = node->next)
    {
        thread = rt_list_entry(node, struct rt_thread, list);
148 149 150 151
        rt_kprintf("%-*.s %02d ",
                   max_name_len,
                   thread->name,
                   thread->current_priority);
152 153 154 155 156 157

        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  ");

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

161
        rt_kprintf(" 0x%08x 0x%08x    %02d%%   0x%08x %03d\n",
162 163
                   thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
                   thread->stack_size,
164 165
                   (thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t) thread->stack_addr)) * 100
                        / thread->stack_size,
166 167
                   thread->remaining_tick,
                   thread->error);
168
    }
169

170
    return 0;
171
}
qiuyiuestc's avatar
qiuyiuestc 已提交
172 173 174

long list_thread(void)
{
175
    return _list_thread(&rt_object_container[RT_Object_Class_Thread].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
176
}
177
FINSH_FUNCTION_EXPORT(list_thread, list thread);
B
Bernard Xiong 已提交
178
MSH_CMD_EXPORT(list_thread, list thread);
179

180
static void show_wait_queue(struct rt_list_node *list)
181
{
182 183 184 185 186 187 188
    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 已提交
189

190
        if (node->next != list)
D
dzzxzz@gmail.com 已提交
191
            rt_kprintf("/");
192
    }
193 194 195
}

#ifdef RT_USING_SEMAPHORE
qiuyiuestc's avatar
qiuyiuestc 已提交
196
static long _list_sem(struct rt_list_node *list)
197
{
198 199
    struct rt_semaphore *sem;
    struct rt_list_node *node;
200 201 202 203
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "semaphore", name_header, name_split_sign, &max_name_len);
204

205 206
    rt_kprintf("%s v   suspend thread\n", name_header);
    rt_kprintf("%s --- --------------\n", name_split_sign);
207 208
    for (node = list->next; node != list; node = node->next)
    {
209
        sem = (struct rt_semaphore *)(rt_list_entry(node, struct rt_object, list));
D
dzzxzz@gmail.com 已提交
210
        if (!rt_list_isempty(&sem->parent.suspend_thread))
211
        {
212 213
            rt_kprintf("%-*.s %03d %d:",
                       max_name_len,
D
dzzxzz@gmail.com 已提交
214 215 216
                       sem->parent.parent.name,
                       sem->value,
                       rt_list_len(&sem->parent.suspend_thread));
217 218 219 220 221
            show_wait_queue(&(sem->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
222 223
            rt_kprintf("%-*.s %03d %d\n",
                       max_name_len,
D
dzzxzz@gmail.com 已提交
224 225 226
                       sem->parent.parent.name,
                       sem->value,
                       rt_list_len(&sem->parent.suspend_thread));
227 228 229 230
        }
    }

    return 0;
231
}
qiuyiuestc's avatar
qiuyiuestc 已提交
232 233 234

long list_sem(void)
{
235
    return _list_sem(&rt_object_container[RT_Object_Class_Semaphore].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
236
}
B
Bernard Xiong 已提交
237 238
FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system);
MSH_CMD_EXPORT(list_sem, list semaphore in system);
239 240 241
#endif

#ifdef RT_USING_EVENT
qiuyiuestc's avatar
qiuyiuestc 已提交
242
static long _list_event(struct rt_list_node *list)
243
{
244 245
    struct rt_event *e;
    struct rt_list_node *node;
246 247 248 249
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "event", name_header, name_split_sign, &max_name_len);
250

251 252
    rt_kprintf("%s set        suspend thread\n", name_header);
    rt_kprintf("%s ---------- --------------\n", name_split_sign);
253 254
    for (node = list->next; node != list; node = node->next)
    {
255 256
        e = (struct rt_event *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&e->parent.suspend_thread))
257
        {
258 259
            rt_kprintf("%-*.s 0x%08x %03d:",
                       max_name_len,
D
dzzxzz@gmail.com 已提交
260 261 262
                       e->parent.parent.name,
                       e->set,
                       rt_list_len(&e->parent.suspend_thread));
263 264 265 266 267
            show_wait_queue(&(e->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
268 269 270 271
            rt_kprintf("%-*.s 0x%08x 0\n",
                       max_name_len,
                       e->parent.parent.name,
                       e->set);
272 273 274 275
        }
    }

    return 0;
276
}
qiuyiuestc's avatar
qiuyiuestc 已提交
277 278 279

long list_event(void)
{
280
    return _list_event(&rt_object_container[RT_Object_Class_Event].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
281
}
B
Bernard Xiong 已提交
282 283
FINSH_FUNCTION_EXPORT(list_event, list event in system);
MSH_CMD_EXPORT(list_event, list event in system);
284 285 286
#endif

#ifdef RT_USING_MUTEX
qiuyiuestc's avatar
qiuyiuestc 已提交
287
static long _list_mutex(struct rt_list_node *list)
288
{
289 290
    struct rt_mutex *m;
    struct rt_list_node *node;
291 292 293 294
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "mutex", name_header, name_split_sign, &max_name_len);
295

296 297
    rt_kprintf("%s owner    hold suspend thread\n", name_header);
    rt_kprintf("%s -------- ---- --------------\n", name_split_sign);
298 299
    for (node = list->next; node != list; node = node->next)
    {
300
        m = (struct rt_mutex *)(rt_list_entry(node, struct rt_object, list));
301 302
        rt_kprintf("%-*.s %-8.*s %04d %d\n",
                   max_name_len,
D
dzzxzz@gmail.com 已提交
303 304 305 306 307
                   m->parent.parent.name,
                   RT_NAME_MAX,
                   m->owner->name,
                   m->hold,
                   rt_list_len(&m->parent.suspend_thread));
308 309 310
    }

    return 0;
311
}
qiuyiuestc's avatar
qiuyiuestc 已提交
312 313 314

long list_mutex(void)
{
315
    return _list_mutex(&rt_object_container[RT_Object_Class_Mutex].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
316
}
B
Bernard Xiong 已提交
317 318
FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system);
MSH_CMD_EXPORT(list_mutex, list mutex in system);
319 320 321
#endif

#ifdef RT_USING_MAILBOX
qiuyiuestc's avatar
qiuyiuestc 已提交
322
static long _list_mailbox(struct rt_list_node *list)
323
{
324 325
    struct rt_mailbox *m;
    struct rt_list_node *node;
326 327 328 329
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "mailbox", name_header, name_split_sign, &max_name_len);
330

331 332
    rt_kprintf("%s entry size suspend thread\n", name_header);
    rt_kprintf("%s ----  ---- --------------\n", name_split_sign);
333 334
    for (node = list->next; node != list; node = node->next)
    {
335 336
        m = (struct rt_mailbox *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&m->parent.suspend_thread))
337
        {
338 339
            rt_kprintf("%-*.s %04d  %04d %d:",
                       max_name_len,
D
dzzxzz@gmail.com 已提交
340 341 342 343
                       m->parent.parent.name,
                       m->entry,
                       m->size,
                       rt_list_len(&m->parent.suspend_thread));
344 345 346 347 348
            show_wait_queue(&(m->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
349 350
            rt_kprintf("%-*.s %04d  %04d %d\n",
                       max_name_len,
D
dzzxzz@gmail.com 已提交
351 352 353 354
                       m->parent.parent.name,
                       m->entry,
                       m->size,
                       rt_list_len(&m->parent.suspend_thread));
355 356 357 358
        }
    }

    return 0;
359
}
qiuyiuestc's avatar
qiuyiuestc 已提交
360 361 362

long list_mailbox(void)
{
363
    return _list_mailbox(&rt_object_container[RT_Object_Class_MailBox].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
364
}
B
Bernard Xiong 已提交
365 366
FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system);
MSH_CMD_EXPORT(list_mailbox, list mail box in system);
367 368 369
#endif

#ifdef RT_USING_MESSAGEQUEUE
qiuyiuestc's avatar
qiuyiuestc 已提交
370
static long _list_msgqueue(struct rt_list_node *list)
371
{
372 373
    struct rt_messagequeue *m;
    struct rt_list_node *node;
374 375 376 377
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "msgqueue", name_header, name_split_sign, &max_name_len);
378

379 380
    rt_kprintf("%s entry suspend thread\n", name_header);
    rt_kprintf("%s ----  --------------\n", name_split_sign);
381 382
    for (node = list->next; node != list; node = node->next)
    {
383 384
        m = (struct rt_messagequeue *)(rt_list_entry(node, struct rt_object, list));
        if (!rt_list_isempty(&m->parent.suspend_thread))
385
        {
386 387
            rt_kprintf("%-*.s %04d  %d:",
                       max_name_len,
D
dzzxzz@gmail.com 已提交
388 389 390
                       m->parent.parent.name,
                       m->entry,
                       rt_list_len(&m->parent.suspend_thread));
391 392 393 394 395
            show_wait_queue(&(m->parent.suspend_thread));
            rt_kprintf("\n");
        }
        else
        {
396 397
            rt_kprintf("%-*.s %04d  %d\n",
                       max_name_len,
D
dzzxzz@gmail.com 已提交
398 399 400
                       m->parent.parent.name,
                       m->entry,
                       rt_list_len(&m->parent.suspend_thread));
401 402 403 404
        }
    }

    return 0;
405
}
qiuyiuestc's avatar
qiuyiuestc 已提交
406 407 408

long list_msgqueue(void)
{
409
    return _list_msgqueue(&rt_object_container[RT_Object_Class_MessageQueue].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
410
}
B
Bernard Xiong 已提交
411 412
FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system);
MSH_CMD_EXPORT(list_msgqueue, list message queue in system);
413 414
#endif

415 416 417 418 419
#ifdef RT_USING_MEMHEAP
static long _list_memheap(struct rt_list_node *list)
{
    struct rt_memheap *mh;
    struct rt_list_node *node;
420 421 422
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t mart_size_t max_name_len = 0* preprocessing the object name show information */
    pre_obj_name_show_info(list, "memheap", name_header, name_split_sign, &max_name_len);
423

424 425
    rt_kprintf("%s pool size  max used size available size\n", name_header);
    rt_kprintf("%s ---------- ------------- --------------\n", name_split_sign);
426 427 428 429
    for (node = list->next; node != list; node = node->next)
    {
        mh = (struct rt_memheap *)rt_list_entry(node, struct rt_object, list);

430 431
        rt_kprintf("%-*.s %-010d %-013d %-05d\n",
                   max_name_len,
D
dzzxzz@gmail.com 已提交
432 433 434 435
                   mh->parent.name,
                   mh->pool_size,
                   mh->max_used_size,
                   mh->available_size);
436 437 438 439 440 441 442 443 444
    }

    return 0;
}

long list_memheap(void)
{
    return _list_memheap(&rt_object_container[RT_Object_Class_MemHeap].object_list);
}
B
Bernard Xiong 已提交
445 446
FINSH_FUNCTION_EXPORT(list_memheap, list memory heap in system);
MSH_CMD_EXPORT(list_memheap, list memory heap in system);
447 448
#endif

449
#ifdef RT_USING_MEMPOOL
qiuyiuestc's avatar
qiuyiuestc 已提交
450
static long _list_mempool(struct rt_list_node *list)
451
{
452 453
    struct rt_mempool *mp;
    struct rt_list_node *node;
454 455 456 457
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "mempool", name_header, name_split_sign, &max_name_len);
458

459 460
    rt_kprintf("%s block total free suspend thread\n", name_header);
    rt_kprintf("%s ----  ----  ---- --------------\n", name_split_sign);
461 462
    for (node = list->next; node != list; node = node->next)
    {
463
        mp = (struct rt_mempool *)rt_list_entry(node, struct rt_object, list);
464 465
        if (mp->suspend_thread_count > 0)
        {
466 467
            rt_kprintf("%-*.s %04d  %04d  %04d %d:",
                       max_name_len,
D
dzzxzz@gmail.com 已提交
468 469 470 471 472
                       mp->parent.name,
                       mp->block_size,
                       mp->block_total_count,
                       mp->block_free_count,
                       mp->suspend_thread_count);
473
            show_wait_queue(&(mp->suspend_thread));
D
dzzxzz@gmail.com 已提交
474
            rt_kprintf("\n");
475 476 477
        }
        else
        {
478 479
            rt_kprintf("%-*.s %04d  %04d  %04d %d\n",
                       max_name_len,
D
dzzxzz@gmail.com 已提交
480 481 482 483 484
                       mp->parent.name,
                       mp->block_size,
                       mp->block_total_count,
                       mp->block_free_count,
                       mp->suspend_thread_count);
485 486 487 488
        }
    }

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

long list_mempool(void)
{
493
    return _list_mempool(&rt_object_container[RT_Object_Class_MemPool].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
494
}
495
FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system)
B
Bernard Xiong 已提交
496
MSH_CMD_EXPORT(list_mempool, list memory pool in system);
497 498
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
499
static long _list_timer(struct rt_list_node *list)
500
{
501 502
    struct rt_timer *timer;
    struct rt_list_node *node;
503 504 505 506
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "timer", name_header, name_split_sign, &max_name_len);
507

508 509
    rt_kprintf("%s periodic   timeout    flag\n", name_header);
    rt_kprintf("%s ---------- ---------- -----------\n", name_split_sign);
510 511
    for (node = list->next; node != list; node = node->next)
    {
512
        timer = (struct rt_timer *)(rt_list_entry(node, struct rt_object, list));
513 514
        rt_kprintf("%-*.s 0x%08x 0x%08x ",
                   max_name_len,
D
dzzxzz@gmail.com 已提交
515 516 517
                   timer->parent.name,
                   timer->init_tick,
                   timer->timeout_tick);
518
        if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
D
dzzxzz@gmail.com 已提交
519
            rt_kprintf("activated\n");
520
        else
D
dzzxzz@gmail.com 已提交
521
            rt_kprintf("deactivated\n");
522 523 524 525 526
    }

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

    return 0;
527
}
qiuyiuestc's avatar
qiuyiuestc 已提交
528 529 530

long list_timer(void)
{
531
    return _list_timer(&rt_object_container[RT_Object_Class_Timer].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
532
}
B
Bernard Xiong 已提交
533 534
FINSH_FUNCTION_EXPORT(list_timer, list timer in system);
MSH_CMD_EXPORT(list_timer, list timer in system);
535 536

#ifdef RT_USING_DEVICE
qiuyiuestc's avatar
qiuyiuestc 已提交
537
static long _list_device(struct rt_list_node *list)
538
{
539 540
    struct rt_device *device;
    struct rt_list_node *node;
541
    char *const device_type_str[] =
542 543 544 545 546 547 548 549
    {
        "Character Device",
        "Block Device",
        "Network Interface",
        "MTD Device",
        "CAN Device",
        "RTC",
        "Sound Device",
550
        "Graphic Device",
551
        "I2C Bus",
552 553 554 555 556
        "USB Slave Device",
        "USB Host Bus",
        "SPI Bus",
        "SPI Device",
        "SDIO Bus",
557
        "PM Pseudo Device",
558 559
        "Pipe",
        "Portal Device",
560 561
        "Timer Device",
        "Miscellaneous Device",
562 563
        "Unknown"
    };
564 565 566 567
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "device", name_header, name_split_sign, &max_name_len);
568

569 570
    rt_kprintf("%s type                 ref count\n", name_header);
    rt_kprintf("%s -------------------- ----------\n", name_split_sign);
571 572
    for (node = list->next; node != list; node = node->next)
    {
573
        device = (struct rt_device *)(rt_list_entry(node, struct rt_object, list));
574 575
        rt_kprintf("%-*.s %-20s %-8d\n",
                   max_name_len,
D
dzzxzz@gmail.com 已提交
576 577 578
                   device->parent.name,
                   (device->type <= RT_Device_Class_Unknown) ?
                   device_type_str[device->type] :
579 580
                   device_type_str[RT_Device_Class_Unknown],
                   device->ref_count);
581 582 583
    }

    return 0;
584
}
qiuyiuestc's avatar
qiuyiuestc 已提交
585 586 587

long list_device(void)
{
588
    return _list_device(&rt_object_container[RT_Object_Class_Device].object_list);
qiuyiuestc's avatar
qiuyiuestc 已提交
589
}
B
Bernard Xiong 已提交
590 591
FINSH_FUNCTION_EXPORT(list_device, list device in system);
MSH_CMD_EXPORT(list_device, list device in system);
592 593
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
594
#ifdef RT_USING_MODULE
qiuyiuestc's avatar
qiuyiuestc 已提交
595 596
#include <rtm.h>

597
int list_module(void)
qiuyiuestc's avatar
qiuyiuestc 已提交
598
{
599 600
    struct rt_module *module;
    struct rt_list_node *list, *node;
601 602 603 604
    char name_header[RT_NAME_MAX], name_split_sign[RT_NAME_MAX];
    rt_size_t max_name_len = 0;
    /* preprocessing the object name show information */
    pre_obj_name_show_info(list, "module", name_header, name_split_sign, &max_name_len);
qiuyiuestc's avatar
qiuyiuestc 已提交
605

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

608 609
    rt_kprintf("%s    ref      address \n", name_header);
    rt_kprintf("%s -------- ------------\n", name_split_sign);
610 611
    for (node = list->next; node != list; node = node->next)
    {
612
        module = (struct rt_module *)(rt_list_entry(node, struct rt_object, list));
613 614 615 616 617
        rt_kprintf("%-*.s %-04d  0x%08x\n",
                   max_name_len,
                   module->parent.name,
                   module->nref,
                   module->module_space);
618
    }
M
mbbill@gmail.com 已提交
619

620
    return 0;
qiuyiuestc's avatar
qiuyiuestc 已提交
621
}
B
bernard 已提交
622 623
FINSH_FUNCTION_EXPORT(list_module, list module in system);
MSH_CMD_EXPORT(list_module, list module in system);
qiuyiuestc's avatar
qiuyiuestc 已提交
624

625
int list_mod_detail(const char *name)
qiuyiuestc's avatar
qiuyiuestc 已提交
626
{
627 628
    int i;
    struct rt_module *module;
629

630
    /* find module */
631
    if ((module = rt_module_find(name)) != RT_NULL)
632 633
    {
        /* module has entry point */
634
        if (!(module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY))
635
        {
636 637
            struct rt_thread *thread;
            struct rt_list_node *tlist;
638
            rt_uint8_t *ptr;
639 640

            /* list main thread in module */
641
            if (module->module_thread != RT_NULL)
B
bernard 已提交
642
            {
643 644 645 646 647 648 649 650 651
                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   ");

652
                ptr = (rt_uint8_t *)thread->stack_addr;
653 654 655
                while (*ptr == '#')ptr ++;

                rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
656 657 658 659 660
                           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);
B
bernard 已提交
661
            }
662 663 664

            /* list sub thread in module */
            tlist = &module->module_object[RT_Object_Class_Thread].object_list;
665
            if (!rt_list_isempty(tlist)) _list_thread(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
666
#ifdef RT_USING_SEMAPHORE
667 668
            /* list semaphored in module */
            tlist = &module->module_object[RT_Object_Class_Semaphore].object_list;
669
            if (!rt_list_isempty(tlist)) _list_sem(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
670 671
#endif
#ifdef RT_USING_MUTEX
672 673
            /* list mutex in module */
            tlist = &module->module_object[RT_Object_Class_Mutex].object_list;
674
            if (!rt_list_isempty(tlist)) _list_mutex(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
675 676
#endif
#ifdef RT_USING_EVENT
677 678
            /* list event in module */
            tlist = &module->module_object[RT_Object_Class_Event].object_list;
679
            if (!rt_list_isempty(tlist)) _list_event(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
680 681
#endif
#ifdef RT_USING_MAILBOX
682 683
            /* list mailbox in module */
            tlist = &module->module_object[RT_Object_Class_MailBox].object_list;
684
            if (!rt_list_isempty(tlist)) _list_mailbox(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
685 686
#endif
#ifdef RT_USING_MESSAGEQUEUE
687 688
            /* list message queue in module */
            tlist = &module->module_object[RT_Object_Class_MessageQueue].object_list;
689 690 691 692 693 694
            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 已提交
695 696
#endif
#ifdef RT_USING_MEMPOOL
697 698
            /* list memory pool in module */
            tlist = &module->module_object[RT_Object_Class_MemPool].object_list;
699
            if (!rt_list_isempty(tlist)) _list_mempool(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
700 701
#endif
#ifdef RT_USING_DEVICE
702 703
            /* list device in module */
            tlist = &module->module_object[RT_Object_Class_Device].object_list;
704
            if (!rt_list_isempty(tlist)) _list_device(tlist);
qiuyiuestc's avatar
qiuyiuestc 已提交
705
#endif
706 707
            /* list timer in module */
            tlist = &module->module_object[RT_Object_Class_Timer].object_list;
708
            if (!rt_list_isempty(tlist)) _list_timer(tlist);
709 710
        }

711 712 713 714 715 716 717 718 719 720 721 722
        if (module->nsym > 0)
        {
            rt_kprintf("symbol    address   \n");
            rt_kprintf("-------- ----------\n");

            /* list module export symbols */
            for (i = 0; i < module->nsym; i++)
            {
                rt_kprintf("%s 0x%x\n",
                           module->symtab[i].name, module->symtab[i].addr);
            }
        }
723 724 725
    }

    return 0;
qiuyiuestc's avatar
qiuyiuestc 已提交
726
}
qiuyiuestc's avatar
qiuyiuestc 已提交
727
FINSH_FUNCTION_EXPORT(list_mod_detail, list module objects in system)
qiuyiuestc's avatar
qiuyiuestc 已提交
728 729
#endif

D
dzzxzz 已提交
730
long list(void)
731
{
B
bernard 已提交
732
#ifndef FINSH_USING_MSH_ONLY
733 734
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
B
bernard 已提交
735
#endif
736

737 738
    rt_kprintf("--Function List:\n");
    {
739
        struct finsh_syscall *index;
D
dzzxzz@gmail.com 已提交
740
        for (index = _syscall_table_begin;
741 742
                index < _syscall_table_end;
                FINSH_NEXT_SYSCALL(index))
743
        {
744 745
            /* skip the internal command */
            if (strncmp((char *)index->name, "__", 2) == 0) continue;
B
Bernard Xiong 已提交
746

747
#ifdef FINSH_USING_DESCRIPTION
748
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
749
#else
750
            rt_kprintf("%s\n", index->name);
751
#endif
752 753 754
        }
    }

B
bernard 已提交
755
#ifndef FINSH_USING_MSH_ONLY
756 757 758 759 760 761 762 763 764 765
    /* 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");
    {
766
        struct finsh_sysvar *index;
767
        for (index = _sysvar_table_begin;
768 769
                index < _sysvar_table_end;
                FINSH_NEXT_SYSVAR(index))
770
        {
771
#ifdef FINSH_USING_DESCRIPTION
772
            rt_kprintf("%-16s -- %s\n", index->name, index->desc);
773
#else
774
            rt_kprintf("%s\n", index->name);
775
#endif
776 777
        }
    }
778

779 780 781 782 783 784
    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 已提交
785
#endif
786

787
    return 0;
788 789 790
}
FINSH_FUNCTION_EXPORT(list, list all symbol in system)

B
bernard 已提交
791
#ifndef FINSH_USING_MSH_ONLY
792
static int str_is_prefix(const char *prefix, const char *str)
793
{
794 795 796 797 798 799
    while ((*prefix) && (*prefix == *str))
    {
        prefix ++;
        str ++;
    }

800
    if (*prefix == 0)
D
dzzxzz@gmail.com 已提交
801 802
        return 0;

803 804 805
    return -1;
}

806
static int str_common(const char *str1, const char *str2)
807
{
808
    const char *str = str1;
809

810
    while ((*str != 0) && (*str2 != 0) && (*str == *str2))
811 812 813 814 815 816
    {
        str ++;
        str2 ++;
    }

    return (str - str1);
817 818
}

819
void list_prefix(char *prefix)
820
{
821 822
    struct finsh_syscall_item *syscall_item;
    struct finsh_sysvar_item *sysvar_item;
823 824
    rt_uint16_t func_cnt, var_cnt;
    int length, min_length;
825
    const char *name_ptr;
826 827 828

    func_cnt = 0;
    var_cnt  = 0;
D
dzzxzz@gmail.com 已提交
829
    min_length = 0;
830 831 832 833
    name_ptr = RT_NULL;

    /* checks in system function call */
    {
D
dzzxzz@gmail.com 已提交
834 835
        struct finsh_syscall *index;
        for (index = _syscall_table_begin;
836 837
                index < _syscall_table_end;
                FINSH_NEXT_SYSCALL(index))
838
        {
839 840 841
            /* skip internal command */
            if (str_is_prefix("__", index->name) == 0) continue;

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
            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;
                }
866 867

#ifdef FINSH_USING_DESCRIPTION
868
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
869
#else
870
                rt_kprintf("%s\n", index->name);
871
#endif
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
            }
        }
    }

    /* 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 */
    {
911
        struct finsh_sysvar *index;
912
        for (index = _sysvar_table_begin;
913 914
                index < _sysvar_table_end;
                FINSH_NEXT_SYSVAR(index))
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
        {
            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 已提交
941

942
#ifdef FINSH_USING_DESCRIPTION
943
                rt_kprintf("%-16s -- %s\n", index->name, index->desc);
944
#else
945
                rt_kprintf("%s\n", index->name);
946
#endif
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
            }
        }
    }

    /* 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);
    }
989
}
B
bernard 已提交
990
#endif
991

B
bernard 已提交
992
#if defined(FINSH_USING_SYMTAB) && !defined(FINSH_USING_MSH_ONLY)
993 994 995
static int dummy = 0;
FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
#endif