aboutsummaryrefslogtreecommitdiff
path: root/gdb/cli
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/cli')
-rw-r--r--gdb/cli/cli-cmds.c190
-rw-r--r--gdb/cli/cli-cmds.h21
-rw-r--r--gdb/cli/cli-decode.c41
-rw-r--r--gdb/cli/cli-decode.h17
-rw-r--r--gdb/cli/cli-dump.c118
-rw-r--r--gdb/cli/cli-script.c108
6 files changed, 195 insertions, 300 deletions
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 0930342..d3ec4ae 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -168,6 +168,10 @@ struct cmd_list_element *maintenanceinfolist;
struct cmd_list_element *maintenanceprintlist;
+/* Chain containing all defined "maintenance check" subcommands. */
+
+struct cmd_list_element *maintenancechecklist;
+
struct cmd_list_element *setprintlist;
struct cmd_list_element *showprintlist;
@@ -238,6 +242,7 @@ help_command (char *command, int from_tty)
help_cmd (command, gdb_stdout);
}
+
/* Note: The "complete" command is used by Emacs to implement completion.
[Is that why this function writes output with *_unfiltered?] */
@@ -245,9 +250,6 @@ static void
complete_command (char *arg_entry, int from_tty)
{
const char *arg = arg_entry;
- int argpoint;
- char *arg_prefix;
- VEC (char_ptr) *completions;
dont_repeat ();
@@ -265,57 +267,66 @@ complete_command (char *arg_entry, int from_tty)
if (arg == NULL)
arg = "";
- argpoint = strlen (arg);
-
- /* complete_line assumes that its first argument is somewhere
- within, and except for filenames at the beginning of, the word to
- be completed. The following crude imitation of readline's
- word-breaking tries to accomodate this. */
- const char *point = arg + argpoint;
- while (point > arg)
- {
- if (strchr (rl_completer_word_break_characters, point[-1]) != 0)
- break;
- point--;
- }
- arg_prefix = (char *) alloca (point - arg + 1);
- memcpy (arg_prefix, arg, point - arg);
- arg_prefix[point - arg] = 0;
+ completion_tracker tracker_handle_brkchars;
+ completion_tracker tracker_handle_completions;
+ completion_tracker *tracker;
- completions = complete_line (point, arg, argpoint);
+ int quote_char = '\0';
+ const char *word;
- if (completions)
+ TRY
{
- int ix, size = VEC_length (char_ptr, completions);
- char *item, *prev = NULL;
+ word = completion_find_completion_word (tracker_handle_brkchars,
+ arg, &quote_char);
+
+ /* Completers that provide a custom word point in the
+ handle_brkchars phase also compute their completions then.
+ Completers that leave the completion word handling to readline
+ must be called twice. */
+ if (tracker_handle_brkchars.use_custom_word_point ())
+ tracker = &tracker_handle_brkchars;
+ else
+ {
+ complete_line (tracker_handle_completions, word, arg, strlen (arg));
+ tracker = &tracker_handle_completions;
+ }
+ }
+ CATCH (ex, RETURN_MASK_ALL)
+ {
+ return;
+ }
+
+ std::string arg_prefix (arg, word - arg);
- qsort (VEC_address (char_ptr, completions), size,
- sizeof (char *), compare_strings);
+ completion_result result
+ = tracker->build_completion_result (word, word - arg, strlen (arg));
- /* We do extra processing here since we only want to print each
- unique item once. */
- for (ix = 0; VEC_iterate (char_ptr, completions, ix, item); ++ix)
+ if (result.number_matches != 0)
+ {
+ if (result.number_matches == 1)
+ printf_unfiltered ("%s%s\n", arg_prefix.c_str (), result.match_list[0]);
+ else
{
- if (prev == NULL || strcmp (item, prev) != 0)
+ result.sort_match_list ();
+
+ for (size_t i = 0; i < result.number_matches; i++)
{
- printf_unfiltered ("%s%s\n", arg_prefix, item);
- xfree (prev);
- prev = item;
+ printf_unfiltered ("%s%s",
+ arg_prefix.c_str (),
+ result.match_list[i + 1]);
+ if (quote_char)
+ printf_unfiltered ("%c", quote_char);
+ printf_unfiltered ("\n");
}
- else
- xfree (item);
}
- xfree (prev);
- VEC_free (char_ptr, completions);
-
- if (size == max_completions)
+ if (result.number_matches == max_completions)
{
- /* ARG_PREFIX and POINT are included in the output so that emacs
+ /* ARG_PREFIX and WORD are included in the output so that emacs
will include the message in the output. */
printf_unfiltered (_("%s%s %s\n"),
- arg_prefix, point,
+ arg_prefix.c_str (), word,
get_max_completions_reached_message ());
}
}
@@ -495,57 +506,47 @@ show_script_ext_mode (struct ui_file *file, int from_tty,
/* Try to open SCRIPT_FILE.
If successful, the full path name is stored in *FULL_PATHP,
- the stream is stored in *STREAMP, and return 1.
- The caller is responsible for freeing *FULL_PATHP.
- If not successful, return 0; errno is set for the last file
+ and the stream is returned.
+ If not successful, return NULL; errno is set for the last file
we tried to open.
If SEARCH_PATH is non-zero, and the file isn't found in cwd,
search for it in the source search path. */
-int
-find_and_open_script (const char *script_file, int search_path,
- FILE **streamp, char **full_pathp)
+gdb::optional<open_script>
+find_and_open_script (const char *script_file, int search_path)
{
- char *file;
int fd;
- struct cleanup *old_cleanups;
int search_flags = OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH;
+ gdb::optional<open_script> opened;
- file = tilde_expand (script_file);
- old_cleanups = make_cleanup (xfree, file);
+ gdb::unique_xmalloc_ptr<char> file (tilde_expand (script_file));
if (search_path)
search_flags |= OPF_SEARCH_IN_PATH;
/* Search for and open 'file' on the search path used for source
files. Put the full location in *FULL_PATHP. */
+ char *temp_path;
fd = openp (source_path, search_flags,
- file, O_RDONLY, full_pathp);
+ file.get (), O_RDONLY, &temp_path);
+ gdb::unique_xmalloc_ptr<char> full_path (temp_path);
if (fd == -1)
- {
- int save_errno = errno;
- do_cleanups (old_cleanups);
- errno = save_errno;
- return 0;
- }
-
- do_cleanups (old_cleanups);
+ return opened;
- *streamp = fdopen (fd, FOPEN_RT);
- if (*streamp == NULL)
+ FILE *result = fdopen (fd, FOPEN_RT);
+ if (result == NULL)
{
int save_errno = errno;
close (fd);
- if (full_pathp)
- xfree (*full_pathp);
errno = save_errno;
- return 0;
}
+ else
+ opened.emplace (gdb_file_up (result), std::move (full_path));
- return 1;
+ return opened;
}
/* Load script FILE, which has already been opened as STREAM.
@@ -596,14 +597,12 @@ source_script_from_stream (FILE *stream, const char *file,
static void
source_script_with_search (const char *file, int from_tty, int search_path)
{
- FILE *stream;
- char *full_path;
- struct cleanup *old_cleanups;
if (file == NULL || *file == 0)
error (_("source command requires file name of file to source."));
- if (!find_and_open_script (file, search_path, &stream, &full_path))
+ gdb::optional<open_script> opened = find_and_open_script (file, search_path);
+ if (!opened)
{
/* The script wasn't found, or was otherwise inaccessible.
If the source command was invoked interactively, throw an
@@ -618,15 +617,13 @@ source_script_with_search (const char *file, int from_tty, int search_path)
}
}
- old_cleanups = make_cleanup (xfree, full_path);
- make_cleanup_fclose (stream);
/* The python support reopens the file, so we need to pass full_path here
in case the file was found on the search path. It's useful to do this
anyway so that error messages show the actual file used. But only do
this if we (may have) used search_path, as printing the full path in
errors for the non-search case can be more noise than signal. */
- source_script_from_stream (stream, file, search_path ? full_path : file);
- do_cleanups (old_cleanups);
+ source_script_from_stream (opened->stream.get (), file,
+ search_path ? opened->full_path.get () : file);
}
/* Wrapper around source_script_with_search to export it to main.c
@@ -638,26 +635,14 @@ source_script (const char *file, int from_tty)
source_script_with_search (file, from_tty, 0);
}
-/* Return the source_verbose global variable to its previous state
- on exit from the source command, by whatever means. */
-static void
-source_verbose_cleanup (void *old_value)
-{
- source_verbose = *(int *)old_value;
- xfree (old_value);
-}
-
static void
source_command (char *args, int from_tty)
{
- struct cleanup *old_cleanups;
char *file = args;
int *old_source_verbose = XNEW (int);
int search_path = 0;
- *old_source_verbose = source_verbose;
- old_cleanups = make_cleanup (source_verbose_cleanup,
- old_source_verbose);
+ scoped_restore save_source_verbose = make_scoped_restore (&source_verbose);
/* -v causes the source command to run in verbose mode.
-s causes the file to be searched in the source search path,
@@ -698,8 +683,6 @@ source_command (char *args, int from_tty)
}
source_script_with_search (file, from_tty, search_path);
-
- do_cleanups (old_cleanups);
}
@@ -1407,31 +1390,27 @@ alias_command (char *args, int from_tty)
{
int i, alias_argc, command_argc;
int abbrev_flag = 0;
- char *args2, *equals;
+ char *equals;
const char *alias, *command;
- char **alias_argv, **command_argv;
- struct cleanup *cleanup;
if (args == NULL || strchr (args, '=') == NULL)
alias_usage_error ();
- args2 = xstrdup (args);
- cleanup = make_cleanup (xfree, args2);
- equals = strchr (args2, '=');
- *equals = '\0';
- alias_argv = gdb_buildargv (args2);
- make_cleanup_freeargv (alias_argv);
- command_argv = gdb_buildargv (equals + 1);
- make_cleanup_freeargv (command_argv);
+ equals = strchr (args, '=');
+ std::string args2 (args, equals - args);
+
+ gdb_argv built_alias_argv (args2.c_str ());
+ gdb_argv command_argv (equals + 1);
- for (i = 0; alias_argv[i] != NULL; )
+ char **alias_argv = built_alias_argv.get ();
+ while (alias_argv[0] != NULL)
{
- if (strcmp (alias_argv[i], "-a") == 0)
+ if (strcmp (alias_argv[0], "-a") == 0)
{
++alias_argv;
abbrev_flag = 1;
}
- else if (strcmp (alias_argv[i], "--") == 0)
+ else if (strcmp (alias_argv[0], "--") == 0)
{
++alias_argv;
break;
@@ -1456,12 +1435,13 @@ alias_command (char *args, int from_tty)
}
alias_argc = countargv (alias_argv);
- command_argc = countargv (command_argv);
+ command_argc = command_argv.count ();
/* COMMAND must exist.
Reconstruct the command to remove any extraneous spaces,
for better error messages. */
- std::string command_string (argv_to_string (command_argv, command_argc));
+ std::string command_string (argv_to_string (command_argv.get (),
+ command_argc));
command = command_string.c_str ();
if (! valid_command_p (command))
error (_("Invalid command to alias to: %s"), command);
@@ -1518,8 +1498,6 @@ alias_command (char *args, int from_tty)
command_argv[command_argc - 1],
class_alias, abbrev_flag, c_command->prefixlist);
}
-
- do_cleanups (cleanup);
}
/* Print a list of files and line numbers which a user may choose from
diff --git a/gdb/cli/cli-cmds.h b/gdb/cli/cli-cmds.h
index 7ff1fca..1122a97 100644
--- a/gdb/cli/cli-cmds.h
+++ b/gdb/cli/cli-cmds.h
@@ -17,6 +17,9 @@
#if !defined (CLI_CMDS_H)
#define CLI_CMDS_H 1
+#include "common/filestuff.h"
+#include "common/gdb_optional.h"
+
/* Chain containing all defined commands. */
extern struct cmd_list_element *cmdlist;
@@ -117,8 +120,22 @@ extern void source_script (const char *, int);
/* Exported to objfiles.c. */
-extern int find_and_open_script (const char *file, int search_path,
- FILE **streamp, char **full_path);
+/* The script that was opened. */
+struct open_script
+{
+ gdb_file_up stream;
+ gdb::unique_xmalloc_ptr<char> full_path;
+
+ open_script (gdb_file_up &&stream_,
+ gdb::unique_xmalloc_ptr<char> &&full_path_)
+ : stream (std::move (stream_)),
+ full_path (std::move (full_path_))
+ {
+ }
+};
+
+extern gdb::optional<open_script>
+ find_and_open_script (const char *file, int search_path);
/* Command tracing state. */
diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 383adf8..1bbbe46 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -166,9 +166,9 @@ set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
void
set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
- completer_ftype_void *completer_handle_brkchars)
+ completer_handle_brkchars_ftype *func)
{
- cmd->completer_handle_brkchars = completer_handle_brkchars;
+ cmd->completer_handle_brkchars = func;
}
/* Add element named NAME.
@@ -244,7 +244,7 @@ add_cmd (const char *name, enum command_class theclass, cmd_cfunc_ftype *fun,
c->allow_unknown = 0;
c->prefix = NULL;
c->abbrev_flag = 0;
- set_cmd_completer (c, make_symbol_completion_list_fn);
+ set_cmd_completer (c, symbol_completer);
c->completer_handle_brkchars = NULL;
c->destroyer = NULL;
c->type = not_set_cmd;
@@ -657,8 +657,9 @@ add_setshow_optional_filename_cmd (const char *name, enum command_class theclass
/* Completes on literal "unlimited". Used by integer commands that
support a special "unlimited" value. */
-static VEC (char_ptr) *
+static void
integer_unlimited_completer (struct cmd_list_element *ignore,
+ completion_tracker &tracker,
const char *text, const char *word)
{
static const char * const keywords[] =
@@ -667,7 +668,7 @@ integer_unlimited_completer (struct cmd_list_element *ignore,
NULL,
};
- return complete_on_enum (keywords, text, word);
+ complete_on_enum (tracker, keywords, text, word);
}
/* Add element named NAME to both the set and show command LISTs (the
@@ -1771,13 +1772,13 @@ lookup_cmd_composition (const char *text,
"foo" and we want to complete to "foobar". If WORD is "oo", return
"oobar"; if WORD is "baz/foo", return "baz/foobar". */
-VEC (char_ptr) *
+void
complete_on_cmdlist (struct cmd_list_element *list,
+ completion_tracker &tracker,
const char *text, const char *word,
int ignore_help_classes)
{
struct cmd_list_element *ptr;
- VEC (char_ptr) *matchlist = NULL;
int textlen = strlen (text);
int pass;
int saw_deprecated_match = 0;
@@ -1786,8 +1787,10 @@ complete_on_cmdlist (struct cmd_list_element *list,
commands. If we see no matching commands in the first pass, and
if we did happen to see a matching deprecated command, we do
another loop to collect those. */
- for (pass = 0; matchlist == 0 && pass < 2; ++pass)
+ for (pass = 0; pass < 2; ++pass)
{
+ bool got_matches = false;
+
for (ptr = list; ptr; ptr = ptr->next)
if (!strncmp (ptr->name, text, textlen)
&& !ptr->abbrev_flag
@@ -1820,32 +1823,34 @@ complete_on_cmdlist (struct cmd_list_element *list,
match[text - word] = '\0';
strcat (match, ptr->name);
}
- VEC_safe_push (char_ptr, matchlist, match);
+ tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
+ got_matches = true;
}
+
+ if (got_matches)
+ break;
+
/* If we saw no matching deprecated commands in the first pass,
just bail out. */
if (!saw_deprecated_match)
break;
}
-
- return matchlist;
}
/* Helper function for SYMBOL_COMPLETION_FUNCTION. */
-/* Return a vector of char pointers which point to the different
- possible completions in CMD of TEXT.
+/* Add the different possible completions in ENUMLIST of TEXT.
WORD points in the same buffer as TEXT, and completions should be
returned relative to this position. For example, suppose TEXT is "foo"
and we want to complete to "foobar". If WORD is "oo", return
"oobar"; if WORD is "baz/foo", return "baz/foobar". */
-VEC (char_ptr) *
-complete_on_enum (const char *const *enumlist,
+void
+complete_on_enum (completion_tracker &tracker,
+ const char *const *enumlist,
const char *text, const char *word)
{
- VEC (char_ptr) *matchlist = NULL;
int textlen = strlen (text);
int i;
const char *name;
@@ -1870,10 +1875,8 @@ complete_on_enum (const char *const *enumlist,
match[text - word] = '\0';
strcat (match, name);
}
- VEC_safe_push (char_ptr, matchlist, match);
+ tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
}
-
- return matchlist;
}
diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 11248ba..50b858c 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -160,19 +160,7 @@ struct cmd_list_element
/* The prefix command of this command. */
struct cmd_list_element *prefix;
- /* Completion routine for this command. TEXT is the text beyond
- what was matched for the command itself (leading whitespace is
- skipped). It stops where we are supposed to stop completing
- (rl_point) and is '\0' terminated.
-
- Return value is a malloc'd vector of pointers to possible
- completions terminated with NULL. If there are no completions,
- returning a pointer to a NULL would work but returning NULL
- itself is also valid. WORD points in the same buffer as TEXT,
- and completions should be returned relative to this position.
- For example, suppose TEXT is "foo" and we want to complete to
- "foobar". If WORD is "oo", return "oobar"; if WORD is
- "baz/foo", return "baz/foobar". */
+ /* Completion routine for this command. */
completer_ftype *completer;
/* Handle the word break characters for this completer. Usually
@@ -181,8 +169,7 @@ struct cmd_list_element
a class) the word break chars may need to be redefined
depending on the completer type (e.g., for filename
completers). */
-
- completer_ftype_void *completer_handle_brkchars;
+ completer_handle_brkchars_ftype *completer_handle_brkchars;
/* Destruction routine for this command. If non-NULL, this is
called when this command instance is destroyed. This may be
diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c
index d6d4aab..30897d8 100644
--- a/gdb/cli/cli-dump.c
+++ b/gdb/cli/cli-dump.c
@@ -33,16 +33,11 @@
#include "filestuff.h"
#include "common/byte-vector.h"
-static const char *
-scan_expression_with_cleanup (const char **cmd, const char *def)
+static gdb::unique_xmalloc_ptr<char>
+scan_expression (const char **cmd, const char *def)
{
if ((*cmd) == NULL || (**cmd) == '\0')
- {
- char *exp = xstrdup (def);
-
- make_cleanup (xfree, exp);
- return exp;
- }
+ return gdb::unique_xmalloc_ptr<char> (xstrdup (def));
else
{
char *exp;
@@ -50,17 +45,16 @@ scan_expression_with_cleanup (const char **cmd, const char *def)
end = (*cmd) + strcspn (*cmd, " \t");
exp = savestring ((*cmd), end - (*cmd));
- make_cleanup (xfree, exp);
(*cmd) = skip_spaces_const (end);
- return exp;
+ return gdb::unique_xmalloc_ptr<char> (exp);
}
}
-static char *
-scan_filename_with_cleanup (const char **cmd, const char *defname)
+static gdb::unique_xmalloc_ptr<char>
+scan_filename (const char **cmd, const char *defname)
{
- char *filename;
+ gdb::unique_xmalloc_ptr<char> filename;
char *fullname;
/* FIXME: Need to get the ``/a(ppend)'' flag from somewhere. */
@@ -70,8 +64,7 @@ scan_filename_with_cleanup (const char **cmd, const char *defname)
{
if (defname == NULL)
error (_("Missing filename."));
- filename = xstrdup (defname);
- make_cleanup (xfree, filename);
+ filename.reset (xstrdup (defname));
}
else
{
@@ -80,27 +73,12 @@ scan_filename_with_cleanup (const char **cmd, const char *defname)
(*cmd) = skip_spaces_const (*cmd);
end = *cmd + strcspn (*cmd, " \t");
- filename = savestring ((*cmd), end - (*cmd));
- make_cleanup (xfree, filename);
+ filename.reset (savestring ((*cmd), end - (*cmd)));
(*cmd) = skip_spaces_const (end);
}
gdb_assert (filename != NULL);
- fullname = tilde_expand (filename);
- make_cleanup (xfree, fullname);
-
- return fullname;
-}
-
-static FILE *
-fopen_with_cleanup (const char *filename, const char *mode)
-{
- FILE *file = gdb_fopen_cloexec (filename, mode);
-
- if (file == NULL)
- perror_with_name (filename);
- make_cleanup_fclose (file);
- return file;
+ return gdb::unique_xmalloc_ptr<char> (tilde_expand (filename.get ()));
}
static gdb_bfd_ref_ptr
@@ -168,11 +146,10 @@ static void
dump_binary_file (const char *filename, const char *mode,
const bfd_byte *buf, ULONGEST len)
{
- FILE *file;
int status;
- file = fopen_with_cleanup (filename, mode);
- status = fwrite (buf, len, 1, file);
+ gdb_file_up file = gdb_fopen_cloexec (filename, mode);
+ status = fwrite (buf, len, 1, file.get ());
if (status != 1)
perror_with_name (filename);
}
@@ -201,28 +178,25 @@ dump_bfd_file (const char *filename, const char *mode,
static void
dump_memory_to_file (const char *cmd, const char *mode, const char *file_format)
{
- struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
CORE_ADDR lo;
CORE_ADDR hi;
ULONGEST count;
- const char *filename;
- const char *lo_exp;
const char *hi_exp;
/* Open the file. */
- filename = scan_filename_with_cleanup (&cmd, NULL);
+ gdb::unique_xmalloc_ptr<char> filename = scan_filename (&cmd, NULL);
/* Find the low address. */
if (cmd == NULL || *cmd == '\0')
error (_("Missing start address."));
- lo_exp = scan_expression_with_cleanup (&cmd, NULL);
+ gdb::unique_xmalloc_ptr<char> lo_exp = scan_expression (&cmd, NULL);
/* Find the second address - rest of line. */
if (cmd == NULL || *cmd == '\0')
error (_("Missing stop address."));
hi_exp = cmd;
- lo = parse_and_eval_address (lo_exp);
+ lo = parse_and_eval_address (lo_exp.get ());
hi = parse_and_eval_address (hi_exp);
if (hi <= lo)
error (_("Invalid memory address range (start >= end)."));
@@ -235,15 +209,9 @@ dump_memory_to_file (const char *cmd, const char *mode, const char *file_format)
/* Have everything. Open/write the data. */
if (file_format == NULL || strcmp (file_format, "binary") == 0)
- {
- dump_binary_file (filename, mode, buf.data (), count);
- }
+ dump_binary_file (filename.get (), mode, buf.data (), count);
else
- {
- dump_bfd_file (filename, mode, file_format, lo, buf.data (), count);
- }
-
- do_cleanups (old_cleanups);
+ dump_bfd_file (filename.get (), mode, file_format, lo, buf.data (), count);
}
static void
@@ -255,12 +223,10 @@ dump_memory_command (char *cmd, const char *mode)
static void
dump_value_to_file (const char *cmd, const char *mode, const char *file_format)
{
- struct cleanup *old_cleanups = make_cleanup (null_cleanup, NULL);
struct value *val;
- const char *filename;
/* Open the file. */
- filename = scan_filename_with_cleanup (&cmd, NULL);
+ gdb::unique_xmalloc_ptr<char> filename = scan_filename (&cmd, NULL);
/* Find the value. */
if (cmd == NULL || *cmd == '\0')
@@ -271,10 +237,8 @@ dump_value_to_file (const char *cmd, const char *mode, const char *file_format)
/* Have everything. Open/write the data. */
if (file_format == NULL || strcmp (file_format, "binary") == 0)
- {
- dump_binary_file (filename, mode, value_contents (val),
- TYPE_LENGTH (value_type (val)));
- }
+ dump_binary_file (filename.get (), mode, value_contents (val),
+ TYPE_LENGTH (value_type (val)));
else
{
CORE_ADDR vaddr;
@@ -289,12 +253,10 @@ dump_value_to_file (const char *cmd, const char *mode, const char *file_format)
warning (_("value is not an lval: address assumed to be zero"));
}
- dump_bfd_file (filename, mode, file_format, vaddr,
+ dump_bfd_file (filename.get (), mode, file_format, vaddr,
value_contents (val),
TYPE_LENGTH (value_type (val)));
}
-
- do_cleanups (old_cleanups);
}
static void
@@ -446,8 +408,6 @@ restore_section_callback (bfd *ibfd, asection *isec, void *args)
bfd_vma sec_end = sec_start + size;
bfd_size_type sec_offset = 0;
bfd_size_type sec_load_count = size;
- struct cleanup *old_chain;
- gdb_byte *buf;
int ret;
/* Ignore non-loadable sections, eg. from elf files. */
@@ -475,9 +435,8 @@ restore_section_callback (bfd *ibfd, asection *isec, void *args)
sec_load_count -= sec_end - data->load_end;
/* Get the data. */
- buf = (gdb_byte *) xmalloc (size);
- old_chain = make_cleanup (xfree, buf);
- if (!bfd_get_section_contents (ibfd, isec, buf, 0, size))
+ gdb::byte_vector buf (size);
+ if (!bfd_get_section_contents (ibfd, isec, buf.data (), 0, size))
error (_("Failed to read bfd file %s: '%s'."), bfd_get_filename (ibfd),
bfd_errmsg (bfd_get_error ()));
@@ -499,24 +458,21 @@ restore_section_callback (bfd *ibfd, asection *isec, void *args)
/* Write the data. */
ret = target_write_memory (sec_start + sec_offset + data->load_offset,
- buf + sec_offset, sec_load_count);
+ &buf[sec_offset], sec_load_count);
if (ret != 0)
warning (_("restore: memory write failed (%s)."), safe_strerror (ret));
- do_cleanups (old_chain);
- return;
}
static void
restore_binary_file (const char *filename, struct callback_data *data)
{
- struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
- FILE *file = fopen_with_cleanup (filename, FOPEN_RB);
+ gdb_file_up file = gdb_fopen_cloexec (filename, FOPEN_RB);
long len;
/* Get the file size for reading. */
- if (fseek (file, 0, SEEK_END) == 0)
+ if (fseek (file.get (), 0, SEEK_END) == 0)
{
- len = ftell (file);
+ len = ftell (file.get ());
if (len < 0)
perror_with_name (filename);
}
@@ -541,12 +497,12 @@ restore_binary_file (const char *filename, struct callback_data *data)
(unsigned long) (data->load_start + data->load_offset + len));
/* Now set the file pos to the requested load start pos. */
- if (fseek (file, data->load_start, SEEK_SET) != 0)
+ if (fseek (file.get (), data->load_start, SEEK_SET) != 0)
perror_with_name (filename);
/* Now allocate a buffer and read the file contents. */
gdb::byte_vector buf (len);
- if (fread (buf.data (), 1, len, file) != len)
+ if (fread (buf.data (), 1, len, file.get ()) != len)
perror_with_name (filename);
/* Now write the buffer into target memory. */
@@ -554,13 +510,11 @@ restore_binary_file (const char *filename, struct callback_data *data)
buf.data (), len);
if (len != 0)
warning (_("restore: memory write failed (%s)."), safe_strerror (len));
- do_cleanups (cleanup);
}
static void
restore_command (char *args_in, int from_tty)
{
- char *filename;
struct callback_data data;
bfd *ibfd;
int binary_flag = 0;
@@ -574,7 +528,7 @@ restore_command (char *args_in, int from_tty)
data.load_end = 0;
/* Parse the input arguments. First is filename (required). */
- filename = scan_filename_with_cleanup (&args, NULL);
+ gdb::unique_xmalloc_ptr<char> filename = scan_filename (&args, NULL);
if (args != NULL && *args != '\0')
{
static const char binary_string[] = "binary";
@@ -589,13 +543,13 @@ restore_command (char *args_in, int from_tty)
/* Parse offset (optional). */
if (args != NULL && *args != '\0')
data.load_offset = binary_flag ?
- parse_and_eval_address (scan_expression_with_cleanup (&args, NULL)) :
- parse_and_eval_long (scan_expression_with_cleanup (&args, NULL));
+ parse_and_eval_address (scan_expression (&args, NULL).get ()) :
+ parse_and_eval_long (scan_expression (&args, NULL).get ());
if (args != NULL && *args != '\0')
{
/* Parse start address (optional). */
data.load_start =
- parse_and_eval_long (scan_expression_with_cleanup (&args, NULL));
+ parse_and_eval_long (scan_expression (&args, NULL).get ());
if (args != NULL && *args != '\0')
{
/* Parse end address (optional). */
@@ -608,18 +562,18 @@ restore_command (char *args_in, int from_tty)
if (info_verbose)
printf_filtered ("Restore file %s offset 0x%lx start 0x%lx end 0x%lx\n",
- filename, (unsigned long) data.load_offset,
+ filename.get (), (unsigned long) data.load_offset,
(unsigned long) data.load_start,
(unsigned long) data.load_end);
if (binary_flag)
{
- restore_binary_file (filename, &data);
+ restore_binary_file (filename.get (), &data);
}
else
{
/* Open the file for loading. */
- gdb_bfd_ref_ptr ibfd (bfd_openr_or_error (filename, NULL));
+ gdb_bfd_ref_ptr ibfd (bfd_openr_or_error (filename.get (), NULL));
/* Process the sections. */
bfd_map_over_sections (ibfd.get (), restore_section_callback, &data);
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index e0e27ef..4b8ae0b 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -372,26 +372,12 @@ execute_cmd_post_hook (struct cmd_list_element *c)
}
}
-/* Execute the command in CMD. */
-static void
-do_restore_user_call_depth (void * call_depth)
-{
- int *depth = (int *) call_depth;
-
- (*depth)--;
- if ((*depth) == 0)
- in_user_command = 0;
-}
-
-
void
execute_user_command (struct cmd_list_element *c, char *args)
{
struct ui *ui = current_ui;
struct command_line *cmdlines;
- struct cleanup *old_chain;
enum command_control_type ret;
- static int user_call_depth = 0;
extern unsigned int max_user_call_depth;
cmdlines = c->user_commands;
@@ -401,23 +387,18 @@ execute_user_command (struct cmd_list_element *c, char *args)
scoped_user_args_level push_user_args (args);
- if (++user_call_depth > max_user_call_depth)
+ if (user_args_stack.size () > max_user_call_depth)
error (_("Max user call depth exceeded -- command aborted."));
- old_chain = make_cleanup (do_restore_user_call_depth, &user_call_depth);
-
/* Set the instream to 0, indicating execution of a
user-defined function. */
- make_cleanup (do_restore_instream_cleanup, ui->instream);
- ui->instream = NULL;
-
- /* Also set the global in_user_command, so that NULL instream is
- not confused with Insight. */
- in_user_command = 1;
+ scoped_restore restore_instream
+ = make_scoped_restore (&ui->instream, nullptr);
scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
- command_nest_depth++;
+ scoped_restore save_nesting
+ = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
while (cmdlines)
{
ret = execute_control_command (cmdlines);
@@ -428,8 +409,6 @@ execute_user_command (struct cmd_list_element *c, char *args)
}
cmdlines = cmdlines->next;
}
- command_nest_depth--;
- do_cleanups (old_chain);
}
/* This function is called every time GDB prints a prompt. It ensures
@@ -548,9 +527,9 @@ execute_control_command (struct command_line *cmd)
current = *cmd->body_list;
while (current)
{
- command_nest_depth++;
+ scoped_restore save_nesting
+ = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
ret = execute_control_command (current);
- command_nest_depth--;
/* If we got an error, or a "break" command, then stop
looping. */
@@ -607,9 +586,9 @@ execute_control_command (struct command_line *cmd)
/* Execute commands in the given arm. */
while (current)
{
- command_nest_depth++;
+ scoped_restore save_nesting
+ = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
ret = execute_control_command (current);
- command_nest_depth--;
/* If we got an error, get out. */
if (ret != simple_control)
@@ -952,6 +931,8 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
const char *cmd_name = p;
struct cmd_list_element *cmd
= lookup_cmd_1 (&cmd_name, cmdlist, NULL, 1);
+ cmd_name = skip_spaces_const (cmd_name);
+ bool inline_cmd = *cmd_name != '\0';
/* If commands are parsed, we skip initial spaces. Otherwise,
which is the case for Python commands and documentation
@@ -997,21 +978,20 @@ process_next_line (char *p, struct command_line **command, int parse_commands,
{
*command = build_command_line (commands_control, line_first_arg (p));
}
- else if (command_name_equals (cmd, "python"))
+ else if (command_name_equals (cmd, "python") && !inline_cmd)
{
/* Note that we ignore the inline "python command" form
here. */
*command = build_command_line (python_control, "");
}
- else if (command_name_equals (cmd, "compile"))
+ else if (command_name_equals (cmd, "compile") && !inline_cmd)
{
/* Note that we ignore the inline "compile command" form
here. */
*command = build_command_line (compile_control, "");
(*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
}
-
- else if (command_name_equals (cmd, "guile"))
+ else if (command_name_equals (cmd, "guile") && !inline_cmd)
{
/* Note that we ignore the inline "guile command" form here. */
*command = build_command_line (guile_control, "");
@@ -1584,58 +1564,34 @@ document_command (char *comname, int from_tty)
}
}
-struct source_cleanup_lines_args
-{
- int old_line;
- const char *old_file;
-};
-
-static void
-source_cleanup_lines (void *args)
-{
- struct source_cleanup_lines_args *p =
- (struct source_cleanup_lines_args *) args;
-
- source_line_number = p->old_line;
- source_file_name = p->old_file;
-}
-
/* Used to implement source_command. */
void
script_from_file (FILE *stream, const char *file)
{
- struct cleanup *old_cleanups;
- struct source_cleanup_lines_args old_lines;
-
if (stream == NULL)
internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
- old_lines.old_line = source_line_number;
- old_lines.old_file = source_file_name;
- old_cleanups = make_cleanup (source_cleanup_lines, &old_lines);
- source_line_number = 0;
- source_file_name = file;
-
- {
- scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
+ scoped_restore restore_line_number
+ = make_scoped_restore (&source_line_number, 0);
+ scoped_restore resotre_file
+ = make_scoped_restore (&source_file_name, file);
- TRY
- {
- read_command_file (stream);
- }
- CATCH (e, RETURN_MASK_ERROR)
- {
- /* Re-throw the error, but with the file name information
- prepended. */
- throw_error (e.error,
- _("%s:%d: Error in sourced command file:\n%s"),
- source_file_name, source_line_number, e.message);
- }
- END_CATCH
- }
+ scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
- do_cleanups (old_cleanups);
+ TRY
+ {
+ read_command_file (stream);
+ }
+ CATCH (e, RETURN_MASK_ERROR)
+ {
+ /* Re-throw the error, but with the file name information
+ prepended. */
+ throw_error (e.error,
+ _("%s:%d: Error in sourced command file:\n%s"),
+ source_file_name, source_line_number, e.message);
+ }
+ END_CATCH
}
/* Print the definition of user command C to STREAM. Or, if C is a