cmd.c 35 KB

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