Skip to content

Commit 8f647ef

Browse files
sswetha18kartben
authored andcommitted
[shell]: Add thread control features through shell
This commit extends the shell command functionality by adding three new operations for thread management. kernel thread suspend <thread_id>: Suspends any thread based on its identifier. kernel thread resume <thread_id>: Resumes the thread that was previously suspended. kernel thread kill <thread_id>: Terminates any thread based on its identifier These extended commands are useful for controlling any threads through Zephyr Shell. Signed-off-by: S Swetha <s.swetha@intel.com>
1 parent f551b2d commit 8f647ef

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

subsys/shell/modules/kernel_service/thread/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ zephyr_sources_ifdef(CONFIG_KERNEL_THREAD_SHELL_MASK pin.c)
1616
zephyr_sources_ifdef(CONFIG_KERNEL_THREAD_SHELL_STACKS stacks.c)
1717

1818
zephyr_sources_ifdef(CONFIG_KERNEL_THREAD_SHELL_UNWIND unwind.c)
19+
20+
zephyr_sources_ifdef(CONFIG_KERNEL_THREAD_SHELL_SUSPEND suspend.c)
21+
22+
zephyr_sources_ifdef(CONFIG_KERNEL_THREAD_SHELL_RESUME resume.c)
23+
24+
zephyr_sources_ifdef(CONFIG_KERNEL_THREAD_SHELL_KILL kill.c)

subsys/shell/modules/kernel_service/thread/Kconfig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,24 @@ config KERNEL_THREAD_SHELL_UNWIND
5252
select KERNEL_THREAD_SHELL
5353
help
5454
Internal helper macro to compile the `unwind` subcommand
55+
56+
config KERNEL_THREAD_SHELL_SUSPEND
57+
bool
58+
default y
59+
select KERNEL_THREAD_SHELL
60+
help
61+
Internal helper macro to compile the 'suspend' subcommand
62+
63+
config KERNEL_THREAD_SHELL_RESUME
64+
bool
65+
default y
66+
select KERNEL_THREAD_SHELL
67+
help
68+
Internal helper macro to compile the 'resume' subcommand
69+
70+
config KERNEL_THREAD_SHELL_KILL
71+
bool
72+
default y
73+
select KERNEL_THREAD_SHELL
74+
help
75+
Internal helper macro to compile the 'kill' subcommad
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "kernel_shell.h"
8+
9+
#include <kernel_internal.h>
10+
#include <zephyr/kernel.h>
11+
#include <stdint.h>
12+
#include <stdlib.h>
13+
14+
static int cmd_kernel_thread_kill(const struct shell *sh, size_t argc, char **argv)
15+
{
16+
/* thread_id is converetd from hex to decimal */
17+
k_tid_t thread_id = (k_tid_t)strtoul(argv[1], NULL, 16);
18+
19+
if (!z_thread_is_valid(thread_id)) {
20+
shell_error(sh, "Thread ID %p is not valid", thread_id);
21+
return -EINVAL;
22+
}
23+
24+
/*Check if the thread ID is the shell thread */
25+
if (thread_id == k_current_get()) {
26+
shell_error(sh, "Error:Shell thread cannot be killed");
27+
return -EINVAL;
28+
}
29+
30+
k_thread_abort(thread_id);
31+
32+
shell_print(sh, "\n Thread %p killed", thread_id);
33+
34+
return 0;
35+
}
36+
37+
KERNEL_THREAD_CMD_ARG_ADD(kill, NULL, "kernel thread kill <thread_id>", cmd_kernel_thread_kill, 2,
38+
0);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "kernel_shell.h"
8+
9+
#include <kernel_internal.h>
10+
#include <zephyr/kernel.h>
11+
#include <stdint.h>
12+
#include <stdlib.h>
13+
14+
static int cmd_kernel_thread_resume(const struct shell *sh, size_t argc, char **argv)
15+
{
16+
/* thread_id is converetd from hex to decimal */
17+
k_tid_t thread_id = (k_tid_t)strtoul(argv[1], NULL, 16);
18+
19+
if (!z_thread_is_valid(thread_id)) {
20+
shell_error(sh, "Thread ID %p is not valid", thread_id);
21+
return -EINVAL;
22+
}
23+
24+
/*Check if the thread ID is the shell thread */
25+
if (thread_id == k_current_get()) {
26+
shell_error(sh, "Error:Shell thread cannot be resumed");
27+
return -EINVAL;
28+
}
29+
30+
k_thread_resume(thread_id);
31+
32+
shell_print(sh, "Thread %p resumed", thread_id);
33+
34+
return 0;
35+
}
36+
37+
KERNEL_THREAD_CMD_ARG_ADD(resume, NULL, "kernel thread resume <thread_id>",
38+
cmd_kernel_thread_resume, 2, 0);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2025 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "kernel_shell.h"
8+
9+
#include <kernel_internal.h>
10+
#include <zephyr/kernel.h>
11+
#include <stdint.h>
12+
#include <stdlib.h>
13+
14+
static int cmd_kernel_thread_suspend(const struct shell *sh, size_t argc, char **argv)
15+
{
16+
/* thread_id is converetd from hex to decimal */
17+
k_tid_t thread_id = (k_tid_t)strtoul(argv[1], NULL, 16);
18+
19+
if (!z_thread_is_valid(thread_id)) {
20+
shell_error(sh, "Thread ID %p is not valid", thread_id);
21+
return -EINVAL;
22+
}
23+
24+
/*Check if the thread ID is the shell thread */
25+
if (thread_id == k_current_get()) {
26+
shell_error(sh, "Error:Shell thread cannot be suspended");
27+
return -EINVAL;
28+
}
29+
30+
k_thread_suspend(thread_id);
31+
32+
shell_print(sh, "Thread %p suspended", thread_id);
33+
34+
return 0;
35+
}
36+
37+
KERNEL_THREAD_CMD_ARG_ADD(suspend, NULL, "kernel thread suspend <thread_id>",
38+
cmd_kernel_thread_suspend, 2, 0);

0 commit comments

Comments
 (0)