cmd.c 26.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2021, 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
S
shaojinchun 已提交
28
 * 2018-11-22     Jesven       list_thread add smp support
29
 * 2018-12-27     Jesven       Fix the problem that disable interrupt too long in list_thread
30
 *                             Provide protection for the "first layer of objects" when list_*
31
 * 2020-04-07     chenhui      add clear
32 33
 */

S
shaojinchun 已提交
34
#include <rthw.h>
35
#include <rtthread.h>
马志远 已提交
36
#include <string.h>
37 38

#ifdef RT_USING_FINSH
马志远 已提交
39
#include <finsh.h>
40

41 42
#define LIST_FIND_OBJ_NR 8

43
static long clear(void)
J
jch12138 已提交
44 45 46 47 48
{
    rt_kprintf("\x1b[2J\x1b[H");

    return 0;
}
马志远 已提交
49
MSH_CMD_EXPORT(clear, clear the terminal screen);
J
jch12138 已提交
50

51
extern void rt_show_version(void);
52
long version(void)
53
{
54
    rt_show_version();
55

56
    return 0;
57
}
G
guozhanxin 已提交
58
MSH_CMD_EXPORT(version, show RT-Thread version information);
59

60
rt_inline void object_split(int len)
61
{
62
    while (len--) rt_kprintf("-");
63 64
}

65
typedef struct
66
{
67 68 69 70 71 72 73 74 75 76 77 78
    rt_list_t *list;
    rt_list_t **array;
    rt_uint8_t type;
    int nr;             /* input: max nr, can't be 0 */
    int nr_out;         /* out: got nr */
} list_get_next_t;

static void list_find_init(list_get_next_t *p, rt_uint8_t type, rt_list_t **array, int nr)
{
    struct rt_object_information *info;
    rt_list_t *list;

79
    info = rt_object_get_information((enum rt_object_class_type)type);
80 81 82 83 84 85 86
    list = &info->object_list;

    p->list = list;
    p->type = type;
    p->array = array;
    p->nr = nr;
    p->nr_out = 0;
87 88
}

89
static rt_list_t *list_get_next(rt_list_t *current, list_get_next_t *arg)
90
{
91 92
    int first_flag = 0;
    rt_ubase_t level;
93 94 95
    rt_list_t *node, *list;
    rt_list_t **array;
    int nr;
96

97 98 99 100 101 102 103 104
    arg->nr_out = 0;

    if (!arg->nr || !arg->type)
    {
        return (rt_list_t *)RT_NULL;
    }

    list = arg->list;
105 106 107 108 109 110 111 112 113 114 115 116 117

    if (!current) /* find first */
    {
        node = list;
        first_flag = 1;
    }
    else
    {
        node = current;
    }

    level = rt_hw_interrupt_disable();

118
    if (!first_flag)
119
    {
120
        struct rt_object *obj;
121
        /* The node in the list? */
122 123
        obj = rt_list_entry(node, struct rt_object, list);
        if ((obj->type & ~RT_Object_Class_Static) != arg->type)
124 125 126 127 128 129
        {
            rt_hw_interrupt_enable(level);
            return (rt_list_t *)RT_NULL;
        }
    }

130 131 132
    nr = 0;
    array = arg->array;
    while (1)
133
    {
134 135 136 137 138 139 140 141 142 143 144 145 146
        node = node->next;

        if (node == list)
        {
            node = (rt_list_t *)RT_NULL;
            break;
        }
        nr++;
        *array++ = node;
        if (nr == arg->nr)
        {
            break;
        }
147
    }
148

149 150 151 152
    rt_hw_interrupt_enable(level);
    arg->nr_out = nr;
    return node;
}
153

