2016-11-04 08:09:17 -04:00
|
|
|
/*
|
2018-08-09 15:03:58 +02:00
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
2016-11-04 08:09:17 -04:00
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-11-04 08:09:17 -04:00
|
|
|
*/
|
|
|
|
|
2019-06-26 10:33:49 -04:00
|
|
|
#include <sys/printk.h>
|
2018-08-09 15:03:58 +02:00
|
|
|
#include <shell/shell.h>
|
2016-11-04 08:09:17 -04:00
|
|
|
#include <init.h>
|
2016-12-24 07:10:20 -05:00
|
|
|
#include <debug/object_tracing.h>
|
2019-06-26 10:44:43 -04:00
|
|
|
#include <power/reboot.h>
|
2019-06-26 10:42:45 -04:00
|
|
|
#include <debug/stack.h>
|
2018-04-22 22:41:36 +02:00
|
|
|
#include <string.h>
|
2018-10-05 09:38:55 -05:00
|
|
|
#include <device.h>
|
2019-07-31 12:43:54 +03:00
|
|
|
#include <drivers/timer/system_timer.h>
|
2016-11-04 08:09:17 -04:00
|
|
|
|
2018-10-01 22:08:59 +02:00
|
|
|
static int cmd_kernel_version(const struct shell *shell,
|
2018-08-09 15:03:58 +02:00
|
|
|
size_t argc, char **argv)
|
2016-11-04 08:09:17 -04:00
|
|
|
{
|
2017-04-21 09:36:04 -05:00
|
|
|
u32_t version = sys_kernel_version_get();
|
2016-11-04 08:09:17 -04:00
|
|
|
|
2016-12-21 00:11:41 -06:00
|
|
|
ARG_UNUSED(argc);
|
|
|
|
ARG_UNUSED(argv);
|
|
|
|
|
2019-07-31 12:43:54 +03:00
|
|
|
shell_print(shell, "Zephyr version %d.%d.%d",
|
2018-08-09 15:03:58 +02:00
|
|
|
SYS_KERNEL_VER_MAJOR(version),
|
|
|
|
SYS_KERNEL_VER_MINOR(version),
|
|
|
|
SYS_KERNEL_VER_PATCHLEVEL(version));
|
2018-10-01 22:08:59 +02:00
|
|
|
return 0;
|
2016-11-04 08:09:17 -04:00
|
|
|
}
|
|
|
|
|
2018-10-01 22:08:59 +02:00
|
|
|
static int cmd_kernel_uptime(const struct shell *shell,
|
|
|
|
size_t argc, char **argv)
|
2016-11-04 08:09:17 -04:00
|
|
|
{
|
2016-12-21 00:11:41 -06:00
|
|
|
ARG_UNUSED(argc);
|
|
|
|
ARG_UNUSED(argv);
|
|
|
|
|
2019-07-31 12:43:54 +03:00
|
|
|
shell_print(shell, "Uptime: %u ms", k_uptime_get_32());
|
2018-10-01 22:08:59 +02:00
|
|
|
return 0;
|
2016-11-04 08:09:17 -04:00
|
|
|
}
|
|
|
|
|
2018-10-01 22:08:59 +02:00
|
|
|
static int cmd_kernel_cycles(const struct shell *shell,
|
2018-08-09 15:03:58 +02:00
|
|
|
size_t argc, char **argv)
|
2016-11-04 08:09:17 -04:00
|
|
|
{
|
2016-12-21 00:11:41 -06:00
|
|
|
ARG_UNUSED(argc);
|
|
|
|
ARG_UNUSED(argv);
|
|
|
|
|
2019-07-31 12:43:54 +03:00
|
|
|
shell_print(shell, "cycles: %u hw cycles", k_cycle_get_32());
|
2018-10-01 22:08:59 +02:00
|
|
|
return 0;
|
2016-11-04 08:09:17 -04:00
|
|
|
}
|
|
|
|
|
2018-09-25 06:12:08 +05:30
|
|
|
#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_MONITOR) \
|
|
|
|
&& defined(CONFIG_THREAD_STACK_INFO)
|
2019-07-31 12:43:54 +03:00
|
|
|
static void shell_tdata_dump(const struct k_thread *cthread, void *user_data)
|
2018-05-09 10:23:43 +05:30
|
|
|
{
|
2019-07-31 12:43:54 +03:00
|
|
|
struct k_thread *thread = (struct k_thread *)cthread;
|
|
|
|
const struct shell *shell = (const struct shell *)user_data;
|
2018-11-29 11:23:03 -08:00
|
|
|
unsigned int pcnt, unused = 0U;
|
2018-09-25 06:12:08 +05:30
|
|
|
unsigned int size = thread->stack_info.size;
|
2018-08-12 14:04:16 -05:00
|
|
|
const char *tname;
|
|
|
|
|
2019-07-31 12:43:54 +03:00
|
|
|
unused = stack_unused_space_get((char *)thread->stack_info.start, size);
|
2018-09-25 06:12:08 +05:30
|
|
|
|
|
|
|
/* Calculate the real size reserved for the stack */
|
2019-03-26 19:57:45 -06:00
|
|
|
pcnt = ((size - unused) * 100U) / size;
|
2018-09-25 06:12:08 +05:30
|
|
|
|
2019-07-31 12:43:54 +03:00
|
|
|
tname = k_thread_name_get(thread);
|
2018-08-12 14:04:16 -05:00
|
|
|
|
2019-07-31 12:43:54 +03:00
|
|
|
shell_print(shell, "%s%p %-10s",
|
2018-08-09 15:03:58 +02:00
|
|
|
(thread == k_current_get()) ? "*" : " ",
|
|
|
|
thread,
|
2018-09-25 06:12:08 +05:30
|
|
|
tname ? tname : "NA");
|
2019-07-31 12:43:54 +03:00
|
|
|
shell_print(shell, "\toptions: 0x%x, priority: %d timeout: %d",
|
2018-08-09 15:03:58 +02:00
|
|
|
thread->base.user_options,
|
2019-07-31 12:43:54 +03:00
|
|
|
thread->base.prio,
|
|
|
|
thread->base.timeout.dticks);
|
|
|
|
shell_print(shell, "\tstate: %s", k_thread_state_str(thread));
|
|
|
|
shell_print(shell, "\tstack size %u, unused %u, usage %u / %u (%u %%)\n",
|
2018-09-25 06:12:08 +05:30
|
|
|
size, unused, size - unused, size, pcnt);
|
|
|
|
|
2018-05-09 10:23:43 +05:30
|
|
|
}
|
|
|
|
|
2018-10-01 22:08:59 +02:00
|
|
|
static int cmd_kernel_threads(const struct shell *shell,
|
2018-08-09 15:03:58 +02:00
|
|
|
size_t argc, char **argv)
|
2016-12-24 07:10:20 -05:00
|
|
|
{
|
|
|
|
ARG_UNUSED(argc);
|
|
|
|
ARG_UNUSED(argv);
|
|
|
|
|
2019-07-31 12:43:54 +03:00
|
|
|
shell_print(shell, "Scheduler: %u since last call", z_clock_elapsed());
|
|
|
|
shell_print(shell, "Threads:");
|
2018-08-09 15:03:58 +02:00
|
|
|
k_thread_foreach(shell_tdata_dump, (void *)shell);
|
2018-10-01 22:08:59 +02:00
|
|
|
return 0;
|
2016-12-24 07:10:20 -05:00
|
|
|
}
|
2016-11-04 08:09:17 -04:00
|
|
|
|
2018-05-09 10:23:43 +05:30
|
|
|
static void shell_stack_dump(const struct k_thread *thread, void *user_data)
|
|
|
|
{
|
2018-11-29 11:23:03 -08:00
|
|
|
unsigned int pcnt, unused = 0U;
|
2018-08-09 15:03:58 +02:00
|
|
|
unsigned int size = thread->stack_info.size;
|
2018-08-12 14:04:16 -05:00
|
|
|
const char *tname;
|
2018-08-09 15:03:58 +02:00
|
|
|
|
2018-08-12 14:04:16 -05:00
|
|
|
tname = k_thread_name_get((struct k_thread *)thread);
|
2019-07-31 12:43:54 +03:00
|
|
|
unused = stack_unused_space_get((char *)thread->stack_info.start, size);
|
2018-08-09 15:03:58 +02:00
|
|
|
|
|
|
|
/* Calculate the real size reserved for the stack */
|
2019-03-26 19:57:45 -06:00
|
|
|
pcnt = ((size - unused) * 100U) / size;
|
2018-08-09 15:03:58 +02:00
|
|
|
|
2019-07-31 12:43:54 +03:00
|
|
|
shell_print((const struct shell *)user_data,
|
|
|
|
"0x%08X %-10s (real size %u):\tunused %u\tusage %u / %u (%u %%)",
|
2018-08-12 14:04:16 -05:00
|
|
|
(u32_t)thread,
|
|
|
|
tname ? tname : "NA",
|
|
|
|
size, unused, size - unused, size, pcnt);
|
2018-05-09 10:23:43 +05:30
|
|
|
}
|
|
|
|
|
2018-10-01 22:08:59 +02:00
|
|
|
static int cmd_kernel_stacks(const struct shell *shell,
|
|
|
|
size_t argc, char **argv)
|
2017-01-10 08:41:12 -05:00
|
|
|
{
|
2018-08-09 15:03:58 +02:00
|
|
|
ARG_UNUSED(argc);
|
|
|
|
ARG_UNUSED(argv);
|
|
|
|
k_thread_foreach(shell_stack_dump, (void *)shell);
|
2018-10-01 22:08:59 +02:00
|
|
|
return 0;
|
2017-01-10 08:41:12 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-22 22:41:36 +02:00
|
|
|
#if defined(CONFIG_REBOOT)
|
2018-10-01 22:08:59 +02:00
|
|
|
static int cmd_kernel_reboot_warm(const struct shell *shell,
|
|
|
|
size_t argc, char **argv)
|
2018-04-22 22:41:36 +02:00
|
|
|
{
|
2018-08-09 15:03:58 +02:00
|
|
|
ARG_UNUSED(argc);
|
|
|
|
ARG_UNUSED(argv);
|
|
|
|
sys_reboot(SYS_REBOOT_WARM);
|
2018-10-01 22:08:59 +02:00
|
|
|
return 0;
|
2018-04-22 22:41:36 +02:00
|
|
|
}
|
2018-08-09 15:03:58 +02:00
|
|
|
|
2018-10-01 22:08:59 +02:00
|
|
|
static int cmd_kernel_reboot_cold(const struct shell *shell,
|
|
|
|
size_t argc, char **argv)
|
2018-08-09 15:03:58 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(argc);
|
|
|
|
ARG_UNUSED(argv);
|
|
|
|
sys_reboot(SYS_REBOOT_COLD);
|
2018-10-01 22:08:59 +02:00
|
|
|
return 0;
|
2018-08-09 15:03:58 +02:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:53:29 +01:00
|
|
|
SHELL_STATIC_SUBCMD_SET_CREATE(sub_kernel_reboot,
|
2018-08-09 15:03:58 +02:00
|
|
|
SHELL_CMD(cold, NULL, "Cold reboot.", cmd_kernel_reboot_cold),
|
|
|
|
SHELL_CMD(warm, NULL, "Warm reboot.", cmd_kernel_reboot_warm),
|
|
|
|
SHELL_SUBCMD_SET_END /* Array terminated. */
|
2019-02-13 14:53:29 +01:00
|
|
|
);
|
2018-04-22 22:41:36 +02:00
|
|
|
#endif
|
|
|
|
|
2019-02-13 14:53:29 +01:00
|
|
|
SHELL_STATIC_SUBCMD_SET_CREATE(sub_kernel,
|
2018-08-09 15:03:58 +02:00
|
|
|
SHELL_CMD(cycles, NULL, "Kernel cycles.", cmd_kernel_cycles),
|
|
|
|
#if defined(CONFIG_REBOOT)
|
|
|
|
SHELL_CMD(reboot, &sub_kernel_reboot, "Reboot.", NULL),
|
2017-01-10 08:41:12 -05:00
|
|
|
#endif
|
2018-05-09 10:23:43 +05:30
|
|
|
#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_MONITOR) \
|
|
|
|
&& defined(CONFIG_THREAD_STACK_INFO)
|
2018-08-09 15:03:58 +02:00
|
|
|
SHELL_CMD(stacks, NULL, "List threads stack usage.", cmd_kernel_stacks),
|
|
|
|
SHELL_CMD(threads, NULL, "List kernel threads.", cmd_kernel_threads),
|
2016-12-24 07:10:20 -05:00
|
|
|
#endif
|
2018-08-09 15:03:58 +02:00
|
|
|
SHELL_CMD(uptime, NULL, "Kernel uptime.", cmd_kernel_uptime),
|
|
|
|
SHELL_CMD(version, NULL, "Kernel version.", cmd_kernel_version),
|
|
|
|
SHELL_SUBCMD_SET_END /* Array terminated. */
|
2019-02-13 14:53:29 +01:00
|
|
|
);
|
2016-11-04 08:09:17 -04:00
|
|
|
|
2018-08-09 15:03:58 +02:00
|
|
|
SHELL_CMD_REGISTER(kernel, &sub_kernel, "Kernel commands", NULL);
|