[gdb] Make execute_command_to_string return string on throw

The pattern for using execute_command_to_string is:
...
  std::string output;
  output = execute_fn_to_string (fn, term_out);
...

This results in a problem when using it in a try/catch:
...
  try
    {
      output = execute_fn_to_string (fn, term_out)
    }
  catch (const gdb_exception &e)
    {
      /* Use output.  */
    }
...

If an expection was thrown during execute_fn_to_string, then the output
remains unassigned, while it could be worthwhile to known what output was
generated by gdb before the expection was thrown.

Fix this by returning the string using a parameter instead:
...
  execute_fn_to_string (output, fn, term_out)
...

Also add a variant without string parameter, to support places where the
function is used while ignoring the result:
...
  execute_fn_to_string (fn, term_out)
...

Tested on x86_64-linux.
This commit is contained in:
Tom de Vries
2021-10-09 18:58:30 +02:00
parent fa9ce2c143
commit 84a6adfd4c
7 changed files with 50 additions and 20 deletions

View File

@@ -91,7 +91,7 @@ test_complaints ()
do \
{ \
std::string output; \
output = execute_fn_to_string ([]() { complaint (STR); }, false); \
execute_fn_to_string (output, []() { complaint (STR); }, false); \
std::string expected \
= _("During symbol reading: ") + std::string (STR "\n"); \
SELF_CHECK (output == expected); \
@@ -102,7 +102,7 @@ test_complaints ()
do \
{ \
std::string output; \
output = execute_fn_to_string ([]() { complaint (STR); }, false); \
execute_fn_to_string (output, []() { complaint (STR); }, false); \
SELF_CHECK (output.empty ()); \
SELF_CHECK (counters[STR] == CNT); \
} while (0)