shell: modules: Adapt kernel commands to new shell

Kernel commands ported to new shell.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2018-08-09 15:03:58 +02:00 committed by Anas Nashif
parent 8feab483c9
commit 73c01f9e9f
2 changed files with 79 additions and 62 deletions

View file

@ -31,7 +31,6 @@ config CONSOLE_SHELL_MAX_CMD_QUEUED
help
Maximum size of the queue for input commands.
source "subsys/shell/modules/Kconfig"
endif
@ -175,6 +174,8 @@ config SHELL_LOG_BACKEND
default y if LOG
default n if !LOG
source "subsys/shell/modules/Kconfig"
endif #SHELL
endmenu

View file

@ -1,72 +1,71 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <misc/printk.h>
#include <shell/legacy_shell.h>
#include <shell/shell.h>
#include <init.h>
#include <debug/object_tracing.h>
#include <misc/reboot.h>
#include <misc/stack.h>
#include <string.h>
#define SHELL_KERNEL "kernel"
static int shell_cmd_version(int argc, char *argv[])
static void cmd_kernel_version(const struct shell *shell,
size_t argc, char **argv)
{
u32_t version = sys_kernel_version_get();
ARG_UNUSED(argc);
ARG_UNUSED(argv);
printk("Zephyr version %d.%d.%d\n",
SYS_KERNEL_VER_MAJOR(version),
SYS_KERNEL_VER_MINOR(version),
SYS_KERNEL_VER_PATCHLEVEL(version));
return 0;
shell_fprintf(shell, SHELL_NORMAL, "Zephyr version %d.%d.%d\r\n",
SYS_KERNEL_VER_MAJOR(version),
SYS_KERNEL_VER_MINOR(version),
SYS_KERNEL_VER_PATCHLEVEL(version));
}
static int shell_cmd_uptime(int argc, char *argv[])
static void cmd_kernel_uptime(const struct shell *shell,
size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
printk("uptime: %u ms\n", k_uptime_get_32());
return 0;
shell_fprintf(shell, SHELL_NORMAL, "Uptime: %u ms\r\n",
k_uptime_get_32());
}
static int shell_cmd_cycles(int argc, char *argv[])
static void cmd_kernel_cycles(const struct shell *shell,
size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
printk("cycles: %u hw cycles\n", k_cycle_get_32());
return 0;
shell_fprintf(shell, SHELL_NORMAL, "cycles: %u hw cycles\r\n",
k_cycle_get_32());
}
#if defined(CONFIG_OBJECT_TRACING) && defined(CONFIG_THREAD_MONITOR)
static void shell_tdata_dump(const struct k_thread *thread, void *user_data)
{
printk("%s%p: options: 0x%x priority: %d\n",
(thread == k_current_get()) ? "*" : " ",
thread,
thread->base.user_options,
thread->base.prio);
shell_fprintf((const struct shell *)user_data, SHELL_NORMAL,
"%s%p: options: 0x%x priority: %d\r\n",
(thread == k_current_get()) ? "*" : " ",
thread,
thread->base.user_options,
thread->base.prio);
}
static int shell_cmd_threads(int argc, char *argv[])
static void cmd_kernel_threads(const struct shell *shell,
size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
printk("Threads:\n");
k_thread_foreach(shell_tdata_dump, NULL);
return 0;
shell_fprintf(shell, SHELL_NORMAL, "Threads:\r\n");
k_thread_foreach(shell_tdata_dump, (void *)shell);
}
#endif
@ -75,55 +74,72 @@ static int shell_cmd_threads(int argc, char *argv[])
&& defined(CONFIG_THREAD_STACK_INFO)
static void shell_stack_dump(const struct k_thread *thread, void *user_data)
{
stack_analyze((char *)user_data, (char *)thread->stack_info.start,
thread->stack_info.size);
unsigned int pcnt, unused = 0;
unsigned int size = thread->stack_info.size;
unused = stack_unused_space_get((char *)thread->stack_info.start,
size);
/* Calculate the real size reserved for the stack */
pcnt = ((size - unused) * 100) / size;
shell_fprintf((const struct shell *)user_data, SHELL_NORMAL,
"0x%08X (real size %u):\tunused %u\tusage %u / %u (%u %%)\r\n",
(u32_t)thread, size, unused, size - unused, size, pcnt);
}
static int shell_cmd_stack(int argc, char *argv[])
static void cmd_kernel_stacks(const struct shell *shell,
size_t argc, char **argv)
{
k_thread_foreach(shell_stack_dump, "Shell");
return 0;
ARG_UNUSED(argc);
ARG_UNUSED(argv);
k_thread_foreach(shell_stack_dump, (void *)shell);
}
#endif
#if defined(CONFIG_REBOOT)
static int shell_cmd_reboot(int argc, char *argv[])
static void cmd_kernel_reboot_warm(const struct shell *shell,
size_t argc, char **argv)
{
int type;
if (argc != 2) {
return -EINVAL;
}
if (!strcmp(argv[1], "warm")) {
type = SYS_REBOOT_WARM;
} else if (!strcmp(argv[1], "cold")) {
type = SYS_REBOOT_COLD;
} else {
return -EINVAL;
}
sys_reboot(type);
return 0;
ARG_UNUSED(argc);
ARG_UNUSED(argv);
sys_reboot(SYS_REBOOT_WARM);
}
static void cmd_kernel_reboot_cold(const struct shell *shell,
size_t argc, char **argv)
{
ARG_UNUSED(argc);
ARG_UNUSED(argv);
sys_reboot(SYS_REBOOT_COLD);
}
SHELL_CREATE_STATIC_SUBCMD_SET(sub_kernel_reboot)
{
/* Alphabetically sorted. */
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. */
};
#endif
struct shell_cmd kernel_commands[] = {
{ "version", shell_cmd_version, "show kernel version" },
{ "uptime", shell_cmd_uptime, "show system uptime in milliseconds" },
{ "cycles", shell_cmd_cycles, "show system hardware cycles" },
#if defined(CONFIG_OBJECT_TRACING) && defined(CONFIG_THREAD_MONITOR)
{ "threads", shell_cmd_threads, "show running threads" },
SHELL_CREATE_STATIC_SUBCMD_SET(sub_kernel)
{
/* Alphabetically sorted. */
SHELL_CMD(cycles, NULL, "Kernel cycles.", cmd_kernel_cycles),
#if defined(CONFIG_REBOOT)
SHELL_CMD(reboot, &sub_kernel_reboot, "Reboot.", NULL),
#endif
#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_MONITOR) \
&& defined(CONFIG_THREAD_STACK_INFO)
{ "stacks", shell_cmd_stack, "show system stacks" },
SHELL_CMD(stacks, NULL, "List threads stack usage.", cmd_kernel_stacks),
#endif
#if defined(CONFIG_REBOOT)
{ "reboot", shell_cmd_reboot, "<warm cold>" },
#if defined(CONFIG_OBJECT_TRACING) && defined(CONFIG_THREAD_MONITOR)
SHELL_CMD(threads, NULL, "List kernel threads.", cmd_kernel_threads),
#endif
{ NULL, NULL, NULL }
SHELL_CMD(uptime, NULL, "Kernel uptime.", cmd_kernel_uptime),
SHELL_CMD(version, NULL, "Kernel version.", cmd_kernel_version),
SHELL_SUBCMD_SET_END /* Array terminated. */
};
SHELL_REGISTER(SHELL_KERNEL, kernel_commands);
SHELL_CMD_REGISTER(kernel, &sub_kernel, "Kernel commands", NULL);