mirror of
https://github.com/espressif/binutils-gdb.git
synced 2025-06-04 22:15:12 +08:00
[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:
@ -91,7 +91,7 @@ test_complaints ()
|
|||||||
do \
|
do \
|
||||||
{ \
|
{ \
|
||||||
std::string output; \
|
std::string output; \
|
||||||
output = execute_fn_to_string ([]() { complaint (STR); }, false); \
|
execute_fn_to_string (output, []() { complaint (STR); }, false); \
|
||||||
std::string expected \
|
std::string expected \
|
||||||
= _("During symbol reading: ") + std::string (STR "\n"); \
|
= _("During symbol reading: ") + std::string (STR "\n"); \
|
||||||
SELF_CHECK (output == expected); \
|
SELF_CHECK (output == expected); \
|
||||||
@ -102,7 +102,7 @@ test_complaints ()
|
|||||||
do \
|
do \
|
||||||
{ \
|
{ \
|
||||||
std::string output; \
|
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 (output.empty ()); \
|
||||||
SELF_CHECK (counters[STR] == CNT); \
|
SELF_CHECK (counters[STR] == CNT); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
12
gdb/gdbcmd.h
12
gdb/gdbcmd.h
@ -42,7 +42,8 @@ extern void execute_fn_to_ui_file (struct ui_file *file, std::function<void(void
|
|||||||
(e.g. with styling). When TERM_OUT is false raw output will be collected
|
(e.g. with styling). When TERM_OUT is false raw output will be collected
|
||||||
(e.g. no styling). */
|
(e.g. no styling). */
|
||||||
|
|
||||||
extern std::string execute_fn_to_string (std::function<void(void)> fn, bool term_out);
|
extern void execute_fn_to_string (std::string &res,
|
||||||
|
std::function<void(void)> fn, bool term_out);
|
||||||
|
|
||||||
/* As execute_fn_to_ui_file, but run execute_command for P and FROM_TTY. */
|
/* As execute_fn_to_ui_file, but run execute_command for P and FROM_TTY. */
|
||||||
|
|
||||||
@ -51,8 +52,13 @@ extern void execute_command_to_ui_file (struct ui_file *file,
|
|||||||
|
|
||||||
/* As execute_fn_to_string, but run execute_command for P and FROM_TTY. */
|
/* As execute_fn_to_string, but run execute_command for P and FROM_TTY. */
|
||||||
|
|
||||||
extern std::string execute_command_to_string (const char *p, int from_tty,
|
extern void execute_command_to_string (std::string &res, const char *p,
|
||||||
bool term_out);
|
int from_tty, bool term_out);
|
||||||
|
|
||||||
|
/* As execute_command_to_string, but ignore resulting string. */
|
||||||
|
|
||||||
|
extern void execute_command_to_string (const char *p,
|
||||||
|
int from_tty, bool term_out);
|
||||||
|
|
||||||
extern void print_command_line (struct command_line *, unsigned int,
|
extern void print_command_line (struct command_line *, unsigned int,
|
||||||
struct ui_file *);
|
struct ui_file *);
|
||||||
|
@ -302,7 +302,7 @@ gdbscm_execute_gdb_command (SCM command_scm, SCM rest)
|
|||||||
|
|
||||||
scoped_restore preventer = prevent_dont_repeat ();
|
scoped_restore preventer = prevent_dont_repeat ();
|
||||||
if (to_string)
|
if (to_string)
|
||||||
to_string_res = execute_command_to_string (command, from_tty, false);
|
execute_command_to_string (to_string_res, command, from_tty, false);
|
||||||
else
|
else
|
||||||
execute_command (command, from_tty);
|
execute_command (command, from_tty);
|
||||||
|
|
||||||
|
@ -1924,11 +1924,11 @@ namespace selftests {
|
|||||||
static void
|
static void
|
||||||
test_python ()
|
test_python ()
|
||||||
{
|
{
|
||||||
#define CMD execute_command_to_string ("python print(5)", 0, true);
|
#define CMD(S) execute_command_to_string (S, "python print(5)", 0, true)
|
||||||
|
|
||||||
std::string output;
|
std::string output;
|
||||||
|
|
||||||
output = CMD;
|
CMD (output);
|
||||||
SELF_CHECK (output == "5\n");
|
SELF_CHECK (output == "5\n");
|
||||||
output.clear ();
|
output.clear ();
|
||||||
|
|
||||||
@ -1937,7 +1937,7 @@ test_python ()
|
|||||||
= make_scoped_restore (&gdb_python_initialized, 0);
|
= make_scoped_restore (&gdb_python_initialized, 0);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
output = CMD;
|
CMD (output);
|
||||||
}
|
}
|
||||||
catch (const gdb_exception &e)
|
catch (const gdb_exception &e)
|
||||||
{
|
{
|
||||||
|
@ -3028,8 +3028,8 @@ frame_apply_command_count (const char *which_command,
|
|||||||
set to the selected frame. */
|
set to the selected frame. */
|
||||||
scoped_restore_current_thread restore_fi_current_frame;
|
scoped_restore_current_thread restore_fi_current_frame;
|
||||||
|
|
||||||
cmd_result = execute_command_to_string
|
execute_command_to_string
|
||||||
(cmd, from_tty, gdb_stdout->term_out ());
|
(cmd_result, cmd, from_tty, gdb_stdout->term_out ());
|
||||||
}
|
}
|
||||||
fi = get_selected_frame (_("frame apply "
|
fi = get_selected_frame (_("frame apply "
|
||||||
"unable to get selected frame."));
|
"unable to get selected frame."));
|
||||||
|
@ -1450,8 +1450,9 @@ thr_try_catch_cmd (thread_info *thr, const char *cmd, int from_tty,
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::string cmd_result = execute_command_to_string
|
std::string cmd_result;
|
||||||
(cmd, from_tty, gdb_stdout->term_out ());
|
execute_command_to_string
|
||||||
|
(cmd_result, cmd, from_tty, gdb_stdout->term_out ());
|
||||||
if (!flags.silent || cmd_result.length () > 0)
|
if (!flags.silent || cmd_result.length () > 0)
|
||||||
{
|
{
|
||||||
if (!flags.quiet)
|
if (!flags.quiet)
|
||||||
|
37
gdb/top.c
37
gdb/top.c
@ -724,13 +724,25 @@ execute_fn_to_ui_file (struct ui_file *file, std::function<void(void)> fn)
|
|||||||
|
|
||||||
/* See gdbcmd.h. */
|
/* See gdbcmd.h. */
|
||||||
|
|
||||||
std::string
|
void
|
||||||
execute_fn_to_string (std::function<void(void)> fn, bool term_out)
|
execute_fn_to_string (std::string &res, std::function<void(void)> fn,
|
||||||
|
bool term_out)
|
||||||
{
|
{
|
||||||
string_file str_file (term_out);
|
string_file str_file (term_out);
|
||||||
|
|
||||||
execute_fn_to_ui_file (&str_file, fn);
|
try
|
||||||
return std::move (str_file.string ());
|
{
|
||||||
|
execute_fn_to_ui_file (&str_file, fn);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
/* Finally. */
|
||||||
|
res = std::move (str_file.string ());
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* And finally. */
|
||||||
|
res = std::move (str_file.string ());
|
||||||
}
|
}
|
||||||
|
|
||||||
/* See gdbcmd.h. */
|
/* See gdbcmd.h. */
|
||||||
@ -744,12 +756,23 @@ execute_command_to_ui_file (struct ui_file *file,
|
|||||||
|
|
||||||
/* See gdbcmd.h. */
|
/* See gdbcmd.h. */
|
||||||
|
|
||||||
std::string
|
void
|
||||||
|
execute_command_to_string (std::string &res, const char *p, int from_tty,
|
||||||
|
bool term_out)
|
||||||
|
{
|
||||||
|
execute_fn_to_string (res, [=]() { execute_command (p, from_tty); },
|
||||||
|
term_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* See gdbcmd.h. */
|
||||||
|
|
||||||
|
void
|
||||||
execute_command_to_string (const char *p, int from_tty,
|
execute_command_to_string (const char *p, int from_tty,
|
||||||
bool term_out)
|
bool term_out)
|
||||||
{
|
{
|
||||||
return
|
std::string dummy;
|
||||||
execute_fn_to_string ([=]() { execute_command (p, from_tty); }, term_out);
|
execute_fn_to_string (dummy, [=]() { execute_command (p, from_tty); },
|
||||||
|
term_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* When nonzero, cause dont_repeat to do nothing. This should only be
|
/* When nonzero, cause dont_repeat to do nothing. This should only be
|
||||||
|
Reference in New Issue
Block a user