154 155 156 157 158
long list_thread(void)
{
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
159
    rt_list_t *next = (rt_list_t *)RT_NULL;
160 161
    const char *item_title = "thread";
    int maxlen;
162

马志远 已提交
163
    list_find_init(&find_arg, RT_Object_Class_Thread, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
164

165
    maxlen = RT_NAME_MAX;
166

S
shaojinchun 已提交
167
#ifdef RT_USING_SMP
马志远 已提交
168 169 170
    rt_kprintf("%-*.s cpu bind pri  status      sp     stack size max used left tick  error\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf(" --- ---- ---  ------- ---------- ----------  ------  ---------- ---\n");
S
shaojinchun 已提交
171
#else
马志远 已提交
172 173 174
    rt_kprintf("%-*.s pri  status      sp     stack size max used left tick  error\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf(" ---  ------- ---------- ----------  ------  ---------- ---\n");
S
shaojinchun 已提交
175
#endif /*RT_USING_SMP*/
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

    do
    {
        next = list_get_next(next, &find_arg);
        {
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_thread thread_info, *thread;

                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();

                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }
                /* copy info */
马志远 已提交
196
                rt_memcpy(&thread_info, obj, sizeof thread_info);
197 198
                rt_hw_interrupt_enable(level);

马志远 已提交
199
                thread = (struct rt_thread *)obj;
200 201 202 203
                {
                    rt_uint8_t stat;
                    rt_uint8_t *ptr;

S
shaojinchun 已提交
204
#ifdef RT_USING_SMP
205
                    if (thread->oncpu != RT_CPU_DETACHED)
206
                        rt_kprintf("%-*.*s %3d %3d %4d ", maxlen, RT_NAME_MAX, thread->name, thread->oncpu, thread->bind_cpu, thread->current_priority);
207
                    else
208
                        rt_kprintf("%-*.*s N/A %3d %4d ", maxlen, RT_NAME_MAX, thread->name, thread->bind_cpu, thread->current_priority);
209

S
shaojinchun 已提交
210
#else
211
                    rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->name, thread->current_priority);
S
shaojinchun 已提交
212
#endif /*RT_USING_SMP*/
213 214 215 216 217
                    stat = (thread->stat & RT_THREAD_STAT_MASK);
                    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  ");
218
                    else if (stat == RT_THREAD_RUNNING) rt_kprintf(" running");
219

220
#if defined(ARCH_CPU_STACK_GROWS_UPWARD)
221 222 223 224
                    ptr = (rt_uint8_t *)thread->stack_addr + thread->stack_size - 1;
                    while (*ptr == '#')ptr --;

                    rt_kprintf(" 0x%08x 0x%08x    %02d%%   0x%08x %03d\n",
马志远 已提交
225 226 227 228 229
                               ((rt_ubase_t)thread->sp - (rt_ubase_t)thread->stack_addr),
                               thread->stack_size,
                               ((rt_ubase_t)ptr - (rt_ubase_t)thread->stack_addr) * 100 / thread->stack_size,
                               thread->remaining_tick,
                               thread->error);
230
#else
231 232 233 234
                    ptr = (rt_uint8_t *)thread->stack_addr;
                    while (*ptr == '#')ptr ++;

                    rt_kprintf(" 0x%08x 0x%08x    %02d%%   0x%08x %03d\n",
马志远 已提交
235 236 237 238 239 240
                               thread->stack_size + ((rt_ubase_t)thread->stack_addr - (rt_ubase_t)thread->sp),
                               thread->stack_size,
                               (thread->stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread->stack_addr)) * 100
                               / thread->stack_size,
                               thread->remaining_tick,
                               thread->error);
241
#endif
242 243 244
                }
            }
        }
245
    }
马志远 已提交
246
    while (next != (rt_list_t *)RT_NULL);
247 248

    return 0;
qiuyiuestc's avatar
qiuyiuestc 已提交
249
}
B
Bernard Xiong 已提交
250
MSH_CMD_EXPORT(list_thread, list thread);
251

252
static void show_wait_queue(struct rt_list_node *list)
253
{
254 255 256 257 258 259
    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);
马志远 已提交
260
        rt_kprintf("%.*s", RT_NAME_MAX, thread->name);
D
dzzxzz@gmail.com 已提交
261

262
        if (node->next != list)
D
dzzxzz@gmail.com 已提交
263
            rt_kprintf("/");
264
    }
265 266 267
}

