cmd.c 36 KB

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