cmd.c 14.5 KB
Newer Older
1 2 3 4 5 6 7
/*
 * File      : cmd.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
8
 * http://www.rt-thread.org/license/LICENSE
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-04-30     Bernard      first implementation
 * 2006-05-04     Bernard      add list_thread,
 *                                 list_sem,
 *                                 list_timer
 * 2006-05-20     Bernard      add list_mutex,
 *                                 list_mailbox,
 *                                 list_msgqueue,
 *                                 list_event,
 *                                 list_fevent,
 *                                 list_mempool
 * 2006-06-03     Bernard      display stack information in list_thread
 * 2006-08-10     Bernard      change version to invoke rt_show_version
 * 2008-09-10     Bernard      update the list function for finsh syscall
 *                                 list and sysvar list
 * 2009-05-30     Bernard      add list_device
 */

#include <rtthread.h>
30 31 32 33
#include "finsh.h"

// Copy from kservice.h because we can not use it out of the kernel.
// Ugly. Should let kservice.h avaliable for applications?
M
mbbill 已提交
34 35 36
rt_inline int rt_list_isempty(const rt_list_t *l)
{
	return l->next == l;
37 38 39 40 41 42 43 44 45 46 47 48
}

rt_inline unsigned int rt_list_len(const rt_list_t *l)
{
	unsigned int len = 0;
	const rt_list_t *p = l;
	while( p->next != l )
	{
		p = p->next;
		len++;
	}
	return len;
M
mbbill 已提交
49
}
50

51
long hello(void)
52 53 54 55 56
{
	rt_kprintf("Hello RT-Thread!\n");

	return 0;
}
57
FINSH_FUNCTION_EXPORT(hello, say hello world);
58 59

extern void rt_show_version(void);
60
long version(void)
61 62 63 64 65
{
	rt_show_version();

	return 0;
}
66
FINSH_FUNCTION_EXPORT(version, show RT-Thread version information);
67 68 69 70 71

#define rt_list_entry(node, type, member) \
    ((type *)((char *)(node) - (unsigned long)(&((type *)0)->member)))
extern struct rt_object_information rt_object_container[];

72
long list_thread(void)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
{
	struct rt_thread *thread;
	struct rt_list_node *list, *node;
	rt_uint8_t* ptr;

	list = &rt_object_container[RT_Object_Class_Thread].object_list;

	rt_kprintf(" thread  pri  status      sp     stack size max used   left tick  error\n");
	rt_kprintf("-------- ---- ------- ---------- ---------- ---------- ---------- ---\n");
	for (node = list->next; node != list; node = node->next)
	{
		thread = rt_list_entry(node, struct rt_thread, list);
		rt_kprintf("%-8s 0x%02x", thread->name, thread->current_priority);

		if (thread->stat == RT_THREAD_READY)		rt_kprintf(" ready  ");
		else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
		else if (thread->stat == RT_THREAD_INIT)	rt_kprintf(" init   ");

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

		rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
			thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
			thread->stack_size,
			thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
			thread->remaining_tick,
			thread->error);

	}
	return 0;
}
104
FINSH_FUNCTION_EXPORT(list_thread, list thread);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

static void show_wait_queue(struct rt_list_node* list)
{
	struct rt_thread *thread;
	struct rt_list_node *node;

	for (node = list->next; node != list; node = node->next)
	{
		thread = rt_list_entry(node, struct rt_thread, tlist);
		rt_kprintf("%s", thread->name);
		if (node->next != list) rt_kprintf("/");
	}
}

#ifdef RT_USING_SEMAPHORE
120
long list_sem(void)
121 122 123 124 125 126 127 128 129 130
{
	struct rt_semaphore *sem;
	struct rt_list_node *list, *node;

	list = &rt_object_container[RT_Object_Class_Semaphore].object_list;

	rt_kprintf("semaphore v   suspend thread\n");
	rt_kprintf("--------  --- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
131
		sem = (struct rt_semaphore*)(rt_list_entry(node, struct rt_object, list));
M
mbbill 已提交
132
		if( !rt_list_isempty(&sem->parent.suspend_thread) )
133
		{
M
mbbill 已提交
134
			rt_kprintf("%-8s  %03d %d:", sem->parent.parent.name, sem->value, rt_list_len(&sem->parent.suspend_thread) );
135 136 137 138 139
			show_wait_queue(&(sem->parent.suspend_thread));
			rt_kprintf("\n");
		}
		else
		{
M
mbbill 已提交
140
			rt_kprintf("%-8s  %03d %d\n", sem->parent.parent.name, sem->value, rt_list_len(&sem->parent.suspend_thread));
141 142 143 144 145 146 147 148 149
		}
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list_sem, list semaphone in system)
#endif

#ifdef RT_USING_EVENT
150
long list_event(void)
151 152 153 154 155 156 157 158 159 160 161
{
	struct rt_event *e;
	struct rt_list_node *list, *node;

	list = &rt_object_container[RT_Object_Class_Event].object_list;

	rt_kprintf("event    set        suspend thread\n");
	rt_kprintf("-------- ---------- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
		e = (struct rt_event*)(rt_list_entry(node, struct rt_object, list));
M
mbbill 已提交
162
		rt_kprintf("%-8s  0x%08x %03d\n", e->parent.parent.name, e->set, rt_list_len(&e->parent.suspend_thread));
163 164 165 166 167 168 169 170
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list_event, list event in system)
#endif

#ifdef RT_USING_MUTEX
171
long list_mutex(void)
172 173 174 175 176 177 178 179 180 181 182
{
	struct rt_mutex *m;
	struct rt_list_node *list, *node;

	list = &rt_object_container[RT_Object_Class_Mutex].object_list;

	rt_kprintf("mutex    owner    hold suspend thread\n");
	rt_kprintf("-------- -------- ---- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
		m = (struct rt_mutex*)(rt_list_entry(node, struct rt_object, list));
M
mbbill 已提交
183
		rt_kprintf("%-8s %-8s %04d %d\n", m->parent.parent.name, m->owner->name, m->hold, rt_list_len(&m->parent.suspend_thread));
184 185 186 187 188 189 190 191
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list_mutex, list mutex in system)
#endif

#ifdef RT_USING_MAILBOX
192
long list_mailbox(void)
193 194 195 196 197 198 199 200 201 202
{
	struct rt_mailbox *m;
	struct rt_list_node *list, *node;

	list = &rt_object_container[RT_Object_Class_MailBox].object_list;

	rt_kprintf("mailbox  entry size suspend thread\n");
	rt_kprintf("-------- ----  ---- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
203
		m = (struct rt_mailbox*)(rt_list_entry(node, struct rt_object, list));
M
mbbill 已提交
204
		if( !rt_list_isempty(&m->parent.suspend_thread) )
205
		{
M
mbbill 已提交
206
			rt_kprintf("%-8s %04d  %04d %d:", m->parent.parent.name, m->entry, m->size, rt_list_len(&m->parent.suspend_thread));
207 208 209 210 211
			show_wait_queue(&(m->parent.suspend_thread));
			rt_kprintf("\n");
		}
		else
		{
M
mbbill 已提交
212
			rt_kprintf("%-8s %04d  %04d %d\n", m->parent.parent.name, m->entry, m->size, rt_list_len(&m->parent.suspend_thread));
213 214 215 216 217 218 219 220 221
		}
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list_mailbox, list mail box in system)
#endif

#ifdef RT_USING_MESSAGEQUEUE
222
long list_msgqueue(void)
223 224 225 226 227 228 229 230 231 232 233
{
	struct rt_messagequeue *m;
	struct rt_list_node *list, *node;

	list = &rt_object_container[RT_Object_Class_MessageQueue].object_list;

	rt_kprintf("msgqueue entry suspend thread\n");
	rt_kprintf("-------- ----  --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
		m = (struct rt_messagequeue*)(rt_list_entry(node, struct rt_object, list));
234 235 236 237 238 239 240 241 242 243
		if( !rt_list_isempty(&m->parent.suspend_thread) )
		{
			rt_kprintf("%-8s %04d  %d:", m->parent.parent.name, m->entry, rt_list_len(&m->parent.suspend_thread));
			show_wait_queue(&(m->parent.suspend_thread));
			rt_kprintf("\n");
		}
		else
		{
			rt_kprintf("%-8s %04d  %d\n", m->parent.parent.name, m->entry, rt_list_len(&m->parent.suspend_thread));
		}
244 245 246 247 248 249 250 251
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list_msgqueue, list message queue in system)
#endif

#ifdef RT_USING_MEMPOOL
252
long list_mempool(void)
253 254 255 256 257 258 259 260 261 262 263
{
	struct rt_mempool *mp;
	struct rt_list_node *list, *node;

	list = &rt_object_container[RT_Object_Class_MemPool].object_list;

	rt_kprintf("mempool  block total free suspend thread\n");
	rt_kprintf("-------- ----  ----  ---- --------------\n");
	for (node = list->next; node != list; node = node->next)
	{
		mp = (struct rt_mempool*)rt_list_entry(node, struct rt_object, list);
264 265 266 267 268 269 270 271 272 273 274 275 276 277
		if (mp->suspend_thread_count > 0)
		{
			rt_kprintf("%-8s %04d  %04d  %04d %d:", mp->parent.name,
				mp->block_size, mp->block_total_count, mp->block_free_count,
				mp->suspend_thread_count);
			show_wait_queue(&(mp->suspend_thread));
			rt_kprintf("\n");			
		}
		else
		{
			rt_kprintf("%-8s %04d  %04d  %04d %d\n", mp->parent.name,
				mp->block_size, mp->block_total_count, mp->block_free_count,
				mp->suspend_thread_count);
		}
278 279 280 281 282 283 284
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list_mempool, list memory pool in system)
#endif

285
long list_timer(void)
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
	struct rt_timer *timer;
	struct rt_list_node *list, *node;

	list = &rt_object_container[RT_Object_Class_Timer].object_list;

	rt_kprintf("timer    periodic   timeout    flag\n");
	rt_kprintf("-------- ---------- ---------- -----------\n");
	for (node = list->next; node != list; node = node->next)
	{
		timer = (struct rt_timer*)(rt_list_entry(node, struct rt_object, list));
		rt_kprintf("%-8s 0x%08x 0x%08x ", timer->parent.name, timer->init_tick, timer->timeout_tick);
		if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) rt_kprintf("activated\n");
		else rt_kprintf("deactivated\n");
	}

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

	return 0;
}
FINSH_FUNCTION_EXPORT(list_timer, list timer in system)

#ifdef RT_USING_DEVICE
309
long list_device(void)
310 311 312
{
	struct rt_device *device;
	struct rt_list_node *list, *node;
B
bernard.xiong 已提交
313
	const char *device_type_str[] =
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	{
		"Character Device",
		"Block Device",
		"Network Interface",
		"MTD Device",
		"CAN",
		"RTC",
		"Unknown"
	};

	list = &rt_object_container[RT_Object_Class_Device].object_list;

	rt_kprintf("device    type      \n");
	rt_kprintf("-------- ---------- \n");
	for (node = list->next; node != list; node = node->next)
	{
		device = (struct rt_device*)(rt_list_entry(node, struct rt_object, list));
		rt_kprintf("%-8s %-8s \n", device->parent.name, device_type_str[device->type]);
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list_device, list device in system)
#endif

qiuyiuestc's avatar
qiuyiuestc 已提交
339
#ifdef RT_USING_MODULE
340
int list_module(void)
qiuyiuestc's avatar
qiuyiuestc 已提交
341 342 343 344 345 346
{
	struct rt_module *module;
	struct rt_list_node *list, *node;

	list = &rt_object_container[RT_Object_Class_Module].object_list;

qiuyiuestc's avatar
qiuyiuestc 已提交
347
	rt_kprintf("module entry      stack size\n");
qiuyiuestc's avatar
qiuyiuestc 已提交
348 349 350
	rt_kprintf("-------- ---------- ----------\n");
	for (node = list->next; node != list; node = node->next)
	{
qiuyiuestc's avatar
qiuyiuestc 已提交
351 352 353 354
		struct rt_thread *thread;
		struct rt_list_node *tlist, *tnode;
		rt_uint8_t* ptr;
		
355
		module = (struct rt_module*)(rt_list_entry(node, struct rt_object, list));
qiuyiuestc's avatar
qiuyiuestc 已提交
356 357
		rt_kprintf("%-8s 0x%08x 0x%08x \n", module->parent.name, (rt_uint32_t)module->module_entry,
			module->stack_size);
qiuyiuestc's avatar
qiuyiuestc 已提交
358 359

		tlist = &module->module_object[RT_Object_Class_Thread].object_list;
qiuyiuestc's avatar
qiuyiuestc 已提交
360 361 362 363

		rt_kprintf(" sub-thread  pri  status      sp     stack size max used   left tick  error\n");
		rt_kprintf("-------- ---- ------- ---------- ---------- ---------- ---------- ---\n");

qiuyiuestc's avatar
qiuyiuestc 已提交
364 365
		for (tnode = tlist->next; tnode != tlist; tnode = tnode->next)
		{
qiuyiuestc's avatar
qiuyiuestc 已提交
366
			thread = rt_list_entry(tnode, struct rt_thread, list);
qiuyiuestc's avatar
qiuyiuestc 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
			rt_kprintf("%-8s 0x%02x", thread->name, thread->current_priority);

			if (thread->stat == RT_THREAD_READY)		rt_kprintf(" ready  ");
			else if (thread->stat == RT_THREAD_SUSPEND) rt_kprintf(" suspend");
			else if (thread->stat == RT_THREAD_INIT)	rt_kprintf(" init   ");

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

			rt_kprintf(" 0x%08x 0x%08x 0x%08x 0x%08x %03d\n",
				thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp),
				thread->stack_size,
				thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t)thread->stack_addr),
				thread->remaining_tick,
				thread->error);
		}
qiuyiuestc's avatar
qiuyiuestc 已提交
383 384 385 386 387 388 389
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list_module, list module in system)
#endif

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
int list()
{
	struct finsh_syscall_item* syscall_item;
	struct finsh_sysvar_item*  sysvar_item;

	rt_kprintf("--Function List:\n");
	{
		struct finsh_syscall* index;
		for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
		{
#ifdef FINSH_USING_DESCRIPTION
			rt_kprintf("%-16s -- %s\n", index->name, index->desc);
#else
			rt_kprintf("%s\n", index->name);
#endif
		}
	}

	/* list syscall list */
	syscall_item = global_syscall_list;
	while (syscall_item != NULL)
	{
		rt_kprintf("[l] %s\n", syscall_item->syscall.name);
		syscall_item = syscall_item->next;
	}

	rt_kprintf("--Variable List:\n");
	{
		struct finsh_sysvar* index;
		for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
		{
#ifdef FINSH_USING_DESCRIPTION
			rt_kprintf("%-16s -- %s\n", index->name, index->desc);
#else
			rt_kprintf("%s\n", index->name);
#endif
		}
	}

	sysvar_item = global_sysvar_list;
	while (sysvar_item != NULL)
	{
		rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
		sysvar_item = sysvar_item->next;
	}

	return 0;
}
FINSH_FUNCTION_EXPORT(list, list all symbol in system)

440
static int str_is_prefix(const char* prefix, const char* str)
441 442 443 444 445 446 447 448 449 450 451 452 453 454
{
	while ((*prefix) && (*prefix == *str))
	{
		prefix ++;
		str ++;
	}

	if (*prefix == 0) return 0;
	return -1;
}

void list_prefix(char* prefix)
{
	struct finsh_syscall_item* syscall_item;
455 456 457 458 459
	struct finsh_sysvar_item*  sysvar_item;
	rt_uint16_t func_cnt, var_cnt;
	const char* name_ptr;

	func_cnt = 0;
460 461
	var_cnt  = 0;
	name_ptr = RT_NULL;
B
bernard.xiong 已提交
462

463 464 465 466 467
	{
		struct finsh_syscall* index;
		for (index = _syscall_table_begin; index < _syscall_table_end; index ++)
		{
			if (str_is_prefix(prefix, index->name) == 0)
468
			{
469
				if (func_cnt == 0)
470 471 472 473 474
					rt_kprintf("--function:\n");

				func_cnt ++;
				/* set name_ptr */
				name_ptr = index->name;
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

#ifdef FINSH_USING_DESCRIPTION
				rt_kprintf("%-16s -- %s\n", index->name, index->desc);
#else
				rt_kprintf("%s\n", index->name);
#endif
			}
		}
	}

	/* list syscall list */
	syscall_item = global_syscall_list;
	while (syscall_item != NULL)
	{
		if (str_is_prefix(prefix, syscall_item->syscall.name) == 0)
490
		{
491 492
			if (func_cnt == 0)
				rt_kprintf("--function:\n");
493 494 495 496
			func_cnt ++;
			/* set name_ptr */
			name_ptr = syscall_item->syscall.name;

497 498 499 500 501 502 503 504 505 506
			rt_kprintf("[l] %s\n", syscall_item->syscall.name);
		}
		syscall_item = syscall_item->next;
	}

	{
		struct finsh_sysvar* index;
		for (index = _sysvar_table_begin; index < _sysvar_table_end; index ++)
		{
			if (str_is_prefix(prefix, index->name) == 0)
507 508 509
			{
				if (var_cnt == 0)
					rt_kprintf("--variable:\n");
510

511 512 513
				var_cnt ++;
				/* set name ptr */
				name_ptr = index->name;
B
bernard.xiong 已提交
514

515 516 517 518 519 520 521 522 523 524 525 526 527 528
#ifdef FINSH_USING_DESCRIPTION
				rt_kprintf("%-16s -- %s\n", index->name, index->desc);
#else
				rt_kprintf("%s\n", index->name);
#endif
			}
		}
	}

	sysvar_item = global_sysvar_list;
	while (sysvar_item != NULL)
	{
		if (str_is_prefix(prefix, sysvar_item->sysvar.name) == 0)
		{
529 530 531 532 533 534 535
			if (var_cnt == 0)
				rt_kprintf("--variable:\n");

			var_cnt ++;
			/* set name ptr */
			name_ptr = sysvar_item->sysvar.name;

536 537 538
			rt_kprintf("[l] %s\n", sysvar_item->sysvar.name);
		}
		sysvar_item = sysvar_item->next;
539 540 541 542 543 544
	}

	/* only one matched */
	if ((func_cnt + var_cnt) == 1)
	{
		rt_strncpy(prefix, name_ptr, strlen(name_ptr));
545 546 547
	}
}

548 549 550 551
#ifdef FINSH_USING_SYMTAB
static int dummy = 0;
FINSH_VAR_EXPORT(dummy, finsh_type_int, dummy variable for finsh)
#endif