From df3ee9ca894f7e831713c332aa7820a6463c2435 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Fri, 2 Dec 2016 19:17:14 +0000 Subject: [PATCH] Support an "unlimited" number of user-defined arguments New in v2: - A few adjustments / simplifications were possible now that we require C++11: . Use std::unique_ptr to make the user_args_stack std::vector own its elements: static std::vector> user_args_stack; . use vector::emplace_back to construct elements directly in the corresponding vectors. . use std::to_string instead of adding a gdb::to_string replacement. - Now includes a test. Docs/NEWS are unchanged from v1 and have already been approved. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I recently wrote a user-defined command that could benefit from supporting an unlimited number of arguments: http://palves.net/list-active-signal-handlers-with-gdb/ E.g., 'info signal-dispositions 1 2 3 4 5 6 7 8 9 10 11' However, we currently only support up to 10 arguments passed to user-defined commands ($arg0..$arg9). I can't find a good reason for that, other than "old code with hard coded limits". This patch removes that limit and modernizes the code along the way: - Makes the user_args struct a real C++ class that uses std::vector for storage. - Removes the "next" pointer from within user_args and uses a std::vector to maintain a stack instead. - Adds a new RAII-based scoped_user_args_level class to help push/pop user args in the stack instead of using a cleanup. gdb/ChangeLog: 2016-12-02 Pedro Alves * NEWS: Mention that user commands now accept an unlimited number of arguments. * cli/cli-script.c: Include . (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. gdb/doc/ChangeLog: 2016-12-02 Pedro Alves * gdb.texinfo (User-defined Commands): Limit on number of arguments passed to user-defined commands removed; update. gdb/testsuite/ChangeLog: 2016-12-02 Pedro Alves * gdb.base/commands.exp (user_defined_command_manyargs_test): New procedure. (top level): Call it. --- gdb/ChangeLog | 20 +++ gdb/NEWS | 3 + gdb/cli/cli-script.c | 181 +++++++++++++++------------- gdb/doc/ChangeLog | 5 + gdb/doc/gdb.texinfo | 6 +- gdb/testsuite/ChangeLog | 6 + gdb/testsuite/gdb.base/commands.exp | 44 +++++++ 7 files changed, 181 insertions(+), 84 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 2066268be1..777abc7c30 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,23 @@ +2016-12-02 Pedro Alves + + * NEWS: Mention that user commands now accept an unlimited number + of arguments. + * cli/cli-script.c: Include . + (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 PR cli/20559 diff --git a/gdb/NEWS b/gdb/NEWS index 408912443e..f8c91cf0d7 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 15947a9a78..b19e550d99 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 + /* 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 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> 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 &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 18564b6549..1bf50f1769 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,8 @@ +2016-12-02 Pedro Alves + + * gdb.texinfo (User-defined Commands): Limit on number of + arguments passed to user-defined commands removed; update. + 2016-12-02 Pedro Alves PR cli/20559 diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index f4dfac2047..a0de7d1b49 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 bfae18bf1d..fff79c2440 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2016-12-02 Pedro Alves + + * gdb.base/commands.exp (user_defined_command_manyargs_test): New + procedure. + (top level): Call it. + 2016-12-02 Pedro Alves * gdb.base/commands.exp (user_defined_command_args_stack_test): diff --git a/gdb/testsuite/gdb.base/commands.exp b/gdb/testsuite/gdb.base/commands.exp index ed85ee29e6..85b0bf7af4 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 -- 2.34.1