aboutsummaryrefslogtreecommitdiff
path: root/gdb
diff options
context:
space:
mode:
Diffstat (limited to 'gdb')
-rw-r--r--gdb/ChangeLog20
-rw-r--r--gdb/NEWS3
-rw-r--r--gdb/cli/cli-script.c181
-rw-r--r--gdb/doc/ChangeLog5
-rw-r--r--gdb/doc/gdb.texinfo6
-rw-r--r--gdb/testsuite/ChangeLog6
-rw-r--r--gdb/testsuite/gdb.base/commands.exp44
7 files changed, 181 insertions, 84 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 2066268..777abc7 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,25 @@
2016-12-02 Pedro Alves <palves@redhat.com>
+ * NEWS: Mention that user commands now accept an unlimited number
+ of arguments.
+ * cli/cli-script.c: Include <vector>.
+ (struct string_view): New type.
+ (MAXUSERARGS): Delete.
+ (struct user_args): Now a C++ class.
+ (user_args_stack): New.
+ (struct scoped_user_args_level): New type.
+ (execute_user_command): Use scoped_user_args_level.
+ (arg_cleanup): Delete.
+ (setup_user_args): Deleted, and refactored as ...
+ (user_args::user_args): ... this new constructor. Limit of number
+ of arguments removed.
+ (insert_user_defined_cmd_args): Defer to user_args_stack.
+ (user_args::insert_args): New, bits based on old
+ insert_user_defined_cmd_args with limit of number of arguments
+ eliminated.
+
+2016-12-02 Pedro Alves <palves@redhat.com>
+
PR cli/20559
* NEWS: Mention "eval" expands user-defined command arguments.
* cli/cli-script.c (execute_control_command): Adjust to rename.
diff --git a/gdb/NEWS b/gdb/NEWS
index 4089124..f8c91cf 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -32,6 +32,9 @@
* Support for Java programs compiled with gcj has been removed.
+* User commands now accept an unlimited number of arguments.
+ Previously, only up to 10 was accepted.
+
* The "eval" command now expands user-defined command arguments.
This makes it easier to process a variable number of arguments:
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index 15947a9..b19e550 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -33,6 +33,8 @@
#include "interps.h"
#include "compile/compile.h"
+#include <vector>
+
/* Prototypes for local functions. */
static enum command_control_type
@@ -41,8 +43,6 @@ recurse_read_control_structure (char * (*read_next_line_func) (void),
void (*validator)(char *, void *),
void *closure);
-static struct cleanup * setup_user_args (char *p);
-
static char *read_next_line (void);
/* Level of control structure when reading. */
@@ -54,24 +54,68 @@ static int command_nest_depth = 1;
/* This is to prevent certain commands being printed twice. */
static int suppress_next_print_command_trace = 0;
+/* A non-owning slice of a string. */
+
+struct string_view
+{
+ string_view (const char *str_, size_t len_)
+ : str (str_), len (len_)
+ {}
+
+ const char *str;
+ size_t len;
+};
+
/* Structure for arguments to user defined functions. */
-#define MAXUSERARGS 10
-struct user_args
+
+class user_args
+{
+public:
+ /* Save the command line and store the locations of arguments passed
+ to the user defined function. */
+ explicit user_args (const char *line);
+
+ /* Insert the stored user defined arguments into the $arg arguments
+ found in LINE. */
+ std::string insert_args (const char *line) const;
+
+private:
+ /* Disable copy/assignment. (Since the elements of A point inside
+ COMMAND, copying would need to reconstruct the A vector in the
+ new copy.) */
+ user_args (const user_args &) =delete;
+ user_args &operator= (const user_args &) =delete;
+
+ /* It is necessary to store a copy of the command line to ensure
+ that the arguments are not overwritten before they are used. */
+ std::string m_command_line;
+
+ /* The arguments. Each element points inside M_COMMAND_LINE. */
+ std::vector<string_view> m_args;
+};
+
+/* The stack of arguments passed to user defined functions. We need a
+ stack because user-defined functions can call other user-defined
+ functions. */
+static std::vector<std::unique_ptr<user_args>> user_args_stack;
+
+/* An RAII-base class used to push/pop args on the user args
+ stack. */
+struct scoped_user_args_level
+{
+ /* Parse the command line and push the arguments in the user args
+ stack. */
+ explicit scoped_user_args_level (const char *line)
{
- struct user_args *next;
- /* It is necessary to store a malloced copy of the command line to
- ensure that the arguments are not overwritten before they are
- used. */
- char *command;
- struct
- {
- char *arg;
- int len;
- }
- a[MAXUSERARGS];
- int count;
+ user_args_stack.emplace_back (new user_args (line));
}
- *user_args;
+
+ /* Pop the current user arguments from the stack. */
+ ~scoped_user_args_level ()
+ {
+ user_args_stack.pop_back ();
+ }
+};
/* Return non-zero if TYPE is a multi-line command (i.e., is terminated
@@ -362,12 +406,12 @@ execute_user_command (struct cmd_list_element *c, char *args)
/* Null command */
return;
- old_chain = setup_user_args (args);
+ scoped_user_args_level push_user_args (args);
if (++user_call_depth > max_user_call_depth)
error (_("Max user call depth exceeded -- command aborted."));
- make_cleanup (do_restore_user_call_depth, &user_call_depth);
+ old_chain = make_cleanup (do_restore_user_call_depth, &user_call_depth);
/* Set the instream to 0, indicating execution of a
user-defined function. */
@@ -668,62 +712,32 @@ if_command (char *arg, int from_tty)
free_command_lines (&command);
}
-/* Cleanup */
-static void
-arg_cleanup (void *ignore)
-{
- struct user_args *oargs = user_args;
-
- if (!user_args)
- internal_error (__FILE__, __LINE__,
- _("arg_cleanup called with no user args.\n"));
-
- user_args = user_args->next;
- xfree (oargs->command);
- xfree (oargs);
-}
-
-/* Bind the incomming arguments for a user defined command to
- $arg0, $arg1 ... $argMAXUSERARGS. */
+/* Bind the incoming arguments for a user defined command to $arg0,
+ $arg1 ... $argN. */
-static struct cleanup *
-setup_user_args (char *p)
+user_args::user_args (const char *command_line)
{
- struct user_args *args;
- struct cleanup *old_chain;
- unsigned int arg_count = 0;
-
- args = XNEW (struct user_args);
- memset (args, 0, sizeof (struct user_args));
-
- args->next = user_args;
- user_args = args;
-
- old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
+ const char *p;
- if (p == NULL)
- return old_chain;
+ if (command_line == NULL)
+ return;
- user_args->command = p = xstrdup (p);
+ m_command_line = command_line;
+ p = m_command_line.c_str ();
while (*p)
{
- char *start_arg;
+ const char *start_arg;
int squote = 0;
int dquote = 0;
int bsquote = 0;
- if (arg_count >= MAXUSERARGS)
- error (_("user defined function may only have %d arguments."),
- MAXUSERARGS);
-
/* Strip whitespace. */
while (*p == ' ' || *p == '\t')
p++;
/* P now points to an argument. */
start_arg = p;
- user_args->a[arg_count].arg = p;
/* Get to the end of this argument. */
while (*p)
@@ -757,11 +771,8 @@ setup_user_args (char *p)
}
}
- user_args->a[arg_count].len = p - start_arg;
- arg_count++;
- user_args->count++;
+ m_args.emplace_back (start_arg, p - start_arg);
}
- return old_chain;
}
/* Given character string P, return a point to the first argument
@@ -787,40 +798,48 @@ insert_user_defined_cmd_args (const char *line)
{
/* If we are not in a user-defined command, treat $argc, $arg0, et
cetera as normal convenience variables. */
- if (user_args == NULL)
+ if (user_args_stack.empty ())
return line;
+ const std::unique_ptr<user_args> &args = user_args_stack.back ();
+ return args->insert_args (line);
+}
+
+/* Insert the user defined arguments stored in user_args into the $arg
+ arguments found in line. */
+
+std::string
+user_args::insert_args (const char *line) const
+{
std::string new_line;
const char *p;
while ((p = locate_arg (line)))
{
- int i, len;
-
new_line.append (line, p - line);
if (p[4] == 'c')
{
- gdb_assert (user_args->count >= 0 && user_args->count <= 10);
- if (user_args->count == 10)
- {
- new_line += '1';
- new_line += '0';
- }
- else
- new_line += user_args->count + '0';
+ new_line += std::to_string (m_args.size ());
+ line = p + 5;
}
else
{
- i = p[4] - '0';
- if (i >= user_args->count)
- error (_("Missing argument %d in user function."), i);
-
- len = user_args->a[i].len;
- if (len > 0)
- new_line.append (user_args->a[i].arg, len);
+ char *tmp;
+ unsigned long i;
+
+ errno = 0;
+ i = strtoul (p + 4, &tmp, 10);
+ if ((i == 0 && tmp == p + 4) || errno != 0)
+ line = p + 4;
+ else if (i >= m_args.size ())
+ error (_("Missing argument %ld in user function."), i);
+ else
+ {
+ new_line.append (m_args[i].str, m_args[i].len);
+ line = tmp;
+ }
}
- line = p + 5;
}
/* Don't forget the tail. */
new_line.append (line);
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 18564b6..1bf50f1 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,5 +1,10 @@
2016-12-02 Pedro Alves <palves@redhat.com>
+ * gdb.texinfo (User-defined Commands): Limit on number of
+ arguments passed to user-defined commands removed; update.
+
+2016-12-02 Pedro Alves <palves@redhat.com>
+
PR cli/20559
* gdb.texinfo (Define): Add example of using "eval" to process a
variable number of arguments.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index f4dfac2..a0de7d1 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -24057,9 +24057,9 @@ files.
@cindex arguments, to user-defined commands
A @dfn{user-defined command} is a sequence of @value{GDBN} commands to
which you assign a new name as a command. This is done with the
-@code{define} command. User commands may accept up to 10 arguments
+@code{define} command. User commands may accept an unlimited number of arguments
separated by whitespace. Arguments are accessed within the user command
-via @code{$arg0@dots{}$arg9}. A trivial example:
+via @code{$arg0@dots{}$argN}. A trivial example:
@smallexample
define adder
@@ -24083,7 +24083,7 @@ functions calls.
@cindex argument count in user-defined commands
@cindex how many arguments (user-defined commands)
In addition, @code{$argc} may be used to find out how many arguments have
-been passed. This expands to a number in the range 0@dots{}10.
+been passed.
@smallexample
define adder
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index bfae18b..fff79c2 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,11 @@
2016-12-02 Pedro Alves <palves@redhat.com>
+ * gdb.base/commands.exp (user_defined_command_manyargs_test): New
+ procedure.
+ (top level): Call it.
+
+2016-12-02 Pedro Alves <palves@redhat.com>
+
* gdb.base/commands.exp (user_defined_command_args_stack_test):
New procedure.
(top level): Call it.
diff --git a/gdb/testsuite/gdb.base/commands.exp b/gdb/testsuite/gdb.base/commands.exp
index ed85ee2..85b0bf7 100644
--- a/gdb/testsuite/gdb.base/commands.exp
+++ b/gdb/testsuite/gdb.base/commands.exp
@@ -440,6 +440,49 @@ proc_with_prefix user_defined_command_args_stack_test {} {
gdb_test "args_stack_command 31 32 33" $expected "execute command"
}
+# Test a simple user defined command with many arguments. GDB <= 7.12
+# used to have a hard coded limit of 10 arguments.
+
+proc_with_prefix user_defined_command_manyargs_test {} {
+ global gdb_prompt
+
+ set test "define command"
+ gdb_test_multiple "define manyargs" $test {
+ -re "End with" {
+ pass $test
+ }
+ }
+
+ # Define a function that doubles its arguments.
+ gdb_test \
+ [multi_line \
+ {printf "nargs=%d:", $argc} \
+ {set $i = 0} \
+ {while $i < $argc} \
+ { eval "printf \" %%d\", 2 * $arg%d\n", $i} \
+ { set $i = $i + 1} \
+ {end} \
+ {printf "\n"} \
+ {end}] \
+ "" \
+ "enter commands"
+
+ # Some random number of arguments, as long as higher than 10.
+ set nargs 100
+
+ set cmd "manyargs"
+ for {set i 1} {$i <= $nargs} {incr i} {
+ append cmd " $i"
+ }
+
+ set expected "nargs=$nargs:"
+ for {set i 1} {$i <= $nargs} {incr i} {
+ append expected " " [expr 2 * $i]
+ }
+
+ gdb_test $cmd $expected "execute command"
+}
+
proc_with_prefix watchpoint_command_test {} {
global gdb_prompt
@@ -972,6 +1015,7 @@ breakpoint_command_test
user_defined_command_test
user_defined_command_args_eval
user_defined_command_args_stack_test
+user_defined_command_manyargs_test
watchpoint_command_test
test_command_prompt_position
deprecated_command_test