Skip to content

Commit 4db7ad9

Browse files
nordic-krchnashif
authored andcommitted
shell: modules: Adapt kernel commands to new shell
Kernel commands ported to new shell. Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent 8feab48 commit 4db7ad9

File tree

2 files changed

+79
-62
lines changed

2 files changed

+79
-62
lines changed

subsys/shell/Kconfig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ config CONSOLE_SHELL_MAX_CMD_QUEUED
3131
help
3232
Maximum size of the queue for input commands.
3333

34-
source "subsys/shell/modules/Kconfig"
3534

3635
endif
3736

@@ -175,6 +174,8 @@ config SHELL_LOG_BACKEND
175174
default y if LOG
176175
default n if !LOG
177176

177+
source "subsys/shell/modules/Kconfig"
178+
178179
endif #SHELL
179180
endmenu
180181

subsys/shell/modules/kernel_service.c

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,71 @@
11
/*
2+
* Copyright (c) 2018 Nordic Semiconductor ASA
23
* Copyright (c) 2016 Intel Corporation
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
67

78
#include <misc/printk.h>
8-
#include <shell/legacy_shell.h>
9+
#include <shell/shell.h>
910
#include <init.h>
1011
#include <debug/object_tracing.h>
1112
#include <misc/reboot.h>
1213
#include <misc/stack.h>
1314
#include <string.h>
1415

15-
#define SHELL_KERNEL "kernel"
16-
17-
static int shell_cmd_version(int argc, char *argv[])
16+
static void cmd_kernel_version(const struct shell *shell,
17+
size_t argc, char **argv)
1818
{
1919
u32_t version = sys_kernel_version_get();
2020

2121
ARG_UNUSED(argc);
2222
ARG_UNUSED(argv);
2323

24-
printk("Zephyr version %d.%d.%d\n",
25-
SYS_KERNEL_VER_MAJOR(version),
26-
SYS_KERNEL_VER_MINOR(version),
27-
SYS_KERNEL_VER_PATCHLEVEL(version));
28-
return 0;
24+
shell_fprintf(shell, SHELL_NORMAL, "Zephyr version %d.%d.%d\r\n",
25+
SYS_KERNEL_VER_MAJOR(version),
26+
SYS_KERNEL_VER_MINOR(version),
27+
SYS_KERNEL_VER_PATCHLEVEL(version));
2928
}
3029

31-
static int shell_cmd_uptime(int argc, char *argv[])
30+
static void cmd_kernel_uptime(const struct shell *shell,
31+
size_t argc, char **argv)
3232
{
3333
ARG_UNUSED(argc);
3434
ARG_UNUSED(argv);
3535

36-
printk("uptime: %u ms\n", k_uptime_get_32());
37-
38-
return 0;
36+
shell_fprintf(shell, SHELL_NORMAL, "Uptime: %u ms\r\n",
37+
k_uptime_get_32());
3938
}
4039

41-
static int shell_cmd_cycles(int argc, char *argv[])
40+
static void cmd_kernel_cycles(const struct shell *shell,
41+
size_t argc, char **argv)
4242
{
4343
ARG_UNUSED(argc);
4444
ARG_UNUSED(argv);
4545

46-
printk("cycles: %u hw cycles\n", k_cycle_get_32());
47-
48-
return 0;
46+
shell_fprintf(shell, SHELL_NORMAL, "cycles: %u hw cycles\r\n",
47+
k_cycle_get_32());
4948
}
5049

5150
#if defined(CONFIG_OBJECT_TRACING) && defined(CONFIG_THREAD_MONITOR)
5251
static void shell_tdata_dump(const struct k_thread *thread, void *user_data)
5352
{
54-
printk("%s%p: options: 0x%x priority: %d\n",
55-
(thread == k_current_get()) ? "*" : " ",
56-
thread,
57-
thread->base.user_options,
58-
thread->base.prio);
53+
shell_fprintf((const struct shell *)user_data, SHELL_NORMAL,
54+
"%s%p: options: 0x%x priority: %d\r\n",
55+
(thread == k_current_get()) ? "*" : " ",
56+
thread,
57+
thread->base.user_options,
58+
thread->base.prio);
5959
}
6060

61-
static int shell_cmd_threads(int argc, char *argv[])
61+
static void cmd_kernel_threads(const struct shell *shell,
62+
size_t argc, char **argv)
6263
{
6364
ARG_UNUSED(argc);
6465
ARG_UNUSED(argv);
6566

66-
printk("Threads:\n");
67-
k_thread_foreach(shell_tdata_dump, NULL);
68-
69-
return 0;
67+
shell_fprintf(shell, SHELL_NORMAL, "Threads:\r\n");
68+
k_thread_foreach(shell_tdata_dump, (void *)shell);
7069
}
7170
#endif
7271

@@ -75,55 +74,72 @@ static int shell_cmd_threads(int argc, char *argv[])
7574
&& defined(CONFIG_THREAD_STACK_INFO)
7675
static void shell_stack_dump(const struct k_thread *thread, void *user_data)
7776
{
78-
stack_analyze((char *)user_data, (char *)thread->stack_info.start,
79-
thread->stack_info.size);
77+
unsigned int pcnt, unused = 0;
78+
unsigned int size = thread->stack_info.size;
79+
80+
unused = stack_unused_space_get((char *)thread->stack_info.start,
81+
size);
82+
83+
/* Calculate the real size reserved for the stack */
84+
pcnt = ((size - unused) * 100) / size;
85+
86+
shell_fprintf((const struct shell *)user_data, SHELL_NORMAL,
87+
"0x%08X (real size %u):\tunused %u\tusage %u / %u (%u %%)\r\n",
88+
(u32_t)thread, size, unused, size - unused, size, pcnt);
8089
}
8190

82-
static int shell_cmd_stack(int argc, char *argv[])
91+
static void cmd_kernel_stacks(const struct shell *shell,
92+
size_t argc, char **argv)
8393
{
84-
k_thread_foreach(shell_stack_dump, "Shell");
85-
return 0;
94+
ARG_UNUSED(argc);
95+
ARG_UNUSED(argv);
96+
k_thread_foreach(shell_stack_dump, (void *)shell);
8697
}
8798
#endif
8899

89100
#if defined(CONFIG_REBOOT)
90-
static int shell_cmd_reboot(int argc, char *argv[])
101+
static void cmd_kernel_reboot_warm(const struct shell *shell,
102+
size_t argc, char **argv)
91103
{
92-
int type;
93-
94-
if (argc != 2) {
95-
return -EINVAL;
96-
}
97-
98-
if (!strcmp(argv[1], "warm")) {
99-
type = SYS_REBOOT_WARM;
100-
} else if (!strcmp(argv[1], "cold")) {
101-
type = SYS_REBOOT_COLD;
102-
} else {
103-
return -EINVAL;
104-
}
105-
106-
sys_reboot(type);
107-
return 0;
104+
ARG_UNUSED(argc);
105+
ARG_UNUSED(argv);
106+
sys_reboot(SYS_REBOOT_WARM);
108107
}
108+
109+
static void cmd_kernel_reboot_cold(const struct shell *shell,
110+
size_t argc, char **argv)
111+
{
112+
ARG_UNUSED(argc);
113+
ARG_UNUSED(argv);
114+
sys_reboot(SYS_REBOOT_COLD);
115+
}
116+
117+
SHELL_CREATE_STATIC_SUBCMD_SET(sub_kernel_reboot)
118+
{
119+
/* Alphabetically sorted. */
120+
SHELL_CMD(cold, NULL, "Cold reboot.", cmd_kernel_reboot_cold),
121+
SHELL_CMD(warm, NULL, "Warm reboot.", cmd_kernel_reboot_warm),
122+
SHELL_SUBCMD_SET_END /* Array terminated. */
123+
};
109124
#endif
110125

111-
struct shell_cmd kernel_commands[] = {
112-
{ "version", shell_cmd_version, "show kernel version" },
113-
{ "uptime", shell_cmd_uptime, "show system uptime in milliseconds" },
114-
{ "cycles", shell_cmd_cycles, "show system hardware cycles" },
115-
#if defined(CONFIG_OBJECT_TRACING) && defined(CONFIG_THREAD_MONITOR)
116-
{ "threads", shell_cmd_threads, "show running threads" },
126+
SHELL_CREATE_STATIC_SUBCMD_SET(sub_kernel)
127+
{
128+
/* Alphabetically sorted. */
129+
SHELL_CMD(cycles, NULL, "Kernel cycles.", cmd_kernel_cycles),
130+
#if defined(CONFIG_REBOOT)
131+
SHELL_CMD(reboot, &sub_kernel_reboot, "Reboot.", NULL),
117132
#endif
118133
#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_MONITOR) \
119134
&& defined(CONFIG_THREAD_STACK_INFO)
120-
{ "stacks", shell_cmd_stack, "show system stacks" },
135+
SHELL_CMD(stacks, NULL, "List threads stack usage.", cmd_kernel_stacks),
121136
#endif
122-
#if defined(CONFIG_REBOOT)
123-
{ "reboot", shell_cmd_reboot, "<warm cold>" },
137+
#if defined(CONFIG_OBJECT_TRACING) && defined(CONFIG_THREAD_MONITOR)
138+
SHELL_CMD(threads, NULL, "List kernel threads.", cmd_kernel_threads),
124139
#endif
125-
{ NULL, NULL, NULL }
140+
SHELL_CMD(uptime, NULL, "Kernel uptime.", cmd_kernel_uptime),
141+
SHELL_CMD(version, NULL, "Kernel version.", cmd_kernel_version),
142+
SHELL_SUBCMD_SET_END /* Array terminated. */
126143
};
127144

128-
129-
SHELL_REGISTER(SHELL_KERNEL, kernel_commands);
145+
SHELL_CMD_REGISTER(kernel, &sub_kernel, "Kernel commands", NULL);

0 commit comments

Comments
 (0)