#ifdef RT_USING_SEMAPHORE
268
long list_sem(void)
269
{
270 271 272
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
273
    rt_list_t *next = (rt_list_t *)RT_NULL;
274

275
    int maxlen;
276
    const char *item_title = "semaphore";
277

马志远 已提交
278
    list_find_init(&find_arg, RT_Object_Class_Semaphore, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
279 280

    maxlen = RT_NAME_MAX;
281

马志远 已提交
282 283 284
    rt_kprintf("%-*.s v   suspend thread\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf(" --- --------------\n");
285 286

    do
287
    {
288
        next = list_get_next(next, &find_arg);
289
        {
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_semaphore *sem;

                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();
                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }
                rt_hw_interrupt_enable(level);

马志远 已提交
305
                sem = (struct rt_semaphore *)obj;
306 307 308
                if (!rt_list_isempty(&sem->parent.suspend_thread))
                {
                    rt_kprintf("%-*.*s %03d %d:",
马志远 已提交
309 310 311 312
                               maxlen, RT_NAME_MAX,
                               sem->parent.parent.name,
                               sem->value,
                               rt_list_len(&sem->parent.suspend_thread));
313 314 315 316 317 318
                    show_wait_queue(&(sem->parent.suspend_thread));
                    rt_kprintf("\n");
                }
                else
                {
                    rt_kprintf("%-*.*s %03d %d\n",
马志远 已提交
319 320 321 322
                               maxlen, RT_NAME_MAX,
                               sem->parent.parent.name,
                               sem->value,
                               rt_list_len(&sem->parent.suspend_thread));
323 324
                }
            }
325 326
        }
    }
马志远 已提交
327
    while (next != (rt_list_t *)RT_NULL);
328 329

    return 0;
330
}
B
Bernard Xiong 已提交
331
MSH_CMD_EXPORT(list_sem, list semaphore in system);
332 333 334
#endif

#ifdef RT_USING_EVENT
335
long list_event(void)
336
{
337 338 339
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
340
    rt_list_t *next = (rt_list_t *)RT_NULL;
341

342
    int maxlen;
343
    const char *item_title = "event";
344

马志远 已提交
345
    list_find_init(&find_arg, RT_Object_Class_Event, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
346 347

    maxlen = RT_NAME_MAX;
348

马志远 已提交
349 350 351
    rt_kprintf("%-*.s      set    suspend thread\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf("  ---------- --------------\n");
352 353

    do
354
    {
355
        next = list_get_next(next, &find_arg);
356
        {
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_event *e;

                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();
                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }

                rt_hw_interrupt_enable(level);

                e = (struct rt_event *)obj;
                if (!rt_list_isempty(&e->parent.suspend_thread))
                {
                    rt_kprintf("%-*.*s  0x%08x %03d:",
马志远 已提交
377 378 379 380
                               maxlen, RT_NAME_MAX,
                               e->parent.parent.name,
                               e->set,
                               rt_list_len(&e->parent.suspend_thread));
381 382 383 384 385 386
                    show_wait_queue(&(e->parent.suspend_thread));
                    rt_kprintf("\n");
                }
                else
                {
                    rt_kprintf("%-*.*s  0x%08x 0\n",
马志远 已提交
387
                               maxlen, RT_NAME_MAX, e->parent.parent.name, e->set);
388 389
                }
            }
390 391
        }
    }
马志远 已提交
392
    while (next != (rt_list_t *)RT_NULL);
393 394

    return 0;
395
}
B
Bernard Xiong 已提交
396
MSH_CMD_EXPORT(list_event, list event in system);
397 398 399
#endif

#ifdef RT_USING_MUTEX
400
long list_mutex(void)
401
{
402 403 404
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
405
    rt_list_t *next = (rt_list_t *)RT_NULL;
406

407
    int maxlen;
408
    const char *item_title = "mutex";
409

马志远 已提交
410
    list_find_init(&find_arg, RT_Object_Class_Mutex, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
411 412

    maxlen = RT_NAME_MAX;
413

马志远 已提交
414 415 416
    rt_kprintf("%-*.s   owner  hold suspend thread\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf(" -------- ---- --------------\n");
417 418

    do
419
    {
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
        next = list_get_next(next, &find_arg);
        {
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_mutex *m;

                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();
                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }

                rt_hw_interrupt_enable(level);

                m = (struct rt_mutex *)obj;
                rt_kprintf("%-*.*s %-8.*s %04d %d\n",
马志远 已提交
440 441 442 443 444 445
                           maxlen, RT_NAME_MAX,
                           m->parent.parent.name,
                           RT_NAME_MAX,
                           m->owner->name,
                           m->hold,
                           rt_list_len(&m->parent.suspend_thread));
446 447 448

            }
        }
449
    }
马志远 已提交
450
    while (next != (rt_list_t *)RT_NULL);
451 452

    return 0;
453
}
B
Bernard Xiong 已提交
454
MSH_CMD_EXPORT(list_mutex, list mutex in system);
455 456 457
#endif

#ifdef RT_USING_MAILBOX
458
long list_mailbox(void)
459
{
460 461 462
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
463
    rt_list_t *next = (rt_list_t *)RT_NULL;
464

465
    int maxlen;
G
gbcwbz 已提交
466
    const char *item_title = "mailbox";
467

马志远 已提交
468
    list_find_init(&find_arg, RT_Object_Class_MailBox, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
469 470

    maxlen = RT_NAME_MAX;
471

马志远 已提交
472 473 474
    rt_kprintf("%-*.s entry size suspend thread\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf(" ----  ---- --------------\n");
475 476

    do
477
    {
478
        next = list_get_next(next, &find_arg);
479
        {
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_mailbox *m;

                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();
                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }

                rt_hw_interrupt_enable(level);

                m = (struct rt_mailbox *)obj;
                if (!rt_list_isempty(&m->parent.suspend_thread))
                {
                    rt_kprintf("%-*.*s %04d  %04d %d:",
马志远 已提交
500 501 502 503 504
                               maxlen, RT_NAME_MAX,
                               m->parent.parent.name,
                               m->entry,
                               m->size,
                               rt_list_len(&m->parent.suspend_thread));
505 506 507 508 509 510
                    show_wait_queue(&(m->parent.suspend_thread));
                    rt_kprintf("\n");
                }
                else
                {
                    rt_kprintf("%-*.*s %04d  %04d %d\n",
马志远 已提交
511 512 513 514 515
                               maxlen, RT_NAME_MAX,
                               m->parent.parent.name,
                               m->entry,
                               m->size,
                               rt_list_len(&m->parent.suspend_thread));
516 517 518
                }

            }
519 520
        }
    }
马志远 已提交
521
    while (next != (rt_list_t *)RT_NULL);
522 523

    return 0;
524
}
B
Bernard Xiong 已提交
525
MSH_CMD_EXPORT(list_mailbox, list mail box in system);
526 527 528
#endif

#ifdef RT_USING_MESSAGEQUEUE
529
long list_msgqueue(void)
530
{
531 532 533
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
534
    rt_list_t *next = (rt_list_t *)RT_NULL;
535

536
    int maxlen;
G
gbcwbz 已提交
537
    const char *item_title = "msgqueue";
538

马志远 已提交
539
    list_find_init(&find_arg, RT_Object_Class_MessageQueue, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
540 541

    maxlen = RT_NAME_MAX;
542

马志远 已提交
543 544 545
    rt_kprintf("%-*.s entry suspend thread\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf(" ----  --------------\n");
546
    do
547
    {
548
        next = list_get_next(next, &find_arg);
549
        {
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_messagequeue *m;

                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();
                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }

                rt_hw_interrupt_enable(level);

                m = (struct rt_messagequeue *)obj;
                if (!rt_list_isempty(&m->parent.suspend_thread))
                {
                    rt_kprintf("%-*.*s %04d  %d:",
马志远 已提交
570 571 572 573
                               maxlen, RT_NAME_MAX,
                               m->parent.parent.name,
                               m->entry,
                               rt_list_len(&m->parent.suspend_thread));
574 575 576 577 578 579
                    show_wait_queue(&(m->parent.suspend_thread));
                    rt_kprintf("\n");
                }
                else
                {
                    rt_kprintf("%-*.*s %04d  %d\n",
马志远 已提交
580 581 582 583
                               maxlen, RT_NAME_MAX,
                               m->parent.parent.name,
                               m->entry,
                               rt_list_len(&m->parent.suspend_thread));
584 585
                }
            }
586 587
        }
    }
马志远 已提交
588
    while (next != (rt_list_t *)RT_NULL);
589 590

    return 0;
591
}
B
Bernard Xiong 已提交
592
MSH_CMD_EXPORT(list_msgqueue, list message queue in system);
593 594
#endif

595
#ifdef RT_USING_MEMHEAP
596
long list_memheap(void)
597
{
598 599 600
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
601
    rt_list_t *next = (rt_list_t *)RT_NULL;
602

603
    int maxlen;
604
    const char *item_title = "memheap";
605

马志远 已提交
606
    list_find_init(&find_arg, RT_Object_Class_MemHeap, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
607 608

    maxlen = RT_NAME_MAX;
609

马志远 已提交
610 611 612
    rt_kprintf("%-*.s  pool size  max used size available size\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf(" ---------- ------------- --------------\n");
613
    do
614
    {
615 616 617 618 619 620 621
        next = list_get_next(next, &find_arg);
        {
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_memheap *mh;
622

623 624 625 626 627 628 629
                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();
                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }
630

631
                rt_hw_interrupt_enable(level);
632

633 634 635
                mh = (struct rt_memheap *)obj;

                rt_kprintf("%-*.*s %-010d %-013d %-05d\n",
马志远 已提交
636 637 638 639 640
                           maxlen, RT_NAME_MAX,
                           mh->parent.name,
                           mh->pool_size,
                           mh->max_used_size,
                           mh->available_size);
641 642 643 644

            }
        }
    }
马志远 已提交
645
    while (next != (rt_list_t *)RT_NULL);
646 647

    return 0;
648
}
B
Bernard Xiong 已提交
649
MSH_CMD_EXPORT(list_memheap, list memory heap in system);
650 651
#endif

652
#ifdef RT_USING_MEMPOOL
653
long list_mempool(void)
654
{
655 656 657
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
658
    rt_list_t *next = (rt_list_t *)RT_NULL;
659

660
    int maxlen;
661
    const char *item_title = "mempool";
662

马志远 已提交
663
    list_find_init(&find_arg, RT_Object_Class_MemPool, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
664 665

    maxlen = RT_NAME_MAX;
666

马志远 已提交
667 668 669
    rt_kprintf("%-*.s block total free suspend thread\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf(" ----  ----  ---- --------------\n");
670
    do
671
    {
672
        next = list_get_next(next, &find_arg);
673
        {
674 675 676 677 678
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_mempool *mp;
679 680
                int suspend_thread_count;
                rt_list_t *node;
681 682 683 684 685 686 687 688 689 690 691 692

                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();
                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }

                rt_hw_interrupt_enable(level);

                mp = (struct rt_mempool *)obj;
693 694

                suspend_thread_count = 0;
695
                rt_list_for_each(node, &mp->suspend_thread)
696 697 698 699 700
                {
                    suspend_thread_count++;
                }

                if (suspend_thread_count > 0)
701 702
                {
                    rt_kprintf("%-*.*s %04d  %04d  %04d %d:",
马志远 已提交
703 704 705 706 707 708
                               maxlen, RT_NAME_MAX,
                               mp->parent.name,
                               mp->block_size,
                               mp->block_total_count,
                               mp->block_free_count,
                               suspend_thread_count);
709 710 711 712 713 714
                    show_wait_queue(&(mp->suspend_thread));
                    rt_kprintf("\n");
                }
                else
                {
                    rt_kprintf("%-*.*s %04d  %04d  %04d %d\n",
马志远 已提交
715 716 717 718 719 720
                               maxlen, RT_NAME_MAX,
                               mp->parent.name,
                               mp->block_size,
                               mp->block_total_count,
                               mp->block_free_count,
                               suspend_thread_count);
721 722
                }
            }
723 724
        }
    }
马志远 已提交
725
    while (next != (rt_list_t *)RT_NULL);
726 727

    return 0;
728
}
B
Bernard Xiong 已提交
729
MSH_CMD_EXPORT(list_mempool, list memory pool in system);
730 731
#endif

