From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 158A13858016 for ; Tue, 9 Jan 2024 14:26:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 158A13858016 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 158A13858016 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704810421; cv=none; b=MDSVB0xLB9LQ5uaYHkYtotDBEgz0VHfshgsU1MMHsraMhW1IZXh0an36jwpFP3cOKZlH+i+Qp52/qCMz5D3JKpFgv3P2cO3ius43Edi4roXZv1POLemnzudjdLQHisoMkLJFZRG3SkYM0W+NNtBxL4oijLjiG7k0OH0ukyHmy84= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704810421; c=relaxed/simple; bh=B/YgFAg3XCAhvfiBWu0tp9VwyaKgaCuKaUWLgzbrVYg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=hGPNRH2Z1YiuAMdvdP0terJy2o5kw8TBAufLkqRtDF7BN4KcE+4GDrd3SdoI/2CLup1FL9pNW49KR8RFLkj5F45XTvCiqFhRSm6boFLza1FNmv6mMjfOepgDtdEC88xZfnRJP+28gV6fezDr4v1zJy60xnuhBDWXmjKsTAJ7x1Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704810417; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lOh7qo/gql9Xr3IgNHSse0+UZKYXDbo2mAZtm45aU2Y=; b=K+Rkfj98gN3dCIcKwrXbb1h7j+5aqWJ+/CIxezPVRx2HPAjMdcMh/sZD/IeE6gCfg9BwEE oInxfq0PFPg4iyjch/ogKMmvxNxZaodxdX+C5WBhLL7caVQN80DQxh/pUhv1Rcv6Ilc5Dr KlUf6/d2mIELgaugUzgHr1NnBYhyubc= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-168-dpa2_MO4NHGqRRFoDPzVEw-1; Tue, 09 Jan 2024 09:26:51 -0500 X-MC-Unique: dpa2_MO4NHGqRRFoDPzVEw-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-3367c893deeso1788223f8f.2 for ; Tue, 09 Jan 2024 06:26:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704810409; x=1705415209; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lOh7qo/gql9Xr3IgNHSse0+UZKYXDbo2mAZtm45aU2Y=; b=T83YXe2H1OhkTP1AHxLR7aYRIO+XAgUWV+oSS/x2HPj3IDJbJRCQUyG8bjKjk1m8Ga ONVznnuwmr7/hVbcZOlgSKt3rG6vBXTX5PenKilrI2Cer1tWc/Fymp54FjFHdWYp5ae6 lPC7bNs93rXA5H+rwSBHvGmdE4ayQdQWLFw7oyNsQLFoqtiGcfqByo/AeDbp+ymtlOi+ GxDWztcWfArPDN13Yt822D4+fpWnHJMYcFYdaLYPljSFPd6KIHTsJVbGBkzWpRHs3+mf /iTwbUK+L7anqR+SFI2xR+8wQMNqCrEpYCMesHUk6OdRYXNh1zX/0pORIdeiXno5ZL98 Vc0A== X-Gm-Message-State: AOJu0YwLHGhTBsMoaTSYZuWTEGW0PmQlUl4qa+U1+Fj8RD/r/wfb+2AP 3HJLE6nieuFhRIU2Cr88UkAJKQPe56wyRZa0mgVDvBlezq52ocjrcO3GDaIsfyB+Zv8cy9JrJi6 Am03aT4dSoh7Q8wEt1W/UBpD8vJNcPwBURPxUV8cKICb9S3F2r6xka/BfidCXszrySOsc4Qnm0N liGyHprAsUAMFjXQ== X-Received: by 2002:a5d:4386:0:b0:336:8638:b3f5 with SMTP id i6-20020a5d4386000000b003368638b3f5mr537973wrq.93.1704810409548; Tue, 09 Jan 2024 06:26:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IFxGzdhodfzueFVGFltvQno8oNLSQ6pqkt9B0aIR01Swh8tVTkOwlVAV3foqHFIVgFw1fbg+w== X-Received: by 2002:a5d:4386:0:b0:336:8638:b3f5 with SMTP id i6-20020a5d4386000000b003368638b3f5mr537962wrq.93.1704810409166; Tue, 09 Jan 2024 06:26:49 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id h16-20020a05600004d000b003367ff4aadasm2555328wri.31.2024.01.09.06.26.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 06:26:48 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Michael Weghorn Subject: [PATCH 05/16] gdbserver: convert program_args to a single string Date: Tue, 9 Jan 2024 14:26:28 +0000 Message-Id: <7f1f0fbb51a1b0284548f6ad62ac53692a388f2d.1704809585.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This commit changes how gdbserver stores the inferior arguments from being a vector of separate arguments into a single string with all of the arguments combined together. Making this change might feel a little strange; intuitively it feels like we would be better off storing the arguments as a vector, but in the context of the upcoming patches, this change makes things easier. First, GDB already stores the inferior arguments as a single string, so doing this moves gdbserver into line with GDB. The common code into which gdbserver calls requires the arguments to be a single string, so currently each target's create_inferior implementation merged the arguments anyway, so all this commit really does is move the merging up the call stack, and store the merged result rather than storing the separate parts. However, the biggest reason for why this commit is needed, is an issue with passing arguments from GDB to gdbserver when starting a new inferior. Consider: (gdb) set args $VAR (gdb) run ... When using a native target the inferior will see the value of $VAR expanded by the shell GDB uses to start the inferior. However, if using an extended-remote target the inferior will see literally $VAR, the unexpanded name of the variable, the reason for this is that, although GDB sends '$VAR' to gdbserver, when gdbserver receives this, it converts this to '\$VAR', which prevents the variable from being expanded by the shell. The reason for this is that construct_inferior_arguments escapes all special shell characters within its arguments, and it is construct_inferior_arguments that is used to combine the separate arguments into a single string. In a later commit I will change construct_inferior_arguments so that it can apply different escaping strategies. When this happens we will want to escape arguments coming from the gdbserver command line differently than arguments coming from GDB (via a vRun packet), which means we need to call construct_inferior_arguments earlier, at the point where we know if the arguments came from the gdbserver command line, or from the vRun packet. This argument escaping issue is discussed in PR gdb/28392. This commit doesn't fix any issues, nor does it change construct_inferior_arguments to actually do different escaping, that will all come in a later commit. This is purely a restructuring. There should be no user visible changes after this commit. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28392 --- gdbserver/linux-low.cc | 5 ++--- gdbserver/linux-low.h | 2 +- gdbserver/netbsd-low.cc | 6 ++---- gdbserver/netbsd-low.h | 2 +- gdbserver/server.cc | 24 +++++++++++++++++++----- gdbserver/target.h | 6 +++--- gdbserver/win32-low.cc | 7 +++---- gdbserver/win32-low.h | 2 +- 8 files changed, 32 insertions(+), 22 deletions(-) diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc index 4aa011c14ec..640c9f330c7 100644 --- a/gdbserver/linux-low.cc +++ b/gdbserver/linux-low.cc @@ -986,7 +986,7 @@ linux_ptrace_fun () int linux_process_target::create_inferior (const char *program, - const std::vector &program_args) + const std::string &program_args) { client_state &cs = get_client_state (); struct lwp_info *new_lwp; @@ -996,10 +996,9 @@ linux_process_target::create_inferior (const char *program, { maybe_disable_address_space_randomization restore_personality (cs.disable_randomization); - std::string str_program_args = construct_inferior_arguments (program_args); pid = fork_inferior (program, - str_program_args.c_str (), + program_args.c_str (), get_environ ()->envp (), linux_ptrace_fun, NULL, NULL, NULL, NULL); } diff --git a/gdbserver/linux-low.h b/gdbserver/linux-low.h index 51d1899893a..a8d7a93aaeb 100644 --- a/gdbserver/linux-low.h +++ b/gdbserver/linux-low.h @@ -141,7 +141,7 @@ class linux_process_target : public process_stratum_target public: int create_inferior (const char *program, - const std::vector &program_args) override; + const std::string &program_args) override; void post_create_inferior () override; diff --git a/gdbserver/netbsd-low.cc b/gdbserver/netbsd-low.cc index 10d8d280b98..0fce43299ac 100644 --- a/gdbserver/netbsd-low.cc +++ b/gdbserver/netbsd-low.cc @@ -79,11 +79,9 @@ netbsd_ptrace_fun () int netbsd_process_target::create_inferior (const char *program, - const std::vector &program_args) + const std::string &program_args) { - std::string str_program_args = construct_inferior_arguments (program_args); - - pid_t pid = fork_inferior (program, str_program_args.c_str (), + pid_t pid = fork_inferior (program, program_args.c_str (), get_environ ()->envp (), netbsd_ptrace_fun, nullptr, nullptr, nullptr, nullptr); diff --git a/gdbserver/netbsd-low.h b/gdbserver/netbsd-low.h index 050b43fc54f..d62fbcf4c7a 100644 --- a/gdbserver/netbsd-low.h +++ b/gdbserver/netbsd-low.h @@ -42,7 +42,7 @@ class netbsd_process_target : public process_stratum_target public: int create_inferior (const char *program, - const std::vector &program_args) override; + const std::string &program_args) override; void post_create_inferior () override; diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 2ec97d7ade4..508e42ee097 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -122,7 +122,20 @@ private: /* The program name, adjusted if needed. */ std::string m_path; } program_path; -static std::vector program_args; + +/* All program arguments are merged into a single string. This is similar + to how GDB manages the inferior arguments, and actually makes our lives + easier; the rules for how arguments are merged into a single string + differ depending on where the arguments come from. Arguments arriving + form the gdbserver command line are quoted, while arguments arriving + from GDB (via a vRun packet) are already quoted. + + NOTE: The comment above is ahead of its time. The differences between + how the PROGRAM_ARGS string is built up have not yet been implemented. + A later patch in this series will make this change, and remove this + note. */ +static std::string program_args; + static std::string wrapper_argv; /* The PID of the originally created or attached inferior. Used to @@ -3424,9 +3437,8 @@ handle_v_run (char *own_buf) else program_path.set (new_program_name.get ()); - /* Free the old argv and install the new one. */ - free_vector_argv (program_args); - program_args = new_argv; + program_args = construct_inferior_arguments (new_argv); + free_vector_argv (new_argv); target_create_inferior (program_path.get (), program_args); @@ -4304,8 +4316,10 @@ captured_main (int argc, char *argv[]) n = argc - (next_arg - argv); program_path.set (next_arg[0]); + std::vector temp_arg_vector; for (i = 1; i < n; i++) - program_args.push_back (xstrdup (next_arg[i])); + temp_arg_vector.push_back (next_arg[i]); + program_args = construct_inferior_arguments (temp_arg_vector); /* Wait till we are at first instruction in program. */ target_create_inferior (program_path.get (), program_args); diff --git a/gdbserver/target.h b/gdbserver/target.h index 28d134e7915..5757aa6b84e 100644 --- a/gdbserver/target.h +++ b/gdbserver/target.h @@ -77,13 +77,13 @@ class process_stratum_target /* Start a new process. PROGRAM is a path to the program to execute. - PROGRAM_ARGS is a standard NULL-terminated array of arguments, - to be passed to the inferior as ``argv'' (along with PROGRAM). + PROGRAM_ARGS is a string containing all of the arguments that will be + used to start the inferior. Returns the new PID on success, -1 on failure. Registers the new process with the process list. */ virtual int create_inferior (const char *program, - const std::vector &program_args) = 0; + const std::string &program_args) = 0; /* Do additional setup after a new process is created, including exec-wrapper completion. */ diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc index 90d9510c7b9..e991152a29e 100644 --- a/gdbserver/win32-low.cc +++ b/gdbserver/win32-low.cc @@ -512,12 +512,12 @@ create_process (const char *program, char *args, /* Start a new process. PROGRAM is the program name. - PROGRAM_ARGS is the vector containing the inferior's args. + PROGRAM_ARGS is a string containing all the inferior's arguments. Returns the new PID on success, -1 on failure. Registers the new process with the process list. */ int win32_process_target::create_inferior (const char *program, - const std::vector &program_args) + const std::string &program_args) { client_state &cs = get_client_state (); #ifndef USE_WIN32API @@ -528,8 +528,7 @@ win32_process_target::create_inferior (const char *program, DWORD flags; PROCESS_INFORMATION pi; DWORD err; - std::string str_program_args = construct_inferior_arguments (program_args); - char *args = (char *) str_program_args.c_str (); + char *args = (char *) program_args.c_str (); /* win32_wait needs to know we're not attaching. */ windows_process.attaching = 0; diff --git a/gdbserver/win32-low.h b/gdbserver/win32-low.h index 6a0b8cf9c84..1526567bfad 100644 --- a/gdbserver/win32-low.h +++ b/gdbserver/win32-low.h @@ -94,7 +94,7 @@ class win32_process_target : public process_stratum_target public: int create_inferior (const char *program, - const std::vector &program_args) override; + const std::string &program_args) override; int attach (unsigned long pid) override; -- 2.25.4