cmd.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-04-30 Bernard first implementation
  9. * 2006-05-04 Bernard add list_thread,
  10. * list_sem,
  11. * list_timer
  12. * 2006-05-20 Bernard add list_mutex,
  13. * list_mailbox,
  14. * list_msgqueue,
  15. * list_event,
  16. * list_fevent,
  17. * list_mempool
  18. * 2006-06-03 Bernard display stack information in list_thread
  19. * 2006-08-10 Bernard change version to invoke rt_show_version
  20. * 2008-09-10 Bernard update the list function for finsh syscall
  21. * list and sysvar list
  22. * 2009-05-30 Bernard add list_device
  23. * 2010-04-21 yi.qiu add list_module
  24. * 2012-04-29 goprife improve the command line auto-complete feature.
  25. * 2012-06-02 lgnq add list_memheap
  26. * 2012-10-22 Bernard add MS VC++ patch.
  27. * 2016-06-02 armink beautify the list_thread command
  28. * 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. * 2022-07-02 Stanley Lwin add list command
  33. * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  34. * 2024-02-09 Bernard fix the version command
  35. */
  36. #include <rthw.h>
  37. #include <rtthread.h>
  38. #include <string.h>
  39. #ifdef RT_USING_FINSH
  40. #include <finsh.h>
  41. #define LIST_DFS_OPT_ID 0x100
  42. #define LIST_FIND_OBJ_NR 8
  43. static long clear(void)
  44. {
  45. rt_kprintf("\x1b[2J\x1b[H");
  46. return 0;
  47. }
  48. MSH_CMD_EXPORT(clear, clear the terminal screen);
  49. static long version(void)
  50. {
  51. rt_show_version();
  52. return 0;
  53. }
  54. MSH_CMD_EXPORT(version, show RT-Thread version information);
  55. rt_inline void object_split(int len)
  56. {
  57. while (len--) rt_kprintf("-");
  58. }
  59. typedef struct
  60. {
  61. rt_list_t *list;
  62. rt_list_t **array;
  63. rt_uint8_t type;
  64. int nr; /* input: max nr, can't be 0 */
  65. int nr_out; /* out: got nr */
  66. } list_get_next_t;
  67. static void list_find_init(list_get_next_t *p, rt_uint8_t type, rt_list_t **array, int nr)
  68. {
  69. struct rt_object_information *info;
  70. rt_list_t *list;
  71. info = rt_object_get_information((enum rt_object_class_type)type);
  72. list = &info->object_list;
  73. p->list = list;
  74. p->type = type;
  75. p->array = array;
  76. p->nr = nr;
  77. p->nr_out = 0;
  78. }
  79. static rt_list_t *list_get_next(rt_list_t *current, list_get_next_t *arg)
  80. {
  81. int first_flag = 0;
  82. rt_base_t level;
  83. rt_list_t *node, *list;
  84. rt_list_t **array;
  85. struct rt_object_information *info;
  86. int nr;
  87. arg->nr_out = 0;
  88. if (!arg->nr || !arg->type)
  89. {
  90. return (rt_list_t *)RT_NULL;
  91. }
  92. list = arg->list;
  93. info = rt_list_entry(list, struct rt_object_information, object_list);
  94. if (!current) /* find first */
  95. {
  96. node = list;
  97. first_flag = 1;
  98. }
  99. else
  100. {
  101. node = current;
  102. }
  103. level = rt_spin_lock_irqsave(&info->spinlock);
  104. if (!first_flag)
  105. {
  106. struct rt_object *obj;
  107. /* The node in the list? */
  108. obj = rt_list_entry(node, struct rt_object, list);
  109. if ((obj->type & ~RT_Object_Class_Static) != arg->type)
  110. {
  111. rt_spin_unlock_irqrestore(&info->spinlock, level);
  112. return (rt_list_t *)RT_NULL;
  113. }
  114. }
  115. nr = 0;
  116. array = arg->array;
  117. while (1)
  118. {
  119. node = node->next;
  120. if (node == list)
  121. {
  122. node = (rt_list_t *)RT_NULL;
  123. break;
  124. }
  125. nr++;
  126. *array++ = node;
  127. if (nr == arg->nr)
  128. {
  129. break;
  130. }
  131. }
  132. rt_spin_unlock_irqrestore(&info->spinlock, level);
  133. arg->nr_out = nr;
  134. return node;
  135. }
  136. long list_thread(void)
  137. {
  138. rt_base_t level;
  139. list_get_next_t find_arg;
  140. struct rt_object_information *info;
  141. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  142. rt_list_t *next = (rt_list_t *)RT_NULL;
  143. const char *item_title = "thread";
  144. const size_t tcb_strlen = sizeof(void *) * 2 + 2;
  145. const size_t usage_strlen = sizeof(void *) + 1;
  146. int maxlen;
  147. list_find_init(&find_arg, RT_Object_Class_Thread, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  148. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  149. maxlen = RT_NAME_MAX;
  150. #ifdef RT_USING_SMP
  151. rt_kprintf("%-*.*s cpu bind pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
  152. object_split(maxlen);
  153. rt_kprintf(" --- ---- --- ------- ---------- ---------- ------ ---------- -------");
  154. rt_kprintf(" ");
  155. object_split(tcb_strlen);
  156. rt_kprintf(" ");
  157. object_split(usage_strlen);
  158. rt_kprintf("\n");
  159. #else
  160. rt_kprintf("%-*.*s pri status sp stack size max used left tick error tcb addr usage\n", maxlen, maxlen, item_title);
  161. object_split(maxlen);
  162. rt_kprintf(" --- ------- ---------- ---------- ------ ---------- -------");
  163. rt_kprintf(" ");
  164. object_split(tcb_strlen);
  165. rt_kprintf(" ");
  166. object_split(usage_strlen);
  167. rt_kprintf("\n");
  168. #endif /*RT_USING_SMP*/
  169. do
  170. {
  171. next = list_get_next(next, &find_arg);
  172. {
  173. int i;
  174. for (i = 0; i < find_arg.nr_out; i++)
  175. {
  176. struct rt_object *obj;
  177. struct rt_thread thread_info, *thread;
  178. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  179. level = rt_spin_lock_irqsave(&info->spinlock);
  180. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  181. {
  182. rt_spin_unlock_irqrestore(&info->spinlock, level);
  183. continue;
  184. }
  185. /* copy info */
  186. rt_memcpy(&thread_info, obj, sizeof thread_info);
  187. rt_spin_unlock_irqrestore(&info->spinlock, level);
  188. thread = (struct rt_thread *)obj;
  189. {
  190. rt_uint8_t stat;
  191. rt_uint8_t *ptr;
  192. #ifdef RT_USING_SMP
  193. /* no synchronization applied since it's only for debug */
  194. if (RT_SCHED_CTX(thread).oncpu != RT_CPU_DETACHED)
  195. rt_kprintf("%-*.*s %3d %3d %4d ", maxlen, RT_NAME_MAX,
  196. thread->parent.name, RT_SCHED_CTX(thread).oncpu,
  197. RT_SCHED_CTX(thread).bind_cpu,
  198. RT_SCHED_PRIV(thread).current_priority);
  199. else
  200. rt_kprintf("%-*.*s N/A %3d %4d ", maxlen, RT_NAME_MAX,
  201. thread->parent.name,
  202. RT_SCHED_CTX(thread).bind_cpu,
  203. RT_SCHED_PRIV(thread).current_priority);
  204. #else
  205. /* no synchronization applied since it's only for debug */
  206. rt_kprintf("%-*.*s %3d ", maxlen, RT_NAME_MAX, thread->parent.name, RT_SCHED_PRIV(thread).current_priority);
  207. #endif /*RT_USING_SMP*/
  208. stat = (RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK);
  209. if (stat == RT_THREAD_READY) rt_kprintf(" ready ");
  210. else if ((stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK) rt_kprintf(" suspend");
  211. else if (stat == RT_THREAD_INIT) rt_kprintf(" init ");
  212. else if (stat == RT_THREAD_CLOSE) rt_kprintf(" close ");
  213. else if (stat == RT_THREAD_RUNNING) rt_kprintf(" running");
  214. #if defined(ARCH_CPU_STACK_GROWS_UPWARD)
  215. ptr = (rt_uint8_t *)thread->stack_addr + thread->stack_size - 1;
  216. while (*ptr == '#')ptr --;
  217. rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p",
  218. ((rt_ubase_t)thread->sp - (rt_ubase_t)thread->stack_addr),
  219. thread->stack_size,
  220. ((rt_ubase_t)ptr - (rt_ubase_t)thread->stack_addr) * 100 / thread->stack_size,
  221. thread->remaining_tick,
  222. rt_strerror(thread->error),
  223. thread);
  224. #ifdef RT_USING_CPU_USAGE_TRACER
  225. rt_kprintf(" %3d%%\n", rt_thread_get_usage(thread));
  226. #else
  227. rt_kprintf(" N/A\n");
  228. #endif
  229. #else
  230. ptr = (rt_uint8_t *)thread->stack_addr;
  231. while (*ptr == '#') ptr ++;
  232. rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %s %p",
  233. thread->stack_size + ((rt_ubase_t)thread->stack_addr - (rt_ubase_t)thread->sp),
  234. thread->stack_size,
  235. (thread->stack_size - ((rt_ubase_t) ptr - (rt_ubase_t) thread->stack_addr)) * 100
  236. / thread->stack_size,
  237. RT_SCHED_PRIV(thread).remaining_tick,
  238. rt_strerror(thread->error),
  239. thread);
  240. #ifdef RT_USING_CPU_USAGE_TRACER
  241. rt_kprintf(" %3d%%\n", rt_thread_get_usage(thread));
  242. #else
  243. rt_kprintf(" N/A\n");
  244. #endif
  245. #endif
  246. }
  247. }
  248. }
  249. }
  250. while (next != (rt_list_t *)RT_NULL);
  251. return 0;
  252. }
  253. #ifdef RT_USING_SEMAPHORE
  254. long list_sem(void)
  255. {
  256. rt_base_t level;
  257. list_get_next_t find_arg;
  258. struct rt_object_information *info;
  259. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  260. rt_list_t *next = (rt_list_t *)RT_NULL;
  261. int maxlen;
  262. const char *item_title = "semaphore";
  263. list_find_init(&find_arg, RT_Object_Class_Semaphore, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  264. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  265. maxlen = RT_NAME_MAX;
  266. rt_kprintf("%-*.*s v suspend thread\n", maxlen, maxlen, item_title);
  267. object_split(maxlen);
  268. rt_kprintf(" --- --------------\n");
  269. do
  270. {
  271. next = list_get_next(next, &find_arg);
  272. {
  273. int i;
  274. for (i = 0; i < find_arg.nr_out; i++)
  275. {
  276. struct rt_object *obj;
  277. struct rt_semaphore *sem;
  278. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  279. level = rt_spin_lock_irqsave(&info->spinlock);
  280. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  281. {
  282. rt_spin_unlock_irqrestore(&info->spinlock, level);
  283. continue;
  284. }
  285. rt_spin_unlock_irqrestore(&info->spinlock, level);
  286. sem = (struct rt_semaphore *)obj;
  287. if (!rt_list_isempty(&sem->parent.suspend_thread))
  288. {
  289. rt_kprintf("%-*.*s %03d %d:",
  290. maxlen, RT_NAME_MAX,
  291. sem->parent.parent.name,
  292. sem->value,
  293. rt_list_len(&sem->parent.suspend_thread));
  294. rt_susp_list_print(&(sem->parent.suspend_thread));
  295. rt_kprintf("\n");
  296. }
  297. else
  298. {
  299. rt_kprintf("%-*.*s %03d %d\n",
  300. maxlen, RT_NAME_MAX,
  301. sem->parent.parent.name,
  302. sem->value,
  303. rt_list_len(&sem->parent.suspend_thread));
  304. }
  305. }
  306. }
  307. }
  308. while (next != (rt_list_t *)RT_NULL);
  309. return 0;
  310. }
  311. #endif /* RT_USING_SEMAPHORE */
  312. #ifdef RT_USING_EVENT
  313. long list_event(void)
  314. {
  315. rt_base_t level;
  316. list_get_next_t find_arg;
  317. struct rt_object_information *info;
  318. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  319. rt_list_t *next = (rt_list_t *)RT_NULL;
  320. int maxlen;
  321. const char *item_title = "event";
  322. list_find_init(&find_arg, RT_Object_Class_Event, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  323. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  324. maxlen = RT_NAME_MAX;
  325. rt_kprintf("%-*.*s set suspend thread\n", maxlen, maxlen, item_title);
  326. object_split(maxlen);
  327. rt_kprintf(" ---------- --------------\n");
  328. do
  329. {
  330. next = list_get_next(next, &find_arg);
  331. {
  332. int i;
  333. for (i = 0; i < find_arg.nr_out; i++)
  334. {
  335. struct rt_object *obj;
  336. struct rt_event *e;
  337. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  338. level = rt_spin_lock_irqsave(&info->spinlock);
  339. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  340. {
  341. rt_spin_unlock_irqrestore(&info->spinlock, level);
  342. continue;
  343. }
  344. rt_spin_unlock_irqrestore(&info->spinlock, level);
  345. e = (struct rt_event *)obj;
  346. if (!rt_list_isempty(&e->parent.suspend_thread))
  347. {
  348. rt_kprintf("%-*.*s 0x%08x %03d:",
  349. maxlen, RT_NAME_MAX,
  350. e->parent.parent.name,
  351. e->set,
  352. rt_list_len(&e->parent.suspend_thread));
  353. rt_susp_list_print(&(e->parent.suspend_thread));
  354. rt_kprintf("\n");
  355. }
  356. else
  357. {
  358. rt_kprintf("%-*.*s 0x%08x 0\n",
  359. maxlen, RT_NAME_MAX, e->parent.parent.name, e->set);
  360. }
  361. }
  362. }
  363. }
  364. while (next != (rt_list_t *)RT_NULL);
  365. return 0;
  366. }
  367. #endif /* RT_USING_EVENT */
  368. #ifdef RT_USING_MUTEX
  369. long list_mutex(void)
  370. {
  371. rt_base_t level;
  372. list_get_next_t find_arg;
  373. struct rt_object_information *info;
  374. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  375. rt_list_t *next = (rt_list_t *)RT_NULL;
  376. int maxlen;
  377. const char *item_title = "mutex";
  378. list_find_init(&find_arg, RT_Object_Class_Mutex, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  379. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  380. maxlen = RT_NAME_MAX;
  381. rt_kprintf("%-*.*s owner hold priority suspend thread \n", maxlen, maxlen, item_title);
  382. object_split(maxlen);
  383. rt_kprintf(" -------- ---- -------- --------------\n");
  384. do
  385. {
  386. next = list_get_next(next, &find_arg);
  387. {
  388. int i;
  389. for (i = 0; i < find_arg.nr_out; i++)
  390. {
  391. struct rt_object *obj;
  392. struct rt_mutex *m;
  393. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  394. level = rt_spin_lock_irqsave(&info->spinlock);
  395. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  396. {
  397. rt_spin_unlock_irqrestore(&info->spinlock, level);
  398. continue;
  399. }
  400. rt_spin_unlock_irqrestore(&info->spinlock, level);
  401. m = (struct rt_mutex *)obj;
  402. if (!rt_list_isempty(&m->parent.suspend_thread))
  403. {
  404. rt_kprintf("%-*.*s %-8.*s %04d %8d %04d ",
  405. maxlen, RT_NAME_MAX,
  406. m->parent.parent.name,
  407. RT_NAME_MAX,
  408. (m->owner == RT_NULL) ? "(null)" : m->owner->parent.name,
  409. m->hold,
  410. m->priority,
  411. rt_list_len(&m->parent.suspend_thread));
  412. rt_susp_list_print(&(m->parent.suspend_thread));
  413. rt_kprintf("\n");
  414. }
  415. else
  416. {
  417. rt_kprintf("%-*.*s %-8.*s %04d %8d %04d\n",
  418. maxlen, RT_NAME_MAX,
  419. m->parent.parent.name,
  420. RT_NAME_MAX,
  421. (m->owner == RT_NULL) ? "(null)" : m->owner->parent.name,
  422. m->hold,
  423. m->priority,
  424. rt_list_len(&m->parent.suspend_thread));
  425. }
  426. }
  427. }
  428. }
  429. while (next != (rt_list_t *)RT_NULL);
  430. return 0;
  431. }
  432. #endif /* RT_USING_MUTEX */
  433. #ifdef RT_USING_MAILBOX
  434. long list_mailbox(void)
  435. {
  436. rt_base_t level;
  437. list_get_next_t find_arg;
  438. struct rt_object_information *info;
  439. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  440. rt_list_t *next = (rt_list_t *)RT_NULL;
  441. int maxlen;
  442. const char *item_title = "mailbox";
  443. list_find_init(&find_arg, RT_Object_Class_MailBox, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  444. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  445. maxlen = RT_NAME_MAX;
  446. rt_kprintf("%-*.*s entry size suspend thread\n", maxlen, maxlen, item_title);
  447. object_split(maxlen);
  448. rt_kprintf(" ---- ---- --------------\n");
  449. do
  450. {
  451. next = list_get_next(next, &find_arg);
  452. {
  453. int i;
  454. for (i = 0; i < find_arg.nr_out; i++)
  455. {
  456. struct rt_object *obj;
  457. struct rt_mailbox *m;
  458. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  459. level = rt_spin_lock_irqsave(&info->spinlock);
  460. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  461. {
  462. rt_spin_unlock_irqrestore(&info->spinlock, level);
  463. continue;
  464. }
  465. rt_spin_unlock_irqrestore(&info->spinlock, level);
  466. m = (struct rt_mailbox *)obj;
  467. if (!rt_list_isempty(&m->parent.suspend_thread))
  468. {
  469. rt_kprintf("%-*.*s %04d %04d %d:",
  470. maxlen, RT_NAME_MAX,
  471. m->parent.parent.name,
  472. m->entry,
  473. m->size,
  474. rt_list_len(&m->parent.suspend_thread));
  475. rt_susp_list_print(&(m->parent.suspend_thread));
  476. rt_kprintf("\n");
  477. }
  478. else
  479. {
  480. rt_kprintf("%-*.*s %04d %04d %d\n",
  481. maxlen, RT_NAME_MAX,
  482. m->parent.parent.name,
  483. m->entry,
  484. m->size,
  485. rt_list_len(&m->parent.suspend_thread));
  486. }
  487. }
  488. }
  489. }
  490. while (next != (rt_list_t *)RT_NULL);
  491. return 0;
  492. }
  493. #endif /* RT_USING_MAILBOX */
  494. #ifdef RT_USING_MESSAGEQUEUE
  495. long list_msgqueue(void)
  496. {
  497. rt_base_t level;
  498. list_get_next_t find_arg;
  499. struct rt_object_information *info;
  500. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  501. rt_list_t *next = (rt_list_t *)RT_NULL;
  502. int maxlen;
  503. const char *item_title = "msgqueue";
  504. list_find_init(&find_arg, RT_Object_Class_MessageQueue, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  505. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  506. maxlen = RT_NAME_MAX;
  507. rt_kprintf("%-*.*s entry suspend thread\n", maxlen, maxlen, item_title);
  508. object_split(maxlen);
  509. rt_kprintf(" ---- --------------\n");
  510. do
  511. {
  512. next = list_get_next(next, &find_arg);
  513. {
  514. int i;
  515. for (i = 0; i < find_arg.nr_out; i++)
  516. {
  517. struct rt_object *obj;
  518. struct rt_messagequeue *m;
  519. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  520. level = rt_spin_lock_irqsave(&info->spinlock);
  521. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  522. {
  523. rt_spin_unlock_irqrestore(&info->spinlock, level);
  524. continue;
  525. }
  526. rt_spin_unlock_irqrestore(&info->spinlock, level);
  527. m = (struct rt_messagequeue *)obj;
  528. if (!rt_list_isempty(&m->parent.suspend_thread))
  529. {
  530. rt_kprintf("%-*.*s %04d %d:",
  531. maxlen, RT_NAME_MAX,
  532. m->parent.parent.name,
  533. m->entry,
  534. rt_list_len(&m->parent.suspend_thread));
  535. rt_susp_list_print(&(m->parent.suspend_thread));
  536. rt_kprintf("\n");
  537. }
  538. else
  539. {
  540. rt_kprintf("%-*.*s %04d %d\n",
  541. maxlen, RT_NAME_MAX,
  542. m->parent.parent.name,
  543. m->entry,
  544. rt_list_len(&m->parent.suspend_thread));
  545. }
  546. }
  547. }
  548. }
  549. while (next != (rt_list_t *)RT_NULL);
  550. return 0;
  551. }
  552. #endif /* RT_USING_MESSAGEQUEUE */
  553. #ifdef RT_USING_MEMHEAP
  554. long list_memheap(void)
  555. {
  556. rt_base_t level;
  557. list_get_next_t find_arg;
  558. struct rt_object_information *info;
  559. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  560. rt_list_t *next = (rt_list_t *)RT_NULL;
  561. int maxlen;
  562. const char *item_title = "memheap";
  563. list_find_init(&find_arg, RT_Object_Class_MemHeap, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  564. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  565. maxlen = RT_NAME_MAX;
  566. rt_kprintf("%-*.*s pool size max used size available size\n", maxlen, maxlen, item_title);
  567. object_split(maxlen);
  568. rt_kprintf(" ---------- ------------- --------------\n");
  569. do
  570. {
  571. next = list_get_next(next, &find_arg);
  572. {
  573. int i;
  574. for (i = 0; i < find_arg.nr_out; i++)
  575. {
  576. struct rt_object *obj;
  577. struct rt_memheap *mh;
  578. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  579. level = rt_spin_lock_irqsave(&info->spinlock);
  580. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  581. {
  582. rt_spin_unlock_irqrestore(&info->spinlock, level);
  583. continue;
  584. }
  585. rt_spin_unlock_irqrestore(&info->spinlock, level);
  586. mh = (struct rt_memheap *)obj;
  587. rt_kprintf("%-*.*s %-010d %-013d %-05d\n",
  588. maxlen, RT_NAME_MAX,
  589. mh->parent.name,
  590. mh->pool_size,
  591. mh->max_used_size,
  592. mh->available_size);
  593. }
  594. }
  595. }
  596. while (next != (rt_list_t *)RT_NULL);
  597. return 0;
  598. }
  599. #endif /* RT_USING_MEMHEAP */
  600. #ifdef RT_USING_MEMPOOL
  601. long list_mempool(void)
  602. {
  603. rt_base_t level;
  604. list_get_next_t find_arg;
  605. struct rt_object_information *info;
  606. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  607. rt_list_t *next = (rt_list_t *)RT_NULL;
  608. int maxlen;
  609. const char *item_title = "mempool";
  610. list_find_init(&find_arg, RT_Object_Class_MemPool, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  611. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  612. maxlen = RT_NAME_MAX;
  613. rt_kprintf("%-*.*s block total free suspend thread\n", maxlen, maxlen, item_title);
  614. object_split(maxlen);
  615. rt_kprintf(" ---- ---- ---- --------------\n");
  616. do
  617. {
  618. next = list_get_next(next, &find_arg);
  619. {
  620. int i;
  621. for (i = 0; i < find_arg.nr_out; i++)
  622. {
  623. struct rt_object *obj;
  624. struct rt_mempool *mp;
  625. int suspend_thread_count;
  626. rt_list_t *node;
  627. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  628. level = rt_spin_lock_irqsave(&info->spinlock);
  629. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  630. {
  631. rt_spin_unlock_irqrestore(&info->spinlock, level);
  632. continue;
  633. }
  634. rt_spin_unlock_irqrestore(&info->spinlock, level);
  635. mp = (struct rt_mempool *)obj;
  636. suspend_thread_count = 0;
  637. rt_list_for_each(node, &mp->suspend_thread)
  638. {
  639. suspend_thread_count++;
  640. }
  641. if (suspend_thread_count > 0)
  642. {
  643. rt_kprintf("%-*.*s %04d %04d %04d %d:",
  644. maxlen, RT_NAME_MAX,
  645. mp->parent.name,
  646. mp->block_size,
  647. mp->block_total_count,
  648. mp->block_free_count,
  649. suspend_thread_count);
  650. rt_susp_list_print(&(mp->suspend_thread));
  651. rt_kprintf("\n");
  652. }
  653. else
  654. {
  655. rt_kprintf("%-*.*s %04d %04d %04d %d\n",
  656. maxlen, RT_NAME_MAX,
  657. mp->parent.name,
  658. mp->block_size,
  659. mp->block_total_count,
  660. mp->block_free_count,
  661. suspend_thread_count);
  662. }
  663. }
  664. }
  665. }
  666. while (next != (rt_list_t *)RT_NULL);
  667. return 0;
  668. }
  669. #endif /* RT_USING_MEMPOOL */
  670. long list_timer(void)
  671. {
  672. rt_base_t level;
  673. list_get_next_t find_arg;
  674. struct rt_object_information *info;
  675. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  676. rt_list_t *next = (rt_list_t *)RT_NULL;
  677. int maxlen;
  678. const char *item_title = "timer";
  679. list_find_init(&find_arg, RT_Object_Class_Timer, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  680. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  681. maxlen = RT_NAME_MAX;
  682. rt_kprintf("%-*.*s periodic timeout activated mode\n", maxlen, maxlen, item_title);
  683. object_split(maxlen);
  684. rt_kprintf(" ---------- ---------- ----------- ---------\n");
  685. do
  686. {
  687. next = list_get_next(next, &find_arg);
  688. {
  689. int i;
  690. for (i = 0; i < find_arg.nr_out; i++)
  691. {
  692. struct rt_object *obj;
  693. struct rt_timer *timer;
  694. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  695. level = rt_spin_lock_irqsave(&info->spinlock);
  696. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  697. {
  698. rt_spin_unlock_irqrestore(&info->spinlock, level);
  699. continue;
  700. }
  701. rt_spin_unlock_irqrestore(&info->spinlock, level);
  702. timer = (struct rt_timer *)obj;
  703. rt_kprintf("%-*.*s 0x%08x 0x%08x ",
  704. maxlen, RT_NAME_MAX,
  705. timer->parent.name,
  706. timer->init_tick,
  707. timer->timeout_tick);
  708. if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
  709. rt_kprintf("activated ");
  710. else
  711. rt_kprintf("deactivated ");
  712. if (timer->parent.flag & RT_TIMER_FLAG_PERIODIC)
  713. rt_kprintf("periodic\n");
  714. else
  715. rt_kprintf("one shot\n");
  716. }
  717. }
  718. }
  719. while (next != (rt_list_t *)RT_NULL);
  720. rt_kprintf("current tick:0x%08x\n", rt_tick_get());
  721. return 0;
  722. }
  723. #ifdef RT_USING_DEVICE
  724. static char *const device_type_str[RT_Device_Class_Unknown] =
  725. {
  726. "Character Device",
  727. "Block Device",
  728. "Network Interface",
  729. "MTD Device",
  730. "CAN Device",
  731. "RTC",
  732. "Sound Device",
  733. "Graphic Device",
  734. "I2C Bus",
  735. "USB Slave Device",
  736. "USB Host Bus",
  737. "USB OTG Bus",
  738. "SPI Bus",
  739. "SPI Device",
  740. "SDIO Bus",
  741. "PM Pseudo Device",
  742. "Pipe",
  743. "Portal Device",
  744. "Timer Device",
  745. "Miscellaneous Device",
  746. "Sensor Device",
  747. "Touch Device",
  748. "Phy Device",
  749. "Security Device",
  750. "WLAN Device",
  751. "Pin Device",
  752. "ADC Device",
  753. "DAC Device",
  754. "WDT Device",
  755. "PWM Device",
  756. "Bus Device",
  757. };
  758. long list_device(void)
  759. {
  760. rt_base_t level;
  761. list_get_next_t find_arg;
  762. struct rt_object_information *info;
  763. rt_list_t *obj_list[LIST_FIND_OBJ_NR];
  764. rt_list_t *next = (rt_list_t *)RT_NULL;
  765. const char *device_type;
  766. int maxlen;
  767. const char *item_title = "device";
  768. list_find_init(&find_arg, RT_Object_Class_Device, obj_list, sizeof(obj_list) / sizeof(obj_list[0]));
  769. info = rt_list_entry(find_arg.list, struct rt_object_information, object_list);
  770. maxlen = RT_NAME_MAX;
  771. rt_kprintf("%-*.*s type ref count\n", maxlen, maxlen, item_title);
  772. object_split(maxlen);
  773. rt_kprintf(" -------------------- ----------\n");
  774. do
  775. {
  776. next = list_get_next(next, &find_arg);
  777. {
  778. int i;
  779. for (i = 0; i < find_arg.nr_out; i++)
  780. {
  781. struct rt_object *obj;
  782. struct rt_device *device;
  783. obj = rt_list_entry(obj_list[i], struct rt_object, list);
  784. level = rt_spin_lock_irqsave(&info->spinlock);
  785. if ((obj->type & ~RT_Object_Class_Static) != find_arg.type)
  786. {
  787. rt_spin_unlock_irqrestore(&info->spinlock, level);
  788. continue;
  789. }
  790. rt_spin_unlock_irqrestore(&info->spinlock, level);
  791. device = (struct rt_device *)obj;
  792. device_type = "Unknown";
  793. if (device->type < RT_Device_Class_Unknown &&
  794. device_type_str[device->type] != RT_NULL)
  795. {
  796. device_type = device_type_str[device->type];
  797. }
  798. rt_kprintf("%-*.*s %-20s %-8d\n",
  799. maxlen, RT_NAME_MAX,
  800. device->parent.name,
  801. device_type,
  802. device->ref_count);
  803. }
  804. }
  805. }
  806. while (next != (rt_list_t *)RT_NULL);
  807. return 0;
  808. }
  809. #endif /* RT_USING_DEVICE */
  810. #ifndef FINSH_USING_OPTION_COMPLETION
  811. static int cmd_list(int argc, char **argv)
  812. {
  813. if(argc == 2)
  814. {
  815. if(strcmp(argv[1], "thread") == 0)
  816. {
  817. list_thread();
  818. }
  819. else if(strcmp(argv[1], "timer") == 0)
  820. {
  821. list_timer();
  822. }
  823. #ifdef RT_USING_SEMAPHORE
  824. else if(strcmp(argv[1], "sem") == 0)
  825. {
  826. list_sem();
  827. }
  828. #endif /* RT_USING_SEMAPHORE */
  829. #ifdef RT_USING_EVENT
  830. else if(strcmp(argv[1], "event") == 0)
  831. {
  832. list_event();
  833. }
  834. #endif /* RT_USING_EVENT */
  835. #ifdef RT_USING_MUTEX
  836. else if(strcmp(argv[1], "mutex") == 0)
  837. {
  838. list_mutex();
  839. }
  840. #endif /* RT_USING_MUTEX */
  841. #ifdef RT_USING_MAILBOX
  842. else if(strcmp(argv[1], "mailbox") == 0)
  843. {
  844. list_mailbox();
  845. }
  846. #endif /* RT_USING_MAILBOX */
  847. #ifdef RT_USING_MESSAGEQUEUE
  848. else if(strcmp(argv[1], "msgqueue") == 0)
  849. {
  850. list_msgqueue();
  851. }
  852. #endif /* RT_USING_MESSAGEQUEUE */
  853. #ifdef RT_USING_MEMHEAP
  854. else if(strcmp(argv[1], "memheap") == 0)
  855. {
  856. list_memheap();
  857. }
  858. #endif /* RT_USING_MEMHEAP */
  859. #ifdef RT_USING_MEMPOOL
  860. else if(strcmp(argv[1], "mempool") == 0)
  861. {
  862. list_mempool();
  863. }
  864. #endif /* RT_USING_MEMPOOL */
  865. #ifdef RT_USING_DEVICE
  866. else if(strcmp(argv[1], "device") == 0)
  867. {
  868. list_device();
  869. }
  870. #endif /* RT_USING_DEVICE */
  871. #ifdef RT_USING_DFS
  872. else if(strcmp(argv[1], "fd") == 0)
  873. {
  874. extern int list_fd(void);
  875. list_fd();
  876. }
  877. #endif /* RT_USING_DFS */
  878. else
  879. {
  880. goto _usage;
  881. }
  882. return 0;
  883. }
  884. _usage:
  885. rt_kprintf("Usage: list [options]\n");
  886. rt_kprintf("[options]:\n");
  887. rt_kprintf(" %-12s - list threads\n", "thread");
  888. rt_kprintf(" %-12s - list timers\n", "timer");
  889. #ifdef RT_USING_SEMAPHORE
  890. rt_kprintf(" %-12s - list semaphores\n", "sem");
  891. #endif /* RT_USING_SEMAPHORE */
  892. #ifdef RT_USING_MUTEX
  893. rt_kprintf(" %-12s - list mutexs\n", "mutex");
  894. #endif /* RT_USING_MUTEX */
  895. #ifdef RT_USING_EVENT
  896. rt_kprintf(" %-12s - list events\n", "event");
  897. #endif /* RT_USING_EVENT */
  898. #ifdef RT_USING_MAILBOX
  899. rt_kprintf(" %-12s - list mailboxs\n", "mailbox");
  900. #endif /* RT_USING_MAILBOX */
  901. #ifdef RT_USING_MESSAGEQUEUE
  902. rt_kprintf(" %-12s - list message queues\n", "msgqueue");
  903. #endif /* RT_USING_MESSAGEQUEUE */
  904. #ifdef RT_USING_MEMHEAP
  905. rt_kprintf(" %-12s - list memory heaps\n", "memheap");
  906. #endif /* RT_USING_MEMHEAP */
  907. #ifdef RT_USING_MEMPOOL
  908. rt_kprintf(" %-12s - list memory pools\n", "mempool");
  909. #endif /* RT_USING_MEMPOOL */
  910. #ifdef RT_USING_DEVICE
  911. rt_kprintf(" %-12s - list devices\n", "device");
  912. #endif /* RT_USING_DEVICE */
  913. #ifdef RT_USING_DFS
  914. rt_kprintf(" %-12s - list file descriptors\n", "fd");
  915. #endif /* RT_USING_DFS */
  916. return 0;
  917. }
  918. #else
  919. CMD_OPTIONS_STATEMENT(cmd_list)
  920. static int cmd_list(int argc, char **argv)
  921. {
  922. if (argc == 2)
  923. {
  924. switch (MSH_OPT_ID_GET(cmd_list))
  925. {
  926. case RT_Object_Class_Thread: list_thread(); break;
  927. case RT_Object_Class_Timer: list_timer(); break;
  928. #ifdef RT_USING_SEMAPHORE
  929. case RT_Object_Class_Semaphore: list_sem(); break;
  930. #endif /* RT_USING_SEMAPHORE */
  931. #ifdef RT_USING_EVENT
  932. case RT_Object_Class_Event: list_event(); break;
  933. #endif /* RT_USING_EVENT */
  934. #ifdef RT_USING_MUTEX
  935. case RT_Object_Class_Mutex: list_mutex(); break;
  936. #endif /* RT_USING_MUTEX */
  937. #ifdef RT_USING_MAILBOX
  938. case RT_Object_Class_MailBox: list_mailbox(); break;
  939. #endif /* RT_USING_MAILBOX */
  940. #ifdef RT_USING_MESSAGEQUEUE
  941. case RT_Object_Class_MessageQueue: list_msgqueue(); break;
  942. #endif /* RT_USING_MESSAGEQUEUE */
  943. #ifdef RT_USING_MEMHEAP
  944. case RT_Object_Class_MemHeap: list_memheap(); break;
  945. #endif /* RT_USING_MEMHEAP */
  946. #ifdef RT_USING_MEMPOOL
  947. case RT_Object_Class_MemPool: list_mempool(); break;
  948. #endif /* RT_USING_MEMPOOL */
  949. #ifdef RT_USING_DEVICE
  950. case RT_Object_Class_Device: list_device(); break;
  951. #endif /* RT_USING_DEVICE */
  952. #ifdef RT_USING_DFS
  953. case LIST_DFS_OPT_ID:
  954. {
  955. extern int list_fd(void);
  956. list_fd();
  957. break;
  958. }
  959. #endif /* RT_USING_DFS */
  960. default:
  961. goto _usage;
  962. };
  963. return 0;
  964. }
  965. _usage:
  966. rt_kprintf("Usage: list [options]\n");
  967. rt_kprintf("[options]:\n");
  968. MSH_OPT_DUMP(cmd_list);
  969. return 0;
  970. }
  971. CMD_OPTIONS_NODE_START(cmd_list)
  972. CMD_OPTIONS_NODE(RT_Object_Class_Thread, thread, list threads)
  973. CMD_OPTIONS_NODE(RT_Object_Class_Timer, timer, list timers)
  974. #ifdef RT_USING_SEMAPHORE
  975. CMD_OPTIONS_NODE(RT_Object_Class_Semaphore, sem, list semaphores)
  976. #endif /* RT_USING_SEMAPHORE */
  977. #ifdef RT_USING_EVENT
  978. CMD_OPTIONS_NODE(RT_Object_Class_Event, event, list events)
  979. #endif /* RT_USING_EVENT */
  980. #ifdef RT_USING_MUTEX
  981. CMD_OPTIONS_NODE(RT_Object_Class_Mutex, mutex, list mutexs)
  982. #endif /* RT_USING_MUTEX */
  983. #ifdef RT_USING_MAILBOX
  984. CMD_OPTIONS_NODE(RT_Object_Class_MailBox, mailbox, list mailboxs)
  985. #endif /* RT_USING_MAILBOX */
  986. #ifdef RT_USING_MESSAGEQUEUE
  987. CMD_OPTIONS_NODE(RT_Object_Class_MessageQueue, msgqueue, list message queues)
  988. #endif /* RT_USING_MESSAGEQUEUE */
  989. #ifdef RT_USING_MEMHEAP
  990. CMD_OPTIONS_NODE(RT_Object_Class_MemHeap, memheap, list memory heaps)
  991. #endif /* RT_USING_MEMHEAP */
  992. #ifdef RT_USING_MEMPOOL
  993. CMD_OPTIONS_NODE(RT_Object_Class_MemPool, mempool, list memory pools)
  994. #endif /* RT_USING_MEMPOOL */
  995. #ifdef RT_USING_DEVICE
  996. CMD_OPTIONS_NODE(RT_Object_Class_Device, device, list devices)
  997. #endif /* RT_USING_DEVICE */
  998. #ifdef RT_USING_DFS
  999. CMD_OPTIONS_NODE(LIST_DFS_OPT_ID, fd, list file descriptors)
  1000. #endif /* RT_USING_DFS */
  1001. CMD_OPTIONS_NODE_END
  1002. #endif /* FINSH_USING_OPTION_COMPLETION */
  1003. MSH_CMD_EXPORT_ALIAS(cmd_list, list, list objects, optenable);
  1004. #endif /* RT_USING_FINSH */