Skip to content
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

command: Convert format-string in heapinfo #1681

Merged
merged 1 commit into from
Nov 15, 2023
Merged

command: Convert format-string in heapinfo #1681

merged 1 commit into from
Nov 15, 2023

Conversation

koendv
Copy link
Contributor

@koendv koendv commented Nov 14, 2023

Detailed description

This PR

Your checklist for this pull request

  • I've read the Code of Conduct
  • I've read the guidelines for contributing to this repository
  • It builds for hardware native (make PROBE_HOST=native)
  • It builds as BMDA (make PROBE_HOST=hosted)
  • I've tested it to the best of my ability
  • My commit messages provide a useful short description of what the commits do

Closing issues

Copy link
Member

@dragonmux dragonmux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of relatively small (easily solved) issues we've spotted in review, but otherwise this looks good as a change. We'll be glad to get this merged once the review items have been addressed as amendments to the existing commit (not new commits).

src/command.c Outdated Show resolved Hide resolved
src/command.c Outdated Show resolved Hide resolved
src/command.c Outdated
See newlib/libc/sys/arm/crt0.S "Issue Angel SWI to read stack info"
- if the system crashes increase heap or stack
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop the extra new line so InteliSense and other tools see the comment as documentation for the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.
Every command has a one-line help message stored in cmd->help.
Maybe some bytes could be saved if a command, instead of printing a help message and exiting, could signal back: print the one-line help message for my command and exit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a usage message, but due to space constraints doesn't detail what the command is, it's purpose for existing, etc.. think you meant to reply to the above review item on the "?\n" change though.

Assuming we're right on that, it's something we've considered before but doing that well is going to be complicated - and not just because it changes so many commands across the code base, but because sometimes the message to be printed needs to be contextual. It's a good idea though to explore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of something like:

diff --git a/src/command.c b/src/command.c
index afae27be..d6fbc7ea 100644
--- a/src/command.c
+++ b/src/command.c
@@ -154,8 +154,14 @@ int command_process(target_s *const t, char *const cmd_buffer)
                /* Accept a partial match as GDB does.
                 * So 'mon ver' will match 'monitor version'
                 */
-               if ((argc == 0) || !strncmp(argv[0], cmd->cmd, strlen(argv[0])))
-                       return !cmd->handler(t, argc, argv);
+               if ((argc == 0) || !strncmp(argv[0], cmd->cmd, strlen(argv[0]))) {
+                       bool retval = cmd->handler(t, argc, argv);
+                       if (retval == 2) {
+                               gdb_out(cmd->help);
+                               return 0;
+                       }
+                       return !retval;
+               }
        }
 
        if (!t)

so a command could do a return 2 to ask for the help message to be printed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could work for a simple version of the implementation - though the parens around the argc check need dropping and you'd need to use something other than bool for retval as bool is not allowed to have an inspectable numerical value other than 0 and not-0 w/ not-0 being coerced to 1 when converted back to integer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, but this will require changing the bool (*cmd_handler_fn)(target_s *target, int argc, const char **argv) function-signature and consequently all the commands just so they can return 2. I'm more inclined towards using identical message strings. See PR1522 for an example of gcc/ld.bfd folding many identical constant string literals.

@dragonmux dragonmux added this to the v2.0 release milestone Nov 14, 2023
@dragonmux dragonmux added Bug Confirmed bug Documentation Project documentation Enhancement General project improvement labels Nov 14, 2023
src/command.c Outdated Show resolved Hide resolved
src/command.c Outdated
stack_base, stack_limit);
gdb_outf("heap_base: %08" PRIx32 " heap_limit: %08" PRIx32 " stack_base: %08" PRIx32 " stack_limit: %08" PRIx32
"\n",
heap_base, heap_limit, stack_base, stack_limit);
target_set_heapinfo(t, heap_base, heap_limit, stack_base, stack_limit);
} else
gdb_outf("heapinfo heap_base heap_limit stack_base stack_limit\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
gdb_outf("heapinfo heap_base heap_limit stack_base stack_limit\n");
gdb_outf("%s\n", "Set semihosting heapinfo: HEAP_BASE HEAP_LIMIT STACK_BASE STACK_LIMIT");

Such that we reuse the Usage/help message but still can stick a newline after it. Maybe even "Usage: %s\n".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such that we reuse the Usage/help message but still can stick a newline after it.

That's a good one. Maybe even pass the help string to the command?

--- a/src/command.c
+++ b/src/command.c
@@ -155,7 +155,7 @@ int command_process(target_s *const t, char *const cmd_buffer)
                 * So 'mon ver' will match 'monitor version'
                 */
                if ((argc == 0) || !strncmp(argv[0], cmd->cmd, strlen(argv[0])))
-                       return !cmd->handler(t, argc, argv);
+                       return !cmd->handler(t, argc, argv, cmd->help);
        }
 
        if (!t)

Copy link
Member

@dragonmux dragonmux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current patch as it stands looks good to us - addressing all the review points. We're going to merge it as it stands to clear the way for #1676.

Further improvements to the command system to allow either the commands to take their help strings as a parameter, or for them to return if they want their usage displayed can be worked out in a separate PR that addresses the problem across all commands. Until a particular direction is agreed, may we suggest making an issue ticket for that so we can put all the conversation for it in one spot?

@dragonmux dragonmux merged commit 529c4a4 into blackmagic-debug:main Nov 15, 2023
6 checks passed
@koendv koendv deleted the heapinfo branch November 15, 2023 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Confirmed bug Documentation Project documentation Enhancement General project improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants