From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id yqUKMqOOyWhW0gYAWB0awg (envelope-from ) for ; Tue, 16 Sep 2025 12:21:55 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=V1hMnjbQ; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id B204B1E0BA; Tue, 16 Sep 2025 12:21:55 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 06BEB1E04C for ; Tue, 16 Sep 2025 12:21:55 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 91959385AC20 for ; Tue, 16 Sep 2025 16:21:54 +0000 (GMT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id C2CB93857733 for ; Tue, 16 Sep 2025 16:21:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C2CB93857733 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C2CB93857733 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758039692; cv=none; b=stmtTIDLOyncHZtxBFIFUl5t883tVkzuT31sHY3h1eUOz4PEuGMB0Dtmhoqht/2jXjJUla+4fmI0quvX4IxTV/1HeT3Lj8z8X/A81zqMjlWFM82YBa7AicNR4gogyGwlY4sSzZFCRoMhnF3rYWULM97wpHQJN0CXr6N3HUAhypo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758039692; c=relaxed/simple; bh=3pmYOR1DlZdHcgdykZ1EwDSXPqxZBTmJOMNavbeh6Jc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Ejq3mBi36noVsbDVOL86bPvIPt0L6JN2KSiQIvIQ8nkn7z30Jb1KXM6ZOkZvCKssk6TbqgBGSzFBO7Hx3bhacdgkye3FRFfmJ6UqYnlmLbd1bA51gXdLlBwcwMHPhTg2Jx0rciqKgf/JUHEhfMa0z/xHTwwA7RruCKxPvey1ovQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C2CB93857733 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=V1hMnjbQ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758039689; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Mif7Un8npkCBPt6DnvJD5qByy7bHsLfGnKUgohkhIik=; b=V1hMnjbQdbunFhYqrwPcsDXUgAW/4Ikz1ny2OxIFeTXpfiBU2Pz7X3TcxKXL1QNv3r7qD5 ig1cTO+AJVvy+tc9f7tW88haLepQkpQNY58farMkjvxMETD7BLIzZQqo7ERCfw1qxi4lBG M0e/00yY7VWp858Bz7rxCQbS1sC35ws= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-653-ywWDerOrPL6OqTNZSVfxNg-1; Tue, 16 Sep 2025 12:21:27 -0400 X-MC-Unique: ywWDerOrPL6OqTNZSVfxNg-1 X-Mimecast-MFC-AGG-ID: ywWDerOrPL6OqTNZSVfxNg_1758039686 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-3ecdd61b798so20328f8f.1 for ; Tue, 16 Sep 2025 09:21:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758039686; x=1758644486; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Mif7Un8npkCBPt6DnvJD5qByy7bHsLfGnKUgohkhIik=; b=nw8zXGZoJMoOmhieDhs2BtXB6EKpCBK5noH70azfT5kCanxoH3onJTfeP5B+nKnsyq yLt+/B5po1blKMxa1ocZlaRoFbvLzYPCoMJLIn6AXLqKcbNpX5HoYw/6AU6nUsbi3gGQ EK99qp85qXIAUGfhO8girJdvnbjOLXT/0j3hIfnccqkgpmdKKnsQElej5kA1MRgRGztL izdV813O8ZUzBFb1n7/yCkn2plqhLmp8FNkWLC4+EB2x7GEjo9BbNz/DCFXNVhxD++uV MUijspFWLpS7tH+Cc5huydxVOxivru4Qx//XXkVzopTobXiY8Z60wz31cxU7pR/akMRO Lfdw== X-Forwarded-Encrypted: i=1; AJvYcCWucaHQ2KsMVOK2KtTbgtceios+gq3NIWCm0jZWD4FkvuRYE78Ed2rbhntkRWMHoU/CdEPBfGaOauoULw==@sourceware.org X-Gm-Message-State: AOJu0Yzz1jjEe3OotmdGmA2gkQ29MISaUF2kgVX7Fi8t74iJn6YkLSDm 7S3FrWzMo/lhDgM430857z7vQnLAkkLkXA6Bsd84MvTphnx180LVGMUJILfK1i+HfjtSxeJyJBE ihkI/OKZ1nPgt+E8xfdrPDCPwiytdwMqWkCLyCIa1XyYpwZCjJXZqrxVrroGj+EQ= X-Gm-Gg: ASbGncvM0jJsUWkTL0M0EEq5VQTpQVevZv7WAJ+0gLhOjlZyy96ng3ie2K86gTh1VIj D6Alco/Lw4wuA3Hs2Lgo2obzD65Kuyzu4Y63QjbB58FczUCKeDyCUb4AtTBSysF2Bm204XmjBzE 1aSF4TQa0v6vqIFASGXDafFpOxt0SqWvxpiAbP8avtusRf3Xc+/i3KeEQ3H+xQRaX9JbggC/yku wIRm1MEjgkWa36HkNRTKp3rkOXNRc19Op7FGDa5XTpC5tTpjzpO48GfDQ1fQdtli/hBvAZYV6t5 Pc0AbIScFufnPZhJMf/bbmkvuOmdNqMvFDs= X-Received: by 2002:a05:6000:24c8:b0:3ec:d7c4:25d5 with SMTP id ffacd0b85a97d-3ecd7c42923mr2629055f8f.50.1758039686112; Tue, 16 Sep 2025 09:21:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHXhJCuWHPtIyY8tN85NBAvQZxEBcYMGX4w1zkDQBy4mJdhxM8TDOPOMOVD/U2MeMkM10/5Wg== X-Received: by 2002:a05:6000:24c8:b0:3ec:d7c4:25d5 with SMTP id ffacd0b85a97d-3ecd7c42923mr2629024f8f.50.1758039685626; Tue, 16 Sep 2025 09:21:25 -0700 (PDT) Received: from localhost ([31.111.84.207]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3ecdd4d15ecsm204319f8f.11.2025.09.16.09.21.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Sep 2025 09:21:25 -0700 (PDT) From: Andrew Burgess To: Luis Cc: Kevin Buettner , gdb-patches@sourceware.org, Michael Weghorn , Eli Zaretskii , Guinevere Larsen Subject: Re: [PATCH] gdb/gdbserver: pass inferior arguments as a single string In-Reply-To: References: <20250624145052.4659642b@f41-zbm-amd> <874it8htc2.fsf@redhat.com> Date: Tue, 16 Sep 2025 17:21:24 +0100 Message-ID: <87ikhigygr.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: rZM5uR-CKWE57PW_N4TrACnkIuVOhRdkHtD-q_n7RG0_1758039686 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org Luis writes: > I think I'm running into a change in behavior for --args, where it now says > no program was specified when previously it was ok. > > Just a standard.... > > ./gdb/gdb -ex run --args gdb/gdb gdb/gdb -ex start > > Did something change in terms of invocation format? Here's a proper fix, with tests. Let me know what you think. Thanks, Andrew --- commit 5dfd9f9da130c5deae9ac81fcda1bd18f8951eac Author: Andrew Burgess Date: Tue Sep 16 17:02:01 2025 +0100 gdb: fix --args handling when inferior argument have dash After the commit: commit e5e76451fa82e0bc00599af96382b361c3d6ac32 Date: Fri Oct 22 07:19:29 2021 +0000 gdb/gdbserver: add a '--no-escape-args' command line option Inferior argument handling on the GDB command line was broken: $ gdb --args /bin/ls --foo ./gdb/gdb: unrecognized option '--foo' ./gdb/gdb: `--args' specified but no program specified Before the above patch the definition of the '--args' argument in the long_options array (in captured_main_1) was such that the getopt_long_only call would directly set the 'set_args' variable to true if '--args' was seen. This meant that, immediately after the getopt_long_only call, we could inspect set_args and break out of the argument processing loop if needed. After the above patch '--args' (and the new '--no-escape-args') no longer set set_args directly via the getopt_long_only call. Instead the getopt_long_only call returns an OPT_* enum value, which we then use in the following switch statement in order to set the set_args variable. What this means is that, immediately after the getopt_long_only call, set_args no longer (immediately) indicates if --args was seen. After the switch statement, when set_args has been updated, we go around the argument processing loop again and call getopt_long_only once more. This extra getopt_long_only call will, if it finds another argument that starts with a dash, update the global optind to point to this option. At this point things have gone wrong, GDB has now lost track of the argument containing the program name the user wanted us to start. This leads to GDB existing with the above error. The solution is to move the check of set_args to either before the getopt_long_only call, or to after the switch statement. I chose to move it earlier as this keeps all the loop exiting checks near the beginning. I've added more tests that cover this issue. diff --git a/gdb/main.c b/gdb/main.c index 04c33638bec..f3049600a06 100644 --- a/gdb/main.c +++ b/gdb/main.c @@ -866,9 +866,14 @@ captured_main_1 (struct captured_main_args *context) { int option_index; + /* If the previous argument was --args or --no-escape-args, then + stop argument processing. */ + if (set_args != NO_ARGS) + break; + c = getopt_long_only (argc, argv, "", long_options, &option_index); - if (c == EOF || set_args != NO_ARGS) + if (c == EOF) break; /* Long option that takes an argument. */ diff --git a/gdb/testsuite/gdb.base/args.exp b/gdb/testsuite/gdb.base/args.exp index 573543cc1f8..03e5e57ee20 100644 --- a/gdb/testsuite/gdb.base/args.exp +++ b/gdb/testsuite/gdb.base/args.exp @@ -186,6 +186,18 @@ proc run_all_tests {} { set ::env(TEST) "ABCD" args_test "shell variable" {{$TEST}} {\\$TEST} {{ABCD}} } + + # At one point we had a bug where, if the last inferior argument, + # appearing after --args, looked like an option GDB might be able + # to process, e.g. started with a dash, then GDB would try to + # process it. This would mean all leave GDB in a broken state, + # and so GDB would fail to start. + foreach_with_prefix flag { args no-escape-args } { + args_test "with trailing GDB flag" [list "--${flag}"] + } + args_test "with trailing GDB option and value" [list "--ex" "start"] + args_test "with trailing double dash option" [list "--foo"] + args_test "with trailing single dash option" [list "-foo"] } run_all_tests