gdb: PR 33384 invalid disassembler option message

This is the gdb part of fixing PR33384, where it is noted that an
error in a disassembler option prints the rest of the comma separated
option string rather than just the option in error.

Removing FOR_EACH_DISASSEMBLER_OPTION seemed a good idea to me, as we
then expose the strchr there which is useful in zero terminating the
option, and in the case of arm-tdep.c, to replace strcspn.  Also, if
the option is zero terminated we don't need disassembler_options_cmp.

Alternatively, you could do similarly to arm-tdep.c in disasm.c by
changing the error message to use %.*s with a length found by strcspn.
I rejected that smaller patch on the grounds that it makes for churn
in message translation.  I also prefer to see code using the standard
string functions.

Regression tested on x86_64-linux.  Message behaviour tested on
powerpc64le-linux and arm-linux-eabi.

	* arm-tdep.c (show_disassembly_style_sfunc): Don't use
	FOR_EACH_DISASSEMBLER_OPTION.  Use strchr needed for loop
	control to size option len.
	* disasm.c (set_disassembler_options): Don't use
	FOR_EACH_DISASSEMBLER_OPTION.  Overwrite comma in options with
	a zero.  Replace disassembler_options_cmp with strcmp.
This commit is contained in:
Alan Modra
2025-10-04 08:44:58 +09:30
parent 3fc9616817
commit 58a722afdb
2 changed files with 23 additions and 10 deletions

View File

@@ -9641,13 +9641,20 @@ show_disassembly_style_sfunc (struct ui_file *file, int from_tty,
const char *options = get_disassembler_options (gdbarch);
const char *style = "";
int len = 0;
const char *opt;
const char *opt = options;
FOR_EACH_DISASSEMBLER_OPTION (opt, options)
if (startswith (opt, "reg-names-"))
if (opt)
while (1)
{
style = &opt[strlen ("reg-names-")];
len = strcspn (style, ",");
const char *opt_end = strchr (opt, ',');
if (startswith (opt, "reg-names-"))
{
style = &opt[strlen ("reg-names-")];
len = opt_end ? opt_end - style : 99;
}
if (!opt_end)
break;
opt = opt_end + 1;
}
gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style);

View File

@@ -1269,7 +1269,6 @@ set_disassembler_options (const char *prospective_options)
= make_unique_xstrdup (prospective_options);
char *options = remove_whitespace_and_extra_commas
(prospective_options_local.get ());
const char *opt;
/* Allow all architectures, even ones that do not support 'set disassembler',
to reset their disassembler options to NULL. */
@@ -1291,9 +1290,13 @@ set_disassembler_options (const char *prospective_options)
valid_options = &valid_options_and_args->options;
/* Verify we have valid disassembler options. */
FOR_EACH_DISASSEMBLER_OPTION (opt, options)
char *opt = options;
while (1)
{
size_t i;
char *opt_end = strchr (opt, ',');
if (opt_end)
*opt_end = 0;
for (i = 0; valid_options->name[i] != NULL; i++)
if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
{
@@ -1308,8 +1311,7 @@ set_disassembler_options (const char *prospective_options)
if (valid_options->arg[i]->values == NULL)
break;
for (j = 0; valid_options->arg[i]->values[j] != NULL; j++)
if (disassembler_options_cmp
(arg, valid_options->arg[i]->values[j]) == 0)
if (strcmp (arg, valid_options->arg[i]->values[j]) == 0)
{
found = true;
break;
@@ -1317,7 +1319,7 @@ set_disassembler_options (const char *prospective_options)
if (found)
break;
}
else if (disassembler_options_cmp (opt, valid_options->name[i]) == 0)
else if (strcmp (opt, valid_options->name[i]) == 0)
break;
if (valid_options->name[i] == NULL)
{
@@ -1326,6 +1328,10 @@ set_disassembler_options (const char *prospective_options)
opt);
return;
}
if (!opt_end)
break;
*opt_end = ',';
opt = opt_end + 1;
}
*disassembler_options = options;