Skip to content

Shell commands and modules added during link time #3424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kasjer
Copy link
Contributor

@kasjer kasjer commented Jun 1, 2025

Shell commands registration

So far shell commands could be added with shell_cmd_register() and modules (set of commands) could be added with shell_register()

This approach requires function call usually added in package initialization function.
Registering commands requires some RAM two tables:

static struct shell_module shell_modules[MYNEWT_VAL(SHELL_MAX_MODULES)];
static struct shell_cmd compat_commands[MYNEWT_VAL(SHELL_MAX_COMPAT_COMMANDS) + 1];

Number of modules that can be registered is defined by two mynewt values SHELL_MAX_MODULES and SHELL_MAX_COMPAT_COMMANDS.
When code tries to add more commands or modules system will crash with assert(0).

Link time table for commands registration

Now when link time table can be used shell commands and modules can be added to be available in shell.
Commands can be added with macro MAKE_SHELL_CMD() and modules can be added using SHELL_MODULE_WITH_TABLE().
Both register commands and modules does not use RAM and do not need function calls.

For backward compatibility shell_cmd_register() and shell_register() can still be used.
To minimize memory usage syscfg values SHELL_MAX_MODULES and SHELL_MAX_COMPAT_COMMANDS can be set to 0.

Benefits of new shell commands registration

  • Memory usage is reduced both RAM and flash
  • commands are in alphabetical order (they were displayed in order they were registered)
  • Targets don't need to specify maximum number of command and modules so adding some package will not result in unexpected assert(0) that instruct you to increase limits.

Scope of this PR

  • Macros for module and commands creation
  • Rework of shell command handling to use link time tables
  • Update of many packages (not all yet) to use new way of making shell commands

Comparison of size for target that with many commands

Code without this changes

objsize
   text	   data	    bss	    dec	    hex	filename
 121368	   1744	  30572	 153684	  25854	bin/targets/blackpill411ce-slinky/app/@apache-mynewt-core/apps/slinky/slinky.elf

Same application after change are applied:

objsize
   text	   data	    bss	    dec	    hex	filename
 120844	   1496	  29988	 152328	  25308	bin/targets/blackpill411ce-slinky/app/@apache-mynewt-core/apps/slinky/slinky.elf

@github-actions github-actions bot added USB All USB work should have this label STM STM32 related size/xl labels Jun 1, 2025
@kasjer kasjer force-pushed the kasjer/shell-static-command-registration branch 4 times, most recently from 3ded33b to 8396589 Compare June 5, 2025 18:14
LINK_TABLE_ELEMENT_SECTION macro that defines attribute
for symbol did not have 'used' part that is needed
for link table to collect unreferenced symbols

Signed-off-by: Jerzy Kasenberg <[email protected]>
@kasjer kasjer force-pushed the kasjer/shell-static-command-registration branch 2 times, most recently from b16cb07 to 174ab8f Compare June 27, 2025 21:10
@github-actions github-actions bot added size/l and removed USB All USB work should have this label STM STM32 related size/xl labels Jun 27, 2025
@kasjer kasjer force-pushed the kasjer/shell-static-command-registration branch 2 times, most recently from 52c29b3 to 6a7ed2d Compare June 27, 2025 21:38
@kasjer kasjer marked this pull request as ready for review June 28, 2025 08:33
@kasjer
Copy link
Contributor Author

kasjer commented Jun 28, 2025

@rymanluk, PR is stripped down now to shell changes only and two examples that shows how os commands tasks, mpool, reset... are converted and module prompt.
Changes to many other commands that are in mynewt-core will follow once basics are accepted.

@kasjer kasjer force-pushed the kasjer/shell-static-command-registration branch from 6a7ed2d to 04c4d67 Compare June 30, 2025 09:44
kasjer added 3 commits June 30, 2025 12:32
Shell commands and modules used to be registered at
run time by packages. This required code and RAM space.
Both shell modules and commands required RAM of some fixed
size (specified in syscfg). When more commands or shell
modules were registered assertion failed at run time.

Now it's possibly to create shell commands that will be
'registered' at link time and will not require RAM
nor explicit function call.
This should eliminate problem of registering to
many modules or commands.

Signed-off-by: Jerzy Kasenberg <[email protected]>
Shell commands are now registered at link time
instead of compile time to compat module commands.

Signed-off-by: Jerzy Kasenberg <[email protected]>
Module prompt is not linked statically.
No need for runtime registering.

Signed-off-by: Jerzy Kasenberg <[email protected]>
@kasjer kasjer force-pushed the kasjer/shell-static-command-registration branch from 04c4d67 to 6cfae3d Compare June 30, 2025 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants