From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id YOznEelNKGB1AgAAWB0awg (envelope-from ) for ; Sat, 13 Feb 2021 17:08:41 -0500 Received: by simark.ca (Postfix, from userid 112) id 3730D1EF78; Sat, 13 Feb 2021 17:08:40 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 3B6811E789 for ; Sat, 13 Feb 2021 17:08:40 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA95F3896830; Sat, 13 Feb 2021 22:08:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA95F3896830 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1613254119; bh=7gWEod7mPbygtsw7VasNVgDEM8Mwn7otyI1GCBzp6wM=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=so7IG5VVjb2C93iEXzj9GIvmu52MRSPG2Wt56JjC5AxCHjKBImIBAOT0BYpv6kYN3 D24/Ao/VbXUrZ3uX6++hh4Pe9EQt6Krh0bhHh5TB1OlE4MNRzm36ySenpUQwxgP63w SSDoXoISJh9iTgXnlbSHpzjNiVngotwjX+AW1KfA= Received: from beryx.lancelotsix.com (beryx.lancelotsix.com [IPv6:2001:41d0:401:3000::1ab3]) by sourceware.org (Postfix) with ESMTPS id 0128A3896C07 for ; Sat, 13 Feb 2021 22:08:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0128A3896C07 Received: from Plymouth.lan (unknown [IPv6:2a02:390:8443:0:2199:d918:170f:6cbf]) by beryx.lancelotsix.com (Postfix) with ESMTPSA id 098422E007; Sat, 13 Feb 2021 23:08:35 +0100 (CET) To: gdb-patches@sourceware.org Subject: [PATCH 3/3] Add completer to the add-inferior command Date: Sat, 13 Feb 2021 22:07:52 +0000 Message-Id: <20210213220752.32581-4-lsix@lancelotsix.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210213220752.32581-1-lsix@lancelotsix.com> References: <20210213220752.32581-1-lsix@lancelotsix.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.5.11 (beryx.lancelotsix.com [0.0.0.0]); Sat, 13 Feb 2021 23:08:36 +0100 (CET) X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Lancelot SIX via Gdb-patches Reply-To: Lancelot SIX Cc: Lancelot SIX Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" This commit adds a completer to the add-inferior command. Argument parsing is also updated to benefit from what is offered by the gdb::option framework. gdb/Changelog: * inferior.c (struct add_inferior_options): Add struct. (add_inferior_options_defs): Create. (make_add_inferior_options_def_group): Create. (add_inferior_completer): Create. (add_inferior_command): Use gdb::option parsing for arguments. (initialize_inferiors): Use new completer for add-inferior command. gdb/testsuite/ChangeLog: * gdb.base/completion.exp: Test add-completion completion. --- gdb/inferior.c | 123 ++++++++++++++++++-------- gdb/testsuite/gdb.base/completion.exp | 11 +++ 2 files changed, 96 insertions(+), 38 deletions(-) diff --git a/gdb/inferior.c b/gdb/inferior.c index 49f869a4c78..6993d043a57 100644 --- a/gdb/inferior.c +++ b/gdb/inferior.c @@ -788,63 +788,110 @@ switch_to_inferior_and_push_target (inferior *new_inf, printf_filtered (_("Added inferior %d\n"), new_inf->num); } +/* Describes the options for the add-inferior command. */ + +struct add_inferior_options +{ + /* Number new inferiors to create. */ + unsigned int copies = 1; + + /* Instructs to start the inferior with no target connected. */ + bool no_connection = false; + + /* Path to the file name of the executable to use as main program. */ + char *exec = nullptr; + + ~add_inferior_options () + { + if (exec != nullptr) + { + xfree (exec); + exec = nullptr; + } + } +}; + +/* Definition of options for the 'add-inferior' command. */ + +static const gdb::option::option_def add_inferior_options_defs[] = { + gdb::option::zuinteger_option_def { + "copies", + [] (add_inferior_options *opt) { return &opt->copies; }, + nullptr, /* show_cmd_cb */ + nullptr, /* set_doc */ + nullptr, /* show_doc */ + nullptr, /* help_doc */ + }, + + gdb::option::flag_option_def { + "no-connection", + [] (add_inferior_options *opt) { return &opt->no_connection; }, + nullptr, /* set_doc */ + nullptr, /* help_doc */ + }, + + gdb::option::filename_option_def { + "exec", + [] (add_inferior_options *opt) { return &opt->exec; }, + nullptr, /* show_cmd_cb */ + nullptr, /* set_doc */ + nullptr, /* show_doc */ + nullptr, /* help_doc */ + }, + +}; + +static gdb::option::option_def_group +make_add_inferior_options_def_group (struct add_inferior_options *opts) +{ + return {{add_inferior_options_defs}, opts}; +} + +/* Completer for the add-inferior command. */ + +static void +add_inferior_completer (struct cmd_list_element *cmd, + completion_tracker &tracker, + const char *text, const char * /*word*/) +{ + const auto group = make_add_inferior_options_def_group (nullptr); + if (gdb::option::complete_options + (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group)) + return; +} + /* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */ static void add_inferior_command (const char *args, int from_tty) { - int i, copies = 1; + int i; gdb::unique_xmalloc_ptr exec; symfile_add_flags add_flags = 0; - bool no_connection = false; + + add_inferior_options opts; + const gdb::option::option_def_group group + = make_add_inferior_options_def_group (&opts); + gdb::option::process_options + (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group); if (from_tty) add_flags |= SYMFILE_VERBOSE; - if (args) - { - gdb_argv built_argv (args); - - for (char **argv = built_argv.get (); *argv != NULL; argv++) - { - if (**argv == '-') - { - if (strcmp (*argv, "-copies") == 0) - { - ++argv; - if (!*argv) - error (_("No argument to -copies")); - copies = parse_and_eval_long (*argv); - } - else if (strcmp (*argv, "-no-connection") == 0) - no_connection = true; - else if (strcmp (*argv, "-exec") == 0) - { - ++argv; - if (!*argv) - error (_("No argument to -exec")); - exec.reset (tilde_expand (*argv)); - } - } - else - error (_("Invalid argument")); - } - } - inferior *orginf = current_inferior (); scoped_restore_current_pspace_and_thread restore_pspace_thread; - for (i = 0; i < copies; ++i) + for (i = 0; i < opts.copies; ++i) { inferior *inf = add_inferior_with_spaces (); - switch_to_inferior_and_push_target (inf, no_connection, orginf); + switch_to_inferior_and_push_target (inf, opts.no_connection, orginf); - if (exec != NULL) + if (opts.filename != nullptr) { - exec_file_attach (exec.get (), from_tty); - symbol_file_add_main (exec.get (), add_flags); + exec_file_attach (opts.filename, from_tty); + symbol_file_add_main (opts.filename, add_flags); } } } @@ -997,7 +1044,7 @@ as main program.\n\ By default, the new inferior inherits the current inferior's connection.\n\ If -no-connection is specified, the new inferior begins with\n\ no target connection yet.")); - set_cmd_completer (c, filename_completer); + set_cmd_completer_handle_brkchars (c, add_inferior_completer); add_com ("remove-inferiors", no_class, remove_inferior_command, _("\ Remove inferior ID (or list of IDs).\n\ diff --git a/gdb/testsuite/gdb.base/completion.exp b/gdb/testsuite/gdb.base/completion.exp index 076134cdc33..c83aa8994e2 100644 --- a/gdb/testsuite/gdb.base/completion.exp +++ b/gdb/testsuite/gdb.base/completion.exp @@ -980,3 +980,14 @@ test_gdb_complete_unique "xxx_yyy_" "xxx_yyy_zzz" gdb_test_no_output "alias set aaa_bbb_ccc=set debug" gdb_test_no_output "maint deprecate set aaa_bbb_ccc" test_gdb_complete_unique "set aaa_bbb_" "set aaa_bbb_ccc" + +# Test the completer of the add-inferior command. +gdb_test_no_output "set max-completions unlimited" +test_gdb_complete_multiple "add-inferior " "-" "" { + "-copies" + "-exec" + "-no-connection" +} +test_gdb_complete_unique \ + "add-inferior -copies 2 -exec some\\ file -n" \ + "add-inferior -copies 2 -exec some\\ file -no-connection" -- 2.29.2