From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id HejBNyDyyWi6MwcAWB0awg (envelope-from ) for ; Tue, 16 Sep 2025 19:26:24 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Am6+c3CU; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id CF6951E047; Tue, 16 Sep 2025 19:26:24 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,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 2B2941E047 for ; Tue, 16 Sep 2025 19:26:24 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 97B6E3858C42 for ; Tue, 16 Sep 2025 23:26:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 97B6E3858C42 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Am6+c3CU Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id 67F433858D33 for ; Tue, 16 Sep 2025 23:25:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 67F433858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 67F433858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::329 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758065151; cv=none; b=QqPNWFqh/13wpr3wGD00IPsHMSf4txG3rdDmUDK4txEfqwz3FfKCCeCvXXAYEJsmhA+wTaNzfwpiEg8SQgI4esOPbrws2olqpLMm3TQ4tHwPI//Kz1rDXinzCL/QZmSP46snNgv3nDyWkcOLEoHSS0hCVwD3n45MjaPh5Yb5xgo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1758065151; c=relaxed/simple; bh=zQxYPg9PDcfq5zSETIaJOGmHE3c67g9UM51ThBWE2xs=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=KBbh9Yr3SXuFkSlVDHMU2zPq9cPZye4SjC4BG6BJaYyw6BFWdqSuIvrmKV4RZx9hx2NtRxx92SIslnRw3bGqfEBiaJ48lnE7TT861tOnB4FDVc5Wwf75NTWZWTl7wGOX9ncJY+GU3HG1ScW5ZfE+O7B5yPB6e8hN+VhtlaOp+Tg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67F433858D33 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-45decc9e83eso66186215e9.3 for ; Tue, 16 Sep 2025 16:25:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758065150; x=1758669950; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=svIZxc+r+t12OFIoI0dDnPfVd1R5A1+Xmno/kYSnECM=; b=Am6+c3CUFj0DcIy7a9+nvbDKV8I6nKdpMBSrVoZTCfOhz/pJOfwFTbEyC4UX6XD8hu PCJgFHoMI2cb7VUlZu85BgWfRZb4fep93jGAn8m0V+aPMJaryNRpFtxKLgYPqnR+Tl2m A8poNfRBJOBbyhoqaw6vZRWPvbkAl8DVMNAsXiLHp6CPXnheBXxOb8MiTKlhVyBhu7IS luK/3A5gS+m2ox00JUvpvkYuCgUwv/rQCwU6lEtULciewCmbr1wtOcamVuY97df1hZY+ mRPnOy67+24dcJB1JnwCwed0Ew6yBKmp4fHBxJDPiNFbsso8IdLEoLt1Jfaml3uoolq6 rSBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758065150; x=1758669950; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=svIZxc+r+t12OFIoI0dDnPfVd1R5A1+Xmno/kYSnECM=; b=wNzgckynsa6N+DcFD7t+Y4dxpaMauClXNMRlH02cN8MlRqgjNF+IiTfYJuNvLv5v0Z XiFBbleDP9z2pwGx3j2AvGs01/386lNCrpOuVC0jpqIkf8C67WJO7xP5XkEHv6Z5qxWf k5MdxjmqJcwKIIZ2GoJdTuw2sggCKSya7OeojgsLXRClCt9CQY3UWFEC31S/pEnpvUse NAB2kqgs9HTtoEZyifRGbY2ZebCL7W7XnfaaGi5RKGvdyXcHtjyaPKy7uAcQxXkbqAGm YST4s+lmgG2ntHVtnBd9Pux9J1b+mk0+uHLyCkEA3MBS8DTzYua3XYag0CLMHknIg9ls wszw== X-Forwarded-Encrypted: i=1; AJvYcCUnDoKOv0mBuDSO7UpnTJkPJOmu86TBjeHty+lYu4xFADD9qRWCWR3uSAISER08XbbtitExxKrfqMkqYA==@sourceware.org X-Gm-Message-State: AOJu0YzQcvBS5dl9nCQwv+6P5KUbhBAsjaVU0fjlWBbZC10R5A4sQzdA 2mH9ZGoyRGZ9j+n7gEUuof2jAhnxov+YjM3AlBwLtVhXxIiI2VperxPU X-Gm-Gg: ASbGnctNmn3Piv1a7p281tQhlRw8tSTTCA2zWhCILH5VXErSQ2WIymxuoyIBCBACMxV /l+fw4tRXoDHLPBX7dWsdBhxGzJ8DxnEJvEABqaM7LbBv1lZJhTIyVVlE4X65vVs3hE9bZQ40pP xC0nJkugc2fHwBethGl8+viUXea3DsANbHgR9N+Ej2t8fe0UdMd9/2IGHFHBHsj4PbsthW7+W6z p7FxsvKGu9Z4vr4CJ1UNI2VSyjNkKXrVDAEVX8/BqAXeBVSqbMRfjtX6G7EuGQQhrIiICW8LeXb XjQfl2r2e3n7tHWCayhfWTHKu74gLNFBr+0qAhc3JNrFjgTvpY/E2eBbk5bgK7PD+AV5hP0IP9C r8X88ieh7cXbznRRCF7VRRbTI5SrR1THoK2XEbYz2v1F8dg== X-Google-Smtp-Source: AGHT+IGaTr9GralJ7XYM8AkqBkHrMAWPuxNEsSdNOf2mDKswh5YxgcVbFdjKH3o0ydEmIgJ0Eyhszw== X-Received: by 2002:a05:600c:4691:b0:45b:73be:c707 with SMTP id 5b1f17b1804b1-46205cc87bfmr339795e9.22.1758065149757; Tue, 16 Sep 2025 16:25:49 -0700 (PDT) Received: from [192.168.0.38] ([86.12.216.189]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-461371847d0sm12263785e9.2.2025.09.16.16.25.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 16 Sep 2025 16:25:49 -0700 (PDT) Message-ID: Date: Wed, 17 Sep 2025 00:25:48 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] gdb/gdbserver: pass inferior arguments as a single string To: Andrew Burgess Cc: Kevin Buettner , gdb-patches@sourceware.org, Michael Weghorn , Eli Zaretskii , Guinevere Larsen References: <20250624145052.4659642b@f41-zbm-amd> <874it8htc2.fsf@redhat.com> <87ikhigygr.fsf@redhat.com> Content-Language: en-US From: Luis In-Reply-To: <87ikhigygr.fsf@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 Hi Andrew, On 16/09/2025 17:21, Andrew Burgess wrote: > 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. Excellent. Thanks for the quick fix. I thought I was misremembering things a bit. Let me give it a go. > > 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 >