732
long list_timer(void)
733
{
734 735 736
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
737
    rt_list_t *next = (rt_list_t *)RT_NULL;
738

739
    int maxlen;
740
    const char *item_title = "timer";
741

马志远 已提交
742
    list_find_init(&find_arg, RT_Object_Class_Timer, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
743 744

    maxlen = RT_NAME_MAX;
745

armink_ztl's avatar
armink_ztl 已提交
746
    rt_kprintf("%-*.s  periodic   timeout    activated     mode\n", maxlen, item_title);
马志远 已提交
747
    object_split(maxlen);
armink_ztl's avatar
armink_ztl 已提交
748
    rt_kprintf(" ---------- ---------- ----------- ---------\n");
马志远 已提交
749
    do
armink_ztl's avatar
armink_ztl 已提交
750
    {
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
        next = list_get_next(next, &find_arg);
        {
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_timer *timer;

                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();
                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }

                rt_hw_interrupt_enable(level);

                timer = (struct rt_timer *)obj;
                rt_kprintf("%-*.*s 0x%08x 0x%08x ",
马志远 已提交
771 772 773 774
                           maxlen, RT_NAME_MAX,
                           timer->parent.name,
                           timer->init_tick,
                           timer->timeout_tick);
775
                if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
armink_ztl's avatar
armink_ztl 已提交
776 777 778 779 780
                    rt_kprintf("activated   ");
                else
                    rt_kprintf("deactivated ");
                if (timer->parent.flag & RT_TIMER_FLAG_PERIODIC)
                    rt_kprintf("periodic\n");
781
                else
armink_ztl's avatar
armink_ztl 已提交
782
                    rt_kprintf("one shot\n");
783 784 785

            }
        }
786
    }
马志远 已提交
787
    while (next != (rt_list_t *)RT_NULL);
788 789 790 791

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

    return 0;
792
}
B
Bernard Xiong 已提交
793
MSH_CMD_EXPORT(list_timer, list timer in system);
794 795

#ifdef RT_USING_DEVICE
796 797 798 799 800 801 802 803 804 805 806 807 808
static char *const device_type_str[] =
{
    "Character Device",
    "Block Device",
    "Network Interface",
    "MTD Device",
    "CAN Device",
    "RTC",
    "Sound Device",
    "Graphic Device",
    "I2C Bus",
    "USB Slave Device",
    "USB Host Bus",
809
    "USB OTG Bus",
810 811 812 813 814 815 816 817
    "SPI Bus",
    "SPI Device",
    "SDIO Bus",
    "PM Pseudo Device",
    "Pipe",
    "Portal Device",
    "Timer Device",
    "Miscellaneous Device",
G
guozhanxin 已提交
818
    "Sensor Device",
T
tyustli 已提交
819
    "Touch Device",
820
    "Phy Device",
821
    "Security Device",
G
guo 已提交
822 823 824 825 826 827
    "WLAN Device",
    "Pin Device",
    "ADC Device",
    "DAC Device",
    "WDT Device",
    "PWM Device",
828 829 830 831
    "Unknown"
};

long list_device(void)
832
{
833 834 835
    rt_ubase_t level;
    list_get_next_t find_arg;
    rt_list_t *obj_list[LIST_FIND_OBJ_NR];
马志远 已提交
836
    rt_list_t *next = (rt_list_t *)RT_NULL;
837

838
    int maxlen;
G
gbcwbz 已提交
839 840
    const char *item_title = "device";

马志远 已提交
841
    list_find_init(&find_arg, RT_Object_Class_Device, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
842 843

    maxlen = RT_NAME_MAX;
844

马志远 已提交
845 846 847
    rt_kprintf("%-*.s         type         ref count\n", maxlen, item_title);
    object_split(maxlen);
    rt_kprintf(" -------------------- ----------\n");
848
    do
849
    {
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
        next = list_get_next(next, &find_arg);
        {
            int i;
            for (i = 0; i < find_arg.nr_out; i++)
            {
                struct rt_object *obj;
                struct rt_device *device;

                obj = rt_list_entry(obj_list[i], struct rt_object, list);
                level = rt_hw_interrupt_disable();
                if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
                {
                    rt_hw_interrupt_enable(level);
                    continue;
                }

                rt_hw_interrupt_enable(level);

                device = (struct rt_device *)obj;
                rt_kprintf("%-*.*s %-20s %-8d\n",
马志远 已提交
870 871 872 873 874 875
                           maxlen, RT_NAME_MAX,
                           device->parent.name,
                           (device->type <= RT_Device_Class_Unknown) ?
                           device_type_str[device->type] :
                           device_type_str[RT_Device_Class_Unknown],
                           device->ref_count);
876 877 878

            }
        }
879
    }
马志远 已提交
880
    while (next != (rt_list_t *)RT_NULL);
881 882

    return 0;
883
}
B
Bernard Xiong 已提交
884
MSH_CMD_EXPORT(list_device, list device in system);
885 886
#endif

887
#endif /* RT_USING_FINSH */